blob: 3cd789139f3f04ebe7f529679b2e99bbd2798f2c [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);
Robert Baldygaab93e012013-09-19 11:50:17 +02001033
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001034/**
Robert Baldygac9f721b2014-01-14 08:36:00 +01001035 * s3c_hsotg_stall_ep0 - stall ep0
1036 * @hsotg: The device state
1037 *
1038 * Set stall for ep0 as response for setup request.
1039 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001040static void s3c_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001041{
Robert Baldygac9f721b2014-01-14 08:36:00 +01001042 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1043 u32 reg;
1044 u32 ctrl;
1045
1046 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1047 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1048
1049 /*
1050 * DxEPCTL_Stall will be cleared by EP once it has
1051 * taken effect, so no need to clear later.
1052 */
1053
1054 ctrl = readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001055 ctrl |= DXEPCTL_STALL;
1056 ctrl |= DXEPCTL_CNAK;
Robert Baldygac9f721b2014-01-14 08:36:00 +01001057 writel(ctrl, hsotg->regs + reg);
1058
1059 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001060 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Robert Baldygac9f721b2014-01-14 08:36:00 +01001061 ctrl, reg, readl(hsotg->regs + reg));
1062
1063 /*
1064 * complete won't be called, so we enqueue
1065 * setup request here
1066 */
1067 s3c_hsotg_enqueue_setup(hsotg);
1068}
1069
1070/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001071 * s3c_hsotg_process_control - process a control request
1072 * @hsotg: The device state
1073 * @ctrl: The control request received
1074 *
1075 * The controller has received the SETUP phase of a control request, and
1076 * needs to work out what to do next (and whether to pass it on to the
1077 * gadget driver).
1078 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001079static void s3c_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001080 struct usb_ctrlrequest *ctrl)
1081{
1082 struct s3c_hsotg_ep *ep0 = &hsotg->eps[0];
1083 int ret = 0;
1084 u32 dcfg;
1085
1086 ep0->sent_zlp = 0;
1087
1088 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1089 ctrl->bRequest, ctrl->bRequestType,
1090 ctrl->wValue, ctrl->wLength);
1091
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001092 /*
1093 * record the direction of the request, for later use when enquing
1094 * packets onto EP0.
1095 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001096
1097 ep0->dir_in = (ctrl->bRequestType & USB_DIR_IN) ? 1 : 0;
1098 dev_dbg(hsotg->dev, "ctrl: dir_in=%d\n", ep0->dir_in);
1099
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001100 /*
1101 * if we've no data with this request, then the last part of the
1102 * transaction is going to implicitly be IN.
1103 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001104 if (ctrl->wLength == 0)
1105 ep0->dir_in = 1;
1106
1107 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1108 switch (ctrl->bRequest) {
1109 case USB_REQ_SET_ADDRESS:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001110 dcfg = readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001111 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001112 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1113 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001114 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001115
1116 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1117
1118 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1119 return;
1120
1121 case USB_REQ_GET_STATUS:
1122 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1123 break;
1124
1125 case USB_REQ_CLEAR_FEATURE:
1126 case USB_REQ_SET_FEATURE:
1127 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1128 break;
1129 }
1130 }
1131
1132 /* as a fallback, try delivering it to the driver to deal with */
1133
1134 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001135 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001136 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001137 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138 if (ret < 0)
1139 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1140 }
1141
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001142 /*
1143 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001144 * so respond with a STALL for the status stage to indicate failure.
1145 */
1146
Robert Baldygac9f721b2014-01-14 08:36:00 +01001147 if (ret < 0)
1148 s3c_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001149}
1150
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001151/**
1152 * s3c_hsotg_complete_setup - completion of a setup transfer
1153 * @ep: The endpoint the request was on.
1154 * @req: The request completed.
1155 *
1156 * Called on completion of any requests the driver itself submitted for
1157 * EP0 setup packets
1158 */
1159static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1160 struct usb_request *req)
1161{
1162 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001163 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001164
1165 if (req->status < 0) {
1166 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1167 return;
1168 }
1169
Robert Baldyga93f599f2013-11-21 13:49:17 +01001170 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001171 if (req->actual == 0)
1172 s3c_hsotg_enqueue_setup(hsotg);
1173 else
1174 s3c_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001175 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001176}
1177
1178/**
1179 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1180 * @hsotg: The device state.
1181 *
1182 * Enqueue a request on EP0 if necessary to received any SETUP packets
1183 * received from the host.
1184 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001185static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001186{
1187 struct usb_request *req = hsotg->ctrl_req;
1188 struct s3c_hsotg_req *hs_req = our_req(req);
1189 int ret;
1190
1191 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1192
1193 req->zero = 0;
1194 req->length = 8;
1195 req->buf = hsotg->ctrl_buff;
1196 req->complete = s3c_hsotg_complete_setup;
1197
1198 if (!list_empty(&hs_req->queue)) {
1199 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1200 return;
1201 }
1202
1203 hsotg->eps[0].dir_in = 0;
1204
1205 ret = s3c_hsotg_ep_queue(&hsotg->eps[0].ep, req, GFP_ATOMIC);
1206 if (ret < 0) {
1207 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001208 /*
1209 * Don't think there's much we can do other than watch the
1210 * driver fail.
1211 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001212 }
1213}
1214
1215/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001216 * s3c_hsotg_complete_request - complete a request given to us
1217 * @hsotg: The device state.
1218 * @hs_ep: The endpoint the request was on.
1219 * @hs_req: The request to complete.
1220 * @result: The result code (0 => Ok, otherwise errno)
1221 *
1222 * The given request has finished, so call the necessary completion
1223 * if it has one and then look to see if we can start a new request
1224 * on the endpoint.
1225 *
1226 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001227 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001228static void s3c_hsotg_complete_request(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001229 struct s3c_hsotg_ep *hs_ep,
1230 struct s3c_hsotg_req *hs_req,
1231 int result)
1232{
1233 bool restart;
1234
1235 if (!hs_req) {
1236 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1237 return;
1238 }
1239
1240 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1241 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1242
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001243 /*
1244 * only replace the status if we've not already set an error
1245 * from a previous transaction
1246 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001247
1248 if (hs_req->req.status == -EINPROGRESS)
1249 hs_req->req.status = result;
1250
1251 hs_ep->req = NULL;
1252 list_del_init(&hs_req->queue);
1253
1254 if (using_dma(hsotg))
1255 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1256
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001257 /*
1258 * call the complete request with the locks off, just in case the
1259 * request tries to queue more work for this endpoint.
1260 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001261
1262 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001263 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001264 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001265 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001266 }
1267
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001268 /*
1269 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001270 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001271 * so be careful when doing this.
1272 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001273
1274 if (!hs_ep->req && result >= 0) {
1275 restart = !list_empty(&hs_ep->queue);
1276 if (restart) {
1277 hs_req = get_ep_head(hs_ep);
1278 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1279 }
1280 }
1281}
1282
1283/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001284 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1285 * @hsotg: The device state.
1286 * @ep_idx: The endpoint index for the data
1287 * @size: The size of data in the fifo, in bytes
1288 *
1289 * The FIFO status shows there is data to read from the FIFO for a given
1290 * endpoint, so sort out whether we need to read the data into a request
1291 * that has been made for that endpoint.
1292 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001293static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001294{
1295 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep_idx];
1296 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001297 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001298 int to_read;
1299 int max_req;
1300 int read_ptr;
1301
Lukasz Majewski22258f42012-06-14 10:02:24 +02001302
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001303 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001304 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001305 int ptr;
1306
1307 dev_warn(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001308 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001309 __func__, size, ep_idx, epctl);
1310
1311 /* dump the data from the FIFO, we've nothing we can do */
1312 for (ptr = 0; ptr < size; ptr += 4)
1313 (void)readl(fifo);
1314
1315 return;
1316 }
1317
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001318 to_read = size;
1319 read_ptr = hs_req->req.actual;
1320 max_req = hs_req->req.length - read_ptr;
1321
Ben Dooksa33e7132010-07-19 09:40:49 +01001322 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1323 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1324
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001325 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001326 /*
1327 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001328 * to deal with in this request.
1329 */
1330
1331 /* currently we don't deal this */
1332 WARN_ON_ONCE(1);
1333 }
1334
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001335 hs_ep->total_data += to_read;
1336 hs_req->req.actual += to_read;
1337 to_read = DIV_ROUND_UP(to_read, 4);
1338
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001339 /*
1340 * note, we might over-write the buffer end by 3 bytes depending on
1341 * alignment of the data.
1342 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001343 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001344}
1345
1346/**
1347 * s3c_hsotg_send_zlp - send zero-length packet on control endpoint
1348 * @hsotg: The device instance
1349 * @req: The request currently on this endpoint
1350 *
1351 * Generate a zero-length IN packet request for terminating a SETUP
1352 * transaction.
1353 *
1354 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001355 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001356 * the TxFIFO.
1357 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001358static void s3c_hsotg_send_zlp(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001359 struct s3c_hsotg_req *req)
1360{
1361 u32 ctrl;
1362
1363 if (!req) {
1364 dev_warn(hsotg->dev, "%s: no request?\n", __func__);
1365 return;
1366 }
1367
1368 if (req->req.length == 0) {
1369 hsotg->eps[0].sent_zlp = 1;
1370 s3c_hsotg_enqueue_setup(hsotg);
1371 return;
1372 }
1373
1374 hsotg->eps[0].dir_in = 1;
1375 hsotg->eps[0].sent_zlp = 1;
1376
1377 dev_dbg(hsotg->dev, "sending zero-length packet\n");
1378
1379 /* issue a zero-sized packet to terminate this */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001380 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1381 DXEPTSIZ_XFERSIZE(0), hsotg->regs + DIEPTSIZ(0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001382
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001383 ctrl = readl(hsotg->regs + DIEPCTL0);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001384 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1385 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1386 ctrl |= DXEPCTL_USBACTEP;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001387 writel(ctrl, hsotg->regs + DIEPCTL0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001388}
1389
1390/**
1391 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1392 * @hsotg: The device instance
1393 * @epnum: The endpoint received from
1394 * @was_setup: Set if processing a SetupDone event.
1395 *
1396 * The RXFIFO has delivered an OutDone event, which means that the data
1397 * transfer for an OUT endpoint has been completed, either by a short
1398 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001399 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001400static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001401 int epnum, bool was_setup)
1402{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001403 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001404 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[epnum];
1405 struct s3c_hsotg_req *hs_req = hs_ep->req;
1406 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001407 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001408 int result = 0;
1409
1410 if (!hs_req) {
1411 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1412 return;
1413 }
1414
1415 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001416 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001417
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001418 /*
1419 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001420 * is left in the endpoint size register and then working it
1421 * out from the amount we loaded for the transfer.
1422 *
1423 * We need to do this as DMA pointers are always 32bit aligned
1424 * so may overshoot/undershoot the transfer.
1425 */
1426
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001427 size_done = hs_ep->size_loaded - size_left;
1428 size_done += hs_ep->last_load;
1429
1430 req->actual = size_done;
1431 }
1432
Ben Dooksa33e7132010-07-19 09:40:49 +01001433 /* if there is more request to do, schedule new transfer */
1434 if (req->actual < req->length && size_left == 0) {
1435 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1436 return;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001437 } else if (epnum == 0) {
1438 /*
1439 * After was_setup = 1 =>
1440 * set CNAK for non Setup requests
1441 */
1442 hsotg->setup = was_setup ? 0 : 1;
Ben Dooksa33e7132010-07-19 09:40:49 +01001443 }
1444
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001445 if (req->actual < req->length && req->short_not_ok) {
1446 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1447 __func__, req->actual, req->length);
1448
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001449 /*
1450 * todo - what should we return here? there's no one else
1451 * even bothering to check the status.
1452 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001453 }
1454
1455 if (epnum == 0) {
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001456 /*
1457 * Condition req->complete != s3c_hsotg_complete_setup says:
1458 * send ZLP when we have an asynchronous request from gadget
1459 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001460 if (!was_setup && req->complete != s3c_hsotg_complete_setup)
1461 s3c_hsotg_send_zlp(hsotg, hs_req);
1462 }
1463
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001464 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001465}
1466
1467/**
1468 * s3c_hsotg_read_frameno - read current frame number
1469 * @hsotg: The device instance
1470 *
1471 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001472 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001473static u32 s3c_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001474{
1475 u32 dsts;
1476
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001477 dsts = readl(hsotg->regs + DSTS);
1478 dsts &= DSTS_SOFFN_MASK;
1479 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001480
1481 return dsts;
1482}
1483
1484/**
1485 * s3c_hsotg_handle_rx - RX FIFO has data
1486 * @hsotg: The device instance
1487 *
1488 * The IRQ handler has detected that the RX FIFO has some data in it
1489 * that requires processing, so find out what is in there and do the
1490 * appropriate read.
1491 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001492 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001493 * chunks, so if you have x packets received on an endpoint you'll get x
1494 * FIFO events delivered, each with a packet's worth of data in it.
1495 *
1496 * When using DMA, we should not be processing events from the RXFIFO
1497 * as the actual data should be sent to the memory directly and we turn
1498 * on the completion interrupts to get notifications of transfer completion.
1499 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001500static void s3c_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001501{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001502 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001503 u32 epnum, status, size;
1504
1505 WARN_ON(using_dma(hsotg));
1506
Dinh Nguyen47a16852014-04-14 14:13:34 -07001507 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1508 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001509
Dinh Nguyen47a16852014-04-14 14:13:34 -07001510 size = grxstsr & GRXSTS_BYTECNT_MASK;
1511 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001512
1513 if (1)
1514 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1515 __func__, grxstsr, size, epnum);
1516
Dinh Nguyen47a16852014-04-14 14:13:34 -07001517 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1518 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1519 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001520 break;
1521
Dinh Nguyen47a16852014-04-14 14:13:34 -07001522 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001523 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1524 s3c_hsotg_read_frameno(hsotg));
1525
1526 if (!using_dma(hsotg))
1527 s3c_hsotg_handle_outdone(hsotg, epnum, false);
1528 break;
1529
Dinh Nguyen47a16852014-04-14 14:13:34 -07001530 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001531 dev_dbg(hsotg->dev,
1532 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1533 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001534 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001535
1536 s3c_hsotg_handle_outdone(hsotg, epnum, true);
1537 break;
1538
Dinh Nguyen47a16852014-04-14 14:13:34 -07001539 case GRXSTS_PKTSTS_OUTRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001540 s3c_hsotg_rx_data(hsotg, epnum, size);
1541 break;
1542
Dinh Nguyen47a16852014-04-14 14:13:34 -07001543 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001544 dev_dbg(hsotg->dev,
1545 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1546 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001547 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001548
1549 s3c_hsotg_rx_data(hsotg, epnum, size);
1550 break;
1551
1552 default:
1553 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1554 __func__, grxstsr);
1555
1556 s3c_hsotg_dump(hsotg);
1557 break;
1558 }
1559}
1560
1561/**
1562 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1563 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001564 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001565static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1566{
1567 switch (mps) {
1568 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001569 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001570 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001571 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001572 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001573 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001574 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001575 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001576 }
1577
1578 /* bad max packet size, warn and return invalid result */
1579 WARN_ON(1);
1580 return (u32)-1;
1581}
1582
1583/**
1584 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1585 * @hsotg: The driver state.
1586 * @ep: The index number of the endpoint
1587 * @mps: The maximum packet size in bytes
1588 *
1589 * Configure the maximum packet size for the given endpoint, updating
1590 * the hardware control registers to reflect this.
1591 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001592static void s3c_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001593 unsigned int ep, unsigned int mps)
1594{
1595 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[ep];
1596 void __iomem *regs = hsotg->regs;
1597 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001598 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001599 u32 reg;
1600
1601 if (ep == 0) {
1602 /* EP0 is a special case */
1603 mpsval = s3c_hsotg_ep0_mps(mps);
1604 if (mpsval > 3)
1605 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001606 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001607 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001608 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001609 mpsval = mps & DXEPCTL_MPS_MASK;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001610 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001611 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001612 mcval = ((mps >> 11) & 0x3) + 1;
1613 hs_ep->mc = mcval;
1614 if (mcval > 3)
1615 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001616 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001617 }
1618
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001619 /*
1620 * update both the in and out endpoint controldir_ registers, even
1621 * if one of the directions may not be in use.
1622 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001623
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001624 reg = readl(regs + DIEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001625 reg &= ~DXEPCTL_MPS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001626 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001627 writel(reg, regs + DIEPCTL(ep));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001628
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001629 if (ep) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001630 reg = readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001631 reg &= ~DXEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001632 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001633 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001634 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001635
1636 return;
1637
1638bad_mps:
1639 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1640}
1641
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001642/**
1643 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1644 * @hsotg: The driver state
1645 * @idx: The index for the endpoint (0..15)
1646 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001647static void s3c_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001648{
1649 int timeout;
1650 int val;
1651
Dinh Nguyen47a16852014-04-14 14:13:34 -07001652 writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001653 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001654
1655 /* wait until the fifo is flushed */
1656 timeout = 100;
1657
1658 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001659 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001660
Dinh Nguyen47a16852014-04-14 14:13:34 -07001661 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001662 break;
1663
1664 if (--timeout == 0) {
1665 dev_err(hsotg->dev,
1666 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1667 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02001668 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001669 }
1670
1671 udelay(1);
1672 }
1673}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001674
1675/**
1676 * s3c_hsotg_trytx - check to see if anything needs transmitting
1677 * @hsotg: The driver state
1678 * @hs_ep: The driver endpoint to check.
1679 *
1680 * Check to see if there is a request that has data to send, and if so
1681 * make an attempt to write data into the FIFO.
1682 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001683static int s3c_hsotg_trytx(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001684 struct s3c_hsotg_ep *hs_ep)
1685{
1686 struct s3c_hsotg_req *hs_req = hs_ep->req;
1687
Robert Baldygaafcf4162013-09-19 11:50:19 +02001688 if (!hs_ep->dir_in || !hs_req) {
1689 /**
1690 * if request is not enqueued, we disable interrupts
1691 * for endpoints, excepting ep0
1692 */
1693 if (hs_ep->index != 0)
1694 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1695 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001696 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001697 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001698
1699 if (hs_req->req.actual < hs_req->req.length) {
1700 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1701 hs_ep->index);
1702 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1703 }
1704
1705 return 0;
1706}
1707
1708/**
1709 * s3c_hsotg_complete_in - complete IN transfer
1710 * @hsotg: The device state.
1711 * @hs_ep: The endpoint that has just completed.
1712 *
1713 * An IN transfer has been completed, update the transfer's state and then
1714 * call the relevant completion routines.
1715 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001716static void s3c_hsotg_complete_in(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001717 struct s3c_hsotg_ep *hs_ep)
1718{
1719 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001720 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001721 int size_left, size_done;
1722
1723 if (!hs_req) {
1724 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1725 return;
1726 }
1727
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001728 /* Finish ZLP handling for IN EP0 transactions */
1729 if (hsotg->eps[0].sent_zlp) {
1730 dev_dbg(hsotg->dev, "zlp packet received\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001731 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001732 return;
1733 }
1734
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001735 /*
1736 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001737 * in the endpoint size register and then working it out from
1738 * the amount we loaded for the transfer.
1739 *
1740 * We do this even for DMA, as the transfer may have incremented
1741 * past the end of the buffer (DMA transfers are always 32bit
1742 * aligned).
1743 */
1744
Dinh Nguyen47a16852014-04-14 14:13:34 -07001745 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001746
1747 size_done = hs_ep->size_loaded - size_left;
1748 size_done += hs_ep->last_load;
1749
1750 if (hs_req->req.actual != size_done)
1751 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1752 __func__, hs_req->req.actual, size_done);
1753
1754 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001755 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1756 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001757
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001758 /*
1759 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1760 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1761 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1762 * inform the host that no more data is available.
1763 * The state of req.zero member is checked to be sure that the value to
1764 * send is smaller than wValue expected from host.
1765 * Check req.length to NOT send another ZLP when the current one is
1766 * under completion (the one for which this completion has been called).
1767 */
1768 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1769 hs_req->req.length == hs_req->req.actual &&
1770 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1771
1772 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
1773 s3c_hsotg_send_zlp(hsotg, hs_req);
1774
1775 return;
1776 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001777
1778 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1779 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1780 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1781 } else
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001782 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001783}
1784
1785/**
1786 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1787 * @hsotg: The driver state
1788 * @idx: The index for the endpoint (0..15)
1789 * @dir_in: Set if this is an IN endpoint
1790 *
1791 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001792 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001793static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001794 int dir_in)
1795{
1796 struct s3c_hsotg_ep *hs_ep = &hsotg->eps[idx];
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001797 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1798 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1799 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001800 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02001801 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001802
1803 ints = readl(hsotg->regs + epint_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02001804 ctrl = readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001805
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001806 /* Clear endpoint interrupts */
1807 writel(ints, hsotg->regs + epint_reg);
1808
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001809 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1810 __func__, idx, dir_in ? "in" : "out", ints);
1811
Dinh Nguyen47a16852014-04-14 14:13:34 -07001812 if (ints & DXEPINT_XFERCOMPL) {
Robert Baldyga1479e842013-10-09 08:41:57 +02001813 if (hs_ep->isochronous && hs_ep->interval == 1) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001814 if (ctrl & DXEPCTL_EOFRNUM)
1815 ctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001816 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001817 ctrl |= DXEPCTL_SETODDFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001818 writel(ctrl, hsotg->regs + epctl_reg);
1819 }
1820
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001821 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001822 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001823 __func__, readl(hsotg->regs + epctl_reg),
1824 readl(hsotg->regs + epsiz_reg));
1825
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001826 /*
1827 * we get OutDone from the FIFO, so we only need to look
1828 * at completing IN requests here
1829 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001830 if (dir_in) {
1831 s3c_hsotg_complete_in(hsotg, hs_ep);
1832
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001833 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001834 s3c_hsotg_enqueue_setup(hsotg);
1835 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001836 /*
1837 * We're using DMA, we need to fire an OutDone here
1838 * as we ignore the RXFIFO.
1839 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001840
1841 s3c_hsotg_handle_outdone(hsotg, idx, false);
1842 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001843 }
1844
Dinh Nguyen47a16852014-04-14 14:13:34 -07001845 if (ints & DXEPINT_EPDISBLD) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001846 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001847
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001848 if (dir_in) {
1849 int epctl = readl(hsotg->regs + epctl_reg);
1850
Robert Baldygab203d0a2014-09-09 10:44:56 +02001851 s3c_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001852
Dinh Nguyen47a16852014-04-14 14:13:34 -07001853 if ((epctl & DXEPCTL_STALL) &&
1854 (epctl & DXEPCTL_EPTYPE_BULK)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001855 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001856
Dinh Nguyen47a16852014-04-14 14:13:34 -07001857 dctl |= DCTL_CGNPINNAK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001858 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001859 }
1860 }
1861 }
1862
Dinh Nguyen47a16852014-04-14 14:13:34 -07001863 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001864 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001865
Dinh Nguyen47a16852014-04-14 14:13:34 -07001866 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001867 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1868
1869 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001870 /*
1871 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001872 * setup packet. In non-DMA mode we'd get this
1873 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001874 * the setup here.
1875 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001876
1877 if (dir_in)
1878 WARN_ON_ONCE(1);
1879 else
1880 s3c_hsotg_handle_outdone(hsotg, 0, true);
1881 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001882 }
1883
Dinh Nguyen47a16852014-04-14 14:13:34 -07001884 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001885 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001886
Robert Baldyga1479e842013-10-09 08:41:57 +02001887 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001888 /* not sure if this is important, but we'll clear it anyway */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001889 if (ints & DIEPMSK_INTKNTXFEMPMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001890 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1891 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001892 }
1893
1894 /* this probably means something bad is happening */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001895 if (ints & DIEPMSK_INTKNEPMISMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001896 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1897 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001898 }
Ben Dooks10aebc72010-07-19 09:40:44 +01001899
1900 /* FIFO has space or is empty (see GAHBCFG) */
1901 if (hsotg->dedicated_fifos &&
Dinh Nguyen47a16852014-04-14 14:13:34 -07001902 ints & DIEPMSK_TXFIFOEMPTY) {
Ben Dooks10aebc72010-07-19 09:40:44 +01001903 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1904 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09001905 if (!using_dma(hsotg))
1906 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01001907 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001908 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001909}
1910
1911/**
1912 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
1913 * @hsotg: The device state.
1914 *
1915 * Handle updating the device settings after the enumeration phase has
1916 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001917 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001918static void s3c_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001919{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001920 u32 dsts = readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09001921 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001922
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001923 /*
1924 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001925 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001926 * we connected at.
1927 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001928
1929 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
1930
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001931 /*
1932 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001933 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001934 * not advertise a 64byte MPS on EP0.
1935 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001936
1937 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001938 switch (dsts & DSTS_ENUMSPD_MASK) {
1939 case DSTS_ENUMSPD_FS:
1940 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001941 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001942 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01001943 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001944 break;
1945
Dinh Nguyen47a16852014-04-14 14:13:34 -07001946 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001947 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001948 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01001949 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950 break;
1951
Dinh Nguyen47a16852014-04-14 14:13:34 -07001952 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001953 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001954 /*
1955 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001956 * moment, and the documentation seems to imply that it isn't
1957 * supported by the PHYs on some of the devices.
1958 */
1959 break;
1960 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02001961 dev_info(hsotg->dev, "new device is %s\n",
1962 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001963
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001964 /*
1965 * we should now know the maximum packet size for an
1966 * endpoint, so set the endpoints to a default value.
1967 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001968
1969 if (ep0_mps) {
1970 int i;
1971 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001972 for (i = 1; i < hsotg->num_of_eps; i++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001973 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps);
1974 }
1975
1976 /* ensure after enumeration our EP0 is active */
1977
1978 s3c_hsotg_enqueue_setup(hsotg);
1979
1980 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001981 readl(hsotg->regs + DIEPCTL0),
1982 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001983}
1984
1985/**
1986 * kill_all_requests - remove all requests from the endpoint's queue
1987 * @hsotg: The device state.
1988 * @ep: The endpoint the requests may be on.
1989 * @result: The result code to use.
1990 * @force: Force removal of any current requests
1991 *
1992 * Go through the requests on the given endpoint and mark them
1993 * completed with the given result code.
1994 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001995static void kill_all_requests(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996 struct s3c_hsotg_ep *ep,
1997 int result, bool force)
1998{
1999 struct s3c_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002000 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002001
2002 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002003 /*
2004 * currently, we can't do much about an already
2005 * running request on an in endpoint
2006 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002007
2008 if (ep->req == req && ep->dir_in && !force)
2009 continue;
2010
2011 s3c_hsotg_complete_request(hsotg, ep, req,
2012 result);
2013 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02002014 if (!hsotg->dedicated_fifos)
2015 return;
2016 size = (readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
2017 if (size < ep->fifo_size)
2018 s3c_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002019}
2020
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002021/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002022 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002023 * @hsotg: The device state.
2024 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002025 * The device has been disconnected. Remove all current
2026 * transactions and signal the gadget driver that this
2027 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002028 */
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002029void s3c_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002030{
2031 unsigned ep;
2032
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002033 if (!hsotg->connected)
2034 return;
2035
2036 hsotg->connected = 0;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002037 for (ep = 0; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002038 kill_all_requests(hsotg, &hsotg->eps[ep], -ESHUTDOWN, true);
2039
2040 call_gadget(hsotg, disconnect);
2041}
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002042EXPORT_SYMBOL_GPL(s3c_hsotg_disconnect);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002043
2044/**
2045 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2046 * @hsotg: The device state:
2047 * @periodic: True if this is a periodic FIFO interrupt
2048 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002049static void s3c_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002050{
2051 struct s3c_hsotg_ep *ep;
2052 int epno, ret;
2053
2054 /* look through for any more data to transmit */
2055
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002056 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002057 ep = &hsotg->eps[epno];
2058
2059 if (!ep->dir_in)
2060 continue;
2061
2062 if ((periodic && !ep->periodic) ||
2063 (!periodic && ep->periodic))
2064 continue;
2065
2066 ret = s3c_hsotg_trytx(hsotg, ep);
2067 if (ret < 0)
2068 break;
2069 }
2070}
2071
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002072/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002073#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2074 GINTSTS_PTXFEMP | \
2075 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002076
2077/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002078 * s3c_hsotg_corereset - issue softreset to the core
2079 * @hsotg: The device state
2080 *
2081 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002082 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002083static int s3c_hsotg_corereset(struct dwc2_hsotg *hsotg)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002084{
2085 int timeout;
2086 u32 grstctl;
2087
2088 dev_dbg(hsotg->dev, "resetting core\n");
2089
2090 /* issue soft reset */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002091 writel(GRSTCTL_CSFTRST, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002092
Du, Changbin2868fea2012-07-24 08:19:25 +08002093 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002094 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002095 grstctl = readl(hsotg->regs + GRSTCTL);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002096 } while ((grstctl & GRSTCTL_CSFTRST) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002097
Dinh Nguyen47a16852014-04-14 14:13:34 -07002098 if (grstctl & GRSTCTL_CSFTRST) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002099 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2100 return -EINVAL;
2101 }
2102
Du, Changbin2868fea2012-07-24 08:19:25 +08002103 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002104
2105 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002106 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002107
2108 if (timeout-- < 0) {
2109 dev_info(hsotg->dev,
2110 "%s: reset failed, GRSTCTL=%08x\n",
2111 __func__, grstctl);
2112 return -ETIMEDOUT;
2113 }
2114
Dinh Nguyen47a16852014-04-14 14:13:34 -07002115 if (!(grstctl & GRSTCTL_AHBIDLE))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002116 continue;
2117
2118 break; /* reset done */
2119 }
2120
2121 dev_dbg(hsotg->dev, "reset successful\n");
2122 return 0;
2123}
2124
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002125/**
2126 * s3c_hsotg_core_init - issue softreset to the core
2127 * @hsotg: The device state
2128 *
2129 * Issue a soft reset to the core, and await the core finishing it.
2130 */
Dinh Nguyen510ffaa2014-11-11 11:13:36 -06002131void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002132{
2133 s3c_hsotg_corereset(hsotg);
2134
2135 /*
2136 * we must now enable ep0 ready for host detection and then
2137 * set configuration.
2138 */
2139
2140 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002141 writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002142 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002143
2144 s3c_hsotg_init_fifo(hsotg);
2145
Dinh Nguyen47a16852014-04-14 14:13:34 -07002146 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002147
Dinh Nguyen47a16852014-04-14 14:13:34 -07002148 writel(1 << 18 | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002149
2150 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002151 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002152
2153 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002154 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002155
Dinh Nguyen47a16852014-04-14 14:13:34 -07002156 writel(GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
2157 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
2158 GINTSTS_CONIDSTSCHNG | GINTSTS_USBRST |
2159 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
2160 GINTSTS_USBSUSP | GINTSTS_WKUPINT,
2161 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002162
2163 if (using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002164 writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2165 GAHBCFG_HBSTLEN_INCR4,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002166 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002167 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07002168 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NP_TXF_EMP_LVL |
2169 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2170 GAHBCFG_GLBL_INTR_EN,
Robert Baldyga8acc8292013-09-19 11:50:23 +02002171 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002172
2173 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002174 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2175 * when we have no data to transfer. Otherwise we get being flooded by
2176 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002177 */
2178
Dinh Nguyen47a16852014-04-14 14:13:34 -07002179 writel(((hsotg->dedicated_fifos) ? DIEPMSK_TXFIFOEMPTY |
2180 DIEPMSK_INTKNTXFEMPMSK : 0) |
2181 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
2182 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2183 DIEPMSK_INTKNEPMISMSK,
2184 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002185
2186 /*
2187 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2188 * DMA mode we may need this.
2189 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002190 writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
2191 DIEPMSK_TIMEOUTMSK) : 0) |
2192 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
2193 DOEPMSK_SETUPMSK,
2194 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002195
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002196 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002197
2198 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002199 readl(hsotg->regs + DIEPCTL0),
2200 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002201
2202 /* enable in and out endpoint interrupts */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002203 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002204
2205 /*
2206 * Enable the RXFIFO when in slave mode, as this is how we collect
2207 * the data. In DMA mode, we get events from the FIFO but also
2208 * things we cannot process, so do not use it.
2209 */
2210 if (!using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002211 s3c_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002212
2213 /* Enable interrupts for EP0 in and out */
2214 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2215 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2216
Dinh Nguyen47a16852014-04-14 14:13:34 -07002217 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002218 udelay(10); /* see openiboot */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002219 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002220
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002221 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002222
2223 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002224 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002225 * writing to the EPCTL register..
2226 */
2227
2228 /* set to read 1 8byte packet */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002229 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2230 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002231
2232 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002233 DXEPCTL_CNAK | DXEPCTL_EPENA |
2234 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002235 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002236
2237 /* enable, but don't activate EP0in */
2238 writel(s3c_hsotg_ep0_mps(hsotg->eps[0].ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002239 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002240
2241 s3c_hsotg_enqueue_setup(hsotg);
2242
2243 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002244 readl(hsotg->regs + DIEPCTL0),
2245 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002246
2247 /* clear global NAKs */
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002248 writel(DCTL_CGOUTNAK | DCTL_CGNPINNAK | DCTL_SFTDISCON,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002249 hsotg->regs + DCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002250
2251 /* must be at-least 3ms to allow bus to see disconnect */
2252 mdelay(3);
2253
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002254 hsotg->last_rst = jiffies;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002255}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002256
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002257static void s3c_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002258{
2259 /* set the soft-disconnect bit */
2260 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2261}
2262
Dinh Nguyen510ffaa2014-11-11 11:13:36 -06002263void s3c_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002264{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002265 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002266 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002267}
2268
2269/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002270 * s3c_hsotg_irq - handle device interrupt
2271 * @irq: The IRQ number triggered
2272 * @pw: The pw value when registered the handler.
2273 */
2274static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2275{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002276 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002277 int retry_count = 8;
2278 u32 gintsts;
2279 u32 gintmsk;
2280
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002281 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002282irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002283 gintsts = readl(hsotg->regs + GINTSTS);
2284 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002285
2286 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2287 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2288
2289 gintsts &= gintmsk;
2290
Dinh Nguyen47a16852014-04-14 14:13:34 -07002291 if (gintsts & GINTSTS_ENUMDONE) {
2292 writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002293
2294 s3c_hsotg_irq_enumdone(hsotg);
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002295 hsotg->connected = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002296 }
2297
Dinh Nguyen47a16852014-04-14 14:13:34 -07002298 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002299 u32 daint = readl(hsotg->regs + DAINT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002300 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2301 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002302 int ep;
2303
Robert Baldyga7e804652013-09-19 11:50:20 +02002304 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002305 daint_out = daint >> DAINT_OUTEP_SHIFT;
2306 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002307
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002308 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2309
2310 for (ep = 0; ep < 15 && daint_out; ep++, daint_out >>= 1) {
2311 if (daint_out & 1)
2312 s3c_hsotg_epint(hsotg, ep, 0);
2313 }
2314
2315 for (ep = 0; ep < 15 && daint_in; ep++, daint_in >>= 1) {
2316 if (daint_in & 1)
2317 s3c_hsotg_epint(hsotg, ep, 1);
2318 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002319 }
2320
Dinh Nguyen47a16852014-04-14 14:13:34 -07002321 if (gintsts & GINTSTS_USBRST) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002322
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002323 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002324
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002325 dev_info(hsotg->dev, "%s: USBRst\n", __func__);
2326 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002327 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002328
Dinh Nguyen47a16852014-04-14 14:13:34 -07002329 writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002330
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002331 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002332 if (time_after(jiffies, hsotg->last_rst +
2333 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002334
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002335 kill_all_requests(hsotg, &hsotg->eps[0],
2336 -ECONNRESET, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002337
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002338 s3c_hsotg_core_init_disconnected(hsotg);
2339 s3c_hsotg_core_connect(hsotg);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002340 }
2341 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002342 }
2343
2344 /* check both FIFOs */
2345
Dinh Nguyen47a16852014-04-14 14:13:34 -07002346 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002347 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2348
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002349 /*
2350 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002351 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002352 * it needs re-enabling
2353 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002354
Dinh Nguyen47a16852014-04-14 14:13:34 -07002355 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002356 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002357 }
2358
Dinh Nguyen47a16852014-04-14 14:13:34 -07002359 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002360 dev_dbg(hsotg->dev, "PTxFEmp\n");
2361
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002362 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002363
Dinh Nguyen47a16852014-04-14 14:13:34 -07002364 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002365 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366 }
2367
Dinh Nguyen47a16852014-04-14 14:13:34 -07002368 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002369 /*
2370 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002371 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002372 * set.
2373 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002374
2375 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002376 }
2377
Dinh Nguyen47a16852014-04-14 14:13:34 -07002378 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002379 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Dinh Nguyen47a16852014-04-14 14:13:34 -07002380 writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002381 }
2382
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002383 /*
2384 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002385 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002386 * the occurrence.
2387 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002388
Dinh Nguyen47a16852014-04-14 14:13:34 -07002389 if (gintsts & GINTSTS_GOUTNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002390 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2391
Dinh Nguyen47a16852014-04-14 14:13:34 -07002392 writel(DCTL_CGOUTNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002393
2394 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002395 }
2396
Dinh Nguyen47a16852014-04-14 14:13:34 -07002397 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002398 dev_info(hsotg->dev, "GINNakEff triggered\n");
2399
Dinh Nguyen47a16852014-04-14 14:13:34 -07002400 writel(DCTL_CGNPINNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002401
2402 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002403 }
2404
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002405 /*
2406 * if we've had fifo events, we should try and go around the
2407 * loop again to see if there's any point in returning yet.
2408 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002409
2410 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2411 goto irq_retry;
2412
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002413 spin_unlock(&hsotg->lock);
2414
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002415 return IRQ_HANDLED;
2416}
2417
2418/**
2419 * s3c_hsotg_ep_enable - enable the given endpoint
2420 * @ep: The USB endpint to configure
2421 * @desc: The USB endpoint descriptor to configure with.
2422 *
2423 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002424 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002425static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2426 const struct usb_endpoint_descriptor *desc)
2427{
2428 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002429 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002430 unsigned long flags;
2431 int index = hs_ep->index;
2432 u32 epctrl_reg;
2433 u32 epctrl;
2434 u32 mps;
2435 int dir_in;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002436 int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02002437 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002438
2439 dev_dbg(hsotg->dev,
2440 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2441 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2442 desc->wMaxPacketSize, desc->bInterval);
2443
2444 /* not to be called for EP0 */
2445 WARN_ON(index == 0);
2446
2447 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2448 if (dir_in != hs_ep->dir_in) {
2449 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2450 return -EINVAL;
2451 }
2452
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002453 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002454
2455 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2456
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002457 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002458 epctrl = readl(hsotg->regs + epctrl_reg);
2459
2460 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2461 __func__, epctrl, epctrl_reg);
2462
Lukasz Majewski22258f42012-06-14 10:02:24 +02002463 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002464
Dinh Nguyen47a16852014-04-14 14:13:34 -07002465 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2466 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002467
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002468 /*
2469 * mark the endpoint as active, otherwise the core may ignore
2470 * transactions entirely for this endpoint
2471 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002472 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002473
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002474 /*
2475 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002476 * do something with data that we've yet got a request to process
2477 * since the RXFIFO will take data for an endpoint even if the
2478 * size register hasn't been set.
2479 */
2480
Dinh Nguyen47a16852014-04-14 14:13:34 -07002481 epctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002482
2483 /* update the endpoint state */
Robert Baldygae9edd1992013-10-09 08:20:02 +02002484 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002485
2486 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002487 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002488 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002489 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002490 hs_ep->interval = desc->bInterval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002491
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002492 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2493 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2494
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002495 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2496 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002497 epctrl |= DXEPCTL_EPTYPE_ISO;
2498 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02002499 hs_ep->isochronous = 1;
2500 if (dir_in)
2501 hs_ep->periodic = 1;
2502 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002503
2504 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002505 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002506 break;
2507
2508 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02002509 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002510 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002511
Dinh Nguyen47a16852014-04-14 14:13:34 -07002512 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002513 break;
2514
2515 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002516 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002517 break;
2518 }
2519
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002520 /*
2521 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002522 * a unique tx-fifo even if it is non-periodic.
2523 */
Robert Baldygab203d0a2014-09-09 10:44:56 +02002524 if (dir_in && hsotg->dedicated_fifos) {
2525 size = hs_ep->ep.maxpacket*hs_ep->mc;
2526 for (i = 1; i <= 8; ++i) {
2527 if (hsotg->fifo_map & (1<<i))
2528 continue;
2529 val = readl(hsotg->regs + DPTXFSIZN(i));
2530 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
2531 if (val < size)
2532 continue;
2533 hsotg->fifo_map |= 1<<i;
2534
2535 epctrl |= DXEPCTL_TXFNUM(i);
2536 hs_ep->fifo_index = i;
2537 hs_ep->fifo_size = val;
2538 break;
2539 }
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302540 if (i == 8) {
2541 ret = -ENOMEM;
2542 goto error;
2543 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02002544 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002545
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002546 /* for non control endpoints, set PID to D0 */
2547 if (index)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002548 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002549
2550 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2551 __func__, epctrl);
2552
2553 writel(epctrl, hsotg->regs + epctrl_reg);
2554 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2555 __func__, readl(hsotg->regs + epctrl_reg));
2556
2557 /* enable the endpoint interrupt */
2558 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2559
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302560error:
Lukasz Majewski22258f42012-06-14 10:02:24 +02002561 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002562 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002563}
2564
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002565/**
2566 * s3c_hsotg_ep_disable - disable given endpoint
2567 * @ep: The endpoint to disable.
2568 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002569static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2570{
2571 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002572 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002573 int dir_in = hs_ep->dir_in;
2574 int index = hs_ep->index;
2575 unsigned long flags;
2576 u32 epctrl_reg;
2577 u32 ctrl;
2578
Marek Szyprowski1e011292014-09-09 10:44:54 +02002579 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002580
2581 if (ep == &hsotg->eps[0].ep) {
2582 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2583 return -EINVAL;
2584 }
2585
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002586 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002587
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002588 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002589 /* terminate all requests with shutdown */
2590 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN, false);
2591
Robert Baldygab203d0a2014-09-09 10:44:56 +02002592 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
2593 hs_ep->fifo_index = 0;
2594 hs_ep->fifo_size = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002595
2596 ctrl = readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002597 ctrl &= ~DXEPCTL_EPENA;
2598 ctrl &= ~DXEPCTL_USBACTEP;
2599 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002600
2601 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2602 writel(ctrl, hsotg->regs + epctrl_reg);
2603
2604 /* disable endpoint interrupts */
2605 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2606
Lukasz Majewski22258f42012-06-14 10:02:24 +02002607 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002608 return 0;
2609}
2610
2611/**
2612 * on_list - check request is on the given endpoint
2613 * @ep: The endpoint to check.
2614 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002615 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002616static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2617{
2618 struct s3c_hsotg_req *req, *treq;
2619
2620 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2621 if (req == test)
2622 return true;
2623 }
2624
2625 return false;
2626}
2627
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002628/**
2629 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2630 * @ep: The endpoint to dequeue.
2631 * @req: The request to be removed from a queue.
2632 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002633static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2634{
2635 struct s3c_hsotg_req *hs_req = our_req(req);
2636 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002637 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002638 unsigned long flags;
2639
Marek Szyprowski1e011292014-09-09 10:44:54 +02002640 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002641
Lukasz Majewski22258f42012-06-14 10:02:24 +02002642 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002643
2644 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002645 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002646 return -EINVAL;
2647 }
2648
2649 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002650 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002651
2652 return 0;
2653}
2654
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002655/**
2656 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2657 * @ep: The endpoint to set halt.
2658 * @value: Set or unset the halt.
2659 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002660static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2661{
2662 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002663 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002664 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002665 u32 epreg;
2666 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002667 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002668
2669 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2670
Robert Baldygac9f721b2014-01-14 08:36:00 +01002671 if (index == 0) {
2672 if (value)
2673 s3c_hsotg_stall_ep0(hs);
2674 else
2675 dev_warn(hs->dev,
2676 "%s: can't clear halt on ep0\n", __func__);
2677 return 0;
2678 }
2679
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002680 /* write both IN and OUT control registers */
2681
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002682 epreg = DIEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002683 epctl = readl(hs->regs + epreg);
2684
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002685 if (value) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002686 epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
2687 if (epctl & DXEPCTL_EPENA)
2688 epctl |= DXEPCTL_EPDIS;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002689 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002690 epctl &= ~DXEPCTL_STALL;
2691 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2692 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2693 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2694 epctl |= DXEPCTL_SETD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002695 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002696
2697 writel(epctl, hs->regs + epreg);
2698
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002699 epreg = DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002700 epctl = readl(hs->regs + epreg);
2701
2702 if (value)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002703 epctl |= DXEPCTL_STALL;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002704 else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002705 epctl &= ~DXEPCTL_STALL;
2706 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2707 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2708 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2709 epctl |= DXEPCTL_SETD0PID;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002710 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002711
2712 writel(epctl, hs->regs + epreg);
2713
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002714 hs_ep->halted = value;
2715
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002716 return 0;
2717}
2718
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002719/**
2720 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2721 * @ep: The endpoint to set halt.
2722 * @value: Set or unset the halt.
2723 */
2724static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2725{
2726 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002727 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002728 unsigned long flags = 0;
2729 int ret = 0;
2730
2731 spin_lock_irqsave(&hs->lock, flags);
2732 ret = s3c_hsotg_ep_sethalt(ep, value);
2733 spin_unlock_irqrestore(&hs->lock, flags);
2734
2735 return ret;
2736}
2737
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002738static struct usb_ep_ops s3c_hsotg_ep_ops = {
2739 .enable = s3c_hsotg_ep_enable,
2740 .disable = s3c_hsotg_ep_disable,
2741 .alloc_request = s3c_hsotg_ep_alloc_request,
2742 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002743 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002744 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002745 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002746 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002747};
2748
2749/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002750 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002751 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002752 *
2753 * A wrapper for platform code responsible for controlling
2754 * low-level USB code
2755 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002756static void s3c_hsotg_phy_enable(struct dwc2_hsotg *hsotg)
Lukasz Majewski41188782012-05-04 14:17:01 +02002757{
2758 struct platform_device *pdev = to_platform_device(hsotg->dev);
2759
2760 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302761
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002762 if (hsotg->uphy)
2763 usb_phy_init(hsotg->uphy);
2764 else if (hsotg->plat && hsotg->plat->phy_init)
2765 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2766 else {
Matt Porter74084842013-12-19 09:23:06 -05002767 phy_init(hsotg->phy);
2768 phy_power_on(hsotg->phy);
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002769 }
Lukasz Majewski41188782012-05-04 14:17:01 +02002770}
2771
2772/**
2773 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002774 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002775 *
2776 * A wrapper for platform code responsible for controlling
2777 * low-level USB code
2778 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002779static void s3c_hsotg_phy_disable(struct dwc2_hsotg *hsotg)
Lukasz Majewski41188782012-05-04 14:17:01 +02002780{
2781 struct platform_device *pdev = to_platform_device(hsotg->dev);
2782
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002783 if (hsotg->uphy)
2784 usb_phy_shutdown(hsotg->uphy);
2785 else if (hsotg->plat && hsotg->plat->phy_exit)
2786 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2787 else {
Matt Porter74084842013-12-19 09:23:06 -05002788 phy_power_off(hsotg->phy);
2789 phy_exit(hsotg->phy);
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002790 }
Lukasz Majewski41188782012-05-04 14:17:01 +02002791}
2792
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002793/**
2794 * s3c_hsotg_init - initalize the usb core
2795 * @hsotg: The driver state
2796 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002797static void s3c_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002798{
2799 /* unmask subset of endpoint interrupts */
2800
Dinh Nguyen47a16852014-04-14 14:13:34 -07002801 writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2802 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
2803 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002804
Dinh Nguyen47a16852014-04-14 14:13:34 -07002805 writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
2806 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
2807 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002808
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002809 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002810
2811 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002812 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002813
2814 if (0) {
2815 /* post global nak until we're ready */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002816 writel(DCTL_SGNPINNAK | DCTL_SGOUTNAK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002817 hsotg->regs + DCTL);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002818 }
2819
2820 /* setup fifos */
2821
2822 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002823 readl(hsotg->regs + GRXFSIZ),
2824 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002825
2826 s3c_hsotg_init_fifo(hsotg);
2827
2828 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002829 writel(GUSBCFG_PHYIF16 | GUSBCFG_TOUTCAL(7) | (0x5 << 10),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002830 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002831
Dinh Nguyen47a16852014-04-14 14:13:34 -07002832 writel(using_dma(hsotg) ? GAHBCFG_DMA_EN : 0x0,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002833 hsotg->regs + GAHBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002834}
2835
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002836/**
2837 * s3c_hsotg_udc_start - prepare the udc for work
2838 * @gadget: The usb gadget state
2839 * @driver: The usb gadget driver
2840 *
2841 * Perform initialization to prepare udc device and driver
2842 * to work.
2843 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002844static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2845 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002846{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002847 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002848 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002849 int ret;
2850
2851 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02002852 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002853 return -ENODEV;
2854 }
2855
2856 if (!driver) {
2857 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2858 return -EINVAL;
2859 }
2860
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002861 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002862 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002863
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002864 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002865 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2866 return -EINVAL;
2867 }
2868
2869 WARN_ON(hsotg->driver);
2870
2871 driver->driver.bus = NULL;
2872 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03002873 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002874 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2875
Robert Baldygad00b4142014-09-09 10:44:57 +02002876 clk_enable(hsotg->clk);
2877
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002878 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
2879 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002880 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002881 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002882 goto err;
2883 }
2884
Marek Szyprowskic816c472014-10-20 12:45:37 +02002885 s3c_hsotg_phy_enable(hsotg);
2886
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002887 spin_lock_irqsave(&hsotg->lock, flags);
2888 s3c_hsotg_init(hsotg);
2889 s3c_hsotg_core_init_disconnected(hsotg);
2890 spin_unlock_irqrestore(&hsotg->lock, flags);
2891
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002892 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002893
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002894 return 0;
2895
2896err:
2897 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002898 return ret;
2899}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002900
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002901/**
2902 * s3c_hsotg_udc_stop - stop the udc
2903 * @gadget: The usb gadget state
2904 * @driver: The usb gadget driver
2905 *
2906 * Stop udc hw block and stay tunned for future transmissions
2907 */
Felipe Balbi22835b82014-10-17 12:05:12 -05002908static int s3c_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002909{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002910 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002911 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002912 int ep;
2913
2914 if (!hsotg)
2915 return -ENODEV;
2916
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002917 /* all endpoints should be shutdown */
Robert Baldyga604eac3c2014-09-09 10:44:13 +02002918 for (ep = 1; ep < hsotg->num_of_eps; ep++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002919 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
2920
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002921 spin_lock_irqsave(&hsotg->lock, flags);
2922
Marek Szyprowski32805c32014-10-20 12:45:33 +02002923 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002924 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002925
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002926 spin_unlock_irqrestore(&hsotg->lock, flags);
2927
Marek Szyprowskic816c472014-10-20 12:45:37 +02002928 s3c_hsotg_phy_disable(hsotg);
2929
Marek Szyprowskic8c10252013-09-12 16:18:48 +02002930 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002931
Robert Baldygad00b4142014-09-09 10:44:57 +02002932 clk_disable(hsotg->clk);
2933
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002934 return 0;
2935}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002936
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002937/**
2938 * s3c_hsotg_gadget_getframe - read the frame number
2939 * @gadget: The usb gadget state
2940 *
2941 * Read the {micro} frame number
2942 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002943static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2944{
2945 return s3c_hsotg_read_frameno(to_hsotg(gadget));
2946}
2947
Lukasz Majewskia188b682012-06-22 09:29:56 +02002948/**
2949 * s3c_hsotg_pullup - connect/disconnect the USB PHY
2950 * @gadget: The usb gadget state
2951 * @is_on: Current state of the USB PHY
2952 *
2953 * Connect/Disconnect the USB PHY pullup
2954 */
2955static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
2956{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002957 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002958 unsigned long flags = 0;
2959
Andrzej Pietrasiewiczd784f1e2014-09-09 10:44:53 +02002960 dev_dbg(hsotg->dev, "%s: is_on: %d\n", __func__, is_on);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002961
2962 spin_lock_irqsave(&hsotg->lock, flags);
2963 if (is_on) {
Robert Baldygad00b4142014-09-09 10:44:57 +02002964 clk_enable(hsotg->clk);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002965 s3c_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002966 } else {
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002967 s3c_hsotg_core_disconnect(hsotg);
Robert Baldygad00b4142014-09-09 10:44:57 +02002968 clk_disable(hsotg->clk);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002969 }
2970
2971 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2972 spin_unlock_irqrestore(&hsotg->lock, flags);
2973
2974 return 0;
2975}
2976
Felipe Balbieeef4582013-01-24 17:58:16 +02002977static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002978 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002979 .udc_start = s3c_hsotg_udc_start,
2980 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02002981 .pullup = s3c_hsotg_pullup,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002982};
2983
2984/**
2985 * s3c_hsotg_initep - initialise a single endpoint
2986 * @hsotg: The device state.
2987 * @hs_ep: The endpoint to be initialised.
2988 * @epnum: The endpoint number
2989 *
2990 * Initialise the given endpoint (as part of the probe and device state
2991 * creation) to give to the gadget driver. Setup the endpoint name, any
2992 * direction information and other state that may be required.
2993 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002994static void s3c_hsotg_initep(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002995 struct s3c_hsotg_ep *hs_ep,
2996 int epnum)
2997{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002998 char *dir;
2999
3000 if (epnum == 0)
3001 dir = "";
3002 else if ((epnum % 2) == 0) {
3003 dir = "out";
3004 } else {
3005 dir = "in";
3006 hs_ep->dir_in = 1;
3007 }
3008
3009 hs_ep->index = epnum;
3010
3011 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3012
3013 INIT_LIST_HEAD(&hs_ep->queue);
3014 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3015
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003016 /* add to the list of endpoints known by the gadget driver */
3017 if (epnum)
3018 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3019
3020 hs_ep->parent = hsotg;
3021 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003022 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003023 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3024
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003025 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003026 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003027 * to be something valid.
3028 */
3029
3030 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003031 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003032 writel(next, hsotg->regs + DIEPCTL(epnum));
3033 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003034 }
3035}
3036
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003037/**
3038 * s3c_hsotg_hw_cfg - read HW configuration registers
3039 * @param: The device state
3040 *
3041 * Read the USB core HW configuration registers
3042 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003043static void s3c_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003044{
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003045 u32 cfg2, cfg3, cfg4;
Ben Dooks10aebc72010-07-19 09:40:44 +01003046 /* check hardware configuration */
3047
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003048 cfg2 = readl(hsotg->regs + 0x48);
3049 hsotg->num_of_eps = (cfg2 >> 10) & 0xF;
3050
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003051 cfg3 = readl(hsotg->regs + 0x4C);
3052 hsotg->fifo_mem = (cfg3 >> 16);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003053
Ben Dooks10aebc72010-07-19 09:40:44 +01003054 cfg4 = readl(hsotg->regs + 0x50);
3055 hsotg->dedicated_fifos = (cfg4 >> 25) & 1;
3056
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003057 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3058 hsotg->num_of_eps,
3059 hsotg->dedicated_fifos ? "dedicated" : "shared",
3060 hsotg->fifo_mem);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003061}
3062
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003063/**
3064 * s3c_hsotg_dump - dump state of the udc
3065 * @param: The device state
3066 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003067static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003068{
Mark Brown83a01802011-06-01 17:16:15 +01003069#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003070 struct device *dev = hsotg->dev;
3071 void __iomem *regs = hsotg->regs;
3072 u32 val;
3073 int idx;
3074
3075 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003076 readl(regs + DCFG), readl(regs + DCTL),
3077 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003078
3079 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003080 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003081
3082 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003083 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003084
3085 /* show periodic fifo settings */
3086
3087 for (idx = 1; idx <= 15; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003088 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003089 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003090 val >> FIFOSIZE_DEPTH_SHIFT,
3091 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003092 }
3093
3094 for (idx = 0; idx < 15; idx++) {
3095 dev_info(dev,
3096 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003097 readl(regs + DIEPCTL(idx)),
3098 readl(regs + DIEPTSIZ(idx)),
3099 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003100
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003101 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003102 dev_info(dev,
3103 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003104 idx, readl(regs + DOEPCTL(idx)),
3105 readl(regs + DOEPTSIZ(idx)),
3106 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003107
3108 }
3109
3110 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003111 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003112#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003113}
3114
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115/**
3116 * state_show - debugfs: show overall driver and device state.
3117 * @seq: The seq file to write to.
3118 * @v: Unused parameter.
3119 *
3120 * This debugfs entry shows the overall state of the hardware and
3121 * some general information about each of the endpoints available
3122 * to the system.
3123 */
3124static int state_show(struct seq_file *seq, void *v)
3125{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003126 struct dwc2_hsotg *hsotg = seq->private;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003127 void __iomem *regs = hsotg->regs;
3128 int idx;
3129
3130 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003131 readl(regs + DCFG),
3132 readl(regs + DCTL),
3133 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003134
3135 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003136 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003137
3138 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003139 readl(regs + GINTMSK),
3140 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003141
3142 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003143 readl(regs + DAINTMSK),
3144 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003145
3146 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003147 readl(regs + GNPTXSTS),
3148 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003149
Pavel Macheka023da32013-09-30 14:56:02 +02003150 seq_puts(seq, "\nEndpoint status:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003151
3152 for (idx = 0; idx < 15; idx++) {
3153 u32 in, out;
3154
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003155 in = readl(regs + DIEPCTL(idx));
3156 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003157
3158 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3159 idx, in, out);
3160
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003161 in = readl(regs + DIEPTSIZ(idx));
3162 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003163
3164 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3165 in, out);
3166
Pavel Macheka023da32013-09-30 14:56:02 +02003167 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003168 }
3169
3170 return 0;
3171}
3172
3173static int state_open(struct inode *inode, struct file *file)
3174{
3175 return single_open(file, state_show, inode->i_private);
3176}
3177
3178static const struct file_operations state_fops = {
3179 .owner = THIS_MODULE,
3180 .open = state_open,
3181 .read = seq_read,
3182 .llseek = seq_lseek,
3183 .release = single_release,
3184};
3185
3186/**
3187 * fifo_show - debugfs: show the fifo information
3188 * @seq: The seq_file to write data to.
3189 * @v: Unused parameter.
3190 *
3191 * Show the FIFO information for the overall fifo and all the
3192 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003193 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003194static int fifo_show(struct seq_file *seq, void *v)
3195{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003196 struct dwc2_hsotg *hsotg = seq->private;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003197 void __iomem *regs = hsotg->regs;
3198 u32 val;
3199 int idx;
3200
Pavel Macheka023da32013-09-30 14:56:02 +02003201 seq_puts(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003202 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003203
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003204 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003205 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Dinh Nguyen47a16852014-04-14 14:13:34 -07003206 val >> FIFOSIZE_DEPTH_SHIFT,
3207 val & FIFOSIZE_DEPTH_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003208
Pavel Macheka023da32013-09-30 14:56:02 +02003209 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003210
3211 for (idx = 1; idx <= 15; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003212 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003213
3214 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003215 val >> FIFOSIZE_DEPTH_SHIFT,
3216 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003217 }
3218
3219 return 0;
3220}
3221
3222static int fifo_open(struct inode *inode, struct file *file)
3223{
3224 return single_open(file, fifo_show, inode->i_private);
3225}
3226
3227static const struct file_operations fifo_fops = {
3228 .owner = THIS_MODULE,
3229 .open = fifo_open,
3230 .read = seq_read,
3231 .llseek = seq_lseek,
3232 .release = single_release,
3233};
3234
3235
3236static const char *decode_direction(int is_in)
3237{
3238 return is_in ? "in" : "out";
3239}
3240
3241/**
3242 * ep_show - debugfs: show the state of an endpoint.
3243 * @seq: The seq_file to write data to.
3244 * @v: Unused parameter.
3245 *
3246 * This debugfs entry shows the state of the given endpoint (one is
3247 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003248 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003249static int ep_show(struct seq_file *seq, void *v)
3250{
3251 struct s3c_hsotg_ep *ep = seq->private;
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003252 struct dwc2_hsotg *hsotg = ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003253 struct s3c_hsotg_req *req;
3254 void __iomem *regs = hsotg->regs;
3255 int index = ep->index;
3256 int show_limit = 15;
3257 unsigned long flags;
3258
3259 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3260 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3261
3262 /* first show the register state */
3263
3264 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003265 readl(regs + DIEPCTL(index)),
3266 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003267
3268 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003269 readl(regs + DIEPDMA(index)),
3270 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003271
3272 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003273 readl(regs + DIEPINT(index)),
3274 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003275
3276 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003277 readl(regs + DIEPTSIZ(index)),
3278 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003279
Pavel Macheka023da32013-09-30 14:56:02 +02003280 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003281 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3282 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3283
3284 seq_printf(seq, "request list (%p,%p):\n",
3285 ep->queue.next, ep->queue.prev);
3286
Lukasz Majewski22258f42012-06-14 10:02:24 +02003287 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003288
3289 list_for_each_entry(req, &ep->queue, queue) {
3290 if (--show_limit < 0) {
Pavel Macheka023da32013-09-30 14:56:02 +02003291 seq_puts(seq, "not showing more requests...\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003292 break;
3293 }
3294
3295 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3296 req == ep->req ? '*' : ' ',
3297 req, req->req.length, req->req.buf);
3298 seq_printf(seq, "%d done, res %d\n",
3299 req->req.actual, req->req.status);
3300 }
3301
Lukasz Majewski22258f42012-06-14 10:02:24 +02003302 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003303
3304 return 0;
3305}
3306
3307static int ep_open(struct inode *inode, struct file *file)
3308{
3309 return single_open(file, ep_show, inode->i_private);
3310}
3311
3312static const struct file_operations ep_fops = {
3313 .owner = THIS_MODULE,
3314 .open = ep_open,
3315 .read = seq_read,
3316 .llseek = seq_lseek,
3317 .release = single_release,
3318};
3319
3320/**
3321 * s3c_hsotg_create_debug - create debugfs directory and files
3322 * @hsotg: The driver state
3323 *
3324 * Create the debugfs files to allow the user to get information
3325 * about the state of the system. The directory name is created
3326 * with the same name as the device itself, in case we end up
3327 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003328 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003329static void s3c_hsotg_create_debug(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003330{
3331 struct dentry *root;
3332 unsigned epidx;
3333
3334 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3335 hsotg->debug_root = root;
3336 if (IS_ERR(root)) {
3337 dev_err(hsotg->dev, "cannot create debug root\n");
3338 return;
3339 }
3340
3341 /* create general state file */
3342
3343 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3344 hsotg, &state_fops);
3345
3346 if (IS_ERR(hsotg->debug_file))
3347 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3348
3349 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3350 hsotg, &fifo_fops);
3351
3352 if (IS_ERR(hsotg->debug_fifo))
3353 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3354
3355 /* create one file for each endpoint */
3356
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003357 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003358 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3359
3360 ep->debugfs = debugfs_create_file(ep->name, 0444,
3361 root, ep, &ep_fops);
3362
3363 if (IS_ERR(ep->debugfs))
3364 dev_err(hsotg->dev, "failed to create %s debug file\n",
3365 ep->name);
3366 }
3367}
3368
3369/**
3370 * s3c_hsotg_delete_debug - cleanup debugfs entries
3371 * @hsotg: The driver state
3372 *
3373 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003374 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003375static void s3c_hsotg_delete_debug(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003376{
3377 unsigned epidx;
3378
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003379 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003380 struct s3c_hsotg_ep *ep = &hsotg->eps[epidx];
3381 debugfs_remove(ep->debugfs);
3382 }
3383
3384 debugfs_remove(hsotg->debug_file);
3385 debugfs_remove(hsotg->debug_fifo);
3386 debugfs_remove(hsotg->debug_root);
3387}
3388
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003389/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06003390 * dwc2_gadget_init - init function for gadget
3391 * @dwc2: The data structure for the DWC2 driver.
3392 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003393 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003394int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003395{
Dinh Nguyen117777b2014-11-11 11:13:34 -06003396 struct device *dev = hsotg->dev;
3397 struct s3c_hsotg_plat *plat = dev->platform_data;
Matt Porter74084842013-12-19 09:23:06 -05003398 struct phy *phy;
3399 struct usb_phy *uphy;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003400 struct s3c_hsotg_ep *eps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003401 int epnum;
3402 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003403 int i;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003404
Kamil Debski1b59fc72014-09-09 10:44:52 +02003405 /* Set default UTMI width */
3406 hsotg->phyif = GUSBCFG_PHYIF16;
3407
Matt Porter74084842013-12-19 09:23:06 -05003408 /*
3409 * Attempt to find a generic PHY, then look for an old style
3410 * USB PHY, finally fall back to pdata
3411 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003412 phy = devm_phy_get(dev, "usb2-phy");
Felipe Balbif4f5ba52013-03-15 10:56:19 +02003413 if (IS_ERR(phy)) {
Matt Porter74084842013-12-19 09:23:06 -05003414 uphy = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
3415 if (IS_ERR(uphy)) {
3416 /* Fallback for pdata */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003417 plat = dev_get_platdata(dev);
Matt Porter74084842013-12-19 09:23:06 -05003418 if (!plat) {
Dinh Nguyen117777b2014-11-11 11:13:34 -06003419 dev_err(dev,
Matt Porter74084842013-12-19 09:23:06 -05003420 "no platform data or transceiver defined\n");
3421 return -EPROBE_DEFER;
3422 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303423 hsotg->plat = plat;
Matt Porter74084842013-12-19 09:23:06 -05003424 } else
3425 hsotg->uphy = uphy;
Kamil Debski1b59fc72014-09-09 10:44:52 +02003426 } else {
Praveen Panerib2e587d2012-11-14 15:57:16 +05303427 hsotg->phy = phy;
Kamil Debski1b59fc72014-09-09 10:44:52 +02003428 /*
3429 * If using the generic PHY framework, check if the PHY bus
3430 * width is 8-bit and set the phyif appropriately.
3431 */
3432 if (phy_get_bus_width(phy) == 8)
3433 hsotg->phyif = GUSBCFG_PHYIF8;
3434 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303435
Dinh Nguyen117777b2014-11-11 11:13:34 -06003436 hsotg->clk = devm_clk_get(dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003437 if (IS_ERR(hsotg->clk)) {
Dinh Nguyen8d736d82014-11-11 11:13:38 -06003438 hsotg->clk = NULL;
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003439 dev_err(dev, "cannot get otg clock\n");
Sachin Kamat338edab2012-05-18 14:33:46 +05303440 return PTR_ERR(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003441 }
3442
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003443 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003444 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3445 hsotg->gadget.name = dev_name(dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003446
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003447 /* reset the system */
3448
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003449 clk_prepare_enable(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003450
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003451 /* regulators */
3452
3453 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3454 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3455
Sachin Kamatcd762132013-01-08 14:27:00 +05303456 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003457 hsotg->supplies);
3458 if (ret) {
Dinh Nguyen117777b2014-11-11 11:13:34 -06003459 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303460 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003461 }
3462
3463 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3464 hsotg->supplies);
3465
3466 if (ret) {
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003467 dev_err(dev, "failed to enable supplies: %d\n", ret);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003468 goto err_supplies;
3469 }
3470
Lukasz Majewski41188782012-05-04 14:17:01 +02003471 /* usb phy enable */
3472 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003473
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003474 s3c_hsotg_corereset(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003475 s3c_hsotg_hw_cfg(hsotg);
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003476 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003477
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003478 ret = devm_request_irq(hsotg->dev, irq, s3c_hsotg_irq, IRQF_SHARED,
3479 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003480 if (ret < 0) {
3481 s3c_hsotg_phy_disable(hsotg);
3482 clk_disable_unprepare(hsotg->clk);
3483 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3484 hsotg->supplies);
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003485 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003486 goto err_clk;
3487 }
3488
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003489 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3490
3491 if (hsotg->num_of_eps == 0) {
3492 dev_err(dev, "wrong number of EPs (zero)\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003493 ret = -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003494 goto err_supplies;
3495 }
3496
3497 eps = kcalloc(hsotg->num_of_eps + 1, sizeof(struct s3c_hsotg_ep),
3498 GFP_KERNEL);
3499 if (!eps) {
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003500 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003501 goto err_supplies;
3502 }
3503
3504 hsotg->eps = eps;
3505
3506 /* setup endpoint information */
3507
3508 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
3509 hsotg->gadget.ep0 = &hsotg->eps[0].ep;
3510
3511 /* allocate EP0 request */
3512
3513 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps[0].ep,
3514 GFP_KERNEL);
3515 if (!hsotg->ctrl_req) {
3516 dev_err(dev, "failed to allocate ctrl req\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003517 ret = -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003518 goto err_ep_mem;
3519 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003520
3521 /* initialise the endpoints now the core has been initialised */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003522 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003523 s3c_hsotg_initep(hsotg, &hsotg->eps[epnum], epnum);
3524
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003525 /* disable power and clock */
Marek Szyprowski3a8146a2014-10-20 12:45:34 +02003526 s3c_hsotg_phy_disable(hsotg);
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003527
3528 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3529 hsotg->supplies);
3530 if (ret) {
Dinh Nguyen117777b2014-11-11 11:13:34 -06003531 dev_err(dev, "failed to disable supplies: %d\n", ret);
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003532 goto err_ep_mem;
3533 }
3534
Dinh Nguyen117777b2014-11-11 11:13:34 -06003535 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003536 if (ret)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003537 goto err_ep_mem;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003538
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003539 s3c_hsotg_create_debug(hsotg);
3540
3541 s3c_hsotg_dump(hsotg);
3542
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003543 return 0;
3544
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003545err_ep_mem:
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003546 kfree(eps);
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003547err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003548 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003549err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003550 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05303551
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003552 return ret;
3553}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003554EXPORT_SYMBOL_GPL(dwc2_gadget_init);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003555
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003556/**
3557 * s3c_hsotg_remove - remove function for hsotg driver
3558 * @pdev: The platform information for the driver
3559 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003560int s3c_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003561{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003562 usb_del_gadget_udc(&hsotg->gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003563 s3c_hsotg_delete_debug(hsotg);
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003564 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003565
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003566 return 0;
3567}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003568EXPORT_SYMBOL_GPL(s3c_hsotg_remove);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003569
Dinh Nguyen117777b2014-11-11 11:13:34 -06003570int s3c_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003571{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003572 unsigned long flags;
3573 int ret = 0;
3574
3575 if (hsotg->driver)
3576 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3577 hsotg->driver->driver.name);
3578
3579 spin_lock_irqsave(&hsotg->lock, flags);
Marek Szyprowski7b093f72014-10-20 12:45:39 +02003580 s3c_hsotg_core_disconnect(hsotg);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003581 s3c_hsotg_disconnect(hsotg);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003582 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3583 spin_unlock_irqrestore(&hsotg->lock, flags);
3584
Marek Szyprowski7b093f72014-10-20 12:45:39 +02003585 s3c_hsotg_phy_disable(hsotg);
3586
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003587 if (hsotg->driver) {
3588 int ep;
3589 for (ep = 0; ep < hsotg->num_of_eps; ep++)
3590 s3c_hsotg_ep_disable(&hsotg->eps[ep].ep);
3591
3592 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3593 hsotg->supplies);
Robert Baldygad00b4142014-09-09 10:44:57 +02003594 clk_disable(hsotg->clk);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003595 }
3596
3597 return ret;
3598}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003599EXPORT_SYMBOL_GPL(s3c_hsotg_suspend);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003600
Dinh Nguyen117777b2014-11-11 11:13:34 -06003601int s3c_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003602{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003603 unsigned long flags;
3604 int ret = 0;
3605
3606 if (hsotg->driver) {
3607 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3608 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02003609
3610 clk_enable(hsotg->clk);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003611 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3612 hsotg->supplies);
3613 }
3614
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003615 s3c_hsotg_phy_enable(hsotg);
Marek Szyprowski7b093f72014-10-20 12:45:39 +02003616
3617 spin_lock_irqsave(&hsotg->lock, flags);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003618 s3c_hsotg_core_init_disconnected(hsotg);
3619 s3c_hsotg_core_connect(hsotg);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003620 spin_unlock_irqrestore(&hsotg->lock, flags);
3621
3622 return ret;
3623}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003624EXPORT_SYMBOL_GPL(s3c_hsotg_resume);