blob: ad0cd0e38f061d8674f2b457ebc6b8fa60d453cd [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 Aharonyan540ccba2016-11-14 19:16:41 -0800738/*
739 * dwc2_gadget_fill_isoc_desc - fills next isochronous descriptor in chain.
740 * @hs_ep: The isochronous endpoint.
741 * @dma_buff: usb requests dma buffer.
742 * @len: usb request transfer length.
743 *
744 * Finds out index of first free entry either in the bottom or up half of
745 * descriptor chain depend on which is under SW control and not processed
746 * by HW. Then fills that descriptor with the data of the arrived usb request,
747 * frame info, sets Last and IOC bits increments next_desc. If filled
748 * descriptor is not the first one, removes L bit from the previous descriptor
749 * status.
750 */
751static int dwc2_gadget_fill_isoc_desc(struct dwc2_hsotg_ep *hs_ep,
752 dma_addr_t dma_buff, unsigned int len)
753{
754 struct dwc2_dma_desc *desc;
755 struct dwc2_hsotg *hsotg = hs_ep->parent;
756 u32 index;
757 u32 maxsize = 0;
758 u32 mask = 0;
759
760 maxsize = dwc2_gadget_get_desc_params(hs_ep, &mask);
761 if (len > maxsize) {
762 dev_err(hsotg->dev, "wrong len %d\n", len);
763 return -EINVAL;
764 }
765
766 /*
767 * If SW has already filled half of chain, then return and wait for
768 * the other chain to be processed by HW.
769 */
770 if (hs_ep->next_desc == MAX_DMA_DESC_NUM_GENERIC / 2)
771 return -EBUSY;
772
773 /* Increment frame number by interval for IN */
774 if (hs_ep->dir_in)
775 dwc2_gadget_incr_frame_num(hs_ep);
776
777 index = (MAX_DMA_DESC_NUM_GENERIC / 2) * hs_ep->isoc_chain_num +
778 hs_ep->next_desc;
779
780 /* Sanity check of calculated index */
781 if ((hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC) ||
782 (!hs_ep->isoc_chain_num && index > MAX_DMA_DESC_NUM_GENERIC / 2)) {
783 dev_err(hsotg->dev, "wrong index %d for iso chain\n", index);
784 return -EINVAL;
785 }
786
787 desc = &hs_ep->desc_list[index];
788
789 /* Clear L bit of previous desc if more than one entries in the chain */
790 if (hs_ep->next_desc)
791 hs_ep->desc_list[index - 1].status &= ~DEV_DMA_L;
792
793 dev_dbg(hsotg->dev, "%s: Filling ep %d, dir %s isoc desc # %d\n",
794 __func__, hs_ep->index, hs_ep->dir_in ? "in" : "out", index);
795
796 desc->status = 0;
797 desc->status |= (DEV_DMA_BUFF_STS_HBUSY << DEV_DMA_BUFF_STS_SHIFT);
798
799 desc->buf = dma_buff;
800 desc->status |= (DEV_DMA_L | DEV_DMA_IOC |
801 ((len << DEV_DMA_NBYTES_SHIFT) & mask));
802
803 if (hs_ep->dir_in) {
804 desc->status |= ((hs_ep->mc << DEV_DMA_ISOC_PID_SHIFT) &
805 DEV_DMA_ISOC_PID_MASK) |
806 ((len % hs_ep->ep.maxpacket) ?
807 DEV_DMA_SHORT : 0) |
808 ((hs_ep->target_frame <<
809 DEV_DMA_ISOC_FRNUM_SHIFT) &
810 DEV_DMA_ISOC_FRNUM_MASK);
811 }
812
813 desc->status &= ~DEV_DMA_BUFF_STS_MASK;
814 desc->status |= (DEV_DMA_BUFF_STS_HREADY << DEV_DMA_BUFF_STS_SHIFT);
815
816 /* Update index of last configured entry in the chain */
817 hs_ep->next_desc++;
818
819 return 0;
820}
821
822/*
823 * dwc2_gadget_start_isoc_ddma - start isochronous transfer in DDMA
824 * @hs_ep: The isochronous endpoint.
825 *
826 * Prepare first descriptor chain for isochronous endpoints. Afterwards
827 * write DMA address to HW and enable the endpoint.
828 *
829 * Switch between descriptor chains via isoc_chain_num to give SW opportunity
830 * to prepare second descriptor chain while first one is being processed by HW.
831 */
832static void dwc2_gadget_start_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
833{
834 struct dwc2_hsotg *hsotg = hs_ep->parent;
835 struct dwc2_hsotg_req *hs_req, *treq;
836 int index = hs_ep->index;
837 int ret;
838 u32 dma_reg;
839 u32 depctl;
840 u32 ctrl;
841
842 if (list_empty(&hs_ep->queue)) {
843 dev_dbg(hsotg->dev, "%s: No requests in queue\n", __func__);
844 return;
845 }
846
847 list_for_each_entry_safe(hs_req, treq, &hs_ep->queue, queue) {
848 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
849 hs_req->req.length);
850 if (ret) {
851 dev_dbg(hsotg->dev, "%s: desc chain full\n", __func__);
852 break;
853 }
854 }
855
856 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
857 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
858
859 /* write descriptor chain address to control register */
860 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
861
862 ctrl = dwc2_readl(hsotg->regs + depctl);
863 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
864 dwc2_writel(ctrl, hsotg->regs + depctl);
865
866 /* Switch ISOC descriptor chain number being processed by SW*/
867 hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
868 hs_ep->next_desc = 0;
869}
870
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800871/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500872 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100873 * @hsotg: The controller state.
874 * @hs_ep: The endpoint to process a request for
875 * @hs_req: The request to start.
876 * @continuing: True if we are doing more for the current request.
877 *
878 * Start the given request running by setting the endpoint registers
879 * appropriately, and writing any data to the FIFOs.
880 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500881static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
882 struct dwc2_hsotg_ep *hs_ep,
883 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100884 bool continuing)
885{
886 struct usb_request *ureq = &hs_req->req;
887 int index = hs_ep->index;
888 int dir_in = hs_ep->dir_in;
889 u32 epctrl_reg;
890 u32 epsize_reg;
891 u32 epsize;
892 u32 ctrl;
893 unsigned length;
894 unsigned packets;
895 unsigned maxreq;
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800896 unsigned int dma_reg;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100897
898 if (index != 0) {
899 if (hs_ep->req && !continuing) {
900 dev_err(hsotg->dev, "%s: active request\n", __func__);
901 WARN_ON(1);
902 return;
903 } else if (hs_ep->req != hs_req && continuing) {
904 dev_err(hsotg->dev,
905 "%s: continue different req\n", __func__);
906 WARN_ON(1);
907 return;
908 }
909 }
910
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800911 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200912 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
913 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100914
915 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300916 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100917 hs_ep->dir_in ? "in" : "out");
918
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900919 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300920 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900921
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200922 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900923 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
924 return;
925 }
926
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100927 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200928 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
929 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100930
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800931 if (!using_desc_dma(hsotg))
932 maxreq = get_ep_limit(hs_ep);
933 else
934 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
935
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100936 if (length > maxreq) {
937 int round = maxreq % hs_ep->ep.maxpacket;
938
939 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
940 __func__, length, maxreq, round);
941
942 /* round down to multiple of packets */
943 if (round)
944 maxreq -= round;
945
946 length = maxreq;
947 }
948
949 if (length)
950 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
951 else
952 packets = 1; /* send one packet if length is zero. */
953
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200954 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
955 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
956 return;
957 }
958
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100959 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200960 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700961 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200962 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700963 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100964 else
965 epsize = 0;
966
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100967 /*
968 * zero length packet should be programmed on its own and should not
969 * be counted in DIEPTSIZ.PktCnt with other packets.
970 */
971 if (dir_in && ureq->zero && !continuing) {
972 /* Test if zlp is actually required. */
973 if ((ureq->length >= hs_ep->ep.maxpacket) &&
974 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +0100975 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100976 }
977
Dinh Nguyen47a16852014-04-14 14:13:34 -0700978 epsize |= DXEPTSIZ_PKTCNT(packets);
979 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100980
981 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
982 __func__, packets, length, ureq->length, epsize, epsize_reg);
983
984 /* store the request as the current one we're doing */
985 hs_ep->req = hs_req;
986
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800987 if (using_desc_dma(hsotg)) {
988 u32 offset = 0;
989 u32 mps = hs_ep->ep.maxpacket;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100990
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -0800991 /* Adjust length: EP0 - MPS, other OUT EPs - multiple of MPS */
992 if (!dir_in) {
993 if (!index)
994 length = mps;
995 else if (length % mps)
996 length += (mps - (length % mps));
997 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100998
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200999 /*
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001000 * If more data to send, adjust DMA for EP0 out data stage.
1001 * ureq->dma stays unchanged, hence increment it by already
1002 * passed passed data count before starting new transaction.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001003 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001004 if (!index && hsotg->ep0_state == DWC2_EP0_DATA_OUT &&
1005 continuing)
1006 offset = ureq->actual;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001007
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001008 /* Fill DDMA chain entries */
1009 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, ureq->dma + offset,
1010 length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001011
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08001012 /* write descriptor chain address to control register */
1013 dwc2_writel(hs_ep->desc_list_dma, hsotg->regs + dma_reg);
1014
1015 dev_dbg(hsotg->dev, "%s: %08x pad => 0x%08x\n",
1016 __func__, (u32)hs_ep->desc_list_dma, dma_reg);
1017 } else {
1018 /* write size / packets */
1019 dwc2_writel(epsize, hsotg->regs + epsize_reg);
1020
1021 if (using_dma(hsotg) && !continuing) {
1022 /*
1023 * write DMA address to control register, buffer
1024 * already synced by dwc2_hsotg_ep_queue().
1025 */
1026
1027 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
1028
1029 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
1030 __func__, &ureq->dma, dma_reg);
1031 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001032 }
1033
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001034 if (hs_ep->isochronous && hs_ep->interval == 1) {
1035 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
1036 dwc2_gadget_incr_frame_num(hs_ep);
1037
1038 if (hs_ep->target_frame & 0x1)
1039 ctrl |= DXEPCTL_SETODDFR;
1040 else
1041 ctrl |= DXEPCTL_SETEVENFR;
1042 }
1043
Dinh Nguyen47a16852014-04-14 14:13:34 -07001044 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001045
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001046 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +02001047
1048 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001049 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -07001050 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +02001051
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001052 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001053 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001054
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001055 /*
1056 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001057 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001058 * this information.
1059 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001060 hs_ep->size_loaded = length;
1061 hs_ep->last_load = ureq->actual;
1062
1063 if (dir_in && !using_dma(hsotg)) {
1064 /* set these anyway, we may need them for non-periodic in */
1065 hs_ep->fifo_load = 0;
1066
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001067 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001068 }
1069
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001070 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001071 * Note, trying to clear the NAK here causes problems with transmit
1072 * on the S3C6400 ending up with the TXFIFO becoming full.
1073 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001074
1075 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001076 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +01001077 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001078 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001079 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001080
Dinh Nguyen47a16852014-04-14 14:13:34 -07001081 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001082 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +02001083
1084 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001085 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001086}
1087
1088/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001089 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001090 * @hsotg: The device state.
1091 * @hs_ep: The endpoint the request is on.
1092 * @req: The request being processed.
1093 *
1094 * We've been asked to queue a request, so ensure that the memory buffer
1095 * is correctly setup for DMA. If we've been passed an extant DMA address
1096 * then ensure the buffer has been synced to memory. If our buffer has no
1097 * DMA memory, then we map the memory and mark our request to allow us to
1098 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001099 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001100static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
1101 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001102 struct usb_request *req)
1103{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001104 struct dwc2_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001105 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001106
1107 /* if the length is zero, ignore the DMA data */
1108 if (hs_req->req.length == 0)
1109 return 0;
1110
Felipe Balbie58ebcd2013-01-28 14:48:36 +02001111 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
1112 if (ret)
1113 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001114
1115 return 0;
1116
1117dma_error:
1118 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
1119 __func__, req->buf, req->length);
1120
1121 return -EIO;
1122}
1123
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001124static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
1125 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001126{
1127 void *req_buf = hs_req->req.buf;
1128
1129 /* If dma is not being used or buffer is aligned */
1130 if (!using_dma(hsotg) || !((long)req_buf & 3))
1131 return 0;
1132
1133 WARN_ON(hs_req->saved_req_buf);
1134
1135 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
1136 hs_ep->ep.name, req_buf, hs_req->req.length);
1137
1138 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
1139 if (!hs_req->req.buf) {
1140 hs_req->req.buf = req_buf;
1141 dev_err(hsotg->dev,
1142 "%s: unable to allocate memory for bounce buffer\n",
1143 __func__);
1144 return -ENOMEM;
1145 }
1146
1147 /* Save actual buffer */
1148 hs_req->saved_req_buf = req_buf;
1149
1150 if (hs_ep->dir_in)
1151 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
1152 return 0;
1153}
1154
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001155static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
1156 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001157{
1158 /* If dma is not being used or buffer was aligned */
1159 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
1160 return;
1161
1162 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
1163 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
1164
1165 /* Copy data from bounce buffer on successful out transfer */
1166 if (!hs_ep->dir_in && !hs_req->req.status)
1167 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
1168 hs_req->req.actual);
1169
1170 /* Free bounce buffer */
1171 kfree(hs_req->req.buf);
1172
1173 hs_req->req.buf = hs_req->saved_req_buf;
1174 hs_req->saved_req_buf = NULL;
1175}
1176
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07001177/**
1178 * dwc2_gadget_target_frame_elapsed - Checks target frame
1179 * @hs_ep: The driver endpoint to check
1180 *
1181 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
1182 * corresponding transfer.
1183 */
1184static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
1185{
1186 struct dwc2_hsotg *hsotg = hs_ep->parent;
1187 u32 target_frame = hs_ep->target_frame;
1188 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
1189 bool frame_overrun = hs_ep->frame_overrun;
1190
1191 if (!frame_overrun && current_frame >= target_frame)
1192 return true;
1193
1194 if (frame_overrun && current_frame >= target_frame &&
1195 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
1196 return true;
1197
1198 return false;
1199}
1200
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001201/*
1202 * dwc2_gadget_set_ep0_desc_chain - Set EP's desc chain pointers
1203 * @hsotg: The driver state
1204 * @hs_ep: the ep descriptor chain is for
1205 *
1206 * Called to update EP0 structure's pointers depend on stage of
1207 * control transfer.
1208 */
1209static int dwc2_gadget_set_ep0_desc_chain(struct dwc2_hsotg *hsotg,
1210 struct dwc2_hsotg_ep *hs_ep)
1211{
1212 switch (hsotg->ep0_state) {
1213 case DWC2_EP0_SETUP:
1214 case DWC2_EP0_STATUS_OUT:
1215 hs_ep->desc_list = hsotg->setup_desc[0];
1216 hs_ep->desc_list_dma = hsotg->setup_desc_dma[0];
1217 break;
1218 case DWC2_EP0_DATA_IN:
1219 case DWC2_EP0_STATUS_IN:
1220 hs_ep->desc_list = hsotg->ctrl_in_desc;
1221 hs_ep->desc_list_dma = hsotg->ctrl_in_desc_dma;
1222 break;
1223 case DWC2_EP0_DATA_OUT:
1224 hs_ep->desc_list = hsotg->ctrl_out_desc;
1225 hs_ep->desc_list_dma = hsotg->ctrl_out_desc_dma;
1226 break;
1227 default:
1228 dev_err(hsotg->dev, "invalid EP 0 state in queue %d\n",
1229 hsotg->ep0_state);
1230 return -EINVAL;
1231 }
1232
1233 return 0;
1234}
1235
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001236static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001237 gfp_t gfp_flags)
1238{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001239 struct dwc2_hsotg_req *hs_req = our_req(req);
1240 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001241 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001242 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001243 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001244
1245 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
1246 ep->name, req, req->length, req->buf, req->no_interrupt,
1247 req->zero, req->short_not_ok);
1248
Gregory Herrero7ababa92015-04-29 22:09:08 +02001249 /* Prevent new request submission when controller is suspended */
1250 if (hs->lx_state == DWC2_L2) {
1251 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
1252 __func__);
1253 return -EAGAIN;
1254 }
1255
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001256 /* initialise status of the request */
1257 INIT_LIST_HEAD(&hs_req->queue);
1258 req->actual = 0;
1259 req->status = -EINPROGRESS;
1260
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001261 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001262 if (ret)
1263 return ret;
1264
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001265 /* if we're using DMA, sync the buffers as necessary */
1266 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001267 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001268 if (ret)
1269 return ret;
1270 }
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001271 /* If using descriptor DMA configure EP0 descriptor chain pointers */
1272 if (using_desc_dma(hs) && !hs_ep->index) {
1273 ret = dwc2_gadget_set_ep0_desc_chain(hs, hs_ep);
1274 if (ret)
1275 return ret;
1276 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001277
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001278 first = list_empty(&hs_ep->queue);
1279 list_add_tail(&hs_req->queue, &hs_ep->queue);
1280
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001281 /*
1282 * Handle DDMA isochronous transfers separately - just add new entry
1283 * to the half of descriptor chain that is not processed by HW.
1284 * Transfer will be started once SW gets either one of NAK or
1285 * OutTknEpDis interrupts.
1286 */
1287 if (using_desc_dma(hs) && hs_ep->isochronous &&
1288 hs_ep->target_frame != TARGET_FRAME_INITIAL) {
1289 ret = dwc2_gadget_fill_isoc_desc(hs_ep, hs_req->req.dma,
1290 hs_req->req.length);
1291 if (ret)
1292 dev_dbg(hs->dev, "%s: ISO desc chain full\n", __func__);
1293
1294 return 0;
1295 }
1296
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001297 if (first) {
1298 if (!hs_ep->isochronous) {
1299 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1300 return 0;
1301 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001302
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001303 while (dwc2_gadget_target_frame_elapsed(hs_ep))
1304 dwc2_gadget_incr_frame_num(hs_ep);
1305
1306 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
1307 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
1308 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001309 return 0;
1310}
1311
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001312static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001313 gfp_t gfp_flags)
1314{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001315 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001316 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001317 unsigned long flags = 0;
1318 int ret = 0;
1319
1320 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001321 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02001322 spin_unlock_irqrestore(&hs->lock, flags);
1323
1324 return ret;
1325}
1326
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001327static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001328 struct usb_request *req)
1329{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001330 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001331
1332 kfree(hs_req);
1333}
1334
1335/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001336 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001337 * @ep: The endpoint the request was on.
1338 * @req: The request completed.
1339 *
1340 * Called on completion of any requests the driver itself
1341 * submitted that need cleaning up.
1342 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001343static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001344 struct usb_request *req)
1345{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001346 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001347 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001348
1349 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1350
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001351 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001352}
1353
1354/**
1355 * ep_from_windex - convert control wIndex value to endpoint
1356 * @hsotg: The driver state.
1357 * @windex: The control request wIndex field (in host order).
1358 *
1359 * Convert the given wIndex into a pointer to an driver endpoint
1360 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001361 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001362static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001363 u32 windex)
1364{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001365 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001366 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1367 int idx = windex & 0x7F;
1368
1369 if (windex >= 0x100)
1370 return NULL;
1371
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001372 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001373 return NULL;
1374
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001375 ep = index_to_ep(hsotg, idx, dir);
1376
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001377 if (idx && ep->dir_in != dir)
1378 return NULL;
1379
1380 return ep;
1381}
1382
1383/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001384 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001385 * @hsotg: The driver state.
1386 * @testmode: requested usb test mode
1387 * Enable usb Test Mode requested by the Host.
1388 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001389int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001390{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001391 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001392
1393 dctl &= ~DCTL_TSTCTL_MASK;
1394 switch (testmode) {
1395 case TEST_J:
1396 case TEST_K:
1397 case TEST_SE0_NAK:
1398 case TEST_PACKET:
1399 case TEST_FORCE_EN:
1400 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1401 break;
1402 default:
1403 return -EINVAL;
1404 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001405 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001406 return 0;
1407}
1408
1409/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001410 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001411 * @hsotg: The device state
1412 * @ep: Endpoint 0
1413 * @buff: Buffer for request
1414 * @length: Length of reply.
1415 *
1416 * Create a request and queue it on the given endpoint. This is useful as
1417 * an internal method of sending replies to certain control requests, etc.
1418 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001419static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
1420 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001421 void *buff,
1422 int length)
1423{
1424 struct usb_request *req;
1425 int ret;
1426
1427 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1428
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001429 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001430 hsotg->ep0_reply = req;
1431 if (!req) {
1432 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1433 return -ENOMEM;
1434 }
1435
1436 req->buf = hsotg->ep0_buff;
1437 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001438 /*
1439 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1440 * STATUS stage.
1441 */
1442 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001443 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001444
1445 if (length)
1446 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001447
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001448 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001449 if (ret) {
1450 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1451 return ret;
1452 }
1453
1454 return 0;
1455}
1456
1457/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001458 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001459 * @hsotg: The device state
1460 * @ctrl: USB control request
1461 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001462static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001463 struct usb_ctrlrequest *ctrl)
1464{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001465 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1466 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001467 __le16 reply;
1468 int ret;
1469
1470 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1471
1472 if (!ep0->dir_in) {
1473 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1474 return -EINVAL;
1475 }
1476
1477 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1478 case USB_RECIP_DEVICE:
1479 reply = cpu_to_le16(0); /* bit 0 => self powered,
1480 * bit 1 => remote wakeup */
1481 break;
1482
1483 case USB_RECIP_INTERFACE:
1484 /* currently, the data result should be zero */
1485 reply = cpu_to_le16(0);
1486 break;
1487
1488 case USB_RECIP_ENDPOINT:
1489 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1490 if (!ep)
1491 return -ENOENT;
1492
1493 reply = cpu_to_le16(ep->halted ? 1 : 0);
1494 break;
1495
1496 default:
1497 return 0;
1498 }
1499
1500 if (le16_to_cpu(ctrl->wLength) != 2)
1501 return -EINVAL;
1502
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001503 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001504 if (ret) {
1505 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1506 return ret;
1507 }
1508
1509 return 1;
1510}
1511
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001512static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001513
1514/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001515 * get_ep_head - return the first request on the endpoint
1516 * @hs_ep: The controller endpoint to get
1517 *
1518 * Get the first request on the endpoint.
1519 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001520static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001521{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001522 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1523 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001524}
1525
1526/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001527 * dwc2_gadget_start_next_request - Starts next request from ep queue
1528 * @hs_ep: Endpoint structure
1529 *
1530 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1531 * in its handler. Hence we need to unmask it here to be able to do
1532 * resynchronization.
1533 */
1534static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1535{
1536 u32 mask;
1537 struct dwc2_hsotg *hsotg = hs_ep->parent;
1538 int dir_in = hs_ep->dir_in;
1539 struct dwc2_hsotg_req *hs_req;
1540 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1541
1542 if (!list_empty(&hs_ep->queue)) {
1543 hs_req = get_ep_head(hs_ep);
1544 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1545 return;
1546 }
1547 if (!hs_ep->isochronous)
1548 return;
1549
1550 if (dir_in) {
1551 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1552 __func__);
1553 } else {
1554 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1555 __func__);
1556 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1557 mask |= DOEPMSK_OUTTKNEPDISMSK;
1558 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1559 }
1560}
1561
1562/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001563 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001564 * @hsotg: The device state
1565 * @ctrl: USB control request
1566 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001567static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001568 struct usb_ctrlrequest *ctrl)
1569{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001570 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1571 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001572 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001573 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001574 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001575 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001576 u32 recip;
1577 u32 wValue;
1578 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001579
1580 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1581 __func__, set ? "SET" : "CLEAR");
1582
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001583 wValue = le16_to_cpu(ctrl->wValue);
1584 wIndex = le16_to_cpu(ctrl->wIndex);
1585 recip = ctrl->bRequestType & USB_RECIP_MASK;
1586
1587 switch (recip) {
1588 case USB_RECIP_DEVICE:
1589 switch (wValue) {
1590 case USB_DEVICE_TEST_MODE:
1591 if ((wIndex & 0xff) != 0)
1592 return -EINVAL;
1593 if (!set)
1594 return -EINVAL;
1595
1596 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001597 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001598 if (ret) {
1599 dev_err(hsotg->dev,
1600 "%s: failed to send reply\n", __func__);
1601 return ret;
1602 }
1603 break;
1604 default:
1605 return -ENOENT;
1606 }
1607 break;
1608
1609 case USB_RECIP_ENDPOINT:
1610 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001611 if (!ep) {
1612 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001613 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001614 return -ENOENT;
1615 }
1616
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001617 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001618 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001619 halted = ep->halted;
1620
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001621 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001622
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001623 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001624 if (ret) {
1625 dev_err(hsotg->dev,
1626 "%s: failed to send reply\n", __func__);
1627 return ret;
1628 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001629
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001630 /*
1631 * we have to complete all requests for ep if it was
1632 * halted, and the halt was cleared by CLEAR_FEATURE
1633 */
1634
1635 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001636 /*
1637 * If we have request in progress,
1638 * then complete it
1639 */
1640 if (ep->req) {
1641 hs_req = ep->req;
1642 ep->req = NULL;
1643 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001644 if (hs_req->req.complete) {
1645 spin_unlock(&hsotg->lock);
1646 usb_gadget_giveback_request(
1647 &ep->ep, &hs_req->req);
1648 spin_lock(&hsotg->lock);
1649 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001650 }
1651
1652 /* If we have pending request, then start it */
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001653 if (!ep->req) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001654 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001655 }
1656 }
1657
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001658 break;
1659
1660 default:
1661 return -ENOENT;
1662 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001663 break;
1664 default:
1665 return -ENOENT;
1666 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001667 return 1;
1668}
1669
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001670static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001671
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001672/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001673 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001674 * @hsotg: The device state
1675 *
1676 * Set stall for ep0 as response for setup request.
1677 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001678static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001679{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001680 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001681 u32 reg;
1682 u32 ctrl;
1683
1684 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1685 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1686
1687 /*
1688 * DxEPCTL_Stall will be cleared by EP once it has
1689 * taken effect, so no need to clear later.
1690 */
1691
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001692 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001693 ctrl |= DXEPCTL_STALL;
1694 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001695 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001696
1697 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001698 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001699 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001700
1701 /*
1702 * complete won't be called, so we enqueue
1703 * setup request here
1704 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001705 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001706}
1707
1708/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001709 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001710 * @hsotg: The device state
1711 * @ctrl: The control request received
1712 *
1713 * The controller has received the SETUP phase of a control request, and
1714 * needs to work out what to do next (and whether to pass it on to the
1715 * gadget driver).
1716 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001717static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001718 struct usb_ctrlrequest *ctrl)
1719{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001720 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001721 int ret = 0;
1722 u32 dcfg;
1723
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001724 dev_dbg(hsotg->dev,
1725 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1726 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1727 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001728
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001729 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001730 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001731 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1732 } else if (ctrl->bRequestType & USB_DIR_IN) {
1733 ep0->dir_in = 1;
1734 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1735 } else {
1736 ep0->dir_in = 0;
1737 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1738 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001739
1740 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1741 switch (ctrl->bRequest) {
1742 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001743 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001744 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001745 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001746 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1747 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001748 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001749
1750 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1751
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001752 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001753 return;
1754
1755 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001756 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001757 break;
1758
1759 case USB_REQ_CLEAR_FEATURE:
1760 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001761 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001762 break;
1763 }
1764 }
1765
1766 /* as a fallback, try delivering it to the driver to deal with */
1767
1768 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001769 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001770 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001771 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001772 if (ret < 0)
1773 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1774 }
1775
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001776 /*
1777 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001778 * so respond with a STALL for the status stage to indicate failure.
1779 */
1780
Robert Baldygac9f721b2014-01-14 08:36:00 +01001781 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001782 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001783}
1784
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001785/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001786 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001787 * @ep: The endpoint the request was on.
1788 * @req: The request completed.
1789 *
1790 * Called on completion of any requests the driver itself submitted for
1791 * EP0 setup packets
1792 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001793static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001794 struct usb_request *req)
1795{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001796 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001797 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001798
1799 if (req->status < 0) {
1800 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1801 return;
1802 }
1803
Robert Baldyga93f599f2013-11-21 13:49:17 +01001804 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001805 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001806 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001807 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001808 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001809 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001810}
1811
1812/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001813 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001814 * @hsotg: The device state.
1815 *
1816 * Enqueue a request on EP0 if necessary to received any SETUP packets
1817 * received from the host.
1818 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001819static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001820{
1821 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001822 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001823 int ret;
1824
1825 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1826
1827 req->zero = 0;
1828 req->length = 8;
1829 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001830 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001831
1832 if (!list_empty(&hs_req->queue)) {
1833 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1834 return;
1835 }
1836
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001837 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001838 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001839 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001840
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001841 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001842 if (ret < 0) {
1843 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001844 /*
1845 * Don't think there's much we can do other than watch the
1846 * driver fail.
1847 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001848 }
1849}
1850
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001851static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1852 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001853{
1854 u32 ctrl;
1855 u8 index = hs_ep->index;
1856 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1857 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1858
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001859 if (hs_ep->dir_in)
1860 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001861 index);
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001862 else
1863 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001864 index);
1865 if (using_desc_dma(hsotg)) {
1866 /* Not specific buffer needed for ep0 ZLP */
1867 dma_addr_t dma = hs_ep->desc_list_dma;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001868
Vahram Aharonyane02f9aa2016-11-14 19:16:24 -08001869 dwc2_gadget_set_ep0_desc_chain(hsotg, hs_ep);
1870 dwc2_gadget_config_nonisoc_xfer_ddma(hs_ep, dma, 0);
1871 } else {
1872 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1873 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1874 epsiz_reg);
1875 }
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001876
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001877 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001878 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1879 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1880 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001881 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001882}
1883
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001884/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001885 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001886 * @hsotg: The device state.
1887 * @hs_ep: The endpoint the request was on.
1888 * @hs_req: The request to complete.
1889 * @result: The result code (0 => Ok, otherwise errno)
1890 *
1891 * The given request has finished, so call the necessary completion
1892 * if it has one and then look to see if we can start a new request
1893 * on the endpoint.
1894 *
1895 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001896 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001897static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1898 struct dwc2_hsotg_ep *hs_ep,
1899 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001900 int result)
1901{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001902
1903 if (!hs_req) {
1904 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1905 return;
1906 }
1907
1908 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1909 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1910
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001911 /*
1912 * only replace the status if we've not already set an error
1913 * from a previous transaction
1914 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001915
1916 if (hs_req->req.status == -EINPROGRESS)
1917 hs_req->req.status = result;
1918
Yunzhi Li44583fe2015-09-29 12:25:01 +02001919 if (using_dma(hsotg))
1920 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1921
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001922 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001923
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001924 hs_ep->req = NULL;
1925 list_del_init(&hs_req->queue);
1926
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001927 /*
1928 * call the complete request with the locks off, just in case the
1929 * request tries to queue more work for this endpoint.
1930 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001931
1932 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001933 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001934 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001935 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001936 }
1937
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001938 /* In DDMA don't need to proceed to starting of next ISOC request */
1939 if (using_desc_dma(hsotg) && hs_ep->isochronous)
1940 return;
1941
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001942 /*
1943 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001944 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001945 * so be careful when doing this.
1946 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001947
1948 if (!hs_ep->req && result >= 0) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001949 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950 }
1951}
1952
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08001953/*
1954 * dwc2_gadget_complete_isoc_request_ddma - complete an isoc request in DDMA
1955 * @hs_ep: The endpoint the request was on.
1956 *
1957 * Get first request from the ep queue, determine descriptor on which complete
1958 * happened. SW based on isoc_chain_num discovers which half of the descriptor
1959 * chain is currently in use by HW, adjusts dma_address and calculates index
1960 * of completed descriptor based on the value of DEPDMA register. Update actual
1961 * length of request, giveback to gadget.
1962 */
1963static void dwc2_gadget_complete_isoc_request_ddma(struct dwc2_hsotg_ep *hs_ep)
1964{
1965 struct dwc2_hsotg *hsotg = hs_ep->parent;
1966 struct dwc2_hsotg_req *hs_req;
1967 struct usb_request *ureq;
1968 int index;
1969 dma_addr_t dma_addr;
1970 u32 dma_reg;
1971 u32 depdma;
1972 u32 desc_sts;
1973 u32 mask;
1974
1975 hs_req = get_ep_head(hs_ep);
1976 if (!hs_req) {
1977 dev_warn(hsotg->dev, "%s: ISOC EP queue empty\n", __func__);
1978 return;
1979 }
1980 ureq = &hs_req->req;
1981
1982 dma_addr = hs_ep->desc_list_dma;
1983
1984 /*
1985 * If lower half of descriptor chain is currently use by SW,
1986 * that means higher half is being processed by HW, so shift
1987 * DMA address to higher half of descriptor chain.
1988 */
1989 if (!hs_ep->isoc_chain_num)
1990 dma_addr += sizeof(struct dwc2_dma_desc) *
1991 (MAX_DMA_DESC_NUM_GENERIC / 2);
1992
1993 dma_reg = hs_ep->dir_in ? DIEPDMA(hs_ep->index) : DOEPDMA(hs_ep->index);
1994 depdma = dwc2_readl(hsotg->regs + dma_reg);
1995
1996 index = (depdma - dma_addr) / sizeof(struct dwc2_dma_desc) - 1;
1997 desc_sts = hs_ep->desc_list[index].status;
1998
1999 mask = hs_ep->dir_in ? DEV_DMA_ISOC_TX_NBYTES_MASK :
2000 DEV_DMA_ISOC_RX_NBYTES_MASK;
2001 ureq->actual = ureq->length -
2002 ((desc_sts & mask) >> DEV_DMA_ISOC_NBYTES_SHIFT);
2003
Vahram Aharonyan95d2b032016-11-14 19:16:46 -08002004 /* Adjust actual length for ISOC Out if length is not align of 4 */
2005 if (!hs_ep->dir_in && ureq->length & 0x3)
2006 ureq->actual += 4 - (ureq->length & 0x3);
2007
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002008 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2009}
2010
2011/*
2012 * dwc2_gadget_start_next_isoc_ddma - start next isoc request, if any.
2013 * @hs_ep: The isochronous endpoint to be re-enabled.
2014 *
2015 * If ep has been disabled due to last descriptor servicing (IN endpoint) or
2016 * BNA (OUT endpoint) check the status of other half of descriptor chain that
2017 * was under SW control till HW was busy and restart the endpoint if needed.
2018 */
2019static void dwc2_gadget_start_next_isoc_ddma(struct dwc2_hsotg_ep *hs_ep)
2020{
2021 struct dwc2_hsotg *hsotg = hs_ep->parent;
2022 u32 depctl;
2023 u32 dma_reg;
2024 u32 ctrl;
2025 u32 dma_addr = hs_ep->desc_list_dma;
2026 unsigned char index = hs_ep->index;
2027
2028 dma_reg = hs_ep->dir_in ? DIEPDMA(index) : DOEPDMA(index);
2029 depctl = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
2030
2031 ctrl = dwc2_readl(hsotg->regs + depctl);
2032
2033 /*
2034 * EP was disabled if HW has processed last descriptor or BNA was set.
2035 * So restart ep if SW has prepared new descriptor chain in ep_queue
2036 * routine while HW was busy.
2037 */
2038 if (!(ctrl & DXEPCTL_EPENA)) {
2039 if (!hs_ep->next_desc) {
2040 dev_dbg(hsotg->dev, "%s: No more ISOC requests\n",
2041 __func__);
2042 return;
2043 }
2044
2045 dma_addr += sizeof(struct dwc2_dma_desc) *
2046 (MAX_DMA_DESC_NUM_GENERIC / 2) *
2047 hs_ep->isoc_chain_num;
2048 dwc2_writel(dma_addr, hsotg->regs + dma_reg);
2049
2050 ctrl |= DXEPCTL_EPENA | DXEPCTL_CNAK;
2051 dwc2_writel(ctrl, hsotg->regs + depctl);
2052
2053 /* Switch ISOC descriptor chain number being processed by SW*/
2054 hs_ep->isoc_chain_num = (hs_ep->isoc_chain_num ^ 1) & 0x1;
2055 hs_ep->next_desc = 0;
2056
2057 dev_dbg(hsotg->dev, "%s: Restarted isochronous endpoint\n",
2058 __func__);
2059 }
2060}
2061
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002062/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002063 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002064 * @hsotg: The device state.
2065 * @ep_idx: The endpoint index for the data
2066 * @size: The size of data in the fifo, in bytes
2067 *
2068 * The FIFO status shows there is data to read from the FIFO for a given
2069 * endpoint, so sort out whether we need to read the data into a request
2070 * that has been made for that endpoint.
2071 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002072static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002073{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002074 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
2075 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002076 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002077 int to_read;
2078 int max_req;
2079 int read_ptr;
2080
Lukasz Majewski22258f42012-06-14 10:02:24 +02002081
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002082 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002083 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002084 int ptr;
2085
Robert Baldyga6b448af2014-12-16 11:51:44 +01002086 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002087 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002088 __func__, size, ep_idx, epctl);
2089
2090 /* dump the data from the FIFO, we've nothing we can do */
2091 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002092 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002093
2094 return;
2095 }
2096
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002097 to_read = size;
2098 read_ptr = hs_req->req.actual;
2099 max_req = hs_req->req.length - read_ptr;
2100
Ben Dooksa33e7132010-07-19 09:40:49 +01002101 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
2102 __func__, to_read, max_req, read_ptr, hs_req->req.length);
2103
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002104 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002105 /*
2106 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002107 * to deal with in this request.
2108 */
2109
2110 /* currently we don't deal this */
2111 WARN_ON_ONCE(1);
2112 }
2113
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002114 hs_ep->total_data += to_read;
2115 hs_req->req.actual += to_read;
2116 to_read = DIV_ROUND_UP(to_read, 4);
2117
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002118 /*
2119 * note, we might over-write the buffer end by 3 bytes depending on
2120 * alignment of the data.
2121 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05002122 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002123}
2124
2125/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002126 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002127 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002128 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002129 *
2130 * Generate a zero-length IN packet request for terminating a SETUP
2131 * transaction.
2132 *
2133 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002134 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002135 * the TxFIFO.
2136 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002137static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002138{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002139 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002140 hsotg->eps_out[0]->dir_in = dir_in;
2141 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002142
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002143 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002144}
2145
Roman Bacikec1f9d92015-09-10 18:13:43 -07002146static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
2147 u32 epctl_reg)
2148{
2149 u32 ctrl;
2150
2151 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
2152 if (ctrl & DXEPCTL_EOFRNUM)
2153 ctrl |= DXEPCTL_SETEVENFR;
2154 else
2155 ctrl |= DXEPCTL_SETODDFR;
2156 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
2157}
2158
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002159/*
2160 * dwc2_gadget_get_xfersize_ddma - get transferred bytes amount from desc
2161 * @hs_ep - The endpoint on which transfer went
2162 *
2163 * Iterate over endpoints descriptor chain and get info on bytes remained
2164 * in DMA descriptors after transfer has completed. Used for non isoc EPs.
2165 */
2166static unsigned int dwc2_gadget_get_xfersize_ddma(struct dwc2_hsotg_ep *hs_ep)
2167{
2168 struct dwc2_hsotg *hsotg = hs_ep->parent;
2169 unsigned int bytes_rem = 0;
2170 struct dwc2_dma_desc *desc = hs_ep->desc_list;
2171 int i;
2172 u32 status;
2173
2174 if (!desc)
2175 return -EINVAL;
2176
2177 for (i = 0; i < hs_ep->desc_count; ++i) {
2178 status = desc->status;
2179 bytes_rem += status & DEV_DMA_NBYTES_MASK;
2180
2181 if (status & DEV_DMA_STS_MASK)
2182 dev_err(hsotg->dev, "descriptor %d closed with %x\n",
2183 i, status & DEV_DMA_STS_MASK);
2184 }
2185
2186 return bytes_rem;
2187}
2188
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002189/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002190 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002191 * @hsotg: The device instance
2192 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002193 *
2194 * The RXFIFO has delivered an OutDone event, which means that the data
2195 * transfer for an OUT endpoint has been completed, either by a short
2196 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002197 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002198static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002199{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002200 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002201 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
2202 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002203 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002204 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002205 int result = 0;
2206
2207 if (!hs_req) {
2208 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
2209 return;
2210 }
2211
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002212 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
2213 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002214 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
2215 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002216 return;
2217 }
2218
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002219 if (using_desc_dma(hsotg))
2220 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2221
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002222 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002223 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002224
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002225 /*
2226 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002227 * is left in the endpoint size register and then working it
2228 * out from the amount we loaded for the transfer.
2229 *
2230 * We need to do this as DMA pointers are always 32bit aligned
2231 * so may overshoot/undershoot the transfer.
2232 */
2233
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002234 size_done = hs_ep->size_loaded - size_left;
2235 size_done += hs_ep->last_load;
2236
2237 req->actual = size_done;
2238 }
2239
Ben Dooksa33e7132010-07-19 09:40:49 +01002240 /* if there is more request to do, schedule new transfer */
2241 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002242 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01002243 return;
2244 }
2245
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002246 if (req->actual < req->length && req->short_not_ok) {
2247 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
2248 __func__, req->actual, req->length);
2249
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002250 /*
2251 * todo - what should we return here? there's no one else
2252 * even bothering to check the status.
2253 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002254 }
2255
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002256 /* DDMA IN status phase will start from StsPhseRcvd interrupt */
2257 if (!using_desc_dma(hsotg) && epnum == 0 &&
2258 hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002259 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002260 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002261 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002262 }
2263
Roman Bacikec1f9d92015-09-10 18:13:43 -07002264 /*
2265 * Slave mode OUT transfers do not go through XferComplete so
2266 * adjust the ISOC parity here.
2267 */
2268 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07002269 if (hs_ep->isochronous && hs_ep->interval == 1)
2270 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002271 else if (hs_ep->isochronous && hs_ep->interval > 1)
2272 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002273 }
2274
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002275 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002276}
2277
2278/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002279 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002280 * @hsotg: The device instance
2281 *
2282 * The IRQ handler has detected that the RX FIFO has some data in it
2283 * that requires processing, so find out what is in there and do the
2284 * appropriate read.
2285 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03002286 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002287 * chunks, so if you have x packets received on an endpoint you'll get x
2288 * FIFO events delivered, each with a packet's worth of data in it.
2289 *
2290 * When using DMA, we should not be processing events from the RXFIFO
2291 * as the actual data should be sent to the memory directly and we turn
2292 * on the completion interrupts to get notifications of transfer completion.
2293 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002294static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002295{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002296 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002297 u32 epnum, status, size;
2298
2299 WARN_ON(using_dma(hsotg));
2300
Dinh Nguyen47a16852014-04-14 14:13:34 -07002301 epnum = grxstsr & GRXSTS_EPNUM_MASK;
2302 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002303
Dinh Nguyen47a16852014-04-14 14:13:34 -07002304 size = grxstsr & GRXSTS_BYTECNT_MASK;
2305 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002306
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01002307 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002308 __func__, grxstsr, size, epnum);
2309
Dinh Nguyen47a16852014-04-14 14:13:34 -07002310 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
2311 case GRXSTS_PKTSTS_GLOBALOUTNAK:
2312 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002313 break;
2314
Dinh Nguyen47a16852014-04-14 14:13:34 -07002315 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002316 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002317 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002318
2319 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002320 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002321 break;
2322
Dinh Nguyen47a16852014-04-14 14:13:34 -07002323 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002324 dev_dbg(hsotg->dev,
2325 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002326 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002327 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002328 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002329 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002330 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
2331 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
2332 */
2333 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002334 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002335 break;
2336
Dinh Nguyen47a16852014-04-14 14:13:34 -07002337 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002338 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002339 break;
2340
Dinh Nguyen47a16852014-04-14 14:13:34 -07002341 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002342 dev_dbg(hsotg->dev,
2343 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002344 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002345 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002346
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002347 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
2348
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002349 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002350 break;
2351
2352 default:
2353 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
2354 __func__, grxstsr);
2355
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002356 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002357 break;
2358 }
2359}
2360
2361/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002362 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002363 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002364 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002365static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366{
2367 switch (mps) {
2368 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002369 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002370 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002371 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002372 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002373 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002374 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002375 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002376 }
2377
2378 /* bad max packet size, warn and return invalid result */
2379 WARN_ON(1);
2380 return (u32)-1;
2381}
2382
2383/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002384 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002385 * @hsotg: The driver state.
2386 * @ep: The index number of the endpoint
2387 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002388 * @mc: The multicount value
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002389 *
2390 * Configure the maximum packet size for the given endpoint, updating
2391 * the hardware control registers to reflect this.
2392 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002393static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002394 unsigned int ep, unsigned int mps,
2395 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002396{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002397 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002398 void __iomem *regs = hsotg->regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002399 u32 reg;
2400
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002401 hs_ep = index_to_ep(hsotg, ep, dir_in);
2402 if (!hs_ep)
2403 return;
2404
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002405 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002406 u32 mps_bytes = mps;
2407
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002408 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002409 mps = dwc2_hsotg_ep0_mps(mps_bytes);
2410 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002411 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002412 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002413 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002414 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002415 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002416 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002417 hs_ep->mc = mc;
2418 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02002419 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002420 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002421 }
2422
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002423 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002424 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002425 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002426 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002427 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002428 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002429 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07002430 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002431 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002432 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09002433 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002434
2435 return;
2436
2437bad_mps:
2438 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
2439}
2440
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002441/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002442 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002443 * @hsotg: The driver state
2444 * @idx: The index for the endpoint (0..15)
2445 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002446static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002447{
2448 int timeout;
2449 int val;
2450
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002451 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
2452 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002453
2454 /* wait until the fifo is flushed */
2455 timeout = 100;
2456
2457 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002458 val = dwc2_readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002459
Dinh Nguyen47a16852014-04-14 14:13:34 -07002460 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002461 break;
2462
2463 if (--timeout == 0) {
2464 dev_err(hsotg->dev,
2465 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
2466 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02002467 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002468 }
2469
2470 udelay(1);
2471 }
2472}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002473
2474/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002475 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002476 * @hsotg: The driver state
2477 * @hs_ep: The driver endpoint to check.
2478 *
2479 * Check to see if there is a request that has data to send, and if so
2480 * make an attempt to write data into the FIFO.
2481 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002482static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
2483 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002484{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002485 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002486
Robert Baldygaafcf4162013-09-19 11:50:19 +02002487 if (!hs_ep->dir_in || !hs_req) {
2488 /**
2489 * if request is not enqueued, we disable interrupts
2490 * for endpoints, excepting ep0
2491 */
2492 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002493 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
Robert Baldygaafcf4162013-09-19 11:50:19 +02002494 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002495 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002496 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002497
2498 if (hs_req->req.actual < hs_req->req.length) {
2499 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2500 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002501 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002502 }
2503
2504 return 0;
2505}
2506
2507/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002508 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002509 * @hsotg: The device state.
2510 * @hs_ep: The endpoint that has just completed.
2511 *
2512 * An IN transfer has been completed, update the transfer's state and then
2513 * call the relevant completion routines.
2514 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002515static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
2516 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002517{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002518 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002519 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520 int size_left, size_done;
2521
2522 if (!hs_req) {
2523 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2524 return;
2525 }
2526
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002527 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002528 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2529 dev_dbg(hsotg->dev, "zlp packet sent\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002530 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002531 if (hsotg->test_mode) {
2532 int ret;
2533
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002534 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002535 if (ret < 0) {
2536 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
2537 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002538 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002539 return;
2540 }
2541 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002542 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002543 return;
2544 }
2545
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002546 /*
2547 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002548 * in the endpoint size register and then working it out from
2549 * the amount we loaded for the transfer.
2550 *
2551 * We do this even for DMA, as the transfer may have incremented
2552 * past the end of the buffer (DMA transfers are always 32bit
2553 * aligned).
2554 */
Vahram Aharonyanaa3e8bc2016-11-14 19:16:26 -08002555 if (using_desc_dma(hsotg)) {
2556 size_left = dwc2_gadget_get_xfersize_ddma(hs_ep);
2557 if (size_left < 0)
2558 dev_err(hsotg->dev, "error parsing DDMA results %d\n",
2559 size_left);
2560 } else {
2561 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
2562 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002563
2564 size_done = hs_ep->size_loaded - size_left;
2565 size_done += hs_ep->last_load;
2566
2567 if (hs_req->req.actual != size_done)
2568 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2569 __func__, hs_req->req.actual, size_done);
2570
2571 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002572 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2573 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002574
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002575 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2576 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002577 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002578 return;
2579 }
2580
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002581 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002582 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002583 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002584 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002585 /* transfer will be completed on next complete interrupt */
2586 return;
2587 }
2588
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002589 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2590 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002591 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002592 return;
2593 }
2594
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002595 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002596}
2597
2598/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002599 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2600 * @hsotg: The device state.
2601 * @idx: Index of ep.
2602 * @dir_in: Endpoint direction 1-in 0-out.
2603 *
2604 * Reads for endpoint with given index and direction, by masking
2605 * epint_reg with coresponding mask.
2606 */
2607static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2608 unsigned int idx, int dir_in)
2609{
2610 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2611 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2612 u32 ints;
2613 u32 mask;
2614 u32 diepempmsk;
2615
2616 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2617 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2618 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2619 mask |= DXEPINT_SETUP_RCVD;
2620
2621 ints = dwc2_readl(hsotg->regs + epint_reg);
2622 ints &= mask;
2623 return ints;
2624}
2625
2626/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002627 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2628 * @hs_ep: The endpoint on which interrupt is asserted.
2629 *
2630 * This interrupt indicates that the endpoint has been disabled per the
2631 * application's request.
2632 *
2633 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2634 * in case of ISOC completes current request.
2635 *
2636 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2637 * request starts it.
2638 */
2639static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2640{
2641 struct dwc2_hsotg *hsotg = hs_ep->parent;
2642 struct dwc2_hsotg_req *hs_req;
2643 unsigned char idx = hs_ep->index;
2644 int dir_in = hs_ep->dir_in;
2645 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2646 int dctl = dwc2_readl(hsotg->regs + DCTL);
2647
2648 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2649
2650 if (dir_in) {
2651 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2652
2653 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2654
2655 if (hs_ep->isochronous) {
2656 dwc2_hsotg_complete_in(hsotg, hs_ep);
2657 return;
2658 }
2659
2660 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2661 int dctl = dwc2_readl(hsotg->regs + DCTL);
2662
2663 dctl |= DCTL_CGNPINNAK;
2664 dwc2_writel(dctl, hsotg->regs + DCTL);
2665 }
2666 return;
2667 }
2668
2669 if (dctl & DCTL_GOUTNAKSTS) {
2670 dctl |= DCTL_CGOUTNAK;
2671 dwc2_writel(dctl, hsotg->regs + DCTL);
2672 }
2673
2674 if (!hs_ep->isochronous)
2675 return;
2676
2677 if (list_empty(&hs_ep->queue)) {
2678 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2679 __func__, hs_ep);
2680 return;
2681 }
2682
2683 do {
2684 hs_req = get_ep_head(hs_ep);
2685 if (hs_req)
2686 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2687 -ENODATA);
2688 dwc2_gadget_incr_frame_num(hs_ep);
2689 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2690
2691 dwc2_gadget_start_next_request(hs_ep);
2692}
2693
2694/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002695 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2696 * @hs_ep: The endpoint on which interrupt is asserted.
2697 *
2698 * This is starting point for ISOC-OUT transfer, synchronization done with
2699 * first out token received from host while corresponding EP is disabled.
2700 *
2701 * Device does not know initial frame in which out token will come. For this
2702 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2703 * getting this interrupt SW starts calculation for next transfer frame.
2704 */
2705static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2706{
2707 struct dwc2_hsotg *hsotg = ep->parent;
2708 int dir_in = ep->dir_in;
2709 u32 doepmsk;
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002710 u32 tmp;
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002711
2712 if (dir_in || !ep->isochronous)
2713 return;
2714
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002715 /*
2716 * Store frame in which irq was asserted here, as
2717 * it can change while completing request below.
2718 */
2719 tmp = dwc2_hsotg_read_frameno(hsotg);
2720
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002721 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
2722
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002723 if (using_desc_dma(hsotg)) {
2724 if (ep->target_frame == TARGET_FRAME_INITIAL) {
2725 /* Start first ISO Out */
2726 ep->target_frame = tmp;
2727 dwc2_gadget_start_isoc_ddma(ep);
2728 }
2729 return;
2730 }
2731
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002732 if (ep->interval > 1 &&
2733 ep->target_frame == TARGET_FRAME_INITIAL) {
2734 u32 dsts;
2735 u32 ctrl;
2736
2737 dsts = dwc2_readl(hsotg->regs + DSTS);
2738 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2739 dwc2_gadget_incr_frame_num(ep);
2740
2741 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2742 if (ep->target_frame & 0x1)
2743 ctrl |= DXEPCTL_SETODDFR;
2744 else
2745 ctrl |= DXEPCTL_SETEVENFR;
2746
2747 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2748 }
2749
2750 dwc2_gadget_start_next_request(ep);
2751 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2752 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2753 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2754}
2755
2756/**
2757* dwc2_gadget_handle_nak - handle NAK interrupt
2758* @hs_ep: The endpoint on which interrupt is asserted.
2759*
2760* This is starting point for ISOC-IN transfer, synchronization done with
2761* first IN token received from host while corresponding EP is disabled.
2762*
2763* Device does not know when first one token will arrive from host. On first
2764* token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2765* and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2766* sent in response to that as there was no data in FIFO. SW is basing on this
2767* interrupt to obtain frame in which token has come and then based on the
2768* interval calculates next frame for transfer.
2769*/
2770static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2771{
2772 struct dwc2_hsotg *hsotg = hs_ep->parent;
2773 int dir_in = hs_ep->dir_in;
2774
2775 if (!dir_in || !hs_ep->isochronous)
2776 return;
2777
2778 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
2779 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002780
2781 if (using_desc_dma(hsotg)) {
2782 dwc2_gadget_start_isoc_ddma(hs_ep);
2783 return;
2784 }
2785
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002786 if (hs_ep->interval > 1) {
2787 u32 ctrl = dwc2_readl(hsotg->regs +
2788 DIEPCTL(hs_ep->index));
2789 if (hs_ep->target_frame & 0x1)
2790 ctrl |= DXEPCTL_SETODDFR;
2791 else
2792 ctrl |= DXEPCTL_SETEVENFR;
2793
2794 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2795 }
2796
2797 dwc2_hsotg_complete_request(hsotg, hs_ep,
2798 get_ep_head(hs_ep), 0);
2799 }
2800
2801 dwc2_gadget_incr_frame_num(hs_ep);
2802}
2803
2804/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002805 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002806 * @hsotg: The driver state
2807 * @idx: The index for the endpoint (0..15)
2808 * @dir_in: Set if this is an IN endpoint
2809 *
2810 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002811 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002812static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002813 int dir_in)
2814{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002815 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002816 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2817 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2818 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002819 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002820 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002821
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002822 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002823 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002824
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002825 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002826 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002827
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002828 if (!hs_ep) {
2829 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
2830 __func__, idx, dir_in ? "in" : "out");
2831 return;
2832 }
2833
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002834 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2835 __func__, idx, dir_in ? "in" : "out", ints);
2836
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002837 /* Don't process XferCompl interrupt if it is a setup packet */
2838 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2839 ints &= ~DXEPINT_XFERCOMPL;
2840
Vahram Aharonyanf0afdb42016-11-14 19:16:48 -08002841 /*
2842 * Don't process XferCompl interrupt in DDMA if EP0 is still in SETUP
2843 * stage and xfercomplete was generated without SETUP phase done
2844 * interrupt. SW should parse received setup packet only after host's
2845 * exit from setup phase of control transfer.
2846 */
2847 if (using_desc_dma(hsotg) && idx == 0 && !hs_ep->dir_in &&
2848 hsotg->ep0_state == DWC2_EP0_SETUP && !(ints & DXEPINT_SETUP))
2849 ints &= ~DXEPINT_XFERCOMPL;
2850
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002851 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002852 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002853 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002854 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2855 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002856
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002857 /* In DDMA handle isochronous requests separately */
2858 if (using_desc_dma(hsotg) && hs_ep->isochronous) {
2859 dwc2_gadget_complete_isoc_request_ddma(hs_ep);
2860 /* Try to start next isoc request */
2861 dwc2_gadget_start_next_isoc_ddma(hs_ep);
2862 } else if (dir_in) {
2863 /*
2864 * We get OutDone from the FIFO, so we only
2865 * need to look at completing IN requests here
2866 * if operating slave mode
2867 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002868 if (hs_ep->isochronous && hs_ep->interval > 1)
2869 dwc2_gadget_incr_frame_num(hs_ep);
2870
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002871 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002872 if (ints & DXEPINT_NAKINTRPT)
2873 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002874
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002875 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002876 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002877 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002878 /*
2879 * We're using DMA, we need to fire an OutDone here
2880 * as we ignore the RXFIFO.
2881 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002882 if (hs_ep->isochronous && hs_ep->interval > 1)
2883 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002884
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002885 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002886 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002887 }
2888
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002889 if (ints & DXEPINT_EPDISBLD)
2890 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002891
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002892 if (ints & DXEPINT_OUTTKNEPDIS)
2893 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2894
2895 if (ints & DXEPINT_NAKINTRPT)
2896 dwc2_gadget_handle_nak(hs_ep);
2897
Dinh Nguyen47a16852014-04-14 14:13:34 -07002898 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002899 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002900
Dinh Nguyen47a16852014-04-14 14:13:34 -07002901 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002902 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2903
2904 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002905 /*
2906 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002907 * setup packet. In non-DMA mode we'd get this
2908 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002909 * the setup here.
2910 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002911
2912 if (dir_in)
2913 WARN_ON_ONCE(1);
2914 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002915 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002916 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002917 }
2918
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002919 if (ints & DXEPINT_STSPHSERCVD) {
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08002920 dev_dbg(hsotg->dev, "%s: StsPhseRcvd\n", __func__);
2921
Vahram Aharonyanef750c72016-11-14 19:16:31 -08002922 /* Move to STATUS IN for DDMA */
2923 if (using_desc_dma(hsotg))
2924 dwc2_hsotg_ep0_zlp(hsotg, true);
2925 }
2926
Dinh Nguyen47a16852014-04-14 14:13:34 -07002927 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002928 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002929
Vahram Aharonyan540ccba2016-11-14 19:16:41 -08002930 if (ints & DXEPINT_BNAINTR) {
2931 dev_dbg(hsotg->dev, "%s: BNA interrupt\n", __func__);
2932
2933 /*
2934 * Try to start next isoc request, if any.
2935 * Sometimes the endpoint remains enabled after BNA interrupt
2936 * assertion, which is not expected, hence we can enter here
2937 * couple of times.
2938 */
2939 if (hs_ep->isochronous)
2940 dwc2_gadget_start_next_isoc_ddma(hs_ep);
2941 }
2942
Robert Baldyga1479e842013-10-09 08:41:57 +02002943 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002944 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002945 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002946 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2947 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002948 }
2949
2950 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002951 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002952 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2953 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002954 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002955
2956 /* FIFO has space or is empty (see GAHBCFG) */
2957 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002958 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002959 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2960 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002961 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002962 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002963 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002964 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002965}
2966
2967/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002968 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002969 * @hsotg: The device state.
2970 *
2971 * Handle updating the device settings after the enumeration phase has
2972 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002973 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002974static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002975{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002976 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09002977 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002978
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002979 /*
2980 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002981 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002982 * we connected at.
2983 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002984
2985 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2986
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002987 /*
2988 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002989 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002990 * not advertise a 64byte MPS on EP0.
2991 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002992
2993 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01002994 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002995 case DSTS_ENUMSPD_FS:
2996 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002997 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002998 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002999 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003000 break;
3001
Dinh Nguyen47a16852014-04-14 14:13:34 -07003002 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003003 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003004 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01003005 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003006 break;
3007
Dinh Nguyen47a16852014-04-14 14:13:34 -07003008 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003009 hsotg->gadget.speed = USB_SPEED_LOW;
Vardan Mikayelyan552d9402016-11-14 19:17:00 -08003010 ep0_mps = 8;
3011 ep_mps = 8;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003012 /*
3013 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003014 * moment, and the documentation seems to imply that it isn't
3015 * supported by the PHYs on some of the devices.
3016 */
3017 break;
3018 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02003019 dev_info(hsotg->dev, "new device is %s\n",
3020 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003021
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003022 /*
3023 * we should now know the maximum packet size for an
3024 * endpoint, so set the endpoints to a default value.
3025 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026
3027 if (ep0_mps) {
3028 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003029 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003030 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
3031 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003032 for (i = 1; i < hsotg->num_of_eps; i++) {
3033 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003034 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3035 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003036 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003037 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
3038 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003039 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003040 }
3041
3042 /* ensure after enumeration our EP0 is active */
3043
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003044 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003045
3046 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003047 dwc2_readl(hsotg->regs + DIEPCTL0),
3048 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003049}
3050
3051/**
3052 * kill_all_requests - remove all requests from the endpoint's queue
3053 * @hsotg: The device state.
3054 * @ep: The endpoint the requests may be on.
3055 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003056 *
3057 * Go through the requests on the given endpoint and mark them
3058 * completed with the given result code.
3059 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003060static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003061 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01003062 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003063{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003064 struct dwc2_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003065 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003066
Robert Baldyga6b448af2014-12-16 11:51:44 +01003067 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003068
Robert Baldyga6b448af2014-12-16 11:51:44 +01003069 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003070 dwc2_hsotg_complete_request(hsotg, ep, req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003071 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01003072
Robert Baldygab203d0a2014-09-09 10:44:56 +02003073 if (!hsotg->dedicated_fifos)
3074 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07003075 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003076 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003077 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003078}
3079
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003080/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003081 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082 * @hsotg: The device state.
3083 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02003084 * The device has been disconnected. Remove all current
3085 * transactions and signal the gadget driver that this
3086 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003087 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003088void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003089{
3090 unsigned ep;
3091
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01003092 if (!hsotg->connected)
3093 return;
3094
3095 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01003096 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003097
3098 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3099 if (hsotg->eps_in[ep])
3100 kill_all_requests(hsotg, hsotg->eps_in[ep],
3101 -ESHUTDOWN);
3102 if (hsotg->eps_out[ep])
3103 kill_all_requests(hsotg, hsotg->eps_out[ep],
3104 -ESHUTDOWN);
3105 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003106
3107 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02003108 hsotg->lx_state = DWC2_L3;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003109}
3110
3111/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003112 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003113 * @hsotg: The device state:
3114 * @periodic: True if this is a periodic FIFO interrupt
3115 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003116static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003117{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003118 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003119 int epno, ret;
3120
3121 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003122 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003123 ep = index_to_ep(hsotg, epno, 1);
3124
3125 if (!ep)
3126 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003127
3128 if (!ep->dir_in)
3129 continue;
3130
3131 if ((periodic && !ep->periodic) ||
3132 (!periodic && ep->periodic))
3133 continue;
3134
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003135 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003136 if (ret < 0)
3137 break;
3138 }
3139}
3140
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003141/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003142#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
3143 GINTSTS_PTXFEMP | \
3144 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003145
3146/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003147 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003148 * @hsotg: The device state
3149 *
3150 * Issue a soft reset to the core, and await the core finishing it.
3151 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003152void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003153 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02003154{
Gregory Herrero1ee69032015-09-29 12:08:27 +02003155 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003156 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003157 u32 usbcfg;
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003158 u32 dcfg = 0;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003159
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003160 /* Kill any ep0 requests as controller will be reinitialized */
3161 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
3162
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003163 if (!is_usb_reset)
John Youn241729b2015-12-17 11:17:59 -08003164 if (dwc2_core_reset(hsotg))
Gregory Herrero86de4892015-09-29 12:08:21 +02003165 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02003166
3167 /*
3168 * we must now enable ep0 ready for host detection and then
3169 * set configuration.
3170 */
3171
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003172 /* keep other bits untouched (so e.g. forced modes are not lost) */
3173 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3174 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3175 GUSBCFG_HNPCAP);
3176
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003177 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS &&
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003178 (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
3179 hsotg->params.speed == DWC2_SPEED_PARAM_LOW)) {
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003180 /* FS/LS Dedicated Transceiver Interface */
3181 usbcfg |= GUSBCFG_PHYSEL;
3182 } else {
3183 /* set the PLL on, remove the HNP/SRP and set the PHY */
3184 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3185 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3186 (val << GUSBCFG_USBTRDTIM_SHIFT);
3187 }
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003188 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003189
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003190 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003191
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003192 if (!is_usb_reset)
3193 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003194
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003195 dcfg |= DCFG_EPMISCNT(1);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003196
3197 switch (hsotg->params.speed) {
3198 case DWC2_SPEED_PARAM_LOW:
3199 dcfg |= DCFG_DEVSPD_LS;
3200 break;
3201 case DWC2_SPEED_PARAM_FULL:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003202 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3203 dcfg |= DCFG_DEVSPD_FS48;
3204 else
3205 dcfg |= DCFG_DEVSPD_FS;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003206 break;
3207 default:
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003208 dcfg |= DCFG_DEVSPD_HS;
3209 }
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08003210
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003211 dwc2_writel(dcfg, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003212
3213 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003214 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003215
3216 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003217 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003218 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003219 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003220 GINTSTS_USBRST | GINTSTS_RESETDET |
3221 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003222 GINTSTS_USBSUSP | GINTSTS_WKUPINT;
3223
3224 if (!using_desc_dma(hsotg))
3225 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003226
John Younbea8e862016-11-03 17:55:53 -07003227 if (hsotg->params.external_id_pin_ctl <= 0)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003228 intmsk |= GINTSTS_CONIDSTSCHNG;
3229
3230 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003231
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003232 if (using_dma(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003233 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
3234 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
3235 hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003236
3237 /* Set DDMA mode support in the core if needed */
3238 if (using_desc_dma(hsotg))
3239 __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
3240
3241 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003242 dwc2_writel(((hsotg->dedicated_fifos) ?
3243 (GAHBCFG_NP_TXF_EMP_LVL |
3244 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3245 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003246 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003247
3248 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003249 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3250 * when we have no data to transfer. Otherwise we get being flooded by
3251 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003252 */
3253
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003254 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003255 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003256 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003257 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003258 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003259
3260 /*
3261 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003262 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003263 */
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003264 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3265 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003266 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003267 DOEPMSK_SETUPMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003268 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003269
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003270 /* Enable BNA interrupt for DDMA */
3271 if (using_desc_dma(hsotg))
3272 __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
3273
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003274 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003275
3276 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003277 dwc2_readl(hsotg->regs + DIEPCTL0),
3278 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003279
3280 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003281 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003282
3283 /*
3284 * Enable the RXFIFO when in slave mode, as this is how we collect
3285 * the data. In DMA mode, we get events from the FIFO but also
3286 * things we cannot process, so do not use it.
3287 */
3288 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003289 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003290
3291 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003292 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3293 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003294
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003295 if (!is_usb_reset) {
3296 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3297 udelay(10); /* see openiboot */
3298 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3299 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003300
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003301 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003302
3303 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003304 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003305 * writing to the EPCTL register..
3306 */
3307
3308 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003309 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003310 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003311
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003312 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003313 DXEPCTL_CNAK | DXEPCTL_EPENA |
3314 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003315 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003316
3317 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003318 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003319 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003320
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003321 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003322
3323 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003324 dwc2_readl(hsotg->regs + DIEPCTL0),
3325 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003326
3327 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003328 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3329 if (!is_usb_reset)
3330 val |= DCTL_SFTDISCON;
3331 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003332
3333 /* must be at-least 3ms to allow bus to see disconnect */
3334 mdelay(3);
3335
Gregory Herrero065d3932015-09-22 15:16:54 +02003336 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003337}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003338
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003339static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003340{
3341 /* set the soft-disconnect bit */
3342 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
3343}
3344
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003345void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003346{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003347 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003348 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003349}
3350
3351/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003352 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3353 * @hsotg: The device state:
3354 *
3355 * This interrupt indicates one of the following conditions occurred while
3356 * transmitting an ISOC transaction.
3357 * - Corrupted IN Token for ISOC EP.
3358 * - Packet not complete in FIFO.
3359 *
3360 * The following actions will be taken:
3361 * - Determine the EP
3362 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3363 */
3364static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3365{
3366 struct dwc2_hsotg_ep *hs_ep;
3367 u32 epctrl;
3368 u32 idx;
3369
3370 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3371
3372 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3373 hs_ep = hsotg->eps_in[idx];
3374 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
3375 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3376 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3377 epctrl |= DXEPCTL_SNAK;
3378 epctrl |= DXEPCTL_EPDIS;
3379 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3380 }
3381 }
3382
3383 /* Clear interrupt */
3384 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3385}
3386
3387/**
3388 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3389 * @hsotg: The device state:
3390 *
3391 * This interrupt indicates one of the following conditions occurred while
3392 * transmitting an ISOC transaction.
3393 * - Corrupted OUT Token for ISOC EP.
3394 * - Packet not complete in FIFO.
3395 *
3396 * The following actions will be taken:
3397 * - Determine the EP
3398 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3399 */
3400static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3401{
3402 u32 gintsts;
3403 u32 gintmsk;
3404 u32 epctrl;
3405 struct dwc2_hsotg_ep *hs_ep;
3406 int idx;
3407
3408 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3409
3410 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3411 hs_ep = hsotg->eps_out[idx];
3412 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3413 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3414 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3415 /* Unmask GOUTNAKEFF interrupt */
3416 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3417 gintmsk |= GINTSTS_GOUTNAKEFF;
3418 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3419
3420 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3421 if (!(gintsts & GINTSTS_GOUTNAKEFF))
3422 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3423 }
3424 }
3425
3426 /* Clear interrupt */
3427 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3428}
3429
3430/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003431 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003432 * @irq: The IRQ number triggered
3433 * @pw: The pw value when registered the handler.
3434 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003435static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003436{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003437 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003438 int retry_count = 8;
3439 u32 gintsts;
3440 u32 gintmsk;
3441
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003442 if (!dwc2_is_device_mode(hsotg))
3443 return IRQ_NONE;
3444
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003445 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003446irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003447 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3448 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003449
3450 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3451 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3452
3453 gintsts &= gintmsk;
3454
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003455 if (gintsts & GINTSTS_RESETDET) {
3456 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3457
3458 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3459
3460 /* This event must be used only if controller is suspended */
3461 if (hsotg->lx_state == DWC2_L2) {
3462 dwc2_exit_hibernation(hsotg, true);
3463 hsotg->lx_state = DWC2_L0;
3464 }
3465 }
3466
3467 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
3468
3469 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3470 u32 connected = hsotg->connected;
3471
3472 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3473 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3474 dwc2_readl(hsotg->regs + GNPTXSTS));
3475
3476 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3477
3478 /* Report disconnection if it is not already done. */
3479 dwc2_hsotg_disconnect(hsotg);
3480
3481 if (usb_status & GOTGCTL_BSESVLD && connected)
3482 dwc2_hsotg_core_init_disconnected(hsotg, true);
3483 }
3484
Dinh Nguyen47a16852014-04-14 14:13:34 -07003485 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003486 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003487
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003488 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003489 }
3490
Dinh Nguyen47a16852014-04-14 14:13:34 -07003491 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003492 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3493 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003494 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003495 int ep;
3496
Robert Baldyga7e804652013-09-19 11:50:20 +02003497 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003498 daint_out = daint >> DAINT_OUTEP_SHIFT;
3499 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003500
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003501 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3502
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003503 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3504 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003505 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003506 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003507 }
3508
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003509 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3510 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003511 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003512 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003513 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003514 }
3515
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003516 /* check both FIFOs */
3517
Dinh Nguyen47a16852014-04-14 14:13:34 -07003518 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003519 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3520
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003521 /*
3522 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003523 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003524 * it needs re-enabling
3525 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003526
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003527 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3528 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003529 }
3530
Dinh Nguyen47a16852014-04-14 14:13:34 -07003531 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003532 dev_dbg(hsotg->dev, "PTxFEmp\n");
3533
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003534 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003535
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003536 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3537 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003538 }
3539
Dinh Nguyen47a16852014-04-14 14:13:34 -07003540 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003541 /*
3542 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003543 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003544 * set.
3545 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003546
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003547 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003548 }
3549
Dinh Nguyen47a16852014-04-14 14:13:34 -07003550 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003551 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003552 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003553 }
3554
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003555 /*
3556 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003557 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003558 * the occurrence.
3559 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003560
Dinh Nguyen47a16852014-04-14 14:13:34 -07003561 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003562 u8 idx;
3563 u32 epctrl;
3564 u32 gintmsk;
3565 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003566
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003567 /* Mask this interrupt */
3568 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3569 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3570 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003571
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003572 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3573 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3574 hs_ep = hsotg->eps_out[idx];
3575 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3576
3577 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
3578 epctrl |= DXEPCTL_SNAK;
3579 epctrl |= DXEPCTL_EPDIS;
3580 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3581 }
3582 }
3583
3584 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003585 }
3586
Dinh Nguyen47a16852014-04-14 14:13:34 -07003587 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003588 dev_info(hsotg->dev, "GINNakEff triggered\n");
3589
Gregory Herrero3be99cd2015-12-07 12:07:31 +01003590 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003591
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003592 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003593 }
3594
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003595 if (gintsts & GINTSTS_INCOMPL_SOIN)
3596 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003597
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003598 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3599 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003600
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003601 /*
3602 * if we've had fifo events, we should try and go around the
3603 * loop again to see if there's any point in returning yet.
3604 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003605
3606 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
3607 goto irq_retry;
3608
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003609 spin_unlock(&hsotg->lock);
3610
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003611 return IRQ_HANDLED;
3612}
3613
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003614static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3615 u32 bit, u32 timeout)
3616{
3617 u32 i;
3618
3619 for (i = 0; i < timeout; i++) {
3620 if (dwc2_readl(hs_otg->regs + reg) & bit)
3621 return 0;
3622 udelay(1);
3623 }
3624
3625 return -ETIMEDOUT;
3626}
3627
3628static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3629 struct dwc2_hsotg_ep *hs_ep)
3630{
3631 u32 epctrl_reg;
3632 u32 epint_reg;
3633
3634 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3635 DOEPCTL(hs_ep->index);
3636 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3637 DOEPINT(hs_ep->index);
3638
3639 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3640 hs_ep->name);
3641
3642 if (hs_ep->dir_in) {
3643 if (hsotg->dedicated_fifos || hs_ep->periodic) {
3644 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3645 /* Wait for Nak effect */
3646 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3647 DXEPINT_INEPNAKEFF, 100))
3648 dev_warn(hsotg->dev,
3649 "%s: timeout DIEPINT.NAKEFF\n",
3650 __func__);
3651 } else {
3652 __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
3653 /* Wait for Nak effect */
3654 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3655 GINTSTS_GINNAKEFF, 100))
3656 dev_warn(hsotg->dev,
3657 "%s: timeout GINTSTS.GINNAKEFF\n",
3658 __func__);
3659 }
3660 } else {
3661 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3662 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3663
3664 /* Wait for global nak to take effect */
3665 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3666 GINTSTS_GOUTNAKEFF, 100))
3667 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3668 __func__);
3669 }
3670
3671 /* Disable ep */
3672 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3673
3674 /* Wait for ep to be disabled */
3675 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3676 dev_warn(hsotg->dev,
3677 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3678
3679 /* Clear EPDISBLD interrupt */
3680 __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
3681
3682 if (hs_ep->dir_in) {
3683 unsigned short fifo_index;
3684
3685 if (hsotg->dedicated_fifos || hs_ep->periodic)
3686 fifo_index = hs_ep->fifo_index;
3687 else
3688 fifo_index = 0;
3689
3690 /* Flush TX FIFO */
3691 dwc2_flush_tx_fifo(hsotg, fifo_index);
3692
3693 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3694 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
3695 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
3696
3697 } else {
3698 /* Remove global NAKs */
3699 __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
3700 }
3701}
3702
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003703/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003704 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003705 * @ep: The USB endpint to configure
3706 * @desc: The USB endpoint descriptor to configure with.
3707 *
3708 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003709 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003710static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003711 const struct usb_endpoint_descriptor *desc)
3712{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003713 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003714 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003715 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003716 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003717 u32 epctrl_reg;
3718 u32 epctrl;
3719 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003720 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003721 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003722 unsigned int dir_in;
3723 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003724 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003725
3726 dev_dbg(hsotg->dev,
3727 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3728 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3729 desc->wMaxPacketSize, desc->bInterval);
3730
3731 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003732 if (index == 0) {
3733 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3734 return -EINVAL;
3735 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003736
3737 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3738 if (dir_in != hs_ep->dir_in) {
3739 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3740 return -EINVAL;
3741 }
3742
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003743 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003744 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003745
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003746 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003747
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003748 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003749 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003750
3751 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3752 __func__, epctrl, epctrl_reg);
3753
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003754 /* Allocate DMA descriptor chain for non-ctrl endpoints */
3755 if (using_desc_dma(hsotg)) {
3756 hs_ep->desc_list = dma_alloc_coherent(hsotg->dev,
3757 MAX_DMA_DESC_NUM_GENERIC *
3758 sizeof(struct dwc2_dma_desc),
3759 &hs_ep->desc_list_dma, GFP_KERNEL);
3760 if (!hs_ep->desc_list) {
3761 ret = -ENOMEM;
3762 goto error2;
3763 }
3764 }
3765
Lukasz Majewski22258f42012-06-14 10:02:24 +02003766 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003767
Dinh Nguyen47a16852014-04-14 14:13:34 -07003768 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3769 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003770
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003771 /*
3772 * mark the endpoint as active, otherwise the core may ignore
3773 * transactions entirely for this endpoint
3774 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003775 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003776
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003777 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003778 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003779
3780 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003781 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003782 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003783 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003784 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003785
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003786 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3787 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003788 epctrl |= DXEPCTL_EPTYPE_ISO;
3789 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003790 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003791 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003792 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003793 hs_ep->isoc_chain_num = 0;
3794 hs_ep->next_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003795 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003796 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003797 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3798 mask |= DIEPMSK_NAKMSK;
3799 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3800 } else {
3801 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3802 mask |= DOEPMSK_OUTTKNEPDISMSK;
3803 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3804 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003805 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003806
3807 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003808 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003809 break;
3810
3811 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003812 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003813 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003814
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003815 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3816 hs_ep->interval = 1 << (desc->bInterval - 1);
3817
Dinh Nguyen47a16852014-04-14 14:13:34 -07003818 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003819 break;
3820
3821 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003822 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003823 break;
3824 }
3825
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003826 /*
3827 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003828 * a unique tx-fifo even if it is non-periodic.
3829 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003830 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003831 u32 fifo_index = 0;
3832 u32 fifo_size = UINT_MAX;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003833 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003834 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02003835 if (hsotg->fifo_map & (1<<i))
3836 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003837 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Robert Baldygab203d0a2014-09-09 10:44:56 +02003838 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
3839 if (val < size)
3840 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003841 /* Search for smallest acceptable fifo */
3842 if (val < fifo_size) {
3843 fifo_size = val;
3844 fifo_index = i;
3845 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003846 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003847 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003848 dev_err(hsotg->dev,
3849 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303850 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003851 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303852 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003853 hsotg->fifo_map |= 1 << fifo_index;
3854 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3855 hs_ep->fifo_index = fifo_index;
3856 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003857 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003858
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003859 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003860 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003861 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003862
3863 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3864 __func__, epctrl);
3865
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003866 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003867 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003868 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003869
3870 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003871 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003872
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003873error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003874 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003875
3876error2:
3877 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
3878 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3879 sizeof(struct dwc2_dma_desc),
3880 hs_ep->desc_list, hs_ep->desc_list_dma);
3881 hs_ep->desc_list = NULL;
3882 }
3883
Julia Lawall19c190f2010-03-29 17:36:44 +02003884 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003885}
3886
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003887/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003888 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003889 * @ep: The endpoint to disable.
3890 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003891static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003892{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003893 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003894 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003895 int dir_in = hs_ep->dir_in;
3896 int index = hs_ep->index;
3897 unsigned long flags;
3898 u32 epctrl_reg;
3899 u32 ctrl;
3900
Marek Szyprowski1e011292014-09-09 10:44:54 +02003901 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003902
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003903 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003904 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3905 return -EINVAL;
3906 }
3907
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003908 /* Remove DMA memory allocated for non-control Endpoints */
3909 if (using_desc_dma(hsotg)) {
3910 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3911 sizeof(struct dwc2_dma_desc),
3912 hs_ep->desc_list, hs_ep->desc_list_dma);
3913 hs_ep->desc_list = NULL;
3914 }
3915
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003916 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003917
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003918 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003919
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003920 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Vahram Aharonyana4f82772016-11-14 19:16:53 -08003921
3922 if (ctrl & DXEPCTL_EPENA)
3923 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
3924
Dinh Nguyen47a16852014-04-14 14:13:34 -07003925 ctrl &= ~DXEPCTL_EPENA;
3926 ctrl &= ~DXEPCTL_USBACTEP;
3927 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003928
3929 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003930 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003931
3932 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003933 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003934
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003935 /* terminate all requests with shutdown */
3936 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3937
Robert Baldyga1c07b202016-08-29 13:39:00 -07003938 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3939 hs_ep->fifo_index = 0;
3940 hs_ep->fifo_size = 0;
3941
Lukasz Majewski22258f42012-06-14 10:02:24 +02003942 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003943 return 0;
3944}
3945
3946/**
3947 * on_list - check request is on the given endpoint
3948 * @ep: The endpoint to check.
3949 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003950 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003951static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003952{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003953 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003954
3955 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
3956 if (req == test)
3957 return true;
3958 }
3959
3960 return false;
3961}
3962
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003963/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003964 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003965 * @ep: The endpoint to dequeue.
3966 * @req: The request to be removed from a queue.
3967 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003968static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003969{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003970 struct dwc2_hsotg_req *hs_req = our_req(req);
3971 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003972 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003973 unsigned long flags;
3974
Marek Szyprowski1e011292014-09-09 10:44:54 +02003975 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003976
Lukasz Majewski22258f42012-06-14 10:02:24 +02003977 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003978
3979 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02003980 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003981 return -EINVAL;
3982 }
3983
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003984 /* Dequeue already started request */
3985 if (req == &hs_ep->req->req)
3986 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
3987
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003988 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02003989 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003990
3991 return 0;
3992}
3993
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003994/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003995 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003996 * @ep: The endpoint to set halt.
3997 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003998 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
3999 * the endpoint is busy processing requests.
4000 *
4001 * We need to stall the endpoint immediately if request comes from set_feature
4002 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004003 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004004static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004005{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004006 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004007 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004008 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004009 u32 epreg;
4010 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004011 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004012
4013 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4014
Robert Baldygac9f721b2014-01-14 08:36:00 +01004015 if (index == 0) {
4016 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004017 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004018 else
4019 dev_warn(hs->dev,
4020 "%s: can't clear halt on ep0\n", __func__);
4021 return 0;
4022 }
4023
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004024 if (hs_ep->isochronous) {
4025 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4026 return -EINVAL;
4027 }
4028
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004029 if (!now && value && !list_empty(&hs_ep->queue)) {
4030 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4031 ep->name);
4032 return -EAGAIN;
4033 }
4034
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004035 if (hs_ep->dir_in) {
4036 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004037 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004038
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004039 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004040 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004041 if (epctl & DXEPCTL_EPENA)
4042 epctl |= DXEPCTL_EPDIS;
4043 } else {
4044 epctl &= ~DXEPCTL_STALL;
4045 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4046 if (xfertype == DXEPCTL_EPTYPE_BULK ||
4047 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
4048 epctl |= DXEPCTL_SETD0PID;
4049 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004050 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004051 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004052
4053 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004054 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004055
4056 if (value)
4057 epctl |= DXEPCTL_STALL;
4058 else {
4059 epctl &= ~DXEPCTL_STALL;
4060 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4061 if (xfertype == DXEPCTL_EPTYPE_BULK ||
4062 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
4063 epctl |= DXEPCTL_SETD0PID;
4064 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004065 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004066 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004067
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004068 hs_ep->halted = value;
4069
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004070 return 0;
4071}
4072
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004073/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004074 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004075 * @ep: The endpoint to set halt.
4076 * @value: Set or unset the halt.
4077 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004078static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004079{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004080 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004081 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004082 unsigned long flags = 0;
4083 int ret = 0;
4084
4085 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004086 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004087 spin_unlock_irqrestore(&hs->lock, flags);
4088
4089 return ret;
4090}
4091
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004092static struct usb_ep_ops dwc2_hsotg_ep_ops = {
4093 .enable = dwc2_hsotg_ep_enable,
4094 .disable = dwc2_hsotg_ep_disable,
4095 .alloc_request = dwc2_hsotg_ep_alloc_request,
4096 .free_request = dwc2_hsotg_ep_free_request,
4097 .queue = dwc2_hsotg_ep_queue_lock,
4098 .dequeue = dwc2_hsotg_ep_dequeue,
4099 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004100 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004101};
4102
4103/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004104 * dwc2_hsotg_init - initalize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004105 * @hsotg: The driver state
4106 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004107static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004108{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004109 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004110 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004111 /* unmask subset of endpoint interrupts */
4112
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004113 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4114 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4115 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004116
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004117 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4118 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4119 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004120
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004121 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004122
4123 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07004124 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004125
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004126 /* setup fifos */
4127
4128 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004129 dwc2_readl(hsotg->regs + GRXFSIZ),
4130 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004131
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004132 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004133
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004134 /* keep other bits untouched (so e.g. forced modes are not lost) */
4135 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4136 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
4137 GUSBCFG_HNPCAP);
4138
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004139 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004140 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004141 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4142 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4143 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004144
Gregory Herrerof5090042015-01-09 13:38:47 +01004145 if (using_dma(hsotg))
4146 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004147}
4148
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004149/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004150 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004151 * @gadget: The usb gadget state
4152 * @driver: The usb gadget driver
4153 *
4154 * Perform initialization to prepare udc device and driver
4155 * to work.
4156 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004157static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004158 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004159{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004160 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004161 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004162 int ret;
4163
4164 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004165 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004166 return -ENODEV;
4167 }
4168
4169 if (!driver) {
4170 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4171 return -EINVAL;
4172 }
4173
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004174 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004175 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004176
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004177 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004178 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4179 return -EINVAL;
4180 }
4181
4182 WARN_ON(hsotg->driver);
4183
4184 driver->driver.bus = NULL;
4185 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004186 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004187 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4188
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004189 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4190 ret = dwc2_lowlevel_hw_enable(hsotg);
4191 if (ret)
4192 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004193 }
4194
Gregory Herrerof6c01592015-01-09 13:38:41 +01004195 if (!IS_ERR_OR_NULL(hsotg->uphy))
4196 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004197
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004198 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004199 if (dwc2_hw_is_device(hsotg)) {
4200 dwc2_hsotg_init(hsotg);
4201 dwc2_hsotg_core_init_disconnected(hsotg, false);
4202 }
4203
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004204 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004205 spin_unlock_irqrestore(&hsotg->lock, flags);
4206
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004207 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004208
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004209 return 0;
4210
4211err:
4212 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004213 return ret;
4214}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004215
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004216/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004217 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004218 * @gadget: The usb gadget state
4219 * @driver: The usb gadget driver
4220 *
4221 * Stop udc hw block and stay tunned for future transmissions
4222 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004223static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004224{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004225 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004226 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004227 int ep;
4228
4229 if (!hsotg)
4230 return -ENODEV;
4231
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004232 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004233 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4234 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004235 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004236 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004237 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004238 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004239
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004240 spin_lock_irqsave(&hsotg->lock, flags);
4241
Marek Szyprowski32805c32014-10-20 12:45:33 +02004242 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004243 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004244 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004245
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004246 spin_unlock_irqrestore(&hsotg->lock, flags);
4247
Gregory Herrerof6c01592015-01-09 13:38:41 +01004248 if (!IS_ERR_OR_NULL(hsotg->uphy))
4249 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004250
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004251 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4252 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004253
4254 return 0;
4255}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004256
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004257/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004258 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004259 * @gadget: The usb gadget state
4260 *
4261 * Read the {micro} frame number
4262 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004263static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004264{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004265 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004266}
4267
Lukasz Majewskia188b682012-06-22 09:29:56 +02004268/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004269 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004270 * @gadget: The usb gadget state
4271 * @is_on: Current state of the USB PHY
4272 *
4273 * Connect/Disconnect the USB PHY pullup
4274 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004275static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004276{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004277 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004278 unsigned long flags = 0;
4279
Gregory Herrero77ba9112015-09-29 12:08:19 +02004280 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
4281 hsotg->op_state);
4282
4283 /* Don't modify pullup state while in host mode */
4284 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4285 hsotg->enabled = is_on;
4286 return 0;
4287 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004288
4289 spin_lock_irqsave(&hsotg->lock, flags);
4290 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004291 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004292 dwc2_hsotg_core_init_disconnected(hsotg, false);
4293 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004294 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004295 dwc2_hsotg_core_disconnect(hsotg);
4296 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004297 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004298 }
4299
4300 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4301 spin_unlock_irqrestore(&hsotg->lock, flags);
4302
4303 return 0;
4304}
4305
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004306static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004307{
4308 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4309 unsigned long flags;
4310
4311 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4312 spin_lock_irqsave(&hsotg->lock, flags);
4313
Gregory Herrero61f72232015-09-29 12:08:28 +02004314 /*
4315 * If controller is hibernated, it must exit from hibernation
4316 * before being initialized / de-initialized
4317 */
4318 if (hsotg->lx_state == DWC2_L2)
4319 dwc2_exit_hibernation(hsotg, false);
4320
Gregory Herrero83d98222015-01-09 13:39:02 +01004321 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004322 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004323
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004324 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01004325 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004326 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004327 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004328 dwc2_hsotg_core_disconnect(hsotg);
4329 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004330 }
4331
4332 spin_unlock_irqrestore(&hsotg->lock, flags);
4333 return 0;
4334}
4335
Gregory Herrero596d6962015-01-09 13:39:08 +01004336/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004337 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004338 * @gadget: The usb gadget state
4339 * @mA: Amount of current
4340 *
4341 * Report how much power the device may consume to the phy.
4342 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004343static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004344{
4345 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4346
4347 if (IS_ERR_OR_NULL(hsotg->uphy))
4348 return -ENOTSUPP;
4349 return usb_phy_set_power(hsotg->uphy, mA);
4350}
4351
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004352static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4353 .get_frame = dwc2_hsotg_gadget_getframe,
4354 .udc_start = dwc2_hsotg_udc_start,
4355 .udc_stop = dwc2_hsotg_udc_stop,
4356 .pullup = dwc2_hsotg_pullup,
4357 .vbus_session = dwc2_hsotg_vbus_session,
4358 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004359};
4360
4361/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004362 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004363 * @hsotg: The device state.
4364 * @hs_ep: The endpoint to be initialised.
4365 * @epnum: The endpoint number
4366 *
4367 * Initialise the given endpoint (as part of the probe and device state
4368 * creation) to give to the gadget driver. Setup the endpoint name, any
4369 * direction information and other state that may be required.
4370 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004371static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
4372 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004373 int epnum,
4374 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004375{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004376 char *dir;
4377
4378 if (epnum == 0)
4379 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004380 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004381 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004382 else
4383 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004384
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004385 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004386 hs_ep->index = epnum;
4387
4388 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4389
4390 INIT_LIST_HEAD(&hs_ep->queue);
4391 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4392
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004393 /* add to the list of endpoints known by the gadget driver */
4394 if (epnum)
4395 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4396
4397 hs_ep->parent = hsotg;
4398 hs_ep->ep.name = hs_ep->name;
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004399
4400 if (hsotg->params.speed == DWC2_SPEED_PARAM_LOW)
4401 usb_ep_set_maxpacket_limit(&hs_ep->ep, 8);
4402 else
4403 usb_ep_set_maxpacket_limit(&hs_ep->ep,
4404 epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004405 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004406
Robert Baldyga29545222015-07-31 16:00:18 +02004407 if (epnum == 0) {
4408 hs_ep->ep.caps.type_control = true;
4409 } else {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08004410 if (hsotg->params.speed != DWC2_SPEED_PARAM_LOW) {
4411 hs_ep->ep.caps.type_iso = true;
4412 hs_ep->ep.caps.type_bulk = true;
4413 }
Robert Baldyga29545222015-07-31 16:00:18 +02004414 hs_ep->ep.caps.type_int = true;
4415 }
4416
4417 if (dir_in)
4418 hs_ep->ep.caps.dir_in = true;
4419 else
4420 hs_ep->ep.caps.dir_out = true;
4421
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004422 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004423 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004424 * to be something valid.
4425 */
4426
4427 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004428 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004429 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004430 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004431 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004432 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004433 }
4434}
4435
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004436/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004437 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004438 * @param: The device state
4439 *
4440 * Read the USB core HW configuration registers
4441 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004442static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004443{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004444 u32 cfg;
4445 u32 ep_type;
4446 u32 i;
4447
Ben Dooks10aebc72010-07-19 09:40:44 +01004448 /* check hardware configuration */
4449
John Youn43e90342015-12-17 11:17:45 -08004450 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4451
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004452 /* Add ep0 */
4453 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004454
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004455 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004456 GFP_KERNEL);
4457 if (!hsotg->eps_in[0])
4458 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004459 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004460 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004461
John Youn43e90342015-12-17 11:17:45 -08004462 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004463 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004464 ep_type = cfg & 3;
4465 /* Direction in or both */
4466 if (!(ep_type & 2)) {
4467 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004468 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004469 if (!hsotg->eps_in[i])
4470 return -ENOMEM;
4471 }
4472 /* Direction out or both */
4473 if (!(ep_type & 1)) {
4474 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004475 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004476 if (!hsotg->eps_out[i])
4477 return -ENOMEM;
4478 }
4479 }
4480
John Youn43e90342015-12-17 11:17:45 -08004481 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4482 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004483
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004484 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4485 hsotg->num_of_eps,
4486 hsotg->dedicated_fifos ? "dedicated" : "shared",
4487 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004488 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004489}
4490
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004491/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004492 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004493 * @param: The device state
4494 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004495static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004496{
Mark Brown83a01802011-06-01 17:16:15 +01004497#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004498 struct device *dev = hsotg->dev;
4499 void __iomem *regs = hsotg->regs;
4500 u32 val;
4501 int idx;
4502
4503 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004504 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4505 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004506
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004507 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004508 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004509
4510 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004511 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004512
4513 /* show periodic fifo settings */
4514
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004515 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004516 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004517 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004518 val >> FIFOSIZE_DEPTH_SHIFT,
4519 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004520 }
4521
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004522 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004523 dev_info(dev,
4524 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004525 dwc2_readl(regs + DIEPCTL(idx)),
4526 dwc2_readl(regs + DIEPTSIZ(idx)),
4527 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004528
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004529 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004530 dev_info(dev,
4531 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004532 idx, dwc2_readl(regs + DOEPCTL(idx)),
4533 dwc2_readl(regs + DOEPTSIZ(idx)),
4534 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004535
4536 }
4537
4538 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004539 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004540#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004541}
4542
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004543/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004544 * dwc2_gadget_init - init function for gadget
4545 * @dwc2: The data structure for the DWC2 driver.
4546 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004547 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06004548int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004549{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004550 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004551 int epnum;
4552 int ret;
John Youn43e90342015-12-17 11:17:45 -08004553
Gregory Herrero0a176272015-01-09 13:38:52 +01004554 /* Dump fifo information */
4555 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004556 hsotg->params.g_np_tx_fifo_size);
4557 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004558
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004559 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004560 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004561 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02004562 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4563 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004564 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4565 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004566
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004567 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004568 if (ret) {
4569 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004570 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004571 }
4572
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004573 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4574 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004575 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004576 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004577
4578 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4579 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004580 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004581 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004582
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004583 if (using_desc_dma(hsotg)) {
4584 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4585 if (ret < 0)
4586 return ret;
4587 }
4588
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004589 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004590 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004591 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004592 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004593 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004594 }
4595
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004596 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4597
4598 if (hsotg->num_of_eps == 0) {
4599 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004600 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004601 }
4602
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004603 /* setup endpoint information */
4604
4605 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004606 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004607
4608 /* allocate EP0 request */
4609
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004610 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004611 GFP_KERNEL);
4612 if (!hsotg->ctrl_req) {
4613 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004614 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004615 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004616
4617 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004618 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4619 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004620 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004621 epnum, 1);
4622 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004623 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004624 epnum, 0);
4625 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004626
Dinh Nguyen117777b2014-11-11 11:13:34 -06004627 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004628 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004629 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004630
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004631 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004632
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004633 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004634}
4635
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004636/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004637 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004638 * @pdev: The platform information for the driver
4639 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004640int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004641{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004642 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004643
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004644 return 0;
4645}
4646
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004647int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004648{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004649 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004650
Gregory Herrero9e779772015-04-29 22:09:07 +02004651 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004652 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004653
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004654 if (hsotg->driver) {
4655 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004656
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004657 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4658 hsotg->driver->driver.name);
4659
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004660 spin_lock_irqsave(&hsotg->lock, flags);
4661 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004662 dwc2_hsotg_core_disconnect(hsotg);
4663 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004664 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4665 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004666
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004667 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4668 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004669 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004670 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004671 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004672 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004673 }
4674
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004675 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004676}
4677
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004678int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004679{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004680 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004681
Gregory Herrero9e779772015-04-29 22:09:07 +02004682 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004683 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004684
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004685 if (hsotg->driver) {
4686 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4687 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004688
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004689 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004690 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004691 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004692 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004693 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004694 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004695
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004696 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004697}
John Youn58e52ff6a2016-02-23 19:54:57 -08004698
4699/**
4700 * dwc2_backup_device_registers() - Backup controller device registers.
4701 * When suspending usb bus, registers needs to be backuped
4702 * if controller power is disabled once suspended.
4703 *
4704 * @hsotg: Programming view of the DWC_otg controller
4705 */
4706int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4707{
4708 struct dwc2_dregs_backup *dr;
4709 int i;
4710
4711 dev_dbg(hsotg->dev, "%s\n", __func__);
4712
4713 /* Backup dev regs */
4714 dr = &hsotg->dr_backup;
4715
4716 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4717 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4718 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4719 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4720 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4721
4722 for (i = 0; i < hsotg->num_of_eps; i++) {
4723 /* Backup IN EPs */
4724 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4725
4726 /* Ensure DATA PID is correctly configured */
4727 if (dr->diepctl[i] & DXEPCTL_DPID)
4728 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4729 else
4730 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4731
4732 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4733 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4734
4735 /* Backup OUT EPs */
4736 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4737
4738 /* Ensure DATA PID is correctly configured */
4739 if (dr->doepctl[i] & DXEPCTL_DPID)
4740 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4741 else
4742 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4743
4744 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4745 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4746 }
4747 dr->valid = true;
4748 return 0;
4749}
4750
4751/**
4752 * dwc2_restore_device_registers() - Restore controller device registers.
4753 * When resuming usb bus, device registers needs to be restored
4754 * if controller power were disabled.
4755 *
4756 * @hsotg: Programming view of the DWC_otg controller
4757 */
4758int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4759{
4760 struct dwc2_dregs_backup *dr;
4761 u32 dctl;
4762 int i;
4763
4764 dev_dbg(hsotg->dev, "%s\n", __func__);
4765
4766 /* Restore dev regs */
4767 dr = &hsotg->dr_backup;
4768 if (!dr->valid) {
4769 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4770 __func__);
4771 return -EINVAL;
4772 }
4773 dr->valid = false;
4774
4775 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4776 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4777 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4778 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4779 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4780
4781 for (i = 0; i < hsotg->num_of_eps; i++) {
4782 /* Restore IN EPs */
4783 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4784 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4785 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4786
4787 /* Restore OUT EPs */
4788 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4789 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4790 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4791 }
4792
4793 /* Set the Power-On Programming done bit */
4794 dctl = dwc2_readl(hsotg->regs + DCTL);
4795 dctl |= DCTL_PWRONPRGDONE;
4796 dwc2_writel(dctl, hsotg->regs + DCTL);
4797
4798 return 0;
4799}