blob: 3cf9c6a5e2c78a532251c69cc85c4582e4f00664 [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>
Marek Szyprowski7ad80962014-11-21 15:14:48 +010024#include <linux/mutex.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010025#include <linux/seq_file.h>
26#include <linux/delay.h>
27#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Maurus Cuelenaeree50bf382010-07-19 09:40:50 +010029#include <linux/clk.h>
Lukasz Majewskifc9a7312012-05-04 14:17:02 +020030#include <linux/regulator/consumer.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020031#include <linux/of_platform.h>
Matt Porter74084842013-12-19 09:23:06 -050032#include <linux/phy/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010033
34#include <linux/usb/ch9.h>
35#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053036#include <linux/usb/phy.h>
Lukasz Majewski126625e2012-05-09 13:16:53 +020037#include <linux/platform_data/s3c-hsotg.h>
Gregory Herrero9e14d0a2015-01-30 09:09:28 +010038#include <linux/uaccess.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010039
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070040#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060041#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010042
43/* conversion functions */
44static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
45{
46 return container_of(req, struct s3c_hsotg_req, req);
47}
48
49static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
50{
51 return container_of(ep, struct s3c_hsotg_ep, ep);
52}
53
Dinh Nguyen941fcce2014-11-11 11:13:33 -060054static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010055{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060056 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010057}
58
59static inline void __orr32(void __iomem *ptr, u32 val)
60{
61 writel(readl(ptr) | val, ptr);
62}
63
64static inline void __bic32(void __iomem *ptr, u32 val)
65{
66 writel(readl(ptr) & ~val, ptr);
67}
68
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010069static inline struct s3c_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
70 u32 ep_index, u32 dir_in)
71{
72 if (dir_in)
73 return hsotg->eps_in[ep_index];
74 else
75 return hsotg->eps_out[ep_index];
76}
77
Mickael Maison997f4f82014-12-23 17:39:45 +010078/* forward declaration of functions */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060079static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010080
81/**
82 * using_dma - return the DMA status of the driver.
83 * @hsotg: The driver state.
84 *
85 * Return true if we're using DMA.
86 *
87 * Currently, we have the DMA support code worked into everywhere
88 * that needs it, but the AMBA DMA implementation in the hardware can
89 * only DMA from 32bit aligned addresses. This means that gadgets such
90 * as the CDC Ethernet cannot work as they often pass packets which are
91 * not 32bit aligned.
92 *
93 * Unfortunately the choice to use DMA or not is global to the controller
94 * and seems to be only settable when the controller is being put through
95 * a core reset. This means we either need to fix the gadgets to take
96 * account of DMA alignment, or add bounce buffers (yuerk).
97 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010098 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010099 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600100static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100101{
Gregory Herreroedd74be2015-01-09 13:38:48 +0100102 return hsotg->g_using_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100103}
104
105/**
106 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
107 * @hsotg: The device state
108 * @ints: A bitmask of the interrupts to enable
109 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600110static void s3c_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100111{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200112 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100113 u32 new_gsintmsk;
114
115 new_gsintmsk = gsintmsk | ints;
116
117 if (new_gsintmsk != gsintmsk) {
118 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200119 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100120 }
121}
122
123/**
124 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
125 * @hsotg: The device state
126 * @ints: A bitmask of the interrupts to enable
127 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600128static void s3c_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100129{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200130 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100131 u32 new_gsintmsk;
132
133 new_gsintmsk = gsintmsk & ~ints;
134
135 if (new_gsintmsk != gsintmsk)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200136 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100137}
138
139/**
140 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
141 * @hsotg: The device state
142 * @ep: The endpoint index
143 * @dir_in: True if direction is in.
144 * @en: The enable value, true to enable
145 *
146 * Set or clear the mask for an individual endpoint's interrupt
147 * request.
148 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600149static void s3c_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100150 unsigned int ep, unsigned int dir_in,
151 unsigned int en)
152{
153 unsigned long flags;
154 u32 bit = 1 << ep;
155 u32 daint;
156
157 if (!dir_in)
158 bit <<= 16;
159
160 local_irq_save(flags);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200161 daint = readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100162 if (en)
163 daint |= bit;
164 else
165 daint &= ~bit;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200166 writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100167 local_irq_restore(flags);
168}
169
170/**
171 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
172 * @hsotg: The device instance.
173 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600174static void s3c_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100175{
Ben Dooks0f002d22010-05-25 05:36:50 +0100176 unsigned int ep;
177 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100178 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100179 u32 val;
180
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100181 /* Reset fifo map if not correctly cleared during previous session */
182 WARN_ON(hsotg->fifo_map);
183 hsotg->fifo_map = 0;
184
Gregory Herrero0a176272015-01-09 13:38:52 +0100185 /* set RX/NPTX FIFO sizes */
186 writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
187 writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
188 (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
189 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100190
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200191 /*
192 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100193 * block have overlapping default addresses. This also ensures
194 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200195 * known values.
196 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100197
198 /* start at the end of the GNPTXFSIZ, rounded up */
Gregory Herrero0a176272015-01-09 13:38:52 +0100199 addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
Ben Dooks0f002d22010-05-25 05:36:50 +0100200
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200201 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100202 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200203 * them to endpoints dynamically according to maxpacket size value of
204 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200205 */
Gregory Herrero0a176272015-01-09 13:38:52 +0100206 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
207 if (!hsotg->g_tx_fifo_sz[ep])
208 continue;
Robert Baldygab203d0a2014-09-09 10:44:56 +0200209 val = addr;
Gregory Herrero0a176272015-01-09 13:38:52 +0100210 val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
211 WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
Robert Baldygab203d0a2014-09-09 10:44:56 +0200212 "insufficient fifo memory");
Gregory Herrero0a176272015-01-09 13:38:52 +0100213 addr += hsotg->g_tx_fifo_sz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100214
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);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100238 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100239 }
240
241 udelay(1);
242 }
243
244 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100245}
246
247/**
248 * @ep: USB endpoint to allocate request for.
249 * @flags: Allocation flags
250 *
251 * Allocate a new USB request structure appropriate for the specified endpoint
252 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000253static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
254 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100255{
256 struct s3c_hsotg_req *req;
257
258 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
259 if (!req)
260 return NULL;
261
262 INIT_LIST_HEAD(&req->queue);
263
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100264 return &req->req;
265}
266
267/**
268 * is_ep_periodic - return true if the endpoint is in periodic mode.
269 * @hs_ep: The endpoint to query.
270 *
271 * Returns true if the endpoint is in periodic mode, meaning it is being
272 * used for an Interrupt or ISO transfer.
273 */
274static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
275{
276 return hs_ep->periodic;
277}
278
279/**
280 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
281 * @hsotg: The device state.
282 * @hs_ep: The endpoint for the request
283 * @hs_req: The request being processed.
284 *
285 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
286 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200287 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600288static void s3c_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100289 struct s3c_hsotg_ep *hs_ep,
290 struct s3c_hsotg_req *hs_req)
291{
292 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100293
294 /* ignore this if we're not moving any data */
295 if (hs_req->req.length == 0)
296 return;
297
Jingoo Han17d966a2013-05-11 21:14:00 +0900298 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100299}
300
301/**
302 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
303 * @hsotg: The controller state.
304 * @hs_ep: The endpoint we're going to write for.
305 * @hs_req: The request to write data for.
306 *
307 * This is called when the TxFIFO has some space in it to hold a new
308 * transmission and we have something to give it. The actual setup of
309 * the data size is done elsewhere, so all we have to do is to actually
310 * write the data.
311 *
312 * The return value is zero if there is more space (or nothing was done)
313 * otherwise -ENOSPC is returned if the FIFO space was used up.
314 *
315 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200316 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600317static int s3c_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100318 struct s3c_hsotg_ep *hs_ep,
319 struct s3c_hsotg_req *hs_req)
320{
321 bool periodic = is_ep_periodic(hs_ep);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200322 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100323 int buf_pos = hs_req->req.actual;
324 int to_write = hs_ep->size_loaded;
325 void *data;
326 int can_write;
327 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200328 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100329
330 to_write -= (buf_pos - hs_ep->last_load);
331
332 /* if there's nothing to write, get out early */
333 if (to_write == 0)
334 return 0;
335
Ben Dooks10aebc72010-07-19 09:40:44 +0100336 if (periodic && !hsotg->dedicated_fifos) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200337 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100338 int size_left;
339 int size_done;
340
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200341 /*
342 * work out how much data was loaded so we can calculate
343 * how much data is left in the fifo.
344 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100345
Dinh Nguyen47a16852014-04-14 14:13:34 -0700346 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100347
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200348 /*
349 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100350 * previous data has been completely sent.
351 */
352 if (hs_ep->fifo_load != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700353 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100354 return -ENOSPC;
355 }
356
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100357 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
358 __func__, size_left,
359 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
360
361 /* how much of the data has moved */
362 size_done = hs_ep->size_loaded - size_left;
363
364 /* how much data is left in the fifo */
365 can_write = hs_ep->fifo_load - size_done;
366 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
367 __func__, can_write);
368
369 can_write = hs_ep->fifo_size - can_write;
370 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
371 __func__, can_write);
372
373 if (can_write <= 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700374 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100375 return -ENOSPC;
376 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100377 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200378 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100379
380 can_write &= 0xffff;
381 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100382 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700383 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100384 dev_dbg(hsotg->dev,
385 "%s: no queue slots available (0x%08x)\n",
386 __func__, gnptxsts);
387
Dinh Nguyen47a16852014-04-14 14:13:34 -0700388 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100389 return -ENOSPC;
390 }
391
Dinh Nguyen47a16852014-04-14 14:13:34 -0700392 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100393 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100394 }
395
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200396 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
397
398 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
399 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100400
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200401 /*
402 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100403 * FIFO, requests of >512 cause the endpoint to get stuck with a
404 * fragment of the end of the transfer in it.
405 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200406 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100407 can_write = 512;
408
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200409 /*
410 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100411 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200412 * doing it.
413 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200414 if (to_write > max_transfer) {
415 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100416
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200417 /* it's needed only when we do not use dedicated fifos */
418 if (!hsotg->dedicated_fifos)
419 s3c_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700420 periodic ? GINTSTS_PTXFEMP :
421 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100422 }
423
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100424 /* see if we can write data */
425
426 if (to_write > can_write) {
427 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200428 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100429
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200430 /*
431 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100432 * exact number of packets.
433 *
434 * Note, we do not currently check to see if we can ever
435 * write a full packet or not to the FIFO.
436 */
437
438 if (pkt_round)
439 to_write -= pkt_round;
440
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200441 /*
442 * enable correct FIFO interrupt to alert us when there
443 * is more room left.
444 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100445
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200446 /* it's needed only when we do not use dedicated fifos */
447 if (!hsotg->dedicated_fifos)
448 s3c_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700449 periodic ? GINTSTS_PTXFEMP :
450 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100451 }
452
453 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
454 to_write, hs_req->req.length, can_write, buf_pos);
455
456 if (to_write <= 0)
457 return -ENOSPC;
458
459 hs_req->req.actual = buf_pos + to_write;
460 hs_ep->total_data += to_write;
461
462 if (periodic)
463 hs_ep->fifo_load += to_write;
464
465 to_write = DIV_ROUND_UP(to_write, 4);
466 data = hs_req->req.buf + buf_pos;
467
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500468 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100469
470 return (to_write >= can_write) ? -ENOSPC : 0;
471}
472
473/**
474 * get_ep_limit - get the maximum data legnth for this endpoint
475 * @hs_ep: The endpoint
476 *
477 * Return the maximum data that can be queued in one go on a given endpoint
478 * so that transfers that are too long can be split.
479 */
480static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
481{
482 int index = hs_ep->index;
483 unsigned maxsize;
484 unsigned maxpkt;
485
486 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700487 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
488 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100489 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100490 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900491 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700492 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900493 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100494 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100495 }
496
497 /* we made the constant loading easier above by using +1 */
498 maxpkt--;
499 maxsize--;
500
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200501 /*
502 * constrain by packet count if maxpkts*pktsize is greater
503 * than the length register size.
504 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100505
506 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
507 maxsize = maxpkt * hs_ep->ep.maxpacket;
508
509 return maxsize;
510}
511
512/**
513 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
514 * @hsotg: The controller state.
515 * @hs_ep: The endpoint to process a request for
516 * @hs_req: The request to start.
517 * @continuing: True if we are doing more for the current request.
518 *
519 * Start the given request running by setting the endpoint registers
520 * appropriately, and writing any data to the FIFOs.
521 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600522static void s3c_hsotg_start_req(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100523 struct s3c_hsotg_ep *hs_ep,
524 struct s3c_hsotg_req *hs_req,
525 bool continuing)
526{
527 struct usb_request *ureq = &hs_req->req;
528 int index = hs_ep->index;
529 int dir_in = hs_ep->dir_in;
530 u32 epctrl_reg;
531 u32 epsize_reg;
532 u32 epsize;
533 u32 ctrl;
534 unsigned length;
535 unsigned packets;
536 unsigned maxreq;
537
538 if (index != 0) {
539 if (hs_ep->req && !continuing) {
540 dev_err(hsotg->dev, "%s: active request\n", __func__);
541 WARN_ON(1);
542 return;
543 } else if (hs_ep->req != hs_req && continuing) {
544 dev_err(hsotg->dev,
545 "%s: continue different req\n", __func__);
546 WARN_ON(1);
547 return;
548 }
549 }
550
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200551 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
552 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100553
554 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
555 __func__, readl(hsotg->regs + epctrl_reg), index,
556 hs_ep->dir_in ? "in" : "out");
557
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900558 /* If endpoint is stalled, we will restart request later */
559 ctrl = readl(hsotg->regs + epctrl_reg);
560
Dinh Nguyen47a16852014-04-14 14:13:34 -0700561 if (ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900562 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
563 return;
564 }
565
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100566 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200567 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
568 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100569
570 maxreq = get_ep_limit(hs_ep);
571 if (length > maxreq) {
572 int round = maxreq % hs_ep->ep.maxpacket;
573
574 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
575 __func__, length, maxreq, round);
576
577 /* round down to multiple of packets */
578 if (round)
579 maxreq -= round;
580
581 length = maxreq;
582 }
583
584 if (length)
585 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
586 else
587 packets = 1; /* send one packet if length is zero. */
588
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200589 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
590 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
591 return;
592 }
593
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100594 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200595 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700596 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200597 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700598 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100599 else
600 epsize = 0;
601
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100602 /*
603 * zero length packet should be programmed on its own and should not
604 * be counted in DIEPTSIZ.PktCnt with other packets.
605 */
606 if (dir_in && ureq->zero && !continuing) {
607 /* Test if zlp is actually required. */
608 if ((ureq->length >= hs_ep->ep.maxpacket) &&
609 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +0100610 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100611 }
612
Dinh Nguyen47a16852014-04-14 14:13:34 -0700613 epsize |= DXEPTSIZ_PKTCNT(packets);
614 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100615
616 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
617 __func__, packets, length, ureq->length, epsize, epsize_reg);
618
619 /* store the request as the current one we're doing */
620 hs_ep->req = hs_req;
621
622 /* write size / packets */
623 writel(epsize, hsotg->regs + epsize_reg);
624
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900625 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100626 unsigned int dma_reg;
627
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200628 /*
629 * write DMA address to control register, buffer already
630 * synced by s3c_hsotg_ep_queue().
631 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100632
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200633 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100634 writel(ureq->dma, hsotg->regs + dma_reg);
635
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300636 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900637 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100638 }
639
Dinh Nguyen47a16852014-04-14 14:13:34 -0700640 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
641 ctrl |= DXEPCTL_USBACTEP;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200642
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100643 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +0200644
645 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100646 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -0700647 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200648
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100649 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
650 writel(ctrl, hsotg->regs + epctrl_reg);
651
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200652 /*
653 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100654 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200655 * this information.
656 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100657 hs_ep->size_loaded = length;
658 hs_ep->last_load = ureq->actual;
659
660 if (dir_in && !using_dma(hsotg)) {
661 /* set these anyway, we may need them for non-periodic in */
662 hs_ep->fifo_load = 0;
663
664 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
665 }
666
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200667 /*
668 * clear the INTknTXFEmpMsk when we start request, more as a aide
669 * to debugging to see what is going on.
670 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100671 if (dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700672 writel(DIEPMSK_INTKNTXFEMPMSK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200673 hsotg->regs + DIEPINT(index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100674
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200675 /*
676 * Note, trying to clear the NAK here causes problems with transmit
677 * on the S3C6400 ending up with the TXFIFO becoming full.
678 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100679
680 /* check ep is enabled */
Dinh Nguyen47a16852014-04-14 14:13:34 -0700681 if (!(readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +0100682 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700683 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100684 index, readl(hsotg->regs + epctrl_reg));
685
Dinh Nguyen47a16852014-04-14 14:13:34 -0700686 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100687 __func__, readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200688
689 /* enable ep interrupts */
690 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100691}
692
693/**
694 * s3c_hsotg_map_dma - map the DMA memory being used for the request
695 * @hsotg: The device state.
696 * @hs_ep: The endpoint the request is on.
697 * @req: The request being processed.
698 *
699 * We've been asked to queue a request, so ensure that the memory buffer
700 * is correctly setup for DMA. If we've been passed an extant DMA address
701 * then ensure the buffer has been synced to memory. If our buffer has no
702 * DMA memory, then we map the memory and mark our request to allow us to
703 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200704 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600705static int s3c_hsotg_map_dma(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100706 struct s3c_hsotg_ep *hs_ep,
707 struct usb_request *req)
708{
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100709 struct s3c_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200710 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100711
712 /* if the length is zero, ignore the DMA data */
713 if (hs_req->req.length == 0)
714 return 0;
715
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200716 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
717 if (ret)
718 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100719
720 return 0;
721
722dma_error:
723 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
724 __func__, req->buf, req->length);
725
726 return -EIO;
727}
728
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100729static int s3c_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
730 struct s3c_hsotg_ep *hs_ep, struct s3c_hsotg_req *hs_req)
731{
732 void *req_buf = hs_req->req.buf;
733
734 /* If dma is not being used or buffer is aligned */
735 if (!using_dma(hsotg) || !((long)req_buf & 3))
736 return 0;
737
738 WARN_ON(hs_req->saved_req_buf);
739
740 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
741 hs_ep->ep.name, req_buf, hs_req->req.length);
742
743 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
744 if (!hs_req->req.buf) {
745 hs_req->req.buf = req_buf;
746 dev_err(hsotg->dev,
747 "%s: unable to allocate memory for bounce buffer\n",
748 __func__);
749 return -ENOMEM;
750 }
751
752 /* Save actual buffer */
753 hs_req->saved_req_buf = req_buf;
754
755 if (hs_ep->dir_in)
756 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
757 return 0;
758}
759
760static void s3c_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
761 struct s3c_hsotg_ep *hs_ep, struct s3c_hsotg_req *hs_req)
762{
763 /* If dma is not being used or buffer was aligned */
764 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
765 return;
766
767 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
768 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
769
770 /* Copy data from bounce buffer on successful out transfer */
771 if (!hs_ep->dir_in && !hs_req->req.status)
772 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
773 hs_req->req.actual);
774
775 /* Free bounce buffer */
776 kfree(hs_req->req.buf);
777
778 hs_req->req.buf = hs_req->saved_req_buf;
779 hs_req->saved_req_buf = NULL;
780}
781
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100782static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
783 gfp_t gfp_flags)
784{
785 struct s3c_hsotg_req *hs_req = our_req(req);
786 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600787 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100788 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100789 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100790
791 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
792 ep->name, req, req->length, req->buf, req->no_interrupt,
793 req->zero, req->short_not_ok);
794
795 /* initialise status of the request */
796 INIT_LIST_HEAD(&hs_req->queue);
797 req->actual = 0;
798 req->status = -EINPROGRESS;
799
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100800 ret = s3c_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
801 if (ret)
802 return ret;
803
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100804 /* if we're using DMA, sync the buffers as necessary */
805 if (using_dma(hs)) {
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100806 ret = s3c_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100807 if (ret)
808 return ret;
809 }
810
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100811 first = list_empty(&hs_ep->queue);
812 list_add_tail(&hs_req->queue, &hs_ep->queue);
813
814 if (first)
815 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
816
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100817 return 0;
818}
819
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200820static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
821 gfp_t gfp_flags)
822{
823 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600824 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200825 unsigned long flags = 0;
826 int ret = 0;
827
828 spin_lock_irqsave(&hs->lock, flags);
829 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
830 spin_unlock_irqrestore(&hs->lock, flags);
831
832 return ret;
833}
834
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100835static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
836 struct usb_request *req)
837{
838 struct s3c_hsotg_req *hs_req = our_req(req);
839
840 kfree(hs_req);
841}
842
843/**
844 * s3c_hsotg_complete_oursetup - setup completion callback
845 * @ep: The endpoint the request was on.
846 * @req: The request completed.
847 *
848 * Called on completion of any requests the driver itself
849 * submitted that need cleaning up.
850 */
851static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
852 struct usb_request *req)
853{
854 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600855 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100856
857 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
858
859 s3c_hsotg_ep_free_request(ep, req);
860}
861
862/**
863 * ep_from_windex - convert control wIndex value to endpoint
864 * @hsotg: The driver state.
865 * @windex: The control request wIndex field (in host order).
866 *
867 * Convert the given wIndex into a pointer to an driver endpoint
868 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200869 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600870static struct s3c_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100871 u32 windex)
872{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100873 struct s3c_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100874 int dir = (windex & USB_DIR_IN) ? 1 : 0;
875 int idx = windex & 0x7F;
876
877 if (windex >= 0x100)
878 return NULL;
879
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200880 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100881 return NULL;
882
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100883 ep = index_to_ep(hsotg, idx, dir);
884
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100885 if (idx && ep->dir_in != dir)
886 return NULL;
887
888 return ep;
889}
890
891/**
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100892 * s3c_hsotg_set_test_mode - Enable usb Test Modes
893 * @hsotg: The driver state.
894 * @testmode: requested usb test mode
895 * Enable usb Test Mode requested by the Host.
896 */
897static int s3c_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
898{
899 int dctl = readl(hsotg->regs + DCTL);
900
901 dctl &= ~DCTL_TSTCTL_MASK;
902 switch (testmode) {
903 case TEST_J:
904 case TEST_K:
905 case TEST_SE0_NAK:
906 case TEST_PACKET:
907 case TEST_FORCE_EN:
908 dctl |= testmode << DCTL_TSTCTL_SHIFT;
909 break;
910 default:
911 return -EINVAL;
912 }
913 writel(dctl, hsotg->regs + DCTL);
914 return 0;
915}
916
917/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100918 * s3c_hsotg_send_reply - send reply to control request
919 * @hsotg: The device state
920 * @ep: Endpoint 0
921 * @buff: Buffer for request
922 * @length: Length of reply.
923 *
924 * Create a request and queue it on the given endpoint. This is useful as
925 * an internal method of sending replies to certain control requests, etc.
926 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600927static int s3c_hsotg_send_reply(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100928 struct s3c_hsotg_ep *ep,
929 void *buff,
930 int length)
931{
932 struct usb_request *req;
933 int ret;
934
935 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
936
937 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
938 hsotg->ep0_reply = req;
939 if (!req) {
940 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
941 return -ENOMEM;
942 }
943
944 req->buf = hsotg->ep0_buff;
945 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100946 /*
947 * zero flag is for sending zlp in DATA IN stage. It has no impact on
948 * STATUS stage.
949 */
950 req->zero = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100951 req->complete = s3c_hsotg_complete_oursetup;
952
953 if (length)
954 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100955
956 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
957 if (ret) {
958 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
959 return ret;
960 }
961
962 return 0;
963}
964
965/**
966 * s3c_hsotg_process_req_status - process request GET_STATUS
967 * @hsotg: The device state
968 * @ctrl: USB control request
969 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600970static int s3c_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100971 struct usb_ctrlrequest *ctrl)
972{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100973 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100974 struct s3c_hsotg_ep *ep;
975 __le16 reply;
976 int ret;
977
978 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
979
980 if (!ep0->dir_in) {
981 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
982 return -EINVAL;
983 }
984
985 switch (ctrl->bRequestType & USB_RECIP_MASK) {
986 case USB_RECIP_DEVICE:
987 reply = cpu_to_le16(0); /* bit 0 => self powered,
988 * bit 1 => remote wakeup */
989 break;
990
991 case USB_RECIP_INTERFACE:
992 /* currently, the data result should be zero */
993 reply = cpu_to_le16(0);
994 break;
995
996 case USB_RECIP_ENDPOINT:
997 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
998 if (!ep)
999 return -ENOENT;
1000
1001 reply = cpu_to_le16(ep->halted ? 1 : 0);
1002 break;
1003
1004 default:
1005 return 0;
1006 }
1007
1008 if (le16_to_cpu(ctrl->wLength) != 2)
1009 return -EINVAL;
1010
1011 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
1012 if (ret) {
1013 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1014 return ret;
1015 }
1016
1017 return 1;
1018}
1019
1020static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
1021
1022/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001023 * get_ep_head - return the first request on the endpoint
1024 * @hs_ep: The controller endpoint to get
1025 *
1026 * Get the first request on the endpoint.
1027 */
1028static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
1029{
1030 if (list_empty(&hs_ep->queue))
1031 return NULL;
1032
1033 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
1034}
1035
1036/**
Gregory Herrero58f7c432015-01-30 09:09:29 +01001037 * s3c_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001038 * @hsotg: The device state
1039 * @ctrl: USB control request
1040 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001041static int s3c_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001042 struct usb_ctrlrequest *ctrl)
1043{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001044 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001045 struct s3c_hsotg_req *hs_req;
1046 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001047 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
1048 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001049 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001050 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001051 u32 recip;
1052 u32 wValue;
1053 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001054
1055 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1056 __func__, set ? "SET" : "CLEAR");
1057
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001058 wValue = le16_to_cpu(ctrl->wValue);
1059 wIndex = le16_to_cpu(ctrl->wIndex);
1060 recip = ctrl->bRequestType & USB_RECIP_MASK;
1061
1062 switch (recip) {
1063 case USB_RECIP_DEVICE:
1064 switch (wValue) {
1065 case USB_DEVICE_TEST_MODE:
1066 if ((wIndex & 0xff) != 0)
1067 return -EINVAL;
1068 if (!set)
1069 return -EINVAL;
1070
1071 hsotg->test_mode = wIndex >> 8;
1072 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1073 if (ret) {
1074 dev_err(hsotg->dev,
1075 "%s: failed to send reply\n", __func__);
1076 return ret;
1077 }
1078 break;
1079 default:
1080 return -ENOENT;
1081 }
1082 break;
1083
1084 case USB_RECIP_ENDPOINT:
1085 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001086 if (!ep) {
1087 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001088 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001089 return -ENOENT;
1090 }
1091
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001092 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001093 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001094 halted = ep->halted;
1095
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001096 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001097
1098 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1099 if (ret) {
1100 dev_err(hsotg->dev,
1101 "%s: failed to send reply\n", __func__);
1102 return ret;
1103 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001104
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001105 /*
1106 * we have to complete all requests for ep if it was
1107 * halted, and the halt was cleared by CLEAR_FEATURE
1108 */
1109
1110 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001111 /*
1112 * If we have request in progress,
1113 * then complete it
1114 */
1115 if (ep->req) {
1116 hs_req = ep->req;
1117 ep->req = NULL;
1118 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001119 if (hs_req->req.complete) {
1120 spin_unlock(&hsotg->lock);
1121 usb_gadget_giveback_request(
1122 &ep->ep, &hs_req->req);
1123 spin_lock(&hsotg->lock);
1124 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001125 }
1126
1127 /* If we have pending request, then start it */
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001128 if (!ep->req) {
1129 restart = !list_empty(&ep->queue);
1130 if (restart) {
1131 hs_req = get_ep_head(ep);
1132 s3c_hsotg_start_req(hsotg, ep,
1133 hs_req, false);
1134 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001135 }
1136 }
1137
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138 break;
1139
1140 default:
1141 return -ENOENT;
1142 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001143 break;
1144 default:
1145 return -ENOENT;
1146 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001147 return 1;
1148}
1149
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001150static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001151
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001152/**
Robert Baldygac9f721b2014-01-14 08:36:00 +01001153 * s3c_hsotg_stall_ep0 - stall ep0
1154 * @hsotg: The device state
1155 *
1156 * Set stall for ep0 as response for setup request.
1157 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001158static void s3c_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001159{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001160 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001161 u32 reg;
1162 u32 ctrl;
1163
1164 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1165 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1166
1167 /*
1168 * DxEPCTL_Stall will be cleared by EP once it has
1169 * taken effect, so no need to clear later.
1170 */
1171
1172 ctrl = readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001173 ctrl |= DXEPCTL_STALL;
1174 ctrl |= DXEPCTL_CNAK;
Robert Baldygac9f721b2014-01-14 08:36:00 +01001175 writel(ctrl, hsotg->regs + reg);
1176
1177 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001178 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Robert Baldygac9f721b2014-01-14 08:36:00 +01001179 ctrl, reg, readl(hsotg->regs + reg));
1180
1181 /*
1182 * complete won't be called, so we enqueue
1183 * setup request here
1184 */
1185 s3c_hsotg_enqueue_setup(hsotg);
1186}
1187
1188/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001189 * s3c_hsotg_process_control - process a control request
1190 * @hsotg: The device state
1191 * @ctrl: The control request received
1192 *
1193 * The controller has received the SETUP phase of a control request, and
1194 * needs to work out what to do next (and whether to pass it on to the
1195 * gadget driver).
1196 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001197static void s3c_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001198 struct usb_ctrlrequest *ctrl)
1199{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001200 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001201 int ret = 0;
1202 u32 dcfg;
1203
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001204 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1205 ctrl->bRequest, ctrl->bRequestType,
1206 ctrl->wValue, ctrl->wLength);
1207
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001208 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001209 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001210 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1211 } else if (ctrl->bRequestType & USB_DIR_IN) {
1212 ep0->dir_in = 1;
1213 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1214 } else {
1215 ep0->dir_in = 0;
1216 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1217 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001218
1219 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1220 switch (ctrl->bRequest) {
1221 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001222 hsotg->connected = 1;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001223 dcfg = readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001224 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001225 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1226 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001227 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001228
1229 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1230
1231 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1232 return;
1233
1234 case USB_REQ_GET_STATUS:
1235 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1236 break;
1237
1238 case USB_REQ_CLEAR_FEATURE:
1239 case USB_REQ_SET_FEATURE:
1240 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1241 break;
1242 }
1243 }
1244
1245 /* as a fallback, try delivering it to the driver to deal with */
1246
1247 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001248 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001249 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001250 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001251 if (ret < 0)
1252 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1253 }
1254
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001255 /*
1256 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001257 * so respond with a STALL for the status stage to indicate failure.
1258 */
1259
Robert Baldygac9f721b2014-01-14 08:36:00 +01001260 if (ret < 0)
1261 s3c_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001262}
1263
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001264/**
1265 * s3c_hsotg_complete_setup - completion of a setup transfer
1266 * @ep: The endpoint the request was on.
1267 * @req: The request completed.
1268 *
1269 * Called on completion of any requests the driver itself submitted for
1270 * EP0 setup packets
1271 */
1272static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1273 struct usb_request *req)
1274{
1275 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001276 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001277
1278 if (req->status < 0) {
1279 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1280 return;
1281 }
1282
Robert Baldyga93f599f2013-11-21 13:49:17 +01001283 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001284 if (req->actual == 0)
1285 s3c_hsotg_enqueue_setup(hsotg);
1286 else
1287 s3c_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001288 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001289}
1290
1291/**
1292 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1293 * @hsotg: The device state.
1294 *
1295 * Enqueue a request on EP0 if necessary to received any SETUP packets
1296 * received from the host.
1297 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001298static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001299{
1300 struct usb_request *req = hsotg->ctrl_req;
1301 struct s3c_hsotg_req *hs_req = our_req(req);
1302 int ret;
1303
1304 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1305
1306 req->zero = 0;
1307 req->length = 8;
1308 req->buf = hsotg->ctrl_buff;
1309 req->complete = s3c_hsotg_complete_setup;
1310
1311 if (!list_empty(&hs_req->queue)) {
1312 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1313 return;
1314 }
1315
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001316 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001317 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001318 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001319
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001320 ret = s3c_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001321 if (ret < 0) {
1322 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001323 /*
1324 * Don't think there's much we can do other than watch the
1325 * driver fail.
1326 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001327 }
1328}
1329
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001330static void s3c_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1331 struct s3c_hsotg_ep *hs_ep)
1332{
1333 u32 ctrl;
1334 u8 index = hs_ep->index;
1335 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1336 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1337
1338 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", index);
1339
1340 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1341 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1342 epsiz_reg);
1343
1344 ctrl = readl(hsotg->regs + epctl_reg);
1345 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1346 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1347 ctrl |= DXEPCTL_USBACTEP;
1348 writel(ctrl, hsotg->regs + epctl_reg);
1349}
1350
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001351/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001352 * s3c_hsotg_complete_request - complete a request given to us
1353 * @hsotg: The device state.
1354 * @hs_ep: The endpoint the request was on.
1355 * @hs_req: The request to complete.
1356 * @result: The result code (0 => Ok, otherwise errno)
1357 *
1358 * The given request has finished, so call the necessary completion
1359 * if it has one and then look to see if we can start a new request
1360 * on the endpoint.
1361 *
1362 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001363 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001364static void s3c_hsotg_complete_request(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001365 struct s3c_hsotg_ep *hs_ep,
1366 struct s3c_hsotg_req *hs_req,
1367 int result)
1368{
1369 bool restart;
1370
1371 if (!hs_req) {
1372 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1373 return;
1374 }
1375
1376 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1377 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1378
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001379 /*
1380 * only replace the status if we've not already set an error
1381 * from a previous transaction
1382 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001383
1384 if (hs_req->req.status == -EINPROGRESS)
1385 hs_req->req.status = result;
1386
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001387 s3c_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
1388
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001389 hs_ep->req = NULL;
1390 list_del_init(&hs_req->queue);
1391
1392 if (using_dma(hsotg))
1393 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1394
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001395 /*
1396 * call the complete request with the locks off, just in case the
1397 * request tries to queue more work for this endpoint.
1398 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001399
1400 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001401 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001402 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001403 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001404 }
1405
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001406 /*
1407 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001408 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001409 * so be careful when doing this.
1410 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001411
1412 if (!hs_ep->req && result >= 0) {
1413 restart = !list_empty(&hs_ep->queue);
1414 if (restart) {
1415 hs_req = get_ep_head(hs_ep);
1416 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1417 }
1418 }
1419}
1420
1421/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001422 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1423 * @hsotg: The device state.
1424 * @ep_idx: The endpoint index for the data
1425 * @size: The size of data in the fifo, in bytes
1426 *
1427 * The FIFO status shows there is data to read from the FIFO for a given
1428 * endpoint, so sort out whether we need to read the data into a request
1429 * that has been made for that endpoint.
1430 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001431static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001432{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001433 struct s3c_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001434 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001435 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001436 int to_read;
1437 int max_req;
1438 int read_ptr;
1439
Lukasz Majewski22258f42012-06-14 10:02:24 +02001440
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001441 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001442 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001443 int ptr;
1444
Robert Baldyga6b448af2014-12-16 11:51:44 +01001445 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001446 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001447 __func__, size, ep_idx, epctl);
1448
1449 /* dump the data from the FIFO, we've nothing we can do */
1450 for (ptr = 0; ptr < size; ptr += 4)
1451 (void)readl(fifo);
1452
1453 return;
1454 }
1455
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001456 to_read = size;
1457 read_ptr = hs_req->req.actual;
1458 max_req = hs_req->req.length - read_ptr;
1459
Ben Dooksa33e7132010-07-19 09:40:49 +01001460 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1461 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1462
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001463 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001464 /*
1465 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001466 * to deal with in this request.
1467 */
1468
1469 /* currently we don't deal this */
1470 WARN_ON_ONCE(1);
1471 }
1472
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001473 hs_ep->total_data += to_read;
1474 hs_req->req.actual += to_read;
1475 to_read = DIV_ROUND_UP(to_read, 4);
1476
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001477 /*
1478 * note, we might over-write the buffer end by 3 bytes depending on
1479 * alignment of the data.
1480 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001481 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001482}
1483
1484/**
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001485 * s3c_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001486 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001487 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001488 *
1489 * Generate a zero-length IN packet request for terminating a SETUP
1490 * transaction.
1491 *
1492 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001493 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001494 * the TxFIFO.
1495 */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001496static void s3c_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001497{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001498 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001499 hsotg->eps_out[0]->dir_in = dir_in;
1500 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001501
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001502 s3c_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001503}
1504
1505/**
1506 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1507 * @hsotg: The device instance
1508 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001509 *
1510 * The RXFIFO has delivered an OutDone event, which means that the data
1511 * transfer for an OUT endpoint has been completed, either by a short
1512 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001513 */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001514static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001515{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001516 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001517 struct s3c_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001518 struct s3c_hsotg_req *hs_req = hs_ep->req;
1519 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001520 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001521 int result = 0;
1522
1523 if (!hs_req) {
1524 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1525 return;
1526 }
1527
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001528 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1529 dev_dbg(hsotg->dev, "zlp packet received\n");
1530 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1531 s3c_hsotg_enqueue_setup(hsotg);
1532 return;
1533 }
1534
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001535 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001536 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001537
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001538 /*
1539 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001540 * is left in the endpoint size register and then working it
1541 * out from the amount we loaded for the transfer.
1542 *
1543 * We need to do this as DMA pointers are always 32bit aligned
1544 * so may overshoot/undershoot the transfer.
1545 */
1546
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001547 size_done = hs_ep->size_loaded - size_left;
1548 size_done += hs_ep->last_load;
1549
1550 req->actual = size_done;
1551 }
1552
Ben Dooksa33e7132010-07-19 09:40:49 +01001553 /* if there is more request to do, schedule new transfer */
1554 if (req->actual < req->length && size_left == 0) {
1555 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1556 return;
1557 }
1558
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001559 if (req->actual < req->length && req->short_not_ok) {
1560 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1561 __func__, req->actual, req->length);
1562
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001563 /*
1564 * todo - what should we return here? there's no one else
1565 * even bothering to check the status.
1566 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001567 }
1568
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001569 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1570 /* Move to STATUS IN */
1571 s3c_hsotg_ep0_zlp(hsotg, true);
1572 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001573 }
1574
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001575 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001576}
1577
1578/**
1579 * s3c_hsotg_read_frameno - read current frame number
1580 * @hsotg: The device instance
1581 *
1582 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001583 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001584static u32 s3c_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001585{
1586 u32 dsts;
1587
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001588 dsts = readl(hsotg->regs + DSTS);
1589 dsts &= DSTS_SOFFN_MASK;
1590 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001591
1592 return dsts;
1593}
1594
1595/**
1596 * s3c_hsotg_handle_rx - RX FIFO has data
1597 * @hsotg: The device instance
1598 *
1599 * The IRQ handler has detected that the RX FIFO has some data in it
1600 * that requires processing, so find out what is in there and do the
1601 * appropriate read.
1602 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001603 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001604 * chunks, so if you have x packets received on an endpoint you'll get x
1605 * FIFO events delivered, each with a packet's worth of data in it.
1606 *
1607 * When using DMA, we should not be processing events from the RXFIFO
1608 * as the actual data should be sent to the memory directly and we turn
1609 * on the completion interrupts to get notifications of transfer completion.
1610 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001611static void s3c_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001612{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001613 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001614 u32 epnum, status, size;
1615
1616 WARN_ON(using_dma(hsotg));
1617
Dinh Nguyen47a16852014-04-14 14:13:34 -07001618 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1619 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001620
Dinh Nguyen47a16852014-04-14 14:13:34 -07001621 size = grxstsr & GRXSTS_BYTECNT_MASK;
1622 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001623
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01001624 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001625 __func__, grxstsr, size, epnum);
1626
Dinh Nguyen47a16852014-04-14 14:13:34 -07001627 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1628 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1629 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001630 break;
1631
Dinh Nguyen47a16852014-04-14 14:13:34 -07001632 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001633 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1634 s3c_hsotg_read_frameno(hsotg));
1635
1636 if (!using_dma(hsotg))
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001637 s3c_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001638 break;
1639
Dinh Nguyen47a16852014-04-14 14:13:34 -07001640 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001641 dev_dbg(hsotg->dev,
1642 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1643 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001644 readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001645 /*
1646 * Call s3c_hsotg_handle_outdone here if it was not called from
1647 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1648 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1649 */
1650 if (hsotg->ep0_state == DWC2_EP0_SETUP)
1651 s3c_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001652 break;
1653
Dinh Nguyen47a16852014-04-14 14:13:34 -07001654 case GRXSTS_PKTSTS_OUTRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001655 s3c_hsotg_rx_data(hsotg, epnum, size);
1656 break;
1657
Dinh Nguyen47a16852014-04-14 14:13:34 -07001658 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001659 dev_dbg(hsotg->dev,
1660 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1661 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001662 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001663
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001664 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1665
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001666 s3c_hsotg_rx_data(hsotg, epnum, size);
1667 break;
1668
1669 default:
1670 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1671 __func__, grxstsr);
1672
1673 s3c_hsotg_dump(hsotg);
1674 break;
1675 }
1676}
1677
1678/**
1679 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1680 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001681 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001682static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1683{
1684 switch (mps) {
1685 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001686 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001687 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001688 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001689 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001690 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001691 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001692 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001693 }
1694
1695 /* bad max packet size, warn and return invalid result */
1696 WARN_ON(1);
1697 return (u32)-1;
1698}
1699
1700/**
1701 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1702 * @hsotg: The driver state.
1703 * @ep: The index number of the endpoint
1704 * @mps: The maximum packet size in bytes
1705 *
1706 * Configure the maximum packet size for the given endpoint, updating
1707 * the hardware control registers to reflect this.
1708 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001709static void s3c_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001710 unsigned int ep, unsigned int mps, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001711{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001712 struct s3c_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001713 void __iomem *regs = hsotg->regs;
1714 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001715 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001716 u32 reg;
1717
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001718 hs_ep = index_to_ep(hsotg, ep, dir_in);
1719 if (!hs_ep)
1720 return;
1721
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001722 if (ep == 0) {
1723 /* EP0 is a special case */
1724 mpsval = s3c_hsotg_ep0_mps(mps);
1725 if (mpsval > 3)
1726 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001727 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001728 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001729 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001730 mpsval = mps & DXEPCTL_MPS_MASK;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001731 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001732 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001733 mcval = ((mps >> 11) & 0x3) + 1;
1734 hs_ep->mc = mcval;
1735 if (mcval > 3)
1736 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001737 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001738 }
1739
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001740 if (dir_in) {
1741 reg = readl(regs + DIEPCTL(ep));
1742 reg &= ~DXEPCTL_MPS_MASK;
1743 reg |= mpsval;
1744 writel(reg, regs + DIEPCTL(ep));
1745 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001746 reg = readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001747 reg &= ~DXEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001748 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001749 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001750 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001751
1752 return;
1753
1754bad_mps:
1755 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1756}
1757
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001758/**
1759 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1760 * @hsotg: The driver state
1761 * @idx: The index for the endpoint (0..15)
1762 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001763static void s3c_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001764{
1765 int timeout;
1766 int val;
1767
Dinh Nguyen47a16852014-04-14 14:13:34 -07001768 writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001769 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001770
1771 /* wait until the fifo is flushed */
1772 timeout = 100;
1773
1774 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001775 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001776
Dinh Nguyen47a16852014-04-14 14:13:34 -07001777 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001778 break;
1779
1780 if (--timeout == 0) {
1781 dev_err(hsotg->dev,
1782 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1783 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02001784 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001785 }
1786
1787 udelay(1);
1788 }
1789}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001790
1791/**
1792 * s3c_hsotg_trytx - check to see if anything needs transmitting
1793 * @hsotg: The driver state
1794 * @hs_ep: The driver endpoint to check.
1795 *
1796 * Check to see if there is a request that has data to send, and if so
1797 * make an attempt to write data into the FIFO.
1798 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001799static int s3c_hsotg_trytx(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001800 struct s3c_hsotg_ep *hs_ep)
1801{
1802 struct s3c_hsotg_req *hs_req = hs_ep->req;
1803
Robert Baldygaafcf4162013-09-19 11:50:19 +02001804 if (!hs_ep->dir_in || !hs_req) {
1805 /**
1806 * if request is not enqueued, we disable interrupts
1807 * for endpoints, excepting ep0
1808 */
1809 if (hs_ep->index != 0)
1810 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1811 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001812 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001813 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001814
1815 if (hs_req->req.actual < hs_req->req.length) {
1816 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1817 hs_ep->index);
1818 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1819 }
1820
1821 return 0;
1822}
1823
1824/**
1825 * s3c_hsotg_complete_in - complete IN transfer
1826 * @hsotg: The device state.
1827 * @hs_ep: The endpoint that has just completed.
1828 *
1829 * An IN transfer has been completed, update the transfer's state and then
1830 * call the relevant completion routines.
1831 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001832static void s3c_hsotg_complete_in(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001833 struct s3c_hsotg_ep *hs_ep)
1834{
1835 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001836 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001837 int size_left, size_done;
1838
1839 if (!hs_req) {
1840 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1841 return;
1842 }
1843
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001844 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001845 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
1846 dev_dbg(hsotg->dev, "zlp packet sent\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001847 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001848 if (hsotg->test_mode) {
1849 int ret;
1850
1851 ret = s3c_hsotg_set_test_mode(hsotg, hsotg->test_mode);
1852 if (ret < 0) {
1853 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
1854 hsotg->test_mode);
1855 s3c_hsotg_stall_ep0(hsotg);
1856 return;
1857 }
1858 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001859 s3c_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001860 return;
1861 }
1862
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001863 /*
1864 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001865 * in the endpoint size register and then working it out from
1866 * the amount we loaded for the transfer.
1867 *
1868 * We do this even for DMA, as the transfer may have incremented
1869 * past the end of the buffer (DMA transfers are always 32bit
1870 * aligned).
1871 */
1872
Dinh Nguyen47a16852014-04-14 14:13:34 -07001873 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001874
1875 size_done = hs_ep->size_loaded - size_left;
1876 size_done += hs_ep->last_load;
1877
1878 if (hs_req->req.actual != size_done)
1879 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1880 __func__, hs_req->req.actual, size_done);
1881
1882 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001883 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1884 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001885
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001886 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1887 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1888 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001889 return;
1890 }
1891
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001892 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001893 if (hs_ep->send_zlp) {
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001894 s3c_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001895 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001896 /* transfer will be completed on next complete interrupt */
1897 return;
1898 }
1899
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001900 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
1901 /* Move to STATUS OUT */
1902 s3c_hsotg_ep0_zlp(hsotg, false);
1903 return;
1904 }
1905
1906 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001907}
1908
1909/**
1910 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1911 * @hsotg: The driver state
1912 * @idx: The index for the endpoint (0..15)
1913 * @dir_in: Set if this is an IN endpoint
1914 *
1915 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001916 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001917static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001918 int dir_in)
1919{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001920 struct s3c_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001921 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1922 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1923 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001924 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02001925 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001926
1927 ints = readl(hsotg->regs + epint_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02001928 ctrl = readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001929
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001930 /* Clear endpoint interrupts */
1931 writel(ints, hsotg->regs + epint_reg);
1932
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001933 if (!hs_ep) {
1934 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
1935 __func__, idx, dir_in ? "in" : "out");
1936 return;
1937 }
1938
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001939 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1940 __func__, idx, dir_in ? "in" : "out", ints);
1941
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01001942 /* Don't process XferCompl interrupt if it is a setup packet */
1943 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
1944 ints &= ~DXEPINT_XFERCOMPL;
1945
Dinh Nguyen47a16852014-04-14 14:13:34 -07001946 if (ints & DXEPINT_XFERCOMPL) {
Robert Baldyga1479e842013-10-09 08:41:57 +02001947 if (hs_ep->isochronous && hs_ep->interval == 1) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001948 if (ctrl & DXEPCTL_EOFRNUM)
1949 ctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001950 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001951 ctrl |= DXEPCTL_SETODDFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001952 writel(ctrl, hsotg->regs + epctl_reg);
1953 }
1954
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001955 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001956 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001957 __func__, readl(hsotg->regs + epctl_reg),
1958 readl(hsotg->regs + epsiz_reg));
1959
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001960 /*
1961 * we get OutDone from the FIFO, so we only need to look
1962 * at completing IN requests here
1963 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001964 if (dir_in) {
1965 s3c_hsotg_complete_in(hsotg, hs_ep);
1966
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001967 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001968 s3c_hsotg_enqueue_setup(hsotg);
1969 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001970 /*
1971 * We're using DMA, we need to fire an OutDone here
1972 * as we ignore the RXFIFO.
1973 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001974
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001975 s3c_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001976 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001977 }
1978
Dinh Nguyen47a16852014-04-14 14:13:34 -07001979 if (ints & DXEPINT_EPDISBLD) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001980 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001981
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001982 if (dir_in) {
1983 int epctl = readl(hsotg->regs + epctl_reg);
1984
Robert Baldygab203d0a2014-09-09 10:44:56 +02001985 s3c_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001986
Dinh Nguyen47a16852014-04-14 14:13:34 -07001987 if ((epctl & DXEPCTL_STALL) &&
1988 (epctl & DXEPCTL_EPTYPE_BULK)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001989 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001990
Dinh Nguyen47a16852014-04-14 14:13:34 -07001991 dctl |= DCTL_CGNPINNAK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001992 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001993 }
1994 }
1995 }
1996
Dinh Nguyen47a16852014-04-14 14:13:34 -07001997 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001998 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001999
Dinh Nguyen47a16852014-04-14 14:13:34 -07002000 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002001 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2002
2003 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002004 /*
2005 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002006 * setup packet. In non-DMA mode we'd get this
2007 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002008 * the setup here.
2009 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002010
2011 if (dir_in)
2012 WARN_ON_ONCE(1);
2013 else
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002014 s3c_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002015 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002016 }
2017
Dinh Nguyen47a16852014-04-14 14:13:34 -07002018 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002019 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002020
Robert Baldyga1479e842013-10-09 08:41:57 +02002021 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002022 /* not sure if this is important, but we'll clear it anyway */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002023 if (ints & DIEPMSK_INTKNTXFEMPMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002024 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2025 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002026 }
2027
2028 /* this probably means something bad is happening */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002029 if (ints & DIEPMSK_INTKNEPMISMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002030 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2031 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002032 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002033
2034 /* FIFO has space or is empty (see GAHBCFG) */
2035 if (hsotg->dedicated_fifos &&
Dinh Nguyen47a16852014-04-14 14:13:34 -07002036 ints & DIEPMSK_TXFIFOEMPTY) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002037 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2038 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002039 if (!using_dma(hsotg))
2040 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002041 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002042 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002043}
2044
2045/**
2046 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
2047 * @hsotg: The device state.
2048 *
2049 * Handle updating the device settings after the enumeration phase has
2050 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002051 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002052static void s3c_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002053{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002054 u32 dsts = readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09002055 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002056
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002057 /*
2058 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002059 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002060 * we connected at.
2061 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002062
2063 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2064
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002065 /*
2066 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002067 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002068 * not advertise a 64byte MPS on EP0.
2069 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002070
2071 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002072 switch (dsts & DSTS_ENUMSPD_MASK) {
2073 case DSTS_ENUMSPD_FS:
2074 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002075 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002076 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002077 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002078 break;
2079
Dinh Nguyen47a16852014-04-14 14:13:34 -07002080 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002081 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002082 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002083 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002084 break;
2085
Dinh Nguyen47a16852014-04-14 14:13:34 -07002086 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002087 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002088 /*
2089 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002090 * moment, and the documentation seems to imply that it isn't
2091 * supported by the PHYs on some of the devices.
2092 */
2093 break;
2094 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002095 dev_info(hsotg->dev, "new device is %s\n",
2096 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002097
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002098 /*
2099 * we should now know the maximum packet size for an
2100 * endpoint, so set the endpoints to a default value.
2101 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002102
2103 if (ep0_mps) {
2104 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002105 /* Initialize ep0 for both in and out directions */
2106 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
2107 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
2108 for (i = 1; i < hsotg->num_of_eps; i++) {
2109 if (hsotg->eps_in[i])
2110 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
2111 if (hsotg->eps_out[i])
2112 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
2113 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002114 }
2115
2116 /* ensure after enumeration our EP0 is active */
2117
2118 s3c_hsotg_enqueue_setup(hsotg);
2119
2120 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002121 readl(hsotg->regs + DIEPCTL0),
2122 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002123}
2124
2125/**
2126 * kill_all_requests - remove all requests from the endpoint's queue
2127 * @hsotg: The device state.
2128 * @ep: The endpoint the requests may be on.
2129 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002130 *
2131 * Go through the requests on the given endpoint and mark them
2132 * completed with the given result code.
2133 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002134static void kill_all_requests(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002135 struct s3c_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01002136 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002137{
2138 struct s3c_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002139 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002140
Robert Baldyga6b448af2014-12-16 11:51:44 +01002141 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002142
Robert Baldyga6b448af2014-12-16 11:51:44 +01002143 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002144 s3c_hsotg_complete_request(hsotg, ep, req,
2145 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01002146
Robert Baldygab203d0a2014-09-09 10:44:56 +02002147 if (!hsotg->dedicated_fifos)
2148 return;
2149 size = (readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
2150 if (size < ep->fifo_size)
2151 s3c_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002152}
2153
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002154/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002155 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002156 * @hsotg: The device state.
2157 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002158 * The device has been disconnected. Remove all current
2159 * transactions and signal the gadget driver that this
2160 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002161 */
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002162void s3c_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002163{
2164 unsigned ep;
2165
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002166 if (!hsotg->connected)
2167 return;
2168
2169 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002170 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002171
2172 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2173 if (hsotg->eps_in[ep])
2174 kill_all_requests(hsotg, hsotg->eps_in[ep],
2175 -ESHUTDOWN);
2176 if (hsotg->eps_out[ep])
2177 kill_all_requests(hsotg, hsotg->eps_out[ep],
2178 -ESHUTDOWN);
2179 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002180
2181 call_gadget(hsotg, disconnect);
2182}
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002183EXPORT_SYMBOL_GPL(s3c_hsotg_disconnect);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002184
2185/**
2186 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2187 * @hsotg: The device state:
2188 * @periodic: True if this is a periodic FIFO interrupt
2189 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002190static void s3c_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002191{
2192 struct s3c_hsotg_ep *ep;
2193 int epno, ret;
2194
2195 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002196 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002197 ep = index_to_ep(hsotg, epno, 1);
2198
2199 if (!ep)
2200 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002201
2202 if (!ep->dir_in)
2203 continue;
2204
2205 if ((periodic && !ep->periodic) ||
2206 (!periodic && ep->periodic))
2207 continue;
2208
2209 ret = s3c_hsotg_trytx(hsotg, ep);
2210 if (ret < 0)
2211 break;
2212 }
2213}
2214
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002215/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002216#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2217 GINTSTS_PTXFEMP | \
2218 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002219
2220/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002221 * s3c_hsotg_corereset - issue softreset to the core
2222 * @hsotg: The device state
2223 *
2224 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002225 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002226static int s3c_hsotg_corereset(struct dwc2_hsotg *hsotg)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002227{
2228 int timeout;
2229 u32 grstctl;
2230
2231 dev_dbg(hsotg->dev, "resetting core\n");
2232
2233 /* issue soft reset */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002234 writel(GRSTCTL_CSFTRST, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002235
Du, Changbin2868fea2012-07-24 08:19:25 +08002236 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002237 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002238 grstctl = readl(hsotg->regs + GRSTCTL);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002239 } while ((grstctl & GRSTCTL_CSFTRST) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002240
Dinh Nguyen47a16852014-04-14 14:13:34 -07002241 if (grstctl & GRSTCTL_CSFTRST) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002242 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2243 return -EINVAL;
2244 }
2245
Du, Changbin2868fea2012-07-24 08:19:25 +08002246 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002247
2248 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002249 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002250
2251 if (timeout-- < 0) {
2252 dev_info(hsotg->dev,
2253 "%s: reset failed, GRSTCTL=%08x\n",
2254 __func__, grstctl);
2255 return -ETIMEDOUT;
2256 }
2257
Dinh Nguyen47a16852014-04-14 14:13:34 -07002258 if (!(grstctl & GRSTCTL_AHBIDLE))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002259 continue;
2260
2261 break; /* reset done */
2262 }
2263
2264 dev_dbg(hsotg->dev, "reset successful\n");
2265 return 0;
2266}
2267
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002268/**
2269 * s3c_hsotg_core_init - issue softreset to the core
2270 * @hsotg: The device state
2271 *
2272 * Issue a soft reset to the core, and await the core finishing it.
2273 */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002274void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
2275 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002276{
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002277 u32 val;
2278
2279 if (!is_usb_reset)
2280 s3c_hsotg_corereset(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002281
2282 /*
2283 * we must now enable ep0 ready for host detection and then
2284 * set configuration.
2285 */
2286
2287 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002288 writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002289 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002290
2291 s3c_hsotg_init_fifo(hsotg);
2292
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002293 if (!is_usb_reset)
2294 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002295
Dinh Nguyen47a16852014-04-14 14:13:34 -07002296 writel(1 << 18 | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002297
2298 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002299 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002300
2301 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002302 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002303
Dinh Nguyen47a16852014-04-14 14:13:34 -07002304 writel(GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
2305 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
2306 GINTSTS_CONIDSTSCHNG | GINTSTS_USBRST |
2307 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
2308 GINTSTS_USBSUSP | GINTSTS_WKUPINT,
2309 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002310
2311 if (using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002312 writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Gregory Herrero5f050482015-01-09 13:38:46 +01002313 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002314 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002315 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07002316 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NP_TXF_EMP_LVL |
2317 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2318 GAHBCFG_GLBL_INTR_EN,
Robert Baldyga8acc8292013-09-19 11:50:23 +02002319 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002320
2321 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002322 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2323 * when we have no data to transfer. Otherwise we get being flooded by
2324 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002325 */
2326
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01002327 writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
2328 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002329 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
2330 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2331 DIEPMSK_INTKNEPMISMSK,
2332 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002333
2334 /*
2335 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2336 * DMA mode we may need this.
2337 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002338 writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
2339 DIEPMSK_TIMEOUTMSK) : 0) |
2340 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
2341 DOEPMSK_SETUPMSK,
2342 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002343
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002344 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002345
2346 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002347 readl(hsotg->regs + DIEPCTL0),
2348 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002349
2350 /* enable in and out endpoint interrupts */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002351 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002352
2353 /*
2354 * Enable the RXFIFO when in slave mode, as this is how we collect
2355 * the data. In DMA mode, we get events from the FIFO but also
2356 * things we cannot process, so do not use it.
2357 */
2358 if (!using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002359 s3c_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002360
2361 /* Enable interrupts for EP0 in and out */
2362 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2363 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2364
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002365 if (!is_usb_reset) {
2366 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2367 udelay(10); /* see openiboot */
2368 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2369 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02002370
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002371 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002372
2373 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002374 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002375 * writing to the EPCTL register..
2376 */
2377
2378 /* set to read 1 8byte packet */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002379 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2380 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002381
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002382 writel(s3c_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002383 DXEPCTL_CNAK | DXEPCTL_EPENA |
2384 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002385 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002386
2387 /* enable, but don't activate EP0in */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002388 writel(s3c_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002389 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002390
2391 s3c_hsotg_enqueue_setup(hsotg);
2392
2393 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002394 readl(hsotg->regs + DIEPCTL0),
2395 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002396
2397 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002398 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
2399 if (!is_usb_reset)
2400 val |= DCTL_SFTDISCON;
2401 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002402
2403 /* must be at-least 3ms to allow bus to see disconnect */
2404 mdelay(3);
2405
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002406 hsotg->last_rst = jiffies;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002407}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002408
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002409static void s3c_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002410{
2411 /* set the soft-disconnect bit */
2412 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2413}
2414
Dinh Nguyen510ffaa2014-11-11 11:13:36 -06002415void s3c_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002416{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002417 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002418 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002419}
2420
2421/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002422 * s3c_hsotg_irq - handle device interrupt
2423 * @irq: The IRQ number triggered
2424 * @pw: The pw value when registered the handler.
2425 */
2426static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2427{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002428 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002429 int retry_count = 8;
2430 u32 gintsts;
2431 u32 gintmsk;
2432
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002433 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002434irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002435 gintsts = readl(hsotg->regs + GINTSTS);
2436 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002437
2438 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2439 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2440
2441 gintsts &= gintmsk;
2442
Dinh Nguyen47a16852014-04-14 14:13:34 -07002443 if (gintsts & GINTSTS_ENUMDONE) {
2444 writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002445
2446 s3c_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002447 }
2448
Dinh Nguyen47a16852014-04-14 14:13:34 -07002449 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002450 u32 daint = readl(hsotg->regs + DAINT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002451 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2452 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002453 int ep;
2454
Robert Baldyga7e804652013-09-19 11:50:20 +02002455 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002456 daint_out = daint >> DAINT_OUTEP_SHIFT;
2457 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002458
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002459 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2460
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002461 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2462 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002463 if (daint_out & 1)
2464 s3c_hsotg_epint(hsotg, ep, 0);
2465 }
2466
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002467 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
2468 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002469 if (daint_in & 1)
2470 s3c_hsotg_epint(hsotg, ep, 1);
2471 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002472 }
2473
Dinh Nguyen47a16852014-04-14 14:13:34 -07002474 if (gintsts & GINTSTS_USBRST) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002475
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002476 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002477
Marek Szyprowski95998152014-10-20 12:45:32 +02002478 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002479 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002480 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002481
Dinh Nguyen47a16852014-04-14 14:13:34 -07002482 writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002483
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01002484 /* Report disconnection if it is not already done. */
2485 s3c_hsotg_disconnect(hsotg);
2486
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002487 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002488 if (time_after(jiffies, hsotg->last_rst +
2489 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002490
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002491 kill_all_requests(hsotg, hsotg->eps_out[0],
Robert Baldyga6b448af2014-12-16 11:51:44 +01002492 -ECONNRESET);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002493
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002494 s3c_hsotg_core_init_disconnected(hsotg, true);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002495 s3c_hsotg_core_connect(hsotg);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002496 }
2497 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498 }
2499
2500 /* check both FIFOs */
2501
Dinh Nguyen47a16852014-04-14 14:13:34 -07002502 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002503 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2504
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002505 /*
2506 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002507 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002508 * it needs re-enabling
2509 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002510
Dinh Nguyen47a16852014-04-14 14:13:34 -07002511 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002512 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002513 }
2514
Dinh Nguyen47a16852014-04-14 14:13:34 -07002515 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 dev_dbg(hsotg->dev, "PTxFEmp\n");
2517
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002518 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002519
Dinh Nguyen47a16852014-04-14 14:13:34 -07002520 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002521 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002522 }
2523
Dinh Nguyen47a16852014-04-14 14:13:34 -07002524 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002525 /*
2526 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002527 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002528 * set.
2529 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002530
2531 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002532 }
2533
Dinh Nguyen47a16852014-04-14 14:13:34 -07002534 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002535 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Dinh Nguyen47a16852014-04-14 14:13:34 -07002536 writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002537 }
2538
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002539 /*
2540 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002541 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002542 * the occurrence.
2543 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002544
Dinh Nguyen47a16852014-04-14 14:13:34 -07002545 if (gintsts & GINTSTS_GOUTNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002546 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2547
Dinh Nguyen47a16852014-04-14 14:13:34 -07002548 writel(DCTL_CGOUTNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002549
2550 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002551 }
2552
Dinh Nguyen47a16852014-04-14 14:13:34 -07002553 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002554 dev_info(hsotg->dev, "GINNakEff triggered\n");
2555
Dinh Nguyen47a16852014-04-14 14:13:34 -07002556 writel(DCTL_CGNPINNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002557
2558 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002559 }
2560
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002561 /*
2562 * if we've had fifo events, we should try and go around the
2563 * loop again to see if there's any point in returning yet.
2564 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002565
2566 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2567 goto irq_retry;
2568
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002569 spin_unlock(&hsotg->lock);
2570
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002571 return IRQ_HANDLED;
2572}
2573
2574/**
2575 * s3c_hsotg_ep_enable - enable the given endpoint
2576 * @ep: The USB endpint to configure
2577 * @desc: The USB endpoint descriptor to configure with.
2578 *
2579 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002580 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002581static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2582 const struct usb_endpoint_descriptor *desc)
2583{
2584 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002585 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002586 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002587 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002588 u32 epctrl_reg;
2589 u32 epctrl;
2590 u32 mps;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002591 unsigned int dir_in;
2592 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02002593 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002594
2595 dev_dbg(hsotg->dev,
2596 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2597 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2598 desc->wMaxPacketSize, desc->bInterval);
2599
2600 /* not to be called for EP0 */
2601 WARN_ON(index == 0);
2602
2603 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2604 if (dir_in != hs_ep->dir_in) {
2605 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2606 return -EINVAL;
2607 }
2608
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002609 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002610
2611 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2612
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002613 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002614 epctrl = readl(hsotg->regs + epctrl_reg);
2615
2616 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2617 __func__, epctrl, epctrl_reg);
2618
Lukasz Majewski22258f42012-06-14 10:02:24 +02002619 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002620
Dinh Nguyen47a16852014-04-14 14:13:34 -07002621 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2622 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002623
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002624 /*
2625 * mark the endpoint as active, otherwise the core may ignore
2626 * transactions entirely for this endpoint
2627 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002628 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002629
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002630 /*
2631 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002632 * do something with data that we've yet got a request to process
2633 * since the RXFIFO will take data for an endpoint even if the
2634 * size register hasn't been set.
2635 */
2636
Dinh Nguyen47a16852014-04-14 14:13:34 -07002637 epctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002638
2639 /* update the endpoint state */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002640 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002641
2642 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002643 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002644 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002645 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002646 hs_ep->interval = desc->bInterval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002647
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002648 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2649 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2650
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002651 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2652 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002653 epctrl |= DXEPCTL_EPTYPE_ISO;
2654 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02002655 hs_ep->isochronous = 1;
2656 if (dir_in)
2657 hs_ep->periodic = 1;
2658 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002659
2660 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002661 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002662 break;
2663
2664 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02002665 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002666 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002667
Dinh Nguyen47a16852014-04-14 14:13:34 -07002668 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002669 break;
2670
2671 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002672 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002673 break;
2674 }
2675
Mian Yousaf Kaukab4556e122015-01-09 13:39:05 +01002676 /* If fifo is already allocated for this ep */
2677 if (hs_ep->fifo_index) {
2678 size = hs_ep->ep.maxpacket * hs_ep->mc;
2679 /* If bigger fifo is required deallocate current one */
2680 if (size > hs_ep->fifo_size) {
2681 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
2682 hs_ep->fifo_index = 0;
2683 hs_ep->fifo_size = 0;
2684 }
2685 }
2686
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002687 /*
2688 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002689 * a unique tx-fifo even if it is non-periodic.
2690 */
Mian Yousaf Kaukab4556e122015-01-09 13:39:05 +01002691 if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002692 u32 fifo_index = 0;
2693 u32 fifo_size = UINT_MAX;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002694 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01002695 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02002696 if (hsotg->fifo_map & (1<<i))
2697 continue;
2698 val = readl(hsotg->regs + DPTXFSIZN(i));
2699 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
2700 if (val < size)
2701 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002702 /* Search for smallest acceptable fifo */
2703 if (val < fifo_size) {
2704 fifo_size = val;
2705 fifo_index = i;
2706 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02002707 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002708 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01002709 dev_err(hsotg->dev,
2710 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302711 ret = -ENOMEM;
2712 goto error;
2713 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002714 hsotg->fifo_map |= 1 << fifo_index;
2715 epctrl |= DXEPCTL_TXFNUM(fifo_index);
2716 hs_ep->fifo_index = fifo_index;
2717 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002718 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002719
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002720 /* for non control endpoints, set PID to D0 */
2721 if (index)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002722 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002723
2724 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2725 __func__, epctrl);
2726
2727 writel(epctrl, hsotg->regs + epctrl_reg);
2728 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2729 __func__, readl(hsotg->regs + epctrl_reg));
2730
2731 /* enable the endpoint interrupt */
2732 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2733
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302734error:
Lukasz Majewski22258f42012-06-14 10:02:24 +02002735 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002736 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002737}
2738
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002739/**
2740 * s3c_hsotg_ep_disable - disable given endpoint
2741 * @ep: The endpoint to disable.
2742 */
Robert Baldyga62f4f062014-12-09 14:41:45 +01002743static int s3c_hsotg_ep_disable_force(struct usb_ep *ep, bool force)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002744{
2745 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002746 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002747 int dir_in = hs_ep->dir_in;
2748 int index = hs_ep->index;
2749 unsigned long flags;
2750 u32 epctrl_reg;
2751 u32 ctrl;
2752
Marek Szyprowski1e011292014-09-09 10:44:54 +02002753 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002754
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002755 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002756 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2757 return -EINVAL;
2758 }
2759
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002760 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002761
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002762 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002763
Robert Baldygab203d0a2014-09-09 10:44:56 +02002764 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
2765 hs_ep->fifo_index = 0;
2766 hs_ep->fifo_size = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002767
2768 ctrl = readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002769 ctrl &= ~DXEPCTL_EPENA;
2770 ctrl &= ~DXEPCTL_USBACTEP;
2771 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002772
2773 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2774 writel(ctrl, hsotg->regs + epctrl_reg);
2775
2776 /* disable endpoint interrupts */
2777 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2778
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01002779 /* terminate all requests with shutdown */
2780 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
2781
Lukasz Majewski22258f42012-06-14 10:02:24 +02002782 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002783 return 0;
2784}
2785
Robert Baldyga62f4f062014-12-09 14:41:45 +01002786static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2787{
2788 return s3c_hsotg_ep_disable_force(ep, false);
2789}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002790/**
2791 * on_list - check request is on the given endpoint
2792 * @ep: The endpoint to check.
2793 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002794 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002795static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2796{
2797 struct s3c_hsotg_req *req, *treq;
2798
2799 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2800 if (req == test)
2801 return true;
2802 }
2803
2804 return false;
2805}
2806
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002807/**
2808 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2809 * @ep: The endpoint to dequeue.
2810 * @req: The request to be removed from a queue.
2811 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002812static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2813{
2814 struct s3c_hsotg_req *hs_req = our_req(req);
2815 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002816 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002817 unsigned long flags;
2818
Marek Szyprowski1e011292014-09-09 10:44:54 +02002819 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002820
Lukasz Majewski22258f42012-06-14 10:02:24 +02002821 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002822
2823 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002824 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002825 return -EINVAL;
2826 }
2827
2828 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002829 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002830
2831 return 0;
2832}
2833
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002834/**
2835 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2836 * @ep: The endpoint to set halt.
2837 * @value: Set or unset the halt.
2838 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002839static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2840{
2841 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002842 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002843 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002844 u32 epreg;
2845 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002846 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002847
2848 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2849
Robert Baldygac9f721b2014-01-14 08:36:00 +01002850 if (index == 0) {
2851 if (value)
2852 s3c_hsotg_stall_ep0(hs);
2853 else
2854 dev_warn(hs->dev,
2855 "%s: can't clear halt on ep0\n", __func__);
2856 return 0;
2857 }
2858
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002859 if (hs_ep->dir_in) {
2860 epreg = DIEPCTL(index);
2861 epctl = readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002862
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002863 if (value) {
2864 epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
2865 if (epctl & DXEPCTL_EPENA)
2866 epctl |= DXEPCTL_EPDIS;
2867 } else {
2868 epctl &= ~DXEPCTL_STALL;
2869 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2870 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2871 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2872 epctl |= DXEPCTL_SETD0PID;
2873 }
2874 writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002875 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002876
2877 epreg = DOEPCTL(index);
2878 epctl = readl(hs->regs + epreg);
2879
2880 if (value)
2881 epctl |= DXEPCTL_STALL;
2882 else {
2883 epctl &= ~DXEPCTL_STALL;
2884 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2885 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2886 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2887 epctl |= DXEPCTL_SETD0PID;
2888 }
2889 writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002890 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002891
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002892 hs_ep->halted = value;
2893
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002894 return 0;
2895}
2896
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002897/**
2898 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2899 * @ep: The endpoint to set halt.
2900 * @value: Set or unset the halt.
2901 */
2902static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2903{
2904 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002905 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002906 unsigned long flags = 0;
2907 int ret = 0;
2908
2909 spin_lock_irqsave(&hs->lock, flags);
2910 ret = s3c_hsotg_ep_sethalt(ep, value);
2911 spin_unlock_irqrestore(&hs->lock, flags);
2912
2913 return ret;
2914}
2915
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002916static struct usb_ep_ops s3c_hsotg_ep_ops = {
2917 .enable = s3c_hsotg_ep_enable,
2918 .disable = s3c_hsotg_ep_disable,
2919 .alloc_request = s3c_hsotg_ep_alloc_request,
2920 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002921 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002922 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002923 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002924 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002925};
2926
2927/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002928 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002929 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002930 *
2931 * A wrapper for platform code responsible for controlling
2932 * low-level USB code
2933 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002934static void s3c_hsotg_phy_enable(struct dwc2_hsotg *hsotg)
Lukasz Majewski41188782012-05-04 14:17:01 +02002935{
2936 struct platform_device *pdev = to_platform_device(hsotg->dev);
2937
2938 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302939
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002940 if (hsotg->uphy)
2941 usb_phy_init(hsotg->uphy);
2942 else if (hsotg->plat && hsotg->plat->phy_init)
2943 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2944 else {
Matt Porter74084842013-12-19 09:23:06 -05002945 phy_init(hsotg->phy);
2946 phy_power_on(hsotg->phy);
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002947 }
Lukasz Majewski41188782012-05-04 14:17:01 +02002948}
2949
2950/**
2951 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002952 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002953 *
2954 * A wrapper for platform code responsible for controlling
2955 * low-level USB code
2956 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002957static void s3c_hsotg_phy_disable(struct dwc2_hsotg *hsotg)
Lukasz Majewski41188782012-05-04 14:17:01 +02002958{
2959 struct platform_device *pdev = to_platform_device(hsotg->dev);
2960
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002961 if (hsotg->uphy)
2962 usb_phy_shutdown(hsotg->uphy);
2963 else if (hsotg->plat && hsotg->plat->phy_exit)
2964 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2965 else {
Matt Porter74084842013-12-19 09:23:06 -05002966 phy_power_off(hsotg->phy);
2967 phy_exit(hsotg->phy);
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002968 }
Lukasz Majewski41188782012-05-04 14:17:01 +02002969}
2970
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002971/**
2972 * s3c_hsotg_init - initalize the usb core
2973 * @hsotg: The driver state
2974 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002975static void s3c_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002976{
2977 /* unmask subset of endpoint interrupts */
2978
Dinh Nguyen47a16852014-04-14 14:13:34 -07002979 writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2980 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
2981 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002982
Dinh Nguyen47a16852014-04-14 14:13:34 -07002983 writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
2984 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
2985 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002986
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002987 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002988
2989 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002990 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002991
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002992 /* setup fifos */
2993
2994 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002995 readl(hsotg->regs + GRXFSIZ),
2996 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002997
2998 s3c_hsotg_init_fifo(hsotg);
2999
3000 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003001 writel(GUSBCFG_PHYIF16 | GUSBCFG_TOUTCAL(7) | (0x5 << 10),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003002 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003003
Gregory Herrerof5090042015-01-09 13:38:47 +01003004 if (using_dma(hsotg))
3005 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003006}
3007
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003008/**
3009 * s3c_hsotg_udc_start - prepare the udc for work
3010 * @gadget: The usb gadget state
3011 * @driver: The usb gadget driver
3012 *
3013 * Perform initialization to prepare udc device and driver
3014 * to work.
3015 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003016static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
3017 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003018{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003019 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003020 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003021 int ret;
3022
3023 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003024 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003025 return -ENODEV;
3026 }
3027
3028 if (!driver) {
3029 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3030 return -EINVAL;
3031 }
3032
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003033 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003034 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003035
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003036 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003037 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3038 return -EINVAL;
3039 }
3040
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003041 mutex_lock(&hsotg->init_mutex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003042 WARN_ON(hsotg->driver);
3043
3044 driver->driver.bus = NULL;
3045 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003046 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003047 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3048
Robert Baldygad00b4142014-09-09 10:44:57 +02003049 clk_enable(hsotg->clk);
3050
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003051 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3052 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003053 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003054 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003055 goto err;
3056 }
3057
Marek Szyprowskic816c472014-10-20 12:45:37 +02003058 s3c_hsotg_phy_enable(hsotg);
Gregory Herrerof6c01592015-01-09 13:38:41 +01003059 if (!IS_ERR_OR_NULL(hsotg->uphy))
3060 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003061
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003062 spin_lock_irqsave(&hsotg->lock, flags);
3063 s3c_hsotg_init(hsotg);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003064 s3c_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003065 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003066 spin_unlock_irqrestore(&hsotg->lock, flags);
3067
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003068 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003069
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003070 mutex_unlock(&hsotg->init_mutex);
3071
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003072 return 0;
3073
3074err:
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003075 mutex_unlock(&hsotg->init_mutex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003076 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003077 return ret;
3078}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003079
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003080/**
3081 * s3c_hsotg_udc_stop - stop the udc
3082 * @gadget: The usb gadget state
3083 * @driver: The usb gadget driver
3084 *
3085 * Stop udc hw block and stay tunned for future transmissions
3086 */
Felipe Balbi22835b82014-10-17 12:05:12 -05003087static int s3c_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003088{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003089 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003090 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003091 int ep;
3092
3093 if (!hsotg)
3094 return -ENODEV;
3095
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003096 mutex_lock(&hsotg->init_mutex);
3097
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003098 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003099 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3100 if (hsotg->eps_in[ep])
3101 s3c_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3102 if (hsotg->eps_out[ep])
3103 s3c_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3104 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003105
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003106 spin_lock_irqsave(&hsotg->lock, flags);
3107
Marek Szyprowski32805c32014-10-20 12:45:33 +02003108 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003109 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003110 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003111
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003112 spin_unlock_irqrestore(&hsotg->lock, flags);
3113
Gregory Herrerof6c01592015-01-09 13:38:41 +01003114 if (!IS_ERR_OR_NULL(hsotg->uphy))
3115 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003116 s3c_hsotg_phy_disable(hsotg);
3117
Marek Szyprowskic8c10252013-09-12 16:18:48 +02003118 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003119
Robert Baldygad00b4142014-09-09 10:44:57 +02003120 clk_disable(hsotg->clk);
3121
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003122 mutex_unlock(&hsotg->init_mutex);
3123
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003124 return 0;
3125}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003126
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003127/**
3128 * s3c_hsotg_gadget_getframe - read the frame number
3129 * @gadget: The usb gadget state
3130 *
3131 * Read the {micro} frame number
3132 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003133static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
3134{
3135 return s3c_hsotg_read_frameno(to_hsotg(gadget));
3136}
3137
Lukasz Majewskia188b682012-06-22 09:29:56 +02003138/**
3139 * s3c_hsotg_pullup - connect/disconnect the USB PHY
3140 * @gadget: The usb gadget state
3141 * @is_on: Current state of the USB PHY
3142 *
3143 * Connect/Disconnect the USB PHY pullup
3144 */
3145static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
3146{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003147 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003148 unsigned long flags = 0;
3149
Andrzej Pietrasiewiczd784f1e2014-09-09 10:44:53 +02003150 dev_dbg(hsotg->dev, "%s: is_on: %d\n", __func__, is_on);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003151
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003152 mutex_lock(&hsotg->init_mutex);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003153 spin_lock_irqsave(&hsotg->lock, flags);
3154 if (is_on) {
Robert Baldygad00b4142014-09-09 10:44:57 +02003155 clk_enable(hsotg->clk);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003156 hsotg->enabled = 1;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003157 s3c_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003158 } else {
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003159 s3c_hsotg_core_disconnect(hsotg);
Gregory Herrero6d136732015-01-09 13:39:07 +01003160 s3c_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003161 hsotg->enabled = 0;
Robert Baldygad00b4142014-09-09 10:44:57 +02003162 clk_disable(hsotg->clk);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003163 }
3164
3165 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3166 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003167 mutex_unlock(&hsotg->init_mutex);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003168
3169 return 0;
3170}
3171
Gregory Herrero83d98222015-01-09 13:39:02 +01003172static int s3c_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
3173{
3174 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3175 unsigned long flags;
3176
3177 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
3178 spin_lock_irqsave(&hsotg->lock, flags);
3179
3180 if (is_active) {
3181 /* Kill any ep0 requests as controller will be reinitialized */
3182 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003183 s3c_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01003184 if (hsotg->enabled)
3185 s3c_hsotg_core_connect(hsotg);
3186 } else {
3187 s3c_hsotg_core_disconnect(hsotg);
3188 s3c_hsotg_disconnect(hsotg);
3189 }
3190
3191 spin_unlock_irqrestore(&hsotg->lock, flags);
3192 return 0;
3193}
3194
Gregory Herrero596d6962015-01-09 13:39:08 +01003195/**
3196 * s3c_hsotg_vbus_draw - report bMaxPower field
3197 * @gadget: The usb gadget state
3198 * @mA: Amount of current
3199 *
3200 * Report how much power the device may consume to the phy.
3201 */
3202static int s3c_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
3203{
3204 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3205
3206 if (IS_ERR_OR_NULL(hsotg->uphy))
3207 return -ENOTSUPP;
3208 return usb_phy_set_power(hsotg->uphy, mA);
3209}
3210
Felipe Balbieeef4582013-01-24 17:58:16 +02003211static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003212 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003213 .udc_start = s3c_hsotg_udc_start,
3214 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02003215 .pullup = s3c_hsotg_pullup,
Gregory Herrero83d98222015-01-09 13:39:02 +01003216 .vbus_session = s3c_hsotg_vbus_session,
Gregory Herrero596d6962015-01-09 13:39:08 +01003217 .vbus_draw = s3c_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003218};
3219
3220/**
3221 * s3c_hsotg_initep - initialise a single endpoint
3222 * @hsotg: The device state.
3223 * @hs_ep: The endpoint to be initialised.
3224 * @epnum: The endpoint number
3225 *
3226 * Initialise the given endpoint (as part of the probe and device state
3227 * creation) to give to the gadget driver. Setup the endpoint name, any
3228 * direction information and other state that may be required.
3229 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003230static void s3c_hsotg_initep(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003231 struct s3c_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003232 int epnum,
3233 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003234{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003235 char *dir;
3236
3237 if (epnum == 0)
3238 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003239 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003240 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003241 else
3242 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003243
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003244 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003245 hs_ep->index = epnum;
3246
3247 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3248
3249 INIT_LIST_HEAD(&hs_ep->queue);
3250 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3251
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003252 /* add to the list of endpoints known by the gadget driver */
3253 if (epnum)
3254 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3255
3256 hs_ep->parent = hsotg;
3257 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003258 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003259 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3260
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003261 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003262 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003263 * to be something valid.
3264 */
3265
3266 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003267 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003268 if (dir_in)
3269 writel(next, hsotg->regs + DIEPCTL(epnum));
3270 else
3271 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003272 }
3273}
3274
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003275/**
3276 * s3c_hsotg_hw_cfg - read HW configuration registers
3277 * @param: The device state
3278 *
3279 * Read the USB core HW configuration registers
3280 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003281static int s3c_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003282{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003283 u32 cfg;
3284 u32 ep_type;
3285 u32 i;
3286
Ben Dooks10aebc72010-07-19 09:40:44 +01003287 /* check hardware configuration */
3288
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003289 cfg = readl(hsotg->regs + GHWCFG2);
3290 hsotg->num_of_eps = (cfg >> 10) & 0xF;
3291 /* Add ep0 */
3292 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003293
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003294 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct s3c_hsotg_ep),
3295 GFP_KERNEL);
3296 if (!hsotg->eps_in[0])
3297 return -ENOMEM;
3298 /* Same s3c_hsotg_ep is used in both directions for ep0 */
3299 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003300
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003301 cfg = readl(hsotg->regs + GHWCFG1);
3302 for (i = 1; i < hsotg->num_of_eps; i++, cfg >>= 2) {
3303 ep_type = cfg & 3;
3304 /* Direction in or both */
3305 if (!(ep_type & 2)) {
3306 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
3307 sizeof(struct s3c_hsotg_ep), GFP_KERNEL);
3308 if (!hsotg->eps_in[i])
3309 return -ENOMEM;
3310 }
3311 /* Direction out or both */
3312 if (!(ep_type & 1)) {
3313 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
3314 sizeof(struct s3c_hsotg_ep), GFP_KERNEL);
3315 if (!hsotg->eps_out[i])
3316 return -ENOMEM;
3317 }
3318 }
3319
3320 cfg = readl(hsotg->regs + GHWCFG3);
3321 hsotg->fifo_mem = (cfg >> 16);
3322
3323 cfg = readl(hsotg->regs + GHWCFG4);
3324 hsotg->dedicated_fifos = (cfg >> 25) & 1;
Ben Dooks10aebc72010-07-19 09:40:44 +01003325
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003326 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3327 hsotg->num_of_eps,
3328 hsotg->dedicated_fifos ? "dedicated" : "shared",
3329 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003330 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003331}
3332
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003333/**
3334 * s3c_hsotg_dump - dump state of the udc
3335 * @param: The device state
3336 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003337static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003338{
Mark Brown83a01802011-06-01 17:16:15 +01003339#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003340 struct device *dev = hsotg->dev;
3341 void __iomem *regs = hsotg->regs;
3342 u32 val;
3343 int idx;
3344
3345 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003346 readl(regs + DCFG), readl(regs + DCTL),
3347 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003348
3349 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003350 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003351
3352 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003353 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003354
3355 /* show periodic fifo settings */
3356
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003357 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003358 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003359 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003360 val >> FIFOSIZE_DEPTH_SHIFT,
3361 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003362 }
3363
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003364 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003365 dev_info(dev,
3366 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003367 readl(regs + DIEPCTL(idx)),
3368 readl(regs + DIEPTSIZ(idx)),
3369 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003370
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003371 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003372 dev_info(dev,
3373 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003374 idx, readl(regs + DOEPCTL(idx)),
3375 readl(regs + DOEPTSIZ(idx)),
3376 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003377
3378 }
3379
3380 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003381 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003382#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003383}
3384
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003385/**
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003386 * testmode_write - debugfs: change usb test mode
3387 * @seq: The seq file to write to.
3388 * @v: Unused parameter.
3389 *
3390 * This debugfs entry modify the current usb test mode.
3391 */
3392static ssize_t testmode_write(struct file *file, const char __user *ubuf, size_t
3393 count, loff_t *ppos)
3394{
3395 struct seq_file *s = file->private_data;
3396 struct dwc2_hsotg *hsotg = s->private;
3397 unsigned long flags;
3398 u32 testmode = 0;
3399 char buf[32];
3400
3401 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
3402 return -EFAULT;
3403
3404 if (!strncmp(buf, "test_j", 6))
3405 testmode = TEST_J;
3406 else if (!strncmp(buf, "test_k", 6))
3407 testmode = TEST_K;
3408 else if (!strncmp(buf, "test_se0_nak", 12))
3409 testmode = TEST_SE0_NAK;
3410 else if (!strncmp(buf, "test_packet", 11))
3411 testmode = TEST_PACKET;
3412 else if (!strncmp(buf, "test_force_enable", 17))
3413 testmode = TEST_FORCE_EN;
3414 else
3415 testmode = 0;
3416
3417 spin_lock_irqsave(&hsotg->lock, flags);
3418 s3c_hsotg_set_test_mode(hsotg, testmode);
3419 spin_unlock_irqrestore(&hsotg->lock, flags);
3420 return count;
3421}
3422
3423/**
3424 * testmode_show - debugfs: show usb test mode state
3425 * @seq: The seq file to write to.
3426 * @v: Unused parameter.
3427 *
3428 * This debugfs entry shows which usb test mode is currently enabled.
3429 */
3430static int testmode_show(struct seq_file *s, void *unused)
3431{
3432 struct dwc2_hsotg *hsotg = s->private;
3433 unsigned long flags;
3434 int dctl;
3435
3436 spin_lock_irqsave(&hsotg->lock, flags);
3437 dctl = readl(hsotg->regs + DCTL);
3438 dctl &= DCTL_TSTCTL_MASK;
3439 dctl >>= DCTL_TSTCTL_SHIFT;
3440 spin_unlock_irqrestore(&hsotg->lock, flags);
3441
3442 switch (dctl) {
3443 case 0:
3444 seq_puts(s, "no test\n");
3445 break;
3446 case TEST_J:
3447 seq_puts(s, "test_j\n");
3448 break;
3449 case TEST_K:
3450 seq_puts(s, "test_k\n");
3451 break;
3452 case TEST_SE0_NAK:
3453 seq_puts(s, "test_se0_nak\n");
3454 break;
3455 case TEST_PACKET:
3456 seq_puts(s, "test_packet\n");
3457 break;
3458 case TEST_FORCE_EN:
3459 seq_puts(s, "test_force_enable\n");
3460 break;
3461 default:
3462 seq_printf(s, "UNKNOWN %d\n", dctl);
3463 }
3464
3465 return 0;
3466}
3467
3468static int testmode_open(struct inode *inode, struct file *file)
3469{
3470 return single_open(file, testmode_show, inode->i_private);
3471}
3472
3473static const struct file_operations testmode_fops = {
3474 .owner = THIS_MODULE,
3475 .open = testmode_open,
3476 .write = testmode_write,
3477 .read = seq_read,
3478 .llseek = seq_lseek,
3479 .release = single_release,
3480};
3481
3482/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003483 * state_show - debugfs: show overall driver and device state.
3484 * @seq: The seq file to write to.
3485 * @v: Unused parameter.
3486 *
3487 * This debugfs entry shows the overall state of the hardware and
3488 * some general information about each of the endpoints available
3489 * to the system.
3490 */
3491static int state_show(struct seq_file *seq, void *v)
3492{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003493 struct dwc2_hsotg *hsotg = seq->private;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003494 void __iomem *regs = hsotg->regs;
3495 int idx;
3496
3497 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003498 readl(regs + DCFG),
3499 readl(regs + DCTL),
3500 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003501
3502 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003503 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003504
3505 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003506 readl(regs + GINTMSK),
3507 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003508
3509 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003510 readl(regs + DAINTMSK),
3511 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003512
3513 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003514 readl(regs + GNPTXSTS),
3515 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003516
Pavel Macheka023da32013-09-30 14:56:02 +02003517 seq_puts(seq, "\nEndpoint status:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003518
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003519 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003520 u32 in, out;
3521
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003522 in = readl(regs + DIEPCTL(idx));
3523 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003524
3525 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3526 idx, in, out);
3527
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003528 in = readl(regs + DIEPTSIZ(idx));
3529 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003530
3531 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3532 in, out);
3533
Pavel Macheka023da32013-09-30 14:56:02 +02003534 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003535 }
3536
3537 return 0;
3538}
3539
3540static int state_open(struct inode *inode, struct file *file)
3541{
3542 return single_open(file, state_show, inode->i_private);
3543}
3544
3545static const struct file_operations state_fops = {
3546 .owner = THIS_MODULE,
3547 .open = state_open,
3548 .read = seq_read,
3549 .llseek = seq_lseek,
3550 .release = single_release,
3551};
3552
3553/**
3554 * fifo_show - debugfs: show the fifo information
3555 * @seq: The seq_file to write data to.
3556 * @v: Unused parameter.
3557 *
3558 * Show the FIFO information for the overall fifo and all the
3559 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003560 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003561static int fifo_show(struct seq_file *seq, void *v)
3562{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003563 struct dwc2_hsotg *hsotg = seq->private;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003564 void __iomem *regs = hsotg->regs;
3565 u32 val;
3566 int idx;
3567
Pavel Macheka023da32013-09-30 14:56:02 +02003568 seq_puts(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003569 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003570
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003571 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003572 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Dinh Nguyen47a16852014-04-14 14:13:34 -07003573 val >> FIFOSIZE_DEPTH_SHIFT,
3574 val & FIFOSIZE_DEPTH_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003575
Pavel Macheka023da32013-09-30 14:56:02 +02003576 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003577
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003578 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003579 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003580
3581 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003582 val >> FIFOSIZE_DEPTH_SHIFT,
3583 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003584 }
3585
3586 return 0;
3587}
3588
3589static int fifo_open(struct inode *inode, struct file *file)
3590{
3591 return single_open(file, fifo_show, inode->i_private);
3592}
3593
3594static const struct file_operations fifo_fops = {
3595 .owner = THIS_MODULE,
3596 .open = fifo_open,
3597 .read = seq_read,
3598 .llseek = seq_lseek,
3599 .release = single_release,
3600};
3601
3602
3603static const char *decode_direction(int is_in)
3604{
3605 return is_in ? "in" : "out";
3606}
3607
3608/**
3609 * ep_show - debugfs: show the state of an endpoint.
3610 * @seq: The seq_file to write data to.
3611 * @v: Unused parameter.
3612 *
3613 * This debugfs entry shows the state of the given endpoint (one is
3614 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003615 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003616static int ep_show(struct seq_file *seq, void *v)
3617{
3618 struct s3c_hsotg_ep *ep = seq->private;
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003619 struct dwc2_hsotg *hsotg = ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003620 struct s3c_hsotg_req *req;
3621 void __iomem *regs = hsotg->regs;
3622 int index = ep->index;
3623 int show_limit = 15;
3624 unsigned long flags;
3625
3626 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3627 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3628
3629 /* first show the register state */
3630
3631 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003632 readl(regs + DIEPCTL(index)),
3633 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003634
3635 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003636 readl(regs + DIEPDMA(index)),
3637 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003638
3639 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003640 readl(regs + DIEPINT(index)),
3641 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003642
3643 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003644 readl(regs + DIEPTSIZ(index)),
3645 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003646
Pavel Macheka023da32013-09-30 14:56:02 +02003647 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003648 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3649 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3650
3651 seq_printf(seq, "request list (%p,%p):\n",
3652 ep->queue.next, ep->queue.prev);
3653
Lukasz Majewski22258f42012-06-14 10:02:24 +02003654 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003655
3656 list_for_each_entry(req, &ep->queue, queue) {
3657 if (--show_limit < 0) {
Pavel Macheka023da32013-09-30 14:56:02 +02003658 seq_puts(seq, "not showing more requests...\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003659 break;
3660 }
3661
3662 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3663 req == ep->req ? '*' : ' ',
3664 req, req->req.length, req->req.buf);
3665 seq_printf(seq, "%d done, res %d\n",
3666 req->req.actual, req->req.status);
3667 }
3668
Lukasz Majewski22258f42012-06-14 10:02:24 +02003669 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003670
3671 return 0;
3672}
3673
3674static int ep_open(struct inode *inode, struct file *file)
3675{
3676 return single_open(file, ep_show, inode->i_private);
3677}
3678
3679static const struct file_operations ep_fops = {
3680 .owner = THIS_MODULE,
3681 .open = ep_open,
3682 .read = seq_read,
3683 .llseek = seq_lseek,
3684 .release = single_release,
3685};
3686
3687/**
3688 * s3c_hsotg_create_debug - create debugfs directory and files
3689 * @hsotg: The driver state
3690 *
3691 * Create the debugfs files to allow the user to get information
3692 * about the state of the system. The directory name is created
3693 * with the same name as the device itself, in case we end up
3694 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003695 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003696static void s3c_hsotg_create_debug(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003697{
3698 struct dentry *root;
3699 unsigned epidx;
3700
3701 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3702 hsotg->debug_root = root;
3703 if (IS_ERR(root)) {
3704 dev_err(hsotg->dev, "cannot create debug root\n");
3705 return;
3706 }
3707
3708 /* create general state file */
3709
3710 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3711 hsotg, &state_fops);
3712
3713 if (IS_ERR(hsotg->debug_file))
3714 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3715
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003716 hsotg->debug_testmode = debugfs_create_file("testmode",
3717 S_IRUGO | S_IWUSR, root,
3718 hsotg, &testmode_fops);
3719
3720 if (IS_ERR(hsotg->debug_testmode))
3721 dev_err(hsotg->dev, "%s: failed to create testmode\n",
3722 __func__);
3723
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003724 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3725 hsotg, &fifo_fops);
3726
3727 if (IS_ERR(hsotg->debug_fifo))
3728 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3729
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003730 /* Create one file for each out endpoint */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003731 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003732 struct s3c_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003733
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003734 ep = hsotg->eps_out[epidx];
3735 if (ep) {
3736 ep->debugfs = debugfs_create_file(ep->name, 0444,
3737 root, ep, &ep_fops);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003738
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003739 if (IS_ERR(ep->debugfs))
3740 dev_err(hsotg->dev, "failed to create %s debug file\n",
3741 ep->name);
3742 }
3743 }
3744 /* Create one file for each in endpoint. EP0 is handled with out eps */
3745 for (epidx = 1; epidx < hsotg->num_of_eps; epidx++) {
3746 struct s3c_hsotg_ep *ep;
3747
3748 ep = hsotg->eps_in[epidx];
3749 if (ep) {
3750 ep->debugfs = debugfs_create_file(ep->name, 0444,
3751 root, ep, &ep_fops);
3752
3753 if (IS_ERR(ep->debugfs))
3754 dev_err(hsotg->dev, "failed to create %s debug file\n",
3755 ep->name);
3756 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003757 }
3758}
3759
3760/**
3761 * s3c_hsotg_delete_debug - cleanup debugfs entries
3762 * @hsotg: The driver state
3763 *
3764 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003765 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003766static void s3c_hsotg_delete_debug(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003767{
3768 unsigned epidx;
3769
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003770 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003771 if (hsotg->eps_in[epidx])
3772 debugfs_remove(hsotg->eps_in[epidx]->debugfs);
3773 if (hsotg->eps_out[epidx])
3774 debugfs_remove(hsotg->eps_out[epidx]->debugfs);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003775 }
3776
3777 debugfs_remove(hsotg->debug_file);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003778 debugfs_remove(hsotg->debug_testmode);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003779 debugfs_remove(hsotg->debug_fifo);
3780 debugfs_remove(hsotg->debug_root);
3781}
3782
Gregory Herreroedd74be2015-01-09 13:38:48 +01003783#ifdef CONFIG_OF
3784static void s3c_hsotg_of_probe(struct dwc2_hsotg *hsotg)
3785{
3786 struct device_node *np = hsotg->dev->of_node;
Gregory Herrero0a176272015-01-09 13:38:52 +01003787 u32 len = 0;
3788 u32 i = 0;
Gregory Herreroedd74be2015-01-09 13:38:48 +01003789
3790 /* Enable dma if requested in device tree */
3791 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
Gregory Herrero0a176272015-01-09 13:38:52 +01003792
3793 /*
3794 * Register TX periodic fifo size per endpoint.
3795 * EP0 is excluded since it has no fifo configuration.
3796 */
3797 if (!of_find_property(np, "g-tx-fifo-size", &len))
3798 goto rx_fifo;
3799
3800 len /= sizeof(u32);
3801
3802 /* Read tx fifo sizes other than ep0 */
3803 if (of_property_read_u32_array(np, "g-tx-fifo-size",
3804 &hsotg->g_tx_fifo_sz[1], len))
3805 goto rx_fifo;
3806
3807 /* Add ep0 */
3808 len++;
3809
3810 /* Make remaining TX fifos unavailable */
3811 if (len < MAX_EPS_CHANNELS) {
3812 for (i = len; i < MAX_EPS_CHANNELS; i++)
3813 hsotg->g_tx_fifo_sz[i] = 0;
3814 }
3815
3816rx_fifo:
3817 /* Register RX fifo size */
3818 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
3819
3820 /* Register NPTX fifo size */
3821 of_property_read_u32(np, "g-np-tx-fifo-size",
3822 &hsotg->g_np_g_tx_fifo_sz);
Gregory Herreroedd74be2015-01-09 13:38:48 +01003823}
3824#else
3825static inline void s3c_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
3826#endif
3827
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003828/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06003829 * dwc2_gadget_init - init function for gadget
3830 * @dwc2: The data structure for the DWC2 driver.
3831 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003832 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003833int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003834{
Dinh Nguyen117777b2014-11-11 11:13:34 -06003835 struct device *dev = hsotg->dev;
3836 struct s3c_hsotg_plat *plat = dev->platform_data;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003837 int epnum;
3838 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003839 int i;
Gregory Herrero0a176272015-01-09 13:38:52 +01003840 u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003841
Kamil Debski1b59fc72014-09-09 10:44:52 +02003842 /* Set default UTMI width */
3843 hsotg->phyif = GUSBCFG_PHYIF16;
3844
Gregory Herreroedd74be2015-01-09 13:38:48 +01003845 s3c_hsotg_of_probe(hsotg);
3846
Gregory Herrero0a176272015-01-09 13:38:52 +01003847 /* Initialize to legacy fifo configuration values */
3848 hsotg->g_rx_fifo_sz = 2048;
3849 hsotg->g_np_g_tx_fifo_sz = 1024;
3850 memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
3851 /* Device tree specific probe */
3852 s3c_hsotg_of_probe(hsotg);
3853 /* Dump fifo information */
3854 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
3855 hsotg->g_np_g_tx_fifo_sz);
3856 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
3857 for (i = 0; i < MAX_EPS_CHANNELS; i++)
3858 dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
3859 hsotg->g_tx_fifo_sz[i]);
Matt Porter74084842013-12-19 09:23:06 -05003860 /*
Yunzhi Li135b3c42014-12-08 17:46:26 +08003861 * If platform probe couldn't find a generic PHY or an old style
3862 * USB PHY, fall back to pdata
Matt Porter74084842013-12-19 09:23:06 -05003863 */
Yunzhi Li135b3c42014-12-08 17:46:26 +08003864 if (IS_ERR_OR_NULL(hsotg->phy) && IS_ERR_OR_NULL(hsotg->uphy)) {
3865 plat = dev_get_platdata(dev);
3866 if (!plat) {
3867 dev_err(dev,
3868 "no platform data or transceiver defined\n");
3869 return -EPROBE_DEFER;
3870 }
3871 hsotg->plat = plat;
3872 } else if (hsotg->phy) {
Kamil Debski1b59fc72014-09-09 10:44:52 +02003873 /*
3874 * If using the generic PHY framework, check if the PHY bus
3875 * width is 8-bit and set the phyif appropriately.
3876 */
Yunzhi Li135b3c42014-12-08 17:46:26 +08003877 if (phy_get_bus_width(hsotg->phy) == 8)
Kamil Debski1b59fc72014-09-09 10:44:52 +02003878 hsotg->phyif = GUSBCFG_PHYIF8;
3879 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303880
Dinh Nguyen117777b2014-11-11 11:13:34 -06003881 hsotg->clk = devm_clk_get(dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003882 if (IS_ERR(hsotg->clk)) {
Dinh Nguyen8d736d82014-11-11 11:13:38 -06003883 hsotg->clk = NULL;
Dinh Nguyenf415fbd2014-11-24 11:02:11 -06003884 dev_dbg(dev, "cannot get otg clock\n");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003885 }
3886
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003887 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003888 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3889 hsotg->gadget.name = dev_name(dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003890
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003891 /* reset the system */
3892
Dinh Nguyenf415fbd2014-11-24 11:02:11 -06003893 ret = clk_prepare_enable(hsotg->clk);
3894 if (ret) {
3895 dev_err(dev, "failed to enable otg clk\n");
3896 goto err_clk;
3897 }
3898
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003899
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003900 /* regulators */
3901
3902 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3903 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3904
Sachin Kamatcd762132013-01-08 14:27:00 +05303905 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003906 hsotg->supplies);
3907 if (ret) {
3908 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303909 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003910 }
3911
3912 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3913 hsotg->supplies);
3914
3915 if (ret) {
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003916 dev_err(dev, "failed to enable supplies: %d\n", ret);
Mian Yousaf Kaukabc139ec22015-01-09 13:38:45 +01003917 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003918 }
3919
Lukasz Majewski41188782012-05-04 14:17:01 +02003920 /* usb phy enable */
3921 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003922
Gregory Herrero1b7a66b2015-01-09 13:39:09 +01003923 /*
3924 * Force Device mode before initialization.
3925 * This allows correctly configuring fifo for device mode.
3926 */
3927 __bic32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEHOSTMODE);
3928 __orr32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEDEVMODE);
3929
3930 /*
3931 * According to Synopsys databook, this sleep is needed for the force
3932 * device mode to take effect.
3933 */
3934 msleep(25);
3935
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003936 s3c_hsotg_corereset(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003937 ret = s3c_hsotg_hw_cfg(hsotg);
3938 if (ret) {
3939 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
3940 goto err_clk;
3941 }
3942
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003943 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003944
Gregory Herrero1b7a66b2015-01-09 13:39:09 +01003945 /* Switch back to default configuration */
3946 __bic32(hsotg->regs + GUSBCFG, GUSBCFG_FORCEDEVMODE);
3947
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003948 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
3949 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3950 if (!hsotg->ctrl_buff) {
3951 dev_err(dev, "failed to allocate ctrl request buff\n");
3952 ret = -ENOMEM;
3953 goto err_supplies;
3954 }
3955
3956 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
3957 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3958 if (!hsotg->ep0_buff) {
3959 dev_err(dev, "failed to allocate ctrl reply buff\n");
3960 ret = -ENOMEM;
3961 goto err_supplies;
3962 }
3963
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003964 ret = devm_request_irq(hsotg->dev, irq, s3c_hsotg_irq, IRQF_SHARED,
3965 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003966 if (ret < 0) {
3967 s3c_hsotg_phy_disable(hsotg);
3968 clk_disable_unprepare(hsotg->clk);
3969 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3970 hsotg->supplies);
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003971 dev_err(dev, "cannot claim IRQ for gadget\n");
Mian Yousaf Kaukabc139ec22015-01-09 13:38:45 +01003972 goto err_supplies;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003973 }
3974
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003975 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3976
3977 if (hsotg->num_of_eps == 0) {
3978 dev_err(dev, "wrong number of EPs (zero)\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003979 ret = -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003980 goto err_supplies;
3981 }
3982
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003983 /* setup endpoint information */
3984
3985 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003986 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003987
3988 /* allocate EP0 request */
3989
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003990 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003991 GFP_KERNEL);
3992 if (!hsotg->ctrl_req) {
3993 dev_err(dev, "failed to allocate ctrl req\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003994 ret = -ENOMEM;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003995 goto err_supplies;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003996 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003997
3998 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003999 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4000 if (hsotg->eps_in[epnum])
4001 s3c_hsotg_initep(hsotg, hsotg->eps_in[epnum],
4002 epnum, 1);
4003 if (hsotg->eps_out[epnum])
4004 s3c_hsotg_initep(hsotg, hsotg->eps_out[epnum],
4005 epnum, 0);
4006 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004007
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004008 /* disable power and clock */
Marek Szyprowski3a8146a2014-10-20 12:45:34 +02004009 s3c_hsotg_phy_disable(hsotg);
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004010
4011 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
4012 hsotg->supplies);
4013 if (ret) {
Dinh Nguyen117777b2014-11-11 11:13:34 -06004014 dev_err(dev, "failed to disable supplies: %d\n", ret);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004015 goto err_supplies;
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004016 }
4017
Dinh Nguyen117777b2014-11-11 11:13:34 -06004018 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004019 if (ret)
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004020 goto err_supplies;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004021
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004022 s3c_hsotg_create_debug(hsotg);
4023
4024 s3c_hsotg_dump(hsotg);
4025
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004026 return 0;
4027
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02004028err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02004029 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004030err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02004031 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05304032
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004033 return ret;
4034}
Dinh Nguyen117777b2014-11-11 11:13:34 -06004035EXPORT_SYMBOL_GPL(dwc2_gadget_init);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004036
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004037/**
4038 * s3c_hsotg_remove - remove function for hsotg driver
4039 * @pdev: The platform information for the driver
4040 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06004041int s3c_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004042{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004043 usb_del_gadget_udc(&hsotg->gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004044 s3c_hsotg_delete_debug(hsotg);
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02004045 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004046
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004047 return 0;
4048}
Dinh Nguyen117777b2014-11-11 11:13:34 -06004049EXPORT_SYMBOL_GPL(s3c_hsotg_remove);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004050
Dinh Nguyen117777b2014-11-11 11:13:34 -06004051int s3c_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004052{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004053 unsigned long flags;
4054 int ret = 0;
4055
Marek Szyprowski7ad80962014-11-21 15:14:48 +01004056 mutex_lock(&hsotg->init_mutex);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004057
4058 if (hsotg->driver) {
4059 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004060
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004061 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4062 hsotg->driver->driver.name);
4063
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004064 spin_lock_irqsave(&hsotg->lock, flags);
4065 if (hsotg->enabled)
4066 s3c_hsotg_core_disconnect(hsotg);
4067 s3c_hsotg_disconnect(hsotg);
4068 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4069 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004070
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004071 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski7b093f72014-10-20 12:45:39 +02004072
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004073 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4074 if (hsotg->eps_in[ep])
4075 s3c_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
4076 if (hsotg->eps_out[ep])
4077 s3c_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
4078 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004079
4080 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
4081 hsotg->supplies);
Robert Baldygad00b4142014-09-09 10:44:57 +02004082 clk_disable(hsotg->clk);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004083 }
4084
Marek Szyprowski7ad80962014-11-21 15:14:48 +01004085 mutex_unlock(&hsotg->init_mutex);
4086
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004087 return ret;
4088}
Dinh Nguyen117777b2014-11-11 11:13:34 -06004089EXPORT_SYMBOL_GPL(s3c_hsotg_suspend);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004090
Dinh Nguyen117777b2014-11-11 11:13:34 -06004091int s3c_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004092{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004093 unsigned long flags;
4094 int ret = 0;
4095
Marek Szyprowski7ad80962014-11-21 15:14:48 +01004096 mutex_lock(&hsotg->init_mutex);
4097
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004098 if (hsotg->driver) {
4099 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4100 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004101
4102 clk_enable(hsotg->clk);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004103 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004104 hsotg->supplies);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004105
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004106 s3c_hsotg_phy_enable(hsotg);
4107
4108 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01004109 s3c_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004110 if (hsotg->enabled)
4111 s3c_hsotg_core_connect(hsotg);
4112 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004113 }
Marek Szyprowski7ad80962014-11-21 15:14:48 +01004114 mutex_unlock(&hsotg->init_mutex);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004115
4116 return ret;
4117}
Dinh Nguyen117777b2014-11-11 11:13:34 -06004118EXPORT_SYMBOL_GPL(s3c_hsotg_resume);