blob: 9de16453a890081cb16a3f1e210d7484cca71ba9 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002/*
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
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040050static inline void dwc2_set_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010051{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040052 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) | val, offset);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010053}
54
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040055static inline void dwc2_clear_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010056{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040057 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) & ~val, offset);
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
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700110 *
111 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
112 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
113 */
114static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
115{
116 hs_ep->target_frame += hs_ep->interval;
117 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600118 hs_ep->frame_overrun = true;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700119 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
120 } else {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600121 hs_ep->frame_overrun = false;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700122 }
123}
124
125/**
Grigor Tovmasyan9d630b92018-08-29 21:00:03 +0400126 * dwc2_gadget_dec_frame_num_by_one - Decrements the targeted frame number
127 * by one.
128 * @hs_ep: The endpoint.
129 *
130 * This function used in service interval based scheduling flow to calculate
131 * descriptor frame number filed value. For service interval mode frame
132 * number in descriptor should point to last (u)frame in the interval.
133 *
134 */
135static inline void dwc2_gadget_dec_frame_num_by_one(struct dwc2_hsotg_ep *hs_ep)
136{
137 if (hs_ep->target_frame)
138 hs_ep->target_frame -= 1;
139 else
140 hs_ep->target_frame = DSTS_SOFFN_LIMIT;
141}
142
143/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500144 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100145 * @hsotg: The device state
146 * @ints: A bitmask of the interrupts to enable
147 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500148static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100149{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400150 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100151 u32 new_gsintmsk;
152
153 new_gsintmsk = gsintmsk | ints;
154
155 if (new_gsintmsk != gsintmsk) {
156 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400157 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100158 }
159}
160
161/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500162 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100163 * @hsotg: The device state
164 * @ints: A bitmask of the interrupts to enable
165 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500166static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100167{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400168 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100169 u32 new_gsintmsk;
170
171 new_gsintmsk = gsintmsk & ~ints;
172
173 if (new_gsintmsk != gsintmsk)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400174 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100175}
176
177/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500178 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100179 * @hsotg: The device state
180 * @ep: The endpoint index
181 * @dir_in: True if direction is in.
182 * @en: The enable value, true to enable
183 *
184 * Set or clear the mask for an individual endpoint's interrupt
185 * request.
186 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500187static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800188 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100189 unsigned int en)
190{
191 unsigned long flags;
192 u32 bit = 1 << ep;
193 u32 daint;
194
195 if (!dir_in)
196 bit <<= 16;
197
198 local_irq_save(flags);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400199 daint = dwc2_readl(hsotg, DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100200 if (en)
201 daint |= bit;
202 else
203 daint &= ~bit;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400204 dwc2_writel(hsotg, daint, DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100205 local_irq_restore(flags);
206}
207
208/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800209 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400210 *
211 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800212 */
213int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
214{
215 if (hsotg->hw_params.en_multiple_tx_fifo)
216 /* In dedicated FIFO mode we need count of IN EPs */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400217 return hsotg->hw_params.num_dev_in_eps;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800218 else
219 /* In shared FIFO mode we need count of Periodic IN EPs */
220 return hsotg->hw_params.num_dev_perio_in_ep;
221}
222
223/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800224 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
225 * device mode TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400226 *
227 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800228 */
229int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
230{
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800231 int addr;
232 int tx_addr_max;
233 u32 np_tx_fifo_size;
234
235 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
236 hsotg->params.g_np_tx_fifo_size);
237
238 /* Get Endpoint Info Control block size in DWORDs. */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400239 tx_addr_max = hsotg->hw_params.total_fifo_size;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800240
241 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
242 if (tx_addr_max <= addr)
243 return 0;
244
245 return tx_addr_max - addr;
246}
247
248/**
249 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
250 * TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400251 *
252 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800253 */
254int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
255{
256 int tx_fifo_count;
257 int tx_fifo_depth;
258
259 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
260
261 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
262
263 if (!tx_fifo_count)
264 return tx_fifo_depth;
265 else
266 return tx_fifo_depth / tx_fifo_count;
267}
268
269/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500270 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100271 * @hsotg: The device instance.
272 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500273static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100274{
John Youn2317eac2016-10-17 17:36:23 -0700275 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100276 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100277 int timeout;
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +0400278
Ben Dooks0f002d22010-05-25 05:36:50 +0100279 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700280 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100281
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100282 /* Reset fifo map if not correctly cleared during previous session */
283 WARN_ON(hsotg->fifo_map);
284 hsotg->fifo_map = 0;
285
Gregory Herrero0a176272015-01-09 13:38:52 +0100286 /* set RX/NPTX FIFO sizes */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400287 dwc2_writel(hsotg, hsotg->params.g_rx_fifo_size, GRXFSIZ);
288 dwc2_writel(hsotg, (hsotg->params.g_rx_fifo_size <<
289 FIFOSIZE_STARTADDR_SHIFT) |
John Youn05ee7992016-11-03 17:56:05 -0700290 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400291 GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100292
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200293 /*
294 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100295 * block have overlapping default addresses. This also ensures
296 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200297 * known values.
298 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100299
300 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700301 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100302
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200303 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100304 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200305 * them to endpoints dynamically according to maxpacket size value of
306 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200307 */
John Youn2317eac2016-10-17 17:36:23 -0700308 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700309 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700310 continue;
311 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700312 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
313 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700314 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700315 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100316
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400317 dwc2_writel(hsotg, val, DPTXFSIZN(ep));
318 val = dwc2_readl(hsotg, DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100319 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100320
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400321 dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size |
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800322 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400323 GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200324 /*
325 * according to p428 of the design guide, we need to ensure that
326 * all fifos are flushed before continuing
327 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100328
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400329 dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
330 GRSTCTL_RXFFLSH, GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100331
332 /* wait until the fifos are both flushed */
333 timeout = 100;
334 while (1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400335 val = dwc2_readl(hsotg, GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100336
Dinh Nguyen47a16852014-04-14 14:13:34 -0700337 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100338 break;
339
340 if (--timeout == 0) {
341 dev_err(hsotg->dev,
342 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
343 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100344 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100345 }
346
347 udelay(1);
348 }
349
350 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100351}
352
353/**
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400354 * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100355 * @ep: USB endpoint to allocate request for.
356 * @flags: Allocation flags
357 *
358 * Allocate a new USB request structure appropriate for the specified endpoint
359 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500360static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800361 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100362{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500363 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100364
John Younec33efe2017-01-17 20:32:41 -0800365 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100366 if (!req)
367 return NULL;
368
369 INIT_LIST_HEAD(&req->queue);
370
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100371 return &req->req;
372}
373
374/**
375 * is_ep_periodic - return true if the endpoint is in periodic mode.
376 * @hs_ep: The endpoint to query.
377 *
378 * Returns true if the endpoint is in periodic mode, meaning it is being
379 * used for an Interrupt or ISO transfer.
380 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500381static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100382{
383 return hs_ep->periodic;
384}
385
386/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500387 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100388 * @hsotg: The device state.
389 * @hs_ep: The endpoint for the request
390 * @hs_req: The request being processed.
391 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500392 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100393 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200394 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500395static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800396 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500397 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100398{
399 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800400
Jingoo Han17d966a2013-05-11 21:14:00 +0900401 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100402}
403
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800404/*
405 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
406 * for Control endpoint
407 * @hsotg: The device state.
408 *
409 * This function will allocate 4 descriptor chains for EP 0: 2 for
410 * Setup stage, per one for IN and OUT data/status transactions.
411 */
412static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
413{
414 hsotg->setup_desc[0] =
415 dmam_alloc_coherent(hsotg->dev,
416 sizeof(struct dwc2_dma_desc),
417 &hsotg->setup_desc_dma[0],
418 GFP_KERNEL);
419 if (!hsotg->setup_desc[0])
420 goto fail;
421
422 hsotg->setup_desc[1] =
423 dmam_alloc_coherent(hsotg->dev,
424 sizeof(struct dwc2_dma_desc),
425 &hsotg->setup_desc_dma[1],
426 GFP_KERNEL);
427 if (!hsotg->setup_desc[1])
428 goto fail;
429
430 hsotg->ctrl_in_desc =
431 dmam_alloc_coherent(hsotg->dev,
432 sizeof(struct dwc2_dma_desc),
433 &hsotg->ctrl_in_desc_dma,
434 GFP_KERNEL);
435 if (!hsotg->ctrl_in_desc)
436 goto fail;
437
438 hsotg->ctrl_out_desc =
439 dmam_alloc_coherent(hsotg->dev,
440 sizeof(struct dwc2_dma_desc),
441 &hsotg->ctrl_out_desc_dma,
442 GFP_KERNEL);
443 if (!hsotg->ctrl_out_desc)
444 goto fail;
445
446 return 0;
447
448fail:
449 return -ENOMEM;
450}
451
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100452/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500453 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100454 * @hsotg: The controller state.
455 * @hs_ep: The endpoint we're going to write for.
456 * @hs_req: The request to write data for.
457 *
458 * This is called when the TxFIFO has some space in it to hold a new
459 * transmission and we have something to give it. The actual setup of
460 * the data size is done elsewhere, so all we have to do is to actually
461 * write the data.
462 *
463 * The return value is zero if there is more space (or nothing was done)
464 * otherwise -ENOSPC is returned if the FIFO space was used up.
465 *
466 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200467 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500468static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800469 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500470 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100471{
472 bool periodic = is_ep_periodic(hs_ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400473 u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100474 int buf_pos = hs_req->req.actual;
475 int to_write = hs_ep->size_loaded;
476 void *data;
477 int can_write;
478 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200479 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100480
481 to_write -= (buf_pos - hs_ep->last_load);
482
483 /* if there's nothing to write, get out early */
484 if (to_write == 0)
485 return 0;
486
Ben Dooks10aebc72010-07-19 09:40:44 +0100487 if (periodic && !hsotg->dedicated_fifos) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400488 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100489 int size_left;
490 int size_done;
491
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200492 /*
493 * work out how much data was loaded so we can calculate
494 * how much data is left in the fifo.
495 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100496
Dinh Nguyen47a16852014-04-14 14:13:34 -0700497 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100498
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200499 /*
500 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100501 * previous data has been completely sent.
502 */
503 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500504 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100505 return -ENOSPC;
506 }
507
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100508 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
509 __func__, size_left,
510 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
511
512 /* how much of the data has moved */
513 size_done = hs_ep->size_loaded - size_left;
514
515 /* how much data is left in the fifo */
516 can_write = hs_ep->fifo_load - size_done;
517 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
518 __func__, can_write);
519
520 can_write = hs_ep->fifo_size - can_write;
521 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
522 __func__, can_write);
523
524 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500525 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100526 return -ENOSPC;
527 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100528 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400529 can_write = dwc2_readl(hsotg,
530 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100531
532 can_write &= 0xffff;
533 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100534 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700535 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100536 dev_dbg(hsotg->dev,
537 "%s: no queue slots available (0x%08x)\n",
538 __func__, gnptxsts);
539
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500540 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100541 return -ENOSPC;
542 }
543
Dinh Nguyen47a16852014-04-14 14:13:34 -0700544 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100545 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100546 }
547
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200548 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
549
550 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800551 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100552
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200553 /*
554 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100555 * FIFO, requests of >512 cause the endpoint to get stuck with a
556 * fragment of the end of the transfer in it.
557 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200558 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100559 can_write = 512;
560
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200561 /*
562 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100563 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200564 * doing it.
565 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200566 if (to_write > max_transfer) {
567 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100568
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200569 /* it's needed only when we do not use dedicated fifos */
570 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500571 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800572 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700573 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100574 }
575
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100576 /* see if we can write data */
577
578 if (to_write > can_write) {
579 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200580 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100581
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200582 /*
583 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100584 * exact number of packets.
585 *
586 * Note, we do not currently check to see if we can ever
587 * write a full packet or not to the FIFO.
588 */
589
590 if (pkt_round)
591 to_write -= pkt_round;
592
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200593 /*
594 * enable correct FIFO interrupt to alert us when there
595 * is more room left.
596 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100597
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200598 /* it's needed only when we do not use dedicated fifos */
599 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500600 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800601 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700602 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100603 }
604
605 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800606 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100607
608 if (to_write <= 0)
609 return -ENOSPC;
610
611 hs_req->req.actual = buf_pos + to_write;
612 hs_ep->total_data += to_write;
613
614 if (periodic)
615 hs_ep->fifo_load += to_write;
616
617 to_write = DIV_ROUND_UP(to_write, 4);
618 data = hs_req->req.buf + buf_pos;
619
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +0400620 dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100621
622 return (to_write >= can_write) ? -ENOSPC : 0;
623}
624
625/**
626 * get_ep_limit - get the maximum data legnth for this endpoint
627 * @hs_ep: The endpoint
628 *
629 * Return the maximum data that can be queued in one go on a given endpoint
630 * so that transfers that are too long can be split.
631 */
John Youn9da51972017-01-17 20:30:27 -0800632static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100633{
634 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800635 unsigned int maxsize;
636 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100637
638 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700639 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
640 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100641 } else {
John Youn9da51972017-01-17 20:30:27 -0800642 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900643 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700644 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900645 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100646 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100647 }
648
649 /* we made the constant loading easier above by using +1 */
650 maxpkt--;
651 maxsize--;
652
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200653 /*
654 * constrain by packet count if maxpkts*pktsize is greater
655 * than the length register size.
656 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100657
658 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
659 maxsize = maxpkt * hs_ep->ep.maxpacket;
660
661 return maxsize;
662}
663
664/**
John Youn38beaec2017-01-17 20:31:13 -0800665 * dwc2_hsotg_read_frameno - read current frame number
666 * @hsotg: The device instance
667 *
668 * Return the current frame number
669 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700670static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
671{
672 u32 dsts;
673
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400674 dsts = dwc2_readl(hsotg, DSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700675 dsts &= DSTS_SOFFN_MASK;
676 dsts >>= DSTS_SOFFN_SHIFT;
677
678 return dsts;
679}
680
681/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800682 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
683 * DMA descriptor chain prepared for specific endpoint
684 * @hs_ep: The endpoint
685 *
686 * Return the maximum data that can be queued in one go on a given endpoint
687 * depending on its descriptor chain capacity so that transfers that
688 * are too long can be split.
689 */
690static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
691{
692 int is_isoc = hs_ep->isochronous;
693 unsigned int maxsize;
694
695 if (is_isoc)
696 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
697 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
698 else
699 maxsize = DEV_DMA_NBYTES_LIMIT;
700
701 /* Above size of one descriptor was chosen, multiple it */
702 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
703
704 return maxsize;
705}
706
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800707/*
708 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
709 * @hs_ep: The endpoint
710 * @mask: RX/TX bytes mask to be defined
711 *
712 * Returns maximum data payload for one descriptor after analyzing endpoint
713 * characteristics.
714 * DMA descriptor transfer bytes limit depends on EP type:
715 * Control out - MPS,
716 * Isochronous - descriptor rx/tx bytes bitfield limit,
717 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
718 * have concatenations from various descriptors within one packet.
719 *
720 * Selects corresponding mask for RX/TX bytes as well.
721 */
722static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
723{
724 u32 mps = hs_ep->ep.maxpacket;
725 int dir_in = hs_ep->dir_in;
726 u32 desc_size = 0;
727
728 if (!hs_ep->index && !dir_in) {
729 desc_size = mps;
730 *mask = DEV_DMA_NBYTES_MASK;
731 } else if (hs_ep->isochronous) {
732 if (dir_in) {
733 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
734 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
735 } else {
736 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
737 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
738 }
739 } else {
740 desc_size = DEV_DMA_NBYTES_LIMIT;
741 *mask = DEV_DMA_NBYTES_MASK;
742
743 /* Round down desc_size to be mps multiple */
744 desc_size -= desc_size % mps;
745 }
746
747 return desc_size;
748}
749
750/*
751 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
752 * @hs_ep: The endpoint
753 * @dma_buff: DMA address to use
754 * @len: Length of the transfer
755 *
756 * This function will iterate over descriptor chain and fill its entries
757 * with corresponding information based on transfer data.
758 */
759static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
760 dma_addr_t dma_buff,
761 unsigned int len)
762{
763 struct dwc2_hsotg *hsotg = hs_ep->parent;
764 int dir_in = hs_ep->dir_in;
765 struct dwc2_dma_desc *desc = hs_ep->desc_list;
766 u32 mps = hs_ep->ep.maxpacket;
767 u32 maxsize = 0;
768 u32 offset = 0;
769 u32 mask = 0;
770 int i;
771
772 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
773
774 hs_ep->desc_count = (len / maxsize) +
775 ((len % maxsize) ? 1 : 0);
776 if (len == 0)
777 hs_ep->desc_count = 1;
778
779 for (i = 0; i < hs_ep->desc_count; ++i) {
780 desc->status = 0;
781 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
782 << DEV_DMA_BUFF_STS_SHIFT);
783
784 if (len > maxsize) {
785 if (!hs_ep->index && !dir_in)
786 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
787
788 desc->status |= (maxsize <<
789 DEV_DMA_NBYTES_SHIFT & mask);
790 desc->buf = dma_buff + offset;
791
792 len -= maxsize;
793 offset += maxsize;
794 } else {
795 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
796
797 if (dir_in)
798 desc->status |= (len % mps) ? DEV_DMA_SHORT :
799 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
800 if (len > maxsize)
801 dev_err(hsotg->dev, "wrong len %d\n", len);
802
803 desc->status |=
804 len << DEV_DMA_NBYTES_SHIFT & mask;
805 desc->buf = dma_buff + offset;
806 }
807
808 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
809 desc->status |= (DEV_DMA_BUFF_STS_HREADY
810 << DEV_DMA_BUFF_STS_SHIFT);
811 desc++;
812 }
813}
814
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800815/*
816 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
817 * @hs_ep: The isochronous endpoint.
818 * @dma_buff: usb requests dma buffer.
819 * @len: usb request transfer length.
820 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400821 * Fills next free descriptor with the data of the arrived usb request,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800822 * frame info, sets Last and IOC bits increments next_desc. If filled
823 * descriptor is not the first one, removes L bit from the previous descriptor
824 * status.
825 */
826static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
827 dma_addr_t dma_buff, unsigned int len)
828{
829 struct dwc2_dma_desc *desc;
830 struct dwc2_hsotg *hsotg = hs_ep->parent;
831 u32 index;
832 u32 maxsize = 0;
833 u32 mask = 0;
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400834 u8 pid = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800835
836 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800837
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400838 index = hs_ep->next_desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800839 desc = &hs_ep->desc_list[index];
840
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400841 /* Check if descriptor chain full */
842 if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
843 DEV_DMA_BUFF_STS_HREADY) {
844 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
845 return 1;
846 }
847
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800848 /* Clear L bit of previous desc if more than one entries in the chain */
849 if (hs_ep->next_desc)
850 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
851
852 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
853 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
854
855 desc->status = 0;
856 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
857
858 desc->buf = dma_buff;
859 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
860 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
861
862 if (hs_ep->dir_in) {
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400863 if (len)
864 pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
865 else
866 pid = 1;
867 desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800868 DEV_DMA_ISOC_PID_MASK) |
869 ((len % hs_ep->ep.maxpacket) ?
870 DEV_DMA_SHORT : 0) |
871 ((hs_ep->target_frame <<
872 DEV_DMA_ISOC_FRNUM_SHIFT) &
873 DEV_DMA_ISOC_FRNUM_MASK);
874 }
875
876 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
877 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
878
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400879 /* Increment frame number by interval for IN */
880 if (hs_ep->dir_in)
881 dwc2_gadget_incr_frame_num(hs_ep);
882
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800883 /* Update index of last configured entry in the chain */
884 hs_ep->next_desc++;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400885 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_GENERIC)
886 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800887
888 return 0;
889}
890
891/*
892 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
893 * @hs_ep: The isochronous endpoint.
894 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400895 * Prepare descriptor chain for isochronous endpoints. Afterwards
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800896 * write DMA address to HW and enable the endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800897 */
898static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
899{
900 struct dwc2_hsotg *hsotg = hs_ep->parent;
901 struct dwc2_hsotg_req *hs_req, *treq;
902 int index = hs_ep->index;
903 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400904 int i;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800905 u32 dma_reg;
906 u32 depctl;
907 u32 ctrl;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400908 struct dwc2_dma_desc *desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800909
910 if (list_empty(&hs_ep->queue)) {
Minas Harutyunyan1ffba902018-06-12 12:37:29 +0400911 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800912 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
913 return;
914 }
915
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400916 /* Initialize descriptor chain by Host Busy status */
917 for (i = 0; i < MAX_DMA_DESC_NUM_GENERIC; i++) {
918 desc = &hs_ep->desc_list[i];
919 desc->status = 0;
920 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
921 << DEV_DMA_BUFF_STS_SHIFT);
922 }
923
924 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800925 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
926 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
927 hs_req->req.length);
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400928 if (ret)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800929 break;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800930 }
931
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400932 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800933 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
934 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
935
936 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400937 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800938
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400939 ctrl = dwc2_readl(hsotg, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800940 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400941 dwc2_writel(hsotg, ctrl, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800942}
943
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800944/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500945 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100946 * @hsotg: The controller state.
947 * @hs_ep: The endpoint to process a request for
948 * @hs_req: The request to start.
949 * @continuing: True if we are doing more for the current request.
950 *
951 * Start the given request running by setting the endpoint registers
952 * appropriately, and writing any data to the FIFOs.
953 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500954static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800955 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500956 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100957 bool continuing)
958{
959 struct usb_request *ureq = &hs_req->req;
960 int index = hs_ep->index;
961 int dir_in = hs_ep->dir_in;
962 u32 epctrl_reg;
963 u32 epsize_reg;
964 u32 epsize;
965 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -0800966 unsigned int length;
967 unsigned int packets;
968 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800969 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100970
971 if (index != 0) {
972 if (hs_ep->req && !continuing) {
973 dev_err(hsotg->dev, "%s: active request\n", __func__);
974 WARN_ON(1);
975 return;
976 } else if (hs_ep->req != hs_req && continuing) {
977 dev_err(hsotg->dev,
978 "%s: continue different req\n", __func__);
979 WARN_ON(1);
980 return;
981 }
982 }
983
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800984 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200985 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
986 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100987
988 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400989 __func__, dwc2_readl(hsotg, epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100990 hs_ep->dir_in ? "in" : "out");
991
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900992 /* If endpoint is stalled, we will restart request later */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400993 ctrl = dwc2_readl(hsotg, epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900994
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200995 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900996 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
997 return;
998 }
999
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001000 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001001 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
1002 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001003
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -08001004 if (!using_desc_dma(hsotg))
1005 maxreq = get_ep_limit(hs_ep);
1006 else
1007 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
1008
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001009 if (length > maxreq) {
1010 int round = maxreq % hs_ep->ep.maxpacket;
1011
1012 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
1013 __func__, length, maxreq, round);
1014
1015 /* round down to multiple of packets */
1016 if (round)
1017 maxreq -= round;
1018
1019 length = maxreq;
1020 }
1021
1022 if (length)
1023 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
1024 else
1025 packets = 1; /* send one packet if length is zero. */
1026
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001027 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1028 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
1029 return;
1030 }
1031
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001032 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001033 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001034 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001035 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001036 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001037 else
1038 epsize = 0;
1039
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001040 /*
1041 * zero length packet should be programmed on its own and should not
1042 * be counted in DIEPTSIZ.PktCnt with other packets.
1043 */
1044 if (dir_in && ureq->zero && !continuing) {
1045 /* Test if zlp is actually required. */
1046 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001047 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001048 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001049 }
1050
Dinh Nguyen47a16852014-04-14 14:13:34 -07001051 epsize |= DXEPTSIZ_PKTCNT(packets);
1052 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001053
1054 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1055 __func__, packets, length, ureq->length, epsize, epsize_reg);
1056
1057 /* store the request as the current one we're doing */
1058 hs_ep->req = hs_req;
1059
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001060 if (using_desc_dma(hsotg)) {
1061 u32 offset = 0;
1062 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001063
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001064 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1065 if (!dir_in) {
1066 if (!index)
1067 length = mps;
1068 else if (length % mps)
1069 length += (mps - (length % mps));
1070 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001071
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001072 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001073 * If more data to send, adjust DMA for EP0 out data stage.
1074 * ureq->dma stays unchanged, hence increment it by already
1075 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001076 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001077 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1078 continuing)
1079 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001080
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001081 /* Fill DDMA chain entries */
1082 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1083 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001084
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001085 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001086 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001087
1088 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1089 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1090 } else {
1091 /* write size / packets */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001092 dwc2_writel(hsotg, epsize, epsize_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001093
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001094 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001095 /*
1096 * write DMA address to control register, buffer
1097 * already synced by dwc2_hsotg_ep_queue().
1098 */
1099
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001100 dwc2_writel(hsotg, ureq->dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001101
1102 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1103 __func__, &ureq->dma, dma_reg);
1104 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001105 }
1106
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001107 if (hs_ep->isochronous && hs_ep->interval == 1) {
1108 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1109 dwc2_gadget_incr_frame_num(hs_ep);
1110
1111 if (hs_ep->target_frame & 0x1)
1112 ctrl |= DXEPCTL_SETODDFR;
1113 else
1114 ctrl |= DXEPCTL_SETEVENFR;
1115 }
1116
Dinh Nguyen47a16852014-04-14 14:13:34 -07001117 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001118
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001119 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001120
1121 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001122 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001123 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001124
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001125 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001126 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001127
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001128 /*
1129 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001130 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001131 * this information.
1132 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001133 hs_ep->size_loaded = length;
1134 hs_ep->last_load = ureq->actual;
1135
1136 if (dir_in && !using_dma(hsotg)) {
1137 /* set these anyway, we may need them for non-periodic in */
1138 hs_ep->fifo_load = 0;
1139
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001140 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001141 }
1142
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001143 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001144 * Note, trying to clear the NAK here causes problems with transmit
1145 * on the S3C6400 ending up with the TXFIFO becoming full.
1146 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001147
1148 /* check ep is enabled */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001149 if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001150 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001151 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001152 index, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001153
Dinh Nguyen47a16852014-04-14 14:13:34 -07001154 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001155 __func__, dwc2_readl(hsotg, epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001156
1157 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001158 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001159}
1160
1161/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001162 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001163 * @hsotg: The device state.
1164 * @hs_ep: The endpoint the request is on.
1165 * @req: The request being processed.
1166 *
1167 * We've been asked to queue a request, so ensure that the memory buffer
1168 * is correctly setup for DMA. If we've been passed an extant DMA address
1169 * then ensure the buffer has been synced to memory. If our buffer has no
1170 * DMA memory, then we map the memory and mark our request to allow us to
1171 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001172 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001173static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001174 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001175 struct usb_request *req)
1176{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001177 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001178
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001179 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1180 if (ret)
1181 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001182
1183 return 0;
1184
1185dma_error:
1186 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1187 __func__, req->buf, req->length);
1188
1189 return -EIO;
1190}
1191
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001192static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001193 struct dwc2_hsotg_ep *hs_ep,
1194 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001195{
1196 void *req_buf = hs_req->req.buf;
1197
1198 /* If dma is not being used or buffer is aligned */
1199 if (!using_dma(hsotg) || !((long)req_buf & 3))
1200 return 0;
1201
1202 WARN_ON(hs_req->saved_req_buf);
1203
1204 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001205 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001206
1207 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1208 if (!hs_req->req.buf) {
1209 hs_req->req.buf = req_buf;
1210 dev_err(hsotg->dev,
1211 "%s: unable to allocate memory for bounce buffer\n",
1212 __func__);
1213 return -ENOMEM;
1214 }
1215
1216 /* Save actual buffer */
1217 hs_req->saved_req_buf = req_buf;
1218
1219 if (hs_ep->dir_in)
1220 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1221 return 0;
1222}
1223
John Younb98866c2017-01-17 20:31:58 -08001224static void
1225dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1226 struct dwc2_hsotg_ep *hs_ep,
1227 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001228{
1229 /* If dma is not being used or buffer was aligned */
1230 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1231 return;
1232
1233 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1234 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1235
1236 /* Copy data from bounce buffer on successful out transfer */
1237 if (!hs_ep->dir_in && !hs_req->req.status)
1238 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001239 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001240
1241 /* Free bounce buffer */
1242 kfree(hs_req->req.buf);
1243
1244 hs_req->req.buf = hs_req->saved_req_buf;
1245 hs_req->saved_req_buf = NULL;
1246}
1247
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001248/**
1249 * dwc2_gadget_target_frame_elapsed - Checks target frame
1250 * @hs_ep: The driver endpoint to check
1251 *
1252 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1253 * corresponding transfer.
1254 */
1255static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1256{
1257 struct dwc2_hsotg *hsotg = hs_ep->parent;
1258 u32 target_frame = hs_ep->target_frame;
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001259 u32 current_frame = hsotg->frame_number;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001260 bool frame_overrun = hs_ep->frame_overrun;
1261
1262 if (!frame_overrun && current_frame >= target_frame)
1263 return true;
1264
1265 if (frame_overrun && current_frame >= target_frame &&
1266 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1267 return true;
1268
1269 return false;
1270}
1271
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001272/*
1273 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1274 * @hsotg: The driver state
1275 * @hs_ep: the ep descriptor chain is for
1276 *
1277 * Called to update EP0 structure's pointers depend on stage of
1278 * control transfer.
1279 */
1280static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1281 struct dwc2_hsotg_ep *hs_ep)
1282{
1283 switch (hsotg->ep0_state) {
1284 case DWC2_EP0_SETUP:
1285 case DWC2_EP0_STATUS_OUT:
1286 hs_ep->desc_list = hsotg->setup_desc[0];
1287 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1288 break;
1289 case DWC2_EP0_DATA_IN:
1290 case DWC2_EP0_STATUS_IN:
1291 hs_ep->desc_list = hsotg->ctrl_in_desc;
1292 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1293 break;
1294 case DWC2_EP0_DATA_OUT:
1295 hs_ep->desc_list = hsotg->ctrl_out_desc;
1296 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1297 break;
1298 default:
1299 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1300 hsotg->ep0_state);
1301 return -EINVAL;
1302 }
1303
1304 return 0;
1305}
1306
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001307static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001308 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001309{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001310 struct dwc2_hsotg_req *hs_req = our_req(req);
1311 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001312 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001313 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001314 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001315 u32 maxsize = 0;
1316 u32 mask = 0;
1317
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001318
1319 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1320 ep->name, req, req->length, req->buf, req->no_interrupt,
1321 req->zero, req->short_not_ok);
1322
Gregory Herrero7ababa92015-04-29 22:09:08 +02001323 /* Prevent new request submission when controller is suspended */
Grigor Tovmasyan88b02f22018-01-24 17:44:25 +04001324 if (hs->lx_state != DWC2_L0) {
1325 dev_dbg(hs->dev, "%s: submit request only in active state\n",
John Youn9da51972017-01-17 20:30:27 -08001326 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001327 return -EAGAIN;
1328 }
1329
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001330 /* initialise status of the request */
1331 INIT_LIST_HEAD(&hs_req->queue);
1332 req->actual = 0;
1333 req->status = -EINPROGRESS;
1334
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001335 /* In DDMA mode for ISOC's don't queue request if length greater
1336 * than descriptor limits.
1337 */
1338 if (using_desc_dma(hs) && hs_ep->isochronous) {
1339 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1340 if (hs_ep->dir_in && req->length > maxsize) {
1341 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1342 req->length, maxsize);
1343 return -EINVAL;
1344 }
1345
1346 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1347 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1348 req->length, hs_ep->ep.maxpacket);
1349 return -EINVAL;
1350 }
1351 }
1352
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001353 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001354 if (ret)
1355 return ret;
1356
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001357 /* if we're using DMA, sync the buffers as necessary */
1358 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001359 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001360 if (ret)
1361 return ret;
1362 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001363 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1364 if (using_desc_dma(hs) && !hs_ep->index) {
1365 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1366 if (ret)
1367 return ret;
1368 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001369
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001370 first = list_empty(&hs_ep->queue);
1371 list_add_tail(&hs_req->queue, &hs_ep->queue);
1372
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001373 /*
1374 * Handle DDMA isochronous transfers separately - just add new entry
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001375 * to the descriptor chain.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001376 * Transfer will be started once SW gets either one of NAK or
1377 * OutTknEpDis interrupts.
1378 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001379 if (using_desc_dma(hs) && hs_ep->isochronous) {
1380 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1381 dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1382 hs_req->req.length);
1383 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001384 return 0;
1385 }
1386
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001387 if (first) {
1388 if (!hs_ep->isochronous) {
1389 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1390 return 0;
1391 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001392
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001393 /* Update current frame number value. */
1394 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1395 while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001396 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001397 /* Update current frame number value once more as it
1398 * changes here.
1399 */
1400 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1401 }
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001402
1403 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1404 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1405 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001406 return 0;
1407}
1408
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001409static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001410 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001411{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001412 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001413 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001414 unsigned long flags = 0;
1415 int ret = 0;
1416
1417 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001418 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001419 spin_unlock_irqrestore(&hs->lock, flags);
1420
1421 return ret;
1422}
1423
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001424static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001425 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001426{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001427 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001428
1429 kfree(hs_req);
1430}
1431
1432/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001433 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001434 * @ep: The endpoint the request was on.
1435 * @req: The request completed.
1436 *
1437 * Called on completion of any requests the driver itself
1438 * submitted that need cleaning up.
1439 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001440static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001441 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001442{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001443 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001444 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001445
1446 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1447
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001448 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001449}
1450
1451/**
1452 * ep_from_windex - convert control wIndex value to endpoint
1453 * @hsotg: The driver state.
1454 * @windex: The control request wIndex field (in host order).
1455 *
1456 * Convert the given wIndex into a pointer to an driver endpoint
1457 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001458 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001459static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001460 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001461{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001462 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001463 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1464 int idx = windex & 0x7F;
1465
1466 if (windex >= 0x100)
1467 return NULL;
1468
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001469 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001470 return NULL;
1471
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001472 ep = index_to_ep(hsotg, idx, dir);
1473
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001474 if (idx && ep->dir_in != dir)
1475 return NULL;
1476
1477 return ep;
1478}
1479
1480/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001481 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001482 * @hsotg: The driver state.
1483 * @testmode: requested usb test mode
1484 * Enable usb Test Mode requested by the Host.
1485 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001486int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001487{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001488 int dctl = dwc2_readl(hsotg, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001489
1490 dctl &= ~DCTL_TSTCTL_MASK;
1491 switch (testmode) {
1492 case TEST_J:
1493 case TEST_K:
1494 case TEST_SE0_NAK:
1495 case TEST_PACKET:
1496 case TEST_FORCE_EN:
1497 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1498 break;
1499 default:
1500 return -EINVAL;
1501 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001502 dwc2_writel(hsotg, dctl, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001503 return 0;
1504}
1505
1506/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001507 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001508 * @hsotg: The device state
1509 * @ep: Endpoint 0
1510 * @buff: Buffer for request
1511 * @length: Length of reply.
1512 *
1513 * Create a request and queue it on the given endpoint. This is useful as
1514 * an internal method of sending replies to certain control requests, etc.
1515 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001516static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001517 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001518 void *buff,
1519 int length)
1520{
1521 struct usb_request *req;
1522 int ret;
1523
1524 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1525
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001526 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001527 hsotg->ep0_reply = req;
1528 if (!req) {
1529 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1530 return -ENOMEM;
1531 }
1532
1533 req->buf = hsotg->ep0_buff;
1534 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001535 /*
1536 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1537 * STATUS stage.
1538 */
1539 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001540 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001541
1542 if (length)
1543 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001544
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001545 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001546 if (ret) {
1547 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1548 return ret;
1549 }
1550
1551 return 0;
1552}
1553
1554/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001555 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001556 * @hsotg: The device state
1557 * @ctrl: USB control request
1558 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001559static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001560 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001561{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001562 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1563 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001564 __le16 reply;
1565 int ret;
1566
1567 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1568
1569 if (!ep0->dir_in) {
1570 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1571 return -EINVAL;
1572 }
1573
1574 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1575 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001576 /*
1577 * bit 0 => self powered
1578 * bit 1 => remote wakeup
1579 */
1580 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001581 break;
1582
1583 case USB_RECIP_INTERFACE:
1584 /* currently, the data result should be zero */
1585 reply = cpu_to_le16(0);
1586 break;
1587
1588 case USB_RECIP_ENDPOINT:
1589 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1590 if (!ep)
1591 return -ENOENT;
1592
1593 reply = cpu_to_le16(ep->halted ? 1 : 0);
1594 break;
1595
1596 default:
1597 return 0;
1598 }
1599
1600 if (le16_to_cpu(ctrl->wLength) != 2)
1601 return -EINVAL;
1602
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001603 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001604 if (ret) {
1605 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1606 return ret;
1607 }
1608
1609 return 1;
1610}
1611
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001612static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001613
1614/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001615 * get_ep_head - return the first request on the endpoint
1616 * @hs_ep: The controller endpoint to get
1617 *
1618 * Get the first request on the endpoint.
1619 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001620static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001621{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001622 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1623 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001624}
1625
1626/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001627 * dwc2_gadget_start_next_request - Starts next request from ep queue
1628 * @hs_ep: Endpoint structure
1629 *
1630 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1631 * in its handler. Hence we need to unmask it here to be able to do
1632 * resynchronization.
1633 */
1634static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1635{
1636 u32 mask;
1637 struct dwc2_hsotg *hsotg = hs_ep->parent;
1638 int dir_in = hs_ep->dir_in;
1639 struct dwc2_hsotg_req *hs_req;
1640 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1641
1642 if (!list_empty(&hs_ep->queue)) {
1643 hs_req = get_ep_head(hs_ep);
1644 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1645 return;
1646 }
1647 if (!hs_ep->isochronous)
1648 return;
1649
1650 if (dir_in) {
1651 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1652 __func__);
1653 } else {
1654 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1655 __func__);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001656 mask = dwc2_readl(hsotg, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001657 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001658 dwc2_writel(hsotg, mask, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001659 }
1660}
1661
1662/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001663 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001664 * @hsotg: The device state
1665 * @ctrl: USB control request
1666 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001667static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001668 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001669{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001670 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1671 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001672 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001673 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001674 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001675 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001676 u32 recip;
1677 u32 wValue;
1678 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001679
1680 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1681 __func__, set ? "SET" : "CLEAR");
1682
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001683 wValue = le16_to_cpu(ctrl->wValue);
1684 wIndex = le16_to_cpu(ctrl->wIndex);
1685 recip = ctrl->bRequestType & USB_RECIP_MASK;
1686
1687 switch (recip) {
1688 case USB_RECIP_DEVICE:
1689 switch (wValue) {
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001690 case USB_DEVICE_REMOTE_WAKEUP:
1691 hsotg->remote_wakeup_allowed = 1;
1692 break;
1693
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001694 case USB_DEVICE_TEST_MODE:
1695 if ((wIndex & 0xff) != 0)
1696 return -EINVAL;
1697 if (!set)
1698 return -EINVAL;
1699
1700 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001701 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001702 if (ret) {
1703 dev_err(hsotg->dev,
1704 "%s: failed to send reply\n", __func__);
1705 return ret;
1706 }
1707 break;
1708 default:
1709 return -ENOENT;
1710 }
1711 break;
1712
1713 case USB_RECIP_ENDPOINT:
1714 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001715 if (!ep) {
1716 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001717 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001718 return -ENOENT;
1719 }
1720
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001721 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001722 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001723 halted = ep->halted;
1724
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001725 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001726
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001727 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001728 if (ret) {
1729 dev_err(hsotg->dev,
1730 "%s: failed to send reply\n", __func__);
1731 return ret;
1732 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001733
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001734 /*
1735 * we have to complete all requests for ep if it was
1736 * halted, and the halt was cleared by CLEAR_FEATURE
1737 */
1738
1739 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001740 /*
1741 * If we have request in progress,
1742 * then complete it
1743 */
1744 if (ep->req) {
1745 hs_req = ep->req;
1746 ep->req = NULL;
1747 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001748 if (hs_req->req.complete) {
1749 spin_unlock(&hsotg->lock);
1750 usb_gadget_giveback_request(
1751 &ep->ep, &hs_req->req);
1752 spin_lock(&hsotg->lock);
1753 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001754 }
1755
1756 /* If we have pending request, then start it */
John Youn34c0887f2017-01-17 20:31:43 -08001757 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001758 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001759 }
1760
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001761 break;
1762
1763 default:
1764 return -ENOENT;
1765 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001766 break;
1767 default:
1768 return -ENOENT;
1769 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001770 return 1;
1771}
1772
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001773static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001774
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001775/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001776 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001777 * @hsotg: The device state
1778 *
1779 * Set stall for ep0 as response for setup request.
1780 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001781static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001782{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001783 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001784 u32 reg;
1785 u32 ctrl;
1786
1787 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1788 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1789
1790 /*
1791 * DxEPCTL_Stall will be cleared by EP once it has
1792 * taken effect, so no need to clear later.
1793 */
1794
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001795 ctrl = dwc2_readl(hsotg, reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001796 ctrl |= DXEPCTL_STALL;
1797 ctrl |= DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001798 dwc2_writel(hsotg, ctrl, reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001799
1800 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001801 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001802 ctrl, reg, dwc2_readl(hsotg, reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001803
1804 /*
1805 * complete won't be called, so we enqueue
1806 * setup request here
1807 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001808 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001809}
1810
1811/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001812 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001813 * @hsotg: The device state
1814 * @ctrl: The control request received
1815 *
1816 * The controller has received the SETUP phase of a control request, and
1817 * needs to work out what to do next (and whether to pass it on to the
1818 * gadget driver).
1819 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001820static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001821 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001822{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001823 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001824 int ret = 0;
1825 u32 dcfg;
1826
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001827 dev_dbg(hsotg->dev,
1828 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1829 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1830 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001831
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001832 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001833 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001834 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1835 } else if (ctrl->bRequestType & USB_DIR_IN) {
1836 ep0->dir_in = 1;
1837 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1838 } else {
1839 ep0->dir_in = 0;
1840 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1841 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001842
1843 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1844 switch (ctrl->bRequest) {
1845 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001846 hsotg->connected = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001847 dcfg = dwc2_readl(hsotg, DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001848 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001849 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1850 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001851 dwc2_writel(hsotg, dcfg, DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001852
1853 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1854
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001855 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001856 return;
1857
1858 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001859 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001860 break;
1861
1862 case USB_REQ_CLEAR_FEATURE:
1863 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001864 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001865 break;
1866 }
1867 }
1868
1869 /* as a fallback, try delivering it to the driver to deal with */
1870
1871 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001872 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001873 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001874 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001875 if (ret < 0)
1876 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1877 }
1878
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001879 /*
1880 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001881 * so respond with a STALL for the status stage to indicate failure.
1882 */
1883
Robert Baldygac9f721b2014-01-14 08:36:00 +01001884 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001885 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001886}
1887
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001888/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001889 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001890 * @ep: The endpoint the request was on.
1891 * @req: The request completed.
1892 *
1893 * Called on completion of any requests the driver itself submitted for
1894 * EP0 setup packets
1895 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001896static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001897 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001898{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001899 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001900 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001901
1902 if (req->status < 0) {
1903 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1904 return;
1905 }
1906
Robert Baldyga93f599f2013-11-21 13:49:17 +01001907 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001908 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001909 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001910 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001911 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001912 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001913}
1914
1915/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001916 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001917 * @hsotg: The device state.
1918 *
1919 * Enqueue a request on EP0 if necessary to received any SETUP packets
1920 * received from the host.
1921 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001922static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001923{
1924 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001925 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001926 int ret;
1927
1928 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1929
1930 req->zero = 0;
1931 req->length = 8;
1932 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001933 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001934
1935 if (!list_empty(&hs_req->queue)) {
1936 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1937 return;
1938 }
1939
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001940 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001941 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001942 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001943
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001944 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001945 if (ret < 0) {
1946 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001947 /*
1948 * Don't think there's much we can do other than watch the
1949 * driver fail.
1950 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001951 }
1952}
1953
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001954static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001955 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001956{
1957 u32 ctrl;
1958 u8 index = hs_ep->index;
1959 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1960 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1961
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001962 if (hs_ep->dir_in)
1963 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001964 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001965 else
1966 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001967 index);
1968 if (using_desc_dma(hsotg)) {
1969 /* Not specific buffer needed for ep0 ZLP */
1970 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001971
Minas Harutyunyan201ec562018-01-16 16:03:32 +04001972 if (!index)
1973 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1974
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001975 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1976 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001977 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1978 DXEPTSIZ_XFERSIZE(0),
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001979 epsiz_reg);
1980 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001981
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001982 ctrl = dwc2_readl(hsotg, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001983 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1984 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1985 ctrl |= DXEPCTL_USBACTEP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001986 dwc2_writel(hsotg, ctrl, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001987}
1988
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001989/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001990 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001991 * @hsotg: The device state.
1992 * @hs_ep: The endpoint the request was on.
1993 * @hs_req: The request to complete.
1994 * @result: The result code (0 => Ok, otherwise errno)
1995 *
1996 * The given request has finished, so call the necessary completion
1997 * if it has one and then look to see if we can start a new request
1998 * on the endpoint.
1999 *
2000 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002001 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002002static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002003 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002004 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002005 int result)
2006{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002007 if (!hs_req) {
2008 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
2009 return;
2010 }
2011
2012 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
2013 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
2014
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002015 /*
2016 * only replace the status if we've not already set an error
2017 * from a previous transaction
2018 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002019
2020 if (hs_req->req.status == -EINPROGRESS)
2021 hs_req->req.status = result;
2022
Yunzhi Li44583fe2015-09-29 12:25:01 +02002023 if (using_dma(hsotg))
2024 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2025
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002026 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01002027
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002028 hs_ep->req = NULL;
2029 list_del_init(&hs_req->queue);
2030
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002031 /*
2032 * call the complete request with the locks off, just in case the
2033 * request tries to queue more work for this endpoint.
2034 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002035
2036 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002037 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002038 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002039 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002040 }
2041
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002042 /* In DDMA don't need to proceed to starting of next ISOC request */
2043 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2044 return;
2045
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002046 /*
2047 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002048 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002049 * so be careful when doing this.
2050 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002051
John Youn34c0887f2017-01-17 20:31:43 -08002052 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002053 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002054}
2055
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002056/*
2057 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2058 * @hs_ep: The endpoint the request was on.
2059 *
2060 * Get first request from the ep queue, determine descriptor on which complete
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002061 * happened. SW discovers which descriptor currently in use by HW, adjusts
2062 * dma_address and calculates index of completed descriptor based on the value
2063 * of DEPDMA register. Update actual length of request, giveback to gadget.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002064 */
2065static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2066{
2067 struct dwc2_hsotg *hsotg = hs_ep->parent;
2068 struct dwc2_hsotg_req *hs_req;
2069 struct usb_request *ureq;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002070 u32 desc_sts;
2071 u32 mask;
2072
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002073 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2074
2075 /* Process only descriptors with buffer status set to DMA done */
2076 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2077 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2078
2079 hs_req = get_ep_head(hs_ep);
2080 if (!hs_req) {
2081 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2082 return;
2083 }
2084 ureq = &hs_req->req;
2085
2086 /* Check completion status */
2087 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2088 DEV_DMA_STS_SUCC) {
2089 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2090 DEV_DMA_ISOC_RX_NBYTES_MASK;
2091 ureq->actual = ureq->length - ((desc_sts & mask) >>
2092 DEV_DMA_ISOC_NBYTES_SHIFT);
2093
2094 /* Adjust actual len for ISOC Out if len is
2095 * not align of 4
2096 */
2097 if (!hs_ep->dir_in && ureq->length & 0x3)
2098 ureq->actual += 4 - (ureq->length & 0x3);
2099 }
2100
2101 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2102
2103 hs_ep->compl_desc++;
2104 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_GENERIC - 1))
2105 hs_ep->compl_desc = 0;
2106 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002107 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002108}
2109
2110/*
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002111 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2112 * @hs_ep: The isochronous endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002113 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002114 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2115 * interrupt. Reset target frame and next_desc to allow to start
2116 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2117 * interrupt for OUT direction.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002118 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002119static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002120{
2121 struct dwc2_hsotg *hsotg = hs_ep->parent;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002122
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002123 if (!hs_ep->dir_in)
2124 dwc2_flush_rx_fifo(hsotg);
2125 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002126
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002127 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2128 hs_ep->next_desc = 0;
2129 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002130}
2131
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002132/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002133 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002134 * @hsotg: The device state.
2135 * @ep_idx: The endpoint index for the data
2136 * @size: The size of data in the fifo, in bytes
2137 *
2138 * The FIFO status shows there is data to read from the FIFO for a given
2139 * endpoint, so sort out whether we need to read the data into a request
2140 * that has been made for that endpoint.
2141 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002142static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002143{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002144 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2145 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002146 int to_read;
2147 int max_req;
2148 int read_ptr;
2149
2150 if (!hs_req) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002151 u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002152 int ptr;
2153
Robert Baldyga6b448af42014-12-16 11:51:44 +01002154 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002155 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002156 __func__, size, ep_idx, epctl);
2157
2158 /* dump the data from the FIFO, we've nothing we can do */
2159 for (ptr = 0; ptr < size; ptr += 4)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002160 (void)dwc2_readl(hsotg, EPFIFO(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002161
2162 return;
2163 }
2164
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002165 to_read = size;
2166 read_ptr = hs_req->req.actual;
2167 max_req = hs_req->req.length - read_ptr;
2168
Ben Dooksa33e7132010-07-19 09:40:49 +01002169 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2170 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2171
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002172 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002173 /*
2174 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002175 * to deal with in this request.
2176 */
2177
2178 /* currently we don't deal this */
2179 WARN_ON_ONCE(1);
2180 }
2181
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002182 hs_ep->total_data += to_read;
2183 hs_req->req.actual += to_read;
2184 to_read = DIV_ROUND_UP(to_read, 4);
2185
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002186 /*
2187 * note, we might over-write the buffer end by 3 bytes depending on
2188 * alignment of the data.
2189 */
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +04002190 dwc2_readl_rep(hsotg, EPFIFO(ep_idx),
2191 hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002192}
2193
2194/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002195 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002196 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002197 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002198 *
2199 * Generate a zero-length IN packet request for terminating a SETUP
2200 * transaction.
2201 *
2202 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002203 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002204 * the TxFIFO.
2205 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002206static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002207{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002208 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002209 hsotg->eps_out[0]->dir_in = dir_in;
2210 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002211
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002212 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002213}
2214
Roman Bacikec1f9d92015-09-10 18:13:43 -07002215static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002216 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002217{
2218 u32 ctrl;
2219
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002220 ctrl = dwc2_readl(hsotg, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002221 if (ctrl & DXEPCTL_EOFRNUM)
2222 ctrl |= DXEPCTL_SETEVENFR;
2223 else
2224 ctrl |= DXEPCTL_SETODDFR;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002225 dwc2_writel(hsotg, ctrl, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002226}
2227
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002228/*
2229 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2230 * @hs_ep - The endpoint on which transfer went
2231 *
2232 * Iterate over endpoints descriptor chain and get info on bytes remained
2233 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2234 */
2235static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2236{
2237 struct dwc2_hsotg *hsotg = hs_ep->parent;
2238 unsigned int bytes_rem = 0;
2239 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2240 int i;
2241 u32 status;
2242
2243 if (!desc)
2244 return -EINVAL;
2245
2246 for (i = 0; i < hs_ep->desc_count; ++i) {
2247 status = desc->status;
2248 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2249
2250 if (status & DEV_DMA_STS_MASK)
2251 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2252 i, status & DEV_DMA_STS_MASK);
2253 }
2254
2255 return bytes_rem;
2256}
2257
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002258/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002259 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002260 * @hsotg: The device instance
2261 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002262 *
2263 * The RXFIFO has delivered an OutDone event, which means that the data
2264 * transfer for an OUT endpoint has been completed, either by a short
2265 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002266 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002267static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002268{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002269 u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002270 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2271 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002272 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002273 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002274 int result = 0;
2275
2276 if (!hs_req) {
2277 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2278 return;
2279 }
2280
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002281 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2282 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002283 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2284 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002285 return;
2286 }
2287
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002288 if (using_desc_dma(hsotg))
2289 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2290
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002291 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002292 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002293
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002294 /*
2295 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002296 * is left in the endpoint size register and then working it
2297 * out from the amount we loaded for the transfer.
2298 *
2299 * We need to do this as DMA pointers are always 32bit aligned
2300 * so may overshoot/undershoot the transfer.
2301 */
2302
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002303 size_done = hs_ep->size_loaded - size_left;
2304 size_done += hs_ep->last_load;
2305
2306 req->actual = size_done;
2307 }
2308
Ben Dooksa33e7132010-07-19 09:40:49 +01002309 /* if there is more request to do, schedule new transfer */
2310 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002311 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002312 return;
2313 }
2314
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002315 if (req->actual < req->length && req->short_not_ok) {
2316 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2317 __func__, req->actual, req->length);
2318
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002319 /*
2320 * todo - what should we return here? there's no one else
2321 * even bothering to check the status.
2322 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002323 }
2324
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002325 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2326 if (!using_desc_dma(hsotg) && epnum == 0 &&
2327 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002328 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002329 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002330 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002331 }
2332
Roman Bacikec1f9d92015-09-10 18:13:43 -07002333 /*
2334 * Slave mode OUT transfers do not go through XferComplete so
2335 * adjust the ISOC parity here.
2336 */
2337 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002338 if (hs_ep->isochronous && hs_ep->interval == 1)
2339 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002340 else if (hs_ep->isochronous && hs_ep->interval > 1)
2341 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002342 }
2343
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002344 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002345}
2346
2347/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002348 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002349 * @hsotg: The device instance
2350 *
2351 * The IRQ handler has detected that the RX FIFO has some data in it
2352 * that requires processing, so find out what is in there and do the
2353 * appropriate read.
2354 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002355 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002356 * chunks, so if you have x packets received on an endpoint you'll get x
2357 * FIFO events delivered, each with a packet's worth of data in it.
2358 *
2359 * When using DMA, we should not be processing events from the RXFIFO
2360 * as the actual data should be sent to the memory directly and we turn
2361 * on the completion interrupts to get notifications of transfer completion.
2362 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002363static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002364{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002365 u32 grxstsr = dwc2_readl(hsotg, GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366 u32 epnum, status, size;
2367
2368 WARN_ON(using_dma(hsotg));
2369
Dinh Nguyen47a16852014-04-14 14:13:34 -07002370 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2371 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002372
Dinh Nguyen47a16852014-04-14 14:13:34 -07002373 size = grxstsr & GRXSTS_BYTECNT_MASK;
2374 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002375
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002376 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002377 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002378
Dinh Nguyen47a16852014-04-14 14:13:34 -07002379 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2380 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2381 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002382 break;
2383
Dinh Nguyen47a16852014-04-14 14:13:34 -07002384 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002385 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002386 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002387
2388 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002389 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002390 break;
2391
Dinh Nguyen47a16852014-04-14 14:13:34 -07002392 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002393 dev_dbg(hsotg->dev,
2394 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002395 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002396 dwc2_readl(hsotg, DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002397 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002398 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002399 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2400 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2401 */
2402 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002403 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002404 break;
2405
Dinh Nguyen47a16852014-04-14 14:13:34 -07002406 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002407 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002408 break;
2409
Dinh Nguyen47a16852014-04-14 14:13:34 -07002410 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002411 dev_dbg(hsotg->dev,
2412 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002413 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002414 dwc2_readl(hsotg, DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002415
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002416 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2417
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002418 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002419 break;
2420
2421 default:
2422 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2423 __func__, grxstsr);
2424
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002425 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002426 break;
2427 }
2428}
2429
2430/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002431 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002432 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002433 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002434static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002435{
2436 switch (mps) {
2437 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002438 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002439 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002440 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002441 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002442 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002443 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002444 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002445 }
2446
2447 /* bad max packet size, warn and return invalid result */
2448 WARN_ON(1);
2449 return (u32)-1;
2450}
2451
2452/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002453 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002454 * @hsotg: The driver state.
2455 * @ep: The index number of the endpoint
2456 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002457 * @mc: The multicount value
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002458 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002459 *
2460 * Configure the maximum packet size for the given endpoint, updating
2461 * the hardware control registers to reflect this.
2462 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002463static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002464 unsigned int ep, unsigned int mps,
2465 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002466{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002467 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002468 u32 reg;
2469
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002470 hs_ep = index_to_ep(hsotg, ep, dir_in);
2471 if (!hs_ep)
2472 return;
2473
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002474 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002475 u32 mps_bytes = mps;
2476
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002477 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002478 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2479 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002480 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002481 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002482 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002483 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002484 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002485 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002486 hs_ep->mc = mc;
2487 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002488 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002489 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002490 }
2491
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002492 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002493 reg = dwc2_readl(hsotg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002494 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002495 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002496 dwc2_writel(hsotg, reg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002497 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002498 reg = dwc2_readl(hsotg, DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002499 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002500 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002501 dwc2_writel(hsotg, reg, DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002502 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002503
2504 return;
2505
2506bad_mps:
2507 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2508}
2509
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002510/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002511 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002512 * @hsotg: The driver state
2513 * @idx: The index for the endpoint (0..15)
2514 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002515static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002516{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002517 dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2518 GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002519
2520 /* wait until the fifo is flushed */
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002521 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2522 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2523 __func__);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002524}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002525
2526/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002527 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002528 * @hsotg: The driver state
2529 * @hs_ep: The driver endpoint to check.
2530 *
2531 * Check to see if there is a request that has data to send, and if so
2532 * make an attempt to write data into the FIFO.
2533 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002534static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002535 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002536{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002537 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002538
Robert Baldygaafcf4162013-09-19 11:50:19 +02002539 if (!hs_ep->dir_in || !hs_req) {
2540 /**
2541 * if request is not enqueued, we disable interrupts
2542 * for endpoints, excepting ep0
2543 */
2544 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002545 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002546 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002547 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002548 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002549
2550 if (hs_req->req.actual < hs_req->req.length) {
2551 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2552 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002553 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002554 }
2555
2556 return 0;
2557}
2558
2559/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002560 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002561 * @hsotg: The device state.
2562 * @hs_ep: The endpoint that has just completed.
2563 *
2564 * An IN transfer has been completed, update the transfer's state and then
2565 * call the relevant completion routines.
2566 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002567static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002568 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002569{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002570 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002571 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002572 int size_left, size_done;
2573
2574 if (!hs_req) {
2575 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2576 return;
2577 }
2578
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002579 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002580 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2581 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002582
2583 /*
2584 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2585 * changed to IN. Change back to complete OUT transfer request
2586 */
2587 hs_ep->dir_in = 0;
2588
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002589 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002590 if (hsotg->test_mode) {
2591 int ret;
2592
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002593 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002594 if (ret < 0) {
2595 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002596 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002597 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002598 return;
2599 }
2600 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002601 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002602 return;
2603 }
2604
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002605 /*
2606 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002607 * in the endpoint size register and then working it out from
2608 * the amount we loaded for the transfer.
2609 *
2610 * We do this even for DMA, as the transfer may have incremented
2611 * past the end of the buffer (DMA transfers are always 32bit
2612 * aligned).
2613 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002614 if (using_desc_dma(hsotg)) {
2615 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2616 if (size_left < 0)
2617 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2618 size_left);
2619 } else {
2620 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2621 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002622
2623 size_done = hs_ep->size_loaded - size_left;
2624 size_done += hs_ep->last_load;
2625
2626 if (hs_req->req.actual != size_done)
2627 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2628 __func__, hs_req->req.actual, size_done);
2629
2630 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002631 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2632 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002633
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002634 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2635 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002636 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002637 return;
2638 }
2639
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002640 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002641 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002642 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002643 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002644 /* transfer will be completed on next complete interrupt */
2645 return;
2646 }
2647
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002648 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2649 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002650 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002651 return;
2652 }
2653
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002654 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002655}
2656
2657/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002658 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2659 * @hsotg: The device state.
2660 * @idx: Index of ep.
2661 * @dir_in: Endpoint direction 1-in 0-out.
2662 *
2663 * Reads for endpoint with given index and direction, by masking
2664 * epint_reg with coresponding mask.
2665 */
2666static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2667 unsigned int idx, int dir_in)
2668{
2669 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2670 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2671 u32 ints;
2672 u32 mask;
2673 u32 diepempmsk;
2674
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002675 mask = dwc2_readl(hsotg, epmsk_reg);
2676 diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002677 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2678 mask |= DXEPINT_SETUP_RCVD;
2679
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002680 ints = dwc2_readl(hsotg, epint_reg);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002681 ints &= mask;
2682 return ints;
2683}
2684
2685/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002686 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2687 * @hs_ep: The endpoint on which interrupt is asserted.
2688 *
2689 * This interrupt indicates that the endpoint has been disabled per the
2690 * application's request.
2691 *
2692 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2693 * in case of ISOC completes current request.
2694 *
2695 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2696 * request starts it.
2697 */
2698static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2699{
2700 struct dwc2_hsotg *hsotg = hs_ep->parent;
2701 struct dwc2_hsotg_req *hs_req;
2702 unsigned char idx = hs_ep->index;
2703 int dir_in = hs_ep->dir_in;
2704 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002705 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002706
2707 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2708
2709 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002710 int epctl = dwc2_readl(hsotg, epctl_reg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002711
2712 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2713
2714 if (hs_ep->isochronous) {
2715 dwc2_hsotg_complete_in(hsotg, hs_ep);
2716 return;
2717 }
2718
2719 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002720 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002721
2722 dctl |= DCTL_CGNPINNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002723 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002724 }
2725 return;
2726 }
2727
2728 if (dctl & DCTL_GOUTNAKSTS) {
2729 dctl |= DCTL_CGOUTNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002730 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002731 }
2732
2733 if (!hs_ep->isochronous)
2734 return;
2735
2736 if (list_empty(&hs_ep->queue)) {
2737 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2738 __func__, hs_ep);
2739 return;
2740 }
2741
2742 do {
2743 hs_req = get_ep_head(hs_ep);
2744 if (hs_req)
2745 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2746 -ENODATA);
2747 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04002748 /* Update current frame number value. */
2749 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002750 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2751
2752 dwc2_gadget_start_next_request(hs_ep);
2753}
2754
2755/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002756 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002757 * @ep: The endpoint on which interrupt is asserted.
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002758 *
2759 * This is starting point for ISOC-OUT transfer, synchronization done with
2760 * first out token received from host while corresponding EP is disabled.
2761 *
2762 * Device does not know initial frame in which out token will come. For this
2763 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2764 * getting this interrupt SW starts calculation for next transfer frame.
2765 */
2766static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2767{
2768 struct dwc2_hsotg *hsotg = ep->parent;
2769 int dir_in = ep->dir_in;
2770 u32 doepmsk;
2771
2772 if (dir_in || !ep->isochronous)
2773 return;
2774
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002775 if (using_desc_dma(hsotg)) {
2776 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2777 /* Start first ISO Out */
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002778 ep->target_frame = hsotg->frame_number;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002779 dwc2_gadget_start_isoc_ddma(ep);
2780 }
2781 return;
2782 }
2783
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002784 if (ep->interval > 1 &&
2785 ep->target_frame == TARGET_FRAME_INITIAL) {
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002786 u32 ctrl;
2787
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002788 ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002789 dwc2_gadget_incr_frame_num(ep);
2790
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002791 ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002792 if (ep->target_frame & 0x1)
2793 ctrl |= DXEPCTL_SETODDFR;
2794 else
2795 ctrl |= DXEPCTL_SETEVENFR;
2796
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002797 dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002798 }
2799
2800 dwc2_gadget_start_next_request(ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002801 doepmsk = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002802 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002803 dwc2_writel(hsotg, doepmsk, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002804}
2805
2806/**
John Youn38beaec2017-01-17 20:31:13 -08002807 * dwc2_gadget_handle_nak - handle NAK interrupt
2808 * @hs_ep: The endpoint on which interrupt is asserted.
2809 *
2810 * This is starting point for ISOC-IN transfer, synchronization done with
2811 * first IN token received from host while corresponding EP is disabled.
2812 *
2813 * Device does not know when first one token will arrive from host. On first
2814 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2815 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2816 * sent in response to that as there was no data in FIFO. SW is basing on this
2817 * interrupt to obtain frame in which token has come and then based on the
2818 * interval calculates next frame for transfer.
2819 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002820static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2821{
2822 struct dwc2_hsotg *hsotg = hs_ep->parent;
2823 int dir_in = hs_ep->dir_in;
2824
2825 if (!dir_in || !hs_ep->isochronous)
2826 return;
2827
2828 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002829
2830 if (using_desc_dma(hsotg)) {
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002831 hs_ep->target_frame = hsotg->frame_number;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002832 dwc2_gadget_incr_frame_num(hs_ep);
Grigor Tovmasyan48dac4e2018-08-29 21:00:33 +04002833
2834 /* In service interval mode target_frame must
2835 * be set to last (u)frame of the service interval.
2836 */
2837 if (hsotg->params.service_interval) {
2838 /* Set target_frame to the first (u)frame of
2839 * the service interval
2840 */
2841 hs_ep->target_frame &= ~hs_ep->interval + 1;
2842
2843 /* Set target_frame to the last (u)frame of
2844 * the service interval
2845 */
2846 dwc2_gadget_incr_frame_num(hs_ep);
2847 dwc2_gadget_dec_frame_num_by_one(hs_ep);
2848 }
2849
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002850 dwc2_gadget_start_isoc_ddma(hs_ep);
2851 return;
2852 }
2853
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002854 hs_ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002855 if (hs_ep->interval > 1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002856 u32 ctrl = dwc2_readl(hsotg,
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002857 DIEPCTL(hs_ep->index));
2858 if (hs_ep->target_frame & 0x1)
2859 ctrl |= DXEPCTL_SETODDFR;
2860 else
2861 ctrl |= DXEPCTL_SETEVENFR;
2862
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002863 dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002864 }
2865
2866 dwc2_hsotg_complete_request(hsotg, hs_ep,
2867 get_ep_head(hs_ep), 0);
2868 }
2869
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002870 if (!using_desc_dma(hsotg))
2871 dwc2_gadget_incr_frame_num(hs_ep);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002872}
2873
2874/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002875 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002876 * @hsotg: The driver state
2877 * @idx: The index for the endpoint (0..15)
2878 * @dir_in: Set if this is an IN endpoint
2879 *
2880 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002881 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002882static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002883 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002884{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002885 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002886 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2887 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2888 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002889 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002890 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002891
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002892 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002893 ctrl = dwc2_readl(hsotg, epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002894
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002895 /* Clear endpoint interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002896 dwc2_writel(hsotg, ints, epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002897
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002898 if (!hs_ep) {
2899 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002900 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002901 return;
2902 }
2903
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002904 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2905 __func__, idx, dir_in ? "in" : "out", ints);
2906
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002907 /* Don't process XferCompl interrupt if it is a setup packet */
2908 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2909 ints &= ~DXEPINT_XFERCOMPL;
2910
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002911 /*
2912 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2913 * stage and xfercomplete was generated without SETUP phase done
2914 * interrupt. SW should parse received setup packet only after host's
2915 * exit from setup phase of control transfer.
2916 */
2917 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2918 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2919 ints &= ~DXEPINT_XFERCOMPL;
2920
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002921 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002922 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002923 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002924 __func__, dwc2_readl(hsotg, epctl_reg),
2925 dwc2_readl(hsotg, epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002926
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002927 /* In DDMA handle isochronous requests separately */
2928 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002929 /* XferCompl set along with BNA */
2930 if (!(ints & DXEPINT_BNAINTR))
2931 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002932 } else if (dir_in) {
2933 /*
2934 * We get OutDone from the FIFO, so we only
2935 * need to look at completing IN requests here
2936 * if operating slave mode
2937 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002938 if (hs_ep->isochronous && hs_ep->interval > 1)
2939 dwc2_gadget_incr_frame_num(hs_ep);
2940
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002941 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002942 if (ints & DXEPINT_NAKINTRPT)
2943 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002944
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002945 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002946 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002947 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002948 /*
2949 * We're using DMA, we need to fire an OutDone here
2950 * as we ignore the RXFIFO.
2951 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002952 if (hs_ep->isochronous && hs_ep->interval > 1)
2953 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002954
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002955 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002956 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002957 }
2958
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002959 if (ints & DXEPINT_EPDISBLD)
2960 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002961
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002962 if (ints & DXEPINT_OUTTKNEPDIS)
2963 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2964
2965 if (ints & DXEPINT_NAKINTRPT)
2966 dwc2_gadget_handle_nak(hs_ep);
2967
Dinh Nguyen47a16852014-04-14 14:13:34 -07002968 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002969 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002970
Dinh Nguyen47a16852014-04-14 14:13:34 -07002971 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002972 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2973
2974 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002975 /*
2976 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002977 * setup packet. In non-DMA mode we'd get this
2978 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002979 * the setup here.
2980 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002981
2982 if (dir_in)
2983 WARN_ON_ONCE(1);
2984 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002985 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002986 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002987 }
2988
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002989 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08002990 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
2991
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04002992 /* Safety check EP0 state when STSPHSERCVD asserted */
2993 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
2994 /* Move to STATUS IN for DDMA */
2995 if (using_desc_dma(hsotg))
2996 dwc2_hsotg_ep0_zlp(hsotg, true);
2997 }
2998
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002999 }
3000
Dinh Nguyen47a16852014-04-14 14:13:34 -07003001 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003002 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003003
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003004 if (ints & DXEPINT_BNAINTR) {
3005 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003006 if (hs_ep->isochronous)
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003007 dwc2_gadget_handle_isoc_bna(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003008 }
3009
Robert Baldyga1479e842013-10-09 08:41:57 +02003010 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003011 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003012 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003013 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
3014 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003015 }
3016
3017 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003018 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003019 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
3020 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003021 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003022
3023 /* FIFO has space or is empty (see GAHBCFG) */
3024 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003025 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01003026 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3027 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09003028 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003029 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01003030 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003031 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003032}
3033
3034/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003035 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003036 * @hsotg: The device state.
3037 *
3038 * Handle updating the device settings after the enumeration phase has
3039 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003040 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003041static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003042{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003043 u32 dsts = dwc2_readl(hsotg, DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003044 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003045
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003046 /*
3047 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003048 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003049 * we connected at.
3050 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003051
3052 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3053
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003054 /*
3055 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003056 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003057 * not advertise a 64byte MPS on EP0.
3058 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003059
3060 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003061 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003062 case DSTS_ENUMSPD_FS:
3063 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003064 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003065 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003066 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003067 break;
3068
Dinh Nguyen47a16852014-04-14 14:13:34 -07003069 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003070 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003071 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003072 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003073 break;
3074
Dinh Nguyen47a16852014-04-14 14:13:34 -07003075 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003076 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003077 ep0_mps = 8;
3078 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003079 /*
3080 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003081 * moment, and the documentation seems to imply that it isn't
3082 * supported by the PHYs on some of the devices.
3083 */
3084 break;
3085 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003086 dev_info(hsotg->dev, "new device is %s\n",
3087 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003088
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003089 /*
3090 * we should now know the maximum packet size for an
3091 * endpoint, so set the endpoints to a default value.
3092 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003093
3094 if (ep0_mps) {
3095 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003096 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003097 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3098 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003099 for (i = 1; i < hsotg->num_of_eps; i++) {
3100 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003101 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3102 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003103 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003104 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3105 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003106 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003107 }
3108
3109 /* ensure after enumeration our EP0 is active */
3110
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003111 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003112
3113 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003114 dwc2_readl(hsotg, DIEPCTL0),
3115 dwc2_readl(hsotg, DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003116}
3117
3118/**
3119 * kill_all_requests - remove all requests from the endpoint's queue
3120 * @hsotg: The device state.
3121 * @ep: The endpoint the requests may be on.
3122 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003123 *
3124 * Go through the requests on the given endpoint and mark them
3125 * completed with the given result code.
3126 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003127static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003128 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af42014-12-16 11:51:44 +01003129 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003130{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003131 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003132 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003133
Robert Baldyga6b448af42014-12-16 11:51:44 +01003134 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003135
Robert Baldyga6b448af42014-12-16 11:51:44 +01003136 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003137 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003138 result);
Robert Baldyga6b448af42014-12-16 11:51:44 +01003139
Robert Baldygab203d0a2014-09-09 10:44:56 +02003140 if (!hsotg->dedicated_fifos)
3141 return;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003142 size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003143 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003144 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003145}
3146
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003147static int dwc2_hsotg_ep_disable(struct usb_ep *ep);
3148
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003149/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003150 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003151 * @hsotg: The device state.
3152 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003153 * The device has been disconnected. Remove all current
3154 * transactions and signal the gadget driver that this
3155 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003156 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003157void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003158{
John Youn9da51972017-01-17 20:30:27 -08003159 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003160
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003161 if (!hsotg->connected)
3162 return;
3163
3164 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003165 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003166
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003167 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003168 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3169 if (hsotg->eps_in[ep])
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003170 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003171 if (hsotg->eps_out[ep])
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003172 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003173 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003174
3175 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003176 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003177
3178 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003179}
3180
3181/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003182 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003183 * @hsotg: The device state:
3184 * @periodic: True if this is a periodic FIFO interrupt
3185 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003186static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003187{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003188 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003189 int epno, ret;
3190
3191 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003192 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003193 ep = index_to_ep(hsotg, epno, 1);
3194
3195 if (!ep)
3196 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003197
3198 if (!ep->dir_in)
3199 continue;
3200
3201 if ((periodic && !ep->periodic) ||
3202 (!periodic && ep->periodic))
3203 continue;
3204
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003205 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003206 if (ret < 0)
3207 break;
3208 }
3209}
3210
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003211/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003212#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3213 GINTSTS_PTXFEMP | \
3214 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003215
3216/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003217 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003218 * @hsotg: The device state
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04003219 * @is_usb_reset: Usb resetting flag
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003220 *
3221 * Issue a soft reset to the core, and await the core finishing it.
3222 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003223void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003224 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003225{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003226 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003227 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003228 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003229 u32 dcfg = 0;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003230 int ep;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003231
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003232 /* Kill any ep0 requests as controller will be reinitialized */
3233 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3234
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003235 if (!is_usb_reset) {
John Stultz6e6360b2017-01-23 14:59:14 -08003236 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003237 return;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003238 } else {
3239 /* all endpoints should be shutdown */
3240 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3241 if (hsotg->eps_in[ep])
3242 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3243 if (hsotg->eps_out[ep])
3244 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3245 }
3246 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003247
3248 /*
3249 * we must now enable ep0 ready for host detection and then
3250 * set configuration.
3251 */
3252
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003253 /* keep other bits untouched (so e.g. forced modes are not lost) */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003254 usbcfg = dwc2_readl(hsotg, GUSBCFG);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003255 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01003256 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003257
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003258 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003259 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3260 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003261 /* FS/LS Dedicated Transceiver Interface */
3262 usbcfg |= GUSBCFG_PHYSEL;
3263 } else {
3264 /* set the PLL on, remove the HNP/SRP and set the PHY */
3265 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3266 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3267 (val << GUSBCFG_USBTRDTIM_SHIFT);
3268 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003269 dwc2_writel(hsotg, usbcfg, GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003270
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003271 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003272
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003273 if (!is_usb_reset)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003274 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003275
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003276 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003277
3278 switch (hsotg->params.speed) {
3279 case DWC2_SPEED_PARAM_LOW:
3280 dcfg |= DCFG_DEVSPD_LS;
3281 break;
3282 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003283 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3284 dcfg |= DCFG_DEVSPD_FS48;
3285 else
3286 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003287 break;
3288 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003289 dcfg |= DCFG_DEVSPD_HS;
3290 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003291
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +04003292 if (hsotg->params.ipg_isoc_en)
3293 dcfg |= DCFG_IPG_ISOC_SUPPORDED;
3294
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003295 dwc2_writel(hsotg, dcfg, DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003296
3297 /* Clear any pending OTG interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003298 dwc2_writel(hsotg, 0xffffffff, GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003299
3300 /* Clear any pending interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003301 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003302 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003303 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003304 GINTSTS_USBRST | GINTSTS_RESETDET |
3305 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003306 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3307 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003308
3309 if (!using_desc_dma(hsotg))
3310 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003311
John Youn95832c02017-01-23 14:57:26 -08003312 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003313 intmsk |= GINTSTS_CONIDSTSCHNG;
3314
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003315 dwc2_writel(hsotg, intmsk, GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003316
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003317 if (using_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003318 dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003319 hsotg->params.ahbcfg,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003320 GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003321
3322 /* Set DDMA mode support in the core if needed */
3323 if (using_desc_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003324 dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003325
3326 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003327 dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ?
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003328 (GAHBCFG_NP_TXF_EMP_LVL |
3329 GAHBCFG_P_TXF_EMP_LVL) : 0) |
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003330 GAHBCFG_GLBL_INTR_EN, GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003331 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003332
3333 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003334 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3335 * when we have no data to transfer. Otherwise we get being flooded by
3336 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003337 */
3338
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003339 dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003340 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003341 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003342 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003343 DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003344
3345 /*
3346 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003347 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003348 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003349 dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003350 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003351 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003352 DOEPMSK_SETUPMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003353 DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003354
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003355 /* Enable BNA interrupt for DDMA */
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003356 if (using_desc_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003357 dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK);
3358 dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK);
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003359 }
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003360
Grigor Tovmasyanca531bc2018-08-29 20:59:34 +04003361 /* Enable Service Interval mode if supported */
3362 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3363 dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED);
3364
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003365 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003366
3367 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003368 dwc2_readl(hsotg, DIEPCTL0),
3369 dwc2_readl(hsotg, DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003370
3371 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003372 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003373
3374 /*
3375 * Enable the RXFIFO when in slave mode, as this is how we collect
3376 * the data. In DMA mode, we get events from the FIFO but also
3377 * things we cannot process, so do not use it.
3378 */
3379 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003380 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003381
3382 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003383 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3384 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003385
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003386 if (!is_usb_reset) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003387 dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003388 udelay(10); /* see openiboot */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003389 dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003390 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003391
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003392 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003393
3394 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003395 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003396 * writing to the EPCTL register..
3397 */
3398
3399 /* set to read 1 8byte packet */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003400 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
3401 DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003402
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003403 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003404 DXEPCTL_CNAK | DXEPCTL_EPENA |
3405 DXEPCTL_USBACTEP,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003406 DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003407
3408 /* enable, but don't activate EP0in */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003409 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3410 DXEPCTL_USBACTEP, DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003411
Lukasz Majewski308d7342012-05-04 14:17:05 +02003412 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003413 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3414 if (!is_usb_reset)
3415 val |= DCTL_SFTDISCON;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003416 dwc2_set_bit(hsotg, DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003417
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003418 /* configure the core to support LPM */
3419 dwc2_gadget_init_lpm(hsotg);
3420
Lukasz Majewski308d7342012-05-04 14:17:05 +02003421 /* must be at-least 3ms to allow bus to see disconnect */
3422 mdelay(3);
3423
Gregory Herrero065d3932015-09-22 15:16:54 +02003424 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003425
3426 dwc2_hsotg_enqueue_setup(hsotg);
3427
3428 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003429 dwc2_readl(hsotg, DIEPCTL0),
3430 dwc2_readl(hsotg, DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003431}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003432
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003433static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003434{
3435 /* set the soft-disconnect bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003436 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003437}
3438
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003439void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003440{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003441 /* remove the soft-disconnect and let's go */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003442 dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003443}
3444
3445/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003446 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3447 * @hsotg: The device state:
3448 *
3449 * This interrupt indicates one of the following conditions occurred while
3450 * transmitting an ISOC transaction.
3451 * - Corrupted IN Token for ISOC EP.
3452 * - Packet not complete in FIFO.
3453 *
3454 * The following actions will be taken:
3455 * - Determine the EP
3456 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3457 */
3458static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3459{
3460 struct dwc2_hsotg_ep *hs_ep;
3461 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003462 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003463 u32 idx;
3464
3465 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3466
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003467 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003468
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003469 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003470 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003471 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003472 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003473 continue;
3474
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003475 epctrl = dwc2_readl(hsotg, DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003476 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003477 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3478 epctrl |= DXEPCTL_SNAK;
3479 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003480 dwc2_writel(hsotg, epctrl, DIEPCTL(idx));
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003481 }
3482 }
3483
3484 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003485 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003486}
3487
3488/**
3489 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3490 * @hsotg: The device state:
3491 *
3492 * This interrupt indicates one of the following conditions occurred while
3493 * transmitting an ISOC transaction.
3494 * - Corrupted OUT Token for ISOC EP.
3495 * - Packet not complete in FIFO.
3496 *
3497 * The following actions will be taken:
3498 * - Determine the EP
3499 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3500 */
3501static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3502{
3503 u32 gintsts;
3504 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003505 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003506 u32 epctrl;
3507 struct dwc2_hsotg_ep *hs_ep;
3508 int idx;
3509
3510 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3511
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003512 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003513 daintmsk >>= DAINT_OUTEP_SHIFT;
3514
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003515 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003516 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003517 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003518 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003519 continue;
3520
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003521 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003522 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003523 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3524 /* Unmask GOUTNAKEFF interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003525 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003526 gintmsk |= GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003527 dwc2_writel(hsotg, gintmsk, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003528
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003529 gintsts = dwc2_readl(hsotg, GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003530 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003531 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003532 break;
3533 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003534 }
3535 }
3536
3537 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003538 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003539}
3540
3541/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003542 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003543 * @irq: The IRQ number triggered
3544 * @pw: The pw value when registered the handler.
3545 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003546static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003547{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003548 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003549 int retry_count = 8;
3550 u32 gintsts;
3551 u32 gintmsk;
3552
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003553 if (!dwc2_is_device_mode(hsotg))
3554 return IRQ_NONE;
3555
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003556 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003557irq_retry:
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003558 gintsts = dwc2_readl(hsotg, GINTSTS);
3559 gintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003560
3561 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3562 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3563
3564 gintsts &= gintmsk;
3565
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003566 if (gintsts & GINTSTS_RESETDET) {
3567 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3568
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003569 dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003570
3571 /* This event must be used only if controller is suspended */
3572 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003573 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003574 hsotg->lx_state = DWC2_L0;
3575 }
3576 }
3577
3578 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003579 u32 usb_status = dwc2_readl(hsotg, GOTGCTL);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003580 u32 connected = hsotg->connected;
3581
3582 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3583 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003584 dwc2_readl(hsotg, GNPTXSTS));
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003585
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003586 dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003587
3588 /* Report disconnection if it is not already done. */
3589 dwc2_hsotg_disconnect(hsotg);
3590
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003591 /* Reset device address to zero */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003592 dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003593
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003594 if (usb_status & GOTGCTL_BSESVLD && connected)
3595 dwc2_hsotg_core_init_disconnected(hsotg, true);
3596 }
3597
Dinh Nguyen47a16852014-04-14 14:13:34 -07003598 if (gintsts & GINTSTS_ENUMDONE) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003599 dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003600
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003601 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003602 }
3603
Dinh Nguyen47a16852014-04-14 14:13:34 -07003604 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003605 u32 daint = dwc2_readl(hsotg, DAINT);
3606 u32 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003607 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003608 int ep;
3609
Robert Baldyga7e804652013-09-19 11:50:20 +02003610 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003611 daint_out = daint >> DAINT_OUTEP_SHIFT;
3612 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003613
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003614 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3615
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003616 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3617 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003618 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003619 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003620 }
3621
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003622 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3623 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003624 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003625 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003626 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003627 }
3628
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003629 /* check both FIFOs */
3630
Dinh Nguyen47a16852014-04-14 14:13:34 -07003631 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003632 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3633
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003634 /*
3635 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003636 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003637 * it needs re-enabling
3638 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003639
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003640 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3641 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003642 }
3643
Dinh Nguyen47a16852014-04-14 14:13:34 -07003644 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003645 dev_dbg(hsotg->dev, "PTxFEmp\n");
3646
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003647 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003648
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003649 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3650 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003651 }
3652
Dinh Nguyen47a16852014-04-14 14:13:34 -07003653 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003654 /*
3655 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003656 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003657 * set.
3658 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003659
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003660 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003661 }
3662
Dinh Nguyen47a16852014-04-14 14:13:34 -07003663 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003664 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003665 dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003666 }
3667
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003668 /*
3669 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003670 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003671 * the occurrence.
3672 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003673
Dinh Nguyen47a16852014-04-14 14:13:34 -07003674 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003675 u8 idx;
3676 u32 epctrl;
3677 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003678 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003679 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003680
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003681 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyand8484552018-01-19 14:41:42 +04003682 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003683 /* Mask this interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003684 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003685 gintmsk &= ~GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003686 dwc2_writel(hsotg, gintmsk, GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003687
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003688 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003689 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003690 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003691 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003692 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyand8484552018-01-19 14:41:42 +04003693 continue;
3694
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003695 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003696
Razmik Karapetyand8484552018-01-19 14:41:42 +04003697 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003698 epctrl |= DXEPCTL_SNAK;
3699 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003700 dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003701 }
3702 }
3703
3704 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003705 }
3706
Dinh Nguyen47a16852014-04-14 14:13:34 -07003707 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003708 dev_info(hsotg->dev, "GINNakEff triggered\n");
3709
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003710 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003711
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003712 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003713 }
3714
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003715 if (gintsts & GINTSTS_INCOMPL_SOIN)
3716 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003717
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003718 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3719 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003720
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003721 /*
3722 * if we've had fifo events, we should try and go around the
3723 * loop again to see if there's any point in returning yet.
3724 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003725
3726 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003727 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003728
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003729 spin_unlock(&hsotg->lock);
3730
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003731 return IRQ_HANDLED;
3732}
3733
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003734static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3735 struct dwc2_hsotg_ep *hs_ep)
3736{
3737 u32 epctrl_reg;
3738 u32 epint_reg;
3739
3740 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3741 DOEPCTL(hs_ep->index);
3742 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3743 DOEPINT(hs_ep->index);
3744
3745 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3746 hs_ep->name);
3747
3748 if (hs_ep->dir_in) {
3749 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003750 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003751 /* Wait for Nak effect */
3752 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3753 DXEPINT_INEPNAKEFF, 100))
3754 dev_warn(hsotg->dev,
3755 "%s: timeout DIEPINT.NAKEFF\n",
3756 __func__);
3757 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003758 dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003759 /* Wait for Nak effect */
3760 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3761 GINTSTS_GINNAKEFF, 100))
3762 dev_warn(hsotg->dev,
3763 "%s: timeout GINTSTS.GINNAKEFF\n",
3764 __func__);
3765 }
3766 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003767 if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF))
3768 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003769
3770 /* Wait for global nak to take effect */
3771 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3772 GINTSTS_GOUTNAKEFF, 100))
3773 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3774 __func__);
3775 }
3776
3777 /* Disable ep */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003778 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003779
3780 /* Wait for ep to be disabled */
3781 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3782 dev_warn(hsotg->dev,
3783 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3784
3785 /* Clear EPDISBLD interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003786 dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003787
3788 if (hs_ep->dir_in) {
3789 unsigned short fifo_index;
3790
3791 if (hsotg->dedicated_fifos || hs_ep->periodic)
3792 fifo_index = hs_ep->fifo_index;
3793 else
3794 fifo_index = 0;
3795
3796 /* Flush TX FIFO */
3797 dwc2_flush_tx_fifo(hsotg, fifo_index);
3798
3799 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3800 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003801 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003802
3803 } else {
3804 /* Remove global NAKs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003805 dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003806 }
3807}
3808
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003809/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003810 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003811 * @ep: The USB endpint to configure
3812 * @desc: The USB endpoint descriptor to configure with.
3813 *
3814 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003815 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003816static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003817 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003818{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003819 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003820 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003821 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003822 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003823 u32 epctrl_reg;
3824 u32 epctrl;
3825 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003826 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003827 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003828 unsigned int dir_in;
3829 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003830 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003831 unsigned char ep_type;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003832
3833 dev_dbg(hsotg->dev,
3834 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3835 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3836 desc->wMaxPacketSize, desc->bInterval);
3837
3838 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003839 if (index == 0) {
3840 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3841 return -EINVAL;
3842 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003843
3844 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3845 if (dir_in != hs_ep->dir_in) {
3846 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3847 return -EINVAL;
3848 }
3849
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003850 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003851 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003852 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003853
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003854 /* ISOC IN in DDMA supported bInterval up to 10 */
3855 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3856 dir_in && desc->bInterval > 10) {
3857 dev_err(hsotg->dev,
3858 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3859 return -EINVAL;
3860 }
3861
3862 /* High bandwidth ISOC OUT in DDMA not supported */
3863 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3864 !dir_in && mc > 1) {
3865 dev_err(hsotg->dev,
3866 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3867 return -EINVAL;
3868 }
3869
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003870 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003871
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003872 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003873 epctrl = dwc2_readl(hsotg, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003874
3875 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3876 __func__, epctrl, epctrl_reg);
3877
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003878 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003879 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3880 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003881 MAX_DMA_DESC_NUM_GENERIC *
3882 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003883 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003884 if (!hs_ep->desc_list) {
3885 ret = -ENOMEM;
3886 goto error2;
3887 }
3888 }
3889
Lukasz Majewski22258f42012-06-14 10:02:24 +02003890 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003891
Dinh Nguyen47a16852014-04-14 14:13:34 -07003892 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3893 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003894
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003895 /*
3896 * mark the endpoint as active, otherwise the core may ignore
3897 * transactions entirely for this endpoint
3898 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003899 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003900
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003901 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003902 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003903
3904 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003905 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003906 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003907 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003908 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003909
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003910 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003911 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003912 epctrl |= DXEPCTL_EPTYPE_ISO;
3913 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003914 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003915 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003916 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003917 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003918 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003919 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003920 hs_ep->periodic = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003921 mask = dwc2_readl(hsotg, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003922 mask |= DIEPMSK_NAKMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003923 dwc2_writel(hsotg, mask, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003924 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003925 mask = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003926 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003927 dwc2_writel(hsotg, mask, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003928 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003929 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003930
3931 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003932 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003933 break;
3934
3935 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003936 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003937 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003938
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003939 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3940 hs_ep->interval = 1 << (desc->bInterval - 1);
3941
Dinh Nguyen47a16852014-04-14 14:13:34 -07003942 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003943 break;
3944
3945 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003946 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003947 break;
3948 }
3949
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003950 /*
3951 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003952 * a unique tx-fifo even if it is non-periodic.
3953 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003954 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003955 u32 fifo_index = 0;
3956 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08003957
3958 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003959 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08003960 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02003961 continue;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003962 val = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08003963 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003964 if (val < size)
3965 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003966 /* Search for smallest acceptable fifo */
3967 if (val < fifo_size) {
3968 fifo_size = val;
3969 fifo_index = i;
3970 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003971 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003972 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003973 dev_err(hsotg->dev,
3974 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303975 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003976 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303977 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003978 hsotg->fifo_map |= 1 << fifo_index;
3979 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3980 hs_ep->fifo_index = fifo_index;
3981 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003982 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003983
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003984 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003985 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003986 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003987
Artur Petrosyan52953222018-04-16 08:45:31 -04003988 /* WA for Full speed ISOC IN in DDMA mode.
3989 * By Clear NAK status of EP, core will send ZLP
3990 * to IN token and assert NAK interrupt relying
3991 * on TxFIFO status only
3992 */
3993
3994 if (hsotg->gadget.speed == USB_SPEED_FULL &&
3995 hs_ep->isochronous && dir_in) {
3996 /* The WA applies only to core versions from 2.72a
3997 * to 4.00a (including both). Also for FS_IOT_1.00a
3998 * and HS_IOT_1.00a.
3999 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004000 u32 gsnpsid = dwc2_readl(hsotg, GSNPSID);
Artur Petrosyan52953222018-04-16 08:45:31 -04004001
4002 if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
4003 gsnpsid <= DWC2_CORE_REV_4_00a) ||
4004 gsnpsid == DWC2_FS_IOT_REV_1_00a ||
4005 gsnpsid == DWC2_HS_IOT_REV_1_00a)
4006 epctrl |= DXEPCTL_CNAK;
4007 }
4008
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004009 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
4010 __func__, epctrl);
4011
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004012 dwc2_writel(hsotg, epctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004013 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004014 __func__, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004015
4016 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004017 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004018
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004019error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02004020 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004021
4022error2:
4023 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08004024 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004025 sizeof(struct dwc2_dma_desc),
4026 hs_ep->desc_list, hs_ep->desc_list_dma);
4027 hs_ep->desc_list = NULL;
4028 }
4029
Julia Lawall19c190f2010-03-29 17:36:44 +02004030 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004031}
4032
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004033/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004034 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004035 * @ep: The endpoint to disable.
4036 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004037static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004038{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004039 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004040 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004041 int dir_in = hs_ep->dir_in;
4042 int index = hs_ep->index;
4043 unsigned long flags;
4044 u32 epctrl_reg;
4045 u32 ctrl;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04004046 int locked;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004047
Marek Szyprowski1e011292014-09-09 10:44:54 +02004048 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004049
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004050 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004051 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4052 return -EINVAL;
4053 }
4054
John Stultz9b4810922017-10-23 14:32:49 -07004055 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4056 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4057 return -EINVAL;
4058 }
4059
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02004060 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004061
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04004062 locked = spin_is_locked(&hsotg->lock);
4063 if (!locked)
4064 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004065
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004066 ctrl = dwc2_readl(hsotg, epctrl_reg);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08004067
4068 if (ctrl & DXEPCTL_EPENA)
4069 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4070
Dinh Nguyen47a16852014-04-14 14:13:34 -07004071 ctrl &= ~DXEPCTL_EPENA;
4072 ctrl &= ~DXEPCTL_USBACTEP;
4073 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004074
4075 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004076 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004077
4078 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004079 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004080
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01004081 /* terminate all requests with shutdown */
4082 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4083
Robert Baldyga1c07b202016-08-29 13:39:00 -07004084 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4085 hs_ep->fifo_index = 0;
4086 hs_ep->fifo_size = 0;
4087
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04004088 if (!locked)
4089 spin_unlock_irqrestore(&hsotg->lock, flags);
4090
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004091 return 0;
4092}
4093
4094/**
4095 * on_list - check request is on the given endpoint
4096 * @ep: The endpoint to check.
4097 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004098 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004099static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004100{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004101 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004102
4103 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4104 if (req == test)
4105 return true;
4106 }
4107
4108 return false;
4109}
4110
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004111/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004112 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004113 * @ep: The endpoint to dequeue.
4114 * @req: The request to be removed from a queue.
4115 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004116static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004117{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004118 struct dwc2_hsotg_req *hs_req = our_req(req);
4119 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004120 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004121 unsigned long flags;
4122
Marek Szyprowski1e011292014-09-09 10:44:54 +02004123 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004124
Lukasz Majewski22258f42012-06-14 10:02:24 +02004125 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004126
4127 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004128 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004129 return -EINVAL;
4130 }
4131
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004132 /* Dequeue already started request */
4133 if (req == &hs_ep->req->req)
4134 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4135
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004136 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004137 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004138
4139 return 0;
4140}
4141
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004142/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004143 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004144 * @ep: The endpoint to set halt.
4145 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004146 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4147 * the endpoint is busy processing requests.
4148 *
4149 * We need to stall the endpoint immediately if request comes from set_feature
4150 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004151 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004152static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004153{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004154 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004155 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004156 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004157 u32 epreg;
4158 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004159 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004160
4161 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4162
Robert Baldygac9f721b2014-01-14 08:36:00 +01004163 if (index == 0) {
4164 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004165 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004166 else
4167 dev_warn(hs->dev,
4168 "%s: can't clear halt on ep0\n", __func__);
4169 return 0;
4170 }
4171
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004172 if (hs_ep->isochronous) {
4173 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4174 return -EINVAL;
4175 }
4176
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004177 if (!now && value && !list_empty(&hs_ep->queue)) {
4178 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4179 ep->name);
4180 return -EAGAIN;
4181 }
4182
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004183 if (hs_ep->dir_in) {
4184 epreg = DIEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004185 epctl = dwc2_readl(hs, epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004186
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004187 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004188 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004189 if (epctl & DXEPCTL_EPENA)
4190 epctl |= DXEPCTL_EPDIS;
4191 } else {
4192 epctl &= ~DXEPCTL_STALL;
4193 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4194 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004195 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004196 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004197 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004198 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004199 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004200 epreg = DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004201 epctl = dwc2_readl(hs, epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004202
John Youn34c0887f2017-01-17 20:31:43 -08004203 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004204 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004205 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004206 epctl &= ~DXEPCTL_STALL;
4207 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4208 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004209 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004210 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004211 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004212 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004213 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004214
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004215 hs_ep->halted = value;
4216
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004217 return 0;
4218}
4219
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004220/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004221 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004222 * @ep: The endpoint to set halt.
4223 * @value: Set or unset the halt.
4224 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004225static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004226{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004227 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004228 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004229 unsigned long flags = 0;
4230 int ret = 0;
4231
4232 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004233 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004234 spin_unlock_irqrestore(&hs->lock, flags);
4235
4236 return ret;
4237}
4238
Bhumika Goyalebce5612017-08-12 17:34:55 +05304239static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004240 .enable = dwc2_hsotg_ep_enable,
4241 .disable = dwc2_hsotg_ep_disable,
4242 .alloc_request = dwc2_hsotg_ep_alloc_request,
4243 .free_request = dwc2_hsotg_ep_free_request,
4244 .queue = dwc2_hsotg_ep_queue_lock,
4245 .dequeue = dwc2_hsotg_ep_dequeue,
4246 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004247 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004248};
4249
4250/**
John Youn9da51972017-01-17 20:30:27 -08004251 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004252 * @hsotg: The driver state
4253 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004254static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004255{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004256 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004257 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004258 /* unmask subset of endpoint interrupts */
4259
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004260 dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004261 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004262 DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004263
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004264 dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004265 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004266 DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004267
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004268 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004269
4270 /* Be in disconnected state until gadget is registered */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004271 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004272
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004273 /* setup fifos */
4274
4275 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004276 dwc2_readl(hsotg, GRXFSIZ),
4277 dwc2_readl(hsotg, GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004278
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004279 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004280
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004281 /* keep other bits untouched (so e.g. forced modes are not lost) */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004282 usbcfg = dwc2_readl(hsotg, GUSBCFG);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004283 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01004284 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004285
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004286 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004287 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004288 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4289 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004290 dwc2_writel(hsotg, usbcfg, GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004291
Gregory Herrerof5090042015-01-09 13:38:47 +01004292 if (using_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004293 dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004294}
4295
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004296/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004297 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004298 * @gadget: The usb gadget state
4299 * @driver: The usb gadget driver
4300 *
4301 * Perform initialization to prepare udc device and driver
4302 * to work.
4303 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004304static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004305 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004306{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004307 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004308 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004309 int ret;
4310
4311 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004312 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004313 return -ENODEV;
4314 }
4315
4316 if (!driver) {
4317 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4318 return -EINVAL;
4319 }
4320
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004321 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004322 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004323
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004324 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004325 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4326 return -EINVAL;
4327 }
4328
4329 WARN_ON(hsotg->driver);
4330
4331 driver->driver.bus = NULL;
4332 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004333 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004334 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4335
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004336 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4337 ret = dwc2_lowlevel_hw_enable(hsotg);
4338 if (ret)
4339 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004340 }
4341
Gregory Herrerof6c01592015-01-09 13:38:41 +01004342 if (!IS_ERR_OR_NULL(hsotg->uphy))
4343 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004344
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004345 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004346 if (dwc2_hw_is_device(hsotg)) {
4347 dwc2_hsotg_init(hsotg);
4348 dwc2_hsotg_core_init_disconnected(hsotg, false);
4349 }
4350
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004351 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004352 spin_unlock_irqrestore(&hsotg->lock, flags);
4353
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004354 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004355
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004356 return 0;
4357
4358err:
4359 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004360 return ret;
4361}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004362
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004363/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004364 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004365 * @gadget: The usb gadget state
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004366 *
4367 * Stop udc hw block and stay tunned for future transmissions
4368 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004369static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004370{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004371 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004372 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004373 int ep;
4374
4375 if (!hsotg)
4376 return -ENODEV;
4377
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004378 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004379 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4380 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004381 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004382 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004383 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004384 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004385
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004386 spin_lock_irqsave(&hsotg->lock, flags);
4387
Marek Szyprowski32805c32014-10-20 12:45:33 +02004388 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004389 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004390 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004391
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004392 spin_unlock_irqrestore(&hsotg->lock, flags);
4393
Gregory Herrerof6c01592015-01-09 13:38:41 +01004394 if (!IS_ERR_OR_NULL(hsotg->uphy))
4395 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004396
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004397 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4398 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004399
4400 return 0;
4401}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004402
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004403/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004404 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004405 * @gadget: The usb gadget state
4406 *
4407 * Read the {micro} frame number
4408 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004409static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004410{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004411 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004412}
4413
Lukasz Majewskia188b682012-06-22 09:29:56 +02004414/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004415 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004416 * @gadget: The usb gadget state
4417 * @is_on: Current state of the USB PHY
4418 *
4419 * Connect/Disconnect the USB PHY pullup
4420 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004421static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004422{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004423 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004424 unsigned long flags = 0;
4425
Gregory Herrero77ba9112015-09-29 12:08:19 +02004426 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004427 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004428
4429 /* Don't modify pullup state while in host mode */
4430 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4431 hsotg->enabled = is_on;
4432 return 0;
4433 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004434
4435 spin_lock_irqsave(&hsotg->lock, flags);
4436 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004437 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004438 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004439 /* Enable ACG feature in device mode,if supported */
4440 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004441 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004442 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004443 dwc2_hsotg_core_disconnect(hsotg);
4444 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004445 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004446 }
4447
4448 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4449 spin_unlock_irqrestore(&hsotg->lock, flags);
4450
4451 return 0;
4452}
4453
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004454static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004455{
4456 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4457 unsigned long flags;
4458
4459 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4460 spin_lock_irqsave(&hsotg->lock, flags);
4461
Gregory Herrero61f72232015-09-29 12:08:28 +02004462 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004463 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004464 * before being initialized / de-initialized
4465 */
4466 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004467 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004468
Gregory Herrero83d98222015-01-09 13:39:02 +01004469 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004470 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004471
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004472 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004473 if (hsotg->enabled) {
4474 /* Enable ACG feature in device mode,if supported */
4475 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004476 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004477 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004478 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004479 dwc2_hsotg_core_disconnect(hsotg);
4480 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004481 }
4482
4483 spin_unlock_irqrestore(&hsotg->lock, flags);
4484 return 0;
4485}
4486
Gregory Herrero596d6962015-01-09 13:39:08 +01004487/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004488 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004489 * @gadget: The usb gadget state
4490 * @mA: Amount of current
4491 *
4492 * Report how much power the device may consume to the phy.
4493 */
John Youn9da51972017-01-17 20:30:27 -08004494static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004495{
4496 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4497
4498 if (IS_ERR_OR_NULL(hsotg->uphy))
4499 return -ENOTSUPP;
4500 return usb_phy_set_power(hsotg->uphy, mA);
4501}
4502
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004503static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4504 .get_frame = dwc2_hsotg_gadget_getframe,
4505 .udc_start = dwc2_hsotg_udc_start,
4506 .udc_stop = dwc2_hsotg_udc_stop,
4507 .pullup = dwc2_hsotg_pullup,
4508 .vbus_session = dwc2_hsotg_vbus_session,
4509 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004510};
4511
4512/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004513 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004514 * @hsotg: The device state.
4515 * @hs_ep: The endpoint to be initialised.
4516 * @epnum: The endpoint number
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004517 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004518 *
4519 * Initialise the given endpoint (as part of the probe and device state
4520 * creation) to give to the gadget driver. Setup the endpoint name, any
4521 * direction information and other state that may be required.
4522 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004523static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004524 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004525 int epnum,
4526 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004527{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004528 char *dir;
4529
4530 if (epnum == 0)
4531 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004532 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004533 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004534 else
4535 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004536
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004537 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004538 hs_ep->index = epnum;
4539
4540 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4541
4542 INIT_LIST_HEAD(&hs_ep->queue);
4543 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4544
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004545 /* add to the list of endpoints known by the gadget driver */
4546 if (epnum)
4547 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4548
4549 hs_ep->parent = hsotg;
4550 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004551
4552 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4553 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4554 else
4555 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4556 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004557 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004558
Robert Baldyga29545222015-07-31 16:00:18 +02004559 if (epnum == 0) {
4560 hs_ep->ep.caps.type_control = true;
4561 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004562 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4563 hs_ep->ep.caps.type_iso = true;
4564 hs_ep->ep.caps.type_bulk = true;
4565 }
Robert Baldyga29545222015-07-31 16:00:18 +02004566 hs_ep->ep.caps.type_int = true;
4567 }
4568
4569 if (dir_in)
4570 hs_ep->ep.caps.dir_in = true;
4571 else
4572 hs_ep->ep.caps.dir_out = true;
4573
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004574 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004575 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004576 * to be something valid.
4577 */
4578
4579 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004580 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004581
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004582 if (dir_in)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004583 dwc2_writel(hsotg, next, DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004584 else
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004585 dwc2_writel(hsotg, next, DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004586 }
4587}
4588
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004589/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004590 * dwc2_hsotg_hw_cfg - read HW configuration registers
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004591 * @hsotg: Programming view of the DWC_otg controller
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004592 *
4593 * Read the USB core HW configuration registers
4594 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004595static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004596{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004597 u32 cfg;
4598 u32 ep_type;
4599 u32 i;
4600
Ben Dooks10aebc72010-07-19 09:40:44 +01004601 /* check hardware configuration */
4602
John Youn43e90342015-12-17 11:17:45 -08004603 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4604
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004605 /* Add ep0 */
4606 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004607
John Younb98866c2017-01-17 20:31:58 -08004608 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4609 sizeof(struct dwc2_hsotg_ep),
4610 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004611 if (!hsotg->eps_in[0])
4612 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004613 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004614 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004615
John Youn43e90342015-12-17 11:17:45 -08004616 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004617 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004618 ep_type = cfg & 3;
4619 /* Direction in or both */
4620 if (!(ep_type & 2)) {
4621 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004622 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004623 if (!hsotg->eps_in[i])
4624 return -ENOMEM;
4625 }
4626 /* Direction out or both */
4627 if (!(ep_type & 1)) {
4628 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004629 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004630 if (!hsotg->eps_out[i])
4631 return -ENOMEM;
4632 }
4633 }
4634
John Youn43e90342015-12-17 11:17:45 -08004635 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4636 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004637
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004638 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4639 hsotg->num_of_eps,
4640 hsotg->dedicated_fifos ? "dedicated" : "shared",
4641 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004642 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004643}
4644
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004645/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004646 * dwc2_hsotg_dump - dump state of the udc
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004647 * @hsotg: Programming view of the DWC_otg controller
4648 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004649 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004650static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004651{
Mark Brown83a01802011-06-01 17:16:15 +01004652#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004653 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004654 u32 val;
4655 int idx;
4656
4657 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004658 dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL),
4659 dwc2_readl(hsotg, DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004660
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004661 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004662 dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004663
4664 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004665 dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004666
4667 /* show periodic fifo settings */
4668
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004669 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004670 val = dwc2_readl(hsotg, DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004671 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004672 val >> FIFOSIZE_DEPTH_SHIFT,
4673 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004674 }
4675
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004676 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004677 dev_info(dev,
4678 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004679 dwc2_readl(hsotg, DIEPCTL(idx)),
4680 dwc2_readl(hsotg, DIEPTSIZ(idx)),
4681 dwc2_readl(hsotg, DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004682
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004683 val = dwc2_readl(hsotg, DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004684 dev_info(dev,
4685 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004686 idx, dwc2_readl(hsotg, DOEPCTL(idx)),
4687 dwc2_readl(hsotg, DOEPTSIZ(idx)),
4688 dwc2_readl(hsotg, DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004689 }
4690
4691 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004692 dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004693#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004694}
4695
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004696/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004697 * dwc2_gadget_init - init function for gadget
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004698 * @hsotg: Programming view of the DWC_otg controller
4699 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004700 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004701int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004702{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004703 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004704 int epnum;
4705 int ret;
John Youn43e90342015-12-17 11:17:45 -08004706
Gregory Herrero0a176272015-01-09 13:38:52 +01004707 /* Dump fifo information */
4708 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004709 hsotg->params.g_np_tx_fifo_size);
4710 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004711
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004712 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004713 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004714 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004715 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004716
4717 if (hsotg->params.lpm)
4718 hsotg->gadget.lpm_capable = true;
4719
Gregory Herrero097ee662015-04-29 22:09:10 +02004720 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4721 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004722 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4723 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004724
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004725 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004726 if (ret) {
4727 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004728 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004729 }
4730
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004731 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4732 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004733 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004734 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004735
4736 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4737 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004738 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004739 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004740
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004741 if (using_desc_dma(hsotg)) {
4742 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4743 if (ret < 0)
4744 return ret;
4745 }
4746
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004747 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4748 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004749 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004750 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004751 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004752 }
4753
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004754 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4755
4756 if (hsotg->num_of_eps == 0) {
4757 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004758 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004759 }
4760
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004761 /* setup endpoint information */
4762
4763 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004764 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004765
4766 /* allocate EP0 request */
4767
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004768 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004769 GFP_KERNEL);
4770 if (!hsotg->ctrl_req) {
4771 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004772 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004773 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004774
4775 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004776 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4777 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004778 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004779 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004780 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004781 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004782 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004783 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004784
Dinh Nguyen117777b2014-11-11 11:13:34 -06004785 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004786 if (ret) {
4787 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep,
4788 hsotg->ctrl_req);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004789 return ret;
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004790 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004791 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004792
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004793 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004794}
4795
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004796/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004797 * dwc2_hsotg_remove - remove function for hsotg driver
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004798 * @hsotg: Programming view of the DWC_otg controller
4799 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004800 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004801int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004802{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004803 usb_del_gadget_udc(&hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004804 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004805
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004806 return 0;
4807}
4808
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004809int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004810{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004811 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004812
Gregory Herrero9e779772015-04-29 22:09:07 +02004813 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004814 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004815
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004816 if (hsotg->driver) {
4817 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004818
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004819 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4820 hsotg->driver->driver.name);
4821
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004822 spin_lock_irqsave(&hsotg->lock, flags);
4823 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004824 dwc2_hsotg_core_disconnect(hsotg);
4825 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004826 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4827 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004828
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004829 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4830 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004831 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004832 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004833 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004834 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004835 }
4836
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004837 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004838}
4839
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004840int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004841{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004842 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004843
Gregory Herrero9e779772015-04-29 22:09:07 +02004844 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004845 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004846
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004847 if (hsotg->driver) {
4848 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4849 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004850
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004851 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004852 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004853 if (hsotg->enabled) {
4854 /* Enable ACG feature in device mode,if supported */
4855 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004856 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004857 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004858 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004859 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004860
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004861 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004862}
John Youn58e52ff6a2016-02-23 19:54:57 -08004863
4864/**
4865 * dwc2_backup_device_registers() - Backup controller device registers.
4866 * When suspending usb bus, registers needs to be backuped
4867 * if controller power is disabled once suspended.
4868 *
4869 * @hsotg: Programming view of the DWC_otg controller
4870 */
4871int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4872{
4873 struct dwc2_dregs_backup *dr;
4874 int i;
4875
4876 dev_dbg(hsotg->dev, "%s\n", __func__);
4877
4878 /* Backup dev regs */
4879 dr = &hsotg->dr_backup;
4880
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004881 dr->dcfg = dwc2_readl(hsotg, DCFG);
4882 dr->dctl = dwc2_readl(hsotg, DCTL);
4883 dr->daintmsk = dwc2_readl(hsotg, DAINTMSK);
4884 dr->diepmsk = dwc2_readl(hsotg, DIEPMSK);
4885 dr->doepmsk = dwc2_readl(hsotg, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08004886
4887 for (i = 0; i < hsotg->num_of_eps; i++) {
4888 /* Backup IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004889 dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004890
4891 /* Ensure DATA PID is correctly configured */
4892 if (dr->diepctl[i] & DXEPCTL_DPID)
4893 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4894 else
4895 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4896
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004897 dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i));
4898 dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004899
4900 /* Backup OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004901 dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004902
4903 /* Ensure DATA PID is correctly configured */
4904 if (dr->doepctl[i] & DXEPCTL_DPID)
4905 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4906 else
4907 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4908
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004909 dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i));
4910 dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i));
4911 dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004912 }
4913 dr->valid = true;
4914 return 0;
4915}
4916
4917/**
4918 * dwc2_restore_device_registers() - Restore controller device registers.
4919 * When resuming usb bus, device registers needs to be restored
4920 * if controller power were disabled.
4921 *
4922 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004923 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
4924 *
4925 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08004926 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004927int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08004928{
4929 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08004930 int i;
4931
4932 dev_dbg(hsotg->dev, "%s\n", __func__);
4933
4934 /* Restore dev regs */
4935 dr = &hsotg->dr_backup;
4936 if (!dr->valid) {
4937 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4938 __func__);
4939 return -EINVAL;
4940 }
4941 dr->valid = false;
4942
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004943 if (!remote_wakeup)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004944 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004945
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004946 dwc2_writel(hsotg, dr->daintmsk, DAINTMSK);
4947 dwc2_writel(hsotg, dr->diepmsk, DIEPMSK);
4948 dwc2_writel(hsotg, dr->doepmsk, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08004949
4950 for (i = 0; i < hsotg->num_of_eps; i++) {
4951 /* Restore IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004952 dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i));
4953 dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i));
4954 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004955 /** WA for enabled EPx's IN in DDMA mode. On entering to
4956 * hibernation wrong value read and saved from DIEPDMAx,
4957 * as result BNA interrupt asserted on hibernation exit
4958 * by restoring from saved area.
4959 */
4960 if (hsotg->params.g_dma_desc &&
4961 (dr->diepctl[i] & DXEPCTL_EPENA))
4962 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004963 dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i));
4964 dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004965 /* Restore OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004966 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004967 /* WA for enabled EPx's OUT in DDMA mode. On entering to
4968 * hibernation wrong value read and saved from DOEPDMAx,
4969 * as result BNA interrupt asserted on hibernation exit
4970 * by restoring from saved area.
4971 */
4972 if (hsotg->params.g_dma_desc &&
4973 (dr->doepctl[i] & DXEPCTL_EPENA))
4974 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004975 dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i));
4976 dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004977 }
4978
John Youn58e52ff6a2016-02-23 19:54:57 -08004979 return 0;
4980}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04004981
4982/**
4983 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
4984 *
4985 * @hsotg: Programming view of DWC_otg controller
4986 *
4987 */
4988void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
4989{
4990 u32 val;
4991
4992 if (!hsotg->params.lpm)
4993 return;
4994
4995 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
4996 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
4997 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
4998 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
4999 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005000 dwc2_writel(hsotg, val, GLPMCFG);
5001 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG));
Sevak Arakelyan21b03402018-01-24 17:43:32 +04005002}
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005003
5004/**
5005 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
5006 *
5007 * @hsotg: Programming view of the DWC_otg controller
5008 *
5009 * Return non-zero if failed to enter to hibernation.
5010 */
5011int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
5012{
5013 u32 gpwrdn;
5014 int ret = 0;
5015
5016 /* Change to L2(suspend) state */
5017 hsotg->lx_state = DWC2_L2;
5018 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
5019 ret = dwc2_backup_global_registers(hsotg);
5020 if (ret) {
5021 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5022 __func__);
5023 return ret;
5024 }
5025 ret = dwc2_backup_device_registers(hsotg);
5026 if (ret) {
5027 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
5028 __func__);
5029 return ret;
5030 }
5031
5032 gpwrdn = GPWRDN_PWRDNRSTN;
5033 gpwrdn |= GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005034 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005035 udelay(10);
5036
5037 /* Set flag to indicate that we are in hibernation */
5038 hsotg->hibernated = 1;
5039
5040 /* Enable interrupts from wake up logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005041 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005042 gpwrdn |= GPWRDN_PMUINTSEL;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005043 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005044 udelay(10);
5045
5046 /* Unmask device mode interrupts in GPWRDN */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005047 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005048 gpwrdn |= GPWRDN_RST_DET_MSK;
5049 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5050 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005051 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005052 udelay(10);
5053
5054 /* Enable Power Down Clamp */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005055 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005056 gpwrdn |= GPWRDN_PWRDNCLMP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005057 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005058 udelay(10);
5059
5060 /* Switch off VDD */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005061 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005062 gpwrdn |= GPWRDN_PWRDNSWTCH;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005063 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005064 udelay(10);
5065
5066 /* Save gpwrdn register for further usage if stschng interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005067 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005068 dev_dbg(hsotg->dev, "Hibernation completed\n");
5069
5070 return ret;
5071}
5072
5073/**
5074 * dwc2_gadget_exit_hibernation()
5075 * This function is for exiting from Device mode hibernation by host initiated
5076 * resume/reset and device initiated remote-wakeup.
5077 *
5078 * @hsotg: Programming view of the DWC_otg controller
5079 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04005080 * @reset: indicates whether resume is initiated by Reset.
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005081 *
5082 * Return non-zero if failed to exit from hibernation.
5083 */
5084int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
5085 int rem_wakeup, int reset)
5086{
5087 u32 pcgcctl;
5088 u32 gpwrdn;
5089 u32 dctl;
5090 int ret = 0;
5091 struct dwc2_gregs_backup *gr;
5092 struct dwc2_dregs_backup *dr;
5093
5094 gr = &hsotg->gr_backup;
5095 dr = &hsotg->dr_backup;
5096
5097 if (!hsotg->hibernated) {
5098 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5099 return 1;
5100 }
5101 dev_dbg(hsotg->dev,
5102 "%s: called with rem_wakeup = %d reset = %d\n",
5103 __func__, rem_wakeup, reset);
5104
5105 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5106
5107 if (!reset) {
5108 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005109 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005110 }
5111
5112 /* De-assert Restore */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005113 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005114 gpwrdn &= ~GPWRDN_RESTORE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005115 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005116 udelay(10);
5117
5118 if (!rem_wakeup) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005119 pcgcctl = dwc2_readl(hsotg, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005120 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005121 dwc2_writel(hsotg, pcgcctl, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005122 }
5123
5124 /* Restore GUSBCFG, DCFG and DCTL */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005125 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5126 dwc2_writel(hsotg, dr->dcfg, DCFG);
5127 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005128
5129 /* De-assert Wakeup Logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005130 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005131 gpwrdn &= ~GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005132 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005133
5134 if (rem_wakeup) {
5135 udelay(10);
5136 /* Start Remote Wakeup Signaling */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005137 dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005138 } else {
5139 udelay(50);
5140 /* Set Device programming done bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005141 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005142 dctl |= DCTL_PWRONPRGDONE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005143 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005144 }
5145 /* Wait for interrupts which must be cleared */
5146 mdelay(2);
5147 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005148 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005149
5150 /* Restore global registers */
5151 ret = dwc2_restore_global_registers(hsotg);
5152 if (ret) {
5153 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5154 __func__);
5155 return ret;
5156 }
5157
5158 /* Restore device registers */
5159 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5160 if (ret) {
5161 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5162 __func__);
5163 return ret;
5164 }
5165
5166 if (rem_wakeup) {
5167 mdelay(10);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005168 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005169 dctl &= ~DCTL_RMTWKUPSIG;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005170 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005171 }
5172
5173 hsotg->hibernated = 0;
5174 hsotg->lx_state = DWC2_L0;
5175 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5176
5177 return ret;
5178}