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