blob: c3977a8d70accecf914b3b9a67212ed48d5677bc [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>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010038
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070039#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060040#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041
42/* conversion functions */
43static inline struct s3c_hsotg_req *our_req(struct usb_request *req)
44{
45 return container_of(req, struct s3c_hsotg_req, req);
46}
47
48static inline struct s3c_hsotg_ep *our_ep(struct usb_ep *ep)
49{
50 return container_of(ep, struct s3c_hsotg_ep, ep);
51}
52
Dinh Nguyen941fcce2014-11-11 11:13:33 -060053static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010054{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060055 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010056}
57
58static inline void __orr32(void __iomem *ptr, u32 val)
59{
60 writel(readl(ptr) | val, ptr);
61}
62
63static inline void __bic32(void __iomem *ptr, u32 val)
64{
65 writel(readl(ptr) & ~val, ptr);
66}
67
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010068static inline struct s3c_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
69 u32 ep_index, u32 dir_in)
70{
71 if (dir_in)
72 return hsotg->eps_in[ep_index];
73 else
74 return hsotg->eps_out[ep_index];
75}
76
Mickael Maison997f4f82014-12-23 17:39:45 +010077/* forward declaration of functions */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060078static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010079
80/**
81 * using_dma - return the DMA status of the driver.
82 * @hsotg: The driver state.
83 *
84 * Return true if we're using DMA.
85 *
86 * Currently, we have the DMA support code worked into everywhere
87 * that needs it, but the AMBA DMA implementation in the hardware can
88 * only DMA from 32bit aligned addresses. This means that gadgets such
89 * as the CDC Ethernet cannot work as they often pass packets which are
90 * not 32bit aligned.
91 *
92 * Unfortunately the choice to use DMA or not is global to the controller
93 * and seems to be only settable when the controller is being put through
94 * a core reset. This means we either need to fix the gadgets to take
95 * account of DMA alignment, or add bounce buffers (yuerk).
96 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010097 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010098 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060099static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100100{
Gregory Herreroedd74be2015-01-09 13:38:48 +0100101 return hsotg->g_using_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100102}
103
104/**
105 * s3c_hsotg_en_gsint - enable one or more of the general interrupt
106 * @hsotg: The device state
107 * @ints: A bitmask of the interrupts to enable
108 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600109static void s3c_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100110{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200111 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100112 u32 new_gsintmsk;
113
114 new_gsintmsk = gsintmsk | ints;
115
116 if (new_gsintmsk != gsintmsk) {
117 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200118 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100119 }
120}
121
122/**
123 * s3c_hsotg_disable_gsint - disable one or more of the general interrupt
124 * @hsotg: The device state
125 * @ints: A bitmask of the interrupts to enable
126 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600127static void s3c_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100128{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200129 u32 gsintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100130 u32 new_gsintmsk;
131
132 new_gsintmsk = gsintmsk & ~ints;
133
134 if (new_gsintmsk != gsintmsk)
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200135 writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100136}
137
138/**
139 * s3c_hsotg_ctrl_epint - enable/disable an endpoint irq
140 * @hsotg: The device state
141 * @ep: The endpoint index
142 * @dir_in: True if direction is in.
143 * @en: The enable value, true to enable
144 *
145 * Set or clear the mask for an individual endpoint's interrupt
146 * request.
147 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600148static void s3c_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100149 unsigned int ep, unsigned int dir_in,
150 unsigned int en)
151{
152 unsigned long flags;
153 u32 bit = 1 << ep;
154 u32 daint;
155
156 if (!dir_in)
157 bit <<= 16;
158
159 local_irq_save(flags);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200160 daint = readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100161 if (en)
162 daint |= bit;
163 else
164 daint &= ~bit;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200165 writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100166 local_irq_restore(flags);
167}
168
169/**
170 * s3c_hsotg_init_fifo - initialise non-periodic FIFOs
171 * @hsotg: The device instance.
172 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600173static void s3c_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100174{
Ben Dooks0f002d22010-05-25 05:36:50 +0100175 unsigned int ep;
176 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100177 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100178 u32 val;
179
Gregory Herrero0a176272015-01-09 13:38:52 +0100180 /* set RX/NPTX FIFO sizes */
181 writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
182 writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
183 (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
184 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100185
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200186 /*
187 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100188 * block have overlapping default addresses. This also ensures
189 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200190 * known values.
191 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100192
193 /* start at the end of the GNPTXFSIZ, rounded up */
Gregory Herrero0a176272015-01-09 13:38:52 +0100194 addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
Ben Dooks0f002d22010-05-25 05:36:50 +0100195
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200196 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100197 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200198 * them to endpoints dynamically according to maxpacket size value of
199 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200200 */
Gregory Herrero0a176272015-01-09 13:38:52 +0100201 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
202 if (!hsotg->g_tx_fifo_sz[ep])
203 continue;
Robert Baldygab203d0a2014-09-09 10:44:56 +0200204 val = addr;
Gregory Herrero0a176272015-01-09 13:38:52 +0100205 val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
206 WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
Robert Baldygab203d0a2014-09-09 10:44:56 +0200207 "insufficient fifo memory");
Gregory Herrero0a176272015-01-09 13:38:52 +0100208 addr += hsotg->g_tx_fifo_sz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100209
Dinh Nguyen47a16852014-04-14 14:13:34 -0700210 writel(val, hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100211 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100212
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200213 /*
214 * according to p428 of the design guide, we need to ensure that
215 * all fifos are flushed before continuing
216 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100217
Dinh Nguyen47a16852014-04-14 14:13:34 -0700218 writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
219 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100220
221 /* wait until the fifos are both flushed */
222 timeout = 100;
223 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200224 val = readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100225
Dinh Nguyen47a16852014-04-14 14:13:34 -0700226 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100227 break;
228
229 if (--timeout == 0) {
230 dev_err(hsotg->dev,
231 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
232 __func__, val);
233 }
234
235 udelay(1);
236 }
237
238 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100239}
240
241/**
242 * @ep: USB endpoint to allocate request for.
243 * @flags: Allocation flags
244 *
245 * Allocate a new USB request structure appropriate for the specified endpoint
246 */
Mark Brown0978f8c2010-01-18 13:18:35 +0000247static struct usb_request *s3c_hsotg_ep_alloc_request(struct usb_ep *ep,
248 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100249{
250 struct s3c_hsotg_req *req;
251
252 req = kzalloc(sizeof(struct s3c_hsotg_req), flags);
253 if (!req)
254 return NULL;
255
256 INIT_LIST_HEAD(&req->queue);
257
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100258 return &req->req;
259}
260
261/**
262 * is_ep_periodic - return true if the endpoint is in periodic mode.
263 * @hs_ep: The endpoint to query.
264 *
265 * Returns true if the endpoint is in periodic mode, meaning it is being
266 * used for an Interrupt or ISO transfer.
267 */
268static inline int is_ep_periodic(struct s3c_hsotg_ep *hs_ep)
269{
270 return hs_ep->periodic;
271}
272
273/**
274 * s3c_hsotg_unmap_dma - unmap the DMA memory being used for the request
275 * @hsotg: The device state.
276 * @hs_ep: The endpoint for the request
277 * @hs_req: The request being processed.
278 *
279 * This is the reverse of s3c_hsotg_map_dma(), called for the completion
280 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200281 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600282static void s3c_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100283 struct s3c_hsotg_ep *hs_ep,
284 struct s3c_hsotg_req *hs_req)
285{
286 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100287
288 /* ignore this if we're not moving any data */
289 if (hs_req->req.length == 0)
290 return;
291
Jingoo Han17d966a2013-05-11 21:14:00 +0900292 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100293}
294
295/**
296 * s3c_hsotg_write_fifo - write packet Data to the TxFIFO
297 * @hsotg: The controller state.
298 * @hs_ep: The endpoint we're going to write for.
299 * @hs_req: The request to write data for.
300 *
301 * This is called when the TxFIFO has some space in it to hold a new
302 * transmission and we have something to give it. The actual setup of
303 * the data size is done elsewhere, so all we have to do is to actually
304 * write the data.
305 *
306 * The return value is zero if there is more space (or nothing was done)
307 * otherwise -ENOSPC is returned if the FIFO space was used up.
308 *
309 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200310 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600311static int s3c_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100312 struct s3c_hsotg_ep *hs_ep,
313 struct s3c_hsotg_req *hs_req)
314{
315 bool periodic = is_ep_periodic(hs_ep);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200316 u32 gnptxsts = readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100317 int buf_pos = hs_req->req.actual;
318 int to_write = hs_ep->size_loaded;
319 void *data;
320 int can_write;
321 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200322 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100323
324 to_write -= (buf_pos - hs_ep->last_load);
325
326 /* if there's nothing to write, get out early */
327 if (to_write == 0)
328 return 0;
329
Ben Dooks10aebc72010-07-19 09:40:44 +0100330 if (periodic && !hsotg->dedicated_fifos) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200331 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100332 int size_left;
333 int size_done;
334
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200335 /*
336 * work out how much data was loaded so we can calculate
337 * how much data is left in the fifo.
338 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100339
Dinh Nguyen47a16852014-04-14 14:13:34 -0700340 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100341
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200342 /*
343 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100344 * previous data has been completely sent.
345 */
346 if (hs_ep->fifo_load != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700347 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100348 return -ENOSPC;
349 }
350
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100351 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
352 __func__, size_left,
353 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
354
355 /* how much of the data has moved */
356 size_done = hs_ep->size_loaded - size_left;
357
358 /* how much data is left in the fifo */
359 can_write = hs_ep->fifo_load - size_done;
360 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
361 __func__, can_write);
362
363 can_write = hs_ep->fifo_size - can_write;
364 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
365 __func__, can_write);
366
367 if (can_write <= 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700368 s3c_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100369 return -ENOSPC;
370 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100371 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200372 can_write = readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100373
374 can_write &= 0xffff;
375 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100376 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700377 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100378 dev_dbg(hsotg->dev,
379 "%s: no queue slots available (0x%08x)\n",
380 __func__, gnptxsts);
381
Dinh Nguyen47a16852014-04-14 14:13:34 -0700382 s3c_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100383 return -ENOSPC;
384 }
385
Dinh Nguyen47a16852014-04-14 14:13:34 -0700386 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100387 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100388 }
389
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200390 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
391
392 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
393 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100394
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200395 /*
396 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100397 * FIFO, requests of >512 cause the endpoint to get stuck with a
398 * fragment of the end of the transfer in it.
399 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200400 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100401 can_write = 512;
402
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200403 /*
404 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100405 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200406 * doing it.
407 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200408 if (to_write > max_transfer) {
409 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100410
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200411 /* it's needed only when we do not use dedicated fifos */
412 if (!hsotg->dedicated_fifos)
413 s3c_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700414 periodic ? GINTSTS_PTXFEMP :
415 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100416 }
417
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100418 /* see if we can write data */
419
420 if (to_write > can_write) {
421 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200422 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100423
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200424 /*
425 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100426 * exact number of packets.
427 *
428 * Note, we do not currently check to see if we can ever
429 * write a full packet or not to the FIFO.
430 */
431
432 if (pkt_round)
433 to_write -= pkt_round;
434
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200435 /*
436 * enable correct FIFO interrupt to alert us when there
437 * is more room left.
438 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100439
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200440 /* it's needed only when we do not use dedicated fifos */
441 if (!hsotg->dedicated_fifos)
442 s3c_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700443 periodic ? GINTSTS_PTXFEMP :
444 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100445 }
446
447 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
448 to_write, hs_req->req.length, can_write, buf_pos);
449
450 if (to_write <= 0)
451 return -ENOSPC;
452
453 hs_req->req.actual = buf_pos + to_write;
454 hs_ep->total_data += to_write;
455
456 if (periodic)
457 hs_ep->fifo_load += to_write;
458
459 to_write = DIV_ROUND_UP(to_write, 4);
460 data = hs_req->req.buf + buf_pos;
461
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500462 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100463
464 return (to_write >= can_write) ? -ENOSPC : 0;
465}
466
467/**
468 * get_ep_limit - get the maximum data legnth for this endpoint
469 * @hs_ep: The endpoint
470 *
471 * Return the maximum data that can be queued in one go on a given endpoint
472 * so that transfers that are too long can be split.
473 */
474static unsigned get_ep_limit(struct s3c_hsotg_ep *hs_ep)
475{
476 int index = hs_ep->index;
477 unsigned maxsize;
478 unsigned maxpkt;
479
480 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700481 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
482 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100483 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100484 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900485 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700486 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900487 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100488 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100489 }
490
491 /* we made the constant loading easier above by using +1 */
492 maxpkt--;
493 maxsize--;
494
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200495 /*
496 * constrain by packet count if maxpkts*pktsize is greater
497 * than the length register size.
498 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100499
500 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
501 maxsize = maxpkt * hs_ep->ep.maxpacket;
502
503 return maxsize;
504}
505
506/**
507 * s3c_hsotg_start_req - start a USB request from an endpoint's queue
508 * @hsotg: The controller state.
509 * @hs_ep: The endpoint to process a request for
510 * @hs_req: The request to start.
511 * @continuing: True if we are doing more for the current request.
512 *
513 * Start the given request running by setting the endpoint registers
514 * appropriately, and writing any data to the FIFOs.
515 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600516static void s3c_hsotg_start_req(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100517 struct s3c_hsotg_ep *hs_ep,
518 struct s3c_hsotg_req *hs_req,
519 bool continuing)
520{
521 struct usb_request *ureq = &hs_req->req;
522 int index = hs_ep->index;
523 int dir_in = hs_ep->dir_in;
524 u32 epctrl_reg;
525 u32 epsize_reg;
526 u32 epsize;
527 u32 ctrl;
528 unsigned length;
529 unsigned packets;
530 unsigned maxreq;
531
532 if (index != 0) {
533 if (hs_ep->req && !continuing) {
534 dev_err(hsotg->dev, "%s: active request\n", __func__);
535 WARN_ON(1);
536 return;
537 } else if (hs_ep->req != hs_req && continuing) {
538 dev_err(hsotg->dev,
539 "%s: continue different req\n", __func__);
540 WARN_ON(1);
541 return;
542 }
543 }
544
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200545 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
546 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100547
548 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
549 __func__, readl(hsotg->regs + epctrl_reg), index,
550 hs_ep->dir_in ? "in" : "out");
551
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900552 /* If endpoint is stalled, we will restart request later */
553 ctrl = readl(hsotg->regs + epctrl_reg);
554
Dinh Nguyen47a16852014-04-14 14:13:34 -0700555 if (ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900556 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
557 return;
558 }
559
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100560 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200561 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
562 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100563 if (0)
564 dev_dbg(hsotg->dev,
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300565 "REQ buf %p len %d dma %pad noi=%d zp=%d snok=%d\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900566 ureq->buf, length, &ureq->dma,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100567 ureq->no_interrupt, ureq->zero, ureq->short_not_ok);
568
569 maxreq = get_ep_limit(hs_ep);
570 if (length > maxreq) {
571 int round = maxreq % hs_ep->ep.maxpacket;
572
573 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
574 __func__, length, maxreq, round);
575
576 /* round down to multiple of packets */
577 if (round)
578 maxreq -= round;
579
580 length = maxreq;
581 }
582
583 if (length)
584 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
585 else
586 packets = 1; /* send one packet if length is zero. */
587
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200588 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
589 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
590 return;
591 }
592
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100593 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200594 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700595 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200596 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700597 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100598 else
599 epsize = 0;
600
601 if (index != 0 && ureq->zero) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200602 /*
603 * test for the packets being exactly right for the
604 * transfer
605 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100606
607 if (length == (packets * hs_ep->ep.maxpacket))
608 packets++;
609 }
610
Dinh Nguyen47a16852014-04-14 14:13:34 -0700611 epsize |= DXEPTSIZ_PKTCNT(packets);
612 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100613
614 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
615 __func__, packets, length, ureq->length, epsize, epsize_reg);
616
617 /* store the request as the current one we're doing */
618 hs_ep->req = hs_req;
619
620 /* write size / packets */
621 writel(epsize, hsotg->regs + epsize_reg);
622
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900623 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100624 unsigned int dma_reg;
625
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200626 /*
627 * write DMA address to control register, buffer already
628 * synced by s3c_hsotg_ep_queue().
629 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100630
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200631 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100632 writel(ureq->dma, hsotg->regs + dma_reg);
633
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300634 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900635 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100636 }
637
Dinh Nguyen47a16852014-04-14 14:13:34 -0700638 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
639 ctrl |= DXEPCTL_USBACTEP;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200640
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100641 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +0200642
643 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100644 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -0700645 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200646
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100647 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
648 writel(ctrl, hsotg->regs + epctrl_reg);
649
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200650 /*
651 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100652 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200653 * this information.
654 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100655 hs_ep->size_loaded = length;
656 hs_ep->last_load = ureq->actual;
657
658 if (dir_in && !using_dma(hsotg)) {
659 /* set these anyway, we may need them for non-periodic in */
660 hs_ep->fifo_load = 0;
661
662 s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
663 }
664
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200665 /*
666 * clear the INTknTXFEmpMsk when we start request, more as a aide
667 * to debugging to see what is going on.
668 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100669 if (dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700670 writel(DIEPMSK_INTKNTXFEMPMSK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200671 hsotg->regs + DIEPINT(index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100672
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200673 /*
674 * Note, trying to clear the NAK here causes problems with transmit
675 * on the S3C6400 ending up with the TXFIFO becoming full.
676 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100677
678 /* check ep is enabled */
Dinh Nguyen47a16852014-04-14 14:13:34 -0700679 if (!(readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100680 dev_warn(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700681 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100682 index, readl(hsotg->regs + epctrl_reg));
683
Dinh Nguyen47a16852014-04-14 14:13:34 -0700684 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100685 __func__, readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200686
687 /* enable ep interrupts */
688 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100689}
690
691/**
692 * s3c_hsotg_map_dma - map the DMA memory being used for the request
693 * @hsotg: The device state.
694 * @hs_ep: The endpoint the request is on.
695 * @req: The request being processed.
696 *
697 * We've been asked to queue a request, so ensure that the memory buffer
698 * is correctly setup for DMA. If we've been passed an extant DMA address
699 * then ensure the buffer has been synced to memory. If our buffer has no
700 * DMA memory, then we map the memory and mark our request to allow us to
701 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200702 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600703static int s3c_hsotg_map_dma(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100704 struct s3c_hsotg_ep *hs_ep,
705 struct usb_request *req)
706{
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100707 struct s3c_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200708 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100709
710 /* if the length is zero, ignore the DMA data */
711 if (hs_req->req.length == 0)
712 return 0;
713
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200714 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
715 if (ret)
716 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100717
718 return 0;
719
720dma_error:
721 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
722 __func__, req->buf, req->length);
723
724 return -EIO;
725}
726
727static int s3c_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
728 gfp_t gfp_flags)
729{
730 struct s3c_hsotg_req *hs_req = our_req(req);
731 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600732 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100733 bool first;
734
735 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
736 ep->name, req, req->length, req->buf, req->no_interrupt,
737 req->zero, req->short_not_ok);
738
739 /* initialise status of the request */
740 INIT_LIST_HEAD(&hs_req->queue);
741 req->actual = 0;
742 req->status = -EINPROGRESS;
743
744 /* if we're using DMA, sync the buffers as necessary */
745 if (using_dma(hs)) {
746 int ret = s3c_hsotg_map_dma(hs, hs_ep, req);
747 if (ret)
748 return ret;
749 }
750
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100751 first = list_empty(&hs_ep->queue);
752 list_add_tail(&hs_req->queue, &hs_ep->queue);
753
754 if (first)
755 s3c_hsotg_start_req(hs, hs_ep, hs_req, false);
756
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100757 return 0;
758}
759
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200760static int s3c_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
761 gfp_t gfp_flags)
762{
763 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600764 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200765 unsigned long flags = 0;
766 int ret = 0;
767
768 spin_lock_irqsave(&hs->lock, flags);
769 ret = s3c_hsotg_ep_queue(ep, req, gfp_flags);
770 spin_unlock_irqrestore(&hs->lock, flags);
771
772 return ret;
773}
774
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100775static void s3c_hsotg_ep_free_request(struct usb_ep *ep,
776 struct usb_request *req)
777{
778 struct s3c_hsotg_req *hs_req = our_req(req);
779
780 kfree(hs_req);
781}
782
783/**
784 * s3c_hsotg_complete_oursetup - setup completion callback
785 * @ep: The endpoint the request was on.
786 * @req: The request completed.
787 *
788 * Called on completion of any requests the driver itself
789 * submitted that need cleaning up.
790 */
791static void s3c_hsotg_complete_oursetup(struct usb_ep *ep,
792 struct usb_request *req)
793{
794 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600795 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100796
797 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
798
799 s3c_hsotg_ep_free_request(ep, req);
800}
801
802/**
803 * ep_from_windex - convert control wIndex value to endpoint
804 * @hsotg: The driver state.
805 * @windex: The control request wIndex field (in host order).
806 *
807 * Convert the given wIndex into a pointer to an driver endpoint
808 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200809 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600810static struct s3c_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100811 u32 windex)
812{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100813 struct s3c_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100814 int dir = (windex & USB_DIR_IN) ? 1 : 0;
815 int idx = windex & 0x7F;
816
817 if (windex >= 0x100)
818 return NULL;
819
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200820 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100821 return NULL;
822
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100823 ep = index_to_ep(hsotg, idx, dir);
824
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100825 if (idx && ep->dir_in != dir)
826 return NULL;
827
828 return ep;
829}
830
831/**
832 * s3c_hsotg_send_reply - send reply to control request
833 * @hsotg: The device state
834 * @ep: Endpoint 0
835 * @buff: Buffer for request
836 * @length: Length of reply.
837 *
838 * Create a request and queue it on the given endpoint. This is useful as
839 * an internal method of sending replies to certain control requests, etc.
840 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600841static int s3c_hsotg_send_reply(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100842 struct s3c_hsotg_ep *ep,
843 void *buff,
844 int length)
845{
846 struct usb_request *req;
847 int ret;
848
849 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
850
851 req = s3c_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
852 hsotg->ep0_reply = req;
853 if (!req) {
854 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
855 return -ENOMEM;
856 }
857
858 req->buf = hsotg->ep0_buff;
859 req->length = length;
860 req->zero = 1; /* always do zero-length final transfer */
861 req->complete = s3c_hsotg_complete_oursetup;
862
863 if (length)
864 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100865
866 ret = s3c_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
867 if (ret) {
868 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
869 return ret;
870 }
871
872 return 0;
873}
874
875/**
876 * s3c_hsotg_process_req_status - process request GET_STATUS
877 * @hsotg: The device state
878 * @ctrl: USB control request
879 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600880static int s3c_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100881 struct usb_ctrlrequest *ctrl)
882{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100883 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100884 struct s3c_hsotg_ep *ep;
885 __le16 reply;
886 int ret;
887
888 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
889
890 if (!ep0->dir_in) {
891 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
892 return -EINVAL;
893 }
894
895 switch (ctrl->bRequestType & USB_RECIP_MASK) {
896 case USB_RECIP_DEVICE:
897 reply = cpu_to_le16(0); /* bit 0 => self powered,
898 * bit 1 => remote wakeup */
899 break;
900
901 case USB_RECIP_INTERFACE:
902 /* currently, the data result should be zero */
903 reply = cpu_to_le16(0);
904 break;
905
906 case USB_RECIP_ENDPOINT:
907 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
908 if (!ep)
909 return -ENOENT;
910
911 reply = cpu_to_le16(ep->halted ? 1 : 0);
912 break;
913
914 default:
915 return 0;
916 }
917
918 if (le16_to_cpu(ctrl->wLength) != 2)
919 return -EINVAL;
920
921 ret = s3c_hsotg_send_reply(hsotg, ep0, &reply, 2);
922 if (ret) {
923 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
924 return ret;
925 }
926
927 return 1;
928}
929
930static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value);
931
932/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900933 * get_ep_head - return the first request on the endpoint
934 * @hs_ep: The controller endpoint to get
935 *
936 * Get the first request on the endpoint.
937 */
938static struct s3c_hsotg_req *get_ep_head(struct s3c_hsotg_ep *hs_ep)
939{
940 if (list_empty(&hs_ep->queue))
941 return NULL;
942
943 return list_first_entry(&hs_ep->queue, struct s3c_hsotg_req, queue);
944}
945
946/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100947 * s3c_hsotg_process_req_featire - process request {SET,CLEAR}_FEATURE
948 * @hsotg: The device state
949 * @ctrl: USB control request
950 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600951static int s3c_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100952 struct usb_ctrlrequest *ctrl)
953{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100954 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900955 struct s3c_hsotg_req *hs_req;
956 bool restart;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100957 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
958 struct s3c_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +0900959 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200960 bool halted;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100961
962 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
963 __func__, set ? "SET" : "CLEAR");
964
965 if (ctrl->bRequestType == USB_RECIP_ENDPOINT) {
966 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
967 if (!ep) {
968 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
969 __func__, le16_to_cpu(ctrl->wIndex));
970 return -ENOENT;
971 }
972
973 switch (le16_to_cpu(ctrl->wValue)) {
974 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200975 halted = ep->halted;
976
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100977 s3c_hsotg_ep_sethalt(&ep->ep, set);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +0900978
979 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
980 if (ret) {
981 dev_err(hsotg->dev,
982 "%s: failed to send reply\n", __func__);
983 return ret;
984 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900985
Robert Baldygabd9ef7b2013-09-19 11:50:22 +0200986 /*
987 * we have to complete all requests for ep if it was
988 * halted, and the halt was cleared by CLEAR_FEATURE
989 */
990
991 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900992 /*
993 * If we have request in progress,
994 * then complete it
995 */
996 if (ep->req) {
997 hs_req = ep->req;
998 ep->req = NULL;
999 list_del_init(&hs_req->queue);
Michal Sojka304f7e52014-09-24 22:43:19 +02001000 usb_gadget_giveback_request(&ep->ep,
1001 &hs_req->req);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001002 }
1003
1004 /* If we have pending request, then start it */
1005 restart = !list_empty(&ep->queue);
1006 if (restart) {
1007 hs_req = get_ep_head(ep);
1008 s3c_hsotg_start_req(hsotg, ep,
1009 hs_req, false);
1010 }
1011 }
1012
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001013 break;
1014
1015 default:
1016 return -ENOENT;
1017 }
1018 } else
1019 return -ENOENT; /* currently only deal with endpoint */
1020
1021 return 1;
1022}
1023
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001024static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001025
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001026/**
Robert Baldygac9f721b2014-01-14 08:36:00 +01001027 * s3c_hsotg_stall_ep0 - stall ep0
1028 * @hsotg: The device state
1029 *
1030 * Set stall for ep0 as response for setup request.
1031 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001032static void s3c_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001033{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001034 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001035 u32 reg;
1036 u32 ctrl;
1037
1038 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1039 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1040
1041 /*
1042 * DxEPCTL_Stall will be cleared by EP once it has
1043 * taken effect, so no need to clear later.
1044 */
1045
1046 ctrl = readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001047 ctrl |= DXEPCTL_STALL;
1048 ctrl |= DXEPCTL_CNAK;
Robert Baldygac9f721b2014-01-14 08:36:00 +01001049 writel(ctrl, hsotg->regs + reg);
1050
1051 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001052 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Robert Baldygac9f721b2014-01-14 08:36:00 +01001053 ctrl, reg, readl(hsotg->regs + reg));
1054
1055 /*
1056 * complete won't be called, so we enqueue
1057 * setup request here
1058 */
1059 s3c_hsotg_enqueue_setup(hsotg);
1060}
1061
1062/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001063 * s3c_hsotg_process_control - process a control request
1064 * @hsotg: The device state
1065 * @ctrl: The control request received
1066 *
1067 * The controller has received the SETUP phase of a control request, and
1068 * needs to work out what to do next (and whether to pass it on to the
1069 * gadget driver).
1070 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001071static void s3c_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001072 struct usb_ctrlrequest *ctrl)
1073{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001074 struct s3c_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001075 int ret = 0;
1076 u32 dcfg;
1077
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001078 dev_dbg(hsotg->dev, "ctrl Req=%02x, Type=%02x, V=%04x, L=%04x\n",
1079 ctrl->bRequest, ctrl->bRequestType,
1080 ctrl->wValue, ctrl->wLength);
1081
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001082 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001083 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001084 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1085 } else if (ctrl->bRequestType & USB_DIR_IN) {
1086 ep0->dir_in = 1;
1087 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1088 } else {
1089 ep0->dir_in = 0;
1090 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1091 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001092
1093 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1094 switch (ctrl->bRequest) {
1095 case USB_REQ_SET_ADDRESS:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001096 dcfg = readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001097 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001098 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1099 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001100 writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001101
1102 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1103
1104 ret = s3c_hsotg_send_reply(hsotg, ep0, NULL, 0);
1105 return;
1106
1107 case USB_REQ_GET_STATUS:
1108 ret = s3c_hsotg_process_req_status(hsotg, ctrl);
1109 break;
1110
1111 case USB_REQ_CLEAR_FEATURE:
1112 case USB_REQ_SET_FEATURE:
1113 ret = s3c_hsotg_process_req_feature(hsotg, ctrl);
1114 break;
1115 }
1116 }
1117
1118 /* as a fallback, try delivering it to the driver to deal with */
1119
1120 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001121 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001123 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001124 if (ret < 0)
1125 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1126 }
1127
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001128 /*
1129 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001130 * so respond with a STALL for the status stage to indicate failure.
1131 */
1132
Robert Baldygac9f721b2014-01-14 08:36:00 +01001133 if (ret < 0)
1134 s3c_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001135}
1136
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001137/**
1138 * s3c_hsotg_complete_setup - completion of a setup transfer
1139 * @ep: The endpoint the request was on.
1140 * @req: The request completed.
1141 *
1142 * Called on completion of any requests the driver itself submitted for
1143 * EP0 setup packets
1144 */
1145static void s3c_hsotg_complete_setup(struct usb_ep *ep,
1146 struct usb_request *req)
1147{
1148 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001149 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001150
1151 if (req->status < 0) {
1152 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1153 return;
1154 }
1155
Robert Baldyga93f599f2013-11-21 13:49:17 +01001156 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001157 if (req->actual == 0)
1158 s3c_hsotg_enqueue_setup(hsotg);
1159 else
1160 s3c_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001161 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001162}
1163
1164/**
1165 * s3c_hsotg_enqueue_setup - start a request for EP0 packets
1166 * @hsotg: The device state.
1167 *
1168 * Enqueue a request on EP0 if necessary to received any SETUP packets
1169 * received from the host.
1170 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001171static void s3c_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001172{
1173 struct usb_request *req = hsotg->ctrl_req;
1174 struct s3c_hsotg_req *hs_req = our_req(req);
1175 int ret;
1176
1177 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1178
1179 req->zero = 0;
1180 req->length = 8;
1181 req->buf = hsotg->ctrl_buff;
1182 req->complete = s3c_hsotg_complete_setup;
1183
1184 if (!list_empty(&hs_req->queue)) {
1185 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1186 return;
1187 }
1188
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001189 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001190 hsotg->eps_out[0]->sent_zlp = 0;
1191 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001192
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001193 ret = s3c_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001194 if (ret < 0) {
1195 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001196 /*
1197 * Don't think there's much we can do other than watch the
1198 * driver fail.
1199 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001200 }
1201}
1202
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001203static void s3c_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1204 struct s3c_hsotg_ep *hs_ep)
1205{
1206 u32 ctrl;
1207 u8 index = hs_ep->index;
1208 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1209 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1210
1211 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n", index);
1212
1213 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1214 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1215 epsiz_reg);
1216
1217 ctrl = readl(hsotg->regs + epctl_reg);
1218 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1219 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1220 ctrl |= DXEPCTL_USBACTEP;
1221 writel(ctrl, hsotg->regs + epctl_reg);
1222}
1223
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001224/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001225 * s3c_hsotg_complete_request - complete a request given to us
1226 * @hsotg: The device state.
1227 * @hs_ep: The endpoint the request was on.
1228 * @hs_req: The request to complete.
1229 * @result: The result code (0 => Ok, otherwise errno)
1230 *
1231 * The given request has finished, so call the necessary completion
1232 * if it has one and then look to see if we can start a new request
1233 * on the endpoint.
1234 *
1235 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001236 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001237static void s3c_hsotg_complete_request(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001238 struct s3c_hsotg_ep *hs_ep,
1239 struct s3c_hsotg_req *hs_req,
1240 int result)
1241{
1242 bool restart;
1243
1244 if (!hs_req) {
1245 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1246 return;
1247 }
1248
1249 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1250 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1251
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001252 /*
1253 * only replace the status if we've not already set an error
1254 * from a previous transaction
1255 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001256
1257 if (hs_req->req.status == -EINPROGRESS)
1258 hs_req->req.status = result;
1259
1260 hs_ep->req = NULL;
1261 list_del_init(&hs_req->queue);
1262
1263 if (using_dma(hsotg))
1264 s3c_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1265
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001266 /*
1267 * call the complete request with the locks off, just in case the
1268 * request tries to queue more work for this endpoint.
1269 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001270
1271 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001272 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001273 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001274 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001275 }
1276
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001277 /*
1278 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001279 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001280 * so be careful when doing this.
1281 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001282
1283 if (!hs_ep->req && result >= 0) {
1284 restart = !list_empty(&hs_ep->queue);
1285 if (restart) {
1286 hs_req = get_ep_head(hs_ep);
1287 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1288 }
1289 }
1290}
1291
1292/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001293 * s3c_hsotg_rx_data - receive data from the FIFO for an endpoint
1294 * @hsotg: The device state.
1295 * @ep_idx: The endpoint index for the data
1296 * @size: The size of data in the fifo, in bytes
1297 *
1298 * The FIFO status shows there is data to read from the FIFO for a given
1299 * endpoint, so sort out whether we need to read the data into a request
1300 * that has been made for that endpoint.
1301 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001302static void s3c_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001303{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001304 struct s3c_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001305 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001306 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001307 int to_read;
1308 int max_req;
1309 int read_ptr;
1310
Lukasz Majewski22258f42012-06-14 10:02:24 +02001311
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001312 if (!hs_req) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001313 u32 epctl = readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001314 int ptr;
1315
Robert Baldyga6b448af2014-12-16 11:51:44 +01001316 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001317 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001318 __func__, size, ep_idx, epctl);
1319
1320 /* dump the data from the FIFO, we've nothing we can do */
1321 for (ptr = 0; ptr < size; ptr += 4)
1322 (void)readl(fifo);
1323
1324 return;
1325 }
1326
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001327 to_read = size;
1328 read_ptr = hs_req->req.actual;
1329 max_req = hs_req->req.length - read_ptr;
1330
Ben Dooksa33e7132010-07-19 09:40:49 +01001331 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1332 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1333
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001334 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001335 /*
1336 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001337 * to deal with in this request.
1338 */
1339
1340 /* currently we don't deal this */
1341 WARN_ON_ONCE(1);
1342 }
1343
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001344 hs_ep->total_data += to_read;
1345 hs_req->req.actual += to_read;
1346 to_read = DIV_ROUND_UP(to_read, 4);
1347
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001348 /*
1349 * note, we might over-write the buffer end by 3 bytes depending on
1350 * alignment of the data.
1351 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001352 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001353}
1354
1355/**
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001356 * s3c_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001357 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001358 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001359 *
1360 * Generate a zero-length IN packet request for terminating a SETUP
1361 * transaction.
1362 *
1363 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001364 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001365 * the TxFIFO.
1366 */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001367static void s3c_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001368{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001369 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001370 hsotg->eps_out[0]->dir_in = dir_in;
1371 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001372
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001373 s3c_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001374}
1375
1376/**
1377 * s3c_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
1378 * @hsotg: The device instance
1379 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001380 *
1381 * The RXFIFO has delivered an OutDone event, which means that the data
1382 * transfer for an OUT endpoint has been completed, either by a short
1383 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001384 */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001385static void s3c_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001386{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001387 u32 epsize = readl(hsotg->regs + DOEPTSIZ(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001388 struct s3c_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001389 struct s3c_hsotg_req *hs_req = hs_ep->req;
1390 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001391 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001392 int result = 0;
1393
1394 if (!hs_req) {
1395 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1396 return;
1397 }
1398
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001399 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1400 dev_dbg(hsotg->dev, "zlp packet received\n");
1401 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1402 s3c_hsotg_enqueue_setup(hsotg);
1403 return;
1404 }
1405
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001406 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001407 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001408
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001409 /*
1410 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001411 * is left in the endpoint size register and then working it
1412 * out from the amount we loaded for the transfer.
1413 *
1414 * We need to do this as DMA pointers are always 32bit aligned
1415 * so may overshoot/undershoot the transfer.
1416 */
1417
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001418 size_done = hs_ep->size_loaded - size_left;
1419 size_done += hs_ep->last_load;
1420
1421 req->actual = size_done;
1422 }
1423
Ben Dooksa33e7132010-07-19 09:40:49 +01001424 /* if there is more request to do, schedule new transfer */
1425 if (req->actual < req->length && size_left == 0) {
1426 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
1427 return;
1428 }
1429
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001430 if (req->actual < req->length && req->short_not_ok) {
1431 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1432 __func__, req->actual, req->length);
1433
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001434 /*
1435 * todo - what should we return here? there's no one else
1436 * even bothering to check the status.
1437 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001438 }
1439
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001440 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1441 /* Move to STATUS IN */
1442 s3c_hsotg_ep0_zlp(hsotg, true);
1443 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001444 }
1445
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001446 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001447}
1448
1449/**
1450 * s3c_hsotg_read_frameno - read current frame number
1451 * @hsotg: The device instance
1452 *
1453 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001454 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001455static u32 s3c_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001456{
1457 u32 dsts;
1458
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001459 dsts = readl(hsotg->regs + DSTS);
1460 dsts &= DSTS_SOFFN_MASK;
1461 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001462
1463 return dsts;
1464}
1465
1466/**
1467 * s3c_hsotg_handle_rx - RX FIFO has data
1468 * @hsotg: The device instance
1469 *
1470 * The IRQ handler has detected that the RX FIFO has some data in it
1471 * that requires processing, so find out what is in there and do the
1472 * appropriate read.
1473 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001474 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001475 * chunks, so if you have x packets received on an endpoint you'll get x
1476 * FIFO events delivered, each with a packet's worth of data in it.
1477 *
1478 * When using DMA, we should not be processing events from the RXFIFO
1479 * as the actual data should be sent to the memory directly and we turn
1480 * on the completion interrupts to get notifications of transfer completion.
1481 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001482static void s3c_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001483{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001484 u32 grxstsr = readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001485 u32 epnum, status, size;
1486
1487 WARN_ON(using_dma(hsotg));
1488
Dinh Nguyen47a16852014-04-14 14:13:34 -07001489 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1490 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001491
Dinh Nguyen47a16852014-04-14 14:13:34 -07001492 size = grxstsr & GRXSTS_BYTECNT_MASK;
1493 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001494
1495 if (1)
1496 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
1497 __func__, grxstsr, size, epnum);
1498
Dinh Nguyen47a16852014-04-14 14:13:34 -07001499 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1500 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1501 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001502 break;
1503
Dinh Nguyen47a16852014-04-14 14:13:34 -07001504 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001505 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
1506 s3c_hsotg_read_frameno(hsotg));
1507
1508 if (!using_dma(hsotg))
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001509 s3c_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001510 break;
1511
Dinh Nguyen47a16852014-04-14 14:13:34 -07001512 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001513 dev_dbg(hsotg->dev,
1514 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1515 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001516 readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001517 /*
1518 * Call s3c_hsotg_handle_outdone here if it was not called from
1519 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1520 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1521 */
1522 if (hsotg->ep0_state == DWC2_EP0_SETUP)
1523 s3c_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001524 break;
1525
Dinh Nguyen47a16852014-04-14 14:13:34 -07001526 case GRXSTS_PKTSTS_OUTRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001527 s3c_hsotg_rx_data(hsotg, epnum, size);
1528 break;
1529
Dinh Nguyen47a16852014-04-14 14:13:34 -07001530 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001531 dev_dbg(hsotg->dev,
1532 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
1533 s3c_hsotg_read_frameno(hsotg),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001534 readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001535
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001536 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1537
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001538 s3c_hsotg_rx_data(hsotg, epnum, size);
1539 break;
1540
1541 default:
1542 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1543 __func__, grxstsr);
1544
1545 s3c_hsotg_dump(hsotg);
1546 break;
1547 }
1548}
1549
1550/**
1551 * s3c_hsotg_ep0_mps - turn max packet size into register setting
1552 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001553 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001554static u32 s3c_hsotg_ep0_mps(unsigned int mps)
1555{
1556 switch (mps) {
1557 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001558 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001559 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001560 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001561 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001562 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001563 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001564 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001565 }
1566
1567 /* bad max packet size, warn and return invalid result */
1568 WARN_ON(1);
1569 return (u32)-1;
1570}
1571
1572/**
1573 * s3c_hsotg_set_ep_maxpacket - set endpoint's max-packet field
1574 * @hsotg: The driver state.
1575 * @ep: The index number of the endpoint
1576 * @mps: The maximum packet size in bytes
1577 *
1578 * Configure the maximum packet size for the given endpoint, updating
1579 * the hardware control registers to reflect this.
1580 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001581static void s3c_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001582 unsigned int ep, unsigned int mps, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001583{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001584 struct s3c_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001585 void __iomem *regs = hsotg->regs;
1586 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001587 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001588 u32 reg;
1589
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001590 hs_ep = index_to_ep(hsotg, ep, dir_in);
1591 if (!hs_ep)
1592 return;
1593
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001594 if (ep == 0) {
1595 /* EP0 is a special case */
1596 mpsval = s3c_hsotg_ep0_mps(mps);
1597 if (mpsval > 3)
1598 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001599 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001600 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001601 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001602 mpsval = mps & DXEPCTL_MPS_MASK;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001603 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001604 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001605 mcval = ((mps >> 11) & 0x3) + 1;
1606 hs_ep->mc = mcval;
1607 if (mcval > 3)
1608 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001609 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001610 }
1611
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001612 if (dir_in) {
1613 reg = readl(regs + DIEPCTL(ep));
1614 reg &= ~DXEPCTL_MPS_MASK;
1615 reg |= mpsval;
1616 writel(reg, regs + DIEPCTL(ep));
1617 } else {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001618 reg = readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001619 reg &= ~DXEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001620 reg |= mpsval;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001621 writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001622 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001623
1624 return;
1625
1626bad_mps:
1627 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1628}
1629
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001630/**
1631 * s3c_hsotg_txfifo_flush - flush Tx FIFO
1632 * @hsotg: The driver state
1633 * @idx: The index for the endpoint (0..15)
1634 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001635static void s3c_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001636{
1637 int timeout;
1638 int val;
1639
Dinh Nguyen47a16852014-04-14 14:13:34 -07001640 writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001641 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001642
1643 /* wait until the fifo is flushed */
1644 timeout = 100;
1645
1646 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001647 val = readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001648
Dinh Nguyen47a16852014-04-14 14:13:34 -07001649 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001650 break;
1651
1652 if (--timeout == 0) {
1653 dev_err(hsotg->dev,
1654 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1655 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02001656 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001657 }
1658
1659 udelay(1);
1660 }
1661}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001662
1663/**
1664 * s3c_hsotg_trytx - check to see if anything needs transmitting
1665 * @hsotg: The driver state
1666 * @hs_ep: The driver endpoint to check.
1667 *
1668 * Check to see if there is a request that has data to send, and if so
1669 * make an attempt to write data into the FIFO.
1670 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001671static int s3c_hsotg_trytx(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001672 struct s3c_hsotg_ep *hs_ep)
1673{
1674 struct s3c_hsotg_req *hs_req = hs_ep->req;
1675
Robert Baldygaafcf4162013-09-19 11:50:19 +02001676 if (!hs_ep->dir_in || !hs_req) {
1677 /**
1678 * if request is not enqueued, we disable interrupts
1679 * for endpoints, excepting ep0
1680 */
1681 if (hs_ep->index != 0)
1682 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index,
1683 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001684 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001685 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001686
1687 if (hs_req->req.actual < hs_req->req.length) {
1688 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1689 hs_ep->index);
1690 return s3c_hsotg_write_fifo(hsotg, hs_ep, hs_req);
1691 }
1692
1693 return 0;
1694}
1695
1696/**
1697 * s3c_hsotg_complete_in - complete IN transfer
1698 * @hsotg: The device state.
1699 * @hs_ep: The endpoint that has just completed.
1700 *
1701 * An IN transfer has been completed, update the transfer's state and then
1702 * call the relevant completion routines.
1703 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001704static void s3c_hsotg_complete_in(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001705 struct s3c_hsotg_ep *hs_ep)
1706{
1707 struct s3c_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001708 u32 epsize = readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001709 int size_left, size_done;
1710
1711 if (!hs_req) {
1712 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1713 return;
1714 }
1715
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001716 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001717 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
1718 dev_dbg(hsotg->dev, "zlp packet sent\n");
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001719 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001720 s3c_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001721 return;
1722 }
1723
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001724 /*
1725 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001726 * in the endpoint size register and then working it out from
1727 * the amount we loaded for the transfer.
1728 *
1729 * We do this even for DMA, as the transfer may have incremented
1730 * past the end of the buffer (DMA transfers are always 32bit
1731 * aligned).
1732 */
1733
Dinh Nguyen47a16852014-04-14 14:13:34 -07001734 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001735
1736 size_done = hs_ep->size_loaded - size_left;
1737 size_done += hs_ep->last_load;
1738
1739 if (hs_req->req.actual != size_done)
1740 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1741 __func__, hs_req->req.actual, size_done);
1742
1743 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001744 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1745 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001746
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001747 /*
1748 * Check if dealing with Maximum Packet Size(MPS) IN transfer at EP0
1749 * When sent data is a multiple MPS size (e.g. 64B ,128B ,192B
1750 * ,256B ... ), after last MPS sized packet send IN ZLP packet to
1751 * inform the host that no more data is available.
1752 * The state of req.zero member is checked to be sure that the value to
1753 * send is smaller than wValue expected from host.
1754 * Check req.length to NOT send another ZLP when the current one is
1755 * under completion (the one for which this completion has been called).
1756 */
1757 if (hs_req->req.length && hs_ep->index == 0 && hs_req->req.zero &&
1758 hs_req->req.length == hs_req->req.actual &&
1759 !(hs_req->req.length % hs_ep->ep.maxpacket)) {
1760
1761 dev_dbg(hsotg->dev, "ep0 zlp IN packet sent\n");
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001762 s3c_hsotg_program_zlp(hsotg, hs_ep);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001763
1764 return;
1765 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001766
1767 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1768 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
1769 s3c_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001770 return;
1771 }
1772
1773 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
1774 /* Move to STATUS OUT */
1775 s3c_hsotg_ep0_zlp(hsotg, false);
1776 return;
1777 }
1778
1779 s3c_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001780}
1781
1782/**
1783 * s3c_hsotg_epint - handle an in/out endpoint interrupt
1784 * @hsotg: The driver state
1785 * @idx: The index for the endpoint (0..15)
1786 * @dir_in: Set if this is an IN endpoint
1787 *
1788 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001789 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001790static void s3c_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001791 int dir_in)
1792{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001793 struct s3c_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001794 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1795 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
1796 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001797 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02001798 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001799
1800 ints = readl(hsotg->regs + epint_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02001801 ctrl = readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001802
Anton Tikhomirova3395f02011-04-21 17:06:39 +09001803 /* Clear endpoint interrupts */
1804 writel(ints, hsotg->regs + epint_reg);
1805
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001806 if (!hs_ep) {
1807 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
1808 __func__, idx, dir_in ? "in" : "out");
1809 return;
1810 }
1811
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001812 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
1813 __func__, idx, dir_in ? "in" : "out", ints);
1814
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01001815 /* Don't process XferCompl interrupt if it is a setup packet */
1816 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
1817 ints &= ~DXEPINT_XFERCOMPL;
1818
Dinh Nguyen47a16852014-04-14 14:13:34 -07001819 if (ints & DXEPINT_XFERCOMPL) {
Robert Baldyga1479e842013-10-09 08:41:57 +02001820 if (hs_ep->isochronous && hs_ep->interval == 1) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001821 if (ctrl & DXEPCTL_EOFRNUM)
1822 ctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001823 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001824 ctrl |= DXEPCTL_SETODDFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02001825 writel(ctrl, hsotg->regs + epctl_reg);
1826 }
1827
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001828 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001829 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001830 __func__, readl(hsotg->regs + epctl_reg),
1831 readl(hsotg->regs + epsiz_reg));
1832
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001833 /*
1834 * we get OutDone from the FIFO, so we only need to look
1835 * at completing IN requests here
1836 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001837 if (dir_in) {
1838 s3c_hsotg_complete_in(hsotg, hs_ep);
1839
Ben Dooksc9a64ea2010-07-19 09:40:46 +01001840 if (idx == 0 && !hs_ep->req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001841 s3c_hsotg_enqueue_setup(hsotg);
1842 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001843 /*
1844 * We're using DMA, we need to fire an OutDone here
1845 * as we ignore the RXFIFO.
1846 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001847
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001848 s3c_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001849 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001850 }
1851
Dinh Nguyen47a16852014-04-14 14:13:34 -07001852 if (ints & DXEPINT_EPDISBLD) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001853 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001854
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001855 if (dir_in) {
1856 int epctl = readl(hsotg->regs + epctl_reg);
1857
Robert Baldygab203d0a2014-09-09 10:44:56 +02001858 s3c_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001859
Dinh Nguyen47a16852014-04-14 14:13:34 -07001860 if ((epctl & DXEPCTL_STALL) &&
1861 (epctl & DXEPCTL_EPTYPE_BULK)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001862 int dctl = readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001863
Dinh Nguyen47a16852014-04-14 14:13:34 -07001864 dctl |= DCTL_CGNPINNAK;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001865 writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001866 }
1867 }
1868 }
1869
Dinh Nguyen47a16852014-04-14 14:13:34 -07001870 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001871 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001872
Dinh Nguyen47a16852014-04-14 14:13:34 -07001873 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001874 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
1875
1876 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001877 /*
1878 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001879 * setup packet. In non-DMA mode we'd get this
1880 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001881 * the setup here.
1882 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001883
1884 if (dir_in)
1885 WARN_ON_ONCE(1);
1886 else
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001887 s3c_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001888 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001889 }
1890
Dinh Nguyen47a16852014-04-14 14:13:34 -07001891 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001892 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001893
Robert Baldyga1479e842013-10-09 08:41:57 +02001894 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001895 /* not sure if this is important, but we'll clear it anyway */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001896 if (ints & DIEPMSK_INTKNTXFEMPMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001897 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
1898 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001899 }
1900
1901 /* this probably means something bad is happening */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001902 if (ints & DIEPMSK_INTKNEPMISMSK) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001903 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
1904 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001905 }
Ben Dooks10aebc72010-07-19 09:40:44 +01001906
1907 /* FIFO has space or is empty (see GAHBCFG) */
1908 if (hsotg->dedicated_fifos &&
Dinh Nguyen47a16852014-04-14 14:13:34 -07001909 ints & DIEPMSK_TXFIFOEMPTY) {
Ben Dooks10aebc72010-07-19 09:40:44 +01001910 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
1911 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09001912 if (!using_dma(hsotg))
1913 s3c_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01001914 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001915 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001916}
1917
1918/**
1919 * s3c_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
1920 * @hsotg: The device state.
1921 *
1922 * Handle updating the device settings after the enumeration phase has
1923 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001924 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001925static void s3c_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001926{
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001927 u32 dsts = readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09001928 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001929
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001930 /*
1931 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001932 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001933 * we connected at.
1934 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001935
1936 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
1937
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001938 /*
1939 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001940 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001941 * not advertise a 64byte MPS on EP0.
1942 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001943
1944 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07001945 switch (dsts & DSTS_ENUMSPD_MASK) {
1946 case DSTS_ENUMSPD_FS:
1947 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001948 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001949 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01001950 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001951 break;
1952
Dinh Nguyen47a16852014-04-14 14:13:34 -07001953 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001954 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001955 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01001956 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001957 break;
1958
Dinh Nguyen47a16852014-04-14 14:13:34 -07001959 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001960 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001961 /*
1962 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001963 * moment, and the documentation seems to imply that it isn't
1964 * supported by the PHYs on some of the devices.
1965 */
1966 break;
1967 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02001968 dev_info(hsotg->dev, "new device is %s\n",
1969 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001970
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001971 /*
1972 * we should now know the maximum packet size for an
1973 * endpoint, so set the endpoints to a default value.
1974 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001975
1976 if (ep0_mps) {
1977 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001978 /* Initialize ep0 for both in and out directions */
1979 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
1980 s3c_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
1981 for (i = 1; i < hsotg->num_of_eps; i++) {
1982 if (hsotg->eps_in[i])
1983 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
1984 if (hsotg->eps_out[i])
1985 s3c_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
1986 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001987 }
1988
1989 /* ensure after enumeration our EP0 is active */
1990
1991 s3c_hsotg_enqueue_setup(hsotg);
1992
1993 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001994 readl(hsotg->regs + DIEPCTL0),
1995 readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996}
1997
1998/**
1999 * kill_all_requests - remove all requests from the endpoint's queue
2000 * @hsotg: The device state.
2001 * @ep: The endpoint the requests may be on.
2002 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002003 *
2004 * Go through the requests on the given endpoint and mark them
2005 * completed with the given result code.
2006 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002007static void kill_all_requests(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002008 struct s3c_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01002009 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002010{
2011 struct s3c_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002012 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002013
Robert Baldyga6b448af2014-12-16 11:51:44 +01002014 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002015
Robert Baldyga6b448af2014-12-16 11:51:44 +01002016 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002017 s3c_hsotg_complete_request(hsotg, ep, req,
2018 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01002019
Robert Baldygab203d0a2014-09-09 10:44:56 +02002020 if (!hsotg->dedicated_fifos)
2021 return;
2022 size = (readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
2023 if (size < ep->fifo_size)
2024 s3c_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002025}
2026
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002027/**
Lukasz Majewski5e891342012-05-04 14:17:07 +02002028 * s3c_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002029 * @hsotg: The device state.
2030 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002031 * The device has been disconnected. Remove all current
2032 * transactions and signal the gadget driver that this
2033 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002034 */
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002035void s3c_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002036{
2037 unsigned ep;
2038
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002039 if (!hsotg->connected)
2040 return;
2041
2042 hsotg->connected = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002043
2044 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2045 if (hsotg->eps_in[ep])
2046 kill_all_requests(hsotg, hsotg->eps_in[ep],
2047 -ESHUTDOWN);
2048 if (hsotg->eps_out[ep])
2049 kill_all_requests(hsotg, hsotg->eps_out[ep],
2050 -ESHUTDOWN);
2051 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002052
2053 call_gadget(hsotg, disconnect);
2054}
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002055EXPORT_SYMBOL_GPL(s3c_hsotg_disconnect);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002056
2057/**
2058 * s3c_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
2059 * @hsotg: The device state:
2060 * @periodic: True if this is a periodic FIFO interrupt
2061 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002062static void s3c_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002063{
2064 struct s3c_hsotg_ep *ep;
2065 int epno, ret;
2066
2067 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002068 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002069 ep = index_to_ep(hsotg, epno, 1);
2070
2071 if (!ep)
2072 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002073
2074 if (!ep->dir_in)
2075 continue;
2076
2077 if ((periodic && !ep->periodic) ||
2078 (!periodic && ep->periodic))
2079 continue;
2080
2081 ret = s3c_hsotg_trytx(hsotg, ep);
2082 if (ret < 0)
2083 break;
2084 }
2085}
2086
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002087/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002088#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2089 GINTSTS_PTXFEMP | \
2090 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002091
2092/**
Lukasz Majewski308d7342012-05-04 14:17:05 +02002093 * s3c_hsotg_corereset - issue softreset to the core
2094 * @hsotg: The device state
2095 *
2096 * Issue a soft reset to the core, and await the core finishing it.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002097 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002098static int s3c_hsotg_corereset(struct dwc2_hsotg *hsotg)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002099{
2100 int timeout;
2101 u32 grstctl;
2102
2103 dev_dbg(hsotg->dev, "resetting core\n");
2104
2105 /* issue soft reset */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002106 writel(GRSTCTL_CSFTRST, hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002107
Du, Changbin2868fea2012-07-24 08:19:25 +08002108 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002109 do {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002110 grstctl = readl(hsotg->regs + GRSTCTL);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002111 } while ((grstctl & GRSTCTL_CSFTRST) && timeout-- > 0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002112
Dinh Nguyen47a16852014-04-14 14:13:34 -07002113 if (grstctl & GRSTCTL_CSFTRST) {
Lukasz Majewski308d7342012-05-04 14:17:05 +02002114 dev_err(hsotg->dev, "Failed to get CSftRst asserted\n");
2115 return -EINVAL;
2116 }
2117
Du, Changbin2868fea2012-07-24 08:19:25 +08002118 timeout = 10000;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002119
2120 while (1) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002121 u32 grstctl = readl(hsotg->regs + GRSTCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002122
2123 if (timeout-- < 0) {
2124 dev_info(hsotg->dev,
2125 "%s: reset failed, GRSTCTL=%08x\n",
2126 __func__, grstctl);
2127 return -ETIMEDOUT;
2128 }
2129
Dinh Nguyen47a16852014-04-14 14:13:34 -07002130 if (!(grstctl & GRSTCTL_AHBIDLE))
Lukasz Majewski308d7342012-05-04 14:17:05 +02002131 continue;
2132
2133 break; /* reset done */
2134 }
2135
2136 dev_dbg(hsotg->dev, "reset successful\n");
2137 return 0;
2138}
2139
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002140/**
2141 * s3c_hsotg_core_init - issue softreset to the core
2142 * @hsotg: The device state
2143 *
2144 * Issue a soft reset to the core, and await the core finishing it.
2145 */
Dinh Nguyen510ffaa2014-11-11 11:13:36 -06002146void s3c_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002147{
2148 s3c_hsotg_corereset(hsotg);
2149
2150 /*
2151 * we must now enable ep0 ready for host detection and then
2152 * set configuration.
2153 */
2154
2155 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002156 writel(hsotg->phyif | GUSBCFG_TOUTCAL(7) |
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002157 (0x5 << 10), hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002158
2159 s3c_hsotg_init_fifo(hsotg);
2160
Dinh Nguyen47a16852014-04-14 14:13:34 -07002161 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002162
Dinh Nguyen47a16852014-04-14 14:13:34 -07002163 writel(1 << 18 | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002164
2165 /* Clear any pending OTG interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002166 writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002167
2168 /* Clear any pending interrupts */
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002169 writel(0xffffffff, hsotg->regs + GINTSTS);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002170
Dinh Nguyen47a16852014-04-14 14:13:34 -07002171 writel(GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
2172 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
2173 GINTSTS_CONIDSTSCHNG | GINTSTS_USBRST |
2174 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
2175 GINTSTS_USBSUSP | GINTSTS_WKUPINT,
2176 hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002177
2178 if (using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002179 writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Gregory Herrero5f050482015-01-09 13:38:46 +01002180 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002181 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002182 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07002183 writel(((hsotg->dedicated_fifos) ? (GAHBCFG_NP_TXF_EMP_LVL |
2184 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2185 GAHBCFG_GLBL_INTR_EN,
Robert Baldyga8acc8292013-09-19 11:50:23 +02002186 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002187
2188 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002189 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2190 * when we have no data to transfer. Otherwise we get being flooded by
2191 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002192 */
2193
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01002194 writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
2195 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002196 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
2197 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2198 DIEPMSK_INTKNEPMISMSK,
2199 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002200
2201 /*
2202 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2203 * DMA mode we may need this.
2204 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002205 writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
2206 DIEPMSK_TIMEOUTMSK) : 0) |
2207 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
2208 DOEPMSK_SETUPMSK,
2209 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002210
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002211 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002212
2213 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002214 readl(hsotg->regs + DIEPCTL0),
2215 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002216
2217 /* enable in and out endpoint interrupts */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002218 s3c_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002219
2220 /*
2221 * Enable the RXFIFO when in slave mode, as this is how we collect
2222 * the data. In DMA mode, we get events from the FIFO but also
2223 * things we cannot process, so do not use it.
2224 */
2225 if (!using_dma(hsotg))
Dinh Nguyen47a16852014-04-14 14:13:34 -07002226 s3c_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002227
2228 /* Enable interrupts for EP0 in and out */
2229 s3c_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2230 s3c_hsotg_ctrl_epint(hsotg, 0, 1, 1);
2231
Dinh Nguyen47a16852014-04-14 14:13:34 -07002232 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002233 udelay(10); /* see openiboot */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002234 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002235
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002236 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002237
2238 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002239 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002240 * writing to the EPCTL register..
2241 */
2242
2243 /* set to read 1 8byte packet */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002244 writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2245 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002246
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002247 writel(s3c_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002248 DXEPCTL_CNAK | DXEPCTL_EPENA |
2249 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002250 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002251
2252 /* enable, but don't activate EP0in */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002253 writel(s3c_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002254 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002255
2256 s3c_hsotg_enqueue_setup(hsotg);
2257
2258 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002259 readl(hsotg->regs + DIEPCTL0),
2260 readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002261
2262 /* clear global NAKs */
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002263 writel(DCTL_CGOUTNAK | DCTL_CGNPINNAK | DCTL_SFTDISCON,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002264 hsotg->regs + DCTL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002265
2266 /* must be at-least 3ms to allow bus to see disconnect */
2267 mdelay(3);
2268
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002269 hsotg->last_rst = jiffies;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002270}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002271
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002272static void s3c_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002273{
2274 /* set the soft-disconnect bit */
2275 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2276}
2277
Dinh Nguyen510ffaa2014-11-11 11:13:36 -06002278void s3c_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002279{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002280 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002281 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002282}
2283
2284/**
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002285 * s3c_hsotg_irq - handle device interrupt
2286 * @irq: The IRQ number triggered
2287 * @pw: The pw value when registered the handler.
2288 */
2289static irqreturn_t s3c_hsotg_irq(int irq, void *pw)
2290{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002291 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002292 int retry_count = 8;
2293 u32 gintsts;
2294 u32 gintmsk;
2295
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002296 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002297irq_retry:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002298 gintsts = readl(hsotg->regs + GINTSTS);
2299 gintmsk = readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002300
2301 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2302 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2303
2304 gintsts &= gintmsk;
2305
Dinh Nguyen47a16852014-04-14 14:13:34 -07002306 if (gintsts & GINTSTS_ENUMDONE) {
2307 writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002308
2309 s3c_hsotg_irq_enumdone(hsotg);
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002310 hsotg->connected = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002311 }
2312
Dinh Nguyen47a16852014-04-14 14:13:34 -07002313 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002314 u32 daint = readl(hsotg->regs + DAINT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002315 u32 daintmsk = readl(hsotg->regs + DAINTMSK);
2316 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002317 int ep;
2318
Robert Baldyga7e804652013-09-19 11:50:20 +02002319 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002320 daint_out = daint >> DAINT_OUTEP_SHIFT;
2321 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002322
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002323 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2324
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002325 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2326 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002327 if (daint_out & 1)
2328 s3c_hsotg_epint(hsotg, ep, 0);
2329 }
2330
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002331 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
2332 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002333 if (daint_in & 1)
2334 s3c_hsotg_epint(hsotg, ep, 1);
2335 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002336 }
2337
Dinh Nguyen47a16852014-04-14 14:13:34 -07002338 if (gintsts & GINTSTS_USBRST) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002339
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002340 u32 usb_status = readl(hsotg->regs + GOTGCTL);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002341
Marek Szyprowski95998152014-10-20 12:45:32 +02002342 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002343 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002344 readl(hsotg->regs + GNPTXSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002345
Dinh Nguyen47a16852014-04-14 14:13:34 -07002346 writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002347
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002348 if (usb_status & GOTGCTL_BSESVLD) {
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002349 if (time_after(jiffies, hsotg->last_rst +
2350 msecs_to_jiffies(200))) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002351
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002352 kill_all_requests(hsotg, hsotg->eps_out[0],
Robert Baldyga6b448af2014-12-16 11:51:44 +01002353 -ECONNRESET);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002354
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002355 s3c_hsotg_core_init_disconnected(hsotg);
2356 s3c_hsotg_core_connect(hsotg);
Lukasz Majewski12a1f4d2012-05-04 14:17:08 +02002357 }
2358 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002359 }
2360
2361 /* check both FIFOs */
2362
Dinh Nguyen47a16852014-04-14 14:13:34 -07002363 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002364 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2365
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002366 /*
2367 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002368 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002369 * it needs re-enabling
2370 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002371
Dinh Nguyen47a16852014-04-14 14:13:34 -07002372 s3c_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002373 s3c_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002374 }
2375
Dinh Nguyen47a16852014-04-14 14:13:34 -07002376 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002377 dev_dbg(hsotg->dev, "PTxFEmp\n");
2378
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002379 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002380
Dinh Nguyen47a16852014-04-14 14:13:34 -07002381 s3c_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002382 s3c_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002383 }
2384
Dinh Nguyen47a16852014-04-14 14:13:34 -07002385 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002386 /*
2387 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002388 * we need to retry s3c_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002389 * set.
2390 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002391
2392 s3c_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002393 }
2394
Dinh Nguyen47a16852014-04-14 14:13:34 -07002395 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002396 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Dinh Nguyen47a16852014-04-14 14:13:34 -07002397 writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002398 }
2399
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002400 /*
2401 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002402 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002403 * the occurrence.
2404 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002405
Dinh Nguyen47a16852014-04-14 14:13:34 -07002406 if (gintsts & GINTSTS_GOUTNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002407 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2408
Dinh Nguyen47a16852014-04-14 14:13:34 -07002409 writel(DCTL_CGOUTNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002410
2411 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002412 }
2413
Dinh Nguyen47a16852014-04-14 14:13:34 -07002414 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002415 dev_info(hsotg->dev, "GINNakEff triggered\n");
2416
Dinh Nguyen47a16852014-04-14 14:13:34 -07002417 writel(DCTL_CGNPINNAK, hsotg->regs + DCTL);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002418
2419 s3c_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002420 }
2421
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002422 /*
2423 * if we've had fifo events, we should try and go around the
2424 * loop again to see if there's any point in returning yet.
2425 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002426
2427 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2428 goto irq_retry;
2429
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002430 spin_unlock(&hsotg->lock);
2431
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002432 return IRQ_HANDLED;
2433}
2434
2435/**
2436 * s3c_hsotg_ep_enable - enable the given endpoint
2437 * @ep: The USB endpint to configure
2438 * @desc: The USB endpoint descriptor to configure with.
2439 *
2440 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002441 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002442static int s3c_hsotg_ep_enable(struct usb_ep *ep,
2443 const struct usb_endpoint_descriptor *desc)
2444{
2445 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002446 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002447 unsigned long flags;
2448 int index = hs_ep->index;
2449 u32 epctrl_reg;
2450 u32 epctrl;
2451 u32 mps;
2452 int dir_in;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002453 int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02002454 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002455
2456 dev_dbg(hsotg->dev,
2457 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2458 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2459 desc->wMaxPacketSize, desc->bInterval);
2460
2461 /* not to be called for EP0 */
2462 WARN_ON(index == 0);
2463
2464 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2465 if (dir_in != hs_ep->dir_in) {
2466 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2467 return -EINVAL;
2468 }
2469
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002470 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002471
2472 /* note, we handle this here instead of s3c_hsotg_set_ep_maxpacket */
2473
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002474 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002475 epctrl = readl(hsotg->regs + epctrl_reg);
2476
2477 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2478 __func__, epctrl, epctrl_reg);
2479
Lukasz Majewski22258f42012-06-14 10:02:24 +02002480 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002481
Dinh Nguyen47a16852014-04-14 14:13:34 -07002482 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2483 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002484
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002485 /*
2486 * mark the endpoint as active, otherwise the core may ignore
2487 * transactions entirely for this endpoint
2488 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002489 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002490
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002491 /*
2492 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002493 * do something with data that we've yet got a request to process
2494 * since the RXFIFO will take data for an endpoint even if the
2495 * size register hasn't been set.
2496 */
2497
Dinh Nguyen47a16852014-04-14 14:13:34 -07002498 epctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002499
2500 /* update the endpoint state */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002501 s3c_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002502
2503 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002504 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002505 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002506 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002507 hs_ep->interval = desc->bInterval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002508
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002509 if (hs_ep->interval > 1 && hs_ep->mc > 1)
2510 dev_err(hsotg->dev, "MC > 1 when interval is not 1\n");
2511
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002512 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2513 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002514 epctrl |= DXEPCTL_EPTYPE_ISO;
2515 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02002516 hs_ep->isochronous = 1;
2517 if (dir_in)
2518 hs_ep->periodic = 1;
2519 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520
2521 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002522 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002523 break;
2524
2525 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02002526 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002527 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002528
Dinh Nguyen47a16852014-04-14 14:13:34 -07002529 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002530 break;
2531
2532 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002533 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002534 break;
2535 }
2536
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002537 /*
2538 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002539 * a unique tx-fifo even if it is non-periodic.
2540 */
Robert Baldygab203d0a2014-09-09 10:44:56 +02002541 if (dir_in && hsotg->dedicated_fifos) {
2542 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01002543 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02002544 if (hsotg->fifo_map & (1<<i))
2545 continue;
2546 val = readl(hsotg->regs + DPTXFSIZN(i));
2547 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
2548 if (val < size)
2549 continue;
2550 hsotg->fifo_map |= 1<<i;
2551
2552 epctrl |= DXEPCTL_TXFNUM(i);
2553 hs_ep->fifo_index = i;
2554 hs_ep->fifo_size = val;
2555 break;
2556 }
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01002557 if (i == hsotg->num_of_eps) {
2558 dev_err(hsotg->dev,
2559 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302560 ret = -ENOMEM;
2561 goto error;
2562 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02002563 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002564
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002565 /* for non control endpoints, set PID to D0 */
2566 if (index)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002567 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002568
2569 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2570 __func__, epctrl);
2571
2572 writel(epctrl, hsotg->regs + epctrl_reg);
2573 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
2574 __func__, readl(hsotg->regs + epctrl_reg));
2575
2576 /* enable the endpoint interrupt */
2577 s3c_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
2578
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302579error:
Lukasz Majewski22258f42012-06-14 10:02:24 +02002580 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002581 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002582}
2583
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002584/**
2585 * s3c_hsotg_ep_disable - disable given endpoint
2586 * @ep: The endpoint to disable.
2587 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002588static int s3c_hsotg_ep_disable(struct usb_ep *ep)
2589{
2590 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002591 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002592 int dir_in = hs_ep->dir_in;
2593 int index = hs_ep->index;
2594 unsigned long flags;
2595 u32 epctrl_reg;
2596 u32 ctrl;
2597
Marek Szyprowski1e011292014-09-09 10:44:54 +02002598 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002599
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002600 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002601 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2602 return -EINVAL;
2603 }
2604
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002605 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002606
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002607 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002608
Robert Baldygab203d0a2014-09-09 10:44:56 +02002609 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
2610 hs_ep->fifo_index = 0;
2611 hs_ep->fifo_size = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002612
2613 ctrl = readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002614 ctrl &= ~DXEPCTL_EPENA;
2615 ctrl &= ~DXEPCTL_USBACTEP;
2616 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002617
2618 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
2619 writel(ctrl, hsotg->regs + epctrl_reg);
2620
2621 /* disable endpoint interrupts */
2622 s3c_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
2623
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01002624 /* terminate all requests with shutdown */
2625 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
2626
Lukasz Majewski22258f42012-06-14 10:02:24 +02002627 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002628 return 0;
2629}
2630
2631/**
2632 * on_list - check request is on the given endpoint
2633 * @ep: The endpoint to check.
2634 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002635 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002636static bool on_list(struct s3c_hsotg_ep *ep, struct s3c_hsotg_req *test)
2637{
2638 struct s3c_hsotg_req *req, *treq;
2639
2640 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2641 if (req == test)
2642 return true;
2643 }
2644
2645 return false;
2646}
2647
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002648/**
2649 * s3c_hsotg_ep_dequeue - dequeue given endpoint
2650 * @ep: The endpoint to dequeue.
2651 * @req: The request to be removed from a queue.
2652 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002653static int s3c_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
2654{
2655 struct s3c_hsotg_req *hs_req = our_req(req);
2656 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002657 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002658 unsigned long flags;
2659
Marek Szyprowski1e011292014-09-09 10:44:54 +02002660 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002661
Lukasz Majewski22258f42012-06-14 10:02:24 +02002662 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002663
2664 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002665 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002666 return -EINVAL;
2667 }
2668
2669 s3c_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002670 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002671
2672 return 0;
2673}
2674
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002675/**
2676 * s3c_hsotg_ep_sethalt - set halt on a given endpoint
2677 * @ep: The endpoint to set halt.
2678 * @value: Set or unset the halt.
2679 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002680static int s3c_hsotg_ep_sethalt(struct usb_ep *ep, int value)
2681{
2682 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002683 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002684 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002685 u32 epreg;
2686 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002687 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002688
2689 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
2690
Robert Baldygac9f721b2014-01-14 08:36:00 +01002691 if (index == 0) {
2692 if (value)
2693 s3c_hsotg_stall_ep0(hs);
2694 else
2695 dev_warn(hs->dev,
2696 "%s: can't clear halt on ep0\n", __func__);
2697 return 0;
2698 }
2699
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002700 if (hs_ep->dir_in) {
2701 epreg = DIEPCTL(index);
2702 epctl = readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002703
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002704 if (value) {
2705 epctl |= DXEPCTL_STALL + DXEPCTL_SNAK;
2706 if (epctl & DXEPCTL_EPENA)
2707 epctl |= DXEPCTL_EPDIS;
2708 } else {
2709 epctl &= ~DXEPCTL_STALL;
2710 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2711 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2712 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2713 epctl |= DXEPCTL_SETD0PID;
2714 }
2715 writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002716 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002717
2718 epreg = DOEPCTL(index);
2719 epctl = readl(hs->regs + epreg);
2720
2721 if (value)
2722 epctl |= DXEPCTL_STALL;
2723 else {
2724 epctl &= ~DXEPCTL_STALL;
2725 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
2726 if (xfertype == DXEPCTL_EPTYPE_BULK ||
2727 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
2728 epctl |= DXEPCTL_SETD0PID;
2729 }
2730 writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002731 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002732
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002733 hs_ep->halted = value;
2734
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002735 return 0;
2736}
2737
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002738/**
2739 * s3c_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
2740 * @ep: The endpoint to set halt.
2741 * @value: Set or unset the halt.
2742 */
2743static int s3c_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
2744{
2745 struct s3c_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002746 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002747 unsigned long flags = 0;
2748 int ret = 0;
2749
2750 spin_lock_irqsave(&hs->lock, flags);
2751 ret = s3c_hsotg_ep_sethalt(ep, value);
2752 spin_unlock_irqrestore(&hs->lock, flags);
2753
2754 return ret;
2755}
2756
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002757static struct usb_ep_ops s3c_hsotg_ep_ops = {
2758 .enable = s3c_hsotg_ep_enable,
2759 .disable = s3c_hsotg_ep_disable,
2760 .alloc_request = s3c_hsotg_ep_alloc_request,
2761 .free_request = s3c_hsotg_ep_free_request,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002762 .queue = s3c_hsotg_ep_queue_lock,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002763 .dequeue = s3c_hsotg_ep_dequeue,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002764 .set_halt = s3c_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002765 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002766};
2767
2768/**
Lukasz Majewski41188782012-05-04 14:17:01 +02002769 * s3c_hsotg_phy_enable - enable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002770 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002771 *
2772 * A wrapper for platform code responsible for controlling
2773 * low-level USB code
2774 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002775static void s3c_hsotg_phy_enable(struct dwc2_hsotg *hsotg)
Lukasz Majewski41188782012-05-04 14:17:01 +02002776{
2777 struct platform_device *pdev = to_platform_device(hsotg->dev);
2778
2779 dev_dbg(hsotg->dev, "pdev 0x%p\n", pdev);
Praveen Panerib2e587d2012-11-14 15:57:16 +05302780
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002781 if (hsotg->uphy)
2782 usb_phy_init(hsotg->uphy);
2783 else if (hsotg->plat && hsotg->plat->phy_init)
2784 hsotg->plat->phy_init(pdev, hsotg->plat->phy_type);
2785 else {
Matt Porter74084842013-12-19 09:23:06 -05002786 phy_init(hsotg->phy);
2787 phy_power_on(hsotg->phy);
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002788 }
Lukasz Majewski41188782012-05-04 14:17:01 +02002789}
2790
2791/**
2792 * s3c_hsotg_phy_disable - disable platform phy dev
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002793 * @hsotg: The driver state
Lukasz Majewski41188782012-05-04 14:17:01 +02002794 *
2795 * A wrapper for platform code responsible for controlling
2796 * low-level USB code
2797 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002798static void s3c_hsotg_phy_disable(struct dwc2_hsotg *hsotg)
Lukasz Majewski41188782012-05-04 14:17:01 +02002799{
2800 struct platform_device *pdev = to_platform_device(hsotg->dev);
2801
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002802 if (hsotg->uphy)
2803 usb_phy_shutdown(hsotg->uphy);
2804 else if (hsotg->plat && hsotg->plat->phy_exit)
2805 hsotg->plat->phy_exit(pdev, hsotg->plat->phy_type);
2806 else {
Matt Porter74084842013-12-19 09:23:06 -05002807 phy_power_off(hsotg->phy);
2808 phy_exit(hsotg->phy);
Kamil Debskica2c5ba2014-09-09 10:44:09 +02002809 }
Lukasz Majewski41188782012-05-04 14:17:01 +02002810}
2811
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002812/**
2813 * s3c_hsotg_init - initalize the usb core
2814 * @hsotg: The driver state
2815 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002816static void s3c_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002817{
2818 /* unmask subset of endpoint interrupts */
2819
Dinh Nguyen47a16852014-04-14 14:13:34 -07002820 writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2821 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
2822 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002823
Dinh Nguyen47a16852014-04-14 14:13:34 -07002824 writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
2825 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
2826 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002827
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002828 writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002829
2830 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002831 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002832
2833 if (0) {
2834 /* post global nak until we're ready */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002835 writel(DCTL_SGNPINNAK | DCTL_SGOUTNAK,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002836 hsotg->regs + DCTL);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002837 }
2838
2839 /* setup fifos */
2840
2841 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002842 readl(hsotg->regs + GRXFSIZ),
2843 readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002844
2845 s3c_hsotg_init_fifo(hsotg);
2846
2847 /* set the PLL on, remove the HNP/SRP and set the PHY */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002848 writel(GUSBCFG_PHYIF16 | GUSBCFG_TOUTCAL(7) | (0x5 << 10),
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002849 hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002850
Gregory Herrerof5090042015-01-09 13:38:47 +01002851 if (using_dma(hsotg))
2852 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002853}
2854
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002855/**
2856 * s3c_hsotg_udc_start - prepare the udc for work
2857 * @gadget: The usb gadget state
2858 * @driver: The usb gadget driver
2859 *
2860 * Perform initialization to prepare udc device and driver
2861 * to work.
2862 */
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002863static int s3c_hsotg_udc_start(struct usb_gadget *gadget,
2864 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002865{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002866 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002867 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002868 int ret;
2869
2870 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02002871 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002872 return -ENODEV;
2873 }
2874
2875 if (!driver) {
2876 dev_err(hsotg->dev, "%s: no driver\n", __func__);
2877 return -EINVAL;
2878 }
2879
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01002880 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002881 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002882
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002883 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002884 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
2885 return -EINVAL;
2886 }
2887
Marek Szyprowski7ad80962014-11-21 15:14:48 +01002888 mutex_lock(&hsotg->init_mutex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002889 WARN_ON(hsotg->driver);
2890
2891 driver->driver.bus = NULL;
2892 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03002893 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002894 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
2895
Robert Baldygad00b4142014-09-09 10:44:57 +02002896 clk_enable(hsotg->clk);
2897
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002898 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
2899 hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002900 if (ret) {
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02002901 dev_err(hsotg->dev, "failed to enable supplies: %d\n", ret);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002902 goto err;
2903 }
2904
Marek Szyprowskic816c472014-10-20 12:45:37 +02002905 s3c_hsotg_phy_enable(hsotg);
Gregory Herrerof6c01592015-01-09 13:38:41 +01002906 if (!IS_ERR_OR_NULL(hsotg->uphy))
2907 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02002908
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002909 spin_lock_irqsave(&hsotg->lock, flags);
2910 s3c_hsotg_init(hsotg);
2911 s3c_hsotg_core_init_disconnected(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01002912 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002913 spin_unlock_irqrestore(&hsotg->lock, flags);
2914
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002915 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02002916
Marek Szyprowski7ad80962014-11-21 15:14:48 +01002917 mutex_unlock(&hsotg->init_mutex);
2918
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002919 return 0;
2920
2921err:
Marek Szyprowski7ad80962014-11-21 15:14:48 +01002922 mutex_unlock(&hsotg->init_mutex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002923 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002924 return ret;
2925}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002926
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002927/**
2928 * s3c_hsotg_udc_stop - stop the udc
2929 * @gadget: The usb gadget state
2930 * @driver: The usb gadget driver
2931 *
2932 * Stop udc hw block and stay tunned for future transmissions
2933 */
Felipe Balbi22835b82014-10-17 12:05:12 -05002934static int s3c_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002935{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002936 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002937 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002938 int ep;
2939
2940 if (!hsotg)
2941 return -ENODEV;
2942
Marek Szyprowski7ad80962014-11-21 15:14:48 +01002943 mutex_lock(&hsotg->init_mutex);
2944
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002945 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002946 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
2947 if (hsotg->eps_in[ep])
2948 s3c_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
2949 if (hsotg->eps_out[ep])
2950 s3c_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
2951 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002952
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002953 spin_lock_irqsave(&hsotg->lock, flags);
2954
Marek Szyprowski32805c32014-10-20 12:45:33 +02002955 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002956 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01002957 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002958
Lukasz Majewski2b19a522012-06-14 10:02:25 +02002959 spin_unlock_irqrestore(&hsotg->lock, flags);
2960
Gregory Herrerof6c01592015-01-09 13:38:41 +01002961 if (!IS_ERR_OR_NULL(hsotg->uphy))
2962 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02002963 s3c_hsotg_phy_disable(hsotg);
2964
Marek Szyprowskic8c10252013-09-12 16:18:48 +02002965 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies), hsotg->supplies);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002966
Robert Baldygad00b4142014-09-09 10:44:57 +02002967 clk_disable(hsotg->clk);
2968
Marek Szyprowski7ad80962014-11-21 15:14:48 +01002969 mutex_unlock(&hsotg->init_mutex);
2970
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002971 return 0;
2972}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002973
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002974/**
2975 * s3c_hsotg_gadget_getframe - read the frame number
2976 * @gadget: The usb gadget state
2977 *
2978 * Read the {micro} frame number
2979 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002980static int s3c_hsotg_gadget_getframe(struct usb_gadget *gadget)
2981{
2982 return s3c_hsotg_read_frameno(to_hsotg(gadget));
2983}
2984
Lukasz Majewskia188b682012-06-22 09:29:56 +02002985/**
2986 * s3c_hsotg_pullup - connect/disconnect the USB PHY
2987 * @gadget: The usb gadget state
2988 * @is_on: Current state of the USB PHY
2989 *
2990 * Connect/Disconnect the USB PHY pullup
2991 */
2992static int s3c_hsotg_pullup(struct usb_gadget *gadget, int is_on)
2993{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002994 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002995 unsigned long flags = 0;
2996
Andrzej Pietrasiewiczd784f1e2014-09-09 10:44:53 +02002997 dev_dbg(hsotg->dev, "%s: is_on: %d\n", __func__, is_on);
Lukasz Majewskia188b682012-06-22 09:29:56 +02002998
Marek Szyprowski7ad80962014-11-21 15:14:48 +01002999 mutex_lock(&hsotg->init_mutex);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003000 spin_lock_irqsave(&hsotg->lock, flags);
3001 if (is_on) {
Robert Baldygad00b4142014-09-09 10:44:57 +02003002 clk_enable(hsotg->clk);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003003 hsotg->enabled = 1;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003004 s3c_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003005 } else {
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003006 s3c_hsotg_core_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003007 hsotg->enabled = 0;
Robert Baldygad00b4142014-09-09 10:44:57 +02003008 clk_disable(hsotg->clk);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003009 }
3010
3011 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3012 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003013 mutex_unlock(&hsotg->init_mutex);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003014
3015 return 0;
3016}
3017
Felipe Balbieeef4582013-01-24 17:58:16 +02003018static const struct usb_gadget_ops s3c_hsotg_gadget_ops = {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003019 .get_frame = s3c_hsotg_gadget_getframe,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003020 .udc_start = s3c_hsotg_udc_start,
3021 .udc_stop = s3c_hsotg_udc_stop,
Lukasz Majewskia188b682012-06-22 09:29:56 +02003022 .pullup = s3c_hsotg_pullup,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003023};
3024
3025/**
3026 * s3c_hsotg_initep - initialise a single endpoint
3027 * @hsotg: The device state.
3028 * @hs_ep: The endpoint to be initialised.
3029 * @epnum: The endpoint number
3030 *
3031 * Initialise the given endpoint (as part of the probe and device state
3032 * creation) to give to the gadget driver. Setup the endpoint name, any
3033 * direction information and other state that may be required.
3034 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003035static void s3c_hsotg_initep(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003036 struct s3c_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003037 int epnum,
3038 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003039{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003040 char *dir;
3041
3042 if (epnum == 0)
3043 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003044 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003045 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003046 else
3047 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003048
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003049 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003050 hs_ep->index = epnum;
3051
3052 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3053
3054 INIT_LIST_HEAD(&hs_ep->queue);
3055 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3056
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003057 /* add to the list of endpoints known by the gadget driver */
3058 if (epnum)
3059 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3060
3061 hs_ep->parent = hsotg;
3062 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003063 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003064 hs_ep->ep.ops = &s3c_hsotg_ep_ops;
3065
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003066 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003067 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003068 * to be something valid.
3069 */
3070
3071 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003072 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003073 if (dir_in)
3074 writel(next, hsotg->regs + DIEPCTL(epnum));
3075 else
3076 writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003077 }
3078}
3079
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003080/**
3081 * s3c_hsotg_hw_cfg - read HW configuration registers
3082 * @param: The device state
3083 *
3084 * Read the USB core HW configuration registers
3085 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003086static int s3c_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003087{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003088 u32 cfg;
3089 u32 ep_type;
3090 u32 i;
3091
Ben Dooks10aebc72010-07-19 09:40:44 +01003092 /* check hardware configuration */
3093
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003094 cfg = readl(hsotg->regs + GHWCFG2);
3095 hsotg->num_of_eps = (cfg >> 10) & 0xF;
3096 /* Add ep0 */
3097 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003098
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003099 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct s3c_hsotg_ep),
3100 GFP_KERNEL);
3101 if (!hsotg->eps_in[0])
3102 return -ENOMEM;
3103 /* Same s3c_hsotg_ep is used in both directions for ep0 */
3104 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003105
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003106 cfg = readl(hsotg->regs + GHWCFG1);
3107 for (i = 1; i < hsotg->num_of_eps; i++, cfg >>= 2) {
3108 ep_type = cfg & 3;
3109 /* Direction in or both */
3110 if (!(ep_type & 2)) {
3111 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
3112 sizeof(struct s3c_hsotg_ep), GFP_KERNEL);
3113 if (!hsotg->eps_in[i])
3114 return -ENOMEM;
3115 }
3116 /* Direction out or both */
3117 if (!(ep_type & 1)) {
3118 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
3119 sizeof(struct s3c_hsotg_ep), GFP_KERNEL);
3120 if (!hsotg->eps_out[i])
3121 return -ENOMEM;
3122 }
3123 }
3124
3125 cfg = readl(hsotg->regs + GHWCFG3);
3126 hsotg->fifo_mem = (cfg >> 16);
3127
3128 cfg = readl(hsotg->regs + GHWCFG4);
3129 hsotg->dedicated_fifos = (cfg >> 25) & 1;
Ben Dooks10aebc72010-07-19 09:40:44 +01003130
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003131 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3132 hsotg->num_of_eps,
3133 hsotg->dedicated_fifos ? "dedicated" : "shared",
3134 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003135 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003136}
3137
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003138/**
3139 * s3c_hsotg_dump - dump state of the udc
3140 * @param: The device state
3141 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003142static void s3c_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003143{
Mark Brown83a01802011-06-01 17:16:15 +01003144#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003145 struct device *dev = hsotg->dev;
3146 void __iomem *regs = hsotg->regs;
3147 u32 val;
3148 int idx;
3149
3150 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003151 readl(regs + DCFG), readl(regs + DCTL),
3152 readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003153
3154 dev_info(dev, "GAHBCFG=0x%08x, 0x44=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003155 readl(regs + GAHBCFG), readl(regs + 0x44));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003156
3157 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003158 readl(regs + GRXFSIZ), readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003159
3160 /* show periodic fifo settings */
3161
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003162 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003163 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003164 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003165 val >> FIFOSIZE_DEPTH_SHIFT,
3166 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003167 }
3168
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003169 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003170 dev_info(dev,
3171 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003172 readl(regs + DIEPCTL(idx)),
3173 readl(regs + DIEPTSIZ(idx)),
3174 readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003175
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003176 val = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003177 dev_info(dev,
3178 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003179 idx, readl(regs + DOEPCTL(idx)),
3180 readl(regs + DOEPTSIZ(idx)),
3181 readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003182
3183 }
3184
3185 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003186 readl(regs + DVBUSDIS), readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003187#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003188}
3189
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003190/**
3191 * state_show - debugfs: show overall driver and device state.
3192 * @seq: The seq file to write to.
3193 * @v: Unused parameter.
3194 *
3195 * This debugfs entry shows the overall state of the hardware and
3196 * some general information about each of the endpoints available
3197 * to the system.
3198 */
3199static int state_show(struct seq_file *seq, void *v)
3200{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003201 struct dwc2_hsotg *hsotg = seq->private;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003202 void __iomem *regs = hsotg->regs;
3203 int idx;
3204
3205 seq_printf(seq, "DCFG=0x%08x, DCTL=0x%08x, DSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003206 readl(regs + DCFG),
3207 readl(regs + DCTL),
3208 readl(regs + DSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003209
3210 seq_printf(seq, "DIEPMSK=0x%08x, DOEPMASK=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003211 readl(regs + DIEPMSK), readl(regs + DOEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003212
3213 seq_printf(seq, "GINTMSK=0x%08x, GINTSTS=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003214 readl(regs + GINTMSK),
3215 readl(regs + GINTSTS));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003216
3217 seq_printf(seq, "DAINTMSK=0x%08x, DAINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003218 readl(regs + DAINTMSK),
3219 readl(regs + DAINT));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003220
3221 seq_printf(seq, "GNPTXSTS=0x%08x, GRXSTSR=%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003222 readl(regs + GNPTXSTS),
3223 readl(regs + GRXSTSR));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003224
Pavel Macheka023da32013-09-30 14:56:02 +02003225 seq_puts(seq, "\nEndpoint status:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003226
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003227 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003228 u32 in, out;
3229
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003230 in = readl(regs + DIEPCTL(idx));
3231 out = readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003232
3233 seq_printf(seq, "ep%d: DIEPCTL=0x%08x, DOEPCTL=0x%08x",
3234 idx, in, out);
3235
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003236 in = readl(regs + DIEPTSIZ(idx));
3237 out = readl(regs + DOEPTSIZ(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003238
3239 seq_printf(seq, ", DIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x",
3240 in, out);
3241
Pavel Macheka023da32013-09-30 14:56:02 +02003242 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003243 }
3244
3245 return 0;
3246}
3247
3248static int state_open(struct inode *inode, struct file *file)
3249{
3250 return single_open(file, state_show, inode->i_private);
3251}
3252
3253static const struct file_operations state_fops = {
3254 .owner = THIS_MODULE,
3255 .open = state_open,
3256 .read = seq_read,
3257 .llseek = seq_lseek,
3258 .release = single_release,
3259};
3260
3261/**
3262 * fifo_show - debugfs: show the fifo information
3263 * @seq: The seq_file to write data to.
3264 * @v: Unused parameter.
3265 *
3266 * Show the FIFO information for the overall fifo and all the
3267 * periodic transmission FIFOs.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003268 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003269static int fifo_show(struct seq_file *seq, void *v)
3270{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003271 struct dwc2_hsotg *hsotg = seq->private;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003272 void __iomem *regs = hsotg->regs;
3273 u32 val;
3274 int idx;
3275
Pavel Macheka023da32013-09-30 14:56:02 +02003276 seq_puts(seq, "Non-periodic FIFOs:\n");
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003277 seq_printf(seq, "RXFIFO: Size %d\n", readl(regs + GRXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003278
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003279 val = readl(regs + GNPTXFSIZ);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003280 seq_printf(seq, "NPTXFIFO: Size %d, Start 0x%08x\n",
Dinh Nguyen47a16852014-04-14 14:13:34 -07003281 val >> FIFOSIZE_DEPTH_SHIFT,
3282 val & FIFOSIZE_DEPTH_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003283
Pavel Macheka023da32013-09-30 14:56:02 +02003284 seq_puts(seq, "\nPeriodic TXFIFOs:\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003285
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003286 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003287 val = readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003288
3289 seq_printf(seq, "\tDPTXFIFO%2d: Size %d, Start 0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003290 val >> FIFOSIZE_DEPTH_SHIFT,
3291 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003292 }
3293
3294 return 0;
3295}
3296
3297static int fifo_open(struct inode *inode, struct file *file)
3298{
3299 return single_open(file, fifo_show, inode->i_private);
3300}
3301
3302static const struct file_operations fifo_fops = {
3303 .owner = THIS_MODULE,
3304 .open = fifo_open,
3305 .read = seq_read,
3306 .llseek = seq_lseek,
3307 .release = single_release,
3308};
3309
3310
3311static const char *decode_direction(int is_in)
3312{
3313 return is_in ? "in" : "out";
3314}
3315
3316/**
3317 * ep_show - debugfs: show the state of an endpoint.
3318 * @seq: The seq_file to write data to.
3319 * @v: Unused parameter.
3320 *
3321 * This debugfs entry shows the state of the given endpoint (one is
3322 * registered for each available).
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003323 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003324static int ep_show(struct seq_file *seq, void *v)
3325{
3326 struct s3c_hsotg_ep *ep = seq->private;
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003327 struct dwc2_hsotg *hsotg = ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003328 struct s3c_hsotg_req *req;
3329 void __iomem *regs = hsotg->regs;
3330 int index = ep->index;
3331 int show_limit = 15;
3332 unsigned long flags;
3333
3334 seq_printf(seq, "Endpoint index %d, named %s, dir %s:\n",
3335 ep->index, ep->ep.name, decode_direction(ep->dir_in));
3336
3337 /* first show the register state */
3338
3339 seq_printf(seq, "\tDIEPCTL=0x%08x, DOEPCTL=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003340 readl(regs + DIEPCTL(index)),
3341 readl(regs + DOEPCTL(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003342
3343 seq_printf(seq, "\tDIEPDMA=0x%08x, DOEPDMA=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003344 readl(regs + DIEPDMA(index)),
3345 readl(regs + DOEPDMA(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003346
3347 seq_printf(seq, "\tDIEPINT=0x%08x, DOEPINT=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003348 readl(regs + DIEPINT(index)),
3349 readl(regs + DOEPINT(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003350
3351 seq_printf(seq, "\tDIEPTSIZ=0x%08x, DOEPTSIZ=0x%08x\n",
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003352 readl(regs + DIEPTSIZ(index)),
3353 readl(regs + DOEPTSIZ(index)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003354
Pavel Macheka023da32013-09-30 14:56:02 +02003355 seq_puts(seq, "\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003356 seq_printf(seq, "mps %d\n", ep->ep.maxpacket);
3357 seq_printf(seq, "total_data=%ld\n", ep->total_data);
3358
3359 seq_printf(seq, "request list (%p,%p):\n",
3360 ep->queue.next, ep->queue.prev);
3361
Lukasz Majewski22258f42012-06-14 10:02:24 +02003362 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003363
3364 list_for_each_entry(req, &ep->queue, queue) {
3365 if (--show_limit < 0) {
Pavel Macheka023da32013-09-30 14:56:02 +02003366 seq_puts(seq, "not showing more requests...\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003367 break;
3368 }
3369
3370 seq_printf(seq, "%c req %p: %d bytes @%p, ",
3371 req == ep->req ? '*' : ' ',
3372 req, req->req.length, req->req.buf);
3373 seq_printf(seq, "%d done, res %d\n",
3374 req->req.actual, req->req.status);
3375 }
3376
Lukasz Majewski22258f42012-06-14 10:02:24 +02003377 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003378
3379 return 0;
3380}
3381
3382static int ep_open(struct inode *inode, struct file *file)
3383{
3384 return single_open(file, ep_show, inode->i_private);
3385}
3386
3387static const struct file_operations ep_fops = {
3388 .owner = THIS_MODULE,
3389 .open = ep_open,
3390 .read = seq_read,
3391 .llseek = seq_lseek,
3392 .release = single_release,
3393};
3394
3395/**
3396 * s3c_hsotg_create_debug - create debugfs directory and files
3397 * @hsotg: The driver state
3398 *
3399 * Create the debugfs files to allow the user to get information
3400 * about the state of the system. The directory name is created
3401 * with the same name as the device itself, in case we end up
3402 * with multiple blocks in future systems.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003403 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003404static void s3c_hsotg_create_debug(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003405{
3406 struct dentry *root;
3407 unsigned epidx;
3408
3409 root = debugfs_create_dir(dev_name(hsotg->dev), NULL);
3410 hsotg->debug_root = root;
3411 if (IS_ERR(root)) {
3412 dev_err(hsotg->dev, "cannot create debug root\n");
3413 return;
3414 }
3415
3416 /* create general state file */
3417
3418 hsotg->debug_file = debugfs_create_file("state", 0444, root,
3419 hsotg, &state_fops);
3420
3421 if (IS_ERR(hsotg->debug_file))
3422 dev_err(hsotg->dev, "%s: failed to create state\n", __func__);
3423
3424 hsotg->debug_fifo = debugfs_create_file("fifo", 0444, root,
3425 hsotg, &fifo_fops);
3426
3427 if (IS_ERR(hsotg->debug_fifo))
3428 dev_err(hsotg->dev, "%s: failed to create fifo\n", __func__);
3429
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003430 /* Create one file for each out endpoint */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003431 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003432 struct s3c_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003433
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003434 ep = hsotg->eps_out[epidx];
3435 if (ep) {
3436 ep->debugfs = debugfs_create_file(ep->name, 0444,
3437 root, ep, &ep_fops);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003438
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003439 if (IS_ERR(ep->debugfs))
3440 dev_err(hsotg->dev, "failed to create %s debug file\n",
3441 ep->name);
3442 }
3443 }
3444 /* Create one file for each in endpoint. EP0 is handled with out eps */
3445 for (epidx = 1; epidx < hsotg->num_of_eps; epidx++) {
3446 struct s3c_hsotg_ep *ep;
3447
3448 ep = hsotg->eps_in[epidx];
3449 if (ep) {
3450 ep->debugfs = debugfs_create_file(ep->name, 0444,
3451 root, ep, &ep_fops);
3452
3453 if (IS_ERR(ep->debugfs))
3454 dev_err(hsotg->dev, "failed to create %s debug file\n",
3455 ep->name);
3456 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003457 }
3458}
3459
3460/**
3461 * s3c_hsotg_delete_debug - cleanup debugfs entries
3462 * @hsotg: The driver state
3463 *
3464 * Cleanup (remove) the debugfs files for use on module exit.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003465 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003466static void s3c_hsotg_delete_debug(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003467{
3468 unsigned epidx;
3469
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003470 for (epidx = 0; epidx < hsotg->num_of_eps; epidx++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003471 if (hsotg->eps_in[epidx])
3472 debugfs_remove(hsotg->eps_in[epidx]->debugfs);
3473 if (hsotg->eps_out[epidx])
3474 debugfs_remove(hsotg->eps_out[epidx]->debugfs);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003475 }
3476
3477 debugfs_remove(hsotg->debug_file);
3478 debugfs_remove(hsotg->debug_fifo);
3479 debugfs_remove(hsotg->debug_root);
3480}
3481
Gregory Herreroedd74be2015-01-09 13:38:48 +01003482#ifdef CONFIG_OF
3483static void s3c_hsotg_of_probe(struct dwc2_hsotg *hsotg)
3484{
3485 struct device_node *np = hsotg->dev->of_node;
Gregory Herrero0a176272015-01-09 13:38:52 +01003486 u32 len = 0;
3487 u32 i = 0;
Gregory Herreroedd74be2015-01-09 13:38:48 +01003488
3489 /* Enable dma if requested in device tree */
3490 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
Gregory Herrero0a176272015-01-09 13:38:52 +01003491
3492 /*
3493 * Register TX periodic fifo size per endpoint.
3494 * EP0 is excluded since it has no fifo configuration.
3495 */
3496 if (!of_find_property(np, "g-tx-fifo-size", &len))
3497 goto rx_fifo;
3498
3499 len /= sizeof(u32);
3500
3501 /* Read tx fifo sizes other than ep0 */
3502 if (of_property_read_u32_array(np, "g-tx-fifo-size",
3503 &hsotg->g_tx_fifo_sz[1], len))
3504 goto rx_fifo;
3505
3506 /* Add ep0 */
3507 len++;
3508
3509 /* Make remaining TX fifos unavailable */
3510 if (len < MAX_EPS_CHANNELS) {
3511 for (i = len; i < MAX_EPS_CHANNELS; i++)
3512 hsotg->g_tx_fifo_sz[i] = 0;
3513 }
3514
3515rx_fifo:
3516 /* Register RX fifo size */
3517 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
3518
3519 /* Register NPTX fifo size */
3520 of_property_read_u32(np, "g-np-tx-fifo-size",
3521 &hsotg->g_np_g_tx_fifo_sz);
Gregory Herreroedd74be2015-01-09 13:38:48 +01003522}
3523#else
3524static inline void s3c_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
3525#endif
3526
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003527/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06003528 * dwc2_gadget_init - init function for gadget
3529 * @dwc2: The data structure for the DWC2 driver.
3530 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003531 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003532int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003533{
Dinh Nguyen117777b2014-11-11 11:13:34 -06003534 struct device *dev = hsotg->dev;
3535 struct s3c_hsotg_plat *plat = dev->platform_data;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003536 int epnum;
3537 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003538 int i;
Gregory Herrero0a176272015-01-09 13:38:52 +01003539 u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003540
Kamil Debski1b59fc72014-09-09 10:44:52 +02003541 /* Set default UTMI width */
3542 hsotg->phyif = GUSBCFG_PHYIF16;
3543
Gregory Herreroedd74be2015-01-09 13:38:48 +01003544 s3c_hsotg_of_probe(hsotg);
3545
Gregory Herrero0a176272015-01-09 13:38:52 +01003546 /* Initialize to legacy fifo configuration values */
3547 hsotg->g_rx_fifo_sz = 2048;
3548 hsotg->g_np_g_tx_fifo_sz = 1024;
3549 memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
3550 /* Device tree specific probe */
3551 s3c_hsotg_of_probe(hsotg);
3552 /* Dump fifo information */
3553 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
3554 hsotg->g_np_g_tx_fifo_sz);
3555 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
3556 for (i = 0; i < MAX_EPS_CHANNELS; i++)
3557 dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
3558 hsotg->g_tx_fifo_sz[i]);
Matt Porter74084842013-12-19 09:23:06 -05003559 /*
Yunzhi Li135b3c42014-12-08 17:46:26 +08003560 * If platform probe couldn't find a generic PHY or an old style
3561 * USB PHY, fall back to pdata
Matt Porter74084842013-12-19 09:23:06 -05003562 */
Yunzhi Li135b3c42014-12-08 17:46:26 +08003563 if (IS_ERR_OR_NULL(hsotg->phy) && IS_ERR_OR_NULL(hsotg->uphy)) {
3564 plat = dev_get_platdata(dev);
3565 if (!plat) {
3566 dev_err(dev,
3567 "no platform data or transceiver defined\n");
3568 return -EPROBE_DEFER;
3569 }
3570 hsotg->plat = plat;
3571 } else if (hsotg->phy) {
Kamil Debski1b59fc72014-09-09 10:44:52 +02003572 /*
3573 * If using the generic PHY framework, check if the PHY bus
3574 * width is 8-bit and set the phyif appropriately.
3575 */
Yunzhi Li135b3c42014-12-08 17:46:26 +08003576 if (phy_get_bus_width(hsotg->phy) == 8)
Kamil Debski1b59fc72014-09-09 10:44:52 +02003577 hsotg->phyif = GUSBCFG_PHYIF8;
3578 }
Praveen Panerib2e587d2012-11-14 15:57:16 +05303579
Dinh Nguyen117777b2014-11-11 11:13:34 -06003580 hsotg->clk = devm_clk_get(dev, "otg");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003581 if (IS_ERR(hsotg->clk)) {
Dinh Nguyen8d736d82014-11-11 11:13:38 -06003582 hsotg->clk = NULL;
Dinh Nguyenf415fbd2014-11-24 11:02:11 -06003583 dev_dbg(dev, "cannot get otg clock\n");
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003584 }
3585
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003586 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003587 hsotg->gadget.ops = &s3c_hsotg_gadget_ops;
3588 hsotg->gadget.name = dev_name(dev);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003589
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003590 /* reset the system */
3591
Dinh Nguyenf415fbd2014-11-24 11:02:11 -06003592 ret = clk_prepare_enable(hsotg->clk);
3593 if (ret) {
3594 dev_err(dev, "failed to enable otg clk\n");
3595 goto err_clk;
3596 }
3597
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003598
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003599 /* regulators */
3600
3601 for (i = 0; i < ARRAY_SIZE(hsotg->supplies); i++)
3602 hsotg->supplies[i].supply = s3c_hsotg_supply_names[i];
3603
Sachin Kamatcd762132013-01-08 14:27:00 +05303604 ret = devm_regulator_bulk_get(dev, ARRAY_SIZE(hsotg->supplies),
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003605 hsotg->supplies);
3606 if (ret) {
3607 dev_err(dev, "failed to request supplies: %d\n", ret);
Sachin Kamat338edab2012-05-18 14:33:46 +05303608 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003609 }
3610
3611 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
3612 hsotg->supplies);
3613
3614 if (ret) {
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003615 dev_err(dev, "failed to enable supplies: %d\n", ret);
Mian Yousaf Kaukabc139ec22015-01-09 13:38:45 +01003616 goto err_clk;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003617 }
3618
Lukasz Majewski41188782012-05-04 14:17:01 +02003619 /* usb phy enable */
3620 s3c_hsotg_phy_enable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003621
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003622 s3c_hsotg_corereset(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003623 ret = s3c_hsotg_hw_cfg(hsotg);
3624 if (ret) {
3625 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
3626 goto err_clk;
3627 }
3628
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003629 s3c_hsotg_init(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003630
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003631 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
3632 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3633 if (!hsotg->ctrl_buff) {
3634 dev_err(dev, "failed to allocate ctrl request buff\n");
3635 ret = -ENOMEM;
3636 goto err_supplies;
3637 }
3638
3639 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
3640 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3641 if (!hsotg->ep0_buff) {
3642 dev_err(dev, "failed to allocate ctrl reply buff\n");
3643 ret = -ENOMEM;
3644 goto err_supplies;
3645 }
3646
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003647 ret = devm_request_irq(hsotg->dev, irq, s3c_hsotg_irq, IRQF_SHARED,
3648 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003649 if (ret < 0) {
3650 s3c_hsotg_phy_disable(hsotg);
3651 clk_disable_unprepare(hsotg->clk);
3652 regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3653 hsotg->supplies);
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003654 dev_err(dev, "cannot claim IRQ for gadget\n");
Mian Yousaf Kaukabc139ec22015-01-09 13:38:45 +01003655 goto err_supplies;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003656 }
3657
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003658 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3659
3660 if (hsotg->num_of_eps == 0) {
3661 dev_err(dev, "wrong number of EPs (zero)\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003662 ret = -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003663 goto err_supplies;
3664 }
3665
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003666 /* setup endpoint information */
3667
3668 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003669 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003670
3671 /* allocate EP0 request */
3672
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003673 hsotg->ctrl_req = s3c_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003674 GFP_KERNEL);
3675 if (!hsotg->ctrl_req) {
3676 dev_err(dev, "failed to allocate ctrl req\n");
Julia Lawalldfdda5a2012-08-14 08:47:34 +02003677 ret = -ENOMEM;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003678 goto err_supplies;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003679 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003680
3681 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003682 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
3683 if (hsotg->eps_in[epnum])
3684 s3c_hsotg_initep(hsotg, hsotg->eps_in[epnum],
3685 epnum, 1);
3686 if (hsotg->eps_out[epnum])
3687 s3c_hsotg_initep(hsotg, hsotg->eps_out[epnum],
3688 epnum, 0);
3689 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003690
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003691 /* disable power and clock */
Marek Szyprowski3a8146a2014-10-20 12:45:34 +02003692 s3c_hsotg_phy_disable(hsotg);
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003693
3694 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3695 hsotg->supplies);
3696 if (ret) {
Dinh Nguyen117777b2014-11-11 11:13:34 -06003697 dev_err(dev, "failed to disable supplies: %d\n", ret);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003698 goto err_supplies;
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003699 }
3700
Dinh Nguyen117777b2014-11-11 11:13:34 -06003701 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003702 if (ret)
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003703 goto err_supplies;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003704
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003705 s3c_hsotg_create_debug(hsotg);
3706
3707 s3c_hsotg_dump(hsotg);
3708
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003709 return 0;
3710
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003711err_supplies:
Lukasz Majewski41188782012-05-04 14:17:01 +02003712 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003713err_clk:
Lukasz Majewski1d144c62012-05-04 14:17:16 +02003714 clk_disable_unprepare(hsotg->clk);
Sachin Kamat338edab2012-05-18 14:33:46 +05303715
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003716 return ret;
3717}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003718EXPORT_SYMBOL_GPL(dwc2_gadget_init);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003719
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003720/**
3721 * s3c_hsotg_remove - remove function for hsotg driver
3722 * @pdev: The platform information for the driver
3723 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003724int s3c_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003725{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003726 usb_del_gadget_udc(&hsotg->gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003727 s3c_hsotg_delete_debug(hsotg);
Lukasz Majewski04b4a0f2012-05-04 14:17:15 +02003728 clk_disable_unprepare(hsotg->clk);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003729
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003730 return 0;
3731}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003732EXPORT_SYMBOL_GPL(s3c_hsotg_remove);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003733
Dinh Nguyen117777b2014-11-11 11:13:34 -06003734int s3c_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003735{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003736 unsigned long flags;
3737 int ret = 0;
3738
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003739 mutex_lock(&hsotg->init_mutex);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003740
3741 if (hsotg->driver) {
3742 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003743
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003744 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3745 hsotg->driver->driver.name);
3746
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003747 spin_lock_irqsave(&hsotg->lock, flags);
3748 if (hsotg->enabled)
3749 s3c_hsotg_core_disconnect(hsotg);
3750 s3c_hsotg_disconnect(hsotg);
3751 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3752 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003753
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003754 s3c_hsotg_phy_disable(hsotg);
Marek Szyprowski7b093f72014-10-20 12:45:39 +02003755
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003756 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3757 if (hsotg->eps_in[ep])
3758 s3c_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3759 if (hsotg->eps_out[ep])
3760 s3c_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3761 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003762
3763 ret = regulator_bulk_disable(ARRAY_SIZE(hsotg->supplies),
3764 hsotg->supplies);
Robert Baldygad00b4142014-09-09 10:44:57 +02003765 clk_disable(hsotg->clk);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003766 }
3767
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003768 mutex_unlock(&hsotg->init_mutex);
3769
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003770 return ret;
3771}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003772EXPORT_SYMBOL_GPL(s3c_hsotg_suspend);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003773
Dinh Nguyen117777b2014-11-11 11:13:34 -06003774int s3c_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003775{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003776 unsigned long flags;
3777 int ret = 0;
3778
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003779 mutex_lock(&hsotg->init_mutex);
3780
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003781 if (hsotg->driver) {
3782 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3783 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02003784
3785 clk_enable(hsotg->clk);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003786 ret = regulator_bulk_enable(ARRAY_SIZE(hsotg->supplies),
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003787 hsotg->supplies);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003788
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003789 s3c_hsotg_phy_enable(hsotg);
3790
3791 spin_lock_irqsave(&hsotg->lock, flags);
3792 s3c_hsotg_core_init_disconnected(hsotg);
3793 if (hsotg->enabled)
3794 s3c_hsotg_core_connect(hsotg);
3795 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003796 }
Marek Szyprowski7ad80962014-11-21 15:14:48 +01003797 mutex_unlock(&hsotg->init_mutex);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003798
3799 return ret;
3800}
Dinh Nguyen117777b2014-11-11 11:13:34 -06003801EXPORT_SYMBOL_GPL(s3c_hsotg_resume);