blob: 5f314a10a1161c671b434016c8023b430e0d38d0 [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>
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +040030#include <linux/usb/composite.h>
31
Ben Dooks5b7d70c2009-06-02 14:58:06 +010032
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070033#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060034#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010035
36/* conversion functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050037static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010038{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050039 return container_of(req, struct dwc2_hsotg_req, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010040}
41
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050042static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010043{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050044 return container_of(ep, struct dwc2_hsotg_ep, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010045}
46
Dinh Nguyen941fcce2014-11-11 11:13:33 -060047static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010048{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060049 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010050}
51
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040052static inline void dwc2_set_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010053{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040054 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) | val, offset);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010055}
56
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040057static inline void dwc2_clear_bit(struct dwc2_hsotg *hsotg, u32 offset, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010058{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +040059 dwc2_writel(hsotg, dwc2_readl(hsotg, offset) & ~val, offset);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010060}
61
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050062static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010063 u32 ep_index, u32 dir_in)
64{
65 if (dir_in)
66 return hsotg->eps_in[ep_index];
67 else
68 return hsotg->eps_out[ep_index];
69}
70
Mickael Maison997f4f82014-12-23 17:39:45 +010071/* forward declaration of functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050072static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010073
74/**
75 * using_dma - return the DMA status of the driver.
76 * @hsotg: The driver state.
77 *
78 * Return true if we're using DMA.
79 *
80 * Currently, we have the DMA support code worked into everywhere
81 * that needs it, but the AMBA DMA implementation in the hardware can
82 * only DMA from 32bit aligned addresses. This means that gadgets such
83 * as the CDC Ethernet cannot work as they often pass packets which are
84 * not 32bit aligned.
85 *
86 * Unfortunately the choice to use DMA or not is global to the controller
87 * and seems to be only settable when the controller is being put through
88 * a core reset. This means we either need to fix the gadgets to take
89 * account of DMA alignment, or add bounce buffers (yuerk).
90 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010091 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010092 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060093static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010094{
John Youn05ee7992016-11-03 17:56:05 -070095 return hsotg->params.g_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +010096}
97
Vahram Aharonyandec4b552016-11-09 19:27:48 -080098/*
99 * using_desc_dma - return the descriptor DMA status of the driver.
100 * @hsotg: The driver state.
101 *
102 * Return true if we're using descriptor DMA.
103 */
104static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
105{
106 return hsotg->params.g_dma_desc;
107}
108
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100109/**
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700110 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
111 * @hs_ep: The endpoint
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700112 *
113 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
114 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
115 */
116static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
117{
118 hs_ep->target_frame += hs_ep->interval;
119 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600120 hs_ep->frame_overrun = true;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700121 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
122 } else {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600123 hs_ep->frame_overrun = false;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700124 }
125}
126
127/**
Grigor Tovmasyan9d630b92018-08-29 21:00:03 +0400128 * dwc2_gadget_dec_frame_num_by_one - Decrements the targeted frame number
129 * by one.
130 * @hs_ep: The endpoint.
131 *
132 * This function used in service interval based scheduling flow to calculate
133 * descriptor frame number filed value. For service interval mode frame
134 * number in descriptor should point to last (u)frame in the interval.
135 *
136 */
137static inline void dwc2_gadget_dec_frame_num_by_one(struct dwc2_hsotg_ep *hs_ep)
138{
139 if (hs_ep->target_frame)
140 hs_ep->target_frame -= 1;
141 else
142 hs_ep->target_frame = DSTS_SOFFN_LIMIT;
143}
144
145/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500146 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100147 * @hsotg: The device state
148 * @ints: A bitmask of the interrupts to enable
149 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500150static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100151{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400152 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100153 u32 new_gsintmsk;
154
155 new_gsintmsk = gsintmsk | ints;
156
157 if (new_gsintmsk != gsintmsk) {
158 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400159 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100160 }
161}
162
163/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500164 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100165 * @hsotg: The device state
166 * @ints: A bitmask of the interrupts to enable
167 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500168static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100169{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400170 u32 gsintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100171 u32 new_gsintmsk;
172
173 new_gsintmsk = gsintmsk & ~ints;
174
175 if (new_gsintmsk != gsintmsk)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400176 dwc2_writel(hsotg, new_gsintmsk, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100177}
178
179/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500180 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100181 * @hsotg: The device state
182 * @ep: The endpoint index
183 * @dir_in: True if direction is in.
184 * @en: The enable value, true to enable
185 *
186 * Set or clear the mask for an individual endpoint's interrupt
187 * request.
188 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500189static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800190 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100191 unsigned int en)
192{
193 unsigned long flags;
194 u32 bit = 1 << ep;
195 u32 daint;
196
197 if (!dir_in)
198 bit <<= 16;
199
200 local_irq_save(flags);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400201 daint = dwc2_readl(hsotg, DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100202 if (en)
203 daint |= bit;
204 else
205 daint &= ~bit;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400206 dwc2_writel(hsotg, daint, DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100207 local_irq_restore(flags);
208}
209
210/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800211 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400212 *
213 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800214 */
215int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
216{
217 if (hsotg->hw_params.en_multiple_tx_fifo)
218 /* In dedicated FIFO mode we need count of IN EPs */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400219 return hsotg->hw_params.num_dev_in_eps;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800220 else
221 /* In shared FIFO mode we need count of Periodic IN EPs */
222 return hsotg->hw_params.num_dev_perio_in_ep;
223}
224
225/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800226 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
227 * device mode TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400228 *
229 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800230 */
231int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
232{
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800233 int addr;
234 int tx_addr_max;
235 u32 np_tx_fifo_size;
236
237 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
238 hsotg->params.g_np_tx_fifo_size);
239
240 /* Get Endpoint Info Control block size in DWORDs. */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400241 tx_addr_max = hsotg->hw_params.total_fifo_size;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800242
243 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
244 if (tx_addr_max <= addr)
245 return 0;
246
247 return tx_addr_max - addr;
248}
249
250/**
Grigor Tovmasyan187c5292018-08-29 21:02:57 +0400251 * dwc2_gadget_wkup_alert_handler - Handler for WKUP_ALERT interrupt
252 *
253 * @hsotg: Programming view of the DWC_otg controller
254 *
255 */
256static void dwc2_gadget_wkup_alert_handler(struct dwc2_hsotg *hsotg)
257{
258 u32 gintsts2;
259 u32 gintmsk2;
260
261 gintsts2 = dwc2_readl(hsotg, GINTSTS2);
262 gintmsk2 = dwc2_readl(hsotg, GINTMSK2);
263
264 if (gintsts2 & GINTSTS2_WKUP_ALERT_INT) {
265 dev_dbg(hsotg->dev, "%s: Wkup_Alert_Int\n", __func__);
Minas Harutyunyan87b6d2c2018-12-12 16:44:32 +0400266 dwc2_set_bit(hsotg, GINTSTS2, GINTSTS2_WKUP_ALERT_INT);
Artur Petrosyand64bc8e2018-11-02 11:29:48 -0400267 dwc2_set_bit(hsotg, DCTL, DCTL_RMTWKUPSIG);
Grigor Tovmasyan187c5292018-08-29 21:02:57 +0400268 }
269}
270
271/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800272 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
273 * TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400274 *
275 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800276 */
277int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
278{
279 int tx_fifo_count;
280 int tx_fifo_depth;
281
282 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
283
284 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
285
286 if (!tx_fifo_count)
287 return tx_fifo_depth;
288 else
289 return tx_fifo_depth / tx_fifo_count;
290}
291
292/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500293 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100294 * @hsotg: The device instance.
295 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500296static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100297{
John Youn2317eac2016-10-17 17:36:23 -0700298 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100299 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100300 int timeout;
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +0400301
Ben Dooks0f002d22010-05-25 05:36:50 +0100302 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700303 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100304
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100305 /* Reset fifo map if not correctly cleared during previous session */
306 WARN_ON(hsotg->fifo_map);
307 hsotg->fifo_map = 0;
308
Gregory Herrero0a176272015-01-09 13:38:52 +0100309 /* set RX/NPTX FIFO sizes */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400310 dwc2_writel(hsotg, hsotg->params.g_rx_fifo_size, GRXFSIZ);
311 dwc2_writel(hsotg, (hsotg->params.g_rx_fifo_size <<
312 FIFOSIZE_STARTADDR_SHIFT) |
John Youn05ee7992016-11-03 17:56:05 -0700313 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400314 GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100315
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200316 /*
317 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100318 * block have overlapping default addresses. This also ensures
319 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200320 * known values.
321 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100322
323 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700324 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100325
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200326 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100327 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200328 * them to endpoints dynamically according to maxpacket size value of
329 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200330 */
John Youn2317eac2016-10-17 17:36:23 -0700331 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700332 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700333 continue;
334 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700335 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
336 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700337 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700338 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100339
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400340 dwc2_writel(hsotg, val, DPTXFSIZN(ep));
341 val = dwc2_readl(hsotg, DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100342 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100343
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400344 dwc2_writel(hsotg, hsotg->hw_params.total_fifo_size |
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800345 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400346 GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200347 /*
348 * according to p428 of the design guide, we need to ensure that
349 * all fifos are flushed before continuing
350 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100351
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400352 dwc2_writel(hsotg, GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
353 GRSTCTL_RXFFLSH, GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100354
355 /* wait until the fifos are both flushed */
356 timeout = 100;
357 while (1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400358 val = dwc2_readl(hsotg, GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100359
Dinh Nguyen47a16852014-04-14 14:13:34 -0700360 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100361 break;
362
363 if (--timeout == 0) {
364 dev_err(hsotg->dev,
365 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
366 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100367 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100368 }
369
370 udelay(1);
371 }
372
373 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100374}
375
376/**
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400377 * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100378 * @ep: USB endpoint to allocate request for.
379 * @flags: Allocation flags
380 *
381 * Allocate a new USB request structure appropriate for the specified endpoint
382 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500383static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800384 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100385{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500386 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100387
John Younec33efe2017-01-17 20:32:41 -0800388 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100389 if (!req)
390 return NULL;
391
392 INIT_LIST_HEAD(&req->queue);
393
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100394 return &req->req;
395}
396
397/**
398 * is_ep_periodic - return true if the endpoint is in periodic mode.
399 * @hs_ep: The endpoint to query.
400 *
401 * Returns true if the endpoint is in periodic mode, meaning it is being
402 * used for an Interrupt or ISO transfer.
403 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500404static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100405{
406 return hs_ep->periodic;
407}
408
409/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500410 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100411 * @hsotg: The device state.
412 * @hs_ep: The endpoint for the request
413 * @hs_req: The request being processed.
414 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500415 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100416 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200417 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500418static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800419 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500420 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100421{
422 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800423
Jingoo Han17d966a2013-05-11 21:14:00 +0900424 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100425}
426
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800427/*
428 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
429 * for Control endpoint
430 * @hsotg: The device state.
431 *
432 * This function will allocate 4 descriptor chains for EP 0: 2 for
433 * Setup stage, per one for IN and OUT data/status transactions.
434 */
435static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
436{
437 hsotg->setup_desc[0] =
438 dmam_alloc_coherent(hsotg->dev,
439 sizeof(struct dwc2_dma_desc),
440 &hsotg->setup_desc_dma[0],
441 GFP_KERNEL);
442 if (!hsotg->setup_desc[0])
443 goto fail;
444
445 hsotg->setup_desc[1] =
446 dmam_alloc_coherent(hsotg->dev,
447 sizeof(struct dwc2_dma_desc),
448 &hsotg->setup_desc_dma[1],
449 GFP_KERNEL);
450 if (!hsotg->setup_desc[1])
451 goto fail;
452
453 hsotg->ctrl_in_desc =
454 dmam_alloc_coherent(hsotg->dev,
455 sizeof(struct dwc2_dma_desc),
456 &hsotg->ctrl_in_desc_dma,
457 GFP_KERNEL);
458 if (!hsotg->ctrl_in_desc)
459 goto fail;
460
461 hsotg->ctrl_out_desc =
462 dmam_alloc_coherent(hsotg->dev,
463 sizeof(struct dwc2_dma_desc),
464 &hsotg->ctrl_out_desc_dma,
465 GFP_KERNEL);
466 if (!hsotg->ctrl_out_desc)
467 goto fail;
468
469 return 0;
470
471fail:
472 return -ENOMEM;
473}
474
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100475/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500476 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100477 * @hsotg: The controller state.
478 * @hs_ep: The endpoint we're going to write for.
479 * @hs_req: The request to write data for.
480 *
481 * This is called when the TxFIFO has some space in it to hold a new
482 * transmission and we have something to give it. The actual setup of
483 * the data size is done elsewhere, so all we have to do is to actually
484 * write the data.
485 *
486 * The return value is zero if there is more space (or nothing was done)
487 * otherwise -ENOSPC is returned if the FIFO space was used up.
488 *
489 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200490 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500491static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800492 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500493 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100494{
495 bool periodic = is_ep_periodic(hs_ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400496 u32 gnptxsts = dwc2_readl(hsotg, GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100497 int buf_pos = hs_req->req.actual;
498 int to_write = hs_ep->size_loaded;
499 void *data;
500 int can_write;
501 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200502 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100503
504 to_write -= (buf_pos - hs_ep->last_load);
505
506 /* if there's nothing to write, get out early */
507 if (to_write == 0)
508 return 0;
509
Ben Dooks10aebc72010-07-19 09:40:44 +0100510 if (periodic && !hsotg->dedicated_fifos) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400511 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100512 int size_left;
513 int size_done;
514
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200515 /*
516 * work out how much data was loaded so we can calculate
517 * how much data is left in the fifo.
518 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100519
Dinh Nguyen47a16852014-04-14 14:13:34 -0700520 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100521
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200522 /*
523 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100524 * previous data has been completely sent.
525 */
526 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500527 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100528 return -ENOSPC;
529 }
530
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100531 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
532 __func__, size_left,
533 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
534
535 /* how much of the data has moved */
536 size_done = hs_ep->size_loaded - size_left;
537
538 /* how much data is left in the fifo */
539 can_write = hs_ep->fifo_load - size_done;
540 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
541 __func__, can_write);
542
543 can_write = hs_ep->fifo_size - can_write;
544 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
545 __func__, can_write);
546
547 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500548 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100549 return -ENOSPC;
550 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100551 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400552 can_write = dwc2_readl(hsotg,
553 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100554
555 can_write &= 0xffff;
556 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100557 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700558 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100559 dev_dbg(hsotg->dev,
560 "%s: no queue slots available (0x%08x)\n",
561 __func__, gnptxsts);
562
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500563 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100564 return -ENOSPC;
565 }
566
Dinh Nguyen47a16852014-04-14 14:13:34 -0700567 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100568 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100569 }
570
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200571 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
572
573 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800574 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100575
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200576 /*
577 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100578 * FIFO, requests of >512 cause the endpoint to get stuck with a
579 * fragment of the end of the transfer in it.
580 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200581 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100582 can_write = 512;
583
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200584 /*
585 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100586 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200587 * doing it.
588 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200589 if (to_write > max_transfer) {
590 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100591
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200592 /* it's needed only when we do not use dedicated fifos */
593 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500594 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800595 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700596 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100597 }
598
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100599 /* see if we can write data */
600
601 if (to_write > can_write) {
602 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200603 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100604
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200605 /*
606 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100607 * exact number of packets.
608 *
609 * Note, we do not currently check to see if we can ever
610 * write a full packet or not to the FIFO.
611 */
612
613 if (pkt_round)
614 to_write -= pkt_round;
615
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200616 /*
617 * enable correct FIFO interrupt to alert us when there
618 * is more room left.
619 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100620
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200621 /* it's needed only when we do not use dedicated fifos */
622 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500623 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800624 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700625 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100626 }
627
628 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800629 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100630
631 if (to_write <= 0)
632 return -ENOSPC;
633
634 hs_req->req.actual = buf_pos + to_write;
635 hs_ep->total_data += to_write;
636
637 if (periodic)
638 hs_ep->fifo_load += to_write;
639
640 to_write = DIV_ROUND_UP(to_write, 4);
641 data = hs_req->req.buf + buf_pos;
642
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +0400643 dwc2_writel_rep(hsotg, EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100644
645 return (to_write >= can_write) ? -ENOSPC : 0;
646}
647
648/**
649 * get_ep_limit - get the maximum data legnth for this endpoint
650 * @hs_ep: The endpoint
651 *
652 * Return the maximum data that can be queued in one go on a given endpoint
653 * so that transfers that are too long can be split.
654 */
John Youn9da51972017-01-17 20:30:27 -0800655static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100656{
657 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800658 unsigned int maxsize;
659 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100660
661 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700662 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
663 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100664 } else {
John Youn9da51972017-01-17 20:30:27 -0800665 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900666 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700667 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900668 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100669 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100670 }
671
672 /* we made the constant loading easier above by using +1 */
673 maxpkt--;
674 maxsize--;
675
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200676 /*
677 * constrain by packet count if maxpkts*pktsize is greater
678 * than the length register size.
679 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100680
681 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
682 maxsize = maxpkt * hs_ep->ep.maxpacket;
683
684 return maxsize;
685}
686
687/**
John Youn38beaec2017-01-17 20:31:13 -0800688 * dwc2_hsotg_read_frameno - read current frame number
689 * @hsotg: The device instance
690 *
691 * Return the current frame number
692 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700693static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
694{
695 u32 dsts;
696
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400697 dsts = dwc2_readl(hsotg, DSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700698 dsts &= DSTS_SOFFN_MASK;
699 dsts >>= DSTS_SOFFN_SHIFT;
700
701 return dsts;
702}
703
704/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800705 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
706 * DMA descriptor chain prepared for specific endpoint
707 * @hs_ep: The endpoint
708 *
709 * Return the maximum data that can be queued in one go on a given endpoint
710 * depending on its descriptor chain capacity so that transfers that
711 * are too long can be split.
712 */
713static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
714{
715 int is_isoc = hs_ep->isochronous;
716 unsigned int maxsize;
717
718 if (is_isoc)
719 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
720 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
721 else
722 maxsize = DEV_DMA_NBYTES_LIMIT;
723
724 /* Above size of one descriptor was chosen, multiple it */
725 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
726
727 return maxsize;
728}
729
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800730/*
731 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
732 * @hs_ep: The endpoint
733 * @mask: RX/TX bytes mask to be defined
734 *
735 * Returns maximum data payload for one descriptor after analyzing endpoint
736 * characteristics.
737 * DMA descriptor transfer bytes limit depends on EP type:
738 * Control out - MPS,
739 * Isochronous - descriptor rx/tx bytes bitfield limit,
740 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
741 * have concatenations from various descriptors within one packet.
742 *
743 * Selects corresponding mask for RX/TX bytes as well.
744 */
745static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
746{
747 u32 mps = hs_ep->ep.maxpacket;
748 int dir_in = hs_ep->dir_in;
749 u32 desc_size = 0;
750
751 if (!hs_ep->index && !dir_in) {
752 desc_size = mps;
753 *mask = DEV_DMA_NBYTES_MASK;
754 } else if (hs_ep->isochronous) {
755 if (dir_in) {
756 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
757 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
758 } else {
759 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
760 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
761 }
762 } else {
763 desc_size = DEV_DMA_NBYTES_LIMIT;
764 *mask = DEV_DMA_NBYTES_MASK;
765
766 /* Round down desc_size to be mps multiple */
767 desc_size -= desc_size % mps;
768 }
769
770 return desc_size;
771}
772
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100773static void dwc2_gadget_fill_nonisoc_xfer_ddma_one(struct dwc2_hsotg_ep *hs_ep,
774 struct dwc2_dma_desc **desc,
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800775 dma_addr_t dma_buff,
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100776 unsigned int len,
777 bool true_last)
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800778{
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800779 int dir_in = hs_ep->dir_in;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800780 u32 mps = hs_ep->ep.maxpacket;
781 u32 maxsize = 0;
782 u32 offset = 0;
783 u32 mask = 0;
784 int i;
785
786 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
787
788 hs_ep->desc_count = (len / maxsize) +
789 ((len % maxsize) ? 1 : 0);
790 if (len == 0)
791 hs_ep->desc_count = 1;
792
793 for (i = 0; i < hs_ep->desc_count; ++i) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100794 (*desc)->status = 0;
795 (*desc)->status |= (DEV_DMA_BUFF_STS_HBUSY
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800796 << DEV_DMA_BUFF_STS_SHIFT);
797
798 if (len > maxsize) {
799 if (!hs_ep->index && !dir_in)
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100800 (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800801
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100802 (*desc)->status |=
803 maxsize << DEV_DMA_NBYTES_SHIFT & mask;
804 (*desc)->buf = dma_buff + offset;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800805
806 len -= maxsize;
807 offset += maxsize;
808 } else {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100809 if (true_last)
810 (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800811
812 if (dir_in)
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100813 (*desc)->status |= (len % mps) ? DEV_DMA_SHORT :
814 ((hs_ep->send_zlp && true_last) ?
815 DEV_DMA_SHORT : 0);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800816
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100817 (*desc)->status |=
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800818 len << DEV_DMA_NBYTES_SHIFT & mask;
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100819 (*desc)->buf = dma_buff + offset;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800820 }
821
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100822 (*desc)->status &= ~DEV_DMA_BUFF_STS_MASK;
823 (*desc)->status |= (DEV_DMA_BUFF_STS_HREADY
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800824 << DEV_DMA_BUFF_STS_SHIFT);
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100825 (*desc)++;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800826 }
827}
828
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800829/*
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100830 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
831 * @hs_ep: The endpoint
832 * @ureq: Request to transfer
833 * @offset: offset in bytes
834 * @len: Length of the transfer
835 *
836 * This function will iterate over descriptor chain and fill its entries
837 * with corresponding information based on transfer data.
838 */
839static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
840 struct usb_request *ureq,
841 unsigned int offset,
842 unsigned int len)
843{
844 struct dwc2_dma_desc *desc = hs_ep->desc_list;
845 struct scatterlist *sg;
846 int i;
847 u8 desc_count = 0;
848
849 /* non-DMA sg buffer */
850 if (!ureq->num_sgs) {
851 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
852 ureq->dma + offset, len, true);
853 return;
854 }
855
856 /* DMA sg buffer */
857 for_each_sg(ureq->sg, sg, ureq->num_sgs, i) {
858 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
859 sg_dma_address(sg) + sg->offset, sg_dma_len(sg),
860 sg_is_last(sg));
861 desc_count += hs_ep->desc_count;
862 }
863
864 hs_ep->desc_count = desc_count;
865}
866
867/*
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800868 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
869 * @hs_ep: The isochronous endpoint.
870 * @dma_buff: usb requests dma buffer.
871 * @len: usb request transfer length.
872 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400873 * Fills next free descriptor with the data of the arrived usb request,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800874 * frame info, sets Last and IOC bits increments next_desc. If filled
875 * descriptor is not the first one, removes L bit from the previous descriptor
876 * status.
877 */
878static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
879 dma_addr_t dma_buff, unsigned int len)
880{
881 struct dwc2_dma_desc *desc;
882 struct dwc2_hsotg *hsotg = hs_ep->parent;
883 u32 index;
884 u32 maxsize = 0;
885 u32 mask = 0;
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400886 u8 pid = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800887
888 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800889
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400890 index = hs_ep->next_desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800891 desc = &hs_ep->desc_list[index];
892
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400893 /* Check if descriptor chain full */
894 if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
895 DEV_DMA_BUFF_STS_HREADY) {
896 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
897 return 1;
898 }
899
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800900 /* Clear L bit of previous desc if more than one entries in the chain */
901 if (hs_ep->next_desc)
902 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
903
904 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
905 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
906
907 desc->status = 0;
908 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
909
910 desc->buf = dma_buff;
911 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
912 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
913
914 if (hs_ep->dir_in) {
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400915 if (len)
916 pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
917 else
918 pid = 1;
919 desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800920 DEV_DMA_ISOC_PID_MASK) |
921 ((len % hs_ep->ep.maxpacket) ?
922 DEV_DMA_SHORT : 0) |
923 ((hs_ep->target_frame <<
924 DEV_DMA_ISOC_FRNUM_SHIFT) &
925 DEV_DMA_ISOC_FRNUM_MASK);
926 }
927
928 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
929 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
930
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400931 /* Increment frame number by interval for IN */
932 if (hs_ep->dir_in)
933 dwc2_gadget_incr_frame_num(hs_ep);
934
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800935 /* Update index of last configured entry in the chain */
936 hs_ep->next_desc++;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400937 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_GENERIC)
938 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800939
940 return 0;
941}
942
943/*
944 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
945 * @hs_ep: The isochronous endpoint.
946 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400947 * Prepare descriptor chain for isochronous endpoints. Afterwards
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800948 * write DMA address to HW and enable the endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800949 */
950static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
951{
952 struct dwc2_hsotg *hsotg = hs_ep->parent;
953 struct dwc2_hsotg_req *hs_req, *treq;
954 int index = hs_ep->index;
955 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400956 int i;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800957 u32 dma_reg;
958 u32 depctl;
959 u32 ctrl;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400960 struct dwc2_dma_desc *desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800961
962 if (list_empty(&hs_ep->queue)) {
Minas Harutyunyan1ffba902018-06-12 12:37:29 +0400963 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800964 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
965 return;
966 }
967
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400968 /* Initialize descriptor chain by Host Busy status */
969 for (i = 0; i < MAX_DMA_DESC_NUM_GENERIC; i++) {
970 desc = &hs_ep->desc_list[i];
971 desc->status = 0;
972 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
973 << DEV_DMA_BUFF_STS_SHIFT);
974 }
975
976 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800977 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100978 dma_addr_t dma_addr = hs_req->req.dma;
979
980 if (hs_req->req.num_sgs) {
981 WARN_ON(hs_req->req.num_sgs > 1);
982 dma_addr = sg_dma_address(hs_req->req.sg);
983 }
984 ret = dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800985 hs_req->req.length);
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400986 if (ret)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800987 break;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800988 }
989
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400990 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800991 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
992 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
993
994 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400995 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800996
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400997 ctrl = dwc2_readl(hsotg, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800998 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400999 dwc2_writel(hsotg, ctrl, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001000}
1001
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -08001002/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001003 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001004 * @hsotg: The controller state.
1005 * @hs_ep: The endpoint to process a request for
1006 * @hs_req: The request to start.
1007 * @continuing: True if we are doing more for the current request.
1008 *
1009 * Start the given request running by setting the endpoint registers
1010 * appropriately, and writing any data to the FIFOs.
1011 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001012static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001013 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001014 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001015 bool continuing)
1016{
1017 struct usb_request *ureq = &hs_req->req;
1018 int index = hs_ep->index;
1019 int dir_in = hs_ep->dir_in;
1020 u32 epctrl_reg;
1021 u32 epsize_reg;
1022 u32 epsize;
1023 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -08001024 unsigned int length;
1025 unsigned int packets;
1026 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001027 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001028
1029 if (index != 0) {
1030 if (hs_ep->req && !continuing) {
1031 dev_err(hsotg->dev, "%s: active request\n", __func__);
1032 WARN_ON(1);
1033 return;
1034 } else if (hs_ep->req != hs_req && continuing) {
1035 dev_err(hsotg->dev,
1036 "%s: continue different req\n", __func__);
1037 WARN_ON(1);
1038 return;
1039 }
1040 }
1041
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001042 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001043 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
1044 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001045
1046 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001047 __func__, dwc2_readl(hsotg, epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001048 hs_ep->dir_in ? "in" : "out");
1049
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001050 /* If endpoint is stalled, we will restart request later */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001051 ctrl = dwc2_readl(hsotg, epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001052
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +02001053 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001054 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
1055 return;
1056 }
1057
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001058 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001059 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
1060 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001061
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -08001062 if (!using_desc_dma(hsotg))
1063 maxreq = get_ep_limit(hs_ep);
1064 else
1065 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
1066
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001067 if (length > maxreq) {
1068 int round = maxreq % hs_ep->ep.maxpacket;
1069
1070 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
1071 __func__, length, maxreq, round);
1072
1073 /* round down to multiple of packets */
1074 if (round)
1075 maxreq -= round;
1076
1077 length = maxreq;
1078 }
1079
1080 if (length)
1081 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
1082 else
1083 packets = 1; /* send one packet if length is zero. */
1084
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001085 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1086 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
1087 return;
1088 }
1089
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001090 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001091 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001092 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001093 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001094 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001095 else
1096 epsize = 0;
1097
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001098 /*
1099 * zero length packet should be programmed on its own and should not
1100 * be counted in DIEPTSIZ.PktCnt with other packets.
1101 */
1102 if (dir_in && ureq->zero && !continuing) {
1103 /* Test if zlp is actually required. */
1104 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001105 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001106 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001107 }
1108
Dinh Nguyen47a16852014-04-14 14:13:34 -07001109 epsize |= DXEPTSIZ_PKTCNT(packets);
1110 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001111
1112 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1113 __func__, packets, length, ureq->length, epsize, epsize_reg);
1114
1115 /* store the request as the current one we're doing */
1116 hs_ep->req = hs_req;
1117
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001118 if (using_desc_dma(hsotg)) {
1119 u32 offset = 0;
1120 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001121
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001122 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1123 if (!dir_in) {
1124 if (!index)
1125 length = mps;
1126 else if (length % mps)
1127 length += (mps - (length % mps));
1128 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001129
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001130 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001131 * If more data to send, adjust DMA for EP0 out data stage.
1132 * ureq->dma stays unchanged, hence increment it by already
1133 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001134 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001135 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1136 continuing)
1137 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001139 /* Fill DDMA chain entries */
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01001140 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq, offset,
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001141 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001142
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001143 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001144 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001145
1146 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1147 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1148 } else {
1149 /* write size / packets */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001150 dwc2_writel(hsotg, epsize, epsize_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001151
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001152 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001153 /*
1154 * write DMA address to control register, buffer
1155 * already synced by dwc2_hsotg_ep_queue().
1156 */
1157
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001158 dwc2_writel(hsotg, ureq->dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001159
1160 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1161 __func__, &ureq->dma, dma_reg);
1162 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001163 }
1164
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001165 if (hs_ep->isochronous && hs_ep->interval == 1) {
1166 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1167 dwc2_gadget_incr_frame_num(hs_ep);
1168
1169 if (hs_ep->target_frame & 0x1)
1170 ctrl |= DXEPCTL_SETODDFR;
1171 else
1172 ctrl |= DXEPCTL_SETEVENFR;
1173 }
1174
Dinh Nguyen47a16852014-04-14 14:13:34 -07001175 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001176
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001177 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001178
1179 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001180 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001181 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001182
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001183 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001184 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001185
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001186 /*
1187 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001188 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001189 * this information.
1190 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001191 hs_ep->size_loaded = length;
1192 hs_ep->last_load = ureq->actual;
1193
1194 if (dir_in && !using_dma(hsotg)) {
1195 /* set these anyway, we may need them for non-periodic in */
1196 hs_ep->fifo_load = 0;
1197
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001198 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001199 }
1200
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001201 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001202 * Note, trying to clear the NAK here causes problems with transmit
1203 * on the S3C6400 ending up with the TXFIFO becoming full.
1204 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001205
1206 /* check ep is enabled */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001207 if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001208 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001209 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001210 index, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001211
Dinh Nguyen47a16852014-04-14 14:13:34 -07001212 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001213 __func__, dwc2_readl(hsotg, epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001214
1215 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001216 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001217}
1218
1219/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001220 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001221 * @hsotg: The device state.
1222 * @hs_ep: The endpoint the request is on.
1223 * @req: The request being processed.
1224 *
1225 * We've been asked to queue a request, so ensure that the memory buffer
1226 * is correctly setup for DMA. If we've been passed an extant DMA address
1227 * then ensure the buffer has been synced to memory. If our buffer has no
1228 * DMA memory, then we map the memory and mark our request to allow us to
1229 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001230 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001231static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001232 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001233 struct usb_request *req)
1234{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001235 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001236
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001237 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1238 if (ret)
1239 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001240
1241 return 0;
1242
1243dma_error:
1244 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1245 __func__, req->buf, req->length);
1246
1247 return -EIO;
1248}
1249
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001250static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001251 struct dwc2_hsotg_ep *hs_ep,
1252 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001253{
1254 void *req_buf = hs_req->req.buf;
1255
1256 /* If dma is not being used or buffer is aligned */
1257 if (!using_dma(hsotg) || !((long)req_buf & 3))
1258 return 0;
1259
1260 WARN_ON(hs_req->saved_req_buf);
1261
1262 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001263 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001264
1265 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1266 if (!hs_req->req.buf) {
1267 hs_req->req.buf = req_buf;
1268 dev_err(hsotg->dev,
1269 "%s: unable to allocate memory for bounce buffer\n",
1270 __func__);
1271 return -ENOMEM;
1272 }
1273
1274 /* Save actual buffer */
1275 hs_req->saved_req_buf = req_buf;
1276
1277 if (hs_ep->dir_in)
1278 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1279 return 0;
1280}
1281
John Younb98866c2017-01-17 20:31:58 -08001282static void
1283dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1284 struct dwc2_hsotg_ep *hs_ep,
1285 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001286{
1287 /* If dma is not being used or buffer was aligned */
1288 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1289 return;
1290
1291 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1292 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1293
1294 /* Copy data from bounce buffer on successful out transfer */
1295 if (!hs_ep->dir_in && !hs_req->req.status)
1296 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001297 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001298
1299 /* Free bounce buffer */
1300 kfree(hs_req->req.buf);
1301
1302 hs_req->req.buf = hs_req->saved_req_buf;
1303 hs_req->saved_req_buf = NULL;
1304}
1305
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001306/**
1307 * dwc2_gadget_target_frame_elapsed - Checks target frame
1308 * @hs_ep: The driver endpoint to check
1309 *
1310 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1311 * corresponding transfer.
1312 */
1313static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1314{
1315 struct dwc2_hsotg *hsotg = hs_ep->parent;
1316 u32 target_frame = hs_ep->target_frame;
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001317 u32 current_frame = hsotg->frame_number;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001318 bool frame_overrun = hs_ep->frame_overrun;
1319
1320 if (!frame_overrun && current_frame >= target_frame)
1321 return true;
1322
1323 if (frame_overrun && current_frame >= target_frame &&
1324 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1325 return true;
1326
1327 return false;
1328}
1329
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001330/*
1331 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1332 * @hsotg: The driver state
1333 * @hs_ep: the ep descriptor chain is for
1334 *
1335 * Called to update EP0 structure's pointers depend on stage of
1336 * control transfer.
1337 */
1338static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1339 struct dwc2_hsotg_ep *hs_ep)
1340{
1341 switch (hsotg->ep0_state) {
1342 case DWC2_EP0_SETUP:
1343 case DWC2_EP0_STATUS_OUT:
1344 hs_ep->desc_list = hsotg->setup_desc[0];
1345 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1346 break;
1347 case DWC2_EP0_DATA_IN:
1348 case DWC2_EP0_STATUS_IN:
1349 hs_ep->desc_list = hsotg->ctrl_in_desc;
1350 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1351 break;
1352 case DWC2_EP0_DATA_OUT:
1353 hs_ep->desc_list = hsotg->ctrl_out_desc;
1354 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1355 break;
1356 default:
1357 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1358 hsotg->ep0_state);
1359 return -EINVAL;
1360 }
1361
1362 return 0;
1363}
1364
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001365static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001366 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001367{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001368 struct dwc2_hsotg_req *hs_req = our_req(req);
1369 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001370 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001371 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001372 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001373 u32 maxsize = 0;
1374 u32 mask = 0;
1375
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001376
1377 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1378 ep->name, req, req->length, req->buf, req->no_interrupt,
1379 req->zero, req->short_not_ok);
1380
Gregory Herrero7ababa92015-04-29 22:09:08 +02001381 /* Prevent new request submission when controller is suspended */
Grigor Tovmasyan88b02f22018-01-24 17:44:25 +04001382 if (hs->lx_state != DWC2_L0) {
1383 dev_dbg(hs->dev, "%s: submit request only in active state\n",
John Youn9da51972017-01-17 20:30:27 -08001384 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001385 return -EAGAIN;
1386 }
1387
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001388 /* initialise status of the request */
1389 INIT_LIST_HEAD(&hs_req->queue);
1390 req->actual = 0;
1391 req->status = -EINPROGRESS;
1392
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001393 /* In DDMA mode for ISOC's don't queue request if length greater
1394 * than descriptor limits.
1395 */
1396 if (using_desc_dma(hs) && hs_ep->isochronous) {
1397 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1398 if (hs_ep->dir_in && req->length > maxsize) {
1399 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1400 req->length, maxsize);
1401 return -EINVAL;
1402 }
1403
1404 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1405 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1406 req->length, hs_ep->ep.maxpacket);
1407 return -EINVAL;
1408 }
1409 }
1410
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001411 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001412 if (ret)
1413 return ret;
1414
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001415 /* if we're using DMA, sync the buffers as necessary */
1416 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001417 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001418 if (ret)
1419 return ret;
1420 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001421 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1422 if (using_desc_dma(hs) && !hs_ep->index) {
1423 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1424 if (ret)
1425 return ret;
1426 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001427
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001428 first = list_empty(&hs_ep->queue);
1429 list_add_tail(&hs_req->queue, &hs_ep->queue);
1430
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001431 /*
1432 * Handle DDMA isochronous transfers separately - just add new entry
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001433 * to the descriptor chain.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001434 * Transfer will be started once SW gets either one of NAK or
1435 * OutTknEpDis interrupts.
1436 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001437 if (using_desc_dma(hs) && hs_ep->isochronous) {
1438 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01001439 dma_addr_t dma_addr = hs_req->req.dma;
1440
1441 if (hs_req->req.num_sgs) {
1442 WARN_ON(hs_req->req.num_sgs > 1);
1443 dma_addr = sg_dma_address(hs_req->req.sg);
1444 }
1445 dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001446 hs_req->req.length);
1447 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001448 return 0;
1449 }
1450
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +04001451 /* Change EP direction if status phase request is after data out */
1452 if (!hs_ep->index && !req->length && !hs_ep->dir_in &&
1453 hs->ep0_state == DWC2_EP0_DATA_OUT)
1454 hs_ep->dir_in = 1;
1455
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001456 if (first) {
1457 if (!hs_ep->isochronous) {
1458 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1459 return 0;
1460 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001461
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001462 /* Update current frame number value. */
1463 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1464 while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001465 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001466 /* Update current frame number value once more as it
1467 * changes here.
1468 */
1469 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1470 }
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001471
1472 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1473 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1474 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001475 return 0;
1476}
1477
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001478static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001479 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001480{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001481 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001482 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001483 unsigned long flags = 0;
1484 int ret = 0;
1485
1486 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001487 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001488 spin_unlock_irqrestore(&hs->lock, flags);
1489
1490 return ret;
1491}
1492
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001493static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001494 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001495{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001496 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001497
1498 kfree(hs_req);
1499}
1500
1501/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001502 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001503 * @ep: The endpoint the request was on.
1504 * @req: The request completed.
1505 *
1506 * Called on completion of any requests the driver itself
1507 * submitted that need cleaning up.
1508 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001509static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001510 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001511{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001512 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001513 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001514
1515 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1516
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001517 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001518}
1519
1520/**
1521 * ep_from_windex - convert control wIndex value to endpoint
1522 * @hsotg: The driver state.
1523 * @windex: The control request wIndex field (in host order).
1524 *
1525 * Convert the given wIndex into a pointer to an driver endpoint
1526 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001527 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001528static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001529 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001530{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001531 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001532 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1533 int idx = windex & 0x7F;
1534
1535 if (windex >= 0x100)
1536 return NULL;
1537
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001538 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001539 return NULL;
1540
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001541 ep = index_to_ep(hsotg, idx, dir);
1542
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001543 if (idx && ep->dir_in != dir)
1544 return NULL;
1545
1546 return ep;
1547}
1548
1549/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001550 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001551 * @hsotg: The driver state.
1552 * @testmode: requested usb test mode
1553 * Enable usb Test Mode requested by the Host.
1554 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001555int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001556{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001557 int dctl = dwc2_readl(hsotg, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001558
1559 dctl &= ~DCTL_TSTCTL_MASK;
1560 switch (testmode) {
1561 case TEST_J:
1562 case TEST_K:
1563 case TEST_SE0_NAK:
1564 case TEST_PACKET:
1565 case TEST_FORCE_EN:
1566 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1567 break;
1568 default:
1569 return -EINVAL;
1570 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001571 dwc2_writel(hsotg, dctl, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001572 return 0;
1573}
1574
1575/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001576 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001577 * @hsotg: The device state
1578 * @ep: Endpoint 0
1579 * @buff: Buffer for request
1580 * @length: Length of reply.
1581 *
1582 * Create a request and queue it on the given endpoint. This is useful as
1583 * an internal method of sending replies to certain control requests, etc.
1584 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001585static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001586 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001587 void *buff,
1588 int length)
1589{
1590 struct usb_request *req;
1591 int ret;
1592
1593 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1594
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001595 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001596 hsotg->ep0_reply = req;
1597 if (!req) {
1598 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1599 return -ENOMEM;
1600 }
1601
1602 req->buf = hsotg->ep0_buff;
1603 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001604 /*
1605 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1606 * STATUS stage.
1607 */
1608 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001609 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001610
1611 if (length)
1612 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001613
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001614 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001615 if (ret) {
1616 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1617 return ret;
1618 }
1619
1620 return 0;
1621}
1622
1623/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001624 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001625 * @hsotg: The device state
1626 * @ctrl: USB control request
1627 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001628static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001629 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001630{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001631 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1632 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001633 __le16 reply;
1634 int ret;
1635
1636 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1637
1638 if (!ep0->dir_in) {
1639 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1640 return -EINVAL;
1641 }
1642
1643 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1644 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001645 /*
1646 * bit 0 => self powered
1647 * bit 1 => remote wakeup
1648 */
1649 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001650 break;
1651
1652 case USB_RECIP_INTERFACE:
1653 /* currently, the data result should be zero */
1654 reply = cpu_to_le16(0);
1655 break;
1656
1657 case USB_RECIP_ENDPOINT:
1658 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1659 if (!ep)
1660 return -ENOENT;
1661
1662 reply = cpu_to_le16(ep->halted ? 1 : 0);
1663 break;
1664
1665 default:
1666 return 0;
1667 }
1668
1669 if (le16_to_cpu(ctrl->wLength) != 2)
1670 return -EINVAL;
1671
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001672 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001673 if (ret) {
1674 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1675 return ret;
1676 }
1677
1678 return 1;
1679}
1680
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001681static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001682
1683/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001684 * get_ep_head - return the first request on the endpoint
1685 * @hs_ep: The controller endpoint to get
1686 *
1687 * Get the first request on the endpoint.
1688 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001689static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001690{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001691 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1692 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001693}
1694
1695/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001696 * dwc2_gadget_start_next_request - Starts next request from ep queue
1697 * @hs_ep: Endpoint structure
1698 *
1699 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1700 * in its handler. Hence we need to unmask it here to be able to do
1701 * resynchronization.
1702 */
1703static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1704{
1705 u32 mask;
1706 struct dwc2_hsotg *hsotg = hs_ep->parent;
1707 int dir_in = hs_ep->dir_in;
1708 struct dwc2_hsotg_req *hs_req;
1709 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1710
1711 if (!list_empty(&hs_ep->queue)) {
1712 hs_req = get_ep_head(hs_ep);
1713 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1714 return;
1715 }
1716 if (!hs_ep->isochronous)
1717 return;
1718
1719 if (dir_in) {
1720 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1721 __func__);
1722 } else {
1723 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1724 __func__);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001725 mask = dwc2_readl(hsotg, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001726 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001727 dwc2_writel(hsotg, mask, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001728 }
1729}
1730
1731/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001732 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001733 * @hsotg: The device state
1734 * @ctrl: USB control request
1735 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001736static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001737 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001738{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001739 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1740 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001741 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001742 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001743 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001744 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001745 u32 recip;
1746 u32 wValue;
1747 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001748
1749 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1750 __func__, set ? "SET" : "CLEAR");
1751
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001752 wValue = le16_to_cpu(ctrl->wValue);
1753 wIndex = le16_to_cpu(ctrl->wIndex);
1754 recip = ctrl->bRequestType & USB_RECIP_MASK;
1755
1756 switch (recip) {
1757 case USB_RECIP_DEVICE:
1758 switch (wValue) {
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001759 case USB_DEVICE_REMOTE_WAKEUP:
1760 hsotg->remote_wakeup_allowed = 1;
1761 break;
1762
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001763 case USB_DEVICE_TEST_MODE:
1764 if ((wIndex & 0xff) != 0)
1765 return -EINVAL;
1766 if (!set)
1767 return -EINVAL;
1768
1769 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001770 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001771 if (ret) {
1772 dev_err(hsotg->dev,
1773 "%s: failed to send reply\n", __func__);
1774 return ret;
1775 }
1776 break;
1777 default:
1778 return -ENOENT;
1779 }
1780 break;
1781
1782 case USB_RECIP_ENDPOINT:
1783 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001784 if (!ep) {
1785 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001786 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001787 return -ENOENT;
1788 }
1789
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001790 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001791 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001792 halted = ep->halted;
1793
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001794 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001795
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001796 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001797 if (ret) {
1798 dev_err(hsotg->dev,
1799 "%s: failed to send reply\n", __func__);
1800 return ret;
1801 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001802
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001803 /*
1804 * we have to complete all requests for ep if it was
1805 * halted, and the halt was cleared by CLEAR_FEATURE
1806 */
1807
1808 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001809 /*
1810 * If we have request in progress,
1811 * then complete it
1812 */
1813 if (ep->req) {
1814 hs_req = ep->req;
1815 ep->req = NULL;
1816 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001817 if (hs_req->req.complete) {
1818 spin_unlock(&hsotg->lock);
1819 usb_gadget_giveback_request(
1820 &ep->ep, &hs_req->req);
1821 spin_lock(&hsotg->lock);
1822 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001823 }
1824
1825 /* If we have pending request, then start it */
John Youn34c0887f2017-01-17 20:31:43 -08001826 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001827 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001828 }
1829
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001830 break;
1831
1832 default:
1833 return -ENOENT;
1834 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001835 break;
1836 default:
1837 return -ENOENT;
1838 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001839 return 1;
1840}
1841
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001842static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001843
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001844/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001845 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001846 * @hsotg: The device state
1847 *
1848 * Set stall for ep0 as response for setup request.
1849 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001850static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001851{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001852 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001853 u32 reg;
1854 u32 ctrl;
1855
1856 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1857 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1858
1859 /*
1860 * DxEPCTL_Stall will be cleared by EP once it has
1861 * taken effect, so no need to clear later.
1862 */
1863
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001864 ctrl = dwc2_readl(hsotg, reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001865 ctrl |= DXEPCTL_STALL;
1866 ctrl |= DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001867 dwc2_writel(hsotg, ctrl, reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001868
1869 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001870 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001871 ctrl, reg, dwc2_readl(hsotg, reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001872
1873 /*
1874 * complete won't be called, so we enqueue
1875 * setup request here
1876 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001877 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001878}
1879
1880/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001881 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001882 * @hsotg: The device state
1883 * @ctrl: The control request received
1884 *
1885 * The controller has received the SETUP phase of a control request, and
1886 * needs to work out what to do next (and whether to pass it on to the
1887 * gadget driver).
1888 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001889static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001890 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001891{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001892 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001893 int ret = 0;
1894 u32 dcfg;
1895
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001896 dev_dbg(hsotg->dev,
1897 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1898 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1899 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001900
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001901 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001902 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001903 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1904 } else if (ctrl->bRequestType & USB_DIR_IN) {
1905 ep0->dir_in = 1;
1906 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1907 } else {
1908 ep0->dir_in = 0;
1909 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1910 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001911
1912 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1913 switch (ctrl->bRequest) {
1914 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001915 hsotg->connected = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001916 dcfg = dwc2_readl(hsotg, DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001917 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001918 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1919 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001920 dwc2_writel(hsotg, dcfg, DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001921
1922 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1923
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001924 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001925 return;
1926
1927 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001928 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001929 break;
1930
1931 case USB_REQ_CLEAR_FEATURE:
1932 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001933 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001934 break;
1935 }
1936 }
1937
1938 /* as a fallback, try delivering it to the driver to deal with */
1939
1940 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001941 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001942 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001943 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001944 if (ret < 0)
1945 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1946 }
1947
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +04001948 hsotg->delayed_status = false;
1949 if (ret == USB_GADGET_DELAYED_STATUS)
1950 hsotg->delayed_status = true;
1951
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001952 /*
1953 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001954 * so respond with a STALL for the status stage to indicate failure.
1955 */
1956
Robert Baldygac9f721b2014-01-14 08:36:00 +01001957 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001958 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001959}
1960
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001961/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001962 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001963 * @ep: The endpoint the request was on.
1964 * @req: The request completed.
1965 *
1966 * Called on completion of any requests the driver itself submitted for
1967 * EP0 setup packets
1968 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001969static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001970 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001971{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001972 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001973 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001974
1975 if (req->status < 0) {
1976 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1977 return;
1978 }
1979
Robert Baldyga93f599f2013-11-21 13:49:17 +01001980 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001981 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001982 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001983 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001984 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001985 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001986}
1987
1988/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001989 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001990 * @hsotg: The device state.
1991 *
1992 * Enqueue a request on EP0 if necessary to received any SETUP packets
1993 * received from the host.
1994 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001995static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996{
1997 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001998 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001999 int ret;
2000
2001 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
2002
2003 req->zero = 0;
2004 req->length = 8;
2005 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002006 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002007
2008 if (!list_empty(&hs_req->queue)) {
2009 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
2010 return;
2011 }
2012
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002013 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002014 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002015 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002016
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002017 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002018 if (ret < 0) {
2019 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002020 /*
2021 * Don't think there's much we can do other than watch the
2022 * driver fail.
2023 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002024 }
2025}
2026
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002027static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002028 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002029{
2030 u32 ctrl;
2031 u8 index = hs_ep->index;
2032 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
2033 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
2034
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01002035 if (hs_ep->dir_in)
2036 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002037 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01002038 else
2039 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002040 index);
2041 if (using_desc_dma(hsotg)) {
Minas Harutyunyan201ec562018-01-16 16:03:32 +04002042 if (!index)
2043 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
2044
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01002045 /* Not specific buffer needed for ep0 ZLP */
2046 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &hs_ep->desc_list,
2047 hs_ep->desc_list_dma, 0, true);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002048 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002049 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2050 DXEPTSIZ_XFERSIZE(0),
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002051 epsiz_reg);
2052 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002053
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002054 ctrl = dwc2_readl(hsotg, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002055 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
2056 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
2057 ctrl |= DXEPCTL_USBACTEP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002058 dwc2_writel(hsotg, ctrl, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002059}
2060
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002061/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002062 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002063 * @hsotg: The device state.
2064 * @hs_ep: The endpoint the request was on.
2065 * @hs_req: The request to complete.
2066 * @result: The result code (0 => Ok, otherwise errno)
2067 *
2068 * The given request has finished, so call the necessary completion
2069 * if it has one and then look to see if we can start a new request
2070 * on the endpoint.
2071 *
2072 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002073 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002074static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002075 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002076 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002077 int result)
2078{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002079 if (!hs_req) {
2080 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
2081 return;
2082 }
2083
2084 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
2085 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
2086
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002087 /*
2088 * only replace the status if we've not already set an error
2089 * from a previous transaction
2090 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002091
2092 if (hs_req->req.status == -EINPROGRESS)
2093 hs_req->req.status = result;
2094
Yunzhi Li44583fe2015-09-29 12:25:01 +02002095 if (using_dma(hsotg))
2096 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2097
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002098 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01002099
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002100 hs_ep->req = NULL;
2101 list_del_init(&hs_req->queue);
2102
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002103 /*
2104 * call the complete request with the locks off, just in case the
2105 * request tries to queue more work for this endpoint.
2106 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002107
2108 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002109 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002110 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002111 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002112 }
2113
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002114 /* In DDMA don't need to proceed to starting of next ISOC request */
2115 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2116 return;
2117
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002118 /*
2119 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002120 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002121 * so be careful when doing this.
2122 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002123
John Youn34c0887f2017-01-17 20:31:43 -08002124 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002125 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002126}
2127
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002128/*
2129 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2130 * @hs_ep: The endpoint the request was on.
2131 *
2132 * Get first request from the ep queue, determine descriptor on which complete
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002133 * happened. SW discovers which descriptor currently in use by HW, adjusts
2134 * dma_address and calculates index of completed descriptor based on the value
2135 * of DEPDMA register. Update actual length of request, giveback to gadget.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002136 */
2137static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2138{
2139 struct dwc2_hsotg *hsotg = hs_ep->parent;
2140 struct dwc2_hsotg_req *hs_req;
2141 struct usb_request *ureq;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002142 u32 desc_sts;
2143 u32 mask;
2144
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002145 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2146
2147 /* Process only descriptors with buffer status set to DMA done */
2148 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2149 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2150
2151 hs_req = get_ep_head(hs_ep);
2152 if (!hs_req) {
2153 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2154 return;
2155 }
2156 ureq = &hs_req->req;
2157
2158 /* Check completion status */
2159 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2160 DEV_DMA_STS_SUCC) {
2161 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2162 DEV_DMA_ISOC_RX_NBYTES_MASK;
2163 ureq->actual = ureq->length - ((desc_sts & mask) >>
2164 DEV_DMA_ISOC_NBYTES_SHIFT);
2165
2166 /* Adjust actual len for ISOC Out if len is
2167 * not align of 4
2168 */
2169 if (!hs_ep->dir_in && ureq->length & 0x3)
2170 ureq->actual += 4 - (ureq->length & 0x3);
2171 }
2172
2173 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2174
2175 hs_ep->compl_desc++;
2176 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_GENERIC - 1))
2177 hs_ep->compl_desc = 0;
2178 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002179 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002180}
2181
2182/*
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002183 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2184 * @hs_ep: The isochronous endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002185 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002186 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2187 * interrupt. Reset target frame and next_desc to allow to start
2188 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2189 * interrupt for OUT direction.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002190 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002191static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002192{
2193 struct dwc2_hsotg *hsotg = hs_ep->parent;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002194
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002195 if (!hs_ep->dir_in)
2196 dwc2_flush_rx_fifo(hsotg);
2197 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002198
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002199 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2200 hs_ep->next_desc = 0;
2201 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002202}
2203
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002204/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002205 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002206 * @hsotg: The device state.
2207 * @ep_idx: The endpoint index for the data
2208 * @size: The size of data in the fifo, in bytes
2209 *
2210 * The FIFO status shows there is data to read from the FIFO for a given
2211 * endpoint, so sort out whether we need to read the data into a request
2212 * that has been made for that endpoint.
2213 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002214static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002215{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002216 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2217 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002218 int to_read;
2219 int max_req;
2220 int read_ptr;
2221
2222 if (!hs_req) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002223 u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002224 int ptr;
2225
Robert Baldyga6b448af42014-12-16 11:51:44 +01002226 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002227 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002228 __func__, size, ep_idx, epctl);
2229
2230 /* dump the data from the FIFO, we've nothing we can do */
2231 for (ptr = 0; ptr < size; ptr += 4)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002232 (void)dwc2_readl(hsotg, EPFIFO(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002233
2234 return;
2235 }
2236
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002237 to_read = size;
2238 read_ptr = hs_req->req.actual;
2239 max_req = hs_req->req.length - read_ptr;
2240
Ben Dooksa33e7132010-07-19 09:40:49 +01002241 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2242 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2243
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002244 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002245 /*
2246 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002247 * to deal with in this request.
2248 */
2249
2250 /* currently we don't deal this */
2251 WARN_ON_ONCE(1);
2252 }
2253
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002254 hs_ep->total_data += to_read;
2255 hs_req->req.actual += to_read;
2256 to_read = DIV_ROUND_UP(to_read, 4);
2257
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002258 /*
2259 * note, we might over-write the buffer end by 3 bytes depending on
2260 * alignment of the data.
2261 */
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +04002262 dwc2_readl_rep(hsotg, EPFIFO(ep_idx),
2263 hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002264}
2265
2266/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002267 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002268 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002269 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002270 *
2271 * Generate a zero-length IN packet request for terminating a SETUP
2272 * transaction.
2273 *
2274 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002275 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002276 * the TxFIFO.
2277 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002278static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002279{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002280 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002281 hsotg->eps_out[0]->dir_in = dir_in;
2282 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002283
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002284 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002285}
2286
Roman Bacikec1f9d92015-09-10 18:13:43 -07002287static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002288 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002289{
2290 u32 ctrl;
2291
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002292 ctrl = dwc2_readl(hsotg, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002293 if (ctrl & DXEPCTL_EOFRNUM)
2294 ctrl |= DXEPCTL_SETEVENFR;
2295 else
2296 ctrl |= DXEPCTL_SETODDFR;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002297 dwc2_writel(hsotg, ctrl, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002298}
2299
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002300/*
2301 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2302 * @hs_ep - The endpoint on which transfer went
2303 *
2304 * Iterate over endpoints descriptor chain and get info on bytes remained
2305 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2306 */
2307static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2308{
2309 struct dwc2_hsotg *hsotg = hs_ep->parent;
2310 unsigned int bytes_rem = 0;
2311 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2312 int i;
2313 u32 status;
2314
2315 if (!desc)
2316 return -EINVAL;
2317
2318 for (i = 0; i < hs_ep->desc_count; ++i) {
2319 status = desc->status;
2320 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2321
2322 if (status & DEV_DMA_STS_MASK)
2323 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2324 i, status & DEV_DMA_STS_MASK);
2325 }
2326
2327 return bytes_rem;
2328}
2329
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002330/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002331 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002332 * @hsotg: The device instance
2333 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002334 *
2335 * The RXFIFO has delivered an OutDone event, which means that the data
2336 * transfer for an OUT endpoint has been completed, either by a short
2337 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002338 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002339static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002340{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002341 u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002342 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2343 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002344 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002345 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002346 int result = 0;
2347
2348 if (!hs_req) {
2349 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2350 return;
2351 }
2352
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002353 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2354 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002355 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2356 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002357 return;
2358 }
2359
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002360 if (using_desc_dma(hsotg))
2361 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2362
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002363 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002364 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002365
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002366 /*
2367 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002368 * is left in the endpoint size register and then working it
2369 * out from the amount we loaded for the transfer.
2370 *
2371 * We need to do this as DMA pointers are always 32bit aligned
2372 * so may overshoot/undershoot the transfer.
2373 */
2374
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002375 size_done = hs_ep->size_loaded - size_left;
2376 size_done += hs_ep->last_load;
2377
2378 req->actual = size_done;
2379 }
2380
Ben Dooksa33e7132010-07-19 09:40:49 +01002381 /* if there is more request to do, schedule new transfer */
2382 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002383 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002384 return;
2385 }
2386
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002387 if (req->actual < req->length && req->short_not_ok) {
2388 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2389 __func__, req->actual, req->length);
2390
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002391 /*
2392 * todo - what should we return here? there's no one else
2393 * even bothering to check the status.
2394 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002395 }
2396
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002397 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2398 if (!using_desc_dma(hsotg) && epnum == 0 &&
2399 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002400 /* Move to STATUS IN */
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +04002401 if (!hsotg->delayed_status)
2402 dwc2_hsotg_ep0_zlp(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002403 }
2404
Roman Bacikec1f9d92015-09-10 18:13:43 -07002405 /*
2406 * Slave mode OUT transfers do not go through XferComplete so
2407 * adjust the ISOC parity here.
2408 */
2409 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002410 if (hs_ep->isochronous && hs_ep->interval == 1)
2411 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002412 else if (hs_ep->isochronous && hs_ep->interval > 1)
2413 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002414 }
2415
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002416 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002417}
2418
2419/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002420 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002421 * @hsotg: The device instance
2422 *
2423 * The IRQ handler has detected that the RX FIFO has some data in it
2424 * that requires processing, so find out what is in there and do the
2425 * appropriate read.
2426 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002427 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002428 * chunks, so if you have x packets received on an endpoint you'll get x
2429 * FIFO events delivered, each with a packet's worth of data in it.
2430 *
2431 * When using DMA, we should not be processing events from the RXFIFO
2432 * as the actual data should be sent to the memory directly and we turn
2433 * on the completion interrupts to get notifications of transfer completion.
2434 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002435static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002436{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002437 u32 grxstsr = dwc2_readl(hsotg, GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002438 u32 epnum, status, size;
2439
2440 WARN_ON(using_dma(hsotg));
2441
Dinh Nguyen47a16852014-04-14 14:13:34 -07002442 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2443 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002444
Dinh Nguyen47a16852014-04-14 14:13:34 -07002445 size = grxstsr & GRXSTS_BYTECNT_MASK;
2446 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002447
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002448 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002449 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002450
Dinh Nguyen47a16852014-04-14 14:13:34 -07002451 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2452 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2453 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002454 break;
2455
Dinh Nguyen47a16852014-04-14 14:13:34 -07002456 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002457 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002458 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002459
2460 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002461 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002462 break;
2463
Dinh Nguyen47a16852014-04-14 14:13:34 -07002464 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002465 dev_dbg(hsotg->dev,
2466 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002467 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002468 dwc2_readl(hsotg, DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002469 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002470 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002471 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2472 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2473 */
2474 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002475 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002476 break;
2477
Dinh Nguyen47a16852014-04-14 14:13:34 -07002478 case GRXSTS_PKTSTS_OUTRX:
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
Dinh Nguyen47a16852014-04-14 14:13:34 -07002482 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002483 dev_dbg(hsotg->dev,
2484 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002485 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002486 dwc2_readl(hsotg, DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002488 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2489
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002490 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002491 break;
2492
2493 default:
2494 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2495 __func__, grxstsr);
2496
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002497 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498 break;
2499 }
2500}
2501
2502/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002503 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002504 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002505 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002506static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002507{
2508 switch (mps) {
2509 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002510 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002511 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002512 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002513 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002514 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002515 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002516 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002517 }
2518
2519 /* bad max packet size, warn and return invalid result */
2520 WARN_ON(1);
2521 return (u32)-1;
2522}
2523
2524/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002525 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002526 * @hsotg: The driver state.
2527 * @ep: The index number of the endpoint
2528 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002529 * @mc: The multicount value
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002530 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002531 *
2532 * Configure the maximum packet size for the given endpoint, updating
2533 * the hardware control registers to reflect this.
2534 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002535static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002536 unsigned int ep, unsigned int mps,
2537 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002538{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002539 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002540 u32 reg;
2541
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002542 hs_ep = index_to_ep(hsotg, ep, dir_in);
2543 if (!hs_ep)
2544 return;
2545
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002546 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002547 u32 mps_bytes = mps;
2548
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002549 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002550 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2551 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002552 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002553 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002554 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002555 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002556 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002557 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002558 hs_ep->mc = mc;
2559 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002560 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002561 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002562 }
2563
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002564 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002565 reg = dwc2_readl(hsotg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002566 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002567 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002568 dwc2_writel(hsotg, reg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002569 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002570 reg = dwc2_readl(hsotg, DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002571 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002572 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002573 dwc2_writel(hsotg, reg, DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002574 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002575
2576 return;
2577
2578bad_mps:
2579 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2580}
2581
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002582/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002583 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002584 * @hsotg: The driver state
2585 * @idx: The index for the endpoint (0..15)
2586 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002587static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002588{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002589 dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2590 GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002591
2592 /* wait until the fifo is flushed */
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002593 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2594 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2595 __func__);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002596}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002597
2598/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002599 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002600 * @hsotg: The driver state
2601 * @hs_ep: The driver endpoint to check.
2602 *
2603 * Check to see if there is a request that has data to send, and if so
2604 * make an attempt to write data into the FIFO.
2605 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002606static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002607 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002608{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002609 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002610
Robert Baldygaafcf4162013-09-19 11:50:19 +02002611 if (!hs_ep->dir_in || !hs_req) {
2612 /**
2613 * if request is not enqueued, we disable interrupts
2614 * for endpoints, excepting ep0
2615 */
2616 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002617 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002618 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002619 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002620 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002621
2622 if (hs_req->req.actual < hs_req->req.length) {
2623 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2624 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002625 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002626 }
2627
2628 return 0;
2629}
2630
2631/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002632 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002633 * @hsotg: The device state.
2634 * @hs_ep: The endpoint that has just completed.
2635 *
2636 * An IN transfer has been completed, update the transfer's state and then
2637 * call the relevant completion routines.
2638 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002639static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002640 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002641{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002642 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002643 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002644 int size_left, size_done;
2645
2646 if (!hs_req) {
2647 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2648 return;
2649 }
2650
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002651 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002652 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2653 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002654
2655 /*
2656 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2657 * changed to IN. Change back to complete OUT transfer request
2658 */
2659 hs_ep->dir_in = 0;
2660
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002661 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002662 if (hsotg->test_mode) {
2663 int ret;
2664
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002665 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002666 if (ret < 0) {
2667 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002668 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002669 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002670 return;
2671 }
2672 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002673 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002674 return;
2675 }
2676
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002677 /*
2678 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002679 * in the endpoint size register and then working it out from
2680 * the amount we loaded for the transfer.
2681 *
2682 * We do this even for DMA, as the transfer may have incremented
2683 * past the end of the buffer (DMA transfers are always 32bit
2684 * aligned).
2685 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002686 if (using_desc_dma(hsotg)) {
2687 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2688 if (size_left < 0)
2689 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2690 size_left);
2691 } else {
2692 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2693 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002694
2695 size_done = hs_ep->size_loaded - size_left;
2696 size_done += hs_ep->last_load;
2697
2698 if (hs_req->req.actual != size_done)
2699 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2700 __func__, hs_req->req.actual, size_done);
2701
2702 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002703 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2704 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002705
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002706 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2707 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002708 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002709 return;
2710 }
2711
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002712 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002713 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002714 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002715 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002716 /* transfer will be completed on next complete interrupt */
2717 return;
2718 }
2719
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002720 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2721 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002722 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002723 return;
2724 }
2725
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002726 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002727}
2728
2729/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002730 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2731 * @hsotg: The device state.
2732 * @idx: Index of ep.
2733 * @dir_in: Endpoint direction 1-in 0-out.
2734 *
2735 * Reads for endpoint with given index and direction, by masking
2736 * epint_reg with coresponding mask.
2737 */
2738static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2739 unsigned int idx, int dir_in)
2740{
2741 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2742 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2743 u32 ints;
2744 u32 mask;
2745 u32 diepempmsk;
2746
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002747 mask = dwc2_readl(hsotg, epmsk_reg);
2748 diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002749 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2750 mask |= DXEPINT_SETUP_RCVD;
2751
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002752 ints = dwc2_readl(hsotg, epint_reg);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002753 ints &= mask;
2754 return ints;
2755}
2756
2757/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002758 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2759 * @hs_ep: The endpoint on which interrupt is asserted.
2760 *
2761 * This interrupt indicates that the endpoint has been disabled per the
2762 * application's request.
2763 *
2764 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2765 * in case of ISOC completes current request.
2766 *
2767 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2768 * request starts it.
2769 */
2770static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2771{
2772 struct dwc2_hsotg *hsotg = hs_ep->parent;
2773 struct dwc2_hsotg_req *hs_req;
2774 unsigned char idx = hs_ep->index;
2775 int dir_in = hs_ep->dir_in;
2776 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002777 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002778
2779 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2780
2781 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002782 int epctl = dwc2_readl(hsotg, epctl_reg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002783
2784 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2785
2786 if (hs_ep->isochronous) {
2787 dwc2_hsotg_complete_in(hsotg, hs_ep);
2788 return;
2789 }
2790
2791 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002792 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002793
2794 dctl |= DCTL_CGNPINNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002795 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002796 }
2797 return;
2798 }
2799
2800 if (dctl & DCTL_GOUTNAKSTS) {
2801 dctl |= DCTL_CGOUTNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002802 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002803 }
2804
2805 if (!hs_ep->isochronous)
2806 return;
2807
2808 if (list_empty(&hs_ep->queue)) {
2809 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2810 __func__, hs_ep);
2811 return;
2812 }
2813
2814 do {
2815 hs_req = get_ep_head(hs_ep);
2816 if (hs_req)
2817 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2818 -ENODATA);
2819 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04002820 /* Update current frame number value. */
2821 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002822 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2823
2824 dwc2_gadget_start_next_request(hs_ep);
2825}
2826
2827/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002828 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002829 * @ep: The endpoint on which interrupt is asserted.
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002830 *
2831 * This is starting point for ISOC-OUT transfer, synchronization done with
2832 * first out token received from host while corresponding EP is disabled.
2833 *
2834 * Device does not know initial frame in which out token will come. For this
2835 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2836 * getting this interrupt SW starts calculation for next transfer frame.
2837 */
2838static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2839{
2840 struct dwc2_hsotg *hsotg = ep->parent;
2841 int dir_in = ep->dir_in;
2842 u32 doepmsk;
2843
2844 if (dir_in || !ep->isochronous)
2845 return;
2846
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002847 if (using_desc_dma(hsotg)) {
2848 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2849 /* Start first ISO Out */
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002850 ep->target_frame = hsotg->frame_number;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002851 dwc2_gadget_start_isoc_ddma(ep);
2852 }
2853 return;
2854 }
2855
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002856 if (ep->interval > 1 &&
2857 ep->target_frame == TARGET_FRAME_INITIAL) {
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002858 u32 ctrl;
2859
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002860 ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002861 dwc2_gadget_incr_frame_num(ep);
2862
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002863 ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002864 if (ep->target_frame & 0x1)
2865 ctrl |= DXEPCTL_SETODDFR;
2866 else
2867 ctrl |= DXEPCTL_SETEVENFR;
2868
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002869 dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002870 }
2871
2872 dwc2_gadget_start_next_request(ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002873 doepmsk = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002874 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002875 dwc2_writel(hsotg, doepmsk, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002876}
2877
2878/**
John Youn38beaec2017-01-17 20:31:13 -08002879 * dwc2_gadget_handle_nak - handle NAK interrupt
2880 * @hs_ep: The endpoint on which interrupt is asserted.
2881 *
2882 * This is starting point for ISOC-IN transfer, synchronization done with
2883 * first IN token received from host while corresponding EP is disabled.
2884 *
2885 * Device does not know when first one token will arrive from host. On first
2886 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2887 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2888 * sent in response to that as there was no data in FIFO. SW is basing on this
2889 * interrupt to obtain frame in which token has come and then based on the
2890 * interval calculates next frame for transfer.
2891 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002892static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2893{
2894 struct dwc2_hsotg *hsotg = hs_ep->parent;
2895 int dir_in = hs_ep->dir_in;
2896
2897 if (!dir_in || !hs_ep->isochronous)
2898 return;
2899
2900 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002901
2902 if (using_desc_dma(hsotg)) {
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002903 hs_ep->target_frame = hsotg->frame_number;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002904 dwc2_gadget_incr_frame_num(hs_ep);
Grigor Tovmasyan48dac4e2018-08-29 21:00:33 +04002905
2906 /* In service interval mode target_frame must
2907 * be set to last (u)frame of the service interval.
2908 */
2909 if (hsotg->params.service_interval) {
2910 /* Set target_frame to the first (u)frame of
2911 * the service interval
2912 */
2913 hs_ep->target_frame &= ~hs_ep->interval + 1;
2914
2915 /* Set target_frame to the last (u)frame of
2916 * the service interval
2917 */
2918 dwc2_gadget_incr_frame_num(hs_ep);
2919 dwc2_gadget_dec_frame_num_by_one(hs_ep);
2920 }
2921
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002922 dwc2_gadget_start_isoc_ddma(hs_ep);
2923 return;
2924 }
2925
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002926 hs_ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002927 if (hs_ep->interval > 1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002928 u32 ctrl = dwc2_readl(hsotg,
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002929 DIEPCTL(hs_ep->index));
2930 if (hs_ep->target_frame & 0x1)
2931 ctrl |= DXEPCTL_SETODDFR;
2932 else
2933 ctrl |= DXEPCTL_SETEVENFR;
2934
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002935 dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002936 }
2937
2938 dwc2_hsotg_complete_request(hsotg, hs_ep,
2939 get_ep_head(hs_ep), 0);
2940 }
2941
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002942 if (!using_desc_dma(hsotg))
2943 dwc2_gadget_incr_frame_num(hs_ep);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002944}
2945
2946/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002947 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002948 * @hsotg: The driver state
2949 * @idx: The index for the endpoint (0..15)
2950 * @dir_in: Set if this is an IN endpoint
2951 *
2952 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002953 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002954static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002955 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002956{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002957 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002958 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2959 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2960 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002961 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002962 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002963
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002964 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002965 ctrl = dwc2_readl(hsotg, epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002966
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002967 /* Clear endpoint interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002968 dwc2_writel(hsotg, ints, epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002969
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002970 if (!hs_ep) {
2971 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002972 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002973 return;
2974 }
2975
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002976 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2977 __func__, idx, dir_in ? "in" : "out", ints);
2978
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002979 /* Don't process XferCompl interrupt if it is a setup packet */
2980 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2981 ints &= ~DXEPINT_XFERCOMPL;
2982
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002983 /*
2984 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2985 * stage and xfercomplete was generated without SETUP phase done
2986 * interrupt. SW should parse received setup packet only after host's
2987 * exit from setup phase of control transfer.
2988 */
2989 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2990 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2991 ints &= ~DXEPINT_XFERCOMPL;
2992
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002993 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002994 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002995 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002996 __func__, dwc2_readl(hsotg, epctl_reg),
2997 dwc2_readl(hsotg, epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002998
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002999 /* In DDMA handle isochronous requests separately */
3000 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003001 /* XferCompl set along with BNA */
3002 if (!(ints & DXEPINT_BNAINTR))
3003 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003004 } else if (dir_in) {
3005 /*
3006 * We get OutDone from the FIFO, so we only
3007 * need to look at completing IN requests here
3008 * if operating slave mode
3009 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003010 if (hs_ep->isochronous && hs_ep->interval > 1)
3011 dwc2_gadget_incr_frame_num(hs_ep);
3012
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003013 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003014 if (ints & DXEPINT_NAKINTRPT)
3015 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003016
Ben Dooksc9a64ea2010-07-19 09:40:46 +01003017 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003018 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003019 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003020 /*
3021 * We're using DMA, we need to fire an OutDone here
3022 * as we ignore the RXFIFO.
3023 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003024 if (hs_ep->isochronous && hs_ep->interval > 1)
3025 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003027 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003028 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003029 }
3030
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07003031 if (ints & DXEPINT_EPDISBLD)
3032 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003033
Vardan Mikayelyan53219222016-05-25 18:07:14 -07003034 if (ints & DXEPINT_OUTTKNEPDIS)
3035 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
3036
3037 if (ints & DXEPINT_NAKINTRPT)
3038 dwc2_gadget_handle_nak(hs_ep);
3039
Dinh Nguyen47a16852014-04-14 14:13:34 -07003040 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003041 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003042
Dinh Nguyen47a16852014-04-14 14:13:34 -07003043 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003044 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
3045
3046 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003047 /*
3048 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003049 * setup packet. In non-DMA mode we'd get this
3050 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003051 * the setup here.
3052 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003053
3054 if (dir_in)
3055 WARN_ON_ONCE(1);
3056 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003057 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003058 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003059 }
3060
Vahram Aharonyanef750c72016-11-14 19:16:31 -08003061 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003062 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
3063
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04003064 /* Safety check EP0 state when STSPHSERCVD asserted */
3065 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
3066 /* Move to STATUS IN for DDMA */
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +04003067 if (using_desc_dma(hsotg)) {
3068 if (!hsotg->delayed_status)
3069 dwc2_hsotg_ep0_zlp(hsotg, true);
3070 else
3071 /* In case of 3 stage Control Write with delayed
3072 * status, when Status IN transfer started
3073 * before STSPHSERCVD asserted, NAKSTS bit not
3074 * cleared by CNAK in dwc2_hsotg_start_req()
3075 * function. Clear now NAKSTS to allow complete
3076 * transfer.
3077 */
3078 dwc2_set_bit(hsotg, DIEPCTL(0),
3079 DXEPCTL_CNAK);
3080 }
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04003081 }
3082
Vahram Aharonyanef750c72016-11-14 19:16:31 -08003083 }
3084
Dinh Nguyen47a16852014-04-14 14:13:34 -07003085 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003086 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003087
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003088 if (ints & DXEPINT_BNAINTR) {
3089 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003090 if (hs_ep->isochronous)
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003091 dwc2_gadget_handle_isoc_bna(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003092 }
3093
Robert Baldyga1479e842013-10-09 08:41:57 +02003094 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003095 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003096 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003097 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
3098 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003099 }
3100
3101 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003102 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003103 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
3104 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003105 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003106
3107 /* FIFO has space or is empty (see GAHBCFG) */
3108 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003109 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01003110 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3111 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09003112 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003113 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01003114 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003116}
3117
3118/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003119 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003120 * @hsotg: The device state.
3121 *
3122 * Handle updating the device settings after the enumeration phase has
3123 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003124 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003125static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003126{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003127 u32 dsts = dwc2_readl(hsotg, DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003128 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003129
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003130 /*
3131 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003132 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003133 * we connected at.
3134 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003135
3136 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3137
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003138 /*
3139 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003140 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003141 * not advertise a 64byte MPS on EP0.
3142 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003143
3144 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003145 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003146 case DSTS_ENUMSPD_FS:
3147 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003148 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003149 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003150 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003151 break;
3152
Dinh Nguyen47a16852014-04-14 14:13:34 -07003153 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003154 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003155 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003156 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003157 break;
3158
Dinh Nguyen47a16852014-04-14 14:13:34 -07003159 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003160 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003161 ep0_mps = 8;
3162 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003163 /*
3164 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003165 * moment, and the documentation seems to imply that it isn't
3166 * supported by the PHYs on some of the devices.
3167 */
3168 break;
3169 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003170 dev_info(hsotg->dev, "new device is %s\n",
3171 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003172
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003173 /*
3174 * we should now know the maximum packet size for an
3175 * endpoint, so set the endpoints to a default value.
3176 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003177
3178 if (ep0_mps) {
3179 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003180 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003181 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3182 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003183 for (i = 1; i < hsotg->num_of_eps; i++) {
3184 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003185 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3186 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003187 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003188 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3189 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003190 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003191 }
3192
3193 /* ensure after enumeration our EP0 is active */
3194
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003195 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003196
3197 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003198 dwc2_readl(hsotg, DIEPCTL0),
3199 dwc2_readl(hsotg, DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003200}
3201
3202/**
3203 * kill_all_requests - remove all requests from the endpoint's queue
3204 * @hsotg: The device state.
3205 * @ep: The endpoint the requests may be on.
3206 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003207 *
3208 * Go through the requests on the given endpoint and mark them
3209 * completed with the given result code.
3210 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003211static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003212 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af42014-12-16 11:51:44 +01003213 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003214{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003215 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003216 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003217
Robert Baldyga6b448af42014-12-16 11:51:44 +01003218 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003219
Robert Baldyga6b448af42014-12-16 11:51:44 +01003220 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003221 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003222 result);
Robert Baldyga6b448af42014-12-16 11:51:44 +01003223
Robert Baldygab203d0a2014-09-09 10:44:56 +02003224 if (!hsotg->dedicated_fifos)
3225 return;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003226 size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003227 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003228 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003229}
3230
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003231/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003232 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003233 * @hsotg: The device state.
3234 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003235 * The device has been disconnected. Remove all current
3236 * transactions and signal the gadget driver that this
3237 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003238 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003239void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003240{
John Youn9da51972017-01-17 20:30:27 -08003241 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003242
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003243 if (!hsotg->connected)
3244 return;
3245
3246 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003247 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003248
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003249 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003250 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3251 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003252 kill_all_requests(hsotg, hsotg->eps_in[ep],
3253 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003254 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003255 kill_all_requests(hsotg, hsotg->eps_out[ep],
3256 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003257 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003258
3259 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003260 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003261
3262 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003263}
3264
3265/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003266 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003267 * @hsotg: The device state:
3268 * @periodic: True if this is a periodic FIFO interrupt
3269 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003270static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003271{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003272 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003273 int epno, ret;
3274
3275 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003276 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003277 ep = index_to_ep(hsotg, epno, 1);
3278
3279 if (!ep)
3280 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003281
3282 if (!ep->dir_in)
3283 continue;
3284
3285 if ((periodic && !ep->periodic) ||
3286 (!periodic && ep->periodic))
3287 continue;
3288
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003289 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003290 if (ret < 0)
3291 break;
3292 }
3293}
3294
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003295/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003296#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3297 GINTSTS_PTXFEMP | \
3298 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003299
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003300static int dwc2_hsotg_ep_disable(struct usb_ep *ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003301/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003302 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003303 * @hsotg: The device state
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04003304 * @is_usb_reset: Usb resetting flag
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003305 *
3306 * Issue a soft reset to the core, and await the core finishing it.
3307 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003308void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003309 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003310{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003311 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003312 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003313 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003314 u32 dcfg = 0;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003315 int ep;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003316
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003317 /* Kill any ep0 requests as controller will be reinitialized */
3318 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3319
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003320 if (!is_usb_reset) {
John Stultz6e6360b2017-01-23 14:59:14 -08003321 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003322 return;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003323 } else {
3324 /* all endpoints should be shutdown */
3325 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3326 if (hsotg->eps_in[ep])
3327 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3328 if (hsotg->eps_out[ep])
3329 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3330 }
3331 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003332
3333 /*
3334 * we must now enable ep0 ready for host detection and then
3335 * set configuration.
3336 */
3337
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003338 /* keep other bits untouched (so e.g. forced modes are not lost) */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003339 usbcfg = dwc2_readl(hsotg, GUSBCFG);
Jules Maselbas1e868542019-04-05 15:35:33 +02003340 usbcfg &= ~GUSBCFG_TOUTCAL_MASK;
Jules Maselbas707d80f2019-04-05 15:35:31 +02003341 usbcfg |= GUSBCFG_TOUTCAL(7);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003342
Jules Maselbas1e868542019-04-05 15:35:33 +02003343 /* remove the HNP/SRP and set the PHY */
3344 usbcfg &= ~(GUSBCFG_SRPCAP | GUSBCFG_HNPCAP);
3345 dwc2_writel(hsotg, usbcfg, GUSBCFG);
Jules Maselbas707d80f2019-04-05 15:35:31 +02003346
Jules Maselbas1e868542019-04-05 15:35:33 +02003347 dwc2_phy_init(hsotg, true);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003348
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003349 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003350
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003351 if (!is_usb_reset)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003352 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003353
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003354 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003355
3356 switch (hsotg->params.speed) {
3357 case DWC2_SPEED_PARAM_LOW:
3358 dcfg |= DCFG_DEVSPD_LS;
3359 break;
3360 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003361 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3362 dcfg |= DCFG_DEVSPD_FS48;
3363 else
3364 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003365 break;
3366 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003367 dcfg |= DCFG_DEVSPD_HS;
3368 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003369
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +04003370 if (hsotg->params.ipg_isoc_en)
3371 dcfg |= DCFG_IPG_ISOC_SUPPORDED;
3372
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003373 dwc2_writel(hsotg, dcfg, DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003374
3375 /* Clear any pending OTG interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003376 dwc2_writel(hsotg, 0xffffffff, GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003377
3378 /* Clear any pending interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003379 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003380 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003381 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003382 GINTSTS_USBRST | GINTSTS_RESETDET |
3383 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003384 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3385 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003386
3387 if (!using_desc_dma(hsotg))
3388 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003389
John Youn95832c02017-01-23 14:57:26 -08003390 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003391 intmsk |= GINTSTS_CONIDSTSCHNG;
3392
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003393 dwc2_writel(hsotg, intmsk, GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003394
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003395 if (using_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003396 dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003397 hsotg->params.ahbcfg,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003398 GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003399
3400 /* Set DDMA mode support in the core if needed */
3401 if (using_desc_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003402 dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003403
3404 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003405 dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ?
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003406 (GAHBCFG_NP_TXF_EMP_LVL |
3407 GAHBCFG_P_TXF_EMP_LVL) : 0) |
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003408 GAHBCFG_GLBL_INTR_EN, GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003409 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003410
3411 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003412 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3413 * when we have no data to transfer. Otherwise we get being flooded by
3414 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003415 */
3416
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003417 dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003418 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003419 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003420 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003421 DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003422
3423 /*
3424 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003425 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003426 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003427 dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003428 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003429 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003430 DOEPMSK_SETUPMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003431 DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003432
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003433 /* Enable BNA interrupt for DDMA */
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003434 if (using_desc_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003435 dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK);
3436 dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK);
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003437 }
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003438
Grigor Tovmasyanca531bc2018-08-29 20:59:34 +04003439 /* Enable Service Interval mode if supported */
3440 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3441 dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED);
3442
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003443 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003444
3445 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003446 dwc2_readl(hsotg, DIEPCTL0),
3447 dwc2_readl(hsotg, DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003448
3449 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003450 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003451
3452 /*
3453 * Enable the RXFIFO when in slave mode, as this is how we collect
3454 * the data. In DMA mode, we get events from the FIFO but also
3455 * things we cannot process, so do not use it.
3456 */
3457 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003458 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003459
3460 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003461 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3462 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003463
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003464 if (!is_usb_reset) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003465 dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003466 udelay(10); /* see openiboot */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003467 dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003468 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003469
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003470 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003471
3472 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003473 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003474 * writing to the EPCTL register..
3475 */
3476
3477 /* set to read 1 8byte packet */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003478 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
3479 DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003480
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003481 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003482 DXEPCTL_CNAK | DXEPCTL_EPENA |
3483 DXEPCTL_USBACTEP,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003484 DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003485
3486 /* enable, but don't activate EP0in */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003487 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3488 DXEPCTL_USBACTEP, DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003489
Lukasz Majewski308d7342012-05-04 14:17:05 +02003490 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003491 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3492 if (!is_usb_reset)
3493 val |= DCTL_SFTDISCON;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003494 dwc2_set_bit(hsotg, DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003495
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003496 /* configure the core to support LPM */
3497 dwc2_gadget_init_lpm(hsotg);
3498
Grigor Tovmasyan15d9dbf2018-08-29 21:01:59 +04003499 /* program GREFCLK register if needed */
3500 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3501 dwc2_gadget_program_ref_clk(hsotg);
3502
Lukasz Majewski308d7342012-05-04 14:17:05 +02003503 /* must be at-least 3ms to allow bus to see disconnect */
3504 mdelay(3);
3505
Gregory Herrero065d3932015-09-22 15:16:54 +02003506 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003507
3508 dwc2_hsotg_enqueue_setup(hsotg);
3509
3510 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003511 dwc2_readl(hsotg, DIEPCTL0),
3512 dwc2_readl(hsotg, DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003513}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003514
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003515static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003516{
3517 /* set the soft-disconnect bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003518 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003519}
3520
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003521void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003522{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003523 /* remove the soft-disconnect and let's go */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003524 dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003525}
3526
3527/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003528 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3529 * @hsotg: The device state:
3530 *
3531 * This interrupt indicates one of the following conditions occurred while
3532 * transmitting an ISOC transaction.
3533 * - Corrupted IN Token for ISOC EP.
3534 * - Packet not complete in FIFO.
3535 *
3536 * The following actions will be taken:
3537 * - Determine the EP
3538 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3539 */
3540static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3541{
3542 struct dwc2_hsotg_ep *hs_ep;
3543 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003544 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003545 u32 idx;
3546
3547 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3548
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003549 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003550
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003551 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003552 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003553 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003554 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003555 continue;
3556
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003557 epctrl = dwc2_readl(hsotg, DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003558 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003559 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3560 epctrl |= DXEPCTL_SNAK;
3561 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003562 dwc2_writel(hsotg, epctrl, DIEPCTL(idx));
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003563 }
3564 }
3565
3566 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003567 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003568}
3569
3570/**
3571 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3572 * @hsotg: The device state:
3573 *
3574 * This interrupt indicates one of the following conditions occurred while
3575 * transmitting an ISOC transaction.
3576 * - Corrupted OUT Token for ISOC EP.
3577 * - Packet not complete in FIFO.
3578 *
3579 * The following actions will be taken:
3580 * - Determine the EP
3581 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3582 */
3583static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3584{
3585 u32 gintsts;
3586 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003587 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003588 u32 epctrl;
3589 struct dwc2_hsotg_ep *hs_ep;
3590 int idx;
3591
3592 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3593
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003594 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003595 daintmsk >>= DAINT_OUTEP_SHIFT;
3596
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003597 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003598 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003599 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003600 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003601 continue;
3602
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003603 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003604 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003605 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3606 /* Unmask GOUTNAKEFF interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003607 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003608 gintmsk |= GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003609 dwc2_writel(hsotg, gintmsk, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003610
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003611 gintsts = dwc2_readl(hsotg, GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003612 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003613 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003614 break;
3615 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003616 }
3617 }
3618
3619 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003620 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003621}
3622
3623/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003624 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003625 * @irq: The IRQ number triggered
3626 * @pw: The pw value when registered the handler.
3627 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003628static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003629{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003630 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003631 int retry_count = 8;
3632 u32 gintsts;
3633 u32 gintmsk;
3634
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003635 if (!dwc2_is_device_mode(hsotg))
3636 return IRQ_NONE;
3637
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003638 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003639irq_retry:
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003640 gintsts = dwc2_readl(hsotg, GINTSTS);
3641 gintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003642
3643 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3644 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3645
3646 gintsts &= gintmsk;
3647
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003648 if (gintsts & GINTSTS_RESETDET) {
3649 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3650
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003651 dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003652
3653 /* This event must be used only if controller is suspended */
3654 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003655 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003656 hsotg->lx_state = DWC2_L0;
3657 }
3658 }
3659
3660 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003661 u32 usb_status = dwc2_readl(hsotg, GOTGCTL);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003662 u32 connected = hsotg->connected;
3663
3664 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3665 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003666 dwc2_readl(hsotg, GNPTXSTS));
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003667
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003668 dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003669
3670 /* Report disconnection if it is not already done. */
3671 dwc2_hsotg_disconnect(hsotg);
3672
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003673 /* Reset device address to zero */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003674 dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003675
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003676 if (usb_status & GOTGCTL_BSESVLD && connected)
3677 dwc2_hsotg_core_init_disconnected(hsotg, true);
3678 }
3679
Dinh Nguyen47a16852014-04-14 14:13:34 -07003680 if (gintsts & GINTSTS_ENUMDONE) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003681 dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003682
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003683 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003684 }
3685
Dinh Nguyen47a16852014-04-14 14:13:34 -07003686 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003687 u32 daint = dwc2_readl(hsotg, DAINT);
3688 u32 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003689 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003690 int ep;
3691
Robert Baldyga7e804652013-09-19 11:50:20 +02003692 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003693 daint_out = daint >> DAINT_OUTEP_SHIFT;
3694 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003695
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003696 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3697
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003698 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3699 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003700 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003701 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003702 }
3703
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003704 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3705 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003706 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003707 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003708 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003709 }
3710
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003711 /* check both FIFOs */
3712
Dinh Nguyen47a16852014-04-14 14:13:34 -07003713 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003714 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3715
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003716 /*
3717 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003718 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003719 * it needs re-enabling
3720 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003721
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003722 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3723 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003724 }
3725
Dinh Nguyen47a16852014-04-14 14:13:34 -07003726 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003727 dev_dbg(hsotg->dev, "PTxFEmp\n");
3728
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003729 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003730
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003731 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3732 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003733 }
3734
Dinh Nguyen47a16852014-04-14 14:13:34 -07003735 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003736 /*
3737 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003738 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003739 * set.
3740 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003741
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003742 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003743 }
3744
Dinh Nguyen47a16852014-04-14 14:13:34 -07003745 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003746 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003747 dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003748 }
3749
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003750 /*
3751 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003752 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003753 * the occurrence.
3754 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003755
Dinh Nguyen47a16852014-04-14 14:13:34 -07003756 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003757 u8 idx;
3758 u32 epctrl;
3759 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003760 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003761 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003762
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003763 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyand8484552018-01-19 14:41:42 +04003764 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003765 /* Mask this interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003766 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003767 gintmsk &= ~GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003768 dwc2_writel(hsotg, gintmsk, GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003769
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003770 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003771 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003772 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003773 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003774 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyand8484552018-01-19 14:41:42 +04003775 continue;
3776
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003777 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003778
Razmik Karapetyand8484552018-01-19 14:41:42 +04003779 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003780 epctrl |= DXEPCTL_SNAK;
3781 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003782 dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003783 }
3784 }
3785
3786 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003787 }
3788
Dinh Nguyen47a16852014-04-14 14:13:34 -07003789 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003790 dev_info(hsotg->dev, "GINNakEff triggered\n");
3791
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003792 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003793
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003794 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003795 }
3796
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003797 if (gintsts & GINTSTS_INCOMPL_SOIN)
3798 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003799
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003800 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3801 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003802
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003803 /*
3804 * if we've had fifo events, we should try and go around the
3805 * loop again to see if there's any point in returning yet.
3806 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003807
3808 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003809 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003810
Grigor Tovmasyan187c5292018-08-29 21:02:57 +04003811 /* Check WKUP_ALERT interrupt*/
3812 if (hsotg->params.service_interval)
3813 dwc2_gadget_wkup_alert_handler(hsotg);
3814
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003815 spin_unlock(&hsotg->lock);
3816
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003817 return IRQ_HANDLED;
3818}
3819
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003820static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3821 struct dwc2_hsotg_ep *hs_ep)
3822{
3823 u32 epctrl_reg;
3824 u32 epint_reg;
3825
3826 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3827 DOEPCTL(hs_ep->index);
3828 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3829 DOEPINT(hs_ep->index);
3830
3831 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3832 hs_ep->name);
3833
3834 if (hs_ep->dir_in) {
3835 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003836 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003837 /* Wait for Nak effect */
3838 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3839 DXEPINT_INEPNAKEFF, 100))
3840 dev_warn(hsotg->dev,
3841 "%s: timeout DIEPINT.NAKEFF\n",
3842 __func__);
3843 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003844 dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003845 /* Wait for Nak effect */
3846 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3847 GINTSTS_GINNAKEFF, 100))
3848 dev_warn(hsotg->dev,
3849 "%s: timeout GINTSTS.GINNAKEFF\n",
3850 __func__);
3851 }
3852 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003853 if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF))
3854 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003855
3856 /* Wait for global nak to take effect */
3857 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3858 GINTSTS_GOUTNAKEFF, 100))
3859 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3860 __func__);
3861 }
3862
3863 /* Disable ep */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003864 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003865
3866 /* Wait for ep to be disabled */
3867 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3868 dev_warn(hsotg->dev,
3869 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3870
3871 /* Clear EPDISBLD interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003872 dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003873
3874 if (hs_ep->dir_in) {
3875 unsigned short fifo_index;
3876
3877 if (hsotg->dedicated_fifos || hs_ep->periodic)
3878 fifo_index = hs_ep->fifo_index;
3879 else
3880 fifo_index = 0;
3881
3882 /* Flush TX FIFO */
3883 dwc2_flush_tx_fifo(hsotg, fifo_index);
3884
3885 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3886 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003887 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003888
3889 } else {
3890 /* Remove global NAKs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003891 dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003892 }
3893}
3894
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003895/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003896 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003897 * @ep: The USB endpint to configure
3898 * @desc: The USB endpoint descriptor to configure with.
3899 *
3900 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003901 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003902static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003903 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003904{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003905 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003906 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003907 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003908 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003909 u32 epctrl_reg;
3910 u32 epctrl;
3911 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003912 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003913 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003914 unsigned int dir_in;
3915 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003916 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003917 unsigned char ep_type;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003918
3919 dev_dbg(hsotg->dev,
3920 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3921 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3922 desc->wMaxPacketSize, desc->bInterval);
3923
3924 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003925 if (index == 0) {
3926 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3927 return -EINVAL;
3928 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003929
3930 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3931 if (dir_in != hs_ep->dir_in) {
3932 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3933 return -EINVAL;
3934 }
3935
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003936 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003937 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003938 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003939
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003940 /* ISOC IN in DDMA supported bInterval up to 10 */
3941 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3942 dir_in && desc->bInterval > 10) {
3943 dev_err(hsotg->dev,
3944 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3945 return -EINVAL;
3946 }
3947
3948 /* High bandwidth ISOC OUT in DDMA not supported */
3949 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3950 !dir_in && mc > 1) {
3951 dev_err(hsotg->dev,
3952 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3953 return -EINVAL;
3954 }
3955
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003956 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003957
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003958 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003959 epctrl = dwc2_readl(hsotg, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003960
3961 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3962 __func__, epctrl, epctrl_reg);
3963
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003964 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003965 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3966 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003967 MAX_DMA_DESC_NUM_GENERIC *
3968 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003969 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003970 if (!hs_ep->desc_list) {
3971 ret = -ENOMEM;
3972 goto error2;
3973 }
3974 }
3975
Lukasz Majewski22258f42012-06-14 10:02:24 +02003976 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003977
Dinh Nguyen47a16852014-04-14 14:13:34 -07003978 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3979 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003980
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003981 /*
3982 * mark the endpoint as active, otherwise the core may ignore
3983 * transactions entirely for this endpoint
3984 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003985 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003986
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003987 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003988 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003989
3990 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003991 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003992 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003993 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003994 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003995
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003996 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003997 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003998 epctrl |= DXEPCTL_EPTYPE_ISO;
3999 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02004000 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07004001 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004002 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08004003 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04004004 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004005 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02004006 hs_ep->periodic = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004007 mask = dwc2_readl(hsotg, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004008 mask |= DIEPMSK_NAKMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004009 dwc2_writel(hsotg, mask, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004010 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004011 mask = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004012 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004013 dwc2_writel(hsotg, mask, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004014 }
Robert Baldyga1479e842013-10-09 08:41:57 +02004015 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004016
4017 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07004018 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004019 break;
4020
4021 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02004022 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004023 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004024
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07004025 if (hsotg->gadget.speed == USB_SPEED_HIGH)
4026 hs_ep->interval = 1 << (desc->bInterval - 1);
4027
Dinh Nguyen47a16852014-04-14 14:13:34 -07004028 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004029 break;
4030
4031 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07004032 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004033 break;
4034 }
4035
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004036 /*
4037 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01004038 * a unique tx-fifo even if it is non-periodic.
4039 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07004040 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004041 u32 fifo_index = 0;
4042 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08004043
4044 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01004045 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08004046 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02004047 continue;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004048 val = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08004049 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02004050 if (val < size)
4051 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004052 /* Search for smallest acceptable fifo */
4053 if (val < fifo_size) {
4054 fifo_size = val;
4055 fifo_index = i;
4056 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02004057 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004058 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01004059 dev_err(hsotg->dev,
4060 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05304061 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004062 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05304063 }
Minas Harutyunyan97311c82019-01-31 18:28:07 +04004064 epctrl &= ~(DXEPCTL_TXFNUM_LIMIT << DXEPCTL_TXFNUM_SHIFT);
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004065 hsotg->fifo_map |= 1 << fifo_index;
4066 epctrl |= DXEPCTL_TXFNUM(fifo_index);
4067 hs_ep->fifo_index = fifo_index;
4068 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02004069 }
Ben Dooks10aebc72010-07-19 09:40:44 +01004070
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004071 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004072 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07004073 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004074
Artur Petrosyan52953222018-04-16 08:45:31 -04004075 /* WA for Full speed ISOC IN in DDMA mode.
4076 * By Clear NAK status of EP, core will send ZLP
4077 * to IN token and assert NAK interrupt relying
4078 * on TxFIFO status only
4079 */
4080
4081 if (hsotg->gadget.speed == USB_SPEED_FULL &&
4082 hs_ep->isochronous && dir_in) {
4083 /* The WA applies only to core versions from 2.72a
4084 * to 4.00a (including both). Also for FS_IOT_1.00a
4085 * and HS_IOT_1.00a.
4086 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004087 u32 gsnpsid = dwc2_readl(hsotg, GSNPSID);
Artur Petrosyan52953222018-04-16 08:45:31 -04004088
4089 if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
4090 gsnpsid <= DWC2_CORE_REV_4_00a) ||
4091 gsnpsid == DWC2_FS_IOT_REV_1_00a ||
4092 gsnpsid == DWC2_HS_IOT_REV_1_00a)
4093 epctrl |= DXEPCTL_CNAK;
4094 }
4095
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004096 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
4097 __func__, epctrl);
4098
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004099 dwc2_writel(hsotg, epctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004100 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004101 __func__, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004102
4103 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004104 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004105
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004106error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02004107 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004108
4109error2:
4110 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08004111 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004112 sizeof(struct dwc2_dma_desc),
4113 hs_ep->desc_list, hs_ep->desc_list_dma);
4114 hs_ep->desc_list = NULL;
4115 }
4116
Julia Lawall19c190f2010-03-29 17:36:44 +02004117 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004118}
4119
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004120/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004121 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004122 * @ep: The endpoint to disable.
4123 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004124static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004125{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004126 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004127 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004128 int dir_in = hs_ep->dir_in;
4129 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004130 u32 epctrl_reg;
4131 u32 ctrl;
4132
Marek Szyprowski1e011292014-09-09 10:44:54 +02004133 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004134
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004135 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004136 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4137 return -EINVAL;
4138 }
4139
John Stultz9b4810922017-10-23 14:32:49 -07004140 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4141 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4142 return -EINVAL;
4143 }
4144
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02004145 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004146
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004147 ctrl = dwc2_readl(hsotg, epctrl_reg);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08004148
4149 if (ctrl & DXEPCTL_EPENA)
4150 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4151
Dinh Nguyen47a16852014-04-14 14:13:34 -07004152 ctrl &= ~DXEPCTL_EPENA;
4153 ctrl &= ~DXEPCTL_USBACTEP;
4154 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004155
4156 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004157 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004158
4159 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004160 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004161
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01004162 /* terminate all requests with shutdown */
4163 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4164
Robert Baldyga1c07b202016-08-29 13:39:00 -07004165 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4166 hs_ep->fifo_index = 0;
4167 hs_ep->fifo_size = 0;
4168
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004169 return 0;
4170}
4171
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004172static int dwc2_hsotg_ep_disable_lock(struct usb_ep *ep)
4173{
4174 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4175 struct dwc2_hsotg *hsotg = hs_ep->parent;
4176 unsigned long flags;
4177 int ret;
4178
4179 spin_lock_irqsave(&hsotg->lock, flags);
4180 ret = dwc2_hsotg_ep_disable(ep);
4181 spin_unlock_irqrestore(&hsotg->lock, flags);
4182 return ret;
4183}
4184
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004185/**
4186 * on_list - check request is on the given endpoint
4187 * @ep: The endpoint to check.
4188 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004189 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004190static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004191{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004192 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004193
4194 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4195 if (req == test)
4196 return true;
4197 }
4198
4199 return false;
4200}
4201
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004202/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004203 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004204 * @ep: The endpoint to dequeue.
4205 * @req: The request to be removed from a queue.
4206 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004207static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004208{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004209 struct dwc2_hsotg_req *hs_req = our_req(req);
4210 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004211 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004212 unsigned long flags;
4213
Marek Szyprowski1e011292014-09-09 10:44:54 +02004214 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004215
Lukasz Majewski22258f42012-06-14 10:02:24 +02004216 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004217
4218 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004219 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004220 return -EINVAL;
4221 }
4222
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004223 /* Dequeue already started request */
4224 if (req == &hs_ep->req->req)
4225 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4226
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004227 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004228 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004229
4230 return 0;
4231}
4232
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004233/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004234 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004235 * @ep: The endpoint to set halt.
4236 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004237 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4238 * the endpoint is busy processing requests.
4239 *
4240 * We need to stall the endpoint immediately if request comes from set_feature
4241 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004242 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004243static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004244{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004245 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004246 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004247 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004248 u32 epreg;
4249 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004250 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004251
4252 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4253
Robert Baldygac9f721b2014-01-14 08:36:00 +01004254 if (index == 0) {
4255 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004256 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004257 else
4258 dev_warn(hs->dev,
4259 "%s: can't clear halt on ep0\n", __func__);
4260 return 0;
4261 }
4262
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004263 if (hs_ep->isochronous) {
4264 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4265 return -EINVAL;
4266 }
4267
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004268 if (!now && value && !list_empty(&hs_ep->queue)) {
4269 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4270 ep->name);
4271 return -EAGAIN;
4272 }
4273
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004274 if (hs_ep->dir_in) {
4275 epreg = DIEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004276 epctl = dwc2_readl(hs, epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004277
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004278 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004279 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004280 if (epctl & DXEPCTL_EPENA)
4281 epctl |= DXEPCTL_EPDIS;
4282 } else {
4283 epctl &= ~DXEPCTL_STALL;
4284 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4285 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004286 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004287 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004288 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004289 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004290 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004291 epreg = DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004292 epctl = dwc2_readl(hs, epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004293
John Youn34c0887f2017-01-17 20:31:43 -08004294 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004295 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004296 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004297 epctl &= ~DXEPCTL_STALL;
4298 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4299 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004300 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004301 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004302 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004303 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004304 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004305
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004306 hs_ep->halted = value;
4307
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004308 return 0;
4309}
4310
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004311/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004312 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004313 * @ep: The endpoint to set halt.
4314 * @value: Set or unset the halt.
4315 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004316static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004317{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004318 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004319 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004320 unsigned long flags = 0;
4321 int ret = 0;
4322
4323 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004324 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004325 spin_unlock_irqrestore(&hs->lock, flags);
4326
4327 return ret;
4328}
4329
Bhumika Goyalebce5612017-08-12 17:34:55 +05304330static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004331 .enable = dwc2_hsotg_ep_enable,
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004332 .disable = dwc2_hsotg_ep_disable_lock,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004333 .alloc_request = dwc2_hsotg_ep_alloc_request,
4334 .free_request = dwc2_hsotg_ep_free_request,
4335 .queue = dwc2_hsotg_ep_queue_lock,
4336 .dequeue = dwc2_hsotg_ep_dequeue,
4337 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004338 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004339};
4340
4341/**
John Youn9da51972017-01-17 20:30:27 -08004342 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004343 * @hsotg: The driver state
4344 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004345static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004346{
4347 /* unmask subset of endpoint interrupts */
4348
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004349 dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004350 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004351 DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004352
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004353 dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004354 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004355 DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004356
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004357 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004358
4359 /* Be in disconnected state until gadget is registered */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004360 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004361
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004362 /* setup fifos */
4363
4364 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004365 dwc2_readl(hsotg, GRXFSIZ),
4366 dwc2_readl(hsotg, GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004367
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004368 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004369
Gregory Herrerof5090042015-01-09 13:38:47 +01004370 if (using_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004371 dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004372}
4373
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004374/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004375 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004376 * @gadget: The usb gadget state
4377 * @driver: The usb gadget driver
4378 *
4379 * Perform initialization to prepare udc device and driver
4380 * to work.
4381 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004382static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004383 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004384{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004385 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004386 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004387 int ret;
4388
4389 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004390 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004391 return -ENODEV;
4392 }
4393
4394 if (!driver) {
4395 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4396 return -EINVAL;
4397 }
4398
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004399 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004400 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004401
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004402 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004403 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4404 return -EINVAL;
4405 }
4406
4407 WARN_ON(hsotg->driver);
4408
4409 driver->driver.bus = NULL;
4410 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004411 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004412 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4413
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004414 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4415 ret = dwc2_lowlevel_hw_enable(hsotg);
4416 if (ret)
4417 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004418 }
4419
Gregory Herrerof6c01592015-01-09 13:38:41 +01004420 if (!IS_ERR_OR_NULL(hsotg->uphy))
4421 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004422
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004423 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004424 if (dwc2_hw_is_device(hsotg)) {
4425 dwc2_hsotg_init(hsotg);
4426 dwc2_hsotg_core_init_disconnected(hsotg, false);
4427 }
4428
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004429 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004430 spin_unlock_irqrestore(&hsotg->lock, flags);
4431
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01004432 gadget->sg_supported = using_desc_dma(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004433 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004434
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004435 return 0;
4436
4437err:
4438 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004439 return ret;
4440}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004441
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004442/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004443 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004444 * @gadget: The usb gadget state
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004445 *
4446 * Stop udc hw block and stay tunned for future transmissions
4447 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004448static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004449{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004450 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004451 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004452 int ep;
4453
4454 if (!hsotg)
4455 return -ENODEV;
4456
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004457 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004458 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4459 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004460 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004461 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004462 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004463 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004464
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004465 spin_lock_irqsave(&hsotg->lock, flags);
4466
Marek Szyprowski32805c32014-10-20 12:45:33 +02004467 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004468 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004469 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004470
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004471 spin_unlock_irqrestore(&hsotg->lock, flags);
4472
Gregory Herrerof6c01592015-01-09 13:38:41 +01004473 if (!IS_ERR_OR_NULL(hsotg->uphy))
4474 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004475
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004476 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4477 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004478
4479 return 0;
4480}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004481
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004482/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004483 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004484 * @gadget: The usb gadget state
4485 *
4486 * Read the {micro} frame number
4487 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004488static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004489{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004490 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004491}
4492
Lukasz Majewskia188b682012-06-22 09:29:56 +02004493/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004494 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004495 * @gadget: The usb gadget state
4496 * @is_on: Current state of the USB PHY
4497 *
4498 * Connect/Disconnect the USB PHY pullup
4499 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004500static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004501{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004502 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004503 unsigned long flags = 0;
4504
Gregory Herrero77ba9112015-09-29 12:08:19 +02004505 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004506 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004507
4508 /* Don't modify pullup state while in host mode */
4509 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4510 hsotg->enabled = is_on;
4511 return 0;
4512 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004513
4514 spin_lock_irqsave(&hsotg->lock, flags);
4515 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004516 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004517 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004518 /* Enable ACG feature in device mode,if supported */
4519 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004520 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004521 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004522 dwc2_hsotg_core_disconnect(hsotg);
4523 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004524 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004525 }
4526
4527 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4528 spin_unlock_irqrestore(&hsotg->lock, flags);
4529
4530 return 0;
4531}
4532
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004533static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004534{
4535 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4536 unsigned long flags;
4537
4538 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4539 spin_lock_irqsave(&hsotg->lock, flags);
4540
Gregory Herrero61f72232015-09-29 12:08:28 +02004541 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004542 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004543 * before being initialized / de-initialized
4544 */
4545 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004546 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004547
Gregory Herrero83d98222015-01-09 13:39:02 +01004548 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004549 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004550
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004551 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004552 if (hsotg->enabled) {
4553 /* Enable ACG feature in device mode,if supported */
4554 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004555 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004556 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004557 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004558 dwc2_hsotg_core_disconnect(hsotg);
4559 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004560 }
4561
4562 spin_unlock_irqrestore(&hsotg->lock, flags);
4563 return 0;
4564}
4565
Gregory Herrero596d6962015-01-09 13:39:08 +01004566/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004567 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004568 * @gadget: The usb gadget state
4569 * @mA: Amount of current
4570 *
4571 * Report how much power the device may consume to the phy.
4572 */
John Youn9da51972017-01-17 20:30:27 -08004573static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004574{
4575 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4576
4577 if (IS_ERR_OR_NULL(hsotg->uphy))
4578 return -ENOTSUPP;
4579 return usb_phy_set_power(hsotg->uphy, mA);
4580}
4581
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004582static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4583 .get_frame = dwc2_hsotg_gadget_getframe,
4584 .udc_start = dwc2_hsotg_udc_start,
4585 .udc_stop = dwc2_hsotg_udc_stop,
4586 .pullup = dwc2_hsotg_pullup,
4587 .vbus_session = dwc2_hsotg_vbus_session,
4588 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004589};
4590
4591/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004592 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004593 * @hsotg: The device state.
4594 * @hs_ep: The endpoint to be initialised.
4595 * @epnum: The endpoint number
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004596 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004597 *
4598 * Initialise the given endpoint (as part of the probe and device state
4599 * creation) to give to the gadget driver. Setup the endpoint name, any
4600 * direction information and other state that may be required.
4601 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004602static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004603 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004604 int epnum,
4605 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004606{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004607 char *dir;
4608
4609 if (epnum == 0)
4610 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004611 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004612 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004613 else
4614 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004615
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004616 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004617 hs_ep->index = epnum;
4618
4619 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4620
4621 INIT_LIST_HEAD(&hs_ep->queue);
4622 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4623
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004624 /* add to the list of endpoints known by the gadget driver */
4625 if (epnum)
4626 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4627
4628 hs_ep->parent = hsotg;
4629 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004630
4631 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4632 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4633 else
4634 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4635 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004636 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004637
Robert Baldyga29545222015-07-31 16:00:18 +02004638 if (epnum == 0) {
4639 hs_ep->ep.caps.type_control = true;
4640 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004641 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4642 hs_ep->ep.caps.type_iso = true;
4643 hs_ep->ep.caps.type_bulk = true;
4644 }
Robert Baldyga29545222015-07-31 16:00:18 +02004645 hs_ep->ep.caps.type_int = true;
4646 }
4647
4648 if (dir_in)
4649 hs_ep->ep.caps.dir_in = true;
4650 else
4651 hs_ep->ep.caps.dir_out = true;
4652
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004653 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004654 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004655 * to be something valid.
4656 */
4657
4658 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004659 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004660
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004661 if (dir_in)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004662 dwc2_writel(hsotg, next, DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004663 else
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004664 dwc2_writel(hsotg, next, DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004665 }
4666}
4667
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004668/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004669 * dwc2_hsotg_hw_cfg - read HW configuration registers
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004670 * @hsotg: Programming view of the DWC_otg controller
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004671 *
4672 * Read the USB core HW configuration registers
4673 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004674static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004675{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004676 u32 cfg;
4677 u32 ep_type;
4678 u32 i;
4679
Ben Dooks10aebc72010-07-19 09:40:44 +01004680 /* check hardware configuration */
4681
John Youn43e90342015-12-17 11:17:45 -08004682 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4683
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004684 /* Add ep0 */
4685 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004686
John Younb98866c2017-01-17 20:31:58 -08004687 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4688 sizeof(struct dwc2_hsotg_ep),
4689 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004690 if (!hsotg->eps_in[0])
4691 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004692 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004693 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004694
John Youn43e90342015-12-17 11:17:45 -08004695 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004696 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004697 ep_type = cfg & 3;
4698 /* Direction in or both */
4699 if (!(ep_type & 2)) {
4700 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004701 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004702 if (!hsotg->eps_in[i])
4703 return -ENOMEM;
4704 }
4705 /* Direction out or both */
4706 if (!(ep_type & 1)) {
4707 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004708 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004709 if (!hsotg->eps_out[i])
4710 return -ENOMEM;
4711 }
4712 }
4713
John Youn43e90342015-12-17 11:17:45 -08004714 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4715 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004716
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004717 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4718 hsotg->num_of_eps,
4719 hsotg->dedicated_fifos ? "dedicated" : "shared",
4720 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004721 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004722}
4723
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004724/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004725 * dwc2_hsotg_dump - dump state of the udc
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004726 * @hsotg: Programming view of the DWC_otg controller
4727 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004728 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004729static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004730{
Mark Brown83a01802011-06-01 17:16:15 +01004731#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004732 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004733 u32 val;
4734 int idx;
4735
4736 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004737 dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL),
4738 dwc2_readl(hsotg, DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004739
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004740 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004741 dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004742
4743 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004744 dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004745
4746 /* show periodic fifo settings */
4747
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004748 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004749 val = dwc2_readl(hsotg, DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004750 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004751 val >> FIFOSIZE_DEPTH_SHIFT,
4752 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004753 }
4754
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004755 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004756 dev_info(dev,
4757 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004758 dwc2_readl(hsotg, DIEPCTL(idx)),
4759 dwc2_readl(hsotg, DIEPTSIZ(idx)),
4760 dwc2_readl(hsotg, DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004761
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004762 val = dwc2_readl(hsotg, DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004763 dev_info(dev,
4764 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004765 idx, dwc2_readl(hsotg, DOEPCTL(idx)),
4766 dwc2_readl(hsotg, DOEPTSIZ(idx)),
4767 dwc2_readl(hsotg, DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004768 }
4769
4770 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004771 dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004772#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004773}
4774
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004775/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004776 * dwc2_gadget_init - init function for gadget
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004777 * @hsotg: Programming view of the DWC_otg controller
4778 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004779 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004780int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004781{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004782 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004783 int epnum;
4784 int ret;
John Youn43e90342015-12-17 11:17:45 -08004785
Gregory Herrero0a176272015-01-09 13:38:52 +01004786 /* Dump fifo information */
4787 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004788 hsotg->params.g_np_tx_fifo_size);
4789 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004790
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004791 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004792 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004793 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004794 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004795
4796 if (hsotg->params.lpm)
4797 hsotg->gadget.lpm_capable = true;
4798
Gregory Herrero097ee662015-04-29 22:09:10 +02004799 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4800 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004801 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4802 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004803
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004804 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004805 if (ret) {
4806 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004807 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004808 }
4809
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004810 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4811 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004812 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004813 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004814
4815 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4816 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004817 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004818 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004819
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004820 if (using_desc_dma(hsotg)) {
4821 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4822 if (ret < 0)
4823 return ret;
4824 }
4825
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004826 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4827 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004828 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004829 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004830 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004831 }
4832
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004833 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4834
4835 if (hsotg->num_of_eps == 0) {
4836 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004837 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004838 }
4839
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004840 /* setup endpoint information */
4841
4842 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004843 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004844
4845 /* allocate EP0 request */
4846
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004847 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004848 GFP_KERNEL);
4849 if (!hsotg->ctrl_req) {
4850 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004851 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004852 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004853
4854 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004855 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4856 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004857 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004858 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004859 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004860 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004861 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004862 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004863
Dinh Nguyen117777b2014-11-11 11:13:34 -06004864 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004865 if (ret) {
4866 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep,
4867 hsotg->ctrl_req);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004868 return ret;
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004869 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004870 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004871
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004872 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004873}
4874
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004875/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004876 * dwc2_hsotg_remove - remove function for hsotg driver
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004877 * @hsotg: Programming view of the DWC_otg controller
4878 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004879 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004880int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004881{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004882 usb_del_gadget_udc(&hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004883 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004884
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004885 return 0;
4886}
4887
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004888int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004889{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004890 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004891
Gregory Herrero9e779772015-04-29 22:09:07 +02004892 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004893 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004894
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004895 if (hsotg->driver) {
4896 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004897
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004898 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4899 hsotg->driver->driver.name);
4900
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004901 spin_lock_irqsave(&hsotg->lock, flags);
4902 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004903 dwc2_hsotg_core_disconnect(hsotg);
4904 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004905 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4906 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004907
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004908 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4909 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004910 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004911 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004912 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004913 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004914 }
4915
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004916 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004917}
4918
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004919int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004920{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004921 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004922
Gregory Herrero9e779772015-04-29 22:09:07 +02004923 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004924 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004925
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004926 if (hsotg->driver) {
4927 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4928 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004929
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004930 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004931 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004932 if (hsotg->enabled) {
4933 /* Enable ACG feature in device mode,if supported */
4934 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004935 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004936 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004937 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004938 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004939
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004940 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004941}
John Youn58e52ff6a2016-02-23 19:54:57 -08004942
4943/**
4944 * dwc2_backup_device_registers() - Backup controller device registers.
4945 * When suspending usb bus, registers needs to be backuped
4946 * if controller power is disabled once suspended.
4947 *
4948 * @hsotg: Programming view of the DWC_otg controller
4949 */
4950int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4951{
4952 struct dwc2_dregs_backup *dr;
4953 int i;
4954
4955 dev_dbg(hsotg->dev, "%s\n", __func__);
4956
4957 /* Backup dev regs */
4958 dr = &hsotg->dr_backup;
4959
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004960 dr->dcfg = dwc2_readl(hsotg, DCFG);
4961 dr->dctl = dwc2_readl(hsotg, DCTL);
4962 dr->daintmsk = dwc2_readl(hsotg, DAINTMSK);
4963 dr->diepmsk = dwc2_readl(hsotg, DIEPMSK);
4964 dr->doepmsk = dwc2_readl(hsotg, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08004965
4966 for (i = 0; i < hsotg->num_of_eps; i++) {
4967 /* Backup IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004968 dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004969
4970 /* Ensure DATA PID is correctly configured */
4971 if (dr->diepctl[i] & DXEPCTL_DPID)
4972 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4973 else
4974 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4975
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004976 dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i));
4977 dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004978
4979 /* Backup OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004980 dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004981
4982 /* Ensure DATA PID is correctly configured */
4983 if (dr->doepctl[i] & DXEPCTL_DPID)
4984 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4985 else
4986 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4987
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004988 dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i));
4989 dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i));
4990 dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004991 }
4992 dr->valid = true;
4993 return 0;
4994}
4995
4996/**
4997 * dwc2_restore_device_registers() - Restore controller device registers.
4998 * When resuming usb bus, device registers needs to be restored
4999 * if controller power were disabled.
5000 *
5001 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005002 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
5003 *
5004 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08005005 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005006int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08005007{
5008 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08005009 int i;
5010
5011 dev_dbg(hsotg->dev, "%s\n", __func__);
5012
5013 /* Restore dev regs */
5014 dr = &hsotg->dr_backup;
5015 if (!dr->valid) {
5016 dev_err(hsotg->dev, "%s: no device registers to restore\n",
5017 __func__);
5018 return -EINVAL;
5019 }
5020 dr->valid = false;
5021
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005022 if (!remote_wakeup)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005023 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005024
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005025 dwc2_writel(hsotg, dr->daintmsk, DAINTMSK);
5026 dwc2_writel(hsotg, dr->diepmsk, DIEPMSK);
5027 dwc2_writel(hsotg, dr->doepmsk, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08005028
5029 for (i = 0; i < hsotg->num_of_eps; i++) {
5030 /* Restore IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005031 dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i));
5032 dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i));
5033 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005034 /** WA for enabled EPx's IN in DDMA mode. On entering to
5035 * hibernation wrong value read and saved from DIEPDMAx,
5036 * as result BNA interrupt asserted on hibernation exit
5037 * by restoring from saved area.
5038 */
5039 if (hsotg->params.g_dma_desc &&
5040 (dr->diepctl[i] & DXEPCTL_EPENA))
5041 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005042 dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i));
5043 dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005044 /* Restore OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005045 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005046 /* WA for enabled EPx's OUT in DDMA mode. On entering to
5047 * hibernation wrong value read and saved from DOEPDMAx,
5048 * as result BNA interrupt asserted on hibernation exit
5049 * by restoring from saved area.
5050 */
5051 if (hsotg->params.g_dma_desc &&
5052 (dr->doepctl[i] & DXEPCTL_EPENA))
5053 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005054 dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i));
5055 dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08005056 }
5057
John Youn58e52ff6a2016-02-23 19:54:57 -08005058 return 0;
5059}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04005060
5061/**
5062 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
5063 *
5064 * @hsotg: Programming view of DWC_otg controller
5065 *
5066 */
5067void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
5068{
5069 u32 val;
5070
5071 if (!hsotg->params.lpm)
5072 return;
5073
5074 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
5075 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
5076 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
5077 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
5078 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
Minas Harutyunyan46637562019-04-18 15:40:43 +04005079 val |= GLPMCFG_LPM_REJECT_CTRL_CONTROL;
Artur Petrosyan9aed8c02018-11-02 11:29:55 -04005080 val |= GLPMCFG_LPM_ACCEPT_CTRL_ISOC;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005081 dwc2_writel(hsotg, val, GLPMCFG);
5082 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG));
Grigor Tovmasyan4abe4532018-08-29 21:02:28 +04005083
5084 /* Unmask WKUP_ALERT Interrupt */
5085 if (hsotg->params.service_interval)
5086 dwc2_set_bit(hsotg, GINTMSK2, GINTMSK2_WKUP_ALERT_INT_MSK);
Sevak Arakelyan21b03402018-01-24 17:43:32 +04005087}
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005088
5089/**
Grigor Tovmasyan15d9dbf2018-08-29 21:01:59 +04005090 * dwc2_gadget_program_ref_clk - Program GREFCLK register in device mode
5091 *
5092 * @hsotg: Programming view of DWC_otg controller
5093 *
5094 */
5095void dwc2_gadget_program_ref_clk(struct dwc2_hsotg *hsotg)
5096{
5097 u32 val = 0;
5098
5099 val |= GREFCLK_REF_CLK_MODE;
5100 val |= hsotg->params.ref_clk_per << GREFCLK_REFCLKPER_SHIFT;
5101 val |= hsotg->params.sof_cnt_wkup_alert <<
5102 GREFCLK_SOF_CNT_WKUP_ALERT_SHIFT;
5103
5104 dwc2_writel(hsotg, val, GREFCLK);
5105 dev_dbg(hsotg->dev, "GREFCLK=0x%08x\n", dwc2_readl(hsotg, GREFCLK));
5106}
5107
5108/**
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005109 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
5110 *
5111 * @hsotg: Programming view of the DWC_otg controller
5112 *
5113 * Return non-zero if failed to enter to hibernation.
5114 */
5115int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
5116{
5117 u32 gpwrdn;
5118 int ret = 0;
5119
5120 /* Change to L2(suspend) state */
5121 hsotg->lx_state = DWC2_L2;
5122 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
5123 ret = dwc2_backup_global_registers(hsotg);
5124 if (ret) {
5125 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5126 __func__);
5127 return ret;
5128 }
5129 ret = dwc2_backup_device_registers(hsotg);
5130 if (ret) {
5131 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
5132 __func__);
5133 return ret;
5134 }
5135
5136 gpwrdn = GPWRDN_PWRDNRSTN;
5137 gpwrdn |= GPWRDN_PMUACTV;
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 /* Set flag to indicate that we are in hibernation */
5142 hsotg->hibernated = 1;
5143
5144 /* Enable interrupts from wake up logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005145 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005146 gpwrdn |= GPWRDN_PMUINTSEL;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005147 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005148 udelay(10);
5149
5150 /* Unmask device mode interrupts in GPWRDN */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005151 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005152 gpwrdn |= GPWRDN_RST_DET_MSK;
5153 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5154 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005155 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005156 udelay(10);
5157
5158 /* Enable Power Down Clamp */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005159 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005160 gpwrdn |= GPWRDN_PWRDNCLMP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005161 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005162 udelay(10);
5163
5164 /* Switch off VDD */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005165 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005166 gpwrdn |= GPWRDN_PWRDNSWTCH;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005167 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005168 udelay(10);
5169
5170 /* Save gpwrdn register for further usage if stschng interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005171 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005172 dev_dbg(hsotg->dev, "Hibernation completed\n");
5173
5174 return ret;
5175}
5176
5177/**
5178 * dwc2_gadget_exit_hibernation()
5179 * This function is for exiting from Device mode hibernation by host initiated
5180 * resume/reset and device initiated remote-wakeup.
5181 *
5182 * @hsotg: Programming view of the DWC_otg controller
5183 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04005184 * @reset: indicates whether resume is initiated by Reset.
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005185 *
5186 * Return non-zero if failed to exit from hibernation.
5187 */
5188int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
5189 int rem_wakeup, int reset)
5190{
5191 u32 pcgcctl;
5192 u32 gpwrdn;
5193 u32 dctl;
5194 int ret = 0;
5195 struct dwc2_gregs_backup *gr;
5196 struct dwc2_dregs_backup *dr;
5197
5198 gr = &hsotg->gr_backup;
5199 dr = &hsotg->dr_backup;
5200
5201 if (!hsotg->hibernated) {
5202 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5203 return 1;
5204 }
5205 dev_dbg(hsotg->dev,
5206 "%s: called with rem_wakeup = %d reset = %d\n",
5207 __func__, rem_wakeup, reset);
5208
5209 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5210
5211 if (!reset) {
5212 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005213 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005214 }
5215
5216 /* De-assert Restore */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005217 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005218 gpwrdn &= ~GPWRDN_RESTORE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005219 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005220 udelay(10);
5221
5222 if (!rem_wakeup) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005223 pcgcctl = dwc2_readl(hsotg, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005224 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005225 dwc2_writel(hsotg, pcgcctl, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005226 }
5227
5228 /* Restore GUSBCFG, DCFG and DCTL */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005229 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5230 dwc2_writel(hsotg, dr->dcfg, DCFG);
5231 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005232
5233 /* De-assert Wakeup Logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005234 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005235 gpwrdn &= ~GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005236 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005237
5238 if (rem_wakeup) {
5239 udelay(10);
5240 /* Start Remote Wakeup Signaling */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005241 dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005242 } else {
5243 udelay(50);
5244 /* Set Device programming done bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005245 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005246 dctl |= DCTL_PWRONPRGDONE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005247 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005248 }
5249 /* Wait for interrupts which must be cleared */
5250 mdelay(2);
5251 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005252 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005253
5254 /* Restore global registers */
5255 ret = dwc2_restore_global_registers(hsotg);
5256 if (ret) {
5257 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5258 __func__);
5259 return ret;
5260 }
5261
5262 /* Restore device registers */
5263 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5264 if (ret) {
5265 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5266 __func__);
5267 return ret;
5268 }
5269
5270 if (rem_wakeup) {
5271 mdelay(10);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005272 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005273 dctl &= ~DCTL_RMTWKUPSIG;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005274 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005275 }
5276
5277 hsotg->hibernated = 0;
5278 hsotg->lx_state = DWC2_L0;
5279 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5280
5281 return ret;
5282}