blob: d4b61330dfbbbbeb929823dd988120a7ea85a9d9 [file] [log] [blame]
Greg Kroah-Hartman5fd54ac2017-11-03 11:28:30 +01001// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002/*
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>
Heiner Kallweit348becd2017-01-25 23:10:51 +010046#include <linux/platform_device.h>
Paul Zimmerman7359d482013-03-11 17:47:59 -070047#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
Chen Yu9156a7e2017-01-23 14:59:57 -080059static void dwc2_port_resume(struct dwc2_hsotg *hsotg);
60
John Younb02038fa2016-02-23 19:55:00 -080061/*
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
John Youn95832c02017-01-23 14:57:26 -080086 if (!hsotg->params.host_dma)
John Younb02038fa2016-02-23 19:55:00 -080087 intmsk |= GINTSTS_RXFLVL;
John Youn95832c02017-01-23 14:57:26 -080088 if (!hsotg->params.external_id_pin_ctl)
John Younb02038fa2016-02-23 19:55:00 -080089 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 &&
John Youn95832c02017-01-23 14:57:26 -0800107 hsotg->params.ulpi_fs_ls) ||
John Younbea8e862016-11-03 17:55:53 -0700108 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
John Younb02038fa2016-02-23 19:55:00 -0800109 /* 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{
Bruno Herrerae35b1352017-01-31 23:25:43 -0200125 u32 usbcfg, ggpio, i2cctl;
John Younb02038fa2016-02-23 19:55:00 -0800126 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 }
Bruno Herrerae35b1352017-01-31 23:25:43 -0200149
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 }
John Younb02038fa2016-02-23 19:55:00 -0800162 }
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
John Youn95832c02017-01-23 14:57:26 -0800172 if (hsotg->params.i2c_enable) {
John Younb02038fa2016-02-23 19:55:00 -0800173 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 */
John Younbea8e862016-11-03 17:55:53 -0700209 switch (hsotg->params.phy_type) {
John Younb02038fa2016-02-23 19:55:00 -0800210 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);
John Youn95832c02017-01-23 14:57:26 -0800215 if (hsotg->params.phy_ulpi_ddr)
John Younb02038fa2016-02-23 19:55:00 -0800216 usbcfg |= GUSBCFG_DDRSEL;
Dinh Nguyenb11633c2017-10-16 08:57:18 -0500217
218 /* Set external VBUS indicator as needed. */
219 if (hsotg->params.oc_disable)
220 usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
221 GUSBCFG_INDICATORPASSTHROUGH);
John Younb02038fa2016-02-23 19:55:00 -0800222 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);
John Younbea8e862016-11-03 17:55:53 -0700227 if (hsotg->params.phy_utmi_width == 16)
John Younb02038fa2016-02-23 19:55:00 -0800228 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
Vardan Mikayelyan38e90022016-11-14 19:17:03 -0800255 if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
256 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
John Younbea8e862016-11-03 17:55:53 -0700257 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -0800258 /* If FS/LS mode with FS/LS PHY */
John Younb02038fa2016-02-23 19:55:00 -0800259 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 &&
John Youn95832c02017-01-23 14:57:26 -0800271 hsotg->params.ulpi_fs_ls) {
John Younb02038fa2016-02-23 19:55:00 -0800272 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");
John Younbea8e862016-11-03 17:55:53 -0700298 if (hsotg->params.ahbcfg != -1) {
John Younb02038fa2016-02-23 19:55:00 -0800299 ahbcfg &= GAHBCFG_CTRL_MASK;
John Younbea8e862016-11-03 17:55:53 -0700300 ahbcfg |= hsotg->params.ahbcfg &
John Younb02038fa2016-02-23 19:55:00 -0800301 ~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
John Youn95832c02017-01-23 14:57:26 -0800311 if (hsotg->params.host_dma)
John Younb02038fa2016-02-23 19:55:00 -0800312 ahbcfg |= GAHBCFG_DMA_EN;
Razmik Karapetyan9d729a72018-01-19 14:43:27 +0400313 else
314 hsotg->params.dma_desc_enable = false;
John Younb02038fa2016-02-23 19:55:00 -0800315
316 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
317
318 return 0;
319}
320
321static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
322{
323 u32 usbcfg;
324
325 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
326 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
327
328 switch (hsotg->hw_params.op_mode) {
329 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
John Younbea8e862016-11-03 17:55:53 -0700330 if (hsotg->params.otg_cap ==
John Younb02038fa2016-02-23 19:55:00 -0800331 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
332 usbcfg |= GUSBCFG_HNPCAP;
John Younbea8e862016-11-03 17:55:53 -0700333 if (hsotg->params.otg_cap !=
John Younb02038fa2016-02-23 19:55:00 -0800334 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
335 usbcfg |= GUSBCFG_SRPCAP;
336 break;
337
338 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
339 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
340 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
John Younbea8e862016-11-03 17:55:53 -0700341 if (hsotg->params.otg_cap !=
John Younb02038fa2016-02-23 19:55:00 -0800342 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
343 usbcfg |= GUSBCFG_SRPCAP;
344 break;
345
346 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
347 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
348 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
349 default:
350 break;
351 }
352
353 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
354}
355
356/**
357 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
358 *
359 * @hsotg: Programming view of DWC_otg controller
360 */
361static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
362{
363 u32 intmsk;
364
365 dev_dbg(hsotg->dev, "%s()\n", __func__);
366
367 /* Disable all interrupts */
368 dwc2_writel(0, hsotg->regs + GINTMSK);
369 dwc2_writel(0, hsotg->regs + HAINTMSK);
370
371 /* Enable the common interrupts */
372 dwc2_enable_common_interrupts(hsotg);
373
374 /* Enable host mode interrupts without disturbing common interrupts */
375 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
376 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
377 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
378}
379
380/**
381 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
382 *
383 * @hsotg: Programming view of DWC_otg controller
384 */
385static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
386{
387 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
388
389 /* Disable host mode interrupts without disturbing common interrupts */
390 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
391 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
392 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
393}
394
395/*
396 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
397 * For system that have a total fifo depth that is smaller than the default
398 * RX + TX fifo size.
399 *
400 * @hsotg: Programming view of DWC_otg controller
401 */
402static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
403{
John Younbea8e862016-11-03 17:55:53 -0700404 struct dwc2_core_params *params = &hsotg->params;
John Younb02038fa2016-02-23 19:55:00 -0800405 struct dwc2_hw_params *hw = &hsotg->hw_params;
406 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
407
408 total_fifo_size = hw->total_fifo_size;
409 rxfsiz = params->host_rx_fifo_size;
410 nptxfsiz = params->host_nperio_tx_fifo_size;
411 ptxfsiz = params->host_perio_tx_fifo_size;
412
413 /*
414 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
415 * allocation with support for high bandwidth endpoints. Synopsys
416 * defines MPS(Max Packet size) for a periodic EP=1024, and for
417 * non-periodic as 512.
418 */
419 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
420 /*
421 * For Buffer DMA mode/Scatter Gather DMA mode
422 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
423 * with n = number of host channel.
424 * 2 * ((1024/4) + 2) = 516
425 */
426 rxfsiz = 516 + hw->host_channels;
427
428 /*
429 * min non-periodic tx fifo depth
430 * 2 * (largest non-periodic USB packet used / 4)
431 * 2 * (512/4) = 256
432 */
433 nptxfsiz = 256;
434
435 /*
436 * min periodic tx fifo depth
437 * (largest packet size*MC)/4
438 * (1024 * 3)/4 = 768
439 */
440 ptxfsiz = 768;
441
442 params->host_rx_fifo_size = rxfsiz;
443 params->host_nperio_tx_fifo_size = nptxfsiz;
444 params->host_perio_tx_fifo_size = ptxfsiz;
445 }
446
447 /*
448 * If the summation of RX, NPTX and PTX fifo sizes is still
449 * bigger than the total_fifo_size, then we have a problem.
450 *
451 * We won't be able to allocate as many endpoints. Right now,
452 * we're just printing an error message, but ideally this FIFO
453 * allocation algorithm would be improved in the future.
454 *
455 * FIXME improve this FIFO allocation algorithm.
456 */
457 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
458 dev_err(hsotg->dev, "invalid fifo sizes\n");
459}
460
461static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
462{
John Younbea8e862016-11-03 17:55:53 -0700463 struct dwc2_core_params *params = &hsotg->params;
John Younb02038fa2016-02-23 19:55:00 -0800464 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
465
466 if (!params->enable_dynamic_fifo)
467 return;
468
469 dwc2_calculate_dynamic_fifo(hsotg);
470
471 /* Rx FIFO */
472 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
473 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
474 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
475 grxfsiz |= params->host_rx_fifo_size <<
476 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
477 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
478 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
479 dwc2_readl(hsotg->regs + GRXFSIZ));
480
481 /* Non-periodic Tx FIFO */
482 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
483 dwc2_readl(hsotg->regs + GNPTXFSIZ));
484 nptxfsiz = params->host_nperio_tx_fifo_size <<
485 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
486 nptxfsiz |= params->host_rx_fifo_size <<
487 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
488 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
489 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
490 dwc2_readl(hsotg->regs + GNPTXFSIZ));
491
492 /* Periodic Tx FIFO */
493 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
494 dwc2_readl(hsotg->regs + HPTXFSIZ));
495 hptxfsiz = params->host_perio_tx_fifo_size <<
496 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
497 hptxfsiz |= (params->host_rx_fifo_size +
498 params->host_nperio_tx_fifo_size) <<
499 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
500 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
501 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
502 dwc2_readl(hsotg->regs + HPTXFSIZ));
503
John Youn95832c02017-01-23 14:57:26 -0800504 if (hsotg->params.en_multiple_tx_fifo &&
Sevak Arakelyane1f411d2017-01-23 15:01:01 -0800505 hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
John Younb02038fa2016-02-23 19:55:00 -0800506 /*
Sevak Arakelyane1f411d2017-01-23 15:01:01 -0800507 * This feature was implemented in 2.91a version
John Younb02038fa2016-02-23 19:55:00 -0800508 * Global DFIFOCFG calculation for Host mode -
509 * include RxFIFO, NPTXFIFO and HPTXFIFO
510 */
511 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
512 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
513 dfifocfg |= (params->host_rx_fifo_size +
514 params->host_nperio_tx_fifo_size +
515 params->host_perio_tx_fifo_size) <<
516 GDFIFOCFG_EPINFOBASE_SHIFT &
517 GDFIFOCFG_EPINFOBASE_MASK;
518 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
519 }
520}
521
522/**
523 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
524 * the HFIR register according to PHY type and speed
525 *
526 * @hsotg: Programming view of DWC_otg controller
527 *
528 * NOTE: The caller can modify the value of the HFIR register only after the
529 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
530 * has been set
531 */
532u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
533{
534 u32 usbcfg;
535 u32 hprt0;
536 int clock = 60; /* default value */
537
538 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
539 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
540
541 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
542 !(usbcfg & GUSBCFG_PHYIF16))
543 clock = 60;
544 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
545 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
546 clock = 48;
547 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
548 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
549 clock = 30;
550 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
551 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
552 clock = 60;
553 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
554 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
555 clock = 48;
556 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
557 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
558 clock = 48;
559 if ((usbcfg & GUSBCFG_PHYSEL) &&
560 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
561 clock = 48;
562
563 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
564 /* High speed case */
565 return 125 * clock - 1;
566
567 /* FS/LS case */
568 return 1000 * clock - 1;
569}
570
571/**
572 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
573 * buffer
574 *
575 * @core_if: Programming view of DWC_otg controller
576 * @dest: Destination buffer for the packet
577 * @bytes: Number of bytes to copy to the destination
578 */
579void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
580{
581 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
582 u32 *data_buf = (u32 *)dest;
583 int word_count = (bytes + 3) / 4;
584 int i;
585
586 /*
587 * Todo: Account for the case where dest is not dword aligned. This
588 * requires reading data from the FIFO into a u32 temp buffer, then
589 * moving it into the data buffer.
590 */
591
592 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
593
594 for (i = 0; i < word_count; i++, data_buf++)
595 *data_buf = dwc2_readl(fifo);
596}
597
Paul Zimmerman7359d482013-03-11 17:47:59 -0700598/**
599 * dwc2_dump_channel_info() - Prints the state of a host channel
600 *
601 * @hsotg: Programming view of DWC_otg controller
602 * @chan: Pointer to the channel to dump
603 *
604 * Must be called with interrupt disabled and spinlock held
605 *
606 * NOTE: This function will be removed once the peripheral controller code
607 * is integrated and the driver is stable
608 */
609static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
610 struct dwc2_host_chan *chan)
611{
612#ifdef VERBOSE_DEBUG
John Younbea8e862016-11-03 17:55:53 -0700613 int num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700614 struct dwc2_qh *qh;
615 u32 hcchar;
616 u32 hcsplt;
617 u32 hctsiz;
618 u32 hc_dma;
619 int i;
620
John Younb02038fa2016-02-23 19:55:00 -0800621 if (!chan)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700622 return;
623
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300624 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
625 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
626 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chan->hc_num));
627 hc_dma = dwc2_readl(hsotg->regs + HCDMA(chan->hc_num));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700628
629 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
630 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
631 hcchar, hcsplt);
632 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
633 hctsiz, hc_dma);
634 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
635 chan->dev_addr, chan->ep_num, chan->ep_is_in);
636 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
637 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
638 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
639 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
640 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
641 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
642 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
643 (unsigned long)chan->xfer_dma);
644 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
645 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
646 dev_dbg(hsotg->dev, " NP inactive sched:\n");
647 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
648 qh_list_entry)
649 dev_dbg(hsotg->dev, " %p\n", qh);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -0800650 dev_dbg(hsotg->dev, " NP waiting sched:\n");
651 list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
652 qh_list_entry)
653 dev_dbg(hsotg->dev, " %p\n", qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700654 dev_dbg(hsotg->dev, " NP active sched:\n");
655 list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
656 qh_list_entry)
657 dev_dbg(hsotg->dev, " %p\n", qh);
658 dev_dbg(hsotg->dev, " Channels:\n");
659 for (i = 0; i < num_channels; i++) {
660 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
661
662 dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
663 }
664#endif /* VERBOSE_DEBUG */
665}
666
Razmik Karapetyan4411beb2016-11-16 15:34:04 -0800667static int _dwc2_hcd_start(struct usb_hcd *hcd);
668
669static void dwc2_host_start(struct dwc2_hsotg *hsotg)
670{
671 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
672
673 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
674 _dwc2_hcd_start(hcd);
675}
676
677static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
678{
679 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
680
681 hcd->self.is_b_host = 0;
682}
683
684static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
685 int *hub_addr, int *hub_port)
686{
687 struct urb *urb = context;
688
689 if (urb->dev->tt)
690 *hub_addr = urb->dev->tt->hub->devnum;
691 else
692 *hub_addr = 0;
693 *hub_port = urb->dev->ttport;
694}
695
Paul Zimmerman7359d482013-03-11 17:47:59 -0700696/*
John Younb02038fa2016-02-23 19:55:00 -0800697 * =========================================================================
698 * Low Level Host Channel Access Functions
699 * =========================================================================
700 */
701
702static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
703 struct dwc2_host_chan *chan)
704{
705 u32 hcintmsk = HCINTMSK_CHHLTD;
706
707 switch (chan->ep_type) {
708 case USB_ENDPOINT_XFER_CONTROL:
709 case USB_ENDPOINT_XFER_BULK:
710 dev_vdbg(hsotg->dev, "control/bulk\n");
711 hcintmsk |= HCINTMSK_XFERCOMPL;
712 hcintmsk |= HCINTMSK_STALL;
713 hcintmsk |= HCINTMSK_XACTERR;
714 hcintmsk |= HCINTMSK_DATATGLERR;
715 if (chan->ep_is_in) {
716 hcintmsk |= HCINTMSK_BBLERR;
717 } else {
718 hcintmsk |= HCINTMSK_NAK;
719 hcintmsk |= HCINTMSK_NYET;
720 if (chan->do_ping)
721 hcintmsk |= HCINTMSK_ACK;
722 }
723
724 if (chan->do_split) {
725 hcintmsk |= HCINTMSK_NAK;
726 if (chan->complete_split)
727 hcintmsk |= HCINTMSK_NYET;
728 else
729 hcintmsk |= HCINTMSK_ACK;
730 }
731
732 if (chan->error_state)
733 hcintmsk |= HCINTMSK_ACK;
734 break;
735
736 case USB_ENDPOINT_XFER_INT:
737 if (dbg_perio())
738 dev_vdbg(hsotg->dev, "intr\n");
739 hcintmsk |= HCINTMSK_XFERCOMPL;
740 hcintmsk |= HCINTMSK_NAK;
741 hcintmsk |= HCINTMSK_STALL;
742 hcintmsk |= HCINTMSK_XACTERR;
743 hcintmsk |= HCINTMSK_DATATGLERR;
744 hcintmsk |= HCINTMSK_FRMOVRUN;
745
746 if (chan->ep_is_in)
747 hcintmsk |= HCINTMSK_BBLERR;
748 if (chan->error_state)
749 hcintmsk |= HCINTMSK_ACK;
750 if (chan->do_split) {
751 if (chan->complete_split)
752 hcintmsk |= HCINTMSK_NYET;
753 else
754 hcintmsk |= HCINTMSK_ACK;
755 }
756 break;
757
758 case USB_ENDPOINT_XFER_ISOC:
759 if (dbg_perio())
760 dev_vdbg(hsotg->dev, "isoc\n");
761 hcintmsk |= HCINTMSK_XFERCOMPL;
762 hcintmsk |= HCINTMSK_FRMOVRUN;
763 hcintmsk |= HCINTMSK_ACK;
764
765 if (chan->ep_is_in) {
766 hcintmsk |= HCINTMSK_XACTERR;
767 hcintmsk |= HCINTMSK_BBLERR;
768 }
769 break;
770 default:
771 dev_err(hsotg->dev, "## Unknown EP type ##\n");
772 break;
773 }
774
775 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
776 if (dbg_hc(chan))
777 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
778}
779
780static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
781 struct dwc2_host_chan *chan)
782{
783 u32 hcintmsk = HCINTMSK_CHHLTD;
784
785 /*
786 * For Descriptor DMA mode core halts the channel on AHB error.
787 * Interrupt is not required.
788 */
John Youn95832c02017-01-23 14:57:26 -0800789 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -0800790 if (dbg_hc(chan))
791 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
792 hcintmsk |= HCINTMSK_AHBERR;
793 } else {
794 if (dbg_hc(chan))
795 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
796 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
797 hcintmsk |= HCINTMSK_XFERCOMPL;
798 }
799
800 if (chan->error_state && !chan->do_split &&
801 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
802 if (dbg_hc(chan))
803 dev_vdbg(hsotg->dev, "setting ACK\n");
804 hcintmsk |= HCINTMSK_ACK;
805 if (chan->ep_is_in) {
806 hcintmsk |= HCINTMSK_DATATGLERR;
807 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
808 hcintmsk |= HCINTMSK_NAK;
809 }
810 }
811
812 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
813 if (dbg_hc(chan))
814 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
815}
816
817static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
818 struct dwc2_host_chan *chan)
819{
820 u32 intmsk;
821
John Youn95832c02017-01-23 14:57:26 -0800822 if (hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -0800823 if (dbg_hc(chan))
824 dev_vdbg(hsotg->dev, "DMA enabled\n");
825 dwc2_hc_enable_dma_ints(hsotg, chan);
826 } else {
827 if (dbg_hc(chan))
828 dev_vdbg(hsotg->dev, "DMA disabled\n");
829 dwc2_hc_enable_slave_ints(hsotg, chan);
830 }
831
832 /* Enable the top level host channel interrupt */
833 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
834 intmsk |= 1 << chan->hc_num;
835 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
836 if (dbg_hc(chan))
837 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
838
839 /* Make sure host channel interrupts are enabled */
840 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
841 intmsk |= GINTSTS_HCHINT;
842 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
843 if (dbg_hc(chan))
844 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
845}
846
847/**
848 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
849 * a specific endpoint
850 *
851 * @hsotg: Programming view of DWC_otg controller
852 * @chan: Information needed to initialize the host channel
853 *
854 * The HCCHARn register is set up with the characteristics specified in chan.
855 * Host channel interrupts that may need to be serviced while this transfer is
856 * in progress are enabled.
857 */
858static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
859{
860 u8 hc_num = chan->hc_num;
861 u32 hcintmsk;
862 u32 hcchar;
863 u32 hcsplt = 0;
864
865 if (dbg_hc(chan))
866 dev_vdbg(hsotg->dev, "%s()\n", __func__);
867
868 /* Clear old interrupt conditions for this host channel */
869 hcintmsk = 0xffffffff;
870 hcintmsk &= ~HCINTMSK_RESERVED14_31;
871 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
872
873 /* Enable channel interrupts required for this transfer */
874 dwc2_hc_enable_ints(hsotg, chan);
875
876 /*
877 * Program the HCCHARn register with the endpoint characteristics for
878 * the current transfer
879 */
880 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
881 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
882 if (chan->ep_is_in)
883 hcchar |= HCCHAR_EPDIR;
884 if (chan->speed == USB_SPEED_LOW)
885 hcchar |= HCCHAR_LSPDDEV;
886 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
887 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
888 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
889 if (dbg_hc(chan)) {
890 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
891 hc_num, hcchar);
892
893 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
894 __func__, hc_num);
895 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
896 chan->dev_addr);
897 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
898 chan->ep_num);
899 dev_vdbg(hsotg->dev, " Is In: %d\n",
900 chan->ep_is_in);
901 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
902 chan->speed == USB_SPEED_LOW);
903 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
904 chan->ep_type);
905 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
906 chan->max_packet);
907 }
908
909 /* Program the HCSPLT register for SPLITs */
910 if (chan->do_split) {
911 if (dbg_hc(chan))
912 dev_vdbg(hsotg->dev,
913 "Programming HC %d with split --> %s\n",
914 hc_num,
915 chan->complete_split ? "CSPLIT" : "SSPLIT");
916 if (chan->complete_split)
917 hcsplt |= HCSPLT_COMPSPLT;
918 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
919 HCSPLT_XACTPOS_MASK;
920 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
921 HCSPLT_HUBADDR_MASK;
922 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
923 HCSPLT_PRTADDR_MASK;
924 if (dbg_hc(chan)) {
925 dev_vdbg(hsotg->dev, " comp split %d\n",
926 chan->complete_split);
927 dev_vdbg(hsotg->dev, " xact pos %d\n",
928 chan->xact_pos);
929 dev_vdbg(hsotg->dev, " hub addr %d\n",
930 chan->hub_addr);
931 dev_vdbg(hsotg->dev, " hub port %d\n",
932 chan->hub_port);
933 dev_vdbg(hsotg->dev, " is_in %d\n",
934 chan->ep_is_in);
935 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
936 chan->max_packet);
937 dev_vdbg(hsotg->dev, " xferlen %d\n",
938 chan->xfer_len);
939 }
940 }
941
942 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
943}
944
945/**
946 * dwc2_hc_halt() - Attempts to halt a host channel
947 *
948 * @hsotg: Controller register interface
949 * @chan: Host channel to halt
950 * @halt_status: Reason for halting the channel
951 *
952 * This function should only be called in Slave mode or to abort a transfer in
953 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
954 * controller halts the channel when the transfer is complete or a condition
955 * occurs that requires application intervention.
956 *
957 * In slave mode, checks for a free request queue entry, then sets the Channel
958 * Enable and Channel Disable bits of the Host Channel Characteristics
959 * register of the specified channel to intiate the halt. If there is no free
960 * request queue entry, sets only the Channel Disable bit of the HCCHARn
961 * register to flush requests for this channel. In the latter case, sets a
962 * flag to indicate that the host channel needs to be halted when a request
963 * queue slot is open.
964 *
965 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
966 * HCCHARn register. The controller ensures there is space in the request
967 * queue before submitting the halt request.
968 *
969 * Some time may elapse before the core flushes any posted requests for this
970 * host channel and halts. The Channel Halted interrupt handler completes the
971 * deactivation of the host channel.
972 */
973void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
974 enum dwc2_halt_status halt_status)
975{
976 u32 nptxsts, hptxsts, hcchar;
977
978 if (dbg_hc(chan))
979 dev_vdbg(hsotg->dev, "%s()\n", __func__);
980 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
981 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
982
983 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
984 halt_status == DWC2_HC_XFER_AHB_ERR) {
985 /*
986 * Disable all channel interrupts except Ch Halted. The QTD
987 * and QH state associated with this transfer has been cleared
988 * (in the case of URB_DEQUEUE), so the channel needs to be
989 * shut down carefully to prevent crashes.
990 */
991 u32 hcintmsk = HCINTMSK_CHHLTD;
992
993 dev_vdbg(hsotg->dev, "dequeue/error\n");
994 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
995
996 /*
997 * Make sure no other interrupts besides halt are currently
998 * pending. Handling another interrupt could cause a crash due
999 * to the QTD and QH state.
1000 */
1001 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1002
1003 /*
1004 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1005 * even if the channel was already halted for some other
1006 * reason
1007 */
1008 chan->halt_status = halt_status;
1009
1010 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1011 if (!(hcchar & HCCHAR_CHENA)) {
1012 /*
1013 * The channel is either already halted or it hasn't
1014 * started yet. In DMA mode, the transfer may halt if
1015 * it finishes normally or a condition occurs that
1016 * requires driver intervention. Don't want to halt
1017 * the channel again. In either Slave or DMA mode,
1018 * it's possible that the transfer has been assigned
1019 * to a channel, but not started yet when an URB is
1020 * dequeued. Don't want to halt a channel that hasn't
1021 * started yet.
1022 */
1023 return;
1024 }
1025 }
1026 if (chan->halt_pending) {
1027 /*
1028 * A halt has already been issued for this channel. This might
1029 * happen when a transfer is aborted by a higher level in
1030 * the stack.
1031 */
1032 dev_vdbg(hsotg->dev,
1033 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1034 __func__, chan->hc_num);
1035 return;
1036 }
1037
1038 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1039
1040 /* No need to set the bit in DDMA for disabling the channel */
1041 /* TODO check it everywhere channel is disabled */
John Youn95832c02017-01-23 14:57:26 -08001042 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08001043 if (dbg_hc(chan))
1044 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1045 hcchar |= HCCHAR_CHENA;
1046 } else {
1047 if (dbg_hc(chan))
1048 dev_dbg(hsotg->dev, "desc DMA enabled\n");
1049 }
1050 hcchar |= HCCHAR_CHDIS;
1051
John Youn95832c02017-01-23 14:57:26 -08001052 if (!hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001053 if (dbg_hc(chan))
1054 dev_vdbg(hsotg->dev, "DMA not enabled\n");
1055 hcchar |= HCCHAR_CHENA;
1056
1057 /* Check for space in the request queue to issue the halt */
1058 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1059 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1060 dev_vdbg(hsotg->dev, "control/bulk\n");
1061 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1062 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1063 dev_vdbg(hsotg->dev, "Disabling channel\n");
1064 hcchar &= ~HCCHAR_CHENA;
1065 }
1066 } else {
1067 if (dbg_perio())
1068 dev_vdbg(hsotg->dev, "isoc/intr\n");
1069 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1070 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1071 hsotg->queuing_high_bandwidth) {
1072 if (dbg_perio())
1073 dev_vdbg(hsotg->dev, "Disabling channel\n");
1074 hcchar &= ~HCCHAR_CHENA;
1075 }
1076 }
1077 } else {
1078 if (dbg_hc(chan))
1079 dev_vdbg(hsotg->dev, "DMA enabled\n");
1080 }
1081
1082 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1083 chan->halt_status = halt_status;
1084
1085 if (hcchar & HCCHAR_CHENA) {
1086 if (dbg_hc(chan))
1087 dev_vdbg(hsotg->dev, "Channel enabled\n");
1088 chan->halt_pending = 1;
1089 chan->halt_on_queue = 0;
1090 } else {
1091 if (dbg_hc(chan))
1092 dev_vdbg(hsotg->dev, "Channel disabled\n");
1093 chan->halt_on_queue = 1;
1094 }
1095
1096 if (dbg_hc(chan)) {
1097 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1098 chan->hc_num);
1099 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1100 hcchar);
1101 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1102 chan->halt_pending);
1103 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1104 chan->halt_on_queue);
1105 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1106 chan->halt_status);
1107 }
1108}
1109
1110/**
1111 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1112 *
1113 * @hsotg: Programming view of DWC_otg controller
1114 * @chan: Identifies the host channel to clean up
1115 *
1116 * This function is normally called after a transfer is done and the host
1117 * channel is being released
1118 */
1119void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1120{
1121 u32 hcintmsk;
1122
1123 chan->xfer_started = 0;
1124
1125 list_del_init(&chan->split_order_list_entry);
1126
1127 /*
1128 * Clear channel interrupt enables and any unhandled channel interrupt
1129 * conditions
1130 */
1131 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1132 hcintmsk = 0xffffffff;
1133 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1134 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1135}
1136
1137/**
1138 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1139 * which frame a periodic transfer should occur
1140 *
1141 * @hsotg: Programming view of DWC_otg controller
1142 * @chan: Identifies the host channel to set up and its properties
1143 * @hcchar: Current value of the HCCHAR register for the specified host channel
1144 *
1145 * This function has no effect on non-periodic transfers
1146 */
1147static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1148 struct dwc2_host_chan *chan, u32 *hcchar)
1149{
1150 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1151 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1152 int host_speed;
1153 int xfer_ns;
1154 int xfer_us;
1155 int bytes_in_fifo;
1156 u16 fifo_space;
1157 u16 frame_number;
1158 u16 wire_frame;
1159
1160 /*
1161 * Try to figure out if we're an even or odd frame. If we set
1162 * even and the current frame number is even the the transfer
1163 * will happen immediately. Similar if both are odd. If one is
1164 * even and the other is odd then the transfer will happen when
1165 * the frame number ticks.
1166 *
1167 * There's a bit of a balancing act to get this right.
1168 * Sometimes we may want to send data in the current frame (AK
1169 * right away). We might want to do this if the frame number
1170 * _just_ ticked, but we might also want to do this in order
1171 * to continue a split transaction that happened late in a
1172 * microframe (so we didn't know to queue the next transfer
1173 * until the frame number had ticked). The problem is that we
1174 * need a lot of knowledge to know if there's actually still
1175 * time to send things or if it would be better to wait until
1176 * the next frame.
1177 *
1178 * We can look at how much time is left in the current frame
1179 * and make a guess about whether we'll have time to transfer.
1180 * We'll do that.
1181 */
1182
1183 /* Get speed host is running at */
1184 host_speed = (chan->speed != USB_SPEED_HIGH &&
1185 !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1186
1187 /* See how many bytes are in the periodic FIFO right now */
1188 fifo_space = (dwc2_readl(hsotg->regs + HPTXSTS) &
1189 TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1190 bytes_in_fifo = sizeof(u32) *
John Younbea8e862016-11-03 17:55:53 -07001191 (hsotg->params.host_perio_tx_fifo_size -
John Younb02038fa2016-02-23 19:55:00 -08001192 fifo_space);
1193
1194 /*
1195 * Roughly estimate bus time for everything in the periodic
1196 * queue + our new transfer. This is "rough" because we're
1197 * using a function that makes takes into account IN/OUT
1198 * and INT/ISO and we're just slamming in one value for all
1199 * transfers. This should be an over-estimate and that should
1200 * be OK, but we can probably tighten it.
1201 */
1202 xfer_ns = usb_calc_bus_time(host_speed, false, false,
1203 chan->xfer_len + bytes_in_fifo);
1204 xfer_us = NS_TO_US(xfer_ns);
1205
1206 /* See what frame number we'll be at by the time we finish */
1207 frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1208
1209 /* This is when we were scheduled to be on the wire */
1210 wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1211
1212 /*
1213 * If we'd finish _after_ the frame we're scheduled in then
1214 * it's hopeless. Just schedule right away and hope for the
1215 * best. Note that it _might_ be wise to call back into the
1216 * scheduler to pick a better frame, but this is better than
1217 * nothing.
1218 */
1219 if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1220 dwc2_sch_vdbg(hsotg,
1221 "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1222 chan->qh, wire_frame, frame_number,
1223 dwc2_frame_num_dec(frame_number,
1224 wire_frame));
1225 wire_frame = frame_number;
1226
1227 /*
1228 * We picked a different frame number; communicate this
1229 * back to the scheduler so it doesn't try to schedule
1230 * another in the same frame.
1231 *
1232 * Remember that next_active_frame is 1 before the wire
1233 * frame.
1234 */
1235 chan->qh->next_active_frame =
1236 dwc2_frame_num_dec(frame_number, 1);
1237 }
1238
1239 if (wire_frame & 1)
1240 *hcchar |= HCCHAR_ODDFRM;
1241 else
1242 *hcchar &= ~HCCHAR_ODDFRM;
1243 }
1244}
1245
1246static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1247{
1248 /* Set up the initial PID for the transfer */
1249 if (chan->speed == USB_SPEED_HIGH) {
1250 if (chan->ep_is_in) {
1251 if (chan->multi_count == 1)
1252 chan->data_pid_start = DWC2_HC_PID_DATA0;
1253 else if (chan->multi_count == 2)
1254 chan->data_pid_start = DWC2_HC_PID_DATA1;
1255 else
1256 chan->data_pid_start = DWC2_HC_PID_DATA2;
1257 } else {
1258 if (chan->multi_count == 1)
1259 chan->data_pid_start = DWC2_HC_PID_DATA0;
1260 else
1261 chan->data_pid_start = DWC2_HC_PID_MDATA;
1262 }
1263 } else {
1264 chan->data_pid_start = DWC2_HC_PID_DATA0;
1265 }
1266}
1267
1268/**
1269 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1270 * the Host Channel
1271 *
1272 * @hsotg: Programming view of DWC_otg controller
1273 * @chan: Information needed to initialize the host channel
1274 *
1275 * This function should only be called in Slave mode. For a channel associated
1276 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1277 * associated with a periodic EP, the periodic Tx FIFO is written.
1278 *
1279 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1280 * the number of bytes written to the Tx FIFO.
1281 */
1282static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1283 struct dwc2_host_chan *chan)
1284{
1285 u32 i;
1286 u32 remaining_count;
1287 u32 byte_count;
1288 u32 dword_count;
1289 u32 __iomem *data_fifo;
1290 u32 *data_buf = (u32 *)chan->xfer_buf;
1291
1292 if (dbg_hc(chan))
1293 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1294
1295 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1296
1297 remaining_count = chan->xfer_len - chan->xfer_count;
1298 if (remaining_count > chan->max_packet)
1299 byte_count = chan->max_packet;
1300 else
1301 byte_count = remaining_count;
1302
1303 dword_count = (byte_count + 3) / 4;
1304
1305 if (((unsigned long)data_buf & 0x3) == 0) {
1306 /* xfer_buf is DWORD aligned */
1307 for (i = 0; i < dword_count; i++, data_buf++)
1308 dwc2_writel(*data_buf, data_fifo);
1309 } else {
1310 /* xfer_buf is not DWORD aligned */
1311 for (i = 0; i < dword_count; i++, data_buf++) {
1312 u32 data = data_buf[0] | data_buf[1] << 8 |
1313 data_buf[2] << 16 | data_buf[3] << 24;
1314 dwc2_writel(data, data_fifo);
1315 }
1316 }
1317
1318 chan->xfer_count += byte_count;
1319 chan->xfer_buf += byte_count;
1320}
1321
1322/**
1323 * dwc2_hc_do_ping() - Starts a PING transfer
1324 *
1325 * @hsotg: Programming view of DWC_otg controller
1326 * @chan: Information needed to initialize the host channel
1327 *
1328 * This function should only be called in Slave mode. The Do Ping bit is set in
1329 * the HCTSIZ register, then the channel is enabled.
1330 */
1331static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1332 struct dwc2_host_chan *chan)
1333{
1334 u32 hcchar;
1335 u32 hctsiz;
1336
1337 if (dbg_hc(chan))
1338 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1339 chan->hc_num);
1340
1341 hctsiz = TSIZ_DOPNG;
1342 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1343 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1344
1345 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1346 hcchar |= HCCHAR_CHENA;
1347 hcchar &= ~HCCHAR_CHDIS;
1348 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1349}
1350
1351/**
1352 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1353 * channel and starts the transfer
1354 *
1355 * @hsotg: Programming view of DWC_otg controller
1356 * @chan: Information needed to initialize the host channel. The xfer_len value
1357 * may be reduced to accommodate the max widths of the XferSize and
1358 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1359 * changed to reflect the final xfer_len value.
1360 *
1361 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1362 * the caller must ensure that there is sufficient space in the request queue
1363 * and Tx Data FIFO.
1364 *
1365 * For an OUT transfer in Slave mode, it loads a data packet into the
1366 * appropriate FIFO. If necessary, additional data packets are loaded in the
1367 * Host ISR.
1368 *
1369 * For an IN transfer in Slave mode, a data packet is requested. The data
1370 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1371 * additional data packets are requested in the Host ISR.
1372 *
1373 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1374 * register along with a packet count of 1 and the channel is enabled. This
1375 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1376 * simply set to 0 since no data transfer occurs in this case.
1377 *
1378 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1379 * all the information required to perform the subsequent data transfer. In
1380 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1381 * controller performs the entire PING protocol, then starts the data
1382 * transfer.
1383 */
1384static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1385 struct dwc2_host_chan *chan)
1386{
John Younbea8e862016-11-03 17:55:53 -07001387 u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1388 u16 max_hc_pkt_count = hsotg->params.max_packet_count;
John Younb02038fa2016-02-23 19:55:00 -08001389 u32 hcchar;
1390 u32 hctsiz = 0;
1391 u16 num_packets;
1392 u32 ec_mc;
1393
1394 if (dbg_hc(chan))
1395 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1396
1397 if (chan->do_ping) {
John Youn95832c02017-01-23 14:57:26 -08001398 if (!hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001399 if (dbg_hc(chan))
1400 dev_vdbg(hsotg->dev, "ping, no DMA\n");
1401 dwc2_hc_do_ping(hsotg, chan);
1402 chan->xfer_started = 1;
1403 return;
1404 }
1405
1406 if (dbg_hc(chan))
1407 dev_vdbg(hsotg->dev, "ping, DMA\n");
1408
1409 hctsiz |= TSIZ_DOPNG;
1410 }
1411
1412 if (chan->do_split) {
1413 if (dbg_hc(chan))
1414 dev_vdbg(hsotg->dev, "split\n");
1415 num_packets = 1;
1416
1417 if (chan->complete_split && !chan->ep_is_in)
1418 /*
1419 * For CSPLIT OUT Transfer, set the size to 0 so the
1420 * core doesn't expect any data written to the FIFO
1421 */
1422 chan->xfer_len = 0;
1423 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1424 chan->xfer_len = chan->max_packet;
1425 else if (!chan->ep_is_in && chan->xfer_len > 188)
1426 chan->xfer_len = 188;
1427
1428 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1429 TSIZ_XFERSIZE_MASK;
1430
1431 /* For split set ec_mc for immediate retries */
1432 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1433 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1434 ec_mc = 3;
1435 else
1436 ec_mc = 1;
1437 } else {
1438 if (dbg_hc(chan))
1439 dev_vdbg(hsotg->dev, "no split\n");
1440 /*
1441 * Ensure that the transfer length and packet count will fit
1442 * in the widths allocated for them in the HCTSIZn register
1443 */
1444 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1445 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1446 /*
1447 * Make sure the transfer size is no larger than one
1448 * (micro)frame's worth of data. (A check was done
1449 * when the periodic transfer was accepted to ensure
1450 * that a (micro)frame's worth of data can be
1451 * programmed into a channel.)
1452 */
1453 u32 max_periodic_len =
1454 chan->multi_count * chan->max_packet;
1455
1456 if (chan->xfer_len > max_periodic_len)
1457 chan->xfer_len = max_periodic_len;
1458 } else if (chan->xfer_len > max_hc_xfer_size) {
1459 /*
1460 * Make sure that xfer_len is a multiple of max packet
1461 * size
1462 */
1463 chan->xfer_len =
1464 max_hc_xfer_size - chan->max_packet + 1;
1465 }
1466
1467 if (chan->xfer_len > 0) {
1468 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1469 chan->max_packet;
1470 if (num_packets > max_hc_pkt_count) {
1471 num_packets = max_hc_pkt_count;
1472 chan->xfer_len = num_packets * chan->max_packet;
1473 }
1474 } else {
1475 /* Need 1 packet for transfer length of 0 */
1476 num_packets = 1;
1477 }
1478
1479 if (chan->ep_is_in)
1480 /*
1481 * Always program an integral # of max packets for IN
1482 * transfers
1483 */
1484 chan->xfer_len = num_packets * chan->max_packet;
1485
1486 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1487 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1488 /*
1489 * Make sure that the multi_count field matches the
1490 * actual transfer length
1491 */
1492 chan->multi_count = num_packets;
1493
1494 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1495 dwc2_set_pid_isoc(chan);
1496
1497 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1498 TSIZ_XFERSIZE_MASK;
1499
1500 /* The ec_mc gets the multi_count for non-split */
1501 ec_mc = chan->multi_count;
1502 }
1503
1504 chan->start_pkt_count = num_packets;
1505 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1506 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1507 TSIZ_SC_MC_PID_MASK;
1508 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1509 if (dbg_hc(chan)) {
1510 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1511 hctsiz, chan->hc_num);
1512
1513 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1514 chan->hc_num);
1515 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1516 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1517 TSIZ_XFERSIZE_SHIFT);
1518 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1519 (hctsiz & TSIZ_PKTCNT_MASK) >>
1520 TSIZ_PKTCNT_SHIFT);
1521 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1522 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1523 TSIZ_SC_MC_PID_SHIFT);
1524 }
1525
John Youn95832c02017-01-23 14:57:26 -08001526 if (hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001527 dwc2_writel((u32)chan->xfer_dma,
1528 hsotg->regs + HCDMA(chan->hc_num));
1529 if (dbg_hc(chan))
1530 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1531 (unsigned long)chan->xfer_dma, chan->hc_num);
1532 }
1533
1534 /* Start the split */
1535 if (chan->do_split) {
1536 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1537
1538 hcsplt |= HCSPLT_SPLTENA;
1539 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1540 }
1541
1542 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1543 hcchar &= ~HCCHAR_MULTICNT_MASK;
1544 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1545 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1546
1547 if (hcchar & HCCHAR_CHDIS)
1548 dev_warn(hsotg->dev,
1549 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1550 __func__, chan->hc_num, hcchar);
1551
1552 /* Set host channel enable after all other setup is complete */
1553 hcchar |= HCCHAR_CHENA;
1554 hcchar &= ~HCCHAR_CHDIS;
1555
1556 if (dbg_hc(chan))
1557 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1558 (hcchar & HCCHAR_MULTICNT_MASK) >>
1559 HCCHAR_MULTICNT_SHIFT);
1560
1561 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1562 if (dbg_hc(chan))
1563 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1564 chan->hc_num);
1565
1566 chan->xfer_started = 1;
1567 chan->requests++;
1568
John Youn95832c02017-01-23 14:57:26 -08001569 if (!hsotg->params.host_dma &&
John Younb02038fa2016-02-23 19:55:00 -08001570 !chan->ep_is_in && chan->xfer_len > 0)
1571 /* Load OUT packet into the appropriate Tx FIFO */
1572 dwc2_hc_write_packet(hsotg, chan);
1573}
1574
1575/**
1576 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1577 * host channel and starts the transfer in Descriptor DMA mode
1578 *
1579 * @hsotg: Programming view of DWC_otg controller
1580 * @chan: Information needed to initialize the host channel
1581 *
1582 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1583 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1584 * with micro-frame bitmap.
1585 *
1586 * Initializes HCDMA register with descriptor list address and CTD value then
1587 * starts the transfer via enabling the channel.
1588 */
1589void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1590 struct dwc2_host_chan *chan)
1591{
1592 u32 hcchar;
1593 u32 hctsiz = 0;
1594
1595 if (chan->do_ping)
1596 hctsiz |= TSIZ_DOPNG;
1597
1598 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1599 dwc2_set_pid_isoc(chan);
1600
1601 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1602 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1603 TSIZ_SC_MC_PID_MASK;
1604
1605 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1606 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1607
1608 /* Non-zero only for high-speed interrupt endpoints */
1609 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1610
1611 if (dbg_hc(chan)) {
1612 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1613 chan->hc_num);
1614 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1615 chan->data_pid_start);
1616 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1617 }
1618
1619 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1620
1621 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1622 chan->desc_list_sz, DMA_TO_DEVICE);
1623
1624 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
1625
1626 if (dbg_hc(chan))
1627 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1628 &chan->desc_list_addr, chan->hc_num);
1629
1630 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1631 hcchar &= ~HCCHAR_MULTICNT_MASK;
1632 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1633 HCCHAR_MULTICNT_MASK;
1634
1635 if (hcchar & HCCHAR_CHDIS)
1636 dev_warn(hsotg->dev,
1637 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1638 __func__, chan->hc_num, hcchar);
1639
1640 /* Set host channel enable after all other setup is complete */
1641 hcchar |= HCCHAR_CHENA;
1642 hcchar &= ~HCCHAR_CHDIS;
1643
1644 if (dbg_hc(chan))
1645 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1646 (hcchar & HCCHAR_MULTICNT_MASK) >>
1647 HCCHAR_MULTICNT_SHIFT);
1648
1649 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1650 if (dbg_hc(chan))
1651 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1652 chan->hc_num);
1653
1654 chan->xfer_started = 1;
1655 chan->requests++;
1656}
1657
1658/**
1659 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1660 * a previous call to dwc2_hc_start_transfer()
1661 *
1662 * @hsotg: Programming view of DWC_otg controller
1663 * @chan: Information needed to initialize the host channel
1664 *
1665 * The caller must ensure there is sufficient space in the request queue and Tx
1666 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1667 * the controller acts autonomously to complete transfers programmed to a host
1668 * channel.
1669 *
1670 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1671 * if there is any data remaining to be queued. For an IN transfer, another
1672 * data packet is always requested. For the SETUP phase of a control transfer,
1673 * this function does nothing.
1674 *
1675 * Return: 1 if a new request is queued, 0 if no more requests are required
1676 * for this transfer
1677 */
1678static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1679 struct dwc2_host_chan *chan)
1680{
1681 if (dbg_hc(chan))
1682 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1683 chan->hc_num);
1684
1685 if (chan->do_split)
1686 /* SPLITs always queue just once per channel */
1687 return 0;
1688
1689 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1690 /* SETUPs are queued only once since they can't be NAK'd */
1691 return 0;
1692
1693 if (chan->ep_is_in) {
1694 /*
1695 * Always queue another request for other IN transfers. If
1696 * back-to-back INs are issued and NAKs are received for both,
1697 * the driver may still be processing the first NAK when the
1698 * second NAK is received. When the interrupt handler clears
1699 * the NAK interrupt for the first NAK, the second NAK will
1700 * not be seen. So we can't depend on the NAK interrupt
1701 * handler to requeue a NAK'd request. Instead, IN requests
1702 * are issued each time this function is called. When the
1703 * transfer completes, the extra requests for the channel will
1704 * be flushed.
1705 */
1706 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1707
1708 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1709 hcchar |= HCCHAR_CHENA;
1710 hcchar &= ~HCCHAR_CHDIS;
1711 if (dbg_hc(chan))
1712 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
1713 hcchar);
1714 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1715 chan->requests++;
1716 return 1;
1717 }
1718
1719 /* OUT transfers */
1720
1721 if (chan->xfer_count < chan->xfer_len) {
1722 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1723 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1724 u32 hcchar = dwc2_readl(hsotg->regs +
1725 HCCHAR(chan->hc_num));
1726
1727 dwc2_hc_set_even_odd_frame(hsotg, chan,
1728 &hcchar);
1729 }
1730
1731 /* Load OUT packet into the appropriate Tx FIFO */
1732 dwc2_hc_write_packet(hsotg, chan);
1733 chan->requests++;
1734 return 1;
1735 }
1736
1737 return 0;
1738}
1739
1740/*
1741 * =========================================================================
1742 * HCD
1743 * =========================================================================
1744 */
1745
1746/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07001747 * Processes all the URBs in a single list of QHs. Completes them with
1748 * -ETIMEDOUT and frees the QTD.
1749 *
1750 * Must be called with interrupt disabled and spinlock held
1751 */
1752static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1753 struct list_head *qh_list)
1754{
1755 struct dwc2_qh *qh, *qh_tmp;
1756 struct dwc2_qtd *qtd, *qtd_tmp;
1757
1758 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1759 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1760 qtd_list_entry) {
Gregory Herrero2e84da62015-09-22 15:16:53 +02001761 dwc2_host_complete(hsotg, qtd, -ECONNRESET);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001762 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001763 }
1764 }
1765}
1766
1767static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1768 struct list_head *qh_list)
1769{
1770 struct dwc2_qtd *qtd, *qtd_tmp;
1771 struct dwc2_qh *qh, *qh_tmp;
1772 unsigned long flags;
1773
1774 if (!qh_list->next)
1775 /* The list hasn't been initialized yet */
1776 return;
1777
1778 spin_lock_irqsave(&hsotg->lock, flags);
1779
1780 /* Ensure there are no QTDs or URBs left */
1781 dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1782
1783 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1784 dwc2_hcd_qh_unlink(hsotg, qh);
1785
1786 /* Free each QTD in the QH's QTD list */
1787 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1788 qtd_list_entry)
1789 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1790
Douglas Anderson16e80212016-01-28 18:19:55 -08001791 if (qh->channel && qh->channel->qh == qh)
1792 qh->channel->qh = NULL;
1793
Paul Zimmerman7359d482013-03-11 17:47:59 -07001794 spin_unlock_irqrestore(&hsotg->lock, flags);
1795 dwc2_hcd_qh_free(hsotg, qh);
1796 spin_lock_irqsave(&hsotg->lock, flags);
1797 }
1798
1799 spin_unlock_irqrestore(&hsotg->lock, flags);
1800}
1801
1802/*
1803 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1804 * and periodic schedules. The QTD associated with each URB is removed from
1805 * the schedule and freed. This function may be called when a disconnect is
1806 * detected or when the HCD is being stopped.
1807 *
1808 * Must be called with interrupt disabled and spinlock held
1809 */
1810static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1811{
1812 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08001813 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001814 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1815 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1816 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1817 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1818 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1819}
1820
1821/**
1822 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1823 *
1824 * @hsotg: Pointer to struct dwc2_hsotg
1825 */
1826void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1827{
1828 u32 hprt0;
1829
1830 if (hsotg->op_state == OTG_STATE_B_HOST) {
1831 /*
1832 * Reset the port. During a HNP mode switch the reset
1833 * needs to occur within 1ms and have a duration of at
1834 * least 50ms.
1835 */
1836 hprt0 = dwc2_read_hprt0(hsotg);
1837 hprt0 |= HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001838 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001839 }
1840
1841 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1842 msecs_to_jiffies(50));
1843}
1844
1845/* Must be called with interrupt disabled and spinlock held */
1846static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1847{
John Younbea8e862016-11-03 17:55:53 -07001848 int num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001849 struct dwc2_host_chan *channel;
1850 u32 hcchar;
1851 int i;
1852
John Youn95832c02017-01-23 14:57:26 -08001853 if (!hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001854 /* Flush out any channel requests in slave mode */
1855 for (i = 0; i < num_channels; i++) {
1856 channel = hsotg->hc_ptr_array[i];
1857 if (!list_empty(&channel->hc_list_entry))
1858 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001859 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001860 if (hcchar & HCCHAR_CHENA) {
1861 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1862 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001863 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001864 }
1865 }
1866 }
1867
1868 for (i = 0; i < num_channels; i++) {
1869 channel = hsotg->hc_ptr_array[i];
1870 if (!list_empty(&channel->hc_list_entry))
1871 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001872 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001873 if (hcchar & HCCHAR_CHENA) {
1874 /* Halt the channel */
1875 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001876 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001877 }
1878
1879 dwc2_hc_cleanup(hsotg, channel);
1880 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1881 /*
1882 * Added for Descriptor DMA to prevent channel double cleanup in
1883 * release_channel_ddma(), which is called from ep_disable when
1884 * device disconnects
1885 */
1886 channel->qh = NULL;
1887 }
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001888 /* All channels have been freed, mark them available */
John Youn95832c02017-01-23 14:57:26 -08001889 if (hsotg->params.uframe_sched) {
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001890 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07001891 hsotg->params.host_channels;
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001892 } else {
1893 hsotg->non_periodic_channels = 0;
1894 hsotg->periodic_channels = 0;
1895 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001896}
1897
1898/**
Douglas Anderson6a659532015-11-19 13:23:14 -08001899 * dwc2_hcd_connect() - Handles connect of the HCD
Paul Zimmerman7359d482013-03-11 17:47:59 -07001900 *
1901 * @hsotg: Pointer to struct dwc2_hsotg
1902 *
1903 * Must be called with interrupt disabled and spinlock held
1904 */
Douglas Anderson6a659532015-11-19 13:23:14 -08001905void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
1906{
1907 if (hsotg->lx_state != DWC2_L0)
1908 usb_hcd_resume_root_hub(hsotg->priv);
1909
1910 hsotg->flags.b.port_connect_status_change = 1;
1911 hsotg->flags.b.port_connect_status = 1;
1912}
1913
1914/**
1915 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
1916 *
1917 * @hsotg: Pointer to struct dwc2_hsotg
1918 * @force: If true, we won't try to reconnect even if we see device connected.
1919 *
1920 * Must be called with interrupt disabled and spinlock held
1921 */
1922void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
Paul Zimmerman7359d482013-03-11 17:47:59 -07001923{
1924 u32 intr;
Douglas Anderson6a659532015-11-19 13:23:14 -08001925 u32 hprt0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001926
1927 /* Set status flags for the hub driver */
1928 hsotg->flags.b.port_connect_status_change = 1;
1929 hsotg->flags.b.port_connect_status = 0;
1930
1931 /*
1932 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1933 * interrupt mask and status bits and disabling subsequent host
1934 * channel interrupts.
1935 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001936 intr = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001937 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001938 dwc2_writel(intr, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001939 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001940 dwc2_writel(intr, hsotg->regs + GINTSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001941
1942 /*
1943 * Turn off the vbus power only if the core has transitioned to device
1944 * mode. If still in host mode, need to keep power on to detect a
1945 * reconnection.
1946 */
1947 if (dwc2_is_device_mode(hsotg)) {
1948 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1949 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001950 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001951 }
1952
1953 dwc2_disable_host_interrupts(hsotg);
1954 }
1955
1956 /* Respond with an error status to all URBs in the schedule */
1957 dwc2_kill_all_urbs(hsotg);
1958
1959 if (dwc2_is_host_mode(hsotg))
1960 /* Clean up any host channels that were in use */
1961 dwc2_hcd_cleanup_channels(hsotg);
1962
1963 dwc2_host_disconnect(hsotg);
Douglas Anderson6a659532015-11-19 13:23:14 -08001964
1965 /*
1966 * Add an extra check here to see if we're actually connected but
1967 * we don't have a detection interrupt pending. This can happen if:
1968 * 1. hardware sees connect
1969 * 2. hardware sees disconnect
1970 * 3. hardware sees connect
1971 * 4. dwc2_port_intr() - clears connect interrupt
1972 * 5. dwc2_handle_common_intr() - calls here
1973 *
1974 * Without the extra check here we will end calling disconnect
1975 * and won't get any future interrupts to handle the connect.
1976 */
1977 if (!force) {
1978 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
1979 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
1980 dwc2_hcd_connect(hsotg);
1981 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001982}
1983
1984/**
1985 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
1986 *
1987 * @hsotg: Pointer to struct dwc2_hsotg
1988 */
1989static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
1990{
Douglas Anderson1fb7f122015-10-22 13:05:03 -07001991 if (hsotg->bus_suspended) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001992 hsotg->flags.b.port_suspend_change = 1;
Gregory Herrerob46146d52015-01-30 09:09:26 +01001993 usb_hcd_resume_root_hub(hsotg->priv);
Gregory Herrerob46146d52015-01-30 09:09:26 +01001994 }
Douglas Anderson1fb7f122015-10-22 13:05:03 -07001995
1996 if (hsotg->lx_state == DWC2_L1)
1997 hsotg->flags.b.port_l1_change = 1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001998}
1999
2000/**
2001 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
2002 *
2003 * @hsotg: Pointer to struct dwc2_hsotg
2004 *
2005 * Must be called with interrupt disabled and spinlock held
2006 */
2007void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
2008{
2009 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
2010
2011 /*
2012 * The root hub should be disconnected before this function is called.
2013 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
2014 * and the QH lists (via ..._hcd_endpoint_disable).
2015 */
2016
2017 /* Turn off all host-specific interrupts */
2018 dwc2_disable_host_interrupts(hsotg);
2019
2020 /* Turn off the vbus power */
2021 dev_dbg(hsotg->dev, "PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002022 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002023}
2024
Gregory Herrero33ad2612015-04-29 22:09:15 +02002025/* Caller must hold driver lock */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002026static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002027 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002028 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002029{
Paul Zimmerman7359d482013-03-11 17:47:59 -07002030 u32 intr_mask;
2031 int retval;
Nick Hudson9f8144c2013-12-06 14:01:44 -08002032 int dev_speed;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002033
2034 if (!hsotg->flags.b.port_connect_status) {
2035 /* No longer connected */
2036 dev_err(hsotg->dev, "Not connected\n");
2037 return -ENODEV;
2038 }
2039
Nick Hudson9f8144c2013-12-06 14:01:44 -08002040 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
2041
2042 /* Some configurations cannot support LS traffic on a FS root port */
2043 if ((dev_speed == USB_SPEED_LOW) &&
2044 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
2045 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002046 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Nick Hudson9f8144c2013-12-06 14:01:44 -08002047 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
2048
2049 if (prtspd == HPRT0_SPD_FULL_SPEED)
2050 return -ENODEV;
2051 }
2052
Paul Zimmerman7359d482013-03-11 17:47:59 -07002053 if (!qtd)
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002054 return -EINVAL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002055
2056 dwc2_hcd_qtd_init(qtd, urb);
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002057 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002058 if (retval) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002059 dev_err(hsotg->dev,
2060 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
2061 retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002062 return retval;
2063 }
2064
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002065 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002066 if (!(intr_mask & GINTSTS_SOF)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002067 enum dwc2_transaction_type tr_type;
2068
2069 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
2070 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
2071 /*
2072 * Do not schedule SG transactions until qtd has
2073 * URB_GIVEBACK_ASAP set
2074 */
2075 return 0;
2076
Paul Zimmerman7359d482013-03-11 17:47:59 -07002077 tr_type = dwc2_hcd_select_transactions(hsotg);
2078 if (tr_type != DWC2_TRANSACTION_NONE)
2079 dwc2_hcd_queue_transactions(hsotg, tr_type);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002080 }
2081
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002082 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002083}
2084
2085/* Must be called with interrupt disabled and spinlock held */
2086static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
2087 struct dwc2_hcd_urb *urb)
2088{
2089 struct dwc2_qh *qh;
2090 struct dwc2_qtd *urb_qtd;
2091
2092 urb_qtd = urb->qtd;
2093 if (!urb_qtd) {
2094 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
2095 return -EINVAL;
2096 }
2097
2098 qh = urb_qtd->qh;
2099 if (!qh) {
2100 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
2101 return -EINVAL;
2102 }
2103
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002104 urb->priv = NULL;
2105
Paul Zimmerman7359d482013-03-11 17:47:59 -07002106 if (urb_qtd->in_process && qh->channel) {
2107 dwc2_dump_channel_info(hsotg, qh->channel);
2108
2109 /* The QTD is in process (it has been assigned to a channel) */
2110 if (hsotg->flags.b.port_connect_status)
2111 /*
2112 * If still connected (i.e. in host mode), halt the
2113 * channel so it can be used for other transfers. If
2114 * no longer connected, the host registers can't be
2115 * written to halt the channel since the core is in
2116 * device mode.
2117 */
2118 dwc2_hc_halt(hsotg, qh->channel,
2119 DWC2_HC_XFER_URB_DEQUEUE);
2120 }
2121
2122 /*
2123 * Free the QTD and clean up the associated QH. Leave the QH in the
2124 * schedule if it has any remaining QTDs.
2125 */
John Youn95832c02017-01-23 14:57:26 -08002126 if (!hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002127 u8 in_process = urb_qtd->in_process;
2128
2129 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2130 if (in_process) {
2131 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
2132 qh->channel = NULL;
2133 } else if (list_empty(&qh->qtd_list)) {
2134 dwc2_hcd_qh_unlink(hsotg, qh);
2135 }
2136 } else {
2137 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2138 }
2139
2140 return 0;
2141}
2142
2143/* Must NOT be called with interrupt disabled or spinlock held */
2144static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
2145 struct usb_host_endpoint *ep, int retry)
2146{
2147 struct dwc2_qtd *qtd, *qtd_tmp;
2148 struct dwc2_qh *qh;
2149 unsigned long flags;
2150 int rc;
2151
2152 spin_lock_irqsave(&hsotg->lock, flags);
2153
2154 qh = ep->hcpriv;
2155 if (!qh) {
2156 rc = -EINVAL;
2157 goto err;
2158 }
2159
2160 while (!list_empty(&qh->qtd_list) && retry--) {
2161 if (retry == 0) {
2162 dev_err(hsotg->dev,
2163 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
2164 rc = -EBUSY;
2165 goto err;
2166 }
2167
2168 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01002169 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002170 spin_lock_irqsave(&hsotg->lock, flags);
2171 qh = ep->hcpriv;
2172 if (!qh) {
2173 rc = -EINVAL;
2174 goto err;
2175 }
2176 }
2177
2178 dwc2_hcd_qh_unlink(hsotg, qh);
2179
2180 /* Free each QTD in the QH's QTD list */
2181 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2182 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2183
2184 ep->hcpriv = NULL;
Douglas Anderson16e80212016-01-28 18:19:55 -08002185
2186 if (qh->channel && qh->channel->qh == qh)
2187 qh->channel->qh = NULL;
2188
Paul Zimmerman7359d482013-03-11 17:47:59 -07002189 spin_unlock_irqrestore(&hsotg->lock, flags);
Douglas Anderson16e80212016-01-28 18:19:55 -08002190
Paul Zimmerman7359d482013-03-11 17:47:59 -07002191 dwc2_hcd_qh_free(hsotg, qh);
2192
2193 return 0;
2194
2195err:
2196 ep->hcpriv = NULL;
2197 spin_unlock_irqrestore(&hsotg->lock, flags);
2198
2199 return rc;
2200}
2201
2202/* Must be called with interrupt disabled and spinlock held */
2203static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2204 struct usb_host_endpoint *ep)
2205{
2206 struct dwc2_qh *qh = ep->hcpriv;
2207
2208 if (!qh)
2209 return -EINVAL;
2210
2211 qh->data_toggle = DWC2_HC_PID_DATA0;
2212
2213 return 0;
2214}
2215
John Younb02038fa2016-02-23 19:55:00 -08002216/**
2217 * dwc2_core_init() - Initializes the DWC_otg controller registers and
2218 * prepares the core for device mode or host mode operation
2219 *
2220 * @hsotg: Programming view of the DWC_otg controller
2221 * @initial_setup: If true then this is the first init for this instance.
2222 */
2223static int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2224{
2225 u32 usbcfg, otgctl;
2226 int retval;
2227
2228 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2229
2230 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2231
2232 /* Set ULPI External VBUS bit if needed */
2233 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
John Youn95832c02017-01-23 14:57:26 -08002234 if (hsotg->params.phy_ulpi_ext_vbus)
John Younb02038fa2016-02-23 19:55:00 -08002235 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2236
2237 /* Set external TS Dline pulsing bit if needed */
2238 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
John Youn95832c02017-01-23 14:57:26 -08002239 if (hsotg->params.ts_dline)
John Younb02038fa2016-02-23 19:55:00 -08002240 usbcfg |= GUSBCFG_TERMSELDLPULSE;
2241
2242 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2243
2244 /*
2245 * Reset the Controller
2246 *
2247 * We only need to reset the controller if this is a re-init.
2248 * For the first init we know for sure that earlier code reset us (it
2249 * needed to in order to properly detect various parameters).
2250 */
2251 if (!initial_setup) {
2252 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
2253 if (retval) {
2254 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2255 __func__);
2256 return retval;
2257 }
2258 }
2259
2260 /*
2261 * This needs to happen in FS mode before any other programming occurs
2262 */
2263 retval = dwc2_phy_init(hsotg, initial_setup);
2264 if (retval)
2265 return retval;
2266
2267 /* Program the GAHBCFG Register */
2268 retval = dwc2_gahbcfg_init(hsotg);
2269 if (retval)
2270 return retval;
2271
2272 /* Program the GUSBCFG register */
2273 dwc2_gusbcfg_init(hsotg);
2274
2275 /* Program the GOTGCTL register */
2276 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2277 otgctl &= ~GOTGCTL_OTGVER;
John Younb02038fa2016-02-23 19:55:00 -08002278 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
John Younb02038fa2016-02-23 19:55:00 -08002279
2280 /* Clear the SRP success bit for FS-I2c */
2281 hsotg->srp_success = 0;
2282
2283 /* Enable common interrupts */
2284 dwc2_enable_common_interrupts(hsotg);
2285
2286 /*
2287 * Do device or host initialization based on mode during PCD and
2288 * HCD initialization
2289 */
2290 if (dwc2_is_host_mode(hsotg)) {
2291 dev_dbg(hsotg->dev, "Host Mode\n");
2292 hsotg->op_state = OTG_STATE_A_HOST;
2293 } else {
2294 dev_dbg(hsotg->dev, "Device Mode\n");
2295 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2296 }
2297
2298 return 0;
2299}
2300
2301/**
2302 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2303 * Host mode
2304 *
2305 * @hsotg: Programming view of DWC_otg controller
2306 *
2307 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2308 * request queues. Host channels are reset to ensure that they are ready for
2309 * performing transfers.
2310 */
2311static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2312{
2313 u32 hcfg, hfir, otgctl;
2314
2315 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2316
2317 /* Restart the Phy Clock */
2318 dwc2_writel(0, hsotg->regs + PCGCTL);
2319
2320 /* Initialize Host Configuration Register */
2321 dwc2_init_fs_ls_pclk_sel(hsotg);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08002322 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
2323 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
John Younb02038fa2016-02-23 19:55:00 -08002324 hcfg = dwc2_readl(hsotg->regs + HCFG);
2325 hcfg |= HCFG_FSLSSUPP;
2326 dwc2_writel(hcfg, hsotg->regs + HCFG);
2327 }
2328
2329 /*
2330 * This bit allows dynamic reloading of the HFIR register during
2331 * runtime. This bit needs to be programmed during initial configuration
2332 * and its value must not be changed during runtime.
2333 */
John Youn95832c02017-01-23 14:57:26 -08002334 if (hsotg->params.reload_ctl) {
John Younb02038fa2016-02-23 19:55:00 -08002335 hfir = dwc2_readl(hsotg->regs + HFIR);
2336 hfir |= HFIR_RLDCTRL;
2337 dwc2_writel(hfir, hsotg->regs + HFIR);
2338 }
2339
John Youn95832c02017-01-23 14:57:26 -08002340 if (hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002341 u32 op_mode = hsotg->hw_params.op_mode;
2342
2343 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2344 !hsotg->hw_params.dma_desc_enable ||
2345 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2346 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2347 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2348 dev_err(hsotg->dev,
2349 "Hardware does not support descriptor DMA mode -\n");
2350 dev_err(hsotg->dev,
2351 "falling back to buffer DMA mode.\n");
John Youn95832c02017-01-23 14:57:26 -08002352 hsotg->params.dma_desc_enable = false;
John Younb02038fa2016-02-23 19:55:00 -08002353 } else {
2354 hcfg = dwc2_readl(hsotg->regs + HCFG);
2355 hcfg |= HCFG_DESCDMA;
2356 dwc2_writel(hcfg, hsotg->regs + HCFG);
2357 }
2358 }
2359
2360 /* Configure data FIFO sizes */
2361 dwc2_config_fifos(hsotg);
2362
2363 /* TODO - check this */
2364 /* Clear Host Set HNP Enable in the OTG Control Register */
2365 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2366 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2367 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2368
2369 /* Make sure the FIFOs are flushed */
2370 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2371 dwc2_flush_rx_fifo(hsotg);
2372
2373 /* Clear Host Set HNP Enable in the OTG Control Register */
2374 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2375 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2376 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2377
John Youn95832c02017-01-23 14:57:26 -08002378 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002379 int num_channels, i;
2380 u32 hcchar;
2381
2382 /* Flush out any leftover queued requests */
John Younbea8e862016-11-03 17:55:53 -07002383 num_channels = hsotg->params.host_channels;
John Younb02038fa2016-02-23 19:55:00 -08002384 for (i = 0; i < num_channels; i++) {
2385 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2386 hcchar &= ~HCCHAR_CHENA;
2387 hcchar |= HCCHAR_CHDIS;
2388 hcchar &= ~HCCHAR_EPDIR;
2389 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2390 }
2391
2392 /* Halt all channels to put them into a known state */
2393 for (i = 0; i < num_channels; i++) {
John Younb02038fa2016-02-23 19:55:00 -08002394 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2395 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2396 hcchar &= ~HCCHAR_EPDIR;
2397 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2398 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2399 __func__, i);
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002400
2401 if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
2402 HCCHAR_CHENA, 1000)) {
2403 dev_warn(hsotg->dev, "Unable to clear enable on channel %d\n",
2404 i);
2405 }
John Younb02038fa2016-02-23 19:55:00 -08002406 }
2407 }
2408
2409 /* Turn on the vbus power */
2410 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2411 if (hsotg->op_state == OTG_STATE_A_HOST) {
2412 u32 hprt0 = dwc2_read_hprt0(hsotg);
2413
2414 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2415 !!(hprt0 & HPRT0_PWR));
2416 if (!(hprt0 & HPRT0_PWR)) {
2417 hprt0 |= HPRT0_PWR;
2418 dwc2_writel(hprt0, hsotg->regs + HPRT0);
2419 }
2420 }
2421
2422 dwc2_enable_host_interrupts(hsotg);
2423}
2424
Paul Zimmerman7359d482013-03-11 17:47:59 -07002425/*
2426 * Initializes dynamic portions of the DWC_otg HCD state
2427 *
2428 * Must be called with interrupt disabled and spinlock held
2429 */
2430static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2431{
2432 struct dwc2_host_chan *chan, *chan_tmp;
2433 int num_channels;
2434 int i;
2435
2436 hsotg->flags.d32 = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002437 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002438
John Youn95832c02017-01-23 14:57:26 -08002439 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002440 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07002441 hsotg->params.host_channels;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002442 } else {
2443 hsotg->non_periodic_channels = 0;
2444 hsotg->periodic_channels = 0;
2445 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002446
2447 /*
2448 * Put all channels in the free channel list and clean up channel
2449 * states
2450 */
2451 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2452 hc_list_entry)
2453 list_del_init(&chan->hc_list_entry);
2454
John Younbea8e862016-11-03 17:55:53 -07002455 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002456 for (i = 0; i < num_channels; i++) {
2457 chan = hsotg->hc_ptr_array[i];
2458 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2459 dwc2_hc_cleanup(hsotg, chan);
2460 }
2461
2462 /* Initialize the DWC core for host mode operation */
2463 dwc2_core_host_init(hsotg);
2464}
2465
2466static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2467 struct dwc2_host_chan *chan,
2468 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2469{
2470 int hub_addr, hub_port;
2471
2472 chan->do_split = 1;
2473 chan->xact_pos = qtd->isoc_split_pos;
2474 chan->complete_split = qtd->complete_split;
2475 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2476 chan->hub_addr = (u8)hub_addr;
2477 chan->hub_port = (u8)hub_port;
2478}
2479
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002480static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2481 struct dwc2_host_chan *chan,
2482 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002483{
2484 struct dwc2_hcd_urb *urb = qtd->urb;
2485 struct dwc2_hcd_iso_packet_desc *frame_desc;
2486
2487 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2488 case USB_ENDPOINT_XFER_CONTROL:
2489 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2490
2491 switch (qtd->control_phase) {
2492 case DWC2_CONTROL_SETUP:
2493 dev_vdbg(hsotg->dev, " Control setup transaction\n");
2494 chan->do_ping = 0;
2495 chan->ep_is_in = 0;
2496 chan->data_pid_start = DWC2_HC_PID_SETUP;
John Youn95832c02017-01-23 14:57:26 -08002497 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002498 chan->xfer_dma = urb->setup_dma;
2499 else
2500 chan->xfer_buf = urb->setup_packet;
2501 chan->xfer_len = 8;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002502 break;
2503
2504 case DWC2_CONTROL_DATA:
2505 dev_vdbg(hsotg->dev, " Control data transaction\n");
2506 chan->data_pid_start = qtd->data_toggle;
2507 break;
2508
2509 case DWC2_CONTROL_STATUS:
2510 /*
2511 * Direction is opposite of data direction or IN if no
2512 * data
2513 */
2514 dev_vdbg(hsotg->dev, " Control status transaction\n");
2515 if (urb->length == 0)
2516 chan->ep_is_in = 1;
2517 else
2518 chan->ep_is_in =
2519 dwc2_hcd_is_pipe_out(&urb->pipe_info);
2520 if (chan->ep_is_in)
2521 chan->do_ping = 0;
2522 chan->data_pid_start = DWC2_HC_PID_DATA1;
2523 chan->xfer_len = 0;
John Youn95832c02017-01-23 14:57:26 -08002524 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002525 chan->xfer_dma = hsotg->status_buf_dma;
2526 else
2527 chan->xfer_buf = hsotg->status_buf;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002528 break;
2529 }
2530 break;
2531
2532 case USB_ENDPOINT_XFER_BULK:
2533 chan->ep_type = USB_ENDPOINT_XFER_BULK;
2534 break;
2535
2536 case USB_ENDPOINT_XFER_INT:
2537 chan->ep_type = USB_ENDPOINT_XFER_INT;
2538 break;
2539
2540 case USB_ENDPOINT_XFER_ISOC:
2541 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
John Youn95832c02017-01-23 14:57:26 -08002542 if (hsotg->params.dma_desc_enable)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002543 break;
2544
2545 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2546 frame_desc->status = 0;
2547
John Youn95832c02017-01-23 14:57:26 -08002548 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002549 chan->xfer_dma = urb->dma;
2550 chan->xfer_dma += frame_desc->offset +
2551 qtd->isoc_split_offset;
2552 } else {
2553 chan->xfer_buf = urb->buf;
2554 chan->xfer_buf += frame_desc->offset +
2555 qtd->isoc_split_offset;
2556 }
2557
2558 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2559
Paul Zimmerman7359d482013-03-11 17:47:59 -07002560 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2561 if (chan->xfer_len <= 188)
2562 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2563 else
2564 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2565 }
2566 break;
2567 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002568}
2569
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002570#define DWC2_USB_DMA_ALIGN 4
2571
2572struct dma_aligned_buffer {
2573 void *kmalloc_ptr;
2574 void *old_xfer_buffer;
2575 u8 data[0];
2576};
2577
2578static void dwc2_free_dma_aligned_buffer(struct urb *urb)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002579{
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002580 struct dma_aligned_buffer *temp;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002581
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002582 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2583 return;
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002584
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002585 temp = container_of(urb->transfer_buffer,
John Youn9da51972017-01-17 20:30:27 -08002586 struct dma_aligned_buffer, data);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002587
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002588 if (usb_urb_dir_in(urb))
2589 memcpy(temp->old_xfer_buffer, temp->data,
2590 urb->transfer_buffer_length);
2591 urb->transfer_buffer = temp->old_xfer_buffer;
2592 kfree(temp->kmalloc_ptr);
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002593
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002594 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2595}
Paul Zimmerman7359d482013-03-11 17:47:59 -07002596
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002597static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
2598{
2599 struct dma_aligned_buffer *temp, *kmalloc_ptr;
2600 size_t kmalloc_size;
Gregory Herrerodb62b9a2015-04-29 22:09:16 +02002601
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002602 if (urb->num_sgs || urb->sg ||
2603 urb->transfer_buffer_length == 0 ||
2604 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2605 return 0;
2606
2607 /* Allocate a buffer with enough padding for alignment */
2608 kmalloc_size = urb->transfer_buffer_length +
2609 sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
2610
2611 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2612 if (!kmalloc_ptr)
2613 return -ENOMEM;
2614
2615 /* Position our struct dma_aligned_buffer such that data is aligned */
2616 temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
2617 temp->kmalloc_ptr = kmalloc_ptr;
2618 temp->old_xfer_buffer = urb->transfer_buffer;
2619 if (usb_urb_dir_out(urb))
2620 memcpy(temp->data, urb->transfer_buffer,
2621 urb->transfer_buffer_length);
2622 urb->transfer_buffer = temp->data;
2623
2624 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2625
Paul Zimmerman7359d482013-03-11 17:47:59 -07002626 return 0;
2627}
2628
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002629static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
John Youn9da51972017-01-17 20:30:27 -08002630 gfp_t mem_flags)
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002631{
2632 int ret;
2633
2634 /* We assume setup_dma is always aligned; warn if not */
2635 WARN_ON_ONCE(urb->setup_dma &&
2636 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
2637
2638 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
2639 if (ret)
2640 return ret;
2641
2642 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2643 if (ret)
2644 dwc2_free_dma_aligned_buffer(urb);
2645
2646 return ret;
2647}
2648
2649static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2650{
2651 usb_hcd_unmap_urb_for_dma(hcd, urb);
2652 dwc2_free_dma_aligned_buffer(urb);
2653}
2654
Paul Zimmerman7359d482013-03-11 17:47:59 -07002655/**
2656 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2657 * channel and initializes the host channel to perform the transactions. The
2658 * host channel is removed from the free list.
2659 *
2660 * @hsotg: The HCD state structure
2661 * @qh: Transactions from the first QTD for this QH are selected and assigned
2662 * to a free host channel
2663 */
Dom Cobley20f2eb92013-09-23 14:23:34 -07002664static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002665{
2666 struct dwc2_host_chan *chan;
2667 struct dwc2_hcd_urb *urb;
2668 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002669
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002670 if (dbg_qh(qh))
2671 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002672
2673 if (list_empty(&qh->qtd_list)) {
2674 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002675 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002676 }
2677
2678 if (list_empty(&hsotg->free_hc_list)) {
2679 dev_dbg(hsotg->dev, "No free channel to assign\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002680 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002681 }
2682
2683 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2684 hc_list_entry);
2685
Dom Cobley20f2eb92013-09-23 14:23:34 -07002686 /* Remove host channel from free list */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002687 list_del_init(&chan->hc_list_entry);
2688
2689 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2690 urb = qtd->urb;
2691 qh->channel = chan;
2692 qtd->in_process = 1;
2693
2694 /*
2695 * Use usb_pipedevice to determine device address. This address is
2696 * 0 before the SET_ADDRESS command and the correct address afterward.
2697 */
2698 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2699 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2700 chan->speed = qh->dev_speed;
2701 chan->max_packet = dwc2_max_packet(qh->maxp);
2702
2703 chan->xfer_started = 0;
2704 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2705 chan->error_state = (qtd->error_count > 0);
2706 chan->halt_on_queue = 0;
2707 chan->halt_pending = 0;
2708 chan->requests = 0;
2709
2710 /*
2711 * The following values may be modified in the transfer type section
2712 * below. The xfer_len value may be reduced when the transfer is
2713 * started to accommodate the max widths of the XferSize and PktCnt
2714 * fields in the HCTSIZn register.
2715 */
2716
2717 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2718 if (chan->ep_is_in)
2719 chan->do_ping = 0;
2720 else
2721 chan->do_ping = qh->ping_state;
2722
2723 chan->data_pid_start = qh->data_toggle;
2724 chan->multi_count = 1;
2725
Rashika Kheriabb6c3422013-10-26 23:11:22 +05302726 if (urb->actual_length > urb->length &&
John Youn9da51972017-01-17 20:30:27 -08002727 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
Paul Zimmerman84181082013-09-23 14:23:33 -07002728 urb->actual_length = urb->length;
2729
John Youn95832c02017-01-23 14:57:26 -08002730 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002731 chan->xfer_dma = urb->dma + urb->actual_length;
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002732 else
Paul Zimmerman7359d482013-03-11 17:47:59 -07002733 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002734
2735 chan->xfer_len = urb->length - urb->actual_length;
2736 chan->xfer_count = 0;
2737
2738 /* Set the split attributes if required */
2739 if (qh->do_split)
2740 dwc2_hc_init_split(hsotg, chan, qtd, urb);
2741 else
2742 chan->do_split = 0;
2743
2744 /* Set the transfer attributes */
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002745 dwc2_hc_init_xfer(hsotg, chan, qtd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002746
2747 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2748 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2749 /*
2750 * This value may be modified when the transfer is started
2751 * to reflect the actual transfer length
2752 */
2753 chan->multi_count = dwc2_hb_mult(qh->maxp);
2754
John Youn95832c02017-01-23 14:57:26 -08002755 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002756 chan->desc_list_addr = qh->desc_list_dma;
Gregory Herrero95105a92015-11-20 11:49:29 +01002757 chan->desc_list_sz = qh->desc_list_sz;
2758 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002759
2760 dwc2_hc_init(hsotg, chan);
2761 chan->qh = qh;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002762
2763 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002764}
2765
2766/**
2767 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2768 * schedule and assigns them to available host channels. Called from the HCD
2769 * interrupt handler functions.
2770 *
2771 * @hsotg: The HCD state structure
2772 *
2773 * Return: The types of new transactions that were assigned to host channels
2774 */
2775enum dwc2_transaction_type dwc2_hcd_select_transactions(
2776 struct dwc2_hsotg *hsotg)
2777{
2778 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2779 struct list_head *qh_ptr;
2780 struct dwc2_qh *qh;
2781 int num_channels;
2782
2783#ifdef DWC2_DEBUG_SOF
2784 dev_vdbg(hsotg->dev, " Select Transactions\n");
2785#endif
2786
2787 /* Process entries in the periodic ready list */
2788 qh_ptr = hsotg->periodic_sched_ready.next;
2789 while (qh_ptr != &hsotg->periodic_sched_ready) {
2790 if (list_empty(&hsotg->free_hc_list))
2791 break;
John Youn95832c02017-01-23 14:57:26 -08002792 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002793 if (hsotg->available_host_channels <= 1)
2794 break;
2795 hsotg->available_host_channels--;
2796 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002797 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -07002798 if (dwc2_assign_and_init_hc(hsotg, qh))
2799 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002800
2801 /*
2802 * Move the QH from the periodic ready schedule to the
2803 * periodic assigned schedule
2804 */
2805 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002806 list_move_tail(&qh->qh_list_entry,
2807 &hsotg->periodic_sched_assigned);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002808 ret_val = DWC2_TRANSACTION_PERIODIC;
2809 }
2810
2811 /*
2812 * Process entries in the inactive portion of the non-periodic
2813 * schedule. Some free host channels may not be used if they are
2814 * reserved for periodic transfers.
2815 */
John Younbea8e862016-11-03 17:55:53 -07002816 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002817 qh_ptr = hsotg->non_periodic_sched_inactive.next;
2818 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
John Youn95832c02017-01-23 14:57:26 -08002819 if (!hsotg->params.uframe_sched &&
Dom Cobley20f2eb92013-09-23 14:23:34 -07002820 hsotg->non_periodic_channels >= num_channels -
Paul Zimmerman7359d482013-03-11 17:47:59 -07002821 hsotg->periodic_channels)
2822 break;
2823 if (list_empty(&hsotg->free_hc_list))
2824 break;
2825 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
John Youn95832c02017-01-23 14:57:26 -08002826 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002827 if (hsotg->available_host_channels < 1)
2828 break;
2829 hsotg->available_host_channels--;
2830 }
2831
2832 if (dwc2_assign_and_init_hc(hsotg, qh))
2833 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002834
2835 /*
2836 * Move the QH from the non-periodic inactive schedule to the
2837 * non-periodic active schedule
2838 */
2839 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002840 list_move_tail(&qh->qh_list_entry,
2841 &hsotg->non_periodic_sched_active);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002842
2843 if (ret_val == DWC2_TRANSACTION_NONE)
2844 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2845 else
2846 ret_val = DWC2_TRANSACTION_ALL;
2847
John Youn95832c02017-01-23 14:57:26 -08002848 if (!hsotg->params.uframe_sched)
Dom Cobley20f2eb92013-09-23 14:23:34 -07002849 hsotg->non_periodic_channels++;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002850 }
2851
2852 return ret_val;
2853}
2854
2855/**
2856 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2857 * a host channel associated with either a periodic or non-periodic transfer
2858 *
2859 * @hsotg: The HCD state structure
2860 * @chan: Host channel descriptor associated with either a periodic or
2861 * non-periodic transfer
2862 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2863 * for periodic transfers or the non-periodic Tx FIFO
2864 * for non-periodic transfers
2865 *
2866 * Return: 1 if a request is queued and more requests may be needed to
2867 * complete the transfer, 0 if no more requests are required for this
2868 * transfer, -1 if there is insufficient space in the Tx FIFO
2869 *
2870 * This function assumes that there is space available in the appropriate
2871 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2872 * it checks whether space is available in the appropriate Tx FIFO.
2873 *
2874 * Must be called with interrupt disabled and spinlock held
2875 */
2876static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2877 struct dwc2_host_chan *chan,
2878 u16 fifo_dwords_avail)
2879{
2880 int retval = 0;
2881
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002882 if (chan->do_split)
2883 /* Put ourselves on the list to keep order straight */
2884 list_move_tail(&chan->split_order_list_entry,
2885 &hsotg->split_order);
2886
John Youn95832c02017-01-23 14:57:26 -08002887 if (hsotg->params.host_dma) {
2888 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002889 if (!chan->xfer_started ||
2890 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2891 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2892 chan->qh->ping_state = 0;
2893 }
2894 } else if (!chan->xfer_started) {
2895 dwc2_hc_start_transfer(hsotg, chan);
2896 chan->qh->ping_state = 0;
2897 }
2898 } else if (chan->halt_pending) {
2899 /* Don't queue a request if the channel has been halted */
2900 } else if (chan->halt_on_queue) {
2901 dwc2_hc_halt(hsotg, chan, chan->halt_status);
2902 } else if (chan->do_ping) {
2903 if (!chan->xfer_started)
2904 dwc2_hc_start_transfer(hsotg, chan);
2905 } else if (!chan->ep_is_in ||
2906 chan->data_pid_start == DWC2_HC_PID_SETUP) {
2907 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2908 if (!chan->xfer_started) {
2909 dwc2_hc_start_transfer(hsotg, chan);
2910 retval = 1;
2911 } else {
2912 retval = dwc2_hc_continue_transfer(hsotg, chan);
2913 }
2914 } else {
2915 retval = -1;
2916 }
2917 } else {
2918 if (!chan->xfer_started) {
2919 dwc2_hc_start_transfer(hsotg, chan);
2920 retval = 1;
2921 } else {
2922 retval = dwc2_hc_continue_transfer(hsotg, chan);
2923 }
2924 }
2925
2926 return retval;
2927}
2928
2929/*
2930 * Processes periodic channels for the next frame and queues transactions for
2931 * these channels to the DWC_otg controller. After queueing transactions, the
2932 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2933 * to queue as Periodic Tx FIFO or request queue space becomes available.
2934 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2935 *
2936 * Must be called with interrupt disabled and spinlock held
2937 */
2938static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2939{
2940 struct list_head *qh_ptr;
2941 struct dwc2_qh *qh;
2942 u32 tx_status;
2943 u32 fspcavail;
2944 u32 gintmsk;
2945 int status;
Douglas Anderson4e50e012016-01-28 18:20:03 -08002946 bool no_queue_space = false;
2947 bool no_fifo_space = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002948 u32 qspcavail;
2949
Douglas Anderson4e50e012016-01-28 18:20:03 -08002950 /* If empty list then just adjust interrupt enables */
2951 if (list_empty(&hsotg->periodic_sched_assigned))
2952 goto exit;
2953
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002954 if (dbg_perio())
2955 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07002956
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002957 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002958 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2959 TXSTS_QSPCAVAIL_SHIFT;
2960 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2961 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002962
2963 if (dbg_perio()) {
2964 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
2965 qspcavail);
2966 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
2967 fspcavail);
2968 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002969
2970 qh_ptr = hsotg->periodic_sched_assigned.next;
2971 while (qh_ptr != &hsotg->periodic_sched_assigned) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002972 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmanacdb9042013-08-30 18:45:16 +02002973 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2974 TXSTS_QSPCAVAIL_SHIFT;
2975 if (qspcavail == 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01002976 no_queue_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002977 break;
2978 }
2979
2980 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2981 if (!qh->channel) {
2982 qh_ptr = qh_ptr->next;
2983 continue;
2984 }
2985
2986 /* Make sure EP's TT buffer is clean before queueing qtds */
2987 if (qh->tt_buffer_dirty) {
2988 qh_ptr = qh_ptr->next;
2989 continue;
2990 }
2991
2992 /*
2993 * Set a flag if we're queuing high-bandwidth in slave mode.
2994 * The flag prevents any halts to get into the request queue in
2995 * the middle of multiple high-bandwidth packets getting queued.
2996 */
John Youn95832c02017-01-23 14:57:26 -08002997 if (!hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08002998 qh->channel->multi_count > 1)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002999 hsotg->queuing_high_bandwidth = 1;
3000
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003001 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3002 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003003 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3004 if (status < 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003005 no_fifo_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003006 break;
3007 }
3008
3009 /*
3010 * In Slave mode, stay on the current transfer until there is
3011 * nothing more to do or the high-bandwidth request count is
3012 * reached. In DMA mode, only need to queue one request. The
3013 * controller automatically handles multiple packets for
3014 * high-bandwidth transfers.
3015 */
John Youn95832c02017-01-23 14:57:26 -08003016 if (hsotg->params.host_dma || status == 0 ||
Paul Zimmerman7359d482013-03-11 17:47:59 -07003017 qh->channel->requests == qh->channel->multi_count) {
3018 qh_ptr = qh_ptr->next;
3019 /*
3020 * Move the QH from the periodic assigned schedule to
3021 * the periodic queued schedule
3022 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08003023 list_move_tail(&qh->qh_list_entry,
3024 &hsotg->periodic_sched_queued);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003025
3026 /* done queuing high bandwidth */
3027 hsotg->queuing_high_bandwidth = 0;
3028 }
3029 }
3030
Douglas Anderson4e50e012016-01-28 18:20:03 -08003031exit:
3032 if (no_queue_space || no_fifo_space ||
John Youn95832c02017-01-23 14:57:26 -08003033 (!hsotg->params.host_dma &&
Douglas Anderson4e50e012016-01-28 18:20:03 -08003034 !list_empty(&hsotg->periodic_sched_assigned))) {
3035 /*
3036 * May need to queue more transactions as the request
3037 * queue or Tx FIFO empties. Enable the periodic Tx
3038 * FIFO empty interrupt. (Always use the half-empty
3039 * level to ensure that new requests are loaded as
3040 * soon as possible.)
3041 */
3042 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3043 if (!(gintmsk & GINTSTS_PTXFEMP)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003044 gintmsk |= GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003045 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Douglas Anderson4e50e012016-01-28 18:20:03 -08003046 }
3047 } else {
3048 /*
3049 * Disable the Tx FIFO empty interrupt since there are
3050 * no more transactions that need to be queued right
3051 * now. This function is called from interrupt
3052 * handlers to queue more transactions as transfer
3053 * states change.
John Youn38beaec2017-01-17 20:31:13 -08003054 */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003055 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3056 if (gintmsk & GINTSTS_PTXFEMP) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003057 gintmsk &= ~GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003058 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003059 }
3060 }
3061}
3062
3063/*
3064 * Processes active non-periodic channels and queues transactions for these
3065 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3066 * FIFO Empty interrupt is enabled if there are more transactions to queue as
3067 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3068 * FIFO Empty interrupt is disabled.
3069 *
3070 * Must be called with interrupt disabled and spinlock held
3071 */
3072static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3073{
3074 struct list_head *orig_qh_ptr;
3075 struct dwc2_qh *qh;
3076 u32 tx_status;
3077 u32 qspcavail;
3078 u32 fspcavail;
3079 u32 gintmsk;
3080 int status;
3081 int no_queue_space = 0;
3082 int no_fifo_space = 0;
3083 int more_to_do = 0;
3084
3085 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3086
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003087 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003088 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3089 TXSTS_QSPCAVAIL_SHIFT;
3090 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3091 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003092 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
3093 qspcavail);
3094 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
3095 fspcavail);
3096
3097 /*
3098 * Keep track of the starting point. Skip over the start-of-list
3099 * entry.
3100 */
3101 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3102 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3103 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3104
3105 /*
3106 * Process once through the active list or until no more space is
3107 * available in the request queue or the Tx FIFO
3108 */
3109 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003110 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003111 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3112 TXSTS_QSPCAVAIL_SHIFT;
John Youn95832c02017-01-23 14:57:26 -08003113 if (!hsotg->params.host_dma && qspcavail == 0) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003114 no_queue_space = 1;
3115 break;
3116 }
3117
3118 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3119 qh_list_entry);
3120 if (!qh->channel)
3121 goto next;
3122
3123 /* Make sure EP's TT buffer is clean before queueing qtds */
3124 if (qh->tt_buffer_dirty)
3125 goto next;
3126
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003127 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3128 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003129 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3130
3131 if (status > 0) {
3132 more_to_do = 1;
3133 } else if (status < 0) {
3134 no_fifo_space = 1;
3135 break;
3136 }
3137next:
3138 /* Advance to next QH, skipping start-of-list entry */
3139 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3140 if (hsotg->non_periodic_qh_ptr ==
3141 &hsotg->non_periodic_sched_active)
3142 hsotg->non_periodic_qh_ptr =
3143 hsotg->non_periodic_qh_ptr->next;
3144 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3145
John Youn95832c02017-01-23 14:57:26 -08003146 if (!hsotg->params.host_dma) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003147 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003148 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3149 TXSTS_QSPCAVAIL_SHIFT;
3150 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3151 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003152 dev_vdbg(hsotg->dev,
3153 " NP Tx Req Queue Space Avail (after queue): %d\n",
3154 qspcavail);
3155 dev_vdbg(hsotg->dev,
3156 " NP Tx FIFO Space Avail (after queue): %d\n",
3157 fspcavail);
3158
3159 if (more_to_do || no_queue_space || no_fifo_space) {
3160 /*
3161 * May need to queue more transactions as the request
3162 * queue or Tx FIFO empties. Enable the non-periodic
3163 * Tx FIFO empty interrupt. (Always use the half-empty
3164 * level to ensure that new requests are loaded as
3165 * soon as possible.)
3166 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003167 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003168 gintmsk |= GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003169 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003170 } else {
3171 /*
3172 * Disable the Tx FIFO empty interrupt since there are
3173 * no more transactions that need to be queued right
3174 * now. This function is called from interrupt
3175 * handlers to queue more transactions as transfer
3176 * states change.
3177 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003178 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003179 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003180 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003181 }
3182 }
3183}
3184
3185/**
3186 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3187 * and queues transactions for these channels to the DWC_otg controller. Called
3188 * from the HCD interrupt handler functions.
3189 *
3190 * @hsotg: The HCD state structure
3191 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3192 * or both)
3193 *
3194 * Must be called with interrupt disabled and spinlock held
3195 */
3196void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3197 enum dwc2_transaction_type tr_type)
3198{
3199#ifdef DWC2_DEBUG_SOF
3200 dev_vdbg(hsotg->dev, "Queue Transactions\n");
3201#endif
3202 /* Process host channels associated with periodic transfers */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003203 if (tr_type == DWC2_TRANSACTION_PERIODIC ||
3204 tr_type == DWC2_TRANSACTION_ALL)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003205 dwc2_process_periodic_channels(hsotg);
3206
3207 /* Process host channels associated with non-periodic transfers */
3208 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3209 tr_type == DWC2_TRANSACTION_ALL) {
3210 if (!list_empty(&hsotg->non_periodic_sched_active)) {
3211 dwc2_process_non_periodic_channels(hsotg);
3212 } else {
3213 /*
3214 * Ensure NP Tx FIFO empty interrupt is disabled when
3215 * there are no non-periodic transfers to process
3216 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003217 u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003218
3219 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003220 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003221 }
3222 }
3223}
3224
3225static void dwc2_conn_id_status_change(struct work_struct *work)
3226{
3227 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3228 wf_otg);
3229 u32 count = 0;
3230 u32 gotgctl;
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003231 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003232
3233 dev_dbg(hsotg->dev, "%s()\n", __func__);
3234
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003235 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003236 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3237 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3238 !!(gotgctl & GOTGCTL_CONID_B));
3239
3240 /* B-Device connector (Device Mode) */
3241 if (gotgctl & GOTGCTL_CONID_B) {
3242 /* Wait for switch to device mode */
3243 dev_dbg(hsotg->dev, "connId B\n");
Chen Yu9156a7e2017-01-23 14:59:57 -08003244 if (hsotg->bus_suspended) {
3245 dev_info(hsotg->dev,
3246 "Do port resume before switching to device mode\n");
3247 dwc2_port_resume(hsotg);
3248 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003249 while (!dwc2_is_device_mode(hsotg)) {
3250 dev_info(hsotg->dev,
3251 "Waiting for Peripheral Mode, Mode=%s\n",
3252 dwc2_is_host_mode(hsotg) ? "Host" :
3253 "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003254 msleep(20);
John Stultzfc30c4b2017-01-23 14:59:35 -08003255 /*
3256 * Sometimes the initial GOTGCTRL read is wrong, so
3257 * check it again and jump to host mode if that was
3258 * the case.
3259 */
3260 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3261 if (!(gotgctl & GOTGCTL_CONID_B))
3262 goto host;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003263 if (++count > 250)
3264 break;
3265 }
3266 if (count > 250)
3267 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003268 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003269 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003270 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003271 dwc2_enable_global_interrupts(hsotg);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003272 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003273 dwc2_hsotg_core_init_disconnected(hsotg, false);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003274 spin_unlock_irqrestore(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003275 dwc2_hsotg_core_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003276 } else {
John Stultzfc30c4b2017-01-23 14:59:35 -08003277host:
Paul Zimmerman7359d482013-03-11 17:47:59 -07003278 /* A-Device connector (Host Mode) */
3279 dev_dbg(hsotg->dev, "connId A\n");
3280 while (!dwc2_is_host_mode(hsotg)) {
3281 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3282 dwc2_is_host_mode(hsotg) ?
3283 "Host" : "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003284 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003285 if (++count > 250)
3286 break;
3287 }
3288 if (count > 250)
3289 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003290 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003291
John Stultzd2471d42017-10-23 14:32:48 -07003292 spin_lock_irqsave(&hsotg->lock, flags);
3293 dwc2_hsotg_disconnect(hsotg);
3294 spin_unlock_irqrestore(&hsotg->lock, flags);
3295
3296 hsotg->op_state = OTG_STATE_A_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003297 /* Initialize the Core for Host mode */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003298 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003299 dwc2_enable_global_interrupts(hsotg);
3300 dwc2_hcd_start(hsotg);
3301 }
3302}
3303
Kees Cooke99e88a2017-10-16 14:43:17 -07003304static void dwc2_wakeup_detected(struct timer_list *t)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003305{
Kees Cooke99e88a2017-10-16 14:43:17 -07003306 struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003307 u32 hprt0;
3308
3309 dev_dbg(hsotg->dev, "%s()\n", __func__);
3310
3311 /*
3312 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3313 * so that OPT tests pass with all PHYs.)
3314 */
3315 hprt0 = dwc2_read_hprt0(hsotg);
3316 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3317 hprt0 &= ~HPRT0_RES;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003318 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003319 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003320 dwc2_readl(hsotg->regs + HPRT0));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003321
3322 dwc2_hcd_rem_wakeup(hsotg);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003323 hsotg->bus_suspended = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003324
3325 /* Change to L0 state */
3326 hsotg->lx_state = DWC2_L0;
3327}
3328
3329static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3330{
3331 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3332
3333 return hcd->self.b_hnp_enable;
3334}
3335
3336/* Must NOT be called with interrupt disabled or spinlock held */
3337static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3338{
3339 unsigned long flags;
3340 u32 hprt0;
3341 u32 pcgctl;
3342 u32 gotgctl;
3343
3344 dev_dbg(hsotg->dev, "%s()\n", __func__);
3345
3346 spin_lock_irqsave(&hsotg->lock, flags);
3347
3348 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003349 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003350 gotgctl |= GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003351 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003352 hsotg->op_state = OTG_STATE_A_SUSPEND;
3353 }
3354
3355 hprt0 = dwc2_read_hprt0(hsotg);
3356 hprt0 |= HPRT0_SUSP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003357 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003358
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003359 hsotg->bus_suspended = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003360
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003361 /*
3362 * If hibernation is supported, Phy clock will be suspended
3363 * after registers are backuped.
3364 */
John Younbea8e862016-11-03 17:55:53 -07003365 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003366 /* Suspend the Phy Clock */
3367 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3368 pcgctl |= PCGCTL_STOPPCLK;
3369 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3370 udelay(10);
3371 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003372
3373 /* For HNP the bus must be suspended for at least 200ms */
3374 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003375 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003376 pcgctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003377 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003378
3379 spin_unlock_irqrestore(&hsotg->lock, flags);
3380
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003381 msleep(200);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003382 } else {
3383 spin_unlock_irqrestore(&hsotg->lock, flags);
3384 }
3385}
3386
Gregory Herrero30db1032015-09-22 15:16:38 +02003387/* Must NOT be called with interrupt disabled or spinlock held */
3388static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
3389{
3390 unsigned long flags;
3391 u32 hprt0;
3392 u32 pcgctl;
3393
Douglas Anderson4d273c22015-10-14 15:58:27 -07003394 spin_lock_irqsave(&hsotg->lock, flags);
3395
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003396 /*
3397 * If hibernation is supported, Phy clock is already resumed
3398 * after registers restore.
3399 */
John Younbea8e862016-11-03 17:55:53 -07003400 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003401 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3402 pcgctl &= ~PCGCTL_STOPPCLK;
3403 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003404 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003405 msleep(20);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003406 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003407 }
Gregory Herrero30db1032015-09-22 15:16:38 +02003408
Gregory Herrero30db1032015-09-22 15:16:38 +02003409 hprt0 = dwc2_read_hprt0(hsotg);
3410 hprt0 |= HPRT0_RES;
3411 hprt0 &= ~HPRT0_SUSP;
3412 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3413 spin_unlock_irqrestore(&hsotg->lock, flags);
3414
3415 msleep(USB_RESUME_TIMEOUT);
3416
3417 spin_lock_irqsave(&hsotg->lock, flags);
3418 hprt0 = dwc2_read_hprt0(hsotg);
3419 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
3420 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003421 hsotg->bus_suspended = false;
Gregory Herrero30db1032015-09-22 15:16:38 +02003422 spin_unlock_irqrestore(&hsotg->lock, flags);
3423}
3424
Paul Zimmerman7359d482013-03-11 17:47:59 -07003425/* Handles hub class-specific requests */
3426static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3427 u16 wvalue, u16 windex, char *buf, u16 wlength)
3428{
3429 struct usb_hub_descriptor *hub_desc;
3430 int retval = 0;
3431 u32 hprt0;
3432 u32 port_status;
3433 u32 speed;
3434 u32 pcgctl;
3435
3436 switch (typereq) {
3437 case ClearHubFeature:
3438 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3439
3440 switch (wvalue) {
3441 case C_HUB_LOCAL_POWER:
3442 case C_HUB_OVER_CURRENT:
3443 /* Nothing required here */
3444 break;
3445
3446 default:
3447 retval = -EINVAL;
3448 dev_err(hsotg->dev,
3449 "ClearHubFeature request %1xh unknown\n",
3450 wvalue);
3451 }
3452 break;
3453
3454 case ClearPortFeature:
3455 if (wvalue != USB_PORT_FEAT_L1)
3456 if (!windex || windex > 1)
3457 goto error;
3458 switch (wvalue) {
3459 case USB_PORT_FEAT_ENABLE:
3460 dev_dbg(hsotg->dev,
3461 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3462 hprt0 = dwc2_read_hprt0(hsotg);
3463 hprt0 |= HPRT0_ENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003464 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003465 break;
3466
3467 case USB_PORT_FEAT_SUSPEND:
3468 dev_dbg(hsotg->dev,
3469 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00003470
Gregory Herrerobea78552015-09-22 15:16:44 +02003471 if (hsotg->bus_suspended)
3472 dwc2_port_resume(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003473 break;
3474
3475 case USB_PORT_FEAT_POWER:
3476 dev_dbg(hsotg->dev,
3477 "ClearPortFeature USB_PORT_FEAT_POWER\n");
3478 hprt0 = dwc2_read_hprt0(hsotg);
3479 hprt0 &= ~HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003480 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003481 break;
3482
3483 case USB_PORT_FEAT_INDICATOR:
3484 dev_dbg(hsotg->dev,
3485 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3486 /* Port indicator not supported */
3487 break;
3488
3489 case USB_PORT_FEAT_C_CONNECTION:
3490 /*
3491 * Clears driver's internal Connect Status Change flag
3492 */
3493 dev_dbg(hsotg->dev,
3494 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3495 hsotg->flags.b.port_connect_status_change = 0;
3496 break;
3497
3498 case USB_PORT_FEAT_C_RESET:
3499 /* Clears driver's internal Port Reset Change flag */
3500 dev_dbg(hsotg->dev,
3501 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3502 hsotg->flags.b.port_reset_change = 0;
3503 break;
3504
3505 case USB_PORT_FEAT_C_ENABLE:
3506 /*
3507 * Clears the driver's internal Port Enable/Disable
3508 * Change flag
3509 */
3510 dev_dbg(hsotg->dev,
3511 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3512 hsotg->flags.b.port_enable_change = 0;
3513 break;
3514
3515 case USB_PORT_FEAT_C_SUSPEND:
3516 /*
3517 * Clears the driver's internal Port Suspend Change
3518 * flag, which is set when resume signaling on the host
3519 * port is complete
3520 */
3521 dev_dbg(hsotg->dev,
3522 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3523 hsotg->flags.b.port_suspend_change = 0;
3524 break;
3525
3526 case USB_PORT_FEAT_C_PORT_L1:
3527 dev_dbg(hsotg->dev,
3528 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3529 hsotg->flags.b.port_l1_change = 0;
3530 break;
3531
3532 case USB_PORT_FEAT_C_OVER_CURRENT:
3533 dev_dbg(hsotg->dev,
3534 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3535 hsotg->flags.b.port_over_current_change = 0;
3536 break;
3537
3538 default:
3539 retval = -EINVAL;
3540 dev_err(hsotg->dev,
3541 "ClearPortFeature request %1xh unknown or unsupported\n",
3542 wvalue);
3543 }
3544 break;
3545
3546 case GetHubDescriptor:
3547 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3548 hub_desc = (struct usb_hub_descriptor *)buf;
3549 hub_desc->bDescLength = 9;
Sergei Shtylyova5dd0392015-03-29 01:36:28 +03003550 hub_desc->bDescriptorType = USB_DT_HUB;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003551 hub_desc->bNbrPorts = 1;
Sergei Shtylyov3d040de2015-01-19 01:54:15 +03003552 hub_desc->wHubCharacteristics =
3553 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
3554 HUB_CHAR_INDV_PORT_OCPM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003555 hub_desc->bPwrOn2PwrGood = 1;
3556 hub_desc->bHubContrCurrent = 0;
3557 hub_desc->u.hs.DeviceRemovable[0] = 0;
3558 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3559 break;
3560
3561 case GetHubStatus:
3562 dev_dbg(hsotg->dev, "GetHubStatus\n");
3563 memset(buf, 0, 4);
3564 break;
3565
3566 case GetPortStatus:
Paul Zimmermanb8313412013-05-24 16:32:12 -07003567 dev_vdbg(hsotg->dev,
3568 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3569 hsotg->flags.d32);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003570 if (!windex || windex > 1)
3571 goto error;
3572
3573 port_status = 0;
3574 if (hsotg->flags.b.port_connect_status_change)
3575 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3576 if (hsotg->flags.b.port_enable_change)
3577 port_status |= USB_PORT_STAT_C_ENABLE << 16;
3578 if (hsotg->flags.b.port_suspend_change)
3579 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3580 if (hsotg->flags.b.port_l1_change)
3581 port_status |= USB_PORT_STAT_C_L1 << 16;
3582 if (hsotg->flags.b.port_reset_change)
3583 port_status |= USB_PORT_STAT_C_RESET << 16;
3584 if (hsotg->flags.b.port_over_current_change) {
3585 dev_warn(hsotg->dev, "Overcurrent change detected\n");
3586 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3587 }
3588
3589 if (!hsotg->flags.b.port_connect_status) {
3590 /*
3591 * The port is disconnected, which means the core is
3592 * either in device mode or it soon will be. Just
3593 * return 0's for the remainder of the port status
3594 * since the port register can't be read if the core
3595 * is in device mode.
3596 */
3597 *(__le32 *)buf = cpu_to_le32(port_status);
3598 break;
3599 }
3600
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003601 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmermanb8313412013-05-24 16:32:12 -07003602 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003603
3604 if (hprt0 & HPRT0_CONNSTS)
3605 port_status |= USB_PORT_STAT_CONNECTION;
3606 if (hprt0 & HPRT0_ENA)
3607 port_status |= USB_PORT_STAT_ENABLE;
3608 if (hprt0 & HPRT0_SUSP)
3609 port_status |= USB_PORT_STAT_SUSPEND;
3610 if (hprt0 & HPRT0_OVRCURRACT)
3611 port_status |= USB_PORT_STAT_OVERCURRENT;
3612 if (hprt0 & HPRT0_RST)
3613 port_status |= USB_PORT_STAT_RESET;
3614 if (hprt0 & HPRT0_PWR)
3615 port_status |= USB_PORT_STAT_POWER;
3616
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02003617 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003618 if (speed == HPRT0_SPD_HIGH_SPEED)
3619 port_status |= USB_PORT_STAT_HIGH_SPEED;
3620 else if (speed == HPRT0_SPD_LOW_SPEED)
3621 port_status |= USB_PORT_STAT_LOW_SPEED;
3622
3623 if (hprt0 & HPRT0_TSTCTL_MASK)
3624 port_status |= USB_PORT_STAT_TEST;
3625 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3626
John Younbea8e862016-11-03 17:55:53 -07003627 if (hsotg->params.dma_desc_fs_enable) {
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003628 /*
3629 * Enable descriptor DMA only if a full speed
3630 * device is connected.
3631 */
3632 if (hsotg->new_connection &&
3633 ((port_status &
3634 (USB_PORT_STAT_CONNECTION |
3635 USB_PORT_STAT_HIGH_SPEED |
3636 USB_PORT_STAT_LOW_SPEED)) ==
3637 USB_PORT_STAT_CONNECTION)) {
3638 u32 hcfg;
3639
3640 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
John Youn95832c02017-01-23 14:57:26 -08003641 hsotg->params.dma_desc_enable = true;
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003642 hcfg = dwc2_readl(hsotg->regs + HCFG);
3643 hcfg |= HCFG_DESCDMA;
3644 dwc2_writel(hcfg, hsotg->regs + HCFG);
3645 hsotg->new_connection = false;
3646 }
3647 }
3648
Paul Zimmermanb8313412013-05-24 16:32:12 -07003649 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003650 *(__le32 *)buf = cpu_to_le32(port_status);
3651 break;
3652
3653 case SetHubFeature:
3654 dev_dbg(hsotg->dev, "SetHubFeature\n");
3655 /* No HUB features supported */
3656 break;
3657
3658 case SetPortFeature:
3659 dev_dbg(hsotg->dev, "SetPortFeature\n");
3660 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3661 goto error;
3662
3663 if (!hsotg->flags.b.port_connect_status) {
3664 /*
3665 * The port is disconnected, which means the core is
3666 * either in device mode or it soon will be. Just
3667 * return without doing anything since the port
3668 * register can't be written if the core is in device
3669 * mode.
3670 */
3671 break;
3672 }
3673
3674 switch (wvalue) {
3675 case USB_PORT_FEAT_SUSPEND:
3676 dev_dbg(hsotg->dev,
3677 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3678 if (windex != hsotg->otg_port)
3679 goto error;
3680 dwc2_port_suspend(hsotg, windex);
3681 break;
3682
3683 case USB_PORT_FEAT_POWER:
3684 dev_dbg(hsotg->dev,
3685 "SetPortFeature - USB_PORT_FEAT_POWER\n");
3686 hprt0 = dwc2_read_hprt0(hsotg);
3687 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003688 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003689 break;
3690
3691 case USB_PORT_FEAT_RESET:
3692 hprt0 = dwc2_read_hprt0(hsotg);
3693 dev_dbg(hsotg->dev,
3694 "SetPortFeature - USB_PORT_FEAT_RESET\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003695 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003696 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003697 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003698 /* ??? Original driver does this */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003699 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003700
3701 hprt0 = dwc2_read_hprt0(hsotg);
3702 /* Clear suspend bit if resetting from suspend state */
3703 hprt0 &= ~HPRT0_SUSP;
3704
3705 /*
3706 * When B-Host the Port reset bit is set in the Start
3707 * HCD Callback function, so that the reset is started
3708 * within 1ms of the HNP success interrupt
3709 */
3710 if (!dwc2_hcd_is_b_host(hsotg)) {
3711 hprt0 |= HPRT0_PWR | HPRT0_RST;
3712 dev_dbg(hsotg->dev,
3713 "In host mode, hprt0=%08x\n", hprt0);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003714 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003715 }
3716
3717 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003718 msleep(50);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003719 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003720 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003721 hsotg->lx_state = DWC2_L0; /* Now back to On state */
3722 break;
3723
3724 case USB_PORT_FEAT_INDICATOR:
3725 dev_dbg(hsotg->dev,
3726 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3727 /* Not supported */
3728 break;
3729
Jingwu Lin96d480e2015-04-29 22:09:17 +02003730 case USB_PORT_FEAT_TEST:
3731 hprt0 = dwc2_read_hprt0(hsotg);
3732 dev_dbg(hsotg->dev,
3733 "SetPortFeature - USB_PORT_FEAT_TEST\n");
3734 hprt0 &= ~HPRT0_TSTCTL_MASK;
3735 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003736 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Jingwu Lin96d480e2015-04-29 22:09:17 +02003737 break;
3738
Paul Zimmerman7359d482013-03-11 17:47:59 -07003739 default:
3740 retval = -EINVAL;
3741 dev_err(hsotg->dev,
3742 "SetPortFeature %1xh unknown or unsupported\n",
3743 wvalue);
3744 break;
3745 }
3746 break;
3747
3748 default:
3749error:
3750 retval = -EINVAL;
3751 dev_dbg(hsotg->dev,
3752 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3753 typereq, windex, wvalue);
3754 break;
3755 }
3756
3757 return retval;
3758}
3759
3760static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3761{
3762 int retval;
3763
Paul Zimmerman7359d482013-03-11 17:47:59 -07003764 if (port != 1)
3765 return -EINVAL;
3766
3767 retval = (hsotg->flags.b.port_connect_status_change ||
3768 hsotg->flags.b.port_reset_change ||
3769 hsotg->flags.b.port_enable_change ||
3770 hsotg->flags.b.port_suspend_change ||
3771 hsotg->flags.b.port_over_current_change);
3772
3773 if (retval) {
3774 dev_dbg(hsotg->dev,
3775 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3776 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
3777 hsotg->flags.b.port_connect_status_change);
3778 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
3779 hsotg->flags.b.port_reset_change);
3780 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
3781 hsotg->flags.b.port_enable_change);
3782 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
3783 hsotg->flags.b.port_suspend_change);
3784 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
3785 hsotg->flags.b.port_over_current_change);
3786 }
3787
3788 return retval;
3789}
3790
3791int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3792{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003793 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003794
3795#ifdef DWC2_DEBUG_SOF
3796 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003797 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003798#endif
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003799 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003800}
3801
Douglas Andersonfae4e822016-01-28 18:20:10 -08003802int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3803{
3804 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
3805 u32 hfir = dwc2_readl(hsotg->regs + HFIR);
3806 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3807 unsigned int us_per_frame;
3808 unsigned int frame_number;
3809 unsigned int remaining;
3810 unsigned int interval;
3811 unsigned int phy_clks;
3812
3813 /* High speed has 125 us per (micro) frame; others are 1 ms per */
3814 us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3815
3816 /* Extract fields */
3817 frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3818 remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3819 interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3820
3821 /*
3822 * Number of phy clocks since the last tick of the frame number after
3823 * "us" has passed.
3824 */
3825 phy_clks = (interval - remaining) +
3826 DIV_ROUND_UP(interval * us, us_per_frame);
3827
3828 return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3829}
3830
Paul Zimmerman7359d482013-03-11 17:47:59 -07003831int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3832{
Aldo Iljazi6bf2e2a2013-11-30 19:33:57 +02003833 return hsotg->op_state == OTG_STATE_B_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003834}
3835
3836static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3837 int iso_desc_count,
3838 gfp_t mem_flags)
3839{
3840 struct dwc2_hcd_urb *urb;
3841 u32 size = sizeof(*urb) + iso_desc_count *
3842 sizeof(struct dwc2_hcd_iso_packet_desc);
3843
3844 urb = kzalloc(size, mem_flags);
3845 if (urb)
3846 urb->packet_count = iso_desc_count;
3847 return urb;
3848}
3849
3850static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3851 struct dwc2_hcd_urb *urb, u8 dev_addr,
3852 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
3853{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02003854 if (dbg_perio() ||
3855 ep_type == USB_ENDPOINT_XFER_BULK ||
3856 ep_type == USB_ENDPOINT_XFER_CONTROL)
3857 dev_vdbg(hsotg->dev,
3858 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
3859 dev_addr, ep_num, ep_dir, ep_type, mps);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003860 urb->pipe_info.dev_addr = dev_addr;
3861 urb->pipe_info.ep_num = ep_num;
3862 urb->pipe_info.pipe_type = ep_type;
3863 urb->pipe_info.pipe_dir = ep_dir;
3864 urb->pipe_info.mps = mps;
3865}
3866
3867/*
3868 * NOTE: This function will be removed once the peripheral controller code
3869 * is integrated and the driver is stable
3870 */
3871void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3872{
3873#ifdef DEBUG
3874 struct dwc2_host_chan *chan;
3875 struct dwc2_hcd_urb *urb;
3876 struct dwc2_qtd *qtd;
3877 int num_channels;
3878 u32 np_tx_status;
3879 u32 p_tx_status;
3880 int i;
3881
John Younbea8e862016-11-03 17:55:53 -07003882 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003883 dev_dbg(hsotg->dev, "\n");
3884 dev_dbg(hsotg->dev,
3885 "************************************************************\n");
3886 dev_dbg(hsotg->dev, "HCD State:\n");
3887 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
3888
3889 for (i = 0; i < num_channels; i++) {
3890 chan = hsotg->hc_ptr_array[i];
3891 dev_dbg(hsotg->dev, " Channel %d:\n", i);
3892 dev_dbg(hsotg->dev,
3893 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3894 chan->dev_addr, chan->ep_num, chan->ep_is_in);
3895 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
3896 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
3897 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
3898 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
3899 chan->data_pid_start);
3900 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
3901 dev_dbg(hsotg->dev, " xfer_started: %d\n",
3902 chan->xfer_started);
3903 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
3904 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
3905 (unsigned long)chan->xfer_dma);
3906 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
3907 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
3908 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
3909 chan->halt_on_queue);
3910 dev_dbg(hsotg->dev, " halt_pending: %d\n",
3911 chan->halt_pending);
3912 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
3913 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
3914 dev_dbg(hsotg->dev, " complete_split: %d\n",
3915 chan->complete_split);
3916 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
3917 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
3918 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
3919 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
3920 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
3921
3922 if (chan->xfer_started) {
3923 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3924
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003925 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3926 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
3927 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
3928 hcint = dwc2_readl(hsotg->regs + HCINT(i));
3929 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003930 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
3931 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
3932 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
3933 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
3934 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
3935 }
3936
3937 if (!(chan->xfer_started && chan->qh))
3938 continue;
3939
3940 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3941 if (!qtd->in_process)
3942 break;
3943 urb = qtd->urb;
3944 dev_dbg(hsotg->dev, " URB Info:\n");
3945 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
3946 qtd, urb);
3947 if (urb) {
3948 dev_dbg(hsotg->dev,
3949 " Dev: %d, EP: %d %s\n",
3950 dwc2_hcd_get_dev_addr(&urb->pipe_info),
3951 dwc2_hcd_get_ep_num(&urb->pipe_info),
3952 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3953 "IN" : "OUT");
3954 dev_dbg(hsotg->dev,
3955 " Max packet size: %d\n",
3956 dwc2_hcd_get_mps(&urb->pipe_info));
3957 dev_dbg(hsotg->dev,
3958 " transfer_buffer: %p\n",
3959 urb->buf);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07003960 dev_dbg(hsotg->dev,
3961 " transfer_dma: %08lx\n",
3962 (unsigned long)urb->dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003963 dev_dbg(hsotg->dev,
3964 " transfer_buffer_length: %d\n",
3965 urb->length);
3966 dev_dbg(hsotg->dev, " actual_length: %d\n",
3967 urb->actual_length);
3968 }
3969 }
3970 }
3971
3972 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
3973 hsotg->non_periodic_channels);
3974 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
3975 hsotg->periodic_channels);
3976 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003977 np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003978 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003979 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003980 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003981 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003982 p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003983 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003984 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003985 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003986 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003987 dwc2_dump_global_registers(hsotg);
3988 dwc2_dump_host_registers(hsotg);
3989 dev_dbg(hsotg->dev,
3990 "************************************************************\n");
3991 dev_dbg(hsotg->dev, "\n");
3992#endif
3993}
3994
Paul Zimmerman7359d482013-03-11 17:47:59 -07003995struct wrapper_priv_data {
3996 struct dwc2_hsotg *hsotg;
3997};
3998
3999/* Gets the dwc2_hsotg from a usb_hcd */
4000static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4001{
4002 struct wrapper_priv_data *p;
4003
John Youn9da51972017-01-17 20:30:27 -08004004 p = (struct wrapper_priv_data *)&hcd->hcd_priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004005 return p->hsotg;
4006}
4007
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004008/**
4009 * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
4010 *
4011 * This will get the dwc2_tt structure (and ttport) associated with the given
4012 * context (which is really just a struct urb pointer).
4013 *
4014 * The first time this is called for a given TT we allocate memory for our
4015 * structure. When everyone is done and has called dwc2_host_put_tt_info()
4016 * then the refcount for the structure will go to 0 and we'll free it.
4017 *
4018 * @hsotg: The HCD state structure for the DWC OTG controller.
4019 * @qh: The QH structure.
4020 * @context: The priv pointer from a struct dwc2_hcd_urb.
4021 * @mem_flags: Flags for allocating memory.
4022 * @ttport: We'll return this device's port number here. That's used to
4023 * reference into the bitmap if we're on a multi_tt hub.
4024 *
4025 * Return: a pointer to a struct dwc2_tt. Don't forget to call
4026 * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
4027 */
4028
4029struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
4030 gfp_t mem_flags, int *ttport)
4031{
4032 struct urb *urb = context;
4033 struct dwc2_tt *dwc_tt = NULL;
4034
4035 if (urb->dev->tt) {
4036 *ttport = urb->dev->ttport;
4037
4038 dwc_tt = urb->dev->tt->hcpriv;
John Youn9da51972017-01-17 20:30:27 -08004039 if (!dwc_tt) {
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004040 size_t bitmap_size;
4041
4042 /*
4043 * For single_tt we need one schedule. For multi_tt
4044 * we need one per port.
4045 */
4046 bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
4047 sizeof(dwc_tt->periodic_bitmaps[0]);
4048 if (urb->dev->tt->multi)
4049 bitmap_size *= urb->dev->tt->hub->maxchild;
4050
4051 dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
4052 mem_flags);
John Youn9da51972017-01-17 20:30:27 -08004053 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004054 return NULL;
4055
4056 dwc_tt->usb_tt = urb->dev->tt;
4057 dwc_tt->usb_tt->hcpriv = dwc_tt;
4058 }
4059
4060 dwc_tt->refcount++;
4061 }
4062
4063 return dwc_tt;
4064}
4065
4066/**
4067 * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
4068 *
4069 * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
4070 * of the structure are done.
4071 *
4072 * It's OK to call this with NULL.
4073 *
4074 * @hsotg: The HCD state structure for the DWC OTG controller.
4075 * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
4076 */
4077void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
4078{
4079 /* Model kfree and make put of NULL a no-op */
John Youn9da51972017-01-17 20:30:27 -08004080 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004081 return;
4082
4083 WARN_ON(dwc_tt->refcount < 1);
4084
4085 dwc_tt->refcount--;
4086 if (!dwc_tt->refcount) {
4087 dwc_tt->usb_tt->hcpriv = NULL;
4088 kfree(dwc_tt);
4089 }
4090}
4091
Paul Zimmerman7359d482013-03-11 17:47:59 -07004092int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4093{
4094 struct urb *urb = context;
4095
4096 return urb->dev->speed;
4097}
4098
4099static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4100 struct urb *urb)
4101{
4102 struct usb_bus *bus = hcd_to_bus(hcd);
4103
4104 if (urb->interval)
4105 bus->bandwidth_allocated += bw / urb->interval;
4106 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4107 bus->bandwidth_isoc_reqs++;
4108 else
4109 bus->bandwidth_int_reqs++;
4110}
4111
4112static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4113 struct urb *urb)
4114{
4115 struct usb_bus *bus = hcd_to_bus(hcd);
4116
4117 if (urb->interval)
4118 bus->bandwidth_allocated -= bw / urb->interval;
4119 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4120 bus->bandwidth_isoc_reqs--;
4121 else
4122 bus->bandwidth_int_reqs--;
4123}
4124
4125/*
4126 * Sets the final status of an URB and returns it to the upper layer. Any
4127 * required cleanup of the URB is performed.
4128 *
4129 * Must be called with interrupt disabled and spinlock held
4130 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004131void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4132 int status)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004133{
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004134 struct urb *urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004135 int i;
4136
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004137 if (!qtd) {
4138 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4139 return;
4140 }
4141
4142 if (!qtd->urb) {
4143 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4144 return;
4145 }
4146
4147 urb = qtd->urb->priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004148 if (!urb) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004149 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004150 return;
4151 }
4152
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004153 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004154
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004155 if (dbg_urb(urb))
4156 dev_vdbg(hsotg->dev,
4157 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4158 __func__, urb, usb_pipedevice(urb->pipe),
4159 usb_pipeendpoint(urb->pipe),
4160 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4161 urb->actual_length);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004162
Paul Zimmerman7359d482013-03-11 17:47:59 -07004163 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004164 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004165 for (i = 0; i < urb->number_of_packets; ++i) {
4166 urb->iso_frame_desc[i].actual_length =
4167 dwc2_hcd_urb_get_iso_desc_actual_length(
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004168 qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004169 urb->iso_frame_desc[i].status =
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004170 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004171 }
4172 }
4173
Gregory Herrerofe9b1772015-09-22 15:16:51 +02004174 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4175 for (i = 0; i < urb->number_of_packets; i++)
4176 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4177 i, urb->iso_frame_desc[i].status);
4178 }
4179
Paul Zimmerman7359d482013-03-11 17:47:59 -07004180 urb->status = status;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004181 if (!status) {
4182 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4183 urb->actual_length < urb->transfer_buffer_length)
4184 urb->status = -EREMOTEIO;
4185 }
4186
4187 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4188 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4189 struct usb_host_endpoint *ep = urb->ep;
4190
4191 if (ep)
4192 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4193 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4194 urb);
4195 }
4196
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004197 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004198 urb->hcpriv = NULL;
4199 kfree(qtd->urb);
4200 qtd->urb = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004201
Paul Zimmerman7359d482013-03-11 17:47:59 -07004202 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004203}
4204
4205/*
4206 * Work queue function for starting the HCD when A-Cable is connected
4207 */
4208static void dwc2_hcd_start_func(struct work_struct *work)
4209{
4210 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4211 start_work.work);
4212
4213 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4214 dwc2_host_start(hsotg);
4215}
4216
4217/*
4218 * Reset work queue function
4219 */
4220static void dwc2_hcd_reset_func(struct work_struct *work)
4221{
4222 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4223 reset_work.work);
Douglas Anderson4a065c72015-11-20 09:06:27 -08004224 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004225 u32 hprt0;
4226
4227 dev_dbg(hsotg->dev, "USB RESET function called\n");
Douglas Anderson4a065c72015-11-20 09:06:27 -08004228
4229 spin_lock_irqsave(&hsotg->lock, flags);
4230
Paul Zimmerman7359d482013-03-11 17:47:59 -07004231 hprt0 = dwc2_read_hprt0(hsotg);
4232 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004233 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004234 hsotg->flags.b.port_reset_change = 1;
Douglas Anderson4a065c72015-11-20 09:06:27 -08004235
4236 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004237}
4238
4239/*
4240 * =========================================================================
4241 * Linux HC Driver Functions
4242 * =========================================================================
4243 */
4244
4245/*
4246 * Initializes the DWC_otg controller and its root hub and prepares it for host
4247 * mode operation. Activates the root port. Returns 0 on success and a negative
4248 * error code on failure.
4249 */
4250static int _dwc2_hcd_start(struct usb_hcd *hcd)
4251{
4252 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4253 struct usb_bus *bus = hcd_to_bus(hcd);
4254 unsigned long flags;
4255
4256 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4257
4258 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero31927b62015-09-22 15:16:41 +02004259 hsotg->lx_state = DWC2_L0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004260 hcd->state = HC_STATE_RUNNING;
Gregory Herrero31927b62015-09-22 15:16:41 +02004261 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004262
4263 if (dwc2_is_device_mode(hsotg)) {
4264 spin_unlock_irqrestore(&hsotg->lock, flags);
4265 return 0; /* why 0 ?? */
4266 }
4267
4268 dwc2_hcd_reinit(hsotg);
4269
4270 /* Initialize and connect root hub if one is not already attached */
4271 if (bus->root_hub) {
4272 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4273 /* Inform the HUB driver to resume */
4274 usb_hcd_resume_root_hub(hcd);
4275 }
4276
4277 spin_unlock_irqrestore(&hsotg->lock, flags);
4278 return 0;
4279}
4280
4281/*
4282 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4283 * stopped.
4284 */
4285static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4286{
4287 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4288 unsigned long flags;
4289
Gregory Herrero5bbf6ce2015-09-22 15:16:48 +02004290 /* Turn off all host-specific interrupts */
4291 dwc2_disable_host_interrupts(hsotg);
4292
Gregory Herrero091473a2015-09-22 15:16:46 +02004293 /* Wait for interrupt processing to finish */
4294 synchronize_irq(hcd->irq);
4295
Paul Zimmerman7359d482013-03-11 17:47:59 -07004296 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero091473a2015-09-22 15:16:46 +02004297 /* Ensure hcd is disconnected */
Douglas Anderson6a659532015-11-19 13:23:14 -08004298 dwc2_hcd_disconnect(hsotg, true);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004299 dwc2_hcd_stop(hsotg);
Gregory Herrero31927b62015-09-22 15:16:41 +02004300 hsotg->lx_state = DWC2_L3;
4301 hcd->state = HC_STATE_HALT;
4302 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004303 spin_unlock_irqrestore(&hsotg->lock, flags);
4304
4305 usleep_range(1000, 3000);
4306}
4307
Gregory Herrero99a65792015-04-29 22:09:13 +02004308static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
4309{
4310 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004311 unsigned long flags;
4312 int ret = 0;
4313 u32 hprt0;
Gregory Herrero99a65792015-04-29 22:09:13 +02004314
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004315 spin_lock_irqsave(&hsotg->lock, flags);
4316
Meng Dongyangf367b722017-08-09 10:34:09 +08004317 if (dwc2_is_device_mode(hsotg))
4318 goto unlock;
4319
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004320 if (hsotg->lx_state != DWC2_L0)
4321 goto unlock;
4322
4323 if (!HCD_HW_ACCESSIBLE(hcd))
4324 goto unlock;
4325
John Stultz866932e2017-01-09 13:10:24 -08004326 if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4327 goto unlock;
4328
John Younbea8e862016-11-03 17:55:53 -07004329 if (!hsotg->params.hibernation)
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004330 goto skip_power_saving;
4331
4332 /*
4333 * Drive USB suspend and disable port Power
4334 * if usb bus is not suspended.
4335 */
4336 if (!hsotg->bus_suspended) {
4337 hprt0 = dwc2_read_hprt0(hsotg);
4338 hprt0 |= HPRT0_SUSP;
4339 hprt0 &= ~HPRT0_PWR;
4340 dwc2_writel(hprt0, hsotg->regs + HPRT0);
4341 }
4342
4343 /* Enter hibernation */
4344 ret = dwc2_enter_hibernation(hsotg);
4345 if (ret) {
4346 if (ret != -ENOTSUPP)
4347 dev_err(hsotg->dev,
4348 "enter hibernation failed\n");
4349 goto skip_power_saving;
4350 }
4351
4352 /* Ask phy to be suspended */
4353 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4354 spin_unlock_irqrestore(&hsotg->lock, flags);
4355 usb_phy_set_suspend(hsotg->uphy, true);
4356 spin_lock_irqsave(&hsotg->lock, flags);
4357 }
4358
4359 /* After entering hibernation, hardware is no more accessible */
4360 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4361
4362skip_power_saving:
Gregory Herrero99a65792015-04-29 22:09:13 +02004363 hsotg->lx_state = DWC2_L2;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004364unlock:
4365 spin_unlock_irqrestore(&hsotg->lock, flags);
4366
4367 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004368}
4369
4370static int _dwc2_hcd_resume(struct usb_hcd *hcd)
4371{
4372 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004373 unsigned long flags;
4374 int ret = 0;
4375
4376 spin_lock_irqsave(&hsotg->lock, flags);
4377
Meng Dongyangf367b722017-08-09 10:34:09 +08004378 if (dwc2_is_device_mode(hsotg))
4379 goto unlock;
4380
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004381 if (hsotg->lx_state != DWC2_L2)
4382 goto unlock;
4383
John Younbea8e862016-11-03 17:55:53 -07004384 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004385 hsotg->lx_state = DWC2_L0;
4386 goto unlock;
4387 }
4388
4389 /*
4390 * Set HW accessible bit before powering on the controller
4391 * since an interrupt may rise.
4392 */
4393 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4394
4395 /*
4396 * Enable power if not already done.
4397 * This must not be spinlocked since duration
4398 * of this call is unknown.
4399 */
4400 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4401 spin_unlock_irqrestore(&hsotg->lock, flags);
4402 usb_phy_set_suspend(hsotg->uphy, false);
4403 spin_lock_irqsave(&hsotg->lock, flags);
4404 }
4405
4406 /* Exit hibernation */
4407 ret = dwc2_exit_hibernation(hsotg, true);
4408 if (ret && (ret != -ENOTSUPP))
4409 dev_err(hsotg->dev, "exit hibernation failed\n");
Gregory Herrero99a65792015-04-29 22:09:13 +02004410
4411 hsotg->lx_state = DWC2_L0;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004412
4413 spin_unlock_irqrestore(&hsotg->lock, flags);
4414
4415 if (hsotg->bus_suspended) {
4416 spin_lock_irqsave(&hsotg->lock, flags);
4417 hsotg->flags.b.port_suspend_change = 1;
4418 spin_unlock_irqrestore(&hsotg->lock, flags);
4419 dwc2_port_resume(hsotg);
4420 } else {
Gregory Herrero5634e012015-09-22 15:16:50 +02004421 /* Wait for controller to correctly update D+/D- level */
4422 usleep_range(3000, 5000);
4423
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004424 /*
4425 * Clear Port Enable and Port Status changes.
4426 * Enable Port Power.
4427 */
4428 dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
4429 HPRT0_ENACHG, hsotg->regs + HPRT0);
4430 /* Wait for controller to detect Port Connect */
Gregory Herrero5634e012015-09-22 15:16:50 +02004431 usleep_range(5000, 7000);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004432 }
4433
4434 return ret;
4435unlock:
4436 spin_unlock_irqrestore(&hsotg->lock, flags);
4437
4438 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004439}
4440
Paul Zimmerman7359d482013-03-11 17:47:59 -07004441/* Returns the current frame number */
4442static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4443{
4444 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4445
4446 return dwc2_hcd_get_frame_number(hsotg);
4447}
4448
4449static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4450 char *fn_name)
4451{
4452#ifdef VERBOSE_DEBUG
4453 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Nicholas Mc Guireefe357f2017-01-12 17:33:26 +01004454 char *pipetype = NULL;
4455 char *speed = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004456
4457 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4458 dev_vdbg(hsotg->dev, " Device address: %d\n",
4459 usb_pipedevice(urb->pipe));
4460 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
4461 usb_pipeendpoint(urb->pipe),
4462 usb_pipein(urb->pipe) ? "IN" : "OUT");
4463
4464 switch (usb_pipetype(urb->pipe)) {
4465 case PIPE_CONTROL:
4466 pipetype = "CONTROL";
4467 break;
4468 case PIPE_BULK:
4469 pipetype = "BULK";
4470 break;
4471 case PIPE_INTERRUPT:
4472 pipetype = "INTERRUPT";
4473 break;
4474 case PIPE_ISOCHRONOUS:
4475 pipetype = "ISOCHRONOUS";
4476 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004477 }
4478
4479 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
4480 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4481 "IN" : "OUT");
4482
4483 switch (urb->dev->speed) {
4484 case USB_SPEED_HIGH:
4485 speed = "HIGH";
4486 break;
4487 case USB_SPEED_FULL:
4488 speed = "FULL";
4489 break;
4490 case USB_SPEED_LOW:
4491 speed = "LOW";
4492 break;
4493 default:
4494 speed = "UNKNOWN";
4495 break;
4496 }
4497
4498 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
4499 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
4500 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
4501 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
4502 urb->transfer_buffer_length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07004503 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
4504 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4505 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
4506 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004507 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
4508
4509 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4510 int i;
4511
4512 for (i = 0; i < urb->number_of_packets; i++) {
4513 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
4514 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
4515 urb->iso_frame_desc[i].offset,
4516 urb->iso_frame_desc[i].length);
4517 }
4518 }
4519#endif
4520}
4521
4522/*
4523 * Starts processing a USB transfer request specified by a USB Request Block
4524 * (URB). mem_flags indicates the type of memory allocation to use while
4525 * processing this URB.
4526 */
4527static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4528 gfp_t mem_flags)
4529{
4530 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4531 struct usb_host_endpoint *ep = urb->ep;
4532 struct dwc2_hcd_urb *dwc2_urb;
4533 int i;
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004534 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004535 int alloc_bandwidth = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004536 u8 ep_type = 0;
4537 u32 tflags = 0;
4538 void *buf;
4539 unsigned long flags;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004540 struct dwc2_qh *qh;
4541 bool qh_allocated = false;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004542 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004543
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004544 if (dbg_urb(urb)) {
4545 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4546 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4547 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004548
John Youn9da51972017-01-17 20:30:27 -08004549 if (!ep)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004550 return -EINVAL;
4551
4552 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4553 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4554 spin_lock_irqsave(&hsotg->lock, flags);
4555 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4556 alloc_bandwidth = 1;
4557 spin_unlock_irqrestore(&hsotg->lock, flags);
4558 }
4559
4560 switch (usb_pipetype(urb->pipe)) {
4561 case PIPE_CONTROL:
4562 ep_type = USB_ENDPOINT_XFER_CONTROL;
4563 break;
4564 case PIPE_ISOCHRONOUS:
4565 ep_type = USB_ENDPOINT_XFER_ISOC;
4566 break;
4567 case PIPE_BULK:
4568 ep_type = USB_ENDPOINT_XFER_BULK;
4569 break;
4570 case PIPE_INTERRUPT:
4571 ep_type = USB_ENDPOINT_XFER_INT;
4572 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004573 }
4574
4575 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4576 mem_flags);
4577 if (!dwc2_urb)
4578 return -ENOMEM;
4579
4580 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4581 usb_pipeendpoint(urb->pipe), ep_type,
4582 usb_pipein(urb->pipe),
4583 usb_maxpacket(urb->dev, urb->pipe,
4584 !(usb_pipein(urb->pipe))));
4585
4586 buf = urb->transfer_buffer;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004587
Paul Zimmerman7359d482013-03-11 17:47:59 -07004588 if (hcd->self.uses_dma) {
Paul Zimmerman25a49442013-07-13 14:53:53 -07004589 if (!buf && (urb->transfer_dma & 3)) {
4590 dev_err(hsotg->dev,
4591 "%s: unaligned transfer with no transfer_buffer",
4592 __func__);
4593 retval = -EINVAL;
Gregory Herrero33ad2612015-04-29 22:09:15 +02004594 goto fail0;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004595 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004596 }
4597
4598 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4599 tflags |= URB_GIVEBACK_ASAP;
4600 if (urb->transfer_flags & URB_ZERO_PACKET)
4601 tflags |= URB_SEND_ZERO_PACKET;
4602
4603 dwc2_urb->priv = urb;
4604 dwc2_urb->buf = buf;
4605 dwc2_urb->dma = urb->transfer_dma;
4606 dwc2_urb->length = urb->transfer_buffer_length;
4607 dwc2_urb->setup_packet = urb->setup_packet;
4608 dwc2_urb->setup_dma = urb->setup_dma;
4609 dwc2_urb->flags = tflags;
4610 dwc2_urb->interval = urb->interval;
4611 dwc2_urb->status = -EINPROGRESS;
4612
4613 for (i = 0; i < urb->number_of_packets; ++i)
4614 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4615 urb->iso_frame_desc[i].offset,
4616 urb->iso_frame_desc[i].length);
4617
4618 urb->hcpriv = dwc2_urb;
John Youn9da51972017-01-17 20:30:27 -08004619 qh = (struct dwc2_qh *)ep->hcpriv;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004620 /* Create QH for the endpoint if it doesn't exist */
4621 if (!qh) {
4622 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4623 if (!qh) {
4624 retval = -ENOMEM;
4625 goto fail0;
4626 }
4627 ep->hcpriv = qh;
4628 qh_allocated = true;
4629 }
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004630
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004631 qtd = kzalloc(sizeof(*qtd), mem_flags);
4632 if (!qtd) {
4633 retval = -ENOMEM;
4634 goto fail1;
4635 }
4636
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004637 spin_lock_irqsave(&hsotg->lock, flags);
4638 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004639 if (retval)
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004640 goto fail2;
4641
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004642 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4643 if (retval)
4644 goto fail3;
4645
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004646 if (alloc_bandwidth) {
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004647 dwc2_allocate_bus_bandwidth(hcd,
4648 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4649 urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004650 }
4651
Gregory Herrero33ad2612015-04-29 22:09:15 +02004652 spin_unlock_irqrestore(&hsotg->lock, flags);
4653
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004654 return 0;
4655
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004656fail3:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004657 dwc2_urb->priv = NULL;
4658 usb_hcd_unlink_urb_from_ep(hcd, urb);
Douglas Anderson16e80212016-01-28 18:19:55 -08004659 if (qh_allocated && qh->channel && qh->channel->qh == qh)
4660 qh->channel->qh = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004661fail2:
Gregory Herrero33ad2612015-04-29 22:09:15 +02004662 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004663 urb->hcpriv = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004664 kfree(qtd);
Vardan Mikayelyanb0d659022016-04-27 20:20:51 -07004665 qtd = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004666fail1:
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004667 if (qh_allocated) {
4668 struct dwc2_qtd *qtd2, *qtd2_tmp;
4669
4670 ep->hcpriv = NULL;
4671 dwc2_hcd_qh_unlink(hsotg, qh);
4672 /* Free each QTD in the QH's QTD list */
4673 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
John Youn9da51972017-01-17 20:30:27 -08004674 qtd_list_entry)
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004675 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4676 dwc2_hcd_qh_free(hsotg, qh);
4677 }
Gregory Herrero33ad2612015-04-29 22:09:15 +02004678fail0:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004679 kfree(dwc2_urb);
4680
Paul Zimmerman7359d482013-03-11 17:47:59 -07004681 return retval;
4682}
4683
4684/*
4685 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4686 */
4687static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4688 int status)
4689{
4690 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004691 int rc;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004692 unsigned long flags;
4693
4694 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4695 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4696
4697 spin_lock_irqsave(&hsotg->lock, flags);
4698
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004699 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4700 if (rc)
4701 goto out;
4702
Paul Zimmerman7359d482013-03-11 17:47:59 -07004703 if (!urb->hcpriv) {
4704 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4705 goto out;
4706 }
4707
4708 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4709
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004710 usb_hcd_unlink_urb_from_ep(hcd, urb);
4711
Paul Zimmerman7359d482013-03-11 17:47:59 -07004712 kfree(urb->hcpriv);
4713 urb->hcpriv = NULL;
4714
4715 /* Higher layer software sets URB status */
4716 spin_unlock(&hsotg->lock);
4717 usb_hcd_giveback_urb(hcd, urb, status);
4718 spin_lock(&hsotg->lock);
4719
4720 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4721 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
4722out:
4723 spin_unlock_irqrestore(&hsotg->lock, flags);
4724
4725 return rc;
4726}
4727
4728/*
4729 * Frees resources in the DWC_otg controller related to a given endpoint. Also
4730 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4731 * must already be dequeued.
4732 */
4733static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4734 struct usb_host_endpoint *ep)
4735{
4736 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4737
4738 dev_dbg(hsotg->dev,
4739 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4740 ep->desc.bEndpointAddress, ep->hcpriv);
4741 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4742}
4743
4744/*
4745 * Resets endpoint specific parameter values, in current version used to reset
4746 * the data toggle (as a WA). This function can be called from usb_clear_halt
4747 * routine.
4748 */
4749static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4750 struct usb_host_endpoint *ep)
4751{
4752 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004753 unsigned long flags;
4754
4755 dev_dbg(hsotg->dev,
4756 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4757 ep->desc.bEndpointAddress);
4758
Paul Zimmerman7359d482013-03-11 17:47:59 -07004759 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004760 dwc2_hcd_endpoint_reset(hsotg, ep);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004761 spin_unlock_irqrestore(&hsotg->lock, flags);
4762}
4763
4764/*
4765 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4766 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4767 * interrupt.
4768 *
4769 * This function is called by the USB core when an interrupt occurs
4770 */
4771static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4772{
4773 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004774
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02004775 return dwc2_handle_hcd_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004776}
4777
4778/*
4779 * Creates Status Change bitmap for the root hub and root port. The bitmap is
4780 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4781 * is the status change indicator for the single root port. Returns 1 if either
4782 * change indicator is 1, otherwise returns 0.
4783 */
4784static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4785{
4786 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4787
4788 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4789 return buf[0] != 0;
4790}
4791
4792/* Handles hub class-specific requests */
4793static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4794 u16 windex, char *buf, u16 wlength)
4795{
4796 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4797 wvalue, windex, buf, wlength);
4798 return retval;
4799}
4800
4801/* Handles hub TT buffer clear completions */
4802static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4803 struct usb_host_endpoint *ep)
4804{
4805 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4806 struct dwc2_qh *qh;
4807 unsigned long flags;
4808
4809 qh = ep->hcpriv;
4810 if (!qh)
4811 return;
4812
4813 spin_lock_irqsave(&hsotg->lock, flags);
4814 qh->tt_buffer_dirty = 0;
4815
4816 if (hsotg->flags.b.port_connect_status)
4817 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4818
4819 spin_unlock_irqrestore(&hsotg->lock, flags);
4820}
4821
Chen Yuca8b0332017-01-23 15:00:18 -08004822/*
4823 * HPRT0_SPD_HIGH_SPEED: high speed
4824 * HPRT0_SPD_FULL_SPEED: full speed
4825 */
4826static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4827{
4828 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4829
4830 if (hsotg->params.speed == speed)
4831 return;
4832
4833 hsotg->params.speed = speed;
4834 queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4835}
4836
4837static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4838{
4839 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4840
4841 if (!hsotg->params.change_speed_quirk)
4842 return;
4843
4844 /*
4845 * On removal, set speed to default high-speed.
4846 */
4847 if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4848 udev->parent->speed < USB_SPEED_HIGH) {
4849 dev_info(hsotg->dev, "Set speed to default high-speed\n");
4850 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4851 }
4852}
4853
4854static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4855{
4856 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4857
4858 if (!hsotg->params.change_speed_quirk)
4859 return 0;
4860
4861 if (udev->speed == USB_SPEED_HIGH) {
4862 dev_info(hsotg->dev, "Set speed to high-speed\n");
4863 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4864 } else if ((udev->speed == USB_SPEED_FULL ||
4865 udev->speed == USB_SPEED_LOW)) {
4866 /*
4867 * Change speed setting to full-speed if there's
4868 * a full-speed or low-speed device plugged in.
4869 */
4870 dev_info(hsotg->dev, "Set speed to full-speed\n");
4871 dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4872 }
4873
4874 return 0;
4875}
4876
Paul Zimmerman7359d482013-03-11 17:47:59 -07004877static struct hc_driver dwc2_hc_driver = {
4878 .description = "dwc2_hsotg",
4879 .product_desc = "DWC OTG Controller",
4880 .hcd_priv_size = sizeof(struct wrapper_priv_data),
4881
4882 .irq = _dwc2_hcd_irq,
Douglas Anderson8add17c2016-01-28 18:20:00 -08004883 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004884
4885 .start = _dwc2_hcd_start,
4886 .stop = _dwc2_hcd_stop,
4887 .urb_enqueue = _dwc2_hcd_urb_enqueue,
4888 .urb_dequeue = _dwc2_hcd_urb_dequeue,
4889 .endpoint_disable = _dwc2_hcd_endpoint_disable,
4890 .endpoint_reset = _dwc2_hcd_endpoint_reset,
4891 .get_frame_number = _dwc2_hcd_get_frame_number,
4892
4893 .hub_status_data = _dwc2_hcd_hub_status_data,
4894 .hub_control = _dwc2_hcd_hub_control,
4895 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
Gregory Herrero99a65792015-04-29 22:09:13 +02004896
4897 .bus_suspend = _dwc2_hcd_suspend,
4898 .bus_resume = _dwc2_hcd_resume,
Douglas Anderson3bc04e22016-01-28 18:19:53 -08004899
4900 .map_urb_for_dma = dwc2_map_urb_for_dma,
4901 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004902};
4903
4904/*
4905 * Frees secondary storage associated with the dwc2_hsotg structure contained
4906 * in the struct usb_hcd field
4907 */
4908static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
4909{
4910 u32 ahbcfg;
4911 u32 dctl;
4912 int i;
4913
4914 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
4915
4916 /* Free memory for QH/QTD lists */
4917 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08004918 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004919 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
4920 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
4921 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
4922 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
4923 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
4924
4925 /* Free memory for the host channels */
4926 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
4927 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
4928
John Youn9da51972017-01-17 20:30:27 -08004929 if (chan) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07004930 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
4931 i, chan);
4932 hsotg->hc_ptr_array[i] = NULL;
4933 kfree(chan);
4934 }
4935 }
4936
John Youn95832c02017-01-23 14:57:26 -08004937 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07004938 if (hsotg->status_buf) {
4939 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
4940 hsotg->status_buf,
4941 hsotg->status_buf_dma);
4942 hsotg->status_buf = NULL;
4943 }
4944 } else {
4945 kfree(hsotg->status_buf);
4946 hsotg->status_buf = NULL;
4947 }
4948
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004949 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004950
4951 /* Disable all interrupts */
4952 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004953 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
4954 dwc2_writel(0, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004955
Matthijs Kooijman9badec22013-08-30 18:45:21 +02004956 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004957 dctl = dwc2_readl(hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004958 dctl |= DCTL_SFTDISCON;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004959 dwc2_writel(dctl, hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004960 }
4961
4962 if (hsotg->wq_otg) {
4963 if (!cancel_work_sync(&hsotg->wf_otg))
4964 flush_workqueue(hsotg->wq_otg);
4965 destroy_workqueue(hsotg->wq_otg);
4966 }
4967
Paul Zimmerman7359d482013-03-11 17:47:59 -07004968 del_timer(&hsotg->wkp_timer);
4969}
4970
4971static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
4972{
4973 /* Turn off all host-specific interrupts */
4974 dwc2_disable_host_interrupts(hsotg);
4975
4976 dwc2_hcd_free(hsotg);
4977}
4978
Matthijs Kooijman8284f932013-04-11 18:43:47 +02004979/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07004980 * Initializes the HCD. This function allocates memory for and initializes the
4981 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
4982 * USB bus with the core and calls the hc_driver->start() function. It returns
4983 * a negative error on failure.
4984 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01004985int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004986{
Heiner Kallweit348becd2017-01-25 23:10:51 +01004987 struct platform_device *pdev = to_platform_device(hsotg->dev);
4988 struct resource *res;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004989 struct usb_hcd *hcd;
4990 struct dwc2_host_chan *channel;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02004991 u32 hcfg;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004992 int i, num_channels;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02004993 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004994
Dinh Nguyenf5500ec2014-11-11 11:13:39 -06004995 if (usb_disabled())
4996 return -ENODEV;
4997
Paul Zimmermane62662c2013-03-25 17:03:35 -07004998 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07004999
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005000 retval = -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005001
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005002 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005003 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005004
5005#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5006 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
5007 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5008 if (!hsotg->frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005009 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005010 hsotg->last_frame_num_array = kzalloc(
5011 sizeof(*hsotg->last_frame_num_array) *
5012 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5013 if (!hsotg->last_frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005014 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005015#endif
Douglas Anderson483bb252016-01-28 18:20:07 -08005016 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005017
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005018 /* Check if the bus driver or platform code has setup a dma_mask */
John Youn95832c02017-01-23 14:57:26 -08005019 if (hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08005020 !hsotg->dev->dma_mask) {
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005021 dev_warn(hsotg->dev,
5022 "dma_mask not set, disabling DMA\n");
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01005023 hsotg->params.host_dma = false;
John Youn95832c02017-01-23 14:57:26 -08005024 hsotg->params.dma_desc_enable = false;
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005025 }
5026
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005027 /* Set device flags indicating whether the HCD supports DMA */
John Youn95832c02017-01-23 14:57:26 -08005028 if (hsotg->params.host_dma) {
Paul Zimmerman30885312013-05-24 16:27:56 -07005029 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5030 dev_warn(hsotg->dev, "can't set DMA mask\n");
Paul Zimmerman25a49442013-07-13 14:53:53 -07005031 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5032 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005033 }
5034
Chen Yuca8b0332017-01-23 15:00:18 -08005035 if (hsotg->params.change_speed_quirk) {
5036 dwc2_hc_driver.free_dev = dwc2_free_dev;
5037 dwc2_hc_driver.reset_device = dwc2_reset_device;
5038 }
5039
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005040 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5041 if (!hcd)
5042 goto error1;
5043
John Youn95832c02017-01-23 14:57:26 -08005044 if (!hsotg->params.host_dma)
Matthijs Kooijman7de76ee2013-07-19 11:34:23 +02005045 hcd->self.uses_dma = 0;
5046
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005047 hcd->has_tt = 1;
5048
Heiner Kallweit348becd2017-01-25 23:10:51 +01005049 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5050 hcd->rsrc_start = res->start;
5051 hcd->rsrc_len = resource_size(res);
5052
John Youn9da51972017-01-17 20:30:27 -08005053 ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005054 hsotg->priv = hcd;
5055
Paul Zimmerman7359d482013-03-11 17:47:59 -07005056 /*
5057 * Disable the global interrupt until all the interrupt handlers are
5058 * installed
5059 */
5060 dwc2_disable_global_interrupts(hsotg);
5061
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005062 /* Initialize the DWC_otg core, and select the Phy type */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08005063 retval = dwc2_core_init(hsotg, true);
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005064 if (retval)
5065 goto error2;
5066
Paul Zimmerman7359d482013-03-11 17:47:59 -07005067 /* Create new workqueue and init work */
Wei Yongjun53510352013-04-12 22:41:48 +08005068 retval = -ENOMEM;
Bhaktipriya Shridharec7b1262016-07-28 13:57:29 +05305069 hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005070 if (!hsotg->wq_otg) {
5071 dev_err(hsotg->dev, "Failed to create workqueue\n");
5072 goto error2;
5073 }
5074 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5075
Kees Cooke99e88a2017-10-16 14:43:17 -07005076 timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005077
5078 /* Initialize the non-periodic schedule */
5079 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08005080 INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005081 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5082
5083 /* Initialize the periodic schedule */
5084 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5085 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5086 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5087 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5088
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005089 INIT_LIST_HEAD(&hsotg->split_order);
5090
Paul Zimmerman7359d482013-03-11 17:47:59 -07005091 /*
5092 * Create a host channel descriptor for each host channel implemented
5093 * in the controller. Initialize the channel descriptor array.
5094 */
5095 INIT_LIST_HEAD(&hsotg->free_hc_list);
John Younbea8e862016-11-03 17:55:53 -07005096 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005097 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5098
5099 for (i = 0; i < num_channels; i++) {
5100 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
John Youn9da51972017-01-17 20:30:27 -08005101 if (!channel)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005102 goto error3;
5103 channel->hc_num = i;
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005104 INIT_LIST_HEAD(&channel->split_order_list_entry);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005105 hsotg->hc_ptr_array[i] = channel;
5106 }
5107
5108 /* Initialize hsotg start work */
5109 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5110
5111 /* Initialize port reset work */
5112 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5113
5114 /*
5115 * Allocate space for storing data on status transactions. Normally no
5116 * data is sent, but this space acts as a bit bucket. This must be
5117 * done after usb_add_hcd since that function allocates the DMA buffer
5118 * pool.
5119 */
John Youn95832c02017-01-23 14:57:26 -08005120 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005121 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5122 DWC2_HCD_STATUS_BUF_SIZE,
5123 &hsotg->status_buf_dma, GFP_KERNEL);
5124 else
5125 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5126 GFP_KERNEL);
5127
5128 if (!hsotg->status_buf)
5129 goto error3;
5130
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005131 /*
5132 * Create kmem caches to handle descriptor buffers in descriptor
5133 * DMA mode.
5134 * Alignment must be set to 512 bytes.
5135 */
John Younbea8e862016-11-03 17:55:53 -07005136 if (hsotg->params.dma_desc_enable ||
5137 hsotg->params.dma_desc_fs_enable) {
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005138 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005139 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005140 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
5141 NULL);
5142 if (!hsotg->desc_gen_cache) {
5143 dev_err(hsotg->dev,
5144 "unable to create dwc2 generic desc cache\n");
5145
5146 /*
5147 * Disable descriptor dma mode since it will not be
5148 * usable.
5149 */
John Youn95832c02017-01-23 14:57:26 -08005150 hsotg->params.dma_desc_enable = false;
5151 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005152 }
5153
5154 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005155 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005156 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
5157 if (!hsotg->desc_hsisoc_cache) {
5158 dev_err(hsotg->dev,
5159 "unable to create dwc2 hs isoc desc cache\n");
5160
5161 kmem_cache_destroy(hsotg->desc_gen_cache);
5162
5163 /*
5164 * Disable descriptor dma mode since it will not be
5165 * usable.
5166 */
John Youn95832c02017-01-23 14:57:26 -08005167 hsotg->params.dma_desc_enable = false;
5168 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005169 }
5170 }
5171
Paul Zimmerman7359d482013-03-11 17:47:59 -07005172 hsotg->otg_port = 1;
5173 hsotg->frame_list = NULL;
5174 hsotg->frame_list_dma = 0;
5175 hsotg->periodic_qh_count = 0;
5176
5177 /* Initiate lx_state to L3 disconnected state */
5178 hsotg->lx_state = DWC2_L3;
5179
5180 hcd->self.otg_port = hsotg->otg_port;
5181
5182 /* Don't support SG list at this point */
5183 hcd->self.sg_tablesize = 0;
5184
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005185 if (!IS_ERR_OR_NULL(hsotg->uphy))
5186 otg_set_host(hsotg->uphy->otg, &hcd->self);
5187
Paul Zimmerman7359d482013-03-11 17:47:59 -07005188 /*
5189 * Finish generic HCD initialization and start the HCD. This function
5190 * allocates the DMA buffer pool, registers the USB bus, requests the
5191 * IRQ line, and calls hcd_start method.
5192 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005193 retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005194 if (retval < 0)
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005195 goto error4;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005196
Peter Chen3c9740a2013-11-05 10:46:02 +08005197 device_wakeup_enable(hcd->self.controller);
5198
Paul Zimmerman7359d482013-03-11 17:47:59 -07005199 dwc2_hcd_dump_state(hsotg);
5200
5201 dwc2_enable_global_interrupts(hsotg);
5202
5203 return 0;
5204
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005205error4:
5206 kmem_cache_destroy(hsotg->desc_gen_cache);
5207 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005208error3:
5209 dwc2_hcd_release(hsotg);
5210error2:
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005211 usb_put_hcd(hcd);
5212error1:
Paul Zimmerman7359d482013-03-11 17:47:59 -07005213
5214#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5215 kfree(hsotg->last_frame_num_array);
5216 kfree(hsotg->frame_num_array);
5217#endif
5218
Paul Zimmermane62662c2013-03-25 17:03:35 -07005219 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005220 return retval;
5221}
Paul Zimmerman7359d482013-03-11 17:47:59 -07005222
5223/*
5224 * Removes the HCD.
5225 * Frees memory and resources associated with the HCD and deregisters the bus.
5226 */
Paul Zimmermane62662c2013-03-25 17:03:35 -07005227void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005228{
5229 struct usb_hcd *hcd;
5230
Paul Zimmermane62662c2013-03-25 17:03:35 -07005231 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005232
5233 hcd = dwc2_hsotg_to_hcd(hsotg);
Paul Zimmermane62662c2013-03-25 17:03:35 -07005234 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005235
5236 if (!hcd) {
Paul Zimmermane62662c2013-03-25 17:03:35 -07005237 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
Paul Zimmerman7359d482013-03-11 17:47:59 -07005238 __func__);
5239 return;
5240 }
5241
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005242 if (!IS_ERR_OR_NULL(hsotg->uphy))
5243 otg_set_host(hsotg->uphy->otg, NULL);
5244
Paul Zimmerman7359d482013-03-11 17:47:59 -07005245 usb_remove_hcd(hcd);
5246 hsotg->priv = NULL;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005247
5248 kmem_cache_destroy(hsotg->desc_gen_cache);
5249 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5250
Paul Zimmerman7359d482013-03-11 17:47:59 -07005251 dwc2_hcd_release(hsotg);
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005252 usb_put_hcd(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005253
5254#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5255 kfree(hsotg->last_frame_num_array);
5256 kfree(hsotg->frame_num_array);
5257#endif
Paul Zimmerman7359d482013-03-11 17:47:59 -07005258}
John Youn58e52ff6a2016-02-23 19:54:57 -08005259
5260/**
5261 * dwc2_backup_host_registers() - Backup controller host registers.
5262 * When suspending usb bus, registers needs to be backuped
5263 * if controller power is disabled once suspended.
5264 *
5265 * @hsotg: Programming view of the DWC_otg controller
5266 */
5267int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
5268{
5269 struct dwc2_hregs_backup *hr;
5270 int i;
5271
5272 dev_dbg(hsotg->dev, "%s\n", __func__);
5273
5274 /* Backup Host regs */
5275 hr = &hsotg->hr_backup;
5276 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
5277 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
John Younbea8e862016-11-03 17:55:53 -07005278 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005279 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
5280
5281 hr->hprt0 = dwc2_read_hprt0(hsotg);
5282 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
5283 hr->valid = true;
5284
5285 return 0;
5286}
5287
5288/**
5289 * dwc2_restore_host_registers() - Restore controller host registers.
5290 * When resuming usb bus, device registers needs to be restored
5291 * if controller power were disabled.
5292 *
5293 * @hsotg: Programming view of the DWC_otg controller
5294 */
5295int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
5296{
5297 struct dwc2_hregs_backup *hr;
5298 int i;
5299
5300 dev_dbg(hsotg->dev, "%s\n", __func__);
5301
5302 /* Restore host regs */
5303 hr = &hsotg->hr_backup;
5304 if (!hr->valid) {
5305 dev_err(hsotg->dev, "%s: no host registers to restore\n",
5306 __func__);
5307 return -EINVAL;
5308 }
5309 hr->valid = false;
5310
5311 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5312 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
5313
John Younbea8e862016-11-03 17:55:53 -07005314 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005315 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
5316
5317 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
5318 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
5319 hsotg->frame_number = 0;
5320
5321 return 0;
5322}