blob: 3264375231f48c99739736af67052ae60a36b5d3 [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
630/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500631 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100632 * @hsotg: The controller state.
633 * @hs_ep: The endpoint to process a request for
634 * @hs_req: The request to start.
635 * @continuing: True if we are doing more for the current request.
636 *
637 * Start the given request running by setting the endpoint registers
638 * appropriately, and writing any data to the FIFOs.
639 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500640static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
641 struct dwc2_hsotg_ep *hs_ep,
642 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100643 bool continuing)
644{
645 struct usb_request *ureq = &hs_req->req;
646 int index = hs_ep->index;
647 int dir_in = hs_ep->dir_in;
648 u32 epctrl_reg;
649 u32 epsize_reg;
650 u32 epsize;
651 u32 ctrl;
652 unsigned length;
653 unsigned packets;
654 unsigned maxreq;
655
656 if (index != 0) {
657 if (hs_ep->req && !continuing) {
658 dev_err(hsotg->dev, "%s: active request\n", __func__);
659 WARN_ON(1);
660 return;
661 } else if (hs_ep->req != hs_req && continuing) {
662 dev_err(hsotg->dev,
663 "%s: continue different req\n", __func__);
664 WARN_ON(1);
665 return;
666 }
667 }
668
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200669 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
670 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100671
672 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300673 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100674 hs_ep->dir_in ? "in" : "out");
675
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900676 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300677 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900678
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200679 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900680 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
681 return;
682 }
683
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100684 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200685 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
686 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100687
Vahram Aharonyancf77b5f2016-11-09 19:28:01 -0800688 if (!using_desc_dma(hsotg))
689 maxreq = get_ep_limit(hs_ep);
690 else
691 maxreq = dwc2_gadget_get_chain_limit(hs_ep);
692
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100693 if (length > maxreq) {
694 int round = maxreq % hs_ep->ep.maxpacket;
695
696 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
697 __func__, length, maxreq, round);
698
699 /* round down to multiple of packets */
700 if (round)
701 maxreq -= round;
702
703 length = maxreq;
704 }
705
706 if (length)
707 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
708 else
709 packets = 1; /* send one packet if length is zero. */
710
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200711 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
712 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
713 return;
714 }
715
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100716 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200717 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700718 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200719 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700720 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100721 else
722 epsize = 0;
723
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100724 /*
725 * zero length packet should be programmed on its own and should not
726 * be counted in DIEPTSIZ.PktCnt with other packets.
727 */
728 if (dir_in && ureq->zero && !continuing) {
729 /* Test if zlp is actually required. */
730 if ((ureq->length >= hs_ep->ep.maxpacket) &&
731 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +0100732 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100733 }
734
Dinh Nguyen47a16852014-04-14 14:13:34 -0700735 epsize |= DXEPTSIZ_PKTCNT(packets);
736 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100737
738 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
739 __func__, packets, length, ureq->length, epsize, epsize_reg);
740
741 /* store the request as the current one we're doing */
742 hs_ep->req = hs_req;
743
744 /* write size / packets */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300745 dwc2_writel(epsize, hsotg->regs + epsize_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100746
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900747 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100748 unsigned int dma_reg;
749
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200750 /*
751 * write DMA address to control register, buffer already
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500752 * synced by dwc2_hsotg_ep_queue().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200753 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100754
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200755 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300756 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100757
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300758 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900759 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100760 }
761
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700762 if (hs_ep->isochronous && hs_ep->interval == 1) {
763 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
764 dwc2_gadget_incr_frame_num(hs_ep);
765
766 if (hs_ep->target_frame & 0x1)
767 ctrl |= DXEPCTL_SETODDFR;
768 else
769 ctrl |= DXEPCTL_SETEVENFR;
770 }
771
Dinh Nguyen47a16852014-04-14 14:13:34 -0700772 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200773
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100774 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +0200775
776 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100777 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -0700778 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200779
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100780 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300781 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100782
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200783 /*
784 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100785 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200786 * this information.
787 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100788 hs_ep->size_loaded = length;
789 hs_ep->last_load = ureq->actual;
790
791 if (dir_in && !using_dma(hsotg)) {
792 /* set these anyway, we may need them for non-periodic in */
793 hs_ep->fifo_load = 0;
794
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500795 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100796 }
797
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200798 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200799 * Note, trying to clear the NAK here causes problems with transmit
800 * on the S3C6400 ending up with the TXFIFO becoming full.
801 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100802
803 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300804 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +0100805 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700806 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300807 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100808
Dinh Nguyen47a16852014-04-14 14:13:34 -0700809 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300810 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200811
812 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500813 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100814}
815
816/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500817 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100818 * @hsotg: The device state.
819 * @hs_ep: The endpoint the request is on.
820 * @req: The request being processed.
821 *
822 * We've been asked to queue a request, so ensure that the memory buffer
823 * is correctly setup for DMA. If we've been passed an extant DMA address
824 * then ensure the buffer has been synced to memory. If our buffer has no
825 * DMA memory, then we map the memory and mark our request to allow us to
826 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200827 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500828static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
829 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100830 struct usb_request *req)
831{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500832 struct dwc2_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200833 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100834
835 /* if the length is zero, ignore the DMA data */
836 if (hs_req->req.length == 0)
837 return 0;
838
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200839 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
840 if (ret)
841 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100842
843 return 0;
844
845dma_error:
846 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
847 __func__, req->buf, req->length);
848
849 return -EIO;
850}
851
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500852static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
853 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100854{
855 void *req_buf = hs_req->req.buf;
856
857 /* If dma is not being used or buffer is aligned */
858 if (!using_dma(hsotg) || !((long)req_buf & 3))
859 return 0;
860
861 WARN_ON(hs_req->saved_req_buf);
862
863 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
864 hs_ep->ep.name, req_buf, hs_req->req.length);
865
866 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
867 if (!hs_req->req.buf) {
868 hs_req->req.buf = req_buf;
869 dev_err(hsotg->dev,
870 "%s: unable to allocate memory for bounce buffer\n",
871 __func__);
872 return -ENOMEM;
873 }
874
875 /* Save actual buffer */
876 hs_req->saved_req_buf = req_buf;
877
878 if (hs_ep->dir_in)
879 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
880 return 0;
881}
882
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500883static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
884 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100885{
886 /* If dma is not being used or buffer was aligned */
887 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
888 return;
889
890 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
891 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
892
893 /* Copy data from bounce buffer on successful out transfer */
894 if (!hs_ep->dir_in && !hs_req->req.status)
895 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
896 hs_req->req.actual);
897
898 /* Free bounce buffer */
899 kfree(hs_req->req.buf);
900
901 hs_req->req.buf = hs_req->saved_req_buf;
902 hs_req->saved_req_buf = NULL;
903}
904
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700905/**
906 * dwc2_gadget_target_frame_elapsed - Checks target frame
907 * @hs_ep: The driver endpoint to check
908 *
909 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
910 * corresponding transfer.
911 */
912static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
913{
914 struct dwc2_hsotg *hsotg = hs_ep->parent;
915 u32 target_frame = hs_ep->target_frame;
916 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
917 bool frame_overrun = hs_ep->frame_overrun;
918
919 if (!frame_overrun && current_frame >= target_frame)
920 return true;
921
922 if (frame_overrun && current_frame >= target_frame &&
923 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
924 return true;
925
926 return false;
927}
928
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500929static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100930 gfp_t gfp_flags)
931{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500932 struct dwc2_hsotg_req *hs_req = our_req(req);
933 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600934 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100935 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100936 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100937
938 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
939 ep->name, req, req->length, req->buf, req->no_interrupt,
940 req->zero, req->short_not_ok);
941
Gregory Herrero7ababa92015-04-29 22:09:08 +0200942 /* Prevent new request submission when controller is suspended */
943 if (hs->lx_state == DWC2_L2) {
944 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
945 __func__);
946 return -EAGAIN;
947 }
948
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100949 /* initialise status of the request */
950 INIT_LIST_HEAD(&hs_req->queue);
951 req->actual = 0;
952 req->status = -EINPROGRESS;
953
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500954 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100955 if (ret)
956 return ret;
957
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100958 /* if we're using DMA, sync the buffers as necessary */
959 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500960 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100961 if (ret)
962 return ret;
963 }
964
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100965 first = list_empty(&hs_ep->queue);
966 list_add_tail(&hs_req->queue, &hs_ep->queue);
967
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700968 if (first) {
969 if (!hs_ep->isochronous) {
970 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
971 return 0;
972 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100973
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700974 while (dwc2_gadget_target_frame_elapsed(hs_ep))
975 dwc2_gadget_incr_frame_num(hs_ep);
976
977 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
978 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
979 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100980 return 0;
981}
982
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500983static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200984 gfp_t gfp_flags)
985{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500986 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600987 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200988 unsigned long flags = 0;
989 int ret = 0;
990
991 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500992 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200993 spin_unlock_irqrestore(&hs->lock, flags);
994
995 return ret;
996}
997
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500998static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100999 struct usb_request *req)
1000{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001001 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001002
1003 kfree(hs_req);
1004}
1005
1006/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001007 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001008 * @ep: The endpoint the request was on.
1009 * @req: The request completed.
1010 *
1011 * Called on completion of any requests the driver itself
1012 * submitted that need cleaning up.
1013 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001014static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001015 struct usb_request *req)
1016{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001017 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001018 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001019
1020 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
1021
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001022 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001023}
1024
1025/**
1026 * ep_from_windex - convert control wIndex value to endpoint
1027 * @hsotg: The driver state.
1028 * @windex: The control request wIndex field (in host order).
1029 *
1030 * Convert the given wIndex into a pointer to an driver endpoint
1031 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001032 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001033static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001034 u32 windex)
1035{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001036 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001037 int dir = (windex & USB_DIR_IN) ? 1 : 0;
1038 int idx = windex & 0x7F;
1039
1040 if (windex >= 0x100)
1041 return NULL;
1042
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02001043 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001044 return NULL;
1045
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001046 ep = index_to_ep(hsotg, idx, dir);
1047
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001048 if (idx && ep->dir_in != dir)
1049 return NULL;
1050
1051 return ep;
1052}
1053
1054/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001055 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001056 * @hsotg: The driver state.
1057 * @testmode: requested usb test mode
1058 * Enable usb Test Mode requested by the Host.
1059 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001060int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001061{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001062 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001063
1064 dctl &= ~DCTL_TSTCTL_MASK;
1065 switch (testmode) {
1066 case TEST_J:
1067 case TEST_K:
1068 case TEST_SE0_NAK:
1069 case TEST_PACKET:
1070 case TEST_FORCE_EN:
1071 dctl |= testmode << DCTL_TSTCTL_SHIFT;
1072 break;
1073 default:
1074 return -EINVAL;
1075 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001076 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001077 return 0;
1078}
1079
1080/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001081 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001082 * @hsotg: The device state
1083 * @ep: Endpoint 0
1084 * @buff: Buffer for request
1085 * @length: Length of reply.
1086 *
1087 * Create a request and queue it on the given endpoint. This is useful as
1088 * an internal method of sending replies to certain control requests, etc.
1089 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001090static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
1091 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001092 void *buff,
1093 int length)
1094{
1095 struct usb_request *req;
1096 int ret;
1097
1098 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1099
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001100 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001101 hsotg->ep0_reply = req;
1102 if (!req) {
1103 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1104 return -ENOMEM;
1105 }
1106
1107 req->buf = hsotg->ep0_buff;
1108 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001109 /*
1110 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1111 * STATUS stage.
1112 */
1113 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001114 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001115
1116 if (length)
1117 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001118
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001119 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001120 if (ret) {
1121 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1122 return ret;
1123 }
1124
1125 return 0;
1126}
1127
1128/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001129 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001130 * @hsotg: The device state
1131 * @ctrl: USB control request
1132 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001133static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001134 struct usb_ctrlrequest *ctrl)
1135{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001136 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1137 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001138 __le16 reply;
1139 int ret;
1140
1141 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1142
1143 if (!ep0->dir_in) {
1144 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1145 return -EINVAL;
1146 }
1147
1148 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1149 case USB_RECIP_DEVICE:
1150 reply = cpu_to_le16(0); /* bit 0 => self powered,
1151 * bit 1 => remote wakeup */
1152 break;
1153
1154 case USB_RECIP_INTERFACE:
1155 /* currently, the data result should be zero */
1156 reply = cpu_to_le16(0);
1157 break;
1158
1159 case USB_RECIP_ENDPOINT:
1160 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1161 if (!ep)
1162 return -ENOENT;
1163
1164 reply = cpu_to_le16(ep->halted ? 1 : 0);
1165 break;
1166
1167 default:
1168 return 0;
1169 }
1170
1171 if (le16_to_cpu(ctrl->wLength) != 2)
1172 return -EINVAL;
1173
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001174 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001175 if (ret) {
1176 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1177 return ret;
1178 }
1179
1180 return 1;
1181}
1182
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001183static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001184
1185/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001186 * get_ep_head - return the first request on the endpoint
1187 * @hs_ep: The controller endpoint to get
1188 *
1189 * Get the first request on the endpoint.
1190 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001191static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001192{
Masahiro Yamadaffc4b402016-09-19 01:03:13 +09001193 return list_first_entry_or_null(&hs_ep->queue, struct dwc2_hsotg_req,
1194 queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001195}
1196
1197/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001198 * dwc2_gadget_start_next_request - Starts next request from ep queue
1199 * @hs_ep: Endpoint structure
1200 *
1201 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1202 * in its handler. Hence we need to unmask it here to be able to do
1203 * resynchronization.
1204 */
1205static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1206{
1207 u32 mask;
1208 struct dwc2_hsotg *hsotg = hs_ep->parent;
1209 int dir_in = hs_ep->dir_in;
1210 struct dwc2_hsotg_req *hs_req;
1211 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1212
1213 if (!list_empty(&hs_ep->queue)) {
1214 hs_req = get_ep_head(hs_ep);
1215 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1216 return;
1217 }
1218 if (!hs_ep->isochronous)
1219 return;
1220
1221 if (dir_in) {
1222 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1223 __func__);
1224 } else {
1225 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1226 __func__);
1227 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1228 mask |= DOEPMSK_OUTTKNEPDISMSK;
1229 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1230 }
1231}
1232
1233/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001234 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001235 * @hsotg: The device state
1236 * @ctrl: USB control request
1237 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001238static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001239 struct usb_ctrlrequest *ctrl)
1240{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001241 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1242 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001243 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001244 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001245 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001246 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001247 u32 recip;
1248 u32 wValue;
1249 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001250
1251 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1252 __func__, set ? "SET" : "CLEAR");
1253
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001254 wValue = le16_to_cpu(ctrl->wValue);
1255 wIndex = le16_to_cpu(ctrl->wIndex);
1256 recip = ctrl->bRequestType & USB_RECIP_MASK;
1257
1258 switch (recip) {
1259 case USB_RECIP_DEVICE:
1260 switch (wValue) {
1261 case USB_DEVICE_TEST_MODE:
1262 if ((wIndex & 0xff) != 0)
1263 return -EINVAL;
1264 if (!set)
1265 return -EINVAL;
1266
1267 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001268 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001269 if (ret) {
1270 dev_err(hsotg->dev,
1271 "%s: failed to send reply\n", __func__);
1272 return ret;
1273 }
1274 break;
1275 default:
1276 return -ENOENT;
1277 }
1278 break;
1279
1280 case USB_RECIP_ENDPOINT:
1281 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001282 if (!ep) {
1283 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001284 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001285 return -ENOENT;
1286 }
1287
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001288 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001289 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001290 halted = ep->halted;
1291
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001292 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001293
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001294 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001295 if (ret) {
1296 dev_err(hsotg->dev,
1297 "%s: failed to send reply\n", __func__);
1298 return ret;
1299 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001300
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001301 /*
1302 * we have to complete all requests for ep if it was
1303 * halted, and the halt was cleared by CLEAR_FEATURE
1304 */
1305
1306 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001307 /*
1308 * If we have request in progress,
1309 * then complete it
1310 */
1311 if (ep->req) {
1312 hs_req = ep->req;
1313 ep->req = NULL;
1314 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001315 if (hs_req->req.complete) {
1316 spin_unlock(&hsotg->lock);
1317 usb_gadget_giveback_request(
1318 &ep->ep, &hs_req->req);
1319 spin_lock(&hsotg->lock);
1320 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001321 }
1322
1323 /* If we have pending request, then start it */
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001324 if (!ep->req) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001325 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001326 }
1327 }
1328
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001329 break;
1330
1331 default:
1332 return -ENOENT;
1333 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001334 break;
1335 default:
1336 return -ENOENT;
1337 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001338 return 1;
1339}
1340
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001341static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001342
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001343/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001344 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001345 * @hsotg: The device state
1346 *
1347 * Set stall for ep0 as response for setup request.
1348 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001349static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001350{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001351 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001352 u32 reg;
1353 u32 ctrl;
1354
1355 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1356 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1357
1358 /*
1359 * DxEPCTL_Stall will be cleared by EP once it has
1360 * taken effect, so no need to clear later.
1361 */
1362
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001363 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001364 ctrl |= DXEPCTL_STALL;
1365 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001366 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001367
1368 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001369 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001370 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001371
1372 /*
1373 * complete won't be called, so we enqueue
1374 * setup request here
1375 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001376 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001377}
1378
1379/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001380 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001381 * @hsotg: The device state
1382 * @ctrl: The control request received
1383 *
1384 * The controller has received the SETUP phase of a control request, and
1385 * needs to work out what to do next (and whether to pass it on to the
1386 * gadget driver).
1387 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001388static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001389 struct usb_ctrlrequest *ctrl)
1390{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001391 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001392 int ret = 0;
1393 u32 dcfg;
1394
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001395 dev_dbg(hsotg->dev,
1396 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1397 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1398 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001399
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001400 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001401 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001402 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1403 } else if (ctrl->bRequestType & USB_DIR_IN) {
1404 ep0->dir_in = 1;
1405 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1406 } else {
1407 ep0->dir_in = 0;
1408 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1409 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001410
1411 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1412 switch (ctrl->bRequest) {
1413 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001414 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001415 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001416 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001417 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1418 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001419 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001420
1421 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1422
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001423 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001424 return;
1425
1426 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001427 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001428 break;
1429
1430 case USB_REQ_CLEAR_FEATURE:
1431 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001432 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001433 break;
1434 }
1435 }
1436
1437 /* as a fallback, try delivering it to the driver to deal with */
1438
1439 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001440 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001441 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001442 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001443 if (ret < 0)
1444 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1445 }
1446
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001447 /*
1448 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001449 * so respond with a STALL for the status stage to indicate failure.
1450 */
1451
Robert Baldygac9f721b2014-01-14 08:36:00 +01001452 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001453 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001454}
1455
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001456/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001457 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001458 * @ep: The endpoint the request was on.
1459 * @req: The request completed.
1460 *
1461 * Called on completion of any requests the driver itself submitted for
1462 * EP0 setup packets
1463 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001464static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001465 struct usb_request *req)
1466{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001467 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001468 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001469
1470 if (req->status < 0) {
1471 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1472 return;
1473 }
1474
Robert Baldyga93f599f2013-11-21 13:49:17 +01001475 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001476 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001477 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001478 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001479 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001480 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001481}
1482
1483/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001484 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001485 * @hsotg: The device state.
1486 *
1487 * Enqueue a request on EP0 if necessary to received any SETUP packets
1488 * received from the host.
1489 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001490static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001491{
1492 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001493 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001494 int ret;
1495
1496 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1497
1498 req->zero = 0;
1499 req->length = 8;
1500 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001501 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001502
1503 if (!list_empty(&hs_req->queue)) {
1504 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1505 return;
1506 }
1507
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001508 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001509 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001510 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001511
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001512 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001513 if (ret < 0) {
1514 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001515 /*
1516 * Don't think there's much we can do other than watch the
1517 * driver fail.
1518 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001519 }
1520}
1521
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001522static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1523 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001524{
1525 u32 ctrl;
1526 u8 index = hs_ep->index;
1527 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1528 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1529
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001530 if (hs_ep->dir_in)
1531 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
1532 index);
1533 else
1534 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
1535 index);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001536
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001537 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1538 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1539 epsiz_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001540
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001541 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001542 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1543 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1544 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001545 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001546}
1547
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001548/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001549 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001550 * @hsotg: The device state.
1551 * @hs_ep: The endpoint the request was on.
1552 * @hs_req: The request to complete.
1553 * @result: The result code (0 => Ok, otherwise errno)
1554 *
1555 * The given request has finished, so call the necessary completion
1556 * if it has one and then look to see if we can start a new request
1557 * on the endpoint.
1558 *
1559 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001560 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001561static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1562 struct dwc2_hsotg_ep *hs_ep,
1563 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001564 int result)
1565{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001566
1567 if (!hs_req) {
1568 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1569 return;
1570 }
1571
1572 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1573 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1574
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001575 /*
1576 * only replace the status if we've not already set an error
1577 * from a previous transaction
1578 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001579
1580 if (hs_req->req.status == -EINPROGRESS)
1581 hs_req->req.status = result;
1582
Yunzhi Li44583fe2015-09-29 12:25:01 +02001583 if (using_dma(hsotg))
1584 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1585
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001586 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001587
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001588 hs_ep->req = NULL;
1589 list_del_init(&hs_req->queue);
1590
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001591 /*
1592 * call the complete request with the locks off, just in case the
1593 * request tries to queue more work for this endpoint.
1594 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001595
1596 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001597 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001598 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001599 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001600 }
1601
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001602 /*
1603 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001604 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001605 * so be careful when doing this.
1606 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001607
1608 if (!hs_ep->req && result >= 0) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001609 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001610 }
1611}
1612
1613/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001614 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001615 * @hsotg: The device state.
1616 * @ep_idx: The endpoint index for the data
1617 * @size: The size of data in the fifo, in bytes
1618 *
1619 * The FIFO status shows there is data to read from the FIFO for a given
1620 * endpoint, so sort out whether we need to read the data into a request
1621 * that has been made for that endpoint.
1622 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001623static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001624{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001625 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
1626 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001627 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001628 int to_read;
1629 int max_req;
1630 int read_ptr;
1631
Lukasz Majewski22258f42012-06-14 10:02:24 +02001632
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001633 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001634 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001635 int ptr;
1636
Robert Baldyga6b448af42014-12-16 11:51:44 +01001637 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001638 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001639 __func__, size, ep_idx, epctl);
1640
1641 /* dump the data from the FIFO, we've nothing we can do */
1642 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001643 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001644
1645 return;
1646 }
1647
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001648 to_read = size;
1649 read_ptr = hs_req->req.actual;
1650 max_req = hs_req->req.length - read_ptr;
1651
Ben Dooksa33e7132010-07-19 09:40:49 +01001652 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1653 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1654
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001655 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001656 /*
1657 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001658 * to deal with in this request.
1659 */
1660
1661 /* currently we don't deal this */
1662 WARN_ON_ONCE(1);
1663 }
1664
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001665 hs_ep->total_data += to_read;
1666 hs_req->req.actual += to_read;
1667 to_read = DIV_ROUND_UP(to_read, 4);
1668
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001669 /*
1670 * note, we might over-write the buffer end by 3 bytes depending on
1671 * alignment of the data.
1672 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001673 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001674}
1675
1676/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001677 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001678 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001679 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001680 *
1681 * Generate a zero-length IN packet request for terminating a SETUP
1682 * transaction.
1683 *
1684 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001685 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001686 * the TxFIFO.
1687 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001688static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001689{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001690 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001691 hsotg->eps_out[0]->dir_in = dir_in;
1692 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001693
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001694 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001695}
1696
Roman Bacikec1f9d92015-09-10 18:13:43 -07001697static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
1698 u32 epctl_reg)
1699{
1700 u32 ctrl;
1701
1702 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
1703 if (ctrl & DXEPCTL_EOFRNUM)
1704 ctrl |= DXEPCTL_SETEVENFR;
1705 else
1706 ctrl |= DXEPCTL_SETODDFR;
1707 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
1708}
1709
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001710/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001711 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001712 * @hsotg: The device instance
1713 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001714 *
1715 * The RXFIFO has delivered an OutDone event, which means that the data
1716 * transfer for an OUT endpoint has been completed, either by a short
1717 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001718 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001719static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001720{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001721 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001722 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
1723 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001724 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001725 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001726 int result = 0;
1727
1728 if (!hs_req) {
1729 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1730 return;
1731 }
1732
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001733 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1734 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001735 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1736 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001737 return;
1738 }
1739
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001740 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001741 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001742
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001743 /*
1744 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001745 * is left in the endpoint size register and then working it
1746 * out from the amount we loaded for the transfer.
1747 *
1748 * We need to do this as DMA pointers are always 32bit aligned
1749 * so may overshoot/undershoot the transfer.
1750 */
1751
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001752 size_done = hs_ep->size_loaded - size_left;
1753 size_done += hs_ep->last_load;
1754
1755 req->actual = size_done;
1756 }
1757
Ben Dooksa33e7132010-07-19 09:40:49 +01001758 /* if there is more request to do, schedule new transfer */
1759 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001760 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01001761 return;
1762 }
1763
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001764 if (req->actual < req->length && req->short_not_ok) {
1765 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1766 __func__, req->actual, req->length);
1767
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001768 /*
1769 * todo - what should we return here? there's no one else
1770 * even bothering to check the status.
1771 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001772 }
1773
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001774 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1775 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001776 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001777 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001778 }
1779
Roman Bacikec1f9d92015-09-10 18:13:43 -07001780 /*
1781 * Slave mode OUT transfers do not go through XferComplete so
1782 * adjust the ISOC parity here.
1783 */
1784 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07001785 if (hs_ep->isochronous && hs_ep->interval == 1)
1786 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001787 else if (hs_ep->isochronous && hs_ep->interval > 1)
1788 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07001789 }
1790
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001791 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001792}
1793
1794/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001795 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001796 * @hsotg: The device instance
1797 *
1798 * The IRQ handler has detected that the RX FIFO has some data in it
1799 * that requires processing, so find out what is in there and do the
1800 * appropriate read.
1801 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001802 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001803 * chunks, so if you have x packets received on an endpoint you'll get x
1804 * FIFO events delivered, each with a packet's worth of data in it.
1805 *
1806 * When using DMA, we should not be processing events from the RXFIFO
1807 * as the actual data should be sent to the memory directly and we turn
1808 * on the completion interrupts to get notifications of transfer completion.
1809 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001810static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001811{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001812 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001813 u32 epnum, status, size;
1814
1815 WARN_ON(using_dma(hsotg));
1816
Dinh Nguyen47a16852014-04-14 14:13:34 -07001817 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1818 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001819
Dinh Nguyen47a16852014-04-14 14:13:34 -07001820 size = grxstsr & GRXSTS_BYTECNT_MASK;
1821 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001822
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01001823 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001824 __func__, grxstsr, size, epnum);
1825
Dinh Nguyen47a16852014-04-14 14:13:34 -07001826 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1827 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1828 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001829 break;
1830
Dinh Nguyen47a16852014-04-14 14:13:34 -07001831 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001832 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001833 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001834
1835 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001836 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001837 break;
1838
Dinh Nguyen47a16852014-04-14 14:13:34 -07001839 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001840 dev_dbg(hsotg->dev,
1841 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001842 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001843 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001844 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001845 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001846 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1847 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1848 */
1849 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001850 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001851 break;
1852
Dinh Nguyen47a16852014-04-14 14:13:34 -07001853 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001854 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001855 break;
1856
Dinh Nguyen47a16852014-04-14 14:13:34 -07001857 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001858 dev_dbg(hsotg->dev,
1859 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001860 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001861 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001862
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001863 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1864
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001865 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001866 break;
1867
1868 default:
1869 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1870 __func__, grxstsr);
1871
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001872 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001873 break;
1874 }
1875}
1876
1877/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001878 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001879 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001880 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001881static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001882{
1883 switch (mps) {
1884 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001885 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001886 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001887 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001888 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001889 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001890 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001891 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001892 }
1893
1894 /* bad max packet size, warn and return invalid result */
1895 WARN_ON(1);
1896 return (u32)-1;
1897}
1898
1899/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001900 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001901 * @hsotg: The driver state.
1902 * @ep: The index number of the endpoint
1903 * @mps: The maximum packet size in bytes
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001904 * @mc: The multicount value
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001905 *
1906 * Configure the maximum packet size for the given endpoint, updating
1907 * the hardware control registers to reflect this.
1908 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001909static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001910 unsigned int ep, unsigned int mps,
1911 unsigned int mc, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001912{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001913 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001914 void __iomem *regs = hsotg->regs;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001915 u32 reg;
1916
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001917 hs_ep = index_to_ep(hsotg, ep, dir_in);
1918 if (!hs_ep)
1919 return;
1920
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001921 if (ep == 0) {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001922 u32 mps_bytes = mps;
1923
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001924 /* EP0 is a special case */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001925 mps = dwc2_hsotg_ep0_mps(mps_bytes);
1926 if (mps > 3)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001927 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001928 hs_ep->ep.maxpacket = mps_bytes;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001929 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001930 } else {
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001931 if (mps > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001932 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001933 hs_ep->mc = mc;
1934 if (mc > 3)
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001935 goto bad_mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001936 hs_ep->ep.maxpacket = mps;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001937 }
1938
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001939 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001940 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001941 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001942 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001943 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001944 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001945 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001946 reg &= ~DXEPCTL_MPS_MASK;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08001947 reg |= mps;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001948 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001949 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001950
1951 return;
1952
1953bad_mps:
1954 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1955}
1956
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001957/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001958 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001959 * @hsotg: The driver state
1960 * @idx: The index for the endpoint (0..15)
1961 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001962static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001963{
1964 int timeout;
1965 int val;
1966
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001967 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
1968 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001969
1970 /* wait until the fifo is flushed */
1971 timeout = 100;
1972
1973 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001974 val = dwc2_readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001975
Dinh Nguyen47a16852014-04-14 14:13:34 -07001976 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001977 break;
1978
1979 if (--timeout == 0) {
1980 dev_err(hsotg->dev,
1981 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1982 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02001983 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001984 }
1985
1986 udelay(1);
1987 }
1988}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001989
1990/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001991 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001992 * @hsotg: The driver state
1993 * @hs_ep: The driver endpoint to check.
1994 *
1995 * Check to see if there is a request that has data to send, and if so
1996 * make an attempt to write data into the FIFO.
1997 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001998static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
1999 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002000{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002001 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002002
Robert Baldygaafcf4162013-09-19 11:50:19 +02002003 if (!hs_ep->dir_in || !hs_req) {
2004 /**
2005 * if request is not enqueued, we disable interrupts
2006 * for endpoints, excepting ep0
2007 */
2008 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002009 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
Robert Baldygaafcf4162013-09-19 11:50:19 +02002010 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002011 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02002012 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002013
2014 if (hs_req->req.actual < hs_req->req.length) {
2015 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
2016 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002017 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002018 }
2019
2020 return 0;
2021}
2022
2023/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002024 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002025 * @hsotg: The device state.
2026 * @hs_ep: The endpoint that has just completed.
2027 *
2028 * An IN transfer has been completed, update the transfer's state and then
2029 * call the relevant completion routines.
2030 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002031static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
2032 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002033{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002034 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002035 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002036 int size_left, size_done;
2037
2038 if (!hs_req) {
2039 dev_dbg(hsotg->dev, "XferCompl but no req\n");
2040 return;
2041 }
2042
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002043 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002044 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
2045 dev_dbg(hsotg->dev, "zlp packet sent\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002046 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002047 if (hsotg->test_mode) {
2048 int ret;
2049
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002050 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002051 if (ret < 0) {
2052 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
2053 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002054 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002055 return;
2056 }
2057 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002058 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002059 return;
2060 }
2061
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002062 /*
2063 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002064 * in the endpoint size register and then working it out from
2065 * the amount we loaded for the transfer.
2066 *
2067 * We do this even for DMA, as the transfer may have incremented
2068 * past the end of the buffer (DMA transfers are always 32bit
2069 * aligned).
2070 */
2071
Dinh Nguyen47a16852014-04-14 14:13:34 -07002072 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002073
2074 size_done = hs_ep->size_loaded - size_left;
2075 size_done += hs_ep->last_load;
2076
2077 if (hs_req->req.actual != size_done)
2078 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
2079 __func__, hs_req->req.actual, size_done);
2080
2081 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02002082 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
2083 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002084
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002085 if (!size_left && hs_req->req.actual < hs_req->req.length) {
2086 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002087 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002088 return;
2089 }
2090
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002091 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002092 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002093 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002094 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002095 /* transfer will be completed on next complete interrupt */
2096 return;
2097 }
2098
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002099 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2100 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002101 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002102 return;
2103 }
2104
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002105 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002106}
2107
2108/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002109 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2110 * @hsotg: The device state.
2111 * @idx: Index of ep.
2112 * @dir_in: Endpoint direction 1-in 0-out.
2113 *
2114 * Reads for endpoint with given index and direction, by masking
2115 * epint_reg with coresponding mask.
2116 */
2117static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2118 unsigned int idx, int dir_in)
2119{
2120 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2121 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2122 u32 ints;
2123 u32 mask;
2124 u32 diepempmsk;
2125
2126 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2127 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2128 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2129 mask |= DXEPINT_SETUP_RCVD;
2130
2131 ints = dwc2_readl(hsotg->regs + epint_reg);
2132 ints &= mask;
2133 return ints;
2134}
2135
2136/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002137 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2138 * @hs_ep: The endpoint on which interrupt is asserted.
2139 *
2140 * This interrupt indicates that the endpoint has been disabled per the
2141 * application's request.
2142 *
2143 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2144 * in case of ISOC completes current request.
2145 *
2146 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2147 * request starts it.
2148 */
2149static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2150{
2151 struct dwc2_hsotg *hsotg = hs_ep->parent;
2152 struct dwc2_hsotg_req *hs_req;
2153 unsigned char idx = hs_ep->index;
2154 int dir_in = hs_ep->dir_in;
2155 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2156 int dctl = dwc2_readl(hsotg->regs + DCTL);
2157
2158 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2159
2160 if (dir_in) {
2161 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2162
2163 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2164
2165 if (hs_ep->isochronous) {
2166 dwc2_hsotg_complete_in(hsotg, hs_ep);
2167 return;
2168 }
2169
2170 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2171 int dctl = dwc2_readl(hsotg->regs + DCTL);
2172
2173 dctl |= DCTL_CGNPINNAK;
2174 dwc2_writel(dctl, hsotg->regs + DCTL);
2175 }
2176 return;
2177 }
2178
2179 if (dctl & DCTL_GOUTNAKSTS) {
2180 dctl |= DCTL_CGOUTNAK;
2181 dwc2_writel(dctl, hsotg->regs + DCTL);
2182 }
2183
2184 if (!hs_ep->isochronous)
2185 return;
2186
2187 if (list_empty(&hs_ep->queue)) {
2188 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2189 __func__, hs_ep);
2190 return;
2191 }
2192
2193 do {
2194 hs_req = get_ep_head(hs_ep);
2195 if (hs_req)
2196 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2197 -ENODATA);
2198 dwc2_gadget_incr_frame_num(hs_ep);
2199 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2200
2201 dwc2_gadget_start_next_request(hs_ep);
2202}
2203
2204/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002205 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2206 * @hs_ep: The endpoint on which interrupt is asserted.
2207 *
2208 * This is starting point for ISOC-OUT transfer, synchronization done with
2209 * first out token received from host while corresponding EP is disabled.
2210 *
2211 * Device does not know initial frame in which out token will come. For this
2212 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2213 * getting this interrupt SW starts calculation for next transfer frame.
2214 */
2215static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2216{
2217 struct dwc2_hsotg *hsotg = ep->parent;
2218 int dir_in = ep->dir_in;
2219 u32 doepmsk;
2220
2221 if (dir_in || !ep->isochronous)
2222 return;
2223
2224 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
2225
2226 if (ep->interval > 1 &&
2227 ep->target_frame == TARGET_FRAME_INITIAL) {
2228 u32 dsts;
2229 u32 ctrl;
2230
2231 dsts = dwc2_readl(hsotg->regs + DSTS);
2232 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2233 dwc2_gadget_incr_frame_num(ep);
2234
2235 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2236 if (ep->target_frame & 0x1)
2237 ctrl |= DXEPCTL_SETODDFR;
2238 else
2239 ctrl |= DXEPCTL_SETEVENFR;
2240
2241 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2242 }
2243
2244 dwc2_gadget_start_next_request(ep);
2245 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2246 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2247 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2248}
2249
2250/**
2251* dwc2_gadget_handle_nak - handle NAK interrupt
2252* @hs_ep: The endpoint on which interrupt is asserted.
2253*
2254* This is starting point for ISOC-IN transfer, synchronization done with
2255* first IN token received from host while corresponding EP is disabled.
2256*
2257* Device does not know when first one token will arrive from host. On first
2258* token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2259* and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2260* sent in response to that as there was no data in FIFO. SW is basing on this
2261* interrupt to obtain frame in which token has come and then based on the
2262* interval calculates next frame for transfer.
2263*/
2264static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2265{
2266 struct dwc2_hsotg *hsotg = hs_ep->parent;
2267 int dir_in = hs_ep->dir_in;
2268
2269 if (!dir_in || !hs_ep->isochronous)
2270 return;
2271
2272 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
2273 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2274 if (hs_ep->interval > 1) {
2275 u32 ctrl = dwc2_readl(hsotg->regs +
2276 DIEPCTL(hs_ep->index));
2277 if (hs_ep->target_frame & 0x1)
2278 ctrl |= DXEPCTL_SETODDFR;
2279 else
2280 ctrl |= DXEPCTL_SETEVENFR;
2281
2282 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2283 }
2284
2285 dwc2_hsotg_complete_request(hsotg, hs_ep,
2286 get_ep_head(hs_ep), 0);
2287 }
2288
2289 dwc2_gadget_incr_frame_num(hs_ep);
2290}
2291
2292/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002293 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002294 * @hsotg: The driver state
2295 * @idx: The index for the endpoint (0..15)
2296 * @dir_in: Set if this is an IN endpoint
2297 *
2298 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002299 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002300static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002301 int dir_in)
2302{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002303 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002304 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2305 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2306 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002307 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002308 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002309
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002310 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002311 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002312
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002313 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002314 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002315
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002316 if (!hs_ep) {
2317 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
2318 __func__, idx, dir_in ? "in" : "out");
2319 return;
2320 }
2321
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002322 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2323 __func__, idx, dir_in ? "in" : "out", ints);
2324
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002325 /* Don't process XferCompl interrupt if it is a setup packet */
2326 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2327 ints &= ~DXEPINT_XFERCOMPL;
2328
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002329 if (ints & DXEPINT_STSPHSERCVD)
2330 dev_dbg(hsotg->dev, "%s: StsPhseRcvd asserted\n", __func__);
Robert Baldyga1479e842013-10-09 08:41:57 +02002331
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002332 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002333 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002334 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002335 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2336 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002337
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002338 /*
2339 * we get OutDone from the FIFO, so we only need to look
2340 * at completing IN requests here
2341 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002342 if (dir_in) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002343 if (hs_ep->isochronous && hs_ep->interval > 1)
2344 dwc2_gadget_incr_frame_num(hs_ep);
2345
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002346 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002347 if (ints & DXEPINT_NAKINTRPT)
2348 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002349
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002350 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002351 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002352 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002353 /*
2354 * We're using DMA, we need to fire an OutDone here
2355 * as we ignore the RXFIFO.
2356 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002357 if (hs_ep->isochronous && hs_ep->interval > 1)
2358 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002359
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002360 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002361 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362 }
2363
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002364 if (ints & DXEPINT_EPDISBLD)
2365 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002366
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002367 if (ints & DXEPINT_OUTTKNEPDIS)
2368 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2369
2370 if (ints & DXEPINT_NAKINTRPT)
2371 dwc2_gadget_handle_nak(hs_ep);
2372
Dinh Nguyen47a16852014-04-14 14:13:34 -07002373 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002374 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002375
Dinh Nguyen47a16852014-04-14 14:13:34 -07002376 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002377 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2378
2379 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002380 /*
2381 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002382 * setup packet. In non-DMA mode we'd get this
2383 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002384 * the setup here.
2385 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002386
2387 if (dir_in)
2388 WARN_ON_ONCE(1);
2389 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002390 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002391 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002392 }
2393
Dinh Nguyen47a16852014-04-14 14:13:34 -07002394 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002395 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002396
Robert Baldyga1479e842013-10-09 08:41:57 +02002397 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002398 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002399 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002400 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2401 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002402 }
2403
2404 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002405 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002406 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2407 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002408 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002409
2410 /* FIFO has space or is empty (see GAHBCFG) */
2411 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002412 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002413 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2414 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002415 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002416 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002417 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002418 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002419}
2420
2421/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002422 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002423 * @hsotg: The device state.
2424 *
2425 * Handle updating the device settings after the enumeration phase has
2426 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002427 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002428static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002429{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002430 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09002431 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002432
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002433 /*
2434 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002435 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002436 * we connected at.
2437 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002438
2439 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2440
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002441 /*
2442 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002443 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002444 * not advertise a 64byte MPS on EP0.
2445 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002446
2447 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01002448 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002449 case DSTS_ENUMSPD_FS:
2450 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002451 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002452 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002453 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002454 break;
2455
Dinh Nguyen47a16852014-04-14 14:13:34 -07002456 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002457 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002458 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002459 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002460 break;
2461
Dinh Nguyen47a16852014-04-14 14:13:34 -07002462 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002463 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002464 /*
2465 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002466 * moment, and the documentation seems to imply that it isn't
2467 * supported by the PHYs on some of the devices.
2468 */
2469 break;
2470 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002471 dev_info(hsotg->dev, "new device is %s\n",
2472 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002473
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002474 /*
2475 * we should now know the maximum packet size for an
2476 * endpoint, so set the endpoints to a default value.
2477 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002478
2479 if (ep0_mps) {
2480 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002481 /* Initialize ep0 for both in and out directions */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002482 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 1);
2483 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002484 for (i = 1; i < hsotg->num_of_eps; i++) {
2485 if (hsotg->eps_in[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002486 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
2487 0, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002488 if (hsotg->eps_out[i])
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08002489 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps,
2490 0, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002491 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002492 }
2493
2494 /* ensure after enumeration our EP0 is active */
2495
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002496 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002497
2498 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002499 dwc2_readl(hsotg->regs + DIEPCTL0),
2500 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002501}
2502
2503/**
2504 * kill_all_requests - remove all requests from the endpoint's queue
2505 * @hsotg: The device state.
2506 * @ep: The endpoint the requests may be on.
2507 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002508 *
2509 * Go through the requests on the given endpoint and mark them
2510 * completed with the given result code.
2511 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002512static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002513 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af42014-12-16 11:51:44 +01002514 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002515{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002516 struct dwc2_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002517 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002518
Robert Baldyga6b448af42014-12-16 11:51:44 +01002519 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002520
Robert Baldyga6b448af42014-12-16 11:51:44 +01002521 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002522 dwc2_hsotg_complete_request(hsotg, ep, req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002523 result);
Robert Baldyga6b448af42014-12-16 11:51:44 +01002524
Robert Baldygab203d0a2014-09-09 10:44:56 +02002525 if (!hsotg->dedicated_fifos)
2526 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07002527 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002528 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002529 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002530}
2531
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002532/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002533 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002534 * @hsotg: The device state.
2535 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002536 * The device has been disconnected. Remove all current
2537 * transactions and signal the gadget driver that this
2538 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002539 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002540void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002541{
2542 unsigned ep;
2543
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002544 if (!hsotg->connected)
2545 return;
2546
2547 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002548 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002549
2550 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2551 if (hsotg->eps_in[ep])
2552 kill_all_requests(hsotg, hsotg->eps_in[ep],
2553 -ESHUTDOWN);
2554 if (hsotg->eps_out[ep])
2555 kill_all_requests(hsotg, hsotg->eps_out[ep],
2556 -ESHUTDOWN);
2557 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002558
2559 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02002560 hsotg->lx_state = DWC2_L3;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002561}
2562
2563/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002564 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002565 * @hsotg: The device state:
2566 * @periodic: True if this is a periodic FIFO interrupt
2567 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002568static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002569{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002570 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002571 int epno, ret;
2572
2573 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002574 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002575 ep = index_to_ep(hsotg, epno, 1);
2576
2577 if (!ep)
2578 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002579
2580 if (!ep->dir_in)
2581 continue;
2582
2583 if ((periodic && !ep->periodic) ||
2584 (!periodic && ep->periodic))
2585 continue;
2586
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002587 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002588 if (ret < 0)
2589 break;
2590 }
2591}
2592
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002593/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002594#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2595 GINTSTS_PTXFEMP | \
2596 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002597
2598/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002599 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002600 * @hsotg: The device state
2601 *
2602 * Issue a soft reset to the core, and await the core finishing it.
2603 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002604void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002605 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002606{
Gregory Herrero1ee69032015-09-29 12:08:27 +02002607 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002608 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002609 u32 usbcfg;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002610
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02002611 /* Kill any ep0 requests as controller will be reinitialized */
2612 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
2613
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002614 if (!is_usb_reset)
John Youn241729b2015-12-17 11:17:59 -08002615 if (dwc2_core_reset(hsotg))
Gregory Herrero86de4892015-09-29 12:08:21 +02002616 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002617
2618 /*
2619 * we must now enable ep0 ready for host detection and then
2620 * set configuration.
2621 */
2622
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002623 /* keep other bits untouched (so e.g. forced modes are not lost) */
2624 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2625 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
2626 GUSBCFG_HNPCAP);
2627
Lukasz Majewski308d7342012-05-04 14:17:05 +02002628 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01002629 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002630 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
2631 (val << GUSBCFG_USBTRDTIM_SHIFT);
2632 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002633
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002634 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002635
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002636 if (!is_usb_reset)
2637 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002638
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002639 dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002640
2641 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002642 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002643
2644 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002645 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02002646 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002647 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02002648 GINTSTS_USBRST | GINTSTS_RESETDET |
2649 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Roman Bacikec1f9d92015-09-10 18:13:43 -07002650 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
2651 GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02002652
John Younbea8e862016-11-03 17:55:53 -07002653 if (hsotg->params.external_id_pin_ctl <= 0)
Gregory Herrero1ee69032015-09-29 12:08:27 +02002654 intmsk |= GINTSTS_CONIDSTSCHNG;
2655
2656 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002657
2658 if (using_dma(hsotg))
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002659 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2660 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
2661 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002662 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002663 dwc2_writel(((hsotg->dedicated_fifos) ?
2664 (GAHBCFG_NP_TXF_EMP_LVL |
2665 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2666 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002667
2668 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002669 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2670 * when we have no data to transfer. Otherwise we get being flooded by
2671 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002672 */
2673
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002674 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01002675 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002676 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002677 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002678 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002679
2680 /*
2681 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2682 * DMA mode we may need this.
2683 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002684 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002685 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002686 DOEPMSK_SETUPMSK | DOEPMSK_STSPHSERCVDMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002687 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002688
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002689 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002690
2691 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002692 dwc2_readl(hsotg->regs + DIEPCTL0),
2693 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002694
2695 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002696 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002697
2698 /*
2699 * Enable the RXFIFO when in slave mode, as this is how we collect
2700 * the data. In DMA mode, we get events from the FIFO but also
2701 * things we cannot process, so do not use it.
2702 */
2703 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002704 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002705
2706 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002707 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2708 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002709
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002710 if (!is_usb_reset) {
2711 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2712 udelay(10); /* see openiboot */
2713 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2714 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02002715
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002716 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002717
2718 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002719 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002720 * writing to the EPCTL register..
2721 */
2722
2723 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002724 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002725 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002726
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002727 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002728 DXEPCTL_CNAK | DXEPCTL_EPENA |
2729 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002730 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002731
2732 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002733 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002734 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002735
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002736 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002737
2738 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002739 dwc2_readl(hsotg->regs + DIEPCTL0),
2740 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002741
2742 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002743 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
2744 if (!is_usb_reset)
2745 val |= DCTL_SFTDISCON;
2746 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002747
2748 /* must be at-least 3ms to allow bus to see disconnect */
2749 mdelay(3);
2750
Gregory Herrero065d3932015-09-22 15:16:54 +02002751 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002752}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002753
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002754static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002755{
2756 /* set the soft-disconnect bit */
2757 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2758}
2759
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002760void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002761{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002762 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002763 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002764}
2765
2766/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07002767 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
2768 * @hsotg: The device state:
2769 *
2770 * This interrupt indicates one of the following conditions occurred while
2771 * transmitting an ISOC transaction.
2772 * - Corrupted IN Token for ISOC EP.
2773 * - Packet not complete in FIFO.
2774 *
2775 * The following actions will be taken:
2776 * - Determine the EP
2777 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
2778 */
2779static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
2780{
2781 struct dwc2_hsotg_ep *hs_ep;
2782 u32 epctrl;
2783 u32 idx;
2784
2785 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
2786
2787 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2788 hs_ep = hsotg->eps_in[idx];
2789 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
2790 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
2791 dwc2_gadget_target_frame_elapsed(hs_ep)) {
2792 epctrl |= DXEPCTL_SNAK;
2793 epctrl |= DXEPCTL_EPDIS;
2794 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
2795 }
2796 }
2797
2798 /* Clear interrupt */
2799 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
2800}
2801
2802/**
2803 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
2804 * @hsotg: The device state:
2805 *
2806 * This interrupt indicates one of the following conditions occurred while
2807 * transmitting an ISOC transaction.
2808 * - Corrupted OUT Token for ISOC EP.
2809 * - Packet not complete in FIFO.
2810 *
2811 * The following actions will be taken:
2812 * - Determine the EP
2813 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
2814 */
2815static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
2816{
2817 u32 gintsts;
2818 u32 gintmsk;
2819 u32 epctrl;
2820 struct dwc2_hsotg_ep *hs_ep;
2821 int idx;
2822
2823 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
2824
2825 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2826 hs_ep = hsotg->eps_out[idx];
2827 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
2828 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
2829 dwc2_gadget_target_frame_elapsed(hs_ep)) {
2830 /* Unmask GOUTNAKEFF interrupt */
2831 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
2832 gintmsk |= GINTSTS_GOUTNAKEFF;
2833 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
2834
2835 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2836 if (!(gintsts & GINTSTS_GOUTNAKEFF))
2837 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
2838 }
2839 }
2840
2841 /* Clear interrupt */
2842 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
2843}
2844
2845/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002846 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002847 * @irq: The IRQ number triggered
2848 * @pw: The pw value when registered the handler.
2849 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002850static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002851{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002852 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002853 int retry_count = 8;
2854 u32 gintsts;
2855 u32 gintmsk;
2856
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07002857 if (!dwc2_is_device_mode(hsotg))
2858 return IRQ_NONE;
2859
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002860 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002861irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002862 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2863 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002864
2865 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2866 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2867
2868 gintsts &= gintmsk;
2869
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02002870 if (gintsts & GINTSTS_RESETDET) {
2871 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
2872
2873 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
2874
2875 /* This event must be used only if controller is suspended */
2876 if (hsotg->lx_state == DWC2_L2) {
2877 dwc2_exit_hibernation(hsotg, true);
2878 hsotg->lx_state = DWC2_L0;
2879 }
2880 }
2881
2882 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
2883
2884 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
2885 u32 connected = hsotg->connected;
2886
2887 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
2888 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2889 dwc2_readl(hsotg->regs + GNPTXSTS));
2890
2891 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
2892
2893 /* Report disconnection if it is not already done. */
2894 dwc2_hsotg_disconnect(hsotg);
2895
2896 if (usb_status & GOTGCTL_BSESVLD && connected)
2897 dwc2_hsotg_core_init_disconnected(hsotg, true);
2898 }
2899
Dinh Nguyen47a16852014-04-14 14:13:34 -07002900 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002901 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002902
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002903 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002904 }
2905
Dinh Nguyen47a16852014-04-14 14:13:34 -07002906 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002907 u32 daint = dwc2_readl(hsotg->regs + DAINT);
2908 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02002909 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002910 int ep;
2911
Robert Baldyga7e804652013-09-19 11:50:20 +02002912 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002913 daint_out = daint >> DAINT_OUTEP_SHIFT;
2914 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002915
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002916 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2917
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002918 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2919 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002920 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002921 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002922 }
2923
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002924 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
2925 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002926 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002927 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002928 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002929 }
2930
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002931 /* check both FIFOs */
2932
Dinh Nguyen47a16852014-04-14 14:13:34 -07002933 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002934 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2935
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002936 /*
2937 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002938 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002939 * it needs re-enabling
2940 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002941
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002942 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
2943 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002944 }
2945
Dinh Nguyen47a16852014-04-14 14:13:34 -07002946 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002947 dev_dbg(hsotg->dev, "PTxFEmp\n");
2948
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002949 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002950
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002951 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
2952 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002953 }
2954
Dinh Nguyen47a16852014-04-14 14:13:34 -07002955 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002956 /*
2957 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002958 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002959 * set.
2960 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002961
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002962 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002963 }
2964
Dinh Nguyen47a16852014-04-14 14:13:34 -07002965 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002966 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002967 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002968 }
2969
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002970 /*
2971 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002972 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002973 * the occurrence.
2974 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002975
Dinh Nguyen47a16852014-04-14 14:13:34 -07002976 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002977 u8 idx;
2978 u32 epctrl;
2979 u32 gintmsk;
2980 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002981
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002982 /* Mask this interrupt */
2983 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
2984 gintmsk &= ~GINTSTS_GOUTNAKEFF;
2985 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002986
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002987 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
2988 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2989 hs_ep = hsotg->eps_out[idx];
2990 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
2991
2992 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
2993 epctrl |= DXEPCTL_SNAK;
2994 epctrl |= DXEPCTL_EPDIS;
2995 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
2996 }
2997 }
2998
2999 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003000 }
3001
Dinh Nguyen47a16852014-04-14 14:13:34 -07003002 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003003 dev_info(hsotg->dev, "GINNakEff triggered\n");
3004
Gregory Herrero3be99cd2015-12-07 12:07:31 +01003005 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09003006
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003007 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003008 }
3009
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003010 if (gintsts & GINTSTS_INCOMPL_SOIN)
3011 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003012
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07003013 if (gintsts & GINTSTS_INCOMPL_SOOUT)
3014 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07003015
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003016 /*
3017 * if we've had fifo events, we should try and go around the
3018 * loop again to see if there's any point in returning yet.
3019 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003020
3021 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
3022 goto irq_retry;
3023
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003024 spin_unlock(&hsotg->lock);
3025
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026 return IRQ_HANDLED;
3027}
3028
3029/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003030 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003031 * @ep: The USB endpint to configure
3032 * @desc: The USB endpoint descriptor to configure with.
3033 *
3034 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003035 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003036static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003037 const struct usb_endpoint_descriptor *desc)
3038{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003039 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003040 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003041 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003042 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003043 u32 epctrl_reg;
3044 u32 epctrl;
3045 u32 mps;
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003046 u32 mc;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003047 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003048 unsigned int dir_in;
3049 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02003050 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003051
3052 dev_dbg(hsotg->dev,
3053 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
3054 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
3055 desc->wMaxPacketSize, desc->bInterval);
3056
3057 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07003058 if (index == 0) {
3059 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
3060 return -EINVAL;
3061 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003062
3063 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
3064 if (dir_in != hs_ep->dir_in) {
3065 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
3066 return -EINVAL;
3067 }
3068
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07003069 mps = usb_endpoint_maxp(desc);
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003070 mc = usb_endpoint_maxp_mult(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003071
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003072 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003073
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003074 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003075 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003076
3077 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
3078 __func__, epctrl, epctrl_reg);
3079
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003080 /* Allocate DMA descriptor chain for non-ctrl endpoints */
3081 if (using_desc_dma(hsotg)) {
3082 hs_ep->desc_list = dma_alloc_coherent(hsotg->dev,
3083 MAX_DMA_DESC_NUM_GENERIC *
3084 sizeof(struct dwc2_dma_desc),
3085 &hs_ep->desc_list_dma, GFP_KERNEL);
3086 if (!hs_ep->desc_list) {
3087 ret = -ENOMEM;
3088 goto error2;
3089 }
3090 }
3091
Lukasz Majewski22258f42012-06-14 10:02:24 +02003092 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003093
Dinh Nguyen47a16852014-04-14 14:13:34 -07003094 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
3095 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003096
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003097 /*
3098 * mark the endpoint as active, otherwise the core may ignore
3099 * transactions entirely for this endpoint
3100 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003101 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003102
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003103 /* update the endpoint state */
Vardan Mikayelyanee2c40d2016-11-08 10:57:00 -08003104 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, mc, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003105
3106 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003107 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003108 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003109 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003110 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003111
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003112 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3113 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003114 epctrl |= DXEPCTL_EPTYPE_ISO;
3115 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003116 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003117 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003118 hs_ep->target_frame = TARGET_FRAME_INITIAL;
3119 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003120 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003121 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3122 mask |= DIEPMSK_NAKMSK;
3123 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3124 } else {
3125 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3126 mask |= DOEPMSK_OUTTKNEPDISMSK;
3127 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3128 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003129 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003130
3131 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003132 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003133 break;
3134
3135 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003136 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003137 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003138
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003139 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3140 hs_ep->interval = 1 << (desc->bInterval - 1);
3141
Dinh Nguyen47a16852014-04-14 14:13:34 -07003142 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003143 break;
3144
3145 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003146 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003147 break;
3148 }
3149
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003150 /*
3151 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003152 * a unique tx-fifo even if it is non-periodic.
3153 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003154 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003155 u32 fifo_index = 0;
3156 u32 fifo_size = UINT_MAX;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003157 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003158 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02003159 if (hsotg->fifo_map & (1<<i))
3160 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003161 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Robert Baldygab203d0a2014-09-09 10:44:56 +02003162 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
3163 if (val < size)
3164 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003165 /* Search for smallest acceptable fifo */
3166 if (val < fifo_size) {
3167 fifo_size = val;
3168 fifo_index = i;
3169 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003170 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003171 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003172 dev_err(hsotg->dev,
3173 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303174 ret = -ENOMEM;
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003175 goto error1;
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303176 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003177 hsotg->fifo_map |= 1 << fifo_index;
3178 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3179 hs_ep->fifo_index = fifo_index;
3180 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003181 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003182
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003183 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003184 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003185 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003186
3187 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3188 __func__, epctrl);
3189
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003190 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003191 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003192 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003193
3194 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003195 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003196
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003197error1:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003198 spin_unlock_irqrestore(&hsotg->lock, flags);
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003199
3200error2:
3201 if (ret && using_desc_dma(hsotg) && hs_ep->desc_list) {
3202 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3203 sizeof(struct dwc2_dma_desc),
3204 hs_ep->desc_list, hs_ep->desc_list_dma);
3205 hs_ep->desc_list = NULL;
3206 }
3207
Julia Lawall19c190f2010-03-29 17:36:44 +02003208 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003209}
3210
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003211/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003212 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003213 * @ep: The endpoint to disable.
3214 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003215static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003216{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003217 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003218 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003219 int dir_in = hs_ep->dir_in;
3220 int index = hs_ep->index;
3221 unsigned long flags;
3222 u32 epctrl_reg;
3223 u32 ctrl;
3224
Marek Szyprowski1e011292014-09-09 10:44:54 +02003225 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003226
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003227 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003228 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3229 return -EINVAL;
3230 }
3231
Vahram Aharonyan5f54c542016-11-09 19:28:03 -08003232 /* Remove DMA memory allocated for non-control Endpoints */
3233 if (using_desc_dma(hsotg)) {
3234 dma_free_coherent(hsotg->dev, MAX_DMA_DESC_NUM_GENERIC *
3235 sizeof(struct dwc2_dma_desc),
3236 hs_ep->desc_list, hs_ep->desc_list_dma);
3237 hs_ep->desc_list = NULL;
3238 }
3239
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003240 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003241
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003242 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003243
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003244 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07003245 ctrl &= ~DXEPCTL_EPENA;
3246 ctrl &= ~DXEPCTL_USBACTEP;
3247 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003248
3249 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003250 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003251
3252 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003253 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003254
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003255 /* terminate all requests with shutdown */
3256 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3257
Robert Baldyga1c07b202016-08-29 13:39:00 -07003258 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3259 hs_ep->fifo_index = 0;
3260 hs_ep->fifo_size = 0;
3261
Lukasz Majewski22258f42012-06-14 10:02:24 +02003262 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003263 return 0;
3264}
3265
3266/**
3267 * on_list - check request is on the given endpoint
3268 * @ep: The endpoint to check.
3269 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003270 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003271static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003272{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003273 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003274
3275 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
3276 if (req == test)
3277 return true;
3278 }
3279
3280 return false;
3281}
3282
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003283static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3284 u32 bit, u32 timeout)
3285{
3286 u32 i;
3287
3288 for (i = 0; i < timeout; i++) {
3289 if (dwc2_readl(hs_otg->regs + reg) & bit)
3290 return 0;
3291 udelay(1);
3292 }
3293
3294 return -ETIMEDOUT;
3295}
3296
3297static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3298 struct dwc2_hsotg_ep *hs_ep)
3299{
3300 u32 epctrl_reg;
3301 u32 epint_reg;
3302
3303 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3304 DOEPCTL(hs_ep->index);
3305 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3306 DOEPINT(hs_ep->index);
3307
3308 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3309 hs_ep->name);
3310 if (hs_ep->dir_in) {
3311 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3312 /* Wait for Nak effect */
3313 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3314 DXEPINT_INEPNAKEFF, 100))
3315 dev_warn(hsotg->dev,
3316 "%s: timeout DIEPINT.NAKEFF\n", __func__);
3317 } else {
Vardan Mikayelyan6b58cb02016-05-25 18:07:02 -07003318 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3319 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003320
3321 /* Wait for global nak to take effect */
3322 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
Du, Changbin0676c7e2015-12-04 15:38:23 +08003323 GINTSTS_GOUTNAKEFF, 100))
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003324 dev_warn(hsotg->dev,
Du, Changbin0676c7e2015-12-04 15:38:23 +08003325 "%s: timeout GINTSTS.GOUTNAKEFF\n", __func__);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003326 }
3327
3328 /* Disable ep */
3329 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3330
3331 /* Wait for ep to be disabled */
3332 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3333 dev_warn(hsotg->dev,
3334 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3335
3336 if (hs_ep->dir_in) {
3337 if (hsotg->dedicated_fifos) {
3338 dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) |
3339 GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL);
3340 /* Wait for fifo flush */
3341 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
3342 GRSTCTL_TXFFLSH, 100))
3343 dev_warn(hsotg->dev,
3344 "%s: timeout flushing fifos\n",
3345 __func__);
3346 }
3347 /* TODO: Flush shared tx fifo */
3348 } else {
3349 /* Remove global NAKs */
Du, Changbin0676c7e2015-12-04 15:38:23 +08003350 __bic32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003351 }
3352}
3353
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003354/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003355 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003356 * @ep: The endpoint to dequeue.
3357 * @req: The request to be removed from a queue.
3358 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003359static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003360{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003361 struct dwc2_hsotg_req *hs_req = our_req(req);
3362 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003363 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003364 unsigned long flags;
3365
Marek Szyprowski1e011292014-09-09 10:44:54 +02003366 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003367
Lukasz Majewski22258f42012-06-14 10:02:24 +02003368 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003369
3370 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02003371 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003372 return -EINVAL;
3373 }
3374
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003375 /* Dequeue already started request */
3376 if (req == &hs_ep->req->req)
3377 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
3378
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003379 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02003380 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003381
3382 return 0;
3383}
3384
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003385/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003386 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003387 * @ep: The endpoint to set halt.
3388 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003389 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
3390 * the endpoint is busy processing requests.
3391 *
3392 * We need to stall the endpoint immediately if request comes from set_feature
3393 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003394 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003395static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003396{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003397 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003398 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003399 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003400 u32 epreg;
3401 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003402 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003403
3404 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
3405
Robert Baldygac9f721b2014-01-14 08:36:00 +01003406 if (index == 0) {
3407 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003408 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01003409 else
3410 dev_warn(hs->dev,
3411 "%s: can't clear halt on ep0\n", __func__);
3412 return 0;
3413 }
3414
Vahram Aharonyan15186f12016-05-23 22:41:59 -07003415 if (hs_ep->isochronous) {
3416 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
3417 return -EINVAL;
3418 }
3419
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003420 if (!now && value && !list_empty(&hs_ep->queue)) {
3421 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
3422 ep->name);
3423 return -EAGAIN;
3424 }
3425
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003426 if (hs_ep->dir_in) {
3427 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003428 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003429
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003430 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05003431 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003432 if (epctl & DXEPCTL_EPENA)
3433 epctl |= DXEPCTL_EPDIS;
3434 } else {
3435 epctl &= ~DXEPCTL_STALL;
3436 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3437 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3438 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3439 epctl |= DXEPCTL_SETD0PID;
3440 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003441 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003442 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003443
3444 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003445 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003446
3447 if (value)
3448 epctl |= DXEPCTL_STALL;
3449 else {
3450 epctl &= ~DXEPCTL_STALL;
3451 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3452 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3453 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3454 epctl |= DXEPCTL_SETD0PID;
3455 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003456 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003457 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003458
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003459 hs_ep->halted = value;
3460
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003461 return 0;
3462}
3463
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003464/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003465 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003466 * @ep: The endpoint to set halt.
3467 * @value: Set or unset the halt.
3468 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003469static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003470{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003471 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003472 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003473 unsigned long flags = 0;
3474 int ret = 0;
3475
3476 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003477 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003478 spin_unlock_irqrestore(&hs->lock, flags);
3479
3480 return ret;
3481}
3482
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003483static struct usb_ep_ops dwc2_hsotg_ep_ops = {
3484 .enable = dwc2_hsotg_ep_enable,
3485 .disable = dwc2_hsotg_ep_disable,
3486 .alloc_request = dwc2_hsotg_ep_alloc_request,
3487 .free_request = dwc2_hsotg_ep_free_request,
3488 .queue = dwc2_hsotg_ep_queue_lock,
3489 .dequeue = dwc2_hsotg_ep_dequeue,
3490 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003491 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003492};
3493
3494/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003495 * dwc2_hsotg_init - initalize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003496 * @hsotg: The driver state
3497 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003498static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003499{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003500 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003501 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003502 /* unmask subset of endpoint interrupts */
3503
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003504 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
3505 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
3506 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003507
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003508 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
3509 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
3510 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003511
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003512 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003513
3514 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003515 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003516
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003517 /* setup fifos */
3518
3519 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003520 dwc2_readl(hsotg->regs + GRXFSIZ),
3521 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003522
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003523 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003524
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003525 /* keep other bits untouched (so e.g. forced modes are not lost) */
3526 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3527 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3528 GUSBCFG_HNPCAP);
3529
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003530 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003531 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003532 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3533 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
3534 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003535
Gregory Herrerof5090042015-01-09 13:38:47 +01003536 if (using_dma(hsotg))
3537 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003538}
3539
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003540/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003541 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003542 * @gadget: The usb gadget state
3543 * @driver: The usb gadget driver
3544 *
3545 * Perform initialization to prepare udc device and driver
3546 * to work.
3547 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003548static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003549 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003550{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003551 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003552 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003553 int ret;
3554
3555 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003556 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003557 return -ENODEV;
3558 }
3559
3560 if (!driver) {
3561 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3562 return -EINVAL;
3563 }
3564
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003565 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003566 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003567
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003568 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003569 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3570 return -EINVAL;
3571 }
3572
3573 WARN_ON(hsotg->driver);
3574
3575 driver->driver.bus = NULL;
3576 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003577 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003578 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3579
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003580 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
3581 ret = dwc2_lowlevel_hw_enable(hsotg);
3582 if (ret)
3583 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003584 }
3585
Gregory Herrerof6c01592015-01-09 13:38:41 +01003586 if (!IS_ERR_OR_NULL(hsotg->uphy))
3587 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003588
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003589 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07003590 if (dwc2_hw_is_device(hsotg)) {
3591 dwc2_hsotg_init(hsotg);
3592 dwc2_hsotg_core_init_disconnected(hsotg, false);
3593 }
3594
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003595 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003596 spin_unlock_irqrestore(&hsotg->lock, flags);
3597
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003598 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003599
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003600 return 0;
3601
3602err:
3603 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003604 return ret;
3605}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003606
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003607/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003608 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003609 * @gadget: The usb gadget state
3610 * @driver: The usb gadget driver
3611 *
3612 * Stop udc hw block and stay tunned for future transmissions
3613 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003614static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003615{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003616 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003617 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003618 int ep;
3619
3620 if (!hsotg)
3621 return -ENODEV;
3622
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003623 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003624 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3625 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003626 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003627 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003628 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003629 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003630
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003631 spin_lock_irqsave(&hsotg->lock, flags);
3632
Marek Szyprowski32805c32014-10-20 12:45:33 +02003633 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003634 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003635 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003636
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003637 spin_unlock_irqrestore(&hsotg->lock, flags);
3638
Gregory Herrerof6c01592015-01-09 13:38:41 +01003639 if (!IS_ERR_OR_NULL(hsotg->uphy))
3640 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003641
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003642 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3643 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003644
3645 return 0;
3646}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003647
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003648/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003649 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003650 * @gadget: The usb gadget state
3651 *
3652 * Read the {micro} frame number
3653 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003654static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003655{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003656 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003657}
3658
Lukasz Majewskia188b682012-06-22 09:29:56 +02003659/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003660 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02003661 * @gadget: The usb gadget state
3662 * @is_on: Current state of the USB PHY
3663 *
3664 * Connect/Disconnect the USB PHY pullup
3665 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003666static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02003667{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003668 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003669 unsigned long flags = 0;
3670
Gregory Herrero77ba9112015-09-29 12:08:19 +02003671 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
3672 hsotg->op_state);
3673
3674 /* Don't modify pullup state while in host mode */
3675 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3676 hsotg->enabled = is_on;
3677 return 0;
3678 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02003679
3680 spin_lock_irqsave(&hsotg->lock, flags);
3681 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003682 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003683 dwc2_hsotg_core_init_disconnected(hsotg, false);
3684 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003685 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003686 dwc2_hsotg_core_disconnect(hsotg);
3687 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003688 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02003689 }
3690
3691 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3692 spin_unlock_irqrestore(&hsotg->lock, flags);
3693
3694 return 0;
3695}
3696
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003697static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01003698{
3699 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3700 unsigned long flags;
3701
3702 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
3703 spin_lock_irqsave(&hsotg->lock, flags);
3704
Gregory Herrero61f72232015-09-29 12:08:28 +02003705 /*
3706 * If controller is hibernated, it must exit from hibernation
3707 * before being initialized / de-initialized
3708 */
3709 if (hsotg->lx_state == DWC2_L2)
3710 dwc2_exit_hibernation(hsotg, false);
3711
Gregory Herrero83d98222015-01-09 13:39:02 +01003712 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02003713 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02003714
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003715 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01003716 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003717 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003718 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003719 dwc2_hsotg_core_disconnect(hsotg);
3720 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003721 }
3722
3723 spin_unlock_irqrestore(&hsotg->lock, flags);
3724 return 0;
3725}
3726
Gregory Herrero596d6962015-01-09 13:39:08 +01003727/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003728 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01003729 * @gadget: The usb gadget state
3730 * @mA: Amount of current
3731 *
3732 * Report how much power the device may consume to the phy.
3733 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003734static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01003735{
3736 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3737
3738 if (IS_ERR_OR_NULL(hsotg->uphy))
3739 return -ENOTSUPP;
3740 return usb_phy_set_power(hsotg->uphy, mA);
3741}
3742
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003743static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
3744 .get_frame = dwc2_hsotg_gadget_getframe,
3745 .udc_start = dwc2_hsotg_udc_start,
3746 .udc_stop = dwc2_hsotg_udc_stop,
3747 .pullup = dwc2_hsotg_pullup,
3748 .vbus_session = dwc2_hsotg_vbus_session,
3749 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003750};
3751
3752/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003753 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003754 * @hsotg: The device state.
3755 * @hs_ep: The endpoint to be initialised.
3756 * @epnum: The endpoint number
3757 *
3758 * Initialise the given endpoint (as part of the probe and device state
3759 * creation) to give to the gadget driver. Setup the endpoint name, any
3760 * direction information and other state that may be required.
3761 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003762static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
3763 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003764 int epnum,
3765 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003766{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003767 char *dir;
3768
3769 if (epnum == 0)
3770 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003771 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003772 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003773 else
3774 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003775
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003776 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003777 hs_ep->index = epnum;
3778
3779 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3780
3781 INIT_LIST_HEAD(&hs_ep->queue);
3782 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3783
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003784 /* add to the list of endpoints known by the gadget driver */
3785 if (epnum)
3786 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3787
3788 hs_ep->parent = hsotg;
3789 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003790 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003791 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003792
Robert Baldyga29545222015-07-31 16:00:18 +02003793 if (epnum == 0) {
3794 hs_ep->ep.caps.type_control = true;
3795 } else {
3796 hs_ep->ep.caps.type_iso = true;
3797 hs_ep->ep.caps.type_bulk = true;
3798 hs_ep->ep.caps.type_int = true;
3799 }
3800
3801 if (dir_in)
3802 hs_ep->ep.caps.dir_in = true;
3803 else
3804 hs_ep->ep.caps.dir_out = true;
3805
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003806 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003807 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003808 * to be something valid.
3809 */
3810
3811 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003812 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003813 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003814 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003815 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003816 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003817 }
3818}
3819
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003820/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003821 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003822 * @param: The device state
3823 *
3824 * Read the USB core HW configuration registers
3825 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003826static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003827{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003828 u32 cfg;
3829 u32 ep_type;
3830 u32 i;
3831
Ben Dooks10aebc72010-07-19 09:40:44 +01003832 /* check hardware configuration */
3833
John Youn43e90342015-12-17 11:17:45 -08003834 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
3835
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003836 /* Add ep0 */
3837 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003838
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003839 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003840 GFP_KERNEL);
3841 if (!hsotg->eps_in[0])
3842 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003843 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003844 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003845
John Youn43e90342015-12-17 11:17:45 -08003846 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08003847 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003848 ep_type = cfg & 3;
3849 /* Direction in or both */
3850 if (!(ep_type & 2)) {
3851 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003852 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003853 if (!hsotg->eps_in[i])
3854 return -ENOMEM;
3855 }
3856 /* Direction out or both */
3857 if (!(ep_type & 1)) {
3858 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003859 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003860 if (!hsotg->eps_out[i])
3861 return -ENOMEM;
3862 }
3863 }
3864
John Youn43e90342015-12-17 11:17:45 -08003865 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
3866 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01003867
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003868 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3869 hsotg->num_of_eps,
3870 hsotg->dedicated_fifos ? "dedicated" : "shared",
3871 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003872 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003873}
3874
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003875/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003876 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003877 * @param: The device state
3878 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003879static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003880{
Mark Brown83a01802011-06-01 17:16:15 +01003881#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003882 struct device *dev = hsotg->dev;
3883 void __iomem *regs = hsotg->regs;
3884 u32 val;
3885 int idx;
3886
3887 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003888 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
3889 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003890
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01003891 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003892 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003893
3894 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003895 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003896
3897 /* show periodic fifo settings */
3898
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003899 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003900 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003901 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003902 val >> FIFOSIZE_DEPTH_SHIFT,
3903 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003904 }
3905
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003906 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003907 dev_info(dev,
3908 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003909 dwc2_readl(regs + DIEPCTL(idx)),
3910 dwc2_readl(regs + DIEPTSIZ(idx)),
3911 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003912
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003913 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003914 dev_info(dev,
3915 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003916 idx, dwc2_readl(regs + DOEPCTL(idx)),
3917 dwc2_readl(regs + DOEPTSIZ(idx)),
3918 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003919
3920 }
3921
3922 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003923 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003924#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003925}
3926
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003927/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06003928 * dwc2_gadget_init - init function for gadget
3929 * @dwc2: The data structure for the DWC2 driver.
3930 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003931 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003932int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003933{
Dinh Nguyen117777b2014-11-11 11:13:34 -06003934 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003935 int epnum;
3936 int ret;
John Youn43e90342015-12-17 11:17:45 -08003937
Gregory Herrero0a176272015-01-09 13:38:52 +01003938 /* Dump fifo information */
3939 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
John Youn05ee7992016-11-03 17:56:05 -07003940 hsotg->params.g_np_tx_fifo_size);
3941 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->params.g_rx_fifo_size);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003942
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003943 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003944 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003945 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02003946 if (hsotg->dr_mode == USB_DR_MODE_OTG)
3947 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02003948 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3949 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003950
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003951 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003952 if (ret) {
3953 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003954 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003955 }
3956
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003957 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
3958 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02003959 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003960 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003961
3962 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
3963 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02003964 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003965 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003966
Vahram Aharonyan0f6b80c2016-11-09 19:27:56 -08003967 if (using_desc_dma(hsotg)) {
3968 ret = dwc2_gadget_alloc_ctrl_desc_chains(hsotg);
3969 if (ret < 0)
3970 return ret;
3971 }
3972
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003973 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003974 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003975 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003976 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003977 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003978 }
3979
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003980 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3981
3982 if (hsotg->num_of_eps == 0) {
3983 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003984 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003985 }
3986
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003987 /* setup endpoint information */
3988
3989 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003990 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003991
3992 /* allocate EP0 request */
3993
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003994 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003995 GFP_KERNEL);
3996 if (!hsotg->ctrl_req) {
3997 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003998 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003999 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004000
4001 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004002 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
4003 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004004 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004005 epnum, 1);
4006 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004007 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004008 epnum, 0);
4009 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004010
Dinh Nguyen117777b2014-11-11 11:13:34 -06004011 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004012 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004013 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004014
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004015 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004016
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004017 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004018}
4019
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004020/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004021 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02004022 * @pdev: The platform information for the driver
4023 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004024int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004025{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03004026 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02004027
Ben Dooks5b7d70c2009-06-02 14:58:06 +01004028 return 0;
4029}
4030
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004031int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004032{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004033 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004034
Gregory Herrero9e779772015-04-29 22:09:07 +02004035 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004036 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004037
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004038 if (hsotg->driver) {
4039 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004040
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004041 dev_info(hsotg->dev, "suspending usb gadget %s\n",
4042 hsotg->driver->driver.name);
4043
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004044 spin_lock_irqsave(&hsotg->lock, flags);
4045 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004046 dwc2_hsotg_core_disconnect(hsotg);
4047 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004048 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
4049 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004050
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004051 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4052 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004053 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004054 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004055 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004056 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004057 }
4058
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004059 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004060}
4061
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004062int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004063{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004064 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004065
Gregory Herrero9e779772015-04-29 22:09:07 +02004066 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004067 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004068
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004069 if (hsotg->driver) {
4070 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4071 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004072
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004073 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004074 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004075 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004076 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004077 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004078 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004079
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004080 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004081}
John Youn58e52ff6a2016-02-23 19:54:57 -08004082
4083/**
4084 * dwc2_backup_device_registers() - Backup controller device registers.
4085 * When suspending usb bus, registers needs to be backuped
4086 * if controller power is disabled once suspended.
4087 *
4088 * @hsotg: Programming view of the DWC_otg controller
4089 */
4090int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4091{
4092 struct dwc2_dregs_backup *dr;
4093 int i;
4094
4095 dev_dbg(hsotg->dev, "%s\n", __func__);
4096
4097 /* Backup dev regs */
4098 dr = &hsotg->dr_backup;
4099
4100 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4101 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4102 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4103 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4104 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4105
4106 for (i = 0; i < hsotg->num_of_eps; i++) {
4107 /* Backup IN EPs */
4108 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4109
4110 /* Ensure DATA PID is correctly configured */
4111 if (dr->diepctl[i] & DXEPCTL_DPID)
4112 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4113 else
4114 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4115
4116 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4117 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4118
4119 /* Backup OUT EPs */
4120 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4121
4122 /* Ensure DATA PID is correctly configured */
4123 if (dr->doepctl[i] & DXEPCTL_DPID)
4124 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4125 else
4126 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4127
4128 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4129 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4130 }
4131 dr->valid = true;
4132 return 0;
4133}
4134
4135/**
4136 * dwc2_restore_device_registers() - Restore controller device registers.
4137 * When resuming usb bus, device registers needs to be restored
4138 * if controller power were disabled.
4139 *
4140 * @hsotg: Programming view of the DWC_otg controller
4141 */
4142int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4143{
4144 struct dwc2_dregs_backup *dr;
4145 u32 dctl;
4146 int i;
4147
4148 dev_dbg(hsotg->dev, "%s\n", __func__);
4149
4150 /* Restore dev regs */
4151 dr = &hsotg->dr_backup;
4152 if (!dr->valid) {
4153 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4154 __func__);
4155 return -EINVAL;
4156 }
4157 dr->valid = false;
4158
4159 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4160 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4161 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4162 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4163 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4164
4165 for (i = 0; i < hsotg->num_of_eps; i++) {
4166 /* Restore IN EPs */
4167 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4168 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4169 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4170
4171 /* Restore OUT EPs */
4172 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4173 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4174 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4175 }
4176
4177 /* Set the Power-On Programming done bit */
4178 dctl = dwc2_readl(hsotg->regs + DCTL);
4179 dctl |= DCTL_PWRONPRGDONE;
4180 dwc2_writel(dctl, hsotg->regs + DCTL);
4181
4182 return 0;
4183}