blob: 367689b3d6458faf3420d11aadad548e2ccc5225 [file] [log] [blame]
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001/**
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09002 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
4 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01005 * Copyright 2008 Openmoko, Inc.
6 * Copyright 2008 Simtec Electronics
7 * Ben Dooks <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
9 *
10 * S3C USB2.0 High-speed / OtG driver
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020015 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010016
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/spinlock.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/dma-mapping.h>
23#include <linux/debugfs.h>
24#include <linux/seq_file.h>
25#include <linux/delay.h>
26#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Maurus Cuelenaeree50bf382010-07-19 09:40:50 +010028#include <linux/clk.h>
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020029#include <linux/regulator/consumer.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020030#include <linux/of_platform.h>
Matt Porter74084842013-12-19 09:23:06 -050031#include <linux/phy/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010032
33#include <linux/usb/ch9.h>
34#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053035#include <linux/usb/phy.h>
Lukasz Majewski126625e2012-05-09 13:16:53 +020036#include <linux/platform_data/s3c-hsotg.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010037
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070038#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060039#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010040
41/* conversion functions */
42static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
43{
44 return container_of(req, struct s3c_hsotg_req, req);
45}
46
47static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
48{
49 return container_of(ep, struct s3c_hsotg_ep, ep);
50}
51
Dinh Nguyen941fcce2014-11-11 11:13:33 -060052static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010053{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060054 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010055}
56
57static inline void __orr32(void __iomem *ptr, u32 val)
58{
59 writel(readl(ptr) | val, ptr);
60}
61
62static inline void __bic32(void __iomem *ptr, u32 val)
63{
64 writel(readl(ptr) & ~val, ptr);
65}
66
67/* forward decleration of functions */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060068static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010069
70/**
71 * using_dma - return the DMA status of the driver.
72 * @hsotg: The driver state.
73 *
74 * Return true if we're using DMA.
75 *
76 * Currently, we have the DMA support code worked into everywhere
77 * that needs it, but the AMBA DMA implementation in the hardware can
78 * only DMA from 32bit aligned addresses. This means that gadgets such
79 * as the CDC Ethernet cannot work as they often pass packets which are
80 * not 32bit aligned.
81 *
82 * Unfortunately the choice to use DMA or not is global to the controller
83 * and seems to be only settable when the controller is being put through
84 * a core reset. This means we either need to fix the gadgets to take
85 * account of DMA alignment, or add bounce buffers (yuerk).
86 *
87 * Until this issue is sorted out, we always return 'false'.
88 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060089static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010090{
91 return false; /* support is not complete */
92}
93
94/**
95 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
96 * @hsotg: The device state
97 * @ints: A bitmask of the interrupts to enable
98 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060099static void s3c_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100100{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200101 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100102 u32 new_gsintmsk;
103
104 new_gsintmsk = gsintmsk | ints;
105
106 if (new_gsintmsk != gsintmsk) {
107 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200108 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100109 }
110}
111
112/**
113 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
114 * @hsotg: The device state
115 * @ints: A bitmask of the interrupts to enable
116 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600117static void s3c_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100118{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200119 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100120 u32 new_gsintmsk;
121
122 new_gsintmsk = gsintmsk & ~ints;
123
124 if (new_gsintmsk != gsintmsk)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200125 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100126}
127
128/**
129 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
130 * @hsotg: The device state
131 * @ep: The endpoint index
132 * @dir_in: True if direction is in.
133 * @en: The enable value, true to enable
134 *
135 * Set or clear the mask for an individual endpoint's interrupt
136 * request.
137 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600138static void s3c_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100139 unsigned int ep, unsigned int dir_in,
140 unsigned int en)
141{
142 unsigned long flags;
143 u32 bit = 1 << ep;
144 u32 daint;
145
146 if (!dir_in)
147 bit <<= 16;
148
149 local_irq_save(flags);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200150 daint = readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100151 if (en)
152 daint |= bit;
153 else
154 daint &= ~bit;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200155 writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100156 local_irq_restore(flags);
157}
158
159/**
160 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
161 * @hsotg: The device instance.
162 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600163static void s3c_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100164{
Ben Dooks0f002d22010-05-25 05:36:50 +0100165 unsigned int ep;
166 unsigned int addr;
167 unsigned int size;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100168 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100169 u32 val;
170
Ben Dooks6d091ee72010-07-19 09:40:40 +0100171 /* set FIFO sizes to 2048/1024 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100172
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200173 writel(2048, hsotg->regs + GRXFSIZ);
Dinh Nguyen47a16852014-04-14 14:13:34 -0700174 writel((2048 << FIFOSIZE_STARTADDR_SHIFT) |
175 (1024 << FIFOSIZE_DEPTH_SHIFT), hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100176
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200177 /*
178 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100179 * block have overlapping default addresses. This also ensures
180 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200181 * known values.
182 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100183
184 /* start at the end of the GNPTXFSIZ, rounded up */
185 addr = 2048 + 1024;
Ben Dooks0f002d22010-05-25 05:36:50 +0100186
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200187 /*
Robert Baldygab203d0a2014-09-09 10:44:56 +0200188 * Because we have not enough memory to have each TX FIFO of size at
189 * least 3072 bytes (the maximum single packet size), we create four
190 * FIFOs of lenght 1024, and four of length 3072 bytes, and assing
191 * them to endpoints dynamically according to maxpacket size value of
192 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200193 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100194
Robert Baldygab203d0a2014-09-09 10:44:56 +0200195 /* 256*4=1024 bytes FIFO length */
196 size = 256;
197 for (ep = 1; ep <= 4; ep++) {
198 val = addr;
199 val |= size << FIFOSIZE_DEPTH_SHIFT;
200 WARN_ONCE(addr + size > hsotg->fifo_mem,
201 "insufficient fifo memory");
202 addr += size;
203
204 writel(val, hsotg->regs + DPTXFSIZN(ep));
205 }
206 /* 768*4=3072 bytes FIFO length */
207 size = 768;
208 for (ep = 5; ep <= 8; ep++) {
Ben Dooks0f002d22010-05-25 05:36:50 +0100209 val = addr;
Dinh Nguyen47a16852014-04-14 14:13:34 -0700210 val |= size << FIFOSIZE_DEPTH_SHIFT;
Marek Szyprowskicff9eb72014-09-09 10:44:55 +0200211 WARN_ONCE(addr + size > hsotg->fifo_mem,
212 "insufficient fifo memory");
Ben Dooks0f002d22010-05-25 05:36:50 +0100213 addr += size;
214
Dinh Nguyen47a16852014-04-14 14:13:34 -0700215 writel(val, hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100216 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100217
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200218 /*
219 * according to p428 of the design guide, we need to ensure that
220 * all fifos are flushed before continuing
221 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100222
Dinh Nguyen47a16852014-04-14 14:13:34 -0700223 writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
224 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100225
226 /* wait until the fifos are both flushed */
227 timeout = 100;
228 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200229 val = readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100230
Dinh Nguyen47a16852014-04-14 14:13:34 -0700231 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100232 break;
233
234 if (--timeout == 0) {
235 dev_err(hsotg->dev,
236 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
237 __func__, val);
238 }
239
240 udelay(1);
241 }
242
243 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100244}
245
246/**
247 * @ep: USB endpoint to allocate request for.
248 * @flags: Allocation flags
249 *
250 * Allocate a new USB request structure appropriate for the specified endpoint
251 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000252static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
253 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100254{
255 struct s3c_hsotg_req *req;
256
257 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
258 if (!req)
259 return NULL;
260
261 INIT_LIST_HEAD(&req->queue);
262
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100263 return &req->req;
264}
265
266/**
267 * is_ep_periodic - return true if the endpoint is in periodic mode.
268 * @hs_ep: The endpoint to query.
269 *
270 * Returns true if the endpoint is in periodic mode, meaning it is being
271 * used for an Interrupt or ISO transfer.
272 */
273static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
274{
275 return hs_ep->periodic;
276}
277
278/**
279 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
280 * @hsotg: The device state.
281 * @hs_ep: The endpoint for the request
282 * @hs_req: The request being processed.
283 *
284 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
285 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200286 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600287static void s3c_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100288 struct s3c_hsotg_ep *hs_ep,
289 struct s3c_hsotg_req *hs_req)
290{
291 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100292
293 /* ignore this if we're not moving any data */
294 if (hs_req->req.length == 0)
295 return;
296
Jingoo Han17d966a2013-05-11 21:14:00 +0900297 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100298}
299
300/**
301 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
302 * @hsotg: The controller state.
303 * @hs_ep: The endpoint we're going to write for.
304 * @hs_req: The request to write data for.
305 *
306 * This is called when the TxFIFO has some space in it to hold a new
307 * transmission and we have something to give it. The actual setup of
308 * the data size is done elsewhere, so all we have to do is to actually
309 * write the data.
310 *
311 * The return value is zero if there is more space (or nothing was done)
312 * otherwise -ENOSPC is returned if the FIFO space was used up.
313 *
314 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200315 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600316static int s3c_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100317 struct s3c_hsotg_ep *hs_ep,
318 struct s3c_hsotg_req *hs_req)
319{
320 bool periodic = is_ep_periodic(hs_ep);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200321 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100322 int buf_pos = hs_req->req.actual;
323 int to_write = hs_ep->size_loaded;
324 void *data;
325 int can_write;
326 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200327 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100328
329 to_write -= (buf_pos - hs_ep->last_load);
330
331 /* if there's nothing to write, get out early */
332 if (to_write == 0)
333 return 0;
334
Ben Dooks10aebc72010-07-19 09:40:44 +0100335 if (periodic && !hsotg->dedicated_fifos) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200336 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100337 int size_left;
338 int size_done;
339
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200340 /*
341 * work out how much data was loaded so we can calculate
342 * how much data is left in the fifo.
343 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100344
Dinh Nguyen47a16852014-04-14 14:13:34 -0700345 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100346
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200347 /*
348 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100349 * previous data has been completely sent.
350 */
351 if (hs_ep->fifo_load != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700352 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100353 return -ENOSPC;
354 }
355
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100356 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
357 __func__, size_left,
358 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
359
360 /* how much of the data has moved */
361 size_done = hs_ep->size_loaded - size_left;
362
363 /* how much data is left in the fifo */
364 can_write = hs_ep->fifo_load - size_done;
365 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
366 __func__, can_write);
367
368 can_write = hs_ep->fifo_size - can_write;
369 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
370 __func__, can_write);
371
372 if (can_write <= 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700373 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100374 return -ENOSPC;
375 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100376 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200377 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100378
379 can_write &= 0xffff;
380 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100381 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700382 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100383 dev_dbg(hsotg->dev,
384 "%s: no queue slots available (0x%08x)\n",
385 __func__, gnptxsts);
386
Dinh Nguyen47a16852014-04-14 14:13:34 -0700387 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100388 return -ENOSPC;
389 }
390
Dinh Nguyen47a16852014-04-14 14:13:34 -0700391 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100392 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100393 }
394
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200395 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
396
397 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
398 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100399
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200400 /*
401 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100402 * FIFO, requests of >512 cause the endpoint to get stuck with a
403 * fragment of the end of the transfer in it.
404 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200405 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100406 can_write = 512;
407
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200408 /*
409 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100410 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200411 * doing it.
412 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200413 if (to_write > max_transfer) {
414 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100415
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200416 /* it's needed only when we do not use dedicated fifos */
417 if (!hsotg->dedicated_fifos)
418 s3c_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700419 periodic ? GINTSTS_PTXFEMP :
420 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100421 }
422
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100423 /* see if we can write data */
424
425 if (to_write > can_write) {
426 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200427 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100428
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200429 /*
430 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100431 * exact number of packets.
432 *
433 * Note, we do not currently check to see if we can ever
434 * write a full packet or not to the FIFO.
435 */
436
437 if (pkt_round)
438 to_write -= pkt_round;
439
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200440 /*
441 * enable correct FIFO interrupt to alert us when there
442 * is more room left.
443 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100444
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200445 /* it's needed only when we do not use dedicated fifos */
446 if (!hsotg->dedicated_fifos)
447 s3c_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700448 periodic ? GINTSTS_PTXFEMP :
449 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100450 }
451
452 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
453 to_write, hs_req->req.length, can_write, buf_pos);
454
455 if (to_write <= 0)
456 return -ENOSPC;
457
458 hs_req->req.actual = buf_pos + to_write;
459 hs_ep->total_data += to_write;
460
461 if (periodic)
462 hs_ep->fifo_load += to_write;
463
464 to_write = DIV_ROUND_UP(to_write, 4);
465 data = hs_req->req.buf + buf_pos;
466
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500467 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100468
469 return (to_write >= can_write) ? -ENOSPC : 0;
470}
471
472/**
473 * get_ep_limit - get the maximum data legnth for this endpoint
474 * @hs_ep: The endpoint
475 *
476 * Return the maximum data that can be queued in one go on a given endpoint
477 * so that transfers that are too long can be split.
478 */
479static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
480{
481 int index = hs_ep->index;
482 unsigned maxsize;
483 unsigned maxpkt;
484
485 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700486 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
487 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100488 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100489 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900490 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700491 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900492 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100493 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100494 }
495
496 /* we made the constant loading easier above by using +1 */
497 maxpkt--;
498 maxsize--;
499
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200500 /*
501 * constrain by packet count if maxpkts*pktsize is greater
502 * than the length register size.
503 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100504
505 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
506 maxsize = maxpkt * hs_ep->ep.maxpacket;
507
508 return maxsize;
509}
510
511/**
512 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
513 * @hsotg: The controller state.
514 * @hs_ep: The endpoint to process a request for
515 * @hs_req: The request to start.
516 * @continuing: True if we are doing more for the current request.
517 *
518 * Start the given request running by setting the endpoint registers
519 * appropriately, and writing any data to the FIFOs.
520 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600521static void s3c_hsotg_start_req(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100522 struct s3c_hsotg_ep *hs_ep,
523 struct s3c_hsotg_req *hs_req,
524 bool continuing)
525{
526 struct usb_request *ureq = &hs_req->req;
527 int index = hs_ep->index;
528 int dir_in = hs_ep->dir_in;
529 u32 epctrl_reg;
530 u32 epsize_reg;
531 u32 epsize;
532 u32 ctrl;
533 unsigned length;
534 unsigned packets;
535 unsigned maxreq;
536
537 if (index != 0) {
538 if (hs_ep->req && !continuing) {
539 dev_err(hsotg->dev, "%s: active request\n", __func__);
540 WARN_ON(1);
541 return;
542 } else if (hs_ep->req != hs_req && continuing) {
543 dev_err(hsotg->dev,
544 "%s: continue different req\n", __func__);
545 WARN_ON(1);
546 return;
547 }
548 }
549
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200550 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
551 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100552
553 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
554 __func__, readl(hsotg->regs + epctrl_reg), index,
555 hs_ep->dir_in ? "in" : "out");
556
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900557 /* If endpoint is stalled, we will restart request later */
558 ctrl = readl(hsotg->regs + epctrl_reg);
559
Dinh Nguyen47a16852014-04-14 14:13:34 -0700560 if (ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900561 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
562 return;
563 }
564
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100565 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200566 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
567 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100568 if (0)
569 dev_dbg(hsotg->dev,
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300570 "REQ buf %p len %d dma %pad noi=%d zp=%d snok=%d\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900571 ureq->buf, length, &ureq->dma,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100572 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
573
574 maxreq = get_ep_limit(hs_ep);
575 if (length > maxreq) {
576 int round = maxreq % hs_ep->ep.maxpacket;
577
578 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
579 __func__, length, maxreq, round);
580
581 /* round down to multiple of packets */
582 if (round)
583 maxreq -= round;
584
585 length = maxreq;
586 }
587
588 if (length)
589 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
590 else
591 packets = 1; /* send one packet if length is zero. */
592
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200593 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
594 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
595 return;
596 }
597
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100598 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200599 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700600 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200601 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700602 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100603 else
604 epsize = 0;
605
606 if (index != 0 && ureq->zero) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200607 /*
608 * test for the packets being exactly right for the
609 * transfer
610 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100611
612 if (length == (packets * hs_ep->ep.maxpacket))
613 packets++;
614 }
615
Dinh Nguyen47a16852014-04-14 14:13:34 -0700616 epsize |= DXEPTSIZ_PKTCNT(packets);
617 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100618
619 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
620 __func__, packets, length, ureq->length, epsize, epsize_reg);
621
622 /* store the request as the current one we're doing */
623 hs_ep->req = hs_req;
624
625 /* write size / packets */
626 writel(epsize, hsotg->regs + epsize_reg);
627
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900628 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100629 unsigned int dma_reg;
630
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200631 /*
632 * write DMA address to control register, buffer already
633 * synced by s3c_hsotg_ep_queue().
634 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100635
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200636 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100637 writel(ureq->dma, hsotg->regs + dma_reg);
638
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300639 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900640 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100641 }
642
Dinh Nguyen47a16852014-04-14 14:13:34 -0700643 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
644 ctrl |= DXEPCTL_USBACTEP;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200645
646 dev_dbg(hsotg->dev, "setup req:%d\n", hsotg->setup);
647
648 /* For Setup request do not clear NAK */
649 if (hsotg->setup && index == 0)
650 hsotg->setup = 0;
651 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700652 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200653
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100654
655 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
656 writel(ctrl, hsotg->regs + epctrl_reg);
657
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200658 /*
659 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100660 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200661 * this information.
662 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100663 hs_ep->size_loaded = length;
664 hs_ep->last_load = ureq->actual;
665
666 if (dir_in && !using_dma(hsotg)) {
667 /* set these anyway, we may need them for non-periodic in */
668 hs_ep->fifo_load = 0;
669
670 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
671 }
672
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200673 /*
674 * clear the INTknTXFEmpMsk when we start request, more as a aide
675 * to debugging to see what is going on.
676 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100677 if (dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700678 writel(DIEPMSK_INTKNTXFEMPMSK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200679 hsotg->regs + DIEPINT(index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100680
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200681 /*
682 * Note, trying to clear the NAK here causes problems with transmit
683 * on the S3C6400 ending up with the TXFIFO becoming full.
684 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100685
686 /* check ep is enabled */
Dinh Nguyen47a16852014-04-14 14:13:34 -0700687 if (!(readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100688 dev_warn(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700689 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100690 index, readl(hsotg->regs + epctrl_reg));
691
Dinh Nguyen47a16852014-04-14 14:13:34 -0700692 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100693 __func__, readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200694
695 /* enable ep interrupts */
696 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100697}
698
699/**
700 * s3c_hsotg_map_dma - map the DMA memory being used for the request
701 * @hsotg: The device state.
702 * @hs_ep: The endpoint the request is on.
703 * @req: The request being processed.
704 *
705 * We've been asked to queue a request, so ensure that the memory buffer
706 * is correctly setup for DMA. If we've been passed an extant DMA address
707 * then ensure the buffer has been synced to memory. If our buffer has no
708 * DMA memory, then we map the memory and mark our request to allow us to
709 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200710 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600711static int s3c_hsotg_map_dma(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100712 struct s3c_hsotg_ep *hs_ep,
713 struct usb_request *req)
714{
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100715 struct s3c_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200716 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100717
718 /* if the length is zero, ignore the DMA data */
719 if (hs_req->req.length == 0)
720 return 0;
721
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200722 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
723 if (ret)
724 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100725
726 return 0;
727
728dma_error:
729 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
730 __func__, req->buf, req->length);
731
732 return -EIO;
733}
734
735static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
736 gfp_t gfp_flags)
737{
738 struct s3c_hsotg_req *hs_req = our_req(req);
739 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600740 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100741 bool first;
742
743 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
744 ep->name, req, req->length, req->buf, req->no_interrupt,
745 req->zero, req->short_not_ok);
746
747 /* initialise status of the request */
748 INIT_LIST_HEAD(&hs_req->queue);
749 req->actual = 0;
750 req->status = -EINPROGRESS;
751
752 /* if we're using DMA, sync the buffers as necessary */
753 if (using_dma(hs)) {
754 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
755 if (ret)
756 return ret;
757 }
758
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100759 first = list_empty(&hs_ep->queue);
760 list_add_tail(&hs_req->queue, &hs_ep->queue);
761
762 if (first)
763 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
764
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100765 return 0;
766}
767
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200768static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
769 gfp_t gfp_flags)
770{
771 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600772 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200773 unsigned long flags = 0;
774 int ret = 0;
775
776 spin_lock_irqsave(&hs->lock, flags);
777 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
778 spin_unlock_irqrestore(&hs->lock, flags);
779
780 return ret;
781}
782
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100783static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
784 struct usb_request *req)
785{
786 struct s3c_hsotg_req *hs_req = our_req(req);
787
788 kfree(hs_req);
789}
790
791/**
792 * s3c_hsotg_complete_oursetup - setup completion callback
793 * @ep: The endpoint the request was on.
794 * @req: The request completed.
795 *
796 * Called on completion of any requests the driver itself
797 * submitted that need cleaning up.
798 */
799static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
800 struct usb_request *req)
801{
802 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600803 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100804
805 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
806
807 s3c_hsotg_ep_free_request(ep, req);
808}
809
810/**
811 * ep_from_windex - convert control wIndex value to endpoint
812 * @hsotg: The driver state.
813 * @windex: The control request wIndex field (in host order).
814 *
815 * Convert the given wIndex into a pointer to an driver endpoint
816 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200817 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600818static struct s3c_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100819 u32 windex)
820{
821 struct s3c_hsotg_ep *ep = &hsotg->eps[windex & 0x7F];
822 int dir = (windex & USB_DIR_IN) ? 1 : 0;
823 int idx = windex & 0x7F;
824
825 if (windex >= 0x100)
826 return NULL;
827
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200828 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100829 return NULL;
830
831 if (idx && ep->dir_in != dir)
832 return NULL;
833
834 return ep;
835}
836
837/**
838 * s3c_hsotg_send_reply - send reply to control request
839 * @hsotg: The device state
840 * @ep: Endpoint 0
841 * @buff: Buffer for request
842 * @length: Length of reply.
843 *
844 * Create a request and queue it on the given endpoint. This is useful as
845 * an internal method of sending replies to certain control requests, etc.
846 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600847static int s3c_hsotg_send_reply(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100848 struct s3c_hsotg_ep *ep,
849 void *buff,
850 int length)
851{
852 struct usb_request *req;
853 int ret;
854
855 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
856
857 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
858 hsotg->ep0_reply = req;
859 if (!req) {
860 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
861 return -ENOMEM;
862 }
863
864 req->buf = hsotg->ep0_buff;
865 req->length = length;
866 req->zero = 1; /* always do zero-length final transfer */
867 req->complete = s3c_hsotg_complete_oursetup;
868
869 if (length)
870 memcpy(req->buf, buff, length);
871 else
872 ep->sent_zlp = 1;
873
874 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
875 if (ret) {
876 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
877 return ret;
878 }
879
880 return 0;
881}
882
883/**
884 * s3c_hsotg_process_req_status - process request GET_STATUS
885 * @hsotg: The device state
886 * @ctrl: USB control request
887 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600888static int s3c_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100889 struct usb_ctrlrequest *ctrl)
890{
891 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
892 struct s3c_hsotg_ep *ep;
893 __le16 reply;
894 int ret;
895
896 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
897
898 if (!ep0->dir_in) {
899 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
900 return -EINVAL;
901 }
902
903 switch (ctrl->bRequestType & USB_RECIP_MASK) {
904 case USB_RECIP_DEVICE:
905 reply = cpu_to_le16(0); /* bit 0 => self powered,
906 * bit 1 => remote wakeup */
907 break;
908
909 case USB_RECIP_INTERFACE:
910 /* currently, the data result should be zero */
911 reply = cpu_to_le16(0);
912 break;
913
914 case USB_RECIP_ENDPOINT:
915 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
916 if (!ep)
917 return -ENOENT;
918
919 reply = cpu_to_le16(ep->halted ? 1 : 0);
920 break;
921
922 default:
923 return 0;
924 }
925
926 if (le16_to_cpu(ctrl->wLength) != 2)
927 return -EINVAL;
928
929 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
930 if (ret) {
931 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
932 return ret;
933 }
934
935 return 1;
936}
937
938static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
939
940/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900941 * get_ep_head - return the first request on the endpoint
942 * @hs_ep: The controller endpoint to get
943 *
944 * Get the first request on the endpoint.
945 */
946static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
947{
948 if (list_empty(&hs_ep->queue))
949 return NULL;
950
951 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
952}
953
954/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100955 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
956 * @hsotg: The device state
957 * @ctrl: USB control request
958 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600959static int s3c_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100960 struct usb_ctrlrequest *ctrl)
961{
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +0900962 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900963 struct s3c_hsotg_req *hs_req;
964 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100965 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
966 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +0900967 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200968 bool halted;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100969
970 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
971 __func__, set ? "SET" : "CLEAR");
972
973 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
974 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
975 if (!ep) {
976 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
977 __func__, le16_to_cpu(ctrl->wIndex));
978 return -ENOENT;
979 }
980
981 switch (le16_to_cpu(ctrl->wValue)) {
982 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200983 halted = ep->halted;
984
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100985 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +0900986
987 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
988 if (ret) {
989 dev_err(hsotg->dev,
990 "%s: failed to send reply\n", __func__);
991 return ret;
992 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900993
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200994 /*
995 * we have to complete all requests for ep if it was
996 * halted, and the halt was cleared by CLEAR_FEATURE
997 */
998
999 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001000 /*
1001 * If we have request in progress,
1002 * then complete it
1003 */
1004 if (ep->req) {
1005 hs_req = ep->req;
1006 ep->req = NULL;
1007 list_del_init(&hs_req->queue);
Michal Sojka304f7e52014-09-24 22:43:19 +02001008 usb_gadget_giveback_request(&ep->ep,
1009 &hs_req->req);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001010 }
1011
1012 /* If we have pending request, then start it */
1013 restart = !list_empty(&ep->queue);
1014 if (restart) {
1015 hs_req = get_ep_head(ep);
1016 s3c_hsotg_start_req(hsotg, ep,
1017 hs_req, false);
1018 }
1019 }
1020
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001021 break;
1022
1023 default:
1024 return -ENOENT;
1025 }
1026 } else
1027 return -ENOENT; /* currently only deal with endpoint */
1028
1029 return 1;
1030}
1031
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001032static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
1033static void s3c_hsotg_disconnect(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001034
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001035/**
Robert Baldygac9f721b2014-01-14 08:36:00 +01001036 * s3c_hsotg_stall_ep0 - stall ep0
1037 * @hsotg: The device state
1038 *
1039 * Set stall for ep0 as response for setup request.
1040 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001041static void s3c_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001042{
Robert Baldygac9f721b2014-01-14 08:36:00 +01001043 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1044 u32 reg;
1045 u32 ctrl;
1046
1047 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1048 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1049
1050 /*
1051 * DxEPCTL_Stall will be cleared by EP once it has
1052 * taken effect, so no need to clear later.
1053 */
1054
1055 ctrl = readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001056 ctrl |= DXEPCTL_STALL;
1057 ctrl |= DXEPCTL_CNAK;
Robert Baldygac9f721b2014-01-14 08:36:00 +01001058 writel(ctrl, hsotg->regs + reg);
1059
1060 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001061 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Robert Baldygac9f721b2014-01-14 08:36:00 +01001062 ctrl, reg, readl(hsotg->regs + reg));
1063
1064 /*
1065 * complete won't be called, so we enqueue
1066 * setup request here
1067 */
1068 s3c_hsotg_enqueue_setup(hsotg);
1069}
1070
1071/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001072 * s3c_hsotg_process_control - process a control request
1073 * @hsotg: The device state
1074 * @ctrl: The control request received
1075 *
1076 * The controller has received the SETUP phase of a control request, and
1077 * needs to work out what to do next (and whether to pass it on to the
1078 * gadget driver).
1079 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001080static void s3c_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001081 struct usb_ctrlrequest *ctrl)
1082{
1083 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1084 int ret = 0;
1085 u32 dcfg;
1086
1087 ep0->sent_zlp = 0;
1088
1089 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1090 ctrl->bRequest, ctrl->bRequestType,
1091 ctrl->wValue, ctrl->wLength);
1092
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001093 /*
1094 * record the direction of the request, for later use when enquing
1095 * packets onto EP0.
1096 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001097
1098 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1099 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1100
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001101 /*
1102 * if we've no data with this request, then the last part of the
1103 * transaction is going to implicitly be IN.
1104 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001105 if (ctrl->wLength == 0)
1106 ep0->dir_in = 1;
1107
1108 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1109 switch (ctrl->bRequest) {
1110 case USB_REQ_SET_ADDRESS:
Robert Baldygad18f71162013-11-21 13:49:18 +01001111 s3c_hsotg_disconnect(hsotg);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001112 dcfg = readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001113 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001114 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1115 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001116 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001117
1118 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1119
1120 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1121 return;
1122
1123 case USB_REQ_GET_STATUS:
1124 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1125 break;
1126
1127 case USB_REQ_CLEAR_FEATURE:
1128 case USB_REQ_SET_FEATURE:
1129 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1130 break;
1131 }
1132 }
1133
1134 /* as a fallback, try delivering it to the driver to deal with */
1135
1136 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001137 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001139 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001140 if (ret < 0)
1141 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1142 }
1143
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001144 /*
1145 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001146 * so respond with a STALL for the status stage to indicate failure.
1147 */
1148
Robert Baldygac9f721b2014-01-14 08:36:00 +01001149 if (ret < 0)
1150 s3c_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001151}
1152
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001153/**
1154 * s3c_hsotg_complete_setup - completion of a setup transfer
1155 * @ep: The endpoint the request was on.
1156 * @req: The request completed.
1157 *
1158 * Called on completion of any requests the driver itself submitted for
1159 * EP0 setup packets
1160 */
1161static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1162 struct usb_request *req)
1163{
1164 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001165 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001166
1167 if (req->status < 0) {
1168 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1169 return;
1170 }
1171
Robert Baldyga93f599f2013-11-21 13:49:17 +01001172 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001173 if (req->actual == 0)
1174 s3c_hsotg_enqueue_setup(hsotg);
1175 else
1176 s3c_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001177 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001178}
1179
1180/**
1181 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1182 * @hsotg: The device state.
1183 *
1184 * Enqueue a request on EP0 if necessary to received any SETUP packets
1185 * received from the host.
1186 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001187static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001188{
1189 struct usb_request *req = hsotg->ctrl_req;
1190 struct s3c_hsotg_req *hs_req = our_req(req);
1191 int ret;
1192
1193 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1194
1195 req->zero = 0;
1196 req->length = 8;
1197 req->buf = hsotg->ctrl_buff;
1198 req->complete = s3c_hsotg_complete_setup;
1199
1200 if (!list_empty(&hs_req->queue)) {
1201 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1202 return;
1203 }
1204
1205 hsotg->eps[0].dir_in = 0;
1206
1207 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1208 if (ret < 0) {
1209 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001210 /*
1211 * Don't think there's much we can do other than watch the
1212 * driver fail.
1213 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001214 }
1215}
1216
1217/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001218 * s3c_hsotg_complete_request - complete a request given to us
1219 * @hsotg: The device state.
1220 * @hs_ep: The endpoint the request was on.
1221 * @hs_req: The request to complete.
1222 * @result: The result code (0 => Ok, otherwise errno)
1223 *
1224 * The given request has finished, so call the necessary completion
1225 * if it has one and then look to see if we can start a new request
1226 * on the endpoint.
1227 *
1228 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001229 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001230static void s3c_hsotg_complete_request(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001231 struct s3c_hsotg_ep *hs_ep,
1232 struct s3c_hsotg_req *hs_req,
1233 int result)
1234{
1235 bool restart;
1236
1237 if (!hs_req) {
1238 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1239 return;
1240 }
1241
1242 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1243 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1244
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001245 /*
1246 * only replace the status if we've not already set an error
1247 * from a previous transaction
1248 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001249
1250 if (hs_req->req.status == -EINPROGRESS)
1251 hs_req->req.status = result;
1252
1253 hs_ep->req = NULL;
1254 list_del_init(&hs_req->queue);
1255
1256 if (using_dma(hsotg))
1257 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1258
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001259 /*
1260 * call the complete request with the locks off, just in case the
1261 * request tries to queue more work for this endpoint.
1262 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001263
1264 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001265 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001266 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001267 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001268 }
1269
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001270 /*
1271 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001272 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001273 * so be careful when doing this.
1274 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001275
1276 if (!hs_ep->req && result >= 0) {
1277 restart = !list_empty(&hs_ep->queue);
1278 if (restart) {
1279 hs_req = get_ep_head(hs_ep);
1280 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1281 }
1282 }
1283}
1284
1285/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001286 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1287 * @hsotg: The device state.
1288 * @ep_idx: The endpoint index for the data
1289 * @size: The size of data in the fifo, in bytes
1290 *
1291 * The FIFO status shows there is data to read from the FIFO for a given
1292 * endpoint, so sort out whether we need to read the data into a request
1293 * that has been made for that endpoint.
1294 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001295static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001296{
1297 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1298 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001299 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001300 int to_read;
1301 int max_req;
1302 int read_ptr;
1303
Lukasz Majewski22258f42012-06-14 10:02:24 +02001304
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001305 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001306 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001307 int ptr;
1308
1309 dev_warn(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001310 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001311 __func__, size, ep_idx, epctl);
1312
1313 /* dump the data from the FIFO, we've nothing we can do */
1314 for (ptr = 0; ptr < size; ptr += 4)
1315 (void)readl(fifo);
1316
1317 return;
1318 }
1319
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001320 to_read = size;
1321 read_ptr = hs_req->req.actual;
1322 max_req = hs_req->req.length - read_ptr;
1323
Ben Dooksa33e7132010-07-19 09:40:49 +01001324 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1325 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1326
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001327 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001328 /*
1329 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001330 * to deal with in this request.
1331 */
1332
1333 /* currently we don't deal this */
1334 WARN_ON_ONCE(1);
1335 }
1336
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001337 hs_ep->total_data += to_read;
1338 hs_req->req.actual += to_read;
1339 to_read = DIV_ROUND_UP(to_read, 4);
1340
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001341 /*
1342 * note, we might over-write the buffer end by 3 bytes depending on
1343 * alignment of the data.
1344 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001345 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001346}
1347
1348/**
1349 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1350 * @hsotg: The device instance
1351 * @req: The request currently on this endpoint
1352 *
1353 * Generate a zero-length IN packet request for terminating a SETUP
1354 * transaction.
1355 *
1356 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001357 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001358 * the TxFIFO.
1359 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001360static void s3c_hsotg_send_zlp(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001361 struct s3c_hsotg_req *req)
1362{
1363 u32 ctrl;
1364
1365 if (!req) {
1366 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1367 return;
1368 }
1369
1370 if (req->req.length == 0) {
1371 hsotg->eps[0].sent_zlp = 1;
1372 s3c_hsotg_enqueue_setup(hsotg);
1373 return;
1374 }
1375
1376 hsotg->eps[0].dir_in = 1;
1377 hsotg->eps[0].sent_zlp = 1;
1378
1379 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1380
1381 /* issue a zero-sized packet to terminate this */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001382 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1383 DXEPTSIZ_XFERSIZE(0), hsotg->regs + DIEPTSIZ(0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001384
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001385 ctrl = readl(hsotg->regs + DIEPCTL0);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001386 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1387 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1388 ctrl |= DXEPCTL_USBACTEP;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001389 writel(ctrl, hsotg->regs + DIEPCTL0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001390}
1391
1392/**
1393 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1394 * @hsotg: The device instance
1395 * @epnum: The endpoint received from
1396 * @was_setup: Set if processing a SetupDone event.
1397 *
1398 * The RXFIFO has delivered an OutDone event, which means that the data
1399 * transfer for an OUT endpoint has been completed, either by a short
1400 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001401 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001402static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001403 int epnum, bool was_setup)
1404{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001405 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001406 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1407 struct s3c_hsotg_req *hs_req = hs_ep->req;
1408 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001409 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001410 int result = 0;
1411
1412 if (!hs_req) {
1413 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1414 return;
1415 }
1416
1417 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001418 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001419
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001420 /*
1421 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001422 * is left in the endpoint size register and then working it
1423 * out from the amount we loaded for the transfer.
1424 *
1425 * We need to do this as DMA pointers are always 32bit aligned
1426 * so may overshoot/undershoot the transfer.
1427 */
1428
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001429 size_done = hs_ep->size_loaded - size_left;
1430 size_done += hs_ep->last_load;
1431
1432 req->actual = size_done;
1433 }
1434
Ben Dooksa33e7132010-07-19 09:40:49 +01001435 /* if there is more request to do, schedule new transfer */
1436 if (req->actual < req->length && size_left == 0) {
1437 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1438 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001439 } else if (epnum == 0) {
1440 /*
1441 * After was_setup = 1 =>
1442 * set CNAK for non Setup requests
1443 */
1444 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001445 }
1446
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001447 if (req->actual < req->length && req->short_not_ok) {
1448 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1449 __func__, req->actual, req->length);
1450
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001451 /*
1452 * todo - what should we return here? there's no one else
1453 * even bothering to check the status.
1454 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001455 }
1456
1457 if (epnum == 0) {
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001458 /*
1459 * Condition req->complete != s3c_hsotg_complete_setup says:
1460 * send ZLP when we have an asynchronous request from gadget
1461 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001462 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1463 s3c_hsotg_send_zlp(hsotg, hs_req);
1464 }
1465
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001466 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001467}
1468
1469/**
1470 * s3c_hsotg_read_frameno - read current frame number
1471 * @hsotg: The device instance
1472 *
1473 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001474 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001475static u32 s3c_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001476{
1477 u32 dsts;
1478
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001479 dsts = readl(hsotg->regs + DSTS);
1480 dsts &= DSTS_SOFFN_MASK;
1481 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001482
1483 return dsts;
1484}
1485
1486/**
1487 * s3c_hsotg_handle_rx - RX FIFO has data
1488 * @hsotg: The device instance
1489 *
1490 * The IRQ handler has detected that the RX FIFO has some data in it
1491 * that requires processing, so find out what is in there and do the
1492 * appropriate read.
1493 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001494 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001495 * chunks, so if you have x packets received on an endpoint you'll get x
1496 * FIFO events delivered, each with a packet's worth of data in it.
1497 *
1498 * When using DMA, we should not be processing events from the RXFIFO
1499 * as the actual data should be sent to the memory directly and we turn
1500 * on the completion interrupts to get notifications of transfer completion.
1501 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001502static void s3c_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001503{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001504 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001505 u32 epnum, status, size;
1506
1507 WARN_ON(using_dma(hsotg));
1508
Dinh Nguyen47a16852014-04-14 14:13:34 -07001509 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1510 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001511
Dinh Nguyen47a16852014-04-14 14:13:34 -07001512 size = grxstsr & GRXSTS_BYTECNT_MASK;
1513 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001514
1515 if (1)
1516 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1517 __func__, grxstsr, size, epnum);
1518
Dinh Nguyen47a16852014-04-14 14:13:34 -07001519 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1520 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1521 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001522 break;
1523
Dinh Nguyen47a16852014-04-14 14:13:34 -07001524 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001525 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1526 s3c_hsotg_read_frameno(hsotg));
1527
1528 if (!using_dma(hsotg))
1529 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1530 break;
1531
Dinh Nguyen47a16852014-04-14 14:13:34 -07001532 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001533 dev_dbg(hsotg->dev,
1534 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1535 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001536 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001537
1538 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1539 break;
1540
Dinh Nguyen47a16852014-04-14 14:13:34 -07001541 case GRXSTS_PKTSTS_OUTRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001542 s3c_hsotg_rx_data(hsotg, epnum, size);
1543 break;
1544
Dinh Nguyen47a16852014-04-14 14:13:34 -07001545 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001546 dev_dbg(hsotg->dev,
1547 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1548 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001549 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001550
1551 s3c_hsotg_rx_data(hsotg, epnum, size);
1552 break;
1553
1554 default:
1555 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1556 __func__, grxstsr);
1557
1558 s3c_hsotg_dump(hsotg);
1559 break;
1560 }
1561}
1562
1563/**
1564 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1565 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001566 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001567static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1568{
1569 switch (mps) {
1570 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001571 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001572 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001573 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001574 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001575 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001576 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001577 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001578 }
1579
1580 /* bad max packet size, warn and return invalid result */
1581 WARN_ON(1);
1582 return (u32)-1;
1583}
1584
1585/**
1586 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1587 * @hsotg: The driver state.
1588 * @ep: The index number of the endpoint
1589 * @mps: The maximum packet size in bytes
1590 *
1591 * Configure the maximum packet size for the given endpoint, updating
1592 * the hardware control registers to reflect this.
1593 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001594static void s3c_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001595 unsigned int ep, unsigned int mps)
1596{
1597 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1598 void __iomem *regs = hsotg->regs;
1599 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001600 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001601 u32 reg;
1602
1603 if (ep == 0) {
1604 /* EP0 is a special case */
1605 mpsval = s3c_hsotg_ep0_mps(mps);
1606 if (mpsval > 3)
1607 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001608 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001609 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001610 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001611 mpsval = mps & DXEPCTL_MPS_MASK;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001612 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001613 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001614 mcval = ((mps >> 11) & 0x3) + 1;
1615 hs_ep->mc = mcval;
1616 if (mcval > 3)
1617 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001618 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001619 }
1620
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001621 /*
1622 * update both the in and out endpoint controldir_ registers, even
1623 * if one of the directions may not be in use.
1624 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001625
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001626 reg = readl(regs + DIEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001627 reg &= ~DXEPCTL_MPS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001628 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001629 writel(reg, regs + DIEPCTL(ep));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001630
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001631 if (ep) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001632 reg = readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001633 reg &= ~DXEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001634 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001635 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001636 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001637
1638 return;
1639
1640bad_mps:
1641 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1642}
1643
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001644/**
1645 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1646 * @hsotg: The driver state
1647 * @idx: The index for the endpoint (0..15)
1648 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001649static void s3c_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001650{
1651 int timeout;
1652 int val;
1653
Dinh Nguyen47a16852014-04-14 14:13:34 -07001654 writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001655 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001656
1657 /* wait until the fifo is flushed */
1658 timeout = 100;
1659
1660 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001661 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001662
Dinh Nguyen47a16852014-04-14 14:13:34 -07001663 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001664 break;
1665
1666 if (--timeout == 0) {
1667 dev_err(hsotg->dev,
1668 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1669 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02001670 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001671 }
1672
1673 udelay(1);
1674 }
1675}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001676
1677/**
1678 * s3c_hsotg_trytx - check to see if anything needs transmitting
1679 * @hsotg: The driver state
1680 * @hs_ep: The driver endpoint to check.
1681 *
1682 * Check to see if there is a request that has data to send, and if so
1683 * make an attempt to write data into the FIFO.
1684 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001685static int s3c_hsotg_trytx(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001686 struct s3c_hsotg_ep *hs_ep)
1687{
1688 struct s3c_hsotg_req *hs_req = hs_ep->req;
1689
Robert Baldygaafcf4162013-09-19 11:50:19 +02001690 if (!hs_ep->dir_in || !hs_req) {
1691 /**
1692 * if request is not enqueued, we disable interrupts
1693 * for endpoints, excepting ep0
1694 */
1695 if (hs_ep->index != 0)
1696 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1697 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001698 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001699 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001700
1701 if (hs_req->req.actual < hs_req->req.length) {
1702 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1703 hs_ep->index);
1704 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1705 }
1706
1707 return 0;
1708}
1709
1710/**
1711 * s3c_hsotg_complete_in - complete IN transfer
1712 * @hsotg: The device state.
1713 * @hs_ep: The endpoint that has just completed.
1714 *
1715 * An IN transfer has been completed, update the transfer's state and then
1716 * call the relevant completion routines.
1717 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001718static void s3c_hsotg_complete_in(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001719 struct s3c_hsotg_ep *hs_ep)
1720{
1721 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001722 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001723 int size_left, size_done;
1724
1725 if (!hs_req) {
1726 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1727 return;
1728 }
1729
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001730 /* Finish ZLP handling for IN EP0 transactions */
1731 if (hsotg->eps[0].sent_zlp) {
1732 dev_dbg(hsotg->dev, "zlp packet received\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001733 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001734 return;
1735 }
1736
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001737 /*
1738 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001739 * in the endpoint size register and then working it out from
1740 * the amount we loaded for the transfer.
1741 *
1742 * We do this even for DMA, as the transfer may have incremented
1743 * past the end of the buffer (DMA transfers are always 32bit
1744 * aligned).
1745 */
1746
Dinh Nguyen47a16852014-04-14 14:13:34 -07001747 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001748
1749 size_done = hs_ep->size_loaded - size_left;
1750 size_done += hs_ep->last_load;
1751
1752 if (hs_req->req.actual != size_done)
1753 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1754 __func__, hs_req->req.actual, size_done);
1755
1756 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001757 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1758 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001759
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001760 /*
1761 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1762 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1763 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1764 * inform the host that no more data is available.
1765 * The state of req.zero member is checked to be sure that the value to
1766 * send is smaller than wValue expected from host.
1767 * Check req.length to NOT send another ZLP when the current one is
1768 * under completion (the one for which this completion has been called).
1769 */
1770 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1771 hs_req->req.length == hs_req->req.actual &&
1772 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1773
1774 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1775 s3c_hsotg_send_zlp(hsotg, hs_req);
1776
1777 return;
1778 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001779
1780 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1781 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1782 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1783 } else
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001784 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001785}
1786
1787/**
1788 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1789 * @hsotg: The driver state
1790 * @idx: The index for the endpoint (0..15)
1791 * @dir_in: Set if this is an IN endpoint
1792 *
1793 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001794 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001795static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001796 int dir_in)
1797{
1798 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001799 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1800 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1801 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001802 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02001803 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001804
1805 ints = readl(hsotg->regs + epint_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02001806 ctrl = readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001807
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001808 /* Clear endpoint interrupts */
1809 writel(ints, hsotg->regs + epint_reg);
1810
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001811 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1812 __func__, idx, dir_in ? "in" : "out", ints);
1813
Dinh Nguyen47a16852014-04-14 14:13:34 -07001814 if (ints & DXEPINT_XFERCOMPL) {
Robert Baldyga1479e842013-10-09 08:41:57 +02001815 if (hs_ep->isochronous && hs_ep->interval == 1) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001816 if (ctrl & DXEPCTL_EOFRNUM)
1817 ctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001818 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001819 ctrl |= DXEPCTL_SETODDFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001820 writel(ctrl, hsotg->regs + epctl_reg);
1821 }
1822
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001823 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001824 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001825 __func__, readl(hsotg->regs + epctl_reg),
1826 readl(hsotg->regs + epsiz_reg));
1827
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001828 /*
1829 * we get OutDone from the FIFO, so we only need to look
1830 * at completing IN requests here
1831 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001832 if (dir_in) {
1833 s3c_hsotg_complete_in(hsotg, hs_ep);
1834
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001835 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001836 s3c_hsotg_enqueue_setup(hsotg);
1837 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001838 /*
1839 * We're using DMA, we need to fire an OutDone here
1840 * as we ignore the RXFIFO.
1841 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001842
1843 s3c_hsotg_handle_outdone(hsotg, idx, false);
1844 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001845 }
1846
Dinh Nguyen47a16852014-04-14 14:13:34 -07001847 if (ints & DXEPINT_EPDISBLD) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001848 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001849
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001850 if (dir_in) {
1851 int epctl = readl(hsotg->regs + epctl_reg);
1852
Robert Baldygab203d0a2014-09-09 10:44:56 +02001853 s3c_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001854
Dinh Nguyen47a16852014-04-14 14:13:34 -07001855 if ((epctl & DXEPCTL_STALL) &&
1856 (epctl & DXEPCTL_EPTYPE_BULK)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001857 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001858
Dinh Nguyen47a16852014-04-14 14:13:34 -07001859 dctl |= DCTL_CGNPINNAK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001860 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001861 }
1862 }
1863 }
1864
Dinh Nguyen47a16852014-04-14 14:13:34 -07001865 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001866 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001867
Dinh Nguyen47a16852014-04-14 14:13:34 -07001868 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001869 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1870
1871 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001872 /*
1873 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001874 * setup packet. In non-DMA mode we'd get this
1875 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001876 * the setup here.
1877 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001878
1879 if (dir_in)
1880 WARN_ON_ONCE(1);
1881 else
1882 s3c_hsotg_handle_outdone(hsotg, 0, true);
1883 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001884 }
1885
Dinh Nguyen47a16852014-04-14 14:13:34 -07001886 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001887 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001888
Robert Baldyga1479e842013-10-09 08:41:57 +02001889 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001890 /* not sure if this is important, but we'll clear it anyway */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001891 if (ints & DIEPMSK_INTKNTXFEMPMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001892 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1893 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001894 }
1895
1896 /* this probably means something bad is happening */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001897 if (ints & DIEPMSK_INTKNEPMISMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001898 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1899 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001900 }
Ben Dooks10aebc72010-07-19 09:40:44 +01001901
1902 /* FIFO has space or is empty (see GAHBCFG) */
1903 if (hsotg->dedicated_fifos &&
Dinh Nguyen47a16852014-04-14 14:13:34 -07001904 ints & DIEPMSK_TXFIFOEMPTY) {
Ben Dooks10aebc72010-07-19 09:40:44 +01001905 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1906 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09001907 if (!using_dma(hsotg))
1908 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01001909 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001910 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001911}
1912
1913/**
1914 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
1915 * @hsotg: The device state.
1916 *
1917 * Handle updating the device settings after the enumeration phase has
1918 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001919 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001920static void s3c_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001921{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001922 u32 dsts = readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09001923 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001924
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001925 /*
1926 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001927 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001928 * we connected at.
1929 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001930
1931 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
1932
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001933 /*
1934 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001935 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001936 * not advertise a 64byte MPS on EP0.
1937 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001938
1939 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001940 switch (dsts & DSTS_ENUMSPD_MASK) {
1941 case DSTS_ENUMSPD_FS:
1942 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001943 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001944 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01001945 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001946 break;
1947
Dinh Nguyen47a16852014-04-14 14:13:34 -07001948 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001949 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01001951 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001952 break;
1953
Dinh Nguyen47a16852014-04-14 14:13:34 -07001954 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001955 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001956 /*
1957 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001958 * moment, and the documentation seems to imply that it isn't
1959 * supported by the PHYs on some of the devices.
1960 */
1961 break;
1962 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02001963 dev_info(hsotg->dev, "new device is %s\n",
1964 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001965
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001966 /*
1967 * we should now know the maximum packet size for an
1968 * endpoint, so set the endpoints to a default value.
1969 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001970
1971 if (ep0_mps) {
1972 int i;
1973 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001974 for (i = 1; i < hsotg->num_of_eps; i++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001975 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
1976 }
1977
1978 /* ensure after enumeration our EP0 is active */
1979
1980 s3c_hsotg_enqueue_setup(hsotg);
1981
1982 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001983 readl(hsotg->regs + DIEPCTL0),
1984 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001985}
1986
1987/**
1988 * kill_all_requests - remove all requests from the endpoint's queue
1989 * @hsotg: The device state.
1990 * @ep: The endpoint the requests may be on.
1991 * @result: The result code to use.
1992 * @force: Force removal of any current requests
1993 *
1994 * Go through the requests on the given endpoint and mark them
1995 * completed with the given result code.
1996 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001997static void kill_all_requests(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001998 struct s3c_hsotg_ep *ep,
1999 int result, bool force)
2000{
2001 struct s3c_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002002 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002003
2004 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002005 /*
2006 * currently, we can't do much about an already
2007 * running request on an in endpoint
2008 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002009
2010 if (ep->req == req && ep->dir_in && !force)
2011 continue;
2012
2013 s3c_hsotg_complete_request(hsotg, ep, req,
2014 result);
2015 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02002016 if (!hsotg->dedicated_fifos)
2017 return;
2018 size = (readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
2019 if (size < ep->fifo_size)
2020 s3c_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002021}
2022
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002023/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002024 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002025 * @hsotg: The device state.
2026 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002027 * The device has been disconnected. Remove all current
2028 * transactions and signal the gadget driver that this
2029 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002030 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002031static void s3c_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002032{
2033 unsigned ep;
2034
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002035 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002036 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2037
2038 call_gadget(hsotg, disconnect);
2039}
2040
2041/**
2042 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2043 * @hsotg: The device state:
2044 * @periodic: True if this is a periodic FIFO interrupt
2045 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002046static void s3c_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002047{
2048 struct s3c_hsotg_ep *ep;
2049 int epno, ret;
2050
2051 /* look through for any more data to transmit */
2052
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002053 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002054 ep = &hsotg->eps[epno];
2055
2056 if (!ep->dir_in)
2057 continue;
2058
2059 if ((periodic && !ep->periodic) ||
2060 (!periodic && ep->periodic))
2061 continue;
2062
2063 ret = s3c_hsotg_trytx(hsotg, ep);
2064 if (ret < 0)
2065 break;
2066 }
2067}
2068
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002069/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002070#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2071 GINTSTS_PTXFEMP | \
2072 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002073
2074/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002075 * s3c_hsotg_corereset - issue softreset to the core
2076 * @hsotg: The device state
2077 *
2078 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002079 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002080static int s3c_hsotg_corereset(struct dwc2_hsotg *hsotg)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002081{
2082 int timeout;
2083 u32 grstctl;
2084
2085 dev_dbg(hsotg->dev, "resetting core\n");
2086
2087 /* issue soft reset */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002088 writel(GRSTCTL_CSFTRST, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002089
Du, Changbin2868fea2012-07-24 08:19:25 +08002090 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002091 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002092 grstctl = readl(hsotg->regs + GRSTCTL);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002093 } while ((grstctl & GRSTCTL_CSFTRST) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002094
Dinh Nguyen47a16852014-04-14 14:13:34 -07002095 if (grstctl & GRSTCTL_CSFTRST) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002096 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2097 return -EINVAL;
2098 }
2099
Du, Changbin2868fea2012-07-24 08:19:25 +08002100 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002101
2102 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002103 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002104
2105 if (timeout-- < 0) {
2106 dev_info(hsotg->dev,
2107 "%s: reset failed, GRSTCTL=%08x\n",
2108 __func__, grstctl);
2109 return -ETIMEDOUT;
2110 }
2111
Dinh Nguyen47a16852014-04-14 14:13:34 -07002112 if (!(grstctl & GRSTCTL_AHBIDLE))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002113 continue;
2114
2115 break; /* reset done */
2116 }
2117
2118 dev_dbg(hsotg->dev, "reset successful\n");
2119 return 0;
2120}
2121
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002122/**
2123 * s3c_hsotg_core_init - issue softreset to the core
2124 * @hsotg: The device state
2125 *
2126 * Issue a soft reset to the core, and await the core finishing it.
2127 */
Dinh Nguyen510ffaa2014-11-11 11:13:36 -06002128void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002129{
2130 s3c_hsotg_corereset(hsotg);
2131
2132 /*
2133 * we must now enable ep0 ready for host detection and then
2134 * set configuration.
2135 */
2136
2137 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002138 writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002139 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002140
2141 s3c_hsotg_init_fifo(hsotg);
2142
Dinh Nguyen47a16852014-04-14 14:13:34 -07002143 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002144
Dinh Nguyen47a16852014-04-14 14:13:34 -07002145 writel(1 << 18 | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002146
2147 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002148 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002149
2150 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002151 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002152
Dinh Nguyen47a16852014-04-14 14:13:34 -07002153 writel(GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
2154 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
2155 GINTSTS_CONIDSTSCHNG | GINTSTS_USBRST |
2156 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
2157 GINTSTS_USBSUSP | GINTSTS_WKUPINT,
2158 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002159
2160 if (using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002161 writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2162 GAHBCFG_HBSTLEN_INCR4,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002163 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002164 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07002165 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NP_TXF_EMP_LVL |
2166 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2167 GAHBCFG_GLBL_INTR_EN,
Robert Baldyga8acc8292013-09-19 11:50:23 +02002168 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002169
2170 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002171 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2172 * when we have no data to transfer. Otherwise we get being flooded by
2173 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002174 */
2175
Dinh Nguyen47a16852014-04-14 14:13:34 -07002176 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TXFIFOEMPTY |
2177 DIEPMSK_INTKNTXFEMPMSK : 0) |
2178 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
2179 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2180 DIEPMSK_INTKNEPMISMSK,
2181 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002182
2183 /*
2184 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2185 * DMA mode we may need this.
2186 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002187 writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
2188 DIEPMSK_TIMEOUTMSK) : 0) |
2189 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
2190 DOEPMSK_SETUPMSK,
2191 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002192
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002193 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002194
2195 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002196 readl(hsotg->regs + DIEPCTL0),
2197 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002198
2199 /* enable in and out endpoint interrupts */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002200 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002201
2202 /*
2203 * Enable the RXFIFO when in slave mode, as this is how we collect
2204 * the data. In DMA mode, we get events from the FIFO but also
2205 * things we cannot process, so do not use it.
2206 */
2207 if (!using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002208 s3c_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002209
2210 /* Enable interrupts for EP0 in and out */
2211 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2212 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2213
Dinh Nguyen47a16852014-04-14 14:13:34 -07002214 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002215 udelay(10); /* see openiboot */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002216 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002217
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002218 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002219
2220 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002221 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002222 * writing to the EPCTL register..
2223 */
2224
2225 /* set to read 1 8byte packet */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002226 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2227 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002228
2229 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002230 DXEPCTL_CNAK | DXEPCTL_EPENA |
2231 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002232 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002233
2234 /* enable, but don't activate EP0in */
2235 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002236 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002237
2238 s3c_hsotg_enqueue_setup(hsotg);
2239
2240 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002241 readl(hsotg->regs + DIEPCTL0),
2242 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002243
2244 /* clear global NAKs */
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002245 writel(DCTL_CGOUTNAK | DCTL_CGNPINNAK | DCTL_SFTDISCON,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002246 hsotg->regs + DCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002247
2248 /* must be at-least 3ms to allow bus to see disconnect */
2249 mdelay(3);
2250
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002251 hsotg->last_rst = jiffies;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002252}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002253
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002254static void s3c_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002255{
2256 /* set the soft-disconnect bit */
2257 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2258}
2259
Dinh Nguyen510ffaa2014-11-11 11:13:36 -06002260void s3c_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002261{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002262 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002263 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002264}
2265
2266/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002267 * s3c_hsotg_irq - handle device interrupt
2268 * @irq: The IRQ number triggered
2269 * @pw: The pw value when registered the handler.
2270 */
2271static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2272{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002273 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002274 int retry_count = 8;
2275 u32 gintsts;
2276 u32 gintmsk;
2277
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002278 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002279irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002280 gintsts = readl(hsotg->regs + GINTSTS);
2281 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002282
2283 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2284 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2285
2286 gintsts &= gintmsk;
2287
Dinh Nguyen47a16852014-04-14 14:13:34 -07002288 if (gintsts & GINTSTS_ENUMDONE) {
2289 writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002290
2291 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002292 }
2293
Dinh Nguyen47a16852014-04-14 14:13:34 -07002294 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002295 u32 daint = readl(hsotg->regs + DAINT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002296 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2297 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002298 int ep;
2299
Robert Baldyga7e804652013-09-19 11:50:20 +02002300 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002301 daint_out = daint >> DAINT_OUTEP_SHIFT;
2302 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002303
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002304 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2305
2306 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2307 if (daint_out & 1)
2308 s3c_hsotg_epint(hsotg, ep, 0);
2309 }
2310
2311 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2312 if (daint_in & 1)
2313 s3c_hsotg_epint(hsotg, ep, 1);
2314 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002315 }
2316
Dinh Nguyen47a16852014-04-14 14:13:34 -07002317 if (gintsts & GINTSTS_USBRST) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002318
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002319 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002320
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002321 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2322 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002323 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002324
Dinh Nguyen47a16852014-04-14 14:13:34 -07002325 writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002326
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002327 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002328 if (time_after(jiffies, hsotg->last_rst +
2329 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002330
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002331 kill_all_requests(hsotg, &hsotg->eps[0],
2332 -ECONNRESET, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002333
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002334 s3c_hsotg_core_init_disconnected(hsotg);
2335 s3c_hsotg_core_connect(hsotg);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002336 }
2337 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002338 }
2339
2340 /* check both FIFOs */
2341
Dinh Nguyen47a16852014-04-14 14:13:34 -07002342 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002343 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2344
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002345 /*
2346 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002347 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002348 * it needs re-enabling
2349 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002350
Dinh Nguyen47a16852014-04-14 14:13:34 -07002351 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002352 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002353 }
2354
Dinh Nguyen47a16852014-04-14 14:13:34 -07002355 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002356 dev_dbg(hsotg->dev, "PTxFEmp\n");
2357
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002358 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002359
Dinh Nguyen47a16852014-04-14 14:13:34 -07002360 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002361 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362 }
2363
Dinh Nguyen47a16852014-04-14 14:13:34 -07002364 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002365 /*
2366 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002367 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002368 * set.
2369 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002370
2371 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002372 }
2373
Dinh Nguyen47a16852014-04-14 14:13:34 -07002374 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002375 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Dinh Nguyen47a16852014-04-14 14:13:34 -07002376 writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002377 }
2378
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002379 /*
2380 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002381 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002382 * the occurrence.
2383 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002384
Dinh Nguyen47a16852014-04-14 14:13:34 -07002385 if (gintsts & GINTSTS_GOUTNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002386 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2387
Dinh Nguyen47a16852014-04-14 14:13:34 -07002388 writel(DCTL_CGOUTNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002389
2390 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002391 }
2392
Dinh Nguyen47a16852014-04-14 14:13:34 -07002393 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002394 dev_info(hsotg->dev, "GINNakEff triggered\n");
2395
Dinh Nguyen47a16852014-04-14 14:13:34 -07002396 writel(DCTL_CGNPINNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002397
2398 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002399 }
2400
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002401 /*
2402 * if we've had fifo events, we should try and go around the
2403 * loop again to see if there's any point in returning yet.
2404 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002405
2406 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2407 goto irq_retry;
2408
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002409 spin_unlock(&hsotg->lock);
2410
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002411 return IRQ_HANDLED;
2412}
2413
2414/**
2415 * s3c_hsotg_ep_enable - enable the given endpoint
2416 * @ep: The USB endpint to configure
2417 * @desc: The USB endpoint descriptor to configure with.
2418 *
2419 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002420 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002421static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2422 const struct usb_endpoint_descriptor *desc)
2423{
2424 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002425 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002426 unsigned long flags;
2427 int index = hs_ep->index;
2428 u32 epctrl_reg;
2429 u32 epctrl;
2430 u32 mps;
2431 int dir_in;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002432 int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02002433 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002434
2435 dev_dbg(hsotg->dev,
2436 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2437 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2438 desc->wMaxPacketSize, desc->bInterval);
2439
2440 /* not to be called for EP0 */
2441 WARN_ON(index == 0);
2442
2443 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2444 if (dir_in != hs_ep->dir_in) {
2445 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2446 return -EINVAL;
2447 }
2448
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002449 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002450
2451 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2452
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002453 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002454 epctrl = readl(hsotg->regs + epctrl_reg);
2455
2456 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2457 __func__, epctrl, epctrl_reg);
2458
Lukasz Majewski22258f42012-06-14 10:02:24 +02002459 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002460
Dinh Nguyen47a16852014-04-14 14:13:34 -07002461 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2462 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002463
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002464 /*
2465 * mark the endpoint as active, otherwise the core may ignore
2466 * transactions entirely for this endpoint
2467 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002468 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002469
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002470 /*
2471 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002472 * do something with data that we've yet got a request to process
2473 * since the RXFIFO will take data for an endpoint even if the
2474 * size register hasn't been set.
2475 */
2476
Dinh Nguyen47a16852014-04-14 14:13:34 -07002477 epctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002478
2479 /* update the endpoint state */
Robert Baldygae9edd1992013-10-09 08:20:02 +02002480 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002481
2482 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002483 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002484 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002485 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002486 hs_ep->interval = desc->bInterval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002488 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2489 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2490
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002491 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2492 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002493 epctrl |= DXEPCTL_EPTYPE_ISO;
2494 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02002495 hs_ep->isochronous = 1;
2496 if (dir_in)
2497 hs_ep->periodic = 1;
2498 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002499
2500 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002501 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002502 break;
2503
2504 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02002505 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002506 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002507
Dinh Nguyen47a16852014-04-14 14:13:34 -07002508 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002509 break;
2510
2511 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002512 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002513 break;
2514 }
2515
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002516 /*
2517 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002518 * a unique tx-fifo even if it is non-periodic.
2519 */
Robert Baldygab203d0a2014-09-09 10:44:56 +02002520 if (dir_in && hsotg->dedicated_fifos) {
2521 size = hs_ep->ep.maxpacket*hs_ep->mc;
2522 for (i = 1; i <= 8; ++i) {
2523 if (hsotg->fifo_map & (1<<i))
2524 continue;
2525 val = readl(hsotg->regs + DPTXFSIZN(i));
2526 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
2527 if (val < size)
2528 continue;
2529 hsotg->fifo_map |= 1<<i;
2530
2531 epctrl |= DXEPCTL_TXFNUM(i);
2532 hs_ep->fifo_index = i;
2533 hs_ep->fifo_size = val;
2534 break;
2535 }
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302536 if (i == 8) {
2537 ret = -ENOMEM;
2538 goto error;
2539 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02002540 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002541
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002542 /* for non control endpoints, set PID to D0 */
2543 if (index)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002544 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002545
2546 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2547 __func__, epctrl);
2548
2549 writel(epctrl, hsotg->regs + epctrl_reg);
2550 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2551 __func__, readl(hsotg->regs + epctrl_reg));
2552
2553 /* enable the endpoint interrupt */
2554 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2555
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302556error:
Lukasz Majewski22258f42012-06-14 10:02:24 +02002557 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002558 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002559}
2560
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002561/**
2562 * s3c_hsotg_ep_disable - disable given endpoint
2563 * @ep: The endpoint to disable.
2564 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002565static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2566{
2567 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002568 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002569 int dir_in = hs_ep->dir_in;
2570 int index = hs_ep->index;
2571 unsigned long flags;
2572 u32 epctrl_reg;
2573 u32 ctrl;
2574
Marek Szyprowski1e011292014-09-09 10:44:54 +02002575 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002576
2577 if (ep == &hsotg->eps[0].ep) {
2578 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2579 return -EINVAL;
2580 }
2581
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002582 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002583
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002584 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002585 /* terminate all requests with shutdown */
2586 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2587
Robert Baldygab203d0a2014-09-09 10:44:56 +02002588 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
2589 hs_ep->fifo_index = 0;
2590 hs_ep->fifo_size = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002591
2592 ctrl = readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002593 ctrl &= ~DXEPCTL_EPENA;
2594 ctrl &= ~DXEPCTL_USBACTEP;
2595 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002596
2597 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2598 writel(ctrl, hsotg->regs + epctrl_reg);
2599
2600 /* disable endpoint interrupts */
2601 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2602
Lukasz Majewski22258f42012-06-14 10:02:24 +02002603 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002604 return 0;
2605}
2606
2607/**
2608 * on_list - check request is on the given endpoint
2609 * @ep: The endpoint to check.
2610 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002611 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002612static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2613{
2614 struct s3c_hsotg_req *req, *treq;
2615
2616 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2617 if (req == test)
2618 return true;
2619 }
2620
2621 return false;
2622}
2623
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002624/**
2625 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2626 * @ep: The endpoint to dequeue.
2627 * @req: The request to be removed from a queue.
2628 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002629static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2630{
2631 struct s3c_hsotg_req *hs_req = our_req(req);
2632 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002633 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002634 unsigned long flags;
2635
Marek Szyprowski1e011292014-09-09 10:44:54 +02002636 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002637
Lukasz Majewski22258f42012-06-14 10:02:24 +02002638 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002639
2640 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002641 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002642 return -EINVAL;
2643 }
2644
2645 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002646 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002647
2648 return 0;
2649}
2650
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002651/**
2652 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2653 * @ep: The endpoint to set halt.
2654 * @value: Set or unset the halt.
2655 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002656static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2657{
2658 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002659 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002660 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002661 u32 epreg;
2662 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002663 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002664
2665 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2666
Robert Baldygac9f721b2014-01-14 08:36:00 +01002667 if (index == 0) {
2668 if (value)
2669 s3c_hsotg_stall_ep0(hs);
2670 else
2671 dev_warn(hs->dev,
2672 "%s: can't clear halt on ep0\n", __func__);
2673 return 0;
2674 }
2675
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002676 /* write both IN and OUT control registers */
2677
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002678 epreg = DIEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002679 epctl = readl(hs->regs + epreg);
2680
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002681 if (value) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002682 epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
2683 if (epctl & DXEPCTL_EPENA)
2684 epctl |= DXEPCTL_EPDIS;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002685 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002686 epctl &= ~DXEPCTL_STALL;
2687 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2688 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2689 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2690 epctl |= DXEPCTL_SETD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002691 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002692
2693 writel(epctl, hs->regs + epreg);
2694
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002695 epreg = DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002696 epctl = readl(hs->regs + epreg);
2697
2698 if (value)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002699 epctl |= DXEPCTL_STALL;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002700 else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002701 epctl &= ~DXEPCTL_STALL;
2702 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2703 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2704 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2705 epctl |= DXEPCTL_SETD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002706 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002707
2708 writel(epctl, hs->regs + epreg);
2709
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002710 hs_ep->halted = value;
2711
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002712 return 0;
2713}
2714
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002715/**
2716 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2717 * @ep: The endpoint to set halt.
2718 * @value: Set or unset the halt.
2719 */
2720static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2721{
2722 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002723 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002724 unsigned long flags = 0;
2725 int ret = 0;
2726
2727 spin_lock_irqsave(&hs->lock, flags);
2728 ret = s3c_hsotg_ep_sethalt(ep, value);
2729 spin_unlock_irqrestore(&hs->lock, flags);
2730
2731 return ret;
2732}
2733
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002734static struct usb_ep_ops s3c_hsotg_ep_ops = {
2735 .enable = s3c_hsotg_ep_enable,
2736 .disable = s3c_hsotg_ep_disable,
2737 .alloc_request = s3c_hsotg_ep_alloc_request,
2738 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002739 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002740 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002741 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002742 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002743};
2744
2745/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002746 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002747 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002748 *
2749 * A wrapper for platform code responsible for controlling
2750 * low-level USB code
2751 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002752static void s3c_hsotg_phy_enable(struct dwc2_hsotg *hsotg)
Lukasz Majewski41188782012-05-04 14:17:01 +02002753{
2754 struct platform_device *pdev = to_platform_device(hsotg->dev);
2755
2756 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302757
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002758 if (hsotg->uphy)
2759 usb_phy_init(hsotg->uphy);
2760 else if (hsotg->plat && hsotg->plat->phy_init)
2761 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2762 else {
Matt Porter74084842013-12-19 09:23:06 -05002763 phy_init(hsotg->phy);
2764 phy_power_on(hsotg->phy);
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002765 }
Lukasz Majewski41188782012-05-04 14:17:01 +02002766}
2767
2768/**
2769 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002770 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002771 *
2772 * A wrapper for platform code responsible for controlling
2773 * low-level USB code
2774 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002775static void s3c_hsotg_phy_disable(struct dwc2_hsotg *hsotg)
Lukasz Majewski41188782012-05-04 14:17:01 +02002776{
2777 struct platform_device *pdev = to_platform_device(hsotg->dev);
2778
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002779 if (hsotg->uphy)
2780 usb_phy_shutdown(hsotg->uphy);
2781 else if (hsotg->plat && hsotg->plat->phy_exit)
2782 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2783 else {
Matt Porter74084842013-12-19 09:23:06 -05002784 phy_power_off(hsotg->phy);
2785 phy_exit(hsotg->phy);
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002786 }
Lukasz Majewski41188782012-05-04 14:17:01 +02002787}
2788
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002789/**
2790 * s3c_hsotg_init - initalize the usb core
2791 * @hsotg: The driver state
2792 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002793static void s3c_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002794{
2795 /* unmask subset of endpoint interrupts */
2796
Dinh Nguyen47a16852014-04-14 14:13:34 -07002797 writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2798 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
2799 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002800
Dinh Nguyen47a16852014-04-14 14:13:34 -07002801 writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
2802 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
2803 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002804
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002805 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002806
2807 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002808 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002809
2810 if (0) {
2811 /* post global nak until we're ready */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002812 writel(DCTL_SGNPINNAK | DCTL_SGOUTNAK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002813 hsotg->regs + DCTL);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002814 }
2815
2816 /* setup fifos */
2817
2818 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002819 readl(hsotg->regs + GRXFSIZ),
2820 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002821
2822 s3c_hsotg_init_fifo(hsotg);
2823
2824 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002825 writel(GUSBCFG_PHYIF16 | GUSBCFG_TOUTCAL(7) | (0x5 << 10),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002826 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002827
Dinh Nguyen47a16852014-04-14 14:13:34 -07002828 writel(using_dma(hsotg) ? GAHBCFG_DMA_EN : 0x0,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002829 hsotg->regs + GAHBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002830}
2831
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002832/**
2833 * s3c_hsotg_udc_start - prepare the udc for work
2834 * @gadget: The usb gadget state
2835 * @driver: The usb gadget driver
2836 *
2837 * Perform initialization to prepare udc device and driver
2838 * to work.
2839 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002840static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2841 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002842{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002843 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002844 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002845 int ret;
2846
2847 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02002848 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002849 return -ENODEV;
2850 }
2851
2852 if (!driver) {
2853 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2854 return -EINVAL;
2855 }
2856
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002857 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002858 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002859
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002860 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002861 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2862 return -EINVAL;
2863 }
2864
2865 WARN_ON(hsotg->driver);
2866
2867 driver->driver.bus = NULL;
2868 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03002869 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002870 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2871
Robert Baldygad00b4142014-09-09 10:44:57 +02002872 clk_enable(hsotg->clk);
2873
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002874 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
2875 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002876 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002877 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002878 goto err;
2879 }
2880
Marek Szyprowskic816c472014-10-20 12:45:37 +02002881 s3c_hsotg_phy_enable(hsotg);
2882
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002883 spin_lock_irqsave(&hsotg->lock, flags);
2884 s3c_hsotg_init(hsotg);
2885 s3c_hsotg_core_init_disconnected(hsotg);
2886 spin_unlock_irqrestore(&hsotg->lock, flags);
2887
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002888 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002889
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002890 return 0;
2891
2892err:
2893 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002894 return ret;
2895}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002896
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002897/**
2898 * s3c_hsotg_udc_stop - stop the udc
2899 * @gadget: The usb gadget state
2900 * @driver: The usb gadget driver
2901 *
2902 * Stop udc hw block and stay tunned for future transmissions
2903 */
Felipe Balbi22835b82014-10-17 12:05:12 -05002904static int s3c_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002905{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002906 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002907 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002908 int ep;
2909
2910 if (!hsotg)
2911 return -ENODEV;
2912
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002913 /* all endpoints should be shutdown */
Robert Baldyga604eac3c2014-09-09 10:44:13 +02002914 for (ep = 1; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002915 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
2916
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002917 spin_lock_irqsave(&hsotg->lock, flags);
2918
Marek Szyprowski32805c32014-10-20 12:45:33 +02002919 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002920 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002921
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002922 spin_unlock_irqrestore(&hsotg->lock, flags);
2923
Marek Szyprowskic816c472014-10-20 12:45:37 +02002924 s3c_hsotg_phy_disable(hsotg);
2925
Marek Szyprowskic8c10252013-09-12 16:18:48 +02002926 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002927
Robert Baldygad00b4142014-09-09 10:44:57 +02002928 clk_disable(hsotg->clk);
2929
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002930 return 0;
2931}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002932
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002933/**
2934 * s3c_hsotg_gadget_getframe - read the frame number
2935 * @gadget: The usb gadget state
2936 *
2937 * Read the {micro} frame number
2938 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002939static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2940{
2941 return s3c_hsotg_read_frameno(to_hsotg(gadget));
2942}
2943
Lukasz Majewskia188b682012-06-22 09:29:56 +02002944/**
2945 * s3c_hsotg_pullup - connect/disconnect the USB PHY
2946 * @gadget: The usb gadget state
2947 * @is_on: Current state of the USB PHY
2948 *
2949 * Connect/Disconnect the USB PHY pullup
2950 */
2951static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
2952{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002953 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002954 unsigned long flags = 0;
2955
Andrzej Pietrasiewiczd784f1e2014-09-09 10:44:53 +02002956 dev_dbg(hsotg->dev, "%s: is_on: %d\n", __func__, is_on);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002957
2958 spin_lock_irqsave(&hsotg->lock, flags);
2959 if (is_on) {
Robert Baldygad00b4142014-09-09 10:44:57 +02002960 clk_enable(hsotg->clk);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002961 s3c_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002962 } else {
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002963 s3c_hsotg_core_disconnect(hsotg);
Robert Baldygad00b4142014-09-09 10:44:57 +02002964 clk_disable(hsotg->clk);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002965 }
2966
2967 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2968 spin_unlock_irqrestore(&hsotg->lock, flags);
2969
2970 return 0;
2971}
2972
Felipe Balbieeef4582013-01-24 17:58:16 +02002973static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002974 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002975 .udc_start = s3c_hsotg_udc_start,
2976 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02002977 .pullup = s3c_hsotg_pullup,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002978};
2979
2980/**
2981 * s3c_hsotg_initep - initialise a single endpoint
2982 * @hsotg: The device state.
2983 * @hs_ep: The endpoint to be initialised.
2984 * @epnum: The endpoint number
2985 *
2986 * Initialise the given endpoint (as part of the probe and device state
2987 * creation) to give to the gadget driver. Setup the endpoint name, any
2988 * direction information and other state that may be required.
2989 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002990static void s3c_hsotg_initep(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002991 struct s3c_hsotg_ep *hs_ep,
2992 int epnum)
2993{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002994 char *dir;
2995
2996 if (epnum == 0)
2997 dir = "";
2998 else if ((epnum % 2) == 0) {
2999 dir = "out";
3000 } else {
3001 dir = "in";
3002 hs_ep->dir_in = 1;
3003 }
3004
3005 hs_ep->index = epnum;
3006
3007 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3008
3009 INIT_LIST_HEAD(&hs_ep->queue);
3010 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3011
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003012 /* add to the list of endpoints known by the gadget driver */
3013 if (epnum)
3014 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3015
3016 hs_ep->parent = hsotg;
3017 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003018 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003019 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3020
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003021 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003022 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003023 * to be something valid.
3024 */
3025
3026 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003027 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003028 writel(next, hsotg->regs + DIEPCTL(epnum));
3029 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003030 }
3031}
3032
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003033/**
3034 * s3c_hsotg_hw_cfg - read HW configuration registers
3035 * @param: The device state
3036 *
3037 * Read the USB core HW configuration registers
3038 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003039static void s3c_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003040{
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003041 u32 cfg2, cfg3, cfg4;
Ben Dooks10aebc72010-07-19 09:40:44 +01003042 /* check hardware configuration */
3043
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003044 cfg2 = readl(hsotg->regs + 0x48);
3045 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3046
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003047 cfg3 = readl(hsotg->regs + 0x4C);
3048 hsotg->fifo_mem = (cfg3 >> 16);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003049
Ben Dooks10aebc72010-07-19 09:40:44 +01003050 cfg4 = readl(hsotg->regs + 0x50);
3051 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3052
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003053 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3054 hsotg->num_of_eps,
3055 hsotg->dedicated_fifos ? "dedicated" : "shared",
3056 hsotg->fifo_mem);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003057}
3058
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003059/**
3060 * s3c_hsotg_dump - dump state of the udc
3061 * @param: The device state
3062 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003063static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003064{
Mark Brown83a01802011-06-01 17:16:15 +01003065#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003066 struct device *dev = hsotg->dev;
3067 void __iomem *regs = hsotg->regs;
3068 u32 val;
3069 int idx;
3070
3071 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003072 readl(regs + DCFG), readl(regs + DCTL),
3073 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003074
3075 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003076 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003077
3078 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003079 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003080
3081 /* show periodic fifo settings */
3082
3083 for (idx = 1; idx <= 15; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003084 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003085 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003086 val >> FIFOSIZE_DEPTH_SHIFT,
3087 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003088 }
3089
3090 for (idx = 0; idx < 15; idx++) {
3091 dev_info(dev,
3092 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003093 readl(regs + DIEPCTL(idx)),
3094 readl(regs + DIEPTSIZ(idx)),
3095 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003096
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003097 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003098 dev_info(dev,
3099 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003100 idx, readl(regs + DOEPCTL(idx)),
3101 readl(regs + DOEPTSIZ(idx)),
3102 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003103
3104 }
3105
3106 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003107 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003108#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003109}
3110
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003111/**
3112 * state_show - debugfs: show overall driver and device state.
3113 * @seq: The seq file to write to.
3114 * @v: Unused parameter.
3115 *
3116 * This debugfs entry shows the overall state of the hardware and
3117 * some general information about each of the endpoints available
3118 * to the system.
3119 */
3120static int state_show(struct seq_file *seq, void *v)
3121{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003122 struct dwc2_hsotg *hsotg = seq->private;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003123 void __iomem *regs = hsotg->regs;
3124 int idx;
3125
3126 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003127 readl(regs + DCFG),
3128 readl(regs + DCTL),
3129 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003130
3131 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003132 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003133
3134 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003135 readl(regs + GINTMSK),
3136 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003137
3138 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003139 readl(regs + DAINTMSK),
3140 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003141
3142 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003143 readl(regs + GNPTXSTS),
3144 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003145
Pavel Macheka023da32013-09-30 14:56:02 +02003146 seq_puts(seq, "\nEndpoint status:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003147
3148 for (idx = 0; idx < 15; idx++) {
3149 u32 in, out;
3150
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003151 in = readl(regs + DIEPCTL(idx));
3152 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003153
3154 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3155 idx, in, out);
3156
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003157 in = readl(regs + DIEPTSIZ(idx));
3158 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003159
3160 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3161 in, out);
3162
Pavel Macheka023da32013-09-30 14:56:02 +02003163 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003164 }
3165
3166 return 0;
3167}
3168
3169static int state_open(struct inode *inode, struct file *file)
3170{
3171 return single_open(file, state_show, inode->i_private);
3172}
3173
3174static const struct file_operations state_fops = {
3175 .owner = THIS_MODULE,
3176 .open = state_open,
3177 .read = seq_read,
3178 .llseek = seq_lseek,
3179 .release = single_release,
3180};
3181
3182/**
3183 * fifo_show - debugfs: show the fifo information
3184 * @seq: The seq_file to write data to.
3185 * @v: Unused parameter.
3186 *
3187 * Show the FIFO information for the overall fifo and all the
3188 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003189 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003190static int fifo_show(struct seq_file *seq, void *v)
3191{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003192 struct dwc2_hsotg *hsotg = seq->private;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003193 void __iomem *regs = hsotg->regs;
3194 u32 val;
3195 int idx;
3196
Pavel Macheka023da32013-09-30 14:56:02 +02003197 seq_puts(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003198 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003199
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003200 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003201 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Dinh Nguyen47a16852014-04-14 14:13:34 -07003202 val >> FIFOSIZE_DEPTH_SHIFT,
3203 val & FIFOSIZE_DEPTH_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003204
Pavel Macheka023da32013-09-30 14:56:02 +02003205 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003206
3207 for (idx = 1; idx <= 15; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003208 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003209
3210 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003211 val >> FIFOSIZE_DEPTH_SHIFT,
3212 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003213 }
3214
3215 return 0;
3216}
3217
3218static int fifo_open(struct inode *inode, struct file *file)
3219{
3220 return single_open(file, fifo_show, inode->i_private);
3221}
3222
3223static const struct file_operations fifo_fops = {
3224 .owner = THIS_MODULE,
3225 .open = fifo_open,
3226 .read = seq_read,
3227 .llseek = seq_lseek,
3228 .release = single_release,
3229};
3230
3231
3232static const char *decode_direction(int is_in)
3233{
3234 return is_in ? "in" : "out";
3235}
3236
3237/**
3238 * ep_show - debugfs: show the state of an endpoint.
3239 * @seq: The seq_file to write data to.
3240 * @v: Unused parameter.
3241 *
3242 * This debugfs entry shows the state of the given endpoint (one is
3243 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003244 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003245static int ep_show(struct seq_file *seq, void *v)
3246{
3247 struct s3c_hsotg_ep *ep = seq->private;
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003248 struct dwc2_hsotg *hsotg = ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003249 struct s3c_hsotg_req *req;
3250 void __iomem *regs = hsotg->regs;
3251 int index = ep->index;
3252 int show_limit = 15;
3253 unsigned long flags;
3254
3255 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3256 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3257
3258 /* first show the register state */
3259
3260 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003261 readl(regs + DIEPCTL(index)),
3262 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003263
3264 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003265 readl(regs + DIEPDMA(index)),
3266 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003267
3268 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003269 readl(regs + DIEPINT(index)),
3270 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003271
3272 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003273 readl(regs + DIEPTSIZ(index)),
3274 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003275
Pavel Macheka023da32013-09-30 14:56:02 +02003276 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003277 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3278 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3279
3280 seq_printf(seq, "request list (%p,%p):\n",
3281 ep->queue.next, ep->queue.prev);
3282
Lukasz Majewski22258f42012-06-14 10:02:24 +02003283 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003284
3285 list_for_each_entry(req, &ep->queue, queue) {
3286 if (--show_limit < 0) {
Pavel Macheka023da32013-09-30 14:56:02 +02003287 seq_puts(seq, "not showing more requests...\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003288 break;
3289 }
3290
3291 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3292 req == ep->req ? '*' : ' ',
3293 req, req->req.length, req->req.buf);
3294 seq_printf(seq, "%d done, res %d\n",
3295 req->req.actual, req->req.status);
3296 }
3297
Lukasz Majewski22258f42012-06-14 10:02:24 +02003298 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003299
3300 return 0;
3301}
3302
3303static int ep_open(struct inode *inode, struct file *file)
3304{
3305 return single_open(file, ep_show, inode->i_private);
3306}
3307
3308static const struct file_operations ep_fops = {
3309 .owner = THIS_MODULE,
3310 .open = ep_open,
3311 .read = seq_read,
3312 .llseek = seq_lseek,
3313 .release = single_release,
3314};
3315
3316/**
3317 * s3c_hsotg_create_debug - create debugfs directory and files
3318 * @hsotg: The driver state
3319 *
3320 * Create the debugfs files to allow the user to get information
3321 * about the state of the system. The directory name is created
3322 * with the same name as the device itself, in case we end up
3323 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003324 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003325static void s3c_hsotg_create_debug(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003326{
3327 struct dentry *root;
3328 unsigned epidx;
3329
3330 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3331 hsotg->debug_root = root;
3332 if (IS_ERR(root)) {
3333 dev_err(hsotg->dev, "cannot create debug root\n");
3334 return;
3335 }
3336
3337 /* create general state file */
3338
3339 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3340 hsotg, &state_fops);
3341
3342 if (IS_ERR(hsotg->debug_file))
3343 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3344
3345 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3346 hsotg, &fifo_fops);
3347
3348 if (IS_ERR(hsotg->debug_fifo))
3349 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3350
3351 /* create one file for each endpoint */
3352
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003353 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003354 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3355
3356 ep->debugfs = debugfs_create_file(ep->name, 0444,
3357 root, ep, &ep_fops);
3358
3359 if (IS_ERR(ep->debugfs))
3360 dev_err(hsotg->dev, "failed to create %s debug file\n",
3361 ep->name);
3362 }
3363}
3364
3365/**
3366 * s3c_hsotg_delete_debug - cleanup debugfs entries
3367 * @hsotg: The driver state
3368 *
3369 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003370 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003371static void s3c_hsotg_delete_debug(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003372{
3373 unsigned epidx;
3374
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003375 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003376 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3377 debugfs_remove(ep->debugfs);
3378 }
3379
3380 debugfs_remove(hsotg->debug_file);
3381 debugfs_remove(hsotg->debug_fifo);
3382 debugfs_remove(hsotg->debug_root);
3383}
3384
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003385/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06003386 * dwc2_gadget_init - init function for gadget
3387 * @dwc2: The data structure for the DWC2 driver.
3388 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003389 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003390int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003391{
Dinh Nguyen117777b2014-11-11 11:13:34 -06003392 struct device *dev = hsotg->dev;
3393 struct s3c_hsotg_plat *plat = dev->platform_data;
Matt Porter74084842013-12-19 09:23:06 -05003394 struct phy *phy;
3395 struct usb_phy *uphy;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003396 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003397 int epnum;
3398 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003399 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003400
Kamil Debski1b59fc72014-09-09 10:44:52 +02003401 /* Set default UTMI width */
3402 hsotg->phyif = GUSBCFG_PHYIF16;
3403
Matt Porter74084842013-12-19 09:23:06 -05003404 /*
3405 * Attempt to find a generic PHY, then look for an old style
3406 * USB PHY, finally fall back to pdata
3407 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003408 phy = devm_phy_get(dev, "usb2-phy");
Felipe Balbif4f5ba52013-03-15 10:56:19 +02003409 if (IS_ERR(phy)) {
Matt Porter74084842013-12-19 09:23:06 -05003410 uphy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3411 if (IS_ERR(uphy)) {
3412 /* Fallback for pdata */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003413 plat = dev_get_platdata(dev);
Matt Porter74084842013-12-19 09:23:06 -05003414 if (!plat) {
Dinh Nguyen117777b2014-11-11 11:13:34 -06003415 dev_err(dev,
Matt Porter74084842013-12-19 09:23:06 -05003416 "no platform data or transceiver defined\n");
3417 return -EPROBE_DEFER;
3418 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303419 hsotg->plat = plat;
Matt Porter74084842013-12-19 09:23:06 -05003420 } else
3421 hsotg->uphy = uphy;
Kamil Debski1b59fc72014-09-09 10:44:52 +02003422 } else {
Praveen Panerib2e587d2012-11-14 15:57:16 +05303423 hsotg->phy = phy;
Kamil Debski1b59fc72014-09-09 10:44:52 +02003424 /*
3425 * If using the generic PHY framework, check if the PHY bus
3426 * width is 8-bit and set the phyif appropriately.
3427 */
3428 if (phy_get_bus_width(phy) == 8)
3429 hsotg->phyif = GUSBCFG_PHYIF8;
3430 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303431
Dinh Nguyen117777b2014-11-11 11:13:34 -06003432 hsotg->clk = devm_clk_get(dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003433 if (IS_ERR(hsotg->clk)) {
Dinh Nguyen8d736d82014-11-11 11:13:38 -06003434 hsotg->clk = NULL;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003435 dev_err(dev, "cannot get otg clock\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303436 return PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003437 }
3438
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003439 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003440 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3441 hsotg->gadget.name = dev_name(dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003442
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003443 /* reset the system */
3444
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003445 clk_prepare_enable(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003446
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003447 /* regulators */
3448
3449 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3450 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3451
Sachin Kamatcd762132013-01-08 14:27:00 +05303452 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003453 hsotg->supplies);
3454 if (ret) {
Dinh Nguyen117777b2014-11-11 11:13:34 -06003455 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303456 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003457 }
3458
3459 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3460 hsotg->supplies);
3461
3462 if (ret) {
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003463 dev_err(dev, "failed to enable supplies: %d\n", ret);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003464 goto err_supplies;
3465 }
3466
Lukasz Majewski41188782012-05-04 14:17:01 +02003467 /* usb phy enable */
3468 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003469
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003470 s3c_hsotg_corereset(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003471 s3c_hsotg_hw_cfg(hsotg);
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003472 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003473
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003474 ret = devm_request_irq(hsotg->dev, irq, s3c_hsotg_irq, IRQF_SHARED,
3475 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003476 if (ret < 0) {
3477 s3c_hsotg_phy_disable(hsotg);
3478 clk_disable_unprepare(hsotg->clk);
3479 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3480 hsotg->supplies);
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003481 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003482 goto err_clk;
3483 }
3484
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003485 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3486
3487 if (hsotg->num_of_eps == 0) {
3488 dev_err(dev, "wrong number of EPs (zero)\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003489 ret = -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003490 goto err_supplies;
3491 }
3492
3493 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3494 GFP_KERNEL);
3495 if (!eps) {
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003496 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003497 goto err_supplies;
3498 }
3499
3500 hsotg->eps = eps;
3501
3502 /* setup endpoint information */
3503
3504 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3505 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3506
3507 /* allocate EP0 request */
3508
3509 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3510 GFP_KERNEL);
3511 if (!hsotg->ctrl_req) {
3512 dev_err(dev, "failed to allocate ctrl req\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003513 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003514 goto err_ep_mem;
3515 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003516
3517 /* initialise the endpoints now the core has been initialised */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003518 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003519 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3520
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003521 /* disable power and clock */
Marek Szyprowski3a8146a2014-10-20 12:45:34 +02003522 s3c_hsotg_phy_disable(hsotg);
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003523
3524 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3525 hsotg->supplies);
3526 if (ret) {
Dinh Nguyen117777b2014-11-11 11:13:34 -06003527 dev_err(dev, "failed to disable supplies: %d\n", ret);
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003528 goto err_ep_mem;
3529 }
3530
Dinh Nguyen117777b2014-11-11 11:13:34 -06003531 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003532 if (ret)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003533 goto err_ep_mem;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003534
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003535 s3c_hsotg_create_debug(hsotg);
3536
3537 s3c_hsotg_dump(hsotg);
3538
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003539 return 0;
3540
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003541err_ep_mem:
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003542 kfree(eps);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003543err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003544 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003545err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003546 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05303547
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003548 return ret;
3549}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003550EXPORT_SYMBOL_GPL(dwc2_gadget_init);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003551
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003552/**
3553 * s3c_hsotg_remove - remove function for hsotg driver
3554 * @pdev: The platform information for the driver
3555 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003556int s3c_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003557{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003558 usb_del_gadget_udc(&hsotg->gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003559 s3c_hsotg_delete_debug(hsotg);
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003560 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003561
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003562 return 0;
3563}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003564EXPORT_SYMBOL_GPL(s3c_hsotg_remove);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003565
Dinh Nguyen117777b2014-11-11 11:13:34 -06003566int s3c_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003567{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003568 unsigned long flags;
3569 int ret = 0;
3570
3571 if (hsotg->driver)
3572 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3573 hsotg->driver->driver.name);
3574
3575 spin_lock_irqsave(&hsotg->lock, flags);
Marek Szyprowski7b093f72014-10-20 12:45:39 +02003576 s3c_hsotg_core_disconnect(hsotg);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003577 s3c_hsotg_disconnect(hsotg);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003578 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3579 spin_unlock_irqrestore(&hsotg->lock, flags);
3580
Marek Szyprowski7b093f72014-10-20 12:45:39 +02003581 s3c_hsotg_phy_disable(hsotg);
3582
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003583 if (hsotg->driver) {
3584 int ep;
3585 for (ep = 0; ep < hsotg->num_of_eps; ep++)
3586 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3587
3588 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3589 hsotg->supplies);
Robert Baldygad00b4142014-09-09 10:44:57 +02003590 clk_disable(hsotg->clk);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003591 }
3592
3593 return ret;
3594}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003595EXPORT_SYMBOL_GPL(s3c_hsotg_suspend);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003596
Dinh Nguyen117777b2014-11-11 11:13:34 -06003597int s3c_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003598{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003599 unsigned long flags;
3600 int ret = 0;
3601
3602 if (hsotg->driver) {
3603 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3604 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02003605
3606 clk_enable(hsotg->clk);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003607 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3608 hsotg->supplies);
3609 }
3610
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003611 s3c_hsotg_phy_enable(hsotg);
Marek Szyprowski7b093f72014-10-20 12:45:39 +02003612
3613 spin_lock_irqsave(&hsotg->lock, flags);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003614 s3c_hsotg_core_init_disconnected(hsotg);
3615 s3c_hsotg_core_connect(hsotg);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003616 spin_unlock_irqrestore(&hsotg->lock, flags);
3617
3618 return ret;
3619}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003620EXPORT_SYMBOL_GPL(s3c_hsotg_resume);