blob: 73df48a6cc2249c9def1f6a72caf1d5cddd9a8a4 [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>
Tomasz Figac50f056c2013-06-25 17:38:23 +020032#include <linux/of_platform.h>
Matt Porter74084842013-12-19 09:23:06 -050033#include <linux/phy/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010034
35#include <linux/usb/ch9.h>
36#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053037#include <linux/usb/phy.h>
Lukasz Majewski126625e2012-05-09 13:16:53 +020038#include <linux/platform_data/s3c-hsotg.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010039
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070040#include "core.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041
42/* conversion functions */
43static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
44{
45 return container_of(req, struct s3c_hsotg_req, req);
46}
47
48static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
49{
50 return container_of(ep, struct s3c_hsotg_ep, ep);
51}
52
53static inline struct s3c_hsotg *to_hsotg(struct usb_gadget *gadget)
54{
55 return container_of(gadget, struct s3c_hsotg, gadget);
56}
57
58static inline void __orr32(void __iomem *ptr, u32 val)
59{
60 writel(readl(ptr) | val, ptr);
61}
62
63static inline void __bic32(void __iomem *ptr, u32 val)
64{
65 writel(readl(ptr) & ~val, ptr);
66}
67
68/* forward decleration of functions */
69static void s3c_hsotg_dump(struct s3c_hsotg *hsotg);
70
71/**
72 * using_dma - return the DMA status of the driver.
73 * @hsotg: The driver state.
74 *
75 * Return true if we're using DMA.
76 *
77 * Currently, we have the DMA support code worked into everywhere
78 * that needs it, but the AMBA DMA implementation in the hardware can
79 * only DMA from 32bit aligned addresses. This means that gadgets such
80 * as the CDC Ethernet cannot work as they often pass packets which are
81 * not 32bit aligned.
82 *
83 * Unfortunately the choice to use DMA or not is global to the controller
84 * and seems to be only settable when the controller is being put through
85 * a core reset. This means we either need to fix the gadgets to take
86 * account of DMA alignment, or add bounce buffers (yuerk).
87 *
88 * Until this issue is sorted out, we always return 'false'.
89 */
90static inline bool using_dma(struct s3c_hsotg *hsotg)
91{
92 return false; /* support is not complete */
93}
94
95/**
96 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
97 * @hsotg: The device state
98 * @ints: A bitmask of the interrupts to enable
99 */
100static void s3c_hsotg_en_gsint(struct s3c_hsotg *hsotg, u32 ints)
101{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200102 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100103 u32 new_gsintmsk;
104
105 new_gsintmsk = gsintmsk | ints;
106
107 if (new_gsintmsk != gsintmsk) {
108 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200109 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100110 }
111}
112
113/**
114 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
115 * @hsotg: The device state
116 * @ints: A bitmask of the interrupts to enable
117 */
118static void s3c_hsotg_disable_gsint(struct s3c_hsotg *hsotg, u32 ints)
119{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200120 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100121 u32 new_gsintmsk;
122
123 new_gsintmsk = gsintmsk & ~ints;
124
125 if (new_gsintmsk != gsintmsk)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200126 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100127}
128
129/**
130 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
131 * @hsotg: The device state
132 * @ep: The endpoint index
133 * @dir_in: True if direction is in.
134 * @en: The enable value, true to enable
135 *
136 * Set or clear the mask for an individual endpoint's interrupt
137 * request.
138 */
139static void s3c_hsotg_ctrl_epint(struct s3c_hsotg *hsotg,
140 unsigned int ep, unsigned int dir_in,
141 unsigned int en)
142{
143 unsigned long flags;
144 u32 bit = 1 << ep;
145 u32 daint;
146
147 if (!dir_in)
148 bit <<= 16;
149
150 local_irq_save(flags);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200151 daint = readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100152 if (en)
153 daint |= bit;
154 else
155 daint &= ~bit;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200156 writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100157 local_irq_restore(flags);
158}
159
160/**
161 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
162 * @hsotg: The device instance.
163 */
164static void s3c_hsotg_init_fifo(struct s3c_hsotg *hsotg)
165{
Ben Dooks0f002d22010-05-25 05:36:50 +0100166 unsigned int ep;
167 unsigned int addr;
168 unsigned int size;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100169 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100170 u32 val;
171
Ben Dooks6d091ee2010-07-19 09:40:40 +0100172 /* set FIFO sizes to 2048/1024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100173
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200174 writel(2048, hsotg->regs + GRXFSIZ);
Dinh Nguyen47a16852014-04-14 14:13:34 -0700175 writel((2048 << FIFOSIZE_STARTADDR_SHIFT) |
176 (1024 << FIFOSIZE_DEPTH_SHIFT), hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100177
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200178 /*
179 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100180 * block have overlapping default addresses. This also ensures
181 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200182 * known values.
183 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100184
185 /* start at the end of the GNPTXFSIZ, rounded up */
186 addr = 2048 + 1024;
187 size = 768;
188
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200189 /*
190 * currently we allocate TX FIFOs for all possible endpoints,
191 * and assume that they are all the same size.
192 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100193
Anton Tikhomirovf7a83fe2012-03-06 14:05:49 +0900194 for (ep = 1; ep <= 15; ep++) {
Ben Dooks0f002d22010-05-25 05:36:50 +0100195 val = addr;
Dinh Nguyen47a16852014-04-14 14:13:34 -0700196 val |= size << FIFOSIZE_DEPTH_SHIFT;
Ben Dooks0f002d22010-05-25 05:36:50 +0100197 addr += size;
198
Dinh Nguyen47a16852014-04-14 14:13:34 -0700199 writel(val, hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100200 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100201
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200202 /*
203 * according to p428 of the design guide, we need to ensure that
204 * all fifos are flushed before continuing
205 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100206
Dinh Nguyen47a16852014-04-14 14:13:34 -0700207 writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
208 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100209
210 /* wait until the fifos are both flushed */
211 timeout = 100;
212 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200213 val = readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100214
Dinh Nguyen47a16852014-04-14 14:13:34 -0700215 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100216 break;
217
218 if (--timeout == 0) {
219 dev_err(hsotg->dev,
220 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
221 __func__, val);
222 }
223
224 udelay(1);
225 }
226
227 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100228}
229
230/**
231 * @ep: USB endpoint to allocate request for.
232 * @flags: Allocation flags
233 *
234 * Allocate a new USB request structure appropriate for the specified endpoint
235 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000236static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
237 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100238{
239 struct s3c_hsotg_req *req;
240
241 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
242 if (!req)
243 return NULL;
244
245 INIT_LIST_HEAD(&req->queue);
246
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100247 return &req->req;
248}
249
250/**
251 * is_ep_periodic - return true if the endpoint is in periodic mode.
252 * @hs_ep: The endpoint to query.
253 *
254 * Returns true if the endpoint is in periodic mode, meaning it is being
255 * used for an Interrupt or ISO transfer.
256 */
257static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
258{
259 return hs_ep->periodic;
260}
261
262/**
263 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
264 * @hsotg: The device state.
265 * @hs_ep: The endpoint for the request
266 * @hs_req: The request being processed.
267 *
268 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
269 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200270 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100271static void s3c_hsotg_unmap_dma(struct s3c_hsotg *hsotg,
272 struct s3c_hsotg_ep *hs_ep,
273 struct s3c_hsotg_req *hs_req)
274{
275 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100276
277 /* ignore this if we're not moving any data */
278 if (hs_req->req.length == 0)
279 return;
280
Jingoo Han17d966a2013-05-11 21:14:00 +0900281 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100282}
283
284/**
285 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
286 * @hsotg: The controller state.
287 * @hs_ep: The endpoint we're going to write for.
288 * @hs_req: The request to write data for.
289 *
290 * This is called when the TxFIFO has some space in it to hold a new
291 * transmission and we have something to give it. The actual setup of
292 * the data size is done elsewhere, so all we have to do is to actually
293 * write the data.
294 *
295 * The return value is zero if there is more space (or nothing was done)
296 * otherwise -ENOSPC is returned if the FIFO space was used up.
297 *
298 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200299 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100300static int s3c_hsotg_write_fifo(struct s3c_hsotg *hsotg,
301 struct s3c_hsotg_ep *hs_ep,
302 struct s3c_hsotg_req *hs_req)
303{
304 bool periodic = is_ep_periodic(hs_ep);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200305 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100306 int buf_pos = hs_req->req.actual;
307 int to_write = hs_ep->size_loaded;
308 void *data;
309 int can_write;
310 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200311 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100312
313 to_write -= (buf_pos - hs_ep->last_load);
314
315 /* if there's nothing to write, get out early */
316 if (to_write == 0)
317 return 0;
318
Ben Dooks10aebc72010-07-19 09:40:44 +0100319 if (periodic && !hsotg->dedicated_fifos) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200320 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100321 int size_left;
322 int size_done;
323
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200324 /*
325 * work out how much data was loaded so we can calculate
326 * how much data is left in the fifo.
327 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100328
Dinh Nguyen47a16852014-04-14 14:13:34 -0700329 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100330
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200331 /*
332 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100333 * previous data has been completely sent.
334 */
335 if (hs_ep->fifo_load != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700336 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100337 return -ENOSPC;
338 }
339
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100340 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
341 __func__, size_left,
342 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
343
344 /* how much of the data has moved */
345 size_done = hs_ep->size_loaded - size_left;
346
347 /* how much data is left in the fifo */
348 can_write = hs_ep->fifo_load - size_done;
349 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
350 __func__, can_write);
351
352 can_write = hs_ep->fifo_size - can_write;
353 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
354 __func__, can_write);
355
356 if (can_write <= 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700357 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100358 return -ENOSPC;
359 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100360 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200361 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100362
363 can_write &= 0xffff;
364 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100365 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700366 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100367 dev_dbg(hsotg->dev,
368 "%s: no queue slots available (0x%08x)\n",
369 __func__, gnptxsts);
370
Dinh Nguyen47a16852014-04-14 14:13:34 -0700371 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100372 return -ENOSPC;
373 }
374
Dinh Nguyen47a16852014-04-14 14:13:34 -0700375 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100376 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100377 }
378
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200379 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
380
381 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
382 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100383
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200384 /*
385 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100386 * FIFO, requests of >512 cause the endpoint to get stuck with a
387 * fragment of the end of the transfer in it.
388 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200389 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100390 can_write = 512;
391
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200392 /*
393 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100394 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200395 * doing it.
396 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200397 if (to_write > max_transfer) {
398 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100399
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200400 /* it's needed only when we do not use dedicated fifos */
401 if (!hsotg->dedicated_fifos)
402 s3c_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700403 periodic ? GINTSTS_PTXFEMP :
404 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100405 }
406
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100407 /* see if we can write data */
408
409 if (to_write > can_write) {
410 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200411 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100412
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200413 /*
414 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100415 * exact number of packets.
416 *
417 * Note, we do not currently check to see if we can ever
418 * write a full packet or not to the FIFO.
419 */
420
421 if (pkt_round)
422 to_write -= pkt_round;
423
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200424 /*
425 * enable correct FIFO interrupt to alert us when there
426 * is more room left.
427 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100428
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200429 /* it's needed only when we do not use dedicated fifos */
430 if (!hsotg->dedicated_fifos)
431 s3c_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700432 periodic ? GINTSTS_PTXFEMP :
433 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100434 }
435
436 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
437 to_write, hs_req->req.length, can_write, buf_pos);
438
439 if (to_write <= 0)
440 return -ENOSPC;
441
442 hs_req->req.actual = buf_pos + to_write;
443 hs_ep->total_data += to_write;
444
445 if (periodic)
446 hs_ep->fifo_load += to_write;
447
448 to_write = DIV_ROUND_UP(to_write, 4);
449 data = hs_req->req.buf + buf_pos;
450
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500451 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100452
453 return (to_write >= can_write) ? -ENOSPC : 0;
454}
455
456/**
457 * get_ep_limit - get the maximum data legnth for this endpoint
458 * @hs_ep: The endpoint
459 *
460 * Return the maximum data that can be queued in one go on a given endpoint
461 * so that transfers that are too long can be split.
462 */
463static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
464{
465 int index = hs_ep->index;
466 unsigned maxsize;
467 unsigned maxpkt;
468
469 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700470 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
471 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100472 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100473 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900474 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700475 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900476 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100477 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100478 }
479
480 /* we made the constant loading easier above by using +1 */
481 maxpkt--;
482 maxsize--;
483
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200484 /*
485 * constrain by packet count if maxpkts*pktsize is greater
486 * than the length register size.
487 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100488
489 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
490 maxsize = maxpkt * hs_ep->ep.maxpacket;
491
492 return maxsize;
493}
494
495/**
496 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
497 * @hsotg: The controller state.
498 * @hs_ep: The endpoint to process a request for
499 * @hs_req: The request to start.
500 * @continuing: True if we are doing more for the current request.
501 *
502 * Start the given request running by setting the endpoint registers
503 * appropriately, and writing any data to the FIFOs.
504 */
505static void s3c_hsotg_start_req(struct s3c_hsotg *hsotg,
506 struct s3c_hsotg_ep *hs_ep,
507 struct s3c_hsotg_req *hs_req,
508 bool continuing)
509{
510 struct usb_request *ureq = &hs_req->req;
511 int index = hs_ep->index;
512 int dir_in = hs_ep->dir_in;
513 u32 epctrl_reg;
514 u32 epsize_reg;
515 u32 epsize;
516 u32 ctrl;
517 unsigned length;
518 unsigned packets;
519 unsigned maxreq;
520
521 if (index != 0) {
522 if (hs_ep->req && !continuing) {
523 dev_err(hsotg->dev, "%s: active request\n", __func__);
524 WARN_ON(1);
525 return;
526 } else if (hs_ep->req != hs_req && continuing) {
527 dev_err(hsotg->dev,
528 "%s: continue different req\n", __func__);
529 WARN_ON(1);
530 return;
531 }
532 }
533
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200534 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
535 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100536
537 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
538 __func__, readl(hsotg->regs + epctrl_reg), index,
539 hs_ep->dir_in ? "in" : "out");
540
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900541 /* If endpoint is stalled, we will restart request later */
542 ctrl = readl(hsotg->regs + epctrl_reg);
543
Dinh Nguyen47a16852014-04-14 14:13:34 -0700544 if (ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900545 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
546 return;
547 }
548
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100549 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200550 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
551 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100552 if (0)
553 dev_dbg(hsotg->dev,
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300554 "REQ buf %p len %d dma %pad noi=%d zp=%d snok=%d\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900555 ureq->buf, length, &ureq->dma,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100556 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
557
558 maxreq = get_ep_limit(hs_ep);
559 if (length > maxreq) {
560 int round = maxreq % hs_ep->ep.maxpacket;
561
562 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
563 __func__, length, maxreq, round);
564
565 /* round down to multiple of packets */
566 if (round)
567 maxreq -= round;
568
569 length = maxreq;
570 }
571
572 if (length)
573 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
574 else
575 packets = 1; /* send one packet if length is zero. */
576
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200577 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
578 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
579 return;
580 }
581
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100582 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200583 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700584 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200585 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700586 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100587 else
588 epsize = 0;
589
590 if (index != 0 && ureq->zero) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200591 /*
592 * test for the packets being exactly right for the
593 * transfer
594 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100595
596 if (length == (packets * hs_ep->ep.maxpacket))
597 packets++;
598 }
599
Dinh Nguyen47a16852014-04-14 14:13:34 -0700600 epsize |= DXEPTSIZ_PKTCNT(packets);
601 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100602
603 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
604 __func__, packets, length, ureq->length, epsize, epsize_reg);
605
606 /* store the request as the current one we're doing */
607 hs_ep->req = hs_req;
608
609 /* write size / packets */
610 writel(epsize, hsotg->regs + epsize_reg);
611
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900612 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100613 unsigned int dma_reg;
614
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200615 /*
616 * write DMA address to control register, buffer already
617 * synced by s3c_hsotg_ep_queue().
618 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100619
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200620 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100621 writel(ureq->dma, hsotg->regs + dma_reg);
622
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300623 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900624 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100625 }
626
Dinh Nguyen47a16852014-04-14 14:13:34 -0700627 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
628 ctrl |= DXEPCTL_USBACTEP;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200629
630 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
631
632 /* For Setup request do not clear NAK */
633 if (hsotg->setup && index == 0)
634 hsotg->setup = 0;
635 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700636 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200637
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100638
639 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
640 writel(ctrl, hsotg->regs + epctrl_reg);
641
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200642 /*
643 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100644 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200645 * this information.
646 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100647 hs_ep->size_loaded = length;
648 hs_ep->last_load = ureq->actual;
649
650 if (dir_in && !using_dma(hsotg)) {
651 /* set these anyway, we may need them for non-periodic in */
652 hs_ep->fifo_load = 0;
653
654 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
655 }
656
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200657 /*
658 * clear the INTknTXFEmpMsk when we start request, more as a aide
659 * to debugging to see what is going on.
660 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100661 if (dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700662 writel(DIEPMSK_INTKNTXFEMPMSK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200663 hsotg->regs + DIEPINT(index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100664
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200665 /*
666 * Note, trying to clear the NAK here causes problems with transmit
667 * on the S3C6400 ending up with the TXFIFO becoming full.
668 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100669
670 /* check ep is enabled */
Dinh Nguyen47a16852014-04-14 14:13:34 -0700671 if (!(readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100672 dev_warn(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700673 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100674 index, readl(hsotg->regs + epctrl_reg));
675
Dinh Nguyen47a16852014-04-14 14:13:34 -0700676 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100677 __func__, readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200678
679 /* enable ep interrupts */
680 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100681}
682
683/**
684 * s3c_hsotg_map_dma - map the DMA memory being used for the request
685 * @hsotg: The device state.
686 * @hs_ep: The endpoint the request is on.
687 * @req: The request being processed.
688 *
689 * We've been asked to queue a request, so ensure that the memory buffer
690 * is correctly setup for DMA. If we've been passed an extant DMA address
691 * then ensure the buffer has been synced to memory. If our buffer has no
692 * DMA memory, then we map the memory and mark our request to allow us to
693 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200694 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100695static int s3c_hsotg_map_dma(struct s3c_hsotg *hsotg,
696 struct s3c_hsotg_ep *hs_ep,
697 struct usb_request *req)
698{
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100699 struct s3c_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200700 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100701
702 /* if the length is zero, ignore the DMA data */
703 if (hs_req->req.length == 0)
704 return 0;
705
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200706 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
707 if (ret)
708 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100709
710 return 0;
711
712dma_error:
713 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
714 __func__, req->buf, req->length);
715
716 return -EIO;
717}
718
719static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
720 gfp_t gfp_flags)
721{
722 struct s3c_hsotg_req *hs_req = our_req(req);
723 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
724 struct s3c_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100725 bool first;
726
727 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
728 ep->name, req, req->length, req->buf, req->no_interrupt,
729 req->zero, req->short_not_ok);
730
731 /* initialise status of the request */
732 INIT_LIST_HEAD(&hs_req->queue);
733 req->actual = 0;
734 req->status = -EINPROGRESS;
735
736 /* if we're using DMA, sync the buffers as necessary */
737 if (using_dma(hs)) {
738 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
739 if (ret)
740 return ret;
741 }
742
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100743 first = list_empty(&hs_ep->queue);
744 list_add_tail(&hs_req->queue, &hs_ep->queue);
745
746 if (first)
747 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
748
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100749 return 0;
750}
751
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200752static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
753 gfp_t gfp_flags)
754{
755 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
756 struct s3c_hsotg *hs = hs_ep->parent;
757 unsigned long flags = 0;
758 int ret = 0;
759
760 spin_lock_irqsave(&hs->lock, flags);
761 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
762 spin_unlock_irqrestore(&hs->lock, flags);
763
764 return ret;
765}
766
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100767static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
768 struct usb_request *req)
769{
770 struct s3c_hsotg_req *hs_req = our_req(req);
771
772 kfree(hs_req);
773}
774
775/**
776 * s3c_hsotg_complete_oursetup - setup completion callback
777 * @ep: The endpoint the request was on.
778 * @req: The request completed.
779 *
780 * Called on completion of any requests the driver itself
781 * submitted that need cleaning up.
782 */
783static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
784 struct usb_request *req)
785{
786 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
787 struct s3c_hsotg *hsotg = hs_ep->parent;
788
789 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
790
791 s3c_hsotg_ep_free_request(ep, req);
792}
793
794/**
795 * ep_from_windex - convert control wIndex value to endpoint
796 * @hsotg: The driver state.
797 * @windex: The control request wIndex field (in host order).
798 *
799 * Convert the given wIndex into a pointer to an driver endpoint
800 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200801 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100802static struct s3c_hsotg_ep *ep_from_windex(struct s3c_hsotg *hsotg,
803 u32 windex)
804{
805 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
806 int dir = (windex & USB_DIR_IN) ? 1 : 0;
807 int idx = windex & 0x7F;
808
809 if (windex >= 0x100)
810 return NULL;
811
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200812 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100813 return NULL;
814
815 if (idx && ep->dir_in != dir)
816 return NULL;
817
818 return ep;
819}
820
821/**
822 * s3c_hsotg_send_reply - send reply to control request
823 * @hsotg: The device state
824 * @ep: Endpoint 0
825 * @buff: Buffer for request
826 * @length: Length of reply.
827 *
828 * Create a request and queue it on the given endpoint. This is useful as
829 * an internal method of sending replies to certain control requests, etc.
830 */
831static int s3c_hsotg_send_reply(struct s3c_hsotg *hsotg,
832 struct s3c_hsotg_ep *ep,
833 void *buff,
834 int length)
835{
836 struct usb_request *req;
837 int ret;
838
839 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
840
841 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
842 hsotg->ep0_reply = req;
843 if (!req) {
844 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
845 return -ENOMEM;
846 }
847
848 req->buf = hsotg->ep0_buff;
849 req->length = length;
850 req->zero = 1; /* always do zero-length final transfer */
851 req->complete = s3c_hsotg_complete_oursetup;
852
853 if (length)
854 memcpy(req->buf, buff, length);
855 else
856 ep->sent_zlp = 1;
857
858 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
859 if (ret) {
860 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
861 return ret;
862 }
863
864 return 0;
865}
866
867/**
868 * s3c_hsotg_process_req_status - process request GET_STATUS
869 * @hsotg: The device state
870 * @ctrl: USB control request
871 */
872static int s3c_hsotg_process_req_status(struct s3c_hsotg *hsotg,
873 struct usb_ctrlrequest *ctrl)
874{
875 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
876 struct s3c_hsotg_ep *ep;
877 __le16 reply;
878 int ret;
879
880 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
881
882 if (!ep0->dir_in) {
883 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
884 return -EINVAL;
885 }
886
887 switch (ctrl->bRequestType & USB_RECIP_MASK) {
888 case USB_RECIP_DEVICE:
889 reply = cpu_to_le16(0); /* bit 0 => self powered,
890 * bit 1 => remote wakeup */
891 break;
892
893 case USB_RECIP_INTERFACE:
894 /* currently, the data result should be zero */
895 reply = cpu_to_le16(0);
896 break;
897
898 case USB_RECIP_ENDPOINT:
899 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
900 if (!ep)
901 return -ENOENT;
902
903 reply = cpu_to_le16(ep->halted ? 1 : 0);
904 break;
905
906 default:
907 return 0;
908 }
909
910 if (le16_to_cpu(ctrl->wLength) != 2)
911 return -EINVAL;
912
913 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
914 if (ret) {
915 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
916 return ret;
917 }
918
919 return 1;
920}
921
922static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
923
924/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900925 * get_ep_head - return the first request on the endpoint
926 * @hs_ep: The controller endpoint to get
927 *
928 * Get the first request on the endpoint.
929 */
930static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
931{
932 if (list_empty(&hs_ep->queue))
933 return NULL;
934
935 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
936}
937
938/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100939 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
940 * @hsotg: The device state
941 * @ctrl: USB control request
942 */
943static int s3c_hsotg_process_req_feature(struct s3c_hsotg *hsotg,
944 struct usb_ctrlrequest *ctrl)
945{
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +0900946 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900947 struct s3c_hsotg_req *hs_req;
948 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100949 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
950 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +0900951 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200952 bool halted;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100953
954 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
955 __func__, set ? "SET" : "CLEAR");
956
957 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
958 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
959 if (!ep) {
960 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
961 __func__, le16_to_cpu(ctrl->wIndex));
962 return -ENOENT;
963 }
964
965 switch (le16_to_cpu(ctrl->wValue)) {
966 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200967 halted = ep->halted;
968
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100969 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +0900970
971 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
972 if (ret) {
973 dev_err(hsotg->dev,
974 "%s: failed to send reply\n", __func__);
975 return ret;
976 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900977
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200978 /*
979 * we have to complete all requests for ep if it was
980 * halted, and the halt was cleared by CLEAR_FEATURE
981 */
982
983 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900984 /*
985 * If we have request in progress,
986 * then complete it
987 */
988 if (ep->req) {
989 hs_req = ep->req;
990 ep->req = NULL;
991 list_del_init(&hs_req->queue);
992 hs_req->req.complete(&ep->ep,
993 &hs_req->req);
994 }
995
996 /* If we have pending request, then start it */
997 restart = !list_empty(&ep->queue);
998 if (restart) {
999 hs_req = get_ep_head(ep);
1000 s3c_hsotg_start_req(hsotg, ep,
1001 hs_req, false);
1002 }
1003 }
1004
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001005 break;
1006
1007 default:
1008 return -ENOENT;
1009 }
1010 } else
1011 return -ENOENT; /* currently only deal with endpoint */
1012
1013 return 1;
1014}
1015
Robert Baldygaab93e012013-09-19 11:50:17 +02001016static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg);
Robert Baldygad18f71162013-11-21 13:49:18 +01001017static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001018
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001019/**
Robert Baldygac9f721b2014-01-14 08:36:00 +01001020 * s3c_hsotg_stall_ep0 - stall ep0
1021 * @hsotg: The device state
1022 *
1023 * Set stall for ep0 as response for setup request.
1024 */
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001025static void s3c_hsotg_stall_ep0(struct s3c_hsotg *hsotg)
1026{
Robert Baldygac9f721b2014-01-14 08:36:00 +01001027 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1028 u32 reg;
1029 u32 ctrl;
1030
1031 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1032 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1033
1034 /*
1035 * DxEPCTL_Stall will be cleared by EP once it has
1036 * taken effect, so no need to clear later.
1037 */
1038
1039 ctrl = readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001040 ctrl |= DXEPCTL_STALL;
1041 ctrl |= DXEPCTL_CNAK;
Robert Baldygac9f721b2014-01-14 08:36:00 +01001042 writel(ctrl, hsotg->regs + reg);
1043
1044 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001045 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Robert Baldygac9f721b2014-01-14 08:36:00 +01001046 ctrl, reg, readl(hsotg->regs + reg));
1047
1048 /*
1049 * complete won't be called, so we enqueue
1050 * setup request here
1051 */
1052 s3c_hsotg_enqueue_setup(hsotg);
1053}
1054
1055/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001056 * s3c_hsotg_process_control - process a control request
1057 * @hsotg: The device state
1058 * @ctrl: The control request received
1059 *
1060 * The controller has received the SETUP phase of a control request, and
1061 * needs to work out what to do next (and whether to pass it on to the
1062 * gadget driver).
1063 */
1064static void s3c_hsotg_process_control(struct s3c_hsotg *hsotg,
1065 struct usb_ctrlrequest *ctrl)
1066{
1067 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1068 int ret = 0;
1069 u32 dcfg;
1070
1071 ep0->sent_zlp = 0;
1072
1073 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1074 ctrl->bRequest, ctrl->bRequestType,
1075 ctrl->wValue, ctrl->wLength);
1076
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001077 /*
1078 * record the direction of the request, for later use when enquing
1079 * packets onto EP0.
1080 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001081
1082 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1083 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1084
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001085 /*
1086 * if we've no data with this request, then the last part of the
1087 * transaction is going to implicitly be IN.
1088 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001089 if (ctrl->wLength == 0)
1090 ep0->dir_in = 1;
1091
1092 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1093 switch (ctrl->bRequest) {
1094 case USB_REQ_SET_ADDRESS:
Robert Baldygad18f71162013-11-21 13:49:18 +01001095 s3c_hsotg_disconnect(hsotg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001096 dcfg = readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001097 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001098 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1099 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001100 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001101
1102 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1103
1104 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1105 return;
1106
1107 case USB_REQ_GET_STATUS:
1108 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1109 break;
1110
1111 case USB_REQ_CLEAR_FEATURE:
1112 case USB_REQ_SET_FEATURE:
1113 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1114 break;
1115 }
1116 }
1117
1118 /* as a fallback, try delivering it to the driver to deal with */
1119
1120 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001121 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001123 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001124 if (ret < 0)
1125 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1126 }
1127
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001128 /*
1129 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001130 * so respond with a STALL for the status stage to indicate failure.
1131 */
1132
Robert Baldygac9f721b2014-01-14 08:36:00 +01001133 if (ret < 0)
1134 s3c_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001135}
1136
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001137/**
1138 * s3c_hsotg_complete_setup - completion of a setup transfer
1139 * @ep: The endpoint the request was on.
1140 * @req: The request completed.
1141 *
1142 * Called on completion of any requests the driver itself submitted for
1143 * EP0 setup packets
1144 */
1145static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1146 struct usb_request *req)
1147{
1148 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
1149 struct s3c_hsotg *hsotg = hs_ep->parent;
1150
1151 if (req->status < 0) {
1152 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1153 return;
1154 }
1155
Robert Baldyga93f599f2013-11-21 13:49:17 +01001156 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001157 if (req->actual == 0)
1158 s3c_hsotg_enqueue_setup(hsotg);
1159 else
1160 s3c_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001161 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001162}
1163
1164/**
1165 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1166 * @hsotg: The device state.
1167 *
1168 * Enqueue a request on EP0 if necessary to received any SETUP packets
1169 * received from the host.
1170 */
1171static void s3c_hsotg_enqueue_setup(struct s3c_hsotg *hsotg)
1172{
1173 struct usb_request *req = hsotg->ctrl_req;
1174 struct s3c_hsotg_req *hs_req = our_req(req);
1175 int ret;
1176
1177 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1178
1179 req->zero = 0;
1180 req->length = 8;
1181 req->buf = hsotg->ctrl_buff;
1182 req->complete = s3c_hsotg_complete_setup;
1183
1184 if (!list_empty(&hs_req->queue)) {
1185 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1186 return;
1187 }
1188
1189 hsotg->eps[0].dir_in = 0;
1190
1191 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1192 if (ret < 0) {
1193 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001194 /*
1195 * Don't think there's much we can do other than watch the
1196 * driver fail.
1197 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001198 }
1199}
1200
1201/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001202 * s3c_hsotg_complete_request - complete a request given to us
1203 * @hsotg: The device state.
1204 * @hs_ep: The endpoint the request was on.
1205 * @hs_req: The request to complete.
1206 * @result: The result code (0 => Ok, otherwise errno)
1207 *
1208 * The given request has finished, so call the necessary completion
1209 * if it has one and then look to see if we can start a new request
1210 * on the endpoint.
1211 *
1212 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001213 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001214static void s3c_hsotg_complete_request(struct s3c_hsotg *hsotg,
1215 struct s3c_hsotg_ep *hs_ep,
1216 struct s3c_hsotg_req *hs_req,
1217 int result)
1218{
1219 bool restart;
1220
1221 if (!hs_req) {
1222 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1223 return;
1224 }
1225
1226 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1227 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1228
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001229 /*
1230 * only replace the status if we've not already set an error
1231 * from a previous transaction
1232 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001233
1234 if (hs_req->req.status == -EINPROGRESS)
1235 hs_req->req.status = result;
1236
1237 hs_ep->req = NULL;
1238 list_del_init(&hs_req->queue);
1239
1240 if (using_dma(hsotg))
1241 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1242
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001243 /*
1244 * call the complete request with the locks off, just in case the
1245 * request tries to queue more work for this endpoint.
1246 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001247
1248 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001249 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001250 hs_req->req.complete(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001251 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001252 }
1253
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001254 /*
1255 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001256 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001257 * so be careful when doing this.
1258 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001259
1260 if (!hs_ep->req && result >= 0) {
1261 restart = !list_empty(&hs_ep->queue);
1262 if (restart) {
1263 hs_req = get_ep_head(hs_ep);
1264 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1265 }
1266 }
1267}
1268
1269/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001270 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1271 * @hsotg: The device state.
1272 * @ep_idx: The endpoint index for the data
1273 * @size: The size of data in the fifo, in bytes
1274 *
1275 * The FIFO status shows there is data to read from the FIFO for a given
1276 * endpoint, so sort out whether we need to read the data into a request
1277 * that has been made for that endpoint.
1278 */
1279static void s3c_hsotg_rx_data(struct s3c_hsotg *hsotg, int ep_idx, int size)
1280{
1281 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1282 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001283 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001284 int to_read;
1285 int max_req;
1286 int read_ptr;
1287
Lukasz Majewski22258f42012-06-14 10:02:24 +02001288
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001289 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001290 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001291 int ptr;
1292
1293 dev_warn(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001294 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001295 __func__, size, ep_idx, epctl);
1296
1297 /* dump the data from the FIFO, we've nothing we can do */
1298 for (ptr = 0; ptr < size; ptr += 4)
1299 (void)readl(fifo);
1300
1301 return;
1302 }
1303
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001304 to_read = size;
1305 read_ptr = hs_req->req.actual;
1306 max_req = hs_req->req.length - read_ptr;
1307
Ben Dooksa33e7132010-07-19 09:40:49 +01001308 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1309 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1310
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001311 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001312 /*
1313 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001314 * to deal with in this request.
1315 */
1316
1317 /* currently we don't deal this */
1318 WARN_ON_ONCE(1);
1319 }
1320
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001321 hs_ep->total_data += to_read;
1322 hs_req->req.actual += to_read;
1323 to_read = DIV_ROUND_UP(to_read, 4);
1324
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001325 /*
1326 * note, we might over-write the buffer end by 3 bytes depending on
1327 * alignment of the data.
1328 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001329 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001330}
1331
1332/**
1333 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1334 * @hsotg: The device instance
1335 * @req: The request currently on this endpoint
1336 *
1337 * Generate a zero-length IN packet request for terminating a SETUP
1338 * transaction.
1339 *
1340 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001341 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001342 * the TxFIFO.
1343 */
1344static void s3c_hsotg_send_zlp(struct s3c_hsotg *hsotg,
1345 struct s3c_hsotg_req *req)
1346{
1347 u32 ctrl;
1348
1349 if (!req) {
1350 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1351 return;
1352 }
1353
1354 if (req->req.length == 0) {
1355 hsotg->eps[0].sent_zlp = 1;
1356 s3c_hsotg_enqueue_setup(hsotg);
1357 return;
1358 }
1359
1360 hsotg->eps[0].dir_in = 1;
1361 hsotg->eps[0].sent_zlp = 1;
1362
1363 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1364
1365 /* issue a zero-sized packet to terminate this */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001366 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1367 DXEPTSIZ_XFERSIZE(0), hsotg->regs + DIEPTSIZ(0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001368
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001369 ctrl = readl(hsotg->regs + DIEPCTL0);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001370 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1371 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1372 ctrl |= DXEPCTL_USBACTEP;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001373 writel(ctrl, hsotg->regs + DIEPCTL0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001374}
1375
1376/**
1377 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1378 * @hsotg: The device instance
1379 * @epnum: The endpoint received from
1380 * @was_setup: Set if processing a SetupDone event.
1381 *
1382 * The RXFIFO has delivered an OutDone event, which means that the data
1383 * transfer for an OUT endpoint has been completed, either by a short
1384 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001385 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001386static void s3c_hsotg_handle_outdone(struct s3c_hsotg *hsotg,
1387 int epnum, bool was_setup)
1388{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001389 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001390 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1391 struct s3c_hsotg_req *hs_req = hs_ep->req;
1392 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001393 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001394 int result = 0;
1395
1396 if (!hs_req) {
1397 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1398 return;
1399 }
1400
1401 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001402 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001403
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001404 /*
1405 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001406 * is left in the endpoint size register and then working it
1407 * out from the amount we loaded for the transfer.
1408 *
1409 * We need to do this as DMA pointers are always 32bit aligned
1410 * so may overshoot/undershoot the transfer.
1411 */
1412
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001413 size_done = hs_ep->size_loaded - size_left;
1414 size_done += hs_ep->last_load;
1415
1416 req->actual = size_done;
1417 }
1418
Ben Dooksa33e7132010-07-19 09:40:49 +01001419 /* if there is more request to do, schedule new transfer */
1420 if (req->actual < req->length && size_left == 0) {
1421 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1422 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001423 } else if (epnum == 0) {
1424 /*
1425 * After was_setup = 1 =>
1426 * set CNAK for non Setup requests
1427 */
1428 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001429 }
1430
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001431 if (req->actual < req->length && req->short_not_ok) {
1432 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1433 __func__, req->actual, req->length);
1434
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001435 /*
1436 * todo - what should we return here? there's no one else
1437 * even bothering to check the status.
1438 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001439 }
1440
1441 if (epnum == 0) {
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001442 /*
1443 * Condition req->complete != s3c_hsotg_complete_setup says:
1444 * send ZLP when we have an asynchronous request from gadget
1445 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001446 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1447 s3c_hsotg_send_zlp(hsotg, hs_req);
1448 }
1449
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001450 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001451}
1452
1453/**
1454 * s3c_hsotg_read_frameno - read current frame number
1455 * @hsotg: The device instance
1456 *
1457 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001458 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001459static u32 s3c_hsotg_read_frameno(struct s3c_hsotg *hsotg)
1460{
1461 u32 dsts;
1462
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001463 dsts = readl(hsotg->regs + DSTS);
1464 dsts &= DSTS_SOFFN_MASK;
1465 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001466
1467 return dsts;
1468}
1469
1470/**
1471 * s3c_hsotg_handle_rx - RX FIFO has data
1472 * @hsotg: The device instance
1473 *
1474 * The IRQ handler has detected that the RX FIFO has some data in it
1475 * that requires processing, so find out what is in there and do the
1476 * appropriate read.
1477 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001478 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001479 * chunks, so if you have x packets received on an endpoint you'll get x
1480 * FIFO events delivered, each with a packet's worth of data in it.
1481 *
1482 * When using DMA, we should not be processing events from the RXFIFO
1483 * as the actual data should be sent to the memory directly and we turn
1484 * on the completion interrupts to get notifications of transfer completion.
1485 */
Mark Brown0978f8c2010-01-18 13:18:35 +00001486static void s3c_hsotg_handle_rx(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001487{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001488 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001489 u32 epnum, status, size;
1490
1491 WARN_ON(using_dma(hsotg));
1492
Dinh Nguyen47a16852014-04-14 14:13:34 -07001493 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1494 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001495
Dinh Nguyen47a16852014-04-14 14:13:34 -07001496 size = grxstsr & GRXSTS_BYTECNT_MASK;
1497 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001498
1499 if (1)
1500 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1501 __func__, grxstsr, size, epnum);
1502
Dinh Nguyen47a16852014-04-14 14:13:34 -07001503 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1504 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1505 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001506 break;
1507
Dinh Nguyen47a16852014-04-14 14:13:34 -07001508 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001509 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1510 s3c_hsotg_read_frameno(hsotg));
1511
1512 if (!using_dma(hsotg))
1513 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1514 break;
1515
Dinh Nguyen47a16852014-04-14 14:13:34 -07001516 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001517 dev_dbg(hsotg->dev,
1518 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1519 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001520 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001521
1522 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1523 break;
1524
Dinh Nguyen47a16852014-04-14 14:13:34 -07001525 case GRXSTS_PKTSTS_OUTRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001526 s3c_hsotg_rx_data(hsotg, epnum, size);
1527 break;
1528
Dinh Nguyen47a16852014-04-14 14:13:34 -07001529 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001530 dev_dbg(hsotg->dev,
1531 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1532 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001533 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001534
1535 s3c_hsotg_rx_data(hsotg, epnum, size);
1536 break;
1537
1538 default:
1539 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1540 __func__, grxstsr);
1541
1542 s3c_hsotg_dump(hsotg);
1543 break;
1544 }
1545}
1546
1547/**
1548 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1549 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001550 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001551static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1552{
1553 switch (mps) {
1554 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001555 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001556 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001557 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001558 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001559 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001560 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001561 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001562 }
1563
1564 /* bad max packet size, warn and return invalid result */
1565 WARN_ON(1);
1566 return (u32)-1;
1567}
1568
1569/**
1570 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1571 * @hsotg: The driver state.
1572 * @ep: The index number of the endpoint
1573 * @mps: The maximum packet size in bytes
1574 *
1575 * Configure the maximum packet size for the given endpoint, updating
1576 * the hardware control registers to reflect this.
1577 */
1578static void s3c_hsotg_set_ep_maxpacket(struct s3c_hsotg *hsotg,
1579 unsigned int ep, unsigned int mps)
1580{
1581 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1582 void __iomem *regs = hsotg->regs;
1583 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001584 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001585 u32 reg;
1586
1587 if (ep == 0) {
1588 /* EP0 is a special case */
1589 mpsval = s3c_hsotg_ep0_mps(mps);
1590 if (mpsval > 3)
1591 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001592 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001593 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001594 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001595 mpsval = mps & DXEPCTL_MPS_MASK;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001596 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001597 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001598 mcval = ((mps >> 11) & 0x3) + 1;
1599 hs_ep->mc = mcval;
1600 if (mcval > 3)
1601 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001602 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001603 }
1604
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001605 /*
1606 * update both the in and out endpoint controldir_ registers, even
1607 * if one of the directions may not be in use.
1608 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001609
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001610 reg = readl(regs + DIEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001611 reg &= ~DXEPCTL_MPS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001612 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001613 writel(reg, regs + DIEPCTL(ep));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001614
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001615 if (ep) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001616 reg = readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001617 reg &= ~DXEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001618 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001619 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001620 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001621
1622 return;
1623
1624bad_mps:
1625 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1626}
1627
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001628/**
1629 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1630 * @hsotg: The driver state
1631 * @idx: The index for the endpoint (0..15)
1632 */
1633static void s3c_hsotg_txfifo_flush(struct s3c_hsotg *hsotg, unsigned int idx)
1634{
1635 int timeout;
1636 int val;
1637
Dinh Nguyen47a16852014-04-14 14:13:34 -07001638 writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001639 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001640
1641 /* wait until the fifo is flushed */
1642 timeout = 100;
1643
1644 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001645 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001646
Dinh Nguyen47a16852014-04-14 14:13:34 -07001647 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001648 break;
1649
1650 if (--timeout == 0) {
1651 dev_err(hsotg->dev,
1652 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1653 __func__, val);
1654 }
1655
1656 udelay(1);
1657 }
1658}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001659
1660/**
1661 * s3c_hsotg_trytx - check to see if anything needs transmitting
1662 * @hsotg: The driver state
1663 * @hs_ep: The driver endpoint to check.
1664 *
1665 * Check to see if there is a request that has data to send, and if so
1666 * make an attempt to write data into the FIFO.
1667 */
1668static int s3c_hsotg_trytx(struct s3c_hsotg *hsotg,
1669 struct s3c_hsotg_ep *hs_ep)
1670{
1671 struct s3c_hsotg_req *hs_req = hs_ep->req;
1672
Robert Baldygaafcf4162013-09-19 11:50:19 +02001673 if (!hs_ep->dir_in || !hs_req) {
1674 /**
1675 * if request is not enqueued, we disable interrupts
1676 * for endpoints, excepting ep0
1677 */
1678 if (hs_ep->index != 0)
1679 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1680 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001681 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001682 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001683
1684 if (hs_req->req.actual < hs_req->req.length) {
1685 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1686 hs_ep->index);
1687 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1688 }
1689
1690 return 0;
1691}
1692
1693/**
1694 * s3c_hsotg_complete_in - complete IN transfer
1695 * @hsotg: The device state.
1696 * @hs_ep: The endpoint that has just completed.
1697 *
1698 * An IN transfer has been completed, update the transfer's state and then
1699 * call the relevant completion routines.
1700 */
1701static void s3c_hsotg_complete_in(struct s3c_hsotg *hsotg,
1702 struct s3c_hsotg_ep *hs_ep)
1703{
1704 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001705 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001706 int size_left, size_done;
1707
1708 if (!hs_req) {
1709 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1710 return;
1711 }
1712
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001713 /* Finish ZLP handling for IN EP0 transactions */
1714 if (hsotg->eps[0].sent_zlp) {
1715 dev_dbg(hsotg->dev, "zlp packet received\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001716 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001717 return;
1718 }
1719
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001720 /*
1721 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001722 * in the endpoint size register and then working it out from
1723 * the amount we loaded for the transfer.
1724 *
1725 * We do this even for DMA, as the transfer may have incremented
1726 * past the end of the buffer (DMA transfers are always 32bit
1727 * aligned).
1728 */
1729
Dinh Nguyen47a16852014-04-14 14:13:34 -07001730 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001731
1732 size_done = hs_ep->size_loaded - size_left;
1733 size_done += hs_ep->last_load;
1734
1735 if (hs_req->req.actual != size_done)
1736 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1737 __func__, hs_req->req.actual, size_done);
1738
1739 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001740 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1741 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001742
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001743 /*
1744 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1745 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1746 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1747 * inform the host that no more data is available.
1748 * The state of req.zero member is checked to be sure that the value to
1749 * send is smaller than wValue expected from host.
1750 * Check req.length to NOT send another ZLP when the current one is
1751 * under completion (the one for which this completion has been called).
1752 */
1753 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1754 hs_req->req.length == hs_req->req.actual &&
1755 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1756
1757 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1758 s3c_hsotg_send_zlp(hsotg, hs_req);
1759
1760 return;
1761 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001762
1763 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1764 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1765 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1766 } else
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001767 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001768}
1769
1770/**
1771 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1772 * @hsotg: The driver state
1773 * @idx: The index for the endpoint (0..15)
1774 * @dir_in: Set if this is an IN endpoint
1775 *
1776 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001777 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001778static void s3c_hsotg_epint(struct s3c_hsotg *hsotg, unsigned int idx,
1779 int dir_in)
1780{
1781 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001782 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1783 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1784 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001785 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02001786 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001787
1788 ints = readl(hsotg->regs + epint_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02001789 ctrl = readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001790
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001791 /* Clear endpoint interrupts */
1792 writel(ints, hsotg->regs + epint_reg);
1793
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001794 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1795 __func__, idx, dir_in ? "in" : "out", ints);
1796
Dinh Nguyen47a16852014-04-14 14:13:34 -07001797 if (ints & DXEPINT_XFERCOMPL) {
Robert Baldyga1479e842013-10-09 08:41:57 +02001798 if (hs_ep->isochronous && hs_ep->interval == 1) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001799 if (ctrl & DXEPCTL_EOFRNUM)
1800 ctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001801 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001802 ctrl |= DXEPCTL_SETODDFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001803 writel(ctrl, hsotg->regs + epctl_reg);
1804 }
1805
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001806 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001807 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001808 __func__, readl(hsotg->regs + epctl_reg),
1809 readl(hsotg->regs + epsiz_reg));
1810
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001811 /*
1812 * we get OutDone from the FIFO, so we only need to look
1813 * at completing IN requests here
1814 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001815 if (dir_in) {
1816 s3c_hsotg_complete_in(hsotg, hs_ep);
1817
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001818 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001819 s3c_hsotg_enqueue_setup(hsotg);
1820 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001821 /*
1822 * We're using DMA, we need to fire an OutDone here
1823 * as we ignore the RXFIFO.
1824 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001825
1826 s3c_hsotg_handle_outdone(hsotg, idx, false);
1827 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001828 }
1829
Dinh Nguyen47a16852014-04-14 14:13:34 -07001830 if (ints & DXEPINT_EPDISBLD) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001831 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001832
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001833 if (dir_in) {
1834 int epctl = readl(hsotg->regs + epctl_reg);
1835
1836 s3c_hsotg_txfifo_flush(hsotg, idx);
1837
Dinh Nguyen47a16852014-04-14 14:13:34 -07001838 if ((epctl & DXEPCTL_STALL) &&
1839 (epctl & DXEPCTL_EPTYPE_BULK)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001840 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001841
Dinh Nguyen47a16852014-04-14 14:13:34 -07001842 dctl |= DCTL_CGNPINNAK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001843 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001844 }
1845 }
1846 }
1847
Dinh Nguyen47a16852014-04-14 14:13:34 -07001848 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001849 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001850
Dinh Nguyen47a16852014-04-14 14:13:34 -07001851 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001852 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1853
1854 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001855 /*
1856 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001857 * setup packet. In non-DMA mode we'd get this
1858 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001859 * the setup here.
1860 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001861
1862 if (dir_in)
1863 WARN_ON_ONCE(1);
1864 else
1865 s3c_hsotg_handle_outdone(hsotg, 0, true);
1866 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001867 }
1868
Dinh Nguyen47a16852014-04-14 14:13:34 -07001869 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001870 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001871
Robert Baldyga1479e842013-10-09 08:41:57 +02001872 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001873 /* not sure if this is important, but we'll clear it anyway */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001874 if (ints & DIEPMSK_INTKNTXFEMPMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001875 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1876 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001877 }
1878
1879 /* this probably means something bad is happening */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001880 if (ints & DIEPMSK_INTKNEPMISMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001881 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1882 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001883 }
Ben Dooks10aebc72010-07-19 09:40:44 +01001884
1885 /* FIFO has space or is empty (see GAHBCFG) */
1886 if (hsotg->dedicated_fifos &&
Dinh Nguyen47a16852014-04-14 14:13:34 -07001887 ints & DIEPMSK_TXFIFOEMPTY) {
Ben Dooks10aebc72010-07-19 09:40:44 +01001888 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1889 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09001890 if (!using_dma(hsotg))
1891 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01001892 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001893 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001894}
1895
1896/**
1897 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
1898 * @hsotg: The device state.
1899 *
1900 * Handle updating the device settings after the enumeration phase has
1901 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001902 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001903static void s3c_hsotg_irq_enumdone(struct s3c_hsotg *hsotg)
1904{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001905 u32 dsts = readl(hsotg->regs + DSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001906 int ep0_mps = 0, ep_mps;
1907
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001908 /*
1909 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001910 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001911 * we connected at.
1912 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001913
1914 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
1915
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001916 /*
1917 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001918 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001919 * not advertise a 64byte MPS on EP0.
1920 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001921
1922 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001923 switch (dsts & DSTS_ENUMSPD_MASK) {
1924 case DSTS_ENUMSPD_FS:
1925 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001926 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001927 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01001928 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001929 break;
1930
Dinh Nguyen47a16852014-04-14 14:13:34 -07001931 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001932 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001933 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01001934 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001935 break;
1936
Dinh Nguyen47a16852014-04-14 14:13:34 -07001937 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001938 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001939 /*
1940 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001941 * moment, and the documentation seems to imply that it isn't
1942 * supported by the PHYs on some of the devices.
1943 */
1944 break;
1945 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02001946 dev_info(hsotg->dev, "new device is %s\n",
1947 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001948
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001949 /*
1950 * we should now know the maximum packet size for an
1951 * endpoint, so set the endpoints to a default value.
1952 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001953
1954 if (ep0_mps) {
1955 int i;
1956 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001957 for (i = 1; i < hsotg->num_of_eps; i++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001958 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
1959 }
1960
1961 /* ensure after enumeration our EP0 is active */
1962
1963 s3c_hsotg_enqueue_setup(hsotg);
1964
1965 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001966 readl(hsotg->regs + DIEPCTL0),
1967 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001968}
1969
1970/**
1971 * kill_all_requests - remove all requests from the endpoint's queue
1972 * @hsotg: The device state.
1973 * @ep: The endpoint the requests may be on.
1974 * @result: The result code to use.
1975 * @force: Force removal of any current requests
1976 *
1977 * Go through the requests on the given endpoint and mark them
1978 * completed with the given result code.
1979 */
1980static void kill_all_requests(struct s3c_hsotg *hsotg,
1981 struct s3c_hsotg_ep *ep,
1982 int result, bool force)
1983{
1984 struct s3c_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001985
1986 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001987 /*
1988 * currently, we can't do much about an already
1989 * running request on an in endpoint
1990 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001991
1992 if (ep->req == req && ep->dir_in && !force)
1993 continue;
1994
1995 s3c_hsotg_complete_request(hsotg, ep, req,
1996 result);
1997 }
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001998 if (hsotg->dedicated_fifos)
Robert Baldygab963a812013-12-06 13:03:45 +01001999 if ((readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4 < 3072)
2000 s3c_hsotg_txfifo_flush(hsotg, ep->index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002001}
2002
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002003/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002004 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002005 * @hsotg: The device state.
2006 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002007 * The device has been disconnected. Remove all current
2008 * transactions and signal the gadget driver that this
2009 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002010 */
Lukasz Majewski5e891342012-05-04 14:17:07 +02002011static void s3c_hsotg_disconnect(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002012{
2013 unsigned ep;
2014
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002015 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002016 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2017
2018 call_gadget(hsotg, disconnect);
2019}
2020
2021/**
2022 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2023 * @hsotg: The device state:
2024 * @periodic: True if this is a periodic FIFO interrupt
2025 */
2026static void s3c_hsotg_irq_fifoempty(struct s3c_hsotg *hsotg, bool periodic)
2027{
2028 struct s3c_hsotg_ep *ep;
2029 int epno, ret;
2030
2031 /* look through for any more data to transmit */
2032
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002033 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002034 ep = &hsotg->eps[epno];
2035
2036 if (!ep->dir_in)
2037 continue;
2038
2039 if ((periodic && !ep->periodic) ||
2040 (!periodic && ep->periodic))
2041 continue;
2042
2043 ret = s3c_hsotg_trytx(hsotg, ep);
2044 if (ret < 0)
2045 break;
2046 }
2047}
2048
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002049/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002050#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2051 GINTSTS_PTXFEMP | \
2052 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002053
2054/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002055 * s3c_hsotg_corereset - issue softreset to the core
2056 * @hsotg: The device state
2057 *
2058 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002059 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002060static int s3c_hsotg_corereset(struct s3c_hsotg *hsotg)
2061{
2062 int timeout;
2063 u32 grstctl;
2064
2065 dev_dbg(hsotg->dev, "resetting core\n");
2066
2067 /* issue soft reset */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002068 writel(GRSTCTL_CSFTRST, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002069
Du, Changbin2868fea2012-07-24 08:19:25 +08002070 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002071 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002072 grstctl = readl(hsotg->regs + GRSTCTL);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002073 } while ((grstctl & GRSTCTL_CSFTRST) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002074
Dinh Nguyen47a16852014-04-14 14:13:34 -07002075 if (grstctl & GRSTCTL_CSFTRST) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002076 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2077 return -EINVAL;
2078 }
2079
Du, Changbin2868fea2012-07-24 08:19:25 +08002080 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002081
2082 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002083 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002084
2085 if (timeout-- < 0) {
2086 dev_info(hsotg->dev,
2087 "%s: reset failed, GRSTCTL=%08x\n",
2088 __func__, grstctl);
2089 return -ETIMEDOUT;
2090 }
2091
Dinh Nguyen47a16852014-04-14 14:13:34 -07002092 if (!(grstctl & GRSTCTL_AHBIDLE))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002093 continue;
2094
2095 break; /* reset done */
2096 }
2097
2098 dev_dbg(hsotg->dev, "reset successful\n");
2099 return 0;
2100}
2101
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002102/**
2103 * s3c_hsotg_core_init - issue softreset to the core
2104 * @hsotg: The device state
2105 *
2106 * Issue a soft reset to the core, and await the core finishing it.
2107 */
Lukasz Majewski308d7342012-05-04 14:17:05 +02002108static void s3c_hsotg_core_init(struct s3c_hsotg *hsotg)
2109{
2110 s3c_hsotg_corereset(hsotg);
2111
2112 /*
2113 * we must now enable ep0 ready for host detection and then
2114 * set configuration.
2115 */
2116
2117 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002118 writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002119 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002120
2121 s3c_hsotg_init_fifo(hsotg);
2122
Dinh Nguyen47a16852014-04-14 14:13:34 -07002123 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002124
Dinh Nguyen47a16852014-04-14 14:13:34 -07002125 writel(1 << 18 | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002126
2127 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002128 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002129
2130 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002131 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002132
Dinh Nguyen47a16852014-04-14 14:13:34 -07002133 writel(GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
2134 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
2135 GINTSTS_CONIDSTSCHNG | GINTSTS_USBRST |
2136 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
2137 GINTSTS_USBSUSP | GINTSTS_WKUPINT,
2138 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002139
2140 if (using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002141 writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2142 GAHBCFG_HBSTLEN_INCR4,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002143 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002144 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07002145 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NP_TXF_EMP_LVL |
2146 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2147 GAHBCFG_GLBL_INTR_EN,
Robert Baldyga8acc8292013-09-19 11:50:23 +02002148 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002149
2150 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002151 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2152 * when we have no data to transfer. Otherwise we get being flooded by
2153 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002154 */
2155
Dinh Nguyen47a16852014-04-14 14:13:34 -07002156 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TXFIFOEMPTY |
2157 DIEPMSK_INTKNTXFEMPMSK : 0) |
2158 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
2159 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2160 DIEPMSK_INTKNEPMISMSK,
2161 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002162
2163 /*
2164 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2165 * DMA mode we may need this.
2166 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002167 writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
2168 DIEPMSK_TIMEOUTMSK) : 0) |
2169 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
2170 DOEPMSK_SETUPMSK,
2171 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002172
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002173 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002174
2175 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002176 readl(hsotg->regs + DIEPCTL0),
2177 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002178
2179 /* enable in and out endpoint interrupts */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002180 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002181
2182 /*
2183 * Enable the RXFIFO when in slave mode, as this is how we collect
2184 * the data. In DMA mode, we get events from the FIFO but also
2185 * things we cannot process, so do not use it.
2186 */
2187 if (!using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002188 s3c_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002189
2190 /* Enable interrupts for EP0 in and out */
2191 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2192 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2193
Dinh Nguyen47a16852014-04-14 14:13:34 -07002194 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002195 udelay(10); /* see openiboot */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002196 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002197
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002198 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002199
2200 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002201 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002202 * writing to the EPCTL register..
2203 */
2204
2205 /* set to read 1 8byte packet */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002206 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2207 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002208
2209 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002210 DXEPCTL_CNAK | DXEPCTL_EPENA |
2211 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002212 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002213
2214 /* enable, but don't activate EP0in */
2215 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002216 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002217
2218 s3c_hsotg_enqueue_setup(hsotg);
2219
2220 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002221 readl(hsotg->regs + DIEPCTL0),
2222 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002223
2224 /* clear global NAKs */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002225 writel(DCTL_CGOUTNAK | DCTL_CGNPINNAK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002226 hsotg->regs + DCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002227
2228 /* must be at-least 3ms to allow bus to see disconnect */
2229 mdelay(3);
2230
2231 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002232 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002233}
2234
2235/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002236 * s3c_hsotg_irq - handle device interrupt
2237 * @irq: The IRQ number triggered
2238 * @pw: The pw value when registered the handler.
2239 */
2240static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2241{
2242 struct s3c_hsotg *hsotg = pw;
2243 int retry_count = 8;
2244 u32 gintsts;
2245 u32 gintmsk;
2246
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002247 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002248irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002249 gintsts = readl(hsotg->regs + GINTSTS);
2250 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002251
2252 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2253 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2254
2255 gintsts &= gintmsk;
2256
Dinh Nguyen47a16852014-04-14 14:13:34 -07002257 if (gintsts & GINTSTS_OTGINT) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002258 u32 otgint = readl(hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002259
2260 dev_info(hsotg->dev, "OTGInt: %08x\n", otgint);
2261
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002262 writel(otgint, hsotg->regs + GOTGINT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002263 }
2264
Dinh Nguyen47a16852014-04-14 14:13:34 -07002265 if (gintsts & GINTSTS_SESSREQINT) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002266 dev_dbg(hsotg->dev, "%s: SessReqInt\n", __func__);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002267 writel(GINTSTS_SESSREQINT, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002268 }
2269
Dinh Nguyen47a16852014-04-14 14:13:34 -07002270 if (gintsts & GINTSTS_ENUMDONE) {
2271 writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002272
2273 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002274 }
2275
Dinh Nguyen47a16852014-04-14 14:13:34 -07002276 if (gintsts & GINTSTS_CONIDSTSCHNG) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002277 dev_dbg(hsotg->dev, "ConIDStsChg (DSTS=0x%08x, GOTCTL=%08x)\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002278 readl(hsotg->regs + DSTS),
2279 readl(hsotg->regs + GOTGCTL));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002280
Dinh Nguyen47a16852014-04-14 14:13:34 -07002281 writel(GINTSTS_CONIDSTSCHNG, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002282 }
2283
Dinh Nguyen47a16852014-04-14 14:13:34 -07002284 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002285 u32 daint = readl(hsotg->regs + DAINT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002286 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2287 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002288 int ep;
2289
Robert Baldyga7e804652013-09-19 11:50:20 +02002290 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002291 daint_out = daint >> DAINT_OUTEP_SHIFT;
2292 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002293
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002294 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2295
2296 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2297 if (daint_out & 1)
2298 s3c_hsotg_epint(hsotg, ep, 0);
2299 }
2300
2301 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2302 if (daint_in & 1)
2303 s3c_hsotg_epint(hsotg, ep, 1);
2304 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002305 }
2306
Dinh Nguyen47a16852014-04-14 14:13:34 -07002307 if (gintsts & GINTSTS_USBRST) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002308
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002309 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002310
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002311 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2312 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002313 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002314
Dinh Nguyen47a16852014-04-14 14:13:34 -07002315 writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002316
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002317 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002318 if (time_after(jiffies, hsotg->last_rst +
2319 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002320
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002321 kill_all_requests(hsotg, &hsotg->eps[0],
2322 -ECONNRESET, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002323
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002324 s3c_hsotg_core_init(hsotg);
2325 hsotg->last_rst = jiffies;
2326 }
2327 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002328 }
2329
2330 /* check both FIFOs */
2331
Dinh Nguyen47a16852014-04-14 14:13:34 -07002332 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002333 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2334
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002335 /*
2336 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002337 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002338 * it needs re-enabling
2339 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002340
Dinh Nguyen47a16852014-04-14 14:13:34 -07002341 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002342 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002343 }
2344
Dinh Nguyen47a16852014-04-14 14:13:34 -07002345 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002346 dev_dbg(hsotg->dev, "PTxFEmp\n");
2347
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002348 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002349
Dinh Nguyen47a16852014-04-14 14:13:34 -07002350 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002351 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002352 }
2353
Dinh Nguyen47a16852014-04-14 14:13:34 -07002354 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002355 /*
2356 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002357 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002358 * set.
2359 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002360
2361 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362 }
2363
Dinh Nguyen47a16852014-04-14 14:13:34 -07002364 if (gintsts & GINTSTS_MODEMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002365 dev_warn(hsotg->dev, "warning, mode mismatch triggered\n");
Dinh Nguyen47a16852014-04-14 14:13:34 -07002366 writel(GINTSTS_MODEMIS, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002367 }
2368
Dinh Nguyen47a16852014-04-14 14:13:34 -07002369 if (gintsts & GINTSTS_USBSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002370 dev_info(hsotg->dev, "GINTSTS_USBSusp\n");
Dinh Nguyen47a16852014-04-14 14:13:34 -07002371 writel(GINTSTS_USBSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002372
2373 call_gadget(hsotg, suspend);
2374 }
2375
Dinh Nguyen47a16852014-04-14 14:13:34 -07002376 if (gintsts & GINTSTS_WKUPINT) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002377 dev_info(hsotg->dev, "GINTSTS_WkUpIn\n");
Dinh Nguyen47a16852014-04-14 14:13:34 -07002378 writel(GINTSTS_WKUPINT, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002379
2380 call_gadget(hsotg, resume);
2381 }
2382
Dinh Nguyen47a16852014-04-14 14:13:34 -07002383 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002384 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Dinh Nguyen47a16852014-04-14 14:13:34 -07002385 writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002386 }
2387
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002388 /*
2389 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002390 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002391 * the occurrence.
2392 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002393
Dinh Nguyen47a16852014-04-14 14:13:34 -07002394 if (gintsts & GINTSTS_GOUTNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002395 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2396
Dinh Nguyen47a16852014-04-14 14:13:34 -07002397 writel(DCTL_CGOUTNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002398
2399 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002400 }
2401
Dinh Nguyen47a16852014-04-14 14:13:34 -07002402 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002403 dev_info(hsotg->dev, "GINNakEff triggered\n");
2404
Dinh Nguyen47a16852014-04-14 14:13:34 -07002405 writel(DCTL_CGNPINNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002406
2407 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002408 }
2409
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002410 /*
2411 * if we've had fifo events, we should try and go around the
2412 * loop again to see if there's any point in returning yet.
2413 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002414
2415 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2416 goto irq_retry;
2417
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002418 spin_unlock(&hsotg->lock);
2419
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002420 return IRQ_HANDLED;
2421}
2422
2423/**
2424 * s3c_hsotg_ep_enable - enable the given endpoint
2425 * @ep: The USB endpint to configure
2426 * @desc: The USB endpoint descriptor to configure with.
2427 *
2428 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002429 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002430static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2431 const struct usb_endpoint_descriptor *desc)
2432{
2433 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2434 struct s3c_hsotg *hsotg = hs_ep->parent;
2435 unsigned long flags;
2436 int index = hs_ep->index;
2437 u32 epctrl_reg;
2438 u32 epctrl;
2439 u32 mps;
2440 int dir_in;
Julia Lawall19c190f2010-03-29 17:36:44 +02002441 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002442
2443 dev_dbg(hsotg->dev,
2444 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2445 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2446 desc->wMaxPacketSize, desc->bInterval);
2447
2448 /* not to be called for EP0 */
2449 WARN_ON(index == 0);
2450
2451 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2452 if (dir_in != hs_ep->dir_in) {
2453 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2454 return -EINVAL;
2455 }
2456
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002457 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002458
2459 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2460
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002461 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002462 epctrl = readl(hsotg->regs + epctrl_reg);
2463
2464 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2465 __func__, epctrl, epctrl_reg);
2466
Lukasz Majewski22258f42012-06-14 10:02:24 +02002467 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002468
Dinh Nguyen47a16852014-04-14 14:13:34 -07002469 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2470 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002471
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002472 /*
2473 * mark the endpoint as active, otherwise the core may ignore
2474 * transactions entirely for this endpoint
2475 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002476 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002477
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002478 /*
2479 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002480 * do something with data that we've yet got a request to process
2481 * since the RXFIFO will take data for an endpoint even if the
2482 * size register hasn't been set.
2483 */
2484
Dinh Nguyen47a16852014-04-14 14:13:34 -07002485 epctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002486
2487 /* update the endpoint state */
Robert Baldygae9edd1992013-10-09 08:20:02 +02002488 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002489
2490 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002491 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002492 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002493 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002494 hs_ep->interval = desc->bInterval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002495
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002496 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2497 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2498
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002499 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2500 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002501 epctrl |= DXEPCTL_EPTYPE_ISO;
2502 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02002503 hs_ep->isochronous = 1;
2504 if (dir_in)
2505 hs_ep->periodic = 1;
2506 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002507
2508 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002509 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002510 break;
2511
2512 case USB_ENDPOINT_XFER_INT:
2513 if (dir_in) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002514 /*
2515 * Allocate our TxFNum by simply using the index
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 * of the endpoint for the moment. We could do
2517 * something better if the host indicates how
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002518 * many FIFOs we are expecting to use.
2519 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520
2521 hs_ep->periodic = 1;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002522 epctrl |= DXEPCTL_TXFNUM(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002523 }
2524
Dinh Nguyen47a16852014-04-14 14:13:34 -07002525 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002526 break;
2527
2528 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002529 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002530 break;
2531 }
2532
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002533 /*
2534 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002535 * a unique tx-fifo even if it is non-periodic.
2536 */
2537 if (dir_in && hsotg->dedicated_fifos)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002538 epctrl |= DXEPCTL_TXFNUM(index);
Ben Dooks10aebc72010-07-19 09:40:44 +01002539
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002540 /* for non control endpoints, set PID to D0 */
2541 if (index)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002542 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002543
2544 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2545 __func__, epctrl);
2546
2547 writel(epctrl, hsotg->regs + epctrl_reg);
2548 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2549 __func__, readl(hsotg->regs + epctrl_reg));
2550
2551 /* enable the endpoint interrupt */
2552 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2553
Lukasz Majewski22258f42012-06-14 10:02:24 +02002554 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002555 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002556}
2557
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002558/**
2559 * s3c_hsotg_ep_disable - disable given endpoint
2560 * @ep: The endpoint to disable.
2561 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002562static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2563{
2564 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2565 struct s3c_hsotg *hsotg = hs_ep->parent;
2566 int dir_in = hs_ep->dir_in;
2567 int index = hs_ep->index;
2568 unsigned long flags;
2569 u32 epctrl_reg;
2570 u32 ctrl;
2571
2572 dev_info(hsotg->dev, "%s(ep %p)\n", __func__, ep);
2573
2574 if (ep == &hsotg->eps[0].ep) {
2575 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2576 return -EINVAL;
2577 }
2578
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002579 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002580
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002581 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002582 /* terminate all requests with shutdown */
2583 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2584
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002585
2586 ctrl = readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002587 ctrl &= ~DXEPCTL_EPENA;
2588 ctrl &= ~DXEPCTL_USBACTEP;
2589 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002590
2591 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2592 writel(ctrl, hsotg->regs + epctrl_reg);
2593
2594 /* disable endpoint interrupts */
2595 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2596
Lukasz Majewski22258f42012-06-14 10:02:24 +02002597 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002598 return 0;
2599}
2600
2601/**
2602 * on_list - check request is on the given endpoint
2603 * @ep: The endpoint to check.
2604 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002605 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002606static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2607{
2608 struct s3c_hsotg_req *req, *treq;
2609
2610 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2611 if (req == test)
2612 return true;
2613 }
2614
2615 return false;
2616}
2617
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002618/**
2619 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2620 * @ep: The endpoint to dequeue.
2621 * @req: The request to be removed from a queue.
2622 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002623static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2624{
2625 struct s3c_hsotg_req *hs_req = our_req(req);
2626 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2627 struct s3c_hsotg *hs = hs_ep->parent;
2628 unsigned long flags;
2629
2630 dev_info(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
2631
Lukasz Majewski22258f42012-06-14 10:02:24 +02002632 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002633
2634 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002635 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002636 return -EINVAL;
2637 }
2638
2639 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002640 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002641
2642 return 0;
2643}
2644
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002645/**
2646 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2647 * @ep: The endpoint to set halt.
2648 * @value: Set or unset the halt.
2649 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002650static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2651{
2652 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2653 struct s3c_hsotg *hs = hs_ep->parent;
2654 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002655 u32 epreg;
2656 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002657 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002658
2659 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2660
Robert Baldygac9f721b2014-01-14 08:36:00 +01002661 if (index == 0) {
2662 if (value)
2663 s3c_hsotg_stall_ep0(hs);
2664 else
2665 dev_warn(hs->dev,
2666 "%s: can't clear halt on ep0\n", __func__);
2667 return 0;
2668 }
2669
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002670 /* write both IN and OUT control registers */
2671
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002672 epreg = DIEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002673 epctl = readl(hs->regs + epreg);
2674
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002675 if (value) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002676 epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
2677 if (epctl & DXEPCTL_EPENA)
2678 epctl |= DXEPCTL_EPDIS;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002679 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002680 epctl &= ~DXEPCTL_STALL;
2681 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2682 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2683 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2684 epctl |= DXEPCTL_SETD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002685 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002686
2687 writel(epctl, hs->regs + epreg);
2688
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002689 epreg = DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002690 epctl = readl(hs->regs + epreg);
2691
2692 if (value)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002693 epctl |= DXEPCTL_STALL;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002694 else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002695 epctl &= ~DXEPCTL_STALL;
2696 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2697 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2698 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2699 epctl |= DXEPCTL_SETD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002700 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002701
2702 writel(epctl, hs->regs + epreg);
2703
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002704 hs_ep->halted = value;
2705
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002706 return 0;
2707}
2708
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002709/**
2710 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2711 * @ep: The endpoint to set halt.
2712 * @value: Set or unset the halt.
2713 */
2714static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2715{
2716 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
2717 struct s3c_hsotg *hs = hs_ep->parent;
2718 unsigned long flags = 0;
2719 int ret = 0;
2720
2721 spin_lock_irqsave(&hs->lock, flags);
2722 ret = s3c_hsotg_ep_sethalt(ep, value);
2723 spin_unlock_irqrestore(&hs->lock, flags);
2724
2725 return ret;
2726}
2727
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002728static struct usb_ep_ops s3c_hsotg_ep_ops = {
2729 .enable = s3c_hsotg_ep_enable,
2730 .disable = s3c_hsotg_ep_disable,
2731 .alloc_request = s3c_hsotg_ep_alloc_request,
2732 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002733 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002734 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002735 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002736 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002737};
2738
2739/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002740 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002741 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002742 *
2743 * A wrapper for platform code responsible for controlling
2744 * low-level USB code
2745 */
2746static void s3c_hsotg_phy_enable(struct s3c_hsotg *hsotg)
2747{
2748 struct platform_device *pdev = to_platform_device(hsotg->dev);
2749
2750 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302751
Matt Porter74084842013-12-19 09:23:06 -05002752 if (hsotg->phy) {
2753 phy_init(hsotg->phy);
2754 phy_power_on(hsotg->phy);
2755 } else if (hsotg->uphy)
2756 usb_phy_init(hsotg->uphy);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302757 else if (hsotg->plat->phy_init)
Lukasz Majewski41188782012-05-04 14:17:01 +02002758 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2759}
2760
2761/**
2762 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002763 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002764 *
2765 * A wrapper for platform code responsible for controlling
2766 * low-level USB code
2767 */
2768static void s3c_hsotg_phy_disable(struct s3c_hsotg *hsotg)
2769{
2770 struct platform_device *pdev = to_platform_device(hsotg->dev);
2771
Matt Porter74084842013-12-19 09:23:06 -05002772 if (hsotg->phy) {
2773 phy_power_off(hsotg->phy);
2774 phy_exit(hsotg->phy);
2775 } else if (hsotg->uphy)
2776 usb_phy_shutdown(hsotg->uphy);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302777 else if (hsotg->plat->phy_exit)
Lukasz Majewski41188782012-05-04 14:17:01 +02002778 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2779}
2780
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002781/**
2782 * s3c_hsotg_init - initalize the usb core
2783 * @hsotg: The driver state
2784 */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002785static void s3c_hsotg_init(struct s3c_hsotg *hsotg)
2786{
2787 /* unmask subset of endpoint interrupts */
2788
Dinh Nguyen47a16852014-04-14 14:13:34 -07002789 writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2790 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
2791 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002792
Dinh Nguyen47a16852014-04-14 14:13:34 -07002793 writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
2794 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
2795 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002796
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002797 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002798
2799 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002800 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002801
2802 if (0) {
2803 /* post global nak until we're ready */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002804 writel(DCTL_SGNPINNAK | DCTL_SGOUTNAK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002805 hsotg->regs + DCTL);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002806 }
2807
2808 /* setup fifos */
2809
2810 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002811 readl(hsotg->regs + GRXFSIZ),
2812 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002813
2814 s3c_hsotg_init_fifo(hsotg);
2815
2816 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002817 writel(GUSBCFG_PHYIF16 | GUSBCFG_TOUTCAL(7) | (0x5 << 10),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002818 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002819
Dinh Nguyen47a16852014-04-14 14:13:34 -07002820 writel(using_dma(hsotg) ? GAHBCFG_DMA_EN : 0x0,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002821 hsotg->regs + GAHBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002822}
2823
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002824/**
2825 * s3c_hsotg_udc_start - prepare the udc for work
2826 * @gadget: The usb gadget state
2827 * @driver: The usb gadget driver
2828 *
2829 * Perform initialization to prepare udc device and driver
2830 * to work.
2831 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002832static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2833 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002834{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02002835 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002836 int ret;
2837
2838 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02002839 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002840 return -ENODEV;
2841 }
2842
2843 if (!driver) {
2844 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2845 return -EINVAL;
2846 }
2847
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002848 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002849 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002850
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002851 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002852 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2853 return -EINVAL;
2854 }
2855
2856 WARN_ON(hsotg->driver);
2857
2858 driver->driver.bus = NULL;
2859 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03002860 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002861 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2862
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002863 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
2864 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002865 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002866 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002867 goto err;
2868 }
2869
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002870 hsotg->last_rst = jiffies;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002871 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
2872 return 0;
2873
2874err:
2875 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002876 return ret;
2877}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002878
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002879/**
2880 * s3c_hsotg_udc_stop - stop the udc
2881 * @gadget: The usb gadget state
2882 * @driver: The usb gadget driver
2883 *
2884 * Stop udc hw block and stay tunned for future transmissions
2885 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002886static int s3c_hsotg_udc_stop(struct usb_gadget *gadget,
2887 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002888{
Lukasz Majewskif99b2bf2012-05-04 14:17:12 +02002889 struct s3c_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002890 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002891 int ep;
2892
2893 if (!hsotg)
2894 return -ENODEV;
2895
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002896 /* all endpoints should be shutdown */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002897 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002898 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
2899
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002900 spin_lock_irqsave(&hsotg->lock, flags);
2901
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002902 s3c_hsotg_phy_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002903
Marek Szyprowskic8c10252013-09-12 16:18:48 +02002904 if (!driver)
2905 hsotg->driver = NULL;
2906
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002907 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002908
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002909 spin_unlock_irqrestore(&hsotg->lock, flags);
2910
Marek Szyprowskic8c10252013-09-12 16:18:48 +02002911 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002912
2913 return 0;
2914}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002915
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002916/**
2917 * s3c_hsotg_gadget_getframe - read the frame number
2918 * @gadget: The usb gadget state
2919 *
2920 * Read the {micro} frame number
2921 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002922static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2923{
2924 return s3c_hsotg_read_frameno(to_hsotg(gadget));
2925}
2926
Lukasz Majewskia188b682012-06-22 09:29:56 +02002927/**
2928 * s3c_hsotg_pullup - connect/disconnect the USB PHY
2929 * @gadget: The usb gadget state
2930 * @is_on: Current state of the USB PHY
2931 *
2932 * Connect/Disconnect the USB PHY pullup
2933 */
2934static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
2935{
2936 struct s3c_hsotg *hsotg = to_hsotg(gadget);
2937 unsigned long flags = 0;
2938
2939 dev_dbg(hsotg->dev, "%s: is_in: %d\n", __func__, is_on);
2940
2941 spin_lock_irqsave(&hsotg->lock, flags);
2942 if (is_on) {
2943 s3c_hsotg_phy_enable(hsotg);
2944 s3c_hsotg_core_init(hsotg);
2945 } else {
2946 s3c_hsotg_disconnect(hsotg);
2947 s3c_hsotg_phy_disable(hsotg);
2948 }
2949
2950 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2951 spin_unlock_irqrestore(&hsotg->lock, flags);
2952
2953 return 0;
2954}
2955
Felipe Balbieeef4582013-01-24 17:58:16 +02002956static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002957 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002958 .udc_start = s3c_hsotg_udc_start,
2959 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02002960 .pullup = s3c_hsotg_pullup,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002961};
2962
2963/**
2964 * s3c_hsotg_initep - initialise a single endpoint
2965 * @hsotg: The device state.
2966 * @hs_ep: The endpoint to be initialised.
2967 * @epnum: The endpoint number
2968 *
2969 * Initialise the given endpoint (as part of the probe and device state
2970 * creation) to give to the gadget driver. Setup the endpoint name, any
2971 * direction information and other state that may be required.
2972 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05002973static void s3c_hsotg_initep(struct s3c_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002974 struct s3c_hsotg_ep *hs_ep,
2975 int epnum)
2976{
2977 u32 ptxfifo;
2978 char *dir;
2979
2980 if (epnum == 0)
2981 dir = "";
2982 else if ((epnum % 2) == 0) {
2983 dir = "out";
2984 } else {
2985 dir = "in";
2986 hs_ep->dir_in = 1;
2987 }
2988
2989 hs_ep->index = epnum;
2990
2991 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
2992
2993 INIT_LIST_HEAD(&hs_ep->queue);
2994 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
2995
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002996 /* add to the list of endpoints known by the gadget driver */
2997 if (epnum)
2998 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
2999
3000 hs_ep->parent = hsotg;
3001 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003002 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003003 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3004
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003005 /*
3006 * Read the FIFO size for the Periodic TX FIFO, even if we're
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003007 * an OUT endpoint, we may as well do this if in future the
3008 * code is changed to make each endpoint's direction changeable.
3009 */
3010
Dinh Nguyen47a16852014-04-14 14:13:34 -07003011 ptxfifo = readl(hsotg->regs + DPTXFSIZN(epnum));
3012 hs_ep->fifo_size = FIFOSIZE_DEPTH_GET(ptxfifo) * 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003013
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003014 /*
3015 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003016 * to be something valid.
3017 */
3018
3019 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003020 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003021 writel(next, hsotg->regs + DIEPCTL(epnum));
3022 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003023 }
3024}
3025
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003026/**
3027 * s3c_hsotg_hw_cfg - read HW configuration registers
3028 * @param: The device state
3029 *
3030 * Read the USB core HW configuration registers
3031 */
3032static void s3c_hsotg_hw_cfg(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003033{
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003034 u32 cfg2, cfg4;
Ben Dooks10aebc72010-07-19 09:40:44 +01003035 /* check hardware configuration */
3036
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003037 cfg2 = readl(hsotg->regs + 0x48);
3038 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3039
3040 dev_info(hsotg->dev, "EPs:%d\n", hsotg->num_of_eps);
3041
Ben Dooks10aebc72010-07-19 09:40:44 +01003042 cfg4 = readl(hsotg->regs + 0x50);
3043 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3044
3045 dev_info(hsotg->dev, "%s fifos\n",
3046 hsotg->dedicated_fifos ? "dedicated" : "shared");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003047}
3048
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003049/**
3050 * s3c_hsotg_dump - dump state of the udc
3051 * @param: The device state
3052 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003053static void s3c_hsotg_dump(struct s3c_hsotg *hsotg)
3054{
Mark Brown83a01802011-06-01 17:16:15 +01003055#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003056 struct device *dev = hsotg->dev;
3057 void __iomem *regs = hsotg->regs;
3058 u32 val;
3059 int idx;
3060
3061 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003062 readl(regs + DCFG), readl(regs + DCTL),
3063 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003064
3065 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003066 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003067
3068 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003069 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003070
3071 /* show periodic fifo settings */
3072
3073 for (idx = 1; idx <= 15; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003074 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003075 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003076 val >> FIFOSIZE_DEPTH_SHIFT,
3077 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003078 }
3079
3080 for (idx = 0; idx < 15; idx++) {
3081 dev_info(dev,
3082 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003083 readl(regs + DIEPCTL(idx)),
3084 readl(regs + DIEPTSIZ(idx)),
3085 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003086
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003087 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003088 dev_info(dev,
3089 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003090 idx, readl(regs + DOEPCTL(idx)),
3091 readl(regs + DOEPTSIZ(idx)),
3092 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003093
3094 }
3095
3096 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003097 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003098#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003099}
3100
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003101/**
3102 * state_show - debugfs: show overall driver and device state.
3103 * @seq: The seq file to write to.
3104 * @v: Unused parameter.
3105 *
3106 * This debugfs entry shows the overall state of the hardware and
3107 * some general information about each of the endpoints available
3108 * to the system.
3109 */
3110static int state_show(struct seq_file *seq, void *v)
3111{
3112 struct s3c_hsotg *hsotg = seq->private;
3113 void __iomem *regs = hsotg->regs;
3114 int idx;
3115
3116 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003117 readl(regs + DCFG),
3118 readl(regs + DCTL),
3119 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003120
3121 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003122 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003123
3124 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003125 readl(regs + GINTMSK),
3126 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003127
3128 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003129 readl(regs + DAINTMSK),
3130 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003131
3132 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003133 readl(regs + GNPTXSTS),
3134 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003135
Pavel Macheka023da32013-09-30 14:56:02 +02003136 seq_puts(seq, "\nEndpoint status:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003137
3138 for (idx = 0; idx < 15; idx++) {
3139 u32 in, out;
3140
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003141 in = readl(regs + DIEPCTL(idx));
3142 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003143
3144 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3145 idx, in, out);
3146
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003147 in = readl(regs + DIEPTSIZ(idx));
3148 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003149
3150 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3151 in, out);
3152
Pavel Macheka023da32013-09-30 14:56:02 +02003153 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003154 }
3155
3156 return 0;
3157}
3158
3159static int state_open(struct inode *inode, struct file *file)
3160{
3161 return single_open(file, state_show, inode->i_private);
3162}
3163
3164static const struct file_operations state_fops = {
3165 .owner = THIS_MODULE,
3166 .open = state_open,
3167 .read = seq_read,
3168 .llseek = seq_lseek,
3169 .release = single_release,
3170};
3171
3172/**
3173 * fifo_show - debugfs: show the fifo information
3174 * @seq: The seq_file to write data to.
3175 * @v: Unused parameter.
3176 *
3177 * Show the FIFO information for the overall fifo and all the
3178 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003179 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003180static int fifo_show(struct seq_file *seq, void *v)
3181{
3182 struct s3c_hsotg *hsotg = seq->private;
3183 void __iomem *regs = hsotg->regs;
3184 u32 val;
3185 int idx;
3186
Pavel Macheka023da32013-09-30 14:56:02 +02003187 seq_puts(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003188 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003189
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003190 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003191 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Dinh Nguyen47a16852014-04-14 14:13:34 -07003192 val >> FIFOSIZE_DEPTH_SHIFT,
3193 val & FIFOSIZE_DEPTH_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003194
Pavel Macheka023da32013-09-30 14:56:02 +02003195 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003196
3197 for (idx = 1; idx <= 15; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003198 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003199
3200 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003201 val >> FIFOSIZE_DEPTH_SHIFT,
3202 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003203 }
3204
3205 return 0;
3206}
3207
3208static int fifo_open(struct inode *inode, struct file *file)
3209{
3210 return single_open(file, fifo_show, inode->i_private);
3211}
3212
3213static const struct file_operations fifo_fops = {
3214 .owner = THIS_MODULE,
3215 .open = fifo_open,
3216 .read = seq_read,
3217 .llseek = seq_lseek,
3218 .release = single_release,
3219};
3220
3221
3222static const char *decode_direction(int is_in)
3223{
3224 return is_in ? "in" : "out";
3225}
3226
3227/**
3228 * ep_show - debugfs: show the state of an endpoint.
3229 * @seq: The seq_file to write data to.
3230 * @v: Unused parameter.
3231 *
3232 * This debugfs entry shows the state of the given endpoint (one is
3233 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003234 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003235static int ep_show(struct seq_file *seq, void *v)
3236{
3237 struct s3c_hsotg_ep *ep = seq->private;
3238 struct s3c_hsotg *hsotg = ep->parent;
3239 struct s3c_hsotg_req *req;
3240 void __iomem *regs = hsotg->regs;
3241 int index = ep->index;
3242 int show_limit = 15;
3243 unsigned long flags;
3244
3245 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3246 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3247
3248 /* first show the register state */
3249
3250 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003251 readl(regs + DIEPCTL(index)),
3252 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003253
3254 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003255 readl(regs + DIEPDMA(index)),
3256 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003257
3258 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003259 readl(regs + DIEPINT(index)),
3260 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003261
3262 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003263 readl(regs + DIEPTSIZ(index)),
3264 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003265
Pavel Macheka023da32013-09-30 14:56:02 +02003266 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003267 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3268 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3269
3270 seq_printf(seq, "request list (%p,%p):\n",
3271 ep->queue.next, ep->queue.prev);
3272
Lukasz Majewski22258f42012-06-14 10:02:24 +02003273 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003274
3275 list_for_each_entry(req, &ep->queue, queue) {
3276 if (--show_limit < 0) {
Pavel Macheka023da32013-09-30 14:56:02 +02003277 seq_puts(seq, "not showing more requests...\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003278 break;
3279 }
3280
3281 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3282 req == ep->req ? '*' : ' ',
3283 req, req->req.length, req->req.buf);
3284 seq_printf(seq, "%d done, res %d\n",
3285 req->req.actual, req->req.status);
3286 }
3287
Lukasz Majewski22258f42012-06-14 10:02:24 +02003288 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003289
3290 return 0;
3291}
3292
3293static int ep_open(struct inode *inode, struct file *file)
3294{
3295 return single_open(file, ep_show, inode->i_private);
3296}
3297
3298static const struct file_operations ep_fops = {
3299 .owner = THIS_MODULE,
3300 .open = ep_open,
3301 .read = seq_read,
3302 .llseek = seq_lseek,
3303 .release = single_release,
3304};
3305
3306/**
3307 * s3c_hsotg_create_debug - create debugfs directory and files
3308 * @hsotg: The driver state
3309 *
3310 * Create the debugfs files to allow the user to get information
3311 * about the state of the system. The directory name is created
3312 * with the same name as the device itself, in case we end up
3313 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003314 */
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003315static void s3c_hsotg_create_debug(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003316{
3317 struct dentry *root;
3318 unsigned epidx;
3319
3320 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3321 hsotg->debug_root = root;
3322 if (IS_ERR(root)) {
3323 dev_err(hsotg->dev, "cannot create debug root\n");
3324 return;
3325 }
3326
3327 /* create general state file */
3328
3329 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3330 hsotg, &state_fops);
3331
3332 if (IS_ERR(hsotg->debug_file))
3333 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3334
3335 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3336 hsotg, &fifo_fops);
3337
3338 if (IS_ERR(hsotg->debug_fifo))
3339 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3340
3341 /* create one file for each endpoint */
3342
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003343 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003344 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3345
3346 ep->debugfs = debugfs_create_file(ep->name, 0444,
3347 root, ep, &ep_fops);
3348
3349 if (IS_ERR(ep->debugfs))
3350 dev_err(hsotg->dev, "failed to create %s debug file\n",
3351 ep->name);
3352 }
3353}
3354
3355/**
3356 * s3c_hsotg_delete_debug - cleanup debugfs entries
3357 * @hsotg: The driver state
3358 *
3359 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003360 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05003361static void s3c_hsotg_delete_debug(struct s3c_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003362{
3363 unsigned epidx;
3364
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003365 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003366 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3367 debugfs_remove(ep->debugfs);
3368 }
3369
3370 debugfs_remove(hsotg->debug_file);
3371 debugfs_remove(hsotg->debug_fifo);
3372 debugfs_remove(hsotg->debug_root);
3373}
3374
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003375/**
3376 * s3c_hsotg_probe - probe function for hsotg driver
3377 * @pdev: The platform information for the driver
3378 */
Lukasz Majewskif026a522012-05-04 14:17:13 +02003379
Bill Pemberton41ac7b32012-11-19 13:21:48 -05003380static int s3c_hsotg_probe(struct platform_device *pdev)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003381{
Jingoo Hane01ee9f2013-07-30 17:00:51 +09003382 struct s3c_hsotg_plat *plat = dev_get_platdata(&pdev->dev);
Matt Porter74084842013-12-19 09:23:06 -05003383 struct phy *phy;
3384 struct usb_phy *uphy;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003385 struct device *dev = &pdev->dev;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003386 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003387 struct s3c_hsotg *hsotg;
3388 struct resource *res;
3389 int epnum;
3390 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003391 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003392
Sachin Kamat338edab2012-05-18 14:33:46 +05303393 hsotg = devm_kzalloc(&pdev->dev, sizeof(struct s3c_hsotg), GFP_KERNEL);
Jingoo Hand04477d2014-06-03 22:15:56 +09003394 if (!hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003395 return -ENOMEM;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003396
Matt Porter74084842013-12-19 09:23:06 -05003397 /*
3398 * Attempt to find a generic PHY, then look for an old style
3399 * USB PHY, finally fall back to pdata
3400 */
3401 phy = devm_phy_get(&pdev->dev, "usb2-phy");
Felipe Balbif4f5ba52013-03-15 10:56:19 +02003402 if (IS_ERR(phy)) {
Matt Porter74084842013-12-19 09:23:06 -05003403 uphy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3404 if (IS_ERR(uphy)) {
3405 /* Fallback for pdata */
3406 plat = dev_get_platdata(&pdev->dev);
3407 if (!plat) {
3408 dev_err(&pdev->dev,
3409 "no platform data or transceiver defined\n");
3410 return -EPROBE_DEFER;
3411 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303412 hsotg->plat = plat;
Matt Porter74084842013-12-19 09:23:06 -05003413 } else
3414 hsotg->uphy = uphy;
3415 } else
Praveen Panerib2e587d2012-11-14 15:57:16 +05303416 hsotg->phy = phy;
Praveen Panerib2e587d2012-11-14 15:57:16 +05303417
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003418 hsotg->dev = dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003419
Sachin Kamat84749c62012-09-03 16:15:18 +05303420 hsotg->clk = devm_clk_get(&pdev->dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003421 if (IS_ERR(hsotg->clk)) {
3422 dev_err(dev, "cannot get otg clock\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303423 return PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003424 }
3425
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003426 platform_set_drvdata(pdev, hsotg);
3427
3428 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003429
Thierry Reding148e1132013-01-21 11:09:22 +01003430 hsotg->regs = devm_ioremap_resource(&pdev->dev, res);
3431 if (IS_ERR(hsotg->regs)) {
3432 ret = PTR_ERR(hsotg->regs);
Sachin Kamat338edab2012-05-18 14:33:46 +05303433 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003434 }
3435
3436 ret = platform_get_irq(pdev, 0);
3437 if (ret < 0) {
3438 dev_err(dev, "cannot find IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303439 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003440 }
3441
Lukasz Majewski22258f42012-06-14 10:02:24 +02003442 spin_lock_init(&hsotg->lock);
3443
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003444 hsotg->irq = ret;
3445
Sachin Kamat338edab2012-05-18 14:33:46 +05303446 ret = devm_request_irq(&pdev->dev, hsotg->irq, s3c_hsotg_irq, 0,
3447 dev_name(dev), hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003448 if (ret < 0) {
3449 dev_err(dev, "cannot claim IRQ\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303450 goto err_clk;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003451 }
3452
3453 dev_info(dev, "regs %p, irq %d\n", hsotg->regs, hsotg->irq);
3454
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003455 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003456 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3457 hsotg->gadget.name = dev_name(dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003458
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003459 /* reset the system */
3460
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003461 clk_prepare_enable(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003462
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003463 /* regulators */
3464
3465 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3466 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3467
Sachin Kamatcd762132013-01-08 14:27:00 +05303468 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003469 hsotg->supplies);
3470 if (ret) {
3471 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303472 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003473 }
3474
3475 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3476 hsotg->supplies);
3477
3478 if (ret) {
3479 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
3480 goto err_supplies;
3481 }
3482
Matt Porterf7e504c2013-12-19 09:23:07 -05003483 /* Set default UTMI width */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003484 hsotg->phyif = GUSBCFG_PHYIF16;
Matt Porterf7e504c2013-12-19 09:23:07 -05003485
3486 /*
3487 * If using the generic PHY framework, check if the PHY bus
3488 * width is 8-bit and set the phyif appropriately.
3489 */
3490 if (hsotg->phy && (phy_get_bus_width(phy) == 8))
Dinh Nguyen47a16852014-04-14 14:13:34 -07003491 hsotg->phyif = GUSBCFG_PHYIF8;
Matt Porterf7e504c2013-12-19 09:23:07 -05003492
Matt Porter74084842013-12-19 09:23:06 -05003493 if (hsotg->phy)
3494 phy_init(hsotg->phy);
3495
Lukasz Majewski41188782012-05-04 14:17:01 +02003496 /* usb phy enable */
3497 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003498
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003499 s3c_hsotg_corereset(hsotg);
3500 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003501 s3c_hsotg_hw_cfg(hsotg);
3502
3503 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3504
3505 if (hsotg->num_of_eps == 0) {
3506 dev_err(dev, "wrong number of EPs (zero)\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003507 ret = -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003508 goto err_supplies;
3509 }
3510
3511 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3512 GFP_KERNEL);
3513 if (!eps) {
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003514 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003515 goto err_supplies;
3516 }
3517
3518 hsotg->eps = eps;
3519
3520 /* setup endpoint information */
3521
3522 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3523 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3524
3525 /* allocate EP0 request */
3526
3527 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3528 GFP_KERNEL);
3529 if (!hsotg->ctrl_req) {
3530 dev_err(dev, "failed to allocate ctrl req\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003531 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003532 goto err_ep_mem;
3533 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003534
3535 /* initialise the endpoints now the core has been initialised */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003536 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003537 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3538
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003539 /* disable power and clock */
3540
3541 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3542 hsotg->supplies);
3543 if (ret) {
3544 dev_err(hsotg->dev, "failed to disable supplies: %d\n", ret);
3545 goto err_ep_mem;
3546 }
3547
3548 s3c_hsotg_phy_disable(hsotg);
3549
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003550 ret = usb_add_gadget_udc(&pdev->dev, &hsotg->gadget);
3551 if (ret)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003552 goto err_ep_mem;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003553
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003554 s3c_hsotg_create_debug(hsotg);
3555
3556 s3c_hsotg_dump(hsotg);
3557
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003558 return 0;
3559
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003560err_ep_mem:
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003561 kfree(eps);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003562err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003563 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003564err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003565 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05303566
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003567 return ret;
3568}
3569
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003570/**
3571 * s3c_hsotg_remove - remove function for hsotg driver
3572 * @pdev: The platform information for the driver
3573 */
Bill Pembertonfb4e98a2012-11-19 13:26:20 -05003574static int s3c_hsotg_remove(struct platform_device *pdev)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003575{
3576 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3577
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003578 usb_del_gadget_udc(&hsotg->gadget);
3579
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003580 s3c_hsotg_delete_debug(hsotg);
3581
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003582 if (hsotg->driver) {
3583 /* should have been done already by driver model core */
3584 usb_gadget_unregister_driver(hsotg->driver);
3585 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003586
Lukasz Majewski41188782012-05-04 14:17:01 +02003587 s3c_hsotg_phy_disable(hsotg);
Matt Porter74084842013-12-19 09:23:06 -05003588 if (hsotg->phy)
3589 phy_exit(hsotg->phy);
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003590 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003591
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003592 return 0;
3593}
3594
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003595static int s3c_hsotg_suspend(struct platform_device *pdev, pm_message_t state)
3596{
3597 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3598 unsigned long flags;
3599 int ret = 0;
3600
3601 if (hsotg->driver)
3602 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3603 hsotg->driver->driver.name);
3604
3605 spin_lock_irqsave(&hsotg->lock, flags);
3606 s3c_hsotg_disconnect(hsotg);
3607 s3c_hsotg_phy_disable(hsotg);
3608 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3609 spin_unlock_irqrestore(&hsotg->lock, flags);
3610
3611 if (hsotg->driver) {
3612 int ep;
3613 for (ep = 0; ep < hsotg->num_of_eps; ep++)
3614 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3615
3616 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3617 hsotg->supplies);
3618 }
3619
3620 return ret;
3621}
3622
3623static int s3c_hsotg_resume(struct platform_device *pdev)
3624{
3625 struct s3c_hsotg *hsotg = platform_get_drvdata(pdev);
3626 unsigned long flags;
3627 int ret = 0;
3628
3629 if (hsotg->driver) {
3630 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3631 hsotg->driver->driver.name);
3632 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3633 hsotg->supplies);
3634 }
3635
3636 spin_lock_irqsave(&hsotg->lock, flags);
3637 hsotg->last_rst = jiffies;
3638 s3c_hsotg_phy_enable(hsotg);
3639 s3c_hsotg_core_init(hsotg);
3640 spin_unlock_irqrestore(&hsotg->lock, flags);
3641
3642 return ret;
3643}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003644
Tomasz Figac50f056c2013-06-25 17:38:23 +02003645#ifdef CONFIG_OF
3646static const struct of_device_id s3c_hsotg_of_ids[] = {
3647 { .compatible = "samsung,s3c6400-hsotg", },
Matt Porter0d33d822013-12-19 09:23:05 -05003648 { .compatible = "snps,dwc2", },
Tomasz Figac50f056c2013-06-25 17:38:23 +02003649 { /* sentinel */ }
3650};
3651MODULE_DEVICE_TABLE(of, s3c_hsotg_of_ids);
3652#endif
3653
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003654static struct platform_driver s3c_hsotg_driver = {
3655 .driver = {
3656 .name = "s3c-hsotg",
3657 .owner = THIS_MODULE,
Tomasz Figac50f056c2013-06-25 17:38:23 +02003658 .of_match_table = of_match_ptr(s3c_hsotg_of_ids),
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003659 },
3660 .probe = s3c_hsotg_probe,
Bill Pemberton76904172012-11-19 13:21:08 -05003661 .remove = s3c_hsotg_remove,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003662 .suspend = s3c_hsotg_suspend,
3663 .resume = s3c_hsotg_resume,
3664};
3665
Axel Lincc27c962011-11-27 20:16:27 +08003666module_platform_driver(s3c_hsotg_driver);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003667
3668MODULE_DESCRIPTION("Samsung S3C USB High-speed/OtG device");
3669MODULE_AUTHOR("Ben Dooks <ben@simtec.co.uk>");
3670MODULE_LICENSE("GPL");
3671MODULE_ALIAS("platform:s3c-hsotg");