blob: 37ee467abea989ac414bf94a33aa2f93cdd9355f [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
Grigor Tovmasyanb43ebc92018-05-05 12:17:58 +04003239 if (hsotg->params.ipg_isoc_en)
3240 dcfg |= DCFG_IPG_ISOC_SUPPORDED;
3241
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003242 dwc2_writel(dcfg, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003243
3244 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003245 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003246
3247 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003248 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003249 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003250 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003251 GINTSTS_USBRST | GINTSTS_RESETDET |
3252 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003253 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3254 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003255
3256 if (!using_desc_dma(hsotg))
3257 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003258
John Youn95832c02017-01-23 14:57:26 -08003259 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003260 intmsk |= GINTSTS_CONIDSTSCHNG;
3261
3262 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003263
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003264 if (using_dma(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003265 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003266 hsotg->params.ahbcfg,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003267 hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003268
3269 /* Set DDMA mode support in the core if needed */
3270 if (using_desc_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003271 dwc2_set_bit(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003272
3273 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003274 dwc2_writel(((hsotg->dedicated_fifos) ?
3275 (GAHBCFG_NP_TXF_EMP_LVL |
3276 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3277 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003278 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003279
3280 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003281 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3282 * when we have no data to transfer. Otherwise we get being flooded by
3283 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003284 */
3285
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003286 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003287 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003288 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003289 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003290 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003291
3292 /*
3293 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003294 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003295 */
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003296 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3297 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003298 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003299 DOEPMSK_SETUPMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003300 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003301
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003302 /* Enable BNA interrupt for DDMA */
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003303 if (using_desc_dma(hsotg)) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003304 dwc2_set_bit(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
Minas Harutyunyan37981e02018-05-03 17:25:37 +04003305 dwc2_set_bit(hsotg->regs + DIEPMSK, DIEPMSK_BNAININTRMSK);
3306 }
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003307
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003308 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003309
3310 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003311 dwc2_readl(hsotg->regs + DIEPCTL0),
3312 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003313
3314 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003315 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003316
3317 /*
3318 * Enable the RXFIFO when in slave mode, as this is how we collect
3319 * the data. In DMA mode, we get events from the FIFO but also
3320 * things we cannot process, so do not use it.
3321 */
3322 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003323 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003324
3325 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003326 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3327 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003328
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003329 if (!is_usb_reset) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003330 dwc2_set_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003331 udelay(10); /* see openiboot */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003332 dwc2_clear_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003333 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003334
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003335 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003336
3337 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003338 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003339 * writing to the EPCTL register..
3340 */
3341
3342 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003343 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003344 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003345
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003346 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003347 DXEPCTL_CNAK | DXEPCTL_EPENA |
3348 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003349 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003350
3351 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003352 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003353 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003354
Lukasz Majewski308d7342012-05-04 14:17:05 +02003355 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003356 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3357 if (!is_usb_reset)
3358 val |= DCTL_SFTDISCON;
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003359 dwc2_set_bit(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003360
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003361 /* configure the core to support LPM */
3362 dwc2_gadget_init_lpm(hsotg);
3363
Lukasz Majewski308d7342012-05-04 14:17:05 +02003364 /* must be at-least 3ms to allow bus to see disconnect */
3365 mdelay(3);
3366
Gregory Herrero065d3932015-09-22 15:16:54 +02003367 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003368
3369 dwc2_hsotg_enqueue_setup(hsotg);
3370
3371 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3372 dwc2_readl(hsotg->regs + DIEPCTL0),
3373 dwc2_readl(hsotg->regs + DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003374}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003375
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003376static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003377{
3378 /* set the soft-disconnect bit */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003379 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003380}
3381
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003382void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003383{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003384 /* remove the soft-disconnect and let's go */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003385 dwc2_clear_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003386}
3387
3388/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003389 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3390 * @hsotg: The device state:
3391 *
3392 * This interrupt indicates one of the following conditions occurred while
3393 * transmitting an ISOC transaction.
3394 * - Corrupted IN Token for ISOC EP.
3395 * - Packet not complete in FIFO.
3396 *
3397 * The following actions will be taken:
3398 * - Determine the EP
3399 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3400 */
3401static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3402{
3403 struct dwc2_hsotg_ep *hs_ep;
3404 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003405 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003406 u32 idx;
3407
3408 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3409
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003410 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3411
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003412 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003413 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003414 /* Proceed only unmasked ISOC EPs */
3415 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3416 continue;
3417
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003418 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003419 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003420 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3421 epctrl |= DXEPCTL_SNAK;
3422 epctrl |= DXEPCTL_EPDIS;
3423 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3424 }
3425 }
3426
3427 /* Clear interrupt */
3428 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3429}
3430
3431/**
3432 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3433 * @hsotg: The device state:
3434 *
3435 * This interrupt indicates one of the following conditions occurred while
3436 * transmitting an ISOC transaction.
3437 * - Corrupted OUT Token for ISOC EP.
3438 * - Packet not complete in FIFO.
3439 *
3440 * The following actions will be taken:
3441 * - Determine the EP
3442 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3443 */
3444static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3445{
3446 u32 gintsts;
3447 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003448 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003449 u32 epctrl;
3450 struct dwc2_hsotg_ep *hs_ep;
3451 int idx;
3452
3453 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3454
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003455 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3456 daintmsk >>= DAINT_OUTEP_SHIFT;
3457
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003458 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003459 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003460 /* Proceed only unmasked ISOC EPs */
3461 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3462 continue;
3463
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003464 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003465 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003466 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3467 /* Unmask GOUTNAKEFF interrupt */
3468 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3469 gintmsk |= GINTSTS_GOUTNAKEFF;
3470 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3471
3472 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003473 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003474 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003475 break;
3476 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003477 }
3478 }
3479
3480 /* Clear interrupt */
3481 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3482}
3483
3484/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003485 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003486 * @irq: The IRQ number triggered
3487 * @pw: The pw value when registered the handler.
3488 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003489static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003490{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003491 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003492 int retry_count = 8;
3493 u32 gintsts;
3494 u32 gintmsk;
3495
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003496 if (!dwc2_is_device_mode(hsotg))
3497 return IRQ_NONE;
3498
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003499 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003500irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003501 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3502 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003503
3504 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3505 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3506
3507 gintsts &= gintmsk;
3508
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003509 if (gintsts & GINTSTS_RESETDET) {
3510 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3511
3512 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3513
3514 /* This event must be used only if controller is suspended */
3515 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003516 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003517 hsotg->lx_state = DWC2_L0;
3518 }
3519 }
3520
3521 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003522 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3523 u32 connected = hsotg->connected;
3524
3525 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3526 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3527 dwc2_readl(hsotg->regs + GNPTXSTS));
3528
3529 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3530
3531 /* Report disconnection if it is not already done. */
3532 dwc2_hsotg_disconnect(hsotg);
3533
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003534 /* Reset device address to zero */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003535 dwc2_clear_bit(hsotg->regs + DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003536
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003537 if (usb_status & GOTGCTL_BSESVLD && connected)
3538 dwc2_hsotg_core_init_disconnected(hsotg, true);
3539 }
3540
Dinh Nguyen47a16852014-04-14 14:13:34 -07003541 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003542 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003543
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003544 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003545 }
3546
Dinh Nguyen47a16852014-04-14 14:13:34 -07003547 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003548 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3549 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003550 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003551 int ep;
3552
Robert Baldyga7e804652013-09-19 11:50:20 +02003553 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003554 daint_out = daint >> DAINT_OUTEP_SHIFT;
3555 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003556
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003557 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3558
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003559 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3560 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003561 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003562 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003563 }
3564
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003565 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3566 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003567 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003568 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003569 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003570 }
3571
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003572 /* check both FIFOs */
3573
Dinh Nguyen47a16852014-04-14 14:13:34 -07003574 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003575 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3576
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003577 /*
3578 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003579 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003580 * it needs re-enabling
3581 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003582
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003583 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3584 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003585 }
3586
Dinh Nguyen47a16852014-04-14 14:13:34 -07003587 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003588 dev_dbg(hsotg->dev, "PTxFEmp\n");
3589
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003590 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003591
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003592 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3593 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003594 }
3595
Dinh Nguyen47a16852014-04-14 14:13:34 -07003596 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003597 /*
3598 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003599 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003600 * set.
3601 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003602
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003603 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003604 }
3605
Dinh Nguyen47a16852014-04-14 14:13:34 -07003606 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003607 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003608 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003609 }
3610
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003611 /*
3612 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003613 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003614 * the occurrence.
3615 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003616
Dinh Nguyen47a16852014-04-14 14:13:34 -07003617 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003618 u8 idx;
3619 u32 epctrl;
3620 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003621 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003622 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003623
Razmik Karapetyand8484552018-01-19 14:41:42 +04003624 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3625 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003626 /* Mask this interrupt */
3627 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3628 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3629 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003630
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003631 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
Artur Petrosyand5d5f072018-05-05 04:30:16 -04003632 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003633 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003634 /* Proceed only unmasked ISOC EPs */
3635 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3636 continue;
3637
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003638 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3639
Razmik Karapetyand8484552018-01-19 14:41:42 +04003640 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003641 epctrl |= DXEPCTL_SNAK;
3642 epctrl |= DXEPCTL_EPDIS;
3643 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3644 }
3645 }
3646
3647 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003648 }
3649
Dinh Nguyen47a16852014-04-14 14:13:34 -07003650 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003651 dev_info(hsotg->dev, "GINNakEff triggered\n");
3652
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003653 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003654
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003655 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003656 }
3657
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003658 if (gintsts & GINTSTS_INCOMPL_SOIN)
3659 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003660
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003661 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3662 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003663
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003664 /*
3665 * if we've had fifo events, we should try and go around the
3666 * loop again to see if there's any point in returning yet.
3667 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003668
3669 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003670 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003671
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003672 spin_unlock(&hsotg->lock);
3673
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003674 return IRQ_HANDLED;
3675}
3676
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003677static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3678 struct dwc2_hsotg_ep *hs_ep)
3679{
3680 u32 epctrl_reg;
3681 u32 epint_reg;
3682
3683 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3684 DOEPCTL(hs_ep->index);
3685 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3686 DOEPINT(hs_ep->index);
3687
3688 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3689 hs_ep->name);
3690
3691 if (hs_ep->dir_in) {
3692 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003693 dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003694 /* Wait for Nak effect */
3695 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3696 DXEPINT_INEPNAKEFF, 100))
3697 dev_warn(hsotg->dev,
3698 "%s: timeout DIEPINT.NAKEFF\n",
3699 __func__);
3700 } else {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003701 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003702 /* Wait for Nak effect */
3703 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3704 GINTSTS_GINNAKEFF, 100))
3705 dev_warn(hsotg->dev,
3706 "%s: timeout GINTSTS.GINNAKEFF\n",
3707 __func__);
3708 }
3709 } else {
3710 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003711 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003712
3713 /* Wait for global nak to take effect */
3714 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3715 GINTSTS_GOUTNAKEFF, 100))
3716 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3717 __func__);
3718 }
3719
3720 /* Disable ep */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003721 dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003722
3723 /* Wait for ep to be disabled */
3724 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3725 dev_warn(hsotg->dev,
3726 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3727
3728 /* Clear EPDISBLD interrupt */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003729 dwc2_set_bit(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003730
3731 if (hs_ep->dir_in) {
3732 unsigned short fifo_index;
3733
3734 if (hsotg->dedicated_fifos || hs_ep->periodic)
3735 fifo_index = hs_ep->fifo_index;
3736 else
3737 fifo_index = 0;
3738
3739 /* Flush TX FIFO */
3740 dwc2_flush_tx_fifo(hsotg, fifo_index);
3741
3742 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3743 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003744 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003745
3746 } else {
3747 /* Remove global NAKs */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003748 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003749 }
3750}
3751
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003752/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003753 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003754 * @ep: The USB endpint to configure
3755 * @desc: The USB endpoint descriptor to configure with.
3756 *
3757 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003758 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003759static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003760 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003761{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003762 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003763 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003764 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003765 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003766 u32 epctrl_reg;
3767 u32 epctrl;
3768 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003769 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003770 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003771 unsigned int dir_in;
3772 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003773 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003774 unsigned char ep_type;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003775
3776 dev_dbg(hsotg->dev,
3777 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3778 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3779 desc->wMaxPacketSize, desc->bInterval);
3780
3781 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003782 if (index == 0) {
3783 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3784 return -EINVAL;
3785 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003786
3787 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3788 if (dir_in != hs_ep->dir_in) {
3789 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3790 return -EINVAL;
3791 }
3792
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003793 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003794 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003795 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003796
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003797 /* ISOC IN in DDMA supported bInterval up to 10 */
3798 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3799 dir_in && desc->bInterval > 10) {
3800 dev_err(hsotg->dev,
3801 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3802 return -EINVAL;
3803 }
3804
3805 /* High bandwidth ISOC OUT in DDMA not supported */
3806 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3807 !dir_in && mc > 1) {
3808 dev_err(hsotg->dev,
3809 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3810 return -EINVAL;
3811 }
3812
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003813 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003814
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003815 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003816 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003817
3818 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3819 __func__, epctrl, epctrl_reg);
3820
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003821 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003822 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3823 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003824 MAX_DMA_DESC_NUM_GENERIC *
3825 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003826 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003827 if (!hs_ep->desc_list) {
3828 ret = -ENOMEM;
3829 goto error2;
3830 }
3831 }
3832
Lukasz Majewski22258f42012-06-14 10:02:24 +02003833 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003834
Dinh Nguyen47a16852014-04-14 14:13:34 -07003835 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3836 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003837
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003838 /*
3839 * mark the endpoint as active, otherwise the core may ignore
3840 * transactions entirely for this endpoint
3841 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003842 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003843
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003844 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003845 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003846
3847 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003848 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003849 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003850 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003851 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003852
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003853 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003854 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003855 epctrl |= DXEPCTL_EPTYPE_ISO;
3856 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003857 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003858 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003859 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003860 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003861 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003862 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003863 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003864 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3865 mask |= DIEPMSK_NAKMSK;
3866 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3867 } else {
3868 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3869 mask |= DOEPMSK_OUTTKNEPDISMSK;
3870 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3871 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003872 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003873
3874 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003875 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003876 break;
3877
3878 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003879 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003880 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003881
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003882 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3883 hs_ep->interval = 1 << (desc->bInterval - 1);
3884
Dinh Nguyen47a16852014-04-14 14:13:34 -07003885 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003886 break;
3887
3888 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003889 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003890 break;
3891 }
3892
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003893 /*
3894 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003895 * a unique tx-fifo even if it is non-periodic.
3896 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003897 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003898 u32 fifo_index = 0;
3899 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08003900
3901 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003902 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08003903 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02003904 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003905 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08003906 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003907 if (val < size)
3908 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003909 /* Search for smallest acceptable fifo */
3910 if (val < fifo_size) {
3911 fifo_size = val;
3912 fifo_index = i;
3913 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003914 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003915 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003916 dev_err(hsotg->dev,
3917 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303918 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003919 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303920 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003921 hsotg->fifo_map |= 1 << fifo_index;
3922 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3923 hs_ep->fifo_index = fifo_index;
3924 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003925 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003926
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003927 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003928 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003929 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003930
3931 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3932 __func__, epctrl);
3933
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003934 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003935 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003936 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003937
3938 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003939 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003940
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003941error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003942 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003943
3944error2:
3945 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003946 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003947 sizeof(struct dwc2_dma_desc),
3948 hs_ep->desc_list, hs_ep->desc_list_dma);
3949 hs_ep->desc_list = NULL;
3950 }
3951
Julia Lawall19c190f2010-03-29 17:36:44 +02003952 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003953}
3954
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003955/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003956 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003957 * @ep: The endpoint to disable.
3958 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003959static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003960{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003961 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003962 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003963 int dir_in = hs_ep->dir_in;
3964 int index = hs_ep->index;
3965 unsigned long flags;
3966 u32 epctrl_reg;
3967 u32 ctrl;
3968
Marek Szyprowski1e011292014-09-09 10:44:54 +02003969 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003970
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003971 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003972 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3973 return -EINVAL;
3974 }
3975
John Stultz9b4810922017-10-23 14:32:49 -07003976 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3977 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
3978 return -EINVAL;
3979 }
3980
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003981 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003982
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003983 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003984
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003985 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003986
3987 if (ctrl & DXEPCTL_EPENA)
3988 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
3989
Dinh Nguyen47a16852014-04-14 14:13:34 -07003990 ctrl &= ~DXEPCTL_EPENA;
3991 ctrl &= ~DXEPCTL_USBACTEP;
3992 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003993
3994 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003995 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003996
3997 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003998 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003999
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01004000 /* terminate all requests with shutdown */
4001 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
4002
Robert Baldyga1c07b202016-08-29 13:39:00 -07004003 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
4004 hs_ep->fifo_index = 0;
4005 hs_ep->fifo_size = 0;
4006
Lukasz Majewski22258f42012-06-14 10:02:24 +02004007 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004008 return 0;
4009}
4010
4011/**
4012 * on_list - check request is on the given endpoint
4013 * @ep: The endpoint to check.
4014 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004015 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004016static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004017{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004018 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004019
4020 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4021 if (req == test)
4022 return true;
4023 }
4024
4025 return false;
4026}
4027
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004028/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004029 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004030 * @ep: The endpoint to dequeue.
4031 * @req: The request to be removed from a queue.
4032 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004033static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004034{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004035 struct dwc2_hsotg_req *hs_req = our_req(req);
4036 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004037 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004038 unsigned long flags;
4039
Marek Szyprowski1e011292014-09-09 10:44:54 +02004040 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004041
Lukasz Majewski22258f42012-06-14 10:02:24 +02004042 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004043
4044 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004045 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004046 return -EINVAL;
4047 }
4048
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004049 /* Dequeue already started request */
4050 if (req == &hs_ep->req->req)
4051 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4052
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004053 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004054 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004055
4056 return 0;
4057}
4058
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004059/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004060 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004061 * @ep: The endpoint to set halt.
4062 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004063 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4064 * the endpoint is busy processing requests.
4065 *
4066 * We need to stall the endpoint immediately if request comes from set_feature
4067 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004068 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004069static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004070{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004071 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004072 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004073 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004074 u32 epreg;
4075 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004076 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004077
4078 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4079
Robert Baldygac9f721b2014-01-14 08:36:00 +01004080 if (index == 0) {
4081 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004082 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004083 else
4084 dev_warn(hs->dev,
4085 "%s: can't clear halt on ep0\n", __func__);
4086 return 0;
4087 }
4088
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004089 if (hs_ep->isochronous) {
4090 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4091 return -EINVAL;
4092 }
4093
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004094 if (!now && value && !list_empty(&hs_ep->queue)) {
4095 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4096 ep->name);
4097 return -EAGAIN;
4098 }
4099
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004100 if (hs_ep->dir_in) {
4101 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004102 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004103
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004104 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004105 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004106 if (epctl & DXEPCTL_EPENA)
4107 epctl |= DXEPCTL_EPDIS;
4108 } else {
4109 epctl &= ~DXEPCTL_STALL;
4110 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4111 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004112 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004113 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004114 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004115 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004116 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004117 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004118 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004119
John Youn34c0887f2017-01-17 20:31:43 -08004120 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004121 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004122 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004123 epctl &= ~DXEPCTL_STALL;
4124 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4125 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004126 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004127 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004128 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004129 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004130 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004131
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004132 hs_ep->halted = value;
4133
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004134 return 0;
4135}
4136
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004137/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004138 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004139 * @ep: The endpoint to set halt.
4140 * @value: Set or unset the halt.
4141 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004142static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004143{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004144 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004145 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004146 unsigned long flags = 0;
4147 int ret = 0;
4148
4149 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004150 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004151 spin_unlock_irqrestore(&hs->lock, flags);
4152
4153 return ret;
4154}
4155
Bhumika Goyalebce5612017-08-12 17:34:55 +05304156static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004157 .enable = dwc2_hsotg_ep_enable,
4158 .disable = dwc2_hsotg_ep_disable,
4159 .alloc_request = dwc2_hsotg_ep_alloc_request,
4160 .free_request = dwc2_hsotg_ep_free_request,
4161 .queue = dwc2_hsotg_ep_queue_lock,
4162 .dequeue = dwc2_hsotg_ep_dequeue,
4163 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004164 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004165};
4166
4167/**
John Youn9da51972017-01-17 20:30:27 -08004168 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004169 * @hsotg: The driver state
4170 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004171static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004172{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004173 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004174 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004175 /* unmask subset of endpoint interrupts */
4176
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004177 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4178 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4179 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004180
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004181 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4182 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4183 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004184
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004185 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004186
4187 /* Be in disconnected state until gadget is registered */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04004188 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004189
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004190 /* setup fifos */
4191
4192 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004193 dwc2_readl(hsotg->regs + GRXFSIZ),
4194 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004195
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004196 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004197
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004198 /* keep other bits untouched (so e.g. forced modes are not lost) */
4199 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4200 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01004201 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004202
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004203 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004204 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004205 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4206 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4207 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004208
Gregory Herrerof5090042015-01-09 13:38:47 +01004209 if (using_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04004210 dwc2_set_bit(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004211}
4212
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004213/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004214 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004215 * @gadget: The usb gadget state
4216 * @driver: The usb gadget driver
4217 *
4218 * Perform initialization to prepare udc device and driver
4219 * to work.
4220 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004221static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004222 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004223{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004224 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004225 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004226 int ret;
4227
4228 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004229 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004230 return -ENODEV;
4231 }
4232
4233 if (!driver) {
4234 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4235 return -EINVAL;
4236 }
4237
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004238 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004239 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004240
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004241 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004242 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4243 return -EINVAL;
4244 }
4245
4246 WARN_ON(hsotg->driver);
4247
4248 driver->driver.bus = NULL;
4249 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004250 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004251 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4252
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004253 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4254 ret = dwc2_lowlevel_hw_enable(hsotg);
4255 if (ret)
4256 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004257 }
4258
Gregory Herrerof6c01592015-01-09 13:38:41 +01004259 if (!IS_ERR_OR_NULL(hsotg->uphy))
4260 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004261
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004262 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004263 if (dwc2_hw_is_device(hsotg)) {
4264 dwc2_hsotg_init(hsotg);
4265 dwc2_hsotg_core_init_disconnected(hsotg, false);
4266 }
4267
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004268 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004269 spin_unlock_irqrestore(&hsotg->lock, flags);
4270
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004271 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004272
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004273 return 0;
4274
4275err:
4276 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004277 return ret;
4278}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004279
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004280/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004281 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004282 * @gadget: The usb gadget state
4283 * @driver: The usb gadget driver
4284 *
4285 * Stop udc hw block and stay tunned for future transmissions
4286 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004287static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004288{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004289 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004290 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004291 int ep;
4292
4293 if (!hsotg)
4294 return -ENODEV;
4295
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004296 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004297 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4298 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004299 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004300 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004301 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004302 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004303
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004304 spin_lock_irqsave(&hsotg->lock, flags);
4305
Marek Szyprowski32805c32014-10-20 12:45:33 +02004306 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004307 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004308 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004309
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004310 spin_unlock_irqrestore(&hsotg->lock, flags);
4311
Gregory Herrerof6c01592015-01-09 13:38:41 +01004312 if (!IS_ERR_OR_NULL(hsotg->uphy))
4313 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004314
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004315 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4316 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004317
4318 return 0;
4319}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004320
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004321/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004322 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004323 * @gadget: The usb gadget state
4324 *
4325 * Read the {micro} frame number
4326 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004327static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004328{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004329 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004330}
4331
Lukasz Majewskia188b682012-06-22 09:29:56 +02004332/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004333 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004334 * @gadget: The usb gadget state
4335 * @is_on: Current state of the USB PHY
4336 *
4337 * Connect/Disconnect the USB PHY pullup
4338 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004339static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004340{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004341 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004342 unsigned long flags = 0;
4343
Gregory Herrero77ba9112015-09-29 12:08:19 +02004344 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004345 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004346
4347 /* Don't modify pullup state while in host mode */
4348 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4349 hsotg->enabled = is_on;
4350 return 0;
4351 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004352
4353 spin_lock_irqsave(&hsotg->lock, flags);
4354 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004355 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004356 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004357 /* Enable ACG feature in device mode,if supported */
4358 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004359 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004360 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004361 dwc2_hsotg_core_disconnect(hsotg);
4362 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004363 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004364 }
4365
4366 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4367 spin_unlock_irqrestore(&hsotg->lock, flags);
4368
4369 return 0;
4370}
4371
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004372static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004373{
4374 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4375 unsigned long flags;
4376
4377 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4378 spin_lock_irqsave(&hsotg->lock, flags);
4379
Gregory Herrero61f72232015-09-29 12:08:28 +02004380 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004381 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004382 * before being initialized / de-initialized
4383 */
4384 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004385 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004386
Gregory Herrero83d98222015-01-09 13:39:02 +01004387 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004388 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004389
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004390 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004391 if (hsotg->enabled) {
4392 /* Enable ACG feature in device mode,if supported */
4393 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004394 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004395 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004396 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004397 dwc2_hsotg_core_disconnect(hsotg);
4398 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004399 }
4400
4401 spin_unlock_irqrestore(&hsotg->lock, flags);
4402 return 0;
4403}
4404
Gregory Herrero596d6962015-01-09 13:39:08 +01004405/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004406 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004407 * @gadget: The usb gadget state
4408 * @mA: Amount of current
4409 *
4410 * Report how much power the device may consume to the phy.
4411 */
John Youn9da51972017-01-17 20:30:27 -08004412static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004413{
4414 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4415
4416 if (IS_ERR_OR_NULL(hsotg->uphy))
4417 return -ENOTSUPP;
4418 return usb_phy_set_power(hsotg->uphy, mA);
4419}
4420
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004421static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4422 .get_frame = dwc2_hsotg_gadget_getframe,
4423 .udc_start = dwc2_hsotg_udc_start,
4424 .udc_stop = dwc2_hsotg_udc_stop,
4425 .pullup = dwc2_hsotg_pullup,
4426 .vbus_session = dwc2_hsotg_vbus_session,
4427 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004428};
4429
4430/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004431 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004432 * @hsotg: The device state.
4433 * @hs_ep: The endpoint to be initialised.
4434 * @epnum: The endpoint number
4435 *
4436 * Initialise the given endpoint (as part of the probe and device state
4437 * creation) to give to the gadget driver. Setup the endpoint name, any
4438 * direction information and other state that may be required.
4439 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004440static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004441 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004442 int epnum,
4443 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004444{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004445 char *dir;
4446
4447 if (epnum == 0)
4448 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004449 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004450 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004451 else
4452 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004453
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004454 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004455 hs_ep->index = epnum;
4456
4457 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4458
4459 INIT_LIST_HEAD(&hs_ep->queue);
4460 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4461
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004462 /* add to the list of endpoints known by the gadget driver */
4463 if (epnum)
4464 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4465
4466 hs_ep->parent = hsotg;
4467 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004468
4469 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4470 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4471 else
4472 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4473 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004474 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004475
Robert Baldyga29545222015-07-31 16:00:18 +02004476 if (epnum == 0) {
4477 hs_ep->ep.caps.type_control = true;
4478 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004479 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4480 hs_ep->ep.caps.type_iso = true;
4481 hs_ep->ep.caps.type_bulk = true;
4482 }
Robert Baldyga29545222015-07-31 16:00:18 +02004483 hs_ep->ep.caps.type_int = true;
4484 }
4485
4486 if (dir_in)
4487 hs_ep->ep.caps.dir_in = true;
4488 else
4489 hs_ep->ep.caps.dir_out = true;
4490
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004491 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004492 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004493 * to be something valid.
4494 */
4495
4496 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004497 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004498
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004499 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004500 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004501 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004502 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004503 }
4504}
4505
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004506/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004507 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004508 * @param: The device state
4509 *
4510 * Read the USB core HW configuration registers
4511 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004512static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004513{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004514 u32 cfg;
4515 u32 ep_type;
4516 u32 i;
4517
Ben Dooks10aebc72010-07-19 09:40:44 +01004518 /* check hardware configuration */
4519
John Youn43e90342015-12-17 11:17:45 -08004520 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4521
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004522 /* Add ep0 */
4523 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004524
John Younb98866c2017-01-17 20:31:58 -08004525 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4526 sizeof(struct dwc2_hsotg_ep),
4527 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004528 if (!hsotg->eps_in[0])
4529 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004530 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004531 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004532
John Youn43e90342015-12-17 11:17:45 -08004533 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004534 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004535 ep_type = cfg & 3;
4536 /* Direction in or both */
4537 if (!(ep_type & 2)) {
4538 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004539 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004540 if (!hsotg->eps_in[i])
4541 return -ENOMEM;
4542 }
4543 /* Direction out or both */
4544 if (!(ep_type & 1)) {
4545 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004546 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004547 if (!hsotg->eps_out[i])
4548 return -ENOMEM;
4549 }
4550 }
4551
John Youn43e90342015-12-17 11:17:45 -08004552 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4553 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004554
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004555 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4556 hsotg->num_of_eps,
4557 hsotg->dedicated_fifos ? "dedicated" : "shared",
4558 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004559 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004560}
4561
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004562/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004563 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004564 * @param: The device state
4565 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004566static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004567{
Mark Brown83a01802011-06-01 17:16:15 +01004568#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004569 struct device *dev = hsotg->dev;
4570 void __iomem *regs = hsotg->regs;
4571 u32 val;
4572 int idx;
4573
4574 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004575 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4576 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004577
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004578 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004579 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004580
4581 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004582 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004583
4584 /* show periodic fifo settings */
4585
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004586 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004587 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004588 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004589 val >> FIFOSIZE_DEPTH_SHIFT,
4590 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004591 }
4592
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004593 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004594 dev_info(dev,
4595 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004596 dwc2_readl(regs + DIEPCTL(idx)),
4597 dwc2_readl(regs + DIEPTSIZ(idx)),
4598 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004599
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004600 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004601 dev_info(dev,
4602 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004603 idx, dwc2_readl(regs + DOEPCTL(idx)),
4604 dwc2_readl(regs + DOEPTSIZ(idx)),
4605 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004606 }
4607
4608 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004609 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004610#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004611}
4612
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004613/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004614 * dwc2_gadget_init - init function for gadget
4615 * @dwc2: The data structure for the DWC2 driver.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004616 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004617int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004618{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004619 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004620 int epnum;
4621 int ret;
John Youn43e90342015-12-17 11:17:45 -08004622
Gregory Herrero0a176272015-01-09 13:38:52 +01004623 /* Dump fifo information */
4624 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004625 hsotg->params.g_np_tx_fifo_size);
4626 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004627
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004628 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004629 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004630 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004631 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004632
4633 if (hsotg->params.lpm)
4634 hsotg->gadget.lpm_capable = true;
4635
Gregory Herrero097ee662015-04-29 22:09:10 +02004636 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4637 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004638 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4639 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004640
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004641 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004642 if (ret) {
4643 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004644 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004645 }
4646
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004647 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4648 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004649 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004650 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004651
4652 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4653 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004654 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004655 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004656
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004657 if (using_desc_dma(hsotg)) {
4658 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4659 if (ret < 0)
4660 return ret;
4661 }
4662
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004663 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4664 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004665 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004666 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004667 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004668 }
4669
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004670 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4671
4672 if (hsotg->num_of_eps == 0) {
4673 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004674 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004675 }
4676
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004677 /* setup endpoint information */
4678
4679 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004680 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004681
4682 /* allocate EP0 request */
4683
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004684 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004685 GFP_KERNEL);
4686 if (!hsotg->ctrl_req) {
4687 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004688 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004689 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004690
4691 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004692 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4693 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004694 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004695 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004696 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004697 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004698 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004699 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004700
Dinh Nguyen117777b2014-11-11 11:13:34 -06004701 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004702 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004703 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004704
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004705 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004706
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004707 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004708}
4709
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004710/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004711 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004712 * @pdev: The platform information for the driver
4713 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004714int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004715{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004716 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004717
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004718 return 0;
4719}
4720
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004721int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004722{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004723 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004724
Gregory Herrero9e779772015-04-29 22:09:07 +02004725 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004726 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004727
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004728 if (hsotg->driver) {
4729 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004730
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004731 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4732 hsotg->driver->driver.name);
4733
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004734 spin_lock_irqsave(&hsotg->lock, flags);
4735 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004736 dwc2_hsotg_core_disconnect(hsotg);
4737 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004738 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4739 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004740
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004741 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4742 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004743 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004744 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004745 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004746 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004747 }
4748
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004749 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004750}
4751
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004752int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004753{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004754 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004755
Gregory Herrero9e779772015-04-29 22:09:07 +02004756 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004757 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004758
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004759 if (hsotg->driver) {
4760 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4761 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004762
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004763 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004764 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004765 if (hsotg->enabled) {
4766 /* Enable ACG feature in device mode,if supported */
4767 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004768 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004769 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004770 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004771 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004772
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004773 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004774}
John Youn58e52ff6a2016-02-23 19:54:57 -08004775
4776/**
4777 * dwc2_backup_device_registers() - Backup controller device registers.
4778 * When suspending usb bus, registers needs to be backuped
4779 * if controller power is disabled once suspended.
4780 *
4781 * @hsotg: Programming view of the DWC_otg controller
4782 */
4783int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4784{
4785 struct dwc2_dregs_backup *dr;
4786 int i;
4787
4788 dev_dbg(hsotg->dev, "%s\n", __func__);
4789
4790 /* Backup dev regs */
4791 dr = &hsotg->dr_backup;
4792
4793 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4794 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4795 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4796 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4797 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4798
4799 for (i = 0; i < hsotg->num_of_eps; i++) {
4800 /* Backup IN EPs */
4801 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4802
4803 /* Ensure DATA PID is correctly configured */
4804 if (dr->diepctl[i] & DXEPCTL_DPID)
4805 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4806 else
4807 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4808
4809 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4810 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4811
4812 /* Backup OUT EPs */
4813 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4814
4815 /* Ensure DATA PID is correctly configured */
4816 if (dr->doepctl[i] & DXEPCTL_DPID)
4817 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4818 else
4819 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4820
4821 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4822 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
Vardan Mikayelyanaf7c2bd2018-02-16 14:07:33 +04004823 dr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004824 }
4825 dr->valid = true;
4826 return 0;
4827}
4828
4829/**
4830 * dwc2_restore_device_registers() - Restore controller device registers.
4831 * When resuming usb bus, device registers needs to be restored
4832 * if controller power were disabled.
4833 *
4834 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004835 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
4836 *
4837 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08004838 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004839int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08004840{
4841 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08004842 int i;
4843
4844 dev_dbg(hsotg->dev, "%s\n", __func__);
4845
4846 /* Restore dev regs */
4847 dr = &hsotg->dr_backup;
4848 if (!dr->valid) {
4849 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4850 __func__);
4851 return -EINVAL;
4852 }
4853 dr->valid = false;
4854
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004855 if (!remote_wakeup)
4856 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4857
John Youn58e52ff6a2016-02-23 19:54:57 -08004858 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4859 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4860 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4861
4862 for (i = 0; i < hsotg->num_of_eps; i++) {
4863 /* Restore IN EPs */
John Youn58e52ff6a2016-02-23 19:54:57 -08004864 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4865 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004866 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004867 /** WA for enabled EPx's IN in DDMA mode. On entering to
4868 * hibernation wrong value read and saved from DIEPDMAx,
4869 * as result BNA interrupt asserted on hibernation exit
4870 * by restoring from saved area.
4871 */
4872 if (hsotg->params.g_dma_desc &&
4873 (dr->diepctl[i] & DXEPCTL_EPENA))
4874 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
4875 dwc2_writel(dr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
4876 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4877 /* Restore OUT EPs */
4878 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4879 /* WA for enabled EPx's OUT in DDMA mode. On entering to
4880 * hibernation wrong value read and saved from DOEPDMAx,
4881 * as result BNA interrupt asserted on hibernation exit
4882 * by restoring from saved area.
4883 */
4884 if (hsotg->params.g_dma_desc &&
4885 (dr->doepctl[i] & DXEPCTL_EPENA))
4886 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
John Youn58e52ff6a2016-02-23 19:54:57 -08004887 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004888 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004889 }
4890
John Youn58e52ff6a2016-02-23 19:54:57 -08004891 return 0;
4892}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04004893
4894/**
4895 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
4896 *
4897 * @hsotg: Programming view of DWC_otg controller
4898 *
4899 */
4900void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
4901{
4902 u32 val;
4903
4904 if (!hsotg->params.lpm)
4905 return;
4906
4907 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
4908 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
4909 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
4910 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
4911 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
4912 dwc2_writel(val, hsotg->regs + GLPMCFG);
4913 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg->regs
4914 + GLPMCFG));
4915}
Vardan Mikayelyanc5c403dc2018-02-16 14:10:13 +04004916
4917/**
4918 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
4919 *
4920 * @hsotg: Programming view of the DWC_otg controller
4921 *
4922 * Return non-zero if failed to enter to hibernation.
4923 */
4924int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
4925{
4926 u32 gpwrdn;
4927 int ret = 0;
4928
4929 /* Change to L2(suspend) state */
4930 hsotg->lx_state = DWC2_L2;
4931 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
4932 ret = dwc2_backup_global_registers(hsotg);
4933 if (ret) {
4934 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
4935 __func__);
4936 return ret;
4937 }
4938 ret = dwc2_backup_device_registers(hsotg);
4939 if (ret) {
4940 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
4941 __func__);
4942 return ret;
4943 }
4944
4945 gpwrdn = GPWRDN_PWRDNRSTN;
4946 gpwrdn |= GPWRDN_PMUACTV;
4947 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4948 udelay(10);
4949
4950 /* Set flag to indicate that we are in hibernation */
4951 hsotg->hibernated = 1;
4952
4953 /* Enable interrupts from wake up logic */
4954 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4955 gpwrdn |= GPWRDN_PMUINTSEL;
4956 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4957 udelay(10);
4958
4959 /* Unmask device mode interrupts in GPWRDN */
4960 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4961 gpwrdn |= GPWRDN_RST_DET_MSK;
4962 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
4963 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
4964 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4965 udelay(10);
4966
4967 /* Enable Power Down Clamp */
4968 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4969 gpwrdn |= GPWRDN_PWRDNCLMP;
4970 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4971 udelay(10);
4972
4973 /* Switch off VDD */
4974 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4975 gpwrdn |= GPWRDN_PWRDNSWTCH;
4976 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4977 udelay(10);
4978
4979 /* Save gpwrdn register for further usage if stschng interrupt */
4980 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4981 dev_dbg(hsotg->dev, "Hibernation completed\n");
4982
4983 return ret;
4984}
4985
4986/**
4987 * dwc2_gadget_exit_hibernation()
4988 * This function is for exiting from Device mode hibernation by host initiated
4989 * resume/reset and device initiated remote-wakeup.
4990 *
4991 * @hsotg: Programming view of the DWC_otg controller
4992 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
4993 * @param reset: indicates whether resume is initiated by Reset.
4994 *
4995 * Return non-zero if failed to exit from hibernation.
4996 */
4997int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
4998 int rem_wakeup, int reset)
4999{
5000 u32 pcgcctl;
5001 u32 gpwrdn;
5002 u32 dctl;
5003 int ret = 0;
5004 struct dwc2_gregs_backup *gr;
5005 struct dwc2_dregs_backup *dr;
5006
5007 gr = &hsotg->gr_backup;
5008 dr = &hsotg->dr_backup;
5009
5010 if (!hsotg->hibernated) {
5011 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5012 return 1;
5013 }
5014 dev_dbg(hsotg->dev,
5015 "%s: called with rem_wakeup = %d reset = %d\n",
5016 __func__, rem_wakeup, reset);
5017
5018 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5019
5020 if (!reset) {
5021 /* Clear all pending interupts */
5022 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5023 }
5024
5025 /* De-assert Restore */
5026 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5027 gpwrdn &= ~GPWRDN_RESTORE;
5028 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5029 udelay(10);
5030
5031 if (!rem_wakeup) {
5032 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
5033 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
5034 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
5035 }
5036
5037 /* Restore GUSBCFG, DCFG and DCTL */
5038 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
5039 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
5040 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
5041
5042 /* De-assert Wakeup Logic */
5043 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5044 gpwrdn &= ~GPWRDN_PMUACTV;
5045 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5046
5047 if (rem_wakeup) {
5048 udelay(10);
5049 /* Start Remote Wakeup Signaling */
5050 dwc2_writel(dr->dctl | DCTL_RMTWKUPSIG, hsotg->regs + DCTL);
5051 } else {
5052 udelay(50);
5053 /* Set Device programming done bit */
5054 dctl = dwc2_readl(hsotg->regs + DCTL);
5055 dctl |= DCTL_PWRONPRGDONE;
5056 dwc2_writel(dctl, hsotg->regs + DCTL);
5057 }
5058 /* Wait for interrupts which must be cleared */
5059 mdelay(2);
5060 /* Clear all pending interupts */
5061 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5062
5063 /* Restore global registers */
5064 ret = dwc2_restore_global_registers(hsotg);
5065 if (ret) {
5066 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5067 __func__);
5068 return ret;
5069 }
5070
5071 /* Restore device registers */
5072 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5073 if (ret) {
5074 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5075 __func__);
5076 return ret;
5077 }
5078
5079 if (rem_wakeup) {
5080 mdelay(10);
5081 dctl = dwc2_readl(hsotg->regs + DCTL);
5082 dctl &= ~DCTL_RMTWKUPSIG;
5083 dwc2_writel(dctl, hsotg->regs + DCTL);
5084 }
5085
5086 hsotg->hibernated = 0;
5087 hsotg->lx_state = DWC2_L0;
5088 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5089
5090 return ret;
5091}