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