blob: 7341d5abe8e102e4e5768afdf067f21dd0cc23f9 [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
Sevak Arakelyan376f0402018-01-24 17:43:06 +040094 if (dwc2_is_device_mode(hsotg) && hsotg->params.lpm)
95 intmsk |= GINTSTS_LPMTRANRCVD;
96
John Younb02038fa2016-02-23 19:55:00 -080097 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
98}
99
100/*
101 * Initializes the FSLSPClkSel field of the HCFG register depending on the
102 * PHY type
103 */
104static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
105{
106 u32 hcfg, val;
107
108 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
109 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
John Youn95832c02017-01-23 14:57:26 -0800110 hsotg->params.ulpi_fs_ls) ||
John Younbea8e862016-11-03 17:55:53 -0700111 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
John Younb02038fa2016-02-23 19:55:00 -0800112 /* Full speed PHY */
113 val = HCFG_FSLSPCLKSEL_48_MHZ;
114 } else {
115 /* High speed PHY running at full speed or high speed */
116 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
117 }
118
119 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
120 hcfg = dwc2_readl(hsotg->regs + HCFG);
121 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
122 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
123 dwc2_writel(hcfg, hsotg->regs + HCFG);
124}
125
126static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
127{
Bruno Herrerae35b1352017-01-31 23:25:43 -0200128 u32 usbcfg, ggpio, i2cctl;
John Younb02038fa2016-02-23 19:55:00 -0800129 int retval = 0;
130
131 /*
132 * core_init() is now called on every switch so only call the
133 * following for the first time through
134 */
135 if (select_phy) {
136 dev_dbg(hsotg->dev, "FS PHY selected\n");
137
138 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
139 if (!(usbcfg & GUSBCFG_PHYSEL)) {
140 usbcfg |= GUSBCFG_PHYSEL;
141 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
142
143 /* Reset after a PHY select */
Vardan Mikayelyan13b1f8e2018-02-16 12:56:03 +0400144 retval = dwc2_core_reset(hsotg, false);
John Younb02038fa2016-02-23 19:55:00 -0800145
146 if (retval) {
147 dev_err(hsotg->dev,
148 "%s: Reset failed, aborting", __func__);
149 return retval;
150 }
151 }
Bruno Herrerae35b1352017-01-31 23:25:43 -0200152
153 if (hsotg->params.activate_stm_fs_transceiver) {
154 ggpio = dwc2_readl(hsotg->regs + GGPIO);
155 if (!(ggpio & GGPIO_STM32_OTG_GCCFG_PWRDWN)) {
156 dev_dbg(hsotg->dev, "Activating transceiver\n");
157 /*
158 * STM32F4x9 uses the GGPIO register as general
159 * core configuration register.
160 */
161 ggpio |= GGPIO_STM32_OTG_GCCFG_PWRDWN;
162 dwc2_writel(ggpio, hsotg->regs + GGPIO);
163 }
164 }
John Younb02038fa2016-02-23 19:55:00 -0800165 }
166
167 /*
168 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
169 * do this on HNP Dev/Host mode switches (done in dev_init and
170 * host_init).
171 */
172 if (dwc2_is_host_mode(hsotg))
173 dwc2_init_fs_ls_pclk_sel(hsotg);
174
John Youn95832c02017-01-23 14:57:26 -0800175 if (hsotg->params.i2c_enable) {
John Younb02038fa2016-02-23 19:55:00 -0800176 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
177
178 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
179 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
180 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
181 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
182
183 /* Program GI2CCTL.I2CEn */
184 i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
185 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
186 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
187 i2cctl &= ~GI2CCTL_I2CEN;
188 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
189 i2cctl |= GI2CCTL_I2CEN;
190 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
191 }
192
193 return retval;
194}
195
196static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
197{
198 u32 usbcfg, usbcfg_old;
199 int retval = 0;
200
201 if (!select_phy)
202 return 0;
203
204 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
205 usbcfg_old = usbcfg;
206
207 /*
208 * HS PHY parameters. These parameters are preserved during soft reset
209 * so only program the first time. Do a soft reset immediately after
210 * setting phyif.
211 */
John Younbea8e862016-11-03 17:55:53 -0700212 switch (hsotg->params.phy_type) {
John Younb02038fa2016-02-23 19:55:00 -0800213 case DWC2_PHY_TYPE_PARAM_ULPI:
214 /* ULPI interface */
215 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
216 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
217 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
John Youn95832c02017-01-23 14:57:26 -0800218 if (hsotg->params.phy_ulpi_ddr)
John Younb02038fa2016-02-23 19:55:00 -0800219 usbcfg |= GUSBCFG_DDRSEL;
Dinh Nguyenb11633c2017-10-16 08:57:18 -0500220
221 /* Set external VBUS indicator as needed. */
222 if (hsotg->params.oc_disable)
223 usbcfg |= (GUSBCFG_ULPI_INT_VBUS_IND |
224 GUSBCFG_INDICATORPASSTHROUGH);
John Younb02038fa2016-02-23 19:55:00 -0800225 break;
226 case DWC2_PHY_TYPE_PARAM_UTMI:
227 /* UTMI+ interface */
228 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
229 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
John Younbea8e862016-11-03 17:55:53 -0700230 if (hsotg->params.phy_utmi_width == 16)
John Younb02038fa2016-02-23 19:55:00 -0800231 usbcfg |= GUSBCFG_PHYIF16;
232 break;
233 default:
234 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
235 break;
236 }
237
238 if (usbcfg != usbcfg_old) {
239 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
240
241 /* Reset after setting the PHY parameters */
Vardan Mikayelyan13b1f8e2018-02-16 12:56:03 +0400242 retval = dwc2_core_reset(hsotg, false);
John Younb02038fa2016-02-23 19:55:00 -0800243 if (retval) {
244 dev_err(hsotg->dev,
245 "%s: Reset failed, aborting", __func__);
246 return retval;
247 }
248 }
249
250 return retval;
251}
252
253static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
254{
255 u32 usbcfg;
256 int retval = 0;
257
Vardan Mikayelyan38e90022016-11-14 19:17:03 -0800258 if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
259 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
John Younbea8e862016-11-03 17:55:53 -0700260 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
Vardan Mikayelyan38e90022016-11-14 19:17:03 -0800261 /* If FS/LS mode with FS/LS PHY */
John Younb02038fa2016-02-23 19:55:00 -0800262 retval = dwc2_fs_phy_init(hsotg, select_phy);
263 if (retval)
264 return retval;
265 } else {
266 /* High speed PHY */
267 retval = dwc2_hs_phy_init(hsotg, select_phy);
268 if (retval)
269 return retval;
270 }
271
272 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
273 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
John Youn95832c02017-01-23 14:57:26 -0800274 hsotg->params.ulpi_fs_ls) {
John Younb02038fa2016-02-23 19:55:00 -0800275 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
276 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
277 usbcfg |= GUSBCFG_ULPI_FS_LS;
278 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
279 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
280 } else {
281 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
282 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
283 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
284 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
285 }
286
287 return retval;
288}
289
290static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
291{
292 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
293
294 switch (hsotg->hw_params.arch) {
295 case GHWCFG2_EXT_DMA_ARCH:
296 dev_err(hsotg->dev, "External DMA Mode not supported\n");
297 return -EINVAL;
298
299 case GHWCFG2_INT_DMA_ARCH:
300 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
John Younbea8e862016-11-03 17:55:53 -0700301 if (hsotg->params.ahbcfg != -1) {
John Younb02038fa2016-02-23 19:55:00 -0800302 ahbcfg &= GAHBCFG_CTRL_MASK;
John Younbea8e862016-11-03 17:55:53 -0700303 ahbcfg |= hsotg->params.ahbcfg &
John Younb02038fa2016-02-23 19:55:00 -0800304 ~GAHBCFG_CTRL_MASK;
305 }
306 break;
307
308 case GHWCFG2_SLAVE_ONLY_ARCH:
309 default:
310 dev_dbg(hsotg->dev, "Slave Only Mode\n");
311 break;
312 }
313
John Youn95832c02017-01-23 14:57:26 -0800314 if (hsotg->params.host_dma)
John Younb02038fa2016-02-23 19:55:00 -0800315 ahbcfg |= GAHBCFG_DMA_EN;
Razmik Karapetyan9d729a72018-01-19 14:43:27 +0400316 else
317 hsotg->params.dma_desc_enable = false;
John Younb02038fa2016-02-23 19:55:00 -0800318
319 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
320
321 return 0;
322}
323
324static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
325{
326 u32 usbcfg;
327
328 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
329 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
330
331 switch (hsotg->hw_params.op_mode) {
332 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
John Younbea8e862016-11-03 17:55:53 -0700333 if (hsotg->params.otg_cap ==
John Younb02038fa2016-02-23 19:55:00 -0800334 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
335 usbcfg |= GUSBCFG_HNPCAP;
John Younbea8e862016-11-03 17:55:53 -0700336 if (hsotg->params.otg_cap !=
John Younb02038fa2016-02-23 19:55:00 -0800337 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
338 usbcfg |= GUSBCFG_SRPCAP;
339 break;
340
341 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
342 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
343 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
John Younbea8e862016-11-03 17:55:53 -0700344 if (hsotg->params.otg_cap !=
John Younb02038fa2016-02-23 19:55:00 -0800345 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
346 usbcfg |= GUSBCFG_SRPCAP;
347 break;
348
349 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
350 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
351 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
352 default:
353 break;
354 }
355
356 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
357}
358
359/**
360 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
361 *
362 * @hsotg: Programming view of DWC_otg controller
363 */
364static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
365{
366 u32 intmsk;
367
368 dev_dbg(hsotg->dev, "%s()\n", __func__);
369
370 /* Disable all interrupts */
371 dwc2_writel(0, hsotg->regs + GINTMSK);
372 dwc2_writel(0, hsotg->regs + HAINTMSK);
373
374 /* Enable the common interrupts */
375 dwc2_enable_common_interrupts(hsotg);
376
377 /* Enable host mode interrupts without disturbing common interrupts */
378 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
379 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
380 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
381}
382
383/**
384 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
385 *
386 * @hsotg: Programming view of DWC_otg controller
387 */
388static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
389{
390 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
391
392 /* Disable host mode interrupts without disturbing common interrupts */
393 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
394 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
395 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
396}
397
398/*
399 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
400 * For system that have a total fifo depth that is smaller than the default
401 * RX + TX fifo size.
402 *
403 * @hsotg: Programming view of DWC_otg controller
404 */
405static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
406{
John Younbea8e862016-11-03 17:55:53 -0700407 struct dwc2_core_params *params = &hsotg->params;
John Younb02038fa2016-02-23 19:55:00 -0800408 struct dwc2_hw_params *hw = &hsotg->hw_params;
409 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
410
411 total_fifo_size = hw->total_fifo_size;
412 rxfsiz = params->host_rx_fifo_size;
413 nptxfsiz = params->host_nperio_tx_fifo_size;
414 ptxfsiz = params->host_perio_tx_fifo_size;
415
416 /*
417 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
418 * allocation with support for high bandwidth endpoints. Synopsys
419 * defines MPS(Max Packet size) for a periodic EP=1024, and for
420 * non-periodic as 512.
421 */
422 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
423 /*
424 * For Buffer DMA mode/Scatter Gather DMA mode
425 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
426 * with n = number of host channel.
427 * 2 * ((1024/4) + 2) = 516
428 */
429 rxfsiz = 516 + hw->host_channels;
430
431 /*
432 * min non-periodic tx fifo depth
433 * 2 * (largest non-periodic USB packet used / 4)
434 * 2 * (512/4) = 256
435 */
436 nptxfsiz = 256;
437
438 /*
439 * min periodic tx fifo depth
440 * (largest packet size*MC)/4
441 * (1024 * 3)/4 = 768
442 */
443 ptxfsiz = 768;
444
445 params->host_rx_fifo_size = rxfsiz;
446 params->host_nperio_tx_fifo_size = nptxfsiz;
447 params->host_perio_tx_fifo_size = ptxfsiz;
448 }
449
450 /*
451 * If the summation of RX, NPTX and PTX fifo sizes is still
452 * bigger than the total_fifo_size, then we have a problem.
453 *
454 * We won't be able to allocate as many endpoints. Right now,
455 * we're just printing an error message, but ideally this FIFO
456 * allocation algorithm would be improved in the future.
457 *
458 * FIXME improve this FIFO allocation algorithm.
459 */
460 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
461 dev_err(hsotg->dev, "invalid fifo sizes\n");
462}
463
464static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
465{
John Younbea8e862016-11-03 17:55:53 -0700466 struct dwc2_core_params *params = &hsotg->params;
John Younb02038fa2016-02-23 19:55:00 -0800467 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
468
469 if (!params->enable_dynamic_fifo)
470 return;
471
472 dwc2_calculate_dynamic_fifo(hsotg);
473
474 /* Rx FIFO */
475 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
476 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
477 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
478 grxfsiz |= params->host_rx_fifo_size <<
479 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
480 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
481 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
482 dwc2_readl(hsotg->regs + GRXFSIZ));
483
484 /* Non-periodic Tx FIFO */
485 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
486 dwc2_readl(hsotg->regs + GNPTXFSIZ));
487 nptxfsiz = params->host_nperio_tx_fifo_size <<
488 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
489 nptxfsiz |= params->host_rx_fifo_size <<
490 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
491 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
492 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
493 dwc2_readl(hsotg->regs + GNPTXFSIZ));
494
495 /* Periodic Tx FIFO */
496 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
497 dwc2_readl(hsotg->regs + HPTXFSIZ));
498 hptxfsiz = params->host_perio_tx_fifo_size <<
499 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
500 hptxfsiz |= (params->host_rx_fifo_size +
501 params->host_nperio_tx_fifo_size) <<
502 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
503 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
504 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
505 dwc2_readl(hsotg->regs + HPTXFSIZ));
506
John Youn95832c02017-01-23 14:57:26 -0800507 if (hsotg->params.en_multiple_tx_fifo &&
Sevak Arakelyane1f411d2017-01-23 15:01:01 -0800508 hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
John Younb02038fa2016-02-23 19:55:00 -0800509 /*
Sevak Arakelyane1f411d2017-01-23 15:01:01 -0800510 * This feature was implemented in 2.91a version
John Younb02038fa2016-02-23 19:55:00 -0800511 * Global DFIFOCFG calculation for Host mode -
512 * include RxFIFO, NPTXFIFO and HPTXFIFO
513 */
514 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
515 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
516 dfifocfg |= (params->host_rx_fifo_size +
517 params->host_nperio_tx_fifo_size +
518 params->host_perio_tx_fifo_size) <<
519 GDFIFOCFG_EPINFOBASE_SHIFT &
520 GDFIFOCFG_EPINFOBASE_MASK;
521 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
522 }
523}
524
525/**
526 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
527 * the HFIR register according to PHY type and speed
528 *
529 * @hsotg: Programming view of DWC_otg controller
530 *
531 * NOTE: The caller can modify the value of the HFIR register only after the
532 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
533 * has been set
534 */
535u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
536{
537 u32 usbcfg;
538 u32 hprt0;
539 int clock = 60; /* default value */
540
541 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
542 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
543
544 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
545 !(usbcfg & GUSBCFG_PHYIF16))
546 clock = 60;
547 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
548 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
549 clock = 48;
550 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
551 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
552 clock = 30;
553 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
554 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
555 clock = 60;
556 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
557 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
558 clock = 48;
559 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
560 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
561 clock = 48;
562 if ((usbcfg & GUSBCFG_PHYSEL) &&
563 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
564 clock = 48;
565
566 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
567 /* High speed case */
568 return 125 * clock - 1;
569
570 /* FS/LS case */
571 return 1000 * clock - 1;
572}
573
574/**
575 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
576 * buffer
577 *
578 * @core_if: Programming view of DWC_otg controller
579 * @dest: Destination buffer for the packet
580 * @bytes: Number of bytes to copy to the destination
581 */
582void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
583{
584 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
585 u32 *data_buf = (u32 *)dest;
586 int word_count = (bytes + 3) / 4;
587 int i;
588
589 /*
590 * Todo: Account for the case where dest is not dword aligned. This
591 * requires reading data from the FIFO into a u32 temp buffer, then
592 * moving it into the data buffer.
593 */
594
595 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
596
597 for (i = 0; i < word_count; i++, data_buf++)
598 *data_buf = dwc2_readl(fifo);
599}
600
Paul Zimmerman7359d482013-03-11 17:47:59 -0700601/**
602 * dwc2_dump_channel_info() - Prints the state of a host channel
603 *
604 * @hsotg: Programming view of DWC_otg controller
605 * @chan: Pointer to the channel to dump
606 *
607 * Must be called with interrupt disabled and spinlock held
608 *
609 * NOTE: This function will be removed once the peripheral controller code
610 * is integrated and the driver is stable
611 */
612static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
613 struct dwc2_host_chan *chan)
614{
615#ifdef VERBOSE_DEBUG
John Younbea8e862016-11-03 17:55:53 -0700616 int num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -0700617 struct dwc2_qh *qh;
618 u32 hcchar;
619 u32 hcsplt;
620 u32 hctsiz;
621 u32 hc_dma;
622 int i;
623
John Younb02038fa2016-02-23 19:55:00 -0800624 if (!chan)
Paul Zimmerman7359d482013-03-11 17:47:59 -0700625 return;
626
Antti Seppälä95c8bc32015-08-20 21:41:07 +0300627 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
628 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
629 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chan->hc_num));
630 hc_dma = dwc2_readl(hsotg->regs + HCDMA(chan->hc_num));
Paul Zimmerman7359d482013-03-11 17:47:59 -0700631
632 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
633 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
634 hcchar, hcsplt);
635 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
636 hctsiz, hc_dma);
637 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
638 chan->dev_addr, chan->ep_num, chan->ep_is_in);
639 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
640 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
641 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
642 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
643 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
644 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
645 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
646 (unsigned long)chan->xfer_dma);
647 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
648 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
649 dev_dbg(hsotg->dev, " NP inactive sched:\n");
650 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
651 qh_list_entry)
652 dev_dbg(hsotg->dev, " %p\n", qh);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -0800653 dev_dbg(hsotg->dev, " NP waiting sched:\n");
654 list_for_each_entry(qh, &hsotg->non_periodic_sched_waiting,
655 qh_list_entry)
656 dev_dbg(hsotg->dev, " %p\n", qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -0700657 dev_dbg(hsotg->dev, " NP active sched:\n");
658 list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
659 qh_list_entry)
660 dev_dbg(hsotg->dev, " %p\n", qh);
661 dev_dbg(hsotg->dev, " Channels:\n");
662 for (i = 0; i < num_channels; i++) {
663 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
664
665 dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
666 }
667#endif /* VERBOSE_DEBUG */
668}
669
Razmik Karapetyan4411beb2016-11-16 15:34:04 -0800670static int _dwc2_hcd_start(struct usb_hcd *hcd);
671
672static void dwc2_host_start(struct dwc2_hsotg *hsotg)
673{
674 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
675
676 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
677 _dwc2_hcd_start(hcd);
678}
679
680static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
681{
682 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
683
684 hcd->self.is_b_host = 0;
685}
686
687static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
688 int *hub_addr, int *hub_port)
689{
690 struct urb *urb = context;
691
692 if (urb->dev->tt)
693 *hub_addr = urb->dev->tt->hub->devnum;
694 else
695 *hub_addr = 0;
696 *hub_port = urb->dev->ttport;
697}
698
Paul Zimmerman7359d482013-03-11 17:47:59 -0700699/*
John Younb02038fa2016-02-23 19:55:00 -0800700 * =========================================================================
701 * Low Level Host Channel Access Functions
702 * =========================================================================
703 */
704
705static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
706 struct dwc2_host_chan *chan)
707{
708 u32 hcintmsk = HCINTMSK_CHHLTD;
709
710 switch (chan->ep_type) {
711 case USB_ENDPOINT_XFER_CONTROL:
712 case USB_ENDPOINT_XFER_BULK:
713 dev_vdbg(hsotg->dev, "control/bulk\n");
714 hcintmsk |= HCINTMSK_XFERCOMPL;
715 hcintmsk |= HCINTMSK_STALL;
716 hcintmsk |= HCINTMSK_XACTERR;
717 hcintmsk |= HCINTMSK_DATATGLERR;
718 if (chan->ep_is_in) {
719 hcintmsk |= HCINTMSK_BBLERR;
720 } else {
721 hcintmsk |= HCINTMSK_NAK;
722 hcintmsk |= HCINTMSK_NYET;
723 if (chan->do_ping)
724 hcintmsk |= HCINTMSK_ACK;
725 }
726
727 if (chan->do_split) {
728 hcintmsk |= HCINTMSK_NAK;
729 if (chan->complete_split)
730 hcintmsk |= HCINTMSK_NYET;
731 else
732 hcintmsk |= HCINTMSK_ACK;
733 }
734
735 if (chan->error_state)
736 hcintmsk |= HCINTMSK_ACK;
737 break;
738
739 case USB_ENDPOINT_XFER_INT:
740 if (dbg_perio())
741 dev_vdbg(hsotg->dev, "intr\n");
742 hcintmsk |= HCINTMSK_XFERCOMPL;
743 hcintmsk |= HCINTMSK_NAK;
744 hcintmsk |= HCINTMSK_STALL;
745 hcintmsk |= HCINTMSK_XACTERR;
746 hcintmsk |= HCINTMSK_DATATGLERR;
747 hcintmsk |= HCINTMSK_FRMOVRUN;
748
749 if (chan->ep_is_in)
750 hcintmsk |= HCINTMSK_BBLERR;
751 if (chan->error_state)
752 hcintmsk |= HCINTMSK_ACK;
753 if (chan->do_split) {
754 if (chan->complete_split)
755 hcintmsk |= HCINTMSK_NYET;
756 else
757 hcintmsk |= HCINTMSK_ACK;
758 }
759 break;
760
761 case USB_ENDPOINT_XFER_ISOC:
762 if (dbg_perio())
763 dev_vdbg(hsotg->dev, "isoc\n");
764 hcintmsk |= HCINTMSK_XFERCOMPL;
765 hcintmsk |= HCINTMSK_FRMOVRUN;
766 hcintmsk |= HCINTMSK_ACK;
767
768 if (chan->ep_is_in) {
769 hcintmsk |= HCINTMSK_XACTERR;
770 hcintmsk |= HCINTMSK_BBLERR;
771 }
772 break;
773 default:
774 dev_err(hsotg->dev, "## Unknown EP type ##\n");
775 break;
776 }
777
778 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
779 if (dbg_hc(chan))
780 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
781}
782
783static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
784 struct dwc2_host_chan *chan)
785{
786 u32 hcintmsk = HCINTMSK_CHHLTD;
787
788 /*
789 * For Descriptor DMA mode core halts the channel on AHB error.
790 * Interrupt is not required.
791 */
John Youn95832c02017-01-23 14:57:26 -0800792 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -0800793 if (dbg_hc(chan))
794 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
795 hcintmsk |= HCINTMSK_AHBERR;
796 } else {
797 if (dbg_hc(chan))
798 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
799 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
800 hcintmsk |= HCINTMSK_XFERCOMPL;
801 }
802
803 if (chan->error_state && !chan->do_split &&
804 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
805 if (dbg_hc(chan))
806 dev_vdbg(hsotg->dev, "setting ACK\n");
807 hcintmsk |= HCINTMSK_ACK;
808 if (chan->ep_is_in) {
809 hcintmsk |= HCINTMSK_DATATGLERR;
810 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
811 hcintmsk |= HCINTMSK_NAK;
812 }
813 }
814
815 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
816 if (dbg_hc(chan))
817 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
818}
819
820static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
821 struct dwc2_host_chan *chan)
822{
823 u32 intmsk;
824
John Youn95832c02017-01-23 14:57:26 -0800825 if (hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -0800826 if (dbg_hc(chan))
827 dev_vdbg(hsotg->dev, "DMA enabled\n");
828 dwc2_hc_enable_dma_ints(hsotg, chan);
829 } else {
830 if (dbg_hc(chan))
831 dev_vdbg(hsotg->dev, "DMA disabled\n");
832 dwc2_hc_enable_slave_ints(hsotg, chan);
833 }
834
835 /* Enable the top level host channel interrupt */
836 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
837 intmsk |= 1 << chan->hc_num;
838 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
839 if (dbg_hc(chan))
840 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
841
842 /* Make sure host channel interrupts are enabled */
843 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
844 intmsk |= GINTSTS_HCHINT;
845 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
846 if (dbg_hc(chan))
847 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
848}
849
850/**
851 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
852 * a specific endpoint
853 *
854 * @hsotg: Programming view of DWC_otg controller
855 * @chan: Information needed to initialize the host channel
856 *
857 * The HCCHARn register is set up with the characteristics specified in chan.
858 * Host channel interrupts that may need to be serviced while this transfer is
859 * in progress are enabled.
860 */
861static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
862{
863 u8 hc_num = chan->hc_num;
864 u32 hcintmsk;
865 u32 hcchar;
866 u32 hcsplt = 0;
867
868 if (dbg_hc(chan))
869 dev_vdbg(hsotg->dev, "%s()\n", __func__);
870
871 /* Clear old interrupt conditions for this host channel */
872 hcintmsk = 0xffffffff;
873 hcintmsk &= ~HCINTMSK_RESERVED14_31;
874 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
875
876 /* Enable channel interrupts required for this transfer */
877 dwc2_hc_enable_ints(hsotg, chan);
878
879 /*
880 * Program the HCCHARn register with the endpoint characteristics for
881 * the current transfer
882 */
883 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
884 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
885 if (chan->ep_is_in)
886 hcchar |= HCCHAR_EPDIR;
887 if (chan->speed == USB_SPEED_LOW)
888 hcchar |= HCCHAR_LSPDDEV;
889 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
890 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
891 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
892 if (dbg_hc(chan)) {
893 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
894 hc_num, hcchar);
895
896 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
897 __func__, hc_num);
898 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
899 chan->dev_addr);
900 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
901 chan->ep_num);
902 dev_vdbg(hsotg->dev, " Is In: %d\n",
903 chan->ep_is_in);
904 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
905 chan->speed == USB_SPEED_LOW);
906 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
907 chan->ep_type);
908 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
909 chan->max_packet);
910 }
911
912 /* Program the HCSPLT register for SPLITs */
913 if (chan->do_split) {
914 if (dbg_hc(chan))
915 dev_vdbg(hsotg->dev,
916 "Programming HC %d with split --> %s\n",
917 hc_num,
918 chan->complete_split ? "CSPLIT" : "SSPLIT");
919 if (chan->complete_split)
920 hcsplt |= HCSPLT_COMPSPLT;
921 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
922 HCSPLT_XACTPOS_MASK;
923 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
924 HCSPLT_HUBADDR_MASK;
925 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
926 HCSPLT_PRTADDR_MASK;
927 if (dbg_hc(chan)) {
928 dev_vdbg(hsotg->dev, " comp split %d\n",
929 chan->complete_split);
930 dev_vdbg(hsotg->dev, " xact pos %d\n",
931 chan->xact_pos);
932 dev_vdbg(hsotg->dev, " hub addr %d\n",
933 chan->hub_addr);
934 dev_vdbg(hsotg->dev, " hub port %d\n",
935 chan->hub_port);
936 dev_vdbg(hsotg->dev, " is_in %d\n",
937 chan->ep_is_in);
938 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
939 chan->max_packet);
940 dev_vdbg(hsotg->dev, " xferlen %d\n",
941 chan->xfer_len);
942 }
943 }
944
945 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
946}
947
948/**
949 * dwc2_hc_halt() - Attempts to halt a host channel
950 *
951 * @hsotg: Controller register interface
952 * @chan: Host channel to halt
953 * @halt_status: Reason for halting the channel
954 *
955 * This function should only be called in Slave mode or to abort a transfer in
956 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
957 * controller halts the channel when the transfer is complete or a condition
958 * occurs that requires application intervention.
959 *
960 * In slave mode, checks for a free request queue entry, then sets the Channel
961 * Enable and Channel Disable bits of the Host Channel Characteristics
962 * register of the specified channel to intiate the halt. If there is no free
963 * request queue entry, sets only the Channel Disable bit of the HCCHARn
964 * register to flush requests for this channel. In the latter case, sets a
965 * flag to indicate that the host channel needs to be halted when a request
966 * queue slot is open.
967 *
968 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
969 * HCCHARn register. The controller ensures there is space in the request
970 * queue before submitting the halt request.
971 *
972 * Some time may elapse before the core flushes any posted requests for this
973 * host channel and halts. The Channel Halted interrupt handler completes the
974 * deactivation of the host channel.
975 */
976void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
977 enum dwc2_halt_status halt_status)
978{
979 u32 nptxsts, hptxsts, hcchar;
980
981 if (dbg_hc(chan))
982 dev_vdbg(hsotg->dev, "%s()\n", __func__);
Minas Harutyunyana82c7ab2018-01-19 14:43:53 +0400983
984 /*
985 * In buffer DMA or external DMA mode channel can't be halted
986 * for non-split periodic channels. At the end of the next
987 * uframe/frame (in the worst case), the core generates a channel
988 * halted and disables the channel automatically.
989 */
990 if ((hsotg->params.g_dma && !hsotg->params.g_dma_desc) ||
991 hsotg->hw_params.arch == GHWCFG2_EXT_DMA_ARCH) {
992 if (!chan->do_split &&
993 (chan->ep_type == USB_ENDPOINT_XFER_ISOC ||
994 chan->ep_type == USB_ENDPOINT_XFER_INT)) {
995 dev_err(hsotg->dev, "%s() Channel can't be halted\n",
996 __func__);
997 return;
998 }
999 }
1000
John Younb02038fa2016-02-23 19:55:00 -08001001 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1002 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1003
1004 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1005 halt_status == DWC2_HC_XFER_AHB_ERR) {
1006 /*
1007 * Disable all channel interrupts except Ch Halted. The QTD
1008 * and QH state associated with this transfer has been cleared
1009 * (in the case of URB_DEQUEUE), so the channel needs to be
1010 * shut down carefully to prevent crashes.
1011 */
1012 u32 hcintmsk = HCINTMSK_CHHLTD;
1013
1014 dev_vdbg(hsotg->dev, "dequeue/error\n");
1015 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1016
1017 /*
1018 * Make sure no other interrupts besides halt are currently
1019 * pending. Handling another interrupt could cause a crash due
1020 * to the QTD and QH state.
1021 */
1022 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1023
1024 /*
1025 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1026 * even if the channel was already halted for some other
1027 * reason
1028 */
1029 chan->halt_status = halt_status;
1030
1031 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1032 if (!(hcchar & HCCHAR_CHENA)) {
1033 /*
1034 * The channel is either already halted or it hasn't
1035 * started yet. In DMA mode, the transfer may halt if
1036 * it finishes normally or a condition occurs that
1037 * requires driver intervention. Don't want to halt
1038 * the channel again. In either Slave or DMA mode,
1039 * it's possible that the transfer has been assigned
1040 * to a channel, but not started yet when an URB is
1041 * dequeued. Don't want to halt a channel that hasn't
1042 * started yet.
1043 */
1044 return;
1045 }
1046 }
1047 if (chan->halt_pending) {
1048 /*
1049 * A halt has already been issued for this channel. This might
1050 * happen when a transfer is aborted by a higher level in
1051 * the stack.
1052 */
1053 dev_vdbg(hsotg->dev,
1054 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1055 __func__, chan->hc_num);
1056 return;
1057 }
1058
1059 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1060
1061 /* No need to set the bit in DDMA for disabling the channel */
1062 /* TODO check it everywhere channel is disabled */
John Youn95832c02017-01-23 14:57:26 -08001063 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08001064 if (dbg_hc(chan))
1065 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1066 hcchar |= HCCHAR_CHENA;
1067 } else {
1068 if (dbg_hc(chan))
1069 dev_dbg(hsotg->dev, "desc DMA enabled\n");
1070 }
1071 hcchar |= HCCHAR_CHDIS;
1072
John Youn95832c02017-01-23 14:57:26 -08001073 if (!hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001074 if (dbg_hc(chan))
1075 dev_vdbg(hsotg->dev, "DMA not enabled\n");
1076 hcchar |= HCCHAR_CHENA;
1077
1078 /* Check for space in the request queue to issue the halt */
1079 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1080 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1081 dev_vdbg(hsotg->dev, "control/bulk\n");
1082 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1083 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1084 dev_vdbg(hsotg->dev, "Disabling channel\n");
1085 hcchar &= ~HCCHAR_CHENA;
1086 }
1087 } else {
1088 if (dbg_perio())
1089 dev_vdbg(hsotg->dev, "isoc/intr\n");
1090 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1091 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1092 hsotg->queuing_high_bandwidth) {
1093 if (dbg_perio())
1094 dev_vdbg(hsotg->dev, "Disabling channel\n");
1095 hcchar &= ~HCCHAR_CHENA;
1096 }
1097 }
1098 } else {
1099 if (dbg_hc(chan))
1100 dev_vdbg(hsotg->dev, "DMA enabled\n");
1101 }
1102
1103 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1104 chan->halt_status = halt_status;
1105
1106 if (hcchar & HCCHAR_CHENA) {
1107 if (dbg_hc(chan))
1108 dev_vdbg(hsotg->dev, "Channel enabled\n");
1109 chan->halt_pending = 1;
1110 chan->halt_on_queue = 0;
1111 } else {
1112 if (dbg_hc(chan))
1113 dev_vdbg(hsotg->dev, "Channel disabled\n");
1114 chan->halt_on_queue = 1;
1115 }
1116
1117 if (dbg_hc(chan)) {
1118 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1119 chan->hc_num);
1120 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1121 hcchar);
1122 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1123 chan->halt_pending);
1124 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1125 chan->halt_on_queue);
1126 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1127 chan->halt_status);
1128 }
1129}
1130
1131/**
1132 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1133 *
1134 * @hsotg: Programming view of DWC_otg controller
1135 * @chan: Identifies the host channel to clean up
1136 *
1137 * This function is normally called after a transfer is done and the host
1138 * channel is being released
1139 */
1140void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1141{
1142 u32 hcintmsk;
1143
1144 chan->xfer_started = 0;
1145
1146 list_del_init(&chan->split_order_list_entry);
1147
1148 /*
1149 * Clear channel interrupt enables and any unhandled channel interrupt
1150 * conditions
1151 */
1152 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1153 hcintmsk = 0xffffffff;
1154 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1155 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1156}
1157
1158/**
1159 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1160 * which frame a periodic transfer should occur
1161 *
1162 * @hsotg: Programming view of DWC_otg controller
1163 * @chan: Identifies the host channel to set up and its properties
1164 * @hcchar: Current value of the HCCHAR register for the specified host channel
1165 *
1166 * This function has no effect on non-periodic transfers
1167 */
1168static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1169 struct dwc2_host_chan *chan, u32 *hcchar)
1170{
1171 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1172 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1173 int host_speed;
1174 int xfer_ns;
1175 int xfer_us;
1176 int bytes_in_fifo;
1177 u16 fifo_space;
1178 u16 frame_number;
1179 u16 wire_frame;
1180
1181 /*
1182 * Try to figure out if we're an even or odd frame. If we set
1183 * even and the current frame number is even the the transfer
1184 * will happen immediately. Similar if both are odd. If one is
1185 * even and the other is odd then the transfer will happen when
1186 * the frame number ticks.
1187 *
1188 * There's a bit of a balancing act to get this right.
1189 * Sometimes we may want to send data in the current frame (AK
1190 * right away). We might want to do this if the frame number
1191 * _just_ ticked, but we might also want to do this in order
1192 * to continue a split transaction that happened late in a
1193 * microframe (so we didn't know to queue the next transfer
1194 * until the frame number had ticked). The problem is that we
1195 * need a lot of knowledge to know if there's actually still
1196 * time to send things or if it would be better to wait until
1197 * the next frame.
1198 *
1199 * We can look at how much time is left in the current frame
1200 * and make a guess about whether we'll have time to transfer.
1201 * We'll do that.
1202 */
1203
1204 /* Get speed host is running at */
1205 host_speed = (chan->speed != USB_SPEED_HIGH &&
1206 !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1207
1208 /* See how many bytes are in the periodic FIFO right now */
1209 fifo_space = (dwc2_readl(hsotg->regs + HPTXSTS) &
1210 TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1211 bytes_in_fifo = sizeof(u32) *
John Younbea8e862016-11-03 17:55:53 -07001212 (hsotg->params.host_perio_tx_fifo_size -
John Younb02038fa2016-02-23 19:55:00 -08001213 fifo_space);
1214
1215 /*
1216 * Roughly estimate bus time for everything in the periodic
1217 * queue + our new transfer. This is "rough" because we're
1218 * using a function that makes takes into account IN/OUT
1219 * and INT/ISO and we're just slamming in one value for all
1220 * transfers. This should be an over-estimate and that should
1221 * be OK, but we can probably tighten it.
1222 */
1223 xfer_ns = usb_calc_bus_time(host_speed, false, false,
1224 chan->xfer_len + bytes_in_fifo);
1225 xfer_us = NS_TO_US(xfer_ns);
1226
1227 /* See what frame number we'll be at by the time we finish */
1228 frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1229
1230 /* This is when we were scheduled to be on the wire */
1231 wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1232
1233 /*
1234 * If we'd finish _after_ the frame we're scheduled in then
1235 * it's hopeless. Just schedule right away and hope for the
1236 * best. Note that it _might_ be wise to call back into the
1237 * scheduler to pick a better frame, but this is better than
1238 * nothing.
1239 */
1240 if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1241 dwc2_sch_vdbg(hsotg,
1242 "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1243 chan->qh, wire_frame, frame_number,
1244 dwc2_frame_num_dec(frame_number,
1245 wire_frame));
1246 wire_frame = frame_number;
1247
1248 /*
1249 * We picked a different frame number; communicate this
1250 * back to the scheduler so it doesn't try to schedule
1251 * another in the same frame.
1252 *
1253 * Remember that next_active_frame is 1 before the wire
1254 * frame.
1255 */
1256 chan->qh->next_active_frame =
1257 dwc2_frame_num_dec(frame_number, 1);
1258 }
1259
1260 if (wire_frame & 1)
1261 *hcchar |= HCCHAR_ODDFRM;
1262 else
1263 *hcchar &= ~HCCHAR_ODDFRM;
1264 }
1265}
1266
1267static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1268{
1269 /* Set up the initial PID for the transfer */
1270 if (chan->speed == USB_SPEED_HIGH) {
1271 if (chan->ep_is_in) {
1272 if (chan->multi_count == 1)
1273 chan->data_pid_start = DWC2_HC_PID_DATA0;
1274 else if (chan->multi_count == 2)
1275 chan->data_pid_start = DWC2_HC_PID_DATA1;
1276 else
1277 chan->data_pid_start = DWC2_HC_PID_DATA2;
1278 } else {
1279 if (chan->multi_count == 1)
1280 chan->data_pid_start = DWC2_HC_PID_DATA0;
1281 else
1282 chan->data_pid_start = DWC2_HC_PID_MDATA;
1283 }
1284 } else {
1285 chan->data_pid_start = DWC2_HC_PID_DATA0;
1286 }
1287}
1288
1289/**
1290 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1291 * the Host Channel
1292 *
1293 * @hsotg: Programming view of DWC_otg controller
1294 * @chan: Information needed to initialize the host channel
1295 *
1296 * This function should only be called in Slave mode. For a channel associated
1297 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1298 * associated with a periodic EP, the periodic Tx FIFO is written.
1299 *
1300 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1301 * the number of bytes written to the Tx FIFO.
1302 */
1303static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1304 struct dwc2_host_chan *chan)
1305{
1306 u32 i;
1307 u32 remaining_count;
1308 u32 byte_count;
1309 u32 dword_count;
1310 u32 __iomem *data_fifo;
1311 u32 *data_buf = (u32 *)chan->xfer_buf;
1312
1313 if (dbg_hc(chan))
1314 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1315
1316 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1317
1318 remaining_count = chan->xfer_len - chan->xfer_count;
1319 if (remaining_count > chan->max_packet)
1320 byte_count = chan->max_packet;
1321 else
1322 byte_count = remaining_count;
1323
1324 dword_count = (byte_count + 3) / 4;
1325
1326 if (((unsigned long)data_buf & 0x3) == 0) {
1327 /* xfer_buf is DWORD aligned */
1328 for (i = 0; i < dword_count; i++, data_buf++)
1329 dwc2_writel(*data_buf, data_fifo);
1330 } else {
1331 /* xfer_buf is not DWORD aligned */
1332 for (i = 0; i < dword_count; i++, data_buf++) {
1333 u32 data = data_buf[0] | data_buf[1] << 8 |
1334 data_buf[2] << 16 | data_buf[3] << 24;
1335 dwc2_writel(data, data_fifo);
1336 }
1337 }
1338
1339 chan->xfer_count += byte_count;
1340 chan->xfer_buf += byte_count;
1341}
1342
1343/**
1344 * dwc2_hc_do_ping() - Starts a PING transfer
1345 *
1346 * @hsotg: Programming view of DWC_otg controller
1347 * @chan: Information needed to initialize the host channel
1348 *
1349 * This function should only be called in Slave mode. The Do Ping bit is set in
1350 * the HCTSIZ register, then the channel is enabled.
1351 */
1352static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1353 struct dwc2_host_chan *chan)
1354{
1355 u32 hcchar;
1356 u32 hctsiz;
1357
1358 if (dbg_hc(chan))
1359 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1360 chan->hc_num);
1361
1362 hctsiz = TSIZ_DOPNG;
1363 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1364 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1365
1366 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1367 hcchar |= HCCHAR_CHENA;
1368 hcchar &= ~HCCHAR_CHDIS;
1369 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1370}
1371
1372/**
1373 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1374 * channel and starts the transfer
1375 *
1376 * @hsotg: Programming view of DWC_otg controller
1377 * @chan: Information needed to initialize the host channel. The xfer_len value
1378 * may be reduced to accommodate the max widths of the XferSize and
1379 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1380 * changed to reflect the final xfer_len value.
1381 *
1382 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1383 * the caller must ensure that there is sufficient space in the request queue
1384 * and Tx Data FIFO.
1385 *
1386 * For an OUT transfer in Slave mode, it loads a data packet into the
1387 * appropriate FIFO. If necessary, additional data packets are loaded in the
1388 * Host ISR.
1389 *
1390 * For an IN transfer in Slave mode, a data packet is requested. The data
1391 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1392 * additional data packets are requested in the Host ISR.
1393 *
1394 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1395 * register along with a packet count of 1 and the channel is enabled. This
1396 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1397 * simply set to 0 since no data transfer occurs in this case.
1398 *
1399 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1400 * all the information required to perform the subsequent data transfer. In
1401 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1402 * controller performs the entire PING protocol, then starts the data
1403 * transfer.
1404 */
1405static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1406 struct dwc2_host_chan *chan)
1407{
John Younbea8e862016-11-03 17:55:53 -07001408 u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1409 u16 max_hc_pkt_count = hsotg->params.max_packet_count;
John Younb02038fa2016-02-23 19:55:00 -08001410 u32 hcchar;
1411 u32 hctsiz = 0;
1412 u16 num_packets;
1413 u32 ec_mc;
1414
1415 if (dbg_hc(chan))
1416 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1417
1418 if (chan->do_ping) {
John Youn95832c02017-01-23 14:57:26 -08001419 if (!hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001420 if (dbg_hc(chan))
1421 dev_vdbg(hsotg->dev, "ping, no DMA\n");
1422 dwc2_hc_do_ping(hsotg, chan);
1423 chan->xfer_started = 1;
1424 return;
1425 }
1426
1427 if (dbg_hc(chan))
1428 dev_vdbg(hsotg->dev, "ping, DMA\n");
1429
1430 hctsiz |= TSIZ_DOPNG;
1431 }
1432
1433 if (chan->do_split) {
1434 if (dbg_hc(chan))
1435 dev_vdbg(hsotg->dev, "split\n");
1436 num_packets = 1;
1437
1438 if (chan->complete_split && !chan->ep_is_in)
1439 /*
1440 * For CSPLIT OUT Transfer, set the size to 0 so the
1441 * core doesn't expect any data written to the FIFO
1442 */
1443 chan->xfer_len = 0;
1444 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1445 chan->xfer_len = chan->max_packet;
1446 else if (!chan->ep_is_in && chan->xfer_len > 188)
1447 chan->xfer_len = 188;
1448
1449 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1450 TSIZ_XFERSIZE_MASK;
1451
1452 /* For split set ec_mc for immediate retries */
1453 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1454 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1455 ec_mc = 3;
1456 else
1457 ec_mc = 1;
1458 } else {
1459 if (dbg_hc(chan))
1460 dev_vdbg(hsotg->dev, "no split\n");
1461 /*
1462 * Ensure that the transfer length and packet count will fit
1463 * in the widths allocated for them in the HCTSIZn register
1464 */
1465 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1466 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1467 /*
1468 * Make sure the transfer size is no larger than one
1469 * (micro)frame's worth of data. (A check was done
1470 * when the periodic transfer was accepted to ensure
1471 * that a (micro)frame's worth of data can be
1472 * programmed into a channel.)
1473 */
1474 u32 max_periodic_len =
1475 chan->multi_count * chan->max_packet;
1476
1477 if (chan->xfer_len > max_periodic_len)
1478 chan->xfer_len = max_periodic_len;
1479 } else if (chan->xfer_len > max_hc_xfer_size) {
1480 /*
1481 * Make sure that xfer_len is a multiple of max packet
1482 * size
1483 */
1484 chan->xfer_len =
1485 max_hc_xfer_size - chan->max_packet + 1;
1486 }
1487
1488 if (chan->xfer_len > 0) {
1489 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1490 chan->max_packet;
1491 if (num_packets > max_hc_pkt_count) {
1492 num_packets = max_hc_pkt_count;
1493 chan->xfer_len = num_packets * chan->max_packet;
1494 }
1495 } else {
1496 /* Need 1 packet for transfer length of 0 */
1497 num_packets = 1;
1498 }
1499
1500 if (chan->ep_is_in)
1501 /*
1502 * Always program an integral # of max packets for IN
1503 * transfers
1504 */
1505 chan->xfer_len = num_packets * chan->max_packet;
1506
1507 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1508 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1509 /*
1510 * Make sure that the multi_count field matches the
1511 * actual transfer length
1512 */
1513 chan->multi_count = num_packets;
1514
1515 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1516 dwc2_set_pid_isoc(chan);
1517
1518 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1519 TSIZ_XFERSIZE_MASK;
1520
1521 /* The ec_mc gets the multi_count for non-split */
1522 ec_mc = chan->multi_count;
1523 }
1524
1525 chan->start_pkt_count = num_packets;
1526 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1527 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1528 TSIZ_SC_MC_PID_MASK;
1529 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1530 if (dbg_hc(chan)) {
1531 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1532 hctsiz, chan->hc_num);
1533
1534 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1535 chan->hc_num);
1536 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1537 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1538 TSIZ_XFERSIZE_SHIFT);
1539 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1540 (hctsiz & TSIZ_PKTCNT_MASK) >>
1541 TSIZ_PKTCNT_SHIFT);
1542 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1543 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1544 TSIZ_SC_MC_PID_SHIFT);
1545 }
1546
John Youn95832c02017-01-23 14:57:26 -08001547 if (hsotg->params.host_dma) {
John Younb02038fa2016-02-23 19:55:00 -08001548 dwc2_writel((u32)chan->xfer_dma,
1549 hsotg->regs + HCDMA(chan->hc_num));
1550 if (dbg_hc(chan))
1551 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1552 (unsigned long)chan->xfer_dma, chan->hc_num);
1553 }
1554
1555 /* Start the split */
1556 if (chan->do_split) {
1557 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1558
1559 hcsplt |= HCSPLT_SPLTENA;
1560 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1561 }
1562
1563 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1564 hcchar &= ~HCCHAR_MULTICNT_MASK;
1565 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1566 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1567
1568 if (hcchar & HCCHAR_CHDIS)
1569 dev_warn(hsotg->dev,
1570 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1571 __func__, chan->hc_num, hcchar);
1572
1573 /* Set host channel enable after all other setup is complete */
1574 hcchar |= HCCHAR_CHENA;
1575 hcchar &= ~HCCHAR_CHDIS;
1576
1577 if (dbg_hc(chan))
1578 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1579 (hcchar & HCCHAR_MULTICNT_MASK) >>
1580 HCCHAR_MULTICNT_SHIFT);
1581
1582 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1583 if (dbg_hc(chan))
1584 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1585 chan->hc_num);
1586
1587 chan->xfer_started = 1;
1588 chan->requests++;
1589
John Youn95832c02017-01-23 14:57:26 -08001590 if (!hsotg->params.host_dma &&
John Younb02038fa2016-02-23 19:55:00 -08001591 !chan->ep_is_in && chan->xfer_len > 0)
1592 /* Load OUT packet into the appropriate Tx FIFO */
1593 dwc2_hc_write_packet(hsotg, chan);
1594}
1595
1596/**
1597 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1598 * host channel and starts the transfer in Descriptor DMA mode
1599 *
1600 * @hsotg: Programming view of DWC_otg controller
1601 * @chan: Information needed to initialize the host channel
1602 *
1603 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1604 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1605 * with micro-frame bitmap.
1606 *
1607 * Initializes HCDMA register with descriptor list address and CTD value then
1608 * starts the transfer via enabling the channel.
1609 */
1610void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1611 struct dwc2_host_chan *chan)
1612{
1613 u32 hcchar;
1614 u32 hctsiz = 0;
1615
1616 if (chan->do_ping)
1617 hctsiz |= TSIZ_DOPNG;
1618
1619 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1620 dwc2_set_pid_isoc(chan);
1621
1622 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1623 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1624 TSIZ_SC_MC_PID_MASK;
1625
1626 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1627 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1628
1629 /* Non-zero only for high-speed interrupt endpoints */
1630 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1631
1632 if (dbg_hc(chan)) {
1633 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1634 chan->hc_num);
1635 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1636 chan->data_pid_start);
1637 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1638 }
1639
1640 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1641
1642 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1643 chan->desc_list_sz, DMA_TO_DEVICE);
1644
1645 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
1646
1647 if (dbg_hc(chan))
1648 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1649 &chan->desc_list_addr, chan->hc_num);
1650
1651 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1652 hcchar &= ~HCCHAR_MULTICNT_MASK;
1653 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1654 HCCHAR_MULTICNT_MASK;
1655
1656 if (hcchar & HCCHAR_CHDIS)
1657 dev_warn(hsotg->dev,
1658 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1659 __func__, chan->hc_num, hcchar);
1660
1661 /* Set host channel enable after all other setup is complete */
1662 hcchar |= HCCHAR_CHENA;
1663 hcchar &= ~HCCHAR_CHDIS;
1664
1665 if (dbg_hc(chan))
1666 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1667 (hcchar & HCCHAR_MULTICNT_MASK) >>
1668 HCCHAR_MULTICNT_SHIFT);
1669
1670 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1671 if (dbg_hc(chan))
1672 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1673 chan->hc_num);
1674
1675 chan->xfer_started = 1;
1676 chan->requests++;
1677}
1678
1679/**
1680 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1681 * a previous call to dwc2_hc_start_transfer()
1682 *
1683 * @hsotg: Programming view of DWC_otg controller
1684 * @chan: Information needed to initialize the host channel
1685 *
1686 * The caller must ensure there is sufficient space in the request queue and Tx
1687 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1688 * the controller acts autonomously to complete transfers programmed to a host
1689 * channel.
1690 *
1691 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1692 * if there is any data remaining to be queued. For an IN transfer, another
1693 * data packet is always requested. For the SETUP phase of a control transfer,
1694 * this function does nothing.
1695 *
1696 * Return: 1 if a new request is queued, 0 if no more requests are required
1697 * for this transfer
1698 */
1699static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1700 struct dwc2_host_chan *chan)
1701{
1702 if (dbg_hc(chan))
1703 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1704 chan->hc_num);
1705
1706 if (chan->do_split)
1707 /* SPLITs always queue just once per channel */
1708 return 0;
1709
1710 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1711 /* SETUPs are queued only once since they can't be NAK'd */
1712 return 0;
1713
1714 if (chan->ep_is_in) {
1715 /*
1716 * Always queue another request for other IN transfers. If
1717 * back-to-back INs are issued and NAKs are received for both,
1718 * the driver may still be processing the first NAK when the
1719 * second NAK is received. When the interrupt handler clears
1720 * the NAK interrupt for the first NAK, the second NAK will
1721 * not be seen. So we can't depend on the NAK interrupt
1722 * handler to requeue a NAK'd request. Instead, IN requests
1723 * are issued each time this function is called. When the
1724 * transfer completes, the extra requests for the channel will
1725 * be flushed.
1726 */
1727 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1728
1729 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1730 hcchar |= HCCHAR_CHENA;
1731 hcchar &= ~HCCHAR_CHDIS;
1732 if (dbg_hc(chan))
1733 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
1734 hcchar);
1735 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1736 chan->requests++;
1737 return 1;
1738 }
1739
1740 /* OUT transfers */
1741
1742 if (chan->xfer_count < chan->xfer_len) {
1743 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1744 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1745 u32 hcchar = dwc2_readl(hsotg->regs +
1746 HCCHAR(chan->hc_num));
1747
1748 dwc2_hc_set_even_odd_frame(hsotg, chan,
1749 &hcchar);
1750 }
1751
1752 /* Load OUT packet into the appropriate Tx FIFO */
1753 dwc2_hc_write_packet(hsotg, chan);
1754 chan->requests++;
1755 return 1;
1756 }
1757
1758 return 0;
1759}
1760
1761/*
1762 * =========================================================================
1763 * HCD
1764 * =========================================================================
1765 */
1766
1767/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07001768 * Processes all the URBs in a single list of QHs. Completes them with
1769 * -ETIMEDOUT and frees the QTD.
1770 *
1771 * Must be called with interrupt disabled and spinlock held
1772 */
1773static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1774 struct list_head *qh_list)
1775{
1776 struct dwc2_qh *qh, *qh_tmp;
1777 struct dwc2_qtd *qtd, *qtd_tmp;
1778
1779 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1780 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1781 qtd_list_entry) {
Gregory Herrero2e84da62015-09-22 15:16:53 +02001782 dwc2_host_complete(hsotg, qtd, -ECONNRESET);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07001783 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001784 }
1785 }
1786}
1787
1788static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1789 struct list_head *qh_list)
1790{
1791 struct dwc2_qtd *qtd, *qtd_tmp;
1792 struct dwc2_qh *qh, *qh_tmp;
1793 unsigned long flags;
1794
1795 if (!qh_list->next)
1796 /* The list hasn't been initialized yet */
1797 return;
1798
1799 spin_lock_irqsave(&hsotg->lock, flags);
1800
1801 /* Ensure there are no QTDs or URBs left */
1802 dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1803
1804 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1805 dwc2_hcd_qh_unlink(hsotg, qh);
1806
1807 /* Free each QTD in the QH's QTD list */
1808 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1809 qtd_list_entry)
1810 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1811
Douglas Anderson16e80212016-01-28 18:19:55 -08001812 if (qh->channel && qh->channel->qh == qh)
1813 qh->channel->qh = NULL;
1814
Paul Zimmerman7359d482013-03-11 17:47:59 -07001815 spin_unlock_irqrestore(&hsotg->lock, flags);
1816 dwc2_hcd_qh_free(hsotg, qh);
1817 spin_lock_irqsave(&hsotg->lock, flags);
1818 }
1819
1820 spin_unlock_irqrestore(&hsotg->lock, flags);
1821}
1822
1823/*
1824 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1825 * and periodic schedules. The QTD associated with each URB is removed from
1826 * the schedule and freed. This function may be called when a disconnect is
1827 * detected or when the HCD is being stopped.
1828 *
1829 * Must be called with interrupt disabled and spinlock held
1830 */
1831static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1832{
1833 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08001834 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001835 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1836 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1837 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1838 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1839 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1840}
1841
1842/**
1843 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1844 *
1845 * @hsotg: Pointer to struct dwc2_hsotg
1846 */
1847void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1848{
1849 u32 hprt0;
1850
1851 if (hsotg->op_state == OTG_STATE_B_HOST) {
1852 /*
1853 * Reset the port. During a HNP mode switch the reset
1854 * needs to occur within 1ms and have a duration of at
1855 * least 50ms.
1856 */
1857 hprt0 = dwc2_read_hprt0(hsotg);
1858 hprt0 |= HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001859 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001860 }
1861
1862 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1863 msecs_to_jiffies(50));
1864}
1865
1866/* Must be called with interrupt disabled and spinlock held */
1867static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1868{
John Younbea8e862016-11-03 17:55:53 -07001869 int num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001870 struct dwc2_host_chan *channel;
1871 u32 hcchar;
1872 int i;
1873
John Youn95832c02017-01-23 14:57:26 -08001874 if (!hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07001875 /* Flush out any channel requests in slave mode */
1876 for (i = 0; i < num_channels; i++) {
1877 channel = hsotg->hc_ptr_array[i];
1878 if (!list_empty(&channel->hc_list_entry))
1879 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001880 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001881 if (hcchar & HCCHAR_CHENA) {
1882 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1883 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001884 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001885 }
1886 }
1887 }
1888
1889 for (i = 0; i < num_channels; i++) {
1890 channel = hsotg->hc_ptr_array[i];
1891 if (!list_empty(&channel->hc_list_entry))
1892 continue;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001893 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001894 if (hcchar & HCCHAR_CHENA) {
1895 /* Halt the channel */
1896 hcchar |= HCCHAR_CHDIS;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001897 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07001898 }
1899
1900 dwc2_hc_cleanup(hsotg, channel);
1901 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1902 /*
1903 * Added for Descriptor DMA to prevent channel double cleanup in
1904 * release_channel_ddma(), which is called from ep_disable when
1905 * device disconnects
1906 */
1907 channel->qh = NULL;
1908 }
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001909 /* All channels have been freed, mark them available */
John Youn95832c02017-01-23 14:57:26 -08001910 if (hsotg->params.uframe_sched) {
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001911 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07001912 hsotg->params.host_channels;
Vincent Palatin7252f1b2015-03-15 13:24:32 -07001913 } else {
1914 hsotg->non_periodic_channels = 0;
1915 hsotg->periodic_channels = 0;
1916 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07001917}
1918
1919/**
Douglas Anderson6a659532015-11-19 13:23:14 -08001920 * dwc2_hcd_connect() - Handles connect of the HCD
Paul Zimmerman7359d482013-03-11 17:47:59 -07001921 *
1922 * @hsotg: Pointer to struct dwc2_hsotg
1923 *
1924 * Must be called with interrupt disabled and spinlock held
1925 */
Douglas Anderson6a659532015-11-19 13:23:14 -08001926void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
1927{
1928 if (hsotg->lx_state != DWC2_L0)
1929 usb_hcd_resume_root_hub(hsotg->priv);
1930
1931 hsotg->flags.b.port_connect_status_change = 1;
1932 hsotg->flags.b.port_connect_status = 1;
1933}
1934
1935/**
1936 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
1937 *
1938 * @hsotg: Pointer to struct dwc2_hsotg
1939 * @force: If true, we won't try to reconnect even if we see device connected.
1940 *
1941 * Must be called with interrupt disabled and spinlock held
1942 */
1943void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
Paul Zimmerman7359d482013-03-11 17:47:59 -07001944{
1945 u32 intr;
Douglas Anderson6a659532015-11-19 13:23:14 -08001946 u32 hprt0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07001947
1948 /* Set status flags for the hub driver */
1949 hsotg->flags.b.port_connect_status_change = 1;
1950 hsotg->flags.b.port_connect_status = 0;
1951
1952 /*
1953 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1954 * interrupt mask and status bits and disabling subsequent host
1955 * channel interrupts.
1956 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001957 intr = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001958 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001959 dwc2_writel(intr, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001960 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001961 dwc2_writel(intr, hsotg->regs + GINTSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001962
1963 /*
1964 * Turn off the vbus power only if the core has transitioned to device
1965 * mode. If still in host mode, need to keep power on to detect a
1966 * reconnection.
1967 */
1968 if (dwc2_is_device_mode(hsotg)) {
1969 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1970 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03001971 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07001972 }
1973
1974 dwc2_disable_host_interrupts(hsotg);
1975 }
1976
1977 /* Respond with an error status to all URBs in the schedule */
1978 dwc2_kill_all_urbs(hsotg);
1979
1980 if (dwc2_is_host_mode(hsotg))
1981 /* Clean up any host channels that were in use */
1982 dwc2_hcd_cleanup_channels(hsotg);
1983
1984 dwc2_host_disconnect(hsotg);
Douglas Anderson6a659532015-11-19 13:23:14 -08001985
1986 /*
1987 * Add an extra check here to see if we're actually connected but
1988 * we don't have a detection interrupt pending. This can happen if:
1989 * 1. hardware sees connect
1990 * 2. hardware sees disconnect
1991 * 3. hardware sees connect
1992 * 4. dwc2_port_intr() - clears connect interrupt
1993 * 5. dwc2_handle_common_intr() - calls here
1994 *
1995 * Without the extra check here we will end calling disconnect
1996 * and won't get any future interrupts to handle the connect.
1997 */
1998 if (!force) {
1999 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
2000 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
2001 dwc2_hcd_connect(hsotg);
2002 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002003}
2004
2005/**
2006 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
2007 *
2008 * @hsotg: Pointer to struct dwc2_hsotg
2009 */
2010static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
2011{
Douglas Anderson1fb7f122015-10-22 13:05:03 -07002012 if (hsotg->bus_suspended) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002013 hsotg->flags.b.port_suspend_change = 1;
Gregory Herrerob46146d52015-01-30 09:09:26 +01002014 usb_hcd_resume_root_hub(hsotg->priv);
Gregory Herrerob46146d52015-01-30 09:09:26 +01002015 }
Douglas Anderson1fb7f122015-10-22 13:05:03 -07002016
2017 if (hsotg->lx_state == DWC2_L1)
2018 hsotg->flags.b.port_l1_change = 1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002019}
2020
2021/**
2022 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
2023 *
2024 * @hsotg: Pointer to struct dwc2_hsotg
2025 *
2026 * Must be called with interrupt disabled and spinlock held
2027 */
2028void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
2029{
2030 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
2031
2032 /*
2033 * The root hub should be disconnected before this function is called.
2034 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
2035 * and the QH lists (via ..._hcd_endpoint_disable).
2036 */
2037
2038 /* Turn off all host-specific interrupts */
2039 dwc2_disable_host_interrupts(hsotg);
2040
2041 /* Turn off the vbus power */
2042 dev_dbg(hsotg->dev, "PortPower off\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002043 dwc2_writel(0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002044}
2045
Gregory Herrero33ad2612015-04-29 22:09:15 +02002046/* Caller must hold driver lock */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002047static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002048 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002049 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002050{
Paul Zimmerman7359d482013-03-11 17:47:59 -07002051 u32 intr_mask;
2052 int retval;
Nick Hudson9f8144c2013-12-06 14:01:44 -08002053 int dev_speed;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002054
2055 if (!hsotg->flags.b.port_connect_status) {
2056 /* No longer connected */
2057 dev_err(hsotg->dev, "Not connected\n");
2058 return -ENODEV;
2059 }
2060
Nick Hudson9f8144c2013-12-06 14:01:44 -08002061 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
2062
2063 /* Some configurations cannot support LS traffic on a FS root port */
2064 if ((dev_speed == USB_SPEED_LOW) &&
2065 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
2066 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002067 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Nick Hudson9f8144c2013-12-06 14:01:44 -08002068 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
2069
2070 if (prtspd == HPRT0_SPD_FULL_SPEED)
2071 return -ENODEV;
2072 }
2073
Paul Zimmerman7359d482013-03-11 17:47:59 -07002074 if (!qtd)
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02002075 return -EINVAL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002076
2077 dwc2_hcd_qtd_init(qtd, urb);
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02002078 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002079 if (retval) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002080 dev_err(hsotg->dev,
2081 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
2082 retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002083 return retval;
2084 }
2085
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002086 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002087 if (!(intr_mask & GINTSTS_SOF)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002088 enum dwc2_transaction_type tr_type;
2089
2090 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
2091 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
2092 /*
2093 * Do not schedule SG transactions until qtd has
2094 * URB_GIVEBACK_ASAP set
2095 */
2096 return 0;
2097
Paul Zimmerman7359d482013-03-11 17:47:59 -07002098 tr_type = dwc2_hcd_select_transactions(hsotg);
2099 if (tr_type != DWC2_TRANSACTION_NONE)
2100 dwc2_hcd_queue_transactions(hsotg, tr_type);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002101 }
2102
Paul Zimmerman9bda1aa2013-11-22 16:43:45 -08002103 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002104}
2105
2106/* Must be called with interrupt disabled and spinlock held */
2107static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
2108 struct dwc2_hcd_urb *urb)
2109{
2110 struct dwc2_qh *qh;
2111 struct dwc2_qtd *urb_qtd;
2112
2113 urb_qtd = urb->qtd;
2114 if (!urb_qtd) {
2115 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
2116 return -EINVAL;
2117 }
2118
2119 qh = urb_qtd->qh;
2120 if (!qh) {
2121 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
2122 return -EINVAL;
2123 }
2124
Paul Zimmerman0d012b92013-07-13 14:53:48 -07002125 urb->priv = NULL;
2126
Paul Zimmerman7359d482013-03-11 17:47:59 -07002127 if (urb_qtd->in_process && qh->channel) {
2128 dwc2_dump_channel_info(hsotg, qh->channel);
2129
2130 /* The QTD is in process (it has been assigned to a channel) */
2131 if (hsotg->flags.b.port_connect_status)
2132 /*
2133 * If still connected (i.e. in host mode), halt the
2134 * channel so it can be used for other transfers. If
2135 * no longer connected, the host registers can't be
2136 * written to halt the channel since the core is in
2137 * device mode.
2138 */
2139 dwc2_hc_halt(hsotg, qh->channel,
2140 DWC2_HC_XFER_URB_DEQUEUE);
2141 }
2142
2143 /*
2144 * Free the QTD and clean up the associated QH. Leave the QH in the
2145 * schedule if it has any remaining QTDs.
2146 */
John Youn95832c02017-01-23 14:57:26 -08002147 if (!hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002148 u8 in_process = urb_qtd->in_process;
2149
2150 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2151 if (in_process) {
2152 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
2153 qh->channel = NULL;
2154 } else if (list_empty(&qh->qtd_list)) {
2155 dwc2_hcd_qh_unlink(hsotg, qh);
2156 }
2157 } else {
2158 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2159 }
2160
2161 return 0;
2162}
2163
2164/* Must NOT be called with interrupt disabled or spinlock held */
2165static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
2166 struct usb_host_endpoint *ep, int retry)
2167{
2168 struct dwc2_qtd *qtd, *qtd_tmp;
2169 struct dwc2_qh *qh;
2170 unsigned long flags;
2171 int rc;
2172
2173 spin_lock_irqsave(&hsotg->lock, flags);
2174
2175 qh = ep->hcpriv;
2176 if (!qh) {
2177 rc = -EINVAL;
2178 goto err;
2179 }
2180
2181 while (!list_empty(&qh->qtd_list) && retry--) {
2182 if (retry == 0) {
2183 dev_err(hsotg->dev,
2184 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
2185 rc = -EBUSY;
2186 goto err;
2187 }
2188
2189 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01002190 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002191 spin_lock_irqsave(&hsotg->lock, flags);
2192 qh = ep->hcpriv;
2193 if (!qh) {
2194 rc = -EINVAL;
2195 goto err;
2196 }
2197 }
2198
2199 dwc2_hcd_qh_unlink(hsotg, qh);
2200
2201 /* Free each QTD in the QH's QTD list */
2202 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2203 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2204
2205 ep->hcpriv = NULL;
Douglas Anderson16e80212016-01-28 18:19:55 -08002206
2207 if (qh->channel && qh->channel->qh == qh)
2208 qh->channel->qh = NULL;
2209
Paul Zimmerman7359d482013-03-11 17:47:59 -07002210 spin_unlock_irqrestore(&hsotg->lock, flags);
Douglas Anderson16e80212016-01-28 18:19:55 -08002211
Paul Zimmerman7359d482013-03-11 17:47:59 -07002212 dwc2_hcd_qh_free(hsotg, qh);
2213
2214 return 0;
2215
2216err:
2217 ep->hcpriv = NULL;
2218 spin_unlock_irqrestore(&hsotg->lock, flags);
2219
2220 return rc;
2221}
2222
2223/* Must be called with interrupt disabled and spinlock held */
2224static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2225 struct usb_host_endpoint *ep)
2226{
2227 struct dwc2_qh *qh = ep->hcpriv;
2228
2229 if (!qh)
2230 return -EINVAL;
2231
2232 qh->data_toggle = DWC2_HC_PID_DATA0;
2233
2234 return 0;
2235}
2236
John Younb02038fa2016-02-23 19:55:00 -08002237/**
2238 * dwc2_core_init() - Initializes the DWC_otg controller registers and
2239 * prepares the core for device mode or host mode operation
2240 *
2241 * @hsotg: Programming view of the DWC_otg controller
2242 * @initial_setup: If true then this is the first init for this instance.
2243 */
Vardan Mikayelyan65c9c4c2018-02-16 14:11:35 +04002244int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
John Younb02038fa2016-02-23 19:55:00 -08002245{
2246 u32 usbcfg, otgctl;
2247 int retval;
2248
2249 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2250
2251 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2252
2253 /* Set ULPI External VBUS bit if needed */
2254 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
John Youn95832c02017-01-23 14:57:26 -08002255 if (hsotg->params.phy_ulpi_ext_vbus)
John Younb02038fa2016-02-23 19:55:00 -08002256 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2257
2258 /* Set external TS Dline pulsing bit if needed */
2259 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
John Youn95832c02017-01-23 14:57:26 -08002260 if (hsotg->params.ts_dline)
John Younb02038fa2016-02-23 19:55:00 -08002261 usbcfg |= GUSBCFG_TERMSELDLPULSE;
2262
2263 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2264
2265 /*
2266 * Reset the Controller
2267 *
2268 * We only need to reset the controller if this is a re-init.
2269 * For the first init we know for sure that earlier code reset us (it
2270 * needed to in order to properly detect various parameters).
2271 */
2272 if (!initial_setup) {
Vardan Mikayelyan13b1f8e2018-02-16 12:56:03 +04002273 retval = dwc2_core_reset(hsotg, false);
John Younb02038fa2016-02-23 19:55:00 -08002274 if (retval) {
2275 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2276 __func__);
2277 return retval;
2278 }
2279 }
2280
2281 /*
2282 * This needs to happen in FS mode before any other programming occurs
2283 */
2284 retval = dwc2_phy_init(hsotg, initial_setup);
2285 if (retval)
2286 return retval;
2287
2288 /* Program the GAHBCFG Register */
2289 retval = dwc2_gahbcfg_init(hsotg);
2290 if (retval)
2291 return retval;
2292
2293 /* Program the GUSBCFG register */
2294 dwc2_gusbcfg_init(hsotg);
2295
2296 /* Program the GOTGCTL register */
2297 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2298 otgctl &= ~GOTGCTL_OTGVER;
John Younb02038fa2016-02-23 19:55:00 -08002299 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
John Younb02038fa2016-02-23 19:55:00 -08002300
2301 /* Clear the SRP success bit for FS-I2c */
2302 hsotg->srp_success = 0;
2303
2304 /* Enable common interrupts */
2305 dwc2_enable_common_interrupts(hsotg);
2306
2307 /*
2308 * Do device or host initialization based on mode during PCD and
2309 * HCD initialization
2310 */
2311 if (dwc2_is_host_mode(hsotg)) {
2312 dev_dbg(hsotg->dev, "Host Mode\n");
2313 hsotg->op_state = OTG_STATE_A_HOST;
2314 } else {
2315 dev_dbg(hsotg->dev, "Device Mode\n");
2316 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2317 }
2318
2319 return 0;
2320}
2321
2322/**
2323 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2324 * Host mode
2325 *
2326 * @hsotg: Programming view of DWC_otg controller
2327 *
2328 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2329 * request queues. Host channels are reset to ensure that they are ready for
2330 * performing transfers.
2331 */
2332static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2333{
Minas Harutyunyan92a8dd22018-01-19 14:44:20 +04002334 u32 hcfg, hfir, otgctl, usbcfg;
John Younb02038fa2016-02-23 19:55:00 -08002335
2336 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2337
Minas Harutyunyan92a8dd22018-01-19 14:44:20 +04002338 /* Set HS/FS Timeout Calibration to 7 (max available value).
2339 * The number of PHY clocks that the application programs in
2340 * this field is added to the high/full speed interpacket timeout
2341 * duration in the core to account for any additional delays
2342 * introduced by the PHY. This can be required, because the delay
2343 * introduced by the PHY in generating the linestate condition
2344 * can vary from one PHY to another.
2345 */
2346 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2347 usbcfg |= GUSBCFG_TOUTCAL(7);
2348 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2349
John Younb02038fa2016-02-23 19:55:00 -08002350 /* Restart the Phy Clock */
2351 dwc2_writel(0, hsotg->regs + PCGCTL);
2352
2353 /* Initialize Host Configuration Register */
2354 dwc2_init_fs_ls_pclk_sel(hsotg);
Vardan Mikayelyan38e90022016-11-14 19:17:03 -08002355 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
2356 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
John Younb02038fa2016-02-23 19:55:00 -08002357 hcfg = dwc2_readl(hsotg->regs + HCFG);
2358 hcfg |= HCFG_FSLSSUPP;
2359 dwc2_writel(hcfg, hsotg->regs + HCFG);
2360 }
2361
2362 /*
2363 * This bit allows dynamic reloading of the HFIR register during
2364 * runtime. This bit needs to be programmed during initial configuration
2365 * and its value must not be changed during runtime.
2366 */
John Youn95832c02017-01-23 14:57:26 -08002367 if (hsotg->params.reload_ctl) {
John Younb02038fa2016-02-23 19:55:00 -08002368 hfir = dwc2_readl(hsotg->regs + HFIR);
2369 hfir |= HFIR_RLDCTRL;
2370 dwc2_writel(hfir, hsotg->regs + HFIR);
2371 }
2372
John Youn95832c02017-01-23 14:57:26 -08002373 if (hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002374 u32 op_mode = hsotg->hw_params.op_mode;
2375
2376 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2377 !hsotg->hw_params.dma_desc_enable ||
2378 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2379 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2380 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2381 dev_err(hsotg->dev,
2382 "Hardware does not support descriptor DMA mode -\n");
2383 dev_err(hsotg->dev,
2384 "falling back to buffer DMA mode.\n");
John Youn95832c02017-01-23 14:57:26 -08002385 hsotg->params.dma_desc_enable = false;
John Younb02038fa2016-02-23 19:55:00 -08002386 } else {
2387 hcfg = dwc2_readl(hsotg->regs + HCFG);
2388 hcfg |= HCFG_DESCDMA;
2389 dwc2_writel(hcfg, hsotg->regs + HCFG);
2390 }
2391 }
2392
2393 /* Configure data FIFO sizes */
2394 dwc2_config_fifos(hsotg);
2395
2396 /* TODO - check this */
2397 /* Clear Host Set HNP Enable in the OTG Control Register */
2398 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2399 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2400 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2401
2402 /* Make sure the FIFOs are flushed */
2403 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2404 dwc2_flush_rx_fifo(hsotg);
2405
2406 /* Clear Host Set HNP Enable in the OTG Control Register */
2407 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2408 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2409 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2410
John Youn95832c02017-01-23 14:57:26 -08002411 if (!hsotg->params.dma_desc_enable) {
John Younb02038fa2016-02-23 19:55:00 -08002412 int num_channels, i;
2413 u32 hcchar;
2414
2415 /* Flush out any leftover queued requests */
John Younbea8e862016-11-03 17:55:53 -07002416 num_channels = hsotg->params.host_channels;
John Younb02038fa2016-02-23 19:55:00 -08002417 for (i = 0; i < num_channels; i++) {
2418 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2419 hcchar &= ~HCCHAR_CHENA;
2420 hcchar |= HCCHAR_CHDIS;
2421 hcchar &= ~HCCHAR_EPDIR;
2422 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2423 }
2424
2425 /* Halt all channels to put them into a known state */
2426 for (i = 0; i < num_channels; i++) {
John Younb02038fa2016-02-23 19:55:00 -08002427 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2428 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2429 hcchar &= ~HCCHAR_EPDIR;
2430 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2431 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2432 __func__, i);
Sevak Arakelyan79d6b8c2018-01-19 14:39:31 +04002433
2434 if (dwc2_hsotg_wait_bit_clear(hsotg, HCCHAR(i),
2435 HCCHAR_CHENA, 1000)) {
2436 dev_warn(hsotg->dev, "Unable to clear enable on channel %d\n",
2437 i);
2438 }
John Younb02038fa2016-02-23 19:55:00 -08002439 }
2440 }
2441
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04002442 /* Enable ACG feature in host mode, if supported */
2443 dwc2_enable_acg(hsotg);
2444
John Younb02038fa2016-02-23 19:55:00 -08002445 /* Turn on the vbus power */
2446 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2447 if (hsotg->op_state == OTG_STATE_A_HOST) {
2448 u32 hprt0 = dwc2_read_hprt0(hsotg);
2449
2450 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2451 !!(hprt0 & HPRT0_PWR));
2452 if (!(hprt0 & HPRT0_PWR)) {
2453 hprt0 |= HPRT0_PWR;
2454 dwc2_writel(hprt0, hsotg->regs + HPRT0);
2455 }
2456 }
2457
2458 dwc2_enable_host_interrupts(hsotg);
2459}
2460
Paul Zimmerman7359d482013-03-11 17:47:59 -07002461/*
2462 * Initializes dynamic portions of the DWC_otg HCD state
2463 *
2464 * Must be called with interrupt disabled and spinlock held
2465 */
2466static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2467{
2468 struct dwc2_host_chan *chan, *chan_tmp;
2469 int num_channels;
2470 int i;
2471
2472 hsotg->flags.d32 = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002473 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002474
John Youn95832c02017-01-23 14:57:26 -08002475 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002476 hsotg->available_host_channels =
John Younbea8e862016-11-03 17:55:53 -07002477 hsotg->params.host_channels;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002478 } else {
2479 hsotg->non_periodic_channels = 0;
2480 hsotg->periodic_channels = 0;
2481 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002482
2483 /*
2484 * Put all channels in the free channel list and clean up channel
2485 * states
2486 */
2487 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2488 hc_list_entry)
2489 list_del_init(&chan->hc_list_entry);
2490
John Younbea8e862016-11-03 17:55:53 -07002491 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002492 for (i = 0; i < num_channels; i++) {
2493 chan = hsotg->hc_ptr_array[i];
2494 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2495 dwc2_hc_cleanup(hsotg, chan);
2496 }
2497
2498 /* Initialize the DWC core for host mode operation */
2499 dwc2_core_host_init(hsotg);
2500}
2501
2502static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2503 struct dwc2_host_chan *chan,
2504 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2505{
2506 int hub_addr, hub_port;
2507
2508 chan->do_split = 1;
2509 chan->xact_pos = qtd->isoc_split_pos;
2510 chan->complete_split = qtd->complete_split;
2511 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2512 chan->hub_addr = (u8)hub_addr;
2513 chan->hub_port = (u8)hub_port;
2514}
2515
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002516static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2517 struct dwc2_host_chan *chan,
2518 struct dwc2_qtd *qtd)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002519{
2520 struct dwc2_hcd_urb *urb = qtd->urb;
2521 struct dwc2_hcd_iso_packet_desc *frame_desc;
2522
2523 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2524 case USB_ENDPOINT_XFER_CONTROL:
2525 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2526
2527 switch (qtd->control_phase) {
2528 case DWC2_CONTROL_SETUP:
2529 dev_vdbg(hsotg->dev, " Control setup transaction\n");
2530 chan->do_ping = 0;
2531 chan->ep_is_in = 0;
2532 chan->data_pid_start = DWC2_HC_PID_SETUP;
John Youn95832c02017-01-23 14:57:26 -08002533 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002534 chan->xfer_dma = urb->setup_dma;
2535 else
2536 chan->xfer_buf = urb->setup_packet;
2537 chan->xfer_len = 8;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002538 break;
2539
2540 case DWC2_CONTROL_DATA:
2541 dev_vdbg(hsotg->dev, " Control data transaction\n");
2542 chan->data_pid_start = qtd->data_toggle;
2543 break;
2544
2545 case DWC2_CONTROL_STATUS:
2546 /*
2547 * Direction is opposite of data direction or IN if no
2548 * data
2549 */
2550 dev_vdbg(hsotg->dev, " Control status transaction\n");
2551 if (urb->length == 0)
2552 chan->ep_is_in = 1;
2553 else
2554 chan->ep_is_in =
2555 dwc2_hcd_is_pipe_out(&urb->pipe_info);
2556 if (chan->ep_is_in)
2557 chan->do_ping = 0;
2558 chan->data_pid_start = DWC2_HC_PID_DATA1;
2559 chan->xfer_len = 0;
John Youn95832c02017-01-23 14:57:26 -08002560 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002561 chan->xfer_dma = hsotg->status_buf_dma;
2562 else
2563 chan->xfer_buf = hsotg->status_buf;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002564 break;
2565 }
2566 break;
2567
2568 case USB_ENDPOINT_XFER_BULK:
2569 chan->ep_type = USB_ENDPOINT_XFER_BULK;
2570 break;
2571
2572 case USB_ENDPOINT_XFER_INT:
2573 chan->ep_type = USB_ENDPOINT_XFER_INT;
2574 break;
2575
2576 case USB_ENDPOINT_XFER_ISOC:
2577 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
John Youn95832c02017-01-23 14:57:26 -08002578 if (hsotg->params.dma_desc_enable)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002579 break;
2580
2581 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2582 frame_desc->status = 0;
2583
John Youn95832c02017-01-23 14:57:26 -08002584 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002585 chan->xfer_dma = urb->dma;
2586 chan->xfer_dma += frame_desc->offset +
2587 qtd->isoc_split_offset;
2588 } else {
2589 chan->xfer_buf = urb->buf;
2590 chan->xfer_buf += frame_desc->offset +
2591 qtd->isoc_split_offset;
2592 }
2593
2594 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2595
Paul Zimmerman7359d482013-03-11 17:47:59 -07002596 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2597 if (chan->xfer_len <= 188)
2598 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2599 else
2600 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2601 }
2602 break;
2603 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002604}
2605
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002606#define DWC2_USB_DMA_ALIGN 4
2607
2608struct dma_aligned_buffer {
2609 void *kmalloc_ptr;
2610 void *old_xfer_buffer;
2611 u8 data[0];
2612};
2613
2614static void dwc2_free_dma_aligned_buffer(struct urb *urb)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002615{
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002616 struct dma_aligned_buffer *temp;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002617
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002618 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2619 return;
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002620
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002621 temp = container_of(urb->transfer_buffer,
John Youn9da51972017-01-17 20:30:27 -08002622 struct dma_aligned_buffer, data);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002623
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002624 if (usb_urb_dir_in(urb))
2625 memcpy(temp->old_xfer_buffer, temp->data,
2626 urb->transfer_buffer_length);
2627 urb->transfer_buffer = temp->old_xfer_buffer;
2628 kfree(temp->kmalloc_ptr);
Paul Zimmerman5dce9552014-09-16 13:47:27 -07002629
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002630 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2631}
Paul Zimmerman7359d482013-03-11 17:47:59 -07002632
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002633static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
2634{
2635 struct dma_aligned_buffer *temp, *kmalloc_ptr;
2636 size_t kmalloc_size;
Gregory Herrerodb62b9a2015-04-29 22:09:16 +02002637
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002638 if (urb->num_sgs || urb->sg ||
2639 urb->transfer_buffer_length == 0 ||
2640 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2641 return 0;
2642
2643 /* Allocate a buffer with enough padding for alignment */
2644 kmalloc_size = urb->transfer_buffer_length +
2645 sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
2646
2647 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2648 if (!kmalloc_ptr)
2649 return -ENOMEM;
2650
2651 /* Position our struct dma_aligned_buffer such that data is aligned */
2652 temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
2653 temp->kmalloc_ptr = kmalloc_ptr;
2654 temp->old_xfer_buffer = urb->transfer_buffer;
2655 if (usb_urb_dir_out(urb))
2656 memcpy(temp->data, urb->transfer_buffer,
2657 urb->transfer_buffer_length);
2658 urb->transfer_buffer = temp->data;
2659
2660 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2661
Paul Zimmerman7359d482013-03-11 17:47:59 -07002662 return 0;
2663}
2664
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002665static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
John Youn9da51972017-01-17 20:30:27 -08002666 gfp_t mem_flags)
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002667{
2668 int ret;
2669
2670 /* We assume setup_dma is always aligned; warn if not */
2671 WARN_ON_ONCE(urb->setup_dma &&
2672 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
2673
2674 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
2675 if (ret)
2676 return ret;
2677
2678 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2679 if (ret)
2680 dwc2_free_dma_aligned_buffer(urb);
2681
2682 return ret;
2683}
2684
2685static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2686{
2687 usb_hcd_unmap_urb_for_dma(hcd, urb);
2688 dwc2_free_dma_aligned_buffer(urb);
2689}
2690
Paul Zimmerman7359d482013-03-11 17:47:59 -07002691/**
2692 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2693 * channel and initializes the host channel to perform the transactions. The
2694 * host channel is removed from the free list.
2695 *
2696 * @hsotg: The HCD state structure
2697 * @qh: Transactions from the first QTD for this QH are selected and assigned
2698 * to a free host channel
2699 */
Dom Cobley20f2eb92013-09-23 14:23:34 -07002700static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002701{
2702 struct dwc2_host_chan *chan;
2703 struct dwc2_hcd_urb *urb;
2704 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002705
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002706 if (dbg_qh(qh))
2707 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002708
2709 if (list_empty(&qh->qtd_list)) {
2710 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002711 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002712 }
2713
2714 if (list_empty(&hsotg->free_hc_list)) {
2715 dev_dbg(hsotg->dev, "No free channel to assign\n");
Dom Cobley20f2eb92013-09-23 14:23:34 -07002716 return -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002717 }
2718
2719 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2720 hc_list_entry);
2721
Dom Cobley20f2eb92013-09-23 14:23:34 -07002722 /* Remove host channel from free list */
Paul Zimmerman7359d482013-03-11 17:47:59 -07002723 list_del_init(&chan->hc_list_entry);
2724
2725 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2726 urb = qtd->urb;
2727 qh->channel = chan;
2728 qtd->in_process = 1;
2729
2730 /*
2731 * Use usb_pipedevice to determine device address. This address is
2732 * 0 before the SET_ADDRESS command and the correct address afterward.
2733 */
2734 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2735 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2736 chan->speed = qh->dev_speed;
2737 chan->max_packet = dwc2_max_packet(qh->maxp);
2738
2739 chan->xfer_started = 0;
2740 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2741 chan->error_state = (qtd->error_count > 0);
2742 chan->halt_on_queue = 0;
2743 chan->halt_pending = 0;
2744 chan->requests = 0;
2745
2746 /*
2747 * The following values may be modified in the transfer type section
2748 * below. The xfer_len value may be reduced when the transfer is
2749 * started to accommodate the max widths of the XferSize and PktCnt
2750 * fields in the HCTSIZn register.
2751 */
2752
2753 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2754 if (chan->ep_is_in)
2755 chan->do_ping = 0;
2756 else
2757 chan->do_ping = qh->ping_state;
2758
2759 chan->data_pid_start = qh->data_toggle;
2760 chan->multi_count = 1;
2761
Rashika Kheriabb6c3422013-10-26 23:11:22 +05302762 if (urb->actual_length > urb->length &&
John Youn9da51972017-01-17 20:30:27 -08002763 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
Paul Zimmerman84181082013-09-23 14:23:33 -07002764 urb->actual_length = urb->length;
2765
John Youn95832c02017-01-23 14:57:26 -08002766 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07002767 chan->xfer_dma = urb->dma + urb->actual_length;
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002768 else
Paul Zimmerman7359d482013-03-11 17:47:59 -07002769 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002770
2771 chan->xfer_len = urb->length - urb->actual_length;
2772 chan->xfer_count = 0;
2773
2774 /* Set the split attributes if required */
2775 if (qh->do_split)
2776 dwc2_hc_init_split(hsotg, chan, qtd, urb);
2777 else
2778 chan->do_split = 0;
2779
2780 /* Set the transfer attributes */
Douglas Anderson3bc04e22016-01-28 18:19:53 -08002781 dwc2_hc_init_xfer(hsotg, chan, qtd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002782
2783 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2784 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2785 /*
2786 * This value may be modified when the transfer is started
2787 * to reflect the actual transfer length
2788 */
2789 chan->multi_count = dwc2_hb_mult(qh->maxp);
2790
John Youn95832c02017-01-23 14:57:26 -08002791 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002792 chan->desc_list_addr = qh->desc_list_dma;
Gregory Herrero95105a92015-11-20 11:49:29 +01002793 chan->desc_list_sz = qh->desc_list_sz;
2794 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002795
2796 dwc2_hc_init(hsotg, chan);
2797 chan->qh = qh;
Dom Cobley20f2eb92013-09-23 14:23:34 -07002798
2799 return 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002800}
2801
2802/**
2803 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2804 * schedule and assigns them to available host channels. Called from the HCD
2805 * interrupt handler functions.
2806 *
2807 * @hsotg: The HCD state structure
2808 *
2809 * Return: The types of new transactions that were assigned to host channels
2810 */
2811enum dwc2_transaction_type dwc2_hcd_select_transactions(
2812 struct dwc2_hsotg *hsotg)
2813{
2814 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2815 struct list_head *qh_ptr;
2816 struct dwc2_qh *qh;
2817 int num_channels;
2818
2819#ifdef DWC2_DEBUG_SOF
2820 dev_vdbg(hsotg->dev, " Select Transactions\n");
2821#endif
2822
2823 /* Process entries in the periodic ready list */
2824 qh_ptr = hsotg->periodic_sched_ready.next;
2825 while (qh_ptr != &hsotg->periodic_sched_ready) {
2826 if (list_empty(&hsotg->free_hc_list))
2827 break;
John Youn95832c02017-01-23 14:57:26 -08002828 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002829 if (hsotg->available_host_channels <= 1)
2830 break;
2831 hsotg->available_host_channels--;
2832 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07002833 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
Dom Cobley20f2eb92013-09-23 14:23:34 -07002834 if (dwc2_assign_and_init_hc(hsotg, qh))
2835 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002836
2837 /*
2838 * Move the QH from the periodic ready schedule to the
2839 * periodic assigned schedule
2840 */
2841 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002842 list_move_tail(&qh->qh_list_entry,
2843 &hsotg->periodic_sched_assigned);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002844 ret_val = DWC2_TRANSACTION_PERIODIC;
2845 }
2846
2847 /*
2848 * Process entries in the inactive portion of the non-periodic
2849 * schedule. Some free host channels may not be used if they are
2850 * reserved for periodic transfers.
2851 */
John Younbea8e862016-11-03 17:55:53 -07002852 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002853 qh_ptr = hsotg->non_periodic_sched_inactive.next;
2854 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
John Youn95832c02017-01-23 14:57:26 -08002855 if (!hsotg->params.uframe_sched &&
Dom Cobley20f2eb92013-09-23 14:23:34 -07002856 hsotg->non_periodic_channels >= num_channels -
Paul Zimmerman7359d482013-03-11 17:47:59 -07002857 hsotg->periodic_channels)
2858 break;
2859 if (list_empty(&hsotg->free_hc_list))
2860 break;
2861 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
John Youn95832c02017-01-23 14:57:26 -08002862 if (hsotg->params.uframe_sched) {
Dom Cobley20f2eb92013-09-23 14:23:34 -07002863 if (hsotg->available_host_channels < 1)
2864 break;
2865 hsotg->available_host_channels--;
2866 }
2867
2868 if (dwc2_assign_and_init_hc(hsotg, qh))
2869 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002870
2871 /*
2872 * Move the QH from the non-periodic inactive schedule to the
2873 * non-periodic active schedule
2874 */
2875 qh_ptr = qh_ptr->next;
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08002876 list_move_tail(&qh->qh_list_entry,
2877 &hsotg->non_periodic_sched_active);
Paul Zimmerman7359d482013-03-11 17:47:59 -07002878
2879 if (ret_val == DWC2_TRANSACTION_NONE)
2880 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2881 else
2882 ret_val = DWC2_TRANSACTION_ALL;
2883
John Youn95832c02017-01-23 14:57:26 -08002884 if (!hsotg->params.uframe_sched)
Dom Cobley20f2eb92013-09-23 14:23:34 -07002885 hsotg->non_periodic_channels++;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002886 }
2887
2888 return ret_val;
2889}
2890
2891/**
2892 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2893 * a host channel associated with either a periodic or non-periodic transfer
2894 *
2895 * @hsotg: The HCD state structure
2896 * @chan: Host channel descriptor associated with either a periodic or
2897 * non-periodic transfer
2898 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2899 * for periodic transfers or the non-periodic Tx FIFO
2900 * for non-periodic transfers
2901 *
2902 * Return: 1 if a request is queued and more requests may be needed to
2903 * complete the transfer, 0 if no more requests are required for this
2904 * transfer, -1 if there is insufficient space in the Tx FIFO
2905 *
2906 * This function assumes that there is space available in the appropriate
2907 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2908 * it checks whether space is available in the appropriate Tx FIFO.
2909 *
2910 * Must be called with interrupt disabled and spinlock held
2911 */
2912static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2913 struct dwc2_host_chan *chan,
2914 u16 fifo_dwords_avail)
2915{
2916 int retval = 0;
2917
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08002918 if (chan->do_split)
2919 /* Put ourselves on the list to keep order straight */
2920 list_move_tail(&chan->split_order_list_entry,
2921 &hsotg->split_order);
2922
John Youn95832c02017-01-23 14:57:26 -08002923 if (hsotg->params.host_dma) {
2924 if (hsotg->params.dma_desc_enable) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07002925 if (!chan->xfer_started ||
2926 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2927 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2928 chan->qh->ping_state = 0;
2929 }
2930 } else if (!chan->xfer_started) {
2931 dwc2_hc_start_transfer(hsotg, chan);
2932 chan->qh->ping_state = 0;
2933 }
2934 } else if (chan->halt_pending) {
2935 /* Don't queue a request if the channel has been halted */
2936 } else if (chan->halt_on_queue) {
2937 dwc2_hc_halt(hsotg, chan, chan->halt_status);
2938 } else if (chan->do_ping) {
2939 if (!chan->xfer_started)
2940 dwc2_hc_start_transfer(hsotg, chan);
2941 } else if (!chan->ep_is_in ||
2942 chan->data_pid_start == DWC2_HC_PID_SETUP) {
2943 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2944 if (!chan->xfer_started) {
2945 dwc2_hc_start_transfer(hsotg, chan);
2946 retval = 1;
2947 } else {
2948 retval = dwc2_hc_continue_transfer(hsotg, chan);
2949 }
2950 } else {
2951 retval = -1;
2952 }
2953 } else {
2954 if (!chan->xfer_started) {
2955 dwc2_hc_start_transfer(hsotg, chan);
2956 retval = 1;
2957 } else {
2958 retval = dwc2_hc_continue_transfer(hsotg, chan);
2959 }
2960 }
2961
2962 return retval;
2963}
2964
2965/*
2966 * Processes periodic channels for the next frame and queues transactions for
2967 * these channels to the DWC_otg controller. After queueing transactions, the
2968 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2969 * to queue as Periodic Tx FIFO or request queue space becomes available.
2970 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2971 *
2972 * Must be called with interrupt disabled and spinlock held
2973 */
2974static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2975{
2976 struct list_head *qh_ptr;
2977 struct dwc2_qh *qh;
2978 u32 tx_status;
2979 u32 fspcavail;
2980 u32 gintmsk;
2981 int status;
Douglas Anderson4e50e012016-01-28 18:20:03 -08002982 bool no_queue_space = false;
2983 bool no_fifo_space = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07002984 u32 qspcavail;
2985
Douglas Anderson4e50e012016-01-28 18:20:03 -08002986 /* If empty list then just adjust interrupt enables */
2987 if (list_empty(&hsotg->periodic_sched_assigned))
2988 goto exit;
2989
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002990 if (dbg_perio())
2991 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07002992
Antti Seppälä95c8bc32015-08-20 21:41:07 +03002993 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02002994 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2995 TXSTS_QSPCAVAIL_SHIFT;
2996 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2997 TXSTS_FSPCAVAIL_SHIFT;
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02002998
2999 if (dbg_perio()) {
3000 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
3001 qspcavail);
3002 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
3003 fspcavail);
3004 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003005
3006 qh_ptr = hsotg->periodic_sched_assigned.next;
3007 while (qh_ptr != &hsotg->periodic_sched_assigned) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003008 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Matthijs Kooijmanacdb9042013-08-30 18:45:16 +02003009 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3010 TXSTS_QSPCAVAIL_SHIFT;
3011 if (qspcavail == 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003012 no_queue_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003013 break;
3014 }
3015
3016 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
3017 if (!qh->channel) {
3018 qh_ptr = qh_ptr->next;
3019 continue;
3020 }
3021
3022 /* Make sure EP's TT buffer is clean before queueing qtds */
3023 if (qh->tt_buffer_dirty) {
3024 qh_ptr = qh_ptr->next;
3025 continue;
3026 }
3027
3028 /*
3029 * Set a flag if we're queuing high-bandwidth in slave mode.
3030 * The flag prevents any halts to get into the request queue in
3031 * the middle of multiple high-bandwidth packets getting queued.
3032 */
John Youn95832c02017-01-23 14:57:26 -08003033 if (!hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08003034 qh->channel->multi_count > 1)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003035 hsotg->queuing_high_bandwidth = 1;
3036
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003037 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3038 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003039 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3040 if (status < 0) {
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003041 no_fifo_space = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003042 break;
3043 }
3044
3045 /*
3046 * In Slave mode, stay on the current transfer until there is
3047 * nothing more to do or the high-bandwidth request count is
3048 * reached. In DMA mode, only need to queue one request. The
3049 * controller automatically handles multiple packets for
3050 * high-bandwidth transfers.
3051 */
John Youn95832c02017-01-23 14:57:26 -08003052 if (hsotg->params.host_dma || status == 0 ||
Paul Zimmerman7359d482013-03-11 17:47:59 -07003053 qh->channel->requests == qh->channel->multi_count) {
3054 qh_ptr = qh_ptr->next;
3055 /*
3056 * Move the QH from the periodic assigned schedule to
3057 * the periodic queued schedule
3058 */
Douglas Anderson94ef7ae2016-01-28 18:19:56 -08003059 list_move_tail(&qh->qh_list_entry,
3060 &hsotg->periodic_sched_queued);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003061
3062 /* done queuing high bandwidth */
3063 hsotg->queuing_high_bandwidth = 0;
3064 }
3065 }
3066
Douglas Anderson4e50e012016-01-28 18:20:03 -08003067exit:
3068 if (no_queue_space || no_fifo_space ||
John Youn95832c02017-01-23 14:57:26 -08003069 (!hsotg->params.host_dma &&
Douglas Anderson4e50e012016-01-28 18:20:03 -08003070 !list_empty(&hsotg->periodic_sched_assigned))) {
3071 /*
3072 * May need to queue more transactions as the request
3073 * queue or Tx FIFO empties. Enable the periodic Tx
3074 * FIFO empty interrupt. (Always use the half-empty
3075 * level to ensure that new requests are loaded as
3076 * soon as possible.)
3077 */
3078 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3079 if (!(gintmsk & GINTSTS_PTXFEMP)) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003080 gintmsk |= GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003081 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Douglas Anderson4e50e012016-01-28 18:20:03 -08003082 }
3083 } else {
3084 /*
3085 * Disable the Tx FIFO empty interrupt since there are
3086 * no more transactions that need to be queued right
3087 * now. This function is called from interrupt
3088 * handlers to queue more transactions as transfer
3089 * states change.
John Youn38beaec2017-01-17 20:31:13 -08003090 */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003091 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3092 if (gintmsk & GINTSTS_PTXFEMP) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003093 gintmsk &= ~GINTSTS_PTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003094 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003095 }
3096 }
3097}
3098
3099/*
3100 * Processes active non-periodic channels and queues transactions for these
3101 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3102 * FIFO Empty interrupt is enabled if there are more transactions to queue as
3103 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3104 * FIFO Empty interrupt is disabled.
3105 *
3106 * Must be called with interrupt disabled and spinlock held
3107 */
3108static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3109{
3110 struct list_head *orig_qh_ptr;
3111 struct dwc2_qh *qh;
3112 u32 tx_status;
3113 u32 qspcavail;
3114 u32 fspcavail;
3115 u32 gintmsk;
3116 int status;
3117 int no_queue_space = 0;
3118 int no_fifo_space = 0;
3119 int more_to_do = 0;
3120
3121 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3122
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003123 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003124 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3125 TXSTS_QSPCAVAIL_SHIFT;
3126 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3127 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003128 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
3129 qspcavail);
3130 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
3131 fspcavail);
3132
3133 /*
3134 * Keep track of the starting point. Skip over the start-of-list
3135 * entry.
3136 */
3137 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3138 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3139 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3140
3141 /*
3142 * Process once through the active list or until no more space is
3143 * available in the request queue or the Tx FIFO
3144 */
3145 do {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003146 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003147 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3148 TXSTS_QSPCAVAIL_SHIFT;
John Youn95832c02017-01-23 14:57:26 -08003149 if (!hsotg->params.host_dma && qspcavail == 0) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07003150 no_queue_space = 1;
3151 break;
3152 }
3153
3154 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3155 qh_list_entry);
3156 if (!qh->channel)
3157 goto next;
3158
3159 /* Make sure EP's TT buffer is clean before queueing qtds */
3160 if (qh->tt_buffer_dirty)
3161 goto next;
3162
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003163 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3164 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003165 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3166
3167 if (status > 0) {
3168 more_to_do = 1;
3169 } else if (status < 0) {
3170 no_fifo_space = 1;
3171 break;
3172 }
3173next:
3174 /* Advance to next QH, skipping start-of-list entry */
3175 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3176 if (hsotg->non_periodic_qh_ptr ==
3177 &hsotg->non_periodic_sched_active)
3178 hsotg->non_periodic_qh_ptr =
3179 hsotg->non_periodic_qh_ptr->next;
3180 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3181
John Youn95832c02017-01-23 14:57:26 -08003182 if (!hsotg->params.host_dma) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003183 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003184 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3185 TXSTS_QSPCAVAIL_SHIFT;
3186 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3187 TXSTS_FSPCAVAIL_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003188 dev_vdbg(hsotg->dev,
3189 " NP Tx Req Queue Space Avail (after queue): %d\n",
3190 qspcavail);
3191 dev_vdbg(hsotg->dev,
3192 " NP Tx FIFO Space Avail (after queue): %d\n",
3193 fspcavail);
3194
3195 if (more_to_do || no_queue_space || no_fifo_space) {
3196 /*
3197 * May need to queue more transactions as the request
3198 * queue or Tx FIFO empties. Enable the non-periodic
3199 * Tx FIFO empty interrupt. (Always use the half-empty
3200 * level to ensure that new requests are loaded as
3201 * soon as possible.)
3202 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003203 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003204 gintmsk |= GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003205 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003206 } else {
3207 /*
3208 * Disable the Tx FIFO empty interrupt since there are
3209 * no more transactions that need to be queued right
3210 * now. This function is called from interrupt
3211 * handlers to queue more transactions as transfer
3212 * states change.
3213 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003214 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003215 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003216 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003217 }
3218 }
3219}
3220
3221/**
3222 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3223 * and queues transactions for these channels to the DWC_otg controller. Called
3224 * from the HCD interrupt handler functions.
3225 *
3226 * @hsotg: The HCD state structure
3227 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3228 * or both)
3229 *
3230 * Must be called with interrupt disabled and spinlock held
3231 */
3232void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3233 enum dwc2_transaction_type tr_type)
3234{
3235#ifdef DWC2_DEBUG_SOF
3236 dev_vdbg(hsotg->dev, "Queue Transactions\n");
3237#endif
3238 /* Process host channels associated with periodic transfers */
Douglas Anderson4e50e012016-01-28 18:20:03 -08003239 if (tr_type == DWC2_TRANSACTION_PERIODIC ||
3240 tr_type == DWC2_TRANSACTION_ALL)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003241 dwc2_process_periodic_channels(hsotg);
3242
3243 /* Process host channels associated with non-periodic transfers */
3244 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3245 tr_type == DWC2_TRANSACTION_ALL) {
3246 if (!list_empty(&hsotg->non_periodic_sched_active)) {
3247 dwc2_process_non_periodic_channels(hsotg);
3248 } else {
3249 /*
3250 * Ensure NP Tx FIFO empty interrupt is disabled when
3251 * there are no non-periodic transfers to process
3252 */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003253 u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003254
3255 gintmsk &= ~GINTSTS_NPTXFEMP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003256 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003257 }
3258 }
3259}
3260
3261static void dwc2_conn_id_status_change(struct work_struct *work)
3262{
3263 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3264 wf_otg);
3265 u32 count = 0;
3266 u32 gotgctl;
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003267 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003268
3269 dev_dbg(hsotg->dev, "%s()\n", __func__);
3270
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003271 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003272 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3273 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3274 !!(gotgctl & GOTGCTL_CONID_B));
3275
3276 /* B-Device connector (Device Mode) */
3277 if (gotgctl & GOTGCTL_CONID_B) {
3278 /* Wait for switch to device mode */
3279 dev_dbg(hsotg->dev, "connId B\n");
Chen Yu9156a7e2017-01-23 14:59:57 -08003280 if (hsotg->bus_suspended) {
3281 dev_info(hsotg->dev,
3282 "Do port resume before switching to device mode\n");
3283 dwc2_port_resume(hsotg);
3284 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003285 while (!dwc2_is_device_mode(hsotg)) {
3286 dev_info(hsotg->dev,
3287 "Waiting for Peripheral Mode, Mode=%s\n",
3288 dwc2_is_host_mode(hsotg) ? "Host" :
3289 "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003290 msleep(20);
John Stultzfc30c4b2017-01-23 14:59:35 -08003291 /*
3292 * Sometimes the initial GOTGCTRL read is wrong, so
3293 * check it again and jump to host mode if that was
3294 * the case.
3295 */
3296 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3297 if (!(gotgctl & GOTGCTL_CONID_B))
3298 goto host;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003299 if (++count > 250)
3300 break;
3301 }
3302 if (count > 250)
3303 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003304 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003305 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003306 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003307 dwc2_enable_global_interrupts(hsotg);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003308 spin_lock_irqsave(&hsotg->lock, flags);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003309 dwc2_hsotg_core_init_disconnected(hsotg, false);
Mian Yousaf Kaukab5390d432015-09-29 12:08:25 +02003310 spin_unlock_irqrestore(&hsotg->lock, flags);
Razmik Karapetyan66e77a22018-01-24 17:40:29 +04003311 /* Enable ACG feature in device mode,if supported */
3312 dwc2_enable_acg(hsotg);
Felipe Balbi1f91b4c2015-08-06 18:11:54 -05003313 dwc2_hsotg_core_connect(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003314 } else {
John Stultzfc30c4b2017-01-23 14:59:35 -08003315host:
Paul Zimmerman7359d482013-03-11 17:47:59 -07003316 /* A-Device connector (Host Mode) */
3317 dev_dbg(hsotg->dev, "connId A\n");
3318 while (!dwc2_is_host_mode(hsotg)) {
3319 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3320 dwc2_is_host_mode(hsotg) ?
3321 "Host" : "Peripheral");
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003322 msleep(20);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003323 if (++count > 250)
3324 break;
3325 }
3326 if (count > 250)
3327 dev_err(hsotg->dev,
Paul Zimmermande9169a2013-04-22 14:00:17 -07003328 "Connection id status change timed out\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07003329
John Stultzd2471d42017-10-23 14:32:48 -07003330 spin_lock_irqsave(&hsotg->lock, flags);
3331 dwc2_hsotg_disconnect(hsotg);
3332 spin_unlock_irqrestore(&hsotg->lock, flags);
3333
3334 hsotg->op_state = OTG_STATE_A_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003335 /* Initialize the Core for Host mode */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08003336 dwc2_core_init(hsotg, false);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003337 dwc2_enable_global_interrupts(hsotg);
3338 dwc2_hcd_start(hsotg);
3339 }
3340}
3341
Kees Cooke99e88a2017-10-16 14:43:17 -07003342static void dwc2_wakeup_detected(struct timer_list *t)
Paul Zimmerman7359d482013-03-11 17:47:59 -07003343{
Kees Cooke99e88a2017-10-16 14:43:17 -07003344 struct dwc2_hsotg *hsotg = from_timer(hsotg, t, wkp_timer);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003345 u32 hprt0;
3346
3347 dev_dbg(hsotg->dev, "%s()\n", __func__);
3348
3349 /*
3350 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3351 * so that OPT tests pass with all PHYs.)
3352 */
3353 hprt0 = dwc2_read_hprt0(hsotg);
3354 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3355 hprt0 &= ~HPRT0_RES;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003356 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003357 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003358 dwc2_readl(hsotg->regs + HPRT0));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003359
3360 dwc2_hcd_rem_wakeup(hsotg);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003361 hsotg->bus_suspended = false;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003362
3363 /* Change to L0 state */
3364 hsotg->lx_state = DWC2_L0;
3365}
3366
3367static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3368{
3369 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3370
3371 return hcd->self.b_hnp_enable;
3372}
3373
3374/* Must NOT be called with interrupt disabled or spinlock held */
3375static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3376{
3377 unsigned long flags;
3378 u32 hprt0;
3379 u32 pcgctl;
3380 u32 gotgctl;
3381
3382 dev_dbg(hsotg->dev, "%s()\n", __func__);
3383
3384 spin_lock_irqsave(&hsotg->lock, flags);
3385
3386 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003387 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003388 gotgctl |= GOTGCTL_HSTSETHNPEN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003389 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003390 hsotg->op_state = OTG_STATE_A_SUSPEND;
3391 }
3392
3393 hprt0 = dwc2_read_hprt0(hsotg);
3394 hprt0 |= HPRT0_SUSP;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003395 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003396
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003397 hsotg->bus_suspended = true;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003398
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003399 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003400 * If power_down is supported, Phy clock will be suspended
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003401 * after registers are backuped.
3402 */
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003403 if (!hsotg->params.power_down) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003404 /* Suspend the Phy Clock */
3405 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3406 pcgctl |= PCGCTL_STOPPCLK;
3407 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3408 udelay(10);
3409 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003410
3411 /* For HNP the bus must be suspended for at least 200ms */
3412 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003413 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003414 pcgctl &= ~PCGCTL_STOPPCLK;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003415 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003416
3417 spin_unlock_irqrestore(&hsotg->lock, flags);
3418
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003419 msleep(200);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003420 } else {
3421 spin_unlock_irqrestore(&hsotg->lock, flags);
3422 }
3423}
3424
Gregory Herrero30db1032015-09-22 15:16:38 +02003425/* Must NOT be called with interrupt disabled or spinlock held */
3426static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
3427{
3428 unsigned long flags;
3429 u32 hprt0;
3430 u32 pcgctl;
3431
Douglas Anderson4d273c22015-10-14 15:58:27 -07003432 spin_lock_irqsave(&hsotg->lock, flags);
3433
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003434 /*
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003435 * If power_down is supported, Phy clock is already resumed
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003436 * after registers restore.
3437 */
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04003438 if (!hsotg->params.power_down) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003439 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3440 pcgctl &= ~PCGCTL_STOPPCLK;
3441 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003442 spin_unlock_irqrestore(&hsotg->lock, flags);
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003443 msleep(20);
Douglas Anderson4d273c22015-10-14 15:58:27 -07003444 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02003445 }
Gregory Herrero30db1032015-09-22 15:16:38 +02003446
Gregory Herrero30db1032015-09-22 15:16:38 +02003447 hprt0 = dwc2_read_hprt0(hsotg);
3448 hprt0 |= HPRT0_RES;
3449 hprt0 &= ~HPRT0_SUSP;
3450 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3451 spin_unlock_irqrestore(&hsotg->lock, flags);
3452
3453 msleep(USB_RESUME_TIMEOUT);
3454
3455 spin_lock_irqsave(&hsotg->lock, flags);
3456 hprt0 = dwc2_read_hprt0(hsotg);
3457 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
3458 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01003459 hsotg->bus_suspended = false;
Gregory Herrero30db1032015-09-22 15:16:38 +02003460 spin_unlock_irqrestore(&hsotg->lock, flags);
3461}
3462
Paul Zimmerman7359d482013-03-11 17:47:59 -07003463/* Handles hub class-specific requests */
3464static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3465 u16 wvalue, u16 windex, char *buf, u16 wlength)
3466{
3467 struct usb_hub_descriptor *hub_desc;
3468 int retval = 0;
3469 u32 hprt0;
3470 u32 port_status;
3471 u32 speed;
3472 u32 pcgctl;
3473
3474 switch (typereq) {
3475 case ClearHubFeature:
3476 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3477
3478 switch (wvalue) {
3479 case C_HUB_LOCAL_POWER:
3480 case C_HUB_OVER_CURRENT:
3481 /* Nothing required here */
3482 break;
3483
3484 default:
3485 retval = -EINVAL;
3486 dev_err(hsotg->dev,
3487 "ClearHubFeature request %1xh unknown\n",
3488 wvalue);
3489 }
3490 break;
3491
3492 case ClearPortFeature:
3493 if (wvalue != USB_PORT_FEAT_L1)
3494 if (!windex || windex > 1)
3495 goto error;
3496 switch (wvalue) {
3497 case USB_PORT_FEAT_ENABLE:
3498 dev_dbg(hsotg->dev,
3499 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3500 hprt0 = dwc2_read_hprt0(hsotg);
3501 hprt0 |= HPRT0_ENA;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003502 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003503 break;
3504
3505 case USB_PORT_FEAT_SUSPEND:
3506 dev_dbg(hsotg->dev,
3507 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
Paul Zimmermanb0bb9bb2015-01-15 19:21:46 +00003508
Vardan Mikayelyanf260b252018-02-16 14:12:02 +04003509 if (hsotg->bus_suspended) {
3510 if (hsotg->hibernated)
3511 dwc2_exit_hibernation(hsotg, 0, 0, 1);
3512 else
3513 dwc2_port_resume(hsotg);
3514 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07003515 break;
3516
3517 case USB_PORT_FEAT_POWER:
3518 dev_dbg(hsotg->dev,
3519 "ClearPortFeature USB_PORT_FEAT_POWER\n");
3520 hprt0 = dwc2_read_hprt0(hsotg);
3521 hprt0 &= ~HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003522 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003523 break;
3524
3525 case USB_PORT_FEAT_INDICATOR:
3526 dev_dbg(hsotg->dev,
3527 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3528 /* Port indicator not supported */
3529 break;
3530
3531 case USB_PORT_FEAT_C_CONNECTION:
3532 /*
3533 * Clears driver's internal Connect Status Change flag
3534 */
3535 dev_dbg(hsotg->dev,
3536 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3537 hsotg->flags.b.port_connect_status_change = 0;
3538 break;
3539
3540 case USB_PORT_FEAT_C_RESET:
3541 /* Clears driver's internal Port Reset Change flag */
3542 dev_dbg(hsotg->dev,
3543 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3544 hsotg->flags.b.port_reset_change = 0;
3545 break;
3546
3547 case USB_PORT_FEAT_C_ENABLE:
3548 /*
3549 * Clears the driver's internal Port Enable/Disable
3550 * Change flag
3551 */
3552 dev_dbg(hsotg->dev,
3553 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3554 hsotg->flags.b.port_enable_change = 0;
3555 break;
3556
3557 case USB_PORT_FEAT_C_SUSPEND:
3558 /*
3559 * Clears the driver's internal Port Suspend Change
3560 * flag, which is set when resume signaling on the host
3561 * port is complete
3562 */
3563 dev_dbg(hsotg->dev,
3564 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3565 hsotg->flags.b.port_suspend_change = 0;
3566 break;
3567
3568 case USB_PORT_FEAT_C_PORT_L1:
3569 dev_dbg(hsotg->dev,
3570 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3571 hsotg->flags.b.port_l1_change = 0;
3572 break;
3573
3574 case USB_PORT_FEAT_C_OVER_CURRENT:
3575 dev_dbg(hsotg->dev,
3576 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3577 hsotg->flags.b.port_over_current_change = 0;
3578 break;
3579
3580 default:
3581 retval = -EINVAL;
3582 dev_err(hsotg->dev,
3583 "ClearPortFeature request %1xh unknown or unsupported\n",
3584 wvalue);
3585 }
3586 break;
3587
3588 case GetHubDescriptor:
3589 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3590 hub_desc = (struct usb_hub_descriptor *)buf;
3591 hub_desc->bDescLength = 9;
Sergei Shtylyova5dd0392015-03-29 01:36:28 +03003592 hub_desc->bDescriptorType = USB_DT_HUB;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003593 hub_desc->bNbrPorts = 1;
Sergei Shtylyov3d040de2015-01-19 01:54:15 +03003594 hub_desc->wHubCharacteristics =
3595 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
3596 HUB_CHAR_INDV_PORT_OCPM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003597 hub_desc->bPwrOn2PwrGood = 1;
3598 hub_desc->bHubContrCurrent = 0;
3599 hub_desc->u.hs.DeviceRemovable[0] = 0;
3600 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3601 break;
3602
3603 case GetHubStatus:
3604 dev_dbg(hsotg->dev, "GetHubStatus\n");
3605 memset(buf, 0, 4);
3606 break;
3607
3608 case GetPortStatus:
Paul Zimmermanb8313412013-05-24 16:32:12 -07003609 dev_vdbg(hsotg->dev,
3610 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3611 hsotg->flags.d32);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003612 if (!windex || windex > 1)
3613 goto error;
3614
3615 port_status = 0;
3616 if (hsotg->flags.b.port_connect_status_change)
3617 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3618 if (hsotg->flags.b.port_enable_change)
3619 port_status |= USB_PORT_STAT_C_ENABLE << 16;
3620 if (hsotg->flags.b.port_suspend_change)
3621 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3622 if (hsotg->flags.b.port_l1_change)
3623 port_status |= USB_PORT_STAT_C_L1 << 16;
3624 if (hsotg->flags.b.port_reset_change)
3625 port_status |= USB_PORT_STAT_C_RESET << 16;
3626 if (hsotg->flags.b.port_over_current_change) {
3627 dev_warn(hsotg->dev, "Overcurrent change detected\n");
3628 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3629 }
3630
3631 if (!hsotg->flags.b.port_connect_status) {
3632 /*
3633 * The port is disconnected, which means the core is
3634 * either in device mode or it soon will be. Just
3635 * return 0's for the remainder of the port status
3636 * since the port register can't be read if the core
3637 * is in device mode.
3638 */
3639 *(__le32 *)buf = cpu_to_le32(port_status);
3640 break;
3641 }
3642
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003643 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
Paul Zimmermanb8313412013-05-24 16:32:12 -07003644 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003645
3646 if (hprt0 & HPRT0_CONNSTS)
3647 port_status |= USB_PORT_STAT_CONNECTION;
3648 if (hprt0 & HPRT0_ENA)
3649 port_status |= USB_PORT_STAT_ENABLE;
3650 if (hprt0 & HPRT0_SUSP)
3651 port_status |= USB_PORT_STAT_SUSPEND;
3652 if (hprt0 & HPRT0_OVRCURRACT)
3653 port_status |= USB_PORT_STAT_OVERCURRENT;
3654 if (hprt0 & HPRT0_RST)
3655 port_status |= USB_PORT_STAT_RESET;
3656 if (hprt0 & HPRT0_PWR)
3657 port_status |= USB_PORT_STAT_POWER;
3658
Matthijs Kooijmanf9234632013-08-30 18:45:13 +02003659 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003660 if (speed == HPRT0_SPD_HIGH_SPEED)
3661 port_status |= USB_PORT_STAT_HIGH_SPEED;
3662 else if (speed == HPRT0_SPD_LOW_SPEED)
3663 port_status |= USB_PORT_STAT_LOW_SPEED;
3664
3665 if (hprt0 & HPRT0_TSTCTL_MASK)
3666 port_status |= USB_PORT_STAT_TEST;
3667 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3668
John Younbea8e862016-11-03 17:55:53 -07003669 if (hsotg->params.dma_desc_fs_enable) {
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003670 /*
3671 * Enable descriptor DMA only if a full speed
3672 * device is connected.
3673 */
3674 if (hsotg->new_connection &&
3675 ((port_status &
3676 (USB_PORT_STAT_CONNECTION |
3677 USB_PORT_STAT_HIGH_SPEED |
3678 USB_PORT_STAT_LOW_SPEED)) ==
3679 USB_PORT_STAT_CONNECTION)) {
3680 u32 hcfg;
3681
3682 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
John Youn95832c02017-01-23 14:57:26 -08003683 hsotg->params.dma_desc_enable = true;
Mian Yousaf Kaukabfbb9e222015-11-20 11:49:28 +01003684 hcfg = dwc2_readl(hsotg->regs + HCFG);
3685 hcfg |= HCFG_DESCDMA;
3686 dwc2_writel(hcfg, hsotg->regs + HCFG);
3687 hsotg->new_connection = false;
3688 }
3689 }
3690
Paul Zimmermanb8313412013-05-24 16:32:12 -07003691 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003692 *(__le32 *)buf = cpu_to_le32(port_status);
3693 break;
3694
3695 case SetHubFeature:
3696 dev_dbg(hsotg->dev, "SetHubFeature\n");
3697 /* No HUB features supported */
3698 break;
3699
3700 case SetPortFeature:
3701 dev_dbg(hsotg->dev, "SetPortFeature\n");
3702 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3703 goto error;
3704
3705 if (!hsotg->flags.b.port_connect_status) {
3706 /*
3707 * The port is disconnected, which means the core is
3708 * either in device mode or it soon will be. Just
3709 * return without doing anything since the port
3710 * register can't be written if the core is in device
3711 * mode.
3712 */
3713 break;
3714 }
3715
3716 switch (wvalue) {
3717 case USB_PORT_FEAT_SUSPEND:
3718 dev_dbg(hsotg->dev,
3719 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3720 if (windex != hsotg->otg_port)
3721 goto error;
Vardan Mikayelyanf260b252018-02-16 14:12:02 +04003722 if (hsotg->params.power_down == 2)
3723 dwc2_enter_hibernation(hsotg, 1);
3724 else
3725 dwc2_port_suspend(hsotg, windex);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003726 break;
3727
3728 case USB_PORT_FEAT_POWER:
3729 dev_dbg(hsotg->dev,
3730 "SetPortFeature - USB_PORT_FEAT_POWER\n");
3731 hprt0 = dwc2_read_hprt0(hsotg);
3732 hprt0 |= HPRT0_PWR;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003733 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003734 break;
3735
3736 case USB_PORT_FEAT_RESET:
Vardan Mikayelyanf260b252018-02-16 14:12:02 +04003737 if (hsotg->params.power_down == 2 &&
3738 hsotg->hibernated)
3739 dwc2_exit_hibernation(hsotg, 0, 1, 1);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003740 hprt0 = dwc2_read_hprt0(hsotg);
3741 dev_dbg(hsotg->dev,
3742 "SetPortFeature - USB_PORT_FEAT_RESET\n");
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003743 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003744 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003745 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003746 /* ??? Original driver does this */
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003747 dwc2_writel(0, hsotg->regs + PCGCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003748
3749 hprt0 = dwc2_read_hprt0(hsotg);
3750 /* Clear suspend bit if resetting from suspend state */
3751 hprt0 &= ~HPRT0_SUSP;
3752
3753 /*
3754 * When B-Host the Port reset bit is set in the Start
3755 * HCD Callback function, so that the reset is started
3756 * within 1ms of the HNP success interrupt
3757 */
3758 if (!dwc2_hcd_is_b_host(hsotg)) {
3759 hprt0 |= HPRT0_PWR | HPRT0_RST;
3760 dev_dbg(hsotg->dev,
3761 "In host mode, hprt0=%08x\n", hprt0);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003762 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003763 }
3764
3765 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
Nicholas Mc Guire04a9db72017-01-12 16:54:03 +01003766 msleep(50);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003767 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003768 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003769 hsotg->lx_state = DWC2_L0; /* Now back to On state */
3770 break;
3771
3772 case USB_PORT_FEAT_INDICATOR:
3773 dev_dbg(hsotg->dev,
3774 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3775 /* Not supported */
3776 break;
3777
Jingwu Lin96d480e2015-04-29 22:09:17 +02003778 case USB_PORT_FEAT_TEST:
3779 hprt0 = dwc2_read_hprt0(hsotg);
3780 dev_dbg(hsotg->dev,
3781 "SetPortFeature - USB_PORT_FEAT_TEST\n");
3782 hprt0 &= ~HPRT0_TSTCTL_MASK;
3783 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003784 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Jingwu Lin96d480e2015-04-29 22:09:17 +02003785 break;
3786
Paul Zimmerman7359d482013-03-11 17:47:59 -07003787 default:
3788 retval = -EINVAL;
3789 dev_err(hsotg->dev,
3790 "SetPortFeature %1xh unknown or unsupported\n",
3791 wvalue);
3792 break;
3793 }
3794 break;
3795
3796 default:
3797error:
3798 retval = -EINVAL;
3799 dev_dbg(hsotg->dev,
3800 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3801 typereq, windex, wvalue);
3802 break;
3803 }
3804
3805 return retval;
3806}
3807
3808static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3809{
3810 int retval;
3811
Paul Zimmerman7359d482013-03-11 17:47:59 -07003812 if (port != 1)
3813 return -EINVAL;
3814
3815 retval = (hsotg->flags.b.port_connect_status_change ||
3816 hsotg->flags.b.port_reset_change ||
3817 hsotg->flags.b.port_enable_change ||
3818 hsotg->flags.b.port_suspend_change ||
3819 hsotg->flags.b.port_over_current_change);
3820
3821 if (retval) {
3822 dev_dbg(hsotg->dev,
3823 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3824 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
3825 hsotg->flags.b.port_connect_status_change);
3826 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
3827 hsotg->flags.b.port_reset_change);
3828 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
3829 hsotg->flags.b.port_enable_change);
3830 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
3831 hsotg->flags.b.port_suspend_change);
3832 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
3833 hsotg->flags.b.port_over_current_change);
3834 }
3835
3836 return retval;
3837}
3838
3839int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3840{
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003841 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003842
3843#ifdef DWC2_DEBUG_SOF
3844 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003845 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003846#endif
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02003847 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003848}
3849
Douglas Andersonfae4e822016-01-28 18:20:10 -08003850int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3851{
3852 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
3853 u32 hfir = dwc2_readl(hsotg->regs + HFIR);
3854 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3855 unsigned int us_per_frame;
3856 unsigned int frame_number;
3857 unsigned int remaining;
3858 unsigned int interval;
3859 unsigned int phy_clks;
3860
3861 /* High speed has 125 us per (micro) frame; others are 1 ms per */
3862 us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3863
3864 /* Extract fields */
3865 frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3866 remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3867 interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3868
3869 /*
3870 * Number of phy clocks since the last tick of the frame number after
3871 * "us" has passed.
3872 */
3873 phy_clks = (interval - remaining) +
3874 DIV_ROUND_UP(interval * us, us_per_frame);
3875
3876 return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3877}
3878
Paul Zimmerman7359d482013-03-11 17:47:59 -07003879int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3880{
Aldo Iljazi6bf2e2a2013-11-30 19:33:57 +02003881 return hsotg->op_state == OTG_STATE_B_HOST;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003882}
3883
3884static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3885 int iso_desc_count,
3886 gfp_t mem_flags)
3887{
3888 struct dwc2_hcd_urb *urb;
3889 u32 size = sizeof(*urb) + iso_desc_count *
3890 sizeof(struct dwc2_hcd_iso_packet_desc);
3891
3892 urb = kzalloc(size, mem_flags);
3893 if (urb)
3894 urb->packet_count = iso_desc_count;
3895 return urb;
3896}
3897
3898static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3899 struct dwc2_hcd_urb *urb, u8 dev_addr,
3900 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
3901{
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02003902 if (dbg_perio() ||
3903 ep_type == USB_ENDPOINT_XFER_BULK ||
3904 ep_type == USB_ENDPOINT_XFER_CONTROL)
3905 dev_vdbg(hsotg->dev,
3906 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
3907 dev_addr, ep_num, ep_dir, ep_type, mps);
Paul Zimmerman7359d482013-03-11 17:47:59 -07003908 urb->pipe_info.dev_addr = dev_addr;
3909 urb->pipe_info.ep_num = ep_num;
3910 urb->pipe_info.pipe_type = ep_type;
3911 urb->pipe_info.pipe_dir = ep_dir;
3912 urb->pipe_info.mps = mps;
3913}
3914
3915/*
3916 * NOTE: This function will be removed once the peripheral controller code
3917 * is integrated and the driver is stable
3918 */
3919void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3920{
3921#ifdef DEBUG
3922 struct dwc2_host_chan *chan;
3923 struct dwc2_hcd_urb *urb;
3924 struct dwc2_qtd *qtd;
3925 int num_channels;
3926 u32 np_tx_status;
3927 u32 p_tx_status;
3928 int i;
3929
John Younbea8e862016-11-03 17:55:53 -07003930 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07003931 dev_dbg(hsotg->dev, "\n");
3932 dev_dbg(hsotg->dev,
3933 "************************************************************\n");
3934 dev_dbg(hsotg->dev, "HCD State:\n");
3935 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
3936
3937 for (i = 0; i < num_channels; i++) {
3938 chan = hsotg->hc_ptr_array[i];
3939 dev_dbg(hsotg->dev, " Channel %d:\n", i);
3940 dev_dbg(hsotg->dev,
3941 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3942 chan->dev_addr, chan->ep_num, chan->ep_is_in);
3943 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
3944 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
3945 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
3946 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
3947 chan->data_pid_start);
3948 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
3949 dev_dbg(hsotg->dev, " xfer_started: %d\n",
3950 chan->xfer_started);
3951 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
3952 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
3953 (unsigned long)chan->xfer_dma);
3954 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
3955 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
3956 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
3957 chan->halt_on_queue);
3958 dev_dbg(hsotg->dev, " halt_pending: %d\n",
3959 chan->halt_pending);
3960 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
3961 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
3962 dev_dbg(hsotg->dev, " complete_split: %d\n",
3963 chan->complete_split);
3964 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
3965 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
3966 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
3967 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
3968 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
3969
3970 if (chan->xfer_started) {
3971 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3972
Antti Seppälä95c8bc32015-08-20 21:41:07 +03003973 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3974 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
3975 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
3976 hcint = dwc2_readl(hsotg->regs + HCINT(i));
3977 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
Paul Zimmerman7359d482013-03-11 17:47:59 -07003978 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
3979 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
3980 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
3981 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
3982 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
3983 }
3984
3985 if (!(chan->xfer_started && chan->qh))
3986 continue;
3987
3988 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3989 if (!qtd->in_process)
3990 break;
3991 urb = qtd->urb;
3992 dev_dbg(hsotg->dev, " URB Info:\n");
3993 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
3994 qtd, urb);
3995 if (urb) {
3996 dev_dbg(hsotg->dev,
3997 " Dev: %d, EP: %d %s\n",
3998 dwc2_hcd_get_dev_addr(&urb->pipe_info),
3999 dwc2_hcd_get_ep_num(&urb->pipe_info),
4000 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
4001 "IN" : "OUT");
4002 dev_dbg(hsotg->dev,
4003 " Max packet size: %d\n",
4004 dwc2_hcd_get_mps(&urb->pipe_info));
4005 dev_dbg(hsotg->dev,
4006 " transfer_buffer: %p\n",
4007 urb->buf);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07004008 dev_dbg(hsotg->dev,
4009 " transfer_dma: %08lx\n",
4010 (unsigned long)urb->dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004011 dev_dbg(hsotg->dev,
4012 " transfer_buffer_length: %d\n",
4013 urb->length);
4014 dev_dbg(hsotg->dev, " actual_length: %d\n",
4015 urb->actual_length);
4016 }
4017 }
4018 }
4019
4020 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
4021 hsotg->non_periodic_channels);
4022 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
4023 hsotg->periodic_channels);
4024 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004025 np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004026 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004027 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004028 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004029 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004030 p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004031 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004032 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004033 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
Matthijs Kooijmand6ec53e2013-08-30 18:45:15 +02004034 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004035 dwc2_dump_global_registers(hsotg);
4036 dwc2_dump_host_registers(hsotg);
4037 dev_dbg(hsotg->dev,
4038 "************************************************************\n");
4039 dev_dbg(hsotg->dev, "\n");
4040#endif
4041}
4042
Paul Zimmerman7359d482013-03-11 17:47:59 -07004043struct wrapper_priv_data {
4044 struct dwc2_hsotg *hsotg;
4045};
4046
4047/* Gets the dwc2_hsotg from a usb_hcd */
4048static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4049{
4050 struct wrapper_priv_data *p;
4051
John Youn9da51972017-01-17 20:30:27 -08004052 p = (struct wrapper_priv_data *)&hcd->hcd_priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004053 return p->hsotg;
4054}
4055
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004056/**
4057 * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
4058 *
4059 * This will get the dwc2_tt structure (and ttport) associated with the given
4060 * context (which is really just a struct urb pointer).
4061 *
4062 * The first time this is called for a given TT we allocate memory for our
4063 * structure. When everyone is done and has called dwc2_host_put_tt_info()
4064 * then the refcount for the structure will go to 0 and we'll free it.
4065 *
4066 * @hsotg: The HCD state structure for the DWC OTG controller.
4067 * @qh: The QH structure.
4068 * @context: The priv pointer from a struct dwc2_hcd_urb.
4069 * @mem_flags: Flags for allocating memory.
4070 * @ttport: We'll return this device's port number here. That's used to
4071 * reference into the bitmap if we're on a multi_tt hub.
4072 *
4073 * Return: a pointer to a struct dwc2_tt. Don't forget to call
4074 * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
4075 */
4076
4077struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
4078 gfp_t mem_flags, int *ttport)
4079{
4080 struct urb *urb = context;
4081 struct dwc2_tt *dwc_tt = NULL;
4082
4083 if (urb->dev->tt) {
4084 *ttport = urb->dev->ttport;
4085
4086 dwc_tt = urb->dev->tt->hcpriv;
John Youn9da51972017-01-17 20:30:27 -08004087 if (!dwc_tt) {
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004088 size_t bitmap_size;
4089
4090 /*
4091 * For single_tt we need one schedule. For multi_tt
4092 * we need one per port.
4093 */
4094 bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
4095 sizeof(dwc_tt->periodic_bitmaps[0]);
4096 if (urb->dev->tt->multi)
4097 bitmap_size *= urb->dev->tt->hub->maxchild;
4098
4099 dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
4100 mem_flags);
John Youn9da51972017-01-17 20:30:27 -08004101 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004102 return NULL;
4103
4104 dwc_tt->usb_tt = urb->dev->tt;
4105 dwc_tt->usb_tt->hcpriv = dwc_tt;
4106 }
4107
4108 dwc_tt->refcount++;
4109 }
4110
4111 return dwc_tt;
4112}
4113
4114/**
4115 * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
4116 *
4117 * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
4118 * of the structure are done.
4119 *
4120 * It's OK to call this with NULL.
4121 *
4122 * @hsotg: The HCD state structure for the DWC OTG controller.
4123 * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
4124 */
4125void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
4126{
4127 /* Model kfree and make put of NULL a no-op */
John Youn9da51972017-01-17 20:30:27 -08004128 if (!dwc_tt)
Douglas Anderson9f9f09b2016-01-28 18:20:12 -08004129 return;
4130
4131 WARN_ON(dwc_tt->refcount < 1);
4132
4133 dwc_tt->refcount--;
4134 if (!dwc_tt->refcount) {
4135 dwc_tt->usb_tt->hcpriv = NULL;
4136 kfree(dwc_tt);
4137 }
4138}
4139
Paul Zimmerman7359d482013-03-11 17:47:59 -07004140int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4141{
4142 struct urb *urb = context;
4143
4144 return urb->dev->speed;
4145}
4146
4147static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4148 struct urb *urb)
4149{
4150 struct usb_bus *bus = hcd_to_bus(hcd);
4151
4152 if (urb->interval)
4153 bus->bandwidth_allocated += bw / urb->interval;
4154 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4155 bus->bandwidth_isoc_reqs++;
4156 else
4157 bus->bandwidth_int_reqs++;
4158}
4159
4160static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4161 struct urb *urb)
4162{
4163 struct usb_bus *bus = hcd_to_bus(hcd);
4164
4165 if (urb->interval)
4166 bus->bandwidth_allocated -= bw / urb->interval;
4167 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4168 bus->bandwidth_isoc_reqs--;
4169 else
4170 bus->bandwidth_int_reqs--;
4171}
4172
4173/*
4174 * Sets the final status of an URB and returns it to the upper layer. Any
4175 * required cleanup of the URB is performed.
4176 *
4177 * Must be called with interrupt disabled and spinlock held
4178 */
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004179void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4180 int status)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004181{
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004182 struct urb *urb;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004183 int i;
4184
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004185 if (!qtd) {
4186 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4187 return;
4188 }
4189
4190 if (!qtd->urb) {
4191 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4192 return;
4193 }
4194
4195 urb = qtd->urb->priv;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004196 if (!urb) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004197 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004198 return;
4199 }
4200
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004201 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004202
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004203 if (dbg_urb(urb))
4204 dev_vdbg(hsotg->dev,
4205 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4206 __func__, urb, usb_pipedevice(urb->pipe),
4207 usb_pipeendpoint(urb->pipe),
4208 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4209 urb->actual_length);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004210
Paul Zimmerman7359d482013-03-11 17:47:59 -07004211 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004212 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004213 for (i = 0; i < urb->number_of_packets; ++i) {
4214 urb->iso_frame_desc[i].actual_length =
4215 dwc2_hcd_urb_get_iso_desc_actual_length(
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004216 qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004217 urb->iso_frame_desc[i].status =
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004218 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004219 }
4220 }
4221
Gregory Herrerofe9b1772015-09-22 15:16:51 +02004222 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4223 for (i = 0; i < urb->number_of_packets; i++)
4224 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4225 i, urb->iso_frame_desc[i].status);
4226 }
4227
Paul Zimmerman7359d482013-03-11 17:47:59 -07004228 urb->status = status;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004229 if (!status) {
4230 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4231 urb->actual_length < urb->transfer_buffer_length)
4232 urb->status = -EREMOTEIO;
4233 }
4234
4235 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4236 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4237 struct usb_host_endpoint *ep = urb->ep;
4238
4239 if (ep)
4240 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4241 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4242 urb);
4243 }
4244
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004245 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
Paul Zimmerman0d012b92013-07-13 14:53:48 -07004246 urb->hcpriv = NULL;
4247 kfree(qtd->urb);
4248 qtd->urb = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004249
Paul Zimmerman7359d482013-03-11 17:47:59 -07004250 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004251}
4252
4253/*
4254 * Work queue function for starting the HCD when A-Cable is connected
4255 */
4256static void dwc2_hcd_start_func(struct work_struct *work)
4257{
4258 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4259 start_work.work);
4260
4261 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4262 dwc2_host_start(hsotg);
4263}
4264
4265/*
4266 * Reset work queue function
4267 */
4268static void dwc2_hcd_reset_func(struct work_struct *work)
4269{
4270 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4271 reset_work.work);
Douglas Anderson4a065c72015-11-20 09:06:27 -08004272 unsigned long flags;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004273 u32 hprt0;
4274
4275 dev_dbg(hsotg->dev, "USB RESET function called\n");
Douglas Anderson4a065c72015-11-20 09:06:27 -08004276
4277 spin_lock_irqsave(&hsotg->lock, flags);
4278
Paul Zimmerman7359d482013-03-11 17:47:59 -07004279 hprt0 = dwc2_read_hprt0(hsotg);
4280 hprt0 &= ~HPRT0_RST;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004281 dwc2_writel(hprt0, hsotg->regs + HPRT0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004282 hsotg->flags.b.port_reset_change = 1;
Douglas Anderson4a065c72015-11-20 09:06:27 -08004283
4284 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004285}
4286
4287/*
4288 * =========================================================================
4289 * Linux HC Driver Functions
4290 * =========================================================================
4291 */
4292
4293/*
4294 * Initializes the DWC_otg controller and its root hub and prepares it for host
4295 * mode operation. Activates the root port. Returns 0 on success and a negative
4296 * error code on failure.
4297 */
4298static int _dwc2_hcd_start(struct usb_hcd *hcd)
4299{
4300 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4301 struct usb_bus *bus = hcd_to_bus(hcd);
4302 unsigned long flags;
4303
4304 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4305
4306 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero31927b62015-09-22 15:16:41 +02004307 hsotg->lx_state = DWC2_L0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004308 hcd->state = HC_STATE_RUNNING;
Gregory Herrero31927b62015-09-22 15:16:41 +02004309 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004310
4311 if (dwc2_is_device_mode(hsotg)) {
4312 spin_unlock_irqrestore(&hsotg->lock, flags);
4313 return 0; /* why 0 ?? */
4314 }
4315
4316 dwc2_hcd_reinit(hsotg);
4317
4318 /* Initialize and connect root hub if one is not already attached */
4319 if (bus->root_hub) {
4320 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4321 /* Inform the HUB driver to resume */
4322 usb_hcd_resume_root_hub(hcd);
4323 }
4324
4325 spin_unlock_irqrestore(&hsotg->lock, flags);
4326 return 0;
4327}
4328
4329/*
4330 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4331 * stopped.
4332 */
4333static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4334{
4335 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4336 unsigned long flags;
4337
Gregory Herrero5bbf6ce2015-09-22 15:16:48 +02004338 /* Turn off all host-specific interrupts */
4339 dwc2_disable_host_interrupts(hsotg);
4340
Gregory Herrero091473a2015-09-22 15:16:46 +02004341 /* Wait for interrupt processing to finish */
4342 synchronize_irq(hcd->irq);
4343
Paul Zimmerman7359d482013-03-11 17:47:59 -07004344 spin_lock_irqsave(&hsotg->lock, flags);
Gregory Herrero091473a2015-09-22 15:16:46 +02004345 /* Ensure hcd is disconnected */
Douglas Anderson6a659532015-11-19 13:23:14 -08004346 dwc2_hcd_disconnect(hsotg, true);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004347 dwc2_hcd_stop(hsotg);
Gregory Herrero31927b62015-09-22 15:16:41 +02004348 hsotg->lx_state = DWC2_L3;
4349 hcd->state = HC_STATE_HALT;
4350 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004351 spin_unlock_irqrestore(&hsotg->lock, flags);
4352
4353 usleep_range(1000, 3000);
4354}
4355
Gregory Herrero99a65792015-04-29 22:09:13 +02004356static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
4357{
4358 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004359 unsigned long flags;
4360 int ret = 0;
4361 u32 hprt0;
Gregory Herrero99a65792015-04-29 22:09:13 +02004362
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004363 spin_lock_irqsave(&hsotg->lock, flags);
4364
Meng Dongyangf367b722017-08-09 10:34:09 +08004365 if (dwc2_is_device_mode(hsotg))
4366 goto unlock;
4367
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004368 if (hsotg->lx_state != DWC2_L0)
4369 goto unlock;
4370
4371 if (!HCD_HW_ACCESSIBLE(hcd))
4372 goto unlock;
4373
John Stultz866932e2017-01-09 13:10:24 -08004374 if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4375 goto unlock;
4376
Vardan Mikayelyan631a2312018-02-16 14:07:05 +04004377 if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL)
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004378 goto skip_power_saving;
4379
4380 /*
4381 * Drive USB suspend and disable port Power
4382 * if usb bus is not suspended.
4383 */
4384 if (!hsotg->bus_suspended) {
4385 hprt0 = dwc2_read_hprt0(hsotg);
4386 hprt0 |= HPRT0_SUSP;
4387 hprt0 &= ~HPRT0_PWR;
4388 dwc2_writel(hprt0, hsotg->regs + HPRT0);
4389 }
4390
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004391 /* Enter partial_power_down */
4392 ret = dwc2_enter_partial_power_down(hsotg);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004393 if (ret) {
4394 if (ret != -ENOTSUPP)
4395 dev_err(hsotg->dev,
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004396 "enter partial_power_down failed\n");
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004397 goto skip_power_saving;
4398 }
4399
4400 /* Ask phy to be suspended */
4401 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4402 spin_unlock_irqrestore(&hsotg->lock, flags);
4403 usb_phy_set_suspend(hsotg->uphy, true);
4404 spin_lock_irqsave(&hsotg->lock, flags);
4405 }
4406
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004407 /* After entering partial_power_down, hardware is no more accessible */
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004408 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4409
4410skip_power_saving:
Gregory Herrero99a65792015-04-29 22:09:13 +02004411 hsotg->lx_state = DWC2_L2;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004412unlock:
4413 spin_unlock_irqrestore(&hsotg->lock, flags);
4414
4415 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004416}
4417
4418static int _dwc2_hcd_resume(struct usb_hcd *hcd)
4419{
4420 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004421 unsigned long flags;
4422 int ret = 0;
4423
4424 spin_lock_irqsave(&hsotg->lock, flags);
4425
Meng Dongyangf367b722017-08-09 10:34:09 +08004426 if (dwc2_is_device_mode(hsotg))
4427 goto unlock;
4428
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004429 if (hsotg->lx_state != DWC2_L2)
4430 goto unlock;
4431
Vardan Mikayelyan631a2312018-02-16 14:07:05 +04004432 if (hsotg->params.power_down != DWC2_POWER_DOWN_PARAM_PARTIAL) {
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004433 hsotg->lx_state = DWC2_L0;
4434 goto unlock;
4435 }
4436
4437 /*
4438 * Set HW accessible bit before powering on the controller
4439 * since an interrupt may rise.
4440 */
4441 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4442
4443 /*
4444 * Enable power if not already done.
4445 * This must not be spinlocked since duration
4446 * of this call is unknown.
4447 */
4448 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4449 spin_unlock_irqrestore(&hsotg->lock, flags);
4450 usb_phy_set_suspend(hsotg->uphy, false);
4451 spin_lock_irqsave(&hsotg->lock, flags);
4452 }
4453
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004454 /* Exit partial_power_down */
4455 ret = dwc2_exit_partial_power_down(hsotg, true);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004456 if (ret && (ret != -ENOTSUPP))
Vardan Mikayelyan41ba9b92018-02-16 14:06:36 +04004457 dev_err(hsotg->dev, "exit partial_power_down failed\n");
Gregory Herrero99a65792015-04-29 22:09:13 +02004458
4459 hsotg->lx_state = DWC2_L0;
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004460
4461 spin_unlock_irqrestore(&hsotg->lock, flags);
4462
4463 if (hsotg->bus_suspended) {
4464 spin_lock_irqsave(&hsotg->lock, flags);
4465 hsotg->flags.b.port_suspend_change = 1;
4466 spin_unlock_irqrestore(&hsotg->lock, flags);
4467 dwc2_port_resume(hsotg);
4468 } else {
Gregory Herrero5634e012015-09-22 15:16:50 +02004469 /* Wait for controller to correctly update D+/D- level */
4470 usleep_range(3000, 5000);
4471
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004472 /*
4473 * Clear Port Enable and Port Status changes.
4474 * Enable Port Power.
4475 */
4476 dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
4477 HPRT0_ENACHG, hsotg->regs + HPRT0);
4478 /* Wait for controller to detect Port Connect */
Gregory Herrero5634e012015-09-22 15:16:50 +02004479 usleep_range(5000, 7000);
Gregory Herreroa2a23d3f2015-09-22 15:16:40 +02004480 }
4481
4482 return ret;
4483unlock:
4484 spin_unlock_irqrestore(&hsotg->lock, flags);
4485
4486 return ret;
Gregory Herrero99a65792015-04-29 22:09:13 +02004487}
4488
Paul Zimmerman7359d482013-03-11 17:47:59 -07004489/* Returns the current frame number */
4490static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4491{
4492 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4493
4494 return dwc2_hcd_get_frame_number(hsotg);
4495}
4496
4497static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4498 char *fn_name)
4499{
4500#ifdef VERBOSE_DEBUG
4501 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Nicholas Mc Guireefe357f2017-01-12 17:33:26 +01004502 char *pipetype = NULL;
4503 char *speed = NULL;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004504
4505 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4506 dev_vdbg(hsotg->dev, " Device address: %d\n",
4507 usb_pipedevice(urb->pipe));
4508 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
4509 usb_pipeendpoint(urb->pipe),
4510 usb_pipein(urb->pipe) ? "IN" : "OUT");
4511
4512 switch (usb_pipetype(urb->pipe)) {
4513 case PIPE_CONTROL:
4514 pipetype = "CONTROL";
4515 break;
4516 case PIPE_BULK:
4517 pipetype = "BULK";
4518 break;
4519 case PIPE_INTERRUPT:
4520 pipetype = "INTERRUPT";
4521 break;
4522 case PIPE_ISOCHRONOUS:
4523 pipetype = "ISOCHRONOUS";
4524 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004525 }
4526
4527 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
4528 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4529 "IN" : "OUT");
4530
4531 switch (urb->dev->speed) {
4532 case USB_SPEED_HIGH:
4533 speed = "HIGH";
4534 break;
4535 case USB_SPEED_FULL:
4536 speed = "FULL";
4537 break;
4538 case USB_SPEED_LOW:
4539 speed = "LOW";
4540 break;
4541 default:
4542 speed = "UNKNOWN";
4543 break;
4544 }
4545
4546 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
4547 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
4548 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
4549 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
4550 urb->transfer_buffer_length);
Paul Zimmerman157dfaa2013-03-14 13:12:00 -07004551 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
4552 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4553 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
4554 urb->setup_packet, (unsigned long)urb->setup_dma);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004555 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
4556
4557 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4558 int i;
4559
4560 for (i = 0; i < urb->number_of_packets; i++) {
4561 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
4562 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
4563 urb->iso_frame_desc[i].offset,
4564 urb->iso_frame_desc[i].length);
4565 }
4566 }
4567#endif
4568}
4569
4570/*
4571 * Starts processing a USB transfer request specified by a USB Request Block
4572 * (URB). mem_flags indicates the type of memory allocation to use while
4573 * processing this URB.
4574 */
4575static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4576 gfp_t mem_flags)
4577{
4578 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4579 struct usb_host_endpoint *ep = urb->ep;
4580 struct dwc2_hcd_urb *dwc2_urb;
4581 int i;
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004582 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004583 int alloc_bandwidth = 0;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004584 u8 ep_type = 0;
4585 u32 tflags = 0;
4586 void *buf;
4587 unsigned long flags;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004588 struct dwc2_qh *qh;
4589 bool qh_allocated = false;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004590 struct dwc2_qtd *qtd;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004591
Matthijs Kooijmanb49977a2013-04-10 09:55:50 +02004592 if (dbg_urb(urb)) {
4593 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4594 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4595 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004596
John Youn9da51972017-01-17 20:30:27 -08004597 if (!ep)
Paul Zimmerman7359d482013-03-11 17:47:59 -07004598 return -EINVAL;
4599
4600 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4601 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4602 spin_lock_irqsave(&hsotg->lock, flags);
4603 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4604 alloc_bandwidth = 1;
4605 spin_unlock_irqrestore(&hsotg->lock, flags);
4606 }
4607
4608 switch (usb_pipetype(urb->pipe)) {
4609 case PIPE_CONTROL:
4610 ep_type = USB_ENDPOINT_XFER_CONTROL;
4611 break;
4612 case PIPE_ISOCHRONOUS:
4613 ep_type = USB_ENDPOINT_XFER_ISOC;
4614 break;
4615 case PIPE_BULK:
4616 ep_type = USB_ENDPOINT_XFER_BULK;
4617 break;
4618 case PIPE_INTERRUPT:
4619 ep_type = USB_ENDPOINT_XFER_INT;
4620 break;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004621 }
4622
4623 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4624 mem_flags);
4625 if (!dwc2_urb)
4626 return -ENOMEM;
4627
4628 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4629 usb_pipeendpoint(urb->pipe), ep_type,
4630 usb_pipein(urb->pipe),
4631 usb_maxpacket(urb->dev, urb->pipe,
4632 !(usb_pipein(urb->pipe))));
4633
4634 buf = urb->transfer_buffer;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004635
Paul Zimmerman7359d482013-03-11 17:47:59 -07004636 if (hcd->self.uses_dma) {
Paul Zimmerman25a49442013-07-13 14:53:53 -07004637 if (!buf && (urb->transfer_dma & 3)) {
4638 dev_err(hsotg->dev,
4639 "%s: unaligned transfer with no transfer_buffer",
4640 __func__);
4641 retval = -EINVAL;
Gregory Herrero33ad2612015-04-29 22:09:15 +02004642 goto fail0;
Paul Zimmerman25a49442013-07-13 14:53:53 -07004643 }
Paul Zimmerman7359d482013-03-11 17:47:59 -07004644 }
4645
4646 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4647 tflags |= URB_GIVEBACK_ASAP;
4648 if (urb->transfer_flags & URB_ZERO_PACKET)
4649 tflags |= URB_SEND_ZERO_PACKET;
4650
4651 dwc2_urb->priv = urb;
4652 dwc2_urb->buf = buf;
4653 dwc2_urb->dma = urb->transfer_dma;
4654 dwc2_urb->length = urb->transfer_buffer_length;
4655 dwc2_urb->setup_packet = urb->setup_packet;
4656 dwc2_urb->setup_dma = urb->setup_dma;
4657 dwc2_urb->flags = tflags;
4658 dwc2_urb->interval = urb->interval;
4659 dwc2_urb->status = -EINPROGRESS;
4660
4661 for (i = 0; i < urb->number_of_packets; ++i)
4662 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4663 urb->iso_frame_desc[i].offset,
4664 urb->iso_frame_desc[i].length);
4665
4666 urb->hcpriv = dwc2_urb;
John Youn9da51972017-01-17 20:30:27 -08004667 qh = (struct dwc2_qh *)ep->hcpriv;
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004668 /* Create QH for the endpoint if it doesn't exist */
4669 if (!qh) {
4670 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4671 if (!qh) {
4672 retval = -ENOMEM;
4673 goto fail0;
4674 }
4675 ep->hcpriv = qh;
4676 qh_allocated = true;
4677 }
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004678
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004679 qtd = kzalloc(sizeof(*qtd), mem_flags);
4680 if (!qtd) {
4681 retval = -ENOMEM;
4682 goto fail1;
4683 }
4684
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004685 spin_lock_irqsave(&hsotg->lock, flags);
4686 retval = usb_hcd_link_urb_to_ep(hcd, urb);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004687 if (retval)
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004688 goto fail2;
4689
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004690 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4691 if (retval)
4692 goto fail3;
4693
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004694 if (alloc_bandwidth) {
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004695 dwc2_allocate_bus_bandwidth(hcd,
4696 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4697 urb);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004698 }
4699
Gregory Herrero33ad2612015-04-29 22:09:15 +02004700 spin_unlock_irqrestore(&hsotg->lock, flags);
4701
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004702 return 0;
4703
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004704fail3:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004705 dwc2_urb->priv = NULL;
4706 usb_hcd_unlink_urb_from_ep(hcd, urb);
Douglas Anderson16e80212016-01-28 18:19:55 -08004707 if (qh_allocated && qh->channel && qh->channel->qh == qh)
4708 qh->channel->qh = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004709fail2:
Gregory Herrero33ad2612015-04-29 22:09:15 +02004710 spin_unlock_irqrestore(&hsotg->lock, flags);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004711 urb->hcpriv = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004712 kfree(qtd);
Vardan Mikayelyanb0d659022016-04-27 20:20:51 -07004713 qtd = NULL;
Mian Yousaf Kaukabb5a468a2015-06-29 11:05:29 +02004714fail1:
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004715 if (qh_allocated) {
4716 struct dwc2_qtd *qtd2, *qtd2_tmp;
4717
4718 ep->hcpriv = NULL;
4719 dwc2_hcd_qh_unlink(hsotg, qh);
4720 /* Free each QTD in the QH's QTD list */
4721 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
John Youn9da51972017-01-17 20:30:27 -08004722 qtd_list_entry)
Mian Yousaf Kaukabb58e6ce2015-06-29 11:05:28 +02004723 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4724 dwc2_hcd_qh_free(hsotg, qh);
4725 }
Gregory Herrero33ad2612015-04-29 22:09:15 +02004726fail0:
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004727 kfree(dwc2_urb);
4728
Paul Zimmerman7359d482013-03-11 17:47:59 -07004729 return retval;
4730}
4731
4732/*
4733 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4734 */
4735static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4736 int status)
4737{
4738 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004739 int rc;
Paul Zimmerman7359d482013-03-11 17:47:59 -07004740 unsigned long flags;
4741
4742 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4743 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4744
4745 spin_lock_irqsave(&hsotg->lock, flags);
4746
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004747 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4748 if (rc)
4749 goto out;
4750
Paul Zimmerman7359d482013-03-11 17:47:59 -07004751 if (!urb->hcpriv) {
4752 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4753 goto out;
4754 }
4755
4756 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4757
Paul Zimmermanc9e1c902013-07-13 14:53:49 -07004758 usb_hcd_unlink_urb_from_ep(hcd, urb);
4759
Paul Zimmerman7359d482013-03-11 17:47:59 -07004760 kfree(urb->hcpriv);
4761 urb->hcpriv = NULL;
4762
4763 /* Higher layer software sets URB status */
4764 spin_unlock(&hsotg->lock);
4765 usb_hcd_giveback_urb(hcd, urb, status);
4766 spin_lock(&hsotg->lock);
4767
4768 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4769 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
4770out:
4771 spin_unlock_irqrestore(&hsotg->lock, flags);
4772
4773 return rc;
4774}
4775
4776/*
4777 * Frees resources in the DWC_otg controller related to a given endpoint. Also
4778 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4779 * must already be dequeued.
4780 */
4781static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4782 struct usb_host_endpoint *ep)
4783{
4784 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4785
4786 dev_dbg(hsotg->dev,
4787 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4788 ep->desc.bEndpointAddress, ep->hcpriv);
4789 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4790}
4791
4792/*
4793 * Resets endpoint specific parameter values, in current version used to reset
4794 * the data toggle (as a WA). This function can be called from usb_clear_halt
4795 * routine.
4796 */
4797static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4798 struct usb_host_endpoint *ep)
4799{
4800 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004801 unsigned long flags;
4802
4803 dev_dbg(hsotg->dev,
4804 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4805 ep->desc.bEndpointAddress);
4806
Paul Zimmerman7359d482013-03-11 17:47:59 -07004807 spin_lock_irqsave(&hsotg->lock, flags);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004808 dwc2_hcd_endpoint_reset(hsotg, ep);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004809 spin_unlock_irqrestore(&hsotg->lock, flags);
4810}
4811
4812/*
4813 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4814 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4815 * interrupt.
4816 *
4817 * This function is called by the USB core when an interrupt occurs
4818 */
4819static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4820{
4821 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004822
Matthijs Kooijmanca18f4a2013-04-25 23:39:15 +02004823 return dwc2_handle_hcd_intr(hsotg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004824}
4825
4826/*
4827 * Creates Status Change bitmap for the root hub and root port. The bitmap is
4828 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4829 * is the status change indicator for the single root port. Returns 1 if either
4830 * change indicator is 1, otherwise returns 0.
4831 */
4832static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4833{
4834 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4835
4836 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4837 return buf[0] != 0;
4838}
4839
4840/* Handles hub class-specific requests */
4841static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4842 u16 windex, char *buf, u16 wlength)
4843{
4844 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4845 wvalue, windex, buf, wlength);
4846 return retval;
4847}
4848
4849/* Handles hub TT buffer clear completions */
4850static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4851 struct usb_host_endpoint *ep)
4852{
4853 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4854 struct dwc2_qh *qh;
4855 unsigned long flags;
4856
4857 qh = ep->hcpriv;
4858 if (!qh)
4859 return;
4860
4861 spin_lock_irqsave(&hsotg->lock, flags);
4862 qh->tt_buffer_dirty = 0;
4863
4864 if (hsotg->flags.b.port_connect_status)
4865 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4866
4867 spin_unlock_irqrestore(&hsotg->lock, flags);
4868}
4869
Chen Yuca8b0332017-01-23 15:00:18 -08004870/*
4871 * HPRT0_SPD_HIGH_SPEED: high speed
4872 * HPRT0_SPD_FULL_SPEED: full speed
4873 */
4874static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4875{
4876 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4877
4878 if (hsotg->params.speed == speed)
4879 return;
4880
4881 hsotg->params.speed = speed;
4882 queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4883}
4884
4885static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4886{
4887 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4888
4889 if (!hsotg->params.change_speed_quirk)
4890 return;
4891
4892 /*
4893 * On removal, set speed to default high-speed.
4894 */
4895 if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4896 udev->parent->speed < USB_SPEED_HIGH) {
4897 dev_info(hsotg->dev, "Set speed to default high-speed\n");
4898 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4899 }
4900}
4901
4902static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4903{
4904 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4905
4906 if (!hsotg->params.change_speed_quirk)
4907 return 0;
4908
4909 if (udev->speed == USB_SPEED_HIGH) {
4910 dev_info(hsotg->dev, "Set speed to high-speed\n");
4911 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4912 } else if ((udev->speed == USB_SPEED_FULL ||
4913 udev->speed == USB_SPEED_LOW)) {
4914 /*
4915 * Change speed setting to full-speed if there's
4916 * a full-speed or low-speed device plugged in.
4917 */
4918 dev_info(hsotg->dev, "Set speed to full-speed\n");
4919 dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4920 }
4921
4922 return 0;
4923}
4924
Paul Zimmerman7359d482013-03-11 17:47:59 -07004925static struct hc_driver dwc2_hc_driver = {
4926 .description = "dwc2_hsotg",
4927 .product_desc = "DWC OTG Controller",
4928 .hcd_priv_size = sizeof(struct wrapper_priv_data),
4929
4930 .irq = _dwc2_hcd_irq,
Douglas Anderson8add17c2016-01-28 18:20:00 -08004931 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004932
4933 .start = _dwc2_hcd_start,
4934 .stop = _dwc2_hcd_stop,
4935 .urb_enqueue = _dwc2_hcd_urb_enqueue,
4936 .urb_dequeue = _dwc2_hcd_urb_dequeue,
4937 .endpoint_disable = _dwc2_hcd_endpoint_disable,
4938 .endpoint_reset = _dwc2_hcd_endpoint_reset,
4939 .get_frame_number = _dwc2_hcd_get_frame_number,
4940
4941 .hub_status_data = _dwc2_hcd_hub_status_data,
4942 .hub_control = _dwc2_hcd_hub_control,
4943 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
Gregory Herrero99a65792015-04-29 22:09:13 +02004944
4945 .bus_suspend = _dwc2_hcd_suspend,
4946 .bus_resume = _dwc2_hcd_resume,
Douglas Anderson3bc04e22016-01-28 18:19:53 -08004947
4948 .map_urb_for_dma = dwc2_map_urb_for_dma,
4949 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
Paul Zimmerman7359d482013-03-11 17:47:59 -07004950};
4951
4952/*
4953 * Frees secondary storage associated with the dwc2_hsotg structure contained
4954 * in the struct usb_hcd field
4955 */
4956static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
4957{
4958 u32 ahbcfg;
4959 u32 dctl;
4960 int i;
4961
4962 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
4963
4964 /* Free memory for QH/QTD lists */
4965 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08004966 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004967 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
4968 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
4969 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
4970 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
4971 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
4972
4973 /* Free memory for the host channels */
4974 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
4975 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
4976
John Youn9da51972017-01-17 20:30:27 -08004977 if (chan) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07004978 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
4979 i, chan);
4980 hsotg->hc_ptr_array[i] = NULL;
4981 kfree(chan);
4982 }
4983 }
4984
John Youn95832c02017-01-23 14:57:26 -08004985 if (hsotg->params.host_dma) {
Paul Zimmerman7359d482013-03-11 17:47:59 -07004986 if (hsotg->status_buf) {
4987 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
4988 hsotg->status_buf,
4989 hsotg->status_buf_dma);
4990 hsotg->status_buf = NULL;
4991 }
4992 } else {
4993 kfree(hsotg->status_buf);
4994 hsotg->status_buf = NULL;
4995 }
4996
Antti Seppälä95c8bc32015-08-20 21:41:07 +03004997 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07004998
4999 /* Disable all interrupts */
5000 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005001 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
5002 dwc2_writel(0, hsotg->regs + GINTMSK);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005003
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005004 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005005 dctl = dwc2_readl(hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005006 dctl |= DCTL_SFTDISCON;
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005007 dwc2_writel(dctl, hsotg->regs + DCTL);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005008 }
5009
5010 if (hsotg->wq_otg) {
5011 if (!cancel_work_sync(&hsotg->wf_otg))
5012 flush_workqueue(hsotg->wq_otg);
5013 destroy_workqueue(hsotg->wq_otg);
5014 }
5015
Paul Zimmerman7359d482013-03-11 17:47:59 -07005016 del_timer(&hsotg->wkp_timer);
5017}
5018
5019static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5020{
5021 /* Turn off all host-specific interrupts */
5022 dwc2_disable_host_interrupts(hsotg);
5023
5024 dwc2_hcd_free(hsotg);
5025}
5026
Matthijs Kooijman8284f932013-04-11 18:43:47 +02005027/*
Paul Zimmerman7359d482013-03-11 17:47:59 -07005028 * Initializes the HCD. This function allocates memory for and initializes the
5029 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5030 * USB bus with the core and calls the hc_driver->start() function. It returns
5031 * a negative error on failure.
5032 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005033int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005034{
Heiner Kallweit348becd2017-01-25 23:10:51 +01005035 struct platform_device *pdev = to_platform_device(hsotg->dev);
5036 struct resource *res;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005037 struct usb_hcd *hcd;
5038 struct dwc2_host_chan *channel;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005039 u32 hcfg;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005040 int i, num_channels;
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005041 int retval;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005042
Dinh Nguyenf5500ec2014-11-11 11:13:39 -06005043 if (usb_disabled())
5044 return -ENODEV;
5045
Paul Zimmermane62662c2013-03-25 17:03:35 -07005046 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005047
Matthijs Kooijman9badec22013-08-30 18:45:21 +02005048 retval = -ENOMEM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005049
Antti Seppälä95c8bc32015-08-20 21:41:07 +03005050 hcfg = dwc2_readl(hsotg->regs + HCFG);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005051 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005052
5053#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5054 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
5055 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5056 if (!hsotg->frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005057 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005058 hsotg->last_frame_num_array = kzalloc(
5059 sizeof(*hsotg->last_frame_num_array) *
5060 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5061 if (!hsotg->last_frame_num_array)
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005062 goto error1;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005063#endif
Douglas Anderson483bb252016-01-28 18:20:07 -08005064 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005065
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005066 /* Check if the bus driver or platform code has setup a dma_mask */
John Youn95832c02017-01-23 14:57:26 -08005067 if (hsotg->params.host_dma &&
John Youn9da51972017-01-17 20:30:27 -08005068 !hsotg->dev->dma_mask) {
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005069 dev_warn(hsotg->dev,
5070 "dma_mask not set, disabling DMA\n");
Nicholas Mc Guirefdb09b32017-01-12 16:55:02 +01005071 hsotg->params.host_dma = false;
John Youn95832c02017-01-23 14:57:26 -08005072 hsotg->params.dma_desc_enable = false;
Matthijs Kooijmana0112f42013-07-19 11:34:22 +02005073 }
5074
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005075 /* Set device flags indicating whether the HCD supports DMA */
John Youn95832c02017-01-23 14:57:26 -08005076 if (hsotg->params.host_dma) {
Paul Zimmerman30885312013-05-24 16:27:56 -07005077 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5078 dev_warn(hsotg->dev, "can't set DMA mask\n");
Paul Zimmerman25a49442013-07-13 14:53:53 -07005079 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5080 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005081 }
5082
Chen Yuca8b0332017-01-23 15:00:18 -08005083 if (hsotg->params.change_speed_quirk) {
5084 dwc2_hc_driver.free_dev = dwc2_free_dev;
5085 dwc2_hc_driver.reset_device = dwc2_reset_device;
5086 }
5087
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005088 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5089 if (!hcd)
5090 goto error1;
5091
John Youn95832c02017-01-23 14:57:26 -08005092 if (!hsotg->params.host_dma)
Matthijs Kooijman7de76ee2013-07-19 11:34:23 +02005093 hcd->self.uses_dma = 0;
5094
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005095 hcd->has_tt = 1;
5096
Heiner Kallweit348becd2017-01-25 23:10:51 +01005097 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5098 hcd->rsrc_start = res->start;
5099 hcd->rsrc_len = resource_size(res);
5100
John Youn9da51972017-01-17 20:30:27 -08005101 ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005102 hsotg->priv = hcd;
5103
Paul Zimmerman7359d482013-03-11 17:47:59 -07005104 /*
5105 * Disable the global interrupt until all the interrupt handlers are
5106 * installed
5107 */
5108 dwc2_disable_global_interrupts(hsotg);
5109
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005110 /* Initialize the DWC_otg core, and select the Phy type */
Douglas Anderson0fe239b2015-12-17 11:14:40 -08005111 retval = dwc2_core_init(hsotg, true);
Matthijs Kooijman6706c722013-04-11 17:52:41 +02005112 if (retval)
5113 goto error2;
5114
Paul Zimmerman7359d482013-03-11 17:47:59 -07005115 /* Create new workqueue and init work */
Wei Yongjun53510352013-04-12 22:41:48 +08005116 retval = -ENOMEM;
Bhaktipriya Shridharec7b1262016-07-28 13:57:29 +05305117 hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005118 if (!hsotg->wq_otg) {
5119 dev_err(hsotg->dev, "Failed to create workqueue\n");
5120 goto error2;
5121 }
5122 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5123
Kees Cooke99e88a2017-10-16 14:43:17 -07005124 timer_setup(&hsotg->wkp_timer, dwc2_wakeup_detected, 0);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005125
5126 /* Initialize the non-periodic schedule */
5127 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
Douglas Anderson38d2b5f2017-12-12 10:30:31 -08005128 INIT_LIST_HEAD(&hsotg->non_periodic_sched_waiting);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005129 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5130
5131 /* Initialize the periodic schedule */
5132 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5133 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5134 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5135 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5136
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005137 INIT_LIST_HEAD(&hsotg->split_order);
5138
Paul Zimmerman7359d482013-03-11 17:47:59 -07005139 /*
5140 * Create a host channel descriptor for each host channel implemented
5141 * in the controller. Initialize the channel descriptor array.
5142 */
5143 INIT_LIST_HEAD(&hsotg->free_hc_list);
John Younbea8e862016-11-03 17:55:53 -07005144 num_channels = hsotg->params.host_channels;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005145 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5146
5147 for (i = 0; i < num_channels; i++) {
5148 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
John Youn9da51972017-01-17 20:30:27 -08005149 if (!channel)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005150 goto error3;
5151 channel->hc_num = i;
Douglas Andersonc9c8ac02016-01-28 18:19:57 -08005152 INIT_LIST_HEAD(&channel->split_order_list_entry);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005153 hsotg->hc_ptr_array[i] = channel;
5154 }
5155
5156 /* Initialize hsotg start work */
5157 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5158
5159 /* Initialize port reset work */
5160 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5161
5162 /*
5163 * Allocate space for storing data on status transactions. Normally no
5164 * data is sent, but this space acts as a bit bucket. This must be
5165 * done after usb_add_hcd since that function allocates the DMA buffer
5166 * pool.
5167 */
John Youn95832c02017-01-23 14:57:26 -08005168 if (hsotg->params.host_dma)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005169 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5170 DWC2_HCD_STATUS_BUF_SIZE,
5171 &hsotg->status_buf_dma, GFP_KERNEL);
5172 else
5173 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5174 GFP_KERNEL);
5175
5176 if (!hsotg->status_buf)
5177 goto error3;
5178
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005179 /*
5180 * Create kmem caches to handle descriptor buffers in descriptor
5181 * DMA mode.
5182 * Alignment must be set to 512 bytes.
5183 */
John Younbea8e862016-11-03 17:55:53 -07005184 if (hsotg->params.dma_desc_enable ||
5185 hsotg->params.dma_desc_fs_enable) {
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005186 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005187 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005188 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
5189 NULL);
5190 if (!hsotg->desc_gen_cache) {
5191 dev_err(hsotg->dev,
5192 "unable to create dwc2 generic desc cache\n");
5193
5194 /*
5195 * Disable descriptor dma mode since it will not be
5196 * usable.
5197 */
John Youn95832c02017-01-23 14:57:26 -08005198 hsotg->params.dma_desc_enable = false;
5199 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005200 }
5201
5202 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
Vahram Aharonyanec703252016-11-09 19:27:43 -08005203 sizeof(struct dwc2_dma_desc) *
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005204 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
5205 if (!hsotg->desc_hsisoc_cache) {
5206 dev_err(hsotg->dev,
5207 "unable to create dwc2 hs isoc desc cache\n");
5208
5209 kmem_cache_destroy(hsotg->desc_gen_cache);
5210
5211 /*
5212 * Disable descriptor dma mode since it will not be
5213 * usable.
5214 */
John Youn95832c02017-01-23 14:57:26 -08005215 hsotg->params.dma_desc_enable = false;
5216 hsotg->params.dma_desc_fs_enable = false;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005217 }
5218 }
5219
Paul Zimmerman7359d482013-03-11 17:47:59 -07005220 hsotg->otg_port = 1;
5221 hsotg->frame_list = NULL;
5222 hsotg->frame_list_dma = 0;
5223 hsotg->periodic_qh_count = 0;
5224
5225 /* Initiate lx_state to L3 disconnected state */
5226 hsotg->lx_state = DWC2_L3;
5227
5228 hcd->self.otg_port = hsotg->otg_port;
5229
5230 /* Don't support SG list at this point */
5231 hcd->self.sg_tablesize = 0;
5232
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005233 if (!IS_ERR_OR_NULL(hsotg->uphy))
5234 otg_set_host(hsotg->uphy->otg, &hcd->self);
5235
Paul Zimmerman7359d482013-03-11 17:47:59 -07005236 /*
5237 * Finish generic HCD initialization and start the HCD. This function
5238 * allocates the DMA buffer pool, registers the USB bus, requests the
5239 * IRQ line, and calls hcd_start method.
5240 */
Heiner Kallweit4fe160d2017-01-25 23:13:37 +01005241 retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005242 if (retval < 0)
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005243 goto error4;
Paul Zimmerman7359d482013-03-11 17:47:59 -07005244
Peter Chen3c9740a2013-11-05 10:46:02 +08005245 device_wakeup_enable(hcd->self.controller);
5246
Paul Zimmerman7359d482013-03-11 17:47:59 -07005247 dwc2_hcd_dump_state(hsotg);
5248
5249 dwc2_enable_global_interrupts(hsotg);
5250
5251 return 0;
5252
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005253error4:
5254 kmem_cache_destroy(hsotg->desc_gen_cache);
5255 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005256error3:
5257 dwc2_hcd_release(hsotg);
5258error2:
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005259 usb_put_hcd(hcd);
5260error1:
Paul Zimmerman7359d482013-03-11 17:47:59 -07005261
5262#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5263 kfree(hsotg->last_frame_num_array);
5264 kfree(hsotg->frame_num_array);
5265#endif
5266
Paul Zimmermane62662c2013-03-25 17:03:35 -07005267 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005268 return retval;
5269}
Paul Zimmerman7359d482013-03-11 17:47:59 -07005270
5271/*
5272 * Removes the HCD.
5273 * Frees memory and resources associated with the HCD and deregisters the bus.
5274 */
Paul Zimmermane62662c2013-03-25 17:03:35 -07005275void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
Paul Zimmerman7359d482013-03-11 17:47:59 -07005276{
5277 struct usb_hcd *hcd;
5278
Paul Zimmermane62662c2013-03-25 17:03:35 -07005279 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
Paul Zimmerman7359d482013-03-11 17:47:59 -07005280
5281 hcd = dwc2_hsotg_to_hcd(hsotg);
Paul Zimmermane62662c2013-03-25 17:03:35 -07005282 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005283
5284 if (!hcd) {
Paul Zimmermane62662c2013-03-25 17:03:35 -07005285 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
Paul Zimmerman7359d482013-03-11 17:47:59 -07005286 __func__);
5287 return;
5288 }
5289
Mian Yousaf Kaukab9df4cea2015-04-29 22:09:12 +02005290 if (!IS_ERR_OR_NULL(hsotg->uphy))
5291 otg_set_host(hsotg->uphy->otg, NULL);
5292
Paul Zimmerman7359d482013-03-11 17:47:59 -07005293 usb_remove_hcd(hcd);
5294 hsotg->priv = NULL;
Gregory Herrero3b5fcc92015-11-20 11:49:31 +01005295
5296 kmem_cache_destroy(hsotg->desc_gen_cache);
5297 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5298
Paul Zimmerman7359d482013-03-11 17:47:59 -07005299 dwc2_hcd_release(hsotg);
Paul Zimmermanba0e60d2013-03-25 17:03:36 -07005300 usb_put_hcd(hcd);
Paul Zimmerman7359d482013-03-11 17:47:59 -07005301
5302#ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5303 kfree(hsotg->last_frame_num_array);
5304 kfree(hsotg->frame_num_array);
5305#endif
Paul Zimmerman7359d482013-03-11 17:47:59 -07005306}
John Youn58e52ff6a2016-02-23 19:54:57 -08005307
5308/**
5309 * dwc2_backup_host_registers() - Backup controller host registers.
5310 * When suspending usb bus, registers needs to be backuped
5311 * if controller power is disabled once suspended.
5312 *
5313 * @hsotg: Programming view of the DWC_otg controller
5314 */
5315int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
5316{
5317 struct dwc2_hregs_backup *hr;
5318 int i;
5319
5320 dev_dbg(hsotg->dev, "%s\n", __func__);
5321
5322 /* Backup Host regs */
5323 hr = &hsotg->hr_backup;
5324 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
5325 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
John Younbea8e862016-11-03 17:55:53 -07005326 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005327 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
5328
5329 hr->hprt0 = dwc2_read_hprt0(hsotg);
5330 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
Vardan Mikayelyan66a36092018-02-16 14:09:19 +04005331 hr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
John Youn58e52ff6a2016-02-23 19:54:57 -08005332 hr->valid = true;
5333
5334 return 0;
5335}
5336
5337/**
5338 * dwc2_restore_host_registers() - Restore controller host registers.
5339 * When resuming usb bus, device registers needs to be restored
5340 * if controller power were disabled.
5341 *
5342 * @hsotg: Programming view of the DWC_otg controller
5343 */
5344int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
5345{
5346 struct dwc2_hregs_backup *hr;
5347 int i;
5348
5349 dev_dbg(hsotg->dev, "%s\n", __func__);
5350
5351 /* Restore host regs */
5352 hr = &hsotg->hr_backup;
5353 if (!hr->valid) {
5354 dev_err(hsotg->dev, "%s: no host registers to restore\n",
5355 __func__);
5356 return -EINVAL;
5357 }
5358 hr->valid = false;
5359
5360 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5361 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
5362
John Younbea8e862016-11-03 17:55:53 -07005363 for (i = 0; i < hsotg->params.host_channels; ++i)
John Youn58e52ff6a2016-02-23 19:54:57 -08005364 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
5365
5366 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
5367 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
Vardan Mikayelyan66a36092018-02-16 14:09:19 +04005368 dwc2_writel(hr->hptxfsiz, hsotg->regs + HPTXFSIZ);
John Youn58e52ff6a2016-02-23 19:54:57 -08005369 hsotg->frame_number = 0;
5370
5371 return 0;
5372}
Vardan Mikayelyanc5c403dc2018-02-16 14:10:13 +04005373
5374/**
5375 * dwc2_host_enter_hibernation() - Put controller in Hibernation.
5376 *
5377 * @hsotg: Programming view of the DWC_otg controller
5378 */
5379int dwc2_host_enter_hibernation(struct dwc2_hsotg *hsotg)
5380{
5381 unsigned long flags;
5382 int ret = 0;
5383 u32 hprt0;
5384 u32 pcgcctl;
5385 u32 gusbcfg;
5386 u32 gpwrdn;
5387
5388 dev_dbg(hsotg->dev, "Preparing host for hibernation\n");
5389 ret = dwc2_backup_global_registers(hsotg);
5390 if (ret) {
5391 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
5392 __func__);
5393 return ret;
5394 }
5395 ret = dwc2_backup_host_registers(hsotg);
5396 if (ret) {
5397 dev_err(hsotg->dev, "%s: failed to backup host registers\n",
5398 __func__);
5399 return ret;
5400 }
5401
5402 /* Enter USB Suspend Mode */
5403 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
5404 hprt0 |= HPRT0_SUSP;
5405 hprt0 &= ~HPRT0_ENA;
5406 dwc2_writel(hprt0, hsotg->regs + HPRT0);
5407
5408 /* Wait for the HPRT0.PrtSusp register field to be set */
5409 if (dwc2_hsotg_wait_bit_set(hsotg, HPRT0, HPRT0_SUSP, 300))
5410 dev_warn(hsotg->dev, "Suspend wasn't genereted\n");
5411
5412 /*
5413 * We need to disable interrupts to prevent servicing of any IRQ
5414 * during going to hibernation
5415 */
5416 spin_lock_irqsave(&hsotg->lock, flags);
5417 hsotg->lx_state = DWC2_L2;
5418
5419 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
5420 if (gusbcfg & GUSBCFG_ULPI_UTMI_SEL) {
5421 /* ULPI interface */
5422 /* Suspend the Phy Clock */
5423 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
5424 pcgcctl |= PCGCTL_STOPPCLK;
5425 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
5426 udelay(10);
5427
5428 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5429 gpwrdn |= GPWRDN_PMUACTV;
5430 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5431 udelay(10);
5432 } else {
5433 /* UTMI+ Interface */
5434 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5435 gpwrdn |= GPWRDN_PMUACTV;
5436 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5437 udelay(10);
5438
5439 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
5440 pcgcctl |= PCGCTL_STOPPCLK;
5441 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
5442 udelay(10);
5443 }
5444
5445 /* Enable interrupts from wake up logic */
5446 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5447 gpwrdn |= GPWRDN_PMUINTSEL;
5448 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5449 udelay(10);
5450
5451 /* Unmask host mode interrupts in GPWRDN */
5452 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5453 gpwrdn |= GPWRDN_DISCONN_DET_MSK;
5454 gpwrdn |= GPWRDN_LNSTSCHG_MSK;
5455 gpwrdn |= GPWRDN_STS_CHGINT_MSK;
5456 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5457 udelay(10);
5458
5459 /* Enable Power Down Clamp */
5460 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5461 gpwrdn |= GPWRDN_PWRDNCLMP;
5462 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5463 udelay(10);
5464
5465 /* Switch off VDD */
5466 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5467 gpwrdn |= GPWRDN_PWRDNSWTCH;
5468 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5469
5470 hsotg->hibernated = 1;
5471 hsotg->bus_suspended = 1;
5472 dev_dbg(hsotg->dev, "Host hibernation completed\n");
5473 spin_unlock_irqrestore(&hsotg->lock, flags);
5474 return ret;
5475}
5476
5477/*
5478 * dwc2_host_exit_hibernation()
5479 *
5480 * @hsotg: Programming view of the DWC_otg controller
5481 * @rem_wakeup: indicates whether resume is initiated by Device or Host.
5482 * @param reset: indicates whether resume is initiated by Reset.
5483 *
5484 * Return: non-zero if failed to enter to hibernation.
5485 *
5486 * This function is for exiting from Host mode hibernation by
5487 * Host Initiated Resume/Reset and Device Initiated Remote-Wakeup.
5488 */
5489int dwc2_host_exit_hibernation(struct dwc2_hsotg *hsotg, int rem_wakeup,
5490 int reset)
5491{
5492 u32 gpwrdn;
5493 u32 hprt0;
5494 int ret = 0;
5495 struct dwc2_gregs_backup *gr;
5496 struct dwc2_hregs_backup *hr;
5497
5498 gr = &hsotg->gr_backup;
5499 hr = &hsotg->hr_backup;
5500
5501 dev_dbg(hsotg->dev,
5502 "%s: called with rem_wakeup = %d reset = %d\n",
5503 __func__, rem_wakeup, reset);
5504
5505 dwc2_hib_restore_common(hsotg, rem_wakeup, 1);
5506 hsotg->hibernated = 0;
5507
5508 /*
5509 * This step is not described in functional spec but if not wait for
5510 * this delay, mismatch interrupts occurred because just after restore
5511 * core is in Device mode(gintsts.curmode == 0)
5512 */
5513 mdelay(100);
5514
5515 /* Clear all pending interupts */
5516 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5517
5518 /* De-assert Restore */
5519 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5520 gpwrdn &= ~GPWRDN_RESTORE;
5521 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5522 udelay(10);
5523
5524 /* Restore GUSBCFG, HCFG */
5525 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
5526 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5527
5528 /* De-assert Wakeup Logic */
5529 gpwrdn = dwc2_readl(hsotg->regs + GPWRDN);
5530 gpwrdn &= ~GPWRDN_PMUACTV;
5531 dwc2_writel(gpwrdn, hsotg->regs + GPWRDN);
5532 udelay(10);
5533
5534 hprt0 = hr->hprt0;
5535 hprt0 |= HPRT0_PWR;
5536 hprt0 &= ~HPRT0_ENA;
5537 hprt0 &= ~HPRT0_SUSP;
5538 dwc2_writel(hprt0, hsotg->regs + HPRT0);
5539
5540 hprt0 = hr->hprt0;
5541 hprt0 |= HPRT0_PWR;
5542 hprt0 &= ~HPRT0_ENA;
5543 hprt0 &= ~HPRT0_SUSP;
5544
5545 if (reset) {
5546 hprt0 |= HPRT0_RST;
5547 dwc2_writel(hprt0, hsotg->regs + HPRT0);
5548
5549 /* Wait for Resume time and then program HPRT again */
5550 mdelay(60);
5551 hprt0 &= ~HPRT0_RST;
5552 dwc2_writel(hprt0, hsotg->regs + HPRT0);
5553 } else {
5554 hprt0 |= HPRT0_RES;
5555 dwc2_writel(hprt0, hsotg->regs + HPRT0);
5556
5557 /* Wait for Resume time and then program HPRT again */
5558 mdelay(100);
5559 hprt0 &= ~HPRT0_RES;
5560 dwc2_writel(hprt0, hsotg->regs + HPRT0);
5561 }
5562 /* Clear all interrupt status */
5563 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
5564 hprt0 |= HPRT0_CONNDET;
5565 hprt0 |= HPRT0_ENACHG;
5566 hprt0 &= ~HPRT0_ENA;
5567 dwc2_writel(hprt0, hsotg->regs + HPRT0);
5568
5569 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
5570
5571 /* Clear all pending interupts */
5572 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
5573
5574 /* Restore global registers */
5575 ret = dwc2_restore_global_registers(hsotg);
5576 if (ret) {
5577 dev_err(hsotg->dev, "%s: failed to restore registers\n",
5578 __func__);
5579 return ret;
5580 }
5581
5582 /* Restore host registers */
5583 ret = dwc2_restore_host_registers(hsotg);
5584 if (ret) {
5585 dev_err(hsotg->dev, "%s: failed to restore host registers\n",
5586 __func__);
5587 return ret;
5588 }
5589
5590 hsotg->hibernated = 0;
5591 hsotg->bus_suspended = 0;
5592 hsotg->lx_state = DWC2_L0;
5593 dev_dbg(hsotg->dev, "Host hibernation restore complete\n");
5594 return ret;
5595}