blob: e67283237fd44030a2337f278dc018bad2b0164e [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 Baldyga6b448af42014-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 Baldyga6b448af42014-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 Baldyga6b448af42014-12-16 11:51:44 +01003067 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003068
Robert Baldyga6b448af42014-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 Baldyga6b448af42014-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 &&
3178 hsotg->params.speed == DWC2_SPEED_PARAM_FULL) {
3179 /* FS/LS Dedicated Transceiver Interface */
3180 usbcfg |= GUSBCFG_PHYSEL;
3181 } else {
3182 /* set the PLL on, remove the HNP/SRP and set the PHY */
3183 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
3184 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3185 (val << GUSBCFG_USBTRDTIM_SHIFT);
3186 }
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003187 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003188
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003189 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003190
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003191 if (!is_usb_reset)
3192 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003193
Vahram Aharonyan79c3b5b2016-11-14 19:16:55 -08003194 dcfg |= DCFG_EPMISCNT(1);
3195 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL) {
3196 if (hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS)
3197 dcfg |= DCFG_DEVSPD_FS48;
3198 else
3199 dcfg |= DCFG_DEVSPD_FS;
3200 } else {
3201 dcfg |= DCFG_DEVSPD_HS;
3202 }
3203 dwc2_writel(dcfg, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003204
3205 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003206 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003207
3208 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003209 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02003210 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003211 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02003212 GINTSTS_USBRST | GINTSTS_RESETDET |
3213 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Vahram Aharonyanf4736702016-11-14 19:16:38 -08003214 GINTSTS_USBSUSP | GINTSTS_WKUPINT;
3215
3216 if (!using_desc_dma(hsotg))
3217 intmsk |= GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02003218
John Younbea8e862016-11-03 17:55:53 -07003219 if (hsotg->params.external_id_pin_ctl <= 0)
Gregory Herrero1ee69032015-09-29 12:08:27 +02003220 intmsk |= GINTSTS_CONIDSTSCHNG;
3221
3222 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003223
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003224 if (using_dma(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003225 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
3226 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
3227 hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003228
3229 /* Set DDMA mode support in the core if needed */
3230 if (using_desc_dma(hsotg))
3231 __orr32(hsotg->regs + DCFG, DCFG_DESCDMA_EN);
3232
3233 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003234 dwc2_writel(((hsotg->dedicated_fifos) ?
3235 (GAHBCFG_NP_TXF_EMP_LVL |
3236 GAHBCFG_P_TXF_EMP_LVL) : 0) |
3237 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Vahram Aharonyana5c18f12016-11-14 19:16:34 -08003238 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003239
3240 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02003241 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
3242 * when we have no data to transfer. Otherwise we get being flooded by
3243 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003244 */
3245
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003246 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01003247 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003248 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003249 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003250 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003251
3252 /*
3253 * don't need XferCompl, we get that from RXFIFO in slave mode. In
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003254 * DMA mode we may need this and StsPhseRcvd.
Lukasz Majewski308d7342012-05-04 14:17:05 +02003255 */
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003256 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK |
3257 DOEPMSK_STSPHSERCVDMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003258 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vahram Aharonyan9d9a6b02016-11-14 19:16:29 -08003259 DOEPMSK_SETUPMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003260 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003261
Vahram Aharonyanec01f0b2016-11-14 19:16:43 -08003262 /* Enable BNA interrupt for DDMA */
3263 if (using_desc_dma(hsotg))
3264 __orr32(hsotg->regs + DOEPMSK, DOEPMSK_BNAMSK);
3265
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003266 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003267
3268 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003269 dwc2_readl(hsotg->regs + DIEPCTL0),
3270 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003271
3272 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003273 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003274
3275 /*
3276 * Enable the RXFIFO when in slave mode, as this is how we collect
3277 * the data. In DMA mode, we get events from the FIFO but also
3278 * things we cannot process, so do not use it.
3279 */
3280 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003281 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003282
3283 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003284 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
3285 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003286
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003287 if (!is_usb_reset) {
3288 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3289 udelay(10); /* see openiboot */
3290 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
3291 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02003292
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003293 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003294
3295 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003296 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02003297 * writing to the EPCTL register..
3298 */
3299
3300 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003301 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003302 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003303
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003304 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003305 DXEPCTL_CNAK | DXEPCTL_EPENA |
3306 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003307 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003308
3309 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003310 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07003311 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003312
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003313 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003314
3315 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003316 dwc2_readl(hsotg->regs + DIEPCTL0),
3317 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02003318
3319 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01003320 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
3321 if (!is_usb_reset)
3322 val |= DCTL_SFTDISCON;
3323 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003324
3325 /* must be at-least 3ms to allow bus to see disconnect */
3326 mdelay(3);
3327
Gregory Herrero065d3932015-09-22 15:16:54 +02003328 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003329}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02003330
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003331static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003332{
3333 /* set the soft-disconnect bit */
3334 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
3335}
3336
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003337void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02003338{
Lukasz Majewski308d7342012-05-04 14:17:05 +02003339 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003340 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02003341}
3342
3343/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003344 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
3345 * @hsotg: The device state:
3346 *
3347 * This interrupt indicates one of the following conditions occurred while
3348 * transmitting an ISOC transaction.
3349 * - Corrupted IN Token for ISOC EP.
3350 * - Packet not complete in FIFO.
3351 *
3352 * The following actions will be taken:
3353 * - Determine the EP
3354 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
3355 */
3356static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
3357{
3358 struct dwc2_hsotg_ep *hs_ep;
3359 u32 epctrl;
3360 u32 idx;
3361
3362 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
3363
3364 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3365 hs_ep = hsotg->eps_in[idx];
3366 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
3367 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3368 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3369 epctrl |= DXEPCTL_SNAK;
3370 epctrl |= DXEPCTL_EPDIS;
3371 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
3372 }
3373 }
3374
3375 /* Clear interrupt */
3376 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
3377}
3378
3379/**
3380 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
3381 * @hsotg: The device state:
3382 *
3383 * This interrupt indicates one of the following conditions occurred while
3384 * transmitting an ISOC transaction.
3385 * - Corrupted OUT Token for ISOC EP.
3386 * - Packet not complete in FIFO.
3387 *
3388 * The following actions will be taken:
3389 * - Determine the EP
3390 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
3391 */
3392static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
3393{
3394 u32 gintsts;
3395 u32 gintmsk;
3396 u32 epctrl;
3397 struct dwc2_hsotg_ep *hs_ep;
3398 int idx;
3399
3400 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
3401
3402 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3403 hs_ep = hsotg->eps_out[idx];
3404 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3405 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
3406 dwc2_gadget_target_frame_elapsed(hs_ep)) {
3407 /* Unmask GOUTNAKEFF interrupt */
3408 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3409 gintmsk |= GINTSTS_GOUTNAKEFF;
3410 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3411
3412 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3413 if (!(gintsts & GINTSTS_GOUTNAKEFF))
3414 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3415 }
3416 }
3417
3418 /* Clear interrupt */
3419 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
3420}
3421
3422/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003423 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003424 * @irq: The IRQ number triggered
3425 * @pw: The pw value when registered the handler.
3426 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003427static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003428{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003429 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003430 int retry_count = 8;
3431 u32 gintsts;
3432 u32 gintmsk;
3433
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07003434 if (!dwc2_is_device_mode(hsotg))
3435 return IRQ_NONE;
3436
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003437 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003438irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003439 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
3440 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003441
3442 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
3443 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
3444
3445 gintsts &= gintmsk;
3446
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02003447 if (gintsts & GINTSTS_RESETDET) {
3448 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
3449
3450 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
3451
3452 /* This event must be used only if controller is suspended */
3453 if (hsotg->lx_state == DWC2_L2) {
3454 dwc2_exit_hibernation(hsotg, true);
3455 hsotg->lx_state = DWC2_L0;
3456 }
3457 }
3458
3459 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
3460
3461 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
3462 u32 connected = hsotg->connected;
3463
3464 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
3465 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
3466 dwc2_readl(hsotg->regs + GNPTXSTS));
3467
3468 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
3469
3470 /* Report disconnection if it is not already done. */
3471 dwc2_hsotg_disconnect(hsotg);
3472
3473 if (usb_status & GOTGCTL_BSESVLD && connected)
3474 dwc2_hsotg_core_init_disconnected(hsotg, true);
3475 }
3476
Dinh Nguyen47a16852014-04-14 14:13:34 -07003477 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003478 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003479
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003480 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003481 }
3482
Dinh Nguyen47a16852014-04-14 14:13:34 -07003483 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003484 u32 daint = dwc2_readl(hsotg->regs + DAINT);
3485 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02003486 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003487 int ep;
3488
Robert Baldyga7e804652013-09-19 11:50:20 +02003489 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07003490 daint_out = daint >> DAINT_OUTEP_SHIFT;
3491 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02003492
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003493 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
3494
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003495 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
3496 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003497 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003498 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003499 }
3500
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01003501 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
3502 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003503 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003504 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003505 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003506 }
3507
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003508 /* check both FIFOs */
3509
Dinh Nguyen47a16852014-04-14 14:13:34 -07003510 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003511 dev_dbg(hsotg->dev, "NPTxFEmp\n");
3512
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003513 /*
3514 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003515 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003516 * it needs re-enabling
3517 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003518
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003519 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
3520 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003521 }
3522
Dinh Nguyen47a16852014-04-14 14:13:34 -07003523 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003524 dev_dbg(hsotg->dev, "PTxFEmp\n");
3525
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003526 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003527
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003528 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
3529 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003530 }
3531
Dinh Nguyen47a16852014-04-14 14:13:34 -07003532 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003533 /*
3534 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003535 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003536 * set.
3537 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003538
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003539 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003540 }
3541
Dinh Nguyen47a16852014-04-14 14:13:34 -07003542 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003543 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003544 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003545 }
3546
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003547 /*
3548 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003549 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003550 * the occurrence.
3551 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003552
Dinh Nguyen47a16852014-04-14 14:13:34 -07003553 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003554 u8 idx;
3555 u32 epctrl;
3556 u32 gintmsk;
3557 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003558
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003559 /* Mask this interrupt */
3560 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3561 gintmsk &= ~GINTSTS_GOUTNAKEFF;
3562 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003563
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003564 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
3565 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
3566 hs_ep = hsotg->eps_out[idx];
3567 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
3568
3569 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
3570 epctrl |= DXEPCTL_SNAK;
3571 epctrl |= DXEPCTL_EPDIS;
3572 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
3573 }
3574 }
3575
3576 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003577 }
3578
Dinh Nguyen47a16852014-04-14 14:13:34 -07003579 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003580 dev_info(hsotg->dev, "GINNakEff triggered\n");
3581
Gregory Herrero3be99cd2015-12-07 12:07:31 +01003582 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003583
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003584 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003585 }
3586
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003587 if (gintsts & GINTSTS_INCOMPL_SOIN)
3588 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003589
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003590 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3591 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003592
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003593 /*
3594 * if we've had fifo events, we should try and go around the
3595 * loop again to see if there's any point in returning yet.
3596 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003597
3598 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
3599 goto irq_retry;
3600
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003601 spin_unlock(&hsotg->lock);
3602
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003603 return IRQ_HANDLED;
3604}
3605
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003606static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3607 u32 bit, u32 timeout)
3608{
3609 u32 i;
3610
3611 for (i = 0; i < timeout; i++) {
3612 if (dwc2_readl(hs_otg->regs + reg) & bit)
3613 return 0;
3614 udelay(1);
3615 }
3616
3617 return -ETIMEDOUT;
3618}
3619
3620static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3621 struct dwc2_hsotg_ep *hs_ep)
3622{
3623 u32 epctrl_reg;
3624 u32 epint_reg;
3625
3626 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3627 DOEPCTL(hs_ep->index);
3628 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3629 DOEPINT(hs_ep->index);
3630
3631 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3632 hs_ep->name);
3633
3634 if (hs_ep->dir_in) {
3635 if (hsotg->dedicated_fifos || hs_ep->periodic) {
3636 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3637 /* Wait for Nak effect */
3638 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3639 DXEPINT_INEPNAKEFF, 100))
3640 dev_warn(hsotg->dev,
3641 "%s: timeout DIEPINT.NAKEFF\n",
3642 __func__);
3643 } else {
3644 __orr32(hsotg->regs + DCTL, DCTL_SGNPINNAK);
3645 /* Wait for Nak effect */
3646 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3647 GINTSTS_GINNAKEFF, 100))
3648 dev_warn(hsotg->dev,
3649 "%s: timeout GINTSTS.GINNAKEFF\n",
3650 __func__);
3651 }
3652 } else {
3653 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3654 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
3655
3656 /* Wait for global nak to take effect */
3657 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
3658 GINTSTS_GOUTNAKEFF, 100))
3659 dev_warn(hsotg->dev, "%s: timeout GINTSTS.GOUTNAKEFF\n",
3660 __func__);
3661 }
3662
3663 /* Disable ep */
3664 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3665
3666 /* Wait for ep to be disabled */
3667 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3668 dev_warn(hsotg->dev,
3669 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3670
3671 /* Clear EPDISBLD interrupt */
3672 __orr32(hsotg->regs + epint_reg, DXEPINT_EPDISBLD);
3673
3674 if (hs_ep->dir_in) {
3675 unsigned short fifo_index;
3676
3677 if (hsotg->dedicated_fifos || hs_ep->periodic)
3678 fifo_index = hs_ep->fifo_index;
3679 else
3680 fifo_index = 0;
3681
3682 /* Flush TX FIFO */
3683 dwc2_flush_tx_fifo(hsotg, fifo_index);
3684
3685 /* Clear Global In NP NAK in Shared FIFO for non periodic ep */
3686 if (!hsotg->dedicated_fifos && !hs_ep->periodic)
3687 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
3688
3689 } else {
3690 /* Remove global NAKs */
3691 __orr32(hsotg->regs + DCTL, DCTL_CGOUTNAK);
3692 }
3693}
3694
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003695/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003696 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003697 * @ep: The USB endpint to configure
3698 * @desc: The USB endpoint descriptor to configure with.
3699 *
3700 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003701 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003702static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003703 const struct usb_endpoint_descriptor *desc)
3704{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003705 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003706 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003707 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003708 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003709 u32 epctrl_reg;
3710 u32 epctrl;
3711 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003712 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003713 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003714 unsigned int dir_in;
3715 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003716 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003717
3718 dev_dbg(hsotg->dev,
3719 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3720 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3721 desc->wMaxPacketSize, desc->bInterval);
3722
3723 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003724 if (index == 0) {
3725 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3726 return -EINVAL;
3727 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003728
3729 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3730 if (dir_in != hs_ep->dir_in) {
3731 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3732 return -EINVAL;
3733 }
3734
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003735 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003736 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003737
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003738 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003739
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003740 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003741 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003742
3743 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3744 __func__, epctrl, epctrl_reg);
3745
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003746 /* Allocate DMA descriptor chain for non-ctrl endpoints */
3747 if (using_desc_dma(hsotg)) {
3748 hs_ep->desc_list = dma_alloc_coherent(hsotg->dev,
3749 MAX_DMA_DESC_NUM_GENERIC *
3750 sizeof(struct dwc2_dma_desc),
3751 &hs_ep->desc_list_dma, GFP_KERNEL);
3752 if (!hs_ep->desc_list) {
3753 ret = -ENOMEM;
3754 goto error2;
3755 }
3756 }
3757
Lukasz Majewski22258f42012-06-14 10:02:24 +02003758 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003759
Dinh Nguyen47a16852014-04-14 14:13:34 -07003760 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3761 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003762
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003763 /*
3764 * mark the endpoint as active, otherwise the core may ignore
3765 * transactions entirely for this endpoint
3766 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003767 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003768
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003769 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003770 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003771
3772 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003773 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003774 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003775 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003776 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003777
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003778 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3779 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003780 epctrl |= DXEPCTL_EPTYPE_ISO;
3781 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003782 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003783 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003784 hs_ep->target_frame = TARGET_FRAME_INITIAL;
Vahram Aharonyanab7d2192016-11-14 19:16:36 -08003785 hs_ep->isoc_chain_num = 0;
3786 hs_ep->next_desc = 0;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003787 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003788 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003789 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3790 mask |= DIEPMSK_NAKMSK;
3791 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3792 } else {
3793 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3794 mask |= DOEPMSK_OUTTKNEPDISMSK;
3795 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3796 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003797 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003798
3799 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003800 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003801 break;
3802
3803 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003804 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003805 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003806
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003807 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3808 hs_ep->interval = 1 << (desc->bInterval - 1);
3809
Dinh Nguyen47a16852014-04-14 14:13:34 -07003810 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003811 break;
3812
3813 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003814 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003815 break;
3816 }
3817
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003818 /*
3819 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003820 * a unique tx-fifo even if it is non-periodic.
3821 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003822 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003823 u32 fifo_index = 0;
3824 u32 fifo_size = UINT_MAX;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003825 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003826 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02003827 if (hsotg->fifo_map & (1<<i))
3828 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003829 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Robert Baldygab203d0a2014-09-09 10:44:56 +02003830 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
3831 if (val < size)
3832 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003833 /* Search for smallest acceptable fifo */
3834 if (val < fifo_size) {
3835 fifo_size = val;
3836 fifo_index = i;
3837 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003838 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003839 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003840 dev_err(hsotg->dev,
3841 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303842 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003843 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303844 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003845 hsotg->fifo_map |= 1 << fifo_index;
3846 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3847 hs_ep->fifo_index = fifo_index;
3848 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003849 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003850
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003851 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003852 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003853 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003854
3855 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3856 __func__, epctrl);
3857
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003858 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003859 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003860 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003861
3862 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003863 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003864
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003865error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003866 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003867
3868error2:
3869 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
3870 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3871 sizeof(struct dwc2_dma_desc),
3872 hs_ep->desc_list, hs_ep->desc_list_dma);
3873 hs_ep->desc_list = NULL;
3874 }
3875
Julia Lawall19c190f2010-03-29 17:36:44 +02003876 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003877}
3878
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003879/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003880 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003881 * @ep: The endpoint to disable.
3882 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003883static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003884{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003885 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003886 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003887 int dir_in = hs_ep->dir_in;
3888 int index = hs_ep->index;
3889 unsigned long flags;
3890 u32 epctrl_reg;
3891 u32 ctrl;
3892
Marek Szyprowski1e011292014-09-09 10:44:54 +02003893 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003894
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003895 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003896 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3897 return -EINVAL;
3898 }
3899
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003900 /* Remove DMA memory allocated for non-control Endpoints */
3901 if (using_desc_dma(hsotg)) {
3902 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3903 sizeof(struct dwc2_dma_desc),
3904 hs_ep->desc_list, hs_ep->desc_list_dma);
3905 hs_ep->desc_list = NULL;
3906 }
3907
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003908 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003909
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003910 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003911
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003912 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Vahram Aharonyana4f827712016-11-14 19:16:53 -08003913
3914 if (ctrl & DXEPCTL_EPENA)
3915 dwc2_hsotg_ep_stop_xfr(hsotg, hs_ep);
3916
Dinh Nguyen47a16852014-04-14 14:13:34 -07003917 ctrl &= ~DXEPCTL_EPENA;
3918 ctrl &= ~DXEPCTL_USBACTEP;
3919 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003920
3921 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003922 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003923
3924 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003925 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003926
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003927 /* terminate all requests with shutdown */
3928 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3929
Robert Baldyga1c07b202016-08-29 13:39:00 -07003930 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3931 hs_ep->fifo_index = 0;
3932 hs_ep->fifo_size = 0;
3933
Lukasz Majewski22258f42012-06-14 10:02:24 +02003934 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003935 return 0;
3936}
3937
3938/**
3939 * on_list - check request is on the given endpoint
3940 * @ep: The endpoint to check.
3941 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003942 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003943static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003944{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003945 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003946
3947 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
3948 if (req == test)
3949 return true;
3950 }
3951
3952 return false;
3953}
3954
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003955/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003956 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003957 * @ep: The endpoint to dequeue.
3958 * @req: The request to be removed from a queue.
3959 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003960static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003961{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003962 struct dwc2_hsotg_req *hs_req = our_req(req);
3963 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003964 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003965 unsigned long flags;
3966
Marek Szyprowski1e011292014-09-09 10:44:54 +02003967 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003968
Lukasz Majewski22258f42012-06-14 10:02:24 +02003969 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003970
3971 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02003972 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003973 return -EINVAL;
3974 }
3975
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003976 /* Dequeue already started request */
3977 if (req == &hs_ep->req->req)
3978 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
3979
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003980 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02003981 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003982
3983 return 0;
3984}
3985
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003986/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003987 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003988 * @ep: The endpoint to set halt.
3989 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003990 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
3991 * the endpoint is busy processing requests.
3992 *
3993 * We need to stall the endpoint immediately if request comes from set_feature
3994 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003995 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003996static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003997{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003998 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003999 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004000 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004001 u32 epreg;
4002 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004003 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004004
4005 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
4006
Robert Baldygac9f721b2014-01-14 08:36:00 +01004007 if (index == 0) {
4008 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004009 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01004010 else
4011 dev_warn(hs->dev,
4012 "%s: can't clear halt on ep0\n", __func__);
4013 return 0;
4014 }
4015
Vahram Aharonyan15186f12016-05-23 22:41:59 -07004016 if (hs_ep->isochronous) {
4017 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
4018 return -EINVAL;
4019 }
4020
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004021 if (!now && value && !list_empty(&hs_ep->queue)) {
4022 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
4023 ep->name);
4024 return -EAGAIN;
4025 }
4026
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004027 if (hs_ep->dir_in) {
4028 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004029 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004030
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004031 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05004032 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004033 if (epctl & DXEPCTL_EPENA)
4034 epctl |= DXEPCTL_EPDIS;
4035 } else {
4036 epctl &= ~DXEPCTL_STALL;
4037 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4038 if (xfertype == DXEPCTL_EPTYPE_BULK ||
4039 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
4040 epctl |= DXEPCTL_SETD0PID;
4041 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004042 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004043 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004044
4045 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004046 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004047
4048 if (value)
4049 epctl |= DXEPCTL_STALL;
4050 else {
4051 epctl &= ~DXEPCTL_STALL;
4052 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
4053 if (xfertype == DXEPCTL_EPTYPE_BULK ||
4054 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
4055 epctl |= DXEPCTL_SETD0PID;
4056 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004057 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09004058 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004059
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02004060 hs_ep->halted = value;
4061
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004062 return 0;
4063}
4064
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004065/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004066 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004067 * @ep: The endpoint to set halt.
4068 * @value: Set or unset the halt.
4069 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004070static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004071{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004072 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004073 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004074 unsigned long flags = 0;
4075 int ret = 0;
4076
4077 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07004078 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02004079 spin_unlock_irqrestore(&hs->lock, flags);
4080
4081 return ret;
4082}
4083
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004084static struct usb_ep_ops dwc2_hsotg_ep_ops = {
4085 .enable = dwc2_hsotg_ep_enable,
4086 .disable = dwc2_hsotg_ep_disable,
4087 .alloc_request = dwc2_hsotg_ep_alloc_request,
4088 .free_request = dwc2_hsotg_ep_free_request,
4089 .queue = dwc2_hsotg_ep_queue_lock,
4090 .dequeue = dwc2_hsotg_ep_dequeue,
4091 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03004092 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004093};
4094
4095/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004096 * dwc2_hsotg_init - initalize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004097 * @hsotg: The driver state
4098 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004099static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004100{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004101 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004102 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004103 /* unmask subset of endpoint interrupts */
4104
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004105 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
4106 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
4107 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004108
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004109 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
4110 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
4111 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004112
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004113 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004114
4115 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07004116 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004117
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004118 /* setup fifos */
4119
4120 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004121 dwc2_readl(hsotg->regs + GRXFSIZ),
4122 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004123
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004124 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004125
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004126 /* keep other bits untouched (so e.g. forced modes are not lost) */
4127 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
4128 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
4129 GUSBCFG_HNPCAP);
4130
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004131 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01004132 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01004133 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
4134 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
4135 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004136
Gregory Herrerof5090042015-01-09 13:38:47 +01004137 if (using_dma(hsotg))
4138 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004139}
4140
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004141/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004142 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004143 * @gadget: The usb gadget state
4144 * @driver: The usb gadget driver
4145 *
4146 * Perform initialization to prepare udc device and driver
4147 * to work.
4148 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004149static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004150 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004151{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004152 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004153 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004154 int ret;
4155
4156 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02004157 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004158 return -ENODEV;
4159 }
4160
4161 if (!driver) {
4162 dev_err(hsotg->dev, "%s: no driver\n", __func__);
4163 return -EINVAL;
4164 }
4165
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01004166 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004167 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004168
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02004169 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004170 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
4171 return -EINVAL;
4172 }
4173
4174 WARN_ON(hsotg->driver);
4175
4176 driver->driver.bus = NULL;
4177 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03004178 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004179 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4180
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004181 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
4182 ret = dwc2_lowlevel_hw_enable(hsotg);
4183 if (ret)
4184 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004185 }
4186
Gregory Herrerof6c01592015-01-09 13:38:41 +01004187 if (!IS_ERR_OR_NULL(hsotg->uphy))
4188 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004189
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004190 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07004191 if (dwc2_hw_is_device(hsotg)) {
4192 dwc2_hsotg_init(hsotg);
4193 dwc2_hsotg_core_init_disconnected(hsotg, false);
4194 }
4195
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004196 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004197 spin_unlock_irqrestore(&hsotg->lock, flags);
4198
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004199 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02004200
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004201 return 0;
4202
4203err:
4204 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004205 return ret;
4206}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004207
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004208/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004209 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004210 * @gadget: The usb gadget state
4211 * @driver: The usb gadget driver
4212 *
4213 * Stop udc hw block and stay tunned for future transmissions
4214 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004215static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004216{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004217 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004218 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004219 int ep;
4220
4221 if (!hsotg)
4222 return -ENODEV;
4223
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004224 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004225 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
4226 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004227 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004228 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004229 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004230 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004231
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004232 spin_lock_irqsave(&hsotg->lock, flags);
4233
Marek Szyprowski32805c32014-10-20 12:45:33 +02004234 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004235 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004236 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004237
Lukasz Majewski2b19a522012-06-14 10:02:25 +02004238 spin_unlock_irqrestore(&hsotg->lock, flags);
4239
Gregory Herrerof6c01592015-01-09 13:38:41 +01004240 if (!IS_ERR_OR_NULL(hsotg->uphy))
4241 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02004242
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004243 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4244 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004245
4246 return 0;
4247}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004248
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004249/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004250 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004251 * @gadget: The usb gadget state
4252 *
4253 * Read the {micro} frame number
4254 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004255static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004256{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004257 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004258}
4259
Lukasz Majewskia188b682012-06-22 09:29:56 +02004260/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004261 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02004262 * @gadget: The usb gadget state
4263 * @is_on: Current state of the USB PHY
4264 *
4265 * Connect/Disconnect the USB PHY pullup
4266 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004267static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02004268{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06004269 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004270 unsigned long flags = 0;
4271
Gregory Herrero77ba9112015-09-29 12:08:19 +02004272 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
4273 hsotg->op_state);
4274
4275 /* Don't modify pullup state while in host mode */
4276 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
4277 hsotg->enabled = is_on;
4278 return 0;
4279 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02004280
4281 spin_lock_irqsave(&hsotg->lock, flags);
4282 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004283 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004284 dwc2_hsotg_core_init_disconnected(hsotg, false);
4285 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02004286 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004287 dwc2_hsotg_core_disconnect(hsotg);
4288 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004289 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02004290 }
4291
4292 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4293 spin_unlock_irqrestore(&hsotg->lock, flags);
4294
4295 return 0;
4296}
4297
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004298static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01004299{
4300 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4301 unsigned long flags;
4302
4303 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
4304 spin_lock_irqsave(&hsotg->lock, flags);
4305
Gregory Herrero61f72232015-09-29 12:08:28 +02004306 /*
4307 * If controller is hibernated, it must exit from hibernation
4308 * before being initialized / de-initialized
4309 */
4310 if (hsotg->lx_state == DWC2_L2)
4311 dwc2_exit_hibernation(hsotg, false);
4312
Gregory Herrero83d98222015-01-09 13:39:02 +01004313 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02004314 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02004315
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004316 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01004317 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004318 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004319 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004320 dwc2_hsotg_core_disconnect(hsotg);
4321 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01004322 }
4323
4324 spin_unlock_irqrestore(&hsotg->lock, flags);
4325 return 0;
4326}
4327
Gregory Herrero596d6962015-01-09 13:39:08 +01004328/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004329 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01004330 * @gadget: The usb gadget state
4331 * @mA: Amount of current
4332 *
4333 * Report how much power the device may consume to the phy.
4334 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004335static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01004336{
4337 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
4338
4339 if (IS_ERR_OR_NULL(hsotg->uphy))
4340 return -ENOTSUPP;
4341 return usb_phy_set_power(hsotg->uphy, mA);
4342}
4343
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004344static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
4345 .get_frame = dwc2_hsotg_gadget_getframe,
4346 .udc_start = dwc2_hsotg_udc_start,
4347 .udc_stop = dwc2_hsotg_udc_stop,
4348 .pullup = dwc2_hsotg_pullup,
4349 .vbus_session = dwc2_hsotg_vbus_session,
4350 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004351};
4352
4353/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004354 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004355 * @hsotg: The device state.
4356 * @hs_ep: The endpoint to be initialised.
4357 * @epnum: The endpoint number
4358 *
4359 * Initialise the given endpoint (as part of the probe and device state
4360 * creation) to give to the gadget driver. Setup the endpoint name, any
4361 * direction information and other state that may be required.
4362 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004363static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
4364 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004365 int epnum,
4366 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004367{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004368 char *dir;
4369
4370 if (epnum == 0)
4371 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004372 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004373 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004374 else
4375 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004376
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004377 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004378 hs_ep->index = epnum;
4379
4380 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
4381
4382 INIT_LIST_HEAD(&hs_ep->queue);
4383 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
4384
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004385 /* add to the list of endpoints known by the gadget driver */
4386 if (epnum)
4387 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
4388
4389 hs_ep->parent = hsotg;
4390 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01004391 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004392 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004393
Robert Baldyga29545222015-07-31 16:00:18 +02004394 if (epnum == 0) {
4395 hs_ep->ep.caps.type_control = true;
4396 } else {
4397 hs_ep->ep.caps.type_iso = true;
4398 hs_ep->ep.caps.type_bulk = true;
4399 hs_ep->ep.caps.type_int = true;
4400 }
4401
4402 if (dir_in)
4403 hs_ep->ep.caps.dir_in = true;
4404 else
4405 hs_ep->ep.caps.dir_out = true;
4406
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004407 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004408 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004409 * to be something valid.
4410 */
4411
4412 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07004413 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004414 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004415 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004416 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004417 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004418 }
4419}
4420
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004421/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004422 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004423 * @param: The device state
4424 *
4425 * Read the USB core HW configuration registers
4426 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004427static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004428{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004429 u32 cfg;
4430 u32 ep_type;
4431 u32 i;
4432
Ben Dooks10aebc72010-07-19 09:40:44 +01004433 /* check hardware configuration */
4434
John Youn43e90342015-12-17 11:17:45 -08004435 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
4436
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004437 /* Add ep0 */
4438 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004439
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004440 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004441 GFP_KERNEL);
4442 if (!hsotg->eps_in[0])
4443 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004444 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004445 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004446
John Youn43e90342015-12-17 11:17:45 -08004447 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08004448 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004449 ep_type = cfg & 3;
4450 /* Direction in or both */
4451 if (!(ep_type & 2)) {
4452 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004453 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004454 if (!hsotg->eps_in[i])
4455 return -ENOMEM;
4456 }
4457 /* Direction out or both */
4458 if (!(ep_type & 1)) {
4459 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004460 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004461 if (!hsotg->eps_out[i])
4462 return -ENOMEM;
4463 }
4464 }
4465
John Youn43e90342015-12-17 11:17:45 -08004466 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
4467 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01004468
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02004469 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
4470 hsotg->num_of_eps,
4471 hsotg->dedicated_fifos ? "dedicated" : "shared",
4472 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004473 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004474}
4475
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004476/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004477 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004478 * @param: The device state
4479 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004480static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004481{
Mark Brown83a01802011-06-01 17:16:15 +01004482#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004483 struct device *dev = hsotg->dev;
4484 void __iomem *regs = hsotg->regs;
4485 u32 val;
4486 int idx;
4487
4488 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004489 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
4490 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004491
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01004492 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004493 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004494
4495 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004496 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004497
4498 /* show periodic fifo settings */
4499
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004500 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004501 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004502 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07004503 val >> FIFOSIZE_DEPTH_SHIFT,
4504 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004505 }
4506
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01004507 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004508 dev_info(dev,
4509 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004510 dwc2_readl(regs + DIEPCTL(idx)),
4511 dwc2_readl(regs + DIEPTSIZ(idx)),
4512 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004513
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004514 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004515 dev_info(dev,
4516 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004517 idx, dwc2_readl(regs + DOEPCTL(idx)),
4518 dwc2_readl(regs + DOEPTSIZ(idx)),
4519 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004520
4521 }
4522
4523 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004524 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01004525#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004526}
4527
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004528/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06004529 * dwc2_gadget_init - init function for gadget
4530 * @dwc2: The data structure for the DWC2 driver.
4531 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004532 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06004533int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004534{
Dinh Nguyen117777b2014-11-11 11:13:34 -06004535 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004536 int epnum;
4537 int ret;
John Youn43e90342015-12-17 11:17:45 -08004538
Gregory Herrero0a176272015-01-09 13:38:52 +01004539 /* Dump fifo information */
4540 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07004541 hsotg->params.g_np_tx_fifo_size);
4542 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004543
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01004544 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004545 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004546 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02004547 if (hsotg->dr_mode == USB_DR_MODE_OTG)
4548 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02004549 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
4550 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004551
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004552 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004553 if (ret) {
4554 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004555 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004556 }
4557
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004558 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
4559 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004560 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004561 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004562
4563 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
4564 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02004565 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004566 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01004567
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08004568 if (using_desc_dma(hsotg)) {
4569 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
4570 if (ret < 0)
4571 return ret;
4572 }
4573
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004574 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004575 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004576 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06004577 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004578 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02004579 }
4580
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004581 /* hsotg->num_of_eps holds number of EPs other than ep0 */
4582
4583 if (hsotg->num_of_eps == 0) {
4584 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004585 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004586 }
4587
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004588 /* setup endpoint information */
4589
4590 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004591 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004592
4593 /* allocate EP0 request */
4594
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004595 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004596 GFP_KERNEL);
4597 if (!hsotg->ctrl_req) {
4598 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004599 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02004600 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004601
4602 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004603 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4604 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004605 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004606 epnum, 1);
4607 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004608 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004609 epnum, 0);
4610 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004611
Dinh Nguyen117777b2014-11-11 11:13:34 -06004612 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004613 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004614 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004615
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004616 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004617
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004618 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004619}
4620
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004621/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004622 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004623 * @pdev: The platform information for the driver
4624 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004625int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004626{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004627 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004628
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004629 return 0;
4630}
4631
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004632int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004633{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004634 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004635
Gregory Herrero9e779772015-04-29 22:09:07 +02004636 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004637 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004638
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004639 if (hsotg->driver) {
4640 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004641
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004642 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4643 hsotg->driver->driver.name);
4644
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004645 spin_lock_irqsave(&hsotg->lock, flags);
4646 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004647 dwc2_hsotg_core_disconnect(hsotg);
4648 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004649 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4650 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004651
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004652 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4653 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004654 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004655 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004656 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004657 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004658 }
4659
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004660 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004661}
4662
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004663int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004664{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004665 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004666
Gregory Herrero9e779772015-04-29 22:09:07 +02004667 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004668 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004669
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004670 if (hsotg->driver) {
4671 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4672 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004673
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004674 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004675 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004676 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004677 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004678 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004679 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004680
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004681 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004682}
John Youn58e52ff6a2016-02-23 19:54:57 -08004683
4684/**
4685 * dwc2_backup_device_registers() - Backup controller device registers.
4686 * When suspending usb bus, registers needs to be backuped
4687 * if controller power is disabled once suspended.
4688 *
4689 * @hsotg: Programming view of the DWC_otg controller
4690 */
4691int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4692{
4693 struct dwc2_dregs_backup *dr;
4694 int i;
4695
4696 dev_dbg(hsotg->dev, "%s\n", __func__);
4697
4698 /* Backup dev regs */
4699 dr = &hsotg->dr_backup;
4700
4701 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4702 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4703 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4704 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4705 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4706
4707 for (i = 0; i < hsotg->num_of_eps; i++) {
4708 /* Backup IN EPs */
4709 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4710
4711 /* Ensure DATA PID is correctly configured */
4712 if (dr->diepctl[i] & DXEPCTL_DPID)
4713 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4714 else
4715 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4716
4717 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4718 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4719
4720 /* Backup OUT EPs */
4721 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4722
4723 /* Ensure DATA PID is correctly configured */
4724 if (dr->doepctl[i] & DXEPCTL_DPID)
4725 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4726 else
4727 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4728
4729 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4730 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4731 }
4732 dr->valid = true;
4733 return 0;
4734}
4735
4736/**
4737 * dwc2_restore_device_registers() - Restore controller device registers.
4738 * When resuming usb bus, device registers needs to be restored
4739 * if controller power were disabled.
4740 *
4741 * @hsotg: Programming view of the DWC_otg controller
4742 */
4743int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4744{
4745 struct dwc2_dregs_backup *dr;
4746 u32 dctl;
4747 int i;
4748
4749 dev_dbg(hsotg->dev, "%s\n", __func__);
4750
4751 /* Restore dev regs */
4752 dr = &hsotg->dr_backup;
4753 if (!dr->valid) {
4754 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4755 __func__);
4756 return -EINVAL;
4757 }
4758 dr->valid = false;
4759
4760 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4761 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4762 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4763 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4764 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4765
4766 for (i = 0; i < hsotg->num_of_eps; i++) {
4767 /* Restore IN EPs */
4768 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4769 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4770 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4771
4772 /* Restore OUT EPs */
4773 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4774 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4775 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4776 }
4777
4778 /* Set the Power-On Programming done bit */
4779 dctl = dwc2_readl(hsotg->regs + DCTL);
4780 dctl |= DCTL_PWRONPRGDONE;
4781 dwc2_writel(dctl, hsotg->regs + DCTL);
4782
4783 return 0;
4784}