blob: 4e15ff2f59dba4e1406fd048dc5d49c37d355d7c [file] [log] [blame]
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001/**
Anton Tikhomirovdfbc6fa2011-04-21 17:06:43 +09002 * Copyright (c) 2011 Samsung Electronics Co., Ltd.
3 * http://www.samsung.com
4 *
Ben Dooks5b7d70c2009-06-02 14:58:06 +01005 * Copyright 2008 Openmoko, Inc.
6 * Copyright 2008 Simtec Electronics
7 * Ben Dooks <ben@simtec.co.uk>
8 * http://armlinux.simtec.co.uk/
9 *
10 * S3C USB2.0 High-speed / OtG driver
11 *
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +020015 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +010016
17#include <linux/kernel.h>
18#include <linux/module.h>
19#include <linux/spinlock.h>
20#include <linux/interrupt.h>
21#include <linux/platform_device.h>
22#include <linux/dma-mapping.h>
Marek Szyprowski7ad80962014-11-21 15:14:48 +010023#include <linux/mutex.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010024#include <linux/seq_file.h>
25#include <linux/delay.h>
26#include <linux/io.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Tomasz Figac50f056c2013-06-25 17:38:23 +020028#include <linux/of_platform.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010029
30#include <linux/usb/ch9.h>
31#include <linux/usb/gadget.h>
Praveen Panerib2e587d2012-11-14 15:57:16 +053032#include <linux/usb/phy.h>
Ben Dooks5b7d70c2009-06-02 14:58:06 +010033
Dinh Nguyenf7c0b142014-04-14 14:13:35 -070034#include "core.h"
Dinh Nguyen941fcce2014-11-11 11:13:33 -060035#include "hw.h"
Ben Dooks5b7d70c2009-06-02 14:58:06 +010036
37/* conversion functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050038static inline struct dwc2_hsotg_req *our_req(struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010039{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050040 return container_of(req, struct dwc2_hsotg_req, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010041}
42
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050043static inline struct dwc2_hsotg_ep *our_ep(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010044{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050045 return container_of(ep, struct dwc2_hsotg_ep, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010046}
47
Dinh Nguyen941fcce2014-11-11 11:13:33 -060048static inline struct dwc2_hsotg *to_hsotg(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010049{
Dinh Nguyen941fcce2014-11-11 11:13:33 -060050 return container_of(gadget, struct dwc2_hsotg, gadget);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010051}
52
53static inline void __orr32(void __iomem *ptr, u32 val)
54{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030055 dwc2_writel(dwc2_readl(ptr) | val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010056}
57
58static inline void __bic32(void __iomem *ptr, u32 val)
59{
Antti Seppälä95c8bc32015-08-20 21:41:07 +030060 dwc2_writel(dwc2_readl(ptr) & ~val, ptr);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010061}
62
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050063static inline struct dwc2_hsotg_ep *index_to_ep(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +010064 u32 ep_index, u32 dir_in)
65{
66 if (dir_in)
67 return hsotg->eps_in[ep_index];
68 else
69 return hsotg->eps_out[ep_index];
70}
71
Mickael Maison997f4f82014-12-23 17:39:45 +010072/* forward declaration of functions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -050073static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +010074
75/**
76 * using_dma - return the DMA status of the driver.
77 * @hsotg: The driver state.
78 *
79 * Return true if we're using DMA.
80 *
81 * Currently, we have the DMA support code worked into everywhere
82 * that needs it, but the AMBA DMA implementation in the hardware can
83 * only DMA from 32bit aligned addresses. This means that gadgets such
84 * as the CDC Ethernet cannot work as they often pass packets which are
85 * not 32bit aligned.
86 *
87 * Unfortunately the choice to use DMA or not is global to the controller
88 * and seems to be only settable when the controller is being put through
89 * a core reset. This means we either need to fix the gadgets to take
90 * account of DMA alignment, or add bounce buffers (yuerk).
91 *
Gregory Herreroedd74be2015-01-09 13:38:48 +010092 * g_using_dma is set depending on dts flag.
Ben Dooks5b7d70c2009-06-02 14:58:06 +010093 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -060094static inline bool using_dma(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +010095{
John Youn05ee7992016-11-03 17:56:05 -070096 return hsotg->params.g_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +010097}
98
Vahram Aharonyandec4b552016-11-09 19:27:48 -080099/*
100 * using_desc_dma - return the descriptor DMA status of the driver.
101 * @hsotg: The driver state.
102 *
103 * Return true if we're using descriptor DMA.
104 */
105static inline bool using_desc_dma(struct dwc2_hsotg *hsotg)
106{
107 return hsotg->params.g_dma_desc;
108}
109
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100110/**
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700111 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
112 * @hs_ep: The endpoint
113 * @increment: The value to increment by
114 *
115 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
116 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
117 */
118static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
119{
120 hs_ep->target_frame += hs_ep->interval;
121 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
122 hs_ep->frame_overrun = 1;
123 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
124 } else {
125 hs_ep->frame_overrun = 0;
126 }
127}
128
129/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500130 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100131 * @hsotg: The device state
132 * @ints: A bitmask of the interrupts to enable
133 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500134static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100135{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300136 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100137 u32 new_gsintmsk;
138
139 new_gsintmsk = gsintmsk | ints;
140
141 if (new_gsintmsk != gsintmsk) {
142 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300143 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100144 }
145}
146
147/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500148 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100149 * @hsotg: The device state
150 * @ints: A bitmask of the interrupts to enable
151 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500152static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100153{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300154 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100155 u32 new_gsintmsk;
156
157 new_gsintmsk = gsintmsk & ~ints;
158
159 if (new_gsintmsk != gsintmsk)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300160 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100161}
162
163/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500164 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100165 * @hsotg: The device state
166 * @ep: The endpoint index
167 * @dir_in: True if direction is in.
168 * @en: The enable value, true to enable
169 *
170 * Set or clear the mask for an individual endpoint's interrupt
171 * request.
172 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500173static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800174 unsigned int ep, unsigned int dir_in,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100175 unsigned int en)
176{
177 unsigned long flags;
178 u32 bit = 1 << ep;
179 u32 daint;
180
181 if (!dir_in)
182 bit <<= 16;
183
184 local_irq_save(flags);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300185 daint = dwc2_readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100186 if (en)
187 daint |= bit;
188 else
189 daint &= ~bit;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300190 dwc2_writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100191 local_irq_restore(flags);
192}
193
194/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500195 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100196 * @hsotg: The device instance.
197 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500198static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100199{
John Youn2317eac2016-10-17 17:36:23 -0700200 unsigned int ep;
Ben Dooks0f002d22010-05-25 05:36:50 +0100201 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100202 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100203 u32 val;
John Youn05ee7992016-11-03 17:56:05 -0700204 u32 *txfsz = hsotg->params.g_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100205
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100206 /* Reset fifo map if not correctly cleared during previous session */
207 WARN_ON(hsotg->fifo_map);
208 hsotg->fifo_map = 0;
209
Gregory Herrero0a176272015-01-09 13:38:52 +0100210 /* set RX/NPTX FIFO sizes */
John Youn05ee7992016-11-03 17:56:05 -0700211 dwc2_writel(hsotg->params.g_rx_fifo_size, hsotg->regs + GRXFSIZ);
212 dwc2_writel((hsotg->params.g_rx_fifo_size << FIFOSIZE_STARTADDR_SHIFT) |
213 (hsotg->params.g_np_tx_fifo_size << FIFOSIZE_DEPTH_SHIFT),
214 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100215
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200216 /*
217 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100218 * block have overlapping default addresses. This also ensures
219 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200220 * known values.
221 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100222
223 /* start at the end of the GNPTXFSIZ, rounded up */
John Youn05ee7992016-11-03 17:56:05 -0700224 addr = hsotg->params.g_rx_fifo_size + hsotg->params.g_np_tx_fifo_size;
Ben Dooks0f002d22010-05-25 05:36:50 +0100225
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200226 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100227 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200228 * them to endpoints dynamically according to maxpacket size value of
229 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200230 */
John Youn2317eac2016-10-17 17:36:23 -0700231 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
John Youn05ee7992016-11-03 17:56:05 -0700232 if (!txfsz[ep])
John Youn3fa95382016-10-17 17:36:25 -0700233 continue;
234 val = addr;
John Youn05ee7992016-11-03 17:56:05 -0700235 val |= txfsz[ep] << FIFOSIZE_DEPTH_SHIFT;
236 WARN_ONCE(addr + txfsz[ep] > hsotg->fifo_mem,
John Youn3fa95382016-10-17 17:36:25 -0700237 "insufficient fifo memory");
John Youn05ee7992016-11-03 17:56:05 -0700238 addr += txfsz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100239
John Youn2317eac2016-10-17 17:36:23 -0700240 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
John Youn05ee7992016-11-03 17:56:05 -0700241 val = dwc2_readl(hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100242 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100243
Sevak Arakelyanf87c8422017-01-18 18:34:19 -0800244 dwc2_writel(hsotg->hw_params.total_fifo_size |
245 addr << GDFIFOCFG_EPINFOBASE_SHIFT,
246 hsotg->regs + GDFIFOCFG);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200247 /*
248 * according to p428 of the design guide, we need to ensure that
249 * all fifos are flushed before continuing
250 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100251
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300252 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700253 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100254
255 /* wait until the fifos are both flushed */
256 timeout = 100;
257 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300258 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100259
Dinh Nguyen47a16852014-04-14 14:13:34 -0700260 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100261 break;
262
263 if (--timeout == 0) {
264 dev_err(hsotg->dev,
265 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
266 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100267 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100268 }
269
270 udelay(1);
271 }
272
273 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100274}
275
276/**
277 * @ep: USB endpoint to allocate request for.
278 * @flags: Allocation flags
279 *
280 * Allocate a new USB request structure appropriate for the specified endpoint
281 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500282static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -0800283 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100284{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500285 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100286
John Younec33efe2017-01-17 20:32:41 -0800287 req = kzalloc(sizeof(*req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100288 if (!req)
289 return NULL;
290
291 INIT_LIST_HEAD(&req->queue);
292
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100293 return &req->req;
294}
295
296/**
297 * is_ep_periodic - return true if the endpoint is in periodic mode.
298 * @hs_ep: The endpoint to query.
299 *
300 * Returns true if the endpoint is in periodic mode, meaning it is being
301 * used for an Interrupt or ISO transfer.
302 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500303static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100304{
305 return hs_ep->periodic;
306}
307
308/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500309 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100310 * @hsotg: The device state.
311 * @hs_ep: The endpoint for the request
312 * @hs_req: The request being processed.
313 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500314 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100315 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200316 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500317static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800318 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500319 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100320{
321 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -0800322
Jingoo Han17d966a2013-05-11 21:14:00 +0900323 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100324}
325
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800326/*
327 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
328 * for Control endpoint
329 * @hsotg: The device state.
330 *
331 * This function will allocate 4 descriptor chains for EP 0: 2 for
332 * Setup stage, per one for IN and OUT data/status transactions.
333 */
334static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
335{
336 hsotg->setup_desc[0] =
337 dmam_alloc_coherent(hsotg->dev,
338 sizeof(struct dwc2_dma_desc),
339 &hsotg->setup_desc_dma[0],
340 GFP_KERNEL);
341 if (!hsotg->setup_desc[0])
342 goto fail;
343
344 hsotg->setup_desc[1] =
345 dmam_alloc_coherent(hsotg->dev,
346 sizeof(struct dwc2_dma_desc),
347 &hsotg->setup_desc_dma[1],
348 GFP_KERNEL);
349 if (!hsotg->setup_desc[1])
350 goto fail;
351
352 hsotg->ctrl_in_desc =
353 dmam_alloc_coherent(hsotg->dev,
354 sizeof(struct dwc2_dma_desc),
355 &hsotg->ctrl_in_desc_dma,
356 GFP_KERNEL);
357 if (!hsotg->ctrl_in_desc)
358 goto fail;
359
360 hsotg->ctrl_out_desc =
361 dmam_alloc_coherent(hsotg->dev,
362 sizeof(struct dwc2_dma_desc),
363 &hsotg->ctrl_out_desc_dma,
364 GFP_KERNEL);
365 if (!hsotg->ctrl_out_desc)
366 goto fail;
367
368 return 0;
369
370fail:
371 return -ENOMEM;
372}
373
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100374/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500375 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100376 * @hsotg: The controller state.
377 * @hs_ep: The endpoint we're going to write for.
378 * @hs_req: The request to write data for.
379 *
380 * This is called when the TxFIFO has some space in it to hold a new
381 * transmission and we have something to give it. The actual setup of
382 * the data size is done elsewhere, so all we have to do is to actually
383 * write the data.
384 *
385 * The return value is zero if there is more space (or nothing was done)
386 * otherwise -ENOSPC is returned if the FIFO space was used up.
387 *
388 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200389 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500390static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800391 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500392 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100393{
394 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300395 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100396 int buf_pos = hs_req->req.actual;
397 int to_write = hs_ep->size_loaded;
398 void *data;
399 int can_write;
400 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200401 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100402
403 to_write -= (buf_pos - hs_ep->last_load);
404
405 /* if there's nothing to write, get out early */
406 if (to_write == 0)
407 return 0;
408
Ben Dooks10aebc72010-07-19 09:40:44 +0100409 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300410 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100411 int size_left;
412 int size_done;
413
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200414 /*
415 * work out how much data was loaded so we can calculate
416 * how much data is left in the fifo.
417 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100418
Dinh Nguyen47a16852014-04-14 14:13:34 -0700419 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100420
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200421 /*
422 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100423 * previous data has been completely sent.
424 */
425 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500426 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100427 return -ENOSPC;
428 }
429
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100430 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
431 __func__, size_left,
432 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
433
434 /* how much of the data has moved */
435 size_done = hs_ep->size_loaded - size_left;
436
437 /* how much data is left in the fifo */
438 can_write = hs_ep->fifo_load - size_done;
439 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
440 __func__, can_write);
441
442 can_write = hs_ep->fifo_size - can_write;
443 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
444 __func__, can_write);
445
446 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500447 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100448 return -ENOSPC;
449 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100450 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Robert Baldygaad674a12016-08-29 13:38:50 -0700451 can_write = dwc2_readl(hsotg->regs +
452 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100453
454 can_write &= 0xffff;
455 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100456 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700457 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100458 dev_dbg(hsotg->dev,
459 "%s: no queue slots available (0x%08x)\n",
460 __func__, gnptxsts);
461
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500462 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100463 return -ENOSPC;
464 }
465
Dinh Nguyen47a16852014-04-14 14:13:34 -0700466 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100467 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100468 }
469
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200470 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
471
472 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
John Youn9da51972017-01-17 20:30:27 -0800473 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100474
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200475 /*
476 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100477 * FIFO, requests of >512 cause the endpoint to get stuck with a
478 * fragment of the end of the transfer in it.
479 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200480 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100481 can_write = 512;
482
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200483 /*
484 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100485 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200486 * doing it.
487 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200488 if (to_write > max_transfer) {
489 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100490
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200491 /* it's needed only when we do not use dedicated fifos */
492 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500493 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800494 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700495 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100496 }
497
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100498 /* see if we can write data */
499
500 if (to_write > can_write) {
501 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200502 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100503
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200504 /*
505 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100506 * exact number of packets.
507 *
508 * Note, we do not currently check to see if we can ever
509 * write a full packet or not to the FIFO.
510 */
511
512 if (pkt_round)
513 to_write -= pkt_round;
514
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200515 /*
516 * enable correct FIFO interrupt to alert us when there
517 * is more room left.
518 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100519
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200520 /* it's needed only when we do not use dedicated fifos */
521 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500522 dwc2_hsotg_en_gsint(hsotg,
John Youn9da51972017-01-17 20:30:27 -0800523 periodic ? GINTSTS_PTXFEMP :
Dinh Nguyen47a16852014-04-14 14:13:34 -0700524 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100525 }
526
527 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
John Youn9da51972017-01-17 20:30:27 -0800528 to_write, hs_req->req.length, can_write, buf_pos);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100529
530 if (to_write <= 0)
531 return -ENOSPC;
532
533 hs_req->req.actual = buf_pos + to_write;
534 hs_ep->total_data += to_write;
535
536 if (periodic)
537 hs_ep->fifo_load += to_write;
538
539 to_write = DIV_ROUND_UP(to_write, 4);
540 data = hs_req->req.buf + buf_pos;
541
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500542 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100543
544 return (to_write >= can_write) ? -ENOSPC : 0;
545}
546
547/**
548 * get_ep_limit - get the maximum data legnth for this endpoint
549 * @hs_ep: The endpoint
550 *
551 * Return the maximum data that can be queued in one go on a given endpoint
552 * so that transfers that are too long can be split.
553 */
John Youn9da51972017-01-17 20:30:27 -0800554static unsigned int get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100555{
556 int index = hs_ep->index;
John Youn9da51972017-01-17 20:30:27 -0800557 unsigned int maxsize;
558 unsigned int maxpkt;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100559
560 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700561 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
562 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100563 } else {
John Youn9da51972017-01-17 20:30:27 -0800564 maxsize = 64 + 64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900565 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700566 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900567 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100568 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100569 }
570
571 /* we made the constant loading easier above by using +1 */
572 maxpkt--;
573 maxsize--;
574
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200575 /*
576 * constrain by packet count if maxpkts*pktsize is greater
577 * than the length register size.
578 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100579
580 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
581 maxsize = maxpkt * hs_ep->ep.maxpacket;
582
583 return maxsize;
584}
585
586/**
John Youn38beaec2017-01-17 20:31:13 -0800587 * dwc2_hsotg_read_frameno - read current frame number
588 * @hsotg: The device instance
589 *
590 * Return the current frame number
591 */
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700592static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
593{
594 u32 dsts;
595
596 dsts = dwc2_readl(hsotg->regs + DSTS);
597 dsts &= DSTS_SOFFN_MASK;
598 dsts >>= DSTS_SOFFN_SHIFT;
599
600 return dsts;
601}
602
603/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800604 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
605 * DMA descriptor chain prepared for specific endpoint
606 * @hs_ep: The endpoint
607 *
608 * Return the maximum data that can be queued in one go on a given endpoint
609 * depending on its descriptor chain capacity so that transfers that
610 * are too long can be split.
611 */
612static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
613{
614 int is_isoc = hs_ep->isochronous;
615 unsigned int maxsize;
616
617 if (is_isoc)
618 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
619 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
620 else
621 maxsize = DEV_DMA_NBYTES_LIMIT;
622
623 /* Above size of one descriptor was chosen, multiple it */
624 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
625
626 return maxsize;
627}
628
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800629/*
630 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
631 * @hs_ep: The endpoint
632 * @mask: RX/TX bytes mask to be defined
633 *
634 * Returns maximum data payload for one descriptor after analyzing endpoint
635 * characteristics.
636 * DMA descriptor transfer bytes limit depends on EP type:
637 * Control out - MPS,
638 * Isochronous - descriptor rx/tx bytes bitfield limit,
639 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
640 * have concatenations from various descriptors within one packet.
641 *
642 * Selects corresponding mask for RX/TX bytes as well.
643 */
644static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
645{
646 u32 mps = hs_ep->ep.maxpacket;
647 int dir_in = hs_ep->dir_in;
648 u32 desc_size = 0;
649
650 if (!hs_ep->index && !dir_in) {
651 desc_size = mps;
652 *mask = DEV_DMA_NBYTES_MASK;
653 } else if (hs_ep->isochronous) {
654 if (dir_in) {
655 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
656 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
657 } else {
658 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
659 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
660 }
661 } else {
662 desc_size = DEV_DMA_NBYTES_LIMIT;
663 *mask = DEV_DMA_NBYTES_MASK;
664
665 /* Round down desc_size to be mps multiple */
666 desc_size -= desc_size % mps;
667 }
668
669 return desc_size;
670}
671
672/*
673 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
674 * @hs_ep: The endpoint
675 * @dma_buff: DMA address to use
676 * @len: Length of the transfer
677 *
678 * This function will iterate over descriptor chain and fill its entries
679 * with corresponding information based on transfer data.
680 */
681static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
682 dma_addr_t dma_buff,
683 unsigned int len)
684{
685 struct dwc2_hsotg *hsotg = hs_ep->parent;
686 int dir_in = hs_ep->dir_in;
687 struct dwc2_dma_desc *desc = hs_ep->desc_list;
688 u32 mps = hs_ep->ep.maxpacket;
689 u32 maxsize = 0;
690 u32 offset = 0;
691 u32 mask = 0;
692 int i;
693
694 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
695
696 hs_ep->desc_count = (len / maxsize) +
697 ((len % maxsize) ? 1 : 0);
698 if (len == 0)
699 hs_ep->desc_count = 1;
700
701 for (i = 0; i < hs_ep->desc_count; ++i) {
702 desc->status = 0;
703 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
704 << DEV_DMA_BUFF_STS_SHIFT);
705
706 if (len > maxsize) {
707 if (!hs_ep->index && !dir_in)
708 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
709
710 desc->status |= (maxsize <<
711 DEV_DMA_NBYTES_SHIFT & mask);
712 desc->buf = dma_buff + offset;
713
714 len -= maxsize;
715 offset += maxsize;
716 } else {
717 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
718
719 if (dir_in)
720 desc->status |= (len % mps) ? DEV_DMA_SHORT :
721 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
722 if (len > maxsize)
723 dev_err(hsotg->dev, "wrong len %d\n", len);
724
725 desc->status |=
726 len << DEV_DMA_NBYTES_SHIFT & mask;
727 desc->buf = dma_buff + offset;
728 }
729
730 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
731 desc->status |= (DEV_DMA_BUFF_STS_HREADY
732 << DEV_DMA_BUFF_STS_SHIFT);
733 desc++;
734 }
735}
736
Vahram Aharonyan540ccba2016-11-14 19:16:41 -0800737/*
738 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
739 * @hs_ep: The isochronous endpoint.
740 * @dma_buff: usb requests dma buffer.
741 * @len: usb request transfer length.
742 *
743 * Finds out index of first free entry either in the bottom or up half of
744 * descriptor chain depend on which is under SW control and not processed
745 * by HW. Then fills that descriptor with the data of the arrived usb request,
746 * frame info, sets Last and IOC bits increments next_desc. If filled
747 * descriptor is not the first one, removes L bit from the previous descriptor
748 * status.
749 */
750static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
751 dma_addr_t dma_buff, unsigned int len)
752{
753 struct dwc2_dma_desc *desc;
754 struct dwc2_hsotg *hsotg = hs_ep->parent;
755 u32 index;
756 u32 maxsize = 0;
757 u32 mask = 0;
758
759 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
760 if (len > maxsize) {
761 dev_err(hsotg->dev, "wrong len %d\n", len);
762 return -EINVAL;
763 }
764
765 /*
766 * If SW has already filled half of chain, then return and wait for
767 * the other chain to be processed by HW.
768 */
769 if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2)
770 return -EBUSY;
771
772 /* Increment frame number by interval for IN */
773 if (hs_ep->dir_in)
774 dwc2_gadget_incr_frame_num(hs_ep);
775
776 index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num +
777 hs_ep->next_desc;
778
779 /* Sanity check of calculated index */
780 if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) ||
781 (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) {
782 dev_err(hsotg->dev, "wrong index %d for iso chain\n", index);
783 return -EINVAL;
784 }
785
786 desc = &hs_ep->desc_list[index];
787
788 /* Clear L bit of previous desc if more than one entries in the chain */
789 if (hs_ep->next_desc)
790 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
791
792 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
793 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
794
795 desc->status = 0;
796 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
797
798 desc->buf = dma_buff;
799 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
800 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
801
802 if (hs_ep->dir_in) {
803 desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) &
804 DEV_DMA_ISOC_PID_MASK) |
805 ((len % hs_ep->ep.maxpacket) ?
806 DEV_DMA_SHORT : 0) |
807 ((hs_ep->target_frame <<
808 DEV_DMA_ISOC_FRNUM_SHIFT) &
809 DEV_DMA_ISOC_FRNUM_MASK);
810 }
811
812 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
813 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
814
815 /* Update index of last configured entry in the chain */
816 hs_ep->next_desc++;
817
818 return 0;
819}
820
821/*
822 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
823 * @hs_ep: The isochronous endpoint.
824 *
825 * Prepare first descriptor chain for isochronous endpoints. Afterwards
826 * write DMA address to HW and enable the endpoint.
827 *
828 * Switch between descriptor chains via isoc_chain_num to give SW opportunity
829 * to prepare second descriptor chain while first one is being processed by HW.
830 */
831static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
832{
833 struct dwc2_hsotg *hsotg = hs_ep->parent;
834 struct dwc2_hsotg_req *hs_req, *treq;
835 int index = hs_ep->index;
836 int ret;
837 u32 dma_reg;
838 u32 depctl;
839 u32 ctrl;
840
841 if (list_empty(&hs_ep->queue)) {
842 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
843 return;
844 }
845
846 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
847 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
848 hs_req->req.length);
849 if (ret) {
850 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
851 break;
852 }
853 }
854
855 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
856 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
857
858 /* write descriptor chain address to control register */
859 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
860
861 ctrl = dwc2_readl(hsotg->regs + depctl);
862 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
863 dwc2_writel(ctrl, hsotg->regs + depctl);
864
865 /* Switch ISOC descriptor chain number being processed by SW*/
866 hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
867 hs_ep->next_desc = 0;
868}
869
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800870/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500871 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100872 * @hsotg: The controller state.
873 * @hs_ep: The endpoint to process a request for
874 * @hs_req: The request to start.
875 * @continuing: True if we are doing more for the current request.
876 *
877 * Start the given request running by setting the endpoint registers
878 * appropriately, and writing any data to the FIFOs.
879 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500880static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -0800881 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500882 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100883 bool continuing)
884{
885 struct usb_request *ureq = &hs_req->req;
886 int index = hs_ep->index;
887 int dir_in = hs_ep->dir_in;
888 u32 epctrl_reg;
889 u32 epsize_reg;
890 u32 epsize;
891 u32 ctrl;
John Youn9da51972017-01-17 20:30:27 -0800892 unsigned int length;
893 unsigned int packets;
894 unsigned int maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800895 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100896
897 if (index != 0) {
898 if (hs_ep->req && !continuing) {
899 dev_err(hsotg->dev, "%s: active request\n", __func__);
900 WARN_ON(1);
901 return;
902 } else if (hs_ep->req != hs_req && continuing) {
903 dev_err(hsotg->dev,
904 "%s: continue different req\n", __func__);
905 WARN_ON(1);
906 return;
907 }
908 }
909
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800910 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200911 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
912 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100913
914 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300915 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100916 hs_ep->dir_in ? "in" : "out");
917
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900918 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300919 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900920
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200921 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900922 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
923 return;
924 }
925
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100926 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200927 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
928 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100929
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800930 if (!using_desc_dma(hsotg))
931 maxreq = get_ep_limit(hs_ep);
932 else
933 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
934
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100935 if (length > maxreq) {
936 int round = maxreq % hs_ep->ep.maxpacket;
937
938 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
939 __func__, length, maxreq, round);
940
941 /* round down to multiple of packets */
942 if (round)
943 maxreq -= round;
944
945 length = maxreq;
946 }
947
948 if (length)
949 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
950 else
951 packets = 1; /* send one packet if length is zero. */
952
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200953 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
954 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
955 return;
956 }
957
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100958 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200959 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700960 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200961 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700962 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100963 else
964 epsize = 0;
965
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100966 /*
967 * zero length packet should be programmed on its own and should not
968 * be counted in DIEPTSIZ.PktCnt with other packets.
969 */
970 if (dir_in && ureq->zero && !continuing) {
971 /* Test if zlp is actually required. */
972 if ((ureq->length >= hs_ep->ep.maxpacket) &&
John Youn9da51972017-01-17 20:30:27 -0800973 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +0100974 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100975 }
976
Dinh Nguyen47a16852014-04-14 14:13:34 -0700977 epsize |= DXEPTSIZ_PKTCNT(packets);
978 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100979
980 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
981 __func__, packets, length, ureq->length, epsize, epsize_reg);
982
983 /* store the request as the current one we're doing */
984 hs_ep->req = hs_req;
985
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800986 if (using_desc_dma(hsotg)) {
987 u32 offset = 0;
988 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100989
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800990 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
991 if (!dir_in) {
992 if (!index)
993 length = mps;
994 else if (length % mps)
995 length += (mps - (length % mps));
996 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100997
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200998 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800999 * If more data to send, adjust DMA for EP0 out data stage.
1000 * ureq->dma stays unchanged, hence increment it by already
1001 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001002 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001003 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1004 continuing)
1005 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001006
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001007 /* Fill DDMA chain entries */
1008 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1009 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001010
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001011 /* write descriptor chain address to control register */
1012 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
1013
1014 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1015 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1016 } else {
1017 /* write size / packets */
1018 dwc2_writel(epsize, hsotg->regs + epsize_reg);
1019
Razmik Karapetyan729e6572016-11-16 15:33:55 -08001020 if (using_dma(hsotg) && !continuing && (length != 0)) {
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001021 /*
1022 * write DMA address to control register, buffer
1023 * already synced by dwc2_hsotg_ep_queue().
1024 */
1025
1026 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
1027
1028 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1029 __func__, &ureq->dma, dma_reg);
1030 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001031 }
1032
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001033 if (hs_ep->isochronous && hs_ep->interval == 1) {
1034 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1035 dwc2_gadget_incr_frame_num(hs_ep);
1036
1037 if (hs_ep->target_frame & 0x1)
1038 ctrl |= DXEPCTL_SETODDFR;
1039 else
1040 ctrl |= DXEPCTL_SETEVENFR;
1041 }
1042
Dinh Nguyen47a16852014-04-14 14:13:34 -07001043 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001044
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001045 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001046
1047 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001048 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001049 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001050
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001051 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001052 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001053
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001054 /*
1055 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001056 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001057 * this information.
1058 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001059 hs_ep->size_loaded = length;
1060 hs_ep->last_load = ureq->actual;
1061
1062 if (dir_in && !using_dma(hsotg)) {
1063 /* set these anyway, we may need them for non-periodic in */
1064 hs_ep->fifo_load = 0;
1065
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001066 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001067 }
1068
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001069 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001070 * Note, trying to clear the NAK here causes problems with transmit
1071 * on the S3C6400 ending up with the TXFIFO becoming full.
1072 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001073
1074 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001075 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001076 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08001077 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001078 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001079
Dinh Nguyen47a16852014-04-14 14:13:34 -07001080 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001081 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001082
1083 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001084 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001085}
1086
1087/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001088 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001089 * @hsotg: The device state.
1090 * @hs_ep: The endpoint the request is on.
1091 * @req: The request being processed.
1092 *
1093 * We've been asked to queue a request, so ensure that the memory buffer
1094 * is correctly setup for DMA. If we've been passed an extant DMA address
1095 * then ensure the buffer has been synced to memory. If our buffer has no
1096 * DMA memory, then we map the memory and mark our request to allow us to
1097 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001098 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001099static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001100 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001101 struct usb_request *req)
1102{
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001103 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001104
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001105 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1106 if (ret)
1107 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001108
1109 return 0;
1110
1111dma_error:
1112 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1113 __func__, req->buf, req->length);
1114
1115 return -EIO;
1116}
1117
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001118static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
John Younb98866c2017-01-17 20:31:58 -08001119 struct dwc2_hsotg_ep *hs_ep,
1120 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001121{
1122 void *req_buf = hs_req->req.buf;
1123
1124 /* If dma is not being used or buffer is aligned */
1125 if (!using_dma(hsotg) || !((long)req_buf & 3))
1126 return 0;
1127
1128 WARN_ON(hs_req->saved_req_buf);
1129
1130 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
John Youn9da51972017-01-17 20:30:27 -08001131 hs_ep->ep.name, req_buf, hs_req->req.length);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001132
1133 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1134 if (!hs_req->req.buf) {
1135 hs_req->req.buf = req_buf;
1136 dev_err(hsotg->dev,
1137 "%s: unable to allocate memory for bounce buffer\n",
1138 __func__);
1139 return -ENOMEM;
1140 }
1141
1142 /* Save actual buffer */
1143 hs_req->saved_req_buf = req_buf;
1144
1145 if (hs_ep->dir_in)
1146 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1147 return 0;
1148}
1149
John Younb98866c2017-01-17 20:31:58 -08001150static void
1151dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1152 struct dwc2_hsotg_ep *hs_ep,
1153 struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001154{
1155 /* If dma is not being used or buffer was aligned */
1156 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1157 return;
1158
1159 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1160 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1161
1162 /* Copy data from bounce buffer on successful out transfer */
1163 if (!hs_ep->dir_in && !hs_req->req.status)
1164 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
John Youn9da51972017-01-17 20:30:27 -08001165 hs_req->req.actual);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001166
1167 /* Free bounce buffer */
1168 kfree(hs_req->req.buf);
1169
1170 hs_req->req.buf = hs_req->saved_req_buf;
1171 hs_req->saved_req_buf = NULL;
1172}
1173
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001174/**
1175 * dwc2_gadget_target_frame_elapsed - Checks target frame
1176 * @hs_ep: The driver endpoint to check
1177 *
1178 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1179 * corresponding transfer.
1180 */
1181static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1182{
1183 struct dwc2_hsotg *hsotg = hs_ep->parent;
1184 u32 target_frame = hs_ep->target_frame;
1185 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
1186 bool frame_overrun = hs_ep->frame_overrun;
1187
1188 if (!frame_overrun && current_frame >= target_frame)
1189 return true;
1190
1191 if (frame_overrun && current_frame >= target_frame &&
1192 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1193 return true;
1194
1195 return false;
1196}
1197
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001198/*
1199 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1200 * @hsotg: The driver state
1201 * @hs_ep: the ep descriptor chain is for
1202 *
1203 * Called to update EP0 structure's pointers depend on stage of
1204 * control transfer.
1205 */
1206static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1207 struct dwc2_hsotg_ep *hs_ep)
1208{
1209 switch (hsotg->ep0_state) {
1210 case DWC2_EP0_SETUP:
1211 case DWC2_EP0_STATUS_OUT:
1212 hs_ep->desc_list = hsotg->setup_desc[0];
1213 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1214 break;
1215 case DWC2_EP0_DATA_IN:
1216 case DWC2_EP0_STATUS_IN:
1217 hs_ep->desc_list = hsotg->ctrl_in_desc;
1218 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1219 break;
1220 case DWC2_EP0_DATA_OUT:
1221 hs_ep->desc_list = hsotg->ctrl_out_desc;
1222 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1223 break;
1224 default:
1225 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1226 hsotg->ep0_state);
1227 return -EINVAL;
1228 }
1229
1230 return 0;
1231}
1232
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001233static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001234 gfp_t gfp_flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001235{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001236 struct dwc2_hsotg_req *hs_req = our_req(req);
1237 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001238 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001239 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001240 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001241
1242 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1243 ep->name, req, req->length, req->buf, req->no_interrupt,
1244 req->zero, req->short_not_ok);
1245
Gregory Herrero7ababa92015-04-29 22:09:08 +02001246 /* Prevent new request submission when controller is suspended */
1247 if (hs->lx_state == DWC2_L2) {
1248 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
John Youn9da51972017-01-17 20:30:27 -08001249 __func__);
Gregory Herrero7ababa92015-04-29 22:09:08 +02001250 return -EAGAIN;
1251 }
1252
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001253 /* initialise status of the request */
1254 INIT_LIST_HEAD(&hs_req->queue);
1255 req->actual = 0;
1256 req->status = -EINPROGRESS;
1257
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001258 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001259 if (ret)
1260 return ret;
1261
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001262 /* if we're using DMA, sync the buffers as necessary */
1263 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001264 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001265 if (ret)
1266 return ret;
1267 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001268 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1269 if (using_desc_dma(hs) && !hs_ep->index) {
1270 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1271 if (ret)
1272 return ret;
1273 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001274
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001275 first = list_empty(&hs_ep->queue);
1276 list_add_tail(&hs_req->queue, &hs_ep->queue);
1277
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001278 /*
1279 * Handle DDMA isochronous transfers separately - just add new entry
1280 * to the half of descriptor chain that is not processed by HW.
1281 * Transfer will be started once SW gets either one of NAK or
1282 * OutTknEpDis interrupts.
1283 */
1284 if (using_desc_dma(hs) && hs_ep->isochronous &&
1285 hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1286 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1287 hs_req->req.length);
1288 if (ret)
1289 dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__);
1290
1291 return 0;
1292 }
1293
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001294 if (first) {
1295 if (!hs_ep->isochronous) {
1296 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1297 return 0;
1298 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001299
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001300 while (dwc2_gadget_target_frame_elapsed(hs_ep))
1301 dwc2_gadget_incr_frame_num(hs_ep);
1302
1303 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1304 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1305 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001306 return 0;
1307}
1308
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001309static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
John Youn9da51972017-01-17 20:30:27 -08001310 gfp_t gfp_flags)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001311{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001312 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001313 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001314 unsigned long flags = 0;
1315 int ret = 0;
1316
1317 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001318 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001319 spin_unlock_irqrestore(&hs->lock, flags);
1320
1321 return ret;
1322}
1323
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001324static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001325 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001326{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001327 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001328
1329 kfree(hs_req);
1330}
1331
1332/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001333 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001334 * @ep: The endpoint the request was on.
1335 * @req: The request completed.
1336 *
1337 * Called on completion of any requests the driver itself
1338 * submitted that need cleaning up.
1339 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001340static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001341 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001342{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001343 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001344 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001345
1346 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1347
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001348 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001349}
1350
1351/**
1352 * ep_from_windex - convert control wIndex value to endpoint
1353 * @hsotg: The driver state.
1354 * @windex: The control request wIndex field (in host order).
1355 *
1356 * Convert the given wIndex into a pointer to an driver endpoint
1357 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001358 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001359static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001360 u32 windex)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001361{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001362 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001363 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1364 int idx = windex & 0x7F;
1365
1366 if (windex >= 0x100)
1367 return NULL;
1368
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001369 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001370 return NULL;
1371
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001372 ep = index_to_ep(hsotg, idx, dir);
1373
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001374 if (idx && ep->dir_in != dir)
1375 return NULL;
1376
1377 return ep;
1378}
1379
1380/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001381 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001382 * @hsotg: The driver state.
1383 * @testmode: requested usb test mode
1384 * Enable usb Test Mode requested by the Host.
1385 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001386int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001387{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001388 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001389
1390 dctl &= ~DCTL_TSTCTL_MASK;
1391 switch (testmode) {
1392 case TEST_J:
1393 case TEST_K:
1394 case TEST_SE0_NAK:
1395 case TEST_PACKET:
1396 case TEST_FORCE_EN:
1397 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1398 break;
1399 default:
1400 return -EINVAL;
1401 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001402 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001403 return 0;
1404}
1405
1406/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001407 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001408 * @hsotg: The device state
1409 * @ep: Endpoint 0
1410 * @buff: Buffer for request
1411 * @length: Length of reply.
1412 *
1413 * Create a request and queue it on the given endpoint. This is useful as
1414 * an internal method of sending replies to certain control requests, etc.
1415 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001416static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001417 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001418 void *buff,
1419 int length)
1420{
1421 struct usb_request *req;
1422 int ret;
1423
1424 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1425
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001426 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001427 hsotg->ep0_reply = req;
1428 if (!req) {
1429 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1430 return -ENOMEM;
1431 }
1432
1433 req->buf = hsotg->ep0_buff;
1434 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001435 /*
1436 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1437 * STATUS stage.
1438 */
1439 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001440 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001441
1442 if (length)
1443 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001444
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001445 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001446 if (ret) {
1447 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1448 return ret;
1449 }
1450
1451 return 0;
1452}
1453
1454/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001455 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001456 * @hsotg: The device state
1457 * @ctrl: USB control request
1458 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001459static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001460 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001461{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001462 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1463 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001464 __le16 reply;
1465 int ret;
1466
1467 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1468
1469 if (!ep0->dir_in) {
1470 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1471 return -EINVAL;
1472 }
1473
1474 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1475 case USB_RECIP_DEVICE:
John Youn38beaec2017-01-17 20:31:13 -08001476 /*
1477 * bit 0 => self powered
1478 * bit 1 => remote wakeup
1479 */
1480 reply = cpu_to_le16(0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001481 break;
1482
1483 case USB_RECIP_INTERFACE:
1484 /* currently, the data result should be zero */
1485 reply = cpu_to_le16(0);
1486 break;
1487
1488 case USB_RECIP_ENDPOINT:
1489 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1490 if (!ep)
1491 return -ENOENT;
1492
1493 reply = cpu_to_le16(ep->halted ? 1 : 0);
1494 break;
1495
1496 default:
1497 return 0;
1498 }
1499
1500 if (le16_to_cpu(ctrl->wLength) != 2)
1501 return -EINVAL;
1502
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001503 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001504 if (ret) {
1505 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1506 return ret;
1507 }
1508
1509 return 1;
1510}
1511
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001512static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001513
1514/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001515 * get_ep_head - return the first request on the endpoint
1516 * @hs_ep: The controller endpoint to get
1517 *
1518 * Get the first request on the endpoint.
1519 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001520static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001521{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001522 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1523 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001524}
1525
1526/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001527 * dwc2_gadget_start_next_request - Starts next request from ep queue
1528 * @hs_ep: Endpoint structure
1529 *
1530 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1531 * in its handler. Hence we need to unmask it here to be able to do
1532 * resynchronization.
1533 */
1534static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1535{
1536 u32 mask;
1537 struct dwc2_hsotg *hsotg = hs_ep->parent;
1538 int dir_in = hs_ep->dir_in;
1539 struct dwc2_hsotg_req *hs_req;
1540 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1541
1542 if (!list_empty(&hs_ep->queue)) {
1543 hs_req = get_ep_head(hs_ep);
1544 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1545 return;
1546 }
1547 if (!hs_ep->isochronous)
1548 return;
1549
1550 if (dir_in) {
1551 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1552 __func__);
1553 } else {
1554 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1555 __func__);
1556 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1557 mask |= DOEPMSK_OUTTKNEPDISMSK;
1558 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1559 }
1560}
1561
1562/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001563 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001564 * @hsotg: The device state
1565 * @ctrl: USB control request
1566 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001567static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001568 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001569{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001570 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1571 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001572 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001573 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001574 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001575 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001576 u32 recip;
1577 u32 wValue;
1578 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001579
1580 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1581 __func__, set ? "SET" : "CLEAR");
1582
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001583 wValue = le16_to_cpu(ctrl->wValue);
1584 wIndex = le16_to_cpu(ctrl->wIndex);
1585 recip = ctrl->bRequestType & USB_RECIP_MASK;
1586
1587 switch (recip) {
1588 case USB_RECIP_DEVICE:
1589 switch (wValue) {
1590 case USB_DEVICE_TEST_MODE:
1591 if ((wIndex & 0xff) != 0)
1592 return -EINVAL;
1593 if (!set)
1594 return -EINVAL;
1595
1596 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001597 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001598 if (ret) {
1599 dev_err(hsotg->dev,
1600 "%s: failed to send reply\n", __func__);
1601 return ret;
1602 }
1603 break;
1604 default:
1605 return -ENOENT;
1606 }
1607 break;
1608
1609 case USB_RECIP_ENDPOINT:
1610 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001611 if (!ep) {
1612 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001613 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001614 return -ENOENT;
1615 }
1616
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001617 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001618 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001619 halted = ep->halted;
1620
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001621 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001622
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001623 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001624 if (ret) {
1625 dev_err(hsotg->dev,
1626 "%s: failed to send reply\n", __func__);
1627 return ret;
1628 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001629
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001630 /*
1631 * we have to complete all requests for ep if it was
1632 * halted, and the halt was cleared by CLEAR_FEATURE
1633 */
1634
1635 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001636 /*
1637 * If we have request in progress,
1638 * then complete it
1639 */
1640 if (ep->req) {
1641 hs_req = ep->req;
1642 ep->req = NULL;
1643 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001644 if (hs_req->req.complete) {
1645 spin_unlock(&hsotg->lock);
1646 usb_gadget_giveback_request(
1647 &ep->ep, &hs_req->req);
1648 spin_lock(&hsotg->lock);
1649 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001650 }
1651
1652 /* If we have pending request, then start it */
John Youn34c0887f2017-01-17 20:31:43 -08001653 if (!ep->req)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001654 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001655 }
1656
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001657 break;
1658
1659 default:
1660 return -ENOENT;
1661 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001662 break;
1663 default:
1664 return -ENOENT;
1665 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001666 return 1;
1667}
1668
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001669static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001670
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001671/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001672 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001673 * @hsotg: The device state
1674 *
1675 * Set stall for ep0 as response for setup request.
1676 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001677static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001678{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001679 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001680 u32 reg;
1681 u32 ctrl;
1682
1683 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1684 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1685
1686 /*
1687 * DxEPCTL_Stall will be cleared by EP once it has
1688 * taken effect, so no need to clear later.
1689 */
1690
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001691 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001692 ctrl |= DXEPCTL_STALL;
1693 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001694 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001695
1696 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001697 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001698 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001699
1700 /*
1701 * complete won't be called, so we enqueue
1702 * setup request here
1703 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001704 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001705}
1706
1707/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001708 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001709 * @hsotg: The device state
1710 * @ctrl: The control request received
1711 *
1712 * The controller has received the SETUP phase of a control request, and
1713 * needs to work out what to do next (and whether to pass it on to the
1714 * gadget driver).
1715 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001716static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001717 struct usb_ctrlrequest *ctrl)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001718{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001719 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001720 int ret = 0;
1721 u32 dcfg;
1722
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001723 dev_dbg(hsotg->dev,
1724 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1725 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1726 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001727
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001728 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001729 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001730 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1731 } else if (ctrl->bRequestType & USB_DIR_IN) {
1732 ep0->dir_in = 1;
1733 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1734 } else {
1735 ep0->dir_in = 0;
1736 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1737 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001738
1739 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1740 switch (ctrl->bRequest) {
1741 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001742 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001743 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001744 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001745 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1746 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001747 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001748
1749 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1750
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001751 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001752 return;
1753
1754 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001755 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001756 break;
1757
1758 case USB_REQ_CLEAR_FEATURE:
1759 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001760 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001761 break;
1762 }
1763 }
1764
1765 /* as a fallback, try delivering it to the driver to deal with */
1766
1767 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001768 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001769 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001770 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001771 if (ret < 0)
1772 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1773 }
1774
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001775 /*
1776 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001777 * so respond with a STALL for the status stage to indicate failure.
1778 */
1779
Robert Baldygac9f721b2014-01-14 08:36:00 +01001780 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001781 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001782}
1783
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001784/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001785 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001786 * @ep: The endpoint the request was on.
1787 * @req: The request completed.
1788 *
1789 * Called on completion of any requests the driver itself submitted for
1790 * EP0 setup packets
1791 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001792static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08001793 struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001794{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001795 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001796 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001797
1798 if (req->status < 0) {
1799 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1800 return;
1801 }
1802
Robert Baldyga93f599f2013-11-21 13:49:17 +01001803 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001804 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001805 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001806 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001807 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001808 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001809}
1810
1811/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001812 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001813 * @hsotg: The device state.
1814 *
1815 * Enqueue a request on EP0 if necessary to received any SETUP packets
1816 * received from the host.
1817 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001818static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001819{
1820 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001821 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001822 int ret;
1823
1824 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1825
1826 req->zero = 0;
1827 req->length = 8;
1828 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001829 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001830
1831 if (!list_empty(&hs_req->queue)) {
1832 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1833 return;
1834 }
1835
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001836 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001837 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001838 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001839
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001840 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001841 if (ret < 0) {
1842 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001843 /*
1844 * Don't think there's much we can do other than watch the
1845 * driver fail.
1846 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001847 }
1848}
1849
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001850static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001851 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001852{
1853 u32 ctrl;
1854 u8 index = hs_ep->index;
1855 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1856 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1857
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001858 if (hs_ep->dir_in)
1859 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001860 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001861 else
1862 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001863 index);
1864 if (using_desc_dma(hsotg)) {
1865 /* Not specific buffer needed for ep0 ZLP */
1866 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001867
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001868 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1869 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1870 } else {
1871 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1872 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1873 epsiz_reg);
1874 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001875
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001876 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001877 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1878 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1879 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001880 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001881}
1882
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001883/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001884 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001885 * @hsotg: The device state.
1886 * @hs_ep: The endpoint the request was on.
1887 * @hs_req: The request to complete.
1888 * @result: The result code (0 => Ok, otherwise errno)
1889 *
1890 * The given request has finished, so call the necessary completion
1891 * if it has one and then look to see if we can start a new request
1892 * on the endpoint.
1893 *
1894 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001895 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001896static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08001897 struct dwc2_hsotg_ep *hs_ep,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001898 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001899 int result)
1900{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001901 if (!hs_req) {
1902 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1903 return;
1904 }
1905
1906 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1907 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1908
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001909 /*
1910 * only replace the status if we've not already set an error
1911 * from a previous transaction
1912 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001913
1914 if (hs_req->req.status == -EINPROGRESS)
1915 hs_req->req.status = result;
1916
Yunzhi Li44583fe2015-09-29 12:25:01 +02001917 if (using_dma(hsotg))
1918 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1919
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001920 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001921
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001922 hs_ep->req = NULL;
1923 list_del_init(&hs_req->queue);
1924
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001925 /*
1926 * call the complete request with the locks off, just in case the
1927 * request tries to queue more work for this endpoint.
1928 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001929
1930 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001931 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001932 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001933 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001934 }
1935
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001936 /* In DDMA don't need to proceed to starting of next ISOC request */
1937 if (using_desc_dma(hsotg) && hs_ep->isochronous)
1938 return;
1939
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001940 /*
1941 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001942 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001943 * so be careful when doing this.
1944 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001945
John Youn34c0887f2017-01-17 20:31:43 -08001946 if (!hs_ep->req && result >= 0)
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001947 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001948}
1949
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001950/*
1951 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
1952 * @hs_ep: The endpoint the request was on.
1953 *
1954 * Get first request from the ep queue, determine descriptor on which complete
1955 * happened. SW based on isoc_chain_num discovers which half of the descriptor
1956 * chain is currently in use by HW, adjusts dma_address and calculates index
1957 * of completed descriptor based on the value of DEPDMA register. Update actual
1958 * length of request, giveback to gadget.
1959 */
1960static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
1961{
1962 struct dwc2_hsotg *hsotg = hs_ep->parent;
1963 struct dwc2_hsotg_req *hs_req;
1964 struct usb_request *ureq;
1965 int index;
1966 dma_addr_t dma_addr;
1967 u32 dma_reg;
1968 u32 depdma;
1969 u32 desc_sts;
1970 u32 mask;
1971
1972 hs_req = get_ep_head(hs_ep);
1973 if (!hs_req) {
1974 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
1975 return;
1976 }
1977 ureq = &hs_req->req;
1978
1979 dma_addr = hs_ep->desc_list_dma;
1980
1981 /*
1982 * If lower half of descriptor chain is currently use by SW,
1983 * that means higher half is being processed by HW, so shift
1984 * DMA address to higher half of descriptor chain.
1985 */
1986 if (!hs_ep->isoc_chain_num)
1987 dma_addr += sizeof(struct dwc2_dma_desc) *
1988 (MAX_DMA_DESC_NUM_GENERIC / 2);
1989
1990 dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index);
1991 depdma = dwc2_readl(hsotg->regs + dma_reg);
1992
1993 index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1;
1994 desc_sts = hs_ep->desc_list[index].status;
1995
1996 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
1997 DEV_DMA_ISOC_RX_NBYTES_MASK;
1998 ureq->actual = ureq->length -
1999 ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT);
2000
Vahram Aharonyan95d2b032016-11-14 19:16:46 -08002001 /* Adjust actual length for ISOC Out if length is not align of 4 */
2002 if (!hs_ep->dir_in && ureq->length & 0x3)
2003 ureq->actual += 4 - (ureq->length & 0x3);
2004
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002005 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2006}
2007
2008/*
2009 * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any.
2010 * @hs_ep: The isochronous endpoint to be re-enabled.
2011 *
2012 * If ep has been disabled due to last descriptor servicing (IN endpoint) or
2013 * BNA (OUT endpoint) check the status of other half of descriptor chain that
2014 * was under SW control till HW was busy and restart the endpoint if needed.
2015 */
2016static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
2017{
2018 struct dwc2_hsotg *hsotg = hs_ep->parent;
2019 u32 depctl;
2020 u32 dma_reg;
2021 u32 ctrl;
2022 u32 dma_addr = hs_ep->desc_list_dma;
2023 unsigned char index = hs_ep->index;
2024
2025 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
2026 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
2027
2028 ctrl = dwc2_readl(hsotg->regs + depctl);
2029
2030 /*
2031 * EP was disabled if HW has processed last descriptor or BNA was set.
2032 * So restart ep if SW has prepared new descriptor chain in ep_queue
2033 * routine while HW was busy.
2034 */
2035 if (!(ctrl & DXEPCTL_EPENA)) {
2036 if (!hs_ep->next_desc) {
2037 dev_dbg(hsotg->dev, "%s: No more ISOC requests\n",
2038 __func__);
2039 return;
2040 }
2041
2042 dma_addr += sizeof(struct dwc2_dma_desc) *
2043 (MAX_DMA_DESC_NUM_GENERIC / 2) *
2044 hs_ep->isoc_chain_num;
2045 dwc2_writel(dma_addr, hsotg->regs + dma_reg);
2046
2047 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
2048 dwc2_writel(ctrl, hsotg->regs + depctl);
2049
2050 /* Switch ISOC descriptor chain number being processed by SW*/
2051 hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
2052 hs_ep->next_desc = 0;
2053
2054 dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n",
2055 __func__);
2056 }
2057}
2058
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002059/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002060 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002061 * @hsotg: The device state.
2062 * @ep_idx: The endpoint index for the data
2063 * @size: The size of data in the fifo, in bytes
2064 *
2065 * The FIFO status shows there is data to read from the FIFO for a given
2066 * endpoint, so sort out whether we need to read the data into a request
2067 * that has been made for that endpoint.
2068 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002069static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002070{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002071 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2072 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002073 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002074 int to_read;
2075 int max_req;
2076 int read_ptr;
2077
2078 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002079 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002080 int ptr;
2081
Robert Baldyga6b448af42014-12-16 11:51:44 +01002082 dev_dbg(hsotg->dev,
John Youn9da51972017-01-17 20:30:27 -08002083 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002084 __func__, size, ep_idx, epctl);
2085
2086 /* dump the data from the FIFO, we've nothing we can do */
2087 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002088 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002089
2090 return;
2091 }
2092
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002093 to_read = size;
2094 read_ptr = hs_req->req.actual;
2095 max_req = hs_req->req.length - read_ptr;
2096
Ben Dooksa33e7132010-07-19 09:40:49 +01002097 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2098 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2099
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002100 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002101 /*
2102 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002103 * to deal with in this request.
2104 */
2105
2106 /* currently we don't deal this */
2107 WARN_ON_ONCE(1);
2108 }
2109
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002110 hs_ep->total_data += to_read;
2111 hs_req->req.actual += to_read;
2112 to_read = DIV_ROUND_UP(to_read, 4);
2113
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002114 /*
2115 * note, we might over-write the buffer end by 3 bytes depending on
2116 * alignment of the data.
2117 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05002118 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002119}
2120
2121/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002122 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002123 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002124 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002125 *
2126 * Generate a zero-length IN packet request for terminating a SETUP
2127 * transaction.
2128 *
2129 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002130 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002131 * the TxFIFO.
2132 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002133static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002134{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002135 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002136 hsotg->eps_out[0]->dir_in = dir_in;
2137 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002138
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002139 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002140}
2141
Roman Bacikec1f9d92015-09-10 18:13:43 -07002142static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002143 u32 epctl_reg)
Roman Bacikec1f9d92015-09-10 18:13:43 -07002144{
2145 u32 ctrl;
2146
2147 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
2148 if (ctrl & DXEPCTL_EOFRNUM)
2149 ctrl |= DXEPCTL_SETEVENFR;
2150 else
2151 ctrl |= DXEPCTL_SETODDFR;
2152 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
2153}
2154
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002155/*
2156 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2157 * @hs_ep - The endpoint on which transfer went
2158 *
2159 * Iterate over endpoints descriptor chain and get info on bytes remained
2160 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2161 */
2162static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2163{
2164 struct dwc2_hsotg *hsotg = hs_ep->parent;
2165 unsigned int bytes_rem = 0;
2166 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2167 int i;
2168 u32 status;
2169
2170 if (!desc)
2171 return -EINVAL;
2172
2173 for (i = 0; i < hs_ep->desc_count; ++i) {
2174 status = desc->status;
2175 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2176
2177 if (status & DEV_DMA_STS_MASK)
2178 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2179 i, status & DEV_DMA_STS_MASK);
2180 }
2181
2182 return bytes_rem;
2183}
2184
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002185/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002186 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002187 * @hsotg: The device instance
2188 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002189 *
2190 * The RXFIFO has delivered an OutDone event, which means that the data
2191 * transfer for an OUT endpoint has been completed, either by a short
2192 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002193 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002194static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002195{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002196 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002197 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2198 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002199 struct usb_request *req = &hs_req->req;
John Youn9da51972017-01-17 20:30:27 -08002200 unsigned int size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002201 int result = 0;
2202
2203 if (!hs_req) {
2204 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2205 return;
2206 }
2207
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002208 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2209 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002210 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2211 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002212 return;
2213 }
2214
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002215 if (using_desc_dma(hsotg))
2216 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2217
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002218 if (using_dma(hsotg)) {
John Youn9da51972017-01-17 20:30:27 -08002219 unsigned int size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002220
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002221 /*
2222 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002223 * is left in the endpoint size register and then working it
2224 * out from the amount we loaded for the transfer.
2225 *
2226 * We need to do this as DMA pointers are always 32bit aligned
2227 * so may overshoot/undershoot the transfer.
2228 */
2229
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002230 size_done = hs_ep->size_loaded - size_left;
2231 size_done += hs_ep->last_load;
2232
2233 req->actual = size_done;
2234 }
2235
Ben Dooksa33e7132010-07-19 09:40:49 +01002236 /* if there is more request to do, schedule new transfer */
2237 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002238 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002239 return;
2240 }
2241
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002242 if (req->actual < req->length && req->short_not_ok) {
2243 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2244 __func__, req->actual, req->length);
2245
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002246 /*
2247 * todo - what should we return here? there's no one else
2248 * even bothering to check the status.
2249 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002250 }
2251
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002252 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2253 if (!using_desc_dma(hsotg) && epnum == 0 &&
2254 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002255 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002256 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002257 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002258 }
2259
Roman Bacikec1f9d92015-09-10 18:13:43 -07002260 /*
2261 * Slave mode OUT transfers do not go through XferComplete so
2262 * adjust the ISOC parity here.
2263 */
2264 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002265 if (hs_ep->isochronous && hs_ep->interval == 1)
2266 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002267 else if (hs_ep->isochronous && hs_ep->interval > 1)
2268 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002269 }
2270
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002271 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002272}
2273
2274/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002275 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002276 * @hsotg: The device instance
2277 *
2278 * The IRQ handler has detected that the RX FIFO has some data in it
2279 * that requires processing, so find out what is in there and do the
2280 * appropriate read.
2281 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002282 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002283 * chunks, so if you have x packets received on an endpoint you'll get x
2284 * FIFO events delivered, each with a packet's worth of data in it.
2285 *
2286 * When using DMA, we should not be processing events from the RXFIFO
2287 * as the actual data should be sent to the memory directly and we turn
2288 * on the completion interrupts to get notifications of transfer completion.
2289 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002290static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002291{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002292 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002293 u32 epnum, status, size;
2294
2295 WARN_ON(using_dma(hsotg));
2296
Dinh Nguyen47a16852014-04-14 14:13:34 -07002297 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2298 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002299
Dinh Nguyen47a16852014-04-14 14:13:34 -07002300 size = grxstsr & GRXSTS_BYTECNT_MASK;
2301 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002302
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002303 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
John Youn9da51972017-01-17 20:30:27 -08002304 __func__, grxstsr, size, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002305
Dinh Nguyen47a16852014-04-14 14:13:34 -07002306 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2307 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2308 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002309 break;
2310
Dinh Nguyen47a16852014-04-14 14:13:34 -07002311 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002312 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002313 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002314
2315 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002316 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002317 break;
2318
Dinh Nguyen47a16852014-04-14 14:13:34 -07002319 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002320 dev_dbg(hsotg->dev,
2321 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002322 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002323 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002324 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002325 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002326 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2327 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2328 */
2329 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002330 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002331 break;
2332
Dinh Nguyen47a16852014-04-14 14:13:34 -07002333 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002334 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002335 break;
2336
Dinh Nguyen47a16852014-04-14 14:13:34 -07002337 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002338 dev_dbg(hsotg->dev,
2339 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002340 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002341 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002342
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002343 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2344
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002345 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002346 break;
2347
2348 default:
2349 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2350 __func__, grxstsr);
2351
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002352 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002353 break;
2354 }
2355}
2356
2357/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002358 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002359 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002360 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002361static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362{
2363 switch (mps) {
2364 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002365 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002367 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002368 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002369 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002370 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002371 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002372 }
2373
2374 /* bad max packet size, warn and return invalid result */
2375 WARN_ON(1);
2376 return (u32)-1;
2377}
2378
2379/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002380 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002381 * @hsotg: The driver state.
2382 * @ep: The index number of the endpoint
2383 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002384 * @mc: The multicount value
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002385 *
2386 * Configure the maximum packet size for the given endpoint, updating
2387 * the hardware control registers to reflect this.
2388 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002389static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002390 unsigned int ep, unsigned int mps,
2391 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002392{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002393 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002394 void __iomem *regs = hsotg->regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002395 u32 reg;
2396
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002397 hs_ep = index_to_ep(hsotg, ep, dir_in);
2398 if (!hs_ep)
2399 return;
2400
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002401 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002402 u32 mps_bytes = mps;
2403
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002404 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002405 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2406 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002407 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002408 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002409 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002410 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002411 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002412 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002413 hs_ep->mc = mc;
2414 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002415 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002416 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002417 }
2418
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002419 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002420 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002421 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002422 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002423 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002424 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002425 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002426 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002427 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002428 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002429 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002430
2431 return;
2432
2433bad_mps:
2434 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2435}
2436
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002437/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002438 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002439 * @hsotg: The driver state
2440 * @idx: The index for the endpoint (0..15)
2441 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002442static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002443{
2444 int timeout;
2445 int val;
2446
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002447 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2448 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002449
2450 /* wait until the fifo is flushed */
2451 timeout = 100;
2452
2453 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002454 val = dwc2_readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002455
Dinh Nguyen47a16852014-04-14 14:13:34 -07002456 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002457 break;
2458
2459 if (--timeout == 0) {
2460 dev_err(hsotg->dev,
2461 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
2462 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02002463 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002464 }
2465
2466 udelay(1);
2467 }
2468}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002469
2470/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002471 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002472 * @hsotg: The driver state
2473 * @hs_ep: The driver endpoint to check.
2474 *
2475 * Check to see if there is a request that has data to send, and if so
2476 * make an attempt to write data into the FIFO.
2477 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002478static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002479 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002480{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002481 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002482
Robert Baldygaafcf4162013-09-19 11:50:19 +02002483 if (!hs_ep->dir_in || !hs_req) {
2484 /**
2485 * if request is not enqueued, we disable interrupts
2486 * for endpoints, excepting ep0
2487 */
2488 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002489 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
John Youn9da51972017-01-17 20:30:27 -08002490 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002491 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002492 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002493
2494 if (hs_req->req.actual < hs_req->req.length) {
2495 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2496 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002497 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498 }
2499
2500 return 0;
2501}
2502
2503/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002504 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002505 * @hsotg: The device state.
2506 * @hs_ep: The endpoint that has just completed.
2507 *
2508 * An IN transfer has been completed, update the transfer's state and then
2509 * call the relevant completion routines.
2510 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002511static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08002512 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002513{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002514 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002515 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002516 int size_left, size_done;
2517
2518 if (!hs_req) {
2519 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2520 return;
2521 }
2522
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002523 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002524 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2525 dev_dbg(hsotg->dev, "zlp packet sent\n");
Razmik Karapetyanc3b22fe2016-11-16 15:33:57 -08002526
2527 /*
2528 * While send zlp for DWC2_EP0_STATUS_IN EP direction was
2529 * changed to IN. Change back to complete OUT transfer request
2530 */
2531 hs_ep->dir_in = 0;
2532
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002533 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002534 if (hsotg->test_mode) {
2535 int ret;
2536
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002537 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002538 if (ret < 0) {
2539 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
John Youn9da51972017-01-17 20:30:27 -08002540 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002541 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002542 return;
2543 }
2544 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002545 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002546 return;
2547 }
2548
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002549 /*
2550 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002551 * in the endpoint size register and then working it out from
2552 * the amount we loaded for the transfer.
2553 *
2554 * We do this even for DMA, as the transfer may have incremented
2555 * past the end of the buffer (DMA transfers are always 32bit
2556 * aligned).
2557 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002558 if (using_desc_dma(hsotg)) {
2559 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2560 if (size_left < 0)
2561 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2562 size_left);
2563 } else {
2564 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2565 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002566
2567 size_done = hs_ep->size_loaded - size_left;
2568 size_done += hs_ep->last_load;
2569
2570 if (hs_req->req.actual != size_done)
2571 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2572 __func__, hs_req->req.actual, size_done);
2573
2574 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002575 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2576 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002577
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002578 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2579 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002580 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002581 return;
2582 }
2583
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002584 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002585 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002586 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002587 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002588 /* transfer will be completed on next complete interrupt */
2589 return;
2590 }
2591
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002592 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2593 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002594 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002595 return;
2596 }
2597
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002598 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002599}
2600
2601/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002602 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2603 * @hsotg: The device state.
2604 * @idx: Index of ep.
2605 * @dir_in: Endpoint direction 1-in 0-out.
2606 *
2607 * Reads for endpoint with given index and direction, by masking
2608 * epint_reg with coresponding mask.
2609 */
2610static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2611 unsigned int idx, int dir_in)
2612{
2613 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2614 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2615 u32 ints;
2616 u32 mask;
2617 u32 diepempmsk;
2618
2619 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2620 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2621 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2622 mask |= DXEPINT_SETUP_RCVD;
2623
2624 ints = dwc2_readl(hsotg->regs + epint_reg);
2625 ints &= mask;
2626 return ints;
2627}
2628
2629/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002630 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2631 * @hs_ep: The endpoint on which interrupt is asserted.
2632 *
2633 * This interrupt indicates that the endpoint has been disabled per the
2634 * application's request.
2635 *
2636 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2637 * in case of ISOC completes current request.
2638 *
2639 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2640 * request starts it.
2641 */
2642static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2643{
2644 struct dwc2_hsotg *hsotg = hs_ep->parent;
2645 struct dwc2_hsotg_req *hs_req;
2646 unsigned char idx = hs_ep->index;
2647 int dir_in = hs_ep->dir_in;
2648 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2649 int dctl = dwc2_readl(hsotg->regs + DCTL);
2650
2651 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2652
2653 if (dir_in) {
2654 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2655
2656 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2657
2658 if (hs_ep->isochronous) {
2659 dwc2_hsotg_complete_in(hsotg, hs_ep);
2660 return;
2661 }
2662
2663 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2664 int dctl = dwc2_readl(hsotg->regs + DCTL);
2665
2666 dctl |= DCTL_CGNPINNAK;
2667 dwc2_writel(dctl, hsotg->regs + DCTL);
2668 }
2669 return;
2670 }
2671
2672 if (dctl & DCTL_GOUTNAKSTS) {
2673 dctl |= DCTL_CGOUTNAK;
2674 dwc2_writel(dctl, hsotg->regs + DCTL);
2675 }
2676
2677 if (!hs_ep->isochronous)
2678 return;
2679
2680 if (list_empty(&hs_ep->queue)) {
2681 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2682 __func__, hs_ep);
2683 return;
2684 }
2685
2686 do {
2687 hs_req = get_ep_head(hs_ep);
2688 if (hs_req)
2689 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2690 -ENODATA);
2691 dwc2_gadget_incr_frame_num(hs_ep);
2692 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2693
2694 dwc2_gadget_start_next_request(hs_ep);
2695}
2696
2697/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002698 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2699 * @hs_ep: The endpoint on which interrupt is asserted.
2700 *
2701 * This is starting point for ISOC-OUT transfer, synchronization done with
2702 * first out token received from host while corresponding EP is disabled.
2703 *
2704 * Device does not know initial frame in which out token will come. For this
2705 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2706 * getting this interrupt SW starts calculation for next transfer frame.
2707 */
2708static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2709{
2710 struct dwc2_hsotg *hsotg = ep->parent;
2711 int dir_in = ep->dir_in;
2712 u32 doepmsk;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002713 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002714
2715 if (dir_in || !ep->isochronous)
2716 return;
2717
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002718 /*
2719 * Store frame in which irq was asserted here, as
2720 * it can change while completing request below.
2721 */
2722 tmp = dwc2_hsotg_read_frameno(hsotg);
2723
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002724 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
2725
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002726 if (using_desc_dma(hsotg)) {
2727 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2728 /* Start first ISO Out */
2729 ep->target_frame = tmp;
2730 dwc2_gadget_start_isoc_ddma(ep);
2731 }
2732 return;
2733 }
2734
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002735 if (ep->interval > 1 &&
2736 ep->target_frame == TARGET_FRAME_INITIAL) {
2737 u32 dsts;
2738 u32 ctrl;
2739
2740 dsts = dwc2_readl(hsotg->regs + DSTS);
2741 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2742 dwc2_gadget_incr_frame_num(ep);
2743
2744 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2745 if (ep->target_frame & 0x1)
2746 ctrl |= DXEPCTL_SETODDFR;
2747 else
2748 ctrl |= DXEPCTL_SETEVENFR;
2749
2750 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2751 }
2752
2753 dwc2_gadget_start_next_request(ep);
2754 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2755 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2756 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2757}
2758
2759/**
John Youn38beaec2017-01-17 20:31:13 -08002760 * dwc2_gadget_handle_nak - handle NAK interrupt
2761 * @hs_ep: The endpoint on which interrupt is asserted.
2762 *
2763 * This is starting point for ISOC-IN transfer, synchronization done with
2764 * first IN token received from host while corresponding EP is disabled.
2765 *
2766 * Device does not know when first one token will arrive from host. On first
2767 * token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2768 * and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2769 * sent in response to that as there was no data in FIFO. SW is basing on this
2770 * interrupt to obtain frame in which token has come and then based on the
2771 * interval calculates next frame for transfer.
2772 */
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002773static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2774{
2775 struct dwc2_hsotg *hsotg = hs_ep->parent;
2776 int dir_in = hs_ep->dir_in;
2777
2778 if (!dir_in || !hs_ep->isochronous)
2779 return;
2780
2781 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
2782 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002783
2784 if (using_desc_dma(hsotg)) {
2785 dwc2_gadget_start_isoc_ddma(hs_ep);
2786 return;
2787 }
2788
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002789 if (hs_ep->interval > 1) {
2790 u32 ctrl = dwc2_readl(hsotg->regs +
2791 DIEPCTL(hs_ep->index));
2792 if (hs_ep->target_frame & 0x1)
2793 ctrl |= DXEPCTL_SETODDFR;
2794 else
2795 ctrl |= DXEPCTL_SETEVENFR;
2796
2797 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2798 }
2799
2800 dwc2_hsotg_complete_request(hsotg, hs_ep,
2801 get_ep_head(hs_ep), 0);
2802 }
2803
2804 dwc2_gadget_incr_frame_num(hs_ep);
2805}
2806
2807/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002808 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002809 * @hsotg: The driver state
2810 * @idx: The index for the endpoint (0..15)
2811 * @dir_in: Set if this is an IN endpoint
2812 *
2813 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002814 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002815static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
John Youn9da51972017-01-17 20:30:27 -08002816 int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002817{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002818 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002819 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2820 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2821 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002822 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002823 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002824
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002825 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002826 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002827
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002828 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002829 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002830
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002831 if (!hs_ep) {
2832 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
John Youn9da51972017-01-17 20:30:27 -08002833 __func__, idx, dir_in ? "in" : "out");
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002834 return;
2835 }
2836
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002837 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2838 __func__, idx, dir_in ? "in" : "out", ints);
2839
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002840 /* Don't process XferCompl interrupt if it is a setup packet */
2841 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2842 ints &= ~DXEPINT_XFERCOMPL;
2843
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002844 /*
2845 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2846 * stage and xfercomplete was generated without SETUP phase done
2847 * interrupt. SW should parse received setup packet only after host's
2848 * exit from setup phase of control transfer.
2849 */
2850 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2851 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2852 ints &= ~DXEPINT_XFERCOMPL;
2853
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002854 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002855 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002856 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002857 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2858 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002859
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002860 /* In DDMA handle isochronous requests separately */
2861 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
2862 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
2863 /* Try to start next isoc request */
2864 dwc2_gadget_start_next_isoc_ddma(hs_ep);
2865 } else if (dir_in) {
2866 /*
2867 * We get OutDone from the FIFO, so we only
2868 * need to look at completing IN requests here
2869 * if operating slave mode
2870 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002871 if (hs_ep->isochronous && hs_ep->interval > 1)
2872 dwc2_gadget_incr_frame_num(hs_ep);
2873
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002874 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002875 if (ints & DXEPINT_NAKINTRPT)
2876 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002877
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002878 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002879 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002880 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002881 /*
2882 * We're using DMA, we need to fire an OutDone here
2883 * as we ignore the RXFIFO.
2884 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002885 if (hs_ep->isochronous && hs_ep->interval > 1)
2886 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002887
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002888 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002889 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002890 }
2891
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002892 if (ints & DXEPINT_EPDISBLD)
2893 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002894
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002895 if (ints & DXEPINT_OUTTKNEPDIS)
2896 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2897
2898 if (ints & DXEPINT_NAKINTRPT)
2899 dwc2_gadget_handle_nak(hs_ep);
2900
Dinh Nguyen47a16852014-04-14 14:13:34 -07002901 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002902 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002903
Dinh Nguyen47a16852014-04-14 14:13:34 -07002904 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002905 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2906
2907 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002908 /*
2909 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002910 * setup packet. In non-DMA mode we'd get this
2911 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002912 * the setup here.
2913 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002914
2915 if (dir_in)
2916 WARN_ON_ONCE(1);
2917 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002918 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002919 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002920 }
2921
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002922 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08002923 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
2924
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002925 /* Move to STATUS IN for DDMA */
2926 if (using_desc_dma(hsotg))
2927 dwc2_hsotg_ep0_zlp(hsotg, true);
2928 }
2929
Dinh Nguyen47a16852014-04-14 14:13:34 -07002930 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002931 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002932
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002933 if (ints & DXEPINT_BNAINTR) {
2934 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
2935
2936 /*
2937 * Try to start next isoc request, if any.
2938 * Sometimes the endpoint remains enabled after BNA interrupt
2939 * assertion, which is not expected, hence we can enter here
2940 * couple of times.
2941 */
2942 if (hs_ep->isochronous)
2943 dwc2_gadget_start_next_isoc_ddma(hs_ep);
2944 }
2945
Robert Baldyga1479e842013-10-09 08:41:57 +02002946 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002947 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002948 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002949 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2950 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002951 }
2952
2953 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002954 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002955 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2956 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002957 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002958
2959 /* FIFO has space or is empty (see GAHBCFG) */
2960 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002961 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002962 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2963 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002964 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002965 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002966 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002967 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002968}
2969
2970/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002971 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002972 * @hsotg: The device state.
2973 *
2974 * Handle updating the device settings after the enumeration phase has
2975 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002976 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002977static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002978{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002979 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09002980 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002981
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002982 /*
2983 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002984 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002985 * we connected at.
2986 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002987
2988 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2989
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002990 /*
2991 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002992 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002993 * not advertise a 64byte MPS on EP0.
2994 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002995
2996 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01002997 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002998 case DSTS_ENUMSPD_FS:
2999 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003000 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003001 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003002 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003003 break;
3004
Dinh Nguyen47a16852014-04-14 14:13:34 -07003005 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003006 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003007 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003008 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003009 break;
3010
Dinh Nguyen47a16852014-04-14 14:13:34 -07003011 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003012 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003013 ep0_mps = 8;
3014 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003015 /*
3016 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003017 * moment, and the documentation seems to imply that it isn't
3018 * supported by the PHYs on some of the devices.
3019 */
3020 break;
3021 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003022 dev_info(hsotg->dev, "new device is %s\n",
3023 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003024
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003025 /*
3026 * we should now know the maximum packet size for an
3027 * endpoint, so set the endpoints to a default value.
3028 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003029
3030 if (ep0_mps) {
3031 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003032 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003033 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3034 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003035 for (i = 1; i < hsotg->num_of_eps; i++) {
3036 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003037 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3038 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003039 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003040 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3041 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003042 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003043 }
3044
3045 /* ensure after enumeration our EP0 is active */
3046
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003047 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003048
3049 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003050 dwc2_readl(hsotg->regs + DIEPCTL0),
3051 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003052}
3053
3054/**
3055 * kill_all_requests - remove all requests from the endpoint's queue
3056 * @hsotg: The device state.
3057 * @ep: The endpoint the requests may be on.
3058 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003059 *
3060 * Go through the requests on the given endpoint and mark them
3061 * completed with the given result code.
3062 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003063static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003064 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af42014-12-16 11:51:44 +01003065 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003066{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003067 struct dwc2_hsotg_req *req, *treq;
John Youn9da51972017-01-17 20:30:27 -08003068 unsigned int size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003069
Robert Baldyga6b448af42014-12-16 11:51:44 +01003070 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003071
Robert Baldyga6b448af42014-12-16 11:51:44 +01003072 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003073 dwc2_hsotg_complete_request(hsotg, ep, req,
John Youn9da51972017-01-17 20:30:27 -08003074 result);
Robert Baldyga6b448af42014-12-16 11:51:44 +01003075
Robert Baldygab203d0a2014-09-09 10:44:56 +02003076 if (!hsotg->dedicated_fifos)
3077 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07003078 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003079 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003080 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003081}
3082
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003083/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003084 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003085 * @hsotg: The device state.
3086 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003087 * The device has been disconnected. Remove all current
3088 * transactions and signal the gadget driver that this
3089 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003090 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003091void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003092{
John Youn9da51972017-01-17 20:30:27 -08003093 unsigned int ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003094
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003095 if (!hsotg->connected)
3096 return;
3097
3098 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003099 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003100
3101 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3102 if (hsotg->eps_in[ep])
3103 kill_all_requests(hsotg, hsotg->eps_in[ep],
John Youn9da51972017-01-17 20:30:27 -08003104 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003105 if (hsotg->eps_out[ep])
3106 kill_all_requests(hsotg, hsotg->eps_out[ep],
John Youn9da51972017-01-17 20:30:27 -08003107 -ESHUTDOWN);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003108 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003109
3110 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003111 hsotg->lx_state = DWC2_L3;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003112}
3113
3114/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003115 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003116 * @hsotg: The device state:
3117 * @periodic: True if this is a periodic FIFO interrupt
3118 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003119static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003120{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003121 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003122 int epno, ret;
3123
3124 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003125 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003126 ep = index_to_ep(hsotg, epno, 1);
3127
3128 if (!ep)
3129 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003130
3131 if (!ep->dir_in)
3132 continue;
3133
3134 if ((periodic && !ep->periodic) ||
3135 (!periodic && ep->periodic))
3136 continue;
3137
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003138 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003139 if (ret < 0)
3140 break;
3141 }
3142}
3143
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003144/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003145#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3146 GINTSTS_PTXFEMP | \
3147 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003148
3149/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003150 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003151 * @hsotg: The device state
3152 *
3153 * Issue a soft reset to the core, and await the core finishing it.
3154 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003155void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08003156 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003157{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003158 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003159 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003160 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003161 u32 dcfg = 0;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003162
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003163 /* Kill any ep0 requests as controller will be reinitialized */
3164 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3165
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003166 if (!is_usb_reset)
John Youn241729b2015-12-17 11:17:59 -08003167 if (dwc2_core_reset(hsotg))
Gregory Herrero86de4892015-09-29 12:08:21 +02003168 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02003169
3170 /*
3171 * we must now enable ep0 ready for host detection and then
3172 * set configuration.
3173 */
3174
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003175 /* keep other bits untouched (so e.g. forced modes are not lost) */
3176 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3177 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3178 GUSBCFG_HNPCAP);
3179
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003180 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003181 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3182 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003183 /* FS/LS Dedicated Transceiver Interface */
3184 usbcfg |= GUSBCFG_PHYSEL;
3185 } else {
3186 /* set the PLL on, remove the HNP/SRP and set the PHY */
3187 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3188 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3189 (val << GUSBCFG_USBTRDTIM_SHIFT);
3190 }
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003191 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003192
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003193 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003194
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003195 if (!is_usb_reset)
3196 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003197
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003198 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003199
3200 switch (hsotg->params.speed) {
3201 case DWC2_SPEED_PARAM_LOW:
3202 dcfg |= DCFG_DEVSPD_LS;
3203 break;
3204 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003205 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3206 dcfg |= DCFG_DEVSPD_FS48;
3207 else
3208 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003209 break;
3210 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003211 dcfg |= DCFG_DEVSPD_HS;
3212 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003213
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003214 dwc2_writel(dcfg, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003215
3216 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003217 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003218
3219 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003220 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003221 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003222 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003223 GINTSTS_USBRST | GINTSTS_RESETDET |
3224 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003225 GINTSTS_USBSUSP | GINTSTS_WKUPINT;
3226
3227 if (!using_desc_dma(hsotg))
3228 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003229
John Younbea8e862016-11-03 17:55:53 -07003230 if (hsotg->params.external_id_pin_ctl <= 0)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003231 intmsk |= GINTSTS_CONIDSTSCHNG;
3232
3233 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003234
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003235 if (using_dma(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003236 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
3237 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
3238 hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003239
3240 /* Set DDMA mode support in the core if needed */
3241 if (using_desc_dma(hsotg))
3242 __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
3243
3244 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003245 dwc2_writel(((hsotg->dedicated_fifos) ?
3246 (GAHBCFG_NP_TXF_EMP_LVL |
3247 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3248 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003249 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003250
3251 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003252 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3253 * when we have no data to transfer. Otherwise we get being flooded by
3254 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003255 */
3256
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003257 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003258 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003259 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003260 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003261 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003262
3263 /*
3264 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003265 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003266 */
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003267 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3268 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003269 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003270 DOEPMSK_SETUPMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003271 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003272
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003273 /* Enable BNA interrupt for DDMA */
3274 if (using_desc_dma(hsotg))
3275 __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
3276
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003277 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003278
3279 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003280 dwc2_readl(hsotg->regs + DIEPCTL0),
3281 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003282
3283 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003284 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003285
3286 /*
3287 * Enable the RXFIFO when in slave mode, as this is how we collect
3288 * the data. In DMA mode, we get events from the FIFO but also
3289 * things we cannot process, so do not use it.
3290 */
3291 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003292 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003293
3294 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003295 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3296 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003297
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003298 if (!is_usb_reset) {
3299 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3300 udelay(10); /* see openiboot */
3301 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3302 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003303
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003304 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003305
3306 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003307 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003308 * writing to the EPCTL register..
3309 */
3310
3311 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003312 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003313 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003314
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003315 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003316 DXEPCTL_CNAK | DXEPCTL_EPENA |
3317 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003318 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003319
3320 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003321 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003322 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003323
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003324 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003325
3326 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003327 dwc2_readl(hsotg->regs + DIEPCTL0),
3328 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003329
3330 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003331 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3332 if (!is_usb_reset)
3333 val |= DCTL_SFTDISCON;
3334 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003335
3336 /* must be at-least 3ms to allow bus to see disconnect */
3337 mdelay(3);
3338
Gregory Herrero065d3932015-09-22 15:16:54 +02003339 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003340}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003341
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003342static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003343{
3344 /* set the soft-disconnect bit */
3345 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
3346}
3347
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003348void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003349{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003350 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003351 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003352}
3353
3354/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003355 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3356 * @hsotg: The device state:
3357 *
3358 * This interrupt indicates one of the following conditions occurred while
3359 * transmitting an ISOC transaction.
3360 * - Corrupted IN Token for ISOC EP.
3361 * - Packet not complete in FIFO.
3362 *
3363 * The following actions will be taken:
3364 * - Determine the EP
3365 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3366 */
3367static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3368{
3369 struct dwc2_hsotg_ep *hs_ep;
3370 u32 epctrl;
3371 u32 idx;
3372
3373 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3374
3375 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3376 hs_ep = hsotg->eps_in[idx];
3377 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
3378 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3379 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3380 epctrl |= DXEPCTL_SNAK;
3381 epctrl |= DXEPCTL_EPDIS;
3382 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3383 }
3384 }
3385
3386 /* Clear interrupt */
3387 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3388}
3389
3390/**
3391 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3392 * @hsotg: The device state:
3393 *
3394 * This interrupt indicates one of the following conditions occurred while
3395 * transmitting an ISOC transaction.
3396 * - Corrupted OUT Token for ISOC EP.
3397 * - Packet not complete in FIFO.
3398 *
3399 * The following actions will be taken:
3400 * - Determine the EP
3401 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3402 */
3403static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3404{
3405 u32 gintsts;
3406 u32 gintmsk;
3407 u32 epctrl;
3408 struct dwc2_hsotg_ep *hs_ep;
3409 int idx;
3410
3411 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3412
3413 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3414 hs_ep = hsotg->eps_out[idx];
3415 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3416 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3417 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3418 /* Unmask GOUTNAKEFF interrupt */
3419 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3420 gintmsk |= GINTSTS_GOUTNAKEFF;
3421 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3422
3423 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3424 if (!(gintsts & GINTSTS_GOUTNAKEFF))
3425 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3426 }
3427 }
3428
3429 /* Clear interrupt */
3430 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3431}
3432
3433/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003434 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003435 * @irq: The IRQ number triggered
3436 * @pw: The pw value when registered the handler.
3437 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003438static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003439{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003440 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003441 int retry_count = 8;
3442 u32 gintsts;
3443 u32 gintmsk;
3444
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003445 if (!dwc2_is_device_mode(hsotg))
3446 return IRQ_NONE;
3447
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003448 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003449irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003450 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3451 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003452
3453 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3454 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3455
3456 gintsts &= gintmsk;
3457
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003458 if (gintsts & GINTSTS_RESETDET) {
3459 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3460
3461 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3462
3463 /* This event must be used only if controller is suspended */
3464 if (hsotg->lx_state == DWC2_L2) {
3465 dwc2_exit_hibernation(hsotg, true);
3466 hsotg->lx_state = DWC2_L0;
3467 }
3468 }
3469
3470 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003471 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3472 u32 connected = hsotg->connected;
3473
3474 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3475 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3476 dwc2_readl(hsotg->regs + GNPTXSTS));
3477
3478 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3479
3480 /* Report disconnection if it is not already done. */
3481 dwc2_hsotg_disconnect(hsotg);
3482
3483 if (usb_status & GOTGCTL_BSESVLD && connected)
3484 dwc2_hsotg_core_init_disconnected(hsotg, true);
3485 }
3486
Dinh Nguyen47a16852014-04-14 14:13:34 -07003487 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003488 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003489
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003490 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003491 }
3492
Dinh Nguyen47a16852014-04-14 14:13:34 -07003493 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003494 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3495 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003496 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003497 int ep;
3498
Robert Baldyga7e804652013-09-19 11:50:20 +02003499 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003500 daint_out = daint >> DAINT_OUTEP_SHIFT;
3501 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003502
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003503 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3504
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003505 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3506 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003507 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003508 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003509 }
3510
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003511 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3512 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003513 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003514 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003515 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003516 }
3517
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003518 /* check both FIFOs */
3519
Dinh Nguyen47a16852014-04-14 14:13:34 -07003520 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003521 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3522
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003523 /*
3524 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003525 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003526 * it needs re-enabling
3527 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003528
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003529 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3530 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003531 }
3532
Dinh Nguyen47a16852014-04-14 14:13:34 -07003533 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003534 dev_dbg(hsotg->dev, "PTxFEmp\n");
3535
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003536 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003537
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003538 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3539 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003540 }
3541
Dinh Nguyen47a16852014-04-14 14:13:34 -07003542 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003543 /*
3544 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003545 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003546 * set.
3547 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003548
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003549 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003550 }
3551
Dinh Nguyen47a16852014-04-14 14:13:34 -07003552 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003553 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003554 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003555 }
3556
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003557 /*
3558 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003559 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003560 * the occurrence.
3561 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003562
Dinh Nguyen47a16852014-04-14 14:13:34 -07003563 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003564 u8 idx;
3565 u32 epctrl;
3566 u32 gintmsk;
3567 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003568
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003569 /* Mask this interrupt */
3570 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3571 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3572 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003573
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003574 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3575 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3576 hs_ep = hsotg->eps_out[idx];
3577 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3578
3579 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
3580 epctrl |= DXEPCTL_SNAK;
3581 epctrl |= DXEPCTL_EPDIS;
3582 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3583 }
3584 }
3585
3586 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003587 }
3588
Dinh Nguyen47a16852014-04-14 14:13:34 -07003589 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003590 dev_info(hsotg->dev, "GINNakEff triggered\n");
3591
Gregory Herrero3be99cd2015-12-07 12:07:31 +01003592 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003593
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003594 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003595 }
3596
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003597 if (gintsts & GINTSTS_INCOMPL_SOIN)
3598 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003599
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003600 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3601 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003602
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003603 /*
3604 * if we've had fifo events, we should try and go around the
3605 * loop again to see if there's any point in returning yet.
3606 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003607
3608 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
John Youn77b62002017-01-17 20:32:12 -08003609 goto irq_retry;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003610
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003611 spin_unlock(&hsotg->lock);
3612
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003613 return IRQ_HANDLED;
3614}
3615
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003616static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3617 u32 bit, u32 timeout)
3618{
3619 u32 i;
3620
3621 for (i = 0; i < timeout; i++) {
3622 if (dwc2_readl(hs_otg->regs + reg) & bit)
3623 return 0;
3624 udelay(1);
3625 }
3626
3627 return -ETIMEDOUT;
3628}
3629
3630static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3631 struct dwc2_hsotg_ep *hs_ep)
3632{
3633 u32 epctrl_reg;
3634 u32 epint_reg;
3635
3636 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3637 DOEPCTL(hs_ep->index);
3638 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3639 DOEPINT(hs_ep->index);
3640
3641 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3642 hs_ep->name);
3643
3644 if (hs_ep->dir_in) {
3645 if (hsotg->dedicated_fifos || hs_ep->periodic) {
3646 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3647 /* Wait for Nak effect */
3648 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3649 DXEPINT_INEPNAKEFF, 100))
3650 dev_warn(hsotg->dev,
3651 "%s: timeout DIEPINT.NAKEFF\n",
3652 __func__);
3653 } else {
3654 __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
3655 /* Wait for Nak effect */
3656 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3657 GINTSTS_GINNAKEFF, 100))
3658 dev_warn(hsotg->dev,
3659 "%s: timeout GINTSTS.GINNAKEFF\n",
3660 __func__);
3661 }
3662 } else {
3663 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3664 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3665
3666 /* Wait for global nak to take effect */
3667 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3668 GINTSTS_GOUTNAKEFF, 100))
3669 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3670 __func__);
3671 }
3672
3673 /* Disable ep */
3674 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3675
3676 /* Wait for ep to be disabled */
3677 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3678 dev_warn(hsotg->dev,
3679 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3680
3681 /* Clear EPDISBLD interrupt */
3682 __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
3683
3684 if (hs_ep->dir_in) {
3685 unsigned short fifo_index;
3686
3687 if (hsotg->dedicated_fifos || hs_ep->periodic)
3688 fifo_index = hs_ep->fifo_index;
3689 else
3690 fifo_index = 0;
3691
3692 /* Flush TX FIFO */
3693 dwc2_flush_tx_fifo(hsotg, fifo_index);
3694
3695 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3696 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
3697 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
3698
3699 } else {
3700 /* Remove global NAKs */
3701 __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
3702 }
3703}
3704
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003705/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003706 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003707 * @ep: The USB endpint to configure
3708 * @desc: The USB endpoint descriptor to configure with.
3709 *
3710 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003711 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003712static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
John Youn9da51972017-01-17 20:30:27 -08003713 const struct usb_endpoint_descriptor *desc)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003714{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003715 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003716 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003717 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003718 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003719 u32 epctrl_reg;
3720 u32 epctrl;
3721 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003722 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003723 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003724 unsigned int dir_in;
3725 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003726 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003727
3728 dev_dbg(hsotg->dev,
3729 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3730 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3731 desc->wMaxPacketSize, desc->bInterval);
3732
3733 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003734 if (index == 0) {
3735 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3736 return -EINVAL;
3737 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003738
3739 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3740 if (dir_in != hs_ep->dir_in) {
3741 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3742 return -EINVAL;
3743 }
3744
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003745 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003746 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003747
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003748 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003749
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003750 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003751 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003752
3753 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3754 __func__, epctrl, epctrl_reg);
3755
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003756 /* Allocate DMA descriptor chain for non-ctrl endpoints */
3757 if (using_desc_dma(hsotg)) {
3758 hs_ep->desc_list = dma_alloc_coherent(hsotg->dev,
3759 MAX_DMA_DESC_NUM_GENERIC *
3760 sizeof(struct dwc2_dma_desc),
Marek Szyprowski86e881e2016-12-01 10:02:11 +01003761 &hs_ep->desc_list_dma, GFP_ATOMIC);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003762 if (!hs_ep->desc_list) {
3763 ret = -ENOMEM;
3764 goto error2;
3765 }
3766 }
3767
Lukasz Majewski22258f42012-06-14 10:02:24 +02003768 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003769
Dinh Nguyen47a16852014-04-14 14:13:34 -07003770 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3771 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003772
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003773 /*
3774 * mark the endpoint as active, otherwise the core may ignore
3775 * transactions entirely for this endpoint
3776 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003777 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003778
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003779 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003780 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003781
3782 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003783 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003784 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003785 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003786 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003787
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003788 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3789 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003790 epctrl |= DXEPCTL_EPTYPE_ISO;
3791 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003792 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003793 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003794 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003795 hs_ep->isoc_chain_num = 0;
3796 hs_ep->next_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003797 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003798 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003799 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3800 mask |= DIEPMSK_NAKMSK;
3801 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3802 } else {
3803 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3804 mask |= DOEPMSK_OUTTKNEPDISMSK;
3805 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3806 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003807 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003808
3809 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003810 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003811 break;
3812
3813 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003814 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003815 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003816
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003817 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3818 hs_ep->interval = 1 << (desc->bInterval - 1);
3819
Dinh Nguyen47a16852014-04-14 14:13:34 -07003820 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003821 break;
3822
3823 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003824 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003825 break;
3826 }
3827
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003828 /*
3829 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003830 * a unique tx-fifo even if it is non-periodic.
3831 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003832 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003833 u32 fifo_index = 0;
3834 u32 fifo_size = UINT_MAX;
John Youn9da51972017-01-17 20:30:27 -08003835
3836 size = hs_ep->ep.maxpacket * hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003837 for (i = 1; i < hsotg->num_of_eps; ++i) {
John Youn9da51972017-01-17 20:30:27 -08003838 if (hsotg->fifo_map & (1 << i))
Robert Baldygab203d0a2014-09-09 10:44:56 +02003839 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003840 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
John Youn9da51972017-01-17 20:30:27 -08003841 val = (val >> FIFOSIZE_DEPTH_SHIFT) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003842 if (val < size)
3843 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003844 /* Search for smallest acceptable fifo */
3845 if (val < fifo_size) {
3846 fifo_size = val;
3847 fifo_index = i;
3848 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003849 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003850 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003851 dev_err(hsotg->dev,
3852 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303853 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003854 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303855 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003856 hsotg->fifo_map |= 1 << fifo_index;
3857 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3858 hs_ep->fifo_index = fifo_index;
3859 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003860 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003861
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003862 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003863 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003864 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003865
3866 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3867 __func__, epctrl);
3868
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003869 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003870 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003871 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003872
3873 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003874 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003875
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003876error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003877 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003878
3879error2:
3880 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
3881 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3882 sizeof(struct dwc2_dma_desc),
3883 hs_ep->desc_list, hs_ep->desc_list_dma);
3884 hs_ep->desc_list = NULL;
3885 }
3886
Julia Lawall19c190f2010-03-29 17:36:44 +02003887 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003888}
3889
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003890/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003891 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003892 * @ep: The endpoint to disable.
3893 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003894static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003895{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003896 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003897 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003898 int dir_in = hs_ep->dir_in;
3899 int index = hs_ep->index;
3900 unsigned long flags;
3901 u32 epctrl_reg;
3902 u32 ctrl;
3903
Marek Szyprowski1e011292014-09-09 10:44:54 +02003904 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003905
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003906 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003907 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3908 return -EINVAL;
3909 }
3910
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003911 /* Remove DMA memory allocated for non-control Endpoints */
3912 if (using_desc_dma(hsotg)) {
3913 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3914 sizeof(struct dwc2_dma_desc),
3915 hs_ep->desc_list, hs_ep->desc_list_dma);
3916 hs_ep->desc_list = NULL;
3917 }
3918
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003919 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003920
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003921 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003922
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003923 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003924
3925 if (ctrl & DXEPCTL_EPENA)
3926 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
3927
Dinh Nguyen47a16852014-04-14 14:13:34 -07003928 ctrl &= ~DXEPCTL_EPENA;
3929 ctrl &= ~DXEPCTL_USBACTEP;
3930 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003931
3932 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003933 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003934
3935 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003936 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003937
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003938 /* terminate all requests with shutdown */
3939 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3940
Robert Baldyga1c07b202016-08-29 13:39:00 -07003941 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3942 hs_ep->fifo_index = 0;
3943 hs_ep->fifo_size = 0;
3944
Lukasz Majewski22258f42012-06-14 10:02:24 +02003945 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003946 return 0;
3947}
3948
3949/**
3950 * on_list - check request is on the given endpoint
3951 * @ep: The endpoint to check.
3952 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003953 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003954static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003955{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003956 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003957
3958 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
3959 if (req == test)
3960 return true;
3961 }
3962
3963 return false;
3964}
3965
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003966/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003967 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003968 * @ep: The endpoint to dequeue.
3969 * @req: The request to be removed from a queue.
3970 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003971static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003972{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003973 struct dwc2_hsotg_req *hs_req = our_req(req);
3974 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003975 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003976 unsigned long flags;
3977
Marek Szyprowski1e011292014-09-09 10:44:54 +02003978 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003979
Lukasz Majewski22258f42012-06-14 10:02:24 +02003980 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003981
3982 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02003983 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003984 return -EINVAL;
3985 }
3986
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003987 /* Dequeue already started request */
3988 if (req == &hs_ep->req->req)
3989 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
3990
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003991 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02003992 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003993
3994 return 0;
3995}
3996
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003997/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003998 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003999 * @ep: The endpoint to set halt.
4000 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004001 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
4002 * the endpoint is busy processing requests.
4003 *
4004 * We need to stall the endpoint immediately if request comes from set_feature
4005 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004006 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004007static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004008{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004009 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004010 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004011 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004012 u32 epreg;
4013 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004014 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004015
4016 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4017
Robert Baldygac9f721b2014-01-14 08:36:00 +01004018 if (index == 0) {
4019 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004020 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004021 else
4022 dev_warn(hs->dev,
4023 "%s: can't clear halt on ep0\n", __func__);
4024 return 0;
4025 }
4026
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004027 if (hs_ep->isochronous) {
4028 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4029 return -EINVAL;
4030 }
4031
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004032 if (!now && value && !list_empty(&hs_ep->queue)) {
4033 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4034 ep->name);
4035 return -EAGAIN;
4036 }
4037
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004038 if (hs_ep->dir_in) {
4039 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004040 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004041
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004042 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004043 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004044 if (epctl & DXEPCTL_EPENA)
4045 epctl |= DXEPCTL_EPDIS;
4046 } else {
4047 epctl &= ~DXEPCTL_STALL;
4048 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4049 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004050 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004051 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004052 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004053 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004054 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004055 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004056 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004057
John Youn34c0887f2017-01-17 20:31:43 -08004058 if (value) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004059 epctl |= DXEPCTL_STALL;
John Youn34c0887f2017-01-17 20:31:43 -08004060 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004061 epctl &= ~DXEPCTL_STALL;
4062 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4063 if (xfertype == DXEPCTL_EPTYPE_BULK ||
John Youn9da51972017-01-17 20:30:27 -08004064 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
John Youn77b62002017-01-17 20:32:12 -08004065 epctl |= DXEPCTL_SETD0PID;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004066 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004067 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004068 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004069
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004070 hs_ep->halted = value;
4071
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004072 return 0;
4073}
4074
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004075/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004076 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004077 * @ep: The endpoint to set halt.
4078 * @value: Set or unset the halt.
4079 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004080static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004081{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004082 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004083 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004084 unsigned long flags = 0;
4085 int ret = 0;
4086
4087 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004088 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004089 spin_unlock_irqrestore(&hs->lock, flags);
4090
4091 return ret;
4092}
4093
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004094static struct usb_ep_ops dwc2_hsotg_ep_ops = {
4095 .enable = dwc2_hsotg_ep_enable,
4096 .disable = dwc2_hsotg_ep_disable,
4097 .alloc_request = dwc2_hsotg_ep_alloc_request,
4098 .free_request = dwc2_hsotg_ep_free_request,
4099 .queue = dwc2_hsotg_ep_queue_lock,
4100 .dequeue = dwc2_hsotg_ep_dequeue,
4101 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004102 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004103};
4104
4105/**
John Youn9da51972017-01-17 20:30:27 -08004106 * dwc2_hsotg_init - initialize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004107 * @hsotg: The driver state
4108 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004109static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004110{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004111 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004112 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004113 /* unmask subset of endpoint interrupts */
4114
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004115 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4116 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4117 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004118
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004119 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4120 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4121 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004122
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004123 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004124
4125 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07004126 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004127
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004128 /* setup fifos */
4129
4130 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004131 dwc2_readl(hsotg->regs + GRXFSIZ),
4132 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004133
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004134 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004135
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004136 /* keep other bits untouched (so e.g. forced modes are not lost) */
4137 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4138 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
4139 GUSBCFG_HNPCAP);
4140
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004141 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004142 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004143 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4144 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4145 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004146
Gregory Herrerof5090042015-01-09 13:38:47 +01004147 if (using_dma(hsotg))
4148 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004149}
4150
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004151/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004152 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004153 * @gadget: The usb gadget state
4154 * @driver: The usb gadget driver
4155 *
4156 * Perform initialization to prepare udc device and driver
4157 * to work.
4158 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004159static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
John Youn9da51972017-01-17 20:30:27 -08004160 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004161{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004162 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004163 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004164 int ret;
4165
4166 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004167 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004168 return -ENODEV;
4169 }
4170
4171 if (!driver) {
4172 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4173 return -EINVAL;
4174 }
4175
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004176 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004177 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004178
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004179 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004180 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4181 return -EINVAL;
4182 }
4183
4184 WARN_ON(hsotg->driver);
4185
4186 driver->driver.bus = NULL;
4187 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004188 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004189 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4190
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004191 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4192 ret = dwc2_lowlevel_hw_enable(hsotg);
4193 if (ret)
4194 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004195 }
4196
Gregory Herrerof6c01592015-01-09 13:38:41 +01004197 if (!IS_ERR_OR_NULL(hsotg->uphy))
4198 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004199
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004200 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004201 if (dwc2_hw_is_device(hsotg)) {
4202 dwc2_hsotg_init(hsotg);
4203 dwc2_hsotg_core_init_disconnected(hsotg, false);
4204 }
4205
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004206 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004207 spin_unlock_irqrestore(&hsotg->lock, flags);
4208
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004209 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004210
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004211 return 0;
4212
4213err:
4214 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004215 return ret;
4216}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004217
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004218/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004219 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004220 * @gadget: The usb gadget state
4221 * @driver: The usb gadget driver
4222 *
4223 * Stop udc hw block and stay tunned for future transmissions
4224 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004225static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004226{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004227 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004228 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004229 int ep;
4230
4231 if (!hsotg)
4232 return -ENODEV;
4233
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004234 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004235 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4236 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004237 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004238 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004239 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004240 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004241
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004242 spin_lock_irqsave(&hsotg->lock, flags);
4243
Marek Szyprowski32805c32014-10-20 12:45:33 +02004244 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004245 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004246 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004247
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004248 spin_unlock_irqrestore(&hsotg->lock, flags);
4249
Gregory Herrerof6c01592015-01-09 13:38:41 +01004250 if (!IS_ERR_OR_NULL(hsotg->uphy))
4251 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004252
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004253 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4254 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004255
4256 return 0;
4257}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004258
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004259/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004260 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004261 * @gadget: The usb gadget state
4262 *
4263 * Read the {micro} frame number
4264 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004265static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004266{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004267 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004268}
4269
Lukasz Majewskia188b682012-06-22 09:29:56 +02004270/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004271 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004272 * @gadget: The usb gadget state
4273 * @is_on: Current state of the USB PHY
4274 *
4275 * Connect/Disconnect the USB PHY pullup
4276 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004277static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004278{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004279 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004280 unsigned long flags = 0;
4281
Gregory Herrero77ba9112015-09-29 12:08:19 +02004282 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
John Youn9da51972017-01-17 20:30:27 -08004283 hsotg->op_state);
Gregory Herrero77ba9112015-09-29 12:08:19 +02004284
4285 /* Don't modify pullup state while in host mode */
4286 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4287 hsotg->enabled = is_on;
4288 return 0;
4289 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004290
4291 spin_lock_irqsave(&hsotg->lock, flags);
4292 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004293 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004294 dwc2_hsotg_core_init_disconnected(hsotg, false);
4295 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004296 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004297 dwc2_hsotg_core_disconnect(hsotg);
4298 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004299 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004300 }
4301
4302 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4303 spin_unlock_irqrestore(&hsotg->lock, flags);
4304
4305 return 0;
4306}
4307
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004308static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004309{
4310 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4311 unsigned long flags;
4312
4313 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4314 spin_lock_irqsave(&hsotg->lock, flags);
4315
Gregory Herrero61f72232015-09-29 12:08:28 +02004316 /*
4317 * If controller is hibernated, it must exit from hibernation
4318 * before being initialized / de-initialized
4319 */
4320 if (hsotg->lx_state == DWC2_L2)
4321 dwc2_exit_hibernation(hsotg, false);
4322
Gregory Herrero83d98222015-01-09 13:39:02 +01004323 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004324 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004325
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004326 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01004327 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004328 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004329 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004330 dwc2_hsotg_core_disconnect(hsotg);
4331 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004332 }
4333
4334 spin_unlock_irqrestore(&hsotg->lock, flags);
4335 return 0;
4336}
4337
Gregory Herrero596d6962015-01-09 13:39:08 +01004338/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004339 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004340 * @gadget: The usb gadget state
4341 * @mA: Amount of current
4342 *
4343 * Report how much power the device may consume to the phy.
4344 */
John Youn9da51972017-01-17 20:30:27 -08004345static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned int mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004346{
4347 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4348
4349 if (IS_ERR_OR_NULL(hsotg->uphy))
4350 return -ENOTSUPP;
4351 return usb_phy_set_power(hsotg->uphy, mA);
4352}
4353
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004354static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4355 .get_frame = dwc2_hsotg_gadget_getframe,
4356 .udc_start = dwc2_hsotg_udc_start,
4357 .udc_stop = dwc2_hsotg_udc_stop,
4358 .pullup = dwc2_hsotg_pullup,
4359 .vbus_session = dwc2_hsotg_vbus_session,
4360 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004361};
4362
4363/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004364 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004365 * @hsotg: The device state.
4366 * @hs_ep: The endpoint to be initialised.
4367 * @epnum: The endpoint number
4368 *
4369 * Initialise the given endpoint (as part of the probe and device state
4370 * creation) to give to the gadget driver. Setup the endpoint name, any
4371 * direction information and other state that may be required.
4372 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004373static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
John Youn9da51972017-01-17 20:30:27 -08004374 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004375 int epnum,
4376 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004377{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004378 char *dir;
4379
4380 if (epnum == 0)
4381 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004382 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004383 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004384 else
4385 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004386
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004387 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004388 hs_ep->index = epnum;
4389
4390 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4391
4392 INIT_LIST_HEAD(&hs_ep->queue);
4393 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4394
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004395 /* add to the list of endpoints known by the gadget driver */
4396 if (epnum)
4397 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4398
4399 hs_ep->parent = hsotg;
4400 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004401
4402 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4403 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4404 else
4405 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4406 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004407 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004408
Robert Baldyga29545222015-07-31 16:00:18 +02004409 if (epnum == 0) {
4410 hs_ep->ep.caps.type_control = true;
4411 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004412 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4413 hs_ep->ep.caps.type_iso = true;
4414 hs_ep->ep.caps.type_bulk = true;
4415 }
Robert Baldyga29545222015-07-31 16:00:18 +02004416 hs_ep->ep.caps.type_int = true;
4417 }
4418
4419 if (dir_in)
4420 hs_ep->ep.caps.dir_in = true;
4421 else
4422 hs_ep->ep.caps.dir_out = true;
4423
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004424 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004425 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004426 * to be something valid.
4427 */
4428
4429 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004430 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
John Youn9da51972017-01-17 20:30:27 -08004431
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004432 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004433 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004434 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004435 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004436 }
4437}
4438
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004439/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004440 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004441 * @param: The device state
4442 *
4443 * Read the USB core HW configuration registers
4444 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004445static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004446{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004447 u32 cfg;
4448 u32 ep_type;
4449 u32 i;
4450
Ben Dooks10aebc72010-07-19 09:40:44 +01004451 /* check hardware configuration */
4452
John Youn43e90342015-12-17 11:17:45 -08004453 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4454
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004455 /* Add ep0 */
4456 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004457
John Younb98866c2017-01-17 20:31:58 -08004458 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev,
4459 sizeof(struct dwc2_hsotg_ep),
4460 GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004461 if (!hsotg->eps_in[0])
4462 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004463 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004464 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004465
John Youn43e90342015-12-17 11:17:45 -08004466 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004467 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004468 ep_type = cfg & 3;
4469 /* Direction in or both */
4470 if (!(ep_type & 2)) {
4471 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004472 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004473 if (!hsotg->eps_in[i])
4474 return -ENOMEM;
4475 }
4476 /* Direction out or both */
4477 if (!(ep_type & 1)) {
4478 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004479 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004480 if (!hsotg->eps_out[i])
4481 return -ENOMEM;
4482 }
4483 }
4484
John Youn43e90342015-12-17 11:17:45 -08004485 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4486 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004487
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004488 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4489 hsotg->num_of_eps,
4490 hsotg->dedicated_fifos ? "dedicated" : "shared",
4491 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004492 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004493}
4494
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004495/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004496 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004497 * @param: The device state
4498 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004499static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004500{
Mark Brown83a01802011-06-01 17:16:15 +01004501#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004502 struct device *dev = hsotg->dev;
4503 void __iomem *regs = hsotg->regs;
4504 u32 val;
4505 int idx;
4506
4507 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004508 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4509 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004510
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004511 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004512 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004513
4514 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004515 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004516
4517 /* show periodic fifo settings */
4518
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004519 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004520 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004521 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004522 val >> FIFOSIZE_DEPTH_SHIFT,
4523 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004524 }
4525
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004526 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004527 dev_info(dev,
4528 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004529 dwc2_readl(regs + DIEPCTL(idx)),
4530 dwc2_readl(regs + DIEPTSIZ(idx)),
4531 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004532
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004533 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004534 dev_info(dev,
4535 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004536 idx, dwc2_readl(regs + DOEPCTL(idx)),
4537 dwc2_readl(regs + DOEPTSIZ(idx)),
4538 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004539 }
4540
4541 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004542 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004543#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004544}
4545
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004546/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004547 * dwc2_gadget_init - init function for gadget
4548 * @dwc2: The data structure for the DWC2 driver.
4549 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004550 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06004551int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004552{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004553 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004554 int epnum;
4555 int ret;
John Youn43e90342015-12-17 11:17:45 -08004556
Gregory Herrero0a176272015-01-09 13:38:52 +01004557 /* Dump fifo information */
4558 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004559 hsotg->params.g_np_tx_fifo_size);
4560 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004561
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004562 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004563 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004564 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02004565 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4566 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004567 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4568 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004569
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004570 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004571 if (ret) {
4572 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004573 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004574 }
4575
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004576 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4577 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004578 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004579 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004580
4581 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4582 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004583 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004584 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004585
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004586 if (using_desc_dma(hsotg)) {
4587 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4588 if (ret < 0)
4589 return ret;
4590 }
4591
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004592 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
John Youn9da51972017-01-17 20:30:27 -08004593 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004594 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004595 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004596 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004597 }
4598
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004599 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4600
4601 if (hsotg->num_of_eps == 0) {
4602 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004603 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004604 }
4605
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004606 /* setup endpoint information */
4607
4608 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004609 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004610
4611 /* allocate EP0 request */
4612
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004613 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004614 GFP_KERNEL);
4615 if (!hsotg->ctrl_req) {
4616 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004617 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004618 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004619
4620 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004621 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4622 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004623 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
John Youn9da51972017-01-17 20:30:27 -08004624 epnum, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004625 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004626 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
John Youn9da51972017-01-17 20:30:27 -08004627 epnum, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004628 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004629
Dinh Nguyen117777b2014-11-11 11:13:34 -06004630 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004631 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004632 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004633
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004634 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004635
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004636 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004637}
4638
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004639/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004640 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004641 * @pdev: The platform information for the driver
4642 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004643int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004644{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004645 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004646
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004647 return 0;
4648}
4649
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004650int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004651{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004652 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004653
Gregory Herrero9e779772015-04-29 22:09:07 +02004654 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004655 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004656
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004657 if (hsotg->driver) {
4658 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004659
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004660 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4661 hsotg->driver->driver.name);
4662
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004663 spin_lock_irqsave(&hsotg->lock, flags);
4664 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004665 dwc2_hsotg_core_disconnect(hsotg);
4666 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004667 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4668 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004669
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004670 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4671 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004672 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004673 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004674 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004675 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004676 }
4677
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004678 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004679}
4680
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004681int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004682{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004683 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004684
Gregory Herrero9e779772015-04-29 22:09:07 +02004685 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004686 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004687
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004688 if (hsotg->driver) {
4689 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4690 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004691
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004692 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004693 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004694 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004695 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004696 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004697 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004698
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004699 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004700}
John Youn58e52ff6a2016-02-23 19:54:57 -08004701
4702/**
4703 * dwc2_backup_device_registers() - Backup controller device registers.
4704 * When suspending usb bus, registers needs to be backuped
4705 * if controller power is disabled once suspended.
4706 *
4707 * @hsotg: Programming view of the DWC_otg controller
4708 */
4709int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4710{
4711 struct dwc2_dregs_backup *dr;
4712 int i;
4713
4714 dev_dbg(hsotg->dev, "%s\n", __func__);
4715
4716 /* Backup dev regs */
4717 dr = &hsotg->dr_backup;
4718
4719 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4720 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4721 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4722 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4723 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4724
4725 for (i = 0; i < hsotg->num_of_eps; i++) {
4726 /* Backup IN EPs */
4727 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4728
4729 /* Ensure DATA PID is correctly configured */
4730 if (dr->diepctl[i] & DXEPCTL_DPID)
4731 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4732 else
4733 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4734
4735 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4736 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4737
4738 /* Backup OUT EPs */
4739 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4740
4741 /* Ensure DATA PID is correctly configured */
4742 if (dr->doepctl[i] & DXEPCTL_DPID)
4743 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4744 else
4745 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4746
4747 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4748 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4749 }
4750 dr->valid = true;
4751 return 0;
4752}
4753
4754/**
4755 * dwc2_restore_device_registers() - Restore controller device registers.
4756 * When resuming usb bus, device registers needs to be restored
4757 * if controller power were disabled.
4758 *
4759 * @hsotg: Programming view of the DWC_otg controller
4760 */
4761int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4762{
4763 struct dwc2_dregs_backup *dr;
4764 u32 dctl;
4765 int i;
4766
4767 dev_dbg(hsotg->dev, "%s\n", __func__);
4768
4769 /* Restore dev regs */
4770 dr = &hsotg->dr_backup;
4771 if (!dr->valid) {
4772 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4773 __func__);
4774 return -EINVAL;
4775 }
4776 dr->valid = false;
4777
4778 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4779 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4780 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4781 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4782 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4783
4784 for (i = 0; i < hsotg->num_of_eps; i++) {
4785 /* Restore IN EPs */
4786 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4787 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4788 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4789
4790 /* Restore OUT EPs */
4791 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4792 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4793 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4794 }
4795
4796 /* Set the Power-On Programming done bit */
4797 dctl = dwc2_readl(hsotg->regs + DCTL);
4798 dctl |= DCTL_PWRONPRGDONE;
4799 dwc2_writel(dctl, hsotg->regs + DCTL);
4800
4801 return 0;
4802}