blob: 403e99026c5256e5d138a582d000d45c88eedc11 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002/**
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
110 * @increment: The value to increment by
111 *
112 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
113 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
114 */
115static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
116{
117 hs_ep->target_frame += hs_ep->interval;
118 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600119 hs_ep->frame_overrun = true;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700120 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
121 } else {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600122 hs_ep->frame_overrun = false;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700123 }
124}
125
126/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500127 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100128 * @hsotg: The device state
129 * @ints: A bitmask of the interrupts to enable
130 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500131static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100132{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300133 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100134 u32 new_gsintmsk;
135
136 new_gsintmsk = gsintmsk | ints;
137
138 if (new_gsintmsk != gsintmsk) {
139 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300140 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100141 }
142}
143
144/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500145 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100146 * @hsotg: The device state
147 * @ints: A bitmask of the interrupts to enable
148 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500149static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100150{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300151 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100152 u32 new_gsintmsk;
153
154 new_gsintmsk = gsintmsk & ~ints;
155
156 if (new_gsintmsk != gsintmsk)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300157 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100158}
159
160/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500161 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100162 * @hsotg: The device state
163 * @ep: The endpoint index
164 * @dir_in: True if direction is in.
165 * @en: The enable value, true to enable
166 *
167 * Set or clear the mask for an individual endpoint's interrupt
168 * request.
169 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500170static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800171 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100172 unsigned int en)
173{
174 unsigned long flags;
175 u32 bit = 1 << ep;
176 u32 daint;
177
178 if (!dir_in)
179 bit <<= 16;
180
181 local_irq_save(flags);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300182 daint = dwc2_readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100183 if (en)
184 daint |= bit;
185 else
186 daint &= ~bit;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300187 dwc2_writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100188 local_irq_restore(flags);
189}
190
191/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800192 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
193 */
194int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
195{
196 if (hsotg->hw_params.en_multiple_tx_fifo)
197 /* In dedicated FIFO mode we need count of IN EPs */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400198 return hsotg->hw_params.num_dev_in_eps;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800199 else
200 /* In shared FIFO mode we need count of Periodic IN EPs */
201 return hsotg->hw_params.num_dev_perio_in_ep;
202}
203
204/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800205 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
206 * device mode TX FIFOs
207 */
208int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
209{
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800210 int addr;
211 int tx_addr_max;
212 u32 np_tx_fifo_size;
213
214 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
215 hsotg->params.g_np_tx_fifo_size);
216
217 /* Get Endpoint Info Control block size in DWORDs. */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400218 tx_addr_max = hsotg->hw_params.total_fifo_size;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800219
220 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
221 if (tx_addr_max <= addr)
222 return 0;
223
224 return tx_addr_max - addr;
225}
226
227/**
228 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
229 * TX FIFOs
230 */
231int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
232{
233 int tx_fifo_count;
234 int tx_fifo_depth;
235
236 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
237
238 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
239
240 if (!tx_fifo_count)
241 return tx_fifo_depth;
242 else
243 return tx_fifo_depth / tx_fifo_count;
244}
245
246/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500247 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100248 * @hsotg: The device instance.
249 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500250static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100251{
John Youn2317eac2016-10-17 17:36:23 -0700252 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100253 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100254 int timeout;
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +0400255
Ben Dooks0f002d22010-05-25 05:36:50 +0100256 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700257 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100258
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100259 /* Reset fifo map if not correctly cleared during previous session */
260 WARN_ON(hsotg->fifo_map);
261 hsotg->fifo_map = 0;
262
Gregory Herrero0a176272015-01-09 13:38:52 +0100263 /* set RX/NPTX FIFO sizes */
John Youn05ee7992016-11-03 17:56:05 -0700264 dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
265 dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
266 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
267 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100268
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200269 /*
270 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100271 * block have overlapping default addresses. This also ensures
272 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200273 * known values.
274 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100275
276 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700277 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100278
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200279 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100280 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200281 * them to endpoints dynamically according to maxpacket size value of
282 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200283 */
John Youn2317eac2016-10-17 17:36:23 -0700284 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700285 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700286 continue;
287 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700288 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
289 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700290 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700291 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100292
John Youn2317eac2016-10-17 17:36:23 -0700293 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
John Youn05ee7992016-11-03 17:56:05 -0700294 val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100295 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100296
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800297 dwc2_writel(hsotg->hw_params.total_fifo_size |
298 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
299 hsotg->regs + GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200300 /*
301 * according to p428 of the design guide, we need to ensure that
302 * all fifos are flushed before continuing
303 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100304
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300305 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700306 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100307
308 /* wait until the fifos are both flushed */
309 timeout = 100;
310 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300311 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100312
Dinh Nguyen47a16852014-04-14 14:13:34 -0700313 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100314 break;
315
316 if (--timeout == 0) {
317 dev_err(hsotg->dev,
318 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
319 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100320 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100321 }
322
323 udelay(1);
324 }
325
326 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100327}
328
329/**
330 * @ep: USB endpoint to allocate request for.
331 * @flags: Allocation flags
332 *
333 * Allocate a new USB request structure appropriate for the specified endpoint
334 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500335static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800336 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100337{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500338 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100339
John Younec33efe2017-01-17 20:32:41 -0800340 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100341 if (!req)
342 return NULL;
343
344 INIT_LIST_HEAD(&req->queue);
345
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100346 return &req->req;
347}
348
349/**
350 * is_ep_periodic - return true if the endpoint is in periodic mode.
351 * @hs_ep: The endpoint to query.
352 *
353 * Returns true if the endpoint is in periodic mode, meaning it is being
354 * used for an Interrupt or ISO transfer.
355 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500356static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100357{
358 return hs_ep->periodic;
359}
360
361/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500362 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100363 * @hsotg: The device state.
364 * @hs_ep: The endpoint for the request
365 * @hs_req: The request being processed.
366 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500367 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100368 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200369 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500370static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800371 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500372 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100373{
374 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800375
Jingoo Han17d966a2013-05-11 21:14:00 +0900376 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100377}
378
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800379/*
380 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
381 * for Control endpoint
382 * @hsotg: The device state.
383 *
384 * This function will allocate 4 descriptor chains for EP 0: 2 for
385 * Setup stage, per one for IN and OUT data/status transactions.
386 */
387static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
388{
389 hsotg->setup_desc[0] =
390 dmam_alloc_coherent(hsotg->dev,
391 sizeof(struct dwc2_dma_desc),
392 &hsotg->setup_desc_dma[0],
393 GFP_KERNEL);
394 if (!hsotg->setup_desc[0])
395 goto fail;
396
397 hsotg->setup_desc[1] =
398 dmam_alloc_coherent(hsotg->dev,
399 sizeof(struct dwc2_dma_desc),
400 &hsotg->setup_desc_dma[1],
401 GFP_KERNEL);
402 if (!hsotg->setup_desc[1])
403 goto fail;
404
405 hsotg->ctrl_in_desc =
406 dmam_alloc_coherent(hsotg->dev,
407 sizeof(struct dwc2_dma_desc),
408 &hsotg->ctrl_in_desc_dma,
409 GFP_KERNEL);
410 if (!hsotg->ctrl_in_desc)
411 goto fail;
412
413 hsotg->ctrl_out_desc =
414 dmam_alloc_coherent(hsotg->dev,
415 sizeof(struct dwc2_dma_desc),
416 &hsotg->ctrl_out_desc_dma,
417 GFP_KERNEL);
418 if (!hsotg->ctrl_out_desc)
419 goto fail;
420
421 return 0;
422
423fail:
424 return -ENOMEM;
425}
426
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100427/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500428 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100429 * @hsotg: The controller state.
430 * @hs_ep: The endpoint we're going to write for.
431 * @hs_req: The request to write data for.
432 *
433 * This is called when the TxFIFO has some space in it to hold a new
434 * transmission and we have something to give it. The actual setup of
435 * the data size is done elsewhere, so all we have to do is to actually
436 * write the data.
437 *
438 * The return value is zero if there is more space (or nothing was done)
439 * otherwise -ENOSPC is returned if the FIFO space was used up.
440 *
441 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200442 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500443static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800444 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500445 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100446{
447 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300448 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100449 int buf_pos = hs_req->req.actual;
450 int to_write = hs_ep->size_loaded;
451 void *data;
452 int can_write;
453 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200454 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100455
456 to_write -= (buf_pos - hs_ep->last_load);
457
458 /* if there's nothing to write, get out early */
459 if (to_write == 0)
460 return 0;
461
Ben Dooks10aebc72010-07-19 09:40:44 +0100462 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300463 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100464 int size_left;
465 int size_done;
466
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200467 /*
468 * work out how much data was loaded so we can calculate
469 * how much data is left in the fifo.
470 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100471
Dinh Nguyen47a16852014-04-14 14:13:34 -0700472 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100473
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200474 /*
475 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100476 * previous data has been completely sent.
477 */
478 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500479 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100480 return -ENOSPC;
481 }
482
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100483 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
484 __func__, size_left,
485 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
486
487 /* how much of the data has moved */
488 size_done = hs_ep->size_loaded - size_left;
489
490 /* how much data is left in the fifo */
491 can_write = hs_ep->fifo_load - size_done;
492 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
493 __func__, can_write);
494
495 can_write = hs_ep->fifo_size - can_write;
496 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
497 __func__, can_write);
498
499 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500500 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100501 return -ENOSPC;
502 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100503 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Robert Baldygaad674a12016-08-29 13:38:50 -0700504 can_write = dwc2_readl(hsotg->regs +
505 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100506
507 can_write &= 0xffff;
508 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100509 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700510 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100511 dev_dbg(hsotg->dev,
512 "%s: no queue slots available (0x%08x)\n",
513 __func__, gnptxsts);
514
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500515 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100516 return -ENOSPC;
517 }
518
Dinh Nguyen47a16852014-04-14 14:13:34 -0700519 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100520 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100521 }
522
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200523 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
524
525 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800526 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100527
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200528 /*
529 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100530 * FIFO, requests of >512 cause the endpoint to get stuck with a
531 * fragment of the end of the transfer in it.
532 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200533 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100534 can_write = 512;
535
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200536 /*
537 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100538 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200539 * doing it.
540 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200541 if (to_write > max_transfer) {
542 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100543
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200544 /* it's needed only when we do not use dedicated fifos */
545 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500546 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800547 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700548 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100549 }
550
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100551 /* see if we can write data */
552
553 if (to_write > can_write) {
554 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200555 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100556
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200557 /*
558 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100559 * exact number of packets.
560 *
561 * Note, we do not currently check to see if we can ever
562 * write a full packet or not to the FIFO.
563 */
564
565 if (pkt_round)
566 to_write -= pkt_round;
567
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200568 /*
569 * enable correct FIFO interrupt to alert us when there
570 * is more room left.
571 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100572
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200573 /* it's needed only when we do not use dedicated fifos */
574 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500575 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800576 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700577 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100578 }
579
580 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800581 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100582
583 if (to_write <= 0)
584 return -ENOSPC;
585
586 hs_req->req.actual = buf_pos + to_write;
587 hs_ep->total_data += to_write;
588
589 if (periodic)
590 hs_ep->fifo_load += to_write;
591
592 to_write = DIV_ROUND_UP(to_write, 4);
593 data = hs_req->req.buf + buf_pos;
594
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500595 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100596
597 return (to_write >= can_write) ? -ENOSPC : 0;
598}
599
600/**
601 * get_ep_limit - get the maximum data legnth for this endpoint
602 * @hs_ep: The endpoint
603 *
604 * Return the maximum data that can be queued in one go on a given endpoint
605 * so that transfers that are too long can be split.
606 */
John Youn9da51972017-01-17 20:30:27 -0800607static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100608{
609 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800610 unsigned int maxsize;
611 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100612
613 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700614 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
615 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100616 } else {
John Youn9da51972017-01-17 20:30:27 -0800617 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900618 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700619 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900620 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100621 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100622 }
623
624 /* we made the constant loading easier above by using +1 */
625 maxpkt--;
626 maxsize--;
627
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200628 /*
629 * constrain by packet count if maxpkts*pktsize is greater
630 * than the length register size.
631 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100632
633 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
634 maxsize = maxpkt * hs_ep->ep.maxpacket;
635
636 return maxsize;
637}
638
639/**
John Youn38beaec2017-01-17 20:31:13 -0800640 * dwc2_hsotg_read_frameno - read current frame number
641 * @hsotg: The device instance
642 *
643 * Return the current frame number
644 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700645static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
646{
647 u32 dsts;
648
649 dsts = dwc2_readl(hsotg->regs + DSTS);
650 dsts &= DSTS_SOFFN_MASK;
651 dsts >>= DSTS_SOFFN_SHIFT;
652
653 return dsts;
654}
655
656/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800657 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
658 * DMA descriptor chain prepared for specific endpoint
659 * @hs_ep: The endpoint
660 *
661 * Return the maximum data that can be queued in one go on a given endpoint
662 * depending on its descriptor chain capacity so that transfers that
663 * are too long can be split.
664 */
665static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
666{
667 int is_isoc = hs_ep->isochronous;
668 unsigned int maxsize;
669
670 if (is_isoc)
671 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
672 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
673 else
674 maxsize = DEV_DMA_NBYTES_LIMIT;
675
676 /* Above size of one descriptor was chosen, multiple it */
677 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
678
679 return maxsize;
680}
681
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800682/*
683 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
684 * @hs_ep: The endpoint
685 * @mask: RX/TX bytes mask to be defined
686 *
687 * Returns maximum data payload for one descriptor after analyzing endpoint
688 * characteristics.
689 * DMA descriptor transfer bytes limit depends on EP type:
690 * Control out - MPS,
691 * Isochronous - descriptor rx/tx bytes bitfield limit,
692 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
693 * have concatenations from various descriptors within one packet.
694 *
695 * Selects corresponding mask for RX/TX bytes as well.
696 */
697static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
698{
699 u32 mps = hs_ep->ep.maxpacket;
700 int dir_in = hs_ep->dir_in;
701 u32 desc_size = 0;
702
703 if (!hs_ep->index && !dir_in) {
704 desc_size = mps;
705 *mask = DEV_DMA_NBYTES_MASK;
706 } else if (hs_ep->isochronous) {
707 if (dir_in) {
708 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
709 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
710 } else {
711 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
712 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
713 }
714 } else {
715 desc_size = DEV_DMA_NBYTES_LIMIT;
716 *mask = DEV_DMA_NBYTES_MASK;
717
718 /* Round down desc_size to be mps multiple */
719 desc_size -= desc_size % mps;
720 }
721
722 return desc_size;
723}
724
725/*
726 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
727 * @hs_ep: The endpoint
728 * @dma_buff: DMA address to use
729 * @len: Length of the transfer
730 *
731 * This function will iterate over descriptor chain and fill its entries
732 * with corresponding information based on transfer data.
733 */
734static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
735 dma_addr_t dma_buff,
736 unsigned int len)
737{
738 struct dwc2_hsotg *hsotg = hs_ep->parent;
739 int dir_in = hs_ep->dir_in;
740 struct dwc2_dma_desc *desc = hs_ep->desc_list;
741 u32 mps = hs_ep->ep.maxpacket;
742 u32 maxsize = 0;
743 u32 offset = 0;
744 u32 mask = 0;
745 int i;
746
747 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
748
749 hs_ep->desc_count = (len / maxsize) +
750 ((len % maxsize) ? 1 : 0);
751 if (len == 0)
752 hs_ep->desc_count = 1;
753
754 for (i = 0; i < hs_ep->desc_count; ++i) {
755 desc->status = 0;
756 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
757 << DEV_DMA_BUFF_STS_SHIFT);
758
759 if (len > maxsize) {
760 if (!hs_ep->index && !dir_in)
761 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
762
763 desc->status |= (maxsize <<
764 DEV_DMA_NBYTES_SHIFT & mask);
765 desc->buf = dma_buff + offset;
766
767 len -= maxsize;
768 offset += maxsize;
769 } else {
770 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
771
772 if (dir_in)
773 desc->status |= (len % mps) ? DEV_DMA_SHORT :
774 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
775 if (len > maxsize)
776 dev_err(hsotg->dev, "wrong len %d\n", len);
777
778 desc->status |=
779 len << DEV_DMA_NBYTES_SHIFT & mask;
780 desc->buf = dma_buff + offset;
781 }
782
783 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
784 desc->status |= (DEV_DMA_BUFF_STS_HREADY
785 << DEV_DMA_BUFF_STS_SHIFT);
786 desc++;
787 }
788}
789
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800790/*
791 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
792 * @hs_ep: The isochronous endpoint.
793 * @dma_buff: usb requests dma buffer.
794 * @len: usb request transfer length.
795 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400796 * Fills next free descriptor with the data of the arrived usb request,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800797 * frame info, sets Last and IOC bits increments next_desc. If filled
798 * descriptor is not the first one, removes L bit from the previous descriptor
799 * status.
800 */
801static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
802 dma_addr_t dma_buff, unsigned int len)
803{
804 struct dwc2_dma_desc *desc;
805 struct dwc2_hsotg *hsotg = hs_ep->parent;
806 u32 index;
807 u32 maxsize = 0;
808 u32 mask = 0;
809
810 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800811
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400812 index = hs_ep->next_desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800813 desc = &hs_ep->desc_list[index];
814
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400815 /* Check if descriptor chain full */
816 if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
817 DEV_DMA_BUFF_STS_HREADY) {
818 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
819 return 1;
820 }
821
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800822 /* Clear L bit of previous desc if more than one entries in the chain */
823 if (hs_ep->next_desc)
824 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
825
826 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
827 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
828
829 desc->status = 0;
830 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
831
832 desc->buf = dma_buff;
833 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
834 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
835
836 if (hs_ep->dir_in) {
837 desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) &
838 DEV_DMA_ISOC_PID_MASK) |
839 ((len % hs_ep->ep.maxpacket) ?
840 DEV_DMA_SHORT : 0) |
841 ((hs_ep->target_frame <<
842 DEV_DMA_ISOC_FRNUM_SHIFT) &
843 DEV_DMA_ISOC_FRNUM_MASK);
844 }
845
846 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
847 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
848
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400849 /* Increment frame number by interval for IN */
850 if (hs_ep->dir_in)
851 dwc2_gadget_incr_frame_num(hs_ep);
852
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800853 /* Update index of last configured entry in the chain */
854 hs_ep->next_desc++;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400855 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_GENERIC)
856 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800857
858 return 0;
859}
860
861/*
862 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
863 * @hs_ep: The isochronous endpoint.
864 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400865 * Prepare descriptor chain for isochronous endpoints. Afterwards
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800866 * write DMA address to HW and enable the endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800867 */
868static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
869{
870 struct dwc2_hsotg *hsotg = hs_ep->parent;
871 struct dwc2_hsotg_req *hs_req, *treq;
872 int index = hs_ep->index;
873 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400874 int i;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800875 u32 dma_reg;
876 u32 depctl;
877 u32 ctrl;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400878 struct dwc2_dma_desc *desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800879
880 if (list_empty(&hs_ep->queue)) {
881 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
882 return;
883 }
884
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400885 /* Initialize descriptor chain by Host Busy status */
886 for (i = 0; i < MAX_DMA_DESC_NUM_GENERIC; i++) {
887 desc = &hs_ep->desc_list[i];
888 desc->status = 0;
889 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
890 << DEV_DMA_BUFF_STS_SHIFT);
891 }
892
893 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800894 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
895 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
896 hs_req->req.length);
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400897 if (ret)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800898 break;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800899 }
900
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400901 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800902 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
903 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
904
905 /* write descriptor chain address to control register */
906 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
907
908 ctrl = dwc2_readl(hsotg->regs + depctl);
909 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
910 dwc2_writel(ctrl, hsotg->regs + depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800911}
912
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800913/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500914 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100915 * @hsotg: The controller state.
916 * @hs_ep: The endpoint to process a request for
917 * @hs_req: The request to start.
918 * @continuing: True if we are doing more for the current request.
919 *
920 * Start the given request running by setting the endpoint registers
921 * appropriately, and writing any data to the FIFOs.
922 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500923static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800924 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500925 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100926 bool continuing)
927{
928 struct usb_request *ureq = &hs_req->req;
929 int index = hs_ep->index;
930 int dir_in = hs_ep->dir_in;
931 u32 epctrl_reg;
932 u32 epsize_reg;
933 u32 epsize;
934 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -0800935 unsigned int length;
936 unsigned int packets;
937 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800938 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100939
940 if (index != 0) {
941 if (hs_ep->req && !continuing) {
942 dev_err(hsotg->dev, "%s: active request\n", __func__);
943 WARN_ON(1);
944 return;
945 } else if (hs_ep->req != hs_req && continuing) {
946 dev_err(hsotg->dev,
947 "%s: continue different req\n", __func__);
948 WARN_ON(1);
949 return;
950 }
951 }
952
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800953 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200954 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
955 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100956
957 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300958 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100959 hs_ep->dir_in ? "in" : "out");
960
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900961 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300962 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900963
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200964 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900965 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
966 return;
967 }
968
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100969 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200970 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
971 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100972
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800973 if (!using_desc_dma(hsotg))
974 maxreq = get_ep_limit(hs_ep);
975 else
976 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
977
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100978 if (length > maxreq) {
979 int round = maxreq % hs_ep->ep.maxpacket;
980
981 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
982 __func__, length, maxreq, round);
983
984 /* round down to multiple of packets */
985 if (round)
986 maxreq -= round;
987
988 length = maxreq;
989 }
990
991 if (length)
992 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
993 else
994 packets = 1; /* send one packet if length is zero. */
995
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200996 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
997 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
998 return;
999 }
1000
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001001 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001002 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001003 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001004 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001005 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001006 else
1007 epsize = 0;
1008
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001009 /*
1010 * zero length packet should be programmed on its own and should not
1011 * be counted in DIEPTSIZ.PktCnt with other packets.
1012 */
1013 if (dir_in && ureq->zero && !continuing) {
1014 /* Test if zlp is actually required. */
1015 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001016 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001017 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001018 }
1019
Dinh Nguyen47a16852014-04-14 14:13:34 -07001020 epsize |= DXEPTSIZ_PKTCNT(packets);
1021 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001022
1023 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1024 __func__, packets, length, ureq->length, epsize, epsize_reg);
1025
1026 /* store the request as the current one we're doing */
1027 hs_ep->req = hs_req;
1028
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001029 if (using_desc_dma(hsotg)) {
1030 u32 offset = 0;
1031 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001032
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001033 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1034 if (!dir_in) {
1035 if (!index)
1036 length = mps;
1037 else if (length % mps)
1038 length += (mps - (length % mps));
1039 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001040
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001041 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001042 * If more data to send, adjust DMA for EP0 out data stage.
1043 * ureq->dma stays unchanged, hence increment it by already
1044 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001045 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001046 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1047 continuing)
1048 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001049
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001050 /* Fill DDMA chain entries */
1051 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1052 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001053
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001054 /* write descriptor chain address to control register */
1055 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
1056
1057 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1058 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1059 } else {
1060 /* write size / packets */
1061 dwc2_writel(epsize, hsotg->regs + epsize_reg);
1062
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001063 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001064 /*
1065 * write DMA address to control register, buffer
1066 * already synced by dwc2_hsotg_ep_queue().
1067 */
1068
1069 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
1070
1071 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1072 __func__, &ureq->dma, dma_reg);
1073 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001074 }
1075
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001076 if (hs_ep->isochronous && hs_ep->interval == 1) {
1077 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1078 dwc2_gadget_incr_frame_num(hs_ep);
1079
1080 if (hs_ep->target_frame & 0x1)
1081 ctrl |= DXEPCTL_SETODDFR;
1082 else
1083 ctrl |= DXEPCTL_SETEVENFR;
1084 }
1085
Dinh Nguyen47a16852014-04-14 14:13:34 -07001086 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001087
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001088 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001089
1090 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001091 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001092 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001093
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001094 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001095 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001096
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001097 /*
1098 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001099 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001100 * this information.
1101 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001102 hs_ep->size_loaded = length;
1103 hs_ep->last_load = ureq->actual;
1104
1105 if (dir_in && !using_dma(hsotg)) {
1106 /* set these anyway, we may need them for non-periodic in */
1107 hs_ep->fifo_load = 0;
1108
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001109 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001110 }
1111
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001112 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001113 * Note, trying to clear the NAK here causes problems with transmit
1114 * on the S3C6400 ending up with the TXFIFO becoming full.
1115 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001116
1117 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001118 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001119 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001120 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001121 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122
Dinh Nguyen47a16852014-04-14 14:13:34 -07001123 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001124 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001125
1126 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001127 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001128}
1129
1130/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001131 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001132 * @hsotg: The device state.
1133 * @hs_ep: The endpoint the request is on.
1134 * @req: The request being processed.
1135 *
1136 * We've been asked to queue a request, so ensure that the memory buffer
1137 * is correctly setup for DMA. If we've been passed an extant DMA address
1138 * then ensure the buffer has been synced to memory. If our buffer has no
1139 * DMA memory, then we map the memory and mark our request to allow us to
1140 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001141 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001142static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001143 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001144 struct usb_request *req)
1145{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001146 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001147
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001148 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1149 if (ret)
1150 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001151
1152 return 0;
1153
1154dma_error:
1155 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1156 __func__, req->buf, req->length);
1157
1158 return -EIO;
1159}
1160
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001161static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001162 struct dwc2_hsotg_ep *hs_ep,
1163 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001164{
1165 void *req_buf = hs_req->req.buf;
1166
1167 /* If dma is not being used or buffer is aligned */
1168 if (!using_dma(hsotg) || !((long)req_buf & 3))
1169 return 0;
1170
1171 WARN_ON(hs_req->saved_req_buf);
1172
1173 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001174 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001175
1176 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1177 if (!hs_req->req.buf) {
1178 hs_req->req.buf = req_buf;
1179 dev_err(hsotg->dev,
1180 "%s: unable to allocate memory for bounce buffer\n",
1181 __func__);
1182 return -ENOMEM;
1183 }
1184
1185 /* Save actual buffer */
1186 hs_req->saved_req_buf = req_buf;
1187
1188 if (hs_ep->dir_in)
1189 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1190 return 0;
1191}
1192
John Younb98866c2017-01-17 20:31:58 -08001193static void
1194dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1195 struct dwc2_hsotg_ep *hs_ep,
1196 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001197{
1198 /* If dma is not being used or buffer was aligned */
1199 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1200 return;
1201
1202 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1203 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1204
1205 /* Copy data from bounce buffer on successful out transfer */
1206 if (!hs_ep->dir_in && !hs_req->req.status)
1207 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001208 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001209
1210 /* Free bounce buffer */
1211 kfree(hs_req->req.buf);
1212
1213 hs_req->req.buf = hs_req->saved_req_buf;
1214 hs_req->saved_req_buf = NULL;
1215}
1216
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001217/**
1218 * dwc2_gadget_target_frame_elapsed - Checks target frame
1219 * @hs_ep: The driver endpoint to check
1220 *
1221 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1222 * corresponding transfer.
1223 */
1224static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1225{
1226 struct dwc2_hsotg *hsotg = hs_ep->parent;
1227 u32 target_frame = hs_ep->target_frame;
1228 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
1229 bool frame_overrun = hs_ep->frame_overrun;
1230
1231 if (!frame_overrun && current_frame >= target_frame)
1232 return true;
1233
1234 if (frame_overrun && current_frame >= target_frame &&
1235 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1236 return true;
1237
1238 return false;
1239}
1240
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001241/*
1242 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1243 * @hsotg: The driver state
1244 * @hs_ep: the ep descriptor chain is for
1245 *
1246 * Called to update EP0 structure's pointers depend on stage of
1247 * control transfer.
1248 */
1249static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1250 struct dwc2_hsotg_ep *hs_ep)
1251{
1252 switch (hsotg->ep0_state) {
1253 case DWC2_EP0_SETUP:
1254 case DWC2_EP0_STATUS_OUT:
1255 hs_ep->desc_list = hsotg->setup_desc[0];
1256 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1257 break;
1258 case DWC2_EP0_DATA_IN:
1259 case DWC2_EP0_STATUS_IN:
1260 hs_ep->desc_list = hsotg->ctrl_in_desc;
1261 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1262 break;
1263 case DWC2_EP0_DATA_OUT:
1264 hs_ep->desc_list = hsotg->ctrl_out_desc;
1265 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1266 break;
1267 default:
1268 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1269 hsotg->ep0_state);
1270 return -EINVAL;
1271 }
1272
1273 return 0;
1274}
1275
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001276static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001277 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001278{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001279 struct dwc2_hsotg_req *hs_req = our_req(req);
1280 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001281 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001282 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001283 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001284 u32 maxsize = 0;
1285 u32 mask = 0;
1286
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001287
1288 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1289 ep->name, req, req->length, req->buf, req->no_interrupt,
1290 req->zero, req->short_not_ok);
1291
Gregory Herrero7ababa92015-04-29 22:09:08 +02001292 /* Prevent new request submission when controller is suspended */
Grigor Tovmasyan88b02f22018-01-24 17:44:25 +04001293 if (hs->lx_state != DWC2_L0) {
1294 dev_dbg(hs->dev, "%s: submit request only in active state\n",
John Youn9da51972017-01-17 20:30:27 -08001295 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001296 return -EAGAIN;
1297 }
1298
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001299 /* initialise status of the request */
1300 INIT_LIST_HEAD(&hs_req->queue);
1301 req->actual = 0;
1302 req->status = -EINPROGRESS;
1303
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001304 /* In DDMA mode for ISOC's don't queue request if length greater
1305 * than descriptor limits.
1306 */
1307 if (using_desc_dma(hs) && hs_ep->isochronous) {
1308 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1309 if (hs_ep->dir_in && req->length > maxsize) {
1310 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1311 req->length, maxsize);
1312 return -EINVAL;
1313 }
1314
1315 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1316 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1317 req->length, hs_ep->ep.maxpacket);
1318 return -EINVAL;
1319 }
1320 }
1321
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001322 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001323 if (ret)
1324 return ret;
1325
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001326 /* if we're using DMA, sync the buffers as necessary */
1327 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001328 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001329 if (ret)
1330 return ret;
1331 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001332 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1333 if (using_desc_dma(hs) && !hs_ep->index) {
1334 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1335 if (ret)
1336 return ret;
1337 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001338
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001339 first = list_empty(&hs_ep->queue);
1340 list_add_tail(&hs_req->queue, &hs_ep->queue);
1341
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001342 /*
1343 * Handle DDMA isochronous transfers separately - just add new entry
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001344 * to the descriptor chain.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001345 * Transfer will be started once SW gets either one of NAK or
1346 * OutTknEpDis interrupts.
1347 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001348 if (using_desc_dma(hs) && hs_ep->isochronous) {
1349 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1350 dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1351 hs_req->req.length);
1352 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001353 return 0;
1354 }
1355
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001356 if (first) {
1357 if (!hs_ep->isochronous) {
1358 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1359 return 0;
1360 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001361
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001362 while (dwc2_gadget_target_frame_elapsed(hs_ep))
1363 dwc2_gadget_incr_frame_num(hs_ep);
1364
1365 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1366 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1367 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001368 return 0;
1369}
1370
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001371static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001372 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001373{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001374 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001375 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001376 unsigned long flags = 0;
1377 int ret = 0;
1378
1379 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001380 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001381 spin_unlock_irqrestore(&hs->lock, flags);
1382
1383 return ret;
1384}
1385
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001386static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001387 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001388{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001389 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001390
1391 kfree(hs_req);
1392}
1393
1394/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001395 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001396 * @ep: The endpoint the request was on.
1397 * @req: The request completed.
1398 *
1399 * Called on completion of any requests the driver itself
1400 * submitted that need cleaning up.
1401 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001402static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001403 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001404{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001405 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001406 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001407
1408 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1409
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001410 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001411}
1412
1413/**
1414 * ep_from_windex - convert control wIndex value to endpoint
1415 * @hsotg: The driver state.
1416 * @windex: The control request wIndex field (in host order).
1417 *
1418 * Convert the given wIndex into a pointer to an driver endpoint
1419 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001420 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001421static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001422 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001423{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001424 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001425 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1426 int idx = windex & 0x7F;
1427
1428 if (windex >= 0x100)
1429 return NULL;
1430
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001431 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001432 return NULL;
1433
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001434 ep = index_to_ep(hsotg, idx, dir);
1435
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001436 if (idx && ep->dir_in != dir)
1437 return NULL;
1438
1439 return ep;
1440}
1441
1442/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001443 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001444 * @hsotg: The driver state.
1445 * @testmode: requested usb test mode
1446 * Enable usb Test Mode requested by the Host.
1447 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001448int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001449{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001450 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001451
1452 dctl &= ~DCTL_TSTCTL_MASK;
1453 switch (testmode) {
1454 case TEST_J:
1455 case TEST_K:
1456 case TEST_SE0_NAK:
1457 case TEST_PACKET:
1458 case TEST_FORCE_EN:
1459 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1460 break;
1461 default:
1462 return -EINVAL;
1463 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001464 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001465 return 0;
1466}
1467
1468/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001469 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001470 * @hsotg: The device state
1471 * @ep: Endpoint 0
1472 * @buff: Buffer for request
1473 * @length: Length of reply.
1474 *
1475 * Create a request and queue it on the given endpoint. This is useful as
1476 * an internal method of sending replies to certain control requests, etc.
1477 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001478static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001479 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001480 void *buff,
1481 int length)
1482{
1483 struct usb_request *req;
1484 int ret;
1485
1486 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1487
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001488 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001489 hsotg->ep0_reply = req;
1490 if (!req) {
1491 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1492 return -ENOMEM;
1493 }
1494
1495 req->buf = hsotg->ep0_buff;
1496 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001497 /*
1498 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1499 * STATUS stage.
1500 */
1501 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001502 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001503
1504 if (length)
1505 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001506
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001507 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001508 if (ret) {
1509 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1510 return ret;
1511 }
1512
1513 return 0;
1514}
1515
1516/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001517 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001518 * @hsotg: The device state
1519 * @ctrl: USB control request
1520 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001521static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001522 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001523{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001524 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1525 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001526 __le16 reply;
1527 int ret;
1528
1529 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1530
1531 if (!ep0->dir_in) {
1532 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1533 return -EINVAL;
1534 }
1535
1536 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1537 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001538 /*
1539 * bit 0 => self powered
1540 * bit 1 => remote wakeup
1541 */
1542 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001543 break;
1544
1545 case USB_RECIP_INTERFACE:
1546 /* currently, the data result should be zero */
1547 reply = cpu_to_le16(0);
1548 break;
1549
1550 case USB_RECIP_ENDPOINT:
1551 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1552 if (!ep)
1553 return -ENOENT;
1554
1555 reply = cpu_to_le16(ep->halted ? 1 : 0);
1556 break;
1557
1558 default:
1559 return 0;
1560 }
1561
1562 if (le16_to_cpu(ctrl->wLength) != 2)
1563 return -EINVAL;
1564
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001565 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001566 if (ret) {
1567 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1568 return ret;
1569 }
1570
1571 return 1;
1572}
1573
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001574static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001575
1576/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001577 * get_ep_head - return the first request on the endpoint
1578 * @hs_ep: The controller endpoint to get
1579 *
1580 * Get the first request on the endpoint.
1581 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001582static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001583{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001584 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1585 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001586}
1587
1588/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001589 * dwc2_gadget_start_next_request - Starts next request from ep queue
1590 * @hs_ep: Endpoint structure
1591 *
1592 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1593 * in its handler. Hence we need to unmask it here to be able to do
1594 * resynchronization.
1595 */
1596static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1597{
1598 u32 mask;
1599 struct dwc2_hsotg *hsotg = hs_ep->parent;
1600 int dir_in = hs_ep->dir_in;
1601 struct dwc2_hsotg_req *hs_req;
1602 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1603
1604 if (!list_empty(&hs_ep->queue)) {
1605 hs_req = get_ep_head(hs_ep);
1606 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1607 return;
1608 }
1609 if (!hs_ep->isochronous)
1610 return;
1611
1612 if (dir_in) {
1613 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1614 __func__);
1615 } else {
1616 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1617 __func__);
1618 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1619 mask |= DOEPMSK_OUTTKNEPDISMSK;
1620 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1621 }
1622}
1623
1624/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001625 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001626 * @hsotg: The device state
1627 * @ctrl: USB control request
1628 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001629static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001630 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001631{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001632 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1633 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001634 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001635 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001636 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001637 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001638 u32 recip;
1639 u32 wValue;
1640 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001641
1642 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1643 __func__, set ? "SET" : "CLEAR");
1644
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001645 wValue = le16_to_cpu(ctrl->wValue);
1646 wIndex = le16_to_cpu(ctrl->wIndex);
1647 recip = ctrl->bRequestType & USB_RECIP_MASK;
1648
1649 switch (recip) {
1650 case USB_RECIP_DEVICE:
1651 switch (wValue) {
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001652 case USB_DEVICE_REMOTE_WAKEUP:
1653 hsotg->remote_wakeup_allowed = 1;
1654 break;
1655
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001656 case USB_DEVICE_TEST_MODE:
1657 if ((wIndex & 0xff) != 0)
1658 return -EINVAL;
1659 if (!set)
1660 return -EINVAL;
1661
1662 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001663 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001664 if (ret) {
1665 dev_err(hsotg->dev,
1666 "%s: failed to send reply\n", __func__);
1667 return ret;
1668 }
1669 break;
1670 default:
1671 return -ENOENT;
1672 }
1673 break;
1674
1675 case USB_RECIP_ENDPOINT:
1676 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001677 if (!ep) {
1678 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001679 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001680 return -ENOENT;
1681 }
1682
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001683 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001684 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001685 halted = ep->halted;
1686
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001687 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001688
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001689 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001690 if (ret) {
1691 dev_err(hsotg->dev,
1692 "%s: failed to send reply\n", __func__);
1693 return ret;
1694 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001695
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001696 /*
1697 * we have to complete all requests for ep if it was
1698 * halted, and the halt was cleared by CLEAR_FEATURE
1699 */
1700
1701 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001702 /*
1703 * If we have request in progress,
1704 * then complete it
1705 */
1706 if (ep->req) {
1707 hs_req = ep->req;
1708 ep->req = NULL;
1709 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001710 if (hs_req->req.complete) {
1711 spin_unlock(&hsotg->lock);
1712 usb_gadget_giveback_request(
1713 &ep->ep, &hs_req->req);
1714 spin_lock(&hsotg->lock);
1715 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001716 }
1717
1718 /* If we have pending request, then start it */
John Youn34c0887f2017-01-17 20:31:43 -08001719 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001720 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001721 }
1722
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001723 break;
1724
1725 default:
1726 return -ENOENT;
1727 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001728 break;
1729 default:
1730 return -ENOENT;
1731 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001732 return 1;
1733}
1734
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001735static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001736
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001737/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001738 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001739 * @hsotg: The device state
1740 *
1741 * Set stall for ep0 as response for setup request.
1742 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001743static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001744{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001745 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001746 u32 reg;
1747 u32 ctrl;
1748
1749 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1750 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1751
1752 /*
1753 * DxEPCTL_Stall will be cleared by EP once it has
1754 * taken effect, so no need to clear later.
1755 */
1756
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001757 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001758 ctrl |= DXEPCTL_STALL;
1759 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001760 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001761
1762 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001763 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001764 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001765
1766 /*
1767 * complete won't be called, so we enqueue
1768 * setup request here
1769 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001770 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001771}
1772
1773/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001774 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001775 * @hsotg: The device state
1776 * @ctrl: The control request received
1777 *
1778 * The controller has received the SETUP phase of a control request, and
1779 * needs to work out what to do next (and whether to pass it on to the
1780 * gadget driver).
1781 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001782static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001783 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001784{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001785 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001786 int ret = 0;
1787 u32 dcfg;
1788
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001789 dev_dbg(hsotg->dev,
1790 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1791 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1792 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001793
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001794 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001795 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001796 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1797 } else if (ctrl->bRequestType & USB_DIR_IN) {
1798 ep0->dir_in = 1;
1799 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1800 } else {
1801 ep0->dir_in = 0;
1802 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1803 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001804
1805 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1806 switch (ctrl->bRequest) {
1807 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001808 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001809 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001810 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001811 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1812 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001813 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001814
1815 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1816
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001817 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001818 return;
1819
1820 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001821 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001822 break;
1823
1824 case USB_REQ_CLEAR_FEATURE:
1825 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001826 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001827 break;
1828 }
1829 }
1830
1831 /* as a fallback, try delivering it to the driver to deal with */
1832
1833 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001834 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001835 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001836 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001837 if (ret < 0)
1838 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1839 }
1840
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001841 /*
1842 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001843 * so respond with a STALL for the status stage to indicate failure.
1844 */
1845
Robert Baldygac9f721b2014-01-14 08:36:00 +01001846 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001847 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001848}
1849
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001850/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001851 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001852 * @ep: The endpoint the request was on.
1853 * @req: The request completed.
1854 *
1855 * Called on completion of any requests the driver itself submitted for
1856 * EP0 setup packets
1857 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001858static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001859 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001860{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001861 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001862 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001863
1864 if (req->status < 0) {
1865 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1866 return;
1867 }
1868
Robert Baldyga93f599f2013-11-21 13:49:17 +01001869 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001870 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001871 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001872 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001873 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001874 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001875}
1876
1877/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001878 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001879 * @hsotg: The device state.
1880 *
1881 * Enqueue a request on EP0 if necessary to received any SETUP packets
1882 * received from the host.
1883 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001884static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001885{
1886 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001887 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001888 int ret;
1889
1890 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1891
1892 req->zero = 0;
1893 req->length = 8;
1894 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001895 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001896
1897 if (!list_empty(&hs_req->queue)) {
1898 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1899 return;
1900 }
1901
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001902 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001903 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001904 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001905
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001906 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001907 if (ret < 0) {
1908 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001909 /*
1910 * Don't think there's much we can do other than watch the
1911 * driver fail.
1912 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001913 }
1914}
1915
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001916static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001917 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001918{
1919 u32 ctrl;
1920 u8 index = hs_ep->index;
1921 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1922 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1923
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001924 if (hs_ep->dir_in)
1925 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001926 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001927 else
1928 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001929 index);
1930 if (using_desc_dma(hsotg)) {
1931 /* Not specific buffer needed for ep0 ZLP */
1932 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001933
Minas Harutyunyan201ec562018-01-16 16:03:32 +04001934 if (!index)
1935 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1936
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001937 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1938 } else {
1939 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1940 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1941 epsiz_reg);
1942 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001943
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001944 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001945 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1946 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1947 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001948 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001949}
1950
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001951/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001952 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001953 * @hsotg: The device state.
1954 * @hs_ep: The endpoint the request was on.
1955 * @hs_req: The request to complete.
1956 * @result: The result code (0 => Ok, otherwise errno)
1957 *
1958 * The given request has finished, so call the necessary completion
1959 * if it has one and then look to see if we can start a new request
1960 * on the endpoint.
1961 *
1962 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001963 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001964static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001965 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001966 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001967 int result)
1968{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001969 if (!hs_req) {
1970 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1971 return;
1972 }
1973
1974 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1975 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1976
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001977 /*
1978 * only replace the status if we've not already set an error
1979 * from a previous transaction
1980 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001981
1982 if (hs_req->req.status == -EINPROGRESS)
1983 hs_req->req.status = result;
1984
Yunzhi Li44583fe2015-09-29 12:25:01 +02001985 if (using_dma(hsotg))
1986 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1987
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001988 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001989
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001990 hs_ep->req = NULL;
1991 list_del_init(&hs_req->queue);
1992
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001993 /*
1994 * call the complete request with the locks off, just in case the
1995 * request tries to queue more work for this endpoint.
1996 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001997
1998 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001999 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002000 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002001 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002002 }
2003
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002004 /* In DDMA don't need to proceed to starting of next ISOC request */
2005 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2006 return;
2007
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002008 /*
2009 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002010 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002011 * so be careful when doing this.
2012 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002013
John Youn34c0887f2017-01-17 20:31:43 -08002014 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002015 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002016}
2017
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002018/*
2019 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2020 * @hs_ep: The endpoint the request was on.
2021 *
2022 * Get first request from the ep queue, determine descriptor on which complete
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002023 * happened. SW discovers which descriptor currently in use by HW, adjusts
2024 * dma_address and calculates index of completed descriptor based on the value
2025 * of DEPDMA register. Update actual length of request, giveback to gadget.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002026 */
2027static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2028{
2029 struct dwc2_hsotg *hsotg = hs_ep->parent;
2030 struct dwc2_hsotg_req *hs_req;
2031 struct usb_request *ureq;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002032 u32 desc_sts;
2033 u32 mask;
2034
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002035 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2036
2037 /* Process only descriptors with buffer status set to DMA done */
2038 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2039 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2040
2041 hs_req = get_ep_head(hs_ep);
2042 if (!hs_req) {
2043 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2044 return;
2045 }
2046 ureq = &hs_req->req;
2047
2048 /* Check completion status */
2049 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2050 DEV_DMA_STS_SUCC) {
2051 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2052 DEV_DMA_ISOC_RX_NBYTES_MASK;
2053 ureq->actual = ureq->length - ((desc_sts & mask) >>
2054 DEV_DMA_ISOC_NBYTES_SHIFT);
2055
2056 /* Adjust actual len for ISOC Out if len is
2057 * not align of 4
2058 */
2059 if (!hs_ep->dir_in && ureq->length & 0x3)
2060 ureq->actual += 4 - (ureq->length & 0x3);
2061 }
2062
2063 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2064
2065 hs_ep->compl_desc++;
2066 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_GENERIC - 1))
2067 hs_ep->compl_desc = 0;
2068 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002069 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002070}
2071
2072/*
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002073 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2074 * @hs_ep: The isochronous endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002075 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002076 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2077 * interrupt. Reset target frame and next_desc to allow to start
2078 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2079 * interrupt for OUT direction.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002080 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002081static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002082{
2083 struct dwc2_hsotg *hsotg = hs_ep->parent;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002084
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002085 if (!hs_ep->dir_in)
2086 dwc2_flush_rx_fifo(hsotg);
2087 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002088
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002089 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2090 hs_ep->next_desc = 0;
2091 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002092}
2093
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002094/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002095 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002096 * @hsotg: The device state.
2097 * @ep_idx: The endpoint index for the data
2098 * @size: The size of data in the fifo, in bytes
2099 *
2100 * The FIFO status shows there is data to read from the FIFO for a given
2101 * endpoint, so sort out whether we need to read the data into a request
2102 * that has been made for that endpoint.
2103 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002104static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002105{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002106 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2107 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002108 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002109 int to_read;
2110 int max_req;
2111 int read_ptr;
2112
2113 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002114 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002115 int ptr;
2116
Robert Baldyga6b448af2014-12-16 11:51:44 +01002117 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002118 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002119 __func__, size, ep_idx, epctl);
2120
2121 /* dump the data from the FIFO, we've nothing we can do */
2122 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002123 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002124
2125 return;
2126 }
2127
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002128 to_read = size;
2129 read_ptr = hs_req->req.actual;
2130 max_req = hs_req->req.length - read_ptr;
2131
Ben Dooksa33e7132010-07-19 09:40:49 +01002132 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2133 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2134
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002135 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002136 /*
2137 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002138 * to deal with in this request.
2139 */
2140
2141 /* currently we don't deal this */
2142 WARN_ON_ONCE(1);
2143 }
2144
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002145 hs_ep->total_data += to_read;
2146 hs_req->req.actual += to_read;
2147 to_read = DIV_ROUND_UP(to_read, 4);
2148
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002149 /*
2150 * note, we might over-write the buffer end by 3 bytes depending on
2151 * alignment of the data.
2152 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05002153 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002154}
2155
2156/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002157 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002158 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002159 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002160 *
2161 * Generate a zero-length IN packet request for terminating a SETUP
2162 * transaction.
2163 *
2164 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002165 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002166 * the TxFIFO.
2167 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002168static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002169{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002170 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002171 hsotg->eps_out[0]->dir_in = dir_in;
2172 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002173
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002174 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002175}
2176
Roman Bacikec1f9d92015-09-10 18:13:43 -07002177static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002178 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002179{
2180 u32 ctrl;
2181
2182 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
2183 if (ctrl & DXEPCTL_EOFRNUM)
2184 ctrl |= DXEPCTL_SETEVENFR;
2185 else
2186 ctrl |= DXEPCTL_SETODDFR;
2187 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
2188}
2189
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002190/*
2191 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2192 * @hs_ep - The endpoint on which transfer went
2193 *
2194 * Iterate over endpoints descriptor chain and get info on bytes remained
2195 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2196 */
2197static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2198{
2199 struct dwc2_hsotg *hsotg = hs_ep->parent;
2200 unsigned int bytes_rem = 0;
2201 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2202 int i;
2203 u32 status;
2204
2205 if (!desc)
2206 return -EINVAL;
2207
2208 for (i = 0; i < hs_ep->desc_count; ++i) {
2209 status = desc->status;
2210 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2211
2212 if (status & DEV_DMA_STS_MASK)
2213 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2214 i, status & DEV_DMA_STS_MASK);
2215 }
2216
2217 return bytes_rem;
2218}
2219
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002220/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002221 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002222 * @hsotg: The device instance
2223 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002224 *
2225 * The RXFIFO has delivered an OutDone event, which means that the data
2226 * transfer for an OUT endpoint has been completed, either by a short
2227 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002228 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002229static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002230{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002231 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002232 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2233 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002234 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002235 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002236 int result = 0;
2237
2238 if (!hs_req) {
2239 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2240 return;
2241 }
2242
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002243 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2244 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002245 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2246 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002247 return;
2248 }
2249
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002250 if (using_desc_dma(hsotg))
2251 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2252
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002253 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002254 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002255
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002256 /*
2257 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002258 * is left in the endpoint size register and then working it
2259 * out from the amount we loaded for the transfer.
2260 *
2261 * We need to do this as DMA pointers are always 32bit aligned
2262 * so may overshoot/undershoot the transfer.
2263 */
2264
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002265 size_done = hs_ep->size_loaded - size_left;
2266 size_done += hs_ep->last_load;
2267
2268 req->actual = size_done;
2269 }
2270
Ben Dooksa33e7132010-07-19 09:40:49 +01002271 /* if there is more request to do, schedule new transfer */
2272 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002273 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002274 return;
2275 }
2276
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002277 if (req->actual < req->length && req->short_not_ok) {
2278 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2279 __func__, req->actual, req->length);
2280
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002281 /*
2282 * todo - what should we return here? there's no one else
2283 * even bothering to check the status.
2284 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002285 }
2286
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002287 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2288 if (!using_desc_dma(hsotg) && epnum == 0 &&
2289 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002290 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002291 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002292 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002293 }
2294
Roman Bacikec1f9d92015-09-10 18:13:43 -07002295 /*
2296 * Slave mode OUT transfers do not go through XferComplete so
2297 * adjust the ISOC parity here.
2298 */
2299 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002300 if (hs_ep->isochronous && hs_ep->interval == 1)
2301 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002302 else if (hs_ep->isochronous && hs_ep->interval > 1)
2303 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002304 }
2305
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002306 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002307}
2308
2309/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002310 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002311 * @hsotg: The device instance
2312 *
2313 * The IRQ handler has detected that the RX FIFO has some data in it
2314 * that requires processing, so find out what is in there and do the
2315 * appropriate read.
2316 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002317 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002318 * chunks, so if you have x packets received on an endpoint you'll get x
2319 * FIFO events delivered, each with a packet's worth of data in it.
2320 *
2321 * When using DMA, we should not be processing events from the RXFIFO
2322 * as the actual data should be sent to the memory directly and we turn
2323 * on the completion interrupts to get notifications of transfer completion.
2324 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002325static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002326{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002327 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002328 u32 epnum, status, size;
2329
2330 WARN_ON(using_dma(hsotg));
2331
Dinh Nguyen47a16852014-04-14 14:13:34 -07002332 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2333 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002334
Dinh Nguyen47a16852014-04-14 14:13:34 -07002335 size = grxstsr & GRXSTS_BYTECNT_MASK;
2336 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002337
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002338 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002339 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002340
Dinh Nguyen47a16852014-04-14 14:13:34 -07002341 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2342 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2343 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002344 break;
2345
Dinh Nguyen47a16852014-04-14 14:13:34 -07002346 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002347 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002348 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002349
2350 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002351 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002352 break;
2353
Dinh Nguyen47a16852014-04-14 14:13:34 -07002354 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002355 dev_dbg(hsotg->dev,
2356 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002357 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002358 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002359 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002360 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002361 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2362 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2363 */
2364 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002365 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366 break;
2367
Dinh Nguyen47a16852014-04-14 14:13:34 -07002368 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002369 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002370 break;
2371
Dinh Nguyen47a16852014-04-14 14:13:34 -07002372 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002373 dev_dbg(hsotg->dev,
2374 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002375 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002376 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002377
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002378 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2379
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002380 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002381 break;
2382
2383 default:
2384 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2385 __func__, grxstsr);
2386
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002387 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002388 break;
2389 }
2390}
2391
2392/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002393 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002394 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002395 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002396static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002397{
2398 switch (mps) {
2399 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002400 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002401 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002402 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002403 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002404 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002405 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002406 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002407 }
2408
2409 /* bad max packet size, warn and return invalid result */
2410 WARN_ON(1);
2411 return (u32)-1;
2412}
2413
2414/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002415 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002416 * @hsotg: The driver state.
2417 * @ep: The index number of the endpoint
2418 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002419 * @mc: The multicount value
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002420 *
2421 * Configure the maximum packet size for the given endpoint, updating
2422 * the hardware control registers to reflect this.
2423 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002424static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002425 unsigned int ep, unsigned int mps,
2426 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002427{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002428 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002429 void __iomem *regs = hsotg->regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002430 u32 reg;
2431
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002432 hs_ep = index_to_ep(hsotg, ep, dir_in);
2433 if (!hs_ep)
2434 return;
2435
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002436 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002437 u32 mps_bytes = mps;
2438
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002439 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002440 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2441 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002442 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002443 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002444 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002445 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002446 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002447 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002448 hs_ep->mc = mc;
2449 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002450 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002451 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002452 }
2453
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002454 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002455 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002456 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002457 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002458 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002459 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002460 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002461 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002462 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002463 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002464 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002465
2466 return;
2467
2468bad_mps:
2469 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2470}
2471
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002472/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002473 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002474 * @hsotg: The driver state
2475 * @idx: The index for the endpoint (0..15)
2476 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002477static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002478{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002479 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2480 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002481
2482 /* wait until the fifo is flushed */
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002483 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2484 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2485 __func__);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002486}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487
2488/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002489 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002490 * @hsotg: The driver state
2491 * @hs_ep: The driver endpoint to check.
2492 *
2493 * Check to see if there is a request that has data to send, and if so
2494 * make an attempt to write data into the FIFO.
2495 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002496static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002497 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002499 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002500
Robert Baldygaafcf4162013-09-19 11:50:19 +02002501 if (!hs_ep->dir_in || !hs_req) {
2502 /**
2503 * if request is not enqueued, we disable interrupts
2504 * for endpoints, excepting ep0
2505 */
2506 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002507 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002508 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002509 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002510 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002511
2512 if (hs_req->req.actual < hs_req->req.length) {
2513 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2514 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002515 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 }
2517
2518 return 0;
2519}
2520
2521/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002522 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002523 * @hsotg: The device state.
2524 * @hs_ep: The endpoint that has just completed.
2525 *
2526 * An IN transfer has been completed, update the transfer's state and then
2527 * call the relevant completion routines.
2528 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002529static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002530 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002531{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002532 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002533 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002534 int size_left, size_done;
2535
2536 if (!hs_req) {
2537 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2538 return;
2539 }
2540
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002541 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002542 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2543 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002544
2545 /*
2546 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2547 * changed to IN. Change back to complete OUT transfer request
2548 */
2549 hs_ep->dir_in = 0;
2550
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002551 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002552 if (hsotg->test_mode) {
2553 int ret;
2554
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002555 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002556 if (ret < 0) {
2557 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002558 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002559 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002560 return;
2561 }
2562 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002563 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002564 return;
2565 }
2566
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002567 /*
2568 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002569 * in the endpoint size register and then working it out from
2570 * the amount we loaded for the transfer.
2571 *
2572 * We do this even for DMA, as the transfer may have incremented
2573 * past the end of the buffer (DMA transfers are always 32bit
2574 * aligned).
2575 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002576 if (using_desc_dma(hsotg)) {
2577 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2578 if (size_left < 0)
2579 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2580 size_left);
2581 } else {
2582 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2583 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002584
2585 size_done = hs_ep->size_loaded - size_left;
2586 size_done += hs_ep->last_load;
2587
2588 if (hs_req->req.actual != size_done)
2589 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2590 __func__, hs_req->req.actual, size_done);
2591
2592 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002593 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2594 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002595
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002596 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2597 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002598 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002599 return;
2600 }
2601
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002602 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002603 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002604 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002605 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002606 /* transfer will be completed on next complete interrupt */
2607 return;
2608 }
2609
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002610 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2611 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002612 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002613 return;
2614 }
2615
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002616 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002617}
2618
2619/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002620 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2621 * @hsotg: The device state.
2622 * @idx: Index of ep.
2623 * @dir_in: Endpoint direction 1-in 0-out.
2624 *
2625 * Reads for endpoint with given index and direction, by masking
2626 * epint_reg with coresponding mask.
2627 */
2628static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2629 unsigned int idx, int dir_in)
2630{
2631 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2632 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2633 u32 ints;
2634 u32 mask;
2635 u32 diepempmsk;
2636
2637 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2638 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2639 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2640 mask |= DXEPINT_SETUP_RCVD;
2641
2642 ints = dwc2_readl(hsotg->regs + epint_reg);
2643 ints &= mask;
2644 return ints;
2645}
2646
2647/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002648 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2649 * @hs_ep: The endpoint on which interrupt is asserted.
2650 *
2651 * This interrupt indicates that the endpoint has been disabled per the
2652 * application's request.
2653 *
2654 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2655 * in case of ISOC completes current request.
2656 *
2657 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2658 * request starts it.
2659 */
2660static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2661{
2662 struct dwc2_hsotg *hsotg = hs_ep->parent;
2663 struct dwc2_hsotg_req *hs_req;
2664 unsigned char idx = hs_ep->index;
2665 int dir_in = hs_ep->dir_in;
2666 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2667 int dctl = dwc2_readl(hsotg->regs + DCTL);
2668
2669 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2670
2671 if (dir_in) {
2672 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2673
2674 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2675
2676 if (hs_ep->isochronous) {
2677 dwc2_hsotg_complete_in(hsotg, hs_ep);
2678 return;
2679 }
2680
2681 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2682 int dctl = dwc2_readl(hsotg->regs + DCTL);
2683
2684 dctl |= DCTL_CGNPINNAK;
2685 dwc2_writel(dctl, hsotg->regs + DCTL);
2686 }
2687 return;
2688 }
2689
2690 if (dctl & DCTL_GOUTNAKSTS) {
2691 dctl |= DCTL_CGOUTNAK;
2692 dwc2_writel(dctl, hsotg->regs + DCTL);
2693 }
2694
2695 if (!hs_ep->isochronous)
2696 return;
2697
2698 if (list_empty(&hs_ep->queue)) {
2699 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2700 __func__, hs_ep);
2701 return;
2702 }
2703
2704 do {
2705 hs_req = get_ep_head(hs_ep);
2706 if (hs_req)
2707 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2708 -ENODATA);
2709 dwc2_gadget_incr_frame_num(hs_ep);
2710 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2711
2712 dwc2_gadget_start_next_request(hs_ep);
2713}
2714
2715/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002716 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2717 * @hs_ep: The endpoint on which interrupt is asserted.
2718 *
2719 * This is starting point for ISOC-OUT transfer, synchronization done with
2720 * first out token received from host while corresponding EP is disabled.
2721 *
2722 * Device does not know initial frame in which out token will come. For this
2723 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2724 * getting this interrupt SW starts calculation for next transfer frame.
2725 */
2726static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2727{
2728 struct dwc2_hsotg *hsotg = ep->parent;
2729 int dir_in = ep->dir_in;
2730 u32 doepmsk;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002731 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002732
2733 if (dir_in || !ep->isochronous)
2734 return;
2735
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002736 /*
2737 * Store frame in which irq was asserted here, as
2738 * it can change while completing request below.
2739 */
2740 tmp = dwc2_hsotg_read_frameno(hsotg);
2741
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002742 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), 0);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002743
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002744 if (using_desc_dma(hsotg)) {
2745 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2746 /* Start first ISO Out */
2747 ep->target_frame = tmp;
2748 dwc2_gadget_start_isoc_ddma(ep);
2749 }
2750 return;
2751 }
2752
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002753 if (ep->interval > 1 &&
2754 ep->target_frame == TARGET_FRAME_INITIAL) {
2755 u32 dsts;
2756 u32 ctrl;
2757
2758 dsts = dwc2_readl(hsotg->regs + DSTS);
2759 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2760 dwc2_gadget_incr_frame_num(ep);
2761
2762 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2763 if (ep->target_frame & 0x1)
2764 ctrl |= DXEPCTL_SETODDFR;
2765 else
2766 ctrl |= DXEPCTL_SETEVENFR;
2767
2768 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2769 }
2770
2771 dwc2_gadget_start_next_request(ep);
2772 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2773 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2774 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2775}
2776
2777/**
John Youn38beaec2017-01-17 20:31:13 -08002778 * dwc2_gadget_handle_nak - handle NAK interrupt
2779 * @hs_ep: The endpoint on which interrupt is asserted.
2780 *
2781 * This is starting point for ISOC-IN transfer, synchronization done with
2782 * first IN token received from host while corresponding EP is disabled.
2783 *
2784 * Device does not know when first one token will arrive from host. On first
2785 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2786 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2787 * sent in response to that as there was no data in FIFO. SW is basing on this
2788 * interrupt to obtain frame in which token has come and then based on the
2789 * interval calculates next frame for transfer.
2790 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002791static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2792{
2793 struct dwc2_hsotg *hsotg = hs_ep->parent;
2794 int dir_in = hs_ep->dir_in;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002795 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002796
2797 if (!dir_in || !hs_ep->isochronous)
2798 return;
2799
2800 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002801
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002802 tmp = dwc2_hsotg_read_frameno(hsotg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002803 if (using_desc_dma(hsotg)) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002804 dwc2_hsotg_complete_request(hsotg, hs_ep,
2805 get_ep_head(hs_ep), 0);
2806
2807 hs_ep->target_frame = tmp;
2808 dwc2_gadget_incr_frame_num(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002809 dwc2_gadget_start_isoc_ddma(hs_ep);
2810 return;
2811 }
2812
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002813 hs_ep->target_frame = tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002814 if (hs_ep->interval > 1) {
2815 u32 ctrl = dwc2_readl(hsotg->regs +
2816 DIEPCTL(hs_ep->index));
2817 if (hs_ep->target_frame & 0x1)
2818 ctrl |= DXEPCTL_SETODDFR;
2819 else
2820 ctrl |= DXEPCTL_SETEVENFR;
2821
2822 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2823 }
2824
2825 dwc2_hsotg_complete_request(hsotg, hs_ep,
2826 get_ep_head(hs_ep), 0);
2827 }
2828
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002829 if (!using_desc_dma(hsotg))
2830 dwc2_gadget_incr_frame_num(hs_ep);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002831}
2832
2833/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002834 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002835 * @hsotg: The driver state
2836 * @idx: The index for the endpoint (0..15)
2837 * @dir_in: Set if this is an IN endpoint
2838 *
2839 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002840 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002841static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002842 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002843{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002844 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002845 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2846 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2847 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002848 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002849 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002850
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002851 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002852 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002853
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002854 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002855 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002856
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002857 if (!hs_ep) {
2858 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002859 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002860 return;
2861 }
2862
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002863 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2864 __func__, idx, dir_in ? "in" : "out", ints);
2865
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002866 /* Don't process XferCompl interrupt if it is a setup packet */
2867 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2868 ints &= ~DXEPINT_XFERCOMPL;
2869
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002870 /*
2871 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2872 * stage and xfercomplete was generated without SETUP phase done
2873 * interrupt. SW should parse received setup packet only after host's
2874 * exit from setup phase of control transfer.
2875 */
2876 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2877 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2878 ints &= ~DXEPINT_XFERCOMPL;
2879
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002880 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002881 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002882 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002883 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2884 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002885
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002886 /* In DDMA handle isochronous requests separately */
2887 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002888 /* XferCompl set along with BNA */
2889 if (!(ints & DXEPINT_BNAINTR))
2890 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002891 } else if (dir_in) {
2892 /*
2893 * We get OutDone from the FIFO, so we only
2894 * need to look at completing IN requests here
2895 * if operating slave mode
2896 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002897 if (hs_ep->isochronous && hs_ep->interval > 1)
2898 dwc2_gadget_incr_frame_num(hs_ep);
2899
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002900 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002901 if (ints & DXEPINT_NAKINTRPT)
2902 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002903
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002904 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002905 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002906 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002907 /*
2908 * We're using DMA, we need to fire an OutDone here
2909 * as we ignore the RXFIFO.
2910 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002911 if (hs_ep->isochronous && hs_ep->interval > 1)
2912 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002913
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002914 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002915 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002916 }
2917
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002918 if (ints & DXEPINT_EPDISBLD)
2919 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002920
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002921 if (ints & DXEPINT_OUTTKNEPDIS)
2922 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2923
2924 if (ints & DXEPINT_NAKINTRPT)
2925 dwc2_gadget_handle_nak(hs_ep);
2926
Dinh Nguyen47a16852014-04-14 14:13:34 -07002927 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002928 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002929
Dinh Nguyen47a16852014-04-14 14:13:34 -07002930 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002931 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2932
2933 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002934 /*
2935 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002936 * setup packet. In non-DMA mode we'd get this
2937 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002938 * the setup here.
2939 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002940
2941 if (dir_in)
2942 WARN_ON_ONCE(1);
2943 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002944 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002945 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002946 }
2947
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002948 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08002949 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
2950
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04002951 /* Safety check EP0 state when STSPHSERCVD asserted */
2952 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
2953 /* Move to STATUS IN for DDMA */
2954 if (using_desc_dma(hsotg))
2955 dwc2_hsotg_ep0_zlp(hsotg, true);
2956 }
2957
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002958 }
2959
Dinh Nguyen47a16852014-04-14 14:13:34 -07002960 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002961 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002962
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002963 if (ints & DXEPINT_BNAINTR) {
2964 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002965 if (hs_ep->isochronous)
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002966 dwc2_gadget_handle_isoc_bna(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002967 }
2968
Robert Baldyga1479e842013-10-09 08:41:57 +02002969 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002970 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002971 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002972 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2973 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002974 }
2975
2976 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002977 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002978 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2979 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002980 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002981
2982 /* FIFO has space or is empty (see GAHBCFG) */
2983 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002984 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002985 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2986 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002987 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002988 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002989 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002990 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002991}
2992
2993/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002994 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002995 * @hsotg: The device state.
2996 *
2997 * Handle updating the device settings after the enumeration phase has
2998 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002999 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003000static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003001{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003002 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003003 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003004
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003005 /*
3006 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003007 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003008 * we connected at.
3009 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003010
3011 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3012
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003013 /*
3014 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003015 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003016 * not advertise a 64byte MPS on EP0.
3017 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003018
3019 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003020 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003021 case DSTS_ENUMSPD_FS:
3022 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003023 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003024 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003025 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026 break;
3027
Dinh Nguyen47a16852014-04-14 14:13:34 -07003028 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003029 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003030 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003031 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003032 break;
3033
Dinh Nguyen47a16852014-04-14 14:13:34 -07003034 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003035 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003036 ep0_mps = 8;
3037 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003038 /*
3039 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003040 * moment, and the documentation seems to imply that it isn't
3041 * supported by the PHYs on some of the devices.
3042 */
3043 break;
3044 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003045 dev_info(hsotg->dev, "new device is %s\n",
3046 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003047
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003048 /*
3049 * we should now know the maximum packet size for an
3050 * endpoint, so set the endpoints to a default value.
3051 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003052
3053 if (ep0_mps) {
3054 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003055 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003056 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3057 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003058 for (i = 1; i < hsotg->num_of_eps; i++) {
3059 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003060 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3061 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003062 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003063 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3064 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003065 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003066 }
3067
3068 /* ensure after enumeration our EP0 is active */
3069
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003070 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003071
3072 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003073 dwc2_readl(hsotg->regs + DIEPCTL0),
3074 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003075}
3076
3077/**
3078 * kill_all_requests - remove all requests from the endpoint's queue
3079 * @hsotg: The device state.
3080 * @ep: The endpoint the requests may be on.
3081 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082 *
3083 * Go through the requests on the given endpoint and mark them
3084 * completed with the given result code.
3085 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003086static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003087 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01003088 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003089{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003090 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003091 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003092
Robert Baldyga6b448af2014-12-16 11:51:44 +01003093 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003094
Robert Baldyga6b448af2014-12-16 11:51:44 +01003095 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003096 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003097 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01003098
Robert Baldygab203d0a2014-09-09 10:44:56 +02003099 if (!hsotg->dedicated_fifos)
3100 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07003101 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003102 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003103 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003104}
3105
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003106/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003107 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003108 * @hsotg: The device state.
3109 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003110 * The device has been disconnected. Remove all current
3111 * transactions and signal the gadget driver that this
3112 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003113 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003114void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115{
John Youn9da51972017-01-17 20:30:27 -08003116 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003117
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003118 if (!hsotg->connected)
3119 return;
3120
3121 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003122 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003123
3124 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3125 if (hsotg->eps_in[ep])
3126 kill_all_requests(hsotg, hsotg->eps_in[ep],
John Youn9da51972017-01-17 20:30:27 -08003127 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003128 if (hsotg->eps_out[ep])
3129 kill_all_requests(hsotg, hsotg->eps_out[ep],
John Youn9da51972017-01-17 20:30:27 -08003130 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003131 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003132
3133 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003134 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003135
3136 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003137}
3138
3139/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003140 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003141 * @hsotg: The device state:
3142 * @periodic: True if this is a periodic FIFO interrupt
3143 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003144static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003145{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003146 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003147 int epno, ret;
3148
3149 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003150 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003151 ep = index_to_ep(hsotg, epno, 1);
3152
3153 if (!ep)
3154 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003155
3156 if (!ep->dir_in)
3157 continue;
3158
3159 if ((periodic && !ep->periodic) ||
3160 (!periodic && ep->periodic))
3161 continue;
3162
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003163 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003164 if (ret < 0)
3165 break;
3166 }
3167}
3168
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003169/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003170#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3171 GINTSTS_PTXFEMP | \
3172 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003173
3174/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003175 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003176 * @hsotg: The device state
3177 *
3178 * Issue a soft reset to the core, and await the core finishing it.
3179 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003180void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003181 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003182{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003183 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003184 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003185 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003186 u32 dcfg = 0;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003187
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003188 /* Kill any ep0 requests as controller will be reinitialized */
3189 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3190
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003191 if (!is_usb_reset)
John Stultz6e6360b2017-01-23 14:59:14 -08003192 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003193 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02003194
3195 /*
3196 * we must now enable ep0 ready for host detection and then
3197 * set configuration.
3198 */
3199
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003200 /* keep other bits untouched (so e.g. forced modes are not lost) */
3201 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3202 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01003203 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003204
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003205 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003206 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3207 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003208 /* FS/LS Dedicated Transceiver Interface */
3209 usbcfg |= GUSBCFG_PHYSEL;
3210 } else {
3211 /* set the PLL on, remove the HNP/SRP and set the PHY */
3212 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3213 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3214 (val << GUSBCFG_USBTRDTIM_SHIFT);
3215 }
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003216 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003217
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003218 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003219
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003220 if (!is_usb_reset)
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003221 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003222
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003223 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003224
3225 switch (hsotg->params.speed) {
3226 case DWC2_SPEED_PARAM_LOW:
3227 dcfg |= DCFG_DEVSPD_LS;
3228 break;
3229 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003230 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3231 dcfg |= DCFG_DEVSPD_FS48;
3232 else
3233 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003234 break;
3235 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003236 dcfg |= DCFG_DEVSPD_HS;
3237 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003238
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003239 dwc2_writel(dcfg, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003240
3241 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003242 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003243
3244 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003245 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003246 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003247 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003248 GINTSTS_USBRST | GINTSTS_RESETDET |
3249 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003250 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3251 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003252
3253 if (!using_desc_dma(hsotg))
3254 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003255
John Youn95832c02017-01-23 14:57:26 -08003256 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003257 intmsk |= GINTSTS_CONIDSTSCHNG;
3258
3259 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003260
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003261 if (using_dma(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003262 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003263 hsotg->params.ahbcfg,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003264 hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003265
3266 /* Set DDMA mode support in the core if needed */
3267 if (using_desc_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003268 dwc2_set_bit(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003269
3270 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003271 dwc2_writel(((hsotg->dedicated_fifos) ?
3272 (GAHBCFG_NP_TXF_EMP_LVL |
3273 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3274 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003275 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003276
3277 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003278 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3279 * when we have no data to transfer. Otherwise we get being flooded by
3280 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003281 */
3282
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003283 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003284 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003285 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003286 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003287 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003288
3289 /*
3290 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003291 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003292 */
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003293 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3294 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003295 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003296 DOEPMSK_SETUPMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003297 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003298
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003299 /* Enable BNA interrupt for DDMA */
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003300 if (using_desc_dma(hsotg)) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003301 dwc2_set_bit(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003302 dwc2_set_bit(hsotg->regs + DIEPMSK, DIEPMSK_BNAININTRMSK);
3303 }
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003304
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003305 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003306
3307 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003308 dwc2_readl(hsotg->regs + DIEPCTL0),
3309 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003310
3311 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003312 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003313
3314 /*
3315 * Enable the RXFIFO when in slave mode, as this is how we collect
3316 * the data. In DMA mode, we get events from the FIFO but also
3317 * things we cannot process, so do not use it.
3318 */
3319 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003320 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003321
3322 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003323 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3324 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003325
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003326 if (!is_usb_reset) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003327 dwc2_set_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003328 udelay(10); /* see openiboot */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003329 dwc2_clear_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003330 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003331
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003332 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003333
3334 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003335 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003336 * writing to the EPCTL register..
3337 */
3338
3339 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003340 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003341 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003342
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003343 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003344 DXEPCTL_CNAK | DXEPCTL_EPENA |
3345 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003346 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003347
3348 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003349 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003350 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003351
Lukasz Majewski308d7342012-05-04 14:17:05 +02003352 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003353 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3354 if (!is_usb_reset)
3355 val |= DCTL_SFTDISCON;
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003356 dwc2_set_bit(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003357
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003358 /* configure the core to support LPM */
3359 dwc2_gadget_init_lpm(hsotg);
3360
Lukasz Majewski308d7342012-05-04 14:17:05 +02003361 /* must be at-least 3ms to allow bus to see disconnect */
3362 mdelay(3);
3363
Gregory Herrero065d3932015-09-22 15:16:54 +02003364 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003365
3366 dwc2_hsotg_enqueue_setup(hsotg);
3367
3368 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3369 dwc2_readl(hsotg->regs + DIEPCTL0),
3370 dwc2_readl(hsotg->regs + DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003371}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003372
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003373static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003374{
3375 /* set the soft-disconnect bit */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003376 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003377}
3378
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003379void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003380{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003381 /* remove the soft-disconnect and let's go */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003382 dwc2_clear_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003383}
3384
3385/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003386 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3387 * @hsotg: The device state:
3388 *
3389 * This interrupt indicates one of the following conditions occurred while
3390 * transmitting an ISOC transaction.
3391 * - Corrupted IN Token for ISOC EP.
3392 * - Packet not complete in FIFO.
3393 *
3394 * The following actions will be taken:
3395 * - Determine the EP
3396 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3397 */
3398static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3399{
3400 struct dwc2_hsotg_ep *hs_ep;
3401 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003402 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003403 u32 idx;
3404
3405 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3406
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003407 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3408
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003409 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3410 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003411 /* Proceed only unmasked ISOC EPs */
3412 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3413 continue;
3414
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003415 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003416 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003417 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3418 epctrl |= DXEPCTL_SNAK;
3419 epctrl |= DXEPCTL_EPDIS;
3420 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3421 }
3422 }
3423
3424 /* Clear interrupt */
3425 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3426}
3427
3428/**
3429 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3430 * @hsotg: The device state:
3431 *
3432 * This interrupt indicates one of the following conditions occurred while
3433 * transmitting an ISOC transaction.
3434 * - Corrupted OUT Token for ISOC EP.
3435 * - Packet not complete in FIFO.
3436 *
3437 * The following actions will be taken:
3438 * - Determine the EP
3439 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3440 */
3441static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3442{
3443 u32 gintsts;
3444 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003445 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003446 u32 epctrl;
3447 struct dwc2_hsotg_ep *hs_ep;
3448 int idx;
3449
3450 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3451
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003452 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3453 daintmsk >>= DAINT_OUTEP_SHIFT;
3454
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003455 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3456 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003457 /* Proceed only unmasked ISOC EPs */
3458 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3459 continue;
3460
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003461 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003462 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003463 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3464 /* Unmask GOUTNAKEFF interrupt */
3465 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3466 gintmsk |= GINTSTS_GOUTNAKEFF;
3467 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3468
3469 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003470 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003471 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003472 break;
3473 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003474 }
3475 }
3476
3477 /* Clear interrupt */
3478 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3479}
3480
3481/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003482 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003483 * @irq: The IRQ number triggered
3484 * @pw: The pw value when registered the handler.
3485 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003486static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003487{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003488 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003489 int retry_count = 8;
3490 u32 gintsts;
3491 u32 gintmsk;
3492
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003493 if (!dwc2_is_device_mode(hsotg))
3494 return IRQ_NONE;
3495
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003496 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003497irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003498 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3499 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003500
3501 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3502 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3503
3504 gintsts &= gintmsk;
3505
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003506 if (gintsts & GINTSTS_RESETDET) {
3507 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3508
3509 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3510
3511 /* This event must be used only if controller is suspended */
3512 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003513 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003514 hsotg->lx_state = DWC2_L0;
3515 }
3516 }
3517
3518 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003519 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3520 u32 connected = hsotg->connected;
3521
3522 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3523 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3524 dwc2_readl(hsotg->regs + GNPTXSTS));
3525
3526 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3527
3528 /* Report disconnection if it is not already done. */
3529 dwc2_hsotg_disconnect(hsotg);
3530
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003531 /* Reset device address to zero */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003532 dwc2_clear_bit(hsotg->regs + DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003533
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003534 if (usb_status & GOTGCTL_BSESVLD && connected)
3535 dwc2_hsotg_core_init_disconnected(hsotg, true);
3536 }
3537
Dinh Nguyen47a16852014-04-14 14:13:34 -07003538 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003539 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003540
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003541 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003542 }
3543
Dinh Nguyen47a16852014-04-14 14:13:34 -07003544 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003545 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3546 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003547 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003548 int ep;
3549
Robert Baldyga7e804652013-09-19 11:50:20 +02003550 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003551 daint_out = daint >> DAINT_OUTEP_SHIFT;
3552 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003553
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003554 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3555
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003556 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3557 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003558 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003559 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003560 }
3561
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003562 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3563 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003564 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003565 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003566 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003567 }
3568
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003569 /* check both FIFOs */
3570
Dinh Nguyen47a16852014-04-14 14:13:34 -07003571 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003572 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3573
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003574 /*
3575 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003576 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003577 * it needs re-enabling
3578 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003579
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003580 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3581 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003582 }
3583
Dinh Nguyen47a16852014-04-14 14:13:34 -07003584 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003585 dev_dbg(hsotg->dev, "PTxFEmp\n");
3586
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003587 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003588
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003589 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3590 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003591 }
3592
Dinh Nguyen47a16852014-04-14 14:13:34 -07003593 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003594 /*
3595 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003596 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003597 * set.
3598 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003599
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003600 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003601 }
3602
Dinh Nguyen47a16852014-04-14 14:13:34 -07003603 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003604 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003605 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003606 }
3607
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003608 /*
3609 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003610 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003611 * the occurrence.
3612 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003613
Dinh Nguyen47a16852014-04-14 14:13:34 -07003614 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003615 u8 idx;
3616 u32 epctrl;
3617 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003618 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003619 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003620
Razmik Karapetyand8484552018-01-19 14:41:42 +04003621 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3622 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003623 /* Mask this interrupt */
3624 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3625 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3626 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003627
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003628 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3629 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3630 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003631 /* Proceed only unmasked ISOC EPs */
3632 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3633 continue;
3634
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003635 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3636
Razmik Karapetyand8484552018-01-19 14:41:42 +04003637 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003638 epctrl |= DXEPCTL_SNAK;
3639 epctrl |= DXEPCTL_EPDIS;
3640 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3641 }
3642 }
3643
3644 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003645 }
3646
Dinh Nguyen47a16852014-04-14 14:13:34 -07003647 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003648 dev_info(hsotg->dev, "GINNakEff triggered\n");
3649
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003650 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003651
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003652 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003653 }
3654
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003655 if (gintsts & GINTSTS_INCOMPL_SOIN)
3656 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003657
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003658 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3659 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003660
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003661 /*
3662 * if we've had fifo events, we should try and go around the
3663 * loop again to see if there's any point in returning yet.
3664 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003665
3666 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003667 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003668
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003669 spin_unlock(&hsotg->lock);
3670
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003671 return IRQ_HANDLED;
3672}
3673
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003674static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3675 struct dwc2_hsotg_ep *hs_ep)
3676{
3677 u32 epctrl_reg;
3678 u32 epint_reg;
3679
3680 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3681 DOEPCTL(hs_ep->index);
3682 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3683 DOEPINT(hs_ep->index);
3684
3685 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3686 hs_ep->name);
3687
3688 if (hs_ep->dir_in) {
3689 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003690 dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003691 /* Wait for Nak effect */
3692 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3693 DXEPINT_INEPNAKEFF, 100))
3694 dev_warn(hsotg->dev,
3695 "%s: timeout DIEPINT.NAKEFF\n",
3696 __func__);
3697 } else {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003698 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003699 /* Wait for Nak effect */
3700 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3701 GINTSTS_GINNAKEFF, 100))
3702 dev_warn(hsotg->dev,
3703 "%s: timeout GINTSTS.GINNAKEFF\n",
3704 __func__);
3705 }
3706 } else {
3707 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003708 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003709
3710 /* Wait for global nak to take effect */
3711 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3712 GINTSTS_GOUTNAKEFF, 100))
3713 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3714 __func__);
3715 }
3716
3717 /* Disable ep */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003718 dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003719
3720 /* Wait for ep to be disabled */
3721 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3722 dev_warn(hsotg->dev,
3723 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3724
3725 /* Clear EPDISBLD interrupt */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003726 dwc2_set_bit(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003727
3728 if (hs_ep->dir_in) {
3729 unsigned short fifo_index;
3730
3731 if (hsotg->dedicated_fifos || hs_ep->periodic)
3732 fifo_index = hs_ep->fifo_index;
3733 else
3734 fifo_index = 0;
3735
3736 /* Flush TX FIFO */
3737 dwc2_flush_tx_fifo(hsotg, fifo_index);
3738
3739 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3740 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003741 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003742
3743 } else {
3744 /* Remove global NAKs */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003745 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003746 }
3747}
3748
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003749/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003750 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003751 * @ep: The USB endpint to configure
3752 * @desc: The USB endpoint descriptor to configure with.
3753 *
3754 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003755 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003756static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003757 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003758{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003759 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003760 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003761 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003762 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003763 u32 epctrl_reg;
3764 u32 epctrl;
3765 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003766 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003767 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003768 unsigned int dir_in;
3769 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003770 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003771 unsigned char ep_type;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003772
3773 dev_dbg(hsotg->dev,
3774 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3775 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3776 desc->wMaxPacketSize, desc->bInterval);
3777
3778 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003779 if (index == 0) {
3780 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3781 return -EINVAL;
3782 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003783
3784 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3785 if (dir_in != hs_ep->dir_in) {
3786 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3787 return -EINVAL;
3788 }
3789
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003790 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003791 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003792 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003793
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003794 /* ISOC IN in DDMA supported bInterval up to 10 */
3795 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3796 dir_in && desc->bInterval > 10) {
3797 dev_err(hsotg->dev,
3798 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3799 return -EINVAL;
3800 }
3801
3802 /* High bandwidth ISOC OUT in DDMA not supported */
3803 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3804 !dir_in && mc > 1) {
3805 dev_err(hsotg->dev,
3806 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3807 return -EINVAL;
3808 }
3809
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003810 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003811
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003812 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003813 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003814
3815 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3816 __func__, epctrl, epctrl_reg);
3817
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003818 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003819 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3820 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003821 MAX_DMA_DESC_NUM_GENERIC *
3822 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003823 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003824 if (!hs_ep->desc_list) {
3825 ret = -ENOMEM;
3826 goto error2;
3827 }
3828 }
3829
Lukasz Majewski22258f42012-06-14 10:02:24 +02003830 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003831
Dinh Nguyen47a16852014-04-14 14:13:34 -07003832 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3833 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003834
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003835 /*
3836 * mark the endpoint as active, otherwise the core may ignore
3837 * transactions entirely for this endpoint
3838 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003839 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003840
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003841 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003842 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003843
3844 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003845 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003846 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003847 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003848 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003849
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003850 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003851 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003852 epctrl |= DXEPCTL_EPTYPE_ISO;
3853 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003854 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003855 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003856 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003857 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003858 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003859 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003860 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003861 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3862 mask |= DIEPMSK_NAKMSK;
3863 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3864 } else {
3865 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3866 mask |= DOEPMSK_OUTTKNEPDISMSK;
3867 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3868 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003869 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003870
3871 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003872 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003873 break;
3874
3875 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003876 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003877 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003878
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003879 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3880 hs_ep->interval = 1 << (desc->bInterval - 1);
3881
Dinh Nguyen47a16852014-04-14 14:13:34 -07003882 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003883 break;
3884
3885 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003886 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003887 break;
3888 }
3889
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003890 /*
3891 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003892 * a unique tx-fifo even if it is non-periodic.
3893 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003894 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003895 u32 fifo_index = 0;
3896 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08003897
3898 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003899 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08003900 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02003901 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003902 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08003903 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003904 if (val < size)
3905 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003906 /* Search for smallest acceptable fifo */
3907 if (val < fifo_size) {
3908 fifo_size = val;
3909 fifo_index = i;
3910 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003911 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003912 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003913 dev_err(hsotg->dev,
3914 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303915 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003916 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303917 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003918 hsotg->fifo_map |= 1 << fifo_index;
3919 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3920 hs_ep->fifo_index = fifo_index;
3921 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003922 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003923
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003924 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003925 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003926 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003927
3928 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3929 __func__, epctrl);
3930
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003931 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003932 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003933 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003934
3935 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003936 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003937
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003938error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003939 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003940
3941error2:
3942 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003943 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003944 sizeof(struct dwc2_dma_desc),
3945 hs_ep->desc_list, hs_ep->desc_list_dma);
3946 hs_ep->desc_list = NULL;
3947 }
3948
Julia Lawall19c190f2010-03-29 17:36:44 +02003949 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003950}
3951
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003952/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003953 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003954 * @ep: The endpoint to disable.
3955 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003956static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003957{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003958 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003959 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003960 int dir_in = hs_ep->dir_in;
3961 int index = hs_ep->index;
3962 unsigned long flags;
3963 u32 epctrl_reg;
3964 u32 ctrl;
3965
Marek Szyprowski1e011292014-09-09 10:44:54 +02003966 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003967
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003968 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003969 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3970 return -EINVAL;
3971 }
3972
John Stultz9b4810922017-10-23 14:32:49 -07003973 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3974 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
3975 return -EINVAL;
3976 }
3977
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003978 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003979
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003980 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003981
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003982 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003983
3984 if (ctrl & DXEPCTL_EPENA)
3985 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
3986
Dinh Nguyen47a16852014-04-14 14:13:34 -07003987 ctrl &= ~DXEPCTL_EPENA;
3988 ctrl &= ~DXEPCTL_USBACTEP;
3989 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003990
3991 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003992 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003993
3994 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003995 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003996
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003997 /* terminate all requests with shutdown */
3998 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3999
Robert Baldyga1c07b202016-08-29 13:39:00 -07004000 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4001 hs_ep->fifo_index = 0;
4002 hs_ep->fifo_size = 0;
4003
Lukasz Majewski22258f42012-06-14 10:02:24 +02004004 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004005 return 0;
4006}
4007
4008/**
4009 * on_list - check request is on the given endpoint
4010 * @ep: The endpoint to check.
4011 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004012 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004013static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004014{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004015 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004016
4017 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4018 if (req == test)
4019 return true;
4020 }
4021
4022 return false;
4023}
4024
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004025/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004026 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004027 * @ep: The endpoint to dequeue.
4028 * @req: The request to be removed from a queue.
4029 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004030static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004031{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004032 struct dwc2_hsotg_req *hs_req = our_req(req);
4033 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004034 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004035 unsigned long flags;
4036
Marek Szyprowski1e011292014-09-09 10:44:54 +02004037 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004038
Lukasz Majewski22258f42012-06-14 10:02:24 +02004039 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004040
4041 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004042 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004043 return -EINVAL;
4044 }
4045
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004046 /* Dequeue already started request */
4047 if (req == &hs_ep->req->req)
4048 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4049
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004050 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004051 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004052
4053 return 0;
4054}
4055
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004056/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004057 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004058 * @ep: The endpoint to set halt.
4059 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004060 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4061 * the endpoint is busy processing requests.
4062 *
4063 * We need to stall the endpoint immediately if request comes from set_feature
4064 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004065 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004066static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004067{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004068 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004069 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004070 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004071 u32 epreg;
4072 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004073 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004074
4075 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4076
Robert Baldygac9f721b2014-01-14 08:36:00 +01004077 if (index == 0) {
4078 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004079 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004080 else
4081 dev_warn(hs->dev,
4082 "%s: can't clear halt on ep0\n", __func__);
4083 return 0;
4084 }
4085
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004086 if (hs_ep->isochronous) {
4087 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4088 return -EINVAL;
4089 }
4090
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004091 if (!now && value && !list_empty(&hs_ep->queue)) {
4092 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4093 ep->name);
4094 return -EAGAIN;
4095 }
4096
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004097 if (hs_ep->dir_in) {
4098 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004099 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004100
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004101 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004102 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004103 if (epctl & DXEPCTL_EPENA)
4104 epctl |= DXEPCTL_EPDIS;
4105 } else {
4106 epctl &= ~DXEPCTL_STALL;
4107 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4108 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004109 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004110 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004111 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004112 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004113 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004114 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004115 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004116
John Youn34c0887f2017-01-17 20:31:43 -08004117 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004118 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004119 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004120 epctl &= ~DXEPCTL_STALL;
4121 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4122 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004123 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004124 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004125 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004126 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004127 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004128
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004129 hs_ep->halted = value;
4130
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004131 return 0;
4132}
4133
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004134/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004135 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004136 * @ep: The endpoint to set halt.
4137 * @value: Set or unset the halt.
4138 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004139static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004140{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004141 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004142 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004143 unsigned long flags = 0;
4144 int ret = 0;
4145
4146 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004147 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004148 spin_unlock_irqrestore(&hs->lock, flags);
4149
4150 return ret;
4151}
4152
Bhumika Goyalebce5612017-08-12 17:34:55 +05304153static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004154 .enable = dwc2_hsotg_ep_enable,
4155 .disable = dwc2_hsotg_ep_disable,
4156 .alloc_request = dwc2_hsotg_ep_alloc_request,
4157 .free_request = dwc2_hsotg_ep_free_request,
4158 .queue = dwc2_hsotg_ep_queue_lock,
4159 .dequeue = dwc2_hsotg_ep_dequeue,
4160 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004161 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004162};
4163
4164/**
John Youn9da51972017-01-17 20:30:27 -08004165 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004166 * @hsotg: The driver state
4167 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004168static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004169{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004170 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004171 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004172 /* unmask subset of endpoint interrupts */
4173
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004174 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4175 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4176 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004177
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004178 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4179 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4180 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004181
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004182 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004183
4184 /* Be in disconnected state until gadget is registered */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04004185 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004186
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004187 /* setup fifos */
4188
4189 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004190 dwc2_readl(hsotg->regs + GRXFSIZ),
4191 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004192
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004193 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004194
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004195 /* keep other bits untouched (so e.g. forced modes are not lost) */
4196 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4197 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01004198 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004199
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004200 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004201 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004202 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4203 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4204 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004205
Gregory Herrerof5090042015-01-09 13:38:47 +01004206 if (using_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04004207 dwc2_set_bit(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004208}
4209
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004210/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004211 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004212 * @gadget: The usb gadget state
4213 * @driver: The usb gadget driver
4214 *
4215 * Perform initialization to prepare udc device and driver
4216 * to work.
4217 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004218static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004219 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004220{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004221 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004222 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004223 int ret;
4224
4225 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004226 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004227 return -ENODEV;
4228 }
4229
4230 if (!driver) {
4231 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4232 return -EINVAL;
4233 }
4234
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004235 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004236 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004237
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004238 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004239 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4240 return -EINVAL;
4241 }
4242
4243 WARN_ON(hsotg->driver);
4244
4245 driver->driver.bus = NULL;
4246 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004247 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004248 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4249
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004250 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4251 ret = dwc2_lowlevel_hw_enable(hsotg);
4252 if (ret)
4253 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004254 }
4255
Gregory Herrerof6c01592015-01-09 13:38:41 +01004256 if (!IS_ERR_OR_NULL(hsotg->uphy))
4257 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004258
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004259 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004260 if (dwc2_hw_is_device(hsotg)) {
4261 dwc2_hsotg_init(hsotg);
4262 dwc2_hsotg_core_init_disconnected(hsotg, false);
4263 }
4264
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004265 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004266 spin_unlock_irqrestore(&hsotg->lock, flags);
4267
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004268 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004269
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004270 return 0;
4271
4272err:
4273 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004274 return ret;
4275}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004276
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004277/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004278 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004279 * @gadget: The usb gadget state
4280 * @driver: The usb gadget driver
4281 *
4282 * Stop udc hw block and stay tunned for future transmissions
4283 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004284static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004285{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004286 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004287 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004288 int ep;
4289
4290 if (!hsotg)
4291 return -ENODEV;
4292
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004293 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004294 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4295 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004296 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004297 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004298 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004299 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004300
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004301 spin_lock_irqsave(&hsotg->lock, flags);
4302
Marek Szyprowski32805c32014-10-20 12:45:33 +02004303 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004304 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004305 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004306
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004307 spin_unlock_irqrestore(&hsotg->lock, flags);
4308
Gregory Herrerof6c01592015-01-09 13:38:41 +01004309 if (!IS_ERR_OR_NULL(hsotg->uphy))
4310 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004311
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004312 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4313 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004314
4315 return 0;
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_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004320 * @gadget: The usb gadget state
4321 *
4322 * Read the {micro} frame number
4323 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004324static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004325{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004326 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004327}
4328
Lukasz Majewskia188b682012-06-22 09:29:56 +02004329/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004330 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004331 * @gadget: The usb gadget state
4332 * @is_on: Current state of the USB PHY
4333 *
4334 * Connect/Disconnect the USB PHY pullup
4335 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004336static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004337{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004338 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004339 unsigned long flags = 0;
4340
Gregory Herrero77ba9112015-09-29 12:08:19 +02004341 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004342 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004343
4344 /* Don't modify pullup state while in host mode */
4345 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4346 hsotg->enabled = is_on;
4347 return 0;
4348 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004349
4350 spin_lock_irqsave(&hsotg->lock, flags);
4351 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004352 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004353 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004354 /* Enable ACG feature in device mode,if supported */
4355 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004356 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004357 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004358 dwc2_hsotg_core_disconnect(hsotg);
4359 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004360 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004361 }
4362
4363 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4364 spin_unlock_irqrestore(&hsotg->lock, flags);
4365
4366 return 0;
4367}
4368
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004369static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004370{
4371 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4372 unsigned long flags;
4373
4374 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4375 spin_lock_irqsave(&hsotg->lock, flags);
4376
Gregory Herrero61f72232015-09-29 12:08:28 +02004377 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004378 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004379 * before being initialized / de-initialized
4380 */
4381 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004382 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004383
Gregory Herrero83d98222015-01-09 13:39:02 +01004384 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004385 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004386
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004387 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004388 if (hsotg->enabled) {
4389 /* Enable ACG feature in device mode,if supported */
4390 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004391 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004392 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004393 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004394 dwc2_hsotg_core_disconnect(hsotg);
4395 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004396 }
4397
4398 spin_unlock_irqrestore(&hsotg->lock, flags);
4399 return 0;
4400}
4401
Gregory Herrero596d6962015-01-09 13:39:08 +01004402/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004403 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004404 * @gadget: The usb gadget state
4405 * @mA: Amount of current
4406 *
4407 * Report how much power the device may consume to the phy.
4408 */
John Youn9da51972017-01-17 20:30:27 -08004409static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004410{
4411 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4412
4413 if (IS_ERR_OR_NULL(hsotg->uphy))
4414 return -ENOTSUPP;
4415 return usb_phy_set_power(hsotg->uphy, mA);
4416}
4417
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004418static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4419 .get_frame = dwc2_hsotg_gadget_getframe,
4420 .udc_start = dwc2_hsotg_udc_start,
4421 .udc_stop = dwc2_hsotg_udc_stop,
4422 .pullup = dwc2_hsotg_pullup,
4423 .vbus_session = dwc2_hsotg_vbus_session,
4424 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004425};
4426
4427/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004428 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004429 * @hsotg: The device state.
4430 * @hs_ep: The endpoint to be initialised.
4431 * @epnum: The endpoint number
4432 *
4433 * Initialise the given endpoint (as part of the probe and device state
4434 * creation) to give to the gadget driver. Setup the endpoint name, any
4435 * direction information and other state that may be required.
4436 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004437static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004438 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004439 int epnum,
4440 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004441{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004442 char *dir;
4443
4444 if (epnum == 0)
4445 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004446 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004447 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004448 else
4449 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004450
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004451 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004452 hs_ep->index = epnum;
4453
4454 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4455
4456 INIT_LIST_HEAD(&hs_ep->queue);
4457 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4458
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004459 /* add to the list of endpoints known by the gadget driver */
4460 if (epnum)
4461 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4462
4463 hs_ep->parent = hsotg;
4464 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004465
4466 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4467 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4468 else
4469 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4470 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004471 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004472
Robert Baldyga29545222015-07-31 16:00:18 +02004473 if (epnum == 0) {
4474 hs_ep->ep.caps.type_control = true;
4475 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004476 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4477 hs_ep->ep.caps.type_iso = true;
4478 hs_ep->ep.caps.type_bulk = true;
4479 }
Robert Baldyga29545222015-07-31 16:00:18 +02004480 hs_ep->ep.caps.type_int = true;
4481 }
4482
4483 if (dir_in)
4484 hs_ep->ep.caps.dir_in = true;
4485 else
4486 hs_ep->ep.caps.dir_out = true;
4487
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004488 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004489 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004490 * to be something valid.
4491 */
4492
4493 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004494 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004495
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004496 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004497 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004498 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004499 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004500 }
4501}
4502
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004503/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004504 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004505 * @param: The device state
4506 *
4507 * Read the USB core HW configuration registers
4508 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004509static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004510{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004511 u32 cfg;
4512 u32 ep_type;
4513 u32 i;
4514
Ben Dooks10aebc72010-07-19 09:40:44 +01004515 /* check hardware configuration */
4516
John Youn43e90342015-12-17 11:17:45 -08004517 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4518
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004519 /* Add ep0 */
4520 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004521
John Younb98866c2017-01-17 20:31:58 -08004522 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4523 sizeof(struct dwc2_hsotg_ep),
4524 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004525 if (!hsotg->eps_in[0])
4526 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004527 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004528 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004529
John Youn43e90342015-12-17 11:17:45 -08004530 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004531 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004532 ep_type = cfg & 3;
4533 /* Direction in or both */
4534 if (!(ep_type & 2)) {
4535 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004536 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004537 if (!hsotg->eps_in[i])
4538 return -ENOMEM;
4539 }
4540 /* Direction out or both */
4541 if (!(ep_type & 1)) {
4542 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004543 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004544 if (!hsotg->eps_out[i])
4545 return -ENOMEM;
4546 }
4547 }
4548
John Youn43e90342015-12-17 11:17:45 -08004549 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4550 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004551
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004552 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4553 hsotg->num_of_eps,
4554 hsotg->dedicated_fifos ? "dedicated" : "shared",
4555 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004556 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004557}
4558
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004559/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004560 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004561 * @param: The device state
4562 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004563static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004564{
Mark Brown83a01802011-06-01 17:16:15 +01004565#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004566 struct device *dev = hsotg->dev;
4567 void __iomem *regs = hsotg->regs;
4568 u32 val;
4569 int idx;
4570
4571 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004572 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4573 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004574
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004575 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004576 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004577
4578 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004579 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004580
4581 /* show periodic fifo settings */
4582
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004583 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004584 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004585 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004586 val >> FIFOSIZE_DEPTH_SHIFT,
4587 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004588 }
4589
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004590 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004591 dev_info(dev,
4592 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004593 dwc2_readl(regs + DIEPCTL(idx)),
4594 dwc2_readl(regs + DIEPTSIZ(idx)),
4595 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004596
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004597 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004598 dev_info(dev,
4599 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004600 idx, dwc2_readl(regs + DOEPCTL(idx)),
4601 dwc2_readl(regs + DOEPTSIZ(idx)),
4602 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004603 }
4604
4605 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004606 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004607#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004608}
4609
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004610/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004611 * dwc2_gadget_init - init function for gadget
4612 * @dwc2: The data structure for the DWC2 driver.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004613 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004614int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004615{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004616 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004617 int epnum;
4618 int ret;
John Youn43e90342015-12-17 11:17:45 -08004619
Gregory Herrero0a176272015-01-09 13:38:52 +01004620 /* Dump fifo information */
4621 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004622 hsotg->params.g_np_tx_fifo_size);
4623 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004624
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004625 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004626 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004627 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004628 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004629
4630 if (hsotg->params.lpm)
4631 hsotg->gadget.lpm_capable = true;
4632
Gregory Herrero097ee662015-04-29 22:09:10 +02004633 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4634 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004635 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4636 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004637
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004638 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004639 if (ret) {
4640 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004641 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004642 }
4643
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004644 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4645 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004646 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004647 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004648
4649 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4650 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004651 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004652 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004653
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004654 if (using_desc_dma(hsotg)) {
4655 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4656 if (ret < 0)
4657 return ret;
4658 }
4659
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004660 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4661 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004662 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004663 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004664 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004665 }
4666
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004667 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4668
4669 if (hsotg->num_of_eps == 0) {
4670 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004671 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004672 }
4673
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004674 /* setup endpoint information */
4675
4676 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004677 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004678
4679 /* allocate EP0 request */
4680
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004681 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004682 GFP_KERNEL);
4683 if (!hsotg->ctrl_req) {
4684 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004685 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004686 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004687
4688 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004689 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4690 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004691 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004692 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004693 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004694 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004695 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004696 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004697
Dinh Nguyen117777b2014-11-11 11:13:34 -06004698 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004699 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004700 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004701
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004702 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004703
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004704 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004705}
4706
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004707/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004708 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004709 * @pdev: The platform information for the driver
4710 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004711int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004712{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004713 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004714
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004715 return 0;
4716}
4717
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004718int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004719{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004720 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004721
Gregory Herrero9e779772015-04-29 22:09:07 +02004722 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004723 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004724
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004725 if (hsotg->driver) {
4726 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004727
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004728 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4729 hsotg->driver->driver.name);
4730
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004731 spin_lock_irqsave(&hsotg->lock, flags);
4732 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004733 dwc2_hsotg_core_disconnect(hsotg);
4734 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004735 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4736 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004737
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004738 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4739 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004740 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004741 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004742 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004743 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004744 }
4745
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004746 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004747}
4748
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004749int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004750{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004751 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004752
Gregory Herrero9e779772015-04-29 22:09:07 +02004753 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004754 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004755
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004756 if (hsotg->driver) {
4757 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4758 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004759
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004760 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004761 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004762 if (hsotg->enabled) {
4763 /* Enable ACG feature in device mode,if supported */
4764 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004765 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004766 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004767 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004768 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004769
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004770 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004771}
John Youn58e52ff6a2016-02-23 19:54:57 -08004772
4773/**
4774 * dwc2_backup_device_registers() - Backup controller device registers.
4775 * When suspending usb bus, registers needs to be backuped
4776 * if controller power is disabled once suspended.
4777 *
4778 * @hsotg: Programming view of the DWC_otg controller
4779 */
4780int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4781{
4782 struct dwc2_dregs_backup *dr;
4783 int i;
4784
4785 dev_dbg(hsotg->dev, "%s\n", __func__);
4786
4787 /* Backup dev regs */
4788 dr = &hsotg->dr_backup;
4789
4790 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4791 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4792 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4793 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4794 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4795
4796 for (i = 0; i < hsotg->num_of_eps; i++) {
4797 /* Backup IN EPs */
4798 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4799
4800 /* Ensure DATA PID is correctly configured */
4801 if (dr->diepctl[i] & DXEPCTL_DPID)
4802 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4803 else
4804 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4805
4806 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4807 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4808
4809 /* Backup OUT EPs */
4810 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4811
4812 /* Ensure DATA PID is correctly configured */
4813 if (dr->doepctl[i] & DXEPCTL_DPID)
4814 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4815 else
4816 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4817
4818 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4819 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
Vardan Mikayelyanaf7c2bd2018-02-16 14:07:33 +04004820 dr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004821 }
4822 dr->valid = true;
4823 return 0;
4824}
4825
4826/**
4827 * dwc2_restore_device_registers() - Restore controller device registers.
4828 * When resuming usb bus, device registers needs to be restored
4829 * if controller power were disabled.
4830 *
4831 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004832 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
4833 *
4834 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08004835 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004836int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08004837{
4838 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08004839 int i;
4840
4841 dev_dbg(hsotg->dev, "%s\n", __func__);
4842
4843 /* Restore dev regs */
4844 dr = &hsotg->dr_backup;
4845 if (!dr->valid) {
4846 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4847 __func__);
4848 return -EINVAL;
4849 }
4850 dr->valid = false;
4851
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004852 if (!remote_wakeup)
4853 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4854
John Youn58e52ff6a2016-02-23 19:54:57 -08004855 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4856 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4857 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4858
4859 for (i = 0; i < hsotg->num_of_eps; i++) {
4860 /* Restore IN EPs */
John Youn58e52ff6a2016-02-23 19:54:57 -08004861 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4862 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004863 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004864 /** WA for enabled EPx's IN in DDMA mode. On entering to
4865 * hibernation wrong value read and saved from DIEPDMAx,
4866 * as result BNA interrupt asserted on hibernation exit
4867 * by restoring from saved area.
4868 */
4869 if (hsotg->params.g_dma_desc &&
4870 (dr->diepctl[i] & DXEPCTL_EPENA))
4871 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
4872 dwc2_writel(dr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
4873 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4874 /* Restore OUT EPs */
4875 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4876 /* WA for enabled EPx's OUT in DDMA mode. On entering to
4877 * hibernation wrong value read and saved from DOEPDMAx,
4878 * as result BNA interrupt asserted on hibernation exit
4879 * by restoring from saved area.
4880 */
4881 if (hsotg->params.g_dma_desc &&
4882 (dr->doepctl[i] & DXEPCTL_EPENA))
4883 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
John Youn58e52ff6a2016-02-23 19:54:57 -08004884 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004885 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004886 }
4887
John Youn58e52ff6a2016-02-23 19:54:57 -08004888 return 0;
4889}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04004890
4891/**
4892 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
4893 *
4894 * @hsotg: Programming view of DWC_otg controller
4895 *
4896 */
4897void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
4898{
4899 u32 val;
4900
4901 if (!hsotg->params.lpm)
4902 return;
4903
4904 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
4905 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
4906 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
4907 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
4908 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
4909 dwc2_writel(val, hsotg->regs + GLPMCFG);
4910 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg->regs
4911 + GLPMCFG));
4912}
Vardan Mikayelyanc5c403dc2018-02-16 14:10:13 +04004913
4914/**
4915 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
4916 *
4917 * @hsotg: Programming view of the DWC_otg controller
4918 *
4919 * Return non-zero if failed to enter to hibernation.
4920 */
4921int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
4922{
4923 u32 gpwrdn;
4924 int ret = 0;
4925
4926 /* Change to L2(suspend) state */
4927 hsotg->lx_state = DWC2_L2;
4928 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
4929 ret = dwc2_backup_global_registers(hsotg);
4930 if (ret) {
4931 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
4932 __func__);
4933 return ret;
4934 }
4935 ret = dwc2_backup_device_registers(hsotg);
4936 if (ret) {
4937 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
4938 __func__);
4939 return ret;
4940 }
4941
4942 gpwrdn = GPWRDN_PWRDNRSTN;
4943 gpwrdn |= GPWRDN_PMUACTV;
4944 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4945 udelay(10);
4946
4947 /* Set flag to indicate that we are in hibernation */
4948 hsotg->hibernated = 1;
4949
4950 /* Enable interrupts from wake up logic */
4951 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4952 gpwrdn |= GPWRDN_PMUINTSEL;
4953 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4954 udelay(10);
4955
4956 /* Unmask device mode interrupts in GPWRDN */
4957 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4958 gpwrdn |= GPWRDN_RST_DET_MSK;
4959 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
4960 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
4961 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4962 udelay(10);
4963
4964 /* Enable Power Down Clamp */
4965 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4966 gpwrdn |= GPWRDN_PWRDNCLMP;
4967 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4968 udelay(10);
4969
4970 /* Switch off VDD */
4971 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4972 gpwrdn |= GPWRDN_PWRDNSWTCH;
4973 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4974 udelay(10);
4975
4976 /* Save gpwrdn register for further usage if stschng interrupt */
4977 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4978 dev_dbg(hsotg->dev, "Hibernation completed\n");
4979
4980 return ret;
4981}
4982
4983/**
4984 * dwc2_gadget_exit_hibernation()
4985 * This function is for exiting from Device mode hibernation by host initiated
4986 * resume/reset and device initiated remote-wakeup.
4987 *
4988 * @hsotg: Programming view of the DWC_otg controller
4989 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
4990 * @param reset: indicates whether resume is initiated by Reset.
4991 *
4992 * Return non-zero if failed to exit from hibernation.
4993 */
4994int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
4995 int rem_wakeup, int reset)
4996{
4997 u32 pcgcctl;
4998 u32 gpwrdn;
4999 u32 dctl;
5000 int ret = 0;
5001 struct dwc2_gregs_backup *gr;
5002 struct dwc2_dregs_backup *dr;
5003
5004 gr = &hsotg->gr_backup;
5005 dr = &hsotg->dr_backup;
5006
5007 if (!hsotg->hibernated) {
5008 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5009 return 1;
5010 }
5011 dev_dbg(hsotg->dev,
5012 "%s: called with rem_wakeup = %d reset = %d\n",
5013 __func__, rem_wakeup, reset);
5014
5015 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5016
5017 if (!reset) {
5018 /* Clear all pending interupts */
5019 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5020 }
5021
5022 /* De-assert Restore */
5023 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5024 gpwrdn &= ~GPWRDN_RESTORE;
5025 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5026 udelay(10);
5027
5028 if (!rem_wakeup) {
5029 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
5030 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
5031 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
5032 }
5033
5034 /* Restore GUSBCFG, DCFG and DCTL */
5035 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
5036 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
5037 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
5038
5039 /* De-assert Wakeup Logic */
5040 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5041 gpwrdn &= ~GPWRDN_PMUACTV;
5042 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5043
5044 if (rem_wakeup) {
5045 udelay(10);
5046 /* Start Remote Wakeup Signaling */
5047 dwc2_writel(dr->dctl | DCTL_RMTWKUPSIG, hsotg->regs + DCTL);
5048 } else {
5049 udelay(50);
5050 /* Set Device programming done bit */
5051 dctl = dwc2_readl(hsotg->regs + DCTL);
5052 dctl |= DCTL_PWRONPRGDONE;
5053 dwc2_writel(dctl, hsotg->regs + DCTL);
5054 }
5055 /* Wait for interrupts which must be cleared */
5056 mdelay(2);
5057 /* Clear all pending interupts */
5058 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5059
5060 /* Restore global registers */
5061 ret = dwc2_restore_global_registers(hsotg);
5062 if (ret) {
5063 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5064 __func__);
5065 return ret;
5066 }
5067
5068 /* Restore device registers */
5069 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5070 if (ret) {
5071 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5072 __func__);
5073 return ret;
5074 }
5075
5076 if (rem_wakeup) {
5077 mdelay(10);
5078 dctl = dwc2_readl(hsotg->regs + DCTL);
5079 dctl &= ~DCTL_RMTWKUPSIG;
5080 dwc2_writel(dctl, hsotg->regs + DCTL);
5081 }
5082
5083 hsotg->hibernated = 0;
5084 hsotg->lx_state = DWC2_L0;
5085 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5086
5087 return ret;
5088}