blob: 4cd6403a75668c35bef6a277f987bf41fc55ed27 [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{
Gregory Herreroedd74be2015-01-09 13:38:48 +010096 return hsotg->g_using_dma;
Ben Dooks5b7d70c2009-06-02 14:58:06 +010097}
98
99/**
Vardan Mikayelyan92d16352016-05-25 18:07:05 -0700100 * dwc2_gadget_incr_frame_num - Increments the targeted frame number.
101 * @hs_ep: The endpoint
102 * @increment: The value to increment by
103 *
104 * This function will also check if the frame number overruns DSTS_SOFFN_LIMIT.
105 * If an overrun occurs it will wrap the value and set the frame_overrun flag.
106 */
107static inline void dwc2_gadget_incr_frame_num(struct dwc2_hsotg_ep *hs_ep)
108{
109 hs_ep->target_frame += hs_ep->interval;
110 if (hs_ep->target_frame > DSTS_SOFFN_LIMIT) {
111 hs_ep->frame_overrun = 1;
112 hs_ep->target_frame &= DSTS_SOFFN_LIMIT;
113 } else {
114 hs_ep->frame_overrun = 0;
115 }
116}
117
118/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500119 * dwc2_hsotg_en_gsint - enable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100120 * @hsotg: The device state
121 * @ints: A bitmask of the interrupts to enable
122 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500123static void dwc2_hsotg_en_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100124{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300125 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100126 u32 new_gsintmsk;
127
128 new_gsintmsk = gsintmsk | ints;
129
130 if (new_gsintmsk != gsintmsk) {
131 dev_dbg(hsotg->dev, "gsintmsk now 0x%08x\n", new_gsintmsk);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300132 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100133 }
134}
135
136/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500137 * dwc2_hsotg_disable_gsint - disable one or more of the general interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100138 * @hsotg: The device state
139 * @ints: A bitmask of the interrupts to enable
140 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500141static void dwc2_hsotg_disable_gsint(struct dwc2_hsotg *hsotg, u32 ints)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100142{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300143 u32 gsintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100144 u32 new_gsintmsk;
145
146 new_gsintmsk = gsintmsk & ~ints;
147
148 if (new_gsintmsk != gsintmsk)
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300149 dwc2_writel(new_gsintmsk, hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100150}
151
152/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500153 * dwc2_hsotg_ctrl_epint - enable/disable an endpoint irq
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100154 * @hsotg: The device state
155 * @ep: The endpoint index
156 * @dir_in: True if direction is in.
157 * @en: The enable value, true to enable
158 *
159 * Set or clear the mask for an individual endpoint's interrupt
160 * request.
161 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500162static void dwc2_hsotg_ctrl_epint(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100163 unsigned int ep, unsigned int dir_in,
164 unsigned int en)
165{
166 unsigned long flags;
167 u32 bit = 1 << ep;
168 u32 daint;
169
170 if (!dir_in)
171 bit <<= 16;
172
173 local_irq_save(flags);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300174 daint = dwc2_readl(hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100175 if (en)
176 daint |= bit;
177 else
178 daint &= ~bit;
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300179 dwc2_writel(daint, hsotg->regs + DAINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100180 local_irq_restore(flags);
181}
182
183/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500184 * dwc2_hsotg_init_fifo - initialise non-periodic FIFOs
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100185 * @hsotg: The device instance.
186 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500187static void dwc2_hsotg_init_fifo(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100188{
Robert Baldygaba48eab2016-08-29 13:38:55 -0700189 unsigned int fifo;
Ben Dooks0f002d22010-05-25 05:36:50 +0100190 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100191 int timeout;
Robert Baldygaaa381a72016-08-29 13:38:52 -0700192 u32 dptxfsizn;
Ben Dooks0f002d22010-05-25 05:36:50 +0100193 u32 val;
194
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100195 /* Reset fifo map if not correctly cleared during previous session */
196 WARN_ON(hsotg->fifo_map);
197 hsotg->fifo_map = 0;
198
Gregory Herrero0a176272015-01-09 13:38:52 +0100199 /* set RX/NPTX FIFO sizes */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300200 dwc2_writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
201 dwc2_writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
Gregory Herrero0a176272015-01-09 13:38:52 +0100202 (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
203 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100204
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200205 /*
206 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100207 * block have overlapping default addresses. This also ensures
208 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200209 * known values.
210 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100211
212 /* start at the end of the GNPTXFSIZ, rounded up */
Gregory Herrero0a176272015-01-09 13:38:52 +0100213 addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
Ben Dooks0f002d22010-05-25 05:36:50 +0100214
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200215 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100216 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200217 * them to endpoints dynamically according to maxpacket size value of
218 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200219 */
Robert Baldygaba48eab2016-08-29 13:38:55 -0700220 for (fifo = 1; fifo < MAX_EPS_CHANNELS; fifo++) {
221 dptxfsizn = dwc2_readl(hsotg->regs + DPTXFSIZN(fifo));
Robert Baldygaaa381a72016-08-29 13:38:52 -0700222
223 val = (dptxfsizn & FIFOSIZE_DEPTH_MASK) | addr;
224 addr += dptxfsizn >> FIFOSIZE_DEPTH_SHIFT;
225
226 if (addr > hsotg->fifo_mem)
227 break;
Ben Dooks0f002d22010-05-25 05:36:50 +0100228
Robert Baldygaba48eab2016-08-29 13:38:55 -0700229 dwc2_writel(val, hsotg->regs + DPTXFSIZN(fifo));
Ben Dooks0f002d22010-05-25 05:36:50 +0100230 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100231
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200232 /*
233 * according to p428 of the design guide, we need to ensure that
234 * all fifos are flushed before continuing
235 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100236
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300237 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700238 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100239
240 /* wait until the fifos are both flushed */
241 timeout = 100;
242 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300243 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100244
Dinh Nguyen47a16852014-04-14 14:13:34 -0700245 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100246 break;
247
248 if (--timeout == 0) {
249 dev_err(hsotg->dev,
250 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
251 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100252 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100253 }
254
255 udelay(1);
256 }
257
258 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100259}
260
261/**
262 * @ep: USB endpoint to allocate request for.
263 * @flags: Allocation flags
264 *
265 * Allocate a new USB request structure appropriate for the specified endpoint
266 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500267static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
Mark Brown0978f8c2010-01-18 13:18:35 +0000268 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100269{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500270 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100271
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500272 req = kzalloc(sizeof(struct dwc2_hsotg_req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100273 if (!req)
274 return NULL;
275
276 INIT_LIST_HEAD(&req->queue);
277
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100278 return &req->req;
279}
280
281/**
282 * is_ep_periodic - return true if the endpoint is in periodic mode.
283 * @hs_ep: The endpoint to query.
284 *
285 * Returns true if the endpoint is in periodic mode, meaning it is being
286 * used for an Interrupt or ISO transfer.
287 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500288static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100289{
290 return hs_ep->periodic;
291}
292
293/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500294 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100295 * @hsotg: The device state.
296 * @hs_ep: The endpoint for the request
297 * @hs_req: The request being processed.
298 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500299 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100300 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200301 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500302static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
303 struct dwc2_hsotg_ep *hs_ep,
304 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100305{
306 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100307
308 /* ignore this if we're not moving any data */
309 if (hs_req->req.length == 0)
310 return;
311
Jingoo Han17d966a2013-05-11 21:14:00 +0900312 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100313}
314
315/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500316 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100317 * @hsotg: The controller state.
318 * @hs_ep: The endpoint we're going to write for.
319 * @hs_req: The request to write data for.
320 *
321 * This is called when the TxFIFO has some space in it to hold a new
322 * transmission and we have something to give it. The actual setup of
323 * the data size is done elsewhere, so all we have to do is to actually
324 * write the data.
325 *
326 * The return value is zero if there is more space (or nothing was done)
327 * otherwise -ENOSPC is returned if the FIFO space was used up.
328 *
329 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200330 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500331static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
332 struct dwc2_hsotg_ep *hs_ep,
333 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100334{
335 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300336 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100337 int buf_pos = hs_req->req.actual;
338 int to_write = hs_ep->size_loaded;
339 void *data;
340 int can_write;
341 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200342 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100343
344 to_write -= (buf_pos - hs_ep->last_load);
345
346 /* if there's nothing to write, get out early */
347 if (to_write == 0)
348 return 0;
349
Ben Dooks10aebc72010-07-19 09:40:44 +0100350 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300351 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100352 int size_left;
353 int size_done;
354
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200355 /*
356 * work out how much data was loaded so we can calculate
357 * how much data is left in the fifo.
358 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100359
Dinh Nguyen47a16852014-04-14 14:13:34 -0700360 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100361
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200362 /*
363 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100364 * previous data has been completely sent.
365 */
366 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500367 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100368 return -ENOSPC;
369 }
370
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100371 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
372 __func__, size_left,
373 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
374
375 /* how much of the data has moved */
376 size_done = hs_ep->size_loaded - size_left;
377
378 /* how much data is left in the fifo */
379 can_write = hs_ep->fifo_load - size_done;
380 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
381 __func__, can_write);
382
383 can_write = hs_ep->fifo_size - can_write;
384 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
385 __func__, can_write);
386
387 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500388 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100389 return -ENOSPC;
390 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100391 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Robert Baldygaad674a12016-08-29 13:38:50 -0700392 can_write = dwc2_readl(hsotg->regs +
393 DTXFSTS(hs_ep->fifo_index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100394
395 can_write &= 0xffff;
396 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100397 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700398 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100399 dev_dbg(hsotg->dev,
400 "%s: no queue slots available (0x%08x)\n",
401 __func__, gnptxsts);
402
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500403 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100404 return -ENOSPC;
405 }
406
Dinh Nguyen47a16852014-04-14 14:13:34 -0700407 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100408 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100409 }
410
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200411 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
412
413 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
414 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100415
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200416 /*
417 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100418 * FIFO, requests of >512 cause the endpoint to get stuck with a
419 * fragment of the end of the transfer in it.
420 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200421 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100422 can_write = 512;
423
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200424 /*
425 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100426 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200427 * doing it.
428 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200429 if (to_write > max_transfer) {
430 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100431
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200432 /* it's needed only when we do not use dedicated fifos */
433 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500434 dwc2_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700435 periodic ? GINTSTS_PTXFEMP :
436 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100437 }
438
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100439 /* see if we can write data */
440
441 if (to_write > can_write) {
442 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200443 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100444
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200445 /*
446 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100447 * exact number of packets.
448 *
449 * Note, we do not currently check to see if we can ever
450 * write a full packet or not to the FIFO.
451 */
452
453 if (pkt_round)
454 to_write -= pkt_round;
455
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200456 /*
457 * enable correct FIFO interrupt to alert us when there
458 * is more room left.
459 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100460
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200461 /* it's needed only when we do not use dedicated fifos */
462 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500463 dwc2_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700464 periodic ? GINTSTS_PTXFEMP :
465 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100466 }
467
468 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
469 to_write, hs_req->req.length, can_write, buf_pos);
470
471 if (to_write <= 0)
472 return -ENOSPC;
473
474 hs_req->req.actual = buf_pos + to_write;
475 hs_ep->total_data += to_write;
476
477 if (periodic)
478 hs_ep->fifo_load += to_write;
479
480 to_write = DIV_ROUND_UP(to_write, 4);
481 data = hs_req->req.buf + buf_pos;
482
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500483 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100484
485 return (to_write >= can_write) ? -ENOSPC : 0;
486}
487
488/**
489 * get_ep_limit - get the maximum data legnth for this endpoint
490 * @hs_ep: The endpoint
491 *
492 * Return the maximum data that can be queued in one go on a given endpoint
493 * so that transfers that are too long can be split.
494 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500495static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100496{
497 int index = hs_ep->index;
498 unsigned maxsize;
499 unsigned maxpkt;
500
501 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700502 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
503 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100504 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100505 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900506 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700507 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900508 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100509 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100510 }
511
512 /* we made the constant loading easier above by using +1 */
513 maxpkt--;
514 maxsize--;
515
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200516 /*
517 * constrain by packet count if maxpkts*pktsize is greater
518 * than the length register size.
519 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100520
521 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
522 maxsize = maxpkt * hs_ep->ep.maxpacket;
523
524 return maxsize;
525}
526
527/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700528* dwc2_hsotg_read_frameno - read current frame number
529* @hsotg: The device instance
530*
531* Return the current frame number
532*/
533static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
534{
535 u32 dsts;
536
537 dsts = dwc2_readl(hsotg->regs + DSTS);
538 dsts &= DSTS_SOFFN_MASK;
539 dsts >>= DSTS_SOFFN_SHIFT;
540
541 return dsts;
542}
543
544/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500545 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100546 * @hsotg: The controller state.
547 * @hs_ep: The endpoint to process a request for
548 * @hs_req: The request to start.
549 * @continuing: True if we are doing more for the current request.
550 *
551 * Start the given request running by setting the endpoint registers
552 * appropriately, and writing any data to the FIFOs.
553 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500554static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
555 struct dwc2_hsotg_ep *hs_ep,
556 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100557 bool continuing)
558{
559 struct usb_request *ureq = &hs_req->req;
560 int index = hs_ep->index;
561 int dir_in = hs_ep->dir_in;
562 u32 epctrl_reg;
563 u32 epsize_reg;
564 u32 epsize;
565 u32 ctrl;
566 unsigned length;
567 unsigned packets;
568 unsigned maxreq;
569
570 if (index != 0) {
571 if (hs_ep->req && !continuing) {
572 dev_err(hsotg->dev, "%s: active request\n", __func__);
573 WARN_ON(1);
574 return;
575 } else if (hs_ep->req != hs_req && continuing) {
576 dev_err(hsotg->dev,
577 "%s: continue different req\n", __func__);
578 WARN_ON(1);
579 return;
580 }
581 }
582
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200583 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
584 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100585
586 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300587 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100588 hs_ep->dir_in ? "in" : "out");
589
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900590 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300591 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900592
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200593 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900594 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
595 return;
596 }
597
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100598 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200599 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
600 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100601
602 maxreq = get_ep_limit(hs_ep);
603 if (length > maxreq) {
604 int round = maxreq % hs_ep->ep.maxpacket;
605
606 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
607 __func__, length, maxreq, round);
608
609 /* round down to multiple of packets */
610 if (round)
611 maxreq -= round;
612
613 length = maxreq;
614 }
615
616 if (length)
617 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
618 else
619 packets = 1; /* send one packet if length is zero. */
620
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200621 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
622 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
623 return;
624 }
625
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100626 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200627 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700628 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200629 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700630 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100631 else
632 epsize = 0;
633
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100634 /*
635 * zero length packet should be programmed on its own and should not
636 * be counted in DIEPTSIZ.PktCnt with other packets.
637 */
638 if (dir_in && ureq->zero && !continuing) {
639 /* Test if zlp is actually required. */
640 if ((ureq->length >= hs_ep->ep.maxpacket) &&
641 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +0100642 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100643 }
644
Dinh Nguyen47a16852014-04-14 14:13:34 -0700645 epsize |= DXEPTSIZ_PKTCNT(packets);
646 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100647
648 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
649 __func__, packets, length, ureq->length, epsize, epsize_reg);
650
651 /* store the request as the current one we're doing */
652 hs_ep->req = hs_req;
653
654 /* write size / packets */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300655 dwc2_writel(epsize, hsotg->regs + epsize_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100656
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900657 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100658 unsigned int dma_reg;
659
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200660 /*
661 * write DMA address to control register, buffer already
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500662 * synced by dwc2_hsotg_ep_queue().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200663 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100664
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200665 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300666 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100667
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300668 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900669 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100670 }
671
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700672 if (hs_ep->isochronous && hs_ep->interval == 1) {
673 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
674 dwc2_gadget_incr_frame_num(hs_ep);
675
676 if (hs_ep->target_frame & 0x1)
677 ctrl |= DXEPCTL_SETODDFR;
678 else
679 ctrl |= DXEPCTL_SETEVENFR;
680 }
681
Dinh Nguyen47a16852014-04-14 14:13:34 -0700682 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200683
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100684 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +0200685
686 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100687 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -0700688 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200689
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100690 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300691 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100692
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200693 /*
694 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100695 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200696 * this information.
697 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100698 hs_ep->size_loaded = length;
699 hs_ep->last_load = ureq->actual;
700
701 if (dir_in && !using_dma(hsotg)) {
702 /* set these anyway, we may need them for non-periodic in */
703 hs_ep->fifo_load = 0;
704
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500705 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100706 }
707
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200708 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200709 * Note, trying to clear the NAK here causes problems with transmit
710 * on the S3C6400 ending up with the TXFIFO becoming full.
711 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100712
713 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300714 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +0100715 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700716 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300717 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100718
Dinh Nguyen47a16852014-04-14 14:13:34 -0700719 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300720 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200721
722 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500723 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100724}
725
726/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500727 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100728 * @hsotg: The device state.
729 * @hs_ep: The endpoint the request is on.
730 * @req: The request being processed.
731 *
732 * We've been asked to queue a request, so ensure that the memory buffer
733 * is correctly setup for DMA. If we've been passed an extant DMA address
734 * then ensure the buffer has been synced to memory. If our buffer has no
735 * DMA memory, then we map the memory and mark our request to allow us to
736 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200737 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500738static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
739 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100740 struct usb_request *req)
741{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500742 struct dwc2_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200743 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100744
745 /* if the length is zero, ignore the DMA data */
746 if (hs_req->req.length == 0)
747 return 0;
748
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200749 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
750 if (ret)
751 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100752
753 return 0;
754
755dma_error:
756 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
757 __func__, req->buf, req->length);
758
759 return -EIO;
760}
761
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500762static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
763 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100764{
765 void *req_buf = hs_req->req.buf;
766
767 /* If dma is not being used or buffer is aligned */
768 if (!using_dma(hsotg) || !((long)req_buf & 3))
769 return 0;
770
771 WARN_ON(hs_req->saved_req_buf);
772
773 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
774 hs_ep->ep.name, req_buf, hs_req->req.length);
775
776 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
777 if (!hs_req->req.buf) {
778 hs_req->req.buf = req_buf;
779 dev_err(hsotg->dev,
780 "%s: unable to allocate memory for bounce buffer\n",
781 __func__);
782 return -ENOMEM;
783 }
784
785 /* Save actual buffer */
786 hs_req->saved_req_buf = req_buf;
787
788 if (hs_ep->dir_in)
789 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
790 return 0;
791}
792
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500793static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
794 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100795{
796 /* If dma is not being used or buffer was aligned */
797 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
798 return;
799
800 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
801 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
802
803 /* Copy data from bounce buffer on successful out transfer */
804 if (!hs_ep->dir_in && !hs_req->req.status)
805 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
806 hs_req->req.actual);
807
808 /* Free bounce buffer */
809 kfree(hs_req->req.buf);
810
811 hs_req->req.buf = hs_req->saved_req_buf;
812 hs_req->saved_req_buf = NULL;
813}
814
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700815/**
816 * dwc2_gadget_target_frame_elapsed - Checks target frame
817 * @hs_ep: The driver endpoint to check
818 *
819 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
820 * corresponding transfer.
821 */
822static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
823{
824 struct dwc2_hsotg *hsotg = hs_ep->parent;
825 u32 target_frame = hs_ep->target_frame;
826 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
827 bool frame_overrun = hs_ep->frame_overrun;
828
829 if (!frame_overrun && current_frame >= target_frame)
830 return true;
831
832 if (frame_overrun && current_frame >= target_frame &&
833 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
834 return true;
835
836 return false;
837}
838
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500839static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100840 gfp_t gfp_flags)
841{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500842 struct dwc2_hsotg_req *hs_req = our_req(req);
843 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600844 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100845 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100846 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100847
848 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
849 ep->name, req, req->length, req->buf, req->no_interrupt,
850 req->zero, req->short_not_ok);
851
Gregory Herrero7ababa92015-04-29 22:09:08 +0200852 /* Prevent new request submission when controller is suspended */
853 if (hs->lx_state == DWC2_L2) {
854 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
855 __func__);
856 return -EAGAIN;
857 }
858
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100859 /* initialise status of the request */
860 INIT_LIST_HEAD(&hs_req->queue);
861 req->actual = 0;
862 req->status = -EINPROGRESS;
863
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500864 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100865 if (ret)
866 return ret;
867
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100868 /* if we're using DMA, sync the buffers as necessary */
869 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500870 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100871 if (ret)
872 return ret;
873 }
874
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100875 first = list_empty(&hs_ep->queue);
876 list_add_tail(&hs_req->queue, &hs_ep->queue);
877
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700878 if (first) {
879 if (!hs_ep->isochronous) {
880 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
881 return 0;
882 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100883
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700884 while (dwc2_gadget_target_frame_elapsed(hs_ep))
885 dwc2_gadget_incr_frame_num(hs_ep);
886
887 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
888 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
889 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100890 return 0;
891}
892
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500893static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200894 gfp_t gfp_flags)
895{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500896 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600897 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200898 unsigned long flags = 0;
899 int ret = 0;
900
901 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500902 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200903 spin_unlock_irqrestore(&hs->lock, flags);
904
905 return ret;
906}
907
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500908static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100909 struct usb_request *req)
910{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500911 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100912
913 kfree(hs_req);
914}
915
916/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500917 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100918 * @ep: The endpoint the request was on.
919 * @req: The request completed.
920 *
921 * Called on completion of any requests the driver itself
922 * submitted that need cleaning up.
923 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500924static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100925 struct usb_request *req)
926{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500927 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600928 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100929
930 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
931
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500932 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100933}
934
935/**
936 * ep_from_windex - convert control wIndex value to endpoint
937 * @hsotg: The driver state.
938 * @windex: The control request wIndex field (in host order).
939 *
940 * Convert the given wIndex into a pointer to an driver endpoint
941 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200942 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500943static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100944 u32 windex)
945{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500946 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100947 int dir = (windex & USB_DIR_IN) ? 1 : 0;
948 int idx = windex & 0x7F;
949
950 if (windex >= 0x100)
951 return NULL;
952
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200953 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100954 return NULL;
955
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100956 ep = index_to_ep(hsotg, idx, dir);
957
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100958 if (idx && ep->dir_in != dir)
959 return NULL;
960
961 return ep;
962}
963
964/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500965 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100966 * @hsotg: The driver state.
967 * @testmode: requested usb test mode
968 * Enable usb Test Mode requested by the Host.
969 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500970int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100971{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300972 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100973
974 dctl &= ~DCTL_TSTCTL_MASK;
975 switch (testmode) {
976 case TEST_J:
977 case TEST_K:
978 case TEST_SE0_NAK:
979 case TEST_PACKET:
980 case TEST_FORCE_EN:
981 dctl |= testmode << DCTL_TSTCTL_SHIFT;
982 break;
983 default:
984 return -EINVAL;
985 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300986 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100987 return 0;
988}
989
990/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500991 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100992 * @hsotg: The device state
993 * @ep: Endpoint 0
994 * @buff: Buffer for request
995 * @length: Length of reply.
996 *
997 * Create a request and queue it on the given endpoint. This is useful as
998 * an internal method of sending replies to certain control requests, etc.
999 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001000static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
1001 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001002 void *buff,
1003 int length)
1004{
1005 struct usb_request *req;
1006 int ret;
1007
1008 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1009
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001010 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001011 hsotg->ep0_reply = req;
1012 if (!req) {
1013 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1014 return -ENOMEM;
1015 }
1016
1017 req->buf = hsotg->ep0_buff;
1018 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001019 /*
1020 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1021 * STATUS stage.
1022 */
1023 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001024 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001025
1026 if (length)
1027 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001028
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001029 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001030 if (ret) {
1031 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1032 return ret;
1033 }
1034
1035 return 0;
1036}
1037
1038/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001039 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001040 * @hsotg: The device state
1041 * @ctrl: USB control request
1042 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001043static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001044 struct usb_ctrlrequest *ctrl)
1045{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001046 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1047 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001048 __le16 reply;
1049 int ret;
1050
1051 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1052
1053 if (!ep0->dir_in) {
1054 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1055 return -EINVAL;
1056 }
1057
1058 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1059 case USB_RECIP_DEVICE:
1060 reply = cpu_to_le16(0); /* bit 0 => self powered,
1061 * bit 1 => remote wakeup */
1062 break;
1063
1064 case USB_RECIP_INTERFACE:
1065 /* currently, the data result should be zero */
1066 reply = cpu_to_le16(0);
1067 break;
1068
1069 case USB_RECIP_ENDPOINT:
1070 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1071 if (!ep)
1072 return -ENOENT;
1073
1074 reply = cpu_to_le16(ep->halted ? 1 : 0);
1075 break;
1076
1077 default:
1078 return 0;
1079 }
1080
1081 if (le16_to_cpu(ctrl->wLength) != 2)
1082 return -EINVAL;
1083
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001084 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001085 if (ret) {
1086 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1087 return ret;
1088 }
1089
1090 return 1;
1091}
1092
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001093static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001094
1095/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001096 * get_ep_head - return the first request on the endpoint
1097 * @hs_ep: The controller endpoint to get
1098 *
1099 * Get the first request on the endpoint.
1100 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001101static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001102{
1103 if (list_empty(&hs_ep->queue))
1104 return NULL;
1105
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001106 return list_first_entry(&hs_ep->queue, struct dwc2_hsotg_req, queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001107}
1108
1109/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001110 * dwc2_gadget_start_next_request - Starts next request from ep queue
1111 * @hs_ep: Endpoint structure
1112 *
1113 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1114 * in its handler. Hence we need to unmask it here to be able to do
1115 * resynchronization.
1116 */
1117static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1118{
1119 u32 mask;
1120 struct dwc2_hsotg *hsotg = hs_ep->parent;
1121 int dir_in = hs_ep->dir_in;
1122 struct dwc2_hsotg_req *hs_req;
1123 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1124
1125 if (!list_empty(&hs_ep->queue)) {
1126 hs_req = get_ep_head(hs_ep);
1127 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1128 return;
1129 }
1130 if (!hs_ep->isochronous)
1131 return;
1132
1133 if (dir_in) {
1134 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1135 __func__);
1136 } else {
1137 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1138 __func__);
1139 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1140 mask |= DOEPMSK_OUTTKNEPDISMSK;
1141 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1142 }
1143}
1144
1145/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001146 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001147 * @hsotg: The device state
1148 * @ctrl: USB control request
1149 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001150static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001151 struct usb_ctrlrequest *ctrl)
1152{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001153 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1154 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001155 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001156 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001157 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001158 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001159 u32 recip;
1160 u32 wValue;
1161 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001162
1163 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1164 __func__, set ? "SET" : "CLEAR");
1165
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001166 wValue = le16_to_cpu(ctrl->wValue);
1167 wIndex = le16_to_cpu(ctrl->wIndex);
1168 recip = ctrl->bRequestType & USB_RECIP_MASK;
1169
1170 switch (recip) {
1171 case USB_RECIP_DEVICE:
1172 switch (wValue) {
1173 case USB_DEVICE_TEST_MODE:
1174 if ((wIndex & 0xff) != 0)
1175 return -EINVAL;
1176 if (!set)
1177 return -EINVAL;
1178
1179 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001180 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001181 if (ret) {
1182 dev_err(hsotg->dev,
1183 "%s: failed to send reply\n", __func__);
1184 return ret;
1185 }
1186 break;
1187 default:
1188 return -ENOENT;
1189 }
1190 break;
1191
1192 case USB_RECIP_ENDPOINT:
1193 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001194 if (!ep) {
1195 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001196 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001197 return -ENOENT;
1198 }
1199
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001200 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001201 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001202 halted = ep->halted;
1203
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001204 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001205
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001206 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001207 if (ret) {
1208 dev_err(hsotg->dev,
1209 "%s: failed to send reply\n", __func__);
1210 return ret;
1211 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001212
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001213 /*
1214 * we have to complete all requests for ep if it was
1215 * halted, and the halt was cleared by CLEAR_FEATURE
1216 */
1217
1218 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001219 /*
1220 * If we have request in progress,
1221 * then complete it
1222 */
1223 if (ep->req) {
1224 hs_req = ep->req;
1225 ep->req = NULL;
1226 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001227 if (hs_req->req.complete) {
1228 spin_unlock(&hsotg->lock);
1229 usb_gadget_giveback_request(
1230 &ep->ep, &hs_req->req);
1231 spin_lock(&hsotg->lock);
1232 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001233 }
1234
1235 /* If we have pending request, then start it */
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001236 if (!ep->req) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001237 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001238 }
1239 }
1240
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001241 break;
1242
1243 default:
1244 return -ENOENT;
1245 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001246 break;
1247 default:
1248 return -ENOENT;
1249 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001250 return 1;
1251}
1252
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001253static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001254
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001255/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001256 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001257 * @hsotg: The device state
1258 *
1259 * Set stall for ep0 as response for setup request.
1260 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001261static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001262{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001263 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001264 u32 reg;
1265 u32 ctrl;
1266
1267 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1268 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1269
1270 /*
1271 * DxEPCTL_Stall will be cleared by EP once it has
1272 * taken effect, so no need to clear later.
1273 */
1274
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001275 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001276 ctrl |= DXEPCTL_STALL;
1277 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001278 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001279
1280 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001281 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001282 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001283
1284 /*
1285 * complete won't be called, so we enqueue
1286 * setup request here
1287 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001288 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001289}
1290
1291/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001292 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001293 * @hsotg: The device state
1294 * @ctrl: The control request received
1295 *
1296 * The controller has received the SETUP phase of a control request, and
1297 * needs to work out what to do next (and whether to pass it on to the
1298 * gadget driver).
1299 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001300static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001301 struct usb_ctrlrequest *ctrl)
1302{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001303 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001304 int ret = 0;
1305 u32 dcfg;
1306
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001307 dev_dbg(hsotg->dev,
1308 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1309 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1310 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001311
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001312 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001313 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001314 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1315 } else if (ctrl->bRequestType & USB_DIR_IN) {
1316 ep0->dir_in = 1;
1317 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1318 } else {
1319 ep0->dir_in = 0;
1320 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1321 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001322
1323 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1324 switch (ctrl->bRequest) {
1325 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001326 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001327 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001328 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001329 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1330 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001331 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001332
1333 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1334
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001335 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001336 return;
1337
1338 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001339 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001340 break;
1341
1342 case USB_REQ_CLEAR_FEATURE:
1343 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001344 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001345 break;
1346 }
1347 }
1348
1349 /* as a fallback, try delivering it to the driver to deal with */
1350
1351 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001352 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001353 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001354 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001355 if (ret < 0)
1356 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1357 }
1358
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001359 /*
1360 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001361 * so respond with a STALL for the status stage to indicate failure.
1362 */
1363
Robert Baldygac9f721b2014-01-14 08:36:00 +01001364 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001365 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001366}
1367
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001368/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001369 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001370 * @ep: The endpoint the request was on.
1371 * @req: The request completed.
1372 *
1373 * Called on completion of any requests the driver itself submitted for
1374 * EP0 setup packets
1375 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001376static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001377 struct usb_request *req)
1378{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001379 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001380 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001381
1382 if (req->status < 0) {
1383 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1384 return;
1385 }
1386
Robert Baldyga93f599f2013-11-21 13:49:17 +01001387 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001388 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001389 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001390 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001391 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001392 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001393}
1394
1395/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001396 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001397 * @hsotg: The device state.
1398 *
1399 * Enqueue a request on EP0 if necessary to received any SETUP packets
1400 * received from the host.
1401 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001402static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001403{
1404 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001405 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001406 int ret;
1407
1408 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1409
1410 req->zero = 0;
1411 req->length = 8;
1412 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001413 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001414
1415 if (!list_empty(&hs_req->queue)) {
1416 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1417 return;
1418 }
1419
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001420 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001421 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001422 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001423
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001424 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001425 if (ret < 0) {
1426 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001427 /*
1428 * Don't think there's much we can do other than watch the
1429 * driver fail.
1430 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001431 }
1432}
1433
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001434static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1435 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001436{
1437 u32 ctrl;
1438 u8 index = hs_ep->index;
1439 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1440 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1441
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001442 if (hs_ep->dir_in)
1443 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
1444 index);
1445 else
1446 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
1447 index);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001448
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001449 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1450 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1451 epsiz_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001452
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001453 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001454 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1455 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1456 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001457 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001458}
1459
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001460/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001461 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001462 * @hsotg: The device state.
1463 * @hs_ep: The endpoint the request was on.
1464 * @hs_req: The request to complete.
1465 * @result: The result code (0 => Ok, otherwise errno)
1466 *
1467 * The given request has finished, so call the necessary completion
1468 * if it has one and then look to see if we can start a new request
1469 * on the endpoint.
1470 *
1471 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001472 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001473static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1474 struct dwc2_hsotg_ep *hs_ep,
1475 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001476 int result)
1477{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001478
1479 if (!hs_req) {
1480 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1481 return;
1482 }
1483
1484 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1485 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1486
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001487 /*
1488 * only replace the status if we've not already set an error
1489 * from a previous transaction
1490 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001491
1492 if (hs_req->req.status == -EINPROGRESS)
1493 hs_req->req.status = result;
1494
Yunzhi Li44583fe2015-09-29 12:25:01 +02001495 if (using_dma(hsotg))
1496 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1497
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001498 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001499
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001500 hs_ep->req = NULL;
1501 list_del_init(&hs_req->queue);
1502
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001503 /*
1504 * call the complete request with the locks off, just in case the
1505 * request tries to queue more work for this endpoint.
1506 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001507
1508 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001509 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001510 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001511 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001512 }
1513
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001514 /*
1515 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001516 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001517 * so be careful when doing this.
1518 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001519
1520 if (!hs_ep->req && result >= 0) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001521 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001522 }
1523}
1524
1525/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001526 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001527 * @hsotg: The device state.
1528 * @ep_idx: The endpoint index for the data
1529 * @size: The size of data in the fifo, in bytes
1530 *
1531 * The FIFO status shows there is data to read from the FIFO for a given
1532 * endpoint, so sort out whether we need to read the data into a request
1533 * that has been made for that endpoint.
1534 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001535static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001536{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001537 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
1538 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001539 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001540 int to_read;
1541 int max_req;
1542 int read_ptr;
1543
Lukasz Majewski22258f42012-06-14 10:02:24 +02001544
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001545 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001546 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001547 int ptr;
1548
Robert Baldyga6b448af2014-12-16 11:51:44 +01001549 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001550 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001551 __func__, size, ep_idx, epctl);
1552
1553 /* dump the data from the FIFO, we've nothing we can do */
1554 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001555 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001556
1557 return;
1558 }
1559
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001560 to_read = size;
1561 read_ptr = hs_req->req.actual;
1562 max_req = hs_req->req.length - read_ptr;
1563
Ben Dooksa33e7132010-07-19 09:40:49 +01001564 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1565 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1566
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001567 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001568 /*
1569 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001570 * to deal with in this request.
1571 */
1572
1573 /* currently we don't deal this */
1574 WARN_ON_ONCE(1);
1575 }
1576
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001577 hs_ep->total_data += to_read;
1578 hs_req->req.actual += to_read;
1579 to_read = DIV_ROUND_UP(to_read, 4);
1580
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001581 /*
1582 * note, we might over-write the buffer end by 3 bytes depending on
1583 * alignment of the data.
1584 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001585 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001586}
1587
1588/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001589 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001590 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001591 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001592 *
1593 * Generate a zero-length IN packet request for terminating a SETUP
1594 * transaction.
1595 *
1596 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001597 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001598 * the TxFIFO.
1599 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001600static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001601{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001602 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001603 hsotg->eps_out[0]->dir_in = dir_in;
1604 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001605
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001606 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001607}
1608
Roman Bacikec1f9d92015-09-10 18:13:43 -07001609static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
1610 u32 epctl_reg)
1611{
1612 u32 ctrl;
1613
1614 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
1615 if (ctrl & DXEPCTL_EOFRNUM)
1616 ctrl |= DXEPCTL_SETEVENFR;
1617 else
1618 ctrl |= DXEPCTL_SETODDFR;
1619 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
1620}
1621
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001622/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001623 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001624 * @hsotg: The device instance
1625 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001626 *
1627 * The RXFIFO has delivered an OutDone event, which means that the data
1628 * transfer for an OUT endpoint has been completed, either by a short
1629 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001630 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001631static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001632{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001633 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001634 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
1635 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001636 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001637 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001638 int result = 0;
1639
1640 if (!hs_req) {
1641 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1642 return;
1643 }
1644
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001645 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1646 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001647 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1648 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001649 return;
1650 }
1651
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001652 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001653 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001654
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001655 /*
1656 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001657 * is left in the endpoint size register and then working it
1658 * out from the amount we loaded for the transfer.
1659 *
1660 * We need to do this as DMA pointers are always 32bit aligned
1661 * so may overshoot/undershoot the transfer.
1662 */
1663
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001664 size_done = hs_ep->size_loaded - size_left;
1665 size_done += hs_ep->last_load;
1666
1667 req->actual = size_done;
1668 }
1669
Ben Dooksa33e7132010-07-19 09:40:49 +01001670 /* if there is more request to do, schedule new transfer */
1671 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001672 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01001673 return;
1674 }
1675
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001676 if (req->actual < req->length && req->short_not_ok) {
1677 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1678 __func__, req->actual, req->length);
1679
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001680 /*
1681 * todo - what should we return here? there's no one else
1682 * even bothering to check the status.
1683 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001684 }
1685
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001686 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1687 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001688 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001689 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001690 }
1691
Roman Bacikec1f9d92015-09-10 18:13:43 -07001692 /*
1693 * Slave mode OUT transfers do not go through XferComplete so
1694 * adjust the ISOC parity here.
1695 */
1696 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07001697 if (hs_ep->isochronous && hs_ep->interval == 1)
1698 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001699 else if (hs_ep->isochronous && hs_ep->interval > 1)
1700 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07001701 }
1702
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001703 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001704}
1705
1706/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001707 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001708 * @hsotg: The device instance
1709 *
1710 * The IRQ handler has detected that the RX FIFO has some data in it
1711 * that requires processing, so find out what is in there and do the
1712 * appropriate read.
1713 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001714 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001715 * chunks, so if you have x packets received on an endpoint you'll get x
1716 * FIFO events delivered, each with a packet's worth of data in it.
1717 *
1718 * When using DMA, we should not be processing events from the RXFIFO
1719 * as the actual data should be sent to the memory directly and we turn
1720 * on the completion interrupts to get notifications of transfer completion.
1721 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001722static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001723{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001724 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001725 u32 epnum, status, size;
1726
1727 WARN_ON(using_dma(hsotg));
1728
Dinh Nguyen47a16852014-04-14 14:13:34 -07001729 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1730 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001731
Dinh Nguyen47a16852014-04-14 14:13:34 -07001732 size = grxstsr & GRXSTS_BYTECNT_MASK;
1733 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001734
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01001735 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001736 __func__, grxstsr, size, epnum);
1737
Dinh Nguyen47a16852014-04-14 14:13:34 -07001738 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1739 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1740 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001741 break;
1742
Dinh Nguyen47a16852014-04-14 14:13:34 -07001743 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001744 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001745 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001746
1747 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001748 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001749 break;
1750
Dinh Nguyen47a16852014-04-14 14:13:34 -07001751 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001752 dev_dbg(hsotg->dev,
1753 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001754 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001755 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001756 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001757 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001758 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1759 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1760 */
1761 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001762 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001763 break;
1764
Dinh Nguyen47a16852014-04-14 14:13:34 -07001765 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001766 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001767 break;
1768
Dinh Nguyen47a16852014-04-14 14:13:34 -07001769 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001770 dev_dbg(hsotg->dev,
1771 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001772 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001773 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001774
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001775 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1776
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001777 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001778 break;
1779
1780 default:
1781 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1782 __func__, grxstsr);
1783
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001784 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001785 break;
1786 }
1787}
1788
1789/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001790 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001791 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001792 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001793static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001794{
1795 switch (mps) {
1796 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001797 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001798 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001799 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001800 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001801 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001802 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001803 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001804 }
1805
1806 /* bad max packet size, warn and return invalid result */
1807 WARN_ON(1);
1808 return (u32)-1;
1809}
1810
1811/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001812 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001813 * @hsotg: The driver state.
1814 * @ep: The index number of the endpoint
1815 * @mps: The maximum packet size in bytes
1816 *
1817 * Configure the maximum packet size for the given endpoint, updating
1818 * the hardware control registers to reflect this.
1819 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001820static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001821 unsigned int ep, unsigned int mps, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001822{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001823 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001824 void __iomem *regs = hsotg->regs;
1825 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001826 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001827 u32 reg;
1828
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001829 hs_ep = index_to_ep(hsotg, ep, dir_in);
1830 if (!hs_ep)
1831 return;
1832
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001833 if (ep == 0) {
1834 /* EP0 is a special case */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001835 mpsval = dwc2_hsotg_ep0_mps(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001836 if (mpsval > 3)
1837 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001838 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001839 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001840 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001841 mpsval = mps & DXEPCTL_MPS_MASK;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001842 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001843 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001844 mcval = ((mps >> 11) & 0x3) + 1;
1845 hs_ep->mc = mcval;
1846 if (mcval > 3)
1847 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001848 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001849 }
1850
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001851 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001852 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001853 reg &= ~DXEPCTL_MPS_MASK;
1854 reg |= mpsval;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001855 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001856 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001857 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001858 reg &= ~DXEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001859 reg |= mpsval;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001860 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001861 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001862
1863 return;
1864
1865bad_mps:
1866 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1867}
1868
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001869/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001870 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001871 * @hsotg: The driver state
1872 * @idx: The index for the endpoint (0..15)
1873 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001874static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001875{
1876 int timeout;
1877 int val;
1878
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001879 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
1880 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001881
1882 /* wait until the fifo is flushed */
1883 timeout = 100;
1884
1885 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001886 val = dwc2_readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001887
Dinh Nguyen47a16852014-04-14 14:13:34 -07001888 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001889 break;
1890
1891 if (--timeout == 0) {
1892 dev_err(hsotg->dev,
1893 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1894 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02001895 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001896 }
1897
1898 udelay(1);
1899 }
1900}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001901
1902/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001903 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001904 * @hsotg: The driver state
1905 * @hs_ep: The driver endpoint to check.
1906 *
1907 * Check to see if there is a request that has data to send, and if so
1908 * make an attempt to write data into the FIFO.
1909 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001910static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
1911 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001912{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001913 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001914
Robert Baldygaafcf4162013-09-19 11:50:19 +02001915 if (!hs_ep->dir_in || !hs_req) {
1916 /**
1917 * if request is not enqueued, we disable interrupts
1918 * for endpoints, excepting ep0
1919 */
1920 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001921 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
Robert Baldygaafcf4162013-09-19 11:50:19 +02001922 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001923 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001924 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001925
1926 if (hs_req->req.actual < hs_req->req.length) {
1927 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1928 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001929 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001930 }
1931
1932 return 0;
1933}
1934
1935/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001936 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001937 * @hsotg: The device state.
1938 * @hs_ep: The endpoint that has just completed.
1939 *
1940 * An IN transfer has been completed, update the transfer's state and then
1941 * call the relevant completion routines.
1942 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001943static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
1944 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001945{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001946 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001947 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001948 int size_left, size_done;
1949
1950 if (!hs_req) {
1951 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1952 return;
1953 }
1954
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001955 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001956 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
1957 dev_dbg(hsotg->dev, "zlp packet sent\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001958 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001959 if (hsotg->test_mode) {
1960 int ret;
1961
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001962 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001963 if (ret < 0) {
1964 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
1965 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001966 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001967 return;
1968 }
1969 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001970 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001971 return;
1972 }
1973
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001974 /*
1975 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001976 * in the endpoint size register and then working it out from
1977 * the amount we loaded for the transfer.
1978 *
1979 * We do this even for DMA, as the transfer may have incremented
1980 * past the end of the buffer (DMA transfers are always 32bit
1981 * aligned).
1982 */
1983
Dinh Nguyen47a16852014-04-14 14:13:34 -07001984 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001985
1986 size_done = hs_ep->size_loaded - size_left;
1987 size_done += hs_ep->last_load;
1988
1989 if (hs_req->req.actual != size_done)
1990 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1991 __func__, hs_req->req.actual, size_done);
1992
1993 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001994 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1995 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001996
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001997 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1998 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001999 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002000 return;
2001 }
2002
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002003 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002004 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002005 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002006 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002007 /* transfer will be completed on next complete interrupt */
2008 return;
2009 }
2010
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002011 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2012 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002013 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002014 return;
2015 }
2016
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002017 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002018}
2019
2020/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002021 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2022 * @hsotg: The device state.
2023 * @idx: Index of ep.
2024 * @dir_in: Endpoint direction 1-in 0-out.
2025 *
2026 * Reads for endpoint with given index and direction, by masking
2027 * epint_reg with coresponding mask.
2028 */
2029static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2030 unsigned int idx, int dir_in)
2031{
2032 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2033 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2034 u32 ints;
2035 u32 mask;
2036 u32 diepempmsk;
2037
2038 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2039 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2040 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2041 mask |= DXEPINT_SETUP_RCVD;
2042
2043 ints = dwc2_readl(hsotg->regs + epint_reg);
2044 ints &= mask;
2045 return ints;
2046}
2047
2048/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002049 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2050 * @hs_ep: The endpoint on which interrupt is asserted.
2051 *
2052 * This interrupt indicates that the endpoint has been disabled per the
2053 * application's request.
2054 *
2055 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2056 * in case of ISOC completes current request.
2057 *
2058 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2059 * request starts it.
2060 */
2061static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2062{
2063 struct dwc2_hsotg *hsotg = hs_ep->parent;
2064 struct dwc2_hsotg_req *hs_req;
2065 unsigned char idx = hs_ep->index;
2066 int dir_in = hs_ep->dir_in;
2067 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2068 int dctl = dwc2_readl(hsotg->regs + DCTL);
2069
2070 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2071
2072 if (dir_in) {
2073 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2074
2075 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2076
2077 if (hs_ep->isochronous) {
2078 dwc2_hsotg_complete_in(hsotg, hs_ep);
2079 return;
2080 }
2081
2082 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2083 int dctl = dwc2_readl(hsotg->regs + DCTL);
2084
2085 dctl |= DCTL_CGNPINNAK;
2086 dwc2_writel(dctl, hsotg->regs + DCTL);
2087 }
2088 return;
2089 }
2090
2091 if (dctl & DCTL_GOUTNAKSTS) {
2092 dctl |= DCTL_CGOUTNAK;
2093 dwc2_writel(dctl, hsotg->regs + DCTL);
2094 }
2095
2096 if (!hs_ep->isochronous)
2097 return;
2098
2099 if (list_empty(&hs_ep->queue)) {
2100 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2101 __func__, hs_ep);
2102 return;
2103 }
2104
2105 do {
2106 hs_req = get_ep_head(hs_ep);
2107 if (hs_req)
2108 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2109 -ENODATA);
2110 dwc2_gadget_incr_frame_num(hs_ep);
2111 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2112
2113 dwc2_gadget_start_next_request(hs_ep);
2114}
2115
2116/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002117 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2118 * @hs_ep: The endpoint on which interrupt is asserted.
2119 *
2120 * This is starting point for ISOC-OUT transfer, synchronization done with
2121 * first out token received from host while corresponding EP is disabled.
2122 *
2123 * Device does not know initial frame in which out token will come. For this
2124 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2125 * getting this interrupt SW starts calculation for next transfer frame.
2126 */
2127static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2128{
2129 struct dwc2_hsotg *hsotg = ep->parent;
2130 int dir_in = ep->dir_in;
2131 u32 doepmsk;
2132
2133 if (dir_in || !ep->isochronous)
2134 return;
2135
2136 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
2137
2138 if (ep->interval > 1 &&
2139 ep->target_frame == TARGET_FRAME_INITIAL) {
2140 u32 dsts;
2141 u32 ctrl;
2142
2143 dsts = dwc2_readl(hsotg->regs + DSTS);
2144 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2145 dwc2_gadget_incr_frame_num(ep);
2146
2147 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2148 if (ep->target_frame & 0x1)
2149 ctrl |= DXEPCTL_SETODDFR;
2150 else
2151 ctrl |= DXEPCTL_SETEVENFR;
2152
2153 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2154 }
2155
2156 dwc2_gadget_start_next_request(ep);
2157 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2158 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2159 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2160}
2161
2162/**
2163* dwc2_gadget_handle_nak - handle NAK interrupt
2164* @hs_ep: The endpoint on which interrupt is asserted.
2165*
2166* This is starting point for ISOC-IN transfer, synchronization done with
2167* first IN token received from host while corresponding EP is disabled.
2168*
2169* Device does not know when first one token will arrive from host. On first
2170* token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2171* and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2172* sent in response to that as there was no data in FIFO. SW is basing on this
2173* interrupt to obtain frame in which token has come and then based on the
2174* interval calculates next frame for transfer.
2175*/
2176static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2177{
2178 struct dwc2_hsotg *hsotg = hs_ep->parent;
2179 int dir_in = hs_ep->dir_in;
2180
2181 if (!dir_in || !hs_ep->isochronous)
2182 return;
2183
2184 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
2185 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2186 if (hs_ep->interval > 1) {
2187 u32 ctrl = dwc2_readl(hsotg->regs +
2188 DIEPCTL(hs_ep->index));
2189 if (hs_ep->target_frame & 0x1)
2190 ctrl |= DXEPCTL_SETODDFR;
2191 else
2192 ctrl |= DXEPCTL_SETEVENFR;
2193
2194 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2195 }
2196
2197 dwc2_hsotg_complete_request(hsotg, hs_ep,
2198 get_ep_head(hs_ep), 0);
2199 }
2200
2201 dwc2_gadget_incr_frame_num(hs_ep);
2202}
2203
2204/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002205 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002206 * @hsotg: The driver state
2207 * @idx: The index for the endpoint (0..15)
2208 * @dir_in: Set if this is an IN endpoint
2209 *
2210 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002211 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002212static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002213 int dir_in)
2214{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002215 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002216 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2217 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2218 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002219 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002220 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002221
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002222 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002223 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002224
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002225 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002226 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002227
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002228 if (!hs_ep) {
2229 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
2230 __func__, idx, dir_in ? "in" : "out");
2231 return;
2232 }
2233
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002234 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2235 __func__, idx, dir_in ? "in" : "out", ints);
2236
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002237 /* Don't process XferCompl interrupt if it is a setup packet */
2238 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2239 ints &= ~DXEPINT_XFERCOMPL;
2240
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002241 if (ints & DXEPINT_STSPHSERCVD)
2242 dev_dbg(hsotg->dev, "%s: StsPhseRcvd asserted\n", __func__);
Robert Baldyga1479e842013-10-09 08:41:57 +02002243
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002244 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002245 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002246 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002247 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2248 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002249
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002250 /*
2251 * we get OutDone from the FIFO, so we only need to look
2252 * at completing IN requests here
2253 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002254 if (dir_in) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002255 if (hs_ep->isochronous && hs_ep->interval > 1)
2256 dwc2_gadget_incr_frame_num(hs_ep);
2257
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002258 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002259 if (ints & DXEPINT_NAKINTRPT)
2260 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002261
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002262 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002263 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002264 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002265 /*
2266 * We're using DMA, we need to fire an OutDone here
2267 * as we ignore the RXFIFO.
2268 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002269 if (hs_ep->isochronous && hs_ep->interval > 1)
2270 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002271
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002272 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002273 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002274 }
2275
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002276 if (ints & DXEPINT_EPDISBLD)
2277 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002278
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002279 if (ints & DXEPINT_OUTTKNEPDIS)
2280 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2281
2282 if (ints & DXEPINT_NAKINTRPT)
2283 dwc2_gadget_handle_nak(hs_ep);
2284
Dinh Nguyen47a16852014-04-14 14:13:34 -07002285 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002286 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002287
Dinh Nguyen47a16852014-04-14 14:13:34 -07002288 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002289 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2290
2291 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002292 /*
2293 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002294 * setup packet. In non-DMA mode we'd get this
2295 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002296 * the setup here.
2297 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002298
2299 if (dir_in)
2300 WARN_ON_ONCE(1);
2301 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002302 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002303 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002304 }
2305
Dinh Nguyen47a16852014-04-14 14:13:34 -07002306 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002307 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002308
Robert Baldyga1479e842013-10-09 08:41:57 +02002309 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002310 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002311 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002312 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2313 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002314 }
2315
2316 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002317 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002318 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2319 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002320 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002321
2322 /* FIFO has space or is empty (see GAHBCFG) */
2323 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002324 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002325 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2326 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002327 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002328 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002329 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002330 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002331}
2332
2333/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002334 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002335 * @hsotg: The device state.
2336 *
2337 * Handle updating the device settings after the enumeration phase has
2338 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002339 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002340static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002341{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002342 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09002343 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002344
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002345 /*
2346 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002347 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002348 * we connected at.
2349 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002350
2351 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2352
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002353 /*
2354 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002355 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002356 * not advertise a 64byte MPS on EP0.
2357 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002358
2359 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01002360 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002361 case DSTS_ENUMSPD_FS:
2362 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002363 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002364 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002365 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002366 break;
2367
Dinh Nguyen47a16852014-04-14 14:13:34 -07002368 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002369 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002370 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002371 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002372 break;
2373
Dinh Nguyen47a16852014-04-14 14:13:34 -07002374 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002375 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002376 /*
2377 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002378 * moment, and the documentation seems to imply that it isn't
2379 * supported by the PHYs on some of the devices.
2380 */
2381 break;
2382 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002383 dev_info(hsotg->dev, "new device is %s\n",
2384 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002385
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002386 /*
2387 * we should now know the maximum packet size for an
2388 * endpoint, so set the endpoints to a default value.
2389 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002390
2391 if (ep0_mps) {
2392 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002393 /* Initialize ep0 for both in and out directions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002394 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
2395 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002396 for (i = 1; i < hsotg->num_of_eps; i++) {
2397 if (hsotg->eps_in[i])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002398 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002399 if (hsotg->eps_out[i])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002400 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002401 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002402 }
2403
2404 /* ensure after enumeration our EP0 is active */
2405
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002406 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002407
2408 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002409 dwc2_readl(hsotg->regs + DIEPCTL0),
2410 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002411}
2412
2413/**
2414 * kill_all_requests - remove all requests from the endpoint's queue
2415 * @hsotg: The device state.
2416 * @ep: The endpoint the requests may be on.
2417 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002418 *
2419 * Go through the requests on the given endpoint and mark them
2420 * completed with the given result code.
2421 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002422static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002423 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01002424 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002425{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002426 struct dwc2_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002427 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002428
Robert Baldyga6b448af2014-12-16 11:51:44 +01002429 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002430
Robert Baldyga6b448af2014-12-16 11:51:44 +01002431 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002432 dwc2_hsotg_complete_request(hsotg, ep, req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002433 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01002434
Robert Baldygab203d0a2014-09-09 10:44:56 +02002435 if (!hsotg->dedicated_fifos)
2436 return;
Robert Baldygaad674a12016-08-29 13:38:50 -07002437 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->fifo_index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002438 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002439 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002440}
2441
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002442/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002443 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002444 * @hsotg: The device state.
2445 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002446 * The device has been disconnected. Remove all current
2447 * transactions and signal the gadget driver that this
2448 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002449 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002450void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002451{
2452 unsigned ep;
2453
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002454 if (!hsotg->connected)
2455 return;
2456
2457 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002458 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002459
2460 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2461 if (hsotg->eps_in[ep])
2462 kill_all_requests(hsotg, hsotg->eps_in[ep],
2463 -ESHUTDOWN);
2464 if (hsotg->eps_out[ep])
2465 kill_all_requests(hsotg, hsotg->eps_out[ep],
2466 -ESHUTDOWN);
2467 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002468
2469 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02002470 hsotg->lx_state = DWC2_L3;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002471}
2472
2473/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002474 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002475 * @hsotg: The device state:
2476 * @periodic: True if this is a periodic FIFO interrupt
2477 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002478static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002479{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002480 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002481 int epno, ret;
2482
2483 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002484 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002485 ep = index_to_ep(hsotg, epno, 1);
2486
2487 if (!ep)
2488 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002489
2490 if (!ep->dir_in)
2491 continue;
2492
2493 if ((periodic && !ep->periodic) ||
2494 (!periodic && ep->periodic))
2495 continue;
2496
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002497 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002498 if (ret < 0)
2499 break;
2500 }
2501}
2502
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002503/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002504#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2505 GINTSTS_PTXFEMP | \
2506 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002507
2508/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002509 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002510 * @hsotg: The device state
2511 *
2512 * Issue a soft reset to the core, and await the core finishing it.
2513 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002514void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002515 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002516{
Gregory Herrero1ee69032015-09-29 12:08:27 +02002517 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002518 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002519 u32 usbcfg;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002520
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02002521 /* Kill any ep0 requests as controller will be reinitialized */
2522 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
2523
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002524 if (!is_usb_reset)
John Youn241729b2015-12-17 11:17:59 -08002525 if (dwc2_core_reset(hsotg))
Gregory Herrero86de4892015-09-29 12:08:21 +02002526 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002527
2528 /*
2529 * we must now enable ep0 ready for host detection and then
2530 * set configuration.
2531 */
2532
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002533 /* keep other bits untouched (so e.g. forced modes are not lost) */
2534 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2535 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
2536 GUSBCFG_HNPCAP);
2537
Lukasz Majewski308d7342012-05-04 14:17:05 +02002538 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01002539 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002540 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
2541 (val << GUSBCFG_USBTRDTIM_SHIFT);
2542 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002543
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002544 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002545
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002546 if (!is_usb_reset)
2547 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002548
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002549 dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002550
2551 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002552 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002553
2554 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002555 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02002556 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002557 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02002558 GINTSTS_USBRST | GINTSTS_RESETDET |
2559 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Roman Bacikec1f9d92015-09-10 18:13:43 -07002560 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
2561 GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02002562
2563 if (hsotg->core_params->external_id_pin_ctl <= 0)
2564 intmsk |= GINTSTS_CONIDSTSCHNG;
2565
2566 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002567
2568 if (using_dma(hsotg))
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002569 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2570 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
2571 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002572 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002573 dwc2_writel(((hsotg->dedicated_fifos) ?
2574 (GAHBCFG_NP_TXF_EMP_LVL |
2575 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2576 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002577
2578 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002579 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2580 * when we have no data to transfer. Otherwise we get being flooded by
2581 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002582 */
2583
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002584 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01002585 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002586 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002587 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002588 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002589
2590 /*
2591 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2592 * DMA mode we may need this.
2593 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002594 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002595 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002596 DOEPMSK_SETUPMSK | DOEPMSK_STSPHSERCVDMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002597 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002598
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002599 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002600
2601 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002602 dwc2_readl(hsotg->regs + DIEPCTL0),
2603 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002604
2605 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002606 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002607
2608 /*
2609 * Enable the RXFIFO when in slave mode, as this is how we collect
2610 * the data. In DMA mode, we get events from the FIFO but also
2611 * things we cannot process, so do not use it.
2612 */
2613 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002614 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002615
2616 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002617 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2618 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002619
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002620 if (!is_usb_reset) {
2621 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2622 udelay(10); /* see openiboot */
2623 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2624 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02002625
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002626 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002627
2628 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002629 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002630 * writing to the EPCTL register..
2631 */
2632
2633 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002634 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002635 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002636
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002637 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002638 DXEPCTL_CNAK | DXEPCTL_EPENA |
2639 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002640 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002641
2642 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002643 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002644 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002645
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002646 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002647
2648 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002649 dwc2_readl(hsotg->regs + DIEPCTL0),
2650 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002651
2652 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002653 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
2654 if (!is_usb_reset)
2655 val |= DCTL_SFTDISCON;
2656 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002657
2658 /* must be at-least 3ms to allow bus to see disconnect */
2659 mdelay(3);
2660
Gregory Herrero065d3932015-09-22 15:16:54 +02002661 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002662}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002663
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002664static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002665{
2666 /* set the soft-disconnect bit */
2667 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2668}
2669
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002670void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002671{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002672 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002673 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002674}
2675
2676/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07002677 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
2678 * @hsotg: The device state:
2679 *
2680 * This interrupt indicates one of the following conditions occurred while
2681 * transmitting an ISOC transaction.
2682 * - Corrupted IN Token for ISOC EP.
2683 * - Packet not complete in FIFO.
2684 *
2685 * The following actions will be taken:
2686 * - Determine the EP
2687 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
2688 */
2689static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
2690{
2691 struct dwc2_hsotg_ep *hs_ep;
2692 u32 epctrl;
2693 u32 idx;
2694
2695 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
2696
2697 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2698 hs_ep = hsotg->eps_in[idx];
2699 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
2700 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
2701 dwc2_gadget_target_frame_elapsed(hs_ep)) {
2702 epctrl |= DXEPCTL_SNAK;
2703 epctrl |= DXEPCTL_EPDIS;
2704 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
2705 }
2706 }
2707
2708 /* Clear interrupt */
2709 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
2710}
2711
2712/**
2713 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
2714 * @hsotg: The device state:
2715 *
2716 * This interrupt indicates one of the following conditions occurred while
2717 * transmitting an ISOC transaction.
2718 * - Corrupted OUT Token for ISOC EP.
2719 * - Packet not complete in FIFO.
2720 *
2721 * The following actions will be taken:
2722 * - Determine the EP
2723 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
2724 */
2725static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
2726{
2727 u32 gintsts;
2728 u32 gintmsk;
2729 u32 epctrl;
2730 struct dwc2_hsotg_ep *hs_ep;
2731 int idx;
2732
2733 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
2734
2735 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2736 hs_ep = hsotg->eps_out[idx];
2737 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
2738 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
2739 dwc2_gadget_target_frame_elapsed(hs_ep)) {
2740 /* Unmask GOUTNAKEFF interrupt */
2741 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
2742 gintmsk |= GINTSTS_GOUTNAKEFF;
2743 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
2744
2745 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2746 if (!(gintsts & GINTSTS_GOUTNAKEFF))
2747 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
2748 }
2749 }
2750
2751 /* Clear interrupt */
2752 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
2753}
2754
2755/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002756 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002757 * @irq: The IRQ number triggered
2758 * @pw: The pw value when registered the handler.
2759 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002760static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002761{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002762 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002763 int retry_count = 8;
2764 u32 gintsts;
2765 u32 gintmsk;
2766
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07002767 if (!dwc2_is_device_mode(hsotg))
2768 return IRQ_NONE;
2769
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002770 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002771irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002772 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2773 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002774
2775 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2776 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2777
2778 gintsts &= gintmsk;
2779
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02002780 if (gintsts & GINTSTS_RESETDET) {
2781 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
2782
2783 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
2784
2785 /* This event must be used only if controller is suspended */
2786 if (hsotg->lx_state == DWC2_L2) {
2787 dwc2_exit_hibernation(hsotg, true);
2788 hsotg->lx_state = DWC2_L0;
2789 }
2790 }
2791
2792 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
2793
2794 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
2795 u32 connected = hsotg->connected;
2796
2797 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
2798 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2799 dwc2_readl(hsotg->regs + GNPTXSTS));
2800
2801 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
2802
2803 /* Report disconnection if it is not already done. */
2804 dwc2_hsotg_disconnect(hsotg);
2805
2806 if (usb_status & GOTGCTL_BSESVLD && connected)
2807 dwc2_hsotg_core_init_disconnected(hsotg, true);
2808 }
2809
Dinh Nguyen47a16852014-04-14 14:13:34 -07002810 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002811 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002812
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002813 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002814 }
2815
Dinh Nguyen47a16852014-04-14 14:13:34 -07002816 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002817 u32 daint = dwc2_readl(hsotg->regs + DAINT);
2818 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02002819 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002820 int ep;
2821
Robert Baldyga7e804652013-09-19 11:50:20 +02002822 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002823 daint_out = daint >> DAINT_OUTEP_SHIFT;
2824 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002825
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002826 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2827
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002828 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2829 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002830 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002831 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002832 }
2833
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002834 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
2835 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002836 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002837 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002838 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002839 }
2840
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002841 /* check both FIFOs */
2842
Dinh Nguyen47a16852014-04-14 14:13:34 -07002843 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002844 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2845
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002846 /*
2847 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002848 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002849 * it needs re-enabling
2850 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002851
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002852 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
2853 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002854 }
2855
Dinh Nguyen47a16852014-04-14 14:13:34 -07002856 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002857 dev_dbg(hsotg->dev, "PTxFEmp\n");
2858
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002859 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002860
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002861 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
2862 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002863 }
2864
Dinh Nguyen47a16852014-04-14 14:13:34 -07002865 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002866 /*
2867 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002868 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002869 * set.
2870 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002871
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002872 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002873 }
2874
Dinh Nguyen47a16852014-04-14 14:13:34 -07002875 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002876 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002877 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002878 }
2879
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002880 /*
2881 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002882 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002883 * the occurrence.
2884 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002885
Dinh Nguyen47a16852014-04-14 14:13:34 -07002886 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002887 u8 idx;
2888 u32 epctrl;
2889 u32 gintmsk;
2890 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002891
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002892 /* Mask this interrupt */
2893 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
2894 gintmsk &= ~GINTSTS_GOUTNAKEFF;
2895 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002896
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002897 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
2898 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2899 hs_ep = hsotg->eps_out[idx];
2900 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
2901
2902 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
2903 epctrl |= DXEPCTL_SNAK;
2904 epctrl |= DXEPCTL_EPDIS;
2905 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
2906 }
2907 }
2908
2909 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002910 }
2911
Dinh Nguyen47a16852014-04-14 14:13:34 -07002912 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002913 dev_info(hsotg->dev, "GINNakEff triggered\n");
2914
Gregory Herrero3be99cd2015-12-07 12:07:31 +01002915 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002916
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002917 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002918 }
2919
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07002920 if (gintsts & GINTSTS_INCOMPL_SOIN)
2921 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002922
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07002923 if (gintsts & GINTSTS_INCOMPL_SOOUT)
2924 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002925
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002926 /*
2927 * if we've had fifo events, we should try and go around the
2928 * loop again to see if there's any point in returning yet.
2929 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002930
2931 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2932 goto irq_retry;
2933
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002934 spin_unlock(&hsotg->lock);
2935
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002936 return IRQ_HANDLED;
2937}
2938
2939/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002940 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002941 * @ep: The USB endpint to configure
2942 * @desc: The USB endpoint descriptor to configure with.
2943 *
2944 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002945 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002946static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002947 const struct usb_endpoint_descriptor *desc)
2948{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002949 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002950 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002951 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002952 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002953 u32 epctrl_reg;
2954 u32 epctrl;
2955 u32 mps;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002956 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002957 unsigned int dir_in;
2958 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02002959 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002960
2961 dev_dbg(hsotg->dev,
2962 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2963 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2964 desc->wMaxPacketSize, desc->bInterval);
2965
2966 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07002967 if (index == 0) {
2968 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
2969 return -EINVAL;
2970 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002971
2972 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2973 if (dir_in != hs_ep->dir_in) {
2974 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2975 return -EINVAL;
2976 }
2977
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002978 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002979
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002980 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002981
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002982 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002983 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002984
2985 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2986 __func__, epctrl, epctrl_reg);
2987
Lukasz Majewski22258f42012-06-14 10:02:24 +02002988 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002989
Dinh Nguyen47a16852014-04-14 14:13:34 -07002990 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2991 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002992
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002993 /*
2994 * mark the endpoint as active, otherwise the core may ignore
2995 * transactions entirely for this endpoint
2996 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002997 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002998
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002999 /* update the endpoint state */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003000 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003001
3002 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003003 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003004 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003005 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003006 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003007
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003008 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3009 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003010 epctrl |= DXEPCTL_EPTYPE_ISO;
3011 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003012 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003013 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003014 hs_ep->target_frame = TARGET_FRAME_INITIAL;
3015 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003016 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003017 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3018 mask |= DIEPMSK_NAKMSK;
3019 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3020 } else {
3021 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3022 mask |= DOEPMSK_OUTTKNEPDISMSK;
3023 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3024 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003025 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003026
3027 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003028 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003029 break;
3030
3031 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003032 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003033 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003034
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003035 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3036 hs_ep->interval = 1 << (desc->bInterval - 1);
3037
Dinh Nguyen47a16852014-04-14 14:13:34 -07003038 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003039 break;
3040
3041 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003042 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003043 break;
3044 }
3045
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003046 /*
3047 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003048 * a unique tx-fifo even if it is non-periodic.
3049 */
Robert Baldyga21f3bb52016-08-29 13:38:57 -07003050 if (dir_in && hsotg->dedicated_fifos) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003051 u32 fifo_index = 0;
3052 u32 fifo_size = UINT_MAX;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003053 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003054 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02003055 if (hsotg->fifo_map & (1<<i))
3056 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003057 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Robert Baldygab203d0a2014-09-09 10:44:56 +02003058 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
3059 if (val < size)
3060 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003061 /* Search for smallest acceptable fifo */
3062 if (val < fifo_size) {
3063 fifo_size = val;
3064 fifo_index = i;
3065 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003066 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003067 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003068 dev_err(hsotg->dev,
3069 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303070 ret = -ENOMEM;
3071 goto error;
3072 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003073 hsotg->fifo_map |= 1 << fifo_index;
3074 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3075 hs_ep->fifo_index = fifo_index;
3076 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003077 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003078
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003079 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003080 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003081 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003082
3083 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3084 __func__, epctrl);
3085
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003086 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003087 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003088 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003089
3090 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003091 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003092
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303093error:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003094 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02003095 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003096}
3097
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003098/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003099 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003100 * @ep: The endpoint to disable.
3101 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003102static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003103{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003104 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003105 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003106 int dir_in = hs_ep->dir_in;
3107 int index = hs_ep->index;
3108 unsigned long flags;
3109 u32 epctrl_reg;
3110 u32 ctrl;
3111
Marek Szyprowski1e011292014-09-09 10:44:54 +02003112 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003113
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003114 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3116 return -EINVAL;
3117 }
3118
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003119 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003120
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003121 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003122
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003123 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07003124 ctrl &= ~DXEPCTL_EPENA;
3125 ctrl &= ~DXEPCTL_USBACTEP;
3126 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003127
3128 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003129 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003130
3131 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003132 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003133
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003134 /* terminate all requests with shutdown */
3135 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3136
Robert Baldyga1c07b202016-08-29 13:39:00 -07003137 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3138 hs_ep->fifo_index = 0;
3139 hs_ep->fifo_size = 0;
3140
Lukasz Majewski22258f42012-06-14 10:02:24 +02003141 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003142 return 0;
3143}
3144
3145/**
3146 * on_list - check request is on the given endpoint
3147 * @ep: The endpoint to check.
3148 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003149 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003150static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003151{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003152 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003153
3154 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
3155 if (req == test)
3156 return true;
3157 }
3158
3159 return false;
3160}
3161
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003162static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3163 u32 bit, u32 timeout)
3164{
3165 u32 i;
3166
3167 for (i = 0; i < timeout; i++) {
3168 if (dwc2_readl(hs_otg->regs + reg) & bit)
3169 return 0;
3170 udelay(1);
3171 }
3172
3173 return -ETIMEDOUT;
3174}
3175
3176static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3177 struct dwc2_hsotg_ep *hs_ep)
3178{
3179 u32 epctrl_reg;
3180 u32 epint_reg;
3181
3182 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3183 DOEPCTL(hs_ep->index);
3184 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3185 DOEPINT(hs_ep->index);
3186
3187 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3188 hs_ep->name);
3189 if (hs_ep->dir_in) {
3190 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3191 /* Wait for Nak effect */
3192 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3193 DXEPINT_INEPNAKEFF, 100))
3194 dev_warn(hsotg->dev,
3195 "%s: timeout DIEPINT.NAKEFF\n", __func__);
3196 } else {
Vardan Mikayelyan6b58cb02016-05-25 18:07:02 -07003197 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3198 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003199
3200 /* Wait for global nak to take effect */
3201 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
Du, Changbin0676c7e2015-12-04 15:38:23 +08003202 GINTSTS_GOUTNAKEFF, 100))
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003203 dev_warn(hsotg->dev,
Du, Changbin0676c7e2015-12-04 15:38:23 +08003204 "%s: timeout GINTSTS.GOUTNAKEFF\n", __func__);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003205 }
3206
3207 /* Disable ep */
3208 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3209
3210 /* Wait for ep to be disabled */
3211 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3212 dev_warn(hsotg->dev,
3213 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3214
3215 if (hs_ep->dir_in) {
3216 if (hsotg->dedicated_fifos) {
3217 dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) |
3218 GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL);
3219 /* Wait for fifo flush */
3220 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
3221 GRSTCTL_TXFFLSH, 100))
3222 dev_warn(hsotg->dev,
3223 "%s: timeout flushing fifos\n",
3224 __func__);
3225 }
3226 /* TODO: Flush shared tx fifo */
3227 } else {
3228 /* Remove global NAKs */
Du, Changbin0676c7e2015-12-04 15:38:23 +08003229 __bic32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003230 }
3231}
3232
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003233/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003234 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003235 * @ep: The endpoint to dequeue.
3236 * @req: The request to be removed from a queue.
3237 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003238static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003239{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003240 struct dwc2_hsotg_req *hs_req = our_req(req);
3241 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003242 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003243 unsigned long flags;
3244
Marek Szyprowski1e011292014-09-09 10:44:54 +02003245 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003246
Lukasz Majewski22258f42012-06-14 10:02:24 +02003247 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003248
3249 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02003250 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003251 return -EINVAL;
3252 }
3253
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003254 /* Dequeue already started request */
3255 if (req == &hs_ep->req->req)
3256 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
3257
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003258 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02003259 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003260
3261 return 0;
3262}
3263
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003264/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003265 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003266 * @ep: The endpoint to set halt.
3267 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003268 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
3269 * the endpoint is busy processing requests.
3270 *
3271 * We need to stall the endpoint immediately if request comes from set_feature
3272 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003273 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003274static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003275{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003276 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003277 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003278 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003279 u32 epreg;
3280 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003281 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003282
3283 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
3284
Robert Baldygac9f721b2014-01-14 08:36:00 +01003285 if (index == 0) {
3286 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003287 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01003288 else
3289 dev_warn(hs->dev,
3290 "%s: can't clear halt on ep0\n", __func__);
3291 return 0;
3292 }
3293
Vahram Aharonyan15186f12016-05-23 22:41:59 -07003294 if (hs_ep->isochronous) {
3295 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
3296 return -EINVAL;
3297 }
3298
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003299 if (!now && value && !list_empty(&hs_ep->queue)) {
3300 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
3301 ep->name);
3302 return -EAGAIN;
3303 }
3304
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003305 if (hs_ep->dir_in) {
3306 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003307 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003308
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003309 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05003310 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003311 if (epctl & DXEPCTL_EPENA)
3312 epctl |= DXEPCTL_EPDIS;
3313 } else {
3314 epctl &= ~DXEPCTL_STALL;
3315 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3316 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3317 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3318 epctl |= DXEPCTL_SETD0PID;
3319 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003320 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003321 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003322
3323 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003324 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003325
3326 if (value)
3327 epctl |= DXEPCTL_STALL;
3328 else {
3329 epctl &= ~DXEPCTL_STALL;
3330 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3331 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3332 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3333 epctl |= DXEPCTL_SETD0PID;
3334 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003335 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003336 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003337
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003338 hs_ep->halted = value;
3339
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003340 return 0;
3341}
3342
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003343/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003344 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003345 * @ep: The endpoint to set halt.
3346 * @value: Set or unset the halt.
3347 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003348static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003349{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003350 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003351 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003352 unsigned long flags = 0;
3353 int ret = 0;
3354
3355 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003356 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003357 spin_unlock_irqrestore(&hs->lock, flags);
3358
3359 return ret;
3360}
3361
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003362static struct usb_ep_ops dwc2_hsotg_ep_ops = {
3363 .enable = dwc2_hsotg_ep_enable,
3364 .disable = dwc2_hsotg_ep_disable,
3365 .alloc_request = dwc2_hsotg_ep_alloc_request,
3366 .free_request = dwc2_hsotg_ep_free_request,
3367 .queue = dwc2_hsotg_ep_queue_lock,
3368 .dequeue = dwc2_hsotg_ep_dequeue,
3369 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003370 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003371};
3372
3373/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003374 * dwc2_hsotg_init - initalize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003375 * @hsotg: The driver state
3376 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003377static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003378{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003379 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003380 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003381 /* unmask subset of endpoint interrupts */
3382
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003383 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
3384 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
3385 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003386
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003387 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
3388 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
3389 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003390
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003391 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003392
3393 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003394 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003395
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003396 /* setup fifos */
3397
3398 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003399 dwc2_readl(hsotg->regs + GRXFSIZ),
3400 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003401
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003402 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003403
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003404 /* keep other bits untouched (so e.g. forced modes are not lost) */
3405 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3406 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3407 GUSBCFG_HNPCAP);
3408
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003409 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003410 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003411 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3412 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
3413 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003414
Gregory Herrerof5090042015-01-09 13:38:47 +01003415 if (using_dma(hsotg))
3416 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003417}
3418
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003419/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003420 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003421 * @gadget: The usb gadget state
3422 * @driver: The usb gadget driver
3423 *
3424 * Perform initialization to prepare udc device and driver
3425 * to work.
3426 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003427static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003428 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003429{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003430 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003431 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003432 int ret;
3433
3434 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003435 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003436 return -ENODEV;
3437 }
3438
3439 if (!driver) {
3440 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3441 return -EINVAL;
3442 }
3443
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003444 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003445 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003446
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003447 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003448 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3449 return -EINVAL;
3450 }
3451
3452 WARN_ON(hsotg->driver);
3453
3454 driver->driver.bus = NULL;
3455 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003456 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003457 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3458
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003459 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
3460 ret = dwc2_lowlevel_hw_enable(hsotg);
3461 if (ret)
3462 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003463 }
3464
Gregory Herrerof6c01592015-01-09 13:38:41 +01003465 if (!IS_ERR_OR_NULL(hsotg->uphy))
3466 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003467
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003468 spin_lock_irqsave(&hsotg->lock, flags);
John Yound0f0ac52016-09-07 19:39:37 -07003469 if (dwc2_hw_is_device(hsotg)) {
3470 dwc2_hsotg_init(hsotg);
3471 dwc2_hsotg_core_init_disconnected(hsotg, false);
3472 }
3473
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003474 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003475 spin_unlock_irqrestore(&hsotg->lock, flags);
3476
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003477 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003478
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003479 return 0;
3480
3481err:
3482 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003483 return ret;
3484}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003485
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003486/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003487 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003488 * @gadget: The usb gadget state
3489 * @driver: The usb gadget driver
3490 *
3491 * Stop udc hw block and stay tunned for future transmissions
3492 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003493static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003494{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003495 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003496 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003497 int ep;
3498
3499 if (!hsotg)
3500 return -ENODEV;
3501
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003502 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003503 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3504 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003505 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003506 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003507 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003508 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003509
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003510 spin_lock_irqsave(&hsotg->lock, flags);
3511
Marek Szyprowski32805c32014-10-20 12:45:33 +02003512 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003513 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003514 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003515
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003516 spin_unlock_irqrestore(&hsotg->lock, flags);
3517
Gregory Herrerof6c01592015-01-09 13:38:41 +01003518 if (!IS_ERR_OR_NULL(hsotg->uphy))
3519 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003520
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003521 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3522 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003523
3524 return 0;
3525}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003526
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003527/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003528 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003529 * @gadget: The usb gadget state
3530 *
3531 * Read the {micro} frame number
3532 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003533static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003534{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003535 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003536}
3537
Lukasz Majewskia188b682012-06-22 09:29:56 +02003538/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003539 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02003540 * @gadget: The usb gadget state
3541 * @is_on: Current state of the USB PHY
3542 *
3543 * Connect/Disconnect the USB PHY pullup
3544 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003545static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02003546{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003547 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003548 unsigned long flags = 0;
3549
Gregory Herrero77ba9112015-09-29 12:08:19 +02003550 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
3551 hsotg->op_state);
3552
3553 /* Don't modify pullup state while in host mode */
3554 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3555 hsotg->enabled = is_on;
3556 return 0;
3557 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02003558
3559 spin_lock_irqsave(&hsotg->lock, flags);
3560 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003561 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003562 dwc2_hsotg_core_init_disconnected(hsotg, false);
3563 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003564 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003565 dwc2_hsotg_core_disconnect(hsotg);
3566 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003567 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02003568 }
3569
3570 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3571 spin_unlock_irqrestore(&hsotg->lock, flags);
3572
3573 return 0;
3574}
3575
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003576static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01003577{
3578 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3579 unsigned long flags;
3580
3581 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
3582 spin_lock_irqsave(&hsotg->lock, flags);
3583
Gregory Herrero61f72232015-09-29 12:08:28 +02003584 /*
3585 * If controller is hibernated, it must exit from hibernation
3586 * before being initialized / de-initialized
3587 */
3588 if (hsotg->lx_state == DWC2_L2)
3589 dwc2_exit_hibernation(hsotg, false);
3590
Gregory Herrero83d98222015-01-09 13:39:02 +01003591 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02003592 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02003593
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003594 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01003595 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003596 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003597 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003598 dwc2_hsotg_core_disconnect(hsotg);
3599 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003600 }
3601
3602 spin_unlock_irqrestore(&hsotg->lock, flags);
3603 return 0;
3604}
3605
Gregory Herrero596d6962015-01-09 13:39:08 +01003606/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003607 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01003608 * @gadget: The usb gadget state
3609 * @mA: Amount of current
3610 *
3611 * Report how much power the device may consume to the phy.
3612 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003613static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01003614{
3615 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3616
3617 if (IS_ERR_OR_NULL(hsotg->uphy))
3618 return -ENOTSUPP;
3619 return usb_phy_set_power(hsotg->uphy, mA);
3620}
3621
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003622static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
3623 .get_frame = dwc2_hsotg_gadget_getframe,
3624 .udc_start = dwc2_hsotg_udc_start,
3625 .udc_stop = dwc2_hsotg_udc_stop,
3626 .pullup = dwc2_hsotg_pullup,
3627 .vbus_session = dwc2_hsotg_vbus_session,
3628 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003629};
3630
3631/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003632 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003633 * @hsotg: The device state.
3634 * @hs_ep: The endpoint to be initialised.
3635 * @epnum: The endpoint number
3636 *
3637 * Initialise the given endpoint (as part of the probe and device state
3638 * creation) to give to the gadget driver. Setup the endpoint name, any
3639 * direction information and other state that may be required.
3640 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003641static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
3642 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003643 int epnum,
3644 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003645{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003646 char *dir;
3647
3648 if (epnum == 0)
3649 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003650 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003651 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003652 else
3653 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003654
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003655 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003656 hs_ep->index = epnum;
3657
3658 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3659
3660 INIT_LIST_HEAD(&hs_ep->queue);
3661 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3662
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003663 /* add to the list of endpoints known by the gadget driver */
3664 if (epnum)
3665 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3666
3667 hs_ep->parent = hsotg;
3668 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003669 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003670 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003671
Robert Baldyga29545222015-07-31 16:00:18 +02003672 if (epnum == 0) {
3673 hs_ep->ep.caps.type_control = true;
3674 } else {
3675 hs_ep->ep.caps.type_iso = true;
3676 hs_ep->ep.caps.type_bulk = true;
3677 hs_ep->ep.caps.type_int = true;
3678 }
3679
3680 if (dir_in)
3681 hs_ep->ep.caps.dir_in = true;
3682 else
3683 hs_ep->ep.caps.dir_out = true;
3684
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003685 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003686 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003687 * to be something valid.
3688 */
3689
3690 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003691 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003692 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003693 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003694 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003695 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003696 }
3697}
3698
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003699/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003700 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003701 * @param: The device state
3702 *
3703 * Read the USB core HW configuration registers
3704 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003705static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003706{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003707 u32 cfg;
3708 u32 ep_type;
3709 u32 i;
3710
Ben Dooks10aebc72010-07-19 09:40:44 +01003711 /* check hardware configuration */
3712
John Youn43e90342015-12-17 11:17:45 -08003713 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
3714
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003715 /* Add ep0 */
3716 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003717
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003718 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003719 GFP_KERNEL);
3720 if (!hsotg->eps_in[0])
3721 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003722 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003723 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003724
John Youn43e90342015-12-17 11:17:45 -08003725 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08003726 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003727 ep_type = cfg & 3;
3728 /* Direction in or both */
3729 if (!(ep_type & 2)) {
3730 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003731 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003732 if (!hsotg->eps_in[i])
3733 return -ENOMEM;
3734 }
3735 /* Direction out or both */
3736 if (!(ep_type & 1)) {
3737 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003738 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003739 if (!hsotg->eps_out[i])
3740 return -ENOMEM;
3741 }
3742 }
3743
John Youn43e90342015-12-17 11:17:45 -08003744 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
3745 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01003746
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003747 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3748 hsotg->num_of_eps,
3749 hsotg->dedicated_fifos ? "dedicated" : "shared",
3750 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003751 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003752}
3753
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003754/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003755 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003756 * @param: The device state
3757 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003758static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003759{
Mark Brown83a01802011-06-01 17:16:15 +01003760#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003761 struct device *dev = hsotg->dev;
3762 void __iomem *regs = hsotg->regs;
3763 u32 val;
3764 int idx;
3765
3766 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003767 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
3768 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003769
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01003770 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003771 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003772
3773 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003774 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003775
3776 /* show periodic fifo settings */
3777
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003778 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003779 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003780 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003781 val >> FIFOSIZE_DEPTH_SHIFT,
3782 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003783 }
3784
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003785 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003786 dev_info(dev,
3787 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003788 dwc2_readl(regs + DIEPCTL(idx)),
3789 dwc2_readl(regs + DIEPTSIZ(idx)),
3790 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003791
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003792 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003793 dev_info(dev,
3794 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003795 idx, dwc2_readl(regs + DOEPCTL(idx)),
3796 dwc2_readl(regs + DOEPTSIZ(idx)),
3797 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003798
3799 }
3800
3801 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003802 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003803#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003804}
3805
Gregory Herreroedd74be2015-01-09 13:38:48 +01003806#ifdef CONFIG_OF
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003807static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg)
Gregory Herreroedd74be2015-01-09 13:38:48 +01003808{
3809 struct device_node *np = hsotg->dev->of_node;
3810
3811 /* Enable dma if requested in device tree */
3812 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
Gregory Herrero0a176272015-01-09 13:38:52 +01003813
Gregory Herrero0a176272015-01-09 13:38:52 +01003814 /* Register RX fifo size */
3815 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
3816
3817 /* Register NPTX fifo size */
3818 of_property_read_u32(np, "g-np-tx-fifo-size",
3819 &hsotg->g_np_g_tx_fifo_sz);
Gregory Herreroedd74be2015-01-09 13:38:48 +01003820}
3821#else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003822static inline void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
Gregory Herreroedd74be2015-01-09 13:38:48 +01003823#endif
3824
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003825/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06003826 * dwc2_gadget_init - init function for gadget
3827 * @dwc2: The data structure for the DWC2 driver.
3828 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003829 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003830int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003831{
Dinh Nguyen117777b2014-11-11 11:13:34 -06003832 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003833 int epnum;
3834 int ret;
3835
Gregory Herrero0a176272015-01-09 13:38:52 +01003836 /* Initialize to legacy fifo configuration values */
3837 hsotg->g_rx_fifo_sz = 2048;
3838 hsotg->g_np_g_tx_fifo_sz = 1024;
Gregory Herrero0a176272015-01-09 13:38:52 +01003839 /* Device tree specific probe */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003840 dwc2_hsotg_of_probe(hsotg);
John Youn43e90342015-12-17 11:17:45 -08003841
3842 /* Check against largest possible value. */
3843 if (hsotg->g_np_g_tx_fifo_sz >
3844 hsotg->hw_params.dev_nperio_tx_fifo_size) {
3845 dev_warn(dev, "Specified GNPTXFDEP=%d > %d\n",
3846 hsotg->g_np_g_tx_fifo_sz,
3847 hsotg->hw_params.dev_nperio_tx_fifo_size);
3848 hsotg->g_np_g_tx_fifo_sz =
3849 hsotg->hw_params.dev_nperio_tx_fifo_size;
3850 }
3851
Gregory Herrero0a176272015-01-09 13:38:52 +01003852 /* Dump fifo information */
3853 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
3854 hsotg->g_np_g_tx_fifo_sz);
3855 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003856
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003857 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003858 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003859 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02003860 if (hsotg->dr_mode == USB_DR_MODE_OTG)
3861 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02003862 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3863 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003864
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003865 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003866 if (ret) {
3867 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003868 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003869 }
3870
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003871 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
3872 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02003873 if (!hsotg->ctrl_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003874 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003875
3876 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
3877 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
Wolfram Sang8bae0f82016-08-25 19:39:02 +02003878 if (!hsotg->ep0_buff)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003879 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003880
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003881 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003882 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003883 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003884 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003885 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003886 }
3887
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003888 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3889
3890 if (hsotg->num_of_eps == 0) {
3891 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003892 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003893 }
3894
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003895 /* setup endpoint information */
3896
3897 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003898 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003899
3900 /* allocate EP0 request */
3901
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003902 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003903 GFP_KERNEL);
3904 if (!hsotg->ctrl_req) {
3905 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003906 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003907 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003908
3909 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003910 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
3911 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003912 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003913 epnum, 1);
3914 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003915 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003916 epnum, 0);
3917 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003918
Dinh Nguyen117777b2014-11-11 11:13:34 -06003919 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003920 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003921 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003922
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003923 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003924
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003925 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003926}
3927
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003928/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003929 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003930 * @pdev: The platform information for the driver
3931 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003932int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003933{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003934 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003935
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003936 return 0;
3937}
3938
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003939int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003940{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003941 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003942
Gregory Herrero9e779772015-04-29 22:09:07 +02003943 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003944 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02003945
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003946 if (hsotg->driver) {
3947 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003948
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003949 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3950 hsotg->driver->driver.name);
3951
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003952 spin_lock_irqsave(&hsotg->lock, flags);
3953 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003954 dwc2_hsotg_core_disconnect(hsotg);
3955 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003956 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3957 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003958
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003959 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
3960 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003961 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003962 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003963 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003964 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003965 }
3966
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003967 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003968}
3969
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003970int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003971{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003972 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003973
Gregory Herrero9e779772015-04-29 22:09:07 +02003974 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003975 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02003976
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003977 if (hsotg->driver) {
3978 dev_info(hsotg->dev, "resuming usb gadget %s\n",
3979 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02003980
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003981 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003982 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003983 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003984 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003985 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003986 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003987
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003988 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003989}
John Youn58e52ff6a2016-02-23 19:54:57 -08003990
3991/**
3992 * dwc2_backup_device_registers() - Backup controller device registers.
3993 * When suspending usb bus, registers needs to be backuped
3994 * if controller power is disabled once suspended.
3995 *
3996 * @hsotg: Programming view of the DWC_otg controller
3997 */
3998int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
3999{
4000 struct dwc2_dregs_backup *dr;
4001 int i;
4002
4003 dev_dbg(hsotg->dev, "%s\n", __func__);
4004
4005 /* Backup dev regs */
4006 dr = &hsotg->dr_backup;
4007
4008 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4009 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4010 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4011 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4012 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4013
4014 for (i = 0; i < hsotg->num_of_eps; i++) {
4015 /* Backup IN EPs */
4016 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4017
4018 /* Ensure DATA PID is correctly configured */
4019 if (dr->diepctl[i] & DXEPCTL_DPID)
4020 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4021 else
4022 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4023
4024 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4025 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4026
4027 /* Backup OUT EPs */
4028 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4029
4030 /* Ensure DATA PID is correctly configured */
4031 if (dr->doepctl[i] & DXEPCTL_DPID)
4032 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4033 else
4034 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4035
4036 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4037 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4038 }
4039 dr->valid = true;
4040 return 0;
4041}
4042
4043/**
4044 * dwc2_restore_device_registers() - Restore controller device registers.
4045 * When resuming usb bus, device registers needs to be restored
4046 * if controller power were disabled.
4047 *
4048 * @hsotg: Programming view of the DWC_otg controller
4049 */
4050int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4051{
4052 struct dwc2_dregs_backup *dr;
4053 u32 dctl;
4054 int i;
4055
4056 dev_dbg(hsotg->dev, "%s\n", __func__);
4057
4058 /* Restore dev regs */
4059 dr = &hsotg->dr_backup;
4060 if (!dr->valid) {
4061 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4062 __func__);
4063 return -EINVAL;
4064 }
4065 dr->valid = false;
4066
4067 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4068 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4069 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4070 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4071 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4072
4073 for (i = 0; i < hsotg->num_of_eps; i++) {
4074 /* Restore IN EPs */
4075 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4076 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4077 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4078
4079 /* Restore OUT EPs */
4080 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4081 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4082 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4083 }
4084
4085 /* Set the Power-On Programming done bit */
4086 dctl = dwc2_readl(hsotg->regs + DCTL);
4087 dctl |= DCTL_PWRONPRGDONE;
4088 dwc2_writel(dctl, hsotg->regs + DCTL);
4089
4090 return 0;
4091}