blob: a17e444e467bd1b49a2e1e18175dd1cfd27b8ef3 [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)
Minas Harutyunyan54f37f52019-03-18 14:24:30 +0400719 maxsize = (hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
720 DEV_DMA_ISOC_RX_NBYTES_LIMIT) *
721 MAX_DMA_DESC_NUM_HS_ISOC;
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800722 else
Minas Harutyunyan54f37f52019-03-18 14:24:30 +0400723 maxsize = DEV_DMA_NBYTES_LIMIT * MAX_DMA_DESC_NUM_GENERIC;
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800724
725 return maxsize;
726}
727
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800728/*
729 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
730 * @hs_ep: The endpoint
731 * @mask: RX/TX bytes mask to be defined
732 *
733 * Returns maximum data payload for one descriptor after analyzing endpoint
734 * characteristics.
735 * DMA descriptor transfer bytes limit depends on EP type:
736 * Control out - MPS,
737 * Isochronous - descriptor rx/tx bytes bitfield limit,
738 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
739 * have concatenations from various descriptors within one packet.
740 *
741 * Selects corresponding mask for RX/TX bytes as well.
742 */
743static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
744{
745 u32 mps = hs_ep->ep.maxpacket;
746 int dir_in = hs_ep->dir_in;
747 u32 desc_size = 0;
748
749 if (!hs_ep->index && !dir_in) {
750 desc_size = mps;
751 *mask = DEV_DMA_NBYTES_MASK;
752 } else if (hs_ep->isochronous) {
753 if (dir_in) {
754 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
755 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
756 } else {
757 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
758 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
759 }
760 } else {
761 desc_size = DEV_DMA_NBYTES_LIMIT;
762 *mask = DEV_DMA_NBYTES_MASK;
763
764 /* Round down desc_size to be mps multiple */
765 desc_size -= desc_size % mps;
766 }
767
768 return desc_size;
769}
770
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100771static void dwc2_gadget_fill_nonisoc_xfer_ddma_one(struct dwc2_hsotg_ep *hs_ep,
772 struct dwc2_dma_desc **desc,
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800773 dma_addr_t dma_buff,
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100774 unsigned int len,
775 bool true_last)
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800776{
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800777 int dir_in = hs_ep->dir_in;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800778 u32 mps = hs_ep->ep.maxpacket;
779 u32 maxsize = 0;
780 u32 offset = 0;
781 u32 mask = 0;
782 int i;
783
784 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
785
786 hs_ep->desc_count = (len / maxsize) +
787 ((len % maxsize) ? 1 : 0);
788 if (len == 0)
789 hs_ep->desc_count = 1;
790
791 for (i = 0; i < hs_ep->desc_count; ++i) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100792 (*desc)->status = 0;
793 (*desc)->status |= (DEV_DMA_BUFF_STS_HBUSY
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800794 << DEV_DMA_BUFF_STS_SHIFT);
795
796 if (len > maxsize) {
797 if (!hs_ep->index && !dir_in)
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100798 (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800799
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100800 (*desc)->status |=
801 maxsize << DEV_DMA_NBYTES_SHIFT & mask;
802 (*desc)->buf = dma_buff + offset;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800803
804 len -= maxsize;
805 offset += maxsize;
806 } else {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100807 if (true_last)
808 (*desc)->status |= (DEV_DMA_L | DEV_DMA_IOC);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800809
810 if (dir_in)
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100811 (*desc)->status |= (len % mps) ? DEV_DMA_SHORT :
812 ((hs_ep->send_zlp && true_last) ?
813 DEV_DMA_SHORT : 0);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800814
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100815 (*desc)->status |=
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800816 len << DEV_DMA_NBYTES_SHIFT & mask;
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100817 (*desc)->buf = dma_buff + offset;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800818 }
819
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100820 (*desc)->status &= ~DEV_DMA_BUFF_STS_MASK;
821 (*desc)->status |= (DEV_DMA_BUFF_STS_HREADY
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800822 << DEV_DMA_BUFF_STS_SHIFT);
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100823 (*desc)++;
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800824 }
825}
826
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800827/*
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100828 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
829 * @hs_ep: The endpoint
830 * @ureq: Request to transfer
831 * @offset: offset in bytes
832 * @len: Length of the transfer
833 *
834 * This function will iterate over descriptor chain and fill its entries
835 * with corresponding information based on transfer data.
836 */
837static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
838 struct usb_request *ureq,
839 unsigned int offset,
840 unsigned int len)
841{
842 struct dwc2_dma_desc *desc = hs_ep->desc_list;
843 struct scatterlist *sg;
844 int i;
845 u8 desc_count = 0;
846
847 /* non-DMA sg buffer */
848 if (!ureq->num_sgs) {
849 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
850 ureq->dma + offset, len, true);
851 return;
852 }
853
854 /* DMA sg buffer */
855 for_each_sg(ureq->sg, sg, ureq->num_sgs, i) {
856 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &desc,
857 sg_dma_address(sg) + sg->offset, sg_dma_len(sg),
858 sg_is_last(sg));
859 desc_count += hs_ep->desc_count;
860 }
861
862 hs_ep->desc_count = desc_count;
863}
864
865/*
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800866 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
867 * @hs_ep: The isochronous endpoint.
868 * @dma_buff: usb requests dma buffer.
869 * @len: usb request transfer length.
870 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400871 * Fills next free descriptor with the data of the arrived usb request,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800872 * frame info, sets Last and IOC bits increments next_desc. If filled
873 * descriptor is not the first one, removes L bit from the previous descriptor
874 * status.
875 */
876static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
877 dma_addr_t dma_buff, unsigned int len)
878{
879 struct dwc2_dma_desc *desc;
880 struct dwc2_hsotg *hsotg = hs_ep->parent;
881 u32 index;
882 u32 maxsize = 0;
883 u32 mask = 0;
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400884 u8 pid = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800885
886 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800887
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400888 index = hs_ep->next_desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800889 desc = &hs_ep->desc_list[index];
890
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400891 /* Check if descriptor chain full */
892 if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
893 DEV_DMA_BUFF_STS_HREADY) {
894 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
895 return 1;
896 }
897
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800898 /* Clear L bit of previous desc if more than one entries in the chain */
899 if (hs_ep->next_desc)
900 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
901
902 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
903 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
904
905 desc->status = 0;
906 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
907
908 desc->buf = dma_buff;
909 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
910 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
911
912 if (hs_ep->dir_in) {
Minas Harutyunyan1d8e5c02018-05-23 16:24:44 +0400913 if (len)
914 pid = DIV_ROUND_UP(len, hs_ep->ep.maxpacket);
915 else
916 pid = 1;
917 desc->status |= ((pid << DEV_DMA_ISOC_PID_SHIFT) &
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800918 DEV_DMA_ISOC_PID_MASK) |
919 ((len % hs_ep->ep.maxpacket) ?
920 DEV_DMA_SHORT : 0) |
921 ((hs_ep->target_frame <<
922 DEV_DMA_ISOC_FRNUM_SHIFT) &
923 DEV_DMA_ISOC_FRNUM_MASK);
924 }
925
926 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
927 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
928
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400929 /* Increment frame number by interval for IN */
930 if (hs_ep->dir_in)
931 dwc2_gadget_incr_frame_num(hs_ep);
932
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800933 /* Update index of last configured entry in the chain */
934 hs_ep->next_desc++;
Minas Harutyunyan54f37f52019-03-18 14:24:30 +0400935 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_HS_ISOC)
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400936 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800937
938 return 0;
939}
940
941/*
942 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
943 * @hs_ep: The isochronous endpoint.
944 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400945 * Prepare descriptor chain for isochronous endpoints. Afterwards
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800946 * write DMA address to HW and enable the endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800947 */
948static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
949{
950 struct dwc2_hsotg *hsotg = hs_ep->parent;
951 struct dwc2_hsotg_req *hs_req, *treq;
952 int index = hs_ep->index;
953 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400954 int i;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800955 u32 dma_reg;
956 u32 depctl;
957 u32 ctrl;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400958 struct dwc2_dma_desc *desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800959
960 if (list_empty(&hs_ep->queue)) {
Minas Harutyunyan1ffba902018-06-12 12:37:29 +0400961 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800962 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
963 return;
964 }
965
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400966 /* Initialize descriptor chain by Host Busy status */
Minas Harutyunyan54f37f52019-03-18 14:24:30 +0400967 for (i = 0; i < MAX_DMA_DESC_NUM_HS_ISOC; i++) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400968 desc = &hs_ep->desc_list[i];
969 desc->status = 0;
970 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
971 << DEV_DMA_BUFF_STS_SHIFT);
972 }
973
974 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800975 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +0100976 dma_addr_t dma_addr = hs_req->req.dma;
977
978 if (hs_req->req.num_sgs) {
979 WARN_ON(hs_req->req.num_sgs > 1);
980 dma_addr = sg_dma_address(hs_req->req.sg);
981 }
982 ret = dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800983 hs_req->req.length);
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400984 if (ret)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800985 break;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800986 }
987
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400988 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800989 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
990 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
991
992 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400993 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800994
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400995 ctrl = dwc2_readl(hsotg, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800996 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +0400997 dwc2_writel(hsotg, ctrl, depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800998}
999
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -08001000/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001001 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001002 * @hsotg: The controller state.
1003 * @hs_ep: The endpoint to process a request for
1004 * @hs_req: The request to start.
1005 * @continuing: True if we are doing more for the current request.
1006 *
1007 * Start the given request running by setting the endpoint registers
1008 * appropriately, and writing any data to the FIFOs.
1009 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001010static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001011 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001012 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001013 bool continuing)
1014{
1015 struct usb_request *ureq = &hs_req->req;
1016 int index = hs_ep->index;
1017 int dir_in = hs_ep->dir_in;
1018 u32 epctrl_reg;
1019 u32 epsize_reg;
1020 u32 epsize;
1021 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -08001022 unsigned int length;
1023 unsigned int packets;
1024 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001025 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001026
1027 if (index != 0) {
1028 if (hs_ep->req && !continuing) {
1029 dev_err(hsotg->dev, "%s: active request\n", __func__);
1030 WARN_ON(1);
1031 return;
1032 } else if (hs_ep->req != hs_req && continuing) {
1033 dev_err(hsotg->dev,
1034 "%s: continue different req\n", __func__);
1035 WARN_ON(1);
1036 return;
1037 }
1038 }
1039
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001040 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001041 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
1042 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001043
1044 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001045 __func__, dwc2_readl(hsotg, epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001046 hs_ep->dir_in ? "in" : "out");
1047
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001048 /* If endpoint is stalled, we will restart request later */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001049 ctrl = dwc2_readl(hsotg, epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001050
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +02001051 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001052 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
1053 return;
1054 }
1055
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001056 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +02001057 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
1058 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001059
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -08001060 if (!using_desc_dma(hsotg))
1061 maxreq = get_ep_limit(hs_ep);
1062 else
1063 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
1064
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001065 if (length > maxreq) {
1066 int round = maxreq % hs_ep->ep.maxpacket;
1067
1068 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
1069 __func__, length, maxreq, round);
1070
1071 /* round down to multiple of packets */
1072 if (round)
1073 maxreq -= round;
1074
1075 length = maxreq;
1076 }
1077
1078 if (length)
1079 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
1080 else
1081 packets = 1; /* send one packet if length is zero. */
1082
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001083 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1084 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
1085 return;
1086 }
1087
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001088 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001089 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001090 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001091 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001092 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001093 else
1094 epsize = 0;
1095
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001096 /*
1097 * zero length packet should be programmed on its own and should not
1098 * be counted in DIEPTSIZ.PktCnt with other packets.
1099 */
1100 if (dir_in && ureq->zero && !continuing) {
1101 /* Test if zlp is actually required. */
1102 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001103 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001104 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001105 }
1106
Dinh Nguyen47a16852014-04-14 14:13:34 -07001107 epsize |= DXEPTSIZ_PKTCNT(packets);
1108 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001109
1110 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1111 __func__, packets, length, ureq->length, epsize, epsize_reg);
1112
1113 /* store the request as the current one we're doing */
1114 hs_ep->req = hs_req;
1115
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001116 if (using_desc_dma(hsotg)) {
1117 u32 offset = 0;
1118 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001119
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001120 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1121 if (!dir_in) {
1122 if (!index)
1123 length = mps;
1124 else if (length % mps)
1125 length += (mps - (length % mps));
1126 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001127
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001128 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001129 * If more data to send, adjust DMA for EP0 out data stage.
1130 * ureq->dma stays unchanged, hence increment it by already
1131 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001132 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001133 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1134 continuing)
1135 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001136
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001137 /* Fill DDMA chain entries */
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01001138 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq, offset,
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001139 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001140
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001141 /* write descriptor chain address to control register */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001142 dwc2_writel(hsotg, hs_ep->desc_list_dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001143
1144 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1145 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1146 } else {
1147 /* write size / packets */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001148 dwc2_writel(hsotg, epsize, epsize_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001149
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001150 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001151 /*
1152 * write DMA address to control register, buffer
1153 * already synced by dwc2_hsotg_ep_queue().
1154 */
1155
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001156 dwc2_writel(hsotg, ureq->dma, dma_reg);
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001157
1158 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1159 __func__, &ureq->dma, dma_reg);
1160 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001161 }
1162
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001163 if (hs_ep->isochronous && hs_ep->interval == 1) {
1164 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1165 dwc2_gadget_incr_frame_num(hs_ep);
1166
1167 if (hs_ep->target_frame & 0x1)
1168 ctrl |= DXEPCTL_SETODDFR;
1169 else
1170 ctrl |= DXEPCTL_SETEVENFR;
1171 }
1172
Dinh Nguyen47a16852014-04-14 14:13:34 -07001173 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001174
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001175 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001176
1177 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001178 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001179 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001180
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001181 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001182 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001183
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001184 /*
1185 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001186 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001187 * this information.
1188 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001189 hs_ep->size_loaded = length;
1190 hs_ep->last_load = ureq->actual;
1191
1192 if (dir_in && !using_dma(hsotg)) {
1193 /* set these anyway, we may need them for non-periodic in */
1194 hs_ep->fifo_load = 0;
1195
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001196 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001197 }
1198
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001199 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001200 * Note, trying to clear the NAK here causes problems with transmit
1201 * on the S3C6400 ending up with the TXFIFO becoming full.
1202 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001203
1204 /* check ep is enabled */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001205 if (!(dwc2_readl(hsotg, epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001206 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001207 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001208 index, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001209
Dinh Nguyen47a16852014-04-14 14:13:34 -07001210 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001211 __func__, dwc2_readl(hsotg, epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001212
1213 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001214 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001215}
1216
1217/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001218 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001219 * @hsotg: The device state.
1220 * @hs_ep: The endpoint the request is on.
1221 * @req: The request being processed.
1222 *
1223 * We've been asked to queue a request, so ensure that the memory buffer
1224 * is correctly setup for DMA. If we've been passed an extant DMA address
1225 * then ensure the buffer has been synced to memory. If our buffer has no
1226 * DMA memory, then we map the memory and mark our request to allow us to
1227 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001228 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001229static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001230 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001231 struct usb_request *req)
1232{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001233 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001234
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001235 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1236 if (ret)
1237 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001238
1239 return 0;
1240
1241dma_error:
1242 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1243 __func__, req->buf, req->length);
1244
1245 return -EIO;
1246}
1247
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001248static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001249 struct dwc2_hsotg_ep *hs_ep,
1250 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001251{
1252 void *req_buf = hs_req->req.buf;
1253
1254 /* If dma is not being used or buffer is aligned */
1255 if (!using_dma(hsotg) || !((long)req_buf & 3))
1256 return 0;
1257
1258 WARN_ON(hs_req->saved_req_buf);
1259
1260 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001261 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001262
1263 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1264 if (!hs_req->req.buf) {
1265 hs_req->req.buf = req_buf;
1266 dev_err(hsotg->dev,
1267 "%s: unable to allocate memory for bounce buffer\n",
1268 __func__);
1269 return -ENOMEM;
1270 }
1271
1272 /* Save actual buffer */
1273 hs_req->saved_req_buf = req_buf;
1274
1275 if (hs_ep->dir_in)
1276 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1277 return 0;
1278}
1279
John Younb98866c2017-01-17 20:31:58 -08001280static void
1281dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1282 struct dwc2_hsotg_ep *hs_ep,
1283 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001284{
1285 /* If dma is not being used or buffer was aligned */
1286 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1287 return;
1288
1289 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1290 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1291
1292 /* Copy data from bounce buffer on successful out transfer */
1293 if (!hs_ep->dir_in && !hs_req->req.status)
1294 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001295 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001296
1297 /* Free bounce buffer */
1298 kfree(hs_req->req.buf);
1299
1300 hs_req->req.buf = hs_req->saved_req_buf;
1301 hs_req->saved_req_buf = NULL;
1302}
1303
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001304/**
1305 * dwc2_gadget_target_frame_elapsed - Checks target frame
1306 * @hs_ep: The driver endpoint to check
1307 *
1308 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1309 * corresponding transfer.
1310 */
1311static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1312{
1313 struct dwc2_hsotg *hsotg = hs_ep->parent;
1314 u32 target_frame = hs_ep->target_frame;
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001315 u32 current_frame = hsotg->frame_number;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001316 bool frame_overrun = hs_ep->frame_overrun;
1317
1318 if (!frame_overrun && current_frame >= target_frame)
1319 return true;
1320
1321 if (frame_overrun && current_frame >= target_frame &&
1322 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1323 return true;
1324
1325 return false;
1326}
1327
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001328/*
1329 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1330 * @hsotg: The driver state
1331 * @hs_ep: the ep descriptor chain is for
1332 *
1333 * Called to update EP0 structure's pointers depend on stage of
1334 * control transfer.
1335 */
1336static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1337 struct dwc2_hsotg_ep *hs_ep)
1338{
1339 switch (hsotg->ep0_state) {
1340 case DWC2_EP0_SETUP:
1341 case DWC2_EP0_STATUS_OUT:
1342 hs_ep->desc_list = hsotg->setup_desc[0];
1343 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1344 break;
1345 case DWC2_EP0_DATA_IN:
1346 case DWC2_EP0_STATUS_IN:
1347 hs_ep->desc_list = hsotg->ctrl_in_desc;
1348 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1349 break;
1350 case DWC2_EP0_DATA_OUT:
1351 hs_ep->desc_list = hsotg->ctrl_out_desc;
1352 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1353 break;
1354 default:
1355 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1356 hsotg->ep0_state);
1357 return -EINVAL;
1358 }
1359
1360 return 0;
1361}
1362
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001363static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001364 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001365{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001366 struct dwc2_hsotg_req *hs_req = our_req(req);
1367 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001368 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001369 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001370 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001371 u32 maxsize = 0;
1372 u32 mask = 0;
1373
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001374
1375 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1376 ep->name, req, req->length, req->buf, req->no_interrupt,
1377 req->zero, req->short_not_ok);
1378
Gregory Herrero7ababa92015-04-29 22:09:08 +02001379 /* Prevent new request submission when controller is suspended */
Grigor Tovmasyan88b02f22018-01-24 17:44:25 +04001380 if (hs->lx_state != DWC2_L0) {
1381 dev_dbg(hs->dev, "%s: submit request only in active state\n",
John Youn9da51972017-01-17 20:30:27 -08001382 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001383 return -EAGAIN;
1384 }
1385
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001386 /* initialise status of the request */
1387 INIT_LIST_HEAD(&hs_req->queue);
1388 req->actual = 0;
1389 req->status = -EINPROGRESS;
1390
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001391 /* In DDMA mode for ISOC's don't queue request if length greater
1392 * than descriptor limits.
1393 */
1394 if (using_desc_dma(hs) && hs_ep->isochronous) {
1395 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1396 if (hs_ep->dir_in && req->length > maxsize) {
1397 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1398 req->length, maxsize);
1399 return -EINVAL;
1400 }
1401
1402 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1403 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1404 req->length, hs_ep->ep.maxpacket);
1405 return -EINVAL;
1406 }
1407 }
1408
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001409 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001410 if (ret)
1411 return ret;
1412
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001413 /* if we're using DMA, sync the buffers as necessary */
1414 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001415 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001416 if (ret)
1417 return ret;
1418 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001419 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1420 if (using_desc_dma(hs) && !hs_ep->index) {
1421 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1422 if (ret)
1423 return ret;
1424 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001425
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001426 first = list_empty(&hs_ep->queue);
1427 list_add_tail(&hs_req->queue, &hs_ep->queue);
1428
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001429 /*
1430 * Handle DDMA isochronous transfers separately - just add new entry
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001431 * to the descriptor chain.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001432 * Transfer will be started once SW gets either one of NAK or
1433 * OutTknEpDis interrupts.
1434 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001435 if (using_desc_dma(hs) && hs_ep->isochronous) {
1436 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01001437 dma_addr_t dma_addr = hs_req->req.dma;
1438
1439 if (hs_req->req.num_sgs) {
1440 WARN_ON(hs_req->req.num_sgs > 1);
1441 dma_addr = sg_dma_address(hs_req->req.sg);
1442 }
1443 dwc2_gadget_fill_isoc_desc(hs_ep, dma_addr,
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001444 hs_req->req.length);
1445 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001446 return 0;
1447 }
1448
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +04001449 /* Change EP direction if status phase request is after data out */
1450 if (!hs_ep->index && !req->length && !hs_ep->dir_in &&
1451 hs->ep0_state == DWC2_EP0_DATA_OUT)
1452 hs_ep->dir_in = 1;
1453
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001454 if (first) {
1455 if (!hs_ep->isochronous) {
1456 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1457 return 0;
1458 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001459
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001460 /* Update current frame number value. */
1461 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1462 while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001463 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001464 /* Update current frame number value once more as it
1465 * changes here.
1466 */
1467 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1468 }
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001469
1470 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1471 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1472 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001473 return 0;
1474}
1475
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001476static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001477 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001478{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001479 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001480 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001481 unsigned long flags = 0;
1482 int ret = 0;
1483
1484 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001485 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001486 spin_unlock_irqrestore(&hs->lock, flags);
1487
1488 return ret;
1489}
1490
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001491static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001492 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001493{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001494 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001495
1496 kfree(hs_req);
1497}
1498
1499/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001500 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001501 * @ep: The endpoint the request was on.
1502 * @req: The request completed.
1503 *
1504 * Called on completion of any requests the driver itself
1505 * submitted that need cleaning up.
1506 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001507static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001508 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001509{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001510 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001511 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001512
1513 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1514
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001515 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001516}
1517
1518/**
1519 * ep_from_windex - convert control wIndex value to endpoint
1520 * @hsotg: The driver state.
1521 * @windex: The control request wIndex field (in host order).
1522 *
1523 * Convert the given wIndex into a pointer to an driver endpoint
1524 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001525 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001526static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001527 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001528{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001529 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001530 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1531 int idx = windex & 0x7F;
1532
1533 if (windex >= 0x100)
1534 return NULL;
1535
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001536 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001537 return NULL;
1538
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001539 ep = index_to_ep(hsotg, idx, dir);
1540
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001541 if (idx && ep->dir_in != dir)
1542 return NULL;
1543
1544 return ep;
1545}
1546
1547/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001548 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001549 * @hsotg: The driver state.
1550 * @testmode: requested usb test mode
1551 * Enable usb Test Mode requested by the Host.
1552 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001553int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001554{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001555 int dctl = dwc2_readl(hsotg, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001556
1557 dctl &= ~DCTL_TSTCTL_MASK;
1558 switch (testmode) {
1559 case TEST_J:
1560 case TEST_K:
1561 case TEST_SE0_NAK:
1562 case TEST_PACKET:
1563 case TEST_FORCE_EN:
1564 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1565 break;
1566 default:
1567 return -EINVAL;
1568 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001569 dwc2_writel(hsotg, dctl, DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001570 return 0;
1571}
1572
1573/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001574 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001575 * @hsotg: The device state
1576 * @ep: Endpoint 0
1577 * @buff: Buffer for request
1578 * @length: Length of reply.
1579 *
1580 * Create a request and queue it on the given endpoint. This is useful as
1581 * an internal method of sending replies to certain control requests, etc.
1582 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001583static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001584 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001585 void *buff,
1586 int length)
1587{
1588 struct usb_request *req;
1589 int ret;
1590
1591 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1592
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001593 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001594 hsotg->ep0_reply = req;
1595 if (!req) {
1596 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1597 return -ENOMEM;
1598 }
1599
1600 req->buf = hsotg->ep0_buff;
1601 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001602 /*
1603 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1604 * STATUS stage.
1605 */
1606 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001607 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001608
1609 if (length)
1610 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001611
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001612 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001613 if (ret) {
1614 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1615 return ret;
1616 }
1617
1618 return 0;
1619}
1620
1621/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001622 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001623 * @hsotg: The device state
1624 * @ctrl: USB control request
1625 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001626static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001627 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001628{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001629 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1630 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001631 __le16 reply;
1632 int ret;
1633
1634 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1635
1636 if (!ep0->dir_in) {
1637 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1638 return -EINVAL;
1639 }
1640
1641 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1642 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001643 /*
1644 * bit 0 => self powered
1645 * bit 1 => remote wakeup
1646 */
1647 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001648 break;
1649
1650 case USB_RECIP_INTERFACE:
1651 /* currently, the data result should be zero */
1652 reply = cpu_to_le16(0);
1653 break;
1654
1655 case USB_RECIP_ENDPOINT:
1656 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1657 if (!ep)
1658 return -ENOENT;
1659
1660 reply = cpu_to_le16(ep->halted ? 1 : 0);
1661 break;
1662
1663 default:
1664 return 0;
1665 }
1666
1667 if (le16_to_cpu(ctrl->wLength) != 2)
1668 return -EINVAL;
1669
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001670 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001671 if (ret) {
1672 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1673 return ret;
1674 }
1675
1676 return 1;
1677}
1678
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001679static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001680
1681/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001682 * get_ep_head - return the first request on the endpoint
1683 * @hs_ep: The controller endpoint to get
1684 *
1685 * Get the first request on the endpoint.
1686 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001687static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001688{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001689 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1690 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001691}
1692
1693/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001694 * dwc2_gadget_start_next_request - Starts next request from ep queue
1695 * @hs_ep: Endpoint structure
1696 *
1697 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1698 * in its handler. Hence we need to unmask it here to be able to do
1699 * resynchronization.
1700 */
1701static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1702{
1703 u32 mask;
1704 struct dwc2_hsotg *hsotg = hs_ep->parent;
1705 int dir_in = hs_ep->dir_in;
1706 struct dwc2_hsotg_req *hs_req;
1707 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1708
1709 if (!list_empty(&hs_ep->queue)) {
1710 hs_req = get_ep_head(hs_ep);
1711 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1712 return;
1713 }
1714 if (!hs_ep->isochronous)
1715 return;
1716
1717 if (dir_in) {
1718 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1719 __func__);
1720 } else {
1721 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1722 __func__);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001723 mask = dwc2_readl(hsotg, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001724 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001725 dwc2_writel(hsotg, mask, epmsk_reg);
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001726 }
1727}
1728
1729/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001730 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001731 * @hsotg: The device state
1732 * @ctrl: USB control request
1733 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001734static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001735 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001736{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001737 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1738 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001739 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001740 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001741 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001742 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001743 u32 recip;
1744 u32 wValue;
1745 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001746
1747 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1748 __func__, set ? "SET" : "CLEAR");
1749
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001750 wValue = le16_to_cpu(ctrl->wValue);
1751 wIndex = le16_to_cpu(ctrl->wIndex);
1752 recip = ctrl->bRequestType & USB_RECIP_MASK;
1753
1754 switch (recip) {
1755 case USB_RECIP_DEVICE:
1756 switch (wValue) {
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001757 case USB_DEVICE_REMOTE_WAKEUP:
1758 hsotg->remote_wakeup_allowed = 1;
1759 break;
1760
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001761 case USB_DEVICE_TEST_MODE:
1762 if ((wIndex & 0xff) != 0)
1763 return -EINVAL;
1764 if (!set)
1765 return -EINVAL;
1766
1767 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001768 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001769 if (ret) {
1770 dev_err(hsotg->dev,
1771 "%s: failed to send reply\n", __func__);
1772 return ret;
1773 }
1774 break;
1775 default:
1776 return -ENOENT;
1777 }
1778 break;
1779
1780 case USB_RECIP_ENDPOINT:
1781 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001782 if (!ep) {
1783 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001784 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001785 return -ENOENT;
1786 }
1787
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001788 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001789 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001790 halted = ep->halted;
1791
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001792 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001793
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001794 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001795 if (ret) {
1796 dev_err(hsotg->dev,
1797 "%s: failed to send reply\n", __func__);
1798 return ret;
1799 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001800
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001801 /*
1802 * we have to complete all requests for ep if it was
1803 * halted, and the halt was cleared by CLEAR_FEATURE
1804 */
1805
1806 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001807 /*
1808 * If we have request in progress,
1809 * then complete it
1810 */
1811 if (ep->req) {
1812 hs_req = ep->req;
1813 ep->req = NULL;
1814 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001815 if (hs_req->req.complete) {
1816 spin_unlock(&hsotg->lock);
1817 usb_gadget_giveback_request(
1818 &ep->ep, &hs_req->req);
1819 spin_lock(&hsotg->lock);
1820 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001821 }
1822
1823 /* If we have pending request, then start it */
John Youn34c0887f2017-01-17 20:31:43 -08001824 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001825 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001826 }
1827
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001828 break;
1829
1830 default:
1831 return -ENOENT;
1832 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001833 break;
1834 default:
1835 return -ENOENT;
1836 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001837 return 1;
1838}
1839
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001840static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001841
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001842/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001843 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001844 * @hsotg: The device state
1845 *
1846 * Set stall for ep0 as response for setup request.
1847 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001848static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001849{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001850 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001851 u32 reg;
1852 u32 ctrl;
1853
1854 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1855 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1856
1857 /*
1858 * DxEPCTL_Stall will be cleared by EP once it has
1859 * taken effect, so no need to clear later.
1860 */
1861
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001862 ctrl = dwc2_readl(hsotg, reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001863 ctrl |= DXEPCTL_STALL;
1864 ctrl |= DXEPCTL_CNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001865 dwc2_writel(hsotg, ctrl, reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001866
1867 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001868 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001869 ctrl, reg, dwc2_readl(hsotg, reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001870
1871 /*
1872 * complete won't be called, so we enqueue
1873 * setup request here
1874 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001875 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001876}
1877
1878/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001879 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001880 * @hsotg: The device state
1881 * @ctrl: The control request received
1882 *
1883 * The controller has received the SETUP phase of a control request, and
1884 * needs to work out what to do next (and whether to pass it on to the
1885 * gadget driver).
1886 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001887static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001888 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001889{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001890 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001891 int ret = 0;
1892 u32 dcfg;
1893
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001894 dev_dbg(hsotg->dev,
1895 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1896 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1897 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001898
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001899 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001900 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001901 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1902 } else if (ctrl->bRequestType & USB_DIR_IN) {
1903 ep0->dir_in = 1;
1904 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1905 } else {
1906 ep0->dir_in = 0;
1907 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1908 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001909
1910 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1911 switch (ctrl->bRequest) {
1912 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001913 hsotg->connected = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001914 dcfg = dwc2_readl(hsotg, DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001915 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001916 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1917 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04001918 dwc2_writel(hsotg, dcfg, DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001919
1920 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1921
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001922 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001923 return;
1924
1925 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001926 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001927 break;
1928
1929 case USB_REQ_CLEAR_FEATURE:
1930 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001931 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001932 break;
1933 }
1934 }
1935
1936 /* as a fallback, try delivering it to the driver to deal with */
1937
1938 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001939 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001940 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001941 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001942 if (ret < 0)
1943 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1944 }
1945
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +04001946 hsotg->delayed_status = false;
1947 if (ret == USB_GADGET_DELAYED_STATUS)
1948 hsotg->delayed_status = true;
1949
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001950 /*
1951 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001952 * so respond with a STALL for the status stage to indicate failure.
1953 */
1954
Robert Baldygac9f721b2014-01-14 08:36:00 +01001955 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001956 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001957}
1958
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001959/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001960 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001961 * @ep: The endpoint the request was on.
1962 * @req: The request completed.
1963 *
1964 * Called on completion of any requests the driver itself submitted for
1965 * EP0 setup packets
1966 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001967static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001968 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001969{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001970 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001971 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001972
1973 if (req->status < 0) {
1974 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1975 return;
1976 }
1977
Robert Baldyga93f599f2013-11-21 13:49:17 +01001978 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001979 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001980 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001981 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001982 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001983 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001984}
1985
1986/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001987 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001988 * @hsotg: The device state.
1989 *
1990 * Enqueue a request on EP0 if necessary to received any SETUP packets
1991 * received from the host.
1992 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001993static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001994{
1995 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001996 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001997 int ret;
1998
1999 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
2000
2001 req->zero = 0;
2002 req->length = 8;
2003 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002004 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002005
2006 if (!list_empty(&hs_req->queue)) {
2007 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
2008 return;
2009 }
2010
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002011 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002012 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002013 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002014
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002015 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002016 if (ret < 0) {
2017 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002018 /*
2019 * Don't think there's much we can do other than watch the
2020 * driver fail.
2021 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002022 }
2023}
2024
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002025static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002026 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002027{
2028 u32 ctrl;
2029 u8 index = hs_ep->index;
2030 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
2031 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
2032
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01002033 if (hs_ep->dir_in)
2034 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002035 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01002036 else
2037 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002038 index);
2039 if (using_desc_dma(hsotg)) {
Minas Harutyunyan201ec562018-01-16 16:03:32 +04002040 if (!index)
2041 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
2042
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01002043 /* Not specific buffer needed for ep0 ZLP */
2044 dwc2_gadget_fill_nonisoc_xfer_ddma_one(hs_ep, &hs_ep->desc_list,
2045 hs_ep->desc_list_dma, 0, true);
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002046 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002047 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
2048 DXEPTSIZ_XFERSIZE(0),
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08002049 epsiz_reg);
2050 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002051
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002052 ctrl = dwc2_readl(hsotg, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002053 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
2054 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
2055 ctrl |= DXEPCTL_USBACTEP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002056 dwc2_writel(hsotg, ctrl, epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002057}
2058
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002059/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002060 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002061 * @hsotg: The device state.
2062 * @hs_ep: The endpoint the request was on.
2063 * @hs_req: The request to complete.
2064 * @result: The result code (0 => Ok, otherwise errno)
2065 *
2066 * The given request has finished, so call the necessary completion
2067 * if it has one and then look to see if we can start a new request
2068 * on the endpoint.
2069 *
2070 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002071 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002072static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002073 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002074 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002075 int result)
2076{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002077 if (!hs_req) {
2078 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
2079 return;
2080 }
2081
2082 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
2083 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
2084
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002085 /*
2086 * only replace the status if we've not already set an error
2087 * from a previous transaction
2088 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002089
2090 if (hs_req->req.status == -EINPROGRESS)
2091 hs_req->req.status = result;
2092
Yunzhi Li44583fe2015-09-29 12:25:01 +02002093 if (using_dma(hsotg))
2094 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2095
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002096 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01002097
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002098 hs_ep->req = NULL;
2099 list_del_init(&hs_req->queue);
2100
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002101 /*
2102 * call the complete request with the locks off, just in case the
2103 * request tries to queue more work for this endpoint.
2104 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002105
2106 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002107 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002108 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002109 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002110 }
2111
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002112 /* In DDMA don't need to proceed to starting of next ISOC request */
2113 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2114 return;
2115
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002116 /*
2117 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002118 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002119 * so be careful when doing this.
2120 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002121
John Youn34c0887f2017-01-17 20:31:43 -08002122 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002123 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002124}
2125
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002126/*
2127 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2128 * @hs_ep: The endpoint the request was on.
2129 *
2130 * Get first request from the ep queue, determine descriptor on which complete
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002131 * happened. SW discovers which descriptor currently in use by HW, adjusts
2132 * dma_address and calculates index of completed descriptor based on the value
2133 * of DEPDMA register. Update actual length of request, giveback to gadget.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002134 */
2135static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2136{
2137 struct dwc2_hsotg *hsotg = hs_ep->parent;
2138 struct dwc2_hsotg_req *hs_req;
2139 struct usb_request *ureq;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002140 u32 desc_sts;
2141 u32 mask;
2142
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002143 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2144
2145 /* Process only descriptors with buffer status set to DMA done */
2146 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2147 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2148
2149 hs_req = get_ep_head(hs_ep);
2150 if (!hs_req) {
2151 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2152 return;
2153 }
2154 ureq = &hs_req->req;
2155
2156 /* Check completion status */
2157 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2158 DEV_DMA_STS_SUCC) {
2159 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2160 DEV_DMA_ISOC_RX_NBYTES_MASK;
2161 ureq->actual = ureq->length - ((desc_sts & mask) >>
2162 DEV_DMA_ISOC_NBYTES_SHIFT);
2163
2164 /* Adjust actual len for ISOC Out if len is
2165 * not align of 4
2166 */
2167 if (!hs_ep->dir_in && ureq->length & 0x3)
2168 ureq->actual += 4 - (ureq->length & 0x3);
Minas Harutyunyanc8006f62019-03-12 13:27:46 +04002169
2170 /* Set actual frame number for completed transfers */
2171 ureq->frame_number =
2172 (desc_sts & DEV_DMA_ISOC_FRNUM_MASK) >>
2173 DEV_DMA_ISOC_FRNUM_SHIFT;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002174 }
2175
2176 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2177
2178 hs_ep->compl_desc++;
Minas Harutyunyan54f37f52019-03-18 14:24:30 +04002179 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_HS_ISOC - 1))
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002180 hs_ep->compl_desc = 0;
2181 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002182 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002183}
2184
2185/*
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002186 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2187 * @hs_ep: The isochronous endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002188 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002189 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2190 * interrupt. Reset target frame and next_desc to allow to start
2191 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2192 * interrupt for OUT direction.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002193 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002194static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002195{
2196 struct dwc2_hsotg *hsotg = hs_ep->parent;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002197
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002198 if (!hs_ep->dir_in)
2199 dwc2_flush_rx_fifo(hsotg);
2200 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002201
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002202 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2203 hs_ep->next_desc = 0;
2204 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002205}
2206
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002207/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002208 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002209 * @hsotg: The device state.
2210 * @ep_idx: The endpoint index for the data
2211 * @size: The size of data in the fifo, in bytes
2212 *
2213 * The FIFO status shows there is data to read from the FIFO for a given
2214 * endpoint, so sort out whether we need to read the data into a request
2215 * that has been made for that endpoint.
2216 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002217static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002218{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002219 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2220 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002221 int to_read;
2222 int max_req;
2223 int read_ptr;
2224
2225 if (!hs_req) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002226 u32 epctl = dwc2_readl(hsotg, DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002227 int ptr;
2228
Robert Baldyga6b448af42014-12-16 11:51:44 +01002229 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002230 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002231 __func__, size, ep_idx, epctl);
2232
2233 /* dump the data from the FIFO, we've nothing we can do */
2234 for (ptr = 0; ptr < size; ptr += 4)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002235 (void)dwc2_readl(hsotg, EPFIFO(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002236
2237 return;
2238 }
2239
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002240 to_read = size;
2241 read_ptr = hs_req->req.actual;
2242 max_req = hs_req->req.length - read_ptr;
2243
Ben Dooksa33e7132010-07-19 09:40:49 +01002244 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2245 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2246
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002247 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002248 /*
2249 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002250 * to deal with in this request.
2251 */
2252
2253 /* currently we don't deal this */
2254 WARN_ON_ONCE(1);
2255 }
2256
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002257 hs_ep->total_data += to_read;
2258 hs_req->req.actual += to_read;
2259 to_read = DIV_ROUND_UP(to_read, 4);
2260
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002261 /*
2262 * note, we might over-write the buffer end by 3 bytes depending on
2263 * alignment of the data.
2264 */
Gevorg Sahakyan342ccce2018-07-26 18:00:41 +04002265 dwc2_readl_rep(hsotg, EPFIFO(ep_idx),
2266 hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002267}
2268
2269/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002270 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002271 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002272 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002273 *
2274 * Generate a zero-length IN packet request for terminating a SETUP
2275 * transaction.
2276 *
2277 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002278 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002279 * the TxFIFO.
2280 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002281static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002282{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002283 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002284 hsotg->eps_out[0]->dir_in = dir_in;
2285 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002286
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002287 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002288}
2289
Roman Bacikec1f9d92015-09-10 18:13:43 -07002290static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002291 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002292{
2293 u32 ctrl;
2294
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002295 ctrl = dwc2_readl(hsotg, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002296 if (ctrl & DXEPCTL_EOFRNUM)
2297 ctrl |= DXEPCTL_SETEVENFR;
2298 else
2299 ctrl |= DXEPCTL_SETODDFR;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002300 dwc2_writel(hsotg, ctrl, epctl_reg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002301}
2302
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002303/*
2304 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2305 * @hs_ep - The endpoint on which transfer went
2306 *
2307 * Iterate over endpoints descriptor chain and get info on bytes remained
2308 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2309 */
2310static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2311{
2312 struct dwc2_hsotg *hsotg = hs_ep->parent;
2313 unsigned int bytes_rem = 0;
2314 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2315 int i;
2316 u32 status;
2317
2318 if (!desc)
2319 return -EINVAL;
2320
2321 for (i = 0; i < hs_ep->desc_count; ++i) {
2322 status = desc->status;
2323 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2324
2325 if (status & DEV_DMA_STS_MASK)
2326 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2327 i, status & DEV_DMA_STS_MASK);
2328 }
2329
2330 return bytes_rem;
2331}
2332
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002333/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002334 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002335 * @hsotg: The device instance
2336 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002337 *
2338 * The RXFIFO has delivered an OutDone event, which means that the data
2339 * transfer for an OUT endpoint has been completed, either by a short
2340 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002341 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002342static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002343{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002344 u32 epsize = dwc2_readl(hsotg, DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002345 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2346 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002347 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002348 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002349 int result = 0;
2350
2351 if (!hs_req) {
2352 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2353 return;
2354 }
2355
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002356 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2357 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002358 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2359 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002360 return;
2361 }
2362
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002363 if (using_desc_dma(hsotg))
2364 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2365
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002367 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002368
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002369 /*
2370 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002371 * is left in the endpoint size register and then working it
2372 * out from the amount we loaded for the transfer.
2373 *
2374 * We need to do this as DMA pointers are always 32bit aligned
2375 * so may overshoot/undershoot the transfer.
2376 */
2377
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002378 size_done = hs_ep->size_loaded - size_left;
2379 size_done += hs_ep->last_load;
2380
2381 req->actual = size_done;
2382 }
2383
Ben Dooksa33e7132010-07-19 09:40:49 +01002384 /* if there is more request to do, schedule new transfer */
2385 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002386 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002387 return;
2388 }
2389
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002390 if (req->actual < req->length && req->short_not_ok) {
2391 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2392 __func__, req->actual, req->length);
2393
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002394 /*
2395 * todo - what should we return here? there's no one else
2396 * even bothering to check the status.
2397 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002398 }
2399
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002400 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2401 if (!using_desc_dma(hsotg) && epnum == 0 &&
2402 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002403 /* Move to STATUS IN */
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +04002404 if (!hsotg->delayed_status)
2405 dwc2_hsotg_ep0_zlp(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002406 }
2407
Roman Bacikec1f9d92015-09-10 18:13:43 -07002408 /*
2409 * Slave mode OUT transfers do not go through XferComplete so
2410 * adjust the ISOC parity here.
2411 */
2412 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002413 if (hs_ep->isochronous && hs_ep->interval == 1)
2414 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002415 else if (hs_ep->isochronous && hs_ep->interval > 1)
2416 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002417 }
2418
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002419 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002420}
2421
2422/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002423 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002424 * @hsotg: The device instance
2425 *
2426 * The IRQ handler has detected that the RX FIFO has some data in it
2427 * that requires processing, so find out what is in there and do the
2428 * appropriate read.
2429 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002430 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002431 * chunks, so if you have x packets received on an endpoint you'll get x
2432 * FIFO events delivered, each with a packet's worth of data in it.
2433 *
2434 * When using DMA, we should not be processing events from the RXFIFO
2435 * as the actual data should be sent to the memory directly and we turn
2436 * on the completion interrupts to get notifications of transfer completion.
2437 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002438static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002439{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002440 u32 grxstsr = dwc2_readl(hsotg, GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002441 u32 epnum, status, size;
2442
2443 WARN_ON(using_dma(hsotg));
2444
Dinh Nguyen47a16852014-04-14 14:13:34 -07002445 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2446 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002447
Dinh Nguyen47a16852014-04-14 14:13:34 -07002448 size = grxstsr & GRXSTS_BYTECNT_MASK;
2449 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002450
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002451 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002452 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002453
Dinh Nguyen47a16852014-04-14 14:13:34 -07002454 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2455 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2456 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002457 break;
2458
Dinh Nguyen47a16852014-04-14 14:13:34 -07002459 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002460 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002461 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002462
2463 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002464 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002465 break;
2466
Dinh Nguyen47a16852014-04-14 14:13:34 -07002467 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002468 dev_dbg(hsotg->dev,
2469 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002470 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002471 dwc2_readl(hsotg, DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002472 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002473 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002474 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2475 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2476 */
2477 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002478 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002479 break;
2480
Dinh Nguyen47a16852014-04-14 14:13:34 -07002481 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002482 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002483 break;
2484
Dinh Nguyen47a16852014-04-14 14:13:34 -07002485 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002486 dev_dbg(hsotg->dev,
2487 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002488 dwc2_hsotg_read_frameno(hsotg),
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002489 dwc2_readl(hsotg, DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002490
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002491 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2492
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002493 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002494 break;
2495
2496 default:
2497 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2498 __func__, grxstsr);
2499
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002500 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002501 break;
2502 }
2503}
2504
2505/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002506 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002507 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002508 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002509static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002510{
2511 switch (mps) {
2512 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002513 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002514 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002515 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002517 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002518 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002519 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520 }
2521
2522 /* bad max packet size, warn and return invalid result */
2523 WARN_ON(1);
2524 return (u32)-1;
2525}
2526
2527/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002528 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002529 * @hsotg: The driver state.
2530 * @ep: The index number of the endpoint
2531 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002532 * @mc: The multicount value
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002533 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002534 *
2535 * Configure the maximum packet size for the given endpoint, updating
2536 * the hardware control registers to reflect this.
2537 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002538static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002539 unsigned int ep, unsigned int mps,
2540 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002541{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002542 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002543 u32 reg;
2544
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002545 hs_ep = index_to_ep(hsotg, ep, dir_in);
2546 if (!hs_ep)
2547 return;
2548
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002549 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002550 u32 mps_bytes = mps;
2551
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002552 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002553 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2554 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002555 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002556 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002557 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002558 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002559 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002560 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002561 hs_ep->mc = mc;
2562 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002563 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002564 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002565 }
2566
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002567 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002568 reg = dwc2_readl(hsotg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002569 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002570 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002571 dwc2_writel(hsotg, reg, DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002572 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002573 reg = dwc2_readl(hsotg, DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002574 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002575 reg |= mps;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002576 dwc2_writel(hsotg, reg, DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002577 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002578
2579 return;
2580
2581bad_mps:
2582 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2583}
2584
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002585/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002586 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002587 * @hsotg: The driver state
2588 * @idx: The index for the endpoint (0..15)
2589 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002590static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002591{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002592 dwc2_writel(hsotg, GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2593 GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002594
2595 /* wait until the fifo is flushed */
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002596 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2597 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2598 __func__);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002599}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002600
2601/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002602 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002603 * @hsotg: The driver state
2604 * @hs_ep: The driver endpoint to check.
2605 *
2606 * Check to see if there is a request that has data to send, and if so
2607 * make an attempt to write data into the FIFO.
2608 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002609static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002610 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002611{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002612 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002613
Robert Baldygaafcf4162013-09-19 11:50:19 +02002614 if (!hs_ep->dir_in || !hs_req) {
2615 /**
2616 * if request is not enqueued, we disable interrupts
2617 * for endpoints, excepting ep0
2618 */
2619 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002620 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002621 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002622 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002623 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002624
2625 if (hs_req->req.actual < hs_req->req.length) {
2626 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2627 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002628 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002629 }
2630
2631 return 0;
2632}
2633
2634/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002635 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002636 * @hsotg: The device state.
2637 * @hs_ep: The endpoint that has just completed.
2638 *
2639 * An IN transfer has been completed, update the transfer's state and then
2640 * call the relevant completion routines.
2641 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002642static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002643 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002644{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002645 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002646 u32 epsize = dwc2_readl(hsotg, DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002647 int size_left, size_done;
2648
2649 if (!hs_req) {
2650 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2651 return;
2652 }
2653
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002654 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002655 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2656 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002657
2658 /*
2659 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2660 * changed to IN. Change back to complete OUT transfer request
2661 */
2662 hs_ep->dir_in = 0;
2663
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002664 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002665 if (hsotg->test_mode) {
2666 int ret;
2667
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002668 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002669 if (ret < 0) {
2670 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002671 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002672 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002673 return;
2674 }
2675 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002676 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002677 return;
2678 }
2679
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002680 /*
2681 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002682 * in the endpoint size register and then working it out from
2683 * the amount we loaded for the transfer.
2684 *
2685 * We do this even for DMA, as the transfer may have incremented
2686 * past the end of the buffer (DMA transfers are always 32bit
2687 * aligned).
2688 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002689 if (using_desc_dma(hsotg)) {
2690 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2691 if (size_left < 0)
2692 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2693 size_left);
2694 } else {
2695 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2696 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002697
2698 size_done = hs_ep->size_loaded - size_left;
2699 size_done += hs_ep->last_load;
2700
2701 if (hs_req->req.actual != size_done)
2702 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2703 __func__, hs_req->req.actual, size_done);
2704
2705 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002706 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2707 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002708
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002709 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2710 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002711 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002712 return;
2713 }
2714
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002715 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002716 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002717 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002718 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002719 /* transfer will be completed on next complete interrupt */
2720 return;
2721 }
2722
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002723 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2724 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002725 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002726 return;
2727 }
2728
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002729 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002730}
2731
2732/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002733 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2734 * @hsotg: The device state.
2735 * @idx: Index of ep.
2736 * @dir_in: Endpoint direction 1-in 0-out.
2737 *
2738 * Reads for endpoint with given index and direction, by masking
2739 * epint_reg with coresponding mask.
2740 */
2741static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2742 unsigned int idx, int dir_in)
2743{
2744 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2745 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2746 u32 ints;
2747 u32 mask;
2748 u32 diepempmsk;
2749
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002750 mask = dwc2_readl(hsotg, epmsk_reg);
2751 diepempmsk = dwc2_readl(hsotg, DIEPEMPMSK);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002752 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2753 mask |= DXEPINT_SETUP_RCVD;
2754
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002755 ints = dwc2_readl(hsotg, epint_reg);
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002756 ints &= mask;
2757 return ints;
2758}
2759
2760/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002761 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2762 * @hs_ep: The endpoint on which interrupt is asserted.
2763 *
2764 * This interrupt indicates that the endpoint has been disabled per the
2765 * application's request.
2766 *
2767 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2768 * in case of ISOC completes current request.
2769 *
2770 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2771 * request starts it.
2772 */
2773static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2774{
2775 struct dwc2_hsotg *hsotg = hs_ep->parent;
2776 struct dwc2_hsotg_req *hs_req;
2777 unsigned char idx = hs_ep->index;
2778 int dir_in = hs_ep->dir_in;
2779 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002780 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002781
2782 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2783
2784 if (dir_in) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002785 int epctl = dwc2_readl(hsotg, epctl_reg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002786
2787 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2788
2789 if (hs_ep->isochronous) {
2790 dwc2_hsotg_complete_in(hsotg, hs_ep);
2791 return;
2792 }
2793
2794 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002795 int dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002796
2797 dctl |= DCTL_CGNPINNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002798 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002799 }
2800 return;
2801 }
2802
2803 if (dctl & DCTL_GOUTNAKSTS) {
2804 dctl |= DCTL_CGOUTNAK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002805 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002806 }
2807
2808 if (!hs_ep->isochronous)
2809 return;
2810
2811 if (list_empty(&hs_ep->queue)) {
2812 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2813 __func__, hs_ep);
2814 return;
2815 }
2816
2817 do {
2818 hs_req = get_ep_head(hs_ep);
2819 if (hs_req)
2820 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2821 -ENODATA);
2822 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04002823 /* Update current frame number value. */
2824 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002825 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2826
2827 dwc2_gadget_start_next_request(hs_ep);
2828}
2829
2830/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002831 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002832 * @ep: The endpoint on which interrupt is asserted.
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002833 *
2834 * This is starting point for ISOC-OUT transfer, synchronization done with
2835 * first out token received from host while corresponding EP is disabled.
2836 *
2837 * Device does not know initial frame in which out token will come. For this
2838 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2839 * getting this interrupt SW starts calculation for next transfer frame.
2840 */
2841static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2842{
2843 struct dwc2_hsotg *hsotg = ep->parent;
2844 int dir_in = ep->dir_in;
2845 u32 doepmsk;
2846
2847 if (dir_in || !ep->isochronous)
2848 return;
2849
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002850 if (using_desc_dma(hsotg)) {
2851 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2852 /* Start first ISO Out */
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002853 ep->target_frame = hsotg->frame_number;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002854 dwc2_gadget_start_isoc_ddma(ep);
2855 }
2856 return;
2857 }
2858
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002859 if (ep->interval > 1 &&
2860 ep->target_frame == TARGET_FRAME_INITIAL) {
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002861 u32 ctrl;
2862
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002863 ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002864 dwc2_gadget_incr_frame_num(ep);
2865
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002866 ctrl = dwc2_readl(hsotg, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002867 if (ep->target_frame & 0x1)
2868 ctrl |= DXEPCTL_SETODDFR;
2869 else
2870 ctrl |= DXEPCTL_SETEVENFR;
2871
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002872 dwc2_writel(hsotg, ctrl, DOEPCTL(ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002873 }
2874
2875 dwc2_gadget_start_next_request(ep);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002876 doepmsk = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002877 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002878 dwc2_writel(hsotg, doepmsk, DOEPMSK);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002879}
2880
2881/**
John Youn38beaec2017-01-17 20:31:13 -08002882 * dwc2_gadget_handle_nak - handle NAK interrupt
2883 * @hs_ep: The endpoint on which interrupt is asserted.
2884 *
2885 * This is starting point for ISOC-IN transfer, synchronization done with
2886 * first IN token received from host while corresponding EP is disabled.
2887 *
2888 * Device does not know when first one token will arrive from host. On first
2889 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2890 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2891 * sent in response to that as there was no data in FIFO. SW is basing on this
2892 * interrupt to obtain frame in which token has come and then based on the
2893 * interval calculates next frame for transfer.
2894 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002895static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2896{
2897 struct dwc2_hsotg *hsotg = hs_ep->parent;
2898 int dir_in = hs_ep->dir_in;
2899
2900 if (!dir_in || !hs_ep->isochronous)
2901 return;
2902
2903 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002904
2905 if (using_desc_dma(hsotg)) {
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002906 hs_ep->target_frame = hsotg->frame_number;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002907 dwc2_gadget_incr_frame_num(hs_ep);
Grigor Tovmasyan48dac4e2018-08-29 21:00:33 +04002908
2909 /* In service interval mode target_frame must
2910 * be set to last (u)frame of the service interval.
2911 */
2912 if (hsotg->params.service_interval) {
2913 /* Set target_frame to the first (u)frame of
2914 * the service interval
2915 */
2916 hs_ep->target_frame &= ~hs_ep->interval + 1;
2917
2918 /* Set target_frame to the last (u)frame of
2919 * the service interval
2920 */
2921 dwc2_gadget_incr_frame_num(hs_ep);
2922 dwc2_gadget_dec_frame_num_by_one(hs_ep);
2923 }
2924
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002925 dwc2_gadget_start_isoc_ddma(hs_ep);
2926 return;
2927 }
2928
Minas Harutyunyan4d4f1e72018-07-27 14:48:45 +04002929 hs_ep->target_frame = hsotg->frame_number;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002930 if (hs_ep->interval > 1) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002931 u32 ctrl = dwc2_readl(hsotg,
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002932 DIEPCTL(hs_ep->index));
2933 if (hs_ep->target_frame & 0x1)
2934 ctrl |= DXEPCTL_SETODDFR;
2935 else
2936 ctrl |= DXEPCTL_SETEVENFR;
2937
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002938 dwc2_writel(hsotg, ctrl, DIEPCTL(hs_ep->index));
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002939 }
2940
2941 dwc2_hsotg_complete_request(hsotg, hs_ep,
2942 get_ep_head(hs_ep), 0);
2943 }
2944
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002945 if (!using_desc_dma(hsotg))
2946 dwc2_gadget_incr_frame_num(hs_ep);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002947}
2948
2949/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002950 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002951 * @hsotg: The driver state
2952 * @idx: The index for the endpoint (0..15)
2953 * @dir_in: Set if this is an IN endpoint
2954 *
2955 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002956 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002957static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002958 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002959{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002960 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002961 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2962 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2963 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002964 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002965 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002966
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002967 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002968 ctrl = dwc2_readl(hsotg, epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002969
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002970 /* Clear endpoint interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002971 dwc2_writel(hsotg, ints, epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002972
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002973 if (!hs_ep) {
2974 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002975 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002976 return;
2977 }
2978
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002979 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2980 __func__, idx, dir_in ? "in" : "out", ints);
2981
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002982 /* Don't process XferCompl interrupt if it is a setup packet */
2983 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2984 ints &= ~DXEPINT_XFERCOMPL;
2985
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002986 /*
2987 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2988 * stage and xfercomplete was generated without SETUP phase done
2989 * interrupt. SW should parse received setup packet only after host's
2990 * exit from setup phase of control transfer.
2991 */
2992 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2993 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2994 ints &= ~DXEPINT_XFERCOMPL;
2995
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002996 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002997 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002998 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04002999 __func__, dwc2_readl(hsotg, epctl_reg),
3000 dwc2_readl(hsotg, epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003001
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003002 /* In DDMA handle isochronous requests separately */
3003 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003004 /* XferCompl set along with BNA */
3005 if (!(ints & DXEPINT_BNAINTR))
3006 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003007 } else if (dir_in) {
3008 /*
3009 * We get OutDone from the FIFO, so we only
3010 * need to look at completing IN requests here
3011 * if operating slave mode
3012 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003013 if (hs_ep->isochronous && hs_ep->interval > 1)
3014 dwc2_gadget_incr_frame_num(hs_ep);
3015
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003016 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003017 if (ints & DXEPINT_NAKINTRPT)
3018 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003019
Ben Dooksc9a64ea2010-07-19 09:40:46 +01003020 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003021 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003022 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003023 /*
3024 * We're using DMA, we need to fire an OutDone here
3025 * as we ignore the RXFIFO.
3026 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003027 if (hs_ep->isochronous && hs_ep->interval > 1)
3028 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003029
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003030 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003031 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003032 }
3033
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07003034 if (ints & DXEPINT_EPDISBLD)
3035 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003036
Vardan Mikayelyan53219222016-05-25 18:07:14 -07003037 if (ints & DXEPINT_OUTTKNEPDIS)
3038 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
3039
3040 if (ints & DXEPINT_NAKINTRPT)
3041 dwc2_gadget_handle_nak(hs_ep);
3042
Dinh Nguyen47a16852014-04-14 14:13:34 -07003043 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003044 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003045
Dinh Nguyen47a16852014-04-14 14:13:34 -07003046 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003047 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
3048
3049 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003050 /*
3051 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003052 * setup packet. In non-DMA mode we'd get this
3053 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003054 * the setup here.
3055 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003056
3057 if (dir_in)
3058 WARN_ON_ONCE(1);
3059 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003060 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003061 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003062 }
3063
Vahram Aharonyanef750c72016-11-14 19:16:31 -08003064 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003065 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
3066
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04003067 /* Safety check EP0 state when STSPHSERCVD asserted */
3068 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
3069 /* Move to STATUS IN for DDMA */
Minas Harutyunyanb4c53b42019-03-12 11:45:12 +04003070 if (using_desc_dma(hsotg)) {
3071 if (!hsotg->delayed_status)
3072 dwc2_hsotg_ep0_zlp(hsotg, true);
3073 else
3074 /* In case of 3 stage Control Write with delayed
3075 * status, when Status IN transfer started
3076 * before STSPHSERCVD asserted, NAKSTS bit not
3077 * cleared by CNAK in dwc2_hsotg_start_req()
3078 * function. Clear now NAKSTS to allow complete
3079 * transfer.
3080 */
3081 dwc2_set_bit(hsotg, DIEPCTL(0),
3082 DXEPCTL_CNAK);
3083 }
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04003084 }
3085
Vahram Aharonyanef750c72016-11-14 19:16:31 -08003086 }
3087
Dinh Nguyen47a16852014-04-14 14:13:34 -07003088 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003089 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003090
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003091 if (ints & DXEPINT_BNAINTR) {
3092 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003093 if (hs_ep->isochronous)
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003094 dwc2_gadget_handle_isoc_bna(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08003095 }
3096
Robert Baldyga1479e842013-10-09 08:41:57 +02003097 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003098 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003099 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003100 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
3101 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003102 }
3103
3104 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003105 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003106 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
3107 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003108 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003109
3110 /* FIFO has space or is empty (see GAHBCFG) */
3111 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003112 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01003113 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3114 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09003115 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003116 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01003117 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003118 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003119}
3120
3121/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003122 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003123 * @hsotg: The device state.
3124 *
3125 * Handle updating the device settings after the enumeration phase has
3126 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003127 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003128static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003129{
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003130 u32 dsts = dwc2_readl(hsotg, DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003131 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003132
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003133 /*
3134 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003135 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003136 * we connected at.
3137 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003138
3139 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3140
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003141 /*
3142 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003143 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003144 * not advertise a 64byte MPS on EP0.
3145 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003146
3147 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003148 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003149 case DSTS_ENUMSPD_FS:
3150 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003151 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003152 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003153 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003154 break;
3155
Dinh Nguyen47a16852014-04-14 14:13:34 -07003156 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003157 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003158 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003159 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003160 break;
3161
Dinh Nguyen47a16852014-04-14 14:13:34 -07003162 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003163 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003164 ep0_mps = 8;
3165 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003166 /*
3167 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003168 * moment, and the documentation seems to imply that it isn't
3169 * supported by the PHYs on some of the devices.
3170 */
3171 break;
3172 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003173 dev_info(hsotg->dev, "new device is %s\n",
3174 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003175
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003176 /*
3177 * we should now know the maximum packet size for an
3178 * endpoint, so set the endpoints to a default value.
3179 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003180
3181 if (ep0_mps) {
3182 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003183 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003184 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3185 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003186 for (i = 1; i < hsotg->num_of_eps; i++) {
3187 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003188 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3189 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003190 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003191 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3192 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003193 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003194 }
3195
3196 /* ensure after enumeration our EP0 is active */
3197
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003198 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003199
3200 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003201 dwc2_readl(hsotg, DIEPCTL0),
3202 dwc2_readl(hsotg, DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003203}
3204
3205/**
3206 * kill_all_requests - remove all requests from the endpoint's queue
3207 * @hsotg: The device state.
3208 * @ep: The endpoint the requests may be on.
3209 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003210 *
3211 * Go through the requests on the given endpoint and mark them
3212 * completed with the given result code.
3213 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003214static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003215 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af42014-12-16 11:51:44 +01003216 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003217{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003218 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003219 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003220
Robert Baldyga6b448af42014-12-16 11:51:44 +01003221 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003222
Robert Baldyga6b448af42014-12-16 11:51:44 +01003223 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003224 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003225 result);
Robert Baldyga6b448af42014-12-16 11:51:44 +01003226
Robert Baldygab203d0a2014-09-09 10:44:56 +02003227 if (!hsotg->dedicated_fifos)
3228 return;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003229 size = (dwc2_readl(hsotg, DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003230 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003231 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003232}
3233
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003234/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003235 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003236 * @hsotg: The device state.
3237 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003238 * The device has been disconnected. Remove all current
3239 * transactions and signal the gadget driver that this
3240 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003241 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003242void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003243{
John Youn9da51972017-01-17 20:30:27 -08003244 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003245
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003246 if (!hsotg->connected)
3247 return;
3248
3249 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003250 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003251
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003252 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003253 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3254 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003255 kill_all_requests(hsotg, hsotg->eps_in[ep],
3256 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003257 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003258 kill_all_requests(hsotg, hsotg->eps_out[ep],
3259 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003260 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003261
3262 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003263 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003264
3265 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003266}
3267
3268/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003269 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003270 * @hsotg: The device state:
3271 * @periodic: True if this is a periodic FIFO interrupt
3272 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003273static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003274{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003275 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003276 int epno, ret;
3277
3278 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003279 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003280 ep = index_to_ep(hsotg, epno, 1);
3281
3282 if (!ep)
3283 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003284
3285 if (!ep->dir_in)
3286 continue;
3287
3288 if ((periodic && !ep->periodic) ||
3289 (!periodic && ep->periodic))
3290 continue;
3291
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003292 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003293 if (ret < 0)
3294 break;
3295 }
3296}
3297
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003298/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003299#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3300 GINTSTS_PTXFEMP | \
3301 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003302
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04003303static int dwc2_hsotg_ep_disable(struct usb_ep *ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003304/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003305 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003306 * @hsotg: The device state
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04003307 * @is_usb_reset: Usb resetting flag
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003308 *
3309 * Issue a soft reset to the core, and await the core finishing it.
3310 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003311void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003312 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003313{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003314 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003315 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003316 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003317 u32 dcfg = 0;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003318 int ep;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003319
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003320 /* Kill any ep0 requests as controller will be reinitialized */
3321 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3322
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003323 if (!is_usb_reset) {
John Stultz6e6360b2017-01-23 14:59:14 -08003324 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003325 return;
Minas Harutyunyandccf1ba2018-09-19 18:13:52 +04003326 } else {
3327 /* all endpoints should be shutdown */
3328 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3329 if (hsotg->eps_in[ep])
3330 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
3331 if (hsotg->eps_out[ep])
3332 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
3333 }
3334 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003335
3336 /*
3337 * we must now enable ep0 ready for host detection and then
3338 * set configuration.
3339 */
3340
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003341 /* keep other bits untouched (so e.g. forced modes are not lost) */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003342 usbcfg = dwc2_readl(hsotg, GUSBCFG);
Jules Maselbas1e868542019-04-05 15:35:33 +02003343 usbcfg &= ~GUSBCFG_TOUTCAL_MASK;
Jules Maselbas707d80f2019-04-05 15:35:31 +02003344 usbcfg |= GUSBCFG_TOUTCAL(7);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003345
Jules Maselbas1e868542019-04-05 15:35:33 +02003346 /* remove the HNP/SRP and set the PHY */
3347 usbcfg &= ~(GUSBCFG_SRPCAP | GUSBCFG_HNPCAP);
3348 dwc2_writel(hsotg, usbcfg, GUSBCFG);
Jules Maselbas707d80f2019-04-05 15:35:31 +02003349
Jules Maselbas1e868542019-04-05 15:35:33 +02003350 dwc2_phy_init(hsotg, true);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003351
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003352 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003353
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003354 if (!is_usb_reset)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003355 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003356
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003357 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003358
3359 switch (hsotg->params.speed) {
3360 case DWC2_SPEED_PARAM_LOW:
3361 dcfg |= DCFG_DEVSPD_LS;
3362 break;
3363 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003364 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3365 dcfg |= DCFG_DEVSPD_FS48;
3366 else
3367 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003368 break;
3369 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003370 dcfg |= DCFG_DEVSPD_HS;
3371 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003372
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +04003373 if (hsotg->params.ipg_isoc_en)
3374 dcfg |= DCFG_IPG_ISOC_SUPPORDED;
3375
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003376 dwc2_writel(hsotg, dcfg, DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003377
3378 /* Clear any pending OTG interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003379 dwc2_writel(hsotg, 0xffffffff, GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003380
3381 /* Clear any pending interrupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003382 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003383 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003384 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003385 GINTSTS_USBRST | GINTSTS_RESETDET |
3386 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003387 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3388 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003389
3390 if (!using_desc_dma(hsotg))
3391 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003392
John Youn95832c02017-01-23 14:57:26 -08003393 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003394 intmsk |= GINTSTS_CONIDSTSCHNG;
3395
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003396 dwc2_writel(hsotg, intmsk, GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003397
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003398 if (using_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003399 dwc2_writel(hsotg, GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003400 hsotg->params.ahbcfg,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003401 GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003402
3403 /* Set DDMA mode support in the core if needed */
3404 if (using_desc_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003405 dwc2_set_bit(hsotg, DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003406
3407 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003408 dwc2_writel(hsotg, ((hsotg->dedicated_fifos) ?
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003409 (GAHBCFG_NP_TXF_EMP_LVL |
3410 GAHBCFG_P_TXF_EMP_LVL) : 0) |
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003411 GAHBCFG_GLBL_INTR_EN, GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003412 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003413
3414 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003415 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3416 * when we have no data to transfer. Otherwise we get being flooded by
3417 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003418 */
3419
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003420 dwc2_writel(hsotg, ((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003421 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003422 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003423 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003424 DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003425
3426 /*
3427 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003428 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003429 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003430 dwc2_writel(hsotg, (using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003431 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003432 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003433 DOEPMSK_SETUPMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003434 DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003435
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003436 /* Enable BNA interrupt for DDMA */
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003437 if (using_desc_dma(hsotg)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003438 dwc2_set_bit(hsotg, DOEPMSK, DOEPMSK_BNAMSK);
3439 dwc2_set_bit(hsotg, DIEPMSK, DIEPMSK_BNAININTRMSK);
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003440 }
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003441
Grigor Tovmasyanca531bc2018-08-29 20:59:34 +04003442 /* Enable Service Interval mode if supported */
3443 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3444 dwc2_set_bit(hsotg, DCTL, DCTL_SERVICE_INTERVAL_SUPPORTED);
3445
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003446 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003447
3448 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003449 dwc2_readl(hsotg, DIEPCTL0),
3450 dwc2_readl(hsotg, DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003451
3452 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003453 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003454
3455 /*
3456 * Enable the RXFIFO when in slave mode, as this is how we collect
3457 * the data. In DMA mode, we get events from the FIFO but also
3458 * things we cannot process, so do not use it.
3459 */
3460 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003461 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003462
3463 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003464 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3465 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003466
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003467 if (!is_usb_reset) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003468 dwc2_set_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003469 udelay(10); /* see openiboot */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003470 dwc2_clear_bit(hsotg, DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003471 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003472
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003473 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg, DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003474
3475 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003476 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003477 * writing to the EPCTL register..
3478 */
3479
3480 /* set to read 1 8byte packet */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003481 dwc2_writel(hsotg, DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
3482 DXEPTSIZ_XFERSIZE(8), DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003483
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003484 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003485 DXEPCTL_CNAK | DXEPCTL_EPENA |
3486 DXEPCTL_USBACTEP,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003487 DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003488
3489 /* enable, but don't activate EP0in */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003490 dwc2_writel(hsotg, dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
3491 DXEPCTL_USBACTEP, DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003492
Lukasz Majewski308d7342012-05-04 14:17:05 +02003493 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003494 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3495 if (!is_usb_reset)
3496 val |= DCTL_SFTDISCON;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003497 dwc2_set_bit(hsotg, DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003498
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003499 /* configure the core to support LPM */
3500 dwc2_gadget_init_lpm(hsotg);
3501
Grigor Tovmasyan15d9dbf2018-08-29 21:01:59 +04003502 /* program GREFCLK register if needed */
3503 if (using_desc_dma(hsotg) && hsotg->params.service_interval)
3504 dwc2_gadget_program_ref_clk(hsotg);
3505
Lukasz Majewski308d7342012-05-04 14:17:05 +02003506 /* must be at-least 3ms to allow bus to see disconnect */
3507 mdelay(3);
3508
Gregory Herrero065d3932015-09-22 15:16:54 +02003509 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003510
3511 dwc2_hsotg_enqueue_setup(hsotg);
3512
3513 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003514 dwc2_readl(hsotg, DIEPCTL0),
3515 dwc2_readl(hsotg, DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003516}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003517
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003518static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003519{
3520 /* set the soft-disconnect bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003521 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003522}
3523
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003524void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003525{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003526 /* remove the soft-disconnect and let's go */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003527 dwc2_clear_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003528}
3529
3530/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003531 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3532 * @hsotg: The device state:
3533 *
3534 * This interrupt indicates one of the following conditions occurred while
3535 * transmitting an ISOC transaction.
3536 * - Corrupted IN Token for ISOC EP.
3537 * - Packet not complete in FIFO.
3538 *
3539 * The following actions will be taken:
3540 * - Determine the EP
3541 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3542 */
3543static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3544{
3545 struct dwc2_hsotg_ep *hs_ep;
3546 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003547 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003548 u32 idx;
3549
3550 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3551
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003552 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003553
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003554 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003555 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003556 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003557 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003558 continue;
3559
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003560 epctrl = dwc2_readl(hsotg, DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003561 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003562 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3563 epctrl |= DXEPCTL_SNAK;
3564 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003565 dwc2_writel(hsotg, epctrl, DIEPCTL(idx));
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003566 }
3567 }
3568
3569 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003570 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOIN, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003571}
3572
3573/**
3574 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3575 * @hsotg: The device state:
3576 *
3577 * This interrupt indicates one of the following conditions occurred while
3578 * transmitting an ISOC transaction.
3579 * - Corrupted OUT Token for ISOC EP.
3580 * - Packet not complete in FIFO.
3581 *
3582 * The following actions will be taken:
3583 * - Determine the EP
3584 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3585 */
3586static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3587{
3588 u32 gintsts;
3589 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003590 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003591 u32 epctrl;
3592 struct dwc2_hsotg_ep *hs_ep;
3593 int idx;
3594
3595 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3596
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003597 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003598 daintmsk >>= DAINT_OUTEP_SHIFT;
3599
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003600 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003601 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003602 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003603 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003604 continue;
3605
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003606 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003607 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003608 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3609 /* Unmask GOUTNAKEFF interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003610 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003611 gintmsk |= GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003612 dwc2_writel(hsotg, gintmsk, GINTMSK);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003613
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003614 gintsts = dwc2_readl(hsotg, GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003615 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003616 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003617 break;
3618 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003619 }
3620 }
3621
3622 /* Clear interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003623 dwc2_writel(hsotg, GINTSTS_INCOMPL_SOOUT, GINTSTS);
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003624}
3625
3626/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003627 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003628 * @irq: The IRQ number triggered
3629 * @pw: The pw value when registered the handler.
3630 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003631static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003632{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003633 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003634 int retry_count = 8;
3635 u32 gintsts;
3636 u32 gintmsk;
3637
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003638 if (!dwc2_is_device_mode(hsotg))
3639 return IRQ_NONE;
3640
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003641 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003642irq_retry:
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003643 gintsts = dwc2_readl(hsotg, GINTSTS);
3644 gintmsk = dwc2_readl(hsotg, GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003645
3646 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3647 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3648
3649 gintsts &= gintmsk;
3650
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003651 if (gintsts & GINTSTS_RESETDET) {
3652 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3653
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003654 dwc2_writel(hsotg, GINTSTS_RESETDET, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003655
3656 /* This event must be used only if controller is suspended */
3657 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003658 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003659 hsotg->lx_state = DWC2_L0;
3660 }
3661 }
3662
3663 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003664 u32 usb_status = dwc2_readl(hsotg, GOTGCTL);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003665 u32 connected = hsotg->connected;
3666
3667 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3668 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003669 dwc2_readl(hsotg, GNPTXSTS));
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003670
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003671 dwc2_writel(hsotg, GINTSTS_USBRST, GINTSTS);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003672
3673 /* Report disconnection if it is not already done. */
3674 dwc2_hsotg_disconnect(hsotg);
3675
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003676 /* Reset device address to zero */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003677 dwc2_clear_bit(hsotg, DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003678
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003679 if (usb_status & GOTGCTL_BSESVLD && connected)
3680 dwc2_hsotg_core_init_disconnected(hsotg, true);
3681 }
3682
Dinh Nguyen47a16852014-04-14 14:13:34 -07003683 if (gintsts & GINTSTS_ENUMDONE) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003684 dwc2_writel(hsotg, GINTSTS_ENUMDONE, GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003685
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003686 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003687 }
3688
Dinh Nguyen47a16852014-04-14 14:13:34 -07003689 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003690 u32 daint = dwc2_readl(hsotg, DAINT);
3691 u32 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003692 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003693 int ep;
3694
Robert Baldyga7e804652013-09-19 11:50:20 +02003695 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003696 daint_out = daint >> DAINT_OUTEP_SHIFT;
3697 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003698
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003699 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3700
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003701 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3702 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003703 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003704 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003705 }
3706
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003707 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3708 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003709 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003710 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003711 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003712 }
3713
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003714 /* check both FIFOs */
3715
Dinh Nguyen47a16852014-04-14 14:13:34 -07003716 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003717 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3718
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003719 /*
3720 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003721 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003722 * it needs re-enabling
3723 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003724
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003725 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3726 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003727 }
3728
Dinh Nguyen47a16852014-04-14 14:13:34 -07003729 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003730 dev_dbg(hsotg->dev, "PTxFEmp\n");
3731
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003732 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003733
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003734 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3735 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003736 }
3737
Dinh Nguyen47a16852014-04-14 14:13:34 -07003738 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003739 /*
3740 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003741 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003742 * set.
3743 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003744
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003745 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003746 }
3747
Dinh Nguyen47a16852014-04-14 14:13:34 -07003748 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003749 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003750 dwc2_writel(hsotg, GINTSTS_ERLYSUSP, GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003751 }
3752
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003753 /*
3754 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003755 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003756 * the occurrence.
3757 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003758
Dinh Nguyen47a16852014-04-14 14:13:34 -07003759 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003760 u8 idx;
3761 u32 epctrl;
3762 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003763 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003764 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003765
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003766 daintmsk = dwc2_readl(hsotg, DAINTMSK);
Razmik Karapetyand8484552018-01-19 14:41:42 +04003767 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003768 /* Mask this interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003769 gintmsk = dwc2_readl(hsotg, GINTMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003770 gintmsk &= ~GINTSTS_GOUTNAKEFF;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003771 dwc2_writel(hsotg, gintmsk, GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003772
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003773 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003774 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003775 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003776 /* Proceed only unmasked ISOC EPs */
John Keeping89066b32018-07-15 15:50:43 +01003777 if ((BIT(idx) & ~daintmsk) || !hs_ep->isochronous)
Razmik Karapetyand8484552018-01-19 14:41:42 +04003778 continue;
3779
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003780 epctrl = dwc2_readl(hsotg, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003781
Razmik Karapetyand8484552018-01-19 14:41:42 +04003782 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003783 epctrl |= DXEPCTL_SNAK;
3784 epctrl |= DXEPCTL_EPDIS;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003785 dwc2_writel(hsotg, epctrl, DOEPCTL(idx));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003786 }
3787 }
3788
3789 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003790 }
3791
Dinh Nguyen47a16852014-04-14 14:13:34 -07003792 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003793 dev_info(hsotg->dev, "GINNakEff triggered\n");
3794
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003795 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003796
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003797 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003798 }
3799
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003800 if (gintsts & GINTSTS_INCOMPL_SOIN)
3801 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003802
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003803 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3804 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003805
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003806 /*
3807 * if we've had fifo events, we should try and go around the
3808 * loop again to see if there's any point in returning yet.
3809 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003810
3811 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003812 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003813
Grigor Tovmasyan187c5292018-08-29 21:02:57 +04003814 /* Check WKUP_ALERT interrupt*/
3815 if (hsotg->params.service_interval)
3816 dwc2_gadget_wkup_alert_handler(hsotg);
3817
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003818 spin_unlock(&hsotg->lock);
3819
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003820 return IRQ_HANDLED;
3821}
3822
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003823static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3824 struct dwc2_hsotg_ep *hs_ep)
3825{
3826 u32 epctrl_reg;
3827 u32 epint_reg;
3828
3829 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3830 DOEPCTL(hs_ep->index);
3831 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3832 DOEPINT(hs_ep->index);
3833
3834 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3835 hs_ep->name);
3836
3837 if (hs_ep->dir_in) {
3838 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003839 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003840 /* Wait for Nak effect */
3841 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3842 DXEPINT_INEPNAKEFF, 100))
3843 dev_warn(hsotg->dev,
3844 "%s: timeout DIEPINT.NAKEFF\n",
3845 __func__);
3846 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003847 dwc2_set_bit(hsotg, DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003848 /* Wait for Nak effect */
3849 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3850 GINTSTS_GINNAKEFF, 100))
3851 dev_warn(hsotg->dev,
3852 "%s: timeout GINTSTS.GINNAKEFF\n",
3853 __func__);
3854 }
3855 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003856 if (!(dwc2_readl(hsotg, GINTSTS) & GINTSTS_GOUTNAKEFF))
3857 dwc2_set_bit(hsotg, DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003858
3859 /* Wait for global nak to take effect */
3860 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3861 GINTSTS_GOUTNAKEFF, 100))
3862 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3863 __func__);
3864 }
3865
3866 /* Disable ep */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003867 dwc2_set_bit(hsotg, epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003868
3869 /* Wait for ep to be disabled */
3870 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3871 dev_warn(hsotg->dev,
3872 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3873
3874 /* Clear EPDISBLD interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003875 dwc2_set_bit(hsotg, epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003876
3877 if (hs_ep->dir_in) {
3878 unsigned short fifo_index;
3879
3880 if (hsotg->dedicated_fifos || hs_ep->periodic)
3881 fifo_index = hs_ep->fifo_index;
3882 else
3883 fifo_index = 0;
3884
3885 /* Flush TX FIFO */
3886 dwc2_flush_tx_fifo(hsotg, fifo_index);
3887
3888 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3889 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003890 dwc2_set_bit(hsotg, DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003891
3892 } else {
3893 /* Remove global NAKs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003894 dwc2_set_bit(hsotg, DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003895 }
3896}
3897
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003898/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003899 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003900 * @ep: The USB endpint to configure
3901 * @desc: The USB endpoint descriptor to configure with.
3902 *
3903 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003904 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003905static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003906 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003907{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003908 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003909 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003910 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003911 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003912 u32 epctrl_reg;
3913 u32 epctrl;
3914 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003915 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003916 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003917 unsigned int dir_in;
3918 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003919 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003920 unsigned char ep_type;
Minas Harutyunyan54f37f52019-03-18 14:24:30 +04003921 int desc_num;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003922
3923 dev_dbg(hsotg->dev,
3924 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3925 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3926 desc->wMaxPacketSize, desc->bInterval);
3927
3928 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003929 if (index == 0) {
3930 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3931 return -EINVAL;
3932 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003933
3934 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3935 if (dir_in != hs_ep->dir_in) {
3936 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3937 return -EINVAL;
3938 }
3939
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003940 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003941 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003942 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003943
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003944 /* ISOC IN in DDMA supported bInterval up to 10 */
3945 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3946 dir_in && desc->bInterval > 10) {
3947 dev_err(hsotg->dev,
3948 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3949 return -EINVAL;
3950 }
3951
3952 /* High bandwidth ISOC OUT in DDMA not supported */
3953 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3954 !dir_in && mc > 1) {
3955 dev_err(hsotg->dev,
3956 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3957 return -EINVAL;
3958 }
3959
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003960 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003961
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003962 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04003963 epctrl = dwc2_readl(hsotg, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003964
3965 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3966 __func__, epctrl, epctrl_reg);
3967
Minas Harutyunyan54f37f52019-03-18 14:24:30 +04003968 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC)
3969 desc_num = MAX_DMA_DESC_NUM_HS_ISOC;
3970 else
3971 desc_num = MAX_DMA_DESC_NUM_GENERIC;
3972
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003973 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003974 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3975 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Minas Harutyunyan54f37f52019-03-18 14:24:30 +04003976 desc_num * sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003977 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003978 if (!hs_ep->desc_list) {
3979 ret = -ENOMEM;
3980 goto error2;
3981 }
3982 }
3983
Lukasz Majewski22258f42012-06-14 10:02:24 +02003984 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003985
Dinh Nguyen47a16852014-04-14 14:13:34 -07003986 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3987 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003988
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003989 /*
3990 * mark the endpoint as active, otherwise the core may ignore
3991 * transactions entirely for this endpoint
3992 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003993 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003994
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003995 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003996 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003997
3998 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003999 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004000 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004001 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02004002 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02004003
Minas Harutyunyan729cac62018-05-03 17:24:28 +04004004 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004005 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07004006 epctrl |= DXEPCTL_EPTYPE_ISO;
4007 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02004008 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07004009 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004010 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08004011 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04004012 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004013 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02004014 hs_ep->periodic = 1;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004015 mask = dwc2_readl(hsotg, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004016 mask |= DIEPMSK_NAKMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004017 dwc2_writel(hsotg, mask, DIEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004018 } else {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004019 mask = dwc2_readl(hsotg, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004020 mask |= DOEPMSK_OUTTKNEPDISMSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004021 dwc2_writel(hsotg, mask, DOEPMSK);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004022 }
Robert Baldyga1479e842013-10-09 08:41:57 +02004023 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004024
4025 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07004026 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004027 break;
4028
4029 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02004030 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004031 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004032
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07004033 if (hsotg->gadget.speed == USB_SPEED_HIGH)
4034 hs_ep->interval = 1 << (desc->bInterval - 1);
4035
Dinh Nguyen47a16852014-04-14 14:13:34 -07004036 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004037 break;
4038
4039 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07004040 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004041 break;
4042 }
4043
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004044 /*
4045 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01004046 * a unique tx-fifo even if it is non-periodic.
4047 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07004048 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004049 u32 fifo_index = 0;
4050 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08004051
4052 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01004053 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08004054 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02004055 continue;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004056 val = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08004057 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02004058 if (val < size)
4059 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004060 /* Search for smallest acceptable fifo */
4061 if (val < fifo_size) {
4062 fifo_size = val;
4063 fifo_index = i;
4064 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02004065 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004066 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01004067 dev_err(hsotg->dev,
4068 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05304069 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004070 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05304071 }
Minas Harutyunyan97311c82019-01-31 18:28:07 +04004072 epctrl &= ~(DXEPCTL_TXFNUM_LIMIT << DXEPCTL_TXFNUM_SHIFT);
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01004073 hsotg->fifo_map |= 1 << fifo_index;
4074 epctrl |= DXEPCTL_TXFNUM(fifo_index);
4075 hs_ep->fifo_index = fifo_index;
4076 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02004077 }
Ben Dooks10aebc72010-07-19 09:40:44 +01004078
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004079 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07004080 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07004081 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004082
Artur Petrosyan52953222018-04-16 08:45:31 -04004083 /* WA for Full speed ISOC IN in DDMA mode.
4084 * By Clear NAK status of EP, core will send ZLP
4085 * to IN token and assert NAK interrupt relying
4086 * on TxFIFO status only
4087 */
4088
4089 if (hsotg->gadget.speed == USB_SPEED_FULL &&
4090 hs_ep->isochronous && dir_in) {
4091 /* The WA applies only to core versions from 2.72a
4092 * to 4.00a (including both). Also for FS_IOT_1.00a
4093 * and HS_IOT_1.00a.
4094 */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004095 u32 gsnpsid = dwc2_readl(hsotg, GSNPSID);
Artur Petrosyan52953222018-04-16 08:45:31 -04004096
4097 if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
4098 gsnpsid <= DWC2_CORE_REV_4_00a) ||
4099 gsnpsid == DWC2_FS_IOT_REV_1_00a ||
4100 gsnpsid == DWC2_HS_IOT_REV_1_00a)
4101 epctrl |= DXEPCTL_CNAK;
4102 }
4103
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004104 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
4105 __func__, epctrl);
4106
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004107 dwc2_writel(hsotg, epctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004108 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004109 __func__, dwc2_readl(hsotg, epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004110
4111 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004112 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004113
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004114error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02004115 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004116
4117error2:
4118 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Minas Harutyunyan54f37f52019-03-18 14:24:30 +04004119 dmam_free_coherent(hsotg->dev, desc_num *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08004120 sizeof(struct dwc2_dma_desc),
4121 hs_ep->desc_list, hs_ep->desc_list_dma);
4122 hs_ep->desc_list = NULL;
4123 }
4124
Julia Lawall19c190f2010-03-29 17:36:44 +02004125 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004126}
4127
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004128/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004129 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004130 * @ep: The endpoint to disable.
4131 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004132static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004133{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004134 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004135 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004136 int dir_in = hs_ep->dir_in;
4137 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004138 u32 epctrl_reg;
4139 u32 ctrl;
4140
Marek Szyprowski1e011292014-09-09 10:44:54 +02004141 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004142
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004143 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004144 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4145 return -EINVAL;
4146 }
4147
John Stultz9b4810922017-10-23 14:32:49 -07004148 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4149 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4150 return -EINVAL;
4151 }
4152
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02004153 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004154
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004155 ctrl = dwc2_readl(hsotg, epctrl_reg);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08004156
4157 if (ctrl & DXEPCTL_EPENA)
4158 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4159
Dinh Nguyen47a16852014-04-14 14:13:34 -07004160 ctrl &= ~DXEPCTL_EPENA;
4161 ctrl &= ~DXEPCTL_USBACTEP;
4162 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004163
4164 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004165 dwc2_writel(hsotg, ctrl, epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004166
4167 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004168 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004169
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01004170 /* terminate all requests with shutdown */
4171 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4172
Robert Baldyga1c07b202016-08-29 13:39:00 -07004173 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4174 hs_ep->fifo_index = 0;
4175 hs_ep->fifo_size = 0;
4176
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004177 return 0;
4178}
4179
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004180static int dwc2_hsotg_ep_disable_lock(struct usb_ep *ep)
4181{
4182 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
4183 struct dwc2_hsotg *hsotg = hs_ep->parent;
4184 unsigned long flags;
4185 int ret;
4186
4187 spin_lock_irqsave(&hsotg->lock, flags);
4188 ret = dwc2_hsotg_ep_disable(ep);
4189 spin_unlock_irqrestore(&hsotg->lock, flags);
4190 return ret;
4191}
4192
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004193/**
4194 * on_list - check request is on the given endpoint
4195 * @ep: The endpoint to check.
4196 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004197 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004198static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004199{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004200 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004201
4202 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4203 if (req == test)
4204 return true;
4205 }
4206
4207 return false;
4208}
4209
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004210/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004211 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004212 * @ep: The endpoint to dequeue.
4213 * @req: The request to be removed from a queue.
4214 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004215static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004216{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004217 struct dwc2_hsotg_req *hs_req = our_req(req);
4218 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004219 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004220 unsigned long flags;
4221
Marek Szyprowski1e011292014-09-09 10:44:54 +02004222 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004223
Lukasz Majewski22258f42012-06-14 10:02:24 +02004224 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004225
4226 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004227 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004228 return -EINVAL;
4229 }
4230
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004231 /* Dequeue already started request */
4232 if (req == &hs_ep->req->req)
4233 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4234
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004235 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004236 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004237
4238 return 0;
4239}
4240
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004241/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004242 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004243 * @ep: The endpoint to set halt.
4244 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004245 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4246 * the endpoint is busy processing requests.
4247 *
4248 * We need to stall the endpoint immediately if request comes from set_feature
4249 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004250 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004251static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004252{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004253 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004254 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004255 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004256 u32 epreg;
4257 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004258 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004259
4260 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4261
Robert Baldygac9f721b2014-01-14 08:36:00 +01004262 if (index == 0) {
4263 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004264 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004265 else
4266 dev_warn(hs->dev,
4267 "%s: can't clear halt on ep0\n", __func__);
4268 return 0;
4269 }
4270
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004271 if (hs_ep->isochronous) {
4272 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4273 return -EINVAL;
4274 }
4275
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004276 if (!now && value && !list_empty(&hs_ep->queue)) {
4277 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4278 ep->name);
4279 return -EAGAIN;
4280 }
4281
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004282 if (hs_ep->dir_in) {
4283 epreg = DIEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004284 epctl = dwc2_readl(hs, epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004285
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004286 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004287 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004288 if (epctl & DXEPCTL_EPENA)
4289 epctl |= DXEPCTL_EPDIS;
4290 } else {
4291 epctl &= ~DXEPCTL_STALL;
4292 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4293 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004294 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004295 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004296 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004297 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004298 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004299 epreg = DOEPCTL(index);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004300 epctl = dwc2_readl(hs, epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004301
John Youn34c0887f2017-01-17 20:31:43 -08004302 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004303 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004304 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004305 epctl &= ~DXEPCTL_STALL;
4306 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4307 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004308 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004309 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004310 }
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004311 dwc2_writel(hs, epctl, epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004312 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004313
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004314 hs_ep->halted = value;
4315
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004316 return 0;
4317}
4318
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004319/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004320 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004321 * @ep: The endpoint to set halt.
4322 * @value: Set or unset the halt.
4323 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004324static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004325{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004326 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004327 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004328 unsigned long flags = 0;
4329 int ret = 0;
4330
4331 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004332 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004333 spin_unlock_irqrestore(&hs->lock, flags);
4334
4335 return ret;
4336}
4337
Bhumika Goyalebce5612017-08-12 17:34:55 +05304338static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004339 .enable = dwc2_hsotg_ep_enable,
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004340 .disable = dwc2_hsotg_ep_disable_lock,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004341 .alloc_request = dwc2_hsotg_ep_alloc_request,
4342 .free_request = dwc2_hsotg_ep_free_request,
4343 .queue = dwc2_hsotg_ep_queue_lock,
4344 .dequeue = dwc2_hsotg_ep_dequeue,
4345 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004346 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004347};
4348
4349/**
John Youn9da51972017-01-17 20:30:27 -08004350 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004351 * @hsotg: The driver state
4352 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004353static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004354{
4355 /* unmask subset of endpoint interrupts */
4356
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004357 dwc2_writel(hsotg, DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004358 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004359 DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004360
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004361 dwc2_writel(hsotg, DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004362 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004363 DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004364
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004365 dwc2_writel(hsotg, 0, DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004366
4367 /* Be in disconnected state until gadget is registered */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004368 dwc2_set_bit(hsotg, DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004369
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004370 /* setup fifos */
4371
4372 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004373 dwc2_readl(hsotg, GRXFSIZ),
4374 dwc2_readl(hsotg, GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004375
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004376 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004377
Gregory Herrerof5090042015-01-09 13:38:47 +01004378 if (using_dma(hsotg))
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004379 dwc2_set_bit(hsotg, GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004380}
4381
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004382/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004383 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004384 * @gadget: The usb gadget state
4385 * @driver: The usb gadget driver
4386 *
4387 * Perform initialization to prepare udc device and driver
4388 * to work.
4389 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004390static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004391 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004392{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004393 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004394 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004395 int ret;
4396
4397 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004398 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004399 return -ENODEV;
4400 }
4401
4402 if (!driver) {
4403 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4404 return -EINVAL;
4405 }
4406
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004407 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004408 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004409
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004410 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004411 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4412 return -EINVAL;
4413 }
4414
4415 WARN_ON(hsotg->driver);
4416
4417 driver->driver.bus = NULL;
4418 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004419 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004420 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4421
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004422 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4423 ret = dwc2_lowlevel_hw_enable(hsotg);
4424 if (ret)
4425 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004426 }
4427
Gregory Herrerof6c01592015-01-09 13:38:41 +01004428 if (!IS_ERR_OR_NULL(hsotg->uphy))
4429 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004430
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004431 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004432 if (dwc2_hw_is_device(hsotg)) {
4433 dwc2_hsotg_init(hsotg);
4434 dwc2_hsotg_core_init_disconnected(hsotg, false);
4435 }
4436
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004437 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004438 spin_unlock_irqrestore(&hsotg->lock, flags);
4439
Andrzej Pietrasiewicz10209ab2019-01-21 14:44:47 +01004440 gadget->sg_supported = using_desc_dma(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004441 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004442
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004443 return 0;
4444
4445err:
4446 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004447 return ret;
4448}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004449
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004450/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004451 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004452 * @gadget: The usb gadget state
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004453 *
4454 * Stop udc hw block and stay tunned for future transmissions
4455 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004456static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004457{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004458 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004459 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004460 int ep;
4461
4462 if (!hsotg)
4463 return -ENODEV;
4464
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004465 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004466 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4467 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004468 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004469 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004470 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004471 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004472
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004473 spin_lock_irqsave(&hsotg->lock, flags);
4474
Marek Szyprowski32805c32014-10-20 12:45:33 +02004475 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004476 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004477 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004478
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004479 spin_unlock_irqrestore(&hsotg->lock, flags);
4480
Gregory Herrerof6c01592015-01-09 13:38:41 +01004481 if (!IS_ERR_OR_NULL(hsotg->uphy))
4482 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004483
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004484 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4485 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004486
4487 return 0;
4488}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004489
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004490/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004491 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004492 * @gadget: The usb gadget state
4493 *
4494 * Read the {micro} frame number
4495 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004496static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004497{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004498 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004499}
4500
Lukasz Majewskia188b682012-06-22 09:29:56 +02004501/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004502 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004503 * @gadget: The usb gadget state
4504 * @is_on: Current state of the USB PHY
4505 *
4506 * Connect/Disconnect the USB PHY pullup
4507 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004508static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004509{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004510 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004511 unsigned long flags = 0;
4512
Gregory Herrero77ba9112015-09-29 12:08:19 +02004513 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004514 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004515
4516 /* Don't modify pullup state while in host mode */
4517 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4518 hsotg->enabled = is_on;
4519 return 0;
4520 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004521
4522 spin_lock_irqsave(&hsotg->lock, flags);
4523 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004524 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004525 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004526 /* Enable ACG feature in device mode,if supported */
4527 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004528 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004529 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004530 dwc2_hsotg_core_disconnect(hsotg);
4531 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004532 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004533 }
4534
4535 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4536 spin_unlock_irqrestore(&hsotg->lock, flags);
4537
4538 return 0;
4539}
4540
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004541static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004542{
4543 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4544 unsigned long flags;
4545
4546 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4547 spin_lock_irqsave(&hsotg->lock, flags);
4548
Gregory Herrero61f72232015-09-29 12:08:28 +02004549 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004550 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004551 * before being initialized / de-initialized
4552 */
4553 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004554 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004555
Gregory Herrero83d98222015-01-09 13:39:02 +01004556 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004557 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004558
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004559 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004560 if (hsotg->enabled) {
4561 /* Enable ACG feature in device mode,if supported */
4562 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004563 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004564 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004565 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004566 dwc2_hsotg_core_disconnect(hsotg);
4567 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004568 }
4569
4570 spin_unlock_irqrestore(&hsotg->lock, flags);
4571 return 0;
4572}
4573
Gregory Herrero596d6962015-01-09 13:39:08 +01004574/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004575 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004576 * @gadget: The usb gadget state
4577 * @mA: Amount of current
4578 *
4579 * Report how much power the device may consume to the phy.
4580 */
John Youn9da51972017-01-17 20:30:27 -08004581static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004582{
4583 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4584
4585 if (IS_ERR_OR_NULL(hsotg->uphy))
4586 return -ENOTSUPP;
4587 return usb_phy_set_power(hsotg->uphy, mA);
4588}
4589
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004590static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4591 .get_frame = dwc2_hsotg_gadget_getframe,
4592 .udc_start = dwc2_hsotg_udc_start,
4593 .udc_stop = dwc2_hsotg_udc_stop,
4594 .pullup = dwc2_hsotg_pullup,
4595 .vbus_session = dwc2_hsotg_vbus_session,
4596 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004597};
4598
4599/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004600 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004601 * @hsotg: The device state.
4602 * @hs_ep: The endpoint to be initialised.
4603 * @epnum: The endpoint number
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004604 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004605 *
4606 * Initialise the given endpoint (as part of the probe and device state
4607 * creation) to give to the gadget driver. Setup the endpoint name, any
4608 * direction information and other state that may be required.
4609 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004610static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004611 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004612 int epnum,
4613 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004614{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004615 char *dir;
4616
4617 if (epnum == 0)
4618 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004619 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004620 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004621 else
4622 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004623
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004624 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004625 hs_ep->index = epnum;
4626
4627 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4628
4629 INIT_LIST_HEAD(&hs_ep->queue);
4630 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4631
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004632 /* add to the list of endpoints known by the gadget driver */
4633 if (epnum)
4634 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4635
4636 hs_ep->parent = hsotg;
4637 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004638
4639 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4640 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4641 else
4642 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4643 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004644 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004645
Robert Baldyga29545222015-07-31 16:00:18 +02004646 if (epnum == 0) {
4647 hs_ep->ep.caps.type_control = true;
4648 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004649 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4650 hs_ep->ep.caps.type_iso = true;
4651 hs_ep->ep.caps.type_bulk = true;
4652 }
Robert Baldyga29545222015-07-31 16:00:18 +02004653 hs_ep->ep.caps.type_int = true;
4654 }
4655
4656 if (dir_in)
4657 hs_ep->ep.caps.dir_in = true;
4658 else
4659 hs_ep->ep.caps.dir_out = true;
4660
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004661 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004662 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004663 * to be something valid.
4664 */
4665
4666 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004667 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004668
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004669 if (dir_in)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004670 dwc2_writel(hsotg, next, DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004671 else
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004672 dwc2_writel(hsotg, next, DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004673 }
4674}
4675
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004676/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004677 * dwc2_hsotg_hw_cfg - read HW configuration registers
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004678 * @hsotg: Programming view of the DWC_otg controller
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004679 *
4680 * Read the USB core HW configuration registers
4681 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004682static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004683{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004684 u32 cfg;
4685 u32 ep_type;
4686 u32 i;
4687
Ben Dooks10aebc72010-07-19 09:40:44 +01004688 /* check hardware configuration */
4689
John Youn43e90342015-12-17 11:17:45 -08004690 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4691
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004692 /* Add ep0 */
4693 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004694
John Younb98866c2017-01-17 20:31:58 -08004695 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4696 sizeof(struct dwc2_hsotg_ep),
4697 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004698 if (!hsotg->eps_in[0])
4699 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004700 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004701 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004702
John Youn43e90342015-12-17 11:17:45 -08004703 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004704 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004705 ep_type = cfg & 3;
4706 /* Direction in or both */
4707 if (!(ep_type & 2)) {
4708 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004709 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004710 if (!hsotg->eps_in[i])
4711 return -ENOMEM;
4712 }
4713 /* Direction out or both */
4714 if (!(ep_type & 1)) {
4715 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004716 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004717 if (!hsotg->eps_out[i])
4718 return -ENOMEM;
4719 }
4720 }
4721
John Youn43e90342015-12-17 11:17:45 -08004722 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4723 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004724
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004725 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4726 hsotg->num_of_eps,
4727 hsotg->dedicated_fifos ? "dedicated" : "shared",
4728 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004729 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004730}
4731
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004732/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004733 * dwc2_hsotg_dump - dump state of the udc
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004734 * @hsotg: Programming view of the DWC_otg controller
4735 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004736 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004737static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004738{
Mark Brown83a01802011-06-01 17:16:15 +01004739#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004740 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004741 u32 val;
4742 int idx;
4743
4744 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004745 dwc2_readl(hsotg, DCFG), dwc2_readl(hsotg, DCTL),
4746 dwc2_readl(hsotg, DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004747
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004748 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004749 dwc2_readl(hsotg, GAHBCFG), dwc2_readl(hsotg, GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004750
4751 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004752 dwc2_readl(hsotg, GRXFSIZ), dwc2_readl(hsotg, GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004753
4754 /* show periodic fifo settings */
4755
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004756 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004757 val = dwc2_readl(hsotg, DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004758 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004759 val >> FIFOSIZE_DEPTH_SHIFT,
4760 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004761 }
4762
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004763 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004764 dev_info(dev,
4765 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004766 dwc2_readl(hsotg, DIEPCTL(idx)),
4767 dwc2_readl(hsotg, DIEPTSIZ(idx)),
4768 dwc2_readl(hsotg, DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004769
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004770 val = dwc2_readl(hsotg, DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004771 dev_info(dev,
4772 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004773 idx, dwc2_readl(hsotg, DOEPCTL(idx)),
4774 dwc2_readl(hsotg, DOEPTSIZ(idx)),
4775 dwc2_readl(hsotg, DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004776 }
4777
4778 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004779 dwc2_readl(hsotg, DVBUSDIS), dwc2_readl(hsotg, DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004780#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004781}
4782
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004783/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004784 * dwc2_gadget_init - init function for gadget
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004785 * @hsotg: Programming view of the DWC_otg controller
4786 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004787 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004788int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004789{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004790 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004791 int epnum;
4792 int ret;
John Youn43e90342015-12-17 11:17:45 -08004793
Gregory Herrero0a176272015-01-09 13:38:52 +01004794 /* Dump fifo information */
4795 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004796 hsotg->params.g_np_tx_fifo_size);
4797 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004798
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004799 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004800 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004801 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004802 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004803
4804 if (hsotg->params.lpm)
4805 hsotg->gadget.lpm_capable = true;
4806
Gregory Herrero097ee662015-04-29 22:09:10 +02004807 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4808 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004809 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4810 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004811
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004812 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004813 if (ret) {
4814 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004815 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004816 }
4817
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004818 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4819 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004820 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004821 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004822
4823 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4824 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004825 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004826 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004827
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004828 if (using_desc_dma(hsotg)) {
4829 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4830 if (ret < 0)
4831 return ret;
4832 }
4833
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004834 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4835 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004836 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004837 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004838 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004839 }
4840
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004841 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4842
4843 if (hsotg->num_of_eps == 0) {
4844 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004845 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004846 }
4847
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004848 /* setup endpoint information */
4849
4850 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004851 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004852
4853 /* allocate EP0 request */
4854
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004855 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004856 GFP_KERNEL);
4857 if (!hsotg->ctrl_req) {
4858 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004859 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004860 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004861
4862 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004863 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4864 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004865 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004866 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004867 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004868 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004869 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004870 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004871
Dinh Nguyen117777b2014-11-11 11:13:34 -06004872 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004873 if (ret) {
4874 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep,
4875 hsotg->ctrl_req);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004876 return ret;
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004877 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004878 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004879
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004880 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004881}
4882
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004883/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004884 * dwc2_hsotg_remove - remove function for hsotg driver
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004885 * @hsotg: Programming view of the DWC_otg controller
4886 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004887 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004888int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004889{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004890 usb_del_gadget_udc(&hsotg->gadget);
Grigor Tovmasyan9bb073a2018-05-24 18:22:30 +04004891 dwc2_hsotg_ep_free_request(&hsotg->eps_out[0]->ep, hsotg->ctrl_req);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004892
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004893 return 0;
4894}
4895
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004896int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004897{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004898 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004899
Gregory Herrero9e779772015-04-29 22:09:07 +02004900 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004901 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004902
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004903 if (hsotg->driver) {
4904 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004905
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004906 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4907 hsotg->driver->driver.name);
4908
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004909 spin_lock_irqsave(&hsotg->lock, flags);
4910 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004911 dwc2_hsotg_core_disconnect(hsotg);
4912 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004913 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4914 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004915
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004916 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4917 if (hsotg->eps_in[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004918 dwc2_hsotg_ep_disable_lock(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004919 if (hsotg->eps_out[ep])
Minas Harutyunyan4fe4f9f2018-12-10 18:09:32 +04004920 dwc2_hsotg_ep_disable_lock(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004921 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004922 }
4923
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004924 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004925}
4926
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004927int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004928{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004929 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004930
Gregory Herrero9e779772015-04-29 22:09:07 +02004931 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004932 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004933
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004934 if (hsotg->driver) {
4935 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4936 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004937
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004938 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004939 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004940 if (hsotg->enabled) {
4941 /* Enable ACG feature in device mode,if supported */
4942 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004943 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004944 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004945 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004946 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004947
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004948 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004949}
John Youn58e52ff6a2016-02-23 19:54:57 -08004950
4951/**
4952 * dwc2_backup_device_registers() - Backup controller device registers.
4953 * When suspending usb bus, registers needs to be backuped
4954 * if controller power is disabled once suspended.
4955 *
4956 * @hsotg: Programming view of the DWC_otg controller
4957 */
4958int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4959{
4960 struct dwc2_dregs_backup *dr;
4961 int i;
4962
4963 dev_dbg(hsotg->dev, "%s\n", __func__);
4964
4965 /* Backup dev regs */
4966 dr = &hsotg->dr_backup;
4967
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004968 dr->dcfg = dwc2_readl(hsotg, DCFG);
4969 dr->dctl = dwc2_readl(hsotg, DCTL);
4970 dr->daintmsk = dwc2_readl(hsotg, DAINTMSK);
4971 dr->diepmsk = dwc2_readl(hsotg, DIEPMSK);
4972 dr->doepmsk = dwc2_readl(hsotg, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08004973
4974 for (i = 0; i < hsotg->num_of_eps; i++) {
4975 /* Backup IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004976 dr->diepctl[i] = dwc2_readl(hsotg, DIEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004977
4978 /* Ensure DATA PID is correctly configured */
4979 if (dr->diepctl[i] & DXEPCTL_DPID)
4980 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4981 else
4982 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4983
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004984 dr->dieptsiz[i] = dwc2_readl(hsotg, DIEPTSIZ(i));
4985 dr->diepdma[i] = dwc2_readl(hsotg, DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004986
4987 /* Backup OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004988 dr->doepctl[i] = dwc2_readl(hsotg, DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004989
4990 /* Ensure DATA PID is correctly configured */
4991 if (dr->doepctl[i] & DXEPCTL_DPID)
4992 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4993 else
4994 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4995
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04004996 dr->doeptsiz[i] = dwc2_readl(hsotg, DOEPTSIZ(i));
4997 dr->doepdma[i] = dwc2_readl(hsotg, DOEPDMA(i));
4998 dr->dtxfsiz[i] = dwc2_readl(hsotg, DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004999 }
5000 dr->valid = true;
5001 return 0;
5002}
5003
5004/**
5005 * dwc2_restore_device_registers() - Restore controller device registers.
5006 * When resuming usb bus, device registers needs to be restored
5007 * if controller power were disabled.
5008 *
5009 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005010 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
5011 *
5012 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08005013 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005014int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08005015{
5016 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08005017 int i;
5018
5019 dev_dbg(hsotg->dev, "%s\n", __func__);
5020
5021 /* Restore dev regs */
5022 dr = &hsotg->dr_backup;
5023 if (!dr->valid) {
5024 dev_err(hsotg->dev, "%s: no device registers to restore\n",
5025 __func__);
5026 return -EINVAL;
5027 }
5028 dr->valid = false;
5029
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005030 if (!remote_wakeup)
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005031 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005032
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005033 dwc2_writel(hsotg, dr->daintmsk, DAINTMSK);
5034 dwc2_writel(hsotg, dr->diepmsk, DIEPMSK);
5035 dwc2_writel(hsotg, dr->doepmsk, DOEPMSK);
John Youn58e52ff6a2016-02-23 19:54:57 -08005036
5037 for (i = 0; i < hsotg->num_of_eps; i++) {
5038 /* Restore IN EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005039 dwc2_writel(hsotg, dr->dieptsiz[i], DIEPTSIZ(i));
5040 dwc2_writel(hsotg, dr->diepdma[i], DIEPDMA(i));
5041 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005042 /** WA for enabled EPx's IN in DDMA mode. On entering to
5043 * hibernation wrong value read and saved from DIEPDMAx,
5044 * as result BNA interrupt asserted on hibernation exit
5045 * by restoring from saved area.
5046 */
5047 if (hsotg->params.g_dma_desc &&
5048 (dr->diepctl[i] & DXEPCTL_EPENA))
5049 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005050 dwc2_writel(hsotg, dr->dtxfsiz[i], DPTXFSIZN(i));
5051 dwc2_writel(hsotg, dr->diepctl[i], DIEPCTL(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005052 /* Restore OUT EPs */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005053 dwc2_writel(hsotg, dr->doeptsiz[i], DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04005054 /* WA for enabled EPx's OUT in DDMA mode. On entering to
5055 * hibernation wrong value read and saved from DOEPDMAx,
5056 * as result BNA interrupt asserted on hibernation exit
5057 * by restoring from saved area.
5058 */
5059 if (hsotg->params.g_dma_desc &&
5060 (dr->doepctl[i] & DXEPCTL_EPENA))
5061 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005062 dwc2_writel(hsotg, dr->doepdma[i], DOEPDMA(i));
5063 dwc2_writel(hsotg, dr->doepctl[i], DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08005064 }
5065
John Youn58e52ff6a2016-02-23 19:54:57 -08005066 return 0;
5067}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04005068
5069/**
5070 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
5071 *
5072 * @hsotg: Programming view of DWC_otg controller
5073 *
5074 */
5075void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
5076{
5077 u32 val;
5078
5079 if (!hsotg->params.lpm)
5080 return;
5081
5082 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
5083 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
5084 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
5085 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
5086 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
Minas Harutyunyan46637562019-04-18 15:40:43 +04005087 val |= GLPMCFG_LPM_REJECT_CTRL_CONTROL;
Artur Petrosyan9aed8c02018-11-02 11:29:55 -04005088 val |= GLPMCFG_LPM_ACCEPT_CTRL_ISOC;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005089 dwc2_writel(hsotg, val, GLPMCFG);
5090 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg, GLPMCFG));
Grigor Tovmasyan4abe4532018-08-29 21:02:28 +04005091
5092 /* Unmask WKUP_ALERT Interrupt */
5093 if (hsotg->params.service_interval)
5094 dwc2_set_bit(hsotg, GINTMSK2, GINTMSK2_WKUP_ALERT_INT_MSK);
Sevak Arakelyan21b03402018-01-24 17:43:32 +04005095}
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005096
5097/**
Grigor Tovmasyan15d9dbf2018-08-29 21:01:59 +04005098 * dwc2_gadget_program_ref_clk - Program GREFCLK register in device mode
5099 *
5100 * @hsotg: Programming view of DWC_otg controller
5101 *
5102 */
5103void dwc2_gadget_program_ref_clk(struct dwc2_hsotg *hsotg)
5104{
5105 u32 val = 0;
5106
5107 val |= GREFCLK_REF_CLK_MODE;
5108 val |= hsotg->params.ref_clk_per << GREFCLK_REFCLKPER_SHIFT;
5109 val |= hsotg->params.sof_cnt_wkup_alert <<
5110 GREFCLK_SOF_CNT_WKUP_ALERT_SHIFT;
5111
5112 dwc2_writel(hsotg, val, GREFCLK);
5113 dev_dbg(hsotg->dev, "GREFCLK=0x%08x\n", dwc2_readl(hsotg, GREFCLK));
5114}
5115
5116/**
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005117 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
5118 *
5119 * @hsotg: Programming view of the DWC_otg controller
5120 *
5121 * Return non-zero if failed to enter to hibernation.
5122 */
5123int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
5124{
5125 u32 gpwrdn;
5126 int ret = 0;
5127
5128 /* Change to L2(suspend) state */
5129 hsotg->lx_state = DWC2_L2;
5130 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
5131 ret = dwc2_backup_global_registers(hsotg);
5132 if (ret) {
5133 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5134 __func__);
5135 return ret;
5136 }
5137 ret = dwc2_backup_device_registers(hsotg);
5138 if (ret) {
5139 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
5140 __func__);
5141 return ret;
5142 }
5143
5144 gpwrdn = GPWRDN_PWRDNRSTN;
5145 gpwrdn |= GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005146 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005147 udelay(10);
5148
5149 /* Set flag to indicate that we are in hibernation */
5150 hsotg->hibernated = 1;
5151
5152 /* Enable interrupts from wake up logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005153 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005154 gpwrdn |= GPWRDN_PMUINTSEL;
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 /* Unmask device mode interrupts in GPWRDN */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005159 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005160 gpwrdn |= GPWRDN_RST_DET_MSK;
5161 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5162 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005163 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005164 udelay(10);
5165
5166 /* Enable Power Down Clamp */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005167 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005168 gpwrdn |= GPWRDN_PWRDNCLMP;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005169 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005170 udelay(10);
5171
5172 /* Switch off VDD */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005173 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005174 gpwrdn |= GPWRDN_PWRDNSWTCH;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005175 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005176 udelay(10);
5177
5178 /* Save gpwrdn register for further usage if stschng interrupt */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005179 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005180 dev_dbg(hsotg->dev, "Hibernation completed\n");
5181
5182 return ret;
5183}
5184
5185/**
5186 * dwc2_gadget_exit_hibernation()
5187 * This function is for exiting from Device mode hibernation by host initiated
5188 * resume/reset and device initiated remote-wakeup.
5189 *
5190 * @hsotg: Programming view of the DWC_otg controller
5191 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04005192 * @reset: indicates whether resume is initiated by Reset.
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005193 *
5194 * Return non-zero if failed to exit from hibernation.
5195 */
5196int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
5197 int rem_wakeup, int reset)
5198{
5199 u32 pcgcctl;
5200 u32 gpwrdn;
5201 u32 dctl;
5202 int ret = 0;
5203 struct dwc2_gregs_backup *gr;
5204 struct dwc2_dregs_backup *dr;
5205
5206 gr = &hsotg->gr_backup;
5207 dr = &hsotg->dr_backup;
5208
5209 if (!hsotg->hibernated) {
5210 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5211 return 1;
5212 }
5213 dev_dbg(hsotg->dev,
5214 "%s: called with rem_wakeup = %d reset = %d\n",
5215 __func__, rem_wakeup, reset);
5216
5217 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5218
5219 if (!reset) {
5220 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005221 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005222 }
5223
5224 /* De-assert Restore */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005225 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005226 gpwrdn &= ~GPWRDN_RESTORE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005227 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005228 udelay(10);
5229
5230 if (!rem_wakeup) {
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005231 pcgcctl = dwc2_readl(hsotg, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005232 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005233 dwc2_writel(hsotg, pcgcctl, PCGCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005234 }
5235
5236 /* Restore GUSBCFG, DCFG and DCTL */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005237 dwc2_writel(hsotg, gr->gusbcfg, GUSBCFG);
5238 dwc2_writel(hsotg, dr->dcfg, DCFG);
5239 dwc2_writel(hsotg, dr->dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005240
5241 /* De-assert Wakeup Logic */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005242 gpwrdn = dwc2_readl(hsotg, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005243 gpwrdn &= ~GPWRDN_PMUACTV;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005244 dwc2_writel(hsotg, gpwrdn, GPWRDN);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005245
5246 if (rem_wakeup) {
5247 udelay(10);
5248 /* Start Remote Wakeup Signaling */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005249 dwc2_writel(hsotg, dr->dctl | DCTL_RMTWKUPSIG, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005250 } else {
5251 udelay(50);
5252 /* Set Device programming done bit */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005253 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005254 dctl |= DCTL_PWRONPRGDONE;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005255 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005256 }
5257 /* Wait for interrupts which must be cleared */
5258 mdelay(2);
5259 /* Clear all pending interupts */
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005260 dwc2_writel(hsotg, 0xffffffff, GINTSTS);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005261
5262 /* Restore global registers */
5263 ret = dwc2_restore_global_registers(hsotg);
5264 if (ret) {
5265 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5266 __func__);
5267 return ret;
5268 }
5269
5270 /* Restore device registers */
5271 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5272 if (ret) {
5273 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5274 __func__);
5275 return ret;
5276 }
5277
5278 if (rem_wakeup) {
5279 mdelay(10);
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005280 dctl = dwc2_readl(hsotg, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005281 dctl &= ~DCTL_RMTWKUPSIG;
Gevorg Sahakyanf25c42b2018-07-26 18:00:13 +04005282 dwc2_writel(hsotg, dctl, DCTL);
Vardan Mikayelyanc5c403d2018-02-16 14:10:13 +04005283 }
5284
5285 hsotg->hibernated = 0;
5286 hsotg->lx_state = DWC2_L0;
5287 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5288
5289 return ret;
5290}