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