blob: e8fee0f969f036c6a158520360a5deb77e3b363c [file] [log] [blame]
Paul Zimmerman7359d482013-03-11 17:47:59 -07001/*
2 * hcd.c - DesignWare HS OTG Controller host-mode routines
3 *
4 * Copyright (C) 2004-2013 Synopsys, Inc.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
18 *
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
22 * later version.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 */
36
37/*
38 * This file contains the core HCD code, and implements the Linux hc_driver
39 * API
40 */
41#include <linux/kernel.h>
42#include <linux/module.h>
43#include <linux/spinlock.h>
44#include <linux/interrupt.h>
Heiner Kallweit348becd2017-01-25 23:10:51 +010045#include <linux/platform_device.h>
Paul Zimmerman7359d482013-03-11 17:47:59 -070046#include <linux/dma-mapping.h>
47#include <linux/delay.h>
48#include <linux/io.h>
49#include <linux/slab.h>
50#include <linux/usb.h>
51
52#include <linux/usb/hcd.h>
53#include <linux/usb/ch11.h>
54
55#include "core.h"
56#include "hcd.h"
57
Chen Yu9156a7e2017-01-23 14:59:57 -080058static void dwc2_port_resume(struct dwc2_hsotg *hsotg);
59
John Younb02038fa2016-02-23 19:55:00 -080060/*
61 * =========================================================================
62 * Host Core Layer Functions
63 * =========================================================================
64 */
65
66/**
67 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
68 * used in both device and host modes
69 *
70 * @hsotg: Programming view of the DWC_otg controller
71 */
72static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
73{
74 u32 intmsk;
75
76 /* Clear any pending OTG Interrupts */
77 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
78
79 /* Clear any pending interrupts */
80 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
81
82 /* Enable the interrupts in the GINTMSK */
83 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
84
John Youn95832c02017-01-23 14:57:26 -080085 if (!hsotg->params.host_dma)
John Younb02038fa2016-02-23 19:55:00 -080086 intmsk |= GINTSTS_RXFLVL;
John Youn95832c02017-01-23 14:57:26 -080087 if (!hsotg->params.external_id_pin_ctl)
John Younb02038fa2016-02-23 19:55:00 -080088 intmsk |= GINTSTS_CONIDSTSCHNG;
89
90 intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
91 GINTSTS_SESSREQINT;
92
93 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
94}
95
96/*
97 * Initializes the FSLSPClkSel field of the HCFG register depending on the
98 * PHY type
99 */
100static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
101{
102 u32 hcfg, val;
103
104 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
105 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
John Youn95832c02017-01-23 14:57:26 -0800106 hsotg->params.ulpi_fs_ls) ||
John Younbea8e862016-11-03 17:55:53 -0700107 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
John Younb02038fa2016-02-23 19:55:00 -0800108 /* Full speed PHY */
109 val = HCFG_FSLSPCLKSEL_48_MHZ;
110 } else {
111 /* High speed PHY running at full speed or high speed */
112 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
113 }
114
115 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
116 hcfg = dwc2_readl(hsotg->regs + HCFG);
117 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
118 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
119 dwc2_writel(hcfg, hsotg->regs + HCFG);
120}
121
122static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
123{
Bruno Herrerae35b1352017-01-31 23:25:43 -0200124 u32 usbcfg, ggpio, i2cctl;
John Younb02038fa2016-02-23 19:55:00 -0800125 int retval = 0;
126
127 /*
128 * core_init() is now called on every switch so only call the
129 * following for the first time through
130 */
131 if (select_phy) {
132 dev_dbg(hsotg->dev, "FS PHY selected\n");
133
134 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
135 if (!(usbcfg & GUSBCFG_PHYSEL)) {
136 usbcfg |= GUSBCFG_PHYSEL;
137 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
138
139 /* Reset after a PHY select */
140 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
141
142 if (retval) {
143 dev_err(hsotg->dev,
144 "%s: Reset failed, aborting", __func__);
145 return retval;
146 }
147 }
Bruno Herrerae35b1352017-01-31 23:25:43 -0200148
149 if (hsotg->params.activate_stm_fs_transceiver) {
150 ggpio = dwc2_readl(hsotg->regs + GGPIO);
151 if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
152 dev_dbg(hsotg->dev, "Activating transceiver\n");
153 /*
154 * STM32F4x9 uses the GGPIO register as general
155 * core configuration register.
156 */
157 ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
158 dwc2_writel(ggpio, hsotg->regs + GGPIO);
159 }
160 }
John Younb02038fa2016-02-23 19:55:00 -0800161 }
162
163 /*
164 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
165 * do this on HNP Dev/Host mode switches (done in dev_init and
166 * host_init).
167 */
168 if (dwc2_is_host_mode(hsotg))
169 dwc2_init_fs_ls_pclk_sel(hsotg);
170
John Youn95832c02017-01-23 14:57:26 -0800171 if (hsotg->params.i2c_enable) {
John Younb02038fa2016-02-23 19:55:00 -0800172 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
173
174 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
175 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
176 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
177 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
178
179 /* Program GI2CCTL.I2CEn */
180 i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
181 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
182 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
183 i2cctl &= ~GI2CCTL_I2CEN;
184 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
185 i2cctl |= GI2CCTL_I2CEN;
186 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
187 }
188
189 return retval;
190}
191
192static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
193{
194 u32 usbcfg, usbcfg_old;
195 int retval = 0;
196
197 if (!select_phy)
198 return 0;
199
200 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
201 usbcfg_old = usbcfg;
202
203 /*
204 * HS PHY parameters. These parameters are preserved during soft reset
205 * so only program the first time. Do a soft reset immediately after
206 * setting phyif.
207 */
John Younbea8e862016-11-03 17:55:53 -0700208 switch (hsotg->params.phy_type) {
John Younb02038fa2016-02-23 19:55:00 -0800209 case DWC2_PHY_TYPE_PARAM_ULPI:
210 /* ULPI interface */
211 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
212 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
213 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
John Youn95832c02017-01-23 14:57:26 -0800214 if (hsotg->params.phy_ulpi_ddr)
John Younb02038fa2016-02-23 19:55:00 -0800215 usbcfg |= GUSBCFG_DDRSEL;
Dinh Nguyenb11633c2017-10-16 08:57:18 -0500216
217 /* Set external VBUS indicator as needed. */
218 if (hsotg->params.oc_disable)
219 usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
220 GUSBCFG_INDICATORPASSTHROUGH);
John Younb02038fa2016-02-23 19:55:00 -0800221 break;
222 case DWC2_PHY_TYPE_PARAM_UTMI:
223 /* UTMI+ interface */
224 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
225 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
John Younbea8e862016-11-03 17:55:53 -0700226 if (hsotg->params.phy_utmi_width == 16)
John Younb02038fa2016-02-23 19:55:00 -0800227 usbcfg |= GUSBCFG_PHYIF16;
228 break;
229 default:
230 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
231 break;
232 }
233
234 if (usbcfg != usbcfg_old) {
235 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
236
237 /* Reset after setting the PHY parameters */
238 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
239 if (retval) {
240 dev_err(hsotg->dev,
241 "%s: Reset failed, aborting", __func__);
242 return retval;
243 }
244 }
245
246 return retval;
247}
248
249static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
250{
251 u32 usbcfg;
252 int retval = 0;
253
Vardan Mikayelyan38e90022016-11-14 19:17:03 -0800254 if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
255 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
John Younbea8e862016-11-03 17:55:53 -0700256 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -0800257 /* If FS/LS mode with FS/LS PHY */
John Younb02038fa2016-02-23 19:55:00 -0800258 retval = dwc2_fs_phy_init(hsotg, select_phy);
259 if (retval)
260 return retval;
261 } else {
262 /* High speed PHY */
263 retval = dwc2_hs_phy_init(hsotg, select_phy);
264 if (retval)
265 return retval;
266 }
267
268 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
269 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
John Youn95832c02017-01-23 14:57:26 -0800270 hsotg->params.ulpi_fs_ls) {
John Younb02038fa2016-02-23 19:55:00 -0800271 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
272 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
273 usbcfg |= GUSBCFG_ULPI_FS_LS;
274 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
275 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
276 } else {
277 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
278 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
279 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
280 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
281 }
282
283 return retval;
284}
285
286static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
287{
288 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
289
290 switch (hsotg->hw_params.arch) {
291 case GHWCFG2_EXT_DMA_ARCH:
292 dev_err(hsotg->dev, "External DMA Mode not supported\n");
293 return -EINVAL;
294
295 case GHWCFG2_INT_DMA_ARCH:
296 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
John Younbea8e862016-11-03 17:55:53 -0700297 if (hsotg->params.ahbcfg != -1) {
John Younb02038fa2016-02-23 19:55:00 -0800298 ahbcfg &= GAHBCFG_CTRL_MASK;
John Younbea8e862016-11-03 17:55:53 -0700299 ahbcfg |= hsotg->params.ahbcfg &
John Younb02038fa2016-02-23 19:55:00 -0800300 ~GAHBCFG_CTRL_MASK;
301 }
302 break;
303
304 case GHWCFG2_SLAVE_ONLY_ARCH:
305 default:
306 dev_dbg(hsotg->dev, "Slave Only Mode\n");
307 break;
308 }
309
John Youne7839f92016-11-03 17:56:07 -0700310 dev_dbg(hsotg->dev, "host_dma:%d dma_desc_enable:%d\n",
311 hsotg->params.host_dma,
John Younbea8e862016-11-03 17:55:53 -0700312 hsotg->params.dma_desc_enable);
John Younb02038fa2016-02-23 19:55:00 -0800313
John Youn95832c02017-01-23 14:57:26 -0800314 if (hsotg->params.host_dma) {
315 if (hsotg->params.dma_desc_enable)
John Younb02038fa2016-02-23 19:55:00 -0800316 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
317 else
318 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
319 } else {
320 dev_dbg(hsotg->dev, "Using Slave mode\n");
John Youn95832c02017-01-23 14:57:26 -0800321 hsotg->params.dma_desc_enable = false;
John Younb02038fa2016-02-23 19:55:00 -0800322 }
323
John Youn95832c02017-01-23 14:57:26 -0800324 if (hsotg->params.host_dma)
John Younb02038fa2016-02-23 19:55:00 -0800325 ahbcfg |= GAHBCFG_DMA_EN;
326
327 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
328
329 return 0;
330}
331
332static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
333{
334 u32 usbcfg;
335
336 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
337 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
338
339 switch (hsotg->hw_params.op_mode) {
340 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
John Younbea8e862016-11-03 17:55:53 -0700341 if (hsotg->params.otg_cap ==
John Younb02038fa2016-02-23 19:55:00 -0800342 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
343 usbcfg |= GUSBCFG_HNPCAP;
John Younbea8e862016-11-03 17:55:53 -0700344 if (hsotg->params.otg_cap !=
John Younb02038fa2016-02-23 19:55:00 -0800345 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
346 usbcfg |= GUSBCFG_SRPCAP;
347 break;
348
349 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
350 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
351 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
John Younbea8e862016-11-03 17:55:53 -0700352 if (hsotg->params.otg_cap !=
John Younb02038fa2016-02-23 19:55:00 -0800353 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
354 usbcfg |= GUSBCFG_SRPCAP;
355 break;
356
357 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
358 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
359 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
360 default:
361 break;
362 }
363
364 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
365}
366
367/**
368 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
369 *
370 * @hsotg: Programming view of DWC_otg controller
371 */
372static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
373{
374 u32 intmsk;
375
376 dev_dbg(hsotg->dev, "%s()\n", __func__);
377
378 /* Disable all interrupts */
379 dwc2_writel(0, hsotg->regs + GINTMSK);
380 dwc2_writel(0, hsotg->regs + HAINTMSK);
381
382 /* Enable the common interrupts */
383 dwc2_enable_common_interrupts(hsotg);
384
385 /* Enable host mode interrupts without disturbing common interrupts */
386 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
387 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
388 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
389}
390
391/**
392 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
393 *
394 * @hsotg: Programming view of DWC_otg controller
395 */
396static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
397{
398 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
399
400 /* Disable host mode interrupts without disturbing common interrupts */
401 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
402 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
403 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
404}
405
406/*
407 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
408 * For system that have a total fifo depth that is smaller than the default
409 * RX + TX fifo size.
410 *
411 * @hsotg: Programming view of DWC_otg controller
412 */
413static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
414{
John Younbea8e862016-11-03 17:55:53 -0700415 struct dwc2_core_params *params = &hsotg->params;
John Younb02038fa2016-02-23 19:55:00 -0800416 struct dwc2_hw_params *hw = &hsotg->hw_params;
417 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
418
419 total_fifo_size = hw->total_fifo_size;
420 rxfsiz = params->host_rx_fifo_size;
421 nptxfsiz = params->host_nperio_tx_fifo_size;
422 ptxfsiz = params->host_perio_tx_fifo_size;
423
424 /*
425 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
426 * allocation with support for high bandwidth endpoints. Synopsys
427 * defines MPS(Max Packet size) for a periodic EP=1024, and for
428 * non-periodic as 512.
429 */
430 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
431 /*
432 * For Buffer DMA mode/Scatter Gather DMA mode
433 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
434 * with n = number of host channel.
435 * 2 * ((1024/4) + 2) = 516
436 */
437 rxfsiz = 516 + hw->host_channels;
438
439 /*
440 * min non-periodic tx fifo depth
441 * 2 * (largest non-periodic USB packet used / 4)
442 * 2 * (512/4) = 256
443 */
444 nptxfsiz = 256;
445
446 /*
447 * min periodic tx fifo depth
448 * (largest packet size*MC)/4
449 * (1024 * 3)/4 = 768
450 */
451 ptxfsiz = 768;
452
453 params->host_rx_fifo_size = rxfsiz;
454 params->host_nperio_tx_fifo_size = nptxfsiz;
455 params->host_perio_tx_fifo_size = ptxfsiz;
456 }
457
458 /*
459 * If the summation of RX, NPTX and PTX fifo sizes is still
460 * bigger than the total_fifo_size, then we have a problem.
461 *
462 * We won't be able to allocate as many endpoints. Right now,
463 * we're just printing an error message, but ideally this FIFO
464 * allocation algorithm would be improved in the future.
465 *
466 * FIXME improve this FIFO allocation algorithm.
467 */
468 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
469 dev_err(hsotg->dev, "invalid fifo sizes\n");
470}
471
472static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
473{
John Younbea8e862016-11-03 17:55:53 -0700474 struct dwc2_core_params *params = &hsotg->params;
John Younb02038fa2016-02-23 19:55:00 -0800475 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
476
477 if (!params->enable_dynamic_fifo)
478 return;
479
480 dwc2_calculate_dynamic_fifo(hsotg);
481
482 /* Rx FIFO */
483 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
484 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
485 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
486 grxfsiz |= params->host_rx_fifo_size <<
487 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
488 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
489 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
490 dwc2_readl(hsotg->regs + GRXFSIZ));
491
492 /* Non-periodic Tx FIFO */
493 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
494 dwc2_readl(hsotg->regs + GNPTXFSIZ));
495 nptxfsiz = params->host_nperio_tx_fifo_size <<
496 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
497 nptxfsiz |= params->host_rx_fifo_size <<
498 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
499 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
500 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
501 dwc2_readl(hsotg->regs + GNPTXFSIZ));
502
503 /* Periodic Tx FIFO */
504 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
505 dwc2_readl(hsotg->regs + HPTXFSIZ));
506 hptxfsiz = params->host_perio_tx_fifo_size <<
507 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
508 hptxfsiz |= (params->host_rx_fifo_size +
509 params->host_nperio_tx_fifo_size) <<
510 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
511 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
512 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
513 dwc2_readl(hsotg->regs + HPTXFSIZ));
514
John Youn95832c02017-01-23 14:57:26 -0800515 if (hsotg->params.en_multiple_tx_fifo &&
Sevak Arakelyane1f411d2017-01-23 15:01:01 -0800516 hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
John Younb02038fa2016-02-23 19:55:00 -0800517 /*
Sevak Arakelyane1f411d2017-01-23 15:01:01 -0800518 * This feature was implemented in 2.91a version
John Younb02038fa2016-02-23 19:55:00 -0800519 * Global DFIFOCFG calculation for Host mode -
520 * include RxFIFO, NPTXFIFO and HPTXFIFO
521 */
522 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
523 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
524 dfifocfg |= (params->host_rx_fifo_size +
525 params->host_nperio_tx_fifo_size +
526 params->host_perio_tx_fifo_size) <<
527 GDFIFOCFG_EPINFOBASE_SHIFT &
528 GDFIFOCFG_EPINFOBASE_MASK;
529 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
530 }
531}
532
533/**
534 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
535 * the HFIR register according to PHY type and speed
536 *
537 * @hsotg: Programming view of DWC_otg controller
538 *
539 * NOTE: The caller can modify the value of the HFIR register only after the
540 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
541 * has been set
542 */
543u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
544{
545 u32 usbcfg;
546 u32 hprt0;
547 int clock = 60; /* default value */
548
549 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
550 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
551
552 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
553 !(usbcfg & GUSBCFG_PHYIF16))
554 clock = 60;
555 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
556 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
557 clock = 48;
558 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
559 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
560 clock = 30;
561 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
562 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
563 clock = 60;
564 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
565 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
566 clock = 48;
567 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
568 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
569 clock = 48;
570 if ((usbcfg & GUSBCFG_PHYSEL) &&
571 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
572 clock = 48;
573
574 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
575 /* High speed case */
576 return 125 * clock - 1;
577
578 /* FS/LS case */
579 return 1000 * clock - 1;
580}
581
582/**
583 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
584 * buffer
585 *
586 * @core_if: Programming view of DWC_otg controller
587 * @dest: Destination buffer for the packet
588 * @bytes: Number of bytes to copy to the destination
589 */
590void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
591{
592 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
593 u32 *data_buf = (u32 *)dest;
594 int word_count = (bytes + 3) / 4;
595 int i;
596
597 /*
598 * Todo: Account for the case where dest is not dword aligned. This
599 * requires reading data from the FIFO into a u32 temp buffer, then
600 * moving it into the data buffer.
601 */
602
603 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
604
605 for (i = 0; i < word_count; i++, data_buf++)
606 *data_buf = dwc2_readl(fifo);
607}
608
Paul Zimmerman7359d482013-03-11 17:47:59 -0700609/**
610 * dwc2_dump_channel_info() - Prints the state of a host channel
611 *
612 * @hsotg: Programming view of DWC_otg controller
613 * @chan: Pointer to the channel to dump
614 *
615 * Must be called with interrupt disabled and spinlock held
616 *
617 * NOTE: This function will be removed once the peripheral controller code
618 * is integrated and the driver is stable
619 */
620static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
621 struct dwc2_host_chan *chan)
622{
623#ifdef VERBOSE_DEBUG
John Younbea8e862016-11-03 17:55:53 -0700624 int num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700625 struct dwc2_qh *qh;
626 u32 hcchar;
627 u32 hcsplt;
628 u32 hctsiz;
629 u32 hc_dma;
630 int i;
631
John Younb02038fa2016-02-23 19:55:00 -0800632 if (!chan)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700633 return;
634
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300635 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
636 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
637 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chan->hc_num));
638 hc_dma = dwc2_readl(hsotg->regs + HCDMA(chan->hc_num));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700639
640 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
641 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
642 hcchar, hcsplt);
643 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
644 hctsiz, hc_dma);
645 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
646 chan->dev_addr, chan->ep_num, chan->ep_is_in);
647 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
648 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
649 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
650 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
651 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
652 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
653 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
654 (unsigned long)chan->xfer_dma);
655 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
656 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
657 dev_dbg(hsotg->dev, " NP inactive sched:\n");
658 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
659 qh_list_entry)
660 dev_dbg(hsotg->dev, " %p\n", qh);
661 dev_dbg(hsotg->dev, " NP active sched:\n");
662 list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
663 qh_list_entry)
664 dev_dbg(hsotg->dev, " %p\n", qh);
665 dev_dbg(hsotg->dev, " Channels:\n");
666 for (i = 0; i < num_channels; i++) {
667 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
668
669 dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
670 }
671#endif /* VERBOSE_DEBUG */
672}
673
Razmik Karapetyan4411beb2016-11-16 15:34:04 -0800674static int _dwc2_hcd_start(struct usb_hcd *hcd);
675
676static void dwc2_host_start(struct dwc2_hsotg *hsotg)
677{
678 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
679
680 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
681 _dwc2_hcd_start(hcd);
682}
683
684static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
685{
686 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
687
688 hcd->self.is_b_host = 0;
689}
690
691static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
692 int *hub_addr, int *hub_port)
693{
694 struct urb *urb = context;
695
696 if (urb->dev->tt)
697 *hub_addr = urb->dev->tt->hub->devnum;
698 else
699 *hub_addr = 0;
700 *hub_port = urb->dev->ttport;
701}
702
Paul Zimmerman7359d482013-03-11 17:47:59 -0700703/*
John Younb02038fa2016-02-23 19:55:00 -0800704 * =========================================================================
705 * Low Level Host Channel Access Functions
706 * =========================================================================
707 */
708
709static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
710 struct dwc2_host_chan *chan)
711{
712 u32 hcintmsk = HCINTMSK_CHHLTD;
713
714 switch (chan->ep_type) {
715 case USB_ENDPOINT_XFER_CONTROL:
716 case USB_ENDPOINT_XFER_BULK:
717 dev_vdbg(hsotg->dev, "control/bulk\n");
718 hcintmsk |= HCINTMSK_XFERCOMPL;
719 hcintmsk |= HCINTMSK_STALL;
720 hcintmsk |= HCINTMSK_XACTERR;
721 hcintmsk |= HCINTMSK_DATATGLERR;
722 if (chan->ep_is_in) {
723 hcintmsk |= HCINTMSK_BBLERR;
724 } else {
725 hcintmsk |= HCINTMSK_NAK;
726 hcintmsk |= HCINTMSK_NYET;
727 if (chan->do_ping)
728 hcintmsk |= HCINTMSK_ACK;
729 }
730
731 if (chan->do_split) {
732 hcintmsk |= HCINTMSK_NAK;
733 if (chan->complete_split)
734 hcintmsk |= HCINTMSK_NYET;
735 else
736 hcintmsk |= HCINTMSK_ACK;
737 }
738
739 if (chan->error_state)
740 hcintmsk |= HCINTMSK_ACK;
741 break;
742
743 case USB_ENDPOINT_XFER_INT:
744 if (dbg_perio())
745 dev_vdbg(hsotg->dev, "intr\n");
746 hcintmsk |= HCINTMSK_XFERCOMPL;
747 hcintmsk |= HCINTMSK_NAK;
748 hcintmsk |= HCINTMSK_STALL;
749 hcintmsk |= HCINTMSK_XACTERR;
750 hcintmsk |= HCINTMSK_DATATGLERR;
751 hcintmsk |= HCINTMSK_FRMOVRUN;
752
753 if (chan->ep_is_in)
754 hcintmsk |= HCINTMSK_BBLERR;
755 if (chan->error_state)
756 hcintmsk |= HCINTMSK_ACK;
757 if (chan->do_split) {
758 if (chan->complete_split)
759 hcintmsk |= HCINTMSK_NYET;
760 else
761 hcintmsk |= HCINTMSK_ACK;
762 }
763 break;
764
765 case USB_ENDPOINT_XFER_ISOC:
766 if (dbg_perio())
767 dev_vdbg(hsotg->dev, "isoc\n");
768 hcintmsk |= HCINTMSK_XFERCOMPL;
769 hcintmsk |= HCINTMSK_FRMOVRUN;
770 hcintmsk |= HCINTMSK_ACK;
771
772 if (chan->ep_is_in) {
773 hcintmsk |= HCINTMSK_XACTERR;
774 hcintmsk |= HCINTMSK_BBLERR;
775 }
776 break;
777 default:
778 dev_err(hsotg->dev, "## Unknown EP type ##\n");
779 break;
780 }
781
782 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
783 if (dbg_hc(chan))
784 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
785}
786
787static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
788 struct dwc2_host_chan *chan)
789{
790 u32 hcintmsk = HCINTMSK_CHHLTD;
791
792 /*
793 * For Descriptor DMA mode core halts the channel on AHB error.
794 * Interrupt is not required.
795 */
John Youn95832c02017-01-23 14:57:26 -0800796 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -0800797 if (dbg_hc(chan))
798 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
799 hcintmsk |= HCINTMSK_AHBERR;
800 } else {
801 if (dbg_hc(chan))
802 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
803 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
804 hcintmsk |= HCINTMSK_XFERCOMPL;
805 }
806
807 if (chan->error_state && !chan->do_split &&
808 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
809 if (dbg_hc(chan))
810 dev_vdbg(hsotg->dev, "setting ACK\n");
811 hcintmsk |= HCINTMSK_ACK;
812 if (chan->ep_is_in) {
813 hcintmsk |= HCINTMSK_DATATGLERR;
814 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
815 hcintmsk |= HCINTMSK_NAK;
816 }
817 }
818
819 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
820 if (dbg_hc(chan))
821 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
822}
823
824static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
825 struct dwc2_host_chan *chan)
826{
827 u32 intmsk;
828
John Youn95832c02017-01-23 14:57:26 -0800829 if (hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -0800830 if (dbg_hc(chan))
831 dev_vdbg(hsotg->dev, "DMA enabled\n");
832 dwc2_hc_enable_dma_ints(hsotg, chan);
833 } else {
834 if (dbg_hc(chan))
835 dev_vdbg(hsotg->dev, "DMA disabled\n");
836 dwc2_hc_enable_slave_ints(hsotg, chan);
837 }
838
839 /* Enable the top level host channel interrupt */
840 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
841 intmsk |= 1 << chan->hc_num;
842 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
843 if (dbg_hc(chan))
844 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
845
846 /* Make sure host channel interrupts are enabled */
847 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
848 intmsk |= GINTSTS_HCHINT;
849 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
850 if (dbg_hc(chan))
851 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
852}
853
854/**
855 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
856 * a specific endpoint
857 *
858 * @hsotg: Programming view of DWC_otg controller
859 * @chan: Information needed to initialize the host channel
860 *
861 * The HCCHARn register is set up with the characteristics specified in chan.
862 * Host channel interrupts that may need to be serviced while this transfer is
863 * in progress are enabled.
864 */
865static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
866{
867 u8 hc_num = chan->hc_num;
868 u32 hcintmsk;
869 u32 hcchar;
870 u32 hcsplt = 0;
871
872 if (dbg_hc(chan))
873 dev_vdbg(hsotg->dev, "%s()\n", __func__);
874
875 /* Clear old interrupt conditions for this host channel */
876 hcintmsk = 0xffffffff;
877 hcintmsk &= ~HCINTMSK_RESERVED14_31;
878 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
879
880 /* Enable channel interrupts required for this transfer */
881 dwc2_hc_enable_ints(hsotg, chan);
882
883 /*
884 * Program the HCCHARn register with the endpoint characteristics for
885 * the current transfer
886 */
887 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
888 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
889 if (chan->ep_is_in)
890 hcchar |= HCCHAR_EPDIR;
891 if (chan->speed == USB_SPEED_LOW)
892 hcchar |= HCCHAR_LSPDDEV;
893 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
894 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
895 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
896 if (dbg_hc(chan)) {
897 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
898 hc_num, hcchar);
899
900 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
901 __func__, hc_num);
902 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
903 chan->dev_addr);
904 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
905 chan->ep_num);
906 dev_vdbg(hsotg->dev, " Is In: %d\n",
907 chan->ep_is_in);
908 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
909 chan->speed == USB_SPEED_LOW);
910 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
911 chan->ep_type);
912 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
913 chan->max_packet);
914 }
915
916 /* Program the HCSPLT register for SPLITs */
917 if (chan->do_split) {
918 if (dbg_hc(chan))
919 dev_vdbg(hsotg->dev,
920 "Programming HC %d with split --> %s\n",
921 hc_num,
922 chan->complete_split ? "CSPLIT" : "SSPLIT");
923 if (chan->complete_split)
924 hcsplt |= HCSPLT_COMPSPLT;
925 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
926 HCSPLT_XACTPOS_MASK;
927 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
928 HCSPLT_HUBADDR_MASK;
929 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
930 HCSPLT_PRTADDR_MASK;
931 if (dbg_hc(chan)) {
932 dev_vdbg(hsotg->dev, " comp split %d\n",
933 chan->complete_split);
934 dev_vdbg(hsotg->dev, " xact pos %d\n",
935 chan->xact_pos);
936 dev_vdbg(hsotg->dev, " hub addr %d\n",
937 chan->hub_addr);
938 dev_vdbg(hsotg->dev, " hub port %d\n",
939 chan->hub_port);
940 dev_vdbg(hsotg->dev, " is_in %d\n",
941 chan->ep_is_in);
942 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
943 chan->max_packet);
944 dev_vdbg(hsotg->dev, " xferlen %d\n",
945 chan->xfer_len);
946 }
947 }
948
949 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
950}
951
952/**
953 * dwc2_hc_halt() - Attempts to halt a host channel
954 *
955 * @hsotg: Controller register interface
956 * @chan: Host channel to halt
957 * @halt_status: Reason for halting the channel
958 *
959 * This function should only be called in Slave mode or to abort a transfer in
960 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
961 * controller halts the channel when the transfer is complete or a condition
962 * occurs that requires application intervention.
963 *
964 * In slave mode, checks for a free request queue entry, then sets the Channel
965 * Enable and Channel Disable bits of the Host Channel Characteristics
966 * register of the specified channel to intiate the halt. If there is no free
967 * request queue entry, sets only the Channel Disable bit of the HCCHARn
968 * register to flush requests for this channel. In the latter case, sets a
969 * flag to indicate that the host channel needs to be halted when a request
970 * queue slot is open.
971 *
972 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
973 * HCCHARn register. The controller ensures there is space in the request
974 * queue before submitting the halt request.
975 *
976 * Some time may elapse before the core flushes any posted requests for this
977 * host channel and halts. The Channel Halted interrupt handler completes the
978 * deactivation of the host channel.
979 */
980void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
981 enum dwc2_halt_status halt_status)
982{
983 u32 nptxsts, hptxsts, hcchar;
984
985 if (dbg_hc(chan))
986 dev_vdbg(hsotg->dev, "%s()\n", __func__);
987 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
988 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
989
990 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
991 halt_status == DWC2_HC_XFER_AHB_ERR) {
992 /*
993 * Disable all channel interrupts except Ch Halted. The QTD
994 * and QH state associated with this transfer has been cleared
995 * (in the case of URB_DEQUEUE), so the channel needs to be
996 * shut down carefully to prevent crashes.
997 */
998 u32 hcintmsk = HCINTMSK_CHHLTD;
999
1000 dev_vdbg(hsotg->dev, "dequeue/error\n");
1001 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1002
1003 /*
1004 * Make sure no other interrupts besides halt are currently
1005 * pending. Handling another interrupt could cause a crash due
1006 * to the QTD and QH state.
1007 */
1008 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1009
1010 /*
1011 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1012 * even if the channel was already halted for some other
1013 * reason
1014 */
1015 chan->halt_status = halt_status;
1016
1017 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1018 if (!(hcchar & HCCHAR_CHENA)) {
1019 /*
1020 * The channel is either already halted or it hasn't
1021 * started yet. In DMA mode, the transfer may halt if
1022 * it finishes normally or a condition occurs that
1023 * requires driver intervention. Don't want to halt
1024 * the channel again. In either Slave or DMA mode,
1025 * it's possible that the transfer has been assigned
1026 * to a channel, but not started yet when an URB is
1027 * dequeued. Don't want to halt a channel that hasn't
1028 * started yet.
1029 */
1030 return;
1031 }
1032 }
1033 if (chan->halt_pending) {
1034 /*
1035 * A halt has already been issued for this channel. This might
1036 * happen when a transfer is aborted by a higher level in
1037 * the stack.
1038 */
1039 dev_vdbg(hsotg->dev,
1040 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1041 __func__, chan->hc_num);
1042 return;
1043 }
1044
1045 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1046
1047 /* No need to set the bit in DDMA for disabling the channel */
1048 /* TODO check it everywhere channel is disabled */
John Youn95832c02017-01-23 14:57:26 -08001049 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08001050 if (dbg_hc(chan))
1051 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1052 hcchar |= HCCHAR_CHENA;
1053 } else {
1054 if (dbg_hc(chan))
1055 dev_dbg(hsotg->dev, "desc DMA enabled\n");
1056 }
1057 hcchar |= HCCHAR_CHDIS;
1058
John Youn95832c02017-01-23 14:57:26 -08001059 if (!hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001060 if (dbg_hc(chan))
1061 dev_vdbg(hsotg->dev, "DMA not enabled\n");
1062 hcchar |= HCCHAR_CHENA;
1063
1064 /* Check for space in the request queue to issue the halt */
1065 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1066 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1067 dev_vdbg(hsotg->dev, "control/bulk\n");
1068 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1069 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1070 dev_vdbg(hsotg->dev, "Disabling channel\n");
1071 hcchar &= ~HCCHAR_CHENA;
1072 }
1073 } else {
1074 if (dbg_perio())
1075 dev_vdbg(hsotg->dev, "isoc/intr\n");
1076 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1077 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1078 hsotg->queuing_high_bandwidth) {
1079 if (dbg_perio())
1080 dev_vdbg(hsotg->dev, "Disabling channel\n");
1081 hcchar &= ~HCCHAR_CHENA;
1082 }
1083 }
1084 } else {
1085 if (dbg_hc(chan))
1086 dev_vdbg(hsotg->dev, "DMA enabled\n");
1087 }
1088
1089 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1090 chan->halt_status = halt_status;
1091
1092 if (hcchar & HCCHAR_CHENA) {
1093 if (dbg_hc(chan))
1094 dev_vdbg(hsotg->dev, "Channel enabled\n");
1095 chan->halt_pending = 1;
1096 chan->halt_on_queue = 0;
1097 } else {
1098 if (dbg_hc(chan))
1099 dev_vdbg(hsotg->dev, "Channel disabled\n");
1100 chan->halt_on_queue = 1;
1101 }
1102
1103 if (dbg_hc(chan)) {
1104 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1105 chan->hc_num);
1106 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1107 hcchar);
1108 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1109 chan->halt_pending);
1110 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1111 chan->halt_on_queue);
1112 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1113 chan->halt_status);
1114 }
1115}
1116
1117/**
1118 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1119 *
1120 * @hsotg: Programming view of DWC_otg controller
1121 * @chan: Identifies the host channel to clean up
1122 *
1123 * This function is normally called after a transfer is done and the host
1124 * channel is being released
1125 */
1126void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1127{
1128 u32 hcintmsk;
1129
1130 chan->xfer_started = 0;
1131
1132 list_del_init(&chan->split_order_list_entry);
1133
1134 /*
1135 * Clear channel interrupt enables and any unhandled channel interrupt
1136 * conditions
1137 */
1138 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1139 hcintmsk = 0xffffffff;
1140 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1141 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1142}
1143
1144/**
1145 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1146 * which frame a periodic transfer should occur
1147 *
1148 * @hsotg: Programming view of DWC_otg controller
1149 * @chan: Identifies the host channel to set up and its properties
1150 * @hcchar: Current value of the HCCHAR register for the specified host channel
1151 *
1152 * This function has no effect on non-periodic transfers
1153 */
1154static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1155 struct dwc2_host_chan *chan, u32 *hcchar)
1156{
1157 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1158 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1159 int host_speed;
1160 int xfer_ns;
1161 int xfer_us;
1162 int bytes_in_fifo;
1163 u16 fifo_space;
1164 u16 frame_number;
1165 u16 wire_frame;
1166
1167 /*
1168 * Try to figure out if we're an even or odd frame. If we set
1169 * even and the current frame number is even the the transfer
1170 * will happen immediately. Similar if both are odd. If one is
1171 * even and the other is odd then the transfer will happen when
1172 * the frame number ticks.
1173 *
1174 * There's a bit of a balancing act to get this right.
1175 * Sometimes we may want to send data in the current frame (AK
1176 * right away). We might want to do this if the frame number
1177 * _just_ ticked, but we might also want to do this in order
1178 * to continue a split transaction that happened late in a
1179 * microframe (so we didn't know to queue the next transfer
1180 * until the frame number had ticked). The problem is that we
1181 * need a lot of knowledge to know if there's actually still
1182 * time to send things or if it would be better to wait until
1183 * the next frame.
1184 *
1185 * We can look at how much time is left in the current frame
1186 * and make a guess about whether we'll have time to transfer.
1187 * We'll do that.
1188 */
1189
1190 /* Get speed host is running at */
1191 host_speed = (chan->speed != USB_SPEED_HIGH &&
1192 !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1193
1194 /* See how many bytes are in the periodic FIFO right now */
1195 fifo_space = (dwc2_readl(hsotg->regs + HPTXSTS) &
1196 TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1197 bytes_in_fifo = sizeof(u32) *
John Younbea8e862016-11-03 17:55:53 -07001198 (hsotg->params.host_perio_tx_fifo_size -
John Younb02038fa2016-02-23 19:55:00 -08001199 fifo_space);
1200
1201 /*
1202 * Roughly estimate bus time for everything in the periodic
1203 * queue + our new transfer. This is "rough" because we're
1204 * using a function that makes takes into account IN/OUT
1205 * and INT/ISO and we're just slamming in one value for all
1206 * transfers. This should be an over-estimate and that should
1207 * be OK, but we can probably tighten it.
1208 */
1209 xfer_ns = usb_calc_bus_time(host_speed, false, false,
1210 chan->xfer_len + bytes_in_fifo);
1211 xfer_us = NS_TO_US(xfer_ns);
1212
1213 /* See what frame number we'll be at by the time we finish */
1214 frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1215
1216 /* This is when we were scheduled to be on the wire */
1217 wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1218
1219 /*
1220 * If we'd finish _after_ the frame we're scheduled in then
1221 * it's hopeless. Just schedule right away and hope for the
1222 * best. Note that it _might_ be wise to call back into the
1223 * scheduler to pick a better frame, but this is better than
1224 * nothing.
1225 */
1226 if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1227 dwc2_sch_vdbg(hsotg,
1228 "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1229 chan->qh, wire_frame, frame_number,
1230 dwc2_frame_num_dec(frame_number,
1231 wire_frame));
1232 wire_frame = frame_number;
1233
1234 /*
1235 * We picked a different frame number; communicate this
1236 * back to the scheduler so it doesn't try to schedule
1237 * another in the same frame.
1238 *
1239 * Remember that next_active_frame is 1 before the wire
1240 * frame.
1241 */
1242 chan->qh->next_active_frame =
1243 dwc2_frame_num_dec(frame_number, 1);
1244 }
1245
1246 if (wire_frame & 1)
1247 *hcchar |= HCCHAR_ODDFRM;
1248 else
1249 *hcchar &= ~HCCHAR_ODDFRM;
1250 }
1251}
1252
1253static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1254{
1255 /* Set up the initial PID for the transfer */
1256 if (chan->speed == USB_SPEED_HIGH) {
1257 if (chan->ep_is_in) {
1258 if (chan->multi_count == 1)
1259 chan->data_pid_start = DWC2_HC_PID_DATA0;
1260 else if (chan->multi_count == 2)
1261 chan->data_pid_start = DWC2_HC_PID_DATA1;
1262 else
1263 chan->data_pid_start = DWC2_HC_PID_DATA2;
1264 } else {
1265 if (chan->multi_count == 1)
1266 chan->data_pid_start = DWC2_HC_PID_DATA0;
1267 else
1268 chan->data_pid_start = DWC2_HC_PID_MDATA;
1269 }
1270 } else {
1271 chan->data_pid_start = DWC2_HC_PID_DATA0;
1272 }
1273}
1274
1275/**
1276 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1277 * the Host Channel
1278 *
1279 * @hsotg: Programming view of DWC_otg controller
1280 * @chan: Information needed to initialize the host channel
1281 *
1282 * This function should only be called in Slave mode. For a channel associated
1283 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1284 * associated with a periodic EP, the periodic Tx FIFO is written.
1285 *
1286 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1287 * the number of bytes written to the Tx FIFO.
1288 */
1289static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1290 struct dwc2_host_chan *chan)
1291{
1292 u32 i;
1293 u32 remaining_count;
1294 u32 byte_count;
1295 u32 dword_count;
1296 u32 __iomem *data_fifo;
1297 u32 *data_buf = (u32 *)chan->xfer_buf;
1298
1299 if (dbg_hc(chan))
1300 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1301
1302 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1303
1304 remaining_count = chan->xfer_len - chan->xfer_count;
1305 if (remaining_count > chan->max_packet)
1306 byte_count = chan->max_packet;
1307 else
1308 byte_count = remaining_count;
1309
1310 dword_count = (byte_count + 3) / 4;
1311
1312 if (((unsigned long)data_buf & 0x3) == 0) {
1313 /* xfer_buf is DWORD aligned */
1314 for (i = 0; i < dword_count; i++, data_buf++)
1315 dwc2_writel(*data_buf, data_fifo);
1316 } else {
1317 /* xfer_buf is not DWORD aligned */
1318 for (i = 0; i < dword_count; i++, data_buf++) {
1319 u32 data = data_buf[0] | data_buf[1] << 8 |
1320 data_buf[2] << 16 | data_buf[3] << 24;
1321 dwc2_writel(data, data_fifo);
1322 }
1323 }
1324
1325 chan->xfer_count += byte_count;
1326 chan->xfer_buf += byte_count;
1327}
1328
1329/**
1330 * dwc2_hc_do_ping() - Starts a PING transfer
1331 *
1332 * @hsotg: Programming view of DWC_otg controller
1333 * @chan: Information needed to initialize the host channel
1334 *
1335 * This function should only be called in Slave mode. The Do Ping bit is set in
1336 * the HCTSIZ register, then the channel is enabled.
1337 */
1338static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1339 struct dwc2_host_chan *chan)
1340{
1341 u32 hcchar;
1342 u32 hctsiz;
1343
1344 if (dbg_hc(chan))
1345 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1346 chan->hc_num);
1347
1348 hctsiz = TSIZ_DOPNG;
1349 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1350 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1351
1352 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1353 hcchar |= HCCHAR_CHENA;
1354 hcchar &= ~HCCHAR_CHDIS;
1355 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1356}
1357
1358/**
1359 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1360 * channel and starts the transfer
1361 *
1362 * @hsotg: Programming view of DWC_otg controller
1363 * @chan: Information needed to initialize the host channel. The xfer_len value
1364 * may be reduced to accommodate the max widths of the XferSize and
1365 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1366 * changed to reflect the final xfer_len value.
1367 *
1368 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1369 * the caller must ensure that there is sufficient space in the request queue
1370 * and Tx Data FIFO.
1371 *
1372 * For an OUT transfer in Slave mode, it loads a data packet into the
1373 * appropriate FIFO. If necessary, additional data packets are loaded in the
1374 * Host ISR.
1375 *
1376 * For an IN transfer in Slave mode, a data packet is requested. The data
1377 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1378 * additional data packets are requested in the Host ISR.
1379 *
1380 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1381 * register along with a packet count of 1 and the channel is enabled. This
1382 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1383 * simply set to 0 since no data transfer occurs in this case.
1384 *
1385 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1386 * all the information required to perform the subsequent data transfer. In
1387 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1388 * controller performs the entire PING protocol, then starts the data
1389 * transfer.
1390 */
1391static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1392 struct dwc2_host_chan *chan)
1393{
John Younbea8e862016-11-03 17:55:53 -07001394 u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1395 u16 max_hc_pkt_count = hsotg->params.max_packet_count;
John Younb02038fa2016-02-23 19:55:00 -08001396 u32 hcchar;
1397 u32 hctsiz = 0;
1398 u16 num_packets;
1399 u32 ec_mc;
1400
1401 if (dbg_hc(chan))
1402 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1403
1404 if (chan->do_ping) {
John Youn95832c02017-01-23 14:57:26 -08001405 if (!hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001406 if (dbg_hc(chan))
1407 dev_vdbg(hsotg->dev, "ping, no DMA\n");
1408 dwc2_hc_do_ping(hsotg, chan);
1409 chan->xfer_started = 1;
1410 return;
1411 }
1412
1413 if (dbg_hc(chan))
1414 dev_vdbg(hsotg->dev, "ping, DMA\n");
1415
1416 hctsiz |= TSIZ_DOPNG;
1417 }
1418
1419 if (chan->do_split) {
1420 if (dbg_hc(chan))
1421 dev_vdbg(hsotg->dev, "split\n");
1422 num_packets = 1;
1423
1424 if (chan->complete_split && !chan->ep_is_in)
1425 /*
1426 * For CSPLIT OUT Transfer, set the size to 0 so the
1427 * core doesn't expect any data written to the FIFO
1428 */
1429 chan->xfer_len = 0;
1430 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1431 chan->xfer_len = chan->max_packet;
1432 else if (!chan->ep_is_in && chan->xfer_len > 188)
1433 chan->xfer_len = 188;
1434
1435 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1436 TSIZ_XFERSIZE_MASK;
1437
1438 /* For split set ec_mc for immediate retries */
1439 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1440 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1441 ec_mc = 3;
1442 else
1443 ec_mc = 1;
1444 } else {
1445 if (dbg_hc(chan))
1446 dev_vdbg(hsotg->dev, "no split\n");
1447 /*
1448 * Ensure that the transfer length and packet count will fit
1449 * in the widths allocated for them in the HCTSIZn register
1450 */
1451 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1452 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1453 /*
1454 * Make sure the transfer size is no larger than one
1455 * (micro)frame's worth of data. (A check was done
1456 * when the periodic transfer was accepted to ensure
1457 * that a (micro)frame's worth of data can be
1458 * programmed into a channel.)
1459 */
1460 u32 max_periodic_len =
1461 chan->multi_count * chan->max_packet;
1462
1463 if (chan->xfer_len > max_periodic_len)
1464 chan->xfer_len = max_periodic_len;
1465 } else if (chan->xfer_len > max_hc_xfer_size) {
1466 /*
1467 * Make sure that xfer_len is a multiple of max packet
1468 * size
1469 */
1470 chan->xfer_len =
1471 max_hc_xfer_size - chan->max_packet + 1;
1472 }
1473
1474 if (chan->xfer_len > 0) {
1475 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1476 chan->max_packet;
1477 if (num_packets > max_hc_pkt_count) {
1478 num_packets = max_hc_pkt_count;
1479 chan->xfer_len = num_packets * chan->max_packet;
1480 }
1481 } else {
1482 /* Need 1 packet for transfer length of 0 */
1483 num_packets = 1;
1484 }
1485
1486 if (chan->ep_is_in)
1487 /*
1488 * Always program an integral # of max packets for IN
1489 * transfers
1490 */
1491 chan->xfer_len = num_packets * chan->max_packet;
1492
1493 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1494 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1495 /*
1496 * Make sure that the multi_count field matches the
1497 * actual transfer length
1498 */
1499 chan->multi_count = num_packets;
1500
1501 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1502 dwc2_set_pid_isoc(chan);
1503
1504 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1505 TSIZ_XFERSIZE_MASK;
1506
1507 /* The ec_mc gets the multi_count for non-split */
1508 ec_mc = chan->multi_count;
1509 }
1510
1511 chan->start_pkt_count = num_packets;
1512 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1513 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1514 TSIZ_SC_MC_PID_MASK;
1515 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1516 if (dbg_hc(chan)) {
1517 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1518 hctsiz, chan->hc_num);
1519
1520 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1521 chan->hc_num);
1522 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1523 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1524 TSIZ_XFERSIZE_SHIFT);
1525 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1526 (hctsiz & TSIZ_PKTCNT_MASK) >>
1527 TSIZ_PKTCNT_SHIFT);
1528 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1529 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1530 TSIZ_SC_MC_PID_SHIFT);
1531 }
1532
John Youn95832c02017-01-23 14:57:26 -08001533 if (hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001534 dwc2_writel((u32)chan->xfer_dma,
1535 hsotg->regs + HCDMA(chan->hc_num));
1536 if (dbg_hc(chan))
1537 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1538 (unsigned long)chan->xfer_dma, chan->hc_num);
1539 }
1540
1541 /* Start the split */
1542 if (chan->do_split) {
1543 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1544
1545 hcsplt |= HCSPLT_SPLTENA;
1546 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1547 }
1548
1549 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1550 hcchar &= ~HCCHAR_MULTICNT_MASK;
1551 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1552 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1553
1554 if (hcchar & HCCHAR_CHDIS)
1555 dev_warn(hsotg->dev,
1556 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1557 __func__, chan->hc_num, hcchar);
1558
1559 /* Set host channel enable after all other setup is complete */
1560 hcchar |= HCCHAR_CHENA;
1561 hcchar &= ~HCCHAR_CHDIS;
1562
1563 if (dbg_hc(chan))
1564 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1565 (hcchar & HCCHAR_MULTICNT_MASK) >>
1566 HCCHAR_MULTICNT_SHIFT);
1567
1568 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1569 if (dbg_hc(chan))
1570 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1571 chan->hc_num);
1572
1573 chan->xfer_started = 1;
1574 chan->requests++;
1575
John Youn95832c02017-01-23 14:57:26 -08001576 if (!hsotg->params.host_dma &&
John Younb02038fa2016-02-23 19:55:00 -08001577 !chan->ep_is_in && chan->xfer_len > 0)
1578 /* Load OUT packet into the appropriate Tx FIFO */
1579 dwc2_hc_write_packet(hsotg, chan);
1580}
1581
1582/**
1583 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1584 * host channel and starts the transfer in Descriptor DMA mode
1585 *
1586 * @hsotg: Programming view of DWC_otg controller
1587 * @chan: Information needed to initialize the host channel
1588 *
1589 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1590 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1591 * with micro-frame bitmap.
1592 *
1593 * Initializes HCDMA register with descriptor list address and CTD value then
1594 * starts the transfer via enabling the channel.
1595 */
1596void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1597 struct dwc2_host_chan *chan)
1598{
1599 u32 hcchar;
1600 u32 hctsiz = 0;
1601
1602 if (chan->do_ping)
1603 hctsiz |= TSIZ_DOPNG;
1604
1605 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1606 dwc2_set_pid_isoc(chan);
1607
1608 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1609 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1610 TSIZ_SC_MC_PID_MASK;
1611
1612 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1613 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1614
1615 /* Non-zero only for high-speed interrupt endpoints */
1616 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1617
1618 if (dbg_hc(chan)) {
1619 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1620 chan->hc_num);
1621 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1622 chan->data_pid_start);
1623 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1624 }
1625
1626 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1627
1628 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1629 chan->desc_list_sz, DMA_TO_DEVICE);
1630
1631 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
1632
1633 if (dbg_hc(chan))
1634 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1635 &chan->desc_list_addr, chan->hc_num);
1636
1637 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1638 hcchar &= ~HCCHAR_MULTICNT_MASK;
1639 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1640 HCCHAR_MULTICNT_MASK;
1641
1642 if (hcchar & HCCHAR_CHDIS)
1643 dev_warn(hsotg->dev,
1644 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1645 __func__, chan->hc_num, hcchar);
1646
1647 /* Set host channel enable after all other setup is complete */
1648 hcchar |= HCCHAR_CHENA;
1649 hcchar &= ~HCCHAR_CHDIS;
1650
1651 if (dbg_hc(chan))
1652 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1653 (hcchar & HCCHAR_MULTICNT_MASK) >>
1654 HCCHAR_MULTICNT_SHIFT);
1655
1656 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1657 if (dbg_hc(chan))
1658 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1659 chan->hc_num);
1660
1661 chan->xfer_started = 1;
1662 chan->requests++;
1663}
1664
1665/**
1666 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1667 * a previous call to dwc2_hc_start_transfer()
1668 *
1669 * @hsotg: Programming view of DWC_otg controller
1670 * @chan: Information needed to initialize the host channel
1671 *
1672 * The caller must ensure there is sufficient space in the request queue and Tx
1673 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1674 * the controller acts autonomously to complete transfers programmed to a host
1675 * channel.
1676 *
1677 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1678 * if there is any data remaining to be queued. For an IN transfer, another
1679 * data packet is always requested. For the SETUP phase of a control transfer,
1680 * this function does nothing.
1681 *
1682 * Return: 1 if a new request is queued, 0 if no more requests are required
1683 * for this transfer
1684 */
1685static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1686 struct dwc2_host_chan *chan)
1687{
1688 if (dbg_hc(chan))
1689 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1690 chan->hc_num);
1691
1692 if (chan->do_split)
1693 /* SPLITs always queue just once per channel */
1694 return 0;
1695
1696 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1697 /* SETUPs are queued only once since they can't be NAK'd */
1698 return 0;
1699
1700 if (chan->ep_is_in) {
1701 /*
1702 * Always queue another request for other IN transfers. If
1703 * back-to-back INs are issued and NAKs are received for both,
1704 * the driver may still be processing the first NAK when the
1705 * second NAK is received. When the interrupt handler clears
1706 * the NAK interrupt for the first NAK, the second NAK will
1707 * not be seen. So we can't depend on the NAK interrupt
1708 * handler to requeue a NAK'd request. Instead, IN requests
1709 * are issued each time this function is called. When the
1710 * transfer completes, the extra requests for the channel will
1711 * be flushed.
1712 */
1713 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1714
1715 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1716 hcchar |= HCCHAR_CHENA;
1717 hcchar &= ~HCCHAR_CHDIS;
1718 if (dbg_hc(chan))
1719 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
1720 hcchar);
1721 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1722 chan->requests++;
1723 return 1;
1724 }
1725
1726 /* OUT transfers */
1727
1728 if (chan->xfer_count < chan->xfer_len) {
1729 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1730 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1731 u32 hcchar = dwc2_readl(hsotg->regs +
1732 HCCHAR(chan->hc_num));
1733
1734 dwc2_hc_set_even_odd_frame(hsotg, chan,
1735 &hcchar);
1736 }
1737
1738 /* Load OUT packet into the appropriate Tx FIFO */
1739 dwc2_hc_write_packet(hsotg, chan);
1740 chan->requests++;
1741 return 1;
1742 }
1743
1744 return 0;
1745}
1746
1747/*
1748 * =========================================================================
1749 * HCD
1750 * =========================================================================
1751 */
1752
1753/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07001754 * Processes all the URBs in a single list of QHs. Completes them with
1755 * -ETIMEDOUT and frees the QTD.
1756 *
1757 * Must be called with interrupt disabled and spinlock held
1758 */
1759static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1760 struct list_head *qh_list)
1761{
1762 struct dwc2_qh *qh, *qh_tmp;
1763 struct dwc2_qtd *qtd, *qtd_tmp;
1764
1765 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1766 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1767 qtd_list_entry) {
Gregory Herrero2e84da62015-09-22 15:16:53 +02001768 dwc2_host_complete(hsotg, qtd, -ECONNRESET);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001769 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001770 }
1771 }
1772}
1773
1774static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1775 struct list_head *qh_list)
1776{
1777 struct dwc2_qtd *qtd, *qtd_tmp;
1778 struct dwc2_qh *qh, *qh_tmp;
1779 unsigned long flags;
1780
1781 if (!qh_list->next)
1782 /* The list hasn't been initialized yet */
1783 return;
1784
1785 spin_lock_irqsave(&hsotg->lock, flags);
1786
1787 /* Ensure there are no QTDs or URBs left */
1788 dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1789
1790 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1791 dwc2_hcd_qh_unlink(hsotg, qh);
1792
1793 /* Free each QTD in the QH's QTD list */
1794 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1795 qtd_list_entry)
1796 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1797
Douglas Anderson16e80212016-01-28 18:19:55 -08001798 if (qh->channel && qh->channel->qh == qh)
1799 qh->channel->qh = NULL;
1800
Paul Zimmerman7359d482013-03-11 17:47:59 -07001801 spin_unlock_irqrestore(&hsotg->lock, flags);
1802 dwc2_hcd_qh_free(hsotg, qh);
1803 spin_lock_irqsave(&hsotg->lock, flags);
1804 }
1805
1806 spin_unlock_irqrestore(&hsotg->lock, flags);
1807}
1808
1809/*
1810 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1811 * and periodic schedules. The QTD associated with each URB is removed from
1812 * the schedule and freed. This function may be called when a disconnect is
1813 * detected or when the HCD is being stopped.
1814 *
1815 * Must be called with interrupt disabled and spinlock held
1816 */
1817static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1818{
1819 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
1820 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1821 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1822 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1823 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1824 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1825}
1826
1827/**
1828 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1829 *
1830 * @hsotg: Pointer to struct dwc2_hsotg
1831 */
1832void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1833{
1834 u32 hprt0;
1835
1836 if (hsotg->op_state == OTG_STATE_B_HOST) {
1837 /*
1838 * Reset the port. During a HNP mode switch the reset
1839 * needs to occur within 1ms and have a duration of at
1840 * least 50ms.
1841 */
1842 hprt0 = dwc2_read_hprt0(hsotg);
1843 hprt0 |= HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001844 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001845 }
1846
1847 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1848 msecs_to_jiffies(50));
1849}
1850
1851/* Must be called with interrupt disabled and spinlock held */
1852static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1853{
John Younbea8e862016-11-03 17:55:53 -07001854 int num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001855 struct dwc2_host_chan *channel;
1856 u32 hcchar;
1857 int i;
1858
John Youn95832c02017-01-23 14:57:26 -08001859 if (!hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001860 /* Flush out any channel requests in slave mode */
1861 for (i = 0; i < num_channels; i++) {
1862 channel = hsotg->hc_ptr_array[i];
1863 if (!list_empty(&channel->hc_list_entry))
1864 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001865 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001866 if (hcchar & HCCHAR_CHENA) {
1867 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1868 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001869 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001870 }
1871 }
1872 }
1873
1874 for (i = 0; i < num_channels; i++) {
1875 channel = hsotg->hc_ptr_array[i];
1876 if (!list_empty(&channel->hc_list_entry))
1877 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001878 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001879 if (hcchar & HCCHAR_CHENA) {
1880 /* Halt the channel */
1881 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001882 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001883 }
1884
1885 dwc2_hc_cleanup(hsotg, channel);
1886 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1887 /*
1888 * Added for Descriptor DMA to prevent channel double cleanup in
1889 * release_channel_ddma(), which is called from ep_disable when
1890 * device disconnects
1891 */
1892 channel->qh = NULL;
1893 }
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001894 /* All channels have been freed, mark them available */
John Youn95832c02017-01-23 14:57:26 -08001895 if (hsotg->params.uframe_sched) {
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001896 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07001897 hsotg->params.host_channels;
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001898 } else {
1899 hsotg->non_periodic_channels = 0;
1900 hsotg->periodic_channels = 0;
1901 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001902}
1903
1904/**
Douglas Anderson6a659532015-11-19 13:23:14 -08001905 * dwc2_hcd_connect() - Handles connect of the HCD
Paul Zimmerman7359d482013-03-11 17:47:59 -07001906 *
1907 * @hsotg: Pointer to struct dwc2_hsotg
1908 *
1909 * Must be called with interrupt disabled and spinlock held
1910 */
Douglas Anderson6a659532015-11-19 13:23:14 -08001911void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
1912{
1913 if (hsotg->lx_state != DWC2_L0)
1914 usb_hcd_resume_root_hub(hsotg->priv);
1915
1916 hsotg->flags.b.port_connect_status_change = 1;
1917 hsotg->flags.b.port_connect_status = 1;
1918}
1919
1920/**
1921 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
1922 *
1923 * @hsotg: Pointer to struct dwc2_hsotg
1924 * @force: If true, we won't try to reconnect even if we see device connected.
1925 *
1926 * Must be called with interrupt disabled and spinlock held
1927 */
1928void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
Paul Zimmerman7359d482013-03-11 17:47:59 -07001929{
1930 u32 intr;
Douglas Anderson6a659532015-11-19 13:23:14 -08001931 u32 hprt0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001932
1933 /* Set status flags for the hub driver */
1934 hsotg->flags.b.port_connect_status_change = 1;
1935 hsotg->flags.b.port_connect_status = 0;
1936
1937 /*
1938 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1939 * interrupt mask and status bits and disabling subsequent host
1940 * channel interrupts.
1941 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001942 intr = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001943 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001944 dwc2_writel(intr, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001945 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001946 dwc2_writel(intr, hsotg->regs + GINTSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001947
1948 /*
1949 * Turn off the vbus power only if the core has transitioned to device
1950 * mode. If still in host mode, need to keep power on to detect a
1951 * reconnection.
1952 */
1953 if (dwc2_is_device_mode(hsotg)) {
1954 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1955 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001956 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001957 }
1958
1959 dwc2_disable_host_interrupts(hsotg);
1960 }
1961
1962 /* Respond with an error status to all URBs in the schedule */
1963 dwc2_kill_all_urbs(hsotg);
1964
1965 if (dwc2_is_host_mode(hsotg))
1966 /* Clean up any host channels that were in use */
1967 dwc2_hcd_cleanup_channels(hsotg);
1968
1969 dwc2_host_disconnect(hsotg);
Douglas Anderson6a659532015-11-19 13:23:14 -08001970
1971 /*
1972 * Add an extra check here to see if we're actually connected but
1973 * we don't have a detection interrupt pending. This can happen if:
1974 * 1. hardware sees connect
1975 * 2. hardware sees disconnect
1976 * 3. hardware sees connect
1977 * 4. dwc2_port_intr() - clears connect interrupt
1978 * 5. dwc2_handle_common_intr() - calls here
1979 *
1980 * Without the extra check here we will end calling disconnect
1981 * and won't get any future interrupts to handle the connect.
1982 */
1983 if (!force) {
1984 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
1985 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
1986 dwc2_hcd_connect(hsotg);
1987 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001988}
1989
1990/**
1991 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
1992 *
1993 * @hsotg: Pointer to struct dwc2_hsotg
1994 */
1995static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
1996{
Douglas Anderson1fb7f122015-10-22 13:05:03 -07001997 if (hsotg->bus_suspended) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001998 hsotg->flags.b.port_suspend_change = 1;
Gregory Herrerob46146d52015-01-30 09:09:26 +01001999 usb_hcd_resume_root_hub(hsotg->priv);
Gregory Herrerob46146d52015-01-30 09:09:26 +01002000 }
Douglas Anderson1fb7f122015-10-22 13:05:03 -07002001
2002 if (hsotg->lx_state == DWC2_L1)
2003 hsotg->flags.b.port_l1_change = 1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002004}
2005
2006/**
2007 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
2008 *
2009 * @hsotg: Pointer to struct dwc2_hsotg
2010 *
2011 * Must be called with interrupt disabled and spinlock held
2012 */
2013void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
2014{
2015 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
2016
2017 /*
2018 * The root hub should be disconnected before this function is called.
2019 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
2020 * and the QH lists (via ..._hcd_endpoint_disable).
2021 */
2022
2023 /* Turn off all host-specific interrupts */
2024 dwc2_disable_host_interrupts(hsotg);
2025
2026 /* Turn off the vbus power */
2027 dev_dbg(hsotg->dev, "PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002028 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002029}
2030
Gregory Herrero33ad2612015-04-29 22:09:15 +02002031/* Caller must hold driver lock */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002032static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002033 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002034 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002035{
Paul Zimmerman7359d482013-03-11 17:47:59 -07002036 u32 intr_mask;
2037 int retval;
Nick Hudson9f8144c2013-12-06 14:01:44 -08002038 int dev_speed;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002039
2040 if (!hsotg->flags.b.port_connect_status) {
2041 /* No longer connected */
2042 dev_err(hsotg->dev, "Not connected\n");
2043 return -ENODEV;
2044 }
2045
Nick Hudson9f8144c2013-12-06 14:01:44 -08002046 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
2047
2048 /* Some configurations cannot support LS traffic on a FS root port */
2049 if ((dev_speed == USB_SPEED_LOW) &&
2050 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
2051 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002052 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Nick Hudson9f8144c2013-12-06 14:01:44 -08002053 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
2054
2055 if (prtspd == HPRT0_SPD_FULL_SPEED)
2056 return -ENODEV;
2057 }
2058
Paul Zimmerman7359d482013-03-11 17:47:59 -07002059 if (!qtd)
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002060 return -EINVAL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002061
2062 dwc2_hcd_qtd_init(qtd, urb);
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002063 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002064 if (retval) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002065 dev_err(hsotg->dev,
2066 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
2067 retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002068 return retval;
2069 }
2070
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002071 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002072 if (!(intr_mask & GINTSTS_SOF)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002073 enum dwc2_transaction_type tr_type;
2074
2075 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
2076 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
2077 /*
2078 * Do not schedule SG transactions until qtd has
2079 * URB_GIVEBACK_ASAP set
2080 */
2081 return 0;
2082
Paul Zimmerman7359d482013-03-11 17:47:59 -07002083 tr_type = dwc2_hcd_select_transactions(hsotg);
2084 if (tr_type != DWC2_TRANSACTION_NONE)
2085 dwc2_hcd_queue_transactions(hsotg, tr_type);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002086 }
2087
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002088 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002089}
2090
2091/* Must be called with interrupt disabled and spinlock held */
2092static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
2093 struct dwc2_hcd_urb *urb)
2094{
2095 struct dwc2_qh *qh;
2096 struct dwc2_qtd *urb_qtd;
2097
2098 urb_qtd = urb->qtd;
2099 if (!urb_qtd) {
2100 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
2101 return -EINVAL;
2102 }
2103
2104 qh = urb_qtd->qh;
2105 if (!qh) {
2106 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
2107 return -EINVAL;
2108 }
2109
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002110 urb->priv = NULL;
2111
Paul Zimmerman7359d482013-03-11 17:47:59 -07002112 if (urb_qtd->in_process && qh->channel) {
2113 dwc2_dump_channel_info(hsotg, qh->channel);
2114
2115 /* The QTD is in process (it has been assigned to a channel) */
2116 if (hsotg->flags.b.port_connect_status)
2117 /*
2118 * If still connected (i.e. in host mode), halt the
2119 * channel so it can be used for other transfers. If
2120 * no longer connected, the host registers can't be
2121 * written to halt the channel since the core is in
2122 * device mode.
2123 */
2124 dwc2_hc_halt(hsotg, qh->channel,
2125 DWC2_HC_XFER_URB_DEQUEUE);
2126 }
2127
2128 /*
2129 * Free the QTD and clean up the associated QH. Leave the QH in the
2130 * schedule if it has any remaining QTDs.
2131 */
John Youn95832c02017-01-23 14:57:26 -08002132 if (!hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002133 u8 in_process = urb_qtd->in_process;
2134
2135 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2136 if (in_process) {
2137 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
2138 qh->channel = NULL;
2139 } else if (list_empty(&qh->qtd_list)) {
2140 dwc2_hcd_qh_unlink(hsotg, qh);
2141 }
2142 } else {
2143 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2144 }
2145
2146 return 0;
2147}
2148
2149/* Must NOT be called with interrupt disabled or spinlock held */
2150static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
2151 struct usb_host_endpoint *ep, int retry)
2152{
2153 struct dwc2_qtd *qtd, *qtd_tmp;
2154 struct dwc2_qh *qh;
2155 unsigned long flags;
2156 int rc;
2157
2158 spin_lock_irqsave(&hsotg->lock, flags);
2159
2160 qh = ep->hcpriv;
2161 if (!qh) {
2162 rc = -EINVAL;
2163 goto err;
2164 }
2165
2166 while (!list_empty(&qh->qtd_list) && retry--) {
2167 if (retry == 0) {
2168 dev_err(hsotg->dev,
2169 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
2170 rc = -EBUSY;
2171 goto err;
2172 }
2173
2174 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01002175 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002176 spin_lock_irqsave(&hsotg->lock, flags);
2177 qh = ep->hcpriv;
2178 if (!qh) {
2179 rc = -EINVAL;
2180 goto err;
2181 }
2182 }
2183
2184 dwc2_hcd_qh_unlink(hsotg, qh);
2185
2186 /* Free each QTD in the QH's QTD list */
2187 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2188 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2189
2190 ep->hcpriv = NULL;
Douglas Anderson16e80212016-01-28 18:19:55 -08002191
2192 if (qh->channel && qh->channel->qh == qh)
2193 qh->channel->qh = NULL;
2194
Paul Zimmerman7359d482013-03-11 17:47:59 -07002195 spin_unlock_irqrestore(&hsotg->lock, flags);
Douglas Anderson16e80212016-01-28 18:19:55 -08002196
Paul Zimmerman7359d482013-03-11 17:47:59 -07002197 dwc2_hcd_qh_free(hsotg, qh);
2198
2199 return 0;
2200
2201err:
2202 ep->hcpriv = NULL;
2203 spin_unlock_irqrestore(&hsotg->lock, flags);
2204
2205 return rc;
2206}
2207
2208/* Must be called with interrupt disabled and spinlock held */
2209static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2210 struct usb_host_endpoint *ep)
2211{
2212 struct dwc2_qh *qh = ep->hcpriv;
2213
2214 if (!qh)
2215 return -EINVAL;
2216
2217 qh->data_toggle = DWC2_HC_PID_DATA0;
2218
2219 return 0;
2220}
2221
John Younb02038fa2016-02-23 19:55:00 -08002222/**
2223 * dwc2_core_init() - Initializes the DWC_otg controller registers and
2224 * prepares the core for device mode or host mode operation
2225 *
2226 * @hsotg: Programming view of the DWC_otg controller
2227 * @initial_setup: If true then this is the first init for this instance.
2228 */
2229static int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2230{
2231 u32 usbcfg, otgctl;
2232 int retval;
2233
2234 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2235
2236 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2237
2238 /* Set ULPI External VBUS bit if needed */
2239 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
John Youn95832c02017-01-23 14:57:26 -08002240 if (hsotg->params.phy_ulpi_ext_vbus)
John Younb02038fa2016-02-23 19:55:00 -08002241 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2242
2243 /* Set external TS Dline pulsing bit if needed */
2244 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
John Youn95832c02017-01-23 14:57:26 -08002245 if (hsotg->params.ts_dline)
John Younb02038fa2016-02-23 19:55:00 -08002246 usbcfg |= GUSBCFG_TERMSELDLPULSE;
2247
2248 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2249
2250 /*
2251 * Reset the Controller
2252 *
2253 * We only need to reset the controller if this is a re-init.
2254 * For the first init we know for sure that earlier code reset us (it
2255 * needed to in order to properly detect various parameters).
2256 */
2257 if (!initial_setup) {
2258 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
2259 if (retval) {
2260 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2261 __func__);
2262 return retval;
2263 }
2264 }
2265
2266 /*
2267 * This needs to happen in FS mode before any other programming occurs
2268 */
2269 retval = dwc2_phy_init(hsotg, initial_setup);
2270 if (retval)
2271 return retval;
2272
2273 /* Program the GAHBCFG Register */
2274 retval = dwc2_gahbcfg_init(hsotg);
2275 if (retval)
2276 return retval;
2277
2278 /* Program the GUSBCFG register */
2279 dwc2_gusbcfg_init(hsotg);
2280
2281 /* Program the GOTGCTL register */
2282 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2283 otgctl &= ~GOTGCTL_OTGVER;
John Younb02038fa2016-02-23 19:55:00 -08002284 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
John Younb02038fa2016-02-23 19:55:00 -08002285
2286 /* Clear the SRP success bit for FS-I2c */
2287 hsotg->srp_success = 0;
2288
2289 /* Enable common interrupts */
2290 dwc2_enable_common_interrupts(hsotg);
2291
2292 /*
2293 * Do device or host initialization based on mode during PCD and
2294 * HCD initialization
2295 */
2296 if (dwc2_is_host_mode(hsotg)) {
2297 dev_dbg(hsotg->dev, "Host Mode\n");
2298 hsotg->op_state = OTG_STATE_A_HOST;
2299 } else {
2300 dev_dbg(hsotg->dev, "Device Mode\n");
2301 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2302 }
2303
2304 return 0;
2305}
2306
2307/**
2308 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2309 * Host mode
2310 *
2311 * @hsotg: Programming view of DWC_otg controller
2312 *
2313 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2314 * request queues. Host channels are reset to ensure that they are ready for
2315 * performing transfers.
2316 */
2317static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2318{
2319 u32 hcfg, hfir, otgctl;
2320
2321 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2322
2323 /* Restart the Phy Clock */
2324 dwc2_writel(0, hsotg->regs + PCGCTL);
2325
2326 /* Initialize Host Configuration Register */
2327 dwc2_init_fs_ls_pclk_sel(hsotg);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08002328 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
2329 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
John Younb02038fa2016-02-23 19:55:00 -08002330 hcfg = dwc2_readl(hsotg->regs + HCFG);
2331 hcfg |= HCFG_FSLSSUPP;
2332 dwc2_writel(hcfg, hsotg->regs + HCFG);
2333 }
2334
2335 /*
2336 * This bit allows dynamic reloading of the HFIR register during
2337 * runtime. This bit needs to be programmed during initial configuration
2338 * and its value must not be changed during runtime.
2339 */
John Youn95832c02017-01-23 14:57:26 -08002340 if (hsotg->params.reload_ctl) {
John Younb02038fa2016-02-23 19:55:00 -08002341 hfir = dwc2_readl(hsotg->regs + HFIR);
2342 hfir |= HFIR_RLDCTRL;
2343 dwc2_writel(hfir, hsotg->regs + HFIR);
2344 }
2345
John Youn95832c02017-01-23 14:57:26 -08002346 if (hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002347 u32 op_mode = hsotg->hw_params.op_mode;
2348
2349 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2350 !hsotg->hw_params.dma_desc_enable ||
2351 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2352 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2353 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2354 dev_err(hsotg->dev,
2355 "Hardware does not support descriptor DMA mode -\n");
2356 dev_err(hsotg->dev,
2357 "falling back to buffer DMA mode.\n");
John Youn95832c02017-01-23 14:57:26 -08002358 hsotg->params.dma_desc_enable = false;
John Younb02038fa2016-02-23 19:55:00 -08002359 } else {
2360 hcfg = dwc2_readl(hsotg->regs + HCFG);
2361 hcfg |= HCFG_DESCDMA;
2362 dwc2_writel(hcfg, hsotg->regs + HCFG);
2363 }
2364 }
2365
2366 /* Configure data FIFO sizes */
2367 dwc2_config_fifos(hsotg);
2368
2369 /* TODO - check this */
2370 /* Clear Host Set HNP Enable in the OTG Control Register */
2371 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2372 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2373 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2374
2375 /* Make sure the FIFOs are flushed */
2376 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2377 dwc2_flush_rx_fifo(hsotg);
2378
2379 /* Clear Host Set HNP Enable in the OTG Control Register */
2380 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2381 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2382 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2383
John Youn95832c02017-01-23 14:57:26 -08002384 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002385 int num_channels, i;
2386 u32 hcchar;
2387
2388 /* Flush out any leftover queued requests */
John Younbea8e862016-11-03 17:55:53 -07002389 num_channels = hsotg->params.host_channels;
John Younb02038fa2016-02-23 19:55:00 -08002390 for (i = 0; i < num_channels; i++) {
2391 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2392 hcchar &= ~HCCHAR_CHENA;
2393 hcchar |= HCCHAR_CHDIS;
2394 hcchar &= ~HCCHAR_EPDIR;
2395 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2396 }
2397
2398 /* Halt all channels to put them into a known state */
2399 for (i = 0; i < num_channels; i++) {
2400 int count = 0;
2401
2402 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2403 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2404 hcchar &= ~HCCHAR_EPDIR;
2405 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2406 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2407 __func__, i);
2408 do {
2409 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2410 if (++count > 1000) {
2411 dev_err(hsotg->dev,
2412 "Unable to clear enable on channel %d\n",
2413 i);
2414 break;
2415 }
2416 udelay(1);
2417 } while (hcchar & HCCHAR_CHENA);
2418 }
2419 }
2420
2421 /* Turn on the vbus power */
2422 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2423 if (hsotg->op_state == OTG_STATE_A_HOST) {
2424 u32 hprt0 = dwc2_read_hprt0(hsotg);
2425
2426 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2427 !!(hprt0 & HPRT0_PWR));
2428 if (!(hprt0 & HPRT0_PWR)) {
2429 hprt0 |= HPRT0_PWR;
2430 dwc2_writel(hprt0, hsotg->regs + HPRT0);
2431 }
2432 }
2433
2434 dwc2_enable_host_interrupts(hsotg);
2435}
2436
Paul Zimmerman7359d482013-03-11 17:47:59 -07002437/*
2438 * Initializes dynamic portions of the DWC_otg HCD state
2439 *
2440 * Must be called with interrupt disabled and spinlock held
2441 */
2442static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2443{
2444 struct dwc2_host_chan *chan, *chan_tmp;
2445 int num_channels;
2446 int i;
2447
2448 hsotg->flags.d32 = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002449 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002450
John Youn95832c02017-01-23 14:57:26 -08002451 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002452 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07002453 hsotg->params.host_channels;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002454 } else {
2455 hsotg->non_periodic_channels = 0;
2456 hsotg->periodic_channels = 0;
2457 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002458
2459 /*
2460 * Put all channels in the free channel list and clean up channel
2461 * states
2462 */
2463 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2464 hc_list_entry)
2465 list_del_init(&chan->hc_list_entry);
2466
John Younbea8e862016-11-03 17:55:53 -07002467 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002468 for (i = 0; i < num_channels; i++) {
2469 chan = hsotg->hc_ptr_array[i];
2470 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2471 dwc2_hc_cleanup(hsotg, chan);
2472 }
2473
2474 /* Initialize the DWC core for host mode operation */
2475 dwc2_core_host_init(hsotg);
2476}
2477
2478static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2479 struct dwc2_host_chan *chan,
2480 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2481{
2482 int hub_addr, hub_port;
2483
2484 chan->do_split = 1;
2485 chan->xact_pos = qtd->isoc_split_pos;
2486 chan->complete_split = qtd->complete_split;
2487 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2488 chan->hub_addr = (u8)hub_addr;
2489 chan->hub_port = (u8)hub_port;
2490}
2491
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002492static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2493 struct dwc2_host_chan *chan,
2494 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002495{
2496 struct dwc2_hcd_urb *urb = qtd->urb;
2497 struct dwc2_hcd_iso_packet_desc *frame_desc;
2498
2499 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2500 case USB_ENDPOINT_XFER_CONTROL:
2501 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2502
2503 switch (qtd->control_phase) {
2504 case DWC2_CONTROL_SETUP:
2505 dev_vdbg(hsotg->dev, " Control setup transaction\n");
2506 chan->do_ping = 0;
2507 chan->ep_is_in = 0;
2508 chan->data_pid_start = DWC2_HC_PID_SETUP;
John Youn95832c02017-01-23 14:57:26 -08002509 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002510 chan->xfer_dma = urb->setup_dma;
2511 else
2512 chan->xfer_buf = urb->setup_packet;
2513 chan->xfer_len = 8;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002514 break;
2515
2516 case DWC2_CONTROL_DATA:
2517 dev_vdbg(hsotg->dev, " Control data transaction\n");
2518 chan->data_pid_start = qtd->data_toggle;
2519 break;
2520
2521 case DWC2_CONTROL_STATUS:
2522 /*
2523 * Direction is opposite of data direction or IN if no
2524 * data
2525 */
2526 dev_vdbg(hsotg->dev, " Control status transaction\n");
2527 if (urb->length == 0)
2528 chan->ep_is_in = 1;
2529 else
2530 chan->ep_is_in =
2531 dwc2_hcd_is_pipe_out(&urb->pipe_info);
2532 if (chan->ep_is_in)
2533 chan->do_ping = 0;
2534 chan->data_pid_start = DWC2_HC_PID_DATA1;
2535 chan->xfer_len = 0;
John Youn95832c02017-01-23 14:57:26 -08002536 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002537 chan->xfer_dma = hsotg->status_buf_dma;
2538 else
2539 chan->xfer_buf = hsotg->status_buf;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002540 break;
2541 }
2542 break;
2543
2544 case USB_ENDPOINT_XFER_BULK:
2545 chan->ep_type = USB_ENDPOINT_XFER_BULK;
2546 break;
2547
2548 case USB_ENDPOINT_XFER_INT:
2549 chan->ep_type = USB_ENDPOINT_XFER_INT;
2550 break;
2551
2552 case USB_ENDPOINT_XFER_ISOC:
2553 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
John Youn95832c02017-01-23 14:57:26 -08002554 if (hsotg->params.dma_desc_enable)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002555 break;
2556
2557 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2558 frame_desc->status = 0;
2559
John Youn95832c02017-01-23 14:57:26 -08002560 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002561 chan->xfer_dma = urb->dma;
2562 chan->xfer_dma += frame_desc->offset +
2563 qtd->isoc_split_offset;
2564 } else {
2565 chan->xfer_buf = urb->buf;
2566 chan->xfer_buf += frame_desc->offset +
2567 qtd->isoc_split_offset;
2568 }
2569
2570 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2571
Paul Zimmerman7359d482013-03-11 17:47:59 -07002572 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2573 if (chan->xfer_len <= 188)
2574 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2575 else
2576 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2577 }
2578 break;
2579 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002580}
2581
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002582#define DWC2_USB_DMA_ALIGN 4
2583
2584struct dma_aligned_buffer {
2585 void *kmalloc_ptr;
2586 void *old_xfer_buffer;
2587 u8 data[0];
2588};
2589
2590static void dwc2_free_dma_aligned_buffer(struct urb *urb)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002591{
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002592 struct dma_aligned_buffer *temp;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002593
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002594 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2595 return;
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002596
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002597 temp = container_of(urb->transfer_buffer,
John Youn9da51972017-01-17 20:30:27 -08002598 struct dma_aligned_buffer, data);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002599
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002600 if (usb_urb_dir_in(urb))
2601 memcpy(temp->old_xfer_buffer, temp->data,
2602 urb->transfer_buffer_length);
2603 urb->transfer_buffer = temp->old_xfer_buffer;
2604 kfree(temp->kmalloc_ptr);
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002605
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002606 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2607}
Paul Zimmerman7359d482013-03-11 17:47:59 -07002608
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002609static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
2610{
2611 struct dma_aligned_buffer *temp, *kmalloc_ptr;
2612 size_t kmalloc_size;
Gregory Herrerodb62b9a2015-04-29 22:09:16 +02002613
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002614 if (urb->num_sgs || urb->sg ||
2615 urb->transfer_buffer_length == 0 ||
2616 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2617 return 0;
2618
2619 /* Allocate a buffer with enough padding for alignment */
2620 kmalloc_size = urb->transfer_buffer_length +
2621 sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
2622
2623 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2624 if (!kmalloc_ptr)
2625 return -ENOMEM;
2626
2627 /* Position our struct dma_aligned_buffer such that data is aligned */
2628 temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
2629 temp->kmalloc_ptr = kmalloc_ptr;
2630 temp->old_xfer_buffer = urb->transfer_buffer;
2631 if (usb_urb_dir_out(urb))
2632 memcpy(temp->data, urb->transfer_buffer,
2633 urb->transfer_buffer_length);
2634 urb->transfer_buffer = temp->data;
2635
2636 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2637
Paul Zimmerman7359d482013-03-11 17:47:59 -07002638 return 0;
2639}
2640
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002641static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
John Youn9da51972017-01-17 20:30:27 -08002642 gfp_t mem_flags)
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002643{
2644 int ret;
2645
2646 /* We assume setup_dma is always aligned; warn if not */
2647 WARN_ON_ONCE(urb->setup_dma &&
2648 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
2649
2650 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
2651 if (ret)
2652 return ret;
2653
2654 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2655 if (ret)
2656 dwc2_free_dma_aligned_buffer(urb);
2657
2658 return ret;
2659}
2660
2661static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2662{
2663 usb_hcd_unmap_urb_for_dma(hcd, urb);
2664 dwc2_free_dma_aligned_buffer(urb);
2665}
2666
Paul Zimmerman7359d482013-03-11 17:47:59 -07002667/**
2668 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2669 * channel and initializes the host channel to perform the transactions. The
2670 * host channel is removed from the free list.
2671 *
2672 * @hsotg: The HCD state structure
2673 * @qh: Transactions from the first QTD for this QH are selected and assigned
2674 * to a free host channel
2675 */
Dom Cobley20f2eb92013-09-23 14:23:34 -07002676static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002677{
2678 struct dwc2_host_chan *chan;
2679 struct dwc2_hcd_urb *urb;
2680 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002681
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002682 if (dbg_qh(qh))
2683 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002684
2685 if (list_empty(&qh->qtd_list)) {
2686 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002687 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002688 }
2689
2690 if (list_empty(&hsotg->free_hc_list)) {
2691 dev_dbg(hsotg->dev, "No free channel to assign\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002692 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002693 }
2694
2695 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2696 hc_list_entry);
2697
Dom Cobley20f2eb92013-09-23 14:23:34 -07002698 /* Remove host channel from free list */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002699 list_del_init(&chan->hc_list_entry);
2700
2701 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2702 urb = qtd->urb;
2703 qh->channel = chan;
2704 qtd->in_process = 1;
2705
2706 /*
2707 * Use usb_pipedevice to determine device address. This address is
2708 * 0 before the SET_ADDRESS command and the correct address afterward.
2709 */
2710 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2711 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2712 chan->speed = qh->dev_speed;
2713 chan->max_packet = dwc2_max_packet(qh->maxp);
2714
2715 chan->xfer_started = 0;
2716 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2717 chan->error_state = (qtd->error_count > 0);
2718 chan->halt_on_queue = 0;
2719 chan->halt_pending = 0;
2720 chan->requests = 0;
2721
2722 /*
2723 * The following values may be modified in the transfer type section
2724 * below. The xfer_len value may be reduced when the transfer is
2725 * started to accommodate the max widths of the XferSize and PktCnt
2726 * fields in the HCTSIZn register.
2727 */
2728
2729 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2730 if (chan->ep_is_in)
2731 chan->do_ping = 0;
2732 else
2733 chan->do_ping = qh->ping_state;
2734
2735 chan->data_pid_start = qh->data_toggle;
2736 chan->multi_count = 1;
2737
Rashika Kheriabb6c3422013-10-26 23:11:22 +05302738 if (urb->actual_length > urb->length &&
John Youn9da51972017-01-17 20:30:27 -08002739 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
Paul Zimmerman84181082013-09-23 14:23:33 -07002740 urb->actual_length = urb->length;
2741
John Youn95832c02017-01-23 14:57:26 -08002742 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002743 chan->xfer_dma = urb->dma + urb->actual_length;
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002744 else
Paul Zimmerman7359d482013-03-11 17:47:59 -07002745 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002746
2747 chan->xfer_len = urb->length - urb->actual_length;
2748 chan->xfer_count = 0;
2749
2750 /* Set the split attributes if required */
2751 if (qh->do_split)
2752 dwc2_hc_init_split(hsotg, chan, qtd, urb);
2753 else
2754 chan->do_split = 0;
2755
2756 /* Set the transfer attributes */
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002757 dwc2_hc_init_xfer(hsotg, chan, qtd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002758
2759 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2760 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2761 /*
2762 * This value may be modified when the transfer is started
2763 * to reflect the actual transfer length
2764 */
2765 chan->multi_count = dwc2_hb_mult(qh->maxp);
2766
John Youn95832c02017-01-23 14:57:26 -08002767 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002768 chan->desc_list_addr = qh->desc_list_dma;
Gregory Herrero95105a92015-11-20 11:49:29 +01002769 chan->desc_list_sz = qh->desc_list_sz;
2770 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002771
2772 dwc2_hc_init(hsotg, chan);
2773 chan->qh = qh;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002774
2775 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002776}
2777
2778/**
2779 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2780 * schedule and assigns them to available host channels. Called from the HCD
2781 * interrupt handler functions.
2782 *
2783 * @hsotg: The HCD state structure
2784 *
2785 * Return: The types of new transactions that were assigned to host channels
2786 */
2787enum dwc2_transaction_type dwc2_hcd_select_transactions(
2788 struct dwc2_hsotg *hsotg)
2789{
2790 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2791 struct list_head *qh_ptr;
2792 struct dwc2_qh *qh;
2793 int num_channels;
2794
2795#ifdef DWC2_DEBUG_SOF
2796 dev_vdbg(hsotg->dev, " Select Transactions\n");
2797#endif
2798
2799 /* Process entries in the periodic ready list */
2800 qh_ptr = hsotg->periodic_sched_ready.next;
2801 while (qh_ptr != &hsotg->periodic_sched_ready) {
2802 if (list_empty(&hsotg->free_hc_list))
2803 break;
John Youn95832c02017-01-23 14:57:26 -08002804 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002805 if (hsotg->available_host_channels <= 1)
2806 break;
2807 hsotg->available_host_channels--;
2808 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002809 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -07002810 if (dwc2_assign_and_init_hc(hsotg, qh))
2811 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002812
2813 /*
2814 * Move the QH from the periodic ready schedule to the
2815 * periodic assigned schedule
2816 */
2817 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002818 list_move_tail(&qh->qh_list_entry,
2819 &hsotg->periodic_sched_assigned);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002820 ret_val = DWC2_TRANSACTION_PERIODIC;
2821 }
2822
2823 /*
2824 * Process entries in the inactive portion of the non-periodic
2825 * schedule. Some free host channels may not be used if they are
2826 * reserved for periodic transfers.
2827 */
John Younbea8e862016-11-03 17:55:53 -07002828 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002829 qh_ptr = hsotg->non_periodic_sched_inactive.next;
2830 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
John Youn95832c02017-01-23 14:57:26 -08002831 if (!hsotg->params.uframe_sched &&
Dom Cobley20f2eb92013-09-23 14:23:34 -07002832 hsotg->non_periodic_channels >= num_channels -
Paul Zimmerman7359d482013-03-11 17:47:59 -07002833 hsotg->periodic_channels)
2834 break;
2835 if (list_empty(&hsotg->free_hc_list))
2836 break;
2837 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
John Youn95832c02017-01-23 14:57:26 -08002838 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002839 if (hsotg->available_host_channels < 1)
2840 break;
2841 hsotg->available_host_channels--;
2842 }
2843
2844 if (dwc2_assign_and_init_hc(hsotg, qh))
2845 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002846
2847 /*
2848 * Move the QH from the non-periodic inactive schedule to the
2849 * non-periodic active schedule
2850 */
2851 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002852 list_move_tail(&qh->qh_list_entry,
2853 &hsotg->non_periodic_sched_active);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002854
2855 if (ret_val == DWC2_TRANSACTION_NONE)
2856 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2857 else
2858 ret_val = DWC2_TRANSACTION_ALL;
2859
John Youn95832c02017-01-23 14:57:26 -08002860 if (!hsotg->params.uframe_sched)
Dom Cobley20f2eb92013-09-23 14:23:34 -07002861 hsotg->non_periodic_channels++;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002862 }
2863
2864 return ret_val;
2865}
2866
2867/**
2868 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2869 * a host channel associated with either a periodic or non-periodic transfer
2870 *
2871 * @hsotg: The HCD state structure
2872 * @chan: Host channel descriptor associated with either a periodic or
2873 * non-periodic transfer
2874 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2875 * for periodic transfers or the non-periodic Tx FIFO
2876 * for non-periodic transfers
2877 *
2878 * Return: 1 if a request is queued and more requests may be needed to
2879 * complete the transfer, 0 if no more requests are required for this
2880 * transfer, -1 if there is insufficient space in the Tx FIFO
2881 *
2882 * This function assumes that there is space available in the appropriate
2883 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2884 * it checks whether space is available in the appropriate Tx FIFO.
2885 *
2886 * Must be called with interrupt disabled and spinlock held
2887 */
2888static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2889 struct dwc2_host_chan *chan,
2890 u16 fifo_dwords_avail)
2891{
2892 int retval = 0;
2893
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002894 if (chan->do_split)
2895 /* Put ourselves on the list to keep order straight */
2896 list_move_tail(&chan->split_order_list_entry,
2897 &hsotg->split_order);
2898
John Youn95832c02017-01-23 14:57:26 -08002899 if (hsotg->params.host_dma) {
2900 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002901 if (!chan->xfer_started ||
2902 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2903 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2904 chan->qh->ping_state = 0;
2905 }
2906 } else if (!chan->xfer_started) {
2907 dwc2_hc_start_transfer(hsotg, chan);
2908 chan->qh->ping_state = 0;
2909 }
2910 } else if (chan->halt_pending) {
2911 /* Don't queue a request if the channel has been halted */
2912 } else if (chan->halt_on_queue) {
2913 dwc2_hc_halt(hsotg, chan, chan->halt_status);
2914 } else if (chan->do_ping) {
2915 if (!chan->xfer_started)
2916 dwc2_hc_start_transfer(hsotg, chan);
2917 } else if (!chan->ep_is_in ||
2918 chan->data_pid_start == DWC2_HC_PID_SETUP) {
2919 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2920 if (!chan->xfer_started) {
2921 dwc2_hc_start_transfer(hsotg, chan);
2922 retval = 1;
2923 } else {
2924 retval = dwc2_hc_continue_transfer(hsotg, chan);
2925 }
2926 } else {
2927 retval = -1;
2928 }
2929 } else {
2930 if (!chan->xfer_started) {
2931 dwc2_hc_start_transfer(hsotg, chan);
2932 retval = 1;
2933 } else {
2934 retval = dwc2_hc_continue_transfer(hsotg, chan);
2935 }
2936 }
2937
2938 return retval;
2939}
2940
2941/*
2942 * Processes periodic channels for the next frame and queues transactions for
2943 * these channels to the DWC_otg controller. After queueing transactions, the
2944 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2945 * to queue as Periodic Tx FIFO or request queue space becomes available.
2946 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2947 *
2948 * Must be called with interrupt disabled and spinlock held
2949 */
2950static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2951{
2952 struct list_head *qh_ptr;
2953 struct dwc2_qh *qh;
2954 u32 tx_status;
2955 u32 fspcavail;
2956 u32 gintmsk;
2957 int status;
Douglas Anderson4e50e012016-01-28 18:20:03 -08002958 bool no_queue_space = false;
2959 bool no_fifo_space = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002960 u32 qspcavail;
2961
Douglas Anderson4e50e012016-01-28 18:20:03 -08002962 /* If empty list then just adjust interrupt enables */
2963 if (list_empty(&hsotg->periodic_sched_assigned))
2964 goto exit;
2965
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002966 if (dbg_perio())
2967 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07002968
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002969 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002970 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2971 TXSTS_QSPCAVAIL_SHIFT;
2972 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2973 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002974
2975 if (dbg_perio()) {
2976 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
2977 qspcavail);
2978 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
2979 fspcavail);
2980 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002981
2982 qh_ptr = hsotg->periodic_sched_assigned.next;
2983 while (qh_ptr != &hsotg->periodic_sched_assigned) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002984 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmanacdb9042013-08-30 18:45:16 +02002985 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2986 TXSTS_QSPCAVAIL_SHIFT;
2987 if (qspcavail == 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01002988 no_queue_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002989 break;
2990 }
2991
2992 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2993 if (!qh->channel) {
2994 qh_ptr = qh_ptr->next;
2995 continue;
2996 }
2997
2998 /* Make sure EP's TT buffer is clean before queueing qtds */
2999 if (qh->tt_buffer_dirty) {
3000 qh_ptr = qh_ptr->next;
3001 continue;
3002 }
3003
3004 /*
3005 * Set a flag if we're queuing high-bandwidth in slave mode.
3006 * The flag prevents any halts to get into the request queue in
3007 * the middle of multiple high-bandwidth packets getting queued.
3008 */
John Youn95832c02017-01-23 14:57:26 -08003009 if (!hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08003010 qh->channel->multi_count > 1)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003011 hsotg->queuing_high_bandwidth = 1;
3012
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003013 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3014 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003015 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3016 if (status < 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003017 no_fifo_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003018 break;
3019 }
3020
3021 /*
3022 * In Slave mode, stay on the current transfer until there is
3023 * nothing more to do or the high-bandwidth request count is
3024 * reached. In DMA mode, only need to queue one request. The
3025 * controller automatically handles multiple packets for
3026 * high-bandwidth transfers.
3027 */
John Youn95832c02017-01-23 14:57:26 -08003028 if (hsotg->params.host_dma || status == 0 ||
Paul Zimmerman7359d482013-03-11 17:47:59 -07003029 qh->channel->requests == qh->channel->multi_count) {
3030 qh_ptr = qh_ptr->next;
3031 /*
3032 * Move the QH from the periodic assigned schedule to
3033 * the periodic queued schedule
3034 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08003035 list_move_tail(&qh->qh_list_entry,
3036 &hsotg->periodic_sched_queued);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003037
3038 /* done queuing high bandwidth */
3039 hsotg->queuing_high_bandwidth = 0;
3040 }
3041 }
3042
Douglas Anderson4e50e012016-01-28 18:20:03 -08003043exit:
3044 if (no_queue_space || no_fifo_space ||
John Youn95832c02017-01-23 14:57:26 -08003045 (!hsotg->params.host_dma &&
Douglas Anderson4e50e012016-01-28 18:20:03 -08003046 !list_empty(&hsotg->periodic_sched_assigned))) {
3047 /*
3048 * May need to queue more transactions as the request
3049 * queue or Tx FIFO empties. Enable the periodic Tx
3050 * FIFO empty interrupt. (Always use the half-empty
3051 * level to ensure that new requests are loaded as
3052 * soon as possible.)
3053 */
3054 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3055 if (!(gintmsk & GINTSTS_PTXFEMP)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003056 gintmsk |= GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003057 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Douglas Anderson4e50e012016-01-28 18:20:03 -08003058 }
3059 } else {
3060 /*
3061 * Disable the Tx FIFO empty interrupt since there are
3062 * no more transactions that need to be queued right
3063 * now. This function is called from interrupt
3064 * handlers to queue more transactions as transfer
3065 * states change.
John Youn38beaec2017-01-17 20:31:13 -08003066 */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003067 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3068 if (gintmsk & GINTSTS_PTXFEMP) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003069 gintmsk &= ~GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003070 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003071 }
3072 }
3073}
3074
3075/*
3076 * Processes active non-periodic channels and queues transactions for these
3077 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3078 * FIFO Empty interrupt is enabled if there are more transactions to queue as
3079 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3080 * FIFO Empty interrupt is disabled.
3081 *
3082 * Must be called with interrupt disabled and spinlock held
3083 */
3084static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3085{
3086 struct list_head *orig_qh_ptr;
3087 struct dwc2_qh *qh;
3088 u32 tx_status;
3089 u32 qspcavail;
3090 u32 fspcavail;
3091 u32 gintmsk;
3092 int status;
3093 int no_queue_space = 0;
3094 int no_fifo_space = 0;
3095 int more_to_do = 0;
3096
3097 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3098
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003099 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003100 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3101 TXSTS_QSPCAVAIL_SHIFT;
3102 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3103 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003104 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
3105 qspcavail);
3106 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
3107 fspcavail);
3108
3109 /*
3110 * Keep track of the starting point. Skip over the start-of-list
3111 * entry.
3112 */
3113 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3114 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3115 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3116
3117 /*
3118 * Process once through the active list or until no more space is
3119 * available in the request queue or the Tx FIFO
3120 */
3121 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003122 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003123 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3124 TXSTS_QSPCAVAIL_SHIFT;
John Youn95832c02017-01-23 14:57:26 -08003125 if (!hsotg->params.host_dma && qspcavail == 0) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003126 no_queue_space = 1;
3127 break;
3128 }
3129
3130 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3131 qh_list_entry);
3132 if (!qh->channel)
3133 goto next;
3134
3135 /* Make sure EP's TT buffer is clean before queueing qtds */
3136 if (qh->tt_buffer_dirty)
3137 goto next;
3138
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003139 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3140 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003141 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3142
3143 if (status > 0) {
3144 more_to_do = 1;
3145 } else if (status < 0) {
3146 no_fifo_space = 1;
3147 break;
3148 }
3149next:
3150 /* Advance to next QH, skipping start-of-list entry */
3151 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3152 if (hsotg->non_periodic_qh_ptr ==
3153 &hsotg->non_periodic_sched_active)
3154 hsotg->non_periodic_qh_ptr =
3155 hsotg->non_periodic_qh_ptr->next;
3156 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3157
John Youn95832c02017-01-23 14:57:26 -08003158 if (!hsotg->params.host_dma) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003159 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003160 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3161 TXSTS_QSPCAVAIL_SHIFT;
3162 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3163 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003164 dev_vdbg(hsotg->dev,
3165 " NP Tx Req Queue Space Avail (after queue): %d\n",
3166 qspcavail);
3167 dev_vdbg(hsotg->dev,
3168 " NP Tx FIFO Space Avail (after queue): %d\n",
3169 fspcavail);
3170
3171 if (more_to_do || no_queue_space || no_fifo_space) {
3172 /*
3173 * May need to queue more transactions as the request
3174 * queue or Tx FIFO empties. Enable the non-periodic
3175 * Tx FIFO empty interrupt. (Always use the half-empty
3176 * level to ensure that new requests are loaded as
3177 * soon as possible.)
3178 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003179 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003180 gintmsk |= GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003181 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003182 } else {
3183 /*
3184 * Disable the Tx FIFO empty interrupt since there are
3185 * no more transactions that need to be queued right
3186 * now. This function is called from interrupt
3187 * handlers to queue more transactions as transfer
3188 * states change.
3189 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003190 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003191 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003192 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003193 }
3194 }
3195}
3196
3197/**
3198 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3199 * and queues transactions for these channels to the DWC_otg controller. Called
3200 * from the HCD interrupt handler functions.
3201 *
3202 * @hsotg: The HCD state structure
3203 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3204 * or both)
3205 *
3206 * Must be called with interrupt disabled and spinlock held
3207 */
3208void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3209 enum dwc2_transaction_type tr_type)
3210{
3211#ifdef DWC2_DEBUG_SOF
3212 dev_vdbg(hsotg->dev, "Queue Transactions\n");
3213#endif
3214 /* Process host channels associated with periodic transfers */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003215 if (tr_type == DWC2_TRANSACTION_PERIODIC ||
3216 tr_type == DWC2_TRANSACTION_ALL)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003217 dwc2_process_periodic_channels(hsotg);
3218
3219 /* Process host channels associated with non-periodic transfers */
3220 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3221 tr_type == DWC2_TRANSACTION_ALL) {
3222 if (!list_empty(&hsotg->non_periodic_sched_active)) {
3223 dwc2_process_non_periodic_channels(hsotg);
3224 } else {
3225 /*
3226 * Ensure NP Tx FIFO empty interrupt is disabled when
3227 * there are no non-periodic transfers to process
3228 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003229 u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003230
3231 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003232 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003233 }
3234 }
3235}
3236
3237static void dwc2_conn_id_status_change(struct work_struct *work)
3238{
3239 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3240 wf_otg);
3241 u32 count = 0;
3242 u32 gotgctl;
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003243 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003244
3245 dev_dbg(hsotg->dev, "%s()\n", __func__);
3246
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003247 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003248 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3249 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3250 !!(gotgctl & GOTGCTL_CONID_B));
3251
3252 /* B-Device connector (Device Mode) */
3253 if (gotgctl & GOTGCTL_CONID_B) {
3254 /* Wait for switch to device mode */
3255 dev_dbg(hsotg->dev, "connId B\n");
Chen Yu9156a7e2017-01-23 14:59:57 -08003256 if (hsotg->bus_suspended) {
3257 dev_info(hsotg->dev,
3258 "Do port resume before switching to device mode\n");
3259 dwc2_port_resume(hsotg);
3260 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003261 while (!dwc2_is_device_mode(hsotg)) {
3262 dev_info(hsotg->dev,
3263 "Waiting for Peripheral Mode, Mode=%s\n",
3264 dwc2_is_host_mode(hsotg) ? "Host" :
3265 "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003266 msleep(20);
John Stultzfc30c4b2017-01-23 14:59:35 -08003267 /*
3268 * Sometimes the initial GOTGCTRL read is wrong, so
3269 * check it again and jump to host mode if that was
3270 * the case.
3271 */
3272 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3273 if (!(gotgctl & GOTGCTL_CONID_B))
3274 goto host;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003275 if (++count > 250)
3276 break;
3277 }
3278 if (count > 250)
3279 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003280 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003281 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003282 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003283 dwc2_enable_global_interrupts(hsotg);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003284 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003285 dwc2_hsotg_core_init_disconnected(hsotg, false);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003286 spin_unlock_irqrestore(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003287 dwc2_hsotg_core_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003288 } else {
John Stultzfc30c4b2017-01-23 14:59:35 -08003289host:
Paul Zimmerman7359d482013-03-11 17:47:59 -07003290 /* A-Device connector (Host Mode) */
3291 dev_dbg(hsotg->dev, "connId A\n");
3292 while (!dwc2_is_host_mode(hsotg)) {
3293 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3294 dwc2_is_host_mode(hsotg) ?
3295 "Host" : "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003296 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003297 if (++count > 250)
3298 break;
3299 }
3300 if (count > 250)
3301 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003302 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003303
John Stultzd2471d42017-10-23 14:32:48 -07003304 spin_lock_irqsave(&hsotg->lock, flags);
3305 dwc2_hsotg_disconnect(hsotg);
3306 spin_unlock_irqrestore(&hsotg->lock, flags);
3307
3308 hsotg->op_state = OTG_STATE_A_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003309 /* Initialize the Core for Host mode */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003310 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003311 dwc2_enable_global_interrupts(hsotg);
3312 dwc2_hcd_start(hsotg);
3313 }
3314}
3315
3316static void dwc2_wakeup_detected(unsigned long data)
3317{
3318 struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)data;
3319 u32 hprt0;
3320
3321 dev_dbg(hsotg->dev, "%s()\n", __func__);
3322
3323 /*
3324 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3325 * so that OPT tests pass with all PHYs.)
3326 */
3327 hprt0 = dwc2_read_hprt0(hsotg);
3328 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3329 hprt0 &= ~HPRT0_RES;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003330 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003331 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003332 dwc2_readl(hsotg->regs + HPRT0));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003333
3334 dwc2_hcd_rem_wakeup(hsotg);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003335 hsotg->bus_suspended = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003336
3337 /* Change to L0 state */
3338 hsotg->lx_state = DWC2_L0;
3339}
3340
3341static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3342{
3343 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3344
3345 return hcd->self.b_hnp_enable;
3346}
3347
3348/* Must NOT be called with interrupt disabled or spinlock held */
3349static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3350{
3351 unsigned long flags;
3352 u32 hprt0;
3353 u32 pcgctl;
3354 u32 gotgctl;
3355
3356 dev_dbg(hsotg->dev, "%s()\n", __func__);
3357
3358 spin_lock_irqsave(&hsotg->lock, flags);
3359
3360 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003361 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003362 gotgctl |= GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003363 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003364 hsotg->op_state = OTG_STATE_A_SUSPEND;
3365 }
3366
3367 hprt0 = dwc2_read_hprt0(hsotg);
3368 hprt0 |= HPRT0_SUSP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003369 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003370
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003371 hsotg->bus_suspended = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003372
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003373 /*
3374 * If hibernation is supported, Phy clock will be suspended
3375 * after registers are backuped.
3376 */
John Younbea8e862016-11-03 17:55:53 -07003377 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003378 /* Suspend the Phy Clock */
3379 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3380 pcgctl |= PCGCTL_STOPPCLK;
3381 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3382 udelay(10);
3383 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003384
3385 /* For HNP the bus must be suspended for at least 200ms */
3386 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003387 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003388 pcgctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003389 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003390
3391 spin_unlock_irqrestore(&hsotg->lock, flags);
3392
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003393 msleep(200);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003394 } else {
3395 spin_unlock_irqrestore(&hsotg->lock, flags);
3396 }
3397}
3398
Gregory Herrero30db1032015-09-22 15:16:38 +02003399/* Must NOT be called with interrupt disabled or spinlock held */
3400static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
3401{
3402 unsigned long flags;
3403 u32 hprt0;
3404 u32 pcgctl;
3405
Douglas Anderson4d273c22015-10-14 15:58:27 -07003406 spin_lock_irqsave(&hsotg->lock, flags);
3407
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003408 /*
3409 * If hibernation is supported, Phy clock is already resumed
3410 * after registers restore.
3411 */
John Younbea8e862016-11-03 17:55:53 -07003412 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003413 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3414 pcgctl &= ~PCGCTL_STOPPCLK;
3415 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003416 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003417 msleep(20);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003418 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003419 }
Gregory Herrero30db1032015-09-22 15:16:38 +02003420
Gregory Herrero30db1032015-09-22 15:16:38 +02003421 hprt0 = dwc2_read_hprt0(hsotg);
3422 hprt0 |= HPRT0_RES;
3423 hprt0 &= ~HPRT0_SUSP;
3424 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3425 spin_unlock_irqrestore(&hsotg->lock, flags);
3426
3427 msleep(USB_RESUME_TIMEOUT);
3428
3429 spin_lock_irqsave(&hsotg->lock, flags);
3430 hprt0 = dwc2_read_hprt0(hsotg);
3431 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
3432 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003433 hsotg->bus_suspended = false;
Gregory Herrero30db1032015-09-22 15:16:38 +02003434 spin_unlock_irqrestore(&hsotg->lock, flags);
3435}
3436
Paul Zimmerman7359d482013-03-11 17:47:59 -07003437/* Handles hub class-specific requests */
3438static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3439 u16 wvalue, u16 windex, char *buf, u16 wlength)
3440{
3441 struct usb_hub_descriptor *hub_desc;
3442 int retval = 0;
3443 u32 hprt0;
3444 u32 port_status;
3445 u32 speed;
3446 u32 pcgctl;
3447
3448 switch (typereq) {
3449 case ClearHubFeature:
3450 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3451
3452 switch (wvalue) {
3453 case C_HUB_LOCAL_POWER:
3454 case C_HUB_OVER_CURRENT:
3455 /* Nothing required here */
3456 break;
3457
3458 default:
3459 retval = -EINVAL;
3460 dev_err(hsotg->dev,
3461 "ClearHubFeature request %1xh unknown\n",
3462 wvalue);
3463 }
3464 break;
3465
3466 case ClearPortFeature:
3467 if (wvalue != USB_PORT_FEAT_L1)
3468 if (!windex || windex > 1)
3469 goto error;
3470 switch (wvalue) {
3471 case USB_PORT_FEAT_ENABLE:
3472 dev_dbg(hsotg->dev,
3473 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3474 hprt0 = dwc2_read_hprt0(hsotg);
3475 hprt0 |= HPRT0_ENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003476 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003477 break;
3478
3479 case USB_PORT_FEAT_SUSPEND:
3480 dev_dbg(hsotg->dev,
3481 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00003482
Gregory Herrerobea78552015-09-22 15:16:44 +02003483 if (hsotg->bus_suspended)
3484 dwc2_port_resume(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003485 break;
3486
3487 case USB_PORT_FEAT_POWER:
3488 dev_dbg(hsotg->dev,
3489 "ClearPortFeature USB_PORT_FEAT_POWER\n");
3490 hprt0 = dwc2_read_hprt0(hsotg);
3491 hprt0 &= ~HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003492 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003493 break;
3494
3495 case USB_PORT_FEAT_INDICATOR:
3496 dev_dbg(hsotg->dev,
3497 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3498 /* Port indicator not supported */
3499 break;
3500
3501 case USB_PORT_FEAT_C_CONNECTION:
3502 /*
3503 * Clears driver's internal Connect Status Change flag
3504 */
3505 dev_dbg(hsotg->dev,
3506 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3507 hsotg->flags.b.port_connect_status_change = 0;
3508 break;
3509
3510 case USB_PORT_FEAT_C_RESET:
3511 /* Clears driver's internal Port Reset Change flag */
3512 dev_dbg(hsotg->dev,
3513 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3514 hsotg->flags.b.port_reset_change = 0;
3515 break;
3516
3517 case USB_PORT_FEAT_C_ENABLE:
3518 /*
3519 * Clears the driver's internal Port Enable/Disable
3520 * Change flag
3521 */
3522 dev_dbg(hsotg->dev,
3523 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3524 hsotg->flags.b.port_enable_change = 0;
3525 break;
3526
3527 case USB_PORT_FEAT_C_SUSPEND:
3528 /*
3529 * Clears the driver's internal Port Suspend Change
3530 * flag, which is set when resume signaling on the host
3531 * port is complete
3532 */
3533 dev_dbg(hsotg->dev,
3534 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3535 hsotg->flags.b.port_suspend_change = 0;
3536 break;
3537
3538 case USB_PORT_FEAT_C_PORT_L1:
3539 dev_dbg(hsotg->dev,
3540 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3541 hsotg->flags.b.port_l1_change = 0;
3542 break;
3543
3544 case USB_PORT_FEAT_C_OVER_CURRENT:
3545 dev_dbg(hsotg->dev,
3546 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3547 hsotg->flags.b.port_over_current_change = 0;
3548 break;
3549
3550 default:
3551 retval = -EINVAL;
3552 dev_err(hsotg->dev,
3553 "ClearPortFeature request %1xh unknown or unsupported\n",
3554 wvalue);
3555 }
3556 break;
3557
3558 case GetHubDescriptor:
3559 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3560 hub_desc = (struct usb_hub_descriptor *)buf;
3561 hub_desc->bDescLength = 9;
Sergei Shtylyova5dd0392015-03-29 01:36:28 +03003562 hub_desc->bDescriptorType = USB_DT_HUB;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003563 hub_desc->bNbrPorts = 1;
Sergei Shtylyov3d040de2015-01-19 01:54:15 +03003564 hub_desc->wHubCharacteristics =
3565 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
3566 HUB_CHAR_INDV_PORT_OCPM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003567 hub_desc->bPwrOn2PwrGood = 1;
3568 hub_desc->bHubContrCurrent = 0;
3569 hub_desc->u.hs.DeviceRemovable[0] = 0;
3570 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3571 break;
3572
3573 case GetHubStatus:
3574 dev_dbg(hsotg->dev, "GetHubStatus\n");
3575 memset(buf, 0, 4);
3576 break;
3577
3578 case GetPortStatus:
Paul Zimmermanb8313412013-05-24 16:32:12 -07003579 dev_vdbg(hsotg->dev,
3580 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3581 hsotg->flags.d32);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003582 if (!windex || windex > 1)
3583 goto error;
3584
3585 port_status = 0;
3586 if (hsotg->flags.b.port_connect_status_change)
3587 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3588 if (hsotg->flags.b.port_enable_change)
3589 port_status |= USB_PORT_STAT_C_ENABLE << 16;
3590 if (hsotg->flags.b.port_suspend_change)
3591 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3592 if (hsotg->flags.b.port_l1_change)
3593 port_status |= USB_PORT_STAT_C_L1 << 16;
3594 if (hsotg->flags.b.port_reset_change)
3595 port_status |= USB_PORT_STAT_C_RESET << 16;
3596 if (hsotg->flags.b.port_over_current_change) {
3597 dev_warn(hsotg->dev, "Overcurrent change detected\n");
3598 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3599 }
3600
3601 if (!hsotg->flags.b.port_connect_status) {
3602 /*
3603 * The port is disconnected, which means the core is
3604 * either in device mode or it soon will be. Just
3605 * return 0's for the remainder of the port status
3606 * since the port register can't be read if the core
3607 * is in device mode.
3608 */
3609 *(__le32 *)buf = cpu_to_le32(port_status);
3610 break;
3611 }
3612
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003613 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmermanb8313412013-05-24 16:32:12 -07003614 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003615
3616 if (hprt0 & HPRT0_CONNSTS)
3617 port_status |= USB_PORT_STAT_CONNECTION;
3618 if (hprt0 & HPRT0_ENA)
3619 port_status |= USB_PORT_STAT_ENABLE;
3620 if (hprt0 & HPRT0_SUSP)
3621 port_status |= USB_PORT_STAT_SUSPEND;
3622 if (hprt0 & HPRT0_OVRCURRACT)
3623 port_status |= USB_PORT_STAT_OVERCURRENT;
3624 if (hprt0 & HPRT0_RST)
3625 port_status |= USB_PORT_STAT_RESET;
3626 if (hprt0 & HPRT0_PWR)
3627 port_status |= USB_PORT_STAT_POWER;
3628
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02003629 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003630 if (speed == HPRT0_SPD_HIGH_SPEED)
3631 port_status |= USB_PORT_STAT_HIGH_SPEED;
3632 else if (speed == HPRT0_SPD_LOW_SPEED)
3633 port_status |= USB_PORT_STAT_LOW_SPEED;
3634
3635 if (hprt0 & HPRT0_TSTCTL_MASK)
3636 port_status |= USB_PORT_STAT_TEST;
3637 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3638
John Younbea8e862016-11-03 17:55:53 -07003639 if (hsotg->params.dma_desc_fs_enable) {
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003640 /*
3641 * Enable descriptor DMA only if a full speed
3642 * device is connected.
3643 */
3644 if (hsotg->new_connection &&
3645 ((port_status &
3646 (USB_PORT_STAT_CONNECTION |
3647 USB_PORT_STAT_HIGH_SPEED |
3648 USB_PORT_STAT_LOW_SPEED)) ==
3649 USB_PORT_STAT_CONNECTION)) {
3650 u32 hcfg;
3651
3652 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
John Youn95832c02017-01-23 14:57:26 -08003653 hsotg->params.dma_desc_enable = true;
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003654 hcfg = dwc2_readl(hsotg->regs + HCFG);
3655 hcfg |= HCFG_DESCDMA;
3656 dwc2_writel(hcfg, hsotg->regs + HCFG);
3657 hsotg->new_connection = false;
3658 }
3659 }
3660
Paul Zimmermanb8313412013-05-24 16:32:12 -07003661 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003662 *(__le32 *)buf = cpu_to_le32(port_status);
3663 break;
3664
3665 case SetHubFeature:
3666 dev_dbg(hsotg->dev, "SetHubFeature\n");
3667 /* No HUB features supported */
3668 break;
3669
3670 case SetPortFeature:
3671 dev_dbg(hsotg->dev, "SetPortFeature\n");
3672 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3673 goto error;
3674
3675 if (!hsotg->flags.b.port_connect_status) {
3676 /*
3677 * The port is disconnected, which means the core is
3678 * either in device mode or it soon will be. Just
3679 * return without doing anything since the port
3680 * register can't be written if the core is in device
3681 * mode.
3682 */
3683 break;
3684 }
3685
3686 switch (wvalue) {
3687 case USB_PORT_FEAT_SUSPEND:
3688 dev_dbg(hsotg->dev,
3689 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3690 if (windex != hsotg->otg_port)
3691 goto error;
3692 dwc2_port_suspend(hsotg, windex);
3693 break;
3694
3695 case USB_PORT_FEAT_POWER:
3696 dev_dbg(hsotg->dev,
3697 "SetPortFeature - USB_PORT_FEAT_POWER\n");
3698 hprt0 = dwc2_read_hprt0(hsotg);
3699 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003700 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003701 break;
3702
3703 case USB_PORT_FEAT_RESET:
3704 hprt0 = dwc2_read_hprt0(hsotg);
3705 dev_dbg(hsotg->dev,
3706 "SetPortFeature - USB_PORT_FEAT_RESET\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003707 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003708 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003709 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003710 /* ??? Original driver does this */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003711 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003712
3713 hprt0 = dwc2_read_hprt0(hsotg);
3714 /* Clear suspend bit if resetting from suspend state */
3715 hprt0 &= ~HPRT0_SUSP;
3716
3717 /*
3718 * When B-Host the Port reset bit is set in the Start
3719 * HCD Callback function, so that the reset is started
3720 * within 1ms of the HNP success interrupt
3721 */
3722 if (!dwc2_hcd_is_b_host(hsotg)) {
3723 hprt0 |= HPRT0_PWR | HPRT0_RST;
3724 dev_dbg(hsotg->dev,
3725 "In host mode, hprt0=%08x\n", hprt0);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003726 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003727 }
3728
3729 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003730 msleep(50);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003731 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003732 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003733 hsotg->lx_state = DWC2_L0; /* Now back to On state */
3734 break;
3735
3736 case USB_PORT_FEAT_INDICATOR:
3737 dev_dbg(hsotg->dev,
3738 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3739 /* Not supported */
3740 break;
3741
Jingwu Lin96d480e2015-04-29 22:09:17 +02003742 case USB_PORT_FEAT_TEST:
3743 hprt0 = dwc2_read_hprt0(hsotg);
3744 dev_dbg(hsotg->dev,
3745 "SetPortFeature - USB_PORT_FEAT_TEST\n");
3746 hprt0 &= ~HPRT0_TSTCTL_MASK;
3747 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003748 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Jingwu Lin96d480e2015-04-29 22:09:17 +02003749 break;
3750
Paul Zimmerman7359d482013-03-11 17:47:59 -07003751 default:
3752 retval = -EINVAL;
3753 dev_err(hsotg->dev,
3754 "SetPortFeature %1xh unknown or unsupported\n",
3755 wvalue);
3756 break;
3757 }
3758 break;
3759
3760 default:
3761error:
3762 retval = -EINVAL;
3763 dev_dbg(hsotg->dev,
3764 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3765 typereq, windex, wvalue);
3766 break;
3767 }
3768
3769 return retval;
3770}
3771
3772static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3773{
3774 int retval;
3775
Paul Zimmerman7359d482013-03-11 17:47:59 -07003776 if (port != 1)
3777 return -EINVAL;
3778
3779 retval = (hsotg->flags.b.port_connect_status_change ||
3780 hsotg->flags.b.port_reset_change ||
3781 hsotg->flags.b.port_enable_change ||
3782 hsotg->flags.b.port_suspend_change ||
3783 hsotg->flags.b.port_over_current_change);
3784
3785 if (retval) {
3786 dev_dbg(hsotg->dev,
3787 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3788 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
3789 hsotg->flags.b.port_connect_status_change);
3790 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
3791 hsotg->flags.b.port_reset_change);
3792 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
3793 hsotg->flags.b.port_enable_change);
3794 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
3795 hsotg->flags.b.port_suspend_change);
3796 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
3797 hsotg->flags.b.port_over_current_change);
3798 }
3799
3800 return retval;
3801}
3802
3803int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3804{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003805 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003806
3807#ifdef DWC2_DEBUG_SOF
3808 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003809 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003810#endif
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003811 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003812}
3813
Douglas Andersonfae4e822016-01-28 18:20:10 -08003814int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3815{
3816 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
3817 u32 hfir = dwc2_readl(hsotg->regs + HFIR);
3818 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3819 unsigned int us_per_frame;
3820 unsigned int frame_number;
3821 unsigned int remaining;
3822 unsigned int interval;
3823 unsigned int phy_clks;
3824
3825 /* High speed has 125 us per (micro) frame; others are 1 ms per */
3826 us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3827
3828 /* Extract fields */
3829 frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3830 remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3831 interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3832
3833 /*
3834 * Number of phy clocks since the last tick of the frame number after
3835 * "us" has passed.
3836 */
3837 phy_clks = (interval - remaining) +
3838 DIV_ROUND_UP(interval * us, us_per_frame);
3839
3840 return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3841}
3842
Paul Zimmerman7359d482013-03-11 17:47:59 -07003843int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3844{
Aldo Iljazi6bf2e2a2013-11-30 19:33:57 +02003845 return hsotg->op_state == OTG_STATE_B_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003846}
3847
3848static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3849 int iso_desc_count,
3850 gfp_t mem_flags)
3851{
3852 struct dwc2_hcd_urb *urb;
3853 u32 size = sizeof(*urb) + iso_desc_count *
3854 sizeof(struct dwc2_hcd_iso_packet_desc);
3855
3856 urb = kzalloc(size, mem_flags);
3857 if (urb)
3858 urb->packet_count = iso_desc_count;
3859 return urb;
3860}
3861
3862static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3863 struct dwc2_hcd_urb *urb, u8 dev_addr,
3864 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
3865{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02003866 if (dbg_perio() ||
3867 ep_type == USB_ENDPOINT_XFER_BULK ||
3868 ep_type == USB_ENDPOINT_XFER_CONTROL)
3869 dev_vdbg(hsotg->dev,
3870 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
3871 dev_addr, ep_num, ep_dir, ep_type, mps);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003872 urb->pipe_info.dev_addr = dev_addr;
3873 urb->pipe_info.ep_num = ep_num;
3874 urb->pipe_info.pipe_type = ep_type;
3875 urb->pipe_info.pipe_dir = ep_dir;
3876 urb->pipe_info.mps = mps;
3877}
3878
3879/*
3880 * NOTE: This function will be removed once the peripheral controller code
3881 * is integrated and the driver is stable
3882 */
3883void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3884{
3885#ifdef DEBUG
3886 struct dwc2_host_chan *chan;
3887 struct dwc2_hcd_urb *urb;
3888 struct dwc2_qtd *qtd;
3889 int num_channels;
3890 u32 np_tx_status;
3891 u32 p_tx_status;
3892 int i;
3893
John Younbea8e862016-11-03 17:55:53 -07003894 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003895 dev_dbg(hsotg->dev, "\n");
3896 dev_dbg(hsotg->dev,
3897 "************************************************************\n");
3898 dev_dbg(hsotg->dev, "HCD State:\n");
3899 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
3900
3901 for (i = 0; i < num_channels; i++) {
3902 chan = hsotg->hc_ptr_array[i];
3903 dev_dbg(hsotg->dev, " Channel %d:\n", i);
3904 dev_dbg(hsotg->dev,
3905 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3906 chan->dev_addr, chan->ep_num, chan->ep_is_in);
3907 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
3908 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
3909 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
3910 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
3911 chan->data_pid_start);
3912 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
3913 dev_dbg(hsotg->dev, " xfer_started: %d\n",
3914 chan->xfer_started);
3915 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
3916 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
3917 (unsigned long)chan->xfer_dma);
3918 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
3919 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
3920 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
3921 chan->halt_on_queue);
3922 dev_dbg(hsotg->dev, " halt_pending: %d\n",
3923 chan->halt_pending);
3924 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
3925 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
3926 dev_dbg(hsotg->dev, " complete_split: %d\n",
3927 chan->complete_split);
3928 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
3929 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
3930 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
3931 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
3932 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
3933
3934 if (chan->xfer_started) {
3935 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3936
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003937 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3938 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
3939 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
3940 hcint = dwc2_readl(hsotg->regs + HCINT(i));
3941 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003942 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
3943 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
3944 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
3945 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
3946 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
3947 }
3948
3949 if (!(chan->xfer_started && chan->qh))
3950 continue;
3951
3952 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3953 if (!qtd->in_process)
3954 break;
3955 urb = qtd->urb;
3956 dev_dbg(hsotg->dev, " URB Info:\n");
3957 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
3958 qtd, urb);
3959 if (urb) {
3960 dev_dbg(hsotg->dev,
3961 " Dev: %d, EP: %d %s\n",
3962 dwc2_hcd_get_dev_addr(&urb->pipe_info),
3963 dwc2_hcd_get_ep_num(&urb->pipe_info),
3964 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3965 "IN" : "OUT");
3966 dev_dbg(hsotg->dev,
3967 " Max packet size: %d\n",
3968 dwc2_hcd_get_mps(&urb->pipe_info));
3969 dev_dbg(hsotg->dev,
3970 " transfer_buffer: %p\n",
3971 urb->buf);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07003972 dev_dbg(hsotg->dev,
3973 " transfer_dma: %08lx\n",
3974 (unsigned long)urb->dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003975 dev_dbg(hsotg->dev,
3976 " transfer_buffer_length: %d\n",
3977 urb->length);
3978 dev_dbg(hsotg->dev, " actual_length: %d\n",
3979 urb->actual_length);
3980 }
3981 }
3982 }
3983
3984 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
3985 hsotg->non_periodic_channels);
3986 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
3987 hsotg->periodic_channels);
3988 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003989 np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003990 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003991 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003992 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003993 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003994 p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003995 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003996 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003997 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003998 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003999 dwc2_hcd_dump_frrem(hsotg);
4000 dwc2_dump_global_registers(hsotg);
4001 dwc2_dump_host_registers(hsotg);
4002 dev_dbg(hsotg->dev,
4003 "************************************************************\n");
4004 dev_dbg(hsotg->dev, "\n");
4005#endif
4006}
4007
4008/*
4009 * NOTE: This function will be removed once the peripheral controller code
4010 * is integrated and the driver is stable
4011 */
4012void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg)
4013{
4014#ifdef DWC2_DUMP_FRREM
4015 dev_dbg(hsotg->dev, "Frame remaining at SOF:\n");
4016 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4017 hsotg->frrem_samples, hsotg->frrem_accum,
4018 hsotg->frrem_samples > 0 ?
4019 hsotg->frrem_accum / hsotg->frrem_samples : 0);
4020 dev_dbg(hsotg->dev, "\n");
4021 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n");
4022 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4023 hsotg->hfnum_7_samples,
4024 hsotg->hfnum_7_frrem_accum,
4025 hsotg->hfnum_7_samples > 0 ?
4026 hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0);
4027 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n");
4028 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4029 hsotg->hfnum_0_samples,
4030 hsotg->hfnum_0_frrem_accum,
4031 hsotg->hfnum_0_samples > 0 ?
4032 hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0);
4033 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n");
4034 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4035 hsotg->hfnum_other_samples,
4036 hsotg->hfnum_other_frrem_accum,
4037 hsotg->hfnum_other_samples > 0 ?
4038 hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples :
4039 0);
4040 dev_dbg(hsotg->dev, "\n");
4041 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n");
4042 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4043 hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a,
4044 hsotg->hfnum_7_samples_a > 0 ?
4045 hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0);
4046 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n");
4047 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4048 hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a,
4049 hsotg->hfnum_0_samples_a > 0 ?
4050 hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0);
4051 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n");
4052 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4053 hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a,
4054 hsotg->hfnum_other_samples_a > 0 ?
4055 hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a
4056 : 0);
4057 dev_dbg(hsotg->dev, "\n");
4058 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n");
4059 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4060 hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b,
4061 hsotg->hfnum_7_samples_b > 0 ?
4062 hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0);
4063 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n");
4064 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4065 hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b,
4066 (hsotg->hfnum_0_samples_b > 0) ?
4067 hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0);
4068 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n");
4069 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4070 hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b,
4071 (hsotg->hfnum_other_samples_b > 0) ?
4072 hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b
4073 : 0);
4074#endif
4075}
4076
4077struct wrapper_priv_data {
4078 struct dwc2_hsotg *hsotg;
4079};
4080
4081/* Gets the dwc2_hsotg from a usb_hcd */
4082static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4083{
4084 struct wrapper_priv_data *p;
4085
John Youn9da51972017-01-17 20:30:27 -08004086 p = (struct wrapper_priv_data *)&hcd->hcd_priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004087 return p->hsotg;
4088}
4089
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004090/**
4091 * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
4092 *
4093 * This will get the dwc2_tt structure (and ttport) associated with the given
4094 * context (which is really just a struct urb pointer).
4095 *
4096 * The first time this is called for a given TT we allocate memory for our
4097 * structure. When everyone is done and has called dwc2_host_put_tt_info()
4098 * then the refcount for the structure will go to 0 and we'll free it.
4099 *
4100 * @hsotg: The HCD state structure for the DWC OTG controller.
4101 * @qh: The QH structure.
4102 * @context: The priv pointer from a struct dwc2_hcd_urb.
4103 * @mem_flags: Flags for allocating memory.
4104 * @ttport: We'll return this device's port number here. That's used to
4105 * reference into the bitmap if we're on a multi_tt hub.
4106 *
4107 * Return: a pointer to a struct dwc2_tt. Don't forget to call
4108 * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
4109 */
4110
4111struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
4112 gfp_t mem_flags, int *ttport)
4113{
4114 struct urb *urb = context;
4115 struct dwc2_tt *dwc_tt = NULL;
4116
4117 if (urb->dev->tt) {
4118 *ttport = urb->dev->ttport;
4119
4120 dwc_tt = urb->dev->tt->hcpriv;
John Youn9da51972017-01-17 20:30:27 -08004121 if (!dwc_tt) {
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004122 size_t bitmap_size;
4123
4124 /*
4125 * For single_tt we need one schedule. For multi_tt
4126 * we need one per port.
4127 */
4128 bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
4129 sizeof(dwc_tt->periodic_bitmaps[0]);
4130 if (urb->dev->tt->multi)
4131 bitmap_size *= urb->dev->tt->hub->maxchild;
4132
4133 dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
4134 mem_flags);
John Youn9da51972017-01-17 20:30:27 -08004135 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004136 return NULL;
4137
4138 dwc_tt->usb_tt = urb->dev->tt;
4139 dwc_tt->usb_tt->hcpriv = dwc_tt;
4140 }
4141
4142 dwc_tt->refcount++;
4143 }
4144
4145 return dwc_tt;
4146}
4147
4148/**
4149 * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
4150 *
4151 * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
4152 * of the structure are done.
4153 *
4154 * It's OK to call this with NULL.
4155 *
4156 * @hsotg: The HCD state structure for the DWC OTG controller.
4157 * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
4158 */
4159void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
4160{
4161 /* Model kfree and make put of NULL a no-op */
John Youn9da51972017-01-17 20:30:27 -08004162 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004163 return;
4164
4165 WARN_ON(dwc_tt->refcount < 1);
4166
4167 dwc_tt->refcount--;
4168 if (!dwc_tt->refcount) {
4169 dwc_tt->usb_tt->hcpriv = NULL;
4170 kfree(dwc_tt);
4171 }
4172}
4173
Paul Zimmerman7359d482013-03-11 17:47:59 -07004174int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4175{
4176 struct urb *urb = context;
4177
4178 return urb->dev->speed;
4179}
4180
4181static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4182 struct urb *urb)
4183{
4184 struct usb_bus *bus = hcd_to_bus(hcd);
4185
4186 if (urb->interval)
4187 bus->bandwidth_allocated += bw / urb->interval;
4188 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4189 bus->bandwidth_isoc_reqs++;
4190 else
4191 bus->bandwidth_int_reqs++;
4192}
4193
4194static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4195 struct urb *urb)
4196{
4197 struct usb_bus *bus = hcd_to_bus(hcd);
4198
4199 if (urb->interval)
4200 bus->bandwidth_allocated -= bw / urb->interval;
4201 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4202 bus->bandwidth_isoc_reqs--;
4203 else
4204 bus->bandwidth_int_reqs--;
4205}
4206
4207/*
4208 * Sets the final status of an URB and returns it to the upper layer. Any
4209 * required cleanup of the URB is performed.
4210 *
4211 * Must be called with interrupt disabled and spinlock held
4212 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004213void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4214 int status)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004215{
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004216 struct urb *urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004217 int i;
4218
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004219 if (!qtd) {
4220 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4221 return;
4222 }
4223
4224 if (!qtd->urb) {
4225 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4226 return;
4227 }
4228
4229 urb = qtd->urb->priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004230 if (!urb) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004231 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004232 return;
4233 }
4234
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004235 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004236
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004237 if (dbg_urb(urb))
4238 dev_vdbg(hsotg->dev,
4239 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4240 __func__, urb, usb_pipedevice(urb->pipe),
4241 usb_pipeendpoint(urb->pipe),
4242 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4243 urb->actual_length);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004244
Paul Zimmerman7359d482013-03-11 17:47:59 -07004245 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004246 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004247 for (i = 0; i < urb->number_of_packets; ++i) {
4248 urb->iso_frame_desc[i].actual_length =
4249 dwc2_hcd_urb_get_iso_desc_actual_length(
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004250 qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004251 urb->iso_frame_desc[i].status =
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004252 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004253 }
4254 }
4255
Gregory Herrerofe9b1772015-09-22 15:16:51 +02004256 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4257 for (i = 0; i < urb->number_of_packets; i++)
4258 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4259 i, urb->iso_frame_desc[i].status);
4260 }
4261
Paul Zimmerman7359d482013-03-11 17:47:59 -07004262 urb->status = status;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004263 if (!status) {
4264 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4265 urb->actual_length < urb->transfer_buffer_length)
4266 urb->status = -EREMOTEIO;
4267 }
4268
4269 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4270 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4271 struct usb_host_endpoint *ep = urb->ep;
4272
4273 if (ep)
4274 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4275 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4276 urb);
4277 }
4278
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004279 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004280 urb->hcpriv = NULL;
4281 kfree(qtd->urb);
4282 qtd->urb = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004283
Paul Zimmerman7359d482013-03-11 17:47:59 -07004284 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004285}
4286
4287/*
4288 * Work queue function for starting the HCD when A-Cable is connected
4289 */
4290static void dwc2_hcd_start_func(struct work_struct *work)
4291{
4292 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4293 start_work.work);
4294
4295 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4296 dwc2_host_start(hsotg);
4297}
4298
4299/*
4300 * Reset work queue function
4301 */
4302static void dwc2_hcd_reset_func(struct work_struct *work)
4303{
4304 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4305 reset_work.work);
Douglas Anderson4a065c72015-11-20 09:06:27 -08004306 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004307 u32 hprt0;
4308
4309 dev_dbg(hsotg->dev, "USB RESET function called\n");
Douglas Anderson4a065c72015-11-20 09:06:27 -08004310
4311 spin_lock_irqsave(&hsotg->lock, flags);
4312
Paul Zimmerman7359d482013-03-11 17:47:59 -07004313 hprt0 = dwc2_read_hprt0(hsotg);
4314 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004315 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004316 hsotg->flags.b.port_reset_change = 1;
Douglas Anderson4a065c72015-11-20 09:06:27 -08004317
4318 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004319}
4320
4321/*
4322 * =========================================================================
4323 * Linux HC Driver Functions
4324 * =========================================================================
4325 */
4326
4327/*
4328 * Initializes the DWC_otg controller and its root hub and prepares it for host
4329 * mode operation. Activates the root port. Returns 0 on success and a negative
4330 * error code on failure.
4331 */
4332static int _dwc2_hcd_start(struct usb_hcd *hcd)
4333{
4334 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4335 struct usb_bus *bus = hcd_to_bus(hcd);
4336 unsigned long flags;
4337
4338 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4339
4340 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero31927b62015-09-22 15:16:41 +02004341 hsotg->lx_state = DWC2_L0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004342 hcd->state = HC_STATE_RUNNING;
Gregory Herrero31927b62015-09-22 15:16:41 +02004343 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004344
4345 if (dwc2_is_device_mode(hsotg)) {
4346 spin_unlock_irqrestore(&hsotg->lock, flags);
4347 return 0; /* why 0 ?? */
4348 }
4349
4350 dwc2_hcd_reinit(hsotg);
4351
4352 /* Initialize and connect root hub if one is not already attached */
4353 if (bus->root_hub) {
4354 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4355 /* Inform the HUB driver to resume */
4356 usb_hcd_resume_root_hub(hcd);
4357 }
4358
4359 spin_unlock_irqrestore(&hsotg->lock, flags);
4360 return 0;
4361}
4362
4363/*
4364 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4365 * stopped.
4366 */
4367static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4368{
4369 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4370 unsigned long flags;
4371
Gregory Herrero5bbf6ce2015-09-22 15:16:48 +02004372 /* Turn off all host-specific interrupts */
4373 dwc2_disable_host_interrupts(hsotg);
4374
Gregory Herrero091473a2015-09-22 15:16:46 +02004375 /* Wait for interrupt processing to finish */
4376 synchronize_irq(hcd->irq);
4377
Paul Zimmerman7359d482013-03-11 17:47:59 -07004378 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero091473a2015-09-22 15:16:46 +02004379 /* Ensure hcd is disconnected */
Douglas Anderson6a659532015-11-19 13:23:14 -08004380 dwc2_hcd_disconnect(hsotg, true);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004381 dwc2_hcd_stop(hsotg);
Gregory Herrero31927b62015-09-22 15:16:41 +02004382 hsotg->lx_state = DWC2_L3;
4383 hcd->state = HC_STATE_HALT;
4384 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004385 spin_unlock_irqrestore(&hsotg->lock, flags);
4386
4387 usleep_range(1000, 3000);
4388}
4389
Gregory Herrero99a65792015-04-29 22:09:13 +02004390static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
4391{
4392 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004393 unsigned long flags;
4394 int ret = 0;
4395 u32 hprt0;
Gregory Herrero99a65792015-04-29 22:09:13 +02004396
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004397 spin_lock_irqsave(&hsotg->lock, flags);
4398
Meng Dongyangf367b722017-08-09 10:34:09 +08004399 if (dwc2_is_device_mode(hsotg))
4400 goto unlock;
4401
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004402 if (hsotg->lx_state != DWC2_L0)
4403 goto unlock;
4404
4405 if (!HCD_HW_ACCESSIBLE(hcd))
4406 goto unlock;
4407
John Stultz866932e2017-01-09 13:10:24 -08004408 if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4409 goto unlock;
4410
John Younbea8e862016-11-03 17:55:53 -07004411 if (!hsotg->params.hibernation)
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004412 goto skip_power_saving;
4413
4414 /*
4415 * Drive USB suspend and disable port Power
4416 * if usb bus is not suspended.
4417 */
4418 if (!hsotg->bus_suspended) {
4419 hprt0 = dwc2_read_hprt0(hsotg);
4420 hprt0 |= HPRT0_SUSP;
4421 hprt0 &= ~HPRT0_PWR;
4422 dwc2_writel(hprt0, hsotg->regs + HPRT0);
4423 }
4424
4425 /* Enter hibernation */
4426 ret = dwc2_enter_hibernation(hsotg);
4427 if (ret) {
4428 if (ret != -ENOTSUPP)
4429 dev_err(hsotg->dev,
4430 "enter hibernation failed\n");
4431 goto skip_power_saving;
4432 }
4433
4434 /* Ask phy to be suspended */
4435 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4436 spin_unlock_irqrestore(&hsotg->lock, flags);
4437 usb_phy_set_suspend(hsotg->uphy, true);
4438 spin_lock_irqsave(&hsotg->lock, flags);
4439 }
4440
4441 /* After entering hibernation, hardware is no more accessible */
4442 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4443
4444skip_power_saving:
Gregory Herrero99a65792015-04-29 22:09:13 +02004445 hsotg->lx_state = DWC2_L2;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004446unlock:
4447 spin_unlock_irqrestore(&hsotg->lock, flags);
4448
4449 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004450}
4451
4452static int _dwc2_hcd_resume(struct usb_hcd *hcd)
4453{
4454 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004455 unsigned long flags;
4456 int ret = 0;
4457
4458 spin_lock_irqsave(&hsotg->lock, flags);
4459
Meng Dongyangf367b722017-08-09 10:34:09 +08004460 if (dwc2_is_device_mode(hsotg))
4461 goto unlock;
4462
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004463 if (hsotg->lx_state != DWC2_L2)
4464 goto unlock;
4465
John Younbea8e862016-11-03 17:55:53 -07004466 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004467 hsotg->lx_state = DWC2_L0;
4468 goto unlock;
4469 }
4470
4471 /*
4472 * Set HW accessible bit before powering on the controller
4473 * since an interrupt may rise.
4474 */
4475 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4476
4477 /*
4478 * Enable power if not already done.
4479 * This must not be spinlocked since duration
4480 * of this call is unknown.
4481 */
4482 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4483 spin_unlock_irqrestore(&hsotg->lock, flags);
4484 usb_phy_set_suspend(hsotg->uphy, false);
4485 spin_lock_irqsave(&hsotg->lock, flags);
4486 }
4487
4488 /* Exit hibernation */
4489 ret = dwc2_exit_hibernation(hsotg, true);
4490 if (ret && (ret != -ENOTSUPP))
4491 dev_err(hsotg->dev, "exit hibernation failed\n");
Gregory Herrero99a65792015-04-29 22:09:13 +02004492
4493 hsotg->lx_state = DWC2_L0;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004494
4495 spin_unlock_irqrestore(&hsotg->lock, flags);
4496
4497 if (hsotg->bus_suspended) {
4498 spin_lock_irqsave(&hsotg->lock, flags);
4499 hsotg->flags.b.port_suspend_change = 1;
4500 spin_unlock_irqrestore(&hsotg->lock, flags);
4501 dwc2_port_resume(hsotg);
4502 } else {
Gregory Herrero5634e012015-09-22 15:16:50 +02004503 /* Wait for controller to correctly update D+/D- level */
4504 usleep_range(3000, 5000);
4505
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004506 /*
4507 * Clear Port Enable and Port Status changes.
4508 * Enable Port Power.
4509 */
4510 dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
4511 HPRT0_ENACHG, hsotg->regs + HPRT0);
4512 /* Wait for controller to detect Port Connect */
Gregory Herrero5634e012015-09-22 15:16:50 +02004513 usleep_range(5000, 7000);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004514 }
4515
4516 return ret;
4517unlock:
4518 spin_unlock_irqrestore(&hsotg->lock, flags);
4519
4520 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004521}
4522
Paul Zimmerman7359d482013-03-11 17:47:59 -07004523/* Returns the current frame number */
4524static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4525{
4526 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4527
4528 return dwc2_hcd_get_frame_number(hsotg);
4529}
4530
4531static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4532 char *fn_name)
4533{
4534#ifdef VERBOSE_DEBUG
4535 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Nicholas Mc Guireefe357f2017-01-12 17:33:26 +01004536 char *pipetype = NULL;
4537 char *speed = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004538
4539 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4540 dev_vdbg(hsotg->dev, " Device address: %d\n",
4541 usb_pipedevice(urb->pipe));
4542 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
4543 usb_pipeendpoint(urb->pipe),
4544 usb_pipein(urb->pipe) ? "IN" : "OUT");
4545
4546 switch (usb_pipetype(urb->pipe)) {
4547 case PIPE_CONTROL:
4548 pipetype = "CONTROL";
4549 break;
4550 case PIPE_BULK:
4551 pipetype = "BULK";
4552 break;
4553 case PIPE_INTERRUPT:
4554 pipetype = "INTERRUPT";
4555 break;
4556 case PIPE_ISOCHRONOUS:
4557 pipetype = "ISOCHRONOUS";
4558 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004559 }
4560
4561 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
4562 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4563 "IN" : "OUT");
4564
4565 switch (urb->dev->speed) {
4566 case USB_SPEED_HIGH:
4567 speed = "HIGH";
4568 break;
4569 case USB_SPEED_FULL:
4570 speed = "FULL";
4571 break;
4572 case USB_SPEED_LOW:
4573 speed = "LOW";
4574 break;
4575 default:
4576 speed = "UNKNOWN";
4577 break;
4578 }
4579
4580 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
4581 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
4582 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
4583 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
4584 urb->transfer_buffer_length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07004585 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
4586 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4587 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
4588 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004589 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
4590
4591 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4592 int i;
4593
4594 for (i = 0; i < urb->number_of_packets; i++) {
4595 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
4596 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
4597 urb->iso_frame_desc[i].offset,
4598 urb->iso_frame_desc[i].length);
4599 }
4600 }
4601#endif
4602}
4603
4604/*
4605 * Starts processing a USB transfer request specified by a USB Request Block
4606 * (URB). mem_flags indicates the type of memory allocation to use while
4607 * processing this URB.
4608 */
4609static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4610 gfp_t mem_flags)
4611{
4612 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4613 struct usb_host_endpoint *ep = urb->ep;
4614 struct dwc2_hcd_urb *dwc2_urb;
4615 int i;
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004616 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004617 int alloc_bandwidth = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004618 u8 ep_type = 0;
4619 u32 tflags = 0;
4620 void *buf;
4621 unsigned long flags;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004622 struct dwc2_qh *qh;
4623 bool qh_allocated = false;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004624 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004625
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004626 if (dbg_urb(urb)) {
4627 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4628 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4629 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004630
John Youn9da51972017-01-17 20:30:27 -08004631 if (!ep)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004632 return -EINVAL;
4633
4634 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4635 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4636 spin_lock_irqsave(&hsotg->lock, flags);
4637 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4638 alloc_bandwidth = 1;
4639 spin_unlock_irqrestore(&hsotg->lock, flags);
4640 }
4641
4642 switch (usb_pipetype(urb->pipe)) {
4643 case PIPE_CONTROL:
4644 ep_type = USB_ENDPOINT_XFER_CONTROL;
4645 break;
4646 case PIPE_ISOCHRONOUS:
4647 ep_type = USB_ENDPOINT_XFER_ISOC;
4648 break;
4649 case PIPE_BULK:
4650 ep_type = USB_ENDPOINT_XFER_BULK;
4651 break;
4652 case PIPE_INTERRUPT:
4653 ep_type = USB_ENDPOINT_XFER_INT;
4654 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004655 }
4656
4657 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4658 mem_flags);
4659 if (!dwc2_urb)
4660 return -ENOMEM;
4661
4662 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4663 usb_pipeendpoint(urb->pipe), ep_type,
4664 usb_pipein(urb->pipe),
4665 usb_maxpacket(urb->dev, urb->pipe,
4666 !(usb_pipein(urb->pipe))));
4667
4668 buf = urb->transfer_buffer;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004669
Paul Zimmerman7359d482013-03-11 17:47:59 -07004670 if (hcd->self.uses_dma) {
Paul Zimmerman25a49442013-07-13 14:53:53 -07004671 if (!buf && (urb->transfer_dma & 3)) {
4672 dev_err(hsotg->dev,
4673 "%s: unaligned transfer with no transfer_buffer",
4674 __func__);
4675 retval = -EINVAL;
Gregory Herrero33ad2612015-04-29 22:09:15 +02004676 goto fail0;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004677 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004678 }
4679
4680 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4681 tflags |= URB_GIVEBACK_ASAP;
4682 if (urb->transfer_flags & URB_ZERO_PACKET)
4683 tflags |= URB_SEND_ZERO_PACKET;
4684
4685 dwc2_urb->priv = urb;
4686 dwc2_urb->buf = buf;
4687 dwc2_urb->dma = urb->transfer_dma;
4688 dwc2_urb->length = urb->transfer_buffer_length;
4689 dwc2_urb->setup_packet = urb->setup_packet;
4690 dwc2_urb->setup_dma = urb->setup_dma;
4691 dwc2_urb->flags = tflags;
4692 dwc2_urb->interval = urb->interval;
4693 dwc2_urb->status = -EINPROGRESS;
4694
4695 for (i = 0; i < urb->number_of_packets; ++i)
4696 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4697 urb->iso_frame_desc[i].offset,
4698 urb->iso_frame_desc[i].length);
4699
4700 urb->hcpriv = dwc2_urb;
John Youn9da51972017-01-17 20:30:27 -08004701 qh = (struct dwc2_qh *)ep->hcpriv;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004702 /* Create QH for the endpoint if it doesn't exist */
4703 if (!qh) {
4704 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4705 if (!qh) {
4706 retval = -ENOMEM;
4707 goto fail0;
4708 }
4709 ep->hcpriv = qh;
4710 qh_allocated = true;
4711 }
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004712
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004713 qtd = kzalloc(sizeof(*qtd), mem_flags);
4714 if (!qtd) {
4715 retval = -ENOMEM;
4716 goto fail1;
4717 }
4718
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004719 spin_lock_irqsave(&hsotg->lock, flags);
4720 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004721 if (retval)
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004722 goto fail2;
4723
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004724 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4725 if (retval)
4726 goto fail3;
4727
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004728 if (alloc_bandwidth) {
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004729 dwc2_allocate_bus_bandwidth(hcd,
4730 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4731 urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004732 }
4733
Gregory Herrero33ad2612015-04-29 22:09:15 +02004734 spin_unlock_irqrestore(&hsotg->lock, flags);
4735
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004736 return 0;
4737
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004738fail3:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004739 dwc2_urb->priv = NULL;
4740 usb_hcd_unlink_urb_from_ep(hcd, urb);
Douglas Anderson16e80212016-01-28 18:19:55 -08004741 if (qh_allocated && qh->channel && qh->channel->qh == qh)
4742 qh->channel->qh = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004743fail2:
Gregory Herrero33ad2612015-04-29 22:09:15 +02004744 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004745 urb->hcpriv = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004746 kfree(qtd);
Vardan Mikayelyanb0d659022016-04-27 20:20:51 -07004747 qtd = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004748fail1:
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004749 if (qh_allocated) {
4750 struct dwc2_qtd *qtd2, *qtd2_tmp;
4751
4752 ep->hcpriv = NULL;
4753 dwc2_hcd_qh_unlink(hsotg, qh);
4754 /* Free each QTD in the QH's QTD list */
4755 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
John Youn9da51972017-01-17 20:30:27 -08004756 qtd_list_entry)
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004757 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4758 dwc2_hcd_qh_free(hsotg, qh);
4759 }
Gregory Herrero33ad2612015-04-29 22:09:15 +02004760fail0:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004761 kfree(dwc2_urb);
4762
Paul Zimmerman7359d482013-03-11 17:47:59 -07004763 return retval;
4764}
4765
4766/*
4767 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4768 */
4769static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4770 int status)
4771{
4772 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004773 int rc;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004774 unsigned long flags;
4775
4776 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4777 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4778
4779 spin_lock_irqsave(&hsotg->lock, flags);
4780
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004781 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4782 if (rc)
4783 goto out;
4784
Paul Zimmerman7359d482013-03-11 17:47:59 -07004785 if (!urb->hcpriv) {
4786 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4787 goto out;
4788 }
4789
4790 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4791
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004792 usb_hcd_unlink_urb_from_ep(hcd, urb);
4793
Paul Zimmerman7359d482013-03-11 17:47:59 -07004794 kfree(urb->hcpriv);
4795 urb->hcpriv = NULL;
4796
4797 /* Higher layer software sets URB status */
4798 spin_unlock(&hsotg->lock);
4799 usb_hcd_giveback_urb(hcd, urb, status);
4800 spin_lock(&hsotg->lock);
4801
4802 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4803 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
4804out:
4805 spin_unlock_irqrestore(&hsotg->lock, flags);
4806
4807 return rc;
4808}
4809
4810/*
4811 * Frees resources in the DWC_otg controller related to a given endpoint. Also
4812 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4813 * must already be dequeued.
4814 */
4815static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4816 struct usb_host_endpoint *ep)
4817{
4818 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4819
4820 dev_dbg(hsotg->dev,
4821 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4822 ep->desc.bEndpointAddress, ep->hcpriv);
4823 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4824}
4825
4826/*
4827 * Resets endpoint specific parameter values, in current version used to reset
4828 * the data toggle (as a WA). This function can be called from usb_clear_halt
4829 * routine.
4830 */
4831static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4832 struct usb_host_endpoint *ep)
4833{
4834 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004835 unsigned long flags;
4836
4837 dev_dbg(hsotg->dev,
4838 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4839 ep->desc.bEndpointAddress);
4840
Paul Zimmerman7359d482013-03-11 17:47:59 -07004841 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004842 dwc2_hcd_endpoint_reset(hsotg, ep);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004843 spin_unlock_irqrestore(&hsotg->lock, flags);
4844}
4845
4846/*
4847 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4848 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4849 * interrupt.
4850 *
4851 * This function is called by the USB core when an interrupt occurs
4852 */
4853static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4854{
4855 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004856
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02004857 return dwc2_handle_hcd_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004858}
4859
4860/*
4861 * Creates Status Change bitmap for the root hub and root port. The bitmap is
4862 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4863 * is the status change indicator for the single root port. Returns 1 if either
4864 * change indicator is 1, otherwise returns 0.
4865 */
4866static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4867{
4868 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4869
4870 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4871 return buf[0] != 0;
4872}
4873
4874/* Handles hub class-specific requests */
4875static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4876 u16 windex, char *buf, u16 wlength)
4877{
4878 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4879 wvalue, windex, buf, wlength);
4880 return retval;
4881}
4882
4883/* Handles hub TT buffer clear completions */
4884static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4885 struct usb_host_endpoint *ep)
4886{
4887 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4888 struct dwc2_qh *qh;
4889 unsigned long flags;
4890
4891 qh = ep->hcpriv;
4892 if (!qh)
4893 return;
4894
4895 spin_lock_irqsave(&hsotg->lock, flags);
4896 qh->tt_buffer_dirty = 0;
4897
4898 if (hsotg->flags.b.port_connect_status)
4899 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4900
4901 spin_unlock_irqrestore(&hsotg->lock, flags);
4902}
4903
Chen Yuca8b0332017-01-23 15:00:18 -08004904/*
4905 * HPRT0_SPD_HIGH_SPEED: high speed
4906 * HPRT0_SPD_FULL_SPEED: full speed
4907 */
4908static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4909{
4910 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4911
4912 if (hsotg->params.speed == speed)
4913 return;
4914
4915 hsotg->params.speed = speed;
4916 queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4917}
4918
4919static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4920{
4921 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4922
4923 if (!hsotg->params.change_speed_quirk)
4924 return;
4925
4926 /*
4927 * On removal, set speed to default high-speed.
4928 */
4929 if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4930 udev->parent->speed < USB_SPEED_HIGH) {
4931 dev_info(hsotg->dev, "Set speed to default high-speed\n");
4932 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4933 }
4934}
4935
4936static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4937{
4938 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4939
4940 if (!hsotg->params.change_speed_quirk)
4941 return 0;
4942
4943 if (udev->speed == USB_SPEED_HIGH) {
4944 dev_info(hsotg->dev, "Set speed to high-speed\n");
4945 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4946 } else if ((udev->speed == USB_SPEED_FULL ||
4947 udev->speed == USB_SPEED_LOW)) {
4948 /*
4949 * Change speed setting to full-speed if there's
4950 * a full-speed or low-speed device plugged in.
4951 */
4952 dev_info(hsotg->dev, "Set speed to full-speed\n");
4953 dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4954 }
4955
4956 return 0;
4957}
4958
Paul Zimmerman7359d482013-03-11 17:47:59 -07004959static struct hc_driver dwc2_hc_driver = {
4960 .description = "dwc2_hsotg",
4961 .product_desc = "DWC OTG Controller",
4962 .hcd_priv_size = sizeof(struct wrapper_priv_data),
4963
4964 .irq = _dwc2_hcd_irq,
Douglas Anderson8add17c2016-01-28 18:20:00 -08004965 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004966
4967 .start = _dwc2_hcd_start,
4968 .stop = _dwc2_hcd_stop,
4969 .urb_enqueue = _dwc2_hcd_urb_enqueue,
4970 .urb_dequeue = _dwc2_hcd_urb_dequeue,
4971 .endpoint_disable = _dwc2_hcd_endpoint_disable,
4972 .endpoint_reset = _dwc2_hcd_endpoint_reset,
4973 .get_frame_number = _dwc2_hcd_get_frame_number,
4974
4975 .hub_status_data = _dwc2_hcd_hub_status_data,
4976 .hub_control = _dwc2_hcd_hub_control,
4977 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
Gregory Herrero99a65792015-04-29 22:09:13 +02004978
4979 .bus_suspend = _dwc2_hcd_suspend,
4980 .bus_resume = _dwc2_hcd_resume,
Douglas Anderson3bc04e22016-01-28 18:19:53 -08004981
4982 .map_urb_for_dma = dwc2_map_urb_for_dma,
4983 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004984};
4985
4986/*
4987 * Frees secondary storage associated with the dwc2_hsotg structure contained
4988 * in the struct usb_hcd field
4989 */
4990static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
4991{
4992 u32 ahbcfg;
4993 u32 dctl;
4994 int i;
4995
4996 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
4997
4998 /* Free memory for QH/QTD lists */
4999 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
5000 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
5001 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
5002 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
5003 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
5004 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
5005
5006 /* Free memory for the host channels */
5007 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
5008 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
5009
John Youn9da51972017-01-17 20:30:27 -08005010 if (chan) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07005011 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
5012 i, chan);
5013 hsotg->hc_ptr_array[i] = NULL;
5014 kfree(chan);
5015 }
5016 }
5017
John Youn95832c02017-01-23 14:57:26 -08005018 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07005019 if (hsotg->status_buf) {
5020 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
5021 hsotg->status_buf,
5022 hsotg->status_buf_dma);
5023 hsotg->status_buf = NULL;
5024 }
5025 } else {
5026 kfree(hsotg->status_buf);
5027 hsotg->status_buf = NULL;
5028 }
5029
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005030 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005031
5032 /* Disable all interrupts */
5033 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005034 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
5035 dwc2_writel(0, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005036
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005037 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005038 dctl = dwc2_readl(hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005039 dctl |= DCTL_SFTDISCON;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005040 dwc2_writel(dctl, hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005041 }
5042
5043 if (hsotg->wq_otg) {
5044 if (!cancel_work_sync(&hsotg->wf_otg))
5045 flush_workqueue(hsotg->wq_otg);
5046 destroy_workqueue(hsotg->wq_otg);
5047 }
5048
Paul Zimmerman7359d482013-03-11 17:47:59 -07005049 del_timer(&hsotg->wkp_timer);
5050}
5051
5052static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5053{
5054 /* Turn off all host-specific interrupts */
5055 dwc2_disable_host_interrupts(hsotg);
5056
5057 dwc2_hcd_free(hsotg);
5058}
5059
Matthijs Kooijman8284f932013-04-11 18:43:47 +02005060/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07005061 * Initializes the HCD. This function allocates memory for and initializes the
5062 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5063 * USB bus with the core and calls the hc_driver->start() function. It returns
5064 * a negative error on failure.
5065 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005066int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005067{
Heiner Kallweit348becd2017-01-25 23:10:51 +01005068 struct platform_device *pdev = to_platform_device(hsotg->dev);
5069 struct resource *res;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005070 struct usb_hcd *hcd;
5071 struct dwc2_host_chan *channel;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005072 u32 hcfg;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005073 int i, num_channels;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005074 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005075
Dinh Nguyenf5500ec2014-11-11 11:13:39 -06005076 if (usb_disabled())
5077 return -ENODEV;
5078
Paul Zimmermane62662c2013-03-25 17:03:35 -07005079 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005080
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005081 retval = -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005082
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005083 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005084 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005085
5086#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5087 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
5088 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5089 if (!hsotg->frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005090 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005091 hsotg->last_frame_num_array = kzalloc(
5092 sizeof(*hsotg->last_frame_num_array) *
5093 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5094 if (!hsotg->last_frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005095 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005096#endif
Douglas Anderson483bb252016-01-28 18:20:07 -08005097 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005098
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005099 /* Check if the bus driver or platform code has setup a dma_mask */
John Youn95832c02017-01-23 14:57:26 -08005100 if (hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08005101 !hsotg->dev->dma_mask) {
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005102 dev_warn(hsotg->dev,
5103 "dma_mask not set, disabling DMA\n");
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01005104 hsotg->params.host_dma = false;
John Youn95832c02017-01-23 14:57:26 -08005105 hsotg->params.dma_desc_enable = false;
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005106 }
5107
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005108 /* Set device flags indicating whether the HCD supports DMA */
John Youn95832c02017-01-23 14:57:26 -08005109 if (hsotg->params.host_dma) {
Paul Zimmerman30885312013-05-24 16:27:56 -07005110 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5111 dev_warn(hsotg->dev, "can't set DMA mask\n");
Paul Zimmerman25a49442013-07-13 14:53:53 -07005112 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5113 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005114 }
5115
Chen Yuca8b0332017-01-23 15:00:18 -08005116 if (hsotg->params.change_speed_quirk) {
5117 dwc2_hc_driver.free_dev = dwc2_free_dev;
5118 dwc2_hc_driver.reset_device = dwc2_reset_device;
5119 }
5120
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005121 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5122 if (!hcd)
5123 goto error1;
5124
John Youn95832c02017-01-23 14:57:26 -08005125 if (!hsotg->params.host_dma)
Matthijs Kooijman7de76ee2013-07-19 11:34:23 +02005126 hcd->self.uses_dma = 0;
5127
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005128 hcd->has_tt = 1;
5129
Heiner Kallweit348becd2017-01-25 23:10:51 +01005130 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5131 hcd->rsrc_start = res->start;
5132 hcd->rsrc_len = resource_size(res);
5133
John Youn9da51972017-01-17 20:30:27 -08005134 ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005135 hsotg->priv = hcd;
5136
Paul Zimmerman7359d482013-03-11 17:47:59 -07005137 /*
5138 * Disable the global interrupt until all the interrupt handlers are
5139 * installed
5140 */
5141 dwc2_disable_global_interrupts(hsotg);
5142
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005143 /* Initialize the DWC_otg core, and select the Phy type */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08005144 retval = dwc2_core_init(hsotg, true);
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005145 if (retval)
5146 goto error2;
5147
Paul Zimmerman7359d482013-03-11 17:47:59 -07005148 /* Create new workqueue and init work */
Wei Yongjun53510352013-04-12 22:41:48 +08005149 retval = -ENOMEM;
Bhaktipriya Shridharec7b1262016-07-28 13:57:29 +05305150 hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005151 if (!hsotg->wq_otg) {
5152 dev_err(hsotg->dev, "Failed to create workqueue\n");
5153 goto error2;
5154 }
5155 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5156
Paul Zimmerman7359d482013-03-11 17:47:59 -07005157 setup_timer(&hsotg->wkp_timer, dwc2_wakeup_detected,
5158 (unsigned long)hsotg);
5159
5160 /* Initialize the non-periodic schedule */
5161 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
5162 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5163
5164 /* Initialize the periodic schedule */
5165 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5166 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5167 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5168 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5169
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005170 INIT_LIST_HEAD(&hsotg->split_order);
5171
Paul Zimmerman7359d482013-03-11 17:47:59 -07005172 /*
5173 * Create a host channel descriptor for each host channel implemented
5174 * in the controller. Initialize the channel descriptor array.
5175 */
5176 INIT_LIST_HEAD(&hsotg->free_hc_list);
John Younbea8e862016-11-03 17:55:53 -07005177 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005178 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5179
5180 for (i = 0; i < num_channels; i++) {
5181 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
John Youn9da51972017-01-17 20:30:27 -08005182 if (!channel)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005183 goto error3;
5184 channel->hc_num = i;
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005185 INIT_LIST_HEAD(&channel->split_order_list_entry);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005186 hsotg->hc_ptr_array[i] = channel;
5187 }
5188
5189 /* Initialize hsotg start work */
5190 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5191
5192 /* Initialize port reset work */
5193 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5194
5195 /*
5196 * Allocate space for storing data on status transactions. Normally no
5197 * data is sent, but this space acts as a bit bucket. This must be
5198 * done after usb_add_hcd since that function allocates the DMA buffer
5199 * pool.
5200 */
John Youn95832c02017-01-23 14:57:26 -08005201 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005202 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5203 DWC2_HCD_STATUS_BUF_SIZE,
5204 &hsotg->status_buf_dma, GFP_KERNEL);
5205 else
5206 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5207 GFP_KERNEL);
5208
5209 if (!hsotg->status_buf)
5210 goto error3;
5211
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005212 /*
5213 * Create kmem caches to handle descriptor buffers in descriptor
5214 * DMA mode.
5215 * Alignment must be set to 512 bytes.
5216 */
John Younbea8e862016-11-03 17:55:53 -07005217 if (hsotg->params.dma_desc_enable ||
5218 hsotg->params.dma_desc_fs_enable) {
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005219 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005220 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005221 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
5222 NULL);
5223 if (!hsotg->desc_gen_cache) {
5224 dev_err(hsotg->dev,
5225 "unable to create dwc2 generic desc cache\n");
5226
5227 /*
5228 * Disable descriptor dma mode since it will not be
5229 * usable.
5230 */
John Youn95832c02017-01-23 14:57:26 -08005231 hsotg->params.dma_desc_enable = false;
5232 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005233 }
5234
5235 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005236 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005237 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
5238 if (!hsotg->desc_hsisoc_cache) {
5239 dev_err(hsotg->dev,
5240 "unable to create dwc2 hs isoc desc cache\n");
5241
5242 kmem_cache_destroy(hsotg->desc_gen_cache);
5243
5244 /*
5245 * Disable descriptor dma mode since it will not be
5246 * usable.
5247 */
John Youn95832c02017-01-23 14:57:26 -08005248 hsotg->params.dma_desc_enable = false;
5249 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005250 }
5251 }
5252
Paul Zimmerman7359d482013-03-11 17:47:59 -07005253 hsotg->otg_port = 1;
5254 hsotg->frame_list = NULL;
5255 hsotg->frame_list_dma = 0;
5256 hsotg->periodic_qh_count = 0;
5257
5258 /* Initiate lx_state to L3 disconnected state */
5259 hsotg->lx_state = DWC2_L3;
5260
5261 hcd->self.otg_port = hsotg->otg_port;
5262
5263 /* Don't support SG list at this point */
5264 hcd->self.sg_tablesize = 0;
5265
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005266 if (!IS_ERR_OR_NULL(hsotg->uphy))
5267 otg_set_host(hsotg->uphy->otg, &hcd->self);
5268
Paul Zimmerman7359d482013-03-11 17:47:59 -07005269 /*
5270 * Finish generic HCD initialization and start the HCD. This function
5271 * allocates the DMA buffer pool, registers the USB bus, requests the
5272 * IRQ line, and calls hcd_start method.
5273 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005274 retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005275 if (retval < 0)
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005276 goto error4;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005277
Peter Chen3c9740a2013-11-05 10:46:02 +08005278 device_wakeup_enable(hcd->self.controller);
5279
Paul Zimmerman7359d482013-03-11 17:47:59 -07005280 dwc2_hcd_dump_state(hsotg);
5281
5282 dwc2_enable_global_interrupts(hsotg);
5283
5284 return 0;
5285
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005286error4:
5287 kmem_cache_destroy(hsotg->desc_gen_cache);
5288 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005289error3:
5290 dwc2_hcd_release(hsotg);
5291error2:
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005292 usb_put_hcd(hcd);
5293error1:
Paul Zimmerman7359d482013-03-11 17:47:59 -07005294
5295#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5296 kfree(hsotg->last_frame_num_array);
5297 kfree(hsotg->frame_num_array);
5298#endif
5299
Paul Zimmermane62662c2013-03-25 17:03:35 -07005300 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005301 return retval;
5302}
Paul Zimmerman7359d482013-03-11 17:47:59 -07005303
5304/*
5305 * Removes the HCD.
5306 * Frees memory and resources associated with the HCD and deregisters the bus.
5307 */
Paul Zimmermane62662c2013-03-25 17:03:35 -07005308void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005309{
5310 struct usb_hcd *hcd;
5311
Paul Zimmermane62662c2013-03-25 17:03:35 -07005312 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005313
5314 hcd = dwc2_hsotg_to_hcd(hsotg);
Paul Zimmermane62662c2013-03-25 17:03:35 -07005315 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005316
5317 if (!hcd) {
Paul Zimmermane62662c2013-03-25 17:03:35 -07005318 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
Paul Zimmerman7359d482013-03-11 17:47:59 -07005319 __func__);
5320 return;
5321 }
5322
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005323 if (!IS_ERR_OR_NULL(hsotg->uphy))
5324 otg_set_host(hsotg->uphy->otg, NULL);
5325
Paul Zimmerman7359d482013-03-11 17:47:59 -07005326 usb_remove_hcd(hcd);
5327 hsotg->priv = NULL;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005328
5329 kmem_cache_destroy(hsotg->desc_gen_cache);
5330 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5331
Paul Zimmerman7359d482013-03-11 17:47:59 -07005332 dwc2_hcd_release(hsotg);
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005333 usb_put_hcd(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005334
5335#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5336 kfree(hsotg->last_frame_num_array);
5337 kfree(hsotg->frame_num_array);
5338#endif
Paul Zimmerman7359d482013-03-11 17:47:59 -07005339}
John Youn58e52ff6a2016-02-23 19:54:57 -08005340
5341/**
5342 * dwc2_backup_host_registers() - Backup controller host registers.
5343 * When suspending usb bus, registers needs to be backuped
5344 * if controller power is disabled once suspended.
5345 *
5346 * @hsotg: Programming view of the DWC_otg controller
5347 */
5348int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
5349{
5350 struct dwc2_hregs_backup *hr;
5351 int i;
5352
5353 dev_dbg(hsotg->dev, "%s\n", __func__);
5354
5355 /* Backup Host regs */
5356 hr = &hsotg->hr_backup;
5357 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
5358 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
John Younbea8e862016-11-03 17:55:53 -07005359 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005360 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
5361
5362 hr->hprt0 = dwc2_read_hprt0(hsotg);
5363 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
5364 hr->valid = true;
5365
5366 return 0;
5367}
5368
5369/**
5370 * dwc2_restore_host_registers() - Restore controller host registers.
5371 * When resuming usb bus, device registers needs to be restored
5372 * if controller power were disabled.
5373 *
5374 * @hsotg: Programming view of the DWC_otg controller
5375 */
5376int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
5377{
5378 struct dwc2_hregs_backup *hr;
5379 int i;
5380
5381 dev_dbg(hsotg->dev, "%s\n", __func__);
5382
5383 /* Restore host regs */
5384 hr = &hsotg->hr_backup;
5385 if (!hr->valid) {
5386 dev_err(hsotg->dev, "%s: no host registers to restore\n",
5387 __func__);
5388 return -EINVAL;
5389 }
5390 hr->valid = false;
5391
5392 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5393 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
5394
John Younbea8e862016-11-03 17:55:53 -07005395 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005396 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
5397
5398 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
5399 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
5400 hsotg->frame_number = 0;
5401
5402 return 0;
5403}