blob: f0d9ccf1d665ad37b2f23786806bde8c16da11ea [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002/*
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09003 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01006 * Copyright 2008 Openmoko, Inc.
7 * Copyright 2008 Simtec Electronics
8 * Ben Dooks <ben@simtec.co.uk>
9 * http://armlinux.simtec.co.uk/
10 *
11 * S3C USB2.0 High-speed / OtG driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020012 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010013
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/spinlock.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
Marek Szyprowski7ad80962014-11-21 15:14:48 +010020#include <linux/mutex.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010021#include <linux/seq_file.h>
22#include <linux/delay.h>
23#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020025#include <linux/of_platform.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010026
27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053029#include <linux/usb/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010030
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070031#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060032#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010033
34/* conversion functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050035static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010036{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050037 return container_of(req, struct dwc2_hsotg_req, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010038}
39
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050040static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050042 return container_of(ep, struct dwc2_hsotg_ep, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010043}
44
Dinh Nguyen941fcce2014-11-11 11:13:33 -060045static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010046{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060047 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010048}
49
Razmik Karapetyanabd064a2018-01-19 14:42:08 +040050static inline void dwc2_set_bit(void __iomem *ptr, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010051{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030052 dwc2_writel(dwc2_readl(ptr) | val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010053}
54
Razmik Karapetyanabd064a2018-01-19 14:42:08 +040055static inline void dwc2_clear_bit(void __iomem *ptr, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010056{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030057 dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010058}
59
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050060static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010061 u32 ep_index, u32 dir_in)
62{
63 if (dir_in)
64 return hsotg->eps_in[ep_index];
65 else
66 return hsotg->eps_out[ep_index];
67}
68
Mickael Maison997f4f82014-12-23 17:39:45 +010069/* forward declaration of functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050070static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010071
72/**
73 * using_dma - return the DMA status of the driver.
74 * @hsotg: The driver state.
75 *
76 * Return true if we're using DMA.
77 *
78 * Currently, we have the DMA support code worked into everywhere
79 * that needs it, but the AMBA DMA implementation in the hardware can
80 * only DMA from 32bit aligned addresses. This means that gadgets such
81 * as the CDC Ethernet cannot work as they often pass packets which are
82 * not 32bit aligned.
83 *
84 * Unfortunately the choice to use DMA or not is global to the controller
85 * and seems to be only settable when the controller is being put through
86 * a core reset. This means we either need to fix the gadgets to take
87 * account of DMA alignment, or add bounce buffers (yuerk).
88 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010089 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010090 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060091static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010092{
John Youn05ee7992016-11-03 17:56:05 -070093 return hsotg->params.g_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +010094}
95
Vahram Aharonyandec4b552016-11-09 19:27:48 -080096/*
97 * using_desc_dma - return the descriptor DMA status of the driver.
98 * @hsotg: The driver state.
99 *
100 * Return true if we're using descriptor DMA.
101 */
102static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
103{
104 return hsotg->params.g_dma_desc;
105}
106
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100107/**
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700108 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
109 * @hs_ep: The endpoint
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700110 *
111 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
112 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
113 */
114static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
115{
116 hs_ep->target_frame += hs_ep->interval;
117 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600118 hs_ep->frame_overrun = true;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700119 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
120 } else {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600121 hs_ep->frame_overrun = false;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700122 }
123}
124
125/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500126 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100127 * @hsotg: The device state
128 * @ints: A bitmask of the interrupts to enable
129 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500130static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100131{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300132 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100133 u32 new_gsintmsk;
134
135 new_gsintmsk = gsintmsk | ints;
136
137 if (new_gsintmsk != gsintmsk) {
138 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300139 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100140 }
141}
142
143/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500144 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100145 * @hsotg: The device state
146 * @ints: A bitmask of the interrupts to enable
147 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500148static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100149{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300150 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100151 u32 new_gsintmsk;
152
153 new_gsintmsk = gsintmsk & ~ints;
154
155 if (new_gsintmsk != gsintmsk)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300156 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100157}
158
159/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500160 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100161 * @hsotg: The device state
162 * @ep: The endpoint index
163 * @dir_in: True if direction is in.
164 * @en: The enable value, true to enable
165 *
166 * Set or clear the mask for an individual endpoint's interrupt
167 * request.
168 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500169static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800170 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100171 unsigned int en)
172{
173 unsigned long flags;
174 u32 bit = 1 << ep;
175 u32 daint;
176
177 if (!dir_in)
178 bit <<= 16;
179
180 local_irq_save(flags);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300181 daint = dwc2_readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100182 if (en)
183 daint |= bit;
184 else
185 daint &= ~bit;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300186 dwc2_writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100187 local_irq_restore(flags);
188}
189
190/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800191 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400192 *
193 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800194 */
195int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
196{
197 if (hsotg->hw_params.en_multiple_tx_fifo)
198 /* In dedicated FIFO mode we need count of IN EPs */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400199 return hsotg->hw_params.num_dev_in_eps;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800200 else
201 /* In shared FIFO mode we need count of Periodic IN EPs */
202 return hsotg->hw_params.num_dev_perio_in_ep;
203}
204
205/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800206 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
207 * device mode TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400208 *
209 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800210 */
211int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
212{
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800213 int addr;
214 int tx_addr_max;
215 u32 np_tx_fifo_size;
216
217 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
218 hsotg->params.g_np_tx_fifo_size);
219
220 /* Get Endpoint Info Control block size in DWORDs. */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400221 tx_addr_max = hsotg->hw_params.total_fifo_size;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800222
223 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
224 if (tx_addr_max <= addr)
225 return 0;
226
227 return tx_addr_max - addr;
228}
229
230/**
231 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
232 * TX FIFOs
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400233 *
234 * @hsotg: Programming view of the DWC_otg controller
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800235 */
236int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
237{
238 int tx_fifo_count;
239 int tx_fifo_depth;
240
241 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
242
243 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
244
245 if (!tx_fifo_count)
246 return tx_fifo_depth;
247 else
248 return tx_fifo_depth / tx_fifo_count;
249}
250
251/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500252 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100253 * @hsotg: The device instance.
254 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500255static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100256{
John Youn2317eac2016-10-17 17:36:23 -0700257 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100258 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100259 int timeout;
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +0400260
Ben Dooks0f002d22010-05-25 05:36:50 +0100261 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700262 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100263
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100264 /* Reset fifo map if not correctly cleared during previous session */
265 WARN_ON(hsotg->fifo_map);
266 hsotg->fifo_map = 0;
267
Gregory Herrero0a176272015-01-09 13:38:52 +0100268 /* set RX/NPTX FIFO sizes */
John Youn05ee7992016-11-03 17:56:05 -0700269 dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
270 dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
271 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
272 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100273
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200274 /*
275 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100276 * block have overlapping default addresses. This also ensures
277 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200278 * known values.
279 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100280
281 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700282 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100283
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200284 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100285 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200286 * them to endpoints dynamically according to maxpacket size value of
287 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200288 */
John Youn2317eac2016-10-17 17:36:23 -0700289 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700290 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700291 continue;
292 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700293 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
294 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700295 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700296 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100297
John Youn2317eac2016-10-17 17:36:23 -0700298 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
John Youn05ee7992016-11-03 17:56:05 -0700299 val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100300 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100301
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800302 dwc2_writel(hsotg->hw_params.total_fifo_size |
303 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
304 hsotg->regs + GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200305 /*
306 * according to p428 of the design guide, we need to ensure that
307 * all fifos are flushed before continuing
308 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100309
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300310 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700311 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100312
313 /* wait until the fifos are both flushed */
314 timeout = 100;
315 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300316 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100317
Dinh Nguyen47a16852014-04-14 14:13:34 -0700318 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100319 break;
320
321 if (--timeout == 0) {
322 dev_err(hsotg->dev,
323 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
324 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100325 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100326 }
327
328 udelay(1);
329 }
330
331 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100332}
333
334/**
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +0400335 * dwc2_hsotg_ep_alloc_request - allocate USB rerequest structure
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100336 * @ep: USB endpoint to allocate request for.
337 * @flags: Allocation flags
338 *
339 * Allocate a new USB request structure appropriate for the specified endpoint
340 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500341static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800342 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100343{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500344 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100345
John Younec33efe2017-01-17 20:32:41 -0800346 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100347 if (!req)
348 return NULL;
349
350 INIT_LIST_HEAD(&req->queue);
351
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100352 return &req->req;
353}
354
355/**
356 * is_ep_periodic - return true if the endpoint is in periodic mode.
357 * @hs_ep: The endpoint to query.
358 *
359 * Returns true if the endpoint is in periodic mode, meaning it is being
360 * used for an Interrupt or ISO transfer.
361 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500362static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100363{
364 return hs_ep->periodic;
365}
366
367/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500368 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100369 * @hsotg: The device state.
370 * @hs_ep: The endpoint for the request
371 * @hs_req: The request being processed.
372 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500373 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100374 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200375 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500376static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800377 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500378 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100379{
380 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800381
Jingoo Han17d966a2013-05-11 21:14:00 +0900382 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100383}
384
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800385/*
386 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
387 * for Control endpoint
388 * @hsotg: The device state.
389 *
390 * This function will allocate 4 descriptor chains for EP 0: 2 for
391 * Setup stage, per one for IN and OUT data/status transactions.
392 */
393static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
394{
395 hsotg->setup_desc[0] =
396 dmam_alloc_coherent(hsotg->dev,
397 sizeof(struct dwc2_dma_desc),
398 &hsotg->setup_desc_dma[0],
399 GFP_KERNEL);
400 if (!hsotg->setup_desc[0])
401 goto fail;
402
403 hsotg->setup_desc[1] =
404 dmam_alloc_coherent(hsotg->dev,
405 sizeof(struct dwc2_dma_desc),
406 &hsotg->setup_desc_dma[1],
407 GFP_KERNEL);
408 if (!hsotg->setup_desc[1])
409 goto fail;
410
411 hsotg->ctrl_in_desc =
412 dmam_alloc_coherent(hsotg->dev,
413 sizeof(struct dwc2_dma_desc),
414 &hsotg->ctrl_in_desc_dma,
415 GFP_KERNEL);
416 if (!hsotg->ctrl_in_desc)
417 goto fail;
418
419 hsotg->ctrl_out_desc =
420 dmam_alloc_coherent(hsotg->dev,
421 sizeof(struct dwc2_dma_desc),
422 &hsotg->ctrl_out_desc_dma,
423 GFP_KERNEL);
424 if (!hsotg->ctrl_out_desc)
425 goto fail;
426
427 return 0;
428
429fail:
430 return -ENOMEM;
431}
432
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100433/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500434 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100435 * @hsotg: The controller state.
436 * @hs_ep: The endpoint we're going to write for.
437 * @hs_req: The request to write data for.
438 *
439 * This is called when the TxFIFO has some space in it to hold a new
440 * transmission and we have something to give it. The actual setup of
441 * the data size is done elsewhere, so all we have to do is to actually
442 * write the data.
443 *
444 * The return value is zero if there is more space (or nothing was done)
445 * otherwise -ENOSPC is returned if the FIFO space was used up.
446 *
447 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200448 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500449static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800450 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500451 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100452{
453 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300454 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100455 int buf_pos = hs_req->req.actual;
456 int to_write = hs_ep->size_loaded;
457 void *data;
458 int can_write;
459 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200460 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100461
462 to_write -= (buf_pos - hs_ep->last_load);
463
464 /* if there's nothing to write, get out early */
465 if (to_write == 0)
466 return 0;
467
Ben Dooks10aebc72010-07-19 09:40:44 +0100468 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300469 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100470 int size_left;
471 int size_done;
472
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200473 /*
474 * work out how much data was loaded so we can calculate
475 * how much data is left in the fifo.
476 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100477
Dinh Nguyen47a16852014-04-14 14:13:34 -0700478 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100479
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200480 /*
481 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100482 * previous data has been completely sent.
483 */
484 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500485 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100486 return -ENOSPC;
487 }
488
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100489 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
490 __func__, size_left,
491 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
492
493 /* how much of the data has moved */
494 size_done = hs_ep->size_loaded - size_left;
495
496 /* how much data is left in the fifo */
497 can_write = hs_ep->fifo_load - size_done;
498 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
499 __func__, can_write);
500
501 can_write = hs_ep->fifo_size - can_write;
502 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
503 __func__, can_write);
504
505 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500506 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100507 return -ENOSPC;
508 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100509 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Robert Baldygaad674a12016-08-29 13:38:50 -0700510 can_write = dwc2_readl(hsotg->regs +
511 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100512
513 can_write &= 0xffff;
514 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100515 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700516 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100517 dev_dbg(hsotg->dev,
518 "%s: no queue slots available (0x%08x)\n",
519 __func__, gnptxsts);
520
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500521 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100522 return -ENOSPC;
523 }
524
Dinh Nguyen47a16852014-04-14 14:13:34 -0700525 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100526 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100527 }
528
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200529 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
530
531 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800532 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100533
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200534 /*
535 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100536 * FIFO, requests of >512 cause the endpoint to get stuck with a
537 * fragment of the end of the transfer in it.
538 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200539 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100540 can_write = 512;
541
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200542 /*
543 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100544 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200545 * doing it.
546 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200547 if (to_write > max_transfer) {
548 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100549
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200550 /* it's needed only when we do not use dedicated fifos */
551 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500552 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800553 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700554 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100555 }
556
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100557 /* see if we can write data */
558
559 if (to_write > can_write) {
560 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200561 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100562
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200563 /*
564 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100565 * exact number of packets.
566 *
567 * Note, we do not currently check to see if we can ever
568 * write a full packet or not to the FIFO.
569 */
570
571 if (pkt_round)
572 to_write -= pkt_round;
573
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200574 /*
575 * enable correct FIFO interrupt to alert us when there
576 * is more room left.
577 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100578
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200579 /* it's needed only when we do not use dedicated fifos */
580 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500581 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800582 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700583 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100584 }
585
586 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800587 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100588
589 if (to_write <= 0)
590 return -ENOSPC;
591
592 hs_req->req.actual = buf_pos + to_write;
593 hs_ep->total_data += to_write;
594
595 if (periodic)
596 hs_ep->fifo_load += to_write;
597
598 to_write = DIV_ROUND_UP(to_write, 4);
599 data = hs_req->req.buf + buf_pos;
600
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500601 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100602
603 return (to_write >= can_write) ? -ENOSPC : 0;
604}
605
606/**
607 * get_ep_limit - get the maximum data legnth for this endpoint
608 * @hs_ep: The endpoint
609 *
610 * Return the maximum data that can be queued in one go on a given endpoint
611 * so that transfers that are too long can be split.
612 */
John Youn9da51972017-01-17 20:30:27 -0800613static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100614{
615 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800616 unsigned int maxsize;
617 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100618
619 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700620 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
621 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100622 } else {
John Youn9da51972017-01-17 20:30:27 -0800623 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900624 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700625 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900626 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100627 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100628 }
629
630 /* we made the constant loading easier above by using +1 */
631 maxpkt--;
632 maxsize--;
633
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200634 /*
635 * constrain by packet count if maxpkts*pktsize is greater
636 * than the length register size.
637 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100638
639 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
640 maxsize = maxpkt * hs_ep->ep.maxpacket;
641
642 return maxsize;
643}
644
645/**
John Youn38beaec2017-01-17 20:31:13 -0800646 * dwc2_hsotg_read_frameno - read current frame number
647 * @hsotg: The device instance
648 *
649 * Return the current frame number
650 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700651static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
652{
653 u32 dsts;
654
655 dsts = dwc2_readl(hsotg->regs + DSTS);
656 dsts &= DSTS_SOFFN_MASK;
657 dsts >>= DSTS_SOFFN_SHIFT;
658
659 return dsts;
660}
661
662/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800663 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
664 * DMA descriptor chain prepared for specific endpoint
665 * @hs_ep: The endpoint
666 *
667 * Return the maximum data that can be queued in one go on a given endpoint
668 * depending on its descriptor chain capacity so that transfers that
669 * are too long can be split.
670 */
671static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
672{
673 int is_isoc = hs_ep->isochronous;
674 unsigned int maxsize;
675
676 if (is_isoc)
677 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
678 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
679 else
680 maxsize = DEV_DMA_NBYTES_LIMIT;
681
682 /* Above size of one descriptor was chosen, multiple it */
683 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
684
685 return maxsize;
686}
687
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800688/*
689 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
690 * @hs_ep: The endpoint
691 * @mask: RX/TX bytes mask to be defined
692 *
693 * Returns maximum data payload for one descriptor after analyzing endpoint
694 * characteristics.
695 * DMA descriptor transfer bytes limit depends on EP type:
696 * Control out - MPS,
697 * Isochronous - descriptor rx/tx bytes bitfield limit,
698 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
699 * have concatenations from various descriptors within one packet.
700 *
701 * Selects corresponding mask for RX/TX bytes as well.
702 */
703static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
704{
705 u32 mps = hs_ep->ep.maxpacket;
706 int dir_in = hs_ep->dir_in;
707 u32 desc_size = 0;
708
709 if (!hs_ep->index && !dir_in) {
710 desc_size = mps;
711 *mask = DEV_DMA_NBYTES_MASK;
712 } else if (hs_ep->isochronous) {
713 if (dir_in) {
714 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
715 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
716 } else {
717 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
718 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
719 }
720 } else {
721 desc_size = DEV_DMA_NBYTES_LIMIT;
722 *mask = DEV_DMA_NBYTES_MASK;
723
724 /* Round down desc_size to be mps multiple */
725 desc_size -= desc_size % mps;
726 }
727
728 return desc_size;
729}
730
731/*
732 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
733 * @hs_ep: The endpoint
734 * @dma_buff: DMA address to use
735 * @len: Length of the transfer
736 *
737 * This function will iterate over descriptor chain and fill its entries
738 * with corresponding information based on transfer data.
739 */
740static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
741 dma_addr_t dma_buff,
742 unsigned int len)
743{
744 struct dwc2_hsotg *hsotg = hs_ep->parent;
745 int dir_in = hs_ep->dir_in;
746 struct dwc2_dma_desc *desc = hs_ep->desc_list;
747 u32 mps = hs_ep->ep.maxpacket;
748 u32 maxsize = 0;
749 u32 offset = 0;
750 u32 mask = 0;
751 int i;
752
753 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
754
755 hs_ep->desc_count = (len / maxsize) +
756 ((len % maxsize) ? 1 : 0);
757 if (len == 0)
758 hs_ep->desc_count = 1;
759
760 for (i = 0; i < hs_ep->desc_count; ++i) {
761 desc->status = 0;
762 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
763 << DEV_DMA_BUFF_STS_SHIFT);
764
765 if (len > maxsize) {
766 if (!hs_ep->index && !dir_in)
767 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
768
769 desc->status |= (maxsize <<
770 DEV_DMA_NBYTES_SHIFT & mask);
771 desc->buf = dma_buff + offset;
772
773 len -= maxsize;
774 offset += maxsize;
775 } else {
776 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
777
778 if (dir_in)
779 desc->status |= (len % mps) ? DEV_DMA_SHORT :
780 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
781 if (len > maxsize)
782 dev_err(hsotg->dev, "wrong len %d\n", len);
783
784 desc->status |=
785 len << DEV_DMA_NBYTES_SHIFT & mask;
786 desc->buf = dma_buff + offset;
787 }
788
789 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
790 desc->status |= (DEV_DMA_BUFF_STS_HREADY
791 << DEV_DMA_BUFF_STS_SHIFT);
792 desc++;
793 }
794}
795
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800796/*
797 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
798 * @hs_ep: The isochronous endpoint.
799 * @dma_buff: usb requests dma buffer.
800 * @len: usb request transfer length.
801 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400802 * Fills next free descriptor with the data of the arrived usb request,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800803 * frame info, sets Last and IOC bits increments next_desc. If filled
804 * descriptor is not the first one, removes L bit from the previous descriptor
805 * status.
806 */
807static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
808 dma_addr_t dma_buff, unsigned int len)
809{
810 struct dwc2_dma_desc *desc;
811 struct dwc2_hsotg *hsotg = hs_ep->parent;
812 u32 index;
813 u32 maxsize = 0;
814 u32 mask = 0;
815
816 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800817
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400818 index = hs_ep->next_desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800819 desc = &hs_ep->desc_list[index];
820
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400821 /* Check if descriptor chain full */
822 if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
823 DEV_DMA_BUFF_STS_HREADY) {
824 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
825 return 1;
826 }
827
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800828 /* Clear L bit of previous desc if more than one entries in the chain */
829 if (hs_ep->next_desc)
830 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
831
832 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
833 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
834
835 desc->status = 0;
836 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
837
838 desc->buf = dma_buff;
839 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
840 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
841
842 if (hs_ep->dir_in) {
843 desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) &
844 DEV_DMA_ISOC_PID_MASK) |
845 ((len % hs_ep->ep.maxpacket) ?
846 DEV_DMA_SHORT : 0) |
847 ((hs_ep->target_frame <<
848 DEV_DMA_ISOC_FRNUM_SHIFT) &
849 DEV_DMA_ISOC_FRNUM_MASK);
850 }
851
852 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
853 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
854
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400855 /* Increment frame number by interval for IN */
856 if (hs_ep->dir_in)
857 dwc2_gadget_incr_frame_num(hs_ep);
858
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800859 /* Update index of last configured entry in the chain */
860 hs_ep->next_desc++;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400861 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_GENERIC)
862 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800863
864 return 0;
865}
866
867/*
868 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
869 * @hs_ep: The isochronous endpoint.
870 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400871 * Prepare descriptor chain for isochronous endpoints. Afterwards
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800872 * write DMA address to HW and enable the endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800873 */
874static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
875{
876 struct dwc2_hsotg *hsotg = hs_ep->parent;
877 struct dwc2_hsotg_req *hs_req, *treq;
878 int index = hs_ep->index;
879 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400880 int i;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800881 u32 dma_reg;
882 u32 depctl;
883 u32 ctrl;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400884 struct dwc2_dma_desc *desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800885
886 if (list_empty(&hs_ep->queue)) {
887 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
888 return;
889 }
890
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400891 /* Initialize descriptor chain by Host Busy status */
892 for (i = 0; i < MAX_DMA_DESC_NUM_GENERIC; i++) {
893 desc = &hs_ep->desc_list[i];
894 desc->status = 0;
895 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
896 << DEV_DMA_BUFF_STS_SHIFT);
897 }
898
899 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800900 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
901 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
902 hs_req->req.length);
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400903 if (ret)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800904 break;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800905 }
906
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400907 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800908 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
909 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
910
911 /* write descriptor chain address to control register */
912 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
913
914 ctrl = dwc2_readl(hsotg->regs + depctl);
915 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
916 dwc2_writel(ctrl, hsotg->regs + depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800917}
918
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800919/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500920 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100921 * @hsotg: The controller state.
922 * @hs_ep: The endpoint to process a request for
923 * @hs_req: The request to start.
924 * @continuing: True if we are doing more for the current request.
925 *
926 * Start the given request running by setting the endpoint registers
927 * appropriately, and writing any data to the FIFOs.
928 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500929static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800930 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500931 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100932 bool continuing)
933{
934 struct usb_request *ureq = &hs_req->req;
935 int index = hs_ep->index;
936 int dir_in = hs_ep->dir_in;
937 u32 epctrl_reg;
938 u32 epsize_reg;
939 u32 epsize;
940 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -0800941 unsigned int length;
942 unsigned int packets;
943 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800944 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100945
946 if (index != 0) {
947 if (hs_ep->req && !continuing) {
948 dev_err(hsotg->dev, "%s: active request\n", __func__);
949 WARN_ON(1);
950 return;
951 } else if (hs_ep->req != hs_req && continuing) {
952 dev_err(hsotg->dev,
953 "%s: continue different req\n", __func__);
954 WARN_ON(1);
955 return;
956 }
957 }
958
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800959 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200960 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
961 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100962
963 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300964 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100965 hs_ep->dir_in ? "in" : "out");
966
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900967 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300968 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900969
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200970 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900971 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
972 return;
973 }
974
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100975 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200976 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
977 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100978
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800979 if (!using_desc_dma(hsotg))
980 maxreq = get_ep_limit(hs_ep);
981 else
982 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
983
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100984 if (length > maxreq) {
985 int round = maxreq % hs_ep->ep.maxpacket;
986
987 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
988 __func__, length, maxreq, round);
989
990 /* round down to multiple of packets */
991 if (round)
992 maxreq -= round;
993
994 length = maxreq;
995 }
996
997 if (length)
998 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
999 else
1000 packets = 1; /* send one packet if length is zero. */
1001
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001002 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
1003 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
1004 return;
1005 }
1006
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001007 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001008 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001009 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001010 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001011 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001012 else
1013 epsize = 0;
1014
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001015 /*
1016 * zero length packet should be programmed on its own and should not
1017 * be counted in DIEPTSIZ.PktCnt with other packets.
1018 */
1019 if (dir_in && ureq->zero && !continuing) {
1020 /* Test if zlp is actually required. */
1021 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001022 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001023 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001024 }
1025
Dinh Nguyen47a16852014-04-14 14:13:34 -07001026 epsize |= DXEPTSIZ_PKTCNT(packets);
1027 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001028
1029 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1030 __func__, packets, length, ureq->length, epsize, epsize_reg);
1031
1032 /* store the request as the current one we're doing */
1033 hs_ep->req = hs_req;
1034
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001035 if (using_desc_dma(hsotg)) {
1036 u32 offset = 0;
1037 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001038
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001039 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1040 if (!dir_in) {
1041 if (!index)
1042 length = mps;
1043 else if (length % mps)
1044 length += (mps - (length % mps));
1045 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001046
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001047 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001048 * If more data to send, adjust DMA for EP0 out data stage.
1049 * ureq->dma stays unchanged, hence increment it by already
1050 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001051 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001052 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1053 continuing)
1054 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001055
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001056 /* Fill DDMA chain entries */
1057 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1058 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001059
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001060 /* write descriptor chain address to control register */
1061 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
1062
1063 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1064 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1065 } else {
1066 /* write size / packets */
1067 dwc2_writel(epsize, hsotg->regs + epsize_reg);
1068
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001069 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001070 /*
1071 * write DMA address to control register, buffer
1072 * already synced by dwc2_hsotg_ep_queue().
1073 */
1074
1075 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
1076
1077 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1078 __func__, &ureq->dma, dma_reg);
1079 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001080 }
1081
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001082 if (hs_ep->isochronous && hs_ep->interval == 1) {
1083 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1084 dwc2_gadget_incr_frame_num(hs_ep);
1085
1086 if (hs_ep->target_frame & 0x1)
1087 ctrl |= DXEPCTL_SETODDFR;
1088 else
1089 ctrl |= DXEPCTL_SETEVENFR;
1090 }
1091
Dinh Nguyen47a16852014-04-14 14:13:34 -07001092 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001093
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001094 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001095
1096 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001097 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001098 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001099
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001100 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001101 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001102
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001103 /*
1104 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001105 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001106 * this information.
1107 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001108 hs_ep->size_loaded = length;
1109 hs_ep->last_load = ureq->actual;
1110
1111 if (dir_in && !using_dma(hsotg)) {
1112 /* set these anyway, we may need them for non-periodic in */
1113 hs_ep->fifo_load = 0;
1114
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001115 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001116 }
1117
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001118 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001119 * Note, trying to clear the NAK here causes problems with transmit
1120 * on the S3C6400 ending up with the TXFIFO becoming full.
1121 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122
1123 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001124 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001125 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001126 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001127 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001128
Dinh Nguyen47a16852014-04-14 14:13:34 -07001129 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001130 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001131
1132 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001133 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001134}
1135
1136/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001137 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138 * @hsotg: The device state.
1139 * @hs_ep: The endpoint the request is on.
1140 * @req: The request being processed.
1141 *
1142 * We've been asked to queue a request, so ensure that the memory buffer
1143 * is correctly setup for DMA. If we've been passed an extant DMA address
1144 * then ensure the buffer has been synced to memory. If our buffer has no
1145 * DMA memory, then we map the memory and mark our request to allow us to
1146 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001147 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001148static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001149 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001150 struct usb_request *req)
1151{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001152 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001153
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001154 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1155 if (ret)
1156 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001157
1158 return 0;
1159
1160dma_error:
1161 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1162 __func__, req->buf, req->length);
1163
1164 return -EIO;
1165}
1166
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001167static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001168 struct dwc2_hsotg_ep *hs_ep,
1169 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001170{
1171 void *req_buf = hs_req->req.buf;
1172
1173 /* If dma is not being used or buffer is aligned */
1174 if (!using_dma(hsotg) || !((long)req_buf & 3))
1175 return 0;
1176
1177 WARN_ON(hs_req->saved_req_buf);
1178
1179 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001180 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001181
1182 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1183 if (!hs_req->req.buf) {
1184 hs_req->req.buf = req_buf;
1185 dev_err(hsotg->dev,
1186 "%s: unable to allocate memory for bounce buffer\n",
1187 __func__);
1188 return -ENOMEM;
1189 }
1190
1191 /* Save actual buffer */
1192 hs_req->saved_req_buf = req_buf;
1193
1194 if (hs_ep->dir_in)
1195 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1196 return 0;
1197}
1198
John Younb98866c2017-01-17 20:31:58 -08001199static void
1200dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1201 struct dwc2_hsotg_ep *hs_ep,
1202 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001203{
1204 /* If dma is not being used or buffer was aligned */
1205 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1206 return;
1207
1208 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1209 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1210
1211 /* Copy data from bounce buffer on successful out transfer */
1212 if (!hs_ep->dir_in && !hs_req->req.status)
1213 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001214 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001215
1216 /* Free bounce buffer */
1217 kfree(hs_req->req.buf);
1218
1219 hs_req->req.buf = hs_req->saved_req_buf;
1220 hs_req->saved_req_buf = NULL;
1221}
1222
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001223/**
1224 * dwc2_gadget_target_frame_elapsed - Checks target frame
1225 * @hs_ep: The driver endpoint to check
1226 *
1227 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1228 * corresponding transfer.
1229 */
1230static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1231{
1232 struct dwc2_hsotg *hsotg = hs_ep->parent;
1233 u32 target_frame = hs_ep->target_frame;
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001234 u32 current_frame = hsotg->frame_number;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001235 bool frame_overrun = hs_ep->frame_overrun;
1236
1237 if (!frame_overrun && current_frame >= target_frame)
1238 return true;
1239
1240 if (frame_overrun && current_frame >= target_frame &&
1241 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1242 return true;
1243
1244 return false;
1245}
1246
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001247/*
1248 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1249 * @hsotg: The driver state
1250 * @hs_ep: the ep descriptor chain is for
1251 *
1252 * Called to update EP0 structure's pointers depend on stage of
1253 * control transfer.
1254 */
1255static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1256 struct dwc2_hsotg_ep *hs_ep)
1257{
1258 switch (hsotg->ep0_state) {
1259 case DWC2_EP0_SETUP:
1260 case DWC2_EP0_STATUS_OUT:
1261 hs_ep->desc_list = hsotg->setup_desc[0];
1262 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1263 break;
1264 case DWC2_EP0_DATA_IN:
1265 case DWC2_EP0_STATUS_IN:
1266 hs_ep->desc_list = hsotg->ctrl_in_desc;
1267 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1268 break;
1269 case DWC2_EP0_DATA_OUT:
1270 hs_ep->desc_list = hsotg->ctrl_out_desc;
1271 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1272 break;
1273 default:
1274 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1275 hsotg->ep0_state);
1276 return -EINVAL;
1277 }
1278
1279 return 0;
1280}
1281
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001282static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001283 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001284{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001285 struct dwc2_hsotg_req *hs_req = our_req(req);
1286 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001287 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001288 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001289 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001290 u32 maxsize = 0;
1291 u32 mask = 0;
1292
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001293
1294 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1295 ep->name, req, req->length, req->buf, req->no_interrupt,
1296 req->zero, req->short_not_ok);
1297
Gregory Herrero7ababa92015-04-29 22:09:08 +02001298 /* Prevent new request submission when controller is suspended */
Grigor Tovmasyan88b02f22018-01-24 17:44:25 +04001299 if (hs->lx_state != DWC2_L0) {
1300 dev_dbg(hs->dev, "%s: submit request only in active state\n",
John Youn9da51972017-01-17 20:30:27 -08001301 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001302 return -EAGAIN;
1303 }
1304
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001305 /* initialise status of the request */
1306 INIT_LIST_HEAD(&hs_req->queue);
1307 req->actual = 0;
1308 req->status = -EINPROGRESS;
1309
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001310 /* In DDMA mode for ISOC's don't queue request if length greater
1311 * than descriptor limits.
1312 */
1313 if (using_desc_dma(hs) && hs_ep->isochronous) {
1314 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1315 if (hs_ep->dir_in && req->length > maxsize) {
1316 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1317 req->length, maxsize);
1318 return -EINVAL;
1319 }
1320
1321 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1322 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1323 req->length, hs_ep->ep.maxpacket);
1324 return -EINVAL;
1325 }
1326 }
1327
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001328 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001329 if (ret)
1330 return ret;
1331
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001332 /* if we're using DMA, sync the buffers as necessary */
1333 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001334 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001335 if (ret)
1336 return ret;
1337 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001338 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1339 if (using_desc_dma(hs) && !hs_ep->index) {
1340 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1341 if (ret)
1342 return ret;
1343 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001344
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001345 first = list_empty(&hs_ep->queue);
1346 list_add_tail(&hs_req->queue, &hs_ep->queue);
1347
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001348 /*
1349 * Handle DDMA isochronous transfers separately - just add new entry
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001350 * to the descriptor chain.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001351 * Transfer will be started once SW gets either one of NAK or
1352 * OutTknEpDis interrupts.
1353 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001354 if (using_desc_dma(hs) && hs_ep->isochronous) {
1355 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1356 dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1357 hs_req->req.length);
1358 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001359 return 0;
1360 }
1361
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001362 if (first) {
1363 if (!hs_ep->isochronous) {
1364 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1365 return 0;
1366 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001367
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001368 /* Update current frame number value. */
1369 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1370 while (dwc2_gadget_target_frame_elapsed(hs_ep)) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001371 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04001372 /* Update current frame number value once more as it
1373 * changes here.
1374 */
1375 hs->frame_number = dwc2_hsotg_read_frameno(hs);
1376 }
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001377
1378 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1379 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1380 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001381 return 0;
1382}
1383
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001384static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001385 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001386{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001387 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001388 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001389 unsigned long flags = 0;
1390 int ret = 0;
1391
1392 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001393 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001394 spin_unlock_irqrestore(&hs->lock, flags);
1395
1396 return ret;
1397}
1398
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001399static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001400 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001401{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001402 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001403
1404 kfree(hs_req);
1405}
1406
1407/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001408 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001409 * @ep: The endpoint the request was on.
1410 * @req: The request completed.
1411 *
1412 * Called on completion of any requests the driver itself
1413 * submitted that need cleaning up.
1414 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001415static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001416 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001417{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001418 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001419 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001420
1421 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1422
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001423 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001424}
1425
1426/**
1427 * ep_from_windex - convert control wIndex value to endpoint
1428 * @hsotg: The driver state.
1429 * @windex: The control request wIndex field (in host order).
1430 *
1431 * Convert the given wIndex into a pointer to an driver endpoint
1432 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001433 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001434static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001435 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001436{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001437 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001438 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1439 int idx = windex & 0x7F;
1440
1441 if (windex >= 0x100)
1442 return NULL;
1443
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001444 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001445 return NULL;
1446
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001447 ep = index_to_ep(hsotg, idx, dir);
1448
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001449 if (idx && ep->dir_in != dir)
1450 return NULL;
1451
1452 return ep;
1453}
1454
1455/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001456 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001457 * @hsotg: The driver state.
1458 * @testmode: requested usb test mode
1459 * Enable usb Test Mode requested by the Host.
1460 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001461int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001462{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001463 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001464
1465 dctl &= ~DCTL_TSTCTL_MASK;
1466 switch (testmode) {
1467 case TEST_J:
1468 case TEST_K:
1469 case TEST_SE0_NAK:
1470 case TEST_PACKET:
1471 case TEST_FORCE_EN:
1472 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1473 break;
1474 default:
1475 return -EINVAL;
1476 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001477 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001478 return 0;
1479}
1480
1481/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001482 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001483 * @hsotg: The device state
1484 * @ep: Endpoint 0
1485 * @buff: Buffer for request
1486 * @length: Length of reply.
1487 *
1488 * Create a request and queue it on the given endpoint. This is useful as
1489 * an internal method of sending replies to certain control requests, etc.
1490 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001491static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001492 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001493 void *buff,
1494 int length)
1495{
1496 struct usb_request *req;
1497 int ret;
1498
1499 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1500
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001501 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001502 hsotg->ep0_reply = req;
1503 if (!req) {
1504 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1505 return -ENOMEM;
1506 }
1507
1508 req->buf = hsotg->ep0_buff;
1509 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001510 /*
1511 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1512 * STATUS stage.
1513 */
1514 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001515 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001516
1517 if (length)
1518 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001519
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001520 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001521 if (ret) {
1522 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1523 return ret;
1524 }
1525
1526 return 0;
1527}
1528
1529/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001530 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001531 * @hsotg: The device state
1532 * @ctrl: USB control request
1533 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001534static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001535 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001536{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001537 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1538 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001539 __le16 reply;
1540 int ret;
1541
1542 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1543
1544 if (!ep0->dir_in) {
1545 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1546 return -EINVAL;
1547 }
1548
1549 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1550 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001551 /*
1552 * bit 0 => self powered
1553 * bit 1 => remote wakeup
1554 */
1555 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001556 break;
1557
1558 case USB_RECIP_INTERFACE:
1559 /* currently, the data result should be zero */
1560 reply = cpu_to_le16(0);
1561 break;
1562
1563 case USB_RECIP_ENDPOINT:
1564 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1565 if (!ep)
1566 return -ENOENT;
1567
1568 reply = cpu_to_le16(ep->halted ? 1 : 0);
1569 break;
1570
1571 default:
1572 return 0;
1573 }
1574
1575 if (le16_to_cpu(ctrl->wLength) != 2)
1576 return -EINVAL;
1577
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001578 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001579 if (ret) {
1580 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1581 return ret;
1582 }
1583
1584 return 1;
1585}
1586
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001587static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001588
1589/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001590 * get_ep_head - return the first request on the endpoint
1591 * @hs_ep: The controller endpoint to get
1592 *
1593 * Get the first request on the endpoint.
1594 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001595static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001596{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001597 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1598 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001599}
1600
1601/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001602 * dwc2_gadget_start_next_request - Starts next request from ep queue
1603 * @hs_ep: Endpoint structure
1604 *
1605 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1606 * in its handler. Hence we need to unmask it here to be able to do
1607 * resynchronization.
1608 */
1609static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1610{
1611 u32 mask;
1612 struct dwc2_hsotg *hsotg = hs_ep->parent;
1613 int dir_in = hs_ep->dir_in;
1614 struct dwc2_hsotg_req *hs_req;
1615 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1616
1617 if (!list_empty(&hs_ep->queue)) {
1618 hs_req = get_ep_head(hs_ep);
1619 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1620 return;
1621 }
1622 if (!hs_ep->isochronous)
1623 return;
1624
1625 if (dir_in) {
1626 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1627 __func__);
1628 } else {
1629 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1630 __func__);
1631 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1632 mask |= DOEPMSK_OUTTKNEPDISMSK;
1633 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1634 }
1635}
1636
1637/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001638 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001639 * @hsotg: The device state
1640 * @ctrl: USB control request
1641 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001642static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001643 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001644{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001645 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1646 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001647 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001648 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001649 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001650 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001651 u32 recip;
1652 u32 wValue;
1653 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001654
1655 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1656 __func__, set ? "SET" : "CLEAR");
1657
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001658 wValue = le16_to_cpu(ctrl->wValue);
1659 wIndex = le16_to_cpu(ctrl->wIndex);
1660 recip = ctrl->bRequestType & USB_RECIP_MASK;
1661
1662 switch (recip) {
1663 case USB_RECIP_DEVICE:
1664 switch (wValue) {
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001665 case USB_DEVICE_REMOTE_WAKEUP:
1666 hsotg->remote_wakeup_allowed = 1;
1667 break;
1668
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001669 case USB_DEVICE_TEST_MODE:
1670 if ((wIndex & 0xff) != 0)
1671 return -EINVAL;
1672 if (!set)
1673 return -EINVAL;
1674
1675 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001676 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001677 if (ret) {
1678 dev_err(hsotg->dev,
1679 "%s: failed to send reply\n", __func__);
1680 return ret;
1681 }
1682 break;
1683 default:
1684 return -ENOENT;
1685 }
1686 break;
1687
1688 case USB_RECIP_ENDPOINT:
1689 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001690 if (!ep) {
1691 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001692 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001693 return -ENOENT;
1694 }
1695
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001696 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001697 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001698 halted = ep->halted;
1699
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001700 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001701
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001702 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001703 if (ret) {
1704 dev_err(hsotg->dev,
1705 "%s: failed to send reply\n", __func__);
1706 return ret;
1707 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001708
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001709 /*
1710 * we have to complete all requests for ep if it was
1711 * halted, and the halt was cleared by CLEAR_FEATURE
1712 */
1713
1714 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001715 /*
1716 * If we have request in progress,
1717 * then complete it
1718 */
1719 if (ep->req) {
1720 hs_req = ep->req;
1721 ep->req = NULL;
1722 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001723 if (hs_req->req.complete) {
1724 spin_unlock(&hsotg->lock);
1725 usb_gadget_giveback_request(
1726 &ep->ep, &hs_req->req);
1727 spin_lock(&hsotg->lock);
1728 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001729 }
1730
1731 /* If we have pending request, then start it */
John Youn34c0887f2017-01-17 20:31:43 -08001732 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001733 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001734 }
1735
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001736 break;
1737
1738 default:
1739 return -ENOENT;
1740 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001741 break;
1742 default:
1743 return -ENOENT;
1744 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001745 return 1;
1746}
1747
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001748static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001749
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001750/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001751 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001752 * @hsotg: The device state
1753 *
1754 * Set stall for ep0 as response for setup request.
1755 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001756static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001757{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001758 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001759 u32 reg;
1760 u32 ctrl;
1761
1762 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1763 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1764
1765 /*
1766 * DxEPCTL_Stall will be cleared by EP once it has
1767 * taken effect, so no need to clear later.
1768 */
1769
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001770 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001771 ctrl |= DXEPCTL_STALL;
1772 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001773 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001774
1775 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001776 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001777 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001778
1779 /*
1780 * complete won't be called, so we enqueue
1781 * setup request here
1782 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001783 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001784}
1785
1786/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001787 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001788 * @hsotg: The device state
1789 * @ctrl: The control request received
1790 *
1791 * The controller has received the SETUP phase of a control request, and
1792 * needs to work out what to do next (and whether to pass it on to the
1793 * gadget driver).
1794 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001795static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001796 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001797{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001798 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001799 int ret = 0;
1800 u32 dcfg;
1801
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001802 dev_dbg(hsotg->dev,
1803 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1804 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1805 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001806
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001807 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001808 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001809 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1810 } else if (ctrl->bRequestType & USB_DIR_IN) {
1811 ep0->dir_in = 1;
1812 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1813 } else {
1814 ep0->dir_in = 0;
1815 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1816 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001817
1818 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1819 switch (ctrl->bRequest) {
1820 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001821 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001822 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001823 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001824 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1825 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001826 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001827
1828 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1829
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001830 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001831 return;
1832
1833 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001834 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001835 break;
1836
1837 case USB_REQ_CLEAR_FEATURE:
1838 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001839 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001840 break;
1841 }
1842 }
1843
1844 /* as a fallback, try delivering it to the driver to deal with */
1845
1846 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001847 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001848 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001849 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001850 if (ret < 0)
1851 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1852 }
1853
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001854 /*
1855 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001856 * so respond with a STALL for the status stage to indicate failure.
1857 */
1858
Robert Baldygac9f721b2014-01-14 08:36:00 +01001859 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001860 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001861}
1862
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001863/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001864 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001865 * @ep: The endpoint the request was on.
1866 * @req: The request completed.
1867 *
1868 * Called on completion of any requests the driver itself submitted for
1869 * EP0 setup packets
1870 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001871static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001872 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001873{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001874 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001875 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001876
1877 if (req->status < 0) {
1878 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1879 return;
1880 }
1881
Robert Baldyga93f599f2013-11-21 13:49:17 +01001882 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001883 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001884 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001885 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001886 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001887 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001888}
1889
1890/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001891 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001892 * @hsotg: The device state.
1893 *
1894 * Enqueue a request on EP0 if necessary to received any SETUP packets
1895 * received from the host.
1896 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001897static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001898{
1899 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001900 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001901 int ret;
1902
1903 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1904
1905 req->zero = 0;
1906 req->length = 8;
1907 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001908 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001909
1910 if (!list_empty(&hs_req->queue)) {
1911 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1912 return;
1913 }
1914
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001915 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001916 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001917 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001918
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001919 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001920 if (ret < 0) {
1921 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001922 /*
1923 * Don't think there's much we can do other than watch the
1924 * driver fail.
1925 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001926 }
1927}
1928
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001929static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001930 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001931{
1932 u32 ctrl;
1933 u8 index = hs_ep->index;
1934 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1935 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1936
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001937 if (hs_ep->dir_in)
1938 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001939 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001940 else
1941 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001942 index);
1943 if (using_desc_dma(hsotg)) {
1944 /* Not specific buffer needed for ep0 ZLP */
1945 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001946
Minas Harutyunyan201ec562018-01-16 16:03:32 +04001947 if (!index)
1948 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1949
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001950 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1951 } else {
1952 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1953 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1954 epsiz_reg);
1955 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001956
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001957 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001958 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1959 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1960 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001961 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001962}
1963
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001964/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001965 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001966 * @hsotg: The device state.
1967 * @hs_ep: The endpoint the request was on.
1968 * @hs_req: The request to complete.
1969 * @result: The result code (0 => Ok, otherwise errno)
1970 *
1971 * The given request has finished, so call the necessary completion
1972 * if it has one and then look to see if we can start a new request
1973 * on the endpoint.
1974 *
1975 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001976 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001977static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001978 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001979 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001980 int result)
1981{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001982 if (!hs_req) {
1983 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1984 return;
1985 }
1986
1987 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1988 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1989
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001990 /*
1991 * only replace the status if we've not already set an error
1992 * from a previous transaction
1993 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001994
1995 if (hs_req->req.status == -EINPROGRESS)
1996 hs_req->req.status = result;
1997
Yunzhi Li44583fe2015-09-29 12:25:01 +02001998 if (using_dma(hsotg))
1999 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
2000
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002001 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01002002
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002003 hs_ep->req = NULL;
2004 list_del_init(&hs_req->queue);
2005
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002006 /*
2007 * call the complete request with the locks off, just in case the
2008 * request tries to queue more work for this endpoint.
2009 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002010
2011 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02002012 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002013 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002014 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002015 }
2016
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002017 /* In DDMA don't need to proceed to starting of next ISOC request */
2018 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2019 return;
2020
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002021 /*
2022 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002023 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002024 * so be careful when doing this.
2025 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002026
John Youn34c0887f2017-01-17 20:31:43 -08002027 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002028 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002029}
2030
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002031/*
2032 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2033 * @hs_ep: The endpoint the request was on.
2034 *
2035 * Get first request from the ep queue, determine descriptor on which complete
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002036 * happened. SW discovers which descriptor currently in use by HW, adjusts
2037 * dma_address and calculates index of completed descriptor based on the value
2038 * of DEPDMA register. Update actual length of request, giveback to gadget.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002039 */
2040static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2041{
2042 struct dwc2_hsotg *hsotg = hs_ep->parent;
2043 struct dwc2_hsotg_req *hs_req;
2044 struct usb_request *ureq;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002045 u32 desc_sts;
2046 u32 mask;
2047
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002048 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2049
2050 /* Process only descriptors with buffer status set to DMA done */
2051 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2052 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2053
2054 hs_req = get_ep_head(hs_ep);
2055 if (!hs_req) {
2056 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2057 return;
2058 }
2059 ureq = &hs_req->req;
2060
2061 /* Check completion status */
2062 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2063 DEV_DMA_STS_SUCC) {
2064 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2065 DEV_DMA_ISOC_RX_NBYTES_MASK;
2066 ureq->actual = ureq->length - ((desc_sts & mask) >>
2067 DEV_DMA_ISOC_NBYTES_SHIFT);
2068
2069 /* Adjust actual len for ISOC Out if len is
2070 * not align of 4
2071 */
2072 if (!hs_ep->dir_in && ureq->length & 0x3)
2073 ureq->actual += 4 - (ureq->length & 0x3);
2074 }
2075
2076 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2077
2078 hs_ep->compl_desc++;
2079 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_GENERIC - 1))
2080 hs_ep->compl_desc = 0;
2081 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002082 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002083}
2084
2085/*
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002086 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2087 * @hs_ep: The isochronous endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002088 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002089 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2090 * interrupt. Reset target frame and next_desc to allow to start
2091 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2092 * interrupt for OUT direction.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002093 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002094static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002095{
2096 struct dwc2_hsotg *hsotg = hs_ep->parent;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002097
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002098 if (!hs_ep->dir_in)
2099 dwc2_flush_rx_fifo(hsotg);
2100 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002101
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002102 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2103 hs_ep->next_desc = 0;
2104 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002105}
2106
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002107/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002108 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002109 * @hsotg: The device state.
2110 * @ep_idx: The endpoint index for the data
2111 * @size: The size of data in the fifo, in bytes
2112 *
2113 * The FIFO status shows there is data to read from the FIFO for a given
2114 * endpoint, so sort out whether we need to read the data into a request
2115 * that has been made for that endpoint.
2116 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002117static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002118{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002119 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2120 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002121 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002122 int to_read;
2123 int max_req;
2124 int read_ptr;
2125
2126 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002127 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002128 int ptr;
2129
Robert Baldyga6b448af2014-12-16 11:51:44 +01002130 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002131 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002132 __func__, size, ep_idx, epctl);
2133
2134 /* dump the data from the FIFO, we've nothing we can do */
2135 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002136 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002137
2138 return;
2139 }
2140
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002141 to_read = size;
2142 read_ptr = hs_req->req.actual;
2143 max_req = hs_req->req.length - read_ptr;
2144
Ben Dooksa33e7132010-07-19 09:40:49 +01002145 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2146 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2147
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002148 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002149 /*
2150 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002151 * to deal with in this request.
2152 */
2153
2154 /* currently we don't deal this */
2155 WARN_ON_ONCE(1);
2156 }
2157
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002158 hs_ep->total_data += to_read;
2159 hs_req->req.actual += to_read;
2160 to_read = DIV_ROUND_UP(to_read, 4);
2161
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002162 /*
2163 * note, we might over-write the buffer end by 3 bytes depending on
2164 * alignment of the data.
2165 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05002166 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002167}
2168
2169/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002170 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002171 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002172 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002173 *
2174 * Generate a zero-length IN packet request for terminating a SETUP
2175 * transaction.
2176 *
2177 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002178 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002179 * the TxFIFO.
2180 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002181static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002182{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002183 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002184 hsotg->eps_out[0]->dir_in = dir_in;
2185 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002186
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002187 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002188}
2189
Roman Bacikec1f9d92015-09-10 18:13:43 -07002190static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002191 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002192{
2193 u32 ctrl;
2194
2195 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
2196 if (ctrl & DXEPCTL_EOFRNUM)
2197 ctrl |= DXEPCTL_SETEVENFR;
2198 else
2199 ctrl |= DXEPCTL_SETODDFR;
2200 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
2201}
2202
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002203/*
2204 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2205 * @hs_ep - The endpoint on which transfer went
2206 *
2207 * Iterate over endpoints descriptor chain and get info on bytes remained
2208 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2209 */
2210static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2211{
2212 struct dwc2_hsotg *hsotg = hs_ep->parent;
2213 unsigned int bytes_rem = 0;
2214 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2215 int i;
2216 u32 status;
2217
2218 if (!desc)
2219 return -EINVAL;
2220
2221 for (i = 0; i < hs_ep->desc_count; ++i) {
2222 status = desc->status;
2223 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2224
2225 if (status & DEV_DMA_STS_MASK)
2226 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2227 i, status & DEV_DMA_STS_MASK);
2228 }
2229
2230 return bytes_rem;
2231}
2232
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002233/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002234 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002235 * @hsotg: The device instance
2236 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002237 *
2238 * The RXFIFO has delivered an OutDone event, which means that the data
2239 * transfer for an OUT endpoint has been completed, either by a short
2240 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002241 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002242static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002243{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002244 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002245 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2246 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002247 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002248 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002249 int result = 0;
2250
2251 if (!hs_req) {
2252 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2253 return;
2254 }
2255
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002256 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2257 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002258 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2259 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002260 return;
2261 }
2262
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002263 if (using_desc_dma(hsotg))
2264 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2265
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002266 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002267 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002268
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002269 /*
2270 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002271 * is left in the endpoint size register and then working it
2272 * out from the amount we loaded for the transfer.
2273 *
2274 * We need to do this as DMA pointers are always 32bit aligned
2275 * so may overshoot/undershoot the transfer.
2276 */
2277
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002278 size_done = hs_ep->size_loaded - size_left;
2279 size_done += hs_ep->last_load;
2280
2281 req->actual = size_done;
2282 }
2283
Ben Dooksa33e7132010-07-19 09:40:49 +01002284 /* if there is more request to do, schedule new transfer */
2285 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002286 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002287 return;
2288 }
2289
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002290 if (req->actual < req->length && req->short_not_ok) {
2291 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2292 __func__, req->actual, req->length);
2293
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002294 /*
2295 * todo - what should we return here? there's no one else
2296 * even bothering to check the status.
2297 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002298 }
2299
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002300 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2301 if (!using_desc_dma(hsotg) && epnum == 0 &&
2302 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002303 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002304 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002305 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002306 }
2307
Roman Bacikec1f9d92015-09-10 18:13:43 -07002308 /*
2309 * Slave mode OUT transfers do not go through XferComplete so
2310 * adjust the ISOC parity here.
2311 */
2312 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002313 if (hs_ep->isochronous && hs_ep->interval == 1)
2314 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002315 else if (hs_ep->isochronous && hs_ep->interval > 1)
2316 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002317 }
2318
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002319 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002320}
2321
2322/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002323 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002324 * @hsotg: The device instance
2325 *
2326 * The IRQ handler has detected that the RX FIFO has some data in it
2327 * that requires processing, so find out what is in there and do the
2328 * appropriate read.
2329 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002330 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002331 * chunks, so if you have x packets received on an endpoint you'll get x
2332 * FIFO events delivered, each with a packet's worth of data in it.
2333 *
2334 * When using DMA, we should not be processing events from the RXFIFO
2335 * as the actual data should be sent to the memory directly and we turn
2336 * on the completion interrupts to get notifications of transfer completion.
2337 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002338static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002339{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002340 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002341 u32 epnum, status, size;
2342
2343 WARN_ON(using_dma(hsotg));
2344
Dinh Nguyen47a16852014-04-14 14:13:34 -07002345 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2346 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002347
Dinh Nguyen47a16852014-04-14 14:13:34 -07002348 size = grxstsr & GRXSTS_BYTECNT_MASK;
2349 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002350
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002351 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002352 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002353
Dinh Nguyen47a16852014-04-14 14:13:34 -07002354 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2355 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2356 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002357 break;
2358
Dinh Nguyen47a16852014-04-14 14:13:34 -07002359 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002360 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002361 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362
2363 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002364 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002365 break;
2366
Dinh Nguyen47a16852014-04-14 14:13:34 -07002367 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002368 dev_dbg(hsotg->dev,
2369 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002370 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002371 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002372 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002373 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002374 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2375 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2376 */
2377 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002378 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002379 break;
2380
Dinh Nguyen47a16852014-04-14 14:13:34 -07002381 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002382 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002383 break;
2384
Dinh Nguyen47a16852014-04-14 14:13:34 -07002385 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002386 dev_dbg(hsotg->dev,
2387 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002388 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002389 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002390
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002391 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2392
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002393 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002394 break;
2395
2396 default:
2397 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2398 __func__, grxstsr);
2399
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002400 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002401 break;
2402 }
2403}
2404
2405/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002406 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002407 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002408 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002409static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002410{
2411 switch (mps) {
2412 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002413 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002414 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002415 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002416 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002417 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002418 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002419 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002420 }
2421
2422 /* bad max packet size, warn and return invalid result */
2423 WARN_ON(1);
2424 return (u32)-1;
2425}
2426
2427/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002428 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002429 * @hsotg: The driver state.
2430 * @ep: The index number of the endpoint
2431 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002432 * @mc: The multicount value
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002433 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002434 *
2435 * Configure the maximum packet size for the given endpoint, updating
2436 * the hardware control registers to reflect this.
2437 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002438static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002439 unsigned int ep, unsigned int mps,
2440 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002441{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002442 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002443 void __iomem *regs = hsotg->regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002444 u32 reg;
2445
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002446 hs_ep = index_to_ep(hsotg, ep, dir_in);
2447 if (!hs_ep)
2448 return;
2449
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002450 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002451 u32 mps_bytes = mps;
2452
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002453 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002454 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2455 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002456 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002457 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002458 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002459 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002460 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002461 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002462 hs_ep->mc = mc;
2463 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002464 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002465 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002466 }
2467
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002468 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002469 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002470 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002471 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002472 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002473 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002474 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002475 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002476 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002477 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002478 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002479
2480 return;
2481
2482bad_mps:
2483 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2484}
2485
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002486/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002487 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002488 * @hsotg: The driver state
2489 * @idx: The index for the endpoint (0..15)
2490 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002491static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002492{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002493 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2494 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002495
2496 /* wait until the fifo is flushed */
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002497 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2498 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2499 __func__);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002500}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002501
2502/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002503 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002504 * @hsotg: The driver state
2505 * @hs_ep: The driver endpoint to check.
2506 *
2507 * Check to see if there is a request that has data to send, and if so
2508 * make an attempt to write data into the FIFO.
2509 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002510static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002511 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002512{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002513 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002514
Robert Baldygaafcf4162013-09-19 11:50:19 +02002515 if (!hs_ep->dir_in || !hs_req) {
2516 /**
2517 * if request is not enqueued, we disable interrupts
2518 * for endpoints, excepting ep0
2519 */
2520 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002521 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002522 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002523 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002524 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002525
2526 if (hs_req->req.actual < hs_req->req.length) {
2527 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2528 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002529 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002530 }
2531
2532 return 0;
2533}
2534
2535/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002536 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002537 * @hsotg: The device state.
2538 * @hs_ep: The endpoint that has just completed.
2539 *
2540 * An IN transfer has been completed, update the transfer's state and then
2541 * call the relevant completion routines.
2542 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002543static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002544 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002545{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002546 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002547 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002548 int size_left, size_done;
2549
2550 if (!hs_req) {
2551 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2552 return;
2553 }
2554
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002555 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002556 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2557 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002558
2559 /*
2560 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2561 * changed to IN. Change back to complete OUT transfer request
2562 */
2563 hs_ep->dir_in = 0;
2564
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002565 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002566 if (hsotg->test_mode) {
2567 int ret;
2568
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002569 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002570 if (ret < 0) {
2571 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002572 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002573 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002574 return;
2575 }
2576 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002577 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002578 return;
2579 }
2580
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002581 /*
2582 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002583 * in the endpoint size register and then working it out from
2584 * the amount we loaded for the transfer.
2585 *
2586 * We do this even for DMA, as the transfer may have incremented
2587 * past the end of the buffer (DMA transfers are always 32bit
2588 * aligned).
2589 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002590 if (using_desc_dma(hsotg)) {
2591 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2592 if (size_left < 0)
2593 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2594 size_left);
2595 } else {
2596 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2597 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002598
2599 size_done = hs_ep->size_loaded - size_left;
2600 size_done += hs_ep->last_load;
2601
2602 if (hs_req->req.actual != size_done)
2603 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2604 __func__, hs_req->req.actual, size_done);
2605
2606 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002607 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2608 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002609
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002610 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2611 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002612 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002613 return;
2614 }
2615
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002616 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002617 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002618 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002619 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002620 /* transfer will be completed on next complete interrupt */
2621 return;
2622 }
2623
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002624 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2625 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002626 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002627 return;
2628 }
2629
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002630 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002631}
2632
2633/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002634 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2635 * @hsotg: The device state.
2636 * @idx: Index of ep.
2637 * @dir_in: Endpoint direction 1-in 0-out.
2638 *
2639 * Reads for endpoint with given index and direction, by masking
2640 * epint_reg with coresponding mask.
2641 */
2642static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2643 unsigned int idx, int dir_in)
2644{
2645 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2646 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2647 u32 ints;
2648 u32 mask;
2649 u32 diepempmsk;
2650
2651 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2652 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2653 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2654 mask |= DXEPINT_SETUP_RCVD;
2655
2656 ints = dwc2_readl(hsotg->regs + epint_reg);
2657 ints &= mask;
2658 return ints;
2659}
2660
2661/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002662 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2663 * @hs_ep: The endpoint on which interrupt is asserted.
2664 *
2665 * This interrupt indicates that the endpoint has been disabled per the
2666 * application's request.
2667 *
2668 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2669 * in case of ISOC completes current request.
2670 *
2671 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2672 * request starts it.
2673 */
2674static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2675{
2676 struct dwc2_hsotg *hsotg = hs_ep->parent;
2677 struct dwc2_hsotg_req *hs_req;
2678 unsigned char idx = hs_ep->index;
2679 int dir_in = hs_ep->dir_in;
2680 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2681 int dctl = dwc2_readl(hsotg->regs + DCTL);
2682
2683 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2684
2685 if (dir_in) {
2686 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2687
2688 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2689
2690 if (hs_ep->isochronous) {
2691 dwc2_hsotg_complete_in(hsotg, hs_ep);
2692 return;
2693 }
2694
2695 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2696 int dctl = dwc2_readl(hsotg->regs + DCTL);
2697
2698 dctl |= DCTL_CGNPINNAK;
2699 dwc2_writel(dctl, hsotg->regs + DCTL);
2700 }
2701 return;
2702 }
2703
2704 if (dctl & DCTL_GOUTNAKSTS) {
2705 dctl |= DCTL_CGOUTNAK;
2706 dwc2_writel(dctl, hsotg->regs + DCTL);
2707 }
2708
2709 if (!hs_ep->isochronous)
2710 return;
2711
2712 if (list_empty(&hs_ep->queue)) {
2713 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2714 __func__, hs_ep);
2715 return;
2716 }
2717
2718 do {
2719 hs_req = get_ep_head(hs_ep);
2720 if (hs_req)
2721 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2722 -ENODATA);
2723 dwc2_gadget_incr_frame_num(hs_ep);
Artur Petrosyanc7c24e72018-05-05 09:46:26 -04002724 /* Update current frame number value. */
2725 hsotg->frame_number = dwc2_hsotg_read_frameno(hsotg);
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002726 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2727
2728 dwc2_gadget_start_next_request(hs_ep);
2729}
2730
2731/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002732 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04002733 * @ep: The endpoint on which interrupt is asserted.
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002734 *
2735 * This is starting point for ISOC-OUT transfer, synchronization done with
2736 * first out token received from host while corresponding EP is disabled.
2737 *
2738 * Device does not know initial frame in which out token will come. For this
2739 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2740 * getting this interrupt SW starts calculation for next transfer frame.
2741 */
2742static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2743{
2744 struct dwc2_hsotg *hsotg = ep->parent;
2745 int dir_in = ep->dir_in;
2746 u32 doepmsk;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002747 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002748
2749 if (dir_in || !ep->isochronous)
2750 return;
2751
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002752 /*
2753 * Store frame in which irq was asserted here, as
2754 * it can change while completing request below.
2755 */
2756 tmp = dwc2_hsotg_read_frameno(hsotg);
2757
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002758 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), 0);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002759
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002760 if (using_desc_dma(hsotg)) {
2761 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2762 /* Start first ISO Out */
2763 ep->target_frame = tmp;
2764 dwc2_gadget_start_isoc_ddma(ep);
2765 }
2766 return;
2767 }
2768
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002769 if (ep->interval > 1 &&
2770 ep->target_frame == TARGET_FRAME_INITIAL) {
2771 u32 dsts;
2772 u32 ctrl;
2773
2774 dsts = dwc2_readl(hsotg->regs + DSTS);
2775 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2776 dwc2_gadget_incr_frame_num(ep);
2777
2778 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2779 if (ep->target_frame & 0x1)
2780 ctrl |= DXEPCTL_SETODDFR;
2781 else
2782 ctrl |= DXEPCTL_SETEVENFR;
2783
2784 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2785 }
2786
2787 dwc2_gadget_start_next_request(ep);
2788 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2789 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2790 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2791}
2792
2793/**
John Youn38beaec2017-01-17 20:31:13 -08002794 * dwc2_gadget_handle_nak - handle NAK interrupt
2795 * @hs_ep: The endpoint on which interrupt is asserted.
2796 *
2797 * This is starting point for ISOC-IN transfer, synchronization done with
2798 * first IN token received from host while corresponding EP is disabled.
2799 *
2800 * Device does not know when first one token will arrive from host. On first
2801 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2802 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2803 * sent in response to that as there was no data in FIFO. SW is basing on this
2804 * interrupt to obtain frame in which token has come and then based on the
2805 * interval calculates next frame for transfer.
2806 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002807static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2808{
2809 struct dwc2_hsotg *hsotg = hs_ep->parent;
2810 int dir_in = hs_ep->dir_in;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002811 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002812
2813 if (!dir_in || !hs_ep->isochronous)
2814 return;
2815
2816 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002817
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002818 tmp = dwc2_hsotg_read_frameno(hsotg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002819 if (using_desc_dma(hsotg)) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002820 dwc2_hsotg_complete_request(hsotg, hs_ep,
2821 get_ep_head(hs_ep), 0);
2822
2823 hs_ep->target_frame = tmp;
2824 dwc2_gadget_incr_frame_num(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002825 dwc2_gadget_start_isoc_ddma(hs_ep);
2826 return;
2827 }
2828
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002829 hs_ep->target_frame = tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002830 if (hs_ep->interval > 1) {
2831 u32 ctrl = dwc2_readl(hsotg->regs +
2832 DIEPCTL(hs_ep->index));
2833 if (hs_ep->target_frame & 0x1)
2834 ctrl |= DXEPCTL_SETODDFR;
2835 else
2836 ctrl |= DXEPCTL_SETEVENFR;
2837
2838 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2839 }
2840
2841 dwc2_hsotg_complete_request(hsotg, hs_ep,
2842 get_ep_head(hs_ep), 0);
2843 }
2844
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002845 if (!using_desc_dma(hsotg))
2846 dwc2_gadget_incr_frame_num(hs_ep);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002847}
2848
2849/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002850 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002851 * @hsotg: The driver state
2852 * @idx: The index for the endpoint (0..15)
2853 * @dir_in: Set if this is an IN endpoint
2854 *
2855 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002856 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002857static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002858 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002859{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002860 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002861 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2862 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2863 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002864 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002865 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002866
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002867 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002868 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002869
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002870 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002871 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002872
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002873 if (!hs_ep) {
2874 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002875 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002876 return;
2877 }
2878
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002879 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2880 __func__, idx, dir_in ? "in" : "out", ints);
2881
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002882 /* Don't process XferCompl interrupt if it is a setup packet */
2883 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2884 ints &= ~DXEPINT_XFERCOMPL;
2885
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002886 /*
2887 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2888 * stage and xfercomplete was generated without SETUP phase done
2889 * interrupt. SW should parse received setup packet only after host's
2890 * exit from setup phase of control transfer.
2891 */
2892 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2893 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2894 ints &= ~DXEPINT_XFERCOMPL;
2895
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002896 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002897 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002898 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002899 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2900 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002901
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002902 /* In DDMA handle isochronous requests separately */
2903 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002904 /* XferCompl set along with BNA */
2905 if (!(ints & DXEPINT_BNAINTR))
2906 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002907 } else if (dir_in) {
2908 /*
2909 * We get OutDone from the FIFO, so we only
2910 * need to look at completing IN requests here
2911 * if operating slave mode
2912 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002913 if (hs_ep->isochronous && hs_ep->interval > 1)
2914 dwc2_gadget_incr_frame_num(hs_ep);
2915
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002916 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002917 if (ints & DXEPINT_NAKINTRPT)
2918 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002919
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002920 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002921 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002922 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002923 /*
2924 * We're using DMA, we need to fire an OutDone here
2925 * as we ignore the RXFIFO.
2926 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002927 if (hs_ep->isochronous && hs_ep->interval > 1)
2928 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002929
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002930 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002931 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002932 }
2933
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002934 if (ints & DXEPINT_EPDISBLD)
2935 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002936
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002937 if (ints & DXEPINT_OUTTKNEPDIS)
2938 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2939
2940 if (ints & DXEPINT_NAKINTRPT)
2941 dwc2_gadget_handle_nak(hs_ep);
2942
Dinh Nguyen47a16852014-04-14 14:13:34 -07002943 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002944 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002945
Dinh Nguyen47a16852014-04-14 14:13:34 -07002946 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002947 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2948
2949 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002950 /*
2951 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002952 * setup packet. In non-DMA mode we'd get this
2953 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002954 * the setup here.
2955 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002956
2957 if (dir_in)
2958 WARN_ON_ONCE(1);
2959 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002960 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002961 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002962 }
2963
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002964 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08002965 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
2966
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04002967 /* Safety check EP0 state when STSPHSERCVD asserted */
2968 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
2969 /* Move to STATUS IN for DDMA */
2970 if (using_desc_dma(hsotg))
2971 dwc2_hsotg_ep0_zlp(hsotg, true);
2972 }
2973
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002974 }
2975
Dinh Nguyen47a16852014-04-14 14:13:34 -07002976 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002977 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002978
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002979 if (ints & DXEPINT_BNAINTR) {
2980 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002981 if (hs_ep->isochronous)
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002982 dwc2_gadget_handle_isoc_bna(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002983 }
2984
Robert Baldyga1479e842013-10-09 08:41:57 +02002985 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002986 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002987 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002988 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2989 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002990 }
2991
2992 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002993 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002994 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2995 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002996 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002997
2998 /* FIFO has space or is empty (see GAHBCFG) */
2999 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07003000 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01003001 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
3002 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09003003 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003004 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01003005 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003006 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003007}
3008
3009/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003010 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003011 * @hsotg: The device state.
3012 *
3013 * Handle updating the device settings after the enumeration phase has
3014 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003015 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003016static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003017{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003018 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003019 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003020
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003021 /*
3022 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003023 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003024 * we connected at.
3025 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026
3027 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3028
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003029 /*
3030 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003031 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003032 * not advertise a 64byte MPS on EP0.
3033 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003034
3035 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003036 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003037 case DSTS_ENUMSPD_FS:
3038 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003039 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003040 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003041 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003042 break;
3043
Dinh Nguyen47a16852014-04-14 14:13:34 -07003044 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003045 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003046 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003047 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003048 break;
3049
Dinh Nguyen47a16852014-04-14 14:13:34 -07003050 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003051 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003052 ep0_mps = 8;
3053 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003054 /*
3055 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003056 * moment, and the documentation seems to imply that it isn't
3057 * supported by the PHYs on some of the devices.
3058 */
3059 break;
3060 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003061 dev_info(hsotg->dev, "new device is %s\n",
3062 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003063
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003064 /*
3065 * we should now know the maximum packet size for an
3066 * endpoint, so set the endpoints to a default value.
3067 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003068
3069 if (ep0_mps) {
3070 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003071 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003072 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3073 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003074 for (i = 1; i < hsotg->num_of_eps; i++) {
3075 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003076 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3077 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003078 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003079 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3080 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003081 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082 }
3083
3084 /* ensure after enumeration our EP0 is active */
3085
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003086 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003087
3088 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003089 dwc2_readl(hsotg->regs + DIEPCTL0),
3090 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003091}
3092
3093/**
3094 * kill_all_requests - remove all requests from the endpoint's queue
3095 * @hsotg: The device state.
3096 * @ep: The endpoint the requests may be on.
3097 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003098 *
3099 * Go through the requests on the given endpoint and mark them
3100 * completed with the given result code.
3101 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003102static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003103 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01003104 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003105{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003106 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003107 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003108
Robert Baldyga6b448af2014-12-16 11:51:44 +01003109 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003110
Robert Baldyga6b448af2014-12-16 11:51:44 +01003111 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003112 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003113 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01003114
Robert Baldygab203d0a2014-09-09 10:44:56 +02003115 if (!hsotg->dedicated_fifos)
3116 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07003117 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003118 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003119 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003120}
3121
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003122/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003123 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003124 * @hsotg: The device state.
3125 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003126 * The device has been disconnected. Remove all current
3127 * transactions and signal the gadget driver that this
3128 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003129 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003130void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003131{
John Youn9da51972017-01-17 20:30:27 -08003132 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003133
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003134 if (!hsotg->connected)
3135 return;
3136
3137 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003138 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003139
3140 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3141 if (hsotg->eps_in[ep])
3142 kill_all_requests(hsotg, hsotg->eps_in[ep],
John Youn9da51972017-01-17 20:30:27 -08003143 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003144 if (hsotg->eps_out[ep])
3145 kill_all_requests(hsotg, hsotg->eps_out[ep],
John Youn9da51972017-01-17 20:30:27 -08003146 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003147 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003148
3149 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003150 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003151
3152 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003153}
3154
3155/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003156 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003157 * @hsotg: The device state:
3158 * @periodic: True if this is a periodic FIFO interrupt
3159 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003160static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003161{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003162 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003163 int epno, ret;
3164
3165 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003166 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003167 ep = index_to_ep(hsotg, epno, 1);
3168
3169 if (!ep)
3170 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003171
3172 if (!ep->dir_in)
3173 continue;
3174
3175 if ((periodic && !ep->periodic) ||
3176 (!periodic && ep->periodic))
3177 continue;
3178
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003179 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003180 if (ret < 0)
3181 break;
3182 }
3183}
3184
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003185/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003186#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3187 GINTSTS_PTXFEMP | \
3188 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003189
3190/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003191 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003192 * @hsotg: The device state
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04003193 * @is_usb_reset: Usb resetting flag
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003194 *
3195 * Issue a soft reset to the core, and await the core finishing it.
3196 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003197void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003198 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003199{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003200 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003201 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003202 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003203 u32 dcfg = 0;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003204
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003205 /* Kill any ep0 requests as controller will be reinitialized */
3206 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3207
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003208 if (!is_usb_reset)
John Stultz6e6360b2017-01-23 14:59:14 -08003209 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003210 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02003211
3212 /*
3213 * we must now enable ep0 ready for host detection and then
3214 * set configuration.
3215 */
3216
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003217 /* keep other bits untouched (so e.g. forced modes are not lost) */
3218 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3219 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01003220 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003221
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003222 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003223 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3224 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003225 /* FS/LS Dedicated Transceiver Interface */
3226 usbcfg |= GUSBCFG_PHYSEL;
3227 } else {
3228 /* set the PLL on, remove the HNP/SRP and set the PHY */
3229 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3230 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3231 (val << GUSBCFG_USBTRDTIM_SHIFT);
3232 }
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003233 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003234
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003235 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003236
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003237 if (!is_usb_reset)
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003238 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003239
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003240 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003241
3242 switch (hsotg->params.speed) {
3243 case DWC2_SPEED_PARAM_LOW:
3244 dcfg |= DCFG_DEVSPD_LS;
3245 break;
3246 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003247 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3248 dcfg |= DCFG_DEVSPD_FS48;
3249 else
3250 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003251 break;
3252 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003253 dcfg |= DCFG_DEVSPD_HS;
3254 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003255
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +04003256 if (hsotg->params.ipg_isoc_en)
3257 dcfg |= DCFG_IPG_ISOC_SUPPORDED;
3258
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003259 dwc2_writel(dcfg, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003260
3261 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003262 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003263
3264 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003265 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003266 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003267 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003268 GINTSTS_USBRST | GINTSTS_RESETDET |
3269 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003270 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3271 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003272
3273 if (!using_desc_dma(hsotg))
3274 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003275
John Youn95832c02017-01-23 14:57:26 -08003276 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003277 intmsk |= GINTSTS_CONIDSTSCHNG;
3278
3279 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003280
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003281 if (using_dma(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003282 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003283 hsotg->params.ahbcfg,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003284 hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003285
3286 /* Set DDMA mode support in the core if needed */
3287 if (using_desc_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003288 dwc2_set_bit(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003289
3290 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003291 dwc2_writel(((hsotg->dedicated_fifos) ?
3292 (GAHBCFG_NP_TXF_EMP_LVL |
3293 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3294 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003295 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003296
3297 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003298 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3299 * when we have no data to transfer. Otherwise we get being flooded by
3300 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003301 */
3302
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003303 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003304 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003305 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003306 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003307 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003308
3309 /*
3310 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003311 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003312 */
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003313 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3314 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003315 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003316 DOEPMSK_SETUPMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003317 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003318
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003319 /* Enable BNA interrupt for DDMA */
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003320 if (using_desc_dma(hsotg)) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003321 dwc2_set_bit(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003322 dwc2_set_bit(hsotg->regs + DIEPMSK, DIEPMSK_BNAININTRMSK);
3323 }
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003324
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003325 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003326
3327 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003328 dwc2_readl(hsotg->regs + DIEPCTL0),
3329 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003330
3331 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003332 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003333
3334 /*
3335 * Enable the RXFIFO when in slave mode, as this is how we collect
3336 * the data. In DMA mode, we get events from the FIFO but also
3337 * things we cannot process, so do not use it.
3338 */
3339 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003340 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003341
3342 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003343 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3344 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003345
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003346 if (!is_usb_reset) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003347 dwc2_set_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003348 udelay(10); /* see openiboot */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003349 dwc2_clear_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003350 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003351
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003352 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003353
3354 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003355 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003356 * writing to the EPCTL register..
3357 */
3358
3359 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003360 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003361 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003362
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003363 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003364 DXEPCTL_CNAK | DXEPCTL_EPENA |
3365 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003366 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003367
3368 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003369 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003370 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003371
Lukasz Majewski308d7342012-05-04 14:17:05 +02003372 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003373 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3374 if (!is_usb_reset)
3375 val |= DCTL_SFTDISCON;
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003376 dwc2_set_bit(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003377
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003378 /* configure the core to support LPM */
3379 dwc2_gadget_init_lpm(hsotg);
3380
Lukasz Majewski308d7342012-05-04 14:17:05 +02003381 /* must be at-least 3ms to allow bus to see disconnect */
3382 mdelay(3);
3383
Gregory Herrero065d3932015-09-22 15:16:54 +02003384 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003385
3386 dwc2_hsotg_enqueue_setup(hsotg);
3387
3388 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3389 dwc2_readl(hsotg->regs + DIEPCTL0),
3390 dwc2_readl(hsotg->regs + DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003391}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003392
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003393static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003394{
3395 /* set the soft-disconnect bit */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003396 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003397}
3398
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003399void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003400{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003401 /* remove the soft-disconnect and let's go */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003402 dwc2_clear_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003403}
3404
3405/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003406 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3407 * @hsotg: The device state:
3408 *
3409 * This interrupt indicates one of the following conditions occurred while
3410 * transmitting an ISOC transaction.
3411 * - Corrupted IN Token for ISOC EP.
3412 * - Packet not complete in FIFO.
3413 *
3414 * The following actions will be taken:
3415 * - Determine the EP
3416 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3417 */
3418static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3419{
3420 struct dwc2_hsotg_ep *hs_ep;
3421 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003422 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003423 u32 idx;
3424
3425 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3426
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003427 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3428
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003429 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003430 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003431 /* Proceed only unmasked ISOC EPs */
3432 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3433 continue;
3434
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003435 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003436 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003437 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3438 epctrl |= DXEPCTL_SNAK;
3439 epctrl |= DXEPCTL_EPDIS;
3440 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3441 }
3442 }
3443
3444 /* Clear interrupt */
3445 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3446}
3447
3448/**
3449 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3450 * @hsotg: The device state:
3451 *
3452 * This interrupt indicates one of the following conditions occurred while
3453 * transmitting an ISOC transaction.
3454 * - Corrupted OUT Token for ISOC EP.
3455 * - Packet not complete in FIFO.
3456 *
3457 * The following actions will be taken:
3458 * - Determine the EP
3459 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3460 */
3461static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3462{
3463 u32 gintsts;
3464 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003465 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003466 u32 epctrl;
3467 struct dwc2_hsotg_ep *hs_ep;
3468 int idx;
3469
3470 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3471
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003472 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3473 daintmsk >>= DAINT_OUTEP_SHIFT;
3474
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003475 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003476 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003477 /* Proceed only unmasked ISOC EPs */
3478 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3479 continue;
3480
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003481 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003482 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003483 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3484 /* Unmask GOUTNAKEFF interrupt */
3485 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3486 gintmsk |= GINTSTS_GOUTNAKEFF;
3487 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3488
3489 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003490 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003491 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003492 break;
3493 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003494 }
3495 }
3496
3497 /* Clear interrupt */
3498 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3499}
3500
3501/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003502 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003503 * @irq: The IRQ number triggered
3504 * @pw: The pw value when registered the handler.
3505 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003506static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003507{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003508 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003509 int retry_count = 8;
3510 u32 gintsts;
3511 u32 gintmsk;
3512
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003513 if (!dwc2_is_device_mode(hsotg))
3514 return IRQ_NONE;
3515
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003516 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003517irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003518 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3519 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003520
3521 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3522 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3523
3524 gintsts &= gintmsk;
3525
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003526 if (gintsts & GINTSTS_RESETDET) {
3527 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3528
3529 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3530
3531 /* This event must be used only if controller is suspended */
3532 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003533 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003534 hsotg->lx_state = DWC2_L0;
3535 }
3536 }
3537
3538 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003539 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3540 u32 connected = hsotg->connected;
3541
3542 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3543 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3544 dwc2_readl(hsotg->regs + GNPTXSTS));
3545
3546 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3547
3548 /* Report disconnection if it is not already done. */
3549 dwc2_hsotg_disconnect(hsotg);
3550
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003551 /* Reset device address to zero */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003552 dwc2_clear_bit(hsotg->regs + DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003553
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003554 if (usb_status & GOTGCTL_BSESVLD && connected)
3555 dwc2_hsotg_core_init_disconnected(hsotg, true);
3556 }
3557
Dinh Nguyen47a16852014-04-14 14:13:34 -07003558 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003559 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003560
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003561 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003562 }
3563
Dinh Nguyen47a16852014-04-14 14:13:34 -07003564 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003565 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3566 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003567 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003568 int ep;
3569
Robert Baldyga7e804652013-09-19 11:50:20 +02003570 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003571 daint_out = daint >> DAINT_OUTEP_SHIFT;
3572 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003573
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003574 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3575
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003576 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3577 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003578 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003579 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003580 }
3581
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003582 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3583 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003584 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003585 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003586 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003587 }
3588
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003589 /* check both FIFOs */
3590
Dinh Nguyen47a16852014-04-14 14:13:34 -07003591 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003592 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3593
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003594 /*
3595 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003596 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003597 * it needs re-enabling
3598 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003599
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003600 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3601 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003602 }
3603
Dinh Nguyen47a16852014-04-14 14:13:34 -07003604 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003605 dev_dbg(hsotg->dev, "PTxFEmp\n");
3606
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003607 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003608
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003609 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3610 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003611 }
3612
Dinh Nguyen47a16852014-04-14 14:13:34 -07003613 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003614 /*
3615 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003616 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003617 * set.
3618 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003619
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003620 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003621 }
3622
Dinh Nguyen47a16852014-04-14 14:13:34 -07003623 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003624 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003625 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003626 }
3627
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003628 /*
3629 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003630 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003631 * the occurrence.
3632 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003633
Dinh Nguyen47a16852014-04-14 14:13:34 -07003634 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003635 u8 idx;
3636 u32 epctrl;
3637 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003638 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003639 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003640
Razmik Karapetyand8484552018-01-19 14:41:42 +04003641 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3642 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003643 /* Mask this interrupt */
3644 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3645 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3646 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003647
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003648 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003649 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003650 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003651 /* Proceed only unmasked ISOC EPs */
3652 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3653 continue;
3654
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003655 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3656
Razmik Karapetyand8484552018-01-19 14:41:42 +04003657 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003658 epctrl |= DXEPCTL_SNAK;
3659 epctrl |= DXEPCTL_EPDIS;
3660 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3661 }
3662 }
3663
3664 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003665 }
3666
Dinh Nguyen47a16852014-04-14 14:13:34 -07003667 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003668 dev_info(hsotg->dev, "GINNakEff triggered\n");
3669
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003670 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003671
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003672 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003673 }
3674
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003675 if (gintsts & GINTSTS_INCOMPL_SOIN)
3676 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003677
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003678 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3679 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003680
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003681 /*
3682 * if we've had fifo events, we should try and go around the
3683 * loop again to see if there's any point in returning yet.
3684 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003685
3686 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003687 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003688
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003689 spin_unlock(&hsotg->lock);
3690
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003691 return IRQ_HANDLED;
3692}
3693
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003694static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3695 struct dwc2_hsotg_ep *hs_ep)
3696{
3697 u32 epctrl_reg;
3698 u32 epint_reg;
3699
3700 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3701 DOEPCTL(hs_ep->index);
3702 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3703 DOEPINT(hs_ep->index);
3704
3705 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3706 hs_ep->name);
3707
3708 if (hs_ep->dir_in) {
3709 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003710 dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003711 /* Wait for Nak effect */
3712 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3713 DXEPINT_INEPNAKEFF, 100))
3714 dev_warn(hsotg->dev,
3715 "%s: timeout DIEPINT.NAKEFF\n",
3716 __func__);
3717 } else {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003718 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003719 /* Wait for Nak effect */
3720 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3721 GINTSTS_GINNAKEFF, 100))
3722 dev_warn(hsotg->dev,
3723 "%s: timeout GINTSTS.GINNAKEFF\n",
3724 __func__);
3725 }
3726 } else {
3727 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003728 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003729
3730 /* Wait for global nak to take effect */
3731 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3732 GINTSTS_GOUTNAKEFF, 100))
3733 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3734 __func__);
3735 }
3736
3737 /* Disable ep */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003738 dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003739
3740 /* Wait for ep to be disabled */
3741 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3742 dev_warn(hsotg->dev,
3743 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3744
3745 /* Clear EPDISBLD interrupt */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003746 dwc2_set_bit(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003747
3748 if (hs_ep->dir_in) {
3749 unsigned short fifo_index;
3750
3751 if (hsotg->dedicated_fifos || hs_ep->periodic)
3752 fifo_index = hs_ep->fifo_index;
3753 else
3754 fifo_index = 0;
3755
3756 /* Flush TX FIFO */
3757 dwc2_flush_tx_fifo(hsotg, fifo_index);
3758
3759 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3760 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003761 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003762
3763 } else {
3764 /* Remove global NAKs */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003765 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003766 }
3767}
3768
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003769/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003770 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003771 * @ep: The USB endpint to configure
3772 * @desc: The USB endpoint descriptor to configure with.
3773 *
3774 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003775 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003776static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003777 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003778{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003779 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003780 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003781 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003782 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003783 u32 epctrl_reg;
3784 u32 epctrl;
3785 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003786 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003787 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003788 unsigned int dir_in;
3789 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003790 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003791 unsigned char ep_type;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003792
3793 dev_dbg(hsotg->dev,
3794 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3795 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3796 desc->wMaxPacketSize, desc->bInterval);
3797
3798 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003799 if (index == 0) {
3800 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3801 return -EINVAL;
3802 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003803
3804 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3805 if (dir_in != hs_ep->dir_in) {
3806 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3807 return -EINVAL;
3808 }
3809
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003810 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003811 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003812 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003813
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003814 /* ISOC IN in DDMA supported bInterval up to 10 */
3815 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3816 dir_in && desc->bInterval > 10) {
3817 dev_err(hsotg->dev,
3818 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3819 return -EINVAL;
3820 }
3821
3822 /* High bandwidth ISOC OUT in DDMA not supported */
3823 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3824 !dir_in && mc > 1) {
3825 dev_err(hsotg->dev,
3826 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3827 return -EINVAL;
3828 }
3829
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003830 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003831
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003832 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003833 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003834
3835 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3836 __func__, epctrl, epctrl_reg);
3837
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003838 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003839 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3840 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003841 MAX_DMA_DESC_NUM_GENERIC *
3842 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003843 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003844 if (!hs_ep->desc_list) {
3845 ret = -ENOMEM;
3846 goto error2;
3847 }
3848 }
3849
Lukasz Majewski22258f42012-06-14 10:02:24 +02003850 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003851
Dinh Nguyen47a16852014-04-14 14:13:34 -07003852 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3853 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003854
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003855 /*
3856 * mark the endpoint as active, otherwise the core may ignore
3857 * transactions entirely for this endpoint
3858 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003859 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003860
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003861 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003862 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003863
3864 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003865 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003866 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003867 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003868 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003869
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003870 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003871 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003872 epctrl |= DXEPCTL_EPTYPE_ISO;
3873 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003874 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003875 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003876 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003877 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003878 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003879 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003880 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003881 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3882 mask |= DIEPMSK_NAKMSK;
3883 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3884 } else {
3885 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3886 mask |= DOEPMSK_OUTTKNEPDISMSK;
3887 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3888 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003889 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003890
3891 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003892 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003893 break;
3894
3895 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003896 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003897 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003898
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003899 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3900 hs_ep->interval = 1 << (desc->bInterval - 1);
3901
Dinh Nguyen47a16852014-04-14 14:13:34 -07003902 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003903 break;
3904
3905 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003906 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003907 break;
3908 }
3909
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003910 /*
3911 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003912 * a unique tx-fifo even if it is non-periodic.
3913 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003914 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003915 u32 fifo_index = 0;
3916 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08003917
3918 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003919 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08003920 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02003921 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003922 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08003923 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003924 if (val < size)
3925 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003926 /* Search for smallest acceptable fifo */
3927 if (val < fifo_size) {
3928 fifo_size = val;
3929 fifo_index = i;
3930 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003931 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003932 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003933 dev_err(hsotg->dev,
3934 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303935 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003936 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303937 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003938 hsotg->fifo_map |= 1 << fifo_index;
3939 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3940 hs_ep->fifo_index = fifo_index;
3941 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003942 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003943
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003944 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003945 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003946 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003947
Artur Petrosyan52953222018-04-16 08:45:31 -04003948 /* WA for Full speed ISOC IN in DDMA mode.
3949 * By Clear NAK status of EP, core will send ZLP
3950 * to IN token and assert NAK interrupt relying
3951 * on TxFIFO status only
3952 */
3953
3954 if (hsotg->gadget.speed == USB_SPEED_FULL &&
3955 hs_ep->isochronous && dir_in) {
3956 /* The WA applies only to core versions from 2.72a
3957 * to 4.00a (including both). Also for FS_IOT_1.00a
3958 * and HS_IOT_1.00a.
3959 */
3960 u32 gsnpsid = dwc2_readl(hsotg->regs + GSNPSID);
3961
3962 if ((gsnpsid >= DWC2_CORE_REV_2_72a &&
3963 gsnpsid <= DWC2_CORE_REV_4_00a) ||
3964 gsnpsid == DWC2_FS_IOT_REV_1_00a ||
3965 gsnpsid == DWC2_HS_IOT_REV_1_00a)
3966 epctrl |= DXEPCTL_CNAK;
3967 }
3968
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003969 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3970 __func__, epctrl);
3971
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003972 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003973 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003974 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003975
3976 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003977 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003978
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003979error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003980 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003981
3982error2:
3983 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003984 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003985 sizeof(struct dwc2_dma_desc),
3986 hs_ep->desc_list, hs_ep->desc_list_dma);
3987 hs_ep->desc_list = NULL;
3988 }
3989
Julia Lawall19c190f2010-03-29 17:36:44 +02003990 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003991}
3992
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003993/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003994 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003995 * @ep: The endpoint to disable.
3996 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003997static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003998{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003999 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004000 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004001 int dir_in = hs_ep->dir_in;
4002 int index = hs_ep->index;
4003 unsigned long flags;
4004 u32 epctrl_reg;
4005 u32 ctrl;
4006
Marek Szyprowski1e011292014-09-09 10:44:54 +02004007 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004008
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004009 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004010 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
4011 return -EINVAL;
4012 }
4013
John Stultz9b4810922017-10-23 14:32:49 -07004014 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4015 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
4016 return -EINVAL;
4017 }
4018
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02004019 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004020
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004021 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004022
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004023 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08004024
4025 if (ctrl & DXEPCTL_EPENA)
4026 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
4027
Dinh Nguyen47a16852014-04-14 14:13:34 -07004028 ctrl &= ~DXEPCTL_EPENA;
4029 ctrl &= ~DXEPCTL_USBACTEP;
4030 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004031
4032 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004033 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004034
4035 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004036 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004037
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01004038 /* terminate all requests with shutdown */
4039 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4040
Robert Baldyga1c07b202016-08-29 13:39:00 -07004041 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4042 hs_ep->fifo_index = 0;
4043 hs_ep->fifo_size = 0;
4044
Lukasz Majewski22258f42012-06-14 10:02:24 +02004045 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004046 return 0;
4047}
4048
4049/**
4050 * on_list - check request is on the given endpoint
4051 * @ep: The endpoint to check.
4052 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004053 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004054static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004055{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004056 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004057
4058 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4059 if (req == test)
4060 return true;
4061 }
4062
4063 return false;
4064}
4065
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004066/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004067 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004068 * @ep: The endpoint to dequeue.
4069 * @req: The request to be removed from a queue.
4070 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004071static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004072{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004073 struct dwc2_hsotg_req *hs_req = our_req(req);
4074 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004075 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004076 unsigned long flags;
4077
Marek Szyprowski1e011292014-09-09 10:44:54 +02004078 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004079
Lukasz Majewski22258f42012-06-14 10:02:24 +02004080 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004081
4082 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004083 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004084 return -EINVAL;
4085 }
4086
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004087 /* Dequeue already started request */
4088 if (req == &hs_ep->req->req)
4089 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4090
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004091 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004092 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004093
4094 return 0;
4095}
4096
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004097/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004098 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004099 * @ep: The endpoint to set halt.
4100 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004101 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4102 * the endpoint is busy processing requests.
4103 *
4104 * We need to stall the endpoint immediately if request comes from set_feature
4105 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004106 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004107static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004108{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004109 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004110 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004111 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004112 u32 epreg;
4113 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004114 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004115
4116 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4117
Robert Baldygac9f721b2014-01-14 08:36:00 +01004118 if (index == 0) {
4119 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004120 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004121 else
4122 dev_warn(hs->dev,
4123 "%s: can't clear halt on ep0\n", __func__);
4124 return 0;
4125 }
4126
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004127 if (hs_ep->isochronous) {
4128 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4129 return -EINVAL;
4130 }
4131
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004132 if (!now && value && !list_empty(&hs_ep->queue)) {
4133 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4134 ep->name);
4135 return -EAGAIN;
4136 }
4137
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004138 if (hs_ep->dir_in) {
4139 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004140 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004141
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004142 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004143 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004144 if (epctl & DXEPCTL_EPENA)
4145 epctl |= DXEPCTL_EPDIS;
4146 } else {
4147 epctl &= ~DXEPCTL_STALL;
4148 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4149 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004150 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004151 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004152 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004153 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004154 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004155 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004156 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004157
John Youn34c0887f2017-01-17 20:31:43 -08004158 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004159 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004160 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004161 epctl &= ~DXEPCTL_STALL;
4162 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4163 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004164 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004165 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004166 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004167 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004168 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004169
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004170 hs_ep->halted = value;
4171
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004172 return 0;
4173}
4174
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004175/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004176 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004177 * @ep: The endpoint to set halt.
4178 * @value: Set or unset the halt.
4179 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004180static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004181{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004182 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004183 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004184 unsigned long flags = 0;
4185 int ret = 0;
4186
4187 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004188 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004189 spin_unlock_irqrestore(&hs->lock, flags);
4190
4191 return ret;
4192}
4193
Bhumika Goyalebce5612017-08-12 17:34:55 +05304194static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004195 .enable = dwc2_hsotg_ep_enable,
4196 .disable = dwc2_hsotg_ep_disable,
4197 .alloc_request = dwc2_hsotg_ep_alloc_request,
4198 .free_request = dwc2_hsotg_ep_free_request,
4199 .queue = dwc2_hsotg_ep_queue_lock,
4200 .dequeue = dwc2_hsotg_ep_dequeue,
4201 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004202 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004203};
4204
4205/**
John Youn9da51972017-01-17 20:30:27 -08004206 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004207 * @hsotg: The driver state
4208 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004209static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004210{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004211 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004212 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004213 /* unmask subset of endpoint interrupts */
4214
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004215 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4216 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4217 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004218
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004219 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4220 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4221 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004222
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004223 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004224
4225 /* Be in disconnected state until gadget is registered */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04004226 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004227
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004228 /* setup fifos */
4229
4230 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004231 dwc2_readl(hsotg->regs + GRXFSIZ),
4232 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004233
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004234 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004235
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004236 /* keep other bits untouched (so e.g. forced modes are not lost) */
4237 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4238 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01004239 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004240
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004241 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004242 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004243 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4244 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4245 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004246
Gregory Herrerof5090042015-01-09 13:38:47 +01004247 if (using_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04004248 dwc2_set_bit(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004249}
4250
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004251/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004252 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004253 * @gadget: The usb gadget state
4254 * @driver: The usb gadget driver
4255 *
4256 * Perform initialization to prepare udc device and driver
4257 * to work.
4258 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004259static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004260 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004261{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004262 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004263 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004264 int ret;
4265
4266 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004267 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004268 return -ENODEV;
4269 }
4270
4271 if (!driver) {
4272 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4273 return -EINVAL;
4274 }
4275
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004276 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004277 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004278
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004279 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004280 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4281 return -EINVAL;
4282 }
4283
4284 WARN_ON(hsotg->driver);
4285
4286 driver->driver.bus = NULL;
4287 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004288 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004289 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4290
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004291 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4292 ret = dwc2_lowlevel_hw_enable(hsotg);
4293 if (ret)
4294 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004295 }
4296
Gregory Herrerof6c01592015-01-09 13:38:41 +01004297 if (!IS_ERR_OR_NULL(hsotg->uphy))
4298 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004299
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004300 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004301 if (dwc2_hw_is_device(hsotg)) {
4302 dwc2_hsotg_init(hsotg);
4303 dwc2_hsotg_core_init_disconnected(hsotg, false);
4304 }
4305
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004306 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004307 spin_unlock_irqrestore(&hsotg->lock, flags);
4308
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004309 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004310
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004311 return 0;
4312
4313err:
4314 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004315 return ret;
4316}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004317
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004318/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004319 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004320 * @gadget: The usb gadget state
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004321 *
4322 * Stop udc hw block and stay tunned for future transmissions
4323 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004324static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004325{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004326 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004327 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004328 int ep;
4329
4330 if (!hsotg)
4331 return -ENODEV;
4332
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004333 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004334 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4335 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004336 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004337 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004338 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004339 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004340
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004341 spin_lock_irqsave(&hsotg->lock, flags);
4342
Marek Szyprowski32805c32014-10-20 12:45:33 +02004343 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004344 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004345 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004346
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004347 spin_unlock_irqrestore(&hsotg->lock, flags);
4348
Gregory Herrerof6c01592015-01-09 13:38:41 +01004349 if (!IS_ERR_OR_NULL(hsotg->uphy))
4350 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004351
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004352 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4353 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004354
4355 return 0;
4356}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004357
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004358/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004359 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004360 * @gadget: The usb gadget state
4361 *
4362 * Read the {micro} frame number
4363 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004364static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004365{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004366 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004367}
4368
Lukasz Majewskia188b682012-06-22 09:29:56 +02004369/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004370 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004371 * @gadget: The usb gadget state
4372 * @is_on: Current state of the USB PHY
4373 *
4374 * Connect/Disconnect the USB PHY pullup
4375 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004376static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004377{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004378 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004379 unsigned long flags = 0;
4380
Gregory Herrero77ba9112015-09-29 12:08:19 +02004381 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004382 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004383
4384 /* Don't modify pullup state while in host mode */
4385 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4386 hsotg->enabled = is_on;
4387 return 0;
4388 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004389
4390 spin_lock_irqsave(&hsotg->lock, flags);
4391 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004392 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004393 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004394 /* Enable ACG feature in device mode,if supported */
4395 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004396 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004397 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004398 dwc2_hsotg_core_disconnect(hsotg);
4399 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004400 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004401 }
4402
4403 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4404 spin_unlock_irqrestore(&hsotg->lock, flags);
4405
4406 return 0;
4407}
4408
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004409static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004410{
4411 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4412 unsigned long flags;
4413
4414 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4415 spin_lock_irqsave(&hsotg->lock, flags);
4416
Gregory Herrero61f72232015-09-29 12:08:28 +02004417 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004418 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004419 * before being initialized / de-initialized
4420 */
4421 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004422 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004423
Gregory Herrero83d98222015-01-09 13:39:02 +01004424 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004425 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004426
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004427 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004428 if (hsotg->enabled) {
4429 /* Enable ACG feature in device mode,if supported */
4430 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004431 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004432 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004433 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004434 dwc2_hsotg_core_disconnect(hsotg);
4435 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004436 }
4437
4438 spin_unlock_irqrestore(&hsotg->lock, flags);
4439 return 0;
4440}
4441
Gregory Herrero596d6962015-01-09 13:39:08 +01004442/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004443 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004444 * @gadget: The usb gadget state
4445 * @mA: Amount of current
4446 *
4447 * Report how much power the device may consume to the phy.
4448 */
John Youn9da51972017-01-17 20:30:27 -08004449static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004450{
4451 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4452
4453 if (IS_ERR_OR_NULL(hsotg->uphy))
4454 return -ENOTSUPP;
4455 return usb_phy_set_power(hsotg->uphy, mA);
4456}
4457
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004458static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4459 .get_frame = dwc2_hsotg_gadget_getframe,
4460 .udc_start = dwc2_hsotg_udc_start,
4461 .udc_stop = dwc2_hsotg_udc_stop,
4462 .pullup = dwc2_hsotg_pullup,
4463 .vbus_session = dwc2_hsotg_vbus_session,
4464 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004465};
4466
4467/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004468 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004469 * @hsotg: The device state.
4470 * @hs_ep: The endpoint to be initialised.
4471 * @epnum: The endpoint number
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004472 * @dir_in: True if direction is in.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004473 *
4474 * Initialise the given endpoint (as part of the probe and device state
4475 * creation) to give to the gadget driver. Setup the endpoint name, any
4476 * direction information and other state that may be required.
4477 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004478static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004479 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004480 int epnum,
4481 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004482{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004483 char *dir;
4484
4485 if (epnum == 0)
4486 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004487 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004488 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004489 else
4490 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004491
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004492 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004493 hs_ep->index = epnum;
4494
4495 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4496
4497 INIT_LIST_HEAD(&hs_ep->queue);
4498 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4499
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004500 /* add to the list of endpoints known by the gadget driver */
4501 if (epnum)
4502 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4503
4504 hs_ep->parent = hsotg;
4505 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004506
4507 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4508 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4509 else
4510 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4511 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004512 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004513
Robert Baldyga29545222015-07-31 16:00:18 +02004514 if (epnum == 0) {
4515 hs_ep->ep.caps.type_control = true;
4516 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004517 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4518 hs_ep->ep.caps.type_iso = true;
4519 hs_ep->ep.caps.type_bulk = true;
4520 }
Robert Baldyga29545222015-07-31 16:00:18 +02004521 hs_ep->ep.caps.type_int = true;
4522 }
4523
4524 if (dir_in)
4525 hs_ep->ep.caps.dir_in = true;
4526 else
4527 hs_ep->ep.caps.dir_out = true;
4528
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004529 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004530 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004531 * to be something valid.
4532 */
4533
4534 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004535 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004536
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004537 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004538 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004539 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004540 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004541 }
4542}
4543
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004544/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004545 * dwc2_hsotg_hw_cfg - read HW configuration registers
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004546 * @hsotg: Programming view of the DWC_otg controller
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004547 *
4548 * Read the USB core HW configuration registers
4549 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004550static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004551{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004552 u32 cfg;
4553 u32 ep_type;
4554 u32 i;
4555
Ben Dooks10aebc72010-07-19 09:40:44 +01004556 /* check hardware configuration */
4557
John Youn43e90342015-12-17 11:17:45 -08004558 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4559
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004560 /* Add ep0 */
4561 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004562
John Younb98866c2017-01-17 20:31:58 -08004563 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4564 sizeof(struct dwc2_hsotg_ep),
4565 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004566 if (!hsotg->eps_in[0])
4567 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004568 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004569 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004570
John Youn43e90342015-12-17 11:17:45 -08004571 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004572 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004573 ep_type = cfg & 3;
4574 /* Direction in or both */
4575 if (!(ep_type & 2)) {
4576 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004577 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004578 if (!hsotg->eps_in[i])
4579 return -ENOMEM;
4580 }
4581 /* Direction out or both */
4582 if (!(ep_type & 1)) {
4583 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004584 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004585 if (!hsotg->eps_out[i])
4586 return -ENOMEM;
4587 }
4588 }
4589
John Youn43e90342015-12-17 11:17:45 -08004590 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4591 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004592
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004593 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4594 hsotg->num_of_eps,
4595 hsotg->dedicated_fifos ? "dedicated" : "shared",
4596 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004597 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004598}
4599
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004600/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004601 * dwc2_hsotg_dump - dump state of the udc
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004602 * @hsotg: Programming view of the DWC_otg controller
4603 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004604 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004605static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004606{
Mark Brown83a01802011-06-01 17:16:15 +01004607#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004608 struct device *dev = hsotg->dev;
4609 void __iomem *regs = hsotg->regs;
4610 u32 val;
4611 int idx;
4612
4613 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004614 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4615 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004616
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004617 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004618 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004619
4620 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004621 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004622
4623 /* show periodic fifo settings */
4624
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004625 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004626 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004627 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004628 val >> FIFOSIZE_DEPTH_SHIFT,
4629 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004630 }
4631
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004632 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004633 dev_info(dev,
4634 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004635 dwc2_readl(regs + DIEPCTL(idx)),
4636 dwc2_readl(regs + DIEPTSIZ(idx)),
4637 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004638
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004639 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004640 dev_info(dev,
4641 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004642 idx, dwc2_readl(regs + DOEPCTL(idx)),
4643 dwc2_readl(regs + DOEPTSIZ(idx)),
4644 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004645 }
4646
4647 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004648 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004649#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004650}
4651
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004652/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004653 * dwc2_gadget_init - init function for gadget
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004654 * @hsotg: Programming view of the DWC_otg controller
4655 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004656 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004657int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004658{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004659 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004660 int epnum;
4661 int ret;
John Youn43e90342015-12-17 11:17:45 -08004662
Gregory Herrero0a176272015-01-09 13:38:52 +01004663 /* Dump fifo information */
4664 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004665 hsotg->params.g_np_tx_fifo_size);
4666 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004667
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004668 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004669 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004670 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004671 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004672
4673 if (hsotg->params.lpm)
4674 hsotg->gadget.lpm_capable = true;
4675
Gregory Herrero097ee662015-04-29 22:09:10 +02004676 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4677 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004678 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4679 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004680
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004681 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004682 if (ret) {
4683 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004684 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004685 }
4686
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004687 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4688 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004689 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004690 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004691
4692 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4693 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004694 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004695 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004696
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004697 if (using_desc_dma(hsotg)) {
4698 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4699 if (ret < 0)
4700 return ret;
4701 }
4702
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004703 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4704 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004705 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004706 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004707 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004708 }
4709
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004710 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4711
4712 if (hsotg->num_of_eps == 0) {
4713 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004714 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004715 }
4716
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004717 /* setup endpoint information */
4718
4719 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004720 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004721
4722 /* allocate EP0 request */
4723
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004724 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004725 GFP_KERNEL);
4726 if (!hsotg->ctrl_req) {
4727 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004728 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004729 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004730
4731 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004732 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4733 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004734 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004735 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004736 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004737 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004738 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004739 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004740
Dinh Nguyen117777b2014-11-11 11:13:34 -06004741 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004742 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004743 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004744
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004745 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004746
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004747 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004748}
4749
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004750/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004751 * dwc2_hsotg_remove - remove function for hsotg driver
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04004752 * @hsotg: Programming view of the DWC_otg controller
4753 *
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004754 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004755int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004756{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004757 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004758
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004759 return 0;
4760}
4761
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004762int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004763{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004764 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004765
Gregory Herrero9e779772015-04-29 22:09:07 +02004766 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004767 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004768
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004769 if (hsotg->driver) {
4770 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004771
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004772 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4773 hsotg->driver->driver.name);
4774
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004775 spin_lock_irqsave(&hsotg->lock, flags);
4776 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004777 dwc2_hsotg_core_disconnect(hsotg);
4778 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004779 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4780 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004781
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004782 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4783 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004784 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004785 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004786 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004787 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004788 }
4789
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004790 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004791}
4792
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004793int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004794{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004795 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004796
Gregory Herrero9e779772015-04-29 22:09:07 +02004797 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004798 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004799
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004800 if (hsotg->driver) {
4801 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4802 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004803
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004804 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004805 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004806 if (hsotg->enabled) {
4807 /* Enable ACG feature in device mode,if supported */
4808 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004809 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004810 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004811 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004812 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004813
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004814 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004815}
John Youn58e52ff6a2016-02-23 19:54:57 -08004816
4817/**
4818 * dwc2_backup_device_registers() - Backup controller device registers.
4819 * When suspending usb bus, registers needs to be backuped
4820 * if controller power is disabled once suspended.
4821 *
4822 * @hsotg: Programming view of the DWC_otg controller
4823 */
4824int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4825{
4826 struct dwc2_dregs_backup *dr;
4827 int i;
4828
4829 dev_dbg(hsotg->dev, "%s\n", __func__);
4830
4831 /* Backup dev regs */
4832 dr = &hsotg->dr_backup;
4833
4834 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4835 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4836 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4837 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4838 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4839
4840 for (i = 0; i < hsotg->num_of_eps; i++) {
4841 /* Backup IN EPs */
4842 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4843
4844 /* Ensure DATA PID is correctly configured */
4845 if (dr->diepctl[i] & DXEPCTL_DPID)
4846 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4847 else
4848 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4849
4850 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4851 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4852
4853 /* Backup OUT EPs */
4854 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4855
4856 /* Ensure DATA PID is correctly configured */
4857 if (dr->doepctl[i] & DXEPCTL_DPID)
4858 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4859 else
4860 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4861
4862 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4863 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
Vardan Mikayelyanaf7c2bd2018-02-16 14:07:33 +04004864 dr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004865 }
4866 dr->valid = true;
4867 return 0;
4868}
4869
4870/**
4871 * dwc2_restore_device_registers() - Restore controller device registers.
4872 * When resuming usb bus, device registers needs to be restored
4873 * if controller power were disabled.
4874 *
4875 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004876 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
4877 *
4878 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08004879 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004880int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08004881{
4882 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08004883 int i;
4884
4885 dev_dbg(hsotg->dev, "%s\n", __func__);
4886
4887 /* Restore dev regs */
4888 dr = &hsotg->dr_backup;
4889 if (!dr->valid) {
4890 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4891 __func__);
4892 return -EINVAL;
4893 }
4894 dr->valid = false;
4895
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004896 if (!remote_wakeup)
4897 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4898
John Youn58e52ff6a2016-02-23 19:54:57 -08004899 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4900 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4901 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4902
4903 for (i = 0; i < hsotg->num_of_eps; i++) {
4904 /* Restore IN EPs */
John Youn58e52ff6a2016-02-23 19:54:57 -08004905 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4906 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004907 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004908 /** WA for enabled EPx's IN in DDMA mode. On entering to
4909 * hibernation wrong value read and saved from DIEPDMAx,
4910 * as result BNA interrupt asserted on hibernation exit
4911 * by restoring from saved area.
4912 */
4913 if (hsotg->params.g_dma_desc &&
4914 (dr->diepctl[i] & DXEPCTL_EPENA))
4915 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
4916 dwc2_writel(dr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
4917 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4918 /* Restore OUT EPs */
4919 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4920 /* WA for enabled EPx's OUT in DDMA mode. On entering to
4921 * hibernation wrong value read and saved from DOEPDMAx,
4922 * as result BNA interrupt asserted on hibernation exit
4923 * by restoring from saved area.
4924 */
4925 if (hsotg->params.g_dma_desc &&
4926 (dr->doepctl[i] & DXEPCTL_EPENA))
4927 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
John Youn58e52ff6a2016-02-23 19:54:57 -08004928 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004929 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004930 }
4931
John Youn58e52ff6a2016-02-23 19:54:57 -08004932 return 0;
4933}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04004934
4935/**
4936 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
4937 *
4938 * @hsotg: Programming view of DWC_otg controller
4939 *
4940 */
4941void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
4942{
4943 u32 val;
4944
4945 if (!hsotg->params.lpm)
4946 return;
4947
4948 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
4949 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
4950 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
4951 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
4952 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
4953 dwc2_writel(val, hsotg->regs + GLPMCFG);
4954 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg->regs
4955 + GLPMCFG));
4956}
Vardan Mikayelyanc5c403dc2018-02-16 14:10:13 +04004957
4958/**
4959 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
4960 *
4961 * @hsotg: Programming view of the DWC_otg controller
4962 *
4963 * Return non-zero if failed to enter to hibernation.
4964 */
4965int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
4966{
4967 u32 gpwrdn;
4968 int ret = 0;
4969
4970 /* Change to L2(suspend) state */
4971 hsotg->lx_state = DWC2_L2;
4972 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
4973 ret = dwc2_backup_global_registers(hsotg);
4974 if (ret) {
4975 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
4976 __func__);
4977 return ret;
4978 }
4979 ret = dwc2_backup_device_registers(hsotg);
4980 if (ret) {
4981 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
4982 __func__);
4983 return ret;
4984 }
4985
4986 gpwrdn = GPWRDN_PWRDNRSTN;
4987 gpwrdn |= GPWRDN_PMUACTV;
4988 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4989 udelay(10);
4990
4991 /* Set flag to indicate that we are in hibernation */
4992 hsotg->hibernated = 1;
4993
4994 /* Enable interrupts from wake up logic */
4995 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4996 gpwrdn |= GPWRDN_PMUINTSEL;
4997 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4998 udelay(10);
4999
5000 /* Unmask device mode interrupts in GPWRDN */
5001 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5002 gpwrdn |= GPWRDN_RST_DET_MSK;
5003 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5004 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5005 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5006 udelay(10);
5007
5008 /* Enable Power Down Clamp */
5009 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5010 gpwrdn |= GPWRDN_PWRDNCLMP;
5011 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5012 udelay(10);
5013
5014 /* Switch off VDD */
5015 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5016 gpwrdn |= GPWRDN_PWRDNSWTCH;
5017 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5018 udelay(10);
5019
5020 /* Save gpwrdn register for further usage if stschng interrupt */
5021 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5022 dev_dbg(hsotg->dev, "Hibernation completed\n");
5023
5024 return ret;
5025}
5026
5027/**
5028 * dwc2_gadget_exit_hibernation()
5029 * This function is for exiting from Device mode hibernation by host initiated
5030 * resume/reset and device initiated remote-wakeup.
5031 *
5032 * @hsotg: Programming view of the DWC_otg controller
5033 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
Grigor Tovmasyan6fb914d2018-05-16 12:04:24 +04005034 * @reset: indicates whether resume is initiated by Reset.
Vardan Mikayelyanc5c403dc2018-02-16 14:10:13 +04005035 *
5036 * Return non-zero if failed to exit from hibernation.
5037 */
5038int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
5039 int rem_wakeup, int reset)
5040{
5041 u32 pcgcctl;
5042 u32 gpwrdn;
5043 u32 dctl;
5044 int ret = 0;
5045 struct dwc2_gregs_backup *gr;
5046 struct dwc2_dregs_backup *dr;
5047
5048 gr = &hsotg->gr_backup;
5049 dr = &hsotg->dr_backup;
5050
5051 if (!hsotg->hibernated) {
5052 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5053 return 1;
5054 }
5055 dev_dbg(hsotg->dev,
5056 "%s: called with rem_wakeup = %d reset = %d\n",
5057 __func__, rem_wakeup, reset);
5058
5059 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5060
5061 if (!reset) {
5062 /* Clear all pending interupts */
5063 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5064 }
5065
5066 /* De-assert Restore */
5067 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5068 gpwrdn &= ~GPWRDN_RESTORE;
5069 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5070 udelay(10);
5071
5072 if (!rem_wakeup) {
5073 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
5074 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
5075 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
5076 }
5077
5078 /* Restore GUSBCFG, DCFG and DCTL */
5079 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
5080 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
5081 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
5082
5083 /* De-assert Wakeup Logic */
5084 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5085 gpwrdn &= ~GPWRDN_PMUACTV;
5086 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5087
5088 if (rem_wakeup) {
5089 udelay(10);
5090 /* Start Remote Wakeup Signaling */
5091 dwc2_writel(dr->dctl | DCTL_RMTWKUPSIG, hsotg->regs + DCTL);
5092 } else {
5093 udelay(50);
5094 /* Set Device programming done bit */
5095 dctl = dwc2_readl(hsotg->regs + DCTL);
5096 dctl |= DCTL_PWRONPRGDONE;
5097 dwc2_writel(dctl, hsotg->regs + DCTL);
5098 }
5099 /* Wait for interrupts which must be cleared */
5100 mdelay(2);
5101 /* Clear all pending interupts */
5102 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5103
5104 /* Restore global registers */
5105 ret = dwc2_restore_global_registers(hsotg);
5106 if (ret) {
5107 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5108 __func__);
5109 return ret;
5110 }
5111
5112 /* Restore device registers */
5113 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5114 if (ret) {
5115 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5116 __func__);
5117 return ret;
5118 }
5119
5120 if (rem_wakeup) {
5121 mdelay(10);
5122 dctl = dwc2_readl(hsotg->regs + DCTL);
5123 dctl &= ~DCTL_RMTWKUPSIG;
5124 dwc2_writel(dctl, hsotg->regs + DCTL);
5125 }
5126
5127 hsotg->hibernated = 0;
5128 hsotg->lx_state = DWC2_L0;
5129 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5130
5131 return ret;
5132}