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