blob: 6f7f9c73b5964fdb9cad48006a9e689ebec914b1 [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{
Minas Harutyunyan92a8dd22018-01-19 14:44:20 +04002331 u32 hcfg, hfir, otgctl, usbcfg;
John Younb02038fa2016-02-23 19:55:00 -08002332
2333 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2334
Minas Harutyunyan92a8dd22018-01-19 14:44:20 +04002335 /* Set HS/FS Timeout Calibration to 7 (max available value).
2336 * The number of PHY clocks that the application programs in
2337 * this field is added to the high/full speed interpacket timeout
2338 * duration in the core to account for any additional delays
2339 * introduced by the PHY. This can be required, because the delay
2340 * introduced by the PHY in generating the linestate condition
2341 * can vary from one PHY to another.
2342 */
2343 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2344 usbcfg |= GUSBCFG_TOUTCAL(7);
2345 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2346
John Younb02038fa2016-02-23 19:55:00 -08002347 /* Restart the Phy Clock */
2348 dwc2_writel(0, hsotg->regs + PCGCTL);
2349
2350 /* Initialize Host Configuration Register */
2351 dwc2_init_fs_ls_pclk_sel(hsotg);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08002352 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
2353 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
John Younb02038fa2016-02-23 19:55:00 -08002354 hcfg = dwc2_readl(hsotg->regs + HCFG);
2355 hcfg |= HCFG_FSLSSUPP;
2356 dwc2_writel(hcfg, hsotg->regs + HCFG);
2357 }
2358
2359 /*
2360 * This bit allows dynamic reloading of the HFIR register during
2361 * runtime. This bit needs to be programmed during initial configuration
2362 * and its value must not be changed during runtime.
2363 */
John Youn95832c02017-01-23 14:57:26 -08002364 if (hsotg->params.reload_ctl) {
John Younb02038fa2016-02-23 19:55:00 -08002365 hfir = dwc2_readl(hsotg->regs + HFIR);
2366 hfir |= HFIR_RLDCTRL;
2367 dwc2_writel(hfir, hsotg->regs + HFIR);
2368 }
2369
John Youn95832c02017-01-23 14:57:26 -08002370 if (hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002371 u32 op_mode = hsotg->hw_params.op_mode;
2372
2373 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2374 !hsotg->hw_params.dma_desc_enable ||
2375 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2376 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2377 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2378 dev_err(hsotg->dev,
2379 "Hardware does not support descriptor DMA mode -\n");
2380 dev_err(hsotg->dev,
2381 "falling back to buffer DMA mode.\n");
John Youn95832c02017-01-23 14:57:26 -08002382 hsotg->params.dma_desc_enable = false;
John Younb02038fa2016-02-23 19:55:00 -08002383 } else {
2384 hcfg = dwc2_readl(hsotg->regs + HCFG);
2385 hcfg |= HCFG_DESCDMA;
2386 dwc2_writel(hcfg, hsotg->regs + HCFG);
2387 }
2388 }
2389
2390 /* Configure data FIFO sizes */
2391 dwc2_config_fifos(hsotg);
2392
2393 /* TODO - check this */
2394 /* Clear Host Set HNP Enable in the OTG Control Register */
2395 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2396 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2397 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2398
2399 /* Make sure the FIFOs are flushed */
2400 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2401 dwc2_flush_rx_fifo(hsotg);
2402
2403 /* Clear Host Set HNP Enable in the OTG Control Register */
2404 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2405 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2406 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2407
John Youn95832c02017-01-23 14:57:26 -08002408 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002409 int num_channels, i;
2410 u32 hcchar;
2411
2412 /* Flush out any leftover queued requests */
John Younbea8e862016-11-03 17:55:53 -07002413 num_channels = hsotg->params.host_channels;
John Younb02038fa2016-02-23 19:55:00 -08002414 for (i = 0; i < num_channels; i++) {
2415 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2416 hcchar &= ~HCCHAR_CHENA;
2417 hcchar |= HCCHAR_CHDIS;
2418 hcchar &= ~HCCHAR_EPDIR;
2419 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2420 }
2421
2422 /* Halt all channels to put them into a known state */
2423 for (i = 0; i < num_channels; i++) {
John Younb02038fa2016-02-23 19:55:00 -08002424 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2425 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2426 hcchar &= ~HCCHAR_EPDIR;
2427 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2428 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2429 __func__, i);
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002430
2431 if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
2432 HCCHAR_CHENA, 1000)) {
2433 dev_warn(hsotg->dev, "Unable to clear enable on channel %d\n",
2434 i);
2435 }
John Younb02038fa2016-02-23 19:55:00 -08002436 }
2437 }
2438
2439 /* Turn on the vbus power */
2440 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2441 if (hsotg->op_state == OTG_STATE_A_HOST) {
2442 u32 hprt0 = dwc2_read_hprt0(hsotg);
2443
2444 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2445 !!(hprt0 & HPRT0_PWR));
2446 if (!(hprt0 & HPRT0_PWR)) {
2447 hprt0 |= HPRT0_PWR;
2448 dwc2_writel(hprt0, hsotg->regs + HPRT0);
2449 }
2450 }
2451
2452 dwc2_enable_host_interrupts(hsotg);
2453}
2454
Paul Zimmerman7359d482013-03-11 17:47:59 -07002455/*
2456 * Initializes dynamic portions of the DWC_otg HCD state
2457 *
2458 * Must be called with interrupt disabled and spinlock held
2459 */
2460static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2461{
2462 struct dwc2_host_chan *chan, *chan_tmp;
2463 int num_channels;
2464 int i;
2465
2466 hsotg->flags.d32 = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002467 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002468
John Youn95832c02017-01-23 14:57:26 -08002469 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002470 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07002471 hsotg->params.host_channels;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002472 } else {
2473 hsotg->non_periodic_channels = 0;
2474 hsotg->periodic_channels = 0;
2475 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002476
2477 /*
2478 * Put all channels in the free channel list and clean up channel
2479 * states
2480 */
2481 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2482 hc_list_entry)
2483 list_del_init(&chan->hc_list_entry);
2484
John Younbea8e862016-11-03 17:55:53 -07002485 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002486 for (i = 0; i < num_channels; i++) {
2487 chan = hsotg->hc_ptr_array[i];
2488 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2489 dwc2_hc_cleanup(hsotg, chan);
2490 }
2491
2492 /* Initialize the DWC core for host mode operation */
2493 dwc2_core_host_init(hsotg);
2494}
2495
2496static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2497 struct dwc2_host_chan *chan,
2498 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2499{
2500 int hub_addr, hub_port;
2501
2502 chan->do_split = 1;
2503 chan->xact_pos = qtd->isoc_split_pos;
2504 chan->complete_split = qtd->complete_split;
2505 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2506 chan->hub_addr = (u8)hub_addr;
2507 chan->hub_port = (u8)hub_port;
2508}
2509
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002510static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2511 struct dwc2_host_chan *chan,
2512 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002513{
2514 struct dwc2_hcd_urb *urb = qtd->urb;
2515 struct dwc2_hcd_iso_packet_desc *frame_desc;
2516
2517 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2518 case USB_ENDPOINT_XFER_CONTROL:
2519 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2520
2521 switch (qtd->control_phase) {
2522 case DWC2_CONTROL_SETUP:
2523 dev_vdbg(hsotg->dev, " Control setup transaction\n");
2524 chan->do_ping = 0;
2525 chan->ep_is_in = 0;
2526 chan->data_pid_start = DWC2_HC_PID_SETUP;
John Youn95832c02017-01-23 14:57:26 -08002527 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002528 chan->xfer_dma = urb->setup_dma;
2529 else
2530 chan->xfer_buf = urb->setup_packet;
2531 chan->xfer_len = 8;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002532 break;
2533
2534 case DWC2_CONTROL_DATA:
2535 dev_vdbg(hsotg->dev, " Control data transaction\n");
2536 chan->data_pid_start = qtd->data_toggle;
2537 break;
2538
2539 case DWC2_CONTROL_STATUS:
2540 /*
2541 * Direction is opposite of data direction or IN if no
2542 * data
2543 */
2544 dev_vdbg(hsotg->dev, " Control status transaction\n");
2545 if (urb->length == 0)
2546 chan->ep_is_in = 1;
2547 else
2548 chan->ep_is_in =
2549 dwc2_hcd_is_pipe_out(&urb->pipe_info);
2550 if (chan->ep_is_in)
2551 chan->do_ping = 0;
2552 chan->data_pid_start = DWC2_HC_PID_DATA1;
2553 chan->xfer_len = 0;
John Youn95832c02017-01-23 14:57:26 -08002554 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002555 chan->xfer_dma = hsotg->status_buf_dma;
2556 else
2557 chan->xfer_buf = hsotg->status_buf;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002558 break;
2559 }
2560 break;
2561
2562 case USB_ENDPOINT_XFER_BULK:
2563 chan->ep_type = USB_ENDPOINT_XFER_BULK;
2564 break;
2565
2566 case USB_ENDPOINT_XFER_INT:
2567 chan->ep_type = USB_ENDPOINT_XFER_INT;
2568 break;
2569
2570 case USB_ENDPOINT_XFER_ISOC:
2571 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
John Youn95832c02017-01-23 14:57:26 -08002572 if (hsotg->params.dma_desc_enable)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002573 break;
2574
2575 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2576 frame_desc->status = 0;
2577
John Youn95832c02017-01-23 14:57:26 -08002578 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002579 chan->xfer_dma = urb->dma;
2580 chan->xfer_dma += frame_desc->offset +
2581 qtd->isoc_split_offset;
2582 } else {
2583 chan->xfer_buf = urb->buf;
2584 chan->xfer_buf += frame_desc->offset +
2585 qtd->isoc_split_offset;
2586 }
2587
2588 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2589
Paul Zimmerman7359d482013-03-11 17:47:59 -07002590 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2591 if (chan->xfer_len <= 188)
2592 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2593 else
2594 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2595 }
2596 break;
2597 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002598}
2599
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002600#define DWC2_USB_DMA_ALIGN 4
2601
2602struct dma_aligned_buffer {
2603 void *kmalloc_ptr;
2604 void *old_xfer_buffer;
2605 u8 data[0];
2606};
2607
2608static void dwc2_free_dma_aligned_buffer(struct urb *urb)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002609{
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002610 struct dma_aligned_buffer *temp;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002611
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002612 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2613 return;
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002614
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002615 temp = container_of(urb->transfer_buffer,
John Youn9da51972017-01-17 20:30:27 -08002616 struct dma_aligned_buffer, data);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002617
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002618 if (usb_urb_dir_in(urb))
2619 memcpy(temp->old_xfer_buffer, temp->data,
2620 urb->transfer_buffer_length);
2621 urb->transfer_buffer = temp->old_xfer_buffer;
2622 kfree(temp->kmalloc_ptr);
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002623
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002624 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2625}
Paul Zimmerman7359d482013-03-11 17:47:59 -07002626
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002627static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
2628{
2629 struct dma_aligned_buffer *temp, *kmalloc_ptr;
2630 size_t kmalloc_size;
Gregory Herrerodb62b9a2015-04-29 22:09:16 +02002631
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002632 if (urb->num_sgs || urb->sg ||
2633 urb->transfer_buffer_length == 0 ||
2634 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2635 return 0;
2636
2637 /* Allocate a buffer with enough padding for alignment */
2638 kmalloc_size = urb->transfer_buffer_length +
2639 sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
2640
2641 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2642 if (!kmalloc_ptr)
2643 return -ENOMEM;
2644
2645 /* Position our struct dma_aligned_buffer such that data is aligned */
2646 temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
2647 temp->kmalloc_ptr = kmalloc_ptr;
2648 temp->old_xfer_buffer = urb->transfer_buffer;
2649 if (usb_urb_dir_out(urb))
2650 memcpy(temp->data, urb->transfer_buffer,
2651 urb->transfer_buffer_length);
2652 urb->transfer_buffer = temp->data;
2653
2654 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2655
Paul Zimmerman7359d482013-03-11 17:47:59 -07002656 return 0;
2657}
2658
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002659static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
John Youn9da51972017-01-17 20:30:27 -08002660 gfp_t mem_flags)
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002661{
2662 int ret;
2663
2664 /* We assume setup_dma is always aligned; warn if not */
2665 WARN_ON_ONCE(urb->setup_dma &&
2666 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
2667
2668 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
2669 if (ret)
2670 return ret;
2671
2672 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2673 if (ret)
2674 dwc2_free_dma_aligned_buffer(urb);
2675
2676 return ret;
2677}
2678
2679static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2680{
2681 usb_hcd_unmap_urb_for_dma(hcd, urb);
2682 dwc2_free_dma_aligned_buffer(urb);
2683}
2684
Paul Zimmerman7359d482013-03-11 17:47:59 -07002685/**
2686 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2687 * channel and initializes the host channel to perform the transactions. The
2688 * host channel is removed from the free list.
2689 *
2690 * @hsotg: The HCD state structure
2691 * @qh: Transactions from the first QTD for this QH are selected and assigned
2692 * to a free host channel
2693 */
Dom Cobley20f2eb92013-09-23 14:23:34 -07002694static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002695{
2696 struct dwc2_host_chan *chan;
2697 struct dwc2_hcd_urb *urb;
2698 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002699
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002700 if (dbg_qh(qh))
2701 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002702
2703 if (list_empty(&qh->qtd_list)) {
2704 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002705 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002706 }
2707
2708 if (list_empty(&hsotg->free_hc_list)) {
2709 dev_dbg(hsotg->dev, "No free channel to assign\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002710 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002711 }
2712
2713 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2714 hc_list_entry);
2715
Dom Cobley20f2eb92013-09-23 14:23:34 -07002716 /* Remove host channel from free list */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002717 list_del_init(&chan->hc_list_entry);
2718
2719 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2720 urb = qtd->urb;
2721 qh->channel = chan;
2722 qtd->in_process = 1;
2723
2724 /*
2725 * Use usb_pipedevice to determine device address. This address is
2726 * 0 before the SET_ADDRESS command and the correct address afterward.
2727 */
2728 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2729 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2730 chan->speed = qh->dev_speed;
2731 chan->max_packet = dwc2_max_packet(qh->maxp);
2732
2733 chan->xfer_started = 0;
2734 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2735 chan->error_state = (qtd->error_count > 0);
2736 chan->halt_on_queue = 0;
2737 chan->halt_pending = 0;
2738 chan->requests = 0;
2739
2740 /*
2741 * The following values may be modified in the transfer type section
2742 * below. The xfer_len value may be reduced when the transfer is
2743 * started to accommodate the max widths of the XferSize and PktCnt
2744 * fields in the HCTSIZn register.
2745 */
2746
2747 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2748 if (chan->ep_is_in)
2749 chan->do_ping = 0;
2750 else
2751 chan->do_ping = qh->ping_state;
2752
2753 chan->data_pid_start = qh->data_toggle;
2754 chan->multi_count = 1;
2755
Rashika Kheriabb6c3422013-10-26 23:11:22 +05302756 if (urb->actual_length > urb->length &&
John Youn9da51972017-01-17 20:30:27 -08002757 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
Paul Zimmerman84181082013-09-23 14:23:33 -07002758 urb->actual_length = urb->length;
2759
John Youn95832c02017-01-23 14:57:26 -08002760 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002761 chan->xfer_dma = urb->dma + urb->actual_length;
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002762 else
Paul Zimmerman7359d482013-03-11 17:47:59 -07002763 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002764
2765 chan->xfer_len = urb->length - urb->actual_length;
2766 chan->xfer_count = 0;
2767
2768 /* Set the split attributes if required */
2769 if (qh->do_split)
2770 dwc2_hc_init_split(hsotg, chan, qtd, urb);
2771 else
2772 chan->do_split = 0;
2773
2774 /* Set the transfer attributes */
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002775 dwc2_hc_init_xfer(hsotg, chan, qtd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002776
2777 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2778 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2779 /*
2780 * This value may be modified when the transfer is started
2781 * to reflect the actual transfer length
2782 */
2783 chan->multi_count = dwc2_hb_mult(qh->maxp);
2784
John Youn95832c02017-01-23 14:57:26 -08002785 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002786 chan->desc_list_addr = qh->desc_list_dma;
Gregory Herrero95105a92015-11-20 11:49:29 +01002787 chan->desc_list_sz = qh->desc_list_sz;
2788 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002789
2790 dwc2_hc_init(hsotg, chan);
2791 chan->qh = qh;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002792
2793 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002794}
2795
2796/**
2797 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2798 * schedule and assigns them to available host channels. Called from the HCD
2799 * interrupt handler functions.
2800 *
2801 * @hsotg: The HCD state structure
2802 *
2803 * Return: The types of new transactions that were assigned to host channels
2804 */
2805enum dwc2_transaction_type dwc2_hcd_select_transactions(
2806 struct dwc2_hsotg *hsotg)
2807{
2808 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2809 struct list_head *qh_ptr;
2810 struct dwc2_qh *qh;
2811 int num_channels;
2812
2813#ifdef DWC2_DEBUG_SOF
2814 dev_vdbg(hsotg->dev, " Select Transactions\n");
2815#endif
2816
2817 /* Process entries in the periodic ready list */
2818 qh_ptr = hsotg->periodic_sched_ready.next;
2819 while (qh_ptr != &hsotg->periodic_sched_ready) {
2820 if (list_empty(&hsotg->free_hc_list))
2821 break;
John Youn95832c02017-01-23 14:57:26 -08002822 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002823 if (hsotg->available_host_channels <= 1)
2824 break;
2825 hsotg->available_host_channels--;
2826 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002827 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -07002828 if (dwc2_assign_and_init_hc(hsotg, qh))
2829 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002830
2831 /*
2832 * Move the QH from the periodic ready schedule to the
2833 * periodic assigned schedule
2834 */
2835 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002836 list_move_tail(&qh->qh_list_entry,
2837 &hsotg->periodic_sched_assigned);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002838 ret_val = DWC2_TRANSACTION_PERIODIC;
2839 }
2840
2841 /*
2842 * Process entries in the inactive portion of the non-periodic
2843 * schedule. Some free host channels may not be used if they are
2844 * reserved for periodic transfers.
2845 */
John Younbea8e862016-11-03 17:55:53 -07002846 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002847 qh_ptr = hsotg->non_periodic_sched_inactive.next;
2848 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
John Youn95832c02017-01-23 14:57:26 -08002849 if (!hsotg->params.uframe_sched &&
Dom Cobley20f2eb92013-09-23 14:23:34 -07002850 hsotg->non_periodic_channels >= num_channels -
Paul Zimmerman7359d482013-03-11 17:47:59 -07002851 hsotg->periodic_channels)
2852 break;
2853 if (list_empty(&hsotg->free_hc_list))
2854 break;
2855 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
John Youn95832c02017-01-23 14:57:26 -08002856 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002857 if (hsotg->available_host_channels < 1)
2858 break;
2859 hsotg->available_host_channels--;
2860 }
2861
2862 if (dwc2_assign_and_init_hc(hsotg, qh))
2863 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002864
2865 /*
2866 * Move the QH from the non-periodic inactive schedule to the
2867 * non-periodic active schedule
2868 */
2869 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002870 list_move_tail(&qh->qh_list_entry,
2871 &hsotg->non_periodic_sched_active);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002872
2873 if (ret_val == DWC2_TRANSACTION_NONE)
2874 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2875 else
2876 ret_val = DWC2_TRANSACTION_ALL;
2877
John Youn95832c02017-01-23 14:57:26 -08002878 if (!hsotg->params.uframe_sched)
Dom Cobley20f2eb92013-09-23 14:23:34 -07002879 hsotg->non_periodic_channels++;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002880 }
2881
2882 return ret_val;
2883}
2884
2885/**
2886 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2887 * a host channel associated with either a periodic or non-periodic transfer
2888 *
2889 * @hsotg: The HCD state structure
2890 * @chan: Host channel descriptor associated with either a periodic or
2891 * non-periodic transfer
2892 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2893 * for periodic transfers or the non-periodic Tx FIFO
2894 * for non-periodic transfers
2895 *
2896 * Return: 1 if a request is queued and more requests may be needed to
2897 * complete the transfer, 0 if no more requests are required for this
2898 * transfer, -1 if there is insufficient space in the Tx FIFO
2899 *
2900 * This function assumes that there is space available in the appropriate
2901 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2902 * it checks whether space is available in the appropriate Tx FIFO.
2903 *
2904 * Must be called with interrupt disabled and spinlock held
2905 */
2906static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2907 struct dwc2_host_chan *chan,
2908 u16 fifo_dwords_avail)
2909{
2910 int retval = 0;
2911
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002912 if (chan->do_split)
2913 /* Put ourselves on the list to keep order straight */
2914 list_move_tail(&chan->split_order_list_entry,
2915 &hsotg->split_order);
2916
John Youn95832c02017-01-23 14:57:26 -08002917 if (hsotg->params.host_dma) {
2918 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002919 if (!chan->xfer_started ||
2920 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2921 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2922 chan->qh->ping_state = 0;
2923 }
2924 } else if (!chan->xfer_started) {
2925 dwc2_hc_start_transfer(hsotg, chan);
2926 chan->qh->ping_state = 0;
2927 }
2928 } else if (chan->halt_pending) {
2929 /* Don't queue a request if the channel has been halted */
2930 } else if (chan->halt_on_queue) {
2931 dwc2_hc_halt(hsotg, chan, chan->halt_status);
2932 } else if (chan->do_ping) {
2933 if (!chan->xfer_started)
2934 dwc2_hc_start_transfer(hsotg, chan);
2935 } else if (!chan->ep_is_in ||
2936 chan->data_pid_start == DWC2_HC_PID_SETUP) {
2937 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2938 if (!chan->xfer_started) {
2939 dwc2_hc_start_transfer(hsotg, chan);
2940 retval = 1;
2941 } else {
2942 retval = dwc2_hc_continue_transfer(hsotg, chan);
2943 }
2944 } else {
2945 retval = -1;
2946 }
2947 } else {
2948 if (!chan->xfer_started) {
2949 dwc2_hc_start_transfer(hsotg, chan);
2950 retval = 1;
2951 } else {
2952 retval = dwc2_hc_continue_transfer(hsotg, chan);
2953 }
2954 }
2955
2956 return retval;
2957}
2958
2959/*
2960 * Processes periodic channels for the next frame and queues transactions for
2961 * these channels to the DWC_otg controller. After queueing transactions, the
2962 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2963 * to queue as Periodic Tx FIFO or request queue space becomes available.
2964 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2965 *
2966 * Must be called with interrupt disabled and spinlock held
2967 */
2968static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2969{
2970 struct list_head *qh_ptr;
2971 struct dwc2_qh *qh;
2972 u32 tx_status;
2973 u32 fspcavail;
2974 u32 gintmsk;
2975 int status;
Douglas Anderson4e50e012016-01-28 18:20:03 -08002976 bool no_queue_space = false;
2977 bool no_fifo_space = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002978 u32 qspcavail;
2979
Douglas Anderson4e50e012016-01-28 18:20:03 -08002980 /* If empty list then just adjust interrupt enables */
2981 if (list_empty(&hsotg->periodic_sched_assigned))
2982 goto exit;
2983
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002984 if (dbg_perio())
2985 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07002986
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002987 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002988 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2989 TXSTS_QSPCAVAIL_SHIFT;
2990 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2991 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002992
2993 if (dbg_perio()) {
2994 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
2995 qspcavail);
2996 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
2997 fspcavail);
2998 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002999
3000 qh_ptr = hsotg->periodic_sched_assigned.next;
3001 while (qh_ptr != &hsotg->periodic_sched_assigned) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003002 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmanacdb9042013-08-30 18:45:16 +02003003 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3004 TXSTS_QSPCAVAIL_SHIFT;
3005 if (qspcavail == 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003006 no_queue_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003007 break;
3008 }
3009
3010 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
3011 if (!qh->channel) {
3012 qh_ptr = qh_ptr->next;
3013 continue;
3014 }
3015
3016 /* Make sure EP's TT buffer is clean before queueing qtds */
3017 if (qh->tt_buffer_dirty) {
3018 qh_ptr = qh_ptr->next;
3019 continue;
3020 }
3021
3022 /*
3023 * Set a flag if we're queuing high-bandwidth in slave mode.
3024 * The flag prevents any halts to get into the request queue in
3025 * the middle of multiple high-bandwidth packets getting queued.
3026 */
John Youn95832c02017-01-23 14:57:26 -08003027 if (!hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08003028 qh->channel->multi_count > 1)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003029 hsotg->queuing_high_bandwidth = 1;
3030
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003031 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3032 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003033 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3034 if (status < 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003035 no_fifo_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003036 break;
3037 }
3038
3039 /*
3040 * In Slave mode, stay on the current transfer until there is
3041 * nothing more to do or the high-bandwidth request count is
3042 * reached. In DMA mode, only need to queue one request. The
3043 * controller automatically handles multiple packets for
3044 * high-bandwidth transfers.
3045 */
John Youn95832c02017-01-23 14:57:26 -08003046 if (hsotg->params.host_dma || status == 0 ||
Paul Zimmerman7359d482013-03-11 17:47:59 -07003047 qh->channel->requests == qh->channel->multi_count) {
3048 qh_ptr = qh_ptr->next;
3049 /*
3050 * Move the QH from the periodic assigned schedule to
3051 * the periodic queued schedule
3052 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08003053 list_move_tail(&qh->qh_list_entry,
3054 &hsotg->periodic_sched_queued);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003055
3056 /* done queuing high bandwidth */
3057 hsotg->queuing_high_bandwidth = 0;
3058 }
3059 }
3060
Douglas Anderson4e50e012016-01-28 18:20:03 -08003061exit:
3062 if (no_queue_space || no_fifo_space ||
John Youn95832c02017-01-23 14:57:26 -08003063 (!hsotg->params.host_dma &&
Douglas Anderson4e50e012016-01-28 18:20:03 -08003064 !list_empty(&hsotg->periodic_sched_assigned))) {
3065 /*
3066 * May need to queue more transactions as the request
3067 * queue or Tx FIFO empties. Enable the periodic Tx
3068 * FIFO empty interrupt. (Always use the half-empty
3069 * level to ensure that new requests are loaded as
3070 * soon as possible.)
3071 */
3072 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3073 if (!(gintmsk & GINTSTS_PTXFEMP)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003074 gintmsk |= GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003075 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Douglas Anderson4e50e012016-01-28 18:20:03 -08003076 }
3077 } else {
3078 /*
3079 * Disable the Tx FIFO empty interrupt since there are
3080 * no more transactions that need to be queued right
3081 * now. This function is called from interrupt
3082 * handlers to queue more transactions as transfer
3083 * states change.
John Youn38beaec2017-01-17 20:31:13 -08003084 */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003085 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3086 if (gintmsk & GINTSTS_PTXFEMP) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003087 gintmsk &= ~GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003088 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003089 }
3090 }
3091}
3092
3093/*
3094 * Processes active non-periodic channels and queues transactions for these
3095 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3096 * FIFO Empty interrupt is enabled if there are more transactions to queue as
3097 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3098 * FIFO Empty interrupt is disabled.
3099 *
3100 * Must be called with interrupt disabled and spinlock held
3101 */
3102static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3103{
3104 struct list_head *orig_qh_ptr;
3105 struct dwc2_qh *qh;
3106 u32 tx_status;
3107 u32 qspcavail;
3108 u32 fspcavail;
3109 u32 gintmsk;
3110 int status;
3111 int no_queue_space = 0;
3112 int no_fifo_space = 0;
3113 int more_to_do = 0;
3114
3115 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3116
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003117 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003118 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3119 TXSTS_QSPCAVAIL_SHIFT;
3120 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3121 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003122 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
3123 qspcavail);
3124 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
3125 fspcavail);
3126
3127 /*
3128 * Keep track of the starting point. Skip over the start-of-list
3129 * entry.
3130 */
3131 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3132 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3133 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3134
3135 /*
3136 * Process once through the active list or until no more space is
3137 * available in the request queue or the Tx FIFO
3138 */
3139 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003140 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003141 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3142 TXSTS_QSPCAVAIL_SHIFT;
John Youn95832c02017-01-23 14:57:26 -08003143 if (!hsotg->params.host_dma && qspcavail == 0) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003144 no_queue_space = 1;
3145 break;
3146 }
3147
3148 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3149 qh_list_entry);
3150 if (!qh->channel)
3151 goto next;
3152
3153 /* Make sure EP's TT buffer is clean before queueing qtds */
3154 if (qh->tt_buffer_dirty)
3155 goto next;
3156
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003157 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3158 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003159 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3160
3161 if (status > 0) {
3162 more_to_do = 1;
3163 } else if (status < 0) {
3164 no_fifo_space = 1;
3165 break;
3166 }
3167next:
3168 /* Advance to next QH, skipping start-of-list entry */
3169 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3170 if (hsotg->non_periodic_qh_ptr ==
3171 &hsotg->non_periodic_sched_active)
3172 hsotg->non_periodic_qh_ptr =
3173 hsotg->non_periodic_qh_ptr->next;
3174 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3175
John Youn95832c02017-01-23 14:57:26 -08003176 if (!hsotg->params.host_dma) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003177 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003178 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3179 TXSTS_QSPCAVAIL_SHIFT;
3180 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3181 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003182 dev_vdbg(hsotg->dev,
3183 " NP Tx Req Queue Space Avail (after queue): %d\n",
3184 qspcavail);
3185 dev_vdbg(hsotg->dev,
3186 " NP Tx FIFO Space Avail (after queue): %d\n",
3187 fspcavail);
3188
3189 if (more_to_do || no_queue_space || no_fifo_space) {
3190 /*
3191 * May need to queue more transactions as the request
3192 * queue or Tx FIFO empties. Enable the non-periodic
3193 * Tx FIFO empty interrupt. (Always use the half-empty
3194 * level to ensure that new requests are loaded as
3195 * soon as possible.)
3196 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003197 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003198 gintmsk |= GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003199 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003200 } else {
3201 /*
3202 * Disable the Tx FIFO empty interrupt since there are
3203 * no more transactions that need to be queued right
3204 * now. This function is called from interrupt
3205 * handlers to queue more transactions as transfer
3206 * states change.
3207 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003208 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003209 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003210 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003211 }
3212 }
3213}
3214
3215/**
3216 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3217 * and queues transactions for these channels to the DWC_otg controller. Called
3218 * from the HCD interrupt handler functions.
3219 *
3220 * @hsotg: The HCD state structure
3221 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3222 * or both)
3223 *
3224 * Must be called with interrupt disabled and spinlock held
3225 */
3226void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3227 enum dwc2_transaction_type tr_type)
3228{
3229#ifdef DWC2_DEBUG_SOF
3230 dev_vdbg(hsotg->dev, "Queue Transactions\n");
3231#endif
3232 /* Process host channels associated with periodic transfers */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003233 if (tr_type == DWC2_TRANSACTION_PERIODIC ||
3234 tr_type == DWC2_TRANSACTION_ALL)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003235 dwc2_process_periodic_channels(hsotg);
3236
3237 /* Process host channels associated with non-periodic transfers */
3238 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3239 tr_type == DWC2_TRANSACTION_ALL) {
3240 if (!list_empty(&hsotg->non_periodic_sched_active)) {
3241 dwc2_process_non_periodic_channels(hsotg);
3242 } else {
3243 /*
3244 * Ensure NP Tx FIFO empty interrupt is disabled when
3245 * there are no non-periodic transfers to process
3246 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003247 u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003248
3249 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003250 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003251 }
3252 }
3253}
3254
3255static void dwc2_conn_id_status_change(struct work_struct *work)
3256{
3257 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3258 wf_otg);
3259 u32 count = 0;
3260 u32 gotgctl;
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003261 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003262
3263 dev_dbg(hsotg->dev, "%s()\n", __func__);
3264
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003265 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003266 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3267 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3268 !!(gotgctl & GOTGCTL_CONID_B));
3269
3270 /* B-Device connector (Device Mode) */
3271 if (gotgctl & GOTGCTL_CONID_B) {
3272 /* Wait for switch to device mode */
3273 dev_dbg(hsotg->dev, "connId B\n");
Chen Yu9156a7e2017-01-23 14:59:57 -08003274 if (hsotg->bus_suspended) {
3275 dev_info(hsotg->dev,
3276 "Do port resume before switching to device mode\n");
3277 dwc2_port_resume(hsotg);
3278 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003279 while (!dwc2_is_device_mode(hsotg)) {
3280 dev_info(hsotg->dev,
3281 "Waiting for Peripheral Mode, Mode=%s\n",
3282 dwc2_is_host_mode(hsotg) ? "Host" :
3283 "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003284 msleep(20);
John Stultzfc30c4b2017-01-23 14:59:35 -08003285 /*
3286 * Sometimes the initial GOTGCTRL read is wrong, so
3287 * check it again and jump to host mode if that was
3288 * the case.
3289 */
3290 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3291 if (!(gotgctl & GOTGCTL_CONID_B))
3292 goto host;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003293 if (++count > 250)
3294 break;
3295 }
3296 if (count > 250)
3297 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003298 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003299 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003300 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003301 dwc2_enable_global_interrupts(hsotg);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003302 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003303 dwc2_hsotg_core_init_disconnected(hsotg, false);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003304 spin_unlock_irqrestore(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003305 dwc2_hsotg_core_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003306 } else {
John Stultzfc30c4b2017-01-23 14:59:35 -08003307host:
Paul Zimmerman7359d482013-03-11 17:47:59 -07003308 /* A-Device connector (Host Mode) */
3309 dev_dbg(hsotg->dev, "connId A\n");
3310 while (!dwc2_is_host_mode(hsotg)) {
3311 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3312 dwc2_is_host_mode(hsotg) ?
3313 "Host" : "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003314 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003315 if (++count > 250)
3316 break;
3317 }
3318 if (count > 250)
3319 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003320 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003321
John Stultzd2471d42017-10-23 14:32:48 -07003322 spin_lock_irqsave(&hsotg->lock, flags);
3323 dwc2_hsotg_disconnect(hsotg);
3324 spin_unlock_irqrestore(&hsotg->lock, flags);
3325
3326 hsotg->op_state = OTG_STATE_A_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003327 /* Initialize the Core for Host mode */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003328 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003329 dwc2_enable_global_interrupts(hsotg);
3330 dwc2_hcd_start(hsotg);
3331 }
3332}
3333
Kees Cooke99e88a2017-10-16 14:43:17 -07003334static void dwc2_wakeup_detected(struct timer_list *t)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003335{
Kees Cooke99e88a2017-10-16 14:43:17 -07003336 struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003337 u32 hprt0;
3338
3339 dev_dbg(hsotg->dev, "%s()\n", __func__);
3340
3341 /*
3342 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3343 * so that OPT tests pass with all PHYs.)
3344 */
3345 hprt0 = dwc2_read_hprt0(hsotg);
3346 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3347 hprt0 &= ~HPRT0_RES;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003348 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003349 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003350 dwc2_readl(hsotg->regs + HPRT0));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003351
3352 dwc2_hcd_rem_wakeup(hsotg);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003353 hsotg->bus_suspended = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003354
3355 /* Change to L0 state */
3356 hsotg->lx_state = DWC2_L0;
3357}
3358
3359static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3360{
3361 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3362
3363 return hcd->self.b_hnp_enable;
3364}
3365
3366/* Must NOT be called with interrupt disabled or spinlock held */
3367static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3368{
3369 unsigned long flags;
3370 u32 hprt0;
3371 u32 pcgctl;
3372 u32 gotgctl;
3373
3374 dev_dbg(hsotg->dev, "%s()\n", __func__);
3375
3376 spin_lock_irqsave(&hsotg->lock, flags);
3377
3378 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003379 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003380 gotgctl |= GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003381 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003382 hsotg->op_state = OTG_STATE_A_SUSPEND;
3383 }
3384
3385 hprt0 = dwc2_read_hprt0(hsotg);
3386 hprt0 |= HPRT0_SUSP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003387 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003388
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003389 hsotg->bus_suspended = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003390
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003391 /*
3392 * If hibernation is supported, Phy clock will be suspended
3393 * after registers are backuped.
3394 */
John Younbea8e862016-11-03 17:55:53 -07003395 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003396 /* Suspend the Phy Clock */
3397 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3398 pcgctl |= PCGCTL_STOPPCLK;
3399 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3400 udelay(10);
3401 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003402
3403 /* For HNP the bus must be suspended for at least 200ms */
3404 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003405 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003406 pcgctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003407 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003408
3409 spin_unlock_irqrestore(&hsotg->lock, flags);
3410
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003411 msleep(200);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003412 } else {
3413 spin_unlock_irqrestore(&hsotg->lock, flags);
3414 }
3415}
3416
Gregory Herrero30db1032015-09-22 15:16:38 +02003417/* Must NOT be called with interrupt disabled or spinlock held */
3418static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
3419{
3420 unsigned long flags;
3421 u32 hprt0;
3422 u32 pcgctl;
3423
Douglas Anderson4d273c22015-10-14 15:58:27 -07003424 spin_lock_irqsave(&hsotg->lock, flags);
3425
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003426 /*
3427 * If hibernation is supported, Phy clock is already resumed
3428 * after registers restore.
3429 */
John Younbea8e862016-11-03 17:55:53 -07003430 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003431 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3432 pcgctl &= ~PCGCTL_STOPPCLK;
3433 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003434 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003435 msleep(20);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003436 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003437 }
Gregory Herrero30db1032015-09-22 15:16:38 +02003438
Gregory Herrero30db1032015-09-22 15:16:38 +02003439 hprt0 = dwc2_read_hprt0(hsotg);
3440 hprt0 |= HPRT0_RES;
3441 hprt0 &= ~HPRT0_SUSP;
3442 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3443 spin_unlock_irqrestore(&hsotg->lock, flags);
3444
3445 msleep(USB_RESUME_TIMEOUT);
3446
3447 spin_lock_irqsave(&hsotg->lock, flags);
3448 hprt0 = dwc2_read_hprt0(hsotg);
3449 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
3450 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003451 hsotg->bus_suspended = false;
Gregory Herrero30db1032015-09-22 15:16:38 +02003452 spin_unlock_irqrestore(&hsotg->lock, flags);
3453}
3454
Paul Zimmerman7359d482013-03-11 17:47:59 -07003455/* Handles hub class-specific requests */
3456static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3457 u16 wvalue, u16 windex, char *buf, u16 wlength)
3458{
3459 struct usb_hub_descriptor *hub_desc;
3460 int retval = 0;
3461 u32 hprt0;
3462 u32 port_status;
3463 u32 speed;
3464 u32 pcgctl;
3465
3466 switch (typereq) {
3467 case ClearHubFeature:
3468 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3469
3470 switch (wvalue) {
3471 case C_HUB_LOCAL_POWER:
3472 case C_HUB_OVER_CURRENT:
3473 /* Nothing required here */
3474 break;
3475
3476 default:
3477 retval = -EINVAL;
3478 dev_err(hsotg->dev,
3479 "ClearHubFeature request %1xh unknown\n",
3480 wvalue);
3481 }
3482 break;
3483
3484 case ClearPortFeature:
3485 if (wvalue != USB_PORT_FEAT_L1)
3486 if (!windex || windex > 1)
3487 goto error;
3488 switch (wvalue) {
3489 case USB_PORT_FEAT_ENABLE:
3490 dev_dbg(hsotg->dev,
3491 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3492 hprt0 = dwc2_read_hprt0(hsotg);
3493 hprt0 |= HPRT0_ENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003494 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003495 break;
3496
3497 case USB_PORT_FEAT_SUSPEND:
3498 dev_dbg(hsotg->dev,
3499 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00003500
Gregory Herrerobea78552015-09-22 15:16:44 +02003501 if (hsotg->bus_suspended)
3502 dwc2_port_resume(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003503 break;
3504
3505 case USB_PORT_FEAT_POWER:
3506 dev_dbg(hsotg->dev,
3507 "ClearPortFeature USB_PORT_FEAT_POWER\n");
3508 hprt0 = dwc2_read_hprt0(hsotg);
3509 hprt0 &= ~HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003510 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003511 break;
3512
3513 case USB_PORT_FEAT_INDICATOR:
3514 dev_dbg(hsotg->dev,
3515 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3516 /* Port indicator not supported */
3517 break;
3518
3519 case USB_PORT_FEAT_C_CONNECTION:
3520 /*
3521 * Clears driver's internal Connect Status Change flag
3522 */
3523 dev_dbg(hsotg->dev,
3524 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3525 hsotg->flags.b.port_connect_status_change = 0;
3526 break;
3527
3528 case USB_PORT_FEAT_C_RESET:
3529 /* Clears driver's internal Port Reset Change flag */
3530 dev_dbg(hsotg->dev,
3531 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3532 hsotg->flags.b.port_reset_change = 0;
3533 break;
3534
3535 case USB_PORT_FEAT_C_ENABLE:
3536 /*
3537 * Clears the driver's internal Port Enable/Disable
3538 * Change flag
3539 */
3540 dev_dbg(hsotg->dev,
3541 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3542 hsotg->flags.b.port_enable_change = 0;
3543 break;
3544
3545 case USB_PORT_FEAT_C_SUSPEND:
3546 /*
3547 * Clears the driver's internal Port Suspend Change
3548 * flag, which is set when resume signaling on the host
3549 * port is complete
3550 */
3551 dev_dbg(hsotg->dev,
3552 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3553 hsotg->flags.b.port_suspend_change = 0;
3554 break;
3555
3556 case USB_PORT_FEAT_C_PORT_L1:
3557 dev_dbg(hsotg->dev,
3558 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3559 hsotg->flags.b.port_l1_change = 0;
3560 break;
3561
3562 case USB_PORT_FEAT_C_OVER_CURRENT:
3563 dev_dbg(hsotg->dev,
3564 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3565 hsotg->flags.b.port_over_current_change = 0;
3566 break;
3567
3568 default:
3569 retval = -EINVAL;
3570 dev_err(hsotg->dev,
3571 "ClearPortFeature request %1xh unknown or unsupported\n",
3572 wvalue);
3573 }
3574 break;
3575
3576 case GetHubDescriptor:
3577 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3578 hub_desc = (struct usb_hub_descriptor *)buf;
3579 hub_desc->bDescLength = 9;
Sergei Shtylyova5dd0392015-03-29 01:36:28 +03003580 hub_desc->bDescriptorType = USB_DT_HUB;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003581 hub_desc->bNbrPorts = 1;
Sergei Shtylyov3d040de2015-01-19 01:54:15 +03003582 hub_desc->wHubCharacteristics =
3583 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
3584 HUB_CHAR_INDV_PORT_OCPM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003585 hub_desc->bPwrOn2PwrGood = 1;
3586 hub_desc->bHubContrCurrent = 0;
3587 hub_desc->u.hs.DeviceRemovable[0] = 0;
3588 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3589 break;
3590
3591 case GetHubStatus:
3592 dev_dbg(hsotg->dev, "GetHubStatus\n");
3593 memset(buf, 0, 4);
3594 break;
3595
3596 case GetPortStatus:
Paul Zimmermanb8313412013-05-24 16:32:12 -07003597 dev_vdbg(hsotg->dev,
3598 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3599 hsotg->flags.d32);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003600 if (!windex || windex > 1)
3601 goto error;
3602
3603 port_status = 0;
3604 if (hsotg->flags.b.port_connect_status_change)
3605 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3606 if (hsotg->flags.b.port_enable_change)
3607 port_status |= USB_PORT_STAT_C_ENABLE << 16;
3608 if (hsotg->flags.b.port_suspend_change)
3609 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3610 if (hsotg->flags.b.port_l1_change)
3611 port_status |= USB_PORT_STAT_C_L1 << 16;
3612 if (hsotg->flags.b.port_reset_change)
3613 port_status |= USB_PORT_STAT_C_RESET << 16;
3614 if (hsotg->flags.b.port_over_current_change) {
3615 dev_warn(hsotg->dev, "Overcurrent change detected\n");
3616 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3617 }
3618
3619 if (!hsotg->flags.b.port_connect_status) {
3620 /*
3621 * The port is disconnected, which means the core is
3622 * either in device mode or it soon will be. Just
3623 * return 0's for the remainder of the port status
3624 * since the port register can't be read if the core
3625 * is in device mode.
3626 */
3627 *(__le32 *)buf = cpu_to_le32(port_status);
3628 break;
3629 }
3630
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003631 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmermanb8313412013-05-24 16:32:12 -07003632 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003633
3634 if (hprt0 & HPRT0_CONNSTS)
3635 port_status |= USB_PORT_STAT_CONNECTION;
3636 if (hprt0 & HPRT0_ENA)
3637 port_status |= USB_PORT_STAT_ENABLE;
3638 if (hprt0 & HPRT0_SUSP)
3639 port_status |= USB_PORT_STAT_SUSPEND;
3640 if (hprt0 & HPRT0_OVRCURRACT)
3641 port_status |= USB_PORT_STAT_OVERCURRENT;
3642 if (hprt0 & HPRT0_RST)
3643 port_status |= USB_PORT_STAT_RESET;
3644 if (hprt0 & HPRT0_PWR)
3645 port_status |= USB_PORT_STAT_POWER;
3646
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02003647 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003648 if (speed == HPRT0_SPD_HIGH_SPEED)
3649 port_status |= USB_PORT_STAT_HIGH_SPEED;
3650 else if (speed == HPRT0_SPD_LOW_SPEED)
3651 port_status |= USB_PORT_STAT_LOW_SPEED;
3652
3653 if (hprt0 & HPRT0_TSTCTL_MASK)
3654 port_status |= USB_PORT_STAT_TEST;
3655 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3656
John Younbea8e862016-11-03 17:55:53 -07003657 if (hsotg->params.dma_desc_fs_enable) {
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003658 /*
3659 * Enable descriptor DMA only if a full speed
3660 * device is connected.
3661 */
3662 if (hsotg->new_connection &&
3663 ((port_status &
3664 (USB_PORT_STAT_CONNECTION |
3665 USB_PORT_STAT_HIGH_SPEED |
3666 USB_PORT_STAT_LOW_SPEED)) ==
3667 USB_PORT_STAT_CONNECTION)) {
3668 u32 hcfg;
3669
3670 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
John Youn95832c02017-01-23 14:57:26 -08003671 hsotg->params.dma_desc_enable = true;
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003672 hcfg = dwc2_readl(hsotg->regs + HCFG);
3673 hcfg |= HCFG_DESCDMA;
3674 dwc2_writel(hcfg, hsotg->regs + HCFG);
3675 hsotg->new_connection = false;
3676 }
3677 }
3678
Paul Zimmermanb8313412013-05-24 16:32:12 -07003679 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003680 *(__le32 *)buf = cpu_to_le32(port_status);
3681 break;
3682
3683 case SetHubFeature:
3684 dev_dbg(hsotg->dev, "SetHubFeature\n");
3685 /* No HUB features supported */
3686 break;
3687
3688 case SetPortFeature:
3689 dev_dbg(hsotg->dev, "SetPortFeature\n");
3690 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3691 goto error;
3692
3693 if (!hsotg->flags.b.port_connect_status) {
3694 /*
3695 * The port is disconnected, which means the core is
3696 * either in device mode or it soon will be. Just
3697 * return without doing anything since the port
3698 * register can't be written if the core is in device
3699 * mode.
3700 */
3701 break;
3702 }
3703
3704 switch (wvalue) {
3705 case USB_PORT_FEAT_SUSPEND:
3706 dev_dbg(hsotg->dev,
3707 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3708 if (windex != hsotg->otg_port)
3709 goto error;
3710 dwc2_port_suspend(hsotg, windex);
3711 break;
3712
3713 case USB_PORT_FEAT_POWER:
3714 dev_dbg(hsotg->dev,
3715 "SetPortFeature - USB_PORT_FEAT_POWER\n");
3716 hprt0 = dwc2_read_hprt0(hsotg);
3717 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003718 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003719 break;
3720
3721 case USB_PORT_FEAT_RESET:
3722 hprt0 = dwc2_read_hprt0(hsotg);
3723 dev_dbg(hsotg->dev,
3724 "SetPortFeature - USB_PORT_FEAT_RESET\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003725 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003726 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003727 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003728 /* ??? Original driver does this */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003729 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003730
3731 hprt0 = dwc2_read_hprt0(hsotg);
3732 /* Clear suspend bit if resetting from suspend state */
3733 hprt0 &= ~HPRT0_SUSP;
3734
3735 /*
3736 * When B-Host the Port reset bit is set in the Start
3737 * HCD Callback function, so that the reset is started
3738 * within 1ms of the HNP success interrupt
3739 */
3740 if (!dwc2_hcd_is_b_host(hsotg)) {
3741 hprt0 |= HPRT0_PWR | HPRT0_RST;
3742 dev_dbg(hsotg->dev,
3743 "In host mode, hprt0=%08x\n", hprt0);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003744 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003745 }
3746
3747 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003748 msleep(50);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003749 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003750 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003751 hsotg->lx_state = DWC2_L0; /* Now back to On state */
3752 break;
3753
3754 case USB_PORT_FEAT_INDICATOR:
3755 dev_dbg(hsotg->dev,
3756 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3757 /* Not supported */
3758 break;
3759
Jingwu Lin96d480e2015-04-29 22:09:17 +02003760 case USB_PORT_FEAT_TEST:
3761 hprt0 = dwc2_read_hprt0(hsotg);
3762 dev_dbg(hsotg->dev,
3763 "SetPortFeature - USB_PORT_FEAT_TEST\n");
3764 hprt0 &= ~HPRT0_TSTCTL_MASK;
3765 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003766 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Jingwu Lin96d480e2015-04-29 22:09:17 +02003767 break;
3768
Paul Zimmerman7359d482013-03-11 17:47:59 -07003769 default:
3770 retval = -EINVAL;
3771 dev_err(hsotg->dev,
3772 "SetPortFeature %1xh unknown or unsupported\n",
3773 wvalue);
3774 break;
3775 }
3776 break;
3777
3778 default:
3779error:
3780 retval = -EINVAL;
3781 dev_dbg(hsotg->dev,
3782 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3783 typereq, windex, wvalue);
3784 break;
3785 }
3786
3787 return retval;
3788}
3789
3790static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3791{
3792 int retval;
3793
Paul Zimmerman7359d482013-03-11 17:47:59 -07003794 if (port != 1)
3795 return -EINVAL;
3796
3797 retval = (hsotg->flags.b.port_connect_status_change ||
3798 hsotg->flags.b.port_reset_change ||
3799 hsotg->flags.b.port_enable_change ||
3800 hsotg->flags.b.port_suspend_change ||
3801 hsotg->flags.b.port_over_current_change);
3802
3803 if (retval) {
3804 dev_dbg(hsotg->dev,
3805 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3806 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
3807 hsotg->flags.b.port_connect_status_change);
3808 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
3809 hsotg->flags.b.port_reset_change);
3810 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
3811 hsotg->flags.b.port_enable_change);
3812 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
3813 hsotg->flags.b.port_suspend_change);
3814 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
3815 hsotg->flags.b.port_over_current_change);
3816 }
3817
3818 return retval;
3819}
3820
3821int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3822{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003823 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003824
3825#ifdef DWC2_DEBUG_SOF
3826 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003827 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003828#endif
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003829 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003830}
3831
Douglas Andersonfae4e822016-01-28 18:20:10 -08003832int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3833{
3834 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
3835 u32 hfir = dwc2_readl(hsotg->regs + HFIR);
3836 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3837 unsigned int us_per_frame;
3838 unsigned int frame_number;
3839 unsigned int remaining;
3840 unsigned int interval;
3841 unsigned int phy_clks;
3842
3843 /* High speed has 125 us per (micro) frame; others are 1 ms per */
3844 us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3845
3846 /* Extract fields */
3847 frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3848 remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3849 interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3850
3851 /*
3852 * Number of phy clocks since the last tick of the frame number after
3853 * "us" has passed.
3854 */
3855 phy_clks = (interval - remaining) +
3856 DIV_ROUND_UP(interval * us, us_per_frame);
3857
3858 return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3859}
3860
Paul Zimmerman7359d482013-03-11 17:47:59 -07003861int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3862{
Aldo Iljazi6bf2e2a2013-11-30 19:33:57 +02003863 return hsotg->op_state == OTG_STATE_B_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003864}
3865
3866static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3867 int iso_desc_count,
3868 gfp_t mem_flags)
3869{
3870 struct dwc2_hcd_urb *urb;
3871 u32 size = sizeof(*urb) + iso_desc_count *
3872 sizeof(struct dwc2_hcd_iso_packet_desc);
3873
3874 urb = kzalloc(size, mem_flags);
3875 if (urb)
3876 urb->packet_count = iso_desc_count;
3877 return urb;
3878}
3879
3880static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3881 struct dwc2_hcd_urb *urb, u8 dev_addr,
3882 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
3883{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02003884 if (dbg_perio() ||
3885 ep_type == USB_ENDPOINT_XFER_BULK ||
3886 ep_type == USB_ENDPOINT_XFER_CONTROL)
3887 dev_vdbg(hsotg->dev,
3888 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
3889 dev_addr, ep_num, ep_dir, ep_type, mps);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003890 urb->pipe_info.dev_addr = dev_addr;
3891 urb->pipe_info.ep_num = ep_num;
3892 urb->pipe_info.pipe_type = ep_type;
3893 urb->pipe_info.pipe_dir = ep_dir;
3894 urb->pipe_info.mps = mps;
3895}
3896
3897/*
3898 * NOTE: This function will be removed once the peripheral controller code
3899 * is integrated and the driver is stable
3900 */
3901void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3902{
3903#ifdef DEBUG
3904 struct dwc2_host_chan *chan;
3905 struct dwc2_hcd_urb *urb;
3906 struct dwc2_qtd *qtd;
3907 int num_channels;
3908 u32 np_tx_status;
3909 u32 p_tx_status;
3910 int i;
3911
John Younbea8e862016-11-03 17:55:53 -07003912 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003913 dev_dbg(hsotg->dev, "\n");
3914 dev_dbg(hsotg->dev,
3915 "************************************************************\n");
3916 dev_dbg(hsotg->dev, "HCD State:\n");
3917 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
3918
3919 for (i = 0; i < num_channels; i++) {
3920 chan = hsotg->hc_ptr_array[i];
3921 dev_dbg(hsotg->dev, " Channel %d:\n", i);
3922 dev_dbg(hsotg->dev,
3923 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3924 chan->dev_addr, chan->ep_num, chan->ep_is_in);
3925 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
3926 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
3927 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
3928 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
3929 chan->data_pid_start);
3930 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
3931 dev_dbg(hsotg->dev, " xfer_started: %d\n",
3932 chan->xfer_started);
3933 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
3934 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
3935 (unsigned long)chan->xfer_dma);
3936 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
3937 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
3938 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
3939 chan->halt_on_queue);
3940 dev_dbg(hsotg->dev, " halt_pending: %d\n",
3941 chan->halt_pending);
3942 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
3943 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
3944 dev_dbg(hsotg->dev, " complete_split: %d\n",
3945 chan->complete_split);
3946 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
3947 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
3948 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
3949 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
3950 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
3951
3952 if (chan->xfer_started) {
3953 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3954
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003955 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3956 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
3957 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
3958 hcint = dwc2_readl(hsotg->regs + HCINT(i));
3959 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003960 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
3961 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
3962 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
3963 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
3964 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
3965 }
3966
3967 if (!(chan->xfer_started && chan->qh))
3968 continue;
3969
3970 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3971 if (!qtd->in_process)
3972 break;
3973 urb = qtd->urb;
3974 dev_dbg(hsotg->dev, " URB Info:\n");
3975 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
3976 qtd, urb);
3977 if (urb) {
3978 dev_dbg(hsotg->dev,
3979 " Dev: %d, EP: %d %s\n",
3980 dwc2_hcd_get_dev_addr(&urb->pipe_info),
3981 dwc2_hcd_get_ep_num(&urb->pipe_info),
3982 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3983 "IN" : "OUT");
3984 dev_dbg(hsotg->dev,
3985 " Max packet size: %d\n",
3986 dwc2_hcd_get_mps(&urb->pipe_info));
3987 dev_dbg(hsotg->dev,
3988 " transfer_buffer: %p\n",
3989 urb->buf);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07003990 dev_dbg(hsotg->dev,
3991 " transfer_dma: %08lx\n",
3992 (unsigned long)urb->dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003993 dev_dbg(hsotg->dev,
3994 " transfer_buffer_length: %d\n",
3995 urb->length);
3996 dev_dbg(hsotg->dev, " actual_length: %d\n",
3997 urb->actual_length);
3998 }
3999 }
4000 }
4001
4002 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
4003 hsotg->non_periodic_channels);
4004 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
4005 hsotg->periodic_channels);
4006 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004007 np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004008 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004009 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004010 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004011 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004012 p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004013 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004014 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004015 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004016 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004017 dwc2_dump_global_registers(hsotg);
4018 dwc2_dump_host_registers(hsotg);
4019 dev_dbg(hsotg->dev,
4020 "************************************************************\n");
4021 dev_dbg(hsotg->dev, "\n");
4022#endif
4023}
4024
Paul Zimmerman7359d482013-03-11 17:47:59 -07004025struct wrapper_priv_data {
4026 struct dwc2_hsotg *hsotg;
4027};
4028
4029/* Gets the dwc2_hsotg from a usb_hcd */
4030static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4031{
4032 struct wrapper_priv_data *p;
4033
John Youn9da51972017-01-17 20:30:27 -08004034 p = (struct wrapper_priv_data *)&hcd->hcd_priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004035 return p->hsotg;
4036}
4037
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004038/**
4039 * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
4040 *
4041 * This will get the dwc2_tt structure (and ttport) associated with the given
4042 * context (which is really just a struct urb pointer).
4043 *
4044 * The first time this is called for a given TT we allocate memory for our
4045 * structure. When everyone is done and has called dwc2_host_put_tt_info()
4046 * then the refcount for the structure will go to 0 and we'll free it.
4047 *
4048 * @hsotg: The HCD state structure for the DWC OTG controller.
4049 * @qh: The QH structure.
4050 * @context: The priv pointer from a struct dwc2_hcd_urb.
4051 * @mem_flags: Flags for allocating memory.
4052 * @ttport: We'll return this device's port number here. That's used to
4053 * reference into the bitmap if we're on a multi_tt hub.
4054 *
4055 * Return: a pointer to a struct dwc2_tt. Don't forget to call
4056 * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
4057 */
4058
4059struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
4060 gfp_t mem_flags, int *ttport)
4061{
4062 struct urb *urb = context;
4063 struct dwc2_tt *dwc_tt = NULL;
4064
4065 if (urb->dev->tt) {
4066 *ttport = urb->dev->ttport;
4067
4068 dwc_tt = urb->dev->tt->hcpriv;
John Youn9da51972017-01-17 20:30:27 -08004069 if (!dwc_tt) {
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004070 size_t bitmap_size;
4071
4072 /*
4073 * For single_tt we need one schedule. For multi_tt
4074 * we need one per port.
4075 */
4076 bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
4077 sizeof(dwc_tt->periodic_bitmaps[0]);
4078 if (urb->dev->tt->multi)
4079 bitmap_size *= urb->dev->tt->hub->maxchild;
4080
4081 dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
4082 mem_flags);
John Youn9da51972017-01-17 20:30:27 -08004083 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004084 return NULL;
4085
4086 dwc_tt->usb_tt = urb->dev->tt;
4087 dwc_tt->usb_tt->hcpriv = dwc_tt;
4088 }
4089
4090 dwc_tt->refcount++;
4091 }
4092
4093 return dwc_tt;
4094}
4095
4096/**
4097 * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
4098 *
4099 * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
4100 * of the structure are done.
4101 *
4102 * It's OK to call this with NULL.
4103 *
4104 * @hsotg: The HCD state structure for the DWC OTG controller.
4105 * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
4106 */
4107void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
4108{
4109 /* Model kfree and make put of NULL a no-op */
John Youn9da51972017-01-17 20:30:27 -08004110 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004111 return;
4112
4113 WARN_ON(dwc_tt->refcount < 1);
4114
4115 dwc_tt->refcount--;
4116 if (!dwc_tt->refcount) {
4117 dwc_tt->usb_tt->hcpriv = NULL;
4118 kfree(dwc_tt);
4119 }
4120}
4121
Paul Zimmerman7359d482013-03-11 17:47:59 -07004122int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4123{
4124 struct urb *urb = context;
4125
4126 return urb->dev->speed;
4127}
4128
4129static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4130 struct urb *urb)
4131{
4132 struct usb_bus *bus = hcd_to_bus(hcd);
4133
4134 if (urb->interval)
4135 bus->bandwidth_allocated += bw / urb->interval;
4136 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4137 bus->bandwidth_isoc_reqs++;
4138 else
4139 bus->bandwidth_int_reqs++;
4140}
4141
4142static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4143 struct urb *urb)
4144{
4145 struct usb_bus *bus = hcd_to_bus(hcd);
4146
4147 if (urb->interval)
4148 bus->bandwidth_allocated -= bw / urb->interval;
4149 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4150 bus->bandwidth_isoc_reqs--;
4151 else
4152 bus->bandwidth_int_reqs--;
4153}
4154
4155/*
4156 * Sets the final status of an URB and returns it to the upper layer. Any
4157 * required cleanup of the URB is performed.
4158 *
4159 * Must be called with interrupt disabled and spinlock held
4160 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004161void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4162 int status)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004163{
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004164 struct urb *urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004165 int i;
4166
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004167 if (!qtd) {
4168 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4169 return;
4170 }
4171
4172 if (!qtd->urb) {
4173 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4174 return;
4175 }
4176
4177 urb = qtd->urb->priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004178 if (!urb) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004179 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004180 return;
4181 }
4182
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004183 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004184
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004185 if (dbg_urb(urb))
4186 dev_vdbg(hsotg->dev,
4187 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4188 __func__, urb, usb_pipedevice(urb->pipe),
4189 usb_pipeendpoint(urb->pipe),
4190 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4191 urb->actual_length);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004192
Paul Zimmerman7359d482013-03-11 17:47:59 -07004193 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004194 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004195 for (i = 0; i < urb->number_of_packets; ++i) {
4196 urb->iso_frame_desc[i].actual_length =
4197 dwc2_hcd_urb_get_iso_desc_actual_length(
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004198 qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004199 urb->iso_frame_desc[i].status =
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004200 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004201 }
4202 }
4203
Gregory Herrerofe9b1772015-09-22 15:16:51 +02004204 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4205 for (i = 0; i < urb->number_of_packets; i++)
4206 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4207 i, urb->iso_frame_desc[i].status);
4208 }
4209
Paul Zimmerman7359d482013-03-11 17:47:59 -07004210 urb->status = status;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004211 if (!status) {
4212 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4213 urb->actual_length < urb->transfer_buffer_length)
4214 urb->status = -EREMOTEIO;
4215 }
4216
4217 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4218 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4219 struct usb_host_endpoint *ep = urb->ep;
4220
4221 if (ep)
4222 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4223 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4224 urb);
4225 }
4226
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004227 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004228 urb->hcpriv = NULL;
4229 kfree(qtd->urb);
4230 qtd->urb = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004231
Paul Zimmerman7359d482013-03-11 17:47:59 -07004232 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004233}
4234
4235/*
4236 * Work queue function for starting the HCD when A-Cable is connected
4237 */
4238static void dwc2_hcd_start_func(struct work_struct *work)
4239{
4240 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4241 start_work.work);
4242
4243 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4244 dwc2_host_start(hsotg);
4245}
4246
4247/*
4248 * Reset work queue function
4249 */
4250static void dwc2_hcd_reset_func(struct work_struct *work)
4251{
4252 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4253 reset_work.work);
Douglas Anderson4a065c72015-11-20 09:06:27 -08004254 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004255 u32 hprt0;
4256
4257 dev_dbg(hsotg->dev, "USB RESET function called\n");
Douglas Anderson4a065c72015-11-20 09:06:27 -08004258
4259 spin_lock_irqsave(&hsotg->lock, flags);
4260
Paul Zimmerman7359d482013-03-11 17:47:59 -07004261 hprt0 = dwc2_read_hprt0(hsotg);
4262 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004263 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004264 hsotg->flags.b.port_reset_change = 1;
Douglas Anderson4a065c72015-11-20 09:06:27 -08004265
4266 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004267}
4268
4269/*
4270 * =========================================================================
4271 * Linux HC Driver Functions
4272 * =========================================================================
4273 */
4274
4275/*
4276 * Initializes the DWC_otg controller and its root hub and prepares it for host
4277 * mode operation. Activates the root port. Returns 0 on success and a negative
4278 * error code on failure.
4279 */
4280static int _dwc2_hcd_start(struct usb_hcd *hcd)
4281{
4282 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4283 struct usb_bus *bus = hcd_to_bus(hcd);
4284 unsigned long flags;
4285
4286 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4287
4288 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero31927b62015-09-22 15:16:41 +02004289 hsotg->lx_state = DWC2_L0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004290 hcd->state = HC_STATE_RUNNING;
Gregory Herrero31927b62015-09-22 15:16:41 +02004291 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004292
4293 if (dwc2_is_device_mode(hsotg)) {
4294 spin_unlock_irqrestore(&hsotg->lock, flags);
4295 return 0; /* why 0 ?? */
4296 }
4297
4298 dwc2_hcd_reinit(hsotg);
4299
4300 /* Initialize and connect root hub if one is not already attached */
4301 if (bus->root_hub) {
4302 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4303 /* Inform the HUB driver to resume */
4304 usb_hcd_resume_root_hub(hcd);
4305 }
4306
4307 spin_unlock_irqrestore(&hsotg->lock, flags);
4308 return 0;
4309}
4310
4311/*
4312 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4313 * stopped.
4314 */
4315static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4316{
4317 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4318 unsigned long flags;
4319
Gregory Herrero5bbf6ce2015-09-22 15:16:48 +02004320 /* Turn off all host-specific interrupts */
4321 dwc2_disable_host_interrupts(hsotg);
4322
Gregory Herrero091473a2015-09-22 15:16:46 +02004323 /* Wait for interrupt processing to finish */
4324 synchronize_irq(hcd->irq);
4325
Paul Zimmerman7359d482013-03-11 17:47:59 -07004326 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero091473a2015-09-22 15:16:46 +02004327 /* Ensure hcd is disconnected */
Douglas Anderson6a659532015-11-19 13:23:14 -08004328 dwc2_hcd_disconnect(hsotg, true);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004329 dwc2_hcd_stop(hsotg);
Gregory Herrero31927b62015-09-22 15:16:41 +02004330 hsotg->lx_state = DWC2_L3;
4331 hcd->state = HC_STATE_HALT;
4332 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004333 spin_unlock_irqrestore(&hsotg->lock, flags);
4334
4335 usleep_range(1000, 3000);
4336}
4337
Gregory Herrero99a65792015-04-29 22:09:13 +02004338static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
4339{
4340 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004341 unsigned long flags;
4342 int ret = 0;
4343 u32 hprt0;
Gregory Herrero99a65792015-04-29 22:09:13 +02004344
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004345 spin_lock_irqsave(&hsotg->lock, flags);
4346
Meng Dongyangf367b722017-08-09 10:34:09 +08004347 if (dwc2_is_device_mode(hsotg))
4348 goto unlock;
4349
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004350 if (hsotg->lx_state != DWC2_L0)
4351 goto unlock;
4352
4353 if (!HCD_HW_ACCESSIBLE(hcd))
4354 goto unlock;
4355
John Stultz866932e2017-01-09 13:10:24 -08004356 if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4357 goto unlock;
4358
John Younbea8e862016-11-03 17:55:53 -07004359 if (!hsotg->params.hibernation)
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004360 goto skip_power_saving;
4361
4362 /*
4363 * Drive USB suspend and disable port Power
4364 * if usb bus is not suspended.
4365 */
4366 if (!hsotg->bus_suspended) {
4367 hprt0 = dwc2_read_hprt0(hsotg);
4368 hprt0 |= HPRT0_SUSP;
4369 hprt0 &= ~HPRT0_PWR;
4370 dwc2_writel(hprt0, hsotg->regs + HPRT0);
4371 }
4372
4373 /* Enter hibernation */
4374 ret = dwc2_enter_hibernation(hsotg);
4375 if (ret) {
4376 if (ret != -ENOTSUPP)
4377 dev_err(hsotg->dev,
4378 "enter hibernation failed\n");
4379 goto skip_power_saving;
4380 }
4381
4382 /* Ask phy to be suspended */
4383 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4384 spin_unlock_irqrestore(&hsotg->lock, flags);
4385 usb_phy_set_suspend(hsotg->uphy, true);
4386 spin_lock_irqsave(&hsotg->lock, flags);
4387 }
4388
4389 /* After entering hibernation, hardware is no more accessible */
4390 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4391
4392skip_power_saving:
Gregory Herrero99a65792015-04-29 22:09:13 +02004393 hsotg->lx_state = DWC2_L2;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004394unlock:
4395 spin_unlock_irqrestore(&hsotg->lock, flags);
4396
4397 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004398}
4399
4400static int _dwc2_hcd_resume(struct usb_hcd *hcd)
4401{
4402 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004403 unsigned long flags;
4404 int ret = 0;
4405
4406 spin_lock_irqsave(&hsotg->lock, flags);
4407
Meng Dongyangf367b722017-08-09 10:34:09 +08004408 if (dwc2_is_device_mode(hsotg))
4409 goto unlock;
4410
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004411 if (hsotg->lx_state != DWC2_L2)
4412 goto unlock;
4413
John Younbea8e862016-11-03 17:55:53 -07004414 if (!hsotg->params.hibernation) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004415 hsotg->lx_state = DWC2_L0;
4416 goto unlock;
4417 }
4418
4419 /*
4420 * Set HW accessible bit before powering on the controller
4421 * since an interrupt may rise.
4422 */
4423 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4424
4425 /*
4426 * Enable power if not already done.
4427 * This must not be spinlocked since duration
4428 * of this call is unknown.
4429 */
4430 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4431 spin_unlock_irqrestore(&hsotg->lock, flags);
4432 usb_phy_set_suspend(hsotg->uphy, false);
4433 spin_lock_irqsave(&hsotg->lock, flags);
4434 }
4435
4436 /* Exit hibernation */
4437 ret = dwc2_exit_hibernation(hsotg, true);
4438 if (ret && (ret != -ENOTSUPP))
4439 dev_err(hsotg->dev, "exit hibernation failed\n");
Gregory Herrero99a65792015-04-29 22:09:13 +02004440
4441 hsotg->lx_state = DWC2_L0;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004442
4443 spin_unlock_irqrestore(&hsotg->lock, flags);
4444
4445 if (hsotg->bus_suspended) {
4446 spin_lock_irqsave(&hsotg->lock, flags);
4447 hsotg->flags.b.port_suspend_change = 1;
4448 spin_unlock_irqrestore(&hsotg->lock, flags);
4449 dwc2_port_resume(hsotg);
4450 } else {
Gregory Herrero5634e012015-09-22 15:16:50 +02004451 /* Wait for controller to correctly update D+/D- level */
4452 usleep_range(3000, 5000);
4453
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004454 /*
4455 * Clear Port Enable and Port Status changes.
4456 * Enable Port Power.
4457 */
4458 dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
4459 HPRT0_ENACHG, hsotg->regs + HPRT0);
4460 /* Wait for controller to detect Port Connect */
Gregory Herrero5634e012015-09-22 15:16:50 +02004461 usleep_range(5000, 7000);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004462 }
4463
4464 return ret;
4465unlock:
4466 spin_unlock_irqrestore(&hsotg->lock, flags);
4467
4468 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004469}
4470
Paul Zimmerman7359d482013-03-11 17:47:59 -07004471/* Returns the current frame number */
4472static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4473{
4474 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4475
4476 return dwc2_hcd_get_frame_number(hsotg);
4477}
4478
4479static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4480 char *fn_name)
4481{
4482#ifdef VERBOSE_DEBUG
4483 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Nicholas Mc Guireefe357f2017-01-12 17:33:26 +01004484 char *pipetype = NULL;
4485 char *speed = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004486
4487 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4488 dev_vdbg(hsotg->dev, " Device address: %d\n",
4489 usb_pipedevice(urb->pipe));
4490 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
4491 usb_pipeendpoint(urb->pipe),
4492 usb_pipein(urb->pipe) ? "IN" : "OUT");
4493
4494 switch (usb_pipetype(urb->pipe)) {
4495 case PIPE_CONTROL:
4496 pipetype = "CONTROL";
4497 break;
4498 case PIPE_BULK:
4499 pipetype = "BULK";
4500 break;
4501 case PIPE_INTERRUPT:
4502 pipetype = "INTERRUPT";
4503 break;
4504 case PIPE_ISOCHRONOUS:
4505 pipetype = "ISOCHRONOUS";
4506 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004507 }
4508
4509 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
4510 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4511 "IN" : "OUT");
4512
4513 switch (urb->dev->speed) {
4514 case USB_SPEED_HIGH:
4515 speed = "HIGH";
4516 break;
4517 case USB_SPEED_FULL:
4518 speed = "FULL";
4519 break;
4520 case USB_SPEED_LOW:
4521 speed = "LOW";
4522 break;
4523 default:
4524 speed = "UNKNOWN";
4525 break;
4526 }
4527
4528 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
4529 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
4530 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
4531 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
4532 urb->transfer_buffer_length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07004533 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
4534 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4535 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
4536 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004537 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
4538
4539 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4540 int i;
4541
4542 for (i = 0; i < urb->number_of_packets; i++) {
4543 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
4544 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
4545 urb->iso_frame_desc[i].offset,
4546 urb->iso_frame_desc[i].length);
4547 }
4548 }
4549#endif
4550}
4551
4552/*
4553 * Starts processing a USB transfer request specified by a USB Request Block
4554 * (URB). mem_flags indicates the type of memory allocation to use while
4555 * processing this URB.
4556 */
4557static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4558 gfp_t mem_flags)
4559{
4560 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4561 struct usb_host_endpoint *ep = urb->ep;
4562 struct dwc2_hcd_urb *dwc2_urb;
4563 int i;
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004564 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004565 int alloc_bandwidth = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004566 u8 ep_type = 0;
4567 u32 tflags = 0;
4568 void *buf;
4569 unsigned long flags;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004570 struct dwc2_qh *qh;
4571 bool qh_allocated = false;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004572 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004573
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004574 if (dbg_urb(urb)) {
4575 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4576 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4577 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004578
John Youn9da51972017-01-17 20:30:27 -08004579 if (!ep)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004580 return -EINVAL;
4581
4582 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4583 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4584 spin_lock_irqsave(&hsotg->lock, flags);
4585 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4586 alloc_bandwidth = 1;
4587 spin_unlock_irqrestore(&hsotg->lock, flags);
4588 }
4589
4590 switch (usb_pipetype(urb->pipe)) {
4591 case PIPE_CONTROL:
4592 ep_type = USB_ENDPOINT_XFER_CONTROL;
4593 break;
4594 case PIPE_ISOCHRONOUS:
4595 ep_type = USB_ENDPOINT_XFER_ISOC;
4596 break;
4597 case PIPE_BULK:
4598 ep_type = USB_ENDPOINT_XFER_BULK;
4599 break;
4600 case PIPE_INTERRUPT:
4601 ep_type = USB_ENDPOINT_XFER_INT;
4602 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004603 }
4604
4605 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4606 mem_flags);
4607 if (!dwc2_urb)
4608 return -ENOMEM;
4609
4610 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4611 usb_pipeendpoint(urb->pipe), ep_type,
4612 usb_pipein(urb->pipe),
4613 usb_maxpacket(urb->dev, urb->pipe,
4614 !(usb_pipein(urb->pipe))));
4615
4616 buf = urb->transfer_buffer;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004617
Paul Zimmerman7359d482013-03-11 17:47:59 -07004618 if (hcd->self.uses_dma) {
Paul Zimmerman25a49442013-07-13 14:53:53 -07004619 if (!buf && (urb->transfer_dma & 3)) {
4620 dev_err(hsotg->dev,
4621 "%s: unaligned transfer with no transfer_buffer",
4622 __func__);
4623 retval = -EINVAL;
Gregory Herrero33ad2612015-04-29 22:09:15 +02004624 goto fail0;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004625 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004626 }
4627
4628 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4629 tflags |= URB_GIVEBACK_ASAP;
4630 if (urb->transfer_flags & URB_ZERO_PACKET)
4631 tflags |= URB_SEND_ZERO_PACKET;
4632
4633 dwc2_urb->priv = urb;
4634 dwc2_urb->buf = buf;
4635 dwc2_urb->dma = urb->transfer_dma;
4636 dwc2_urb->length = urb->transfer_buffer_length;
4637 dwc2_urb->setup_packet = urb->setup_packet;
4638 dwc2_urb->setup_dma = urb->setup_dma;
4639 dwc2_urb->flags = tflags;
4640 dwc2_urb->interval = urb->interval;
4641 dwc2_urb->status = -EINPROGRESS;
4642
4643 for (i = 0; i < urb->number_of_packets; ++i)
4644 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4645 urb->iso_frame_desc[i].offset,
4646 urb->iso_frame_desc[i].length);
4647
4648 urb->hcpriv = dwc2_urb;
John Youn9da51972017-01-17 20:30:27 -08004649 qh = (struct dwc2_qh *)ep->hcpriv;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004650 /* Create QH for the endpoint if it doesn't exist */
4651 if (!qh) {
4652 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4653 if (!qh) {
4654 retval = -ENOMEM;
4655 goto fail0;
4656 }
4657 ep->hcpriv = qh;
4658 qh_allocated = true;
4659 }
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004660
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004661 qtd = kzalloc(sizeof(*qtd), mem_flags);
4662 if (!qtd) {
4663 retval = -ENOMEM;
4664 goto fail1;
4665 }
4666
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004667 spin_lock_irqsave(&hsotg->lock, flags);
4668 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004669 if (retval)
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004670 goto fail2;
4671
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004672 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4673 if (retval)
4674 goto fail3;
4675
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004676 if (alloc_bandwidth) {
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004677 dwc2_allocate_bus_bandwidth(hcd,
4678 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4679 urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004680 }
4681
Gregory Herrero33ad2612015-04-29 22:09:15 +02004682 spin_unlock_irqrestore(&hsotg->lock, flags);
4683
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004684 return 0;
4685
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004686fail3:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004687 dwc2_urb->priv = NULL;
4688 usb_hcd_unlink_urb_from_ep(hcd, urb);
Douglas Anderson16e80212016-01-28 18:19:55 -08004689 if (qh_allocated && qh->channel && qh->channel->qh == qh)
4690 qh->channel->qh = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004691fail2:
Gregory Herrero33ad2612015-04-29 22:09:15 +02004692 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004693 urb->hcpriv = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004694 kfree(qtd);
Vardan Mikayelyanb0d659022016-04-27 20:20:51 -07004695 qtd = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004696fail1:
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004697 if (qh_allocated) {
4698 struct dwc2_qtd *qtd2, *qtd2_tmp;
4699
4700 ep->hcpriv = NULL;
4701 dwc2_hcd_qh_unlink(hsotg, qh);
4702 /* Free each QTD in the QH's QTD list */
4703 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
John Youn9da51972017-01-17 20:30:27 -08004704 qtd_list_entry)
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004705 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4706 dwc2_hcd_qh_free(hsotg, qh);
4707 }
Gregory Herrero33ad2612015-04-29 22:09:15 +02004708fail0:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004709 kfree(dwc2_urb);
4710
Paul Zimmerman7359d482013-03-11 17:47:59 -07004711 return retval;
4712}
4713
4714/*
4715 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4716 */
4717static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4718 int status)
4719{
4720 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004721 int rc;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004722 unsigned long flags;
4723
4724 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4725 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4726
4727 spin_lock_irqsave(&hsotg->lock, flags);
4728
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004729 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4730 if (rc)
4731 goto out;
4732
Paul Zimmerman7359d482013-03-11 17:47:59 -07004733 if (!urb->hcpriv) {
4734 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4735 goto out;
4736 }
4737
4738 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4739
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004740 usb_hcd_unlink_urb_from_ep(hcd, urb);
4741
Paul Zimmerman7359d482013-03-11 17:47:59 -07004742 kfree(urb->hcpriv);
4743 urb->hcpriv = NULL;
4744
4745 /* Higher layer software sets URB status */
4746 spin_unlock(&hsotg->lock);
4747 usb_hcd_giveback_urb(hcd, urb, status);
4748 spin_lock(&hsotg->lock);
4749
4750 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4751 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
4752out:
4753 spin_unlock_irqrestore(&hsotg->lock, flags);
4754
4755 return rc;
4756}
4757
4758/*
4759 * Frees resources in the DWC_otg controller related to a given endpoint. Also
4760 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4761 * must already be dequeued.
4762 */
4763static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4764 struct usb_host_endpoint *ep)
4765{
4766 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4767
4768 dev_dbg(hsotg->dev,
4769 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4770 ep->desc.bEndpointAddress, ep->hcpriv);
4771 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4772}
4773
4774/*
4775 * Resets endpoint specific parameter values, in current version used to reset
4776 * the data toggle (as a WA). This function can be called from usb_clear_halt
4777 * routine.
4778 */
4779static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4780 struct usb_host_endpoint *ep)
4781{
4782 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004783 unsigned long flags;
4784
4785 dev_dbg(hsotg->dev,
4786 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4787 ep->desc.bEndpointAddress);
4788
Paul Zimmerman7359d482013-03-11 17:47:59 -07004789 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004790 dwc2_hcd_endpoint_reset(hsotg, ep);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004791 spin_unlock_irqrestore(&hsotg->lock, flags);
4792}
4793
4794/*
4795 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4796 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4797 * interrupt.
4798 *
4799 * This function is called by the USB core when an interrupt occurs
4800 */
4801static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4802{
4803 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004804
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02004805 return dwc2_handle_hcd_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004806}
4807
4808/*
4809 * Creates Status Change bitmap for the root hub and root port. The bitmap is
4810 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4811 * is the status change indicator for the single root port. Returns 1 if either
4812 * change indicator is 1, otherwise returns 0.
4813 */
4814static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4815{
4816 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4817
4818 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4819 return buf[0] != 0;
4820}
4821
4822/* Handles hub class-specific requests */
4823static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4824 u16 windex, char *buf, u16 wlength)
4825{
4826 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4827 wvalue, windex, buf, wlength);
4828 return retval;
4829}
4830
4831/* Handles hub TT buffer clear completions */
4832static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4833 struct usb_host_endpoint *ep)
4834{
4835 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4836 struct dwc2_qh *qh;
4837 unsigned long flags;
4838
4839 qh = ep->hcpriv;
4840 if (!qh)
4841 return;
4842
4843 spin_lock_irqsave(&hsotg->lock, flags);
4844 qh->tt_buffer_dirty = 0;
4845
4846 if (hsotg->flags.b.port_connect_status)
4847 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4848
4849 spin_unlock_irqrestore(&hsotg->lock, flags);
4850}
4851
Chen Yuca8b0332017-01-23 15:00:18 -08004852/*
4853 * HPRT0_SPD_HIGH_SPEED: high speed
4854 * HPRT0_SPD_FULL_SPEED: full speed
4855 */
4856static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4857{
4858 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4859
4860 if (hsotg->params.speed == speed)
4861 return;
4862
4863 hsotg->params.speed = speed;
4864 queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4865}
4866
4867static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4868{
4869 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4870
4871 if (!hsotg->params.change_speed_quirk)
4872 return;
4873
4874 /*
4875 * On removal, set speed to default high-speed.
4876 */
4877 if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4878 udev->parent->speed < USB_SPEED_HIGH) {
4879 dev_info(hsotg->dev, "Set speed to default high-speed\n");
4880 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4881 }
4882}
4883
4884static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4885{
4886 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4887
4888 if (!hsotg->params.change_speed_quirk)
4889 return 0;
4890
4891 if (udev->speed == USB_SPEED_HIGH) {
4892 dev_info(hsotg->dev, "Set speed to high-speed\n");
4893 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4894 } else if ((udev->speed == USB_SPEED_FULL ||
4895 udev->speed == USB_SPEED_LOW)) {
4896 /*
4897 * Change speed setting to full-speed if there's
4898 * a full-speed or low-speed device plugged in.
4899 */
4900 dev_info(hsotg->dev, "Set speed to full-speed\n");
4901 dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4902 }
4903
4904 return 0;
4905}
4906
Paul Zimmerman7359d482013-03-11 17:47:59 -07004907static struct hc_driver dwc2_hc_driver = {
4908 .description = "dwc2_hsotg",
4909 .product_desc = "DWC OTG Controller",
4910 .hcd_priv_size = sizeof(struct wrapper_priv_data),
4911
4912 .irq = _dwc2_hcd_irq,
Douglas Anderson8add17c2016-01-28 18:20:00 -08004913 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004914
4915 .start = _dwc2_hcd_start,
4916 .stop = _dwc2_hcd_stop,
4917 .urb_enqueue = _dwc2_hcd_urb_enqueue,
4918 .urb_dequeue = _dwc2_hcd_urb_dequeue,
4919 .endpoint_disable = _dwc2_hcd_endpoint_disable,
4920 .endpoint_reset = _dwc2_hcd_endpoint_reset,
4921 .get_frame_number = _dwc2_hcd_get_frame_number,
4922
4923 .hub_status_data = _dwc2_hcd_hub_status_data,
4924 .hub_control = _dwc2_hcd_hub_control,
4925 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
Gregory Herrero99a65792015-04-29 22:09:13 +02004926
4927 .bus_suspend = _dwc2_hcd_suspend,
4928 .bus_resume = _dwc2_hcd_resume,
Douglas Anderson3bc04e22016-01-28 18:19:53 -08004929
4930 .map_urb_for_dma = dwc2_map_urb_for_dma,
4931 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004932};
4933
4934/*
4935 * Frees secondary storage associated with the dwc2_hsotg structure contained
4936 * in the struct usb_hcd field
4937 */
4938static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
4939{
4940 u32 ahbcfg;
4941 u32 dctl;
4942 int i;
4943
4944 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
4945
4946 /* Free memory for QH/QTD lists */
4947 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08004948 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004949 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
4950 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
4951 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
4952 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
4953 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
4954
4955 /* Free memory for the host channels */
4956 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
4957 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
4958
John Youn9da51972017-01-17 20:30:27 -08004959 if (chan) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07004960 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
4961 i, chan);
4962 hsotg->hc_ptr_array[i] = NULL;
4963 kfree(chan);
4964 }
4965 }
4966
John Youn95832c02017-01-23 14:57:26 -08004967 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07004968 if (hsotg->status_buf) {
4969 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
4970 hsotg->status_buf,
4971 hsotg->status_buf_dma);
4972 hsotg->status_buf = NULL;
4973 }
4974 } else {
4975 kfree(hsotg->status_buf);
4976 hsotg->status_buf = NULL;
4977 }
4978
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004979 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004980
4981 /* Disable all interrupts */
4982 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004983 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
4984 dwc2_writel(0, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004985
Matthijs Kooijman9badec22013-08-30 18:45:21 +02004986 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004987 dctl = dwc2_readl(hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004988 dctl |= DCTL_SFTDISCON;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004989 dwc2_writel(dctl, hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004990 }
4991
4992 if (hsotg->wq_otg) {
4993 if (!cancel_work_sync(&hsotg->wf_otg))
4994 flush_workqueue(hsotg->wq_otg);
4995 destroy_workqueue(hsotg->wq_otg);
4996 }
4997
Paul Zimmerman7359d482013-03-11 17:47:59 -07004998 del_timer(&hsotg->wkp_timer);
4999}
5000
5001static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5002{
5003 /* Turn off all host-specific interrupts */
5004 dwc2_disable_host_interrupts(hsotg);
5005
5006 dwc2_hcd_free(hsotg);
5007}
5008
Matthijs Kooijman8284f932013-04-11 18:43:47 +02005009/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07005010 * Initializes the HCD. This function allocates memory for and initializes the
5011 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5012 * USB bus with the core and calls the hc_driver->start() function. It returns
5013 * a negative error on failure.
5014 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005015int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005016{
Heiner Kallweit348becd2017-01-25 23:10:51 +01005017 struct platform_device *pdev = to_platform_device(hsotg->dev);
5018 struct resource *res;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005019 struct usb_hcd *hcd;
5020 struct dwc2_host_chan *channel;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005021 u32 hcfg;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005022 int i, num_channels;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005023 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005024
Dinh Nguyenf5500ec2014-11-11 11:13:39 -06005025 if (usb_disabled())
5026 return -ENODEV;
5027
Paul Zimmermane62662c2013-03-25 17:03:35 -07005028 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005029
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005030 retval = -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005031
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005032 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005033 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005034
5035#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5036 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
5037 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5038 if (!hsotg->frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005039 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005040 hsotg->last_frame_num_array = kzalloc(
5041 sizeof(*hsotg->last_frame_num_array) *
5042 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5043 if (!hsotg->last_frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005044 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005045#endif
Douglas Anderson483bb252016-01-28 18:20:07 -08005046 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005047
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005048 /* Check if the bus driver or platform code has setup a dma_mask */
John Youn95832c02017-01-23 14:57:26 -08005049 if (hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08005050 !hsotg->dev->dma_mask) {
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005051 dev_warn(hsotg->dev,
5052 "dma_mask not set, disabling DMA\n");
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01005053 hsotg->params.host_dma = false;
John Youn95832c02017-01-23 14:57:26 -08005054 hsotg->params.dma_desc_enable = false;
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005055 }
5056
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005057 /* Set device flags indicating whether the HCD supports DMA */
John Youn95832c02017-01-23 14:57:26 -08005058 if (hsotg->params.host_dma) {
Paul Zimmerman30885312013-05-24 16:27:56 -07005059 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5060 dev_warn(hsotg->dev, "can't set DMA mask\n");
Paul Zimmerman25a49442013-07-13 14:53:53 -07005061 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5062 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005063 }
5064
Chen Yuca8b0332017-01-23 15:00:18 -08005065 if (hsotg->params.change_speed_quirk) {
5066 dwc2_hc_driver.free_dev = dwc2_free_dev;
5067 dwc2_hc_driver.reset_device = dwc2_reset_device;
5068 }
5069
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005070 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5071 if (!hcd)
5072 goto error1;
5073
John Youn95832c02017-01-23 14:57:26 -08005074 if (!hsotg->params.host_dma)
Matthijs Kooijman7de76ee2013-07-19 11:34:23 +02005075 hcd->self.uses_dma = 0;
5076
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005077 hcd->has_tt = 1;
5078
Heiner Kallweit348becd2017-01-25 23:10:51 +01005079 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5080 hcd->rsrc_start = res->start;
5081 hcd->rsrc_len = resource_size(res);
5082
John Youn9da51972017-01-17 20:30:27 -08005083 ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005084 hsotg->priv = hcd;
5085
Paul Zimmerman7359d482013-03-11 17:47:59 -07005086 /*
5087 * Disable the global interrupt until all the interrupt handlers are
5088 * installed
5089 */
5090 dwc2_disable_global_interrupts(hsotg);
5091
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005092 /* Initialize the DWC_otg core, and select the Phy type */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08005093 retval = dwc2_core_init(hsotg, true);
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005094 if (retval)
5095 goto error2;
5096
Paul Zimmerman7359d482013-03-11 17:47:59 -07005097 /* Create new workqueue and init work */
Wei Yongjun53510352013-04-12 22:41:48 +08005098 retval = -ENOMEM;
Bhaktipriya Shridharec7b1262016-07-28 13:57:29 +05305099 hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005100 if (!hsotg->wq_otg) {
5101 dev_err(hsotg->dev, "Failed to create workqueue\n");
5102 goto error2;
5103 }
5104 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5105
Kees Cooke99e88a2017-10-16 14:43:17 -07005106 timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005107
5108 /* Initialize the non-periodic schedule */
5109 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08005110 INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005111 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5112
5113 /* Initialize the periodic schedule */
5114 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5115 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5116 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5117 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5118
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005119 INIT_LIST_HEAD(&hsotg->split_order);
5120
Paul Zimmerman7359d482013-03-11 17:47:59 -07005121 /*
5122 * Create a host channel descriptor for each host channel implemented
5123 * in the controller. Initialize the channel descriptor array.
5124 */
5125 INIT_LIST_HEAD(&hsotg->free_hc_list);
John Younbea8e862016-11-03 17:55:53 -07005126 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005127 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5128
5129 for (i = 0; i < num_channels; i++) {
5130 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
John Youn9da51972017-01-17 20:30:27 -08005131 if (!channel)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005132 goto error3;
5133 channel->hc_num = i;
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005134 INIT_LIST_HEAD(&channel->split_order_list_entry);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005135 hsotg->hc_ptr_array[i] = channel;
5136 }
5137
5138 /* Initialize hsotg start work */
5139 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5140
5141 /* Initialize port reset work */
5142 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5143
5144 /*
5145 * Allocate space for storing data on status transactions. Normally no
5146 * data is sent, but this space acts as a bit bucket. This must be
5147 * done after usb_add_hcd since that function allocates the DMA buffer
5148 * pool.
5149 */
John Youn95832c02017-01-23 14:57:26 -08005150 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005151 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5152 DWC2_HCD_STATUS_BUF_SIZE,
5153 &hsotg->status_buf_dma, GFP_KERNEL);
5154 else
5155 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5156 GFP_KERNEL);
5157
5158 if (!hsotg->status_buf)
5159 goto error3;
5160
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005161 /*
5162 * Create kmem caches to handle descriptor buffers in descriptor
5163 * DMA mode.
5164 * Alignment must be set to 512 bytes.
5165 */
John Younbea8e862016-11-03 17:55:53 -07005166 if (hsotg->params.dma_desc_enable ||
5167 hsotg->params.dma_desc_fs_enable) {
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005168 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005169 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005170 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
5171 NULL);
5172 if (!hsotg->desc_gen_cache) {
5173 dev_err(hsotg->dev,
5174 "unable to create dwc2 generic desc cache\n");
5175
5176 /*
5177 * Disable descriptor dma mode since it will not be
5178 * usable.
5179 */
John Youn95832c02017-01-23 14:57:26 -08005180 hsotg->params.dma_desc_enable = false;
5181 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005182 }
5183
5184 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005185 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005186 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
5187 if (!hsotg->desc_hsisoc_cache) {
5188 dev_err(hsotg->dev,
5189 "unable to create dwc2 hs isoc desc cache\n");
5190
5191 kmem_cache_destroy(hsotg->desc_gen_cache);
5192
5193 /*
5194 * Disable descriptor dma mode since it will not be
5195 * usable.
5196 */
John Youn95832c02017-01-23 14:57:26 -08005197 hsotg->params.dma_desc_enable = false;
5198 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005199 }
5200 }
5201
Paul Zimmerman7359d482013-03-11 17:47:59 -07005202 hsotg->otg_port = 1;
5203 hsotg->frame_list = NULL;
5204 hsotg->frame_list_dma = 0;
5205 hsotg->periodic_qh_count = 0;
5206
5207 /* Initiate lx_state to L3 disconnected state */
5208 hsotg->lx_state = DWC2_L3;
5209
5210 hcd->self.otg_port = hsotg->otg_port;
5211
5212 /* Don't support SG list at this point */
5213 hcd->self.sg_tablesize = 0;
5214
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005215 if (!IS_ERR_OR_NULL(hsotg->uphy))
5216 otg_set_host(hsotg->uphy->otg, &hcd->self);
5217
Paul Zimmerman7359d482013-03-11 17:47:59 -07005218 /*
5219 * Finish generic HCD initialization and start the HCD. This function
5220 * allocates the DMA buffer pool, registers the USB bus, requests the
5221 * IRQ line, and calls hcd_start method.
5222 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005223 retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005224 if (retval < 0)
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005225 goto error4;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005226
Peter Chen3c9740a2013-11-05 10:46:02 +08005227 device_wakeup_enable(hcd->self.controller);
5228
Paul Zimmerman7359d482013-03-11 17:47:59 -07005229 dwc2_hcd_dump_state(hsotg);
5230
5231 dwc2_enable_global_interrupts(hsotg);
5232
5233 return 0;
5234
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005235error4:
5236 kmem_cache_destroy(hsotg->desc_gen_cache);
5237 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005238error3:
5239 dwc2_hcd_release(hsotg);
5240error2:
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005241 usb_put_hcd(hcd);
5242error1:
Paul Zimmerman7359d482013-03-11 17:47:59 -07005243
5244#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5245 kfree(hsotg->last_frame_num_array);
5246 kfree(hsotg->frame_num_array);
5247#endif
5248
Paul Zimmermane62662c2013-03-25 17:03:35 -07005249 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005250 return retval;
5251}
Paul Zimmerman7359d482013-03-11 17:47:59 -07005252
5253/*
5254 * Removes the HCD.
5255 * Frees memory and resources associated with the HCD and deregisters the bus.
5256 */
Paul Zimmermane62662c2013-03-25 17:03:35 -07005257void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005258{
5259 struct usb_hcd *hcd;
5260
Paul Zimmermane62662c2013-03-25 17:03:35 -07005261 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005262
5263 hcd = dwc2_hsotg_to_hcd(hsotg);
Paul Zimmermane62662c2013-03-25 17:03:35 -07005264 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005265
5266 if (!hcd) {
Paul Zimmermane62662c2013-03-25 17:03:35 -07005267 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
Paul Zimmerman7359d482013-03-11 17:47:59 -07005268 __func__);
5269 return;
5270 }
5271
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005272 if (!IS_ERR_OR_NULL(hsotg->uphy))
5273 otg_set_host(hsotg->uphy->otg, NULL);
5274
Paul Zimmerman7359d482013-03-11 17:47:59 -07005275 usb_remove_hcd(hcd);
5276 hsotg->priv = NULL;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005277
5278 kmem_cache_destroy(hsotg->desc_gen_cache);
5279 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5280
Paul Zimmerman7359d482013-03-11 17:47:59 -07005281 dwc2_hcd_release(hsotg);
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005282 usb_put_hcd(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005283
5284#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5285 kfree(hsotg->last_frame_num_array);
5286 kfree(hsotg->frame_num_array);
5287#endif
Paul Zimmerman7359d482013-03-11 17:47:59 -07005288}
John Youn58e52ff6a2016-02-23 19:54:57 -08005289
5290/**
5291 * dwc2_backup_host_registers() - Backup controller host registers.
5292 * When suspending usb bus, registers needs to be backuped
5293 * if controller power is disabled once suspended.
5294 *
5295 * @hsotg: Programming view of the DWC_otg controller
5296 */
5297int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
5298{
5299 struct dwc2_hregs_backup *hr;
5300 int i;
5301
5302 dev_dbg(hsotg->dev, "%s\n", __func__);
5303
5304 /* Backup Host regs */
5305 hr = &hsotg->hr_backup;
5306 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
5307 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
John Younbea8e862016-11-03 17:55:53 -07005308 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005309 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
5310
5311 hr->hprt0 = dwc2_read_hprt0(hsotg);
5312 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
5313 hr->valid = true;
5314
5315 return 0;
5316}
5317
5318/**
5319 * dwc2_restore_host_registers() - Restore controller host registers.
5320 * When resuming usb bus, device registers needs to be restored
5321 * if controller power were disabled.
5322 *
5323 * @hsotg: Programming view of the DWC_otg controller
5324 */
5325int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
5326{
5327 struct dwc2_hregs_backup *hr;
5328 int i;
5329
5330 dev_dbg(hsotg->dev, "%s\n", __func__);
5331
5332 /* Restore host regs */
5333 hr = &hsotg->hr_backup;
5334 if (!hr->valid) {
5335 dev_err(hsotg->dev, "%s: no host registers to restore\n",
5336 __func__);
5337 return -EINVAL;
5338 }
5339 hr->valid = false;
5340
5341 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5342 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
5343
John Younbea8e862016-11-03 17:55:53 -07005344 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005345 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
5346
5347 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
5348 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
5349 hsotg->frame_number = 0;
5350
5351 return 0;
5352}