blob: ec88f7e23f31b592972b64970901135ea4653e41 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: GPL-2.0
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002/**
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09003 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
4 * http://www.samsung.com
5 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01006 * Copyright 2008 Openmoko, Inc.
7 * Copyright 2008 Simtec Electronics
8 * Ben Dooks <ben@simtec.co.uk>
9 * http://armlinux.simtec.co.uk/
10 *
11 * S3C USB2.0 High-speed / OtG driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020012 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010013
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/spinlock.h>
17#include <linux/interrupt.h>
18#include <linux/platform_device.h>
19#include <linux/dma-mapping.h>
Marek Szyprowski7ad80962014-11-21 15:14:48 +010020#include <linux/mutex.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010021#include <linux/seq_file.h>
22#include <linux/delay.h>
23#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020025#include <linux/of_platform.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010026
27#include <linux/usb/ch9.h>
28#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053029#include <linux/usb/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010030
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070031#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060032#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010033
34/* conversion functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050035static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010036{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050037 return container_of(req, struct dwc2_hsotg_req, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010038}
39
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050040static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050042 return container_of(ep, struct dwc2_hsotg_ep, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010043}
44
Dinh Nguyen941fcce2014-11-11 11:13:33 -060045static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010046{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060047 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010048}
49
Razmik Karapetyanabd064a2018-01-19 14:42:08 +040050static inline void dwc2_set_bit(void __iomem *ptr, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010051{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030052 dwc2_writel(dwc2_readl(ptr) | val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010053}
54
Razmik Karapetyanabd064a2018-01-19 14:42:08 +040055static inline void dwc2_clear_bit(void __iomem *ptr, u32 val)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010056{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030057 dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010058}
59
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050060static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010061 u32 ep_index, u32 dir_in)
62{
63 if (dir_in)
64 return hsotg->eps_in[ep_index];
65 else
66 return hsotg->eps_out[ep_index];
67}
68
Mickael Maison997f4f82014-12-23 17:39:45 +010069/* forward declaration of functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050070static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010071
72/**
73 * using_dma - return the DMA status of the driver.
74 * @hsotg: The driver state.
75 *
76 * Return true if we're using DMA.
77 *
78 * Currently, we have the DMA support code worked into everywhere
79 * that needs it, but the AMBA DMA implementation in the hardware can
80 * only DMA from 32bit aligned addresses. This means that gadgets such
81 * as the CDC Ethernet cannot work as they often pass packets which are
82 * not 32bit aligned.
83 *
84 * Unfortunately the choice to use DMA or not is global to the controller
85 * and seems to be only settable when the controller is being put through
86 * a core reset. This means we either need to fix the gadgets to take
87 * account of DMA alignment, or add bounce buffers (yuerk).
88 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010089 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010090 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060091static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010092{
John Youn05ee7992016-11-03 17:56:05 -070093 return hsotg->params.g_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +010094}
95
Vahram Aharonyandec4b552016-11-09 19:27:48 -080096/*
97 * using_desc_dma - return the descriptor DMA status of the driver.
98 * @hsotg: The driver state.
99 *
100 * Return true if we're using descriptor DMA.
101 */
102static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
103{
104 return hsotg->params.g_dma_desc;
105}
106
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100107/**
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700108 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
109 * @hs_ep: The endpoint
110 * @increment: The value to increment by
111 *
112 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
113 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
114 */
115static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
116{
117 hs_ep->target_frame += hs_ep->interval;
118 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600119 hs_ep->frame_overrun = true;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700120 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
121 } else {
Gustavo A. R. Silvac1d5df62018-01-23 09:45:31 -0600122 hs_ep->frame_overrun = false;
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700123 }
124}
125
126/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500127 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100128 * @hsotg: The device state
129 * @ints: A bitmask of the interrupts to enable
130 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500131static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100132{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300133 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100134 u32 new_gsintmsk;
135
136 new_gsintmsk = gsintmsk | ints;
137
138 if (new_gsintmsk != gsintmsk) {
139 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300140 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100141 }
142}
143
144/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500145 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100146 * @hsotg: The device state
147 * @ints: A bitmask of the interrupts to enable
148 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500149static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100150{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300151 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100152 u32 new_gsintmsk;
153
154 new_gsintmsk = gsintmsk & ~ints;
155
156 if (new_gsintmsk != gsintmsk)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300157 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100158}
159
160/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500161 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100162 * @hsotg: The device state
163 * @ep: The endpoint index
164 * @dir_in: True if direction is in.
165 * @en: The enable value, true to enable
166 *
167 * Set or clear the mask for an individual endpoint's interrupt
168 * request.
169 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500170static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800171 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100172 unsigned int en)
173{
174 unsigned long flags;
175 u32 bit = 1 << ep;
176 u32 daint;
177
178 if (!dir_in)
179 bit <<= 16;
180
181 local_irq_save(flags);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300182 daint = dwc2_readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100183 if (en)
184 daint |= bit;
185 else
186 daint &= ~bit;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300187 dwc2_writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100188 local_irq_restore(flags);
189}
190
191/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800192 * dwc2_hsotg_tx_fifo_count - return count of TX FIFOs in device mode
193 */
194int dwc2_hsotg_tx_fifo_count(struct dwc2_hsotg *hsotg)
195{
196 if (hsotg->hw_params.en_multiple_tx_fifo)
197 /* In dedicated FIFO mode we need count of IN EPs */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400198 return hsotg->hw_params.num_dev_in_eps;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800199 else
200 /* In shared FIFO mode we need count of Periodic IN EPs */
201 return hsotg->hw_params.num_dev_perio_in_ep;
202}
203
204/**
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800205 * dwc2_hsotg_tx_fifo_total_depth - return total FIFO depth available for
206 * device mode TX FIFOs
207 */
208int dwc2_hsotg_tx_fifo_total_depth(struct dwc2_hsotg *hsotg)
209{
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800210 int addr;
211 int tx_addr_max;
212 u32 np_tx_fifo_size;
213
214 np_tx_fifo_size = min_t(u32, hsotg->hw_params.dev_nperio_tx_fifo_size,
215 hsotg->params.g_np_tx_fifo_size);
216
217 /* Get Endpoint Info Control block size in DWORDs. */
Minas Harutyunyan92730832017-11-30 12:16:37 +0400218 tx_addr_max = hsotg->hw_params.total_fifo_size;
Sevak Arakelyanc138ecf2017-01-23 15:01:23 -0800219
220 addr = hsotg->params.g_rx_fifo_size + np_tx_fifo_size;
221 if (tx_addr_max <= addr)
222 return 0;
223
224 return tx_addr_max - addr;
225}
226
227/**
228 * dwc2_hsotg_tx_fifo_average_depth - returns average depth of device mode
229 * TX FIFOs
230 */
231int dwc2_hsotg_tx_fifo_average_depth(struct dwc2_hsotg *hsotg)
232{
233 int tx_fifo_count;
234 int tx_fifo_depth;
235
236 tx_fifo_depth = dwc2_hsotg_tx_fifo_total_depth(hsotg);
237
238 tx_fifo_count = dwc2_hsotg_tx_fifo_count(hsotg);
239
240 if (!tx_fifo_count)
241 return tx_fifo_depth;
242 else
243 return tx_fifo_depth / tx_fifo_count;
244}
245
246/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500247 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100248 * @hsotg: The device instance.
249 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500250static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100251{
John Youn2317eac2016-10-17 17:36:23 -0700252 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100253 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100254 int timeout;
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +0400255
Ben Dooks0f002d22010-05-25 05:36:50 +0100256 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700257 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100258
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100259 /* Reset fifo map if not correctly cleared during previous session */
260 WARN_ON(hsotg->fifo_map);
261 hsotg->fifo_map = 0;
262
Gregory Herrero0a176272015-01-09 13:38:52 +0100263 /* set RX/NPTX FIFO sizes */
John Youn05ee7992016-11-03 17:56:05 -0700264 dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
265 dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
266 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
267 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100268
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200269 /*
270 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100271 * block have overlapping default addresses. This also ensures
272 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200273 * known values.
274 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100275
276 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700277 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100278
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200279 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100280 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200281 * them to endpoints dynamically according to maxpacket size value of
282 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200283 */
John Youn2317eac2016-10-17 17:36:23 -0700284 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700285 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700286 continue;
287 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700288 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
289 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700290 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700291 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100292
John Youn2317eac2016-10-17 17:36:23 -0700293 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
John Youn05ee7992016-11-03 17:56:05 -0700294 val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100295 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100296
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800297 dwc2_writel(hsotg->hw_params.total_fifo_size |
298 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
299 hsotg->regs + GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200300 /*
301 * according to p428 of the design guide, we need to ensure that
302 * all fifos are flushed before continuing
303 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100304
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300305 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700306 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100307
308 /* wait until the fifos are both flushed */
309 timeout = 100;
310 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300311 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100312
Dinh Nguyen47a16852014-04-14 14:13:34 -0700313 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100314 break;
315
316 if (--timeout == 0) {
317 dev_err(hsotg->dev,
318 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
319 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100320 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100321 }
322
323 udelay(1);
324 }
325
326 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100327}
328
329/**
330 * @ep: USB endpoint to allocate request for.
331 * @flags: Allocation flags
332 *
333 * Allocate a new USB request structure appropriate for the specified endpoint
334 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500335static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800336 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100337{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500338 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100339
John Younec33efe2017-01-17 20:32:41 -0800340 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100341 if (!req)
342 return NULL;
343
344 INIT_LIST_HEAD(&req->queue);
345
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100346 return &req->req;
347}
348
349/**
350 * is_ep_periodic - return true if the endpoint is in periodic mode.
351 * @hs_ep: The endpoint to query.
352 *
353 * Returns true if the endpoint is in periodic mode, meaning it is being
354 * used for an Interrupt or ISO transfer.
355 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500356static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100357{
358 return hs_ep->periodic;
359}
360
361/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500362 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100363 * @hsotg: The device state.
364 * @hs_ep: The endpoint for the request
365 * @hs_req: The request being processed.
366 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500367 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100368 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200369 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500370static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800371 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500372 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100373{
374 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800375
Jingoo Han17d966a2013-05-11 21:14:00 +0900376 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100377}
378
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800379/*
380 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
381 * for Control endpoint
382 * @hsotg: The device state.
383 *
384 * This function will allocate 4 descriptor chains for EP 0: 2 for
385 * Setup stage, per one for IN and OUT data/status transactions.
386 */
387static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
388{
389 hsotg->setup_desc[0] =
390 dmam_alloc_coherent(hsotg->dev,
391 sizeof(struct dwc2_dma_desc),
392 &hsotg->setup_desc_dma[0],
393 GFP_KERNEL);
394 if (!hsotg->setup_desc[0])
395 goto fail;
396
397 hsotg->setup_desc[1] =
398 dmam_alloc_coherent(hsotg->dev,
399 sizeof(struct dwc2_dma_desc),
400 &hsotg->setup_desc_dma[1],
401 GFP_KERNEL);
402 if (!hsotg->setup_desc[1])
403 goto fail;
404
405 hsotg->ctrl_in_desc =
406 dmam_alloc_coherent(hsotg->dev,
407 sizeof(struct dwc2_dma_desc),
408 &hsotg->ctrl_in_desc_dma,
409 GFP_KERNEL);
410 if (!hsotg->ctrl_in_desc)
411 goto fail;
412
413 hsotg->ctrl_out_desc =
414 dmam_alloc_coherent(hsotg->dev,
415 sizeof(struct dwc2_dma_desc),
416 &hsotg->ctrl_out_desc_dma,
417 GFP_KERNEL);
418 if (!hsotg->ctrl_out_desc)
419 goto fail;
420
421 return 0;
422
423fail:
424 return -ENOMEM;
425}
426
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100427/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500428 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100429 * @hsotg: The controller state.
430 * @hs_ep: The endpoint we're going to write for.
431 * @hs_req: The request to write data for.
432 *
433 * This is called when the TxFIFO has some space in it to hold a new
434 * transmission and we have something to give it. The actual setup of
435 * the data size is done elsewhere, so all we have to do is to actually
436 * write the data.
437 *
438 * The return value is zero if there is more space (or nothing was done)
439 * otherwise -ENOSPC is returned if the FIFO space was used up.
440 *
441 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200442 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500443static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800444 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500445 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100446{
447 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300448 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100449 int buf_pos = hs_req->req.actual;
450 int to_write = hs_ep->size_loaded;
451 void *data;
452 int can_write;
453 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200454 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100455
456 to_write -= (buf_pos - hs_ep->last_load);
457
458 /* if there's nothing to write, get out early */
459 if (to_write == 0)
460 return 0;
461
Ben Dooks10aebc72010-07-19 09:40:44 +0100462 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300463 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100464 int size_left;
465 int size_done;
466
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200467 /*
468 * work out how much data was loaded so we can calculate
469 * how much data is left in the fifo.
470 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100471
Dinh Nguyen47a16852014-04-14 14:13:34 -0700472 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100473
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200474 /*
475 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100476 * previous data has been completely sent.
477 */
478 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500479 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100480 return -ENOSPC;
481 }
482
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100483 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
484 __func__, size_left,
485 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
486
487 /* how much of the data has moved */
488 size_done = hs_ep->size_loaded - size_left;
489
490 /* how much data is left in the fifo */
491 can_write = hs_ep->fifo_load - size_done;
492 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
493 __func__, can_write);
494
495 can_write = hs_ep->fifo_size - can_write;
496 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
497 __func__, can_write);
498
499 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500500 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100501 return -ENOSPC;
502 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100503 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Robert Baldygaad674a12016-08-29 13:38:50 -0700504 can_write = dwc2_readl(hsotg->regs +
505 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100506
507 can_write &= 0xffff;
508 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100509 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700510 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100511 dev_dbg(hsotg->dev,
512 "%s: no queue slots available (0x%08x)\n",
513 __func__, gnptxsts);
514
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500515 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100516 return -ENOSPC;
517 }
518
Dinh Nguyen47a16852014-04-14 14:13:34 -0700519 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100520 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100521 }
522
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200523 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
524
525 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800526 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100527
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200528 /*
529 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100530 * FIFO, requests of >512 cause the endpoint to get stuck with a
531 * fragment of the end of the transfer in it.
532 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200533 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100534 can_write = 512;
535
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200536 /*
537 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100538 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200539 * doing it.
540 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200541 if (to_write > max_transfer) {
542 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100543
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200544 /* it's needed only when we do not use dedicated fifos */
545 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500546 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800547 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700548 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100549 }
550
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100551 /* see if we can write data */
552
553 if (to_write > can_write) {
554 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200555 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100556
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200557 /*
558 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100559 * exact number of packets.
560 *
561 * Note, we do not currently check to see if we can ever
562 * write a full packet or not to the FIFO.
563 */
564
565 if (pkt_round)
566 to_write -= pkt_round;
567
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200568 /*
569 * enable correct FIFO interrupt to alert us when there
570 * is more room left.
571 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100572
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200573 /* it's needed only when we do not use dedicated fifos */
574 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500575 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800576 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700577 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100578 }
579
580 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800581 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100582
583 if (to_write <= 0)
584 return -ENOSPC;
585
586 hs_req->req.actual = buf_pos + to_write;
587 hs_ep->total_data += to_write;
588
589 if (periodic)
590 hs_ep->fifo_load += to_write;
591
592 to_write = DIV_ROUND_UP(to_write, 4);
593 data = hs_req->req.buf + buf_pos;
594
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500595 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100596
597 return (to_write >= can_write) ? -ENOSPC : 0;
598}
599
600/**
601 * get_ep_limit - get the maximum data legnth for this endpoint
602 * @hs_ep: The endpoint
603 *
604 * Return the maximum data that can be queued in one go on a given endpoint
605 * so that transfers that are too long can be split.
606 */
John Youn9da51972017-01-17 20:30:27 -0800607static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100608{
609 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800610 unsigned int maxsize;
611 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100612
613 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700614 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
615 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100616 } else {
John Youn9da51972017-01-17 20:30:27 -0800617 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900618 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700619 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900620 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100621 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100622 }
623
624 /* we made the constant loading easier above by using +1 */
625 maxpkt--;
626 maxsize--;
627
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200628 /*
629 * constrain by packet count if maxpkts*pktsize is greater
630 * than the length register size.
631 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100632
633 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
634 maxsize = maxpkt * hs_ep->ep.maxpacket;
635
636 return maxsize;
637}
638
639/**
John Youn38beaec2017-01-17 20:31:13 -0800640 * dwc2_hsotg_read_frameno - read current frame number
641 * @hsotg: The device instance
642 *
643 * Return the current frame number
644 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700645static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
646{
647 u32 dsts;
648
649 dsts = dwc2_readl(hsotg->regs + DSTS);
650 dsts &= DSTS_SOFFN_MASK;
651 dsts >>= DSTS_SOFFN_SHIFT;
652
653 return dsts;
654}
655
656/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800657 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
658 * DMA descriptor chain prepared for specific endpoint
659 * @hs_ep: The endpoint
660 *
661 * Return the maximum data that can be queued in one go on a given endpoint
662 * depending on its descriptor chain capacity so that transfers that
663 * are too long can be split.
664 */
665static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
666{
667 int is_isoc = hs_ep->isochronous;
668 unsigned int maxsize;
669
670 if (is_isoc)
671 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
672 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
673 else
674 maxsize = DEV_DMA_NBYTES_LIMIT;
675
676 /* Above size of one descriptor was chosen, multiple it */
677 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
678
679 return maxsize;
680}
681
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800682/*
683 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
684 * @hs_ep: The endpoint
685 * @mask: RX/TX bytes mask to be defined
686 *
687 * Returns maximum data payload for one descriptor after analyzing endpoint
688 * characteristics.
689 * DMA descriptor transfer bytes limit depends on EP type:
690 * Control out - MPS,
691 * Isochronous - descriptor rx/tx bytes bitfield limit,
692 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
693 * have concatenations from various descriptors within one packet.
694 *
695 * Selects corresponding mask for RX/TX bytes as well.
696 */
697static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
698{
699 u32 mps = hs_ep->ep.maxpacket;
700 int dir_in = hs_ep->dir_in;
701 u32 desc_size = 0;
702
703 if (!hs_ep->index && !dir_in) {
704 desc_size = mps;
705 *mask = DEV_DMA_NBYTES_MASK;
706 } else if (hs_ep->isochronous) {
707 if (dir_in) {
708 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
709 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
710 } else {
711 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
712 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
713 }
714 } else {
715 desc_size = DEV_DMA_NBYTES_LIMIT;
716 *mask = DEV_DMA_NBYTES_MASK;
717
718 /* Round down desc_size to be mps multiple */
719 desc_size -= desc_size % mps;
720 }
721
722 return desc_size;
723}
724
725/*
726 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
727 * @hs_ep: The endpoint
728 * @dma_buff: DMA address to use
729 * @len: Length of the transfer
730 *
731 * This function will iterate over descriptor chain and fill its entries
732 * with corresponding information based on transfer data.
733 */
734static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
735 dma_addr_t dma_buff,
736 unsigned int len)
737{
738 struct dwc2_hsotg *hsotg = hs_ep->parent;
739 int dir_in = hs_ep->dir_in;
740 struct dwc2_dma_desc *desc = hs_ep->desc_list;
741 u32 mps = hs_ep->ep.maxpacket;
742 u32 maxsize = 0;
743 u32 offset = 0;
744 u32 mask = 0;
745 int i;
746
747 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
748
749 hs_ep->desc_count = (len / maxsize) +
750 ((len % maxsize) ? 1 : 0);
751 if (len == 0)
752 hs_ep->desc_count = 1;
753
754 for (i = 0; i < hs_ep->desc_count; ++i) {
755 desc->status = 0;
756 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
757 << DEV_DMA_BUFF_STS_SHIFT);
758
759 if (len > maxsize) {
760 if (!hs_ep->index && !dir_in)
761 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
762
763 desc->status |= (maxsize <<
764 DEV_DMA_NBYTES_SHIFT & mask);
765 desc->buf = dma_buff + offset;
766
767 len -= maxsize;
768 offset += maxsize;
769 } else {
770 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
771
772 if (dir_in)
773 desc->status |= (len % mps) ? DEV_DMA_SHORT :
774 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
775 if (len > maxsize)
776 dev_err(hsotg->dev, "wrong len %d\n", len);
777
778 desc->status |=
779 len << DEV_DMA_NBYTES_SHIFT & mask;
780 desc->buf = dma_buff + offset;
781 }
782
783 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
784 desc->status |= (DEV_DMA_BUFF_STS_HREADY
785 << DEV_DMA_BUFF_STS_SHIFT);
786 desc++;
787 }
788}
789
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800790/*
791 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
792 * @hs_ep: The isochronous endpoint.
793 * @dma_buff: usb requests dma buffer.
794 * @len: usb request transfer length.
795 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400796 * Fills next free descriptor with the data of the arrived usb request,
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800797 * frame info, sets Last and IOC bits increments next_desc. If filled
798 * descriptor is not the first one, removes L bit from the previous descriptor
799 * status.
800 */
801static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
802 dma_addr_t dma_buff, unsigned int len)
803{
804 struct dwc2_dma_desc *desc;
805 struct dwc2_hsotg *hsotg = hs_ep->parent;
806 u32 index;
807 u32 maxsize = 0;
808 u32 mask = 0;
809
810 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800811
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400812 index = hs_ep->next_desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800813 desc = &hs_ep->desc_list[index];
814
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400815 /* Check if descriptor chain full */
816 if ((desc->status >> DEV_DMA_BUFF_STS_SHIFT) ==
817 DEV_DMA_BUFF_STS_HREADY) {
818 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
819 return 1;
820 }
821
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800822 /* Clear L bit of previous desc if more than one entries in the chain */
823 if (hs_ep->next_desc)
824 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
825
826 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
827 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
828
829 desc->status = 0;
830 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
831
832 desc->buf = dma_buff;
833 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
834 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
835
836 if (hs_ep->dir_in) {
837 desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) &
838 DEV_DMA_ISOC_PID_MASK) |
839 ((len % hs_ep->ep.maxpacket) ?
840 DEV_DMA_SHORT : 0) |
841 ((hs_ep->target_frame <<
842 DEV_DMA_ISOC_FRNUM_SHIFT) &
843 DEV_DMA_ISOC_FRNUM_MASK);
844 }
845
846 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
847 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
848
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400849 /* Increment frame number by interval for IN */
850 if (hs_ep->dir_in)
851 dwc2_gadget_incr_frame_num(hs_ep);
852
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800853 /* Update index of last configured entry in the chain */
854 hs_ep->next_desc++;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400855 if (hs_ep->next_desc >= MAX_DMA_DESC_NUM_GENERIC)
856 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800857
858 return 0;
859}
860
861/*
862 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
863 * @hs_ep: The isochronous endpoint.
864 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400865 * Prepare descriptor chain for isochronous endpoints. Afterwards
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800866 * write DMA address to HW and enable the endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800867 */
868static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
869{
870 struct dwc2_hsotg *hsotg = hs_ep->parent;
871 struct dwc2_hsotg_req *hs_req, *treq;
872 int index = hs_ep->index;
873 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400874 int i;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800875 u32 dma_reg;
876 u32 depctl;
877 u32 ctrl;
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400878 struct dwc2_dma_desc *desc;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800879
880 if (list_empty(&hs_ep->queue)) {
881 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
882 return;
883 }
884
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400885 /* Initialize descriptor chain by Host Busy status */
886 for (i = 0; i < MAX_DMA_DESC_NUM_GENERIC; i++) {
887 desc = &hs_ep->desc_list[i];
888 desc->status = 0;
889 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
890 << DEV_DMA_BUFF_STS_SHIFT);
891 }
892
893 hs_ep->next_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800894 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
895 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
896 hs_req->req.length);
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400897 if (ret)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800898 break;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800899 }
900
Minas Harutyunyan729cac62018-05-03 17:24:28 +0400901 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800902 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
903 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
904
905 /* write descriptor chain address to control register */
906 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
907
908 ctrl = dwc2_readl(hsotg->regs + depctl);
909 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
910 dwc2_writel(ctrl, hsotg->regs + depctl);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800911}
912
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800913/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500914 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100915 * @hsotg: The controller state.
916 * @hs_ep: The endpoint to process a request for
917 * @hs_req: The request to start.
918 * @continuing: True if we are doing more for the current request.
919 *
920 * Start the given request running by setting the endpoint registers
921 * appropriately, and writing any data to the FIFOs.
922 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500923static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800924 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500925 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100926 bool continuing)
927{
928 struct usb_request *ureq = &hs_req->req;
929 int index = hs_ep->index;
930 int dir_in = hs_ep->dir_in;
931 u32 epctrl_reg;
932 u32 epsize_reg;
933 u32 epsize;
934 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -0800935 unsigned int length;
936 unsigned int packets;
937 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800938 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100939
940 if (index != 0) {
941 if (hs_ep->req && !continuing) {
942 dev_err(hsotg->dev, "%s: active request\n", __func__);
943 WARN_ON(1);
944 return;
945 } else if (hs_ep->req != hs_req && continuing) {
946 dev_err(hsotg->dev,
947 "%s: continue different req\n", __func__);
948 WARN_ON(1);
949 return;
950 }
951 }
952
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800953 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200954 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
955 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100956
957 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300958 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100959 hs_ep->dir_in ? "in" : "out");
960
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900961 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300962 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900963
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200964 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900965 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
966 return;
967 }
968
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100969 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200970 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
971 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100972
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800973 if (!using_desc_dma(hsotg))
974 maxreq = get_ep_limit(hs_ep);
975 else
976 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
977
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100978 if (length > maxreq) {
979 int round = maxreq % hs_ep->ep.maxpacket;
980
981 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
982 __func__, length, maxreq, round);
983
984 /* round down to multiple of packets */
985 if (round)
986 maxreq -= round;
987
988 length = maxreq;
989 }
990
991 if (length)
992 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
993 else
994 packets = 1; /* send one packet if length is zero. */
995
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200996 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
997 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
998 return;
999 }
1000
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001001 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001002 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07001003 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001004 else
Dinh Nguyen47a16852014-04-14 14:13:34 -07001005 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001006 else
1007 epsize = 0;
1008
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001009 /*
1010 * zero length packet should be programmed on its own and should not
1011 * be counted in DIEPTSIZ.PktCnt with other packets.
1012 */
1013 if (dir_in && ureq->zero && !continuing) {
1014 /* Test if zlp is actually required. */
1015 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -08001016 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001017 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001018 }
1019
Dinh Nguyen47a16852014-04-14 14:13:34 -07001020 epsize |= DXEPTSIZ_PKTCNT(packets);
1021 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001022
1023 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
1024 __func__, packets, length, ureq->length, epsize, epsize_reg);
1025
1026 /* store the request as the current one we're doing */
1027 hs_ep->req = hs_req;
1028
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001029 if (using_desc_dma(hsotg)) {
1030 u32 offset = 0;
1031 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001032
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001033 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
1034 if (!dir_in) {
1035 if (!index)
1036 length = mps;
1037 else if (length % mps)
1038 length += (mps - (length % mps));
1039 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001040
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001041 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001042 * If more data to send, adjust DMA for EP0 out data stage.
1043 * ureq->dma stays unchanged, hence increment it by already
1044 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001045 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001046 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1047 continuing)
1048 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001049
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001050 /* Fill DDMA chain entries */
1051 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1052 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001053
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001054 /* write descriptor chain address to control register */
1055 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
1056
1057 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1058 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1059 } else {
1060 /* write size / packets */
1061 dwc2_writel(epsize, hsotg->regs + epsize_reg);
1062
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001063 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001064 /*
1065 * write DMA address to control register, buffer
1066 * already synced by dwc2_hsotg_ep_queue().
1067 */
1068
1069 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
1070
1071 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1072 __func__, &ureq->dma, dma_reg);
1073 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001074 }
1075
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001076 if (hs_ep->isochronous && hs_ep->interval == 1) {
1077 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1078 dwc2_gadget_incr_frame_num(hs_ep);
1079
1080 if (hs_ep->target_frame & 0x1)
1081 ctrl |= DXEPCTL_SETODDFR;
1082 else
1083 ctrl |= DXEPCTL_SETEVENFR;
1084 }
1085
Dinh Nguyen47a16852014-04-14 14:13:34 -07001086 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001087
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001088 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001089
1090 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001091 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001092 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001093
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001094 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001095 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001096
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001097 /*
1098 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001099 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001100 * this information.
1101 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001102 hs_ep->size_loaded = length;
1103 hs_ep->last_load = ureq->actual;
1104
1105 if (dir_in && !using_dma(hsotg)) {
1106 /* set these anyway, we may need them for non-periodic in */
1107 hs_ep->fifo_load = 0;
1108
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001109 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001110 }
1111
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001112 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001113 * Note, trying to clear the NAK here causes problems with transmit
1114 * on the S3C6400 ending up with the TXFIFO becoming full.
1115 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001116
1117 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001118 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001119 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001120 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001121 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122
Dinh Nguyen47a16852014-04-14 14:13:34 -07001123 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001124 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001125
1126 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001127 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001128}
1129
1130/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001131 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001132 * @hsotg: The device state.
1133 * @hs_ep: The endpoint the request is on.
1134 * @req: The request being processed.
1135 *
1136 * We've been asked to queue a request, so ensure that the memory buffer
1137 * is correctly setup for DMA. If we've been passed an extant DMA address
1138 * then ensure the buffer has been synced to memory. If our buffer has no
1139 * DMA memory, then we map the memory and mark our request to allow us to
1140 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001141 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001142static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001143 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001144 struct usb_request *req)
1145{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001146 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001147
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001148 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1149 if (ret)
1150 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001151
1152 return 0;
1153
1154dma_error:
1155 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1156 __func__, req->buf, req->length);
1157
1158 return -EIO;
1159}
1160
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001161static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001162 struct dwc2_hsotg_ep *hs_ep,
1163 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001164{
1165 void *req_buf = hs_req->req.buf;
1166
1167 /* If dma is not being used or buffer is aligned */
1168 if (!using_dma(hsotg) || !((long)req_buf & 3))
1169 return 0;
1170
1171 WARN_ON(hs_req->saved_req_buf);
1172
1173 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001174 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001175
1176 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1177 if (!hs_req->req.buf) {
1178 hs_req->req.buf = req_buf;
1179 dev_err(hsotg->dev,
1180 "%s: unable to allocate memory for bounce buffer\n",
1181 __func__);
1182 return -ENOMEM;
1183 }
1184
1185 /* Save actual buffer */
1186 hs_req->saved_req_buf = req_buf;
1187
1188 if (hs_ep->dir_in)
1189 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1190 return 0;
1191}
1192
John Younb98866c2017-01-17 20:31:58 -08001193static void
1194dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1195 struct dwc2_hsotg_ep *hs_ep,
1196 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001197{
1198 /* If dma is not being used or buffer was aligned */
1199 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1200 return;
1201
1202 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1203 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1204
1205 /* Copy data from bounce buffer on successful out transfer */
1206 if (!hs_ep->dir_in && !hs_req->req.status)
1207 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001208 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001209
1210 /* Free bounce buffer */
1211 kfree(hs_req->req.buf);
1212
1213 hs_req->req.buf = hs_req->saved_req_buf;
1214 hs_req->saved_req_buf = NULL;
1215}
1216
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001217/**
1218 * dwc2_gadget_target_frame_elapsed - Checks target frame
1219 * @hs_ep: The driver endpoint to check
1220 *
1221 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1222 * corresponding transfer.
1223 */
1224static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1225{
1226 struct dwc2_hsotg *hsotg = hs_ep->parent;
1227 u32 target_frame = hs_ep->target_frame;
1228 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
1229 bool frame_overrun = hs_ep->frame_overrun;
1230
1231 if (!frame_overrun && current_frame >= target_frame)
1232 return true;
1233
1234 if (frame_overrun && current_frame >= target_frame &&
1235 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1236 return true;
1237
1238 return false;
1239}
1240
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001241/*
1242 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1243 * @hsotg: The driver state
1244 * @hs_ep: the ep descriptor chain is for
1245 *
1246 * Called to update EP0 structure's pointers depend on stage of
1247 * control transfer.
1248 */
1249static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1250 struct dwc2_hsotg_ep *hs_ep)
1251{
1252 switch (hsotg->ep0_state) {
1253 case DWC2_EP0_SETUP:
1254 case DWC2_EP0_STATUS_OUT:
1255 hs_ep->desc_list = hsotg->setup_desc[0];
1256 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1257 break;
1258 case DWC2_EP0_DATA_IN:
1259 case DWC2_EP0_STATUS_IN:
1260 hs_ep->desc_list = hsotg->ctrl_in_desc;
1261 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1262 break;
1263 case DWC2_EP0_DATA_OUT:
1264 hs_ep->desc_list = hsotg->ctrl_out_desc;
1265 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1266 break;
1267 default:
1268 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1269 hsotg->ep0_state);
1270 return -EINVAL;
1271 }
1272
1273 return 0;
1274}
1275
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001276static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001277 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001278{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001279 struct dwc2_hsotg_req *hs_req = our_req(req);
1280 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001281 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001282 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001283 int ret;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001284 u32 maxsize = 0;
1285 u32 mask = 0;
1286
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001287
1288 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1289 ep->name, req, req->length, req->buf, req->no_interrupt,
1290 req->zero, req->short_not_ok);
1291
Gregory Herrero7ababa92015-04-29 22:09:08 +02001292 /* Prevent new request submission when controller is suspended */
Grigor Tovmasyan88b02f22018-01-24 17:44:25 +04001293 if (hs->lx_state != DWC2_L0) {
1294 dev_dbg(hs->dev, "%s: submit request only in active state\n",
John Youn9da51972017-01-17 20:30:27 -08001295 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001296 return -EAGAIN;
1297 }
1298
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001299 /* initialise status of the request */
1300 INIT_LIST_HEAD(&hs_req->queue);
1301 req->actual = 0;
1302 req->status = -EINPROGRESS;
1303
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001304 /* In DDMA mode for ISOC's don't queue request if length greater
1305 * than descriptor limits.
1306 */
1307 if (using_desc_dma(hs) && hs_ep->isochronous) {
1308 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
1309 if (hs_ep->dir_in && req->length > maxsize) {
1310 dev_err(hs->dev, "wrong length %d (maxsize=%d)\n",
1311 req->length, maxsize);
1312 return -EINVAL;
1313 }
1314
1315 if (!hs_ep->dir_in && req->length > hs_ep->ep.maxpacket) {
1316 dev_err(hs->dev, "ISOC OUT: wrong length %d (mps=%d)\n",
1317 req->length, hs_ep->ep.maxpacket);
1318 return -EINVAL;
1319 }
1320 }
1321
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001322 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001323 if (ret)
1324 return ret;
1325
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001326 /* if we're using DMA, sync the buffers as necessary */
1327 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001328 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001329 if (ret)
1330 return ret;
1331 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001332 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1333 if (using_desc_dma(hs) && !hs_ep->index) {
1334 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1335 if (ret)
1336 return ret;
1337 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001338
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001339 first = list_empty(&hs_ep->queue);
1340 list_add_tail(&hs_req->queue, &hs_ep->queue);
1341
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001342 /*
1343 * Handle DDMA isochronous transfers separately - just add new entry
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001344 * to the descriptor chain.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001345 * Transfer will be started once SW gets either one of NAK or
1346 * OutTknEpDis interrupts.
1347 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04001348 if (using_desc_dma(hs) && hs_ep->isochronous) {
1349 if (hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1350 dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1351 hs_req->req.length);
1352 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001353 return 0;
1354 }
1355
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001356 if (first) {
1357 if (!hs_ep->isochronous) {
1358 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1359 return 0;
1360 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001361
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001362 while (dwc2_gadget_target_frame_elapsed(hs_ep))
1363 dwc2_gadget_incr_frame_num(hs_ep);
1364
1365 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1366 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1367 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001368 return 0;
1369}
1370
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001371static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001372 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001373{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001374 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001375 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001376 unsigned long flags = 0;
1377 int ret = 0;
1378
1379 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001380 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001381 spin_unlock_irqrestore(&hs->lock, flags);
1382
1383 return ret;
1384}
1385
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001386static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001387 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001388{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001389 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001390
1391 kfree(hs_req);
1392}
1393
1394/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001395 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001396 * @ep: The endpoint the request was on.
1397 * @req: The request completed.
1398 *
1399 * Called on completion of any requests the driver itself
1400 * submitted that need cleaning up.
1401 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001402static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001403 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001404{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001405 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001406 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001407
1408 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1409
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001410 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001411}
1412
1413/**
1414 * ep_from_windex - convert control wIndex value to endpoint
1415 * @hsotg: The driver state.
1416 * @windex: The control request wIndex field (in host order).
1417 *
1418 * Convert the given wIndex into a pointer to an driver endpoint
1419 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001420 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001421static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001422 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001423{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001424 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001425 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1426 int idx = windex & 0x7F;
1427
1428 if (windex >= 0x100)
1429 return NULL;
1430
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001431 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001432 return NULL;
1433
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001434 ep = index_to_ep(hsotg, idx, dir);
1435
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001436 if (idx && ep->dir_in != dir)
1437 return NULL;
1438
1439 return ep;
1440}
1441
1442/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001443 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001444 * @hsotg: The driver state.
1445 * @testmode: requested usb test mode
1446 * Enable usb Test Mode requested by the Host.
1447 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001448int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001449{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001450 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001451
1452 dctl &= ~DCTL_TSTCTL_MASK;
1453 switch (testmode) {
1454 case TEST_J:
1455 case TEST_K:
1456 case TEST_SE0_NAK:
1457 case TEST_PACKET:
1458 case TEST_FORCE_EN:
1459 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1460 break;
1461 default:
1462 return -EINVAL;
1463 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001464 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001465 return 0;
1466}
1467
1468/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001469 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001470 * @hsotg: The device state
1471 * @ep: Endpoint 0
1472 * @buff: Buffer for request
1473 * @length: Length of reply.
1474 *
1475 * Create a request and queue it on the given endpoint. This is useful as
1476 * an internal method of sending replies to certain control requests, etc.
1477 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001478static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001479 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001480 void *buff,
1481 int length)
1482{
1483 struct usb_request *req;
1484 int ret;
1485
1486 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1487
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001488 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001489 hsotg->ep0_reply = req;
1490 if (!req) {
1491 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1492 return -ENOMEM;
1493 }
1494
1495 req->buf = hsotg->ep0_buff;
1496 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001497 /*
1498 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1499 * STATUS stage.
1500 */
1501 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001502 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001503
1504 if (length)
1505 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001506
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001507 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001508 if (ret) {
1509 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1510 return ret;
1511 }
1512
1513 return 0;
1514}
1515
1516/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001517 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001518 * @hsotg: The device state
1519 * @ctrl: USB control request
1520 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001521static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001522 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001523{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001524 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1525 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001526 __le16 reply;
1527 int ret;
1528
1529 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1530
1531 if (!ep0->dir_in) {
1532 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1533 return -EINVAL;
1534 }
1535
1536 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1537 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001538 /*
1539 * bit 0 => self powered
1540 * bit 1 => remote wakeup
1541 */
1542 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001543 break;
1544
1545 case USB_RECIP_INTERFACE:
1546 /* currently, the data result should be zero */
1547 reply = cpu_to_le16(0);
1548 break;
1549
1550 case USB_RECIP_ENDPOINT:
1551 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1552 if (!ep)
1553 return -ENOENT;
1554
1555 reply = cpu_to_le16(ep->halted ? 1 : 0);
1556 break;
1557
1558 default:
1559 return 0;
1560 }
1561
1562 if (le16_to_cpu(ctrl->wLength) != 2)
1563 return -EINVAL;
1564
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001565 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001566 if (ret) {
1567 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1568 return ret;
1569 }
1570
1571 return 1;
1572}
1573
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001574static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001575
1576/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001577 * get_ep_head - return the first request on the endpoint
1578 * @hs_ep: The controller endpoint to get
1579 *
1580 * Get the first request on the endpoint.
1581 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001582static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001583{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001584 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1585 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001586}
1587
1588/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001589 * dwc2_gadget_start_next_request - Starts next request from ep queue
1590 * @hs_ep: Endpoint structure
1591 *
1592 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1593 * in its handler. Hence we need to unmask it here to be able to do
1594 * resynchronization.
1595 */
1596static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1597{
1598 u32 mask;
1599 struct dwc2_hsotg *hsotg = hs_ep->parent;
1600 int dir_in = hs_ep->dir_in;
1601 struct dwc2_hsotg_req *hs_req;
1602 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1603
1604 if (!list_empty(&hs_ep->queue)) {
1605 hs_req = get_ep_head(hs_ep);
1606 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1607 return;
1608 }
1609 if (!hs_ep->isochronous)
1610 return;
1611
1612 if (dir_in) {
1613 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1614 __func__);
1615 } else {
1616 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1617 __func__);
1618 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1619 mask |= DOEPMSK_OUTTKNEPDISMSK;
1620 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1621 }
1622}
1623
1624/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001625 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001626 * @hsotg: The device state
1627 * @ctrl: USB control request
1628 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001629static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001630 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001631{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001632 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1633 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001634 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001635 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001636 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001637 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001638 u32 recip;
1639 u32 wValue;
1640 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001641
1642 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1643 __func__, set ? "SET" : "CLEAR");
1644
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001645 wValue = le16_to_cpu(ctrl->wValue);
1646 wIndex = le16_to_cpu(ctrl->wIndex);
1647 recip = ctrl->bRequestType & USB_RECIP_MASK;
1648
1649 switch (recip) {
1650 case USB_RECIP_DEVICE:
1651 switch (wValue) {
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04001652 case USB_DEVICE_REMOTE_WAKEUP:
1653 hsotg->remote_wakeup_allowed = 1;
1654 break;
1655
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001656 case USB_DEVICE_TEST_MODE:
1657 if ((wIndex & 0xff) != 0)
1658 return -EINVAL;
1659 if (!set)
1660 return -EINVAL;
1661
1662 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001663 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001664 if (ret) {
1665 dev_err(hsotg->dev,
1666 "%s: failed to send reply\n", __func__);
1667 return ret;
1668 }
1669 break;
1670 default:
1671 return -ENOENT;
1672 }
1673 break;
1674
1675 case USB_RECIP_ENDPOINT:
1676 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001677 if (!ep) {
1678 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001679 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001680 return -ENOENT;
1681 }
1682
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001683 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001684 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001685 halted = ep->halted;
1686
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001687 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001688
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001689 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001690 if (ret) {
1691 dev_err(hsotg->dev,
1692 "%s: failed to send reply\n", __func__);
1693 return ret;
1694 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001695
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001696 /*
1697 * we have to complete all requests for ep if it was
1698 * halted, and the halt was cleared by CLEAR_FEATURE
1699 */
1700
1701 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001702 /*
1703 * If we have request in progress,
1704 * then complete it
1705 */
1706 if (ep->req) {
1707 hs_req = ep->req;
1708 ep->req = NULL;
1709 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001710 if (hs_req->req.complete) {
1711 spin_unlock(&hsotg->lock);
1712 usb_gadget_giveback_request(
1713 &ep->ep, &hs_req->req);
1714 spin_lock(&hsotg->lock);
1715 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001716 }
1717
1718 /* If we have pending request, then start it */
John Youn34c0887f2017-01-17 20:31:43 -08001719 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001720 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001721 }
1722
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001723 break;
1724
1725 default:
1726 return -ENOENT;
1727 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001728 break;
1729 default:
1730 return -ENOENT;
1731 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001732 return 1;
1733}
1734
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001735static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001736
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001737/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001738 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001739 * @hsotg: The device state
1740 *
1741 * Set stall for ep0 as response for setup request.
1742 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001743static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001744{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001745 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001746 u32 reg;
1747 u32 ctrl;
1748
1749 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1750 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1751
1752 /*
1753 * DxEPCTL_Stall will be cleared by EP once it has
1754 * taken effect, so no need to clear later.
1755 */
1756
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001757 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001758 ctrl |= DXEPCTL_STALL;
1759 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001760 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001761
1762 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001763 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001764 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001765
1766 /*
1767 * complete won't be called, so we enqueue
1768 * setup request here
1769 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001770 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001771}
1772
1773/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001774 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001775 * @hsotg: The device state
1776 * @ctrl: The control request received
1777 *
1778 * The controller has received the SETUP phase of a control request, and
1779 * needs to work out what to do next (and whether to pass it on to the
1780 * gadget driver).
1781 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001782static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001783 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001784{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001785 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001786 int ret = 0;
1787 u32 dcfg;
1788
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001789 dev_dbg(hsotg->dev,
1790 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1791 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1792 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001793
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001794 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001795 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001796 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1797 } else if (ctrl->bRequestType & USB_DIR_IN) {
1798 ep0->dir_in = 1;
1799 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1800 } else {
1801 ep0->dir_in = 0;
1802 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1803 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001804
1805 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1806 switch (ctrl->bRequest) {
1807 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001808 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001809 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001810 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001811 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1812 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001813 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001814
1815 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1816
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001817 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001818 return;
1819
1820 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001821 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001822 break;
1823
1824 case USB_REQ_CLEAR_FEATURE:
1825 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001826 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001827 break;
1828 }
1829 }
1830
1831 /* as a fallback, try delivering it to the driver to deal with */
1832
1833 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001834 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001835 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001836 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001837 if (ret < 0)
1838 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1839 }
1840
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001841 /*
1842 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001843 * so respond with a STALL for the status stage to indicate failure.
1844 */
1845
Robert Baldygac9f721b2014-01-14 08:36:00 +01001846 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001847 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001848}
1849
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001850/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001851 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001852 * @ep: The endpoint the request was on.
1853 * @req: The request completed.
1854 *
1855 * Called on completion of any requests the driver itself submitted for
1856 * EP0 setup packets
1857 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001858static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001859 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001860{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001861 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001862 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001863
1864 if (req->status < 0) {
1865 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1866 return;
1867 }
1868
Robert Baldyga93f599f2013-11-21 13:49:17 +01001869 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001870 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001871 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001872 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001873 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001874 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001875}
1876
1877/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001878 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001879 * @hsotg: The device state.
1880 *
1881 * Enqueue a request on EP0 if necessary to received any SETUP packets
1882 * received from the host.
1883 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001884static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001885{
1886 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001887 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001888 int ret;
1889
1890 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1891
1892 req->zero = 0;
1893 req->length = 8;
1894 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001895 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001896
1897 if (!list_empty(&hs_req->queue)) {
1898 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1899 return;
1900 }
1901
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001902 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001903 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001904 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001905
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001906 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001907 if (ret < 0) {
1908 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001909 /*
1910 * Don't think there's much we can do other than watch the
1911 * driver fail.
1912 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001913 }
1914}
1915
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001916static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001917 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001918{
1919 u32 ctrl;
1920 u8 index = hs_ep->index;
1921 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1922 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1923
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001924 if (hs_ep->dir_in)
1925 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001926 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001927 else
1928 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001929 index);
1930 if (using_desc_dma(hsotg)) {
1931 /* Not specific buffer needed for ep0 ZLP */
1932 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001933
Minas Harutyunyan201ec562018-01-16 16:03:32 +04001934 if (!index)
1935 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1936
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001937 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1938 } else {
1939 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1940 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1941 epsiz_reg);
1942 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001943
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001944 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001945 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1946 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1947 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001948 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001949}
1950
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001951/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001952 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001953 * @hsotg: The device state.
1954 * @hs_ep: The endpoint the request was on.
1955 * @hs_req: The request to complete.
1956 * @result: The result code (0 => Ok, otherwise errno)
1957 *
1958 * The given request has finished, so call the necessary completion
1959 * if it has one and then look to see if we can start a new request
1960 * on the endpoint.
1961 *
1962 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001963 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001964static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001965 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001966 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001967 int result)
1968{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001969 if (!hs_req) {
1970 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1971 return;
1972 }
1973
1974 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1975 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1976
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001977 /*
1978 * only replace the status if we've not already set an error
1979 * from a previous transaction
1980 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001981
1982 if (hs_req->req.status == -EINPROGRESS)
1983 hs_req->req.status = result;
1984
Yunzhi Li44583fe2015-09-29 12:25:01 +02001985 if (using_dma(hsotg))
1986 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1987
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001988 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001989
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001990 hs_ep->req = NULL;
1991 list_del_init(&hs_req->queue);
1992
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001993 /*
1994 * call the complete request with the locks off, just in case the
1995 * request tries to queue more work for this endpoint.
1996 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001997
1998 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001999 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02002000 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02002001 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002002 }
2003
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002004 /* In DDMA don't need to proceed to starting of next ISOC request */
2005 if (using_desc_dma(hsotg) && hs_ep->isochronous)
2006 return;
2007
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002008 /*
2009 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002010 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002011 * so be careful when doing this.
2012 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002013
John Youn34c0887f2017-01-17 20:31:43 -08002014 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07002015 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002016}
2017
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002018/*
2019 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
2020 * @hs_ep: The endpoint the request was on.
2021 *
2022 * Get first request from the ep queue, determine descriptor on which complete
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002023 * happened. SW discovers which descriptor currently in use by HW, adjusts
2024 * dma_address and calculates index of completed descriptor based on the value
2025 * of DEPDMA register. Update actual length of request, giveback to gadget.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002026 */
2027static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
2028{
2029 struct dwc2_hsotg *hsotg = hs_ep->parent;
2030 struct dwc2_hsotg_req *hs_req;
2031 struct usb_request *ureq;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002032 u32 desc_sts;
2033 u32 mask;
2034
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002035 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
2036
2037 /* Process only descriptors with buffer status set to DMA done */
2038 while ((desc_sts & DEV_DMA_BUFF_STS_MASK) >>
2039 DEV_DMA_BUFF_STS_SHIFT == DEV_DMA_BUFF_STS_DMADONE) {
2040
2041 hs_req = get_ep_head(hs_ep);
2042 if (!hs_req) {
2043 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
2044 return;
2045 }
2046 ureq = &hs_req->req;
2047
2048 /* Check completion status */
2049 if ((desc_sts & DEV_DMA_STS_MASK) >> DEV_DMA_STS_SHIFT ==
2050 DEV_DMA_STS_SUCC) {
2051 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2052 DEV_DMA_ISOC_RX_NBYTES_MASK;
2053 ureq->actual = ureq->length - ((desc_sts & mask) >>
2054 DEV_DMA_ISOC_NBYTES_SHIFT);
2055
2056 /* Adjust actual len for ISOC Out if len is
2057 * not align of 4
2058 */
2059 if (!hs_ep->dir_in && ureq->length & 0x3)
2060 ureq->actual += 4 - (ureq->length & 0x3);
2061 }
2062
2063 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2064
2065 hs_ep->compl_desc++;
2066 if (hs_ep->compl_desc > (MAX_DMA_DESC_NUM_GENERIC - 1))
2067 hs_ep->compl_desc = 0;
2068 desc_sts = hs_ep->desc_list[hs_ep->compl_desc].status;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002069 }
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002070}
2071
2072/*
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002073 * dwc2_gadget_handle_isoc_bna - handle BNA interrupt for ISOC.
2074 * @hs_ep: The isochronous endpoint.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002075 *
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002076 * If EP ISOC OUT then need to flush RX FIFO to remove source of BNA
2077 * interrupt. Reset target frame and next_desc to allow to start
2078 * ISOC's on NAK interrupt for IN direction or on OUTTKNEPDIS
2079 * interrupt for OUT direction.
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002080 */
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002081static void dwc2_gadget_handle_isoc_bna(struct dwc2_hsotg_ep *hs_ep)
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002082{
2083 struct dwc2_hsotg *hsotg = hs_ep->parent;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002084
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002085 if (!hs_ep->dir_in)
2086 dwc2_flush_rx_fifo(hsotg);
2087 dwc2_hsotg_complete_request(hsotg, hs_ep, get_ep_head(hs_ep), 0);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002088
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002089 hs_ep->target_frame = TARGET_FRAME_INITIAL;
2090 hs_ep->next_desc = 0;
2091 hs_ep->compl_desc = 0;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002092}
2093
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002094/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002095 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002096 * @hsotg: The device state.
2097 * @ep_idx: The endpoint index for the data
2098 * @size: The size of data in the fifo, in bytes
2099 *
2100 * The FIFO status shows there is data to read from the FIFO for a given
2101 * endpoint, so sort out whether we need to read the data into a request
2102 * that has been made for that endpoint.
2103 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002104static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002105{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002106 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2107 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002108 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002109 int to_read;
2110 int max_req;
2111 int read_ptr;
2112
2113 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002114 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002115 int ptr;
2116
Robert Baldyga6b448af2014-12-16 11:51:44 +01002117 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002118 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002119 __func__, size, ep_idx, epctl);
2120
2121 /* dump the data from the FIFO, we've nothing we can do */
2122 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002123 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002124
2125 return;
2126 }
2127
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002128 to_read = size;
2129 read_ptr = hs_req->req.actual;
2130 max_req = hs_req->req.length - read_ptr;
2131
Ben Dooksa33e7132010-07-19 09:40:49 +01002132 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2133 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2134
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002135 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002136 /*
2137 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002138 * to deal with in this request.
2139 */
2140
2141 /* currently we don't deal this */
2142 WARN_ON_ONCE(1);
2143 }
2144
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002145 hs_ep->total_data += to_read;
2146 hs_req->req.actual += to_read;
2147 to_read = DIV_ROUND_UP(to_read, 4);
2148
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002149 /*
2150 * note, we might over-write the buffer end by 3 bytes depending on
2151 * alignment of the data.
2152 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05002153 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002154}
2155
2156/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002157 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002158 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002159 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002160 *
2161 * Generate a zero-length IN packet request for terminating a SETUP
2162 * transaction.
2163 *
2164 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002165 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002166 * the TxFIFO.
2167 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002168static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002169{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002170 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002171 hsotg->eps_out[0]->dir_in = dir_in;
2172 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002173
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002174 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002175}
2176
Roman Bacikec1f9d92015-09-10 18:13:43 -07002177static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002178 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002179{
2180 u32 ctrl;
2181
2182 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
2183 if (ctrl & DXEPCTL_EOFRNUM)
2184 ctrl |= DXEPCTL_SETEVENFR;
2185 else
2186 ctrl |= DXEPCTL_SETODDFR;
2187 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
2188}
2189
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002190/*
2191 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2192 * @hs_ep - The endpoint on which transfer went
2193 *
2194 * Iterate over endpoints descriptor chain and get info on bytes remained
2195 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2196 */
2197static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2198{
2199 struct dwc2_hsotg *hsotg = hs_ep->parent;
2200 unsigned int bytes_rem = 0;
2201 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2202 int i;
2203 u32 status;
2204
2205 if (!desc)
2206 return -EINVAL;
2207
2208 for (i = 0; i < hs_ep->desc_count; ++i) {
2209 status = desc->status;
2210 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2211
2212 if (status & DEV_DMA_STS_MASK)
2213 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2214 i, status & DEV_DMA_STS_MASK);
2215 }
2216
2217 return bytes_rem;
2218}
2219
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002220/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002221 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002222 * @hsotg: The device instance
2223 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002224 *
2225 * The RXFIFO has delivered an OutDone event, which means that the data
2226 * transfer for an OUT endpoint has been completed, either by a short
2227 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002228 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002229static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002230{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002231 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002232 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2233 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002234 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002235 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002236 int result = 0;
2237
2238 if (!hs_req) {
2239 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2240 return;
2241 }
2242
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002243 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2244 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002245 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2246 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002247 return;
2248 }
2249
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002250 if (using_desc_dma(hsotg))
2251 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2252
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002253 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002254 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002255
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002256 /*
2257 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002258 * is left in the endpoint size register and then working it
2259 * out from the amount we loaded for the transfer.
2260 *
2261 * We need to do this as DMA pointers are always 32bit aligned
2262 * so may overshoot/undershoot the transfer.
2263 */
2264
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002265 size_done = hs_ep->size_loaded - size_left;
2266 size_done += hs_ep->last_load;
2267
2268 req->actual = size_done;
2269 }
2270
Ben Dooksa33e7132010-07-19 09:40:49 +01002271 /* if there is more request to do, schedule new transfer */
2272 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002273 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002274 return;
2275 }
2276
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002277 if (req->actual < req->length && req->short_not_ok) {
2278 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2279 __func__, req->actual, req->length);
2280
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002281 /*
2282 * todo - what should we return here? there's no one else
2283 * even bothering to check the status.
2284 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002285 }
2286
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002287 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2288 if (!using_desc_dma(hsotg) && epnum == 0 &&
2289 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002290 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002291 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002292 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002293 }
2294
Roman Bacikec1f9d92015-09-10 18:13:43 -07002295 /*
2296 * Slave mode OUT transfers do not go through XferComplete so
2297 * adjust the ISOC parity here.
2298 */
2299 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002300 if (hs_ep->isochronous && hs_ep->interval == 1)
2301 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002302 else if (hs_ep->isochronous && hs_ep->interval > 1)
2303 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002304 }
2305
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002306 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002307}
2308
2309/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002310 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002311 * @hsotg: The device instance
2312 *
2313 * The IRQ handler has detected that the RX FIFO has some data in it
2314 * that requires processing, so find out what is in there and do the
2315 * appropriate read.
2316 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002317 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002318 * chunks, so if you have x packets received on an endpoint you'll get x
2319 * FIFO events delivered, each with a packet's worth of data in it.
2320 *
2321 * When using DMA, we should not be processing events from the RXFIFO
2322 * as the actual data should be sent to the memory directly and we turn
2323 * on the completion interrupts to get notifications of transfer completion.
2324 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002325static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002326{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002327 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002328 u32 epnum, status, size;
2329
2330 WARN_ON(using_dma(hsotg));
2331
Dinh Nguyen47a16852014-04-14 14:13:34 -07002332 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2333 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002334
Dinh Nguyen47a16852014-04-14 14:13:34 -07002335 size = grxstsr & GRXSTS_BYTECNT_MASK;
2336 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002337
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002338 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002339 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002340
Dinh Nguyen47a16852014-04-14 14:13:34 -07002341 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2342 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2343 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002344 break;
2345
Dinh Nguyen47a16852014-04-14 14:13:34 -07002346 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002347 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002348 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002349
2350 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002351 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002352 break;
2353
Dinh Nguyen47a16852014-04-14 14:13:34 -07002354 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002355 dev_dbg(hsotg->dev,
2356 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002357 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002358 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002359 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002360 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002361 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2362 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2363 */
2364 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002365 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366 break;
2367
Dinh Nguyen47a16852014-04-14 14:13:34 -07002368 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002369 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002370 break;
2371
Dinh Nguyen47a16852014-04-14 14:13:34 -07002372 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002373 dev_dbg(hsotg->dev,
2374 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002375 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002376 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002377
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002378 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2379
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002380 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002381 break;
2382
2383 default:
2384 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2385 __func__, grxstsr);
2386
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002387 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002388 break;
2389 }
2390}
2391
2392/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002393 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002394 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002395 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002396static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002397{
2398 switch (mps) {
2399 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002400 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002401 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002402 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002403 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002404 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002405 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002406 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002407 }
2408
2409 /* bad max packet size, warn and return invalid result */
2410 WARN_ON(1);
2411 return (u32)-1;
2412}
2413
2414/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002415 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002416 * @hsotg: The driver state.
2417 * @ep: The index number of the endpoint
2418 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002419 * @mc: The multicount value
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002420 *
2421 * Configure the maximum packet size for the given endpoint, updating
2422 * the hardware control registers to reflect this.
2423 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002424static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002425 unsigned int ep, unsigned int mps,
2426 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002427{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002428 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002429 void __iomem *regs = hsotg->regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002430 u32 reg;
2431
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002432 hs_ep = index_to_ep(hsotg, ep, dir_in);
2433 if (!hs_ep)
2434 return;
2435
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002436 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002437 u32 mps_bytes = mps;
2438
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002439 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002440 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2441 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002442 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002443 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002444 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002445 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002446 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002447 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002448 hs_ep->mc = mc;
2449 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002450 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002451 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002452 }
2453
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002454 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002455 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002456 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002457 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002458 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002459 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002460 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002461 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002462 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002463 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002464 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002465
2466 return;
2467
2468bad_mps:
2469 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2470}
2471
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002472/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002473 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002474 * @hsotg: The driver state
2475 * @idx: The index for the endpoint (0..15)
2476 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002477static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002478{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002479 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2480 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002481
2482 /* wait until the fifo is flushed */
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002483 if (dwc2_hsotg_wait_bit_clear(hsotg, GRSTCTL, GRSTCTL_TXFFLSH, 100))
2484 dev_warn(hsotg->dev, "%s: timeout flushing fifo GRSTCTL_TXFFLSH\n",
2485 __func__);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002486}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487
2488/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002489 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002490 * @hsotg: The driver state
2491 * @hs_ep: The driver endpoint to check.
2492 *
2493 * Check to see if there is a request that has data to send, and if so
2494 * make an attempt to write data into the FIFO.
2495 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002496static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002497 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002499 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002500
Robert Baldygaafcf4162013-09-19 11:50:19 +02002501 if (!hs_ep->dir_in || !hs_req) {
2502 /**
2503 * if request is not enqueued, we disable interrupts
2504 * for endpoints, excepting ep0
2505 */
2506 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002507 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002508 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002509 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002510 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002511
2512 if (hs_req->req.actual < hs_req->req.length) {
2513 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2514 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002515 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 }
2517
2518 return 0;
2519}
2520
2521/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002522 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002523 * @hsotg: The device state.
2524 * @hs_ep: The endpoint that has just completed.
2525 *
2526 * An IN transfer has been completed, update the transfer's state and then
2527 * call the relevant completion routines.
2528 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002529static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002530 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002531{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002532 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002533 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002534 int size_left, size_done;
2535
2536 if (!hs_req) {
2537 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2538 return;
2539 }
2540
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002541 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002542 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2543 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002544
2545 /*
2546 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2547 * changed to IN. Change back to complete OUT transfer request
2548 */
2549 hs_ep->dir_in = 0;
2550
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002551 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002552 if (hsotg->test_mode) {
2553 int ret;
2554
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002555 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002556 if (ret < 0) {
2557 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002558 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002559 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002560 return;
2561 }
2562 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002563 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002564 return;
2565 }
2566
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002567 /*
2568 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002569 * in the endpoint size register and then working it out from
2570 * the amount we loaded for the transfer.
2571 *
2572 * We do this even for DMA, as the transfer may have incremented
2573 * past the end of the buffer (DMA transfers are always 32bit
2574 * aligned).
2575 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002576 if (using_desc_dma(hsotg)) {
2577 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2578 if (size_left < 0)
2579 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2580 size_left);
2581 } else {
2582 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2583 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002584
2585 size_done = hs_ep->size_loaded - size_left;
2586 size_done += hs_ep->last_load;
2587
2588 if (hs_req->req.actual != size_done)
2589 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2590 __func__, hs_req->req.actual, size_done);
2591
2592 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002593 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2594 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002595
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002596 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2597 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002598 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002599 return;
2600 }
2601
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002602 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002603 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002604 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002605 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002606 /* transfer will be completed on next complete interrupt */
2607 return;
2608 }
2609
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002610 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2611 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002612 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002613 return;
2614 }
2615
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002616 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002617}
2618
2619/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002620 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2621 * @hsotg: The device state.
2622 * @idx: Index of ep.
2623 * @dir_in: Endpoint direction 1-in 0-out.
2624 *
2625 * Reads for endpoint with given index and direction, by masking
2626 * epint_reg with coresponding mask.
2627 */
2628static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2629 unsigned int idx, int dir_in)
2630{
2631 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2632 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2633 u32 ints;
2634 u32 mask;
2635 u32 diepempmsk;
2636
2637 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2638 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2639 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2640 mask |= DXEPINT_SETUP_RCVD;
2641
2642 ints = dwc2_readl(hsotg->regs + epint_reg);
2643 ints &= mask;
2644 return ints;
2645}
2646
2647/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002648 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2649 * @hs_ep: The endpoint on which interrupt is asserted.
2650 *
2651 * This interrupt indicates that the endpoint has been disabled per the
2652 * application's request.
2653 *
2654 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2655 * in case of ISOC completes current request.
2656 *
2657 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2658 * request starts it.
2659 */
2660static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2661{
2662 struct dwc2_hsotg *hsotg = hs_ep->parent;
2663 struct dwc2_hsotg_req *hs_req;
2664 unsigned char idx = hs_ep->index;
2665 int dir_in = hs_ep->dir_in;
2666 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2667 int dctl = dwc2_readl(hsotg->regs + DCTL);
2668
2669 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2670
2671 if (dir_in) {
2672 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2673
2674 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2675
2676 if (hs_ep->isochronous) {
2677 dwc2_hsotg_complete_in(hsotg, hs_ep);
2678 return;
2679 }
2680
2681 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2682 int dctl = dwc2_readl(hsotg->regs + DCTL);
2683
2684 dctl |= DCTL_CGNPINNAK;
2685 dwc2_writel(dctl, hsotg->regs + DCTL);
2686 }
2687 return;
2688 }
2689
2690 if (dctl & DCTL_GOUTNAKSTS) {
2691 dctl |= DCTL_CGOUTNAK;
2692 dwc2_writel(dctl, hsotg->regs + DCTL);
2693 }
2694
2695 if (!hs_ep->isochronous)
2696 return;
2697
2698 if (list_empty(&hs_ep->queue)) {
2699 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2700 __func__, hs_ep);
2701 return;
2702 }
2703
2704 do {
2705 hs_req = get_ep_head(hs_ep);
2706 if (hs_req)
2707 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2708 -ENODATA);
2709 dwc2_gadget_incr_frame_num(hs_ep);
2710 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2711
2712 dwc2_gadget_start_next_request(hs_ep);
2713}
2714
2715/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002716 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2717 * @hs_ep: The endpoint on which interrupt is asserted.
2718 *
2719 * This is starting point for ISOC-OUT transfer, synchronization done with
2720 * first out token received from host while corresponding EP is disabled.
2721 *
2722 * Device does not know initial frame in which out token will come. For this
2723 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2724 * getting this interrupt SW starts calculation for next transfer frame.
2725 */
2726static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2727{
2728 struct dwc2_hsotg *hsotg = ep->parent;
2729 int dir_in = ep->dir_in;
2730 u32 doepmsk;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002731 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002732
2733 if (dir_in || !ep->isochronous)
2734 return;
2735
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002736 /*
2737 * Store frame in which irq was asserted here, as
2738 * it can change while completing request below.
2739 */
2740 tmp = dwc2_hsotg_read_frameno(hsotg);
2741
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002742 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), 0);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002743
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002744 if (using_desc_dma(hsotg)) {
2745 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2746 /* Start first ISO Out */
2747 ep->target_frame = tmp;
2748 dwc2_gadget_start_isoc_ddma(ep);
2749 }
2750 return;
2751 }
2752
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002753 if (ep->interval > 1 &&
2754 ep->target_frame == TARGET_FRAME_INITIAL) {
2755 u32 dsts;
2756 u32 ctrl;
2757
2758 dsts = dwc2_readl(hsotg->regs + DSTS);
2759 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2760 dwc2_gadget_incr_frame_num(ep);
2761
2762 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2763 if (ep->target_frame & 0x1)
2764 ctrl |= DXEPCTL_SETODDFR;
2765 else
2766 ctrl |= DXEPCTL_SETEVENFR;
2767
2768 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2769 }
2770
2771 dwc2_gadget_start_next_request(ep);
2772 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2773 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2774 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2775}
2776
2777/**
John Youn38beaec2017-01-17 20:31:13 -08002778 * dwc2_gadget_handle_nak - handle NAK interrupt
2779 * @hs_ep: The endpoint on which interrupt is asserted.
2780 *
2781 * This is starting point for ISOC-IN transfer, synchronization done with
2782 * first IN token received from host while corresponding EP is disabled.
2783 *
2784 * Device does not know when first one token will arrive from host. On first
2785 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2786 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2787 * sent in response to that as there was no data in FIFO. SW is basing on this
2788 * interrupt to obtain frame in which token has come and then based on the
2789 * interval calculates next frame for transfer.
2790 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002791static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2792{
2793 struct dwc2_hsotg *hsotg = hs_ep->parent;
2794 int dir_in = hs_ep->dir_in;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002795 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002796
2797 if (!dir_in || !hs_ep->isochronous)
2798 return;
2799
2800 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002801
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002802 tmp = dwc2_hsotg_read_frameno(hsotg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002803 if (using_desc_dma(hsotg)) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002804 dwc2_hsotg_complete_request(hsotg, hs_ep,
2805 get_ep_head(hs_ep), 0);
2806
2807 hs_ep->target_frame = tmp;
2808 dwc2_gadget_incr_frame_num(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002809 dwc2_gadget_start_isoc_ddma(hs_ep);
2810 return;
2811 }
2812
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002813 hs_ep->target_frame = tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002814 if (hs_ep->interval > 1) {
2815 u32 ctrl = dwc2_readl(hsotg->regs +
2816 DIEPCTL(hs_ep->index));
2817 if (hs_ep->target_frame & 0x1)
2818 ctrl |= DXEPCTL_SETODDFR;
2819 else
2820 ctrl |= DXEPCTL_SETEVENFR;
2821
2822 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2823 }
2824
2825 dwc2_hsotg_complete_request(hsotg, hs_ep,
2826 get_ep_head(hs_ep), 0);
2827 }
2828
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002829 if (!using_desc_dma(hsotg))
2830 dwc2_gadget_incr_frame_num(hs_ep);
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002831}
2832
2833/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002834 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002835 * @hsotg: The driver state
2836 * @idx: The index for the endpoint (0..15)
2837 * @dir_in: Set if this is an IN endpoint
2838 *
2839 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002840 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002841static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002842 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002843{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002844 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002845 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2846 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2847 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002848 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002849 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002850
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002851 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002852 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002853
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002854 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002855 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002856
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002857 if (!hs_ep) {
2858 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002859 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002860 return;
2861 }
2862
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002863 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2864 __func__, idx, dir_in ? "in" : "out", ints);
2865
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002866 /* Don't process XferCompl interrupt if it is a setup packet */
2867 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2868 ints &= ~DXEPINT_XFERCOMPL;
2869
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002870 /*
2871 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2872 * stage and xfercomplete was generated without SETUP phase done
2873 * interrupt. SW should parse received setup packet only after host's
2874 * exit from setup phase of control transfer.
2875 */
2876 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2877 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2878 ints &= ~DXEPINT_XFERCOMPL;
2879
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002880 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002881 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002882 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002883 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2884 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002885
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002886 /* In DDMA handle isochronous requests separately */
2887 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002888 /* XferCompl set along with BNA */
2889 if (!(ints & DXEPINT_BNAINTR))
2890 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002891 } else if (dir_in) {
2892 /*
2893 * We get OutDone from the FIFO, so we only
2894 * need to look at completing IN requests here
2895 * if operating slave mode
2896 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002897 if (hs_ep->isochronous && hs_ep->interval > 1)
2898 dwc2_gadget_incr_frame_num(hs_ep);
2899
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002900 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002901 if (ints & DXEPINT_NAKINTRPT)
2902 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002903
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002904 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002905 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002906 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002907 /*
2908 * We're using DMA, we need to fire an OutDone here
2909 * as we ignore the RXFIFO.
2910 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002911 if (hs_ep->isochronous && hs_ep->interval > 1)
2912 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002913
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002914 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002915 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002916 }
2917
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002918 if (ints & DXEPINT_EPDISBLD)
2919 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002920
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002921 if (ints & DXEPINT_OUTTKNEPDIS)
2922 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2923
2924 if (ints & DXEPINT_NAKINTRPT)
2925 dwc2_gadget_handle_nak(hs_ep);
2926
Dinh Nguyen47a16852014-04-14 14:13:34 -07002927 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002928 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002929
Dinh Nguyen47a16852014-04-14 14:13:34 -07002930 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002931 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2932
2933 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002934 /*
2935 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002936 * setup packet. In non-DMA mode we'd get this
2937 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002938 * the setup here.
2939 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002940
2941 if (dir_in)
2942 WARN_ON_ONCE(1);
2943 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002944 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002945 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002946 }
2947
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002948 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08002949 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
2950
Minas Harutyunyan9e95a662018-01-16 16:03:58 +04002951 /* Safety check EP0 state when STSPHSERCVD asserted */
2952 if (hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
2953 /* Move to STATUS IN for DDMA */
2954 if (using_desc_dma(hsotg))
2955 dwc2_hsotg_ep0_zlp(hsotg, true);
2956 }
2957
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002958 }
2959
Dinh Nguyen47a16852014-04-14 14:13:34 -07002960 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002961 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002962
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002963 if (ints & DXEPINT_BNAINTR) {
2964 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002965 if (hs_ep->isochronous)
Minas Harutyunyan729cac62018-05-03 17:24:28 +04002966 dwc2_gadget_handle_isoc_bna(hs_ep);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002967 }
2968
Robert Baldyga1479e842013-10-09 08:41:57 +02002969 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002970 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002971 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002972 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2973 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002974 }
2975
2976 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002977 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002978 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2979 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002980 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002981
2982 /* FIFO has space or is empty (see GAHBCFG) */
2983 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002984 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002985 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2986 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002987 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002988 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002989 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002990 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002991}
2992
2993/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002994 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002995 * @hsotg: The device state.
2996 *
2997 * Handle updating the device settings after the enumeration phase has
2998 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002999 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003000static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003001{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003002 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09003003 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003004
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003005 /*
3006 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003007 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003008 * we connected at.
3009 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003010
3011 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
3012
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003013 /*
3014 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003015 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003016 * not advertise a 64byte MPS on EP0.
3017 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003018
3019 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01003020 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003021 case DSTS_ENUMSPD_FS:
3022 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003023 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003024 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003025 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026 break;
3027
Dinh Nguyen47a16852014-04-14 14:13:34 -07003028 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003029 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003030 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003031 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003032 break;
3033
Dinh Nguyen47a16852014-04-14 14:13:34 -07003034 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003035 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003036 ep0_mps = 8;
3037 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003038 /*
3039 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003040 * moment, and the documentation seems to imply that it isn't
3041 * supported by the PHYs on some of the devices.
3042 */
3043 break;
3044 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003045 dev_info(hsotg->dev, "new device is %s\n",
3046 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003047
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003048 /*
3049 * we should now know the maximum packet size for an
3050 * endpoint, so set the endpoints to a default value.
3051 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003052
3053 if (ep0_mps) {
3054 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003055 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003056 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3057 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003058 for (i = 1; i < hsotg->num_of_eps; i++) {
3059 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003060 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3061 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003062 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003063 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3064 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003065 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003066 }
3067
3068 /* ensure after enumeration our EP0 is active */
3069
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003070 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003071
3072 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003073 dwc2_readl(hsotg->regs + DIEPCTL0),
3074 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003075}
3076
3077/**
3078 * kill_all_requests - remove all requests from the endpoint's queue
3079 * @hsotg: The device state.
3080 * @ep: The endpoint the requests may be on.
3081 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082 *
3083 * Go through the requests on the given endpoint and mark them
3084 * completed with the given result code.
3085 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003086static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003087 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01003088 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003089{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003090 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003091 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003092
Robert Baldyga6b448af2014-12-16 11:51:44 +01003093 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003094
Robert Baldyga6b448af2014-12-16 11:51:44 +01003095 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003096 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003097 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01003098
Robert Baldygab203d0a2014-09-09 10:44:56 +02003099 if (!hsotg->dedicated_fifos)
3100 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07003101 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003102 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003103 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003104}
3105
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003106/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003107 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003108 * @hsotg: The device state.
3109 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003110 * The device has been disconnected. Remove all current
3111 * transactions and signal the gadget driver that this
3112 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003113 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003114void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115{
John Youn9da51972017-01-17 20:30:27 -08003116 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003117
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003118 if (!hsotg->connected)
3119 return;
3120
3121 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003122 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003123
3124 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3125 if (hsotg->eps_in[ep])
3126 kill_all_requests(hsotg, hsotg->eps_in[ep],
John Youn9da51972017-01-17 20:30:27 -08003127 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003128 if (hsotg->eps_out[ep])
3129 kill_all_requests(hsotg, hsotg->eps_out[ep],
John Youn9da51972017-01-17 20:30:27 -08003130 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003131 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003132
3133 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003134 hsotg->lx_state = DWC2_L3;
John Stultzce2b21a2017-10-23 14:32:50 -07003135
3136 usb_gadget_set_state(&hsotg->gadget, USB_STATE_NOTATTACHED);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003137}
3138
3139/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003140 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003141 * @hsotg: The device state:
3142 * @periodic: True if this is a periodic FIFO interrupt
3143 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003144static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003145{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003146 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003147 int epno, ret;
3148
3149 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003150 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003151 ep = index_to_ep(hsotg, epno, 1);
3152
3153 if (!ep)
3154 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003155
3156 if (!ep->dir_in)
3157 continue;
3158
3159 if ((periodic && !ep->periodic) ||
3160 (!periodic && ep->periodic))
3161 continue;
3162
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003163 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003164 if (ret < 0)
3165 break;
3166 }
3167}
3168
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003169/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003170#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3171 GINTSTS_PTXFEMP | \
3172 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003173
3174/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003175 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003176 * @hsotg: The device state
3177 *
3178 * Issue a soft reset to the core, and await the core finishing it.
3179 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003180void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003181 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003182{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003183 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003184 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003185 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003186 u32 dcfg = 0;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003187
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003188 /* Kill any ep0 requests as controller will be reinitialized */
3189 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3190
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003191 if (!is_usb_reset)
John Stultz6e6360b2017-01-23 14:59:14 -08003192 if (dwc2_core_reset(hsotg, true))
Gregory Herrero86de4892015-09-29 12:08:21 +02003193 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02003194
3195 /*
3196 * we must now enable ep0 ready for host detection and then
3197 * set configuration.
3198 */
3199
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003200 /* keep other bits untouched (so e.g. forced modes are not lost) */
3201 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3202 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01003203 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003204
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003205 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003206 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3207 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003208 /* FS/LS Dedicated Transceiver Interface */
3209 usbcfg |= GUSBCFG_PHYSEL;
3210 } else {
3211 /* set the PLL on, remove the HNP/SRP and set the PHY */
3212 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3213 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3214 (val << GUSBCFG_USBTRDTIM_SHIFT);
3215 }
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003216 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003217
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003218 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003219
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003220 if (!is_usb_reset)
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003221 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003222
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003223 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003224
3225 switch (hsotg->params.speed) {
3226 case DWC2_SPEED_PARAM_LOW:
3227 dcfg |= DCFG_DEVSPD_LS;
3228 break;
3229 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003230 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3231 dcfg |= DCFG_DEVSPD_FS48;
3232 else
3233 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003234 break;
3235 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003236 dcfg |= DCFG_DEVSPD_HS;
3237 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003238
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003239 dwc2_writel(dcfg, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003240
3241 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003242 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003243
3244 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003245 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003246 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003247 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003248 GINTSTS_USBRST | GINTSTS_RESETDET |
3249 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Sevak Arakelyan376f0402018-01-24 17:43:06 +04003250 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
3251 GINTSTS_LPMTRANRCVD;
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003252
3253 if (!using_desc_dma(hsotg))
3254 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003255
John Youn95832c02017-01-23 14:57:26 -08003256 if (!hsotg->params.external_id_pin_ctl)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003257 intmsk |= GINTSTS_CONIDSTSCHNG;
3258
3259 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003260
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003261 if (using_dma(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003262 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
Razmik Karapetyand1ac8c82018-01-19 14:39:57 +04003263 hsotg->params.ahbcfg,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003264 hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003265
3266 /* Set DDMA mode support in the core if needed */
3267 if (using_desc_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003268 dwc2_set_bit(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003269
3270 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003271 dwc2_writel(((hsotg->dedicated_fifos) ?
3272 (GAHBCFG_NP_TXF_EMP_LVL |
3273 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3274 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003275 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003276
3277 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003278 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3279 * when we have no data to transfer. Otherwise we get being flooded by
3280 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003281 */
3282
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003283 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003284 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003285 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003286 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003287 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003288
3289 /*
3290 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003291 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003292 */
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003293 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3294 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003295 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003296 DOEPMSK_SETUPMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003297 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003298
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003299 /* Enable BNA interrupt for DDMA */
3300 if (using_desc_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003301 dwc2_set_bit(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003302
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003303 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003304
3305 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003306 dwc2_readl(hsotg->regs + DIEPCTL0),
3307 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003308
3309 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003310 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003311
3312 /*
3313 * Enable the RXFIFO when in slave mode, as this is how we collect
3314 * the data. In DMA mode, we get events from the FIFO but also
3315 * things we cannot process, so do not use it.
3316 */
3317 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003318 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003319
3320 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003321 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3322 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003323
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003324 if (!is_usb_reset) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003325 dwc2_set_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003326 udelay(10); /* see openiboot */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003327 dwc2_clear_bit(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003328 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003329
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003330 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003331
3332 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003333 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003334 * writing to the EPCTL register..
3335 */
3336
3337 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003338 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003339 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003340
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003341 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003342 DXEPCTL_CNAK | DXEPCTL_EPENA |
3343 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003344 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003345
3346 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003347 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003348 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003349
Lukasz Majewski308d7342012-05-04 14:17:05 +02003350 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003351 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3352 if (!is_usb_reset)
3353 val |= DCTL_SFTDISCON;
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003354 dwc2_set_bit(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003355
Sevak Arakelyan21b03402018-01-24 17:43:32 +04003356 /* configure the core to support LPM */
3357 dwc2_gadget_init_lpm(hsotg);
3358
Lukasz Majewski308d7342012-05-04 14:17:05 +02003359 /* must be at-least 3ms to allow bus to see disconnect */
3360 mdelay(3);
3361
Gregory Herrero065d3932015-09-22 15:16:54 +02003362 hsotg->lx_state = DWC2_L0;
Vardan Mikayelyan755d7392018-01-16 16:04:24 +04003363
3364 dwc2_hsotg_enqueue_setup(hsotg);
3365
3366 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
3367 dwc2_readl(hsotg->regs + DIEPCTL0),
3368 dwc2_readl(hsotg->regs + DOEPCTL0));
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003369}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003370
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003371static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003372{
3373 /* set the soft-disconnect bit */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003374 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003375}
3376
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003377void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003378{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003379 /* remove the soft-disconnect and let's go */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003380 dwc2_clear_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003381}
3382
3383/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003384 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3385 * @hsotg: The device state:
3386 *
3387 * This interrupt indicates one of the following conditions occurred while
3388 * transmitting an ISOC transaction.
3389 * - Corrupted IN Token for ISOC EP.
3390 * - Packet not complete in FIFO.
3391 *
3392 * The following actions will be taken:
3393 * - Determine the EP
3394 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3395 */
3396static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3397{
3398 struct dwc2_hsotg_ep *hs_ep;
3399 u32 epctrl;
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003400 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003401 u32 idx;
3402
3403 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3404
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003405 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3406
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003407 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3408 hs_ep = hsotg->eps_in[idx];
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003409 /* Proceed only unmasked ISOC EPs */
3410 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3411 continue;
3412
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003413 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
Razmik Karapetyan1b4977c2018-01-19 14:40:49 +04003414 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003415 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3416 epctrl |= DXEPCTL_SNAK;
3417 epctrl |= DXEPCTL_EPDIS;
3418 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3419 }
3420 }
3421
3422 /* Clear interrupt */
3423 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3424}
3425
3426/**
3427 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3428 * @hsotg: The device state:
3429 *
3430 * This interrupt indicates one of the following conditions occurred while
3431 * transmitting an ISOC transaction.
3432 * - Corrupted OUT Token for ISOC EP.
3433 * - Packet not complete in FIFO.
3434 *
3435 * The following actions will be taken:
3436 * - Determine the EP
3437 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3438 */
3439static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3440{
3441 u32 gintsts;
3442 u32 gintmsk;
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003443 u32 daintmsk;
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003444 u32 epctrl;
3445 struct dwc2_hsotg_ep *hs_ep;
3446 int idx;
3447
3448 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3449
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003450 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3451 daintmsk >>= DAINT_OUTEP_SHIFT;
3452
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003453 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3454 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003455 /* Proceed only unmasked ISOC EPs */
3456 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3457 continue;
3458
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003459 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003460 if ((epctrl & DXEPCTL_EPENA) &&
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003461 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3462 /* Unmask GOUTNAKEFF interrupt */
3463 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3464 gintmsk |= GINTSTS_GOUTNAKEFF;
3465 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3466
3467 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003468 if (!(gintsts & GINTSTS_GOUTNAKEFF)) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003469 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Razmik Karapetyan689efb22018-01-19 14:41:16 +04003470 break;
3471 }
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003472 }
3473 }
3474
3475 /* Clear interrupt */
3476 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3477}
3478
3479/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003480 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003481 * @irq: The IRQ number triggered
3482 * @pw: The pw value when registered the handler.
3483 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003484static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003485{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003486 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003487 int retry_count = 8;
3488 u32 gintsts;
3489 u32 gintmsk;
3490
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003491 if (!dwc2_is_device_mode(hsotg))
3492 return IRQ_NONE;
3493
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003494 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003495irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003496 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3497 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003498
3499 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3500 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3501
3502 gintsts &= gintmsk;
3503
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003504 if (gintsts & GINTSTS_RESETDET) {
3505 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3506
3507 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3508
3509 /* This event must be used only if controller is suspended */
3510 if (hsotg->lx_state == DWC2_L2) {
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003511 dwc2_exit_partial_power_down(hsotg, true);
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003512 hsotg->lx_state = DWC2_L0;
3513 }
3514 }
3515
3516 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003517 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3518 u32 connected = hsotg->connected;
3519
3520 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3521 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3522 dwc2_readl(hsotg->regs + GNPTXSTS));
3523
3524 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3525
3526 /* Report disconnection if it is not already done. */
3527 dwc2_hsotg_disconnect(hsotg);
3528
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003529 /* Reset device address to zero */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003530 dwc2_clear_bit(hsotg->regs + DCFG, DCFG_DEVADDR_MASK);
Minas Harutyunyan307bc112017-07-11 14:25:13 +04003531
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003532 if (usb_status & GOTGCTL_BSESVLD && connected)
3533 dwc2_hsotg_core_init_disconnected(hsotg, true);
3534 }
3535
Dinh Nguyen47a16852014-04-14 14:13:34 -07003536 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003537 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003538
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003539 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003540 }
3541
Dinh Nguyen47a16852014-04-14 14:13:34 -07003542 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003543 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3544 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003545 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003546 int ep;
3547
Robert Baldyga7e804652013-09-19 11:50:20 +02003548 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003549 daint_out = daint >> DAINT_OUTEP_SHIFT;
3550 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003551
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003552 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3553
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003554 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3555 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003556 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003557 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003558 }
3559
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003560 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3561 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003562 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003563 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003564 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003565 }
3566
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003567 /* check both FIFOs */
3568
Dinh Nguyen47a16852014-04-14 14:13:34 -07003569 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003570 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3571
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003572 /*
3573 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003574 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003575 * it needs re-enabling
3576 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003577
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003578 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3579 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003580 }
3581
Dinh Nguyen47a16852014-04-14 14:13:34 -07003582 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003583 dev_dbg(hsotg->dev, "PTxFEmp\n");
3584
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003585 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003586
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003587 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3588 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003589 }
3590
Dinh Nguyen47a16852014-04-14 14:13:34 -07003591 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003592 /*
3593 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003594 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003595 * set.
3596 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003597
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003598 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003599 }
3600
Dinh Nguyen47a16852014-04-14 14:13:34 -07003601 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003602 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003603 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003604 }
3605
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003606 /*
3607 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003608 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003609 * the occurrence.
3610 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003611
Dinh Nguyen47a16852014-04-14 14:13:34 -07003612 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003613 u8 idx;
3614 u32 epctrl;
3615 u32 gintmsk;
Razmik Karapetyand8484552018-01-19 14:41:42 +04003616 u32 daintmsk;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003617 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003618
Razmik Karapetyand8484552018-01-19 14:41:42 +04003619 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
3620 daintmsk >>= DAINT_OUTEP_SHIFT;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003621 /* Mask this interrupt */
3622 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3623 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3624 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003625
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003626 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3627 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3628 hs_ep = hsotg->eps_out[idx];
Razmik Karapetyand8484552018-01-19 14:41:42 +04003629 /* Proceed only unmasked ISOC EPs */
3630 if (!hs_ep->isochronous || (BIT(idx) & ~daintmsk))
3631 continue;
3632
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003633 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3634
Razmik Karapetyand8484552018-01-19 14:41:42 +04003635 if (epctrl & DXEPCTL_EPENA) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003636 epctrl |= DXEPCTL_SNAK;
3637 epctrl |= DXEPCTL_EPDIS;
3638 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3639 }
3640 }
3641
3642 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003643 }
3644
Dinh Nguyen47a16852014-04-14 14:13:34 -07003645 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003646 dev_info(hsotg->dev, "GINNakEff triggered\n");
3647
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003648 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003649
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003650 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003651 }
3652
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003653 if (gintsts & GINTSTS_INCOMPL_SOIN)
3654 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003655
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003656 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3657 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003658
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003659 /*
3660 * if we've had fifo events, we should try and go around the
3661 * loop again to see if there's any point in returning yet.
3662 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003663
3664 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003665 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003666
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003667 spin_unlock(&hsotg->lock);
3668
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003669 return IRQ_HANDLED;
3670}
3671
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003672static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3673 struct dwc2_hsotg_ep *hs_ep)
3674{
3675 u32 epctrl_reg;
3676 u32 epint_reg;
3677
3678 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3679 DOEPCTL(hs_ep->index);
3680 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3681 DOEPINT(hs_ep->index);
3682
3683 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3684 hs_ep->name);
3685
3686 if (hs_ep->dir_in) {
3687 if (hsotg->dedicated_fifos || hs_ep->periodic) {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003688 dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003689 /* Wait for Nak effect */
3690 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3691 DXEPINT_INEPNAKEFF, 100))
3692 dev_warn(hsotg->dev,
3693 "%s: timeout DIEPINT.NAKEFF\n",
3694 __func__);
3695 } else {
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003696 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003697 /* Wait for Nak effect */
3698 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3699 GINTSTS_GINNAKEFF, 100))
3700 dev_warn(hsotg->dev,
3701 "%s: timeout GINTSTS.GINNAKEFF\n",
3702 __func__);
3703 }
3704 } else {
3705 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003706 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003707
3708 /* Wait for global nak to take effect */
3709 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3710 GINTSTS_GOUTNAKEFF, 100))
3711 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3712 __func__);
3713 }
3714
3715 /* Disable ep */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003716 dwc2_set_bit(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003717
3718 /* Wait for ep to be disabled */
3719 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3720 dev_warn(hsotg->dev,
3721 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3722
3723 /* Clear EPDISBLD interrupt */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003724 dwc2_set_bit(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003725
3726 if (hs_ep->dir_in) {
3727 unsigned short fifo_index;
3728
3729 if (hsotg->dedicated_fifos || hs_ep->periodic)
3730 fifo_index = hs_ep->fifo_index;
3731 else
3732 fifo_index = 0;
3733
3734 /* Flush TX FIFO */
3735 dwc2_flush_tx_fifo(hsotg, fifo_index);
3736
3737 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3738 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003739 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003740
3741 } else {
3742 /* Remove global NAKs */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04003743 dwc2_set_bit(hsotg->regs + DCTL, DCTL_CGOUTNAK);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003744 }
3745}
3746
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003747/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003748 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003749 * @ep: The USB endpint to configure
3750 * @desc: The USB endpoint descriptor to configure with.
3751 *
3752 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003753 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003754static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003755 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003756{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003757 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003758 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003759 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003760 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003761 u32 epctrl_reg;
3762 u32 epctrl;
3763 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003764 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003765 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003766 unsigned int dir_in;
3767 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003768 int ret = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003769 unsigned char ep_type;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003770
3771 dev_dbg(hsotg->dev,
3772 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3773 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3774 desc->wMaxPacketSize, desc->bInterval);
3775
3776 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003777 if (index == 0) {
3778 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3779 return -EINVAL;
3780 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003781
3782 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3783 if (dir_in != hs_ep->dir_in) {
3784 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3785 return -EINVAL;
3786 }
3787
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003788 ep_type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003789 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003790 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003791
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003792 /* ISOC IN in DDMA supported bInterval up to 10 */
3793 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3794 dir_in && desc->bInterval > 10) {
3795 dev_err(hsotg->dev,
3796 "%s: ISOC IN, DDMA: bInterval>10 not supported!\n", __func__);
3797 return -EINVAL;
3798 }
3799
3800 /* High bandwidth ISOC OUT in DDMA not supported */
3801 if (using_desc_dma(hsotg) && ep_type == USB_ENDPOINT_XFER_ISOC &&
3802 !dir_in && mc > 1) {
3803 dev_err(hsotg->dev,
3804 "%s: ISOC OUT, DDMA: HB not supported!\n", __func__);
3805 return -EINVAL;
3806 }
3807
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003808 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003809
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003810 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003811 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003812
3813 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3814 __func__, epctrl, epctrl_reg);
3815
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003816 /* Allocate DMA descriptor chain for non-ctrl endpoints */
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003817 if (using_desc_dma(hsotg) && !hs_ep->desc_list) {
3818 hs_ep->desc_list = dmam_alloc_coherent(hsotg->dev,
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003819 MAX_DMA_DESC_NUM_GENERIC *
3820 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003821 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003822 if (!hs_ep->desc_list) {
3823 ret = -ENOMEM;
3824 goto error2;
3825 }
3826 }
3827
Lukasz Majewski22258f42012-06-14 10:02:24 +02003828 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003829
Dinh Nguyen47a16852014-04-14 14:13:34 -07003830 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3831 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003832
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003833 /*
3834 * mark the endpoint as active, otherwise the core may ignore
3835 * transactions entirely for this endpoint
3836 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003837 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003838
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003839 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003840 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003841
3842 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003843 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003844 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003845 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003846 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003847
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003848 switch (ep_type) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003849 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003850 epctrl |= DXEPCTL_EPTYPE_ISO;
3851 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003852 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003853 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003854 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003855 hs_ep->next_desc = 0;
Minas Harutyunyan729cac62018-05-03 17:24:28 +04003856 hs_ep->compl_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003857 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003858 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003859 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3860 mask |= DIEPMSK_NAKMSK;
3861 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3862 } else {
3863 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3864 mask |= DOEPMSK_OUTTKNEPDISMSK;
3865 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3866 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003867 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003868
3869 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003870 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003871 break;
3872
3873 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003874 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003875 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003876
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003877 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3878 hs_ep->interval = 1 << (desc->bInterval - 1);
3879
Dinh Nguyen47a16852014-04-14 14:13:34 -07003880 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003881 break;
3882
3883 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003884 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003885 break;
3886 }
3887
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003888 /*
3889 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003890 * a unique tx-fifo even if it is non-periodic.
3891 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003892 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003893 u32 fifo_index = 0;
3894 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08003895
3896 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003897 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08003898 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02003899 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003900 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08003901 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003902 if (val < size)
3903 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003904 /* Search for smallest acceptable fifo */
3905 if (val < fifo_size) {
3906 fifo_size = val;
3907 fifo_index = i;
3908 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003909 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003910 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003911 dev_err(hsotg->dev,
3912 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303913 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003914 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303915 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003916 hsotg->fifo_map |= 1 << fifo_index;
3917 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3918 hs_ep->fifo_index = fifo_index;
3919 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003920 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003921
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003922 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003923 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003924 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003925
3926 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3927 __func__, epctrl);
3928
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003929 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003930 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003931 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003932
3933 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003934 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003935
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003936error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003937 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003938
3939error2:
3940 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
Vardan Mikayelyan9383e082017-01-05 18:01:48 -08003941 dmam_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003942 sizeof(struct dwc2_dma_desc),
3943 hs_ep->desc_list, hs_ep->desc_list_dma);
3944 hs_ep->desc_list = NULL;
3945 }
3946
Julia Lawall19c190f2010-03-29 17:36:44 +02003947 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003948}
3949
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003950/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003951 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003952 * @ep: The endpoint to disable.
3953 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003954static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003955{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003956 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003957 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003958 int dir_in = hs_ep->dir_in;
3959 int index = hs_ep->index;
3960 unsigned long flags;
3961 u32 epctrl_reg;
3962 u32 ctrl;
3963
Marek Szyprowski1e011292014-09-09 10:44:54 +02003964 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003965
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003966 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003967 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3968 return -EINVAL;
3969 }
3970
John Stultz9b4810922017-10-23 14:32:49 -07003971 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3972 dev_err(hsotg->dev, "%s: called in host mode?\n", __func__);
3973 return -EINVAL;
3974 }
3975
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003976 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003977
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003978 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003979
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003980 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003981
3982 if (ctrl & DXEPCTL_EPENA)
3983 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
3984
Dinh Nguyen47a16852014-04-14 14:13:34 -07003985 ctrl &= ~DXEPCTL_EPENA;
3986 ctrl &= ~DXEPCTL_USBACTEP;
3987 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003988
3989 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003990 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003991
3992 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003993 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003994
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003995 /* terminate all requests with shutdown */
3996 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3997
Robert Baldyga1c07b202016-08-29 13:39:00 -07003998 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3999 hs_ep->fifo_index = 0;
4000 hs_ep->fifo_size = 0;
4001
Lukasz Majewski22258f42012-06-14 10:02:24 +02004002 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004003 return 0;
4004}
4005
4006/**
4007 * on_list - check request is on the given endpoint
4008 * @ep: The endpoint to check.
4009 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004010 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004011static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004012{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004013 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004014
4015 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
4016 if (req == test)
4017 return true;
4018 }
4019
4020 return false;
4021}
4022
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004023/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004024 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004025 * @ep: The endpoint to dequeue.
4026 * @req: The request to be removed from a queue.
4027 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004028static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004029{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004030 struct dwc2_hsotg_req *hs_req = our_req(req);
4031 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004032 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004033 unsigned long flags;
4034
Marek Szyprowski1e011292014-09-09 10:44:54 +02004035 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004036
Lukasz Majewski22258f42012-06-14 10:02:24 +02004037 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004038
4039 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02004040 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004041 return -EINVAL;
4042 }
4043
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02004044 /* Dequeue already started request */
4045 if (req == &hs_ep->req->req)
4046 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
4047
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004048 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02004049 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004050
4051 return 0;
4052}
4053
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004054/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004055 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004056 * @ep: The endpoint to set halt.
4057 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004058 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4059 * the endpoint is busy processing requests.
4060 *
4061 * We need to stall the endpoint immediately if request comes from set_feature
4062 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004063 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004064static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004065{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004066 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004067 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004068 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004069 u32 epreg;
4070 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004071 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004072
4073 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4074
Robert Baldygac9f721b2014-01-14 08:36:00 +01004075 if (index == 0) {
4076 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004077 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004078 else
4079 dev_warn(hs->dev,
4080 "%s: can't clear halt on ep0\n", __func__);
4081 return 0;
4082 }
4083
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004084 if (hs_ep->isochronous) {
4085 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4086 return -EINVAL;
4087 }
4088
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004089 if (!now && value && !list_empty(&hs_ep->queue)) {
4090 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4091 ep->name);
4092 return -EAGAIN;
4093 }
4094
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004095 if (hs_ep->dir_in) {
4096 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004097 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004098
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004099 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004100 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004101 if (epctl & DXEPCTL_EPENA)
4102 epctl |= DXEPCTL_EPDIS;
4103 } else {
4104 epctl &= ~DXEPCTL_STALL;
4105 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4106 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004107 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004108 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004109 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004110 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004111 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004112 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004113 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004114
John Youn34c0887f2017-01-17 20:31:43 -08004115 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004116 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004117 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004118 epctl &= ~DXEPCTL_STALL;
4119 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4120 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004121 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004122 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004123 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004124 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004125 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004126
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004127 hs_ep->halted = value;
4128
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004129 return 0;
4130}
4131
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004132/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004133 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004134 * @ep: The endpoint to set halt.
4135 * @value: Set or unset the halt.
4136 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004137static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004138{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004139 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004140 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004141 unsigned long flags = 0;
4142 int ret = 0;
4143
4144 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004145 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004146 spin_unlock_irqrestore(&hs->lock, flags);
4147
4148 return ret;
4149}
4150
Bhumika Goyalebce5612017-08-12 17:34:55 +05304151static const struct usb_ep_ops dwc2_hsotg_ep_ops = {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004152 .enable = dwc2_hsotg_ep_enable,
4153 .disable = dwc2_hsotg_ep_disable,
4154 .alloc_request = dwc2_hsotg_ep_alloc_request,
4155 .free_request = dwc2_hsotg_ep_free_request,
4156 .queue = dwc2_hsotg_ep_queue_lock,
4157 .dequeue = dwc2_hsotg_ep_dequeue,
4158 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004159 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004160};
4161
4162/**
John Youn9da51972017-01-17 20:30:27 -08004163 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004164 * @hsotg: The driver state
4165 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004166static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004167{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004168 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004169 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004170 /* unmask subset of endpoint interrupts */
4171
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004172 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4173 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4174 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004175
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004176 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4177 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4178 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004179
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004180 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004181
4182 /* Be in disconnected state until gadget is registered */
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04004183 dwc2_set_bit(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004184
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004185 /* setup fifos */
4186
4187 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004188 dwc2_readl(hsotg->regs + GRXFSIZ),
4189 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004190
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004191 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004192
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004193 /* keep other bits untouched (so e.g. forced modes are not lost) */
4194 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4195 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
Amelie Delaunayca029542017-01-12 16:09:44 +01004196 GUSBCFG_HNPCAP | GUSBCFG_USBTRDTIM_MASK);
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004197
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004198 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004199 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004200 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4201 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4202 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004203
Gregory Herrerof5090042015-01-09 13:38:47 +01004204 if (using_dma(hsotg))
Razmik Karapetyanabd064a2018-01-19 14:42:08 +04004205 dwc2_set_bit(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004206}
4207
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004208/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004209 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004210 * @gadget: The usb gadget state
4211 * @driver: The usb gadget driver
4212 *
4213 * Perform initialization to prepare udc device and driver
4214 * to work.
4215 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004216static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004217 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004218{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004219 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004220 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004221 int ret;
4222
4223 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004224 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004225 return -ENODEV;
4226 }
4227
4228 if (!driver) {
4229 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4230 return -EINVAL;
4231 }
4232
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004233 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004234 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004235
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004236 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004237 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4238 return -EINVAL;
4239 }
4240
4241 WARN_ON(hsotg->driver);
4242
4243 driver->driver.bus = NULL;
4244 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004245 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004246 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4247
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004248 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4249 ret = dwc2_lowlevel_hw_enable(hsotg);
4250 if (ret)
4251 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004252 }
4253
Gregory Herrerof6c01592015-01-09 13:38:41 +01004254 if (!IS_ERR_OR_NULL(hsotg->uphy))
4255 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004256
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004257 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004258 if (dwc2_hw_is_device(hsotg)) {
4259 dwc2_hsotg_init(hsotg);
4260 dwc2_hsotg_core_init_disconnected(hsotg, false);
4261 }
4262
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004263 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004264 spin_unlock_irqrestore(&hsotg->lock, flags);
4265
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004266 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004267
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004268 return 0;
4269
4270err:
4271 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004272 return ret;
4273}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004274
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004275/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004276 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004277 * @gadget: The usb gadget state
4278 * @driver: The usb gadget driver
4279 *
4280 * Stop udc hw block and stay tunned for future transmissions
4281 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004282static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004283{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004284 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004285 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004286 int ep;
4287
4288 if (!hsotg)
4289 return -ENODEV;
4290
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004291 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004292 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4293 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004294 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004295 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004296 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004297 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004298
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004299 spin_lock_irqsave(&hsotg->lock, flags);
4300
Marek Szyprowski32805c32014-10-20 12:45:33 +02004301 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004302 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004303 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004304
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004305 spin_unlock_irqrestore(&hsotg->lock, flags);
4306
Gregory Herrerof6c01592015-01-09 13:38:41 +01004307 if (!IS_ERR_OR_NULL(hsotg->uphy))
4308 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004309
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004310 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4311 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004312
4313 return 0;
4314}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004315
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004316/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004317 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004318 * @gadget: The usb gadget state
4319 *
4320 * Read the {micro} frame number
4321 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004322static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004323{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004324 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004325}
4326
Lukasz Majewskia188b682012-06-22 09:29:56 +02004327/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004328 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004329 * @gadget: The usb gadget state
4330 * @is_on: Current state of the USB PHY
4331 *
4332 * Connect/Disconnect the USB PHY pullup
4333 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004334static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004335{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004336 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004337 unsigned long flags = 0;
4338
Gregory Herrero77ba9112015-09-29 12:08:19 +02004339 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004340 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004341
4342 /* Don't modify pullup state while in host mode */
4343 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4344 hsotg->enabled = is_on;
4345 return 0;
4346 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004347
4348 spin_lock_irqsave(&hsotg->lock, flags);
4349 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004350 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004351 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004352 /* Enable ACG feature in device mode,if supported */
4353 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004354 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004355 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004356 dwc2_hsotg_core_disconnect(hsotg);
4357 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004358 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004359 }
4360
4361 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4362 spin_unlock_irqrestore(&hsotg->lock, flags);
4363
4364 return 0;
4365}
4366
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004367static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004368{
4369 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4370 unsigned long flags;
4371
4372 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4373 spin_lock_irqsave(&hsotg->lock, flags);
4374
Gregory Herrero61f72232015-09-29 12:08:28 +02004375 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004376 * If controller is hibernated, it must exit from power_down
Gregory Herrero61f72232015-09-29 12:08:28 +02004377 * before being initialized / de-initialized
4378 */
4379 if (hsotg->lx_state == DWC2_L2)
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004380 dwc2_exit_partial_power_down(hsotg, false);
Gregory Herrero61f72232015-09-29 12:08:28 +02004381
Gregory Herrero83d98222015-01-09 13:39:02 +01004382 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004383 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004384
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004385 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004386 if (hsotg->enabled) {
4387 /* Enable ACG feature in device mode,if supported */
4388 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004389 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004390 }
Gregory Herrero83d98222015-01-09 13:39:02 +01004391 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004392 dwc2_hsotg_core_disconnect(hsotg);
4393 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004394 }
4395
4396 spin_unlock_irqrestore(&hsotg->lock, flags);
4397 return 0;
4398}
4399
Gregory Herrero596d6962015-01-09 13:39:08 +01004400/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004401 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004402 * @gadget: The usb gadget state
4403 * @mA: Amount of current
4404 *
4405 * Report how much power the device may consume to the phy.
4406 */
John Youn9da51972017-01-17 20:30:27 -08004407static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004408{
4409 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4410
4411 if (IS_ERR_OR_NULL(hsotg->uphy))
4412 return -ENOTSUPP;
4413 return usb_phy_set_power(hsotg->uphy, mA);
4414}
4415
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004416static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4417 .get_frame = dwc2_hsotg_gadget_getframe,
4418 .udc_start = dwc2_hsotg_udc_start,
4419 .udc_stop = dwc2_hsotg_udc_stop,
4420 .pullup = dwc2_hsotg_pullup,
4421 .vbus_session = dwc2_hsotg_vbus_session,
4422 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004423};
4424
4425/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004426 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004427 * @hsotg: The device state.
4428 * @hs_ep: The endpoint to be initialised.
4429 * @epnum: The endpoint number
4430 *
4431 * Initialise the given endpoint (as part of the probe and device state
4432 * creation) to give to the gadget driver. Setup the endpoint name, any
4433 * direction information and other state that may be required.
4434 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004435static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004436 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004437 int epnum,
4438 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004439{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004440 char *dir;
4441
4442 if (epnum == 0)
4443 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004444 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004445 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004446 else
4447 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004448
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004449 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004450 hs_ep->index = epnum;
4451
4452 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4453
4454 INIT_LIST_HEAD(&hs_ep->queue);
4455 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4456
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004457 /* add to the list of endpoints known by the gadget driver */
4458 if (epnum)
4459 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4460
4461 hs_ep->parent = hsotg;
4462 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004463
4464 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4465 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4466 else
4467 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4468 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004469 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004470
Robert Baldyga29545222015-07-31 16:00:18 +02004471 if (epnum == 0) {
4472 hs_ep->ep.caps.type_control = true;
4473 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004474 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4475 hs_ep->ep.caps.type_iso = true;
4476 hs_ep->ep.caps.type_bulk = true;
4477 }
Robert Baldyga29545222015-07-31 16:00:18 +02004478 hs_ep->ep.caps.type_int = true;
4479 }
4480
4481 if (dir_in)
4482 hs_ep->ep.caps.dir_in = true;
4483 else
4484 hs_ep->ep.caps.dir_out = true;
4485
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004486 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004487 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004488 * to be something valid.
4489 */
4490
4491 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004492 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004493
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004494 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004495 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004496 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004497 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004498 }
4499}
4500
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004501/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004502 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004503 * @param: The device state
4504 *
4505 * Read the USB core HW configuration registers
4506 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004507static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004508{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004509 u32 cfg;
4510 u32 ep_type;
4511 u32 i;
4512
Ben Dooks10aebc72010-07-19 09:40:44 +01004513 /* check hardware configuration */
4514
John Youn43e90342015-12-17 11:17:45 -08004515 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4516
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004517 /* Add ep0 */
4518 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004519
John Younb98866c2017-01-17 20:31:58 -08004520 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4521 sizeof(struct dwc2_hsotg_ep),
4522 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004523 if (!hsotg->eps_in[0])
4524 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004525 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004526 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004527
John Youn43e90342015-12-17 11:17:45 -08004528 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004529 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004530 ep_type = cfg & 3;
4531 /* Direction in or both */
4532 if (!(ep_type & 2)) {
4533 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004534 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004535 if (!hsotg->eps_in[i])
4536 return -ENOMEM;
4537 }
4538 /* Direction out or both */
4539 if (!(ep_type & 1)) {
4540 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004541 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004542 if (!hsotg->eps_out[i])
4543 return -ENOMEM;
4544 }
4545 }
4546
John Youn43e90342015-12-17 11:17:45 -08004547 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4548 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004549
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004550 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4551 hsotg->num_of_eps,
4552 hsotg->dedicated_fifos ? "dedicated" : "shared",
4553 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004554 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004555}
4556
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004557/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004558 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004559 * @param: The device state
4560 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004561static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004562{
Mark Brown83a01802011-06-01 17:16:15 +01004563#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004564 struct device *dev = hsotg->dev;
4565 void __iomem *regs = hsotg->regs;
4566 u32 val;
4567 int idx;
4568
4569 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004570 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4571 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004572
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004573 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004574 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004575
4576 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004577 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004578
4579 /* show periodic fifo settings */
4580
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004581 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004582 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004583 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004584 val >> FIFOSIZE_DEPTH_SHIFT,
4585 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004586 }
4587
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004588 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004589 dev_info(dev,
4590 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004591 dwc2_readl(regs + DIEPCTL(idx)),
4592 dwc2_readl(regs + DIEPTSIZ(idx)),
4593 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004594
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004595 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004596 dev_info(dev,
4597 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004598 idx, dwc2_readl(regs + DOEPCTL(idx)),
4599 dwc2_readl(regs + DOEPTSIZ(idx)),
4600 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004601 }
4602
4603 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004604 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004605#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004606}
4607
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004608/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004609 * dwc2_gadget_init - init function for gadget
4610 * @dwc2: The data structure for the DWC2 driver.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004611 */
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004612int dwc2_gadget_init(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004613{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004614 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004615 int epnum;
4616 int ret;
John Youn43e90342015-12-17 11:17:45 -08004617
Gregory Herrero0a176272015-01-09 13:38:52 +01004618 /* Dump fifo information */
4619 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004620 hsotg->params.g_np_tx_fifo_size);
4621 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004622
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004623 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004624 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004625 hsotg->gadget.name = dev_name(dev);
Vardan Mikayelyanfa389a62018-02-16 14:08:53 +04004626 hsotg->remote_wakeup_allowed = 0;
John Youn7455f8b2018-01-24 17:44:51 +04004627
4628 if (hsotg->params.lpm)
4629 hsotg->gadget.lpm_capable = true;
4630
Gregory Herrero097ee662015-04-29 22:09:10 +02004631 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4632 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004633 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4634 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004635
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004636 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004637 if (ret) {
4638 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004639 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004640 }
4641
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004642 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4643 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004644 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004645 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004646
4647 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4648 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004649 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004650 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004651
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004652 if (using_desc_dma(hsotg)) {
4653 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4654 if (ret < 0)
4655 return ret;
4656 }
4657
Vardan Mikayelyanf3768992017-12-25 15:17:45 +04004658 ret = devm_request_irq(hsotg->dev, hsotg->irq, dwc2_hsotg_irq,
4659 IRQF_SHARED, dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004660 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004661 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004662 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004663 }
4664
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004665 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4666
4667 if (hsotg->num_of_eps == 0) {
4668 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004669 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004670 }
4671
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004672 /* setup endpoint information */
4673
4674 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004675 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004676
4677 /* allocate EP0 request */
4678
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004679 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004680 GFP_KERNEL);
4681 if (!hsotg->ctrl_req) {
4682 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004683 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004684 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004685
4686 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004687 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4688 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004689 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004690 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004691 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004692 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004693 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004694 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004695
Dinh Nguyen117777b2014-11-11 11:13:34 -06004696 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004697 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004698 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004699
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004700 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004701
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004702 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004703}
4704
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004705/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004706 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004707 * @pdev: The platform information for the driver
4708 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004709int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004710{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004711 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004712
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004713 return 0;
4714}
4715
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004716int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004717{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004718 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004719
Gregory Herrero9e779772015-04-29 22:09:07 +02004720 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004721 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004722
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004723 if (hsotg->driver) {
4724 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004725
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004726 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4727 hsotg->driver->driver.name);
4728
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004729 spin_lock_irqsave(&hsotg->lock, flags);
4730 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004731 dwc2_hsotg_core_disconnect(hsotg);
4732 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004733 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4734 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004735
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004736 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4737 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004738 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004739 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004740 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004741 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004742 }
4743
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004744 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004745}
4746
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004747int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004748{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004749 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004750
Gregory Herrero9e779772015-04-29 22:09:07 +02004751 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004752 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004753
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004754 if (hsotg->driver) {
4755 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4756 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004757
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004758 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004759 dwc2_hsotg_core_init_disconnected(hsotg, false);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004760 if (hsotg->enabled) {
4761 /* Enable ACG feature in device mode,if supported */
4762 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004763 dwc2_hsotg_core_connect(hsotg);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04004764 }
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004765 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004766 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004767
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004768 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004769}
John Youn58e52ff6a2016-02-23 19:54:57 -08004770
4771/**
4772 * dwc2_backup_device_registers() - Backup controller device registers.
4773 * When suspending usb bus, registers needs to be backuped
4774 * if controller power is disabled once suspended.
4775 *
4776 * @hsotg: Programming view of the DWC_otg controller
4777 */
4778int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4779{
4780 struct dwc2_dregs_backup *dr;
4781 int i;
4782
4783 dev_dbg(hsotg->dev, "%s\n", __func__);
4784
4785 /* Backup dev regs */
4786 dr = &hsotg->dr_backup;
4787
4788 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4789 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4790 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4791 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4792 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4793
4794 for (i = 0; i < hsotg->num_of_eps; i++) {
4795 /* Backup IN EPs */
4796 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4797
4798 /* Ensure DATA PID is correctly configured */
4799 if (dr->diepctl[i] & DXEPCTL_DPID)
4800 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4801 else
4802 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4803
4804 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4805 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4806
4807 /* Backup OUT EPs */
4808 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4809
4810 /* Ensure DATA PID is correctly configured */
4811 if (dr->doepctl[i] & DXEPCTL_DPID)
4812 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4813 else
4814 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4815
4816 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4817 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
Vardan Mikayelyanaf7c2bd2018-02-16 14:07:33 +04004818 dr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004819 }
4820 dr->valid = true;
4821 return 0;
4822}
4823
4824/**
4825 * dwc2_restore_device_registers() - Restore controller device registers.
4826 * When resuming usb bus, device registers needs to be restored
4827 * if controller power were disabled.
4828 *
4829 * @hsotg: Programming view of the DWC_otg controller
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004830 * @remote_wakeup: Indicates whether resume is initiated by Device or Host.
4831 *
4832 * Return: 0 if successful, negative error code otherwise
John Youn58e52ff6a2016-02-23 19:54:57 -08004833 */
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004834int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg, int remote_wakeup)
John Youn58e52ff6a2016-02-23 19:54:57 -08004835{
4836 struct dwc2_dregs_backup *dr;
John Youn58e52ff6a2016-02-23 19:54:57 -08004837 int i;
4838
4839 dev_dbg(hsotg->dev, "%s\n", __func__);
4840
4841 /* Restore dev regs */
4842 dr = &hsotg->dr_backup;
4843 if (!dr->valid) {
4844 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4845 __func__);
4846 return -EINVAL;
4847 }
4848 dr->valid = false;
4849
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004850 if (!remote_wakeup)
4851 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4852
John Youn58e52ff6a2016-02-23 19:54:57 -08004853 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4854 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4855 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4856
4857 for (i = 0; i < hsotg->num_of_eps; i++) {
4858 /* Restore IN EPs */
John Youn58e52ff6a2016-02-23 19:54:57 -08004859 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4860 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004861 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004862 /** WA for enabled EPx's IN in DDMA mode. On entering to
4863 * hibernation wrong value read and saved from DIEPDMAx,
4864 * as result BNA interrupt asserted on hibernation exit
4865 * by restoring from saved area.
4866 */
4867 if (hsotg->params.g_dma_desc &&
4868 (dr->diepctl[i] & DXEPCTL_EPENA))
4869 dr->diepdma[i] = hsotg->eps_in[i]->desc_list_dma;
4870 dwc2_writel(dr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
4871 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4872 /* Restore OUT EPs */
4873 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4874 /* WA for enabled EPx's OUT in DDMA mode. On entering to
4875 * hibernation wrong value read and saved from DOEPDMAx,
4876 * as result BNA interrupt asserted on hibernation exit
4877 * by restoring from saved area.
4878 */
4879 if (hsotg->params.g_dma_desc &&
4880 (dr->doepctl[i] & DXEPCTL_EPENA))
4881 dr->doepdma[i] = hsotg->eps_out[i]->desc_list_dma;
John Youn58e52ff6a2016-02-23 19:54:57 -08004882 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
Vardan Mikayelyan9a5d2812018-02-16 14:08:00 +04004883 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
John Youn58e52ff6a2016-02-23 19:54:57 -08004884 }
4885
John Youn58e52ff6a2016-02-23 19:54:57 -08004886 return 0;
4887}
Sevak Arakelyan21b03402018-01-24 17:43:32 +04004888
4889/**
4890 * dwc2_gadget_init_lpm - Configure the core to support LPM in device mode
4891 *
4892 * @hsotg: Programming view of DWC_otg controller
4893 *
4894 */
4895void dwc2_gadget_init_lpm(struct dwc2_hsotg *hsotg)
4896{
4897 u32 val;
4898
4899 if (!hsotg->params.lpm)
4900 return;
4901
4902 val = GLPMCFG_LPMCAP | GLPMCFG_APPL1RES;
4903 val |= hsotg->params.hird_threshold_en ? GLPMCFG_HIRD_THRES_EN : 0;
4904 val |= hsotg->params.lpm_clock_gating ? GLPMCFG_ENBLSLPM : 0;
4905 val |= hsotg->params.hird_threshold << GLPMCFG_HIRD_THRES_SHIFT;
4906 val |= hsotg->params.besl ? GLPMCFG_ENBESL : 0;
4907 dwc2_writel(val, hsotg->regs + GLPMCFG);
4908 dev_dbg(hsotg->dev, "GLPMCFG=0x%08x\n", dwc2_readl(hsotg->regs
4909 + GLPMCFG));
4910}
Vardan Mikayelyanc5c403dc2018-02-16 14:10:13 +04004911
4912/**
4913 * dwc2_gadget_enter_hibernation() - Put controller in Hibernation.
4914 *
4915 * @hsotg: Programming view of the DWC_otg controller
4916 *
4917 * Return non-zero if failed to enter to hibernation.
4918 */
4919int dwc2_gadget_enter_hibernation(struct dwc2_hsotg *hsotg)
4920{
4921 u32 gpwrdn;
4922 int ret = 0;
4923
4924 /* Change to L2(suspend) state */
4925 hsotg->lx_state = DWC2_L2;
4926 dev_dbg(hsotg->dev, "Start of hibernation completed\n");
4927 ret = dwc2_backup_global_registers(hsotg);
4928 if (ret) {
4929 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
4930 __func__);
4931 return ret;
4932 }
4933 ret = dwc2_backup_device_registers(hsotg);
4934 if (ret) {
4935 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
4936 __func__);
4937 return ret;
4938 }
4939
4940 gpwrdn = GPWRDN_PWRDNRSTN;
4941 gpwrdn |= GPWRDN_PMUACTV;
4942 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4943 udelay(10);
4944
4945 /* Set flag to indicate that we are in hibernation */
4946 hsotg->hibernated = 1;
4947
4948 /* Enable interrupts from wake up logic */
4949 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4950 gpwrdn |= GPWRDN_PMUINTSEL;
4951 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4952 udelay(10);
4953
4954 /* Unmask device mode interrupts in GPWRDN */
4955 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4956 gpwrdn |= GPWRDN_RST_DET_MSK;
4957 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
4958 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
4959 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4960 udelay(10);
4961
4962 /* Enable Power Down Clamp */
4963 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4964 gpwrdn |= GPWRDN_PWRDNCLMP;
4965 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4966 udelay(10);
4967
4968 /* Switch off VDD */
4969 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4970 gpwrdn |= GPWRDN_PWRDNSWTCH;
4971 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
4972 udelay(10);
4973
4974 /* Save gpwrdn register for further usage if stschng interrupt */
4975 hsotg->gr_backup.gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
4976 dev_dbg(hsotg->dev, "Hibernation completed\n");
4977
4978 return ret;
4979}
4980
4981/**
4982 * dwc2_gadget_exit_hibernation()
4983 * This function is for exiting from Device mode hibernation by host initiated
4984 * resume/reset and device initiated remote-wakeup.
4985 *
4986 * @hsotg: Programming view of the DWC_otg controller
4987 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
4988 * @param reset: indicates whether resume is initiated by Reset.
4989 *
4990 * Return non-zero if failed to exit from hibernation.
4991 */
4992int dwc2_gadget_exit_hibernation(struct dwc2_hsotg *hsotg,
4993 int rem_wakeup, int reset)
4994{
4995 u32 pcgcctl;
4996 u32 gpwrdn;
4997 u32 dctl;
4998 int ret = 0;
4999 struct dwc2_gregs_backup *gr;
5000 struct dwc2_dregs_backup *dr;
5001
5002 gr = &hsotg->gr_backup;
5003 dr = &hsotg->dr_backup;
5004
5005 if (!hsotg->hibernated) {
5006 dev_dbg(hsotg->dev, "Already exited from Hibernation\n");
5007 return 1;
5008 }
5009 dev_dbg(hsotg->dev,
5010 "%s: called with rem_wakeup = %d reset = %d\n",
5011 __func__, rem_wakeup, reset);
5012
5013 dwc2_hib_restore_common(hsotg, rem_wakeup, 0);
5014
5015 if (!reset) {
5016 /* Clear all pending interupts */
5017 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5018 }
5019
5020 /* De-assert Restore */
5021 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5022 gpwrdn &= ~GPWRDN_RESTORE;
5023 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5024 udelay(10);
5025
5026 if (!rem_wakeup) {
5027 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
5028 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
5029 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
5030 }
5031
5032 /* Restore GUSBCFG, DCFG and DCTL */
5033 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
5034 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
5035 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
5036
5037 /* De-assert Wakeup Logic */
5038 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5039 gpwrdn &= ~GPWRDN_PMUACTV;
5040 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5041
5042 if (rem_wakeup) {
5043 udelay(10);
5044 /* Start Remote Wakeup Signaling */
5045 dwc2_writel(dr->dctl | DCTL_RMTWKUPSIG, hsotg->regs + DCTL);
5046 } else {
5047 udelay(50);
5048 /* Set Device programming done bit */
5049 dctl = dwc2_readl(hsotg->regs + DCTL);
5050 dctl |= DCTL_PWRONPRGDONE;
5051 dwc2_writel(dctl, hsotg->regs + DCTL);
5052 }
5053 /* Wait for interrupts which must be cleared */
5054 mdelay(2);
5055 /* Clear all pending interupts */
5056 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5057
5058 /* Restore global registers */
5059 ret = dwc2_restore_global_registers(hsotg);
5060 if (ret) {
5061 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5062 __func__);
5063 return ret;
5064 }
5065
5066 /* Restore device registers */
5067 ret = dwc2_restore_device_registers(hsotg, rem_wakeup);
5068 if (ret) {
5069 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
5070 __func__);
5071 return ret;
5072 }
5073
5074 if (rem_wakeup) {
5075 mdelay(10);
5076 dctl = dwc2_readl(hsotg->regs + DCTL);
5077 dctl &= ~DCTL_RMTWKUPSIG;
5078 dwc2_writel(dctl, hsotg->regs + DCTL);
5079 }
5080
5081 hsotg->hibernated = 0;
5082 hsotg->lx_state = DWC2_L0;
5083 dev_dbg(hsotg->dev, "Hibernation recovery completes here\n");
5084
5085 return ret;
5086}