blob: 2e2f9cbf6a3d603739e4e7c1f190608b5efc1aa5 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002/*
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09003 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01006 * Copyright 2008 Openmoko, Inc.
7 * Copyright 2008 Simtec Electronics
8 * Ben Dooks <ben@simtec.co.uk>
9 * http://armlinux.simtec.co.uk/
10 *
11 * S3C USB2.0 High-speed / OtG driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020012 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010013
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/spinlock.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
Marek Szyprowski7ad80962014-11-21 15:14:48 +010020#include <linux/mutex.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010021#include <linux/seq_file.h>
22#include <linux/delay.h>
23#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020025#include <linux/of_platform.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010026
27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053029#include <linux/usb/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010030
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070031#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060032#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010033
34/* conversion functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050035static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010036{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050037 return container_of(req, struct dwc2_hsotg_req, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010038}
39
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050040static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050042 return container_of(ep, struct dwc2_hsotg_ep, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010043}
44
Dinh Nguyen941fcce2014-11-11 11:13:33 -060045static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010046{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060047 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010048}
49
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040050static inline void dwc2_set_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010051{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040052 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) | val, offset);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010053}
54
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040055static inline void dwc2_clear_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010056{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040057 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) & ~val, offset);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010058}
59
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050060static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010061 u32 ep_index, u32 dir_in)
62{
63 if (dir_in)
64 return hsotg->eps_in[ep_index];
65 else
66 return hsotg->eps_out[ep_index];
67}
68
Mickael Maison997f4f82014-12-23 17:39:45 +010069/* forward declaration of functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050070static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010071
72/**
73 * using_dma - return the DMA status of the driver.
74 * @hsotg: The driver state.
75 *
76 * Return true if we're using DMA.
77 *
78 * Currently, we have the DMA support code worked into everywhere
79 * that needs it, but the AMBA DMA implementation in the hardware can
80 * only DMA from 32bit aligned addresses. This means that gadgets such
81 * as the CDC Ethernet cannot work as they often pass packets which are
82 * not 32bit aligned.
83 *
84 * Unfortunately the choice to use DMA or not is global to the controller
85 * and seems to be only settable when the controller is being put through
86 * a core reset. This means we either need to fix the gadgets to take
87 * account of DMA alignment, or add bounce buffers (yuerk).
88 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010089 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010090 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060091static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010092{
John Youn05ee7992016-11-03 17:56:05 -070093 return hsotg->params.g_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +010094}
95
Vahram Aharonyandec4b552016-11-09 19:27:48 -080096/*
97 * using_desc_dma - return the descriptor DMA status of the driver.
98 * @hsotg: The driver state.
99 *
100 * Return true if we're using descriptor DMA.
101 */
102static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
103{
104 return hsotg->params.g_dma_desc;
105}
106
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100107/**
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700108 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
109 * @hs_ep: The endpoint
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700110 *
111 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
112 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
113 */
114static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
115{
116 hs_ep->target_frame += hs_ep->interval;
117 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600118 hs_ep->frame_overrun = true;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700119 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
120 } else {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600121 hs_ep->frame_overrun = false;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700122 }
123}
124
125/**
Grigor Tovmasyan9d630b92018-08-29 21:00:03 +0400126 * dwc2_gadget_dec_frame_num_by_one - Decrements the targeted frame number
127 * by one.
128 * @hs_ep: The endpoint.
129 *
130 * This function used in service interval based scheduling flow to calculate
131 * descriptor frame number filed value. For service interval mode frame
132 * number in descriptor should point to last (u)frame in the interval.
133 *
134 */
135static inline void dwc2_gadget_dec_frame_num_by_one(struct dwc2_hsotg_ep *hs_ep)
136{
137 if (hs_ep->target_frame)
138 hs_ep->target_frame -= 1;
139 else
140 hs_ep->target_frame = DSTS_SOFFN_LIMIT;
141}
142
143/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500144 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100145 * @hsotg: The device state
146 * @ints: A bitmask of the interrupts to enable
147 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500148static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100149{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400150 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100151 u32 new_gsintmsk;
152
153 new_gsintmsk = gsintmsk | ints;
154
155 if (new_gsintmsk != gsintmsk) {
156 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400157 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100158 }
159}
160
161/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500162 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100163 * @hsotg: The device state
164 * @ints: A bitmask of the interrupts to enable
165 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500166static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100167{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400168 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100169 u32 new_gsintmsk;
170
171 new_gsintmsk = gsintmsk & ~ints;
172
173 if (new_gsintmsk != gsintmsk)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400174 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100175}
176
177/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500178 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100179 * @hsotg: The device state
180 * @ep: The endpoint index
181 * @dir_in: True if direction is in.
182 * @en: The enable value, true to enable
183 *
184 * Set or clear the mask for an individual endpoint's interrupt
185 * request.
186 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500187static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800188 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100189 unsigned int en)
190{
191 unsigned long flags;
192 u32 bit = 1 << ep;
193 u32 daint;
194
195 if (!dir_in)
196 bit <<= 16;
197
198 local_irq_save(flags);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400199 daint = dwc2_readl(hsotg, DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100200 if (en)
201 daint |= bit;
202 else
203 daint &= ~bit;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400204 dwc2_writel(hsotg, daint, DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100205 local_irq_restore(flags);
206}
207
208/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800209 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400210 *
211 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800212 */
213int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
214{
215 if (hsotg->hw_params.en_multiple_tx_fifo)
216 /* In dedicated FIFO mode we need count of IN EPs */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400217 return hsotg->hw_params.num_dev_in_eps;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800218 else
219 /* In shared FIFO mode we need count of Periodic IN EPs */
220 return hsotg->hw_params.num_dev_perio_in_ep;
221}
222
223/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800224 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
225 * device mode TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400226 *
227 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800228 */
229int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
230{
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800231 int addr;
232 int tx_addr_max;
233 u32 np_tx_fifo_size;
234
235 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
236 hsotg->params.g_np_tx_fifo_size);
237
238 /* Get Endpoint Info Control block size in DWORDs. */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400239 tx_addr_max = hsotg->hw_params.total_fifo_size;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800240
241 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
242 if (tx_addr_max <= addr)
243 return 0;
244
245 return tx_addr_max - addr;
246}
247
248/**
Grigor Tovmasyan187c5292018-08-29 21:02:57 +0400249 * dwc2_gadget_wkup_alert_handler - Handler for WKUP_ALERT interrupt
250 *
251 * @hsotg: Programming view of the DWC_otg controller
252 *
253 */
254static void dwc2_gadget_wkup_alert_handler(struct dwc2_hsotg *hsotg)
255{
256 u32 gintsts2;
257 u32 gintmsk2;
258
259 gintsts2 = dwc2_readl(hsotg, GINTSTS2);
260 gintmsk2 = dwc2_readl(hsotg, GINTMSK2);
261
262 if (gintsts2 & GINTSTS2_WKUP_ALERT_INT) {
263 dev_dbg(hsotg->dev, "%s: Wkup_Alert_Int\n", __func__);
Minas Harutyunyan87b6d2c2018-12-12 16:44:32 +0400264 dwc2_set_bit(hsotg, GINTSTS2, GINTSTS2_WKUP_ALERT_INT);
Artur Petrosyand64bc8e2018-11-02 11:29:48 -0400265 dwc2_set_bit(hsotg, DCTL, DCTL_RMTWKUPSIG);
Grigor Tovmasyan187c5292018-08-29 21:02:57 +0400266 }
267}
268
269/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800270 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
271 * TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400272 *
273 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800274 */
275int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
276{
277 int tx_fifo_count;
278 int tx_fifo_depth;
279
280 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
281
282 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
283
284 if (!tx_fifo_count)
285 return tx_fifo_depth;
286 else
287 return tx_fifo_depth / tx_fifo_count;
288}
289
290/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500291 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100292 * @hsotg: The device instance.
293 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500294static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100295{
John Youn2317eac2016-10-17 17:36:23 -0700296 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100297 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100298 int timeout;
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +0400299
Ben Dooks0f002d22010-05-25 05:36:50 +0100300 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700301 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100302
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100303 /* Reset fifo map if not correctly cleared during previous session */
304 WARN_ON(hsotg->fifo_map);
305 hsotg->fifo_map = 0;
306
Gregory Herrero0a176272015-01-09 13:38:52 +0100307 /* set RX/NPTX FIFO sizes */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400308 dwc2_writel(hsotg, hsotg->params.g_rx_fifo_size, GRXFSIZ);
309 dwc2_writel(hsotg, (hsotg->params.g_rx_fifo_size <<
310 FIFOSIZE_STARTADDR_SHIFT) |
John Youn05ee7992016-11-03 17:56:05 -0700311 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400312 GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100313
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200314 /*
315 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100316 * block have overlapping default addresses. This also ensures
317 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200318 * known values.
319 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100320
321 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700322 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100323
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200324 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100325 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200326 * them to endpoints dynamically according to maxpacket size value of
327 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200328 */
John Youn2317eac2016-10-17 17:36:23 -0700329 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700330 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700331 continue;
332 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700333 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
334 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700335 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700336 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100337
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400338 dwc2_writel(hsotg, val, DPTXFSIZN(ep));
339 val = dwc2_readl(hsotg, DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100340 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100341
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400342 dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size |
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800343 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400344 GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200345 /*
346 * according to p428 of the design guide, we need to ensure that
347 * all fifos are flushed before continuing
348 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100349
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400350 dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
351 GRSTCTL_RXFFLSH, GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100352
353 /* wait until the fifos are both flushed */
354 timeout = 100;
355 while (1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400356 val = dwc2_readl(hsotg, GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100357
Dinh Nguyen47a16852014-04-14 14:13:34 -0700358 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100359 break;
360
361 if (--timeout == 0) {
362 dev_err(hsotg->dev,
363 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
364 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100365 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100366 }
367
368 udelay(1);
369 }
370
371 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100372}
373
374/**
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400375 * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100376 * @ep: USB endpoint to allocate request for.
377 * @flags: Allocation flags
378 *
379 * Allocate a new USB request structure appropriate for the specified endpoint
380 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500381static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800382 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100383{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500384 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100385
John Younec33efe2017-01-17 20:32:41 -0800386 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100387 if (!req)
388 return NULL;
389
390 INIT_LIST_HEAD(&req->queue);
391
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100392 return &req->req;
393}
394
395/**
396 * is_ep_periodic - return true if the endpoint is in periodic mode.
397 * @hs_ep: The endpoint to query.
398 *
399 * Returns true if the endpoint is in periodic mode, meaning it is being
400 * used for an Interrupt or ISO transfer.
401 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500402static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100403{
404 return hs_ep->periodic;
405}
406
407/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500408 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100409 * @hsotg: The device state.
410 * @hs_ep: The endpoint for the request
411 * @hs_req: The request being processed.
412 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500413 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100414 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200415 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500416static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800417 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500418 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100419{
420 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800421
Jingoo Han17d966a2013-05-11 21:14:00 +0900422 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100423}
424
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800425/*
426 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
427 * for Control endpoint
428 * @hsotg: The device state.
429 *
430 * This function will allocate 4 descriptor chains for EP 0: 2 for
431 * Setup stage, per one for IN and OUT data/status transactions.
432 */
433static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
434{
435 hsotg->setup_desc[0] =
436 dmam_alloc_coherent(hsotg->dev,
437 sizeof(struct dwc2_dma_desc),
438 &hsotg->setup_desc_dma[0],
439 GFP_KERNEL);
440 if (!hsotg->setup_desc[0])
441 goto fail;
442
443 hsotg->setup_desc[1] =
444 dmam_alloc_coherent(hsotg->dev,
445 sizeof(struct dwc2_dma_desc),
446 &hsotg->setup_desc_dma[1],
447 GFP_KERNEL);
448 if (!hsotg->setup_desc[1])
449 goto fail;
450
451 hsotg->ctrl_in_desc =
452 dmam_alloc_coherent(hsotg->dev,
453 sizeof(struct dwc2_dma_desc),
454 &hsotg->ctrl_in_desc_dma,
455 GFP_KERNEL);
456 if (!hsotg->ctrl_in_desc)
457 goto fail;
458
459 hsotg->ctrl_out_desc =
460 dmam_alloc_coherent(hsotg->dev,
461 sizeof(struct dwc2_dma_desc),
462 &hsotg->ctrl_out_desc_dma,
463 GFP_KERNEL);
464 if (!hsotg->ctrl_out_desc)
465 goto fail;
466
467 return 0;
468
469fail:
470 return -ENOMEM;
471}
472
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100473/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500474 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100475 * @hsotg: The controller state.
476 * @hs_ep: The endpoint we're going to write for.
477 * @hs_req: The request to write data for.
478 *
479 * This is called when the TxFIFO has some space in it to hold a new
480 * transmission and we have something to give it. The actual setup of
481 * the data size is done elsewhere, so all we have to do is to actually
482 * write the data.
483 *
484 * The return value is zero if there is more space (or nothing was done)
485 * otherwise -ENOSPC is returned if the FIFO space was used up.
486 *
487 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200488 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500489static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800490 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500491 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100492{
493 bool periodic = is_ep_periodic(hs_ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400494 u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100495 int buf_pos = hs_req->req.actual;
496 int to_write = hs_ep->size_loaded;
497 void *data;
498 int can_write;
499 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200500 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100501
502 to_write -= (buf_pos - hs_ep->last_load);
503
504 /* if there's nothing to write, get out early */
505 if (to_write == 0)
506 return 0;
507
Ben Dooks10aebc72010-07-19 09:40:44 +0100508 if (periodic && !hsotg->dedicated_fifos) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400509 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100510 int size_left;
511 int size_done;
512
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200513 /*
514 * work out how much data was loaded so we can calculate
515 * how much data is left in the fifo.
516 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100517
Dinh Nguyen47a16852014-04-14 14:13:34 -0700518 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100519
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200520 /*
521 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100522 * previous data has been completely sent.
523 */
524 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500525 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100526 return -ENOSPC;
527 }
528
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100529 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
530 __func__, size_left,
531 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
532
533 /* how much of the data has moved */
534 size_done = hs_ep->size_loaded - size_left;
535
536 /* how much data is left in the fifo */
537 can_write = hs_ep->fifo_load - size_done;
538 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
539 __func__, can_write);
540
541 can_write = hs_ep->fifo_size - can_write;
542 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
543 __func__, can_write);
544
545 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500546 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100547 return -ENOSPC;
548 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100549 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400550 can_write = dwc2_readl(hsotg,
551 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100552
553 can_write &= 0xffff;
554 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100555 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700556 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100557 dev_dbg(hsotg->dev,
558 "%s: no queue slots available (0x%08x)\n",
559 __func__, gnptxsts);
560
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500561 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100562 return -ENOSPC;
563 }
564
Dinh Nguyen47a16852014-04-14 14:13:34 -0700565 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100566 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100567 }
568
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200569 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
570
571 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800572 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100573
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200574 /*
575 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100576 * FIFO, requests of >512 cause the endpoint to get stuck with a
577 * fragment of the end of the transfer in it.
578 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200579 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100580 can_write = 512;
581
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200582 /*
583 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100584 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200585 * doing it.
586 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200587 if (to_write > max_transfer) {
588 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100589
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200590 /* it's needed only when we do not use dedicated fifos */
591 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500592 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800593 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700594 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100595 }
596
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100597 /* see if we can write data */
598
599 if (to_write > can_write) {
600 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200601 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100602
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200603 /*
604 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100605 * exact number of packets.
606 *
607 * Note, we do not currently check to see if we can ever
608 * write a full packet or not to the FIFO.
609 */
610
611 if (pkt_round)
612 to_write -= pkt_round;
613
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200614 /*
615 * enable correct FIFO interrupt to alert us when there
616 * is more room left.
617 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100618
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200619 /* it's needed only when we do not use dedicated fifos */
620 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500621 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800622 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700623 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100624 }
625
626 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800627 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100628
629 if (to_write <= 0)
630 return -ENOSPC;
631
632 hs_req->req.actual = buf_pos + to_write;
633 hs_ep->total_data += to_write;
634
635 if (periodic)
636 hs_ep->fifo_load += to_write;
637
638 to_write = DIV_ROUND_UP(to_write, 4);
639 data = hs_req->req.buf + buf_pos;
640
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +0400641 dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100642
643 return (to_write >= can_write) ? -ENOSPC : 0;
644}
645
646/**
647 * get_ep_limit - get the maximum data legnth for this endpoint
648 * @hs_ep: The endpoint
649 *
650 * Return the maximum data that can be queued in one go on a given endpoint
651 * so that transfers that are too long can be split.
652 */
John Youn9da51972017-01-17 20:30:27 -0800653static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100654{
655 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800656 unsigned int maxsize;
657 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100658
659 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700660 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
661 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100662 } else {
John Youn9da51972017-01-17 20:30:27 -0800663 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900664 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700665 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900666 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100667 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100668 }
669
670 /* we made the constant loading easier above by using +1 */
671 maxpkt--;
672 maxsize--;
673
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200674 /*
675 * constrain by packet count if maxpkts*pktsize is greater
676 * than the length register size.
677 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100678
679 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
680 maxsize = maxpkt * hs_ep->ep.maxpacket;
681
682 return maxsize;
683}
684
685/**
John Youn38beaec2017-01-17 20:31:13 -0800686 * dwc2_hsotg_read_frameno - read current frame number
687 * @hsotg: The device instance
688 *
689 * Return the current frame number
690 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700691static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
692{
693 u32 dsts;
694
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400695 dsts = dwc2_readl(hsotg, DSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700696 dsts &= DSTS_SOFFN_MASK;
697 dsts >>= DSTS_SOFFN_SHIFT;
698
699 return dsts;
700}
701
702/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800703 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
704 * DMA descriptor chain prepared for specific endpoint
705 * @hs_ep: The endpoint
706 *
707 * Return the maximum data that can be queued in one go on a given endpoint
708 * depending on its descriptor chain capacity so that transfers that
709 * are too long can be split.
710 */
711static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
712{
713 int is_isoc = hs_ep->isochronous;
714 unsigned int maxsize;
715
716 if (is_isoc)
717 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
718 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
719 else
720 maxsize = DEV_DMA_NBYTES_LIMIT;
721
722 /* Above size of one descriptor was chosen, multiple it */
723 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
724
725 return maxsize;
726}
727
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800728/*
729 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
730 * @hs_ep: The endpoint
731 * @mask: RX/TX bytes mask to be defined
732 *
733 * Returns maximum data payload for one descriptor after analyzing endpoint
734 * characteristics.
735 * DMA descriptor transfer bytes limit depends on EP type:
736 * Control out - MPS,
737 * Isochronous - descriptor rx/tx bytes bitfield limit,
738 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
739 * have concatenations from various descriptors within one packet.
740 *
741 * Selects corresponding mask for RX/TX bytes as well.
742 */
743static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
744{
745 u32 mps = hs_ep->ep.maxpacket;
746 int dir_in = hs_ep->dir_in;
747 u32 desc_size = 0;
748
749 if (!hs_ep->index && !dir_in) {
750 desc_size = mps;
751 *mask = DEV_DMA_NBYTES_MASK;
752 } else if (hs_ep->isochronous) {
753 if (dir_in) {
754 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
755 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
756 } else {
757 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
758 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
759 }
760 } else {
761 desc_size = DEV_DMA_NBYTES_LIMIT;
762 *mask = DEV_DMA_NBYTES_MASK;
763
764 /* Round down desc_size to be mps multiple */
765 desc_size -= desc_size % mps;
766 }
767
768 return desc_size;
769}
770
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100771static void dwc2_gadget_fill_nonisoc_xfer_ddma_one(struct dwc2_hsotg_ep *hs_ep,
772 struct dwc2_dma_desc **desc,
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800773 dma_addr_t dma_buff,
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100774 unsigned int len,
775 bool true_last)
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800776{
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800777 int dir_in = hs_ep->dir_in;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800778 u32 mps = hs_ep->ep.maxpacket;
779 u32 maxsize = 0;
780 u32 offset = 0;
781 u32 mask = 0;
782 int i;
783
784 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
785
786 hs_ep->desc_count = (len / maxsize) +
787 ((len % maxsize) ? 1 : 0);
788 if (len == 0)
789 hs_ep->desc_count = 1;
790
791 for (i = 0; i < hs_ep->desc_count; ++i) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100792 (*desc)->status = 0;
793 (*desc)->status |= (DEV_DMA_BUFF_STS_HBUSY
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800794 << DEV_DMA_BUFF_STS_SHIFT);
795
796 if (len > maxsize) {
797 if (!hs_ep->index && !dir_in)
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100798 (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800799
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100800 (*desc)->status |=
801 maxsize << DEV_DMA_NBYTES_SHIFT & mask;
802 (*desc)->buf = dma_buff + offset;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800803
804 len -= maxsize;
805 offset += maxsize;
806 } else {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100807 if (true_last)
808 (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800809
810 if (dir_in)
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100811 (*desc)->status |= (len % mps) ? DEV_DMA_SHORT :
812 ((hs_ep->send_zlp && true_last) ?
813 DEV_DMA_SHORT : 0);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800814
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100815 (*desc)->status |=
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800816 len << DEV_DMA_NBYTES_SHIFT & mask;
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100817 (*desc)->buf = dma_buff + offset;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800818 }
819
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100820 (*desc)->status &= ~DEV_DMA_BUFF_STS_MASK;
821 (*desc)->status |= (DEV_DMA_BUFF_STS_HREADY
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800822 << DEV_DMA_BUFF_STS_SHIFT);
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100823 (*desc)++;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800824 }
825}
826
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800827/*
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100828 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
829 * @hs_ep: The endpoint
830 * @ureq: Request to transfer
831 * @offset: offset in bytes
832 * @len: Length of the transfer
833 *
834 * This function will iterate over descriptor chain and fill its entries
835 * with corresponding information based on transfer data.
836 */
837static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
838 struct usb_request *ureq,
839 unsigned int offset,
840 unsigned int len)
841{
842 struct dwc2_dma_desc *desc = hs_ep->desc_list;
843 struct scatterlist *sg;
844 int i;
845 u8 desc_count = 0;
846
847 /* non-DMA sg buffer */
848 if (!ureq->num_sgs) {
849 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
850 ureq->dma + offset, len, true);
851 return;
852 }
853
854 /* DMA sg buffer */
855 for_each_sg(ureq->sg, sg, ureq->num_sgs, i) {
856 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
857 sg_dma_address(sg) + sg->offset, sg_dma_len(sg),
858 sg_is_last(sg));
859 desc_count += hs_ep->desc_count;
860 }
861
862 hs_ep->desc_count = desc_count;
863}
864
865/*
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800866 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
867 * @hs_ep: The isochronous endpoint.
868 * @dma_buff: usb requests dma buffer.
869 * @len: usb request transfer length.
870 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400871 * Fills next free descriptor with the data of the arrived usb request,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800872 * frame info, sets Last and IOC bits increments next_desc. If filled
873 * descriptor is not the first one, removes L bit from the previous descriptor
874 * status.
875 */
876static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
877 dma_addr_t dma_buff, unsigned int len)
878{
879 struct dwc2_dma_desc *desc;
880 struct dwc2_hsotg *hsotg = hs_ep->parent;
881 u32 index;
882 u32 maxsize = 0;
883 u32 mask = 0;
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400884 u8 pid = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800885
886 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800887
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400888 index = hs_ep->next_desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800889 desc = &hs_ep->desc_list[index];
890
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400891 /* Check if descriptor chain full */
892 if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
893 DEV_DMA_BUFF_STS_HREADY) {
894 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
895 return 1;
896 }
897
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800898 /* Clear L bit of previous desc if more than one entries in the chain */
899 if (hs_ep->next_desc)
900 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
901
902 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
903 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
904
905 desc->status = 0;
906 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
907
908 desc->buf = dma_buff;
909 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
910 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
911
912 if (hs_ep->dir_in) {
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400913 if (len)
914 pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
915 else
916 pid = 1;
917 desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800918 DEV_DMA_ISOC_PID_MASK) |
919 ((len % hs_ep->ep.maxpacket) ?
920 DEV_DMA_SHORT : 0) |
921 ((hs_ep->target_frame <<
922 DEV_DMA_ISOC_FRNUM_SHIFT) &
923 DEV_DMA_ISOC_FRNUM_MASK);
924 }
925
926 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
927 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
928
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400929 /* Increment frame number by interval for IN */
930 if (hs_ep->dir_in)
931 dwc2_gadget_incr_frame_num(hs_ep);
932
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800933 /* Update index of last configured entry in the chain */
934 hs_ep->next_desc++;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400935 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_GENERIC)
936 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800937
938 return 0;
939}
940
941/*
942 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
943 * @hs_ep: The isochronous endpoint.
944 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400945 * Prepare descriptor chain for isochronous endpoints. Afterwards
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800946 * write DMA address to HW and enable the endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800947 */
948static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
949{
950 struct dwc2_hsotg *hsotg = hs_ep->parent;
951 struct dwc2_hsotg_req *hs_req, *treq;
952 int index = hs_ep->index;
953 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400954 int i;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800955 u32 dma_reg;
956 u32 depctl;
957 u32 ctrl;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400958 struct dwc2_dma_desc *desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800959
960 if (list_empty(&hs_ep->queue)) {
Minas Harutyunyan1ffba902018-06-12 12:37:29 +0400961 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800962 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
963 return;
964 }
965
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400966 /* Initialize descriptor chain by Host Busy status */
967 for (i = 0; i < MAX_DMA_DESC_NUM_GENERIC; i++) {
968 desc = &hs_ep->desc_list[i];
969 desc->status = 0;
970 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
971 << DEV_DMA_BUFF_STS_SHIFT);
972 }
973
974 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800975 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100976 dma_addr_t dma_addr = hs_req->req.dma;
977
978 if (hs_req->req.num_sgs) {
979 WARN_ON(hs_req->req.num_sgs > 1);
980 dma_addr = sg_dma_address(hs_req->req.sg);
981 }
982 ret = dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800983 hs_req->req.length);
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400984 if (ret)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800985 break;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800986 }
987
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400988 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800989 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
990 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
991
992 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400993 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800994
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400995 ctrl = dwc2_readl(hsotg, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800996 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400997 dwc2_writel(hsotg, ctrl, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800998}
999
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -08001000/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001001 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001002 * @hsotg: The controller state.
1003 * @hs_ep: The endpoint to process a request for
1004 * @hs_req: The request to start.
1005 * @continuing: True if we are doing more for the current request.
1006 *
1007 * Start the given request running by setting the endpoint registers
1008 * appropriately, and writing any data to the FIFOs.
1009 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001010static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001011 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001012 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001013 bool continuing)
1014{
1015 struct usb_request *ureq = &hs_req->req;
1016 int index = hs_ep->index;
1017 int dir_in = hs_ep->dir_in;
1018 u32 epctrl_reg;
1019 u32 epsize_reg;
1020 u32 epsize;
1021 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -08001022 unsigned int length;
1023 unsigned int packets;
1024 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001025 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001026
1027 if (index != 0) {
1028 if (hs_ep->req && !continuing) {
1029 dev_err(hsotg->dev, "%s: active request\n", __func__);
1030 WARN_ON(1);
1031 return;
1032 } else if (hs_ep->req != hs_req && continuing) {
1033 dev_err(hsotg->dev,
1034 "%s: continue different req\n", __func__);
1035 WARN_ON(1);
1036 return;
1037 }
1038 }
1039
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001040 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001041 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
1042 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001043
1044 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001045 __func__, dwc2_readl(hsotg, epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001046 hs_ep->dir_in ? "in" : "out");
1047
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001048 /* If endpoint is stalled, we will restart request later */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001049 ctrl = dwc2_readl(hsotg, epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001050
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +02001051 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001052 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
1053 return;
1054 }
1055
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001056 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001057 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
1058 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001059
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -08001060 if (!using_desc_dma(hsotg))
1061 maxreq = get_ep_limit(hs_ep);
1062 else
1063 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
1064
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001065 if (length > maxreq) {
1066 int round = maxreq % hs_ep->ep.maxpacket;
1067
1068 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
1069 __func__, length, maxreq, round);
1070
1071 /* round down to multiple of packets */
1072 if (round)
1073 maxreq -= round;
1074
1075 length = maxreq;
1076 }
1077
1078 if (length)
1079 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
1080 else
1081 packets = 1; /* send one packet if length is zero. */
1082
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001083 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1084 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
1085 return;
1086 }
1087
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001088 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001089 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001090 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001091 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001092 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001093 else
1094 epsize = 0;
1095
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001096 /*
1097 * zero length packet should be programmed on its own and should not
1098 * be counted in DIEPTSIZ.PktCnt with other packets.
1099 */
1100 if (dir_in && ureq->zero && !continuing) {
1101 /* Test if zlp is actually required. */
1102 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001103 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001104 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001105 }
1106
Dinh Nguyen47a16852014-04-14 14:13:34 -07001107 epsize |= DXEPTSIZ_PKTCNT(packets);
1108 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001109
1110 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1111 __func__, packets, length, ureq->length, epsize, epsize_reg);
1112
1113 /* store the request as the current one we're doing */
1114 hs_ep->req = hs_req;
1115
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001116 if (using_desc_dma(hsotg)) {
1117 u32 offset = 0;
1118 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001119
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001120 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1121 if (!dir_in) {
1122 if (!index)
1123 length = mps;
1124 else if (length % mps)
1125 length += (mps - (length % mps));
1126 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001127
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001128 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001129 * If more data to send, adjust DMA for EP0 out data stage.
1130 * ureq->dma stays unchanged, hence increment it by already
1131 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001132 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001133 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1134 continuing)
1135 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001136
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001137 /* Fill DDMA chain entries */
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01001138 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq, offset,
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001139 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001140
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001141 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001142 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001143
1144 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1145 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1146 } else {
1147 /* write size / packets */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001148 dwc2_writel(hsotg, epsize, epsize_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001149
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001150 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001151 /*
1152 * write DMA address to control register, buffer
1153 * already synced by dwc2_hsotg_ep_queue().
1154 */
1155
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001156 dwc2_writel(hsotg, ureq->dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001157
1158 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1159 __func__, &ureq->dma, dma_reg);
1160 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001161 }
1162
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001163 if (hs_ep->isochronous && hs_ep->interval == 1) {
1164 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1165 dwc2_gadget_incr_frame_num(hs_ep);
1166
1167 if (hs_ep->target_frame & 0x1)
1168 ctrl |= DXEPCTL_SETODDFR;
1169 else
1170 ctrl |= DXEPCTL_SETEVENFR;
1171 }
1172
Dinh Nguyen47a16852014-04-14 14:13:34 -07001173 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001174
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001175 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001176
1177 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001178 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001179 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001180
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001181 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001182 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001183
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001184 /*
1185 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001186 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001187 * this information.
1188 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001189 hs_ep->size_loaded = length;
1190 hs_ep->last_load = ureq->actual;
1191
1192 if (dir_in && !using_dma(hsotg)) {
1193 /* set these anyway, we may need them for non-periodic in */
1194 hs_ep->fifo_load = 0;
1195
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001196 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001197 }
1198
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001199 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001200 * Note, trying to clear the NAK here causes problems with transmit
1201 * on the S3C6400 ending up with the TXFIFO becoming full.
1202 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001203
1204 /* check ep is enabled */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001205 if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001206 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001207 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001208 index, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001209
Dinh Nguyen47a16852014-04-14 14:13:34 -07001210 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001211 __func__, dwc2_readl(hsotg, epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001212
1213 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001214 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001215}
1216
1217/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001218 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001219 * @hsotg: The device state.
1220 * @hs_ep: The endpoint the request is on.
1221 * @req: The request being processed.
1222 *
1223 * We've been asked to queue a request, so ensure that the memory buffer
1224 * is correctly setup for DMA. If we've been passed an extant DMA address
1225 * then ensure the buffer has been synced to memory. If our buffer has no
1226 * DMA memory, then we map the memory and mark our request to allow us to
1227 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001228 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001229static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001230 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001231 struct usb_request *req)
1232{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001233 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001234
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001235 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1236 if (ret)
1237 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001238
1239 return 0;
1240
1241dma_error:
1242 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1243 __func__, req->buf, req->length);
1244
1245 return -EIO;
1246}
1247
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001248static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001249 struct dwc2_hsotg_ep *hs_ep,
1250 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001251{
1252 void *req_buf = hs_req->req.buf;
1253
1254 /* If dma is not being used or buffer is aligned */
1255 if (!using_dma(hsotg) || !((long)req_buf & 3))
1256 return 0;
1257
1258 WARN_ON(hs_req->saved_req_buf);
1259
1260 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001261 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001262
1263 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1264 if (!hs_req->req.buf) {
1265 hs_req->req.buf = req_buf;
1266 dev_err(hsotg->dev,
1267 "%s: unable to allocate memory for bounce buffer\n",
1268 __func__);
1269 return -ENOMEM;
1270 }
1271
1272 /* Save actual buffer */
1273 hs_req->saved_req_buf = req_buf;
1274
1275 if (hs_ep->dir_in)
1276 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1277 return 0;
1278}
1279
John Younb98866c2017-01-17 20:31:58 -08001280static void
1281dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1282 struct dwc2_hsotg_ep *hs_ep,
1283 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001284{
1285 /* If dma is not being used or buffer was aligned */
1286 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1287 return;
1288
1289 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1290 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1291
1292 /* Copy data from bounce buffer on successful out transfer */
1293 if (!hs_ep->dir_in && !hs_req->req.status)
1294 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001295 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001296
1297 /* Free bounce buffer */
1298 kfree(hs_req->req.buf);
1299
1300 hs_req->req.buf = hs_req->saved_req_buf;
1301 hs_req->saved_req_buf = NULL;
1302}
1303
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001304/**
1305 * dwc2_gadget_target_frame_elapsed - Checks target frame
1306 * @hs_ep: The driver endpoint to check
1307 *
1308 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1309 * corresponding transfer.
1310 */
1311static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1312{
1313 struct dwc2_hsotg *hsotg = hs_ep->parent;
1314 u32 target_frame = hs_ep->target_frame;
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001315 u32 current_frame = hsotg->frame_number;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001316 bool frame_overrun = hs_ep->frame_overrun;
1317
1318 if (!frame_overrun && current_frame >= target_frame)
1319 return true;
1320
1321 if (frame_overrun && current_frame >= target_frame &&
1322 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1323 return true;
1324
1325 return false;
1326}
1327
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001328/*
1329 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1330 * @hsotg: The driver state
1331 * @hs_ep: the ep descriptor chain is for
1332 *
1333 * Called to update EP0 structure's pointers depend on stage of
1334 * control transfer.
1335 */
1336static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1337 struct dwc2_hsotg_ep *hs_ep)
1338{
1339 switch (hsotg->ep0_state) {
1340 case DWC2_EP0_SETUP:
1341 case DWC2_EP0_STATUS_OUT:
1342 hs_ep->desc_list = hsotg->setup_desc[0];
1343 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1344 break;
1345 case DWC2_EP0_DATA_IN:
1346 case DWC2_EP0_STATUS_IN:
1347 hs_ep->desc_list = hsotg->ctrl_in_desc;
1348 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1349 break;
1350 case DWC2_EP0_DATA_OUT:
1351 hs_ep->desc_list = hsotg->ctrl_out_desc;
1352 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1353 break;
1354 default:
1355 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1356 hsotg->ep0_state);
1357 return -EINVAL;
1358 }
1359
1360 return 0;
1361}
1362
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001363static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001364 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001365{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001366 struct dwc2_hsotg_req *hs_req = our_req(req);
1367 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001368 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001369 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001370 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001371 u32 maxsize = 0;
1372 u32 mask = 0;
1373
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001374
1375 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1376 ep->name, req, req->length, req->buf, req->no_interrupt,
1377 req->zero, req->short_not_ok);
1378
Gregory Herrero7ababa92015-04-29 22:09:08 +02001379 /* Prevent new request submission when controller is suspended */
Grigor Tovmasyan88b02f22018-01-24 17:44:25 +04001380 if (hs->lx_state != DWC2_L0) {
1381 dev_dbg(hs->dev, "%s: submit request only in active state\n",
John Youn9da51972017-01-17 20:30:27 -08001382 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001383 return -EAGAIN;
1384 }
1385
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001386 /* initialise status of the request */
1387 INIT_LIST_HEAD(&hs_req->queue);
1388 req->actual = 0;
1389 req->status = -EINPROGRESS;
1390
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001391 /* In DDMA mode for ISOC's don't queue request if length greater
1392 * than descriptor limits.
1393 */
1394 if (using_desc_dma(hs) && hs_ep->isochronous) {
1395 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1396 if (hs_ep->dir_in && req->length > maxsize) {
1397 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1398 req->length, maxsize);
1399 return -EINVAL;
1400 }
1401
1402 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1403 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1404 req->length, hs_ep->ep.maxpacket);
1405 return -EINVAL;
1406 }
1407 }
1408
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001409 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001410 if (ret)
1411 return ret;
1412
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001413 /* if we're using DMA, sync the buffers as necessary */
1414 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001415 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001416 if (ret)
1417 return ret;
1418 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001419 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1420 if (using_desc_dma(hs) && !hs_ep->index) {
1421 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1422 if (ret)
1423 return ret;
1424 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001425
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001426 first = list_empty(&hs_ep->queue);
1427 list_add_tail(&hs_req->queue, &hs_ep->queue);
1428
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001429 /*
1430 * Handle DDMA isochronous transfers separately - just add new entry
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001431 * to the descriptor chain.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001432 * Transfer will be started once SW gets either one of NAK or
1433 * OutTknEpDis interrupts.
1434 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001435 if (using_desc_dma(hs) && hs_ep->isochronous) {
1436 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01001437 dma_addr_t dma_addr = hs_req->req.dma;
1438
1439 if (hs_req->req.num_sgs) {
1440 WARN_ON(hs_req->req.num_sgs > 1);
1441 dma_addr = sg_dma_address(hs_req->req.sg);
1442 }
1443 dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001444 hs_req->req.length);
1445 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001446 return 0;
1447 }
1448
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001449 if (first) {
1450 if (!hs_ep->isochronous) {
1451 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1452 return 0;
1453 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001454
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001455 /* Update current frame number value. */
1456 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1457 while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001458 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001459 /* Update current frame number value once more as it
1460 * changes here.
1461 */
1462 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1463 }
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001464
1465 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1466 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1467 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001468 return 0;
1469}
1470
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001471static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001472 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001473{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001474 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001475 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001476 unsigned long flags = 0;
1477 int ret = 0;
1478
1479 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001480 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001481 spin_unlock_irqrestore(&hs->lock, flags);
1482
1483 return ret;
1484}
1485
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001486static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001487 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001488{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001489 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001490
1491 kfree(hs_req);
1492}
1493
1494/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001495 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001496 * @ep: The endpoint the request was on.
1497 * @req: The request completed.
1498 *
1499 * Called on completion of any requests the driver itself
1500 * submitted that need cleaning up.
1501 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001502static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001503 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001504{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001505 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001506 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001507
1508 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1509
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001510 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001511}
1512
1513/**
1514 * ep_from_windex - convert control wIndex value to endpoint
1515 * @hsotg: The driver state.
1516 * @windex: The control request wIndex field (in host order).
1517 *
1518 * Convert the given wIndex into a pointer to an driver endpoint
1519 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001520 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001521static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001522 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001523{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001524 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001525 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1526 int idx = windex & 0x7F;
1527
1528 if (windex >= 0x100)
1529 return NULL;
1530
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001531 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001532 return NULL;
1533
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001534 ep = index_to_ep(hsotg, idx, dir);
1535
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001536 if (idx && ep->dir_in != dir)
1537 return NULL;
1538
1539 return ep;
1540}
1541
1542/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001543 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001544 * @hsotg: The driver state.
1545 * @testmode: requested usb test mode
1546 * Enable usb Test Mode requested by the Host.
1547 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001548int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001549{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001550 int dctl = dwc2_readl(hsotg, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001551
1552 dctl &= ~DCTL_TSTCTL_MASK;
1553 switch (testmode) {
1554 case TEST_J:
1555 case TEST_K:
1556 case TEST_SE0_NAK:
1557 case TEST_PACKET:
1558 case TEST_FORCE_EN:
1559 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1560 break;
1561 default:
1562 return -EINVAL;
1563 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001564 dwc2_writel(hsotg, dctl, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001565 return 0;
1566}
1567
1568/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001569 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001570 * @hsotg: The device state
1571 * @ep: Endpoint 0
1572 * @buff: Buffer for request
1573 * @length: Length of reply.
1574 *
1575 * Create a request and queue it on the given endpoint. This is useful as
1576 * an internal method of sending replies to certain control requests, etc.
1577 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001578static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001579 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001580 void *buff,
1581 int length)
1582{
1583 struct usb_request *req;
1584 int ret;
1585
1586 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1587
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001588 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001589 hsotg->ep0_reply = req;
1590 if (!req) {
1591 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1592 return -ENOMEM;
1593 }
1594
1595 req->buf = hsotg->ep0_buff;
1596 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001597 /*
1598 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1599 * STATUS stage.
1600 */
1601 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001602 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001603
1604 if (length)
1605 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001606
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001607 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001608 if (ret) {
1609 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1610 return ret;
1611 }
1612
1613 return 0;
1614}
1615
1616/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001617 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001618 * @hsotg: The device state
1619 * @ctrl: USB control request
1620 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001621static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001622 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001623{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001624 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1625 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001626 __le16 reply;
1627 int ret;
1628
1629 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1630
1631 if (!ep0->dir_in) {
1632 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1633 return -EINVAL;
1634 }
1635
1636 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1637 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001638 /*
1639 * bit 0 => self powered
1640 * bit 1 => remote wakeup
1641 */
1642 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001643 break;
1644
1645 case USB_RECIP_INTERFACE:
1646 /* currently, the data result should be zero */
1647 reply = cpu_to_le16(0);
1648 break;
1649
1650 case USB_RECIP_ENDPOINT:
1651 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1652 if (!ep)
1653 return -ENOENT;
1654
1655 reply = cpu_to_le16(ep->halted ? 1 : 0);
1656 break;
1657
1658 default:
1659 return 0;
1660 }
1661
1662 if (le16_to_cpu(ctrl->wLength) != 2)
1663 return -EINVAL;
1664
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001665 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001666 if (ret) {
1667 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1668 return ret;
1669 }
1670
1671 return 1;
1672}
1673
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001674static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001675
1676/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001677 * get_ep_head - return the first request on the endpoint
1678 * @hs_ep: The controller endpoint to get
1679 *
1680 * Get the first request on the endpoint.
1681 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001682static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001683{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001684 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1685 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001686}
1687
1688/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001689 * dwc2_gadget_start_next_request - Starts next request from ep queue
1690 * @hs_ep: Endpoint structure
1691 *
1692 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1693 * in its handler. Hence we need to unmask it here to be able to do
1694 * resynchronization.
1695 */
1696static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1697{
1698 u32 mask;
1699 struct dwc2_hsotg *hsotg = hs_ep->parent;
1700 int dir_in = hs_ep->dir_in;
1701 struct dwc2_hsotg_req *hs_req;
1702 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1703
1704 if (!list_empty(&hs_ep->queue)) {
1705 hs_req = get_ep_head(hs_ep);
1706 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1707 return;
1708 }
1709 if (!hs_ep->isochronous)
1710 return;
1711
1712 if (dir_in) {
1713 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1714 __func__);
1715 } else {
1716 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1717 __func__);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001718 mask = dwc2_readl(hsotg, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001719 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001720 dwc2_writel(hsotg, mask, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001721 }
1722}
1723
1724/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001725 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001726 * @hsotg: The device state
1727 * @ctrl: USB control request
1728 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001729static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001730 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001731{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001732 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1733 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001734 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001735 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001736 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001737 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001738 u32 recip;
1739 u32 wValue;
1740 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001741
1742 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1743 __func__, set ? "SET" : "CLEAR");
1744
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001745 wValue = le16_to_cpu(ctrl->wValue);
1746 wIndex = le16_to_cpu(ctrl->wIndex);
1747 recip = ctrl->bRequestType & USB_RECIP_MASK;
1748
1749 switch (recip) {
1750 case USB_RECIP_DEVICE:
1751 switch (wValue) {
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001752 case USB_DEVICE_REMOTE_WAKEUP:
1753 hsotg->remote_wakeup_allowed = 1;
1754 break;
1755
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001756 case USB_DEVICE_TEST_MODE:
1757 if ((wIndex & 0xff) != 0)
1758 return -EINVAL;
1759 if (!set)
1760 return -EINVAL;
1761
1762 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001763 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001764 if (ret) {
1765 dev_err(hsotg->dev,
1766 "%s: failed to send reply\n", __func__);
1767 return ret;
1768 }
1769 break;
1770 default:
1771 return -ENOENT;
1772 }
1773 break;
1774
1775 case USB_RECIP_ENDPOINT:
1776 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001777 if (!ep) {
1778 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001779 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001780 return -ENOENT;
1781 }
1782
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001783 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001784 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001785 halted = ep->halted;
1786
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001787 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001788
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001789 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001790 if (ret) {
1791 dev_err(hsotg->dev,
1792 "%s: failed to send reply\n", __func__);
1793 return ret;
1794 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001795
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001796 /*
1797 * we have to complete all requests for ep if it was
1798 * halted, and the halt was cleared by CLEAR_FEATURE
1799 */
1800
1801 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001802 /*
1803 * If we have request in progress,
1804 * then complete it
1805 */
1806 if (ep->req) {
1807 hs_req = ep->req;
1808 ep->req = NULL;
1809 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001810 if (hs_req->req.complete) {
1811 spin_unlock(&hsotg->lock);
1812 usb_gadget_giveback_request(
1813 &ep->ep, &hs_req->req);
1814 spin_lock(&hsotg->lock);
1815 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001816 }
1817
1818 /* If we have pending request, then start it */
John Youn34c0887f2017-01-17 20:31:43 -08001819 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001820 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001821 }
1822
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001823 break;
1824
1825 default:
1826 return -ENOENT;
1827 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001828 break;
1829 default:
1830 return -ENOENT;
1831 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001832 return 1;
1833}
1834
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001835static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001836
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001837/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001838 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001839 * @hsotg: The device state
1840 *
1841 * Set stall for ep0 as response for setup request.
1842 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001843static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001844{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001845 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001846 u32 reg;
1847 u32 ctrl;
1848
1849 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1850 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1851
1852 /*
1853 * DxEPCTL_Stall will be cleared by EP once it has
1854 * taken effect, so no need to clear later.
1855 */
1856
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001857 ctrl = dwc2_readl(hsotg, reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001858 ctrl |= DXEPCTL_STALL;
1859 ctrl |= DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001860 dwc2_writel(hsotg, ctrl, reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001861
1862 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001863 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001864 ctrl, reg, dwc2_readl(hsotg, reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001865
1866 /*
1867 * complete won't be called, so we enqueue
1868 * setup request here
1869 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001870 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001871}
1872
1873/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001874 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001875 * @hsotg: The device state
1876 * @ctrl: The control request received
1877 *
1878 * The controller has received the SETUP phase of a control request, and
1879 * needs to work out what to do next (and whether to pass it on to the
1880 * gadget driver).
1881 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001882static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001883 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001884{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001885 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001886 int ret = 0;
1887 u32 dcfg;
1888
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001889 dev_dbg(hsotg->dev,
1890 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1891 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1892 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001893
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001894 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001895 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001896 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1897 } else if (ctrl->bRequestType & USB_DIR_IN) {
1898 ep0->dir_in = 1;
1899 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1900 } else {
1901 ep0->dir_in = 0;
1902 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1903 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001904
1905 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1906 switch (ctrl->bRequest) {
1907 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001908 hsotg->connected = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001909 dcfg = dwc2_readl(hsotg, DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001910 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001911 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1912 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001913 dwc2_writel(hsotg, dcfg, DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001914
1915 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1916
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001917 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001918 return;
1919
1920 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001921 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001922 break;
1923
1924 case USB_REQ_CLEAR_FEATURE:
1925 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001926 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001927 break;
1928 }
1929 }
1930
1931 /* as a fallback, try delivering it to the driver to deal with */
1932
1933 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001934 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001935 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001936 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001937 if (ret < 0)
1938 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1939 }
1940
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001941 /*
1942 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001943 * so respond with a STALL for the status stage to indicate failure.
1944 */
1945
Robert Baldygac9f721b2014-01-14 08:36:00 +01001946 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001947 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001948}
1949
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001951 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001952 * @ep: The endpoint the request was on.
1953 * @req: The request completed.
1954 *
1955 * Called on completion of any requests the driver itself submitted for
1956 * EP0 setup packets
1957 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001958static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001959 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001960{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001961 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001962 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001963
1964 if (req->status < 0) {
1965 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1966 return;
1967 }
1968
Robert Baldyga93f599f2013-11-21 13:49:17 +01001969 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001970 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001971 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001972 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001973 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001974 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001975}
1976
1977/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001978 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001979 * @hsotg: The device state.
1980 *
1981 * Enqueue a request on EP0 if necessary to received any SETUP packets
1982 * received from the host.
1983 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001984static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001985{
1986 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001987 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001988 int ret;
1989
1990 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1991
1992 req->zero = 0;
1993 req->length = 8;
1994 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001995 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996
1997 if (!list_empty(&hs_req->queue)) {
1998 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1999 return;
2000 }
2001
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002002 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002003 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002004 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002005
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002006 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002007 if (ret < 0) {
2008 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002009 /*
2010 * Don't think there's much we can do other than watch the
2011 * driver fail.
2012 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002013 }
2014}
2015
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002016static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002017 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002018{
2019 u32 ctrl;
2020 u8 index = hs_ep->index;
2021 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
2022 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
2023
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01002024 if (hs_ep->dir_in)
2025 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002026 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01002027 else
2028 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002029 index);
2030 if (using_desc_dma(hsotg)) {
Minas Harutyunyan201ec562018-01-16 16:03:32 +04002031 if (!index)
2032 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
2033
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01002034 /* Not specific buffer needed for ep0 ZLP */
2035 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &hs_ep->desc_list,
2036 hs_ep->desc_list_dma, 0, true);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002037 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002038 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2039 DXEPTSIZ_XFERSIZE(0),
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002040 epsiz_reg);
2041 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002042
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002043 ctrl = dwc2_readl(hsotg, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002044 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
2045 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
2046 ctrl |= DXEPCTL_USBACTEP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002047 dwc2_writel(hsotg, ctrl, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002048}
2049
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002050/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002051 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002052 * @hsotg: The device state.
2053 * @hs_ep: The endpoint the request was on.
2054 * @hs_req: The request to complete.
2055 * @result: The result code (0 => Ok, otherwise errno)
2056 *
2057 * The given request has finished, so call the necessary completion
2058 * if it has one and then look to see if we can start a new request
2059 * on the endpoint.
2060 *
2061 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002062 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002063static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002064 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002065 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002066 int result)
2067{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002068 if (!hs_req) {
2069 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
2070 return;
2071 }
2072
2073 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
2074 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
2075
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002076 /*
2077 * only replace the status if we've not already set an error
2078 * from a previous transaction
2079 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002080
2081 if (hs_req->req.status == -EINPROGRESS)
2082 hs_req->req.status = result;
2083
Yunzhi Li44583fe2015-09-29 12:25:01 +02002084 if (using_dma(hsotg))
2085 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2086
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002087 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01002088
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002089 hs_ep->req = NULL;
2090 list_del_init(&hs_req->queue);
2091
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002092 /*
2093 * call the complete request with the locks off, just in case the
2094 * request tries to queue more work for this endpoint.
2095 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002096
2097 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002098 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002099 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002100 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002101 }
2102
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002103 /* In DDMA don't need to proceed to starting of next ISOC request */
2104 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2105 return;
2106
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002107 /*
2108 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002109 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002110 * so be careful when doing this.
2111 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002112
John Youn34c0887f2017-01-17 20:31:43 -08002113 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002114 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002115}
2116
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002117/*
2118 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2119 * @hs_ep: The endpoint the request was on.
2120 *
2121 * Get first request from the ep queue, determine descriptor on which complete
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002122 * happened. SW discovers which descriptor currently in use by HW, adjusts
2123 * dma_address and calculates index of completed descriptor based on the value
2124 * of DEPDMA register. Update actual length of request, giveback to gadget.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002125 */
2126static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2127{
2128 struct dwc2_hsotg *hsotg = hs_ep->parent;
2129 struct dwc2_hsotg_req *hs_req;
2130 struct usb_request *ureq;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002131 u32 desc_sts;
2132 u32 mask;
2133
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002134 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2135
2136 /* Process only descriptors with buffer status set to DMA done */
2137 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2138 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2139
2140 hs_req = get_ep_head(hs_ep);
2141 if (!hs_req) {
2142 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2143 return;
2144 }
2145 ureq = &hs_req->req;
2146
2147 /* Check completion status */
2148 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2149 DEV_DMA_STS_SUCC) {
2150 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2151 DEV_DMA_ISOC_RX_NBYTES_MASK;
2152 ureq->actual = ureq->length - ((desc_sts & mask) >>
2153 DEV_DMA_ISOC_NBYTES_SHIFT);
2154
2155 /* Adjust actual len for ISOC Out if len is
2156 * not align of 4
2157 */
2158 if (!hs_ep->dir_in && ureq->length & 0x3)
2159 ureq->actual += 4 - (ureq->length & 0x3);
2160 }
2161
2162 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2163
2164 hs_ep->compl_desc++;
2165 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_GENERIC - 1))
2166 hs_ep->compl_desc = 0;
2167 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002168 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002169}
2170
2171/*
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002172 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2173 * @hs_ep: The isochronous endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002174 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002175 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2176 * interrupt. Reset target frame and next_desc to allow to start
2177 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2178 * interrupt for OUT direction.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002179 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002180static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002181{
2182 struct dwc2_hsotg *hsotg = hs_ep->parent;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002183
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002184 if (!hs_ep->dir_in)
2185 dwc2_flush_rx_fifo(hsotg);
2186 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002187
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002188 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2189 hs_ep->next_desc = 0;
2190 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002191}
2192
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002193/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002194 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002195 * @hsotg: The device state.
2196 * @ep_idx: The endpoint index for the data
2197 * @size: The size of data in the fifo, in bytes
2198 *
2199 * The FIFO status shows there is data to read from the FIFO for a given
2200 * endpoint, so sort out whether we need to read the data into a request
2201 * that has been made for that endpoint.
2202 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002203static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002204{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002205 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2206 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002207 int to_read;
2208 int max_req;
2209 int read_ptr;
2210
2211 if (!hs_req) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002212 u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002213 int ptr;
2214
Robert Baldyga6b448af42014-12-16 11:51:44 +01002215 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002216 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002217 __func__, size, ep_idx, epctl);
2218
2219 /* dump the data from the FIFO, we've nothing we can do */
2220 for (ptr = 0; ptr < size; ptr += 4)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002221 (void)dwc2_readl(hsotg, EPFIFO(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002222
2223 return;
2224 }
2225
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002226 to_read = size;
2227 read_ptr = hs_req->req.actual;
2228 max_req = hs_req->req.length - read_ptr;
2229
Ben Dooksa33e7132010-07-19 09:40:49 +01002230 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2231 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2232
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002233 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002234 /*
2235 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002236 * to deal with in this request.
2237 */
2238
2239 /* currently we don't deal this */
2240 WARN_ON_ONCE(1);
2241 }
2242
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002243 hs_ep->total_data += to_read;
2244 hs_req->req.actual += to_read;
2245 to_read = DIV_ROUND_UP(to_read, 4);
2246
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002247 /*
2248 * note, we might over-write the buffer end by 3 bytes depending on
2249 * alignment of the data.
2250 */
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +04002251 dwc2_readl_rep(hsotg, EPFIFO(ep_idx),
2252 hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002253}
2254
2255/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002256 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002257 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002258 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002259 *
2260 * Generate a zero-length IN packet request for terminating a SETUP
2261 * transaction.
2262 *
2263 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002264 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002265 * the TxFIFO.
2266 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002267static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002268{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002269 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002270 hsotg->eps_out[0]->dir_in = dir_in;
2271 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002272
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002273 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002274}
2275
Roman Bacikec1f9d92015-09-10 18:13:43 -07002276static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002277 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002278{
2279 u32 ctrl;
2280
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002281 ctrl = dwc2_readl(hsotg, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002282 if (ctrl & DXEPCTL_EOFRNUM)
2283 ctrl |= DXEPCTL_SETEVENFR;
2284 else
2285 ctrl |= DXEPCTL_SETODDFR;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002286 dwc2_writel(hsotg, ctrl, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002287}
2288
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002289/*
2290 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2291 * @hs_ep - The endpoint on which transfer went
2292 *
2293 * Iterate over endpoints descriptor chain and get info on bytes remained
2294 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2295 */
2296static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2297{
2298 struct dwc2_hsotg *hsotg = hs_ep->parent;
2299 unsigned int bytes_rem = 0;
2300 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2301 int i;
2302 u32 status;
2303
2304 if (!desc)
2305 return -EINVAL;
2306
2307 for (i = 0; i < hs_ep->desc_count; ++i) {
2308 status = desc->status;
2309 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2310
2311 if (status & DEV_DMA_STS_MASK)
2312 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2313 i, status & DEV_DMA_STS_MASK);
2314 }
2315
2316 return bytes_rem;
2317}
2318
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002319/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002320 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002321 * @hsotg: The device instance
2322 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002323 *
2324 * The RXFIFO has delivered an OutDone event, which means that the data
2325 * transfer for an OUT endpoint has been completed, either by a short
2326 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002327 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002328static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002329{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002330 u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002331 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2332 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002333 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002334 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002335 int result = 0;
2336
2337 if (!hs_req) {
2338 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2339 return;
2340 }
2341
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002342 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2343 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002344 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2345 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002346 return;
2347 }
2348
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002349 if (using_desc_dma(hsotg))
2350 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2351
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002352 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002353 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002354
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002355 /*
2356 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002357 * is left in the endpoint size register and then working it
2358 * out from the amount we loaded for the transfer.
2359 *
2360 * We need to do this as DMA pointers are always 32bit aligned
2361 * so may overshoot/undershoot the transfer.
2362 */
2363
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002364 size_done = hs_ep->size_loaded - size_left;
2365 size_done += hs_ep->last_load;
2366
2367 req->actual = size_done;
2368 }
2369
Ben Dooksa33e7132010-07-19 09:40:49 +01002370 /* if there is more request to do, schedule new transfer */
2371 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002372 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002373 return;
2374 }
2375
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002376 if (req->actual < req->length && req->short_not_ok) {
2377 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2378 __func__, req->actual, req->length);
2379
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002380 /*
2381 * todo - what should we return here? there's no one else
2382 * even bothering to check the status.
2383 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002384 }
2385
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002386 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2387 if (!using_desc_dma(hsotg) && epnum == 0 &&
2388 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002389 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002390 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002391 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002392 }
2393
Roman Bacikec1f9d92015-09-10 18:13:43 -07002394 /*
2395 * Slave mode OUT transfers do not go through XferComplete so
2396 * adjust the ISOC parity here.
2397 */
2398 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002399 if (hs_ep->isochronous && hs_ep->interval == 1)
2400 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002401 else if (hs_ep->isochronous && hs_ep->interval > 1)
2402 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002403 }
2404
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002405 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002406}
2407
2408/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002409 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002410 * @hsotg: The device instance
2411 *
2412 * The IRQ handler has detected that the RX FIFO has some data in it
2413 * that requires processing, so find out what is in there and do the
2414 * appropriate read.
2415 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002416 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002417 * chunks, so if you have x packets received on an endpoint you'll get x
2418 * FIFO events delivered, each with a packet's worth of data in it.
2419 *
2420 * When using DMA, we should not be processing events from the RXFIFO
2421 * as the actual data should be sent to the memory directly and we turn
2422 * on the completion interrupts to get notifications of transfer completion.
2423 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002424static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002425{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002426 u32 grxstsr = dwc2_readl(hsotg, GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002427 u32 epnum, status, size;
2428
2429 WARN_ON(using_dma(hsotg));
2430
Dinh Nguyen47a16852014-04-14 14:13:34 -07002431 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2432 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002433
Dinh Nguyen47a16852014-04-14 14:13:34 -07002434 size = grxstsr & GRXSTS_BYTECNT_MASK;
2435 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002436
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002437 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002438 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002439
Dinh Nguyen47a16852014-04-14 14:13:34 -07002440 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2441 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2442 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002443 break;
2444
Dinh Nguyen47a16852014-04-14 14:13:34 -07002445 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002446 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002447 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002448
2449 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002450 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002451 break;
2452
Dinh Nguyen47a16852014-04-14 14:13:34 -07002453 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002454 dev_dbg(hsotg->dev,
2455 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002456 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002457 dwc2_readl(hsotg, DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002458 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002459 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002460 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2461 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2462 */
2463 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002464 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002465 break;
2466
Dinh Nguyen47a16852014-04-14 14:13:34 -07002467 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002468 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002469 break;
2470
Dinh Nguyen47a16852014-04-14 14:13:34 -07002471 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002472 dev_dbg(hsotg->dev,
2473 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002474 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002475 dwc2_readl(hsotg, DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002476
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002477 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2478
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002479 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002480 break;
2481
2482 default:
2483 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2484 __func__, grxstsr);
2485
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002486 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487 break;
2488 }
2489}
2490
2491/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002492 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002493 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002494 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002495static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002496{
2497 switch (mps) {
2498 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002499 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002500 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002501 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002502 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002503 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002504 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002505 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002506 }
2507
2508 /* bad max packet size, warn and return invalid result */
2509 WARN_ON(1);
2510 return (u32)-1;
2511}
2512
2513/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002514 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002515 * @hsotg: The driver state.
2516 * @ep: The index number of the endpoint
2517 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002518 * @mc: The multicount value
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002519 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520 *
2521 * Configure the maximum packet size for the given endpoint, updating
2522 * the hardware control registers to reflect this.
2523 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002524static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002525 unsigned int ep, unsigned int mps,
2526 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002527{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002528 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002529 u32 reg;
2530
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002531 hs_ep = index_to_ep(hsotg, ep, dir_in);
2532 if (!hs_ep)
2533 return;
2534
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002535 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002536 u32 mps_bytes = mps;
2537
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002538 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002539 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2540 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002541 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002542 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002543 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002544 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002545 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002546 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002547 hs_ep->mc = mc;
2548 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002549 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002550 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002551 }
2552
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002553 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002554 reg = dwc2_readl(hsotg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002555 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002556 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002557 dwc2_writel(hsotg, reg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002558 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002559 reg = dwc2_readl(hsotg, DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002560 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002561 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002562 dwc2_writel(hsotg, reg, DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002563 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002564
2565 return;
2566
2567bad_mps:
2568 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2569}
2570
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002571/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002572 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002573 * @hsotg: The driver state
2574 * @idx: The index for the endpoint (0..15)
2575 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002576static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002577{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002578 dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2579 GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002580
2581 /* wait until the fifo is flushed */
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002582 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2583 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2584 __func__);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002585}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002586
2587/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002588 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002589 * @hsotg: The driver state
2590 * @hs_ep: The driver endpoint to check.
2591 *
2592 * Check to see if there is a request that has data to send, and if so
2593 * make an attempt to write data into the FIFO.
2594 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002595static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002596 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002597{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002598 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002599
Robert Baldygaafcf4162013-09-19 11:50:19 +02002600 if (!hs_ep->dir_in || !hs_req) {
2601 /**
2602 * if request is not enqueued, we disable interrupts
2603 * for endpoints, excepting ep0
2604 */
2605 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002606 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002607 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002608 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002609 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002610
2611 if (hs_req->req.actual < hs_req->req.length) {
2612 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2613 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002614 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002615 }
2616
2617 return 0;
2618}
2619
2620/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002621 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002622 * @hsotg: The device state.
2623 * @hs_ep: The endpoint that has just completed.
2624 *
2625 * An IN transfer has been completed, update the transfer's state and then
2626 * call the relevant completion routines.
2627 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002628static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002629 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002630{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002631 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002632 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002633 int size_left, size_done;
2634
2635 if (!hs_req) {
2636 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2637 return;
2638 }
2639
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002640 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002641 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2642 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002643
2644 /*
2645 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2646 * changed to IN. Change back to complete OUT transfer request
2647 */
2648 hs_ep->dir_in = 0;
2649
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002650 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002651 if (hsotg->test_mode) {
2652 int ret;
2653
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002654 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002655 if (ret < 0) {
2656 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002657 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002658 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002659 return;
2660 }
2661 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002662 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002663 return;
2664 }
2665
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002666 /*
2667 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002668 * in the endpoint size register and then working it out from
2669 * the amount we loaded for the transfer.
2670 *
2671 * We do this even for DMA, as the transfer may have incremented
2672 * past the end of the buffer (DMA transfers are always 32bit
2673 * aligned).
2674 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002675 if (using_desc_dma(hsotg)) {
2676 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2677 if (size_left < 0)
2678 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2679 size_left);
2680 } else {
2681 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2682 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002683
2684 size_done = hs_ep->size_loaded - size_left;
2685 size_done += hs_ep->last_load;
2686
2687 if (hs_req->req.actual != size_done)
2688 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2689 __func__, hs_req->req.actual, size_done);
2690
2691 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002692 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2693 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002694
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002695 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2696 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002697 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002698 return;
2699 }
2700
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002701 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002702 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002703 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002704 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002705 /* transfer will be completed on next complete interrupt */
2706 return;
2707 }
2708
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002709 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2710 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002711 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002712 return;
2713 }
2714
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002715 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002716}
2717
2718/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002719 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2720 * @hsotg: The device state.
2721 * @idx: Index of ep.
2722 * @dir_in: Endpoint direction 1-in 0-out.
2723 *
2724 * Reads for endpoint with given index and direction, by masking
2725 * epint_reg with coresponding mask.
2726 */
2727static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2728 unsigned int idx, int dir_in)
2729{
2730 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2731 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2732 u32 ints;
2733 u32 mask;
2734 u32 diepempmsk;
2735
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002736 mask = dwc2_readl(hsotg, epmsk_reg);
2737 diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002738 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2739 mask |= DXEPINT_SETUP_RCVD;
2740
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002741 ints = dwc2_readl(hsotg, epint_reg);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002742 ints &= mask;
2743 return ints;
2744}
2745
2746/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002747 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2748 * @hs_ep: The endpoint on which interrupt is asserted.
2749 *
2750 * This interrupt indicates that the endpoint has been disabled per the
2751 * application's request.
2752 *
2753 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2754 * in case of ISOC completes current request.
2755 *
2756 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2757 * request starts it.
2758 */
2759static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2760{
2761 struct dwc2_hsotg *hsotg = hs_ep->parent;
2762 struct dwc2_hsotg_req *hs_req;
2763 unsigned char idx = hs_ep->index;
2764 int dir_in = hs_ep->dir_in;
2765 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002766 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002767
2768 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2769
2770 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002771 int epctl = dwc2_readl(hsotg, epctl_reg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002772
2773 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2774
2775 if (hs_ep->isochronous) {
2776 dwc2_hsotg_complete_in(hsotg, hs_ep);
2777 return;
2778 }
2779
2780 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002781 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002782
2783 dctl |= DCTL_CGNPINNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002784 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002785 }
2786 return;
2787 }
2788
2789 if (dctl & DCTL_GOUTNAKSTS) {
2790 dctl |= DCTL_CGOUTNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002791 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002792 }
2793
2794 if (!hs_ep->isochronous)
2795 return;
2796
2797 if (list_empty(&hs_ep->queue)) {
2798 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2799 __func__, hs_ep);
2800 return;
2801 }
2802
2803 do {
2804 hs_req = get_ep_head(hs_ep);
2805 if (hs_req)
2806 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2807 -ENODATA);
2808 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04002809 /* Update current frame number value. */
2810 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002811 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2812
2813 dwc2_gadget_start_next_request(hs_ep);
2814}
2815
2816/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002817 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002818 * @ep: The endpoint on which interrupt is asserted.
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002819 *
2820 * This is starting point for ISOC-OUT transfer, synchronization done with
2821 * first out token received from host while corresponding EP is disabled.
2822 *
2823 * Device does not know initial frame in which out token will come. For this
2824 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2825 * getting this interrupt SW starts calculation for next transfer frame.
2826 */
2827static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2828{
2829 struct dwc2_hsotg *hsotg = ep->parent;
2830 int dir_in = ep->dir_in;
2831 u32 doepmsk;
2832
2833 if (dir_in || !ep->isochronous)
2834 return;
2835
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002836 if (using_desc_dma(hsotg)) {
2837 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2838 /* Start first ISO Out */
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002839 ep->target_frame = hsotg->frame_number;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002840 dwc2_gadget_start_isoc_ddma(ep);
2841 }
2842 return;
2843 }
2844
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002845 if (ep->interval > 1 &&
2846 ep->target_frame == TARGET_FRAME_INITIAL) {
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002847 u32 ctrl;
2848
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002849 ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002850 dwc2_gadget_incr_frame_num(ep);
2851
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002852 ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002853 if (ep->target_frame & 0x1)
2854 ctrl |= DXEPCTL_SETODDFR;
2855 else
2856 ctrl |= DXEPCTL_SETEVENFR;
2857
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002858 dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002859 }
2860
2861 dwc2_gadget_start_next_request(ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002862 doepmsk = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002863 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002864 dwc2_writel(hsotg, doepmsk, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002865}
2866
2867/**
John Youn38beaec2017-01-17 20:31:13 -08002868 * dwc2_gadget_handle_nak - handle NAK interrupt
2869 * @hs_ep: The endpoint on which interrupt is asserted.
2870 *
2871 * This is starting point for ISOC-IN transfer, synchronization done with
2872 * first IN token received from host while corresponding EP is disabled.
2873 *
2874 * Device does not know when first one token will arrive from host. On first
2875 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2876 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2877 * sent in response to that as there was no data in FIFO. SW is basing on this
2878 * interrupt to obtain frame in which token has come and then based on the
2879 * interval calculates next frame for transfer.
2880 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002881static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2882{
2883 struct dwc2_hsotg *hsotg = hs_ep->parent;
2884 int dir_in = hs_ep->dir_in;
2885
2886 if (!dir_in || !hs_ep->isochronous)
2887 return;
2888
2889 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002890
2891 if (using_desc_dma(hsotg)) {
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002892 hs_ep->target_frame = hsotg->frame_number;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002893 dwc2_gadget_incr_frame_num(hs_ep);
Grigor Tovmasyan48dac4e2018-08-29 21:00:33 +04002894
2895 /* In service interval mode target_frame must
2896 * be set to last (u)frame of the service interval.
2897 */
2898 if (hsotg->params.service_interval) {
2899 /* Set target_frame to the first (u)frame of
2900 * the service interval
2901 */
2902 hs_ep->target_frame &= ~hs_ep->interval + 1;
2903
2904 /* Set target_frame to the last (u)frame of
2905 * the service interval
2906 */
2907 dwc2_gadget_incr_frame_num(hs_ep);
2908 dwc2_gadget_dec_frame_num_by_one(hs_ep);
2909 }
2910
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002911 dwc2_gadget_start_isoc_ddma(hs_ep);
2912 return;
2913 }
2914
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002915 hs_ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002916 if (hs_ep->interval > 1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002917 u32 ctrl = dwc2_readl(hsotg,
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002918 DIEPCTL(hs_ep->index));
2919 if (hs_ep->target_frame & 0x1)
2920 ctrl |= DXEPCTL_SETODDFR;
2921 else
2922 ctrl |= DXEPCTL_SETEVENFR;
2923
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002924 dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002925 }
2926
2927 dwc2_hsotg_complete_request(hsotg, hs_ep,
2928 get_ep_head(hs_ep), 0);
2929 }
2930
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002931 if (!using_desc_dma(hsotg))
2932 dwc2_gadget_incr_frame_num(hs_ep);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002933}
2934
2935/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002936 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002937 * @hsotg: The driver state
2938 * @idx: The index for the endpoint (0..15)
2939 * @dir_in: Set if this is an IN endpoint
2940 *
2941 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002942 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002943static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002944 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002945{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002946 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002947 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2948 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2949 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002950 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002951 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002952
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002953 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002954 ctrl = dwc2_readl(hsotg, epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002955
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002956 /* Clear endpoint interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002957 dwc2_writel(hsotg, ints, epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002958
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002959 if (!hs_ep) {
2960 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002961 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002962 return;
2963 }
2964
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002965 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2966 __func__, idx, dir_in ? "in" : "out", ints);
2967
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002968 /* Don't process XferCompl interrupt if it is a setup packet */
2969 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2970 ints &= ~DXEPINT_XFERCOMPL;
2971
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002972 /*
2973 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2974 * stage and xfercomplete was generated without SETUP phase done
2975 * interrupt. SW should parse received setup packet only after host's
2976 * exit from setup phase of control transfer.
2977 */
2978 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2979 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2980 ints &= ~DXEPINT_XFERCOMPL;
2981
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002982 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002983 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002984 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002985 __func__, dwc2_readl(hsotg, epctl_reg),
2986 dwc2_readl(hsotg, epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002987
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002988 /* In DDMA handle isochronous requests separately */
2989 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002990 /* XferCompl set along with BNA */
2991 if (!(ints & DXEPINT_BNAINTR))
2992 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002993 } else if (dir_in) {
2994 /*
2995 * We get OutDone from the FIFO, so we only
2996 * need to look at completing IN requests here
2997 * if operating slave mode
2998 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002999 if (hs_ep->isochronous && hs_ep->interval > 1)
3000 dwc2_gadget_incr_frame_num(hs_ep);
3001
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003002 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003003 if (ints & DXEPINT_NAKINTRPT)
3004 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003005
Ben Dooksc9a64ea2010-07-19 09:40:46 +01003006 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003007 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003008 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003009 /*
3010 * We're using DMA, we need to fire an OutDone here
3011 * as we ignore the RXFIFO.
3012 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003013 if (hs_ep->isochronous && hs_ep->interval > 1)
3014 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003015
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003016 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003017 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003018 }
3019
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07003020 if (ints & DXEPINT_EPDISBLD)
3021 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003022
Vardan Mikayelyan53219222016-05-25 18:07:14 -07003023 if (ints & DXEPINT_OUTTKNEPDIS)
3024 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
3025
3026 if (ints & DXEPINT_NAKINTRPT)
3027 dwc2_gadget_handle_nak(hs_ep);
3028
Dinh Nguyen47a16852014-04-14 14:13:34 -07003029 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003030 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003031
Dinh Nguyen47a16852014-04-14 14:13:34 -07003032 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003033 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
3034
3035 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003036 /*
3037 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003038 * setup packet. In non-DMA mode we'd get this
3039 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003040 * the setup here.
3041 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003042
3043 if (dir_in)
3044 WARN_ON_ONCE(1);
3045 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003046 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003047 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003048 }
3049
Vahram Aharonyanef750c72016-11-14 19:16:31 -08003050 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003051 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
3052
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04003053 /* Safety check EP0 state when STSPHSERCVD asserted */
3054 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
3055 /* Move to STATUS IN for DDMA */
3056 if (using_desc_dma(hsotg))
3057 dwc2_hsotg_ep0_zlp(hsotg, true);
3058 }
3059
Vahram Aharonyanef750c72016-11-14 19:16:31 -08003060 }
3061
Dinh Nguyen47a16852014-04-14 14:13:34 -07003062 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003063 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003064
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003065 if (ints & DXEPINT_BNAINTR) {
3066 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003067 if (hs_ep->isochronous)
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003068 dwc2_gadget_handle_isoc_bna(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003069 }
3070
Robert Baldyga1479e842013-10-09 08:41:57 +02003071 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003072 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003073 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003074 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
3075 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003076 }
3077
3078 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003079 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003080 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
3081 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003083
3084 /* FIFO has space or is empty (see GAHBCFG) */
3085 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003086 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01003087 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3088 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09003089 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003090 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01003091 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003092 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003093}
3094
3095/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003096 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003097 * @hsotg: The device state.
3098 *
3099 * Handle updating the device settings after the enumeration phase has
3100 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003101 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003102static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003103{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003104 u32 dsts = dwc2_readl(hsotg, DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003105 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003106
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003107 /*
3108 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003109 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003110 * we connected at.
3111 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003112
3113 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3114
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003115 /*
3116 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003117 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003118 * not advertise a 64byte MPS on EP0.
3119 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003120
3121 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003122 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003123 case DSTS_ENUMSPD_FS:
3124 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003125 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003126 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003127 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003128 break;
3129
Dinh Nguyen47a16852014-04-14 14:13:34 -07003130 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003131 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003132 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003133 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003134 break;
3135
Dinh Nguyen47a16852014-04-14 14:13:34 -07003136 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003137 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003138 ep0_mps = 8;
3139 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003140 /*
3141 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003142 * moment, and the documentation seems to imply that it isn't
3143 * supported by the PHYs on some of the devices.
3144 */
3145 break;
3146 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003147 dev_info(hsotg->dev, "new device is %s\n",
3148 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003149
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003150 /*
3151 * we should now know the maximum packet size for an
3152 * endpoint, so set the endpoints to a default value.
3153 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003154
3155 if (ep0_mps) {
3156 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003157 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003158 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3159 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003160 for (i = 1; i < hsotg->num_of_eps; i++) {
3161 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003162 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3163 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003164 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003165 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3166 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003167 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003168 }
3169
3170 /* ensure after enumeration our EP0 is active */
3171
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003172 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003173
3174 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003175 dwc2_readl(hsotg, DIEPCTL0),
3176 dwc2_readl(hsotg, DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003177}
3178
3179/**
3180 * kill_all_requests - remove all requests from the endpoint's queue
3181 * @hsotg: The device state.
3182 * @ep: The endpoint the requests may be on.
3183 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003184 *
3185 * Go through the requests on the given endpoint and mark them
3186 * completed with the given result code.
3187 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003188static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003189 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af42014-12-16 11:51:44 +01003190 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003191{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003192 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003193 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003194
Robert Baldyga6b448af42014-12-16 11:51:44 +01003195 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003196
Robert Baldyga6b448af42014-12-16 11:51:44 +01003197 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003198 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003199 result);
Robert Baldyga6b448af42014-12-16 11:51:44 +01003200
Robert Baldygab203d0a2014-09-09 10:44:56 +02003201 if (!hsotg->dedicated_fifos)
3202 return;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003203 size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003204 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003205 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003206}
3207
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003208/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003209 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003210 * @hsotg: The device state.
3211 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003212 * The device has been disconnected. Remove all current
3213 * transactions and signal the gadget driver that this
3214 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003215 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003216void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003217{
John Youn9da51972017-01-17 20:30:27 -08003218 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003219
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003220 if (!hsotg->connected)
3221 return;
3222
3223 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003224 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003225
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003226 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003227 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3228 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003229 kill_all_requests(hsotg, hsotg->eps_in[ep],
3230 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003231 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003232 kill_all_requests(hsotg, hsotg->eps_out[ep],
3233 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003234 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003235
3236 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003237 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003238
3239 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003240}
3241
3242/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003243 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003244 * @hsotg: The device state:
3245 * @periodic: True if this is a periodic FIFO interrupt
3246 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003247static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003248{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003249 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003250 int epno, ret;
3251
3252 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003253 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003254 ep = index_to_ep(hsotg, epno, 1);
3255
3256 if (!ep)
3257 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003258
3259 if (!ep->dir_in)
3260 continue;
3261
3262 if ((periodic && !ep->periodic) ||
3263 (!periodic && ep->periodic))
3264 continue;
3265
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003266 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003267 if (ret < 0)
3268 break;
3269 }
3270}
3271
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003272/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003273#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3274 GINTSTS_PTXFEMP | \
3275 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003276
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003277static int dwc2_hsotg_ep_disable(struct usb_ep *ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003278/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003279 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003280 * @hsotg: The device state
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04003281 * @is_usb_reset: Usb resetting flag
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003282 *
3283 * Issue a soft reset to the core, and await the core finishing it.
3284 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003285void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003286 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003287{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003288 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003289 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003290 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003291 u32 dcfg = 0;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003292 int ep;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003293
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003294 /* Kill any ep0 requests as controller will be reinitialized */
3295 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3296
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003297 if (!is_usb_reset) {
John Stultz6e6360b2017-01-23 14:59:14 -08003298 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003299 return;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003300 } else {
3301 /* all endpoints should be shutdown */
3302 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3303 if (hsotg->eps_in[ep])
3304 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3305 if (hsotg->eps_out[ep])
3306 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3307 }
3308 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003309
3310 /*
3311 * we must now enable ep0 ready for host detection and then
3312 * set configuration.
3313 */
3314
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003315 /* keep other bits untouched (so e.g. forced modes are not lost) */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003316 usbcfg = dwc2_readl(hsotg, GUSBCFG);
Jules Maselbas1e868542019-04-05 15:35:33 +02003317 usbcfg &= ~GUSBCFG_TOUTCAL_MASK;
Jules Maselbas707d80f2019-04-05 15:35:31 +02003318 usbcfg |= GUSBCFG_TOUTCAL(7);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003319
Jules Maselbas1e868542019-04-05 15:35:33 +02003320 /* remove the HNP/SRP and set the PHY */
3321 usbcfg &= ~(GUSBCFG_SRPCAP | GUSBCFG_HNPCAP);
3322 dwc2_writel(hsotg, usbcfg, GUSBCFG);
Jules Maselbas707d80f2019-04-05 15:35:31 +02003323
Jules Maselbas1e868542019-04-05 15:35:33 +02003324 dwc2_phy_init(hsotg, true);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003325
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003326 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003327
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003328 if (!is_usb_reset)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003329 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003330
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003331 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003332
3333 switch (hsotg->params.speed) {
3334 case DWC2_SPEED_PARAM_LOW:
3335 dcfg |= DCFG_DEVSPD_LS;
3336 break;
3337 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003338 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3339 dcfg |= DCFG_DEVSPD_FS48;
3340 else
3341 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003342 break;
3343 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003344 dcfg |= DCFG_DEVSPD_HS;
3345 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003346
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +04003347 if (hsotg->params.ipg_isoc_en)
3348 dcfg |= DCFG_IPG_ISOC_SUPPORDED;
3349
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003350 dwc2_writel(hsotg, dcfg, DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003351
3352 /* Clear any pending OTG interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003353 dwc2_writel(hsotg, 0xffffffff, GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003354
3355 /* Clear any pending interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003356 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003357 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003358 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003359 GINTSTS_USBRST | GINTSTS_RESETDET |
3360 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003361 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3362 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003363
3364 if (!using_desc_dma(hsotg))
3365 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003366
John Youn95832c02017-01-23 14:57:26 -08003367 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003368 intmsk |= GINTSTS_CONIDSTSCHNG;
3369
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003370 dwc2_writel(hsotg, intmsk, GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003371
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003372 if (using_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003373 dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003374 hsotg->params.ahbcfg,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003375 GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003376
3377 /* Set DDMA mode support in the core if needed */
3378 if (using_desc_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003379 dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003380
3381 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003382 dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ?
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003383 (GAHBCFG_NP_TXF_EMP_LVL |
3384 GAHBCFG_P_TXF_EMP_LVL) : 0) |
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003385 GAHBCFG_GLBL_INTR_EN, GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003386 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003387
3388 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003389 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3390 * when we have no data to transfer. Otherwise we get being flooded by
3391 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003392 */
3393
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003394 dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003395 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003396 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003397 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003398 DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003399
3400 /*
3401 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003402 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003403 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003404 dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003405 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003406 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003407 DOEPMSK_SETUPMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003408 DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003409
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003410 /* Enable BNA interrupt for DDMA */
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003411 if (using_desc_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003412 dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK);
3413 dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK);
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003414 }
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003415
Grigor Tovmasyanca531bc2018-08-29 20:59:34 +04003416 /* Enable Service Interval mode if supported */
3417 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3418 dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED);
3419
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003420 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003421
3422 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003423 dwc2_readl(hsotg, DIEPCTL0),
3424 dwc2_readl(hsotg, DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003425
3426 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003427 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003428
3429 /*
3430 * Enable the RXFIFO when in slave mode, as this is how we collect
3431 * the data. In DMA mode, we get events from the FIFO but also
3432 * things we cannot process, so do not use it.
3433 */
3434 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003435 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003436
3437 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003438 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3439 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003440
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003441 if (!is_usb_reset) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003442 dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003443 udelay(10); /* see openiboot */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003444 dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003445 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003446
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003447 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003448
3449 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003450 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003451 * writing to the EPCTL register..
3452 */
3453
3454 /* set to read 1 8byte packet */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003455 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
3456 DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003457
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003458 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003459 DXEPCTL_CNAK | DXEPCTL_EPENA |
3460 DXEPCTL_USBACTEP,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003461 DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003462
3463 /* enable, but don't activate EP0in */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003464 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3465 DXEPCTL_USBACTEP, DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003466
Lukasz Majewski308d7342012-05-04 14:17:05 +02003467 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003468 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3469 if (!is_usb_reset)
3470 val |= DCTL_SFTDISCON;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003471 dwc2_set_bit(hsotg, DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003472
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003473 /* configure the core to support LPM */
3474 dwc2_gadget_init_lpm(hsotg);
3475
Grigor Tovmasyan15d9dbf2018-08-29 21:01:59 +04003476 /* program GREFCLK register if needed */
3477 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3478 dwc2_gadget_program_ref_clk(hsotg);
3479
Lukasz Majewski308d7342012-05-04 14:17:05 +02003480 /* must be at-least 3ms to allow bus to see disconnect */
3481 mdelay(3);
3482
Gregory Herrero065d3932015-09-22 15:16:54 +02003483 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003484
3485 dwc2_hsotg_enqueue_setup(hsotg);
3486
3487 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003488 dwc2_readl(hsotg, DIEPCTL0),
3489 dwc2_readl(hsotg, DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003490}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003491
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003492static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003493{
3494 /* set the soft-disconnect bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003495 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003496}
3497
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003498void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003499{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003500 /* remove the soft-disconnect and let's go */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003501 dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003502}
3503
3504/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003505 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3506 * @hsotg: The device state:
3507 *
3508 * This interrupt indicates one of the following conditions occurred while
3509 * transmitting an ISOC transaction.
3510 * - Corrupted IN Token for ISOC EP.
3511 * - Packet not complete in FIFO.
3512 *
3513 * The following actions will be taken:
3514 * - Determine the EP
3515 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3516 */
3517static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3518{
3519 struct dwc2_hsotg_ep *hs_ep;
3520 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003521 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003522 u32 idx;
3523
3524 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3525
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003526 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003527
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003528 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003529 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003530 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003531 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003532 continue;
3533
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003534 epctrl = dwc2_readl(hsotg, DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003535 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003536 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3537 epctrl |= DXEPCTL_SNAK;
3538 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003539 dwc2_writel(hsotg, epctrl, DIEPCTL(idx));
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003540 }
3541 }
3542
3543 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003544 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003545}
3546
3547/**
3548 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3549 * @hsotg: The device state:
3550 *
3551 * This interrupt indicates one of the following conditions occurred while
3552 * transmitting an ISOC transaction.
3553 * - Corrupted OUT Token for ISOC EP.
3554 * - Packet not complete in FIFO.
3555 *
3556 * The following actions will be taken:
3557 * - Determine the EP
3558 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3559 */
3560static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3561{
3562 u32 gintsts;
3563 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003564 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003565 u32 epctrl;
3566 struct dwc2_hsotg_ep *hs_ep;
3567 int idx;
3568
3569 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3570
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003571 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003572 daintmsk >>= DAINT_OUTEP_SHIFT;
3573
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003574 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003575 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003576 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003577 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003578 continue;
3579
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003580 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003581 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003582 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3583 /* Unmask GOUTNAKEFF interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003584 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003585 gintmsk |= GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003586 dwc2_writel(hsotg, gintmsk, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003587
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003588 gintsts = dwc2_readl(hsotg, GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003589 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003590 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003591 break;
3592 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003593 }
3594 }
3595
3596 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003597 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003598}
3599
3600/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003601 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003602 * @irq: The IRQ number triggered
3603 * @pw: The pw value when registered the handler.
3604 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003605static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003606{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003607 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003608 int retry_count = 8;
3609 u32 gintsts;
3610 u32 gintmsk;
3611
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003612 if (!dwc2_is_device_mode(hsotg))
3613 return IRQ_NONE;
3614
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003615 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003616irq_retry:
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003617 gintsts = dwc2_readl(hsotg, GINTSTS);
3618 gintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003619
3620 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3621 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3622
3623 gintsts &= gintmsk;
3624
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003625 if (gintsts & GINTSTS_RESETDET) {
3626 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3627
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003628 dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003629
3630 /* This event must be used only if controller is suspended */
3631 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003632 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003633 hsotg->lx_state = DWC2_L0;
3634 }
3635 }
3636
3637 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003638 u32 usb_status = dwc2_readl(hsotg, GOTGCTL);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003639 u32 connected = hsotg->connected;
3640
3641 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3642 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003643 dwc2_readl(hsotg, GNPTXSTS));
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003644
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003645 dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003646
3647 /* Report disconnection if it is not already done. */
3648 dwc2_hsotg_disconnect(hsotg);
3649
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003650 /* Reset device address to zero */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003651 dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003652
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003653 if (usb_status & GOTGCTL_BSESVLD && connected)
3654 dwc2_hsotg_core_init_disconnected(hsotg, true);
3655 }
3656
Dinh Nguyen47a16852014-04-14 14:13:34 -07003657 if (gintsts & GINTSTS_ENUMDONE) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003658 dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003659
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003660 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003661 }
3662
Dinh Nguyen47a16852014-04-14 14:13:34 -07003663 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003664 u32 daint = dwc2_readl(hsotg, DAINT);
3665 u32 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003666 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003667 int ep;
3668
Robert Baldyga7e804652013-09-19 11:50:20 +02003669 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003670 daint_out = daint >> DAINT_OUTEP_SHIFT;
3671 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003672
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003673 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3674
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003675 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3676 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003677 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003678 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003679 }
3680
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003681 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3682 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003683 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003684 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003685 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003686 }
3687
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003688 /* check both FIFOs */
3689
Dinh Nguyen47a16852014-04-14 14:13:34 -07003690 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003691 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3692
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003693 /*
3694 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003695 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003696 * it needs re-enabling
3697 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003698
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003699 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3700 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003701 }
3702
Dinh Nguyen47a16852014-04-14 14:13:34 -07003703 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003704 dev_dbg(hsotg->dev, "PTxFEmp\n");
3705
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003706 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003707
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003708 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3709 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003710 }
3711
Dinh Nguyen47a16852014-04-14 14:13:34 -07003712 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003713 /*
3714 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003715 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003716 * set.
3717 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003718
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003719 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003720 }
3721
Dinh Nguyen47a16852014-04-14 14:13:34 -07003722 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003723 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003724 dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003725 }
3726
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003727 /*
3728 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003729 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003730 * the occurrence.
3731 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003732
Dinh Nguyen47a16852014-04-14 14:13:34 -07003733 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003734 u8 idx;
3735 u32 epctrl;
3736 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003737 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003738 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003739
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003740 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyand8484552018-01-19 14:41:42 +04003741 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003742 /* Mask this interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003743 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003744 gintmsk &= ~GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003745 dwc2_writel(hsotg, gintmsk, GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003746
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003747 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003748 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003749 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003750 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003751 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyand8484552018-01-19 14:41:42 +04003752 continue;
3753
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003754 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003755
Razmik Karapetyand8484552018-01-19 14:41:42 +04003756 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003757 epctrl |= DXEPCTL_SNAK;
3758 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003759 dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003760 }
3761 }
3762
3763 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003764 }
3765
Dinh Nguyen47a16852014-04-14 14:13:34 -07003766 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003767 dev_info(hsotg->dev, "GINNakEff triggered\n");
3768
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003769 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003770
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003771 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003772 }
3773
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003774 if (gintsts & GINTSTS_INCOMPL_SOIN)
3775 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003776
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003777 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3778 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003779
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003780 /*
3781 * if we've had fifo events, we should try and go around the
3782 * loop again to see if there's any point in returning yet.
3783 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003784
3785 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003786 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003787
Grigor Tovmasyan187c5292018-08-29 21:02:57 +04003788 /* Check WKUP_ALERT interrupt*/
3789 if (hsotg->params.service_interval)
3790 dwc2_gadget_wkup_alert_handler(hsotg);
3791
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003792 spin_unlock(&hsotg->lock);
3793
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003794 return IRQ_HANDLED;
3795}
3796
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003797static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3798 struct dwc2_hsotg_ep *hs_ep)
3799{
3800 u32 epctrl_reg;
3801 u32 epint_reg;
3802
3803 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3804 DOEPCTL(hs_ep->index);
3805 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3806 DOEPINT(hs_ep->index);
3807
3808 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3809 hs_ep->name);
3810
3811 if (hs_ep->dir_in) {
3812 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003813 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003814 /* Wait for Nak effect */
3815 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3816 DXEPINT_INEPNAKEFF, 100))
3817 dev_warn(hsotg->dev,
3818 "%s: timeout DIEPINT.NAKEFF\n",
3819 __func__);
3820 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003821 dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003822 /* Wait for Nak effect */
3823 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3824 GINTSTS_GINNAKEFF, 100))
3825 dev_warn(hsotg->dev,
3826 "%s: timeout GINTSTS.GINNAKEFF\n",
3827 __func__);
3828 }
3829 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003830 if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF))
3831 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003832
3833 /* Wait for global nak to take effect */
3834 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3835 GINTSTS_GOUTNAKEFF, 100))
3836 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3837 __func__);
3838 }
3839
3840 /* Disable ep */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003841 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003842
3843 /* Wait for ep to be disabled */
3844 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3845 dev_warn(hsotg->dev,
3846 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3847
3848 /* Clear EPDISBLD interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003849 dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003850
3851 if (hs_ep->dir_in) {
3852 unsigned short fifo_index;
3853
3854 if (hsotg->dedicated_fifos || hs_ep->periodic)
3855 fifo_index = hs_ep->fifo_index;
3856 else
3857 fifo_index = 0;
3858
3859 /* Flush TX FIFO */
3860 dwc2_flush_tx_fifo(hsotg, fifo_index);
3861
3862 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3863 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003864 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003865
3866 } else {
3867 /* Remove global NAKs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003868 dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003869 }
3870}
3871
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003872/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003873 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003874 * @ep: The USB endpint to configure
3875 * @desc: The USB endpoint descriptor to configure with.
3876 *
3877 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003878 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003879static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003880 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003881{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003882 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003883 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003884 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003885 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003886 u32 epctrl_reg;
3887 u32 epctrl;
3888 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003889 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003890 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003891 unsigned int dir_in;
3892 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003893 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003894 unsigned char ep_type;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003895
3896 dev_dbg(hsotg->dev,
3897 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3898 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3899 desc->wMaxPacketSize, desc->bInterval);
3900
3901 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003902 if (index == 0) {
3903 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3904 return -EINVAL;
3905 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003906
3907 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3908 if (dir_in != hs_ep->dir_in) {
3909 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3910 return -EINVAL;
3911 }
3912
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003913 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003914 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003915 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003916
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003917 /* ISOC IN in DDMA supported bInterval up to 10 */
3918 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3919 dir_in && desc->bInterval > 10) {
3920 dev_err(hsotg->dev,
3921 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3922 return -EINVAL;
3923 }
3924
3925 /* High bandwidth ISOC OUT in DDMA not supported */
3926 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3927 !dir_in && mc > 1) {
3928 dev_err(hsotg->dev,
3929 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3930 return -EINVAL;
3931 }
3932
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003933 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003934
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003935 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003936 epctrl = dwc2_readl(hsotg, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003937
3938 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3939 __func__, epctrl, epctrl_reg);
3940
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003941 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003942 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3943 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003944 MAX_DMA_DESC_NUM_GENERIC *
3945 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003946 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003947 if (!hs_ep->desc_list) {
3948 ret = -ENOMEM;
3949 goto error2;
3950 }
3951 }
3952
Lukasz Majewski22258f42012-06-14 10:02:24 +02003953 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003954
Dinh Nguyen47a16852014-04-14 14:13:34 -07003955 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3956 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003957
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003958 /*
3959 * mark the endpoint as active, otherwise the core may ignore
3960 * transactions entirely for this endpoint
3961 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003962 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003963
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003964 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003965 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003966
3967 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003968 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003969 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003970 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003971 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003972
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003973 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003974 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003975 epctrl |= DXEPCTL_EPTYPE_ISO;
3976 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003977 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003978 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003979 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003980 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003981 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003982 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003983 hs_ep->periodic = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003984 mask = dwc2_readl(hsotg, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003985 mask |= DIEPMSK_NAKMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003986 dwc2_writel(hsotg, mask, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003987 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003988 mask = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003989 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003990 dwc2_writel(hsotg, mask, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003991 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003992 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003993
3994 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003995 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003996 break;
3997
3998 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003999 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004000 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004001
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07004002 if (hsotg->gadget.speed == USB_SPEED_HIGH)
4003 hs_ep->interval = 1 << (desc->bInterval - 1);
4004
Dinh Nguyen47a16852014-04-14 14:13:34 -07004005 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004006 break;
4007
4008 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07004009 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004010 break;
4011 }
4012
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004013 /*
4014 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01004015 * a unique tx-fifo even if it is non-periodic.
4016 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07004017 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004018 u32 fifo_index = 0;
4019 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08004020
4021 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01004022 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08004023 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02004024 continue;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004025 val = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08004026 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02004027 if (val < size)
4028 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004029 /* Search for smallest acceptable fifo */
4030 if (val < fifo_size) {
4031 fifo_size = val;
4032 fifo_index = i;
4033 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02004034 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004035 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01004036 dev_err(hsotg->dev,
4037 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05304038 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004039 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05304040 }
Minas Harutyunyan97311c82019-01-31 18:28:07 +04004041 epctrl &= ~(DXEPCTL_TXFNUM_LIMIT << DXEPCTL_TXFNUM_SHIFT);
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004042 hsotg->fifo_map |= 1 << fifo_index;
4043 epctrl |= DXEPCTL_TXFNUM(fifo_index);
4044 hs_ep->fifo_index = fifo_index;
4045 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02004046 }
Ben Dooks10aebc72010-07-19 09:40:44 +01004047
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004048 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004049 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07004050 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004051
Artur Petrosyan52953222018-04-16 08:45:31 -04004052 /* WA for Full speed ISOC IN in DDMA mode.
4053 * By Clear NAK status of EP, core will send ZLP
4054 * to IN token and assert NAK interrupt relying
4055 * on TxFIFO status only
4056 */
4057
4058 if (hsotg->gadget.speed == USB_SPEED_FULL &&
4059 hs_ep->isochronous && dir_in) {
4060 /* The WA applies only to core versions from 2.72a
4061 * to 4.00a (including both). Also for FS_IOT_1.00a
4062 * and HS_IOT_1.00a.
4063 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004064 u32 gsnpsid = dwc2_readl(hsotg, GSNPSID);
Artur Petrosyan52953222018-04-16 08:45:31 -04004065
4066 if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
4067 gsnpsid <= DWC2_CORE_REV_4_00a) ||
4068 gsnpsid == DWC2_FS_IOT_REV_1_00a ||
4069 gsnpsid == DWC2_HS_IOT_REV_1_00a)
4070 epctrl |= DXEPCTL_CNAK;
4071 }
4072
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004073 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
4074 __func__, epctrl);
4075
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004076 dwc2_writel(hsotg, epctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004077 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004078 __func__, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004079
4080 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004081 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004082
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004083error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02004084 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004085
4086error2:
4087 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08004088 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004089 sizeof(struct dwc2_dma_desc),
4090 hs_ep->desc_list, hs_ep->desc_list_dma);
4091 hs_ep->desc_list = NULL;
4092 }
4093
Julia Lawall19c190f2010-03-29 17:36:44 +02004094 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004095}
4096
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004097/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004098 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004099 * @ep: The endpoint to disable.
4100 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004101static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004102{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004103 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004104 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004105 int dir_in = hs_ep->dir_in;
4106 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004107 u32 epctrl_reg;
4108 u32 ctrl;
4109
Marek Szyprowski1e011292014-09-09 10:44:54 +02004110 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004111
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004112 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004113 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4114 return -EINVAL;
4115 }
4116
John Stultz9b4810922017-10-23 14:32:49 -07004117 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4118 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4119 return -EINVAL;
4120 }
4121
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02004122 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004123
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004124 ctrl = dwc2_readl(hsotg, epctrl_reg);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08004125
4126 if (ctrl & DXEPCTL_EPENA)
4127 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4128
Dinh Nguyen47a16852014-04-14 14:13:34 -07004129 ctrl &= ~DXEPCTL_EPENA;
4130 ctrl &= ~DXEPCTL_USBACTEP;
4131 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004132
4133 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004134 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004135
4136 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004137 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004138
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01004139 /* terminate all requests with shutdown */
4140 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4141
Robert Baldyga1c07b202016-08-29 13:39:00 -07004142 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4143 hs_ep->fifo_index = 0;
4144 hs_ep->fifo_size = 0;
4145
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004146 return 0;
4147}
4148
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004149static int dwc2_hsotg_ep_disable_lock(struct usb_ep *ep)
4150{
4151 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4152 struct dwc2_hsotg *hsotg = hs_ep->parent;
4153 unsigned long flags;
4154 int ret;
4155
4156 spin_lock_irqsave(&hsotg->lock, flags);
4157 ret = dwc2_hsotg_ep_disable(ep);
4158 spin_unlock_irqrestore(&hsotg->lock, flags);
4159 return ret;
4160}
4161
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004162/**
4163 * on_list - check request is on the given endpoint
4164 * @ep: The endpoint to check.
4165 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004166 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004167static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004168{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004169 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004170
4171 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4172 if (req == test)
4173 return true;
4174 }
4175
4176 return false;
4177}
4178
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004179/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004180 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004181 * @ep: The endpoint to dequeue.
4182 * @req: The request to be removed from a queue.
4183 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004184static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004185{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004186 struct dwc2_hsotg_req *hs_req = our_req(req);
4187 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004188 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004189 unsigned long flags;
4190
Marek Szyprowski1e011292014-09-09 10:44:54 +02004191 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004192
Lukasz Majewski22258f42012-06-14 10:02:24 +02004193 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004194
4195 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004196 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004197 return -EINVAL;
4198 }
4199
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004200 /* Dequeue already started request */
4201 if (req == &hs_ep->req->req)
4202 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4203
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004204 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004205 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004206
4207 return 0;
4208}
4209
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004210/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004211 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004212 * @ep: The endpoint to set halt.
4213 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004214 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4215 * the endpoint is busy processing requests.
4216 *
4217 * We need to stall the endpoint immediately if request comes from set_feature
4218 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004219 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004220static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004221{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004222 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004223 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004224 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004225 u32 epreg;
4226 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004227 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004228
4229 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4230
Robert Baldygac9f721b2014-01-14 08:36:00 +01004231 if (index == 0) {
4232 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004233 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004234 else
4235 dev_warn(hs->dev,
4236 "%s: can't clear halt on ep0\n", __func__);
4237 return 0;
4238 }
4239
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004240 if (hs_ep->isochronous) {
4241 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4242 return -EINVAL;
4243 }
4244
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004245 if (!now && value && !list_empty(&hs_ep->queue)) {
4246 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4247 ep->name);
4248 return -EAGAIN;
4249 }
4250
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004251 if (hs_ep->dir_in) {
4252 epreg = DIEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004253 epctl = dwc2_readl(hs, epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004254
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004255 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004256 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004257 if (epctl & DXEPCTL_EPENA)
4258 epctl |= DXEPCTL_EPDIS;
4259 } else {
4260 epctl &= ~DXEPCTL_STALL;
4261 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4262 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004263 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004264 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004265 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004266 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004267 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004268 epreg = DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004269 epctl = dwc2_readl(hs, epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004270
John Youn34c0887f2017-01-17 20:31:43 -08004271 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004272 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004273 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004274 epctl &= ~DXEPCTL_STALL;
4275 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4276 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004277 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004278 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004279 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004280 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004281 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004282
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004283 hs_ep->halted = value;
4284
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004285 return 0;
4286}
4287
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004288/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004289 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004290 * @ep: The endpoint to set halt.
4291 * @value: Set or unset the halt.
4292 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004293static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004294{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004295 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004296 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004297 unsigned long flags = 0;
4298 int ret = 0;
4299
4300 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004301 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004302 spin_unlock_irqrestore(&hs->lock, flags);
4303
4304 return ret;
4305}
4306
Bhumika Goyalebce5612017-08-12 17:34:55 +05304307static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004308 .enable = dwc2_hsotg_ep_enable,
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004309 .disable = dwc2_hsotg_ep_disable_lock,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004310 .alloc_request = dwc2_hsotg_ep_alloc_request,
4311 .free_request = dwc2_hsotg_ep_free_request,
4312 .queue = dwc2_hsotg_ep_queue_lock,
4313 .dequeue = dwc2_hsotg_ep_dequeue,
4314 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004315 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004316};
4317
4318/**
John Youn9da51972017-01-17 20:30:27 -08004319 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004320 * @hsotg: The driver state
4321 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004322static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004323{
4324 /* unmask subset of endpoint interrupts */
4325
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004326 dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004327 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004328 DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004329
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004330 dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004331 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004332 DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004333
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004334 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004335
4336 /* Be in disconnected state until gadget is registered */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004337 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004338
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004339 /* setup fifos */
4340
4341 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004342 dwc2_readl(hsotg, GRXFSIZ),
4343 dwc2_readl(hsotg, GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004344
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004345 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004346
Gregory Herrerof5090042015-01-09 13:38:47 +01004347 if (using_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004348 dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004349}
4350
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004351/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004352 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004353 * @gadget: The usb gadget state
4354 * @driver: The usb gadget driver
4355 *
4356 * Perform initialization to prepare udc device and driver
4357 * to work.
4358 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004359static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004360 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004361{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004362 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004363 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004364 int ret;
4365
4366 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004367 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004368 return -ENODEV;
4369 }
4370
4371 if (!driver) {
4372 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4373 return -EINVAL;
4374 }
4375
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004376 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004377 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004378
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004379 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004380 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4381 return -EINVAL;
4382 }
4383
4384 WARN_ON(hsotg->driver);
4385
4386 driver->driver.bus = NULL;
4387 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004388 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004389 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4390
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004391 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4392 ret = dwc2_lowlevel_hw_enable(hsotg);
4393 if (ret)
4394 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004395 }
4396
Gregory Herrerof6c01592015-01-09 13:38:41 +01004397 if (!IS_ERR_OR_NULL(hsotg->uphy))
4398 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004399
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004400 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004401 if (dwc2_hw_is_device(hsotg)) {
4402 dwc2_hsotg_init(hsotg);
4403 dwc2_hsotg_core_init_disconnected(hsotg, false);
4404 }
4405
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004406 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004407 spin_unlock_irqrestore(&hsotg->lock, flags);
4408
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01004409 gadget->sg_supported = using_desc_dma(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004410 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004411
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004412 return 0;
4413
4414err:
4415 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004416 return ret;
4417}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004418
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004419/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004420 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004421 * @gadget: The usb gadget state
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004422 *
4423 * Stop udc hw block and stay tunned for future transmissions
4424 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004425static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004426{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004427 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004428 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004429 int ep;
4430
4431 if (!hsotg)
4432 return -ENODEV;
4433
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004434 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004435 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4436 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004437 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004438 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004439 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004440 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004441
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004442 spin_lock_irqsave(&hsotg->lock, flags);
4443
Marek Szyprowski32805c32014-10-20 12:45:33 +02004444 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004445 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004446 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004447
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004448 spin_unlock_irqrestore(&hsotg->lock, flags);
4449
Gregory Herrerof6c01592015-01-09 13:38:41 +01004450 if (!IS_ERR_OR_NULL(hsotg->uphy))
4451 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004452
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004453 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4454 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004455
4456 return 0;
4457}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004458
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004459/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004460 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004461 * @gadget: The usb gadget state
4462 *
4463 * Read the {micro} frame number
4464 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004465static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004466{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004467 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004468}
4469
Lukasz Majewskia188b682012-06-22 09:29:56 +02004470/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004471 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004472 * @gadget: The usb gadget state
4473 * @is_on: Current state of the USB PHY
4474 *
4475 * Connect/Disconnect the USB PHY pullup
4476 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004477static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004478{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004479 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004480 unsigned long flags = 0;
4481
Gregory Herrero77ba9112015-09-29 12:08:19 +02004482 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004483 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004484
4485 /* Don't modify pullup state while in host mode */
4486 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4487 hsotg->enabled = is_on;
4488 return 0;
4489 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004490
4491 spin_lock_irqsave(&hsotg->lock, flags);
4492 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004493 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004494 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004495 /* Enable ACG feature in device mode,if supported */
4496 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004497 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004498 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004499 dwc2_hsotg_core_disconnect(hsotg);
4500 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004501 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004502 }
4503
4504 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4505 spin_unlock_irqrestore(&hsotg->lock, flags);
4506
4507 return 0;
4508}
4509
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004510static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004511{
4512 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4513 unsigned long flags;
4514
4515 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4516 spin_lock_irqsave(&hsotg->lock, flags);
4517
Gregory Herrero61f72232015-09-29 12:08:28 +02004518 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004519 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004520 * before being initialized / de-initialized
4521 */
4522 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004523 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004524
Gregory Herrero83d98222015-01-09 13:39:02 +01004525 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004526 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004527
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004528 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004529 if (hsotg->enabled) {
4530 /* Enable ACG feature in device mode,if supported */
4531 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004532 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004533 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004534 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004535 dwc2_hsotg_core_disconnect(hsotg);
4536 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004537 }
4538
4539 spin_unlock_irqrestore(&hsotg->lock, flags);
4540 return 0;
4541}
4542
Gregory Herrero596d6962015-01-09 13:39:08 +01004543/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004544 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004545 * @gadget: The usb gadget state
4546 * @mA: Amount of current
4547 *
4548 * Report how much power the device may consume to the phy.
4549 */
John Youn9da51972017-01-17 20:30:27 -08004550static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004551{
4552 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4553
4554 if (IS_ERR_OR_NULL(hsotg->uphy))
4555 return -ENOTSUPP;
4556 return usb_phy_set_power(hsotg->uphy, mA);
4557}
4558
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004559static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4560 .get_frame = dwc2_hsotg_gadget_getframe,
4561 .udc_start = dwc2_hsotg_udc_start,
4562 .udc_stop = dwc2_hsotg_udc_stop,
4563 .pullup = dwc2_hsotg_pullup,
4564 .vbus_session = dwc2_hsotg_vbus_session,
4565 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004566};
4567
4568/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004569 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004570 * @hsotg: The device state.
4571 * @hs_ep: The endpoint to be initialised.
4572 * @epnum: The endpoint number
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004573 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004574 *
4575 * Initialise the given endpoint (as part of the probe and device state
4576 * creation) to give to the gadget driver. Setup the endpoint name, any
4577 * direction information and other state that may be required.
4578 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004579static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004580 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004581 int epnum,
4582 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004583{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004584 char *dir;
4585
4586 if (epnum == 0)
4587 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004588 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004589 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004590 else
4591 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004592
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004593 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004594 hs_ep->index = epnum;
4595
4596 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4597
4598 INIT_LIST_HEAD(&hs_ep->queue);
4599 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4600
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004601 /* add to the list of endpoints known by the gadget driver */
4602 if (epnum)
4603 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4604
4605 hs_ep->parent = hsotg;
4606 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004607
4608 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4609 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4610 else
4611 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4612 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004613 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004614
Robert Baldyga29545222015-07-31 16:00:18 +02004615 if (epnum == 0) {
4616 hs_ep->ep.caps.type_control = true;
4617 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004618 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4619 hs_ep->ep.caps.type_iso = true;
4620 hs_ep->ep.caps.type_bulk = true;
4621 }
Robert Baldyga29545222015-07-31 16:00:18 +02004622 hs_ep->ep.caps.type_int = true;
4623 }
4624
4625 if (dir_in)
4626 hs_ep->ep.caps.dir_in = true;
4627 else
4628 hs_ep->ep.caps.dir_out = true;
4629
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004630 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004631 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004632 * to be something valid.
4633 */
4634
4635 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004636 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004637
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004638 if (dir_in)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004639 dwc2_writel(hsotg, next, DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004640 else
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004641 dwc2_writel(hsotg, next, DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004642 }
4643}
4644
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004645/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004646 * dwc2_hsotg_hw_cfg - read HW configuration registers
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004647 * @hsotg: Programming view of the DWC_otg controller
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004648 *
4649 * Read the USB core HW configuration registers
4650 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004651static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004652{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004653 u32 cfg;
4654 u32 ep_type;
4655 u32 i;
4656
Ben Dooks10aebc72010-07-19 09:40:44 +01004657 /* check hardware configuration */
4658
John Youn43e90342015-12-17 11:17:45 -08004659 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4660
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004661 /* Add ep0 */
4662 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004663
John Younb98866c2017-01-17 20:31:58 -08004664 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4665 sizeof(struct dwc2_hsotg_ep),
4666 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004667 if (!hsotg->eps_in[0])
4668 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004669 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004670 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004671
John Youn43e90342015-12-17 11:17:45 -08004672 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004673 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004674 ep_type = cfg & 3;
4675 /* Direction in or both */
4676 if (!(ep_type & 2)) {
4677 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004678 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004679 if (!hsotg->eps_in[i])
4680 return -ENOMEM;
4681 }
4682 /* Direction out or both */
4683 if (!(ep_type & 1)) {
4684 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004685 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004686 if (!hsotg->eps_out[i])
4687 return -ENOMEM;
4688 }
4689 }
4690
John Youn43e90342015-12-17 11:17:45 -08004691 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4692 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004693
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004694 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4695 hsotg->num_of_eps,
4696 hsotg->dedicated_fifos ? "dedicated" : "shared",
4697 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004698 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004699}
4700
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004701/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004702 * dwc2_hsotg_dump - dump state of the udc
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004703 * @hsotg: Programming view of the DWC_otg controller
4704 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004705 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004706static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004707{
Mark Brown83a01802011-06-01 17:16:15 +01004708#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004709 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004710 u32 val;
4711 int idx;
4712
4713 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004714 dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL),
4715 dwc2_readl(hsotg, DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004716
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004717 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004718 dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004719
4720 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004721 dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004722
4723 /* show periodic fifo settings */
4724
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004725 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004726 val = dwc2_readl(hsotg, DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004727 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004728 val >> FIFOSIZE_DEPTH_SHIFT,
4729 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004730 }
4731
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004732 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004733 dev_info(dev,
4734 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004735 dwc2_readl(hsotg, DIEPCTL(idx)),
4736 dwc2_readl(hsotg, DIEPTSIZ(idx)),
4737 dwc2_readl(hsotg, DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004738
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004739 val = dwc2_readl(hsotg, DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004740 dev_info(dev,
4741 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004742 idx, dwc2_readl(hsotg, DOEPCTL(idx)),
4743 dwc2_readl(hsotg, DOEPTSIZ(idx)),
4744 dwc2_readl(hsotg, DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004745 }
4746
4747 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004748 dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004749#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004750}
4751
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004752/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004753 * dwc2_gadget_init - init function for gadget
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004754 * @hsotg: Programming view of the DWC_otg controller
4755 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004756 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004757int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004758{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004759 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004760 int epnum;
4761 int ret;
John Youn43e90342015-12-17 11:17:45 -08004762
Gregory Herrero0a176272015-01-09 13:38:52 +01004763 /* Dump fifo information */
4764 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004765 hsotg->params.g_np_tx_fifo_size);
4766 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004767
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004768 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004769 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004770 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004771 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004772
4773 if (hsotg->params.lpm)
4774 hsotg->gadget.lpm_capable = true;
4775
Gregory Herrero097ee662015-04-29 22:09:10 +02004776 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4777 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004778 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4779 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004780
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004781 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004782 if (ret) {
4783 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004784 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004785 }
4786
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004787 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4788 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004789 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004790 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004791
4792 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4793 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004794 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004795 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004796
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004797 if (using_desc_dma(hsotg)) {
4798 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4799 if (ret < 0)
4800 return ret;
4801 }
4802
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004803 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4804 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004805 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004806 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004807 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004808 }
4809
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004810 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4811
4812 if (hsotg->num_of_eps == 0) {
4813 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004814 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004815 }
4816
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004817 /* setup endpoint information */
4818
4819 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004820 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004821
4822 /* allocate EP0 request */
4823
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004824 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004825 GFP_KERNEL);
4826 if (!hsotg->ctrl_req) {
4827 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004828 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004829 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004830
4831 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004832 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4833 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004834 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004835 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004836 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004837 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004838 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004839 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004840
Dinh Nguyen117777b2014-11-11 11:13:34 -06004841 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004842 if (ret) {
4843 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep,
4844 hsotg->ctrl_req);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004845 return ret;
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004846 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004847 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004848
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004849 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004850}
4851
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004852/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004853 * dwc2_hsotg_remove - remove function for hsotg driver
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004854 * @hsotg: Programming view of the DWC_otg controller
4855 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004856 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004857int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004858{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004859 usb_del_gadget_udc(&hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004860 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004861
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004862 return 0;
4863}
4864
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004865int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004866{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004867 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004868
Gregory Herrero9e779772015-04-29 22:09:07 +02004869 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004870 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004871
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004872 if (hsotg->driver) {
4873 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004874
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004875 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4876 hsotg->driver->driver.name);
4877
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004878 spin_lock_irqsave(&hsotg->lock, flags);
4879 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004880 dwc2_hsotg_core_disconnect(hsotg);
4881 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004882 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4883 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004884
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004885 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4886 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004887 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004888 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004889 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004890 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004891 }
4892
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004893 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004894}
4895
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004896int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004897{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004898 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004899
Gregory Herrero9e779772015-04-29 22:09:07 +02004900 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004901 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004902
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004903 if (hsotg->driver) {
4904 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4905 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004906
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004907 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004908 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004909 if (hsotg->enabled) {
4910 /* Enable ACG feature in device mode,if supported */
4911 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004912 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004913 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004914 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004915 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004916
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004917 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004918}
John Youn58e52ff6a2016-02-23 19:54:57 -08004919
4920/**
4921 * dwc2_backup_device_registers() - Backup controller device registers.
4922 * When suspending usb bus, registers needs to be backuped
4923 * if controller power is disabled once suspended.
4924 *
4925 * @hsotg: Programming view of the DWC_otg controller
4926 */
4927int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4928{
4929 struct dwc2_dregs_backup *dr;
4930 int i;
4931
4932 dev_dbg(hsotg->dev, "%s\n", __func__);
4933
4934 /* Backup dev regs */
4935 dr = &hsotg->dr_backup;
4936
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004937 dr->dcfg = dwc2_readl(hsotg, DCFG);
4938 dr->dctl = dwc2_readl(hsotg, DCTL);
4939 dr->daintmsk = dwc2_readl(hsotg, DAINTMSK);
4940 dr->diepmsk = dwc2_readl(hsotg, DIEPMSK);
4941 dr->doepmsk = dwc2_readl(hsotg, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08004942
4943 for (i = 0; i < hsotg->num_of_eps; i++) {
4944 /* Backup IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004945 dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004946
4947 /* Ensure DATA PID is correctly configured */
4948 if (dr->diepctl[i] & DXEPCTL_DPID)
4949 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4950 else
4951 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4952
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004953 dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i));
4954 dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004955
4956 /* Backup OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004957 dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004958
4959 /* Ensure DATA PID is correctly configured */
4960 if (dr->doepctl[i] & DXEPCTL_DPID)
4961 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4962 else
4963 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4964
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004965 dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i));
4966 dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i));
4967 dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004968 }
4969 dr->valid = true;
4970 return 0;
4971}
4972
4973/**
4974 * dwc2_restore_device_registers() - Restore controller device registers.
4975 * When resuming usb bus, device registers needs to be restored
4976 * if controller power were disabled.
4977 *
4978 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004979 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
4980 *
4981 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08004982 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004983int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08004984{
4985 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08004986 int i;
4987
4988 dev_dbg(hsotg->dev, "%s\n", __func__);
4989
4990 /* Restore dev regs */
4991 dr = &hsotg->dr_backup;
4992 if (!dr->valid) {
4993 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4994 __func__);
4995 return -EINVAL;
4996 }
4997 dr->valid = false;
4998
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004999 if (!remote_wakeup)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005000 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005001
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005002 dwc2_writel(hsotg, dr->daintmsk, DAINTMSK);
5003 dwc2_writel(hsotg, dr->diepmsk, DIEPMSK);
5004 dwc2_writel(hsotg, dr->doepmsk, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08005005
5006 for (i = 0; i < hsotg->num_of_eps; i++) {
5007 /* Restore IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005008 dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i));
5009 dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i));
5010 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005011 /** WA for enabled EPx's IN in DDMA mode. On entering to
5012 * hibernation wrong value read and saved from DIEPDMAx,
5013 * as result BNA interrupt asserted on hibernation exit
5014 * by restoring from saved area.
5015 */
5016 if (hsotg->params.g_dma_desc &&
5017 (dr->diepctl[i] & DXEPCTL_EPENA))
5018 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005019 dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i));
5020 dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005021 /* Restore OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005022 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005023 /* WA for enabled EPx's OUT in DDMA mode. On entering to
5024 * hibernation wrong value read and saved from DOEPDMAx,
5025 * as result BNA interrupt asserted on hibernation exit
5026 * by restoring from saved area.
5027 */
5028 if (hsotg->params.g_dma_desc &&
5029 (dr->doepctl[i] & DXEPCTL_EPENA))
5030 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005031 dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i));
5032 dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08005033 }
5034
John Youn58e52ff6a2016-02-23 19:54:57 -08005035 return 0;
5036}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04005037
5038/**
5039 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
5040 *
5041 * @hsotg: Programming view of DWC_otg controller
5042 *
5043 */
5044void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
5045{
5046 u32 val;
5047
5048 if (!hsotg->params.lpm)
5049 return;
5050
5051 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
5052 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
5053 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
5054 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
5055 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
Minas Harutyunyan46637562019-04-18 15:40:43 +04005056 val |= GLPMCFG_LPM_REJECT_CTRL_CONTROL;
Artur Petrosyan9aed8c02018-11-02 11:29:55 -04005057 val |= GLPMCFG_LPM_ACCEPT_CTRL_ISOC;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005058 dwc2_writel(hsotg, val, GLPMCFG);
5059 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG));
Grigor Tovmasyan4abe4532018-08-29 21:02:28 +04005060
5061 /* Unmask WKUP_ALERT Interrupt */
5062 if (hsotg->params.service_interval)
5063 dwc2_set_bit(hsotg, GINTMSK2, GINTMSK2_WKUP_ALERT_INT_MSK);
Sevak Arakelyan21b03402018-01-24 17:43:32 +04005064}
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005065
5066/**
Grigor Tovmasyan15d9dbf2018-08-29 21:01:59 +04005067 * dwc2_gadget_program_ref_clk - Program GREFCLK register in device mode
5068 *
5069 * @hsotg: Programming view of DWC_otg controller
5070 *
5071 */
5072void dwc2_gadget_program_ref_clk(struct dwc2_hsotg *hsotg)
5073{
5074 u32 val = 0;
5075
5076 val |= GREFCLK_REF_CLK_MODE;
5077 val |= hsotg->params.ref_clk_per << GREFCLK_REFCLKPER_SHIFT;
5078 val |= hsotg->params.sof_cnt_wkup_alert <<
5079 GREFCLK_SOF_CNT_WKUP_ALERT_SHIFT;
5080
5081 dwc2_writel(hsotg, val, GREFCLK);
5082 dev_dbg(hsotg->dev, "GREFCLK=0x%08x\n", dwc2_readl(hsotg, GREFCLK));
5083}
5084
5085/**
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005086 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
5087 *
5088 * @hsotg: Programming view of the DWC_otg controller
5089 *
5090 * Return non-zero if failed to enter to hibernation.
5091 */
5092int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
5093{
5094 u32 gpwrdn;
5095 int ret = 0;
5096
5097 /* Change to L2(suspend) state */
5098 hsotg->lx_state = DWC2_L2;
5099 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
5100 ret = dwc2_backup_global_registers(hsotg);
5101 if (ret) {
5102 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5103 __func__);
5104 return ret;
5105 }
5106 ret = dwc2_backup_device_registers(hsotg);
5107 if (ret) {
5108 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
5109 __func__);
5110 return ret;
5111 }
5112
5113 gpwrdn = GPWRDN_PWRDNRSTN;
5114 gpwrdn |= GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005115 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005116 udelay(10);
5117
5118 /* Set flag to indicate that we are in hibernation */
5119 hsotg->hibernated = 1;
5120
5121 /* Enable interrupts from wake up logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005122 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005123 gpwrdn |= GPWRDN_PMUINTSEL;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005124 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005125 udelay(10);
5126
5127 /* Unmask device mode interrupts in GPWRDN */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005128 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005129 gpwrdn |= GPWRDN_RST_DET_MSK;
5130 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5131 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005132 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005133 udelay(10);
5134
5135 /* Enable Power Down Clamp */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005136 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005137 gpwrdn |= GPWRDN_PWRDNCLMP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005138 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005139 udelay(10);
5140
5141 /* Switch off VDD */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005142 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005143 gpwrdn |= GPWRDN_PWRDNSWTCH;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005144 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005145 udelay(10);
5146
5147 /* Save gpwrdn register for further usage if stschng interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005148 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005149 dev_dbg(hsotg->dev, "Hibernation completed\n");
5150
5151 return ret;
5152}
5153
5154/**
5155 * dwc2_gadget_exit_hibernation()
5156 * This function is for exiting from Device mode hibernation by host initiated
5157 * resume/reset and device initiated remote-wakeup.
5158 *
5159 * @hsotg: Programming view of the DWC_otg controller
5160 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04005161 * @reset: indicates whether resume is initiated by Reset.
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005162 *
5163 * Return non-zero if failed to exit from hibernation.
5164 */
5165int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
5166 int rem_wakeup, int reset)
5167{
5168 u32 pcgcctl;
5169 u32 gpwrdn;
5170 u32 dctl;
5171 int ret = 0;
5172 struct dwc2_gregs_backup *gr;
5173 struct dwc2_dregs_backup *dr;
5174
5175 gr = &hsotg->gr_backup;
5176 dr = &hsotg->dr_backup;
5177
5178 if (!hsotg->hibernated) {
5179 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5180 return 1;
5181 }
5182 dev_dbg(hsotg->dev,
5183 "%s: called with rem_wakeup = %d reset = %d\n",
5184 __func__, rem_wakeup, reset);
5185
5186 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5187
5188 if (!reset) {
5189 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005190 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005191 }
5192
5193 /* De-assert Restore */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005194 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005195 gpwrdn &= ~GPWRDN_RESTORE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005196 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005197 udelay(10);
5198
5199 if (!rem_wakeup) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005200 pcgcctl = dwc2_readl(hsotg, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005201 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005202 dwc2_writel(hsotg, pcgcctl, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005203 }
5204
5205 /* Restore GUSBCFG, DCFG and DCTL */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005206 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5207 dwc2_writel(hsotg, dr->dcfg, DCFG);
5208 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005209
5210 /* De-assert Wakeup Logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005211 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005212 gpwrdn &= ~GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005213 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005214
5215 if (rem_wakeup) {
5216 udelay(10);
5217 /* Start Remote Wakeup Signaling */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005218 dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005219 } else {
5220 udelay(50);
5221 /* Set Device programming done bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005222 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005223 dctl |= DCTL_PWRONPRGDONE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005224 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005225 }
5226 /* Wait for interrupts which must be cleared */
5227 mdelay(2);
5228 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005229 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005230
5231 /* Restore global registers */
5232 ret = dwc2_restore_global_registers(hsotg);
5233 if (ret) {
5234 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5235 __func__);
5236 return ret;
5237 }
5238
5239 /* Restore device registers */
5240 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5241 if (ret) {
5242 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5243 __func__);
5244 return ret;
5245 }
5246
5247 if (rem_wakeup) {
5248 mdelay(10);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005249 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005250 dctl &= ~DCTL_RMTWKUPSIG;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005251 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005252 }
5253
5254 hsotg->hibernated = 0;
5255 hsotg->lx_state = DWC2_L0;
5256 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5257
5258 return ret;
5259}