blob: d8424834902db52c4cee4cac1053f03ffa99fdb3 [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/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500126 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100127 * @hsotg: The device state
128 * @ints: A bitmask of the interrupts to enable
129 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500130static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100131{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400132 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100133 u32 new_gsintmsk;
134
135 new_gsintmsk = gsintmsk | ints;
136
137 if (new_gsintmsk != gsintmsk) {
138 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400139 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100140 }
141}
142
143/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500144 * dwc2_hsotg_disable_gsint - disable 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_disable_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)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400156 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100157}
158
159/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500160 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100161 * @hsotg: The device state
162 * @ep: The endpoint index
163 * @dir_in: True if direction is in.
164 * @en: The enable value, true to enable
165 *
166 * Set or clear the mask for an individual endpoint's interrupt
167 * request.
168 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500169static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800170 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100171 unsigned int en)
172{
173 unsigned long flags;
174 u32 bit = 1 << ep;
175 u32 daint;
176
177 if (!dir_in)
178 bit <<= 16;
179
180 local_irq_save(flags);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400181 daint = dwc2_readl(hsotg, DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100182 if (en)
183 daint |= bit;
184 else
185 daint &= ~bit;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400186 dwc2_writel(hsotg, daint, DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100187 local_irq_restore(flags);
188}
189
190/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800191 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400192 *
193 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800194 */
195int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
196{
197 if (hsotg->hw_params.en_multiple_tx_fifo)
198 /* In dedicated FIFO mode we need count of IN EPs */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400199 return hsotg->hw_params.num_dev_in_eps;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800200 else
201 /* In shared FIFO mode we need count of Periodic IN EPs */
202 return hsotg->hw_params.num_dev_perio_in_ep;
203}
204
205/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800206 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
207 * device mode TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400208 *
209 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800210 */
211int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
212{
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800213 int addr;
214 int tx_addr_max;
215 u32 np_tx_fifo_size;
216
217 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
218 hsotg->params.g_np_tx_fifo_size);
219
220 /* Get Endpoint Info Control block size in DWORDs. */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400221 tx_addr_max = hsotg->hw_params.total_fifo_size;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800222
223 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
224 if (tx_addr_max <= addr)
225 return 0;
226
227 return tx_addr_max - addr;
228}
229
230/**
231 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
232 * TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400233 *
234 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800235 */
236int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
237{
238 int tx_fifo_count;
239 int tx_fifo_depth;
240
241 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
242
243 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
244
245 if (!tx_fifo_count)
246 return tx_fifo_depth;
247 else
248 return tx_fifo_depth / tx_fifo_count;
249}
250
251/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500252 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100253 * @hsotg: The device instance.
254 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500255static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100256{
John Youn2317eac2016-10-17 17:36:23 -0700257 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100258 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100259 int timeout;
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +0400260
Ben Dooks0f002d22010-05-25 05:36:50 +0100261 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700262 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100263
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100264 /* Reset fifo map if not correctly cleared during previous session */
265 WARN_ON(hsotg->fifo_map);
266 hsotg->fifo_map = 0;
267
Gregory Herrero0a176272015-01-09 13:38:52 +0100268 /* set RX/NPTX FIFO sizes */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400269 dwc2_writel(hsotg, hsotg->params.g_rx_fifo_size, GRXFSIZ);
270 dwc2_writel(hsotg, (hsotg->params.g_rx_fifo_size <<
271 FIFOSIZE_STARTADDR_SHIFT) |
John Youn05ee7992016-11-03 17:56:05 -0700272 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400273 GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100274
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200275 /*
276 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100277 * block have overlapping default addresses. This also ensures
278 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200279 * known values.
280 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100281
282 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700283 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100284
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200285 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100286 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200287 * them to endpoints dynamically according to maxpacket size value of
288 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200289 */
John Youn2317eac2016-10-17 17:36:23 -0700290 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700291 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700292 continue;
293 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700294 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
295 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700296 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700297 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100298
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400299 dwc2_writel(hsotg, val, DPTXFSIZN(ep));
300 val = dwc2_readl(hsotg, DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100301 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100302
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400303 dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size |
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800304 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400305 GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200306 /*
307 * according to p428 of the design guide, we need to ensure that
308 * all fifos are flushed before continuing
309 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100310
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400311 dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
312 GRSTCTL_RXFFLSH, GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100313
314 /* wait until the fifos are both flushed */
315 timeout = 100;
316 while (1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400317 val = dwc2_readl(hsotg, GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100318
Dinh Nguyen47a16852014-04-14 14:13:34 -0700319 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100320 break;
321
322 if (--timeout == 0) {
323 dev_err(hsotg->dev,
324 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
325 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100326 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100327 }
328
329 udelay(1);
330 }
331
332 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100333}
334
335/**
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400336 * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100337 * @ep: USB endpoint to allocate request for.
338 * @flags: Allocation flags
339 *
340 * Allocate a new USB request structure appropriate for the specified endpoint
341 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500342static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800343 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100344{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500345 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100346
John Younec33efe2017-01-17 20:32:41 -0800347 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100348 if (!req)
349 return NULL;
350
351 INIT_LIST_HEAD(&req->queue);
352
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100353 return &req->req;
354}
355
356/**
357 * is_ep_periodic - return true if the endpoint is in periodic mode.
358 * @hs_ep: The endpoint to query.
359 *
360 * Returns true if the endpoint is in periodic mode, meaning it is being
361 * used for an Interrupt or ISO transfer.
362 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500363static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100364{
365 return hs_ep->periodic;
366}
367
368/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500369 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100370 * @hsotg: The device state.
371 * @hs_ep: The endpoint for the request
372 * @hs_req: The request being processed.
373 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500374 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100375 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200376 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500377static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800378 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500379 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100380{
381 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800382
Jingoo Han17d966a2013-05-11 21:14:00 +0900383 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100384}
385
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800386/*
387 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
388 * for Control endpoint
389 * @hsotg: The device state.
390 *
391 * This function will allocate 4 descriptor chains for EP 0: 2 for
392 * Setup stage, per one for IN and OUT data/status transactions.
393 */
394static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
395{
396 hsotg->setup_desc[0] =
397 dmam_alloc_coherent(hsotg->dev,
398 sizeof(struct dwc2_dma_desc),
399 &hsotg->setup_desc_dma[0],
400 GFP_KERNEL);
401 if (!hsotg->setup_desc[0])
402 goto fail;
403
404 hsotg->setup_desc[1] =
405 dmam_alloc_coherent(hsotg->dev,
406 sizeof(struct dwc2_dma_desc),
407 &hsotg->setup_desc_dma[1],
408 GFP_KERNEL);
409 if (!hsotg->setup_desc[1])
410 goto fail;
411
412 hsotg->ctrl_in_desc =
413 dmam_alloc_coherent(hsotg->dev,
414 sizeof(struct dwc2_dma_desc),
415 &hsotg->ctrl_in_desc_dma,
416 GFP_KERNEL);
417 if (!hsotg->ctrl_in_desc)
418 goto fail;
419
420 hsotg->ctrl_out_desc =
421 dmam_alloc_coherent(hsotg->dev,
422 sizeof(struct dwc2_dma_desc),
423 &hsotg->ctrl_out_desc_dma,
424 GFP_KERNEL);
425 if (!hsotg->ctrl_out_desc)
426 goto fail;
427
428 return 0;
429
430fail:
431 return -ENOMEM;
432}
433
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100434/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500435 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100436 * @hsotg: The controller state.
437 * @hs_ep: The endpoint we're going to write for.
438 * @hs_req: The request to write data for.
439 *
440 * This is called when the TxFIFO has some space in it to hold a new
441 * transmission and we have something to give it. The actual setup of
442 * the data size is done elsewhere, so all we have to do is to actually
443 * write the data.
444 *
445 * The return value is zero if there is more space (or nothing was done)
446 * otherwise -ENOSPC is returned if the FIFO space was used up.
447 *
448 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200449 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500450static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800451 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500452 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100453{
454 bool periodic = is_ep_periodic(hs_ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400455 u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100456 int buf_pos = hs_req->req.actual;
457 int to_write = hs_ep->size_loaded;
458 void *data;
459 int can_write;
460 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200461 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100462
463 to_write -= (buf_pos - hs_ep->last_load);
464
465 /* if there's nothing to write, get out early */
466 if (to_write == 0)
467 return 0;
468
Ben Dooks10aebc72010-07-19 09:40:44 +0100469 if (periodic && !hsotg->dedicated_fifos) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400470 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100471 int size_left;
472 int size_done;
473
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200474 /*
475 * work out how much data was loaded so we can calculate
476 * how much data is left in the fifo.
477 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100478
Dinh Nguyen47a16852014-04-14 14:13:34 -0700479 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100480
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200481 /*
482 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100483 * previous data has been completely sent.
484 */
485 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500486 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100487 return -ENOSPC;
488 }
489
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100490 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
491 __func__, size_left,
492 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
493
494 /* how much of the data has moved */
495 size_done = hs_ep->size_loaded - size_left;
496
497 /* how much data is left in the fifo */
498 can_write = hs_ep->fifo_load - size_done;
499 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
500 __func__, can_write);
501
502 can_write = hs_ep->fifo_size - can_write;
503 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
504 __func__, can_write);
505
506 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500507 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100508 return -ENOSPC;
509 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100510 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400511 can_write = dwc2_readl(hsotg,
512 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100513
514 can_write &= 0xffff;
515 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100516 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700517 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100518 dev_dbg(hsotg->dev,
519 "%s: no queue slots available (0x%08x)\n",
520 __func__, gnptxsts);
521
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500522 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100523 return -ENOSPC;
524 }
525
Dinh Nguyen47a16852014-04-14 14:13:34 -0700526 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100527 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100528 }
529
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200530 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
531
532 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800533 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100534
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200535 /*
536 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100537 * FIFO, requests of >512 cause the endpoint to get stuck with a
538 * fragment of the end of the transfer in it.
539 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200540 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100541 can_write = 512;
542
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200543 /*
544 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100545 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200546 * doing it.
547 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200548 if (to_write > max_transfer) {
549 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100550
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200551 /* it's needed only when we do not use dedicated fifos */
552 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500553 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800554 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700555 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100556 }
557
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100558 /* see if we can write data */
559
560 if (to_write > can_write) {
561 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200562 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100563
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200564 /*
565 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100566 * exact number of packets.
567 *
568 * Note, we do not currently check to see if we can ever
569 * write a full packet or not to the FIFO.
570 */
571
572 if (pkt_round)
573 to_write -= pkt_round;
574
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200575 /*
576 * enable correct FIFO interrupt to alert us when there
577 * is more room left.
578 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100579
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200580 /* it's needed only when we do not use dedicated fifos */
581 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500582 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800583 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700584 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100585 }
586
587 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800588 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100589
590 if (to_write <= 0)
591 return -ENOSPC;
592
593 hs_req->req.actual = buf_pos + to_write;
594 hs_ep->total_data += to_write;
595
596 if (periodic)
597 hs_ep->fifo_load += to_write;
598
599 to_write = DIV_ROUND_UP(to_write, 4);
600 data = hs_req->req.buf + buf_pos;
601
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +0400602 dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100603
604 return (to_write >= can_write) ? -ENOSPC : 0;
605}
606
607/**
608 * get_ep_limit - get the maximum data legnth for this endpoint
609 * @hs_ep: The endpoint
610 *
611 * Return the maximum data that can be queued in one go on a given endpoint
612 * so that transfers that are too long can be split.
613 */
John Youn9da51972017-01-17 20:30:27 -0800614static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100615{
616 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800617 unsigned int maxsize;
618 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100619
620 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700621 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
622 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100623 } else {
John Youn9da51972017-01-17 20:30:27 -0800624 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900625 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700626 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900627 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100628 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100629 }
630
631 /* we made the constant loading easier above by using +1 */
632 maxpkt--;
633 maxsize--;
634
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200635 /*
636 * constrain by packet count if maxpkts*pktsize is greater
637 * than the length register size.
638 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100639
640 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
641 maxsize = maxpkt * hs_ep->ep.maxpacket;
642
643 return maxsize;
644}
645
646/**
John Youn38beaec2017-01-17 20:31:13 -0800647 * dwc2_hsotg_read_frameno - read current frame number
648 * @hsotg: The device instance
649 *
650 * Return the current frame number
651 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700652static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
653{
654 u32 dsts;
655
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400656 dsts = dwc2_readl(hsotg, DSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700657 dsts &= DSTS_SOFFN_MASK;
658 dsts >>= DSTS_SOFFN_SHIFT;
659
660 return dsts;
661}
662
663/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800664 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
665 * DMA descriptor chain prepared for specific endpoint
666 * @hs_ep: The endpoint
667 *
668 * Return the maximum data that can be queued in one go on a given endpoint
669 * depending on its descriptor chain capacity so that transfers that
670 * are too long can be split.
671 */
672static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
673{
674 int is_isoc = hs_ep->isochronous;
675 unsigned int maxsize;
676
677 if (is_isoc)
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +0400678 maxsize = (hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
679 DEV_DMA_ISOC_RX_NBYTES_LIMIT) *
680 MAX_DMA_DESC_NUM_HS_ISOC;
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800681 else
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +0400682 maxsize = DEV_DMA_NBYTES_LIMIT * MAX_DMA_DESC_NUM_GENERIC;
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800683
684 return maxsize;
685}
686
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800687/*
688 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
689 * @hs_ep: The endpoint
690 * @mask: RX/TX bytes mask to be defined
691 *
692 * Returns maximum data payload for one descriptor after analyzing endpoint
693 * characteristics.
694 * DMA descriptor transfer bytes limit depends on EP type:
695 * Control out - MPS,
696 * Isochronous - descriptor rx/tx bytes bitfield limit,
697 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
698 * have concatenations from various descriptors within one packet.
699 *
700 * Selects corresponding mask for RX/TX bytes as well.
701 */
702static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
703{
704 u32 mps = hs_ep->ep.maxpacket;
705 int dir_in = hs_ep->dir_in;
706 u32 desc_size = 0;
707
708 if (!hs_ep->index && !dir_in) {
709 desc_size = mps;
710 *mask = DEV_DMA_NBYTES_MASK;
711 } else if (hs_ep->isochronous) {
712 if (dir_in) {
713 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
714 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
715 } else {
716 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
717 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
718 }
719 } else {
720 desc_size = DEV_DMA_NBYTES_LIMIT;
721 *mask = DEV_DMA_NBYTES_MASK;
722
723 /* Round down desc_size to be mps multiple */
724 desc_size -= desc_size % mps;
725 }
726
727 return desc_size;
728}
729
730/*
731 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
732 * @hs_ep: The endpoint
733 * @dma_buff: DMA address to use
734 * @len: Length of the transfer
735 *
736 * This function will iterate over descriptor chain and fill its entries
737 * with corresponding information based on transfer data.
738 */
739static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
740 dma_addr_t dma_buff,
741 unsigned int len)
742{
743 struct dwc2_hsotg *hsotg = hs_ep->parent;
744 int dir_in = hs_ep->dir_in;
745 struct dwc2_dma_desc *desc = hs_ep->desc_list;
746 u32 mps = hs_ep->ep.maxpacket;
747 u32 maxsize = 0;
748 u32 offset = 0;
749 u32 mask = 0;
750 int i;
751
752 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
753
754 hs_ep->desc_count = (len / maxsize) +
755 ((len % maxsize) ? 1 : 0);
756 if (len == 0)
757 hs_ep->desc_count = 1;
758
759 for (i = 0; i < hs_ep->desc_count; ++i) {
760 desc->status = 0;
761 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
762 << DEV_DMA_BUFF_STS_SHIFT);
763
764 if (len > maxsize) {
765 if (!hs_ep->index && !dir_in)
766 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
767
768 desc->status |= (maxsize <<
769 DEV_DMA_NBYTES_SHIFT & mask);
770 desc->buf = dma_buff + offset;
771
772 len -= maxsize;
773 offset += maxsize;
774 } else {
775 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
776
777 if (dir_in)
778 desc->status |= (len % mps) ? DEV_DMA_SHORT :
779 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
780 if (len > maxsize)
781 dev_err(hsotg->dev, "wrong len %d\n", len);
782
783 desc->status |=
784 len << DEV_DMA_NBYTES_SHIFT & mask;
785 desc->buf = dma_buff + offset;
786 }
787
788 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
789 desc->status |= (DEV_DMA_BUFF_STS_HREADY
790 << DEV_DMA_BUFF_STS_SHIFT);
791 desc++;
792 }
793}
794
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800795/*
796 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
797 * @hs_ep: The isochronous endpoint.
798 * @dma_buff: usb requests dma buffer.
799 * @len: usb request transfer length.
800 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400801 * Fills next free descriptor with the data of the arrived usb request,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800802 * frame info, sets Last and IOC bits increments next_desc. If filled
803 * descriptor is not the first one, removes L bit from the previous descriptor
804 * status.
805 */
806static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
807 dma_addr_t dma_buff, unsigned int len)
808{
809 struct dwc2_dma_desc *desc;
810 struct dwc2_hsotg *hsotg = hs_ep->parent;
811 u32 index;
812 u32 maxsize = 0;
813 u32 mask = 0;
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400814 u8 pid = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800815
816 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800817
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400818 index = hs_ep->next_desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800819 desc = &hs_ep->desc_list[index];
820
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400821 /* Check if descriptor chain full */
822 if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
823 DEV_DMA_BUFF_STS_HREADY) {
824 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
825 return 1;
826 }
827
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800828 /* Clear L bit of previous desc if more than one entries in the chain */
829 if (hs_ep->next_desc)
830 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
831
832 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
833 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
834
835 desc->status = 0;
836 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
837
838 desc->buf = dma_buff;
839 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
840 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
841
842 if (hs_ep->dir_in) {
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400843 if (len)
844 pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
845 else
846 pid = 1;
847 desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800848 DEV_DMA_ISOC_PID_MASK) |
849 ((len % hs_ep->ep.maxpacket) ?
850 DEV_DMA_SHORT : 0) |
851 ((hs_ep->target_frame <<
852 DEV_DMA_ISOC_FRNUM_SHIFT) &
853 DEV_DMA_ISOC_FRNUM_MASK);
854 }
855
856 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
857 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
858
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400859 /* Increment frame number by interval for IN */
860 if (hs_ep->dir_in)
861 dwc2_gadget_incr_frame_num(hs_ep);
862
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800863 /* Update index of last configured entry in the chain */
864 hs_ep->next_desc++;
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +0400865 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_HS_ISOC)
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400866 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800867
868 return 0;
869}
870
871/*
872 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
873 * @hs_ep: The isochronous endpoint.
874 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400875 * Prepare descriptor chain for isochronous endpoints. Afterwards
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800876 * write DMA address to HW and enable the endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800877 */
878static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
879{
880 struct dwc2_hsotg *hsotg = hs_ep->parent;
881 struct dwc2_hsotg_req *hs_req, *treq;
882 int index = hs_ep->index;
883 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400884 int i;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800885 u32 dma_reg;
886 u32 depctl;
887 u32 ctrl;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400888 struct dwc2_dma_desc *desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800889
890 if (list_empty(&hs_ep->queue)) {
Minas Harutyunyan1ffba902018-06-12 12:37:29 +0400891 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800892 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
893 return;
894 }
895
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400896 /* Initialize descriptor chain by Host Busy status */
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +0400897 for (i = 0; i < MAX_DMA_DESC_NUM_HS_ISOC; i++) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400898 desc = &hs_ep->desc_list[i];
899 desc->status = 0;
900 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
901 << DEV_DMA_BUFF_STS_SHIFT);
902 }
903
904 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800905 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
906 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
907 hs_req->req.length);
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400908 if (ret)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800909 break;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800910 }
911
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400912 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800913 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
914 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
915
916 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400917 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800918
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400919 ctrl = dwc2_readl(hsotg, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800920 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400921 dwc2_writel(hsotg, ctrl, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800922}
923
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800924/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500925 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100926 * @hsotg: The controller state.
927 * @hs_ep: The endpoint to process a request for
928 * @hs_req: The request to start.
929 * @continuing: True if we are doing more for the current request.
930 *
931 * Start the given request running by setting the endpoint registers
932 * appropriately, and writing any data to the FIFOs.
933 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500934static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800935 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500936 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100937 bool continuing)
938{
939 struct usb_request *ureq = &hs_req->req;
940 int index = hs_ep->index;
941 int dir_in = hs_ep->dir_in;
942 u32 epctrl_reg;
943 u32 epsize_reg;
944 u32 epsize;
945 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -0800946 unsigned int length;
947 unsigned int packets;
948 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800949 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100950
951 if (index != 0) {
952 if (hs_ep->req && !continuing) {
953 dev_err(hsotg->dev, "%s: active request\n", __func__);
954 WARN_ON(1);
955 return;
956 } else if (hs_ep->req != hs_req && continuing) {
957 dev_err(hsotg->dev,
958 "%s: continue different req\n", __func__);
959 WARN_ON(1);
960 return;
961 }
962 }
963
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800964 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200965 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
966 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100967
968 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400969 __func__, dwc2_readl(hsotg, epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100970 hs_ep->dir_in ? "in" : "out");
971
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900972 /* If endpoint is stalled, we will restart request later */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400973 ctrl = dwc2_readl(hsotg, epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900974
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200975 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900976 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
977 return;
978 }
979
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100980 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200981 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
982 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100983
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800984 if (!using_desc_dma(hsotg))
985 maxreq = get_ep_limit(hs_ep);
986 else
987 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
988
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100989 if (length > maxreq) {
990 int round = maxreq % hs_ep->ep.maxpacket;
991
992 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
993 __func__, length, maxreq, round);
994
995 /* round down to multiple of packets */
996 if (round)
997 maxreq -= round;
998
999 length = maxreq;
1000 }
1001
1002 if (length)
1003 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
1004 else
1005 packets = 1; /* send one packet if length is zero. */
1006
1007 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001008 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001009 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001010 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001011 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001012 else
1013 epsize = 0;
1014
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001015 /*
1016 * zero length packet should be programmed on its own and should not
1017 * be counted in DIEPTSIZ.PktCnt with other packets.
1018 */
1019 if (dir_in && ureq->zero && !continuing) {
1020 /* Test if zlp is actually required. */
1021 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001022 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001023 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001024 }
1025
Dinh Nguyen47a16852014-04-14 14:13:34 -07001026 epsize |= DXEPTSIZ_PKTCNT(packets);
1027 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001028
1029 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1030 __func__, packets, length, ureq->length, epsize, epsize_reg);
1031
1032 /* store the request as the current one we're doing */
1033 hs_ep->req = hs_req;
1034
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001035 if (using_desc_dma(hsotg)) {
1036 u32 offset = 0;
1037 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001038
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001039 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1040 if (!dir_in) {
1041 if (!index)
1042 length = mps;
1043 else if (length % mps)
1044 length += (mps - (length % mps));
1045 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001046
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001047 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001048 * If more data to send, adjust DMA for EP0 out data stage.
1049 * ureq->dma stays unchanged, hence increment it by already
1050 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001051 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001052 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1053 continuing)
1054 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001055
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001056 /* Fill DDMA chain entries */
1057 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1058 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001059
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001060 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001061 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001062
1063 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1064 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1065 } else {
1066 /* write size / packets */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001067 dwc2_writel(hsotg, epsize, epsize_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001068
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001069 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001070 /*
1071 * write DMA address to control register, buffer
1072 * already synced by dwc2_hsotg_ep_queue().
1073 */
1074
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001075 dwc2_writel(hsotg, ureq->dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001076
1077 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1078 __func__, &ureq->dma, dma_reg);
1079 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001080 }
1081
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001082 if (hs_ep->isochronous && hs_ep->interval == 1) {
1083 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1084 dwc2_gadget_incr_frame_num(hs_ep);
1085
1086 if (hs_ep->target_frame & 0x1)
1087 ctrl |= DXEPCTL_SETODDFR;
1088 else
1089 ctrl |= DXEPCTL_SETEVENFR;
1090 }
1091
Dinh Nguyen47a16852014-04-14 14:13:34 -07001092 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001093
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001094 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001095
1096 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001097 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001098 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001099
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001100 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001101 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001102
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001103 /*
1104 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001105 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001106 * this information.
1107 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001108 hs_ep->size_loaded = length;
1109 hs_ep->last_load = ureq->actual;
1110
1111 if (dir_in && !using_dma(hsotg)) {
1112 /* set these anyway, we may need them for non-periodic in */
1113 hs_ep->fifo_load = 0;
1114
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001115 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001116 }
1117
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001118 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001119 * Note, trying to clear the NAK here causes problems with transmit
1120 * on the S3C6400 ending up with the TXFIFO becoming full.
1121 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122
1123 /* check ep is enabled */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001124 if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001125 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001126 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001127 index, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001128
Dinh Nguyen47a16852014-04-14 14:13:34 -07001129 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001130 __func__, dwc2_readl(hsotg, epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001131
1132 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001133 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001134}
1135
1136/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001137 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138 * @hsotg: The device state.
1139 * @hs_ep: The endpoint the request is on.
1140 * @req: The request being processed.
1141 *
1142 * We've been asked to queue a request, so ensure that the memory buffer
1143 * is correctly setup for DMA. If we've been passed an extant DMA address
1144 * then ensure the buffer has been synced to memory. If our buffer has no
1145 * DMA memory, then we map the memory and mark our request to allow us to
1146 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001147 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001148static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001149 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001150 struct usb_request *req)
1151{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001152 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001153
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001154 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1155 if (ret)
1156 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001157
1158 return 0;
1159
1160dma_error:
1161 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1162 __func__, req->buf, req->length);
1163
1164 return -EIO;
1165}
1166
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001167static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001168 struct dwc2_hsotg_ep *hs_ep,
1169 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001170{
1171 void *req_buf = hs_req->req.buf;
1172
1173 /* If dma is not being used or buffer is aligned */
1174 if (!using_dma(hsotg) || !((long)req_buf & 3))
1175 return 0;
1176
1177 WARN_ON(hs_req->saved_req_buf);
1178
1179 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001180 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001181
1182 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1183 if (!hs_req->req.buf) {
1184 hs_req->req.buf = req_buf;
1185 dev_err(hsotg->dev,
1186 "%s: unable to allocate memory for bounce buffer\n",
1187 __func__);
1188 return -ENOMEM;
1189 }
1190
1191 /* Save actual buffer */
1192 hs_req->saved_req_buf = req_buf;
1193
1194 if (hs_ep->dir_in)
1195 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1196 return 0;
1197}
1198
John Younb98866c2017-01-17 20:31:58 -08001199static void
1200dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1201 struct dwc2_hsotg_ep *hs_ep,
1202 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001203{
1204 /* If dma is not being used or buffer was aligned */
1205 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1206 return;
1207
1208 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1209 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1210
1211 /* Copy data from bounce buffer on successful out transfer */
1212 if (!hs_ep->dir_in && !hs_req->req.status)
1213 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001214 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001215
1216 /* Free bounce buffer */
1217 kfree(hs_req->req.buf);
1218
1219 hs_req->req.buf = hs_req->saved_req_buf;
1220 hs_req->saved_req_buf = NULL;
1221}
1222
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001223/**
1224 * dwc2_gadget_target_frame_elapsed - Checks target frame
1225 * @hs_ep: The driver endpoint to check
1226 *
1227 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1228 * corresponding transfer.
1229 */
1230static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1231{
1232 struct dwc2_hsotg *hsotg = hs_ep->parent;
1233 u32 target_frame = hs_ep->target_frame;
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001234 u32 current_frame = hsotg->frame_number;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001235 bool frame_overrun = hs_ep->frame_overrun;
1236
1237 if (!frame_overrun && current_frame >= target_frame)
1238 return true;
1239
1240 if (frame_overrun && current_frame >= target_frame &&
1241 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1242 return true;
1243
1244 return false;
1245}
1246
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001247/*
1248 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1249 * @hsotg: The driver state
1250 * @hs_ep: the ep descriptor chain is for
1251 *
1252 * Called to update EP0 structure's pointers depend on stage of
1253 * control transfer.
1254 */
1255static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1256 struct dwc2_hsotg_ep *hs_ep)
1257{
1258 switch (hsotg->ep0_state) {
1259 case DWC2_EP0_SETUP:
1260 case DWC2_EP0_STATUS_OUT:
1261 hs_ep->desc_list = hsotg->setup_desc[0];
1262 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1263 break;
1264 case DWC2_EP0_DATA_IN:
1265 case DWC2_EP0_STATUS_IN:
1266 hs_ep->desc_list = hsotg->ctrl_in_desc;
1267 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1268 break;
1269 case DWC2_EP0_DATA_OUT:
1270 hs_ep->desc_list = hsotg->ctrl_out_desc;
1271 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1272 break;
1273 default:
1274 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1275 hsotg->ep0_state);
1276 return -EINVAL;
1277 }
1278
1279 return 0;
1280}
1281
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001282static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001283 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001284{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001285 struct dwc2_hsotg_req *hs_req = our_req(req);
1286 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001287 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001288 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001289 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001290 u32 maxsize = 0;
1291 u32 mask = 0;
1292
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001293
1294 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1295 ep->name, req, req->length, req->buf, req->no_interrupt,
1296 req->zero, req->short_not_ok);
1297
Gregory Herrero7ababa92015-04-29 22:09:08 +02001298 /* Prevent new request submission when controller is suspended */
Grigor Tovmasyan88b02f22018-01-24 17:44:25 +04001299 if (hs->lx_state != DWC2_L0) {
1300 dev_dbg(hs->dev, "%s: submit request only in active state\n",
John Youn9da51972017-01-17 20:30:27 -08001301 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001302 return -EAGAIN;
1303 }
1304
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001305 /* initialise status of the request */
1306 INIT_LIST_HEAD(&hs_req->queue);
1307 req->actual = 0;
1308 req->status = -EINPROGRESS;
1309
Minas Harutyunyanf8e6a342020-01-21 14:24:04 +04001310 /* Don't queue ISOC request if length greater than mps*mc */
1311 if (hs_ep->isochronous &&
1312 req->length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1313 dev_err(hs->dev, "req length > maxpacket*mc\n");
1314 return -EINVAL;
1315 }
1316
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001317 /* In DDMA mode for ISOC's don't queue request if length greater
1318 * than descriptor limits.
1319 */
1320 if (using_desc_dma(hs) && hs_ep->isochronous) {
1321 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1322 if (hs_ep->dir_in && req->length > maxsize) {
1323 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1324 req->length, maxsize);
1325 return -EINVAL;
1326 }
1327
1328 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1329 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1330 req->length, hs_ep->ep.maxpacket);
1331 return -EINVAL;
1332 }
1333 }
1334
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001335 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001336 if (ret)
1337 return ret;
1338
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001339 /* if we're using DMA, sync the buffers as necessary */
1340 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001341 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001342 if (ret)
1343 return ret;
1344 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001345 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1346 if (using_desc_dma(hs) && !hs_ep->index) {
1347 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1348 if (ret)
1349 return ret;
1350 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001351
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001352 first = list_empty(&hs_ep->queue);
1353 list_add_tail(&hs_req->queue, &hs_ep->queue);
1354
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001355 /*
1356 * Handle DDMA isochronous transfers separately - just add new entry
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001357 * to the descriptor chain.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001358 * Transfer will be started once SW gets either one of NAK or
1359 * OutTknEpDis interrupts.
1360 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001361 if (using_desc_dma(hs) && hs_ep->isochronous) {
1362 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1363 dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1364 hs_req->req.length);
1365 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001366 return 0;
1367 }
1368
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001369 if (first) {
1370 if (!hs_ep->isochronous) {
1371 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1372 return 0;
1373 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001374
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001375 /* Update current frame number value. */
1376 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1377 while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001378 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001379 /* Update current frame number value once more as it
1380 * changes here.
1381 */
1382 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1383 }
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001384
1385 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1386 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1387 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001388 return 0;
1389}
1390
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001391static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001392 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001393{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001394 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001395 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001396 unsigned long flags = 0;
1397 int ret = 0;
1398
1399 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001400 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001401 spin_unlock_irqrestore(&hs->lock, flags);
1402
1403 return ret;
1404}
1405
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001406static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001407 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001408{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001409 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001410
1411 kfree(hs_req);
1412}
1413
1414/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001415 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001416 * @ep: The endpoint the request was on.
1417 * @req: The request completed.
1418 *
1419 * Called on completion of any requests the driver itself
1420 * submitted that need cleaning up.
1421 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001422static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001423 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001424{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001425 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001426 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001427
1428 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1429
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001430 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001431}
1432
1433/**
1434 * ep_from_windex - convert control wIndex value to endpoint
1435 * @hsotg: The driver state.
1436 * @windex: The control request wIndex field (in host order).
1437 *
1438 * Convert the given wIndex into a pointer to an driver endpoint
1439 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001440 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001441static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001442 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001443{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001444 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001445 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1446 int idx = windex & 0x7F;
1447
1448 if (windex >= 0x100)
1449 return NULL;
1450
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001451 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001452 return NULL;
1453
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001454 ep = index_to_ep(hsotg, idx, dir);
1455
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001456 if (idx && ep->dir_in != dir)
1457 return NULL;
1458
1459 return ep;
1460}
1461
1462/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001463 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001464 * @hsotg: The driver state.
1465 * @testmode: requested usb test mode
1466 * Enable usb Test Mode requested by the Host.
1467 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001468int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001469{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001470 int dctl = dwc2_readl(hsotg, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001471
1472 dctl &= ~DCTL_TSTCTL_MASK;
1473 switch (testmode) {
1474 case TEST_J:
1475 case TEST_K:
1476 case TEST_SE0_NAK:
1477 case TEST_PACKET:
1478 case TEST_FORCE_EN:
1479 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1480 break;
1481 default:
1482 return -EINVAL;
1483 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001484 dwc2_writel(hsotg, dctl, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001485 return 0;
1486}
1487
1488/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001489 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001490 * @hsotg: The device state
1491 * @ep: Endpoint 0
1492 * @buff: Buffer for request
1493 * @length: Length of reply.
1494 *
1495 * Create a request and queue it on the given endpoint. This is useful as
1496 * an internal method of sending replies to certain control requests, etc.
1497 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001498static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001499 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001500 void *buff,
1501 int length)
1502{
1503 struct usb_request *req;
1504 int ret;
1505
1506 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1507
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001508 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001509 hsotg->ep0_reply = req;
1510 if (!req) {
1511 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1512 return -ENOMEM;
1513 }
1514
1515 req->buf = hsotg->ep0_buff;
1516 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001517 /*
1518 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1519 * STATUS stage.
1520 */
1521 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001522 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001523
1524 if (length)
1525 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001526
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001527 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001528 if (ret) {
1529 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1530 return ret;
1531 }
1532
1533 return 0;
1534}
1535
1536/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001537 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001538 * @hsotg: The device state
1539 * @ctrl: USB control request
1540 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001541static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001542 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001543{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001544 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1545 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001546 __le16 reply;
Minas Harutyunyanc7874442020-01-21 14:17:07 +04001547 u16 status;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001548 int ret;
1549
1550 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1551
1552 if (!ep0->dir_in) {
1553 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1554 return -EINVAL;
1555 }
1556
1557 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1558 case USB_RECIP_DEVICE:
Minas Harutyunyanc7874442020-01-21 14:17:07 +04001559 status = 1 << USB_DEVICE_SELF_POWERED;
1560 status |= hsotg->remote_wakeup_allowed <<
1561 USB_DEVICE_REMOTE_WAKEUP;
1562 reply = cpu_to_le16(status);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001563 break;
1564
1565 case USB_RECIP_INTERFACE:
1566 /* currently, the data result should be zero */
1567 reply = cpu_to_le16(0);
1568 break;
1569
1570 case USB_RECIP_ENDPOINT:
1571 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1572 if (!ep)
1573 return -ENOENT;
1574
1575 reply = cpu_to_le16(ep->halted ? 1 : 0);
1576 break;
1577
1578 default:
1579 return 0;
1580 }
1581
1582 if (le16_to_cpu(ctrl->wLength) != 2)
1583 return -EINVAL;
1584
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001585 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001586 if (ret) {
1587 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1588 return ret;
1589 }
1590
1591 return 1;
1592}
1593
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001594static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001595
1596/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001597 * get_ep_head - return the first request on the endpoint
1598 * @hs_ep: The controller endpoint to get
1599 *
1600 * Get the first request on the endpoint.
1601 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001602static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001603{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001604 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1605 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001606}
1607
1608/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001609 * dwc2_gadget_start_next_request - Starts next request from ep queue
1610 * @hs_ep: Endpoint structure
1611 *
1612 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1613 * in its handler. Hence we need to unmask it here to be able to do
1614 * resynchronization.
1615 */
1616static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1617{
1618 u32 mask;
1619 struct dwc2_hsotg *hsotg = hs_ep->parent;
1620 int dir_in = hs_ep->dir_in;
1621 struct dwc2_hsotg_req *hs_req;
1622 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1623
1624 if (!list_empty(&hs_ep->queue)) {
1625 hs_req = get_ep_head(hs_ep);
1626 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1627 return;
1628 }
1629 if (!hs_ep->isochronous)
1630 return;
1631
1632 if (dir_in) {
1633 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1634 __func__);
1635 } else {
1636 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1637 __func__);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001638 mask = dwc2_readl(hsotg, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001639 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001640 dwc2_writel(hsotg, mask, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001641 }
1642}
1643
1644/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001645 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001646 * @hsotg: The device state
1647 * @ctrl: USB control request
1648 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001649static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001650 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001651{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001652 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1653 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001654 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001655 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001656 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001657 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001658 u32 recip;
1659 u32 wValue;
1660 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001661
1662 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1663 __func__, set ? "SET" : "CLEAR");
1664
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001665 wValue = le16_to_cpu(ctrl->wValue);
1666 wIndex = le16_to_cpu(ctrl->wIndex);
1667 recip = ctrl->bRequestType & USB_RECIP_MASK;
1668
1669 switch (recip) {
1670 case USB_RECIP_DEVICE:
1671 switch (wValue) {
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001672 case USB_DEVICE_REMOTE_WAKEUP:
Minas Harutyunyanc7874442020-01-21 14:17:07 +04001673 if (set)
1674 hsotg->remote_wakeup_allowed = 1;
1675 else
1676 hsotg->remote_wakeup_allowed = 0;
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001677 break;
1678
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001679 case USB_DEVICE_TEST_MODE:
1680 if ((wIndex & 0xff) != 0)
1681 return -EINVAL;
1682 if (!set)
1683 return -EINVAL;
1684
1685 hsotg->test_mode = wIndex >> 8;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001686 break;
1687 default:
1688 return -ENOENT;
1689 }
Minas Harutyunyanc7874442020-01-21 14:17:07 +04001690
1691 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
1692 if (ret) {
1693 dev_err(hsotg->dev,
1694 "%s: failed to send reply\n", __func__);
1695 return ret;
1696 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001697 break;
1698
1699 case USB_RECIP_ENDPOINT:
1700 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001701 if (!ep) {
1702 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001703 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001704 return -ENOENT;
1705 }
1706
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001707 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001708 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001709 halted = ep->halted;
1710
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001711 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001712
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001713 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001714 if (ret) {
1715 dev_err(hsotg->dev,
1716 "%s: failed to send reply\n", __func__);
1717 return ret;
1718 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001719
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001720 /*
1721 * we have to complete all requests for ep if it was
1722 * halted, and the halt was cleared by CLEAR_FEATURE
1723 */
1724
1725 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001726 /*
1727 * If we have request in progress,
1728 * then complete it
1729 */
1730 if (ep->req) {
1731 hs_req = ep->req;
1732 ep->req = NULL;
1733 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001734 if (hs_req->req.complete) {
1735 spin_unlock(&hsotg->lock);
1736 usb_gadget_giveback_request(
1737 &ep->ep, &hs_req->req);
1738 spin_lock(&hsotg->lock);
1739 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001740 }
1741
1742 /* If we have pending request, then start it */
John Youn34c08872017-01-17 20:31:43 -08001743 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001744 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001745 }
1746
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001747 break;
1748
1749 default:
1750 return -ENOENT;
1751 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001752 break;
1753 default:
1754 return -ENOENT;
1755 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001756 return 1;
1757}
1758
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001759static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001760
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001761/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001762 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001763 * @hsotg: The device state
1764 *
1765 * Set stall for ep0 as response for setup request.
1766 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001767static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001768{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001769 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001770 u32 reg;
1771 u32 ctrl;
1772
1773 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1774 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1775
1776 /*
1777 * DxEPCTL_Stall will be cleared by EP once it has
1778 * taken effect, so no need to clear later.
1779 */
1780
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001781 ctrl = dwc2_readl(hsotg, reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001782 ctrl |= DXEPCTL_STALL;
1783 ctrl |= DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001784 dwc2_writel(hsotg, ctrl, reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001785
1786 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001787 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001788 ctrl, reg, dwc2_readl(hsotg, reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001789
1790 /*
1791 * complete won't be called, so we enqueue
1792 * setup request here
1793 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001794 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001795}
1796
1797/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001798 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001799 * @hsotg: The device state
1800 * @ctrl: The control request received
1801 *
1802 * The controller has received the SETUP phase of a control request, and
1803 * needs to work out what to do next (and whether to pass it on to the
1804 * gadget driver).
1805 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001806static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001807 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001808{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001809 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001810 int ret = 0;
1811 u32 dcfg;
1812
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001813 dev_dbg(hsotg->dev,
1814 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1815 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1816 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001817
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001818 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001819 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001820 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1821 } else if (ctrl->bRequestType & USB_DIR_IN) {
1822 ep0->dir_in = 1;
1823 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1824 } else {
1825 ep0->dir_in = 0;
1826 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1827 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001828
1829 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1830 switch (ctrl->bRequest) {
1831 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001832 hsotg->connected = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001833 dcfg = dwc2_readl(hsotg, DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001834 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001835 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1836 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001837 dwc2_writel(hsotg, dcfg, DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001838
1839 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1840
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001841 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001842 return;
1843
1844 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001845 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001846 break;
1847
1848 case USB_REQ_CLEAR_FEATURE:
1849 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001850 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001851 break;
1852 }
1853 }
1854
1855 /* as a fallback, try delivering it to the driver to deal with */
1856
1857 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001858 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001859 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001860 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001861 if (ret < 0)
1862 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1863 }
1864
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001865 /*
1866 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001867 * so respond with a STALL for the status stage to indicate failure.
1868 */
1869
Robert Baldygac9f721b2014-01-14 08:36:00 +01001870 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001871 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001872}
1873
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001874/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001875 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001876 * @ep: The endpoint the request was on.
1877 * @req: The request completed.
1878 *
1879 * Called on completion of any requests the driver itself submitted for
1880 * EP0 setup packets
1881 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001882static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001883 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001884{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001885 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001886 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001887
1888 if (req->status < 0) {
1889 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1890 return;
1891 }
1892
Robert Baldyga93f599f2013-11-21 13:49:17 +01001893 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001894 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001895 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001896 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001897 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001898 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001899}
1900
1901/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001902 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001903 * @hsotg: The device state.
1904 *
1905 * Enqueue a request on EP0 if necessary to received any SETUP packets
1906 * received from the host.
1907 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001908static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001909{
1910 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001911 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001912 int ret;
1913
1914 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1915
1916 req->zero = 0;
1917 req->length = 8;
1918 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001919 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001920
1921 if (!list_empty(&hs_req->queue)) {
1922 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1923 return;
1924 }
1925
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001926 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001927 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001928 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001929
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001930 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001931 if (ret < 0) {
1932 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001933 /*
1934 * Don't think there's much we can do other than watch the
1935 * driver fail.
1936 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001937 }
1938}
1939
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001940static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001941 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001942{
1943 u32 ctrl;
1944 u8 index = hs_ep->index;
1945 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1946 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1947
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001948 if (hs_ep->dir_in)
1949 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001950 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001951 else
1952 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001953 index);
1954 if (using_desc_dma(hsotg)) {
1955 /* Not specific buffer needed for ep0 ZLP */
1956 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001957
Minas Harutyunyan201ec562018-01-16 16:03:32 +04001958 if (!index)
1959 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1960
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001961 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1962 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001963 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1964 DXEPTSIZ_XFERSIZE(0),
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001965 epsiz_reg);
1966 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001967
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001968 ctrl = dwc2_readl(hsotg, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001969 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1970 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1971 ctrl |= DXEPCTL_USBACTEP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001972 dwc2_writel(hsotg, ctrl, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001973}
1974
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001975/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001976 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001977 * @hsotg: The device state.
1978 * @hs_ep: The endpoint the request was on.
1979 * @hs_req: The request to complete.
1980 * @result: The result code (0 => Ok, otherwise errno)
1981 *
1982 * The given request has finished, so call the necessary completion
1983 * if it has one and then look to see if we can start a new request
1984 * on the endpoint.
1985 *
1986 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001987 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001988static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001989 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001990 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001991 int result)
1992{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001993 if (!hs_req) {
1994 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1995 return;
1996 }
1997
1998 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1999 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
2000
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002001 /*
2002 * only replace the status if we've not already set an error
2003 * from a previous transaction
2004 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002005
2006 if (hs_req->req.status == -EINPROGRESS)
2007 hs_req->req.status = result;
2008
Yunzhi Li44583fe2015-09-29 12:25:01 +02002009 if (using_dma(hsotg))
2010 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2011
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002012 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01002013
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002014 hs_ep->req = NULL;
2015 list_del_init(&hs_req->queue);
2016
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002017 /*
2018 * call the complete request with the locks off, just in case the
2019 * request tries to queue more work for this endpoint.
2020 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002021
2022 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002023 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002024 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002025 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002026 }
2027
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002028 /* In DDMA don't need to proceed to starting of next ISOC request */
2029 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2030 return;
2031
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002032 /*
2033 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002034 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002035 * so be careful when doing this.
2036 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002037
John Youn34c08872017-01-17 20:31:43 -08002038 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002039 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002040}
2041
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002042/*
2043 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2044 * @hs_ep: The endpoint the request was on.
2045 *
2046 * Get first request from the ep queue, determine descriptor on which complete
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002047 * happened. SW discovers which descriptor currently in use by HW, adjusts
2048 * dma_address and calculates index of completed descriptor based on the value
2049 * of DEPDMA register. Update actual length of request, giveback to gadget.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002050 */
2051static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2052{
2053 struct dwc2_hsotg *hsotg = hs_ep->parent;
2054 struct dwc2_hsotg_req *hs_req;
2055 struct usb_request *ureq;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002056 u32 desc_sts;
2057 u32 mask;
2058
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002059 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2060
2061 /* Process only descriptors with buffer status set to DMA done */
2062 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2063 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2064
2065 hs_req = get_ep_head(hs_ep);
2066 if (!hs_req) {
2067 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2068 return;
2069 }
2070 ureq = &hs_req->req;
2071
2072 /* Check completion status */
2073 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2074 DEV_DMA_STS_SUCC) {
2075 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2076 DEV_DMA_ISOC_RX_NBYTES_MASK;
2077 ureq->actual = ureq->length - ((desc_sts & mask) >>
2078 DEV_DMA_ISOC_NBYTES_SHIFT);
2079
2080 /* Adjust actual len for ISOC Out if len is
2081 * not align of 4
2082 */
2083 if (!hs_ep->dir_in && ureq->length & 0x3)
2084 ureq->actual += 4 - (ureq->length & 0x3);
2085 }
2086
2087 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2088
2089 hs_ep->compl_desc++;
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +04002090 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_HS_ISOC - 1))
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002091 hs_ep->compl_desc = 0;
2092 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002093 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002094}
2095
2096/*
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002097 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2098 * @hs_ep: The isochronous endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002099 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002100 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2101 * interrupt. Reset target frame and next_desc to allow to start
2102 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2103 * interrupt for OUT direction.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002104 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002105static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002106{
2107 struct dwc2_hsotg *hsotg = hs_ep->parent;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002108
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002109 if (!hs_ep->dir_in)
2110 dwc2_flush_rx_fifo(hsotg);
2111 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002112
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002113 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2114 hs_ep->next_desc = 0;
2115 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002116}
2117
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002118/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002119 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002120 * @hsotg: The device state.
2121 * @ep_idx: The endpoint index for the data
2122 * @size: The size of data in the fifo, in bytes
2123 *
2124 * The FIFO status shows there is data to read from the FIFO for a given
2125 * endpoint, so sort out whether we need to read the data into a request
2126 * that has been made for that endpoint.
2127 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002128static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002129{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002130 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2131 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002132 int to_read;
2133 int max_req;
2134 int read_ptr;
2135
2136 if (!hs_req) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002137 u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002138 int ptr;
2139
Robert Baldyga6b448af2014-12-16 11:51:44 +01002140 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002141 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002142 __func__, size, ep_idx, epctl);
2143
2144 /* dump the data from the FIFO, we've nothing we can do */
2145 for (ptr = 0; ptr < size; ptr += 4)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002146 (void)dwc2_readl(hsotg, EPFIFO(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002147
2148 return;
2149 }
2150
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002151 to_read = size;
2152 read_ptr = hs_req->req.actual;
2153 max_req = hs_req->req.length - read_ptr;
2154
Ben Dooksa33e7132010-07-19 09:40:49 +01002155 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2156 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2157
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002158 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002159 /*
2160 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002161 * to deal with in this request.
2162 */
2163
2164 /* currently we don't deal this */
2165 WARN_ON_ONCE(1);
2166 }
2167
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002168 hs_ep->total_data += to_read;
2169 hs_req->req.actual += to_read;
2170 to_read = DIV_ROUND_UP(to_read, 4);
2171
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002172 /*
2173 * note, we might over-write the buffer end by 3 bytes depending on
2174 * alignment of the data.
2175 */
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +04002176 dwc2_readl_rep(hsotg, EPFIFO(ep_idx),
2177 hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002178}
2179
2180/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002181 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002182 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002183 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002184 *
2185 * Generate a zero-length IN packet request for terminating a SETUP
2186 * transaction.
2187 *
2188 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002189 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002190 * the TxFIFO.
2191 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002192static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002193{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002194 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002195 hsotg->eps_out[0]->dir_in = dir_in;
2196 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002197
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002198 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002199}
2200
Roman Bacikec1f9d92015-09-10 18:13:43 -07002201static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002202 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002203{
2204 u32 ctrl;
2205
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002206 ctrl = dwc2_readl(hsotg, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002207 if (ctrl & DXEPCTL_EOFRNUM)
2208 ctrl |= DXEPCTL_SETEVENFR;
2209 else
2210 ctrl |= DXEPCTL_SETODDFR;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002211 dwc2_writel(hsotg, ctrl, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002212}
2213
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002214/*
2215 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2216 * @hs_ep - The endpoint on which transfer went
2217 *
2218 * Iterate over endpoints descriptor chain and get info on bytes remained
2219 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2220 */
2221static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2222{
2223 struct dwc2_hsotg *hsotg = hs_ep->parent;
2224 unsigned int bytes_rem = 0;
2225 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2226 int i;
2227 u32 status;
2228
2229 if (!desc)
2230 return -EINVAL;
2231
2232 for (i = 0; i < hs_ep->desc_count; ++i) {
2233 status = desc->status;
2234 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2235
2236 if (status & DEV_DMA_STS_MASK)
2237 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2238 i, status & DEV_DMA_STS_MASK);
Minas Harutyunyanac4217e2019-02-22 15:49:19 +04002239 desc++;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002240 }
2241
2242 return bytes_rem;
2243}
2244
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002245/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002246 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002247 * @hsotg: The device instance
2248 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002249 *
2250 * The RXFIFO has delivered an OutDone event, which means that the data
2251 * transfer for an OUT endpoint has been completed, either by a short
2252 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002253 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002254static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002255{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002256 u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002257 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2258 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002259 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002260 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002261 int result = 0;
2262
2263 if (!hs_req) {
2264 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2265 return;
2266 }
2267
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002268 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2269 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002270 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2271 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002272 return;
2273 }
2274
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002275 if (using_desc_dma(hsotg))
2276 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2277
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002278 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002279 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002280
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002281 /*
2282 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002283 * is left in the endpoint size register and then working it
2284 * out from the amount we loaded for the transfer.
2285 *
2286 * We need to do this as DMA pointers are always 32bit aligned
2287 * so may overshoot/undershoot the transfer.
2288 */
2289
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002290 size_done = hs_ep->size_loaded - size_left;
2291 size_done += hs_ep->last_load;
2292
2293 req->actual = size_done;
2294 }
2295
Ben Dooksa33e7132010-07-19 09:40:49 +01002296 /* if there is more request to do, schedule new transfer */
2297 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002298 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002299 return;
2300 }
2301
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002302 if (req->actual < req->length && req->short_not_ok) {
2303 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2304 __func__, req->actual, req->length);
2305
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002306 /*
2307 * todo - what should we return here? there's no one else
2308 * even bothering to check the status.
2309 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002310 }
2311
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002312 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2313 if (!using_desc_dma(hsotg) && epnum == 0 &&
2314 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002315 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002316 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002317 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002318 }
2319
Roman Bacikec1f9d92015-09-10 18:13:43 -07002320 /*
2321 * Slave mode OUT transfers do not go through XferComplete so
2322 * adjust the ISOC parity here.
2323 */
2324 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002325 if (hs_ep->isochronous && hs_ep->interval == 1)
2326 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002327 else if (hs_ep->isochronous && hs_ep->interval > 1)
2328 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002329 }
2330
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002331 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002332}
2333
2334/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002335 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002336 * @hsotg: The device instance
2337 *
2338 * The IRQ handler has detected that the RX FIFO has some data in it
2339 * that requires processing, so find out what is in there and do the
2340 * appropriate read.
2341 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002342 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002343 * chunks, so if you have x packets received on an endpoint you'll get x
2344 * FIFO events delivered, each with a packet's worth of data in it.
2345 *
2346 * When using DMA, we should not be processing events from the RXFIFO
2347 * as the actual data should be sent to the memory directly and we turn
2348 * on the completion interrupts to get notifications of transfer completion.
2349 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002350static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002351{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002352 u32 grxstsr = dwc2_readl(hsotg, GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002353 u32 epnum, status, size;
2354
2355 WARN_ON(using_dma(hsotg));
2356
Dinh Nguyen47a16852014-04-14 14:13:34 -07002357 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2358 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002359
Dinh Nguyen47a16852014-04-14 14:13:34 -07002360 size = grxstsr & GRXSTS_BYTECNT_MASK;
2361 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002363 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002364 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002365
Dinh Nguyen47a16852014-04-14 14:13:34 -07002366 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2367 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2368 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002369 break;
2370
Dinh Nguyen47a16852014-04-14 14:13:34 -07002371 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002372 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002373 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002374
2375 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002376 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002377 break;
2378
Dinh Nguyen47a16852014-04-14 14:13:34 -07002379 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002380 dev_dbg(hsotg->dev,
2381 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002382 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002383 dwc2_readl(hsotg, DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002384 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002385 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002386 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2387 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2388 */
2389 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002390 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002391 break;
2392
Dinh Nguyen47a16852014-04-14 14:13:34 -07002393 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002394 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002395 break;
2396
Dinh Nguyen47a16852014-04-14 14:13:34 -07002397 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002398 dev_dbg(hsotg->dev,
2399 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002400 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002401 dwc2_readl(hsotg, DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002402
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002403 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2404
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002405 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002406 break;
2407
2408 default:
2409 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2410 __func__, grxstsr);
2411
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002412 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002413 break;
2414 }
2415}
2416
2417/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002418 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002419 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002420 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002421static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002422{
2423 switch (mps) {
2424 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002425 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002426 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002427 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002428 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002429 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002430 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002431 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002432 }
2433
2434 /* bad max packet size, warn and return invalid result */
2435 WARN_ON(1);
2436 return (u32)-1;
2437}
2438
2439/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002440 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002441 * @hsotg: The driver state.
2442 * @ep: The index number of the endpoint
2443 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002444 * @mc: The multicount value
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002445 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002446 *
2447 * Configure the maximum packet size for the given endpoint, updating
2448 * the hardware control registers to reflect this.
2449 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002450static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002451 unsigned int ep, unsigned int mps,
2452 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002453{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002454 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002455 u32 reg;
2456
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002457 hs_ep = index_to_ep(hsotg, ep, dir_in);
2458 if (!hs_ep)
2459 return;
2460
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002461 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002462 u32 mps_bytes = mps;
2463
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002464 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002465 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2466 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002467 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002468 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002469 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002470 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002471 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002472 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002473 hs_ep->mc = mc;
2474 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002475 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002476 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002477 }
2478
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002479 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002480 reg = dwc2_readl(hsotg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002481 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002482 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002483 dwc2_writel(hsotg, reg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002484 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002485 reg = dwc2_readl(hsotg, DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002486 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002487 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002488 dwc2_writel(hsotg, reg, DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002489 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002490
2491 return;
2492
2493bad_mps:
2494 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2495}
2496
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002497/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002498 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002499 * @hsotg: The driver state
2500 * @idx: The index for the endpoint (0..15)
2501 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002502static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002503{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002504 dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2505 GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002506
2507 /* wait until the fifo is flushed */
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002508 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2509 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2510 __func__);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002511}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002512
2513/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002514 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002515 * @hsotg: The driver state
2516 * @hs_ep: The driver endpoint to check.
2517 *
2518 * Check to see if there is a request that has data to send, and if so
2519 * make an attempt to write data into the FIFO.
2520 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002521static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002522 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002523{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002524 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002525
Robert Baldygaafcf4162013-09-19 11:50:19 +02002526 if (!hs_ep->dir_in || !hs_req) {
2527 /**
2528 * if request is not enqueued, we disable interrupts
2529 * for endpoints, excepting ep0
2530 */
2531 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002532 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002533 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002534 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002535 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002536
2537 if (hs_req->req.actual < hs_req->req.length) {
2538 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2539 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002540 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002541 }
2542
2543 return 0;
2544}
2545
2546/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002547 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002548 * @hsotg: The device state.
2549 * @hs_ep: The endpoint that has just completed.
2550 *
2551 * An IN transfer has been completed, update the transfer's state and then
2552 * call the relevant completion routines.
2553 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002554static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002555 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002556{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002557 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002558 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002559 int size_left, size_done;
2560
2561 if (!hs_req) {
2562 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2563 return;
2564 }
2565
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002566 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002567 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2568 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002569
2570 /*
2571 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2572 * changed to IN. Change back to complete OUT transfer request
2573 */
2574 hs_ep->dir_in = 0;
2575
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002576 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002577 if (hsotg->test_mode) {
2578 int ret;
2579
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002580 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002581 if (ret < 0) {
2582 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002583 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002584 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002585 return;
2586 }
2587 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002588 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002589 return;
2590 }
2591
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002592 /*
2593 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002594 * in the endpoint size register and then working it out from
2595 * the amount we loaded for the transfer.
2596 *
2597 * We do this even for DMA, as the transfer may have incremented
2598 * past the end of the buffer (DMA transfers are always 32bit
2599 * aligned).
2600 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002601 if (using_desc_dma(hsotg)) {
2602 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2603 if (size_left < 0)
2604 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2605 size_left);
2606 } else {
2607 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2608 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002609
2610 size_done = hs_ep->size_loaded - size_left;
2611 size_done += hs_ep->last_load;
2612
2613 if (hs_req->req.actual != size_done)
2614 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2615 __func__, hs_req->req.actual, size_done);
2616
2617 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002618 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2619 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002620
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002621 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2622 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002623 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002624 return;
2625 }
2626
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002627 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002628 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002629 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002630 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002631 /* transfer will be completed on next complete interrupt */
2632 return;
2633 }
2634
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002635 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2636 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002637 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002638 return;
2639 }
2640
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002641 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002642}
2643
2644/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002645 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2646 * @hsotg: The device state.
2647 * @idx: Index of ep.
2648 * @dir_in: Endpoint direction 1-in 0-out.
2649 *
2650 * Reads for endpoint with given index and direction, by masking
2651 * epint_reg with coresponding mask.
2652 */
2653static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2654 unsigned int idx, int dir_in)
2655{
2656 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2657 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2658 u32 ints;
2659 u32 mask;
2660 u32 diepempmsk;
2661
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002662 mask = dwc2_readl(hsotg, epmsk_reg);
2663 diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002664 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2665 mask |= DXEPINT_SETUP_RCVD;
2666
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002667 ints = dwc2_readl(hsotg, epint_reg);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002668 ints &= mask;
2669 return ints;
2670}
2671
2672/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002673 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2674 * @hs_ep: The endpoint on which interrupt is asserted.
2675 *
2676 * This interrupt indicates that the endpoint has been disabled per the
2677 * application's request.
2678 *
2679 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2680 * in case of ISOC completes current request.
2681 *
2682 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2683 * request starts it.
2684 */
2685static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2686{
2687 struct dwc2_hsotg *hsotg = hs_ep->parent;
2688 struct dwc2_hsotg_req *hs_req;
2689 unsigned char idx = hs_ep->index;
2690 int dir_in = hs_ep->dir_in;
2691 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002692 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002693
2694 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2695
2696 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002697 int epctl = dwc2_readl(hsotg, epctl_reg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002698
2699 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2700
2701 if (hs_ep->isochronous) {
2702 dwc2_hsotg_complete_in(hsotg, hs_ep);
2703 return;
2704 }
2705
2706 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002707 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002708
2709 dctl |= DCTL_CGNPINNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002710 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002711 }
2712 return;
2713 }
2714
2715 if (dctl & DCTL_GOUTNAKSTS) {
2716 dctl |= DCTL_CGOUTNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002717 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002718 }
2719
2720 if (!hs_ep->isochronous)
2721 return;
2722
2723 if (list_empty(&hs_ep->queue)) {
2724 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2725 __func__, hs_ep);
2726 return;
2727 }
2728
2729 do {
2730 hs_req = get_ep_head(hs_ep);
2731 if (hs_req)
2732 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2733 -ENODATA);
2734 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04002735 /* Update current frame number value. */
2736 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002737 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2738
2739 dwc2_gadget_start_next_request(hs_ep);
2740}
2741
2742/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002743 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002744 * @ep: The endpoint on which interrupt is asserted.
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002745 *
2746 * This is starting point for ISOC-OUT transfer, synchronization done with
2747 * first out token received from host while corresponding EP is disabled.
2748 *
2749 * Device does not know initial frame in which out token will come. For this
2750 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2751 * getting this interrupt SW starts calculation for next transfer frame.
2752 */
2753static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2754{
2755 struct dwc2_hsotg *hsotg = ep->parent;
2756 int dir_in = ep->dir_in;
2757 u32 doepmsk;
2758
2759 if (dir_in || !ep->isochronous)
2760 return;
2761
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002762 if (using_desc_dma(hsotg)) {
2763 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2764 /* Start first ISO Out */
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002765 ep->target_frame = hsotg->frame_number;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002766 dwc2_gadget_start_isoc_ddma(ep);
2767 }
2768 return;
2769 }
2770
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002771 if (ep->interval > 1 &&
2772 ep->target_frame == TARGET_FRAME_INITIAL) {
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002773 u32 ctrl;
2774
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002775 ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002776 dwc2_gadget_incr_frame_num(ep);
2777
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002778 ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002779 if (ep->target_frame & 0x1)
2780 ctrl |= DXEPCTL_SETODDFR;
2781 else
2782 ctrl |= DXEPCTL_SETEVENFR;
2783
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002784 dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002785 }
2786
2787 dwc2_gadget_start_next_request(ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002788 doepmsk = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002789 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002790 dwc2_writel(hsotg, doepmsk, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002791}
2792
2793/**
John Youn38beaec2017-01-17 20:31:13 -08002794 * dwc2_gadget_handle_nak - handle NAK interrupt
2795 * @hs_ep: The endpoint on which interrupt is asserted.
2796 *
2797 * This is starting point for ISOC-IN transfer, synchronization done with
2798 * first IN token received from host while corresponding EP is disabled.
2799 *
2800 * Device does not know when first one token will arrive from host. On first
2801 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2802 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2803 * sent in response to that as there was no data in FIFO. SW is basing on this
2804 * interrupt to obtain frame in which token has come and then based on the
2805 * interval calculates next frame for transfer.
2806 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002807static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2808{
2809 struct dwc2_hsotg *hsotg = hs_ep->parent;
2810 int dir_in = hs_ep->dir_in;
2811
2812 if (!dir_in || !hs_ep->isochronous)
2813 return;
2814
2815 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002816
2817 if (using_desc_dma(hsotg)) {
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002818 hs_ep->target_frame = hsotg->frame_number;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002819 dwc2_gadget_incr_frame_num(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002820 dwc2_gadget_start_isoc_ddma(hs_ep);
2821 return;
2822 }
2823
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002824 hs_ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002825 if (hs_ep->interval > 1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002826 u32 ctrl = dwc2_readl(hsotg,
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002827 DIEPCTL(hs_ep->index));
2828 if (hs_ep->target_frame & 0x1)
2829 ctrl |= DXEPCTL_SETODDFR;
2830 else
2831 ctrl |= DXEPCTL_SETEVENFR;
2832
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002833 dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002834 }
2835
2836 dwc2_hsotg_complete_request(hsotg, hs_ep,
2837 get_ep_head(hs_ep), 0);
2838 }
2839
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002840 if (!using_desc_dma(hsotg))
2841 dwc2_gadget_incr_frame_num(hs_ep);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002842}
2843
2844/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002845 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002846 * @hsotg: The driver state
2847 * @idx: The index for the endpoint (0..15)
2848 * @dir_in: Set if this is an IN endpoint
2849 *
2850 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002851 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002852static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002853 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002854{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002855 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002856 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2857 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2858 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002859 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002860 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002861
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002862 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002863 ctrl = dwc2_readl(hsotg, epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002864
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002865 /* Clear endpoint interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002866 dwc2_writel(hsotg, ints, epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002867
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002868 if (!hs_ep) {
2869 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002870 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002871 return;
2872 }
2873
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002874 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2875 __func__, idx, dir_in ? "in" : "out", ints);
2876
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002877 /* Don't process XferCompl interrupt if it is a setup packet */
2878 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2879 ints &= ~DXEPINT_XFERCOMPL;
2880
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002881 /*
2882 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2883 * stage and xfercomplete was generated without SETUP phase done
2884 * interrupt. SW should parse received setup packet only after host's
2885 * exit from setup phase of control transfer.
2886 */
2887 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2888 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2889 ints &= ~DXEPINT_XFERCOMPL;
2890
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002891 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002892 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002893 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002894 __func__, dwc2_readl(hsotg, epctl_reg),
2895 dwc2_readl(hsotg, epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002896
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002897 /* In DDMA handle isochronous requests separately */
2898 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002899 /* XferCompl set along with BNA */
2900 if (!(ints & DXEPINT_BNAINTR))
2901 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002902 } else if (dir_in) {
2903 /*
2904 * We get OutDone from the FIFO, so we only
2905 * need to look at completing IN requests here
2906 * if operating slave mode
2907 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002908 if (hs_ep->isochronous && hs_ep->interval > 1)
2909 dwc2_gadget_incr_frame_num(hs_ep);
2910
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002911 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002912 if (ints & DXEPINT_NAKINTRPT)
2913 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002914
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002915 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002916 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002917 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002918 /*
2919 * We're using DMA, we need to fire an OutDone here
2920 * as we ignore the RXFIFO.
2921 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002922 if (hs_ep->isochronous && hs_ep->interval > 1)
2923 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002924
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002925 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002926 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002927 }
2928
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002929 if (ints & DXEPINT_EPDISBLD)
2930 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002931
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002932 if (ints & DXEPINT_OUTTKNEPDIS)
2933 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2934
2935 if (ints & DXEPINT_NAKINTRPT)
2936 dwc2_gadget_handle_nak(hs_ep);
2937
Dinh Nguyen47a16852014-04-14 14:13:34 -07002938 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002939 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002940
Dinh Nguyen47a16852014-04-14 14:13:34 -07002941 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002942 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2943
2944 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002945 /*
2946 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002947 * setup packet. In non-DMA mode we'd get this
2948 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002949 * the setup here.
2950 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002951
2952 if (dir_in)
2953 WARN_ON_ONCE(1);
2954 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002955 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002956 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002957 }
2958
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002959 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08002960 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
2961
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04002962 /* Safety check EP0 state when STSPHSERCVD asserted */
2963 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
2964 /* Move to STATUS IN for DDMA */
2965 if (using_desc_dma(hsotg))
2966 dwc2_hsotg_ep0_zlp(hsotg, true);
2967 }
2968
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002969 }
2970
Dinh Nguyen47a16852014-04-14 14:13:34 -07002971 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002972 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002973
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002974 if (ints & DXEPINT_BNAINTR) {
2975 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002976 if (hs_ep->isochronous)
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002977 dwc2_gadget_handle_isoc_bna(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002978 }
2979
Robert Baldyga1479e842013-10-09 08:41:57 +02002980 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002981 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002982 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002983 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2984 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002985 }
2986
2987 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002988 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002989 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2990 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002991 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002992
2993 /* FIFO has space or is empty (see GAHBCFG) */
2994 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002995 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002996 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2997 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002998 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002999 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01003000 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003001 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003002}
3003
3004/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003005 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003006 * @hsotg: The device state.
3007 *
3008 * Handle updating the device settings after the enumeration phase has
3009 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003010 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003011static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003012{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003013 u32 dsts = dwc2_readl(hsotg, DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003014 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003015
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003016 /*
3017 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003018 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003019 * we connected at.
3020 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003021
3022 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3023
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003024 /*
3025 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003027 * not advertise a 64byte MPS on EP0.
3028 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003029
3030 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003031 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003032 case DSTS_ENUMSPD_FS:
3033 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003034 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003035 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003036 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003037 break;
3038
Dinh Nguyen47a16852014-04-14 14:13:34 -07003039 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003040 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003041 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003042 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003043 break;
3044
Dinh Nguyen47a16852014-04-14 14:13:34 -07003045 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003046 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003047 ep0_mps = 8;
3048 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003049 /*
3050 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003051 * moment, and the documentation seems to imply that it isn't
3052 * supported by the PHYs on some of the devices.
3053 */
3054 break;
3055 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003056 dev_info(hsotg->dev, "new device is %s\n",
3057 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003058
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003059 /*
3060 * we should now know the maximum packet size for an
3061 * endpoint, so set the endpoints to a default value.
3062 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003063
3064 if (ep0_mps) {
3065 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003066 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003067 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3068 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003069 for (i = 1; i < hsotg->num_of_eps; i++) {
3070 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003071 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3072 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003073 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003074 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3075 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003076 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003077 }
3078
3079 /* ensure after enumeration our EP0 is active */
3080
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003081 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082
3083 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003084 dwc2_readl(hsotg, DIEPCTL0),
3085 dwc2_readl(hsotg, DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003086}
3087
3088/**
3089 * kill_all_requests - remove all requests from the endpoint's queue
3090 * @hsotg: The device state.
3091 * @ep: The endpoint the requests may be on.
3092 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003093 *
3094 * Go through the requests on the given endpoint and mark them
3095 * completed with the given result code.
3096 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003097static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003098 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01003099 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003100{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003101 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003102 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003103
Robert Baldyga6b448af2014-12-16 11:51:44 +01003104 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003105
Robert Baldyga6b448af2014-12-16 11:51:44 +01003106 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003107 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003108 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01003109
Robert Baldygab203d0a2014-09-09 10:44:56 +02003110 if (!hsotg->dedicated_fifos)
3111 return;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003112 size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003113 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003114 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115}
3116
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003117/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003118 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003119 * @hsotg: The device state.
3120 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003121 * The device has been disconnected. Remove all current
3122 * transactions and signal the gadget driver that this
3123 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003124 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003125void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003126{
John Youn9da51972017-01-17 20:30:27 -08003127 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003128
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003129 if (!hsotg->connected)
3130 return;
3131
3132 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003133 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003134
Minas Harutyunyanec58bfa2018-09-19 18:13:52 +04003135 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003136 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3137 if (hsotg->eps_in[ep])
Minas Harutyunyanb544a682018-12-10 18:09:32 +04003138 kill_all_requests(hsotg, hsotg->eps_in[ep],
3139 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003140 if (hsotg->eps_out[ep])
Minas Harutyunyanb544a682018-12-10 18:09:32 +04003141 kill_all_requests(hsotg, hsotg->eps_out[ep],
3142 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003143 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003144
3145 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003146 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003147
3148 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003149}
3150
3151/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003152 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003153 * @hsotg: The device state:
3154 * @periodic: True if this is a periodic FIFO interrupt
3155 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003156static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003157{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003158 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003159 int epno, ret;
3160
3161 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003162 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003163 ep = index_to_ep(hsotg, epno, 1);
3164
3165 if (!ep)
3166 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003167
3168 if (!ep->dir_in)
3169 continue;
3170
3171 if ((periodic && !ep->periodic) ||
3172 (!periodic && ep->periodic))
3173 continue;
3174
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003175 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003176 if (ret < 0)
3177 break;
3178 }
3179}
3180
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003181/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003182#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3183 GINTSTS_PTXFEMP | \
3184 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003185
Minas Harutyunyanb544a682018-12-10 18:09:32 +04003186static int dwc2_hsotg_ep_disable(struct usb_ep *ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003187/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003188 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003189 * @hsotg: The device state
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04003190 * @is_usb_reset: Usb resetting flag
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003191 *
3192 * Issue a soft reset to the core, and await the core finishing it.
3193 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003194void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003195 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003196{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003197 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003198 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003199 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003200 u32 dcfg = 0;
Minas Harutyunyanec58bfa2018-09-19 18:13:52 +04003201 int ep;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003202
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003203 /* Kill any ep0 requests as controller will be reinitialized */
3204 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3205
Minas Harutyunyanec58bfa2018-09-19 18:13:52 +04003206 if (!is_usb_reset) {
John Stultz6e6360b2017-01-23 14:59:14 -08003207 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003208 return;
Minas Harutyunyanec58bfa2018-09-19 18:13:52 +04003209 } else {
3210 /* all endpoints should be shutdown */
3211 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3212 if (hsotg->eps_in[ep])
3213 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3214 if (hsotg->eps_out[ep])
3215 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3216 }
3217 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003218
3219 /*
3220 * we must now enable ep0 ready for host detection and then
3221 * set configuration.
3222 */
3223
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003224 /* keep other bits untouched (so e.g. forced modes are not lost) */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003225 usbcfg = dwc2_readl(hsotg, GUSBCFG);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003226 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01003227 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003228
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003229 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003230 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3231 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003232 /* FS/LS Dedicated Transceiver Interface */
3233 usbcfg |= GUSBCFG_PHYSEL;
3234 } else {
3235 /* set the PLL on, remove the HNP/SRP and set the PHY */
3236 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3237 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3238 (val << GUSBCFG_USBTRDTIM_SHIFT);
3239 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003240 dwc2_writel(hsotg, usbcfg, GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003241
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003242 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003243
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003244 if (!is_usb_reset)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003245 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003246
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003247 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003248
3249 switch (hsotg->params.speed) {
3250 case DWC2_SPEED_PARAM_LOW:
3251 dcfg |= DCFG_DEVSPD_LS;
3252 break;
3253 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003254 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3255 dcfg |= DCFG_DEVSPD_FS48;
3256 else
3257 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003258 break;
3259 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003260 dcfg |= DCFG_DEVSPD_HS;
3261 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003262
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +04003263 if (hsotg->params.ipg_isoc_en)
3264 dcfg |= DCFG_IPG_ISOC_SUPPORDED;
3265
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003266 dwc2_writel(hsotg, dcfg, DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003267
3268 /* Clear any pending OTG interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003269 dwc2_writel(hsotg, 0xffffffff, GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003270
3271 /* Clear any pending interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003272 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003273 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003274 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003275 GINTSTS_USBRST | GINTSTS_RESETDET |
3276 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003277 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3278 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003279
3280 if (!using_desc_dma(hsotg))
3281 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003282
John Youn95832c02017-01-23 14:57:26 -08003283 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003284 intmsk |= GINTSTS_CONIDSTSCHNG;
3285
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003286 dwc2_writel(hsotg, intmsk, GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003287
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003288 if (using_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003289 dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003290 hsotg->params.ahbcfg,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003291 GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003292
3293 /* Set DDMA mode support in the core if needed */
3294 if (using_desc_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003295 dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003296
3297 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003298 dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ?
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003299 (GAHBCFG_NP_TXF_EMP_LVL |
3300 GAHBCFG_P_TXF_EMP_LVL) : 0) |
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003301 GAHBCFG_GLBL_INTR_EN, GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003302 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003303
3304 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003305 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3306 * when we have no data to transfer. Otherwise we get being flooded by
3307 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003308 */
3309
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003310 dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003311 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003312 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003313 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003314 DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003315
3316 /*
3317 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003318 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003319 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003320 dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003321 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003322 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003323 DOEPMSK_SETUPMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003324 DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003325
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003326 /* Enable BNA interrupt for DDMA */
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003327 if (using_desc_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003328 dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK);
3329 dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK);
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003330 }
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003331
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003332 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003333
3334 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003335 dwc2_readl(hsotg, DIEPCTL0),
3336 dwc2_readl(hsotg, DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003337
3338 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003339 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003340
3341 /*
3342 * Enable the RXFIFO when in slave mode, as this is how we collect
3343 * the data. In DMA mode, we get events from the FIFO but also
3344 * things we cannot process, so do not use it.
3345 */
3346 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003347 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003348
3349 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003350 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3351 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003352
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003353 if (!is_usb_reset) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003354 dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003355 udelay(10); /* see openiboot */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003356 dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003357 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003358
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003359 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003360
3361 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003362 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003363 * writing to the EPCTL register..
3364 */
3365
3366 /* set to read 1 8byte packet */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003367 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
3368 DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003369
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003370 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003371 DXEPCTL_CNAK | DXEPCTL_EPENA |
3372 DXEPCTL_USBACTEP,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003373 DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003374
3375 /* enable, but don't activate EP0in */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003376 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3377 DXEPCTL_USBACTEP, DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003378
Lukasz Majewski308d7342012-05-04 14:17:05 +02003379 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003380 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3381 if (!is_usb_reset)
3382 val |= DCTL_SFTDISCON;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003383 dwc2_set_bit(hsotg, DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003384
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003385 /* configure the core to support LPM */
3386 dwc2_gadget_init_lpm(hsotg);
3387
Lukasz Majewski308d7342012-05-04 14:17:05 +02003388 /* must be at-least 3ms to allow bus to see disconnect */
3389 mdelay(3);
3390
Gregory Herrero065d3932015-09-22 15:16:54 +02003391 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003392
3393 dwc2_hsotg_enqueue_setup(hsotg);
3394
3395 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003396 dwc2_readl(hsotg, DIEPCTL0),
3397 dwc2_readl(hsotg, DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003398}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003399
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003400static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003401{
3402 /* set the soft-disconnect bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003403 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003404}
3405
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003406void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003407{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003408 /* remove the soft-disconnect and let's go */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003409 dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003410}
3411
3412/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003413 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3414 * @hsotg: The device state:
3415 *
3416 * This interrupt indicates one of the following conditions occurred while
3417 * transmitting an ISOC transaction.
3418 * - Corrupted IN Token for ISOC EP.
3419 * - Packet not complete in FIFO.
3420 *
3421 * The following actions will be taken:
3422 * - Determine the EP
3423 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3424 */
3425static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3426{
3427 struct dwc2_hsotg_ep *hs_ep;
3428 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003429 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003430 u32 idx;
3431
3432 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3433
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003434 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003435
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003436 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003437 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003438 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003439 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003440 continue;
3441
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003442 epctrl = dwc2_readl(hsotg, DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003443 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003444 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3445 epctrl |= DXEPCTL_SNAK;
3446 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003447 dwc2_writel(hsotg, epctrl, DIEPCTL(idx));
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003448 }
3449 }
3450
3451 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003452 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003453}
3454
3455/**
3456 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3457 * @hsotg: The device state:
3458 *
3459 * This interrupt indicates one of the following conditions occurred while
3460 * transmitting an ISOC transaction.
3461 * - Corrupted OUT Token for ISOC EP.
3462 * - Packet not complete in FIFO.
3463 *
3464 * The following actions will be taken:
3465 * - Determine the EP
3466 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3467 */
3468static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3469{
3470 u32 gintsts;
3471 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003472 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003473 u32 epctrl;
3474 struct dwc2_hsotg_ep *hs_ep;
3475 int idx;
3476
3477 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3478
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003479 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003480 daintmsk >>= DAINT_OUTEP_SHIFT;
3481
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003482 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003483 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003484 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003485 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003486 continue;
3487
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003488 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003489 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003490 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3491 /* Unmask GOUTNAKEFF interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003492 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003493 gintmsk |= GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003494 dwc2_writel(hsotg, gintmsk, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003495
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003496 gintsts = dwc2_readl(hsotg, GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003497 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003498 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003499 break;
3500 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003501 }
3502 }
3503
3504 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003505 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003506}
3507
3508/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003509 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003510 * @irq: The IRQ number triggered
3511 * @pw: The pw value when registered the handler.
3512 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003513static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003514{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003515 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003516 int retry_count = 8;
3517 u32 gintsts;
3518 u32 gintmsk;
3519
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003520 if (!dwc2_is_device_mode(hsotg))
3521 return IRQ_NONE;
3522
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003523 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003524irq_retry:
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003525 gintsts = dwc2_readl(hsotg, GINTSTS);
3526 gintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003527
3528 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3529 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3530
3531 gintsts &= gintmsk;
3532
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003533 if (gintsts & GINTSTS_RESETDET) {
3534 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3535
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003536 dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003537
3538 /* This event must be used only if controller is suspended */
3539 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003540 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003541 hsotg->lx_state = DWC2_L0;
3542 }
3543 }
3544
3545 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003546 u32 usb_status = dwc2_readl(hsotg, GOTGCTL);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003547 u32 connected = hsotg->connected;
3548
3549 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3550 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003551 dwc2_readl(hsotg, GNPTXSTS));
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003552
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003553 dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003554
3555 /* Report disconnection if it is not already done. */
3556 dwc2_hsotg_disconnect(hsotg);
3557
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003558 /* Reset device address to zero */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003559 dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003560
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003561 if (usb_status & GOTGCTL_BSESVLD && connected)
3562 dwc2_hsotg_core_init_disconnected(hsotg, true);
3563 }
3564
Dinh Nguyen47a16852014-04-14 14:13:34 -07003565 if (gintsts & GINTSTS_ENUMDONE) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003566 dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003567
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003568 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003569 }
3570
Dinh Nguyen47a16852014-04-14 14:13:34 -07003571 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003572 u32 daint = dwc2_readl(hsotg, DAINT);
3573 u32 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003574 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003575 int ep;
3576
Robert Baldyga7e804652013-09-19 11:50:20 +02003577 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003578 daint_out = daint >> DAINT_OUTEP_SHIFT;
3579 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003580
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003581 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3582
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003583 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3584 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003585 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003586 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003587 }
3588
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003589 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3590 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003591 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003592 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003593 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003594 }
3595
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003596 /* check both FIFOs */
3597
Dinh Nguyen47a16852014-04-14 14:13:34 -07003598 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003599 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3600
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003601 /*
3602 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003603 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003604 * it needs re-enabling
3605 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003606
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003607 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3608 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003609 }
3610
Dinh Nguyen47a16852014-04-14 14:13:34 -07003611 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003612 dev_dbg(hsotg->dev, "PTxFEmp\n");
3613
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003614 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003615
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003616 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3617 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003618 }
3619
Dinh Nguyen47a16852014-04-14 14:13:34 -07003620 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003621 /*
3622 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003623 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003624 * set.
3625 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003626
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003627 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003628 }
3629
Dinh Nguyen47a16852014-04-14 14:13:34 -07003630 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003631 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003632 dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003633 }
3634
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003635 /*
3636 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003637 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003638 * the occurrence.
3639 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003640
Dinh Nguyen47a16852014-04-14 14:13:34 -07003641 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003642 u8 idx;
3643 u32 epctrl;
3644 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003645 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003646 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003647
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003648 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyand8484552018-01-19 14:41:42 +04003649 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003650 /* Mask this interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003651 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003652 gintmsk &= ~GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003653 dwc2_writel(hsotg, gintmsk, GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003654
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003655 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003656 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003657 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003658 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003659 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyand8484552018-01-19 14:41:42 +04003660 continue;
3661
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003662 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003663
Razmik Karapetyand8484552018-01-19 14:41:42 +04003664 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003665 epctrl |= DXEPCTL_SNAK;
3666 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003667 dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003668 }
3669 }
3670
3671 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003672 }
3673
Dinh Nguyen47a16852014-04-14 14:13:34 -07003674 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003675 dev_info(hsotg->dev, "GINNakEff triggered\n");
3676
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003677 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003678
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003679 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003680 }
3681
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003682 if (gintsts & GINTSTS_INCOMPL_SOIN)
3683 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003684
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003685 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3686 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003687
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003688 /*
3689 * if we've had fifo events, we should try and go around the
3690 * loop again to see if there's any point in returning yet.
3691 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003692
3693 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003694 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003695
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003696 spin_unlock(&hsotg->lock);
3697
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003698 return IRQ_HANDLED;
3699}
3700
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003701static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3702 struct dwc2_hsotg_ep *hs_ep)
3703{
3704 u32 epctrl_reg;
3705 u32 epint_reg;
3706
3707 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3708 DOEPCTL(hs_ep->index);
3709 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3710 DOEPINT(hs_ep->index);
3711
3712 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3713 hs_ep->name);
3714
3715 if (hs_ep->dir_in) {
3716 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003717 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003718 /* Wait for Nak effect */
3719 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3720 DXEPINT_INEPNAKEFF, 100))
3721 dev_warn(hsotg->dev,
3722 "%s: timeout DIEPINT.NAKEFF\n",
3723 __func__);
3724 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003725 dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003726 /* Wait for Nak effect */
3727 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3728 GINTSTS_GINNAKEFF, 100))
3729 dev_warn(hsotg->dev,
3730 "%s: timeout GINTSTS.GINNAKEFF\n",
3731 __func__);
3732 }
3733 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003734 if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF))
3735 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003736
3737 /* Wait for global nak to take effect */
3738 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3739 GINTSTS_GOUTNAKEFF, 100))
3740 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3741 __func__);
3742 }
3743
3744 /* Disable ep */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003745 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003746
3747 /* Wait for ep to be disabled */
3748 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3749 dev_warn(hsotg->dev,
3750 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3751
3752 /* Clear EPDISBLD interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003753 dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003754
3755 if (hs_ep->dir_in) {
3756 unsigned short fifo_index;
3757
3758 if (hsotg->dedicated_fifos || hs_ep->periodic)
3759 fifo_index = hs_ep->fifo_index;
3760 else
3761 fifo_index = 0;
3762
3763 /* Flush TX FIFO */
3764 dwc2_flush_tx_fifo(hsotg, fifo_index);
3765
3766 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3767 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003768 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003769
3770 } else {
3771 /* Remove global NAKs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003772 dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003773 }
3774}
3775
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003776/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003777 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003778 * @ep: The USB endpint to configure
3779 * @desc: The USB endpoint descriptor to configure with.
3780 *
3781 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003782 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003783static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003784 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003785{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003786 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003787 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003788 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003789 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003790 u32 epctrl_reg;
3791 u32 epctrl;
3792 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003793 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003794 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003795 unsigned int dir_in;
3796 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003797 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003798 unsigned char ep_type;
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +04003799 int desc_num;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003800
3801 dev_dbg(hsotg->dev,
3802 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3803 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3804 desc->wMaxPacketSize, desc->bInterval);
3805
3806 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003807 if (index == 0) {
3808 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3809 return -EINVAL;
3810 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003811
3812 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3813 if (dir_in != hs_ep->dir_in) {
3814 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3815 return -EINVAL;
3816 }
3817
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003818 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003819 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003820 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003821
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003822 /* ISOC IN in DDMA supported bInterval up to 10 */
3823 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3824 dir_in && desc->bInterval > 10) {
3825 dev_err(hsotg->dev,
3826 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3827 return -EINVAL;
3828 }
3829
3830 /* High bandwidth ISOC OUT in DDMA not supported */
3831 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3832 !dir_in && mc > 1) {
3833 dev_err(hsotg->dev,
3834 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3835 return -EINVAL;
3836 }
3837
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003838 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003839
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003840 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003841 epctrl = dwc2_readl(hsotg, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003842
3843 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3844 __func__, epctrl, epctrl_reg);
3845
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +04003846 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC)
3847 desc_num = MAX_DMA_DESC_NUM_HS_ISOC;
3848 else
3849 desc_num = MAX_DMA_DESC_NUM_GENERIC;
3850
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003851 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003852 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3853 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +04003854 desc_num * sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003855 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003856 if (!hs_ep->desc_list) {
3857 ret = -ENOMEM;
3858 goto error2;
3859 }
3860 }
3861
Lukasz Majewski22258f42012-06-14 10:02:24 +02003862 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003863
Dinh Nguyen47a16852014-04-14 14:13:34 -07003864 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3865 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003866
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003867 /*
3868 * mark the endpoint as active, otherwise the core may ignore
3869 * transactions entirely for this endpoint
3870 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003871 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003872
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003873 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003874 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003875
3876 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003877 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003878 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003879 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003880 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003881
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003882 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003883 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003884 epctrl |= DXEPCTL_EPTYPE_ISO;
3885 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003886 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003887 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003888 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003889 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003890 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003891 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003892 hs_ep->periodic = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003893 mask = dwc2_readl(hsotg, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003894 mask |= DIEPMSK_NAKMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003895 dwc2_writel(hsotg, mask, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003896 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003897 mask = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003898 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003899 dwc2_writel(hsotg, mask, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003900 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003901 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003902
3903 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003904 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003905 break;
3906
3907 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003908 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003909 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003910
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003911 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3912 hs_ep->interval = 1 << (desc->bInterval - 1);
3913
Dinh Nguyen47a16852014-04-14 14:13:34 -07003914 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003915 break;
3916
3917 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003918 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003919 break;
3920 }
3921
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003922 /*
3923 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003924 * a unique tx-fifo even if it is non-periodic.
3925 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003926 if (dir_in && hsotg->dedicated_fifos) {
John Keeping39a80bb2019-12-19 11:34:31 +00003927 unsigned fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003928 u32 fifo_index = 0;
3929 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08003930
3931 size = hs_ep->ep.maxpacket * hs_ep->mc;
John Keeping39a80bb2019-12-19 11:34:31 +00003932 for (i = 1; i <= fifo_count; ++i) {
John Youn9da51972017-01-17 20:30:27 -08003933 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02003934 continue;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003935 val = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08003936 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003937 if (val < size)
3938 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003939 /* Search for smallest acceptable fifo */
3940 if (val < fifo_size) {
3941 fifo_size = val;
3942 fifo_index = i;
3943 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003944 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003945 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003946 dev_err(hsotg->dev,
3947 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303948 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003949 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303950 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003951 hsotg->fifo_map |= 1 << fifo_index;
3952 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3953 hs_ep->fifo_index = fifo_index;
3954 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003955 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003956
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003957 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003958 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003959 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003960
Artur Petrosyan52953222018-04-16 08:45:31 -04003961 /* WA for Full speed ISOC IN in DDMA mode.
3962 * By Clear NAK status of EP, core will send ZLP
3963 * to IN token and assert NAK interrupt relying
3964 * on TxFIFO status only
3965 */
3966
3967 if (hsotg->gadget.speed == USB_SPEED_FULL &&
3968 hs_ep->isochronous && dir_in) {
3969 /* The WA applies only to core versions from 2.72a
3970 * to 4.00a (including both). Also for FS_IOT_1.00a
3971 * and HS_IOT_1.00a.
3972 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003973 u32 gsnpsid = dwc2_readl(hsotg, GSNPSID);
Artur Petrosyan52953222018-04-16 08:45:31 -04003974
3975 if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
3976 gsnpsid <= DWC2_CORE_REV_4_00a) ||
3977 gsnpsid == DWC2_FS_IOT_REV_1_00a ||
3978 gsnpsid == DWC2_HS_IOT_REV_1_00a)
3979 epctrl |= DXEPCTL_CNAK;
3980 }
3981
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003982 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3983 __func__, epctrl);
3984
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003985 dwc2_writel(hsotg, epctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003986 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003987 __func__, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003988
3989 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003990 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003991
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003992error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003993 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003994
3995error2:
3996 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Minas Harutyunyan1a7be0f2019-03-18 14:24:30 +04003997 dmam_free_coherent(hsotg->dev, desc_num *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003998 sizeof(struct dwc2_dma_desc),
3999 hs_ep->desc_list, hs_ep->desc_list_dma);
4000 hs_ep->desc_list = NULL;
4001 }
4002
Julia Lawall19c190f2010-03-29 17:36:44 +02004003 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004004}
4005
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004006/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004007 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004008 * @ep: The endpoint to disable.
4009 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004010static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004011{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004012 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004013 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004014 int dir_in = hs_ep->dir_in;
4015 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004016 u32 epctrl_reg;
4017 u32 ctrl;
4018
Marek Szyprowski1e011292014-09-09 10:44:54 +02004019 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004020
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004021 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004022 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4023 return -EINVAL;
4024 }
4025
John Stultz9b4810922017-10-23 14:32:49 -07004026 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4027 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4028 return -EINVAL;
4029 }
4030
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02004031 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004032
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004033 ctrl = dwc2_readl(hsotg, epctrl_reg);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08004034
4035 if (ctrl & DXEPCTL_EPENA)
4036 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4037
Dinh Nguyen47a16852014-04-14 14:13:34 -07004038 ctrl &= ~DXEPCTL_EPENA;
4039 ctrl &= ~DXEPCTL_USBACTEP;
4040 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004041
4042 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004043 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004044
4045 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004046 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004047
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01004048 /* terminate all requests with shutdown */
4049 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4050
Robert Baldyga1c07b202016-08-29 13:39:00 -07004051 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4052 hs_ep->fifo_index = 0;
4053 hs_ep->fifo_size = 0;
4054
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004055 return 0;
4056}
4057
Minas Harutyunyanb544a682018-12-10 18:09:32 +04004058static int dwc2_hsotg_ep_disable_lock(struct usb_ep *ep)
4059{
4060 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4061 struct dwc2_hsotg *hsotg = hs_ep->parent;
4062 unsigned long flags;
4063 int ret;
4064
4065 spin_lock_irqsave(&hsotg->lock, flags);
4066 ret = dwc2_hsotg_ep_disable(ep);
4067 spin_unlock_irqrestore(&hsotg->lock, flags);
4068 return ret;
4069}
4070
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004071/**
4072 * on_list - check request is on the given endpoint
4073 * @ep: The endpoint to check.
4074 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004075 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004076static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004077{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004078 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004079
4080 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4081 if (req == test)
4082 return true;
4083 }
4084
4085 return false;
4086}
4087
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004088/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004089 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004090 * @ep: The endpoint to dequeue.
4091 * @req: The request to be removed from a queue.
4092 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004093static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004094{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004095 struct dwc2_hsotg_req *hs_req = our_req(req);
4096 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004097 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004098 unsigned long flags;
4099
Marek Szyprowski1e011292014-09-09 10:44:54 +02004100 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004101
Lukasz Majewski22258f42012-06-14 10:02:24 +02004102 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004103
4104 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004105 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004106 return -EINVAL;
4107 }
4108
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004109 /* Dequeue already started request */
4110 if (req == &hs_ep->req->req)
4111 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4112
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004113 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004114 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004115
4116 return 0;
4117}
4118
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004119/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004120 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004121 * @ep: The endpoint to set halt.
4122 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004123 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4124 * the endpoint is busy processing requests.
4125 *
4126 * We need to stall the endpoint immediately if request comes from set_feature
4127 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004128 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004129static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004130{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004131 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004132 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004133 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004134 u32 epreg;
4135 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004136 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004137
4138 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4139
Robert Baldygac9f721b2014-01-14 08:36:00 +01004140 if (index == 0) {
4141 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004142 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004143 else
4144 dev_warn(hs->dev,
4145 "%s: can't clear halt on ep0\n", __func__);
4146 return 0;
4147 }
4148
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004149 if (hs_ep->isochronous) {
4150 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4151 return -EINVAL;
4152 }
4153
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004154 if (!now && value && !list_empty(&hs_ep->queue)) {
4155 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4156 ep->name);
4157 return -EAGAIN;
4158 }
4159
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004160 if (hs_ep->dir_in) {
4161 epreg = DIEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004162 epctl = dwc2_readl(hs, epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004163
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004164 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004165 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004166 if (epctl & DXEPCTL_EPENA)
4167 epctl |= DXEPCTL_EPDIS;
4168 } else {
4169 epctl &= ~DXEPCTL_STALL;
4170 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4171 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004172 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004173 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004174 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004175 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004176 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004177 epreg = DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004178 epctl = dwc2_readl(hs, epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004179
John Youn34c08872017-01-17 20:31:43 -08004180 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004181 epctl |= DXEPCTL_STALL;
John Youn34c08872017-01-17 20:31:43 -08004182 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004183 epctl &= ~DXEPCTL_STALL;
4184 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4185 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004186 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004187 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004188 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004189 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004190 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004191
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004192 hs_ep->halted = value;
4193
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004194 return 0;
4195}
4196
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004197/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004198 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004199 * @ep: The endpoint to set halt.
4200 * @value: Set or unset the halt.
4201 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004202static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004203{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004204 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004205 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004206 unsigned long flags = 0;
4207 int ret = 0;
4208
4209 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004210 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004211 spin_unlock_irqrestore(&hs->lock, flags);
4212
4213 return ret;
4214}
4215
Bhumika Goyalebce5612017-08-12 17:34:55 +05304216static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004217 .enable = dwc2_hsotg_ep_enable,
Minas Harutyunyanb544a682018-12-10 18:09:32 +04004218 .disable = dwc2_hsotg_ep_disable_lock,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004219 .alloc_request = dwc2_hsotg_ep_alloc_request,
4220 .free_request = dwc2_hsotg_ep_free_request,
4221 .queue = dwc2_hsotg_ep_queue_lock,
4222 .dequeue = dwc2_hsotg_ep_dequeue,
4223 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004224 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004225};
4226
4227/**
John Youn9da51972017-01-17 20:30:27 -08004228 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004229 * @hsotg: The driver state
4230 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004231static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004232{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004233 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004234 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004235 /* unmask subset of endpoint interrupts */
4236
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004237 dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004238 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004239 DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004240
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004241 dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004242 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004243 DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004244
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004245 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004246
4247 /* Be in disconnected state until gadget is registered */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004248 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004249
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004250 /* setup fifos */
4251
4252 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004253 dwc2_readl(hsotg, GRXFSIZ),
4254 dwc2_readl(hsotg, GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004255
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004256 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004257
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004258 /* keep other bits untouched (so e.g. forced modes are not lost) */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004259 usbcfg = dwc2_readl(hsotg, GUSBCFG);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004260 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01004261 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004262
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004263 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004264 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004265 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4266 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004267 dwc2_writel(hsotg, usbcfg, GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004268
Gregory Herrerof5090042015-01-09 13:38:47 +01004269 if (using_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004270 dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004271}
4272
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004273/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004274 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004275 * @gadget: The usb gadget state
4276 * @driver: The usb gadget driver
4277 *
4278 * Perform initialization to prepare udc device and driver
4279 * to work.
4280 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004281static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004282 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004283{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004284 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004285 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004286 int ret;
4287
4288 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004289 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004290 return -ENODEV;
4291 }
4292
4293 if (!driver) {
4294 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4295 return -EINVAL;
4296 }
4297
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004298 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004299 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004300
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004301 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004302 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4303 return -EINVAL;
4304 }
4305
4306 WARN_ON(hsotg->driver);
4307
4308 driver->driver.bus = NULL;
4309 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004310 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004311 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4312
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004313 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4314 ret = dwc2_lowlevel_hw_enable(hsotg);
4315 if (ret)
4316 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004317 }
4318
Gregory Herrerof6c01592015-01-09 13:38:41 +01004319 if (!IS_ERR_OR_NULL(hsotg->uphy))
4320 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004321
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004322 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004323 if (dwc2_hw_is_device(hsotg)) {
4324 dwc2_hsotg_init(hsotg);
4325 dwc2_hsotg_core_init_disconnected(hsotg, false);
4326 }
4327
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004328 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004329 spin_unlock_irqrestore(&hsotg->lock, flags);
4330
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004331 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004332
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004333 return 0;
4334
4335err:
4336 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004337 return ret;
4338}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004339
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004340/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004341 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004342 * @gadget: The usb gadget state
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004343 *
4344 * Stop udc hw block and stay tunned for future transmissions
4345 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004346static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004347{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004348 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004349 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004350 int ep;
4351
4352 if (!hsotg)
4353 return -ENODEV;
4354
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004355 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004356 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4357 if (hsotg->eps_in[ep])
Minas Harutyunyanb544a682018-12-10 18:09:32 +04004358 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004359 if (hsotg->eps_out[ep])
Minas Harutyunyanb544a682018-12-10 18:09:32 +04004360 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004361 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004362
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004363 spin_lock_irqsave(&hsotg->lock, flags);
4364
Marek Szyprowski32805c32014-10-20 12:45:33 +02004365 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004366 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004367 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004368
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004369 spin_unlock_irqrestore(&hsotg->lock, flags);
4370
Gregory Herrerof6c01592015-01-09 13:38:41 +01004371 if (!IS_ERR_OR_NULL(hsotg->uphy))
4372 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004373
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004374 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4375 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004376
4377 return 0;
4378}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004379
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004380/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004381 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004382 * @gadget: The usb gadget state
4383 *
4384 * Read the {micro} frame number
4385 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004386static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004387{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004388 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004389}
4390
Lukasz Majewskia188b682012-06-22 09:29:56 +02004391/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004392 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004393 * @gadget: The usb gadget state
4394 * @is_on: Current state of the USB PHY
4395 *
4396 * Connect/Disconnect the USB PHY pullup
4397 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004398static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004399{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004400 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004401 unsigned long flags = 0;
4402
Gregory Herrero77ba9112015-09-29 12:08:19 +02004403 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004404 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004405
4406 /* Don't modify pullup state while in host mode */
4407 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4408 hsotg->enabled = is_on;
4409 return 0;
4410 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004411
4412 spin_lock_irqsave(&hsotg->lock, flags);
4413 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004414 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004415 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004416 /* Enable ACG feature in device mode,if supported */
4417 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004418 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004419 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004420 dwc2_hsotg_core_disconnect(hsotg);
4421 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004422 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004423 }
4424
4425 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4426 spin_unlock_irqrestore(&hsotg->lock, flags);
4427
4428 return 0;
4429}
4430
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004431static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004432{
4433 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4434 unsigned long flags;
4435
4436 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4437 spin_lock_irqsave(&hsotg->lock, flags);
4438
Gregory Herrero61f72232015-09-29 12:08:28 +02004439 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004440 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004441 * before being initialized / de-initialized
4442 */
4443 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004444 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004445
Gregory Herrero83d98222015-01-09 13:39:02 +01004446 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004447 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004448
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004449 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004450 if (hsotg->enabled) {
4451 /* Enable ACG feature in device mode,if supported */
4452 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004453 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004454 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004455 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004456 dwc2_hsotg_core_disconnect(hsotg);
4457 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004458 }
4459
4460 spin_unlock_irqrestore(&hsotg->lock, flags);
4461 return 0;
4462}
4463
Gregory Herrero596d6962015-01-09 13:39:08 +01004464/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004465 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004466 * @gadget: The usb gadget state
4467 * @mA: Amount of current
4468 *
4469 * Report how much power the device may consume to the phy.
4470 */
John Youn9da51972017-01-17 20:30:27 -08004471static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004472{
4473 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4474
4475 if (IS_ERR_OR_NULL(hsotg->uphy))
4476 return -ENOTSUPP;
4477 return usb_phy_set_power(hsotg->uphy, mA);
4478}
4479
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004480static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4481 .get_frame = dwc2_hsotg_gadget_getframe,
4482 .udc_start = dwc2_hsotg_udc_start,
4483 .udc_stop = dwc2_hsotg_udc_stop,
4484 .pullup = dwc2_hsotg_pullup,
4485 .vbus_session = dwc2_hsotg_vbus_session,
4486 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004487};
4488
4489/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004490 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004491 * @hsotg: The device state.
4492 * @hs_ep: The endpoint to be initialised.
4493 * @epnum: The endpoint number
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004494 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004495 *
4496 * Initialise the given endpoint (as part of the probe and device state
4497 * creation) to give to the gadget driver. Setup the endpoint name, any
4498 * direction information and other state that may be required.
4499 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004500static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004501 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004502 int epnum,
4503 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004504{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004505 char *dir;
4506
4507 if (epnum == 0)
4508 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004509 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004510 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004511 else
4512 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004513
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004514 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004515 hs_ep->index = epnum;
4516
4517 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4518
4519 INIT_LIST_HEAD(&hs_ep->queue);
4520 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4521
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004522 /* add to the list of endpoints known by the gadget driver */
4523 if (epnum)
4524 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4525
4526 hs_ep->parent = hsotg;
4527 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004528
4529 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4530 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4531 else
4532 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4533 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004534 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004535
Robert Baldyga29545222015-07-31 16:00:18 +02004536 if (epnum == 0) {
4537 hs_ep->ep.caps.type_control = true;
4538 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004539 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4540 hs_ep->ep.caps.type_iso = true;
4541 hs_ep->ep.caps.type_bulk = true;
4542 }
Robert Baldyga29545222015-07-31 16:00:18 +02004543 hs_ep->ep.caps.type_int = true;
4544 }
4545
4546 if (dir_in)
4547 hs_ep->ep.caps.dir_in = true;
4548 else
4549 hs_ep->ep.caps.dir_out = true;
4550
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004551 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004552 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004553 * to be something valid.
4554 */
4555
4556 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004557 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004558
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004559 if (dir_in)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004560 dwc2_writel(hsotg, next, DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004561 else
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004562 dwc2_writel(hsotg, next, DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004563 }
4564}
4565
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004566/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004567 * dwc2_hsotg_hw_cfg - read HW configuration registers
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004568 * @hsotg: Programming view of the DWC_otg controller
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004569 *
4570 * Read the USB core HW configuration registers
4571 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004572static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004573{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004574 u32 cfg;
4575 u32 ep_type;
4576 u32 i;
4577
Ben Dooks10aebc72010-07-19 09:40:44 +01004578 /* check hardware configuration */
4579
John Youn43e90342015-12-17 11:17:45 -08004580 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4581
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004582 /* Add ep0 */
4583 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004584
John Younb98866c2017-01-17 20:31:58 -08004585 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4586 sizeof(struct dwc2_hsotg_ep),
4587 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004588 if (!hsotg->eps_in[0])
4589 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004590 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004591 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004592
John Youn43e90342015-12-17 11:17:45 -08004593 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004594 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004595 ep_type = cfg & 3;
4596 /* Direction in or both */
4597 if (!(ep_type & 2)) {
4598 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004599 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004600 if (!hsotg->eps_in[i])
4601 return -ENOMEM;
4602 }
4603 /* Direction out or both */
4604 if (!(ep_type & 1)) {
4605 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004606 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004607 if (!hsotg->eps_out[i])
4608 return -ENOMEM;
4609 }
4610 }
4611
John Youn43e90342015-12-17 11:17:45 -08004612 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4613 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004614
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004615 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4616 hsotg->num_of_eps,
4617 hsotg->dedicated_fifos ? "dedicated" : "shared",
4618 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004619 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004620}
4621
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004622/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004623 * dwc2_hsotg_dump - dump state of the udc
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004624 * @hsotg: Programming view of the DWC_otg controller
4625 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004626 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004627static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004628{
Mark Brown83a01802011-06-01 17:16:15 +01004629#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004630 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004631 u32 val;
4632 int idx;
4633
4634 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004635 dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL),
4636 dwc2_readl(hsotg, DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004637
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004638 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004639 dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004640
4641 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004642 dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004643
4644 /* show periodic fifo settings */
4645
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004646 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004647 val = dwc2_readl(hsotg, DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004648 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004649 val >> FIFOSIZE_DEPTH_SHIFT,
4650 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004651 }
4652
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004653 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004654 dev_info(dev,
4655 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004656 dwc2_readl(hsotg, DIEPCTL(idx)),
4657 dwc2_readl(hsotg, DIEPTSIZ(idx)),
4658 dwc2_readl(hsotg, DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004659
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004660 val = dwc2_readl(hsotg, DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004661 dev_info(dev,
4662 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004663 idx, dwc2_readl(hsotg, DOEPCTL(idx)),
4664 dwc2_readl(hsotg, DOEPTSIZ(idx)),
4665 dwc2_readl(hsotg, DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004666 }
4667
4668 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004669 dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004670#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004671}
4672
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004673/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004674 * dwc2_gadget_init - init function for gadget
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004675 * @hsotg: Programming view of the DWC_otg controller
4676 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004677 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004678int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004679{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004680 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004681 int epnum;
4682 int ret;
John Youn43e90342015-12-17 11:17:45 -08004683
Gregory Herrero0a176272015-01-09 13:38:52 +01004684 /* Dump fifo information */
4685 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004686 hsotg->params.g_np_tx_fifo_size);
4687 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004688
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004689 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004690 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004691 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004692 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004693
4694 if (hsotg->params.lpm)
4695 hsotg->gadget.lpm_capable = true;
4696
Gregory Herrero097ee662015-04-29 22:09:10 +02004697 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4698 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004699 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4700 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004701
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004702 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004703 if (ret) {
4704 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004705 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004706 }
4707
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004708 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4709 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004710 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004711 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004712
4713 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4714 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004715 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004716 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004717
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004718 if (using_desc_dma(hsotg)) {
4719 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4720 if (ret < 0)
4721 return ret;
4722 }
4723
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004724 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4725 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004726 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004727 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004728 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004729 }
4730
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004731 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4732
4733 if (hsotg->num_of_eps == 0) {
4734 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004735 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004736 }
4737
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004738 /* setup endpoint information */
4739
4740 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004741 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004742
4743 /* allocate EP0 request */
4744
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004745 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004746 GFP_KERNEL);
4747 if (!hsotg->ctrl_req) {
4748 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004749 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004750 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004751
4752 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004753 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4754 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004755 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004756 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004757 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004758 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004759 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004760 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004761
Dinh Nguyen117777b2014-11-11 11:13:34 -06004762 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004763 if (ret) {
4764 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep,
4765 hsotg->ctrl_req);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004766 return ret;
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004767 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004768 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004769
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004770 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004771}
4772
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004773/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004774 * dwc2_hsotg_remove - remove function for hsotg driver
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004775 * @hsotg: Programming view of the DWC_otg controller
4776 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004777 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004778int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004779{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004780 usb_del_gadget_udc(&hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004781 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004782
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004783 return 0;
4784}
4785
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004786int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004787{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004788 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004789
Gregory Herrero9e779772015-04-29 22:09:07 +02004790 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004791 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004792
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004793 if (hsotg->driver) {
4794 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004795
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004796 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4797 hsotg->driver->driver.name);
4798
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004799 spin_lock_irqsave(&hsotg->lock, flags);
4800 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004801 dwc2_hsotg_core_disconnect(hsotg);
4802 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004803 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4804 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004805
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004806 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4807 if (hsotg->eps_in[ep])
Minas Harutyunyanb544a682018-12-10 18:09:32 +04004808 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004809 if (hsotg->eps_out[ep])
Minas Harutyunyanb544a682018-12-10 18:09:32 +04004810 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004811 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004812 }
4813
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004814 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004815}
4816
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004817int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004818{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004819 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004820
Gregory Herrero9e779772015-04-29 22:09:07 +02004821 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004822 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004823
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004824 if (hsotg->driver) {
4825 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4826 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004827
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004828 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004829 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004830 if (hsotg->enabled) {
4831 /* Enable ACG feature in device mode,if supported */
4832 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004833 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004834 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004835 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004836 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004837
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004838 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004839}
John Youn58e52ff6a2016-02-23 19:54:57 -08004840
4841/**
4842 * dwc2_backup_device_registers() - Backup controller device registers.
4843 * When suspending usb bus, registers needs to be backuped
4844 * if controller power is disabled once suspended.
4845 *
4846 * @hsotg: Programming view of the DWC_otg controller
4847 */
4848int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4849{
4850 struct dwc2_dregs_backup *dr;
4851 int i;
4852
4853 dev_dbg(hsotg->dev, "%s\n", __func__);
4854
4855 /* Backup dev regs */
4856 dr = &hsotg->dr_backup;
4857
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004858 dr->dcfg = dwc2_readl(hsotg, DCFG);
4859 dr->dctl = dwc2_readl(hsotg, DCTL);
4860 dr->daintmsk = dwc2_readl(hsotg, DAINTMSK);
4861 dr->diepmsk = dwc2_readl(hsotg, DIEPMSK);
4862 dr->doepmsk = dwc2_readl(hsotg, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08004863
4864 for (i = 0; i < hsotg->num_of_eps; i++) {
4865 /* Backup IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004866 dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004867
4868 /* Ensure DATA PID is correctly configured */
4869 if (dr->diepctl[i] & DXEPCTL_DPID)
4870 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4871 else
4872 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4873
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004874 dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i));
4875 dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004876
4877 /* Backup OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004878 dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004879
4880 /* Ensure DATA PID is correctly configured */
4881 if (dr->doepctl[i] & DXEPCTL_DPID)
4882 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4883 else
4884 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4885
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004886 dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i));
4887 dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i));
4888 dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004889 }
4890 dr->valid = true;
4891 return 0;
4892}
4893
4894/**
4895 * dwc2_restore_device_registers() - Restore controller device registers.
4896 * When resuming usb bus, device registers needs to be restored
4897 * if controller power were disabled.
4898 *
4899 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004900 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
4901 *
4902 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08004903 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004904int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08004905{
4906 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08004907 int i;
4908
4909 dev_dbg(hsotg->dev, "%s\n", __func__);
4910
4911 /* Restore dev regs */
4912 dr = &hsotg->dr_backup;
4913 if (!dr->valid) {
4914 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4915 __func__);
4916 return -EINVAL;
4917 }
4918 dr->valid = false;
4919
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004920 if (!remote_wakeup)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004921 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004922
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004923 dwc2_writel(hsotg, dr->daintmsk, DAINTMSK);
4924 dwc2_writel(hsotg, dr->diepmsk, DIEPMSK);
4925 dwc2_writel(hsotg, dr->doepmsk, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08004926
4927 for (i = 0; i < hsotg->num_of_eps; i++) {
4928 /* Restore IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004929 dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i));
4930 dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i));
4931 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004932 /** WA for enabled EPx's IN in DDMA mode. On entering to
4933 * hibernation wrong value read and saved from DIEPDMAx,
4934 * as result BNA interrupt asserted on hibernation exit
4935 * by restoring from saved area.
4936 */
4937 if (hsotg->params.g_dma_desc &&
4938 (dr->diepctl[i] & DXEPCTL_EPENA))
4939 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004940 dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i));
4941 dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004942 /* Restore OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004943 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004944 /* WA for enabled EPx's OUT in DDMA mode. On entering to
4945 * hibernation wrong value read and saved from DOEPDMAx,
4946 * as result BNA interrupt asserted on hibernation exit
4947 * by restoring from saved area.
4948 */
4949 if (hsotg->params.g_dma_desc &&
4950 (dr->doepctl[i] & DXEPCTL_EPENA))
4951 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004952 dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i));
4953 dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004954 }
4955
John Youn58e52ff6a2016-02-23 19:54:57 -08004956 return 0;
4957}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04004958
4959/**
4960 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
4961 *
4962 * @hsotg: Programming view of DWC_otg controller
4963 *
4964 */
4965void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
4966{
4967 u32 val;
4968
4969 if (!hsotg->params.lpm)
4970 return;
4971
4972 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
4973 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
4974 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
4975 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
4976 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004977 dwc2_writel(hsotg, val, GLPMCFG);
4978 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG));
Sevak Arakelyan21b03402018-01-24 17:43:32 +04004979}
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04004980
4981/**
4982 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
4983 *
4984 * @hsotg: Programming view of the DWC_otg controller
4985 *
4986 * Return non-zero if failed to enter to hibernation.
4987 */
4988int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
4989{
4990 u32 gpwrdn;
4991 int ret = 0;
4992
4993 /* Change to L2(suspend) state */
4994 hsotg->lx_state = DWC2_L2;
4995 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
4996 ret = dwc2_backup_global_registers(hsotg);
4997 if (ret) {
4998 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
4999 __func__);
5000 return ret;
5001 }
5002 ret = dwc2_backup_device_registers(hsotg);
5003 if (ret) {
5004 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
5005 __func__);
5006 return ret;
5007 }
5008
5009 gpwrdn = GPWRDN_PWRDNRSTN;
5010 gpwrdn |= GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005011 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005012 udelay(10);
5013
5014 /* Set flag to indicate that we are in hibernation */
5015 hsotg->hibernated = 1;
5016
5017 /* Enable interrupts from wake up logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005018 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005019 gpwrdn |= GPWRDN_PMUINTSEL;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005020 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005021 udelay(10);
5022
5023 /* Unmask device mode interrupts in GPWRDN */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005024 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005025 gpwrdn |= GPWRDN_RST_DET_MSK;
5026 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5027 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005028 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005029 udelay(10);
5030
5031 /* Enable Power Down Clamp */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005032 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005033 gpwrdn |= GPWRDN_PWRDNCLMP;
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 /* Switch off VDD */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005038 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005039 gpwrdn |= GPWRDN_PWRDNSWTCH;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005040 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005041 udelay(10);
5042
5043 /* Save gpwrdn register for further usage if stschng interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005044 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005045 dev_dbg(hsotg->dev, "Hibernation completed\n");
5046
5047 return ret;
5048}
5049
5050/**
5051 * dwc2_gadget_exit_hibernation()
5052 * This function is for exiting from Device mode hibernation by host initiated
5053 * resume/reset and device initiated remote-wakeup.
5054 *
5055 * @hsotg: Programming view of the DWC_otg controller
5056 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04005057 * @reset: indicates whether resume is initiated by Reset.
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005058 *
5059 * Return non-zero if failed to exit from hibernation.
5060 */
5061int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
5062 int rem_wakeup, int reset)
5063{
5064 u32 pcgcctl;
5065 u32 gpwrdn;
5066 u32 dctl;
5067 int ret = 0;
5068 struct dwc2_gregs_backup *gr;
5069 struct dwc2_dregs_backup *dr;
5070
5071 gr = &hsotg->gr_backup;
5072 dr = &hsotg->dr_backup;
5073
5074 if (!hsotg->hibernated) {
5075 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5076 return 1;
5077 }
5078 dev_dbg(hsotg->dev,
5079 "%s: called with rem_wakeup = %d reset = %d\n",
5080 __func__, rem_wakeup, reset);
5081
5082 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5083
5084 if (!reset) {
5085 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005086 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005087 }
5088
5089 /* De-assert Restore */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005090 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005091 gpwrdn &= ~GPWRDN_RESTORE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005092 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005093 udelay(10);
5094
5095 if (!rem_wakeup) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005096 pcgcctl = dwc2_readl(hsotg, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005097 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005098 dwc2_writel(hsotg, pcgcctl, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005099 }
5100
5101 /* Restore GUSBCFG, DCFG and DCTL */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005102 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5103 dwc2_writel(hsotg, dr->dcfg, DCFG);
5104 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005105
5106 /* De-assert Wakeup Logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005107 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005108 gpwrdn &= ~GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005109 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005110
5111 if (rem_wakeup) {
5112 udelay(10);
5113 /* Start Remote Wakeup Signaling */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005114 dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005115 } else {
5116 udelay(50);
5117 /* Set Device programming done bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005118 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005119 dctl |= DCTL_PWRONPRGDONE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005120 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005121 }
5122 /* Wait for interrupts which must be cleared */
5123 mdelay(2);
5124 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005125 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005126
5127 /* Restore global registers */
5128 ret = dwc2_restore_global_registers(hsotg);
5129 if (ret) {
5130 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5131 __func__);
5132 return ret;
5133 }
5134
5135 /* Restore device registers */
5136 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5137 if (ret) {
5138 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5139 __func__);
5140 return ret;
5141 }
5142
5143 if (rem_wakeup) {
5144 mdelay(10);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005145 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005146 dctl &= ~DCTL_RMTWKUPSIG;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005147 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005148 }
5149
5150 hsotg->hibernated = 0;
5151 hsotg->lx_state = DWC2_L0;
5152 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5153
5154 return ret;
5155}