blob: caa428a4a3eb2f27b18f4b2d2a0586c44bdb55f1 [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,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100174 unsigned int ep, unsigned int dir_in,
175 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
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200244 /*
245 * according to p428 of the design guide, we need to ensure that
246 * all fifos are flushed before continuing
247 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100248
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300249 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700250 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100251
252 /* wait until the fifos are both flushed */
253 timeout = 100;
254 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300255 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100256
Dinh Nguyen47a16852014-04-14 14:13:34 -0700257 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100258 break;
259
260 if (--timeout == 0) {
261 dev_err(hsotg->dev,
262 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
263 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100264 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100265 }
266
267 udelay(1);
268 }
269
270 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100271}
272
273/**
274 * @ep: USB endpoint to allocate request for.
275 * @flags: Allocation flags
276 *
277 * Allocate a new USB request structure appropriate for the specified endpoint
278 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500279static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
Mark Brown0978f8c2010-01-18 13:18:35 +0000280 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100281{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500282 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100283
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500284 req = kzalloc(sizeof(struct dwc2_hsotg_req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100285 if (!req)
286 return NULL;
287
288 INIT_LIST_HEAD(&req->queue);
289
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100290 return &req->req;
291}
292
293/**
294 * is_ep_periodic - return true if the endpoint is in periodic mode.
295 * @hs_ep: The endpoint to query.
296 *
297 * Returns true if the endpoint is in periodic mode, meaning it is being
298 * used for an Interrupt or ISO transfer.
299 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500300static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100301{
302 return hs_ep->periodic;
303}
304
305/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500306 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100307 * @hsotg: The device state.
308 * @hs_ep: The endpoint for the request
309 * @hs_req: The request being processed.
310 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500311 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100312 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200313 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500314static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
315 struct dwc2_hsotg_ep *hs_ep,
316 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100317{
318 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100319
320 /* ignore this if we're not moving any data */
321 if (hs_req->req.length == 0)
322 return;
323
Jingoo Han17d966a2013-05-11 21:14:00 +0900324 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100325}
326
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -0800327/*
328 * dwc2_gadget_alloc_ctrl_desc_chains - allocate DMA descriptor chains
329 * for Control endpoint
330 * @hsotg: The device state.
331 *
332 * This function will allocate 4 descriptor chains for EP 0: 2 for
333 * Setup stage, per one for IN and OUT data/status transactions.
334 */
335static int dwc2_gadget_alloc_ctrl_desc_chains(struct dwc2_hsotg *hsotg)
336{
337 hsotg->setup_desc[0] =
338 dmam_alloc_coherent(hsotg->dev,
339 sizeof(struct dwc2_dma_desc),
340 &hsotg->setup_desc_dma[0],
341 GFP_KERNEL);
342 if (!hsotg->setup_desc[0])
343 goto fail;
344
345 hsotg->setup_desc[1] =
346 dmam_alloc_coherent(hsotg->dev,
347 sizeof(struct dwc2_dma_desc),
348 &hsotg->setup_desc_dma[1],
349 GFP_KERNEL);
350 if (!hsotg->setup_desc[1])
351 goto fail;
352
353 hsotg->ctrl_in_desc =
354 dmam_alloc_coherent(hsotg->dev,
355 sizeof(struct dwc2_dma_desc),
356 &hsotg->ctrl_in_desc_dma,
357 GFP_KERNEL);
358 if (!hsotg->ctrl_in_desc)
359 goto fail;
360
361 hsotg->ctrl_out_desc =
362 dmam_alloc_coherent(hsotg->dev,
363 sizeof(struct dwc2_dma_desc),
364 &hsotg->ctrl_out_desc_dma,
365 GFP_KERNEL);
366 if (!hsotg->ctrl_out_desc)
367 goto fail;
368
369 return 0;
370
371fail:
372 return -ENOMEM;
373}
374
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100375/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500376 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100377 * @hsotg: The controller state.
378 * @hs_ep: The endpoint we're going to write for.
379 * @hs_req: The request to write data for.
380 *
381 * This is called when the TxFIFO has some space in it to hold a new
382 * transmission and we have something to give it. The actual setup of
383 * the data size is done elsewhere, so all we have to do is to actually
384 * write the data.
385 *
386 * The return value is zero if there is more space (or nothing was done)
387 * otherwise -ENOSPC is returned if the FIFO space was used up.
388 *
389 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200390 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500391static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
392 struct dwc2_hsotg_ep *hs_ep,
393 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100394{
395 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300396 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100397 int buf_pos = hs_req->req.actual;
398 int to_write = hs_ep->size_loaded;
399 void *data;
400 int can_write;
401 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200402 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100403
404 to_write -= (buf_pos - hs_ep->last_load);
405
406 /* if there's nothing to write, get out early */
407 if (to_write == 0)
408 return 0;
409
Ben Dooks10aebc72010-07-19 09:40:44 +0100410 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300411 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100412 int size_left;
413 int size_done;
414
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200415 /*
416 * work out how much data was loaded so we can calculate
417 * how much data is left in the fifo.
418 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100419
Dinh Nguyen47a16852014-04-14 14:13:34 -0700420 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100421
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200422 /*
423 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100424 * previous data has been completely sent.
425 */
426 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500427 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100428 return -ENOSPC;
429 }
430
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100431 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
432 __func__, size_left,
433 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
434
435 /* how much of the data has moved */
436 size_done = hs_ep->size_loaded - size_left;
437
438 /* how much data is left in the fifo */
439 can_write = hs_ep->fifo_load - size_done;
440 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
441 __func__, can_write);
442
443 can_write = hs_ep->fifo_size - can_write;
444 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
445 __func__, can_write);
446
447 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500448 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100449 return -ENOSPC;
450 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100451 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Robert Baldygaad674a12016-08-29 13:38:50 -0700452 can_write = dwc2_readl(hsotg->regs +
453 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100454
455 can_write &= 0xffff;
456 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100457 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700458 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100459 dev_dbg(hsotg->dev,
460 "%s: no queue slots available (0x%08x)\n",
461 __func__, gnptxsts);
462
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500463 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100464 return -ENOSPC;
465 }
466
Dinh Nguyen47a16852014-04-14 14:13:34 -0700467 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100468 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100469 }
470
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200471 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
472
473 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
474 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100475
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200476 /*
477 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100478 * FIFO, requests of >512 cause the endpoint to get stuck with a
479 * fragment of the end of the transfer in it.
480 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200481 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100482 can_write = 512;
483
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200484 /*
485 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100486 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200487 * doing it.
488 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200489 if (to_write > max_transfer) {
490 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100491
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200492 /* it's needed only when we do not use dedicated fifos */
493 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500494 dwc2_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700495 periodic ? GINTSTS_PTXFEMP :
496 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100497 }
498
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100499 /* see if we can write data */
500
501 if (to_write > can_write) {
502 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200503 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100504
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200505 /*
506 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100507 * exact number of packets.
508 *
509 * Note, we do not currently check to see if we can ever
510 * write a full packet or not to the FIFO.
511 */
512
513 if (pkt_round)
514 to_write -= pkt_round;
515
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200516 /*
517 * enable correct FIFO interrupt to alert us when there
518 * is more room left.
519 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100520
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200521 /* it's needed only when we do not use dedicated fifos */
522 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500523 dwc2_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700524 periodic ? GINTSTS_PTXFEMP :
525 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100526 }
527
528 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
529 to_write, hs_req->req.length, can_write, buf_pos);
530
531 if (to_write <= 0)
532 return -ENOSPC;
533
534 hs_req->req.actual = buf_pos + to_write;
535 hs_ep->total_data += to_write;
536
537 if (periodic)
538 hs_ep->fifo_load += to_write;
539
540 to_write = DIV_ROUND_UP(to_write, 4);
541 data = hs_req->req.buf + buf_pos;
542
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500543 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100544
545 return (to_write >= can_write) ? -ENOSPC : 0;
546}
547
548/**
549 * get_ep_limit - get the maximum data legnth for this endpoint
550 * @hs_ep: The endpoint
551 *
552 * Return the maximum data that can be queued in one go on a given endpoint
553 * so that transfers that are too long can be split.
554 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500555static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100556{
557 int index = hs_ep->index;
558 unsigned maxsize;
559 unsigned maxpkt;
560
561 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700562 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
563 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100564 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100565 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900566 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700567 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900568 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100569 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100570 }
571
572 /* we made the constant loading easier above by using +1 */
573 maxpkt--;
574 maxsize--;
575
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200576 /*
577 * constrain by packet count if maxpkts*pktsize is greater
578 * than the length register size.
579 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100580
581 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
582 maxsize = maxpkt * hs_ep->ep.maxpacket;
583
584 return maxsize;
585}
586
587/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700588* dwc2_hsotg_read_frameno - read current frame number
589* @hsotg: The device instance
590*
591* Return the current frame number
592*/
593static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
594{
595 u32 dsts;
596
597 dsts = dwc2_readl(hsotg->regs + DSTS);
598 dsts &= DSTS_SOFFN_MASK;
599 dsts >>= DSTS_SOFFN_SHIFT;
600
601 return dsts;
602}
603
604/**
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800605 * dwc2_gadget_get_chain_limit - get the maximum data payload value of the
606 * DMA descriptor chain prepared for specific endpoint
607 * @hs_ep: The endpoint
608 *
609 * Return the maximum data that can be queued in one go on a given endpoint
610 * depending on its descriptor chain capacity so that transfers that
611 * are too long can be split.
612 */
613static unsigned int dwc2_gadget_get_chain_limit(struct dwc2_hsotg_ep *hs_ep)
614{
615 int is_isoc = hs_ep->isochronous;
616 unsigned int maxsize;
617
618 if (is_isoc)
619 maxsize = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_LIMIT :
620 DEV_DMA_ISOC_RX_NBYTES_LIMIT;
621 else
622 maxsize = DEV_DMA_NBYTES_LIMIT;
623
624 /* Above size of one descriptor was chosen, multiple it */
625 maxsize *= MAX_DMA_DESC_NUM_GENERIC;
626
627 return maxsize;
628}
629
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -0800630/*
631 * dwc2_gadget_get_desc_params - get DMA descriptor parameters.
632 * @hs_ep: The endpoint
633 * @mask: RX/TX bytes mask to be defined
634 *
635 * Returns maximum data payload for one descriptor after analyzing endpoint
636 * characteristics.
637 * DMA descriptor transfer bytes limit depends on EP type:
638 * Control out - MPS,
639 * Isochronous - descriptor rx/tx bytes bitfield limit,
640 * Control In/Bulk/Interrupt - multiple of mps. This will allow to not
641 * have concatenations from various descriptors within one packet.
642 *
643 * Selects corresponding mask for RX/TX bytes as well.
644 */
645static u32 dwc2_gadget_get_desc_params(struct dwc2_hsotg_ep *hs_ep, u32 *mask)
646{
647 u32 mps = hs_ep->ep.maxpacket;
648 int dir_in = hs_ep->dir_in;
649 u32 desc_size = 0;
650
651 if (!hs_ep->index && !dir_in) {
652 desc_size = mps;
653 *mask = DEV_DMA_NBYTES_MASK;
654 } else if (hs_ep->isochronous) {
655 if (dir_in) {
656 desc_size = DEV_DMA_ISOC_TX_NBYTES_LIMIT;
657 *mask = DEV_DMA_ISOC_TX_NBYTES_MASK;
658 } else {
659 desc_size = DEV_DMA_ISOC_RX_NBYTES_LIMIT;
660 *mask = DEV_DMA_ISOC_RX_NBYTES_MASK;
661 }
662 } else {
663 desc_size = DEV_DMA_NBYTES_LIMIT;
664 *mask = DEV_DMA_NBYTES_MASK;
665
666 /* Round down desc_size to be mps multiple */
667 desc_size -= desc_size % mps;
668 }
669
670 return desc_size;
671}
672
673/*
674 * dwc2_gadget_config_nonisoc_xfer_ddma - prepare non ISOC DMA desc chain.
675 * @hs_ep: The endpoint
676 * @dma_buff: DMA address to use
677 * @len: Length of the transfer
678 *
679 * This function will iterate over descriptor chain and fill its entries
680 * with corresponding information based on transfer data.
681 */
682static void dwc2_gadget_config_nonisoc_xfer_ddma(struct dwc2_hsotg_ep *hs_ep,
683 dma_addr_t dma_buff,
684 unsigned int len)
685{
686 struct dwc2_hsotg *hsotg = hs_ep->parent;
687 int dir_in = hs_ep->dir_in;
688 struct dwc2_dma_desc *desc = hs_ep->desc_list;
689 u32 mps = hs_ep->ep.maxpacket;
690 u32 maxsize = 0;
691 u32 offset = 0;
692 u32 mask = 0;
693 int i;
694
695 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
696
697 hs_ep->desc_count = (len / maxsize) +
698 ((len % maxsize) ? 1 : 0);
699 if (len == 0)
700 hs_ep->desc_count = 1;
701
702 for (i = 0; i < hs_ep->desc_count; ++i) {
703 desc->status = 0;
704 desc->status |= (DEV_DMA_BUFF_STS_HBUSY
705 << DEV_DMA_BUFF_STS_SHIFT);
706
707 if (len > maxsize) {
708 if (!hs_ep->index && !dir_in)
709 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
710
711 desc->status |= (maxsize <<
712 DEV_DMA_NBYTES_SHIFT & mask);
713 desc->buf = dma_buff + offset;
714
715 len -= maxsize;
716 offset += maxsize;
717 } else {
718 desc->status |= (DEV_DMA_L | DEV_DMA_IOC);
719
720 if (dir_in)
721 desc->status |= (len % mps) ? DEV_DMA_SHORT :
722 ((hs_ep->send_zlp) ? DEV_DMA_SHORT : 0);
723 if (len > maxsize)
724 dev_err(hsotg->dev, "wrong len %d\n", len);
725
726 desc->status |=
727 len << DEV_DMA_NBYTES_SHIFT & mask;
728 desc->buf = dma_buff + offset;
729 }
730
731 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
732 desc->status |= (DEV_DMA_BUFF_STS_HREADY
733 << DEV_DMA_BUFF_STS_SHIFT);
734 desc++;
735 }
736}
737
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800738/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500739 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100740 * @hsotg: The controller state.
741 * @hs_ep: The endpoint to process a request for
742 * @hs_req: The request to start.
743 * @continuing: True if we are doing more for the current request.
744 *
745 * Start the given request running by setting the endpoint registers
746 * appropriately, and writing any data to the FIFOs.
747 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500748static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
749 struct dwc2_hsotg_ep *hs_ep,
750 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100751 bool continuing)
752{
753 struct usb_request *ureq = &hs_req->req;
754 int index = hs_ep->index;
755 int dir_in = hs_ep->dir_in;
756 u32 epctrl_reg;
757 u32 epsize_reg;
758 u32 epsize;
759 u32 ctrl;
760 unsigned length;
761 unsigned packets;
762 unsigned maxreq;
763
764 if (index != 0) {
765 if (hs_ep->req && !continuing) {
766 dev_err(hsotg->dev, "%s: active request\n", __func__);
767 WARN_ON(1);
768 return;
769 } else if (hs_ep->req != hs_req && continuing) {
770 dev_err(hsotg->dev,
771 "%s: continue different req\n", __func__);
772 WARN_ON(1);
773 return;
774 }
775 }
776
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200777 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
778 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100779
780 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300781 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100782 hs_ep->dir_in ? "in" : "out");
783
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900784 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300785 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900786
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200787 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900788 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
789 return;
790 }
791
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100792 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200793 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
794 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100795
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800796 if (!using_desc_dma(hsotg))
797 maxreq = get_ep_limit(hs_ep);
798 else
799 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
800
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100801 if (length > maxreq) {
802 int round = maxreq % hs_ep->ep.maxpacket;
803
804 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
805 __func__, length, maxreq, round);
806
807 /* round down to multiple of packets */
808 if (round)
809 maxreq -= round;
810
811 length = maxreq;
812 }
813
814 if (length)
815 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
816 else
817 packets = 1; /* send one packet if length is zero. */
818
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200819 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
820 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
821 return;
822 }
823
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100824 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200825 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700826 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200827 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700828 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100829 else
830 epsize = 0;
831
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100832 /*
833 * zero length packet should be programmed on its own and should not
834 * be counted in DIEPTSIZ.PktCnt with other packets.
835 */
836 if (dir_in && ureq->zero && !continuing) {
837 /* Test if zlp is actually required. */
838 if ((ureq->length >= hs_ep->ep.maxpacket) &&
839 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +0100840 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100841 }
842
Dinh Nguyen47a16852014-04-14 14:13:34 -0700843 epsize |= DXEPTSIZ_PKTCNT(packets);
844 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100845
846 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
847 __func__, packets, length, ureq->length, epsize, epsize_reg);
848
849 /* store the request as the current one we're doing */
850 hs_ep->req = hs_req;
851
852 /* write size / packets */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300853 dwc2_writel(epsize, hsotg->regs + epsize_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100854
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900855 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100856 unsigned int dma_reg;
857
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200858 /*
859 * write DMA address to control register, buffer already
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500860 * synced by dwc2_hsotg_ep_queue().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200861 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100862
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200863 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300864 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100865
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300866 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900867 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100868 }
869
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700870 if (hs_ep->isochronous && hs_ep->interval == 1) {
871 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
872 dwc2_gadget_incr_frame_num(hs_ep);
873
874 if (hs_ep->target_frame & 0x1)
875 ctrl |= DXEPCTL_SETODDFR;
876 else
877 ctrl |= DXEPCTL_SETEVENFR;
878 }
879
Dinh Nguyen47a16852014-04-14 14:13:34 -0700880 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200881
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100882 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +0200883
884 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100885 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -0700886 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200887
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100888 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300889 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100890
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200891 /*
892 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100893 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200894 * this information.
895 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100896 hs_ep->size_loaded = length;
897 hs_ep->last_load = ureq->actual;
898
899 if (dir_in && !using_dma(hsotg)) {
900 /* set these anyway, we may need them for non-periodic in */
901 hs_ep->fifo_load = 0;
902
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500903 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100904 }
905
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200906 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200907 * Note, trying to clear the NAK here causes problems with transmit
908 * on the S3C6400 ending up with the TXFIFO becoming full.
909 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100910
911 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300912 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +0100913 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700914 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300915 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100916
Dinh Nguyen47a16852014-04-14 14:13:34 -0700917 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300918 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200919
920 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500921 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100922}
923
924/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500925 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100926 * @hsotg: The device state.
927 * @hs_ep: The endpoint the request is on.
928 * @req: The request being processed.
929 *
930 * We've been asked to queue a request, so ensure that the memory buffer
931 * is correctly setup for DMA. If we've been passed an extant DMA address
932 * then ensure the buffer has been synced to memory. If our buffer has no
933 * DMA memory, then we map the memory and mark our request to allow us to
934 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200935 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500936static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
937 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100938 struct usb_request *req)
939{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500940 struct dwc2_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200941 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100942
943 /* if the length is zero, ignore the DMA data */
944 if (hs_req->req.length == 0)
945 return 0;
946
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200947 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
948 if (ret)
949 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100950
951 return 0;
952
953dma_error:
954 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
955 __func__, req->buf, req->length);
956
957 return -EIO;
958}
959
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500960static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
961 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100962{
963 void *req_buf = hs_req->req.buf;
964
965 /* If dma is not being used or buffer is aligned */
966 if (!using_dma(hsotg) || !((long)req_buf & 3))
967 return 0;
968
969 WARN_ON(hs_req->saved_req_buf);
970
971 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
972 hs_ep->ep.name, req_buf, hs_req->req.length);
973
974 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
975 if (!hs_req->req.buf) {
976 hs_req->req.buf = req_buf;
977 dev_err(hsotg->dev,
978 "%s: unable to allocate memory for bounce buffer\n",
979 __func__);
980 return -ENOMEM;
981 }
982
983 /* Save actual buffer */
984 hs_req->saved_req_buf = req_buf;
985
986 if (hs_ep->dir_in)
987 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
988 return 0;
989}
990
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500991static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
992 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100993{
994 /* If dma is not being used or buffer was aligned */
995 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
996 return;
997
998 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
999 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1000
1001 /* Copy data from bounce buffer on successful out transfer */
1002 if (!hs_ep->dir_in && !hs_req->req.status)
1003 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
1004 hs_req->req.actual);
1005
1006 /* Free bounce buffer */
1007 kfree(hs_req->req.buf);
1008
1009 hs_req->req.buf = hs_req->saved_req_buf;
1010 hs_req->saved_req_buf = NULL;
1011}
1012
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001013/**
1014 * dwc2_gadget_target_frame_elapsed - Checks target frame
1015 * @hs_ep: The driver endpoint to check
1016 *
1017 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1018 * corresponding transfer.
1019 */
1020static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1021{
1022 struct dwc2_hsotg *hsotg = hs_ep->parent;
1023 u32 target_frame = hs_ep->target_frame;
1024 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
1025 bool frame_overrun = hs_ep->frame_overrun;
1026
1027 if (!frame_overrun && current_frame >= target_frame)
1028 return true;
1029
1030 if (frame_overrun && current_frame >= target_frame &&
1031 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1032 return true;
1033
1034 return false;
1035}
1036
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001037/*
1038 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1039 * @hsotg: The driver state
1040 * @hs_ep: the ep descriptor chain is for
1041 *
1042 * Called to update EP0 structure's pointers depend on stage of
1043 * control transfer.
1044 */
1045static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1046 struct dwc2_hsotg_ep *hs_ep)
1047{
1048 switch (hsotg->ep0_state) {
1049 case DWC2_EP0_SETUP:
1050 case DWC2_EP0_STATUS_OUT:
1051 hs_ep->desc_list = hsotg->setup_desc[0];
1052 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1053 break;
1054 case DWC2_EP0_DATA_IN:
1055 case DWC2_EP0_STATUS_IN:
1056 hs_ep->desc_list = hsotg->ctrl_in_desc;
1057 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1058 break;
1059 case DWC2_EP0_DATA_OUT:
1060 hs_ep->desc_list = hsotg->ctrl_out_desc;
1061 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1062 break;
1063 default:
1064 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1065 hsotg->ep0_state);
1066 return -EINVAL;
1067 }
1068
1069 return 0;
1070}
1071
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001072static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001073 gfp_t gfp_flags)
1074{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001075 struct dwc2_hsotg_req *hs_req = our_req(req);
1076 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001077 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001078 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001079 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001080
1081 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1082 ep->name, req, req->length, req->buf, req->no_interrupt,
1083 req->zero, req->short_not_ok);
1084
Gregory Herrero7ababa92015-04-29 22:09:08 +02001085 /* Prevent new request submission when controller is suspended */
1086 if (hs->lx_state == DWC2_L2) {
1087 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
1088 __func__);
1089 return -EAGAIN;
1090 }
1091
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001092 /* initialise status of the request */
1093 INIT_LIST_HEAD(&hs_req->queue);
1094 req->actual = 0;
1095 req->status = -EINPROGRESS;
1096
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001097 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001098 if (ret)
1099 return ret;
1100
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001101 /* if we're using DMA, sync the buffers as necessary */
1102 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001103 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001104 if (ret)
1105 return ret;
1106 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001107 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1108 if (using_desc_dma(hs) && !hs_ep->index) {
1109 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1110 if (ret)
1111 return ret;
1112 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001113
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001114 first = list_empty(&hs_ep->queue);
1115 list_add_tail(&hs_req->queue, &hs_ep->queue);
1116
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001117 if (first) {
1118 if (!hs_ep->isochronous) {
1119 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1120 return 0;
1121 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001122
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001123 while (dwc2_gadget_target_frame_elapsed(hs_ep))
1124 dwc2_gadget_incr_frame_num(hs_ep);
1125
1126 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1127 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1128 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001129 return 0;
1130}
1131
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001132static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001133 gfp_t gfp_flags)
1134{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001135 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001136 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001137 unsigned long flags = 0;
1138 int ret = 0;
1139
1140 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001141 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001142 spin_unlock_irqrestore(&hs->lock, flags);
1143
1144 return ret;
1145}
1146
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001147static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001148 struct usb_request *req)
1149{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001150 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001151
1152 kfree(hs_req);
1153}
1154
1155/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001156 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001157 * @ep: The endpoint the request was on.
1158 * @req: The request completed.
1159 *
1160 * Called on completion of any requests the driver itself
1161 * submitted that need cleaning up.
1162 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001163static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001164 struct usb_request *req)
1165{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001166 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001167 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001168
1169 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1170
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001171 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001172}
1173
1174/**
1175 * ep_from_windex - convert control wIndex value to endpoint
1176 * @hsotg: The driver state.
1177 * @windex: The control request wIndex field (in host order).
1178 *
1179 * Convert the given wIndex into a pointer to an driver endpoint
1180 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001181 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001182static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001183 u32 windex)
1184{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001185 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001186 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1187 int idx = windex & 0x7F;
1188
1189 if (windex >= 0x100)
1190 return NULL;
1191
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001192 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001193 return NULL;
1194
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001195 ep = index_to_ep(hsotg, idx, dir);
1196
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001197 if (idx && ep->dir_in != dir)
1198 return NULL;
1199
1200 return ep;
1201}
1202
1203/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001204 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001205 * @hsotg: The driver state.
1206 * @testmode: requested usb test mode
1207 * Enable usb Test Mode requested by the Host.
1208 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001209int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001210{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001211 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001212
1213 dctl &= ~DCTL_TSTCTL_MASK;
1214 switch (testmode) {
1215 case TEST_J:
1216 case TEST_K:
1217 case TEST_SE0_NAK:
1218 case TEST_PACKET:
1219 case TEST_FORCE_EN:
1220 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1221 break;
1222 default:
1223 return -EINVAL;
1224 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001225 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001226 return 0;
1227}
1228
1229/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001230 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001231 * @hsotg: The device state
1232 * @ep: Endpoint 0
1233 * @buff: Buffer for request
1234 * @length: Length of reply.
1235 *
1236 * Create a request and queue it on the given endpoint. This is useful as
1237 * an internal method of sending replies to certain control requests, etc.
1238 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001239static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
1240 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001241 void *buff,
1242 int length)
1243{
1244 struct usb_request *req;
1245 int ret;
1246
1247 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1248
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001249 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001250 hsotg->ep0_reply = req;
1251 if (!req) {
1252 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1253 return -ENOMEM;
1254 }
1255
1256 req->buf = hsotg->ep0_buff;
1257 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001258 /*
1259 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1260 * STATUS stage.
1261 */
1262 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001263 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001264
1265 if (length)
1266 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001267
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001268 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001269 if (ret) {
1270 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1271 return ret;
1272 }
1273
1274 return 0;
1275}
1276
1277/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001278 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001279 * @hsotg: The device state
1280 * @ctrl: USB control request
1281 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001282static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001283 struct usb_ctrlrequest *ctrl)
1284{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001285 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1286 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001287 __le16 reply;
1288 int ret;
1289
1290 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1291
1292 if (!ep0->dir_in) {
1293 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1294 return -EINVAL;
1295 }
1296
1297 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1298 case USB_RECIP_DEVICE:
1299 reply = cpu_to_le16(0); /* bit 0 => self powered,
1300 * bit 1 => remote wakeup */
1301 break;
1302
1303 case USB_RECIP_INTERFACE:
1304 /* currently, the data result should be zero */
1305 reply = cpu_to_le16(0);
1306 break;
1307
1308 case USB_RECIP_ENDPOINT:
1309 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1310 if (!ep)
1311 return -ENOENT;
1312
1313 reply = cpu_to_le16(ep->halted ? 1 : 0);
1314 break;
1315
1316 default:
1317 return 0;
1318 }
1319
1320 if (le16_to_cpu(ctrl->wLength) != 2)
1321 return -EINVAL;
1322
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001323 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001324 if (ret) {
1325 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1326 return ret;
1327 }
1328
1329 return 1;
1330}
1331
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001332static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001333
1334/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001335 * get_ep_head - return the first request on the endpoint
1336 * @hs_ep: The controller endpoint to get
1337 *
1338 * Get the first request on the endpoint.
1339 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001340static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001341{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001342 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1343 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001344}
1345
1346/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001347 * dwc2_gadget_start_next_request - Starts next request from ep queue
1348 * @hs_ep: Endpoint structure
1349 *
1350 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1351 * in its handler. Hence we need to unmask it here to be able to do
1352 * resynchronization.
1353 */
1354static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1355{
1356 u32 mask;
1357 struct dwc2_hsotg *hsotg = hs_ep->parent;
1358 int dir_in = hs_ep->dir_in;
1359 struct dwc2_hsotg_req *hs_req;
1360 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1361
1362 if (!list_empty(&hs_ep->queue)) {
1363 hs_req = get_ep_head(hs_ep);
1364 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1365 return;
1366 }
1367 if (!hs_ep->isochronous)
1368 return;
1369
1370 if (dir_in) {
1371 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1372 __func__);
1373 } else {
1374 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1375 __func__);
1376 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1377 mask |= DOEPMSK_OUTTKNEPDISMSK;
1378 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1379 }
1380}
1381
1382/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001383 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001384 * @hsotg: The device state
1385 * @ctrl: USB control request
1386 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001387static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001388 struct usb_ctrlrequest *ctrl)
1389{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001390 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1391 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001392 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001393 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001394 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001395 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001396 u32 recip;
1397 u32 wValue;
1398 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001399
1400 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1401 __func__, set ? "SET" : "CLEAR");
1402
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001403 wValue = le16_to_cpu(ctrl->wValue);
1404 wIndex = le16_to_cpu(ctrl->wIndex);
1405 recip = ctrl->bRequestType & USB_RECIP_MASK;
1406
1407 switch (recip) {
1408 case USB_RECIP_DEVICE:
1409 switch (wValue) {
1410 case USB_DEVICE_TEST_MODE:
1411 if ((wIndex & 0xff) != 0)
1412 return -EINVAL;
1413 if (!set)
1414 return -EINVAL;
1415
1416 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001417 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001418 if (ret) {
1419 dev_err(hsotg->dev,
1420 "%s: failed to send reply\n", __func__);
1421 return ret;
1422 }
1423 break;
1424 default:
1425 return -ENOENT;
1426 }
1427 break;
1428
1429 case USB_RECIP_ENDPOINT:
1430 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001431 if (!ep) {
1432 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001433 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001434 return -ENOENT;
1435 }
1436
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001437 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001438 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001439 halted = ep->halted;
1440
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001441 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001442
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001443 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001444 if (ret) {
1445 dev_err(hsotg->dev,
1446 "%s: failed to send reply\n", __func__);
1447 return ret;
1448 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001449
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001450 /*
1451 * we have to complete all requests for ep if it was
1452 * halted, and the halt was cleared by CLEAR_FEATURE
1453 */
1454
1455 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001456 /*
1457 * If we have request in progress,
1458 * then complete it
1459 */
1460 if (ep->req) {
1461 hs_req = ep->req;
1462 ep->req = NULL;
1463 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001464 if (hs_req->req.complete) {
1465 spin_unlock(&hsotg->lock);
1466 usb_gadget_giveback_request(
1467 &ep->ep, &hs_req->req);
1468 spin_lock(&hsotg->lock);
1469 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001470 }
1471
1472 /* If we have pending request, then start it */
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001473 if (!ep->req) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001474 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001475 }
1476 }
1477
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001478 break;
1479
1480 default:
1481 return -ENOENT;
1482 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001483 break;
1484 default:
1485 return -ENOENT;
1486 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001487 return 1;
1488}
1489
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001490static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001491
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001492/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001493 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001494 * @hsotg: The device state
1495 *
1496 * Set stall for ep0 as response for setup request.
1497 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001498static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001499{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001500 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001501 u32 reg;
1502 u32 ctrl;
1503
1504 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1505 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1506
1507 /*
1508 * DxEPCTL_Stall will be cleared by EP once it has
1509 * taken effect, so no need to clear later.
1510 */
1511
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001512 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001513 ctrl |= DXEPCTL_STALL;
1514 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001515 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001516
1517 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001518 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001519 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001520
1521 /*
1522 * complete won't be called, so we enqueue
1523 * setup request here
1524 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001525 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001526}
1527
1528/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001529 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001530 * @hsotg: The device state
1531 * @ctrl: The control request received
1532 *
1533 * The controller has received the SETUP phase of a control request, and
1534 * needs to work out what to do next (and whether to pass it on to the
1535 * gadget driver).
1536 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001537static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001538 struct usb_ctrlrequest *ctrl)
1539{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001540 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001541 int ret = 0;
1542 u32 dcfg;
1543
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001544 dev_dbg(hsotg->dev,
1545 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1546 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1547 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001548
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001549 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001550 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001551 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1552 } else if (ctrl->bRequestType & USB_DIR_IN) {
1553 ep0->dir_in = 1;
1554 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1555 } else {
1556 ep0->dir_in = 0;
1557 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1558 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001559
1560 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1561 switch (ctrl->bRequest) {
1562 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001563 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001564 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001565 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001566 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1567 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001568 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001569
1570 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1571
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001572 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001573 return;
1574
1575 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001576 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001577 break;
1578
1579 case USB_REQ_CLEAR_FEATURE:
1580 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001581 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001582 break;
1583 }
1584 }
1585
1586 /* as a fallback, try delivering it to the driver to deal with */
1587
1588 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001589 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001590 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001591 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001592 if (ret < 0)
1593 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1594 }
1595
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001596 /*
1597 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001598 * so respond with a STALL for the status stage to indicate failure.
1599 */
1600
Robert Baldygac9f721b2014-01-14 08:36:00 +01001601 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001602 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001603}
1604
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001605/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001606 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001607 * @ep: The endpoint the request was on.
1608 * @req: The request completed.
1609 *
1610 * Called on completion of any requests the driver itself submitted for
1611 * EP0 setup packets
1612 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001613static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001614 struct usb_request *req)
1615{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001616 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001617 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001618
1619 if (req->status < 0) {
1620 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1621 return;
1622 }
1623
Robert Baldyga93f599f2013-11-21 13:49:17 +01001624 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001625 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001626 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001627 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001628 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001629 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001630}
1631
1632/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001633 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001634 * @hsotg: The device state.
1635 *
1636 * Enqueue a request on EP0 if necessary to received any SETUP packets
1637 * received from the host.
1638 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001639static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001640{
1641 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001642 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001643 int ret;
1644
1645 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1646
1647 req->zero = 0;
1648 req->length = 8;
1649 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001650 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001651
1652 if (!list_empty(&hs_req->queue)) {
1653 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1654 return;
1655 }
1656
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001657 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001658 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001659 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001660
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001661 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001662 if (ret < 0) {
1663 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001664 /*
1665 * Don't think there's much we can do other than watch the
1666 * driver fail.
1667 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001668 }
1669}
1670
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001671static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1672 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001673{
1674 u32 ctrl;
1675 u8 index = hs_ep->index;
1676 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1677 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1678
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001679 if (hs_ep->dir_in)
1680 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001681 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001682 else
1683 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001684 index);
1685 if (using_desc_dma(hsotg)) {
1686 /* Not specific buffer needed for ep0 ZLP */
1687 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001688
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001689 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1690 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1691 } else {
1692 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1693 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1694 epsiz_reg);
1695 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001696
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001697 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001698 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1699 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1700 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001701 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001702}
1703
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001704/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001705 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001706 * @hsotg: The device state.
1707 * @hs_ep: The endpoint the request was on.
1708 * @hs_req: The request to complete.
1709 * @result: The result code (0 => Ok, otherwise errno)
1710 *
1711 * The given request has finished, so call the necessary completion
1712 * if it has one and then look to see if we can start a new request
1713 * on the endpoint.
1714 *
1715 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001716 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001717static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1718 struct dwc2_hsotg_ep *hs_ep,
1719 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001720 int result)
1721{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001722
1723 if (!hs_req) {
1724 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1725 return;
1726 }
1727
1728 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1729 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1730
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001731 /*
1732 * only replace the status if we've not already set an error
1733 * from a previous transaction
1734 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001735
1736 if (hs_req->req.status == -EINPROGRESS)
1737 hs_req->req.status = result;
1738
Yunzhi Li44583fe2015-09-29 12:25:01 +02001739 if (using_dma(hsotg))
1740 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1741
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001742 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001743
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001744 hs_ep->req = NULL;
1745 list_del_init(&hs_req->queue);
1746
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001747 /*
1748 * call the complete request with the locks off, just in case the
1749 * request tries to queue more work for this endpoint.
1750 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001751
1752 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001753 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001754 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001755 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001756 }
1757
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001758 /*
1759 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001760 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001761 * so be careful when doing this.
1762 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001763
1764 if (!hs_ep->req && result >= 0) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001765 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001766 }
1767}
1768
1769/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001770 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001771 * @hsotg: The device state.
1772 * @ep_idx: The endpoint index for the data
1773 * @size: The size of data in the fifo, in bytes
1774 *
1775 * The FIFO status shows there is data to read from the FIFO for a given
1776 * endpoint, so sort out whether we need to read the data into a request
1777 * that has been made for that endpoint.
1778 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001779static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001780{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001781 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
1782 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001783 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001784 int to_read;
1785 int max_req;
1786 int read_ptr;
1787
Lukasz Majewski22258f42012-06-14 10:02:24 +02001788
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001789 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001790 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001791 int ptr;
1792
Robert Baldyga6b448af42014-12-16 11:51:44 +01001793 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001794 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001795 __func__, size, ep_idx, epctl);
1796
1797 /* dump the data from the FIFO, we've nothing we can do */
1798 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001799 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001800
1801 return;
1802 }
1803
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001804 to_read = size;
1805 read_ptr = hs_req->req.actual;
1806 max_req = hs_req->req.length - read_ptr;
1807
Ben Dooksa33e7132010-07-19 09:40:49 +01001808 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1809 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1810
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001811 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001812 /*
1813 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001814 * to deal with in this request.
1815 */
1816
1817 /* currently we don't deal this */
1818 WARN_ON_ONCE(1);
1819 }
1820
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001821 hs_ep->total_data += to_read;
1822 hs_req->req.actual += to_read;
1823 to_read = DIV_ROUND_UP(to_read, 4);
1824
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001825 /*
1826 * note, we might over-write the buffer end by 3 bytes depending on
1827 * alignment of the data.
1828 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001829 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001830}
1831
1832/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001833 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001834 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001835 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001836 *
1837 * Generate a zero-length IN packet request for terminating a SETUP
1838 * transaction.
1839 *
1840 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001841 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001842 * the TxFIFO.
1843 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001844static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001845{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001846 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001847 hsotg->eps_out[0]->dir_in = dir_in;
1848 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001849
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001850 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001851}
1852
Roman Bacikec1f9d92015-09-10 18:13:43 -07001853static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
1854 u32 epctl_reg)
1855{
1856 u32 ctrl;
1857
1858 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
1859 if (ctrl & DXEPCTL_EOFRNUM)
1860 ctrl |= DXEPCTL_SETEVENFR;
1861 else
1862 ctrl |= DXEPCTL_SETODDFR;
1863 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
1864}
1865
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001866/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001867 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001868 * @hsotg: The device instance
1869 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001870 *
1871 * The RXFIFO has delivered an OutDone event, which means that the data
1872 * transfer for an OUT endpoint has been completed, either by a short
1873 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001874 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001875static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001876{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001877 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001878 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
1879 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001880 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001881 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001882 int result = 0;
1883
1884 if (!hs_req) {
1885 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1886 return;
1887 }
1888
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001889 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1890 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001891 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1892 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001893 return;
1894 }
1895
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001896 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001897 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001898
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001899 /*
1900 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001901 * is left in the endpoint size register and then working it
1902 * out from the amount we loaded for the transfer.
1903 *
1904 * We need to do this as DMA pointers are always 32bit aligned
1905 * so may overshoot/undershoot the transfer.
1906 */
1907
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001908 size_done = hs_ep->size_loaded - size_left;
1909 size_done += hs_ep->last_load;
1910
1911 req->actual = size_done;
1912 }
1913
Ben Dooksa33e7132010-07-19 09:40:49 +01001914 /* if there is more request to do, schedule new transfer */
1915 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001916 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01001917 return;
1918 }
1919
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001920 if (req->actual < req->length && req->short_not_ok) {
1921 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1922 __func__, req->actual, req->length);
1923
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001924 /*
1925 * todo - what should we return here? there's no one else
1926 * even bothering to check the status.
1927 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001928 }
1929
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001930 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1931 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001932 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001933 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001934 }
1935
Roman Bacikec1f9d92015-09-10 18:13:43 -07001936 /*
1937 * Slave mode OUT transfers do not go through XferComplete so
1938 * adjust the ISOC parity here.
1939 */
1940 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07001941 if (hs_ep->isochronous && hs_ep->interval == 1)
1942 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001943 else if (hs_ep->isochronous && hs_ep->interval > 1)
1944 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07001945 }
1946
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001947 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001948}
1949
1950/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001951 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001952 * @hsotg: The device instance
1953 *
1954 * The IRQ handler has detected that the RX FIFO has some data in it
1955 * that requires processing, so find out what is in there and do the
1956 * appropriate read.
1957 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001958 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001959 * chunks, so if you have x packets received on an endpoint you'll get x
1960 * FIFO events delivered, each with a packet's worth of data in it.
1961 *
1962 * When using DMA, we should not be processing events from the RXFIFO
1963 * as the actual data should be sent to the memory directly and we turn
1964 * on the completion interrupts to get notifications of transfer completion.
1965 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001966static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001967{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001968 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001969 u32 epnum, status, size;
1970
1971 WARN_ON(using_dma(hsotg));
1972
Dinh Nguyen47a16852014-04-14 14:13:34 -07001973 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1974 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001975
Dinh Nguyen47a16852014-04-14 14:13:34 -07001976 size = grxstsr & GRXSTS_BYTECNT_MASK;
1977 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001978
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01001979 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001980 __func__, grxstsr, size, epnum);
1981
Dinh Nguyen47a16852014-04-14 14:13:34 -07001982 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1983 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1984 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001985 break;
1986
Dinh Nguyen47a16852014-04-14 14:13:34 -07001987 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001988 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001989 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001990
1991 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001992 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001993 break;
1994
Dinh Nguyen47a16852014-04-14 14:13:34 -07001995 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996 dev_dbg(hsotg->dev,
1997 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001998 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001999 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002000 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002001 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002002 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2003 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2004 */
2005 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002006 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002007 break;
2008
Dinh Nguyen47a16852014-04-14 14:13:34 -07002009 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002010 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002011 break;
2012
Dinh Nguyen47a16852014-04-14 14:13:34 -07002013 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002014 dev_dbg(hsotg->dev,
2015 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002016 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002017 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002018
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002019 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2020
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002021 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002022 break;
2023
2024 default:
2025 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2026 __func__, grxstsr);
2027
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002028 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002029 break;
2030 }
2031}
2032
2033/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002034 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002035 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002036 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002037static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002038{
2039 switch (mps) {
2040 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002041 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002042 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002043 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002044 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002045 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002046 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002047 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002048 }
2049
2050 /* bad max packet size, warn and return invalid result */
2051 WARN_ON(1);
2052 return (u32)-1;
2053}
2054
2055/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002056 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002057 * @hsotg: The driver state.
2058 * @ep: The index number of the endpoint
2059 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002060 * @mc: The multicount value
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002061 *
2062 * Configure the maximum packet size for the given endpoint, updating
2063 * the hardware control registers to reflect this.
2064 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002065static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002066 unsigned int ep, unsigned int mps,
2067 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002068{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002069 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002070 void __iomem *regs = hsotg->regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002071 u32 reg;
2072
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002073 hs_ep = index_to_ep(hsotg, ep, dir_in);
2074 if (!hs_ep)
2075 return;
2076
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002077 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002078 u32 mps_bytes = mps;
2079
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002080 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002081 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2082 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002083 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002084 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002085 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002086 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002087 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002088 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002089 hs_ep->mc = mc;
2090 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002091 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002092 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002093 }
2094
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002095 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002096 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002097 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002098 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002099 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002100 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002101 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002102 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002103 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002104 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002105 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002106
2107 return;
2108
2109bad_mps:
2110 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2111}
2112
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002113/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002114 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002115 * @hsotg: The driver state
2116 * @idx: The index for the endpoint (0..15)
2117 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002118static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002119{
2120 int timeout;
2121 int val;
2122
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002123 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2124 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002125
2126 /* wait until the fifo is flushed */
2127 timeout = 100;
2128
2129 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002130 val = dwc2_readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002131
Dinh Nguyen47a16852014-04-14 14:13:34 -07002132 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002133 break;
2134
2135 if (--timeout == 0) {
2136 dev_err(hsotg->dev,
2137 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
2138 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02002139 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002140 }
2141
2142 udelay(1);
2143 }
2144}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002145
2146/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002147 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002148 * @hsotg: The driver state
2149 * @hs_ep: The driver endpoint to check.
2150 *
2151 * Check to see if there is a request that has data to send, and if so
2152 * make an attempt to write data into the FIFO.
2153 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002154static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
2155 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002156{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002157 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002158
Robert Baldygaafcf4162013-09-19 11:50:19 +02002159 if (!hs_ep->dir_in || !hs_req) {
2160 /**
2161 * if request is not enqueued, we disable interrupts
2162 * for endpoints, excepting ep0
2163 */
2164 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002165 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
Robert Baldygaafcf4162013-09-19 11:50:19 +02002166 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002167 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002168 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002169
2170 if (hs_req->req.actual < hs_req->req.length) {
2171 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2172 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002173 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002174 }
2175
2176 return 0;
2177}
2178
2179/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002180 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002181 * @hsotg: The device state.
2182 * @hs_ep: The endpoint that has just completed.
2183 *
2184 * An IN transfer has been completed, update the transfer's state and then
2185 * call the relevant completion routines.
2186 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002187static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
2188 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002189{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002190 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002191 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002192 int size_left, size_done;
2193
2194 if (!hs_req) {
2195 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2196 return;
2197 }
2198
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002199 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002200 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2201 dev_dbg(hsotg->dev, "zlp packet sent\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002202 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002203 if (hsotg->test_mode) {
2204 int ret;
2205
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002206 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002207 if (ret < 0) {
2208 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
2209 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002210 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002211 return;
2212 }
2213 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002214 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002215 return;
2216 }
2217
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002218 /*
2219 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002220 * in the endpoint size register and then working it out from
2221 * the amount we loaded for the transfer.
2222 *
2223 * We do this even for DMA, as the transfer may have incremented
2224 * past the end of the buffer (DMA transfers are always 32bit
2225 * aligned).
2226 */
2227
Dinh Nguyen47a16852014-04-14 14:13:34 -07002228 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002229
2230 size_done = hs_ep->size_loaded - size_left;
2231 size_done += hs_ep->last_load;
2232
2233 if (hs_req->req.actual != size_done)
2234 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2235 __func__, hs_req->req.actual, size_done);
2236
2237 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002238 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2239 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002240
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002241 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2242 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002243 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002244 return;
2245 }
2246
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002247 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002248 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002249 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002250 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002251 /* transfer will be completed on next complete interrupt */
2252 return;
2253 }
2254
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002255 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2256 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002257 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002258 return;
2259 }
2260
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002261 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002262}
2263
2264/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002265 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2266 * @hsotg: The device state.
2267 * @idx: Index of ep.
2268 * @dir_in: Endpoint direction 1-in 0-out.
2269 *
2270 * Reads for endpoint with given index and direction, by masking
2271 * epint_reg with coresponding mask.
2272 */
2273static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2274 unsigned int idx, int dir_in)
2275{
2276 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2277 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2278 u32 ints;
2279 u32 mask;
2280 u32 diepempmsk;
2281
2282 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2283 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2284 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2285 mask |= DXEPINT_SETUP_RCVD;
2286
2287 ints = dwc2_readl(hsotg->regs + epint_reg);
2288 ints &= mask;
2289 return ints;
2290}
2291
2292/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002293 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2294 * @hs_ep: The endpoint on which interrupt is asserted.
2295 *
2296 * This interrupt indicates that the endpoint has been disabled per the
2297 * application's request.
2298 *
2299 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2300 * in case of ISOC completes current request.
2301 *
2302 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2303 * request starts it.
2304 */
2305static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2306{
2307 struct dwc2_hsotg *hsotg = hs_ep->parent;
2308 struct dwc2_hsotg_req *hs_req;
2309 unsigned char idx = hs_ep->index;
2310 int dir_in = hs_ep->dir_in;
2311 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2312 int dctl = dwc2_readl(hsotg->regs + DCTL);
2313
2314 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2315
2316 if (dir_in) {
2317 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2318
2319 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2320
2321 if (hs_ep->isochronous) {
2322 dwc2_hsotg_complete_in(hsotg, hs_ep);
2323 return;
2324 }
2325
2326 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2327 int dctl = dwc2_readl(hsotg->regs + DCTL);
2328
2329 dctl |= DCTL_CGNPINNAK;
2330 dwc2_writel(dctl, hsotg->regs + DCTL);
2331 }
2332 return;
2333 }
2334
2335 if (dctl & DCTL_GOUTNAKSTS) {
2336 dctl |= DCTL_CGOUTNAK;
2337 dwc2_writel(dctl, hsotg->regs + DCTL);
2338 }
2339
2340 if (!hs_ep->isochronous)
2341 return;
2342
2343 if (list_empty(&hs_ep->queue)) {
2344 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2345 __func__, hs_ep);
2346 return;
2347 }
2348
2349 do {
2350 hs_req = get_ep_head(hs_ep);
2351 if (hs_req)
2352 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2353 -ENODATA);
2354 dwc2_gadget_incr_frame_num(hs_ep);
2355 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2356
2357 dwc2_gadget_start_next_request(hs_ep);
2358}
2359
2360/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002361 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2362 * @hs_ep: The endpoint on which interrupt is asserted.
2363 *
2364 * This is starting point for ISOC-OUT transfer, synchronization done with
2365 * first out token received from host while corresponding EP is disabled.
2366 *
2367 * Device does not know initial frame in which out token will come. For this
2368 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2369 * getting this interrupt SW starts calculation for next transfer frame.
2370 */
2371static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2372{
2373 struct dwc2_hsotg *hsotg = ep->parent;
2374 int dir_in = ep->dir_in;
2375 u32 doepmsk;
2376
2377 if (dir_in || !ep->isochronous)
2378 return;
2379
2380 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
2381
2382 if (ep->interval > 1 &&
2383 ep->target_frame == TARGET_FRAME_INITIAL) {
2384 u32 dsts;
2385 u32 ctrl;
2386
2387 dsts = dwc2_readl(hsotg->regs + DSTS);
2388 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2389 dwc2_gadget_incr_frame_num(ep);
2390
2391 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2392 if (ep->target_frame & 0x1)
2393 ctrl |= DXEPCTL_SETODDFR;
2394 else
2395 ctrl |= DXEPCTL_SETEVENFR;
2396
2397 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2398 }
2399
2400 dwc2_gadget_start_next_request(ep);
2401 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2402 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2403 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2404}
2405
2406/**
2407* dwc2_gadget_handle_nak - handle NAK interrupt
2408* @hs_ep: The endpoint on which interrupt is asserted.
2409*
2410* This is starting point for ISOC-IN transfer, synchronization done with
2411* first IN token received from host while corresponding EP is disabled.
2412*
2413* Device does not know when first one token will arrive from host. On first
2414* token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2415* and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2416* sent in response to that as there was no data in FIFO. SW is basing on this
2417* interrupt to obtain frame in which token has come and then based on the
2418* interval calculates next frame for transfer.
2419*/
2420static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2421{
2422 struct dwc2_hsotg *hsotg = hs_ep->parent;
2423 int dir_in = hs_ep->dir_in;
2424
2425 if (!dir_in || !hs_ep->isochronous)
2426 return;
2427
2428 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
2429 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2430 if (hs_ep->interval > 1) {
2431 u32 ctrl = dwc2_readl(hsotg->regs +
2432 DIEPCTL(hs_ep->index));
2433 if (hs_ep->target_frame & 0x1)
2434 ctrl |= DXEPCTL_SETODDFR;
2435 else
2436 ctrl |= DXEPCTL_SETEVENFR;
2437
2438 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2439 }
2440
2441 dwc2_hsotg_complete_request(hsotg, hs_ep,
2442 get_ep_head(hs_ep), 0);
2443 }
2444
2445 dwc2_gadget_incr_frame_num(hs_ep);
2446}
2447
2448/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002449 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002450 * @hsotg: The driver state
2451 * @idx: The index for the endpoint (0..15)
2452 * @dir_in: Set if this is an IN endpoint
2453 *
2454 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002455 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002456static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002457 int dir_in)
2458{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002459 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002460 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2461 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2462 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002463 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002464 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002465
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002466 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002467 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002468
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002469 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002470 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002471
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002472 if (!hs_ep) {
2473 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
2474 __func__, idx, dir_in ? "in" : "out");
2475 return;
2476 }
2477
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002478 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2479 __func__, idx, dir_in ? "in" : "out", ints);
2480
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002481 /* Don't process XferCompl interrupt if it is a setup packet */
2482 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2483 ints &= ~DXEPINT_XFERCOMPL;
2484
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002485 if (ints & DXEPINT_STSPHSERCVD)
2486 dev_dbg(hsotg->dev, "%s: StsPhseRcvd asserted\n", __func__);
Robert Baldyga1479e842013-10-09 08:41:57 +02002487
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002488 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002489 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002490 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002491 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2492 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002493
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002494 /*
2495 * we get OutDone from the FIFO, so we only need to look
2496 * at completing IN requests here
2497 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498 if (dir_in) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002499 if (hs_ep->isochronous && hs_ep->interval > 1)
2500 dwc2_gadget_incr_frame_num(hs_ep);
2501
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002502 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002503 if (ints & DXEPINT_NAKINTRPT)
2504 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002505
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002506 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002507 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002508 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002509 /*
2510 * We're using DMA, we need to fire an OutDone here
2511 * as we ignore the RXFIFO.
2512 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002513 if (hs_ep->isochronous && hs_ep->interval > 1)
2514 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002515
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002516 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002517 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002518 }
2519
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002520 if (ints & DXEPINT_EPDISBLD)
2521 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002522
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002523 if (ints & DXEPINT_OUTTKNEPDIS)
2524 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2525
2526 if (ints & DXEPINT_NAKINTRPT)
2527 dwc2_gadget_handle_nak(hs_ep);
2528
Dinh Nguyen47a16852014-04-14 14:13:34 -07002529 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002530 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002531
Dinh Nguyen47a16852014-04-14 14:13:34 -07002532 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002533 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2534
2535 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002536 /*
2537 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002538 * setup packet. In non-DMA mode we'd get this
2539 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002540 * the setup here.
2541 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002542
2543 if (dir_in)
2544 WARN_ON_ONCE(1);
2545 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002546 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002547 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002548 }
2549
Dinh Nguyen47a16852014-04-14 14:13:34 -07002550 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002551 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002552
Robert Baldyga1479e842013-10-09 08:41:57 +02002553 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002554 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002555 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002556 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2557 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002558 }
2559
2560 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002561 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002562 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2563 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002564 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002565
2566 /* FIFO has space or is empty (see GAHBCFG) */
2567 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002568 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002569 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2570 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002571 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002572 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002573 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002574 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002575}
2576
2577/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002578 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002579 * @hsotg: The device state.
2580 *
2581 * Handle updating the device settings after the enumeration phase has
2582 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002583 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002584static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002585{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002586 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09002587 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002588
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002589 /*
2590 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002591 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002592 * we connected at.
2593 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002594
2595 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2596
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002597 /*
2598 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002599 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002600 * not advertise a 64byte MPS on EP0.
2601 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002602
2603 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01002604 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002605 case DSTS_ENUMSPD_FS:
2606 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002607 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002608 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002609 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002610 break;
2611
Dinh Nguyen47a16852014-04-14 14:13:34 -07002612 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002613 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002614 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002615 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002616 break;
2617
Dinh Nguyen47a16852014-04-14 14:13:34 -07002618 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002619 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002620 /*
2621 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002622 * moment, and the documentation seems to imply that it isn't
2623 * supported by the PHYs on some of the devices.
2624 */
2625 break;
2626 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002627 dev_info(hsotg->dev, "new device is %s\n",
2628 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002629
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002630 /*
2631 * we should now know the maximum packet size for an
2632 * endpoint, so set the endpoints to a default value.
2633 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002634
2635 if (ep0_mps) {
2636 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002637 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002638 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
2639 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002640 for (i = 1; i < hsotg->num_of_eps; i++) {
2641 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002642 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
2643 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002644 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002645 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
2646 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002647 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002648 }
2649
2650 /* ensure after enumeration our EP0 is active */
2651
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002652 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002653
2654 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002655 dwc2_readl(hsotg->regs + DIEPCTL0),
2656 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002657}
2658
2659/**
2660 * kill_all_requests - remove all requests from the endpoint's queue
2661 * @hsotg: The device state.
2662 * @ep: The endpoint the requests may be on.
2663 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002664 *
2665 * Go through the requests on the given endpoint and mark them
2666 * completed with the given result code.
2667 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002668static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002669 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af42014-12-16 11:51:44 +01002670 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002671{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002672 struct dwc2_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002673 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002674
Robert Baldyga6b448af42014-12-16 11:51:44 +01002675 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002676
Robert Baldyga6b448af42014-12-16 11:51:44 +01002677 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002678 dwc2_hsotg_complete_request(hsotg, ep, req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002679 result);
Robert Baldyga6b448af42014-12-16 11:51:44 +01002680
Robert Baldygab203d0a2014-09-09 10:44:56 +02002681 if (!hsotg->dedicated_fifos)
2682 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07002683 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002684 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002685 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002686}
2687
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002688/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002689 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002690 * @hsotg: The device state.
2691 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002692 * The device has been disconnected. Remove all current
2693 * transactions and signal the gadget driver that this
2694 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002695 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002696void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002697{
2698 unsigned ep;
2699
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002700 if (!hsotg->connected)
2701 return;
2702
2703 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002704 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002705
2706 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2707 if (hsotg->eps_in[ep])
2708 kill_all_requests(hsotg, hsotg->eps_in[ep],
2709 -ESHUTDOWN);
2710 if (hsotg->eps_out[ep])
2711 kill_all_requests(hsotg, hsotg->eps_out[ep],
2712 -ESHUTDOWN);
2713 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002714
2715 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02002716 hsotg->lx_state = DWC2_L3;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002717}
2718
2719/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002720 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002721 * @hsotg: The device state:
2722 * @periodic: True if this is a periodic FIFO interrupt
2723 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002724static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002725{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002726 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002727 int epno, ret;
2728
2729 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002730 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002731 ep = index_to_ep(hsotg, epno, 1);
2732
2733 if (!ep)
2734 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002735
2736 if (!ep->dir_in)
2737 continue;
2738
2739 if ((periodic && !ep->periodic) ||
2740 (!periodic && ep->periodic))
2741 continue;
2742
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002743 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002744 if (ret < 0)
2745 break;
2746 }
2747}
2748
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002749/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002750#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2751 GINTSTS_PTXFEMP | \
2752 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002753
2754/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002755 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002756 * @hsotg: The device state
2757 *
2758 * Issue a soft reset to the core, and await the core finishing it.
2759 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002760void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002761 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002762{
Gregory Herrero1ee69032015-09-29 12:08:27 +02002763 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002764 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002765 u32 usbcfg;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002766
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02002767 /* Kill any ep0 requests as controller will be reinitialized */
2768 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
2769
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002770 if (!is_usb_reset)
John Youn241729b2015-12-17 11:17:59 -08002771 if (dwc2_core_reset(hsotg))
Gregory Herrero86de4892015-09-29 12:08:21 +02002772 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002773
2774 /*
2775 * we must now enable ep0 ready for host detection and then
2776 * set configuration.
2777 */
2778
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002779 /* keep other bits untouched (so e.g. forced modes are not lost) */
2780 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2781 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
2782 GUSBCFG_HNPCAP);
2783
Lukasz Majewski308d7342012-05-04 14:17:05 +02002784 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01002785 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002786 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
2787 (val << GUSBCFG_USBTRDTIM_SHIFT);
2788 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002789
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002790 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002791
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002792 if (!is_usb_reset)
2793 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002794
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002795 dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002796
2797 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002798 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002799
2800 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002801 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02002802 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002803 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02002804 GINTSTS_USBRST | GINTSTS_RESETDET |
2805 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Roman Bacikec1f9d92015-09-10 18:13:43 -07002806 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
2807 GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02002808
John Younbea8e862016-11-03 17:55:53 -07002809 if (hsotg->params.external_id_pin_ctl <= 0)
Gregory Herrero1ee69032015-09-29 12:08:27 +02002810 intmsk |= GINTSTS_CONIDSTSCHNG;
2811
2812 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002813
2814 if (using_dma(hsotg))
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002815 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2816 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
2817 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002818 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002819 dwc2_writel(((hsotg->dedicated_fifos) ?
2820 (GAHBCFG_NP_TXF_EMP_LVL |
2821 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2822 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002823
2824 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002825 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2826 * when we have no data to transfer. Otherwise we get being flooded by
2827 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002828 */
2829
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002830 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01002831 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002832 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002833 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002834 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002835
2836 /*
2837 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2838 * DMA mode we may need this.
2839 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002840 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002841 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002842 DOEPMSK_SETUPMSK | DOEPMSK_STSPHSERCVDMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002843 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002844
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002845 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002846
2847 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002848 dwc2_readl(hsotg->regs + DIEPCTL0),
2849 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002850
2851 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002852 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002853
2854 /*
2855 * Enable the RXFIFO when in slave mode, as this is how we collect
2856 * the data. In DMA mode, we get events from the FIFO but also
2857 * things we cannot process, so do not use it.
2858 */
2859 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002860 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002861
2862 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002863 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2864 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002865
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002866 if (!is_usb_reset) {
2867 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2868 udelay(10); /* see openiboot */
2869 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2870 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02002871
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002872 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002873
2874 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002875 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002876 * writing to the EPCTL register..
2877 */
2878
2879 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002880 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002881 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002882
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002883 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002884 DXEPCTL_CNAK | DXEPCTL_EPENA |
2885 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002886 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002887
2888 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002889 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002890 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002891
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002892 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002893
2894 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002895 dwc2_readl(hsotg->regs + DIEPCTL0),
2896 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002897
2898 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002899 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
2900 if (!is_usb_reset)
2901 val |= DCTL_SFTDISCON;
2902 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002903
2904 /* must be at-least 3ms to allow bus to see disconnect */
2905 mdelay(3);
2906
Gregory Herrero065d3932015-09-22 15:16:54 +02002907 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002908}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002909
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002910static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002911{
2912 /* set the soft-disconnect bit */
2913 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2914}
2915
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002916void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002917{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002918 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002919 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002920}
2921
2922/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07002923 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
2924 * @hsotg: The device state:
2925 *
2926 * This interrupt indicates one of the following conditions occurred while
2927 * transmitting an ISOC transaction.
2928 * - Corrupted IN Token for ISOC EP.
2929 * - Packet not complete in FIFO.
2930 *
2931 * The following actions will be taken:
2932 * - Determine the EP
2933 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
2934 */
2935static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
2936{
2937 struct dwc2_hsotg_ep *hs_ep;
2938 u32 epctrl;
2939 u32 idx;
2940
2941 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
2942
2943 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2944 hs_ep = hsotg->eps_in[idx];
2945 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
2946 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
2947 dwc2_gadget_target_frame_elapsed(hs_ep)) {
2948 epctrl |= DXEPCTL_SNAK;
2949 epctrl |= DXEPCTL_EPDIS;
2950 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
2951 }
2952 }
2953
2954 /* Clear interrupt */
2955 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
2956}
2957
2958/**
2959 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
2960 * @hsotg: The device state:
2961 *
2962 * This interrupt indicates one of the following conditions occurred while
2963 * transmitting an ISOC transaction.
2964 * - Corrupted OUT Token for ISOC EP.
2965 * - Packet not complete in FIFO.
2966 *
2967 * The following actions will be taken:
2968 * - Determine the EP
2969 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
2970 */
2971static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
2972{
2973 u32 gintsts;
2974 u32 gintmsk;
2975 u32 epctrl;
2976 struct dwc2_hsotg_ep *hs_ep;
2977 int idx;
2978
2979 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
2980
2981 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2982 hs_ep = hsotg->eps_out[idx];
2983 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
2984 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
2985 dwc2_gadget_target_frame_elapsed(hs_ep)) {
2986 /* Unmask GOUTNAKEFF interrupt */
2987 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
2988 gintmsk |= GINTSTS_GOUTNAKEFF;
2989 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
2990
2991 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2992 if (!(gintsts & GINTSTS_GOUTNAKEFF))
2993 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
2994 }
2995 }
2996
2997 /* Clear interrupt */
2998 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
2999}
3000
3001/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003002 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003003 * @irq: The IRQ number triggered
3004 * @pw: The pw value when registered the handler.
3005 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003006static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003007{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003008 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003009 int retry_count = 8;
3010 u32 gintsts;
3011 u32 gintmsk;
3012
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003013 if (!dwc2_is_device_mode(hsotg))
3014 return IRQ_NONE;
3015
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003016 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003017irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003018 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3019 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003020
3021 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3022 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3023
3024 gintsts &= gintmsk;
3025
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003026 if (gintsts & GINTSTS_RESETDET) {
3027 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3028
3029 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3030
3031 /* This event must be used only if controller is suspended */
3032 if (hsotg->lx_state == DWC2_L2) {
3033 dwc2_exit_hibernation(hsotg, true);
3034 hsotg->lx_state = DWC2_L0;
3035 }
3036 }
3037
3038 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
3039
3040 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3041 u32 connected = hsotg->connected;
3042
3043 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3044 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3045 dwc2_readl(hsotg->regs + GNPTXSTS));
3046
3047 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3048
3049 /* Report disconnection if it is not already done. */
3050 dwc2_hsotg_disconnect(hsotg);
3051
3052 if (usb_status & GOTGCTL_BSESVLD && connected)
3053 dwc2_hsotg_core_init_disconnected(hsotg, true);
3054 }
3055
Dinh Nguyen47a16852014-04-14 14:13:34 -07003056 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003057 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003058
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003059 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003060 }
3061
Dinh Nguyen47a16852014-04-14 14:13:34 -07003062 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003063 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3064 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003065 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003066 int ep;
3067
Robert Baldyga7e804652013-09-19 11:50:20 +02003068 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003069 daint_out = daint >> DAINT_OUTEP_SHIFT;
3070 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003071
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003072 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3073
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003074 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3075 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003076 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003077 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003078 }
3079
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003080 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3081 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003083 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003084 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003085 }
3086
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003087 /* check both FIFOs */
3088
Dinh Nguyen47a16852014-04-14 14:13:34 -07003089 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003090 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3091
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003092 /*
3093 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003094 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003095 * it needs re-enabling
3096 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003097
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003098 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3099 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003100 }
3101
Dinh Nguyen47a16852014-04-14 14:13:34 -07003102 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003103 dev_dbg(hsotg->dev, "PTxFEmp\n");
3104
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003105 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003106
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003107 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3108 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003109 }
3110
Dinh Nguyen47a16852014-04-14 14:13:34 -07003111 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003112 /*
3113 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003114 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003115 * set.
3116 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003117
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003118 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003119 }
3120
Dinh Nguyen47a16852014-04-14 14:13:34 -07003121 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003122 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003123 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003124 }
3125
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003126 /*
3127 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003128 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003129 * the occurrence.
3130 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003131
Dinh Nguyen47a16852014-04-14 14:13:34 -07003132 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003133 u8 idx;
3134 u32 epctrl;
3135 u32 gintmsk;
3136 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003137
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003138 /* Mask this interrupt */
3139 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3140 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3141 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003142
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003143 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3144 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3145 hs_ep = hsotg->eps_out[idx];
3146 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3147
3148 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
3149 epctrl |= DXEPCTL_SNAK;
3150 epctrl |= DXEPCTL_EPDIS;
3151 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3152 }
3153 }
3154
3155 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003156 }
3157
Dinh Nguyen47a16852014-04-14 14:13:34 -07003158 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003159 dev_info(hsotg->dev, "GINNakEff triggered\n");
3160
Gregory Herrero3be99cd2015-12-07 12:07:31 +01003161 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003162
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003163 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003164 }
3165
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003166 if (gintsts & GINTSTS_INCOMPL_SOIN)
3167 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003168
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003169 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3170 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003171
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003172 /*
3173 * if we've had fifo events, we should try and go around the
3174 * loop again to see if there's any point in returning yet.
3175 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003176
3177 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
3178 goto irq_retry;
3179
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003180 spin_unlock(&hsotg->lock);
3181
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003182 return IRQ_HANDLED;
3183}
3184
3185/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003186 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003187 * @ep: The USB endpint to configure
3188 * @desc: The USB endpoint descriptor to configure with.
3189 *
3190 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003191 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003192static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003193 const struct usb_endpoint_descriptor *desc)
3194{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003195 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003196 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003197 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003198 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003199 u32 epctrl_reg;
3200 u32 epctrl;
3201 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003202 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003203 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003204 unsigned int dir_in;
3205 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003206 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003207
3208 dev_dbg(hsotg->dev,
3209 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3210 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3211 desc->wMaxPacketSize, desc->bInterval);
3212
3213 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003214 if (index == 0) {
3215 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3216 return -EINVAL;
3217 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003218
3219 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3220 if (dir_in != hs_ep->dir_in) {
3221 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3222 return -EINVAL;
3223 }
3224
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003225 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003226 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003227
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003228 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003229
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003230 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003231 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003232
3233 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3234 __func__, epctrl, epctrl_reg);
3235
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003236 /* Allocate DMA descriptor chain for non-ctrl endpoints */
3237 if (using_desc_dma(hsotg)) {
3238 hs_ep->desc_list = dma_alloc_coherent(hsotg->dev,
3239 MAX_DMA_DESC_NUM_GENERIC *
3240 sizeof(struct dwc2_dma_desc),
3241 &hs_ep->desc_list_dma, GFP_KERNEL);
3242 if (!hs_ep->desc_list) {
3243 ret = -ENOMEM;
3244 goto error2;
3245 }
3246 }
3247
Lukasz Majewski22258f42012-06-14 10:02:24 +02003248 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003249
Dinh Nguyen47a16852014-04-14 14:13:34 -07003250 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3251 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003252
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003253 /*
3254 * mark the endpoint as active, otherwise the core may ignore
3255 * transactions entirely for this endpoint
3256 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003257 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003258
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003259 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003260 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003261
3262 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003263 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003264 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003265 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003266 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003267
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003268 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3269 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003270 epctrl |= DXEPCTL_EPTYPE_ISO;
3271 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003272 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003273 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003274 hs_ep->target_frame = TARGET_FRAME_INITIAL;
3275 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003276 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003277 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3278 mask |= DIEPMSK_NAKMSK;
3279 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3280 } else {
3281 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3282 mask |= DOEPMSK_OUTTKNEPDISMSK;
3283 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3284 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003285 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003286
3287 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003288 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003289 break;
3290
3291 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003292 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003293 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003294
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003295 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3296 hs_ep->interval = 1 << (desc->bInterval - 1);
3297
Dinh Nguyen47a16852014-04-14 14:13:34 -07003298 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003299 break;
3300
3301 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003302 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003303 break;
3304 }
3305
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003306 /*
3307 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003308 * a unique tx-fifo even if it is non-periodic.
3309 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003310 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003311 u32 fifo_index = 0;
3312 u32 fifo_size = UINT_MAX;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003313 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003314 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02003315 if (hsotg->fifo_map & (1<<i))
3316 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003317 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Robert Baldygab203d0a2014-09-09 10:44:56 +02003318 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
3319 if (val < size)
3320 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003321 /* Search for smallest acceptable fifo */
3322 if (val < fifo_size) {
3323 fifo_size = val;
3324 fifo_index = i;
3325 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003326 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003327 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003328 dev_err(hsotg->dev,
3329 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303330 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003331 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303332 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003333 hsotg->fifo_map |= 1 << fifo_index;
3334 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3335 hs_ep->fifo_index = fifo_index;
3336 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003337 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003338
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003339 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003340 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003341 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003342
3343 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3344 __func__, epctrl);
3345
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003346 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003347 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003348 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003349
3350 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003351 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003352
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003353error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003354 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003355
3356error2:
3357 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
3358 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3359 sizeof(struct dwc2_dma_desc),
3360 hs_ep->desc_list, hs_ep->desc_list_dma);
3361 hs_ep->desc_list = NULL;
3362 }
3363
Julia Lawall19c190f2010-03-29 17:36:44 +02003364 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003365}
3366
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003367/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003368 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003369 * @ep: The endpoint to disable.
3370 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003371static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003372{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003373 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003374 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003375 int dir_in = hs_ep->dir_in;
3376 int index = hs_ep->index;
3377 unsigned long flags;
3378 u32 epctrl_reg;
3379 u32 ctrl;
3380
Marek Szyprowski1e011292014-09-09 10:44:54 +02003381 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003382
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003383 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003384 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3385 return -EINVAL;
3386 }
3387
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003388 /* Remove DMA memory allocated for non-control Endpoints */
3389 if (using_desc_dma(hsotg)) {
3390 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3391 sizeof(struct dwc2_dma_desc),
3392 hs_ep->desc_list, hs_ep->desc_list_dma);
3393 hs_ep->desc_list = NULL;
3394 }
3395
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003396 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003397
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003398 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003399
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003400 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07003401 ctrl &= ~DXEPCTL_EPENA;
3402 ctrl &= ~DXEPCTL_USBACTEP;
3403 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003404
3405 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003406 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003407
3408 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003409 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003410
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003411 /* terminate all requests with shutdown */
3412 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3413
Robert Baldyga1c07b202016-08-29 13:39:00 -07003414 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3415 hs_ep->fifo_index = 0;
3416 hs_ep->fifo_size = 0;
3417
Lukasz Majewski22258f42012-06-14 10:02:24 +02003418 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003419 return 0;
3420}
3421
3422/**
3423 * on_list - check request is on the given endpoint
3424 * @ep: The endpoint to check.
3425 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003426 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003427static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003428{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003429 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003430
3431 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
3432 if (req == test)
3433 return true;
3434 }
3435
3436 return false;
3437}
3438
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003439static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3440 u32 bit, u32 timeout)
3441{
3442 u32 i;
3443
3444 for (i = 0; i < timeout; i++) {
3445 if (dwc2_readl(hs_otg->regs + reg) & bit)
3446 return 0;
3447 udelay(1);
3448 }
3449
3450 return -ETIMEDOUT;
3451}
3452
3453static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3454 struct dwc2_hsotg_ep *hs_ep)
3455{
3456 u32 epctrl_reg;
3457 u32 epint_reg;
3458
3459 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3460 DOEPCTL(hs_ep->index);
3461 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3462 DOEPINT(hs_ep->index);
3463
3464 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3465 hs_ep->name);
3466 if (hs_ep->dir_in) {
3467 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3468 /* Wait for Nak effect */
3469 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3470 DXEPINT_INEPNAKEFF, 100))
3471 dev_warn(hsotg->dev,
3472 "%s: timeout DIEPINT.NAKEFF\n", __func__);
3473 } else {
Vardan Mikayelyan6b58cb02016-05-25 18:07:02 -07003474 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3475 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003476
3477 /* Wait for global nak to take effect */
3478 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
Du, Changbin0676c7e2015-12-04 15:38:23 +08003479 GINTSTS_GOUTNAKEFF, 100))
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003480 dev_warn(hsotg->dev,
Du, Changbin0676c7e2015-12-04 15:38:23 +08003481 "%s: timeout GINTSTS.GOUTNAKEFF\n", __func__);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003482 }
3483
3484 /* Disable ep */
3485 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3486
3487 /* Wait for ep to be disabled */
3488 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3489 dev_warn(hsotg->dev,
3490 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3491
3492 if (hs_ep->dir_in) {
3493 if (hsotg->dedicated_fifos) {
3494 dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) |
3495 GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL);
3496 /* Wait for fifo flush */
3497 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
3498 GRSTCTL_TXFFLSH, 100))
3499 dev_warn(hsotg->dev,
3500 "%s: timeout flushing fifos\n",
3501 __func__);
3502 }
3503 /* TODO: Flush shared tx fifo */
3504 } else {
3505 /* Remove global NAKs */
Du, Changbin0676c7e2015-12-04 15:38:23 +08003506 __bic32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003507 }
3508}
3509
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003510/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003511 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003512 * @ep: The endpoint to dequeue.
3513 * @req: The request to be removed from a queue.
3514 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003515static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003516{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003517 struct dwc2_hsotg_req *hs_req = our_req(req);
3518 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003519 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003520 unsigned long flags;
3521
Marek Szyprowski1e011292014-09-09 10:44:54 +02003522 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003523
Lukasz Majewski22258f42012-06-14 10:02:24 +02003524 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003525
3526 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02003527 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003528 return -EINVAL;
3529 }
3530
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003531 /* Dequeue already started request */
3532 if (req == &hs_ep->req->req)
3533 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
3534
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003535 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02003536 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003537
3538 return 0;
3539}
3540
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003541/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003542 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003543 * @ep: The endpoint to set halt.
3544 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003545 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
3546 * the endpoint is busy processing requests.
3547 *
3548 * We need to stall the endpoint immediately if request comes from set_feature
3549 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003550 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003551static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003552{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003553 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003554 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003555 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003556 u32 epreg;
3557 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003558 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003559
3560 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
3561
Robert Baldygac9f721b2014-01-14 08:36:00 +01003562 if (index == 0) {
3563 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003564 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01003565 else
3566 dev_warn(hs->dev,
3567 "%s: can't clear halt on ep0\n", __func__);
3568 return 0;
3569 }
3570
Vahram Aharonyan15186f12016-05-23 22:41:59 -07003571 if (hs_ep->isochronous) {
3572 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
3573 return -EINVAL;
3574 }
3575
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003576 if (!now && value && !list_empty(&hs_ep->queue)) {
3577 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
3578 ep->name);
3579 return -EAGAIN;
3580 }
3581
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003582 if (hs_ep->dir_in) {
3583 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003584 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003585
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003586 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05003587 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003588 if (epctl & DXEPCTL_EPENA)
3589 epctl |= DXEPCTL_EPDIS;
3590 } else {
3591 epctl &= ~DXEPCTL_STALL;
3592 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3593 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3594 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3595 epctl |= DXEPCTL_SETD0PID;
3596 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003597 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003598 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003599
3600 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003601 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003602
3603 if (value)
3604 epctl |= DXEPCTL_STALL;
3605 else {
3606 epctl &= ~DXEPCTL_STALL;
3607 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3608 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3609 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3610 epctl |= DXEPCTL_SETD0PID;
3611 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003612 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003613 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003614
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003615 hs_ep->halted = value;
3616
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003617 return 0;
3618}
3619
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003620/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003621 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003622 * @ep: The endpoint to set halt.
3623 * @value: Set or unset the halt.
3624 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003625static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003626{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003627 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003628 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003629 unsigned long flags = 0;
3630 int ret = 0;
3631
3632 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003633 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003634 spin_unlock_irqrestore(&hs->lock, flags);
3635
3636 return ret;
3637}
3638
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003639static struct usb_ep_ops dwc2_hsotg_ep_ops = {
3640 .enable = dwc2_hsotg_ep_enable,
3641 .disable = dwc2_hsotg_ep_disable,
3642 .alloc_request = dwc2_hsotg_ep_alloc_request,
3643 .free_request = dwc2_hsotg_ep_free_request,
3644 .queue = dwc2_hsotg_ep_queue_lock,
3645 .dequeue = dwc2_hsotg_ep_dequeue,
3646 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003647 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003648};
3649
3650/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003651 * dwc2_hsotg_init - initalize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003652 * @hsotg: The driver state
3653 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003654static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003655{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003656 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003657 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003658 /* unmask subset of endpoint interrupts */
3659
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003660 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
3661 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
3662 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003663
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003664 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
3665 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
3666 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003667
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003668 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003669
3670 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003671 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003672
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003673 /* setup fifos */
3674
3675 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003676 dwc2_readl(hsotg->regs + GRXFSIZ),
3677 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003678
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003679 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003680
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003681 /* keep other bits untouched (so e.g. forced modes are not lost) */
3682 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3683 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3684 GUSBCFG_HNPCAP);
3685
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003686 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003687 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003688 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3689 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
3690 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003691
Gregory Herrerof5090042015-01-09 13:38:47 +01003692 if (using_dma(hsotg))
3693 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003694}
3695
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003696/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003697 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003698 * @gadget: The usb gadget state
3699 * @driver: The usb gadget driver
3700 *
3701 * Perform initialization to prepare udc device and driver
3702 * to work.
3703 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003704static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003705 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003706{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003707 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003708 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003709 int ret;
3710
3711 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003712 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003713 return -ENODEV;
3714 }
3715
3716 if (!driver) {
3717 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3718 return -EINVAL;
3719 }
3720
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003721 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003722 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003723
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003724 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003725 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3726 return -EINVAL;
3727 }
3728
3729 WARN_ON(hsotg->driver);
3730
3731 driver->driver.bus = NULL;
3732 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003733 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003734 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3735
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003736 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
3737 ret = dwc2_lowlevel_hw_enable(hsotg);
3738 if (ret)
3739 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003740 }
3741
Gregory Herrerof6c01592015-01-09 13:38:41 +01003742 if (!IS_ERR_OR_NULL(hsotg->uphy))
3743 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003744
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003745 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07003746 if (dwc2_hw_is_device(hsotg)) {
3747 dwc2_hsotg_init(hsotg);
3748 dwc2_hsotg_core_init_disconnected(hsotg, false);
3749 }
3750
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003751 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003752 spin_unlock_irqrestore(&hsotg->lock, flags);
3753
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003754 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003755
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003756 return 0;
3757
3758err:
3759 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003760 return ret;
3761}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003762
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003763/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003764 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003765 * @gadget: The usb gadget state
3766 * @driver: The usb gadget driver
3767 *
3768 * Stop udc hw block and stay tunned for future transmissions
3769 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003770static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003771{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003772 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003773 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003774 int ep;
3775
3776 if (!hsotg)
3777 return -ENODEV;
3778
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003779 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003780 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3781 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003782 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003783 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003784 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003785 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003786
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003787 spin_lock_irqsave(&hsotg->lock, flags);
3788
Marek Szyprowski32805c32014-10-20 12:45:33 +02003789 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003790 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003791 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003792
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003793 spin_unlock_irqrestore(&hsotg->lock, flags);
3794
Gregory Herrerof6c01592015-01-09 13:38:41 +01003795 if (!IS_ERR_OR_NULL(hsotg->uphy))
3796 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003797
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003798 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3799 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003800
3801 return 0;
3802}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003803
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003804/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003805 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003806 * @gadget: The usb gadget state
3807 *
3808 * Read the {micro} frame number
3809 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003810static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003811{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003812 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003813}
3814
Lukasz Majewskia188b682012-06-22 09:29:56 +02003815/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003816 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02003817 * @gadget: The usb gadget state
3818 * @is_on: Current state of the USB PHY
3819 *
3820 * Connect/Disconnect the USB PHY pullup
3821 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003822static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02003823{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003824 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003825 unsigned long flags = 0;
3826
Gregory Herrero77ba9112015-09-29 12:08:19 +02003827 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
3828 hsotg->op_state);
3829
3830 /* Don't modify pullup state while in host mode */
3831 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3832 hsotg->enabled = is_on;
3833 return 0;
3834 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02003835
3836 spin_lock_irqsave(&hsotg->lock, flags);
3837 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003838 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003839 dwc2_hsotg_core_init_disconnected(hsotg, false);
3840 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003841 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003842 dwc2_hsotg_core_disconnect(hsotg);
3843 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003844 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02003845 }
3846
3847 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3848 spin_unlock_irqrestore(&hsotg->lock, flags);
3849
3850 return 0;
3851}
3852
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003853static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01003854{
3855 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3856 unsigned long flags;
3857
3858 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
3859 spin_lock_irqsave(&hsotg->lock, flags);
3860
Gregory Herrero61f72232015-09-29 12:08:28 +02003861 /*
3862 * If controller is hibernated, it must exit from hibernation
3863 * before being initialized / de-initialized
3864 */
3865 if (hsotg->lx_state == DWC2_L2)
3866 dwc2_exit_hibernation(hsotg, false);
3867
Gregory Herrero83d98222015-01-09 13:39:02 +01003868 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02003869 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02003870
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003871 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01003872 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003873 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003874 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003875 dwc2_hsotg_core_disconnect(hsotg);
3876 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003877 }
3878
3879 spin_unlock_irqrestore(&hsotg->lock, flags);
3880 return 0;
3881}
3882
Gregory Herrero596d6962015-01-09 13:39:08 +01003883/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003884 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01003885 * @gadget: The usb gadget state
3886 * @mA: Amount of current
3887 *
3888 * Report how much power the device may consume to the phy.
3889 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003890static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01003891{
3892 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3893
3894 if (IS_ERR_OR_NULL(hsotg->uphy))
3895 return -ENOTSUPP;
3896 return usb_phy_set_power(hsotg->uphy, mA);
3897}
3898
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003899static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
3900 .get_frame = dwc2_hsotg_gadget_getframe,
3901 .udc_start = dwc2_hsotg_udc_start,
3902 .udc_stop = dwc2_hsotg_udc_stop,
3903 .pullup = dwc2_hsotg_pullup,
3904 .vbus_session = dwc2_hsotg_vbus_session,
3905 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003906};
3907
3908/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003909 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003910 * @hsotg: The device state.
3911 * @hs_ep: The endpoint to be initialised.
3912 * @epnum: The endpoint number
3913 *
3914 * Initialise the given endpoint (as part of the probe and device state
3915 * creation) to give to the gadget driver. Setup the endpoint name, any
3916 * direction information and other state that may be required.
3917 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003918static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
3919 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003920 int epnum,
3921 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003922{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003923 char *dir;
3924
3925 if (epnum == 0)
3926 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003927 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003928 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003929 else
3930 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003931
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003932 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003933 hs_ep->index = epnum;
3934
3935 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3936
3937 INIT_LIST_HEAD(&hs_ep->queue);
3938 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3939
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003940 /* add to the list of endpoints known by the gadget driver */
3941 if (epnum)
3942 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3943
3944 hs_ep->parent = hsotg;
3945 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003946 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003947 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003948
Robert Baldyga29545222015-07-31 16:00:18 +02003949 if (epnum == 0) {
3950 hs_ep->ep.caps.type_control = true;
3951 } else {
3952 hs_ep->ep.caps.type_iso = true;
3953 hs_ep->ep.caps.type_bulk = true;
3954 hs_ep->ep.caps.type_int = true;
3955 }
3956
3957 if (dir_in)
3958 hs_ep->ep.caps.dir_in = true;
3959 else
3960 hs_ep->ep.caps.dir_out = true;
3961
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003962 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003963 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003964 * to be something valid.
3965 */
3966
3967 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003968 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003969 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003970 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003971 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003972 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003973 }
3974}
3975
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003976/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003977 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003978 * @param: The device state
3979 *
3980 * Read the USB core HW configuration registers
3981 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003982static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003983{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003984 u32 cfg;
3985 u32 ep_type;
3986 u32 i;
3987
Ben Dooks10aebc72010-07-19 09:40:44 +01003988 /* check hardware configuration */
3989
John Youn43e90342015-12-17 11:17:45 -08003990 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
3991
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003992 /* Add ep0 */
3993 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003994
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003995 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003996 GFP_KERNEL);
3997 if (!hsotg->eps_in[0])
3998 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003999 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004000 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004001
John Youn43e90342015-12-17 11:17:45 -08004002 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004003 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004004 ep_type = cfg & 3;
4005 /* Direction in or both */
4006 if (!(ep_type & 2)) {
4007 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004008 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004009 if (!hsotg->eps_in[i])
4010 return -ENOMEM;
4011 }
4012 /* Direction out or both */
4013 if (!(ep_type & 1)) {
4014 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004015 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004016 if (!hsotg->eps_out[i])
4017 return -ENOMEM;
4018 }
4019 }
4020
John Youn43e90342015-12-17 11:17:45 -08004021 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4022 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004023
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004024 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4025 hsotg->num_of_eps,
4026 hsotg->dedicated_fifos ? "dedicated" : "shared",
4027 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004028 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004029}
4030
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004031/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004032 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004033 * @param: The device state
4034 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004035static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004036{
Mark Brown83a01802011-06-01 17:16:15 +01004037#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004038 struct device *dev = hsotg->dev;
4039 void __iomem *regs = hsotg->regs;
4040 u32 val;
4041 int idx;
4042
4043 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004044 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4045 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004046
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004047 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004048 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004049
4050 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004051 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004052
4053 /* show periodic fifo settings */
4054
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004055 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004056 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004057 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004058 val >> FIFOSIZE_DEPTH_SHIFT,
4059 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004060 }
4061
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004062 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004063 dev_info(dev,
4064 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004065 dwc2_readl(regs + DIEPCTL(idx)),
4066 dwc2_readl(regs + DIEPTSIZ(idx)),
4067 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004068
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004069 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004070 dev_info(dev,
4071 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004072 idx, dwc2_readl(regs + DOEPCTL(idx)),
4073 dwc2_readl(regs + DOEPTSIZ(idx)),
4074 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004075
4076 }
4077
4078 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004079 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004080#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004081}
4082
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004083/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004084 * dwc2_gadget_init - init function for gadget
4085 * @dwc2: The data structure for the DWC2 driver.
4086 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004087 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06004088int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004089{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004090 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004091 int epnum;
4092 int ret;
John Youn43e90342015-12-17 11:17:45 -08004093
Gregory Herrero0a176272015-01-09 13:38:52 +01004094 /* Dump fifo information */
4095 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004096 hsotg->params.g_np_tx_fifo_size);
4097 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004098
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004099 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004100 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004101 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02004102 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4103 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004104 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4105 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004106
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004107 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004108 if (ret) {
4109 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004110 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004111 }
4112
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004113 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4114 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004115 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004116 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004117
4118 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4119 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004120 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004121 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004122
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004123 if (using_desc_dma(hsotg)) {
4124 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4125 if (ret < 0)
4126 return ret;
4127 }
4128
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004129 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004130 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004131 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004132 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004133 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004134 }
4135
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004136 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4137
4138 if (hsotg->num_of_eps == 0) {
4139 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004140 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004141 }
4142
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004143 /* setup endpoint information */
4144
4145 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004146 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004147
4148 /* allocate EP0 request */
4149
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004150 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004151 GFP_KERNEL);
4152 if (!hsotg->ctrl_req) {
4153 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004154 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004155 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004156
4157 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004158 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4159 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004160 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004161 epnum, 1);
4162 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004163 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004164 epnum, 0);
4165 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004166
Dinh Nguyen117777b2014-11-11 11:13:34 -06004167 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004168 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004169 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004170
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004171 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004172
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004173 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004174}
4175
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004176/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004177 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004178 * @pdev: The platform information for the driver
4179 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004180int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004181{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004182 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004183
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004184 return 0;
4185}
4186
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004187int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004188{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004189 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004190
Gregory Herrero9e779772015-04-29 22:09:07 +02004191 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004192 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004193
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004194 if (hsotg->driver) {
4195 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004196
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004197 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4198 hsotg->driver->driver.name);
4199
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004200 spin_lock_irqsave(&hsotg->lock, flags);
4201 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004202 dwc2_hsotg_core_disconnect(hsotg);
4203 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004204 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4205 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004206
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004207 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4208 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004209 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004210 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004211 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004212 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004213 }
4214
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004215 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004216}
4217
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004218int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004219{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004220 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004221
Gregory Herrero9e779772015-04-29 22:09:07 +02004222 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004223 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004224
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004225 if (hsotg->driver) {
4226 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4227 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004228
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004229 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004230 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004231 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004232 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004233 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004234 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004235
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004236 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004237}
John Youn58e52ff6a2016-02-23 19:54:57 -08004238
4239/**
4240 * dwc2_backup_device_registers() - Backup controller device registers.
4241 * When suspending usb bus, registers needs to be backuped
4242 * if controller power is disabled once suspended.
4243 *
4244 * @hsotg: Programming view of the DWC_otg controller
4245 */
4246int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4247{
4248 struct dwc2_dregs_backup *dr;
4249 int i;
4250
4251 dev_dbg(hsotg->dev, "%s\n", __func__);
4252
4253 /* Backup dev regs */
4254 dr = &hsotg->dr_backup;
4255
4256 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4257 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4258 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4259 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4260 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4261
4262 for (i = 0; i < hsotg->num_of_eps; i++) {
4263 /* Backup IN EPs */
4264 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4265
4266 /* Ensure DATA PID is correctly configured */
4267 if (dr->diepctl[i] & DXEPCTL_DPID)
4268 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4269 else
4270 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4271
4272 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4273 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4274
4275 /* Backup OUT EPs */
4276 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4277
4278 /* Ensure DATA PID is correctly configured */
4279 if (dr->doepctl[i] & DXEPCTL_DPID)
4280 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4281 else
4282 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4283
4284 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4285 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4286 }
4287 dr->valid = true;
4288 return 0;
4289}
4290
4291/**
4292 * dwc2_restore_device_registers() - Restore controller device registers.
4293 * When resuming usb bus, device registers needs to be restored
4294 * if controller power were disabled.
4295 *
4296 * @hsotg: Programming view of the DWC_otg controller
4297 */
4298int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4299{
4300 struct dwc2_dregs_backup *dr;
4301 u32 dctl;
4302 int i;
4303
4304 dev_dbg(hsotg->dev, "%s\n", __func__);
4305
4306 /* Restore dev regs */
4307 dr = &hsotg->dr_backup;
4308 if (!dr->valid) {
4309 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4310 __func__);
4311 return -EINVAL;
4312 }
4313 dr->valid = false;
4314
4315 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4316 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4317 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4318 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4319 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4320
4321 for (i = 0; i < hsotg->num_of_eps; i++) {
4322 /* Restore IN EPs */
4323 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4324 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4325 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4326
4327 /* Restore OUT EPs */
4328 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4329 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4330 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4331 }
4332
4333 /* Set the Power-On Programming done bit */
4334 dctl = dwc2_readl(hsotg->regs + DCTL);
4335 dctl |= DCTL_PWRONPRGDONE;
4336 dwc2_writel(dctl, hsotg->regs + DCTL);
4337
4338 return 0;
4339}