blob: 88529d0925039f53112458e59c208c057a32a608 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002/**
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09003 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01006 * Copyright 2008 Openmoko, Inc.
7 * Copyright 2008 Simtec Electronics
8 * Ben Dooks <ben@simtec.co.uk>
9 * http://armlinux.simtec.co.uk/
10 *
11 * S3C USB2.0 High-speed / OtG driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020012 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010013
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/spinlock.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
Marek Szyprowski7ad80962014-11-21 15:14:48 +010020#include <linux/mutex.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010021#include <linux/seq_file.h>
22#include <linux/delay.h>
23#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020025#include <linux/of_platform.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010026
27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053029#include <linux/usb/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010030
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070031#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060032#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010033
34/* conversion functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050035static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010036{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050037 return container_of(req, struct dwc2_hsotg_req, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010038}
39
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050040static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050042 return container_of(ep, struct dwc2_hsotg_ep, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010043}
44
Dinh Nguyen941fcce2014-11-11 11:13:33 -060045static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010046{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060047 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010048}
49
50static inline void __orr32(void __iomem *ptr, u32 val)
51{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030052 dwc2_writel(dwc2_readl(ptr) | val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010053}
54
55static inline void __bic32(void __iomem *ptr, u32 val)
56{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030057 dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010058}
59
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050060static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010061 u32 ep_index, u32 dir_in)
62{
63 if (dir_in)
64 return hsotg->eps_in[ep_index];
65 else
66 return hsotg->eps_out[ep_index];
67}
68
Mickael Maison997f4f82014-12-23 17:39:45 +010069/* forward declaration of functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050070static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010071
72/**
73 * using_dma - return the DMA status of the driver.
74 * @hsotg: The driver state.
75 *
76 * Return true if we're using DMA.
77 *
78 * Currently, we have the DMA support code worked into everywhere
79 * that needs it, but the AMBA DMA implementation in the hardware can
80 * only DMA from 32bit aligned addresses. This means that gadgets such
81 * as the CDC Ethernet cannot work as they often pass packets which are
82 * not 32bit aligned.
83 *
84 * Unfortunately the choice to use DMA or not is global to the controller
85 * and seems to be only settable when the controller is being put through
86 * a core reset. This means we either need to fix the gadgets to take
87 * account of DMA alignment, or add bounce buffers (yuerk).
88 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010089 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010090 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060091static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010092{
John Youn05ee7992016-11-03 17:56:05 -070093 return hsotg->params.g_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +010094}
95
Vahram Aharonyandec4b552016-11-09 19:27:48 -080096/*
97 * using_desc_dma - return the descriptor DMA status of the driver.
98 * @hsotg: The driver state.
99 *
100 * Return true if we're using descriptor DMA.
101 */
102static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
103{
104 return hsotg->params.g_dma_desc;
105}
106
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100107/**
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700108 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
109 * @hs_ep: The endpoint
110 * @increment: The value to increment by
111 *
112 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
113 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
114 */
115static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
116{
117 hs_ep->target_frame += hs_ep->interval;
118 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
119 hs_ep->frame_overrun = 1;
120 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
121 } else {
122 hs_ep->frame_overrun = 0;
123 }
124}
125
126/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500127 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100128 * @hsotg: The device state
129 * @ints: A bitmask of the interrupts to enable
130 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500131static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100132{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300133 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100134 u32 new_gsintmsk;
135
136 new_gsintmsk = gsintmsk | ints;
137
138 if (new_gsintmsk != gsintmsk) {
139 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300140 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100141 }
142}
143
144/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500145 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100146 * @hsotg: The device state
147 * @ints: A bitmask of the interrupts to enable
148 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500149static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100150{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300151 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100152 u32 new_gsintmsk;
153
154 new_gsintmsk = gsintmsk & ~ints;
155
156 if (new_gsintmsk != gsintmsk)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300157 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100158}
159
160/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500161 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100162 * @hsotg: The device state
163 * @ep: The endpoint index
164 * @dir_in: True if direction is in.
165 * @en: The enable value, true to enable
166 *
167 * Set or clear the mask for an individual endpoint's interrupt
168 * request.
169 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500170static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800171 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100172 unsigned int en)
173{
174 unsigned long flags;
175 u32 bit = 1 << ep;
176 u32 daint;
177
178 if (!dir_in)
179 bit <<= 16;
180
181 local_irq_save(flags);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300182 daint = dwc2_readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100183 if (en)
184 daint |= bit;
185 else
186 daint &= ~bit;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300187 dwc2_writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100188 local_irq_restore(flags);
189}
190
191/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800192 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
193 */
194int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
195{
196 if (hsotg->hw_params.en_multiple_tx_fifo)
197 /* In dedicated FIFO mode we need count of IN EPs */
198 return (dwc2_readl(hsotg->regs + GHWCFG4) &
199 GHWCFG4_NUM_IN_EPS_MASK) >> GHWCFG4_NUM_IN_EPS_SHIFT;
200 else
201 /* In shared FIFO mode we need count of Periodic IN EPs */
202 return hsotg->hw_params.num_dev_perio_in_ep;
203}
204
205/**
206 * dwc2_hsotg_ep_info_size - return Endpoint Info Control block size in DWORDs
207 */
208static int dwc2_hsotg_ep_info_size(struct dwc2_hsotg *hsotg)
209{
210 int val = 0;
211 int i;
212 u32 ep_dirs;
213
214 /*
215 * Don't need additional space for ep info control registers in
216 * slave mode.
217 */
218 if (!using_dma(hsotg)) {
219 dev_dbg(hsotg->dev, "Buffer DMA ep info size 0\n");
220 return 0;
221 }
222
223 /*
224 * Buffer DMA mode - 1 location per endpoit
225 * Descriptor DMA mode - 4 locations per endpoint
226 */
227 ep_dirs = hsotg->hw_params.dev_ep_dirs;
228
229 for (i = 0; i <= hsotg->hw_params.num_dev_ep; i++) {
230 val += ep_dirs & 3 ? 1 : 2;
231 ep_dirs >>= 2;
232 }
233
234 if (using_desc_dma(hsotg))
235 val = val * 4;
236
237 return val;
238}
239
240/**
241 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
242 * device mode TX FIFOs
243 */
244int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
245{
246 int ep_info_size;
247 int addr;
248 int tx_addr_max;
249 u32 np_tx_fifo_size;
250
251 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
252 hsotg->params.g_np_tx_fifo_size);
253
254 /* Get Endpoint Info Control block size in DWORDs. */
255 ep_info_size = dwc2_hsotg_ep_info_size(hsotg);
256 tx_addr_max = hsotg->hw_params.total_fifo_size - ep_info_size;
257
258 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
259 if (tx_addr_max <= addr)
260 return 0;
261
262 return tx_addr_max - addr;
263}
264
265/**
266 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
267 * TX FIFOs
268 */
269int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
270{
271 int tx_fifo_count;
272 int tx_fifo_depth;
273
274 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
275
276 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
277
278 if (!tx_fifo_count)
279 return tx_fifo_depth;
280 else
281 return tx_fifo_depth / tx_fifo_count;
282}
283
284/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500285 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100286 * @hsotg: The device instance.
287 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500288static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100289{
John Youn2317eac2016-10-17 17:36:23 -0700290 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100291 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100292 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100293 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700294 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100295
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100296 /* Reset fifo map if not correctly cleared during previous session */
297 WARN_ON(hsotg->fifo_map);
298 hsotg->fifo_map = 0;
299
Gregory Herrero0a176272015-01-09 13:38:52 +0100300 /* set RX/NPTX FIFO sizes */
John Youn05ee7992016-11-03 17:56:05 -0700301 dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
302 dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
303 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
304 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100305
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200306 /*
307 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100308 * block have overlapping default addresses. This also ensures
309 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200310 * known values.
311 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100312
313 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700314 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100315
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200316 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100317 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200318 * them to endpoints dynamically according to maxpacket size value of
319 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200320 */
John Youn2317eac2016-10-17 17:36:23 -0700321 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700322 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700323 continue;
324 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700325 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
326 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700327 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700328 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100329
John Youn2317eac2016-10-17 17:36:23 -0700330 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
John Youn05ee7992016-11-03 17:56:05 -0700331 val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100332 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100333
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800334 dwc2_writel(hsotg->hw_params.total_fifo_size |
335 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
336 hsotg->regs + GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200337 /*
338 * according to p428 of the design guide, we need to ensure that
339 * all fifos are flushed before continuing
340 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100341
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300342 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700343 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100344
345 /* wait until the fifos are both flushed */
346 timeout = 100;
347 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300348 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100349
Dinh Nguyen47a16852014-04-14 14:13:34 -0700350 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100351 break;
352
353 if (--timeout == 0) {
354 dev_err(hsotg->dev,
355 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
356 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100357 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100358 }
359
360 udelay(1);
361 }
362
363 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100364}
365
366/**
367 * @ep: USB endpoint to allocate request for.
368 * @flags: Allocation flags
369 *
370 * Allocate a new USB request structure appropriate for the specified endpoint
371 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500372static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800373 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100374{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500375 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100376
John Younec33efe2017-01-17 20:32:41 -0800377 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100378 if (!req)
379 return NULL;
380
381 INIT_LIST_HEAD(&req->queue);
382
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100383 return &req->req;
384}
385
386/**
387 * is_ep_periodic - return true if the endpoint is in periodic mode.
388 * @hs_ep: The endpoint to query.
389 *
390 * Returns true if the endpoint is in periodic mode, meaning it is being
391 * used for an Interrupt or ISO transfer.
392 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500393static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100394{
395 return hs_ep->periodic;
396}
397
398/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500399 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100400 * @hsotg: The device state.
401 * @hs_ep: The endpoint for the request
402 * @hs_req: The request being processed.
403 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500404 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100405 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200406 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500407static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800408 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500409 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100410{
411 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800412
Jingoo Han17d966a2013-05-11 21:14:00 +0900413 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100414}
415
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800416/*
417 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
418 * for Control endpoint
419 * @hsotg: The device state.
420 *
421 * This function will allocate 4 descriptor chains for EP 0: 2 for
422 * Setup stage, per one for IN and OUT data/status transactions.
423 */
424static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
425{
426 hsotg->setup_desc[0] =
427 dmam_alloc_coherent(hsotg->dev,
428 sizeof(struct dwc2_dma_desc),
429 &hsotg->setup_desc_dma[0],
430 GFP_KERNEL);
431 if (!hsotg->setup_desc[0])
432 goto fail;
433
434 hsotg->setup_desc[1] =
435 dmam_alloc_coherent(hsotg->dev,
436 sizeof(struct dwc2_dma_desc),
437 &hsotg->setup_desc_dma[1],
438 GFP_KERNEL);
439 if (!hsotg->setup_desc[1])
440 goto fail;
441
442 hsotg->ctrl_in_desc =
443 dmam_alloc_coherent(hsotg->dev,
444 sizeof(struct dwc2_dma_desc),
445 &hsotg->ctrl_in_desc_dma,
446 GFP_KERNEL);
447 if (!hsotg->ctrl_in_desc)
448 goto fail;
449
450 hsotg->ctrl_out_desc =
451 dmam_alloc_coherent(hsotg->dev,
452 sizeof(struct dwc2_dma_desc),
453 &hsotg->ctrl_out_desc_dma,
454 GFP_KERNEL);
455 if (!hsotg->ctrl_out_desc)
456 goto fail;
457
458 return 0;
459
460fail:
461 return -ENOMEM;
462}
463
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100464/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500465 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100466 * @hsotg: The controller state.
467 * @hs_ep: The endpoint we're going to write for.
468 * @hs_req: The request to write data for.
469 *
470 * This is called when the TxFIFO has some space in it to hold a new
471 * transmission and we have something to give it. The actual setup of
472 * the data size is done elsewhere, so all we have to do is to actually
473 * write the data.
474 *
475 * The return value is zero if there is more space (or nothing was done)
476 * otherwise -ENOSPC is returned if the FIFO space was used up.
477 *
478 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200479 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500480static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800481 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500482 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100483{
484 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300485 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100486 int buf_pos = hs_req->req.actual;
487 int to_write = hs_ep->size_loaded;
488 void *data;
489 int can_write;
490 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200491 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100492
493 to_write -= (buf_pos - hs_ep->last_load);
494
495 /* if there's nothing to write, get out early */
496 if (to_write == 0)
497 return 0;
498
Ben Dooks10aebc72010-07-19 09:40:44 +0100499 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300500 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100501 int size_left;
502 int size_done;
503
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200504 /*
505 * work out how much data was loaded so we can calculate
506 * how much data is left in the fifo.
507 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100508
Dinh Nguyen47a16852014-04-14 14:13:34 -0700509 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100510
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200511 /*
512 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100513 * previous data has been completely sent.
514 */
515 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500516 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100517 return -ENOSPC;
518 }
519
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100520 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
521 __func__, size_left,
522 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
523
524 /* how much of the data has moved */
525 size_done = hs_ep->size_loaded - size_left;
526
527 /* how much data is left in the fifo */
528 can_write = hs_ep->fifo_load - size_done;
529 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
530 __func__, can_write);
531
532 can_write = hs_ep->fifo_size - can_write;
533 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
534 __func__, can_write);
535
536 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500537 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100538 return -ENOSPC;
539 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100540 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Robert Baldygaad674a12016-08-29 13:38:50 -0700541 can_write = dwc2_readl(hsotg->regs +
542 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100543
544 can_write &= 0xffff;
545 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100546 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700547 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100548 dev_dbg(hsotg->dev,
549 "%s: no queue slots available (0x%08x)\n",
550 __func__, gnptxsts);
551
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500552 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100553 return -ENOSPC;
554 }
555
Dinh Nguyen47a16852014-04-14 14:13:34 -0700556 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100557 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100558 }
559
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200560 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
561
562 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800563 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100564
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200565 /*
566 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100567 * FIFO, requests of >512 cause the endpoint to get stuck with a
568 * fragment of the end of the transfer in it.
569 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200570 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100571 can_write = 512;
572
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200573 /*
574 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100575 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200576 * doing it.
577 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200578 if (to_write > max_transfer) {
579 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100580
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200581 /* it's needed only when we do not use dedicated fifos */
582 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500583 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800584 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700585 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100586 }
587
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100588 /* see if we can write data */
589
590 if (to_write > can_write) {
591 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200592 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100593
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200594 /*
595 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100596 * exact number of packets.
597 *
598 * Note, we do not currently check to see if we can ever
599 * write a full packet or not to the FIFO.
600 */
601
602 if (pkt_round)
603 to_write -= pkt_round;
604
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200605 /*
606 * enable correct FIFO interrupt to alert us when there
607 * is more room left.
608 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100609
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200610 /* it's needed only when we do not use dedicated fifos */
611 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500612 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800613 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700614 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100615 }
616
617 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800618 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100619
620 if (to_write <= 0)
621 return -ENOSPC;
622
623 hs_req->req.actual = buf_pos + to_write;
624 hs_ep->total_data += to_write;
625
626 if (periodic)
627 hs_ep->fifo_load += to_write;
628
629 to_write = DIV_ROUND_UP(to_write, 4);
630 data = hs_req->req.buf + buf_pos;
631
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500632 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100633
634 return (to_write >= can_write) ? -ENOSPC : 0;
635}
636
637/**
638 * get_ep_limit - get the maximum data legnth for this endpoint
639 * @hs_ep: The endpoint
640 *
641 * Return the maximum data that can be queued in one go on a given endpoint
642 * so that transfers that are too long can be split.
643 */
John Youn9da51972017-01-17 20:30:27 -0800644static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100645{
646 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800647 unsigned int maxsize;
648 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100649
650 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700651 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
652 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100653 } else {
John Youn9da51972017-01-17 20:30:27 -0800654 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900655 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700656 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900657 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100658 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100659 }
660
661 /* we made the constant loading easier above by using +1 */
662 maxpkt--;
663 maxsize--;
664
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200665 /*
666 * constrain by packet count if maxpkts*pktsize is greater
667 * than the length register size.
668 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100669
670 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
671 maxsize = maxpkt * hs_ep->ep.maxpacket;
672
673 return maxsize;
674}
675
676/**
John Youn38beaec2017-01-17 20:31:13 -0800677 * dwc2_hsotg_read_frameno - read current frame number
678 * @hsotg: The device instance
679 *
680 * Return the current frame number
681 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700682static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
683{
684 u32 dsts;
685
686 dsts = dwc2_readl(hsotg->regs + DSTS);
687 dsts &= DSTS_SOFFN_MASK;
688 dsts >>= DSTS_SOFFN_SHIFT;
689
690 return dsts;
691}
692
693/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800694 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
695 * DMA descriptor chain prepared for specific endpoint
696 * @hs_ep: The endpoint
697 *
698 * Return the maximum data that can be queued in one go on a given endpoint
699 * depending on its descriptor chain capacity so that transfers that
700 * are too long can be split.
701 */
702static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
703{
704 int is_isoc = hs_ep->isochronous;
705 unsigned int maxsize;
706
707 if (is_isoc)
708 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
709 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
710 else
711 maxsize = DEV_DMA_NBYTES_LIMIT;
712
713 /* Above size of one descriptor was chosen, multiple it */
714 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
715
716 return maxsize;
717}
718
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800719/*
720 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
721 * @hs_ep: The endpoint
722 * @mask: RX/TX bytes mask to be defined
723 *
724 * Returns maximum data payload for one descriptor after analyzing endpoint
725 * characteristics.
726 * DMA descriptor transfer bytes limit depends on EP type:
727 * Control out - MPS,
728 * Isochronous - descriptor rx/tx bytes bitfield limit,
729 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
730 * have concatenations from various descriptors within one packet.
731 *
732 * Selects corresponding mask for RX/TX bytes as well.
733 */
734static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
735{
736 u32 mps = hs_ep->ep.maxpacket;
737 int dir_in = hs_ep->dir_in;
738 u32 desc_size = 0;
739
740 if (!hs_ep->index && !dir_in) {
741 desc_size = mps;
742 *mask = DEV_DMA_NBYTES_MASK;
743 } else if (hs_ep->isochronous) {
744 if (dir_in) {
745 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
746 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
747 } else {
748 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
749 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
750 }
751 } else {
752 desc_size = DEV_DMA_NBYTES_LIMIT;
753 *mask = DEV_DMA_NBYTES_MASK;
754
755 /* Round down desc_size to be mps multiple */
756 desc_size -= desc_size % mps;
757 }
758
759 return desc_size;
760}
761
762/*
763 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
764 * @hs_ep: The endpoint
765 * @dma_buff: DMA address to use
766 * @len: Length of the transfer
767 *
768 * This function will iterate over descriptor chain and fill its entries
769 * with corresponding information based on transfer data.
770 */
771static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
772 dma_addr_t dma_buff,
773 unsigned int len)
774{
775 struct dwc2_hsotg *hsotg = hs_ep->parent;
776 int dir_in = hs_ep->dir_in;
777 struct dwc2_dma_desc *desc = hs_ep->desc_list;
778 u32 mps = hs_ep->ep.maxpacket;
779 u32 maxsize = 0;
780 u32 offset = 0;
781 u32 mask = 0;
782 int i;
783
784 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
785
786 hs_ep->desc_count = (len / maxsize) +
787 ((len % maxsize) ? 1 : 0);
788 if (len == 0)
789 hs_ep->desc_count = 1;
790
791 for (i = 0; i < hs_ep->desc_count; ++i) {
792 desc->status = 0;
793 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
794 << DEV_DMA_BUFF_STS_SHIFT);
795
796 if (len > maxsize) {
797 if (!hs_ep->index && !dir_in)
798 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
799
800 desc->status |= (maxsize <<
801 DEV_DMA_NBYTES_SHIFT & mask);
802 desc->buf = dma_buff + offset;
803
804 len -= maxsize;
805 offset += maxsize;
806 } else {
807 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
808
809 if (dir_in)
810 desc->status |= (len % mps) ? DEV_DMA_SHORT :
811 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
812 if (len > maxsize)
813 dev_err(hsotg->dev, "wrong len %d\n", len);
814
815 desc->status |=
816 len << DEV_DMA_NBYTES_SHIFT & mask;
817 desc->buf = dma_buff + offset;
818 }
819
820 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
821 desc->status |= (DEV_DMA_BUFF_STS_HREADY
822 << DEV_DMA_BUFF_STS_SHIFT);
823 desc++;
824 }
825}
826
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800827/*
828 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
829 * @hs_ep: The isochronous endpoint.
830 * @dma_buff: usb requests dma buffer.
831 * @len: usb request transfer length.
832 *
833 * Finds out index of first free entry either in the bottom or up half of
834 * descriptor chain depend on which is under SW control and not processed
835 * by HW. Then fills that descriptor with the data of the arrived usb request,
836 * frame info, sets Last and IOC bits increments next_desc. If filled
837 * descriptor is not the first one, removes L bit from the previous descriptor
838 * status.
839 */
840static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
841 dma_addr_t dma_buff, unsigned int len)
842{
843 struct dwc2_dma_desc *desc;
844 struct dwc2_hsotg *hsotg = hs_ep->parent;
845 u32 index;
846 u32 maxsize = 0;
847 u32 mask = 0;
848
849 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
850 if (len > maxsize) {
851 dev_err(hsotg->dev, "wrong len %d\n", len);
852 return -EINVAL;
853 }
854
855 /*
856 * If SW has already filled half of chain, then return and wait for
857 * the other chain to be processed by HW.
858 */
859 if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2)
860 return -EBUSY;
861
862 /* Increment frame number by interval for IN */
863 if (hs_ep->dir_in)
864 dwc2_gadget_incr_frame_num(hs_ep);
865
866 index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num +
867 hs_ep->next_desc;
868
869 /* Sanity check of calculated index */
870 if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) ||
871 (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) {
872 dev_err(hsotg->dev, "wrong index %d for iso chain\n", index);
873 return -EINVAL;
874 }
875
876 desc = &hs_ep->desc_list[index];
877
878 /* Clear L bit of previous desc if more than one entries in the chain */
879 if (hs_ep->next_desc)
880 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
881
882 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
883 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
884
885 desc->status = 0;
886 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
887
888 desc->buf = dma_buff;
889 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
890 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
891
892 if (hs_ep->dir_in) {
893 desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) &
894 DEV_DMA_ISOC_PID_MASK) |
895 ((len % hs_ep->ep.maxpacket) ?
896 DEV_DMA_SHORT : 0) |
897 ((hs_ep->target_frame <<
898 DEV_DMA_ISOC_FRNUM_SHIFT) &
899 DEV_DMA_ISOC_FRNUM_MASK);
900 }
901
902 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
903 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
904
905 /* Update index of last configured entry in the chain */
906 hs_ep->next_desc++;
907
908 return 0;
909}
910
911/*
912 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
913 * @hs_ep: The isochronous endpoint.
914 *
915 * Prepare first descriptor chain for isochronous endpoints. Afterwards
916 * write DMA address to HW and enable the endpoint.
917 *
918 * Switch between descriptor chains via isoc_chain_num to give SW opportunity
919 * to prepare second descriptor chain while first one is being processed by HW.
920 */
921static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
922{
923 struct dwc2_hsotg *hsotg = hs_ep->parent;
924 struct dwc2_hsotg_req *hs_req, *treq;
925 int index = hs_ep->index;
926 int ret;
927 u32 dma_reg;
928 u32 depctl;
929 u32 ctrl;
930
931 if (list_empty(&hs_ep->queue)) {
932 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
933 return;
934 }
935
936 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
937 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
938 hs_req->req.length);
939 if (ret) {
940 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
941 break;
942 }
943 }
944
945 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
946 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
947
948 /* write descriptor chain address to control register */
949 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
950
951 ctrl = dwc2_readl(hsotg->regs + depctl);
952 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
953 dwc2_writel(ctrl, hsotg->regs + depctl);
954
955 /* Switch ISOC descriptor chain number being processed by SW*/
956 hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
957 hs_ep->next_desc = 0;
958}
959
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800960/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500961 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100962 * @hsotg: The controller state.
963 * @hs_ep: The endpoint to process a request for
964 * @hs_req: The request to start.
965 * @continuing: True if we are doing more for the current request.
966 *
967 * Start the given request running by setting the endpoint registers
968 * appropriately, and writing any data to the FIFOs.
969 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500970static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800971 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500972 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100973 bool continuing)
974{
975 struct usb_request *ureq = &hs_req->req;
976 int index = hs_ep->index;
977 int dir_in = hs_ep->dir_in;
978 u32 epctrl_reg;
979 u32 epsize_reg;
980 u32 epsize;
981 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -0800982 unsigned int length;
983 unsigned int packets;
984 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800985 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100986
987 if (index != 0) {
988 if (hs_ep->req && !continuing) {
989 dev_err(hsotg->dev, "%s: active request\n", __func__);
990 WARN_ON(1);
991 return;
992 } else if (hs_ep->req != hs_req && continuing) {
993 dev_err(hsotg->dev,
994 "%s: continue different req\n", __func__);
995 WARN_ON(1);
996 return;
997 }
998 }
999
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001000 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001001 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
1002 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001003
1004 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001005 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001006 hs_ep->dir_in ? "in" : "out");
1007
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001008 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001009 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001010
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +02001011 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001012 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
1013 return;
1014 }
1015
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001016 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001017 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
1018 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001019
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -08001020 if (!using_desc_dma(hsotg))
1021 maxreq = get_ep_limit(hs_ep);
1022 else
1023 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
1024
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001025 if (length > maxreq) {
1026 int round = maxreq % hs_ep->ep.maxpacket;
1027
1028 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
1029 __func__, length, maxreq, round);
1030
1031 /* round down to multiple of packets */
1032 if (round)
1033 maxreq -= round;
1034
1035 length = maxreq;
1036 }
1037
1038 if (length)
1039 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
1040 else
1041 packets = 1; /* send one packet if length is zero. */
1042
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001043 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1044 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
1045 return;
1046 }
1047
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001048 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001049 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001050 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001051 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001052 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001053 else
1054 epsize = 0;
1055
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001056 /*
1057 * zero length packet should be programmed on its own and should not
1058 * be counted in DIEPTSIZ.PktCnt with other packets.
1059 */
1060 if (dir_in && ureq->zero && !continuing) {
1061 /* Test if zlp is actually required. */
1062 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001063 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001064 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001065 }
1066
Dinh Nguyen47a16852014-04-14 14:13:34 -07001067 epsize |= DXEPTSIZ_PKTCNT(packets);
1068 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001069
1070 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1071 __func__, packets, length, ureq->length, epsize, epsize_reg);
1072
1073 /* store the request as the current one we're doing */
1074 hs_ep->req = hs_req;
1075
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001076 if (using_desc_dma(hsotg)) {
1077 u32 offset = 0;
1078 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001079
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001080 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1081 if (!dir_in) {
1082 if (!index)
1083 length = mps;
1084 else if (length % mps)
1085 length += (mps - (length % mps));
1086 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001087
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001088 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001089 * If more data to send, adjust DMA for EP0 out data stage.
1090 * ureq->dma stays unchanged, hence increment it by already
1091 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001092 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001093 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1094 continuing)
1095 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001096
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001097 /* Fill DDMA chain entries */
1098 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1099 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001100
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001101 /* write descriptor chain address to control register */
1102 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
1103
1104 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1105 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1106 } else {
1107 /* write size / packets */
1108 dwc2_writel(epsize, hsotg->regs + epsize_reg);
1109
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001110 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001111 /*
1112 * write DMA address to control register, buffer
1113 * already synced by dwc2_hsotg_ep_queue().
1114 */
1115
1116 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
1117
1118 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1119 __func__, &ureq->dma, dma_reg);
1120 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001121 }
1122
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001123 if (hs_ep->isochronous && hs_ep->interval == 1) {
1124 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1125 dwc2_gadget_incr_frame_num(hs_ep);
1126
1127 if (hs_ep->target_frame & 0x1)
1128 ctrl |= DXEPCTL_SETODDFR;
1129 else
1130 ctrl |= DXEPCTL_SETEVENFR;
1131 }
1132
Dinh Nguyen47a16852014-04-14 14:13:34 -07001133 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001134
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001135 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001136
1137 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001138 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001139 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001140
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001141 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001142 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001143
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001144 /*
1145 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001146 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001147 * this information.
1148 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001149 hs_ep->size_loaded = length;
1150 hs_ep->last_load = ureq->actual;
1151
1152 if (dir_in && !using_dma(hsotg)) {
1153 /* set these anyway, we may need them for non-periodic in */
1154 hs_ep->fifo_load = 0;
1155
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001156 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001157 }
1158
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001159 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001160 * Note, trying to clear the NAK here causes problems with transmit
1161 * on the S3C6400 ending up with the TXFIFO becoming full.
1162 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001163
1164 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001165 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001166 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001167 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001168 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001169
Dinh Nguyen47a16852014-04-14 14:13:34 -07001170 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001171 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001172
1173 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001174 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001175}
1176
1177/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001178 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001179 * @hsotg: The device state.
1180 * @hs_ep: The endpoint the request is on.
1181 * @req: The request being processed.
1182 *
1183 * We've been asked to queue a request, so ensure that the memory buffer
1184 * is correctly setup for DMA. If we've been passed an extant DMA address
1185 * then ensure the buffer has been synced to memory. If our buffer has no
1186 * DMA memory, then we map the memory and mark our request to allow us to
1187 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001188 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001189static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001190 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001191 struct usb_request *req)
1192{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001193 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001194
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001195 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1196 if (ret)
1197 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001198
1199 return 0;
1200
1201dma_error:
1202 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1203 __func__, req->buf, req->length);
1204
1205 return -EIO;
1206}
1207
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001208static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001209 struct dwc2_hsotg_ep *hs_ep,
1210 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001211{
1212 void *req_buf = hs_req->req.buf;
1213
1214 /* If dma is not being used or buffer is aligned */
1215 if (!using_dma(hsotg) || !((long)req_buf & 3))
1216 return 0;
1217
1218 WARN_ON(hs_req->saved_req_buf);
1219
1220 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001221 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001222
1223 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1224 if (!hs_req->req.buf) {
1225 hs_req->req.buf = req_buf;
1226 dev_err(hsotg->dev,
1227 "%s: unable to allocate memory for bounce buffer\n",
1228 __func__);
1229 return -ENOMEM;
1230 }
1231
1232 /* Save actual buffer */
1233 hs_req->saved_req_buf = req_buf;
1234
1235 if (hs_ep->dir_in)
1236 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1237 return 0;
1238}
1239
John Younb98866c2017-01-17 20:31:58 -08001240static void
1241dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1242 struct dwc2_hsotg_ep *hs_ep,
1243 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001244{
1245 /* If dma is not being used or buffer was aligned */
1246 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1247 return;
1248
1249 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1250 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1251
1252 /* Copy data from bounce buffer on successful out transfer */
1253 if (!hs_ep->dir_in && !hs_req->req.status)
1254 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001255 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001256
1257 /* Free bounce buffer */
1258 kfree(hs_req->req.buf);
1259
1260 hs_req->req.buf = hs_req->saved_req_buf;
1261 hs_req->saved_req_buf = NULL;
1262}
1263
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001264/**
1265 * dwc2_gadget_target_frame_elapsed - Checks target frame
1266 * @hs_ep: The driver endpoint to check
1267 *
1268 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1269 * corresponding transfer.
1270 */
1271static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1272{
1273 struct dwc2_hsotg *hsotg = hs_ep->parent;
1274 u32 target_frame = hs_ep->target_frame;
1275 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
1276 bool frame_overrun = hs_ep->frame_overrun;
1277
1278 if (!frame_overrun && current_frame >= target_frame)
1279 return true;
1280
1281 if (frame_overrun && current_frame >= target_frame &&
1282 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1283 return true;
1284
1285 return false;
1286}
1287
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001288/*
1289 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1290 * @hsotg: The driver state
1291 * @hs_ep: the ep descriptor chain is for
1292 *
1293 * Called to update EP0 structure's pointers depend on stage of
1294 * control transfer.
1295 */
1296static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1297 struct dwc2_hsotg_ep *hs_ep)
1298{
1299 switch (hsotg->ep0_state) {
1300 case DWC2_EP0_SETUP:
1301 case DWC2_EP0_STATUS_OUT:
1302 hs_ep->desc_list = hsotg->setup_desc[0];
1303 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1304 break;
1305 case DWC2_EP0_DATA_IN:
1306 case DWC2_EP0_STATUS_IN:
1307 hs_ep->desc_list = hsotg->ctrl_in_desc;
1308 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1309 break;
1310 case DWC2_EP0_DATA_OUT:
1311 hs_ep->desc_list = hsotg->ctrl_out_desc;
1312 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1313 break;
1314 default:
1315 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1316 hsotg->ep0_state);
1317 return -EINVAL;
1318 }
1319
1320 return 0;
1321}
1322
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001323static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001324 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001325{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001326 struct dwc2_hsotg_req *hs_req = our_req(req);
1327 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001328 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001329 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001330 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001331
1332 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1333 ep->name, req, req->length, req->buf, req->no_interrupt,
1334 req->zero, req->short_not_ok);
1335
Gregory Herrero7ababa92015-04-29 22:09:08 +02001336 /* Prevent new request submission when controller is suspended */
1337 if (hs->lx_state == DWC2_L2) {
1338 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
John Youn9da51972017-01-17 20:30:27 -08001339 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001340 return -EAGAIN;
1341 }
1342
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001343 /* initialise status of the request */
1344 INIT_LIST_HEAD(&hs_req->queue);
1345 req->actual = 0;
1346 req->status = -EINPROGRESS;
1347
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001348 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001349 if (ret)
1350 return ret;
1351
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001352 /* if we're using DMA, sync the buffers as necessary */
1353 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001354 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001355 if (ret)
1356 return ret;
1357 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001358 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1359 if (using_desc_dma(hs) && !hs_ep->index) {
1360 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1361 if (ret)
1362 return ret;
1363 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001364
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001365 first = list_empty(&hs_ep->queue);
1366 list_add_tail(&hs_req->queue, &hs_ep->queue);
1367
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001368 /*
1369 * Handle DDMA isochronous transfers separately - just add new entry
1370 * to the half of descriptor chain that is not processed by HW.
1371 * Transfer will be started once SW gets either one of NAK or
1372 * OutTknEpDis interrupts.
1373 */
1374 if (using_desc_dma(hs) && hs_ep->isochronous &&
1375 hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1376 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1377 hs_req->req.length);
1378 if (ret)
1379 dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__);
1380
1381 return 0;
1382 }
1383
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001384 if (first) {
1385 if (!hs_ep->isochronous) {
1386 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1387 return 0;
1388 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001389
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001390 while (dwc2_gadget_target_frame_elapsed(hs_ep))
1391 dwc2_gadget_incr_frame_num(hs_ep);
1392
1393 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1394 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1395 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001396 return 0;
1397}
1398
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001399static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001400 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001401{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001402 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001403 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001404 unsigned long flags = 0;
1405 int ret = 0;
1406
1407 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001408 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001409 spin_unlock_irqrestore(&hs->lock, flags);
1410
1411 return ret;
1412}
1413
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001414static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001415 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001416{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001417 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001418
1419 kfree(hs_req);
1420}
1421
1422/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001423 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001424 * @ep: The endpoint the request was on.
1425 * @req: The request completed.
1426 *
1427 * Called on completion of any requests the driver itself
1428 * submitted that need cleaning up.
1429 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001430static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001431 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001432{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001433 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001434 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001435
1436 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1437
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001438 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001439}
1440
1441/**
1442 * ep_from_windex - convert control wIndex value to endpoint
1443 * @hsotg: The driver state.
1444 * @windex: The control request wIndex field (in host order).
1445 *
1446 * Convert the given wIndex into a pointer to an driver endpoint
1447 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001448 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001449static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001450 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001451{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001452 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001453 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1454 int idx = windex & 0x7F;
1455
1456 if (windex >= 0x100)
1457 return NULL;
1458
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001459 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001460 return NULL;
1461
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001462 ep = index_to_ep(hsotg, idx, dir);
1463
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001464 if (idx && ep->dir_in != dir)
1465 return NULL;
1466
1467 return ep;
1468}
1469
1470/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001471 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001472 * @hsotg: The driver state.
1473 * @testmode: requested usb test mode
1474 * Enable usb Test Mode requested by the Host.
1475 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001476int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001477{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001478 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001479
1480 dctl &= ~DCTL_TSTCTL_MASK;
1481 switch (testmode) {
1482 case TEST_J:
1483 case TEST_K:
1484 case TEST_SE0_NAK:
1485 case TEST_PACKET:
1486 case TEST_FORCE_EN:
1487 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1488 break;
1489 default:
1490 return -EINVAL;
1491 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001492 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001493 return 0;
1494}
1495
1496/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001497 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001498 * @hsotg: The device state
1499 * @ep: Endpoint 0
1500 * @buff: Buffer for request
1501 * @length: Length of reply.
1502 *
1503 * Create a request and queue it on the given endpoint. This is useful as
1504 * an internal method of sending replies to certain control requests, etc.
1505 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001506static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001507 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001508 void *buff,
1509 int length)
1510{
1511 struct usb_request *req;
1512 int ret;
1513
1514 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1515
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001516 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001517 hsotg->ep0_reply = req;
1518 if (!req) {
1519 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1520 return -ENOMEM;
1521 }
1522
1523 req->buf = hsotg->ep0_buff;
1524 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001525 /*
1526 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1527 * STATUS stage.
1528 */
1529 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001530 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001531
1532 if (length)
1533 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001534
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001535 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001536 if (ret) {
1537 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1538 return ret;
1539 }
1540
1541 return 0;
1542}
1543
1544/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001545 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001546 * @hsotg: The device state
1547 * @ctrl: USB control request
1548 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001549static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001550 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001551{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001552 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1553 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001554 __le16 reply;
1555 int ret;
1556
1557 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1558
1559 if (!ep0->dir_in) {
1560 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1561 return -EINVAL;
1562 }
1563
1564 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1565 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001566 /*
1567 * bit 0 => self powered
1568 * bit 1 => remote wakeup
1569 */
1570 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001571 break;
1572
1573 case USB_RECIP_INTERFACE:
1574 /* currently, the data result should be zero */
1575 reply = cpu_to_le16(0);
1576 break;
1577
1578 case USB_RECIP_ENDPOINT:
1579 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1580 if (!ep)
1581 return -ENOENT;
1582
1583 reply = cpu_to_le16(ep->halted ? 1 : 0);
1584 break;
1585
1586 default:
1587 return 0;
1588 }
1589
1590 if (le16_to_cpu(ctrl->wLength) != 2)
1591 return -EINVAL;
1592
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001593 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001594 if (ret) {
1595 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1596 return ret;
1597 }
1598
1599 return 1;
1600}
1601
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001602static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001603
1604/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001605 * get_ep_head - return the first request on the endpoint
1606 * @hs_ep: The controller endpoint to get
1607 *
1608 * Get the first request on the endpoint.
1609 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001610static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001611{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001612 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1613 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001614}
1615
1616/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001617 * dwc2_gadget_start_next_request - Starts next request from ep queue
1618 * @hs_ep: Endpoint structure
1619 *
1620 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1621 * in its handler. Hence we need to unmask it here to be able to do
1622 * resynchronization.
1623 */
1624static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1625{
1626 u32 mask;
1627 struct dwc2_hsotg *hsotg = hs_ep->parent;
1628 int dir_in = hs_ep->dir_in;
1629 struct dwc2_hsotg_req *hs_req;
1630 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1631
1632 if (!list_empty(&hs_ep->queue)) {
1633 hs_req = get_ep_head(hs_ep);
1634 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1635 return;
1636 }
1637 if (!hs_ep->isochronous)
1638 return;
1639
1640 if (dir_in) {
1641 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1642 __func__);
1643 } else {
1644 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1645 __func__);
1646 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1647 mask |= DOEPMSK_OUTTKNEPDISMSK;
1648 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1649 }
1650}
1651
1652/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001653 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001654 * @hsotg: The device state
1655 * @ctrl: USB control request
1656 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001657static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001658 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001659{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001660 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1661 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001662 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001663 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001664 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001665 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001666 u32 recip;
1667 u32 wValue;
1668 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001669
1670 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1671 __func__, set ? "SET" : "CLEAR");
1672
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001673 wValue = le16_to_cpu(ctrl->wValue);
1674 wIndex = le16_to_cpu(ctrl->wIndex);
1675 recip = ctrl->bRequestType & USB_RECIP_MASK;
1676
1677 switch (recip) {
1678 case USB_RECIP_DEVICE:
1679 switch (wValue) {
1680 case USB_DEVICE_TEST_MODE:
1681 if ((wIndex & 0xff) != 0)
1682 return -EINVAL;
1683 if (!set)
1684 return -EINVAL;
1685
1686 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001687 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001688 if (ret) {
1689 dev_err(hsotg->dev,
1690 "%s: failed to send reply\n", __func__);
1691 return ret;
1692 }
1693 break;
1694 default:
1695 return -ENOENT;
1696 }
1697 break;
1698
1699 case USB_RECIP_ENDPOINT:
1700 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001701 if (!ep) {
1702 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001703 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001704 return -ENOENT;
1705 }
1706
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001707 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001708 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001709 halted = ep->halted;
1710
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001711 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001712
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001713 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001714 if (ret) {
1715 dev_err(hsotg->dev,
1716 "%s: failed to send reply\n", __func__);
1717 return ret;
1718 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001719
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001720 /*
1721 * we have to complete all requests for ep if it was
1722 * halted, and the halt was cleared by CLEAR_FEATURE
1723 */
1724
1725 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001726 /*
1727 * If we have request in progress,
1728 * then complete it
1729 */
1730 if (ep->req) {
1731 hs_req = ep->req;
1732 ep->req = NULL;
1733 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001734 if (hs_req->req.complete) {
1735 spin_unlock(&hsotg->lock);
1736 usb_gadget_giveback_request(
1737 &ep->ep, &hs_req->req);
1738 spin_lock(&hsotg->lock);
1739 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001740 }
1741
1742 /* If we have pending request, then start it */
John Youn34c08872017-01-17 20:31:43 -08001743 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001744 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001745 }
1746
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001747 break;
1748
1749 default:
1750 return -ENOENT;
1751 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001752 break;
1753 default:
1754 return -ENOENT;
1755 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001756 return 1;
1757}
1758
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001759static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001760
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001761/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001762 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001763 * @hsotg: The device state
1764 *
1765 * Set stall for ep0 as response for setup request.
1766 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001767static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001768{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001769 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001770 u32 reg;
1771 u32 ctrl;
1772
1773 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1774 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1775
1776 /*
1777 * DxEPCTL_Stall will be cleared by EP once it has
1778 * taken effect, so no need to clear later.
1779 */
1780
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001781 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001782 ctrl |= DXEPCTL_STALL;
1783 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001784 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001785
1786 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001787 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001788 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001789
1790 /*
1791 * complete won't be called, so we enqueue
1792 * setup request here
1793 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001794 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001795}
1796
1797/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001798 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001799 * @hsotg: The device state
1800 * @ctrl: The control request received
1801 *
1802 * The controller has received the SETUP phase of a control request, and
1803 * needs to work out what to do next (and whether to pass it on to the
1804 * gadget driver).
1805 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001806static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001807 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001808{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001809 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001810 int ret = 0;
1811 u32 dcfg;
1812
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001813 dev_dbg(hsotg->dev,
1814 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1815 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1816 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001817
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001818 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001819 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001820 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1821 } else if (ctrl->bRequestType & USB_DIR_IN) {
1822 ep0->dir_in = 1;
1823 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1824 } else {
1825 ep0->dir_in = 0;
1826 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1827 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001828
1829 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1830 switch (ctrl->bRequest) {
1831 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001832 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001833 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001834 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001835 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1836 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001837 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001838
1839 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1840
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001841 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001842 return;
1843
1844 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001845 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001846 break;
1847
1848 case USB_REQ_CLEAR_FEATURE:
1849 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001850 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001851 break;
1852 }
1853 }
1854
1855 /* as a fallback, try delivering it to the driver to deal with */
1856
1857 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001858 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001859 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001860 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001861 if (ret < 0)
1862 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1863 }
1864
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001865 /*
1866 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001867 * so respond with a STALL for the status stage to indicate failure.
1868 */
1869
Robert Baldygac9f721b2014-01-14 08:36:00 +01001870 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001871 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001872}
1873
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001874/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001875 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001876 * @ep: The endpoint the request was on.
1877 * @req: The request completed.
1878 *
1879 * Called on completion of any requests the driver itself submitted for
1880 * EP0 setup packets
1881 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001882static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001883 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001884{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001885 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001886 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001887
1888 if (req->status < 0) {
1889 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1890 return;
1891 }
1892
Robert Baldyga93f599f2013-11-21 13:49:17 +01001893 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001894 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001895 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001896 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001897 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001898 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001899}
1900
1901/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001902 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001903 * @hsotg: The device state.
1904 *
1905 * Enqueue a request on EP0 if necessary to received any SETUP packets
1906 * received from the host.
1907 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001908static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001909{
1910 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001911 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001912 int ret;
1913
1914 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1915
1916 req->zero = 0;
1917 req->length = 8;
1918 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001919 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001920
1921 if (!list_empty(&hs_req->queue)) {
1922 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1923 return;
1924 }
1925
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001926 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001927 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001928 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001929
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001930 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001931 if (ret < 0) {
1932 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001933 /*
1934 * Don't think there's much we can do other than watch the
1935 * driver fail.
1936 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001937 }
1938}
1939
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001940static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001941 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001942{
1943 u32 ctrl;
1944 u8 index = hs_ep->index;
1945 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1946 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1947
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001948 if (hs_ep->dir_in)
1949 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001950 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001951 else
1952 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001953 index);
1954 if (using_desc_dma(hsotg)) {
1955 /* Not specific buffer needed for ep0 ZLP */
1956 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001957
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001958 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1959 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1960 } else {
1961 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1962 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1963 epsiz_reg);
1964 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001965
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001966 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001967 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1968 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1969 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001970 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001971}
1972
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001973/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001974 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001975 * @hsotg: The device state.
1976 * @hs_ep: The endpoint the request was on.
1977 * @hs_req: The request to complete.
1978 * @result: The result code (0 => Ok, otherwise errno)
1979 *
1980 * The given request has finished, so call the necessary completion
1981 * if it has one and then look to see if we can start a new request
1982 * on the endpoint.
1983 *
1984 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001985 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001986static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001987 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001988 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001989 int result)
1990{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001991 if (!hs_req) {
1992 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1993 return;
1994 }
1995
1996 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1997 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1998
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001999 /*
2000 * only replace the status if we've not already set an error
2001 * from a previous transaction
2002 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002003
2004 if (hs_req->req.status == -EINPROGRESS)
2005 hs_req->req.status = result;
2006
Yunzhi Li44583fe2015-09-29 12:25:01 +02002007 if (using_dma(hsotg))
2008 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2009
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002010 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01002011
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002012 hs_ep->req = NULL;
2013 list_del_init(&hs_req->queue);
2014
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002015 /*
2016 * call the complete request with the locks off, just in case the
2017 * request tries to queue more work for this endpoint.
2018 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002019
2020 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002021 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002022 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002023 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002024 }
2025
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002026 /* In DDMA don't need to proceed to starting of next ISOC request */
2027 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2028 return;
2029
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002030 /*
2031 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002032 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002033 * so be careful when doing this.
2034 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002035
John Youn34c08872017-01-17 20:31:43 -08002036 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002037 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002038}
2039
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002040/*
2041 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2042 * @hs_ep: The endpoint the request was on.
2043 *
2044 * Get first request from the ep queue, determine descriptor on which complete
2045 * happened. SW based on isoc_chain_num discovers which half of the descriptor
2046 * chain is currently in use by HW, adjusts dma_address and calculates index
2047 * of completed descriptor based on the value of DEPDMA register. Update actual
2048 * length of request, giveback to gadget.
2049 */
2050static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2051{
2052 struct dwc2_hsotg *hsotg = hs_ep->parent;
2053 struct dwc2_hsotg_req *hs_req;
2054 struct usb_request *ureq;
2055 int index;
2056 dma_addr_t dma_addr;
2057 u32 dma_reg;
2058 u32 depdma;
2059 u32 desc_sts;
2060 u32 mask;
2061
2062 hs_req = get_ep_head(hs_ep);
2063 if (!hs_req) {
2064 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2065 return;
2066 }
2067 ureq = &hs_req->req;
2068
2069 dma_addr = hs_ep->desc_list_dma;
2070
2071 /*
2072 * If lower half of descriptor chain is currently use by SW,
2073 * that means higher half is being processed by HW, so shift
2074 * DMA address to higher half of descriptor chain.
2075 */
2076 if (!hs_ep->isoc_chain_num)
2077 dma_addr += sizeof(struct dwc2_dma_desc) *
2078 (MAX_DMA_DESC_NUM_GENERIC / 2);
2079
2080 dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index);
2081 depdma = dwc2_readl(hsotg->regs + dma_reg);
2082
2083 index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1;
2084 desc_sts = hs_ep->desc_list[index].status;
2085
2086 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2087 DEV_DMA_ISOC_RX_NBYTES_MASK;
2088 ureq->actual = ureq->length -
2089 ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT);
2090
Vahram Aharonyan95d2b032016-11-14 19:16:46 -08002091 /* Adjust actual length for ISOC Out if length is not align of 4 */
2092 if (!hs_ep->dir_in && ureq->length & 0x3)
2093 ureq->actual += 4 - (ureq->length & 0x3);
2094
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002095 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2096}
2097
2098/*
2099 * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any.
2100 * @hs_ep: The isochronous endpoint to be re-enabled.
2101 *
2102 * If ep has been disabled due to last descriptor servicing (IN endpoint) or
2103 * BNA (OUT endpoint) check the status of other half of descriptor chain that
2104 * was under SW control till HW was busy and restart the endpoint if needed.
2105 */
2106static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
2107{
2108 struct dwc2_hsotg *hsotg = hs_ep->parent;
2109 u32 depctl;
2110 u32 dma_reg;
2111 u32 ctrl;
2112 u32 dma_addr = hs_ep->desc_list_dma;
2113 unsigned char index = hs_ep->index;
2114
2115 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
2116 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
2117
2118 ctrl = dwc2_readl(hsotg->regs + depctl);
2119
2120 /*
2121 * EP was disabled if HW has processed last descriptor or BNA was set.
2122 * So restart ep if SW has prepared new descriptor chain in ep_queue
2123 * routine while HW was busy.
2124 */
2125 if (!(ctrl & DXEPCTL_EPENA)) {
2126 if (!hs_ep->next_desc) {
2127 dev_dbg(hsotg->dev, "%s: No more ISOC requests\n",
2128 __func__);
2129 return;
2130 }
2131
2132 dma_addr += sizeof(struct dwc2_dma_desc) *
2133 (MAX_DMA_DESC_NUM_GENERIC / 2) *
2134 hs_ep->isoc_chain_num;
2135 dwc2_writel(dma_addr, hsotg->regs + dma_reg);
2136
2137 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
2138 dwc2_writel(ctrl, hsotg->regs + depctl);
2139
2140 /* Switch ISOC descriptor chain number being processed by SW*/
2141 hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
2142 hs_ep->next_desc = 0;
2143
2144 dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n",
2145 __func__);
2146 }
2147}
2148
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002149/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002150 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002151 * @hsotg: The device state.
2152 * @ep_idx: The endpoint index for the data
2153 * @size: The size of data in the fifo, in bytes
2154 *
2155 * The FIFO status shows there is data to read from the FIFO for a given
2156 * endpoint, so sort out whether we need to read the data into a request
2157 * that has been made for that endpoint.
2158 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002159static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002160{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002161 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2162 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002163 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002164 int to_read;
2165 int max_req;
2166 int read_ptr;
2167
2168 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002169 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002170 int ptr;
2171
Robert Baldyga6b448af2014-12-16 11:51:44 +01002172 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002173 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002174 __func__, size, ep_idx, epctl);
2175
2176 /* dump the data from the FIFO, we've nothing we can do */
2177 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002178 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002179
2180 return;
2181 }
2182
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002183 to_read = size;
2184 read_ptr = hs_req->req.actual;
2185 max_req = hs_req->req.length - read_ptr;
2186
Ben Dooksa33e7132010-07-19 09:40:49 +01002187 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2188 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2189
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002190 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002191 /*
2192 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002193 * to deal with in this request.
2194 */
2195
2196 /* currently we don't deal this */
2197 WARN_ON_ONCE(1);
2198 }
2199
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002200 hs_ep->total_data += to_read;
2201 hs_req->req.actual += to_read;
2202 to_read = DIV_ROUND_UP(to_read, 4);
2203
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002204 /*
2205 * note, we might over-write the buffer end by 3 bytes depending on
2206 * alignment of the data.
2207 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05002208 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002209}
2210
2211/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002212 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002213 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002214 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002215 *
2216 * Generate a zero-length IN packet request for terminating a SETUP
2217 * transaction.
2218 *
2219 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002220 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002221 * the TxFIFO.
2222 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002223static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002224{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002225 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002226 hsotg->eps_out[0]->dir_in = dir_in;
2227 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002228
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002229 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002230}
2231
Roman Bacikec1f9d92015-09-10 18:13:43 -07002232static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002233 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002234{
2235 u32 ctrl;
2236
2237 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
2238 if (ctrl & DXEPCTL_EOFRNUM)
2239 ctrl |= DXEPCTL_SETEVENFR;
2240 else
2241 ctrl |= DXEPCTL_SETODDFR;
2242 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
2243}
2244
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002245/*
2246 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2247 * @hs_ep - The endpoint on which transfer went
2248 *
2249 * Iterate over endpoints descriptor chain and get info on bytes remained
2250 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2251 */
2252static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2253{
2254 struct dwc2_hsotg *hsotg = hs_ep->parent;
2255 unsigned int bytes_rem = 0;
2256 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2257 int i;
2258 u32 status;
2259
2260 if (!desc)
2261 return -EINVAL;
2262
2263 for (i = 0; i < hs_ep->desc_count; ++i) {
2264 status = desc->status;
2265 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2266
2267 if (status & DEV_DMA_STS_MASK)
2268 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2269 i, status & DEV_DMA_STS_MASK);
2270 }
2271
2272 return bytes_rem;
2273}
2274
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002275/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002276 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002277 * @hsotg: The device instance
2278 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002279 *
2280 * The RXFIFO has delivered an OutDone event, which means that the data
2281 * transfer for an OUT endpoint has been completed, either by a short
2282 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002283 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002284static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002285{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002286 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002287 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2288 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002289 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002290 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002291 int result = 0;
2292
2293 if (!hs_req) {
2294 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2295 return;
2296 }
2297
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002298 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2299 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002300 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2301 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002302 return;
2303 }
2304
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002305 if (using_desc_dma(hsotg))
2306 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2307
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002308 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002309 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002310
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002311 /*
2312 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002313 * is left in the endpoint size register and then working it
2314 * out from the amount we loaded for the transfer.
2315 *
2316 * We need to do this as DMA pointers are always 32bit aligned
2317 * so may overshoot/undershoot the transfer.
2318 */
2319
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002320 size_done = hs_ep->size_loaded - size_left;
2321 size_done += hs_ep->last_load;
2322
2323 req->actual = size_done;
2324 }
2325
Ben Dooksa33e7132010-07-19 09:40:49 +01002326 /* if there is more request to do, schedule new transfer */
2327 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002328 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002329 return;
2330 }
2331
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002332 if (req->actual < req->length && req->short_not_ok) {
2333 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2334 __func__, req->actual, req->length);
2335
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002336 /*
2337 * todo - what should we return here? there's no one else
2338 * even bothering to check the status.
2339 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002340 }
2341
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002342 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2343 if (!using_desc_dma(hsotg) && epnum == 0 &&
2344 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002345 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002346 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002347 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002348 }
2349
Roman Bacikec1f9d92015-09-10 18:13:43 -07002350 /*
2351 * Slave mode OUT transfers do not go through XferComplete so
2352 * adjust the ISOC parity here.
2353 */
2354 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002355 if (hs_ep->isochronous && hs_ep->interval == 1)
2356 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002357 else if (hs_ep->isochronous && hs_ep->interval > 1)
2358 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002359 }
2360
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002361 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362}
2363
2364/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002365 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366 * @hsotg: The device instance
2367 *
2368 * The IRQ handler has detected that the RX FIFO has some data in it
2369 * that requires processing, so find out what is in there and do the
2370 * appropriate read.
2371 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002372 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002373 * chunks, so if you have x packets received on an endpoint you'll get x
2374 * FIFO events delivered, each with a packet's worth of data in it.
2375 *
2376 * When using DMA, we should not be processing events from the RXFIFO
2377 * as the actual data should be sent to the memory directly and we turn
2378 * on the completion interrupts to get notifications of transfer completion.
2379 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002380static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002381{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002382 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002383 u32 epnum, status, size;
2384
2385 WARN_ON(using_dma(hsotg));
2386
Dinh Nguyen47a16852014-04-14 14:13:34 -07002387 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2388 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002389
Dinh Nguyen47a16852014-04-14 14:13:34 -07002390 size = grxstsr & GRXSTS_BYTECNT_MASK;
2391 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002392
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002393 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002394 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002395
Dinh Nguyen47a16852014-04-14 14:13:34 -07002396 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2397 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2398 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002399 break;
2400
Dinh Nguyen47a16852014-04-14 14:13:34 -07002401 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002402 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002403 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002404
2405 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002406 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002407 break;
2408
Dinh Nguyen47a16852014-04-14 14:13:34 -07002409 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002410 dev_dbg(hsotg->dev,
2411 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002412 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002413 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002414 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002415 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002416 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2417 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2418 */
2419 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002420 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002421 break;
2422
Dinh Nguyen47a16852014-04-14 14:13:34 -07002423 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002424 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002425 break;
2426
Dinh Nguyen47a16852014-04-14 14:13:34 -07002427 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002428 dev_dbg(hsotg->dev,
2429 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002430 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002431 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002432
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002433 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2434
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002435 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002436 break;
2437
2438 default:
2439 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2440 __func__, grxstsr);
2441
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002442 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002443 break;
2444 }
2445}
2446
2447/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002448 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002449 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002450 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002451static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002452{
2453 switch (mps) {
2454 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002455 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002456 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002457 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002458 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002459 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002460 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002461 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002462 }
2463
2464 /* bad max packet size, warn and return invalid result */
2465 WARN_ON(1);
2466 return (u32)-1;
2467}
2468
2469/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002470 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002471 * @hsotg: The driver state.
2472 * @ep: The index number of the endpoint
2473 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002474 * @mc: The multicount value
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002475 *
2476 * Configure the maximum packet size for the given endpoint, updating
2477 * the hardware control registers to reflect this.
2478 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002479static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002480 unsigned int ep, unsigned int mps,
2481 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002482{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002483 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002484 void __iomem *regs = hsotg->regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002485 u32 reg;
2486
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002487 hs_ep = index_to_ep(hsotg, ep, dir_in);
2488 if (!hs_ep)
2489 return;
2490
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002491 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002492 u32 mps_bytes = mps;
2493
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002494 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002495 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2496 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002497 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002498 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002499 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002500 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002501 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002502 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002503 hs_ep->mc = mc;
2504 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002505 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002506 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002507 }
2508
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002509 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002510 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002511 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002512 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002513 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002514 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002515 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002516 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002517 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002518 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002519 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520
2521 return;
2522
2523bad_mps:
2524 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2525}
2526
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002527/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002528 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002529 * @hsotg: The driver state
2530 * @idx: The index for the endpoint (0..15)
2531 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002532static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002533{
2534 int timeout;
2535 int val;
2536
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002537 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2538 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002539
2540 /* wait until the fifo is flushed */
2541 timeout = 100;
2542
2543 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002544 val = dwc2_readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002545
Dinh Nguyen47a16852014-04-14 14:13:34 -07002546 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002547 break;
2548
2549 if (--timeout == 0) {
2550 dev_err(hsotg->dev,
2551 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
2552 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02002553 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002554 }
2555
2556 udelay(1);
2557 }
2558}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002559
2560/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002561 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002562 * @hsotg: The driver state
2563 * @hs_ep: The driver endpoint to check.
2564 *
2565 * Check to see if there is a request that has data to send, and if so
2566 * make an attempt to write data into the FIFO.
2567 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002568static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002569 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002570{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002571 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002572
Robert Baldygaafcf4162013-09-19 11:50:19 +02002573 if (!hs_ep->dir_in || !hs_req) {
2574 /**
2575 * if request is not enqueued, we disable interrupts
2576 * for endpoints, excepting ep0
2577 */
2578 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002579 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002580 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002581 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002582 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002583
2584 if (hs_req->req.actual < hs_req->req.length) {
2585 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2586 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002587 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002588 }
2589
2590 return 0;
2591}
2592
2593/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002594 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002595 * @hsotg: The device state.
2596 * @hs_ep: The endpoint that has just completed.
2597 *
2598 * An IN transfer has been completed, update the transfer's state and then
2599 * call the relevant completion routines.
2600 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002601static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002602 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002603{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002604 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002605 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002606 int size_left, size_done;
2607
2608 if (!hs_req) {
2609 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2610 return;
2611 }
2612
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002613 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002614 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2615 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002616
2617 /*
2618 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2619 * changed to IN. Change back to complete OUT transfer request
2620 */
2621 hs_ep->dir_in = 0;
2622
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002623 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002624 if (hsotg->test_mode) {
2625 int ret;
2626
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002627 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002628 if (ret < 0) {
2629 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002630 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002631 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002632 return;
2633 }
2634 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002635 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002636 return;
2637 }
2638
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002639 /*
2640 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002641 * in the endpoint size register and then working it out from
2642 * the amount we loaded for the transfer.
2643 *
2644 * We do this even for DMA, as the transfer may have incremented
2645 * past the end of the buffer (DMA transfers are always 32bit
2646 * aligned).
2647 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002648 if (using_desc_dma(hsotg)) {
2649 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2650 if (size_left < 0)
2651 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2652 size_left);
2653 } else {
2654 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2655 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002656
2657 size_done = hs_ep->size_loaded - size_left;
2658 size_done += hs_ep->last_load;
2659
2660 if (hs_req->req.actual != size_done)
2661 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2662 __func__, hs_req->req.actual, size_done);
2663
2664 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002665 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2666 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002667
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002668 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2669 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002670 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002671 return;
2672 }
2673
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002674 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002675 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002676 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002677 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002678 /* transfer will be completed on next complete interrupt */
2679 return;
2680 }
2681
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002682 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2683 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002684 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002685 return;
2686 }
2687
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002688 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002689}
2690
2691/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002692 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2693 * @hsotg: The device state.
2694 * @idx: Index of ep.
2695 * @dir_in: Endpoint direction 1-in 0-out.
2696 *
2697 * Reads for endpoint with given index and direction, by masking
2698 * epint_reg with coresponding mask.
2699 */
2700static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2701 unsigned int idx, int dir_in)
2702{
2703 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2704 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2705 u32 ints;
2706 u32 mask;
2707 u32 diepempmsk;
2708
2709 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2710 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2711 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2712 mask |= DXEPINT_SETUP_RCVD;
2713
2714 ints = dwc2_readl(hsotg->regs + epint_reg);
2715 ints &= mask;
2716 return ints;
2717}
2718
2719/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002720 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2721 * @hs_ep: The endpoint on which interrupt is asserted.
2722 *
2723 * This interrupt indicates that the endpoint has been disabled per the
2724 * application's request.
2725 *
2726 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2727 * in case of ISOC completes current request.
2728 *
2729 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2730 * request starts it.
2731 */
2732static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2733{
2734 struct dwc2_hsotg *hsotg = hs_ep->parent;
2735 struct dwc2_hsotg_req *hs_req;
2736 unsigned char idx = hs_ep->index;
2737 int dir_in = hs_ep->dir_in;
2738 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2739 int dctl = dwc2_readl(hsotg->regs + DCTL);
2740
2741 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2742
2743 if (dir_in) {
2744 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2745
2746 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2747
2748 if (hs_ep->isochronous) {
2749 dwc2_hsotg_complete_in(hsotg, hs_ep);
2750 return;
2751 }
2752
2753 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2754 int dctl = dwc2_readl(hsotg->regs + DCTL);
2755
2756 dctl |= DCTL_CGNPINNAK;
2757 dwc2_writel(dctl, hsotg->regs + DCTL);
2758 }
2759 return;
2760 }
2761
2762 if (dctl & DCTL_GOUTNAKSTS) {
2763 dctl |= DCTL_CGOUTNAK;
2764 dwc2_writel(dctl, hsotg->regs + DCTL);
2765 }
2766
2767 if (!hs_ep->isochronous)
2768 return;
2769
2770 if (list_empty(&hs_ep->queue)) {
2771 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2772 __func__, hs_ep);
2773 return;
2774 }
2775
2776 do {
2777 hs_req = get_ep_head(hs_ep);
2778 if (hs_req)
2779 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2780 -ENODATA);
2781 dwc2_gadget_incr_frame_num(hs_ep);
2782 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2783
2784 dwc2_gadget_start_next_request(hs_ep);
2785}
2786
2787/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002788 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2789 * @hs_ep: The endpoint on which interrupt is asserted.
2790 *
2791 * This is starting point for ISOC-OUT transfer, synchronization done with
2792 * first out token received from host while corresponding EP is disabled.
2793 *
2794 * Device does not know initial frame in which out token will come. For this
2795 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2796 * getting this interrupt SW starts calculation for next transfer frame.
2797 */
2798static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2799{
2800 struct dwc2_hsotg *hsotg = ep->parent;
2801 int dir_in = ep->dir_in;
2802 u32 doepmsk;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002803 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002804
2805 if (dir_in || !ep->isochronous)
2806 return;
2807
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002808 /*
2809 * Store frame in which irq was asserted here, as
2810 * it can change while completing request below.
2811 */
2812 tmp = dwc2_hsotg_read_frameno(hsotg);
2813
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002814 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
2815
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002816 if (using_desc_dma(hsotg)) {
2817 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2818 /* Start first ISO Out */
2819 ep->target_frame = tmp;
2820 dwc2_gadget_start_isoc_ddma(ep);
2821 }
2822 return;
2823 }
2824
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002825 if (ep->interval > 1 &&
2826 ep->target_frame == TARGET_FRAME_INITIAL) {
2827 u32 dsts;
2828 u32 ctrl;
2829
2830 dsts = dwc2_readl(hsotg->regs + DSTS);
2831 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2832 dwc2_gadget_incr_frame_num(ep);
2833
2834 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2835 if (ep->target_frame & 0x1)
2836 ctrl |= DXEPCTL_SETODDFR;
2837 else
2838 ctrl |= DXEPCTL_SETEVENFR;
2839
2840 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2841 }
2842
2843 dwc2_gadget_start_next_request(ep);
2844 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2845 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2846 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2847}
2848
2849/**
John Youn38beaec2017-01-17 20:31:13 -08002850 * dwc2_gadget_handle_nak - handle NAK interrupt
2851 * @hs_ep: The endpoint on which interrupt is asserted.
2852 *
2853 * This is starting point for ISOC-IN transfer, synchronization done with
2854 * first IN token received from host while corresponding EP is disabled.
2855 *
2856 * Device does not know when first one token will arrive from host. On first
2857 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2858 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2859 * sent in response to that as there was no data in FIFO. SW is basing on this
2860 * interrupt to obtain frame in which token has come and then based on the
2861 * interval calculates next frame for transfer.
2862 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002863static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2864{
2865 struct dwc2_hsotg *hsotg = hs_ep->parent;
2866 int dir_in = hs_ep->dir_in;
2867
2868 if (!dir_in || !hs_ep->isochronous)
2869 return;
2870
2871 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
2872 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002873
2874 if (using_desc_dma(hsotg)) {
2875 dwc2_gadget_start_isoc_ddma(hs_ep);
2876 return;
2877 }
2878
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002879 if (hs_ep->interval > 1) {
2880 u32 ctrl = dwc2_readl(hsotg->regs +
2881 DIEPCTL(hs_ep->index));
2882 if (hs_ep->target_frame & 0x1)
2883 ctrl |= DXEPCTL_SETODDFR;
2884 else
2885 ctrl |= DXEPCTL_SETEVENFR;
2886
2887 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2888 }
2889
2890 dwc2_hsotg_complete_request(hsotg, hs_ep,
2891 get_ep_head(hs_ep), 0);
2892 }
2893
2894 dwc2_gadget_incr_frame_num(hs_ep);
2895}
2896
2897/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002898 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002899 * @hsotg: The driver state
2900 * @idx: The index for the endpoint (0..15)
2901 * @dir_in: Set if this is an IN endpoint
2902 *
2903 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002904 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002905static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002906 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002907{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002908 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002909 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2910 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2911 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002912 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002913 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002914
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002915 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002916 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002917
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002918 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002919 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002920
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002921 if (!hs_ep) {
2922 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002923 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002924 return;
2925 }
2926
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002927 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2928 __func__, idx, dir_in ? "in" : "out", ints);
2929
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002930 /* Don't process XferCompl interrupt if it is a setup packet */
2931 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2932 ints &= ~DXEPINT_XFERCOMPL;
2933
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002934 /*
2935 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2936 * stage and xfercomplete was generated without SETUP phase done
2937 * interrupt. SW should parse received setup packet only after host's
2938 * exit from setup phase of control transfer.
2939 */
2940 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2941 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2942 ints &= ~DXEPINT_XFERCOMPL;
2943
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002944 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002945 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002946 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002947 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2948 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002949
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002950 /* In DDMA handle isochronous requests separately */
2951 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
2952 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
2953 /* Try to start next isoc request */
2954 dwc2_gadget_start_next_isoc_ddma(hs_ep);
2955 } else if (dir_in) {
2956 /*
2957 * We get OutDone from the FIFO, so we only
2958 * need to look at completing IN requests here
2959 * if operating slave mode
2960 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002961 if (hs_ep->isochronous && hs_ep->interval > 1)
2962 dwc2_gadget_incr_frame_num(hs_ep);
2963
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002964 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002965 if (ints & DXEPINT_NAKINTRPT)
2966 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002967
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002968 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002969 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002970 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002971 /*
2972 * We're using DMA, we need to fire an OutDone here
2973 * as we ignore the RXFIFO.
2974 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002975 if (hs_ep->isochronous && hs_ep->interval > 1)
2976 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002977
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002978 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002979 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002980 }
2981
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002982 if (ints & DXEPINT_EPDISBLD)
2983 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002984
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002985 if (ints & DXEPINT_OUTTKNEPDIS)
2986 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2987
2988 if (ints & DXEPINT_NAKINTRPT)
2989 dwc2_gadget_handle_nak(hs_ep);
2990
Dinh Nguyen47a16852014-04-14 14:13:34 -07002991 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002992 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002993
Dinh Nguyen47a16852014-04-14 14:13:34 -07002994 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002995 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2996
2997 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002998 /*
2999 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003000 * setup packet. In non-DMA mode we'd get this
3001 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003002 * the setup here.
3003 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003004
3005 if (dir_in)
3006 WARN_ON_ONCE(1);
3007 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003008 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003009 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003010 }
3011
Vahram Aharonyanef750c72016-11-14 19:16:31 -08003012 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003013 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
3014
Vahram Aharonyanef750c72016-11-14 19:16:31 -08003015 /* Move to STATUS IN for DDMA */
3016 if (using_desc_dma(hsotg))
3017 dwc2_hsotg_ep0_zlp(hsotg, true);
3018 }
3019
Dinh Nguyen47a16852014-04-14 14:13:34 -07003020 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003021 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003022
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003023 if (ints & DXEPINT_BNAINTR) {
3024 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
3025
3026 /*
3027 * Try to start next isoc request, if any.
3028 * Sometimes the endpoint remains enabled after BNA interrupt
3029 * assertion, which is not expected, hence we can enter here
3030 * couple of times.
3031 */
3032 if (hs_ep->isochronous)
3033 dwc2_gadget_start_next_isoc_ddma(hs_ep);
3034 }
3035
Robert Baldyga1479e842013-10-09 08:41:57 +02003036 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003037 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003038 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003039 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
3040 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003041 }
3042
3043 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003044 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003045 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
3046 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003047 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003048
3049 /* FIFO has space or is empty (see GAHBCFG) */
3050 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003051 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01003052 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3053 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09003054 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003055 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01003056 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003057 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003058}
3059
3060/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003061 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003062 * @hsotg: The device state.
3063 *
3064 * Handle updating the device settings after the enumeration phase has
3065 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003066 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003067static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003068{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003069 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003070 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003071
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003072 /*
3073 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003074 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003075 * we connected at.
3076 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003077
3078 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3079
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003080 /*
3081 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003083 * not advertise a 64byte MPS on EP0.
3084 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003085
3086 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003087 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003088 case DSTS_ENUMSPD_FS:
3089 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003090 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003091 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003092 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003093 break;
3094
Dinh Nguyen47a16852014-04-14 14:13:34 -07003095 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003096 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003097 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003098 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003099 break;
3100
Dinh Nguyen47a16852014-04-14 14:13:34 -07003101 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003102 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003103 ep0_mps = 8;
3104 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003105 /*
3106 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003107 * moment, and the documentation seems to imply that it isn't
3108 * supported by the PHYs on some of the devices.
3109 */
3110 break;
3111 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003112 dev_info(hsotg->dev, "new device is %s\n",
3113 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003114
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003115 /*
3116 * we should now know the maximum packet size for an
3117 * endpoint, so set the endpoints to a default value.
3118 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003119
3120 if (ep0_mps) {
3121 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003122 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003123 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3124 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003125 for (i = 1; i < hsotg->num_of_eps; i++) {
3126 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003127 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3128 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003129 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003130 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3131 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003132 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003133 }
3134
3135 /* ensure after enumeration our EP0 is active */
3136
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003137 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003138
3139 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003140 dwc2_readl(hsotg->regs + DIEPCTL0),
3141 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003142}
3143
3144/**
3145 * kill_all_requests - remove all requests from the endpoint's queue
3146 * @hsotg: The device state.
3147 * @ep: The endpoint the requests may be on.
3148 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003149 *
3150 * Go through the requests on the given endpoint and mark them
3151 * completed with the given result code.
3152 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003153static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003154 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01003155 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003156{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003157 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003158 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003159
Robert Baldyga6b448af2014-12-16 11:51:44 +01003160 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003161
Robert Baldyga6b448af2014-12-16 11:51:44 +01003162 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003163 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003164 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01003165
Robert Baldygab203d0a2014-09-09 10:44:56 +02003166 if (!hsotg->dedicated_fifos)
3167 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07003168 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003169 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003170 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003171}
3172
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003173/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003174 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003175 * @hsotg: The device state.
3176 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003177 * The device has been disconnected. Remove all current
3178 * transactions and signal the gadget driver that this
3179 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003180 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003181void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003182{
John Youn9da51972017-01-17 20:30:27 -08003183 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003184
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003185 if (!hsotg->connected)
3186 return;
3187
3188 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003189 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003190
3191 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3192 if (hsotg->eps_in[ep])
3193 kill_all_requests(hsotg, hsotg->eps_in[ep],
John Youn9da51972017-01-17 20:30:27 -08003194 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003195 if (hsotg->eps_out[ep])
3196 kill_all_requests(hsotg, hsotg->eps_out[ep],
John Youn9da51972017-01-17 20:30:27 -08003197 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003198 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003199
3200 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003201 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003202
3203 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003204}
3205
3206/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003207 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003208 * @hsotg: The device state:
3209 * @periodic: True if this is a periodic FIFO interrupt
3210 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003211static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003212{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003213 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003214 int epno, ret;
3215
3216 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003217 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003218 ep = index_to_ep(hsotg, epno, 1);
3219
3220 if (!ep)
3221 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003222
3223 if (!ep->dir_in)
3224 continue;
3225
3226 if ((periodic && !ep->periodic) ||
3227 (!periodic && ep->periodic))
3228 continue;
3229
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003230 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003231 if (ret < 0)
3232 break;
3233 }
3234}
3235
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003236/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003237#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3238 GINTSTS_PTXFEMP | \
3239 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003240
3241/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003242 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003243 * @hsotg: The device state
3244 *
3245 * Issue a soft reset to the core, and await the core finishing it.
3246 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003247void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003248 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003249{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003250 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003251 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003252 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003253 u32 dcfg = 0;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003254
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003255 /* Kill any ep0 requests as controller will be reinitialized */
3256 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3257
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003258 if (!is_usb_reset)
John Stultz6e6360b2017-01-23 14:59:14 -08003259 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003260 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02003261
3262 /*
3263 * we must now enable ep0 ready for host detection and then
3264 * set configuration.
3265 */
3266
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003267 /* keep other bits untouched (so e.g. forced modes are not lost) */
3268 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3269 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01003270 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003271
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003272 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003273 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3274 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003275 /* FS/LS Dedicated Transceiver Interface */
3276 usbcfg |= GUSBCFG_PHYSEL;
3277 } else {
3278 /* set the PLL on, remove the HNP/SRP and set the PHY */
3279 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3280 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3281 (val << GUSBCFG_USBTRDTIM_SHIFT);
3282 }
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003283 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003284
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003285 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003286
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003287 if (!is_usb_reset)
3288 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003289
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003290 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003291
3292 switch (hsotg->params.speed) {
3293 case DWC2_SPEED_PARAM_LOW:
3294 dcfg |= DCFG_DEVSPD_LS;
3295 break;
3296 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003297 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3298 dcfg |= DCFG_DEVSPD_FS48;
3299 else
3300 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003301 break;
3302 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003303 dcfg |= DCFG_DEVSPD_HS;
3304 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003305
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003306 dwc2_writel(dcfg, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003307
3308 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003309 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003310
3311 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003312 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003313 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003314 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003315 GINTSTS_USBRST | GINTSTS_RESETDET |
3316 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003317 GINTSTS_USBSUSP | GINTSTS_WKUPINT;
3318
3319 if (!using_desc_dma(hsotg))
3320 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003321
John Youn95832c02017-01-23 14:57:26 -08003322 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003323 intmsk |= GINTSTS_CONIDSTSCHNG;
3324
3325 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003326
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003327 if (using_dma(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003328 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
3329 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
3330 hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003331
3332 /* Set DDMA mode support in the core if needed */
3333 if (using_desc_dma(hsotg))
3334 __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
3335
3336 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003337 dwc2_writel(((hsotg->dedicated_fifos) ?
3338 (GAHBCFG_NP_TXF_EMP_LVL |
3339 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3340 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003341 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003342
3343 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003344 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3345 * when we have no data to transfer. Otherwise we get being flooded by
3346 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003347 */
3348
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003349 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003350 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003351 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003352 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003353 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003354
3355 /*
3356 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003357 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003358 */
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003359 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3360 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003361 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003362 DOEPMSK_SETUPMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003363 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003364
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003365 /* Enable BNA interrupt for DDMA */
3366 if (using_desc_dma(hsotg))
3367 __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
3368
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003369 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003370
3371 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003372 dwc2_readl(hsotg->regs + DIEPCTL0),
3373 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003374
3375 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003376 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003377
3378 /*
3379 * Enable the RXFIFO when in slave mode, as this is how we collect
3380 * the data. In DMA mode, we get events from the FIFO but also
3381 * things we cannot process, so do not use it.
3382 */
3383 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003384 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003385
3386 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003387 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3388 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003389
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003390 if (!is_usb_reset) {
3391 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3392 udelay(10); /* see openiboot */
3393 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3394 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003395
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003396 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003397
3398 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003399 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003400 * writing to the EPCTL register..
3401 */
3402
3403 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003404 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003405 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003406
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003407 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003408 DXEPCTL_CNAK | DXEPCTL_EPENA |
3409 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003410 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003411
3412 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003413 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003414 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003415
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003416 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003417
3418 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003419 dwc2_readl(hsotg->regs + DIEPCTL0),
3420 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003421
3422 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003423 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3424 if (!is_usb_reset)
3425 val |= DCTL_SFTDISCON;
3426 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003427
3428 /* must be at-least 3ms to allow bus to see disconnect */
3429 mdelay(3);
3430
Gregory Herrero065d3932015-09-22 15:16:54 +02003431 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003432}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003433
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003434static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003435{
3436 /* set the soft-disconnect bit */
3437 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
3438}
3439
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003440void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003441{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003442 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003443 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003444}
3445
3446/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003447 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3448 * @hsotg: The device state:
3449 *
3450 * This interrupt indicates one of the following conditions occurred while
3451 * transmitting an ISOC transaction.
3452 * - Corrupted IN Token for ISOC EP.
3453 * - Packet not complete in FIFO.
3454 *
3455 * The following actions will be taken:
3456 * - Determine the EP
3457 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3458 */
3459static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3460{
3461 struct dwc2_hsotg_ep *hs_ep;
3462 u32 epctrl;
3463 u32 idx;
3464
3465 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3466
3467 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3468 hs_ep = hsotg->eps_in[idx];
3469 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
3470 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3471 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3472 epctrl |= DXEPCTL_SNAK;
3473 epctrl |= DXEPCTL_EPDIS;
3474 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3475 }
3476 }
3477
3478 /* Clear interrupt */
3479 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3480}
3481
3482/**
3483 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3484 * @hsotg: The device state:
3485 *
3486 * This interrupt indicates one of the following conditions occurred while
3487 * transmitting an ISOC transaction.
3488 * - Corrupted OUT Token for ISOC EP.
3489 * - Packet not complete in FIFO.
3490 *
3491 * The following actions will be taken:
3492 * - Determine the EP
3493 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3494 */
3495static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3496{
3497 u32 gintsts;
3498 u32 gintmsk;
3499 u32 epctrl;
3500 struct dwc2_hsotg_ep *hs_ep;
3501 int idx;
3502
3503 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3504
3505 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3506 hs_ep = hsotg->eps_out[idx];
3507 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3508 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3509 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3510 /* Unmask GOUTNAKEFF interrupt */
3511 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3512 gintmsk |= GINTSTS_GOUTNAKEFF;
3513 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3514
3515 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3516 if (!(gintsts & GINTSTS_GOUTNAKEFF))
3517 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3518 }
3519 }
3520
3521 /* Clear interrupt */
3522 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3523}
3524
3525/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003526 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003527 * @irq: The IRQ number triggered
3528 * @pw: The pw value when registered the handler.
3529 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003530static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003531{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003532 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003533 int retry_count = 8;
3534 u32 gintsts;
3535 u32 gintmsk;
3536
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003537 if (!dwc2_is_device_mode(hsotg))
3538 return IRQ_NONE;
3539
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003540 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003541irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003542 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3543 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003544
3545 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3546 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3547
3548 gintsts &= gintmsk;
3549
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003550 if (gintsts & GINTSTS_RESETDET) {
3551 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3552
3553 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3554
3555 /* This event must be used only if controller is suspended */
3556 if (hsotg->lx_state == DWC2_L2) {
3557 dwc2_exit_hibernation(hsotg, true);
3558 hsotg->lx_state = DWC2_L0;
3559 }
3560 }
3561
3562 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003563 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3564 u32 connected = hsotg->connected;
3565
3566 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3567 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3568 dwc2_readl(hsotg->regs + GNPTXSTS));
3569
3570 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3571
3572 /* Report disconnection if it is not already done. */
3573 dwc2_hsotg_disconnect(hsotg);
3574
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003575 /* Reset device address to zero */
3576 __bic32(hsotg->regs + DCFG, DCFG_DEVADDR_MASK);
3577
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003578 if (usb_status & GOTGCTL_BSESVLD && connected)
3579 dwc2_hsotg_core_init_disconnected(hsotg, true);
3580 }
3581
Dinh Nguyen47a16852014-04-14 14:13:34 -07003582 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003583 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003584
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003585 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003586 }
3587
Dinh Nguyen47a16852014-04-14 14:13:34 -07003588 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003589 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3590 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003591 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003592 int ep;
3593
Robert Baldyga7e804652013-09-19 11:50:20 +02003594 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003595 daint_out = daint >> DAINT_OUTEP_SHIFT;
3596 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003597
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003598 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3599
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003600 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3601 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003602 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003603 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003604 }
3605
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003606 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3607 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003608 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003609 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003610 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003611 }
3612
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003613 /* check both FIFOs */
3614
Dinh Nguyen47a16852014-04-14 14:13:34 -07003615 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003616 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3617
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003618 /*
3619 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003620 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003621 * it needs re-enabling
3622 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003623
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003624 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3625 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003626 }
3627
Dinh Nguyen47a16852014-04-14 14:13:34 -07003628 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003629 dev_dbg(hsotg->dev, "PTxFEmp\n");
3630
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003631 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003632
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003633 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3634 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003635 }
3636
Dinh Nguyen47a16852014-04-14 14:13:34 -07003637 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003638 /*
3639 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003640 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003641 * set.
3642 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003643
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003644 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003645 }
3646
Dinh Nguyen47a16852014-04-14 14:13:34 -07003647 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003648 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003649 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003650 }
3651
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003652 /*
3653 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003654 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003655 * the occurrence.
3656 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003657
Dinh Nguyen47a16852014-04-14 14:13:34 -07003658 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003659 u8 idx;
3660 u32 epctrl;
3661 u32 gintmsk;
3662 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003663
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003664 /* Mask this interrupt */
3665 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3666 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3667 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003668
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003669 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3670 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3671 hs_ep = hsotg->eps_out[idx];
3672 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3673
3674 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
3675 epctrl |= DXEPCTL_SNAK;
3676 epctrl |= DXEPCTL_EPDIS;
3677 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3678 }
3679 }
3680
3681 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003682 }
3683
Dinh Nguyen47a16852014-04-14 14:13:34 -07003684 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003685 dev_info(hsotg->dev, "GINNakEff triggered\n");
3686
Gregory Herrero3be99cd2015-12-07 12:07:31 +01003687 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003688
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003689 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003690 }
3691
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003692 if (gintsts & GINTSTS_INCOMPL_SOIN)
3693 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003694
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003695 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3696 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003697
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003698 /*
3699 * if we've had fifo events, we should try and go around the
3700 * loop again to see if there's any point in returning yet.
3701 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003702
3703 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003704 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003705
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003706 spin_unlock(&hsotg->lock);
3707
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003708 return IRQ_HANDLED;
3709}
3710
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003711static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3712 u32 bit, u32 timeout)
3713{
3714 u32 i;
3715
3716 for (i = 0; i < timeout; i++) {
3717 if (dwc2_readl(hs_otg->regs + reg) & bit)
3718 return 0;
3719 udelay(1);
3720 }
3721
3722 return -ETIMEDOUT;
3723}
3724
3725static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3726 struct dwc2_hsotg_ep *hs_ep)
3727{
3728 u32 epctrl_reg;
3729 u32 epint_reg;
3730
3731 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3732 DOEPCTL(hs_ep->index);
3733 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3734 DOEPINT(hs_ep->index);
3735
3736 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3737 hs_ep->name);
3738
3739 if (hs_ep->dir_in) {
3740 if (hsotg->dedicated_fifos || hs_ep->periodic) {
3741 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3742 /* Wait for Nak effect */
3743 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3744 DXEPINT_INEPNAKEFF, 100))
3745 dev_warn(hsotg->dev,
3746 "%s: timeout DIEPINT.NAKEFF\n",
3747 __func__);
3748 } else {
3749 __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
3750 /* Wait for Nak effect */
3751 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3752 GINTSTS_GINNAKEFF, 100))
3753 dev_warn(hsotg->dev,
3754 "%s: timeout GINTSTS.GINNAKEFF\n",
3755 __func__);
3756 }
3757 } else {
3758 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3759 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3760
3761 /* Wait for global nak to take effect */
3762 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3763 GINTSTS_GOUTNAKEFF, 100))
3764 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3765 __func__);
3766 }
3767
3768 /* Disable ep */
3769 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3770
3771 /* Wait for ep to be disabled */
3772 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3773 dev_warn(hsotg->dev,
3774 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3775
3776 /* Clear EPDISBLD interrupt */
3777 __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
3778
3779 if (hs_ep->dir_in) {
3780 unsigned short fifo_index;
3781
3782 if (hsotg->dedicated_fifos || hs_ep->periodic)
3783 fifo_index = hs_ep->fifo_index;
3784 else
3785 fifo_index = 0;
3786
3787 /* Flush TX FIFO */
3788 dwc2_flush_tx_fifo(hsotg, fifo_index);
3789
3790 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3791 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
3792 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
3793
3794 } else {
3795 /* Remove global NAKs */
3796 __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
3797 }
3798}
3799
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003800/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003801 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003802 * @ep: The USB endpint to configure
3803 * @desc: The USB endpoint descriptor to configure with.
3804 *
3805 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003806 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003807static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003808 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003809{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003810 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003811 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003812 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003813 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003814 u32 epctrl_reg;
3815 u32 epctrl;
3816 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003817 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003818 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003819 unsigned int dir_in;
3820 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003821 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003822
3823 dev_dbg(hsotg->dev,
3824 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3825 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3826 desc->wMaxPacketSize, desc->bInterval);
3827
3828 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003829 if (index == 0) {
3830 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3831 return -EINVAL;
3832 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003833
3834 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3835 if (dir_in != hs_ep->dir_in) {
3836 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3837 return -EINVAL;
3838 }
3839
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003840 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003841 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003842
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003843 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003844
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003845 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003846 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003847
3848 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3849 __func__, epctrl, epctrl_reg);
3850
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003851 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003852 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3853 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003854 MAX_DMA_DESC_NUM_GENERIC *
3855 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003856 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003857 if (!hs_ep->desc_list) {
3858 ret = -ENOMEM;
3859 goto error2;
3860 }
3861 }
3862
Lukasz Majewski22258f42012-06-14 10:02:24 +02003863 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003864
Dinh Nguyen47a16852014-04-14 14:13:34 -07003865 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3866 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003867
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003868 /*
3869 * mark the endpoint as active, otherwise the core may ignore
3870 * transactions entirely for this endpoint
3871 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003872 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003873
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003874 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003875 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003876
3877 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003878 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003879 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003880 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003881 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003882
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003883 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3884 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003885 epctrl |= DXEPCTL_EPTYPE_ISO;
3886 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003887 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003888 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003889 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003890 hs_ep->isoc_chain_num = 0;
3891 hs_ep->next_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003892 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003893 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003894 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3895 mask |= DIEPMSK_NAKMSK;
3896 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3897 } else {
3898 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3899 mask |= DOEPMSK_OUTTKNEPDISMSK;
3900 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3901 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003902 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003903
3904 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003905 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003906 break;
3907
3908 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003909 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003910 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003911
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003912 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3913 hs_ep->interval = 1 << (desc->bInterval - 1);
3914
Dinh Nguyen47a16852014-04-14 14:13:34 -07003915 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003916 break;
3917
3918 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003919 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003920 break;
3921 }
3922
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003923 /*
3924 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003925 * a unique tx-fifo even if it is non-periodic.
3926 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003927 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003928 u32 fifo_index = 0;
3929 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08003930
3931 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003932 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08003933 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02003934 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003935 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08003936 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003937 if (val < size)
3938 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003939 /* Search for smallest acceptable fifo */
3940 if (val < fifo_size) {
3941 fifo_size = val;
3942 fifo_index = i;
3943 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003944 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003945 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003946 dev_err(hsotg->dev,
3947 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303948 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003949 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303950 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003951 hsotg->fifo_map |= 1 << fifo_index;
3952 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3953 hs_ep->fifo_index = fifo_index;
3954 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003955 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003956
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003957 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003958 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003959 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003960
3961 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3962 __func__, epctrl);
3963
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003964 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003965 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003966 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003967
3968 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003969 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003970
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003971error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003972 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003973
3974error2:
3975 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003976 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003977 sizeof(struct dwc2_dma_desc),
3978 hs_ep->desc_list, hs_ep->desc_list_dma);
3979 hs_ep->desc_list = NULL;
3980 }
3981
Julia Lawall19c190f2010-03-29 17:36:44 +02003982 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003983}
3984
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003985/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003986 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003987 * @ep: The endpoint to disable.
3988 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003989static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003990{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003991 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003992 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003993 int dir_in = hs_ep->dir_in;
3994 int index = hs_ep->index;
3995 unsigned long flags;
3996 u32 epctrl_reg;
3997 u32 ctrl;
3998
Marek Szyprowski1e011292014-09-09 10:44:54 +02003999 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004000
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004001 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004002 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4003 return -EINVAL;
4004 }
4005
John Stultz9b4810922017-10-23 14:32:49 -07004006 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4007 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4008 return -EINVAL;
4009 }
4010
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02004011 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004012
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004013 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004014
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004015 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08004016
4017 if (ctrl & DXEPCTL_EPENA)
4018 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4019
Dinh Nguyen47a16852014-04-14 14:13:34 -07004020 ctrl &= ~DXEPCTL_EPENA;
4021 ctrl &= ~DXEPCTL_USBACTEP;
4022 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004023
4024 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004025 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004026
4027 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004028 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004029
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01004030 /* terminate all requests with shutdown */
4031 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4032
Robert Baldyga1c07b202016-08-29 13:39:00 -07004033 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4034 hs_ep->fifo_index = 0;
4035 hs_ep->fifo_size = 0;
4036
Lukasz Majewski22258f42012-06-14 10:02:24 +02004037 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004038 return 0;
4039}
4040
4041/**
4042 * on_list - check request is on the given endpoint
4043 * @ep: The endpoint to check.
4044 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004045 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004046static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004047{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004048 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004049
4050 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4051 if (req == test)
4052 return true;
4053 }
4054
4055 return false;
4056}
4057
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004058/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004059 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004060 * @ep: The endpoint to dequeue.
4061 * @req: The request to be removed from a queue.
4062 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004063static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004064{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004065 struct dwc2_hsotg_req *hs_req = our_req(req);
4066 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004067 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004068 unsigned long flags;
4069
Marek Szyprowski1e011292014-09-09 10:44:54 +02004070 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004071
Lukasz Majewski22258f42012-06-14 10:02:24 +02004072 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004073
4074 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004075 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004076 return -EINVAL;
4077 }
4078
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004079 /* Dequeue already started request */
4080 if (req == &hs_ep->req->req)
4081 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4082
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004083 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004084 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004085
4086 return 0;
4087}
4088
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004089/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004090 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004091 * @ep: The endpoint to set halt.
4092 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004093 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4094 * the endpoint is busy processing requests.
4095 *
4096 * We need to stall the endpoint immediately if request comes from set_feature
4097 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004098 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004099static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004100{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004101 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004102 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004103 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004104 u32 epreg;
4105 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004106 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004107
4108 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4109
Robert Baldygac9f721b2014-01-14 08:36:00 +01004110 if (index == 0) {
4111 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004112 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004113 else
4114 dev_warn(hs->dev,
4115 "%s: can't clear halt on ep0\n", __func__);
4116 return 0;
4117 }
4118
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004119 if (hs_ep->isochronous) {
4120 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4121 return -EINVAL;
4122 }
4123
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004124 if (!now && value && !list_empty(&hs_ep->queue)) {
4125 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4126 ep->name);
4127 return -EAGAIN;
4128 }
4129
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004130 if (hs_ep->dir_in) {
4131 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004132 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004133
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004134 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004135 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004136 if (epctl & DXEPCTL_EPENA)
4137 epctl |= DXEPCTL_EPDIS;
4138 } else {
4139 epctl &= ~DXEPCTL_STALL;
4140 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4141 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004142 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004143 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004144 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004145 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004146 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004147 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004148 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004149
John Youn34c08872017-01-17 20:31:43 -08004150 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004151 epctl |= DXEPCTL_STALL;
John Youn34c08872017-01-17 20:31:43 -08004152 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004153 epctl &= ~DXEPCTL_STALL;
4154 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4155 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004156 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004157 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004158 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004159 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004160 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004161
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004162 hs_ep->halted = value;
4163
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004164 return 0;
4165}
4166
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004167/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004168 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004169 * @ep: The endpoint to set halt.
4170 * @value: Set or unset the halt.
4171 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004172static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004173{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004174 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004175 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004176 unsigned long flags = 0;
4177 int ret = 0;
4178
4179 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004180 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004181 spin_unlock_irqrestore(&hs->lock, flags);
4182
4183 return ret;
4184}
4185
Bhumika Goyalebce5612017-08-12 17:34:55 +05304186static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004187 .enable = dwc2_hsotg_ep_enable,
4188 .disable = dwc2_hsotg_ep_disable,
4189 .alloc_request = dwc2_hsotg_ep_alloc_request,
4190 .free_request = dwc2_hsotg_ep_free_request,
4191 .queue = dwc2_hsotg_ep_queue_lock,
4192 .dequeue = dwc2_hsotg_ep_dequeue,
4193 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004194 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004195};
4196
4197/**
John Youn9da51972017-01-17 20:30:27 -08004198 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004199 * @hsotg: The driver state
4200 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004201static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004202{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004203 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004204 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004205 /* unmask subset of endpoint interrupts */
4206
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004207 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4208 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4209 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004210
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004211 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4212 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4213 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004214
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004215 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004216
4217 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07004218 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004219
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004220 /* setup fifos */
4221
4222 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004223 dwc2_readl(hsotg->regs + GRXFSIZ),
4224 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004225
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004226 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004227
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004228 /* keep other bits untouched (so e.g. forced modes are not lost) */
4229 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4230 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01004231 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004232
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004233 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004234 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004235 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4236 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4237 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004238
Gregory Herrerof5090042015-01-09 13:38:47 +01004239 if (using_dma(hsotg))
4240 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004241}
4242
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004243/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004244 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004245 * @gadget: The usb gadget state
4246 * @driver: The usb gadget driver
4247 *
4248 * Perform initialization to prepare udc device and driver
4249 * to work.
4250 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004251static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004252 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004253{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004254 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004255 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004256 int ret;
4257
4258 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004259 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004260 return -ENODEV;
4261 }
4262
4263 if (!driver) {
4264 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4265 return -EINVAL;
4266 }
4267
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004268 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004269 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004270
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004271 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004272 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4273 return -EINVAL;
4274 }
4275
4276 WARN_ON(hsotg->driver);
4277
4278 driver->driver.bus = NULL;
4279 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004280 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004281 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4282
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004283 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4284 ret = dwc2_lowlevel_hw_enable(hsotg);
4285 if (ret)
4286 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004287 }
4288
Gregory Herrerof6c01592015-01-09 13:38:41 +01004289 if (!IS_ERR_OR_NULL(hsotg->uphy))
4290 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004291
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004292 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004293 if (dwc2_hw_is_device(hsotg)) {
4294 dwc2_hsotg_init(hsotg);
4295 dwc2_hsotg_core_init_disconnected(hsotg, false);
4296 }
4297
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004298 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004299 spin_unlock_irqrestore(&hsotg->lock, flags);
4300
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004301 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004302
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004303 return 0;
4304
4305err:
4306 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004307 return ret;
4308}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004309
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004310/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004311 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004312 * @gadget: The usb gadget state
4313 * @driver: The usb gadget driver
4314 *
4315 * Stop udc hw block and stay tunned for future transmissions
4316 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004317static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004318{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004319 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004320 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004321 int ep;
4322
4323 if (!hsotg)
4324 return -ENODEV;
4325
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004326 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004327 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4328 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004329 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004330 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004331 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004332 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004333
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004334 spin_lock_irqsave(&hsotg->lock, flags);
4335
Marek Szyprowski32805c32014-10-20 12:45:33 +02004336 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004337 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004338 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004339
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004340 spin_unlock_irqrestore(&hsotg->lock, flags);
4341
Gregory Herrerof6c01592015-01-09 13:38:41 +01004342 if (!IS_ERR_OR_NULL(hsotg->uphy))
4343 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004344
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004345 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4346 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004347
4348 return 0;
4349}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004350
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004351/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004352 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004353 * @gadget: The usb gadget state
4354 *
4355 * Read the {micro} frame number
4356 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004357static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004358{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004359 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004360}
4361
Lukasz Majewskia188b682012-06-22 09:29:56 +02004362/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004363 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004364 * @gadget: The usb gadget state
4365 * @is_on: Current state of the USB PHY
4366 *
4367 * Connect/Disconnect the USB PHY pullup
4368 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004369static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004370{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004371 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004372 unsigned long flags = 0;
4373
Gregory Herrero77ba9112015-09-29 12:08:19 +02004374 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004375 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004376
4377 /* Don't modify pullup state while in host mode */
4378 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4379 hsotg->enabled = is_on;
4380 return 0;
4381 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004382
4383 spin_lock_irqsave(&hsotg->lock, flags);
4384 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004385 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004386 dwc2_hsotg_core_init_disconnected(hsotg, false);
4387 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004388 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004389 dwc2_hsotg_core_disconnect(hsotg);
4390 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004391 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004392 }
4393
4394 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4395 spin_unlock_irqrestore(&hsotg->lock, flags);
4396
4397 return 0;
4398}
4399
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004400static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004401{
4402 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4403 unsigned long flags;
4404
4405 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4406 spin_lock_irqsave(&hsotg->lock, flags);
4407
Gregory Herrero61f72232015-09-29 12:08:28 +02004408 /*
4409 * If controller is hibernated, it must exit from hibernation
4410 * before being initialized / de-initialized
4411 */
4412 if (hsotg->lx_state == DWC2_L2)
4413 dwc2_exit_hibernation(hsotg, false);
4414
Gregory Herrero83d98222015-01-09 13:39:02 +01004415 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004416 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004417
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004418 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01004419 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004420 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004421 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004422 dwc2_hsotg_core_disconnect(hsotg);
4423 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004424 }
4425
4426 spin_unlock_irqrestore(&hsotg->lock, flags);
4427 return 0;
4428}
4429
Gregory Herrero596d6962015-01-09 13:39:08 +01004430/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004431 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004432 * @gadget: The usb gadget state
4433 * @mA: Amount of current
4434 *
4435 * Report how much power the device may consume to the phy.
4436 */
John Youn9da51972017-01-17 20:30:27 -08004437static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004438{
4439 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4440
4441 if (IS_ERR_OR_NULL(hsotg->uphy))
4442 return -ENOTSUPP;
4443 return usb_phy_set_power(hsotg->uphy, mA);
4444}
4445
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004446static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4447 .get_frame = dwc2_hsotg_gadget_getframe,
4448 .udc_start = dwc2_hsotg_udc_start,
4449 .udc_stop = dwc2_hsotg_udc_stop,
4450 .pullup = dwc2_hsotg_pullup,
4451 .vbus_session = dwc2_hsotg_vbus_session,
4452 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004453};
4454
4455/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004456 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004457 * @hsotg: The device state.
4458 * @hs_ep: The endpoint to be initialised.
4459 * @epnum: The endpoint number
4460 *
4461 * Initialise the given endpoint (as part of the probe and device state
4462 * creation) to give to the gadget driver. Setup the endpoint name, any
4463 * direction information and other state that may be required.
4464 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004465static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004466 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004467 int epnum,
4468 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004469{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004470 char *dir;
4471
4472 if (epnum == 0)
4473 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004474 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004475 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004476 else
4477 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004478
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004479 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004480 hs_ep->index = epnum;
4481
4482 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4483
4484 INIT_LIST_HEAD(&hs_ep->queue);
4485 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4486
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004487 /* add to the list of endpoints known by the gadget driver */
4488 if (epnum)
4489 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4490
4491 hs_ep->parent = hsotg;
4492 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004493
4494 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4495 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4496 else
4497 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4498 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004499 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004500
Robert Baldyga29545222015-07-31 16:00:18 +02004501 if (epnum == 0) {
4502 hs_ep->ep.caps.type_control = true;
4503 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004504 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4505 hs_ep->ep.caps.type_iso = true;
4506 hs_ep->ep.caps.type_bulk = true;
4507 }
Robert Baldyga29545222015-07-31 16:00:18 +02004508 hs_ep->ep.caps.type_int = true;
4509 }
4510
4511 if (dir_in)
4512 hs_ep->ep.caps.dir_in = true;
4513 else
4514 hs_ep->ep.caps.dir_out = true;
4515
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004516 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004517 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004518 * to be something valid.
4519 */
4520
4521 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004522 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004523
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004524 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004525 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004526 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004527 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004528 }
4529}
4530
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004531/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004532 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004533 * @param: The device state
4534 *
4535 * Read the USB core HW configuration registers
4536 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004537static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004538{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004539 u32 cfg;
4540 u32 ep_type;
4541 u32 i;
4542
Ben Dooks10aebc72010-07-19 09:40:44 +01004543 /* check hardware configuration */
4544
John Youn43e90342015-12-17 11:17:45 -08004545 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4546
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004547 /* Add ep0 */
4548 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004549
John Younb98866c2017-01-17 20:31:58 -08004550 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4551 sizeof(struct dwc2_hsotg_ep),
4552 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004553 if (!hsotg->eps_in[0])
4554 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004555 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004556 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004557
John Youn43e90342015-12-17 11:17:45 -08004558 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004559 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004560 ep_type = cfg & 3;
4561 /* Direction in or both */
4562 if (!(ep_type & 2)) {
4563 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004564 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004565 if (!hsotg->eps_in[i])
4566 return -ENOMEM;
4567 }
4568 /* Direction out or both */
4569 if (!(ep_type & 1)) {
4570 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004571 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004572 if (!hsotg->eps_out[i])
4573 return -ENOMEM;
4574 }
4575 }
4576
John Youn43e90342015-12-17 11:17:45 -08004577 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4578 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004579
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004580 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4581 hsotg->num_of_eps,
4582 hsotg->dedicated_fifos ? "dedicated" : "shared",
4583 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004584 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004585}
4586
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004587/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004588 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004589 * @param: The device state
4590 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004591static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004592{
Mark Brown83a01802011-06-01 17:16:15 +01004593#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004594 struct device *dev = hsotg->dev;
4595 void __iomem *regs = hsotg->regs;
4596 u32 val;
4597 int idx;
4598
4599 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004600 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4601 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004602
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004603 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004604 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004605
4606 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004607 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004608
4609 /* show periodic fifo settings */
4610
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004611 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004612 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004613 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004614 val >> FIFOSIZE_DEPTH_SHIFT,
4615 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004616 }
4617
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004618 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004619 dev_info(dev,
4620 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004621 dwc2_readl(regs + DIEPCTL(idx)),
4622 dwc2_readl(regs + DIEPTSIZ(idx)),
4623 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004624
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004625 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004626 dev_info(dev,
4627 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004628 idx, dwc2_readl(regs + DOEPCTL(idx)),
4629 dwc2_readl(regs + DOEPTSIZ(idx)),
4630 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004631 }
4632
4633 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004634 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004635#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004636}
4637
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004638/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004639 * dwc2_gadget_init - init function for gadget
4640 * @dwc2: The data structure for the DWC2 driver.
4641 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004642 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06004643int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004644{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004645 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004646 int epnum;
4647 int ret;
John Youn43e90342015-12-17 11:17:45 -08004648
Gregory Herrero0a176272015-01-09 13:38:52 +01004649 /* Dump fifo information */
4650 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004651 hsotg->params.g_np_tx_fifo_size);
4652 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004653
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004654 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004655 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004656 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02004657 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4658 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004659 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4660 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004661
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004662 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004663 if (ret) {
4664 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004665 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004666 }
4667
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004668 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4669 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004670 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004671 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004672
4673 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4674 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004675 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004676 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004677
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004678 if (using_desc_dma(hsotg)) {
4679 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4680 if (ret < 0)
4681 return ret;
4682 }
4683
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004684 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
John Youn9da51972017-01-17 20:30:27 -08004685 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004686 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004687 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004688 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004689 }
4690
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004691 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4692
4693 if (hsotg->num_of_eps == 0) {
4694 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004695 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004696 }
4697
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004698 /* setup endpoint information */
4699
4700 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004701 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004702
4703 /* allocate EP0 request */
4704
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004705 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004706 GFP_KERNEL);
4707 if (!hsotg->ctrl_req) {
4708 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004709 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004710 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004711
4712 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004713 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4714 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004715 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004716 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004717 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004718 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004719 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004720 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004721
Dinh Nguyen117777b2014-11-11 11:13:34 -06004722 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004723 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004724 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004725
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004726 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004727
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004728 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004729}
4730
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004731/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004732 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004733 * @pdev: The platform information for the driver
4734 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004735int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004736{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004737 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004738
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004739 return 0;
4740}
4741
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004742int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004743{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004744 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004745
Gregory Herrero9e779772015-04-29 22:09:07 +02004746 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004747 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004748
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004749 if (hsotg->driver) {
4750 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004751
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004752 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4753 hsotg->driver->driver.name);
4754
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004755 spin_lock_irqsave(&hsotg->lock, flags);
4756 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004757 dwc2_hsotg_core_disconnect(hsotg);
4758 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004759 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4760 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004761
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004762 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4763 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004764 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004765 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004766 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004767 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004768 }
4769
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004770 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004771}
4772
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004773int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004774{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004775 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004776
Gregory Herrero9e779772015-04-29 22:09:07 +02004777 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004778 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004779
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004780 if (hsotg->driver) {
4781 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4782 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004783
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004784 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004785 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004786 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004787 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004788 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004789 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004790
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004791 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004792}
John Youn58e52ff6a2016-02-23 19:54:57 -08004793
4794/**
4795 * dwc2_backup_device_registers() - Backup controller device registers.
4796 * When suspending usb bus, registers needs to be backuped
4797 * if controller power is disabled once suspended.
4798 *
4799 * @hsotg: Programming view of the DWC_otg controller
4800 */
4801int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4802{
4803 struct dwc2_dregs_backup *dr;
4804 int i;
4805
4806 dev_dbg(hsotg->dev, "%s\n", __func__);
4807
4808 /* Backup dev regs */
4809 dr = &hsotg->dr_backup;
4810
4811 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4812 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4813 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4814 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4815 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4816
4817 for (i = 0; i < hsotg->num_of_eps; i++) {
4818 /* Backup IN EPs */
4819 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4820
4821 /* Ensure DATA PID is correctly configured */
4822 if (dr->diepctl[i] & DXEPCTL_DPID)
4823 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4824 else
4825 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4826
4827 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4828 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4829
4830 /* Backup OUT EPs */
4831 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4832
4833 /* Ensure DATA PID is correctly configured */
4834 if (dr->doepctl[i] & DXEPCTL_DPID)
4835 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4836 else
4837 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4838
4839 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4840 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4841 }
4842 dr->valid = true;
4843 return 0;
4844}
4845
4846/**
4847 * dwc2_restore_device_registers() - Restore controller device registers.
4848 * When resuming usb bus, device registers needs to be restored
4849 * if controller power were disabled.
4850 *
4851 * @hsotg: Programming view of the DWC_otg controller
4852 */
4853int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4854{
4855 struct dwc2_dregs_backup *dr;
4856 u32 dctl;
4857 int i;
4858
4859 dev_dbg(hsotg->dev, "%s\n", __func__);
4860
4861 /* Restore dev regs */
4862 dr = &hsotg->dr_backup;
4863 if (!dr->valid) {
4864 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4865 __func__);
4866 return -EINVAL;
4867 }
4868 dr->valid = false;
4869
4870 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4871 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4872 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4873 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4874 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4875
4876 for (i = 0; i < hsotg->num_of_eps; i++) {
4877 /* Restore IN EPs */
4878 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4879 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4880 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4881
4882 /* Restore OUT EPs */
4883 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4884 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4885 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4886 }
4887
4888 /* Set the Power-On Programming done bit */
4889 dctl = dwc2_readl(hsotg->regs + DCTL);
4890 dctl |= DCTL_PWRONPRGDONE;
4891 dwc2_writel(dctl, hsotg->regs + DCTL);
4892
4893 return 0;
4894}