blob: 5b90a3c159e6b627cdd2cc92694dbbbefd655837 [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>
Lukasz Majewski126625e2012-05-09 13:16:53 +020035#include <linux/platform_data/s3c-hsotg.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010036
37#include <mach/map.h>
38
Lukasz Majewski127d42a2012-05-04 14:16:59 +020039#include "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
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100115
116 unsigned long total_data;
117 unsigned int size_loaded;
118 unsigned int last_load;
119 unsigned int fifo_load;
120 unsigned short fifo_size;
121
122 unsigned char dir_in;
123 unsigned char index;
124
125 unsigned int halted:1;
126 unsigned int periodic:1;
127 unsigned int sent_zlp:1;
128
129 char name[10];
130};
131
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100132/**
133 * struct s3c_hsotg - driver state.
134 * @dev: The parent device supplied to the probe function
135 * @driver: USB gadget driver
136 * @plat: The platform specific configuration data.
137 * @regs: The memory area mapped for accessing registers.
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100138 * @irq: The IRQ number we are using
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200139 * @supplies: Definition of USB power supplies
Ben Dooks10aebc72010-07-19 09:40:44 +0100140 * @dedicated_fifos: Set if the hardware has dedicated IN-EP fifos.
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200141 * @num_of_eps: Number of available EPs (excluding EP0)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100142 * @debug_root: root directrory for debugfs.
143 * @debug_file: main status file for debugfs.
144 * @debug_fifo: FIFO status file for debugfs.
145 * @ep0_reply: Request used for ep0 reply.
146 * @ep0_buff: Buffer for EP0 reply data, if needed.
147 * @ctrl_buff: Buffer for EP0 control requests.
148 * @ctrl_req: Request for EP0 control packets.
Lukasz Majewski71225be2012-05-04 14:17:03 +0200149 * @setup: NAK management for EP0 SETUP
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200150 * @last_rst: Time of last reset
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100151 * @eps: The endpoints being supplied to the gadget framework
152 */
153struct s3c_hsotg {
154 struct device *dev;
155 struct usb_gadget_driver *driver;
156 struct s3c_hsotg_plat *plat;
157
Lukasz Majewski22258f42012-06-14 10:02:24 +0200158 spinlock_t lock;
159
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100160 void __iomem *regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100161 int irq;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +0200162 struct clk *clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100163
Lukasz Majewskifc9a7312012-05-04 14:17:02 +0200164 struct regulator_bulk_data supplies[ARRAY_SIZE(s3c_hsotg_supply_names)];
165
Ben Dooks10aebc72010-07-19 09:40:44 +0100166 unsigned int dedicated_fifos:1;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200167 unsigned char num_of_eps;
Ben Dooks10aebc72010-07-19 09:40:44 +0100168
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100169 struct dentry *debug_root;
170 struct dentry *debug_file;
171 struct dentry *debug_fifo;
172
173 struct usb_request *ep0_reply;
174 struct usb_request *ctrl_req;
175 u8 ep0_buff[8];
176 u8 ctrl_buff[8];
177
178 struct usb_gadget gadget;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200179 unsigned int setup;
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +0200180 unsigned long last_rst;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200181 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100182};
183
184/**
185 * struct s3c_hsotg_req - data transfer request
186 * @req: The USB gadget request
187 * @queue: The list of requests for the endpoint this is queued for.
188 * @in_progress: Has already had size/packets written to core
189 * @mapped: DMA buffer for this request has been mapped via dma_map_single().
190 */
191struct s3c_hsotg_req {
192 struct usb_request req;
193 struct list_head queue;
194 unsigned char in_progress;
195 unsigned char mapped;
196};
197
198/* conversion functions */
199static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
200{
201 return container_of(req, struct s3c_hsotg_req, req);
202}
203
204static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
205{
206 return container_of(ep, struct s3c_hsotg_ep, ep);
207}
208
209static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
210{
211 return container_of(gadget, struct s3c_hsotg, gadget);
212}
213
214static inline void __orr32(void __iomem *ptr, u32 val)
215{
216 writel(readl(ptr) | val, ptr);
217}
218
219static inline void __bic32(void __iomem *ptr, u32 val)
220{
221 writel(readl(ptr) & ~val, ptr);
222}
223
224/* forward decleration of functions */
225static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
226
227/**
228 * using_dma - return the DMA status of the driver.
229 * @hsotg: The driver state.
230 *
231 * Return true if we're using DMA.
232 *
233 * Currently, we have the DMA support code worked into everywhere
234 * that needs it, but the AMBA DMA implementation in the hardware can
235 * only DMA from 32bit aligned addresses. This means that gadgets such
236 * as the CDC Ethernet cannot work as they often pass packets which are
237 * not 32bit aligned.
238 *
239 * Unfortunately the choice to use DMA or not is global to the controller
240 * and seems to be only settable when the controller is being put through
241 * a core reset. This means we either need to fix the gadgets to take
242 * account of DMA alignment, or add bounce buffers (yuerk).
243 *
244 * Until this issue is sorted out, we always return 'false'.
245 */
246static inline bool using_dma(struct s3c_hsotg *hsotg)
247{
248 return false; /* support is not complete */
249}
250
251/**
252 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
253 * @hsotg: The device state
254 * @ints: A bitmask of the interrupts to enable
255 */
256static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
257{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200258 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100259 u32 new_gsintmsk;
260
261 new_gsintmsk = gsintmsk | ints;
262
263 if (new_gsintmsk != gsintmsk) {
264 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200265 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100266 }
267}
268
269/**
270 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
271 * @hsotg: The device state
272 * @ints: A bitmask of the interrupts to enable
273 */
274static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
275{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200276 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100277 u32 new_gsintmsk;
278
279 new_gsintmsk = gsintmsk & ~ints;
280
281 if (new_gsintmsk != gsintmsk)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200282 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100283}
284
285/**
286 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
287 * @hsotg: The device state
288 * @ep: The endpoint index
289 * @dir_in: True if direction is in.
290 * @en: The enable value, true to enable
291 *
292 * Set or clear the mask for an individual endpoint's interrupt
293 * request.
294 */
295static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
296 unsigned int ep, unsigned int dir_in,
297 unsigned int en)
298{
299 unsigned long flags;
300 u32 bit = 1 << ep;
301 u32 daint;
302
303 if (!dir_in)
304 bit <<= 16;
305
306 local_irq_save(flags);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200307 daint = readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100308 if (en)
309 daint |= bit;
310 else
311 daint &= ~bit;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200312 writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100313 local_irq_restore(flags);
314}
315
316/**
317 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
318 * @hsotg: The device instance.
319 */
320static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
321{
Ben Dooks0f002d22010-05-25 05:36:50 +0100322 unsigned int ep;
323 unsigned int addr;
324 unsigned int size;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100325 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100326 u32 val;
327
Ben Dooks6d091ee2010-07-19 09:40:40 +0100328 /* set FIFO sizes to 2048/1024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100329
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200330 writel(2048, hsotg->regs + GRXFSIZ);
331 writel(GNPTXFSIZ_NPTxFStAddr(2048) |
332 GNPTXFSIZ_NPTxFDep(1024),
333 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100334
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200335 /*
336 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100337 * block have overlapping default addresses. This also ensures
338 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200339 * known values.
340 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100341
342 /* start at the end of the GNPTXFSIZ, rounded up */
343 addr = 2048 + 1024;
344 size = 768;
345
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200346 /*
347 * currently we allocate TX FIFOs for all possible endpoints,
348 * and assume that they are all the same size.
349 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100350
Anton Tikhomirovf7a83fe2012-03-06 14:05:49 +0900351 for (ep = 1; ep <= 15; ep++) {
Ben Dooks0f002d22010-05-25 05:36:50 +0100352 val = addr;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200353 val |= size << DPTXFSIZn_DPTxFSize_SHIFT;
Ben Dooks0f002d22010-05-25 05:36:50 +0100354 addr += size;
355
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200356 writel(val, hsotg->regs + DPTXFSIZn(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100357 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100358
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200359 /*
360 * according to p428 of the design guide, we need to ensure that
361 * all fifos are flushed before continuing
362 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100363
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200364 writel(GRSTCTL_TxFNum(0x10) | GRSTCTL_TxFFlsh |
365 GRSTCTL_RxFFlsh, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100366
367 /* wait until the fifos are both flushed */
368 timeout = 100;
369 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200370 val = readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100371
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200372 if ((val & (GRSTCTL_TxFFlsh | GRSTCTL_RxFFlsh)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100373 break;
374
375 if (--timeout == 0) {
376 dev_err(hsotg->dev,
377 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
378 __func__, val);
379 }
380
381 udelay(1);
382 }
383
384 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100385}
386
387/**
388 * @ep: USB endpoint to allocate request for.
389 * @flags: Allocation flags
390 *
391 * Allocate a new USB request structure appropriate for the specified endpoint
392 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000393static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
394 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100395{
396 struct s3c_hsotg_req *req;
397
398 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
399 if (!req)
400 return NULL;
401
402 INIT_LIST_HEAD(&req->queue);
403
404 req->req.dma = DMA_ADDR_INVALID;
405 return &req->req;
406}
407
408/**
409 * is_ep_periodic - return true if the endpoint is in periodic mode.
410 * @hs_ep: The endpoint to query.
411 *
412 * Returns true if the endpoint is in periodic mode, meaning it is being
413 * used for an Interrupt or ISO transfer.
414 */
415static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
416{
417 return hs_ep->periodic;
418}
419
420/**
421 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
422 * @hsotg: The device state.
423 * @hs_ep: The endpoint for the request
424 * @hs_req: The request being processed.
425 *
426 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
427 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200428 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100429static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
430 struct s3c_hsotg_ep *hs_ep,
431 struct s3c_hsotg_req *hs_req)
432{
433 struct usb_request *req = &hs_req->req;
434 enum dma_data_direction dir;
435
436 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
437
438 /* ignore this if we're not moving any data */
439 if (hs_req->req.length == 0)
440 return;
441
442 if (hs_req->mapped) {
443 /* we mapped this, so unmap and remove the dma */
444
445 dma_unmap_single(hsotg->dev, req->dma, req->length, dir);
446
447 req->dma = DMA_ADDR_INVALID;
448 hs_req->mapped = 0;
449 } else {
FUJITA Tomonori5b520252010-01-25 11:07:19 +0900450 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100451 }
452}
453
454/**
455 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
456 * @hsotg: The controller state.
457 * @hs_ep: The endpoint we're going to write for.
458 * @hs_req: The request to write data for.
459 *
460 * This is called when the TxFIFO has some space in it to hold a new
461 * transmission and we have something to give it. The actual setup of
462 * the data size is done elsewhere, so all we have to do is to actually
463 * write the data.
464 *
465 * The return value is zero if there is more space (or nothing was done)
466 * otherwise -ENOSPC is returned if the FIFO space was used up.
467 *
468 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200469 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100470static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
471 struct s3c_hsotg_ep *hs_ep,
472 struct s3c_hsotg_req *hs_req)
473{
474 bool periodic = is_ep_periodic(hs_ep);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200475 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100476 int buf_pos = hs_req->req.actual;
477 int to_write = hs_ep->size_loaded;
478 void *data;
479 int can_write;
480 int pkt_round;
481
482 to_write -= (buf_pos - hs_ep->last_load);
483
484 /* if there's nothing to write, get out early */
485 if (to_write == 0)
486 return 0;
487
Ben Dooks10aebc72010-07-19 09:40:44 +0100488 if (periodic && !hsotg->dedicated_fifos) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200489 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100490 int size_left;
491 int size_done;
492
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200493 /*
494 * work out how much data was loaded so we can calculate
495 * how much data is left in the fifo.
496 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100497
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200498 size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100499
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200500 /*
501 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100502 * previous data has been completely sent.
503 */
504 if (hs_ep->fifo_load != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200505 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100506 return -ENOSPC;
507 }
508
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100509 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
510 __func__, size_left,
511 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
512
513 /* how much of the data has moved */
514 size_done = hs_ep->size_loaded - size_left;
515
516 /* how much data is left in the fifo */
517 can_write = hs_ep->fifo_load - size_done;
518 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
519 __func__, can_write);
520
521 can_write = hs_ep->fifo_size - can_write;
522 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
523 __func__, can_write);
524
525 if (can_write <= 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200526 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100527 return -ENOSPC;
528 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100529 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200530 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100531
532 can_write &= 0xffff;
533 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100534 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200535 if (GNPTXSTS_NPTxQSpcAvail_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100536 dev_dbg(hsotg->dev,
537 "%s: no queue slots available (0x%08x)\n",
538 __func__, gnptxsts);
539
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200540 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100541 return -ENOSPC;
542 }
543
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200544 can_write = GNPTXSTS_NPTxFSpcAvail_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100545 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100546 }
547
548 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, mps %d\n",
549 __func__, gnptxsts, can_write, to_write, hs_ep->ep.maxpacket);
550
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200551 /*
552 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100553 * FIFO, requests of >512 cause the endpoint to get stuck with a
554 * fragment of the end of the transfer in it.
555 */
556 if (can_write > 512)
557 can_write = 512;
558
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200559 /*
560 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100561 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200562 * doing it.
563 */
Ben Dooks03e10e52010-07-19 09:40:45 +0100564 if (to_write > hs_ep->ep.maxpacket) {
565 to_write = hs_ep->ep.maxpacket;
566
567 s3c_hsotg_en_gsint(hsotg,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200568 periodic ? GINTSTS_PTxFEmp :
569 GINTSTS_NPTxFEmp);
Ben Dooks03e10e52010-07-19 09:40:45 +0100570 }
571
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100572 /* see if we can write data */
573
574 if (to_write > can_write) {
575 to_write = can_write;
576 pkt_round = to_write % hs_ep->ep.maxpacket;
577
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200578 /*
579 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100580 * exact number of packets.
581 *
582 * Note, we do not currently check to see if we can ever
583 * write a full packet or not to the FIFO.
584 */
585
586 if (pkt_round)
587 to_write -= pkt_round;
588
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200589 /*
590 * enable correct FIFO interrupt to alert us when there
591 * is more room left.
592 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100593
594 s3c_hsotg_en_gsint(hsotg,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200595 periodic ? GINTSTS_PTxFEmp :
596 GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100597 }
598
599 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
600 to_write, hs_req->req.length, can_write, buf_pos);
601
602 if (to_write <= 0)
603 return -ENOSPC;
604
605 hs_req->req.actual = buf_pos + to_write;
606 hs_ep->total_data += to_write;
607
608 if (periodic)
609 hs_ep->fifo_load += to_write;
610
611 to_write = DIV_ROUND_UP(to_write, 4);
612 data = hs_req->req.buf + buf_pos;
613
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200614 writesl(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100615
616 return (to_write >= can_write) ? -ENOSPC : 0;
617}
618
619/**
620 * get_ep_limit - get the maximum data legnth for this endpoint
621 * @hs_ep: The endpoint
622 *
623 * Return the maximum data that can be queued in one go on a given endpoint
624 * so that transfers that are too long can be split.
625 */
626static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
627{
628 int index = hs_ep->index;
629 unsigned maxsize;
630 unsigned maxpkt;
631
632 if (index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200633 maxsize = DxEPTSIZ_XferSize_LIMIT + 1;
634 maxpkt = DxEPTSIZ_PktCnt_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100635 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100636 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900637 if (hs_ep->dir_in)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200638 maxpkt = DIEPTSIZ0_PktCnt_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900639 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100640 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100641 }
642
643 /* we made the constant loading easier above by using +1 */
644 maxpkt--;
645 maxsize--;
646
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200647 /*
648 * constrain by packet count if maxpkts*pktsize is greater
649 * than the length register size.
650 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100651
652 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
653 maxsize = maxpkt * hs_ep->ep.maxpacket;
654
655 return maxsize;
656}
657
658/**
659 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
660 * @hsotg: The controller state.
661 * @hs_ep: The endpoint to process a request for
662 * @hs_req: The request to start.
663 * @continuing: True if we are doing more for the current request.
664 *
665 * Start the given request running by setting the endpoint registers
666 * appropriately, and writing any data to the FIFOs.
667 */
668static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
669 struct s3c_hsotg_ep *hs_ep,
670 struct s3c_hsotg_req *hs_req,
671 bool continuing)
672{
673 struct usb_request *ureq = &hs_req->req;
674 int index = hs_ep->index;
675 int dir_in = hs_ep->dir_in;
676 u32 epctrl_reg;
677 u32 epsize_reg;
678 u32 epsize;
679 u32 ctrl;
680 unsigned length;
681 unsigned packets;
682 unsigned maxreq;
683
684 if (index != 0) {
685 if (hs_ep->req && !continuing) {
686 dev_err(hsotg->dev, "%s: active request\n", __func__);
687 WARN_ON(1);
688 return;
689 } else if (hs_ep->req != hs_req && continuing) {
690 dev_err(hsotg->dev,
691 "%s: continue different req\n", __func__);
692 WARN_ON(1);
693 return;
694 }
695 }
696
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200697 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
698 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100699
700 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
701 __func__, readl(hsotg->regs + epctrl_reg), index,
702 hs_ep->dir_in ? "in" : "out");
703
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900704 /* If endpoint is stalled, we will restart request later */
705 ctrl = readl(hsotg->regs + epctrl_reg);
706
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200707 if (ctrl & DxEPCTL_Stall) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900708 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
709 return;
710 }
711
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100712 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200713 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
714 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100715 if (0)
716 dev_dbg(hsotg->dev,
717 "REQ buf %p len %d dma 0x%08x noi=%d zp=%d snok=%d\n",
718 ureq->buf, length, ureq->dma,
719 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
720
721 maxreq = get_ep_limit(hs_ep);
722 if (length > maxreq) {
723 int round = maxreq % hs_ep->ep.maxpacket;
724
725 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
726 __func__, length, maxreq, round);
727
728 /* round down to multiple of packets */
729 if (round)
730 maxreq -= round;
731
732 length = maxreq;
733 }
734
735 if (length)
736 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
737 else
738 packets = 1; /* send one packet if length is zero. */
739
740 if (dir_in && index != 0)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200741 epsize = DxEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100742 else
743 epsize = 0;
744
745 if (index != 0 && ureq->zero) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200746 /*
747 * test for the packets being exactly right for the
748 * transfer
749 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100750
751 if (length == (packets * hs_ep->ep.maxpacket))
752 packets++;
753 }
754
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200755 epsize |= DxEPTSIZ_PktCnt(packets);
756 epsize |= DxEPTSIZ_XferSize(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100757
758 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
759 __func__, packets, length, ureq->length, epsize, epsize_reg);
760
761 /* store the request as the current one we're doing */
762 hs_ep->req = hs_req;
763
764 /* write size / packets */
765 writel(epsize, hsotg->regs + epsize_reg);
766
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900767 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100768 unsigned int dma_reg;
769
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200770 /*
771 * write DMA address to control register, buffer already
772 * synced by s3c_hsotg_ep_queue().
773 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100774
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200775 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100776 writel(ureq->dma, hsotg->regs + dma_reg);
777
778 dev_dbg(hsotg->dev, "%s: 0x%08x => 0x%08x\n",
779 __func__, ureq->dma, dma_reg);
780 }
781
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200782 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
783 ctrl |= DxEPCTL_USBActEp;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200784
785 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
786
787 /* For Setup request do not clear NAK */
788 if (hsotg->setup && index == 0)
789 hsotg->setup = 0;
790 else
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200791 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200792
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100793
794 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
795 writel(ctrl, hsotg->regs + epctrl_reg);
796
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200797 /*
798 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100799 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200800 * this information.
801 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100802 hs_ep->size_loaded = length;
803 hs_ep->last_load = ureq->actual;
804
805 if (dir_in && !using_dma(hsotg)) {
806 /* set these anyway, we may need them for non-periodic in */
807 hs_ep->fifo_load = 0;
808
809 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
810 }
811
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200812 /*
813 * clear the INTknTXFEmpMsk when we start request, more as a aide
814 * to debugging to see what is going on.
815 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100816 if (dir_in)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200817 writel(DIEPMSK_INTknTXFEmpMsk,
818 hsotg->regs + DIEPINT(index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100819
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200820 /*
821 * Note, trying to clear the NAK here causes problems with transmit
822 * on the S3C6400 ending up with the TXFIFO becoming full.
823 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100824
825 /* check ep is enabled */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200826 if (!(readl(hsotg->regs + epctrl_reg) & DxEPCTL_EPEna))
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100827 dev_warn(hsotg->dev,
828 "ep%d: failed to become enabled (DxEPCTL=0x%08x)?\n",
829 index, readl(hsotg->regs + epctrl_reg));
830
831 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n",
832 __func__, readl(hsotg->regs + epctrl_reg));
833}
834
835/**
836 * s3c_hsotg_map_dma - map the DMA memory being used for the request
837 * @hsotg: The device state.
838 * @hs_ep: The endpoint the request is on.
839 * @req: The request being processed.
840 *
841 * We've been asked to queue a request, so ensure that the memory buffer
842 * is correctly setup for DMA. If we've been passed an extant DMA address
843 * then ensure the buffer has been synced to memory. If our buffer has no
844 * DMA memory, then we map the memory and mark our request to allow us to
845 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200846 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100847static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
848 struct s3c_hsotg_ep *hs_ep,
849 struct usb_request *req)
850{
851 enum dma_data_direction dir;
852 struct s3c_hsotg_req *hs_req = our_req(req);
853
854 dir = hs_ep->dir_in ? DMA_TO_DEVICE : DMA_FROM_DEVICE;
855
856 /* if the length is zero, ignore the DMA data */
857 if (hs_req->req.length == 0)
858 return 0;
859
860 if (req->dma == DMA_ADDR_INVALID) {
861 dma_addr_t dma;
862
863 dma = dma_map_single(hsotg->dev, req->buf, req->length, dir);
864
865 if (unlikely(dma_mapping_error(hsotg->dev, dma)))
866 goto dma_error;
867
868 if (dma & 3) {
869 dev_err(hsotg->dev, "%s: unaligned dma buffer\n",
870 __func__);
871
872 dma_unmap_single(hsotg->dev, dma, req->length, dir);
873 return -EINVAL;
874 }
875
876 hs_req->mapped = 1;
877 req->dma = dma;
878 } else {
FUJITA Tomonori5b520252010-01-25 11:07:19 +0900879 dma_sync_single_for_cpu(hsotg->dev, req->dma, req->length, dir);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100880 hs_req->mapped = 0;
881 }
882
883 return 0;
884
885dma_error:
886 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
887 __func__, req->buf, req->length);
888
889 return -EIO;
890}
891
892static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
893 gfp_t gfp_flags)
894{
895 struct s3c_hsotg_req *hs_req = our_req(req);
896 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
897 struct s3c_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100898 bool first;
899
900 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
901 ep->name, req, req->length, req->buf, req->no_interrupt,
902 req->zero, req->short_not_ok);
903
904 /* initialise status of the request */
905 INIT_LIST_HEAD(&hs_req->queue);
906 req->actual = 0;
907 req->status = -EINPROGRESS;
908
909 /* if we're using DMA, sync the buffers as necessary */
910 if (using_dma(hs)) {
911 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
912 if (ret)
913 return ret;
914 }
915
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100916 first = list_empty(&hs_ep->queue);
917 list_add_tail(&hs_req->queue, &hs_ep->queue);
918
919 if (first)
920 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
921
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100922 return 0;
923}
924
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200925static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
926 gfp_t gfp_flags)
927{
928 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
929 struct s3c_hsotg *hs = hs_ep->parent;
930 unsigned long flags = 0;
931 int ret = 0;
932
933 spin_lock_irqsave(&hs->lock, flags);
934 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
935 spin_unlock_irqrestore(&hs->lock, flags);
936
937 return ret;
938}
939
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100940static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
941 struct usb_request *req)
942{
943 struct s3c_hsotg_req *hs_req = our_req(req);
944
945 kfree(hs_req);
946}
947
948/**
949 * s3c_hsotg_complete_oursetup - setup completion callback
950 * @ep: The endpoint the request was on.
951 * @req: The request completed.
952 *
953 * Called on completion of any requests the driver itself
954 * submitted that need cleaning up.
955 */
956static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
957 struct usb_request *req)
958{
959 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
960 struct s3c_hsotg *hsotg = hs_ep->parent;
961
962 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
963
964 s3c_hsotg_ep_free_request(ep, req);
965}
966
967/**
968 * ep_from_windex - convert control wIndex value to endpoint
969 * @hsotg: The driver state.
970 * @windex: The control request wIndex field (in host order).
971 *
972 * Convert the given wIndex into a pointer to an driver endpoint
973 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200974 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100975static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
976 u32 windex)
977{
978 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
979 int dir = (windex & USB_DIR_IN) ? 1 : 0;
980 int idx = windex & 0x7F;
981
982 if (windex >= 0x100)
983 return NULL;
984
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200985 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100986 return NULL;
987
988 if (idx && ep->dir_in != dir)
989 return NULL;
990
991 return ep;
992}
993
994/**
995 * s3c_hsotg_send_reply - send reply to control request
996 * @hsotg: The device state
997 * @ep: Endpoint 0
998 * @buff: Buffer for request
999 * @length: Length of reply.
1000 *
1001 * Create a request and queue it on the given endpoint. This is useful as
1002 * an internal method of sending replies to certain control requests, etc.
1003 */
1004static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
1005 struct s3c_hsotg_ep *ep,
1006 void *buff,
1007 int length)
1008{
1009 struct usb_request *req;
1010 int ret;
1011
1012 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1013
1014 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
1015 hsotg->ep0_reply = req;
1016 if (!req) {
1017 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1018 return -ENOMEM;
1019 }
1020
1021 req->buf = hsotg->ep0_buff;
1022 req->length = length;
1023 req->zero = 1; /* always do zero-length final transfer */
1024 req->complete = s3c_hsotg_complete_oursetup;
1025
1026 if (length)
1027 memcpy(req->buf, buff, length);
1028 else
1029 ep->sent_zlp = 1;
1030
1031 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
1032 if (ret) {
1033 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1034 return ret;
1035 }
1036
1037 return 0;
1038}
1039
1040/**
1041 * s3c_hsotg_process_req_status - process request GET_STATUS
1042 * @hsotg: The device state
1043 * @ctrl: USB control request
1044 */
1045static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
1046 struct usb_ctrlrequest *ctrl)
1047{
1048 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1049 struct s3c_hsotg_ep *ep;
1050 __le16 reply;
1051 int ret;
1052
1053 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1054
1055 if (!ep0->dir_in) {
1056 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1057 return -EINVAL;
1058 }
1059
1060 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1061 case USB_RECIP_DEVICE:
1062 reply = cpu_to_le16(0); /* bit 0 => self powered,
1063 * bit 1 => remote wakeup */
1064 break;
1065
1066 case USB_RECIP_INTERFACE:
1067 /* currently, the data result should be zero */
1068 reply = cpu_to_le16(0);
1069 break;
1070
1071 case USB_RECIP_ENDPOINT:
1072 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1073 if (!ep)
1074 return -ENOENT;
1075
1076 reply = cpu_to_le16(ep->halted ? 1 : 0);
1077 break;
1078
1079 default:
1080 return 0;
1081 }
1082
1083 if (le16_to_cpu(ctrl->wLength) != 2)
1084 return -EINVAL;
1085
1086 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1087 if (ret) {
1088 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1089 return ret;
1090 }
1091
1092 return 1;
1093}
1094
1095static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1096
1097/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001098 * get_ep_head - return the first request on the endpoint
1099 * @hs_ep: The controller endpoint to get
1100 *
1101 * Get the first request on the endpoint.
1102 */
1103static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1104{
1105 if (list_empty(&hs_ep->queue))
1106 return NULL;
1107
1108 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1109}
1110
1111/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001112 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
1113 * @hsotg: The device state
1114 * @ctrl: USB control request
1115 */
1116static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
1117 struct usb_ctrlrequest *ctrl)
1118{
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001119 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001120 struct s3c_hsotg_req *hs_req;
1121 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1123 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001124 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001125
1126 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1127 __func__, set ? "SET" : "CLEAR");
1128
1129 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
1130 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1131 if (!ep) {
1132 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
1133 __func__, le16_to_cpu(ctrl->wIndex));
1134 return -ENOENT;
1135 }
1136
1137 switch (le16_to_cpu(ctrl->wValue)) {
1138 case USB_ENDPOINT_HALT:
1139 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001140
1141 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1142 if (ret) {
1143 dev_err(hsotg->dev,
1144 "%s: failed to send reply\n", __func__);
1145 return ret;
1146 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001147
1148 if (!set) {
1149 /*
1150 * If we have request in progress,
1151 * then complete it
1152 */
1153 if (ep->req) {
1154 hs_req = ep->req;
1155 ep->req = NULL;
1156 list_del_init(&hs_req->queue);
1157 hs_req->req.complete(&ep->ep,
1158 &hs_req->req);
1159 }
1160
1161 /* If we have pending request, then start it */
1162 restart = !list_empty(&ep->queue);
1163 if (restart) {
1164 hs_req = get_ep_head(ep);
1165 s3c_hsotg_start_req(hsotg, ep,
1166 hs_req, false);
1167 }
1168 }
1169
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001170 break;
1171
1172 default:
1173 return -ENOENT;
1174 }
1175 } else
1176 return -ENOENT; /* currently only deal with endpoint */
1177
1178 return 1;
1179}
1180
1181/**
1182 * s3c_hsotg_process_control - process a control request
1183 * @hsotg: The device state
1184 * @ctrl: The control request received
1185 *
1186 * The controller has received the SETUP phase of a control request, and
1187 * needs to work out what to do next (and whether to pass it on to the
1188 * gadget driver).
1189 */
1190static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1191 struct usb_ctrlrequest *ctrl)
1192{
1193 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1194 int ret = 0;
1195 u32 dcfg;
1196
1197 ep0->sent_zlp = 0;
1198
1199 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1200 ctrl->bRequest, ctrl->bRequestType,
1201 ctrl->wValue, ctrl->wLength);
1202
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001203 /*
1204 * record the direction of the request, for later use when enquing
1205 * packets onto EP0.
1206 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001207
1208 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1209 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1210
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001211 /*
1212 * if we've no data with this request, then the last part of the
1213 * transaction is going to implicitly be IN.
1214 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001215 if (ctrl->wLength == 0)
1216 ep0->dir_in = 1;
1217
1218 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1219 switch (ctrl->bRequest) {
1220 case USB_REQ_SET_ADDRESS:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001221 dcfg = readl(hsotg->regs + DCFG);
1222 dcfg &= ~DCFG_DevAddr_MASK;
1223 dcfg |= ctrl->wValue << DCFG_DevAddr_SHIFT;
1224 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001225
1226 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1227
1228 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1229 return;
1230
1231 case USB_REQ_GET_STATUS:
1232 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1233 break;
1234
1235 case USB_REQ_CLEAR_FEATURE:
1236 case USB_REQ_SET_FEATURE:
1237 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1238 break;
1239 }
1240 }
1241
1242 /* as a fallback, try delivering it to the driver to deal with */
1243
1244 if (ret == 0 && hsotg->driver) {
1245 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
1246 if (ret < 0)
1247 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1248 }
1249
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001250 /*
1251 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001252 * so respond with a STALL for the status stage to indicate failure.
1253 */
1254
1255 if (ret < 0) {
1256 u32 reg;
1257 u32 ctrl;
1258
1259 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001260 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001261
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001262 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001263 * DxEPCTL_Stall will be cleared by EP once it has
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001264 * taken effect, so no need to clear later.
1265 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001266
1267 ctrl = readl(hsotg->regs + reg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001268 ctrl |= DxEPCTL_Stall;
1269 ctrl |= DxEPCTL_CNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001270 writel(ctrl, hsotg->regs + reg);
1271
1272 dev_dbg(hsotg->dev,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001273 "written DxEPCTL=0x%08x to %08x (DxEPCTL=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001274 ctrl, reg, readl(hsotg->regs + reg));
1275
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001276 /*
1277 * don't believe we need to anything more to get the EP
1278 * to reply with a STALL packet
1279 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001280 }
1281}
1282
1283static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
1284
1285/**
1286 * s3c_hsotg_complete_setup - completion of a setup transfer
1287 * @ep: The endpoint the request was on.
1288 * @req: The request completed.
1289 *
1290 * Called on completion of any requests the driver itself submitted for
1291 * EP0 setup packets
1292 */
1293static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1294 struct usb_request *req)
1295{
1296 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1297 struct s3c_hsotg *hsotg = hs_ep->parent;
1298
1299 if (req->status < 0) {
1300 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1301 return;
1302 }
1303
1304 if (req->actual == 0)
1305 s3c_hsotg_enqueue_setup(hsotg);
1306 else
1307 s3c_hsotg_process_control(hsotg, req->buf);
1308}
1309
1310/**
1311 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1312 * @hsotg: The device state.
1313 *
1314 * Enqueue a request on EP0 if necessary to received any SETUP packets
1315 * received from the host.
1316 */
1317static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1318{
1319 struct usb_request *req = hsotg->ctrl_req;
1320 struct s3c_hsotg_req *hs_req = our_req(req);
1321 int ret;
1322
1323 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1324
1325 req->zero = 0;
1326 req->length = 8;
1327 req->buf = hsotg->ctrl_buff;
1328 req->complete = s3c_hsotg_complete_setup;
1329
1330 if (!list_empty(&hs_req->queue)) {
1331 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1332 return;
1333 }
1334
1335 hsotg->eps[0].dir_in = 0;
1336
1337 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1338 if (ret < 0) {
1339 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001340 /*
1341 * Don't think there's much we can do other than watch the
1342 * driver fail.
1343 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001344 }
1345}
1346
1347/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001348 * s3c_hsotg_complete_request - complete a request given to us
1349 * @hsotg: The device state.
1350 * @hs_ep: The endpoint the request was on.
1351 * @hs_req: The request to complete.
1352 * @result: The result code (0 => Ok, otherwise errno)
1353 *
1354 * The given request has finished, so call the necessary completion
1355 * if it has one and then look to see if we can start a new request
1356 * on the endpoint.
1357 *
1358 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001359 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001360static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1361 struct s3c_hsotg_ep *hs_ep,
1362 struct s3c_hsotg_req *hs_req,
1363 int result)
1364{
1365 bool restart;
1366
1367 if (!hs_req) {
1368 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1369 return;
1370 }
1371
1372 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1373 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1374
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001375 /*
1376 * only replace the status if we've not already set an error
1377 * from a previous transaction
1378 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001379
1380 if (hs_req->req.status == -EINPROGRESS)
1381 hs_req->req.status = result;
1382
1383 hs_ep->req = NULL;
1384 list_del_init(&hs_req->queue);
1385
1386 if (using_dma(hsotg))
1387 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1388
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001389 /*
1390 * call the complete request with the locks off, just in case the
1391 * request tries to queue more work for this endpoint.
1392 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001393
1394 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001395 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001396 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001397 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001398 }
1399
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001400 /*
1401 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001402 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001403 * so be careful when doing this.
1404 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001405
1406 if (!hs_ep->req && result >= 0) {
1407 restart = !list_empty(&hs_ep->queue);
1408 if (restart) {
1409 hs_req = get_ep_head(hs_ep);
1410 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1411 }
1412 }
1413}
1414
1415/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001416 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1417 * @hsotg: The device state.
1418 * @ep_idx: The endpoint index for the data
1419 * @size: The size of data in the fifo, in bytes
1420 *
1421 * The FIFO status shows there is data to read from the FIFO for a given
1422 * endpoint, so sort out whether we need to read the data into a request
1423 * that has been made for that endpoint.
1424 */
1425static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1426{
1427 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1428 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001429 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001430 int to_read;
1431 int max_req;
1432 int read_ptr;
1433
Lukasz Majewski22258f42012-06-14 10:02:24 +02001434
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001435 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001436 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001437 int ptr;
1438
1439 dev_warn(hsotg->dev,
1440 "%s: FIFO %d bytes on ep%d but no req (DxEPCTl=0x%08x)\n",
1441 __func__, size, ep_idx, epctl);
1442
1443 /* dump the data from the FIFO, we've nothing we can do */
1444 for (ptr = 0; ptr < size; ptr += 4)
1445 (void)readl(fifo);
1446
1447 return;
1448 }
1449
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001450 to_read = size;
1451 read_ptr = hs_req->req.actual;
1452 max_req = hs_req->req.length - read_ptr;
1453
Ben Dooksa33e7132010-07-19 09:40:49 +01001454 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1455 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1456
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001457 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001458 /*
1459 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001460 * to deal with in this request.
1461 */
1462
1463 /* currently we don't deal this */
1464 WARN_ON_ONCE(1);
1465 }
1466
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001467 hs_ep->total_data += to_read;
1468 hs_req->req.actual += to_read;
1469 to_read = DIV_ROUND_UP(to_read, 4);
1470
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001471 /*
1472 * note, we might over-write the buffer end by 3 bytes depending on
1473 * alignment of the data.
1474 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001475 readsl(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001476}
1477
1478/**
1479 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1480 * @hsotg: The device instance
1481 * @req: The request currently on this endpoint
1482 *
1483 * Generate a zero-length IN packet request for terminating a SETUP
1484 * transaction.
1485 *
1486 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001487 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001488 * the TxFIFO.
1489 */
1490static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1491 struct s3c_hsotg_req *req)
1492{
1493 u32 ctrl;
1494
1495 if (!req) {
1496 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1497 return;
1498 }
1499
1500 if (req->req.length == 0) {
1501 hsotg->eps[0].sent_zlp = 1;
1502 s3c_hsotg_enqueue_setup(hsotg);
1503 return;
1504 }
1505
1506 hsotg->eps[0].dir_in = 1;
1507 hsotg->eps[0].sent_zlp = 1;
1508
1509 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1510
1511 /* issue a zero-sized packet to terminate this */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001512 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
1513 DxEPTSIZ_XferSize(0), hsotg->regs + DIEPTSIZ(0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001514
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001515 ctrl = readl(hsotg->regs + DIEPCTL0);
1516 ctrl |= DxEPCTL_CNAK; /* clear NAK set by core */
1517 ctrl |= DxEPCTL_EPEna; /* ensure ep enabled */
1518 ctrl |= DxEPCTL_USBActEp;
1519 writel(ctrl, hsotg->regs + DIEPCTL0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001520}
1521
1522/**
1523 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1524 * @hsotg: The device instance
1525 * @epnum: The endpoint received from
1526 * @was_setup: Set if processing a SetupDone event.
1527 *
1528 * The RXFIFO has delivered an OutDone event, which means that the data
1529 * transfer for an OUT endpoint has been completed, either by a short
1530 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001531 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001532static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1533 int epnum, bool was_setup)
1534{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001535 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001536 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1537 struct s3c_hsotg_req *hs_req = hs_ep->req;
1538 struct usb_request *req = &hs_req->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001539 unsigned size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001540 int result = 0;
1541
1542 if (!hs_req) {
1543 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1544 return;
1545 }
1546
1547 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001548 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001549
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001550 /*
1551 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001552 * is left in the endpoint size register and then working it
1553 * out from the amount we loaded for the transfer.
1554 *
1555 * We need to do this as DMA pointers are always 32bit aligned
1556 * so may overshoot/undershoot the transfer.
1557 */
1558
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001559 size_done = hs_ep->size_loaded - size_left;
1560 size_done += hs_ep->last_load;
1561
1562 req->actual = size_done;
1563 }
1564
Ben Dooksa33e7132010-07-19 09:40:49 +01001565 /* if there is more request to do, schedule new transfer */
1566 if (req->actual < req->length && size_left == 0) {
1567 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1568 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001569 } else if (epnum == 0) {
1570 /*
1571 * After was_setup = 1 =>
1572 * set CNAK for non Setup requests
1573 */
1574 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001575 }
1576
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001577 if (req->actual < req->length && req->short_not_ok) {
1578 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1579 __func__, req->actual, req->length);
1580
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001581 /*
1582 * todo - what should we return here? there's no one else
1583 * even bothering to check the status.
1584 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001585 }
1586
1587 if (epnum == 0) {
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001588 /*
1589 * Condition req->complete != s3c_hsotg_complete_setup says:
1590 * send ZLP when we have an asynchronous request from gadget
1591 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001592 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1593 s3c_hsotg_send_zlp(hsotg, hs_req);
1594 }
1595
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001596 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001597}
1598
1599/**
1600 * s3c_hsotg_read_frameno - read current frame number
1601 * @hsotg: The device instance
1602 *
1603 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001604 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001605static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1606{
1607 u32 dsts;
1608
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001609 dsts = readl(hsotg->regs + DSTS);
1610 dsts &= DSTS_SOFFN_MASK;
1611 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001612
1613 return dsts;
1614}
1615
1616/**
1617 * s3c_hsotg_handle_rx - RX FIFO has data
1618 * @hsotg: The device instance
1619 *
1620 * The IRQ handler has detected that the RX FIFO has some data in it
1621 * that requires processing, so find out what is in there and do the
1622 * appropriate read.
1623 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001624 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001625 * chunks, so if you have x packets received on an endpoint you'll get x
1626 * FIFO events delivered, each with a packet's worth of data in it.
1627 *
1628 * When using DMA, we should not be processing events from the RXFIFO
1629 * as the actual data should be sent to the memory directly and we turn
1630 * on the completion interrupts to get notifications of transfer completion.
1631 */
Mark Brown0978f8c2010-01-18 13:18:35 +00001632static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001633{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001634 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001635 u32 epnum, status, size;
1636
1637 WARN_ON(using_dma(hsotg));
1638
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001639 epnum = grxstsr & GRXSTS_EPNum_MASK;
1640 status = grxstsr & GRXSTS_PktSts_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001641
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001642 size = grxstsr & GRXSTS_ByteCnt_MASK;
1643 size >>= GRXSTS_ByteCnt_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001644
1645 if (1)
1646 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1647 __func__, grxstsr, size, epnum);
1648
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001649#define __status(x) ((x) >> GRXSTS_PktSts_SHIFT)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001650
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001651 switch (status >> GRXSTS_PktSts_SHIFT) {
1652 case __status(GRXSTS_PktSts_GlobalOutNAK):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001653 dev_dbg(hsotg->dev, "GlobalOutNAK\n");
1654 break;
1655
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001656 case __status(GRXSTS_PktSts_OutDone):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001657 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1658 s3c_hsotg_read_frameno(hsotg));
1659
1660 if (!using_dma(hsotg))
1661 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1662 break;
1663
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001664 case __status(GRXSTS_PktSts_SetupDone):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001665 dev_dbg(hsotg->dev,
1666 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1667 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001668 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001669
1670 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1671 break;
1672
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001673 case __status(GRXSTS_PktSts_OutRX):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001674 s3c_hsotg_rx_data(hsotg, epnum, size);
1675 break;
1676
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001677 case __status(GRXSTS_PktSts_SetupRX):
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001678 dev_dbg(hsotg->dev,
1679 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1680 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001681 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001682
1683 s3c_hsotg_rx_data(hsotg, epnum, size);
1684 break;
1685
1686 default:
1687 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1688 __func__, grxstsr);
1689
1690 s3c_hsotg_dump(hsotg);
1691 break;
1692 }
1693}
1694
1695/**
1696 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1697 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001698 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001699static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1700{
1701 switch (mps) {
1702 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001703 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001704 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001705 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001706 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001707 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001708 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001709 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001710 }
1711
1712 /* bad max packet size, warn and return invalid result */
1713 WARN_ON(1);
1714 return (u32)-1;
1715}
1716
1717/**
1718 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1719 * @hsotg: The driver state.
1720 * @ep: The index number of the endpoint
1721 * @mps: The maximum packet size in bytes
1722 *
1723 * Configure the maximum packet size for the given endpoint, updating
1724 * the hardware control registers to reflect this.
1725 */
1726static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1727 unsigned int ep, unsigned int mps)
1728{
1729 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1730 void __iomem *regs = hsotg->regs;
1731 u32 mpsval;
1732 u32 reg;
1733
1734 if (ep == 0) {
1735 /* EP0 is a special case */
1736 mpsval = s3c_hsotg_ep0_mps(mps);
1737 if (mpsval > 3)
1738 goto bad_mps;
1739 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001740 if (mps >= DxEPCTL_MPS_LIMIT+1)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001741 goto bad_mps;
1742
1743 mpsval = mps;
1744 }
1745
1746 hs_ep->ep.maxpacket = mps;
1747
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001748 /*
1749 * update both the in and out endpoint controldir_ registers, even
1750 * if one of the directions may not be in use.
1751 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001752
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001753 reg = readl(regs + DIEPCTL(ep));
1754 reg &= ~DxEPCTL_MPS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001755 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001756 writel(reg, regs + DIEPCTL(ep));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001757
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001758 if (ep) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001759 reg = readl(regs + DOEPCTL(ep));
1760 reg &= ~DxEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001761 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001762 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001763 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001764
1765 return;
1766
1767bad_mps:
1768 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1769}
1770
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001771/**
1772 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1773 * @hsotg: The driver state
1774 * @idx: The index for the endpoint (0..15)
1775 */
1776static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1777{
1778 int timeout;
1779 int val;
1780
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001781 writel(GRSTCTL_TxFNum(idx) | GRSTCTL_TxFFlsh,
1782 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001783
1784 /* wait until the fifo is flushed */
1785 timeout = 100;
1786
1787 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001788 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001789
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001790 if ((val & (GRSTCTL_TxFFlsh)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001791 break;
1792
1793 if (--timeout == 0) {
1794 dev_err(hsotg->dev,
1795 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1796 __func__, val);
1797 }
1798
1799 udelay(1);
1800 }
1801}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001802
1803/**
1804 * s3c_hsotg_trytx - check to see if anything needs transmitting
1805 * @hsotg: The driver state
1806 * @hs_ep: The driver endpoint to check.
1807 *
1808 * Check to see if there is a request that has data to send, and if so
1809 * make an attempt to write data into the FIFO.
1810 */
1811static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1812 struct s3c_hsotg_ep *hs_ep)
1813{
1814 struct s3c_hsotg_req *hs_req = hs_ep->req;
1815
1816 if (!hs_ep->dir_in || !hs_req)
1817 return 0;
1818
1819 if (hs_req->req.actual < hs_req->req.length) {
1820 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1821 hs_ep->index);
1822 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1823 }
1824
1825 return 0;
1826}
1827
1828/**
1829 * s3c_hsotg_complete_in - complete IN transfer
1830 * @hsotg: The device state.
1831 * @hs_ep: The endpoint that has just completed.
1832 *
1833 * An IN transfer has been completed, update the transfer's state and then
1834 * call the relevant completion routines.
1835 */
1836static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1837 struct s3c_hsotg_ep *hs_ep)
1838{
1839 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001840 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001841 int size_left, size_done;
1842
1843 if (!hs_req) {
1844 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1845 return;
1846 }
1847
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001848 /* Finish ZLP handling for IN EP0 transactions */
1849 if (hsotg->eps[0].sent_zlp) {
1850 dev_dbg(hsotg->dev, "zlp packet received\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001851 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001852 return;
1853 }
1854
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001855 /*
1856 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001857 * in the endpoint size register and then working it out from
1858 * the amount we loaded for the transfer.
1859 *
1860 * We do this even for DMA, as the transfer may have incremented
1861 * past the end of the buffer (DMA transfers are always 32bit
1862 * aligned).
1863 */
1864
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001865 size_left = DxEPTSIZ_XferSize_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001866
1867 size_done = hs_ep->size_loaded - size_left;
1868 size_done += hs_ep->last_load;
1869
1870 if (hs_req->req.actual != size_done)
1871 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1872 __func__, hs_req->req.actual, size_done);
1873
1874 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001875 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1876 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001877
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001878 /*
1879 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1880 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1881 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1882 * inform the host that no more data is available.
1883 * The state of req.zero member is checked to be sure that the value to
1884 * send is smaller than wValue expected from host.
1885 * Check req.length to NOT send another ZLP when the current one is
1886 * under completion (the one for which this completion has been called).
1887 */
1888 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1889 hs_req->req.length == hs_req->req.actual &&
1890 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1891
1892 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1893 s3c_hsotg_send_zlp(hsotg, hs_req);
1894
1895 return;
1896 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001897
1898 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1899 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1900 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1901 } else
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001902 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001903}
1904
1905/**
1906 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1907 * @hsotg: The driver state
1908 * @idx: The index for the endpoint (0..15)
1909 * @dir_in: Set if this is an IN endpoint
1910 *
1911 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001912 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001913static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1914 int dir_in)
1915{
1916 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001917 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1918 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1919 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001920 u32 ints;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001921
1922 ints = readl(hsotg->regs + epint_reg);
1923
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001924 /* Clear endpoint interrupts */
1925 writel(ints, hsotg->regs + epint_reg);
1926
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001927 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1928 __func__, idx, dir_in ? "in" : "out", ints);
1929
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001930 if (ints & DxEPINT_XferCompl) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001931 dev_dbg(hsotg->dev,
1932 "%s: XferCompl: DxEPCTL=0x%08x, DxEPTSIZ=%08x\n",
1933 __func__, readl(hsotg->regs + epctl_reg),
1934 readl(hsotg->regs + epsiz_reg));
1935
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001936 /*
1937 * we get OutDone from the FIFO, so we only need to look
1938 * at completing IN requests here
1939 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001940 if (dir_in) {
1941 s3c_hsotg_complete_in(hsotg, hs_ep);
1942
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001943 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001944 s3c_hsotg_enqueue_setup(hsotg);
1945 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001946 /*
1947 * We're using DMA, we need to fire an OutDone here
1948 * as we ignore the RXFIFO.
1949 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950
1951 s3c_hsotg_handle_outdone(hsotg, idx, false);
1952 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001953 }
1954
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001955 if (ints & DxEPINT_EPDisbld) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001956 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001957
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001958 if (dir_in) {
1959 int epctl = readl(hsotg->regs + epctl_reg);
1960
1961 s3c_hsotg_txfifo_flush(hsotg, idx);
1962
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001963 if ((epctl & DxEPCTL_Stall) &&
1964 (epctl & DxEPCTL_EPType_Bulk)) {
1965 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001966
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001967 dctl |= DCTL_CGNPInNAK;
1968 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001969 }
1970 }
1971 }
1972
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001973 if (ints & DxEPINT_AHBErr)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001974 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001975
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001976 if (ints & DxEPINT_Setup) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001977 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1978
1979 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001980 /*
1981 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001982 * setup packet. In non-DMA mode we'd get this
1983 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001984 * the setup here.
1985 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001986
1987 if (dir_in)
1988 WARN_ON_ONCE(1);
1989 else
1990 s3c_hsotg_handle_outdone(hsotg, 0, true);
1991 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001992 }
1993
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001994 if (ints & DxEPINT_Back2BackSetup)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001995 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996
1997 if (dir_in) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001998 /* not sure if this is important, but we'll clear it anyway */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001999 if (ints & DIEPMSK_INTknTXFEmpMsk) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002000 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2001 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002002 }
2003
2004 /* this probably means something bad is happening */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002005 if (ints & DIEPMSK_INTknEPMisMsk) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002006 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2007 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002008 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002009
2010 /* FIFO has space or is empty (see GAHBCFG) */
2011 if (hsotg->dedicated_fifos &&
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002012 ints & DIEPMSK_TxFIFOEmpty) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002013 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2014 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002015 if (!using_dma(hsotg))
2016 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002017 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002018 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002019}
2020
2021/**
2022 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
2023 * @hsotg: The device state.
2024 *
2025 * Handle updating the device settings after the enumeration phase has
2026 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002027 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002028static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
2029{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002030 u32 dsts = readl(hsotg->regs + DSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002031 int ep0_mps = 0, ep_mps;
2032
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002033 /*
2034 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002035 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002036 * we connected at.
2037 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002038
2039 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2040
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002041 /*
2042 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002043 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002044 * not advertise a 64byte MPS on EP0.
2045 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002046
2047 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002048 switch (dsts & DSTS_EnumSpd_MASK) {
2049 case DSTS_EnumSpd_FS:
2050 case DSTS_EnumSpd_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002051 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002052 ep0_mps = EP0_MPS_LIMIT;
2053 ep_mps = 64;
2054 break;
2055
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002056 case DSTS_EnumSpd_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002057 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002058 ep0_mps = EP0_MPS_LIMIT;
2059 ep_mps = 512;
2060 break;
2061
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002062 case DSTS_EnumSpd_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002063 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002064 /*
2065 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002066 * moment, and the documentation seems to imply that it isn't
2067 * supported by the PHYs on some of the devices.
2068 */
2069 break;
2070 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002071 dev_info(hsotg->dev, "new device is %s\n",
2072 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002073
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002074 /*
2075 * we should now know the maximum packet size for an
2076 * endpoint, so set the endpoints to a default value.
2077 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002078
2079 if (ep0_mps) {
2080 int i;
2081 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002082 for (i = 1; i < hsotg->num_of_eps; i++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002083 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
2084 }
2085
2086 /* ensure after enumeration our EP0 is active */
2087
2088 s3c_hsotg_enqueue_setup(hsotg);
2089
2090 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002091 readl(hsotg->regs + DIEPCTL0),
2092 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002093}
2094
2095/**
2096 * kill_all_requests - remove all requests from the endpoint's queue
2097 * @hsotg: The device state.
2098 * @ep: The endpoint the requests may be on.
2099 * @result: The result code to use.
2100 * @force: Force removal of any current requests
2101 *
2102 * Go through the requests on the given endpoint and mark them
2103 * completed with the given result code.
2104 */
2105static void kill_all_requests(struct s3c_hsotg *hsotg,
2106 struct s3c_hsotg_ep *ep,
2107 int result, bool force)
2108{
2109 struct s3c_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002110
2111 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002112 /*
2113 * currently, we can't do much about an already
2114 * running request on an in endpoint
2115 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002116
2117 if (ep->req == req && ep->dir_in && !force)
2118 continue;
2119
2120 s3c_hsotg_complete_request(hsotg, ep, req,
2121 result);
2122 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002123}
2124
2125#define call_gadget(_hs, _entry) \
2126 if ((_hs)->gadget.speed != USB_SPEED_UNKNOWN && \
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002127 (_hs)->driver && (_hs)->driver->_entry) { \
2128 spin_unlock(&_hs->lock); \
2129 (_hs)->driver->_entry(&(_hs)->gadget); \
2130 spin_lock(&_hs->lock); \
2131 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002132
2133/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002134 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002135 * @hsotg: The device state.
2136 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002137 * The device has been disconnected. Remove all current
2138 * transactions and signal the gadget driver that this
2139 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002140 */
Lukasz Majewski5e891342012-05-04 14:17:07 +02002141static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002142{
2143 unsigned ep;
2144
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002145 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002146 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2147
2148 call_gadget(hsotg, disconnect);
2149}
2150
2151/**
2152 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2153 * @hsotg: The device state:
2154 * @periodic: True if this is a periodic FIFO interrupt
2155 */
2156static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2157{
2158 struct s3c_hsotg_ep *ep;
2159 int epno, ret;
2160
2161 /* look through for any more data to transmit */
2162
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002163 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002164 ep = &hsotg->eps[epno];
2165
2166 if (!ep->dir_in)
2167 continue;
2168
2169 if ((periodic && !ep->periodic) ||
2170 (!periodic && ep->periodic))
2171 continue;
2172
2173 ret = s3c_hsotg_trytx(hsotg, ep);
2174 if (ret < 0)
2175 break;
2176 }
2177}
2178
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002179/* IRQ flags which will trigger a retry around the IRQ loop */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002180#define IRQ_RETRY_MASK (GINTSTS_NPTxFEmp | \
2181 GINTSTS_PTxFEmp | \
2182 GINTSTS_RxFLvl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002183
2184/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002185 * s3c_hsotg_corereset - issue softreset to the core
2186 * @hsotg: The device state
2187 *
2188 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002189 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002190static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2191{
2192 int timeout;
2193 u32 grstctl;
2194
2195 dev_dbg(hsotg->dev, "resetting core\n");
2196
2197 /* issue soft reset */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002198 writel(GRSTCTL_CSftRst, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002199
Du, Changbin2868fea2012-07-24 08:19:25 +08002200 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002201 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002202 grstctl = readl(hsotg->regs + GRSTCTL);
2203 } while ((grstctl & GRSTCTL_CSftRst) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002204
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002205 if (grstctl & GRSTCTL_CSftRst) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002206 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2207 return -EINVAL;
2208 }
2209
Du, Changbin2868fea2012-07-24 08:19:25 +08002210 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002211
2212 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002213 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002214
2215 if (timeout-- < 0) {
2216 dev_info(hsotg->dev,
2217 "%s: reset failed, GRSTCTL=%08x\n",
2218 __func__, grstctl);
2219 return -ETIMEDOUT;
2220 }
2221
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002222 if (!(grstctl & GRSTCTL_AHBIdle))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002223 continue;
2224
2225 break; /* reset done */
2226 }
2227
2228 dev_dbg(hsotg->dev, "reset successful\n");
2229 return 0;
2230}
2231
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002232/**
2233 * s3c_hsotg_core_init - issue softreset to the core
2234 * @hsotg: The device state
2235 *
2236 * Issue a soft reset to the core, and await the core finishing it.
2237 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002238static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2239{
2240 s3c_hsotg_corereset(hsotg);
2241
2242 /*
2243 * we must now enable ep0 ready for host detection and then
2244 * set configuration.
2245 */
2246
2247 /* set the PLL on, remove the HNP/SRP and set the PHY */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002248 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) |
2249 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002250
2251 s3c_hsotg_init_fifo(hsotg);
2252
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002253 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002254
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002255 writel(1 << 18 | DCFG_DevSpd_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002256
2257 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002258 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002259
2260 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002261 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002262
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002263 writel(GINTSTS_ErlySusp | GINTSTS_SessReqInt |
2264 GINTSTS_GOUTNakEff | GINTSTS_GINNakEff |
2265 GINTSTS_ConIDStsChng | GINTSTS_USBRst |
2266 GINTSTS_EnumDone | GINTSTS_OTGInt |
2267 GINTSTS_USBSusp | GINTSTS_WkUpInt,
2268 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002269
2270 if (using_dma(hsotg))
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002271 writel(GAHBCFG_GlblIntrEn | GAHBCFG_DMAEn |
2272 GAHBCFG_HBstLen_Incr4,
2273 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002274 else
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002275 writel(GAHBCFG_GlblIntrEn, hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002276
2277 /*
2278 * Enabling INTknTXFEmpMsk here seems to be a big mistake, we end
2279 * up being flooded with interrupts if the host is polling the
2280 * endpoint to try and read data.
2281 */
2282
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002283 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TxFIFOEmpty : 0) |
2284 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk |
2285 DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2286 DIEPMSK_INTknEPMisMsk,
2287 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002288
2289 /*
2290 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2291 * DMA mode we may need this.
2292 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002293 writel((using_dma(hsotg) ? (DIEPMSK_XferComplMsk |
2294 DIEPMSK_TimeOUTMsk) : 0) |
2295 DOEPMSK_EPDisbldMsk | DOEPMSK_AHBErrMsk |
2296 DOEPMSK_SetupMsk,
2297 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002298
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002299 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002300
2301 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002302 readl(hsotg->regs + DIEPCTL0),
2303 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002304
2305 /* enable in and out endpoint interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002306 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPInt | GINTSTS_IEPInt);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002307
2308 /*
2309 * Enable the RXFIFO when in slave mode, as this is how we collect
2310 * the data. In DMA mode, we get events from the FIFO but also
2311 * things we cannot process, so do not use it.
2312 */
2313 if (!using_dma(hsotg))
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002314 s3c_hsotg_en_gsint(hsotg, GINTSTS_RxFLvl);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002315
2316 /* Enable interrupts for EP0 in and out */
2317 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2318 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2319
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002320 __orr32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002321 udelay(10); /* see openiboot */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002322 __bic32(hsotg->regs + DCTL, DCTL_PWROnPrgDone);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002323
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002324 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002325
2326 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002327 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002328 * writing to the EPCTL register..
2329 */
2330
2331 /* set to read 1 8byte packet */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002332 writel(DxEPTSIZ_MC(1) | DxEPTSIZ_PktCnt(1) |
2333 DxEPTSIZ_XferSize(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002334
2335 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002336 DxEPCTL_CNAK | DxEPCTL_EPEna |
2337 DxEPCTL_USBActEp,
2338 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002339
2340 /* enable, but don't activate EP0in */
2341 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002342 DxEPCTL_USBActEp, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002343
2344 s3c_hsotg_enqueue_setup(hsotg);
2345
2346 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002347 readl(hsotg->regs + DIEPCTL0),
2348 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002349
2350 /* clear global NAKs */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002351 writel(DCTL_CGOUTNak | DCTL_CGNPInNAK,
2352 hsotg->regs + DCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002353
2354 /* must be at-least 3ms to allow bus to see disconnect */
2355 mdelay(3);
2356
2357 /* remove the soft-disconnect and let's go */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002358 __bic32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002359}
2360
2361/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362 * s3c_hsotg_irq - handle device interrupt
2363 * @irq: The IRQ number triggered
2364 * @pw: The pw value when registered the handler.
2365 */
2366static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2367{
2368 struct s3c_hsotg *hsotg = pw;
2369 int retry_count = 8;
2370 u32 gintsts;
2371 u32 gintmsk;
2372
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002373 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002374irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002375 gintsts = readl(hsotg->regs + GINTSTS);
2376 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002377
2378 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2379 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2380
2381 gintsts &= gintmsk;
2382
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002383 if (gintsts & GINTSTS_OTGInt) {
2384 u32 otgint = readl(hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002385
2386 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2387
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002388 writel(otgint, hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002389 }
2390
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002391 if (gintsts & GINTSTS_SessReqInt) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002392 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002393 writel(GINTSTS_SessReqInt, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002394 }
2395
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002396 if (gintsts & GINTSTS_EnumDone) {
2397 writel(GINTSTS_EnumDone, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002398
2399 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002400 }
2401
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002402 if (gintsts & GINTSTS_ConIDStsChng) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002403 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002404 readl(hsotg->regs + DSTS),
2405 readl(hsotg->regs + GOTGCTL));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002406
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002407 writel(GINTSTS_ConIDStsChng, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002408 }
2409
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002410 if (gintsts & (GINTSTS_OEPInt | GINTSTS_IEPInt)) {
2411 u32 daint = readl(hsotg->regs + DAINT);
2412 u32 daint_out = daint >> DAINT_OutEP_SHIFT;
2413 u32 daint_in = daint & ~(daint_out << DAINT_OutEP_SHIFT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002414 int ep;
2415
2416 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2417
2418 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2419 if (daint_out & 1)
2420 s3c_hsotg_epint(hsotg, ep, 0);
2421 }
2422
2423 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2424 if (daint_in & 1)
2425 s3c_hsotg_epint(hsotg, ep, 1);
2426 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002427 }
2428
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002429 if (gintsts & GINTSTS_USBRst) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002430
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002431 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002432
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002433 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2434 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002435 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002436
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002437 writel(GINTSTS_USBRst, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002438
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002439 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002440 if (time_after(jiffies, hsotg->last_rst +
2441 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002442
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002443 kill_all_requests(hsotg, &hsotg->eps[0],
2444 -ECONNRESET, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002445
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002446 s3c_hsotg_core_init(hsotg);
2447 hsotg->last_rst = jiffies;
2448 }
2449 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002450 }
2451
2452 /* check both FIFOs */
2453
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002454 if (gintsts & GINTSTS_NPTxFEmp) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002455 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2456
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002457 /*
2458 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002459 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002460 * it needs re-enabling
2461 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002462
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002463 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002464 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002465 }
2466
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002467 if (gintsts & GINTSTS_PTxFEmp) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002468 dev_dbg(hsotg->dev, "PTxFEmp\n");
2469
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002470 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002471
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002472 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTxFEmp);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002473 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002474 }
2475
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002476 if (gintsts & GINTSTS_RxFLvl) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002477 /*
2478 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002479 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002480 * set.
2481 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002482
2483 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002484 }
2485
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002486 if (gintsts & GINTSTS_ModeMis) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002488 writel(GINTSTS_ModeMis, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002489 }
2490
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002491 if (gintsts & GINTSTS_USBSusp) {
2492 dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
2493 writel(GINTSTS_USBSusp, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002494
2495 call_gadget(hsotg, suspend);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002496 s3c_hsotg_disconnect(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002497 }
2498
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002499 if (gintsts & GINTSTS_WkUpInt) {
2500 dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
2501 writel(GINTSTS_WkUpInt, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002502
2503 call_gadget(hsotg, resume);
2504 }
2505
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002506 if (gintsts & GINTSTS_ErlySusp) {
2507 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
2508 writel(GINTSTS_ErlySusp, hsotg->regs + GINTSTS);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002509
2510 s3c_hsotg_disconnect(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002511 }
2512
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002513 /*
2514 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002515 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002516 * the occurrence.
2517 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002518
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002519 if (gintsts & GINTSTS_GOUTNakEff) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2521
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002522 writel(DCTL_CGOUTNak, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002523
2524 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002525 }
2526
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002527 if (gintsts & GINTSTS_GINNakEff) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002528 dev_info(hsotg->dev, "GINNakEff triggered\n");
2529
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002530 writel(DCTL_CGNPInNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002531
2532 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002533 }
2534
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002535 /*
2536 * if we've had fifo events, we should try and go around the
2537 * loop again to see if there's any point in returning yet.
2538 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002539
2540 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2541 goto irq_retry;
2542
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002543 spin_unlock(&hsotg->lock);
2544
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002545 return IRQ_HANDLED;
2546}
2547
2548/**
2549 * s3c_hsotg_ep_enable - enable the given endpoint
2550 * @ep: The USB endpint to configure
2551 * @desc: The USB endpoint descriptor to configure with.
2552 *
2553 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002554 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002555static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2556 const struct usb_endpoint_descriptor *desc)
2557{
2558 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2559 struct s3c_hsotg *hsotg = hs_ep->parent;
2560 unsigned long flags;
2561 int index = hs_ep->index;
2562 u32 epctrl_reg;
2563 u32 epctrl;
2564 u32 mps;
2565 int dir_in;
Julia Lawall19c190f2010-03-29 17:36:44 +02002566 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002567
2568 dev_dbg(hsotg->dev,
2569 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2570 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2571 desc->wMaxPacketSize, desc->bInterval);
2572
2573 /* not to be called for EP0 */
2574 WARN_ON(index == 0);
2575
2576 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2577 if (dir_in != hs_ep->dir_in) {
2578 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2579 return -EINVAL;
2580 }
2581
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002582 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002583
2584 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2585
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002586 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002587 epctrl = readl(hsotg->regs + epctrl_reg);
2588
2589 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2590 __func__, epctrl, epctrl_reg);
2591
Lukasz Majewski22258f42012-06-14 10:02:24 +02002592 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002593
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002594 epctrl &= ~(DxEPCTL_EPType_MASK | DxEPCTL_MPS_MASK);
2595 epctrl |= DxEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002596
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002597 /*
2598 * mark the endpoint as active, otherwise the core may ignore
2599 * transactions entirely for this endpoint
2600 */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002601 epctrl |= DxEPCTL_USBActEp;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002602
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002603 /*
2604 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002605 * do something with data that we've yet got a request to process
2606 * since the RXFIFO will take data for an endpoint even if the
2607 * size register hasn't been set.
2608 */
2609
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002610 epctrl |= DxEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002611
2612 /* update the endpoint state */
2613 hs_ep->ep.maxpacket = mps;
2614
2615 /* default, set to non-periodic */
2616 hs_ep->periodic = 0;
2617
2618 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2619 case USB_ENDPOINT_XFER_ISOC:
2620 dev_err(hsotg->dev, "no current ISOC support\n");
Julia Lawall19c190f2010-03-29 17:36:44 +02002621 ret = -EINVAL;
2622 goto out;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002623
2624 case USB_ENDPOINT_XFER_BULK:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002625 epctrl |= DxEPCTL_EPType_Bulk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002626 break;
2627
2628 case USB_ENDPOINT_XFER_INT:
2629 if (dir_in) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002630 /*
2631 * Allocate our TxFNum by simply using the index
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002632 * of the endpoint for the moment. We could do
2633 * something better if the host indicates how
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002634 * many FIFOs we are expecting to use.
2635 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002636
2637 hs_ep->periodic = 1;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002638 epctrl |= DxEPCTL_TxFNum(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002639 }
2640
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002641 epctrl |= DxEPCTL_EPType_Intterupt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002642 break;
2643
2644 case USB_ENDPOINT_XFER_CONTROL:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002645 epctrl |= DxEPCTL_EPType_Control;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002646 break;
2647 }
2648
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002649 /*
2650 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002651 * a unique tx-fifo even if it is non-periodic.
2652 */
2653 if (dir_in && hsotg->dedicated_fifos)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002654 epctrl |= DxEPCTL_TxFNum(index);
Ben Dooks10aebc72010-07-19 09:40:44 +01002655
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002656 /* for non control endpoints, set PID to D0 */
2657 if (index)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002658 epctrl |= DxEPCTL_SetD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002659
2660 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2661 __func__, epctrl);
2662
2663 writel(epctrl, hsotg->regs + epctrl_reg);
2664 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2665 __func__, readl(hsotg->regs + epctrl_reg));
2666
2667 /* enable the endpoint interrupt */
2668 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2669
Julia Lawall19c190f2010-03-29 17:36:44 +02002670out:
Lukasz Majewski22258f42012-06-14 10:02:24 +02002671 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002672 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002673}
2674
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002675/**
2676 * s3c_hsotg_ep_disable - disable given endpoint
2677 * @ep: The endpoint to disable.
2678 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002679static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2680{
2681 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2682 struct s3c_hsotg *hsotg = hs_ep->parent;
2683 int dir_in = hs_ep->dir_in;
2684 int index = hs_ep->index;
2685 unsigned long flags;
2686 u32 epctrl_reg;
2687 u32 ctrl;
2688
2689 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2690
2691 if (ep == &hsotg->eps[0].ep) {
2692 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2693 return -EINVAL;
2694 }
2695
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002696 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002697
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002698 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002699 /* terminate all requests with shutdown */
2700 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2701
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002702
2703 ctrl = readl(hsotg->regs + epctrl_reg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002704 ctrl &= ~DxEPCTL_EPEna;
2705 ctrl &= ~DxEPCTL_USBActEp;
2706 ctrl |= DxEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002707
2708 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2709 writel(ctrl, hsotg->regs + epctrl_reg);
2710
2711 /* disable endpoint interrupts */
2712 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2713
Lukasz Majewski22258f42012-06-14 10:02:24 +02002714 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002715 return 0;
2716}
2717
2718/**
2719 * on_list - check request is on the given endpoint
2720 * @ep: The endpoint to check.
2721 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002722 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002723static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2724{
2725 struct s3c_hsotg_req *req, *treq;
2726
2727 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2728 if (req == test)
2729 return true;
2730 }
2731
2732 return false;
2733}
2734
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002735/**
2736 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2737 * @ep: The endpoint to dequeue.
2738 * @req: The request to be removed from a queue.
2739 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002740static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2741{
2742 struct s3c_hsotg_req *hs_req = our_req(req);
2743 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2744 struct s3c_hsotg *hs = hs_ep->parent;
2745 unsigned long flags;
2746
2747 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2748
Lukasz Majewski22258f42012-06-14 10:02:24 +02002749 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002750
2751 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002752 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002753 return -EINVAL;
2754 }
2755
2756 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002757 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002758
2759 return 0;
2760}
2761
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002762/**
2763 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2764 * @ep: The endpoint to set halt.
2765 * @value: Set or unset the halt.
2766 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002767static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2768{
2769 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2770 struct s3c_hsotg *hs = hs_ep->parent;
2771 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002772 u32 epreg;
2773 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002774 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002775
2776 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2777
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002778 /* write both IN and OUT control registers */
2779
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002780 epreg = DIEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002781 epctl = readl(hs->regs + epreg);
2782
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002783 if (value) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002784 epctl |= DxEPCTL_Stall + DxEPCTL_SNAK;
2785 if (epctl & DxEPCTL_EPEna)
2786 epctl |= DxEPCTL_EPDis;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002787 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002788 epctl &= ~DxEPCTL_Stall;
2789 xfertype = epctl & DxEPCTL_EPType_MASK;
2790 if (xfertype == DxEPCTL_EPType_Bulk ||
2791 xfertype == DxEPCTL_EPType_Intterupt)
2792 epctl |= DxEPCTL_SetD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002793 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002794
2795 writel(epctl, hs->regs + epreg);
2796
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002797 epreg = DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002798 epctl = readl(hs->regs + epreg);
2799
2800 if (value)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002801 epctl |= DxEPCTL_Stall;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002802 else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002803 epctl &= ~DxEPCTL_Stall;
2804 xfertype = epctl & DxEPCTL_EPType_MASK;
2805 if (xfertype == DxEPCTL_EPType_Bulk ||
2806 xfertype == DxEPCTL_EPType_Intterupt)
2807 epctl |= DxEPCTL_SetD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002808 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002809
2810 writel(epctl, hs->regs + epreg);
2811
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002812 return 0;
2813}
2814
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002815/**
2816 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2817 * @ep: The endpoint to set halt.
2818 * @value: Set or unset the halt.
2819 */
2820static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2821{
2822 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2823 struct s3c_hsotg *hs = hs_ep->parent;
2824 unsigned long flags = 0;
2825 int ret = 0;
2826
2827 spin_lock_irqsave(&hs->lock, flags);
2828 ret = s3c_hsotg_ep_sethalt(ep, value);
2829 spin_unlock_irqrestore(&hs->lock, flags);
2830
2831 return ret;
2832}
2833
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002834static struct usb_ep_ops s3c_hsotg_ep_ops = {
2835 .enable = s3c_hsotg_ep_enable,
2836 .disable = s3c_hsotg_ep_disable,
2837 .alloc_request = s3c_hsotg_ep_alloc_request,
2838 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002839 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002840 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002841 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002842 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002843};
2844
2845/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002846 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002847 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002848 *
2849 * A wrapper for platform code responsible for controlling
2850 * low-level USB code
2851 */
2852static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2853{
2854 struct platform_device *pdev = to_platform_device(hsotg->dev);
2855
2856 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
2857 if (hsotg->plat->phy_init)
2858 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2859}
2860
2861/**
2862 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002863 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002864 *
2865 * A wrapper for platform code responsible for controlling
2866 * low-level USB code
2867 */
2868static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2869{
2870 struct platform_device *pdev = to_platform_device(hsotg->dev);
2871
2872 if (hsotg->plat->phy_exit)
2873 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2874}
2875
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002876/**
2877 * s3c_hsotg_init - initalize the usb core
2878 * @hsotg: The driver state
2879 */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002880static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2881{
2882 /* unmask subset of endpoint interrupts */
2883
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002884 writel(DIEPMSK_TimeOUTMsk | DIEPMSK_AHBErrMsk |
2885 DIEPMSK_EPDisbldMsk | DIEPMSK_XferComplMsk,
2886 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002887
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002888 writel(DOEPMSK_SetupMsk | DOEPMSK_AHBErrMsk |
2889 DOEPMSK_EPDisbldMsk | DOEPMSK_XferComplMsk,
2890 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002891
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002892 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002893
2894 /* Be in disconnected state until gadget is registered */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002895 __orr32(hsotg->regs + DCTL, DCTL_SftDiscon);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002896
2897 if (0) {
2898 /* post global nak until we're ready */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002899 writel(DCTL_SGNPInNAK | DCTL_SGOUTNak,
2900 hsotg->regs + DCTL);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002901 }
2902
2903 /* setup fifos */
2904
2905 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002906 readl(hsotg->regs + GRXFSIZ),
2907 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002908
2909 s3c_hsotg_init_fifo(hsotg);
2910
2911 /* set the PLL on, remove the HNP/SRP and set the PHY */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002912 writel(GUSBCFG_PHYIf16 | GUSBCFG_TOutCal(7) | (0x5 << 10),
2913 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002914
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002915 writel(using_dma(hsotg) ? GAHBCFG_DMAEn : 0x0,
2916 hsotg->regs + GAHBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002917}
2918
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002919/**
2920 * s3c_hsotg_udc_start - prepare the udc for work
2921 * @gadget: The usb gadget state
2922 * @driver: The usb gadget driver
2923 *
2924 * Perform initialization to prepare udc device and driver
2925 * to work.
2926 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002927static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2928 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002929{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02002930 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002931 int ret;
2932
2933 if (!hsotg) {
2934 printk(KERN_ERR "%s: called with no device\n", __func__);
2935 return -ENODEV;
2936 }
2937
2938 if (!driver) {
2939 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2940 return -EINVAL;
2941 }
2942
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002943 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002944 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002945
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002946 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002947 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2948 return -EINVAL;
2949 }
2950
2951 WARN_ON(hsotg->driver);
2952
2953 driver->driver.bus = NULL;
2954 hsotg->driver = driver;
2955 hsotg->gadget.dev.driver = &driver->driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03002956 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002957 hsotg->gadget.dev.dma_mask = hsotg->dev->dma_mask;
2958 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2959
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002960 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
2961 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002962 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002963 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002964 goto err;
2965 }
2966
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002967 hsotg->last_rst = jiffies;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002968 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
2969 return 0;
2970
2971err:
2972 hsotg->driver = NULL;
2973 hsotg->gadget.dev.driver = NULL;
2974 return ret;
2975}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002976
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002977/**
2978 * s3c_hsotg_udc_stop - stop the udc
2979 * @gadget: The usb gadget state
2980 * @driver: The usb gadget driver
2981 *
2982 * Stop udc hw block and stay tunned for future transmissions
2983 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002984static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
2985 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002986{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02002987 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002988 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002989 int ep;
2990
2991 if (!hsotg)
2992 return -ENODEV;
2993
2994 if (!driver || driver != hsotg->driver || !driver->unbind)
2995 return -EINVAL;
2996
2997 /* all endpoints should be shutdown */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002998 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002999 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3000
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003001 spin_lock_irqsave(&hsotg->lock, flags);
3002
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003003 s3c_hsotg_phy_disable(hsotg);
3004 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003005
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003006 hsotg->driver = NULL;
3007 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003008 hsotg->gadget.dev.driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003009
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003010 spin_unlock_irqrestore(&hsotg->lock, flags);
3011
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003012 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
Lukasz Majewskia188b682012-06-22 09:29:56 +02003029/**
3030 * s3c_hsotg_pullup - connect/disconnect the USB PHY
3031 * @gadget: The usb gadget state
3032 * @is_on: Current state of the USB PHY
3033 *
3034 * Connect/Disconnect the USB PHY pullup
3035 */
3036static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
3037{
3038 struct s3c_hsotg *hsotg = to_hsotg(gadget);
3039 unsigned long flags = 0;
3040
3041 dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on);
3042
3043 spin_lock_irqsave(&hsotg->lock, flags);
3044 if (is_on) {
3045 s3c_hsotg_phy_enable(hsotg);
3046 s3c_hsotg_core_init(hsotg);
3047 } else {
3048 s3c_hsotg_disconnect(hsotg);
3049 s3c_hsotg_phy_disable(hsotg);
3050 }
3051
3052 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3053 spin_unlock_irqrestore(&hsotg->lock, flags);
3054
3055 return 0;
3056}
3057
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003058static struct usb_gadget_ops s3c_hsotg_gadget_ops = {
3059 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003060 .udc_start = s3c_hsotg_udc_start,
3061 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02003062 .pullup = s3c_hsotg_pullup,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003063};
3064
3065/**
3066 * s3c_hsotg_initep - initialise a single endpoint
3067 * @hsotg: The device state.
3068 * @hs_ep: The endpoint to be initialised.
3069 * @epnum: The endpoint number
3070 *
3071 * Initialise the given endpoint (as part of the probe and device state
3072 * creation) to give to the gadget driver. Setup the endpoint name, any
3073 * direction information and other state that may be required.
3074 */
3075static void __devinit s3c_hsotg_initep(struct s3c_hsotg *hsotg,
3076 struct s3c_hsotg_ep *hs_ep,
3077 int epnum)
3078{
3079 u32 ptxfifo;
3080 char *dir;
3081
3082 if (epnum == 0)
3083 dir = "";
3084 else if ((epnum % 2) == 0) {
3085 dir = "out";
3086 } else {
3087 dir = "in";
3088 hs_ep->dir_in = 1;
3089 }
3090
3091 hs_ep->index = epnum;
3092
3093 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3094
3095 INIT_LIST_HEAD(&hs_ep->queue);
3096 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3097
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003098 /* add to the list of endpoints known by the gadget driver */
3099 if (epnum)
3100 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3101
3102 hs_ep->parent = hsotg;
3103 hs_ep->ep.name = hs_ep->name;
3104 hs_ep->ep.maxpacket = epnum ? 512 : EP0_MPS_LIMIT;
3105 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3106
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003107 /*
3108 * Read the FIFO size for the Periodic TX FIFO, even if we're
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003109 * an OUT endpoint, we may as well do this if in future the
3110 * code is changed to make each endpoint's direction changeable.
3111 */
3112
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003113 ptxfifo = readl(hsotg->regs + DPTXFSIZn(epnum));
3114 hs_ep->fifo_size = DPTXFSIZn_DPTxFSize_GET(ptxfifo) * 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003116 /*
3117 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003118 * to be something valid.
3119 */
3120
3121 if (using_dma(hsotg)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003122 u32 next = DxEPCTL_NextEp((epnum + 1) % 15);
3123 writel(next, hsotg->regs + DIEPCTL(epnum));
3124 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003125 }
3126}
3127
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003128/**
3129 * s3c_hsotg_hw_cfg - read HW configuration registers
3130 * @param: The device state
3131 *
3132 * Read the USB core HW configuration registers
3133 */
3134static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003135{
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003136 u32 cfg2, cfg4;
Ben Dooks10aebc72010-07-19 09:40:44 +01003137 /* check hardware configuration */
3138
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003139 cfg2 = readl(hsotg->regs + 0x48);
3140 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3141
3142 dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
3143
Ben Dooks10aebc72010-07-19 09:40:44 +01003144 cfg4 = readl(hsotg->regs + 0x50);
3145 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3146
3147 dev_info(hsotg->dev, "%s fifos\n",
3148 hsotg->dedicated_fifos ? "dedicated" : "shared");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003149}
3150
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003151/**
3152 * s3c_hsotg_dump - dump state of the udc
3153 * @param: The device state
3154 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003155static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
3156{
Mark Brown83a01802011-06-01 17:16:15 +01003157#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003158 struct device *dev = hsotg->dev;
3159 void __iomem *regs = hsotg->regs;
3160 u32 val;
3161 int idx;
3162
3163 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003164 readl(regs + DCFG), readl(regs + DCTL),
3165 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003166
3167 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003168 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003169
3170 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003171 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003172
3173 /* show periodic fifo settings */
3174
3175 for (idx = 1; idx <= 15; idx++) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003176 val = readl(regs + DPTXFSIZn(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003177 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003178 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3179 val & DPTXFSIZn_DPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003180 }
3181
3182 for (idx = 0; idx < 15; idx++) {
3183 dev_info(dev,
3184 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003185 readl(regs + DIEPCTL(idx)),
3186 readl(regs + DIEPTSIZ(idx)),
3187 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003188
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003189 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003190 dev_info(dev,
3191 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003192 idx, readl(regs + DOEPCTL(idx)),
3193 readl(regs + DOEPTSIZ(idx)),
3194 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003195
3196 }
3197
3198 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003199 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003200#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003201}
3202
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003203/**
3204 * state_show - debugfs: show overall driver and device state.
3205 * @seq: The seq file to write to.
3206 * @v: Unused parameter.
3207 *
3208 * This debugfs entry shows the overall state of the hardware and
3209 * some general information about each of the endpoints available
3210 * to the system.
3211 */
3212static int state_show(struct seq_file *seq, void *v)
3213{
3214 struct s3c_hsotg *hsotg = seq->private;
3215 void __iomem *regs = hsotg->regs;
3216 int idx;
3217
3218 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003219 readl(regs + DCFG),
3220 readl(regs + DCTL),
3221 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003222
3223 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003224 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003225
3226 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003227 readl(regs + GINTMSK),
3228 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003229
3230 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003231 readl(regs + DAINTMSK),
3232 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003233
3234 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003235 readl(regs + GNPTXSTS),
3236 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003237
3238 seq_printf(seq, "\nEndpoint status:\n");
3239
3240 for (idx = 0; idx < 15; idx++) {
3241 u32 in, out;
3242
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003243 in = readl(regs + DIEPCTL(idx));
3244 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003245
3246 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3247 idx, in, out);
3248
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003249 in = readl(regs + DIEPTSIZ(idx));
3250 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003251
3252 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3253 in, out);
3254
3255 seq_printf(seq, "\n");
3256 }
3257
3258 return 0;
3259}
3260
3261static int state_open(struct inode *inode, struct file *file)
3262{
3263 return single_open(file, state_show, inode->i_private);
3264}
3265
3266static const struct file_operations state_fops = {
3267 .owner = THIS_MODULE,
3268 .open = state_open,
3269 .read = seq_read,
3270 .llseek = seq_lseek,
3271 .release = single_release,
3272};
3273
3274/**
3275 * fifo_show - debugfs: show the fifo information
3276 * @seq: The seq_file to write data to.
3277 * @v: Unused parameter.
3278 *
3279 * Show the FIFO information for the overall fifo and all the
3280 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003281 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003282static int fifo_show(struct seq_file *seq, void *v)
3283{
3284 struct s3c_hsotg *hsotg = seq->private;
3285 void __iomem *regs = hsotg->regs;
3286 u32 val;
3287 int idx;
3288
3289 seq_printf(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003290 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003291
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003292 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003293 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003294 val >> GNPTXFSIZ_NPTxFDep_SHIFT,
3295 val & GNPTXFSIZ_NPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003296
3297 seq_printf(seq, "\nPeriodic TXFIFOs:\n");
3298
3299 for (idx = 1; idx <= 15; idx++) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003300 val = readl(regs + DPTXFSIZn(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003301
3302 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003303 val >> DPTXFSIZn_DPTxFSize_SHIFT,
3304 val & DPTXFSIZn_DPTxFStAddr_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003305 }
3306
3307 return 0;
3308}
3309
3310static int fifo_open(struct inode *inode, struct file *file)
3311{
3312 return single_open(file, fifo_show, inode->i_private);
3313}
3314
3315static const struct file_operations fifo_fops = {
3316 .owner = THIS_MODULE,
3317 .open = fifo_open,
3318 .read = seq_read,
3319 .llseek = seq_lseek,
3320 .release = single_release,
3321};
3322
3323
3324static const char *decode_direction(int is_in)
3325{
3326 return is_in ? "in" : "out";
3327}
3328
3329/**
3330 * ep_show - debugfs: show the state of an endpoint.
3331 * @seq: The seq_file to write data to.
3332 * @v: Unused parameter.
3333 *
3334 * This debugfs entry shows the state of the given endpoint (one is
3335 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003336 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003337static int ep_show(struct seq_file *seq, void *v)
3338{
3339 struct s3c_hsotg_ep *ep = seq->private;
3340 struct s3c_hsotg *hsotg = ep->parent;
3341 struct s3c_hsotg_req *req;
3342 void __iomem *regs = hsotg->regs;
3343 int index = ep->index;
3344 int show_limit = 15;
3345 unsigned long flags;
3346
3347 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3348 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3349
3350 /* first show the register state */
3351
3352 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003353 readl(regs + DIEPCTL(index)),
3354 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003355
3356 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003357 readl(regs + DIEPDMA(index)),
3358 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003359
3360 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003361 readl(regs + DIEPINT(index)),
3362 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003363
3364 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003365 readl(regs + DIEPTSIZ(index)),
3366 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003367
3368 seq_printf(seq, "\n");
3369 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3370 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3371
3372 seq_printf(seq, "request list (%p,%p):\n",
3373 ep->queue.next, ep->queue.prev);
3374
Lukasz Majewski22258f42012-06-14 10:02:24 +02003375 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003376
3377 list_for_each_entry(req, &ep->queue, queue) {
3378 if (--show_limit < 0) {
3379 seq_printf(seq, "not showing more requests...\n");
3380 break;
3381 }
3382
3383 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3384 req == ep->req ? '*' : ' ',
3385 req, req->req.length, req->req.buf);
3386 seq_printf(seq, "%d done, res %d\n",
3387 req->req.actual, req->req.status);
3388 }
3389
Lukasz Majewski22258f42012-06-14 10:02:24 +02003390 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003391
3392 return 0;
3393}
3394
3395static int ep_open(struct inode *inode, struct file *file)
3396{
3397 return single_open(file, ep_show, inode->i_private);
3398}
3399
3400static const struct file_operations ep_fops = {
3401 .owner = THIS_MODULE,
3402 .open = ep_open,
3403 .read = seq_read,
3404 .llseek = seq_lseek,
3405 .release = single_release,
3406};
3407
3408/**
3409 * s3c_hsotg_create_debug - create debugfs directory and files
3410 * @hsotg: The driver state
3411 *
3412 * Create the debugfs files to allow the user to get information
3413 * about the state of the system. The directory name is created
3414 * with the same name as the device itself, in case we end up
3415 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003416 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003417static void __devinit s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
3418{
3419 struct dentry *root;
3420 unsigned epidx;
3421
3422 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3423 hsotg->debug_root = root;
3424 if (IS_ERR(root)) {
3425 dev_err(hsotg->dev, "cannot create debug root\n");
3426 return;
3427 }
3428
3429 /* create general state file */
3430
3431 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3432 hsotg, &state_fops);
3433
3434 if (IS_ERR(hsotg->debug_file))
3435 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3436
3437 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3438 hsotg, &fifo_fops);
3439
3440 if (IS_ERR(hsotg->debug_fifo))
3441 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3442
3443 /* create one file for each endpoint */
3444
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003445 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003446 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3447
3448 ep->debugfs = debugfs_create_file(ep->name, 0444,
3449 root, ep, &ep_fops);
3450
3451 if (IS_ERR(ep->debugfs))
3452 dev_err(hsotg->dev, "failed to create %s debug file\n",
3453 ep->name);
3454 }
3455}
3456
3457/**
3458 * s3c_hsotg_delete_debug - cleanup debugfs entries
3459 * @hsotg: The driver state
3460 *
3461 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003462 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003463static void __devexit s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
3464{
3465 unsigned epidx;
3466
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003467 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003468 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3469 debugfs_remove(ep->debugfs);
3470 }
3471
3472 debugfs_remove(hsotg->debug_file);
3473 debugfs_remove(hsotg->debug_fifo);
3474 debugfs_remove(hsotg->debug_root);
3475}
3476
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003477/**
Lukasz Majewskif026a522012-05-04 14:17:13 +02003478 * s3c_hsotg_release - release callback for hsotg device
3479 * @dev: Device to for which release is called
3480 */
3481static void s3c_hsotg_release(struct device *dev)
3482{
3483 struct s3c_hsotg *hsotg = dev_get_drvdata(dev);
3484
3485 kfree(hsotg);
3486}
3487
3488/**
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003489 * s3c_hsotg_probe - probe function for hsotg driver
3490 * @pdev: The platform information for the driver
3491 */
Lukasz Majewskif026a522012-05-04 14:17:13 +02003492
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003493static int __devinit s3c_hsotg_probe(struct platform_device *pdev)
3494{
3495 struct s3c_hsotg_plat *plat = pdev->dev.platform_data;
3496 struct device *dev = &pdev->dev;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003497 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003498 struct s3c_hsotg *hsotg;
3499 struct resource *res;
3500 int epnum;
3501 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003502 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003503
Lukasz Majewski41188782012-05-04 14:17:01 +02003504 plat = pdev->dev.platform_data;
3505 if (!plat) {
3506 dev_err(&pdev->dev, "no platform data defined\n");
3507 return -EINVAL;
3508 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003509
Sachin Kamat338edab2012-05-18 14:33:46 +05303510 hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003511 if (!hsotg) {
3512 dev_err(dev, "cannot get memory\n");
3513 return -ENOMEM;
3514 }
3515
3516 hsotg->dev = dev;
3517 hsotg->plat = plat;
3518
Sachin Kamat84749c62012-09-03 16:15:18 +05303519 hsotg->clk = devm_clk_get(&pdev->dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003520 if (IS_ERR(hsotg->clk)) {
3521 dev_err(dev, "cannot get otg clock\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303522 return PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003523 }
3524
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003525 platform_set_drvdata(pdev, hsotg);
3526
3527 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003528
Sachin Kamat338edab2012-05-18 14:33:46 +05303529 hsotg->regs = devm_request_and_ioremap(&pdev->dev, res);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003530 if (!hsotg->regs) {
3531 dev_err(dev, "cannot map registers\n");
3532 ret = -ENXIO;
Sachin Kamat338edab2012-05-18 14:33:46 +05303533 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003534 }
3535
3536 ret = platform_get_irq(pdev, 0);
3537 if (ret < 0) {
3538 dev_err(dev, "cannot find IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303539 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003540 }
3541
Lukasz Majewski22258f42012-06-14 10:02:24 +02003542 spin_lock_init(&hsotg->lock);
3543
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003544 hsotg->irq = ret;
3545
Sachin Kamat338edab2012-05-18 14:33:46 +05303546 ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
3547 dev_name(dev), hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003548 if (ret < 0) {
3549 dev_err(dev, "cannot claim IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303550 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003551 }
3552
3553 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3554
3555 device_initialize(&hsotg->gadget.dev);
3556
3557 dev_set_name(&hsotg->gadget.dev, "gadget");
3558
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003559 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003560 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3561 hsotg->gadget.name = dev_name(dev);
3562
3563 hsotg->gadget.dev.parent = dev;
3564 hsotg->gadget.dev.dma_mask = dev->dma_mask;
Lukasz Majewskif026a522012-05-04 14:17:13 +02003565 hsotg->gadget.dev.release = s3c_hsotg_release;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003566
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003567 /* reset the system */
3568
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003569 clk_prepare_enable(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003570
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003571 /* regulators */
3572
3573 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3574 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3575
3576 ret = regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
3577 hsotg->supplies);
3578 if (ret) {
3579 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303580 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003581 }
3582
3583 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3584 hsotg->supplies);
3585
3586 if (ret) {
3587 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3588 goto err_supplies;
3589 }
3590
Lukasz Majewski41188782012-05-04 14:17:01 +02003591 /* usb phy enable */
3592 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003593
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003594 s3c_hsotg_corereset(hsotg);
3595 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003596 s3c_hsotg_hw_cfg(hsotg);
3597
3598 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3599
3600 if (hsotg->num_of_eps == 0) {
3601 dev_err(dev, "wrong number of EPs (zero)\n");
3602 goto err_supplies;
3603 }
3604
3605 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3606 GFP_KERNEL);
3607 if (!eps) {
3608 dev_err(dev, "cannot get memory\n");
3609 goto err_supplies;
3610 }
3611
3612 hsotg->eps = eps;
3613
3614 /* setup endpoint information */
3615
3616 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3617 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3618
3619 /* allocate EP0 request */
3620
3621 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3622 GFP_KERNEL);
3623 if (!hsotg->ctrl_req) {
3624 dev_err(dev, "failed to allocate ctrl req\n");
3625 goto err_ep_mem;
3626 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003627
3628 /* initialise the endpoints now the core has been initialised */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003629 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003630 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3631
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003632 /* disable power and clock */
3633
3634 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3635 hsotg->supplies);
3636 if (ret) {
3637 dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
3638 goto err_ep_mem;
3639 }
3640
3641 s3c_hsotg_phy_disable(hsotg);
3642
3643 ret = device_add(&hsotg->gadget.dev);
3644 if (ret) {
3645 put_device(&hsotg->gadget.dev);
3646 goto err_ep_mem;
3647 }
3648
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003649 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3650 if (ret)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003651 goto err_ep_mem;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003652
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003653 s3c_hsotg_create_debug(hsotg);
3654
3655 s3c_hsotg_dump(hsotg);
3656
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003657 return 0;
3658
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003659err_ep_mem:
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003660 kfree(eps);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003661err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003662 s3c_hsotg_phy_disable(hsotg);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003663 regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003664
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003665err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003666 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05303667
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003668 return ret;
3669}
3670
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003671/**
3672 * s3c_hsotg_remove - remove function for hsotg driver
3673 * @pdev: The platform information for the driver
3674 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003675static int __devexit s3c_hsotg_remove(struct platform_device *pdev)
3676{
3677 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3678
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003679 usb_del_gadget_udc(&hsotg->gadget);
3680
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003681 s3c_hsotg_delete_debug(hsotg);
3682
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003683 if (hsotg->driver) {
3684 /* should have been done already by driver model core */
3685 usb_gadget_unregister_driver(hsotg->driver);
3686 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003687
Lukasz Majewski41188782012-05-04 14:17:01 +02003688 s3c_hsotg_phy_disable(hsotg);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003689 regulator_bulk_free(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
3690
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003691 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003692
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003693 device_unregister(&hsotg->gadget.dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003694 return 0;
3695}
3696
3697#if 1
3698#define s3c_hsotg_suspend NULL
3699#define s3c_hsotg_resume NULL
3700#endif
3701
3702static struct platform_driver s3c_hsotg_driver = {
3703 .driver = {
3704 .name = "s3c-hsotg",
3705 .owner = THIS_MODULE,
3706 },
3707 .probe = s3c_hsotg_probe,
3708 .remove = __devexit_p(s3c_hsotg_remove),
3709 .suspend = s3c_hsotg_suspend,
3710 .resume = s3c_hsotg_resume,
3711};
3712
Axel Lincc27c962011-11-27 20:16:27 +08003713module_platform_driver(s3c_hsotg_driver);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003714
3715MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3716MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3717MODULE_LICENSE("GPL");
3718MODULE_ALIAS("platform:s3c-hsotg");