blob: 2cb4f4f3981f7ec598cefbd373a65276becb996d [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__);
Minas Harutyunyana82c7ab2018-01-19 14:43:53 +0400980
981 /*
982 * In buffer DMA or external DMA mode channel can't be halted
983 * for non-split periodic channels. At the end of the next
984 * uframe/frame (in the worst case), the core generates a channel
985 * halted and disables the channel automatically.
986 */
987 if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
988 hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
989 if (!chan->do_split &&
990 (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
991 chan->ep_type == USB_ENDPOINT_XFER_INT)) {
992 dev_err(hsotg->dev, "%s() Channel can't be halted\n",
993 __func__);
994 return;
995 }
996 }
997
John Younb02038fa2016-02-23 19:55:00 -0800998 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
999 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1000
1001 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1002 halt_status == DWC2_HC_XFER_AHB_ERR) {
1003 /*
1004 * Disable all channel interrupts except Ch Halted. The QTD
1005 * and QH state associated with this transfer has been cleared
1006 * (in the case of URB_DEQUEUE), so the channel needs to be
1007 * shut down carefully to prevent crashes.
1008 */
1009 u32 hcintmsk = HCINTMSK_CHHLTD;
1010
1011 dev_vdbg(hsotg->dev, "dequeue/error\n");
1012 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1013
1014 /*
1015 * Make sure no other interrupts besides halt are currently
1016 * pending. Handling another interrupt could cause a crash due
1017 * to the QTD and QH state.
1018 */
1019 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1020
1021 /*
1022 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1023 * even if the channel was already halted for some other
1024 * reason
1025 */
1026 chan->halt_status = halt_status;
1027
1028 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1029 if (!(hcchar & HCCHAR_CHENA)) {
1030 /*
1031 * The channel is either already halted or it hasn't
1032 * started yet. In DMA mode, the transfer may halt if
1033 * it finishes normally or a condition occurs that
1034 * requires driver intervention. Don't want to halt
1035 * the channel again. In either Slave or DMA mode,
1036 * it's possible that the transfer has been assigned
1037 * to a channel, but not started yet when an URB is
1038 * dequeued. Don't want to halt a channel that hasn't
1039 * started yet.
1040 */
1041 return;
1042 }
1043 }
1044 if (chan->halt_pending) {
1045 /*
1046 * A halt has already been issued for this channel. This might
1047 * happen when a transfer is aborted by a higher level in
1048 * the stack.
1049 */
1050 dev_vdbg(hsotg->dev,
1051 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1052 __func__, chan->hc_num);
1053 return;
1054 }
1055
1056 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1057
1058 /* No need to set the bit in DDMA for disabling the channel */
1059 /* TODO check it everywhere channel is disabled */
John Youn95832c02017-01-23 14:57:26 -08001060 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08001061 if (dbg_hc(chan))
1062 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1063 hcchar |= HCCHAR_CHENA;
1064 } else {
1065 if (dbg_hc(chan))
1066 dev_dbg(hsotg->dev, "desc DMA enabled\n");
1067 }
1068 hcchar |= HCCHAR_CHDIS;
1069
John Youn95832c02017-01-23 14:57:26 -08001070 if (!hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001071 if (dbg_hc(chan))
1072 dev_vdbg(hsotg->dev, "DMA not enabled\n");
1073 hcchar |= HCCHAR_CHENA;
1074
1075 /* Check for space in the request queue to issue the halt */
1076 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1077 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1078 dev_vdbg(hsotg->dev, "control/bulk\n");
1079 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1080 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1081 dev_vdbg(hsotg->dev, "Disabling channel\n");
1082 hcchar &= ~HCCHAR_CHENA;
1083 }
1084 } else {
1085 if (dbg_perio())
1086 dev_vdbg(hsotg->dev, "isoc/intr\n");
1087 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1088 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1089 hsotg->queuing_high_bandwidth) {
1090 if (dbg_perio())
1091 dev_vdbg(hsotg->dev, "Disabling channel\n");
1092 hcchar &= ~HCCHAR_CHENA;
1093 }
1094 }
1095 } else {
1096 if (dbg_hc(chan))
1097 dev_vdbg(hsotg->dev, "DMA enabled\n");
1098 }
1099
1100 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1101 chan->halt_status = halt_status;
1102
1103 if (hcchar & HCCHAR_CHENA) {
1104 if (dbg_hc(chan))
1105 dev_vdbg(hsotg->dev, "Channel enabled\n");
1106 chan->halt_pending = 1;
1107 chan->halt_on_queue = 0;
1108 } else {
1109 if (dbg_hc(chan))
1110 dev_vdbg(hsotg->dev, "Channel disabled\n");
1111 chan->halt_on_queue = 1;
1112 }
1113
1114 if (dbg_hc(chan)) {
1115 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1116 chan->hc_num);
1117 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1118 hcchar);
1119 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1120 chan->halt_pending);
1121 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1122 chan->halt_on_queue);
1123 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1124 chan->halt_status);
1125 }
1126}
1127
1128/**
1129 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1130 *
1131 * @hsotg: Programming view of DWC_otg controller
1132 * @chan: Identifies the host channel to clean up
1133 *
1134 * This function is normally called after a transfer is done and the host
1135 * channel is being released
1136 */
1137void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1138{
1139 u32 hcintmsk;
1140
1141 chan->xfer_started = 0;
1142
1143 list_del_init(&chan->split_order_list_entry);
1144
1145 /*
1146 * Clear channel interrupt enables and any unhandled channel interrupt
1147 * conditions
1148 */
1149 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1150 hcintmsk = 0xffffffff;
1151 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1152 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1153}
1154
1155/**
1156 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1157 * which frame a periodic transfer should occur
1158 *
1159 * @hsotg: Programming view of DWC_otg controller
1160 * @chan: Identifies the host channel to set up and its properties
1161 * @hcchar: Current value of the HCCHAR register for the specified host channel
1162 *
1163 * This function has no effect on non-periodic transfers
1164 */
1165static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1166 struct dwc2_host_chan *chan, u32 *hcchar)
1167{
1168 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1169 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1170 int host_speed;
1171 int xfer_ns;
1172 int xfer_us;
1173 int bytes_in_fifo;
1174 u16 fifo_space;
1175 u16 frame_number;
1176 u16 wire_frame;
1177
1178 /*
1179 * Try to figure out if we're an even or odd frame. If we set
1180 * even and the current frame number is even the the transfer
1181 * will happen immediately. Similar if both are odd. If one is
1182 * even and the other is odd then the transfer will happen when
1183 * the frame number ticks.
1184 *
1185 * There's a bit of a balancing act to get this right.
1186 * Sometimes we may want to send data in the current frame (AK
1187 * right away). We might want to do this if the frame number
1188 * _just_ ticked, but we might also want to do this in order
1189 * to continue a split transaction that happened late in a
1190 * microframe (so we didn't know to queue the next transfer
1191 * until the frame number had ticked). The problem is that we
1192 * need a lot of knowledge to know if there's actually still
1193 * time to send things or if it would be better to wait until
1194 * the next frame.
1195 *
1196 * We can look at how much time is left in the current frame
1197 * and make a guess about whether we'll have time to transfer.
1198 * We'll do that.
1199 */
1200
1201 /* Get speed host is running at */
1202 host_speed = (chan->speed != USB_SPEED_HIGH &&
1203 !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1204
1205 /* See how many bytes are in the periodic FIFO right now */
1206 fifo_space = (dwc2_readl(hsotg->regs + HPTXSTS) &
1207 TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1208 bytes_in_fifo = sizeof(u32) *
John Younbea8e862016-11-03 17:55:53 -07001209 (hsotg->params.host_perio_tx_fifo_size -
John Younb02038fa2016-02-23 19:55:00 -08001210 fifo_space);
1211
1212 /*
1213 * Roughly estimate bus time for everything in the periodic
1214 * queue + our new transfer. This is "rough" because we're
1215 * using a function that makes takes into account IN/OUT
1216 * and INT/ISO and we're just slamming in one value for all
1217 * transfers. This should be an over-estimate and that should
1218 * be OK, but we can probably tighten it.
1219 */
1220 xfer_ns = usb_calc_bus_time(host_speed, false, false,
1221 chan->xfer_len + bytes_in_fifo);
1222 xfer_us = NS_TO_US(xfer_ns);
1223
1224 /* See what frame number we'll be at by the time we finish */
1225 frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1226
1227 /* This is when we were scheduled to be on the wire */
1228 wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1229
1230 /*
1231 * If we'd finish _after_ the frame we're scheduled in then
1232 * it's hopeless. Just schedule right away and hope for the
1233 * best. Note that it _might_ be wise to call back into the
1234 * scheduler to pick a better frame, but this is better than
1235 * nothing.
1236 */
1237 if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1238 dwc2_sch_vdbg(hsotg,
1239 "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1240 chan->qh, wire_frame, frame_number,
1241 dwc2_frame_num_dec(frame_number,
1242 wire_frame));
1243 wire_frame = frame_number;
1244
1245 /*
1246 * We picked a different frame number; communicate this
1247 * back to the scheduler so it doesn't try to schedule
1248 * another in the same frame.
1249 *
1250 * Remember that next_active_frame is 1 before the wire
1251 * frame.
1252 */
1253 chan->qh->next_active_frame =
1254 dwc2_frame_num_dec(frame_number, 1);
1255 }
1256
1257 if (wire_frame & 1)
1258 *hcchar |= HCCHAR_ODDFRM;
1259 else
1260 *hcchar &= ~HCCHAR_ODDFRM;
1261 }
1262}
1263
1264static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1265{
1266 /* Set up the initial PID for the transfer */
1267 if (chan->speed == USB_SPEED_HIGH) {
1268 if (chan->ep_is_in) {
1269 if (chan->multi_count == 1)
1270 chan->data_pid_start = DWC2_HC_PID_DATA0;
1271 else if (chan->multi_count == 2)
1272 chan->data_pid_start = DWC2_HC_PID_DATA1;
1273 else
1274 chan->data_pid_start = DWC2_HC_PID_DATA2;
1275 } else {
1276 if (chan->multi_count == 1)
1277 chan->data_pid_start = DWC2_HC_PID_DATA0;
1278 else
1279 chan->data_pid_start = DWC2_HC_PID_MDATA;
1280 }
1281 } else {
1282 chan->data_pid_start = DWC2_HC_PID_DATA0;
1283 }
1284}
1285
1286/**
1287 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1288 * the Host Channel
1289 *
1290 * @hsotg: Programming view of DWC_otg controller
1291 * @chan: Information needed to initialize the host channel
1292 *
1293 * This function should only be called in Slave mode. For a channel associated
1294 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1295 * associated with a periodic EP, the periodic Tx FIFO is written.
1296 *
1297 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1298 * the number of bytes written to the Tx FIFO.
1299 */
1300static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1301 struct dwc2_host_chan *chan)
1302{
1303 u32 i;
1304 u32 remaining_count;
1305 u32 byte_count;
1306 u32 dword_count;
1307 u32 __iomem *data_fifo;
1308 u32 *data_buf = (u32 *)chan->xfer_buf;
1309
1310 if (dbg_hc(chan))
1311 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1312
1313 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1314
1315 remaining_count = chan->xfer_len - chan->xfer_count;
1316 if (remaining_count > chan->max_packet)
1317 byte_count = chan->max_packet;
1318 else
1319 byte_count = remaining_count;
1320
1321 dword_count = (byte_count + 3) / 4;
1322
1323 if (((unsigned long)data_buf & 0x3) == 0) {
1324 /* xfer_buf is DWORD aligned */
1325 for (i = 0; i < dword_count; i++, data_buf++)
1326 dwc2_writel(*data_buf, data_fifo);
1327 } else {
1328 /* xfer_buf is not DWORD aligned */
1329 for (i = 0; i < dword_count; i++, data_buf++) {
1330 u32 data = data_buf[0] | data_buf[1] << 8 |
1331 data_buf[2] << 16 | data_buf[3] << 24;
1332 dwc2_writel(data, data_fifo);
1333 }
1334 }
1335
1336 chan->xfer_count += byte_count;
1337 chan->xfer_buf += byte_count;
1338}
1339
1340/**
1341 * dwc2_hc_do_ping() - Starts a PING transfer
1342 *
1343 * @hsotg: Programming view of DWC_otg controller
1344 * @chan: Information needed to initialize the host channel
1345 *
1346 * This function should only be called in Slave mode. The Do Ping bit is set in
1347 * the HCTSIZ register, then the channel is enabled.
1348 */
1349static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1350 struct dwc2_host_chan *chan)
1351{
1352 u32 hcchar;
1353 u32 hctsiz;
1354
1355 if (dbg_hc(chan))
1356 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1357 chan->hc_num);
1358
1359 hctsiz = TSIZ_DOPNG;
1360 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1361 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1362
1363 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1364 hcchar |= HCCHAR_CHENA;
1365 hcchar &= ~HCCHAR_CHDIS;
1366 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1367}
1368
1369/**
1370 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1371 * channel and starts the transfer
1372 *
1373 * @hsotg: Programming view of DWC_otg controller
1374 * @chan: Information needed to initialize the host channel. The xfer_len value
1375 * may be reduced to accommodate the max widths of the XferSize and
1376 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1377 * changed to reflect the final xfer_len value.
1378 *
1379 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1380 * the caller must ensure that there is sufficient space in the request queue
1381 * and Tx Data FIFO.
1382 *
1383 * For an OUT transfer in Slave mode, it loads a data packet into the
1384 * appropriate FIFO. If necessary, additional data packets are loaded in the
1385 * Host ISR.
1386 *
1387 * For an IN transfer in Slave mode, a data packet is requested. The data
1388 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1389 * additional data packets are requested in the Host ISR.
1390 *
1391 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1392 * register along with a packet count of 1 and the channel is enabled. This
1393 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1394 * simply set to 0 since no data transfer occurs in this case.
1395 *
1396 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1397 * all the information required to perform the subsequent data transfer. In
1398 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1399 * controller performs the entire PING protocol, then starts the data
1400 * transfer.
1401 */
1402static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1403 struct dwc2_host_chan *chan)
1404{
John Younbea8e862016-11-03 17:55:53 -07001405 u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1406 u16 max_hc_pkt_count = hsotg->params.max_packet_count;
John Younb02038fa2016-02-23 19:55:00 -08001407 u32 hcchar;
1408 u32 hctsiz = 0;
1409 u16 num_packets;
1410 u32 ec_mc;
1411
1412 if (dbg_hc(chan))
1413 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1414
1415 if (chan->do_ping) {
John Youn95832c02017-01-23 14:57:26 -08001416 if (!hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001417 if (dbg_hc(chan))
1418 dev_vdbg(hsotg->dev, "ping, no DMA\n");
1419 dwc2_hc_do_ping(hsotg, chan);
1420 chan->xfer_started = 1;
1421 return;
1422 }
1423
1424 if (dbg_hc(chan))
1425 dev_vdbg(hsotg->dev, "ping, DMA\n");
1426
1427 hctsiz |= TSIZ_DOPNG;
1428 }
1429
1430 if (chan->do_split) {
1431 if (dbg_hc(chan))
1432 dev_vdbg(hsotg->dev, "split\n");
1433 num_packets = 1;
1434
1435 if (chan->complete_split && !chan->ep_is_in)
1436 /*
1437 * For CSPLIT OUT Transfer, set the size to 0 so the
1438 * core doesn't expect any data written to the FIFO
1439 */
1440 chan->xfer_len = 0;
1441 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1442 chan->xfer_len = chan->max_packet;
1443 else if (!chan->ep_is_in && chan->xfer_len > 188)
1444 chan->xfer_len = 188;
1445
1446 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1447 TSIZ_XFERSIZE_MASK;
1448
1449 /* For split set ec_mc for immediate retries */
1450 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1451 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1452 ec_mc = 3;
1453 else
1454 ec_mc = 1;
1455 } else {
1456 if (dbg_hc(chan))
1457 dev_vdbg(hsotg->dev, "no split\n");
1458 /*
1459 * Ensure that the transfer length and packet count will fit
1460 * in the widths allocated for them in the HCTSIZn register
1461 */
1462 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1463 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1464 /*
1465 * Make sure the transfer size is no larger than one
1466 * (micro)frame's worth of data. (A check was done
1467 * when the periodic transfer was accepted to ensure
1468 * that a (micro)frame's worth of data can be
1469 * programmed into a channel.)
1470 */
1471 u32 max_periodic_len =
1472 chan->multi_count * chan->max_packet;
1473
1474 if (chan->xfer_len > max_periodic_len)
1475 chan->xfer_len = max_periodic_len;
1476 } else if (chan->xfer_len > max_hc_xfer_size) {
1477 /*
1478 * Make sure that xfer_len is a multiple of max packet
1479 * size
1480 */
1481 chan->xfer_len =
1482 max_hc_xfer_size - chan->max_packet + 1;
1483 }
1484
1485 if (chan->xfer_len > 0) {
1486 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1487 chan->max_packet;
1488 if (num_packets > max_hc_pkt_count) {
1489 num_packets = max_hc_pkt_count;
1490 chan->xfer_len = num_packets * chan->max_packet;
1491 }
1492 } else {
1493 /* Need 1 packet for transfer length of 0 */
1494 num_packets = 1;
1495 }
1496
1497 if (chan->ep_is_in)
1498 /*
1499 * Always program an integral # of max packets for IN
1500 * transfers
1501 */
1502 chan->xfer_len = num_packets * chan->max_packet;
1503
1504 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1505 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1506 /*
1507 * Make sure that the multi_count field matches the
1508 * actual transfer length
1509 */
1510 chan->multi_count = num_packets;
1511
1512 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1513 dwc2_set_pid_isoc(chan);
1514
1515 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1516 TSIZ_XFERSIZE_MASK;
1517
1518 /* The ec_mc gets the multi_count for non-split */
1519 ec_mc = chan->multi_count;
1520 }
1521
1522 chan->start_pkt_count = num_packets;
1523 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1524 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1525 TSIZ_SC_MC_PID_MASK;
1526 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1527 if (dbg_hc(chan)) {
1528 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1529 hctsiz, chan->hc_num);
1530
1531 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1532 chan->hc_num);
1533 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1534 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1535 TSIZ_XFERSIZE_SHIFT);
1536 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1537 (hctsiz & TSIZ_PKTCNT_MASK) >>
1538 TSIZ_PKTCNT_SHIFT);
1539 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1540 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1541 TSIZ_SC_MC_PID_SHIFT);
1542 }
1543
John Youn95832c02017-01-23 14:57:26 -08001544 if (hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001545 dwc2_writel((u32)chan->xfer_dma,
1546 hsotg->regs + HCDMA(chan->hc_num));
1547 if (dbg_hc(chan))
1548 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1549 (unsigned long)chan->xfer_dma, chan->hc_num);
1550 }
1551
1552 /* Start the split */
1553 if (chan->do_split) {
1554 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1555
1556 hcsplt |= HCSPLT_SPLTENA;
1557 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1558 }
1559
1560 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1561 hcchar &= ~HCCHAR_MULTICNT_MASK;
1562 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1563 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1564
1565 if (hcchar & HCCHAR_CHDIS)
1566 dev_warn(hsotg->dev,
1567 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1568 __func__, chan->hc_num, hcchar);
1569
1570 /* Set host channel enable after all other setup is complete */
1571 hcchar |= HCCHAR_CHENA;
1572 hcchar &= ~HCCHAR_CHDIS;
1573
1574 if (dbg_hc(chan))
1575 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1576 (hcchar & HCCHAR_MULTICNT_MASK) >>
1577 HCCHAR_MULTICNT_SHIFT);
1578
1579 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1580 if (dbg_hc(chan))
1581 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1582 chan->hc_num);
1583
1584 chan->xfer_started = 1;
1585 chan->requests++;
1586
John Youn95832c02017-01-23 14:57:26 -08001587 if (!hsotg->params.host_dma &&
John Younb02038fa2016-02-23 19:55:00 -08001588 !chan->ep_is_in && chan->xfer_len > 0)
1589 /* Load OUT packet into the appropriate Tx FIFO */
1590 dwc2_hc_write_packet(hsotg, chan);
1591}
1592
1593/**
1594 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1595 * host channel and starts the transfer in Descriptor DMA mode
1596 *
1597 * @hsotg: Programming view of DWC_otg controller
1598 * @chan: Information needed to initialize the host channel
1599 *
1600 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1601 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1602 * with micro-frame bitmap.
1603 *
1604 * Initializes HCDMA register with descriptor list address and CTD value then
1605 * starts the transfer via enabling the channel.
1606 */
1607void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1608 struct dwc2_host_chan *chan)
1609{
1610 u32 hcchar;
1611 u32 hctsiz = 0;
1612
1613 if (chan->do_ping)
1614 hctsiz |= TSIZ_DOPNG;
1615
1616 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1617 dwc2_set_pid_isoc(chan);
1618
1619 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1620 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1621 TSIZ_SC_MC_PID_MASK;
1622
1623 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1624 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1625
1626 /* Non-zero only for high-speed interrupt endpoints */
1627 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1628
1629 if (dbg_hc(chan)) {
1630 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1631 chan->hc_num);
1632 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1633 chan->data_pid_start);
1634 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1635 }
1636
1637 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1638
1639 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1640 chan->desc_list_sz, DMA_TO_DEVICE);
1641
1642 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
1643
1644 if (dbg_hc(chan))
1645 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1646 &chan->desc_list_addr, chan->hc_num);
1647
1648 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1649 hcchar &= ~HCCHAR_MULTICNT_MASK;
1650 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1651 HCCHAR_MULTICNT_MASK;
1652
1653 if (hcchar & HCCHAR_CHDIS)
1654 dev_warn(hsotg->dev,
1655 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1656 __func__, chan->hc_num, hcchar);
1657
1658 /* Set host channel enable after all other setup is complete */
1659 hcchar |= HCCHAR_CHENA;
1660 hcchar &= ~HCCHAR_CHDIS;
1661
1662 if (dbg_hc(chan))
1663 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1664 (hcchar & HCCHAR_MULTICNT_MASK) >>
1665 HCCHAR_MULTICNT_SHIFT);
1666
1667 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1668 if (dbg_hc(chan))
1669 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1670 chan->hc_num);
1671
1672 chan->xfer_started = 1;
1673 chan->requests++;
1674}
1675
1676/**
1677 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1678 * a previous call to dwc2_hc_start_transfer()
1679 *
1680 * @hsotg: Programming view of DWC_otg controller
1681 * @chan: Information needed to initialize the host channel
1682 *
1683 * The caller must ensure there is sufficient space in the request queue and Tx
1684 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1685 * the controller acts autonomously to complete transfers programmed to a host
1686 * channel.
1687 *
1688 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1689 * if there is any data remaining to be queued. For an IN transfer, another
1690 * data packet is always requested. For the SETUP phase of a control transfer,
1691 * this function does nothing.
1692 *
1693 * Return: 1 if a new request is queued, 0 if no more requests are required
1694 * for this transfer
1695 */
1696static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1697 struct dwc2_host_chan *chan)
1698{
1699 if (dbg_hc(chan))
1700 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1701 chan->hc_num);
1702
1703 if (chan->do_split)
1704 /* SPLITs always queue just once per channel */
1705 return 0;
1706
1707 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1708 /* SETUPs are queued only once since they can't be NAK'd */
1709 return 0;
1710
1711 if (chan->ep_is_in) {
1712 /*
1713 * Always queue another request for other IN transfers. If
1714 * back-to-back INs are issued and NAKs are received for both,
1715 * the driver may still be processing the first NAK when the
1716 * second NAK is received. When the interrupt handler clears
1717 * the NAK interrupt for the first NAK, the second NAK will
1718 * not be seen. So we can't depend on the NAK interrupt
1719 * handler to requeue a NAK'd request. Instead, IN requests
1720 * are issued each time this function is called. When the
1721 * transfer completes, the extra requests for the channel will
1722 * be flushed.
1723 */
1724 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1725
1726 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1727 hcchar |= HCCHAR_CHENA;
1728 hcchar &= ~HCCHAR_CHDIS;
1729 if (dbg_hc(chan))
1730 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
1731 hcchar);
1732 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1733 chan->requests++;
1734 return 1;
1735 }
1736
1737 /* OUT transfers */
1738
1739 if (chan->xfer_count < chan->xfer_len) {
1740 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1741 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1742 u32 hcchar = dwc2_readl(hsotg->regs +
1743 HCCHAR(chan->hc_num));
1744
1745 dwc2_hc_set_even_odd_frame(hsotg, chan,
1746 &hcchar);
1747 }
1748
1749 /* Load OUT packet into the appropriate Tx FIFO */
1750 dwc2_hc_write_packet(hsotg, chan);
1751 chan->requests++;
1752 return 1;
1753 }
1754
1755 return 0;
1756}
1757
1758/*
1759 * =========================================================================
1760 * HCD
1761 * =========================================================================
1762 */
1763
1764/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07001765 * Processes all the URBs in a single list of QHs. Completes them with
1766 * -ETIMEDOUT and frees the QTD.
1767 *
1768 * Must be called with interrupt disabled and spinlock held
1769 */
1770static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1771 struct list_head *qh_list)
1772{
1773 struct dwc2_qh *qh, *qh_tmp;
1774 struct dwc2_qtd *qtd, *qtd_tmp;
1775
1776 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1777 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1778 qtd_list_entry) {
Gregory Herrero2e84da62015-09-22 15:16:53 +02001779 dwc2_host_complete(hsotg, qtd, -ECONNRESET);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001780 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001781 }
1782 }
1783}
1784
1785static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1786 struct list_head *qh_list)
1787{
1788 struct dwc2_qtd *qtd, *qtd_tmp;
1789 struct dwc2_qh *qh, *qh_tmp;
1790 unsigned long flags;
1791
1792 if (!qh_list->next)
1793 /* The list hasn't been initialized yet */
1794 return;
1795
1796 spin_lock_irqsave(&hsotg->lock, flags);
1797
1798 /* Ensure there are no QTDs or URBs left */
1799 dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1800
1801 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1802 dwc2_hcd_qh_unlink(hsotg, qh);
1803
1804 /* Free each QTD in the QH's QTD list */
1805 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1806 qtd_list_entry)
1807 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1808
Douglas Anderson16e80212016-01-28 18:19:55 -08001809 if (qh->channel && qh->channel->qh == qh)
1810 qh->channel->qh = NULL;
1811
Paul Zimmerman7359d482013-03-11 17:47:59 -07001812 spin_unlock_irqrestore(&hsotg->lock, flags);
1813 dwc2_hcd_qh_free(hsotg, qh);
1814 spin_lock_irqsave(&hsotg->lock, flags);
1815 }
1816
1817 spin_unlock_irqrestore(&hsotg->lock, flags);
1818}
1819
1820/*
1821 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1822 * and periodic schedules. The QTD associated with each URB is removed from
1823 * the schedule and freed. This function may be called when a disconnect is
1824 * detected or when the HCD is being stopped.
1825 *
1826 * Must be called with interrupt disabled and spinlock held
1827 */
1828static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1829{
1830 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08001831 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001832 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1833 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1834 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1835 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1836 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1837}
1838
1839/**
1840 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1841 *
1842 * @hsotg: Pointer to struct dwc2_hsotg
1843 */
1844void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1845{
1846 u32 hprt0;
1847
1848 if (hsotg->op_state == OTG_STATE_B_HOST) {
1849 /*
1850 * Reset the port. During a HNP mode switch the reset
1851 * needs to occur within 1ms and have a duration of at
1852 * least 50ms.
1853 */
1854 hprt0 = dwc2_read_hprt0(hsotg);
1855 hprt0 |= HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001856 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001857 }
1858
1859 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1860 msecs_to_jiffies(50));
1861}
1862
1863/* Must be called with interrupt disabled and spinlock held */
1864static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1865{
John Younbea8e862016-11-03 17:55:53 -07001866 int num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001867 struct dwc2_host_chan *channel;
1868 u32 hcchar;
1869 int i;
1870
John Youn95832c02017-01-23 14:57:26 -08001871 if (!hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001872 /* Flush out any channel requests in slave mode */
1873 for (i = 0; i < num_channels; i++) {
1874 channel = hsotg->hc_ptr_array[i];
1875 if (!list_empty(&channel->hc_list_entry))
1876 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001877 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001878 if (hcchar & HCCHAR_CHENA) {
1879 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1880 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001881 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001882 }
1883 }
1884 }
1885
1886 for (i = 0; i < num_channels; i++) {
1887 channel = hsotg->hc_ptr_array[i];
1888 if (!list_empty(&channel->hc_list_entry))
1889 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001890 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001891 if (hcchar & HCCHAR_CHENA) {
1892 /* Halt the channel */
1893 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001894 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001895 }
1896
1897 dwc2_hc_cleanup(hsotg, channel);
1898 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1899 /*
1900 * Added for Descriptor DMA to prevent channel double cleanup in
1901 * release_channel_ddma(), which is called from ep_disable when
1902 * device disconnects
1903 */
1904 channel->qh = NULL;
1905 }
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001906 /* All channels have been freed, mark them available */
John Youn95832c02017-01-23 14:57:26 -08001907 if (hsotg->params.uframe_sched) {
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001908 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07001909 hsotg->params.host_channels;
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001910 } else {
1911 hsotg->non_periodic_channels = 0;
1912 hsotg->periodic_channels = 0;
1913 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001914}
1915
1916/**
Douglas Anderson6a659532015-11-19 13:23:14 -08001917 * dwc2_hcd_connect() - Handles connect of the HCD
Paul Zimmerman7359d482013-03-11 17:47:59 -07001918 *
1919 * @hsotg: Pointer to struct dwc2_hsotg
1920 *
1921 * Must be called with interrupt disabled and spinlock held
1922 */
Douglas Anderson6a659532015-11-19 13:23:14 -08001923void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
1924{
1925 if (hsotg->lx_state != DWC2_L0)
1926 usb_hcd_resume_root_hub(hsotg->priv);
1927
1928 hsotg->flags.b.port_connect_status_change = 1;
1929 hsotg->flags.b.port_connect_status = 1;
1930}
1931
1932/**
1933 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
1934 *
1935 * @hsotg: Pointer to struct dwc2_hsotg
1936 * @force: If true, we won't try to reconnect even if we see device connected.
1937 *
1938 * Must be called with interrupt disabled and spinlock held
1939 */
1940void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
Paul Zimmerman7359d482013-03-11 17:47:59 -07001941{
1942 u32 intr;
Douglas Anderson6a659532015-11-19 13:23:14 -08001943 u32 hprt0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001944
1945 /* Set status flags for the hub driver */
1946 hsotg->flags.b.port_connect_status_change = 1;
1947 hsotg->flags.b.port_connect_status = 0;
1948
1949 /*
1950 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1951 * interrupt mask and status bits and disabling subsequent host
1952 * channel interrupts.
1953 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001954 intr = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001955 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001956 dwc2_writel(intr, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001957 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001958 dwc2_writel(intr, hsotg->regs + GINTSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001959
1960 /*
1961 * Turn off the vbus power only if the core has transitioned to device
1962 * mode. If still in host mode, need to keep power on to detect a
1963 * reconnection.
1964 */
1965 if (dwc2_is_device_mode(hsotg)) {
1966 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1967 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001968 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001969 }
1970
1971 dwc2_disable_host_interrupts(hsotg);
1972 }
1973
1974 /* Respond with an error status to all URBs in the schedule */
1975 dwc2_kill_all_urbs(hsotg);
1976
1977 if (dwc2_is_host_mode(hsotg))
1978 /* Clean up any host channels that were in use */
1979 dwc2_hcd_cleanup_channels(hsotg);
1980
1981 dwc2_host_disconnect(hsotg);
Douglas Anderson6a659532015-11-19 13:23:14 -08001982
1983 /*
1984 * Add an extra check here to see if we're actually connected but
1985 * we don't have a detection interrupt pending. This can happen if:
1986 * 1. hardware sees connect
1987 * 2. hardware sees disconnect
1988 * 3. hardware sees connect
1989 * 4. dwc2_port_intr() - clears connect interrupt
1990 * 5. dwc2_handle_common_intr() - calls here
1991 *
1992 * Without the extra check here we will end calling disconnect
1993 * and won't get any future interrupts to handle the connect.
1994 */
1995 if (!force) {
1996 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
1997 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
1998 dwc2_hcd_connect(hsotg);
1999 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002000}
2001
2002/**
2003 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
2004 *
2005 * @hsotg: Pointer to struct dwc2_hsotg
2006 */
2007static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
2008{
Douglas Anderson1fb7f122015-10-22 13:05:03 -07002009 if (hsotg->bus_suspended) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002010 hsotg->flags.b.port_suspend_change = 1;
Gregory Herrerob46146d52015-01-30 09:09:26 +01002011 usb_hcd_resume_root_hub(hsotg->priv);
Gregory Herrerob46146d52015-01-30 09:09:26 +01002012 }
Douglas Anderson1fb7f122015-10-22 13:05:03 -07002013
2014 if (hsotg->lx_state == DWC2_L1)
2015 hsotg->flags.b.port_l1_change = 1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002016}
2017
2018/**
2019 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
2020 *
2021 * @hsotg: Pointer to struct dwc2_hsotg
2022 *
2023 * Must be called with interrupt disabled and spinlock held
2024 */
2025void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
2026{
2027 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
2028
2029 /*
2030 * The root hub should be disconnected before this function is called.
2031 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
2032 * and the QH lists (via ..._hcd_endpoint_disable).
2033 */
2034
2035 /* Turn off all host-specific interrupts */
2036 dwc2_disable_host_interrupts(hsotg);
2037
2038 /* Turn off the vbus power */
2039 dev_dbg(hsotg->dev, "PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002040 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002041}
2042
Gregory Herrero33ad2612015-04-29 22:09:15 +02002043/* Caller must hold driver lock */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002044static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002045 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002046 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002047{
Paul Zimmerman7359d482013-03-11 17:47:59 -07002048 u32 intr_mask;
2049 int retval;
Nick Hudson9f8144c2013-12-06 14:01:44 -08002050 int dev_speed;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002051
2052 if (!hsotg->flags.b.port_connect_status) {
2053 /* No longer connected */
2054 dev_err(hsotg->dev, "Not connected\n");
2055 return -ENODEV;
2056 }
2057
Nick Hudson9f8144c2013-12-06 14:01:44 -08002058 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
2059
2060 /* Some configurations cannot support LS traffic on a FS root port */
2061 if ((dev_speed == USB_SPEED_LOW) &&
2062 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
2063 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002064 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Nick Hudson9f8144c2013-12-06 14:01:44 -08002065 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
2066
2067 if (prtspd == HPRT0_SPD_FULL_SPEED)
2068 return -ENODEV;
2069 }
2070
Paul Zimmerman7359d482013-03-11 17:47:59 -07002071 if (!qtd)
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002072 return -EINVAL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002073
2074 dwc2_hcd_qtd_init(qtd, urb);
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002075 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002076 if (retval) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002077 dev_err(hsotg->dev,
2078 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
2079 retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002080 return retval;
2081 }
2082
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002083 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002084 if (!(intr_mask & GINTSTS_SOF)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002085 enum dwc2_transaction_type tr_type;
2086
2087 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
2088 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
2089 /*
2090 * Do not schedule SG transactions until qtd has
2091 * URB_GIVEBACK_ASAP set
2092 */
2093 return 0;
2094
Paul Zimmerman7359d482013-03-11 17:47:59 -07002095 tr_type = dwc2_hcd_select_transactions(hsotg);
2096 if (tr_type != DWC2_TRANSACTION_NONE)
2097 dwc2_hcd_queue_transactions(hsotg, tr_type);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002098 }
2099
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002100 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002101}
2102
2103/* Must be called with interrupt disabled and spinlock held */
2104static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
2105 struct dwc2_hcd_urb *urb)
2106{
2107 struct dwc2_qh *qh;
2108 struct dwc2_qtd *urb_qtd;
2109
2110 urb_qtd = urb->qtd;
2111 if (!urb_qtd) {
2112 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
2113 return -EINVAL;
2114 }
2115
2116 qh = urb_qtd->qh;
2117 if (!qh) {
2118 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
2119 return -EINVAL;
2120 }
2121
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002122 urb->priv = NULL;
2123
Paul Zimmerman7359d482013-03-11 17:47:59 -07002124 if (urb_qtd->in_process && qh->channel) {
2125 dwc2_dump_channel_info(hsotg, qh->channel);
2126
2127 /* The QTD is in process (it has been assigned to a channel) */
2128 if (hsotg->flags.b.port_connect_status)
2129 /*
2130 * If still connected (i.e. in host mode), halt the
2131 * channel so it can be used for other transfers. If
2132 * no longer connected, the host registers can't be
2133 * written to halt the channel since the core is in
2134 * device mode.
2135 */
2136 dwc2_hc_halt(hsotg, qh->channel,
2137 DWC2_HC_XFER_URB_DEQUEUE);
2138 }
2139
2140 /*
2141 * Free the QTD and clean up the associated QH. Leave the QH in the
2142 * schedule if it has any remaining QTDs.
2143 */
John Youn95832c02017-01-23 14:57:26 -08002144 if (!hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002145 u8 in_process = urb_qtd->in_process;
2146
2147 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2148 if (in_process) {
2149 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
2150 qh->channel = NULL;
2151 } else if (list_empty(&qh->qtd_list)) {
2152 dwc2_hcd_qh_unlink(hsotg, qh);
2153 }
2154 } else {
2155 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2156 }
2157
2158 return 0;
2159}
2160
2161/* Must NOT be called with interrupt disabled or spinlock held */
2162static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
2163 struct usb_host_endpoint *ep, int retry)
2164{
2165 struct dwc2_qtd *qtd, *qtd_tmp;
2166 struct dwc2_qh *qh;
2167 unsigned long flags;
2168 int rc;
2169
2170 spin_lock_irqsave(&hsotg->lock, flags);
2171
2172 qh = ep->hcpriv;
2173 if (!qh) {
2174 rc = -EINVAL;
2175 goto err;
2176 }
2177
2178 while (!list_empty(&qh->qtd_list) && retry--) {
2179 if (retry == 0) {
2180 dev_err(hsotg->dev,
2181 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
2182 rc = -EBUSY;
2183 goto err;
2184 }
2185
2186 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01002187 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002188 spin_lock_irqsave(&hsotg->lock, flags);
2189 qh = ep->hcpriv;
2190 if (!qh) {
2191 rc = -EINVAL;
2192 goto err;
2193 }
2194 }
2195
2196 dwc2_hcd_qh_unlink(hsotg, qh);
2197
2198 /* Free each QTD in the QH's QTD list */
2199 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2200 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2201
2202 ep->hcpriv = NULL;
Douglas Anderson16e80212016-01-28 18:19:55 -08002203
2204 if (qh->channel && qh->channel->qh == qh)
2205 qh->channel->qh = NULL;
2206
Paul Zimmerman7359d482013-03-11 17:47:59 -07002207 spin_unlock_irqrestore(&hsotg->lock, flags);
Douglas Anderson16e80212016-01-28 18:19:55 -08002208
Paul Zimmerman7359d482013-03-11 17:47:59 -07002209 dwc2_hcd_qh_free(hsotg, qh);
2210
2211 return 0;
2212
2213err:
2214 ep->hcpriv = NULL;
2215 spin_unlock_irqrestore(&hsotg->lock, flags);
2216
2217 return rc;
2218}
2219
2220/* Must be called with interrupt disabled and spinlock held */
2221static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2222 struct usb_host_endpoint *ep)
2223{
2224 struct dwc2_qh *qh = ep->hcpriv;
2225
2226 if (!qh)
2227 return -EINVAL;
2228
2229 qh->data_toggle = DWC2_HC_PID_DATA0;
2230
2231 return 0;
2232}
2233
John Younb02038fa2016-02-23 19:55:00 -08002234/**
2235 * dwc2_core_init() - Initializes the DWC_otg controller registers and
2236 * prepares the core for device mode or host mode operation
2237 *
2238 * @hsotg: Programming view of the DWC_otg controller
2239 * @initial_setup: If true then this is the first init for this instance.
2240 */
2241static int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2242{
2243 u32 usbcfg, otgctl;
2244 int retval;
2245
2246 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2247
2248 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2249
2250 /* Set ULPI External VBUS bit if needed */
2251 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
John Youn95832c02017-01-23 14:57:26 -08002252 if (hsotg->params.phy_ulpi_ext_vbus)
John Younb02038fa2016-02-23 19:55:00 -08002253 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2254
2255 /* Set external TS Dline pulsing bit if needed */
2256 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
John Youn95832c02017-01-23 14:57:26 -08002257 if (hsotg->params.ts_dline)
John Younb02038fa2016-02-23 19:55:00 -08002258 usbcfg |= GUSBCFG_TERMSELDLPULSE;
2259
2260 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2261
2262 /*
2263 * Reset the Controller
2264 *
2265 * We only need to reset the controller if this is a re-init.
2266 * For the first init we know for sure that earlier code reset us (it
2267 * needed to in order to properly detect various parameters).
2268 */
2269 if (!initial_setup) {
2270 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
2271 if (retval) {
2272 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2273 __func__);
2274 return retval;
2275 }
2276 }
2277
2278 /*
2279 * This needs to happen in FS mode before any other programming occurs
2280 */
2281 retval = dwc2_phy_init(hsotg, initial_setup);
2282 if (retval)
2283 return retval;
2284
2285 /* Program the GAHBCFG Register */
2286 retval = dwc2_gahbcfg_init(hsotg);
2287 if (retval)
2288 return retval;
2289
2290 /* Program the GUSBCFG register */
2291 dwc2_gusbcfg_init(hsotg);
2292
2293 /* Program the GOTGCTL register */
2294 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2295 otgctl &= ~GOTGCTL_OTGVER;
John Younb02038fa2016-02-23 19:55:00 -08002296 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
John Younb02038fa2016-02-23 19:55:00 -08002297
2298 /* Clear the SRP success bit for FS-I2c */
2299 hsotg->srp_success = 0;
2300
2301 /* Enable common interrupts */
2302 dwc2_enable_common_interrupts(hsotg);
2303
2304 /*
2305 * Do device or host initialization based on mode during PCD and
2306 * HCD initialization
2307 */
2308 if (dwc2_is_host_mode(hsotg)) {
2309 dev_dbg(hsotg->dev, "Host Mode\n");
2310 hsotg->op_state = OTG_STATE_A_HOST;
2311 } else {
2312 dev_dbg(hsotg->dev, "Device Mode\n");
2313 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2314 }
2315
2316 return 0;
2317}
2318
2319/**
2320 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2321 * Host mode
2322 *
2323 * @hsotg: Programming view of DWC_otg controller
2324 *
2325 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2326 * request queues. Host channels are reset to ensure that they are ready for
2327 * performing transfers.
2328 */
2329static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2330{
2331 u32 hcfg, hfir, otgctl;
2332
2333 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2334
2335 /* Restart the Phy Clock */
2336 dwc2_writel(0, hsotg->regs + PCGCTL);
2337
2338 /* Initialize Host Configuration Register */
2339 dwc2_init_fs_ls_pclk_sel(hsotg);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08002340 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
2341 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
John Younb02038fa2016-02-23 19:55:00 -08002342 hcfg = dwc2_readl(hsotg->regs + HCFG);
2343 hcfg |= HCFG_FSLSSUPP;
2344 dwc2_writel(hcfg, hsotg->regs + HCFG);
2345 }
2346
2347 /*
2348 * This bit allows dynamic reloading of the HFIR register during
2349 * runtime. This bit needs to be programmed during initial configuration
2350 * and its value must not be changed during runtime.
2351 */
John Youn95832c02017-01-23 14:57:26 -08002352 if (hsotg->params.reload_ctl) {
John Younb02038fa2016-02-23 19:55:00 -08002353 hfir = dwc2_readl(hsotg->regs + HFIR);
2354 hfir |= HFIR_RLDCTRL;
2355 dwc2_writel(hfir, hsotg->regs + HFIR);
2356 }
2357
John Youn95832c02017-01-23 14:57:26 -08002358 if (hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002359 u32 op_mode = hsotg->hw_params.op_mode;
2360
2361 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2362 !hsotg->hw_params.dma_desc_enable ||
2363 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2364 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2365 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2366 dev_err(hsotg->dev,
2367 "Hardware does not support descriptor DMA mode -\n");
2368 dev_err(hsotg->dev,
2369 "falling back to buffer DMA mode.\n");
John Youn95832c02017-01-23 14:57:26 -08002370 hsotg->params.dma_desc_enable = false;
John Younb02038fa2016-02-23 19:55:00 -08002371 } else {
2372 hcfg = dwc2_readl(hsotg->regs + HCFG);
2373 hcfg |= HCFG_DESCDMA;
2374 dwc2_writel(hcfg, hsotg->regs + HCFG);
2375 }
2376 }
2377
2378 /* Configure data FIFO sizes */
2379 dwc2_config_fifos(hsotg);
2380
2381 /* TODO - check this */
2382 /* Clear Host Set HNP Enable in the OTG Control Register */
2383 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2384 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2385 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2386
2387 /* Make sure the FIFOs are flushed */
2388 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2389 dwc2_flush_rx_fifo(hsotg);
2390
2391 /* Clear Host Set HNP Enable in the OTG Control Register */
2392 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2393 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2394 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2395
John Youn95832c02017-01-23 14:57:26 -08002396 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002397 int num_channels, i;
2398 u32 hcchar;
2399
2400 /* Flush out any leftover queued requests */
John Younbea8e862016-11-03 17:55:53 -07002401 num_channels = hsotg->params.host_channels;
John Younb02038fa2016-02-23 19:55:00 -08002402 for (i = 0; i < num_channels; i++) {
2403 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2404 hcchar &= ~HCCHAR_CHENA;
2405 hcchar |= HCCHAR_CHDIS;
2406 hcchar &= ~HCCHAR_EPDIR;
2407 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2408 }
2409
2410 /* Halt all channels to put them into a known state */
2411 for (i = 0; i < num_channels; i++) {
John Younb02038fa2016-02-23 19:55:00 -08002412 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2413 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2414 hcchar &= ~HCCHAR_EPDIR;
2415 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2416 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2417 __func__, i);
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002418
2419 if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
2420 HCCHAR_CHENA, 1000)) {
2421 dev_warn(hsotg->dev, "Unable to clear enable on channel %d\n",
2422 i);
2423 }
John Younb02038fa2016-02-23 19:55:00 -08002424 }
2425 }
2426
2427 /* Turn on the vbus power */
2428 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2429 if (hsotg->op_state == OTG_STATE_A_HOST) {
2430 u32 hprt0 = dwc2_read_hprt0(hsotg);
2431
2432 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2433 !!(hprt0 & HPRT0_PWR));
2434 if (!(hprt0 & HPRT0_PWR)) {
2435 hprt0 |= HPRT0_PWR;
2436 dwc2_writel(hprt0, hsotg->regs + HPRT0);
2437 }
2438 }
2439
2440 dwc2_enable_host_interrupts(hsotg);
2441}
2442
Paul Zimmerman7359d482013-03-11 17:47:59 -07002443/*
2444 * Initializes dynamic portions of the DWC_otg HCD state
2445 *
2446 * Must be called with interrupt disabled and spinlock held
2447 */
2448static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2449{
2450 struct dwc2_host_chan *chan, *chan_tmp;
2451 int num_channels;
2452 int i;
2453
2454 hsotg->flags.d32 = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002455 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002456
John Youn95832c02017-01-23 14:57:26 -08002457 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002458 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07002459 hsotg->params.host_channels;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002460 } else {
2461 hsotg->non_periodic_channels = 0;
2462 hsotg->periodic_channels = 0;
2463 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002464
2465 /*
2466 * Put all channels in the free channel list and clean up channel
2467 * states
2468 */
2469 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2470 hc_list_entry)
2471 list_del_init(&chan->hc_list_entry);
2472
John Younbea8e862016-11-03 17:55:53 -07002473 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002474 for (i = 0; i < num_channels; i++) {
2475 chan = hsotg->hc_ptr_array[i];
2476 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2477 dwc2_hc_cleanup(hsotg, chan);
2478 }
2479
2480 /* Initialize the DWC core for host mode operation */
2481 dwc2_core_host_init(hsotg);
2482}
2483
2484static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2485 struct dwc2_host_chan *chan,
2486 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2487{
2488 int hub_addr, hub_port;
2489
2490 chan->do_split = 1;
2491 chan->xact_pos = qtd->isoc_split_pos;
2492 chan->complete_split = qtd->complete_split;
2493 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2494 chan->hub_addr = (u8)hub_addr;
2495 chan->hub_port = (u8)hub_port;
2496}
2497
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002498static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2499 struct dwc2_host_chan *chan,
2500 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002501{
2502 struct dwc2_hcd_urb *urb = qtd->urb;
2503 struct dwc2_hcd_iso_packet_desc *frame_desc;
2504
2505 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2506 case USB_ENDPOINT_XFER_CONTROL:
2507 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2508
2509 switch (qtd->control_phase) {
2510 case DWC2_CONTROL_SETUP:
2511 dev_vdbg(hsotg->dev, " Control setup transaction\n");
2512 chan->do_ping = 0;
2513 chan->ep_is_in = 0;
2514 chan->data_pid_start = DWC2_HC_PID_SETUP;
John Youn95832c02017-01-23 14:57:26 -08002515 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002516 chan->xfer_dma = urb->setup_dma;
2517 else
2518 chan->xfer_buf = urb->setup_packet;
2519 chan->xfer_len = 8;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002520 break;
2521
2522 case DWC2_CONTROL_DATA:
2523 dev_vdbg(hsotg->dev, " Control data transaction\n");
2524 chan->data_pid_start = qtd->data_toggle;
2525 break;
2526
2527 case DWC2_CONTROL_STATUS:
2528 /*
2529 * Direction is opposite of data direction or IN if no
2530 * data
2531 */
2532 dev_vdbg(hsotg->dev, " Control status transaction\n");
2533 if (urb->length == 0)
2534 chan->ep_is_in = 1;
2535 else
2536 chan->ep_is_in =
2537 dwc2_hcd_is_pipe_out(&urb->pipe_info);
2538 if (chan->ep_is_in)
2539 chan->do_ping = 0;
2540 chan->data_pid_start = DWC2_HC_PID_DATA1;
2541 chan->xfer_len = 0;
John Youn95832c02017-01-23 14:57:26 -08002542 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002543 chan->xfer_dma = hsotg->status_buf_dma;
2544 else
2545 chan->xfer_buf = hsotg->status_buf;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002546 break;
2547 }
2548 break;
2549
2550 case USB_ENDPOINT_XFER_BULK:
2551 chan->ep_type = USB_ENDPOINT_XFER_BULK;
2552 break;
2553
2554 case USB_ENDPOINT_XFER_INT:
2555 chan->ep_type = USB_ENDPOINT_XFER_INT;
2556 break;
2557
2558 case USB_ENDPOINT_XFER_ISOC:
2559 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
John Youn95832c02017-01-23 14:57:26 -08002560 if (hsotg->params.dma_desc_enable)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002561 break;
2562
2563 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2564 frame_desc->status = 0;
2565
John Youn95832c02017-01-23 14:57:26 -08002566 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002567 chan->xfer_dma = urb->dma;
2568 chan->xfer_dma += frame_desc->offset +
2569 qtd->isoc_split_offset;
2570 } else {
2571 chan->xfer_buf = urb->buf;
2572 chan->xfer_buf += frame_desc->offset +
2573 qtd->isoc_split_offset;
2574 }
2575
2576 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2577
Paul Zimmerman7359d482013-03-11 17:47:59 -07002578 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2579 if (chan->xfer_len <= 188)
2580 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2581 else
2582 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2583 }
2584 break;
2585 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002586}
2587
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002588#define DWC2_USB_DMA_ALIGN 4
2589
2590struct dma_aligned_buffer {
2591 void *kmalloc_ptr;
2592 void *old_xfer_buffer;
2593 u8 data[0];
2594};
2595
2596static void dwc2_free_dma_aligned_buffer(struct urb *urb)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002597{
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002598 struct dma_aligned_buffer *temp;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002599
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002600 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2601 return;
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002602
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002603 temp = container_of(urb->transfer_buffer,
John Youn9da51972017-01-17 20:30:27 -08002604 struct dma_aligned_buffer, data);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002605
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002606 if (usb_urb_dir_in(urb))
2607 memcpy(temp->old_xfer_buffer, temp->data,
2608 urb->transfer_buffer_length);
2609 urb->transfer_buffer = temp->old_xfer_buffer;
2610 kfree(temp->kmalloc_ptr);
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002611
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002612 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2613}
Paul Zimmerman7359d482013-03-11 17:47:59 -07002614
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002615static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
2616{
2617 struct dma_aligned_buffer *temp, *kmalloc_ptr;
2618 size_t kmalloc_size;
Gregory Herrerodb62b9a2015-04-29 22:09:16 +02002619
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002620 if (urb->num_sgs || urb->sg ||
2621 urb->transfer_buffer_length == 0 ||
2622 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2623 return 0;
2624
2625 /* Allocate a buffer with enough padding for alignment */
2626 kmalloc_size = urb->transfer_buffer_length +
2627 sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
2628
2629 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2630 if (!kmalloc_ptr)
2631 return -ENOMEM;
2632
2633 /* Position our struct dma_aligned_buffer such that data is aligned */
2634 temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
2635 temp->kmalloc_ptr = kmalloc_ptr;
2636 temp->old_xfer_buffer = urb->transfer_buffer;
2637 if (usb_urb_dir_out(urb))
2638 memcpy(temp->data, urb->transfer_buffer,
2639 urb->transfer_buffer_length);
2640 urb->transfer_buffer = temp->data;
2641
2642 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2643
Paul Zimmerman7359d482013-03-11 17:47:59 -07002644 return 0;
2645}
2646
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002647static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
John Youn9da51972017-01-17 20:30:27 -08002648 gfp_t mem_flags)
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002649{
2650 int ret;
2651
2652 /* We assume setup_dma is always aligned; warn if not */
2653 WARN_ON_ONCE(urb->setup_dma &&
2654 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
2655
2656 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
2657 if (ret)
2658 return ret;
2659
2660 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2661 if (ret)
2662 dwc2_free_dma_aligned_buffer(urb);
2663
2664 return ret;
2665}
2666
2667static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2668{
2669 usb_hcd_unmap_urb_for_dma(hcd, urb);
2670 dwc2_free_dma_aligned_buffer(urb);
2671}
2672
Paul Zimmerman7359d482013-03-11 17:47:59 -07002673/**
2674 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2675 * channel and initializes the host channel to perform the transactions. The
2676 * host channel is removed from the free list.
2677 *
2678 * @hsotg: The HCD state structure
2679 * @qh: Transactions from the first QTD for this QH are selected and assigned
2680 * to a free host channel
2681 */
Dom Cobley20f2eb92013-09-23 14:23:34 -07002682static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002683{
2684 struct dwc2_host_chan *chan;
2685 struct dwc2_hcd_urb *urb;
2686 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002687
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002688 if (dbg_qh(qh))
2689 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002690
2691 if (list_empty(&qh->qtd_list)) {
2692 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002693 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002694 }
2695
2696 if (list_empty(&hsotg->free_hc_list)) {
2697 dev_dbg(hsotg->dev, "No free channel to assign\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002698 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002699 }
2700
2701 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2702 hc_list_entry);
2703
Dom Cobley20f2eb92013-09-23 14:23:34 -07002704 /* Remove host channel from free list */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002705 list_del_init(&chan->hc_list_entry);
2706
2707 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2708 urb = qtd->urb;
2709 qh->channel = chan;
2710 qtd->in_process = 1;
2711
2712 /*
2713 * Use usb_pipedevice to determine device address. This address is
2714 * 0 before the SET_ADDRESS command and the correct address afterward.
2715 */
2716 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2717 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2718 chan->speed = qh->dev_speed;
2719 chan->max_packet = dwc2_max_packet(qh->maxp);
2720
2721 chan->xfer_started = 0;
2722 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2723 chan->error_state = (qtd->error_count > 0);
2724 chan->halt_on_queue = 0;
2725 chan->halt_pending = 0;
2726 chan->requests = 0;
2727
2728 /*
2729 * The following values may be modified in the transfer type section
2730 * below. The xfer_len value may be reduced when the transfer is
2731 * started to accommodate the max widths of the XferSize and PktCnt
2732 * fields in the HCTSIZn register.
2733 */
2734
2735 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2736 if (chan->ep_is_in)
2737 chan->do_ping = 0;
2738 else
2739 chan->do_ping = qh->ping_state;
2740
2741 chan->data_pid_start = qh->data_toggle;
2742 chan->multi_count = 1;
2743
Rashika Kheriabb6c3422013-10-26 23:11:22 +05302744 if (urb->actual_length > urb->length &&
John Youn9da51972017-01-17 20:30:27 -08002745 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
Paul Zimmerman84181082013-09-23 14:23:33 -07002746 urb->actual_length = urb->length;
2747
John Youn95832c02017-01-23 14:57:26 -08002748 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002749 chan->xfer_dma = urb->dma + urb->actual_length;
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002750 else
Paul Zimmerman7359d482013-03-11 17:47:59 -07002751 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002752
2753 chan->xfer_len = urb->length - urb->actual_length;
2754 chan->xfer_count = 0;
2755
2756 /* Set the split attributes if required */
2757 if (qh->do_split)
2758 dwc2_hc_init_split(hsotg, chan, qtd, urb);
2759 else
2760 chan->do_split = 0;
2761
2762 /* Set the transfer attributes */
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002763 dwc2_hc_init_xfer(hsotg, chan, qtd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002764
2765 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2766 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2767 /*
2768 * This value may be modified when the transfer is started
2769 * to reflect the actual transfer length
2770 */
2771 chan->multi_count = dwc2_hb_mult(qh->maxp);
2772
John Youn95832c02017-01-23 14:57:26 -08002773 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002774 chan->desc_list_addr = qh->desc_list_dma;
Gregory Herrero95105a92015-11-20 11:49:29 +01002775 chan->desc_list_sz = qh->desc_list_sz;
2776 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002777
2778 dwc2_hc_init(hsotg, chan);
2779 chan->qh = qh;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002780
2781 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002782}
2783
2784/**
2785 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2786 * schedule and assigns them to available host channels. Called from the HCD
2787 * interrupt handler functions.
2788 *
2789 * @hsotg: The HCD state structure
2790 *
2791 * Return: The types of new transactions that were assigned to host channels
2792 */
2793enum dwc2_transaction_type dwc2_hcd_select_transactions(
2794 struct dwc2_hsotg *hsotg)
2795{
2796 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2797 struct list_head *qh_ptr;
2798 struct dwc2_qh *qh;
2799 int num_channels;
2800
2801#ifdef DWC2_DEBUG_SOF
2802 dev_vdbg(hsotg->dev, " Select Transactions\n");
2803#endif
2804
2805 /* Process entries in the periodic ready list */
2806 qh_ptr = hsotg->periodic_sched_ready.next;
2807 while (qh_ptr != &hsotg->periodic_sched_ready) {
2808 if (list_empty(&hsotg->free_hc_list))
2809 break;
John Youn95832c02017-01-23 14:57:26 -08002810 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002811 if (hsotg->available_host_channels <= 1)
2812 break;
2813 hsotg->available_host_channels--;
2814 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002815 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -07002816 if (dwc2_assign_and_init_hc(hsotg, qh))
2817 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002818
2819 /*
2820 * Move the QH from the periodic ready schedule to the
2821 * periodic assigned schedule
2822 */
2823 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002824 list_move_tail(&qh->qh_list_entry,
2825 &hsotg->periodic_sched_assigned);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002826 ret_val = DWC2_TRANSACTION_PERIODIC;
2827 }
2828
2829 /*
2830 * Process entries in the inactive portion of the non-periodic
2831 * schedule. Some free host channels may not be used if they are
2832 * reserved for periodic transfers.
2833 */
John Younbea8e862016-11-03 17:55:53 -07002834 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002835 qh_ptr = hsotg->non_periodic_sched_inactive.next;
2836 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
John Youn95832c02017-01-23 14:57:26 -08002837 if (!hsotg->params.uframe_sched &&
Dom Cobley20f2eb92013-09-23 14:23:34 -07002838 hsotg->non_periodic_channels >= num_channels -
Paul Zimmerman7359d482013-03-11 17:47:59 -07002839 hsotg->periodic_channels)
2840 break;
2841 if (list_empty(&hsotg->free_hc_list))
2842 break;
2843 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
John Youn95832c02017-01-23 14:57:26 -08002844 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002845 if (hsotg->available_host_channels < 1)
2846 break;
2847 hsotg->available_host_channels--;
2848 }
2849
2850 if (dwc2_assign_and_init_hc(hsotg, qh))
2851 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002852
2853 /*
2854 * Move the QH from the non-periodic inactive schedule to the
2855 * non-periodic active schedule
2856 */
2857 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002858 list_move_tail(&qh->qh_list_entry,
2859 &hsotg->non_periodic_sched_active);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002860
2861 if (ret_val == DWC2_TRANSACTION_NONE)
2862 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2863 else
2864 ret_val = DWC2_TRANSACTION_ALL;
2865
John Youn95832c02017-01-23 14:57:26 -08002866 if (!hsotg->params.uframe_sched)
Dom Cobley20f2eb92013-09-23 14:23:34 -07002867 hsotg->non_periodic_channels++;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002868 }
2869
2870 return ret_val;
2871}
2872
2873/**
2874 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2875 * a host channel associated with either a periodic or non-periodic transfer
2876 *
2877 * @hsotg: The HCD state structure
2878 * @chan: Host channel descriptor associated with either a periodic or
2879 * non-periodic transfer
2880 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2881 * for periodic transfers or the non-periodic Tx FIFO
2882 * for non-periodic transfers
2883 *
2884 * Return: 1 if a request is queued and more requests may be needed to
2885 * complete the transfer, 0 if no more requests are required for this
2886 * transfer, -1 if there is insufficient space in the Tx FIFO
2887 *
2888 * This function assumes that there is space available in the appropriate
2889 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2890 * it checks whether space is available in the appropriate Tx FIFO.
2891 *
2892 * Must be called with interrupt disabled and spinlock held
2893 */
2894static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2895 struct dwc2_host_chan *chan,
2896 u16 fifo_dwords_avail)
2897{
2898 int retval = 0;
2899
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002900 if (chan->do_split)
2901 /* Put ourselves on the list to keep order straight */
2902 list_move_tail(&chan->split_order_list_entry,
2903 &hsotg->split_order);
2904
John Youn95832c02017-01-23 14:57:26 -08002905 if (hsotg->params.host_dma) {
2906 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002907 if (!chan->xfer_started ||
2908 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2909 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2910 chan->qh->ping_state = 0;
2911 }
2912 } else if (!chan->xfer_started) {
2913 dwc2_hc_start_transfer(hsotg, chan);
2914 chan->qh->ping_state = 0;
2915 }
2916 } else if (chan->halt_pending) {
2917 /* Don't queue a request if the channel has been halted */
2918 } else if (chan->halt_on_queue) {
2919 dwc2_hc_halt(hsotg, chan, chan->halt_status);
2920 } else if (chan->do_ping) {
2921 if (!chan->xfer_started)
2922 dwc2_hc_start_transfer(hsotg, chan);
2923 } else if (!chan->ep_is_in ||
2924 chan->data_pid_start == DWC2_HC_PID_SETUP) {
2925 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2926 if (!chan->xfer_started) {
2927 dwc2_hc_start_transfer(hsotg, chan);
2928 retval = 1;
2929 } else {
2930 retval = dwc2_hc_continue_transfer(hsotg, chan);
2931 }
2932 } else {
2933 retval = -1;
2934 }
2935 } else {
2936 if (!chan->xfer_started) {
2937 dwc2_hc_start_transfer(hsotg, chan);
2938 retval = 1;
2939 } else {
2940 retval = dwc2_hc_continue_transfer(hsotg, chan);
2941 }
2942 }
2943
2944 return retval;
2945}
2946
2947/*
2948 * Processes periodic channels for the next frame and queues transactions for
2949 * these channels to the DWC_otg controller. After queueing transactions, the
2950 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2951 * to queue as Periodic Tx FIFO or request queue space becomes available.
2952 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2953 *
2954 * Must be called with interrupt disabled and spinlock held
2955 */
2956static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2957{
2958 struct list_head *qh_ptr;
2959 struct dwc2_qh *qh;
2960 u32 tx_status;
2961 u32 fspcavail;
2962 u32 gintmsk;
2963 int status;
Douglas Anderson4e50e012016-01-28 18:20:03 -08002964 bool no_queue_space = false;
2965 bool no_fifo_space = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002966 u32 qspcavail;
2967
Douglas Anderson4e50e012016-01-28 18:20:03 -08002968 /* If empty list then just adjust interrupt enables */
2969 if (list_empty(&hsotg->periodic_sched_assigned))
2970 goto exit;
2971
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002972 if (dbg_perio())
2973 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07002974
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002975 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002976 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2977 TXSTS_QSPCAVAIL_SHIFT;
2978 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2979 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002980
2981 if (dbg_perio()) {
2982 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
2983 qspcavail);
2984 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
2985 fspcavail);
2986 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002987
2988 qh_ptr = hsotg->periodic_sched_assigned.next;
2989 while (qh_ptr != &hsotg->periodic_sched_assigned) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002990 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmanacdb9042013-08-30 18:45:16 +02002991 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2992 TXSTS_QSPCAVAIL_SHIFT;
2993 if (qspcavail == 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01002994 no_queue_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002995 break;
2996 }
2997
2998 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2999 if (!qh->channel) {
3000 qh_ptr = qh_ptr->next;
3001 continue;
3002 }
3003
3004 /* Make sure EP's TT buffer is clean before queueing qtds */
3005 if (qh->tt_buffer_dirty) {
3006 qh_ptr = qh_ptr->next;
3007 continue;
3008 }
3009
3010 /*
3011 * Set a flag if we're queuing high-bandwidth in slave mode.
3012 * The flag prevents any halts to get into the request queue in
3013 * the middle of multiple high-bandwidth packets getting queued.
3014 */
John Youn95832c02017-01-23 14:57:26 -08003015 if (!hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08003016 qh->channel->multi_count > 1)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003017 hsotg->queuing_high_bandwidth = 1;
3018
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003019 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3020 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003021 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3022 if (status < 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003023 no_fifo_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003024 break;
3025 }
3026
3027 /*
3028 * In Slave mode, stay on the current transfer until there is
3029 * nothing more to do or the high-bandwidth request count is
3030 * reached. In DMA mode, only need to queue one request. The
3031 * controller automatically handles multiple packets for
3032 * high-bandwidth transfers.
3033 */
John Youn95832c02017-01-23 14:57:26 -08003034 if (hsotg->params.host_dma || status == 0 ||
Paul Zimmerman7359d482013-03-11 17:47:59 -07003035 qh->channel->requests == qh->channel->multi_count) {
3036 qh_ptr = qh_ptr->next;
3037 /*
3038 * Move the QH from the periodic assigned schedule to
3039 * the periodic queued schedule
3040 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08003041 list_move_tail(&qh->qh_list_entry,
3042 &hsotg->periodic_sched_queued);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003043
3044 /* done queuing high bandwidth */
3045 hsotg->queuing_high_bandwidth = 0;
3046 }
3047 }
3048
Douglas Anderson4e50e012016-01-28 18:20:03 -08003049exit:
3050 if (no_queue_space || no_fifo_space ||
John Youn95832c02017-01-23 14:57:26 -08003051 (!hsotg->params.host_dma &&
Douglas Anderson4e50e012016-01-28 18:20:03 -08003052 !list_empty(&hsotg->periodic_sched_assigned))) {
3053 /*
3054 * May need to queue more transactions as the request
3055 * queue or Tx FIFO empties. Enable the periodic Tx
3056 * FIFO empty interrupt. (Always use the half-empty
3057 * level to ensure that new requests are loaded as
3058 * soon as possible.)
3059 */
3060 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3061 if (!(gintmsk & GINTSTS_PTXFEMP)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003062 gintmsk |= GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003063 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Douglas Anderson4e50e012016-01-28 18:20:03 -08003064 }
3065 } else {
3066 /*
3067 * Disable the Tx FIFO empty interrupt since there are
3068 * no more transactions that need to be queued right
3069 * now. This function is called from interrupt
3070 * handlers to queue more transactions as transfer
3071 * states change.
John Youn38beaec2017-01-17 20:31:13 -08003072 */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003073 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3074 if (gintmsk & GINTSTS_PTXFEMP) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003075 gintmsk &= ~GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003076 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003077 }
3078 }
3079}
3080
3081/*
3082 * Processes active non-periodic channels and queues transactions for these
3083 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3084 * FIFO Empty interrupt is enabled if there are more transactions to queue as
3085 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3086 * FIFO Empty interrupt is disabled.
3087 *
3088 * Must be called with interrupt disabled and spinlock held
3089 */
3090static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3091{
3092 struct list_head *orig_qh_ptr;
3093 struct dwc2_qh *qh;
3094 u32 tx_status;
3095 u32 qspcavail;
3096 u32 fspcavail;
3097 u32 gintmsk;
3098 int status;
3099 int no_queue_space = 0;
3100 int no_fifo_space = 0;
3101 int more_to_do = 0;
3102
3103 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3104
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003105 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003106 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3107 TXSTS_QSPCAVAIL_SHIFT;
3108 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3109 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003110 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
3111 qspcavail);
3112 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
3113 fspcavail);
3114
3115 /*
3116 * Keep track of the starting point. Skip over the start-of-list
3117 * entry.
3118 */
3119 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3120 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3121 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3122
3123 /*
3124 * Process once through the active list or until no more space is
3125 * available in the request queue or the Tx FIFO
3126 */
3127 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003128 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003129 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3130 TXSTS_QSPCAVAIL_SHIFT;
John Youn95832c02017-01-23 14:57:26 -08003131 if (!hsotg->params.host_dma && qspcavail == 0) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003132 no_queue_space = 1;
3133 break;
3134 }
3135
3136 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3137 qh_list_entry);
3138 if (!qh->channel)
3139 goto next;
3140
3141 /* Make sure EP's TT buffer is clean before queueing qtds */
3142 if (qh->tt_buffer_dirty)
3143 goto next;
3144
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003145 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3146 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003147 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3148
3149 if (status > 0) {
3150 more_to_do = 1;
3151 } else if (status < 0) {
3152 no_fifo_space = 1;
3153 break;
3154 }
3155next:
3156 /* Advance to next QH, skipping start-of-list entry */
3157 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3158 if (hsotg->non_periodic_qh_ptr ==
3159 &hsotg->non_periodic_sched_active)
3160 hsotg->non_periodic_qh_ptr =
3161 hsotg->non_periodic_qh_ptr->next;
3162 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3163
John Youn95832c02017-01-23 14:57:26 -08003164 if (!hsotg->params.host_dma) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003165 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003166 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3167 TXSTS_QSPCAVAIL_SHIFT;
3168 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3169 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003170 dev_vdbg(hsotg->dev,
3171 " NP Tx Req Queue Space Avail (after queue): %d\n",
3172 qspcavail);
3173 dev_vdbg(hsotg->dev,
3174 " NP Tx FIFO Space Avail (after queue): %d\n",
3175 fspcavail);
3176
3177 if (more_to_do || no_queue_space || no_fifo_space) {
3178 /*
3179 * May need to queue more transactions as the request
3180 * queue or Tx FIFO empties. Enable the non-periodic
3181 * Tx FIFO empty interrupt. (Always use the half-empty
3182 * level to ensure that new requests are loaded as
3183 * soon as possible.)
3184 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003185 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003186 gintmsk |= GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003187 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003188 } else {
3189 /*
3190 * Disable the Tx FIFO empty interrupt since there are
3191 * no more transactions that need to be queued right
3192 * now. This function is called from interrupt
3193 * handlers to queue more transactions as transfer
3194 * states change.
3195 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003196 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003197 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003198 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003199 }
3200 }
3201}
3202
3203/**
3204 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3205 * and queues transactions for these channels to the DWC_otg controller. Called
3206 * from the HCD interrupt handler functions.
3207 *
3208 * @hsotg: The HCD state structure
3209 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3210 * or both)
3211 *
3212 * Must be called with interrupt disabled and spinlock held
3213 */
3214void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3215 enum dwc2_transaction_type tr_type)
3216{
3217#ifdef DWC2_DEBUG_SOF
3218 dev_vdbg(hsotg->dev, "Queue Transactions\n");
3219#endif
3220 /* Process host channels associated with periodic transfers */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003221 if (tr_type == DWC2_TRANSACTION_PERIODIC ||
3222 tr_type == DWC2_TRANSACTION_ALL)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003223 dwc2_process_periodic_channels(hsotg);
3224
3225 /* Process host channels associated with non-periodic transfers */
3226 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3227 tr_type == DWC2_TRANSACTION_ALL) {
3228 if (!list_empty(&hsotg->non_periodic_sched_active)) {
3229 dwc2_process_non_periodic_channels(hsotg);
3230 } else {
3231 /*
3232 * Ensure NP Tx FIFO empty interrupt is disabled when
3233 * there are no non-periodic transfers to process
3234 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003235 u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003236
3237 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003238 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003239 }
3240 }
3241}
3242
3243static void dwc2_conn_id_status_change(struct work_struct *work)
3244{
3245 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3246 wf_otg);
3247 u32 count = 0;
3248 u32 gotgctl;
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003249 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003250
3251 dev_dbg(hsotg->dev, "%s()\n", __func__);
3252
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003253 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003254 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3255 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3256 !!(gotgctl & GOTGCTL_CONID_B));
3257
3258 /* B-Device connector (Device Mode) */
3259 if (gotgctl & GOTGCTL_CONID_B) {
3260 /* Wait for switch to device mode */
3261 dev_dbg(hsotg->dev, "connId B\n");
Chen Yu9156a7e2017-01-23 14:59:57 -08003262 if (hsotg->bus_suspended) {
3263 dev_info(hsotg->dev,
3264 "Do port resume before switching to device mode\n");
3265 dwc2_port_resume(hsotg);
3266 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003267 while (!dwc2_is_device_mode(hsotg)) {
3268 dev_info(hsotg->dev,
3269 "Waiting for Peripheral Mode, Mode=%s\n",
3270 dwc2_is_host_mode(hsotg) ? "Host" :
3271 "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003272 msleep(20);
John Stultzfc30c4b2017-01-23 14:59:35 -08003273 /*
3274 * Sometimes the initial GOTGCTRL read is wrong, so
3275 * check it again and jump to host mode if that was
3276 * the case.
3277 */
3278 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3279 if (!(gotgctl & GOTGCTL_CONID_B))
3280 goto host;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003281 if (++count > 250)
3282 break;
3283 }
3284 if (count > 250)
3285 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003286 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003287 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003288 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003289 dwc2_enable_global_interrupts(hsotg);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003290 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003291 dwc2_hsotg_core_init_disconnected(hsotg, false);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003292 spin_unlock_irqrestore(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003293 dwc2_hsotg_core_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003294 } else {
John Stultzfc30c4b2017-01-23 14:59:35 -08003295host:
Paul Zimmerman7359d482013-03-11 17:47:59 -07003296 /* A-Device connector (Host Mode) */
3297 dev_dbg(hsotg->dev, "connId A\n");
3298 while (!dwc2_is_host_mode(hsotg)) {
3299 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3300 dwc2_is_host_mode(hsotg) ?
3301 "Host" : "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003302 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003303 if (++count > 250)
3304 break;
3305 }
3306 if (count > 250)
3307 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003308 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003309
John Stultzd2471d42017-10-23 14:32:48 -07003310 spin_lock_irqsave(&hsotg->lock, flags);
3311 dwc2_hsotg_disconnect(hsotg);
3312 spin_unlock_irqrestore(&hsotg->lock, flags);
3313
3314 hsotg->op_state = OTG_STATE_A_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003315 /* Initialize the Core for Host mode */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003316 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003317 dwc2_enable_global_interrupts(hsotg);
3318 dwc2_hcd_start(hsotg);
3319 }
3320}
3321
Kees Cooke99e88a2017-10-16 14:43:17 -07003322static void dwc2_wakeup_detected(struct timer_list *t)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003323{
Kees Cooke99e88a2017-10-16 14:43:17 -07003324 struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003325 u32 hprt0;
3326
3327 dev_dbg(hsotg->dev, "%s()\n", __func__);
3328
3329 /*
3330 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3331 * so that OPT tests pass with all PHYs.)
3332 */
3333 hprt0 = dwc2_read_hprt0(hsotg);
3334 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3335 hprt0 &= ~HPRT0_RES;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003336 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003337 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003338 dwc2_readl(hsotg->regs + HPRT0));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003339
3340 dwc2_hcd_rem_wakeup(hsotg);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003341 hsotg->bus_suspended = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003342
3343 /* Change to L0 state */
3344 hsotg->lx_state = DWC2_L0;
3345}
3346
3347static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3348{
3349 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3350
3351 return hcd->self.b_hnp_enable;
3352}
3353
3354/* Must NOT be called with interrupt disabled or spinlock held */
3355static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3356{
3357 unsigned long flags;
3358 u32 hprt0;
3359 u32 pcgctl;
3360 u32 gotgctl;
3361
3362 dev_dbg(hsotg->dev, "%s()\n", __func__);
3363
3364 spin_lock_irqsave(&hsotg->lock, flags);
3365
3366 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003367 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003368 gotgctl |= GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003369 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003370 hsotg->op_state = OTG_STATE_A_SUSPEND;
3371 }
3372
3373 hprt0 = dwc2_read_hprt0(hsotg);
3374 hprt0 |= HPRT0_SUSP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003375 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003376
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003377 hsotg->bus_suspended = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003378
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003379 /*
3380 * If hibernation is supported, Phy clock will be suspended
3381 * after registers are backuped.
3382 */
John Younbea8e862016-11-03 17:55:53 -07003383 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003384 /* Suspend the Phy Clock */
3385 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3386 pcgctl |= PCGCTL_STOPPCLK;
3387 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3388 udelay(10);
3389 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003390
3391 /* For HNP the bus must be suspended for at least 200ms */
3392 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003393 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003394 pcgctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003395 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003396
3397 spin_unlock_irqrestore(&hsotg->lock, flags);
3398
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003399 msleep(200);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003400 } else {
3401 spin_unlock_irqrestore(&hsotg->lock, flags);
3402 }
3403}
3404
Gregory Herrero30db1032015-09-22 15:16:38 +02003405/* Must NOT be called with interrupt disabled or spinlock held */
3406static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
3407{
3408 unsigned long flags;
3409 u32 hprt0;
3410 u32 pcgctl;
3411
Douglas Anderson4d273c22015-10-14 15:58:27 -07003412 spin_lock_irqsave(&hsotg->lock, flags);
3413
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003414 /*
3415 * If hibernation is supported, Phy clock is already resumed
3416 * after registers restore.
3417 */
John Younbea8e862016-11-03 17:55:53 -07003418 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003419 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3420 pcgctl &= ~PCGCTL_STOPPCLK;
3421 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003422 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003423 msleep(20);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003424 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003425 }
Gregory Herrero30db1032015-09-22 15:16:38 +02003426
Gregory Herrero30db1032015-09-22 15:16:38 +02003427 hprt0 = dwc2_read_hprt0(hsotg);
3428 hprt0 |= HPRT0_RES;
3429 hprt0 &= ~HPRT0_SUSP;
3430 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3431 spin_unlock_irqrestore(&hsotg->lock, flags);
3432
3433 msleep(USB_RESUME_TIMEOUT);
3434
3435 spin_lock_irqsave(&hsotg->lock, flags);
3436 hprt0 = dwc2_read_hprt0(hsotg);
3437 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
3438 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003439 hsotg->bus_suspended = false;
Gregory Herrero30db1032015-09-22 15:16:38 +02003440 spin_unlock_irqrestore(&hsotg->lock, flags);
3441}
3442
Paul Zimmerman7359d482013-03-11 17:47:59 -07003443/* Handles hub class-specific requests */
3444static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3445 u16 wvalue, u16 windex, char *buf, u16 wlength)
3446{
3447 struct usb_hub_descriptor *hub_desc;
3448 int retval = 0;
3449 u32 hprt0;
3450 u32 port_status;
3451 u32 speed;
3452 u32 pcgctl;
3453
3454 switch (typereq) {
3455 case ClearHubFeature:
3456 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3457
3458 switch (wvalue) {
3459 case C_HUB_LOCAL_POWER:
3460 case C_HUB_OVER_CURRENT:
3461 /* Nothing required here */
3462 break;
3463
3464 default:
3465 retval = -EINVAL;
3466 dev_err(hsotg->dev,
3467 "ClearHubFeature request %1xh unknown\n",
3468 wvalue);
3469 }
3470 break;
3471
3472 case ClearPortFeature:
3473 if (wvalue != USB_PORT_FEAT_L1)
3474 if (!windex || windex > 1)
3475 goto error;
3476 switch (wvalue) {
3477 case USB_PORT_FEAT_ENABLE:
3478 dev_dbg(hsotg->dev,
3479 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3480 hprt0 = dwc2_read_hprt0(hsotg);
3481 hprt0 |= HPRT0_ENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003482 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003483 break;
3484
3485 case USB_PORT_FEAT_SUSPEND:
3486 dev_dbg(hsotg->dev,
3487 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00003488
Gregory Herrerobea78552015-09-22 15:16:44 +02003489 if (hsotg->bus_suspended)
3490 dwc2_port_resume(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003491 break;
3492
3493 case USB_PORT_FEAT_POWER:
3494 dev_dbg(hsotg->dev,
3495 "ClearPortFeature USB_PORT_FEAT_POWER\n");
3496 hprt0 = dwc2_read_hprt0(hsotg);
3497 hprt0 &= ~HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003498 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003499 break;
3500
3501 case USB_PORT_FEAT_INDICATOR:
3502 dev_dbg(hsotg->dev,
3503 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3504 /* Port indicator not supported */
3505 break;
3506
3507 case USB_PORT_FEAT_C_CONNECTION:
3508 /*
3509 * Clears driver's internal Connect Status Change flag
3510 */
3511 dev_dbg(hsotg->dev,
3512 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3513 hsotg->flags.b.port_connect_status_change = 0;
3514 break;
3515
3516 case USB_PORT_FEAT_C_RESET:
3517 /* Clears driver's internal Port Reset Change flag */
3518 dev_dbg(hsotg->dev,
3519 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3520 hsotg->flags.b.port_reset_change = 0;
3521 break;
3522
3523 case USB_PORT_FEAT_C_ENABLE:
3524 /*
3525 * Clears the driver's internal Port Enable/Disable
3526 * Change flag
3527 */
3528 dev_dbg(hsotg->dev,
3529 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3530 hsotg->flags.b.port_enable_change = 0;
3531 break;
3532
3533 case USB_PORT_FEAT_C_SUSPEND:
3534 /*
3535 * Clears the driver's internal Port Suspend Change
3536 * flag, which is set when resume signaling on the host
3537 * port is complete
3538 */
3539 dev_dbg(hsotg->dev,
3540 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3541 hsotg->flags.b.port_suspend_change = 0;
3542 break;
3543
3544 case USB_PORT_FEAT_C_PORT_L1:
3545 dev_dbg(hsotg->dev,
3546 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3547 hsotg->flags.b.port_l1_change = 0;
3548 break;
3549
3550 case USB_PORT_FEAT_C_OVER_CURRENT:
3551 dev_dbg(hsotg->dev,
3552 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3553 hsotg->flags.b.port_over_current_change = 0;
3554 break;
3555
3556 default:
3557 retval = -EINVAL;
3558 dev_err(hsotg->dev,
3559 "ClearPortFeature request %1xh unknown or unsupported\n",
3560 wvalue);
3561 }
3562 break;
3563
3564 case GetHubDescriptor:
3565 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3566 hub_desc = (struct usb_hub_descriptor *)buf;
3567 hub_desc->bDescLength = 9;
Sergei Shtylyova5dd0392015-03-29 01:36:28 +03003568 hub_desc->bDescriptorType = USB_DT_HUB;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003569 hub_desc->bNbrPorts = 1;
Sergei Shtylyov3d040de2015-01-19 01:54:15 +03003570 hub_desc->wHubCharacteristics =
3571 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
3572 HUB_CHAR_INDV_PORT_OCPM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003573 hub_desc->bPwrOn2PwrGood = 1;
3574 hub_desc->bHubContrCurrent = 0;
3575 hub_desc->u.hs.DeviceRemovable[0] = 0;
3576 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3577 break;
3578
3579 case GetHubStatus:
3580 dev_dbg(hsotg->dev, "GetHubStatus\n");
3581 memset(buf, 0, 4);
3582 break;
3583
3584 case GetPortStatus:
Paul Zimmermanb8313412013-05-24 16:32:12 -07003585 dev_vdbg(hsotg->dev,
3586 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3587 hsotg->flags.d32);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003588 if (!windex || windex > 1)
3589 goto error;
3590
3591 port_status = 0;
3592 if (hsotg->flags.b.port_connect_status_change)
3593 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3594 if (hsotg->flags.b.port_enable_change)
3595 port_status |= USB_PORT_STAT_C_ENABLE << 16;
3596 if (hsotg->flags.b.port_suspend_change)
3597 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3598 if (hsotg->flags.b.port_l1_change)
3599 port_status |= USB_PORT_STAT_C_L1 << 16;
3600 if (hsotg->flags.b.port_reset_change)
3601 port_status |= USB_PORT_STAT_C_RESET << 16;
3602 if (hsotg->flags.b.port_over_current_change) {
3603 dev_warn(hsotg->dev, "Overcurrent change detected\n");
3604 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3605 }
3606
3607 if (!hsotg->flags.b.port_connect_status) {
3608 /*
3609 * The port is disconnected, which means the core is
3610 * either in device mode or it soon will be. Just
3611 * return 0's for the remainder of the port status
3612 * since the port register can't be read if the core
3613 * is in device mode.
3614 */
3615 *(__le32 *)buf = cpu_to_le32(port_status);
3616 break;
3617 }
3618
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003619 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmermanb8313412013-05-24 16:32:12 -07003620 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003621
3622 if (hprt0 & HPRT0_CONNSTS)
3623 port_status |= USB_PORT_STAT_CONNECTION;
3624 if (hprt0 & HPRT0_ENA)
3625 port_status |= USB_PORT_STAT_ENABLE;
3626 if (hprt0 & HPRT0_SUSP)
3627 port_status |= USB_PORT_STAT_SUSPEND;
3628 if (hprt0 & HPRT0_OVRCURRACT)
3629 port_status |= USB_PORT_STAT_OVERCURRENT;
3630 if (hprt0 & HPRT0_RST)
3631 port_status |= USB_PORT_STAT_RESET;
3632 if (hprt0 & HPRT0_PWR)
3633 port_status |= USB_PORT_STAT_POWER;
3634
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02003635 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003636 if (speed == HPRT0_SPD_HIGH_SPEED)
3637 port_status |= USB_PORT_STAT_HIGH_SPEED;
3638 else if (speed == HPRT0_SPD_LOW_SPEED)
3639 port_status |= USB_PORT_STAT_LOW_SPEED;
3640
3641 if (hprt0 & HPRT0_TSTCTL_MASK)
3642 port_status |= USB_PORT_STAT_TEST;
3643 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3644
John Younbea8e862016-11-03 17:55:53 -07003645 if (hsotg->params.dma_desc_fs_enable) {
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003646 /*
3647 * Enable descriptor DMA only if a full speed
3648 * device is connected.
3649 */
3650 if (hsotg->new_connection &&
3651 ((port_status &
3652 (USB_PORT_STAT_CONNECTION |
3653 USB_PORT_STAT_HIGH_SPEED |
3654 USB_PORT_STAT_LOW_SPEED)) ==
3655 USB_PORT_STAT_CONNECTION)) {
3656 u32 hcfg;
3657
3658 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
John Youn95832c02017-01-23 14:57:26 -08003659 hsotg->params.dma_desc_enable = true;
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003660 hcfg = dwc2_readl(hsotg->regs + HCFG);
3661 hcfg |= HCFG_DESCDMA;
3662 dwc2_writel(hcfg, hsotg->regs + HCFG);
3663 hsotg->new_connection = false;
3664 }
3665 }
3666
Paul Zimmermanb8313412013-05-24 16:32:12 -07003667 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003668 *(__le32 *)buf = cpu_to_le32(port_status);
3669 break;
3670
3671 case SetHubFeature:
3672 dev_dbg(hsotg->dev, "SetHubFeature\n");
3673 /* No HUB features supported */
3674 break;
3675
3676 case SetPortFeature:
3677 dev_dbg(hsotg->dev, "SetPortFeature\n");
3678 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3679 goto error;
3680
3681 if (!hsotg->flags.b.port_connect_status) {
3682 /*
3683 * The port is disconnected, which means the core is
3684 * either in device mode or it soon will be. Just
3685 * return without doing anything since the port
3686 * register can't be written if the core is in device
3687 * mode.
3688 */
3689 break;
3690 }
3691
3692 switch (wvalue) {
3693 case USB_PORT_FEAT_SUSPEND:
3694 dev_dbg(hsotg->dev,
3695 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3696 if (windex != hsotg->otg_port)
3697 goto error;
3698 dwc2_port_suspend(hsotg, windex);
3699 break;
3700
3701 case USB_PORT_FEAT_POWER:
3702 dev_dbg(hsotg->dev,
3703 "SetPortFeature - USB_PORT_FEAT_POWER\n");
3704 hprt0 = dwc2_read_hprt0(hsotg);
3705 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003706 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003707 break;
3708
3709 case USB_PORT_FEAT_RESET:
3710 hprt0 = dwc2_read_hprt0(hsotg);
3711 dev_dbg(hsotg->dev,
3712 "SetPortFeature - USB_PORT_FEAT_RESET\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003713 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003714 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003715 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003716 /* ??? Original driver does this */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003717 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003718
3719 hprt0 = dwc2_read_hprt0(hsotg);
3720 /* Clear suspend bit if resetting from suspend state */
3721 hprt0 &= ~HPRT0_SUSP;
3722
3723 /*
3724 * When B-Host the Port reset bit is set in the Start
3725 * HCD Callback function, so that the reset is started
3726 * within 1ms of the HNP success interrupt
3727 */
3728 if (!dwc2_hcd_is_b_host(hsotg)) {
3729 hprt0 |= HPRT0_PWR | HPRT0_RST;
3730 dev_dbg(hsotg->dev,
3731 "In host mode, hprt0=%08x\n", hprt0);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003732 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003733 }
3734
3735 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003736 msleep(50);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003737 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003738 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003739 hsotg->lx_state = DWC2_L0; /* Now back to On state */
3740 break;
3741
3742 case USB_PORT_FEAT_INDICATOR:
3743 dev_dbg(hsotg->dev,
3744 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3745 /* Not supported */
3746 break;
3747
Jingwu Lin96d480e2015-04-29 22:09:17 +02003748 case USB_PORT_FEAT_TEST:
3749 hprt0 = dwc2_read_hprt0(hsotg);
3750 dev_dbg(hsotg->dev,
3751 "SetPortFeature - USB_PORT_FEAT_TEST\n");
3752 hprt0 &= ~HPRT0_TSTCTL_MASK;
3753 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003754 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Jingwu Lin96d480e2015-04-29 22:09:17 +02003755 break;
3756
Paul Zimmerman7359d482013-03-11 17:47:59 -07003757 default:
3758 retval = -EINVAL;
3759 dev_err(hsotg->dev,
3760 "SetPortFeature %1xh unknown or unsupported\n",
3761 wvalue);
3762 break;
3763 }
3764 break;
3765
3766 default:
3767error:
3768 retval = -EINVAL;
3769 dev_dbg(hsotg->dev,
3770 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3771 typereq, windex, wvalue);
3772 break;
3773 }
3774
3775 return retval;
3776}
3777
3778static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3779{
3780 int retval;
3781
Paul Zimmerman7359d482013-03-11 17:47:59 -07003782 if (port != 1)
3783 return -EINVAL;
3784
3785 retval = (hsotg->flags.b.port_connect_status_change ||
3786 hsotg->flags.b.port_reset_change ||
3787 hsotg->flags.b.port_enable_change ||
3788 hsotg->flags.b.port_suspend_change ||
3789 hsotg->flags.b.port_over_current_change);
3790
3791 if (retval) {
3792 dev_dbg(hsotg->dev,
3793 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3794 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
3795 hsotg->flags.b.port_connect_status_change);
3796 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
3797 hsotg->flags.b.port_reset_change);
3798 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
3799 hsotg->flags.b.port_enable_change);
3800 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
3801 hsotg->flags.b.port_suspend_change);
3802 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
3803 hsotg->flags.b.port_over_current_change);
3804 }
3805
3806 return retval;
3807}
3808
3809int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3810{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003811 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003812
3813#ifdef DWC2_DEBUG_SOF
3814 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003815 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003816#endif
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003817 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003818}
3819
Douglas Andersonfae4e822016-01-28 18:20:10 -08003820int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3821{
3822 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
3823 u32 hfir = dwc2_readl(hsotg->regs + HFIR);
3824 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3825 unsigned int us_per_frame;
3826 unsigned int frame_number;
3827 unsigned int remaining;
3828 unsigned int interval;
3829 unsigned int phy_clks;
3830
3831 /* High speed has 125 us per (micro) frame; others are 1 ms per */
3832 us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3833
3834 /* Extract fields */
3835 frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3836 remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3837 interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3838
3839 /*
3840 * Number of phy clocks since the last tick of the frame number after
3841 * "us" has passed.
3842 */
3843 phy_clks = (interval - remaining) +
3844 DIV_ROUND_UP(interval * us, us_per_frame);
3845
3846 return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3847}
3848
Paul Zimmerman7359d482013-03-11 17:47:59 -07003849int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3850{
Aldo Iljazi6bf2e2a2013-11-30 19:33:57 +02003851 return hsotg->op_state == OTG_STATE_B_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003852}
3853
3854static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3855 int iso_desc_count,
3856 gfp_t mem_flags)
3857{
3858 struct dwc2_hcd_urb *urb;
3859 u32 size = sizeof(*urb) + iso_desc_count *
3860 sizeof(struct dwc2_hcd_iso_packet_desc);
3861
3862 urb = kzalloc(size, mem_flags);
3863 if (urb)
3864 urb->packet_count = iso_desc_count;
3865 return urb;
3866}
3867
3868static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3869 struct dwc2_hcd_urb *urb, u8 dev_addr,
3870 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
3871{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02003872 if (dbg_perio() ||
3873 ep_type == USB_ENDPOINT_XFER_BULK ||
3874 ep_type == USB_ENDPOINT_XFER_CONTROL)
3875 dev_vdbg(hsotg->dev,
3876 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
3877 dev_addr, ep_num, ep_dir, ep_type, mps);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003878 urb->pipe_info.dev_addr = dev_addr;
3879 urb->pipe_info.ep_num = ep_num;
3880 urb->pipe_info.pipe_type = ep_type;
3881 urb->pipe_info.pipe_dir = ep_dir;
3882 urb->pipe_info.mps = mps;
3883}
3884
3885/*
3886 * NOTE: This function will be removed once the peripheral controller code
3887 * is integrated and the driver is stable
3888 */
3889void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3890{
3891#ifdef DEBUG
3892 struct dwc2_host_chan *chan;
3893 struct dwc2_hcd_urb *urb;
3894 struct dwc2_qtd *qtd;
3895 int num_channels;
3896 u32 np_tx_status;
3897 u32 p_tx_status;
3898 int i;
3899
John Younbea8e862016-11-03 17:55:53 -07003900 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003901 dev_dbg(hsotg->dev, "\n");
3902 dev_dbg(hsotg->dev,
3903 "************************************************************\n");
3904 dev_dbg(hsotg->dev, "HCD State:\n");
3905 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
3906
3907 for (i = 0; i < num_channels; i++) {
3908 chan = hsotg->hc_ptr_array[i];
3909 dev_dbg(hsotg->dev, " Channel %d:\n", i);
3910 dev_dbg(hsotg->dev,
3911 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3912 chan->dev_addr, chan->ep_num, chan->ep_is_in);
3913 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
3914 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
3915 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
3916 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
3917 chan->data_pid_start);
3918 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
3919 dev_dbg(hsotg->dev, " xfer_started: %d\n",
3920 chan->xfer_started);
3921 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
3922 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
3923 (unsigned long)chan->xfer_dma);
3924 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
3925 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
3926 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
3927 chan->halt_on_queue);
3928 dev_dbg(hsotg->dev, " halt_pending: %d\n",
3929 chan->halt_pending);
3930 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
3931 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
3932 dev_dbg(hsotg->dev, " complete_split: %d\n",
3933 chan->complete_split);
3934 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
3935 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
3936 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
3937 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
3938 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
3939
3940 if (chan->xfer_started) {
3941 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3942
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003943 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3944 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
3945 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
3946 hcint = dwc2_readl(hsotg->regs + HCINT(i));
3947 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003948 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
3949 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
3950 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
3951 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
3952 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
3953 }
3954
3955 if (!(chan->xfer_started && chan->qh))
3956 continue;
3957
3958 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3959 if (!qtd->in_process)
3960 break;
3961 urb = qtd->urb;
3962 dev_dbg(hsotg->dev, " URB Info:\n");
3963 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
3964 qtd, urb);
3965 if (urb) {
3966 dev_dbg(hsotg->dev,
3967 " Dev: %d, EP: %d %s\n",
3968 dwc2_hcd_get_dev_addr(&urb->pipe_info),
3969 dwc2_hcd_get_ep_num(&urb->pipe_info),
3970 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3971 "IN" : "OUT");
3972 dev_dbg(hsotg->dev,
3973 " Max packet size: %d\n",
3974 dwc2_hcd_get_mps(&urb->pipe_info));
3975 dev_dbg(hsotg->dev,
3976 " transfer_buffer: %p\n",
3977 urb->buf);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07003978 dev_dbg(hsotg->dev,
3979 " transfer_dma: %08lx\n",
3980 (unsigned long)urb->dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003981 dev_dbg(hsotg->dev,
3982 " transfer_buffer_length: %d\n",
3983 urb->length);
3984 dev_dbg(hsotg->dev, " actual_length: %d\n",
3985 urb->actual_length);
3986 }
3987 }
3988 }
3989
3990 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
3991 hsotg->non_periodic_channels);
3992 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
3993 hsotg->periodic_channels);
3994 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003995 np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003996 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003997 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003998 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003999 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004000 p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004001 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004002 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004003 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004004 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004005 dwc2_dump_global_registers(hsotg);
4006 dwc2_dump_host_registers(hsotg);
4007 dev_dbg(hsotg->dev,
4008 "************************************************************\n");
4009 dev_dbg(hsotg->dev, "\n");
4010#endif
4011}
4012
Paul Zimmerman7359d482013-03-11 17:47:59 -07004013struct wrapper_priv_data {
4014 struct dwc2_hsotg *hsotg;
4015};
4016
4017/* Gets the dwc2_hsotg from a usb_hcd */
4018static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4019{
4020 struct wrapper_priv_data *p;
4021
John Youn9da51972017-01-17 20:30:27 -08004022 p = (struct wrapper_priv_data *)&hcd->hcd_priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004023 return p->hsotg;
4024}
4025
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004026/**
4027 * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
4028 *
4029 * This will get the dwc2_tt structure (and ttport) associated with the given
4030 * context (which is really just a struct urb pointer).
4031 *
4032 * The first time this is called for a given TT we allocate memory for our
4033 * structure. When everyone is done and has called dwc2_host_put_tt_info()
4034 * then the refcount for the structure will go to 0 and we'll free it.
4035 *
4036 * @hsotg: The HCD state structure for the DWC OTG controller.
4037 * @qh: The QH structure.
4038 * @context: The priv pointer from a struct dwc2_hcd_urb.
4039 * @mem_flags: Flags for allocating memory.
4040 * @ttport: We'll return this device's port number here. That's used to
4041 * reference into the bitmap if we're on a multi_tt hub.
4042 *
4043 * Return: a pointer to a struct dwc2_tt. Don't forget to call
4044 * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
4045 */
4046
4047struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
4048 gfp_t mem_flags, int *ttport)
4049{
4050 struct urb *urb = context;
4051 struct dwc2_tt *dwc_tt = NULL;
4052
4053 if (urb->dev->tt) {
4054 *ttport = urb->dev->ttport;
4055
4056 dwc_tt = urb->dev->tt->hcpriv;
John Youn9da51972017-01-17 20:30:27 -08004057 if (!dwc_tt) {
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004058 size_t bitmap_size;
4059
4060 /*
4061 * For single_tt we need one schedule. For multi_tt
4062 * we need one per port.
4063 */
4064 bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
4065 sizeof(dwc_tt->periodic_bitmaps[0]);
4066 if (urb->dev->tt->multi)
4067 bitmap_size *= urb->dev->tt->hub->maxchild;
4068
4069 dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
4070 mem_flags);
John Youn9da51972017-01-17 20:30:27 -08004071 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004072 return NULL;
4073
4074 dwc_tt->usb_tt = urb->dev->tt;
4075 dwc_tt->usb_tt->hcpriv = dwc_tt;
4076 }
4077
4078 dwc_tt->refcount++;
4079 }
4080
4081 return dwc_tt;
4082}
4083
4084/**
4085 * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
4086 *
4087 * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
4088 * of the structure are done.
4089 *
4090 * It's OK to call this with NULL.
4091 *
4092 * @hsotg: The HCD state structure for the DWC OTG controller.
4093 * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
4094 */
4095void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
4096{
4097 /* Model kfree and make put of NULL a no-op */
John Youn9da51972017-01-17 20:30:27 -08004098 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004099 return;
4100
4101 WARN_ON(dwc_tt->refcount < 1);
4102
4103 dwc_tt->refcount--;
4104 if (!dwc_tt->refcount) {
4105 dwc_tt->usb_tt->hcpriv = NULL;
4106 kfree(dwc_tt);
4107 }
4108}
4109
Paul Zimmerman7359d482013-03-11 17:47:59 -07004110int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4111{
4112 struct urb *urb = context;
4113
4114 return urb->dev->speed;
4115}
4116
4117static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4118 struct urb *urb)
4119{
4120 struct usb_bus *bus = hcd_to_bus(hcd);
4121
4122 if (urb->interval)
4123 bus->bandwidth_allocated += bw / urb->interval;
4124 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4125 bus->bandwidth_isoc_reqs++;
4126 else
4127 bus->bandwidth_int_reqs++;
4128}
4129
4130static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4131 struct urb *urb)
4132{
4133 struct usb_bus *bus = hcd_to_bus(hcd);
4134
4135 if (urb->interval)
4136 bus->bandwidth_allocated -= bw / urb->interval;
4137 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4138 bus->bandwidth_isoc_reqs--;
4139 else
4140 bus->bandwidth_int_reqs--;
4141}
4142
4143/*
4144 * Sets the final status of an URB and returns it to the upper layer. Any
4145 * required cleanup of the URB is performed.
4146 *
4147 * Must be called with interrupt disabled and spinlock held
4148 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004149void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4150 int status)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004151{
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004152 struct urb *urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004153 int i;
4154
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004155 if (!qtd) {
4156 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4157 return;
4158 }
4159
4160 if (!qtd->urb) {
4161 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4162 return;
4163 }
4164
4165 urb = qtd->urb->priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004166 if (!urb) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004167 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004168 return;
4169 }
4170
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004171 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004172
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004173 if (dbg_urb(urb))
4174 dev_vdbg(hsotg->dev,
4175 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4176 __func__, urb, usb_pipedevice(urb->pipe),
4177 usb_pipeendpoint(urb->pipe),
4178 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4179 urb->actual_length);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004180
Paul Zimmerman7359d482013-03-11 17:47:59 -07004181 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004182 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004183 for (i = 0; i < urb->number_of_packets; ++i) {
4184 urb->iso_frame_desc[i].actual_length =
4185 dwc2_hcd_urb_get_iso_desc_actual_length(
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004186 qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004187 urb->iso_frame_desc[i].status =
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004188 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004189 }
4190 }
4191
Gregory Herrerofe9b1772015-09-22 15:16:51 +02004192 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4193 for (i = 0; i < urb->number_of_packets; i++)
4194 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4195 i, urb->iso_frame_desc[i].status);
4196 }
4197
Paul Zimmerman7359d482013-03-11 17:47:59 -07004198 urb->status = status;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004199 if (!status) {
4200 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4201 urb->actual_length < urb->transfer_buffer_length)
4202 urb->status = -EREMOTEIO;
4203 }
4204
4205 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4206 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4207 struct usb_host_endpoint *ep = urb->ep;
4208
4209 if (ep)
4210 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4211 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4212 urb);
4213 }
4214
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004215 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004216 urb->hcpriv = NULL;
4217 kfree(qtd->urb);
4218 qtd->urb = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004219
Paul Zimmerman7359d482013-03-11 17:47:59 -07004220 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004221}
4222
4223/*
4224 * Work queue function for starting the HCD when A-Cable is connected
4225 */
4226static void dwc2_hcd_start_func(struct work_struct *work)
4227{
4228 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4229 start_work.work);
4230
4231 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4232 dwc2_host_start(hsotg);
4233}
4234
4235/*
4236 * Reset work queue function
4237 */
4238static void dwc2_hcd_reset_func(struct work_struct *work)
4239{
4240 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4241 reset_work.work);
Douglas Anderson4a065c72015-11-20 09:06:27 -08004242 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004243 u32 hprt0;
4244
4245 dev_dbg(hsotg->dev, "USB RESET function called\n");
Douglas Anderson4a065c72015-11-20 09:06:27 -08004246
4247 spin_lock_irqsave(&hsotg->lock, flags);
4248
Paul Zimmerman7359d482013-03-11 17:47:59 -07004249 hprt0 = dwc2_read_hprt0(hsotg);
4250 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004251 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004252 hsotg->flags.b.port_reset_change = 1;
Douglas Anderson4a065c72015-11-20 09:06:27 -08004253
4254 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004255}
4256
4257/*
4258 * =========================================================================
4259 * Linux HC Driver Functions
4260 * =========================================================================
4261 */
4262
4263/*
4264 * Initializes the DWC_otg controller and its root hub and prepares it for host
4265 * mode operation. Activates the root port. Returns 0 on success and a negative
4266 * error code on failure.
4267 */
4268static int _dwc2_hcd_start(struct usb_hcd *hcd)
4269{
4270 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4271 struct usb_bus *bus = hcd_to_bus(hcd);
4272 unsigned long flags;
4273
4274 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4275
4276 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero31927b62015-09-22 15:16:41 +02004277 hsotg->lx_state = DWC2_L0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004278 hcd->state = HC_STATE_RUNNING;
Gregory Herrero31927b62015-09-22 15:16:41 +02004279 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004280
4281 if (dwc2_is_device_mode(hsotg)) {
4282 spin_unlock_irqrestore(&hsotg->lock, flags);
4283 return 0; /* why 0 ?? */
4284 }
4285
4286 dwc2_hcd_reinit(hsotg);
4287
4288 /* Initialize and connect root hub if one is not already attached */
4289 if (bus->root_hub) {
4290 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4291 /* Inform the HUB driver to resume */
4292 usb_hcd_resume_root_hub(hcd);
4293 }
4294
4295 spin_unlock_irqrestore(&hsotg->lock, flags);
4296 return 0;
4297}
4298
4299/*
4300 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4301 * stopped.
4302 */
4303static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4304{
4305 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4306 unsigned long flags;
4307
Gregory Herrero5bbf6ce2015-09-22 15:16:48 +02004308 /* Turn off all host-specific interrupts */
4309 dwc2_disable_host_interrupts(hsotg);
4310
Gregory Herrero091473a2015-09-22 15:16:46 +02004311 /* Wait for interrupt processing to finish */
4312 synchronize_irq(hcd->irq);
4313
Paul Zimmerman7359d482013-03-11 17:47:59 -07004314 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero091473a2015-09-22 15:16:46 +02004315 /* Ensure hcd is disconnected */
Douglas Anderson6a659532015-11-19 13:23:14 -08004316 dwc2_hcd_disconnect(hsotg, true);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004317 dwc2_hcd_stop(hsotg);
Gregory Herrero31927b62015-09-22 15:16:41 +02004318 hsotg->lx_state = DWC2_L3;
4319 hcd->state = HC_STATE_HALT;
4320 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004321 spin_unlock_irqrestore(&hsotg->lock, flags);
4322
4323 usleep_range(1000, 3000);
4324}
4325
Gregory Herrero99a65792015-04-29 22:09:13 +02004326static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
4327{
4328 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004329 unsigned long flags;
4330 int ret = 0;
4331 u32 hprt0;
Gregory Herrero99a65792015-04-29 22:09:13 +02004332
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004333 spin_lock_irqsave(&hsotg->lock, flags);
4334
Meng Dongyangf367b722017-08-09 10:34:09 +08004335 if (dwc2_is_device_mode(hsotg))
4336 goto unlock;
4337
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004338 if (hsotg->lx_state != DWC2_L0)
4339 goto unlock;
4340
4341 if (!HCD_HW_ACCESSIBLE(hcd))
4342 goto unlock;
4343
John Stultz866932e2017-01-09 13:10:24 -08004344 if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4345 goto unlock;
4346
John Younbea8e862016-11-03 17:55:53 -07004347 if (!hsotg->params.hibernation)
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004348 goto skip_power_saving;
4349
4350 /*
4351 * Drive USB suspend and disable port Power
4352 * if usb bus is not suspended.
4353 */
4354 if (!hsotg->bus_suspended) {
4355 hprt0 = dwc2_read_hprt0(hsotg);
4356 hprt0 |= HPRT0_SUSP;
4357 hprt0 &= ~HPRT0_PWR;
4358 dwc2_writel(hprt0, hsotg->regs + HPRT0);
4359 }
4360
4361 /* Enter hibernation */
4362 ret = dwc2_enter_hibernation(hsotg);
4363 if (ret) {
4364 if (ret != -ENOTSUPP)
4365 dev_err(hsotg->dev,
4366 "enter hibernation failed\n");
4367 goto skip_power_saving;
4368 }
4369
4370 /* Ask phy to be suspended */
4371 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4372 spin_unlock_irqrestore(&hsotg->lock, flags);
4373 usb_phy_set_suspend(hsotg->uphy, true);
4374 spin_lock_irqsave(&hsotg->lock, flags);
4375 }
4376
4377 /* After entering hibernation, hardware is no more accessible */
4378 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4379
4380skip_power_saving:
Gregory Herrero99a65792015-04-29 22:09:13 +02004381 hsotg->lx_state = DWC2_L2;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004382unlock:
4383 spin_unlock_irqrestore(&hsotg->lock, flags);
4384
4385 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004386}
4387
4388static int _dwc2_hcd_resume(struct usb_hcd *hcd)
4389{
4390 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004391 unsigned long flags;
4392 int ret = 0;
4393
4394 spin_lock_irqsave(&hsotg->lock, flags);
4395
Meng Dongyangf367b722017-08-09 10:34:09 +08004396 if (dwc2_is_device_mode(hsotg))
4397 goto unlock;
4398
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004399 if (hsotg->lx_state != DWC2_L2)
4400 goto unlock;
4401
John Younbea8e862016-11-03 17:55:53 -07004402 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004403 hsotg->lx_state = DWC2_L0;
4404 goto unlock;
4405 }
4406
4407 /*
4408 * Set HW accessible bit before powering on the controller
4409 * since an interrupt may rise.
4410 */
4411 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4412
4413 /*
4414 * Enable power if not already done.
4415 * This must not be spinlocked since duration
4416 * of this call is unknown.
4417 */
4418 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4419 spin_unlock_irqrestore(&hsotg->lock, flags);
4420 usb_phy_set_suspend(hsotg->uphy, false);
4421 spin_lock_irqsave(&hsotg->lock, flags);
4422 }
4423
4424 /* Exit hibernation */
4425 ret = dwc2_exit_hibernation(hsotg, true);
4426 if (ret && (ret != -ENOTSUPP))
4427 dev_err(hsotg->dev, "exit hibernation failed\n");
Gregory Herrero99a65792015-04-29 22:09:13 +02004428
4429 hsotg->lx_state = DWC2_L0;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004430
4431 spin_unlock_irqrestore(&hsotg->lock, flags);
4432
4433 if (hsotg->bus_suspended) {
4434 spin_lock_irqsave(&hsotg->lock, flags);
4435 hsotg->flags.b.port_suspend_change = 1;
4436 spin_unlock_irqrestore(&hsotg->lock, flags);
4437 dwc2_port_resume(hsotg);
4438 } else {
Gregory Herrero5634e012015-09-22 15:16:50 +02004439 /* Wait for controller to correctly update D+/D- level */
4440 usleep_range(3000, 5000);
4441
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004442 /*
4443 * Clear Port Enable and Port Status changes.
4444 * Enable Port Power.
4445 */
4446 dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
4447 HPRT0_ENACHG, hsotg->regs + HPRT0);
4448 /* Wait for controller to detect Port Connect */
Gregory Herrero5634e012015-09-22 15:16:50 +02004449 usleep_range(5000, 7000);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004450 }
4451
4452 return ret;
4453unlock:
4454 spin_unlock_irqrestore(&hsotg->lock, flags);
4455
4456 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004457}
4458
Paul Zimmerman7359d482013-03-11 17:47:59 -07004459/* Returns the current frame number */
4460static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4461{
4462 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4463
4464 return dwc2_hcd_get_frame_number(hsotg);
4465}
4466
4467static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4468 char *fn_name)
4469{
4470#ifdef VERBOSE_DEBUG
4471 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Nicholas Mc Guireefe357f2017-01-12 17:33:26 +01004472 char *pipetype = NULL;
4473 char *speed = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004474
4475 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4476 dev_vdbg(hsotg->dev, " Device address: %d\n",
4477 usb_pipedevice(urb->pipe));
4478 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
4479 usb_pipeendpoint(urb->pipe),
4480 usb_pipein(urb->pipe) ? "IN" : "OUT");
4481
4482 switch (usb_pipetype(urb->pipe)) {
4483 case PIPE_CONTROL:
4484 pipetype = "CONTROL";
4485 break;
4486 case PIPE_BULK:
4487 pipetype = "BULK";
4488 break;
4489 case PIPE_INTERRUPT:
4490 pipetype = "INTERRUPT";
4491 break;
4492 case PIPE_ISOCHRONOUS:
4493 pipetype = "ISOCHRONOUS";
4494 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004495 }
4496
4497 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
4498 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4499 "IN" : "OUT");
4500
4501 switch (urb->dev->speed) {
4502 case USB_SPEED_HIGH:
4503 speed = "HIGH";
4504 break;
4505 case USB_SPEED_FULL:
4506 speed = "FULL";
4507 break;
4508 case USB_SPEED_LOW:
4509 speed = "LOW";
4510 break;
4511 default:
4512 speed = "UNKNOWN";
4513 break;
4514 }
4515
4516 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
4517 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
4518 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
4519 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
4520 urb->transfer_buffer_length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07004521 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
4522 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4523 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
4524 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004525 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
4526
4527 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4528 int i;
4529
4530 for (i = 0; i < urb->number_of_packets; i++) {
4531 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
4532 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
4533 urb->iso_frame_desc[i].offset,
4534 urb->iso_frame_desc[i].length);
4535 }
4536 }
4537#endif
4538}
4539
4540/*
4541 * Starts processing a USB transfer request specified by a USB Request Block
4542 * (URB). mem_flags indicates the type of memory allocation to use while
4543 * processing this URB.
4544 */
4545static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4546 gfp_t mem_flags)
4547{
4548 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4549 struct usb_host_endpoint *ep = urb->ep;
4550 struct dwc2_hcd_urb *dwc2_urb;
4551 int i;
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004552 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004553 int alloc_bandwidth = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004554 u8 ep_type = 0;
4555 u32 tflags = 0;
4556 void *buf;
4557 unsigned long flags;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004558 struct dwc2_qh *qh;
4559 bool qh_allocated = false;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004560 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004561
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004562 if (dbg_urb(urb)) {
4563 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4564 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4565 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004566
John Youn9da51972017-01-17 20:30:27 -08004567 if (!ep)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004568 return -EINVAL;
4569
4570 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4571 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4572 spin_lock_irqsave(&hsotg->lock, flags);
4573 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4574 alloc_bandwidth = 1;
4575 spin_unlock_irqrestore(&hsotg->lock, flags);
4576 }
4577
4578 switch (usb_pipetype(urb->pipe)) {
4579 case PIPE_CONTROL:
4580 ep_type = USB_ENDPOINT_XFER_CONTROL;
4581 break;
4582 case PIPE_ISOCHRONOUS:
4583 ep_type = USB_ENDPOINT_XFER_ISOC;
4584 break;
4585 case PIPE_BULK:
4586 ep_type = USB_ENDPOINT_XFER_BULK;
4587 break;
4588 case PIPE_INTERRUPT:
4589 ep_type = USB_ENDPOINT_XFER_INT;
4590 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004591 }
4592
4593 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4594 mem_flags);
4595 if (!dwc2_urb)
4596 return -ENOMEM;
4597
4598 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4599 usb_pipeendpoint(urb->pipe), ep_type,
4600 usb_pipein(urb->pipe),
4601 usb_maxpacket(urb->dev, urb->pipe,
4602 !(usb_pipein(urb->pipe))));
4603
4604 buf = urb->transfer_buffer;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004605
Paul Zimmerman7359d482013-03-11 17:47:59 -07004606 if (hcd->self.uses_dma) {
Paul Zimmerman25a49442013-07-13 14:53:53 -07004607 if (!buf && (urb->transfer_dma & 3)) {
4608 dev_err(hsotg->dev,
4609 "%s: unaligned transfer with no transfer_buffer",
4610 __func__);
4611 retval = -EINVAL;
Gregory Herrero33ad2612015-04-29 22:09:15 +02004612 goto fail0;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004613 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004614 }
4615
4616 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4617 tflags |= URB_GIVEBACK_ASAP;
4618 if (urb->transfer_flags & URB_ZERO_PACKET)
4619 tflags |= URB_SEND_ZERO_PACKET;
4620
4621 dwc2_urb->priv = urb;
4622 dwc2_urb->buf = buf;
4623 dwc2_urb->dma = urb->transfer_dma;
4624 dwc2_urb->length = urb->transfer_buffer_length;
4625 dwc2_urb->setup_packet = urb->setup_packet;
4626 dwc2_urb->setup_dma = urb->setup_dma;
4627 dwc2_urb->flags = tflags;
4628 dwc2_urb->interval = urb->interval;
4629 dwc2_urb->status = -EINPROGRESS;
4630
4631 for (i = 0; i < urb->number_of_packets; ++i)
4632 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4633 urb->iso_frame_desc[i].offset,
4634 urb->iso_frame_desc[i].length);
4635
4636 urb->hcpriv = dwc2_urb;
John Youn9da51972017-01-17 20:30:27 -08004637 qh = (struct dwc2_qh *)ep->hcpriv;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004638 /* Create QH for the endpoint if it doesn't exist */
4639 if (!qh) {
4640 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4641 if (!qh) {
4642 retval = -ENOMEM;
4643 goto fail0;
4644 }
4645 ep->hcpriv = qh;
4646 qh_allocated = true;
4647 }
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004648
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004649 qtd = kzalloc(sizeof(*qtd), mem_flags);
4650 if (!qtd) {
4651 retval = -ENOMEM;
4652 goto fail1;
4653 }
4654
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004655 spin_lock_irqsave(&hsotg->lock, flags);
4656 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004657 if (retval)
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004658 goto fail2;
4659
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004660 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4661 if (retval)
4662 goto fail3;
4663
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004664 if (alloc_bandwidth) {
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004665 dwc2_allocate_bus_bandwidth(hcd,
4666 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4667 urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004668 }
4669
Gregory Herrero33ad2612015-04-29 22:09:15 +02004670 spin_unlock_irqrestore(&hsotg->lock, flags);
4671
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004672 return 0;
4673
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004674fail3:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004675 dwc2_urb->priv = NULL;
4676 usb_hcd_unlink_urb_from_ep(hcd, urb);
Douglas Anderson16e80212016-01-28 18:19:55 -08004677 if (qh_allocated && qh->channel && qh->channel->qh == qh)
4678 qh->channel->qh = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004679fail2:
Gregory Herrero33ad2612015-04-29 22:09:15 +02004680 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004681 urb->hcpriv = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004682 kfree(qtd);
Vardan Mikayelyanb0d659022016-04-27 20:20:51 -07004683 qtd = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004684fail1:
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004685 if (qh_allocated) {
4686 struct dwc2_qtd *qtd2, *qtd2_tmp;
4687
4688 ep->hcpriv = NULL;
4689 dwc2_hcd_qh_unlink(hsotg, qh);
4690 /* Free each QTD in the QH's QTD list */
4691 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
John Youn9da51972017-01-17 20:30:27 -08004692 qtd_list_entry)
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004693 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4694 dwc2_hcd_qh_free(hsotg, qh);
4695 }
Gregory Herrero33ad2612015-04-29 22:09:15 +02004696fail0:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004697 kfree(dwc2_urb);
4698
Paul Zimmerman7359d482013-03-11 17:47:59 -07004699 return retval;
4700}
4701
4702/*
4703 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4704 */
4705static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4706 int status)
4707{
4708 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004709 int rc;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004710 unsigned long flags;
4711
4712 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4713 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4714
4715 spin_lock_irqsave(&hsotg->lock, flags);
4716
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004717 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4718 if (rc)
4719 goto out;
4720
Paul Zimmerman7359d482013-03-11 17:47:59 -07004721 if (!urb->hcpriv) {
4722 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4723 goto out;
4724 }
4725
4726 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4727
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004728 usb_hcd_unlink_urb_from_ep(hcd, urb);
4729
Paul Zimmerman7359d482013-03-11 17:47:59 -07004730 kfree(urb->hcpriv);
4731 urb->hcpriv = NULL;
4732
4733 /* Higher layer software sets URB status */
4734 spin_unlock(&hsotg->lock);
4735 usb_hcd_giveback_urb(hcd, urb, status);
4736 spin_lock(&hsotg->lock);
4737
4738 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4739 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
4740out:
4741 spin_unlock_irqrestore(&hsotg->lock, flags);
4742
4743 return rc;
4744}
4745
4746/*
4747 * Frees resources in the DWC_otg controller related to a given endpoint. Also
4748 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4749 * must already be dequeued.
4750 */
4751static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4752 struct usb_host_endpoint *ep)
4753{
4754 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4755
4756 dev_dbg(hsotg->dev,
4757 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4758 ep->desc.bEndpointAddress, ep->hcpriv);
4759 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4760}
4761
4762/*
4763 * Resets endpoint specific parameter values, in current version used to reset
4764 * the data toggle (as a WA). This function can be called from usb_clear_halt
4765 * routine.
4766 */
4767static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4768 struct usb_host_endpoint *ep)
4769{
4770 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004771 unsigned long flags;
4772
4773 dev_dbg(hsotg->dev,
4774 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4775 ep->desc.bEndpointAddress);
4776
Paul Zimmerman7359d482013-03-11 17:47:59 -07004777 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004778 dwc2_hcd_endpoint_reset(hsotg, ep);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004779 spin_unlock_irqrestore(&hsotg->lock, flags);
4780}
4781
4782/*
4783 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4784 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4785 * interrupt.
4786 *
4787 * This function is called by the USB core when an interrupt occurs
4788 */
4789static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4790{
4791 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004792
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02004793 return dwc2_handle_hcd_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004794}
4795
4796/*
4797 * Creates Status Change bitmap for the root hub and root port. The bitmap is
4798 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4799 * is the status change indicator for the single root port. Returns 1 if either
4800 * change indicator is 1, otherwise returns 0.
4801 */
4802static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4803{
4804 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4805
4806 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4807 return buf[0] != 0;
4808}
4809
4810/* Handles hub class-specific requests */
4811static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4812 u16 windex, char *buf, u16 wlength)
4813{
4814 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4815 wvalue, windex, buf, wlength);
4816 return retval;
4817}
4818
4819/* Handles hub TT buffer clear completions */
4820static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4821 struct usb_host_endpoint *ep)
4822{
4823 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4824 struct dwc2_qh *qh;
4825 unsigned long flags;
4826
4827 qh = ep->hcpriv;
4828 if (!qh)
4829 return;
4830
4831 spin_lock_irqsave(&hsotg->lock, flags);
4832 qh->tt_buffer_dirty = 0;
4833
4834 if (hsotg->flags.b.port_connect_status)
4835 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4836
4837 spin_unlock_irqrestore(&hsotg->lock, flags);
4838}
4839
Chen Yuca8b0332017-01-23 15:00:18 -08004840/*
4841 * HPRT0_SPD_HIGH_SPEED: high speed
4842 * HPRT0_SPD_FULL_SPEED: full speed
4843 */
4844static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4845{
4846 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4847
4848 if (hsotg->params.speed == speed)
4849 return;
4850
4851 hsotg->params.speed = speed;
4852 queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4853}
4854
4855static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4856{
4857 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4858
4859 if (!hsotg->params.change_speed_quirk)
4860 return;
4861
4862 /*
4863 * On removal, set speed to default high-speed.
4864 */
4865 if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4866 udev->parent->speed < USB_SPEED_HIGH) {
4867 dev_info(hsotg->dev, "Set speed to default high-speed\n");
4868 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4869 }
4870}
4871
4872static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4873{
4874 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4875
4876 if (!hsotg->params.change_speed_quirk)
4877 return 0;
4878
4879 if (udev->speed == USB_SPEED_HIGH) {
4880 dev_info(hsotg->dev, "Set speed to high-speed\n");
4881 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4882 } else if ((udev->speed == USB_SPEED_FULL ||
4883 udev->speed == USB_SPEED_LOW)) {
4884 /*
4885 * Change speed setting to full-speed if there's
4886 * a full-speed or low-speed device plugged in.
4887 */
4888 dev_info(hsotg->dev, "Set speed to full-speed\n");
4889 dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4890 }
4891
4892 return 0;
4893}
4894
Paul Zimmerman7359d482013-03-11 17:47:59 -07004895static struct hc_driver dwc2_hc_driver = {
4896 .description = "dwc2_hsotg",
4897 .product_desc = "DWC OTG Controller",
4898 .hcd_priv_size = sizeof(struct wrapper_priv_data),
4899
4900 .irq = _dwc2_hcd_irq,
Douglas Anderson8add17c2016-01-28 18:20:00 -08004901 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004902
4903 .start = _dwc2_hcd_start,
4904 .stop = _dwc2_hcd_stop,
4905 .urb_enqueue = _dwc2_hcd_urb_enqueue,
4906 .urb_dequeue = _dwc2_hcd_urb_dequeue,
4907 .endpoint_disable = _dwc2_hcd_endpoint_disable,
4908 .endpoint_reset = _dwc2_hcd_endpoint_reset,
4909 .get_frame_number = _dwc2_hcd_get_frame_number,
4910
4911 .hub_status_data = _dwc2_hcd_hub_status_data,
4912 .hub_control = _dwc2_hcd_hub_control,
4913 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
Gregory Herrero99a65792015-04-29 22:09:13 +02004914
4915 .bus_suspend = _dwc2_hcd_suspend,
4916 .bus_resume = _dwc2_hcd_resume,
Douglas Anderson3bc04e22016-01-28 18:19:53 -08004917
4918 .map_urb_for_dma = dwc2_map_urb_for_dma,
4919 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004920};
4921
4922/*
4923 * Frees secondary storage associated with the dwc2_hsotg structure contained
4924 * in the struct usb_hcd field
4925 */
4926static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
4927{
4928 u32 ahbcfg;
4929 u32 dctl;
4930 int i;
4931
4932 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
4933
4934 /* Free memory for QH/QTD lists */
4935 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08004936 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004937 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
4938 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
4939 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
4940 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
4941 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
4942
4943 /* Free memory for the host channels */
4944 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
4945 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
4946
John Youn9da51972017-01-17 20:30:27 -08004947 if (chan) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07004948 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
4949 i, chan);
4950 hsotg->hc_ptr_array[i] = NULL;
4951 kfree(chan);
4952 }
4953 }
4954
John Youn95832c02017-01-23 14:57:26 -08004955 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07004956 if (hsotg->status_buf) {
4957 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
4958 hsotg->status_buf,
4959 hsotg->status_buf_dma);
4960 hsotg->status_buf = NULL;
4961 }
4962 } else {
4963 kfree(hsotg->status_buf);
4964 hsotg->status_buf = NULL;
4965 }
4966
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004967 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004968
4969 /* Disable all interrupts */
4970 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004971 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
4972 dwc2_writel(0, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004973
Matthijs Kooijman9badec22013-08-30 18:45:21 +02004974 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004975 dctl = dwc2_readl(hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004976 dctl |= DCTL_SFTDISCON;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004977 dwc2_writel(dctl, hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004978 }
4979
4980 if (hsotg->wq_otg) {
4981 if (!cancel_work_sync(&hsotg->wf_otg))
4982 flush_workqueue(hsotg->wq_otg);
4983 destroy_workqueue(hsotg->wq_otg);
4984 }
4985
Paul Zimmerman7359d482013-03-11 17:47:59 -07004986 del_timer(&hsotg->wkp_timer);
4987}
4988
4989static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
4990{
4991 /* Turn off all host-specific interrupts */
4992 dwc2_disable_host_interrupts(hsotg);
4993
4994 dwc2_hcd_free(hsotg);
4995}
4996
Matthijs Kooijman8284f932013-04-11 18:43:47 +02004997/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07004998 * Initializes the HCD. This function allocates memory for and initializes the
4999 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5000 * USB bus with the core and calls the hc_driver->start() function. It returns
5001 * a negative error on failure.
5002 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005003int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005004{
Heiner Kallweit348becd2017-01-25 23:10:51 +01005005 struct platform_device *pdev = to_platform_device(hsotg->dev);
5006 struct resource *res;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005007 struct usb_hcd *hcd;
5008 struct dwc2_host_chan *channel;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005009 u32 hcfg;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005010 int i, num_channels;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005011 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005012
Dinh Nguyenf5500ec2014-11-11 11:13:39 -06005013 if (usb_disabled())
5014 return -ENODEV;
5015
Paul Zimmermane62662c2013-03-25 17:03:35 -07005016 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005017
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005018 retval = -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005019
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005020 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005021 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005022
5023#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5024 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
5025 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5026 if (!hsotg->frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005027 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005028 hsotg->last_frame_num_array = kzalloc(
5029 sizeof(*hsotg->last_frame_num_array) *
5030 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5031 if (!hsotg->last_frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005032 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005033#endif
Douglas Anderson483bb252016-01-28 18:20:07 -08005034 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005035
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005036 /* Check if the bus driver or platform code has setup a dma_mask */
John Youn95832c02017-01-23 14:57:26 -08005037 if (hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08005038 !hsotg->dev->dma_mask) {
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005039 dev_warn(hsotg->dev,
5040 "dma_mask not set, disabling DMA\n");
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01005041 hsotg->params.host_dma = false;
John Youn95832c02017-01-23 14:57:26 -08005042 hsotg->params.dma_desc_enable = false;
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005043 }
5044
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005045 /* Set device flags indicating whether the HCD supports DMA */
John Youn95832c02017-01-23 14:57:26 -08005046 if (hsotg->params.host_dma) {
Paul Zimmerman30885312013-05-24 16:27:56 -07005047 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5048 dev_warn(hsotg->dev, "can't set DMA mask\n");
Paul Zimmerman25a49442013-07-13 14:53:53 -07005049 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5050 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005051 }
5052
Chen Yuca8b0332017-01-23 15:00:18 -08005053 if (hsotg->params.change_speed_quirk) {
5054 dwc2_hc_driver.free_dev = dwc2_free_dev;
5055 dwc2_hc_driver.reset_device = dwc2_reset_device;
5056 }
5057
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005058 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5059 if (!hcd)
5060 goto error1;
5061
John Youn95832c02017-01-23 14:57:26 -08005062 if (!hsotg->params.host_dma)
Matthijs Kooijman7de76ee2013-07-19 11:34:23 +02005063 hcd->self.uses_dma = 0;
5064
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005065 hcd->has_tt = 1;
5066
Heiner Kallweit348becd2017-01-25 23:10:51 +01005067 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5068 hcd->rsrc_start = res->start;
5069 hcd->rsrc_len = resource_size(res);
5070
John Youn9da51972017-01-17 20:30:27 -08005071 ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005072 hsotg->priv = hcd;
5073
Paul Zimmerman7359d482013-03-11 17:47:59 -07005074 /*
5075 * Disable the global interrupt until all the interrupt handlers are
5076 * installed
5077 */
5078 dwc2_disable_global_interrupts(hsotg);
5079
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005080 /* Initialize the DWC_otg core, and select the Phy type */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08005081 retval = dwc2_core_init(hsotg, true);
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005082 if (retval)
5083 goto error2;
5084
Paul Zimmerman7359d482013-03-11 17:47:59 -07005085 /* Create new workqueue and init work */
Wei Yongjun53510352013-04-12 22:41:48 +08005086 retval = -ENOMEM;
Bhaktipriya Shridharec7b1262016-07-28 13:57:29 +05305087 hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005088 if (!hsotg->wq_otg) {
5089 dev_err(hsotg->dev, "Failed to create workqueue\n");
5090 goto error2;
5091 }
5092 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5093
Kees Cooke99e88a2017-10-16 14:43:17 -07005094 timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005095
5096 /* Initialize the non-periodic schedule */
5097 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08005098 INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005099 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5100
5101 /* Initialize the periodic schedule */
5102 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5103 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5104 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5105 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5106
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005107 INIT_LIST_HEAD(&hsotg->split_order);
5108
Paul Zimmerman7359d482013-03-11 17:47:59 -07005109 /*
5110 * Create a host channel descriptor for each host channel implemented
5111 * in the controller. Initialize the channel descriptor array.
5112 */
5113 INIT_LIST_HEAD(&hsotg->free_hc_list);
John Younbea8e862016-11-03 17:55:53 -07005114 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005115 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5116
5117 for (i = 0; i < num_channels; i++) {
5118 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
John Youn9da51972017-01-17 20:30:27 -08005119 if (!channel)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005120 goto error3;
5121 channel->hc_num = i;
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005122 INIT_LIST_HEAD(&channel->split_order_list_entry);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005123 hsotg->hc_ptr_array[i] = channel;
5124 }
5125
5126 /* Initialize hsotg start work */
5127 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5128
5129 /* Initialize port reset work */
5130 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5131
5132 /*
5133 * Allocate space for storing data on status transactions. Normally no
5134 * data is sent, but this space acts as a bit bucket. This must be
5135 * done after usb_add_hcd since that function allocates the DMA buffer
5136 * pool.
5137 */
John Youn95832c02017-01-23 14:57:26 -08005138 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005139 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5140 DWC2_HCD_STATUS_BUF_SIZE,
5141 &hsotg->status_buf_dma, GFP_KERNEL);
5142 else
5143 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5144 GFP_KERNEL);
5145
5146 if (!hsotg->status_buf)
5147 goto error3;
5148
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005149 /*
5150 * Create kmem caches to handle descriptor buffers in descriptor
5151 * DMA mode.
5152 * Alignment must be set to 512 bytes.
5153 */
John Younbea8e862016-11-03 17:55:53 -07005154 if (hsotg->params.dma_desc_enable ||
5155 hsotg->params.dma_desc_fs_enable) {
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005156 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005157 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005158 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
5159 NULL);
5160 if (!hsotg->desc_gen_cache) {
5161 dev_err(hsotg->dev,
5162 "unable to create dwc2 generic desc cache\n");
5163
5164 /*
5165 * Disable descriptor dma mode since it will not be
5166 * usable.
5167 */
John Youn95832c02017-01-23 14:57:26 -08005168 hsotg->params.dma_desc_enable = false;
5169 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005170 }
5171
5172 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005173 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005174 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
5175 if (!hsotg->desc_hsisoc_cache) {
5176 dev_err(hsotg->dev,
5177 "unable to create dwc2 hs isoc desc cache\n");
5178
5179 kmem_cache_destroy(hsotg->desc_gen_cache);
5180
5181 /*
5182 * Disable descriptor dma mode since it will not be
5183 * usable.
5184 */
John Youn95832c02017-01-23 14:57:26 -08005185 hsotg->params.dma_desc_enable = false;
5186 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005187 }
5188 }
5189
Paul Zimmerman7359d482013-03-11 17:47:59 -07005190 hsotg->otg_port = 1;
5191 hsotg->frame_list = NULL;
5192 hsotg->frame_list_dma = 0;
5193 hsotg->periodic_qh_count = 0;
5194
5195 /* Initiate lx_state to L3 disconnected state */
5196 hsotg->lx_state = DWC2_L3;
5197
5198 hcd->self.otg_port = hsotg->otg_port;
5199
5200 /* Don't support SG list at this point */
5201 hcd->self.sg_tablesize = 0;
5202
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005203 if (!IS_ERR_OR_NULL(hsotg->uphy))
5204 otg_set_host(hsotg->uphy->otg, &hcd->self);
5205
Paul Zimmerman7359d482013-03-11 17:47:59 -07005206 /*
5207 * Finish generic HCD initialization and start the HCD. This function
5208 * allocates the DMA buffer pool, registers the USB bus, requests the
5209 * IRQ line, and calls hcd_start method.
5210 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005211 retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005212 if (retval < 0)
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005213 goto error4;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005214
Peter Chen3c9740a2013-11-05 10:46:02 +08005215 device_wakeup_enable(hcd->self.controller);
5216
Paul Zimmerman7359d482013-03-11 17:47:59 -07005217 dwc2_hcd_dump_state(hsotg);
5218
5219 dwc2_enable_global_interrupts(hsotg);
5220
5221 return 0;
5222
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005223error4:
5224 kmem_cache_destroy(hsotg->desc_gen_cache);
5225 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005226error3:
5227 dwc2_hcd_release(hsotg);
5228error2:
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005229 usb_put_hcd(hcd);
5230error1:
Paul Zimmerman7359d482013-03-11 17:47:59 -07005231
5232#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5233 kfree(hsotg->last_frame_num_array);
5234 kfree(hsotg->frame_num_array);
5235#endif
5236
Paul Zimmermane62662c2013-03-25 17:03:35 -07005237 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005238 return retval;
5239}
Paul Zimmerman7359d482013-03-11 17:47:59 -07005240
5241/*
5242 * Removes the HCD.
5243 * Frees memory and resources associated with the HCD and deregisters the bus.
5244 */
Paul Zimmermane62662c2013-03-25 17:03:35 -07005245void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005246{
5247 struct usb_hcd *hcd;
5248
Paul Zimmermane62662c2013-03-25 17:03:35 -07005249 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005250
5251 hcd = dwc2_hsotg_to_hcd(hsotg);
Paul Zimmermane62662c2013-03-25 17:03:35 -07005252 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005253
5254 if (!hcd) {
Paul Zimmermane62662c2013-03-25 17:03:35 -07005255 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
Paul Zimmerman7359d482013-03-11 17:47:59 -07005256 __func__);
5257 return;
5258 }
5259
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005260 if (!IS_ERR_OR_NULL(hsotg->uphy))
5261 otg_set_host(hsotg->uphy->otg, NULL);
5262
Paul Zimmerman7359d482013-03-11 17:47:59 -07005263 usb_remove_hcd(hcd);
5264 hsotg->priv = NULL;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005265
5266 kmem_cache_destroy(hsotg->desc_gen_cache);
5267 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5268
Paul Zimmerman7359d482013-03-11 17:47:59 -07005269 dwc2_hcd_release(hsotg);
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005270 usb_put_hcd(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005271
5272#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5273 kfree(hsotg->last_frame_num_array);
5274 kfree(hsotg->frame_num_array);
5275#endif
Paul Zimmerman7359d482013-03-11 17:47:59 -07005276}
John Youn58e52ff6a2016-02-23 19:54:57 -08005277
5278/**
5279 * dwc2_backup_host_registers() - Backup controller host registers.
5280 * When suspending usb bus, registers needs to be backuped
5281 * if controller power is disabled once suspended.
5282 *
5283 * @hsotg: Programming view of the DWC_otg controller
5284 */
5285int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
5286{
5287 struct dwc2_hregs_backup *hr;
5288 int i;
5289
5290 dev_dbg(hsotg->dev, "%s\n", __func__);
5291
5292 /* Backup Host regs */
5293 hr = &hsotg->hr_backup;
5294 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
5295 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
John Younbea8e862016-11-03 17:55:53 -07005296 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005297 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
5298
5299 hr->hprt0 = dwc2_read_hprt0(hsotg);
5300 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
5301 hr->valid = true;
5302
5303 return 0;
5304}
5305
5306/**
5307 * dwc2_restore_host_registers() - Restore controller host registers.
5308 * When resuming usb bus, device registers needs to be restored
5309 * if controller power were disabled.
5310 *
5311 * @hsotg: Programming view of the DWC_otg controller
5312 */
5313int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
5314{
5315 struct dwc2_hregs_backup *hr;
5316 int i;
5317
5318 dev_dbg(hsotg->dev, "%s\n", __func__);
5319
5320 /* Restore host regs */
5321 hr = &hsotg->hr_backup;
5322 if (!hr->valid) {
5323 dev_err(hsotg->dev, "%s: no host registers to restore\n",
5324 __func__);
5325 return -EINVAL;
5326 }
5327 hr->valid = false;
5328
5329 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5330 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
5331
John Younbea8e862016-11-03 17:55:53 -07005332 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005333 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
5334
5335 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
5336 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
5337 hsotg->frame_number = 0;
5338
5339 return 0;
5340}