2 * hcd.c - DesignWare HS OTG Controller host-mode routines
4 * Copyright (C) 2004-2013 Synopsys, Inc.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 * This file contains the core HCD code, and implements the Linux hc_driver
41 #include <linux/kernel.h>
42 #include <linux/module.h>
43 #include <linux/spinlock.h>
44 #include <linux/interrupt.h>
45 #include <linux/platform_device.h>
46 #include <linux/dma-mapping.h>
47 #include <linux/delay.h>
49 #include <linux/slab.h>
50 #include <linux/usb.h>
52 #include <linux/usb/hcd.h>
53 #include <linux/usb/ch11.h>
58 static void dwc2_port_resume(struct dwc2_hsotg *hsotg);
61 * =========================================================================
62 * Host Core Layer Functions
63 * =========================================================================
67 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
68 * used in both device and host modes
70 * @hsotg: Programming view of the DWC_otg controller
72 static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
76 /* Clear any pending OTG Interrupts */
77 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
79 /* Clear any pending interrupts */
80 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
82 /* Enable the interrupts in the GINTMSK */
83 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
85 if (!hsotg->params.host_dma)
86 intmsk |= GINTSTS_RXFLVL;
87 if (!hsotg->params.external_id_pin_ctl)
88 intmsk |= GINTSTS_CONIDSTSCHNG;
90 intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
93 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
97 * Initializes the FSLSPClkSel field of the HCFG register depending on the
100 static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
104 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
105 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
106 hsotg->params.ulpi_fs_ls) ||
107 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
109 val = HCFG_FSLSPCLKSEL_48_MHZ;
111 /* High speed PHY running at full speed or high speed */
112 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
115 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
116 hcfg = dwc2_readl(hsotg->regs + HCFG);
117 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
118 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
119 dwc2_writel(hcfg, hsotg->regs + HCFG);
122 static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
128 * core_init() is now called on every switch so only call the
129 * following for the first time through
132 dev_dbg(hsotg->dev, "FS PHY selected\n");
134 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
135 if (!(usbcfg & GUSBCFG_PHYSEL)) {
136 usbcfg |= GUSBCFG_PHYSEL;
137 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
139 /* Reset after a PHY select */
140 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
144 "%s: Reset failed, aborting", __func__);
151 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
152 * do this on HNP Dev/Host mode switches (done in dev_init and
155 if (dwc2_is_host_mode(hsotg))
156 dwc2_init_fs_ls_pclk_sel(hsotg);
158 if (hsotg->params.i2c_enable) {
159 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
161 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
162 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
163 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
164 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
166 /* Program GI2CCTL.I2CEn */
167 i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
168 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
169 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
170 i2cctl &= ~GI2CCTL_I2CEN;
171 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
172 i2cctl |= GI2CCTL_I2CEN;
173 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
179 static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
181 u32 usbcfg, usbcfg_old;
187 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
191 * HS PHY parameters. These parameters are preserved during soft reset
192 * so only program the first time. Do a soft reset immediately after
195 switch (hsotg->params.phy_type) {
196 case DWC2_PHY_TYPE_PARAM_ULPI:
198 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
199 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
200 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
201 if (hsotg->params.phy_ulpi_ddr)
202 usbcfg |= GUSBCFG_DDRSEL;
204 case DWC2_PHY_TYPE_PARAM_UTMI:
205 /* UTMI+ interface */
206 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
207 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
208 if (hsotg->params.phy_utmi_width == 16)
209 usbcfg |= GUSBCFG_PHYIF16;
212 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
216 if (usbcfg != usbcfg_old) {
217 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
219 /* Reset after setting the PHY parameters */
220 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
223 "%s: Reset failed, aborting", __func__);
231 static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
236 if ((hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
237 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) &&
238 hsotg->params.phy_type == DWC2_PHY_TYPE_PARAM_FS) {
239 /* If FS/LS mode with FS/LS PHY */
240 retval = dwc2_fs_phy_init(hsotg, select_phy);
245 retval = dwc2_hs_phy_init(hsotg, select_phy);
250 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
251 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
252 hsotg->params.ulpi_fs_ls) {
253 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
254 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
255 usbcfg |= GUSBCFG_ULPI_FS_LS;
256 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
257 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
259 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
260 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
261 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
262 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
268 static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
270 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
272 switch (hsotg->hw_params.arch) {
273 case GHWCFG2_EXT_DMA_ARCH:
274 dev_err(hsotg->dev, "External DMA Mode not supported\n");
277 case GHWCFG2_INT_DMA_ARCH:
278 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
279 if (hsotg->params.ahbcfg != -1) {
280 ahbcfg &= GAHBCFG_CTRL_MASK;
281 ahbcfg |= hsotg->params.ahbcfg &
286 case GHWCFG2_SLAVE_ONLY_ARCH:
288 dev_dbg(hsotg->dev, "Slave Only Mode\n");
292 dev_dbg(hsotg->dev, "host_dma:%d dma_desc_enable:%d\n",
293 hsotg->params.host_dma,
294 hsotg->params.dma_desc_enable);
296 if (hsotg->params.host_dma) {
297 if (hsotg->params.dma_desc_enable)
298 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
300 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
302 dev_dbg(hsotg->dev, "Using Slave mode\n");
303 hsotg->params.dma_desc_enable = false;
306 if (hsotg->params.host_dma)
307 ahbcfg |= GAHBCFG_DMA_EN;
309 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
314 static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
318 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
319 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
321 switch (hsotg->hw_params.op_mode) {
322 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
323 if (hsotg->params.otg_cap ==
324 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
325 usbcfg |= GUSBCFG_HNPCAP;
326 if (hsotg->params.otg_cap !=
327 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
328 usbcfg |= GUSBCFG_SRPCAP;
331 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
332 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
333 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
334 if (hsotg->params.otg_cap !=
335 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
336 usbcfg |= GUSBCFG_SRPCAP;
339 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
340 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
341 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
346 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
350 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
352 * @hsotg: Programming view of DWC_otg controller
354 static void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
358 dev_dbg(hsotg->dev, "%s()\n", __func__);
360 /* Disable all interrupts */
361 dwc2_writel(0, hsotg->regs + GINTMSK);
362 dwc2_writel(0, hsotg->regs + HAINTMSK);
364 /* Enable the common interrupts */
365 dwc2_enable_common_interrupts(hsotg);
367 /* Enable host mode interrupts without disturbing common interrupts */
368 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
369 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
370 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
374 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
376 * @hsotg: Programming view of DWC_otg controller
378 static void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
380 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
382 /* Disable host mode interrupts without disturbing common interrupts */
383 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
384 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
385 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
389 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
390 * For system that have a total fifo depth that is smaller than the default
393 * @hsotg: Programming view of DWC_otg controller
395 static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
397 struct dwc2_core_params *params = &hsotg->params;
398 struct dwc2_hw_params *hw = &hsotg->hw_params;
399 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
401 total_fifo_size = hw->total_fifo_size;
402 rxfsiz = params->host_rx_fifo_size;
403 nptxfsiz = params->host_nperio_tx_fifo_size;
404 ptxfsiz = params->host_perio_tx_fifo_size;
407 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
408 * allocation with support for high bandwidth endpoints. Synopsys
409 * defines MPS(Max Packet size) for a periodic EP=1024, and for
410 * non-periodic as 512.
412 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
414 * For Buffer DMA mode/Scatter Gather DMA mode
415 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
416 * with n = number of host channel.
417 * 2 * ((1024/4) + 2) = 516
419 rxfsiz = 516 + hw->host_channels;
422 * min non-periodic tx fifo depth
423 * 2 * (largest non-periodic USB packet used / 4)
429 * min periodic tx fifo depth
430 * (largest packet size*MC)/4
435 params->host_rx_fifo_size = rxfsiz;
436 params->host_nperio_tx_fifo_size = nptxfsiz;
437 params->host_perio_tx_fifo_size = ptxfsiz;
441 * If the summation of RX, NPTX and PTX fifo sizes is still
442 * bigger than the total_fifo_size, then we have a problem.
444 * We won't be able to allocate as many endpoints. Right now,
445 * we're just printing an error message, but ideally this FIFO
446 * allocation algorithm would be improved in the future.
448 * FIXME improve this FIFO allocation algorithm.
450 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
451 dev_err(hsotg->dev, "invalid fifo sizes\n");
454 static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
456 struct dwc2_core_params *params = &hsotg->params;
457 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
459 if (!params->enable_dynamic_fifo)
462 dwc2_calculate_dynamic_fifo(hsotg);
465 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
466 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
467 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
468 grxfsiz |= params->host_rx_fifo_size <<
469 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
470 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
471 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
472 dwc2_readl(hsotg->regs + GRXFSIZ));
474 /* Non-periodic Tx FIFO */
475 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
476 dwc2_readl(hsotg->regs + GNPTXFSIZ));
477 nptxfsiz = params->host_nperio_tx_fifo_size <<
478 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
479 nptxfsiz |= params->host_rx_fifo_size <<
480 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
481 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
482 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
483 dwc2_readl(hsotg->regs + GNPTXFSIZ));
485 /* Periodic Tx FIFO */
486 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
487 dwc2_readl(hsotg->regs + HPTXFSIZ));
488 hptxfsiz = params->host_perio_tx_fifo_size <<
489 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
490 hptxfsiz |= (params->host_rx_fifo_size +
491 params->host_nperio_tx_fifo_size) <<
492 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
493 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
494 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
495 dwc2_readl(hsotg->regs + HPTXFSIZ));
497 if (hsotg->params.en_multiple_tx_fifo &&
498 hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_91a) {
500 * This feature was implemented in 2.91a version
501 * Global DFIFOCFG calculation for Host mode -
502 * include RxFIFO, NPTXFIFO and HPTXFIFO
504 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
505 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
506 dfifocfg |= (params->host_rx_fifo_size +
507 params->host_nperio_tx_fifo_size +
508 params->host_perio_tx_fifo_size) <<
509 GDFIFOCFG_EPINFOBASE_SHIFT &
510 GDFIFOCFG_EPINFOBASE_MASK;
511 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
516 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
517 * the HFIR register according to PHY type and speed
519 * @hsotg: Programming view of DWC_otg controller
521 * NOTE: The caller can modify the value of the HFIR register only after the
522 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
525 u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
529 int clock = 60; /* default value */
531 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
532 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
534 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
535 !(usbcfg & GUSBCFG_PHYIF16))
537 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
538 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
540 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
541 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
543 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
544 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
546 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
547 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
549 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
550 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
552 if ((usbcfg & GUSBCFG_PHYSEL) &&
553 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
556 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
557 /* High speed case */
558 return 125 * clock - 1;
561 return 1000 * clock - 1;
565 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
568 * @core_if: Programming view of DWC_otg controller
569 * @dest: Destination buffer for the packet
570 * @bytes: Number of bytes to copy to the destination
572 void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
574 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
575 u32 *data_buf = (u32 *)dest;
576 int word_count = (bytes + 3) / 4;
580 * Todo: Account for the case where dest is not dword aligned. This
581 * requires reading data from the FIFO into a u32 temp buffer, then
582 * moving it into the data buffer.
585 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
587 for (i = 0; i < word_count; i++, data_buf++)
588 *data_buf = dwc2_readl(fifo);
592 * dwc2_dump_channel_info() - Prints the state of a host channel
594 * @hsotg: Programming view of DWC_otg controller
595 * @chan: Pointer to the channel to dump
597 * Must be called with interrupt disabled and spinlock held
599 * NOTE: This function will be removed once the peripheral controller code
600 * is integrated and the driver is stable
602 static void dwc2_dump_channel_info(struct dwc2_hsotg *hsotg,
603 struct dwc2_host_chan *chan)
606 int num_channels = hsotg->params.host_channels;
617 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
618 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
619 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(chan->hc_num));
620 hc_dma = dwc2_readl(hsotg->regs + HCDMA(chan->hc_num));
622 dev_dbg(hsotg->dev, " Assigned to channel %p:\n", chan);
623 dev_dbg(hsotg->dev, " hcchar 0x%08x, hcsplt 0x%08x\n",
625 dev_dbg(hsotg->dev, " hctsiz 0x%08x, hc_dma 0x%08x\n",
627 dev_dbg(hsotg->dev, " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
628 chan->dev_addr, chan->ep_num, chan->ep_is_in);
629 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
630 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
631 dev_dbg(hsotg->dev, " data_pid_start: %d\n", chan->data_pid_start);
632 dev_dbg(hsotg->dev, " xfer_started: %d\n", chan->xfer_started);
633 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
634 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
635 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
636 (unsigned long)chan->xfer_dma);
637 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
638 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
639 dev_dbg(hsotg->dev, " NP inactive sched:\n");
640 list_for_each_entry(qh, &hsotg->non_periodic_sched_inactive,
642 dev_dbg(hsotg->dev, " %p\n", qh);
643 dev_dbg(hsotg->dev, " NP active sched:\n");
644 list_for_each_entry(qh, &hsotg->non_periodic_sched_active,
646 dev_dbg(hsotg->dev, " %p\n", qh);
647 dev_dbg(hsotg->dev, " Channels:\n");
648 for (i = 0; i < num_channels; i++) {
649 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
651 dev_dbg(hsotg->dev, " %2d: %p\n", i, chan);
653 #endif /* VERBOSE_DEBUG */
656 static int _dwc2_hcd_start(struct usb_hcd *hcd);
658 static void dwc2_host_start(struct dwc2_hsotg *hsotg)
660 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
662 hcd->self.is_b_host = dwc2_hcd_is_b_host(hsotg);
663 _dwc2_hcd_start(hcd);
666 static void dwc2_host_disconnect(struct dwc2_hsotg *hsotg)
668 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
670 hcd->self.is_b_host = 0;
673 static void dwc2_host_hub_info(struct dwc2_hsotg *hsotg, void *context,
674 int *hub_addr, int *hub_port)
676 struct urb *urb = context;
679 *hub_addr = urb->dev->tt->hub->devnum;
682 *hub_port = urb->dev->ttport;
686 * =========================================================================
687 * Low Level Host Channel Access Functions
688 * =========================================================================
691 static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
692 struct dwc2_host_chan *chan)
694 u32 hcintmsk = HCINTMSK_CHHLTD;
696 switch (chan->ep_type) {
697 case USB_ENDPOINT_XFER_CONTROL:
698 case USB_ENDPOINT_XFER_BULK:
699 dev_vdbg(hsotg->dev, "control/bulk\n");
700 hcintmsk |= HCINTMSK_XFERCOMPL;
701 hcintmsk |= HCINTMSK_STALL;
702 hcintmsk |= HCINTMSK_XACTERR;
703 hcintmsk |= HCINTMSK_DATATGLERR;
704 if (chan->ep_is_in) {
705 hcintmsk |= HCINTMSK_BBLERR;
707 hcintmsk |= HCINTMSK_NAK;
708 hcintmsk |= HCINTMSK_NYET;
710 hcintmsk |= HCINTMSK_ACK;
713 if (chan->do_split) {
714 hcintmsk |= HCINTMSK_NAK;
715 if (chan->complete_split)
716 hcintmsk |= HCINTMSK_NYET;
718 hcintmsk |= HCINTMSK_ACK;
721 if (chan->error_state)
722 hcintmsk |= HCINTMSK_ACK;
725 case USB_ENDPOINT_XFER_INT:
727 dev_vdbg(hsotg->dev, "intr\n");
728 hcintmsk |= HCINTMSK_XFERCOMPL;
729 hcintmsk |= HCINTMSK_NAK;
730 hcintmsk |= HCINTMSK_STALL;
731 hcintmsk |= HCINTMSK_XACTERR;
732 hcintmsk |= HCINTMSK_DATATGLERR;
733 hcintmsk |= HCINTMSK_FRMOVRUN;
736 hcintmsk |= HCINTMSK_BBLERR;
737 if (chan->error_state)
738 hcintmsk |= HCINTMSK_ACK;
739 if (chan->do_split) {
740 if (chan->complete_split)
741 hcintmsk |= HCINTMSK_NYET;
743 hcintmsk |= HCINTMSK_ACK;
747 case USB_ENDPOINT_XFER_ISOC:
749 dev_vdbg(hsotg->dev, "isoc\n");
750 hcintmsk |= HCINTMSK_XFERCOMPL;
751 hcintmsk |= HCINTMSK_FRMOVRUN;
752 hcintmsk |= HCINTMSK_ACK;
754 if (chan->ep_is_in) {
755 hcintmsk |= HCINTMSK_XACTERR;
756 hcintmsk |= HCINTMSK_BBLERR;
760 dev_err(hsotg->dev, "## Unknown EP type ##\n");
764 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
766 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
769 static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
770 struct dwc2_host_chan *chan)
772 u32 hcintmsk = HCINTMSK_CHHLTD;
775 * For Descriptor DMA mode core halts the channel on AHB error.
776 * Interrupt is not required.
778 if (!hsotg->params.dma_desc_enable) {
780 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
781 hcintmsk |= HCINTMSK_AHBERR;
784 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
785 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
786 hcintmsk |= HCINTMSK_XFERCOMPL;
789 if (chan->error_state && !chan->do_split &&
790 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
792 dev_vdbg(hsotg->dev, "setting ACK\n");
793 hcintmsk |= HCINTMSK_ACK;
794 if (chan->ep_is_in) {
795 hcintmsk |= HCINTMSK_DATATGLERR;
796 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
797 hcintmsk |= HCINTMSK_NAK;
801 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
803 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
806 static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
807 struct dwc2_host_chan *chan)
811 if (hsotg->params.host_dma) {
813 dev_vdbg(hsotg->dev, "DMA enabled\n");
814 dwc2_hc_enable_dma_ints(hsotg, chan);
817 dev_vdbg(hsotg->dev, "DMA disabled\n");
818 dwc2_hc_enable_slave_ints(hsotg, chan);
821 /* Enable the top level host channel interrupt */
822 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
823 intmsk |= 1 << chan->hc_num;
824 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
826 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
828 /* Make sure host channel interrupts are enabled */
829 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
830 intmsk |= GINTSTS_HCHINT;
831 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
833 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
837 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
838 * a specific endpoint
840 * @hsotg: Programming view of DWC_otg controller
841 * @chan: Information needed to initialize the host channel
843 * The HCCHARn register is set up with the characteristics specified in chan.
844 * Host channel interrupts that may need to be serviced while this transfer is
845 * in progress are enabled.
847 static void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
849 u8 hc_num = chan->hc_num;
855 dev_vdbg(hsotg->dev, "%s()\n", __func__);
857 /* Clear old interrupt conditions for this host channel */
858 hcintmsk = 0xffffffff;
859 hcintmsk &= ~HCINTMSK_RESERVED14_31;
860 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
862 /* Enable channel interrupts required for this transfer */
863 dwc2_hc_enable_ints(hsotg, chan);
866 * Program the HCCHARn register with the endpoint characteristics for
867 * the current transfer
869 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
870 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
872 hcchar |= HCCHAR_EPDIR;
873 if (chan->speed == USB_SPEED_LOW)
874 hcchar |= HCCHAR_LSPDDEV;
875 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
876 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
877 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
879 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
882 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
884 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
886 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
888 dev_vdbg(hsotg->dev, " Is In: %d\n",
890 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
891 chan->speed == USB_SPEED_LOW);
892 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
894 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
898 /* Program the HCSPLT register for SPLITs */
899 if (chan->do_split) {
902 "Programming HC %d with split --> %s\n",
904 chan->complete_split ? "CSPLIT" : "SSPLIT");
905 if (chan->complete_split)
906 hcsplt |= HCSPLT_COMPSPLT;
907 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
909 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
911 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
914 dev_vdbg(hsotg->dev, " comp split %d\n",
915 chan->complete_split);
916 dev_vdbg(hsotg->dev, " xact pos %d\n",
918 dev_vdbg(hsotg->dev, " hub addr %d\n",
920 dev_vdbg(hsotg->dev, " hub port %d\n",
922 dev_vdbg(hsotg->dev, " is_in %d\n",
924 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
926 dev_vdbg(hsotg->dev, " xferlen %d\n",
931 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
935 * dwc2_hc_halt() - Attempts to halt a host channel
937 * @hsotg: Controller register interface
938 * @chan: Host channel to halt
939 * @halt_status: Reason for halting the channel
941 * This function should only be called in Slave mode or to abort a transfer in
942 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
943 * controller halts the channel when the transfer is complete or a condition
944 * occurs that requires application intervention.
946 * In slave mode, checks for a free request queue entry, then sets the Channel
947 * Enable and Channel Disable bits of the Host Channel Characteristics
948 * register of the specified channel to intiate the halt. If there is no free
949 * request queue entry, sets only the Channel Disable bit of the HCCHARn
950 * register to flush requests for this channel. In the latter case, sets a
951 * flag to indicate that the host channel needs to be halted when a request
952 * queue slot is open.
954 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
955 * HCCHARn register. The controller ensures there is space in the request
956 * queue before submitting the halt request.
958 * Some time may elapse before the core flushes any posted requests for this
959 * host channel and halts. The Channel Halted interrupt handler completes the
960 * deactivation of the host channel.
962 void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
963 enum dwc2_halt_status halt_status)
965 u32 nptxsts, hptxsts, hcchar;
968 dev_vdbg(hsotg->dev, "%s()\n", __func__);
969 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
970 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
972 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
973 halt_status == DWC2_HC_XFER_AHB_ERR) {
975 * Disable all channel interrupts except Ch Halted. The QTD
976 * and QH state associated with this transfer has been cleared
977 * (in the case of URB_DEQUEUE), so the channel needs to be
978 * shut down carefully to prevent crashes.
980 u32 hcintmsk = HCINTMSK_CHHLTD;
982 dev_vdbg(hsotg->dev, "dequeue/error\n");
983 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
986 * Make sure no other interrupts besides halt are currently
987 * pending. Handling another interrupt could cause a crash due
988 * to the QTD and QH state.
990 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
993 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
994 * even if the channel was already halted for some other
997 chan->halt_status = halt_status;
999 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1000 if (!(hcchar & HCCHAR_CHENA)) {
1002 * The channel is either already halted or it hasn't
1003 * started yet. In DMA mode, the transfer may halt if
1004 * it finishes normally or a condition occurs that
1005 * requires driver intervention. Don't want to halt
1006 * the channel again. In either Slave or DMA mode,
1007 * it's possible that the transfer has been assigned
1008 * to a channel, but not started yet when an URB is
1009 * dequeued. Don't want to halt a channel that hasn't
1015 if (chan->halt_pending) {
1017 * A halt has already been issued for this channel. This might
1018 * happen when a transfer is aborted by a higher level in
1021 dev_vdbg(hsotg->dev,
1022 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1023 __func__, chan->hc_num);
1027 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1029 /* No need to set the bit in DDMA for disabling the channel */
1030 /* TODO check it everywhere channel is disabled */
1031 if (!hsotg->params.dma_desc_enable) {
1033 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1034 hcchar |= HCCHAR_CHENA;
1037 dev_dbg(hsotg->dev, "desc DMA enabled\n");
1039 hcchar |= HCCHAR_CHDIS;
1041 if (!hsotg->params.host_dma) {
1043 dev_vdbg(hsotg->dev, "DMA not enabled\n");
1044 hcchar |= HCCHAR_CHENA;
1046 /* Check for space in the request queue to issue the halt */
1047 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1048 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1049 dev_vdbg(hsotg->dev, "control/bulk\n");
1050 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1051 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1052 dev_vdbg(hsotg->dev, "Disabling channel\n");
1053 hcchar &= ~HCCHAR_CHENA;
1057 dev_vdbg(hsotg->dev, "isoc/intr\n");
1058 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1059 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1060 hsotg->queuing_high_bandwidth) {
1062 dev_vdbg(hsotg->dev, "Disabling channel\n");
1063 hcchar &= ~HCCHAR_CHENA;
1068 dev_vdbg(hsotg->dev, "DMA enabled\n");
1071 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1072 chan->halt_status = halt_status;
1074 if (hcchar & HCCHAR_CHENA) {
1076 dev_vdbg(hsotg->dev, "Channel enabled\n");
1077 chan->halt_pending = 1;
1078 chan->halt_on_queue = 0;
1081 dev_vdbg(hsotg->dev, "Channel disabled\n");
1082 chan->halt_on_queue = 1;
1086 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1088 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1090 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1091 chan->halt_pending);
1092 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1093 chan->halt_on_queue);
1094 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1100 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1102 * @hsotg: Programming view of DWC_otg controller
1103 * @chan: Identifies the host channel to clean up
1105 * This function is normally called after a transfer is done and the host
1106 * channel is being released
1108 void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1112 chan->xfer_started = 0;
1114 list_del_init(&chan->split_order_list_entry);
1117 * Clear channel interrupt enables and any unhandled channel interrupt
1120 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1121 hcintmsk = 0xffffffff;
1122 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1123 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1127 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1128 * which frame a periodic transfer should occur
1130 * @hsotg: Programming view of DWC_otg controller
1131 * @chan: Identifies the host channel to set up and its properties
1132 * @hcchar: Current value of the HCCHAR register for the specified host channel
1134 * This function has no effect on non-periodic transfers
1136 static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1137 struct dwc2_host_chan *chan, u32 *hcchar)
1139 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1140 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1150 * Try to figure out if we're an even or odd frame. If we set
1151 * even and the current frame number is even the the transfer
1152 * will happen immediately. Similar if both are odd. If one is
1153 * even and the other is odd then the transfer will happen when
1154 * the frame number ticks.
1156 * There's a bit of a balancing act to get this right.
1157 * Sometimes we may want to send data in the current frame (AK
1158 * right away). We might want to do this if the frame number
1159 * _just_ ticked, but we might also want to do this in order
1160 * to continue a split transaction that happened late in a
1161 * microframe (so we didn't know to queue the next transfer
1162 * until the frame number had ticked). The problem is that we
1163 * need a lot of knowledge to know if there's actually still
1164 * time to send things or if it would be better to wait until
1167 * We can look at how much time is left in the current frame
1168 * and make a guess about whether we'll have time to transfer.
1172 /* Get speed host is running at */
1173 host_speed = (chan->speed != USB_SPEED_HIGH &&
1174 !chan->do_split) ? chan->speed : USB_SPEED_HIGH;
1176 /* See how many bytes are in the periodic FIFO right now */
1177 fifo_space = (dwc2_readl(hsotg->regs + HPTXSTS) &
1178 TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT;
1179 bytes_in_fifo = sizeof(u32) *
1180 (hsotg->params.host_perio_tx_fifo_size -
1184 * Roughly estimate bus time for everything in the periodic
1185 * queue + our new transfer. This is "rough" because we're
1186 * using a function that makes takes into account IN/OUT
1187 * and INT/ISO and we're just slamming in one value for all
1188 * transfers. This should be an over-estimate and that should
1189 * be OK, but we can probably tighten it.
1191 xfer_ns = usb_calc_bus_time(host_speed, false, false,
1192 chan->xfer_len + bytes_in_fifo);
1193 xfer_us = NS_TO_US(xfer_ns);
1195 /* See what frame number we'll be at by the time we finish */
1196 frame_number = dwc2_hcd_get_future_frame_number(hsotg, xfer_us);
1198 /* This is when we were scheduled to be on the wire */
1199 wire_frame = dwc2_frame_num_inc(chan->qh->next_active_frame, 1);
1202 * If we'd finish _after_ the frame we're scheduled in then
1203 * it's hopeless. Just schedule right away and hope for the
1204 * best. Note that it _might_ be wise to call back into the
1205 * scheduler to pick a better frame, but this is better than
1208 if (dwc2_frame_num_gt(frame_number, wire_frame)) {
1209 dwc2_sch_vdbg(hsotg,
1210 "QH=%p EO MISS fr=%04x=>%04x (%+d)\n",
1211 chan->qh, wire_frame, frame_number,
1212 dwc2_frame_num_dec(frame_number,
1214 wire_frame = frame_number;
1217 * We picked a different frame number; communicate this
1218 * back to the scheduler so it doesn't try to schedule
1219 * another in the same frame.
1221 * Remember that next_active_frame is 1 before the wire
1224 chan->qh->next_active_frame =
1225 dwc2_frame_num_dec(frame_number, 1);
1229 *hcchar |= HCCHAR_ODDFRM;
1231 *hcchar &= ~HCCHAR_ODDFRM;
1235 static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1237 /* Set up the initial PID for the transfer */
1238 if (chan->speed == USB_SPEED_HIGH) {
1239 if (chan->ep_is_in) {
1240 if (chan->multi_count == 1)
1241 chan->data_pid_start = DWC2_HC_PID_DATA0;
1242 else if (chan->multi_count == 2)
1243 chan->data_pid_start = DWC2_HC_PID_DATA1;
1245 chan->data_pid_start = DWC2_HC_PID_DATA2;
1247 if (chan->multi_count == 1)
1248 chan->data_pid_start = DWC2_HC_PID_DATA0;
1250 chan->data_pid_start = DWC2_HC_PID_MDATA;
1253 chan->data_pid_start = DWC2_HC_PID_DATA0;
1258 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1261 * @hsotg: Programming view of DWC_otg controller
1262 * @chan: Information needed to initialize the host channel
1264 * This function should only be called in Slave mode. For a channel associated
1265 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1266 * associated with a periodic EP, the periodic Tx FIFO is written.
1268 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1269 * the number of bytes written to the Tx FIFO.
1271 static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1272 struct dwc2_host_chan *chan)
1275 u32 remaining_count;
1278 u32 __iomem *data_fifo;
1279 u32 *data_buf = (u32 *)chan->xfer_buf;
1282 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1284 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1286 remaining_count = chan->xfer_len - chan->xfer_count;
1287 if (remaining_count > chan->max_packet)
1288 byte_count = chan->max_packet;
1290 byte_count = remaining_count;
1292 dword_count = (byte_count + 3) / 4;
1294 if (((unsigned long)data_buf & 0x3) == 0) {
1295 /* xfer_buf is DWORD aligned */
1296 for (i = 0; i < dword_count; i++, data_buf++)
1297 dwc2_writel(*data_buf, data_fifo);
1299 /* xfer_buf is not DWORD aligned */
1300 for (i = 0; i < dword_count; i++, data_buf++) {
1301 u32 data = data_buf[0] | data_buf[1] << 8 |
1302 data_buf[2] << 16 | data_buf[3] << 24;
1303 dwc2_writel(data, data_fifo);
1307 chan->xfer_count += byte_count;
1308 chan->xfer_buf += byte_count;
1312 * dwc2_hc_do_ping() - Starts a PING transfer
1314 * @hsotg: Programming view of DWC_otg controller
1315 * @chan: Information needed to initialize the host channel
1317 * This function should only be called in Slave mode. The Do Ping bit is set in
1318 * the HCTSIZ register, then the channel is enabled.
1320 static void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg,
1321 struct dwc2_host_chan *chan)
1327 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1330 hctsiz = TSIZ_DOPNG;
1331 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1332 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1334 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1335 hcchar |= HCCHAR_CHENA;
1336 hcchar &= ~HCCHAR_CHDIS;
1337 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1341 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1342 * channel and starts the transfer
1344 * @hsotg: Programming view of DWC_otg controller
1345 * @chan: Information needed to initialize the host channel. The xfer_len value
1346 * may be reduced to accommodate the max widths of the XferSize and
1347 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1348 * changed to reflect the final xfer_len value.
1350 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1351 * the caller must ensure that there is sufficient space in the request queue
1354 * For an OUT transfer in Slave mode, it loads a data packet into the
1355 * appropriate FIFO. If necessary, additional data packets are loaded in the
1358 * For an IN transfer in Slave mode, a data packet is requested. The data
1359 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1360 * additional data packets are requested in the Host ISR.
1362 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1363 * register along with a packet count of 1 and the channel is enabled. This
1364 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1365 * simply set to 0 since no data transfer occurs in this case.
1367 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1368 * all the information required to perform the subsequent data transfer. In
1369 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1370 * controller performs the entire PING protocol, then starts the data
1373 static void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1374 struct dwc2_host_chan *chan)
1376 u32 max_hc_xfer_size = hsotg->params.max_transfer_size;
1377 u16 max_hc_pkt_count = hsotg->params.max_packet_count;
1384 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1386 if (chan->do_ping) {
1387 if (!hsotg->params.host_dma) {
1389 dev_vdbg(hsotg->dev, "ping, no DMA\n");
1390 dwc2_hc_do_ping(hsotg, chan);
1391 chan->xfer_started = 1;
1396 dev_vdbg(hsotg->dev, "ping, DMA\n");
1398 hctsiz |= TSIZ_DOPNG;
1401 if (chan->do_split) {
1403 dev_vdbg(hsotg->dev, "split\n");
1406 if (chan->complete_split && !chan->ep_is_in)
1408 * For CSPLIT OUT Transfer, set the size to 0 so the
1409 * core doesn't expect any data written to the FIFO
1412 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1413 chan->xfer_len = chan->max_packet;
1414 else if (!chan->ep_is_in && chan->xfer_len > 188)
1415 chan->xfer_len = 188;
1417 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1420 /* For split set ec_mc for immediate retries */
1421 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1422 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1428 dev_vdbg(hsotg->dev, "no split\n");
1430 * Ensure that the transfer length and packet count will fit
1431 * in the widths allocated for them in the HCTSIZn register
1433 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1434 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1436 * Make sure the transfer size is no larger than one
1437 * (micro)frame's worth of data. (A check was done
1438 * when the periodic transfer was accepted to ensure
1439 * that a (micro)frame's worth of data can be
1440 * programmed into a channel.)
1442 u32 max_periodic_len =
1443 chan->multi_count * chan->max_packet;
1445 if (chan->xfer_len > max_periodic_len)
1446 chan->xfer_len = max_periodic_len;
1447 } else if (chan->xfer_len > max_hc_xfer_size) {
1449 * Make sure that xfer_len is a multiple of max packet
1453 max_hc_xfer_size - chan->max_packet + 1;
1456 if (chan->xfer_len > 0) {
1457 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1459 if (num_packets > max_hc_pkt_count) {
1460 num_packets = max_hc_pkt_count;
1461 chan->xfer_len = num_packets * chan->max_packet;
1464 /* Need 1 packet for transfer length of 0 */
1470 * Always program an integral # of max packets for IN
1473 chan->xfer_len = num_packets * chan->max_packet;
1475 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1476 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1478 * Make sure that the multi_count field matches the
1479 * actual transfer length
1481 chan->multi_count = num_packets;
1483 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1484 dwc2_set_pid_isoc(chan);
1486 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1489 /* The ec_mc gets the multi_count for non-split */
1490 ec_mc = chan->multi_count;
1493 chan->start_pkt_count = num_packets;
1494 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1495 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1496 TSIZ_SC_MC_PID_MASK;
1497 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1499 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1500 hctsiz, chan->hc_num);
1502 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1504 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1505 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1506 TSIZ_XFERSIZE_SHIFT);
1507 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1508 (hctsiz & TSIZ_PKTCNT_MASK) >>
1510 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1511 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1512 TSIZ_SC_MC_PID_SHIFT);
1515 if (hsotg->params.host_dma) {
1516 dwc2_writel((u32)chan->xfer_dma,
1517 hsotg->regs + HCDMA(chan->hc_num));
1519 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1520 (unsigned long)chan->xfer_dma, chan->hc_num);
1523 /* Start the split */
1524 if (chan->do_split) {
1525 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1527 hcsplt |= HCSPLT_SPLTENA;
1528 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1531 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1532 hcchar &= ~HCCHAR_MULTICNT_MASK;
1533 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1534 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1536 if (hcchar & HCCHAR_CHDIS)
1537 dev_warn(hsotg->dev,
1538 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1539 __func__, chan->hc_num, hcchar);
1541 /* Set host channel enable after all other setup is complete */
1542 hcchar |= HCCHAR_CHENA;
1543 hcchar &= ~HCCHAR_CHDIS;
1546 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1547 (hcchar & HCCHAR_MULTICNT_MASK) >>
1548 HCCHAR_MULTICNT_SHIFT);
1550 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1552 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1555 chan->xfer_started = 1;
1558 if (!hsotg->params.host_dma &&
1559 !chan->ep_is_in && chan->xfer_len > 0)
1560 /* Load OUT packet into the appropriate Tx FIFO */
1561 dwc2_hc_write_packet(hsotg, chan);
1565 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1566 * host channel and starts the transfer in Descriptor DMA mode
1568 * @hsotg: Programming view of DWC_otg controller
1569 * @chan: Information needed to initialize the host channel
1571 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1572 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1573 * with micro-frame bitmap.
1575 * Initializes HCDMA register with descriptor list address and CTD value then
1576 * starts the transfer via enabling the channel.
1578 void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1579 struct dwc2_host_chan *chan)
1585 hctsiz |= TSIZ_DOPNG;
1587 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1588 dwc2_set_pid_isoc(chan);
1590 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1591 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1592 TSIZ_SC_MC_PID_MASK;
1594 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1595 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1597 /* Non-zero only for high-speed interrupt endpoints */
1598 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1601 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1603 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1604 chan->data_pid_start);
1605 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1608 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1610 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1611 chan->desc_list_sz, DMA_TO_DEVICE);
1613 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
1616 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1617 &chan->desc_list_addr, chan->hc_num);
1619 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1620 hcchar &= ~HCCHAR_MULTICNT_MASK;
1621 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1622 HCCHAR_MULTICNT_MASK;
1624 if (hcchar & HCCHAR_CHDIS)
1625 dev_warn(hsotg->dev,
1626 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1627 __func__, chan->hc_num, hcchar);
1629 /* Set host channel enable after all other setup is complete */
1630 hcchar |= HCCHAR_CHENA;
1631 hcchar &= ~HCCHAR_CHDIS;
1634 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1635 (hcchar & HCCHAR_MULTICNT_MASK) >>
1636 HCCHAR_MULTICNT_SHIFT);
1638 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1640 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1643 chan->xfer_started = 1;
1648 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1649 * a previous call to dwc2_hc_start_transfer()
1651 * @hsotg: Programming view of DWC_otg controller
1652 * @chan: Information needed to initialize the host channel
1654 * The caller must ensure there is sufficient space in the request queue and Tx
1655 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1656 * the controller acts autonomously to complete transfers programmed to a host
1659 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1660 * if there is any data remaining to be queued. For an IN transfer, another
1661 * data packet is always requested. For the SETUP phase of a control transfer,
1662 * this function does nothing.
1664 * Return: 1 if a new request is queued, 0 if no more requests are required
1667 static int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1668 struct dwc2_host_chan *chan)
1671 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1675 /* SPLITs always queue just once per channel */
1678 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1679 /* SETUPs are queued only once since they can't be NAK'd */
1682 if (chan->ep_is_in) {
1684 * Always queue another request for other IN transfers. If
1685 * back-to-back INs are issued and NAKs are received for both,
1686 * the driver may still be processing the first NAK when the
1687 * second NAK is received. When the interrupt handler clears
1688 * the NAK interrupt for the first NAK, the second NAK will
1689 * not be seen. So we can't depend on the NAK interrupt
1690 * handler to requeue a NAK'd request. Instead, IN requests
1691 * are issued each time this function is called. When the
1692 * transfer completes, the extra requests for the channel will
1695 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1697 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1698 hcchar |= HCCHAR_CHENA;
1699 hcchar &= ~HCCHAR_CHDIS;
1701 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
1703 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1710 if (chan->xfer_count < chan->xfer_len) {
1711 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1712 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1713 u32 hcchar = dwc2_readl(hsotg->regs +
1714 HCCHAR(chan->hc_num));
1716 dwc2_hc_set_even_odd_frame(hsotg, chan,
1720 /* Load OUT packet into the appropriate Tx FIFO */
1721 dwc2_hc_write_packet(hsotg, chan);
1730 * =========================================================================
1732 * =========================================================================
1736 * Processes all the URBs in a single list of QHs. Completes them with
1737 * -ETIMEDOUT and frees the QTD.
1739 * Must be called with interrupt disabled and spinlock held
1741 static void dwc2_kill_urbs_in_qh_list(struct dwc2_hsotg *hsotg,
1742 struct list_head *qh_list)
1744 struct dwc2_qh *qh, *qh_tmp;
1745 struct dwc2_qtd *qtd, *qtd_tmp;
1747 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1748 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1750 dwc2_host_complete(hsotg, qtd, -ECONNRESET);
1751 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1756 static void dwc2_qh_list_free(struct dwc2_hsotg *hsotg,
1757 struct list_head *qh_list)
1759 struct dwc2_qtd *qtd, *qtd_tmp;
1760 struct dwc2_qh *qh, *qh_tmp;
1761 unsigned long flags;
1764 /* The list hasn't been initialized yet */
1767 spin_lock_irqsave(&hsotg->lock, flags);
1769 /* Ensure there are no QTDs or URBs left */
1770 dwc2_kill_urbs_in_qh_list(hsotg, qh_list);
1772 list_for_each_entry_safe(qh, qh_tmp, qh_list, qh_list_entry) {
1773 dwc2_hcd_qh_unlink(hsotg, qh);
1775 /* Free each QTD in the QH's QTD list */
1776 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list,
1778 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
1780 if (qh->channel && qh->channel->qh == qh)
1781 qh->channel->qh = NULL;
1783 spin_unlock_irqrestore(&hsotg->lock, flags);
1784 dwc2_hcd_qh_free(hsotg, qh);
1785 spin_lock_irqsave(&hsotg->lock, flags);
1788 spin_unlock_irqrestore(&hsotg->lock, flags);
1792 * Responds with an error status of -ETIMEDOUT to all URBs in the non-periodic
1793 * and periodic schedules. The QTD associated with each URB is removed from
1794 * the schedule and freed. This function may be called when a disconnect is
1795 * detected or when the HCD is being stopped.
1797 * Must be called with interrupt disabled and spinlock held
1799 static void dwc2_kill_all_urbs(struct dwc2_hsotg *hsotg)
1801 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_inactive);
1802 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->non_periodic_sched_active);
1803 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_inactive);
1804 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_ready);
1805 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_assigned);
1806 dwc2_kill_urbs_in_qh_list(hsotg, &hsotg->periodic_sched_queued);
1810 * dwc2_hcd_start() - Starts the HCD when switching to Host mode
1812 * @hsotg: Pointer to struct dwc2_hsotg
1814 void dwc2_hcd_start(struct dwc2_hsotg *hsotg)
1818 if (hsotg->op_state == OTG_STATE_B_HOST) {
1820 * Reset the port. During a HNP mode switch the reset
1821 * needs to occur within 1ms and have a duration of at
1824 hprt0 = dwc2_read_hprt0(hsotg);
1826 dwc2_writel(hprt0, hsotg->regs + HPRT0);
1829 queue_delayed_work(hsotg->wq_otg, &hsotg->start_work,
1830 msecs_to_jiffies(50));
1833 /* Must be called with interrupt disabled and spinlock held */
1834 static void dwc2_hcd_cleanup_channels(struct dwc2_hsotg *hsotg)
1836 int num_channels = hsotg->params.host_channels;
1837 struct dwc2_host_chan *channel;
1841 if (!hsotg->params.host_dma) {
1842 /* Flush out any channel requests in slave mode */
1843 for (i = 0; i < num_channels; i++) {
1844 channel = hsotg->hc_ptr_array[i];
1845 if (!list_empty(&channel->hc_list_entry))
1847 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1848 if (hcchar & HCCHAR_CHENA) {
1849 hcchar &= ~(HCCHAR_CHENA | HCCHAR_EPDIR);
1850 hcchar |= HCCHAR_CHDIS;
1851 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
1856 for (i = 0; i < num_channels; i++) {
1857 channel = hsotg->hc_ptr_array[i];
1858 if (!list_empty(&channel->hc_list_entry))
1860 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1861 if (hcchar & HCCHAR_CHENA) {
1862 /* Halt the channel */
1863 hcchar |= HCCHAR_CHDIS;
1864 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
1867 dwc2_hc_cleanup(hsotg, channel);
1868 list_add_tail(&channel->hc_list_entry, &hsotg->free_hc_list);
1870 * Added for Descriptor DMA to prevent channel double cleanup in
1871 * release_channel_ddma(), which is called from ep_disable when
1872 * device disconnects
1876 /* All channels have been freed, mark them available */
1877 if (hsotg->params.uframe_sched) {
1878 hsotg->available_host_channels =
1879 hsotg->params.host_channels;
1881 hsotg->non_periodic_channels = 0;
1882 hsotg->periodic_channels = 0;
1887 * dwc2_hcd_connect() - Handles connect of the HCD
1889 * @hsotg: Pointer to struct dwc2_hsotg
1891 * Must be called with interrupt disabled and spinlock held
1893 void dwc2_hcd_connect(struct dwc2_hsotg *hsotg)
1895 if (hsotg->lx_state != DWC2_L0)
1896 usb_hcd_resume_root_hub(hsotg->priv);
1898 hsotg->flags.b.port_connect_status_change = 1;
1899 hsotg->flags.b.port_connect_status = 1;
1903 * dwc2_hcd_disconnect() - Handles disconnect of the HCD
1905 * @hsotg: Pointer to struct dwc2_hsotg
1906 * @force: If true, we won't try to reconnect even if we see device connected.
1908 * Must be called with interrupt disabled and spinlock held
1910 void dwc2_hcd_disconnect(struct dwc2_hsotg *hsotg, bool force)
1915 /* Set status flags for the hub driver */
1916 hsotg->flags.b.port_connect_status_change = 1;
1917 hsotg->flags.b.port_connect_status = 0;
1920 * Shutdown any transfers in process by clearing the Tx FIFO Empty
1921 * interrupt mask and status bits and disabling subsequent host
1922 * channel interrupts.
1924 intr = dwc2_readl(hsotg->regs + GINTMSK);
1925 intr &= ~(GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT);
1926 dwc2_writel(intr, hsotg->regs + GINTMSK);
1927 intr = GINTSTS_NPTXFEMP | GINTSTS_PTXFEMP | GINTSTS_HCHINT;
1928 dwc2_writel(intr, hsotg->regs + GINTSTS);
1931 * Turn off the vbus power only if the core has transitioned to device
1932 * mode. If still in host mode, need to keep power on to detect a
1935 if (dwc2_is_device_mode(hsotg)) {
1936 if (hsotg->op_state != OTG_STATE_A_SUSPEND) {
1937 dev_dbg(hsotg->dev, "Disconnect: PortPower off\n");
1938 dwc2_writel(0, hsotg->regs + HPRT0);
1941 dwc2_disable_host_interrupts(hsotg);
1944 /* Respond with an error status to all URBs in the schedule */
1945 dwc2_kill_all_urbs(hsotg);
1947 if (dwc2_is_host_mode(hsotg))
1948 /* Clean up any host channels that were in use */
1949 dwc2_hcd_cleanup_channels(hsotg);
1951 dwc2_host_disconnect(hsotg);
1954 * Add an extra check here to see if we're actually connected but
1955 * we don't have a detection interrupt pending. This can happen if:
1956 * 1. hardware sees connect
1957 * 2. hardware sees disconnect
1958 * 3. hardware sees connect
1959 * 4. dwc2_port_intr() - clears connect interrupt
1960 * 5. dwc2_handle_common_intr() - calls here
1962 * Without the extra check here we will end calling disconnect
1963 * and won't get any future interrupts to handle the connect.
1966 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
1967 if (!(hprt0 & HPRT0_CONNDET) && (hprt0 & HPRT0_CONNSTS))
1968 dwc2_hcd_connect(hsotg);
1973 * dwc2_hcd_rem_wakeup() - Handles Remote Wakeup
1975 * @hsotg: Pointer to struct dwc2_hsotg
1977 static void dwc2_hcd_rem_wakeup(struct dwc2_hsotg *hsotg)
1979 if (hsotg->bus_suspended) {
1980 hsotg->flags.b.port_suspend_change = 1;
1981 usb_hcd_resume_root_hub(hsotg->priv);
1984 if (hsotg->lx_state == DWC2_L1)
1985 hsotg->flags.b.port_l1_change = 1;
1989 * dwc2_hcd_stop() - Halts the DWC_otg host mode operations in a clean manner
1991 * @hsotg: Pointer to struct dwc2_hsotg
1993 * Must be called with interrupt disabled and spinlock held
1995 void dwc2_hcd_stop(struct dwc2_hsotg *hsotg)
1997 dev_dbg(hsotg->dev, "DWC OTG HCD STOP\n");
2000 * The root hub should be disconnected before this function is called.
2001 * The disconnect will clear the QTD lists (via ..._hcd_urb_dequeue)
2002 * and the QH lists (via ..._hcd_endpoint_disable).
2005 /* Turn off all host-specific interrupts */
2006 dwc2_disable_host_interrupts(hsotg);
2008 /* Turn off the vbus power */
2009 dev_dbg(hsotg->dev, "PortPower off\n");
2010 dwc2_writel(0, hsotg->regs + HPRT0);
2013 /* Caller must hold driver lock */
2014 static int dwc2_hcd_urb_enqueue(struct dwc2_hsotg *hsotg,
2015 struct dwc2_hcd_urb *urb, struct dwc2_qh *qh,
2016 struct dwc2_qtd *qtd)
2022 if (!hsotg->flags.b.port_connect_status) {
2023 /* No longer connected */
2024 dev_err(hsotg->dev, "Not connected\n");
2028 dev_speed = dwc2_host_get_speed(hsotg, urb->priv);
2030 /* Some configurations cannot support LS traffic on a FS root port */
2031 if ((dev_speed == USB_SPEED_LOW) &&
2032 (hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED) &&
2033 (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI)) {
2034 u32 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
2035 u32 prtspd = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
2037 if (prtspd == HPRT0_SPD_FULL_SPEED)
2044 dwc2_hcd_qtd_init(qtd, urb);
2045 retval = dwc2_hcd_qtd_add(hsotg, qtd, qh);
2048 "DWC OTG HCD URB Enqueue failed adding QTD. Error status %d\n",
2053 intr_mask = dwc2_readl(hsotg->regs + GINTMSK);
2054 if (!(intr_mask & GINTSTS_SOF)) {
2055 enum dwc2_transaction_type tr_type;
2057 if (qtd->qh->ep_type == USB_ENDPOINT_XFER_BULK &&
2058 !(qtd->urb->flags & URB_GIVEBACK_ASAP))
2060 * Do not schedule SG transactions until qtd has
2061 * URB_GIVEBACK_ASAP set
2065 tr_type = dwc2_hcd_select_transactions(hsotg);
2066 if (tr_type != DWC2_TRANSACTION_NONE)
2067 dwc2_hcd_queue_transactions(hsotg, tr_type);
2073 /* Must be called with interrupt disabled and spinlock held */
2074 static int dwc2_hcd_urb_dequeue(struct dwc2_hsotg *hsotg,
2075 struct dwc2_hcd_urb *urb)
2078 struct dwc2_qtd *urb_qtd;
2082 dev_dbg(hsotg->dev, "## Urb QTD is NULL ##\n");
2088 dev_dbg(hsotg->dev, "## Urb QTD QH is NULL ##\n");
2094 if (urb_qtd->in_process && qh->channel) {
2095 dwc2_dump_channel_info(hsotg, qh->channel);
2097 /* The QTD is in process (it has been assigned to a channel) */
2098 if (hsotg->flags.b.port_connect_status)
2100 * If still connected (i.e. in host mode), halt the
2101 * channel so it can be used for other transfers. If
2102 * no longer connected, the host registers can't be
2103 * written to halt the channel since the core is in
2106 dwc2_hc_halt(hsotg, qh->channel,
2107 DWC2_HC_XFER_URB_DEQUEUE);
2111 * Free the QTD and clean up the associated QH. Leave the QH in the
2112 * schedule if it has any remaining QTDs.
2114 if (!hsotg->params.dma_desc_enable) {
2115 u8 in_process = urb_qtd->in_process;
2117 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2119 dwc2_hcd_qh_deactivate(hsotg, qh, 0);
2121 } else if (list_empty(&qh->qtd_list)) {
2122 dwc2_hcd_qh_unlink(hsotg, qh);
2125 dwc2_hcd_qtd_unlink_and_free(hsotg, urb_qtd, qh);
2131 /* Must NOT be called with interrupt disabled or spinlock held */
2132 static int dwc2_hcd_endpoint_disable(struct dwc2_hsotg *hsotg,
2133 struct usb_host_endpoint *ep, int retry)
2135 struct dwc2_qtd *qtd, *qtd_tmp;
2137 unsigned long flags;
2140 spin_lock_irqsave(&hsotg->lock, flags);
2148 while (!list_empty(&qh->qtd_list) && retry--) {
2151 "## timeout in dwc2_hcd_endpoint_disable() ##\n");
2156 spin_unlock_irqrestore(&hsotg->lock, flags);
2158 spin_lock_irqsave(&hsotg->lock, flags);
2166 dwc2_hcd_qh_unlink(hsotg, qh);
2168 /* Free each QTD in the QH's QTD list */
2169 list_for_each_entry_safe(qtd, qtd_tmp, &qh->qtd_list, qtd_list_entry)
2170 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd, qh);
2174 if (qh->channel && qh->channel->qh == qh)
2175 qh->channel->qh = NULL;
2177 spin_unlock_irqrestore(&hsotg->lock, flags);
2179 dwc2_hcd_qh_free(hsotg, qh);
2185 spin_unlock_irqrestore(&hsotg->lock, flags);
2190 /* Must be called with interrupt disabled and spinlock held */
2191 static int dwc2_hcd_endpoint_reset(struct dwc2_hsotg *hsotg,
2192 struct usb_host_endpoint *ep)
2194 struct dwc2_qh *qh = ep->hcpriv;
2199 qh->data_toggle = DWC2_HC_PID_DATA0;
2205 * dwc2_core_init() - Initializes the DWC_otg controller registers and
2206 * prepares the core for device mode or host mode operation
2208 * @hsotg: Programming view of the DWC_otg controller
2209 * @initial_setup: If true then this is the first init for this instance.
2211 static int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
2216 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2218 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2220 /* Set ULPI External VBUS bit if needed */
2221 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
2222 if (hsotg->params.phy_ulpi_ext_vbus)
2223 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
2225 /* Set external TS Dline pulsing bit if needed */
2226 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
2227 if (hsotg->params.ts_dline)
2228 usbcfg |= GUSBCFG_TERMSELDLPULSE;
2230 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
2233 * Reset the Controller
2235 * We only need to reset the controller if this is a re-init.
2236 * For the first init we know for sure that earlier code reset us (it
2237 * needed to in order to properly detect various parameters).
2239 if (!initial_setup) {
2240 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
2242 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
2249 * This needs to happen in FS mode before any other programming occurs
2251 retval = dwc2_phy_init(hsotg, initial_setup);
2255 /* Program the GAHBCFG Register */
2256 retval = dwc2_gahbcfg_init(hsotg);
2260 /* Program the GUSBCFG register */
2261 dwc2_gusbcfg_init(hsotg);
2263 /* Program the GOTGCTL register */
2264 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2265 otgctl &= ~GOTGCTL_OTGVER;
2266 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2268 /* Clear the SRP success bit for FS-I2c */
2269 hsotg->srp_success = 0;
2271 /* Enable common interrupts */
2272 dwc2_enable_common_interrupts(hsotg);
2275 * Do device or host initialization based on mode during PCD and
2276 * HCD initialization
2278 if (dwc2_is_host_mode(hsotg)) {
2279 dev_dbg(hsotg->dev, "Host Mode\n");
2280 hsotg->op_state = OTG_STATE_A_HOST;
2282 dev_dbg(hsotg->dev, "Device Mode\n");
2283 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
2290 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
2293 * @hsotg: Programming view of DWC_otg controller
2295 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
2296 * request queues. Host channels are reset to ensure that they are ready for
2297 * performing transfers.
2299 static void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
2301 u32 hcfg, hfir, otgctl;
2303 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
2305 /* Restart the Phy Clock */
2306 dwc2_writel(0, hsotg->regs + PCGCTL);
2308 /* Initialize Host Configuration Register */
2309 dwc2_init_fs_ls_pclk_sel(hsotg);
2310 if (hsotg->params.speed == DWC2_SPEED_PARAM_FULL ||
2311 hsotg->params.speed == DWC2_SPEED_PARAM_LOW) {
2312 hcfg = dwc2_readl(hsotg->regs + HCFG);
2313 hcfg |= HCFG_FSLSSUPP;
2314 dwc2_writel(hcfg, hsotg->regs + HCFG);
2318 * This bit allows dynamic reloading of the HFIR register during
2319 * runtime. This bit needs to be programmed during initial configuration
2320 * and its value must not be changed during runtime.
2322 if (hsotg->params.reload_ctl) {
2323 hfir = dwc2_readl(hsotg->regs + HFIR);
2324 hfir |= HFIR_RLDCTRL;
2325 dwc2_writel(hfir, hsotg->regs + HFIR);
2328 if (hsotg->params.dma_desc_enable) {
2329 u32 op_mode = hsotg->hw_params.op_mode;
2331 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
2332 !hsotg->hw_params.dma_desc_enable ||
2333 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
2334 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
2335 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
2337 "Hardware does not support descriptor DMA mode -\n");
2339 "falling back to buffer DMA mode.\n");
2340 hsotg->params.dma_desc_enable = false;
2342 hcfg = dwc2_readl(hsotg->regs + HCFG);
2343 hcfg |= HCFG_DESCDMA;
2344 dwc2_writel(hcfg, hsotg->regs + HCFG);
2348 /* Configure data FIFO sizes */
2349 dwc2_config_fifos(hsotg);
2351 /* TODO - check this */
2352 /* Clear Host Set HNP Enable in the OTG Control Register */
2353 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2354 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2355 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2357 /* Make sure the FIFOs are flushed */
2358 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
2359 dwc2_flush_rx_fifo(hsotg);
2361 /* Clear Host Set HNP Enable in the OTG Control Register */
2362 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
2363 otgctl &= ~GOTGCTL_HSTSETHNPEN;
2364 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
2366 if (!hsotg->params.dma_desc_enable) {
2367 int num_channels, i;
2370 /* Flush out any leftover queued requests */
2371 num_channels = hsotg->params.host_channels;
2372 for (i = 0; i < num_channels; i++) {
2373 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2374 hcchar &= ~HCCHAR_CHENA;
2375 hcchar |= HCCHAR_CHDIS;
2376 hcchar &= ~HCCHAR_EPDIR;
2377 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2380 /* Halt all channels to put them into a known state */
2381 for (i = 0; i < num_channels; i++) {
2384 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2385 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
2386 hcchar &= ~HCCHAR_EPDIR;
2387 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
2388 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
2391 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
2392 if (++count > 1000) {
2394 "Unable to clear enable on channel %d\n",
2399 } while (hcchar & HCCHAR_CHENA);
2403 /* Turn on the vbus power */
2404 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
2405 if (hsotg->op_state == OTG_STATE_A_HOST) {
2406 u32 hprt0 = dwc2_read_hprt0(hsotg);
2408 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
2409 !!(hprt0 & HPRT0_PWR));
2410 if (!(hprt0 & HPRT0_PWR)) {
2412 dwc2_writel(hprt0, hsotg->regs + HPRT0);
2416 dwc2_enable_host_interrupts(hsotg);
2420 * Initializes dynamic portions of the DWC_otg HCD state
2422 * Must be called with interrupt disabled and spinlock held
2424 static void dwc2_hcd_reinit(struct dwc2_hsotg *hsotg)
2426 struct dwc2_host_chan *chan, *chan_tmp;
2430 hsotg->flags.d32 = 0;
2431 hsotg->non_periodic_qh_ptr = &hsotg->non_periodic_sched_active;
2433 if (hsotg->params.uframe_sched) {
2434 hsotg->available_host_channels =
2435 hsotg->params.host_channels;
2437 hsotg->non_periodic_channels = 0;
2438 hsotg->periodic_channels = 0;
2442 * Put all channels in the free channel list and clean up channel
2445 list_for_each_entry_safe(chan, chan_tmp, &hsotg->free_hc_list,
2447 list_del_init(&chan->hc_list_entry);
2449 num_channels = hsotg->params.host_channels;
2450 for (i = 0; i < num_channels; i++) {
2451 chan = hsotg->hc_ptr_array[i];
2452 list_add_tail(&chan->hc_list_entry, &hsotg->free_hc_list);
2453 dwc2_hc_cleanup(hsotg, chan);
2456 /* Initialize the DWC core for host mode operation */
2457 dwc2_core_host_init(hsotg);
2460 static void dwc2_hc_init_split(struct dwc2_hsotg *hsotg,
2461 struct dwc2_host_chan *chan,
2462 struct dwc2_qtd *qtd, struct dwc2_hcd_urb *urb)
2464 int hub_addr, hub_port;
2467 chan->xact_pos = qtd->isoc_split_pos;
2468 chan->complete_split = qtd->complete_split;
2469 dwc2_host_hub_info(hsotg, urb->priv, &hub_addr, &hub_port);
2470 chan->hub_addr = (u8)hub_addr;
2471 chan->hub_port = (u8)hub_port;
2474 static void dwc2_hc_init_xfer(struct dwc2_hsotg *hsotg,
2475 struct dwc2_host_chan *chan,
2476 struct dwc2_qtd *qtd)
2478 struct dwc2_hcd_urb *urb = qtd->urb;
2479 struct dwc2_hcd_iso_packet_desc *frame_desc;
2481 switch (dwc2_hcd_get_pipe_type(&urb->pipe_info)) {
2482 case USB_ENDPOINT_XFER_CONTROL:
2483 chan->ep_type = USB_ENDPOINT_XFER_CONTROL;
2485 switch (qtd->control_phase) {
2486 case DWC2_CONTROL_SETUP:
2487 dev_vdbg(hsotg->dev, " Control setup transaction\n");
2490 chan->data_pid_start = DWC2_HC_PID_SETUP;
2491 if (hsotg->params.host_dma)
2492 chan->xfer_dma = urb->setup_dma;
2494 chan->xfer_buf = urb->setup_packet;
2498 case DWC2_CONTROL_DATA:
2499 dev_vdbg(hsotg->dev, " Control data transaction\n");
2500 chan->data_pid_start = qtd->data_toggle;
2503 case DWC2_CONTROL_STATUS:
2505 * Direction is opposite of data direction or IN if no
2508 dev_vdbg(hsotg->dev, " Control status transaction\n");
2509 if (urb->length == 0)
2513 dwc2_hcd_is_pipe_out(&urb->pipe_info);
2516 chan->data_pid_start = DWC2_HC_PID_DATA1;
2518 if (hsotg->params.host_dma)
2519 chan->xfer_dma = hsotg->status_buf_dma;
2521 chan->xfer_buf = hsotg->status_buf;
2526 case USB_ENDPOINT_XFER_BULK:
2527 chan->ep_type = USB_ENDPOINT_XFER_BULK;
2530 case USB_ENDPOINT_XFER_INT:
2531 chan->ep_type = USB_ENDPOINT_XFER_INT;
2534 case USB_ENDPOINT_XFER_ISOC:
2535 chan->ep_type = USB_ENDPOINT_XFER_ISOC;
2536 if (hsotg->params.dma_desc_enable)
2539 frame_desc = &urb->iso_descs[qtd->isoc_frame_index];
2540 frame_desc->status = 0;
2542 if (hsotg->params.host_dma) {
2543 chan->xfer_dma = urb->dma;
2544 chan->xfer_dma += frame_desc->offset +
2545 qtd->isoc_split_offset;
2547 chan->xfer_buf = urb->buf;
2548 chan->xfer_buf += frame_desc->offset +
2549 qtd->isoc_split_offset;
2552 chan->xfer_len = frame_desc->length - qtd->isoc_split_offset;
2554 if (chan->xact_pos == DWC2_HCSPLT_XACTPOS_ALL) {
2555 if (chan->xfer_len <= 188)
2556 chan->xact_pos = DWC2_HCSPLT_XACTPOS_ALL;
2558 chan->xact_pos = DWC2_HCSPLT_XACTPOS_BEGIN;
2564 #define DWC2_USB_DMA_ALIGN 4
2566 struct dma_aligned_buffer {
2568 void *old_xfer_buffer;
2572 static void dwc2_free_dma_aligned_buffer(struct urb *urb)
2574 struct dma_aligned_buffer *temp;
2576 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
2579 temp = container_of(urb->transfer_buffer,
2580 struct dma_aligned_buffer, data);
2582 if (usb_urb_dir_in(urb))
2583 memcpy(temp->old_xfer_buffer, temp->data,
2584 urb->transfer_buffer_length);
2585 urb->transfer_buffer = temp->old_xfer_buffer;
2586 kfree(temp->kmalloc_ptr);
2588 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
2591 static int dwc2_alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
2593 struct dma_aligned_buffer *temp, *kmalloc_ptr;
2594 size_t kmalloc_size;
2596 if (urb->num_sgs || urb->sg ||
2597 urb->transfer_buffer_length == 0 ||
2598 !((uintptr_t)urb->transfer_buffer & (DWC2_USB_DMA_ALIGN - 1)))
2601 /* Allocate a buffer with enough padding for alignment */
2602 kmalloc_size = urb->transfer_buffer_length +
2603 sizeof(struct dma_aligned_buffer) + DWC2_USB_DMA_ALIGN - 1;
2605 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
2609 /* Position our struct dma_aligned_buffer such that data is aligned */
2610 temp = PTR_ALIGN(kmalloc_ptr + 1, DWC2_USB_DMA_ALIGN) - 1;
2611 temp->kmalloc_ptr = kmalloc_ptr;
2612 temp->old_xfer_buffer = urb->transfer_buffer;
2613 if (usb_urb_dir_out(urb))
2614 memcpy(temp->data, urb->transfer_buffer,
2615 urb->transfer_buffer_length);
2616 urb->transfer_buffer = temp->data;
2618 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
2623 static int dwc2_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
2628 /* We assume setup_dma is always aligned; warn if not */
2629 WARN_ON_ONCE(urb->setup_dma &&
2630 (urb->setup_dma & (DWC2_USB_DMA_ALIGN - 1)));
2632 ret = dwc2_alloc_dma_aligned_buffer(urb, mem_flags);
2636 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
2638 dwc2_free_dma_aligned_buffer(urb);
2643 static void dwc2_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
2645 usb_hcd_unmap_urb_for_dma(hcd, urb);
2646 dwc2_free_dma_aligned_buffer(urb);
2650 * dwc2_assign_and_init_hc() - Assigns transactions from a QTD to a free host
2651 * channel and initializes the host channel to perform the transactions. The
2652 * host channel is removed from the free list.
2654 * @hsotg: The HCD state structure
2655 * @qh: Transactions from the first QTD for this QH are selected and assigned
2656 * to a free host channel
2658 static int dwc2_assign_and_init_hc(struct dwc2_hsotg *hsotg, struct dwc2_qh *qh)
2660 struct dwc2_host_chan *chan;
2661 struct dwc2_hcd_urb *urb;
2662 struct dwc2_qtd *qtd;
2665 dev_vdbg(hsotg->dev, "%s(%p,%p)\n", __func__, hsotg, qh);
2667 if (list_empty(&qh->qtd_list)) {
2668 dev_dbg(hsotg->dev, "No QTDs in QH list\n");
2672 if (list_empty(&hsotg->free_hc_list)) {
2673 dev_dbg(hsotg->dev, "No free channel to assign\n");
2677 chan = list_first_entry(&hsotg->free_hc_list, struct dwc2_host_chan,
2680 /* Remove host channel from free list */
2681 list_del_init(&chan->hc_list_entry);
2683 qtd = list_first_entry(&qh->qtd_list, struct dwc2_qtd, qtd_list_entry);
2686 qtd->in_process = 1;
2689 * Use usb_pipedevice to determine device address. This address is
2690 * 0 before the SET_ADDRESS command and the correct address afterward.
2692 chan->dev_addr = dwc2_hcd_get_dev_addr(&urb->pipe_info);
2693 chan->ep_num = dwc2_hcd_get_ep_num(&urb->pipe_info);
2694 chan->speed = qh->dev_speed;
2695 chan->max_packet = dwc2_max_packet(qh->maxp);
2697 chan->xfer_started = 0;
2698 chan->halt_status = DWC2_HC_XFER_NO_HALT_STATUS;
2699 chan->error_state = (qtd->error_count > 0);
2700 chan->halt_on_queue = 0;
2701 chan->halt_pending = 0;
2705 * The following values may be modified in the transfer type section
2706 * below. The xfer_len value may be reduced when the transfer is
2707 * started to accommodate the max widths of the XferSize and PktCnt
2708 * fields in the HCTSIZn register.
2711 chan->ep_is_in = (dwc2_hcd_is_pipe_in(&urb->pipe_info) != 0);
2715 chan->do_ping = qh->ping_state;
2717 chan->data_pid_start = qh->data_toggle;
2718 chan->multi_count = 1;
2720 if (urb->actual_length > urb->length &&
2721 !dwc2_hcd_is_pipe_in(&urb->pipe_info))
2722 urb->actual_length = urb->length;
2724 if (hsotg->params.host_dma)
2725 chan->xfer_dma = urb->dma + urb->actual_length;
2727 chan->xfer_buf = (u8 *)urb->buf + urb->actual_length;
2729 chan->xfer_len = urb->length - urb->actual_length;
2730 chan->xfer_count = 0;
2732 /* Set the split attributes if required */
2734 dwc2_hc_init_split(hsotg, chan, qtd, urb);
2738 /* Set the transfer attributes */
2739 dwc2_hc_init_xfer(hsotg, chan, qtd);
2741 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2742 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
2744 * This value may be modified when the transfer is started
2745 * to reflect the actual transfer length
2747 chan->multi_count = dwc2_hb_mult(qh->maxp);
2749 if (hsotg->params.dma_desc_enable) {
2750 chan->desc_list_addr = qh->desc_list_dma;
2751 chan->desc_list_sz = qh->desc_list_sz;
2754 dwc2_hc_init(hsotg, chan);
2761 * dwc2_hcd_select_transactions() - Selects transactions from the HCD transfer
2762 * schedule and assigns them to available host channels. Called from the HCD
2763 * interrupt handler functions.
2765 * @hsotg: The HCD state structure
2767 * Return: The types of new transactions that were assigned to host channels
2769 enum dwc2_transaction_type dwc2_hcd_select_transactions(
2770 struct dwc2_hsotg *hsotg)
2772 enum dwc2_transaction_type ret_val = DWC2_TRANSACTION_NONE;
2773 struct list_head *qh_ptr;
2777 #ifdef DWC2_DEBUG_SOF
2778 dev_vdbg(hsotg->dev, " Select Transactions\n");
2781 /* Process entries in the periodic ready list */
2782 qh_ptr = hsotg->periodic_sched_ready.next;
2783 while (qh_ptr != &hsotg->periodic_sched_ready) {
2784 if (list_empty(&hsotg->free_hc_list))
2786 if (hsotg->params.uframe_sched) {
2787 if (hsotg->available_host_channels <= 1)
2789 hsotg->available_host_channels--;
2791 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2792 if (dwc2_assign_and_init_hc(hsotg, qh))
2796 * Move the QH from the periodic ready schedule to the
2797 * periodic assigned schedule
2799 qh_ptr = qh_ptr->next;
2800 list_move_tail(&qh->qh_list_entry,
2801 &hsotg->periodic_sched_assigned);
2802 ret_val = DWC2_TRANSACTION_PERIODIC;
2806 * Process entries in the inactive portion of the non-periodic
2807 * schedule. Some free host channels may not be used if they are
2808 * reserved for periodic transfers.
2810 num_channels = hsotg->params.host_channels;
2811 qh_ptr = hsotg->non_periodic_sched_inactive.next;
2812 while (qh_ptr != &hsotg->non_periodic_sched_inactive) {
2813 if (!hsotg->params.uframe_sched &&
2814 hsotg->non_periodic_channels >= num_channels -
2815 hsotg->periodic_channels)
2817 if (list_empty(&hsotg->free_hc_list))
2819 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2820 if (hsotg->params.uframe_sched) {
2821 if (hsotg->available_host_channels < 1)
2823 hsotg->available_host_channels--;
2826 if (dwc2_assign_and_init_hc(hsotg, qh))
2830 * Move the QH from the non-periodic inactive schedule to the
2831 * non-periodic active schedule
2833 qh_ptr = qh_ptr->next;
2834 list_move_tail(&qh->qh_list_entry,
2835 &hsotg->non_periodic_sched_active);
2837 if (ret_val == DWC2_TRANSACTION_NONE)
2838 ret_val = DWC2_TRANSACTION_NON_PERIODIC;
2840 ret_val = DWC2_TRANSACTION_ALL;
2842 if (!hsotg->params.uframe_sched)
2843 hsotg->non_periodic_channels++;
2850 * dwc2_queue_transaction() - Attempts to queue a single transaction request for
2851 * a host channel associated with either a periodic or non-periodic transfer
2853 * @hsotg: The HCD state structure
2854 * @chan: Host channel descriptor associated with either a periodic or
2855 * non-periodic transfer
2856 * @fifo_dwords_avail: Number of DWORDs available in the periodic Tx FIFO
2857 * for periodic transfers or the non-periodic Tx FIFO
2858 * for non-periodic transfers
2860 * Return: 1 if a request is queued and more requests may be needed to
2861 * complete the transfer, 0 if no more requests are required for this
2862 * transfer, -1 if there is insufficient space in the Tx FIFO
2864 * This function assumes that there is space available in the appropriate
2865 * request queue. For an OUT transfer or SETUP transaction in Slave mode,
2866 * it checks whether space is available in the appropriate Tx FIFO.
2868 * Must be called with interrupt disabled and spinlock held
2870 static int dwc2_queue_transaction(struct dwc2_hsotg *hsotg,
2871 struct dwc2_host_chan *chan,
2872 u16 fifo_dwords_avail)
2877 /* Put ourselves on the list to keep order straight */
2878 list_move_tail(&chan->split_order_list_entry,
2879 &hsotg->split_order);
2881 if (hsotg->params.host_dma) {
2882 if (hsotg->params.dma_desc_enable) {
2883 if (!chan->xfer_started ||
2884 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2885 dwc2_hcd_start_xfer_ddma(hsotg, chan->qh);
2886 chan->qh->ping_state = 0;
2888 } else if (!chan->xfer_started) {
2889 dwc2_hc_start_transfer(hsotg, chan);
2890 chan->qh->ping_state = 0;
2892 } else if (chan->halt_pending) {
2893 /* Don't queue a request if the channel has been halted */
2894 } else if (chan->halt_on_queue) {
2895 dwc2_hc_halt(hsotg, chan, chan->halt_status);
2896 } else if (chan->do_ping) {
2897 if (!chan->xfer_started)
2898 dwc2_hc_start_transfer(hsotg, chan);
2899 } else if (!chan->ep_is_in ||
2900 chan->data_pid_start == DWC2_HC_PID_SETUP) {
2901 if ((fifo_dwords_avail * 4) >= chan->max_packet) {
2902 if (!chan->xfer_started) {
2903 dwc2_hc_start_transfer(hsotg, chan);
2906 retval = dwc2_hc_continue_transfer(hsotg, chan);
2912 if (!chan->xfer_started) {
2913 dwc2_hc_start_transfer(hsotg, chan);
2916 retval = dwc2_hc_continue_transfer(hsotg, chan);
2924 * Processes periodic channels for the next frame and queues transactions for
2925 * these channels to the DWC_otg controller. After queueing transactions, the
2926 * Periodic Tx FIFO Empty interrupt is enabled if there are more transactions
2927 * to queue as Periodic Tx FIFO or request queue space becomes available.
2928 * Otherwise, the Periodic Tx FIFO Empty interrupt is disabled.
2930 * Must be called with interrupt disabled and spinlock held
2932 static void dwc2_process_periodic_channels(struct dwc2_hsotg *hsotg)
2934 struct list_head *qh_ptr;
2940 bool no_queue_space = false;
2941 bool no_fifo_space = false;
2944 /* If empty list then just adjust interrupt enables */
2945 if (list_empty(&hsotg->periodic_sched_assigned))
2949 dev_vdbg(hsotg->dev, "Queue periodic transactions\n");
2951 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
2952 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2953 TXSTS_QSPCAVAIL_SHIFT;
2954 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2955 TXSTS_FSPCAVAIL_SHIFT;
2958 dev_vdbg(hsotg->dev, " P Tx Req Queue Space Avail (before queue): %d\n",
2960 dev_vdbg(hsotg->dev, " P Tx FIFO Space Avail (before queue): %d\n",
2964 qh_ptr = hsotg->periodic_sched_assigned.next;
2965 while (qh_ptr != &hsotg->periodic_sched_assigned) {
2966 tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
2967 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
2968 TXSTS_QSPCAVAIL_SHIFT;
2969 if (qspcavail == 0) {
2970 no_queue_space = true;
2974 qh = list_entry(qh_ptr, struct dwc2_qh, qh_list_entry);
2976 qh_ptr = qh_ptr->next;
2980 /* Make sure EP's TT buffer is clean before queueing qtds */
2981 if (qh->tt_buffer_dirty) {
2982 qh_ptr = qh_ptr->next;
2987 * Set a flag if we're queuing high-bandwidth in slave mode.
2988 * The flag prevents any halts to get into the request queue in
2989 * the middle of multiple high-bandwidth packets getting queued.
2991 if (!hsotg->params.host_dma &&
2992 qh->channel->multi_count > 1)
2993 hsotg->queuing_high_bandwidth = 1;
2995 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
2996 TXSTS_FSPCAVAIL_SHIFT;
2997 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
2999 no_fifo_space = true;
3004 * In Slave mode, stay on the current transfer until there is
3005 * nothing more to do or the high-bandwidth request count is
3006 * reached. In DMA mode, only need to queue one request. The
3007 * controller automatically handles multiple packets for
3008 * high-bandwidth transfers.
3010 if (hsotg->params.host_dma || status == 0 ||
3011 qh->channel->requests == qh->channel->multi_count) {
3012 qh_ptr = qh_ptr->next;
3014 * Move the QH from the periodic assigned schedule to
3015 * the periodic queued schedule
3017 list_move_tail(&qh->qh_list_entry,
3018 &hsotg->periodic_sched_queued);
3020 /* done queuing high bandwidth */
3021 hsotg->queuing_high_bandwidth = 0;
3026 if (no_queue_space || no_fifo_space ||
3027 (!hsotg->params.host_dma &&
3028 !list_empty(&hsotg->periodic_sched_assigned))) {
3030 * May need to queue more transactions as the request
3031 * queue or Tx FIFO empties. Enable the periodic Tx
3032 * FIFO empty interrupt. (Always use the half-empty
3033 * level to ensure that new requests are loaded as
3034 * soon as possible.)
3036 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3037 if (!(gintmsk & GINTSTS_PTXFEMP)) {
3038 gintmsk |= GINTSTS_PTXFEMP;
3039 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3043 * Disable the Tx FIFO empty interrupt since there are
3044 * no more transactions that need to be queued right
3045 * now. This function is called from interrupt
3046 * handlers to queue more transactions as transfer
3049 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3050 if (gintmsk & GINTSTS_PTXFEMP) {
3051 gintmsk &= ~GINTSTS_PTXFEMP;
3052 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3058 * Processes active non-periodic channels and queues transactions for these
3059 * channels to the DWC_otg controller. After queueing transactions, the NP Tx
3060 * FIFO Empty interrupt is enabled if there are more transactions to queue as
3061 * NP Tx FIFO or request queue space becomes available. Otherwise, the NP Tx
3062 * FIFO Empty interrupt is disabled.
3064 * Must be called with interrupt disabled and spinlock held
3066 static void dwc2_process_non_periodic_channels(struct dwc2_hsotg *hsotg)
3068 struct list_head *orig_qh_ptr;
3075 int no_queue_space = 0;
3076 int no_fifo_space = 0;
3079 dev_vdbg(hsotg->dev, "Queue non-periodic transactions\n");
3081 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
3082 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3083 TXSTS_QSPCAVAIL_SHIFT;
3084 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3085 TXSTS_FSPCAVAIL_SHIFT;
3086 dev_vdbg(hsotg->dev, " NP Tx Req Queue Space Avail (before queue): %d\n",
3088 dev_vdbg(hsotg->dev, " NP Tx FIFO Space Avail (before queue): %d\n",
3092 * Keep track of the starting point. Skip over the start-of-list
3095 if (hsotg->non_periodic_qh_ptr == &hsotg->non_periodic_sched_active)
3096 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3097 orig_qh_ptr = hsotg->non_periodic_qh_ptr;
3100 * Process once through the active list or until no more space is
3101 * available in the request queue or the Tx FIFO
3104 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
3105 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3106 TXSTS_QSPCAVAIL_SHIFT;
3107 if (!hsotg->params.host_dma && qspcavail == 0) {
3112 qh = list_entry(hsotg->non_periodic_qh_ptr, struct dwc2_qh,
3117 /* Make sure EP's TT buffer is clean before queueing qtds */
3118 if (qh->tt_buffer_dirty)
3121 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3122 TXSTS_FSPCAVAIL_SHIFT;
3123 status = dwc2_queue_transaction(hsotg, qh->channel, fspcavail);
3127 } else if (status < 0) {
3132 /* Advance to next QH, skipping start-of-list entry */
3133 hsotg->non_periodic_qh_ptr = hsotg->non_periodic_qh_ptr->next;
3134 if (hsotg->non_periodic_qh_ptr ==
3135 &hsotg->non_periodic_sched_active)
3136 hsotg->non_periodic_qh_ptr =
3137 hsotg->non_periodic_qh_ptr->next;
3138 } while (hsotg->non_periodic_qh_ptr != orig_qh_ptr);
3140 if (!hsotg->params.host_dma) {
3141 tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
3142 qspcavail = (tx_status & TXSTS_QSPCAVAIL_MASK) >>
3143 TXSTS_QSPCAVAIL_SHIFT;
3144 fspcavail = (tx_status & TXSTS_FSPCAVAIL_MASK) >>
3145 TXSTS_FSPCAVAIL_SHIFT;
3146 dev_vdbg(hsotg->dev,
3147 " NP Tx Req Queue Space Avail (after queue): %d\n",
3149 dev_vdbg(hsotg->dev,
3150 " NP Tx FIFO Space Avail (after queue): %d\n",
3153 if (more_to_do || no_queue_space || no_fifo_space) {
3155 * May need to queue more transactions as the request
3156 * queue or Tx FIFO empties. Enable the non-periodic
3157 * Tx FIFO empty interrupt. (Always use the half-empty
3158 * level to ensure that new requests are loaded as
3159 * soon as possible.)
3161 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3162 gintmsk |= GINTSTS_NPTXFEMP;
3163 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3166 * Disable the Tx FIFO empty interrupt since there are
3167 * no more transactions that need to be queued right
3168 * now. This function is called from interrupt
3169 * handlers to queue more transactions as transfer
3172 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3173 gintmsk &= ~GINTSTS_NPTXFEMP;
3174 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3180 * dwc2_hcd_queue_transactions() - Processes the currently active host channels
3181 * and queues transactions for these channels to the DWC_otg controller. Called
3182 * from the HCD interrupt handler functions.
3184 * @hsotg: The HCD state structure
3185 * @tr_type: The type(s) of transactions to queue (non-periodic, periodic,
3188 * Must be called with interrupt disabled and spinlock held
3190 void dwc2_hcd_queue_transactions(struct dwc2_hsotg *hsotg,
3191 enum dwc2_transaction_type tr_type)
3193 #ifdef DWC2_DEBUG_SOF
3194 dev_vdbg(hsotg->dev, "Queue Transactions\n");
3196 /* Process host channels associated with periodic transfers */
3197 if (tr_type == DWC2_TRANSACTION_PERIODIC ||
3198 tr_type == DWC2_TRANSACTION_ALL)
3199 dwc2_process_periodic_channels(hsotg);
3201 /* Process host channels associated with non-periodic transfers */
3202 if (tr_type == DWC2_TRANSACTION_NON_PERIODIC ||
3203 tr_type == DWC2_TRANSACTION_ALL) {
3204 if (!list_empty(&hsotg->non_periodic_sched_active)) {
3205 dwc2_process_non_periodic_channels(hsotg);
3208 * Ensure NP Tx FIFO empty interrupt is disabled when
3209 * there are no non-periodic transfers to process
3211 u32 gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
3213 gintmsk &= ~GINTSTS_NPTXFEMP;
3214 dwc2_writel(gintmsk, hsotg->regs + GINTMSK);
3219 static void dwc2_conn_id_status_change(struct work_struct *work)
3221 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
3225 unsigned long flags;
3227 dev_dbg(hsotg->dev, "%s()\n", __func__);
3229 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3230 dev_dbg(hsotg->dev, "gotgctl=%0x\n", gotgctl);
3231 dev_dbg(hsotg->dev, "gotgctl.b.conidsts=%d\n",
3232 !!(gotgctl & GOTGCTL_CONID_B));
3234 /* B-Device connector (Device Mode) */
3235 if (gotgctl & GOTGCTL_CONID_B) {
3236 /* Wait for switch to device mode */
3237 dev_dbg(hsotg->dev, "connId B\n");
3238 if (hsotg->bus_suspended) {
3239 dev_info(hsotg->dev,
3240 "Do port resume before switching to device mode\n");
3241 dwc2_port_resume(hsotg);
3243 while (!dwc2_is_device_mode(hsotg)) {
3244 dev_info(hsotg->dev,
3245 "Waiting for Peripheral Mode, Mode=%s\n",
3246 dwc2_is_host_mode(hsotg) ? "Host" :
3250 * Sometimes the initial GOTGCTRL read is wrong, so
3251 * check it again and jump to host mode if that was
3254 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3255 if (!(gotgctl & GOTGCTL_CONID_B))
3262 "Connection id status change timed out\n");
3263 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
3264 dwc2_core_init(hsotg, false);
3265 dwc2_enable_global_interrupts(hsotg);
3266 spin_lock_irqsave(&hsotg->lock, flags);
3267 dwc2_hsotg_core_init_disconnected(hsotg, false);
3268 spin_unlock_irqrestore(&hsotg->lock, flags);
3269 dwc2_hsotg_core_connect(hsotg);
3272 /* A-Device connector (Host Mode) */
3273 dev_dbg(hsotg->dev, "connId A\n");
3274 while (!dwc2_is_host_mode(hsotg)) {
3275 dev_info(hsotg->dev, "Waiting for Host Mode, Mode=%s\n",
3276 dwc2_is_host_mode(hsotg) ?
3277 "Host" : "Peripheral");
3284 "Connection id status change timed out\n");
3285 hsotg->op_state = OTG_STATE_A_HOST;
3287 /* Initialize the Core for Host mode */
3288 dwc2_core_init(hsotg, false);
3289 dwc2_enable_global_interrupts(hsotg);
3290 dwc2_hcd_start(hsotg);
3294 static void dwc2_wakeup_detected(unsigned long data)
3296 struct dwc2_hsotg *hsotg = (struct dwc2_hsotg *)data;
3299 dev_dbg(hsotg->dev, "%s()\n", __func__);
3302 * Clear the Resume after 70ms. (Need 20 ms minimum. Use 70 ms
3303 * so that OPT tests pass with all PHYs.)
3305 hprt0 = dwc2_read_hprt0(hsotg);
3306 dev_dbg(hsotg->dev, "Resume: HPRT0=%0x\n", hprt0);
3307 hprt0 &= ~HPRT0_RES;
3308 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3309 dev_dbg(hsotg->dev, "Clear Resume: HPRT0=%0x\n",
3310 dwc2_readl(hsotg->regs + HPRT0));
3312 dwc2_hcd_rem_wakeup(hsotg);
3313 hsotg->bus_suspended = false;
3315 /* Change to L0 state */
3316 hsotg->lx_state = DWC2_L0;
3319 static int dwc2_host_is_b_hnp_enabled(struct dwc2_hsotg *hsotg)
3321 struct usb_hcd *hcd = dwc2_hsotg_to_hcd(hsotg);
3323 return hcd->self.b_hnp_enable;
3326 /* Must NOT be called with interrupt disabled or spinlock held */
3327 static void dwc2_port_suspend(struct dwc2_hsotg *hsotg, u16 windex)
3329 unsigned long flags;
3334 dev_dbg(hsotg->dev, "%s()\n", __func__);
3336 spin_lock_irqsave(&hsotg->lock, flags);
3338 if (windex == hsotg->otg_port && dwc2_host_is_b_hnp_enabled(hsotg)) {
3339 gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
3340 gotgctl |= GOTGCTL_HSTSETHNPEN;
3341 dwc2_writel(gotgctl, hsotg->regs + GOTGCTL);
3342 hsotg->op_state = OTG_STATE_A_SUSPEND;
3345 hprt0 = dwc2_read_hprt0(hsotg);
3346 hprt0 |= HPRT0_SUSP;
3347 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3349 hsotg->bus_suspended = true;
3352 * If hibernation is supported, Phy clock will be suspended
3353 * after registers are backuped.
3355 if (!hsotg->params.hibernation) {
3356 /* Suspend the Phy Clock */
3357 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3358 pcgctl |= PCGCTL_STOPPCLK;
3359 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3363 /* For HNP the bus must be suspended for at least 200ms */
3364 if (dwc2_host_is_b_hnp_enabled(hsotg)) {
3365 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3366 pcgctl &= ~PCGCTL_STOPPCLK;
3367 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3369 spin_unlock_irqrestore(&hsotg->lock, flags);
3373 spin_unlock_irqrestore(&hsotg->lock, flags);
3377 /* Must NOT be called with interrupt disabled or spinlock held */
3378 static void dwc2_port_resume(struct dwc2_hsotg *hsotg)
3380 unsigned long flags;
3384 spin_lock_irqsave(&hsotg->lock, flags);
3387 * If hibernation is supported, Phy clock is already resumed
3388 * after registers restore.
3390 if (!hsotg->params.hibernation) {
3391 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3392 pcgctl &= ~PCGCTL_STOPPCLK;
3393 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3394 spin_unlock_irqrestore(&hsotg->lock, flags);
3396 spin_lock_irqsave(&hsotg->lock, flags);
3399 hprt0 = dwc2_read_hprt0(hsotg);
3401 hprt0 &= ~HPRT0_SUSP;
3402 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3403 spin_unlock_irqrestore(&hsotg->lock, flags);
3405 msleep(USB_RESUME_TIMEOUT);
3407 spin_lock_irqsave(&hsotg->lock, flags);
3408 hprt0 = dwc2_read_hprt0(hsotg);
3409 hprt0 &= ~(HPRT0_RES | HPRT0_SUSP);
3410 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3411 hsotg->bus_suspended = false;
3412 spin_unlock_irqrestore(&hsotg->lock, flags);
3415 /* Handles hub class-specific requests */
3416 static int dwc2_hcd_hub_control(struct dwc2_hsotg *hsotg, u16 typereq,
3417 u16 wvalue, u16 windex, char *buf, u16 wlength)
3419 struct usb_hub_descriptor *hub_desc;
3427 case ClearHubFeature:
3428 dev_dbg(hsotg->dev, "ClearHubFeature %1xh\n", wvalue);
3431 case C_HUB_LOCAL_POWER:
3432 case C_HUB_OVER_CURRENT:
3433 /* Nothing required here */
3439 "ClearHubFeature request %1xh unknown\n",
3444 case ClearPortFeature:
3445 if (wvalue != USB_PORT_FEAT_L1)
3446 if (!windex || windex > 1)
3449 case USB_PORT_FEAT_ENABLE:
3451 "ClearPortFeature USB_PORT_FEAT_ENABLE\n");
3452 hprt0 = dwc2_read_hprt0(hsotg);
3454 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3457 case USB_PORT_FEAT_SUSPEND:
3459 "ClearPortFeature USB_PORT_FEAT_SUSPEND\n");
3461 if (hsotg->bus_suspended)
3462 dwc2_port_resume(hsotg);
3465 case USB_PORT_FEAT_POWER:
3467 "ClearPortFeature USB_PORT_FEAT_POWER\n");
3468 hprt0 = dwc2_read_hprt0(hsotg);
3469 hprt0 &= ~HPRT0_PWR;
3470 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3473 case USB_PORT_FEAT_INDICATOR:
3475 "ClearPortFeature USB_PORT_FEAT_INDICATOR\n");
3476 /* Port indicator not supported */
3479 case USB_PORT_FEAT_C_CONNECTION:
3481 * Clears driver's internal Connect Status Change flag
3484 "ClearPortFeature USB_PORT_FEAT_C_CONNECTION\n");
3485 hsotg->flags.b.port_connect_status_change = 0;
3488 case USB_PORT_FEAT_C_RESET:
3489 /* Clears driver's internal Port Reset Change flag */
3491 "ClearPortFeature USB_PORT_FEAT_C_RESET\n");
3492 hsotg->flags.b.port_reset_change = 0;
3495 case USB_PORT_FEAT_C_ENABLE:
3497 * Clears the driver's internal Port Enable/Disable
3501 "ClearPortFeature USB_PORT_FEAT_C_ENABLE\n");
3502 hsotg->flags.b.port_enable_change = 0;
3505 case USB_PORT_FEAT_C_SUSPEND:
3507 * Clears the driver's internal Port Suspend Change
3508 * flag, which is set when resume signaling on the host
3512 "ClearPortFeature USB_PORT_FEAT_C_SUSPEND\n");
3513 hsotg->flags.b.port_suspend_change = 0;
3516 case USB_PORT_FEAT_C_PORT_L1:
3518 "ClearPortFeature USB_PORT_FEAT_C_PORT_L1\n");
3519 hsotg->flags.b.port_l1_change = 0;
3522 case USB_PORT_FEAT_C_OVER_CURRENT:
3524 "ClearPortFeature USB_PORT_FEAT_C_OVER_CURRENT\n");
3525 hsotg->flags.b.port_over_current_change = 0;
3531 "ClearPortFeature request %1xh unknown or unsupported\n",
3536 case GetHubDescriptor:
3537 dev_dbg(hsotg->dev, "GetHubDescriptor\n");
3538 hub_desc = (struct usb_hub_descriptor *)buf;
3539 hub_desc->bDescLength = 9;
3540 hub_desc->bDescriptorType = USB_DT_HUB;
3541 hub_desc->bNbrPorts = 1;
3542 hub_desc->wHubCharacteristics =
3543 cpu_to_le16(HUB_CHAR_COMMON_LPSM |
3544 HUB_CHAR_INDV_PORT_OCPM);
3545 hub_desc->bPwrOn2PwrGood = 1;
3546 hub_desc->bHubContrCurrent = 0;
3547 hub_desc->u.hs.DeviceRemovable[0] = 0;
3548 hub_desc->u.hs.DeviceRemovable[1] = 0xff;
3552 dev_dbg(hsotg->dev, "GetHubStatus\n");
3557 dev_vdbg(hsotg->dev,
3558 "GetPortStatus wIndex=0x%04x flags=0x%08x\n", windex,
3560 if (!windex || windex > 1)
3564 if (hsotg->flags.b.port_connect_status_change)
3565 port_status |= USB_PORT_STAT_C_CONNECTION << 16;
3566 if (hsotg->flags.b.port_enable_change)
3567 port_status |= USB_PORT_STAT_C_ENABLE << 16;
3568 if (hsotg->flags.b.port_suspend_change)
3569 port_status |= USB_PORT_STAT_C_SUSPEND << 16;
3570 if (hsotg->flags.b.port_l1_change)
3571 port_status |= USB_PORT_STAT_C_L1 << 16;
3572 if (hsotg->flags.b.port_reset_change)
3573 port_status |= USB_PORT_STAT_C_RESET << 16;
3574 if (hsotg->flags.b.port_over_current_change) {
3575 dev_warn(hsotg->dev, "Overcurrent change detected\n");
3576 port_status |= USB_PORT_STAT_C_OVERCURRENT << 16;
3579 if (!hsotg->flags.b.port_connect_status) {
3581 * The port is disconnected, which means the core is
3582 * either in device mode or it soon will be. Just
3583 * return 0's for the remainder of the port status
3584 * since the port register can't be read if the core
3585 * is in device mode.
3587 *(__le32 *)buf = cpu_to_le32(port_status);
3591 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
3592 dev_vdbg(hsotg->dev, " HPRT0: 0x%08x\n", hprt0);
3594 if (hprt0 & HPRT0_CONNSTS)
3595 port_status |= USB_PORT_STAT_CONNECTION;
3596 if (hprt0 & HPRT0_ENA)
3597 port_status |= USB_PORT_STAT_ENABLE;
3598 if (hprt0 & HPRT0_SUSP)
3599 port_status |= USB_PORT_STAT_SUSPEND;
3600 if (hprt0 & HPRT0_OVRCURRACT)
3601 port_status |= USB_PORT_STAT_OVERCURRENT;
3602 if (hprt0 & HPRT0_RST)
3603 port_status |= USB_PORT_STAT_RESET;
3604 if (hprt0 & HPRT0_PWR)
3605 port_status |= USB_PORT_STAT_POWER;
3607 speed = (hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT;
3608 if (speed == HPRT0_SPD_HIGH_SPEED)
3609 port_status |= USB_PORT_STAT_HIGH_SPEED;
3610 else if (speed == HPRT0_SPD_LOW_SPEED)
3611 port_status |= USB_PORT_STAT_LOW_SPEED;
3613 if (hprt0 & HPRT0_TSTCTL_MASK)
3614 port_status |= USB_PORT_STAT_TEST;
3615 /* USB_PORT_FEAT_INDICATOR unsupported always 0 */
3617 if (hsotg->params.dma_desc_fs_enable) {
3619 * Enable descriptor DMA only if a full speed
3620 * device is connected.
3622 if (hsotg->new_connection &&
3624 (USB_PORT_STAT_CONNECTION |
3625 USB_PORT_STAT_HIGH_SPEED |
3626 USB_PORT_STAT_LOW_SPEED)) ==
3627 USB_PORT_STAT_CONNECTION)) {
3630 dev_info(hsotg->dev, "Enabling descriptor DMA mode\n");
3631 hsotg->params.dma_desc_enable = true;
3632 hcfg = dwc2_readl(hsotg->regs + HCFG);
3633 hcfg |= HCFG_DESCDMA;
3634 dwc2_writel(hcfg, hsotg->regs + HCFG);
3635 hsotg->new_connection = false;
3639 dev_vdbg(hsotg->dev, "port_status=%08x\n", port_status);
3640 *(__le32 *)buf = cpu_to_le32(port_status);
3644 dev_dbg(hsotg->dev, "SetHubFeature\n");
3645 /* No HUB features supported */
3648 case SetPortFeature:
3649 dev_dbg(hsotg->dev, "SetPortFeature\n");
3650 if (wvalue != USB_PORT_FEAT_TEST && (!windex || windex > 1))
3653 if (!hsotg->flags.b.port_connect_status) {
3655 * The port is disconnected, which means the core is
3656 * either in device mode or it soon will be. Just
3657 * return without doing anything since the port
3658 * register can't be written if the core is in device
3665 case USB_PORT_FEAT_SUSPEND:
3667 "SetPortFeature - USB_PORT_FEAT_SUSPEND\n");
3668 if (windex != hsotg->otg_port)
3670 dwc2_port_suspend(hsotg, windex);
3673 case USB_PORT_FEAT_POWER:
3675 "SetPortFeature - USB_PORT_FEAT_POWER\n");
3676 hprt0 = dwc2_read_hprt0(hsotg);
3678 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3681 case USB_PORT_FEAT_RESET:
3682 hprt0 = dwc2_read_hprt0(hsotg);
3684 "SetPortFeature - USB_PORT_FEAT_RESET\n");
3685 pcgctl = dwc2_readl(hsotg->regs + PCGCTL);
3686 pcgctl &= ~(PCGCTL_ENBL_SLEEP_GATING | PCGCTL_STOPPCLK);
3687 dwc2_writel(pcgctl, hsotg->regs + PCGCTL);
3688 /* ??? Original driver does this */
3689 dwc2_writel(0, hsotg->regs + PCGCTL);
3691 hprt0 = dwc2_read_hprt0(hsotg);
3692 /* Clear suspend bit if resetting from suspend state */
3693 hprt0 &= ~HPRT0_SUSP;
3696 * When B-Host the Port reset bit is set in the Start
3697 * HCD Callback function, so that the reset is started
3698 * within 1ms of the HNP success interrupt
3700 if (!dwc2_hcd_is_b_host(hsotg)) {
3701 hprt0 |= HPRT0_PWR | HPRT0_RST;
3703 "In host mode, hprt0=%08x\n", hprt0);
3704 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3707 /* Clear reset bit in 10ms (FS/LS) or 50ms (HS) */
3709 hprt0 &= ~HPRT0_RST;
3710 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3711 hsotg->lx_state = DWC2_L0; /* Now back to On state */
3714 case USB_PORT_FEAT_INDICATOR:
3716 "SetPortFeature - USB_PORT_FEAT_INDICATOR\n");
3720 case USB_PORT_FEAT_TEST:
3721 hprt0 = dwc2_read_hprt0(hsotg);
3723 "SetPortFeature - USB_PORT_FEAT_TEST\n");
3724 hprt0 &= ~HPRT0_TSTCTL_MASK;
3725 hprt0 |= (windex >> 8) << HPRT0_TSTCTL_SHIFT;
3726 dwc2_writel(hprt0, hsotg->regs + HPRT0);
3732 "SetPortFeature %1xh unknown or unsupported\n",
3742 "Unknown hub control request: %1xh wIndex: %1xh wValue: %1xh\n",
3743 typereq, windex, wvalue);
3750 static int dwc2_hcd_is_status_changed(struct dwc2_hsotg *hsotg, int port)
3757 retval = (hsotg->flags.b.port_connect_status_change ||
3758 hsotg->flags.b.port_reset_change ||
3759 hsotg->flags.b.port_enable_change ||
3760 hsotg->flags.b.port_suspend_change ||
3761 hsotg->flags.b.port_over_current_change);
3765 "DWC OTG HCD HUB STATUS DATA: Root port status changed\n");
3766 dev_dbg(hsotg->dev, " port_connect_status_change: %d\n",
3767 hsotg->flags.b.port_connect_status_change);
3768 dev_dbg(hsotg->dev, " port_reset_change: %d\n",
3769 hsotg->flags.b.port_reset_change);
3770 dev_dbg(hsotg->dev, " port_enable_change: %d\n",
3771 hsotg->flags.b.port_enable_change);
3772 dev_dbg(hsotg->dev, " port_suspend_change: %d\n",
3773 hsotg->flags.b.port_suspend_change);
3774 dev_dbg(hsotg->dev, " port_over_current_change: %d\n",
3775 hsotg->flags.b.port_over_current_change);
3781 int dwc2_hcd_get_frame_number(struct dwc2_hsotg *hsotg)
3783 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3785 #ifdef DWC2_DEBUG_SOF
3786 dev_vdbg(hsotg->dev, "DWC OTG HCD GET FRAME NUMBER %d\n",
3787 (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT);
3789 return (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3792 int dwc2_hcd_get_future_frame_number(struct dwc2_hsotg *hsotg, int us)
3794 u32 hprt = dwc2_readl(hsotg->regs + HPRT0);
3795 u32 hfir = dwc2_readl(hsotg->regs + HFIR);
3796 u32 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3797 unsigned int us_per_frame;
3798 unsigned int frame_number;
3799 unsigned int remaining;
3800 unsigned int interval;
3801 unsigned int phy_clks;
3803 /* High speed has 125 us per (micro) frame; others are 1 ms per */
3804 us_per_frame = (hprt & HPRT0_SPD_MASK) ? 1000 : 125;
3806 /* Extract fields */
3807 frame_number = (hfnum & HFNUM_FRNUM_MASK) >> HFNUM_FRNUM_SHIFT;
3808 remaining = (hfnum & HFNUM_FRREM_MASK) >> HFNUM_FRREM_SHIFT;
3809 interval = (hfir & HFIR_FRINT_MASK) >> HFIR_FRINT_SHIFT;
3812 * Number of phy clocks since the last tick of the frame number after
3815 phy_clks = (interval - remaining) +
3816 DIV_ROUND_UP(interval * us, us_per_frame);
3818 return dwc2_frame_num_inc(frame_number, phy_clks / interval);
3821 int dwc2_hcd_is_b_host(struct dwc2_hsotg *hsotg)
3823 return hsotg->op_state == OTG_STATE_B_HOST;
3826 static struct dwc2_hcd_urb *dwc2_hcd_urb_alloc(struct dwc2_hsotg *hsotg,
3830 struct dwc2_hcd_urb *urb;
3831 u32 size = sizeof(*urb) + iso_desc_count *
3832 sizeof(struct dwc2_hcd_iso_packet_desc);
3834 urb = kzalloc(size, mem_flags);
3836 urb->packet_count = iso_desc_count;
3840 static void dwc2_hcd_urb_set_pipeinfo(struct dwc2_hsotg *hsotg,
3841 struct dwc2_hcd_urb *urb, u8 dev_addr,
3842 u8 ep_num, u8 ep_type, u8 ep_dir, u16 mps)
3845 ep_type == USB_ENDPOINT_XFER_BULK ||
3846 ep_type == USB_ENDPOINT_XFER_CONTROL)
3847 dev_vdbg(hsotg->dev,
3848 "addr=%d, ep_num=%d, ep_dir=%1x, ep_type=%1x, mps=%d\n",
3849 dev_addr, ep_num, ep_dir, ep_type, mps);
3850 urb->pipe_info.dev_addr = dev_addr;
3851 urb->pipe_info.ep_num = ep_num;
3852 urb->pipe_info.pipe_type = ep_type;
3853 urb->pipe_info.pipe_dir = ep_dir;
3854 urb->pipe_info.mps = mps;
3858 * NOTE: This function will be removed once the peripheral controller code
3859 * is integrated and the driver is stable
3861 void dwc2_hcd_dump_state(struct dwc2_hsotg *hsotg)
3864 struct dwc2_host_chan *chan;
3865 struct dwc2_hcd_urb *urb;
3866 struct dwc2_qtd *qtd;
3872 num_channels = hsotg->params.host_channels;
3873 dev_dbg(hsotg->dev, "\n");
3875 "************************************************************\n");
3876 dev_dbg(hsotg->dev, "HCD State:\n");
3877 dev_dbg(hsotg->dev, " Num channels: %d\n", num_channels);
3879 for (i = 0; i < num_channels; i++) {
3880 chan = hsotg->hc_ptr_array[i];
3881 dev_dbg(hsotg->dev, " Channel %d:\n", i);
3883 " dev_addr: %d, ep_num: %d, ep_is_in: %d\n",
3884 chan->dev_addr, chan->ep_num, chan->ep_is_in);
3885 dev_dbg(hsotg->dev, " speed: %d\n", chan->speed);
3886 dev_dbg(hsotg->dev, " ep_type: %d\n", chan->ep_type);
3887 dev_dbg(hsotg->dev, " max_packet: %d\n", chan->max_packet);
3888 dev_dbg(hsotg->dev, " data_pid_start: %d\n",
3889 chan->data_pid_start);
3890 dev_dbg(hsotg->dev, " multi_count: %d\n", chan->multi_count);
3891 dev_dbg(hsotg->dev, " xfer_started: %d\n",
3892 chan->xfer_started);
3893 dev_dbg(hsotg->dev, " xfer_buf: %p\n", chan->xfer_buf);
3894 dev_dbg(hsotg->dev, " xfer_dma: %08lx\n",
3895 (unsigned long)chan->xfer_dma);
3896 dev_dbg(hsotg->dev, " xfer_len: %d\n", chan->xfer_len);
3897 dev_dbg(hsotg->dev, " xfer_count: %d\n", chan->xfer_count);
3898 dev_dbg(hsotg->dev, " halt_on_queue: %d\n",
3899 chan->halt_on_queue);
3900 dev_dbg(hsotg->dev, " halt_pending: %d\n",
3901 chan->halt_pending);
3902 dev_dbg(hsotg->dev, " halt_status: %d\n", chan->halt_status);
3903 dev_dbg(hsotg->dev, " do_split: %d\n", chan->do_split);
3904 dev_dbg(hsotg->dev, " complete_split: %d\n",
3905 chan->complete_split);
3906 dev_dbg(hsotg->dev, " hub_addr: %d\n", chan->hub_addr);
3907 dev_dbg(hsotg->dev, " hub_port: %d\n", chan->hub_port);
3908 dev_dbg(hsotg->dev, " xact_pos: %d\n", chan->xact_pos);
3909 dev_dbg(hsotg->dev, " requests: %d\n", chan->requests);
3910 dev_dbg(hsotg->dev, " qh: %p\n", chan->qh);
3912 if (chan->xfer_started) {
3913 u32 hfnum, hcchar, hctsiz, hcint, hcintmsk;
3915 hfnum = dwc2_readl(hsotg->regs + HFNUM);
3916 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
3917 hctsiz = dwc2_readl(hsotg->regs + HCTSIZ(i));
3918 hcint = dwc2_readl(hsotg->regs + HCINT(i));
3919 hcintmsk = dwc2_readl(hsotg->regs + HCINTMSK(i));
3920 dev_dbg(hsotg->dev, " hfnum: 0x%08x\n", hfnum);
3921 dev_dbg(hsotg->dev, " hcchar: 0x%08x\n", hcchar);
3922 dev_dbg(hsotg->dev, " hctsiz: 0x%08x\n", hctsiz);
3923 dev_dbg(hsotg->dev, " hcint: 0x%08x\n", hcint);
3924 dev_dbg(hsotg->dev, " hcintmsk: 0x%08x\n", hcintmsk);
3927 if (!(chan->xfer_started && chan->qh))
3930 list_for_each_entry(qtd, &chan->qh->qtd_list, qtd_list_entry) {
3931 if (!qtd->in_process)
3934 dev_dbg(hsotg->dev, " URB Info:\n");
3935 dev_dbg(hsotg->dev, " qtd: %p, urb: %p\n",
3939 " Dev: %d, EP: %d %s\n",
3940 dwc2_hcd_get_dev_addr(&urb->pipe_info),
3941 dwc2_hcd_get_ep_num(&urb->pipe_info),
3942 dwc2_hcd_is_pipe_in(&urb->pipe_info) ?
3945 " Max packet size: %d\n",
3946 dwc2_hcd_get_mps(&urb->pipe_info));
3948 " transfer_buffer: %p\n",
3951 " transfer_dma: %08lx\n",
3952 (unsigned long)urb->dma);
3954 " transfer_buffer_length: %d\n",
3956 dev_dbg(hsotg->dev, " actual_length: %d\n",
3957 urb->actual_length);
3962 dev_dbg(hsotg->dev, " non_periodic_channels: %d\n",
3963 hsotg->non_periodic_channels);
3964 dev_dbg(hsotg->dev, " periodic_channels: %d\n",
3965 hsotg->periodic_channels);
3966 dev_dbg(hsotg->dev, " periodic_usecs: %d\n", hsotg->periodic_usecs);
3967 np_tx_status = dwc2_readl(hsotg->regs + GNPTXSTS);
3968 dev_dbg(hsotg->dev, " NP Tx Req Queue Space Avail: %d\n",
3969 (np_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3970 dev_dbg(hsotg->dev, " NP Tx FIFO Space Avail: %d\n",
3971 (np_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3972 p_tx_status = dwc2_readl(hsotg->regs + HPTXSTS);
3973 dev_dbg(hsotg->dev, " P Tx Req Queue Space Avail: %d\n",
3974 (p_tx_status & TXSTS_QSPCAVAIL_MASK) >> TXSTS_QSPCAVAIL_SHIFT);
3975 dev_dbg(hsotg->dev, " P Tx FIFO Space Avail: %d\n",
3976 (p_tx_status & TXSTS_FSPCAVAIL_MASK) >> TXSTS_FSPCAVAIL_SHIFT);
3977 dwc2_hcd_dump_frrem(hsotg);
3978 dwc2_dump_global_registers(hsotg);
3979 dwc2_dump_host_registers(hsotg);
3981 "************************************************************\n");
3982 dev_dbg(hsotg->dev, "\n");
3987 * NOTE: This function will be removed once the peripheral controller code
3988 * is integrated and the driver is stable
3990 void dwc2_hcd_dump_frrem(struct dwc2_hsotg *hsotg)
3992 #ifdef DWC2_DUMP_FRREM
3993 dev_dbg(hsotg->dev, "Frame remaining at SOF:\n");
3994 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
3995 hsotg->frrem_samples, hsotg->frrem_accum,
3996 hsotg->frrem_samples > 0 ?
3997 hsotg->frrem_accum / hsotg->frrem_samples : 0);
3998 dev_dbg(hsotg->dev, "\n");
3999 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 7):\n");
4000 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4001 hsotg->hfnum_7_samples,
4002 hsotg->hfnum_7_frrem_accum,
4003 hsotg->hfnum_7_samples > 0 ?
4004 hsotg->hfnum_7_frrem_accum / hsotg->hfnum_7_samples : 0);
4005 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 0):\n");
4006 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4007 hsotg->hfnum_0_samples,
4008 hsotg->hfnum_0_frrem_accum,
4009 hsotg->hfnum_0_samples > 0 ?
4010 hsotg->hfnum_0_frrem_accum / hsotg->hfnum_0_samples : 0);
4011 dev_dbg(hsotg->dev, "Frame remaining at start_transfer (uframe 1-6):\n");
4012 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4013 hsotg->hfnum_other_samples,
4014 hsotg->hfnum_other_frrem_accum,
4015 hsotg->hfnum_other_samples > 0 ?
4016 hsotg->hfnum_other_frrem_accum / hsotg->hfnum_other_samples :
4018 dev_dbg(hsotg->dev, "\n");
4019 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 7):\n");
4020 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4021 hsotg->hfnum_7_samples_a, hsotg->hfnum_7_frrem_accum_a,
4022 hsotg->hfnum_7_samples_a > 0 ?
4023 hsotg->hfnum_7_frrem_accum_a / hsotg->hfnum_7_samples_a : 0);
4024 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 0):\n");
4025 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4026 hsotg->hfnum_0_samples_a, hsotg->hfnum_0_frrem_accum_a,
4027 hsotg->hfnum_0_samples_a > 0 ?
4028 hsotg->hfnum_0_frrem_accum_a / hsotg->hfnum_0_samples_a : 0);
4029 dev_dbg(hsotg->dev, "Frame remaining at sample point A (uframe 1-6):\n");
4030 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4031 hsotg->hfnum_other_samples_a, hsotg->hfnum_other_frrem_accum_a,
4032 hsotg->hfnum_other_samples_a > 0 ?
4033 hsotg->hfnum_other_frrem_accum_a / hsotg->hfnum_other_samples_a
4035 dev_dbg(hsotg->dev, "\n");
4036 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 7):\n");
4037 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4038 hsotg->hfnum_7_samples_b, hsotg->hfnum_7_frrem_accum_b,
4039 hsotg->hfnum_7_samples_b > 0 ?
4040 hsotg->hfnum_7_frrem_accum_b / hsotg->hfnum_7_samples_b : 0);
4041 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 0):\n");
4042 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4043 hsotg->hfnum_0_samples_b, hsotg->hfnum_0_frrem_accum_b,
4044 (hsotg->hfnum_0_samples_b > 0) ?
4045 hsotg->hfnum_0_frrem_accum_b / hsotg->hfnum_0_samples_b : 0);
4046 dev_dbg(hsotg->dev, "Frame remaining at sample point B (uframe 1-6):\n");
4047 dev_dbg(hsotg->dev, " samples %u, accum %llu, avg %llu\n",
4048 hsotg->hfnum_other_samples_b, hsotg->hfnum_other_frrem_accum_b,
4049 (hsotg->hfnum_other_samples_b > 0) ?
4050 hsotg->hfnum_other_frrem_accum_b / hsotg->hfnum_other_samples_b
4055 struct wrapper_priv_data {
4056 struct dwc2_hsotg *hsotg;
4059 /* Gets the dwc2_hsotg from a usb_hcd */
4060 static struct dwc2_hsotg *dwc2_hcd_to_hsotg(struct usb_hcd *hcd)
4062 struct wrapper_priv_data *p;
4064 p = (struct wrapper_priv_data *)&hcd->hcd_priv;
4069 * dwc2_host_get_tt_info() - Get the dwc2_tt associated with context
4071 * This will get the dwc2_tt structure (and ttport) associated with the given
4072 * context (which is really just a struct urb pointer).
4074 * The first time this is called for a given TT we allocate memory for our
4075 * structure. When everyone is done and has called dwc2_host_put_tt_info()
4076 * then the refcount for the structure will go to 0 and we'll free it.
4078 * @hsotg: The HCD state structure for the DWC OTG controller.
4079 * @qh: The QH structure.
4080 * @context: The priv pointer from a struct dwc2_hcd_urb.
4081 * @mem_flags: Flags for allocating memory.
4082 * @ttport: We'll return this device's port number here. That's used to
4083 * reference into the bitmap if we're on a multi_tt hub.
4085 * Return: a pointer to a struct dwc2_tt. Don't forget to call
4086 * dwc2_host_put_tt_info()! Returns NULL upon memory alloc failure.
4089 struct dwc2_tt *dwc2_host_get_tt_info(struct dwc2_hsotg *hsotg, void *context,
4090 gfp_t mem_flags, int *ttport)
4092 struct urb *urb = context;
4093 struct dwc2_tt *dwc_tt = NULL;
4096 *ttport = urb->dev->ttport;
4098 dwc_tt = urb->dev->tt->hcpriv;
4103 * For single_tt we need one schedule. For multi_tt
4104 * we need one per port.
4106 bitmap_size = DWC2_ELEMENTS_PER_LS_BITMAP *
4107 sizeof(dwc_tt->periodic_bitmaps[0]);
4108 if (urb->dev->tt->multi)
4109 bitmap_size *= urb->dev->tt->hub->maxchild;
4111 dwc_tt = kzalloc(sizeof(*dwc_tt) + bitmap_size,
4116 dwc_tt->usb_tt = urb->dev->tt;
4117 dwc_tt->usb_tt->hcpriv = dwc_tt;
4127 * dwc2_host_put_tt_info() - Put the dwc2_tt from dwc2_host_get_tt_info()
4129 * Frees resources allocated by dwc2_host_get_tt_info() if all current holders
4130 * of the structure are done.
4132 * It's OK to call this with NULL.
4134 * @hsotg: The HCD state structure for the DWC OTG controller.
4135 * @dwc_tt: The pointer returned by dwc2_host_get_tt_info.
4137 void dwc2_host_put_tt_info(struct dwc2_hsotg *hsotg, struct dwc2_tt *dwc_tt)
4139 /* Model kfree and make put of NULL a no-op */
4143 WARN_ON(dwc_tt->refcount < 1);
4146 if (!dwc_tt->refcount) {
4147 dwc_tt->usb_tt->hcpriv = NULL;
4152 int dwc2_host_get_speed(struct dwc2_hsotg *hsotg, void *context)
4154 struct urb *urb = context;
4156 return urb->dev->speed;
4159 static void dwc2_allocate_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4162 struct usb_bus *bus = hcd_to_bus(hcd);
4165 bus->bandwidth_allocated += bw / urb->interval;
4166 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4167 bus->bandwidth_isoc_reqs++;
4169 bus->bandwidth_int_reqs++;
4172 static void dwc2_free_bus_bandwidth(struct usb_hcd *hcd, u16 bw,
4175 struct usb_bus *bus = hcd_to_bus(hcd);
4178 bus->bandwidth_allocated -= bw / urb->interval;
4179 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
4180 bus->bandwidth_isoc_reqs--;
4182 bus->bandwidth_int_reqs--;
4186 * Sets the final status of an URB and returns it to the upper layer. Any
4187 * required cleanup of the URB is performed.
4189 * Must be called with interrupt disabled and spinlock held
4191 void dwc2_host_complete(struct dwc2_hsotg *hsotg, struct dwc2_qtd *qtd,
4198 dev_dbg(hsotg->dev, "## %s: qtd is NULL ##\n", __func__);
4203 dev_dbg(hsotg->dev, "## %s: qtd->urb is NULL ##\n", __func__);
4207 urb = qtd->urb->priv;
4209 dev_dbg(hsotg->dev, "## %s: urb->priv is NULL ##\n", __func__);
4213 urb->actual_length = dwc2_hcd_urb_get_actual_length(qtd->urb);
4216 dev_vdbg(hsotg->dev,
4217 "%s: urb %p device %d ep %d-%s status %d actual %d\n",
4218 __func__, urb, usb_pipedevice(urb->pipe),
4219 usb_pipeendpoint(urb->pipe),
4220 usb_pipein(urb->pipe) ? "IN" : "OUT", status,
4221 urb->actual_length);
4223 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4224 urb->error_count = dwc2_hcd_urb_get_error_count(qtd->urb);
4225 for (i = 0; i < urb->number_of_packets; ++i) {
4226 urb->iso_frame_desc[i].actual_length =
4227 dwc2_hcd_urb_get_iso_desc_actual_length(
4229 urb->iso_frame_desc[i].status =
4230 dwc2_hcd_urb_get_iso_desc_status(qtd->urb, i);
4234 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS && dbg_perio()) {
4235 for (i = 0; i < urb->number_of_packets; i++)
4236 dev_vdbg(hsotg->dev, " ISO Desc %d status %d\n",
4237 i, urb->iso_frame_desc[i].status);
4240 urb->status = status;
4242 if ((urb->transfer_flags & URB_SHORT_NOT_OK) &&
4243 urb->actual_length < urb->transfer_buffer_length)
4244 urb->status = -EREMOTEIO;
4247 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4248 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4249 struct usb_host_endpoint *ep = urb->ep;
4252 dwc2_free_bus_bandwidth(dwc2_hsotg_to_hcd(hsotg),
4253 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4257 usb_hcd_unlink_urb_from_ep(dwc2_hsotg_to_hcd(hsotg), urb);
4262 usb_hcd_giveback_urb(dwc2_hsotg_to_hcd(hsotg), urb, status);
4266 * Work queue function for starting the HCD when A-Cable is connected
4268 static void dwc2_hcd_start_func(struct work_struct *work)
4270 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4273 dev_dbg(hsotg->dev, "%s() %p\n", __func__, hsotg);
4274 dwc2_host_start(hsotg);
4278 * Reset work queue function
4280 static void dwc2_hcd_reset_func(struct work_struct *work)
4282 struct dwc2_hsotg *hsotg = container_of(work, struct dwc2_hsotg,
4284 unsigned long flags;
4287 dev_dbg(hsotg->dev, "USB RESET function called\n");
4289 spin_lock_irqsave(&hsotg->lock, flags);
4291 hprt0 = dwc2_read_hprt0(hsotg);
4292 hprt0 &= ~HPRT0_RST;
4293 dwc2_writel(hprt0, hsotg->regs + HPRT0);
4294 hsotg->flags.b.port_reset_change = 1;
4296 spin_unlock_irqrestore(&hsotg->lock, flags);
4300 * =========================================================================
4301 * Linux HC Driver Functions
4302 * =========================================================================
4306 * Initializes the DWC_otg controller and its root hub and prepares it for host
4307 * mode operation. Activates the root port. Returns 0 on success and a negative
4308 * error code on failure.
4310 static int _dwc2_hcd_start(struct usb_hcd *hcd)
4312 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4313 struct usb_bus *bus = hcd_to_bus(hcd);
4314 unsigned long flags;
4316 dev_dbg(hsotg->dev, "DWC OTG HCD START\n");
4318 spin_lock_irqsave(&hsotg->lock, flags);
4319 hsotg->lx_state = DWC2_L0;
4320 hcd->state = HC_STATE_RUNNING;
4321 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4323 if (dwc2_is_device_mode(hsotg)) {
4324 spin_unlock_irqrestore(&hsotg->lock, flags);
4325 return 0; /* why 0 ?? */
4328 dwc2_hcd_reinit(hsotg);
4330 /* Initialize and connect root hub if one is not already attached */
4331 if (bus->root_hub) {
4332 dev_dbg(hsotg->dev, "DWC OTG HCD Has Root Hub\n");
4333 /* Inform the HUB driver to resume */
4334 usb_hcd_resume_root_hub(hcd);
4337 spin_unlock_irqrestore(&hsotg->lock, flags);
4342 * Halts the DWC_otg host mode operations in a clean manner. USB transfers are
4345 static void _dwc2_hcd_stop(struct usb_hcd *hcd)
4347 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4348 unsigned long flags;
4350 /* Turn off all host-specific interrupts */
4351 dwc2_disable_host_interrupts(hsotg);
4353 /* Wait for interrupt processing to finish */
4354 synchronize_irq(hcd->irq);
4356 spin_lock_irqsave(&hsotg->lock, flags);
4357 /* Ensure hcd is disconnected */
4358 dwc2_hcd_disconnect(hsotg, true);
4359 dwc2_hcd_stop(hsotg);
4360 hsotg->lx_state = DWC2_L3;
4361 hcd->state = HC_STATE_HALT;
4362 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4363 spin_unlock_irqrestore(&hsotg->lock, flags);
4365 usleep_range(1000, 3000);
4368 static int _dwc2_hcd_suspend(struct usb_hcd *hcd)
4370 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4371 unsigned long flags;
4375 spin_lock_irqsave(&hsotg->lock, flags);
4377 if (hsotg->lx_state != DWC2_L0)
4380 if (!HCD_HW_ACCESSIBLE(hcd))
4383 if (hsotg->op_state == OTG_STATE_B_PERIPHERAL)
4386 if (!hsotg->params.hibernation)
4387 goto skip_power_saving;
4390 * Drive USB suspend and disable port Power
4391 * if usb bus is not suspended.
4393 if (!hsotg->bus_suspended) {
4394 hprt0 = dwc2_read_hprt0(hsotg);
4395 hprt0 |= HPRT0_SUSP;
4396 hprt0 &= ~HPRT0_PWR;
4397 dwc2_writel(hprt0, hsotg->regs + HPRT0);
4400 /* Enter hibernation */
4401 ret = dwc2_enter_hibernation(hsotg);
4403 if (ret != -ENOTSUPP)
4405 "enter hibernation failed\n");
4406 goto skip_power_saving;
4409 /* Ask phy to be suspended */
4410 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4411 spin_unlock_irqrestore(&hsotg->lock, flags);
4412 usb_phy_set_suspend(hsotg->uphy, true);
4413 spin_lock_irqsave(&hsotg->lock, flags);
4416 /* After entering hibernation, hardware is no more accessible */
4417 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4420 hsotg->lx_state = DWC2_L2;
4422 spin_unlock_irqrestore(&hsotg->lock, flags);
4427 static int _dwc2_hcd_resume(struct usb_hcd *hcd)
4429 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4430 unsigned long flags;
4433 spin_lock_irqsave(&hsotg->lock, flags);
4435 if (hsotg->lx_state != DWC2_L2)
4438 if (!hsotg->params.hibernation) {
4439 hsotg->lx_state = DWC2_L0;
4444 * Set HW accessible bit before powering on the controller
4445 * since an interrupt may rise.
4447 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4450 * Enable power if not already done.
4451 * This must not be spinlocked since duration
4452 * of this call is unknown.
4454 if (!IS_ERR_OR_NULL(hsotg->uphy)) {
4455 spin_unlock_irqrestore(&hsotg->lock, flags);
4456 usb_phy_set_suspend(hsotg->uphy, false);
4457 spin_lock_irqsave(&hsotg->lock, flags);
4460 /* Exit hibernation */
4461 ret = dwc2_exit_hibernation(hsotg, true);
4462 if (ret && (ret != -ENOTSUPP))
4463 dev_err(hsotg->dev, "exit hibernation failed\n");
4465 hsotg->lx_state = DWC2_L0;
4467 spin_unlock_irqrestore(&hsotg->lock, flags);
4469 if (hsotg->bus_suspended) {
4470 spin_lock_irqsave(&hsotg->lock, flags);
4471 hsotg->flags.b.port_suspend_change = 1;
4472 spin_unlock_irqrestore(&hsotg->lock, flags);
4473 dwc2_port_resume(hsotg);
4475 /* Wait for controller to correctly update D+/D- level */
4476 usleep_range(3000, 5000);
4479 * Clear Port Enable and Port Status changes.
4480 * Enable Port Power.
4482 dwc2_writel(HPRT0_PWR | HPRT0_CONNDET |
4483 HPRT0_ENACHG, hsotg->regs + HPRT0);
4484 /* Wait for controller to detect Port Connect */
4485 usleep_range(5000, 7000);
4490 spin_unlock_irqrestore(&hsotg->lock, flags);
4495 /* Returns the current frame number */
4496 static int _dwc2_hcd_get_frame_number(struct usb_hcd *hcd)
4498 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4500 return dwc2_hcd_get_frame_number(hsotg);
4503 static void dwc2_dump_urb_info(struct usb_hcd *hcd, struct urb *urb,
4506 #ifdef VERBOSE_DEBUG
4507 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4508 char *pipetype = NULL;
4511 dev_vdbg(hsotg->dev, "%s, urb %p\n", fn_name, urb);
4512 dev_vdbg(hsotg->dev, " Device address: %d\n",
4513 usb_pipedevice(urb->pipe));
4514 dev_vdbg(hsotg->dev, " Endpoint: %d, %s\n",
4515 usb_pipeendpoint(urb->pipe),
4516 usb_pipein(urb->pipe) ? "IN" : "OUT");
4518 switch (usb_pipetype(urb->pipe)) {
4520 pipetype = "CONTROL";
4525 case PIPE_INTERRUPT:
4526 pipetype = "INTERRUPT";
4528 case PIPE_ISOCHRONOUS:
4529 pipetype = "ISOCHRONOUS";
4533 dev_vdbg(hsotg->dev, " Endpoint type: %s %s (%s)\n", pipetype,
4534 usb_urb_dir_in(urb) ? "IN" : "OUT", usb_pipein(urb->pipe) ?
4537 switch (urb->dev->speed) {
4538 case USB_SPEED_HIGH:
4541 case USB_SPEED_FULL:
4552 dev_vdbg(hsotg->dev, " Speed: %s\n", speed);
4553 dev_vdbg(hsotg->dev, " Max packet size: %d\n",
4554 usb_maxpacket(urb->dev, urb->pipe, usb_pipeout(urb->pipe)));
4555 dev_vdbg(hsotg->dev, " Data buffer length: %d\n",
4556 urb->transfer_buffer_length);
4557 dev_vdbg(hsotg->dev, " Transfer buffer: %p, Transfer DMA: %08lx\n",
4558 urb->transfer_buffer, (unsigned long)urb->transfer_dma);
4559 dev_vdbg(hsotg->dev, " Setup buffer: %p, Setup DMA: %08lx\n",
4560 urb->setup_packet, (unsigned long)urb->setup_dma);
4561 dev_vdbg(hsotg->dev, " Interval: %d\n", urb->interval);
4563 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
4566 for (i = 0; i < urb->number_of_packets; i++) {
4567 dev_vdbg(hsotg->dev, " ISO Desc %d:\n", i);
4568 dev_vdbg(hsotg->dev, " offset: %d, length %d\n",
4569 urb->iso_frame_desc[i].offset,
4570 urb->iso_frame_desc[i].length);
4577 * Starts processing a USB transfer request specified by a USB Request Block
4578 * (URB). mem_flags indicates the type of memory allocation to use while
4579 * processing this URB.
4581 static int _dwc2_hcd_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4584 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4585 struct usb_host_endpoint *ep = urb->ep;
4586 struct dwc2_hcd_urb *dwc2_urb;
4589 int alloc_bandwidth = 0;
4593 unsigned long flags;
4595 bool qh_allocated = false;
4596 struct dwc2_qtd *qtd;
4599 dev_vdbg(hsotg->dev, "DWC OTG HCD URB Enqueue\n");
4600 dwc2_dump_urb_info(hcd, urb, "urb_enqueue");
4606 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS ||
4607 usb_pipetype(urb->pipe) == PIPE_INTERRUPT) {
4608 spin_lock_irqsave(&hsotg->lock, flags);
4609 if (!dwc2_hcd_is_bandwidth_allocated(hsotg, ep))
4610 alloc_bandwidth = 1;
4611 spin_unlock_irqrestore(&hsotg->lock, flags);
4614 switch (usb_pipetype(urb->pipe)) {
4616 ep_type = USB_ENDPOINT_XFER_CONTROL;
4618 case PIPE_ISOCHRONOUS:
4619 ep_type = USB_ENDPOINT_XFER_ISOC;
4622 ep_type = USB_ENDPOINT_XFER_BULK;
4624 case PIPE_INTERRUPT:
4625 ep_type = USB_ENDPOINT_XFER_INT;
4629 dwc2_urb = dwc2_hcd_urb_alloc(hsotg, urb->number_of_packets,
4634 dwc2_hcd_urb_set_pipeinfo(hsotg, dwc2_urb, usb_pipedevice(urb->pipe),
4635 usb_pipeendpoint(urb->pipe), ep_type,
4636 usb_pipein(urb->pipe),
4637 usb_maxpacket(urb->dev, urb->pipe,
4638 !(usb_pipein(urb->pipe))));
4640 buf = urb->transfer_buffer;
4642 if (hcd->self.uses_dma) {
4643 if (!buf && (urb->transfer_dma & 3)) {
4645 "%s: unaligned transfer with no transfer_buffer",
4652 if (!(urb->transfer_flags & URB_NO_INTERRUPT))
4653 tflags |= URB_GIVEBACK_ASAP;
4654 if (urb->transfer_flags & URB_ZERO_PACKET)
4655 tflags |= URB_SEND_ZERO_PACKET;
4657 dwc2_urb->priv = urb;
4658 dwc2_urb->buf = buf;
4659 dwc2_urb->dma = urb->transfer_dma;
4660 dwc2_urb->length = urb->transfer_buffer_length;
4661 dwc2_urb->setup_packet = urb->setup_packet;
4662 dwc2_urb->setup_dma = urb->setup_dma;
4663 dwc2_urb->flags = tflags;
4664 dwc2_urb->interval = urb->interval;
4665 dwc2_urb->status = -EINPROGRESS;
4667 for (i = 0; i < urb->number_of_packets; ++i)
4668 dwc2_hcd_urb_set_iso_desc_params(dwc2_urb, i,
4669 urb->iso_frame_desc[i].offset,
4670 urb->iso_frame_desc[i].length);
4672 urb->hcpriv = dwc2_urb;
4673 qh = (struct dwc2_qh *)ep->hcpriv;
4674 /* Create QH for the endpoint if it doesn't exist */
4676 qh = dwc2_hcd_qh_create(hsotg, dwc2_urb, mem_flags);
4682 qh_allocated = true;
4685 qtd = kzalloc(sizeof(*qtd), mem_flags);
4691 spin_lock_irqsave(&hsotg->lock, flags);
4692 retval = usb_hcd_link_urb_to_ep(hcd, urb);
4696 retval = dwc2_hcd_urb_enqueue(hsotg, dwc2_urb, qh, qtd);
4700 if (alloc_bandwidth) {
4701 dwc2_allocate_bus_bandwidth(hcd,
4702 dwc2_hcd_get_ep_bandwidth(hsotg, ep),
4706 spin_unlock_irqrestore(&hsotg->lock, flags);
4711 dwc2_urb->priv = NULL;
4712 usb_hcd_unlink_urb_from_ep(hcd, urb);
4713 if (qh_allocated && qh->channel && qh->channel->qh == qh)
4714 qh->channel->qh = NULL;
4716 spin_unlock_irqrestore(&hsotg->lock, flags);
4722 struct dwc2_qtd *qtd2, *qtd2_tmp;
4725 dwc2_hcd_qh_unlink(hsotg, qh);
4726 /* Free each QTD in the QH's QTD list */
4727 list_for_each_entry_safe(qtd2, qtd2_tmp, &qh->qtd_list,
4729 dwc2_hcd_qtd_unlink_and_free(hsotg, qtd2, qh);
4730 dwc2_hcd_qh_free(hsotg, qh);
4739 * Aborts/cancels a USB transfer request. Always returns 0 to indicate success.
4741 static int _dwc2_hcd_urb_dequeue(struct usb_hcd *hcd, struct urb *urb,
4744 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4746 unsigned long flags;
4748 dev_dbg(hsotg->dev, "DWC OTG HCD URB Dequeue\n");
4749 dwc2_dump_urb_info(hcd, urb, "urb_dequeue");
4751 spin_lock_irqsave(&hsotg->lock, flags);
4753 rc = usb_hcd_check_unlink_urb(hcd, urb, status);
4758 dev_dbg(hsotg->dev, "## urb->hcpriv is NULL ##\n");
4762 rc = dwc2_hcd_urb_dequeue(hsotg, urb->hcpriv);
4764 usb_hcd_unlink_urb_from_ep(hcd, urb);
4769 /* Higher layer software sets URB status */
4770 spin_unlock(&hsotg->lock);
4771 usb_hcd_giveback_urb(hcd, urb, status);
4772 spin_lock(&hsotg->lock);
4774 dev_dbg(hsotg->dev, "Called usb_hcd_giveback_urb()\n");
4775 dev_dbg(hsotg->dev, " urb->status = %d\n", urb->status);
4777 spin_unlock_irqrestore(&hsotg->lock, flags);
4783 * Frees resources in the DWC_otg controller related to a given endpoint. Also
4784 * clears state in the HCD related to the endpoint. Any URBs for the endpoint
4785 * must already be dequeued.
4787 static void _dwc2_hcd_endpoint_disable(struct usb_hcd *hcd,
4788 struct usb_host_endpoint *ep)
4790 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4793 "DWC OTG HCD EP DISABLE: bEndpointAddress=0x%02x, ep->hcpriv=%p\n",
4794 ep->desc.bEndpointAddress, ep->hcpriv);
4795 dwc2_hcd_endpoint_disable(hsotg, ep, 250);
4799 * Resets endpoint specific parameter values, in current version used to reset
4800 * the data toggle (as a WA). This function can be called from usb_clear_halt
4803 static void _dwc2_hcd_endpoint_reset(struct usb_hcd *hcd,
4804 struct usb_host_endpoint *ep)
4806 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4807 unsigned long flags;
4810 "DWC OTG HCD EP RESET: bEndpointAddress=0x%02x\n",
4811 ep->desc.bEndpointAddress);
4813 spin_lock_irqsave(&hsotg->lock, flags);
4814 dwc2_hcd_endpoint_reset(hsotg, ep);
4815 spin_unlock_irqrestore(&hsotg->lock, flags);
4819 * Handles host mode interrupts for the DWC_otg controller. Returns IRQ_NONE if
4820 * there was no interrupt to handle. Returns IRQ_HANDLED if there was a valid
4823 * This function is called by the USB core when an interrupt occurs
4825 static irqreturn_t _dwc2_hcd_irq(struct usb_hcd *hcd)
4827 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4829 return dwc2_handle_hcd_intr(hsotg);
4833 * Creates Status Change bitmap for the root hub and root port. The bitmap is
4834 * returned in buf. Bit 0 is the status change indicator for the root hub. Bit 1
4835 * is the status change indicator for the single root port. Returns 1 if either
4836 * change indicator is 1, otherwise returns 0.
4838 static int _dwc2_hcd_hub_status_data(struct usb_hcd *hcd, char *buf)
4840 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4842 buf[0] = dwc2_hcd_is_status_changed(hsotg, 1) << 1;
4846 /* Handles hub class-specific requests */
4847 static int _dwc2_hcd_hub_control(struct usb_hcd *hcd, u16 typereq, u16 wvalue,
4848 u16 windex, char *buf, u16 wlength)
4850 int retval = dwc2_hcd_hub_control(dwc2_hcd_to_hsotg(hcd), typereq,
4851 wvalue, windex, buf, wlength);
4855 /* Handles hub TT buffer clear completions */
4856 static void _dwc2_hcd_clear_tt_buffer_complete(struct usb_hcd *hcd,
4857 struct usb_host_endpoint *ep)
4859 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4861 unsigned long flags;
4867 spin_lock_irqsave(&hsotg->lock, flags);
4868 qh->tt_buffer_dirty = 0;
4870 if (hsotg->flags.b.port_connect_status)
4871 dwc2_hcd_queue_transactions(hsotg, DWC2_TRANSACTION_ALL);
4873 spin_unlock_irqrestore(&hsotg->lock, flags);
4877 * HPRT0_SPD_HIGH_SPEED: high speed
4878 * HPRT0_SPD_FULL_SPEED: full speed
4880 static void dwc2_change_bus_speed(struct usb_hcd *hcd, int speed)
4882 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4884 if (hsotg->params.speed == speed)
4887 hsotg->params.speed = speed;
4888 queue_work(hsotg->wq_otg, &hsotg->wf_otg);
4891 static void dwc2_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
4893 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4895 if (!hsotg->params.change_speed_quirk)
4899 * On removal, set speed to default high-speed.
4901 if (udev->parent && udev->parent->speed > USB_SPEED_UNKNOWN &&
4902 udev->parent->speed < USB_SPEED_HIGH) {
4903 dev_info(hsotg->dev, "Set speed to default high-speed\n");
4904 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4908 static int dwc2_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
4910 struct dwc2_hsotg *hsotg = dwc2_hcd_to_hsotg(hcd);
4912 if (!hsotg->params.change_speed_quirk)
4915 if (udev->speed == USB_SPEED_HIGH) {
4916 dev_info(hsotg->dev, "Set speed to high-speed\n");
4917 dwc2_change_bus_speed(hcd, HPRT0_SPD_HIGH_SPEED);
4918 } else if ((udev->speed == USB_SPEED_FULL ||
4919 udev->speed == USB_SPEED_LOW)) {
4921 * Change speed setting to full-speed if there's
4922 * a full-speed or low-speed device plugged in.
4924 dev_info(hsotg->dev, "Set speed to full-speed\n");
4925 dwc2_change_bus_speed(hcd, HPRT0_SPD_FULL_SPEED);
4931 static struct hc_driver dwc2_hc_driver = {
4932 .description = "dwc2_hsotg",
4933 .product_desc = "DWC OTG Controller",
4934 .hcd_priv_size = sizeof(struct wrapper_priv_data),
4936 .irq = _dwc2_hcd_irq,
4937 .flags = HCD_MEMORY | HCD_USB2 | HCD_BH,
4939 .start = _dwc2_hcd_start,
4940 .stop = _dwc2_hcd_stop,
4941 .urb_enqueue = _dwc2_hcd_urb_enqueue,
4942 .urb_dequeue = _dwc2_hcd_urb_dequeue,
4943 .endpoint_disable = _dwc2_hcd_endpoint_disable,
4944 .endpoint_reset = _dwc2_hcd_endpoint_reset,
4945 .get_frame_number = _dwc2_hcd_get_frame_number,
4947 .hub_status_data = _dwc2_hcd_hub_status_data,
4948 .hub_control = _dwc2_hcd_hub_control,
4949 .clear_tt_buffer_complete = _dwc2_hcd_clear_tt_buffer_complete,
4951 .bus_suspend = _dwc2_hcd_suspend,
4952 .bus_resume = _dwc2_hcd_resume,
4954 .map_urb_for_dma = dwc2_map_urb_for_dma,
4955 .unmap_urb_for_dma = dwc2_unmap_urb_for_dma,
4959 * Frees secondary storage associated with the dwc2_hsotg structure contained
4960 * in the struct usb_hcd field
4962 static void dwc2_hcd_free(struct dwc2_hsotg *hsotg)
4968 dev_dbg(hsotg->dev, "DWC OTG HCD FREE\n");
4970 /* Free memory for QH/QTD lists */
4971 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_inactive);
4972 dwc2_qh_list_free(hsotg, &hsotg->non_periodic_sched_active);
4973 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_inactive);
4974 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_ready);
4975 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_assigned);
4976 dwc2_qh_list_free(hsotg, &hsotg->periodic_sched_queued);
4978 /* Free memory for the host channels */
4979 for (i = 0; i < MAX_EPS_CHANNELS; i++) {
4980 struct dwc2_host_chan *chan = hsotg->hc_ptr_array[i];
4983 dev_dbg(hsotg->dev, "HCD Free channel #%i, chan=%p\n",
4985 hsotg->hc_ptr_array[i] = NULL;
4990 if (hsotg->params.host_dma) {
4991 if (hsotg->status_buf) {
4992 dma_free_coherent(hsotg->dev, DWC2_HCD_STATUS_BUF_SIZE,
4994 hsotg->status_buf_dma);
4995 hsotg->status_buf = NULL;
4998 kfree(hsotg->status_buf);
4999 hsotg->status_buf = NULL;
5002 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
5004 /* Disable all interrupts */
5005 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
5006 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
5007 dwc2_writel(0, hsotg->regs + GINTMSK);
5009 if (hsotg->hw_params.snpsid >= DWC2_CORE_REV_3_00a) {
5010 dctl = dwc2_readl(hsotg->regs + DCTL);
5011 dctl |= DCTL_SFTDISCON;
5012 dwc2_writel(dctl, hsotg->regs + DCTL);
5015 if (hsotg->wq_otg) {
5016 if (!cancel_work_sync(&hsotg->wf_otg))
5017 flush_workqueue(hsotg->wq_otg);
5018 destroy_workqueue(hsotg->wq_otg);
5021 del_timer(&hsotg->wkp_timer);
5024 static void dwc2_hcd_release(struct dwc2_hsotg *hsotg)
5026 /* Turn off all host-specific interrupts */
5027 dwc2_disable_host_interrupts(hsotg);
5029 dwc2_hcd_free(hsotg);
5033 * Initializes the HCD. This function allocates memory for and initializes the
5034 * static parts of the usb_hcd and dwc2_hsotg structures. It also registers the
5035 * USB bus with the core and calls the hc_driver->start() function. It returns
5036 * a negative error on failure.
5038 int dwc2_hcd_init(struct dwc2_hsotg *hsotg)
5040 struct platform_device *pdev = to_platform_device(hsotg->dev);
5041 struct resource *res;
5042 struct usb_hcd *hcd;
5043 struct dwc2_host_chan *channel;
5045 int i, num_channels;
5051 dev_dbg(hsotg->dev, "DWC OTG HCD INIT\n");
5055 hcfg = dwc2_readl(hsotg->regs + HCFG);
5056 dev_dbg(hsotg->dev, "hcfg=%08x\n", hcfg);
5058 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5059 hsotg->frame_num_array = kzalloc(sizeof(*hsotg->frame_num_array) *
5060 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5061 if (!hsotg->frame_num_array)
5063 hsotg->last_frame_num_array = kzalloc(
5064 sizeof(*hsotg->last_frame_num_array) *
5065 FRAME_NUM_ARRAY_SIZE, GFP_KERNEL);
5066 if (!hsotg->last_frame_num_array)
5069 hsotg->last_frame_num = HFNUM_MAX_FRNUM;
5071 /* Check if the bus driver or platform code has setup a dma_mask */
5072 if (hsotg->params.host_dma &&
5073 !hsotg->dev->dma_mask) {
5074 dev_warn(hsotg->dev,
5075 "dma_mask not set, disabling DMA\n");
5076 hsotg->params.host_dma = false;
5077 hsotg->params.dma_desc_enable = false;
5080 /* Set device flags indicating whether the HCD supports DMA */
5081 if (hsotg->params.host_dma) {
5082 if (dma_set_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5083 dev_warn(hsotg->dev, "can't set DMA mask\n");
5084 if (dma_set_coherent_mask(hsotg->dev, DMA_BIT_MASK(32)) < 0)
5085 dev_warn(hsotg->dev, "can't set coherent DMA mask\n");
5088 if (hsotg->params.change_speed_quirk) {
5089 dwc2_hc_driver.free_dev = dwc2_free_dev;
5090 dwc2_hc_driver.reset_device = dwc2_reset_device;
5093 hcd = usb_create_hcd(&dwc2_hc_driver, hsotg->dev, dev_name(hsotg->dev));
5097 if (!hsotg->params.host_dma)
5098 hcd->self.uses_dma = 0;
5102 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
5103 hcd->rsrc_start = res->start;
5104 hcd->rsrc_len = resource_size(res);
5106 ((struct wrapper_priv_data *)&hcd->hcd_priv)->hsotg = hsotg;
5110 * Disable the global interrupt until all the interrupt handlers are
5113 dwc2_disable_global_interrupts(hsotg);
5115 /* Initialize the DWC_otg core, and select the Phy type */
5116 retval = dwc2_core_init(hsotg, true);
5120 /* Create new workqueue and init work */
5122 hsotg->wq_otg = alloc_ordered_workqueue("dwc2", 0);
5123 if (!hsotg->wq_otg) {
5124 dev_err(hsotg->dev, "Failed to create workqueue\n");
5127 INIT_WORK(&hsotg->wf_otg, dwc2_conn_id_status_change);
5129 setup_timer(&hsotg->wkp_timer, dwc2_wakeup_detected,
5130 (unsigned long)hsotg);
5132 /* Initialize the non-periodic schedule */
5133 INIT_LIST_HEAD(&hsotg->non_periodic_sched_inactive);
5134 INIT_LIST_HEAD(&hsotg->non_periodic_sched_active);
5136 /* Initialize the periodic schedule */
5137 INIT_LIST_HEAD(&hsotg->periodic_sched_inactive);
5138 INIT_LIST_HEAD(&hsotg->periodic_sched_ready);
5139 INIT_LIST_HEAD(&hsotg->periodic_sched_assigned);
5140 INIT_LIST_HEAD(&hsotg->periodic_sched_queued);
5142 INIT_LIST_HEAD(&hsotg->split_order);
5145 * Create a host channel descriptor for each host channel implemented
5146 * in the controller. Initialize the channel descriptor array.
5148 INIT_LIST_HEAD(&hsotg->free_hc_list);
5149 num_channels = hsotg->params.host_channels;
5150 memset(&hsotg->hc_ptr_array[0], 0, sizeof(hsotg->hc_ptr_array));
5152 for (i = 0; i < num_channels; i++) {
5153 channel = kzalloc(sizeof(*channel), GFP_KERNEL);
5156 channel->hc_num = i;
5157 INIT_LIST_HEAD(&channel->split_order_list_entry);
5158 hsotg->hc_ptr_array[i] = channel;
5161 /* Initialize hsotg start work */
5162 INIT_DELAYED_WORK(&hsotg->start_work, dwc2_hcd_start_func);
5164 /* Initialize port reset work */
5165 INIT_DELAYED_WORK(&hsotg->reset_work, dwc2_hcd_reset_func);
5168 * Allocate space for storing data on status transactions. Normally no
5169 * data is sent, but this space acts as a bit bucket. This must be
5170 * done after usb_add_hcd since that function allocates the DMA buffer
5173 if (hsotg->params.host_dma)
5174 hsotg->status_buf = dma_alloc_coherent(hsotg->dev,
5175 DWC2_HCD_STATUS_BUF_SIZE,
5176 &hsotg->status_buf_dma, GFP_KERNEL);
5178 hsotg->status_buf = kzalloc(DWC2_HCD_STATUS_BUF_SIZE,
5181 if (!hsotg->status_buf)
5185 * Create kmem caches to handle descriptor buffers in descriptor
5187 * Alignment must be set to 512 bytes.
5189 if (hsotg->params.dma_desc_enable ||
5190 hsotg->params.dma_desc_fs_enable) {
5191 hsotg->desc_gen_cache = kmem_cache_create("dwc2-gen-desc",
5192 sizeof(struct dwc2_dma_desc) *
5193 MAX_DMA_DESC_NUM_GENERIC, 512, SLAB_CACHE_DMA,
5195 if (!hsotg->desc_gen_cache) {
5197 "unable to create dwc2 generic desc cache\n");
5200 * Disable descriptor dma mode since it will not be
5203 hsotg->params.dma_desc_enable = false;
5204 hsotg->params.dma_desc_fs_enable = false;
5207 hsotg->desc_hsisoc_cache = kmem_cache_create("dwc2-hsisoc-desc",
5208 sizeof(struct dwc2_dma_desc) *
5209 MAX_DMA_DESC_NUM_HS_ISOC, 512, 0, NULL);
5210 if (!hsotg->desc_hsisoc_cache) {
5212 "unable to create dwc2 hs isoc desc cache\n");
5214 kmem_cache_destroy(hsotg->desc_gen_cache);
5217 * Disable descriptor dma mode since it will not be
5220 hsotg->params.dma_desc_enable = false;
5221 hsotg->params.dma_desc_fs_enable = false;
5225 hsotg->otg_port = 1;
5226 hsotg->frame_list = NULL;
5227 hsotg->frame_list_dma = 0;
5228 hsotg->periodic_qh_count = 0;
5230 /* Initiate lx_state to L3 disconnected state */
5231 hsotg->lx_state = DWC2_L3;
5233 hcd->self.otg_port = hsotg->otg_port;
5235 /* Don't support SG list at this point */
5236 hcd->self.sg_tablesize = 0;
5238 if (!IS_ERR_OR_NULL(hsotg->uphy))
5239 otg_set_host(hsotg->uphy->otg, &hcd->self);
5242 * Finish generic HCD initialization and start the HCD. This function
5243 * allocates the DMA buffer pool, registers the USB bus, requests the
5244 * IRQ line, and calls hcd_start method.
5246 retval = usb_add_hcd(hcd, hsotg->irq, IRQF_SHARED);
5250 device_wakeup_enable(hcd->self.controller);
5252 dwc2_hcd_dump_state(hsotg);
5254 dwc2_enable_global_interrupts(hsotg);
5259 kmem_cache_destroy(hsotg->desc_gen_cache);
5260 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5262 dwc2_hcd_release(hsotg);
5267 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5268 kfree(hsotg->last_frame_num_array);
5269 kfree(hsotg->frame_num_array);
5272 dev_err(hsotg->dev, "%s() FAILED, returning %d\n", __func__, retval);
5278 * Frees memory and resources associated with the HCD and deregisters the bus.
5280 void dwc2_hcd_remove(struct dwc2_hsotg *hsotg)
5282 struct usb_hcd *hcd;
5284 dev_dbg(hsotg->dev, "DWC OTG HCD REMOVE\n");
5286 hcd = dwc2_hsotg_to_hcd(hsotg);
5287 dev_dbg(hsotg->dev, "hsotg->hcd = %p\n", hcd);
5290 dev_dbg(hsotg->dev, "%s: dwc2_hsotg_to_hcd(hsotg) NULL!\n",
5295 if (!IS_ERR_OR_NULL(hsotg->uphy))
5296 otg_set_host(hsotg->uphy->otg, NULL);
5298 usb_remove_hcd(hcd);
5301 kmem_cache_destroy(hsotg->desc_gen_cache);
5302 kmem_cache_destroy(hsotg->desc_hsisoc_cache);
5304 dwc2_hcd_release(hsotg);
5307 #ifdef CONFIG_USB_DWC2_TRACK_MISSED_SOFS
5308 kfree(hsotg->last_frame_num_array);
5309 kfree(hsotg->frame_num_array);
5314 * dwc2_backup_host_registers() - Backup controller host registers.
5315 * When suspending usb bus, registers needs to be backuped
5316 * if controller power is disabled once suspended.
5318 * @hsotg: Programming view of the DWC_otg controller
5320 int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
5322 struct dwc2_hregs_backup *hr;
5325 dev_dbg(hsotg->dev, "%s\n", __func__);
5327 /* Backup Host regs */
5328 hr = &hsotg->hr_backup;
5329 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
5330 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
5331 for (i = 0; i < hsotg->params.host_channels; ++i)
5332 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
5334 hr->hprt0 = dwc2_read_hprt0(hsotg);
5335 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
5342 * dwc2_restore_host_registers() - Restore controller host registers.
5343 * When resuming usb bus, device registers needs to be restored
5344 * if controller power were disabled.
5346 * @hsotg: Programming view of the DWC_otg controller
5348 int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
5350 struct dwc2_hregs_backup *hr;
5353 dev_dbg(hsotg->dev, "%s\n", __func__);
5355 /* Restore host regs */
5356 hr = &hsotg->hr_backup;
5358 dev_err(hsotg->dev, "%s: no host registers to restore\n",
5364 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
5365 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
5367 for (i = 0; i < hsotg->params.host_channels; ++i)
5368 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
5370 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
5371 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
5372 hsotg->frame_number = 0;