blob: 710823b49dd4421a05f24ecc271c1a83cc389664 [file] [log] [blame]
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001/**
2 * linux/drivers/usb/gadget/s3c-hsotg.c
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003 *
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09004 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
5 * http://www.samsung.com
6 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01007 * Copyright 2008 Openmoko, Inc.
8 * Copyright 2008 Simtec Electronics
9 * Ben Dooks <ben@simtec.co.uk>
10 * http://armlinux.simtec.co.uk/
11 *
12 * S3C USB2.0 High-speed / OtG driver
13 *
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License version 2 as
16 * published by the Free Software Foundation.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020017 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010018
19#include <linux/kernel.h>
20#include <linux/module.h>
21#include <linux/spinlock.h>
22#include <linux/interrupt.h>
23#include <linux/platform_device.h>
24#include <linux/dma-mapping.h>
25#include <linux/debugfs.h>
26#include <linux/seq_file.h>
27#include <linux/delay.h>
28#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090029#include <linux/slab.h>
Maurus Cuelenaeree50bf382010-07-19 09:40:50 +010030#include <linux/clk.h>
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020031#include <linux/regulator/consumer.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010032
33#include <linux/usb/ch9.h>
34#include <linux/usb/gadget.h>
35
36#include <mach/map.h>
37
Lukasz Majewski127d42a2012-05-04 14:16:59 +020038#include "s3c-hsotg.h"
39#include <linux/platform_data/s3c-hsotg.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010040
41#define DMA_ADDR_INVALID (~((dma_addr_t)0))
42
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020043static const char * const s3c_hsotg_supply_names[] = {
44 "vusb_d", /* digital USB supply, 1.2V */
45 "vusb_a", /* analog USB supply, 1.1V */
46};
47
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020048/*
49 * EP0_MPS_LIMIT
Ben Dooks5b7d70c2009-06-02 14:58:06 +010050 *
51 * Unfortunately there seems to be a limit of the amount of data that can
Lucas De Marchi25985ed2011-03-30 22:57:33 -030052 * be transferred by IN transactions on EP0. This is either 127 bytes or 3
53 * packets (which practically means 1 packet and 63 bytes of data) when the
Ben Dooks5b7d70c2009-06-02 14:58:06 +010054 * MPS is set to 64.
55 *
56 * This means if we are wanting to move >127 bytes of data, we need to
57 * split the transactions up, but just doing one packet at a time does
58 * not work (this may be an implicit DATA0 PID on first packet of the
59 * transaction) and doing 2 packets is outside the controller's limits.
60 *
61 * If we try to lower the MPS size for EP0, then no transfers work properly
62 * for EP0, and the system will fail basic enumeration. As no cause for this
63 * has currently been found, we cannot support any large IN transfers for
64 * EP0.
65 */
66#define EP0_MPS_LIMIT 64
67
68struct s3c_hsotg;
69struct s3c_hsotg_req;
70
71/**
72 * struct s3c_hsotg_ep - driver endpoint definition.
73 * @ep: The gadget layer representation of the endpoint.
74 * @name: The driver generated name for the endpoint.
75 * @queue: Queue of requests for this endpoint.
76 * @parent: Reference back to the parent device structure.
77 * @req: The current request that the endpoint is processing. This is
78 * used to indicate an request has been loaded onto the endpoint
79 * and has yet to be completed (maybe due to data move, or simply
80 * awaiting an ack from the core all the data has been completed).
81 * @debugfs: File entry for debugfs file for this endpoint.
82 * @lock: State lock to protect contents of endpoint.
83 * @dir_in: Set to true if this endpoint is of the IN direction, which
84 * means that it is sending data to the Host.
85 * @index: The index for the endpoint registers.
86 * @name: The name array passed to the USB core.
87 * @halted: Set if the endpoint has been halted.
88 * @periodic: Set if this is a periodic ep, such as Interrupt
89 * @sent_zlp: Set if we've sent a zero-length packet.
90 * @total_data: The total number of data bytes done.
91 * @fifo_size: The size of the FIFO (for periodic IN endpoints)
92 * @fifo_load: The amount of data loaded into the FIFO (periodic IN)
93 * @last_load: The offset of data for the last start of request.
94 * @size_loaded: The last loaded size for DxEPTSIZE for periodic IN
95 *
96 * This is the driver's state for each registered enpoint, allowing it
97 * to keep track of transactions that need doing. Each endpoint has a
98 * lock to protect the state, to try and avoid using an overall lock
99 * for the host controller as much as possible.
100 *
101 * For periodic IN endpoints, we have fifo_size and fifo_load to try
102 * and keep track of the amount of data in the periodic FIFO for each
103 * of these as we don't have a status register that tells us how much
Ben Dookse7a9ff52010-07-19 09:40:42 +0100104 * is in each of them. (note, this may actually be useless information
105 * as in shared-fifo mode periodic in acts like a single-frame packet
106 * buffer than a fifo)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100107 */
108struct s3c_hsotg_ep {
109 struct usb_ep ep;
110 struct list_head queue;
111 struct s3c_hsotg *parent;
112 struct s3c_hsotg_req *req;
113 struct dentry *debugfs;
114
115 spinlock_t lock;
116
117 unsigned long total_data;
118 unsigned int size_loaded;
119 unsigned int last_load;
120 unsigned int fifo_load;
121 unsigned short fifo_size;
122
123 unsigned char dir_in;
124 unsigned char index;
125
126 unsigned int halted:1;
127 unsigned int periodic:1;
128 unsigned int sent_zlp:1;
129
130 char name[10];
131};
132
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100133/**
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.
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200143 * @num_of_eps: Number of available EPs (excluding EP0)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100144 * @debug_root: root directrory for debugfs.
145 * @debug_file: main status file for debugfs.
146 * @debug_fifo: FIFO status file for debugfs.
147 * @ep0_reply: Request used for ep0 reply.
148 * @ep0_buff: Buffer for EP0 reply data, if needed.
149 * @ctrl_buff: Buffer for EP0 control requests.
150 * @ctrl_req: Request for EP0 control packets.
Lukasz Majewski71225be2012-05-04 14:17:03 +0200151 * @setup: NAK management for EP0 SETUP
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200152 * @last_rst: Time of last reset
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100153 * @eps: The endpoints being supplied to the gadget framework
154 */
155struct s3c_hsotg {
156 struct device *dev;
157 struct usb_gadget_driver *driver;
158 struct s3c_hsotg_plat *plat;
159
160 void __iomem *regs;
161 struct resource *regs_res;
162 int irq;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +0200163 struct clk *clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100164
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200165 struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
166
Ben Dooks10aebc72010-07-19 09:40:44 +0100167 unsigned int dedicated_fifos:1;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200168 unsigned char num_of_eps;
Ben Dooks10aebc72010-07-19 09:40:44 +0100169
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100170 struct dentry *debug_root;
171 struct dentry *debug_file;
172 struct dentry *debug_fifo;
173
174 struct usb_request *ep0_reply;
175 struct usb_request *ctrl_req;
176 u8 ep0_buff[8];
177 u8 ctrl_buff[8];
178
179 struct usb_gadget gadget;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200180 unsigned int setup;
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200181 unsigned long last_rst;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200182 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100183};
184
185/**
186 * struct s3c_hsotg_req - data transfer request
187 * @req: The USB gadget request
188 * @queue: The list of requests for the endpoint this is queued for.
189 * @in_progress: Has already had size/packets written to core
190 * @mapped: DMA buffer for this request has been mapped via dma_map_single().
191 */
192struct s3c_hsotg_req {
193 struct usb_request req;
194 struct list_head queue;
195 unsigned char in_progress;
196 unsigned char mapped;
197};
198
199/* conversion functions */
200static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
201{
202 return container_of(req, struct s3c_hsotg_req, req);
203}
204
205static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
206{
207 return container_of(ep, struct s3c_hsotg_ep, ep);
208}
209
210static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
211{
212 return container_of(gadget, struct s3c_hsotg, gadget);
213}
214
215static inline void __orr32(void __iomem *ptr, u32 val)
216{
217 writel(readl(ptr) | val, ptr);
218}
219
220static inline void __bic32(void __iomem *ptr, u32 val)
221{
222 writel(readl(ptr) & ~val, ptr);
223}
224
225/* forward decleration of functions */
226static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
227
228/**
229 * using_dma - return the DMA status of the driver.
230 * @hsotg: The driver state.
231 *
232 * Return true if we're using DMA.
233 *
234 * Currently, we have the DMA support code worked into everywhere
235 * that needs it, but the AMBA DMA implementation in the hardware can
236 * only DMA from 32bit aligned addresses. This means that gadgets such
237 * as the CDC Ethernet cannot work as they often pass packets which are
238 * not 32bit aligned.
239 *
240 * Unfortunately the choice to use DMA or not is global to the controller
241 * and seems to be only settable when the controller is being put through
242 * a core reset. This means we either need to fix the gadgets to take
243 * account of DMA alignment, or add bounce buffers (yuerk).
244 *
245 * Until this issue is sorted out, we always return 'false'.
246 */
247static inline bool using_dma(struct s3c_hsotg *hsotg)
248{
249 return false; /* support is not complete */
250}
251
252/**
253 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
254 * @hsotg: The device state
255 * @ints: A bitmask of the interrupts to enable
256 */
257static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
258{
259 u32 gsintmsk = readl(hsotg->regs + S3C_GINTMSK);
260 u32 new_gsintmsk;
261
262 new_gsintmsk = gsintmsk | ints;
263
264 if (new_gsintmsk != gsintmsk) {
265 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
266 writel(new_gsintmsk, hsotg->regs + S3C_GINTMSK);
267 }
268}
269
270/**
271 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
272 * @hsotg: The device state
273 * @ints: A bitmask of the interrupts to enable
274 */
275static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
276{
277 u32 gsintmsk = readl(hsotg->regs + S3C_GINTMSK);
278 u32 new_gsintmsk;
279
280 new_gsintmsk = gsintmsk & ~ints;
281
282 if (new_gsintmsk != gsintmsk)
283 writel(new_gsintmsk, hsotg->regs + S3C_GINTMSK);
284}
285
286/**
287 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
288 * @hsotg: The device state
289 * @ep: The endpoint index
290 * @dir_in: True if direction is in.
291 * @en: The enable value, true to enable
292 *
293 * Set or clear the mask for an individual endpoint's interrupt
294 * request.
295 */
296static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
297 unsigned int ep, unsigned int dir_in,
298 unsigned int en)
299{
300 unsigned long flags;
301 u32 bit = 1 << ep;
302 u32 daint;
303
304 if (!dir_in)
305 bit <<= 16;
306
307 local_irq_save(flags);
308 daint = readl(hsotg->regs + S3C_DAINTMSK);
309 if (en)
310 daint |= bit;
311 else
312 daint &= ~bit;
313 writel(daint, hsotg->regs + S3C_DAINTMSK);
314 local_irq_restore(flags);
315}
316
317/**
318 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
319 * @hsotg: The device instance.
320 */
321static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
322{
Ben Dooks0f002d22010-05-25 05:36:50 +0100323 unsigned int ep;
324 unsigned int addr;
325 unsigned int size;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100326 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100327 u32 val;
328
Ben Dooks6d091ee2010-07-19 09:40:40 +0100329 /* set FIFO sizes to 2048/1024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100330
331 writel(2048, hsotg->regs + S3C_GRXFSIZ);
332 writel(S3C_GNPTXFSIZ_NPTxFStAddr(2048) |
Ben Dooks6d091ee2010-07-19 09:40:40 +0100333 S3C_GNPTXFSIZ_NPTxFDep(1024),
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100334 hsotg->regs + S3C_GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100335
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200336 /*
337 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100338 * block have overlapping default addresses. This also ensures
339 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200340 * known values.
341 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100342
343 /* start at the end of the GNPTXFSIZ, rounded up */
344 addr = 2048 + 1024;
345 size = 768;
346
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200347 /*
348 * currently we allocate TX FIFOs for all possible endpoints,
349 * and assume that they are all the same size.
350 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100351
Anton Tikhomirovf7a83fe2012-03-06 14:05:49 +0900352 for (ep = 1; ep <= 15; ep++) {
Ben Dooks0f002d22010-05-25 05:36:50 +0100353 val = addr;
354 val |= size << S3C_DPTXFSIZn_DPTxFSize_SHIFT;
355 addr += size;
356
357 writel(val, hsotg->regs + S3C_DPTXFSIZn(ep));
358 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100359
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200360 /*
361 * according to p428 of the design guide, we need to ensure that
362 * all fifos are flushed before continuing
363 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100364
365 writel(S3C_GRSTCTL_TxFNum(0x10) | S3C_GRSTCTL_TxFFlsh |
366 S3C_GRSTCTL_RxFFlsh, hsotg->regs + S3C_GRSTCTL);
367
368 /* wait until the fifos are both flushed */
369 timeout = 100;
370 while (1) {
371 val = readl(hsotg->regs + S3C_GRSTCTL);
372
373 if ((val & (S3C_GRSTCTL_TxFFlsh | S3C_GRSTCTL_RxFFlsh)) == 0)
374 break;
375
376 if (--timeout == 0) {
377 dev_err(hsotg->dev,
378 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
379 __func__, val);
380 }
381
382 udelay(1);
383 }
384
385 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100386}
387
388/**
389 * @ep: USB endpoint to allocate request for.
390 * @flags: Allocation flags
391 *
392 * Allocate a new USB request structure appropriate for the specified endpoint
393 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000394static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
395 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100396{
397 struct s3c_hsotg_req *req;
398
399 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
400 if (!req)
401 return NULL;
402
403 INIT_LIST_HEAD(&req->queue);
404
405 req->req.dma = DMA_ADDR_INVALID;
406 return &req->req;
407}
408
409/**
410 * is_ep_periodic - return true if the endpoint is in periodic mode.
411 * @hs_ep: The endpoint to query.
412 *
413 * Returns true if the endpoint is in periodic mode, meaning it is being
414 * used for an Interrupt or ISO transfer.
415 */
416static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
417{
418 return hs_ep->periodic;
419}
420
421/**
422 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
423 * @hsotg: The device state.
424 * @hs_ep: The endpoint for the request
425 * @hs_req: The request being processed.
426 *
427 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
428 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200429 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100430static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
431 struct s3c_hsotg_ep *hs_ep,
432 struct s3c_hsotg_req *hs_req)
433{
434 struct usb_request *req = &hs_req->req;
435 enum dma_data_direction dir;
436
437 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
438
439 /* ignore this if we're not moving any data */
440 if (hs_req->req.length == 0)
441 return;
442
443 if (hs_req->mapped) {
444 /* we mapped this, so unmap and remove the dma */
445
446 dma_unmap_single(hsotg->dev, req->dma, req->length, dir);
447
448 req->dma = DMA_ADDR_INVALID;
449 hs_req->mapped = 0;
450 } else {
FUJITA Tomonori5b520252010-01-25 11:07:19 +0900451 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100452 }
453}
454
455/**
456 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
457 * @hsotg: The controller state.
458 * @hs_ep: The endpoint we're going to write for.
459 * @hs_req: The request to write data for.
460 *
461 * This is called when the TxFIFO has some space in it to hold a new
462 * transmission and we have something to give it. The actual setup of
463 * the data size is done elsewhere, so all we have to do is to actually
464 * write the data.
465 *
466 * The return value is zero if there is more space (or nothing was done)
467 * otherwise -ENOSPC is returned if the FIFO space was used up.
468 *
469 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200470 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100471static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
472 struct s3c_hsotg_ep *hs_ep,
473 struct s3c_hsotg_req *hs_req)
474{
475 bool periodic = is_ep_periodic(hs_ep);
476 u32 gnptxsts = readl(hsotg->regs + S3C_GNPTXSTS);
477 int buf_pos = hs_req->req.actual;
478 int to_write = hs_ep->size_loaded;
479 void *data;
480 int can_write;
481 int pkt_round;
482
483 to_write -= (buf_pos - hs_ep->last_load);
484
485 /* if there's nothing to write, get out early */
486 if (to_write == 0)
487 return 0;
488
Ben Dooks10aebc72010-07-19 09:40:44 +0100489 if (periodic && !hsotg->dedicated_fifos) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100490 u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
491 int size_left;
492 int size_done;
493
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200494 /*
495 * work out how much data was loaded so we can calculate
496 * how much data is left in the fifo.
497 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100498
499 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
500
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200501 /*
502 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100503 * previous data has been completely sent.
504 */
505 if (hs_ep->fifo_load != 0) {
506 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
507 return -ENOSPC;
508 }
509
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100510 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
511 __func__, size_left,
512 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
513
514 /* how much of the data has moved */
515 size_done = hs_ep->size_loaded - size_left;
516
517 /* how much data is left in the fifo */
518 can_write = hs_ep->fifo_load - size_done;
519 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
520 __func__, can_write);
521
522 can_write = hs_ep->fifo_size - can_write;
523 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
524 __func__, can_write);
525
526 if (can_write <= 0) {
527 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
528 return -ENOSPC;
529 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100530 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
531 can_write = readl(hsotg->regs + S3C_DTXFSTS(hs_ep->index));
532
533 can_write &= 0xffff;
534 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100535 } else {
536 if (S3C_GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
537 dev_dbg(hsotg->dev,
538 "%s: no queue slots available (0x%08x)\n",
539 __func__, gnptxsts);
540
541 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
542 return -ENOSPC;
543 }
544
545 can_write = S3C_GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100546 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100547 }
548
549 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n",
550 __func__, gnptxsts, can_write, to_write, hs_ep->ep.maxpacket);
551
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200552 /*
553 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100554 * FIFO, requests of >512 cause the endpoint to get stuck with a
555 * fragment of the end of the transfer in it.
556 */
557 if (can_write > 512)
558 can_write = 512;
559
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200560 /*
561 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100562 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200563 * doing it.
564 */
Ben Dooks03e10e52010-07-19 09:40:45 +0100565 if (to_write > hs_ep->ep.maxpacket) {
566 to_write = hs_ep->ep.maxpacket;
567
568 s3c_hsotg_en_gsint(hsotg,
569 periodic ? S3C_GINTSTS_PTxFEmp :
570 S3C_GINTSTS_NPTxFEmp);
571 }
572
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100573 /* see if we can write data */
574
575 if (to_write > can_write) {
576 to_write = can_write;
577 pkt_round = to_write % hs_ep->ep.maxpacket;
578
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200579 /*
580 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100581 * exact number of packets.
582 *
583 * Note, we do not currently check to see if we can ever
584 * write a full packet or not to the FIFO.
585 */
586
587 if (pkt_round)
588 to_write -= pkt_round;
589
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200590 /*
591 * enable correct FIFO interrupt to alert us when there
592 * is more room left.
593 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100594
595 s3c_hsotg_en_gsint(hsotg,
596 periodic ? S3C_GINTSTS_PTxFEmp :
597 S3C_GINTSTS_NPTxFEmp);
598 }
599
600 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
601 to_write, hs_req->req.length, can_write, buf_pos);
602
603 if (to_write <= 0)
604 return -ENOSPC;
605
606 hs_req->req.actual = buf_pos + to_write;
607 hs_ep->total_data += to_write;
608
609 if (periodic)
610 hs_ep->fifo_load += to_write;
611
612 to_write = DIV_ROUND_UP(to_write, 4);
613 data = hs_req->req.buf + buf_pos;
614
615 writesl(hsotg->regs + S3C_EPFIFO(hs_ep->index), data, to_write);
616
617 return (to_write >= can_write) ? -ENOSPC : 0;
618}
619
620/**
621 * get_ep_limit - get the maximum data legnth for this endpoint
622 * @hs_ep: The endpoint
623 *
624 * Return the maximum data that can be queued in one go on a given endpoint
625 * so that transfers that are too long can be split.
626 */
627static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
628{
629 int index = hs_ep->index;
630 unsigned maxsize;
631 unsigned maxpkt;
632
633 if (index != 0) {
634 maxsize = S3C_DxEPTSIZ_XferSize_LIMIT + 1;
635 maxpkt = S3C_DxEPTSIZ_PktCnt_LIMIT + 1;
636 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100637 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900638 if (hs_ep->dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100639 maxpkt = S3C_DIEPTSIZ0_PktCnt_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900640 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100641 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100642 }
643
644 /* we made the constant loading easier above by using +1 */
645 maxpkt--;
646 maxsize--;
647
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200648 /*
649 * constrain by packet count if maxpkts*pktsize is greater
650 * than the length register size.
651 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100652
653 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
654 maxsize = maxpkt * hs_ep->ep.maxpacket;
655
656 return maxsize;
657}
658
659/**
660 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
661 * @hsotg: The controller state.
662 * @hs_ep: The endpoint to process a request for
663 * @hs_req: The request to start.
664 * @continuing: True if we are doing more for the current request.
665 *
666 * Start the given request running by setting the endpoint registers
667 * appropriately, and writing any data to the FIFOs.
668 */
669static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
670 struct s3c_hsotg_ep *hs_ep,
671 struct s3c_hsotg_req *hs_req,
672 bool continuing)
673{
674 struct usb_request *ureq = &hs_req->req;
675 int index = hs_ep->index;
676 int dir_in = hs_ep->dir_in;
677 u32 epctrl_reg;
678 u32 epsize_reg;
679 u32 epsize;
680 u32 ctrl;
681 unsigned length;
682 unsigned packets;
683 unsigned maxreq;
684
685 if (index != 0) {
686 if (hs_ep->req && !continuing) {
687 dev_err(hsotg->dev, "%s: active request\n", __func__);
688 WARN_ON(1);
689 return;
690 } else if (hs_ep->req != hs_req && continuing) {
691 dev_err(hsotg->dev,
692 "%s: continue different req\n", __func__);
693 WARN_ON(1);
694 return;
695 }
696 }
697
698 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
699 epsize_reg = dir_in ? S3C_DIEPTSIZ(index) : S3C_DOEPTSIZ(index);
700
701 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
702 __func__, readl(hsotg->regs + epctrl_reg), index,
703 hs_ep->dir_in ? "in" : "out");
704
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900705 /* If endpoint is stalled, we will restart request later */
706 ctrl = readl(hsotg->regs + epctrl_reg);
707
708 if (ctrl & S3C_DxEPCTL_Stall) {
709 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
710 return;
711 }
712
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100713 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200714 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
715 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100716 if (0)
717 dev_dbg(hsotg->dev,
718 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
719 ureq->buf, length, ureq->dma,
720 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
721
722 maxreq = get_ep_limit(hs_ep);
723 if (length > maxreq) {
724 int round = maxreq % hs_ep->ep.maxpacket;
725
726 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
727 __func__, length, maxreq, round);
728
729 /* round down to multiple of packets */
730 if (round)
731 maxreq -= round;
732
733 length = maxreq;
734 }
735
736 if (length)
737 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
738 else
739 packets = 1; /* send one packet if length is zero. */
740
741 if (dir_in && index != 0)
742 epsize = S3C_DxEPTSIZ_MC(1);
743 else
744 epsize = 0;
745
746 if (index != 0 && ureq->zero) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200747 /*
748 * test for the packets being exactly right for the
749 * transfer
750 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100751
752 if (length == (packets * hs_ep->ep.maxpacket))
753 packets++;
754 }
755
756 epsize |= S3C_DxEPTSIZ_PktCnt(packets);
757 epsize |= S3C_DxEPTSIZ_XferSize(length);
758
759 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
760 __func__, packets, length, ureq->length, epsize, epsize_reg);
761
762 /* store the request as the current one we're doing */
763 hs_ep->req = hs_req;
764
765 /* write size / packets */
766 writel(epsize, hsotg->regs + epsize_reg);
767
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900768 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100769 unsigned int dma_reg;
770
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200771 /*
772 * write DMA address to control register, buffer already
773 * synced by s3c_hsotg_ep_queue().
774 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100775
776 dma_reg = dir_in ? S3C_DIEPDMA(index) : S3C_DOEPDMA(index);
777 writel(ureq->dma, hsotg->regs + dma_reg);
778
779 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
780 __func__, ureq->dma, dma_reg);
781 }
782
783 ctrl |= S3C_DxEPCTL_EPEna; /* ensure ep enabled */
784 ctrl |= S3C_DxEPCTL_USBActEp;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200785
786 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
787
788 /* For Setup request do not clear NAK */
789 if (hsotg->setup && index == 0)
790 hsotg->setup = 0;
791 else
792 ctrl |= S3C_DxEPCTL_CNAK; /* clear NAK set by core */
793
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100794
795 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
796 writel(ctrl, hsotg->regs + epctrl_reg);
797
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200798 /*
799 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100800 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200801 * this information.
802 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100803 hs_ep->size_loaded = length;
804 hs_ep->last_load = ureq->actual;
805
806 if (dir_in && !using_dma(hsotg)) {
807 /* set these anyway, we may need them for non-periodic in */
808 hs_ep->fifo_load = 0;
809
810 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
811 }
812
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200813 /*
814 * clear the INTknTXFEmpMsk when we start request, more as a aide
815 * to debugging to see what is going on.
816 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100817 if (dir_in)
818 writel(S3C_DIEPMSK_INTknTXFEmpMsk,
819 hsotg->regs + S3C_DIEPINT(index));
820
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200821 /*
822 * Note, trying to clear the NAK here causes problems with transmit
823 * on the S3C6400 ending up with the TXFIFO becoming full.
824 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100825
826 /* check ep is enabled */
827 if (!(readl(hsotg->regs + epctrl_reg) & S3C_DxEPCTL_EPEna))
828 dev_warn(hsotg->dev,
829 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
830 index, readl(hsotg->regs + epctrl_reg));
831
832 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
833 __func__, readl(hsotg->regs + epctrl_reg));
834}
835
836/**
837 * s3c_hsotg_map_dma - map the DMA memory being used for the request
838 * @hsotg: The device state.
839 * @hs_ep: The endpoint the request is on.
840 * @req: The request being processed.
841 *
842 * We've been asked to queue a request, so ensure that the memory buffer
843 * is correctly setup for DMA. If we've been passed an extant DMA address
844 * then ensure the buffer has been synced to memory. If our buffer has no
845 * DMA memory, then we map the memory and mark our request to allow us to
846 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200847 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100848static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
849 struct s3c_hsotg_ep *hs_ep,
850 struct usb_request *req)
851{
852 enum dma_data_direction dir;
853 struct s3c_hsotg_req *hs_req = our_req(req);
854
855 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
856
857 /* if the length is zero, ignore the DMA data */
858 if (hs_req->req.length == 0)
859 return 0;
860
861 if (req->dma == DMA_ADDR_INVALID) {
862 dma_addr_t dma;
863
864 dma = dma_map_single(hsotg->dev, req->buf, req->length, dir);
865
866 if (unlikely(dma_mapping_error(hsotg->dev, dma)))
867 goto dma_error;
868
869 if (dma & 3) {
870 dev_err(hsotg->dev, "%s: unaligned dma buffer\n",
871 __func__);
872
873 dma_unmap_single(hsotg->dev, dma, req->length, dir);
874 return -EINVAL;
875 }
876
877 hs_req->mapped = 1;
878 req->dma = dma;
879 } else {
FUJITA Tomonori5b520252010-01-25 11:07:19 +0900880 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100881 hs_req->mapped = 0;
882 }
883
884 return 0;
885
886dma_error:
887 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
888 __func__, req->buf, req->length);
889
890 return -EIO;
891}
892
893static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
894 gfp_t gfp_flags)
895{
896 struct s3c_hsotg_req *hs_req = our_req(req);
897 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
898 struct s3c_hsotg *hs = hs_ep->parent;
899 unsigned long irqflags;
900 bool first;
901
902 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
903 ep->name, req, req->length, req->buf, req->no_interrupt,
904 req->zero, req->short_not_ok);
905
906 /* initialise status of the request */
907 INIT_LIST_HEAD(&hs_req->queue);
908 req->actual = 0;
909 req->status = -EINPROGRESS;
910
911 /* if we're using DMA, sync the buffers as necessary */
912 if (using_dma(hs)) {
913 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
914 if (ret)
915 return ret;
916 }
917
918 spin_lock_irqsave(&hs_ep->lock, irqflags);
919
920 first = list_empty(&hs_ep->queue);
921 list_add_tail(&hs_req->queue, &hs_ep->queue);
922
923 if (first)
924 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
925
926 spin_unlock_irqrestore(&hs_ep->lock, irqflags);
927
928 return 0;
929}
930
931static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
932 struct usb_request *req)
933{
934 struct s3c_hsotg_req *hs_req = our_req(req);
935
936 kfree(hs_req);
937}
938
939/**
940 * s3c_hsotg_complete_oursetup - setup completion callback
941 * @ep: The endpoint the request was on.
942 * @req: The request completed.
943 *
944 * Called on completion of any requests the driver itself
945 * submitted that need cleaning up.
946 */
947static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
948 struct usb_request *req)
949{
950 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
951 struct s3c_hsotg *hsotg = hs_ep->parent;
952
953 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
954
955 s3c_hsotg_ep_free_request(ep, req);
956}
957
958/**
959 * ep_from_windex - convert control wIndex value to endpoint
960 * @hsotg: The driver state.
961 * @windex: The control request wIndex field (in host order).
962 *
963 * Convert the given wIndex into a pointer to an driver endpoint
964 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200965 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100966static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
967 u32 windex)
968{
969 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
970 int dir = (windex & USB_DIR_IN) ? 1 : 0;
971 int idx = windex & 0x7F;
972
973 if (windex >= 0x100)
974 return NULL;
975
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200976 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100977 return NULL;
978
979 if (idx && ep->dir_in != dir)
980 return NULL;
981
982 return ep;
983}
984
985/**
986 * s3c_hsotg_send_reply - send reply to control request
987 * @hsotg: The device state
988 * @ep: Endpoint 0
989 * @buff: Buffer for request
990 * @length: Length of reply.
991 *
992 * Create a request and queue it on the given endpoint. This is useful as
993 * an internal method of sending replies to certain control requests, etc.
994 */
995static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
996 struct s3c_hsotg_ep *ep,
997 void *buff,
998 int length)
999{
1000 struct usb_request *req;
1001 int ret;
1002
1003 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1004
1005 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1006 hsotg->ep0_reply = req;
1007 if (!req) {
1008 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1009 return -ENOMEM;
1010 }
1011
1012 req->buf = hsotg->ep0_buff;
1013 req->length = length;
1014 req->zero = 1; /* always do zero-length final transfer */
1015 req->complete = s3c_hsotg_complete_oursetup;
1016
1017 if (length)
1018 memcpy(req->buf, buff, length);
1019 else
1020 ep->sent_zlp = 1;
1021
1022 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1023 if (ret) {
1024 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1025 return ret;
1026 }
1027
1028 return 0;
1029}
1030
1031/**
1032 * s3c_hsotg_process_req_status - process request GET_STATUS
1033 * @hsotg: The device state
1034 * @ctrl: USB control request
1035 */
1036static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1037 struct usb_ctrlrequest *ctrl)
1038{
1039 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1040 struct s3c_hsotg_ep *ep;
1041 __le16 reply;
1042 int ret;
1043
1044 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1045
1046 if (!ep0->dir_in) {
1047 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1048 return -EINVAL;
1049 }
1050
1051 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1052 case USB_RECIP_DEVICE:
1053 reply = cpu_to_le16(0); /* bit 0 => self powered,
1054 * bit 1 => remote wakeup */
1055 break;
1056
1057 case USB_RECIP_INTERFACE:
1058 /* currently, the data result should be zero */
1059 reply = cpu_to_le16(0);
1060 break;
1061
1062 case USB_RECIP_ENDPOINT:
1063 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1064 if (!ep)
1065 return -ENOENT;
1066
1067 reply = cpu_to_le16(ep->halted ? 1 : 0);
1068 break;
1069
1070 default:
1071 return 0;
1072 }
1073
1074 if (le16_to_cpu(ctrl->wLength) != 2)
1075 return -EINVAL;
1076
1077 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1078 if (ret) {
1079 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1080 return ret;
1081 }
1082
1083 return 1;
1084}
1085
1086static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1087
1088/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001089 * get_ep_head - return the first request on the endpoint
1090 * @hs_ep: The controller endpoint to get
1091 *
1092 * Get the first request on the endpoint.
1093 */
1094static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1095{
1096 if (list_empty(&hs_ep->queue))
1097 return NULL;
1098
1099 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1100}
1101
1102/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001103 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1104 * @hsotg: The device state
1105 * @ctrl: USB control request
1106 */
1107static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1108 struct usb_ctrlrequest *ctrl)
1109{
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001110 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001111 struct s3c_hsotg_req *hs_req;
1112 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001113 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1114 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001115 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001116
1117 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1118 __func__, set ? "SET" : "CLEAR");
1119
1120 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1121 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1122 if (!ep) {
1123 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1124 __func__, le16_to_cpu(ctrl->wIndex));
1125 return -ENOENT;
1126 }
1127
1128 switch (le16_to_cpu(ctrl->wValue)) {
1129 case USB_ENDPOINT_HALT:
1130 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001131
1132 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1133 if (ret) {
1134 dev_err(hsotg->dev,
1135 "%s: failed to send reply\n", __func__);
1136 return ret;
1137 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001138
1139 if (!set) {
1140 /*
1141 * If we have request in progress,
1142 * then complete it
1143 */
1144 if (ep->req) {
1145 hs_req = ep->req;
1146 ep->req = NULL;
1147 list_del_init(&hs_req->queue);
1148 hs_req->req.complete(&ep->ep,
1149 &hs_req->req);
1150 }
1151
1152 /* If we have pending request, then start it */
1153 restart = !list_empty(&ep->queue);
1154 if (restart) {
1155 hs_req = get_ep_head(ep);
1156 s3c_hsotg_start_req(hsotg, ep,
1157 hs_req, false);
1158 }
1159 }
1160
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001161 break;
1162
1163 default:
1164 return -ENOENT;
1165 }
1166 } else
1167 return -ENOENT; /* currently only deal with endpoint */
1168
1169 return 1;
1170}
1171
1172/**
1173 * s3c_hsotg_process_control - process a control request
1174 * @hsotg: The device state
1175 * @ctrl: The control request received
1176 *
1177 * The controller has received the SETUP phase of a control request, and
1178 * needs to work out what to do next (and whether to pass it on to the
1179 * gadget driver).
1180 */
1181static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1182 struct usb_ctrlrequest *ctrl)
1183{
1184 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1185 int ret = 0;
1186 u32 dcfg;
1187
1188 ep0->sent_zlp = 0;
1189
1190 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1191 ctrl->bRequest, ctrl->bRequestType,
1192 ctrl->wValue, ctrl->wLength);
1193
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001194 /*
1195 * record the direction of the request, for later use when enquing
1196 * packets onto EP0.
1197 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001198
1199 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1200 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1201
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001202 /*
1203 * if we've no data with this request, then the last part of the
1204 * transaction is going to implicitly be IN.
1205 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001206 if (ctrl->wLength == 0)
1207 ep0->dir_in = 1;
1208
1209 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1210 switch (ctrl->bRequest) {
1211 case USB_REQ_SET_ADDRESS:
1212 dcfg = readl(hsotg->regs + S3C_DCFG);
1213 dcfg &= ~S3C_DCFG_DevAddr_MASK;
1214 dcfg |= ctrl->wValue << S3C_DCFG_DevAddr_SHIFT;
1215 writel(dcfg, hsotg->regs + S3C_DCFG);
1216
1217 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1218
1219 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1220 return;
1221
1222 case USB_REQ_GET_STATUS:
1223 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1224 break;
1225
1226 case USB_REQ_CLEAR_FEATURE:
1227 case USB_REQ_SET_FEATURE:
1228 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1229 break;
1230 }
1231 }
1232
1233 /* as a fallback, try delivering it to the driver to deal with */
1234
1235 if (ret == 0 && hsotg->driver) {
1236 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1237 if (ret < 0)
1238 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1239 }
1240
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001241 /*
1242 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001243 * so respond with a STALL for the status stage to indicate failure.
1244 */
1245
1246 if (ret < 0) {
1247 u32 reg;
1248 u32 ctrl;
1249
1250 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1251 reg = (ep0->dir_in) ? S3C_DIEPCTL0 : S3C_DOEPCTL0;
1252
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001253 /*
1254 * S3C_DxEPCTL_Stall will be cleared by EP once it has
1255 * taken effect, so no need to clear later.
1256 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001257
1258 ctrl = readl(hsotg->regs + reg);
1259 ctrl |= S3C_DxEPCTL_Stall;
1260 ctrl |= S3C_DxEPCTL_CNAK;
1261 writel(ctrl, hsotg->regs + reg);
1262
1263 dev_dbg(hsotg->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001264 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001265 ctrl, reg, readl(hsotg->regs + reg));
1266
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001267 /*
1268 * don't believe we need to anything more to get the EP
1269 * to reply with a STALL packet
1270 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001271 }
1272}
1273
1274static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
1275
1276/**
1277 * s3c_hsotg_complete_setup - completion of a setup transfer
1278 * @ep: The endpoint the request was on.
1279 * @req: The request completed.
1280 *
1281 * Called on completion of any requests the driver itself submitted for
1282 * EP0 setup packets
1283 */
1284static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1285 struct usb_request *req)
1286{
1287 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1288 struct s3c_hsotg *hsotg = hs_ep->parent;
1289
1290 if (req->status < 0) {
1291 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1292 return;
1293 }
1294
1295 if (req->actual == 0)
1296 s3c_hsotg_enqueue_setup(hsotg);
1297 else
1298 s3c_hsotg_process_control(hsotg, req->buf);
1299}
1300
1301/**
1302 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1303 * @hsotg: The device state.
1304 *
1305 * Enqueue a request on EP0 if necessary to received any SETUP packets
1306 * received from the host.
1307 */
1308static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1309{
1310 struct usb_request *req = hsotg->ctrl_req;
1311 struct s3c_hsotg_req *hs_req = our_req(req);
1312 int ret;
1313
1314 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1315
1316 req->zero = 0;
1317 req->length = 8;
1318 req->buf = hsotg->ctrl_buff;
1319 req->complete = s3c_hsotg_complete_setup;
1320
1321 if (!list_empty(&hs_req->queue)) {
1322 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1323 return;
1324 }
1325
1326 hsotg->eps[0].dir_in = 0;
1327
1328 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1329 if (ret < 0) {
1330 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001331 /*
1332 * Don't think there's much we can do other than watch the
1333 * driver fail.
1334 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001335 }
1336}
1337
1338/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001339 * s3c_hsotg_complete_request - complete a request given to us
1340 * @hsotg: The device state.
1341 * @hs_ep: The endpoint the request was on.
1342 * @hs_req: The request to complete.
1343 * @result: The result code (0 => Ok, otherwise errno)
1344 *
1345 * The given request has finished, so call the necessary completion
1346 * if it has one and then look to see if we can start a new request
1347 * on the endpoint.
1348 *
1349 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001350 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001351static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1352 struct s3c_hsotg_ep *hs_ep,
1353 struct s3c_hsotg_req *hs_req,
1354 int result)
1355{
1356 bool restart;
1357
1358 if (!hs_req) {
1359 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1360 return;
1361 }
1362
1363 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1364 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1365
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001366 /*
1367 * only replace the status if we've not already set an error
1368 * from a previous transaction
1369 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001370
1371 if (hs_req->req.status == -EINPROGRESS)
1372 hs_req->req.status = result;
1373
1374 hs_ep->req = NULL;
1375 list_del_init(&hs_req->queue);
1376
1377 if (using_dma(hsotg))
1378 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1379
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001380 /*
1381 * call the complete request with the locks off, just in case the
1382 * request tries to queue more work for this endpoint.
1383 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001384
1385 if (hs_req->req.complete) {
1386 spin_unlock(&hs_ep->lock);
1387 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
1388 spin_lock(&hs_ep->lock);
1389 }
1390
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001391 /*
1392 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001393 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001394 * so be careful when doing this.
1395 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001396
1397 if (!hs_ep->req && result >= 0) {
1398 restart = !list_empty(&hs_ep->queue);
1399 if (restart) {
1400 hs_req = get_ep_head(hs_ep);
1401 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1402 }
1403 }
1404}
1405
1406/**
1407 * s3c_hsotg_complete_request_lock - complete a request given to us (locked)
1408 * @hsotg: The device state.
1409 * @hs_ep: The endpoint the request was on.
1410 * @hs_req: The request to complete.
1411 * @result: The result code (0 => Ok, otherwise errno)
1412 *
1413 * See s3c_hsotg_complete_request(), but called with the endpoint's
1414 * lock held.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001415 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001416static void s3c_hsotg_complete_request_lock(struct s3c_hsotg *hsotg,
1417 struct s3c_hsotg_ep *hs_ep,
1418 struct s3c_hsotg_req *hs_req,
1419 int result)
1420{
1421 unsigned long flags;
1422
1423 spin_lock_irqsave(&hs_ep->lock, flags);
1424 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
1425 spin_unlock_irqrestore(&hs_ep->lock, flags);
1426}
1427
1428/**
1429 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1430 * @hsotg: The device state.
1431 * @ep_idx: The endpoint index for the data
1432 * @size: The size of data in the fifo, in bytes
1433 *
1434 * The FIFO status shows there is data to read from the FIFO for a given
1435 * endpoint, so sort out whether we need to read the data into a request
1436 * that has been made for that endpoint.
1437 */
1438static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1439{
1440 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1441 struct s3c_hsotg_req *hs_req = hs_ep->req;
1442 void __iomem *fifo = hsotg->regs + S3C_EPFIFO(ep_idx);
1443 int to_read;
1444 int max_req;
1445 int read_ptr;
1446
1447 if (!hs_req) {
1448 u32 epctl = readl(hsotg->regs + S3C_DOEPCTL(ep_idx));
1449 int ptr;
1450
1451 dev_warn(hsotg->dev,
1452 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1453 __func__, size, ep_idx, epctl);
1454
1455 /* dump the data from the FIFO, we've nothing we can do */
1456 for (ptr = 0; ptr < size; ptr += 4)
1457 (void)readl(fifo);
1458
1459 return;
1460 }
1461
1462 spin_lock(&hs_ep->lock);
1463
1464 to_read = size;
1465 read_ptr = hs_req->req.actual;
1466 max_req = hs_req->req.length - read_ptr;
1467
Ben Dooksa33e7132010-07-19 09:40:49 +01001468 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1469 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1470
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001471 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001472 /*
1473 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001474 * to deal with in this request.
1475 */
1476
1477 /* currently we don't deal this */
1478 WARN_ON_ONCE(1);
1479 }
1480
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001481 hs_ep->total_data += to_read;
1482 hs_req->req.actual += to_read;
1483 to_read = DIV_ROUND_UP(to_read, 4);
1484
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001485 /*
1486 * note, we might over-write the buffer end by 3 bytes depending on
1487 * alignment of the data.
1488 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001489 readsl(fifo, hs_req->req.buf + read_ptr, to_read);
1490
1491 spin_unlock(&hs_ep->lock);
1492}
1493
1494/**
1495 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1496 * @hsotg: The device instance
1497 * @req: The request currently on this endpoint
1498 *
1499 * Generate a zero-length IN packet request for terminating a SETUP
1500 * transaction.
1501 *
1502 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001503 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001504 * the TxFIFO.
1505 */
1506static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1507 struct s3c_hsotg_req *req)
1508{
1509 u32 ctrl;
1510
1511 if (!req) {
1512 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1513 return;
1514 }
1515
1516 if (req->req.length == 0) {
1517 hsotg->eps[0].sent_zlp = 1;
1518 s3c_hsotg_enqueue_setup(hsotg);
1519 return;
1520 }
1521
1522 hsotg->eps[0].dir_in = 1;
1523 hsotg->eps[0].sent_zlp = 1;
1524
1525 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1526
1527 /* issue a zero-sized packet to terminate this */
1528 writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
1529 S3C_DxEPTSIZ_XferSize(0), hsotg->regs + S3C_DIEPTSIZ(0));
1530
1531 ctrl = readl(hsotg->regs + S3C_DIEPCTL0);
1532 ctrl |= S3C_DxEPCTL_CNAK; /* clear NAK set by core */
1533 ctrl |= S3C_DxEPCTL_EPEna; /* ensure ep enabled */
1534 ctrl |= S3C_DxEPCTL_USBActEp;
1535 writel(ctrl, hsotg->regs + S3C_DIEPCTL0);
1536}
1537
1538/**
1539 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1540 * @hsotg: The device instance
1541 * @epnum: The endpoint received from
1542 * @was_setup: Set if processing a SetupDone event.
1543 *
1544 * The RXFIFO has delivered an OutDone event, which means that the data
1545 * transfer for an OUT endpoint has been completed, either by a short
1546 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001547 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001548static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1549 int epnum, bool was_setup)
1550{
Ben Dooksa33e7132010-07-19 09:40:49 +01001551 u32 epsize = readl(hsotg->regs + S3C_DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001552 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1553 struct s3c_hsotg_req *hs_req = hs_ep->req;
1554 struct usb_request *req = &hs_req->req;
Ben Dooksa33e7132010-07-19 09:40:49 +01001555 unsigned size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001556 int result = 0;
1557
1558 if (!hs_req) {
1559 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1560 return;
1561 }
1562
1563 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001564 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001565
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001566 /*
1567 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001568 * is left in the endpoint size register and then working it
1569 * out from the amount we loaded for the transfer.
1570 *
1571 * We need to do this as DMA pointers are always 32bit aligned
1572 * so may overshoot/undershoot the transfer.
1573 */
1574
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001575 size_done = hs_ep->size_loaded - size_left;
1576 size_done += hs_ep->last_load;
1577
1578 req->actual = size_done;
1579 }
1580
Ben Dooksa33e7132010-07-19 09:40:49 +01001581 /* if there is more request to do, schedule new transfer */
1582 if (req->actual < req->length && size_left == 0) {
1583 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1584 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001585 } else if (epnum == 0) {
1586 /*
1587 * After was_setup = 1 =>
1588 * set CNAK for non Setup requests
1589 */
1590 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001591 }
1592
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001593 if (req->actual < req->length && req->short_not_ok) {
1594 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1595 __func__, req->actual, req->length);
1596
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001597 /*
1598 * todo - what should we return here? there's no one else
1599 * even bothering to check the status.
1600 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001601 }
1602
1603 if (epnum == 0) {
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001604 /*
1605 * Condition req->complete != s3c_hsotg_complete_setup says:
1606 * send ZLP when we have an asynchronous request from gadget
1607 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001608 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1609 s3c_hsotg_send_zlp(hsotg, hs_req);
1610 }
1611
1612 s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, result);
1613}
1614
1615/**
1616 * s3c_hsotg_read_frameno - read current frame number
1617 * @hsotg: The device instance
1618 *
1619 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001620 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001621static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1622{
1623 u32 dsts;
1624
1625 dsts = readl(hsotg->regs + S3C_DSTS);
1626 dsts &= S3C_DSTS_SOFFN_MASK;
1627 dsts >>= S3C_DSTS_SOFFN_SHIFT;
1628
1629 return dsts;
1630}
1631
1632/**
1633 * s3c_hsotg_handle_rx - RX FIFO has data
1634 * @hsotg: The device instance
1635 *
1636 * The IRQ handler has detected that the RX FIFO has some data in it
1637 * that requires processing, so find out what is in there and do the
1638 * appropriate read.
1639 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001640 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001641 * chunks, so if you have x packets received on an endpoint you'll get x
1642 * FIFO events delivered, each with a packet's worth of data in it.
1643 *
1644 * When using DMA, we should not be processing events from the RXFIFO
1645 * as the actual data should be sent to the memory directly and we turn
1646 * on the completion interrupts to get notifications of transfer completion.
1647 */
Mark Brown0978f8c2010-01-18 13:18:35 +00001648static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001649{
1650 u32 grxstsr = readl(hsotg->regs + S3C_GRXSTSP);
1651 u32 epnum, status, size;
1652
1653 WARN_ON(using_dma(hsotg));
1654
1655 epnum = grxstsr & S3C_GRXSTS_EPNum_MASK;
1656 status = grxstsr & S3C_GRXSTS_PktSts_MASK;
1657
1658 size = grxstsr & S3C_GRXSTS_ByteCnt_MASK;
1659 size >>= S3C_GRXSTS_ByteCnt_SHIFT;
1660
1661 if (1)
1662 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1663 __func__, grxstsr, size, epnum);
1664
1665#define __status(x) ((x) >> S3C_GRXSTS_PktSts_SHIFT)
1666
1667 switch (status >> S3C_GRXSTS_PktSts_SHIFT) {
1668 case __status(S3C_GRXSTS_PktSts_GlobalOutNAK):
1669 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1670 break;
1671
1672 case __status(S3C_GRXSTS_PktSts_OutDone):
1673 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1674 s3c_hsotg_read_frameno(hsotg));
1675
1676 if (!using_dma(hsotg))
1677 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1678 break;
1679
1680 case __status(S3C_GRXSTS_PktSts_SetupDone):
1681 dev_dbg(hsotg->dev,
1682 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1683 s3c_hsotg_read_frameno(hsotg),
1684 readl(hsotg->regs + S3C_DOEPCTL(0)));
1685
1686 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1687 break;
1688
1689 case __status(S3C_GRXSTS_PktSts_OutRX):
1690 s3c_hsotg_rx_data(hsotg, epnum, size);
1691 break;
1692
1693 case __status(S3C_GRXSTS_PktSts_SetupRX):
1694 dev_dbg(hsotg->dev,
1695 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1696 s3c_hsotg_read_frameno(hsotg),
1697 readl(hsotg->regs + S3C_DOEPCTL(0)));
1698
1699 s3c_hsotg_rx_data(hsotg, epnum, size);
1700 break;
1701
1702 default:
1703 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1704 __func__, grxstsr);
1705
1706 s3c_hsotg_dump(hsotg);
1707 break;
1708 }
1709}
1710
1711/**
1712 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1713 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001714 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001715static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1716{
1717 switch (mps) {
1718 case 64:
1719 return S3C_D0EPCTL_MPS_64;
1720 case 32:
1721 return S3C_D0EPCTL_MPS_32;
1722 case 16:
1723 return S3C_D0EPCTL_MPS_16;
1724 case 8:
1725 return S3C_D0EPCTL_MPS_8;
1726 }
1727
1728 /* bad max packet size, warn and return invalid result */
1729 WARN_ON(1);
1730 return (u32)-1;
1731}
1732
1733/**
1734 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1735 * @hsotg: The driver state.
1736 * @ep: The index number of the endpoint
1737 * @mps: The maximum packet size in bytes
1738 *
1739 * Configure the maximum packet size for the given endpoint, updating
1740 * the hardware control registers to reflect this.
1741 */
1742static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1743 unsigned int ep, unsigned int mps)
1744{
1745 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1746 void __iomem *regs = hsotg->regs;
1747 u32 mpsval;
1748 u32 reg;
1749
1750 if (ep == 0) {
1751 /* EP0 is a special case */
1752 mpsval = s3c_hsotg_ep0_mps(mps);
1753 if (mpsval > 3)
1754 goto bad_mps;
1755 } else {
1756 if (mps >= S3C_DxEPCTL_MPS_LIMIT+1)
1757 goto bad_mps;
1758
1759 mpsval = mps;
1760 }
1761
1762 hs_ep->ep.maxpacket = mps;
1763
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001764 /*
1765 * update both the in and out endpoint controldir_ registers, even
1766 * if one of the directions may not be in use.
1767 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001768
1769 reg = readl(regs + S3C_DIEPCTL(ep));
1770 reg &= ~S3C_DxEPCTL_MPS_MASK;
1771 reg |= mpsval;
1772 writel(reg, regs + S3C_DIEPCTL(ep));
1773
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001774 if (ep) {
1775 reg = readl(regs + S3C_DOEPCTL(ep));
1776 reg &= ~S3C_DxEPCTL_MPS_MASK;
1777 reg |= mpsval;
1778 writel(reg, regs + S3C_DOEPCTL(ep));
1779 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001780
1781 return;
1782
1783bad_mps:
1784 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1785}
1786
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001787/**
1788 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1789 * @hsotg: The driver state
1790 * @idx: The index for the endpoint (0..15)
1791 */
1792static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1793{
1794 int timeout;
1795 int val;
1796
1797 writel(S3C_GRSTCTL_TxFNum(idx) | S3C_GRSTCTL_TxFFlsh,
1798 hsotg->regs + S3C_GRSTCTL);
1799
1800 /* wait until the fifo is flushed */
1801 timeout = 100;
1802
1803 while (1) {
1804 val = readl(hsotg->regs + S3C_GRSTCTL);
1805
1806 if ((val & (S3C_GRSTCTL_TxFFlsh)) == 0)
1807 break;
1808
1809 if (--timeout == 0) {
1810 dev_err(hsotg->dev,
1811 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1812 __func__, val);
1813 }
1814
1815 udelay(1);
1816 }
1817}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001818
1819/**
1820 * s3c_hsotg_trytx - check to see if anything needs transmitting
1821 * @hsotg: The driver state
1822 * @hs_ep: The driver endpoint to check.
1823 *
1824 * Check to see if there is a request that has data to send, and if so
1825 * make an attempt to write data into the FIFO.
1826 */
1827static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1828 struct s3c_hsotg_ep *hs_ep)
1829{
1830 struct s3c_hsotg_req *hs_req = hs_ep->req;
1831
1832 if (!hs_ep->dir_in || !hs_req)
1833 return 0;
1834
1835 if (hs_req->req.actual < hs_req->req.length) {
1836 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1837 hs_ep->index);
1838 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1839 }
1840
1841 return 0;
1842}
1843
1844/**
1845 * s3c_hsotg_complete_in - complete IN transfer
1846 * @hsotg: The device state.
1847 * @hs_ep: The endpoint that has just completed.
1848 *
1849 * An IN transfer has been completed, update the transfer's state and then
1850 * call the relevant completion routines.
1851 */
1852static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1853 struct s3c_hsotg_ep *hs_ep)
1854{
1855 struct s3c_hsotg_req *hs_req = hs_ep->req;
1856 u32 epsize = readl(hsotg->regs + S3C_DIEPTSIZ(hs_ep->index));
1857 int size_left, size_done;
1858
1859 if (!hs_req) {
1860 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1861 return;
1862 }
1863
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001864 /* Finish ZLP handling for IN EP0 transactions */
1865 if (hsotg->eps[0].sent_zlp) {
1866 dev_dbg(hsotg->dev, "zlp packet received\n");
1867 s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, 0);
1868 return;
1869 }
1870
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001871 /*
1872 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001873 * in the endpoint size register and then working it out from
1874 * the amount we loaded for the transfer.
1875 *
1876 * We do this even for DMA, as the transfer may have incremented
1877 * past the end of the buffer (DMA transfers are always 32bit
1878 * aligned).
1879 */
1880
1881 size_left = S3C_DxEPTSIZ_XferSize_GET(epsize);
1882
1883 size_done = hs_ep->size_loaded - size_left;
1884 size_done += hs_ep->last_load;
1885
1886 if (hs_req->req.actual != size_done)
1887 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1888 __func__, hs_req->req.actual, size_done);
1889
1890 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001891 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1892 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001893
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001894 /*
1895 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1896 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1897 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1898 * inform the host that no more data is available.
1899 * The state of req.zero member is checked to be sure that the value to
1900 * send is smaller than wValue expected from host.
1901 * Check req.length to NOT send another ZLP when the current one is
1902 * under completion (the one for which this completion has been called).
1903 */
1904 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1905 hs_req->req.length == hs_req->req.actual &&
1906 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1907
1908 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1909 s3c_hsotg_send_zlp(hsotg, hs_req);
1910
1911 return;
1912 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001913
1914 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1915 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1916 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1917 } else
1918 s3c_hsotg_complete_request_lock(hsotg, hs_ep, hs_req, 0);
1919}
1920
1921/**
1922 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1923 * @hsotg: The driver state
1924 * @idx: The index for the endpoint (0..15)
1925 * @dir_in: Set if this is an IN endpoint
1926 *
1927 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001928 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001929static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1930 int dir_in)
1931{
1932 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
1933 u32 epint_reg = dir_in ? S3C_DIEPINT(idx) : S3C_DOEPINT(idx);
1934 u32 epctl_reg = dir_in ? S3C_DIEPCTL(idx) : S3C_DOEPCTL(idx);
1935 u32 epsiz_reg = dir_in ? S3C_DIEPTSIZ(idx) : S3C_DOEPTSIZ(idx);
1936 u32 ints;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001937
1938 ints = readl(hsotg->regs + epint_reg);
1939
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001940 /* Clear endpoint interrupts */
1941 writel(ints, hsotg->regs + epint_reg);
1942
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001943 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1944 __func__, idx, dir_in ? "in" : "out", ints);
1945
1946 if (ints & S3C_DxEPINT_XferCompl) {
1947 dev_dbg(hsotg->dev,
1948 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1949 __func__, readl(hsotg->regs + epctl_reg),
1950 readl(hsotg->regs + epsiz_reg));
1951
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001952 /*
1953 * we get OutDone from the FIFO, so we only need to look
1954 * at completing IN requests here
1955 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001956 if (dir_in) {
1957 s3c_hsotg_complete_in(hsotg, hs_ep);
1958
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001959 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001960 s3c_hsotg_enqueue_setup(hsotg);
1961 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001962 /*
1963 * We're using DMA, we need to fire an OutDone here
1964 * as we ignore the RXFIFO.
1965 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001966
1967 s3c_hsotg_handle_outdone(hsotg, idx, false);
1968 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001969 }
1970
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001971 if (ints & S3C_DxEPINT_EPDisbld) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001972 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001973
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001974 if (dir_in) {
1975 int epctl = readl(hsotg->regs + epctl_reg);
1976
1977 s3c_hsotg_txfifo_flush(hsotg, idx);
1978
1979 if ((epctl & S3C_DxEPCTL_Stall) &&
1980 (epctl & S3C_DxEPCTL_EPType_Bulk)) {
1981 int dctl = readl(hsotg->regs + S3C_DCTL);
1982
1983 dctl |= S3C_DCTL_CGNPInNAK;
1984 writel(dctl, hsotg->regs + S3C_DCTL);
1985 }
1986 }
1987 }
1988
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001989 if (ints & S3C_DxEPINT_AHBErr)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001990 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001991
1992 if (ints & S3C_DxEPINT_Setup) { /* Setup or Timeout */
1993 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1994
1995 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001996 /*
1997 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001998 * setup packet. In non-DMA mode we'd get this
1999 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002000 * the setup here.
2001 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002002
2003 if (dir_in)
2004 WARN_ON_ONCE(1);
2005 else
2006 s3c_hsotg_handle_outdone(hsotg, 0, true);
2007 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002008 }
2009
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002010 if (ints & S3C_DxEPINT_Back2BackSetup)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002011 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002012
2013 if (dir_in) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002014 /* not sure if this is important, but we'll clear it anyway */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002015 if (ints & S3C_DIEPMSK_INTknTXFEmpMsk) {
2016 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2017 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002018 }
2019
2020 /* this probably means something bad is happening */
2021 if (ints & S3C_DIEPMSK_INTknEPMisMsk) {
2022 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2023 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002024 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002025
2026 /* FIFO has space or is empty (see GAHBCFG) */
2027 if (hsotg->dedicated_fifos &&
2028 ints & S3C_DIEPMSK_TxFIFOEmpty) {
2029 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2030 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002031 if (!using_dma(hsotg))
2032 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002033 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002034 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002035}
2036
2037/**
2038 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
2039 * @hsotg: The device state.
2040 *
2041 * Handle updating the device settings after the enumeration phase has
2042 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002043 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002044static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
2045{
2046 u32 dsts = readl(hsotg->regs + S3C_DSTS);
2047 int ep0_mps = 0, ep_mps;
2048
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002049 /*
2050 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002051 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002052 * we connected at.
2053 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002054
2055 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2056
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002057 /*
2058 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002059 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002060 * not advertise a 64byte MPS on EP0.
2061 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002062
2063 /* catch both EnumSpd_FS and EnumSpd_FS48 */
2064 switch (dsts & S3C_DSTS_EnumSpd_MASK) {
2065 case S3C_DSTS_EnumSpd_FS:
2066 case S3C_DSTS_EnumSpd_FS48:
2067 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002068 ep0_mps = EP0_MPS_LIMIT;
2069 ep_mps = 64;
2070 break;
2071
2072 case S3C_DSTS_EnumSpd_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002073 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002074 ep0_mps = EP0_MPS_LIMIT;
2075 ep_mps = 512;
2076 break;
2077
2078 case S3C_DSTS_EnumSpd_LS:
2079 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002080 /*
2081 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002082 * moment, and the documentation seems to imply that it isn't
2083 * supported by the PHYs on some of the devices.
2084 */
2085 break;
2086 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002087 dev_info(hsotg->dev, "new device is %s\n",
2088 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002089
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002090 /*
2091 * we should now know the maximum packet size for an
2092 * endpoint, so set the endpoints to a default value.
2093 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002094
2095 if (ep0_mps) {
2096 int i;
2097 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002098 for (i = 1; i < hsotg->num_of_eps; i++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002099 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
2100 }
2101
2102 /* ensure after enumeration our EP0 is active */
2103
2104 s3c_hsotg_enqueue_setup(hsotg);
2105
2106 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2107 readl(hsotg->regs + S3C_DIEPCTL0),
2108 readl(hsotg->regs + S3C_DOEPCTL0));
2109}
2110
2111/**
2112 * kill_all_requests - remove all requests from the endpoint's queue
2113 * @hsotg: The device state.
2114 * @ep: The endpoint the requests may be on.
2115 * @result: The result code to use.
2116 * @force: Force removal of any current requests
2117 *
2118 * Go through the requests on the given endpoint and mark them
2119 * completed with the given result code.
2120 */
2121static void kill_all_requests(struct s3c_hsotg *hsotg,
2122 struct s3c_hsotg_ep *ep,
2123 int result, bool force)
2124{
2125 struct s3c_hsotg_req *req, *treq;
2126 unsigned long flags;
2127
2128 spin_lock_irqsave(&ep->lock, flags);
2129
2130 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002131 /*
2132 * currently, we can't do much about an already
2133 * running request on an in endpoint
2134 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002135
2136 if (ep->req == req && ep->dir_in && !force)
2137 continue;
2138
2139 s3c_hsotg_complete_request(hsotg, ep, req,
2140 result);
2141 }
2142
2143 spin_unlock_irqrestore(&ep->lock, flags);
2144}
2145
2146#define call_gadget(_hs, _entry) \
2147 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
2148 (_hs)->driver && (_hs)->driver->_entry) \
2149 (_hs)->driver->_entry(&(_hs)->gadget);
2150
2151/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002152 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002153 * @hsotg: The device state.
2154 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002155 * The device has been disconnected. Remove all current
2156 * transactions and signal the gadget driver that this
2157 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002158 */
Lukasz Majewski5e891342012-05-04 14:17:07 +02002159static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002160{
2161 unsigned ep;
2162
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002163 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002164 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2165
2166 call_gadget(hsotg, disconnect);
2167}
2168
2169/**
2170 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2171 * @hsotg: The device state:
2172 * @periodic: True if this is a periodic FIFO interrupt
2173 */
2174static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2175{
2176 struct s3c_hsotg_ep *ep;
2177 int epno, ret;
2178
2179 /* look through for any more data to transmit */
2180
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002181 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002182 ep = &hsotg->eps[epno];
2183
2184 if (!ep->dir_in)
2185 continue;
2186
2187 if ((periodic && !ep->periodic) ||
2188 (!periodic && ep->periodic))
2189 continue;
2190
2191 ret = s3c_hsotg_trytx(hsotg, ep);
2192 if (ret < 0)
2193 break;
2194 }
2195}
2196
2197static struct s3c_hsotg *our_hsotg;
2198
2199/* IRQ flags which will trigger a retry around the IRQ loop */
2200#define IRQ_RETRY_MASK (S3C_GINTSTS_NPTxFEmp | \
2201 S3C_GINTSTS_PTxFEmp | \
2202 S3C_GINTSTS_RxFLvl)
2203
2204/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002205 * s3c_hsotg_corereset - issue softreset to the core
2206 * @hsotg: The device state
2207 *
2208 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002209 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002210static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2211{
2212 int timeout;
2213 u32 grstctl;
2214
2215 dev_dbg(hsotg->dev, "resetting core\n");
2216
2217 /* issue soft reset */
2218 writel(S3C_GRSTCTL_CSftRst, hsotg->regs + S3C_GRSTCTL);
2219
2220 timeout = 1000;
2221 do {
2222 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2223 } while ((grstctl & S3C_GRSTCTL_CSftRst) && timeout-- > 0);
2224
2225 if (grstctl & S3C_GRSTCTL_CSftRst) {
2226 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2227 return -EINVAL;
2228 }
2229
2230 timeout = 1000;
2231
2232 while (1) {
2233 u32 grstctl = readl(hsotg->regs + S3C_GRSTCTL);
2234
2235 if (timeout-- < 0) {
2236 dev_info(hsotg->dev,
2237 "%s: reset failed, GRSTCTL=%08x\n",
2238 __func__, grstctl);
2239 return -ETIMEDOUT;
2240 }
2241
2242 if (!(grstctl & S3C_GRSTCTL_AHBIdle))
2243 continue;
2244
2245 break; /* reset done */
2246 }
2247
2248 dev_dbg(hsotg->dev, "reset successful\n");
2249 return 0;
2250}
2251
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002252/**
2253 * s3c_hsotg_core_init - issue softreset to the core
2254 * @hsotg: The device state
2255 *
2256 * Issue a soft reset to the core, and await the core finishing it.
2257 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002258static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2259{
2260 s3c_hsotg_corereset(hsotg);
2261
2262 /*
2263 * we must now enable ep0 ready for host detection and then
2264 * set configuration.
2265 */
2266
2267 /* set the PLL on, remove the HNP/SRP and set the PHY */
2268 writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) |
2269 (0x5 << 10), hsotg->regs + S3C_GUSBCFG);
2270
2271 s3c_hsotg_init_fifo(hsotg);
2272
2273 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2274
2275 writel(1 << 18 | S3C_DCFG_DevSpd_HS, hsotg->regs + S3C_DCFG);
2276
2277 /* Clear any pending OTG interrupts */
2278 writel(0xffffffff, hsotg->regs + S3C_GOTGINT);
2279
2280 /* Clear any pending interrupts */
2281 writel(0xffffffff, hsotg->regs + S3C_GINTSTS);
2282
Lukasz Majewskib3546c92012-05-04 14:17:06 +02002283 writel(S3C_GINTSTS_ErlySusp | S3C_GINTSTS_SessReqInt |
Lukasz Majewski308d7342012-05-04 14:17:05 +02002284 S3C_GINTSTS_GOUTNakEff | S3C_GINTSTS_GINNakEff |
2285 S3C_GINTSTS_ConIDStsChng | S3C_GINTSTS_USBRst |
2286 S3C_GINTSTS_EnumDone | S3C_GINTSTS_OTGInt |
Lukasz Majewskib3546c92012-05-04 14:17:06 +02002287 S3C_GINTSTS_USBSusp | S3C_GINTSTS_WkUpInt,
Lukasz Majewski308d7342012-05-04 14:17:05 +02002288 hsotg->regs + S3C_GINTMSK);
2289
2290 if (using_dma(hsotg))
2291 writel(S3C_GAHBCFG_GlblIntrEn | S3C_GAHBCFG_DMAEn |
2292 S3C_GAHBCFG_HBstLen_Incr4,
2293 hsotg->regs + S3C_GAHBCFG);
2294 else
2295 writel(S3C_GAHBCFG_GlblIntrEn, hsotg->regs + S3C_GAHBCFG);
2296
2297 /*
2298 * Enabling INTknTXFEmpMsk here seems to be a big mistake, we end
2299 * up being flooded with interrupts if the host is polling the
2300 * endpoint to try and read data.
2301 */
2302
2303 writel(((hsotg->dedicated_fifos) ? S3C_DIEPMSK_TxFIFOEmpty : 0) |
2304 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk |
2305 S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2306 S3C_DIEPMSK_INTknEPMisMsk,
2307 hsotg->regs + S3C_DIEPMSK);
2308
2309 /*
2310 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2311 * DMA mode we may need this.
2312 */
2313 writel((using_dma(hsotg) ? (S3C_DIEPMSK_XferComplMsk |
2314 S3C_DIEPMSK_TimeOUTMsk) : 0) |
2315 S3C_DOEPMSK_EPDisbldMsk | S3C_DOEPMSK_AHBErrMsk |
2316 S3C_DOEPMSK_SetupMsk,
2317 hsotg->regs + S3C_DOEPMSK);
2318
2319 writel(0, hsotg->regs + S3C_DAINTMSK);
2320
2321 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2322 readl(hsotg->regs + S3C_DIEPCTL0),
2323 readl(hsotg->regs + S3C_DOEPCTL0));
2324
2325 /* enable in and out endpoint interrupts */
2326 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt);
2327
2328 /*
2329 * Enable the RXFIFO when in slave mode, as this is how we collect
2330 * the data. In DMA mode, we get events from the FIFO but also
2331 * things we cannot process, so do not use it.
2332 */
2333 if (!using_dma(hsotg))
2334 s3c_hsotg_en_gsint(hsotg, S3C_GINTSTS_RxFLvl);
2335
2336 /* Enable interrupts for EP0 in and out */
2337 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2338 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2339
2340 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2341 udelay(10); /* see openiboot */
2342 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_PWROnPrgDone);
2343
2344 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + S3C_DCTL));
2345
2346 /*
2347 * S3C_DxEPCTL_USBActEp says RO in manual, but seems to be set by
2348 * writing to the EPCTL register..
2349 */
2350
2351 /* set to read 1 8byte packet */
2352 writel(S3C_DxEPTSIZ_MC(1) | S3C_DxEPTSIZ_PktCnt(1) |
2353 S3C_DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
2354
2355 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2356 S3C_DxEPCTL_CNAK | S3C_DxEPCTL_EPEna |
2357 S3C_DxEPCTL_USBActEp,
2358 hsotg->regs + S3C_DOEPCTL0);
2359
2360 /* enable, but don't activate EP0in */
2361 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
2362 S3C_DxEPCTL_USBActEp, hsotg->regs + S3C_DIEPCTL0);
2363
2364 s3c_hsotg_enqueue_setup(hsotg);
2365
2366 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
2367 readl(hsotg->regs + S3C_DIEPCTL0),
2368 readl(hsotg->regs + S3C_DOEPCTL0));
2369
2370 /* clear global NAKs */
2371 writel(S3C_DCTL_CGOUTNak | S3C_DCTL_CGNPInNAK,
2372 hsotg->regs + S3C_DCTL);
2373
2374 /* must be at-least 3ms to allow bus to see disconnect */
2375 mdelay(3);
2376
2377 /* remove the soft-disconnect and let's go */
2378 __bic32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2379}
2380
2381/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002382 * s3c_hsotg_irq - handle device interrupt
2383 * @irq: The IRQ number triggered
2384 * @pw: The pw value when registered the handler.
2385 */
2386static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2387{
2388 struct s3c_hsotg *hsotg = pw;
2389 int retry_count = 8;
2390 u32 gintsts;
2391 u32 gintmsk;
2392
2393irq_retry:
2394 gintsts = readl(hsotg->regs + S3C_GINTSTS);
2395 gintmsk = readl(hsotg->regs + S3C_GINTMSK);
2396
2397 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2398 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2399
2400 gintsts &= gintmsk;
2401
2402 if (gintsts & S3C_GINTSTS_OTGInt) {
2403 u32 otgint = readl(hsotg->regs + S3C_GOTGINT);
2404
2405 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2406
2407 writel(otgint, hsotg->regs + S3C_GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002408 }
2409
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002410 if (gintsts & S3C_GINTSTS_SessReqInt) {
2411 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
2412 writel(S3C_GINTSTS_SessReqInt, hsotg->regs + S3C_GINTSTS);
2413 }
2414
2415 if (gintsts & S3C_GINTSTS_EnumDone) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002416 writel(S3C_GINTSTS_EnumDone, hsotg->regs + S3C_GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002417
2418 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002419 }
2420
2421 if (gintsts & S3C_GINTSTS_ConIDStsChng) {
2422 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
2423 readl(hsotg->regs + S3C_DSTS),
2424 readl(hsotg->regs + S3C_GOTGCTL));
2425
2426 writel(S3C_GINTSTS_ConIDStsChng, hsotg->regs + S3C_GINTSTS);
2427 }
2428
2429 if (gintsts & (S3C_GINTSTS_OEPInt | S3C_GINTSTS_IEPInt)) {
2430 u32 daint = readl(hsotg->regs + S3C_DAINT);
2431 u32 daint_out = daint >> S3C_DAINT_OutEP_SHIFT;
2432 u32 daint_in = daint & ~(daint_out << S3C_DAINT_OutEP_SHIFT);
2433 int ep;
2434
2435 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2436
2437 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2438 if (daint_out & 1)
2439 s3c_hsotg_epint(hsotg, ep, 0);
2440 }
2441
2442 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2443 if (daint_in & 1)
2444 s3c_hsotg_epint(hsotg, ep, 1);
2445 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002446 }
2447
2448 if (gintsts & S3C_GINTSTS_USBRst) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002449
2450 u32 usb_status = readl(hsotg->regs + S3C_GOTGCTL);
2451
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002452 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2453 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2454 readl(hsotg->regs + S3C_GNPTXSTS));
2455
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002456 writel(S3C_GINTSTS_USBRst, hsotg->regs + S3C_GINTSTS);
2457
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002458 if (usb_status & S3C_GOTGCTL_BSESVLD) {
2459 if (time_after(jiffies, hsotg->last_rst +
2460 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002461
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002462 kill_all_requests(hsotg, &hsotg->eps[0],
2463 -ECONNRESET, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002464
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002465 s3c_hsotg_core_init(hsotg);
2466 hsotg->last_rst = jiffies;
2467 }
2468 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002469 }
2470
2471 /* check both FIFOs */
2472
2473 if (gintsts & S3C_GINTSTS_NPTxFEmp) {
2474 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2475
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002476 /*
2477 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002478 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002479 * it needs re-enabling
2480 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002481
2482 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_NPTxFEmp);
2483 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002484 }
2485
2486 if (gintsts & S3C_GINTSTS_PTxFEmp) {
2487 dev_dbg(hsotg->dev, "PTxFEmp\n");
2488
2489 /* See note in S3C_GINTSTS_NPTxFEmp */
2490
2491 s3c_hsotg_disable_gsint(hsotg, S3C_GINTSTS_PTxFEmp);
2492 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002493 }
2494
2495 if (gintsts & S3C_GINTSTS_RxFLvl) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002496 /*
2497 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002499 * set.
2500 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002501
2502 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002503 }
2504
2505 if (gintsts & S3C_GINTSTS_ModeMis) {
2506 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
2507 writel(S3C_GINTSTS_ModeMis, hsotg->regs + S3C_GINTSTS);
2508 }
2509
2510 if (gintsts & S3C_GINTSTS_USBSusp) {
2511 dev_info(hsotg->dev, "S3C_GINTSTS_USBSusp\n");
2512 writel(S3C_GINTSTS_USBSusp, hsotg->regs + S3C_GINTSTS);
2513
2514 call_gadget(hsotg, suspend);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002515 s3c_hsotg_disconnect(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 }
2517
2518 if (gintsts & S3C_GINTSTS_WkUpInt) {
2519 dev_info(hsotg->dev, "S3C_GINTSTS_WkUpIn\n");
2520 writel(S3C_GINTSTS_WkUpInt, hsotg->regs + S3C_GINTSTS);
2521
2522 call_gadget(hsotg, resume);
2523 }
2524
2525 if (gintsts & S3C_GINTSTS_ErlySusp) {
2526 dev_dbg(hsotg->dev, "S3C_GINTSTS_ErlySusp\n");
2527 writel(S3C_GINTSTS_ErlySusp, hsotg->regs + S3C_GINTSTS);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002528
2529 s3c_hsotg_disconnect(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002530 }
2531
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002532 /*
2533 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002534 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002535 * the occurrence.
2536 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002537
2538 if (gintsts & S3C_GINTSTS_GOUTNakEff) {
2539 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2540
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002541 writel(S3C_DCTL_CGOUTNak, hsotg->regs + S3C_DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002542
2543 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002544 }
2545
2546 if (gintsts & S3C_GINTSTS_GINNakEff) {
2547 dev_info(hsotg->dev, "GINNakEff triggered\n");
2548
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002549 writel(S3C_DCTL_CGNPInNAK, hsotg->regs + S3C_DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002550
2551 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002552 }
2553
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002554 /*
2555 * if we've had fifo events, we should try and go around the
2556 * loop again to see if there's any point in returning yet.
2557 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002558
2559 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2560 goto irq_retry;
2561
2562 return IRQ_HANDLED;
2563}
2564
2565/**
2566 * s3c_hsotg_ep_enable - enable the given endpoint
2567 * @ep: The USB endpint to configure
2568 * @desc: The USB endpoint descriptor to configure with.
2569 *
2570 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002571 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002572static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2573 const struct usb_endpoint_descriptor *desc)
2574{
2575 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2576 struct s3c_hsotg *hsotg = hs_ep->parent;
2577 unsigned long flags;
2578 int index = hs_ep->index;
2579 u32 epctrl_reg;
2580 u32 epctrl;
2581 u32 mps;
2582 int dir_in;
Julia Lawall19c190f2010-03-29 17:36:44 +02002583 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002584
2585 dev_dbg(hsotg->dev,
2586 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2587 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2588 desc->wMaxPacketSize, desc->bInterval);
2589
2590 /* not to be called for EP0 */
2591 WARN_ON(index == 0);
2592
2593 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2594 if (dir_in != hs_ep->dir_in) {
2595 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2596 return -EINVAL;
2597 }
2598
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002599 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002600
2601 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2602
2603 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2604 epctrl = readl(hsotg->regs + epctrl_reg);
2605
2606 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2607 __func__, epctrl, epctrl_reg);
2608
2609 spin_lock_irqsave(&hs_ep->lock, flags);
2610
2611 epctrl &= ~(S3C_DxEPCTL_EPType_MASK | S3C_DxEPCTL_MPS_MASK);
2612 epctrl |= S3C_DxEPCTL_MPS(mps);
2613
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002614 /*
2615 * mark the endpoint as active, otherwise the core may ignore
2616 * transactions entirely for this endpoint
2617 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002618 epctrl |= S3C_DxEPCTL_USBActEp;
2619
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002620 /*
2621 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002622 * do something with data that we've yet got a request to process
2623 * since the RXFIFO will take data for an endpoint even if the
2624 * size register hasn't been set.
2625 */
2626
2627 epctrl |= S3C_DxEPCTL_SNAK;
2628
2629 /* update the endpoint state */
2630 hs_ep->ep.maxpacket = mps;
2631
2632 /* default, set to non-periodic */
2633 hs_ep->periodic = 0;
2634
2635 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2636 case USB_ENDPOINT_XFER_ISOC:
2637 dev_err(hsotg->dev, "no current ISOC support\n");
Julia Lawall19c190f2010-03-29 17:36:44 +02002638 ret = -EINVAL;
2639 goto out;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002640
2641 case USB_ENDPOINT_XFER_BULK:
2642 epctrl |= S3C_DxEPCTL_EPType_Bulk;
2643 break;
2644
2645 case USB_ENDPOINT_XFER_INT:
2646 if (dir_in) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002647 /*
2648 * Allocate our TxFNum by simply using the index
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002649 * of the endpoint for the moment. We could do
2650 * something better if the host indicates how
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002651 * many FIFOs we are expecting to use.
2652 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002653
2654 hs_ep->periodic = 1;
2655 epctrl |= S3C_DxEPCTL_TxFNum(index);
2656 }
2657
2658 epctrl |= S3C_DxEPCTL_EPType_Intterupt;
2659 break;
2660
2661 case USB_ENDPOINT_XFER_CONTROL:
2662 epctrl |= S3C_DxEPCTL_EPType_Control;
2663 break;
2664 }
2665
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002666 /*
2667 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002668 * a unique tx-fifo even if it is non-periodic.
2669 */
2670 if (dir_in && hsotg->dedicated_fifos)
2671 epctrl |= S3C_DxEPCTL_TxFNum(index);
2672
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002673 /* for non control endpoints, set PID to D0 */
2674 if (index)
2675 epctrl |= S3C_DxEPCTL_SetD0PID;
2676
2677 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2678 __func__, epctrl);
2679
2680 writel(epctrl, hsotg->regs + epctrl_reg);
2681 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2682 __func__, readl(hsotg->regs + epctrl_reg));
2683
2684 /* enable the endpoint interrupt */
2685 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2686
Julia Lawall19c190f2010-03-29 17:36:44 +02002687out:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002688 spin_unlock_irqrestore(&hs_ep->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002689 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002690}
2691
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002692/**
2693 * s3c_hsotg_ep_disable - disable given endpoint
2694 * @ep: The endpoint to disable.
2695 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002696static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2697{
2698 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2699 struct s3c_hsotg *hsotg = hs_ep->parent;
2700 int dir_in = hs_ep->dir_in;
2701 int index = hs_ep->index;
2702 unsigned long flags;
2703 u32 epctrl_reg;
2704 u32 ctrl;
2705
2706 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2707
2708 if (ep == &hsotg->eps[0].ep) {
2709 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2710 return -EINVAL;
2711 }
2712
2713 epctrl_reg = dir_in ? S3C_DIEPCTL(index) : S3C_DOEPCTL(index);
2714
2715 /* terminate all requests with shutdown */
2716 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2717
2718 spin_lock_irqsave(&hs_ep->lock, flags);
2719
2720 ctrl = readl(hsotg->regs + epctrl_reg);
2721 ctrl &= ~S3C_DxEPCTL_EPEna;
2722 ctrl &= ~S3C_DxEPCTL_USBActEp;
2723 ctrl |= S3C_DxEPCTL_SNAK;
2724
2725 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2726 writel(ctrl, hsotg->regs + epctrl_reg);
2727
2728 /* disable endpoint interrupts */
2729 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2730
2731 spin_unlock_irqrestore(&hs_ep->lock, flags);
2732 return 0;
2733}
2734
2735/**
2736 * on_list - check request is on the given endpoint
2737 * @ep: The endpoint to check.
2738 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002739 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002740static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2741{
2742 struct s3c_hsotg_req *req, *treq;
2743
2744 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2745 if (req == test)
2746 return true;
2747 }
2748
2749 return false;
2750}
2751
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002752/**
2753 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2754 * @ep: The endpoint to dequeue.
2755 * @req: The request to be removed from a queue.
2756 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002757static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2758{
2759 struct s3c_hsotg_req *hs_req = our_req(req);
2760 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2761 struct s3c_hsotg *hs = hs_ep->parent;
2762 unsigned long flags;
2763
2764 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2765
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002766 spin_lock_irqsave(&hs_ep->lock, flags);
2767
2768 if (!on_list(hs_ep, hs_req)) {
2769 spin_unlock_irqrestore(&hs_ep->lock, flags);
2770 return -EINVAL;
2771 }
2772
2773 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
2774 spin_unlock_irqrestore(&hs_ep->lock, flags);
2775
2776 return 0;
2777}
2778
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002779/**
2780 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2781 * @ep: The endpoint to set halt.
2782 * @value: Set or unset the halt.
2783 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002784static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2785{
2786 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2787 struct s3c_hsotg *hs = hs_ep->parent;
2788 int index = hs_ep->index;
2789 unsigned long irqflags;
2790 u32 epreg;
2791 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002792 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002793
2794 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2795
2796 spin_lock_irqsave(&hs_ep->lock, irqflags);
2797
2798 /* write both IN and OUT control registers */
2799
2800 epreg = S3C_DIEPCTL(index);
2801 epctl = readl(hs->regs + epreg);
2802
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002803 if (value) {
2804 epctl |= S3C_DxEPCTL_Stall + S3C_DxEPCTL_SNAK;
2805 if (epctl & S3C_DxEPCTL_EPEna)
2806 epctl |= S3C_DxEPCTL_EPDis;
2807 } else {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002808 epctl &= ~S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002809 xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2810 if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2811 xfertype == S3C_DxEPCTL_EPType_Intterupt)
2812 epctl |= S3C_DxEPCTL_SetD0PID;
2813 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002814
2815 writel(epctl, hs->regs + epreg);
2816
2817 epreg = S3C_DOEPCTL(index);
2818 epctl = readl(hs->regs + epreg);
2819
2820 if (value)
2821 epctl |= S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002822 else {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002823 epctl &= ~S3C_DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002824 xfertype = epctl & S3C_DxEPCTL_EPType_MASK;
2825 if (xfertype == S3C_DxEPCTL_EPType_Bulk ||
2826 xfertype == S3C_DxEPCTL_EPType_Intterupt)
2827 epctl |= S3C_DxEPCTL_SetD0PID;
2828 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002829
2830 writel(epctl, hs->regs + epreg);
2831
2832 spin_unlock_irqrestore(&hs_ep->lock, irqflags);
2833
2834 return 0;
2835}
2836
2837static struct usb_ep_ops s3c_hsotg_ep_ops = {
2838 .enable = s3c_hsotg_ep_enable,
2839 .disable = s3c_hsotg_ep_disable,
2840 .alloc_request = s3c_hsotg_ep_alloc_request,
2841 .free_request = s3c_hsotg_ep_free_request,
2842 .queue = s3c_hsotg_ep_queue,
2843 .dequeue = s3c_hsotg_ep_dequeue,
2844 .set_halt = s3c_hsotg_ep_sethalt,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002845 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002846};
2847
2848/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002849 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002850 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002851 *
2852 * A wrapper for platform code responsible for controlling
2853 * low-level USB code
2854 */
2855static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2856{
2857 struct platform_device *pdev = to_platform_device(hsotg->dev);
2858
2859 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
2860 if (hsotg->plat->phy_init)
2861 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2862}
2863
2864/**
2865 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002866 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002867 *
2868 * A wrapper for platform code responsible for controlling
2869 * low-level USB code
2870 */
2871static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2872{
2873 struct platform_device *pdev = to_platform_device(hsotg->dev);
2874
2875 if (hsotg->plat->phy_exit)
2876 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2877}
2878
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002879/**
2880 * s3c_hsotg_init - initalize the usb core
2881 * @hsotg: The driver state
2882 */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002883static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2884{
2885 /* unmask subset of endpoint interrupts */
2886
2887 writel(S3C_DIEPMSK_TimeOUTMsk | S3C_DIEPMSK_AHBErrMsk |
2888 S3C_DIEPMSK_EPDisbldMsk | S3C_DIEPMSK_XferComplMsk,
2889 hsotg->regs + S3C_DIEPMSK);
2890
2891 writel(S3C_DOEPMSK_SetupMsk | S3C_DOEPMSK_AHBErrMsk |
2892 S3C_DOEPMSK_EPDisbldMsk | S3C_DOEPMSK_XferComplMsk,
2893 hsotg->regs + S3C_DOEPMSK);
2894
2895 writel(0, hsotg->regs + S3C_DAINTMSK);
2896
2897 /* Be in disconnected state until gadget is registered */
2898 __orr32(hsotg->regs + S3C_DCTL, S3C_DCTL_SftDiscon);
2899
2900 if (0) {
2901 /* post global nak until we're ready */
2902 writel(S3C_DCTL_SGNPInNAK | S3C_DCTL_SGOUTNak,
2903 hsotg->regs + S3C_DCTL);
2904 }
2905
2906 /* setup fifos */
2907
2908 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
2909 readl(hsotg->regs + S3C_GRXFSIZ),
2910 readl(hsotg->regs + S3C_GNPTXFSIZ));
2911
2912 s3c_hsotg_init_fifo(hsotg);
2913
2914 /* set the PLL on, remove the HNP/SRP and set the PHY */
2915 writel(S3C_GUSBCFG_PHYIf16 | S3C_GUSBCFG_TOutCal(7) | (0x5 << 10),
2916 hsotg->regs + S3C_GUSBCFG);
2917
2918 writel(using_dma(hsotg) ? S3C_GAHBCFG_DMAEn : 0x0,
2919 hsotg->regs + S3C_GAHBCFG);
2920}
2921
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002922/**
2923 * s3c_hsotg_udc_start - prepare the udc for work
2924 * @gadget: The usb gadget state
2925 * @driver: The usb gadget driver
2926 *
2927 * Perform initialization to prepare udc device and driver
2928 * to work.
2929 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002930static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2931 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002932{
2933 struct s3c_hsotg *hsotg = our_hsotg;
2934 int ret;
2935
2936 if (!hsotg) {
2937 printk(KERN_ERR "%s: called with no device\n", __func__);
2938 return -ENODEV;
2939 }
2940
2941 if (!driver) {
2942 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2943 return -EINVAL;
2944 }
2945
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002946 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002947 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002948
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002949 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002950 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2951 return -EINVAL;
2952 }
2953
2954 WARN_ON(hsotg->driver);
2955
2956 driver->driver.bus = NULL;
2957 hsotg->driver = driver;
2958 hsotg->gadget.dev.driver = &driver->driver;
2959 hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
2960 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2961
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002962 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
2963 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002964 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002965 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002966 goto err;
2967 }
2968
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002969 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002970
Lukasz Majewski308d7342012-05-04 14:17:05 +02002971 s3c_hsotg_core_init(hsotg);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002972 hsotg->last_rst = jiffies;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002973 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
2974 return 0;
2975
2976err:
2977 hsotg->driver = NULL;
2978 hsotg->gadget.dev.driver = NULL;
2979 return ret;
2980}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002981
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002982/**
2983 * s3c_hsotg_udc_stop - stop the udc
2984 * @gadget: The usb gadget state
2985 * @driver: The usb gadget driver
2986 *
2987 * Stop udc hw block and stay tunned for future transmissions
2988 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002989static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
2990 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002991{
2992 struct s3c_hsotg *hsotg = our_hsotg;
2993 int ep;
2994
2995 if (!hsotg)
2996 return -ENODEV;
2997
2998 if (!driver || driver != hsotg->driver || !driver->unbind)
2999 return -EINVAL;
3000
3001 /* all endpoints should be shutdown */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003002 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003003 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3004
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003005 s3c_hsotg_phy_disable(hsotg);
3006 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003007
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003008 hsotg->driver = NULL;
3009 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003010 hsotg->gadget.dev.driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003011
3012 dev_info(hsotg->dev, "unregistered gadget driver '%s'\n",
3013 driver->driver.name);
3014
3015 return 0;
3016}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003017
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003018/**
3019 * s3c_hsotg_gadget_getframe - read the frame number
3020 * @gadget: The usb gadget state
3021 *
3022 * Read the {micro} frame number
3023 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003024static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
3025{
3026 return s3c_hsotg_read_frameno(to_hsotg(gadget));
3027}
3028
3029static struct usb_gadget_ops s3c_hsotg_gadget_ops = {
3030 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003031 .udc_start = s3c_hsotg_udc_start,
3032 .udc_stop = s3c_hsotg_udc_stop,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003033};
3034
3035/**
3036 * s3c_hsotg_initep - initialise a single endpoint
3037 * @hsotg: The device state.
3038 * @hs_ep: The endpoint to be initialised.
3039 * @epnum: The endpoint number
3040 *
3041 * Initialise the given endpoint (as part of the probe and device state
3042 * creation) to give to the gadget driver. Setup the endpoint name, any
3043 * direction information and other state that may be required.
3044 */
3045static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
3046 struct s3c_hsotg_ep *hs_ep,
3047 int epnum)
3048{
3049 u32 ptxfifo;
3050 char *dir;
3051
3052 if (epnum == 0)
3053 dir = "";
3054 else if ((epnum % 2) == 0) {
3055 dir = "out";
3056 } else {
3057 dir = "in";
3058 hs_ep->dir_in = 1;
3059 }
3060
3061 hs_ep->index = epnum;
3062
3063 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3064
3065 INIT_LIST_HEAD(&hs_ep->queue);
3066 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3067
3068 spin_lock_init(&hs_ep->lock);
3069
3070 /* add to the list of endpoints known by the gadget driver */
3071 if (epnum)
3072 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3073
3074 hs_ep->parent = hsotg;
3075 hs_ep->ep.name = hs_ep->name;
3076 hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT;
3077 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3078
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003079 /*
3080 * Read the FIFO size for the Periodic TX FIFO, even if we're
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003081 * an OUT endpoint, we may as well do this if in future the
3082 * code is changed to make each endpoint's direction changeable.
3083 */
3084
3085 ptxfifo = readl(hsotg->regs + S3C_DPTXFSIZn(epnum));
Ben Dooks679f9b72010-07-19 09:40:41 +01003086 hs_ep->fifo_size = S3C_DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003087
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003088 /*
3089 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003090 * to be something valid.
3091 */
3092
3093 if (using_dma(hsotg)) {
3094 u32 next = S3C_DxEPCTL_NextEp((epnum + 1) % 15);
3095 writel(next, hsotg->regs + S3C_DIEPCTL(epnum));
3096 writel(next, hsotg->regs + S3C_DOEPCTL(epnum));
3097 }
3098}
3099
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003100/**
3101 * s3c_hsotg_hw_cfg - read HW configuration registers
3102 * @param: The device state
3103 *
3104 * Read the USB core HW configuration registers
3105 */
3106static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003107{
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003108 u32 cfg2, cfg4;
Ben Dooks10aebc72010-07-19 09:40:44 +01003109 /* check hardware configuration */
3110
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003111 cfg2 = readl(hsotg->regs + 0x48);
3112 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3113
3114 dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
3115
Ben Dooks10aebc72010-07-19 09:40:44 +01003116 cfg4 = readl(hsotg->regs + 0x50);
3117 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3118
3119 dev_info(hsotg->dev, "%s fifos\n",
3120 hsotg->dedicated_fifos ? "dedicated" : "shared");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003121}
3122
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003123/**
3124 * s3c_hsotg_dump - dump state of the udc
3125 * @param: The device state
3126 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003127static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
3128{
Mark Brown83a01802011-06-01 17:16:15 +01003129#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003130 struct device *dev = hsotg->dev;
3131 void __iomem *regs = hsotg->regs;
3132 u32 val;
3133 int idx;
3134
3135 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
3136 readl(regs + S3C_DCFG), readl(regs + S3C_DCTL),
3137 readl(regs + S3C_DIEPMSK));
3138
3139 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
3140 readl(regs + S3C_GAHBCFG), readl(regs + 0x44));
3141
3142 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
3143 readl(regs + S3C_GRXFSIZ), readl(regs + S3C_GNPTXFSIZ));
3144
3145 /* show periodic fifo settings */
3146
3147 for (idx = 1; idx <= 15; idx++) {
3148 val = readl(regs + S3C_DPTXFSIZn(idx));
3149 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
3150 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
3151 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
3152 }
3153
3154 for (idx = 0; idx < 15; idx++) {
3155 dev_info(dev,
3156 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
3157 readl(regs + S3C_DIEPCTL(idx)),
3158 readl(regs + S3C_DIEPTSIZ(idx)),
3159 readl(regs + S3C_DIEPDMA(idx)));
3160
3161 val = readl(regs + S3C_DOEPCTL(idx));
3162 dev_info(dev,
3163 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
3164 idx, readl(regs + S3C_DOEPCTL(idx)),
3165 readl(regs + S3C_DOEPTSIZ(idx)),
3166 readl(regs + S3C_DOEPDMA(idx)));
3167
3168 }
3169
3170 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
3171 readl(regs + S3C_DVBUSDIS), readl(regs + S3C_DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003172#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003173}
3174
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003175/**
3176 * state_show - debugfs: show overall driver and device state.
3177 * @seq: The seq file to write to.
3178 * @v: Unused parameter.
3179 *
3180 * This debugfs entry shows the overall state of the hardware and
3181 * some general information about each of the endpoints available
3182 * to the system.
3183 */
3184static int state_show(struct seq_file *seq, void *v)
3185{
3186 struct s3c_hsotg *hsotg = seq->private;
3187 void __iomem *regs = hsotg->regs;
3188 int idx;
3189
3190 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
3191 readl(regs + S3C_DCFG),
3192 readl(regs + S3C_DCTL),
3193 readl(regs + S3C_DSTS));
3194
3195 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
3196 readl(regs + S3C_DIEPMSK), readl(regs + S3C_DOEPMSK));
3197
3198 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
3199 readl(regs + S3C_GINTMSK),
3200 readl(regs + S3C_GINTSTS));
3201
3202 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
3203 readl(regs + S3C_DAINTMSK),
3204 readl(regs + S3C_DAINT));
3205
3206 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
3207 readl(regs + S3C_GNPTXSTS),
3208 readl(regs + S3C_GRXSTSR));
3209
3210 seq_printf(seq, "\nEndpoint status:\n");
3211
3212 for (idx = 0; idx < 15; idx++) {
3213 u32 in, out;
3214
3215 in = readl(regs + S3C_DIEPCTL(idx));
3216 out = readl(regs + S3C_DOEPCTL(idx));
3217
3218 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3219 idx, in, out);
3220
3221 in = readl(regs + S3C_DIEPTSIZ(idx));
3222 out = readl(regs + S3C_DOEPTSIZ(idx));
3223
3224 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3225 in, out);
3226
3227 seq_printf(seq, "\n");
3228 }
3229
3230 return 0;
3231}
3232
3233static int state_open(struct inode *inode, struct file *file)
3234{
3235 return single_open(file, state_show, inode->i_private);
3236}
3237
3238static const struct file_operations state_fops = {
3239 .owner = THIS_MODULE,
3240 .open = state_open,
3241 .read = seq_read,
3242 .llseek = seq_lseek,
3243 .release = single_release,
3244};
3245
3246/**
3247 * fifo_show - debugfs: show the fifo information
3248 * @seq: The seq_file to write data to.
3249 * @v: Unused parameter.
3250 *
3251 * Show the FIFO information for the overall fifo and all the
3252 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003253 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003254static int fifo_show(struct seq_file *seq, void *v)
3255{
3256 struct s3c_hsotg *hsotg = seq->private;
3257 void __iomem *regs = hsotg->regs;
3258 u32 val;
3259 int idx;
3260
3261 seq_printf(seq, "Non-periodic FIFOs:\n");
3262 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + S3C_GRXFSIZ));
3263
3264 val = readl(regs + S3C_GNPTXFSIZ);
3265 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
3266 val >> S3C_GNPTXFSIZ_NPTxFDep_SHIFT,
3267 val & S3C_GNPTXFSIZ_NPTxFStAddr_MASK);
3268
3269 seq_printf(seq, "\nPeriodic TXFIFOs:\n");
3270
3271 for (idx = 1; idx <= 15; idx++) {
3272 val = readl(regs + S3C_DPTXFSIZn(idx));
3273
3274 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
3275 val >> S3C_DPTXFSIZn_DPTxFSize_SHIFT,
3276 val & S3C_DPTXFSIZn_DPTxFStAddr_MASK);
3277 }
3278
3279 return 0;
3280}
3281
3282static int fifo_open(struct inode *inode, struct file *file)
3283{
3284 return single_open(file, fifo_show, inode->i_private);
3285}
3286
3287static const struct file_operations fifo_fops = {
3288 .owner = THIS_MODULE,
3289 .open = fifo_open,
3290 .read = seq_read,
3291 .llseek = seq_lseek,
3292 .release = single_release,
3293};
3294
3295
3296static const char *decode_direction(int is_in)
3297{
3298 return is_in ? "in" : "out";
3299}
3300
3301/**
3302 * ep_show - debugfs: show the state of an endpoint.
3303 * @seq: The seq_file to write data to.
3304 * @v: Unused parameter.
3305 *
3306 * This debugfs entry shows the state of the given endpoint (one is
3307 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003308 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003309static int ep_show(struct seq_file *seq, void *v)
3310{
3311 struct s3c_hsotg_ep *ep = seq->private;
3312 struct s3c_hsotg *hsotg = ep->parent;
3313 struct s3c_hsotg_req *req;
3314 void __iomem *regs = hsotg->regs;
3315 int index = ep->index;
3316 int show_limit = 15;
3317 unsigned long flags;
3318
3319 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3320 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3321
3322 /* first show the register state */
3323
3324 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
3325 readl(regs + S3C_DIEPCTL(index)),
3326 readl(regs + S3C_DOEPCTL(index)));
3327
3328 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
3329 readl(regs + S3C_DIEPDMA(index)),
3330 readl(regs + S3C_DOEPDMA(index)));
3331
3332 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
3333 readl(regs + S3C_DIEPINT(index)),
3334 readl(regs + S3C_DOEPINT(index)));
3335
3336 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
3337 readl(regs + S3C_DIEPTSIZ(index)),
3338 readl(regs + S3C_DOEPTSIZ(index)));
3339
3340 seq_printf(seq, "\n");
3341 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3342 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3343
3344 seq_printf(seq, "request list (%p,%p):\n",
3345 ep->queue.next, ep->queue.prev);
3346
3347 spin_lock_irqsave(&ep->lock, flags);
3348
3349 list_for_each_entry(req, &ep->queue, queue) {
3350 if (--show_limit < 0) {
3351 seq_printf(seq, "not showing more requests...\n");
3352 break;
3353 }
3354
3355 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3356 req == ep->req ? '*' : ' ',
3357 req, req->req.length, req->req.buf);
3358 seq_printf(seq, "%d done, res %d\n",
3359 req->req.actual, req->req.status);
3360 }
3361
3362 spin_unlock_irqrestore(&ep->lock, flags);
3363
3364 return 0;
3365}
3366
3367static int ep_open(struct inode *inode, struct file *file)
3368{
3369 return single_open(file, ep_show, inode->i_private);
3370}
3371
3372static const struct file_operations ep_fops = {
3373 .owner = THIS_MODULE,
3374 .open = ep_open,
3375 .read = seq_read,
3376 .llseek = seq_lseek,
3377 .release = single_release,
3378};
3379
3380/**
3381 * s3c_hsotg_create_debug - create debugfs directory and files
3382 * @hsotg: The driver state
3383 *
3384 * Create the debugfs files to allow the user to get information
3385 * about the state of the system. The directory name is created
3386 * with the same name as the device itself, in case we end up
3387 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003388 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003389static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3390{
3391 struct dentry *root;
3392 unsigned epidx;
3393
3394 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3395 hsotg->debug_root = root;
3396 if (IS_ERR(root)) {
3397 dev_err(hsotg->dev, "cannot create debug root\n");
3398 return;
3399 }
3400
3401 /* create general state file */
3402
3403 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3404 hsotg, &state_fops);
3405
3406 if (IS_ERR(hsotg->debug_file))
3407 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3408
3409 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3410 hsotg, &fifo_fops);
3411
3412 if (IS_ERR(hsotg->debug_fifo))
3413 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3414
3415 /* create one file for each endpoint */
3416
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003417 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003418 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3419
3420 ep->debugfs = debugfs_create_file(ep->name, 0444,
3421 root, ep, &ep_fops);
3422
3423 if (IS_ERR(ep->debugfs))
3424 dev_err(hsotg->dev, "failed to create %s debug file\n",
3425 ep->name);
3426 }
3427}
3428
3429/**
3430 * s3c_hsotg_delete_debug - cleanup debugfs entries
3431 * @hsotg: The driver state
3432 *
3433 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003434 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003435static void __devexit s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3436{
3437 unsigned epidx;
3438
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003439 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003440 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3441 debugfs_remove(ep->debugfs);
3442 }
3443
3444 debugfs_remove(hsotg->debug_file);
3445 debugfs_remove(hsotg->debug_fifo);
3446 debugfs_remove(hsotg->debug_root);
3447}
3448
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003449/**
3450 * s3c_hsotg_probe - probe function for hsotg driver
3451 * @pdev: The platform information for the driver
3452 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003453static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3454{
3455 struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
3456 struct device *dev = &pdev->dev;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003457 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003458 struct s3c_hsotg *hsotg;
3459 struct resource *res;
3460 int epnum;
3461 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003462 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003463
Lukasz Majewski41188782012-05-04 14:17:01 +02003464 plat = pdev->dev.platform_data;
3465 if (!plat) {
3466 dev_err(&pdev->dev, "no platform data defined\n");
3467 return -EINVAL;
3468 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003469
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003470 hsotg = kzalloc(sizeof(struct s3c_hsotg), GFP_KERNEL);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003471 if (!hsotg) {
3472 dev_err(dev, "cannot get memory\n");
3473 return -ENOMEM;
3474 }
3475
3476 hsotg->dev = dev;
3477 hsotg->plat = plat;
3478
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003479 hsotg->clk = clk_get(&pdev->dev, "otg");
3480 if (IS_ERR(hsotg->clk)) {
3481 dev_err(dev, "cannot get otg clock\n");
Jingoo Han2328cea2011-05-13 21:26:23 +09003482 ret = PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003483 goto err_mem;
3484 }
3485
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003486 platform_set_drvdata(pdev, hsotg);
3487
3488 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
3489 if (!res) {
3490 dev_err(dev, "cannot find register resource 0\n");
3491 ret = -EINVAL;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003492 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003493 }
3494
3495 hsotg->regs_res = request_mem_region(res->start, resource_size(res),
3496 dev_name(dev));
3497 if (!hsotg->regs_res) {
3498 dev_err(dev, "cannot reserve registers\n");
3499 ret = -ENOENT;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003500 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003501 }
3502
3503 hsotg->regs = ioremap(res->start, resource_size(res));
3504 if (!hsotg->regs) {
3505 dev_err(dev, "cannot map registers\n");
3506 ret = -ENXIO;
3507 goto err_regs_res;
3508 }
3509
3510 ret = platform_get_irq(pdev, 0);
3511 if (ret < 0) {
3512 dev_err(dev, "cannot find IRQ\n");
3513 goto err_regs;
3514 }
3515
3516 hsotg->irq = ret;
3517
3518 ret = request_irq(ret, s3c_hsotg_irq, 0, dev_name(dev), hsotg);
3519 if (ret < 0) {
3520 dev_err(dev, "cannot claim IRQ\n");
3521 goto err_regs;
3522 }
3523
3524 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3525
3526 device_initialize(&hsotg->gadget.dev);
3527
3528 dev_set_name(&hsotg->gadget.dev, "gadget");
3529
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003530 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003531 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3532 hsotg->gadget.name = dev_name(dev);
3533
3534 hsotg->gadget.dev.parent = dev;
3535 hsotg->gadget.dev.dma_mask = dev->dma_mask;
3536
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003537 /* reset the system */
3538
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003539 clk_enable(hsotg->clk);
3540
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003541 /* regulators */
3542
3543 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3544 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3545
3546 ret = regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
3547 hsotg->supplies);
3548 if (ret) {
3549 dev_err(dev, "failed to request supplies: %d\n", ret);
3550 goto err_supplies;
3551 }
3552
3553 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3554 hsotg->supplies);
3555
3556 if (ret) {
3557 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3558 goto err_supplies;
3559 }
3560
Lukasz Majewski41188782012-05-04 14:17:01 +02003561 /* usb phy enable */
3562 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003563
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003564 s3c_hsotg_corereset(hsotg);
3565 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003566 s3c_hsotg_hw_cfg(hsotg);
3567
3568 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3569
3570 if (hsotg->num_of_eps == 0) {
3571 dev_err(dev, "wrong number of EPs (zero)\n");
3572 goto err_supplies;
3573 }
3574
3575 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3576 GFP_KERNEL);
3577 if (!eps) {
3578 dev_err(dev, "cannot get memory\n");
3579 goto err_supplies;
3580 }
3581
3582 hsotg->eps = eps;
3583
3584 /* setup endpoint information */
3585
3586 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3587 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3588
3589 /* allocate EP0 request */
3590
3591 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3592 GFP_KERNEL);
3593 if (!hsotg->ctrl_req) {
3594 dev_err(dev, "failed to allocate ctrl req\n");
3595 goto err_ep_mem;
3596 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003597
3598 /* initialise the endpoints now the core has been initialised */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003599 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003600 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3601
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003602 /* disable power and clock */
3603
3604 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3605 hsotg->supplies);
3606 if (ret) {
3607 dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
3608 goto err_ep_mem;
3609 }
3610
3611 s3c_hsotg_phy_disable(hsotg);
3612
3613 ret = device_add(&hsotg->gadget.dev);
3614 if (ret) {
3615 put_device(&hsotg->gadget.dev);
3616 goto err_ep_mem;
3617 }
3618
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003619 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3620 if (ret)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003621 goto err_ep_mem;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003622
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003623 s3c_hsotg_create_debug(hsotg);
3624
3625 s3c_hsotg_dump(hsotg);
3626
3627 our_hsotg = hsotg;
3628 return 0;
3629
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003630 err_ep_mem:
3631 kfree(eps);
3632
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003633err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003634 s3c_hsotg_phy_disable(hsotg);
3635
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003636 regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3637
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003638 clk_disable(hsotg->clk);
3639 clk_put(hsotg->clk);
3640
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003641err_regs:
3642 iounmap(hsotg->regs);
3643
3644err_regs_res:
3645 release_resource(hsotg->regs_res);
3646 kfree(hsotg->regs_res);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003647err_clk:
3648 clk_put(hsotg->clk);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003649err_mem:
3650 kfree(hsotg);
3651 return ret;
3652}
3653
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003654/**
3655 * s3c_hsotg_remove - remove function for hsotg driver
3656 * @pdev: The platform information for the driver
3657 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003658static int __devexit s3c_hsotg_remove(struct platform_device *pdev)
3659{
3660 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3661
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003662 usb_del_gadget_udc(&hsotg->gadget);
3663
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003664 s3c_hsotg_delete_debug(hsotg);
3665
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003666 if (hsotg->driver) {
3667 /* should have been done already by driver model core */
3668 usb_gadget_unregister_driver(hsotg->driver);
3669 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003670
3671 free_irq(hsotg->irq, hsotg);
3672 iounmap(hsotg->regs);
3673
3674 release_resource(hsotg->regs_res);
3675 kfree(hsotg->regs_res);
3676
Lukasz Majewski41188782012-05-04 14:17:01 +02003677 s3c_hsotg_phy_disable(hsotg);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003678 regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3679
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003680 clk_disable(hsotg->clk);
3681 clk_put(hsotg->clk);
3682
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003683 device_unregister(&hsotg->gadget.dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003684 kfree(hsotg);
3685 return 0;
3686}
3687
3688#if 1
3689#define s3c_hsotg_suspend NULL
3690#define s3c_hsotg_resume NULL
3691#endif
3692
3693static struct platform_driver s3c_hsotg_driver = {
3694 .driver = {
3695 .name = "s3c-hsotg",
3696 .owner = THIS_MODULE,
3697 },
3698 .probe = s3c_hsotg_probe,
3699 .remove = __devexit_p(s3c_hsotg_remove),
3700 .suspend = s3c_hsotg_suspend,
3701 .resume = s3c_hsotg_resume,
3702};
3703
Axel Lincc27c962011-11-27 20:16:27 +08003704module_platform_driver(s3c_hsotg_driver);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003705
3706MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3707MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3708MODULE_LICENSE("GPL");
3709MODULE_ALIAS("platform:s3c-hsotg");