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