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