blob: 7b46af3e014f2e4ea12ef8e5d798c4b806d0807e [file] [log] [blame]
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001/* linux/drivers/usb/gadget/s3c-hsotg.c
2 *
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09003 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01006 * Copyright 2008 Openmoko, Inc.
7 * Copyright 2008 Simtec Electronics
8 * Ben Dooks <ben@simtec.co.uk>
9 * http://armlinux.simtec.co.uk/
10 *
11 * S3C USB2.0 High-speed / OtG driver
12 *
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License version 2 as
15 * published by the Free Software Foundation.
16*/
17
18#include <linux/kernel.h>
19#include <linux/module.h>
20#include <linux/spinlock.h>
21#include <linux/interrupt.h>
22#include <linux/platform_device.h>
23#include <linux/dma-mapping.h>
24#include <linux/debugfs.h>
25#include <linux/seq_file.h>
26#include <linux/delay.h>
27#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Maurus Cuelenaeree50bf382010-07-19 09:40:50 +010029#include <linux/clk.h>
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020030#include <linux/regulator/consumer.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010031
32#include <linux/usb/ch9.h>
33#include <linux/usb/gadget.h>
34
35#include <mach/map.h>
36
Lukasz Majewski127d42a2012-05-04 14:16:59 +020037#include "s3c-hsotg.h"
38#include <linux/platform_data/s3c-hsotg.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010039
40#define DMA_ADDR_INVALID (~((dma_addr_t)0))
41
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020042static const char * const s3c_hsotg_supply_names[] = {
43 "vusb_d", /* digital USB supply, 1.2V */
44 "vusb_a", /* analog USB supply, 1.1V */
45};
46
Ben Dooks5b7d70c2009-06-02 14:58:06 +010047/* EP0_MPS_LIMIT
48 *
49 * Unfortunately there seems to be a limit of the amount of data that can
Lucas De Marchi25985ed2011-03-30 22:57:33 -030050 * be transferred by IN transactions on EP0. This is either 127 bytes or 3
51 * packets (which practically means 1 packet and 63 bytes of data) when the
Ben Dooks5b7d70c2009-06-02 14:58:06 +010052 * MPS is set to 64.
53 *
54 * This means if we are wanting to move >127 bytes of data, we need to
55 * split the transactions up, but just doing one packet at a time does
56 * not work (this may be an implicit DATA0 PID on first packet of the
57 * transaction) and doing 2 packets is outside the controller's limits.
58 *
59 * If we try to lower the MPS size for EP0, then no transfers work properly
60 * for EP0, and the system will fail basic enumeration. As no cause for this
61 * has currently been found, we cannot support any large IN transfers for
62 * EP0.
63 */
64#define EP0_MPS_LIMIT 64
65
66struct s3c_hsotg;
67struct s3c_hsotg_req;
68
69/**
70 * struct s3c_hsotg_ep - driver endpoint definition.
71 * @ep: The gadget layer representation of the endpoint.
72 * @name: The driver generated name for the endpoint.
73 * @queue: Queue of requests for this endpoint.
74 * @parent: Reference back to the parent device structure.
75 * @req: The current request that the endpoint is processing. This is
76 * used to indicate an request has been loaded onto the endpoint
77 * and has yet to be completed (maybe due to data move, or simply
78 * awaiting an ack from the core all the data has been completed).
79 * @debugfs: File entry for debugfs file for this endpoint.
80 * @lock: State lock to protect contents of endpoint.
81 * @dir_in: Set to true if this endpoint is of the IN direction, which
82 * means that it is sending data to the Host.
83 * @index: The index for the endpoint registers.
84 * @name: The name array passed to the USB core.
85 * @halted: Set if the endpoint has been halted.
86 * @periodic: Set if this is a periodic ep, such as Interrupt
87 * @sent_zlp: Set if we've sent a zero-length packet.
88 * @total_data: The total number of data bytes done.
89 * @fifo_size: The size of the FIFO (for periodic IN endpoints)
90 * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
91 * @last_load: The offset of data for the last start of request.
92 * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
93 *
94 * This is the driver's state for each registered enpoint, allowing it
95 * to keep track of transactions that need doing. Each endpoint has a
96 * lock to protect the state, to try and avoid using an overall lock
97 * for the host controller as much as possible.
98 *
99 * For periodic IN endpoints, we have fifo_size and fifo_load to try
100 * and keep track of the amount of data in the periodic FIFO for each
101 * of these as we don't have a status register that tells us how much
Ben Dookse7a9ff52010-07-19 09:40:42 +0100102 * is in each of them. (note, this may actually be useless information
103 * as in shared-fifo mode periodic in acts like a single-frame packet
104 * buffer than a fifo)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100105 */
106struct s3c_hsotg_ep {
107 struct usb_ep ep;
108 struct list_head queue;
109 struct s3c_hsotg *parent;
110 struct s3c_hsotg_req *req;
111 struct dentry *debugfs;
112
113 spinlock_t lock;
114
115 unsigned long total_data;
116 unsigned int size_loaded;
117 unsigned int last_load;
118 unsigned int fifo_load;
119 unsigned short fifo_size;
120
121 unsigned char dir_in;
122 unsigned char index;
123
124 unsigned int halted:1;
125 unsigned int periodic:1;
126 unsigned int sent_zlp:1;
127
128 char name[10];
129};
130
131#define S3C_HSOTG_EPS (8+1) /* limit to 9 for the moment */
132
133/**
134 * struct s3c_hsotg - driver state.
135 * @dev: The parent device supplied to the probe function
136 * @driver: USB gadget driver
137 * @plat: The platform specific configuration data.
138 * @regs: The memory area mapped for accessing registers.
139 * @regs_res: The resource that was allocated when claiming register space.
140 * @irq: The IRQ number we are using
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200141 * @supplies: Definition of USB power supplies
Ben Dooks10aebc72010-07-19 09:40:44 +0100142 * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100143 * @debug_root: root directrory for debugfs.
144 * @debug_file: main status file for debugfs.
145 * @debug_fifo: FIFO status file for debugfs.
146 * @ep0_reply: Request used for ep0 reply.
147 * @ep0_buff: Buffer for EP0 reply data, if needed.
148 * @ctrl_buff: Buffer for EP0 control requests.
149 * @ctrl_req: Request for EP0 control packets.
Lukasz Majewski71225be2012-05-04 14:17:03 +0200150 * @setup: NAK management for EP0 SETUP
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100151 * @eps: The endpoints being supplied to the gadget framework
152 */
153struct s3c_hsotg {
154 struct device *dev;
155 struct usb_gadget_driver *driver;
156 struct s3c_hsotg_plat *plat;
157
158 void __iomem *regs;
159 struct resource *regs_res;
160 int irq;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +0200161 struct clk *clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100162
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200163 struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
164
Ben Dooks10aebc72010-07-19 09:40:44 +0100165 unsigned int dedicated_fifos:1;
166
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100167 struct dentry *debug_root;
168 struct dentry *debug_file;
169 struct dentry *debug_fifo;
170
171 struct usb_request *ep0_reply;
172 struct usb_request *ctrl_req;
173 u8 ep0_buff[8];
174 u8 ctrl_buff[8];
175
176 struct usb_gadget gadget;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200177 unsigned int setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100178 struct s3c_hsotg_ep eps[];
179};
180
181/**
182 * struct s3c_hsotg_req - data transfer request
183 * @req: The USB gadget request
184 * @queue: The list of requests for the endpoint this is queued for.
185 * @in_progress: Has already had size/packets written to core
186 * @mapped: DMA buffer for this request has been mapped via dma_map_single().
187 */
188struct s3c_hsotg_req {
189 struct usb_request req;
190 struct list_head queue;
191 unsigned char in_progress;
192 unsigned char mapped;
193};
194
195/* conversion functions */
196static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
197{
198 return container_of(req, struct s3c_hsotg_req, req);
199}
200
201static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
202{
203 return container_of(ep, struct s3c_hsotg_ep, ep);
204}
205
206static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
207{
208 return container_of(gadget, struct s3c_hsotg, gadget);
209}
210
211static inline void __orr32(void __iomem *ptr, u32 val)
212{
213 writel(readl(ptr) | val, ptr);
214}
215
216static inline void __bic32(void __iomem *ptr, u32 val)
217{
218 writel(readl(ptr) & ~val, ptr);
219}
220
221/* forward decleration of functions */
222static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
223
224/**
225 * using_dma - return the DMA status of the driver.
226 * @hsotg: The driver state.
227 *
228 * Return true if we're using DMA.
229 *
230 * Currently, we have the DMA support code worked into everywhere
231 * that needs it, but the AMBA DMA implementation in the hardware can
232 * only DMA from 32bit aligned addresses. This means that gadgets such
233 * as the CDC Ethernet cannot work as they often pass packets which are
234 * not 32bit aligned.
235 *
236 * Unfortunately the choice to use DMA or not is global to the controller
237 * and seems to be only settable when the controller is being put through
238 * a core reset. This means we either need to fix the gadgets to take
239 * account of DMA alignment, or add bounce buffers (yuerk).
240 *
241 * Until this issue is sorted out, we always return 'false'.
242 */
243static inline bool using_dma(struct s3c_hsotg *hsotg)
244{
245 return false; /* support is not complete */
246}
247
248/**
249 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
250 * @hsotg: The device state
251 * @ints: A bitmask of the interrupts to enable
252 */
253static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
254{
255 u32 gsintmsk = readl(hsotg->regs + S3C_GINTMSK);
256 u32 new_gsintmsk;
257
258 new_gsintmsk = gsintmsk | ints;
259
260 if (new_gsintmsk != gsintmsk) {
261 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
262 writel(new_gsintmsk, hsotg->regs + S3C_GINTMSK);
263 }
264}
265
266/**
267 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
268 * @hsotg: The device state
269 * @ints: A bitmask of the interrupts to enable
270 */
271static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
272{
273 u32 gsintmsk = readl(hsotg->regs + S3C_GINTMSK);
274 u32 new_gsintmsk;
275
276 new_gsintmsk = gsintmsk & ~ints;
277
278 if (new_gsintmsk != gsintmsk)
279 writel(new_gsintmsk, hsotg->regs + S3C_GINTMSK);
280}
281
282/**
283 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
284 * @hsotg: The device state
285 * @ep: The endpoint index
286 * @dir_in: True if direction is in.
287 * @en: The enable value, true to enable
288 *
289 * Set or clear the mask for an individual endpoint's interrupt
290 * request.
291 */
292static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
293 unsigned int ep, unsigned int dir_in,
294 unsigned int en)
295{
296 unsigned long flags;
297 u32 bit = 1 << ep;
298 u32 daint;
299
300 if (!dir_in)
301 bit <<= 16;
302
303 local_irq_save(flags);
304 daint = readl(hsotg->regs + S3C_DAINTMSK);
305 if (en)
306 daint |= bit;
307 else
308 daint &= ~bit;
309 writel(daint, hsotg->regs + S3C_DAINTMSK);
310 local_irq_restore(flags);
311}
312
313/**
314 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
315 * @hsotg: The device instance.
316 */
317static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
318{
Ben Dooks0f002d22010-05-25 05:36:50 +0100319 unsigned int ep;
320 unsigned int addr;
321 unsigned int size;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100322 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100323 u32 val;
324
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100325 /* the ryu 2.6.24 release ahs
326 writel(0x1C0, hsotg->regs + S3C_GRXFSIZ);
327 writel(S3C_GNPTXFSIZ_NPTxFStAddr(0x200) |
328 S3C_GNPTXFSIZ_NPTxFDep(0x1C0),
329 hsotg->regs + S3C_GNPTXFSIZ);
330 */
331
Ben Dooks6d091ee2010-07-19 09:40:40 +0100332 /* set FIFO sizes to 2048/1024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100333
334 writel(2048, hsotg->regs + S3C_GRXFSIZ);
335 writel(S3C_GNPTXFSIZ_NPTxFStAddr(2048) |
Ben Dooks6d091ee2010-07-19 09:40:40 +0100336 S3C_GNPTXFSIZ_NPTxFDep(1024),
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100337 hsotg->regs + S3C_GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100338
339 /* arange all the rest of the TX FIFOs, as some versions of this
340 * block have overlapping default addresses. This also ensures
341 * that if the settings have been changed, then they are set to
342 * known values. */
343
344 /* start at the end of the GNPTXFSIZ, rounded up */
345 addr = 2048 + 1024;
346 size = 768;
347
348 /* currently we allocate TX FIFOs for all possible endpoints,
349 * and assume that they are all the same size. */
350
Anton Tikhomirovf7a83fe2012-03-06 14:05:49 +0900351 for (ep = 1; ep <= 15; ep++) {
Ben Dooks0f002d22010-05-25 05:36:50 +0100352 val = addr;
353 val |= size << S3C_DPTXFSIZn_DPTxFSize_SHIFT;
354 addr += size;
355
356 writel(val, hsotg->regs + S3C_DPTXFSIZn(ep));
357 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100358
359 /* according to p428 of the design guide, we need to ensure that
360 * all fifos are flushed before continuing */
361
362 writel(S3C_GRSTCTL_TxFNum(0x10) | S3C_GRSTCTL_TxFFlsh |
363 S3C_GRSTCTL_RxFFlsh, hsotg->regs + S3C_GRSTCTL);
364
365 /* wait until the fifos are both flushed */
366 timeout = 100;
367 while (1) {
368 val = readl(hsotg->regs + S3C_GRSTCTL);
369
370 if ((val & (S3C_GRSTCTL_TxFFlsh | S3C_GRSTCTL_RxFFlsh)) == 0)
371 break;
372
373 if (--timeout == 0) {
374 dev_err(hsotg->dev,
375 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
376 __func__, val);
377 }
378
379 udelay(1);
380 }
381
382 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100383}
384
385/**
386 * @ep: USB endpoint to allocate request for.
387 * @flags: Allocation flags
388 *
389 * Allocate a new USB request structure appropriate for the specified endpoint
390 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000391static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
392 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100393{
394 struct s3c_hsotg_req *req;
395
396 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
397 if (!req)
398 return NULL;
399
400 INIT_LIST_HEAD(&req->queue);
401
402 req->req.dma = DMA_ADDR_INVALID;
403 return &req->req;
404}
405
406/**
407 * is_ep_periodic - return true if the endpoint is in periodic mode.
408 * @hs_ep: The endpoint to query.
409 *
410 * Returns true if the endpoint is in periodic mode, meaning it is being
411 * used for an Interrupt or ISO transfer.
412 */
413static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
414{
415 return hs_ep->periodic;
416}
417
418/**
419 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
420 * @hsotg: The device state.
421 * @hs_ep: The endpoint for the request
422 * @hs_req: The request being processed.
423 *
424 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
425 * of a request to ensure the buffer is ready for access by the caller.
426*/
427static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
428 struct s3c_hsotg_ep *hs_ep,
429 struct s3c_hsotg_req *hs_req)
430{
431 struct usb_request *req = &hs_req->req;
432 enum dma_data_direction dir;
433
434 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
435
436 /* ignore this if we're not moving any data */
437 if (hs_req->req.length == 0)
438 return;
439
440 if (hs_req->mapped) {
441 /* we mapped this, so unmap and remove the dma */
442
443 dma_unmap_single(hsotg->dev, req->dma, req->length, dir);
444
445 req->dma = DMA_ADDR_INVALID;
446 hs_req->mapped = 0;
447 } else {
FUJITA Tomonori5b520252010-01-25 11:07:19 +0900448 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100449 }
450}
451
452/**
453 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
454 * @hsotg: The controller state.
455 * @hs_ep: The endpoint we're going to write for.
456 * @hs_req: The request to write data for.
457 *
458 * This is called when the TxFIFO has some space in it to hold a new
459 * transmission and we have something to give it. The actual setup of
460 * the data size is done elsewhere, so all we have to do is to actually
461 * write the data.
462 *
463 * The return value is zero if there is more space (or nothing was done)
464 * otherwise -ENOSPC is returned if the FIFO space was used up.
465 *
466 * This routine is only needed for PIO
467*/
468static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
469 struct s3c_hsotg_ep *hs_ep,
470 struct s3c_hsotg_req *hs_req)
471{
472 bool periodic = is_ep_periodic(hs_ep);
473 u32 gnptxsts = readl(hsotg->regs + S3C_GNPTXSTS);
474 int buf_pos = hs_req->req.actual;
475 int to_write = hs_ep->size_loaded;
476 void *data;
477 int can_write;
478 int pkt_round;
479
480 to_write -= (buf_pos - hs_ep->last_load);
481
482 /* if there's nothing to write, get out early */
483 if (to_write == 0)
484 return 0;
485
Ben Dooks10aebc72010-07-19 09:40:44 +0100486 if (periodic && !hsotg->dedicated_fifos) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100487 u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
488 int size_left;
489 int size_done;
490
491 /* work out how much data was loaded so we can calculate
492 * how much data is left in the fifo. */
493
494 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
495
Ben Dookse7a9ff52010-07-19 09:40:42 +0100496 /* if shared fifo, we cannot write anything until the
497 * previous data has been completely sent.
498 */
499 if (hs_ep->fifo_load != 0) {
500 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
501 return -ENOSPC;
502 }
503
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100504 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
505 __func__, size_left,
506 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
507
508 /* how much of the data has moved */
509 size_done = hs_ep->size_loaded - size_left;
510
511 /* how much data is left in the fifo */
512 can_write = hs_ep->fifo_load - size_done;
513 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
514 __func__, can_write);
515
516 can_write = hs_ep->fifo_size - can_write;
517 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
518 __func__, can_write);
519
520 if (can_write <= 0) {
521 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
522 return -ENOSPC;
523 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100524 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
525 can_write = readl(hsotg->regs + S3C_DTXFSTS(hs_ep->index));
526
527 can_write &= 0xffff;
528 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100529 } else {
530 if (S3C_GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
531 dev_dbg(hsotg->dev,
532 "%s: no queue slots available (0x%08x)\n",
533 __func__, gnptxsts);
534
535 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
536 return -ENOSPC;
537 }
538
539 can_write = S3C_GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100540 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100541 }
542
543 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n",
544 __func__, gnptxsts, can_write, to_write, hs_ep->ep.maxpacket);
545
546 /* limit to 512 bytes of data, it seems at least on the non-periodic
547 * FIFO, requests of >512 cause the endpoint to get stuck with a
548 * fragment of the end of the transfer in it.
549 */
550 if (can_write > 512)
551 can_write = 512;
552
Ben Dooks03e10e52010-07-19 09:40:45 +0100553 /* limit the write to one max-packet size worth of data, but allow
554 * the transfer to return that it did not run out of fifo space
555 * doing it. */
556 if (to_write > hs_ep->ep.maxpacket) {
557 to_write = hs_ep->ep.maxpacket;
558
559 s3c_hsotg_en_gsint(hsotg,
560 periodic ? S3C_GINTSTS_PTxFEmp :
561 S3C_GINTSTS_NPTxFEmp);
562 }
563
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100564 /* see if we can write data */
565
566 if (to_write > can_write) {
567 to_write = can_write;
568 pkt_round = to_write % hs_ep->ep.maxpacket;
569
570 /* Not sure, but we probably shouldn't be writing partial
571 * packets into the FIFO, so round the write down to an
572 * exact number of packets.
573 *
574 * Note, we do not currently check to see if we can ever
575 * write a full packet or not to the FIFO.
576 */
577
578 if (pkt_round)
579 to_write -= pkt_round;
580
581 /* enable correct FIFO interrupt to alert us when there
582 * is more room left. */
583
584 s3c_hsotg_en_gsint(hsotg,
585 periodic ? S3C_GINTSTS_PTxFEmp :
586 S3C_GINTSTS_NPTxFEmp);
587 }
588
589 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
590 to_write, hs_req->req.length, can_write, buf_pos);
591
592 if (to_write <= 0)
593 return -ENOSPC;
594
595 hs_req->req.actual = buf_pos + to_write;
596 hs_ep->total_data += to_write;
597
598 if (periodic)
599 hs_ep->fifo_load += to_write;
600
601 to_write = DIV_ROUND_UP(to_write, 4);
602 data = hs_req->req.buf + buf_pos;
603
604 writesl(hsotg->regs + S3C_EPFIFO(hs_ep->index), data, to_write);
605
606 return (to_write >= can_write) ? -ENOSPC : 0;
607}
608
609/**
610 * get_ep_limit - get the maximum data legnth for this endpoint
611 * @hs_ep: The endpoint
612 *
613 * Return the maximum data that can be queued in one go on a given endpoint
614 * so that transfers that are too long can be split.
615 */
616static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
617{
618 int index = hs_ep->index;
619 unsigned maxsize;
620 unsigned maxpkt;
621
622 if (index != 0) {
623 maxsize = S3C_DxEPTSIZ_XferSize_LIMIT + 1;
624 maxpkt = S3C_DxEPTSIZ_PktCnt_LIMIT + 1;
625 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100626 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900627 if (hs_ep->dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100628 maxpkt = S3C_DIEPTSIZ0_PktCnt_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900629 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100630 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100631 }
632
633 /* we made the constant loading easier above by using +1 */
634 maxpkt--;
635 maxsize--;
636
637 /* constrain by packet count if maxpkts*pktsize is greater
638 * than the length register size. */
639
640 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
641 maxsize = maxpkt * hs_ep->ep.maxpacket;
642
643 return maxsize;
644}
645
646/**
647 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
648 * @hsotg: The controller state.
649 * @hs_ep: The endpoint to process a request for
650 * @hs_req: The request to start.
651 * @continuing: True if we are doing more for the current request.
652 *
653 * Start the given request running by setting the endpoint registers
654 * appropriately, and writing any data to the FIFOs.
655 */
656static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
657 struct s3c_hsotg_ep *hs_ep,
658 struct s3c_hsotg_req *hs_req,
659 bool continuing)
660{
661 struct usb_request *ureq = &hs_req->req;
662 int index = hs_ep->index;
663 int dir_in = hs_ep->dir_in;
664 u32 epctrl_reg;
665 u32 epsize_reg;
666 u32 epsize;
667 u32 ctrl;
668 unsigned length;
669 unsigned packets;
670 unsigned maxreq;
671
672 if (index != 0) {
673 if (hs_ep->req && !continuing) {
674 dev_err(hsotg->dev, "%s: active request\n", __func__);
675 WARN_ON(1);
676 return;
677 } else if (hs_ep->req != hs_req && continuing) {
678 dev_err(hsotg->dev,
679 "%s: continue different req\n", __func__);
680 WARN_ON(1);
681 return;
682 }
683 }
684
685 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
686 epsize_reg = dir_in ? S3C_DIEPTSIZ(index) : S3C_DOEPTSIZ(index);
687
688 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
689 __func__, readl(hsotg->regs + epctrl_reg), index,
690 hs_ep->dir_in ? "in" : "out");
691
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900692 /* If endpoint is stalled, we will restart request later */
693 ctrl = readl(hsotg->regs + epctrl_reg);
694
695 if (ctrl & S3C_DxEPCTL_Stall) {
696 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
697 return;
698 }
699
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100700 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200701 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
702 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100703 if (0)
704 dev_dbg(hsotg->dev,
705 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
706 ureq->buf, length, ureq->dma,
707 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
708
709 maxreq = get_ep_limit(hs_ep);
710 if (length > maxreq) {
711 int round = maxreq % hs_ep->ep.maxpacket;
712
713 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
714 __func__, length, maxreq, round);
715
716 /* round down to multiple of packets */
717 if (round)
718 maxreq -= round;
719
720 length = maxreq;
721 }
722
723 if (length)
724 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
725 else
726 packets = 1; /* send one packet if length is zero. */
727
728 if (dir_in && index != 0)
729 epsize = S3C_DxEPTSIZ_MC(1);
730 else
731 epsize = 0;
732
733 if (index != 0 && ureq->zero) {
734 /* test for the packets being exactly right for the
735 * transfer */
736
737 if (length == (packets * hs_ep->ep.maxpacket))
738 packets++;
739 }
740
741 epsize |= S3C_DxEPTSIZ_PktCnt(packets);
742 epsize |= S3C_DxEPTSIZ_XferSize(length);
743
744 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
745 __func__, packets, length, ureq->length, epsize, epsize_reg);
746
747 /* store the request as the current one we're doing */
748 hs_ep->req = hs_req;
749
750 /* write size / packets */
751 writel(epsize, hsotg->regs + epsize_reg);
752
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900753 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100754 unsigned int dma_reg;
755
756 /* write DMA address to control register, buffer already
757 * synced by s3c_hsotg_ep_queue(). */
758
759 dma_reg = dir_in ? S3C_DIEPDMA(index) : S3C_DOEPDMA(index);
760 writel(ureq->dma, hsotg->regs + dma_reg);
761
762 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
763 __func__, ureq->dma, dma_reg);
764 }
765
766 ctrl |= S3C_DxEPCTL_EPEna; /* ensure ep enabled */
767 ctrl |= S3C_DxEPCTL_USBActEp;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200768
769 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
770
771 /* For Setup request do not clear NAK */
772 if (hsotg->setup && index == 0)
773 hsotg->setup = 0;
774 else
775 ctrl |= S3C_DxEPCTL_CNAK; /* clear NAK set by core */
776
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100777
778 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
779 writel(ctrl, hsotg->regs + epctrl_reg);
780
781 /* set these, it seems that DMA support increments past the end
782 * of the packet buffer so we need to calculate the length from
783 * this information. */
784 hs_ep->size_loaded = length;
785 hs_ep->last_load = ureq->actual;
786
787 if (dir_in && !using_dma(hsotg)) {
788 /* set these anyway, we may need them for non-periodic in */
789 hs_ep->fifo_load = 0;
790
791 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
792 }
793
794 /* clear the INTknTXFEmpMsk when we start request, more as a aide
795 * to debugging to see what is going on. */
796 if (dir_in)
797 writel(S3C_DIEPMSK_INTknTXFEmpMsk,
798 hsotg->regs + S3C_DIEPINT(index));
799
800 /* Note, trying to clear the NAK here causes problems with transmit
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300801 * on the S3C6400 ending up with the TXFIFO becoming full. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100802
803 /* check ep is enabled */
804 if (!(readl(hsotg->regs + epctrl_reg) & S3C_DxEPCTL_EPEna))
805 dev_warn(hsotg->dev,
806 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
807 index, readl(hsotg->regs + epctrl_reg));
808
809 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
810 __func__, readl(hsotg->regs + epctrl_reg));
811}
812
813/**
814 * s3c_hsotg_map_dma - map the DMA memory being used for the request
815 * @hsotg: The device state.
816 * @hs_ep: The endpoint the request is on.
817 * @req: The request being processed.
818 *
819 * We've been asked to queue a request, so ensure that the memory buffer
820 * is correctly setup for DMA. If we've been passed an extant DMA address
821 * then ensure the buffer has been synced to memory. If our buffer has no
822 * DMA memory, then we map the memory and mark our request to allow us to
823 * cleanup on completion.
824*/
825static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
826 struct s3c_hsotg_ep *hs_ep,
827 struct usb_request *req)
828{
829 enum dma_data_direction dir;
830 struct s3c_hsotg_req *hs_req = our_req(req);
831
832 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
833
834 /* if the length is zero, ignore the DMA data */
835 if (hs_req->req.length == 0)
836 return 0;
837
838 if (req->dma == DMA_ADDR_INVALID) {
839 dma_addr_t dma;
840
841 dma = dma_map_single(hsotg->dev, req->buf, req->length, dir);
842
843 if (unlikely(dma_mapping_error(hsotg->dev, dma)))
844 goto dma_error;
845
846 if (dma & 3) {
847 dev_err(hsotg->dev, "%s: unaligned dma buffer\n",
848 __func__);
849
850 dma_unmap_single(hsotg->dev, dma, req->length, dir);
851 return -EINVAL;
852 }
853
854 hs_req->mapped = 1;
855 req->dma = dma;
856 } else {
FUJITA Tomonori5b520252010-01-25 11:07:19 +0900857 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100858 hs_req->mapped = 0;
859 }
860
861 return 0;
862
863dma_error:
864 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
865 __func__, req->buf, req->length);
866
867 return -EIO;
868}
869
870static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
871 gfp_t gfp_flags)
872{
873 struct s3c_hsotg_req *hs_req = our_req(req);
874 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
875 struct s3c_hsotg *hs = hs_ep->parent;
876 unsigned long irqflags;
877 bool first;
878
879 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
880 ep->name, req, req->length, req->buf, req->no_interrupt,
881 req->zero, req->short_not_ok);
882
883 /* initialise status of the request */
884 INIT_LIST_HEAD(&hs_req->queue);
885 req->actual = 0;
886 req->status = -EINPROGRESS;
887
888 /* if we're using DMA, sync the buffers as necessary */
889 if (using_dma(hs)) {
890 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
891 if (ret)
892 return ret;
893 }
894
895 spin_lock_irqsave(&hs_ep->lock, irqflags);
896
897 first = list_empty(&hs_ep->queue);
898 list_add_tail(&hs_req->queue, &hs_ep->queue);
899
900 if (first)
901 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
902
903 spin_unlock_irqrestore(&hs_ep->lock, irqflags);
904
905 return 0;
906}
907
908static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
909 struct usb_request *req)
910{
911 struct s3c_hsotg_req *hs_req = our_req(req);
912
913 kfree(hs_req);
914}
915
916/**
917 * s3c_hsotg_complete_oursetup - setup completion callback
918 * @ep: The endpoint the request was on.
919 * @req: The request completed.
920 *
921 * Called on completion of any requests the driver itself
922 * submitted that need cleaning up.
923 */
924static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
925 struct usb_request *req)
926{
927 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
928 struct s3c_hsotg *hsotg = hs_ep->parent;
929
930 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
931
932 s3c_hsotg_ep_free_request(ep, req);
933}
934
935/**
936 * ep_from_windex - convert control wIndex value to endpoint
937 * @hsotg: The driver state.
938 * @windex: The control request wIndex field (in host order).
939 *
940 * Convert the given wIndex into a pointer to an driver endpoint
941 * structure, or return NULL if it is not a valid endpoint.
942*/
943static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
944 u32 windex)
945{
946 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
947 int dir = (windex & USB_DIR_IN) ? 1 : 0;
948 int idx = windex & 0x7F;
949
950 if (windex >= 0x100)
951 return NULL;
952
953 if (idx > S3C_HSOTG_EPS)
954 return NULL;
955
956 if (idx && ep->dir_in != dir)
957 return NULL;
958
959 return ep;
960}
961
962/**
963 * s3c_hsotg_send_reply - send reply to control request
964 * @hsotg: The device state
965 * @ep: Endpoint 0
966 * @buff: Buffer for request
967 * @length: Length of reply.
968 *
969 * Create a request and queue it on the given endpoint. This is useful as
970 * an internal method of sending replies to certain control requests, etc.
971 */
972static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
973 struct s3c_hsotg_ep *ep,
974 void *buff,
975 int length)
976{
977 struct usb_request *req;
978 int ret;
979
980 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
981
982 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
983 hsotg->ep0_reply = req;
984 if (!req) {
985 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
986 return -ENOMEM;
987 }
988
989 req->buf = hsotg->ep0_buff;
990 req->length = length;
991 req->zero = 1; /* always do zero-length final transfer */
992 req->complete = s3c_hsotg_complete_oursetup;
993
994 if (length)
995 memcpy(req->buf, buff, length);
996 else
997 ep->sent_zlp = 1;
998
999 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1000 if (ret) {
1001 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1002 return ret;
1003 }
1004
1005 return 0;
1006}
1007
1008/**
1009 * s3c_hsotg_process_req_status - process request GET_STATUS
1010 * @hsotg: The device state
1011 * @ctrl: USB control request
1012 */
1013static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1014 struct usb_ctrlrequest *ctrl)
1015{
1016 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1017 struct s3c_hsotg_ep *ep;
1018 __le16 reply;
1019 int ret;
1020
1021 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1022
1023 if (!ep0->dir_in) {
1024 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1025 return -EINVAL;
1026 }
1027
1028 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1029 case USB_RECIP_DEVICE:
1030 reply = cpu_to_le16(0); /* bit 0 => self powered,
1031 * bit 1 => remote wakeup */
1032 break;
1033
1034 case USB_RECIP_INTERFACE:
1035 /* currently, the data result should be zero */
1036 reply = cpu_to_le16(0);
1037 break;
1038
1039 case USB_RECIP_ENDPOINT:
1040 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1041 if (!ep)
1042 return -ENOENT;
1043
1044 reply = cpu_to_le16(ep->halted ? 1 : 0);
1045 break;
1046
1047 default:
1048 return 0;
1049 }
1050
1051 if (le16_to_cpu(ctrl->wLength) != 2)
1052 return -EINVAL;
1053
1054 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1055 if (ret) {
1056 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1057 return ret;
1058 }
1059
1060 return 1;
1061}
1062
1063static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1064
1065/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001066 * get_ep_head - return the first request on the endpoint
1067 * @hs_ep: The controller endpoint to get
1068 *
1069 * Get the first request on the endpoint.
1070 */
1071static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1072{
1073 if (list_empty(&hs_ep->queue))
1074 return NULL;
1075
1076 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1077}
1078
1079/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001080 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1081 * @hsotg: The device state
1082 * @ctrl: USB control request
1083 */
1084static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1085 struct usb_ctrlrequest *ctrl)
1086{
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001087 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001088 struct s3c_hsotg_req *hs_req;
1089 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001090 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1091 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001092 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001093
1094 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1095 __func__, set ? "SET" : "CLEAR");
1096
1097 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1098 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1099 if (!ep) {
1100 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1101 __func__, le16_to_cpu(ctrl->wIndex));
1102 return -ENOENT;
1103 }
1104
1105 switch (le16_to_cpu(ctrl->wValue)) {
1106 case USB_ENDPOINT_HALT:
1107 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001108
1109 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1110 if (ret) {
1111 dev_err(hsotg->dev,
1112 "%s: failed to send reply\n", __func__);
1113 return ret;
1114 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001115
1116 if (!set) {
1117 /*
1118 * If we have request in progress,
1119 * then complete it
1120 */
1121 if (ep->req) {
1122 hs_req = ep->req;
1123 ep->req = NULL;
1124 list_del_init(&hs_req->queue);
1125 hs_req->req.complete(&ep->ep,
1126 &hs_req->req);
1127 }
1128
1129 /* If we have pending request, then start it */
1130 restart = !list_empty(&ep->queue);
1131 if (restart) {
1132 hs_req = get_ep_head(ep);
1133 s3c_hsotg_start_req(hsotg, ep,
1134 hs_req, false);
1135 }
1136 }
1137
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138 break;
1139
1140 default:
1141 return -ENOENT;
1142 }
1143 } else
1144 return -ENOENT; /* currently only deal with endpoint */
1145
1146 return 1;
1147}
1148
1149/**
1150 * s3c_hsotg_process_control - process a control request
1151 * @hsotg: The device state
1152 * @ctrl: The control request received
1153 *
1154 * The controller has received the SETUP phase of a control request, and
1155 * needs to work out what to do next (and whether to pass it on to the
1156 * gadget driver).
1157 */
1158static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1159 struct usb_ctrlrequest *ctrl)
1160{
1161 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1162 int ret = 0;
1163 u32 dcfg;
1164
1165 ep0->sent_zlp = 0;
1166
1167 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1168 ctrl->bRequest, ctrl->bRequestType,
1169 ctrl->wValue, ctrl->wLength);
1170
1171 /* record the direction of the request, for later use when enquing
1172 * packets onto EP0. */
1173
1174 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1175 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1176
1177 /* if we've no data with this request, then the last part of the
1178 * transaction is going to implicitly be IN. */
1179 if (ctrl->wLength == 0)
1180 ep0->dir_in = 1;
1181
1182 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1183 switch (ctrl->bRequest) {
1184 case USB_REQ_SET_ADDRESS:
1185 dcfg = readl(hsotg->regs + S3C_DCFG);
1186 dcfg &= ~S3C_DCFG_DevAddr_MASK;
1187 dcfg |= ctrl->wValue << S3C_DCFG_DevAddr_SHIFT;
1188 writel(dcfg, hsotg->regs + S3C_DCFG);
1189
1190 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1191
1192 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1193 return;
1194
1195 case USB_REQ_GET_STATUS:
1196 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1197 break;
1198
1199 case USB_REQ_CLEAR_FEATURE:
1200 case USB_REQ_SET_FEATURE:
1201 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1202 break;
1203 }
1204 }
1205
1206 /* as a fallback, try delivering it to the driver to deal with */
1207
1208 if (ret == 0 && hsotg->driver) {
1209 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1210 if (ret < 0)
1211 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1212 }
1213
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001214 /* the request is either unhandlable, or is not formatted correctly
1215 * so respond with a STALL for the status stage to indicate failure.
1216 */
1217
1218 if (ret < 0) {
1219 u32 reg;
1220 u32 ctrl;
1221
1222 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1223 reg = (ep0->dir_in) ? S3C_DIEPCTL0 : S3C_DOEPCTL0;
1224
1225 /* S3C_DxEPCTL_Stall will be cleared by EP once it has
1226 * taken effect, so no need to clear later. */
1227
1228 ctrl = readl(hsotg->regs + reg);
1229 ctrl |= S3C_DxEPCTL_Stall;
1230 ctrl |= S3C_DxEPCTL_CNAK;
1231 writel(ctrl, hsotg->regs + reg);
1232
1233 dev_dbg(hsotg->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001234 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001235 ctrl, reg, readl(hsotg->regs + reg));
1236
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001237 /* don't believe we need to anything more to get the EP
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001238 * to reply with a STALL packet */
1239 }
1240}
1241
1242static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
1243
1244/**
1245 * s3c_hsotg_complete_setup - completion of a setup transfer
1246 * @ep: The endpoint the request was on.
1247 * @req: The request completed.
1248 *
1249 * Called on completion of any requests the driver itself submitted for
1250 * EP0 setup packets
1251 */
1252static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1253 struct usb_request *req)
1254{
1255 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1256 struct s3c_hsotg *hsotg = hs_ep->parent;
1257
1258 if (req->status < 0) {
1259 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1260 return;
1261 }
1262
1263 if (req->actual == 0)
1264 s3c_hsotg_enqueue_setup(hsotg);
1265 else
1266 s3c_hsotg_process_control(hsotg, req->buf);
1267}
1268
1269/**
1270 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1271 * @hsotg: The device state.
1272 *
1273 * Enqueue a request on EP0 if necessary to received any SETUP packets
1274 * received from the host.
1275 */
1276static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1277{
1278 struct usb_request *req = hsotg->ctrl_req;
1279 struct s3c_hsotg_req *hs_req = our_req(req);
1280 int ret;
1281
1282 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1283
1284 req->zero = 0;
1285 req->length = 8;
1286 req->buf = hsotg->ctrl_buff;
1287 req->complete = s3c_hsotg_complete_setup;
1288
1289 if (!list_empty(&hs_req->queue)) {
1290 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1291 return;
1292 }
1293
1294 hsotg->eps[0].dir_in = 0;
1295
1296 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1297 if (ret < 0) {
1298 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
1299 /* Don't think there's much we can do other than watch the
1300 * driver fail. */
1301 }
1302}
1303
1304/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001305 * s3c_hsotg_complete_request - complete a request given to us
1306 * @hsotg: The device state.
1307 * @hs_ep: The endpoint the request was on.
1308 * @hs_req: The request to complete.
1309 * @result: The result code (0 => Ok, otherwise errno)
1310 *
1311 * The given request has finished, so call the necessary completion
1312 * if it has one and then look to see if we can start a new request
1313 * on the endpoint.
1314 *
1315 * Note, expects the ep to already be locked as appropriate.
1316*/
1317static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1318 struct s3c_hsotg_ep *hs_ep,
1319 struct s3c_hsotg_req *hs_req,
1320 int result)
1321{
1322 bool restart;
1323
1324 if (!hs_req) {
1325 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1326 return;
1327 }
1328
1329 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1330 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1331
1332 /* only replace the status if we've not already set an error
1333 * from a previous transaction */
1334
1335 if (hs_req->req.status == -EINPROGRESS)
1336 hs_req->req.status = result;
1337
1338 hs_ep->req = NULL;
1339 list_del_init(&hs_req->queue);
1340
1341 if (using_dma(hsotg))
1342 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1343
1344 /* call the complete request with the locks off, just in case the
1345 * request tries to queue more work for this endpoint. */
1346
1347 if (hs_req->req.complete) {
1348 spin_unlock(&hs_ep->lock);
1349 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
1350 spin_lock(&hs_ep->lock);
1351 }
1352
1353 /* Look to see if there is anything else to do. Note, the completion
1354 * of the previous request may have caused a new request to be started
1355 * so be careful when doing this. */
1356
1357 if (!hs_ep->req && result >= 0) {
1358 restart = !list_empty(&hs_ep->queue);
1359 if (restart) {
1360 hs_req = get_ep_head(hs_ep);
1361 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1362 }
1363 }
1364}
1365
1366/**
1367 * s3c_hsotg_complete_request_lock - complete a request given to us (locked)
1368 * @hsotg: The device state.
1369 * @hs_ep: The endpoint the request was on.
1370 * @hs_req: The request to complete.
1371 * @result: The result code (0 => Ok, otherwise errno)
1372 *
1373 * See s3c_hsotg_complete_request(), but called with the endpoint's
1374 * lock held.
1375*/
1376static void s3c_hsotg_complete_request_lock(struct s3c_hsotg *hsotg,
1377 struct s3c_hsotg_ep *hs_ep,
1378 struct s3c_hsotg_req *hs_req,
1379 int result)
1380{
1381 unsigned long flags;
1382
1383 spin_lock_irqsave(&hs_ep->lock, flags);
1384 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
1385 spin_unlock_irqrestore(&hs_ep->lock, flags);
1386}
1387
1388/**
1389 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1390 * @hsotg: The device state.
1391 * @ep_idx: The endpoint index for the data
1392 * @size: The size of data in the fifo, in bytes
1393 *
1394 * The FIFO status shows there is data to read from the FIFO for a given
1395 * endpoint, so sort out whether we need to read the data into a request
1396 * that has been made for that endpoint.
1397 */
1398static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1399{
1400 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1401 struct s3c_hsotg_req *hs_req = hs_ep->req;
1402 void __iomem *fifo = hsotg->regs + S3C_EPFIFO(ep_idx);
1403 int to_read;
1404 int max_req;
1405 int read_ptr;
1406
1407 if (!hs_req) {
1408 u32 epctl = readl(hsotg->regs + S3C_DOEPCTL(ep_idx));
1409 int ptr;
1410
1411 dev_warn(hsotg->dev,
1412 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1413 __func__, size, ep_idx, epctl);
1414
1415 /* dump the data from the FIFO, we've nothing we can do */
1416 for (ptr = 0; ptr < size; ptr += 4)
1417 (void)readl(fifo);
1418
1419 return;
1420 }
1421
1422 spin_lock(&hs_ep->lock);
1423
1424 to_read = size;
1425 read_ptr = hs_req->req.actual;
1426 max_req = hs_req->req.length - read_ptr;
1427
Ben Dooksa33e7132010-07-19 09:40:49 +01001428 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1429 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1430
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001431 if (to_read > max_req) {
1432 /* more data appeared than we where willing
1433 * to deal with in this request.
1434 */
1435
1436 /* currently we don't deal this */
1437 WARN_ON_ONCE(1);
1438 }
1439
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001440 hs_ep->total_data += to_read;
1441 hs_req->req.actual += to_read;
1442 to_read = DIV_ROUND_UP(to_read, 4);
1443
1444 /* note, we might over-write the buffer end by 3 bytes depending on
1445 * alignment of the data. */
1446 readsl(fifo, hs_req->req.buf + read_ptr, to_read);
1447
1448 spin_unlock(&hs_ep->lock);
1449}
1450
1451/**
1452 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1453 * @hsotg: The device instance
1454 * @req: The request currently on this endpoint
1455 *
1456 * Generate a zero-length IN packet request for terminating a SETUP
1457 * transaction.
1458 *
1459 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001460 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001461 * the TxFIFO.
1462 */
1463static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1464 struct s3c_hsotg_req *req)
1465{
1466 u32 ctrl;
1467
1468 if (!req) {
1469 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1470 return;
1471 }
1472
1473 if (req->req.length == 0) {
1474 hsotg->eps[0].sent_zlp = 1;
1475 s3c_hsotg_enqueue_setup(hsotg);
1476 return;
1477 }
1478
1479 hsotg->eps[0].dir_in = 1;
1480 hsotg->eps[0].sent_zlp = 1;
1481
1482 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1483
1484 /* issue a zero-sized packet to terminate this */
1485 writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
1486 S3C_DxEPTSIZ_XferSize(0), hsotg->regs + S3C_DIEPTSIZ(0));
1487
1488 ctrl = readl(hsotg->regs + S3C_DIEPCTL0);
1489 ctrl |= S3C_DxEPCTL_CNAK; /* clear NAK set by core */
1490 ctrl |= S3C_DxEPCTL_EPEna; /* ensure ep enabled */
1491 ctrl |= S3C_DxEPCTL_USBActEp;
1492 writel(ctrl, hsotg->regs + S3C_DIEPCTL0);
1493}
1494
1495/**
1496 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1497 * @hsotg: The device instance
1498 * @epnum: The endpoint received from
1499 * @was_setup: Set if processing a SetupDone event.
1500 *
1501 * The RXFIFO has delivered an OutDone event, which means that the data
1502 * transfer for an OUT endpoint has been completed, either by a short
1503 * packet or by the finish of a transfer.
1504*/
1505static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1506 int epnum, bool was_setup)
1507{
Ben Dooksa33e7132010-07-19 09:40:49 +01001508 u32 epsize = readl(hsotg->regs + S3C_DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001509 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1510 struct s3c_hsotg_req *hs_req = hs_ep->req;
1511 struct usb_request *req = &hs_req->req;
Ben Dooksa33e7132010-07-19 09:40:49 +01001512 unsigned size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001513 int result = 0;
1514
1515 if (!hs_req) {
1516 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1517 return;
1518 }
1519
1520 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001521 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001522
1523 /* Calculate the size of the transfer by checking how much
1524 * is left in the endpoint size register and then working it
1525 * out from the amount we loaded for the transfer.
1526 *
1527 * We need to do this as DMA pointers are always 32bit aligned
1528 * so may overshoot/undershoot the transfer.
1529 */
1530
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001531 size_done = hs_ep->size_loaded - size_left;
1532 size_done += hs_ep->last_load;
1533
1534 req->actual = size_done;
1535 }
1536
Ben Dooksa33e7132010-07-19 09:40:49 +01001537 /* if there is more request to do, schedule new transfer */
1538 if (req->actual < req->length && size_left == 0) {
1539 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1540 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001541 } else if (epnum == 0) {
1542 /*
1543 * After was_setup = 1 =>
1544 * set CNAK for non Setup requests
1545 */
1546 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001547 }
1548
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001549 if (req->actual < req->length && req->short_not_ok) {
1550 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1551 __func__, req->actual, req->length);
1552
1553 /* todo - what should we return here? there's no one else
1554 * even bothering to check the status. */
1555 }
1556
1557 if (epnum == 0) {
1558 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1559 s3c_hsotg_send_zlp(hsotg, hs_req);
1560 }
1561
1562 s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, result);
1563}
1564
1565/**
1566 * s3c_hsotg_read_frameno - read current frame number
1567 * @hsotg: The device instance
1568 *
1569 * Return the current frame number
1570*/
1571static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1572{
1573 u32 dsts;
1574
1575 dsts = readl(hsotg->regs + S3C_DSTS);
1576 dsts &= S3C_DSTS_SOFFN_MASK;
1577 dsts >>= S3C_DSTS_SOFFN_SHIFT;
1578
1579 return dsts;
1580}
1581
1582/**
1583 * s3c_hsotg_handle_rx - RX FIFO has data
1584 * @hsotg: The device instance
1585 *
1586 * The IRQ handler has detected that the RX FIFO has some data in it
1587 * that requires processing, so find out what is in there and do the
1588 * appropriate read.
1589 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001590 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001591 * chunks, so if you have x packets received on an endpoint you'll get x
1592 * FIFO events delivered, each with a packet's worth of data in it.
1593 *
1594 * When using DMA, we should not be processing events from the RXFIFO
1595 * as the actual data should be sent to the memory directly and we turn
1596 * on the completion interrupts to get notifications of transfer completion.
1597 */
Mark Brown0978f8c2010-01-18 13:18:35 +00001598static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001599{
1600 u32 grxstsr = readl(hsotg->regs + S3C_GRXSTSP);
1601 u32 epnum, status, size;
1602
1603 WARN_ON(using_dma(hsotg));
1604
1605 epnum = grxstsr & S3C_GRXSTS_EPNum_MASK;
1606 status = grxstsr & S3C_GRXSTS_PktSts_MASK;
1607
1608 size = grxstsr & S3C_GRXSTS_ByteCnt_MASK;
1609 size >>= S3C_GRXSTS_ByteCnt_SHIFT;
1610
1611 if (1)
1612 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1613 __func__, grxstsr, size, epnum);
1614
1615#define __status(x) ((x) >> S3C_GRXSTS_PktSts_SHIFT)
1616
1617 switch (status >> S3C_GRXSTS_PktSts_SHIFT) {
1618 case __status(S3C_GRXSTS_PktSts_GlobalOutNAK):
1619 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1620 break;
1621
1622 case __status(S3C_GRXSTS_PktSts_OutDone):
1623 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1624 s3c_hsotg_read_frameno(hsotg));
1625
1626 if (!using_dma(hsotg))
1627 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1628 break;
1629
1630 case __status(S3C_GRXSTS_PktSts_SetupDone):
1631 dev_dbg(hsotg->dev,
1632 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1633 s3c_hsotg_read_frameno(hsotg),
1634 readl(hsotg->regs + S3C_DOEPCTL(0)));
1635
1636 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1637 break;
1638
1639 case __status(S3C_GRXSTS_PktSts_OutRX):
1640 s3c_hsotg_rx_data(hsotg, epnum, size);
1641 break;
1642
1643 case __status(S3C_GRXSTS_PktSts_SetupRX):
1644 dev_dbg(hsotg->dev,
1645 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1646 s3c_hsotg_read_frameno(hsotg),
1647 readl(hsotg->regs + S3C_DOEPCTL(0)));
1648
1649 s3c_hsotg_rx_data(hsotg, epnum, size);
1650 break;
1651
1652 default:
1653 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1654 __func__, grxstsr);
1655
1656 s3c_hsotg_dump(hsotg);
1657 break;
1658 }
1659}
1660
1661/**
1662 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1663 * @mps: The maximum packet size in bytes.
1664*/
1665static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1666{
1667 switch (mps) {
1668 case 64:
1669 return S3C_D0EPCTL_MPS_64;
1670 case 32:
1671 return S3C_D0EPCTL_MPS_32;
1672 case 16:
1673 return S3C_D0EPCTL_MPS_16;
1674 case 8:
1675 return S3C_D0EPCTL_MPS_8;
1676 }
1677
1678 /* bad max packet size, warn and return invalid result */
1679 WARN_ON(1);
1680 return (u32)-1;
1681}
1682
1683/**
1684 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1685 * @hsotg: The driver state.
1686 * @ep: The index number of the endpoint
1687 * @mps: The maximum packet size in bytes
1688 *
1689 * Configure the maximum packet size for the given endpoint, updating
1690 * the hardware control registers to reflect this.
1691 */
1692static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1693 unsigned int ep, unsigned int mps)
1694{
1695 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1696 void __iomem *regs = hsotg->regs;
1697 u32 mpsval;
1698 u32 reg;
1699
1700 if (ep == 0) {
1701 /* EP0 is a special case */
1702 mpsval = s3c_hsotg_ep0_mps(mps);
1703 if (mpsval > 3)
1704 goto bad_mps;
1705 } else {
1706 if (mps >= S3C_DxEPCTL_MPS_LIMIT+1)
1707 goto bad_mps;
1708
1709 mpsval = mps;
1710 }
1711
1712 hs_ep->ep.maxpacket = mps;
1713
1714 /* update both the in and out endpoint controldir_ registers, even
1715 * if one of the directions may not be in use. */
1716
1717 reg = readl(regs + S3C_DIEPCTL(ep));
1718 reg &= ~S3C_DxEPCTL_MPS_MASK;
1719 reg |= mpsval;
1720 writel(reg, regs + S3C_DIEPCTL(ep));
1721
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001722 if (ep) {
1723 reg = readl(regs + S3C_DOEPCTL(ep));
1724 reg &= ~S3C_DxEPCTL_MPS_MASK;
1725 reg |= mpsval;
1726 writel(reg, regs + S3C_DOEPCTL(ep));
1727 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001728
1729 return;
1730
1731bad_mps:
1732 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1733}
1734
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001735/**
1736 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1737 * @hsotg: The driver state
1738 * @idx: The index for the endpoint (0..15)
1739 */
1740static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1741{
1742 int timeout;
1743 int val;
1744
1745 writel(S3C_GRSTCTL_TxFNum(idx) | S3C_GRSTCTL_TxFFlsh,
1746 hsotg->regs + S3C_GRSTCTL);
1747
1748 /* wait until the fifo is flushed */
1749 timeout = 100;
1750
1751 while (1) {
1752 val = readl(hsotg->regs + S3C_GRSTCTL);
1753
1754 if ((val & (S3C_GRSTCTL_TxFFlsh)) == 0)
1755 break;
1756
1757 if (--timeout == 0) {
1758 dev_err(hsotg->dev,
1759 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1760 __func__, val);
1761 }
1762
1763 udelay(1);
1764 }
1765}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001766
1767/**
1768 * s3c_hsotg_trytx - check to see if anything needs transmitting
1769 * @hsotg: The driver state
1770 * @hs_ep: The driver endpoint to check.
1771 *
1772 * Check to see if there is a request that has data to send, and if so
1773 * make an attempt to write data into the FIFO.
1774 */
1775static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1776 struct s3c_hsotg_ep *hs_ep)
1777{
1778 struct s3c_hsotg_req *hs_req = hs_ep->req;
1779
1780 if (!hs_ep->dir_in || !hs_req)
1781 return 0;
1782
1783 if (hs_req->req.actual < hs_req->req.length) {
1784 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1785 hs_ep->index);
1786 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1787 }
1788
1789 return 0;
1790}
1791
1792/**
1793 * s3c_hsotg_complete_in - complete IN transfer
1794 * @hsotg: The device state.
1795 * @hs_ep: The endpoint that has just completed.
1796 *
1797 * An IN transfer has been completed, update the transfer's state and then
1798 * call the relevant completion routines.
1799 */
1800static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1801 struct s3c_hsotg_ep *hs_ep)
1802{
1803 struct s3c_hsotg_req *hs_req = hs_ep->req;
1804 u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
1805 int size_left, size_done;
1806
1807 if (!hs_req) {
1808 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1809 return;
1810 }
1811
1812 /* Calculate the size of the transfer by checking how much is left
1813 * in the endpoint size register and then working it out from
1814 * the amount we loaded for the transfer.
1815 *
1816 * We do this even for DMA, as the transfer may have incremented
1817 * past the end of the buffer (DMA transfers are always 32bit
1818 * aligned).
1819 */
1820
1821 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
1822
1823 size_done = hs_ep->size_loaded - size_left;
1824 size_done += hs_ep->last_load;
1825
1826 if (hs_req->req.actual != size_done)
1827 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1828 __func__, hs_req->req.actual, size_done);
1829
1830 hs_req->req.actual = size_done;
1831
1832 /* if we did all of the transfer, and there is more data left
1833 * around, then try restarting the rest of the request */
1834
1835 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1836 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1837 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1838 } else
1839 s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, 0);
1840}
1841
1842/**
1843 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1844 * @hsotg: The driver state
1845 * @idx: The index for the endpoint (0..15)
1846 * @dir_in: Set if this is an IN endpoint
1847 *
1848 * Process and clear any interrupt pending for an individual endpoint
1849*/
1850static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1851 int dir_in)
1852{
1853 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
1854 u32 epint_reg = dir_in ? S3C_DIEPINT(idx) : S3C_DOEPINT(idx);
1855 u32 epctl_reg = dir_in ? S3C_DIEPCTL(idx) : S3C_DOEPCTL(idx);
1856 u32 epsiz_reg = dir_in ? S3C_DIEPTSIZ(idx) : S3C_DOEPTSIZ(idx);
1857 u32 ints;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001858
1859 ints = readl(hsotg->regs + epint_reg);
1860
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001861 /* Clear endpoint interrupts */
1862 writel(ints, hsotg->regs + epint_reg);
1863
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001864 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1865 __func__, idx, dir_in ? "in" : "out", ints);
1866
1867 if (ints & S3C_DxEPINT_XferCompl) {
1868 dev_dbg(hsotg->dev,
1869 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1870 __func__, readl(hsotg->regs + epctl_reg),
1871 readl(hsotg->regs + epsiz_reg));
1872
1873 /* we get OutDone from the FIFO, so we only need to look
1874 * at completing IN requests here */
1875 if (dir_in) {
1876 s3c_hsotg_complete_in(hsotg, hs_ep);
1877
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001878 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001879 s3c_hsotg_enqueue_setup(hsotg);
1880 } else if (using_dma(hsotg)) {
1881 /* We're using DMA, we need to fire an OutDone here
1882 * as we ignore the RXFIFO. */
1883
1884 s3c_hsotg_handle_outdone(hsotg, idx, false);
1885 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001886 }
1887
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001888 if (ints & S3C_DxEPINT_EPDisbld) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001889 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001890
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001891 if (dir_in) {
1892 int epctl = readl(hsotg->regs + epctl_reg);
1893
1894 s3c_hsotg_txfifo_flush(hsotg, idx);
1895
1896 if ((epctl & S3C_DxEPCTL_Stall) &&
1897 (epctl & S3C_DxEPCTL_EPType_Bulk)) {
1898 int dctl = readl(hsotg->regs + S3C_DCTL);
1899
1900 dctl |= S3C_DCTL_CGNPInNAK;
1901 writel(dctl, hsotg->regs + S3C_DCTL);
1902 }
1903 }
1904 }
1905
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001906 if (ints & S3C_DxEPINT_AHBErr)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001907 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001908
1909 if (ints & S3C_DxEPINT_Setup) { /* Setup or Timeout */
1910 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1911
1912 if (using_dma(hsotg) && idx == 0) {
1913 /* this is the notification we've received a
1914 * setup packet. In non-DMA mode we'd get this
1915 * from the RXFIFO, instead we need to process
1916 * the setup here. */
1917
1918 if (dir_in)
1919 WARN_ON_ONCE(1);
1920 else
1921 s3c_hsotg_handle_outdone(hsotg, 0, true);
1922 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001923 }
1924
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001925 if (ints & S3C_DxEPINT_Back2BackSetup)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001926 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001927
1928 if (dir_in) {
1929 /* not sure if this is important, but we'll clear it anyway
1930 */
1931 if (ints & S3C_DIEPMSK_INTknTXFEmpMsk) {
1932 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1933 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001934 }
1935
1936 /* this probably means something bad is happening */
1937 if (ints & S3C_DIEPMSK_INTknEPMisMsk) {
1938 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1939 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001940 }
Ben Dooks10aebc72010-07-19 09:40:44 +01001941
1942 /* FIFO has space or is empty (see GAHBCFG) */
1943 if (hsotg->dedicated_fifos &&
1944 ints & S3C_DIEPMSK_TxFIFOEmpty) {
1945 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1946 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09001947 if (!using_dma(hsotg))
1948 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01001949 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001951}
1952
1953/**
1954 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
1955 * @hsotg: The device state.
1956 *
1957 * Handle updating the device settings after the enumeration phase has
1958 * been completed.
1959*/
1960static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
1961{
1962 u32 dsts = readl(hsotg->regs + S3C_DSTS);
1963 int ep0_mps = 0, ep_mps;
1964
1965 /* This should signal the finish of the enumeration phase
1966 * of the USB handshaking, so we should now know what rate
1967 * we connected at. */
1968
1969 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
1970
1971 /* note, since we're limited by the size of transfer on EP0, and
1972 * it seems IN transfers must be a even number of packets we do
1973 * not advertise a 64byte MPS on EP0. */
1974
1975 /* catch both EnumSpd_FS and EnumSpd_FS48 */
1976 switch (dsts & S3C_DSTS_EnumSpd_MASK) {
1977 case S3C_DSTS_EnumSpd_FS:
1978 case S3C_DSTS_EnumSpd_FS48:
1979 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001980 ep0_mps = EP0_MPS_LIMIT;
1981 ep_mps = 64;
1982 break;
1983
1984 case S3C_DSTS_EnumSpd_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001985 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001986 ep0_mps = EP0_MPS_LIMIT;
1987 ep_mps = 512;
1988 break;
1989
1990 case S3C_DSTS_EnumSpd_LS:
1991 hsotg->gadget.speed = USB_SPEED_LOW;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001992 /* note, we don't actually support LS in this driver at the
1993 * moment, and the documentation seems to imply that it isn't
1994 * supported by the PHYs on some of the devices.
1995 */
1996 break;
1997 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02001998 dev_info(hsotg->dev, "new device is %s\n",
1999 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002000
2001 /* we should now know the maximum packet size for an
2002 * endpoint, so set the endpoints to a default value. */
2003
2004 if (ep0_mps) {
2005 int i;
2006 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
2007 for (i = 1; i < S3C_HSOTG_EPS; i++)
2008 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
2009 }
2010
2011 /* ensure after enumeration our EP0 is active */
2012
2013 s3c_hsotg_enqueue_setup(hsotg);
2014
2015 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2016 readl(hsotg->regs + S3C_DIEPCTL0),
2017 readl(hsotg->regs + S3C_DOEPCTL0));
2018}
2019
2020/**
2021 * kill_all_requests - remove all requests from the endpoint's queue
2022 * @hsotg: The device state.
2023 * @ep: The endpoint the requests may be on.
2024 * @result: The result code to use.
2025 * @force: Force removal of any current requests
2026 *
2027 * Go through the requests on the given endpoint and mark them
2028 * completed with the given result code.
2029 */
2030static void kill_all_requests(struct s3c_hsotg *hsotg,
2031 struct s3c_hsotg_ep *ep,
2032 int result, bool force)
2033{
2034 struct s3c_hsotg_req *req, *treq;
2035 unsigned long flags;
2036
2037 spin_lock_irqsave(&ep->lock, flags);
2038
2039 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2040 /* currently, we can't do much about an already
2041 * running request on an in endpoint */
2042
2043 if (ep->req == req && ep->dir_in && !force)
2044 continue;
2045
2046 s3c_hsotg_complete_request(hsotg, ep, req,
2047 result);
2048 }
2049
2050 spin_unlock_irqrestore(&ep->lock, flags);
2051}
2052
2053#define call_gadget(_hs, _entry) \
2054 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
2055 (_hs)->driver && (_hs)->driver->_entry) \
2056 (_hs)->driver->_entry(&(_hs)->gadget);
2057
2058/**
2059 * s3c_hsotg_disconnect_irq - disconnect irq service
2060 * @hsotg: The device state.
2061 *
2062 * A disconnect IRQ has been received, meaning that the host has
2063 * lost contact with the bus. Remove all current transactions
2064 * and signal the gadget driver that this has happened.
2065*/
2066static void s3c_hsotg_disconnect_irq(struct s3c_hsotg *hsotg)
2067{
2068 unsigned ep;
2069
2070 for (ep = 0; ep < S3C_HSOTG_EPS; ep++)
2071 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2072
2073 call_gadget(hsotg, disconnect);
2074}
2075
2076/**
2077 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2078 * @hsotg: The device state:
2079 * @periodic: True if this is a periodic FIFO interrupt
2080 */
2081static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2082{
2083 struct s3c_hsotg_ep *ep;
2084 int epno, ret;
2085
2086 /* look through for any more data to transmit */
2087
2088 for (epno = 0; epno < S3C_HSOTG_EPS; epno++) {
2089 ep = &hsotg->eps[epno];
2090
2091 if (!ep->dir_in)
2092 continue;
2093
2094 if ((periodic && !ep->periodic) ||
2095 (!periodic && ep->periodic))
2096 continue;
2097
2098 ret = s3c_hsotg_trytx(hsotg, ep);
2099 if (ret < 0)
2100 break;
2101 }
2102}
2103
2104static struct s3c_hsotg *our_hsotg;
2105
2106/* IRQ flags which will trigger a retry around the IRQ loop */
2107#define IRQ_RETRY_MASK (S3C_GINTSTS_NPTxFEmp | \
2108 S3C_GINTSTS_PTxFEmp | \
2109 S3C_GINTSTS_RxFLvl)
2110
2111/**
2112 * s3c_hsotg_irq - handle device interrupt
2113 * @irq: The IRQ number triggered
2114 * @pw: The pw value when registered the handler.
2115 */
2116static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2117{
2118 struct s3c_hsotg *hsotg = pw;
2119 int retry_count = 8;
2120 u32 gintsts;
2121 u32 gintmsk;
2122
2123irq_retry:
2124 gintsts = readl(hsotg->regs + S3C_GINTSTS);
2125 gintmsk = readl(hsotg->regs + S3C_GINTMSK);
2126
2127 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2128 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2129
2130 gintsts &= gintmsk;
2131
2132 if (gintsts & S3C_GINTSTS_OTGInt) {
2133 u32 otgint = readl(hsotg->regs + S3C_GOTGINT);
2134
2135 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2136
2137 writel(otgint, hsotg->regs + S3C_GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002138 }
2139
2140 if (gintsts & S3C_GINTSTS_DisconnInt) {
2141 dev_dbg(hsotg->dev, "%s: DisconnInt\n", __func__);
2142 writel(S3C_GINTSTS_DisconnInt, hsotg->regs + S3C_GINTSTS);
2143
2144 s3c_hsotg_disconnect_irq(hsotg);
2145 }
2146
2147 if (gintsts & S3C_GINTSTS_SessReqInt) {
2148 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
2149 writel(S3C_GINTSTS_SessReqInt, hsotg->regs + S3C_GINTSTS);
2150 }
2151
2152 if (gintsts & S3C_GINTSTS_EnumDone) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002153 writel(S3C_GINTSTS_EnumDone, hsotg->regs + S3C_GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002154
2155 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002156 }
2157
2158 if (gintsts & S3C_GINTSTS_ConIDStsChng) {
2159 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
2160 readl(hsotg->regs + S3C_DSTS),
2161 readl(hsotg->regs + S3C_GOTGCTL));
2162
2163 writel(S3C_GINTSTS_ConIDStsChng, hsotg->regs + S3C_GINTSTS);
2164 }
2165
2166 if (gintsts & (S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt)) {
2167 u32 daint = readl(hsotg->regs + S3C_DAINT);
2168 u32 daint_out = daint >> S3C_DAINT_OutEP_SHIFT;
2169 u32 daint_in = daint & ~(daint_out << S3C_DAINT_OutEP_SHIFT);
2170 int ep;
2171
2172 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2173
2174 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2175 if (daint_out & 1)
2176 s3c_hsotg_epint(hsotg, ep, 0);
2177 }
2178
2179 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2180 if (daint_in & 1)
2181 s3c_hsotg_epint(hsotg, ep, 1);
2182 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002183 }
2184
2185 if (gintsts & S3C_GINTSTS_USBRst) {
2186 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2187 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2188 readl(hsotg->regs + S3C_GNPTXSTS));
2189
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002190 writel(S3C_GINTSTS_USBRst, hsotg->regs + S3C_GINTSTS);
2191
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002192 kill_all_requests(hsotg, &hsotg->eps[0], -ECONNRESET, true);
2193
2194 /* it seems after a reset we can end up with a situation
Ben Dooksb3864ce2010-07-19 09:40:43 +01002195 * where the TXFIFO still has data in it... the docs
2196 * suggest resetting all the fifos, so use the init_fifo
2197 * code to relayout and flush the fifos.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002198 */
2199
Ben Dooksb3864ce2010-07-19 09:40:43 +01002200 s3c_hsotg_init_fifo(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002201
2202 s3c_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002203 }
2204
2205 /* check both FIFOs */
2206
2207 if (gintsts & S3C_GINTSTS_NPTxFEmp) {
2208 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2209
2210 /* Disable the interrupt to stop it happening again
2211 * unless one of these endpoint routines decides that
2212 * it needs re-enabling */
2213
2214 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
2215 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002216 }
2217
2218 if (gintsts & S3C_GINTSTS_PTxFEmp) {
2219 dev_dbg(hsotg->dev, "PTxFEmp\n");
2220
2221 /* See note in S3C_GINTSTS_NPTxFEmp */
2222
2223 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
2224 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002225 }
2226
2227 if (gintsts & S3C_GINTSTS_RxFLvl) {
2228 /* note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
2229 * we need to retry s3c_hsotg_handle_rx if this is still
2230 * set. */
2231
2232 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002233 }
2234
2235 if (gintsts & S3C_GINTSTS_ModeMis) {
2236 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
2237 writel(S3C_GINTSTS_ModeMis, hsotg->regs + S3C_GINTSTS);
2238 }
2239
2240 if (gintsts & S3C_GINTSTS_USBSusp) {
2241 dev_info(hsotg->dev, "S3C_GINTSTS_USBSusp\n");
2242 writel(S3C_GINTSTS_USBSusp, hsotg->regs + S3C_GINTSTS);
2243
2244 call_gadget(hsotg, suspend);
2245 }
2246
2247 if (gintsts & S3C_GINTSTS_WkUpInt) {
2248 dev_info(hsotg->dev, "S3C_GINTSTS_WkUpIn\n");
2249 writel(S3C_GINTSTS_WkUpInt, hsotg->regs + S3C_GINTSTS);
2250
2251 call_gadget(hsotg, resume);
2252 }
2253
2254 if (gintsts & S3C_GINTSTS_ErlySusp) {
2255 dev_dbg(hsotg->dev, "S3C_GINTSTS_ErlySusp\n");
2256 writel(S3C_GINTSTS_ErlySusp, hsotg->regs + S3C_GINTSTS);
2257 }
2258
2259 /* these next two seem to crop-up occasionally causing the core
2260 * to shutdown the USB transfer, so try clearing them and logging
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002261 * the occurrence. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002262
2263 if (gintsts & S3C_GINTSTS_GOUTNakEff) {
2264 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2265
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002266 writel(S3C_DCTL_CGOUTNak, hsotg->regs + S3C_DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002267
2268 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002269 }
2270
2271 if (gintsts & S3C_GINTSTS_GINNakEff) {
2272 dev_info(hsotg->dev, "GINNakEff triggered\n");
2273
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002274 writel(S3C_DCTL_CGNPInNAK, hsotg->regs + S3C_DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002275
2276 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002277 }
2278
2279 /* if we've had fifo events, we should try and go around the
2280 * loop again to see if there's any point in returning yet. */
2281
2282 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2283 goto irq_retry;
2284
2285 return IRQ_HANDLED;
2286}
2287
2288/**
2289 * s3c_hsotg_ep_enable - enable the given endpoint
2290 * @ep: The USB endpint to configure
2291 * @desc: The USB endpoint descriptor to configure with.
2292 *
2293 * This is called from the USB gadget code's usb_ep_enable().
2294*/
2295static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2296 const struct usb_endpoint_descriptor *desc)
2297{
2298 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2299 struct s3c_hsotg *hsotg = hs_ep->parent;
2300 unsigned long flags;
2301 int index = hs_ep->index;
2302 u32 epctrl_reg;
2303 u32 epctrl;
2304 u32 mps;
2305 int dir_in;
Julia Lawall19c190f2010-03-29 17:36:44 +02002306 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002307
2308 dev_dbg(hsotg->dev,
2309 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2310 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2311 desc->wMaxPacketSize, desc->bInterval);
2312
2313 /* not to be called for EP0 */
2314 WARN_ON(index == 0);
2315
2316 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2317 if (dir_in != hs_ep->dir_in) {
2318 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2319 return -EINVAL;
2320 }
2321
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002322 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002323
2324 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2325
2326 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2327 epctrl = readl(hsotg->regs + epctrl_reg);
2328
2329 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2330 __func__, epctrl, epctrl_reg);
2331
2332 spin_lock_irqsave(&hs_ep->lock, flags);
2333
2334 epctrl &= ~(S3C_DxEPCTL_EPType_MASK | S3C_DxEPCTL_MPS_MASK);
2335 epctrl |= S3C_DxEPCTL_MPS(mps);
2336
2337 /* mark the endpoint as active, otherwise the core may ignore
2338 * transactions entirely for this endpoint */
2339 epctrl |= S3C_DxEPCTL_USBActEp;
2340
2341 /* set the NAK status on the endpoint, otherwise we might try and
2342 * do something with data that we've yet got a request to process
2343 * since the RXFIFO will take data for an endpoint even if the
2344 * size register hasn't been set.
2345 */
2346
2347 epctrl |= S3C_DxEPCTL_SNAK;
2348
2349 /* update the endpoint state */
2350 hs_ep->ep.maxpacket = mps;
2351
2352 /* default, set to non-periodic */
2353 hs_ep->periodic = 0;
2354
2355 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2356 case USB_ENDPOINT_XFER_ISOC:
2357 dev_err(hsotg->dev, "no current ISOC support\n");
Julia Lawall19c190f2010-03-29 17:36:44 +02002358 ret = -EINVAL;
2359 goto out;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002360
2361 case USB_ENDPOINT_XFER_BULK:
2362 epctrl |= S3C_DxEPCTL_EPType_Bulk;
2363 break;
2364
2365 case USB_ENDPOINT_XFER_INT:
2366 if (dir_in) {
2367 /* Allocate our TxFNum by simply using the index
2368 * of the endpoint for the moment. We could do
2369 * something better if the host indicates how
2370 * many FIFOs we are expecting to use. */
2371
2372 hs_ep->periodic = 1;
2373 epctrl |= S3C_DxEPCTL_TxFNum(index);
2374 }
2375
2376 epctrl |= S3C_DxEPCTL_EPType_Intterupt;
2377 break;
2378
2379 case USB_ENDPOINT_XFER_CONTROL:
2380 epctrl |= S3C_DxEPCTL_EPType_Control;
2381 break;
2382 }
2383
Ben Dooks10aebc72010-07-19 09:40:44 +01002384 /* if the hardware has dedicated fifos, we must give each IN EP
2385 * a unique tx-fifo even if it is non-periodic.
2386 */
2387 if (dir_in && hsotg->dedicated_fifos)
2388 epctrl |= S3C_DxEPCTL_TxFNum(index);
2389
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002390 /* for non control endpoints, set PID to D0 */
2391 if (index)
2392 epctrl |= S3C_DxEPCTL_SetD0PID;
2393
2394 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2395 __func__, epctrl);
2396
2397 writel(epctrl, hsotg->regs + epctrl_reg);
2398 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2399 __func__, readl(hsotg->regs + epctrl_reg));
2400
2401 /* enable the endpoint interrupt */
2402 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2403
Julia Lawall19c190f2010-03-29 17:36:44 +02002404out:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002405 spin_unlock_irqrestore(&hs_ep->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002406 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002407}
2408
2409static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2410{
2411 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2412 struct s3c_hsotg *hsotg = hs_ep->parent;
2413 int dir_in = hs_ep->dir_in;
2414 int index = hs_ep->index;
2415 unsigned long flags;
2416 u32 epctrl_reg;
2417 u32 ctrl;
2418
2419 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2420
2421 if (ep == &hsotg->eps[0].ep) {
2422 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2423 return -EINVAL;
2424 }
2425
2426 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2427
2428 /* terminate all requests with shutdown */
2429 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2430
2431 spin_lock_irqsave(&hs_ep->lock, flags);
2432
2433 ctrl = readl(hsotg->regs + epctrl_reg);
2434 ctrl &= ~S3C_DxEPCTL_EPEna;
2435 ctrl &= ~S3C_DxEPCTL_USBActEp;
2436 ctrl |= S3C_DxEPCTL_SNAK;
2437
2438 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2439 writel(ctrl, hsotg->regs + epctrl_reg);
2440
2441 /* disable endpoint interrupts */
2442 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2443
2444 spin_unlock_irqrestore(&hs_ep->lock, flags);
2445 return 0;
2446}
2447
2448/**
2449 * on_list - check request is on the given endpoint
2450 * @ep: The endpoint to check.
2451 * @test: The request to test if it is on the endpoint.
2452*/
2453static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2454{
2455 struct s3c_hsotg_req *req, *treq;
2456
2457 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2458 if (req == test)
2459 return true;
2460 }
2461
2462 return false;
2463}
2464
2465static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2466{
2467 struct s3c_hsotg_req *hs_req = our_req(req);
2468 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2469 struct s3c_hsotg *hs = hs_ep->parent;
2470 unsigned long flags;
2471
2472 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2473
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002474 spin_lock_irqsave(&hs_ep->lock, flags);
2475
2476 if (!on_list(hs_ep, hs_req)) {
2477 spin_unlock_irqrestore(&hs_ep->lock, flags);
2478 return -EINVAL;
2479 }
2480
2481 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
2482 spin_unlock_irqrestore(&hs_ep->lock, flags);
2483
2484 return 0;
2485}
2486
2487static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2488{
2489 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2490 struct s3c_hsotg *hs = hs_ep->parent;
2491 int index = hs_ep->index;
2492 unsigned long irqflags;
2493 u32 epreg;
2494 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002495 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002496
2497 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2498
2499 spin_lock_irqsave(&hs_ep->lock, irqflags);
2500
2501 /* write both IN and OUT control registers */
2502
2503 epreg = S3C_DIEPCTL(index);
2504 epctl = readl(hs->regs + epreg);
2505
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002506 if (value) {
2507 epctl |= S3C_DxEPCTL_Stall + S3C_DxEPCTL_SNAK;
2508 if (epctl & S3C_DxEPCTL_EPEna)
2509 epctl |= S3C_DxEPCTL_EPDis;
2510 } else {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002511 epctl &= ~S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002512 xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2513 if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2514 xfertype == S3C_DxEPCTL_EPType_Intterupt)
2515 epctl |= S3C_DxEPCTL_SetD0PID;
2516 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002517
2518 writel(epctl, hs->regs + epreg);
2519
2520 epreg = S3C_DOEPCTL(index);
2521 epctl = readl(hs->regs + epreg);
2522
2523 if (value)
2524 epctl |= S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002525 else {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002526 epctl &= ~S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002527 xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2528 if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2529 xfertype == S3C_DxEPCTL_EPType_Intterupt)
2530 epctl |= S3C_DxEPCTL_SetD0PID;
2531 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002532
2533 writel(epctl, hs->regs + epreg);
2534
2535 spin_unlock_irqrestore(&hs_ep->lock, irqflags);
2536
2537 return 0;
2538}
2539
2540static struct usb_ep_ops s3c_hsotg_ep_ops = {
2541 .enable = s3c_hsotg_ep_enable,
2542 .disable = s3c_hsotg_ep_disable,
2543 .alloc_request = s3c_hsotg_ep_alloc_request,
2544 .free_request = s3c_hsotg_ep_free_request,
2545 .queue = s3c_hsotg_ep_queue,
2546 .dequeue = s3c_hsotg_ep_dequeue,
2547 .set_halt = s3c_hsotg_ep_sethalt,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002548 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002549};
2550
2551/**
2552 * s3c_hsotg_corereset - issue softreset to the core
2553 * @hsotg: The device state
2554 *
2555 * Issue a soft reset to the core, and await the core finishing it.
2556*/
2557static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2558{
2559 int timeout;
2560 u32 grstctl;
2561
2562 dev_dbg(hsotg->dev, "resetting core\n");
2563
2564 /* issue soft reset */
2565 writel(S3C_GRSTCTL_CSftRst, hsotg->regs + S3C_GRSTCTL);
2566
2567 timeout = 1000;
2568 do {
2569 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
Anton Tikhomirovd00f5002011-04-21 17:06:38 +09002570 } while ((grstctl & S3C_GRSTCTL_CSftRst) && timeout-- > 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002571
Anton Tikhomirovd00f5002011-04-21 17:06:38 +09002572 if (grstctl & S3C_GRSTCTL_CSftRst) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002573 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2574 return -EINVAL;
2575 }
2576
2577 timeout = 1000;
2578
2579 while (1) {
2580 u32 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2581
2582 if (timeout-- < 0) {
2583 dev_info(hsotg->dev,
2584 "%s: reset failed, GRSTCTL=%08x\n",
2585 __func__, grstctl);
2586 return -ETIMEDOUT;
2587 }
2588
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002589 if (!(grstctl & S3C_GRSTCTL_AHBIdle))
2590 continue;
2591
Jingoo Han66e5c642011-05-13 21:26:15 +09002592 break; /* reset done */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002593 }
2594
2595 dev_dbg(hsotg->dev, "reset successful\n");
2596 return 0;
2597}
2598
Lukasz Majewski41188782012-05-04 14:17:01 +02002599/**
2600 * s3c_hsotg_phy_enable - enable platform phy dev
2601 *
2602 * @param: The driver state
2603 *
2604 * A wrapper for platform code responsible for controlling
2605 * low-level USB code
2606 */
2607static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2608{
2609 struct platform_device *pdev = to_platform_device(hsotg->dev);
2610
2611 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
2612 if (hsotg->plat->phy_init)
2613 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2614}
2615
2616/**
2617 * s3c_hsotg_phy_disable - disable platform phy dev
2618 *
2619 * @param: The driver state
2620 *
2621 * A wrapper for platform code responsible for controlling
2622 * low-level USB code
2623 */
2624static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2625{
2626 struct platform_device *pdev = to_platform_device(hsotg->dev);
2627
2628 if (hsotg->plat->phy_exit)
2629 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2630}
2631
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002632static int s3c_hsotg_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002633 int (*bind)(struct usb_gadget *))
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002634{
2635 struct s3c_hsotg *hsotg = our_hsotg;
2636 int ret;
2637
2638 if (!hsotg) {
2639 printk(KERN_ERR "%s: called with no device\n", __func__);
2640 return -ENODEV;
2641 }
2642
2643 if (!driver) {
2644 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2645 return -EINVAL;
2646 }
2647
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002648 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002649 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002650
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002651 if (!bind || !driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002652 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2653 return -EINVAL;
2654 }
2655
2656 WARN_ON(hsotg->driver);
2657
2658 driver->driver.bus = NULL;
2659 hsotg->driver = driver;
2660 hsotg->gadget.dev.driver = &driver->driver;
2661 hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
2662 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2663
2664 ret = device_add(&hsotg->gadget.dev);
2665 if (ret) {
2666 dev_err(hsotg->dev, "failed to register gadget device\n");
2667 goto err;
2668 }
2669
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002670 ret = bind(&hsotg->gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002671 if (ret) {
2672 dev_err(hsotg->dev, "failed bind %s\n", driver->driver.name);
2673
2674 hsotg->gadget.dev.driver = NULL;
2675 hsotg->driver = NULL;
2676 goto err;
2677 }
2678
2679 /* we must now enable ep0 ready for host detection and then
2680 * set configuration. */
2681
2682 s3c_hsotg_corereset(hsotg);
2683
2684 /* set the PLL on, remove the HNP/SRP and set the PHY */
2685 writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) |
2686 (0x5 << 10), hsotg->regs + S3C_GUSBCFG);
2687
2688 /* looks like soft-reset changes state of FIFOs */
2689 s3c_hsotg_init_fifo(hsotg);
2690
2691 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2692
2693 writel(1 << 18 | S3C_DCFG_DevSpd_HS, hsotg->regs + S3C_DCFG);
2694
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002695 /* Clear any pending OTG interrupts */
2696 writel(0xffffffff, hsotg->regs + S3C_GOTGINT);
2697
2698 /* Clear any pending interrupts */
2699 writel(0xffffffff, hsotg->regs + S3C_GINTSTS);
2700
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002701 writel(S3C_GINTSTS_DisconnInt | S3C_GINTSTS_SessReqInt |
2702 S3C_GINTSTS_ConIDStsChng | S3C_GINTSTS_USBRst |
2703 S3C_GINTSTS_EnumDone | S3C_GINTSTS_OTGInt |
2704 S3C_GINTSTS_USBSusp | S3C_GINTSTS_WkUpInt |
2705 S3C_GINTSTS_GOUTNakEff | S3C_GINTSTS_GINNakEff |
2706 S3C_GINTSTS_ErlySusp,
2707 hsotg->regs + S3C_GINTMSK);
2708
2709 if (using_dma(hsotg))
2710 writel(S3C_GAHBCFG_GlblIntrEn | S3C_GAHBCFG_DMAEn |
2711 S3C_GAHBCFG_HBstLen_Incr4,
2712 hsotg->regs + S3C_GAHBCFG);
2713 else
2714 writel(S3C_GAHBCFG_GlblIntrEn, hsotg->regs + S3C_GAHBCFG);
2715
2716 /* Enabling INTknTXFEmpMsk here seems to be a big mistake, we end
2717 * up being flooded with interrupts if the host is polling the
2718 * endpoint to try and read data. */
2719
2720 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2721 S3C_DIEPMSK_INTknEPMisMsk |
Ben Dooks10aebc72010-07-19 09:40:44 +01002722 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk |
2723 ((hsotg->dedicated_fifos) ? S3C_DIEPMSK_TxFIFOEmpty : 0),
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002724 hsotg->regs + S3C_DIEPMSK);
2725
2726 /* don't need XferCompl, we get that from RXFIFO in slave mode. In
2727 * DMA mode we may need this. */
2728 writel(S3C_DOEPMSK_SetupMsk | S3C_DOEPMSK_AHBErrMsk |
2729 S3C_DOEPMSK_EPDisbldMsk |
Roel Kluinb7800212009-07-15 20:12:30 +02002730 (using_dma(hsotg) ? (S3C_DIEPMSK_XferComplMsk |
2731 S3C_DIEPMSK_TimeOUTMsk) : 0),
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002732 hsotg->regs + S3C_DOEPMSK);
2733
2734 writel(0, hsotg->regs + S3C_DAINTMSK);
2735
Mark Brown83a01802011-06-01 17:16:15 +01002736 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2737 readl(hsotg->regs + S3C_DIEPCTL0),
2738 readl(hsotg->regs + S3C_DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002739
2740 /* enable in and out endpoint interrupts */
2741 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt);
2742
2743 /* Enable the RXFIFO when in slave mode, as this is how we collect
2744 * the data. In DMA mode, we get events from the FIFO but also
2745 * things we cannot process, so do not use it. */
2746 if (!using_dma(hsotg))
2747 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_RxFLvl);
2748
2749 /* Enable interrupts for EP0 in and out */
2750 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2751 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2752
2753 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2754 udelay(10); /* see openiboot */
2755 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2756
Mark Brown83a01802011-06-01 17:16:15 +01002757 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + S3C_DCTL));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002758
2759 /* S3C_DxEPCTL_USBActEp says RO in manual, but seems to be set by
2760 writing to the EPCTL register.. */
2761
2762 /* set to read 1 8byte packet */
2763 writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
2764 S3C_DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
2765
2766 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2767 S3C_DxEPCTL_CNAK | S3C_DxEPCTL_EPEna |
2768 S3C_DxEPCTL_USBActEp,
2769 hsotg->regs + S3C_DOEPCTL0);
2770
2771 /* enable, but don't activate EP0in */
2772 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2773 S3C_DxEPCTL_USBActEp, hsotg->regs + S3C_DIEPCTL0);
2774
2775 s3c_hsotg_enqueue_setup(hsotg);
2776
Mark Brown83a01802011-06-01 17:16:15 +01002777 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2778 readl(hsotg->regs + S3C_DIEPCTL0),
2779 readl(hsotg->regs + S3C_DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002780
2781 /* clear global NAKs */
2782 writel(S3C_DCTL_CGOUTNak | S3C_DCTL_CGNPInNAK,
2783 hsotg->regs + S3C_DCTL);
2784
Ben Dooks2e0e0772010-05-25 05:36:51 +01002785 /* must be at-least 3ms to allow bus to see disconnect */
2786 msleep(3);
2787
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002788 /* remove the soft-disconnect and let's go */
2789 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2790
2791 /* report to the user, and return */
2792
2793 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
2794 return 0;
2795
2796err:
2797 hsotg->driver = NULL;
2798 hsotg->gadget.dev.driver = NULL;
2799 return ret;
2800}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002801
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002802static int s3c_hsotg_stop(struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002803{
2804 struct s3c_hsotg *hsotg = our_hsotg;
2805 int ep;
2806
2807 if (!hsotg)
2808 return -ENODEV;
2809
2810 if (!driver || driver != hsotg->driver || !driver->unbind)
2811 return -EINVAL;
2812
2813 /* all endpoints should be shutdown */
2814 for (ep = 0; ep < S3C_HSOTG_EPS; ep++)
2815 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
2816
2817 call_gadget(hsotg, disconnect);
2818
2819 driver->unbind(&hsotg->gadget);
2820 hsotg->driver = NULL;
2821 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2822
2823 device_del(&hsotg->gadget.dev);
2824
2825 dev_info(hsotg->dev, "unregistered gadget driver '%s'\n",
2826 driver->driver.name);
2827
2828 return 0;
2829}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002830
2831static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2832{
2833 return s3c_hsotg_read_frameno(to_hsotg(gadget));
2834}
2835
2836static struct usb_gadget_ops s3c_hsotg_gadget_ops = {
2837 .get_frame = s3c_hsotg_gadget_getframe,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002838 .start = s3c_hsotg_start,
2839 .stop = s3c_hsotg_stop,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002840};
2841
2842/**
2843 * s3c_hsotg_initep - initialise a single endpoint
2844 * @hsotg: The device state.
2845 * @hs_ep: The endpoint to be initialised.
2846 * @epnum: The endpoint number
2847 *
2848 * Initialise the given endpoint (as part of the probe and device state
2849 * creation) to give to the gadget driver. Setup the endpoint name, any
2850 * direction information and other state that may be required.
2851 */
2852static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
2853 struct s3c_hsotg_ep *hs_ep,
2854 int epnum)
2855{
2856 u32 ptxfifo;
2857 char *dir;
2858
2859 if (epnum == 0)
2860 dir = "";
2861 else if ((epnum % 2) == 0) {
2862 dir = "out";
2863 } else {
2864 dir = "in";
2865 hs_ep->dir_in = 1;
2866 }
2867
2868 hs_ep->index = epnum;
2869
2870 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
2871
2872 INIT_LIST_HEAD(&hs_ep->queue);
2873 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
2874
2875 spin_lock_init(&hs_ep->lock);
2876
2877 /* add to the list of endpoints known by the gadget driver */
2878 if (epnum)
2879 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
2880
2881 hs_ep->parent = hsotg;
2882 hs_ep->ep.name = hs_ep->name;
2883 hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT;
2884 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
2885
2886 /* Read the FIFO size for the Periodic TX FIFO, even if we're
2887 * an OUT endpoint, we may as well do this if in future the
2888 * code is changed to make each endpoint's direction changeable.
2889 */
2890
2891 ptxfifo = readl(hsotg->regs + S3C_DPTXFSIZn(epnum));
Ben Dooks679f9b72010-07-19 09:40:41 +01002892 hs_ep->fifo_size = S3C_DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002893
2894 /* if we're using dma, we need to set the next-endpoint pointer
2895 * to be something valid.
2896 */
2897
2898 if (using_dma(hsotg)) {
2899 u32 next = S3C_DxEPCTL_NextEp((epnum + 1) % 15);
2900 writel(next, hsotg->regs + S3C_DIEPCTL(epnum));
2901 writel(next, hsotg->regs + S3C_DOEPCTL(epnum));
2902 }
2903}
2904
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002905static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2906{
Ben Dooks10aebc72010-07-19 09:40:44 +01002907 u32 cfg4;
2908
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002909 /* unmask subset of endpoint interrupts */
2910
2911 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2912 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk,
2913 hsotg->regs + S3C_DIEPMSK);
2914
2915 writel(S3C_DOEPMSK_SetupMsk | S3C_DOEPMSK_AHBErrMsk |
2916 S3C_DOEPMSK_EPDisbldMsk | S3C_DOEPMSK_XferComplMsk,
2917 hsotg->regs + S3C_DOEPMSK);
2918
2919 writel(0, hsotg->regs + S3C_DAINTMSK);
2920
Thomas Abraham390b1662010-05-24 17:48:56 +09002921 /* Be in disconnected state until gadget is registered */
2922 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2923
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002924 if (0) {
2925 /* post global nak until we're ready */
2926 writel(S3C_DCTL_SGNPInNAK | S3C_DCTL_SGOUTNak,
2927 hsotg->regs + S3C_DCTL);
2928 }
2929
2930 /* setup fifos */
2931
Mark Brown83a01802011-06-01 17:16:15 +01002932 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2933 readl(hsotg->regs + S3C_GRXFSIZ),
2934 readl(hsotg->regs + S3C_GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002935
2936 s3c_hsotg_init_fifo(hsotg);
2937
2938 /* set the PLL on, remove the HNP/SRP and set the PHY */
2939 writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) | (0x5 << 10),
2940 hsotg->regs + S3C_GUSBCFG);
2941
2942 writel(using_dma(hsotg) ? S3C_GAHBCFG_DMAEn : 0x0,
2943 hsotg->regs + S3C_GAHBCFG);
Ben Dooks10aebc72010-07-19 09:40:44 +01002944
2945 /* check hardware configuration */
2946
2947 cfg4 = readl(hsotg->regs + 0x50);
2948 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
2949
2950 dev_info(hsotg->dev, "%s fifos\n",
2951 hsotg->dedicated_fifos ? "dedicated" : "shared");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002952}
2953
2954static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
2955{
Mark Brown83a01802011-06-01 17:16:15 +01002956#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002957 struct device *dev = hsotg->dev;
2958 void __iomem *regs = hsotg->regs;
2959 u32 val;
2960 int idx;
2961
2962 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
2963 readl(regs + S3C_DCFG), readl(regs + S3C_DCTL),
2964 readl(regs + S3C_DIEPMSK));
2965
2966 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
2967 readl(regs + S3C_GAHBCFG), readl(regs + 0x44));
2968
2969 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2970 readl(regs + S3C_GRXFSIZ), readl(regs + S3C_GNPTXFSIZ));
2971
2972 /* show periodic fifo settings */
2973
2974 for (idx = 1; idx <= 15; idx++) {
2975 val = readl(regs + S3C_DPTXFSIZn(idx));
2976 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
2977 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
2978 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
2979 }
2980
2981 for (idx = 0; idx < 15; idx++) {
2982 dev_info(dev,
2983 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
2984 readl(regs + S3C_DIEPCTL(idx)),
2985 readl(regs + S3C_DIEPTSIZ(idx)),
2986 readl(regs + S3C_DIEPDMA(idx)));
2987
2988 val = readl(regs + S3C_DOEPCTL(idx));
2989 dev_info(dev,
2990 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
2991 idx, readl(regs + S3C_DOEPCTL(idx)),
2992 readl(regs + S3C_DOEPTSIZ(idx)),
2993 readl(regs + S3C_DOEPDMA(idx)));
2994
2995 }
2996
2997 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
2998 readl(regs + S3C_DVBUSDIS), readl(regs + S3C_DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01002999#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003000}
3001
3002
3003/**
3004 * state_show - debugfs: show overall driver and device state.
3005 * @seq: The seq file to write to.
3006 * @v: Unused parameter.
3007 *
3008 * This debugfs entry shows the overall state of the hardware and
3009 * some general information about each of the endpoints available
3010 * to the system.
3011 */
3012static int state_show(struct seq_file *seq, void *v)
3013{
3014 struct s3c_hsotg *hsotg = seq->private;
3015 void __iomem *regs = hsotg->regs;
3016 int idx;
3017
3018 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
3019 readl(regs + S3C_DCFG),
3020 readl(regs + S3C_DCTL),
3021 readl(regs + S3C_DSTS));
3022
3023 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
3024 readl(regs + S3C_DIEPMSK), readl(regs + S3C_DOEPMSK));
3025
3026 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
3027 readl(regs + S3C_GINTMSK),
3028 readl(regs + S3C_GINTSTS));
3029
3030 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
3031 readl(regs + S3C_DAINTMSK),
3032 readl(regs + S3C_DAINT));
3033
3034 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
3035 readl(regs + S3C_GNPTXSTS),
3036 readl(regs + S3C_GRXSTSR));
3037
3038 seq_printf(seq, "\nEndpoint status:\n");
3039
3040 for (idx = 0; idx < 15; idx++) {
3041 u32 in, out;
3042
3043 in = readl(regs + S3C_DIEPCTL(idx));
3044 out = readl(regs + S3C_DOEPCTL(idx));
3045
3046 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3047 idx, in, out);
3048
3049 in = readl(regs + S3C_DIEPTSIZ(idx));
3050 out = readl(regs + S3C_DOEPTSIZ(idx));
3051
3052 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3053 in, out);
3054
3055 seq_printf(seq, "\n");
3056 }
3057
3058 return 0;
3059}
3060
3061static int state_open(struct inode *inode, struct file *file)
3062{
3063 return single_open(file, state_show, inode->i_private);
3064}
3065
3066static const struct file_operations state_fops = {
3067 .owner = THIS_MODULE,
3068 .open = state_open,
3069 .read = seq_read,
3070 .llseek = seq_lseek,
3071 .release = single_release,
3072};
3073
3074/**
3075 * fifo_show - debugfs: show the fifo information
3076 * @seq: The seq_file to write data to.
3077 * @v: Unused parameter.
3078 *
3079 * Show the FIFO information for the overall fifo and all the
3080 * periodic transmission FIFOs.
3081*/
3082static int fifo_show(struct seq_file *seq, void *v)
3083{
3084 struct s3c_hsotg *hsotg = seq->private;
3085 void __iomem *regs = hsotg->regs;
3086 u32 val;
3087 int idx;
3088
3089 seq_printf(seq, "Non-periodic FIFOs:\n");
3090 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + S3C_GRXFSIZ));
3091
3092 val = readl(regs + S3C_GNPTXFSIZ);
3093 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
3094 val >> S3C_GNPTXFSIZ_NPTxFDep_SHIFT,
3095 val & S3C_GNPTXFSIZ_NPTxFStAddr_MASK);
3096
3097 seq_printf(seq, "\nPeriodic TXFIFOs:\n");
3098
3099 for (idx = 1; idx <= 15; idx++) {
3100 val = readl(regs + S3C_DPTXFSIZn(idx));
3101
3102 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
3103 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
3104 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
3105 }
3106
3107 return 0;
3108}
3109
3110static int fifo_open(struct inode *inode, struct file *file)
3111{
3112 return single_open(file, fifo_show, inode->i_private);
3113}
3114
3115static const struct file_operations fifo_fops = {
3116 .owner = THIS_MODULE,
3117 .open = fifo_open,
3118 .read = seq_read,
3119 .llseek = seq_lseek,
3120 .release = single_release,
3121};
3122
3123
3124static const char *decode_direction(int is_in)
3125{
3126 return is_in ? "in" : "out";
3127}
3128
3129/**
3130 * ep_show - debugfs: show the state of an endpoint.
3131 * @seq: The seq_file to write data to.
3132 * @v: Unused parameter.
3133 *
3134 * This debugfs entry shows the state of the given endpoint (one is
3135 * registered for each available).
3136*/
3137static int ep_show(struct seq_file *seq, void *v)
3138{
3139 struct s3c_hsotg_ep *ep = seq->private;
3140 struct s3c_hsotg *hsotg = ep->parent;
3141 struct s3c_hsotg_req *req;
3142 void __iomem *regs = hsotg->regs;
3143 int index = ep->index;
3144 int show_limit = 15;
3145 unsigned long flags;
3146
3147 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3148 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3149
3150 /* first show the register state */
3151
3152 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
3153 readl(regs + S3C_DIEPCTL(index)),
3154 readl(regs + S3C_DOEPCTL(index)));
3155
3156 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
3157 readl(regs + S3C_DIEPDMA(index)),
3158 readl(regs + S3C_DOEPDMA(index)));
3159
3160 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
3161 readl(regs + S3C_DIEPINT(index)),
3162 readl(regs + S3C_DOEPINT(index)));
3163
3164 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
3165 readl(regs + S3C_DIEPTSIZ(index)),
3166 readl(regs + S3C_DOEPTSIZ(index)));
3167
3168 seq_printf(seq, "\n");
3169 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3170 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3171
3172 seq_printf(seq, "request list (%p,%p):\n",
3173 ep->queue.next, ep->queue.prev);
3174
3175 spin_lock_irqsave(&ep->lock, flags);
3176
3177 list_for_each_entry(req, &ep->queue, queue) {
3178 if (--show_limit < 0) {
3179 seq_printf(seq, "not showing more requests...\n");
3180 break;
3181 }
3182
3183 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3184 req == ep->req ? '*' : ' ',
3185 req, req->req.length, req->req.buf);
3186 seq_printf(seq, "%d done, res %d\n",
3187 req->req.actual, req->req.status);
3188 }
3189
3190 spin_unlock_irqrestore(&ep->lock, flags);
3191
3192 return 0;
3193}
3194
3195static int ep_open(struct inode *inode, struct file *file)
3196{
3197 return single_open(file, ep_show, inode->i_private);
3198}
3199
3200static const struct file_operations ep_fops = {
3201 .owner = THIS_MODULE,
3202 .open = ep_open,
3203 .read = seq_read,
3204 .llseek = seq_lseek,
3205 .release = single_release,
3206};
3207
3208/**
3209 * s3c_hsotg_create_debug - create debugfs directory and files
3210 * @hsotg: The driver state
3211 *
3212 * Create the debugfs files to allow the user to get information
3213 * about the state of the system. The directory name is created
3214 * with the same name as the device itself, in case we end up
3215 * with multiple blocks in future systems.
3216*/
3217static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3218{
3219 struct dentry *root;
3220 unsigned epidx;
3221
3222 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3223 hsotg->debug_root = root;
3224 if (IS_ERR(root)) {
3225 dev_err(hsotg->dev, "cannot create debug root\n");
3226 return;
3227 }
3228
3229 /* create general state file */
3230
3231 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3232 hsotg, &state_fops);
3233
3234 if (IS_ERR(hsotg->debug_file))
3235 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3236
3237 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3238 hsotg, &fifo_fops);
3239
3240 if (IS_ERR(hsotg->debug_fifo))
3241 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3242
3243 /* create one file for each endpoint */
3244
3245 for (epidx = 0; epidx < S3C_HSOTG_EPS; epidx++) {
3246 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3247
3248 ep->debugfs = debugfs_create_file(ep->name, 0444,
3249 root, ep, &ep_fops);
3250
3251 if (IS_ERR(ep->debugfs))
3252 dev_err(hsotg->dev, "failed to create %s debug file\n",
3253 ep->name);
3254 }
3255}
3256
3257/**
3258 * s3c_hsotg_delete_debug - cleanup debugfs entries
3259 * @hsotg: The driver state
3260 *
3261 * Cleanup (remove) the debugfs files for use on module exit.
3262*/
3263static void __devexit s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3264{
3265 unsigned epidx;
3266
3267 for (epidx = 0; epidx < S3C_HSOTG_EPS; epidx++) {
3268 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3269 debugfs_remove(ep->debugfs);
3270 }
3271
3272 debugfs_remove(hsotg->debug_file);
3273 debugfs_remove(hsotg->debug_fifo);
3274 debugfs_remove(hsotg->debug_root);
3275}
3276
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003277static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3278{
3279 struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
3280 struct device *dev = &pdev->dev;
3281 struct s3c_hsotg *hsotg;
3282 struct resource *res;
3283 int epnum;
3284 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003285 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003286
Lukasz Majewski41188782012-05-04 14:17:01 +02003287 plat = pdev->dev.platform_data;
3288 if (!plat) {
3289 dev_err(&pdev->dev, "no platform data defined\n");
3290 return -EINVAL;
3291 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003292
3293 hsotg = kzalloc(sizeof(struct s3c_hsotg) +
3294 sizeof(struct s3c_hsotg_ep) * S3C_HSOTG_EPS,
3295 GFP_KERNEL);
3296 if (!hsotg) {
3297 dev_err(dev, "cannot get memory\n");
3298 return -ENOMEM;
3299 }
3300
3301 hsotg->dev = dev;
3302 hsotg->plat = plat;
3303
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003304 hsotg->clk = clk_get(&pdev->dev, "otg");
3305 if (IS_ERR(hsotg->clk)) {
3306 dev_err(dev, "cannot get otg clock\n");
Jingoo Han2328cea2011-05-13 21:26:23 +09003307 ret = PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003308 goto err_mem;
3309 }
3310
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003311 platform_set_drvdata(pdev, hsotg);
3312
3313 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3314 if (!res) {
3315 dev_err(dev, "cannot find register resource 0\n");
3316 ret = -EINVAL;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003317 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003318 }
3319
3320 hsotg->regs_res = request_mem_region(res->start, resource_size(res),
3321 dev_name(dev));
3322 if (!hsotg->regs_res) {
3323 dev_err(dev, "cannot reserve registers\n");
3324 ret = -ENOENT;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003325 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003326 }
3327
3328 hsotg->regs = ioremap(res->start, resource_size(res));
3329 if (!hsotg->regs) {
3330 dev_err(dev, "cannot map registers\n");
3331 ret = -ENXIO;
3332 goto err_regs_res;
3333 }
3334
3335 ret = platform_get_irq(pdev, 0);
3336 if (ret < 0) {
3337 dev_err(dev, "cannot find IRQ\n");
3338 goto err_regs;
3339 }
3340
3341 hsotg->irq = ret;
3342
3343 ret = request_irq(ret, s3c_hsotg_irq, 0, dev_name(dev), hsotg);
3344 if (ret < 0) {
3345 dev_err(dev, "cannot claim IRQ\n");
3346 goto err_regs;
3347 }
3348
3349 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3350
3351 device_initialize(&hsotg->gadget.dev);
3352
3353 dev_set_name(&hsotg->gadget.dev, "gadget");
3354
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003355 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003356 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3357 hsotg->gadget.name = dev_name(dev);
3358
3359 hsotg->gadget.dev.parent = dev;
3360 hsotg->gadget.dev.dma_mask = dev->dma_mask;
3361
3362 /* setup endpoint information */
3363
3364 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3365 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3366
3367 /* allocate EP0 request */
3368
3369 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3370 GFP_KERNEL);
3371 if (!hsotg->ctrl_req) {
3372 dev_err(dev, "failed to allocate ctrl req\n");
3373 goto err_regs;
3374 }
3375
3376 /* reset the system */
3377
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003378 clk_enable(hsotg->clk);
3379
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003380 /* regulators */
3381
3382 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3383 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3384
3385 ret = regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
3386 hsotg->supplies);
3387 if (ret) {
3388 dev_err(dev, "failed to request supplies: %d\n", ret);
3389 goto err_supplies;
3390 }
3391
3392 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3393 hsotg->supplies);
3394
3395 if (ret) {
3396 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3397 goto err_supplies;
3398 }
3399
Lukasz Majewski41188782012-05-04 14:17:01 +02003400 /* usb phy enable */
3401 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003402
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003403 s3c_hsotg_corereset(hsotg);
3404 s3c_hsotg_init(hsotg);
3405
3406 /* initialise the endpoints now the core has been initialised */
3407 for (epnum = 0; epnum < S3C_HSOTG_EPS; epnum++)
3408 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3409
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003410 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3411 if (ret)
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003412 goto err_supplies;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003413
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003414 s3c_hsotg_create_debug(hsotg);
3415
3416 s3c_hsotg_dump(hsotg);
3417
3418 our_hsotg = hsotg;
3419 return 0;
3420
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003421err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003422 s3c_hsotg_phy_disable(hsotg);
3423
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003424 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3425 regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3426
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003427 clk_disable(hsotg->clk);
3428 clk_put(hsotg->clk);
3429
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003430err_regs:
3431 iounmap(hsotg->regs);
3432
3433err_regs_res:
3434 release_resource(hsotg->regs_res);
3435 kfree(hsotg->regs_res);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003436err_clk:
3437 clk_put(hsotg->clk);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003438err_mem:
3439 kfree(hsotg);
3440 return ret;
3441}
3442
3443static int __devexit s3c_hsotg_remove(struct platform_device *pdev)
3444{
3445 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3446
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003447 usb_del_gadget_udc(&hsotg->gadget);
3448
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003449 s3c_hsotg_delete_debug(hsotg);
3450
3451 usb_gadget_unregister_driver(hsotg->driver);
3452
3453 free_irq(hsotg->irq, hsotg);
3454 iounmap(hsotg->regs);
3455
3456 release_resource(hsotg->regs_res);
3457 kfree(hsotg->regs_res);
3458
Lukasz Majewski41188782012-05-04 14:17:01 +02003459 s3c_hsotg_phy_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003460
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003461
3462 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3463 regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3464
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003465 clk_disable(hsotg->clk);
3466 clk_put(hsotg->clk);
3467
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003468 kfree(hsotg);
3469 return 0;
3470}
3471
3472#if 1
3473#define s3c_hsotg_suspend NULL
3474#define s3c_hsotg_resume NULL
3475#endif
3476
3477static struct platform_driver s3c_hsotg_driver = {
3478 .driver = {
3479 .name = "s3c-hsotg",
3480 .owner = THIS_MODULE,
3481 },
3482 .probe = s3c_hsotg_probe,
3483 .remove = __devexit_p(s3c_hsotg_remove),
3484 .suspend = s3c_hsotg_suspend,
3485 .resume = s3c_hsotg_resume,
3486};
3487
Axel Lincc27c962011-11-27 20:16:27 +08003488module_platform_driver(s3c_hsotg_driver);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003489
3490MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3491MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3492MODULE_LICENSE("GPL");
3493MODULE_ALIAS("platform:s3c-hsotg");