blob: 8d8652876c6730a684fc0f5723ec1c9a5eca9d1e [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 Dooks6d091ee72010-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 Dooks6d091ee72010-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/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002142 * s3c_hsotg_corereset - issue softreset to the core
2143 * @hsotg: The device state
2144 *
2145 * Issue a soft reset to the core, and await the core finishing it.
2146*/
2147static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2148{
2149 int timeout;
2150 u32 grstctl;
2151
2152 dev_dbg(hsotg->dev, "resetting core\n");
2153
2154 /* issue soft reset */
2155 writel(S3C_GRSTCTL_CSftRst, hsotg->regs + S3C_GRSTCTL);
2156
2157 timeout = 1000;
2158 do {
2159 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2160 } while ((grstctl & S3C_GRSTCTL_CSftRst) && timeout-- > 0);
2161
2162 if (grstctl & S3C_GRSTCTL_CSftRst) {
2163 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2164 return -EINVAL;
2165 }
2166
2167 timeout = 1000;
2168
2169 while (1) {
2170 u32 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2171
2172 if (timeout-- < 0) {
2173 dev_info(hsotg->dev,
2174 "%s: reset failed, GRSTCTL=%08x\n",
2175 __func__, grstctl);
2176 return -ETIMEDOUT;
2177 }
2178
2179 if (!(grstctl & S3C_GRSTCTL_AHBIdle))
2180 continue;
2181
2182 break; /* reset done */
2183 }
2184
2185 dev_dbg(hsotg->dev, "reset successful\n");
2186 return 0;
2187}
2188
2189static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2190{
2191 s3c_hsotg_corereset(hsotg);
2192
2193 /*
2194 * we must now enable ep0 ready for host detection and then
2195 * set configuration.
2196 */
2197
2198 /* set the PLL on, remove the HNP/SRP and set the PHY */
2199 writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) |
2200 (0x5 << 10), hsotg->regs + S3C_GUSBCFG);
2201
2202 s3c_hsotg_init_fifo(hsotg);
2203
2204 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2205
2206 writel(1 << 18 | S3C_DCFG_DevSpd_HS, hsotg->regs + S3C_DCFG);
2207
2208 /* Clear any pending OTG interrupts */
2209 writel(0xffffffff, hsotg->regs + S3C_GOTGINT);
2210
2211 /* Clear any pending interrupts */
2212 writel(0xffffffff, hsotg->regs + S3C_GINTSTS);
2213
2214 writel(S3C_GINTSTS_DisconnInt | S3C_GINTSTS_SessReqInt |
2215 S3C_GINTSTS_GOUTNakEff | S3C_GINTSTS_GINNakEff |
2216 S3C_GINTSTS_ConIDStsChng | S3C_GINTSTS_USBRst |
2217 S3C_GINTSTS_EnumDone | S3C_GINTSTS_OTGInt |
2218 S3C_GINTSTS_USBSusp | S3C_GINTSTS_WkUpInt |
2219 S3C_GINTSTS_ErlySusp,
2220 hsotg->regs + S3C_GINTMSK);
2221
2222 if (using_dma(hsotg))
2223 writel(S3C_GAHBCFG_GlblIntrEn | S3C_GAHBCFG_DMAEn |
2224 S3C_GAHBCFG_HBstLen_Incr4,
2225 hsotg->regs + S3C_GAHBCFG);
2226 else
2227 writel(S3C_GAHBCFG_GlblIntrEn, hsotg->regs + S3C_GAHBCFG);
2228
2229 /*
2230 * Enabling INTknTXFEmpMsk here seems to be a big mistake, we end
2231 * up being flooded with interrupts if the host is polling the
2232 * endpoint to try and read data.
2233 */
2234
2235 writel(((hsotg->dedicated_fifos) ? S3C_DIEPMSK_TxFIFOEmpty : 0) |
2236 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk |
2237 S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2238 S3C_DIEPMSK_INTknEPMisMsk,
2239 hsotg->regs + S3C_DIEPMSK);
2240
2241 /*
2242 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2243 * DMA mode we may need this.
2244 */
2245 writel((using_dma(hsotg) ? (S3C_DIEPMSK_XferComplMsk |
2246 S3C_DIEPMSK_TimeOUTMsk) : 0) |
2247 S3C_DOEPMSK_EPDisbldMsk | S3C_DOEPMSK_AHBErrMsk |
2248 S3C_DOEPMSK_SetupMsk,
2249 hsotg->regs + S3C_DOEPMSK);
2250
2251 writel(0, hsotg->regs + S3C_DAINTMSK);
2252
2253 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2254 readl(hsotg->regs + S3C_DIEPCTL0),
2255 readl(hsotg->regs + S3C_DOEPCTL0));
2256
2257 /* enable in and out endpoint interrupts */
2258 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt);
2259
2260 /*
2261 * Enable the RXFIFO when in slave mode, as this is how we collect
2262 * the data. In DMA mode, we get events from the FIFO but also
2263 * things we cannot process, so do not use it.
2264 */
2265 if (!using_dma(hsotg))
2266 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_RxFLvl);
2267
2268 /* Enable interrupts for EP0 in and out */
2269 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2270 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2271
2272 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2273 udelay(10); /* see openiboot */
2274 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2275
2276 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + S3C_DCTL));
2277
2278 /*
2279 * S3C_DxEPCTL_USBActEp says RO in manual, but seems to be set by
2280 * writing to the EPCTL register..
2281 */
2282
2283 /* set to read 1 8byte packet */
2284 writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
2285 S3C_DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
2286
2287 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2288 S3C_DxEPCTL_CNAK | S3C_DxEPCTL_EPEna |
2289 S3C_DxEPCTL_USBActEp,
2290 hsotg->regs + S3C_DOEPCTL0);
2291
2292 /* enable, but don't activate EP0in */
2293 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2294 S3C_DxEPCTL_USBActEp, hsotg->regs + S3C_DIEPCTL0);
2295
2296 s3c_hsotg_enqueue_setup(hsotg);
2297
2298 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2299 readl(hsotg->regs + S3C_DIEPCTL0),
2300 readl(hsotg->regs + S3C_DOEPCTL0));
2301
2302 /* clear global NAKs */
2303 writel(S3C_DCTL_CGOUTNak | S3C_DCTL_CGNPInNAK,
2304 hsotg->regs + S3C_DCTL);
2305
2306 /* must be at-least 3ms to allow bus to see disconnect */
2307 mdelay(3);
2308
2309 /* remove the soft-disconnect and let's go */
2310 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2311}
2312
2313/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002314 * s3c_hsotg_irq - handle device interrupt
2315 * @irq: The IRQ number triggered
2316 * @pw: The pw value when registered the handler.
2317 */
2318static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2319{
2320 struct s3c_hsotg *hsotg = pw;
2321 int retry_count = 8;
2322 u32 gintsts;
2323 u32 gintmsk;
2324
2325irq_retry:
2326 gintsts = readl(hsotg->regs + S3C_GINTSTS);
2327 gintmsk = readl(hsotg->regs + S3C_GINTMSK);
2328
2329 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2330 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2331
2332 gintsts &= gintmsk;
2333
2334 if (gintsts & S3C_GINTSTS_OTGInt) {
2335 u32 otgint = readl(hsotg->regs + S3C_GOTGINT);
2336
2337 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2338
2339 writel(otgint, hsotg->regs + S3C_GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002340 }
2341
2342 if (gintsts & S3C_GINTSTS_DisconnInt) {
2343 dev_dbg(hsotg->dev, "%s: DisconnInt\n", __func__);
2344 writel(S3C_GINTSTS_DisconnInt, hsotg->regs + S3C_GINTSTS);
2345
2346 s3c_hsotg_disconnect_irq(hsotg);
2347 }
2348
2349 if (gintsts & S3C_GINTSTS_SessReqInt) {
2350 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
2351 writel(S3C_GINTSTS_SessReqInt, hsotg->regs + S3C_GINTSTS);
2352 }
2353
2354 if (gintsts & S3C_GINTSTS_EnumDone) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002355 writel(S3C_GINTSTS_EnumDone, hsotg->regs + S3C_GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002356
2357 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002358 }
2359
2360 if (gintsts & S3C_GINTSTS_ConIDStsChng) {
2361 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
2362 readl(hsotg->regs + S3C_DSTS),
2363 readl(hsotg->regs + S3C_GOTGCTL));
2364
2365 writel(S3C_GINTSTS_ConIDStsChng, hsotg->regs + S3C_GINTSTS);
2366 }
2367
2368 if (gintsts & (S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt)) {
2369 u32 daint = readl(hsotg->regs + S3C_DAINT);
2370 u32 daint_out = daint >> S3C_DAINT_OutEP_SHIFT;
2371 u32 daint_in = daint & ~(daint_out << S3C_DAINT_OutEP_SHIFT);
2372 int ep;
2373
2374 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2375
2376 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2377 if (daint_out & 1)
2378 s3c_hsotg_epint(hsotg, ep, 0);
2379 }
2380
2381 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2382 if (daint_in & 1)
2383 s3c_hsotg_epint(hsotg, ep, 1);
2384 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002385 }
2386
2387 if (gintsts & S3C_GINTSTS_USBRst) {
2388 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2389 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2390 readl(hsotg->regs + S3C_GNPTXSTS));
2391
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002392 writel(S3C_GINTSTS_USBRst, hsotg->regs + S3C_GINTSTS);
2393
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002394 kill_all_requests(hsotg, &hsotg->eps[0], -ECONNRESET, true);
2395
2396 /* it seems after a reset we can end up with a situation
Ben Dooksb3864ce2010-07-19 09:40:43 +01002397 * where the TXFIFO still has data in it... the docs
2398 * suggest resetting all the fifos, so use the init_fifo
2399 * code to relayout and flush the fifos.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002400 */
2401
Ben Dooksb3864ce2010-07-19 09:40:43 +01002402 s3c_hsotg_init_fifo(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002403
2404 s3c_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002405 }
2406
2407 /* check both FIFOs */
2408
2409 if (gintsts & S3C_GINTSTS_NPTxFEmp) {
2410 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2411
2412 /* Disable the interrupt to stop it happening again
2413 * unless one of these endpoint routines decides that
2414 * it needs re-enabling */
2415
2416 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
2417 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002418 }
2419
2420 if (gintsts & S3C_GINTSTS_PTxFEmp) {
2421 dev_dbg(hsotg->dev, "PTxFEmp\n");
2422
2423 /* See note in S3C_GINTSTS_NPTxFEmp */
2424
2425 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
2426 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002427 }
2428
2429 if (gintsts & S3C_GINTSTS_RxFLvl) {
2430 /* note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
2431 * we need to retry s3c_hsotg_handle_rx if this is still
2432 * set. */
2433
2434 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002435 }
2436
2437 if (gintsts & S3C_GINTSTS_ModeMis) {
2438 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
2439 writel(S3C_GINTSTS_ModeMis, hsotg->regs + S3C_GINTSTS);
2440 }
2441
2442 if (gintsts & S3C_GINTSTS_USBSusp) {
2443 dev_info(hsotg->dev, "S3C_GINTSTS_USBSusp\n");
2444 writel(S3C_GINTSTS_USBSusp, hsotg->regs + S3C_GINTSTS);
2445
2446 call_gadget(hsotg, suspend);
2447 }
2448
2449 if (gintsts & S3C_GINTSTS_WkUpInt) {
2450 dev_info(hsotg->dev, "S3C_GINTSTS_WkUpIn\n");
2451 writel(S3C_GINTSTS_WkUpInt, hsotg->regs + S3C_GINTSTS);
2452
2453 call_gadget(hsotg, resume);
2454 }
2455
2456 if (gintsts & S3C_GINTSTS_ErlySusp) {
2457 dev_dbg(hsotg->dev, "S3C_GINTSTS_ErlySusp\n");
2458 writel(S3C_GINTSTS_ErlySusp, hsotg->regs + S3C_GINTSTS);
2459 }
2460
2461 /* these next two seem to crop-up occasionally causing the core
2462 * to shutdown the USB transfer, so try clearing them and logging
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002463 * the occurrence. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002464
2465 if (gintsts & S3C_GINTSTS_GOUTNakEff) {
2466 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2467
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002468 writel(S3C_DCTL_CGOUTNak, hsotg->regs + S3C_DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002469
2470 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002471 }
2472
2473 if (gintsts & S3C_GINTSTS_GINNakEff) {
2474 dev_info(hsotg->dev, "GINNakEff triggered\n");
2475
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002476 writel(S3C_DCTL_CGNPInNAK, hsotg->regs + S3C_DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002477
2478 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002479 }
2480
2481 /* if we've had fifo events, we should try and go around the
2482 * loop again to see if there's any point in returning yet. */
2483
2484 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2485 goto irq_retry;
2486
2487 return IRQ_HANDLED;
2488}
2489
2490/**
2491 * s3c_hsotg_ep_enable - enable the given endpoint
2492 * @ep: The USB endpint to configure
2493 * @desc: The USB endpoint descriptor to configure with.
2494 *
2495 * This is called from the USB gadget code's usb_ep_enable().
2496*/
2497static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2498 const struct usb_endpoint_descriptor *desc)
2499{
2500 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2501 struct s3c_hsotg *hsotg = hs_ep->parent;
2502 unsigned long flags;
2503 int index = hs_ep->index;
2504 u32 epctrl_reg;
2505 u32 epctrl;
2506 u32 mps;
2507 int dir_in;
Julia Lawall19c190f2010-03-29 17:36:44 +02002508 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002509
2510 dev_dbg(hsotg->dev,
2511 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2512 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2513 desc->wMaxPacketSize, desc->bInterval);
2514
2515 /* not to be called for EP0 */
2516 WARN_ON(index == 0);
2517
2518 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2519 if (dir_in != hs_ep->dir_in) {
2520 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2521 return -EINVAL;
2522 }
2523
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002524 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002525
2526 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2527
2528 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2529 epctrl = readl(hsotg->regs + epctrl_reg);
2530
2531 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2532 __func__, epctrl, epctrl_reg);
2533
2534 spin_lock_irqsave(&hs_ep->lock, flags);
2535
2536 epctrl &= ~(S3C_DxEPCTL_EPType_MASK | S3C_DxEPCTL_MPS_MASK);
2537 epctrl |= S3C_DxEPCTL_MPS(mps);
2538
2539 /* mark the endpoint as active, otherwise the core may ignore
2540 * transactions entirely for this endpoint */
2541 epctrl |= S3C_DxEPCTL_USBActEp;
2542
2543 /* set the NAK status on the endpoint, otherwise we might try and
2544 * do something with data that we've yet got a request to process
2545 * since the RXFIFO will take data for an endpoint even if the
2546 * size register hasn't been set.
2547 */
2548
2549 epctrl |= S3C_DxEPCTL_SNAK;
2550
2551 /* update the endpoint state */
2552 hs_ep->ep.maxpacket = mps;
2553
2554 /* default, set to non-periodic */
2555 hs_ep->periodic = 0;
2556
2557 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2558 case USB_ENDPOINT_XFER_ISOC:
2559 dev_err(hsotg->dev, "no current ISOC support\n");
Julia Lawall19c190f2010-03-29 17:36:44 +02002560 ret = -EINVAL;
2561 goto out;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002562
2563 case USB_ENDPOINT_XFER_BULK:
2564 epctrl |= S3C_DxEPCTL_EPType_Bulk;
2565 break;
2566
2567 case USB_ENDPOINT_XFER_INT:
2568 if (dir_in) {
2569 /* Allocate our TxFNum by simply using the index
2570 * of the endpoint for the moment. We could do
2571 * something better if the host indicates how
2572 * many FIFOs we are expecting to use. */
2573
2574 hs_ep->periodic = 1;
2575 epctrl |= S3C_DxEPCTL_TxFNum(index);
2576 }
2577
2578 epctrl |= S3C_DxEPCTL_EPType_Intterupt;
2579 break;
2580
2581 case USB_ENDPOINT_XFER_CONTROL:
2582 epctrl |= S3C_DxEPCTL_EPType_Control;
2583 break;
2584 }
2585
Ben Dooks10aebc72010-07-19 09:40:44 +01002586 /* if the hardware has dedicated fifos, we must give each IN EP
2587 * a unique tx-fifo even if it is non-periodic.
2588 */
2589 if (dir_in && hsotg->dedicated_fifos)
2590 epctrl |= S3C_DxEPCTL_TxFNum(index);
2591
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002592 /* for non control endpoints, set PID to D0 */
2593 if (index)
2594 epctrl |= S3C_DxEPCTL_SetD0PID;
2595
2596 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2597 __func__, epctrl);
2598
2599 writel(epctrl, hsotg->regs + epctrl_reg);
2600 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2601 __func__, readl(hsotg->regs + epctrl_reg));
2602
2603 /* enable the endpoint interrupt */
2604 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2605
Julia Lawall19c190f2010-03-29 17:36:44 +02002606out:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002607 spin_unlock_irqrestore(&hs_ep->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002608 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002609}
2610
2611static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2612{
2613 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2614 struct s3c_hsotg *hsotg = hs_ep->parent;
2615 int dir_in = hs_ep->dir_in;
2616 int index = hs_ep->index;
2617 unsigned long flags;
2618 u32 epctrl_reg;
2619 u32 ctrl;
2620
2621 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2622
2623 if (ep == &hsotg->eps[0].ep) {
2624 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2625 return -EINVAL;
2626 }
2627
2628 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2629
2630 /* terminate all requests with shutdown */
2631 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2632
2633 spin_lock_irqsave(&hs_ep->lock, flags);
2634
2635 ctrl = readl(hsotg->regs + epctrl_reg);
2636 ctrl &= ~S3C_DxEPCTL_EPEna;
2637 ctrl &= ~S3C_DxEPCTL_USBActEp;
2638 ctrl |= S3C_DxEPCTL_SNAK;
2639
2640 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2641 writel(ctrl, hsotg->regs + epctrl_reg);
2642
2643 /* disable endpoint interrupts */
2644 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2645
2646 spin_unlock_irqrestore(&hs_ep->lock, flags);
2647 return 0;
2648}
2649
2650/**
2651 * on_list - check request is on the given endpoint
2652 * @ep: The endpoint to check.
2653 * @test: The request to test if it is on the endpoint.
2654*/
2655static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2656{
2657 struct s3c_hsotg_req *req, *treq;
2658
2659 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2660 if (req == test)
2661 return true;
2662 }
2663
2664 return false;
2665}
2666
2667static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2668{
2669 struct s3c_hsotg_req *hs_req = our_req(req);
2670 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2671 struct s3c_hsotg *hs = hs_ep->parent;
2672 unsigned long flags;
2673
2674 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2675
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002676 spin_lock_irqsave(&hs_ep->lock, flags);
2677
2678 if (!on_list(hs_ep, hs_req)) {
2679 spin_unlock_irqrestore(&hs_ep->lock, flags);
2680 return -EINVAL;
2681 }
2682
2683 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
2684 spin_unlock_irqrestore(&hs_ep->lock, flags);
2685
2686 return 0;
2687}
2688
2689static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2690{
2691 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2692 struct s3c_hsotg *hs = hs_ep->parent;
2693 int index = hs_ep->index;
2694 unsigned long irqflags;
2695 u32 epreg;
2696 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002697 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002698
2699 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2700
2701 spin_lock_irqsave(&hs_ep->lock, irqflags);
2702
2703 /* write both IN and OUT control registers */
2704
2705 epreg = S3C_DIEPCTL(index);
2706 epctl = readl(hs->regs + epreg);
2707
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002708 if (value) {
2709 epctl |= S3C_DxEPCTL_Stall + S3C_DxEPCTL_SNAK;
2710 if (epctl & S3C_DxEPCTL_EPEna)
2711 epctl |= S3C_DxEPCTL_EPDis;
2712 } else {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002713 epctl &= ~S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002714 xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2715 if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2716 xfertype == S3C_DxEPCTL_EPType_Intterupt)
2717 epctl |= S3C_DxEPCTL_SetD0PID;
2718 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002719
2720 writel(epctl, hs->regs + epreg);
2721
2722 epreg = S3C_DOEPCTL(index);
2723 epctl = readl(hs->regs + epreg);
2724
2725 if (value)
2726 epctl |= S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002727 else {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002728 epctl &= ~S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002729 xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2730 if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2731 xfertype == S3C_DxEPCTL_EPType_Intterupt)
2732 epctl |= S3C_DxEPCTL_SetD0PID;
2733 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002734
2735 writel(epctl, hs->regs + epreg);
2736
2737 spin_unlock_irqrestore(&hs_ep->lock, irqflags);
2738
2739 return 0;
2740}
2741
2742static struct usb_ep_ops s3c_hsotg_ep_ops = {
2743 .enable = s3c_hsotg_ep_enable,
2744 .disable = s3c_hsotg_ep_disable,
2745 .alloc_request = s3c_hsotg_ep_alloc_request,
2746 .free_request = s3c_hsotg_ep_free_request,
2747 .queue = s3c_hsotg_ep_queue,
2748 .dequeue = s3c_hsotg_ep_dequeue,
2749 .set_halt = s3c_hsotg_ep_sethalt,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002750 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002751};
2752
2753/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002754 * s3c_hsotg_phy_enable - enable platform phy dev
2755 *
2756 * @param: The driver state
2757 *
2758 * A wrapper for platform code responsible for controlling
2759 * low-level USB code
2760 */
2761static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2762{
2763 struct platform_device *pdev = to_platform_device(hsotg->dev);
2764
2765 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
2766 if (hsotg->plat->phy_init)
2767 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2768}
2769
2770/**
2771 * s3c_hsotg_phy_disable - disable platform phy dev
2772 *
2773 * @param: The driver state
2774 *
2775 * A wrapper for platform code responsible for controlling
2776 * low-level USB code
2777 */
2778static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2779{
2780 struct platform_device *pdev = to_platform_device(hsotg->dev);
2781
2782 if (hsotg->plat->phy_exit)
2783 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2784}
2785
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002786static int s3c_hsotg_start(struct usb_gadget_driver *driver,
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002787 int (*bind)(struct usb_gadget *))
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002788{
2789 struct s3c_hsotg *hsotg = our_hsotg;
2790 int ret;
2791
2792 if (!hsotg) {
2793 printk(KERN_ERR "%s: called with no device\n", __func__);
2794 return -ENODEV;
2795 }
2796
2797 if (!driver) {
2798 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2799 return -EINVAL;
2800 }
2801
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002802 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002803 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002804
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002805 if (!bind || !driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002806 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2807 return -EINVAL;
2808 }
2809
2810 WARN_ON(hsotg->driver);
2811
2812 driver->driver.bus = NULL;
2813 hsotg->driver = driver;
2814 hsotg->gadget.dev.driver = &driver->driver;
2815 hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
2816 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2817
2818 ret = device_add(&hsotg->gadget.dev);
2819 if (ret) {
2820 dev_err(hsotg->dev, "failed to register gadget device\n");
2821 goto err;
2822 }
2823
Uwe Kleine-Königb0fca502010-08-12 17:43:53 +02002824 ret = bind(&hsotg->gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002825 if (ret) {
2826 dev_err(hsotg->dev, "failed bind %s\n", driver->driver.name);
2827
2828 hsotg->gadget.dev.driver = NULL;
2829 hsotg->driver = NULL;
2830 goto err;
2831 }
2832
Lukasz Majewski308d7342012-05-04 14:17:05 +02002833 s3c_hsotg_core_init(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002834
2835 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
2836 return 0;
2837
2838err:
2839 hsotg->driver = NULL;
2840 hsotg->gadget.dev.driver = NULL;
2841 return ret;
2842}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002843
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002844static int s3c_hsotg_stop(struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002845{
2846 struct s3c_hsotg *hsotg = our_hsotg;
2847 int ep;
2848
2849 if (!hsotg)
2850 return -ENODEV;
2851
2852 if (!driver || driver != hsotg->driver || !driver->unbind)
2853 return -EINVAL;
2854
2855 /* all endpoints should be shutdown */
2856 for (ep = 0; ep < S3C_HSOTG_EPS; ep++)
2857 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
2858
2859 call_gadget(hsotg, disconnect);
2860
2861 driver->unbind(&hsotg->gadget);
2862 hsotg->driver = NULL;
2863 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2864
2865 device_del(&hsotg->gadget.dev);
2866
2867 dev_info(hsotg->dev, "unregistered gadget driver '%s'\n",
2868 driver->driver.name);
2869
2870 return 0;
2871}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002872
2873static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2874{
2875 return s3c_hsotg_read_frameno(to_hsotg(gadget));
2876}
2877
2878static struct usb_gadget_ops s3c_hsotg_gadget_ops = {
2879 .get_frame = s3c_hsotg_gadget_getframe,
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03002880 .start = s3c_hsotg_start,
2881 .stop = s3c_hsotg_stop,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002882};
2883
2884/**
2885 * s3c_hsotg_initep - initialise a single endpoint
2886 * @hsotg: The device state.
2887 * @hs_ep: The endpoint to be initialised.
2888 * @epnum: The endpoint number
2889 *
2890 * Initialise the given endpoint (as part of the probe and device state
2891 * creation) to give to the gadget driver. Setup the endpoint name, any
2892 * direction information and other state that may be required.
2893 */
2894static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
2895 struct s3c_hsotg_ep *hs_ep,
2896 int epnum)
2897{
2898 u32 ptxfifo;
2899 char *dir;
2900
2901 if (epnum == 0)
2902 dir = "";
2903 else if ((epnum % 2) == 0) {
2904 dir = "out";
2905 } else {
2906 dir = "in";
2907 hs_ep->dir_in = 1;
2908 }
2909
2910 hs_ep->index = epnum;
2911
2912 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
2913
2914 INIT_LIST_HEAD(&hs_ep->queue);
2915 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
2916
2917 spin_lock_init(&hs_ep->lock);
2918
2919 /* add to the list of endpoints known by the gadget driver */
2920 if (epnum)
2921 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
2922
2923 hs_ep->parent = hsotg;
2924 hs_ep->ep.name = hs_ep->name;
2925 hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT;
2926 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
2927
2928 /* Read the FIFO size for the Periodic TX FIFO, even if we're
2929 * an OUT endpoint, we may as well do this if in future the
2930 * code is changed to make each endpoint's direction changeable.
2931 */
2932
2933 ptxfifo = readl(hsotg->regs + S3C_DPTXFSIZn(epnum));
Ben Dooks679f9b72010-07-19 09:40:41 +01002934 hs_ep->fifo_size = S3C_DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002935
2936 /* if we're using dma, we need to set the next-endpoint pointer
2937 * to be something valid.
2938 */
2939
2940 if (using_dma(hsotg)) {
2941 u32 next = S3C_DxEPCTL_NextEp((epnum + 1) % 15);
2942 writel(next, hsotg->regs + S3C_DIEPCTL(epnum));
2943 writel(next, hsotg->regs + S3C_DOEPCTL(epnum));
2944 }
2945}
2946
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002947static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2948{
Ben Dooks10aebc72010-07-19 09:40:44 +01002949 u32 cfg4;
2950
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002951 /* unmask subset of endpoint interrupts */
2952
2953 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2954 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk,
2955 hsotg->regs + S3C_DIEPMSK);
2956
2957 writel(S3C_DOEPMSK_SetupMsk | S3C_DOEPMSK_AHBErrMsk |
2958 S3C_DOEPMSK_EPDisbldMsk | S3C_DOEPMSK_XferComplMsk,
2959 hsotg->regs + S3C_DOEPMSK);
2960
2961 writel(0, hsotg->regs + S3C_DAINTMSK);
2962
Thomas Abraham390b1662010-05-24 17:48:56 +09002963 /* Be in disconnected state until gadget is registered */
2964 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2965
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002966 if (0) {
2967 /* post global nak until we're ready */
2968 writel(S3C_DCTL_SGNPInNAK | S3C_DCTL_SGOUTNak,
2969 hsotg->regs + S3C_DCTL);
2970 }
2971
2972 /* setup fifos */
2973
Mark Brown83a01802011-06-01 17:16:15 +01002974 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2975 readl(hsotg->regs + S3C_GRXFSIZ),
2976 readl(hsotg->regs + S3C_GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002977
2978 s3c_hsotg_init_fifo(hsotg);
2979
2980 /* set the PLL on, remove the HNP/SRP and set the PHY */
2981 writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) | (0x5 << 10),
2982 hsotg->regs + S3C_GUSBCFG);
2983
2984 writel(using_dma(hsotg) ? S3C_GAHBCFG_DMAEn : 0x0,
2985 hsotg->regs + S3C_GAHBCFG);
Ben Dooks10aebc72010-07-19 09:40:44 +01002986
2987 /* check hardware configuration */
2988
2989 cfg4 = readl(hsotg->regs + 0x50);
2990 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
2991
2992 dev_info(hsotg->dev, "%s fifos\n",
2993 hsotg->dedicated_fifos ? "dedicated" : "shared");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002994}
2995
2996static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
2997{
Mark Brown83a01802011-06-01 17:16:15 +01002998#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002999 struct device *dev = hsotg->dev;
3000 void __iomem *regs = hsotg->regs;
3001 u32 val;
3002 int idx;
3003
3004 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
3005 readl(regs + S3C_DCFG), readl(regs + S3C_DCTL),
3006 readl(regs + S3C_DIEPMSK));
3007
3008 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
3009 readl(regs + S3C_GAHBCFG), readl(regs + 0x44));
3010
3011 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
3012 readl(regs + S3C_GRXFSIZ), readl(regs + S3C_GNPTXFSIZ));
3013
3014 /* show periodic fifo settings */
3015
3016 for (idx = 1; idx <= 15; idx++) {
3017 val = readl(regs + S3C_DPTXFSIZn(idx));
3018 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
3019 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
3020 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
3021 }
3022
3023 for (idx = 0; idx < 15; idx++) {
3024 dev_info(dev,
3025 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
3026 readl(regs + S3C_DIEPCTL(idx)),
3027 readl(regs + S3C_DIEPTSIZ(idx)),
3028 readl(regs + S3C_DIEPDMA(idx)));
3029
3030 val = readl(regs + S3C_DOEPCTL(idx));
3031 dev_info(dev,
3032 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
3033 idx, readl(regs + S3C_DOEPCTL(idx)),
3034 readl(regs + S3C_DOEPTSIZ(idx)),
3035 readl(regs + S3C_DOEPDMA(idx)));
3036
3037 }
3038
3039 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
3040 readl(regs + S3C_DVBUSDIS), readl(regs + S3C_DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003041#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003042}
3043
3044
3045/**
3046 * state_show - debugfs: show overall driver and device state.
3047 * @seq: The seq file to write to.
3048 * @v: Unused parameter.
3049 *
3050 * This debugfs entry shows the overall state of the hardware and
3051 * some general information about each of the endpoints available
3052 * to the system.
3053 */
3054static int state_show(struct seq_file *seq, void *v)
3055{
3056 struct s3c_hsotg *hsotg = seq->private;
3057 void __iomem *regs = hsotg->regs;
3058 int idx;
3059
3060 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
3061 readl(regs + S3C_DCFG),
3062 readl(regs + S3C_DCTL),
3063 readl(regs + S3C_DSTS));
3064
3065 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
3066 readl(regs + S3C_DIEPMSK), readl(regs + S3C_DOEPMSK));
3067
3068 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
3069 readl(regs + S3C_GINTMSK),
3070 readl(regs + S3C_GINTSTS));
3071
3072 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
3073 readl(regs + S3C_DAINTMSK),
3074 readl(regs + S3C_DAINT));
3075
3076 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
3077 readl(regs + S3C_GNPTXSTS),
3078 readl(regs + S3C_GRXSTSR));
3079
3080 seq_printf(seq, "\nEndpoint status:\n");
3081
3082 for (idx = 0; idx < 15; idx++) {
3083 u32 in, out;
3084
3085 in = readl(regs + S3C_DIEPCTL(idx));
3086 out = readl(regs + S3C_DOEPCTL(idx));
3087
3088 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3089 idx, in, out);
3090
3091 in = readl(regs + S3C_DIEPTSIZ(idx));
3092 out = readl(regs + S3C_DOEPTSIZ(idx));
3093
3094 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3095 in, out);
3096
3097 seq_printf(seq, "\n");
3098 }
3099
3100 return 0;
3101}
3102
3103static int state_open(struct inode *inode, struct file *file)
3104{
3105 return single_open(file, state_show, inode->i_private);
3106}
3107
3108static const struct file_operations state_fops = {
3109 .owner = THIS_MODULE,
3110 .open = state_open,
3111 .read = seq_read,
3112 .llseek = seq_lseek,
3113 .release = single_release,
3114};
3115
3116/**
3117 * fifo_show - debugfs: show the fifo information
3118 * @seq: The seq_file to write data to.
3119 * @v: Unused parameter.
3120 *
3121 * Show the FIFO information for the overall fifo and all the
3122 * periodic transmission FIFOs.
3123*/
3124static int fifo_show(struct seq_file *seq, void *v)
3125{
3126 struct s3c_hsotg *hsotg = seq->private;
3127 void __iomem *regs = hsotg->regs;
3128 u32 val;
3129 int idx;
3130
3131 seq_printf(seq, "Non-periodic FIFOs:\n");
3132 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + S3C_GRXFSIZ));
3133
3134 val = readl(regs + S3C_GNPTXFSIZ);
3135 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
3136 val >> S3C_GNPTXFSIZ_NPTxFDep_SHIFT,
3137 val & S3C_GNPTXFSIZ_NPTxFStAddr_MASK);
3138
3139 seq_printf(seq, "\nPeriodic TXFIFOs:\n");
3140
3141 for (idx = 1; idx <= 15; idx++) {
3142 val = readl(regs + S3C_DPTXFSIZn(idx));
3143
3144 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
3145 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
3146 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
3147 }
3148
3149 return 0;
3150}
3151
3152static int fifo_open(struct inode *inode, struct file *file)
3153{
3154 return single_open(file, fifo_show, inode->i_private);
3155}
3156
3157static const struct file_operations fifo_fops = {
3158 .owner = THIS_MODULE,
3159 .open = fifo_open,
3160 .read = seq_read,
3161 .llseek = seq_lseek,
3162 .release = single_release,
3163};
3164
3165
3166static const char *decode_direction(int is_in)
3167{
3168 return is_in ? "in" : "out";
3169}
3170
3171/**
3172 * ep_show - debugfs: show the state of an endpoint.
3173 * @seq: The seq_file to write data to.
3174 * @v: Unused parameter.
3175 *
3176 * This debugfs entry shows the state of the given endpoint (one is
3177 * registered for each available).
3178*/
3179static int ep_show(struct seq_file *seq, void *v)
3180{
3181 struct s3c_hsotg_ep *ep = seq->private;
3182 struct s3c_hsotg *hsotg = ep->parent;
3183 struct s3c_hsotg_req *req;
3184 void __iomem *regs = hsotg->regs;
3185 int index = ep->index;
3186 int show_limit = 15;
3187 unsigned long flags;
3188
3189 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3190 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3191
3192 /* first show the register state */
3193
3194 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
3195 readl(regs + S3C_DIEPCTL(index)),
3196 readl(regs + S3C_DOEPCTL(index)));
3197
3198 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
3199 readl(regs + S3C_DIEPDMA(index)),
3200 readl(regs + S3C_DOEPDMA(index)));
3201
3202 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
3203 readl(regs + S3C_DIEPINT(index)),
3204 readl(regs + S3C_DOEPINT(index)));
3205
3206 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
3207 readl(regs + S3C_DIEPTSIZ(index)),
3208 readl(regs + S3C_DOEPTSIZ(index)));
3209
3210 seq_printf(seq, "\n");
3211 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3212 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3213
3214 seq_printf(seq, "request list (%p,%p):\n",
3215 ep->queue.next, ep->queue.prev);
3216
3217 spin_lock_irqsave(&ep->lock, flags);
3218
3219 list_for_each_entry(req, &ep->queue, queue) {
3220 if (--show_limit < 0) {
3221 seq_printf(seq, "not showing more requests...\n");
3222 break;
3223 }
3224
3225 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3226 req == ep->req ? '*' : ' ',
3227 req, req->req.length, req->req.buf);
3228 seq_printf(seq, "%d done, res %d\n",
3229 req->req.actual, req->req.status);
3230 }
3231
3232 spin_unlock_irqrestore(&ep->lock, flags);
3233
3234 return 0;
3235}
3236
3237static int ep_open(struct inode *inode, struct file *file)
3238{
3239 return single_open(file, ep_show, inode->i_private);
3240}
3241
3242static const struct file_operations ep_fops = {
3243 .owner = THIS_MODULE,
3244 .open = ep_open,
3245 .read = seq_read,
3246 .llseek = seq_lseek,
3247 .release = single_release,
3248};
3249
3250/**
3251 * s3c_hsotg_create_debug - create debugfs directory and files
3252 * @hsotg: The driver state
3253 *
3254 * Create the debugfs files to allow the user to get information
3255 * about the state of the system. The directory name is created
3256 * with the same name as the device itself, in case we end up
3257 * with multiple blocks in future systems.
3258*/
3259static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3260{
3261 struct dentry *root;
3262 unsigned epidx;
3263
3264 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3265 hsotg->debug_root = root;
3266 if (IS_ERR(root)) {
3267 dev_err(hsotg->dev, "cannot create debug root\n");
3268 return;
3269 }
3270
3271 /* create general state file */
3272
3273 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3274 hsotg, &state_fops);
3275
3276 if (IS_ERR(hsotg->debug_file))
3277 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3278
3279 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3280 hsotg, &fifo_fops);
3281
3282 if (IS_ERR(hsotg->debug_fifo))
3283 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3284
3285 /* create one file for each endpoint */
3286
3287 for (epidx = 0; epidx < S3C_HSOTG_EPS; epidx++) {
3288 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3289
3290 ep->debugfs = debugfs_create_file(ep->name, 0444,
3291 root, ep, &ep_fops);
3292
3293 if (IS_ERR(ep->debugfs))
3294 dev_err(hsotg->dev, "failed to create %s debug file\n",
3295 ep->name);
3296 }
3297}
3298
3299/**
3300 * s3c_hsotg_delete_debug - cleanup debugfs entries
3301 * @hsotg: The driver state
3302 *
3303 * Cleanup (remove) the debugfs files for use on module exit.
3304*/
3305static void __devexit s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3306{
3307 unsigned epidx;
3308
3309 for (epidx = 0; epidx < S3C_HSOTG_EPS; epidx++) {
3310 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3311 debugfs_remove(ep->debugfs);
3312 }
3313
3314 debugfs_remove(hsotg->debug_file);
3315 debugfs_remove(hsotg->debug_fifo);
3316 debugfs_remove(hsotg->debug_root);
3317}
3318
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003319static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3320{
3321 struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
3322 struct device *dev = &pdev->dev;
3323 struct s3c_hsotg *hsotg;
3324 struct resource *res;
3325 int epnum;
3326 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003327 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003328
Lukasz Majewski41188782012-05-04 14:17:01 +02003329 plat = pdev->dev.platform_data;
3330 if (!plat) {
3331 dev_err(&pdev->dev, "no platform data defined\n");
3332 return -EINVAL;
3333 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003334
3335 hsotg = kzalloc(sizeof(struct s3c_hsotg) +
3336 sizeof(struct s3c_hsotg_ep) * S3C_HSOTG_EPS,
3337 GFP_KERNEL);
3338 if (!hsotg) {
3339 dev_err(dev, "cannot get memory\n");
3340 return -ENOMEM;
3341 }
3342
3343 hsotg->dev = dev;
3344 hsotg->plat = plat;
3345
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003346 hsotg->clk = clk_get(&pdev->dev, "otg");
3347 if (IS_ERR(hsotg->clk)) {
3348 dev_err(dev, "cannot get otg clock\n");
Jingoo Han2328cea2011-05-13 21:26:23 +09003349 ret = PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003350 goto err_mem;
3351 }
3352
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003353 platform_set_drvdata(pdev, hsotg);
3354
3355 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3356 if (!res) {
3357 dev_err(dev, "cannot find register resource 0\n");
3358 ret = -EINVAL;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003359 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003360 }
3361
3362 hsotg->regs_res = request_mem_region(res->start, resource_size(res),
3363 dev_name(dev));
3364 if (!hsotg->regs_res) {
3365 dev_err(dev, "cannot reserve registers\n");
3366 ret = -ENOENT;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003367 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003368 }
3369
3370 hsotg->regs = ioremap(res->start, resource_size(res));
3371 if (!hsotg->regs) {
3372 dev_err(dev, "cannot map registers\n");
3373 ret = -ENXIO;
3374 goto err_regs_res;
3375 }
3376
3377 ret = platform_get_irq(pdev, 0);
3378 if (ret < 0) {
3379 dev_err(dev, "cannot find IRQ\n");
3380 goto err_regs;
3381 }
3382
3383 hsotg->irq = ret;
3384
3385 ret = request_irq(ret, s3c_hsotg_irq, 0, dev_name(dev), hsotg);
3386 if (ret < 0) {
3387 dev_err(dev, "cannot claim IRQ\n");
3388 goto err_regs;
3389 }
3390
3391 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3392
3393 device_initialize(&hsotg->gadget.dev);
3394
3395 dev_set_name(&hsotg->gadget.dev, "gadget");
3396
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003397 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003398 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3399 hsotg->gadget.name = dev_name(dev);
3400
3401 hsotg->gadget.dev.parent = dev;
3402 hsotg->gadget.dev.dma_mask = dev->dma_mask;
3403
3404 /* setup endpoint information */
3405
3406 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3407 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3408
3409 /* allocate EP0 request */
3410
3411 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3412 GFP_KERNEL);
3413 if (!hsotg->ctrl_req) {
3414 dev_err(dev, "failed to allocate ctrl req\n");
3415 goto err_regs;
3416 }
3417
3418 /* reset the system */
3419
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003420 clk_enable(hsotg->clk);
3421
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003422 /* regulators */
3423
3424 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3425 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3426
3427 ret = regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
3428 hsotg->supplies);
3429 if (ret) {
3430 dev_err(dev, "failed to request supplies: %d\n", ret);
3431 goto err_supplies;
3432 }
3433
3434 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3435 hsotg->supplies);
3436
3437 if (ret) {
3438 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3439 goto err_supplies;
3440 }
3441
Lukasz Majewski41188782012-05-04 14:17:01 +02003442 /* usb phy enable */
3443 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003444
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003445 s3c_hsotg_corereset(hsotg);
3446 s3c_hsotg_init(hsotg);
3447
3448 /* initialise the endpoints now the core has been initialised */
3449 for (epnum = 0; epnum < S3C_HSOTG_EPS; epnum++)
3450 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3451
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003452 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3453 if (ret)
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003454 goto err_supplies;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003455
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003456 s3c_hsotg_create_debug(hsotg);
3457
3458 s3c_hsotg_dump(hsotg);
3459
3460 our_hsotg = hsotg;
3461 return 0;
3462
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003463err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003464 s3c_hsotg_phy_disable(hsotg);
3465
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003466 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3467 regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3468
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003469 clk_disable(hsotg->clk);
3470 clk_put(hsotg->clk);
3471
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003472err_regs:
3473 iounmap(hsotg->regs);
3474
3475err_regs_res:
3476 release_resource(hsotg->regs_res);
3477 kfree(hsotg->regs_res);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003478err_clk:
3479 clk_put(hsotg->clk);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003480err_mem:
3481 kfree(hsotg);
3482 return ret;
3483}
3484
3485static int __devexit s3c_hsotg_remove(struct platform_device *pdev)
3486{
3487 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3488
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003489 usb_del_gadget_udc(&hsotg->gadget);
3490
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003491 s3c_hsotg_delete_debug(hsotg);
3492
3493 usb_gadget_unregister_driver(hsotg->driver);
3494
3495 free_irq(hsotg->irq, hsotg);
3496 iounmap(hsotg->regs);
3497
3498 release_resource(hsotg->regs_res);
3499 kfree(hsotg->regs_res);
3500
Lukasz Majewski41188782012-05-04 14:17:01 +02003501 s3c_hsotg_phy_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003502
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003503
3504 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3505 regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3506
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003507 clk_disable(hsotg->clk);
3508 clk_put(hsotg->clk);
3509
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003510 kfree(hsotg);
3511 return 0;
3512}
3513
3514#if 1
3515#define s3c_hsotg_suspend NULL
3516#define s3c_hsotg_resume NULL
3517#endif
3518
3519static struct platform_driver s3c_hsotg_driver = {
3520 .driver = {
3521 .name = "s3c-hsotg",
3522 .owner = THIS_MODULE,
3523 },
3524 .probe = s3c_hsotg_probe,
3525 .remove = __devexit_p(s3c_hsotg_remove),
3526 .suspend = s3c_hsotg_suspend,
3527 .resume = s3c_hsotg_resume,
3528};
3529
Axel Lincc27c962011-11-27 20:16:27 +08003530module_platform_driver(s3c_hsotg_driver);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003531
3532MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3533MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3534MODULE_LICENSE("GPL");
3535MODULE_ALIAS("platform:s3c-hsotg");