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