blob: 7db9f9fa40de7c87de21ff5cca0a3445858c5f6e [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>
Marek Szyprowski7ad80962014-11-21 15:14:48 +010023#include <linux/mutex.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010024#include <linux/seq_file.h>
25#include <linux/delay.h>
26#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020028#include <linux/of_platform.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010029
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053032#include <linux/usb/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010033
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070034#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060035#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010036
37/* conversion functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050038static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010039{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050040 return container_of(req, struct dwc2_hsotg_req, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041}
42
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050043static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010044{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050045 return container_of(ep, struct dwc2_hsotg_ep, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010046}
47
Dinh Nguyen941fcce2014-11-11 11:13:33 -060048static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010049{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060050 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010051}
52
53static inline void __orr32(void __iomem *ptr, u32 val)
54{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030055 dwc2_writel(dwc2_readl(ptr) | val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010056}
57
58static inline void __bic32(void __iomem *ptr, u32 val)
59{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030060 dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010061}
62
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050063static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010064 u32 ep_index, u32 dir_in)
65{
66 if (dir_in)
67 return hsotg->eps_in[ep_index];
68 else
69 return hsotg->eps_out[ep_index];
70}
71
Mickael Maison997f4f82014-12-23 17:39:45 +010072/* forward declaration of functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050073static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010074
75/**
76 * using_dma - return the DMA status of the driver.
77 * @hsotg: The driver state.
78 *
79 * Return true if we're using DMA.
80 *
81 * Currently, we have the DMA support code worked into everywhere
82 * that needs it, but the AMBA DMA implementation in the hardware can
83 * only DMA from 32bit aligned addresses. This means that gadgets such
84 * as the CDC Ethernet cannot work as they often pass packets which are
85 * not 32bit aligned.
86 *
87 * Unfortunately the choice to use DMA or not is global to the controller
88 * and seems to be only settable when the controller is being put through
89 * a core reset. This means we either need to fix the gadgets to take
90 * account of DMA alignment, or add bounce buffers (yuerk).
91 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010092 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010093 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060094static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010095{
Gregory Herreroedd74be2015-01-09 13:38:48 +010096 return hsotg->g_using_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +010097}
98
99/**
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700100 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
101 * @hs_ep: The endpoint
102 * @increment: The value to increment by
103 *
104 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
105 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
106 */
107static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
108{
109 hs_ep->target_frame += hs_ep->interval;
110 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
111 hs_ep->frame_overrun = 1;
112 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
113 } else {
114 hs_ep->frame_overrun = 0;
115 }
116}
117
118/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500119 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100120 * @hsotg: The device state
121 * @ints: A bitmask of the interrupts to enable
122 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500123static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100124{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300125 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100126 u32 new_gsintmsk;
127
128 new_gsintmsk = gsintmsk | ints;
129
130 if (new_gsintmsk != gsintmsk) {
131 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300132 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100133 }
134}
135
136/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500137 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100138 * @hsotg: The device state
139 * @ints: A bitmask of the interrupts to enable
140 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500141static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100142{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300143 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100144 u32 new_gsintmsk;
145
146 new_gsintmsk = gsintmsk & ~ints;
147
148 if (new_gsintmsk != gsintmsk)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300149 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100150}
151
152/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500153 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100154 * @hsotg: The device state
155 * @ep: The endpoint index
156 * @dir_in: True if direction is in.
157 * @en: The enable value, true to enable
158 *
159 * Set or clear the mask for an individual endpoint's interrupt
160 * request.
161 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500162static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100163 unsigned int ep, unsigned int dir_in,
164 unsigned int en)
165{
166 unsigned long flags;
167 u32 bit = 1 << ep;
168 u32 daint;
169
170 if (!dir_in)
171 bit <<= 16;
172
173 local_irq_save(flags);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300174 daint = dwc2_readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100175 if (en)
176 daint |= bit;
177 else
178 daint &= ~bit;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300179 dwc2_writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100180 local_irq_restore(flags);
181}
182
183/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500184 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100185 * @hsotg: The device instance.
186 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500187static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100188{
Ben Dooks0f002d22010-05-25 05:36:50 +0100189 unsigned int ep;
190 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100191 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100192 u32 val;
193
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100194 /* Reset fifo map if not correctly cleared during previous session */
195 WARN_ON(hsotg->fifo_map);
196 hsotg->fifo_map = 0;
197
Gregory Herrero0a176272015-01-09 13:38:52 +0100198 /* set RX/NPTX FIFO sizes */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300199 dwc2_writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
200 dwc2_writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
Gregory Herrero0a176272015-01-09 13:38:52 +0100201 (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
202 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100203
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200204 /*
205 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100206 * block have overlapping default addresses. This also ensures
207 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200208 * known values.
209 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100210
211 /* start at the end of the GNPTXFSIZ, rounded up */
Gregory Herrero0a176272015-01-09 13:38:52 +0100212 addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
Ben Dooks0f002d22010-05-25 05:36:50 +0100213
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200214 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100215 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200216 * them to endpoints dynamically according to maxpacket size value of
217 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200218 */
Gregory Herrero0a176272015-01-09 13:38:52 +0100219 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
220 if (!hsotg->g_tx_fifo_sz[ep])
221 continue;
Robert Baldygab203d0a2014-09-09 10:44:56 +0200222 val = addr;
Gregory Herrero0a176272015-01-09 13:38:52 +0100223 val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
224 WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
Robert Baldygab203d0a2014-09-09 10:44:56 +0200225 "insufficient fifo memory");
Gregory Herrero0a176272015-01-09 13:38:52 +0100226 addr += hsotg->g_tx_fifo_sz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100227
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300228 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100229 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100230
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200231 /*
232 * according to p428 of the design guide, we need to ensure that
233 * all fifos are flushed before continuing
234 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100235
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300236 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700237 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100238
239 /* wait until the fifos are both flushed */
240 timeout = 100;
241 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300242 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100243
Dinh Nguyen47a16852014-04-14 14:13:34 -0700244 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100245 break;
246
247 if (--timeout == 0) {
248 dev_err(hsotg->dev,
249 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
250 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100251 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100252 }
253
254 udelay(1);
255 }
256
257 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100258}
259
260/**
261 * @ep: USB endpoint to allocate request for.
262 * @flags: Allocation flags
263 *
264 * Allocate a new USB request structure appropriate for the specified endpoint
265 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500266static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
Mark Brown0978f8c2010-01-18 13:18:35 +0000267 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100268{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500269 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100270
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500271 req = kzalloc(sizeof(struct dwc2_hsotg_req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100272 if (!req)
273 return NULL;
274
275 INIT_LIST_HEAD(&req->queue);
276
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100277 return &req->req;
278}
279
280/**
281 * is_ep_periodic - return true if the endpoint is in periodic mode.
282 * @hs_ep: The endpoint to query.
283 *
284 * Returns true if the endpoint is in periodic mode, meaning it is being
285 * used for an Interrupt or ISO transfer.
286 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500287static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100288{
289 return hs_ep->periodic;
290}
291
292/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500293 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100294 * @hsotg: The device state.
295 * @hs_ep: The endpoint for the request
296 * @hs_req: The request being processed.
297 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500298 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100299 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200300 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500301static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
302 struct dwc2_hsotg_ep *hs_ep,
303 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100304{
305 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100306
307 /* ignore this if we're not moving any data */
308 if (hs_req->req.length == 0)
309 return;
310
Jingoo Han17d966a2013-05-11 21:14:00 +0900311 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100312}
313
314/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500315 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100316 * @hsotg: The controller state.
317 * @hs_ep: The endpoint we're going to write for.
318 * @hs_req: The request to write data for.
319 *
320 * This is called when the TxFIFO has some space in it to hold a new
321 * transmission and we have something to give it. The actual setup of
322 * the data size is done elsewhere, so all we have to do is to actually
323 * write the data.
324 *
325 * The return value is zero if there is more space (or nothing was done)
326 * otherwise -ENOSPC is returned if the FIFO space was used up.
327 *
328 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200329 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500330static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
331 struct dwc2_hsotg_ep *hs_ep,
332 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100333{
334 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300335 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100336 int buf_pos = hs_req->req.actual;
337 int to_write = hs_ep->size_loaded;
338 void *data;
339 int can_write;
340 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200341 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100342
343 to_write -= (buf_pos - hs_ep->last_load);
344
345 /* if there's nothing to write, get out early */
346 if (to_write == 0)
347 return 0;
348
Ben Dooks10aebc72010-07-19 09:40:44 +0100349 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300350 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100351 int size_left;
352 int size_done;
353
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200354 /*
355 * work out how much data was loaded so we can calculate
356 * how much data is left in the fifo.
357 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100358
Dinh Nguyen47a16852014-04-14 14:13:34 -0700359 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100360
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200361 /*
362 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100363 * previous data has been completely sent.
364 */
365 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500366 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100367 return -ENOSPC;
368 }
369
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100370 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
371 __func__, size_left,
372 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
373
374 /* how much of the data has moved */
375 size_done = hs_ep->size_loaded - size_left;
376
377 /* how much data is left in the fifo */
378 can_write = hs_ep->fifo_load - size_done;
379 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
380 __func__, can_write);
381
382 can_write = hs_ep->fifo_size - can_write;
383 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
384 __func__, can_write);
385
386 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500387 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100388 return -ENOSPC;
389 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100390 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300391 can_write = dwc2_readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100392
393 can_write &= 0xffff;
394 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100395 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700396 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100397 dev_dbg(hsotg->dev,
398 "%s: no queue slots available (0x%08x)\n",
399 __func__, gnptxsts);
400
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500401 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100402 return -ENOSPC;
403 }
404
Dinh Nguyen47a16852014-04-14 14:13:34 -0700405 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100406 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100407 }
408
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200409 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
410
411 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
412 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100413
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200414 /*
415 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100416 * FIFO, requests of >512 cause the endpoint to get stuck with a
417 * fragment of the end of the transfer in it.
418 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200419 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100420 can_write = 512;
421
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200422 /*
423 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100424 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200425 * doing it.
426 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200427 if (to_write > max_transfer) {
428 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100429
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200430 /* it's needed only when we do not use dedicated fifos */
431 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500432 dwc2_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700433 periodic ? GINTSTS_PTXFEMP :
434 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100435 }
436
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100437 /* see if we can write data */
438
439 if (to_write > can_write) {
440 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200441 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100442
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200443 /*
444 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100445 * exact number of packets.
446 *
447 * Note, we do not currently check to see if we can ever
448 * write a full packet or not to the FIFO.
449 */
450
451 if (pkt_round)
452 to_write -= pkt_round;
453
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200454 /*
455 * enable correct FIFO interrupt to alert us when there
456 * is more room left.
457 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100458
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200459 /* it's needed only when we do not use dedicated fifos */
460 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500461 dwc2_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700462 periodic ? GINTSTS_PTXFEMP :
463 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100464 }
465
466 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
467 to_write, hs_req->req.length, can_write, buf_pos);
468
469 if (to_write <= 0)
470 return -ENOSPC;
471
472 hs_req->req.actual = buf_pos + to_write;
473 hs_ep->total_data += to_write;
474
475 if (periodic)
476 hs_ep->fifo_load += to_write;
477
478 to_write = DIV_ROUND_UP(to_write, 4);
479 data = hs_req->req.buf + buf_pos;
480
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500481 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100482
483 return (to_write >= can_write) ? -ENOSPC : 0;
484}
485
486/**
487 * get_ep_limit - get the maximum data legnth for this endpoint
488 * @hs_ep: The endpoint
489 *
490 * Return the maximum data that can be queued in one go on a given endpoint
491 * so that transfers that are too long can be split.
492 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500493static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100494{
495 int index = hs_ep->index;
496 unsigned maxsize;
497 unsigned maxpkt;
498
499 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700500 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
501 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100502 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100503 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900504 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700505 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900506 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100507 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100508 }
509
510 /* we made the constant loading easier above by using +1 */
511 maxpkt--;
512 maxsize--;
513
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200514 /*
515 * constrain by packet count if maxpkts*pktsize is greater
516 * than the length register size.
517 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100518
519 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
520 maxsize = maxpkt * hs_ep->ep.maxpacket;
521
522 return maxsize;
523}
524
525/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500526 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100527 * @hsotg: The controller state.
528 * @hs_ep: The endpoint to process a request for
529 * @hs_req: The request to start.
530 * @continuing: True if we are doing more for the current request.
531 *
532 * Start the given request running by setting the endpoint registers
533 * appropriately, and writing any data to the FIFOs.
534 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500535static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
536 struct dwc2_hsotg_ep *hs_ep,
537 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100538 bool continuing)
539{
540 struct usb_request *ureq = &hs_req->req;
541 int index = hs_ep->index;
542 int dir_in = hs_ep->dir_in;
543 u32 epctrl_reg;
544 u32 epsize_reg;
545 u32 epsize;
546 u32 ctrl;
547 unsigned length;
548 unsigned packets;
549 unsigned maxreq;
550
551 if (index != 0) {
552 if (hs_ep->req && !continuing) {
553 dev_err(hsotg->dev, "%s: active request\n", __func__);
554 WARN_ON(1);
555 return;
556 } else if (hs_ep->req != hs_req && continuing) {
557 dev_err(hsotg->dev,
558 "%s: continue different req\n", __func__);
559 WARN_ON(1);
560 return;
561 }
562 }
563
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200564 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
565 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100566
567 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300568 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100569 hs_ep->dir_in ? "in" : "out");
570
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900571 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300572 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900573
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200574 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900575 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
576 return;
577 }
578
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100579 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200580 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
581 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100582
583 maxreq = get_ep_limit(hs_ep);
584 if (length > maxreq) {
585 int round = maxreq % hs_ep->ep.maxpacket;
586
587 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
588 __func__, length, maxreq, round);
589
590 /* round down to multiple of packets */
591 if (round)
592 maxreq -= round;
593
594 length = maxreq;
595 }
596
597 if (length)
598 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
599 else
600 packets = 1; /* send one packet if length is zero. */
601
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200602 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
603 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
604 return;
605 }
606
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100607 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200608 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700609 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200610 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700611 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100612 else
613 epsize = 0;
614
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100615 /*
616 * zero length packet should be programmed on its own and should not
617 * be counted in DIEPTSIZ.PktCnt with other packets.
618 */
619 if (dir_in && ureq->zero && !continuing) {
620 /* Test if zlp is actually required. */
621 if ((ureq->length >= hs_ep->ep.maxpacket) &&
622 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +0100623 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100624 }
625
Dinh Nguyen47a16852014-04-14 14:13:34 -0700626 epsize |= DXEPTSIZ_PKTCNT(packets);
627 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100628
629 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
630 __func__, packets, length, ureq->length, epsize, epsize_reg);
631
632 /* store the request as the current one we're doing */
633 hs_ep->req = hs_req;
634
635 /* write size / packets */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300636 dwc2_writel(epsize, hsotg->regs + epsize_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100637
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900638 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100639 unsigned int dma_reg;
640
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200641 /*
642 * write DMA address to control register, buffer already
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500643 * synced by dwc2_hsotg_ep_queue().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200644 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100645
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200646 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300647 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100648
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300649 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900650 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100651 }
652
Dinh Nguyen47a16852014-04-14 14:13:34 -0700653 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200654
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100655 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +0200656
657 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100658 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -0700659 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200660
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100661 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300662 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100663
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200664 /*
665 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100666 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200667 * this information.
668 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100669 hs_ep->size_loaded = length;
670 hs_ep->last_load = ureq->actual;
671
672 if (dir_in && !using_dma(hsotg)) {
673 /* set these anyway, we may need them for non-periodic in */
674 hs_ep->fifo_load = 0;
675
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500676 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100677 }
678
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200679 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200680 * Note, trying to clear the NAK here causes problems with transmit
681 * on the S3C6400 ending up with the TXFIFO becoming full.
682 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100683
684 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300685 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +0100686 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700687 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300688 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100689
Dinh Nguyen47a16852014-04-14 14:13:34 -0700690 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300691 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200692
693 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500694 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100695}
696
697/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500698 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100699 * @hsotg: The device state.
700 * @hs_ep: The endpoint the request is on.
701 * @req: The request being processed.
702 *
703 * We've been asked to queue a request, so ensure that the memory buffer
704 * is correctly setup for DMA. If we've been passed an extant DMA address
705 * then ensure the buffer has been synced to memory. If our buffer has no
706 * DMA memory, then we map the memory and mark our request to allow us to
707 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200708 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500709static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
710 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100711 struct usb_request *req)
712{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500713 struct dwc2_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200714 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100715
716 /* if the length is zero, ignore the DMA data */
717 if (hs_req->req.length == 0)
718 return 0;
719
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200720 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
721 if (ret)
722 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100723
724 return 0;
725
726dma_error:
727 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
728 __func__, req->buf, req->length);
729
730 return -EIO;
731}
732
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500733static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
734 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100735{
736 void *req_buf = hs_req->req.buf;
737
738 /* If dma is not being used or buffer is aligned */
739 if (!using_dma(hsotg) || !((long)req_buf & 3))
740 return 0;
741
742 WARN_ON(hs_req->saved_req_buf);
743
744 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
745 hs_ep->ep.name, req_buf, hs_req->req.length);
746
747 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
748 if (!hs_req->req.buf) {
749 hs_req->req.buf = req_buf;
750 dev_err(hsotg->dev,
751 "%s: unable to allocate memory for bounce buffer\n",
752 __func__);
753 return -ENOMEM;
754 }
755
756 /* Save actual buffer */
757 hs_req->saved_req_buf = req_buf;
758
759 if (hs_ep->dir_in)
760 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
761 return 0;
762}
763
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500764static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
765 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100766{
767 /* If dma is not being used or buffer was aligned */
768 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
769 return;
770
771 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
772 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
773
774 /* Copy data from bounce buffer on successful out transfer */
775 if (!hs_ep->dir_in && !hs_req->req.status)
776 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
777 hs_req->req.actual);
778
779 /* Free bounce buffer */
780 kfree(hs_req->req.buf);
781
782 hs_req->req.buf = hs_req->saved_req_buf;
783 hs_req->saved_req_buf = NULL;
784}
785
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500786static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100787 gfp_t gfp_flags)
788{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500789 struct dwc2_hsotg_req *hs_req = our_req(req);
790 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600791 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100792 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100793 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100794
795 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
796 ep->name, req, req->length, req->buf, req->no_interrupt,
797 req->zero, req->short_not_ok);
798
Gregory Herrero7ababa92015-04-29 22:09:08 +0200799 /* Prevent new request submission when controller is suspended */
800 if (hs->lx_state == DWC2_L2) {
801 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
802 __func__);
803 return -EAGAIN;
804 }
805
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100806 /* initialise status of the request */
807 INIT_LIST_HEAD(&hs_req->queue);
808 req->actual = 0;
809 req->status = -EINPROGRESS;
810
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500811 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100812 if (ret)
813 return ret;
814
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100815 /* if we're using DMA, sync the buffers as necessary */
816 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500817 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100818 if (ret)
819 return ret;
820 }
821
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100822 first = list_empty(&hs_ep->queue);
823 list_add_tail(&hs_req->queue, &hs_ep->queue);
824
825 if (first)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500826 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100827
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100828 return 0;
829}
830
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500831static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200832 gfp_t gfp_flags)
833{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500834 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600835 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200836 unsigned long flags = 0;
837 int ret = 0;
838
839 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500840 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200841 spin_unlock_irqrestore(&hs->lock, flags);
842
843 return ret;
844}
845
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500846static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100847 struct usb_request *req)
848{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500849 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100850
851 kfree(hs_req);
852}
853
854/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500855 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100856 * @ep: The endpoint the request was on.
857 * @req: The request completed.
858 *
859 * Called on completion of any requests the driver itself
860 * submitted that need cleaning up.
861 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500862static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100863 struct usb_request *req)
864{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500865 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600866 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100867
868 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
869
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500870 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100871}
872
873/**
874 * ep_from_windex - convert control wIndex value to endpoint
875 * @hsotg: The driver state.
876 * @windex: The control request wIndex field (in host order).
877 *
878 * Convert the given wIndex into a pointer to an driver endpoint
879 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200880 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500881static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100882 u32 windex)
883{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500884 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100885 int dir = (windex & USB_DIR_IN) ? 1 : 0;
886 int idx = windex & 0x7F;
887
888 if (windex >= 0x100)
889 return NULL;
890
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200891 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100892 return NULL;
893
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100894 ep = index_to_ep(hsotg, idx, dir);
895
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100896 if (idx && ep->dir_in != dir)
897 return NULL;
898
899 return ep;
900}
901
902/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500903 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100904 * @hsotg: The driver state.
905 * @testmode: requested usb test mode
906 * Enable usb Test Mode requested by the Host.
907 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500908int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100909{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300910 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100911
912 dctl &= ~DCTL_TSTCTL_MASK;
913 switch (testmode) {
914 case TEST_J:
915 case TEST_K:
916 case TEST_SE0_NAK:
917 case TEST_PACKET:
918 case TEST_FORCE_EN:
919 dctl |= testmode << DCTL_TSTCTL_SHIFT;
920 break;
921 default:
922 return -EINVAL;
923 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300924 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100925 return 0;
926}
927
928/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500929 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100930 * @hsotg: The device state
931 * @ep: Endpoint 0
932 * @buff: Buffer for request
933 * @length: Length of reply.
934 *
935 * Create a request and queue it on the given endpoint. This is useful as
936 * an internal method of sending replies to certain control requests, etc.
937 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500938static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
939 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100940 void *buff,
941 int length)
942{
943 struct usb_request *req;
944 int ret;
945
946 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
947
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500948 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100949 hsotg->ep0_reply = req;
950 if (!req) {
951 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
952 return -ENOMEM;
953 }
954
955 req->buf = hsotg->ep0_buff;
956 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100957 /*
958 * zero flag is for sending zlp in DATA IN stage. It has no impact on
959 * STATUS stage.
960 */
961 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500962 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100963
964 if (length)
965 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100966
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500967 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100968 if (ret) {
969 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
970 return ret;
971 }
972
973 return 0;
974}
975
976/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500977 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100978 * @hsotg: The device state
979 * @ctrl: USB control request
980 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500981static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100982 struct usb_ctrlrequest *ctrl)
983{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500984 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
985 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100986 __le16 reply;
987 int ret;
988
989 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
990
991 if (!ep0->dir_in) {
992 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
993 return -EINVAL;
994 }
995
996 switch (ctrl->bRequestType & USB_RECIP_MASK) {
997 case USB_RECIP_DEVICE:
998 reply = cpu_to_le16(0); /* bit 0 => self powered,
999 * bit 1 => remote wakeup */
1000 break;
1001
1002 case USB_RECIP_INTERFACE:
1003 /* currently, the data result should be zero */
1004 reply = cpu_to_le16(0);
1005 break;
1006
1007 case USB_RECIP_ENDPOINT:
1008 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1009 if (!ep)
1010 return -ENOENT;
1011
1012 reply = cpu_to_le16(ep->halted ? 1 : 0);
1013 break;
1014
1015 default:
1016 return 0;
1017 }
1018
1019 if (le16_to_cpu(ctrl->wLength) != 2)
1020 return -EINVAL;
1021
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001022 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001023 if (ret) {
1024 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1025 return ret;
1026 }
1027
1028 return 1;
1029}
1030
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001031static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001032
1033/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001034 * get_ep_head - return the first request on the endpoint
1035 * @hs_ep: The controller endpoint to get
1036 *
1037 * Get the first request on the endpoint.
1038 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001039static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001040{
1041 if (list_empty(&hs_ep->queue))
1042 return NULL;
1043
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001044 return list_first_entry(&hs_ep->queue, struct dwc2_hsotg_req, queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001045}
1046
1047/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001048 * dwc2_gadget_start_next_request - Starts next request from ep queue
1049 * @hs_ep: Endpoint structure
1050 *
1051 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1052 * in its handler. Hence we need to unmask it here to be able to do
1053 * resynchronization.
1054 */
1055static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1056{
1057 u32 mask;
1058 struct dwc2_hsotg *hsotg = hs_ep->parent;
1059 int dir_in = hs_ep->dir_in;
1060 struct dwc2_hsotg_req *hs_req;
1061 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1062
1063 if (!list_empty(&hs_ep->queue)) {
1064 hs_req = get_ep_head(hs_ep);
1065 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1066 return;
1067 }
1068 if (!hs_ep->isochronous)
1069 return;
1070
1071 if (dir_in) {
1072 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1073 __func__);
1074 } else {
1075 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1076 __func__);
1077 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1078 mask |= DOEPMSK_OUTTKNEPDISMSK;
1079 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1080 }
1081}
1082
1083/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001084 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001085 * @hsotg: The device state
1086 * @ctrl: USB control request
1087 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001088static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001089 struct usb_ctrlrequest *ctrl)
1090{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001091 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1092 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001093 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001094 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001095 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001096 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001097 u32 recip;
1098 u32 wValue;
1099 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001100
1101 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1102 __func__, set ? "SET" : "CLEAR");
1103
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001104 wValue = le16_to_cpu(ctrl->wValue);
1105 wIndex = le16_to_cpu(ctrl->wIndex);
1106 recip = ctrl->bRequestType & USB_RECIP_MASK;
1107
1108 switch (recip) {
1109 case USB_RECIP_DEVICE:
1110 switch (wValue) {
1111 case USB_DEVICE_TEST_MODE:
1112 if ((wIndex & 0xff) != 0)
1113 return -EINVAL;
1114 if (!set)
1115 return -EINVAL;
1116
1117 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001118 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001119 if (ret) {
1120 dev_err(hsotg->dev,
1121 "%s: failed to send reply\n", __func__);
1122 return ret;
1123 }
1124 break;
1125 default:
1126 return -ENOENT;
1127 }
1128 break;
1129
1130 case USB_RECIP_ENDPOINT:
1131 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001132 if (!ep) {
1133 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001134 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001135 return -ENOENT;
1136 }
1137
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001138 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001139 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001140 halted = ep->halted;
1141
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001142 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001143
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001144 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001145 if (ret) {
1146 dev_err(hsotg->dev,
1147 "%s: failed to send reply\n", __func__);
1148 return ret;
1149 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001150
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001151 /*
1152 * we have to complete all requests for ep if it was
1153 * halted, and the halt was cleared by CLEAR_FEATURE
1154 */
1155
1156 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001157 /*
1158 * If we have request in progress,
1159 * then complete it
1160 */
1161 if (ep->req) {
1162 hs_req = ep->req;
1163 ep->req = NULL;
1164 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001165 if (hs_req->req.complete) {
1166 spin_unlock(&hsotg->lock);
1167 usb_gadget_giveback_request(
1168 &ep->ep, &hs_req->req);
1169 spin_lock(&hsotg->lock);
1170 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001171 }
1172
1173 /* If we have pending request, then start it */
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001174 if (!ep->req) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001175 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001176 }
1177 }
1178
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001179 break;
1180
1181 default:
1182 return -ENOENT;
1183 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001184 break;
1185 default:
1186 return -ENOENT;
1187 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001188 return 1;
1189}
1190
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001191static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001192
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001193/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001194 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001195 * @hsotg: The device state
1196 *
1197 * Set stall for ep0 as response for setup request.
1198 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001199static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001200{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001201 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001202 u32 reg;
1203 u32 ctrl;
1204
1205 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1206 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1207
1208 /*
1209 * DxEPCTL_Stall will be cleared by EP once it has
1210 * taken effect, so no need to clear later.
1211 */
1212
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001213 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001214 ctrl |= DXEPCTL_STALL;
1215 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001216 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001217
1218 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001219 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001220 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001221
1222 /*
1223 * complete won't be called, so we enqueue
1224 * setup request here
1225 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001226 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001227}
1228
1229/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001230 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001231 * @hsotg: The device state
1232 * @ctrl: The control request received
1233 *
1234 * The controller has received the SETUP phase of a control request, and
1235 * needs to work out what to do next (and whether to pass it on to the
1236 * gadget driver).
1237 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001238static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001239 struct usb_ctrlrequest *ctrl)
1240{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001241 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001242 int ret = 0;
1243 u32 dcfg;
1244
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001245 dev_dbg(hsotg->dev,
1246 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1247 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1248 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001249
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001250 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001251 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001252 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1253 } else if (ctrl->bRequestType & USB_DIR_IN) {
1254 ep0->dir_in = 1;
1255 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1256 } else {
1257 ep0->dir_in = 0;
1258 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1259 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001260
1261 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1262 switch (ctrl->bRequest) {
1263 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001264 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001265 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001266 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001267 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1268 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001269 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001270
1271 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1272
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001273 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001274 return;
1275
1276 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001277 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001278 break;
1279
1280 case USB_REQ_CLEAR_FEATURE:
1281 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001282 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001283 break;
1284 }
1285 }
1286
1287 /* as a fallback, try delivering it to the driver to deal with */
1288
1289 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001290 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001291 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001292 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001293 if (ret < 0)
1294 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1295 }
1296
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001297 /*
1298 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001299 * so respond with a STALL for the status stage to indicate failure.
1300 */
1301
Robert Baldygac9f721b2014-01-14 08:36:00 +01001302 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001303 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001304}
1305
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001306/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001307 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001308 * @ep: The endpoint the request was on.
1309 * @req: The request completed.
1310 *
1311 * Called on completion of any requests the driver itself submitted for
1312 * EP0 setup packets
1313 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001314static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001315 struct usb_request *req)
1316{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001317 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001318 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001319
1320 if (req->status < 0) {
1321 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1322 return;
1323 }
1324
Robert Baldyga93f599f2013-11-21 13:49:17 +01001325 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001326 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001327 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001328 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001329 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001330 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001331}
1332
1333/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001334 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001335 * @hsotg: The device state.
1336 *
1337 * Enqueue a request on EP0 if necessary to received any SETUP packets
1338 * received from the host.
1339 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001340static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001341{
1342 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001343 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001344 int ret;
1345
1346 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1347
1348 req->zero = 0;
1349 req->length = 8;
1350 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001351 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001352
1353 if (!list_empty(&hs_req->queue)) {
1354 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1355 return;
1356 }
1357
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001358 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001359 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001360 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001361
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001362 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001363 if (ret < 0) {
1364 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001365 /*
1366 * Don't think there's much we can do other than watch the
1367 * driver fail.
1368 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001369 }
1370}
1371
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001372static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1373 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001374{
1375 u32 ctrl;
1376 u8 index = hs_ep->index;
1377 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1378 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1379
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001380 if (hs_ep->dir_in)
1381 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
1382 index);
1383 else
1384 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
1385 index);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001386
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001387 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1388 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1389 epsiz_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001390
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001391 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001392 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1393 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1394 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001395 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001396}
1397
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001398/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001399 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001400 * @hsotg: The device state.
1401 * @hs_ep: The endpoint the request was on.
1402 * @hs_req: The request to complete.
1403 * @result: The result code (0 => Ok, otherwise errno)
1404 *
1405 * The given request has finished, so call the necessary completion
1406 * if it has one and then look to see if we can start a new request
1407 * on the endpoint.
1408 *
1409 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001410 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001411static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1412 struct dwc2_hsotg_ep *hs_ep,
1413 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001414 int result)
1415{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001416
1417 if (!hs_req) {
1418 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1419 return;
1420 }
1421
1422 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1423 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1424
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001425 /*
1426 * only replace the status if we've not already set an error
1427 * from a previous transaction
1428 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001429
1430 if (hs_req->req.status == -EINPROGRESS)
1431 hs_req->req.status = result;
1432
Yunzhi Li44583fe2015-09-29 12:25:01 +02001433 if (using_dma(hsotg))
1434 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1435
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001436 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001437
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001438 hs_ep->req = NULL;
1439 list_del_init(&hs_req->queue);
1440
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001441 /*
1442 * call the complete request with the locks off, just in case the
1443 * request tries to queue more work for this endpoint.
1444 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001445
1446 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001447 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001448 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001449 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001450 }
1451
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001452 /*
1453 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001454 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001455 * so be careful when doing this.
1456 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001457
1458 if (!hs_ep->req && result >= 0) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001459 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001460 }
1461}
1462
1463/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001464 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001465 * @hsotg: The device state.
1466 * @ep_idx: The endpoint index for the data
1467 * @size: The size of data in the fifo, in bytes
1468 *
1469 * The FIFO status shows there is data to read from the FIFO for a given
1470 * endpoint, so sort out whether we need to read the data into a request
1471 * that has been made for that endpoint.
1472 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001473static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001474{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001475 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
1476 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001477 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001478 int to_read;
1479 int max_req;
1480 int read_ptr;
1481
Lukasz Majewski22258f42012-06-14 10:02:24 +02001482
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001483 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001484 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001485 int ptr;
1486
Robert Baldyga6b448af42014-12-16 11:51:44 +01001487 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001488 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001489 __func__, size, ep_idx, epctl);
1490
1491 /* dump the data from the FIFO, we've nothing we can do */
1492 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001493 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001494
1495 return;
1496 }
1497
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001498 to_read = size;
1499 read_ptr = hs_req->req.actual;
1500 max_req = hs_req->req.length - read_ptr;
1501
Ben Dooksa33e7132010-07-19 09:40:49 +01001502 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1503 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1504
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001505 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001506 /*
1507 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001508 * to deal with in this request.
1509 */
1510
1511 /* currently we don't deal this */
1512 WARN_ON_ONCE(1);
1513 }
1514
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001515 hs_ep->total_data += to_read;
1516 hs_req->req.actual += to_read;
1517 to_read = DIV_ROUND_UP(to_read, 4);
1518
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001519 /*
1520 * note, we might over-write the buffer end by 3 bytes depending on
1521 * alignment of the data.
1522 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001523 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001524}
1525
1526/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001527 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001528 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001529 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001530 *
1531 * Generate a zero-length IN packet request for terminating a SETUP
1532 * transaction.
1533 *
1534 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001535 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001536 * the TxFIFO.
1537 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001538static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001539{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001540 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001541 hsotg->eps_out[0]->dir_in = dir_in;
1542 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001543
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001544 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001545}
1546
Roman Bacikec1f9d92015-09-10 18:13:43 -07001547static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
1548 u32 epctl_reg)
1549{
1550 u32 ctrl;
1551
1552 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
1553 if (ctrl & DXEPCTL_EOFRNUM)
1554 ctrl |= DXEPCTL_SETEVENFR;
1555 else
1556 ctrl |= DXEPCTL_SETODDFR;
1557 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
1558}
1559
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001560/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001561 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001562 * @hsotg: The device instance
1563 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001564 *
1565 * The RXFIFO has delivered an OutDone event, which means that the data
1566 * transfer for an OUT endpoint has been completed, either by a short
1567 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001568 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001569static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001570{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001571 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001572 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
1573 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001574 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001575 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001576 int result = 0;
1577
1578 if (!hs_req) {
1579 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1580 return;
1581 }
1582
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001583 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1584 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001585 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1586 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001587 return;
1588 }
1589
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001590 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001591 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001592
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001593 /*
1594 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001595 * is left in the endpoint size register and then working it
1596 * out from the amount we loaded for the transfer.
1597 *
1598 * We need to do this as DMA pointers are always 32bit aligned
1599 * so may overshoot/undershoot the transfer.
1600 */
1601
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001602 size_done = hs_ep->size_loaded - size_left;
1603 size_done += hs_ep->last_load;
1604
1605 req->actual = size_done;
1606 }
1607
Ben Dooksa33e7132010-07-19 09:40:49 +01001608 /* if there is more request to do, schedule new transfer */
1609 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001610 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01001611 return;
1612 }
1613
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001614 if (req->actual < req->length && req->short_not_ok) {
1615 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1616 __func__, req->actual, req->length);
1617
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001618 /*
1619 * todo - what should we return here? there's no one else
1620 * even bothering to check the status.
1621 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001622 }
1623
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001624 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1625 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001626 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001627 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001628 }
1629
Roman Bacikec1f9d92015-09-10 18:13:43 -07001630 /*
1631 * Slave mode OUT transfers do not go through XferComplete so
1632 * adjust the ISOC parity here.
1633 */
1634 if (!using_dma(hsotg)) {
1635 hs_ep->has_correct_parity = 1;
1636 if (hs_ep->isochronous && hs_ep->interval == 1)
1637 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
1638 }
1639
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001640 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001641}
1642
1643/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001644 * dwc2_hsotg_read_frameno - read current frame number
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001645 * @hsotg: The device instance
1646 *
1647 * Return the current frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001648 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001649static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001650{
1651 u32 dsts;
1652
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001653 dsts = dwc2_readl(hsotg->regs + DSTS);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001654 dsts &= DSTS_SOFFN_MASK;
1655 dsts >>= DSTS_SOFFN_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001656
1657 return dsts;
1658}
1659
1660/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001661 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001662 * @hsotg: The device instance
1663 *
1664 * The IRQ handler has detected that the RX FIFO has some data in it
1665 * that requires processing, so find out what is in there and do the
1666 * appropriate read.
1667 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001668 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001669 * chunks, so if you have x packets received on an endpoint you'll get x
1670 * FIFO events delivered, each with a packet's worth of data in it.
1671 *
1672 * When using DMA, we should not be processing events from the RXFIFO
1673 * as the actual data should be sent to the memory directly and we turn
1674 * on the completion interrupts to get notifications of transfer completion.
1675 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001676static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001677{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001678 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001679 u32 epnum, status, size;
1680
1681 WARN_ON(using_dma(hsotg));
1682
Dinh Nguyen47a16852014-04-14 14:13:34 -07001683 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1684 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001685
Dinh Nguyen47a16852014-04-14 14:13:34 -07001686 size = grxstsr & GRXSTS_BYTECNT_MASK;
1687 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001688
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01001689 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001690 __func__, grxstsr, size, epnum);
1691
Dinh Nguyen47a16852014-04-14 14:13:34 -07001692 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1693 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1694 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001695 break;
1696
Dinh Nguyen47a16852014-04-14 14:13:34 -07001697 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001698 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001699 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001700
1701 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001702 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001703 break;
1704
Dinh Nguyen47a16852014-04-14 14:13:34 -07001705 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001706 dev_dbg(hsotg->dev,
1707 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001708 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001709 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001710 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001711 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001712 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1713 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1714 */
1715 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001716 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001717 break;
1718
Dinh Nguyen47a16852014-04-14 14:13:34 -07001719 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001720 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001721 break;
1722
Dinh Nguyen47a16852014-04-14 14:13:34 -07001723 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001724 dev_dbg(hsotg->dev,
1725 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001726 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001727 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001728
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001729 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1730
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001731 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001732 break;
1733
1734 default:
1735 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1736 __func__, grxstsr);
1737
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001738 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001739 break;
1740 }
1741}
1742
1743/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001744 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001745 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001746 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001747static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001748{
1749 switch (mps) {
1750 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001751 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001752 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001753 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001754 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001755 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001756 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001757 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001758 }
1759
1760 /* bad max packet size, warn and return invalid result */
1761 WARN_ON(1);
1762 return (u32)-1;
1763}
1764
1765/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001766 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001767 * @hsotg: The driver state.
1768 * @ep: The index number of the endpoint
1769 * @mps: The maximum packet size in bytes
1770 *
1771 * Configure the maximum packet size for the given endpoint, updating
1772 * the hardware control registers to reflect this.
1773 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001774static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001775 unsigned int ep, unsigned int mps, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001776{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001777 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001778 void __iomem *regs = hsotg->regs;
1779 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001780 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001781 u32 reg;
1782
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001783 hs_ep = index_to_ep(hsotg, ep, dir_in);
1784 if (!hs_ep)
1785 return;
1786
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001787 if (ep == 0) {
1788 /* EP0 is a special case */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001789 mpsval = dwc2_hsotg_ep0_mps(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001790 if (mpsval > 3)
1791 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001792 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001793 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001794 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001795 mpsval = mps & DXEPCTL_MPS_MASK;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001796 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001797 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001798 mcval = ((mps >> 11) & 0x3) + 1;
1799 hs_ep->mc = mcval;
1800 if (mcval > 3)
1801 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001802 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001803 }
1804
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001805 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001806 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001807 reg &= ~DXEPCTL_MPS_MASK;
1808 reg |= mpsval;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001809 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001810 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001811 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001812 reg &= ~DXEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001813 reg |= mpsval;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001814 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001815 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001816
1817 return;
1818
1819bad_mps:
1820 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1821}
1822
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001823/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001824 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001825 * @hsotg: The driver state
1826 * @idx: The index for the endpoint (0..15)
1827 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001828static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001829{
1830 int timeout;
1831 int val;
1832
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001833 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
1834 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001835
1836 /* wait until the fifo is flushed */
1837 timeout = 100;
1838
1839 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001840 val = dwc2_readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001841
Dinh Nguyen47a16852014-04-14 14:13:34 -07001842 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001843 break;
1844
1845 if (--timeout == 0) {
1846 dev_err(hsotg->dev,
1847 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1848 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02001849 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001850 }
1851
1852 udelay(1);
1853 }
1854}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001855
1856/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001857 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001858 * @hsotg: The driver state
1859 * @hs_ep: The driver endpoint to check.
1860 *
1861 * Check to see if there is a request that has data to send, and if so
1862 * make an attempt to write data into the FIFO.
1863 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001864static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
1865 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001866{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001867 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001868
Robert Baldygaafcf4162013-09-19 11:50:19 +02001869 if (!hs_ep->dir_in || !hs_req) {
1870 /**
1871 * if request is not enqueued, we disable interrupts
1872 * for endpoints, excepting ep0
1873 */
1874 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001875 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
Robert Baldygaafcf4162013-09-19 11:50:19 +02001876 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001877 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001878 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001879
1880 if (hs_req->req.actual < hs_req->req.length) {
1881 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1882 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001883 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001884 }
1885
1886 return 0;
1887}
1888
1889/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001890 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001891 * @hsotg: The device state.
1892 * @hs_ep: The endpoint that has just completed.
1893 *
1894 * An IN transfer has been completed, update the transfer's state and then
1895 * call the relevant completion routines.
1896 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001897static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
1898 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001899{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001900 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001901 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001902 int size_left, size_done;
1903
1904 if (!hs_req) {
1905 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1906 return;
1907 }
1908
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001909 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001910 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
1911 dev_dbg(hsotg->dev, "zlp packet sent\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001912 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001913 if (hsotg->test_mode) {
1914 int ret;
1915
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001916 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001917 if (ret < 0) {
1918 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
1919 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001920 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001921 return;
1922 }
1923 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001924 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001925 return;
1926 }
1927
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001928 /*
1929 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001930 * in the endpoint size register and then working it out from
1931 * the amount we loaded for the transfer.
1932 *
1933 * We do this even for DMA, as the transfer may have incremented
1934 * past the end of the buffer (DMA transfers are always 32bit
1935 * aligned).
1936 */
1937
Dinh Nguyen47a16852014-04-14 14:13:34 -07001938 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001939
1940 size_done = hs_ep->size_loaded - size_left;
1941 size_done += hs_ep->last_load;
1942
1943 if (hs_req->req.actual != size_done)
1944 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1945 __func__, hs_req->req.actual, size_done);
1946
1947 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001948 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1949 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001951 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1952 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001953 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001954 return;
1955 }
1956
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001957 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001958 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001959 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001960 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001961 /* transfer will be completed on next complete interrupt */
1962 return;
1963 }
1964
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001965 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
1966 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001967 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001968 return;
1969 }
1970
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001971 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001972}
1973
1974/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07001975 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
1976 * @hsotg: The device state.
1977 * @idx: Index of ep.
1978 * @dir_in: Endpoint direction 1-in 0-out.
1979 *
1980 * Reads for endpoint with given index and direction, by masking
1981 * epint_reg with coresponding mask.
1982 */
1983static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
1984 unsigned int idx, int dir_in)
1985{
1986 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1987 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
1988 u32 ints;
1989 u32 mask;
1990 u32 diepempmsk;
1991
1992 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1993 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
1994 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
1995 mask |= DXEPINT_SETUP_RCVD;
1996
1997 ints = dwc2_readl(hsotg->regs + epint_reg);
1998 ints &= mask;
1999 return ints;
2000}
2001
2002/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002003 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002004 * @hsotg: The driver state
2005 * @idx: The index for the endpoint (0..15)
2006 * @dir_in: Set if this is an IN endpoint
2007 *
2008 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002009 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002010static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002011 int dir_in)
2012{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002013 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002014 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2015 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2016 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002017 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002018 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002019
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002020 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002021 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002022
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002023 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002024 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002025
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002026 if (!hs_ep) {
2027 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
2028 __func__, idx, dir_in ? "in" : "out");
2029 return;
2030 }
2031
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002032 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2033 __func__, idx, dir_in ? "in" : "out", ints);
2034
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002035 /* Don't process XferCompl interrupt if it is a setup packet */
2036 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2037 ints &= ~DXEPINT_XFERCOMPL;
2038
Dinh Nguyen47a16852014-04-14 14:13:34 -07002039 if (ints & DXEPINT_XFERCOMPL) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002040 hs_ep->has_correct_parity = 1;
2041 if (hs_ep->isochronous && hs_ep->interval == 1)
2042 dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg);
Robert Baldyga1479e842013-10-09 08:41:57 +02002043
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002044 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002045 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002046 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2047 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002048
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002049 /*
2050 * we get OutDone from the FIFO, so we only need to look
2051 * at completing IN requests here
2052 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002053 if (dir_in) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002054 dwc2_hsotg_complete_in(hsotg, hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002055
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002056 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002057 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002058 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002059 /*
2060 * We're using DMA, we need to fire an OutDone here
2061 * as we ignore the RXFIFO.
2062 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002063
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002064 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002065 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002066 }
2067
Dinh Nguyen47a16852014-04-14 14:13:34 -07002068 if (ints & DXEPINT_EPDISBLD) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002069 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002070
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002071 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002072 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002073
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002074 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002075
Dinh Nguyen47a16852014-04-14 14:13:34 -07002076 if ((epctl & DXEPCTL_STALL) &&
2077 (epctl & DXEPCTL_EPTYPE_BULK)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002078 int dctl = dwc2_readl(hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002079
Dinh Nguyen47a16852014-04-14 14:13:34 -07002080 dctl |= DCTL_CGNPINNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002081 dwc2_writel(dctl, hsotg->regs + DCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002082 }
2083 }
2084 }
2085
Dinh Nguyen47a16852014-04-14 14:13:34 -07002086 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002087 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002088
Dinh Nguyen47a16852014-04-14 14:13:34 -07002089 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002090 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2091
2092 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002093 /*
2094 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002095 * setup packet. In non-DMA mode we'd get this
2096 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002097 * the setup here.
2098 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002099
2100 if (dir_in)
2101 WARN_ON_ONCE(1);
2102 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002103 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002104 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002105 }
2106
Dinh Nguyen47a16852014-04-14 14:13:34 -07002107 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002108 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002109
Robert Baldyga1479e842013-10-09 08:41:57 +02002110 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002111 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002112 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002113 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2114 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002115 }
2116
2117 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002118 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002119 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2120 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002121 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002122
2123 /* FIFO has space or is empty (see GAHBCFG) */
2124 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002125 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002126 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2127 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002128 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002129 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002130 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002131 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002132}
2133
2134/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002135 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002136 * @hsotg: The device state.
2137 *
2138 * Handle updating the device settings after the enumeration phase has
2139 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002140 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002141static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002142{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002143 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09002144 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002145
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002146 /*
2147 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002148 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002149 * we connected at.
2150 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002151
2152 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2153
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002154 /*
2155 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002156 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002157 * not advertise a 64byte MPS on EP0.
2158 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002159
2160 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01002161 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002162 case DSTS_ENUMSPD_FS:
2163 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002164 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002165 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002166 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002167 break;
2168
Dinh Nguyen47a16852014-04-14 14:13:34 -07002169 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002170 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002171 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002172 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002173 break;
2174
Dinh Nguyen47a16852014-04-14 14:13:34 -07002175 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002176 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002177 /*
2178 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002179 * moment, and the documentation seems to imply that it isn't
2180 * supported by the PHYs on some of the devices.
2181 */
2182 break;
2183 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002184 dev_info(hsotg->dev, "new device is %s\n",
2185 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002186
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002187 /*
2188 * we should now know the maximum packet size for an
2189 * endpoint, so set the endpoints to a default value.
2190 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002191
2192 if (ep0_mps) {
2193 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002194 /* Initialize ep0 for both in and out directions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002195 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
2196 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002197 for (i = 1; i < hsotg->num_of_eps; i++) {
2198 if (hsotg->eps_in[i])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002199 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002200 if (hsotg->eps_out[i])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002201 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002202 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002203 }
2204
2205 /* ensure after enumeration our EP0 is active */
2206
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002207 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002208
2209 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002210 dwc2_readl(hsotg->regs + DIEPCTL0),
2211 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002212}
2213
2214/**
2215 * kill_all_requests - remove all requests from the endpoint's queue
2216 * @hsotg: The device state.
2217 * @ep: The endpoint the requests may be on.
2218 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002219 *
2220 * Go through the requests on the given endpoint and mark them
2221 * completed with the given result code.
2222 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002223static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002224 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af42014-12-16 11:51:44 +01002225 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002226{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002227 struct dwc2_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002228 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002229
Robert Baldyga6b448af42014-12-16 11:51:44 +01002230 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002231
Robert Baldyga6b448af42014-12-16 11:51:44 +01002232 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002233 dwc2_hsotg_complete_request(hsotg, ep, req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002234 result);
Robert Baldyga6b448af42014-12-16 11:51:44 +01002235
Robert Baldygab203d0a2014-09-09 10:44:56 +02002236 if (!hsotg->dedicated_fifos)
2237 return;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002238 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002239 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002240 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002241}
2242
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002243/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002244 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002245 * @hsotg: The device state.
2246 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002247 * The device has been disconnected. Remove all current
2248 * transactions and signal the gadget driver that this
2249 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002250 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002251void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002252{
2253 unsigned ep;
2254
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002255 if (!hsotg->connected)
2256 return;
2257
2258 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002259 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002260
2261 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2262 if (hsotg->eps_in[ep])
2263 kill_all_requests(hsotg, hsotg->eps_in[ep],
2264 -ESHUTDOWN);
2265 if (hsotg->eps_out[ep])
2266 kill_all_requests(hsotg, hsotg->eps_out[ep],
2267 -ESHUTDOWN);
2268 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002269
2270 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02002271 hsotg->lx_state = DWC2_L3;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002272}
2273
2274/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002275 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002276 * @hsotg: The device state:
2277 * @periodic: True if this is a periodic FIFO interrupt
2278 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002279static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002280{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002281 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002282 int epno, ret;
2283
2284 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002285 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002286 ep = index_to_ep(hsotg, epno, 1);
2287
2288 if (!ep)
2289 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002290
2291 if (!ep->dir_in)
2292 continue;
2293
2294 if ((periodic && !ep->periodic) ||
2295 (!periodic && ep->periodic))
2296 continue;
2297
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002298 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002299 if (ret < 0)
2300 break;
2301 }
2302}
2303
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002304/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002305#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2306 GINTSTS_PTXFEMP | \
2307 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002308
2309/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002310 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002311 * @hsotg: The device state
2312 *
2313 * Issue a soft reset to the core, and await the core finishing it.
2314 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002315void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002316 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002317{
Gregory Herrero1ee69032015-09-29 12:08:27 +02002318 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002319 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002320 u32 usbcfg;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002321
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02002322 /* Kill any ep0 requests as controller will be reinitialized */
2323 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
2324
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002325 if (!is_usb_reset)
John Youn241729b2015-12-17 11:17:59 -08002326 if (dwc2_core_reset(hsotg))
Gregory Herrero86de4892015-09-29 12:08:21 +02002327 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002328
2329 /*
2330 * we must now enable ep0 ready for host detection and then
2331 * set configuration.
2332 */
2333
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002334 /* keep other bits untouched (so e.g. forced modes are not lost) */
2335 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2336 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
2337 GUSBCFG_HNPCAP);
2338
Lukasz Majewski308d7342012-05-04 14:17:05 +02002339 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01002340 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002341 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
2342 (val << GUSBCFG_USBTRDTIM_SHIFT);
2343 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002344
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002345 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002346
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002347 if (!is_usb_reset)
2348 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002349
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002350 dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002351
2352 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002353 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002354
2355 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002356 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02002357 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002358 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02002359 GINTSTS_USBRST | GINTSTS_RESETDET |
2360 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Roman Bacikec1f9d92015-09-10 18:13:43 -07002361 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
2362 GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02002363
2364 if (hsotg->core_params->external_id_pin_ctl <= 0)
2365 intmsk |= GINTSTS_CONIDSTSCHNG;
2366
2367 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002368
2369 if (using_dma(hsotg))
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002370 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2371 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
2372 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002373 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002374 dwc2_writel(((hsotg->dedicated_fifos) ?
2375 (GAHBCFG_NP_TXF_EMP_LVL |
2376 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2377 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002378
2379 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002380 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2381 * when we have no data to transfer. Otherwise we get being flooded by
2382 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002383 */
2384
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002385 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01002386 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002387 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
2388 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
2389 DIEPMSK_INTKNEPMISMSK,
2390 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002391
2392 /*
2393 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2394 * DMA mode we may need this.
2395 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002396 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002397 DIEPMSK_TIMEOUTMSK) : 0) |
2398 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
2399 DOEPMSK_SETUPMSK,
2400 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002401
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002402 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002403
2404 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002405 dwc2_readl(hsotg->regs + DIEPCTL0),
2406 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002407
2408 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002409 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002410
2411 /*
2412 * Enable the RXFIFO when in slave mode, as this is how we collect
2413 * the data. In DMA mode, we get events from the FIFO but also
2414 * things we cannot process, so do not use it.
2415 */
2416 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002417 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002418
2419 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002420 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2421 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002422
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002423 if (!is_usb_reset) {
2424 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2425 udelay(10); /* see openiboot */
2426 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2427 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02002428
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002429 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002430
2431 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002432 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002433 * writing to the EPCTL register..
2434 */
2435
2436 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002437 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002438 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002439
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002440 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002441 DXEPCTL_CNAK | DXEPCTL_EPENA |
2442 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002443 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002444
2445 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002446 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002447 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002448
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002449 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002450
2451 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002452 dwc2_readl(hsotg->regs + DIEPCTL0),
2453 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002454
2455 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002456 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
2457 if (!is_usb_reset)
2458 val |= DCTL_SFTDISCON;
2459 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002460
2461 /* must be at-least 3ms to allow bus to see disconnect */
2462 mdelay(3);
2463
Gregory Herrero065d3932015-09-22 15:16:54 +02002464 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002465}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002466
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002467static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002468{
2469 /* set the soft-disconnect bit */
2470 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2471}
2472
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002473void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002474{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002475 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002476 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002477}
2478
2479/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002480 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002481 * @irq: The IRQ number triggered
2482 * @pw: The pw value when registered the handler.
2483 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002484static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002485{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002486 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487 int retry_count = 8;
2488 u32 gintsts;
2489 u32 gintmsk;
2490
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07002491 if (!dwc2_is_device_mode(hsotg))
2492 return IRQ_NONE;
2493
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002494 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002495irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002496 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2497 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498
2499 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2500 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2501
2502 gintsts &= gintmsk;
2503
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02002504 if (gintsts & GINTSTS_RESETDET) {
2505 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
2506
2507 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
2508
2509 /* This event must be used only if controller is suspended */
2510 if (hsotg->lx_state == DWC2_L2) {
2511 dwc2_exit_hibernation(hsotg, true);
2512 hsotg->lx_state = DWC2_L0;
2513 }
2514 }
2515
2516 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
2517
2518 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
2519 u32 connected = hsotg->connected;
2520
2521 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
2522 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2523 dwc2_readl(hsotg->regs + GNPTXSTS));
2524
2525 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
2526
2527 /* Report disconnection if it is not already done. */
2528 dwc2_hsotg_disconnect(hsotg);
2529
2530 if (usb_status & GOTGCTL_BSESVLD && connected)
2531 dwc2_hsotg_core_init_disconnected(hsotg, true);
2532 }
2533
Dinh Nguyen47a16852014-04-14 14:13:34 -07002534 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002535 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002536
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002537 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002538 }
2539
Dinh Nguyen47a16852014-04-14 14:13:34 -07002540 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002541 u32 daint = dwc2_readl(hsotg->regs + DAINT);
2542 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02002543 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002544 int ep;
2545
Robert Baldyga7e804652013-09-19 11:50:20 +02002546 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002547 daint_out = daint >> DAINT_OUTEP_SHIFT;
2548 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002549
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002550 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2551
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002552 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2553 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002554 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002555 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002556 }
2557
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002558 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
2559 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002560 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002561 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002562 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002563 }
2564
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002565 /* check both FIFOs */
2566
Dinh Nguyen47a16852014-04-14 14:13:34 -07002567 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002568 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2569
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002570 /*
2571 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002572 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002573 * it needs re-enabling
2574 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002575
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002576 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
2577 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002578 }
2579
Dinh Nguyen47a16852014-04-14 14:13:34 -07002580 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002581 dev_dbg(hsotg->dev, "PTxFEmp\n");
2582
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002583 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002584
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002585 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
2586 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002587 }
2588
Dinh Nguyen47a16852014-04-14 14:13:34 -07002589 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002590 /*
2591 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002592 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002593 * set.
2594 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002595
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002596 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002597 }
2598
Dinh Nguyen47a16852014-04-14 14:13:34 -07002599 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002600 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002601 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002602 }
2603
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002604 /*
2605 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002606 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002607 * the occurrence.
2608 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002609
Dinh Nguyen47a16852014-04-14 14:13:34 -07002610 if (gintsts & GINTSTS_GOUTNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002611 dev_info(hsotg->dev, "GOUTNakEff triggered\n");
2612
Gregory Herrero3be99cd2015-12-07 12:07:31 +01002613 __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002614
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002615 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002616 }
2617
Dinh Nguyen47a16852014-04-14 14:13:34 -07002618 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002619 dev_info(hsotg->dev, "GINNakEff triggered\n");
2620
Gregory Herrero3be99cd2015-12-07 12:07:31 +01002621 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002622
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002623 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002624 }
2625
Roman Bacikec1f9d92015-09-10 18:13:43 -07002626 if (gintsts & GINTSTS_INCOMPL_SOIN) {
2627 u32 idx, epctl_reg;
2628 struct dwc2_hsotg_ep *hs_ep;
2629
2630 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOIN\n", __func__);
2631 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
2632 hs_ep = hsotg->eps_in[idx];
2633
2634 if (!hs_ep->isochronous || hs_ep->has_correct_parity)
2635 continue;
2636
2637 epctl_reg = DIEPCTL(idx);
2638 dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg);
2639 }
2640 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
2641 }
2642
2643 if (gintsts & GINTSTS_INCOMPL_SOOUT) {
2644 u32 idx, epctl_reg;
2645 struct dwc2_hsotg_ep *hs_ep;
2646
2647 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
2648 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
2649 hs_ep = hsotg->eps_out[idx];
2650
2651 if (!hs_ep->isochronous || hs_ep->has_correct_parity)
2652 continue;
2653
2654 epctl_reg = DOEPCTL(idx);
2655 dwc2_hsotg_change_ep_iso_parity(hsotg, epctl_reg);
2656 }
2657 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
2658 }
2659
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002660 /*
2661 * if we've had fifo events, we should try and go around the
2662 * loop again to see if there's any point in returning yet.
2663 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002664
2665 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2666 goto irq_retry;
2667
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002668 spin_unlock(&hsotg->lock);
2669
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002670 return IRQ_HANDLED;
2671}
2672
2673/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002674 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002675 * @ep: The USB endpint to configure
2676 * @desc: The USB endpoint descriptor to configure with.
2677 *
2678 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002679 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002680static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002681 const struct usb_endpoint_descriptor *desc)
2682{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002683 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002684 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002685 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002686 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002687 u32 epctrl_reg;
2688 u32 epctrl;
2689 u32 mps;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002690 unsigned int dir_in;
2691 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02002692 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002693
2694 dev_dbg(hsotg->dev,
2695 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2696 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2697 desc->wMaxPacketSize, desc->bInterval);
2698
2699 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07002700 if (index == 0) {
2701 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
2702 return -EINVAL;
2703 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002704
2705 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2706 if (dir_in != hs_ep->dir_in) {
2707 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2708 return -EINVAL;
2709 }
2710
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002711 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002712
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002713 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002714
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002715 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002716 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002717
2718 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2719 __func__, epctrl, epctrl_reg);
2720
Lukasz Majewski22258f42012-06-14 10:02:24 +02002721 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002722
Dinh Nguyen47a16852014-04-14 14:13:34 -07002723 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2724 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002725
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002726 /*
2727 * mark the endpoint as active, otherwise the core may ignore
2728 * transactions entirely for this endpoint
2729 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002730 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002731
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002732 /*
2733 * set the NAK status on the endpoint, otherwise we might try and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002734 * do something with data that we've yet got a request to process
2735 * since the RXFIFO will take data for an endpoint even if the
2736 * size register hasn't been set.
2737 */
2738
Dinh Nguyen47a16852014-04-14 14:13:34 -07002739 epctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002740
2741 /* update the endpoint state */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002742 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002743
2744 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02002745 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002746 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02002747 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02002748 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002749
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002750 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
2751 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002752 epctrl |= DXEPCTL_EPTYPE_ISO;
2753 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02002754 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07002755 hs_ep->interval = 1 << (desc->bInterval - 1);
Robert Baldyga1479e842013-10-09 08:41:57 +02002756 if (dir_in)
2757 hs_ep->periodic = 1;
2758 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002759
2760 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002761 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002762 break;
2763
2764 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02002765 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002766 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002767
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07002768 if (hsotg->gadget.speed == USB_SPEED_HIGH)
2769 hs_ep->interval = 1 << (desc->bInterval - 1);
2770
Dinh Nguyen47a16852014-04-14 14:13:34 -07002771 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002772 break;
2773
2774 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07002775 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002776 break;
2777 }
2778
Mian Yousaf Kaukab4556e122015-01-09 13:39:05 +01002779 /* If fifo is already allocated for this ep */
2780 if (hs_ep->fifo_index) {
2781 size = hs_ep->ep.maxpacket * hs_ep->mc;
2782 /* If bigger fifo is required deallocate current one */
2783 if (size > hs_ep->fifo_size) {
2784 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
2785 hs_ep->fifo_index = 0;
2786 hs_ep->fifo_size = 0;
2787 }
2788 }
2789
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002790 /*
2791 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01002792 * a unique tx-fifo even if it is non-periodic.
2793 */
Mian Yousaf Kaukab4556e122015-01-09 13:39:05 +01002794 if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002795 u32 fifo_index = 0;
2796 u32 fifo_size = UINT_MAX;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002797 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01002798 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02002799 if (hsotg->fifo_map & (1<<i))
2800 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002801 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Robert Baldygab203d0a2014-09-09 10:44:56 +02002802 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
2803 if (val < size)
2804 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002805 /* Search for smallest acceptable fifo */
2806 if (val < fifo_size) {
2807 fifo_size = val;
2808 fifo_index = i;
2809 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02002810 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002811 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01002812 dev_err(hsotg->dev,
2813 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302814 ret = -ENOMEM;
2815 goto error;
2816 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002817 hsotg->fifo_map |= 1 << fifo_index;
2818 epctrl |= DXEPCTL_TXFNUM(fifo_index);
2819 hs_ep->fifo_index = fifo_index;
2820 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002821 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002822
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002823 /* for non control endpoints, set PID to D0 */
2824 if (index)
Dinh Nguyen47a16852014-04-14 14:13:34 -07002825 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002826
2827 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
2828 __func__, epctrl);
2829
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002830 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002831 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002832 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002833
2834 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002835 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002836
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05302837error:
Lukasz Majewski22258f42012-06-14 10:02:24 +02002838 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02002839 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002840}
2841
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002842/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002843 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002844 * @ep: The endpoint to disable.
2845 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002846static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002847{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002848 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002849 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002850 int dir_in = hs_ep->dir_in;
2851 int index = hs_ep->index;
2852 unsigned long flags;
2853 u32 epctrl_reg;
2854 u32 ctrl;
2855
Marek Szyprowski1e011292014-09-09 10:44:54 +02002856 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002857
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002858 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002859 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
2860 return -EINVAL;
2861 }
2862
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002863 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002864
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002865 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002866
Robert Baldygab203d0a2014-09-09 10:44:56 +02002867 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
2868 hs_ep->fifo_index = 0;
2869 hs_ep->fifo_size = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002870
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002871 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07002872 ctrl &= ~DXEPCTL_EPENA;
2873 ctrl &= ~DXEPCTL_USBACTEP;
2874 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002875
2876 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002877 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002878
2879 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002880 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002881
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01002882 /* terminate all requests with shutdown */
2883 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
2884
Lukasz Majewski22258f42012-06-14 10:02:24 +02002885 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002886 return 0;
2887}
2888
2889/**
2890 * on_list - check request is on the given endpoint
2891 * @ep: The endpoint to check.
2892 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002893 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002894static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002895{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002896 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002897
2898 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
2899 if (req == test)
2900 return true;
2901 }
2902
2903 return false;
2904}
2905
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02002906static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
2907 u32 bit, u32 timeout)
2908{
2909 u32 i;
2910
2911 for (i = 0; i < timeout; i++) {
2912 if (dwc2_readl(hs_otg->regs + reg) & bit)
2913 return 0;
2914 udelay(1);
2915 }
2916
2917 return -ETIMEDOUT;
2918}
2919
2920static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
2921 struct dwc2_hsotg_ep *hs_ep)
2922{
2923 u32 epctrl_reg;
2924 u32 epint_reg;
2925
2926 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
2927 DOEPCTL(hs_ep->index);
2928 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
2929 DOEPINT(hs_ep->index);
2930
2931 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
2932 hs_ep->name);
2933 if (hs_ep->dir_in) {
2934 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
2935 /* Wait for Nak effect */
2936 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
2937 DXEPINT_INEPNAKEFF, 100))
2938 dev_warn(hsotg->dev,
2939 "%s: timeout DIEPINT.NAKEFF\n", __func__);
2940 } else {
Vardan Mikayelyan6b58cb02016-05-25 18:07:02 -07002941 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
2942 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02002943
2944 /* Wait for global nak to take effect */
2945 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
Du, Changbin0676c7e2015-12-04 15:38:23 +08002946 GINTSTS_GOUTNAKEFF, 100))
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02002947 dev_warn(hsotg->dev,
Du, Changbin0676c7e2015-12-04 15:38:23 +08002948 "%s: timeout GINTSTS.GOUTNAKEFF\n", __func__);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02002949 }
2950
2951 /* Disable ep */
2952 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
2953
2954 /* Wait for ep to be disabled */
2955 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
2956 dev_warn(hsotg->dev,
2957 "%s: timeout DOEPCTL.EPDisable\n", __func__);
2958
2959 if (hs_ep->dir_in) {
2960 if (hsotg->dedicated_fifos) {
2961 dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) |
2962 GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL);
2963 /* Wait for fifo flush */
2964 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
2965 GRSTCTL_TXFFLSH, 100))
2966 dev_warn(hsotg->dev,
2967 "%s: timeout flushing fifos\n",
2968 __func__);
2969 }
2970 /* TODO: Flush shared tx fifo */
2971 } else {
2972 /* Remove global NAKs */
Du, Changbin0676c7e2015-12-04 15:38:23 +08002973 __bic32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02002974 }
2975}
2976
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002977/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002978 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002979 * @ep: The endpoint to dequeue.
2980 * @req: The request to be removed from a queue.
2981 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002982static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002983{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002984 struct dwc2_hsotg_req *hs_req = our_req(req);
2985 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002986 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002987 unsigned long flags;
2988
Marek Szyprowski1e011292014-09-09 10:44:54 +02002989 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002990
Lukasz Majewski22258f42012-06-14 10:02:24 +02002991 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002992
2993 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002994 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002995 return -EINVAL;
2996 }
2997
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02002998 /* Dequeue already started request */
2999 if (req == &hs_ep->req->req)
3000 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
3001
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003002 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02003003 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003004
3005 return 0;
3006}
3007
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003008/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003009 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003010 * @ep: The endpoint to set halt.
3011 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003012 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
3013 * the endpoint is busy processing requests.
3014 *
3015 * We need to stall the endpoint immediately if request comes from set_feature
3016 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003017 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003018static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003019{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003020 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003021 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003022 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003023 u32 epreg;
3024 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003025 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026
3027 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
3028
Robert Baldygac9f721b2014-01-14 08:36:00 +01003029 if (index == 0) {
3030 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003031 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01003032 else
3033 dev_warn(hs->dev,
3034 "%s: can't clear halt on ep0\n", __func__);
3035 return 0;
3036 }
3037
Vahram Aharonyan15186f12016-05-23 22:41:59 -07003038 if (hs_ep->isochronous) {
3039 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
3040 return -EINVAL;
3041 }
3042
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003043 if (!now && value && !list_empty(&hs_ep->queue)) {
3044 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
3045 ep->name);
3046 return -EAGAIN;
3047 }
3048
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003049 if (hs_ep->dir_in) {
3050 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003051 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003052
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003053 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05003054 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003055 if (epctl & DXEPCTL_EPENA)
3056 epctl |= DXEPCTL_EPDIS;
3057 } else {
3058 epctl &= ~DXEPCTL_STALL;
3059 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3060 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3061 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3062 epctl |= DXEPCTL_SETD0PID;
3063 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003064 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003065 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003066
3067 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003068 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003069
3070 if (value)
3071 epctl |= DXEPCTL_STALL;
3072 else {
3073 epctl &= ~DXEPCTL_STALL;
3074 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3075 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3076 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3077 epctl |= DXEPCTL_SETD0PID;
3078 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003079 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003080 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003081
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003082 hs_ep->halted = value;
3083
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003084 return 0;
3085}
3086
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003087/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003088 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003089 * @ep: The endpoint to set halt.
3090 * @value: Set or unset the halt.
3091 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003092static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003093{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003094 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003095 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003096 unsigned long flags = 0;
3097 int ret = 0;
3098
3099 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003100 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003101 spin_unlock_irqrestore(&hs->lock, flags);
3102
3103 return ret;
3104}
3105
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003106static struct usb_ep_ops dwc2_hsotg_ep_ops = {
3107 .enable = dwc2_hsotg_ep_enable,
3108 .disable = dwc2_hsotg_ep_disable,
3109 .alloc_request = dwc2_hsotg_ep_alloc_request,
3110 .free_request = dwc2_hsotg_ep_free_request,
3111 .queue = dwc2_hsotg_ep_queue_lock,
3112 .dequeue = dwc2_hsotg_ep_dequeue,
3113 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003114 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115};
3116
3117/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003118 * dwc2_hsotg_init - initalize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003119 * @hsotg: The driver state
3120 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003121static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003122{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003123 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003124 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003125 /* unmask subset of endpoint interrupts */
3126
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003127 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
3128 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
3129 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003130
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003131 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
3132 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
3133 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003134
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003135 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003136
3137 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003138 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003139
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003140 /* setup fifos */
3141
3142 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003143 dwc2_readl(hsotg->regs + GRXFSIZ),
3144 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003145
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003146 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003147
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003148 /* keep other bits untouched (so e.g. forced modes are not lost) */
3149 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3150 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3151 GUSBCFG_HNPCAP);
3152
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003153 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003154 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003155 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3156 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
3157 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003158
Gregory Herrerof5090042015-01-09 13:38:47 +01003159 if (using_dma(hsotg))
3160 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003161}
3162
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003163/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003164 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003165 * @gadget: The usb gadget state
3166 * @driver: The usb gadget driver
3167 *
3168 * Perform initialization to prepare udc device and driver
3169 * to work.
3170 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003171static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003172 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003173{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003174 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003175 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003176 int ret;
3177
3178 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003179 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003180 return -ENODEV;
3181 }
3182
3183 if (!driver) {
3184 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3185 return -EINVAL;
3186 }
3187
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003188 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003189 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003190
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003191 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003192 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3193 return -EINVAL;
3194 }
3195
3196 WARN_ON(hsotg->driver);
3197
3198 driver->driver.bus = NULL;
3199 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003200 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003201 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3202
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003203 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
3204 ret = dwc2_lowlevel_hw_enable(hsotg);
3205 if (ret)
3206 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003207 }
3208
Gregory Herrerof6c01592015-01-09 13:38:41 +01003209 if (!IS_ERR_OR_NULL(hsotg->uphy))
3210 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003211
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003212 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003213 dwc2_hsotg_init(hsotg);
3214 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003215 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003216 spin_unlock_irqrestore(&hsotg->lock, flags);
3217
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003218 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003219
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003220 return 0;
3221
3222err:
3223 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003224 return ret;
3225}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003226
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003227/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003228 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003229 * @gadget: The usb gadget state
3230 * @driver: The usb gadget driver
3231 *
3232 * Stop udc hw block and stay tunned for future transmissions
3233 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003234static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003235{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003236 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003237 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003238 int ep;
3239
3240 if (!hsotg)
3241 return -ENODEV;
3242
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003243 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003244 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3245 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003246 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003247 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003248 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003249 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003250
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003251 spin_lock_irqsave(&hsotg->lock, flags);
3252
Marek Szyprowski32805c32014-10-20 12:45:33 +02003253 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003254 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003255 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003256
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003257 spin_unlock_irqrestore(&hsotg->lock, flags);
3258
Gregory Herrerof6c01592015-01-09 13:38:41 +01003259 if (!IS_ERR_OR_NULL(hsotg->uphy))
3260 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003261
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003262 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3263 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003264
3265 return 0;
3266}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003267
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003268/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003269 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003270 * @gadget: The usb gadget state
3271 *
3272 * Read the {micro} frame number
3273 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003274static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003275{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003276 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003277}
3278
Lukasz Majewskia188b682012-06-22 09:29:56 +02003279/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003280 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02003281 * @gadget: The usb gadget state
3282 * @is_on: Current state of the USB PHY
3283 *
3284 * Connect/Disconnect the USB PHY pullup
3285 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003286static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02003287{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003288 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003289 unsigned long flags = 0;
3290
Gregory Herrero77ba9112015-09-29 12:08:19 +02003291 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
3292 hsotg->op_state);
3293
3294 /* Don't modify pullup state while in host mode */
3295 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3296 hsotg->enabled = is_on;
3297 return 0;
3298 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02003299
3300 spin_lock_irqsave(&hsotg->lock, flags);
3301 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003302 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003303 dwc2_hsotg_core_init_disconnected(hsotg, false);
3304 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003305 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003306 dwc2_hsotg_core_disconnect(hsotg);
3307 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003308 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02003309 }
3310
3311 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3312 spin_unlock_irqrestore(&hsotg->lock, flags);
3313
3314 return 0;
3315}
3316
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003317static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01003318{
3319 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3320 unsigned long flags;
3321
3322 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
3323 spin_lock_irqsave(&hsotg->lock, flags);
3324
Gregory Herrero61f72232015-09-29 12:08:28 +02003325 /*
3326 * If controller is hibernated, it must exit from hibernation
3327 * before being initialized / de-initialized
3328 */
3329 if (hsotg->lx_state == DWC2_L2)
3330 dwc2_exit_hibernation(hsotg, false);
3331
Gregory Herrero83d98222015-01-09 13:39:02 +01003332 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02003333 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02003334
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003335 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01003336 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003337 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003338 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003339 dwc2_hsotg_core_disconnect(hsotg);
3340 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003341 }
3342
3343 spin_unlock_irqrestore(&hsotg->lock, flags);
3344 return 0;
3345}
3346
Gregory Herrero596d6962015-01-09 13:39:08 +01003347/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003348 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01003349 * @gadget: The usb gadget state
3350 * @mA: Amount of current
3351 *
3352 * Report how much power the device may consume to the phy.
3353 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003354static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01003355{
3356 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3357
3358 if (IS_ERR_OR_NULL(hsotg->uphy))
3359 return -ENOTSUPP;
3360 return usb_phy_set_power(hsotg->uphy, mA);
3361}
3362
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003363static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
3364 .get_frame = dwc2_hsotg_gadget_getframe,
3365 .udc_start = dwc2_hsotg_udc_start,
3366 .udc_stop = dwc2_hsotg_udc_stop,
3367 .pullup = dwc2_hsotg_pullup,
3368 .vbus_session = dwc2_hsotg_vbus_session,
3369 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003370};
3371
3372/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003373 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003374 * @hsotg: The device state.
3375 * @hs_ep: The endpoint to be initialised.
3376 * @epnum: The endpoint number
3377 *
3378 * Initialise the given endpoint (as part of the probe and device state
3379 * creation) to give to the gadget driver. Setup the endpoint name, any
3380 * direction information and other state that may be required.
3381 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003382static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
3383 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003384 int epnum,
3385 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003386{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003387 char *dir;
3388
3389 if (epnum == 0)
3390 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003391 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003392 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003393 else
3394 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003395
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003396 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003397 hs_ep->index = epnum;
3398
3399 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3400
3401 INIT_LIST_HEAD(&hs_ep->queue);
3402 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3403
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003404 /* add to the list of endpoints known by the gadget driver */
3405 if (epnum)
3406 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3407
3408 hs_ep->parent = hsotg;
3409 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003410 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003411 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003412
Robert Baldyga29545222015-07-31 16:00:18 +02003413 if (epnum == 0) {
3414 hs_ep->ep.caps.type_control = true;
3415 } else {
3416 hs_ep->ep.caps.type_iso = true;
3417 hs_ep->ep.caps.type_bulk = true;
3418 hs_ep->ep.caps.type_int = true;
3419 }
3420
3421 if (dir_in)
3422 hs_ep->ep.caps.dir_in = true;
3423 else
3424 hs_ep->ep.caps.dir_out = true;
3425
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003426 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003427 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003428 * to be something valid.
3429 */
3430
3431 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003432 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003433 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003434 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003435 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003436 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003437 }
3438}
3439
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003440/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003441 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003442 * @param: The device state
3443 *
3444 * Read the USB core HW configuration registers
3445 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003446static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003447{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003448 u32 cfg;
3449 u32 ep_type;
3450 u32 i;
3451
Ben Dooks10aebc72010-07-19 09:40:44 +01003452 /* check hardware configuration */
3453
John Youn43e90342015-12-17 11:17:45 -08003454 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
3455
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003456 /* Add ep0 */
3457 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003458
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003459 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003460 GFP_KERNEL);
3461 if (!hsotg->eps_in[0])
3462 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003463 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003464 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003465
John Youn43e90342015-12-17 11:17:45 -08003466 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08003467 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003468 ep_type = cfg & 3;
3469 /* Direction in or both */
3470 if (!(ep_type & 2)) {
3471 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003472 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003473 if (!hsotg->eps_in[i])
3474 return -ENOMEM;
3475 }
3476 /* Direction out or both */
3477 if (!(ep_type & 1)) {
3478 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003479 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003480 if (!hsotg->eps_out[i])
3481 return -ENOMEM;
3482 }
3483 }
3484
John Youn43e90342015-12-17 11:17:45 -08003485 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
3486 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01003487
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003488 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3489 hsotg->num_of_eps,
3490 hsotg->dedicated_fifos ? "dedicated" : "shared",
3491 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003492 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003493}
3494
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003495/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003496 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003497 * @param: The device state
3498 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003499static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003500{
Mark Brown83a01802011-06-01 17:16:15 +01003501#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003502 struct device *dev = hsotg->dev;
3503 void __iomem *regs = hsotg->regs;
3504 u32 val;
3505 int idx;
3506
3507 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003508 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
3509 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003510
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01003511 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003512 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003513
3514 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003515 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003516
3517 /* show periodic fifo settings */
3518
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003519 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003520 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003521 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003522 val >> FIFOSIZE_DEPTH_SHIFT,
3523 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003524 }
3525
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003526 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003527 dev_info(dev,
3528 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003529 dwc2_readl(regs + DIEPCTL(idx)),
3530 dwc2_readl(regs + DIEPTSIZ(idx)),
3531 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003532
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003533 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003534 dev_info(dev,
3535 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003536 idx, dwc2_readl(regs + DOEPCTL(idx)),
3537 dwc2_readl(regs + DOEPTSIZ(idx)),
3538 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003539
3540 }
3541
3542 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003543 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003544#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003545}
3546
Gregory Herreroedd74be2015-01-09 13:38:48 +01003547#ifdef CONFIG_OF
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003548static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg)
Gregory Herreroedd74be2015-01-09 13:38:48 +01003549{
3550 struct device_node *np = hsotg->dev->of_node;
Gregory Herrero0a176272015-01-09 13:38:52 +01003551 u32 len = 0;
3552 u32 i = 0;
Gregory Herreroedd74be2015-01-09 13:38:48 +01003553
3554 /* Enable dma if requested in device tree */
3555 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
Gregory Herrero0a176272015-01-09 13:38:52 +01003556
3557 /*
3558 * Register TX periodic fifo size per endpoint.
3559 * EP0 is excluded since it has no fifo configuration.
3560 */
3561 if (!of_find_property(np, "g-tx-fifo-size", &len))
3562 goto rx_fifo;
3563
3564 len /= sizeof(u32);
3565
3566 /* Read tx fifo sizes other than ep0 */
3567 if (of_property_read_u32_array(np, "g-tx-fifo-size",
3568 &hsotg->g_tx_fifo_sz[1], len))
3569 goto rx_fifo;
3570
3571 /* Add ep0 */
3572 len++;
3573
3574 /* Make remaining TX fifos unavailable */
3575 if (len < MAX_EPS_CHANNELS) {
3576 for (i = len; i < MAX_EPS_CHANNELS; i++)
3577 hsotg->g_tx_fifo_sz[i] = 0;
3578 }
3579
3580rx_fifo:
3581 /* Register RX fifo size */
3582 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
3583
3584 /* Register NPTX fifo size */
3585 of_property_read_u32(np, "g-np-tx-fifo-size",
3586 &hsotg->g_np_g_tx_fifo_sz);
Gregory Herreroedd74be2015-01-09 13:38:48 +01003587}
3588#else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003589static inline void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
Gregory Herreroedd74be2015-01-09 13:38:48 +01003590#endif
3591
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003592/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06003593 * dwc2_gadget_init - init function for gadget
3594 * @dwc2: The data structure for the DWC2 driver.
3595 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003596 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003597int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003598{
Dinh Nguyen117777b2014-11-11 11:13:34 -06003599 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003600 int epnum;
3601 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003602 int i;
Gregory Herrero0a176272015-01-09 13:38:52 +01003603 u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003604
Gregory Herrero0a176272015-01-09 13:38:52 +01003605 /* Initialize to legacy fifo configuration values */
3606 hsotg->g_rx_fifo_sz = 2048;
3607 hsotg->g_np_g_tx_fifo_sz = 1024;
3608 memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
3609 /* Device tree specific probe */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003610 dwc2_hsotg_of_probe(hsotg);
John Youn43e90342015-12-17 11:17:45 -08003611
3612 /* Check against largest possible value. */
3613 if (hsotg->g_np_g_tx_fifo_sz >
3614 hsotg->hw_params.dev_nperio_tx_fifo_size) {
3615 dev_warn(dev, "Specified GNPTXFDEP=%d > %d\n",
3616 hsotg->g_np_g_tx_fifo_sz,
3617 hsotg->hw_params.dev_nperio_tx_fifo_size);
3618 hsotg->g_np_g_tx_fifo_sz =
3619 hsotg->hw_params.dev_nperio_tx_fifo_size;
3620 }
3621
Gregory Herrero0a176272015-01-09 13:38:52 +01003622 /* Dump fifo information */
3623 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
3624 hsotg->g_np_g_tx_fifo_sz);
3625 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
3626 for (i = 0; i < MAX_EPS_CHANNELS; i++)
3627 dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
3628 hsotg->g_tx_fifo_sz[i]);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003629
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003630 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003631 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003632 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02003633 if (hsotg->dr_mode == USB_DR_MODE_OTG)
3634 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02003635 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3636 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003637
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003638 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003639 if (ret) {
3640 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003641 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003642 }
3643
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003644 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
3645 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3646 if (!hsotg->ctrl_buff) {
3647 dev_err(dev, "failed to allocate ctrl request buff\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003648 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003649 }
3650
3651 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
3652 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3653 if (!hsotg->ep0_buff) {
3654 dev_err(dev, "failed to allocate ctrl reply buff\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003655 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003656 }
3657
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003658 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003659 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003660 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003661 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003662 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003663 }
3664
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003665 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3666
3667 if (hsotg->num_of_eps == 0) {
3668 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003669 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003670 }
3671
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003672 /* setup endpoint information */
3673
3674 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003675 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003676
3677 /* allocate EP0 request */
3678
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003679 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003680 GFP_KERNEL);
3681 if (!hsotg->ctrl_req) {
3682 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003683 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003684 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003685
3686 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003687 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
3688 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003689 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003690 epnum, 1);
3691 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003692 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003693 epnum, 0);
3694 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003695
Dinh Nguyen117777b2014-11-11 11:13:34 -06003696 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003697 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003698 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003699
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003700 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003701
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003702 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003703}
3704
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003705/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003706 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003707 * @pdev: The platform information for the driver
3708 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003709int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003710{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003711 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003712
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003713 return 0;
3714}
3715
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003716int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003717{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003718 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003719
Gregory Herrero9e779772015-04-29 22:09:07 +02003720 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003721 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02003722
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003723 if (hsotg->driver) {
3724 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003725
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003726 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3727 hsotg->driver->driver.name);
3728
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003729 spin_lock_irqsave(&hsotg->lock, flags);
3730 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003731 dwc2_hsotg_core_disconnect(hsotg);
3732 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003733 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3734 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003735
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003736 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3737 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003738 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003739 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003740 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003741 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003742 }
3743
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003744 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003745}
3746
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003747int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003748{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003749 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003750
Gregory Herrero9e779772015-04-29 22:09:07 +02003751 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003752 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02003753
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003754 if (hsotg->driver) {
3755 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3756 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02003757
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003758 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003759 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003760 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003761 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003762 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003763 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003764
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003765 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003766}
John Youn58e52ff6a2016-02-23 19:54:57 -08003767
3768/**
3769 * dwc2_backup_device_registers() - Backup controller device registers.
3770 * When suspending usb bus, registers needs to be backuped
3771 * if controller power is disabled once suspended.
3772 *
3773 * @hsotg: Programming view of the DWC_otg controller
3774 */
3775int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
3776{
3777 struct dwc2_dregs_backup *dr;
3778 int i;
3779
3780 dev_dbg(hsotg->dev, "%s\n", __func__);
3781
3782 /* Backup dev regs */
3783 dr = &hsotg->dr_backup;
3784
3785 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
3786 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
3787 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3788 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
3789 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
3790
3791 for (i = 0; i < hsotg->num_of_eps; i++) {
3792 /* Backup IN EPs */
3793 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
3794
3795 /* Ensure DATA PID is correctly configured */
3796 if (dr->diepctl[i] & DXEPCTL_DPID)
3797 dr->diepctl[i] |= DXEPCTL_SETD1PID;
3798 else
3799 dr->diepctl[i] |= DXEPCTL_SETD0PID;
3800
3801 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
3802 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
3803
3804 /* Backup OUT EPs */
3805 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
3806
3807 /* Ensure DATA PID is correctly configured */
3808 if (dr->doepctl[i] & DXEPCTL_DPID)
3809 dr->doepctl[i] |= DXEPCTL_SETD1PID;
3810 else
3811 dr->doepctl[i] |= DXEPCTL_SETD0PID;
3812
3813 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
3814 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
3815 }
3816 dr->valid = true;
3817 return 0;
3818}
3819
3820/**
3821 * dwc2_restore_device_registers() - Restore controller device registers.
3822 * When resuming usb bus, device registers needs to be restored
3823 * if controller power were disabled.
3824 *
3825 * @hsotg: Programming view of the DWC_otg controller
3826 */
3827int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
3828{
3829 struct dwc2_dregs_backup *dr;
3830 u32 dctl;
3831 int i;
3832
3833 dev_dbg(hsotg->dev, "%s\n", __func__);
3834
3835 /* Restore dev regs */
3836 dr = &hsotg->dr_backup;
3837 if (!dr->valid) {
3838 dev_err(hsotg->dev, "%s: no device registers to restore\n",
3839 __func__);
3840 return -EINVAL;
3841 }
3842 dr->valid = false;
3843
3844 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
3845 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
3846 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
3847 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
3848 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
3849
3850 for (i = 0; i < hsotg->num_of_eps; i++) {
3851 /* Restore IN EPs */
3852 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
3853 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
3854 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
3855
3856 /* Restore OUT EPs */
3857 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
3858 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
3859 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
3860 }
3861
3862 /* Set the Power-On Programming done bit */
3863 dctl = dwc2_readl(hsotg->regs + DCTL);
3864 dctl |= DCTL_PWRONPRGDONE;
3865 dwc2_writel(dctl, hsotg->regs + DCTL);
3866
3867 return 0;
3868}