]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - drivers/usb/dwc2/hcd.c
usb: dwc2: hcd: Fix host channel halt flow
[mirror_ubuntu-bionic-kernel.git] / drivers / usb / dwc2 / hcd.c
CommitLineData
5fd54ace 1// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
7359d482
PZ
2/*
3 * hcd.c - DesignWare HS OTG Controller host-mode routines
4 *
5 * Copyright (C) 2004-2013 Synopsys, Inc.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions, and the following disclaimer,
12 * without modification.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The names of the above-listed copyright holders may not be used
17 * to endorse or promote products derived from this software without
18 * specific prior written permission.
19 *
20 * ALTERNATIVELY, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") as published by the Free Software
22 * Foundation; either version 2 of the License, or (at your option) any
23 * later version.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
26 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
27 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
28 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
29 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
30 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
31 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
32 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38/*
39 * This file contains the core HCD code, and implements the Linux hc_driver
40 * API
41 */
42#include <linux/kernel.h>
43#include <linux/module.h>
44#include <linux/spinlock.h>
45#include <linux/interrupt.h>
348becdc 46#include <linux/platform_device.h>
7359d482
PZ
47#include <linux/dma-mapping.h>
48#include <linux/delay.h>
49#include <linux/io.h>
50#include <linux/slab.h>
51#include <linux/usb.h>
52
53#include <linux/usb/hcd.h>
54#include <linux/usb/ch11.h>
55
56#include "core.h"
57#include "hcd.h"
58
9156a7ef
CY
59static void dwc2_port_resume(struct dwc2_hsotg *hsotg);
60
b02038fa
JY
61/*
62 * =========================================================================
63 * Host Core Layer Functions
64 * =========================================================================
65 */
66
67/**
68 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
69 * used in both device and host modes
70 *
71 * @hsotg: Programming view of the DWC_otg controller
72 */
73static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
74{
75 u32 intmsk;
76
77 /* Clear any pending OTG Interrupts */
78 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
79
80 /* Clear any pending interrupts */
81 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
82
83 /* Enable the interrupts in the GINTMSK */
84 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
85
95832c00 86 if (!hsotg->params.host_dma)
b02038fa 87 intmsk |= GINTSTS_RXFLVL;
95832c00 88 if (!hsotg->params.external_id_pin_ctl)
b02038fa
JY
89 intmsk |= GINTSTS_CONIDSTSCHNG;
90
91 intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
92 GINTSTS_SESSREQINT;
93
94 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
95}
96
97/*
98 * Initializes the FSLSPClkSel field of the HCFG register depending on the
99 * PHY type
100 */
101static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
102{
103 u32 hcfg, val;
104
105 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
106 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
95832c00 107 hsotg->params.ulpi_fs_ls) ||
bea8e86c 108 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
b02038fa
JY
109 /* Full speed PHY */
110 val = HCFG_FSLSPCLKSEL_48_MHZ;
111 } else {
112 /* High speed PHY running at full speed or high speed */
113 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
114 }
115
116 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
117 hcfg = dwc2_readl(hsotg->regs + HCFG);
118 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
119 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
120 dwc2_writel(hcfg, hsotg->regs + HCFG);
121}
122
123static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
124{
e35b1350 125 u32 usbcfg, ggpio, i2cctl;
b02038fa
JY
126 int retval = 0;
127
128 /*
129 * core_init() is now called on every switch so only call the
130 * following for the first time through
131 */
132 if (select_phy) {
133 dev_dbg(hsotg->dev, "FS PHY selected\n");
134
135 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
136 if (!(usbcfg & GUSBCFG_PHYSEL)) {
137 usbcfg |= GUSBCFG_PHYSEL;
138 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
139
140 /* Reset after a PHY select */
141 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
142
143 if (retval) {
144 dev_err(hsotg->dev,
145 "%s: Reset failed, aborting", __func__);
146 return retval;
147 }
148 }
e35b1350
BH
149
150 if (hsotg->params.activate_stm_fs_transceiver) {
151 ggpio = dwc2_readl(hsotg->regs + GGPIO);
152 if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
153 dev_dbg(hsotg->dev, "Activating transceiver\n");
154 /*
155 * STM32F4x9 uses the GGPIO register as general
156 * core configuration register.
157 */
158 ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
159 dwc2_writel(ggpio, hsotg->regs + GGPIO);
160 }
161 }
b02038fa
JY
162 }
163
164 /*
165 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
166 * do this on HNP Dev/Host mode switches (done in dev_init and
167 * host_init).
168 */
169 if (dwc2_is_host_mode(hsotg))
170 dwc2_init_fs_ls_pclk_sel(hsotg);
171
95832c00 172 if (hsotg->params.i2c_enable) {
b02038fa
JY
173 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
174
175 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
176 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
177 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
178 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
179
180 /* Program GI2CCTL.I2CEn */
181 i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
182 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
183 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
184 i2cctl &= ~GI2CCTL_I2CEN;
185 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
186 i2cctl |= GI2CCTL_I2CEN;
187 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
188 }
189
190 return retval;
191}
192
193static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
194{
195 u32 usbcfg, usbcfg_old;
196 int retval = 0;
197
198 if (!select_phy)
199 return 0;
200
201 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
202 usbcfg_old = usbcfg;
203
204 /*
205 * HS PHY parameters. These parameters are preserved during soft reset
206 * so only program the first time. Do a soft reset immediately after
207 * setting phyif.
208 */
bea8e86c 209 switch (hsotg->params.phy_type) {
b02038fa
JY
210 case DWC2_PHY_TYPE_PARAM_ULPI:
211 /* ULPI interface */
212 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
213 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
214 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
95832c00 215 if (hsotg->params.phy_ulpi_ddr)
b02038fa 216 usbcfg |= GUSBCFG_DDRSEL;
b11633c4
DN
217
218 /* Set external VBUS indicator as needed. */
219 if (hsotg->params.oc_disable)
220 usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
221 GUSBCFG_INDICATORPASSTHROUGH);
b02038fa
JY
222 break;
223 case DWC2_PHY_TYPE_PARAM_UTMI:
224 /* UTMI+ interface */
225 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
226 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
bea8e86c 227 if (hsotg->params.phy_utmi_width == 16)
b02038fa
JY
228 usbcfg |= GUSBCFG_PHYIF16;
229 break;
230 default:
231 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
232 break;
233 }
234
235 if (usbcfg != usbcfg_old) {
236 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
237
238 /* Reset after setting the PHY parameters */
239 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
240 if (retval) {
241 dev_err(hsotg->dev,
242 "%s: Reset failed, aborting", __func__);
243 return retval;
244 }
245 }
246
247 return retval;
248}
249
250static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
251{
252 u32 usbcfg;
253 int retval = 0;
254
38e9002b
VM
255 if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
256 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
bea8e86c 257 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
38e9002b 258 /* If FS/LS mode with FS/LS PHY */
b02038fa
JY
259 retval = dwc2_fs_phy_init(hsotg, select_phy);
260 if (retval)
261 return retval;
262 } else {
263 /* High speed PHY */
264 retval = dwc2_hs_phy_init(hsotg, select_phy);
265 if (retval)
266 return retval;
267 }
268
269 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
270 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
95832c00 271 hsotg->params.ulpi_fs_ls) {
b02038fa
JY
272 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
273 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
274 usbcfg |= GUSBCFG_ULPI_FS_LS;
275 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
276 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
277 } else {
278 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
279 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
280 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
281 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
282 }
283
284 return retval;
285}
286
287static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
288{
289 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
290
291 switch (hsotg->hw_params.arch) {
292 case GHWCFG2_EXT_DMA_ARCH:
293 dev_err(hsotg->dev, "External DMA Mode not supported\n");
294 return -EINVAL;
295
296 case GHWCFG2_INT_DMA_ARCH:
297 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
bea8e86c 298 if (hsotg->params.ahbcfg != -1) {
b02038fa 299 ahbcfg &= GAHBCFG_CTRL_MASK;
bea8e86c 300 ahbcfg |= hsotg->params.ahbcfg &
b02038fa
JY
301 ~GAHBCFG_CTRL_MASK;
302 }
303 break;
304
305 case GHWCFG2_SLAVE_ONLY_ARCH:
306 default:
307 dev_dbg(hsotg->dev, "Slave Only Mode\n");
308 break;
309 }
310
e7839f99
JY
311 dev_dbg(hsotg->dev, "host_dma:%d dma_desc_enable:%d\n",
312 hsotg->params.host_dma,
bea8e86c 313 hsotg->params.dma_desc_enable);
b02038fa 314
95832c00
JY
315 if (hsotg->params.host_dma) {
316 if (hsotg->params.dma_desc_enable)
b02038fa
JY
317 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
318 else
319 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
320 } else {
321 dev_dbg(hsotg->dev, "Using Slave mode\n");
95832c00 322 hsotg->params.dma_desc_enable = false;
b02038fa
JY
323 }
324
95832c00 325 if (hsotg->params.host_dma)
b02038fa
JY
326 ahbcfg |= GAHBCFG_DMA_EN;
327
328 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
329
330 return 0;
331}
332
333static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
334{
335 u32 usbcfg;
336
337 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
338 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
339
340 switch (hsotg->hw_params.op_mode) {
341 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
bea8e86c 342 if (hsotg->params.otg_cap ==
b02038fa
JY
343 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
344 usbcfg |= GUSBCFG_HNPCAP;
bea8e86c 345 if (hsotg->params.otg_cap !=
b02038fa
JY
346 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
347 usbcfg |= GUSBCFG_SRPCAP;
348 break;
349
350 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
351 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
352 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
bea8e86c 353 if (hsotg->params.otg_cap !=
b02038fa
JY
354 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
355 usbcfg |= GUSBCFG_SRPCAP;
356 break;
357
358 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
359 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
360 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
361 default:
362 break;
363 }
364
365 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
366}
367
368/**
369 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
370 *
371 * @hsotg: Programming view of DWC_otg controller
372 */
373static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
374{
375 u32 intmsk;
376
377 dev_dbg(hsotg->dev, "%s()\n", __func__);
378
379 /* Disable all interrupts */
380 dwc2_writel(0, hsotg->regs + GINTMSK);
381 dwc2_writel(0, hsotg->regs + HAINTMSK);
382
383 /* Enable the common interrupts */
384 dwc2_enable_common_interrupts(hsotg);
385
386 /* Enable host mode interrupts without disturbing common interrupts */
387 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
388 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
389 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
390}
391
392/**
393 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
394 *
395 * @hsotg: Programming view of DWC_otg controller
396 */
397static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
398{
399 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
400
401 /* Disable host mode interrupts without disturbing common interrupts */
402 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
403 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
404 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
405}
406
407/*
408 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
409 * For system that have a total fifo depth that is smaller than the default
410 * RX + TX fifo size.
411 *
412 * @hsotg: Programming view of DWC_otg controller
413 */
414static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
415{
bea8e86c 416 struct dwc2_core_params *params = &hsotg->params;
b02038fa
JY
417 struct dwc2_hw_params *hw = &hsotg->hw_params;
418 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
419
420 total_fifo_size = hw->total_fifo_size;
421 rxfsiz = params->host_rx_fifo_size;
422 nptxfsiz = params->host_nperio_tx_fifo_size;
423 ptxfsiz = params->host_perio_tx_fifo_size;
424
425 /*
426 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
427 * allocation with support for high bandwidth endpoints. Synopsys
428 * defines MPS(Max Packet size) for a periodic EP=1024, and for
429 * non-periodic as 512.
430 */
431 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
432 /*
433 * For Buffer DMA mode/Scatter Gather DMA mode
434 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
435 * with n = number of host channel.
436 * 2 * ((1024/4) + 2) = 516
437 */
438 rxfsiz = 516 + hw->host_channels;
439
440 /*
441 * min non-periodic tx fifo depth
442 * 2 * (largest non-periodic USB packet used / 4)
443 * 2 * (512/4) = 256
444 */
445 nptxfsiz = 256;
446
447 /*
448 * min periodic tx fifo depth
449 * (largest packet size*MC)/4
450 * (1024 * 3)/4 = 768
451 */
452 ptxfsiz = 768;
453
454 params->host_rx_fifo_size = rxfsiz;
455 params->host_nperio_tx_fifo_size = nptxfsiz;
456 params->host_perio_tx_fifo_size = ptxfsiz;
457 }
458
459 /*
460 * If the summation of RX, NPTX and PTX fifo sizes is still
461 * bigger than the total_fifo_size, then we have a problem.
462 *
463 * We won't be able to allocate as many endpoints. Right now,
464 * we're just printing an error message, but ideally this FIFO
465 * allocation algorithm would be improved in the future.
466 *
467 * FIXME improve this FIFO allocation algorithm.
468 */
469 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
470 dev_err(hsotg->dev, "invalid fifo sizes\n");
471}
472
473static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
474{
bea8e86c 475 struct dwc2_core_params *params = &hsotg->params;
b02038fa
JY
476 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
477
478 if (!params->enable_dynamic_fifo)
479 return;
480
481 dwc2_calculate_dynamic_fifo(hsotg);
482
483 /* Rx FIFO */
484 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
485 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
486 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
487 grxfsiz |= params->host_rx_fifo_size <<
488 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
489 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
490 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
491 dwc2_readl(hsotg->regs + GRXFSIZ));
492
493 /* Non-periodic Tx FIFO */
494 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
495 dwc2_readl(hsotg->regs + GNPTXFSIZ));
496 nptxfsiz = params->host_nperio_tx_fifo_size <<
497 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
498 nptxfsiz |= params->host_rx_fifo_size <<
499 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
500 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
501 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
502 dwc2_readl(hsotg->regs + GNPTXFSIZ));
503
504 /* Periodic Tx FIFO */
505 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
506 dwc2_readl(hsotg->regs + HPTXFSIZ));
507 hptxfsiz = params->host_perio_tx_fifo_size <<
508 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
509 hptxfsiz |= (params->host_rx_fifo_size +
510 params->host_nperio_tx_fifo_size) <<
511 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
512 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
513 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
514 dwc2_readl(hsotg->regs + HPTXFSIZ));
515
95832c00 516 if (hsotg->params.en_multiple_tx_fifo &&
e1f411d1 517 hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
b02038fa 518 /*
e1f411d1 519 * This feature was implemented in 2.91a version
b02038fa
JY
520 * Global DFIFOCFG calculation for Host mode -
521 * include RxFIFO, NPTXFIFO and HPTXFIFO
522 */
523 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
524 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
525 dfifocfg |= (params->host_rx_fifo_size +
526 params->host_nperio_tx_fifo_size +
527 params->host_perio_tx_fifo_size) <<
528 GDFIFOCFG_EPINFOBASE_SHIFT &
529 GDFIFOCFG_EPINFOBASE_MASK;
530 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
531 }
532}
533
534/**
535 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
536 * the HFIR register according to PHY type and speed
537 *
538 * @hsotg: Programming view of DWC_otg controller
539 *
540 * NOTE: The caller can modify the value of the HFIR register only after the
541 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
542 * has been set
543 */
544u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
545{
546 u32 usbcfg;
547 u32 hprt0;
548 int clock = 60; /* default value */
549
550 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
551 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
552
553 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
554 !(usbcfg & GUSBCFG_PHYIF16))
555 clock = 60;
556 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
557 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
558 clock = 48;
559 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
560 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
561 clock = 30;
562 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
563 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
564 clock = 60;
565 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
566 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
567 clock = 48;
568 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
569 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
570 clock = 48;
571 if ((usbcfg & GUSBCFG_PHYSEL) &&
572 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
573 clock = 48;
574
575 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
576 /* High speed case */
577 return 125 * clock - 1;
578
579 /* FS/LS case */
580 return 1000 * clock - 1;
581}
582
583/**
584 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
585 * buffer
586 *
587 * @core_if: Programming view of DWC_otg controller
588 * @dest: Destination buffer for the packet
589 * @bytes: Number of bytes to copy to the destination
590 */
591void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
592{
593 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
594 u32 *data_buf = (u32 *)dest;
595 int word_count = (bytes + 3) / 4;
596 int i;
597
598 /*
599 * Todo: Account for the case where dest is not dword aligned. This
600 * requires reading data from the FIFO into a u32 temp buffer, then
601 * moving it into the data buffer.
602 */
603
604 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
605
606 for (i = 0; i < word_count; i++, data_buf++)
607 *data_buf = dwc2_readl(fifo);
608}
609
610/**
611 * dwc2_dump_channel_info() - Prints the state of a host channel
612 *
613 * @hsotg: Programming view of DWC_otg controller
614 * @chan: Pointer to the channel to dump
615 *
616 * Must be called with interrupt disabled and spinlock held
617 *
618 * NOTE: This function will be removed once the peripheral controller code
619 * is integrated and the driver is stable
620 */
621static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
622 struct dwc2_host_chan *chan)
623{
624#ifdef VERBOSE_DEBUG
bea8e86c 625 int num_channels = hsotg->params.host_channels;
b02038fa
JY
626 struct dwc2_qh *qh;
627 u32 hcchar;
628 u32 hcsplt;
629 u32 hctsiz;
630 u32 hc_dma;
631 int i;
632
633 if (!chan)
634 return;
635
636 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
637 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
638 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chan->hc_num));
639 hc_dma = dwc2_readl(hsotg->regs + HCDMA(chan->hc_num));
640
641 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
642 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
643 hcchar, hcsplt);
644 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
645 hctsiz, hc_dma);
646 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
647 chan->dev_addr, chan->ep_num, chan->ep_is_in);
648 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
649 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
650 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
651 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
652 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
653 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
654 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
655 (unsigned long)chan->xfer_dma);
656 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
657 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
658 dev_dbg(hsotg->dev, " NP inactive sched:\n");
659 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
660 qh_list_entry)
661 dev_dbg(hsotg->dev, " %p\n", qh);
662 dev_dbg(hsotg->dev, " NP active sched:\n");
663 list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
664 qh_list_entry)
665 dev_dbg(hsotg->dev, " %p\n", qh);
666 dev_dbg(hsotg->dev, " Channels:\n");
667 for (i = 0; i < num_channels; i++) {
668 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
669
670 dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
671 }
672#endif /* VERBOSE_DEBUG */
673}
674
4411beba
RK
675static int _dwc2_hcd_start(struct usb_hcd *hcd);
676
677static void dwc2_host_start(struct dwc2_hsotg *hsotg)
678{
679 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
680
681 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
682 _dwc2_hcd_start(hcd);
683}
684
685static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
686{
687 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
688
689 hcd->self.is_b_host = 0;
690}
691
692static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
693 int *hub_addr, int *hub_port)
694{
695 struct urb *urb = context;
696
697 if (urb->dev->tt)
698 *hub_addr = urb->dev->tt->hub->devnum;
699 else
700 *hub_addr = 0;
701 *hub_port = urb->dev->ttport;
702}
703
b02038fa
JY
704/*
705 * =========================================================================
706 * Low Level Host Channel Access Functions
707 * =========================================================================
708 */
709
710static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
711 struct dwc2_host_chan *chan)
712{
713 u32 hcintmsk = HCINTMSK_CHHLTD;
714
715 switch (chan->ep_type) {
716 case USB_ENDPOINT_XFER_CONTROL:
717 case USB_ENDPOINT_XFER_BULK:
718 dev_vdbg(hsotg->dev, "control/bulk\n");
719 hcintmsk |= HCINTMSK_XFERCOMPL;
720 hcintmsk |= HCINTMSK_STALL;
721 hcintmsk |= HCINTMSK_XACTERR;
722 hcintmsk |= HCINTMSK_DATATGLERR;
723 if (chan->ep_is_in) {
724 hcintmsk |= HCINTMSK_BBLERR;
725 } else {
726 hcintmsk |= HCINTMSK_NAK;
727 hcintmsk |= HCINTMSK_NYET;
728 if (chan->do_ping)
729 hcintmsk |= HCINTMSK_ACK;
730 }
731
732 if (chan->do_split) {
733 hcintmsk |= HCINTMSK_NAK;
734 if (chan->complete_split)
735 hcintmsk |= HCINTMSK_NYET;
736 else
737 hcintmsk |= HCINTMSK_ACK;
738 }
739
740 if (chan->error_state)
741 hcintmsk |= HCINTMSK_ACK;
742 break;
743
744 case USB_ENDPOINT_XFER_INT:
745 if (dbg_perio())
746 dev_vdbg(hsotg->dev, "intr\n");
747 hcintmsk |= HCINTMSK_XFERCOMPL;
748 hcintmsk |= HCINTMSK_NAK;
749 hcintmsk |= HCINTMSK_STALL;
750 hcintmsk |= HCINTMSK_XACTERR;
751 hcintmsk |= HCINTMSK_DATATGLERR;
752 hcintmsk |= HCINTMSK_FRMOVRUN;
753
754 if (chan->ep_is_in)
755 hcintmsk |= HCINTMSK_BBLERR;
756 if (chan->error_state)
757 hcintmsk |= HCINTMSK_ACK;
758 if (chan->do_split) {
759 if (chan->complete_split)
760 hcintmsk |= HCINTMSK_NYET;
761 else
762 hcintmsk |= HCINTMSK_ACK;
763 }
764 break;
765
766 case USB_ENDPOINT_XFER_ISOC:
767 if (dbg_perio())
768 dev_vdbg(hsotg->dev, "isoc\n");
769 hcintmsk |= HCINTMSK_XFERCOMPL;
770 hcintmsk |= HCINTMSK_FRMOVRUN;
771 hcintmsk |= HCINTMSK_ACK;
772
773 if (chan->ep_is_in) {
774 hcintmsk |= HCINTMSK_XACTERR;
775 hcintmsk |= HCINTMSK_BBLERR;
776 }
777 break;
778 default:
779 dev_err(hsotg->dev, "## Unknown EP type ##\n");
780 break;
781 }
782
783 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
784 if (dbg_hc(chan))
785 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
786}
787
788static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
789 struct dwc2_host_chan *chan)
790{
791 u32 hcintmsk = HCINTMSK_CHHLTD;
792
793 /*
794 * For Descriptor DMA mode core halts the channel on AHB error.
795 * Interrupt is not required.
796 */
95832c00 797 if (!hsotg->params.dma_desc_enable) {
b02038fa
JY
798 if (dbg_hc(chan))
799 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
800 hcintmsk |= HCINTMSK_AHBERR;
801 } else {
802 if (dbg_hc(chan))
803 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
804 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
805 hcintmsk |= HCINTMSK_XFERCOMPL;
806 }
807
808 if (chan->error_state && !chan->do_split &&
809 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
810 if (dbg_hc(chan))
811 dev_vdbg(hsotg->dev, "setting ACK\n");
812 hcintmsk |= HCINTMSK_ACK;
813 if (chan->ep_is_in) {
814 hcintmsk |= HCINTMSK_DATATGLERR;
815 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
816 hcintmsk |= HCINTMSK_NAK;
817 }
818 }
819
820 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
821 if (dbg_hc(chan))
822 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
823}
824
825static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
826 struct dwc2_host_chan *chan)
827{
828 u32 intmsk;
829
95832c00 830 if (hsotg->params.host_dma) {
b02038fa
JY
831 if (dbg_hc(chan))
832 dev_vdbg(hsotg->dev, "DMA enabled\n");
833 dwc2_hc_enable_dma_ints(hsotg, chan);
834 } else {
835 if (dbg_hc(chan))
836 dev_vdbg(hsotg->dev, "DMA disabled\n");
837 dwc2_hc_enable_slave_ints(hsotg, chan);
838 }
839
840 /* Enable the top level host channel interrupt */
841 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
842 intmsk |= 1 << chan->hc_num;
843 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
844 if (dbg_hc(chan))
845 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
846
847 /* Make sure host channel interrupts are enabled */
848 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
849 intmsk |= GINTSTS_HCHINT;
850 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
851 if (dbg_hc(chan))
852 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
853}
854
855/**
856 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
857 * a specific endpoint
858 *
859 * @hsotg: Programming view of DWC_otg controller
860 * @chan: Information needed to initialize the host channel
861 *
862 * The HCCHARn register is set up with the characteristics specified in chan.
863 * Host channel interrupts that may need to be serviced while this transfer is
864 * in progress are enabled.
865 */
866static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
867{
868 u8 hc_num = chan->hc_num;
869 u32 hcintmsk;
870 u32 hcchar;
871 u32 hcsplt = 0;
872
873 if (dbg_hc(chan))
874 dev_vdbg(hsotg->dev, "%s()\n", __func__);
875
876 /* Clear old interrupt conditions for this host channel */
877 hcintmsk = 0xffffffff;
878 hcintmsk &= ~HCINTMSK_RESERVED14_31;
879 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
880
881 /* Enable channel interrupts required for this transfer */
882 dwc2_hc_enable_ints(hsotg, chan);
883
884 /*
885 * Program the HCCHARn register with the endpoint characteristics for
886 * the current transfer
887 */
888 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
889 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
890 if (chan->ep_is_in)
891 hcchar |= HCCHAR_EPDIR;
892 if (chan->speed == USB_SPEED_LOW)
893 hcchar |= HCCHAR_LSPDDEV;
894 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
895 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
896 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
897 if (dbg_hc(chan)) {
898 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
899 hc_num, hcchar);
900
901 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
902 __func__, hc_num);
903 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
904 chan->dev_addr);
905 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
906 chan->ep_num);
907 dev_vdbg(hsotg->dev, " Is In: %d\n",
908 chan->ep_is_in);
909 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
910 chan->speed == USB_SPEED_LOW);
911 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
912 chan->ep_type);
913 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
914 chan->max_packet);
915 }
916
917 /* Program the HCSPLT register for SPLITs */
918 if (chan->do_split) {
919 if (dbg_hc(chan))
920 dev_vdbg(hsotg->dev,
921 "Programming HC %d with split --> %s\n",
922 hc_num,
923 chan->complete_split ? "CSPLIT" : "SSPLIT");
924 if (chan->complete_split)
925 hcsplt |= HCSPLT_COMPSPLT;
926 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
927 HCSPLT_XACTPOS_MASK;
928 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
929 HCSPLT_HUBADDR_MASK;
930 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
931 HCSPLT_PRTADDR_MASK;
932 if (dbg_hc(chan)) {
933 dev_vdbg(hsotg->dev, " comp split %d\n",
934 chan->complete_split);
935 dev_vdbg(hsotg->dev, " xact pos %d\n",
936 chan->xact_pos);
937 dev_vdbg(hsotg->dev, " hub addr %d\n",
938 chan->hub_addr);
939 dev_vdbg(hsotg->dev, " hub port %d\n",
940 chan->hub_port);
941 dev_vdbg(hsotg->dev, " is_in %d\n",
942 chan->ep_is_in);
943 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
944 chan->max_packet);
945 dev_vdbg(hsotg->dev, " xferlen %d\n",
946 chan->xfer_len);
947 }
948 }
949
950 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
951}
952
953/**
954 * dwc2_hc_halt() - Attempts to halt a host channel
955 *
956 * @hsotg: Controller register interface
957 * @chan: Host channel to halt
958 * @halt_status: Reason for halting the channel
959 *
960 * This function should only be called in Slave mode or to abort a transfer in
961 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
962 * controller halts the channel when the transfer is complete or a condition
963 * occurs that requires application intervention.
964 *
965 * In slave mode, checks for a free request queue entry, then sets the Channel
966 * Enable and Channel Disable bits of the Host Channel Characteristics
967 * register of the specified channel to intiate the halt. If there is no free
968 * request queue entry, sets only the Channel Disable bit of the HCCHARn
969 * register to flush requests for this channel. In the latter case, sets a
970 * flag to indicate that the host channel needs to be halted when a request
971 * queue slot is open.
972 *
973 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
974 * HCCHARn register. The controller ensures there is space in the request
975 * queue before submitting the halt request.
976 *
977 * Some time may elapse before the core flushes any posted requests for this
978 * host channel and halts. The Channel Halted interrupt handler completes the
979 * deactivation of the host channel.
980 */
981void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
982 enum dwc2_halt_status halt_status)
983{
984 u32 nptxsts, hptxsts, hcchar;
985
986 if (dbg_hc(chan))
987 dev_vdbg(hsotg->dev, "%s()\n", __func__);
13578c8b
MH
988
989 /*
990 * In buffer DMA or external DMA mode channel can't be halted
991 * for non-split periodic channels. At the end of the next
992 * uframe/frame (in the worst case), the core generates a channel
993 * halted and disables the channel automatically.
994 */
995 if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
996 hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
997 if (!chan->do_split &&
998 (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
999 chan->ep_type == USB_ENDPOINT_XFER_INT)) {
1000 dev_err(hsotg->dev, "%s() Channel can't be halted\n",
1001 __func__);
1002 return;
1003 }
1004 }
1005
b02038fa
JY
1006 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1007 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1008
1009 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1010 halt_status == DWC2_HC_XFER_AHB_ERR) {
1011 /*
1012 * Disable all channel interrupts except Ch Halted. The QTD
1013 * and QH state associated with this transfer has been cleared
1014 * (in the case of URB_DEQUEUE), so the channel needs to be
1015 * shut down carefully to prevent crashes.
1016 */
1017 u32 hcintmsk = HCINTMSK_CHHLTD;
1018
1019 dev_vdbg(hsotg->dev, "dequeue/error\n");
1020 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1021
1022 /*
1023 * Make sure no other interrupts besides halt are currently
1024 * pending. Handling another interrupt could cause a crash due
1025 * to the QTD and QH state.
1026 */
1027 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1028
1029 /*
1030 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1031 * even if the channel was already halted for some other
1032 * reason
1033 */
1034 chan->halt_status = halt_status;
1035
1036 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1037 if (!(hcchar & HCCHAR_CHENA)) {
1038 /*
1039 * The channel is either already halted or it hasn't
1040 * started yet. In DMA mode, the transfer may halt if
1041 * it finishes normally or a condition occurs that
1042 * requires driver intervention. Don't want to halt
1043 * the channel again. In either Slave or DMA mode,
1044 * it's possible that the transfer has been assigned
1045 * to a channel, but not started yet when an URB is
1046 * dequeued. Don't want to halt a channel that hasn't
1047 * started yet.
1048 */
1049 return;
1050 }
1051 }
1052 if (chan->halt_pending) {
1053 /*
1054 * A halt has already been issued for this channel. This might
1055 * happen when a transfer is aborted by a higher level in
1056 * the stack.
1057 */
1058 dev_vdbg(hsotg->dev,
1059 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1060 __func__, chan->hc_num);
1061 return;
1062 }
1063
1064 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1065
1066 /* No need to set the bit in DDMA for disabling the channel */
1067 /* TODO check it everywhere channel is disabled */
95832c00 1068 if (!hsotg->params.dma_desc_enable) {
b02038fa
JY
1069 if (dbg_hc(chan))
1070 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1071 hcchar |= HCCHAR_CHENA;
1072 } else {
1073 if (dbg_hc(chan))
1074 dev_dbg(hsotg->dev, "desc DMA enabled\n");
1075 }
1076 hcchar |= HCCHAR_CHDIS;
1077
95832c00 1078 if (!hsotg->params.host_dma) {
b02038fa
JY
1079 if (dbg_hc(chan))
1080 dev_vdbg(hsotg->dev, "DMA not enabled\n");
1081 hcchar |= HCCHAR_CHENA;
1082
1083 /* Check for space in the request queue to issue the halt */
1084 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1085 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1086 dev_vdbg(hsotg->dev, "control/bulk\n");
1087 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1088 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1089 dev_vdbg(hsotg->dev, "Disabling channel\n");
1090 hcchar &= ~HCCHAR_CHENA;
1091 }
1092 } else {
1093 if (dbg_perio())
1094 dev_vdbg(hsotg->dev, "isoc/intr\n");
1095 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1096 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1097 hsotg->queuing_high_bandwidth) {
1098 if (dbg_perio())
1099 dev_vdbg(hsotg->dev, "Disabling channel\n");
1100 hcchar &= ~HCCHAR_CHENA;
1101 }
1102 }
1103 } else {
1104 if (dbg_hc(chan))
1105 dev_vdbg(hsotg->dev, "DMA enabled\n");
1106 }
1107
1108 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1109 chan->halt_status = halt_status;
1110
1111 if (hcchar & HCCHAR_CHENA) {
1112 if (dbg_hc(chan))
1113 dev_vdbg(hsotg->dev, "Channel enabled\n");
1114 chan->halt_pending = 1;
1115 chan->halt_on_queue = 0;
1116 } else {
1117 if (dbg_hc(chan))
1118 dev_vdbg(hsotg->dev, "Channel disabled\n");
1119 chan->halt_on_queue = 1;
1120 }
1121
1122 if (dbg_hc(chan)) {
1123 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1124 chan->hc_num);
1125 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1126 hcchar);
1127 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1128 chan->halt_pending);
1129 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1130 chan->halt_on_queue);
1131 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1132 chan->halt_status);
1133 }
1134}
1135
1136/**
1137 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1138 *
1139 * @hsotg: Programming view of DWC_otg controller
1140 * @chan: Identifies the host channel to clean up
1141 *
1142 * This function is normally called after a transfer is done and the host
1143 * channel is being released
1144 */
1145void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1146{
1147 u32 hcintmsk;
1148
1149 chan->xfer_started = 0;
1150
1151 list_del_init(&chan->split_order_list_entry);
1152
1153 /*
1154 * Clear channel interrupt enables and any unhandled channel interrupt
1155 * conditions
1156 */
1157 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1158 hcintmsk = 0xffffffff;
1159 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1160 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1161}
1162
1163/**
1164 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1165 * which frame a periodic transfer should occur
1166 *
1167 * @hsotg: Programming view of DWC_otg controller
1168 * @chan: Identifies the host channel to set up and its properties
1169 * @hcchar: Current value of the HCCHAR register for the specified host channel
1170 *
1171 * This function has no effect on non-periodic transfers
1172 */
1173static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1174 struct dwc2_host_chan *chan, u32 *hcchar)
1175{
1176 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1177 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1178 int host_speed;
1179 int xfer_ns;
1180 int xfer_us;
1181 int bytes_in_fifo;
1182 u16 fifo_space;
1183 u16 frame_number;
1184 u16 wire_frame;
1185
1186 /*
1187 * Try to figure out if we're an even or odd frame. If we set
1188 * even and the current frame number is even the the transfer
1189 * will happen immediately. Similar if both are odd. If one is
1190 * even and the other is odd then the transfer will happen when
1191 * the frame number ticks.
1192 *
1193 * There's a bit of a balancing act to get this right.
1194 * Sometimes we may want to send data in the current frame (AK
1195 * right away). We might want to do this if the frame number
1196 * _just_ ticked, but we might also want to do this in order
1197 * to continue a split transaction that happened late in a
1198 * microframe (so we didn't know to queue the next transfer
1199 * until the frame number had ticked). The problem is that we
1200 * need a lot of knowledge to know if there's actually still
1201 * time to send things or if it would be better to wait until
1202 * the next frame.
1203 *
1204 * We can look at how much time is left in the current frame
1205 * and make a guess about whether we'll have time to transfer.
1206 * We'll do that.
1207 */
1208
1209 /* Get speed host is running at */
1210 host_speed = (chan->speed != USB_SPEED_HIGH &&
1211 !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1212
1213 /* See how many bytes are in the periodic FIFO right now */
1214 fifo_space = (dwc2_readl(hsotg->regs + HPTXSTS) &
1215 TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1216 bytes_in_fifo = sizeof(u32) *
bea8e86c 1217 (hsotg->params.host_perio_tx_fifo_size -
b02038fa
JY
1218 fifo_space);
1219
1220 /*
1221 * Roughly estimate bus time for everything in the periodic
1222 * queue + our new transfer. This is "rough" because we're
1223 * using a function that makes takes into account IN/OUT
1224 * and INT/ISO and we're just slamming in one value for all
1225 * transfers. This should be an over-estimate and that should
1226 * be OK, but we can probably tighten it.
1227 */
1228 xfer_ns = usb_calc_bus_time(host_speed, false, false,
1229 chan->xfer_len + bytes_in_fifo);
1230 xfer_us = NS_TO_US(xfer_ns);
1231
1232 /* See what frame number we'll be at by the time we finish */
1233 frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1234
1235 /* This is when we were scheduled to be on the wire */
1236 wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1237
1238 /*
1239 * If we'd finish _after_ the frame we're scheduled in then
1240 * it's hopeless. Just schedule right away and hope for the
1241 * best. Note that it _might_ be wise to call back into the
1242 * scheduler to pick a better frame, but this is better than
1243 * nothing.
1244 */
1245 if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1246 dwc2_sch_vdbg(hsotg,
1247 "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1248 chan->qh, wire_frame, frame_number,
1249 dwc2_frame_num_dec(frame_number,
1250 wire_frame));
1251 wire_frame = frame_number;
1252
1253 /*
1254 * We picked a different frame number; communicate this
1255 * back to the scheduler so it doesn't try to schedule
1256 * another in the same frame.
1257 *
1258 * Remember that next_active_frame is 1 before the wire
1259 * frame.
1260 */
1261 chan->qh->next_active_frame =
1262 dwc2_frame_num_dec(frame_number, 1);
1263 }
1264
1265 if (wire_frame & 1)
1266 *hcchar |= HCCHAR_ODDFRM;
1267 else
1268 *hcchar &= ~HCCHAR_ODDFRM;
1269 }
1270}
1271
1272static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1273{
1274 /* Set up the initial PID for the transfer */
1275 if (chan->speed == USB_SPEED_HIGH) {
1276 if (chan->ep_is_in) {
1277 if (chan->multi_count == 1)
1278 chan->data_pid_start = DWC2_HC_PID_DATA0;
1279 else if (chan->multi_count == 2)
1280 chan->data_pid_start = DWC2_HC_PID_DATA1;
1281 else
1282 chan->data_pid_start = DWC2_HC_PID_DATA2;
1283 } else {
1284 if (chan->multi_count == 1)
1285 chan->data_pid_start = DWC2_HC_PID_DATA0;
1286 else
1287 chan->data_pid_start = DWC2_HC_PID_MDATA;
1288 }
1289 } else {
1290 chan->data_pid_start = DWC2_HC_PID_DATA0;
1291 }
1292}
1293
7359d482 1294/**
b02038fa
JY
1295 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1296 * the Host Channel
7359d482
PZ
1297 *
1298 * @hsotg: Programming view of DWC_otg controller
b02038fa 1299 * @chan: Information needed to initialize the host channel
7359d482 1300 *
b02038fa
JY
1301 * This function should only be called in Slave mode. For a channel associated
1302 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1303 * associated with a periodic EP, the periodic Tx FIFO is written.
7359d482 1304 *
b02038fa
JY
1305 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1306 * the number of bytes written to the Tx FIFO.
7359d482 1307 */
b02038fa
JY
1308static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1309 struct dwc2_host_chan *chan)
1310{
1311 u32 i;
1312 u32 remaining_count;
1313 u32 byte_count;
1314 u32 dword_count;
1315 u32 __iomem *data_fifo;
1316 u32 *data_buf = (u32 *)chan->xfer_buf;
1317
1318 if (dbg_hc(chan))
1319 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1320
1321 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1322
1323 remaining_count = chan->xfer_len - chan->xfer_count;
1324 if (remaining_count > chan->max_packet)
1325 byte_count = chan->max_packet;
1326 else
1327 byte_count = remaining_count;
1328
1329 dword_count = (byte_count + 3) / 4;
1330
1331 if (((unsigned long)data_buf & 0x3) == 0) {
1332 /* xfer_buf is DWORD aligned */
1333 for (i = 0; i < dword_count; i++, data_buf++)
1334 dwc2_writel(*data_buf, data_fifo);
1335 } else {
1336 /* xfer_buf is not DWORD aligned */
1337 for (i = 0; i < dword_count; i++, data_buf++) {
1338 u32 data = data_buf[0] | data_buf[1] << 8 |
1339 data_buf[2] << 16 | data_buf[3] << 24;
1340 dwc2_writel(data, data_fifo);
1341 }
1342 }
1343
1344 chan->xfer_count += byte_count;
1345 chan->xfer_buf += byte_count;
1346}
1347
1348/**
1349 * dwc2_hc_do_ping() - Starts a PING transfer
1350 *
1351 * @hsotg: Programming view of DWC_otg controller
1352 * @chan: Information needed to initialize the host channel
1353 *
1354 * This function should only be called in Slave mode. The Do Ping bit is set in
1355 * the HCTSIZ register, then the channel is enabled.
1356 */
1357static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1358 struct dwc2_host_chan *chan)
7359d482 1359{
7359d482 1360 u32 hcchar;
7359d482 1361 u32 hctsiz;
7359d482 1362
b02038fa
JY
1363 if (dbg_hc(chan))
1364 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1365 chan->hc_num);
1366
1367 hctsiz = TSIZ_DOPNG;
1368 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1369 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
7359d482 1370
95c8bc36 1371 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
b02038fa
JY
1372 hcchar |= HCCHAR_CHENA;
1373 hcchar &= ~HCCHAR_CHDIS;
1374 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1375}
7359d482 1376
b02038fa
JY
1377/**
1378 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1379 * channel and starts the transfer
1380 *
1381 * @hsotg: Programming view of DWC_otg controller
1382 * @chan: Information needed to initialize the host channel. The xfer_len value
1383 * may be reduced to accommodate the max widths of the XferSize and
1384 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1385 * changed to reflect the final xfer_len value.
1386 *
1387 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1388 * the caller must ensure that there is sufficient space in the request queue
1389 * and Tx Data FIFO.
1390 *
1391 * For an OUT transfer in Slave mode, it loads a data packet into the
1392 * appropriate FIFO. If necessary, additional data packets are loaded in the
1393 * Host ISR.
1394 *
1395 * For an IN transfer in Slave mode, a data packet is requested. The data
1396 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1397 * additional data packets are requested in the Host ISR.
1398 *
1399 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1400 * register along with a packet count of 1 and the channel is enabled. This
1401 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1402 * simply set to 0 since no data transfer occurs in this case.
1403 *
1404 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1405 * all the information required to perform the subsequent data transfer. In
1406 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1407 * controller performs the entire PING protocol, then starts the data
1408 * transfer.
1409 */
1410static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1411 struct dwc2_host_chan *chan)
1412{
bea8e86c
JY
1413 u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1414 u16 max_hc_pkt_count = hsotg->params.max_packet_count;
b02038fa
JY
1415 u32 hcchar;
1416 u32 hctsiz = 0;
1417 u16 num_packets;
1418 u32 ec_mc;
1419
1420 if (dbg_hc(chan))
1421 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1422
1423 if (chan->do_ping) {
95832c00 1424 if (!hsotg->params.host_dma) {
b02038fa
JY
1425 if (dbg_hc(chan))
1426 dev_vdbg(hsotg->dev, "ping, no DMA\n");
1427 dwc2_hc_do_ping(hsotg, chan);
1428 chan->xfer_started = 1;
1429 return;
1430 }
7359d482 1431
b02038fa
JY
1432 if (dbg_hc(chan))
1433 dev_vdbg(hsotg->dev, "ping, DMA\n");
1434
1435 hctsiz |= TSIZ_DOPNG;
7359d482 1436 }
b02038fa
JY
1437
1438 if (chan->do_split) {
1439 if (dbg_hc(chan))
1440 dev_vdbg(hsotg->dev, "split\n");
1441 num_packets = 1;
1442
1443 if (chan->complete_split && !chan->ep_is_in)
1444 /*
1445 * For CSPLIT OUT Transfer, set the size to 0 so the
1446 * core doesn't expect any data written to the FIFO
1447 */
1448 chan->xfer_len = 0;
1449 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1450 chan->xfer_len = chan->max_packet;
1451 else if (!chan->ep_is_in && chan->xfer_len > 188)
1452 chan->xfer_len = 188;
1453
1454 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1455 TSIZ_XFERSIZE_MASK;
1456
1457 /* For split set ec_mc for immediate retries */
1458 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1459 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1460 ec_mc = 3;
1461 else
1462 ec_mc = 1;
1463 } else {
1464 if (dbg_hc(chan))
1465 dev_vdbg(hsotg->dev, "no split\n");
1466 /*
1467 * Ensure that the transfer length and packet count will fit
1468 * in the widths allocated for them in the HCTSIZn register
1469 */
1470 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1471 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1472 /*
1473 * Make sure the transfer size is no larger than one
1474 * (micro)frame's worth of data. (A check was done
1475 * when the periodic transfer was accepted to ensure
1476 * that a (micro)frame's worth of data can be
1477 * programmed into a channel.)
1478 */
1479 u32 max_periodic_len =
1480 chan->multi_count * chan->max_packet;
1481
1482 if (chan->xfer_len > max_periodic_len)
1483 chan->xfer_len = max_periodic_len;
1484 } else if (chan->xfer_len > max_hc_xfer_size) {
1485 /*
1486 * Make sure that xfer_len is a multiple of max packet
1487 * size
1488 */
1489 chan->xfer_len =
1490 max_hc_xfer_size - chan->max_packet + 1;
1491 }
1492
1493 if (chan->xfer_len > 0) {
1494 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1495 chan->max_packet;
1496 if (num_packets > max_hc_pkt_count) {
1497 num_packets = max_hc_pkt_count;
1498 chan->xfer_len = num_packets * chan->max_packet;
1499 }
1500 } else {
1501 /* Need 1 packet for transfer length of 0 */
1502 num_packets = 1;
1503 }
1504
1505 if (chan->ep_is_in)
1506 /*
1507 * Always program an integral # of max packets for IN
1508 * transfers
1509 */
1510 chan->xfer_len = num_packets * chan->max_packet;
1511
1512 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1513 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1514 /*
1515 * Make sure that the multi_count field matches the
1516 * actual transfer length
1517 */
1518 chan->multi_count = num_packets;
1519
1520 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1521 dwc2_set_pid_isoc(chan);
1522
1523 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1524 TSIZ_XFERSIZE_MASK;
1525
1526 /* The ec_mc gets the multi_count for non-split */
1527 ec_mc = chan->multi_count;
1528 }
1529
1530 chan->start_pkt_count = num_packets;
1531 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1532 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1533 TSIZ_SC_MC_PID_MASK;
1534 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1535 if (dbg_hc(chan)) {
1536 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1537 hctsiz, chan->hc_num);
1538
1539 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1540 chan->hc_num);
1541 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1542 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1543 TSIZ_XFERSIZE_SHIFT);
1544 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1545 (hctsiz & TSIZ_PKTCNT_MASK) >>
1546 TSIZ_PKTCNT_SHIFT);
1547 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1548 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1549 TSIZ_SC_MC_PID_SHIFT);
1550 }
1551
95832c00 1552 if (hsotg->params.host_dma) {
b02038fa
JY
1553 dwc2_writel((u32)chan->xfer_dma,
1554 hsotg->regs + HCDMA(chan->hc_num));
1555 if (dbg_hc(chan))
1556 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1557 (unsigned long)chan->xfer_dma, chan->hc_num);
1558 }
1559
1560 /* Start the split */
1561 if (chan->do_split) {
1562 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1563
1564 hcsplt |= HCSPLT_SPLTENA;
1565 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1566 }
1567
1568 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1569 hcchar &= ~HCCHAR_MULTICNT_MASK;
1570 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1571 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1572
1573 if (hcchar & HCCHAR_CHDIS)
1574 dev_warn(hsotg->dev,
1575 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1576 __func__, chan->hc_num, hcchar);
1577
1578 /* Set host channel enable after all other setup is complete */
1579 hcchar |= HCCHAR_CHENA;
1580 hcchar &= ~HCCHAR_CHDIS;
1581
1582 if (dbg_hc(chan))
1583 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1584 (hcchar & HCCHAR_MULTICNT_MASK) >>
1585 HCCHAR_MULTICNT_SHIFT);
1586
1587 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1588 if (dbg_hc(chan))
1589 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1590 chan->hc_num);
1591
1592 chan->xfer_started = 1;
1593 chan->requests++;
1594
95832c00 1595 if (!hsotg->params.host_dma &&
b02038fa
JY
1596 !chan->ep_is_in && chan->xfer_len > 0)
1597 /* Load OUT packet into the appropriate Tx FIFO */
1598 dwc2_hc_write_packet(hsotg, chan);
1599}
1600
1601/**
1602 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1603 * host channel and starts the transfer in Descriptor DMA mode
1604 *
1605 * @hsotg: Programming view of DWC_otg controller
1606 * @chan: Information needed to initialize the host channel
1607 *
1608 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1609 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1610 * with micro-frame bitmap.
1611 *
1612 * Initializes HCDMA register with descriptor list address and CTD value then
1613 * starts the transfer via enabling the channel.
1614 */
1615void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1616 struct dwc2_host_chan *chan)
1617{
1618 u32 hcchar;
1619 u32 hctsiz = 0;
1620
1621 if (chan->do_ping)
1622 hctsiz |= TSIZ_DOPNG;
1623
1624 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1625 dwc2_set_pid_isoc(chan);
1626
1627 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1628 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1629 TSIZ_SC_MC_PID_MASK;
1630
1631 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1632 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1633
1634 /* Non-zero only for high-speed interrupt endpoints */
1635 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1636
1637 if (dbg_hc(chan)) {
1638 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1639 chan->hc_num);
1640 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1641 chan->data_pid_start);
1642 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1643 }
1644
1645 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1646
1647 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1648 chan->desc_list_sz, DMA_TO_DEVICE);
1649
1650 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
1651
1652 if (dbg_hc(chan))
1653 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1654 &chan->desc_list_addr, chan->hc_num);
1655
1656 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1657 hcchar &= ~HCCHAR_MULTICNT_MASK;
1658 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1659 HCCHAR_MULTICNT_MASK;
1660
1661 if (hcchar & HCCHAR_CHDIS)
1662 dev_warn(hsotg->dev,
1663 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1664 __func__, chan->hc_num, hcchar);
1665
1666 /* Set host channel enable after all other setup is complete */
1667 hcchar |= HCCHAR_CHENA;
1668 hcchar &= ~HCCHAR_CHDIS;
1669
1670 if (dbg_hc(chan))
1671 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1672 (hcchar & HCCHAR_MULTICNT_MASK) >>
1673 HCCHAR_MULTICNT_SHIFT);
1674
1675 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1676 if (dbg_hc(chan))
1677 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1678 chan->hc_num);
1679
1680 chan->xfer_started = 1;
1681 chan->requests++;
1682}
1683
1684/**
1685 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1686 * a previous call to dwc2_hc_start_transfer()
1687 *
1688 * @hsotg: Programming view of DWC_otg controller
1689 * @chan: Information needed to initialize the host channel
1690 *
1691 * The caller must ensure there is sufficient space in the request queue and Tx
1692 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1693 * the controller acts autonomously to complete transfers programmed to a host
1694 * channel.
1695 *
1696 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1697 * if there is any data remaining to be queued. For an IN transfer, another
1698 * data packet is always requested. For the SETUP phase of a control transfer,
1699 * this function does nothing.
1700 *
1701 * Return: 1 if a new request is queued, 0 if no more requests are required
1702 * for this transfer
1703 */
1704static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1705 struct dwc2_host_chan *chan)
1706{
1707 if (dbg_hc(chan))
1708 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1709 chan->hc_num);
1710
1711 if (chan->do_split)
1712 /* SPLITs always queue just once per channel */
1713 return 0;
1714
1715 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1716 /* SETUPs are queued only once since they can't be NAK'd */
1717 return 0;
1718
1719 if (chan->ep_is_in) {
1720 /*
1721 * Always queue another request for other IN transfers. If
1722 * back-to-back INs are issued and NAKs are received for both,
1723 * the driver may still be processing the first NAK when the
1724 * second NAK is received. When the interrupt handler clears
1725 * the NAK interrupt for the first NAK, the second NAK will
1726 * not be seen. So we can't depend on the NAK interrupt
1727 * handler to requeue a NAK'd request. Instead, IN requests
1728 * are issued each time this function is called. When the
1729 * transfer completes, the extra requests for the channel will
1730 * be flushed.
1731 */
1732 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1733
1734 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1735 hcchar |= HCCHAR_CHENA;
1736 hcchar &= ~HCCHAR_CHDIS;
1737 if (dbg_hc(chan))
1738 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
1739 hcchar);
1740 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1741 chan->requests++;
1742 return 1;
1743 }
1744
1745 /* OUT transfers */
1746
1747 if (chan->xfer_count < chan->xfer_len) {
1748 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1749 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1750 u32 hcchar = dwc2_readl(hsotg->regs +
1751 HCCHAR(chan->hc_num));
1752
1753 dwc2_hc_set_even_odd_frame(hsotg, chan,
1754 &hcchar);
1755 }
1756
1757 /* Load OUT packet into the appropriate Tx FIFO */
1758 dwc2_hc_write_packet(hsotg, chan);
1759 chan->requests++;
1760 return 1;
1761 }
1762
1763 return 0;
7359d482
PZ
1764}
1765
b02038fa
JY
1766/*
1767 * =========================================================================
1768 * HCD
1769 * =========================================================================
1770 */
1771
7359d482
PZ
1772/*
1773 * Processes all the URBs in a single list of QHs. Completes them with
1774 * -ETIMEDOUT and frees the QTD.
1775 *
1776 * Must be called with interrupt disabled and spinlock held
1777 */
1778static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1779 struct list_head *qh_list)
1780{
1781 struct dwc2_qh *qh, *qh_tmp;
1782 struct dwc2_qtd *qtd, *qtd_tmp;
1783
1784 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1785 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1786 qtd_list_entry) {
2e84da6e 1787 dwc2_host_complete(hsotg, qtd, -ECONNRESET);
0d012b98 1788 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
7359d482
PZ
1789 }
1790 }
1791}
1792
1793static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1794 struct list_head *qh_list)
1795{
1796 struct dwc2_qtd *qtd, *qtd_tmp;
1797 struct dwc2_qh *qh, *qh_tmp;
1798 unsigned long flags;
1799
1800 if (!qh_list->next)
1801 /* The list hasn't been initialized yet */
1802 return;
1803
1804 spin_lock_irqsave(&hsotg->lock, flags);
1805
1806 /* Ensure there are no QTDs or URBs left */
1807 dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1808
1809 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1810 dwc2_hcd_qh_unlink(hsotg, qh);
1811
1812 /* Free each QTD in the QH's QTD list */
1813 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1814 qtd_list_entry)
1815 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1816
16e80218
DA
1817 if (qh->channel && qh->channel->qh == qh)
1818 qh->channel->qh = NULL;
1819
7359d482
PZ
1820 spin_unlock_irqrestore(&hsotg->lock, flags);
1821 dwc2_hcd_qh_free(hsotg, qh);
1822 spin_lock_irqsave(&hsotg->lock, flags);
1823 }
1824
1825 spin_unlock_irqrestore(&hsotg->lock, flags);
1826}
1827
1828/*
1829 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1830 * and periodic schedules. The QTD associated with each URB is removed from
1831 * the schedule and freed. This function may be called when a disconnect is
1832 * detected or when the HCD is being stopped.
1833 *
1834 * Must be called with interrupt disabled and spinlock held
1835 */
1836static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1837{
1838 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
1839 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1840 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1841 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1842 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1843 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1844}
1845
1846/**
1847 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1848 *
1849 * @hsotg: Pointer to struct dwc2_hsotg
1850 */
1851void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1852{
1853 u32 hprt0;
1854
1855 if (hsotg->op_state == OTG_STATE_B_HOST) {
1856 /*
1857 * Reset the port. During a HNP mode switch the reset
1858 * needs to occur within 1ms and have a duration of at
1859 * least 50ms.
1860 */
1861 hprt0 = dwc2_read_hprt0(hsotg);
1862 hprt0 |= HPRT0_RST;
95c8bc36 1863 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482
PZ
1864 }
1865
1866 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1867 msecs_to_jiffies(50));
1868}
1869
1870/* Must be called with interrupt disabled and spinlock held */
1871static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1872{
bea8e86c 1873 int num_channels = hsotg->params.host_channels;
7359d482
PZ
1874 struct dwc2_host_chan *channel;
1875 u32 hcchar;
1876 int i;
1877
95832c00 1878 if (!hsotg->params.host_dma) {
7359d482
PZ
1879 /* Flush out any channel requests in slave mode */
1880 for (i = 0; i < num_channels; i++) {
1881 channel = hsotg->hc_ptr_array[i];
1882 if (!list_empty(&channel->hc_list_entry))
1883 continue;
95c8bc36 1884 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
7359d482
PZ
1885 if (hcchar & HCCHAR_CHENA) {
1886 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1887 hcchar |= HCCHAR_CHDIS;
95c8bc36 1888 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
7359d482
PZ
1889 }
1890 }
1891 }
1892
1893 for (i = 0; i < num_channels; i++) {
1894 channel = hsotg->hc_ptr_array[i];
1895 if (!list_empty(&channel->hc_list_entry))
1896 continue;
95c8bc36 1897 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
7359d482
PZ
1898 if (hcchar & HCCHAR_CHENA) {
1899 /* Halt the channel */
1900 hcchar |= HCCHAR_CHDIS;
95c8bc36 1901 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
7359d482
PZ
1902 }
1903
1904 dwc2_hc_cleanup(hsotg, channel);
1905 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1906 /*
1907 * Added for Descriptor DMA to prevent channel double cleanup in
1908 * release_channel_ddma(), which is called from ep_disable when
1909 * device disconnects
1910 */
1911 channel->qh = NULL;
1912 }
7252f1bf 1913 /* All channels have been freed, mark them available */
95832c00 1914 if (hsotg->params.uframe_sched) {
7252f1bf 1915 hsotg->available_host_channels =
bea8e86c 1916 hsotg->params.host_channels;
7252f1bf
VP
1917 } else {
1918 hsotg->non_periodic_channels = 0;
1919 hsotg->periodic_channels = 0;
1920 }
7359d482
PZ
1921}
1922
6a659531
DA
1923/**
1924 * dwc2_hcd_connect() - Handles connect of the HCD
1925 *
1926 * @hsotg: Pointer to struct dwc2_hsotg
1927 *
1928 * Must be called with interrupt disabled and spinlock held
1929 */
1930void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
1931{
1932 if (hsotg->lx_state != DWC2_L0)
1933 usb_hcd_resume_root_hub(hsotg->priv);
1934
1935 hsotg->flags.b.port_connect_status_change = 1;
1936 hsotg->flags.b.port_connect_status = 1;
1937}
1938
7359d482
PZ
1939/**
1940 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
1941 *
1942 * @hsotg: Pointer to struct dwc2_hsotg
6a659531 1943 * @force: If true, we won't try to reconnect even if we see device connected.
7359d482
PZ
1944 *
1945 * Must be called with interrupt disabled and spinlock held
1946 */
6a659531 1947void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
7359d482
PZ
1948{
1949 u32 intr;
6a659531 1950 u32 hprt0;
7359d482
PZ
1951
1952 /* Set status flags for the hub driver */
1953 hsotg->flags.b.port_connect_status_change = 1;
1954 hsotg->flags.b.port_connect_status = 0;
1955
1956 /*
1957 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1958 * interrupt mask and status bits and disabling subsequent host
1959 * channel interrupts.
1960 */
95c8bc36 1961 intr = dwc2_readl(hsotg->regs + GINTMSK);
7359d482 1962 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
95c8bc36 1963 dwc2_writel(intr, hsotg->regs + GINTMSK);
7359d482 1964 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
95c8bc36 1965 dwc2_writel(intr, hsotg->regs + GINTSTS);
7359d482
PZ
1966
1967 /*
1968 * Turn off the vbus power only if the core has transitioned to device
1969 * mode. If still in host mode, need to keep power on to detect a
1970 * reconnection.
1971 */
1972 if (dwc2_is_device_mode(hsotg)) {
1973 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1974 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
95c8bc36 1975 dwc2_writel(0, hsotg->regs + HPRT0);
7359d482
PZ
1976 }
1977
1978 dwc2_disable_host_interrupts(hsotg);
1979 }
1980
1981 /* Respond with an error status to all URBs in the schedule */
1982 dwc2_kill_all_urbs(hsotg);
1983
1984 if (dwc2_is_host_mode(hsotg))
1985 /* Clean up any host channels that were in use */
1986 dwc2_hcd_cleanup_channels(hsotg);
1987
1988 dwc2_host_disconnect(hsotg);
6a659531
DA
1989
1990 /*
1991 * Add an extra check here to see if we're actually connected but
1992 * we don't have a detection interrupt pending. This can happen if:
1993 * 1. hardware sees connect
1994 * 2. hardware sees disconnect
1995 * 3. hardware sees connect
1996 * 4. dwc2_port_intr() - clears connect interrupt
1997 * 5. dwc2_handle_common_intr() - calls here
1998 *
1999 * Without the extra check here we will end calling disconnect
2000 * and won't get any future interrupts to handle the connect.
2001 */
2002 if (!force) {
2003 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
2004 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
2005 dwc2_hcd_connect(hsotg);
2006 }
7359d482
PZ
2007}
2008
2009/**
2010 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
2011 *
2012 * @hsotg: Pointer to struct dwc2_hsotg
2013 */
2014static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
2015{
1fb7f12d 2016 if (hsotg->bus_suspended) {
7359d482 2017 hsotg->flags.b.port_suspend_change = 1;
b46146d5 2018 usb_hcd_resume_root_hub(hsotg->priv);
b46146d5 2019 }
1fb7f12d
DA
2020
2021 if (hsotg->lx_state == DWC2_L1)
2022 hsotg->flags.b.port_l1_change = 1;
7359d482
PZ
2023}
2024
2025/**
2026 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
2027 *
2028 * @hsotg: Pointer to struct dwc2_hsotg
2029 *
2030 * Must be called with interrupt disabled and spinlock held
2031 */
2032void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
2033{
2034 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
2035
2036 /*
2037 * The root hub should be disconnected before this function is called.
2038 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
2039 * and the QH lists (via ..._hcd_endpoint_disable).
2040 */
2041
2042 /* Turn off all host-specific interrupts */
2043 dwc2_disable_host_interrupts(hsotg);
2044
2045 /* Turn off the vbus power */
2046 dev_dbg(hsotg->dev, "PortPower off\n");
95c8bc36 2047 dwc2_writel(0, hsotg->regs + HPRT0);
7359d482
PZ
2048}
2049
33ad261a 2050/* Caller must hold driver lock */
7359d482 2051static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
b58e6cee 2052 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
b5a468a6 2053 struct dwc2_qtd *qtd)
7359d482 2054{
7359d482
PZ
2055 u32 intr_mask;
2056 int retval;
9f8144c6 2057 int dev_speed;
7359d482
PZ
2058
2059 if (!hsotg->flags.b.port_connect_status) {
2060 /* No longer connected */
2061 dev_err(hsotg->dev, "Not connected\n");
2062 return -ENODEV;
2063 }
2064
9f8144c6
NH
2065 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
2066
2067 /* Some configurations cannot support LS traffic on a FS root port */
2068 if ((dev_speed == USB_SPEED_LOW) &&
2069 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
2070 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
95c8bc36 2071 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
9f8144c6
NH
2072 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
2073
2074 if (prtspd == HPRT0_SPD_FULL_SPEED)
2075 return -ENODEV;
2076 }
2077
7359d482 2078 if (!qtd)
b5a468a6 2079 return -EINVAL;
7359d482
PZ
2080
2081 dwc2_hcd_qtd_init(qtd, urb);
b58e6cee 2082 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
9bda1aac 2083 if (retval) {
7359d482
PZ
2084 dev_err(hsotg->dev,
2085 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
2086 retval);
7359d482
PZ
2087 return retval;
2088 }
2089
95c8bc36 2090 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
9bda1aac 2091 if (!(intr_mask & GINTSTS_SOF)) {
7359d482
PZ
2092 enum dwc2_transaction_type tr_type;
2093
2094 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
2095 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
2096 /*
2097 * Do not schedule SG transactions until qtd has
2098 * URB_GIVEBACK_ASAP set
2099 */
2100 return 0;
2101
7359d482
PZ
2102 tr_type = dwc2_hcd_select_transactions(hsotg);
2103 if (tr_type != DWC2_TRANSACTION_NONE)
2104 dwc2_hcd_queue_transactions(hsotg, tr_type);
7359d482
PZ
2105 }
2106
9bda1aac 2107 return 0;
7359d482
PZ
2108}
2109
2110/* Must be called with interrupt disabled and spinlock held */
2111static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
2112 struct dwc2_hcd_urb *urb)
2113{
2114 struct dwc2_qh *qh;
2115 struct dwc2_qtd *urb_qtd;
2116
2117 urb_qtd = urb->qtd;
2118 if (!urb_qtd) {
2119 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
2120 return -EINVAL;
2121 }
2122
2123 qh = urb_qtd->qh;
2124 if (!qh) {
2125 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
2126 return -EINVAL;
2127 }
2128
0d012b98
PZ
2129 urb->priv = NULL;
2130
7359d482
PZ
2131 if (urb_qtd->in_process && qh->channel) {
2132 dwc2_dump_channel_info(hsotg, qh->channel);
2133
2134 /* The QTD is in process (it has been assigned to a channel) */
2135 if (hsotg->flags.b.port_connect_status)
2136 /*
2137 * If still connected (i.e. in host mode), halt the
2138 * channel so it can be used for other transfers. If
2139 * no longer connected, the host registers can't be
2140 * written to halt the channel since the core is in
2141 * device mode.
2142 */
2143 dwc2_hc_halt(hsotg, qh->channel,
2144 DWC2_HC_XFER_URB_DEQUEUE);
2145 }
2146
2147 /*
2148 * Free the QTD and clean up the associated QH. Leave the QH in the
2149 * schedule if it has any remaining QTDs.
2150 */
95832c00 2151 if (!hsotg->params.dma_desc_enable) {
7359d482
PZ
2152 u8 in_process = urb_qtd->in_process;
2153
2154 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2155 if (in_process) {
2156 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
2157 qh->channel = NULL;
2158 } else if (list_empty(&qh->qtd_list)) {
2159 dwc2_hcd_qh_unlink(hsotg, qh);
2160 }
2161 } else {
2162 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2163 }
2164
2165 return 0;
2166}
2167
2168/* Must NOT be called with interrupt disabled or spinlock held */
2169static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
2170 struct usb_host_endpoint *ep, int retry)
2171{
2172 struct dwc2_qtd *qtd, *qtd_tmp;
2173 struct dwc2_qh *qh;
2174 unsigned long flags;
2175 int rc;
2176
2177 spin_lock_irqsave(&hsotg->lock, flags);
2178
2179 qh = ep->hcpriv;
2180 if (!qh) {
2181 rc = -EINVAL;
2182 goto err;
2183 }
2184
2185 while (!list_empty(&qh->qtd_list) && retry--) {
2186 if (retry == 0) {
2187 dev_err(hsotg->dev,
2188 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
2189 rc = -EBUSY;
2190 goto err;
2191 }
2192
2193 spin_unlock_irqrestore(&hsotg->lock, flags);
04a9db79 2194 msleep(20);
7359d482
PZ
2195 spin_lock_irqsave(&hsotg->lock, flags);
2196 qh = ep->hcpriv;
2197 if (!qh) {
2198 rc = -EINVAL;
2199 goto err;
2200 }
2201 }
2202
2203 dwc2_hcd_qh_unlink(hsotg, qh);
2204
2205 /* Free each QTD in the QH's QTD list */
2206 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2207 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2208
2209 ep->hcpriv = NULL;
16e80218
DA
2210
2211 if (qh->channel && qh->channel->qh == qh)
2212 qh->channel->qh = NULL;
2213
7359d482 2214 spin_unlock_irqrestore(&hsotg->lock, flags);
16e80218 2215
7359d482
PZ
2216 dwc2_hcd_qh_free(hsotg, qh);
2217
2218 return 0;
2219
2220err:
2221 ep->hcpriv = NULL;
2222 spin_unlock_irqrestore(&hsotg->lock, flags);
2223
2224 return rc;
2225}
2226
2227/* Must be called with interrupt disabled and spinlock held */
2228static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2229 struct usb_host_endpoint *ep)
2230{
2231 struct dwc2_qh *qh = ep->hcpriv;
2232
2233 if (!qh)
2234 return -EINVAL;
2235
2236 qh->data_toggle = DWC2_HC_PID_DATA0;
2237
2238 return 0;
2239}
2240
b02038fa
JY
2241/**
2242 * dwc2_core_init() - Initializes the DWC_otg controller registers and
2243 * prepares the core for device mode or host mode operation
2244 *
2245 * @hsotg: Programming view of the DWC_otg controller
2246 * @initial_setup: If true then this is the first init for this instance.
2247 */
2248static int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2249{
2250 u32 usbcfg, otgctl;
2251 int retval;
2252
2253 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2254
2255 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2256
2257 /* Set ULPI External VBUS bit if needed */
2258 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
95832c00 2259 if (hsotg->params.phy_ulpi_ext_vbus)
b02038fa
JY
2260 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2261
2262 /* Set external TS Dline pulsing bit if needed */
2263 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
95832c00 2264 if (hsotg->params.ts_dline)
b02038fa
JY
2265 usbcfg |= GUSBCFG_TERMSELDLPULSE;
2266
2267 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2268
2269 /*
2270 * Reset the Controller
2271 *
2272 * We only need to reset the controller if this is a re-init.
2273 * For the first init we know for sure that earlier code reset us (it
2274 * needed to in order to properly detect various parameters).
2275 */
2276 if (!initial_setup) {
2277 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
2278 if (retval) {
2279 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2280 __func__);
2281 return retval;
2282 }
2283 }
2284
2285 /*
2286 * This needs to happen in FS mode before any other programming occurs
2287 */
2288 retval = dwc2_phy_init(hsotg, initial_setup);
2289 if (retval)
2290 return retval;
2291
2292 /* Program the GAHBCFG Register */
2293 retval = dwc2_gahbcfg_init(hsotg);
2294 if (retval)
2295 return retval;
2296
2297 /* Program the GUSBCFG register */
2298 dwc2_gusbcfg_init(hsotg);
2299
2300 /* Program the GOTGCTL register */
2301 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2302 otgctl &= ~GOTGCTL_OTGVER;
b02038fa 2303 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
b02038fa
JY
2304
2305 /* Clear the SRP success bit for FS-I2c */
2306 hsotg->srp_success = 0;
2307
2308 /* Enable common interrupts */
2309 dwc2_enable_common_interrupts(hsotg);
2310
2311 /*
2312 * Do device or host initialization based on mode during PCD and
2313 * HCD initialization
2314 */
2315 if (dwc2_is_host_mode(hsotg)) {
2316 dev_dbg(hsotg->dev, "Host Mode\n");
2317 hsotg->op_state = OTG_STATE_A_HOST;
2318 } else {
2319 dev_dbg(hsotg->dev, "Device Mode\n");
2320 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2321 }
2322
2323 return 0;
2324}
2325
2326/**
2327 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2328 * Host mode
2329 *
2330 * @hsotg: Programming view of DWC_otg controller
2331 *
2332 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2333 * request queues. Host channels are reset to ensure that they are ready for
2334 * performing transfers.
2335 */
2336static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2337{
2338 u32 hcfg, hfir, otgctl;
2339
2340 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2341
2342 /* Restart the Phy Clock */
2343 dwc2_writel(0, hsotg->regs + PCGCTL);
2344
2345 /* Initialize Host Configuration Register */
2346 dwc2_init_fs_ls_pclk_sel(hsotg);
38e9002b
VM
2347 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
2348 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
b02038fa
JY
2349 hcfg = dwc2_readl(hsotg->regs + HCFG);
2350 hcfg |= HCFG_FSLSSUPP;
2351 dwc2_writel(hcfg, hsotg->regs + HCFG);
2352 }
2353
2354 /*
2355 * This bit allows dynamic reloading of the HFIR register during
2356 * runtime. This bit needs to be programmed during initial configuration
2357 * and its value must not be changed during runtime.
2358 */
95832c00 2359 if (hsotg->params.reload_ctl) {
b02038fa
JY
2360 hfir = dwc2_readl(hsotg->regs + HFIR);
2361 hfir |= HFIR_RLDCTRL;
2362 dwc2_writel(hfir, hsotg->regs + HFIR);
2363 }
2364
95832c00 2365 if (hsotg->params.dma_desc_enable) {
b02038fa
JY
2366 u32 op_mode = hsotg->hw_params.op_mode;
2367
2368 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2369 !hsotg->hw_params.dma_desc_enable ||
2370 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2371 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2372 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2373 dev_err(hsotg->dev,
2374 "Hardware does not support descriptor DMA mode -\n");
2375 dev_err(hsotg->dev,
2376 "falling back to buffer DMA mode.\n");
95832c00 2377 hsotg->params.dma_desc_enable = false;
b02038fa
JY
2378 } else {
2379 hcfg = dwc2_readl(hsotg->regs + HCFG);
2380 hcfg |= HCFG_DESCDMA;
2381 dwc2_writel(hcfg, hsotg->regs + HCFG);
2382 }
2383 }
2384
2385 /* Configure data FIFO sizes */
2386 dwc2_config_fifos(hsotg);
2387
2388 /* TODO - check this */
2389 /* Clear Host Set HNP Enable in the OTG Control Register */
2390 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2391 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2392 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2393
2394 /* Make sure the FIFOs are flushed */
2395 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2396 dwc2_flush_rx_fifo(hsotg);
2397
2398 /* Clear Host Set HNP Enable in the OTG Control Register */
2399 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2400 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2401 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2402
95832c00 2403 if (!hsotg->params.dma_desc_enable) {
b02038fa
JY
2404 int num_channels, i;
2405 u32 hcchar;
2406
2407 /* Flush out any leftover queued requests */
bea8e86c 2408 num_channels = hsotg->params.host_channels;
b02038fa
JY
2409 for (i = 0; i < num_channels; i++) {
2410 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2411 hcchar &= ~HCCHAR_CHENA;
2412 hcchar |= HCCHAR_CHDIS;
2413 hcchar &= ~HCCHAR_EPDIR;
2414 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2415 }
2416
2417 /* Halt all channels to put them into a known state */
2418 for (i = 0; i < num_channels; i++) {
2419 int count = 0;
2420
2421 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2422 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2423 hcchar &= ~HCCHAR_EPDIR;
2424 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2425 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2426 __func__, i);
2427 do {
2428 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2429 if (++count > 1000) {
2430 dev_err(hsotg->dev,
2431 "Unable to clear enable on channel %d\n",
2432 i);
2433 break;
2434 }
2435 udelay(1);
2436 } while (hcchar & HCCHAR_CHENA);
2437 }
2438 }
2439
2440 /* Turn on the vbus power */
2441 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2442 if (hsotg->op_state == OTG_STATE_A_HOST) {
2443 u32 hprt0 = dwc2_read_hprt0(hsotg);
2444
2445 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2446 !!(hprt0 & HPRT0_PWR));
2447 if (!(hprt0 & HPRT0_PWR)) {
2448 hprt0 |= HPRT0_PWR;
2449 dwc2_writel(hprt0, hsotg->regs + HPRT0);
2450 }
2451 }
2452
2453 dwc2_enable_host_interrupts(hsotg);
2454}
2455
7359d482
PZ
2456/*
2457 * Initializes dynamic portions of the DWC_otg HCD state
2458 *
2459 * Must be called with interrupt disabled and spinlock held
2460 */
2461static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2462{
2463 struct dwc2_host_chan *chan, *chan_tmp;
2464 int num_channels;
2465 int i;
2466
2467 hsotg->flags.d32 = 0;
7359d482 2468 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
20f2eb9c 2469
95832c00 2470 if (hsotg->params.uframe_sched) {
20f2eb9c 2471 hsotg->available_host_channels =
bea8e86c 2472 hsotg->params.host_channels;
20f2eb9c
DC
2473 } else {
2474 hsotg->non_periodic_channels = 0;
2475 hsotg->periodic_channels = 0;
2476 }
7359d482
PZ
2477
2478 /*
2479 * Put all channels in the free channel list and clean up channel
2480 * states
2481 */
2482 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2483 hc_list_entry)
2484 list_del_init(&chan->hc_list_entry);
2485
bea8e86c 2486 num_channels = hsotg->params.host_channels;
7359d482
PZ
2487 for (i = 0; i < num_channels; i++) {
2488 chan = hsotg->hc_ptr_array[i];
2489 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2490 dwc2_hc_cleanup(hsotg, chan);
2491 }
2492
2493 /* Initialize the DWC core for host mode operation */
2494 dwc2_core_host_init(hsotg);
2495}
2496
2497static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2498 struct dwc2_host_chan *chan,
2499 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2500{
2501 int hub_addr, hub_port;
2502
2503 chan->do_split = 1;
2504 chan->xact_pos = qtd->isoc_split_pos;
2505 chan->complete_split = qtd->complete_split;
2506 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2507 chan->hub_addr = (u8)hub_addr;
2508 chan->hub_port = (u8)hub_port;
2509}
2510
3bc04e28
DA
2511static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2512 struct dwc2_host_chan *chan,
2513 struct dwc2_qtd *qtd)
7359d482
PZ
2514{
2515 struct dwc2_hcd_urb *urb = qtd->urb;
2516 struct dwc2_hcd_iso_packet_desc *frame_desc;
2517
2518 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2519 case USB_ENDPOINT_XFER_CONTROL:
2520 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2521
2522 switch (qtd->control_phase) {
2523 case DWC2_CONTROL_SETUP:
2524 dev_vdbg(hsotg->dev, " Control setup transaction\n");
2525 chan->do_ping = 0;
2526 chan->ep_is_in = 0;
2527 chan->data_pid_start = DWC2_HC_PID_SETUP;
95832c00 2528 if (hsotg->params.host_dma)
7359d482
PZ
2529 chan->xfer_dma = urb->setup_dma;
2530 else
2531 chan->xfer_buf = urb->setup_packet;
2532 chan->xfer_len = 8;
7359d482
PZ
2533 break;
2534
2535 case DWC2_CONTROL_DATA:
2536 dev_vdbg(hsotg->dev, " Control data transaction\n");
2537 chan->data_pid_start = qtd->data_toggle;
2538 break;
2539
2540 case DWC2_CONTROL_STATUS:
2541 /*
2542 * Direction is opposite of data direction or IN if no
2543 * data
2544 */
2545 dev_vdbg(hsotg->dev, " Control status transaction\n");
2546 if (urb->length == 0)
2547 chan->ep_is_in = 1;
2548 else
2549 chan->ep_is_in =
2550 dwc2_hcd_is_pipe_out(&urb->pipe_info);
2551 if (chan->ep_is_in)
2552 chan->do_ping = 0;
2553 chan->data_pid_start = DWC2_HC_PID_DATA1;
2554 chan->xfer_len = 0;
95832c00 2555 if (hsotg->params.host_dma)
7359d482
PZ
2556 chan->xfer_dma = hsotg->status_buf_dma;
2557 else
2558 chan->xfer_buf = hsotg->status_buf;
7359d482
PZ
2559 break;
2560 }
2561 break;
2562
2563 case USB_ENDPOINT_XFER_BULK:
2564 chan->ep_type = USB_ENDPOINT_XFER_BULK;
2565 break;
2566
2567 case USB_ENDPOINT_XFER_INT:
2568 chan->ep_type = USB_ENDPOINT_XFER_INT;
2569 break;
2570
2571 case USB_ENDPOINT_XFER_ISOC:
2572 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
95832c00 2573 if (hsotg->params.dma_desc_enable)
7359d482
PZ
2574 break;
2575
2576 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2577 frame_desc->status = 0;
2578
95832c00 2579 if (hsotg->params.host_dma) {
7359d482
PZ
2580 chan->xfer_dma = urb->dma;
2581 chan->xfer_dma += frame_desc->offset +
2582 qtd->isoc_split_offset;
2583 } else {
2584 chan->xfer_buf = urb->buf;
2585 chan->xfer_buf += frame_desc->offset +
2586 qtd->isoc_split_offset;
2587 }
2588
2589 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2590
7359d482
PZ
2591 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2592 if (chan->xfer_len <= 188)
2593 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2594 else
2595 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2596 }
2597 break;
2598 }
3bc04e28
DA
2599}
2600
2601#define DWC2_USB_DMA_ALIGN 4
2602
2603struct dma_aligned_buffer {
2604 void *kmalloc_ptr;
2605 void *old_xfer_buffer;
2606 u8 data[0];
2607};
2608
2609static void dwc2_free_dma_aligned_buffer(struct urb *urb)
2610{
2611 struct dma_aligned_buffer *temp;
2612
2613 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2614 return;
7359d482 2615
3bc04e28 2616 temp = container_of(urb->transfer_buffer,
9da51974 2617 struct dma_aligned_buffer, data);
3bc04e28
DA
2618
2619 if (usb_urb_dir_in(urb))
2620 memcpy(temp->old_xfer_buffer, temp->data,
2621 urb->transfer_buffer_length);
2622 urb->transfer_buffer = temp->old_xfer_buffer;
2623 kfree(temp->kmalloc_ptr);
2624
2625 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
7359d482
PZ
2626}
2627
3bc04e28 2628static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
7359d482 2629{
3bc04e28
DA
2630 struct dma_aligned_buffer *temp, *kmalloc_ptr;
2631 size_t kmalloc_size;
7359d482 2632
3bc04e28
DA
2633 if (urb->num_sgs || urb->sg ||
2634 urb->transfer_buffer_length == 0 ||
2635 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2636 return 0;
5dce9555 2637
3bc04e28
DA
2638 /* Allocate a buffer with enough padding for alignment */
2639 kmalloc_size = urb->transfer_buffer_length +
2640 sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
7359d482 2641
3bc04e28
DA
2642 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2643 if (!kmalloc_ptr)
2644 return -ENOMEM;
5dce9555 2645
3bc04e28
DA
2646 /* Position our struct dma_aligned_buffer such that data is aligned */
2647 temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
2648 temp->kmalloc_ptr = kmalloc_ptr;
2649 temp->old_xfer_buffer = urb->transfer_buffer;
2650 if (usb_urb_dir_out(urb))
2651 memcpy(temp->data, urb->transfer_buffer,
2652 urb->transfer_buffer_length);
2653 urb->transfer_buffer = temp->data;
7359d482 2654
3bc04e28 2655 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
db62b9a8 2656
7359d482
PZ
2657 return 0;
2658}
2659
3bc04e28 2660static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
9da51974 2661 gfp_t mem_flags)
3bc04e28
DA
2662{
2663 int ret;
2664
2665 /* We assume setup_dma is always aligned; warn if not */
2666 WARN_ON_ONCE(urb->setup_dma &&
2667 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
2668
2669 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
2670 if (ret)
2671 return ret;
2672
2673 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2674 if (ret)
2675 dwc2_free_dma_aligned_buffer(urb);
2676
2677 return ret;
2678}
2679
2680static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2681{
2682 usb_hcd_unmap_urb_for_dma(hcd, urb);
2683 dwc2_free_dma_aligned_buffer(urb);
2684}
2685
7359d482
PZ
2686/**
2687 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2688 * channel and initializes the host channel to perform the transactions. The
2689 * host channel is removed from the free list.
2690 *
2691 * @hsotg: The HCD state structure
2692 * @qh: Transactions from the first QTD for this QH are selected and assigned
2693 * to a free host channel
2694 */
20f2eb9c 2695static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
7359d482
PZ
2696{
2697 struct dwc2_host_chan *chan;
2698 struct dwc2_hcd_urb *urb;
2699 struct dwc2_qtd *qtd;
7359d482 2700
b49977a6
MK
2701 if (dbg_qh(qh))
2702 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
7359d482
PZ
2703
2704 if (list_empty(&qh->qtd_list)) {
2705 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
20f2eb9c 2706 return -ENOMEM;
7359d482
PZ
2707 }
2708
2709 if (list_empty(&hsotg->free_hc_list)) {
2710 dev_dbg(hsotg->dev, "No free channel to assign\n");
20f2eb9c 2711 return -ENOMEM;
7359d482
PZ
2712 }
2713
2714 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2715 hc_list_entry);
2716
20f2eb9c 2717 /* Remove host channel from free list */
7359d482
PZ
2718 list_del_init(&chan->hc_list_entry);
2719
2720 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2721 urb = qtd->urb;
2722 qh->channel = chan;
2723 qtd->in_process = 1;
2724
2725 /*
2726 * Use usb_pipedevice to determine device address. This address is
2727 * 0 before the SET_ADDRESS command and the correct address afterward.
2728 */
2729 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2730 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2731 chan->speed = qh->dev_speed;
2732 chan->max_packet = dwc2_max_packet(qh->maxp);
2733
2734 chan->xfer_started = 0;
2735 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2736 chan->error_state = (qtd->error_count > 0);
2737 chan->halt_on_queue = 0;
2738 chan->halt_pending = 0;
2739 chan->requests = 0;
2740
2741 /*
2742 * The following values may be modified in the transfer type section
2743 * below. The xfer_len value may be reduced when the transfer is
2744 * started to accommodate the max widths of the XferSize and PktCnt
2745 * fields in the HCTSIZn register.
2746 */
2747
2748 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2749 if (chan->ep_is_in)
2750 chan->do_ping = 0;
2751 else
2752 chan->do_ping = qh->ping_state;
2753
2754 chan->data_pid_start = qh->data_toggle;
2755 chan->multi_count = 1;
2756
bb6c3422 2757 if (urb->actual_length > urb->length &&
9da51974 2758 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
84181086
PZ
2759 urb->actual_length = urb->length;
2760
95832c00 2761 if (hsotg->params.host_dma)
7359d482 2762 chan->xfer_dma = urb->dma + urb->actual_length;
3bc04e28 2763 else
7359d482 2764 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
7359d482
PZ
2765
2766 chan->xfer_len = urb->length - urb->actual_length;
2767 chan->xfer_count = 0;
2768
2769 /* Set the split attributes if required */
2770 if (qh->do_split)
2771 dwc2_hc_init_split(hsotg, chan, qtd, urb);
2772 else
2773 chan->do_split = 0;
2774
2775 /* Set the transfer attributes */
3bc04e28 2776 dwc2_hc_init_xfer(hsotg, chan, qtd);
7359d482
PZ
2777
2778 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2779 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2780 /*
2781 * This value may be modified when the transfer is started
2782 * to reflect the actual transfer length
2783 */
2784 chan->multi_count = dwc2_hb_mult(qh->maxp);
2785
95832c00 2786 if (hsotg->params.dma_desc_enable) {
7359d482 2787 chan->desc_list_addr = qh->desc_list_dma;
95105a99
GH
2788 chan->desc_list_sz = qh->desc_list_sz;
2789 }
7359d482
PZ
2790
2791 dwc2_hc_init(hsotg, chan);
2792 chan->qh = qh;
20f2eb9c
DC
2793
2794 return 0;
7359d482
PZ
2795}
2796
2797/**
2798 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2799 * schedule and assigns them to available host channels. Called from the HCD
2800 * interrupt handler functions.
2801 *
2802 * @hsotg: The HCD state structure
2803 *
2804 * Return: The types of new transactions that were assigned to host channels
2805 */
2806enum dwc2_transaction_type dwc2_hcd_select_transactions(
2807 struct dwc2_hsotg *hsotg)
2808{
2809 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2810 struct list_head *qh_ptr;
2811 struct dwc2_qh *qh;
2812 int num_channels;
2813
2814#ifdef DWC2_DEBUG_SOF
2815 dev_vdbg(hsotg->dev, " Select Transactions\n");
2816#endif
2817
2818 /* Process entries in the periodic ready list */
2819 qh_ptr = hsotg->periodic_sched_ready.next;
2820 while (qh_ptr != &hsotg->periodic_sched_ready) {
2821 if (list_empty(&hsotg->free_hc_list))
2822 break;
95832c00 2823 if (hsotg->params.uframe_sched) {
20f2eb9c
DC
2824 if (hsotg->available_host_channels <= 1)
2825 break;
2826 hsotg->available_host_channels--;
2827 }
7359d482 2828 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
20f2eb9c
DC
2829 if (dwc2_assign_and_init_hc(hsotg, qh))
2830 break;
7359d482
PZ
2831
2832 /*
2833 * Move the QH from the periodic ready schedule to the
2834 * periodic assigned schedule
2835 */
2836 qh_ptr = qh_ptr->next;
94ef7aee
DA
2837 list_move_tail(&qh->qh_list_entry,
2838 &hsotg->periodic_sched_assigned);
7359d482
PZ
2839 ret_val = DWC2_TRANSACTION_PERIODIC;
2840 }
2841
2842 /*
2843 * Process entries in the inactive portion of the non-periodic
2844 * schedule. Some free host channels may not be used if they are
2845 * reserved for periodic transfers.
2846 */
bea8e86c 2847 num_channels = hsotg->params.host_channels;
7359d482
PZ
2848 qh_ptr = hsotg->non_periodic_sched_inactive.next;
2849 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
95832c00 2850 if (!hsotg->params.uframe_sched &&
20f2eb9c 2851 hsotg->non_periodic_channels >= num_channels -
7359d482
PZ
2852 hsotg->periodic_channels)
2853 break;
2854 if (list_empty(&hsotg->free_hc_list))
2855 break;
2856 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
95832c00 2857 if (hsotg->params.uframe_sched) {
20f2eb9c
DC
2858 if (hsotg->available_host_channels < 1)
2859 break;
2860 hsotg->available_host_channels--;
2861 }
2862
2863 if (dwc2_assign_and_init_hc(hsotg, qh))
2864 break;
7359d482
PZ
2865
2866 /*
2867 * Move the QH from the non-periodic inactive schedule to the
2868 * non-periodic active schedule
2869 */
2870 qh_ptr = qh_ptr->next;
94ef7aee
DA
2871 list_move_tail(&qh->qh_list_entry,
2872 &hsotg->non_periodic_sched_active);
7359d482
PZ
2873
2874 if (ret_val == DWC2_TRANSACTION_NONE)
2875 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2876 else
2877 ret_val = DWC2_TRANSACTION_ALL;
2878
95832c00 2879 if (!hsotg->params.uframe_sched)
20f2eb9c 2880 hsotg->non_periodic_channels++;
7359d482
PZ
2881 }
2882
2883 return ret_val;
2884}
2885
2886/**
2887 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2888 * a host channel associated with either a periodic or non-periodic transfer
2889 *
2890 * @hsotg: The HCD state structure
2891 * @chan: Host channel descriptor associated with either a periodic or
2892 * non-periodic transfer
2893 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2894 * for periodic transfers or the non-periodic Tx FIFO
2895 * for non-periodic transfers
2896 *
2897 * Return: 1 if a request is queued and more requests may be needed to
2898 * complete the transfer, 0 if no more requests are required for this
2899 * transfer, -1 if there is insufficient space in the Tx FIFO
2900 *
2901 * This function assumes that there is space available in the appropriate
2902 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2903 * it checks whether space is available in the appropriate Tx FIFO.
2904 *
2905 * Must be called with interrupt disabled and spinlock held
2906 */
2907static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2908 struct dwc2_host_chan *chan,
2909 u16 fifo_dwords_avail)
2910{
2911 int retval = 0;
2912
c9c8ac01
DA
2913 if (chan->do_split)
2914 /* Put ourselves on the list to keep order straight */
2915 list_move_tail(&chan->split_order_list_entry,
2916 &hsotg->split_order);
2917
95832c00
JY
2918 if (hsotg->params.host_dma) {
2919 if (hsotg->params.dma_desc_enable) {
7359d482
PZ
2920 if (!chan->xfer_started ||
2921 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2922 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2923 chan->qh->ping_state = 0;
2924 }
2925 } else if (!chan->xfer_started) {
2926 dwc2_hc_start_transfer(hsotg, chan);
2927 chan->qh->ping_state = 0;
2928 }
2929 } else if (chan->halt_pending) {
2930 /* Don't queue a request if the channel has been halted */
2931 } else if (chan->halt_on_queue) {
2932 dwc2_hc_halt(hsotg, chan, chan->halt_status);
2933 } else if (chan->do_ping) {
2934 if (!chan->xfer_started)
2935 dwc2_hc_start_transfer(hsotg, chan);
2936 } else if (!chan->ep_is_in ||
2937 chan->data_pid_start == DWC2_HC_PID_SETUP) {
2938 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2939 if (!chan->xfer_started) {
2940 dwc2_hc_start_transfer(hsotg, chan);
2941 retval = 1;
2942 } else {
2943 retval = dwc2_hc_continue_transfer(hsotg, chan);
2944 }
2945 } else {
2946 retval = -1;
2947 }
2948 } else {
2949 if (!chan->xfer_started) {
2950 dwc2_hc_start_transfer(hsotg, chan);
2951 retval = 1;
2952 } else {
2953 retval = dwc2_hc_continue_transfer(hsotg, chan);
2954 }
2955 }
2956
2957 return retval;
2958}
2959
2960/*
2961 * Processes periodic channels for the next frame and queues transactions for
2962 * these channels to the DWC_otg controller. After queueing transactions, the
2963 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2964 * to queue as Periodic Tx FIFO or request queue space becomes available.
2965 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2966 *
2967 * Must be called with interrupt disabled and spinlock held
2968 */
2969static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2970{
2971 struct list_head *qh_ptr;
2972 struct dwc2_qh *qh;
2973 u32 tx_status;
2974 u32 fspcavail;
2975 u32 gintmsk;
2976 int status;
4e50e011
DA
2977 bool no_queue_space = false;
2978 bool no_fifo_space = false;
7359d482
PZ
2979 u32 qspcavail;
2980
4e50e011
DA
2981 /* If empty list then just adjust interrupt enables */
2982 if (list_empty(&hsotg->periodic_sched_assigned))
2983 goto exit;
2984
b49977a6
MK
2985 if (dbg_perio())
2986 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
7359d482 2987
95c8bc36 2988 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
d6ec53e0
MK
2989 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2990 TXSTS_QSPCAVAIL_SHIFT;
2991 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2992 TXSTS_FSPCAVAIL_SHIFT;
b49977a6
MK
2993
2994 if (dbg_perio()) {
2995 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
2996 qspcavail);
2997 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
2998 fspcavail);
2999 }
7359d482
PZ
3000
3001 qh_ptr = hsotg->periodic_sched_assigned.next;
3002 while (qh_ptr != &hsotg->periodic_sched_assigned) {
95c8bc36 3003 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
acdb9046
MK
3004 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3005 TXSTS_QSPCAVAIL_SHIFT;
3006 if (qspcavail == 0) {
fdb09b3e 3007 no_queue_space = true;
7359d482
PZ
3008 break;
3009 }
3010
3011 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
3012 if (!qh->channel) {
3013 qh_ptr = qh_ptr->next;
3014 continue;
3015 }
3016
3017 /* Make sure EP's TT buffer is clean before queueing qtds */
3018 if (qh->tt_buffer_dirty) {
3019 qh_ptr = qh_ptr->next;
3020 continue;
3021 }
3022
3023 /*
3024 * Set a flag if we're queuing high-bandwidth in slave mode.
3025 * The flag prevents any halts to get into the request queue in
3026 * the middle of multiple high-bandwidth packets getting queued.
3027 */
95832c00 3028 if (!hsotg->params.host_dma &&
9da51974 3029 qh->channel->multi_count > 1)
7359d482
PZ
3030 hsotg->queuing_high_bandwidth = 1;
3031
d6ec53e0
MK
3032 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3033 TXSTS_FSPCAVAIL_SHIFT;
7359d482
PZ
3034 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3035 if (status < 0) {
fdb09b3e 3036 no_fifo_space = true;
7359d482
PZ
3037 break;
3038 }
3039
3040 /*
3041 * In Slave mode, stay on the current transfer until there is
3042 * nothing more to do or the high-bandwidth request count is
3043 * reached. In DMA mode, only need to queue one request. The
3044 * controller automatically handles multiple packets for
3045 * high-bandwidth transfers.
3046 */
95832c00 3047 if (hsotg->params.host_dma || status == 0 ||
7359d482
PZ
3048 qh->channel->requests == qh->channel->multi_count) {
3049 qh_ptr = qh_ptr->next;
3050 /*
3051 * Move the QH from the periodic assigned schedule to
3052 * the periodic queued schedule
3053 */
94ef7aee
DA
3054 list_move_tail(&qh->qh_list_entry,
3055 &hsotg->periodic_sched_queued);
7359d482
PZ
3056
3057 /* done queuing high bandwidth */
3058 hsotg->queuing_high_bandwidth = 0;
3059 }
3060 }
3061
4e50e011
DA
3062exit:
3063 if (no_queue_space || no_fifo_space ||
95832c00 3064 (!hsotg->params.host_dma &&
4e50e011
DA
3065 !list_empty(&hsotg->periodic_sched_assigned))) {
3066 /*
3067 * May need to queue more transactions as the request
3068 * queue or Tx FIFO empties. Enable the periodic Tx
3069 * FIFO empty interrupt. (Always use the half-empty
3070 * level to ensure that new requests are loaded as
3071 * soon as possible.)
3072 */
3073 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3074 if (!(gintmsk & GINTSTS_PTXFEMP)) {
7359d482 3075 gintmsk |= GINTSTS_PTXFEMP;
95c8bc36 3076 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
4e50e011
DA
3077 }
3078 } else {
3079 /*
3080 * Disable the Tx FIFO empty interrupt since there are
3081 * no more transactions that need to be queued right
3082 * now. This function is called from interrupt
3083 * handlers to queue more transactions as transfer
3084 * states change.
38beaec6 3085 */
4e50e011
DA
3086 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3087 if (gintmsk & GINTSTS_PTXFEMP) {
7359d482 3088 gintmsk &= ~GINTSTS_PTXFEMP;
95c8bc36 3089 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
7359d482
PZ
3090 }
3091 }
3092}
3093
3094/*
3095 * Processes active non-periodic channels and queues transactions for these
3096 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3097 * FIFO Empty interrupt is enabled if there are more transactions to queue as
3098 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3099 * FIFO Empty interrupt is disabled.
3100 *
3101 * Must be called with interrupt disabled and spinlock held
3102 */
3103static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3104{
3105 struct list_head *orig_qh_ptr;
3106 struct dwc2_qh *qh;
3107 u32 tx_status;
3108 u32 qspcavail;
3109 u32 fspcavail;
3110 u32 gintmsk;
3111 int status;
3112 int no_queue_space = 0;
3113 int no_fifo_space = 0;
3114 int more_to_do = 0;
3115
3116 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3117
95c8bc36 3118 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
d6ec53e0
MK
3119 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3120 TXSTS_QSPCAVAIL_SHIFT;
3121 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3122 TXSTS_FSPCAVAIL_SHIFT;
7359d482
PZ
3123 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
3124 qspcavail);
3125 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
3126 fspcavail);
3127
3128 /*
3129 * Keep track of the starting point. Skip over the start-of-list
3130 * entry.
3131 */
3132 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3133 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3134 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3135
3136 /*
3137 * Process once through the active list or until no more space is
3138 * available in the request queue or the Tx FIFO
3139 */
3140 do {
95c8bc36 3141 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
d6ec53e0
MK
3142 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3143 TXSTS_QSPCAVAIL_SHIFT;
95832c00 3144 if (!hsotg->params.host_dma && qspcavail == 0) {
7359d482
PZ
3145 no_queue_space = 1;
3146 break;
3147 }
3148
3149 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3150 qh_list_entry);
3151 if (!qh->channel)
3152 goto next;
3153
3154 /* Make sure EP's TT buffer is clean before queueing qtds */
3155 if (qh->tt_buffer_dirty)
3156 goto next;
3157
d6ec53e0
MK
3158 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3159 TXSTS_FSPCAVAIL_SHIFT;
7359d482
PZ
3160 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3161
3162 if (status > 0) {
3163 more_to_do = 1;
3164 } else if (status < 0) {
3165 no_fifo_space = 1;
3166 break;
3167 }
3168next:
3169 /* Advance to next QH, skipping start-of-list entry */
3170 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3171 if (hsotg->non_periodic_qh_ptr ==
3172 &hsotg->non_periodic_sched_active)
3173 hsotg->non_periodic_qh_ptr =
3174 hsotg->non_periodic_qh_ptr->next;
3175 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3176
95832c00 3177 if (!hsotg->params.host_dma) {
95c8bc36 3178 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
d6ec53e0
MK
3179 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3180 TXSTS_QSPCAVAIL_SHIFT;
3181 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3182 TXSTS_FSPCAVAIL_SHIFT;
7359d482
PZ
3183 dev_vdbg(hsotg->dev,
3184 " NP Tx Req Queue Space Avail (after queue): %d\n",
3185 qspcavail);
3186 dev_vdbg(hsotg->dev,
3187 " NP Tx FIFO Space Avail (after queue): %d\n",
3188 fspcavail);
3189
3190 if (more_to_do || no_queue_space || no_fifo_space) {
3191 /*
3192 * May need to queue more transactions as the request
3193 * queue or Tx FIFO empties. Enable the non-periodic
3194 * Tx FIFO empty interrupt. (Always use the half-empty
3195 * level to ensure that new requests are loaded as
3196 * soon as possible.)
3197 */
95c8bc36 3198 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
7359d482 3199 gintmsk |= GINTSTS_NPTXFEMP;
95c8bc36 3200 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
7359d482
PZ
3201 } else {
3202 /*
3203 * Disable the Tx FIFO empty interrupt since there are
3204 * no more transactions that need to be queued right
3205 * now. This function is called from interrupt
3206 * handlers to queue more transactions as transfer
3207 * states change.
3208 */
95c8bc36 3209 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
7359d482 3210 gintmsk &= ~GINTSTS_NPTXFEMP;
95c8bc36 3211 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
7359d482
PZ
3212 }
3213 }
3214}
3215
3216/**
3217 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3218 * and queues transactions for these channels to the DWC_otg controller. Called
3219 * from the HCD interrupt handler functions.
3220 *
3221 * @hsotg: The HCD state structure
3222 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3223 * or both)
3224 *
3225 * Must be called with interrupt disabled and spinlock held
3226 */
3227void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3228 enum dwc2_transaction_type tr_type)
3229{
3230#ifdef DWC2_DEBUG_SOF
3231 dev_vdbg(hsotg->dev, "Queue Transactions\n");
3232#endif
3233 /* Process host channels associated with periodic transfers */
4e50e011
DA
3234 if (tr_type == DWC2_TRANSACTION_PERIODIC ||
3235 tr_type == DWC2_TRANSACTION_ALL)
7359d482
PZ
3236 dwc2_process_periodic_channels(hsotg);
3237
3238 /* Process host channels associated with non-periodic transfers */
3239 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3240 tr_type == DWC2_TRANSACTION_ALL) {
3241 if (!list_empty(&hsotg->non_periodic_sched_active)) {
3242 dwc2_process_non_periodic_channels(hsotg);
3243 } else {
3244 /*
3245 * Ensure NP Tx FIFO empty interrupt is disabled when
3246 * there are no non-periodic transfers to process
3247 */
95c8bc36 3248 u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
7359d482
PZ
3249
3250 gintmsk &= ~GINTSTS_NPTXFEMP;
95c8bc36 3251 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
7359d482
PZ
3252 }
3253 }
3254}
3255
3256static void dwc2_conn_id_status_change(struct work_struct *work)
3257{
3258 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3259 wf_otg);
3260 u32 count = 0;
3261 u32 gotgctl;
5390d438 3262 unsigned long flags;
7359d482
PZ
3263
3264 dev_dbg(hsotg->dev, "%s()\n", __func__);
3265
95c8bc36 3266 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
7359d482
PZ
3267 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3268 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3269 !!(gotgctl & GOTGCTL_CONID_B));
3270
3271 /* B-Device connector (Device Mode) */
3272 if (gotgctl & GOTGCTL_CONID_B) {
3273 /* Wait for switch to device mode */
3274 dev_dbg(hsotg->dev, "connId B\n");
9156a7ef
CY
3275 if (hsotg->bus_suspended) {
3276 dev_info(hsotg->dev,
3277 "Do port resume before switching to device mode\n");
3278 dwc2_port_resume(hsotg);
3279 }
7359d482
PZ
3280 while (!dwc2_is_device_mode(hsotg)) {
3281 dev_info(hsotg->dev,
3282 "Waiting for Peripheral Mode, Mode=%s\n",
3283 dwc2_is_host_mode(hsotg) ? "Host" :
3284 "Peripheral");
04a9db79 3285 msleep(20);
fc30c4bb
JS
3286 /*
3287 * Sometimes the initial GOTGCTRL read is wrong, so
3288 * check it again and jump to host mode if that was
3289 * the case.
3290 */
3291 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3292 if (!(gotgctl & GOTGCTL_CONID_B))
3293 goto host;
7359d482
PZ
3294 if (++count > 250)
3295 break;
3296 }
3297 if (count > 250)
3298 dev_err(hsotg->dev,
de9169a1 3299 "Connection id status change timed out\n");
7359d482 3300 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
0fe239bc 3301 dwc2_core_init(hsotg, false);
7359d482 3302 dwc2_enable_global_interrupts(hsotg);
5390d438 3303 spin_lock_irqsave(&hsotg->lock, flags);
1f91b4cc 3304 dwc2_hsotg_core_init_disconnected(hsotg, false);
5390d438 3305 spin_unlock_irqrestore(&hsotg->lock, flags);
1f91b4cc 3306 dwc2_hsotg_core_connect(hsotg);
7359d482 3307 } else {
fc30c4bb 3308host:
7359d482
PZ
3309 /* A-Device connector (Host Mode) */
3310 dev_dbg(hsotg->dev, "connId A\n");
3311 while (!dwc2_is_host_mode(hsotg)) {
3312 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3313 dwc2_is_host_mode(hsotg) ?
3314 "Host" : "Peripheral");
04a9db79 3315 msleep(20);
7359d482
PZ
3316 if (++count > 250)
3317 break;
3318 }
3319 if (count > 250)
3320 dev_err(hsotg->dev,
de9169a1 3321 "Connection id status change timed out\n");
7359d482 3322
d2471d4a
JS
3323 spin_lock_irqsave(&hsotg->lock, flags);
3324 dwc2_hsotg_disconnect(hsotg);
3325 spin_unlock_irqrestore(&hsotg->lock, flags);
3326
3327 hsotg->op_state = OTG_STATE_A_HOST;
7359d482 3328 /* Initialize the Core for Host mode */
0fe239bc 3329 dwc2_core_init(hsotg, false);
7359d482
PZ
3330 dwc2_enable_global_interrupts(hsotg);
3331 dwc2_hcd_start(hsotg);
3332 }
3333}
3334
e99e88a9 3335static void dwc2_wakeup_detected(struct timer_list *t)
7359d482 3336{
e99e88a9 3337 struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
7359d482
PZ
3338 u32 hprt0;
3339
3340 dev_dbg(hsotg->dev, "%s()\n", __func__);
3341
3342 /*
3343 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3344 * so that OPT tests pass with all PHYs.)
3345 */
3346 hprt0 = dwc2_read_hprt0(hsotg);
3347 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3348 hprt0 &= ~HPRT0_RES;
95c8bc36 3349 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482 3350 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
95c8bc36 3351 dwc2_readl(hsotg->regs + HPRT0));
7359d482
PZ
3352
3353 dwc2_hcd_rem_wakeup(hsotg);
fdb09b3e 3354 hsotg->bus_suspended = false;
7359d482
PZ
3355
3356 /* Change to L0 state */
3357 hsotg->lx_state = DWC2_L0;
3358}
3359
3360static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3361{
3362 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3363
3364 return hcd->self.b_hnp_enable;
3365}
3366
3367/* Must NOT be called with interrupt disabled or spinlock held */
3368static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3369{
3370 unsigned long flags;
3371 u32 hprt0;
3372 u32 pcgctl;
3373 u32 gotgctl;
3374
3375 dev_dbg(hsotg->dev, "%s()\n", __func__);
3376
3377 spin_lock_irqsave(&hsotg->lock, flags);
3378
3379 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
95c8bc36 3380 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
7359d482 3381 gotgctl |= GOTGCTL_HSTSETHNPEN;
95c8bc36 3382 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
7359d482
PZ
3383 hsotg->op_state = OTG_STATE_A_SUSPEND;
3384 }
3385
3386 hprt0 = dwc2_read_hprt0(hsotg);
3387 hprt0 |= HPRT0_SUSP;
95c8bc36 3388 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482 3389
fdb09b3e 3390 hsotg->bus_suspended = true;
7359d482 3391
a2a23d3f
GH
3392 /*
3393 * If hibernation is supported, Phy clock will be suspended
3394 * after registers are backuped.
3395 */
bea8e86c 3396 if (!hsotg->params.hibernation) {
a2a23d3f
GH
3397 /* Suspend the Phy Clock */
3398 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3399 pcgctl |= PCGCTL_STOPPCLK;
3400 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3401 udelay(10);
3402 }
7359d482
PZ
3403
3404 /* For HNP the bus must be suspended for at least 200ms */
3405 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
95c8bc36 3406 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
7359d482 3407 pcgctl &= ~PCGCTL_STOPPCLK;
95c8bc36 3408 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
7359d482
PZ
3409
3410 spin_unlock_irqrestore(&hsotg->lock, flags);
3411
04a9db79 3412 msleep(200);
7359d482
PZ
3413 } else {
3414 spin_unlock_irqrestore(&hsotg->lock, flags);
3415 }
3416}
3417
30db103c
GH
3418/* Must NOT be called with interrupt disabled or spinlock held */
3419static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
3420{
3421 unsigned long flags;
3422 u32 hprt0;
3423 u32 pcgctl;
3424
4d273c2a
DA
3425 spin_lock_irqsave(&hsotg->lock, flags);
3426
a2a23d3f
GH
3427 /*
3428 * If hibernation is supported, Phy clock is already resumed
3429 * after registers restore.
3430 */
bea8e86c 3431 if (!hsotg->params.hibernation) {
a2a23d3f
GH
3432 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3433 pcgctl &= ~PCGCTL_STOPPCLK;
3434 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
4d273c2a 3435 spin_unlock_irqrestore(&hsotg->lock, flags);
04a9db79 3436 msleep(20);
4d273c2a 3437 spin_lock_irqsave(&hsotg->lock, flags);
a2a23d3f 3438 }
30db103c 3439
30db103c
GH
3440 hprt0 = dwc2_read_hprt0(hsotg);
3441 hprt0 |= HPRT0_RES;
3442 hprt0 &= ~HPRT0_SUSP;
3443 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3444 spin_unlock_irqrestore(&hsotg->lock, flags);
3445
3446 msleep(USB_RESUME_TIMEOUT);
3447
3448 spin_lock_irqsave(&hsotg->lock, flags);
3449 hprt0 = dwc2_read_hprt0(hsotg);
3450 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
3451 dwc2_writel(hprt0, hsotg->regs + HPRT0);
fdb09b3e 3452 hsotg->bus_suspended = false;
30db103c
GH
3453 spin_unlock_irqrestore(&hsotg->lock, flags);
3454}
3455
7359d482
PZ
3456/* Handles hub class-specific requests */
3457static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3458 u16 wvalue, u16 windex, char *buf, u16 wlength)
3459{
3460 struct usb_hub_descriptor *hub_desc;
3461 int retval = 0;
3462 u32 hprt0;
3463 u32 port_status;
3464 u32 speed;
3465 u32 pcgctl;
3466
3467 switch (typereq) {
3468 case ClearHubFeature:
3469 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3470
3471 switch (wvalue) {
3472 case C_HUB_LOCAL_POWER:
3473 case C_HUB_OVER_CURRENT:
3474 /* Nothing required here */
3475 break;
3476
3477 default:
3478 retval = -EINVAL;
3479 dev_err(hsotg->dev,
3480 "ClearHubFeature request %1xh unknown\n",
3481 wvalue);
3482 }
3483 break;
3484
3485 case ClearPortFeature:
3486 if (wvalue != USB_PORT_FEAT_L1)
3487 if (!windex || windex > 1)
3488 goto error;
3489 switch (wvalue) {
3490 case USB_PORT_FEAT_ENABLE:
3491 dev_dbg(hsotg->dev,
3492 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3493 hprt0 = dwc2_read_hprt0(hsotg);
3494 hprt0 |= HPRT0_ENA;
95c8bc36 3495 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482
PZ
3496 break;
3497
3498 case USB_PORT_FEAT_SUSPEND:
3499 dev_dbg(hsotg->dev,
3500 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
b0bb9bb6 3501
bea78555
GH
3502 if (hsotg->bus_suspended)
3503 dwc2_port_resume(hsotg);
7359d482
PZ
3504 break;
3505
3506 case USB_PORT_FEAT_POWER:
3507 dev_dbg(hsotg->dev,
3508 "ClearPortFeature USB_PORT_FEAT_POWER\n");
3509 hprt0 = dwc2_read_hprt0(hsotg);
3510 hprt0 &= ~HPRT0_PWR;
95c8bc36 3511 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482
PZ
3512 break;
3513
3514 case USB_PORT_FEAT_INDICATOR:
3515 dev_dbg(hsotg->dev,
3516 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3517 /* Port indicator not supported */
3518 break;
3519
3520 case USB_PORT_FEAT_C_CONNECTION:
3521 /*
3522 * Clears driver's internal Connect Status Change flag
3523 */
3524 dev_dbg(hsotg->dev,
3525 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3526 hsotg->flags.b.port_connect_status_change = 0;
3527 break;
3528
3529 case USB_PORT_FEAT_C_RESET:
3530 /* Clears driver's internal Port Reset Change flag */
3531 dev_dbg(hsotg->dev,
3532 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3533 hsotg->flags.b.port_reset_change = 0;
3534 break;
3535
3536 case USB_PORT_FEAT_C_ENABLE:
3537 /*
3538 * Clears the driver's internal Port Enable/Disable
3539 * Change flag
3540 */
3541 dev_dbg(hsotg->dev,
3542 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3543 hsotg->flags.b.port_enable_change = 0;
3544 break;
3545
3546 case USB_PORT_FEAT_C_SUSPEND:
3547 /*
3548 * Clears the driver's internal Port Suspend Change
3549 * flag, which is set when resume signaling on the host
3550 * port is complete
3551 */
3552 dev_dbg(hsotg->dev,
3553 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3554 hsotg->flags.b.port_suspend_change = 0;
3555 break;
3556
3557 case USB_PORT_FEAT_C_PORT_L1:
3558 dev_dbg(hsotg->dev,
3559 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3560 hsotg->flags.b.port_l1_change = 0;
3561 break;
3562
3563 case USB_PORT_FEAT_C_OVER_CURRENT:
3564 dev_dbg(hsotg->dev,
3565 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3566 hsotg->flags.b.port_over_current_change = 0;
3567 break;
3568
3569 default:
3570 retval = -EINVAL;
3571 dev_err(hsotg->dev,
3572 "ClearPortFeature request %1xh unknown or unsupported\n",
3573 wvalue);
3574 }
3575 break;
3576
3577 case GetHubDescriptor:
3578 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3579 hub_desc = (struct usb_hub_descriptor *)buf;
3580 hub_desc->bDescLength = 9;
a5dd0395 3581 hub_desc->bDescriptorType = USB_DT_HUB;
7359d482 3582 hub_desc->bNbrPorts = 1;
3d040de8
SS
3583 hub_desc->wHubCharacteristics =
3584 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
3585 HUB_CHAR_INDV_PORT_OCPM);
7359d482
PZ
3586 hub_desc->bPwrOn2PwrGood = 1;
3587 hub_desc->bHubContrCurrent = 0;
3588 hub_desc->u.hs.DeviceRemovable[0] = 0;
3589 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3590 break;
3591
3592 case GetHubStatus:
3593 dev_dbg(hsotg->dev, "GetHubStatus\n");
3594 memset(buf, 0, 4);
3595 break;
3596
3597 case GetPortStatus:
b8313417
PZ
3598 dev_vdbg(hsotg->dev,
3599 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3600 hsotg->flags.d32);
7359d482
PZ
3601 if (!windex || windex > 1)
3602 goto error;
3603
3604 port_status = 0;
3605 if (hsotg->flags.b.port_connect_status_change)
3606 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3607 if (hsotg->flags.b.port_enable_change)
3608 port_status |= USB_PORT_STAT_C_ENABLE << 16;
3609 if (hsotg->flags.b.port_suspend_change)
3610 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3611 if (hsotg->flags.b.port_l1_change)
3612 port_status |= USB_PORT_STAT_C_L1 << 16;
3613 if (hsotg->flags.b.port_reset_change)
3614 port_status |= USB_PORT_STAT_C_RESET << 16;
3615 if (hsotg->flags.b.port_over_current_change) {
3616 dev_warn(hsotg->dev, "Overcurrent change detected\n");
3617 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3618 }
3619
3620 if (!hsotg->flags.b.port_connect_status) {
3621 /*
3622 * The port is disconnected, which means the core is
3623 * either in device mode or it soon will be. Just
3624 * return 0's for the remainder of the port status
3625 * since the port register can't be read if the core
3626 * is in device mode.
3627 */
3628 *(__le32 *)buf = cpu_to_le32(port_status);
3629 break;
3630 }
3631
95c8bc36 3632 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
b8313417 3633 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
7359d482
PZ
3634
3635 if (hprt0 & HPRT0_CONNSTS)
3636 port_status |= USB_PORT_STAT_CONNECTION;
3637 if (hprt0 & HPRT0_ENA)
3638 port_status |= USB_PORT_STAT_ENABLE;
3639 if (hprt0 & HPRT0_SUSP)
3640 port_status |= USB_PORT_STAT_SUSPEND;
3641 if (hprt0 & HPRT0_OVRCURRACT)
3642 port_status |= USB_PORT_STAT_OVERCURRENT;
3643 if (hprt0 & HPRT0_RST)
3644 port_status |= USB_PORT_STAT_RESET;
3645 if (hprt0 & HPRT0_PWR)
3646 port_status |= USB_PORT_STAT_POWER;
3647
f9234633 3648 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
7359d482
PZ
3649 if (speed == HPRT0_SPD_HIGH_SPEED)
3650 port_status |= USB_PORT_STAT_HIGH_SPEED;
3651 else if (speed == HPRT0_SPD_LOW_SPEED)
3652 port_status |= USB_PORT_STAT_LOW_SPEED;
3653
3654 if (hprt0 & HPRT0_TSTCTL_MASK)
3655 port_status |= USB_PORT_STAT_TEST;
3656 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3657
bea8e86c 3658 if (hsotg->params.dma_desc_fs_enable) {
fbb9e22b
MYK
3659 /*
3660 * Enable descriptor DMA only if a full speed
3661 * device is connected.
3662 */
3663 if (hsotg->new_connection &&
3664 ((port_status &
3665 (USB_PORT_STAT_CONNECTION |
3666 USB_PORT_STAT_HIGH_SPEED |
3667 USB_PORT_STAT_LOW_SPEED)) ==
3668 USB_PORT_STAT_CONNECTION)) {
3669 u32 hcfg;
3670
3671 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
95832c00 3672 hsotg->params.dma_desc_enable = true;
fbb9e22b
MYK
3673 hcfg = dwc2_readl(hsotg->regs + HCFG);
3674 hcfg |= HCFG_DESCDMA;
3675 dwc2_writel(hcfg, hsotg->regs + HCFG);
3676 hsotg->new_connection = false;
3677 }
3678 }
3679
b8313417 3680 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
7359d482
PZ
3681 *(__le32 *)buf = cpu_to_le32(port_status);
3682 break;
3683
3684 case SetHubFeature:
3685 dev_dbg(hsotg->dev, "SetHubFeature\n");
3686 /* No HUB features supported */
3687 break;
3688
3689 case SetPortFeature:
3690 dev_dbg(hsotg->dev, "SetPortFeature\n");
3691 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3692 goto error;
3693
3694 if (!hsotg->flags.b.port_connect_status) {
3695 /*
3696 * The port is disconnected, which means the core is
3697 * either in device mode or it soon will be. Just
3698 * return without doing anything since the port
3699 * register can't be written if the core is in device
3700 * mode.
3701 */
3702 break;
3703 }
3704
3705 switch (wvalue) {
3706 case USB_PORT_FEAT_SUSPEND:
3707 dev_dbg(hsotg->dev,
3708 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3709 if (windex != hsotg->otg_port)
3710 goto error;
3711 dwc2_port_suspend(hsotg, windex);
3712 break;
3713
3714 case USB_PORT_FEAT_POWER:
3715 dev_dbg(hsotg->dev,
3716 "SetPortFeature - USB_PORT_FEAT_POWER\n");
3717 hprt0 = dwc2_read_hprt0(hsotg);
3718 hprt0 |= HPRT0_PWR;
95c8bc36 3719 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482
PZ
3720 break;
3721
3722 case USB_PORT_FEAT_RESET:
3723 hprt0 = dwc2_read_hprt0(hsotg);
3724 dev_dbg(hsotg->dev,
3725 "SetPortFeature - USB_PORT_FEAT_RESET\n");
95c8bc36 3726 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
7359d482 3727 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
95c8bc36 3728 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
7359d482 3729 /* ??? Original driver does this */
95c8bc36 3730 dwc2_writel(0, hsotg->regs + PCGCTL);
7359d482
PZ
3731
3732 hprt0 = dwc2_read_hprt0(hsotg);
3733 /* Clear suspend bit if resetting from suspend state */
3734 hprt0 &= ~HPRT0_SUSP;
3735
3736 /*
3737 * When B-Host the Port reset bit is set in the Start
3738 * HCD Callback function, so that the reset is started
3739 * within 1ms of the HNP success interrupt
3740 */
3741 if (!dwc2_hcd_is_b_host(hsotg)) {
3742 hprt0 |= HPRT0_PWR | HPRT0_RST;
3743 dev_dbg(hsotg->dev,
3744 "In host mode, hprt0=%08x\n", hprt0);
95c8bc36 3745 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482
PZ
3746 }
3747
3748 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
04a9db79 3749 msleep(50);
7359d482 3750 hprt0 &= ~HPRT0_RST;
95c8bc36 3751 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482
PZ
3752 hsotg->lx_state = DWC2_L0; /* Now back to On state */
3753 break;
3754
3755 case USB_PORT_FEAT_INDICATOR:
3756 dev_dbg(hsotg->dev,
3757 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3758 /* Not supported */
3759 break;
3760
96d480e6
JL
3761 case USB_PORT_FEAT_TEST:
3762 hprt0 = dwc2_read_hprt0(hsotg);
3763 dev_dbg(hsotg->dev,
3764 "SetPortFeature - USB_PORT_FEAT_TEST\n");
3765 hprt0 &= ~HPRT0_TSTCTL_MASK;
3766 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
95c8bc36 3767 dwc2_writel(hprt0, hsotg->regs + HPRT0);
96d480e6
JL
3768 break;
3769
7359d482
PZ
3770 default:
3771 retval = -EINVAL;
3772 dev_err(hsotg->dev,
3773 "SetPortFeature %1xh unknown or unsupported\n",
3774 wvalue);
3775 break;
3776 }
3777 break;
3778
3779 default:
3780error:
3781 retval = -EINVAL;
3782 dev_dbg(hsotg->dev,
3783 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3784 typereq, windex, wvalue);
3785 break;
3786 }
3787
3788 return retval;
3789}
3790
3791static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3792{
3793 int retval;
3794
7359d482
PZ
3795 if (port != 1)
3796 return -EINVAL;
3797
3798 retval = (hsotg->flags.b.port_connect_status_change ||
3799 hsotg->flags.b.port_reset_change ||
3800 hsotg->flags.b.port_enable_change ||
3801 hsotg->flags.b.port_suspend_change ||
3802 hsotg->flags.b.port_over_current_change);
3803
3804 if (retval) {
3805 dev_dbg(hsotg->dev,
3806 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3807 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
3808 hsotg->flags.b.port_connect_status_change);
3809 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
3810 hsotg->flags.b.port_reset_change);
3811 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
3812 hsotg->flags.b.port_enable_change);
3813 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
3814 hsotg->flags.b.port_suspend_change);
3815 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
3816 hsotg->flags.b.port_over_current_change);
3817 }
3818
3819 return retval;
3820}
3821
3822int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3823{
95c8bc36 3824 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
7359d482
PZ
3825
3826#ifdef DWC2_DEBUG_SOF
3827 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
d6ec53e0 3828 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
7359d482 3829#endif
d6ec53e0 3830 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
7359d482
PZ
3831}
3832
fae4e826
DA
3833int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3834{
3835 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
3836 u32 hfir = dwc2_readl(hsotg->regs + HFIR);
3837 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3838 unsigned int us_per_frame;
3839 unsigned int frame_number;
3840 unsigned int remaining;
3841 unsigned int interval;
3842 unsigned int phy_clks;
3843
3844 /* High speed has 125 us per (micro) frame; others are 1 ms per */
3845 us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3846
3847 /* Extract fields */
3848 frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3849 remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3850 interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3851
3852 /*
3853 * Number of phy clocks since the last tick of the frame number after
3854 * "us" has passed.
3855 */
3856 phy_clks = (interval - remaining) +
3857 DIV_ROUND_UP(interval * us, us_per_frame);
3858
3859 return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3860}
3861
7359d482
PZ
3862int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3863{
6bf2e2a5 3864 return hsotg->op_state == OTG_STATE_B_HOST;
7359d482
PZ
3865}
3866
3867static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3868 int iso_desc_count,
3869 gfp_t mem_flags)
3870{
3871 struct dwc2_hcd_urb *urb;
3872 u32 size = sizeof(*urb) + iso_desc_count *
3873 sizeof(struct dwc2_hcd_iso_packet_desc);
3874
3875 urb = kzalloc(size, mem_flags);
3876 if (urb)
3877 urb->packet_count = iso_desc_count;
3878 return urb;
3879}
3880
3881static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3882 struct dwc2_hcd_urb *urb, u8 dev_addr,
3883 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
3884{
b49977a6
MK
3885 if (dbg_perio() ||
3886 ep_type == USB_ENDPOINT_XFER_BULK ||
3887 ep_type == USB_ENDPOINT_XFER_CONTROL)
3888 dev_vdbg(hsotg->dev,
3889 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
3890 dev_addr, ep_num, ep_dir, ep_type, mps);
7359d482
PZ
3891 urb->pipe_info.dev_addr = dev_addr;
3892 urb->pipe_info.ep_num = ep_num;
3893 urb->pipe_info.pipe_type = ep_type;
3894 urb->pipe_info.pipe_dir = ep_dir;
3895 urb->pipe_info.mps = mps;
3896}
3897
3898/*
3899 * NOTE: This function will be removed once the peripheral controller code
3900 * is integrated and the driver is stable
3901 */
3902void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3903{
3904#ifdef DEBUG
3905 struct dwc2_host_chan *chan;
3906 struct dwc2_hcd_urb *urb;
3907 struct dwc2_qtd *qtd;
3908 int num_channels;
3909 u32 np_tx_status;
3910 u32 p_tx_status;
3911 int i;
3912
bea8e86c 3913 num_channels = hsotg->params.host_channels;
7359d482
PZ
3914 dev_dbg(hsotg->dev, "\n");
3915 dev_dbg(hsotg->dev,
3916 "************************************************************\n");
3917 dev_dbg(hsotg->dev, "HCD State:\n");
3918 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
3919
3920 for (i = 0; i < num_channels; i++) {
3921 chan = hsotg->hc_ptr_array[i];
3922 dev_dbg(hsotg->dev, " Channel %d:\n", i);
3923 dev_dbg(hsotg->dev,
3924 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3925 chan->dev_addr, chan->ep_num, chan->ep_is_in);
3926 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
3927 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
3928 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
3929 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
3930 chan->data_pid_start);
3931 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
3932 dev_dbg(hsotg->dev, " xfer_started: %d\n",
3933 chan->xfer_started);
3934 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
3935 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
3936 (unsigned long)chan->xfer_dma);
3937 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
3938 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
3939 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
3940 chan->halt_on_queue);
3941 dev_dbg(hsotg->dev, " halt_pending: %d\n",
3942 chan->halt_pending);
3943 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
3944 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
3945 dev_dbg(hsotg->dev, " complete_split: %d\n",
3946 chan->complete_split);
3947 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
3948 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
3949 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
3950 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
3951 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
3952
3953 if (chan->xfer_started) {
3954 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3955
95c8bc36
AS
3956 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3957 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
3958 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
3959 hcint = dwc2_readl(hsotg->regs + HCINT(i));
3960 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
7359d482
PZ
3961 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
3962 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
3963 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
3964 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
3965 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
3966 }
3967
3968 if (!(chan->xfer_started && chan->qh))
3969 continue;
3970
3971 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3972 if (!qtd->in_process)
3973 break;
3974 urb = qtd->urb;
3975 dev_dbg(hsotg->dev, " URB Info:\n");
3976 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
3977 qtd, urb);
3978 if (urb) {
3979 dev_dbg(hsotg->dev,
3980 " Dev: %d, EP: %d %s\n",
3981 dwc2_hcd_get_dev_addr(&urb->pipe_info),
3982 dwc2_hcd_get_ep_num(&urb->pipe_info),
3983 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3984 "IN" : "OUT");
3985 dev_dbg(hsotg->dev,
3986 " Max packet size: %d\n",
3987 dwc2_hcd_get_mps(&urb->pipe_info));
3988 dev_dbg(hsotg->dev,
3989 " transfer_buffer: %p\n",
3990 urb->buf);
157dfaac
PZ
3991 dev_dbg(hsotg->dev,
3992 " transfer_dma: %08lx\n",
3993 (unsigned long)urb->dma);
7359d482
PZ
3994 dev_dbg(hsotg->dev,
3995 " transfer_buffer_length: %d\n",
3996 urb->length);
3997 dev_dbg(hsotg->dev, " actual_length: %d\n",
3998 urb->actual_length);
3999 }
4000 }
4001 }
4002
4003 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
4004 hsotg->non_periodic_channels);
4005 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
4006 hsotg->periodic_channels);
4007 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
95c8bc36 4008 np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
7359d482 4009 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
d6ec53e0 4010 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
7359d482 4011 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
d6ec53e0 4012 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
95c8bc36 4013 p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
7359d482 4014 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
d6ec53e0 4015 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
7359d482 4016 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
d6ec53e0 4017 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
7359d482
PZ
4018 dwc2_hcd_dump_frrem(hsotg);
4019 dwc2_dump_global_registers(hsotg);
4020 dwc2_dump_host_registers(hsotg);
4021 dev_dbg(hsotg->dev,
4022 "************************************************************\n");
4023 dev_dbg(hsotg->dev, "\n");
4024#endif
4025}
4026
4027/*
4028 * NOTE: This function will be removed once the peripheral controller code
4029 * is integrated and the driver is stable
4030 */
4031void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg)
4032{
4033#ifdef DWC2_DUMP_FRREM
4034 dev_dbg(hsotg->dev, "Frame remaining at SOF:\n");
4035 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4036 hsotg->frrem_samples, hsotg->frrem_accum,
4037 hsotg->frrem_samples > 0 ?
4038 hsotg->frrem_accum / hsotg->frrem_samples : 0);
4039 dev_dbg(hsotg->dev, "\n");
4040 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n");
4041 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4042 hsotg->hfnum_7_samples,
4043 hsotg->hfnum_7_frrem_accum,
4044 hsotg->hfnum_7_samples > 0 ?
4045 hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0);
4046 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n");
4047 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4048 hsotg->hfnum_0_samples,
4049 hsotg->hfnum_0_frrem_accum,
4050 hsotg->hfnum_0_samples > 0 ?
4051 hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0);
4052 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n");
4053 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4054 hsotg->hfnum_other_samples,
4055 hsotg->hfnum_other_frrem_accum,
4056 hsotg->hfnum_other_samples > 0 ?
4057 hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples :
4058 0);
4059 dev_dbg(hsotg->dev, "\n");
4060 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n");
4061 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4062 hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a,
4063 hsotg->hfnum_7_samples_a > 0 ?
4064 hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0);
4065 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n");
4066 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4067 hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a,
4068 hsotg->hfnum_0_samples_a > 0 ?
4069 hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0);
4070 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n");
4071 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4072 hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a,
4073 hsotg->hfnum_other_samples_a > 0 ?
4074 hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a
4075 : 0);
4076 dev_dbg(hsotg->dev, "\n");
4077 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n");
4078 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4079 hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b,
4080 hsotg->hfnum_7_samples_b > 0 ?
4081 hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0);
4082 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n");
4083 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4084 hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b,
4085 (hsotg->hfnum_0_samples_b > 0) ?
4086 hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0);
4087 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n");
4088 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4089 hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b,
4090 (hsotg->hfnum_other_samples_b > 0) ?
4091 hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b
4092 : 0);
4093#endif
4094}
4095
4096struct wrapper_priv_data {
4097 struct dwc2_hsotg *hsotg;
4098};
4099
4100/* Gets the dwc2_hsotg from a usb_hcd */
4101static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4102{
4103 struct wrapper_priv_data *p;
4104
9da51974 4105 p = (struct wrapper_priv_data *)&hcd->hcd_priv;
7359d482
PZ
4106 return p->hsotg;
4107}
4108
9f9f09b0
DA
4109/**
4110 * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
4111 *
4112 * This will get the dwc2_tt structure (and ttport) associated with the given
4113 * context (which is really just a struct urb pointer).
4114 *
4115 * The first time this is called for a given TT we allocate memory for our
4116 * structure. When everyone is done and has called dwc2_host_put_tt_info()
4117 * then the refcount for the structure will go to 0 and we'll free it.
4118 *
4119 * @hsotg: The HCD state structure for the DWC OTG controller.
4120 * @qh: The QH structure.
4121 * @context: The priv pointer from a struct dwc2_hcd_urb.
4122 * @mem_flags: Flags for allocating memory.
4123 * @ttport: We'll return this device's port number here. That's used to
4124 * reference into the bitmap if we're on a multi_tt hub.
4125 *
4126 * Return: a pointer to a struct dwc2_tt. Don't forget to call
4127 * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
4128 */
4129
4130struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
4131 gfp_t mem_flags, int *ttport)
4132{
4133 struct urb *urb = context;
4134 struct dwc2_tt *dwc_tt = NULL;
4135
4136 if (urb->dev->tt) {
4137 *ttport = urb->dev->ttport;
4138
4139 dwc_tt = urb->dev->tt->hcpriv;
9da51974 4140 if (!dwc_tt) {
9f9f09b0
DA
4141 size_t bitmap_size;
4142
4143 /*
4144 * For single_tt we need one schedule. For multi_tt
4145 * we need one per port.
4146 */
4147 bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
4148 sizeof(dwc_tt->periodic_bitmaps[0]);
4149 if (urb->dev->tt->multi)
4150 bitmap_size *= urb->dev->tt->hub->maxchild;
4151
4152 dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
4153 mem_flags);
9da51974 4154 if (!dwc_tt)
9f9f09b0
DA
4155 return NULL;
4156
4157 dwc_tt->usb_tt = urb->dev->tt;
4158 dwc_tt->usb_tt->hcpriv = dwc_tt;
4159 }
4160
4161 dwc_tt->refcount++;
4162 }
4163
4164 return dwc_tt;
4165}
4166
4167/**
4168 * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
4169 *
4170 * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
4171 * of the structure are done.
4172 *
4173 * It's OK to call this with NULL.
4174 *
4175 * @hsotg: The HCD state structure for the DWC OTG controller.
4176 * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
4177 */
4178void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
4179{
4180 /* Model kfree and make put of NULL a no-op */
9da51974 4181 if (!dwc_tt)
9f9f09b0
DA
4182 return;
4183
4184 WARN_ON(dwc_tt->refcount < 1);
4185
4186 dwc_tt->refcount--;
4187 if (!dwc_tt->refcount) {
4188 dwc_tt->usb_tt->hcpriv = NULL;
4189 kfree(dwc_tt);
4190 }
4191}
4192
7359d482
PZ
4193int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4194{
4195 struct urb *urb = context;
4196
4197 return urb->dev->speed;
4198}
4199
4200static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4201 struct urb *urb)
4202{
4203 struct usb_bus *bus = hcd_to_bus(hcd);
4204
4205 if (urb->interval)
4206 bus->bandwidth_allocated += bw / urb->interval;
4207 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4208 bus->bandwidth_isoc_reqs++;
4209 else
4210 bus->bandwidth_int_reqs++;
4211}
4212
4213static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4214 struct urb *urb)
4215{
4216 struct usb_bus *bus = hcd_to_bus(hcd);
4217
4218 if (urb->interval)
4219 bus->bandwidth_allocated -= bw / urb->interval;
4220 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4221 bus->bandwidth_isoc_reqs--;
4222 else
4223 bus->bandwidth_int_reqs--;
4224}
4225
4226/*
4227 * Sets the final status of an URB and returns it to the upper layer. Any
4228 * required cleanup of the URB is performed.
4229 *
4230 * Must be called with interrupt disabled and spinlock held
4231 */
0d012b98
PZ
4232void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4233 int status)
7359d482 4234{
0d012b98 4235 struct urb *urb;
7359d482
PZ
4236 int i;
4237
0d012b98
PZ
4238 if (!qtd) {
4239 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
7359d482
PZ
4240 return;
4241 }
4242
0d012b98
PZ
4243 if (!qtd->urb) {
4244 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
7359d482
PZ
4245 return;
4246 }
4247
0d012b98
PZ
4248 urb = qtd->urb->priv;
4249 if (!urb) {
4250 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
4251 return;
4252 }
4253
4254 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
7359d482 4255
b49977a6
MK
4256 if (dbg_urb(urb))
4257 dev_vdbg(hsotg->dev,
4258 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4259 __func__, urb, usb_pipedevice(urb->pipe),
4260 usb_pipeendpoint(urb->pipe),
4261 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4262 urb->actual_length);
7359d482 4263
7359d482 4264 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
0d012b98 4265 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
7359d482
PZ
4266 for (i = 0; i < urb->number_of_packets; ++i) {
4267 urb->iso_frame_desc[i].actual_length =
4268 dwc2_hcd_urb_get_iso_desc_actual_length(
0d012b98 4269 qtd->urb, i);
7359d482 4270 urb->iso_frame_desc[i].status =
0d012b98 4271 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
7359d482
PZ
4272 }
4273 }
4274
fe9b1773
GH
4275 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4276 for (i = 0; i < urb->number_of_packets; i++)
4277 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4278 i, urb->iso_frame_desc[i].status);
4279 }
4280
7359d482 4281 urb->status = status;
7359d482
PZ
4282 if (!status) {
4283 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4284 urb->actual_length < urb->transfer_buffer_length)
4285 urb->status = -EREMOTEIO;
4286 }
4287
4288 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4289 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4290 struct usb_host_endpoint *ep = urb->ep;
4291
4292 if (ep)
4293 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4294 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4295 urb);
4296 }
4297
c9e1c907 4298 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
0d012b98
PZ
4299 urb->hcpriv = NULL;
4300 kfree(qtd->urb);
4301 qtd->urb = NULL;
7359d482 4302
7359d482 4303 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
7359d482
PZ
4304}
4305
4306/*
4307 * Work queue function for starting the HCD when A-Cable is connected
4308 */
4309static void dwc2_hcd_start_func(struct work_struct *work)
4310{
4311 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4312 start_work.work);
4313
4314 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4315 dwc2_host_start(hsotg);
4316}
4317
4318/*
4319 * Reset work queue function
4320 */
4321static void dwc2_hcd_reset_func(struct work_struct *work)
4322{
4323 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4324 reset_work.work);
4a065c7b 4325 unsigned long flags;
7359d482
PZ
4326 u32 hprt0;
4327
4328 dev_dbg(hsotg->dev, "USB RESET function called\n");
4a065c7b
DA
4329
4330 spin_lock_irqsave(&hsotg->lock, flags);
4331
7359d482
PZ
4332 hprt0 = dwc2_read_hprt0(hsotg);
4333 hprt0 &= ~HPRT0_RST;
95c8bc36 4334 dwc2_writel(hprt0, hsotg->regs + HPRT0);
7359d482 4335 hsotg->flags.b.port_reset_change = 1;
4a065c7b
DA
4336
4337 spin_unlock_irqrestore(&hsotg->lock, flags);
7359d482
PZ
4338}
4339
4340/*
4341 * =========================================================================
4342 * Linux HC Driver Functions
4343 * =========================================================================
4344 */
4345
4346/*
4347 * Initializes the DWC_otg controller and its root hub and prepares it for host
4348 * mode operation. Activates the root port. Returns 0 on success and a negative
4349 * error code on failure.
4350 */
4351static int _dwc2_hcd_start(struct usb_hcd *hcd)
4352{
4353 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4354 struct usb_bus *bus = hcd_to_bus(hcd);
4355 unsigned long flags;
4356
4357 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4358
4359 spin_lock_irqsave(&hsotg->lock, flags);
31927b6b 4360 hsotg->lx_state = DWC2_L0;
7359d482 4361 hcd->state = HC_STATE_RUNNING;
31927b6b 4362 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
7359d482
PZ
4363
4364 if (dwc2_is_device_mode(hsotg)) {
4365 spin_unlock_irqrestore(&hsotg->lock, flags);
4366 return 0; /* why 0 ?? */
4367 }
4368
4369 dwc2_hcd_reinit(hsotg);
4370
4371 /* Initialize and connect root hub if one is not already attached */
4372 if (bus->root_hub) {
4373 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4374 /* Inform the HUB driver to resume */
4375 usb_hcd_resume_root_hub(hcd);
4376 }
4377
4378 spin_unlock_irqrestore(&hsotg->lock, flags);
4379 return 0;
4380}
4381
4382/*
4383 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4384 * stopped.
4385 */
4386static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4387{
4388 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4389 unsigned long flags;
4390
5bbf6ce0
GH
4391 /* Turn off all host-specific interrupts */
4392 dwc2_disable_host_interrupts(hsotg);
4393
091473ad
GH
4394 /* Wait for interrupt processing to finish */
4395 synchronize_irq(hcd->irq);
4396
7359d482 4397 spin_lock_irqsave(&hsotg->lock, flags);
091473ad 4398 /* Ensure hcd is disconnected */
6a659531 4399 dwc2_hcd_disconnect(hsotg, true);
7359d482 4400 dwc2_hcd_stop(hsotg);
31927b6b
GH
4401 hsotg->lx_state = DWC2_L3;
4402 hcd->state = HC_STATE_HALT;
4403 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
7359d482
PZ
4404 spin_unlock_irqrestore(&hsotg->lock, flags);
4405
4406 usleep_range(1000, 3000);
4407}
4408
99a65798
GH
4409static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
4410{
4411 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
a2a23d3f
GH
4412 unsigned long flags;
4413 int ret = 0;
4414 u32 hprt0;
4415
4416 spin_lock_irqsave(&hsotg->lock, flags);
4417
f367b72c
MD
4418 if (dwc2_is_device_mode(hsotg))
4419 goto unlock;
4420
a2a23d3f
GH
4421 if (hsotg->lx_state != DWC2_L0)
4422 goto unlock;
4423
4424 if (!HCD_HW_ACCESSIBLE(hcd))
4425 goto unlock;
4426
866932e2
JS
4427 if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4428 goto unlock;
4429
bea8e86c 4430 if (!hsotg->params.hibernation)
a2a23d3f
GH
4431 goto skip_power_saving;
4432
4433 /*
4434 * Drive USB suspend and disable port Power
4435 * if usb bus is not suspended.
4436 */
4437 if (!hsotg->bus_suspended) {
4438 hprt0 = dwc2_read_hprt0(hsotg);
4439 hprt0 |= HPRT0_SUSP;
4440 hprt0 &= ~HPRT0_PWR;
4441 dwc2_writel(hprt0, hsotg->regs + HPRT0);
4442 }
4443
4444 /* Enter hibernation */
4445 ret = dwc2_enter_hibernation(hsotg);
4446 if (ret) {
4447 if (ret != -ENOTSUPP)
4448 dev_err(hsotg->dev,
4449 "enter hibernation failed\n");
4450 goto skip_power_saving;
4451 }
4452
4453 /* Ask phy to be suspended */
4454 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4455 spin_unlock_irqrestore(&hsotg->lock, flags);
4456 usb_phy_set_suspend(hsotg->uphy, true);
4457 spin_lock_irqsave(&hsotg->lock, flags);
4458 }
4459
4460 /* After entering hibernation, hardware is no more accessible */
4461 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
99a65798 4462
a2a23d3f 4463skip_power_saving:
99a65798 4464 hsotg->lx_state = DWC2_L2;
a2a23d3f
GH
4465unlock:
4466 spin_unlock_irqrestore(&hsotg->lock, flags);
4467
4468 return ret;
99a65798
GH
4469}
4470
4471static int _dwc2_hcd_resume(struct usb_hcd *hcd)
4472{
4473 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
a2a23d3f
GH
4474 unsigned long flags;
4475 int ret = 0;
4476
4477 spin_lock_irqsave(&hsotg->lock, flags);
4478
f367b72c
MD
4479 if (dwc2_is_device_mode(hsotg))
4480 goto unlock;
4481
a2a23d3f
GH
4482 if (hsotg->lx_state != DWC2_L2)
4483 goto unlock;
4484
bea8e86c 4485 if (!hsotg->params.hibernation) {
a2a23d3f
GH
4486 hsotg->lx_state = DWC2_L0;
4487 goto unlock;
4488 }
4489
4490 /*
4491 * Set HW accessible bit before powering on the controller
4492 * since an interrupt may rise.
4493 */
4494 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4495
4496 /*
4497 * Enable power if not already done.
4498 * This must not be spinlocked since duration
4499 * of this call is unknown.
4500 */
4501 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4502 spin_unlock_irqrestore(&hsotg->lock, flags);
4503 usb_phy_set_suspend(hsotg->uphy, false);
4504 spin_lock_irqsave(&hsotg->lock, flags);
4505 }
4506
4507 /* Exit hibernation */
4508 ret = dwc2_exit_hibernation(hsotg, true);
4509 if (ret && (ret != -ENOTSUPP))
4510 dev_err(hsotg->dev, "exit hibernation failed\n");
99a65798
GH
4511
4512 hsotg->lx_state = DWC2_L0;
a2a23d3f
GH
4513
4514 spin_unlock_irqrestore(&hsotg->lock, flags);
4515
4516 if (hsotg->bus_suspended) {
4517 spin_lock_irqsave(&hsotg->lock, flags);
4518 hsotg->flags.b.port_suspend_change = 1;
4519 spin_unlock_irqrestore(&hsotg->lock, flags);
4520 dwc2_port_resume(hsotg);
4521 } else {
5634e016
GH
4522 /* Wait for controller to correctly update D+/D- level */
4523 usleep_range(3000, 5000);
4524
a2a23d3f
GH
4525 /*
4526 * Clear Port Enable and Port Status changes.
4527 * Enable Port Power.
4528 */
4529 dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
4530 HPRT0_ENACHG, hsotg->regs + HPRT0);
4531 /* Wait for controller to detect Port Connect */
5634e016 4532 usleep_range(5000, 7000);
a2a23d3f
GH
4533 }
4534
4535 return ret;
4536unlock:
4537 spin_unlock_irqrestore(&hsotg->lock, flags);
4538
4539 return ret;
99a65798
GH
4540}
4541
7359d482
PZ
4542/* Returns the current frame number */
4543static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4544{
4545 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4546
4547 return dwc2_hcd_get_frame_number(hsotg);
4548}
4549
4550static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4551 char *fn_name)
4552{
4553#ifdef VERBOSE_DEBUG
4554 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
efe357f4
NMG
4555 char *pipetype = NULL;
4556 char *speed = NULL;
7359d482
PZ
4557
4558 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4559 dev_vdbg(hsotg->dev, " Device address: %d\n",
4560 usb_pipedevice(urb->pipe));
4561 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
4562 usb_pipeendpoint(urb->pipe),
4563 usb_pipein(urb->pipe) ? "IN" : "OUT");
4564
4565 switch (usb_pipetype(urb->pipe)) {
4566 case PIPE_CONTROL:
4567 pipetype = "CONTROL";
4568 break;
4569 case PIPE_BULK:
4570 pipetype = "BULK";
4571 break;
4572 case PIPE_INTERRUPT:
4573 pipetype = "INTERRUPT";
4574 break;
4575 case PIPE_ISOCHRONOUS:
4576 pipetype = "ISOCHRONOUS";
4577 break;
7359d482
PZ
4578 }
4579
4580 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
4581 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4582 "IN" : "OUT");
4583
4584 switch (urb->dev->speed) {
4585 case USB_SPEED_HIGH:
4586 speed = "HIGH";
4587 break;
4588 case USB_SPEED_FULL:
4589 speed = "FULL";
4590 break;
4591 case USB_SPEED_LOW:
4592 speed = "LOW";
4593 break;
4594 default:
4595 speed = "UNKNOWN";
4596 break;
4597 }
4598
4599 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
4600 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
4601 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
4602 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
4603 urb->transfer_buffer_length);
157dfaac
PZ
4604 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
4605 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4606 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
4607 urb->setup_packet, (unsigned long)urb->setup_dma);
7359d482
PZ
4608 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
4609
4610 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4611 int i;
4612
4613 for (i = 0; i < urb->number_of_packets; i++) {
4614 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
4615 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
4616 urb->iso_frame_desc[i].offset,
4617 urb->iso_frame_desc[i].length);
4618 }
4619 }
4620#endif
4621}
4622
4623/*
4624 * Starts processing a USB transfer request specified by a USB Request Block
4625 * (URB). mem_flags indicates the type of memory allocation to use while
4626 * processing this URB.
4627 */
4628static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4629 gfp_t mem_flags)
4630{
4631 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4632 struct usb_host_endpoint *ep = urb->ep;
4633 struct dwc2_hcd_urb *dwc2_urb;
4634 int i;
c9e1c907 4635 int retval;
7359d482 4636 int alloc_bandwidth = 0;
7359d482
PZ
4637 u8 ep_type = 0;
4638 u32 tflags = 0;
4639 void *buf;
4640 unsigned long flags;
b58e6cee
MYK
4641 struct dwc2_qh *qh;
4642 bool qh_allocated = false;
b5a468a6 4643 struct dwc2_qtd *qtd;
7359d482 4644
b49977a6
MK
4645 if (dbg_urb(urb)) {
4646 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4647 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4648 }
7359d482 4649
9da51974 4650 if (!ep)
7359d482
PZ
4651 return -EINVAL;
4652
4653 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4654 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4655 spin_lock_irqsave(&hsotg->lock, flags);
4656 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4657 alloc_bandwidth = 1;
4658 spin_unlock_irqrestore(&hsotg->lock, flags);
4659 }
4660
4661 switch (usb_pipetype(urb->pipe)) {
4662 case PIPE_CONTROL:
4663 ep_type = USB_ENDPOINT_XFER_CONTROL;
4664 break;
4665 case PIPE_ISOCHRONOUS:
4666 ep_type = USB_ENDPOINT_XFER_ISOC;
4667 break;
4668 case PIPE_BULK:
4669 ep_type = USB_ENDPOINT_XFER_BULK;
4670 break;
4671 case PIPE_INTERRUPT:
4672 ep_type = USB_ENDPOINT_XFER_INT;
4673 break;
7359d482
PZ
4674 }
4675
4676 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4677 mem_flags);
4678 if (!dwc2_urb)
4679 return -ENOMEM;
4680
4681 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4682 usb_pipeendpoint(urb->pipe), ep_type,
4683 usb_pipein(urb->pipe),
4684 usb_maxpacket(urb->dev, urb->pipe,
4685 !(usb_pipein(urb->pipe))));
4686
4687 buf = urb->transfer_buffer;
25a49445 4688
7359d482 4689 if (hcd->self.uses_dma) {
25a49445
PZ
4690 if (!buf && (urb->transfer_dma & 3)) {
4691 dev_err(hsotg->dev,
4692 "%s: unaligned transfer with no transfer_buffer",
4693 __func__);
4694 retval = -EINVAL;
33ad261a 4695 goto fail0;
25a49445 4696 }
7359d482
PZ
4697 }
4698
4699 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4700 tflags |= URB_GIVEBACK_ASAP;
4701 if (urb->transfer_flags & URB_ZERO_PACKET)
4702 tflags |= URB_SEND_ZERO_PACKET;
4703
4704 dwc2_urb->priv = urb;
4705 dwc2_urb->buf = buf;
4706 dwc2_urb->dma = urb->transfer_dma;
4707 dwc2_urb->length = urb->transfer_buffer_length;
4708 dwc2_urb->setup_packet = urb->setup_packet;
4709 dwc2_urb->setup_dma = urb->setup_dma;
4710 dwc2_urb->flags = tflags;
4711 dwc2_urb->interval = urb->interval;
4712 dwc2_urb->status = -EINPROGRESS;
4713
4714 for (i = 0; i < urb->number_of_packets; ++i)
4715 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4716 urb->iso_frame_desc[i].offset,
4717 urb->iso_frame_desc[i].length);
4718
4719 urb->hcpriv = dwc2_urb;
9da51974 4720 qh = (struct dwc2_qh *)ep->hcpriv;
b58e6cee
MYK
4721 /* Create QH for the endpoint if it doesn't exist */
4722 if (!qh) {
4723 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4724 if (!qh) {
4725 retval = -ENOMEM;
4726 goto fail0;
4727 }
4728 ep->hcpriv = qh;
4729 qh_allocated = true;
4730 }
c9e1c907 4731
b5a468a6
MYK
4732 qtd = kzalloc(sizeof(*qtd), mem_flags);
4733 if (!qtd) {
4734 retval = -ENOMEM;
4735 goto fail1;
4736 }
4737
c9e1c907
PZ
4738 spin_lock_irqsave(&hsotg->lock, flags);
4739 retval = usb_hcd_link_urb_to_ep(hcd, urb);
c9e1c907 4740 if (retval)
b5a468a6 4741 goto fail2;
c9e1c907 4742
b5a468a6 4743 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
c9e1c907 4744 if (retval)
b5a468a6 4745 goto fail3;
c9e1c907
PZ
4746
4747 if (alloc_bandwidth) {
c9e1c907
PZ
4748 dwc2_allocate_bus_bandwidth(hcd,
4749 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4750 urb);
7359d482
PZ
4751 }
4752
33ad261a
GH
4753 spin_unlock_irqrestore(&hsotg->lock, flags);
4754
c9e1c907
PZ
4755 return 0;
4756
b5a468a6 4757fail3:
c9e1c907
PZ
4758 dwc2_urb->priv = NULL;
4759 usb_hcd_unlink_urb_from_ep(hcd, urb);
16e80218
DA
4760 if (qh_allocated && qh->channel && qh->channel->qh == qh)
4761 qh->channel->qh = NULL;
b5a468a6 4762fail2:
33ad261a 4763 spin_unlock_irqrestore(&hsotg->lock, flags);
c9e1c907 4764 urb->hcpriv = NULL;
b5a468a6 4765 kfree(qtd);
b0d65902 4766 qtd = NULL;
b5a468a6 4767fail1:
b58e6cee
MYK
4768 if (qh_allocated) {
4769 struct dwc2_qtd *qtd2, *qtd2_tmp;
4770
4771 ep->hcpriv = NULL;
4772 dwc2_hcd_qh_unlink(hsotg, qh);
4773 /* Free each QTD in the QH's QTD list */
4774 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
9da51974 4775 qtd_list_entry)
b58e6cee
MYK
4776 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4777 dwc2_hcd_qh_free(hsotg, qh);
4778 }
33ad261a 4779fail0:
c9e1c907
PZ
4780 kfree(dwc2_urb);
4781
7359d482
PZ
4782 return retval;
4783}
4784
4785/*
4786 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4787 */
4788static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4789 int status)
4790{
4791 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
c9e1c907 4792 int rc;
7359d482
PZ
4793 unsigned long flags;
4794
4795 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4796 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4797
4798 spin_lock_irqsave(&hsotg->lock, flags);
4799
c9e1c907
PZ
4800 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4801 if (rc)
4802 goto out;
4803
7359d482
PZ
4804 if (!urb->hcpriv) {
4805 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4806 goto out;
4807 }
4808
4809 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4810
c9e1c907
PZ
4811 usb_hcd_unlink_urb_from_ep(hcd, urb);
4812
7359d482
PZ
4813 kfree(urb->hcpriv);
4814 urb->hcpriv = NULL;
4815
4816 /* Higher layer software sets URB status */
4817 spin_unlock(&hsotg->lock);
4818 usb_hcd_giveback_urb(hcd, urb, status);
4819 spin_lock(&hsotg->lock);
4820
4821 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4822 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
4823out:
4824 spin_unlock_irqrestore(&hsotg->lock, flags);
4825
4826 return rc;
4827}
4828
4829/*
4830 * Frees resources in the DWC_otg controller related to a given endpoint. Also
4831 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4832 * must already be dequeued.
4833 */
4834static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4835 struct usb_host_endpoint *ep)
4836{
4837 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4838
4839 dev_dbg(hsotg->dev,
4840 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4841 ep->desc.bEndpointAddress, ep->hcpriv);
4842 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4843}
4844
4845/*
4846 * Resets endpoint specific parameter values, in current version used to reset
4847 * the data toggle (as a WA). This function can be called from usb_clear_halt
4848 * routine.
4849 */
4850static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4851 struct usb_host_endpoint *ep)
4852{
4853 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
7359d482
PZ
4854 unsigned long flags;
4855
4856 dev_dbg(hsotg->dev,
4857 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4858 ep->desc.bEndpointAddress);
4859
7359d482 4860 spin_lock_irqsave(&hsotg->lock, flags);
7359d482 4861 dwc2_hcd_endpoint_reset(hsotg, ep);
7359d482
PZ
4862 spin_unlock_irqrestore(&hsotg->lock, flags);
4863}
4864
4865/*
4866 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4867 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4868 * interrupt.
4869 *
4870 * This function is called by the USB core when an interrupt occurs
4871 */
4872static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4873{
4874 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
7359d482 4875
ca18f4a6 4876 return dwc2_handle_hcd_intr(hsotg);
7359d482
PZ
4877}
4878
4879/*
4880 * Creates Status Change bitmap for the root hub and root port. The bitmap is
4881 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4882 * is the status change indicator for the single root port. Returns 1 if either
4883 * change indicator is 1, otherwise returns 0.
4884 */
4885static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4886{
4887 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4888
4889 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4890 return buf[0] != 0;
4891}
4892
4893/* Handles hub class-specific requests */
4894static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4895 u16 windex, char *buf, u16 wlength)
4896{
4897 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4898 wvalue, windex, buf, wlength);
4899 return retval;
4900}
4901
4902/* Handles hub TT buffer clear completions */
4903static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4904 struct usb_host_endpoint *ep)
4905{
4906 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4907 struct dwc2_qh *qh;
4908 unsigned long flags;
4909
4910 qh = ep->hcpriv;
4911 if (!qh)
4912 return;
4913
4914 spin_lock_irqsave(&hsotg->lock, flags);
4915 qh->tt_buffer_dirty = 0;
4916
4917 if (hsotg->flags.b.port_connect_status)
4918 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4919
4920 spin_unlock_irqrestore(&hsotg->lock, flags);
4921}
4922
ca8b0332
CY
4923/*
4924 * HPRT0_SPD_HIGH_SPEED: high speed
4925 * HPRT0_SPD_FULL_SPEED: full speed
4926 */
4927static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4928{
4929 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4930
4931 if (hsotg->params.speed == speed)
4932 return;
4933
4934 hsotg->params.speed = speed;
4935 queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4936}
4937
4938static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4939{
4940 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4941
4942 if (!hsotg->params.change_speed_quirk)
4943 return;
4944
4945 /*
4946 * On removal, set speed to default high-speed.
4947 */
4948 if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4949 udev->parent->speed < USB_SPEED_HIGH) {
4950 dev_info(hsotg->dev, "Set speed to default high-speed\n");
4951 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4952 }
4953}
4954
4955static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4956{
4957 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4958
4959 if (!hsotg->params.change_speed_quirk)
4960 return 0;
4961
4962 if (udev->speed == USB_SPEED_HIGH) {
4963 dev_info(hsotg->dev, "Set speed to high-speed\n");
4964 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4965 } else if ((udev->speed == USB_SPEED_FULL ||
4966 udev->speed == USB_SPEED_LOW)) {
4967 /*
4968 * Change speed setting to full-speed if there's
4969 * a full-speed or low-speed device plugged in.
4970 */
4971 dev_info(hsotg->dev, "Set speed to full-speed\n");
4972 dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4973 }
4974
4975 return 0;
4976}
4977
7359d482
PZ
4978static struct hc_driver dwc2_hc_driver = {
4979 .description = "dwc2_hsotg",
4980 .product_desc = "DWC OTG Controller",
4981 .hcd_priv_size = sizeof(struct wrapper_priv_data),
4982
4983 .irq = _dwc2_hcd_irq,
8add17cf 4984 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
7359d482
PZ
4985
4986 .start = _dwc2_hcd_start,
4987 .stop = _dwc2_hcd_stop,
4988 .urb_enqueue = _dwc2_hcd_urb_enqueue,
4989 .urb_dequeue = _dwc2_hcd_urb_dequeue,
4990 .endpoint_disable = _dwc2_hcd_endpoint_disable,
4991 .endpoint_reset = _dwc2_hcd_endpoint_reset,
4992 .get_frame_number = _dwc2_hcd_get_frame_number,
4993
4994 .hub_status_data = _dwc2_hcd_hub_status_data,
4995 .hub_control = _dwc2_hcd_hub_control,
4996 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
99a65798
GH
4997
4998 .bus_suspend = _dwc2_hcd_suspend,
4999 .bus_resume = _dwc2_hcd_resume,
3bc04e28
DA
5000
5001 .map_urb_for_dma = dwc2_map_urb_for_dma,
5002 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
7359d482
PZ
5003};
5004
5005/*
5006 * Frees secondary storage associated with the dwc2_hsotg structure contained
5007 * in the struct usb_hcd field
5008 */
5009static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
5010{
5011 u32 ahbcfg;
5012 u32 dctl;
5013 int i;
5014
5015 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
5016
5017 /* Free memory for QH/QTD lists */
5018 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
5019 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
5020 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
5021 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
5022 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
5023 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
5024
5025 /* Free memory for the host channels */
5026 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
5027 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
5028
9da51974 5029 if (chan) {
7359d482
PZ
5030 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
5031 i, chan);
5032 hsotg->hc_ptr_array[i] = NULL;
5033 kfree(chan);
5034 }
5035 }
5036
95832c00 5037 if (hsotg->params.host_dma) {
7359d482
PZ
5038 if (hsotg->status_buf) {
5039 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
5040 hsotg->status_buf,
5041 hsotg->status_buf_dma);
5042 hsotg->status_buf = NULL;
5043 }
5044 } else {
5045 kfree(hsotg->status_buf);
5046 hsotg->status_buf = NULL;
5047 }
5048
95c8bc36 5049 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
7359d482
PZ
5050
5051 /* Disable all interrupts */
5052 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
95c8bc36
AS
5053 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
5054 dwc2_writel(0, hsotg->regs + GINTMSK);
7359d482 5055
9badec2f 5056 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
95c8bc36 5057 dctl = dwc2_readl(hsotg->regs + DCTL);
7359d482 5058 dctl |= DCTL_SFTDISCON;
95c8bc36 5059 dwc2_writel(dctl, hsotg->regs + DCTL);
7359d482
PZ
5060 }
5061
5062 if (hsotg->wq_otg) {
5063 if (!cancel_work_sync(&hsotg->wf_otg))
5064 flush_workqueue(hsotg->wq_otg);
5065 destroy_workqueue(hsotg->wq_otg);
5066 }
5067
7359d482
PZ
5068 del_timer(&hsotg->wkp_timer);
5069}
5070
5071static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5072{
5073 /* Turn off all host-specific interrupts */
5074 dwc2_disable_host_interrupts(hsotg);
5075
5076 dwc2_hcd_free(hsotg);
5077}
5078
7359d482
PZ
5079/*
5080 * Initializes the HCD. This function allocates memory for and initializes the
5081 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5082 * USB bus with the core and calls the hc_driver->start() function. It returns
5083 * a negative error on failure.
5084 */
4fe160d5 5085int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
7359d482 5086{
348becdc
HK
5087 struct platform_device *pdev = to_platform_device(hsotg->dev);
5088 struct resource *res;
7359d482
PZ
5089 struct usb_hcd *hcd;
5090 struct dwc2_host_chan *channel;
9badec2f 5091 u32 hcfg;
7359d482 5092 int i, num_channels;
9badec2f 5093 int retval;
7359d482 5094
f5500ecc
DN
5095 if (usb_disabled())
5096 return -ENODEV;
5097
e62662c7 5098 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
7359d482 5099
9badec2f 5100 retval = -ENOMEM;
7359d482 5101
95c8bc36 5102 hcfg = dwc2_readl(hsotg->regs + HCFG);
7359d482 5103 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
7359d482
PZ
5104
5105#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5106 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
5107 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5108 if (!hsotg->frame_num_array)
ba0e60d1 5109 goto error1;
7359d482
PZ
5110 hsotg->last_frame_num_array = kzalloc(
5111 sizeof(*hsotg->last_frame_num_array) *
5112 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5113 if (!hsotg->last_frame_num_array)
ba0e60d1 5114 goto error1;
7359d482 5115#endif
483bb254 5116 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
7359d482 5117
a0112f48 5118 /* Check if the bus driver or platform code has setup a dma_mask */
95832c00 5119 if (hsotg->params.host_dma &&
9da51974 5120 !hsotg->dev->dma_mask) {
a0112f48
MK
5121 dev_warn(hsotg->dev,
5122 "dma_mask not set, disabling DMA\n");
fdb09b3e 5123 hsotg->params.host_dma = false;
95832c00 5124 hsotg->params.dma_desc_enable = false;
a0112f48
MK
5125 }
5126
ba0e60d1 5127 /* Set device flags indicating whether the HCD supports DMA */
95832c00 5128 if (hsotg->params.host_dma) {
30885313
PZ
5129 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5130 dev_warn(hsotg->dev, "can't set DMA mask\n");
25a49445
PZ
5131 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5132 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
ba0e60d1
PZ
5133 }
5134
ca8b0332
CY
5135 if (hsotg->params.change_speed_quirk) {
5136 dwc2_hc_driver.free_dev = dwc2_free_dev;
5137 dwc2_hc_driver.reset_device = dwc2_reset_device;
5138 }
5139
ba0e60d1
PZ
5140 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5141 if (!hcd)
5142 goto error1;
5143
95832c00 5144 if (!hsotg->params.host_dma)
7de76ee1
MK
5145 hcd->self.uses_dma = 0;
5146
ba0e60d1
PZ
5147 hcd->has_tt = 1;
5148
348becdc
HK
5149 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5150 hcd->rsrc_start = res->start;
5151 hcd->rsrc_len = resource_size(res);
5152
9da51974 5153 ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
ba0e60d1
PZ
5154 hsotg->priv = hcd;
5155
7359d482
PZ
5156 /*
5157 * Disable the global interrupt until all the interrupt handlers are
5158 * installed
5159 */
5160 dwc2_disable_global_interrupts(hsotg);
5161
6706c721 5162 /* Initialize the DWC_otg core, and select the Phy type */
0fe239bc 5163 retval = dwc2_core_init(hsotg, true);
6706c721
MK
5164 if (retval)
5165 goto error2;
5166
7359d482 5167 /* Create new workqueue and init work */
53510352 5168 retval = -ENOMEM;
ec7b1268 5169 hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
7359d482
PZ
5170 if (!hsotg->wq_otg) {
5171 dev_err(hsotg->dev, "Failed to create workqueue\n");
5172 goto error2;
5173 }
5174 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5175
e99e88a9 5176 timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
7359d482
PZ
5177
5178 /* Initialize the non-periodic schedule */
5179 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
5180 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5181
5182 /* Initialize the periodic schedule */
5183 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5184 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5185 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5186 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5187
c9c8ac01
DA
5188 INIT_LIST_HEAD(&hsotg->split_order);
5189
7359d482
PZ
5190 /*
5191 * Create a host channel descriptor for each host channel implemented
5192 * in the controller. Initialize the channel descriptor array.
5193 */
5194 INIT_LIST_HEAD(&hsotg->free_hc_list);
bea8e86c 5195 num_channels = hsotg->params.host_channels;
7359d482
PZ
5196 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5197
5198 for (i = 0; i < num_channels; i++) {
5199 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
9da51974 5200 if (!channel)
7359d482
PZ
5201 goto error3;
5202 channel->hc_num = i;
c9c8ac01 5203 INIT_LIST_HEAD(&channel->split_order_list_entry);
7359d482
PZ
5204 hsotg->hc_ptr_array[i] = channel;
5205 }
5206
5207 /* Initialize hsotg start work */
5208 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5209
5210 /* Initialize port reset work */
5211 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5212
5213 /*
5214 * Allocate space for storing data on status transactions. Normally no
5215 * data is sent, but this space acts as a bit bucket. This must be
5216 * done after usb_add_hcd since that function allocates the DMA buffer
5217 * pool.
5218 */
95832c00 5219 if (hsotg->params.host_dma)
7359d482
PZ
5220 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5221 DWC2_HCD_STATUS_BUF_SIZE,
5222 &hsotg->status_buf_dma, GFP_KERNEL);
5223 else
5224 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5225 GFP_KERNEL);
5226
5227 if (!hsotg->status_buf)
5228 goto error3;
5229
3b5fcc9a
GH
5230 /*
5231 * Create kmem caches to handle descriptor buffers in descriptor
5232 * DMA mode.
5233 * Alignment must be set to 512 bytes.
5234 */
bea8e86c
JY
5235 if (hsotg->params.dma_desc_enable ||
5236 hsotg->params.dma_desc_fs_enable) {
3b5fcc9a 5237 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
ec703251 5238 sizeof(struct dwc2_dma_desc) *
3b5fcc9a
GH
5239 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
5240 NULL);
5241 if (!hsotg->desc_gen_cache) {
5242 dev_err(hsotg->dev,
5243 "unable to create dwc2 generic desc cache\n");
5244
5245 /*
5246 * Disable descriptor dma mode since it will not be
5247 * usable.
5248 */
95832c00
JY
5249 hsotg->params.dma_desc_enable = false;
5250 hsotg->params.dma_desc_fs_enable = false;
3b5fcc9a
GH
5251 }
5252
5253 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
ec703251 5254 sizeof(struct dwc2_dma_desc) *
3b5fcc9a
GH
5255 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
5256 if (!hsotg->desc_hsisoc_cache) {
5257 dev_err(hsotg->dev,
5258 "unable to create dwc2 hs isoc desc cache\n");
5259
5260 kmem_cache_destroy(hsotg->desc_gen_cache);
5261
5262 /*
5263 * Disable descriptor dma mode since it will not be
5264 * usable.
5265 */
95832c00
JY
5266 hsotg->params.dma_desc_enable = false;
5267 hsotg->params.dma_desc_fs_enable = false;
3b5fcc9a
GH
5268 }
5269 }
5270
7359d482
PZ
5271 hsotg->otg_port = 1;
5272 hsotg->frame_list = NULL;
5273 hsotg->frame_list_dma = 0;
5274 hsotg->periodic_qh_count = 0;
5275
5276 /* Initiate lx_state to L3 disconnected state */
5277 hsotg->lx_state = DWC2_L3;
5278
5279 hcd->self.otg_port = hsotg->otg_port;
5280
5281 /* Don't support SG list at this point */
5282 hcd->self.sg_tablesize = 0;
5283
9df4ceac
MYK
5284 if (!IS_ERR_OR_NULL(hsotg->uphy))
5285 otg_set_host(hsotg->uphy->otg, &hcd->self);
5286
7359d482
PZ
5287 /*
5288 * Finish generic HCD initialization and start the HCD. This function
5289 * allocates the DMA buffer pool, registers the USB bus, requests the
5290 * IRQ line, and calls hcd_start method.
5291 */
4fe160d5 5292 retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
7359d482 5293 if (retval < 0)
3b5fcc9a 5294 goto error4;
7359d482 5295
3c9740a1
PC
5296 device_wakeup_enable(hcd->self.controller);
5297
7359d482
PZ
5298 dwc2_hcd_dump_state(hsotg);
5299
5300 dwc2_enable_global_interrupts(hsotg);
5301
5302 return 0;
5303
3b5fcc9a
GH
5304error4:
5305 kmem_cache_destroy(hsotg->desc_gen_cache);
5306 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
7359d482
PZ
5307error3:
5308 dwc2_hcd_release(hsotg);
5309error2:
ba0e60d1
PZ
5310 usb_put_hcd(hcd);
5311error1:
7359d482
PZ
5312
5313#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5314 kfree(hsotg->last_frame_num_array);
5315 kfree(hsotg->frame_num_array);
5316#endif
5317
e62662c7 5318 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
7359d482
PZ
5319 return retval;
5320}
7359d482
PZ
5321
5322/*
5323 * Removes the HCD.
5324 * Frees memory and resources associated with the HCD and deregisters the bus.
5325 */
e62662c7 5326void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
7359d482
PZ
5327{
5328 struct usb_hcd *hcd;
5329
e62662c7 5330 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
7359d482
PZ
5331
5332 hcd = dwc2_hsotg_to_hcd(hsotg);
e62662c7 5333 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
7359d482
PZ
5334
5335 if (!hcd) {
e62662c7 5336 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
7359d482
PZ
5337 __func__);
5338 return;
5339 }
5340
9df4ceac
MYK
5341 if (!IS_ERR_OR_NULL(hsotg->uphy))
5342 otg_set_host(hsotg->uphy->otg, NULL);
5343
7359d482
PZ
5344 usb_remove_hcd(hcd);
5345 hsotg->priv = NULL;
3b5fcc9a
GH
5346
5347 kmem_cache_destroy(hsotg->desc_gen_cache);
5348 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5349
7359d482 5350 dwc2_hcd_release(hsotg);
ba0e60d1 5351 usb_put_hcd(hcd);
7359d482
PZ
5352
5353#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5354 kfree(hsotg->last_frame_num_array);
5355 kfree(hsotg->frame_num_array);
5356#endif
7359d482 5357}
58e52ff6
JY
5358
5359/**
5360 * dwc2_backup_host_registers() - Backup controller host registers.
5361 * When suspending usb bus, registers needs to be backuped
5362 * if controller power is disabled once suspended.
5363 *
5364 * @hsotg: Programming view of the DWC_otg controller
5365 */
5366int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
5367{
5368 struct dwc2_hregs_backup *hr;
5369 int i;
5370
5371 dev_dbg(hsotg->dev, "%s\n", __func__);
5372
5373 /* Backup Host regs */
5374 hr = &hsotg->hr_backup;
5375 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
5376 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
bea8e86c 5377 for (i = 0; i < hsotg->params.host_channels; ++i)
58e52ff6
JY
5378 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
5379
5380 hr->hprt0 = dwc2_read_hprt0(hsotg);
5381 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
5382 hr->valid = true;
5383
5384 return 0;
5385}
5386
5387/**
5388 * dwc2_restore_host_registers() - Restore controller host registers.
5389 * When resuming usb bus, device registers needs to be restored
5390 * if controller power were disabled.
5391 *
5392 * @hsotg: Programming view of the DWC_otg controller
5393 */
5394int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
5395{
5396 struct dwc2_hregs_backup *hr;
5397 int i;
5398
5399 dev_dbg(hsotg->dev, "%s\n", __func__);
5400
5401 /* Restore host regs */
5402 hr = &hsotg->hr_backup;
5403 if (!hr->valid) {
5404 dev_err(hsotg->dev, "%s: no host registers to restore\n",
5405 __func__);
5406 return -EINVAL;
5407 }
5408 hr->valid = false;
5409
5410 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5411 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
5412
bea8e86c 5413 for (i = 0; i < hsotg->params.host_channels; ++i)
58e52ff6
JY
5414 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
5415
5416 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
5417 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
5418 hsotg->frame_number = 0;
5419
5420 return 0;
5421}