blob: af46adfae41ca5fd92b1401eff078e84bf27c7aa [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{
Ben Dooks0f002d22010-05-25 05:36:50 +0100189 unsigned int ep;
190 unsigned int addr;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100191 int timeout;
Ben Dooks0f002d22010-05-25 05:36:50 +0100192 u32 val;
193
Gregory Herrero7fcbc952015-01-09 13:39:06 +0100194 /* Reset fifo map if not correctly cleared during previous session */
195 WARN_ON(hsotg->fifo_map);
196 hsotg->fifo_map = 0;
197
Gregory Herrero0a176272015-01-09 13:38:52 +0100198 /* set RX/NPTX FIFO sizes */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300199 dwc2_writel(hsotg->g_rx_fifo_sz, hsotg->regs + GRXFSIZ);
200 dwc2_writel((hsotg->g_rx_fifo_sz << FIFOSIZE_STARTADDR_SHIFT) |
Gregory Herrero0a176272015-01-09 13:38:52 +0100201 (hsotg->g_np_g_tx_fifo_sz << FIFOSIZE_DEPTH_SHIFT),
202 hsotg->regs + GNPTXFSIZ);
Ben Dooks0f002d22010-05-25 05:36:50 +0100203
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200204 /*
205 * arange all the rest of the TX FIFOs, as some versions of this
Ben Dooks0f002d22010-05-25 05:36:50 +0100206 * block have overlapping default addresses. This also ensures
207 * that if the settings have been changed, then they are set to
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200208 * known values.
209 */
Ben Dooks0f002d22010-05-25 05:36:50 +0100210
211 /* start at the end of the GNPTXFSIZ, rounded up */
Gregory Herrero0a176272015-01-09 13:38:52 +0100212 addr = hsotg->g_rx_fifo_sz + hsotg->g_np_g_tx_fifo_sz;
Ben Dooks0f002d22010-05-25 05:36:50 +0100213
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200214 /*
Gregory Herrero0a176272015-01-09 13:38:52 +0100215 * Configure fifos sizes from provided configuration and assign
Robert Baldygab203d0a2014-09-09 10:44:56 +0200216 * them to endpoints dynamically according to maxpacket size value of
217 * given endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200218 */
Gregory Herrero0a176272015-01-09 13:38:52 +0100219 for (ep = 1; ep < MAX_EPS_CHANNELS; ep++) {
220 if (!hsotg->g_tx_fifo_sz[ep])
221 continue;
Robert Baldygab203d0a2014-09-09 10:44:56 +0200222 val = addr;
Gregory Herrero0a176272015-01-09 13:38:52 +0100223 val |= hsotg->g_tx_fifo_sz[ep] << FIFOSIZE_DEPTH_SHIFT;
224 WARN_ONCE(addr + hsotg->g_tx_fifo_sz[ep] > hsotg->fifo_mem,
Robert Baldygab203d0a2014-09-09 10:44:56 +0200225 "insufficient fifo memory");
Gregory Herrero0a176272015-01-09 13:38:52 +0100226 addr += hsotg->g_tx_fifo_sz[ep];
Ben Dooks0f002d22010-05-25 05:36:50 +0100227
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300228 dwc2_writel(val, hsotg->regs + DPTXFSIZN(ep));
Ben Dooks0f002d22010-05-25 05:36:50 +0100229 }
Ben Dooks1703a6d2010-05-25 05:36:52 +0100230
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200231 /*
232 * according to p428 of the design guide, we need to ensure that
233 * all fifos are flushed before continuing
234 */
Ben Dooks1703a6d2010-05-25 05:36:52 +0100235
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300236 dwc2_writel(GRSTCTL_TXFNUM(0x10) | GRSTCTL_TXFFLSH |
Dinh Nguyen47a16852014-04-14 14:13:34 -0700237 GRSTCTL_RXFFLSH, hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100238
239 /* wait until the fifos are both flushed */
240 timeout = 100;
241 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300242 val = dwc2_readl(hsotg->regs + GRSTCTL);
Ben Dooks1703a6d2010-05-25 05:36:52 +0100243
Dinh Nguyen47a16852014-04-14 14:13:34 -0700244 if ((val & (GRSTCTL_TXFFLSH | GRSTCTL_RXFFLSH)) == 0)
Ben Dooks1703a6d2010-05-25 05:36:52 +0100245 break;
246
247 if (--timeout == 0) {
248 dev_err(hsotg->dev,
249 "%s: timeout flushing fifos (GRSTCTL=%08x)\n",
250 __func__, val);
Gregory Herrero48b20bc2015-01-09 13:39:01 +0100251 break;
Ben Dooks1703a6d2010-05-25 05:36:52 +0100252 }
253
254 udelay(1);
255 }
256
257 dev_dbg(hsotg->dev, "FIFOs reset, timeout at %d\n", timeout);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100258}
259
260/**
261 * @ep: USB endpoint to allocate request for.
262 * @flags: Allocation flags
263 *
264 * Allocate a new USB request structure appropriate for the specified endpoint
265 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500266static struct usb_request *dwc2_hsotg_ep_alloc_request(struct usb_ep *ep,
Mark Brown0978f8c2010-01-18 13:18:35 +0000267 gfp_t flags)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100268{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500269 struct dwc2_hsotg_req *req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100270
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500271 req = kzalloc(sizeof(struct dwc2_hsotg_req), flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100272 if (!req)
273 return NULL;
274
275 INIT_LIST_HEAD(&req->queue);
276
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100277 return &req->req;
278}
279
280/**
281 * is_ep_periodic - return true if the endpoint is in periodic mode.
282 * @hs_ep: The endpoint to query.
283 *
284 * Returns true if the endpoint is in periodic mode, meaning it is being
285 * used for an Interrupt or ISO transfer.
286 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500287static inline int is_ep_periodic(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100288{
289 return hs_ep->periodic;
290}
291
292/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500293 * dwc2_hsotg_unmap_dma - unmap the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100294 * @hsotg: The device state.
295 * @hs_ep: The endpoint for the request
296 * @hs_req: The request being processed.
297 *
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500298 * This is the reverse of dwc2_hsotg_map_dma(), called for the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100299 * of a request to ensure the buffer is ready for access by the caller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200300 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500301static void dwc2_hsotg_unmap_dma(struct dwc2_hsotg *hsotg,
302 struct dwc2_hsotg_ep *hs_ep,
303 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100304{
305 struct usb_request *req = &hs_req->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100306
307 /* ignore this if we're not moving any data */
308 if (hs_req->req.length == 0)
309 return;
310
Jingoo Han17d966a2013-05-11 21:14:00 +0900311 usb_gadget_unmap_request(&hsotg->gadget, req, hs_ep->dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100312}
313
314/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500315 * dwc2_hsotg_write_fifo - write packet Data to the TxFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100316 * @hsotg: The controller state.
317 * @hs_ep: The endpoint we're going to write for.
318 * @hs_req: The request to write data for.
319 *
320 * This is called when the TxFIFO has some space in it to hold a new
321 * transmission and we have something to give it. The actual setup of
322 * the data size is done elsewhere, so all we have to do is to actually
323 * write the data.
324 *
325 * The return value is zero if there is more space (or nothing was done)
326 * otherwise -ENOSPC is returned if the FIFO space was used up.
327 *
328 * This routine is only needed for PIO
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200329 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500330static int dwc2_hsotg_write_fifo(struct dwc2_hsotg *hsotg,
331 struct dwc2_hsotg_ep *hs_ep,
332 struct dwc2_hsotg_req *hs_req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100333{
334 bool periodic = is_ep_periodic(hs_ep);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300335 u32 gnptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100336 int buf_pos = hs_req->req.actual;
337 int to_write = hs_ep->size_loaded;
338 void *data;
339 int can_write;
340 int pkt_round;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200341 int max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100342
343 to_write -= (buf_pos - hs_ep->last_load);
344
345 /* if there's nothing to write, get out early */
346 if (to_write == 0)
347 return 0;
348
Ben Dooks10aebc72010-07-19 09:40:44 +0100349 if (periodic && !hsotg->dedicated_fifos) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300350 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100351 int size_left;
352 int size_done;
353
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200354 /*
355 * work out how much data was loaded so we can calculate
356 * how much data is left in the fifo.
357 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100358
Dinh Nguyen47a16852014-04-14 14:13:34 -0700359 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100360
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200361 /*
362 * if shared fifo, we cannot write anything until the
Ben Dookse7a9ff52010-07-19 09:40:42 +0100363 * previous data has been completely sent.
364 */
365 if (hs_ep->fifo_load != 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500366 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dookse7a9ff52010-07-19 09:40:42 +0100367 return -ENOSPC;
368 }
369
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100370 dev_dbg(hsotg->dev, "%s: left=%d, load=%d, fifo=%d, size %d\n",
371 __func__, size_left,
372 hs_ep->size_loaded, hs_ep->fifo_load, hs_ep->fifo_size);
373
374 /* how much of the data has moved */
375 size_done = hs_ep->size_loaded - size_left;
376
377 /* how much data is left in the fifo */
378 can_write = hs_ep->fifo_load - size_done;
379 dev_dbg(hsotg->dev, "%s: => can_write1=%d\n",
380 __func__, can_write);
381
382 can_write = hs_ep->fifo_size - can_write;
383 dev_dbg(hsotg->dev, "%s: => can_write2=%d\n",
384 __func__, can_write);
385
386 if (can_write <= 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500387 dwc2_hsotg_en_gsint(hsotg, GINTSTS_PTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100388 return -ENOSPC;
389 }
Ben Dooks10aebc72010-07-19 09:40:44 +0100390 } else if (hsotg->dedicated_fifos && hs_ep->index != 0) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300391 can_write = dwc2_readl(hsotg->regs + DTXFSTS(hs_ep->index));
Ben Dooks10aebc72010-07-19 09:40:44 +0100392
393 can_write &= 0xffff;
394 can_write *= 4;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100395 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700396 if (GNPTXSTS_NP_TXQ_SPC_AVAIL_GET(gnptxsts) == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100397 dev_dbg(hsotg->dev,
398 "%s: no queue slots available (0x%08x)\n",
399 __func__, gnptxsts);
400
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500401 dwc2_hsotg_en_gsint(hsotg, GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100402 return -ENOSPC;
403 }
404
Dinh Nguyen47a16852014-04-14 14:13:34 -0700405 can_write = GNPTXSTS_NP_TXF_SPC_AVAIL_GET(gnptxsts);
Ben Dooks679f9b72010-07-19 09:40:41 +0100406 can_write *= 4; /* fifo size is in 32bit quantities. */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100407 }
408
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200409 max_transfer = hs_ep->ep.maxpacket * hs_ep->mc;
410
411 dev_dbg(hsotg->dev, "%s: GNPTXSTS=%08x, can=%d, to=%d, max_transfer %d\n",
412 __func__, gnptxsts, can_write, to_write, max_transfer);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100413
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200414 /*
415 * limit to 512 bytes of data, it seems at least on the non-periodic
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100416 * FIFO, requests of >512 cause the endpoint to get stuck with a
417 * fragment of the end of the transfer in it.
418 */
Robert Baldyga811f3302013-09-24 11:24:28 +0200419 if (can_write > 512 && !periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100420 can_write = 512;
421
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200422 /*
423 * limit the write to one max-packet size worth of data, but allow
Ben Dooks03e10e52010-07-19 09:40:45 +0100424 * the transfer to return that it did not run out of fifo space
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200425 * doing it.
426 */
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200427 if (to_write > max_transfer) {
428 to_write = max_transfer;
Ben Dooks03e10e52010-07-19 09:40:45 +0100429
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200430 /* it's needed only when we do not use dedicated fifos */
431 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500432 dwc2_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700433 periodic ? GINTSTS_PTXFEMP :
434 GINTSTS_NPTXFEMP);
Ben Dooks03e10e52010-07-19 09:40:45 +0100435 }
436
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100437 /* see if we can write data */
438
439 if (to_write > can_write) {
440 to_write = can_write;
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200441 pkt_round = to_write % max_transfer;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100442
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200443 /*
444 * Round the write down to an
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100445 * exact number of packets.
446 *
447 * Note, we do not currently check to see if we can ever
448 * write a full packet or not to the FIFO.
449 */
450
451 if (pkt_round)
452 to_write -= pkt_round;
453
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200454 /*
455 * enable correct FIFO interrupt to alert us when there
456 * is more room left.
457 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100458
Robert Baldyga5cb2ff02013-09-19 11:50:18 +0200459 /* it's needed only when we do not use dedicated fifos */
460 if (!hsotg->dedicated_fifos)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500461 dwc2_hsotg_en_gsint(hsotg,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700462 periodic ? GINTSTS_PTXFEMP :
463 GINTSTS_NPTXFEMP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100464 }
465
466 dev_dbg(hsotg->dev, "write %d/%d, can_write %d, done %d\n",
467 to_write, hs_req->req.length, can_write, buf_pos);
468
469 if (to_write <= 0)
470 return -ENOSPC;
471
472 hs_req->req.actual = buf_pos + to_write;
473 hs_ep->total_data += to_write;
474
475 if (periodic)
476 hs_ep->fifo_load += to_write;
477
478 to_write = DIV_ROUND_UP(to_write, 4);
479 data = hs_req->req.buf + buf_pos;
480
Matt Porter1a7ed5b2014-02-03 10:29:09 -0500481 iowrite32_rep(hsotg->regs + EPFIFO(hs_ep->index), data, to_write);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100482
483 return (to_write >= can_write) ? -ENOSPC : 0;
484}
485
486/**
487 * get_ep_limit - get the maximum data legnth for this endpoint
488 * @hs_ep: The endpoint
489 *
490 * Return the maximum data that can be queued in one go on a given endpoint
491 * so that transfers that are too long can be split.
492 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500493static unsigned get_ep_limit(struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100494{
495 int index = hs_ep->index;
496 unsigned maxsize;
497 unsigned maxpkt;
498
499 if (index != 0) {
Dinh Nguyen47a16852014-04-14 14:13:34 -0700500 maxsize = DXEPTSIZ_XFERSIZE_LIMIT + 1;
501 maxpkt = DXEPTSIZ_PKTCNT_LIMIT + 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100502 } else {
Ben Dooksb05ca582010-07-19 09:40:48 +0100503 maxsize = 64+64;
Jingoo Han66e5c642011-05-13 21:26:15 +0900504 if (hs_ep->dir_in)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700505 maxpkt = DIEPTSIZ0_PKTCNT_LIMIT + 1;
Jingoo Han66e5c642011-05-13 21:26:15 +0900506 else
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100507 maxpkt = 2;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100508 }
509
510 /* we made the constant loading easier above by using +1 */
511 maxpkt--;
512 maxsize--;
513
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200514 /*
515 * constrain by packet count if maxpkts*pktsize is greater
516 * than the length register size.
517 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100518
519 if ((maxpkt * hs_ep->ep.maxpacket) < maxsize)
520 maxsize = maxpkt * hs_ep->ep.maxpacket;
521
522 return maxsize;
523}
524
525/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700526* dwc2_hsotg_read_frameno - read current frame number
527* @hsotg: The device instance
528*
529* Return the current frame number
530*/
531static u32 dwc2_hsotg_read_frameno(struct dwc2_hsotg *hsotg)
532{
533 u32 dsts;
534
535 dsts = dwc2_readl(hsotg->regs + DSTS);
536 dsts &= DSTS_SOFFN_MASK;
537 dsts >>= DSTS_SOFFN_SHIFT;
538
539 return dsts;
540}
541
542/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500543 * dwc2_hsotg_start_req - start a USB request from an endpoint's queue
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100544 * @hsotg: The controller state.
545 * @hs_ep: The endpoint to process a request for
546 * @hs_req: The request to start.
547 * @continuing: True if we are doing more for the current request.
548 *
549 * Start the given request running by setting the endpoint registers
550 * appropriately, and writing any data to the FIFOs.
551 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500552static void dwc2_hsotg_start_req(struct dwc2_hsotg *hsotg,
553 struct dwc2_hsotg_ep *hs_ep,
554 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100555 bool continuing)
556{
557 struct usb_request *ureq = &hs_req->req;
558 int index = hs_ep->index;
559 int dir_in = hs_ep->dir_in;
560 u32 epctrl_reg;
561 u32 epsize_reg;
562 u32 epsize;
563 u32 ctrl;
564 unsigned length;
565 unsigned packets;
566 unsigned maxreq;
567
568 if (index != 0) {
569 if (hs_ep->req && !continuing) {
570 dev_err(hsotg->dev, "%s: active request\n", __func__);
571 WARN_ON(1);
572 return;
573 } else if (hs_ep->req != hs_req && continuing) {
574 dev_err(hsotg->dev,
575 "%s: continue different req\n", __func__);
576 WARN_ON(1);
577 return;
578 }
579 }
580
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200581 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
582 epsize_reg = dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100583
584 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x, ep %d, dir %s\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300585 __func__, dwc2_readl(hsotg->regs + epctrl_reg), index,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100586 hs_ep->dir_in ? "in" : "out");
587
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900588 /* If endpoint is stalled, we will restart request later */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300589 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900590
Mian Yousaf Kaukabb2d4c542015-09-29 12:08:22 +0200591 if (index && ctrl & DXEPCTL_STALL) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +0900592 dev_warn(hsotg->dev, "%s: ep%d is stalled\n", __func__, index);
593 return;
594 }
595
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100596 length = ureq->length - ureq->actual;
Lukasz Majewski71225be2012-05-04 14:17:03 +0200597 dev_dbg(hsotg->dev, "ureq->length:%d ureq->actual:%d\n",
598 ureq->length, ureq->actual);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100599
600 maxreq = get_ep_limit(hs_ep);
601 if (length > maxreq) {
602 int round = maxreq % hs_ep->ep.maxpacket;
603
604 dev_dbg(hsotg->dev, "%s: length %d, max-req %d, r %d\n",
605 __func__, length, maxreq, round);
606
607 /* round down to multiple of packets */
608 if (round)
609 maxreq -= round;
610
611 length = maxreq;
612 }
613
614 if (length)
615 packets = DIV_ROUND_UP(length, hs_ep->ep.maxpacket);
616 else
617 packets = 1; /* send one packet if length is zero. */
618
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200619 if (hs_ep->isochronous && length > (hs_ep->mc * hs_ep->ep.maxpacket)) {
620 dev_err(hsotg->dev, "req length > maxpacket*mc\n");
621 return;
622 }
623
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100624 if (dir_in && index != 0)
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200625 if (hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -0700626 epsize = DXEPTSIZ_MC(packets);
Robert Baldyga4fca54a2013-10-09 09:00:02 +0200627 else
Dinh Nguyen47a16852014-04-14 14:13:34 -0700628 epsize = DXEPTSIZ_MC(1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100629 else
630 epsize = 0;
631
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +0100632 /*
633 * zero length packet should be programmed on its own and should not
634 * be counted in DIEPTSIZ.PktCnt with other packets.
635 */
636 if (dir_in && ureq->zero && !continuing) {
637 /* Test if zlp is actually required. */
638 if ((ureq->length >= hs_ep->ep.maxpacket) &&
639 !(ureq->length % hs_ep->ep.maxpacket))
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +0100640 hs_ep->send_zlp = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100641 }
642
Dinh Nguyen47a16852014-04-14 14:13:34 -0700643 epsize |= DXEPTSIZ_PKTCNT(packets);
644 epsize |= DXEPTSIZ_XFERSIZE(length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100645
646 dev_dbg(hsotg->dev, "%s: %d@%d/%d, 0x%08x => 0x%08x\n",
647 __func__, packets, length, ureq->length, epsize, epsize_reg);
648
649 /* store the request as the current one we're doing */
650 hs_ep->req = hs_req;
651
652 /* write size / packets */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300653 dwc2_writel(epsize, hsotg->regs + epsize_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100654
Anton Tikhomirovdb1d8ba2012-03-06 14:09:19 +0900655 if (using_dma(hsotg) && !continuing) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100656 unsigned int dma_reg;
657
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200658 /*
659 * write DMA address to control register, buffer already
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500660 * synced by dwc2_hsotg_ep_queue().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200661 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100662
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +0200663 dma_reg = dir_in ? DIEPDMA(index) : DOEPDMA(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300664 dwc2_writel(ureq->dma, hsotg->regs + dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100665
Fabio Estevam0cc4cf62014-04-29 00:49:42 -0300666 dev_dbg(hsotg->dev, "%s: %pad => 0x%08x\n",
Jingoo Han8b3bc142014-02-04 14:25:29 +0900667 __func__, &ureq->dma, dma_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100668 }
669
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700670 if (hs_ep->isochronous && hs_ep->interval == 1) {
671 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
672 dwc2_gadget_incr_frame_num(hs_ep);
673
674 if (hs_ep->target_frame & 0x1)
675 ctrl |= DXEPCTL_SETODDFR;
676 else
677 ctrl |= DXEPCTL_SETEVENFR;
678 }
679
Dinh Nguyen47a16852014-04-14 14:13:34 -0700680 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200681
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100682 dev_dbg(hsotg->dev, "ep0 state:%d\n", hsotg->ep0_state);
Lukasz Majewski71225be2012-05-04 14:17:03 +0200683
684 /* For Setup request do not clear NAK */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +0100685 if (!(index == 0 && hsotg->ep0_state == DWC2_EP0_SETUP))
Dinh Nguyen47a16852014-04-14 14:13:34 -0700686 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
Lukasz Majewski71225be2012-05-04 14:17:03 +0200687
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100688 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300689 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100690
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200691 /*
692 * set these, it seems that DMA support increments past the end
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100693 * of the packet buffer so we need to calculate the length from
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200694 * this information.
695 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100696 hs_ep->size_loaded = length;
697 hs_ep->last_load = ureq->actual;
698
699 if (dir_in && !using_dma(hsotg)) {
700 /* set these anyway, we may need them for non-periodic in */
701 hs_ep->fifo_load = 0;
702
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500703 dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100704 }
705
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200706 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200707 * Note, trying to clear the NAK here causes problems with transmit
708 * on the S3C6400 ending up with the TXFIFO becoming full.
709 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100710
711 /* check ep is enabled */
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300712 if (!(dwc2_readl(hsotg->regs + epctrl_reg) & DXEPCTL_EPENA))
Mian Yousaf Kaukab1a0ed862015-01-09 13:39:00 +0100713 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -0700714 "ep%d: failed to become enabled (DXEPCTL=0x%08x)?\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300715 index, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100716
Dinh Nguyen47a16852014-04-14 14:13:34 -0700717 dev_dbg(hsotg->dev, "%s: DXEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300718 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Robert Baldygaafcf4162013-09-19 11:50:19 +0200719
720 /* enable ep interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500721 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100722}
723
724/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500725 * dwc2_hsotg_map_dma - map the DMA memory being used for the request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100726 * @hsotg: The device state.
727 * @hs_ep: The endpoint the request is on.
728 * @req: The request being processed.
729 *
730 * We've been asked to queue a request, so ensure that the memory buffer
731 * is correctly setup for DMA. If we've been passed an extant DMA address
732 * then ensure the buffer has been synced to memory. If our buffer has no
733 * DMA memory, then we map the memory and mark our request to allow us to
734 * cleanup on completion.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200735 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500736static int dwc2_hsotg_map_dma(struct dwc2_hsotg *hsotg,
737 struct dwc2_hsotg_ep *hs_ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100738 struct usb_request *req)
739{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500740 struct dwc2_hsotg_req *hs_req = our_req(req);
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200741 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100742
743 /* if the length is zero, ignore the DMA data */
744 if (hs_req->req.length == 0)
745 return 0;
746
Felipe Balbie58ebcd2013-01-28 14:48:36 +0200747 ret = usb_gadget_map_request(&hsotg->gadget, req, hs_ep->dir_in);
748 if (ret)
749 goto dma_error;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100750
751 return 0;
752
753dma_error:
754 dev_err(hsotg->dev, "%s: failed to map buffer %p, %d bytes\n",
755 __func__, req->buf, req->length);
756
757 return -EIO;
758}
759
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500760static int dwc2_hsotg_handle_unaligned_buf_start(struct dwc2_hsotg *hsotg,
761 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100762{
763 void *req_buf = hs_req->req.buf;
764
765 /* If dma is not being used or buffer is aligned */
766 if (!using_dma(hsotg) || !((long)req_buf & 3))
767 return 0;
768
769 WARN_ON(hs_req->saved_req_buf);
770
771 dev_dbg(hsotg->dev, "%s: %s: buf=%p length=%d\n", __func__,
772 hs_ep->ep.name, req_buf, hs_req->req.length);
773
774 hs_req->req.buf = kmalloc(hs_req->req.length, GFP_ATOMIC);
775 if (!hs_req->req.buf) {
776 hs_req->req.buf = req_buf;
777 dev_err(hsotg->dev,
778 "%s: unable to allocate memory for bounce buffer\n",
779 __func__);
780 return -ENOMEM;
781 }
782
783 /* Save actual buffer */
784 hs_req->saved_req_buf = req_buf;
785
786 if (hs_ep->dir_in)
787 memcpy(hs_req->req.buf, req_buf, hs_req->req.length);
788 return 0;
789}
790
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500791static void dwc2_hsotg_handle_unaligned_buf_complete(struct dwc2_hsotg *hsotg,
792 struct dwc2_hsotg_ep *hs_ep, struct dwc2_hsotg_req *hs_req)
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100793{
794 /* If dma is not being used or buffer was aligned */
795 if (!using_dma(hsotg) || !hs_req->saved_req_buf)
796 return;
797
798 dev_dbg(hsotg->dev, "%s: %s: status=%d actual-length=%d\n", __func__,
799 hs_ep->ep.name, hs_req->req.status, hs_req->req.actual);
800
801 /* Copy data from bounce buffer on successful out transfer */
802 if (!hs_ep->dir_in && !hs_req->req.status)
803 memcpy(hs_req->saved_req_buf, hs_req->req.buf,
804 hs_req->req.actual);
805
806 /* Free bounce buffer */
807 kfree(hs_req->req.buf);
808
809 hs_req->req.buf = hs_req->saved_req_buf;
810 hs_req->saved_req_buf = NULL;
811}
812
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -0700813/**
814 * dwc2_gadget_target_frame_elapsed - Checks target frame
815 * @hs_ep: The driver endpoint to check
816 *
817 * Returns 1 if targeted frame elapsed. If returned 1 then we need to drop
818 * corresponding transfer.
819 */
820static bool dwc2_gadget_target_frame_elapsed(struct dwc2_hsotg_ep *hs_ep)
821{
822 struct dwc2_hsotg *hsotg = hs_ep->parent;
823 u32 target_frame = hs_ep->target_frame;
824 u32 current_frame = dwc2_hsotg_read_frameno(hsotg);
825 bool frame_overrun = hs_ep->frame_overrun;
826
827 if (!frame_overrun && current_frame >= target_frame)
828 return true;
829
830 if (frame_overrun && current_frame >= target_frame &&
831 ((current_frame - target_frame) < DSTS_SOFFN_LIMIT / 2))
832 return true;
833
834 return false;
835}
836
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500837static int dwc2_hsotg_ep_queue(struct usb_ep *ep, struct usb_request *req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100838 gfp_t gfp_flags)
839{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500840 struct dwc2_hsotg_req *hs_req = our_req(req);
841 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600842 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100843 bool first;
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100844 int ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100845
846 dev_dbg(hs->dev, "%s: req %p: %d@%p, noi=%d, zero=%d, snok=%d\n",
847 ep->name, req, req->length, req->buf, req->no_interrupt,
848 req->zero, req->short_not_ok);
849
Gregory Herrero7ababa92015-04-29 22:09:08 +0200850 /* Prevent new request submission when controller is suspended */
851 if (hs->lx_state == DWC2_L2) {
852 dev_dbg(hs->dev, "%s: don't submit request while suspended\n",
853 __func__);
854 return -EAGAIN;
855 }
856
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100857 /* initialise status of the request */
858 INIT_LIST_HEAD(&hs_req->queue);
859 req->actual = 0;
860 req->status = -EINPROGRESS;
861
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500862 ret = dwc2_hsotg_handle_unaligned_buf_start(hs, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +0100863 if (ret)
864 return ret;
865
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100866 /* if we're using DMA, sync the buffers as necessary */
867 if (using_dma(hs)) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500868 ret = dwc2_hsotg_map_dma(hs, hs_ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100869 if (ret)
870 return ret;
871 }
872
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100873 first = list_empty(&hs_ep->queue);
874 list_add_tail(&hs_req->queue, &hs_ep->queue);
875
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700876 if (first) {
877 if (!hs_ep->isochronous) {
878 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
879 return 0;
880 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100881
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -0700882 while (dwc2_gadget_target_frame_elapsed(hs_ep))
883 dwc2_gadget_incr_frame_num(hs_ep);
884
885 if (hs_ep->target_frame != TARGET_FRAME_INITIAL)
886 dwc2_hsotg_start_req(hs, hs_ep, hs_req, false);
887 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100888 return 0;
889}
890
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500891static int dwc2_hsotg_ep_queue_lock(struct usb_ep *ep, struct usb_request *req,
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200892 gfp_t gfp_flags)
893{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500894 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600895 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200896 unsigned long flags = 0;
897 int ret = 0;
898
899 spin_lock_irqsave(&hs->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500900 ret = dwc2_hsotg_ep_queue(ep, req, gfp_flags);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +0200901 spin_unlock_irqrestore(&hs->lock, flags);
902
903 return ret;
904}
905
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500906static void dwc2_hsotg_ep_free_request(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100907 struct usb_request *req)
908{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500909 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100910
911 kfree(hs_req);
912}
913
914/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500915 * dwc2_hsotg_complete_oursetup - setup completion callback
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100916 * @ep: The endpoint the request was on.
917 * @req: The request completed.
918 *
919 * Called on completion of any requests the driver itself
920 * submitted that need cleaning up.
921 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500922static void dwc2_hsotg_complete_oursetup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100923 struct usb_request *req)
924{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500925 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -0600926 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100927
928 dev_dbg(hsotg->dev, "%s: ep %p, req %p\n", __func__, ep, req);
929
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500930 dwc2_hsotg_ep_free_request(ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100931}
932
933/**
934 * ep_from_windex - convert control wIndex value to endpoint
935 * @hsotg: The driver state.
936 * @windex: The control request wIndex field (in host order).
937 *
938 * Convert the given wIndex into a pointer to an driver endpoint
939 * structure, or return NULL if it is not a valid endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +0200940 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500941static struct dwc2_hsotg_ep *ep_from_windex(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100942 u32 windex)
943{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500944 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100945 int dir = (windex & USB_DIR_IN) ? 1 : 0;
946 int idx = windex & 0x7F;
947
948 if (windex >= 0x100)
949 return NULL;
950
Lukasz Majewskib3f489b2012-05-04 14:17:09 +0200951 if (idx > hsotg->num_of_eps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100952 return NULL;
953
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +0100954 ep = index_to_ep(hsotg, idx, dir);
955
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100956 if (idx && ep->dir_in != dir)
957 return NULL;
958
959 return ep;
960}
961
962/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500963 * dwc2_hsotg_set_test_mode - Enable usb Test Modes
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100964 * @hsotg: The driver state.
965 * @testmode: requested usb test mode
966 * Enable usb Test Mode requested by the Host.
967 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500968int dwc2_hsotg_set_test_mode(struct dwc2_hsotg *hsotg, int testmode)
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100969{
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300970 int dctl = dwc2_readl(hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100971
972 dctl &= ~DCTL_TSTCTL_MASK;
973 switch (testmode) {
974 case TEST_J:
975 case TEST_K:
976 case TEST_SE0_NAK:
977 case TEST_PACKET:
978 case TEST_FORCE_EN:
979 dctl |= testmode << DCTL_TSTCTL_SHIFT;
980 break;
981 default:
982 return -EINVAL;
983 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300984 dwc2_writel(dctl, hsotg->regs + DCTL);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +0100985 return 0;
986}
987
988/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500989 * dwc2_hsotg_send_reply - send reply to control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +0100990 * @hsotg: The device state
991 * @ep: Endpoint 0
992 * @buff: Buffer for request
993 * @length: Length of reply.
994 *
995 * Create a request and queue it on the given endpoint. This is useful as
996 * an internal method of sending replies to certain control requests, etc.
997 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -0500998static int dwc2_hsotg_send_reply(struct dwc2_hsotg *hsotg,
999 struct dwc2_hsotg_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001000 void *buff,
1001 int length)
1002{
1003 struct usb_request *req;
1004 int ret;
1005
1006 dev_dbg(hsotg->dev, "%s: buff %p, len %d\n", __func__, buff, length);
1007
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001008 req = dwc2_hsotg_ep_alloc_request(&ep->ep, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001009 hsotg->ep0_reply = req;
1010 if (!req) {
1011 dev_warn(hsotg->dev, "%s: cannot alloc req\n", __func__);
1012 return -ENOMEM;
1013 }
1014
1015 req->buf = hsotg->ep0_buff;
1016 req->length = length;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01001017 /*
1018 * zero flag is for sending zlp in DATA IN stage. It has no impact on
1019 * STATUS stage.
1020 */
1021 req->zero = 0;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001022 req->complete = dwc2_hsotg_complete_oursetup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001023
1024 if (length)
1025 memcpy(req->buf, buff, length);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001026
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001027 ret = dwc2_hsotg_ep_queue(&ep->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001028 if (ret) {
1029 dev_warn(hsotg->dev, "%s: cannot queue req\n", __func__);
1030 return ret;
1031 }
1032
1033 return 0;
1034}
1035
1036/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001037 * dwc2_hsotg_process_req_status - process request GET_STATUS
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001038 * @hsotg: The device state
1039 * @ctrl: USB control request
1040 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001041static int dwc2_hsotg_process_req_status(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001042 struct usb_ctrlrequest *ctrl)
1043{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001044 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1045 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001046 __le16 reply;
1047 int ret;
1048
1049 dev_dbg(hsotg->dev, "%s: USB_REQ_GET_STATUS\n", __func__);
1050
1051 if (!ep0->dir_in) {
1052 dev_warn(hsotg->dev, "%s: direction out?\n", __func__);
1053 return -EINVAL;
1054 }
1055
1056 switch (ctrl->bRequestType & USB_RECIP_MASK) {
1057 case USB_RECIP_DEVICE:
1058 reply = cpu_to_le16(0); /* bit 0 => self powered,
1059 * bit 1 => remote wakeup */
1060 break;
1061
1062 case USB_RECIP_INTERFACE:
1063 /* currently, the data result should be zero */
1064 reply = cpu_to_le16(0);
1065 break;
1066
1067 case USB_RECIP_ENDPOINT:
1068 ep = ep_from_windex(hsotg, le16_to_cpu(ctrl->wIndex));
1069 if (!ep)
1070 return -ENOENT;
1071
1072 reply = cpu_to_le16(ep->halted ? 1 : 0);
1073 break;
1074
1075 default:
1076 return 0;
1077 }
1078
1079 if (le16_to_cpu(ctrl->wLength) != 2)
1080 return -EINVAL;
1081
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001082 ret = dwc2_hsotg_send_reply(hsotg, ep0, &reply, 2);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001083 if (ret) {
1084 dev_err(hsotg->dev, "%s: failed to send reply\n", __func__);
1085 return ret;
1086 }
1087
1088 return 1;
1089}
1090
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001091static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001092
1093/**
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001094 * get_ep_head - return the first request on the endpoint
1095 * @hs_ep: The controller endpoint to get
1096 *
1097 * Get the first request on the endpoint.
1098 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001099static struct dwc2_hsotg_req *get_ep_head(struct dwc2_hsotg_ep *hs_ep)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001100{
1101 if (list_empty(&hs_ep->queue))
1102 return NULL;
1103
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001104 return list_first_entry(&hs_ep->queue, struct dwc2_hsotg_req, queue);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001105}
1106
1107/**
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001108 * dwc2_gadget_start_next_request - Starts next request from ep queue
1109 * @hs_ep: Endpoint structure
1110 *
1111 * If queue is empty and EP is ISOC-OUT - unmasks OUTTKNEPDIS which is masked
1112 * in its handler. Hence we need to unmask it here to be able to do
1113 * resynchronization.
1114 */
1115static void dwc2_gadget_start_next_request(struct dwc2_hsotg_ep *hs_ep)
1116{
1117 u32 mask;
1118 struct dwc2_hsotg *hsotg = hs_ep->parent;
1119 int dir_in = hs_ep->dir_in;
1120 struct dwc2_hsotg_req *hs_req;
1121 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
1122
1123 if (!list_empty(&hs_ep->queue)) {
1124 hs_req = get_ep_head(hs_ep);
1125 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, false);
1126 return;
1127 }
1128 if (!hs_ep->isochronous)
1129 return;
1130
1131 if (dir_in) {
1132 dev_dbg(hsotg->dev, "%s: No more ISOC-IN requests\n",
1133 __func__);
1134 } else {
1135 dev_dbg(hsotg->dev, "%s: No more ISOC-OUT requests\n",
1136 __func__);
1137 mask = dwc2_readl(hsotg->regs + epmsk_reg);
1138 mask |= DOEPMSK_OUTTKNEPDISMSK;
1139 dwc2_writel(mask, hsotg->regs + epmsk_reg);
1140 }
1141}
1142
1143/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001144 * dwc2_hsotg_process_req_feature - process request {SET,CLEAR}_FEATURE
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001145 * @hsotg: The device state
1146 * @ctrl: USB control request
1147 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001148static int dwc2_hsotg_process_req_feature(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001149 struct usb_ctrlrequest *ctrl)
1150{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001151 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
1152 struct dwc2_hsotg_req *hs_req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001153 bool set = (ctrl->bRequest == USB_REQ_SET_FEATURE);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001154 struct dwc2_hsotg_ep *ep;
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001155 int ret;
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001156 bool halted;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001157 u32 recip;
1158 u32 wValue;
1159 u32 wIndex;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001160
1161 dev_dbg(hsotg->dev, "%s: %s_FEATURE\n",
1162 __func__, set ? "SET" : "CLEAR");
1163
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001164 wValue = le16_to_cpu(ctrl->wValue);
1165 wIndex = le16_to_cpu(ctrl->wIndex);
1166 recip = ctrl->bRequestType & USB_RECIP_MASK;
1167
1168 switch (recip) {
1169 case USB_RECIP_DEVICE:
1170 switch (wValue) {
1171 case USB_DEVICE_TEST_MODE:
1172 if ((wIndex & 0xff) != 0)
1173 return -EINVAL;
1174 if (!set)
1175 return -EINVAL;
1176
1177 hsotg->test_mode = wIndex >> 8;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001178 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001179 if (ret) {
1180 dev_err(hsotg->dev,
1181 "%s: failed to send reply\n", __func__);
1182 return ret;
1183 }
1184 break;
1185 default:
1186 return -ENOENT;
1187 }
1188 break;
1189
1190 case USB_RECIP_ENDPOINT:
1191 ep = ep_from_windex(hsotg, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001192 if (!ep) {
1193 dev_dbg(hsotg->dev, "%s: no endpoint for 0x%04x\n",
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001194 __func__, wIndex);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001195 return -ENOENT;
1196 }
1197
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001198 switch (wValue) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001199 case USB_ENDPOINT_HALT:
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001200 halted = ep->halted;
1201
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07001202 dwc2_hsotg_ep_sethalt(&ep->ep, set, true);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001203
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001204 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Anton Tikhomirov26ab3d02011-04-21 17:06:40 +09001205 if (ret) {
1206 dev_err(hsotg->dev,
1207 "%s: failed to send reply\n", __func__);
1208 return ret;
1209 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001210
Robert Baldygabd9ef7b2013-09-19 11:50:22 +02001211 /*
1212 * we have to complete all requests for ep if it was
1213 * halted, and the halt was cleared by CLEAR_FEATURE
1214 */
1215
1216 if (!set && halted) {
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001217 /*
1218 * If we have request in progress,
1219 * then complete it
1220 */
1221 if (ep->req) {
1222 hs_req = ep->req;
1223 ep->req = NULL;
1224 list_del_init(&hs_req->queue);
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001225 if (hs_req->req.complete) {
1226 spin_unlock(&hsotg->lock);
1227 usb_gadget_giveback_request(
1228 &ep->ep, &hs_req->req);
1229 spin_lock(&hsotg->lock);
1230 }
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001231 }
1232
1233 /* If we have pending request, then start it */
Gregory Herreroc00dd4a2015-01-30 09:09:27 +01001234 if (!ep->req) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001235 dwc2_gadget_start_next_request(ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001236 }
1237 }
1238
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001239 break;
1240
1241 default:
1242 return -ENOENT;
1243 }
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001244 break;
1245 default:
1246 return -ENOENT;
1247 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001248 return 1;
1249}
1250
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001251static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg);
Robert Baldygaab93e012013-09-19 11:50:17 +02001252
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001253/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001254 * dwc2_hsotg_stall_ep0 - stall ep0
Robert Baldygac9f721b2014-01-14 08:36:00 +01001255 * @hsotg: The device state
1256 *
1257 * Set stall for ep0 as response for setup request.
1258 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001259static void dwc2_hsotg_stall_ep0(struct dwc2_hsotg *hsotg)
Jingoo Hane9ebe7c2014-06-03 22:14:56 +09001260{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001261 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Robert Baldygac9f721b2014-01-14 08:36:00 +01001262 u32 reg;
1263 u32 ctrl;
1264
1265 dev_dbg(hsotg->dev, "ep0 stall (dir=%d)\n", ep0->dir_in);
1266 reg = (ep0->dir_in) ? DIEPCTL0 : DOEPCTL0;
1267
1268 /*
1269 * DxEPCTL_Stall will be cleared by EP once it has
1270 * taken effect, so no need to clear later.
1271 */
1272
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001273 ctrl = dwc2_readl(hsotg->regs + reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001274 ctrl |= DXEPCTL_STALL;
1275 ctrl |= DXEPCTL_CNAK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001276 dwc2_writel(ctrl, hsotg->regs + reg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001277
1278 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001279 "written DXEPCTL=0x%08x to %08x (DXEPCTL=0x%08x)\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001280 ctrl, reg, dwc2_readl(hsotg->regs + reg));
Robert Baldygac9f721b2014-01-14 08:36:00 +01001281
1282 /*
1283 * complete won't be called, so we enqueue
1284 * setup request here
1285 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001286 dwc2_hsotg_enqueue_setup(hsotg);
Robert Baldygac9f721b2014-01-14 08:36:00 +01001287}
1288
1289/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001290 * dwc2_hsotg_process_control - process a control request
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001291 * @hsotg: The device state
1292 * @ctrl: The control request received
1293 *
1294 * The controller has received the SETUP phase of a control request, and
1295 * needs to work out what to do next (and whether to pass it on to the
1296 * gadget driver).
1297 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001298static void dwc2_hsotg_process_control(struct dwc2_hsotg *hsotg,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001299 struct usb_ctrlrequest *ctrl)
1300{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001301 struct dwc2_hsotg_ep *ep0 = hsotg->eps_out[0];
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001302 int ret = 0;
1303 u32 dcfg;
1304
Mian Yousaf Kaukabe525e742015-09-29 12:08:23 +02001305 dev_dbg(hsotg->dev,
1306 "ctrl Type=%02x, Req=%02x, V=%04x, I=%04x, L=%04x\n",
1307 ctrl->bRequestType, ctrl->bRequest, ctrl->wValue,
1308 ctrl->wIndex, ctrl->wLength);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001309
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001310 if (ctrl->wLength == 0) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001311 ep0->dir_in = 1;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001312 hsotg->ep0_state = DWC2_EP0_STATUS_IN;
1313 } else if (ctrl->bRequestType & USB_DIR_IN) {
1314 ep0->dir_in = 1;
1315 hsotg->ep0_state = DWC2_EP0_DATA_IN;
1316 } else {
1317 ep0->dir_in = 0;
1318 hsotg->ep0_state = DWC2_EP0_DATA_OUT;
1319 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001320
1321 if ((ctrl->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1322 switch (ctrl->bRequest) {
1323 case USB_REQ_SET_ADDRESS:
Mian Yousaf Kaukab6d713c12015-01-09 13:39:10 +01001324 hsotg->connected = 1;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001325 dcfg = dwc2_readl(hsotg->regs + DCFG);
Dinh Nguyen47a16852014-04-14 14:13:34 -07001326 dcfg &= ~DCFG_DEVADDR_MASK;
Paul Zimmermand5dbd3f2014-04-25 14:18:13 -07001327 dcfg |= (le16_to_cpu(ctrl->wValue) <<
1328 DCFG_DEVADDR_SHIFT) & DCFG_DEVADDR_MASK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001329 dwc2_writel(dcfg, hsotg->regs + DCFG);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001330
1331 dev_info(hsotg->dev, "new address %d\n", ctrl->wValue);
1332
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001333 ret = dwc2_hsotg_send_reply(hsotg, ep0, NULL, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001334 return;
1335
1336 case USB_REQ_GET_STATUS:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001337 ret = dwc2_hsotg_process_req_status(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001338 break;
1339
1340 case USB_REQ_CLEAR_FEATURE:
1341 case USB_REQ_SET_FEATURE:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001342 ret = dwc2_hsotg_process_req_feature(hsotg, ctrl);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001343 break;
1344 }
1345 }
1346
1347 /* as a fallback, try delivering it to the driver to deal with */
1348
1349 if (ret == 0 && hsotg->driver) {
Robert Baldyga93f599f2013-11-21 13:49:17 +01001350 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001351 ret = hsotg->driver->setup(&hsotg->gadget, ctrl);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001352 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001353 if (ret < 0)
1354 dev_dbg(hsotg->dev, "driver->setup() ret %d\n", ret);
1355 }
1356
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001357 /*
1358 * the request is either unhandlable, or is not formatted correctly
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001359 * so respond with a STALL for the status stage to indicate failure.
1360 */
1361
Robert Baldygac9f721b2014-01-14 08:36:00 +01001362 if (ret < 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001363 dwc2_hsotg_stall_ep0(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001364}
1365
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001366/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001367 * dwc2_hsotg_complete_setup - completion of a setup transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001368 * @ep: The endpoint the request was on.
1369 * @req: The request completed.
1370 *
1371 * Called on completion of any requests the driver itself submitted for
1372 * EP0 setup packets
1373 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001374static void dwc2_hsotg_complete_setup(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001375 struct usb_request *req)
1376{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001377 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06001378 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001379
1380 if (req->status < 0) {
1381 dev_dbg(hsotg->dev, "%s: failed %d\n", __func__, req->status);
1382 return;
1383 }
1384
Robert Baldyga93f599f2013-11-21 13:49:17 +01001385 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001386 if (req->actual == 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001387 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001388 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001389 dwc2_hsotg_process_control(hsotg, req->buf);
Robert Baldyga93f599f2013-11-21 13:49:17 +01001390 spin_unlock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001391}
1392
1393/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001394 * dwc2_hsotg_enqueue_setup - start a request for EP0 packets
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001395 * @hsotg: The device state.
1396 *
1397 * Enqueue a request on EP0 if necessary to received any SETUP packets
1398 * received from the host.
1399 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001400static void dwc2_hsotg_enqueue_setup(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001401{
1402 struct usb_request *req = hsotg->ctrl_req;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001403 struct dwc2_hsotg_req *hs_req = our_req(req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001404 int ret;
1405
1406 dev_dbg(hsotg->dev, "%s: queueing setup request\n", __func__);
1407
1408 req->zero = 0;
1409 req->length = 8;
1410 req->buf = hsotg->ctrl_buff;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001411 req->complete = dwc2_hsotg_complete_setup;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001412
1413 if (!list_empty(&hs_req->queue)) {
1414 dev_dbg(hsotg->dev, "%s already queued???\n", __func__);
1415 return;
1416 }
1417
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001418 hsotg->eps_out[0]->dir_in = 0;
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01001419 hsotg->eps_out[0]->send_zlp = 0;
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001420 hsotg->ep0_state = DWC2_EP0_SETUP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001421
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001422 ret = dwc2_hsotg_ep_queue(&hsotg->eps_out[0]->ep, req, GFP_ATOMIC);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001423 if (ret < 0) {
1424 dev_err(hsotg->dev, "%s: failed queue (%d)\n", __func__, ret);
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001425 /*
1426 * Don't think there's much we can do other than watch the
1427 * driver fail.
1428 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001429 }
1430}
1431
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001432static void dwc2_hsotg_program_zlp(struct dwc2_hsotg *hsotg,
1433 struct dwc2_hsotg_ep *hs_ep)
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001434{
1435 u32 ctrl;
1436 u8 index = hs_ep->index;
1437 u32 epctl_reg = hs_ep->dir_in ? DIEPCTL(index) : DOEPCTL(index);
1438 u32 epsiz_reg = hs_ep->dir_in ? DIEPTSIZ(index) : DOEPTSIZ(index);
1439
Mian Yousaf Kaukabccb34a92015-01-30 09:09:34 +01001440 if (hs_ep->dir_in)
1441 dev_dbg(hsotg->dev, "Sending zero-length packet on ep%d\n",
1442 index);
1443 else
1444 dev_dbg(hsotg->dev, "Receiving zero-length packet on ep%d\n",
1445 index);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001446
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001447 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
1448 DXEPTSIZ_XFERSIZE(0), hsotg->regs +
1449 epsiz_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001450
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001451 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001452 ctrl |= DXEPCTL_CNAK; /* clear NAK set by core */
1453 ctrl |= DXEPCTL_EPENA; /* ensure ep enabled */
1454 ctrl |= DXEPCTL_USBACTEP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001455 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001456}
1457
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001458/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001459 * dwc2_hsotg_complete_request - complete a request given to us
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001460 * @hsotg: The device state.
1461 * @hs_ep: The endpoint the request was on.
1462 * @hs_req: The request to complete.
1463 * @result: The result code (0 => Ok, otherwise errno)
1464 *
1465 * The given request has finished, so call the necessary completion
1466 * if it has one and then look to see if we can start a new request
1467 * on the endpoint.
1468 *
1469 * Note, expects the ep to already be locked as appropriate.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001470 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001471static void dwc2_hsotg_complete_request(struct dwc2_hsotg *hsotg,
1472 struct dwc2_hsotg_ep *hs_ep,
1473 struct dwc2_hsotg_req *hs_req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001474 int result)
1475{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001476
1477 if (!hs_req) {
1478 dev_dbg(hsotg->dev, "%s: nothing to complete?\n", __func__);
1479 return;
1480 }
1481
1482 dev_dbg(hsotg->dev, "complete: ep %p %s, req %p, %d => %p\n",
1483 hs_ep, hs_ep->ep.name, hs_req, result, hs_req->req.complete);
1484
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001485 /*
1486 * only replace the status if we've not already set an error
1487 * from a previous transaction
1488 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001489
1490 if (hs_req->req.status == -EINPROGRESS)
1491 hs_req->req.status = result;
1492
Yunzhi Li44583fe2015-09-29 12:25:01 +02001493 if (using_dma(hsotg))
1494 dwc2_hsotg_unmap_dma(hsotg, hs_ep, hs_req);
1495
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001496 dwc2_hsotg_handle_unaligned_buf_complete(hsotg, hs_ep, hs_req);
Mian Yousaf Kaukab7d24c1b2015-01-30 09:09:31 +01001497
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001498 hs_ep->req = NULL;
1499 list_del_init(&hs_req->queue);
1500
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001501 /*
1502 * call the complete request with the locks off, just in case the
1503 * request tries to queue more work for this endpoint.
1504 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001505
1506 if (hs_req->req.complete) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02001507 spin_unlock(&hsotg->lock);
Michal Sojka304f7e52014-09-24 22:43:19 +02001508 usb_gadget_giveback_request(&hs_ep->ep, &hs_req->req);
Lukasz Majewski22258f42012-06-14 10:02:24 +02001509 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001510 }
1511
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001512 /*
1513 * Look to see if there is anything else to do. Note, the completion
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001514 * of the previous request may have caused a new request to be started
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001515 * so be careful when doing this.
1516 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001517
1518 if (!hs_ep->req && result >= 0) {
Vardan Mikayelyan41cc4cd2016-05-25 18:07:12 -07001519 dwc2_gadget_start_next_request(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001520 }
1521}
1522
1523/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001524 * dwc2_hsotg_rx_data - receive data from the FIFO for an endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001525 * @hsotg: The device state.
1526 * @ep_idx: The endpoint index for the data
1527 * @size: The size of data in the fifo, in bytes
1528 *
1529 * The FIFO status shows there is data to read from the FIFO for a given
1530 * endpoint, so sort out whether we need to read the data into a request
1531 * that has been made for that endpoint.
1532 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001533static void dwc2_hsotg_rx_data(struct dwc2_hsotg *hsotg, int ep_idx, int size)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001534{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001535 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[ep_idx];
1536 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001537 void __iomem *fifo = hsotg->regs + EPFIFO(ep_idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001538 int to_read;
1539 int max_req;
1540 int read_ptr;
1541
Lukasz Majewski22258f42012-06-14 10:02:24 +02001542
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001543 if (!hs_req) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001544 u32 epctl = dwc2_readl(hsotg->regs + DOEPCTL(ep_idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001545 int ptr;
1546
Robert Baldyga6b448af2014-12-16 11:51:44 +01001547 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07001548 "%s: FIFO %d bytes on ep%d but no req (DXEPCTl=0x%08x)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001549 __func__, size, ep_idx, epctl);
1550
1551 /* dump the data from the FIFO, we've nothing we can do */
1552 for (ptr = 0; ptr < size; ptr += 4)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001553 (void)dwc2_readl(fifo);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001554
1555 return;
1556 }
1557
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001558 to_read = size;
1559 read_ptr = hs_req->req.actual;
1560 max_req = hs_req->req.length - read_ptr;
1561
Ben Dooksa33e7132010-07-19 09:40:49 +01001562 dev_dbg(hsotg->dev, "%s: read %d/%d, done %d/%d\n",
1563 __func__, to_read, max_req, read_ptr, hs_req->req.length);
1564
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001565 if (to_read > max_req) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001566 /*
1567 * more data appeared than we where willing
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001568 * to deal with in this request.
1569 */
1570
1571 /* currently we don't deal this */
1572 WARN_ON_ONCE(1);
1573 }
1574
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001575 hs_ep->total_data += to_read;
1576 hs_req->req.actual += to_read;
1577 to_read = DIV_ROUND_UP(to_read, 4);
1578
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001579 /*
1580 * note, we might over-write the buffer end by 3 bytes depending on
1581 * alignment of the data.
1582 */
Matt Porter1a7ed5b2014-02-03 10:29:09 -05001583 ioread32_rep(fifo, hs_req->req.buf + read_ptr, to_read);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001584}
1585
1586/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001587 * dwc2_hsotg_ep0_zlp - send/receive zero-length packet on control endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001588 * @hsotg: The device instance
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001589 * @dir_in: If IN zlp
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001590 *
1591 * Generate a zero-length IN packet request for terminating a SETUP
1592 * transaction.
1593 *
1594 * Note, since we don't write any data to the TxFIFO, then it is
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001595 * currently believed that we do not need to wait for any space in
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001596 * the TxFIFO.
1597 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001598static void dwc2_hsotg_ep0_zlp(struct dwc2_hsotg *hsotg, bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001599{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001600 /* eps_out[0] is used in both directions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001601 hsotg->eps_out[0]->dir_in = dir_in;
1602 hsotg->ep0_state = dir_in ? DWC2_EP0_STATUS_IN : DWC2_EP0_STATUS_OUT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001603
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001604 dwc2_hsotg_program_zlp(hsotg, hsotg->eps_out[0]);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001605}
1606
Roman Bacikec1f9d92015-09-10 18:13:43 -07001607static void dwc2_hsotg_change_ep_iso_parity(struct dwc2_hsotg *hsotg,
1608 u32 epctl_reg)
1609{
1610 u32 ctrl;
1611
1612 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
1613 if (ctrl & DXEPCTL_EOFRNUM)
1614 ctrl |= DXEPCTL_SETEVENFR;
1615 else
1616 ctrl |= DXEPCTL_SETODDFR;
1617 dwc2_writel(ctrl, hsotg->regs + epctl_reg);
1618}
1619
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001620/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001621 * dwc2_hsotg_handle_outdone - handle receiving OutDone/SetupDone from RXFIFO
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001622 * @hsotg: The device instance
1623 * @epnum: The endpoint received from
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001624 *
1625 * The RXFIFO has delivered an OutDone event, which means that the data
1626 * transfer for an OUT endpoint has been completed, either by a short
1627 * packet or by the finish of a transfer.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001628 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001629static void dwc2_hsotg_handle_outdone(struct dwc2_hsotg *hsotg, int epnum)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001630{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001631 u32 epsize = dwc2_readl(hsotg->regs + DOEPTSIZ(epnum));
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001632 struct dwc2_hsotg_ep *hs_ep = hsotg->eps_out[epnum];
1633 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001634 struct usb_request *req = &hs_req->req;
Dinh Nguyen47a16852014-04-14 14:13:34 -07001635 unsigned size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001636 int result = 0;
1637
1638 if (!hs_req) {
1639 dev_dbg(hsotg->dev, "%s: no request active\n", __func__);
1640 return;
1641 }
1642
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001643 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_OUT) {
1644 dev_dbg(hsotg->dev, "zlp packet received\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001645 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
1646 dwc2_hsotg_enqueue_setup(hsotg);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001647 return;
1648 }
1649
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001650 if (using_dma(hsotg)) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001651 unsigned size_done;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001652
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001653 /*
1654 * Calculate the size of the transfer by checking how much
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001655 * is left in the endpoint size register and then working it
1656 * out from the amount we loaded for the transfer.
1657 *
1658 * We need to do this as DMA pointers are always 32bit aligned
1659 * so may overshoot/undershoot the transfer.
1660 */
1661
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001662 size_done = hs_ep->size_loaded - size_left;
1663 size_done += hs_ep->last_load;
1664
1665 req->actual = size_done;
1666 }
1667
Ben Dooksa33e7132010-07-19 09:40:49 +01001668 /* if there is more request to do, schedule new transfer */
1669 if (req->actual < req->length && size_left == 0) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001670 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Ben Dooksa33e7132010-07-19 09:40:49 +01001671 return;
1672 }
1673
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001674 if (req->actual < req->length && req->short_not_ok) {
1675 dev_dbg(hsotg->dev, "%s: got %d/%d (short not ok) => error\n",
1676 __func__, req->actual, req->length);
1677
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001678 /*
1679 * todo - what should we return here? there's no one else
1680 * even bothering to check the status.
1681 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001682 }
1683
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001684 if (epnum == 0 && hsotg->ep0_state == DWC2_EP0_DATA_OUT) {
1685 /* Move to STATUS IN */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001686 dwc2_hsotg_ep0_zlp(hsotg, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001687 return;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001688 }
1689
Roman Bacikec1f9d92015-09-10 18:13:43 -07001690 /*
1691 * Slave mode OUT transfers do not go through XferComplete so
1692 * adjust the ISOC parity here.
1693 */
1694 if (!using_dma(hsotg)) {
Roman Bacikec1f9d92015-09-10 18:13:43 -07001695 if (hs_ep->isochronous && hs_ep->interval == 1)
1696 dwc2_hsotg_change_ep_iso_parity(hsotg, DOEPCTL(epnum));
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07001697 else if (hs_ep->isochronous && hs_ep->interval > 1)
1698 dwc2_gadget_incr_frame_num(hs_ep);
Roman Bacikec1f9d92015-09-10 18:13:43 -07001699 }
1700
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001701 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, result);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001702}
1703
1704/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001705 * dwc2_hsotg_handle_rx - RX FIFO has data
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001706 * @hsotg: The device instance
1707 *
1708 * The IRQ handler has detected that the RX FIFO has some data in it
1709 * that requires processing, so find out what is in there and do the
1710 * appropriate read.
1711 *
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001712 * The RXFIFO is a true FIFO, the packets coming out are still in packet
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001713 * chunks, so if you have x packets received on an endpoint you'll get x
1714 * FIFO events delivered, each with a packet's worth of data in it.
1715 *
1716 * When using DMA, we should not be processing events from the RXFIFO
1717 * as the actual data should be sent to the memory directly and we turn
1718 * on the completion interrupts to get notifications of transfer completion.
1719 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001720static void dwc2_hsotg_handle_rx(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001721{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001722 u32 grxstsr = dwc2_readl(hsotg->regs + GRXSTSP);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001723 u32 epnum, status, size;
1724
1725 WARN_ON(using_dma(hsotg));
1726
Dinh Nguyen47a16852014-04-14 14:13:34 -07001727 epnum = grxstsr & GRXSTS_EPNUM_MASK;
1728 status = grxstsr & GRXSTS_PKTSTS_MASK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001729
Dinh Nguyen47a16852014-04-14 14:13:34 -07001730 size = grxstsr & GRXSTS_BYTECNT_MASK;
1731 size >>= GRXSTS_BYTECNT_SHIFT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001732
Mian Yousaf Kaukabd7c747c2015-01-30 09:09:30 +01001733 dev_dbg(hsotg->dev, "%s: GRXSTSP=0x%08x (%d@%d)\n",
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001734 __func__, grxstsr, size, epnum);
1735
Dinh Nguyen47a16852014-04-14 14:13:34 -07001736 switch ((status & GRXSTS_PKTSTS_MASK) >> GRXSTS_PKTSTS_SHIFT) {
1737 case GRXSTS_PKTSTS_GLOBALOUTNAK:
1738 dev_dbg(hsotg->dev, "GLOBALOUTNAK\n");
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001739 break;
1740
Dinh Nguyen47a16852014-04-14 14:13:34 -07001741 case GRXSTS_PKTSTS_OUTDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001742 dev_dbg(hsotg->dev, "OutDone (Frame=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001743 dwc2_hsotg_read_frameno(hsotg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001744
1745 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001746 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001747 break;
1748
Dinh Nguyen47a16852014-04-14 14:13:34 -07001749 case GRXSTS_PKTSTS_SETUPDONE:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001750 dev_dbg(hsotg->dev,
1751 "SetupDone (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001752 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001753 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001754 /*
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001755 * Call dwc2_hsotg_handle_outdone here if it was not called from
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001756 * GRXSTS_PKTSTS_OUTDONE. That is, if the core didn't
1757 * generate GRXSTS_PKTSTS_OUTDONE for setup packet.
1758 */
1759 if (hsotg->ep0_state == DWC2_EP0_SETUP)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001760 dwc2_hsotg_handle_outdone(hsotg, epnum);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001761 break;
1762
Dinh Nguyen47a16852014-04-14 14:13:34 -07001763 case GRXSTS_PKTSTS_OUTRX:
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001764 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001765 break;
1766
Dinh Nguyen47a16852014-04-14 14:13:34 -07001767 case GRXSTS_PKTSTS_SETUPRX:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001768 dev_dbg(hsotg->dev,
1769 "SetupRX (Frame=0x%08x, DOPEPCTL=0x%08x)\n",
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001770 dwc2_hsotg_read_frameno(hsotg),
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001771 dwc2_readl(hsotg->regs + DOEPCTL(0)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001772
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001773 WARN_ON(hsotg->ep0_state != DWC2_EP0_SETUP);
1774
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001775 dwc2_hsotg_rx_data(hsotg, epnum, size);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001776 break;
1777
1778 default:
1779 dev_warn(hsotg->dev, "%s: unknown status %08x\n",
1780 __func__, grxstsr);
1781
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001782 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001783 break;
1784 }
1785}
1786
1787/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001788 * dwc2_hsotg_ep0_mps - turn max packet size into register setting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001789 * @mps: The maximum packet size in bytes.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001790 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001791static u32 dwc2_hsotg_ep0_mps(unsigned int mps)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001792{
1793 switch (mps) {
1794 case 64:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001795 return D0EPCTL_MPS_64;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001796 case 32:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001797 return D0EPCTL_MPS_32;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001798 case 16:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001799 return D0EPCTL_MPS_16;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001800 case 8:
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02001801 return D0EPCTL_MPS_8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001802 }
1803
1804 /* bad max packet size, warn and return invalid result */
1805 WARN_ON(1);
1806 return (u32)-1;
1807}
1808
1809/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001810 * dwc2_hsotg_set_ep_maxpacket - set endpoint's max-packet field
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001811 * @hsotg: The driver state.
1812 * @ep: The index number of the endpoint
1813 * @mps: The maximum packet size in bytes
1814 *
1815 * Configure the maximum packet size for the given endpoint, updating
1816 * the hardware control registers to reflect this.
1817 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001818static void dwc2_hsotg_set_ep_maxpacket(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001819 unsigned int ep, unsigned int mps, unsigned int dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001820{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001821 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001822 void __iomem *regs = hsotg->regs;
1823 u32 mpsval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001824 u32 mcval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001825 u32 reg;
1826
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001827 hs_ep = index_to_ep(hsotg, ep, dir_in);
1828 if (!hs_ep)
1829 return;
1830
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001831 if (ep == 0) {
1832 /* EP0 is a special case */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001833 mpsval = dwc2_hsotg_ep0_mps(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001834 if (mpsval > 3)
1835 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001836 hs_ep->ep.maxpacket = mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001837 hs_ep->mc = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001838 } else {
Dinh Nguyen47a16852014-04-14 14:13:34 -07001839 mpsval = mps & DXEPCTL_MPS_MASK;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001840 if (mpsval > 1024)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001841 goto bad_mps;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02001842 mcval = ((mps >> 11) & 0x3) + 1;
1843 hs_ep->mc = mcval;
1844 if (mcval > 3)
1845 goto bad_mps;
Robert Baldygae9edd1992013-10-09 08:20:02 +02001846 hs_ep->ep.maxpacket = mpsval;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001847 }
1848
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001849 if (dir_in) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001850 reg = dwc2_readl(regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001851 reg &= ~DXEPCTL_MPS_MASK;
1852 reg |= mpsval;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001853 dwc2_writel(reg, regs + DIEPCTL(ep));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01001854 } else {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001855 reg = dwc2_readl(regs + DOEPCTL(ep));
Dinh Nguyen47a16852014-04-14 14:13:34 -07001856 reg &= ~DXEPCTL_MPS_MASK;
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001857 reg |= mpsval;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001858 dwc2_writel(reg, regs + DOEPCTL(ep));
Anton Tikhomirov659ad602012-03-06 14:07:29 +09001859 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001860
1861 return;
1862
1863bad_mps:
1864 dev_err(hsotg->dev, "ep%d: bad mps of %d\n", ep, mps);
1865}
1866
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001867/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001868 * dwc2_hsotg_txfifo_flush - flush Tx FIFO
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001869 * @hsotg: The driver state
1870 * @idx: The index for the endpoint (0..15)
1871 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001872static void dwc2_hsotg_txfifo_flush(struct dwc2_hsotg *hsotg, unsigned int idx)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001873{
1874 int timeout;
1875 int val;
1876
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001877 dwc2_writel(GRSTCTL_TXFNUM(idx) | GRSTCTL_TXFFLSH,
1878 hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001879
1880 /* wait until the fifo is flushed */
1881 timeout = 100;
1882
1883 while (1) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001884 val = dwc2_readl(hsotg->regs + GRSTCTL);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001885
Dinh Nguyen47a16852014-04-14 14:13:34 -07001886 if ((val & (GRSTCTL_TXFFLSH)) == 0)
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001887 break;
1888
1889 if (--timeout == 0) {
1890 dev_err(hsotg->dev,
1891 "%s: timeout flushing fifo (GRSTCTL=%08x)\n",
1892 __func__, val);
Marek Szyprowskie0cbe592014-09-09 10:44:10 +02001893 break;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09001894 }
1895
1896 udelay(1);
1897 }
1898}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001899
1900/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001901 * dwc2_hsotg_trytx - check to see if anything needs transmitting
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001902 * @hsotg: The driver state
1903 * @hs_ep: The driver endpoint to check.
1904 *
1905 * Check to see if there is a request that has data to send, and if so
1906 * make an attempt to write data into the FIFO.
1907 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001908static int dwc2_hsotg_trytx(struct dwc2_hsotg *hsotg,
1909 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001910{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001911 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001912
Robert Baldygaafcf4162013-09-19 11:50:19 +02001913 if (!hs_ep->dir_in || !hs_req) {
1914 /**
1915 * if request is not enqueued, we disable interrupts
1916 * for endpoints, excepting ep0
1917 */
1918 if (hs_ep->index != 0)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001919 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index,
Robert Baldygaafcf4162013-09-19 11:50:19 +02001920 hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001921 return 0;
Robert Baldygaafcf4162013-09-19 11:50:19 +02001922 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001923
1924 if (hs_req->req.actual < hs_req->req.length) {
1925 dev_dbg(hsotg->dev, "trying to write more for ep%d\n",
1926 hs_ep->index);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001927 return dwc2_hsotg_write_fifo(hsotg, hs_ep, hs_req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001928 }
1929
1930 return 0;
1931}
1932
1933/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001934 * dwc2_hsotg_complete_in - complete IN transfer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001935 * @hsotg: The device state.
1936 * @hs_ep: The endpoint that has just completed.
1937 *
1938 * An IN transfer has been completed, update the transfer's state and then
1939 * call the relevant completion routines.
1940 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001941static void dwc2_hsotg_complete_in(struct dwc2_hsotg *hsotg,
1942 struct dwc2_hsotg_ep *hs_ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001943{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001944 struct dwc2_hsotg_req *hs_req = hs_ep->req;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001945 u32 epsize = dwc2_readl(hsotg->regs + DIEPTSIZ(hs_ep->index));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001946 int size_left, size_done;
1947
1948 if (!hs_req) {
1949 dev_dbg(hsotg->dev, "XferCompl but no req\n");
1950 return;
1951 }
1952
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001953 /* Finish ZLP handling for IN EP0 transactions */
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001954 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_STATUS_IN) {
1955 dev_dbg(hsotg->dev, "zlp packet sent\n");
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001956 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001957 if (hsotg->test_mode) {
1958 int ret;
1959
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001960 ret = dwc2_hsotg_set_test_mode(hsotg, hsotg->test_mode);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001961 if (ret < 0) {
1962 dev_dbg(hsotg->dev, "Invalid Test #%d\n",
1963 hsotg->test_mode);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001964 dwc2_hsotg_stall_ep0(hsotg);
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01001965 return;
1966 }
1967 }
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001968 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001969 return;
1970 }
1971
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02001972 /*
1973 * Calculate the size of the transfer by checking how much is left
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001974 * in the endpoint size register and then working it out from
1975 * the amount we loaded for the transfer.
1976 *
1977 * We do this even for DMA, as the transfer may have incremented
1978 * past the end of the buffer (DMA transfers are always 32bit
1979 * aligned).
1980 */
1981
Dinh Nguyen47a16852014-04-14 14:13:34 -07001982 size_left = DXEPTSIZ_XFERSIZE_GET(epsize);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001983
1984 size_done = hs_ep->size_loaded - size_left;
1985 size_done += hs_ep->last_load;
1986
1987 if (hs_req->req.actual != size_done)
1988 dev_dbg(hsotg->dev, "%s: adjusting size done %d => %d\n",
1989 __func__, hs_req->req.actual, size_done);
1990
1991 hs_req->req.actual = size_done;
Lukasz Majewskid3ca0252012-05-04 14:17:04 +02001992 dev_dbg(hsotg->dev, "req->length:%d req->actual:%d req->zero:%d\n",
1993 hs_req->req.length, hs_req->req.actual, hs_req->req.zero);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001994
Ben Dooks5b7d70c2009-06-02 14:58:06 +01001995 if (!size_left && hs_req->req.actual < hs_req->req.length) {
1996 dev_dbg(hsotg->dev, "%s trying more for req...\n", __func__);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05001997 dwc2_hsotg_start_req(hsotg, hs_ep, hs_req, true);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01001998 return;
1999 }
2000
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002001 /* Zlp for all endpoints, for ep0 only in DATA IN stage */
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002002 if (hs_ep->send_zlp) {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002003 dwc2_hsotg_program_zlp(hsotg, hs_ep);
Mian Yousaf Kaukab8a20fa42015-01-09 13:39:03 +01002004 hs_ep->send_zlp = 0;
Mian Yousaf Kaukabf71b5e22015-01-09 13:38:59 +01002005 /* transfer will be completed on next complete interrupt */
2006 return;
2007 }
2008
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002009 if (hs_ep->index == 0 && hsotg->ep0_state == DWC2_EP0_DATA_IN) {
2010 /* Move to STATUS OUT */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002011 dwc2_hsotg_ep0_zlp(hsotg, false);
Mian Yousaf Kaukabfe0b94a2015-01-09 13:38:58 +01002012 return;
2013 }
2014
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002015 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002016}
2017
2018/**
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002019 * dwc2_gadget_read_ep_interrupts - reads interrupts for given ep
2020 * @hsotg: The device state.
2021 * @idx: Index of ep.
2022 * @dir_in: Endpoint direction 1-in 0-out.
2023 *
2024 * Reads for endpoint with given index and direction, by masking
2025 * epint_reg with coresponding mask.
2026 */
2027static u32 dwc2_gadget_read_ep_interrupts(struct dwc2_hsotg *hsotg,
2028 unsigned int idx, int dir_in)
2029{
2030 u32 epmsk_reg = dir_in ? DIEPMSK : DOEPMSK;
2031 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2032 u32 ints;
2033 u32 mask;
2034 u32 diepempmsk;
2035
2036 mask = dwc2_readl(hsotg->regs + epmsk_reg);
2037 diepempmsk = dwc2_readl(hsotg->regs + DIEPEMPMSK);
2038 mask |= ((diepempmsk >> idx) & 0x1) ? DIEPMSK_TXFIFOEMPTY : 0;
2039 mask |= DXEPINT_SETUP_RCVD;
2040
2041 ints = dwc2_readl(hsotg->regs + epint_reg);
2042 ints &= mask;
2043 return ints;
2044}
2045
2046/**
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002047 * dwc2_gadget_handle_ep_disabled - handle DXEPINT_EPDISBLD
2048 * @hs_ep: The endpoint on which interrupt is asserted.
2049 *
2050 * This interrupt indicates that the endpoint has been disabled per the
2051 * application's request.
2052 *
2053 * For IN endpoints flushes txfifo, in case of BULK clears DCTL_CGNPINNAK,
2054 * in case of ISOC completes current request.
2055 *
2056 * For ISOC-OUT endpoints completes expired requests. If there is remaining
2057 * request starts it.
2058 */
2059static void dwc2_gadget_handle_ep_disabled(struct dwc2_hsotg_ep *hs_ep)
2060{
2061 struct dwc2_hsotg *hsotg = hs_ep->parent;
2062 struct dwc2_hsotg_req *hs_req;
2063 unsigned char idx = hs_ep->index;
2064 int dir_in = hs_ep->dir_in;
2065 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2066 int dctl = dwc2_readl(hsotg->regs + DCTL);
2067
2068 dev_dbg(hsotg->dev, "%s: EPDisbld\n", __func__);
2069
2070 if (dir_in) {
2071 int epctl = dwc2_readl(hsotg->regs + epctl_reg);
2072
2073 dwc2_hsotg_txfifo_flush(hsotg, hs_ep->fifo_index);
2074
2075 if (hs_ep->isochronous) {
2076 dwc2_hsotg_complete_in(hsotg, hs_ep);
2077 return;
2078 }
2079
2080 if ((epctl & DXEPCTL_STALL) && (epctl & DXEPCTL_EPTYPE_BULK)) {
2081 int dctl = dwc2_readl(hsotg->regs + DCTL);
2082
2083 dctl |= DCTL_CGNPINNAK;
2084 dwc2_writel(dctl, hsotg->regs + DCTL);
2085 }
2086 return;
2087 }
2088
2089 if (dctl & DCTL_GOUTNAKSTS) {
2090 dctl |= DCTL_CGOUTNAK;
2091 dwc2_writel(dctl, hsotg->regs + DCTL);
2092 }
2093
2094 if (!hs_ep->isochronous)
2095 return;
2096
2097 if (list_empty(&hs_ep->queue)) {
2098 dev_dbg(hsotg->dev, "%s: complete_ep 0x%p, ep->queue empty!\n",
2099 __func__, hs_ep);
2100 return;
2101 }
2102
2103 do {
2104 hs_req = get_ep_head(hs_ep);
2105 if (hs_req)
2106 dwc2_hsotg_complete_request(hsotg, hs_ep, hs_req,
2107 -ENODATA);
2108 dwc2_gadget_incr_frame_num(hs_ep);
2109 } while (dwc2_gadget_target_frame_elapsed(hs_ep));
2110
2111 dwc2_gadget_start_next_request(hs_ep);
2112}
2113
2114/**
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002115 * dwc2_gadget_handle_out_token_ep_disabled - handle DXEPINT_OUTTKNEPDIS
2116 * @hs_ep: The endpoint on which interrupt is asserted.
2117 *
2118 * This is starting point for ISOC-OUT transfer, synchronization done with
2119 * first out token received from host while corresponding EP is disabled.
2120 *
2121 * Device does not know initial frame in which out token will come. For this
2122 * HW generates OUTTKNEPDIS - out token is received while EP is disabled. Upon
2123 * getting this interrupt SW starts calculation for next transfer frame.
2124 */
2125static void dwc2_gadget_handle_out_token_ep_disabled(struct dwc2_hsotg_ep *ep)
2126{
2127 struct dwc2_hsotg *hsotg = ep->parent;
2128 int dir_in = ep->dir_in;
2129 u32 doepmsk;
2130
2131 if (dir_in || !ep->isochronous)
2132 return;
2133
2134 dwc2_hsotg_complete_request(hsotg, ep, get_ep_head(ep), -ENODATA);
2135
2136 if (ep->interval > 1 &&
2137 ep->target_frame == TARGET_FRAME_INITIAL) {
2138 u32 dsts;
2139 u32 ctrl;
2140
2141 dsts = dwc2_readl(hsotg->regs + DSTS);
2142 ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2143 dwc2_gadget_incr_frame_num(ep);
2144
2145 ctrl = dwc2_readl(hsotg->regs + DOEPCTL(ep->index));
2146 if (ep->target_frame & 0x1)
2147 ctrl |= DXEPCTL_SETODDFR;
2148 else
2149 ctrl |= DXEPCTL_SETEVENFR;
2150
2151 dwc2_writel(ctrl, hsotg->regs + DOEPCTL(ep->index));
2152 }
2153
2154 dwc2_gadget_start_next_request(ep);
2155 doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
2156 doepmsk &= ~DOEPMSK_OUTTKNEPDISMSK;
2157 dwc2_writel(doepmsk, hsotg->regs + DOEPMSK);
2158}
2159
2160/**
2161* dwc2_gadget_handle_nak - handle NAK interrupt
2162* @hs_ep: The endpoint on which interrupt is asserted.
2163*
2164* This is starting point for ISOC-IN transfer, synchronization done with
2165* first IN token received from host while corresponding EP is disabled.
2166*
2167* Device does not know when first one token will arrive from host. On first
2168* token arrival HW generates 2 interrupts: 'in token received while FIFO empty'
2169* and 'NAK'. NAK interrupt for ISOC-IN means that token has arrived and ZLP was
2170* sent in response to that as there was no data in FIFO. SW is basing on this
2171* interrupt to obtain frame in which token has come and then based on the
2172* interval calculates next frame for transfer.
2173*/
2174static void dwc2_gadget_handle_nak(struct dwc2_hsotg_ep *hs_ep)
2175{
2176 struct dwc2_hsotg *hsotg = hs_ep->parent;
2177 int dir_in = hs_ep->dir_in;
2178
2179 if (!dir_in || !hs_ep->isochronous)
2180 return;
2181
2182 if (hs_ep->target_frame == TARGET_FRAME_INITIAL) {
2183 hs_ep->target_frame = dwc2_hsotg_read_frameno(hsotg);
2184 if (hs_ep->interval > 1) {
2185 u32 ctrl = dwc2_readl(hsotg->regs +
2186 DIEPCTL(hs_ep->index));
2187 if (hs_ep->target_frame & 0x1)
2188 ctrl |= DXEPCTL_SETODDFR;
2189 else
2190 ctrl |= DXEPCTL_SETEVENFR;
2191
2192 dwc2_writel(ctrl, hsotg->regs + DIEPCTL(hs_ep->index));
2193 }
2194
2195 dwc2_hsotg_complete_request(hsotg, hs_ep,
2196 get_ep_head(hs_ep), 0);
2197 }
2198
2199 dwc2_gadget_incr_frame_num(hs_ep);
2200}
2201
2202/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002203 * dwc2_hsotg_epint - handle an in/out endpoint interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002204 * @hsotg: The driver state
2205 * @idx: The index for the endpoint (0..15)
2206 * @dir_in: Set if this is an IN endpoint
2207 *
2208 * Process and clear any interrupt pending for an individual endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002209 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002210static void dwc2_hsotg_epint(struct dwc2_hsotg *hsotg, unsigned int idx,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002211 int dir_in)
2212{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002213 struct dwc2_hsotg_ep *hs_ep = index_to_ep(hsotg, idx, dir_in);
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002214 u32 epint_reg = dir_in ? DIEPINT(idx) : DOEPINT(idx);
2215 u32 epctl_reg = dir_in ? DIEPCTL(idx) : DOEPCTL(idx);
2216 u32 epsiz_reg = dir_in ? DIEPTSIZ(idx) : DOEPTSIZ(idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002217 u32 ints;
Robert Baldyga1479e842013-10-09 08:41:57 +02002218 u32 ctrl;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002219
Vardan Mikayelyan32601582016-05-25 18:07:10 -07002220 ints = dwc2_gadget_read_ep_interrupts(hsotg, idx, dir_in);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002221 ctrl = dwc2_readl(hsotg->regs + epctl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002222
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002223 /* Clear endpoint interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002224 dwc2_writel(ints, hsotg->regs + epint_reg);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002225
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002226 if (!hs_ep) {
2227 dev_err(hsotg->dev, "%s:Interrupt for unconfigured ep%d(%s)\n",
2228 __func__, idx, dir_in ? "in" : "out");
2229 return;
2230 }
2231
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002232 dev_dbg(hsotg->dev, "%s: ep%d(%s) DxEPINT=0x%08x\n",
2233 __func__, idx, dir_in ? "in" : "out", ints);
2234
Mian Yousaf Kaukabb787d752015-01-09 13:38:43 +01002235 /* Don't process XferCompl interrupt if it is a setup packet */
2236 if (idx == 0 && (ints & (DXEPINT_SETUP | DXEPINT_SETUP_RCVD)))
2237 ints &= ~DXEPINT_XFERCOMPL;
2238
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002239 if (ints & DXEPINT_STSPHSERCVD)
2240 dev_dbg(hsotg->dev, "%s: StsPhseRcvd asserted\n", __func__);
Robert Baldyga1479e842013-10-09 08:41:57 +02002241
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002242 if (ints & DXEPINT_XFERCOMPL) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002243 dev_dbg(hsotg->dev,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002244 "%s: XferCompl: DxEPCTL=0x%08x, DXEPTSIZ=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002245 __func__, dwc2_readl(hsotg->regs + epctl_reg),
2246 dwc2_readl(hsotg->regs + epsiz_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002247
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002248 /*
2249 * we get OutDone from the FIFO, so we only need to look
2250 * at completing IN requests here
2251 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002252 if (dir_in) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002253 if (hs_ep->isochronous && hs_ep->interval > 1)
2254 dwc2_gadget_incr_frame_num(hs_ep);
2255
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002256 dwc2_hsotg_complete_in(hsotg, hs_ep);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002257 if (ints & DXEPINT_NAKINTRPT)
2258 ints &= ~DXEPINT_NAKINTRPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002259
Ben Dooksc9a64ea2010-07-19 09:40:46 +01002260 if (idx == 0 && !hs_ep->req)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002261 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002262 } else if (using_dma(hsotg)) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002263 /*
2264 * We're using DMA, we need to fire an OutDone here
2265 * as we ignore the RXFIFO.
2266 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002267 if (hs_ep->isochronous && hs_ep->interval > 1)
2268 dwc2_gadget_incr_frame_num(hs_ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002269
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002270 dwc2_hsotg_handle_outdone(hsotg, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002271 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002272 }
2273
Vardan Mikayelyanbd9971f2016-05-25 18:07:19 -07002274 if (ints & DXEPINT_EPDISBLD)
2275 dwc2_gadget_handle_ep_disabled(hs_ep);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09002276
Vardan Mikayelyan53219222016-05-25 18:07:14 -07002277 if (ints & DXEPINT_OUTTKNEPDIS)
2278 dwc2_gadget_handle_out_token_ep_disabled(hs_ep);
2279
2280 if (ints & DXEPINT_NAKINTRPT)
2281 dwc2_gadget_handle_nak(hs_ep);
2282
Dinh Nguyen47a16852014-04-14 14:13:34 -07002283 if (ints & DXEPINT_AHBERR)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002284 dev_dbg(hsotg->dev, "%s: AHBErr\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002285
Dinh Nguyen47a16852014-04-14 14:13:34 -07002286 if (ints & DXEPINT_SETUP) { /* Setup or Timeout */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002287 dev_dbg(hsotg->dev, "%s: Setup/Timeout\n", __func__);
2288
2289 if (using_dma(hsotg) && idx == 0) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002290 /*
2291 * this is the notification we've received a
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002292 * setup packet. In non-DMA mode we'd get this
2293 * from the RXFIFO, instead we need to process
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002294 * the setup here.
2295 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002296
2297 if (dir_in)
2298 WARN_ON_ONCE(1);
2299 else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002300 dwc2_hsotg_handle_outdone(hsotg, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002301 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002302 }
2303
Dinh Nguyen47a16852014-04-14 14:13:34 -07002304 if (ints & DXEPINT_BACK2BACKSETUP)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002305 dev_dbg(hsotg->dev, "%s: B2BSetup/INEPNakEff\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002306
Robert Baldyga1479e842013-10-09 08:41:57 +02002307 if (dir_in && !hs_ep->isochronous) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002308 /* not sure if this is important, but we'll clear it anyway */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002309 if (ints & DXEPINT_INTKNTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002310 dev_dbg(hsotg->dev, "%s: ep%d: INTknTXFEmpMsk\n",
2311 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002312 }
2313
2314 /* this probably means something bad is happening */
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002315 if (ints & DXEPINT_INTKNEPMIS) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002316 dev_warn(hsotg->dev, "%s: ep%d: INTknEP\n",
2317 __func__, idx);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002318 }
Ben Dooks10aebc72010-07-19 09:40:44 +01002319
2320 /* FIFO has space or is empty (see GAHBCFG) */
2321 if (hsotg->dedicated_fifos &&
Vardan Mikayelyan26ddef52016-05-25 18:07:00 -07002322 ints & DXEPINT_TXFEMP) {
Ben Dooks10aebc72010-07-19 09:40:44 +01002323 dev_dbg(hsotg->dev, "%s: ep%d: TxFIFOEmpty\n",
2324 __func__, idx);
Anton Tikhomirov70fa0302012-03-06 14:08:29 +09002325 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002326 dwc2_hsotg_trytx(hsotg, hs_ep);
Ben Dooks10aebc72010-07-19 09:40:44 +01002327 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002328 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002329}
2330
2331/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002332 * dwc2_hsotg_irq_enumdone - Handle EnumDone interrupt (enumeration done)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002333 * @hsotg: The device state.
2334 *
2335 * Handle updating the device settings after the enumeration phase has
2336 * been completed.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002337 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002338static void dwc2_hsotg_irq_enumdone(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002339{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002340 u32 dsts = dwc2_readl(hsotg->regs + DSTS);
Jingoo Han9b2667f2014-08-20 12:04:09 +09002341 int ep0_mps = 0, ep_mps = 8;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002342
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002343 /*
2344 * This should signal the finish of the enumeration phase
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002345 * of the USB handshaking, so we should now know what rate
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002346 * we connected at.
2347 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002348
2349 dev_dbg(hsotg->dev, "EnumDone (DSTS=0x%08x)\n", dsts);
2350
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002351 /*
2352 * note, since we're limited by the size of transfer on EP0, and
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002353 * it seems IN transfers must be a even number of packets we do
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002354 * not advertise a 64byte MPS on EP0.
2355 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002356
2357 /* catch both EnumSpd_FS and EnumSpd_FS48 */
Marek Vasut6d76c922015-12-18 03:26:17 +01002358 switch ((dsts & DSTS_ENUMSPD_MASK) >> DSTS_ENUMSPD_SHIFT) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07002359 case DSTS_ENUMSPD_FS:
2360 case DSTS_ENUMSPD_FS48:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002361 hsotg->gadget.speed = USB_SPEED_FULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002362 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002363 ep_mps = 1023;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002364 break;
2365
Dinh Nguyen47a16852014-04-14 14:13:34 -07002366 case DSTS_ENUMSPD_HS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002367 hsotg->gadget.speed = USB_SPEED_HIGH;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002368 ep0_mps = EP0_MPS_LIMIT;
Robert Baldyga295538f2013-12-06 13:03:44 +01002369 ep_mps = 1024;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002370 break;
2371
Dinh Nguyen47a16852014-04-14 14:13:34 -07002372 case DSTS_ENUMSPD_LS:
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002373 hsotg->gadget.speed = USB_SPEED_LOW;
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002374 /*
2375 * note, we don't actually support LS in this driver at the
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002376 * moment, and the documentation seems to imply that it isn't
2377 * supported by the PHYs on some of the devices.
2378 */
2379 break;
2380 }
Michal Nazarewicze538dfd2011-08-30 17:11:19 +02002381 dev_info(hsotg->dev, "new device is %s\n",
2382 usb_speed_string(hsotg->gadget.speed));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002383
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002384 /*
2385 * we should now know the maximum packet size for an
2386 * endpoint, so set the endpoints to a default value.
2387 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002388
2389 if (ep0_mps) {
2390 int i;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002391 /* Initialize ep0 for both in and out directions */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002392 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 1);
2393 dwc2_hsotg_set_ep_maxpacket(hsotg, 0, ep0_mps, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002394 for (i = 1; i < hsotg->num_of_eps; i++) {
2395 if (hsotg->eps_in[i])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002396 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 1);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002397 if (hsotg->eps_out[i])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002398 dwc2_hsotg_set_ep_maxpacket(hsotg, i, ep_mps, 0);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002399 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002400 }
2401
2402 /* ensure after enumeration our EP0 is active */
2403
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002404 dwc2_hsotg_enqueue_setup(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002405
2406 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002407 dwc2_readl(hsotg->regs + DIEPCTL0),
2408 dwc2_readl(hsotg->regs + DOEPCTL0));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002409}
2410
2411/**
2412 * kill_all_requests - remove all requests from the endpoint's queue
2413 * @hsotg: The device state.
2414 * @ep: The endpoint the requests may be on.
2415 * @result: The result code to use.
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002416 *
2417 * Go through the requests on the given endpoint and mark them
2418 * completed with the given result code.
2419 */
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002420static void kill_all_requests(struct dwc2_hsotg *hsotg,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002421 struct dwc2_hsotg_ep *ep,
Robert Baldyga6b448af2014-12-16 11:51:44 +01002422 int result)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002423{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002424 struct dwc2_hsotg_req *req, *treq;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002425 unsigned size;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002426
Robert Baldyga6b448af2014-12-16 11:51:44 +01002427 ep->req = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002428
Robert Baldyga6b448af2014-12-16 11:51:44 +01002429 list_for_each_entry_safe(req, treq, &ep->queue, queue)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002430 dwc2_hsotg_complete_request(hsotg, ep, req,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002431 result);
Robert Baldyga6b448af2014-12-16 11:51:44 +01002432
Robert Baldygab203d0a2014-09-09 10:44:56 +02002433 if (!hsotg->dedicated_fifos)
2434 return;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002435 size = (dwc2_readl(hsotg->regs + DTXFSTS(ep->index)) & 0xffff) * 4;
Robert Baldygab203d0a2014-09-09 10:44:56 +02002436 if (size < ep->fifo_size)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002437 dwc2_hsotg_txfifo_flush(hsotg, ep->fifo_index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002438}
2439
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002440/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002441 * dwc2_hsotg_disconnect - disconnect service
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002442 * @hsotg: The device state.
2443 *
Lukasz Majewski5e891342012-05-04 14:17:07 +02002444 * The device has been disconnected. Remove all current
2445 * transactions and signal the gadget driver that this
2446 * has happened.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002447 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002448void dwc2_hsotg_disconnect(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002449{
2450 unsigned ep;
2451
Marek Szyprowski4ace06e2014-11-21 15:14:47 +01002452 if (!hsotg->connected)
2453 return;
2454
2455 hsotg->connected = 0;
Gregory Herrero9e14d0a2015-01-30 09:09:28 +01002456 hsotg->test_mode = 0;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002457
2458 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
2459 if (hsotg->eps_in[ep])
2460 kill_all_requests(hsotg, hsotg->eps_in[ep],
2461 -ESHUTDOWN);
2462 if (hsotg->eps_out[ep])
2463 kill_all_requests(hsotg, hsotg->eps_out[ep],
2464 -ESHUTDOWN);
2465 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002466
2467 call_gadget(hsotg, disconnect);
Gregory Herrero065d3932015-09-22 15:16:54 +02002468 hsotg->lx_state = DWC2_L3;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002469}
2470
2471/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002472 * dwc2_hsotg_irq_fifoempty - TX FIFO empty interrupt handler
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002473 * @hsotg: The device state:
2474 * @periodic: True if this is a periodic FIFO interrupt
2475 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002476static void dwc2_hsotg_irq_fifoempty(struct dwc2_hsotg *hsotg, bool periodic)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002477{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002478 struct dwc2_hsotg_ep *ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002479 int epno, ret;
2480
2481 /* look through for any more data to transmit */
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02002482 for (epno = 0; epno < hsotg->num_of_eps; epno++) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01002483 ep = index_to_ep(hsotg, epno, 1);
2484
2485 if (!ep)
2486 continue;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002487
2488 if (!ep->dir_in)
2489 continue;
2490
2491 if ((periodic && !ep->periodic) ||
2492 (!periodic && ep->periodic))
2493 continue;
2494
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002495 ret = dwc2_hsotg_trytx(hsotg, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002496 if (ret < 0)
2497 break;
2498 }
2499}
2500
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002501/* IRQ flags which will trigger a retry around the IRQ loop */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002502#define IRQ_RETRY_MASK (GINTSTS_NPTXFEMP | \
2503 GINTSTS_PTXFEMP | \
2504 GINTSTS_RXFLVL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002505
2506/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002507 * dwc2_hsotg_core_init - issue softreset to the core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002508 * @hsotg: The device state
2509 *
2510 * Issue a soft reset to the core, and await the core finishing it.
2511 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002512void dwc2_hsotg_core_init_disconnected(struct dwc2_hsotg *hsotg,
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002513 bool is_usb_reset)
Lukasz Majewski308d7342012-05-04 14:17:05 +02002514{
Gregory Herrero1ee69032015-09-29 12:08:27 +02002515 u32 intmsk;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002516 u32 val;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002517 u32 usbcfg;
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002518
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02002519 /* Kill any ep0 requests as controller will be reinitialized */
2520 kill_all_requests(hsotg, hsotg->eps_out[0], -ECONNRESET);
2521
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002522 if (!is_usb_reset)
John Youn241729b2015-12-17 11:17:59 -08002523 if (dwc2_core_reset(hsotg))
Gregory Herrero86de4892015-09-29 12:08:21 +02002524 return;
Lukasz Majewski308d7342012-05-04 14:17:05 +02002525
2526 /*
2527 * we must now enable ep0 ready for host detection and then
2528 * set configuration.
2529 */
2530
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002531 /* keep other bits untouched (so e.g. forced modes are not lost) */
2532 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2533 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
2534 GUSBCFG_HNPCAP);
2535
Lukasz Majewski308d7342012-05-04 14:17:05 +02002536 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01002537 val = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01002538 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
2539 (val << GUSBCFG_USBTRDTIM_SHIFT);
2540 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002541
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002542 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002543
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002544 if (!is_usb_reset)
2545 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002546
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002547 dwc2_writel(DCFG_EPMISCNT(1) | DCFG_DEVSPD_HS, hsotg->regs + DCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002548
2549 /* Clear any pending OTG interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002550 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002551
2552 /* Clear any pending interrupts */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002553 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
Gregory Herrero1ee69032015-09-29 12:08:27 +02002554 intmsk = GINTSTS_ERLYSUSP | GINTSTS_SESSREQINT |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002555 GINTSTS_GOUTNAKEFF | GINTSTS_GINNAKEFF |
Gregory Herrero1ee69032015-09-29 12:08:27 +02002556 GINTSTS_USBRST | GINTSTS_RESETDET |
2557 GINTSTS_ENUMDONE | GINTSTS_OTGINT |
Roman Bacikec1f9d92015-09-10 18:13:43 -07002558 GINTSTS_USBSUSP | GINTSTS_WKUPINT |
2559 GINTSTS_INCOMPL_SOIN | GINTSTS_INCOMPL_SOOUT;
Gregory Herrero1ee69032015-09-29 12:08:27 +02002560
2561 if (hsotg->core_params->external_id_pin_ctl <= 0)
2562 intmsk |= GINTSTS_CONIDSTSCHNG;
2563
2564 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002565
2566 if (using_dma(hsotg))
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002567 dwc2_writel(GAHBCFG_GLBL_INTR_EN | GAHBCFG_DMA_EN |
2568 (GAHBCFG_HBSTLEN_INCR4 << GAHBCFG_HBSTLEN_SHIFT),
2569 hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002570 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002571 dwc2_writel(((hsotg->dedicated_fifos) ?
2572 (GAHBCFG_NP_TXF_EMP_LVL |
2573 GAHBCFG_P_TXF_EMP_LVL) : 0) |
2574 GAHBCFG_GLBL_INTR_EN, hsotg->regs + GAHBCFG);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002575
2576 /*
Robert Baldyga8acc8292013-09-19 11:50:23 +02002577 * If INTknTXFEmpMsk is enabled, it's important to disable ep interrupts
2578 * when we have no data to transfer. Otherwise we get being flooded by
2579 * interrupts.
Lukasz Majewski308d7342012-05-04 14:17:05 +02002580 */
2581
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002582 dwc2_writel(((hsotg->dedicated_fifos && !using_dma(hsotg)) ?
Mian Yousaf Kaukab6ff2e832015-01-09 13:38:42 +01002583 DIEPMSK_TXFIFOEMPTY | DIEPMSK_INTKNTXFEMPMSK : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002584 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002585 DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002586 hsotg->regs + DIEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002587
2588 /*
2589 * don't need XferCompl, we get that from RXFIFO in slave mode. In
2590 * DMA mode we may need this.
2591 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002592 dwc2_writel((using_dma(hsotg) ? (DIEPMSK_XFERCOMPLMSK) : 0) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002593 DOEPMSK_EPDISBLDMSK | DOEPMSK_AHBERRMSK |
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002594 DOEPMSK_SETUPMSK | DOEPMSK_STSPHSERCVDMSK,
Dinh Nguyen47a16852014-04-14 14:13:34 -07002595 hsotg->regs + DOEPMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002596
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002597 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002598
2599 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002600 dwc2_readl(hsotg->regs + DIEPCTL0),
2601 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002602
2603 /* enable in and out endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002604 dwc2_hsotg_en_gsint(hsotg, GINTSTS_OEPINT | GINTSTS_IEPINT);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002605
2606 /*
2607 * Enable the RXFIFO when in slave mode, as this is how we collect
2608 * the data. In DMA mode, we get events from the FIFO but also
2609 * things we cannot process, so do not use it.
2610 */
2611 if (!using_dma(hsotg))
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002612 dwc2_hsotg_en_gsint(hsotg, GINTSTS_RXFLVL);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002613
2614 /* Enable interrupts for EP0 in and out */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002615 dwc2_hsotg_ctrl_epint(hsotg, 0, 0, 1);
2616 dwc2_hsotg_ctrl_epint(hsotg, 0, 1, 1);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002617
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002618 if (!is_usb_reset) {
2619 __orr32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2620 udelay(10); /* see openiboot */
2621 __bic32(hsotg->regs + DCTL, DCTL_PWRONPRGDONE);
2622 }
Lukasz Majewski308d7342012-05-04 14:17:05 +02002623
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002624 dev_dbg(hsotg->dev, "DCTL=0x%08x\n", dwc2_readl(hsotg->regs + DCTL));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002625
2626 /*
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002627 * DxEPCTL_USBActEp says RO in manual, but seems to be set by
Lukasz Majewski308d7342012-05-04 14:17:05 +02002628 * writing to the EPCTL register..
2629 */
2630
2631 /* set to read 1 8byte packet */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002632 dwc2_writel(DXEPTSIZ_MC(1) | DXEPTSIZ_PKTCNT(1) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002633 DXEPTSIZ_XFERSIZE(8), hsotg->regs + DOEPTSIZ0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002634
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002635 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002636 DXEPCTL_CNAK | DXEPCTL_EPENA |
2637 DXEPCTL_USBACTEP,
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002638 hsotg->regs + DOEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002639
2640 /* enable, but don't activate EP0in */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002641 dwc2_writel(dwc2_hsotg_ep0_mps(hsotg->eps_out[0]->ep.maxpacket) |
Dinh Nguyen47a16852014-04-14 14:13:34 -07002642 DXEPCTL_USBACTEP, hsotg->regs + DIEPCTL0);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002643
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002644 dwc2_hsotg_enqueue_setup(hsotg);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002645
2646 dev_dbg(hsotg->dev, "EP0: DIEPCTL0=0x%08x, DOEPCTL0=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002647 dwc2_readl(hsotg->regs + DIEPCTL0),
2648 dwc2_readl(hsotg->regs + DOEPCTL0));
Lukasz Majewski308d7342012-05-04 14:17:05 +02002649
2650 /* clear global NAKs */
Gregory Herrero643cc4d2015-01-30 09:09:32 +01002651 val = DCTL_CGOUTNAK | DCTL_CGNPINNAK;
2652 if (!is_usb_reset)
2653 val |= DCTL_SFTDISCON;
2654 __orr32(hsotg->regs + DCTL, val);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002655
2656 /* must be at-least 3ms to allow bus to see disconnect */
2657 mdelay(3);
2658
Gregory Herrero065d3932015-09-22 15:16:54 +02002659 hsotg->lx_state = DWC2_L0;
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002660}
Marek Szyprowskiac3c81f2014-10-20 12:45:35 +02002661
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002662static void dwc2_hsotg_core_disconnect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002663{
2664 /* set the soft-disconnect bit */
2665 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
2666}
2667
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002668void dwc2_hsotg_core_connect(struct dwc2_hsotg *hsotg)
Marek Szyprowskiad38dc52014-10-20 12:45:36 +02002669{
Lukasz Majewski308d7342012-05-04 14:17:05 +02002670 /* remove the soft-disconnect and let's go */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002671 __bic32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewski308d7342012-05-04 14:17:05 +02002672}
2673
2674/**
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07002675 * dwc2_gadget_handle_incomplete_isoc_in - handle incomplete ISO IN Interrupt.
2676 * @hsotg: The device state:
2677 *
2678 * This interrupt indicates one of the following conditions occurred while
2679 * transmitting an ISOC transaction.
2680 * - Corrupted IN Token for ISOC EP.
2681 * - Packet not complete in FIFO.
2682 *
2683 * The following actions will be taken:
2684 * - Determine the EP
2685 * - Disable EP; when 'Endpoint Disabled' interrupt is received Flush FIFO
2686 */
2687static void dwc2_gadget_handle_incomplete_isoc_in(struct dwc2_hsotg *hsotg)
2688{
2689 struct dwc2_hsotg_ep *hs_ep;
2690 u32 epctrl;
2691 u32 idx;
2692
2693 dev_dbg(hsotg->dev, "Incomplete isoc in interrupt received:\n");
2694
2695 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2696 hs_ep = hsotg->eps_in[idx];
2697 epctrl = dwc2_readl(hsotg->regs + DIEPCTL(idx));
2698 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
2699 dwc2_gadget_target_frame_elapsed(hs_ep)) {
2700 epctrl |= DXEPCTL_SNAK;
2701 epctrl |= DXEPCTL_EPDIS;
2702 dwc2_writel(epctrl, hsotg->regs + DIEPCTL(idx));
2703 }
2704 }
2705
2706 /* Clear interrupt */
2707 dwc2_writel(GINTSTS_INCOMPL_SOIN, hsotg->regs + GINTSTS);
2708}
2709
2710/**
2711 * dwc2_gadget_handle_incomplete_isoc_out - handle incomplete ISO OUT Interrupt
2712 * @hsotg: The device state:
2713 *
2714 * This interrupt indicates one of the following conditions occurred while
2715 * transmitting an ISOC transaction.
2716 * - Corrupted OUT Token for ISOC EP.
2717 * - Packet not complete in FIFO.
2718 *
2719 * The following actions will be taken:
2720 * - Determine the EP
2721 * - Set DCTL_SGOUTNAK and unmask GOUTNAKEFF if target frame elapsed.
2722 */
2723static void dwc2_gadget_handle_incomplete_isoc_out(struct dwc2_hsotg *hsotg)
2724{
2725 u32 gintsts;
2726 u32 gintmsk;
2727 u32 epctrl;
2728 struct dwc2_hsotg_ep *hs_ep;
2729 int idx;
2730
2731 dev_dbg(hsotg->dev, "%s: GINTSTS_INCOMPL_SOOUT\n", __func__);
2732
2733 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2734 hs_ep = hsotg->eps_out[idx];
2735 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
2736 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous &&
2737 dwc2_gadget_target_frame_elapsed(hs_ep)) {
2738 /* Unmask GOUTNAKEFF interrupt */
2739 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
2740 gintmsk |= GINTSTS_GOUTNAKEFF;
2741 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
2742
2743 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2744 if (!(gintsts & GINTSTS_GOUTNAKEFF))
2745 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
2746 }
2747 }
2748
2749 /* Clear interrupt */
2750 dwc2_writel(GINTSTS_INCOMPL_SOOUT, hsotg->regs + GINTSTS);
2751}
2752
2753/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002754 * dwc2_hsotg_irq - handle device interrupt
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002755 * @irq: The IRQ number triggered
2756 * @pw: The pw value when registered the handler.
2757 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002758static irqreturn_t dwc2_hsotg_irq(int irq, void *pw)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002759{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002760 struct dwc2_hsotg *hsotg = pw;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002761 int retry_count = 8;
2762 u32 gintsts;
2763 u32 gintmsk;
2764
Vardan Mikayelyanee3de8d2016-04-27 20:20:48 -07002765 if (!dwc2_is_device_mode(hsotg))
2766 return IRQ_NONE;
2767
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002768 spin_lock(&hsotg->lock);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002769irq_retry:
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002770 gintsts = dwc2_readl(hsotg->regs + GINTSTS);
2771 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002772
2773 dev_dbg(hsotg->dev, "%s: %08x %08x (%08x) retry %d\n",
2774 __func__, gintsts, gintsts & gintmsk, gintmsk, retry_count);
2775
2776 gintsts &= gintmsk;
2777
Mian Yousaf Kaukab8fc37b82015-09-29 12:08:29 +02002778 if (gintsts & GINTSTS_RESETDET) {
2779 dev_dbg(hsotg->dev, "%s: USBRstDet\n", __func__);
2780
2781 dwc2_writel(GINTSTS_RESETDET, hsotg->regs + GINTSTS);
2782
2783 /* This event must be used only if controller is suspended */
2784 if (hsotg->lx_state == DWC2_L2) {
2785 dwc2_exit_hibernation(hsotg, true);
2786 hsotg->lx_state = DWC2_L0;
2787 }
2788 }
2789
2790 if (gintsts & (GINTSTS_USBRST | GINTSTS_RESETDET)) {
2791
2792 u32 usb_status = dwc2_readl(hsotg->regs + GOTGCTL);
2793 u32 connected = hsotg->connected;
2794
2795 dev_dbg(hsotg->dev, "%s: USBRst\n", __func__);
2796 dev_dbg(hsotg->dev, "GNPTXSTS=%08x\n",
2797 dwc2_readl(hsotg->regs + GNPTXSTS));
2798
2799 dwc2_writel(GINTSTS_USBRST, hsotg->regs + GINTSTS);
2800
2801 /* Report disconnection if it is not already done. */
2802 dwc2_hsotg_disconnect(hsotg);
2803
2804 if (usb_status & GOTGCTL_BSESVLD && connected)
2805 dwc2_hsotg_core_init_disconnected(hsotg, true);
2806 }
2807
Dinh Nguyen47a16852014-04-14 14:13:34 -07002808 if (gintsts & GINTSTS_ENUMDONE) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002809 dwc2_writel(GINTSTS_ENUMDONE, hsotg->regs + GINTSTS);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002810
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002811 dwc2_hsotg_irq_enumdone(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002812 }
2813
Dinh Nguyen47a16852014-04-14 14:13:34 -07002814 if (gintsts & (GINTSTS_OEPINT | GINTSTS_IEPINT)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002815 u32 daint = dwc2_readl(hsotg->regs + DAINT);
2816 u32 daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
Robert Baldyga7e804652013-09-19 11:50:20 +02002817 u32 daint_out, daint_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002818 int ep;
2819
Robert Baldyga7e804652013-09-19 11:50:20 +02002820 daint &= daintmsk;
Dinh Nguyen47a16852014-04-14 14:13:34 -07002821 daint_out = daint >> DAINT_OUTEP_SHIFT;
2822 daint_in = daint & ~(daint_out << DAINT_OUTEP_SHIFT);
Robert Baldyga7e804652013-09-19 11:50:20 +02002823
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002824 dev_dbg(hsotg->dev, "%s: daint=%08x\n", __func__, daint);
2825
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002826 for (ep = 0; ep < hsotg->num_of_eps && daint_out;
2827 ep++, daint_out >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002828 if (daint_out & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002829 dwc2_hsotg_epint(hsotg, ep, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002830 }
2831
Mian Yousaf Kaukabcec87f12015-01-09 13:38:51 +01002832 for (ep = 0; ep < hsotg->num_of_eps && daint_in;
2833 ep++, daint_in >>= 1) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002834 if (daint_in & 1)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002835 dwc2_hsotg_epint(hsotg, ep, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002836 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002837 }
2838
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002839 /* check both FIFOs */
2840
Dinh Nguyen47a16852014-04-14 14:13:34 -07002841 if (gintsts & GINTSTS_NPTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002842 dev_dbg(hsotg->dev, "NPTxFEmp\n");
2843
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002844 /*
2845 * Disable the interrupt to stop it happening again
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002846 * unless one of these endpoint routines decides that
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002847 * it needs re-enabling
2848 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002849
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002850 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_NPTXFEMP);
2851 dwc2_hsotg_irq_fifoempty(hsotg, false);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002852 }
2853
Dinh Nguyen47a16852014-04-14 14:13:34 -07002854 if (gintsts & GINTSTS_PTXFEMP) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002855 dev_dbg(hsotg->dev, "PTxFEmp\n");
2856
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002857 /* See note in GINTSTS_NPTxFEmp */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002858
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002859 dwc2_hsotg_disable_gsint(hsotg, GINTSTS_PTXFEMP);
2860 dwc2_hsotg_irq_fifoempty(hsotg, true);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002861 }
2862
Dinh Nguyen47a16852014-04-14 14:13:34 -07002863 if (gintsts & GINTSTS_RXFLVL) {
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002864 /*
2865 * note, since GINTSTS_RxFLvl doubles as FIFO-not-empty,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002866 * we need to retry dwc2_hsotg_handle_rx if this is still
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002867 * set.
2868 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002869
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002870 dwc2_hsotg_handle_rx(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002871 }
2872
Dinh Nguyen47a16852014-04-14 14:13:34 -07002873 if (gintsts & GINTSTS_ERLYSUSP) {
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002874 dev_dbg(hsotg->dev, "GINTSTS_ErlySusp\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002875 dwc2_writel(GINTSTS_ERLYSUSP, hsotg->regs + GINTSTS);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002876 }
2877
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002878 /*
2879 * these next two seem to crop-up occasionally causing the core
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002880 * to shutdown the USB transfer, so try clearing them and logging
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002881 * the occurrence.
2882 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002883
Dinh Nguyen47a16852014-04-14 14:13:34 -07002884 if (gintsts & GINTSTS_GOUTNAKEFF) {
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002885 u8 idx;
2886 u32 epctrl;
2887 u32 gintmsk;
2888 struct dwc2_hsotg_ep *hs_ep;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002889
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002890 /* Mask this interrupt */
2891 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
2892 gintmsk &= ~GINTSTS_GOUTNAKEFF;
2893 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002894
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002895 dev_dbg(hsotg->dev, "GOUTNakEff triggered\n");
2896 for (idx = 1; idx <= hsotg->num_of_eps; idx++) {
2897 hs_ep = hsotg->eps_out[idx];
2898 epctrl = dwc2_readl(hsotg->regs + DOEPCTL(idx));
2899
2900 if ((epctrl & DXEPCTL_EPENA) && hs_ep->isochronous) {
2901 epctrl |= DXEPCTL_SNAK;
2902 epctrl |= DXEPCTL_EPDIS;
2903 dwc2_writel(epctrl, hsotg->regs + DOEPCTL(idx));
2904 }
2905 }
2906
2907 /* This interrupt bit is cleared in DXEPINT_EPDISBLD handler */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002908 }
2909
Dinh Nguyen47a16852014-04-14 14:13:34 -07002910 if (gintsts & GINTSTS_GINNAKEFF) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002911 dev_info(hsotg->dev, "GINNakEff triggered\n");
2912
Gregory Herrero3be99cd2015-12-07 12:07:31 +01002913 __orr32(hsotg->regs + DCTL, DCTL_CGNPINNAK);
Anton Tikhomirova3395f02011-04-21 17:06:39 +09002914
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002915 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002916 }
2917
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07002918 if (gintsts & GINTSTS_INCOMPL_SOIN)
2919 dwc2_gadget_handle_incomplete_isoc_in(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002920
Vardan Mikayelyan381fc8f2016-05-25 18:07:17 -07002921 if (gintsts & GINTSTS_INCOMPL_SOOUT)
2922 dwc2_gadget_handle_incomplete_isoc_out(hsotg);
Roman Bacikec1f9d92015-09-10 18:13:43 -07002923
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002924 /*
2925 * if we've had fifo events, we should try and go around the
2926 * loop again to see if there's any point in returning yet.
2927 */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002928
2929 if (gintsts & IRQ_RETRY_MASK && --retry_count > 0)
2930 goto irq_retry;
2931
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02002932 spin_unlock(&hsotg->lock);
2933
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002934 return IRQ_HANDLED;
2935}
2936
2937/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002938 * dwc2_hsotg_ep_enable - enable the given endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002939 * @ep: The USB endpint to configure
2940 * @desc: The USB endpoint descriptor to configure with.
2941 *
2942 * This is called from the USB gadget code's usb_ep_enable().
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002943 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002944static int dwc2_hsotg_ep_enable(struct usb_ep *ep,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002945 const struct usb_endpoint_descriptor *desc)
2946{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002947 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06002948 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002949 unsigned long flags;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002950 unsigned int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002951 u32 epctrl_reg;
2952 u32 epctrl;
2953 u32 mps;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07002954 u32 mask;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01002955 unsigned int dir_in;
2956 unsigned int i, val, size;
Julia Lawall19c190f2010-03-29 17:36:44 +02002957 int ret = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002958
2959 dev_dbg(hsotg->dev,
2960 "%s: ep %s: a 0x%02x, attr 0x%02x, mps 0x%04x, intr %d\n",
2961 __func__, ep->name, desc->bEndpointAddress, desc->bmAttributes,
2962 desc->wMaxPacketSize, desc->bInterval);
2963
2964 /* not to be called for EP0 */
Vahram Aharonyan8c3d6092016-04-27 20:20:46 -07002965 if (index == 0) {
2966 dev_err(hsotg->dev, "%s: called for EP 0\n", __func__);
2967 return -EINVAL;
2968 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002969
2970 dir_in = (desc->bEndpointAddress & USB_ENDPOINT_DIR_MASK) ? 1 : 0;
2971 if (dir_in != hs_ep->dir_in) {
2972 dev_err(hsotg->dev, "%s: direction mismatch!\n", __func__);
2973 return -EINVAL;
2974 }
2975
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07002976 mps = usb_endpoint_maxp(desc);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002977
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002978 /* note, we handle this here instead of dwc2_hsotg_set_ep_maxpacket */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002979
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02002980 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002981 epctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002982
2983 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x from 0x%08x\n",
2984 __func__, epctrl, epctrl_reg);
2985
Lukasz Majewski22258f42012-06-14 10:02:24 +02002986 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002987
Dinh Nguyen47a16852014-04-14 14:13:34 -07002988 epctrl &= ~(DXEPCTL_EPTYPE_MASK | DXEPCTL_MPS_MASK);
2989 epctrl |= DXEPCTL_MPS(mps);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002990
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02002991 /*
2992 * mark the endpoint as active, otherwise the core may ignore
2993 * transactions entirely for this endpoint
2994 */
Dinh Nguyen47a16852014-04-14 14:13:34 -07002995 epctrl |= DXEPCTL_USBACTEP;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002996
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002997 /* update the endpoint state */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05002998 dwc2_hsotg_set_ep_maxpacket(hsotg, hs_ep->index, mps, dir_in);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01002999
3000 /* default, set to non-periodic */
Robert Baldyga1479e842013-10-09 08:41:57 +02003001 hs_ep->isochronous = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003002 hs_ep->periodic = 0;
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003003 hs_ep->halted = 0;
Robert Baldyga1479e842013-10-09 08:41:57 +02003004 hs_ep->interval = desc->bInterval;
Robert Baldyga4fca54a2013-10-09 09:00:02 +02003005
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003006 switch (desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK) {
3007 case USB_ENDPOINT_XFER_ISOC:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003008 epctrl |= DXEPCTL_EPTYPE_ISO;
3009 epctrl |= DXEPCTL_SETEVENFR;
Robert Baldyga1479e842013-10-09 08:41:57 +02003010 hs_ep->isochronous = 1;
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003011 hs_ep->interval = 1 << (desc->bInterval - 1);
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003012 hs_ep->target_frame = TARGET_FRAME_INITIAL;
3013 if (dir_in) {
Robert Baldyga1479e842013-10-09 08:41:57 +02003014 hs_ep->periodic = 1;
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003015 mask = dwc2_readl(hsotg->regs + DIEPMSK);
3016 mask |= DIEPMSK_NAKMSK;
3017 dwc2_writel(mask, hsotg->regs + DIEPMSK);
3018 } else {
3019 mask = dwc2_readl(hsotg->regs + DOEPMSK);
3020 mask |= DOEPMSK_OUTTKNEPDISMSK;
3021 dwc2_writel(mask, hsotg->regs + DOEPMSK);
3022 }
Robert Baldyga1479e842013-10-09 08:41:57 +02003023 break;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003024
3025 case USB_ENDPOINT_XFER_BULK:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003026 epctrl |= DXEPCTL_EPTYPE_BULK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003027 break;
3028
3029 case USB_ENDPOINT_XFER_INT:
Robert Baldygab203d0a2014-09-09 10:44:56 +02003030 if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003031 hs_ep->periodic = 1;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003032
Vardan Mikayelyan142bd332016-05-25 18:07:07 -07003033 if (hsotg->gadget.speed == USB_SPEED_HIGH)
3034 hs_ep->interval = 1 << (desc->bInterval - 1);
3035
Dinh Nguyen47a16852014-04-14 14:13:34 -07003036 epctrl |= DXEPCTL_EPTYPE_INTERRUPT;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003037 break;
3038
3039 case USB_ENDPOINT_XFER_CONTROL:
Dinh Nguyen47a16852014-04-14 14:13:34 -07003040 epctrl |= DXEPCTL_EPTYPE_CONTROL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003041 break;
3042 }
3043
Mian Yousaf Kaukab4556e122015-01-09 13:39:05 +01003044 /* If fifo is already allocated for this ep */
3045 if (hs_ep->fifo_index) {
3046 size = hs_ep->ep.maxpacket * hs_ep->mc;
3047 /* If bigger fifo is required deallocate current one */
3048 if (size > hs_ep->fifo_size) {
3049 hsotg->fifo_map &= ~(1 << hs_ep->fifo_index);
3050 hs_ep->fifo_index = 0;
3051 hs_ep->fifo_size = 0;
3052 }
3053 }
3054
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003055 /*
3056 * if the hardware has dedicated fifos, we must give each IN EP
Ben Dooks10aebc72010-07-19 09:40:44 +01003057 * a unique tx-fifo even if it is non-periodic.
3058 */
Mian Yousaf Kaukab4556e122015-01-09 13:39:05 +01003059 if (dir_in && hsotg->dedicated_fifos && !hs_ep->fifo_index) {
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003060 u32 fifo_index = 0;
3061 u32 fifo_size = UINT_MAX;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003062 size = hs_ep->ep.maxpacket*hs_ep->mc;
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003063 for (i = 1; i < hsotg->num_of_eps; ++i) {
Robert Baldygab203d0a2014-09-09 10:44:56 +02003064 if (hsotg->fifo_map & (1<<i))
3065 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003066 val = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
Robert Baldygab203d0a2014-09-09 10:44:56 +02003067 val = (val >> FIFOSIZE_DEPTH_SHIFT)*4;
3068 if (val < size)
3069 continue;
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003070 /* Search for smallest acceptable fifo */
3071 if (val < fifo_size) {
3072 fifo_size = val;
3073 fifo_index = i;
3074 }
Robert Baldygab203d0a2014-09-09 10:44:56 +02003075 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003076 if (!fifo_index) {
Mian Yousaf Kaukab5f2196b2015-01-09 13:38:56 +01003077 dev_err(hsotg->dev,
3078 "%s: No suitable fifo found\n", __func__);
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303079 ret = -ENOMEM;
3080 goto error;
3081 }
Mian Yousaf Kaukabca4c55a2015-01-09 13:39:04 +01003082 hsotg->fifo_map |= 1 << fifo_index;
3083 epctrl |= DXEPCTL_TXFNUM(fifo_index);
3084 hs_ep->fifo_index = fifo_index;
3085 hs_ep->fifo_size = fifo_size;
Robert Baldygab203d0a2014-09-09 10:44:56 +02003086 }
Ben Dooks10aebc72010-07-19 09:40:44 +01003087
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003088 /* for non control endpoints, set PID to D0 */
Vardan Mikayelyan837e9f02016-05-25 18:07:22 -07003089 if (index && !hs_ep->isochronous)
Dinh Nguyen47a16852014-04-14 14:13:34 -07003090 epctrl |= DXEPCTL_SETD0PID;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003091
3092 dev_dbg(hsotg->dev, "%s: write DxEPCTL=0x%08x\n",
3093 __func__, epctrl);
3094
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003095 dwc2_writel(epctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003096 dev_dbg(hsotg->dev, "%s: read DxEPCTL=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003097 __func__, dwc2_readl(hsotg->regs + epctrl_reg));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003098
3099 /* enable the endpoint interrupt */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003100 dwc2_hsotg_ctrl_epint(hsotg, index, dir_in, 1);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003101
Sudip Mukherjeeb585a482014-10-17 10:14:02 +05303102error:
Lukasz Majewski22258f42012-06-14 10:02:24 +02003103 spin_unlock_irqrestore(&hsotg->lock, flags);
Julia Lawall19c190f2010-03-29 17:36:44 +02003104 return ret;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003105}
3106
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003107/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003108 * dwc2_hsotg_ep_disable - disable given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003109 * @ep: The endpoint to disable.
3110 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003111static int dwc2_hsotg_ep_disable(struct usb_ep *ep)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003112{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003113 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003114 struct dwc2_hsotg *hsotg = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003115 int dir_in = hs_ep->dir_in;
3116 int index = hs_ep->index;
3117 unsigned long flags;
3118 u32 epctrl_reg;
3119 u32 ctrl;
3120
Marek Szyprowski1e011292014-09-09 10:44:54 +02003121 dev_dbg(hsotg->dev, "%s(ep %p)\n", __func__, ep);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003122
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003123 if (ep == &hsotg->eps_out[0]->ep) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003124 dev_err(hsotg->dev, "%s: called for ep0\n", __func__);
3125 return -EINVAL;
3126 }
3127
Lukasz Majewski94cb8fd2012-05-04 14:17:14 +02003128 epctrl_reg = dir_in ? DIEPCTL(index) : DOEPCTL(index);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003129
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003130 spin_lock_irqsave(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003131
Robert Baldygab203d0a2014-09-09 10:44:56 +02003132 hsotg->fifo_map &= ~(1<<hs_ep->fifo_index);
3133 hs_ep->fifo_index = 0;
3134 hs_ep->fifo_size = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003135
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003136 ctrl = dwc2_readl(hsotg->regs + epctrl_reg);
Dinh Nguyen47a16852014-04-14 14:13:34 -07003137 ctrl &= ~DXEPCTL_EPENA;
3138 ctrl &= ~DXEPCTL_USBACTEP;
3139 ctrl |= DXEPCTL_SNAK;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003140
3141 dev_dbg(hsotg->dev, "%s: DxEPCTL=0x%08x\n", __func__, ctrl);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003142 dwc2_writel(ctrl, hsotg->regs + epctrl_reg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003143
3144 /* disable endpoint interrupts */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003145 dwc2_hsotg_ctrl_epint(hsotg, hs_ep->index, hs_ep->dir_in, 0);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003146
Mian Yousaf Kaukab1141ea02015-01-09 13:38:57 +01003147 /* terminate all requests with shutdown */
3148 kill_all_requests(hsotg, hs_ep, -ESHUTDOWN);
3149
Lukasz Majewski22258f42012-06-14 10:02:24 +02003150 spin_unlock_irqrestore(&hsotg->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003151 return 0;
3152}
3153
3154/**
3155 * on_list - check request is on the given endpoint
3156 * @ep: The endpoint to check.
3157 * @test: The request to test if it is on the endpoint.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003158 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003159static bool on_list(struct dwc2_hsotg_ep *ep, struct dwc2_hsotg_req *test)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003160{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003161 struct dwc2_hsotg_req *req, *treq;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003162
3163 list_for_each_entry_safe(req, treq, &ep->queue, queue) {
3164 if (req == test)
3165 return true;
3166 }
3167
3168 return false;
3169}
3170
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003171static int dwc2_hsotg_wait_bit_set(struct dwc2_hsotg *hs_otg, u32 reg,
3172 u32 bit, u32 timeout)
3173{
3174 u32 i;
3175
3176 for (i = 0; i < timeout; i++) {
3177 if (dwc2_readl(hs_otg->regs + reg) & bit)
3178 return 0;
3179 udelay(1);
3180 }
3181
3182 return -ETIMEDOUT;
3183}
3184
3185static void dwc2_hsotg_ep_stop_xfr(struct dwc2_hsotg *hsotg,
3186 struct dwc2_hsotg_ep *hs_ep)
3187{
3188 u32 epctrl_reg;
3189 u32 epint_reg;
3190
3191 epctrl_reg = hs_ep->dir_in ? DIEPCTL(hs_ep->index) :
3192 DOEPCTL(hs_ep->index);
3193 epint_reg = hs_ep->dir_in ? DIEPINT(hs_ep->index) :
3194 DOEPINT(hs_ep->index);
3195
3196 dev_dbg(hsotg->dev, "%s: stopping transfer on %s\n", __func__,
3197 hs_ep->name);
3198 if (hs_ep->dir_in) {
3199 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_SNAK);
3200 /* Wait for Nak effect */
3201 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg,
3202 DXEPINT_INEPNAKEFF, 100))
3203 dev_warn(hsotg->dev,
3204 "%s: timeout DIEPINT.NAKEFF\n", __func__);
3205 } else {
Vardan Mikayelyan6b58cb02016-05-25 18:07:02 -07003206 if (!(dwc2_readl(hsotg->regs + GINTSTS) & GINTSTS_GOUTNAKEFF))
3207 __orr32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003208
3209 /* Wait for global nak to take effect */
3210 if (dwc2_hsotg_wait_bit_set(hsotg, GINTSTS,
Du, Changbin0676c7e2015-12-04 15:38:23 +08003211 GINTSTS_GOUTNAKEFF, 100))
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003212 dev_warn(hsotg->dev,
Du, Changbin0676c7e2015-12-04 15:38:23 +08003213 "%s: timeout GINTSTS.GOUTNAKEFF\n", __func__);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003214 }
3215
3216 /* Disable ep */
3217 __orr32(hsotg->regs + epctrl_reg, DXEPCTL_EPDIS | DXEPCTL_SNAK);
3218
3219 /* Wait for ep to be disabled */
3220 if (dwc2_hsotg_wait_bit_set(hsotg, epint_reg, DXEPINT_EPDISBLD, 100))
3221 dev_warn(hsotg->dev,
3222 "%s: timeout DOEPCTL.EPDisable\n", __func__);
3223
3224 if (hs_ep->dir_in) {
3225 if (hsotg->dedicated_fifos) {
3226 dwc2_writel(GRSTCTL_TXFNUM(hs_ep->fifo_index) |
3227 GRSTCTL_TXFFLSH, hsotg->regs + GRSTCTL);
3228 /* Wait for fifo flush */
3229 if (dwc2_hsotg_wait_bit_set(hsotg, GRSTCTL,
3230 GRSTCTL_TXFFLSH, 100))
3231 dev_warn(hsotg->dev,
3232 "%s: timeout flushing fifos\n",
3233 __func__);
3234 }
3235 /* TODO: Flush shared tx fifo */
3236 } else {
3237 /* Remove global NAKs */
Du, Changbin0676c7e2015-12-04 15:38:23 +08003238 __bic32(hsotg->regs + DCTL, DCTL_SGOUTNAK);
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003239 }
3240}
3241
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003242/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003243 * dwc2_hsotg_ep_dequeue - dequeue given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003244 * @ep: The endpoint to dequeue.
3245 * @req: The request to be removed from a queue.
3246 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003247static int dwc2_hsotg_ep_dequeue(struct usb_ep *ep, struct usb_request *req)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003248{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003249 struct dwc2_hsotg_req *hs_req = our_req(req);
3250 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003251 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003252 unsigned long flags;
3253
Marek Szyprowski1e011292014-09-09 10:44:54 +02003254 dev_dbg(hs->dev, "ep_dequeue(%p,%p)\n", ep, req);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003255
Lukasz Majewski22258f42012-06-14 10:02:24 +02003256 spin_lock_irqsave(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003257
3258 if (!on_list(hs_ep, hs_req)) {
Lukasz Majewski22258f42012-06-14 10:02:24 +02003259 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003260 return -EINVAL;
3261 }
3262
Mian Yousaf Kaukabc524dd52015-09-29 12:08:24 +02003263 /* Dequeue already started request */
3264 if (req == &hs_ep->req->req)
3265 dwc2_hsotg_ep_stop_xfr(hs, hs_ep);
3266
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003267 dwc2_hsotg_complete_request(hs, hs_ep, hs_req, -ECONNRESET);
Lukasz Majewski22258f42012-06-14 10:02:24 +02003268 spin_unlock_irqrestore(&hs->lock, flags);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003269
3270 return 0;
3271}
3272
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003273/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003274 * dwc2_hsotg_ep_sethalt - set halt on a given endpoint
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003275 * @ep: The endpoint to set halt.
3276 * @value: Set or unset the halt.
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003277 * @now: If true, stall the endpoint now. Otherwise return -EAGAIN if
3278 * the endpoint is busy processing requests.
3279 *
3280 * We need to stall the endpoint immediately if request comes from set_feature
3281 * protocol command handler.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003282 */
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003283static int dwc2_hsotg_ep_sethalt(struct usb_ep *ep, int value, bool now)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003284{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003285 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003286 struct dwc2_hsotg *hs = hs_ep->parent;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003287 int index = hs_ep->index;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003288 u32 epreg;
3289 u32 epctl;
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003290 u32 xfertype;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003291
3292 dev_info(hs->dev, "%s(ep %p %s, %d)\n", __func__, ep, ep->name, value);
3293
Robert Baldygac9f721b2014-01-14 08:36:00 +01003294 if (index == 0) {
3295 if (value)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003296 dwc2_hsotg_stall_ep0(hs);
Robert Baldygac9f721b2014-01-14 08:36:00 +01003297 else
3298 dev_warn(hs->dev,
3299 "%s: can't clear halt on ep0\n", __func__);
3300 return 0;
3301 }
3302
Vahram Aharonyan15186f12016-05-23 22:41:59 -07003303 if (hs_ep->isochronous) {
3304 dev_err(hs->dev, "%s is Isochronous Endpoint\n", ep->name);
3305 return -EINVAL;
3306 }
3307
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003308 if (!now && value && !list_empty(&hs_ep->queue)) {
3309 dev_dbg(hs->dev, "%s request is pending, cannot halt\n",
3310 ep->name);
3311 return -EAGAIN;
3312 }
3313
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003314 if (hs_ep->dir_in) {
3315 epreg = DIEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003316 epctl = dwc2_readl(hs->regs + epreg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003317
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003318 if (value) {
Felipe Balbi5a350d52015-06-29 20:17:22 -05003319 epctl |= DXEPCTL_STALL | DXEPCTL_SNAK;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003320 if (epctl & DXEPCTL_EPENA)
3321 epctl |= DXEPCTL_EPDIS;
3322 } else {
3323 epctl &= ~DXEPCTL_STALL;
3324 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3325 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3326 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3327 epctl |= DXEPCTL_SETD0PID;
3328 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003329 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003330 } else {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003331
3332 epreg = DOEPCTL(index);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003333 epctl = dwc2_readl(hs->regs + epreg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003334
3335 if (value)
3336 epctl |= DXEPCTL_STALL;
3337 else {
3338 epctl &= ~DXEPCTL_STALL;
3339 xfertype = epctl & DXEPCTL_EPTYPE_MASK;
3340 if (xfertype == DXEPCTL_EPTYPE_BULK ||
3341 xfertype == DXEPCTL_EPTYPE_INTERRUPT)
3342 epctl |= DXEPCTL_SETD0PID;
3343 }
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003344 dwc2_writel(epctl, hs->regs + epreg);
Anton Tikhomirov9c39ddc2011-04-21 17:06:41 +09003345 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003346
Robert Baldygaa18ed7b2013-09-19 11:50:21 +02003347 hs_ep->halted = value;
3348
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003349 return 0;
3350}
3351
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003352/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003353 * dwc2_hsotg_ep_sethalt_lock - set halt on a given endpoint with lock held
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003354 * @ep: The endpoint to set halt.
3355 * @value: Set or unset the halt.
3356 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003357static int dwc2_hsotg_ep_sethalt_lock(struct usb_ep *ep, int value)
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003358{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003359 struct dwc2_hsotg_ep *hs_ep = our_ep(ep);
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003360 struct dwc2_hsotg *hs = hs_ep->parent;
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003361 unsigned long flags = 0;
3362 int ret = 0;
3363
3364 spin_lock_irqsave(&hs->lock, flags);
Vahram Aharonyan51da43b2016-05-23 22:41:57 -07003365 ret = dwc2_hsotg_ep_sethalt(ep, value, false);
Lukasz Majewski5ad1d312012-06-14 10:02:26 +02003366 spin_unlock_irqrestore(&hs->lock, flags);
3367
3368 return ret;
3369}
3370
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003371static struct usb_ep_ops dwc2_hsotg_ep_ops = {
3372 .enable = dwc2_hsotg_ep_enable,
3373 .disable = dwc2_hsotg_ep_disable,
3374 .alloc_request = dwc2_hsotg_ep_alloc_request,
3375 .free_request = dwc2_hsotg_ep_free_request,
3376 .queue = dwc2_hsotg_ep_queue_lock,
3377 .dequeue = dwc2_hsotg_ep_dequeue,
3378 .set_halt = dwc2_hsotg_ep_sethalt_lock,
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003379 /* note, don't believe we have any call for the fifo routines */
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003380};
3381
3382/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003383 * dwc2_hsotg_init - initalize the usb core
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003384 * @hsotg: The driver state
3385 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003386static void dwc2_hsotg_init(struct dwc2_hsotg *hsotg)
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003387{
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003388 u32 trdtim;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003389 u32 usbcfg;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003390 /* unmask subset of endpoint interrupts */
3391
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003392 dwc2_writel(DIEPMSK_TIMEOUTMSK | DIEPMSK_AHBERRMSK |
3393 DIEPMSK_EPDISBLDMSK | DIEPMSK_XFERCOMPLMSK,
3394 hsotg->regs + DIEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003395
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003396 dwc2_writel(DOEPMSK_SETUPMSK | DOEPMSK_AHBERRMSK |
3397 DOEPMSK_EPDISBLDMSK | DOEPMSK_XFERCOMPLMSK,
3398 hsotg->regs + DOEPMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003399
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003400 dwc2_writel(0, hsotg->regs + DAINTMSK);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003401
3402 /* Be in disconnected state until gadget is registered */
Dinh Nguyen47a16852014-04-14 14:13:34 -07003403 __orr32(hsotg->regs + DCTL, DCTL_SFTDISCON);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003404
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003405 /* setup fifos */
3406
3407 dev_dbg(hsotg->dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003408 dwc2_readl(hsotg->regs + GRXFSIZ),
3409 dwc2_readl(hsotg->regs + GNPTXFSIZ));
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003410
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003411 dwc2_hsotg_init_fifo(hsotg);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003412
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003413 /* keep other bits untouched (so e.g. forced modes are not lost) */
3414 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3415 usbcfg &= ~(GUSBCFG_TOUTCAL_MASK | GUSBCFG_PHYIF16 | GUSBCFG_SRPCAP |
3416 GUSBCFG_HNPCAP);
3417
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003418 /* set the PLL on, remove the HNP/SRP and set the PHY */
Mian Yousaf Kaukabfa4a8d72015-01-30 09:09:35 +01003419 trdtim = (hsotg->phyif == GUSBCFG_PHYIF8) ? 9 : 5;
Przemek Rudyecd9a7a2016-03-16 23:10:26 +01003420 usbcfg |= hsotg->phyif | GUSBCFG_TOUTCAL(7) |
3421 (trdtim << GUSBCFG_USBTRDTIM_SHIFT);
3422 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003423
Gregory Herrerof5090042015-01-09 13:38:47 +01003424 if (using_dma(hsotg))
3425 __orr32(hsotg->regs + GAHBCFG, GAHBCFG_DMA_EN);
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003426}
3427
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003428/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003429 * dwc2_hsotg_udc_start - prepare the udc for work
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003430 * @gadget: The usb gadget state
3431 * @driver: The usb gadget driver
3432 *
3433 * Perform initialization to prepare udc device and driver
3434 * to work.
3435 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003436static int dwc2_hsotg_udc_start(struct usb_gadget *gadget,
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003437 struct usb_gadget_driver *driver)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003438{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003439 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003440 unsigned long flags;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003441 int ret;
3442
3443 if (!hsotg) {
Pavel Macheka023da32013-09-30 14:56:02 +02003444 pr_err("%s: called with no device\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003445 return -ENODEV;
3446 }
3447
3448 if (!driver) {
3449 dev_err(hsotg->dev, "%s: no driver\n", __func__);
3450 return -EINVAL;
3451 }
3452
Michal Nazarewicz7177aed2011-11-19 18:27:38 +01003453 if (driver->max_speed < USB_SPEED_FULL)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003454 dev_err(hsotg->dev, "%s: bad speed\n", __func__);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003455
Lukasz Majewskif65f0f12012-05-04 14:17:10 +02003456 if (!driver->setup) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003457 dev_err(hsotg->dev, "%s: missing entry points\n", __func__);
3458 return -EINVAL;
3459 }
3460
3461 WARN_ON(hsotg->driver);
3462
3463 driver->driver.bus = NULL;
3464 hsotg->driver = driver;
Alexandre Pereira da Silva7d7b2292012-06-26 11:27:10 -03003465 hsotg->gadget.dev.of_node = hsotg->dev->of_node;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003466 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3467
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003468 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
3469 ret = dwc2_lowlevel_hw_enable(hsotg);
3470 if (ret)
3471 goto err;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003472 }
3473
Gregory Herrerof6c01592015-01-09 13:38:41 +01003474 if (!IS_ERR_OR_NULL(hsotg->uphy))
3475 otg_set_peripheral(hsotg->uphy->otg, &hsotg->gadget);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003476
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003477 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003478 dwc2_hsotg_init(hsotg);
3479 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003480 hsotg->enabled = 0;
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003481 spin_unlock_irqrestore(&hsotg->lock, flags);
3482
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003483 dev_info(hsotg->dev, "bound driver %s\n", driver->driver.name);
Marek Szyprowski5b9451f2014-10-20 12:45:38 +02003484
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003485 return 0;
3486
3487err:
3488 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003489 return ret;
3490}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003491
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003492/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003493 * dwc2_hsotg_udc_stop - stop the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003494 * @gadget: The usb gadget state
3495 * @driver: The usb gadget driver
3496 *
3497 * Stop udc hw block and stay tunned for future transmissions
3498 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003499static int dwc2_hsotg_udc_stop(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003500{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003501 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003502 unsigned long flags = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003503 int ep;
3504
3505 if (!hsotg)
3506 return -ENODEV;
3507
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003508 /* all endpoints should be shutdown */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003509 for (ep = 1; ep < hsotg->num_of_eps; ep++) {
3510 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003511 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003512 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003513 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003514 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003515
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003516 spin_lock_irqsave(&hsotg->lock, flags);
3517
Marek Szyprowski32805c32014-10-20 12:45:33 +02003518 hsotg->driver = NULL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003519 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003520 hsotg->enabled = 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003521
Lukasz Majewski2b19a522012-06-14 10:02:25 +02003522 spin_unlock_irqrestore(&hsotg->lock, flags);
3523
Gregory Herrerof6c01592015-01-09 13:38:41 +01003524 if (!IS_ERR_OR_NULL(hsotg->uphy))
3525 otg_set_peripheral(hsotg->uphy->otg, NULL);
Marek Szyprowskic816c472014-10-20 12:45:37 +02003526
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003527 if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3528 dwc2_lowlevel_hw_disable(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003529
3530 return 0;
3531}
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003532
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003533/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003534 * dwc2_hsotg_gadget_getframe - read the frame number
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003535 * @gadget: The usb gadget state
3536 *
3537 * Read the {micro} frame number
3538 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003539static int dwc2_hsotg_gadget_getframe(struct usb_gadget *gadget)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003540{
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003541 return dwc2_hsotg_read_frameno(to_hsotg(gadget));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003542}
3543
Lukasz Majewskia188b682012-06-22 09:29:56 +02003544/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003545 * dwc2_hsotg_pullup - connect/disconnect the USB PHY
Lukasz Majewskia188b682012-06-22 09:29:56 +02003546 * @gadget: The usb gadget state
3547 * @is_on: Current state of the USB PHY
3548 *
3549 * Connect/Disconnect the USB PHY pullup
3550 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003551static int dwc2_hsotg_pullup(struct usb_gadget *gadget, int is_on)
Lukasz Majewskia188b682012-06-22 09:29:56 +02003552{
Dinh Nguyen941fcce2014-11-11 11:13:33 -06003553 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003554 unsigned long flags = 0;
3555
Gregory Herrero77ba9112015-09-29 12:08:19 +02003556 dev_dbg(hsotg->dev, "%s: is_on: %d op_state: %d\n", __func__, is_on,
3557 hsotg->op_state);
3558
3559 /* Don't modify pullup state while in host mode */
3560 if (hsotg->op_state != OTG_STATE_B_PERIPHERAL) {
3561 hsotg->enabled = is_on;
3562 return 0;
3563 }
Lukasz Majewskia188b682012-06-22 09:29:56 +02003564
3565 spin_lock_irqsave(&hsotg->lock, flags);
3566 if (is_on) {
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003567 hsotg->enabled = 1;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003568 dwc2_hsotg_core_init_disconnected(hsotg, false);
3569 dwc2_hsotg_core_connect(hsotg);
Lukasz Majewskia188b682012-06-22 09:29:56 +02003570 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003571 dwc2_hsotg_core_disconnect(hsotg);
3572 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003573 hsotg->enabled = 0;
Lukasz Majewskia188b682012-06-22 09:29:56 +02003574 }
3575
3576 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3577 spin_unlock_irqrestore(&hsotg->lock, flags);
3578
3579 return 0;
3580}
3581
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003582static int dwc2_hsotg_vbus_session(struct usb_gadget *gadget, int is_active)
Gregory Herrero83d98222015-01-09 13:39:02 +01003583{
3584 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3585 unsigned long flags;
3586
3587 dev_dbg(hsotg->dev, "%s: is_active: %d\n", __func__, is_active);
3588 spin_lock_irqsave(&hsotg->lock, flags);
3589
Gregory Herrero61f72232015-09-29 12:08:28 +02003590 /*
3591 * If controller is hibernated, it must exit from hibernation
3592 * before being initialized / de-initialized
3593 */
3594 if (hsotg->lx_state == DWC2_L2)
3595 dwc2_exit_hibernation(hsotg, false);
3596
Gregory Herrero83d98222015-01-09 13:39:02 +01003597 if (is_active) {
Gregory Herrerocd0e6412015-09-29 12:08:20 +02003598 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Gregory Herrero065d3932015-09-22 15:16:54 +02003599
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003600 dwc2_hsotg_core_init_disconnected(hsotg, false);
Gregory Herrero83d98222015-01-09 13:39:02 +01003601 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003602 dwc2_hsotg_core_connect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003603 } else {
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003604 dwc2_hsotg_core_disconnect(hsotg);
3605 dwc2_hsotg_disconnect(hsotg);
Gregory Herrero83d98222015-01-09 13:39:02 +01003606 }
3607
3608 spin_unlock_irqrestore(&hsotg->lock, flags);
3609 return 0;
3610}
3611
Gregory Herrero596d6962015-01-09 13:39:08 +01003612/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003613 * dwc2_hsotg_vbus_draw - report bMaxPower field
Gregory Herrero596d6962015-01-09 13:39:08 +01003614 * @gadget: The usb gadget state
3615 * @mA: Amount of current
3616 *
3617 * Report how much power the device may consume to the phy.
3618 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003619static int dwc2_hsotg_vbus_draw(struct usb_gadget *gadget, unsigned mA)
Gregory Herrero596d6962015-01-09 13:39:08 +01003620{
3621 struct dwc2_hsotg *hsotg = to_hsotg(gadget);
3622
3623 if (IS_ERR_OR_NULL(hsotg->uphy))
3624 return -ENOTSUPP;
3625 return usb_phy_set_power(hsotg->uphy, mA);
3626}
3627
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003628static const struct usb_gadget_ops dwc2_hsotg_gadget_ops = {
3629 .get_frame = dwc2_hsotg_gadget_getframe,
3630 .udc_start = dwc2_hsotg_udc_start,
3631 .udc_stop = dwc2_hsotg_udc_stop,
3632 .pullup = dwc2_hsotg_pullup,
3633 .vbus_session = dwc2_hsotg_vbus_session,
3634 .vbus_draw = dwc2_hsotg_vbus_draw,
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003635};
3636
3637/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003638 * dwc2_hsotg_initep - initialise a single endpoint
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003639 * @hsotg: The device state.
3640 * @hs_ep: The endpoint to be initialised.
3641 * @epnum: The endpoint number
3642 *
3643 * Initialise the given endpoint (as part of the probe and device state
3644 * creation) to give to the gadget driver. Setup the endpoint name, any
3645 * direction information and other state that may be required.
3646 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003647static void dwc2_hsotg_initep(struct dwc2_hsotg *hsotg,
3648 struct dwc2_hsotg_ep *hs_ep,
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003649 int epnum,
3650 bool dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003651{
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003652 char *dir;
3653
3654 if (epnum == 0)
3655 dir = "";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003656 else if (dir_in)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003657 dir = "in";
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003658 else
3659 dir = "out";
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003660
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003661 hs_ep->dir_in = dir_in;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003662 hs_ep->index = epnum;
3663
3664 snprintf(hs_ep->name, sizeof(hs_ep->name), "ep%d%s", epnum, dir);
3665
3666 INIT_LIST_HEAD(&hs_ep->queue);
3667 INIT_LIST_HEAD(&hs_ep->ep.ep_list);
3668
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003669 /* add to the list of endpoints known by the gadget driver */
3670 if (epnum)
3671 list_add_tail(&hs_ep->ep.ep_list, &hsotg->gadget.ep_list);
3672
3673 hs_ep->parent = hsotg;
3674 hs_ep->ep.name = hs_ep->name;
Robert Baldygae117e742013-12-13 12:23:38 +01003675 usb_ep_set_maxpacket_limit(&hs_ep->ep, epnum ? 1024 : EP0_MPS_LIMIT);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003676 hs_ep->ep.ops = &dwc2_hsotg_ep_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003677
Robert Baldyga29545222015-07-31 16:00:18 +02003678 if (epnum == 0) {
3679 hs_ep->ep.caps.type_control = true;
3680 } else {
3681 hs_ep->ep.caps.type_iso = true;
3682 hs_ep->ep.caps.type_bulk = true;
3683 hs_ep->ep.caps.type_int = true;
3684 }
3685
3686 if (dir_in)
3687 hs_ep->ep.caps.dir_in = true;
3688 else
3689 hs_ep->ep.caps.dir_out = true;
3690
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003691 /*
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003692 * if we're using dma, we need to set the next-endpoint pointer
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003693 * to be something valid.
3694 */
3695
3696 if (using_dma(hsotg)) {
Dinh Nguyen47a16852014-04-14 14:13:34 -07003697 u32 next = DXEPCTL_NEXTEP((epnum + 1) % 15);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003698 if (dir_in)
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003699 dwc2_writel(next, hsotg->regs + DIEPCTL(epnum));
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003700 else
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003701 dwc2_writel(next, hsotg->regs + DOEPCTL(epnum));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003702 }
3703}
3704
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003705/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003706 * dwc2_hsotg_hw_cfg - read HW configuration registers
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003707 * @param: The device state
3708 *
3709 * Read the USB core HW configuration registers
3710 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003711static int dwc2_hsotg_hw_cfg(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003712{
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003713 u32 cfg;
3714 u32 ep_type;
3715 u32 i;
3716
Ben Dooks10aebc72010-07-19 09:40:44 +01003717 /* check hardware configuration */
3718
John Youn43e90342015-12-17 11:17:45 -08003719 hsotg->num_of_eps = hsotg->hw_params.num_dev_ep;
3720
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003721 /* Add ep0 */
3722 hsotg->num_of_eps++;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003723
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003724 hsotg->eps_in[0] = devm_kzalloc(hsotg->dev, sizeof(struct dwc2_hsotg_ep),
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003725 GFP_KERNEL);
3726 if (!hsotg->eps_in[0])
3727 return -ENOMEM;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003728 /* Same dwc2_hsotg_ep is used in both directions for ep0 */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003729 hsotg->eps_out[0] = hsotg->eps_in[0];
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003730
John Youn43e90342015-12-17 11:17:45 -08003731 cfg = hsotg->hw_params.dev_ep_dirs;
Roshan Pius251a17f2015-02-02 14:55:38 -08003732 for (i = 1, cfg >>= 2; i < hsotg->num_of_eps; i++, cfg >>= 2) {
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003733 ep_type = cfg & 3;
3734 /* Direction in or both */
3735 if (!(ep_type & 2)) {
3736 hsotg->eps_in[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003737 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003738 if (!hsotg->eps_in[i])
3739 return -ENOMEM;
3740 }
3741 /* Direction out or both */
3742 if (!(ep_type & 1)) {
3743 hsotg->eps_out[i] = devm_kzalloc(hsotg->dev,
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003744 sizeof(struct dwc2_hsotg_ep), GFP_KERNEL);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003745 if (!hsotg->eps_out[i])
3746 return -ENOMEM;
3747 }
3748 }
3749
John Youn43e90342015-12-17 11:17:45 -08003750 hsotg->fifo_mem = hsotg->hw_params.total_fifo_size;
3751 hsotg->dedicated_fifos = hsotg->hw_params.en_multiple_tx_fifo;
Ben Dooks10aebc72010-07-19 09:40:44 +01003752
Marek Szyprowskicff9eb72014-09-09 10:44:55 +02003753 dev_info(hsotg->dev, "EPs: %d, %s fifos, %d entries in SPRAM\n",
3754 hsotg->num_of_eps,
3755 hsotg->dedicated_fifos ? "dedicated" : "shared",
3756 hsotg->fifo_mem);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003757 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003758}
3759
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003760/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003761 * dwc2_hsotg_dump - dump state of the udc
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003762 * @param: The device state
3763 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003764static void dwc2_hsotg_dump(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003765{
Mark Brown83a01802011-06-01 17:16:15 +01003766#ifdef DEBUG
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003767 struct device *dev = hsotg->dev;
3768 void __iomem *regs = hsotg->regs;
3769 u32 val;
3770 int idx;
3771
3772 dev_info(dev, "DCFG=0x%08x, DCTL=0x%08x, DIEPMSK=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003773 dwc2_readl(regs + DCFG), dwc2_readl(regs + DCTL),
3774 dwc2_readl(regs + DIEPMSK));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003775
Mian Yousaf Kaukabf889f232015-01-30 09:09:36 +01003776 dev_info(dev, "GAHBCFG=0x%08x, GHWCFG1=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003777 dwc2_readl(regs + GAHBCFG), dwc2_readl(regs + GHWCFG1));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003778
3779 dev_info(dev, "GRXFSIZ=0x%08x, GNPTXFSIZ=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003780 dwc2_readl(regs + GRXFSIZ), dwc2_readl(regs + GNPTXFSIZ));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003781
3782 /* show periodic fifo settings */
3783
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003784 for (idx = 1; idx < hsotg->num_of_eps; idx++) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003785 val = dwc2_readl(regs + DPTXFSIZN(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003786 dev_info(dev, "DPTx[%d] FSize=%d, StAddr=0x%08x\n", idx,
Dinh Nguyen47a16852014-04-14 14:13:34 -07003787 val >> FIFOSIZE_DEPTH_SHIFT,
3788 val & FIFOSIZE_STARTADDR_MASK);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003789 }
3790
Mian Yousaf Kaukab364f8e92015-01-09 13:38:55 +01003791 for (idx = 0; idx < hsotg->num_of_eps; idx++) {
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003792 dev_info(dev,
3793 "ep%d-in: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n", idx,
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003794 dwc2_readl(regs + DIEPCTL(idx)),
3795 dwc2_readl(regs + DIEPTSIZ(idx)),
3796 dwc2_readl(regs + DIEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003797
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003798 val = dwc2_readl(regs + DOEPCTL(idx));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003799 dev_info(dev,
3800 "ep%d-out: EPCTL=0x%08x, SIZ=0x%08x, DMA=0x%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003801 idx, dwc2_readl(regs + DOEPCTL(idx)),
3802 dwc2_readl(regs + DOEPTSIZ(idx)),
3803 dwc2_readl(regs + DOEPDMA(idx)));
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003804
3805 }
3806
3807 dev_info(dev, "DVBUSDIS=0x%08x, DVBUSPULSE=%08x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003808 dwc2_readl(regs + DVBUSDIS), dwc2_readl(regs + DVBUSPULSE));
Mark Brown83a01802011-06-01 17:16:15 +01003809#endif
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003810}
3811
Gregory Herreroedd74be2015-01-09 13:38:48 +01003812#ifdef CONFIG_OF
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003813static void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg)
Gregory Herreroedd74be2015-01-09 13:38:48 +01003814{
3815 struct device_node *np = hsotg->dev->of_node;
Gregory Herrero0a176272015-01-09 13:38:52 +01003816 u32 len = 0;
3817 u32 i = 0;
Gregory Herreroedd74be2015-01-09 13:38:48 +01003818
3819 /* Enable dma if requested in device tree */
3820 hsotg->g_using_dma = of_property_read_bool(np, "g-use-dma");
Gregory Herrero0a176272015-01-09 13:38:52 +01003821
3822 /*
3823 * Register TX periodic fifo size per endpoint.
3824 * EP0 is excluded since it has no fifo configuration.
3825 */
3826 if (!of_find_property(np, "g-tx-fifo-size", &len))
3827 goto rx_fifo;
3828
3829 len /= sizeof(u32);
3830
3831 /* Read tx fifo sizes other than ep0 */
3832 if (of_property_read_u32_array(np, "g-tx-fifo-size",
3833 &hsotg->g_tx_fifo_sz[1], len))
3834 goto rx_fifo;
3835
3836 /* Add ep0 */
3837 len++;
3838
3839 /* Make remaining TX fifos unavailable */
3840 if (len < MAX_EPS_CHANNELS) {
3841 for (i = len; i < MAX_EPS_CHANNELS; i++)
3842 hsotg->g_tx_fifo_sz[i] = 0;
3843 }
3844
3845rx_fifo:
3846 /* Register RX fifo size */
3847 of_property_read_u32(np, "g-rx-fifo-size", &hsotg->g_rx_fifo_sz);
3848
3849 /* Register NPTX fifo size */
3850 of_property_read_u32(np, "g-np-tx-fifo-size",
3851 &hsotg->g_np_g_tx_fifo_sz);
Gregory Herreroedd74be2015-01-09 13:38:48 +01003852}
3853#else
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003854static inline void dwc2_hsotg_of_probe(struct dwc2_hsotg *hsotg) { }
Gregory Herreroedd74be2015-01-09 13:38:48 +01003855#endif
3856
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003857/**
Dinh Nguyen117777b2014-11-11 11:13:34 -06003858 * dwc2_gadget_init - init function for gadget
3859 * @dwc2: The data structure for the DWC2 driver.
3860 * @irq: The IRQ number for the controller.
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003861 */
Dinh Nguyen117777b2014-11-11 11:13:34 -06003862int dwc2_gadget_init(struct dwc2_hsotg *hsotg, int irq)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003863{
Dinh Nguyen117777b2014-11-11 11:13:34 -06003864 struct device *dev = hsotg->dev;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003865 int epnum;
3866 int ret;
Lukasz Majewskifc9a7312012-05-04 14:17:02 +02003867 int i;
Gregory Herrero0a176272015-01-09 13:38:52 +01003868 u32 p_tx_fifo[] = DWC2_G_P_LEGACY_TX_FIFO_SIZE;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003869
Gregory Herrero0a176272015-01-09 13:38:52 +01003870 /* Initialize to legacy fifo configuration values */
3871 hsotg->g_rx_fifo_sz = 2048;
3872 hsotg->g_np_g_tx_fifo_sz = 1024;
3873 memcpy(&hsotg->g_tx_fifo_sz[1], p_tx_fifo, sizeof(p_tx_fifo));
3874 /* Device tree specific probe */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003875 dwc2_hsotg_of_probe(hsotg);
John Youn43e90342015-12-17 11:17:45 -08003876
3877 /* Check against largest possible value. */
3878 if (hsotg->g_np_g_tx_fifo_sz >
3879 hsotg->hw_params.dev_nperio_tx_fifo_size) {
3880 dev_warn(dev, "Specified GNPTXFDEP=%d > %d\n",
3881 hsotg->g_np_g_tx_fifo_sz,
3882 hsotg->hw_params.dev_nperio_tx_fifo_size);
3883 hsotg->g_np_g_tx_fifo_sz =
3884 hsotg->hw_params.dev_nperio_tx_fifo_size;
3885 }
3886
Gregory Herrero0a176272015-01-09 13:38:52 +01003887 /* Dump fifo information */
3888 dev_dbg(dev, "NonPeriodic TXFIFO size: %d\n",
3889 hsotg->g_np_g_tx_fifo_sz);
3890 dev_dbg(dev, "RXFIFO size: %d\n", hsotg->g_rx_fifo_sz);
3891 for (i = 0; i < MAX_EPS_CHANNELS; i++)
3892 dev_dbg(dev, "Periodic TXFIFO%2d size: %d\n", i,
3893 hsotg->g_tx_fifo_sz[i]);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003894
Michal Nazarewiczd327ab52011-11-19 18:27:37 +01003895 hsotg->gadget.max_speed = USB_SPEED_HIGH;
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003896 hsotg->gadget.ops = &dwc2_hsotg_gadget_ops;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003897 hsotg->gadget.name = dev_name(dev);
Gregory Herrero097ee662015-04-29 22:09:10 +02003898 if (hsotg->dr_mode == USB_DR_MODE_OTG)
3899 hsotg->gadget.is_otg = 1;
Mian Yousaf Kaukabec4cc652015-09-22 15:16:55 +02003900 else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL)
3901 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003902
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003903 ret = dwc2_hsotg_hw_cfg(hsotg);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003904 if (ret) {
3905 dev_err(hsotg->dev, "Hardware configuration failed: %d\n", ret);
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003906 return ret;
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003907 }
3908
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003909 hsotg->ctrl_buff = devm_kzalloc(hsotg->dev,
3910 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3911 if (!hsotg->ctrl_buff) {
3912 dev_err(dev, "failed to allocate ctrl request buff\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003913 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003914 }
3915
3916 hsotg->ep0_buff = devm_kzalloc(hsotg->dev,
3917 DWC2_CTRL_BUFF_SIZE, GFP_KERNEL);
3918 if (!hsotg->ep0_buff) {
3919 dev_err(dev, "failed to allocate ctrl reply buff\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003920 return -ENOMEM;
Mian Yousaf Kaukab3f950012015-01-09 13:38:44 +01003921 }
3922
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003923 ret = devm_request_irq(hsotg->dev, irq, dwc2_hsotg_irq, IRQF_SHARED,
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003924 dev_name(hsotg->dev), hsotg);
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003925 if (ret < 0) {
Dinh Nguyendb8178c2014-11-11 11:13:37 -06003926 dev_err(dev, "cannot claim IRQ for gadget\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003927 return ret;
Marek Szyprowskieb3c56c2014-09-09 10:44:12 +02003928 }
3929
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003930 /* hsotg->num_of_eps holds number of EPs other than ep0 */
3931
3932 if (hsotg->num_of_eps == 0) {
3933 dev_err(dev, "wrong number of EPs (zero)\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003934 return -EINVAL;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003935 }
3936
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003937 /* setup endpoint information */
3938
3939 INIT_LIST_HEAD(&hsotg->gadget.ep_list);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003940 hsotg->gadget.ep0 = &hsotg->eps_out[0]->ep;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003941
3942 /* allocate EP0 request */
3943
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003944 hsotg->ctrl_req = dwc2_hsotg_ep_alloc_request(&hsotg->eps_out[0]->ep,
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003945 GFP_KERNEL);
3946 if (!hsotg->ctrl_req) {
3947 dev_err(dev, "failed to allocate ctrl req\n");
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003948 return -ENOMEM;
Lukasz Majewskib3f489b2012-05-04 14:17:09 +02003949 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003950
3951 /* initialise the endpoints now the core has been initialised */
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003952 for (epnum = 0; epnum < hsotg->num_of_eps; epnum++) {
3953 if (hsotg->eps_in[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003954 dwc2_hsotg_initep(hsotg, hsotg->eps_in[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003955 epnum, 1);
3956 if (hsotg->eps_out[epnum])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003957 dwc2_hsotg_initep(hsotg, hsotg->eps_out[epnum],
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01003958 epnum, 0);
3959 }
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003960
Dinh Nguyen117777b2014-11-11 11:13:34 -06003961 ret = usb_add_gadget_udc(dev, &hsotg->gadget);
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003962 if (ret)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003963 return ret;
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003964
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003965 dwc2_hsotg_dump(hsotg);
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003966
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003967 return 0;
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003968}
3969
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003970/**
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003971 * dwc2_hsotg_remove - remove function for hsotg driver
Lukasz Majewski8b9bc462012-05-04 14:17:11 +02003972 * @pdev: The platform information for the driver
3973 */
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003974int dwc2_hsotg_remove(struct dwc2_hsotg *hsotg)
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003975{
Sebastian Andrzej Siewior0f913492011-06-28 16:33:47 +03003976 usb_del_gadget_udc(&hsotg->gadget);
Marek Szyprowski31ee04d2010-07-19 16:01:42 +02003977
Ben Dooks5b7d70c2009-06-02 14:58:06 +01003978 return 0;
3979}
3980
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003981int dwc2_hsotg_suspend(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003982{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003983 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003984
Gregory Herrero9e779772015-04-29 22:09:07 +02003985 if (hsotg->lx_state != DWC2_L0)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02003986 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02003987
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003988 if (hsotg->driver) {
3989 int ep;
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003990
Marek Szyprowskib83e3332014-02-28 13:06:11 +01003991 dev_info(hsotg->dev, "suspending usb gadget %s\n",
3992 hsotg->driver->driver.name);
3993
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003994 spin_lock_irqsave(&hsotg->lock, flags);
3995 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003996 dwc2_hsotg_core_disconnect(hsotg);
3997 dwc2_hsotg_disconnect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01003998 hsotg->gadget.speed = USB_SPEED_UNKNOWN;
3999 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004000
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004001 for (ep = 0; ep < hsotg->num_of_eps; ep++) {
4002 if (hsotg->eps_in[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004003 dwc2_hsotg_ep_disable(&hsotg->eps_in[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004004 if (hsotg->eps_out[ep])
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004005 dwc2_hsotg_ep_disable(&hsotg->eps_out[ep]->ep);
Mian Yousaf Kaukabc6f5c052015-01-09 13:38:50 +01004006 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004007 }
4008
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004009 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004010}
4011
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004012int dwc2_hsotg_resume(struct dwc2_hsotg *hsotg)
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004013{
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004014 unsigned long flags;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004015
Gregory Herrero9e779772015-04-29 22:09:07 +02004016 if (hsotg->lx_state == DWC2_L2)
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004017 return 0;
Gregory Herrero9e779772015-04-29 22:09:07 +02004018
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004019 if (hsotg->driver) {
4020 dev_info(hsotg->dev, "resuming usb gadget %s\n",
4021 hsotg->driver->driver.name);
Robert Baldygad00b4142014-09-09 10:44:57 +02004022
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004023 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004024 dwc2_hsotg_core_init_disconnected(hsotg, false);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004025 if (hsotg->enabled)
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05004026 dwc2_hsotg_core_connect(hsotg);
Marek Szyprowskidc6e69e2014-11-21 15:14:49 +01004027 spin_unlock_irqrestore(&hsotg->lock, flags);
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004028 }
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004029
Marek Szyprowski09a75e82015-10-14 08:52:29 +02004030 return 0;
Marek Szyprowskib83e3332014-02-28 13:06:11 +01004031}
John Youn58e52ff6a2016-02-23 19:54:57 -08004032
4033/**
4034 * dwc2_backup_device_registers() - Backup controller device registers.
4035 * When suspending usb bus, registers needs to be backuped
4036 * if controller power is disabled once suspended.
4037 *
4038 * @hsotg: Programming view of the DWC_otg controller
4039 */
4040int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
4041{
4042 struct dwc2_dregs_backup *dr;
4043 int i;
4044
4045 dev_dbg(hsotg->dev, "%s\n", __func__);
4046
4047 /* Backup dev regs */
4048 dr = &hsotg->dr_backup;
4049
4050 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
4051 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
4052 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
4053 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
4054 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
4055
4056 for (i = 0; i < hsotg->num_of_eps; i++) {
4057 /* Backup IN EPs */
4058 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
4059
4060 /* Ensure DATA PID is correctly configured */
4061 if (dr->diepctl[i] & DXEPCTL_DPID)
4062 dr->diepctl[i] |= DXEPCTL_SETD1PID;
4063 else
4064 dr->diepctl[i] |= DXEPCTL_SETD0PID;
4065
4066 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
4067 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
4068
4069 /* Backup OUT EPs */
4070 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
4071
4072 /* Ensure DATA PID is correctly configured */
4073 if (dr->doepctl[i] & DXEPCTL_DPID)
4074 dr->doepctl[i] |= DXEPCTL_SETD1PID;
4075 else
4076 dr->doepctl[i] |= DXEPCTL_SETD0PID;
4077
4078 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
4079 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
4080 }
4081 dr->valid = true;
4082 return 0;
4083}
4084
4085/**
4086 * dwc2_restore_device_registers() - Restore controller device registers.
4087 * When resuming usb bus, device registers needs to be restored
4088 * if controller power were disabled.
4089 *
4090 * @hsotg: Programming view of the DWC_otg controller
4091 */
4092int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
4093{
4094 struct dwc2_dregs_backup *dr;
4095 u32 dctl;
4096 int i;
4097
4098 dev_dbg(hsotg->dev, "%s\n", __func__);
4099
4100 /* Restore dev regs */
4101 dr = &hsotg->dr_backup;
4102 if (!dr->valid) {
4103 dev_err(hsotg->dev, "%s: no device registers to restore\n",
4104 __func__);
4105 return -EINVAL;
4106 }
4107 dr->valid = false;
4108
4109 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
4110 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
4111 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
4112 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
4113 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
4114
4115 for (i = 0; i < hsotg->num_of_eps; i++) {
4116 /* Restore IN EPs */
4117 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
4118 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
4119 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
4120
4121 /* Restore OUT EPs */
4122 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
4123 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
4124 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
4125 }
4126
4127 /* Set the Power-On Programming done bit */
4128 dctl = dwc2_readl(hsotg->regs + DCTL);
4129 dctl |= DCTL_PWRONPRGDONE;
4130 dwc2_writel(dctl, hsotg->regs + DCTL);
4131
4132 return 0;
4133}