2 * core.c - DesignWare HS OTG Controller common 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 * The Core code provides basic services for accessing and managing the
39 * DWC_otg hardware. These services are used by both the Host Controller
40 * Driver and the Peripheral Controller Driver.
42 #include <linux/kernel.h>
43 #include <linux/module.h>
44 #include <linux/moduleparam.h>
45 #include <linux/spinlock.h>
46 #include <linux/interrupt.h>
47 #include <linux/dma-mapping.h>
48 #include <linux/delay.h>
50 #include <linux/slab.h>
51 #include <linux/usb.h>
53 #include <linux/usb/hcd.h>
54 #include <linux/usb/ch11.h>
60 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
61 * used in both device and host modes
63 * @hsotg: Programming view of the DWC_otg controller
65 static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
69 /* Clear any pending OTG Interrupts */
70 writel(0xffffffff, hsotg->regs + GOTGINT);
72 /* Clear any pending interrupts */
73 writel(0xffffffff, hsotg->regs + GINTSTS);
75 /* Enable the interrupts in the GINTMSK */
76 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
78 if (hsotg->core_params->dma_enable <= 0)
79 intmsk |= GINTSTS_RXFLVL;
81 intmsk |= GINTSTS_CONIDSTSCHNG | GINTSTS_WKUPINT | GINTSTS_USBSUSP |
84 writel(intmsk, hsotg->regs + GINTMSK);
88 * Initializes the FSLSPClkSel field of the HCFG register depending on the
91 static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
95 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
96 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
97 hsotg->core_params->ulpi_fs_ls > 0) ||
98 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
100 val = HCFG_FSLSPCLKSEL_48_MHZ;
102 /* High speed PHY running at full speed or high speed */
103 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
106 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
107 hcfg = readl(hsotg->regs + HCFG);
108 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
109 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
110 writel(hcfg, hsotg->regs + HCFG);
114 * Do core a soft reset of the core. Be careful with this because it
115 * resets all the internal state machines of the core.
117 static int dwc2_core_reset(struct dwc2_hsotg *hsotg)
122 dev_vdbg(hsotg->dev, "%s()\n", __func__);
124 /* Wait for AHB master IDLE state */
126 usleep_range(20000, 40000);
127 greset = readl(hsotg->regs + GRSTCTL);
130 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
134 } while (!(greset & GRSTCTL_AHBIDLE));
136 /* Core Soft Reset */
138 greset |= GRSTCTL_CSFTRST;
139 writel(greset, hsotg->regs + GRSTCTL);
141 usleep_range(20000, 40000);
142 greset = readl(hsotg->regs + GRSTCTL);
145 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
149 } while (greset & GRSTCTL_CSFTRST);
152 * NOTE: This long sleep is _very_ important, otherwise the core will
153 * not stay in host mode after a connector ID change!
155 usleep_range(150000, 200000);
160 static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
166 * core_init() is now called on every switch so only call the
167 * following for the first time through
170 dev_dbg(hsotg->dev, "FS PHY selected\n");
171 usbcfg = readl(hsotg->regs + GUSBCFG);
172 usbcfg |= GUSBCFG_PHYSEL;
173 writel(usbcfg, hsotg->regs + GUSBCFG);
175 /* Reset after a PHY select */
176 retval = dwc2_core_reset(hsotg);
178 dev_err(hsotg->dev, "%s() Reset failed, aborting",
185 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
186 * do this on HNP Dev/Host mode switches (done in dev_init and
189 if (dwc2_is_host_mode(hsotg))
190 dwc2_init_fs_ls_pclk_sel(hsotg);
192 if (hsotg->core_params->i2c_enable > 0) {
193 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
195 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
196 usbcfg = readl(hsotg->regs + GUSBCFG);
197 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
198 writel(usbcfg, hsotg->regs + GUSBCFG);
200 /* Program GI2CCTL.I2CEn */
201 i2cctl = readl(hsotg->regs + GI2CCTL);
202 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
203 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
204 i2cctl &= ~GI2CCTL_I2CEN;
205 writel(i2cctl, hsotg->regs + GI2CCTL);
206 i2cctl |= GI2CCTL_I2CEN;
207 writel(i2cctl, hsotg->regs + GI2CCTL);
213 static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
221 usbcfg = readl(hsotg->regs + GUSBCFG);
224 * HS PHY parameters. These parameters are preserved during soft reset
225 * so only program the first time. Do a soft reset immediately after
228 switch (hsotg->core_params->phy_type) {
229 case DWC2_PHY_TYPE_PARAM_ULPI:
231 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
232 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
233 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
234 if (hsotg->core_params->phy_ulpi_ddr > 0)
235 usbcfg |= GUSBCFG_DDRSEL;
237 case DWC2_PHY_TYPE_PARAM_UTMI:
238 /* UTMI+ interface */
239 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
240 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
241 if (hsotg->core_params->phy_utmi_width == 16)
242 usbcfg |= GUSBCFG_PHYIF16;
245 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
249 writel(usbcfg, hsotg->regs + GUSBCFG);
251 /* Reset after setting the PHY parameters */
252 retval = dwc2_core_reset(hsotg);
254 dev_err(hsotg->dev, "%s() Reset failed, aborting",
262 static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
267 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
268 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
269 /* If FS mode with FS PHY */
270 retval = dwc2_fs_phy_init(hsotg, select_phy);
275 retval = dwc2_hs_phy_init(hsotg, select_phy);
280 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
281 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
282 hsotg->core_params->ulpi_fs_ls > 0) {
283 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
284 usbcfg = readl(hsotg->regs + GUSBCFG);
285 usbcfg |= GUSBCFG_ULPI_FS_LS;
286 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
287 writel(usbcfg, hsotg->regs + GUSBCFG);
289 usbcfg = readl(hsotg->regs + GUSBCFG);
290 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
291 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
292 writel(usbcfg, hsotg->regs + GUSBCFG);
298 static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
300 u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
302 switch (hsotg->hw_params.arch) {
303 case GHWCFG2_EXT_DMA_ARCH:
304 dev_err(hsotg->dev, "External DMA Mode not supported\n");
307 case GHWCFG2_INT_DMA_ARCH:
308 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
309 if (hsotg->core_params->ahbcfg != -1) {
310 ahbcfg &= GAHBCFG_CTRL_MASK;
311 ahbcfg |= hsotg->core_params->ahbcfg &
316 case GHWCFG2_SLAVE_ONLY_ARCH:
318 dev_dbg(hsotg->dev, "Slave Only Mode\n");
322 dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
323 hsotg->core_params->dma_enable,
324 hsotg->core_params->dma_desc_enable);
326 if (hsotg->core_params->dma_enable > 0) {
327 if (hsotg->core_params->dma_desc_enable > 0)
328 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
330 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
332 dev_dbg(hsotg->dev, "Using Slave mode\n");
333 hsotg->core_params->dma_desc_enable = 0;
336 if (hsotg->core_params->dma_enable > 0)
337 ahbcfg |= GAHBCFG_DMA_EN;
339 writel(ahbcfg, hsotg->regs + GAHBCFG);
344 static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
348 usbcfg = readl(hsotg->regs + GUSBCFG);
349 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
351 switch (hsotg->hw_params.op_mode) {
352 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
353 if (hsotg->core_params->otg_cap ==
354 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
355 usbcfg |= GUSBCFG_HNPCAP;
356 if (hsotg->core_params->otg_cap !=
357 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
358 usbcfg |= GUSBCFG_SRPCAP;
361 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
362 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
363 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
364 if (hsotg->core_params->otg_cap !=
365 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
366 usbcfg |= GUSBCFG_SRPCAP;
369 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
370 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
371 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
376 writel(usbcfg, hsotg->regs + GUSBCFG);
380 * dwc2_core_init() - Initializes the DWC_otg controller registers and
381 * prepares the core for device mode or host mode operation
383 * @hsotg: Programming view of the DWC_otg controller
384 * @select_phy: If true then also set the Phy type
385 * @irq: If >= 0, the irq to register
387 int dwc2_core_init(struct dwc2_hsotg *hsotg, bool select_phy, int irq)
392 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
394 usbcfg = readl(hsotg->regs + GUSBCFG);
396 /* Set ULPI External VBUS bit if needed */
397 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
398 if (hsotg->core_params->phy_ulpi_ext_vbus ==
399 DWC2_PHY_ULPI_EXTERNAL_VBUS)
400 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
402 /* Set external TS Dline pulsing bit if needed */
403 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
404 if (hsotg->core_params->ts_dline > 0)
405 usbcfg |= GUSBCFG_TERMSELDLPULSE;
407 writel(usbcfg, hsotg->regs + GUSBCFG);
409 /* Reset the Controller */
410 retval = dwc2_core_reset(hsotg);
412 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
418 * This needs to happen in FS mode before any other programming occurs
420 retval = dwc2_phy_init(hsotg, select_phy);
424 /* Program the GAHBCFG Register */
425 retval = dwc2_gahbcfg_init(hsotg);
429 /* Program the GUSBCFG register */
430 dwc2_gusbcfg_init(hsotg);
432 /* Program the GOTGCTL register */
433 otgctl = readl(hsotg->regs + GOTGCTL);
434 otgctl &= ~GOTGCTL_OTGVER;
435 if (hsotg->core_params->otg_ver > 0)
436 otgctl |= GOTGCTL_OTGVER;
437 writel(otgctl, hsotg->regs + GOTGCTL);
438 dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
440 /* Clear the SRP success bit for FS-I2c */
441 hsotg->srp_success = 0;
444 dev_dbg(hsotg->dev, "registering common handler for irq%d\n",
446 retval = devm_request_irq(hsotg->dev, irq,
447 dwc2_handle_common_intr, IRQF_SHARED,
448 dev_name(hsotg->dev), hsotg);
453 /* Enable common interrupts */
454 dwc2_enable_common_interrupts(hsotg);
457 * Do device or host intialization based on mode during PCD and
460 if (dwc2_is_host_mode(hsotg)) {
461 dev_dbg(hsotg->dev, "Host Mode\n");
462 hsotg->op_state = OTG_STATE_A_HOST;
464 dev_dbg(hsotg->dev, "Device Mode\n");
465 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
472 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
474 * @hsotg: Programming view of DWC_otg controller
476 void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
480 dev_dbg(hsotg->dev, "%s()\n", __func__);
482 /* Disable all interrupts */
483 writel(0, hsotg->regs + GINTMSK);
484 writel(0, hsotg->regs + HAINTMSK);
486 /* Enable the common interrupts */
487 dwc2_enable_common_interrupts(hsotg);
489 /* Enable host mode interrupts without disturbing common interrupts */
490 intmsk = readl(hsotg->regs + GINTMSK);
491 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
492 writel(intmsk, hsotg->regs + GINTMSK);
496 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
498 * @hsotg: Programming view of DWC_otg controller
500 void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
502 u32 intmsk = readl(hsotg->regs + GINTMSK);
504 /* Disable host mode interrupts without disturbing common interrupts */
505 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
506 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP);
507 writel(intmsk, hsotg->regs + GINTMSK);
511 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
512 * For system that have a total fifo depth that is smaller than the default
515 * @hsotg: Programming view of DWC_otg controller
517 static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
519 struct dwc2_core_params *params = hsotg->core_params;
520 struct dwc2_hw_params *hw = &hsotg->hw_params;
521 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
523 total_fifo_size = hw->total_fifo_size;
524 rxfsiz = params->host_rx_fifo_size;
525 nptxfsiz = params->host_nperio_tx_fifo_size;
526 ptxfsiz = params->host_perio_tx_fifo_size;
529 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
530 * allocation with support for high bandwidth endpoints. Synopsys
531 * defines MPS(Max Packet size) for a periodic EP=1024, and for
532 * non-periodic as 512.
534 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
536 * For Buffer DMA mode/Scatter Gather DMA mode
537 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
538 * with n = number of host channel.
539 * 2 * ((1024/4) + 2) = 516
541 rxfsiz = 516 + hw->host_channels;
544 * min non-periodic tx fifo depth
545 * 2 * (largest non-periodic USB packet used / 4)
551 * min periodic tx fifo depth
552 * (largest packet size*MC)/4
557 params->host_rx_fifo_size = rxfsiz;
558 params->host_nperio_tx_fifo_size = nptxfsiz;
559 params->host_perio_tx_fifo_size = ptxfsiz;
563 * If the summation of RX, NPTX and PTX fifo sizes is still
564 * bigger than the total_fifo_size, then we have a problem.
566 * We won't be able to allocate as many endpoints. Right now,
567 * we're just printing an error message, but ideally this FIFO
568 * allocation algorithm would be improved in the future.
570 * FIXME improve this FIFO allocation algorithm.
572 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
573 dev_err(hsotg->dev, "invalid fifo sizes\n");
576 static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
578 struct dwc2_core_params *params = hsotg->core_params;
579 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
581 if (!params->enable_dynamic_fifo)
584 dwc2_calculate_dynamic_fifo(hsotg);
587 grxfsiz = readl(hsotg->regs + GRXFSIZ);
588 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
589 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
590 grxfsiz |= params->host_rx_fifo_size <<
591 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
592 writel(grxfsiz, hsotg->regs + GRXFSIZ);
593 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n", readl(hsotg->regs + GRXFSIZ));
595 /* Non-periodic Tx FIFO */
596 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
597 readl(hsotg->regs + GNPTXFSIZ));
598 nptxfsiz = params->host_nperio_tx_fifo_size <<
599 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
600 nptxfsiz |= params->host_rx_fifo_size <<
601 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
602 writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
603 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
604 readl(hsotg->regs + GNPTXFSIZ));
606 /* Periodic Tx FIFO */
607 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
608 readl(hsotg->regs + HPTXFSIZ));
609 hptxfsiz = params->host_perio_tx_fifo_size <<
610 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
611 hptxfsiz |= (params->host_rx_fifo_size +
612 params->host_nperio_tx_fifo_size) <<
613 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
614 writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
615 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
616 readl(hsotg->regs + HPTXFSIZ));
618 if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
619 hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
621 * Global DFIFOCFG calculation for Host mode -
622 * include RxFIFO, NPTXFIFO and HPTXFIFO
624 dfifocfg = readl(hsotg->regs + GDFIFOCFG);
625 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
626 dfifocfg |= (params->host_rx_fifo_size +
627 params->host_nperio_tx_fifo_size +
628 params->host_perio_tx_fifo_size) <<
629 GDFIFOCFG_EPINFOBASE_SHIFT &
630 GDFIFOCFG_EPINFOBASE_MASK;
631 writel(dfifocfg, hsotg->regs + GDFIFOCFG);
636 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
639 * @hsotg: Programming view of DWC_otg controller
641 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
642 * request queues. Host channels are reset to ensure that they are ready for
643 * performing transfers.
645 void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
647 u32 hcfg, hfir, otgctl;
649 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
651 /* Restart the Phy Clock */
652 writel(0, hsotg->regs + PCGCTL);
654 /* Initialize Host Configuration Register */
655 dwc2_init_fs_ls_pclk_sel(hsotg);
656 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
657 hcfg = readl(hsotg->regs + HCFG);
658 hcfg |= HCFG_FSLSSUPP;
659 writel(hcfg, hsotg->regs + HCFG);
663 * This bit allows dynamic reloading of the HFIR register during
664 * runtime. This bit needs to be programmed during initial configuration
665 * and its value must not be changed during runtime.
667 if (hsotg->core_params->reload_ctl > 0) {
668 hfir = readl(hsotg->regs + HFIR);
669 hfir |= HFIR_RLDCTRL;
670 writel(hfir, hsotg->regs + HFIR);
673 if (hsotg->core_params->dma_desc_enable > 0) {
674 u32 op_mode = hsotg->hw_params.op_mode;
675 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
676 !hsotg->hw_params.dma_desc_enable ||
677 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
678 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
679 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
681 "Hardware does not support descriptor DMA mode -\n");
683 "falling back to buffer DMA mode.\n");
684 hsotg->core_params->dma_desc_enable = 0;
686 hcfg = readl(hsotg->regs + HCFG);
687 hcfg |= HCFG_DESCDMA;
688 writel(hcfg, hsotg->regs + HCFG);
692 /* Configure data FIFO sizes */
693 dwc2_config_fifos(hsotg);
695 /* TODO - check this */
696 /* Clear Host Set HNP Enable in the OTG Control Register */
697 otgctl = readl(hsotg->regs + GOTGCTL);
698 otgctl &= ~GOTGCTL_HSTSETHNPEN;
699 writel(otgctl, hsotg->regs + GOTGCTL);
701 /* Make sure the FIFOs are flushed */
702 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
703 dwc2_flush_rx_fifo(hsotg);
705 /* Clear Host Set HNP Enable in the OTG Control Register */
706 otgctl = readl(hsotg->regs + GOTGCTL);
707 otgctl &= ~GOTGCTL_HSTSETHNPEN;
708 writel(otgctl, hsotg->regs + GOTGCTL);
710 if (hsotg->core_params->dma_desc_enable <= 0) {
714 /* Flush out any leftover queued requests */
715 num_channels = hsotg->core_params->host_channels;
716 for (i = 0; i < num_channels; i++) {
717 hcchar = readl(hsotg->regs + HCCHAR(i));
718 hcchar &= ~HCCHAR_CHENA;
719 hcchar |= HCCHAR_CHDIS;
720 hcchar &= ~HCCHAR_EPDIR;
721 writel(hcchar, hsotg->regs + HCCHAR(i));
724 /* Halt all channels to put them into a known state */
725 for (i = 0; i < num_channels; i++) {
728 hcchar = readl(hsotg->regs + HCCHAR(i));
729 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
730 hcchar &= ~HCCHAR_EPDIR;
731 writel(hcchar, hsotg->regs + HCCHAR(i));
732 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
735 hcchar = readl(hsotg->regs + HCCHAR(i));
736 if (++count > 1000) {
738 "Unable to clear enable on channel %d\n",
743 } while (hcchar & HCCHAR_CHENA);
747 /* Turn on the vbus power */
748 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
749 if (hsotg->op_state == OTG_STATE_A_HOST) {
750 u32 hprt0 = dwc2_read_hprt0(hsotg);
752 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
753 !!(hprt0 & HPRT0_PWR));
754 if (!(hprt0 & HPRT0_PWR)) {
756 writel(hprt0, hsotg->regs + HPRT0);
760 dwc2_enable_host_interrupts(hsotg);
763 static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
764 struct dwc2_host_chan *chan)
766 u32 hcintmsk = HCINTMSK_CHHLTD;
768 switch (chan->ep_type) {
769 case USB_ENDPOINT_XFER_CONTROL:
770 case USB_ENDPOINT_XFER_BULK:
771 dev_vdbg(hsotg->dev, "control/bulk\n");
772 hcintmsk |= HCINTMSK_XFERCOMPL;
773 hcintmsk |= HCINTMSK_STALL;
774 hcintmsk |= HCINTMSK_XACTERR;
775 hcintmsk |= HCINTMSK_DATATGLERR;
776 if (chan->ep_is_in) {
777 hcintmsk |= HCINTMSK_BBLERR;
779 hcintmsk |= HCINTMSK_NAK;
780 hcintmsk |= HCINTMSK_NYET;
782 hcintmsk |= HCINTMSK_ACK;
785 if (chan->do_split) {
786 hcintmsk |= HCINTMSK_NAK;
787 if (chan->complete_split)
788 hcintmsk |= HCINTMSK_NYET;
790 hcintmsk |= HCINTMSK_ACK;
793 if (chan->error_state)
794 hcintmsk |= HCINTMSK_ACK;
797 case USB_ENDPOINT_XFER_INT:
799 dev_vdbg(hsotg->dev, "intr\n");
800 hcintmsk |= HCINTMSK_XFERCOMPL;
801 hcintmsk |= HCINTMSK_NAK;
802 hcintmsk |= HCINTMSK_STALL;
803 hcintmsk |= HCINTMSK_XACTERR;
804 hcintmsk |= HCINTMSK_DATATGLERR;
805 hcintmsk |= HCINTMSK_FRMOVRUN;
808 hcintmsk |= HCINTMSK_BBLERR;
809 if (chan->error_state)
810 hcintmsk |= HCINTMSK_ACK;
811 if (chan->do_split) {
812 if (chan->complete_split)
813 hcintmsk |= HCINTMSK_NYET;
815 hcintmsk |= HCINTMSK_ACK;
819 case USB_ENDPOINT_XFER_ISOC:
821 dev_vdbg(hsotg->dev, "isoc\n");
822 hcintmsk |= HCINTMSK_XFERCOMPL;
823 hcintmsk |= HCINTMSK_FRMOVRUN;
824 hcintmsk |= HCINTMSK_ACK;
826 if (chan->ep_is_in) {
827 hcintmsk |= HCINTMSK_XACTERR;
828 hcintmsk |= HCINTMSK_BBLERR;
832 dev_err(hsotg->dev, "## Unknown EP type ##\n");
836 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
838 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
841 static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
842 struct dwc2_host_chan *chan)
844 u32 hcintmsk = HCINTMSK_CHHLTD;
847 * For Descriptor DMA mode core halts the channel on AHB error.
848 * Interrupt is not required.
850 if (hsotg->core_params->dma_desc_enable <= 0) {
852 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
853 hcintmsk |= HCINTMSK_AHBERR;
856 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
857 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
858 hcintmsk |= HCINTMSK_XFERCOMPL;
861 if (chan->error_state && !chan->do_split &&
862 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
864 dev_vdbg(hsotg->dev, "setting ACK\n");
865 hcintmsk |= HCINTMSK_ACK;
866 if (chan->ep_is_in) {
867 hcintmsk |= HCINTMSK_DATATGLERR;
868 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
869 hcintmsk |= HCINTMSK_NAK;
873 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
875 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
878 static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
879 struct dwc2_host_chan *chan)
883 if (hsotg->core_params->dma_enable > 0) {
885 dev_vdbg(hsotg->dev, "DMA enabled\n");
886 dwc2_hc_enable_dma_ints(hsotg, chan);
889 dev_vdbg(hsotg->dev, "DMA disabled\n");
890 dwc2_hc_enable_slave_ints(hsotg, chan);
893 /* Enable the top level host channel interrupt */
894 intmsk = readl(hsotg->regs + HAINTMSK);
895 intmsk |= 1 << chan->hc_num;
896 writel(intmsk, hsotg->regs + HAINTMSK);
898 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
900 /* Make sure host channel interrupts are enabled */
901 intmsk = readl(hsotg->regs + GINTMSK);
902 intmsk |= GINTSTS_HCHINT;
903 writel(intmsk, hsotg->regs + GINTMSK);
905 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
909 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
910 * a specific endpoint
912 * @hsotg: Programming view of DWC_otg controller
913 * @chan: Information needed to initialize the host channel
915 * The HCCHARn register is set up with the characteristics specified in chan.
916 * Host channel interrupts that may need to be serviced while this transfer is
917 * in progress are enabled.
919 void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
921 u8 hc_num = chan->hc_num;
927 dev_vdbg(hsotg->dev, "%s()\n", __func__);
929 /* Clear old interrupt conditions for this host channel */
930 hcintmsk = 0xffffffff;
931 hcintmsk &= ~HCINTMSK_RESERVED14_31;
932 writel(hcintmsk, hsotg->regs + HCINT(hc_num));
934 /* Enable channel interrupts required for this transfer */
935 dwc2_hc_enable_ints(hsotg, chan);
938 * Program the HCCHARn register with the endpoint characteristics for
939 * the current transfer
941 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
942 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
944 hcchar |= HCCHAR_EPDIR;
945 if (chan->speed == USB_SPEED_LOW)
946 hcchar |= HCCHAR_LSPDDEV;
947 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
948 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
949 writel(hcchar, hsotg->regs + HCCHAR(hc_num));
951 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
954 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
956 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
958 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
960 dev_vdbg(hsotg->dev, " Is In: %d\n",
962 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
963 chan->speed == USB_SPEED_LOW);
964 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
966 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
970 /* Program the HCSPLT register for SPLITs */
971 if (chan->do_split) {
974 "Programming HC %d with split --> %s\n",
976 chan->complete_split ? "CSPLIT" : "SSPLIT");
977 if (chan->complete_split)
978 hcsplt |= HCSPLT_COMPSPLT;
979 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
981 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
983 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
986 dev_vdbg(hsotg->dev, " comp split %d\n",
987 chan->complete_split);
988 dev_vdbg(hsotg->dev, " xact pos %d\n",
990 dev_vdbg(hsotg->dev, " hub addr %d\n",
992 dev_vdbg(hsotg->dev, " hub port %d\n",
994 dev_vdbg(hsotg->dev, " is_in %d\n",
996 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
998 dev_vdbg(hsotg->dev, " xferlen %d\n",
1003 writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
1007 * dwc2_hc_halt() - Attempts to halt a host channel
1009 * @hsotg: Controller register interface
1010 * @chan: Host channel to halt
1011 * @halt_status: Reason for halting the channel
1013 * This function should only be called in Slave mode or to abort a transfer in
1014 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
1015 * controller halts the channel when the transfer is complete or a condition
1016 * occurs that requires application intervention.
1018 * In slave mode, checks for a free request queue entry, then sets the Channel
1019 * Enable and Channel Disable bits of the Host Channel Characteristics
1020 * register of the specified channel to intiate the halt. If there is no free
1021 * request queue entry, sets only the Channel Disable bit of the HCCHARn
1022 * register to flush requests for this channel. In the latter case, sets a
1023 * flag to indicate that the host channel needs to be halted when a request
1024 * queue slot is open.
1026 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
1027 * HCCHARn register. The controller ensures there is space in the request
1028 * queue before submitting the halt request.
1030 * Some time may elapse before the core flushes any posted requests for this
1031 * host channel and halts. The Channel Halted interrupt handler completes the
1032 * deactivation of the host channel.
1034 void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
1035 enum dwc2_halt_status halt_status)
1037 u32 nptxsts, hptxsts, hcchar;
1040 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1041 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1042 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1044 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1045 halt_status == DWC2_HC_XFER_AHB_ERR) {
1047 * Disable all channel interrupts except Ch Halted. The QTD
1048 * and QH state associated with this transfer has been cleared
1049 * (in the case of URB_DEQUEUE), so the channel needs to be
1050 * shut down carefully to prevent crashes.
1052 u32 hcintmsk = HCINTMSK_CHHLTD;
1054 dev_vdbg(hsotg->dev, "dequeue/error\n");
1055 writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1058 * Make sure no other interrupts besides halt are currently
1059 * pending. Handling another interrupt could cause a crash due
1060 * to the QTD and QH state.
1062 writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1065 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1066 * even if the channel was already halted for some other
1069 chan->halt_status = halt_status;
1071 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1072 if (!(hcchar & HCCHAR_CHENA)) {
1074 * The channel is either already halted or it hasn't
1075 * started yet. In DMA mode, the transfer may halt if
1076 * it finishes normally or a condition occurs that
1077 * requires driver intervention. Don't want to halt
1078 * the channel again. In either Slave or DMA mode,
1079 * it's possible that the transfer has been assigned
1080 * to a channel, but not started yet when an URB is
1081 * dequeued. Don't want to halt a channel that hasn't
1087 if (chan->halt_pending) {
1089 * A halt has already been issued for this channel. This might
1090 * happen when a transfer is aborted by a higher level in
1093 dev_vdbg(hsotg->dev,
1094 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1095 __func__, chan->hc_num);
1099 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1101 /* No need to set the bit in DDMA for disabling the channel */
1102 /* TODO check it everywhere channel is disabled */
1103 if (hsotg->core_params->dma_desc_enable <= 0) {
1105 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1106 hcchar |= HCCHAR_CHENA;
1109 dev_dbg(hsotg->dev, "desc DMA enabled\n");
1111 hcchar |= HCCHAR_CHDIS;
1113 if (hsotg->core_params->dma_enable <= 0) {
1115 dev_vdbg(hsotg->dev, "DMA not enabled\n");
1116 hcchar |= HCCHAR_CHENA;
1118 /* Check for space in the request queue to issue the halt */
1119 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1120 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1121 dev_vdbg(hsotg->dev, "control/bulk\n");
1122 nptxsts = readl(hsotg->regs + GNPTXSTS);
1123 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1124 dev_vdbg(hsotg->dev, "Disabling channel\n");
1125 hcchar &= ~HCCHAR_CHENA;
1129 dev_vdbg(hsotg->dev, "isoc/intr\n");
1130 hptxsts = readl(hsotg->regs + HPTXSTS);
1131 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1132 hsotg->queuing_high_bandwidth) {
1134 dev_vdbg(hsotg->dev, "Disabling channel\n");
1135 hcchar &= ~HCCHAR_CHENA;
1140 dev_vdbg(hsotg->dev, "DMA enabled\n");
1143 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1144 chan->halt_status = halt_status;
1146 if (hcchar & HCCHAR_CHENA) {
1148 dev_vdbg(hsotg->dev, "Channel enabled\n");
1149 chan->halt_pending = 1;
1150 chan->halt_on_queue = 0;
1153 dev_vdbg(hsotg->dev, "Channel disabled\n");
1154 chan->halt_on_queue = 1;
1158 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1160 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1162 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1163 chan->halt_pending);
1164 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1165 chan->halt_on_queue);
1166 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1172 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1174 * @hsotg: Programming view of DWC_otg controller
1175 * @chan: Identifies the host channel to clean up
1177 * This function is normally called after a transfer is done and the host
1178 * channel is being released
1180 void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1184 chan->xfer_started = 0;
1187 * Clear channel interrupt enables and any unhandled channel interrupt
1190 writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1191 hcintmsk = 0xffffffff;
1192 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1193 writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1197 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1198 * which frame a periodic transfer should occur
1200 * @hsotg: Programming view of DWC_otg controller
1201 * @chan: Identifies the host channel to set up and its properties
1202 * @hcchar: Current value of the HCCHAR register for the specified host channel
1204 * This function has no effect on non-periodic transfers
1206 static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1207 struct dwc2_host_chan *chan, u32 *hcchar)
1209 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1210 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1211 /* 1 if _next_ frame is odd, 0 if it's even */
1212 if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
1213 *hcchar |= HCCHAR_ODDFRM;
1217 static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1219 /* Set up the initial PID for the transfer */
1220 if (chan->speed == USB_SPEED_HIGH) {
1221 if (chan->ep_is_in) {
1222 if (chan->multi_count == 1)
1223 chan->data_pid_start = DWC2_HC_PID_DATA0;
1224 else if (chan->multi_count == 2)
1225 chan->data_pid_start = DWC2_HC_PID_DATA1;
1227 chan->data_pid_start = DWC2_HC_PID_DATA2;
1229 if (chan->multi_count == 1)
1230 chan->data_pid_start = DWC2_HC_PID_DATA0;
1232 chan->data_pid_start = DWC2_HC_PID_MDATA;
1235 chan->data_pid_start = DWC2_HC_PID_DATA0;
1240 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1243 * @hsotg: Programming view of DWC_otg controller
1244 * @chan: Information needed to initialize the host channel
1246 * This function should only be called in Slave mode. For a channel associated
1247 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1248 * associated with a periodic EP, the periodic Tx FIFO is written.
1250 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1251 * the number of bytes written to the Tx FIFO.
1253 static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1254 struct dwc2_host_chan *chan)
1257 u32 remaining_count;
1260 u32 __iomem *data_fifo;
1261 u32 *data_buf = (u32 *)chan->xfer_buf;
1264 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1266 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1268 remaining_count = chan->xfer_len - chan->xfer_count;
1269 if (remaining_count > chan->max_packet)
1270 byte_count = chan->max_packet;
1272 byte_count = remaining_count;
1274 dword_count = (byte_count + 3) / 4;
1276 if (((unsigned long)data_buf & 0x3) == 0) {
1277 /* xfer_buf is DWORD aligned */
1278 for (i = 0; i < dword_count; i++, data_buf++)
1279 writel(*data_buf, data_fifo);
1281 /* xfer_buf is not DWORD aligned */
1282 for (i = 0; i < dword_count; i++, data_buf++) {
1283 u32 data = data_buf[0] | data_buf[1] << 8 |
1284 data_buf[2] << 16 | data_buf[3] << 24;
1285 writel(data, data_fifo);
1289 chan->xfer_count += byte_count;
1290 chan->xfer_buf += byte_count;
1294 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1295 * channel and starts the transfer
1297 * @hsotg: Programming view of DWC_otg controller
1298 * @chan: Information needed to initialize the host channel. The xfer_len value
1299 * may be reduced to accommodate the max widths of the XferSize and
1300 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1301 * changed to reflect the final xfer_len value.
1303 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1304 * the caller must ensure that there is sufficient space in the request queue
1307 * For an OUT transfer in Slave mode, it loads a data packet into the
1308 * appropriate FIFO. If necessary, additional data packets are loaded in the
1311 * For an IN transfer in Slave mode, a data packet is requested. The data
1312 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1313 * additional data packets are requested in the Host ISR.
1315 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1316 * register along with a packet count of 1 and the channel is enabled. This
1317 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1318 * simply set to 0 since no data transfer occurs in this case.
1320 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1321 * all the information required to perform the subsequent data transfer. In
1322 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1323 * controller performs the entire PING protocol, then starts the data
1326 void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1327 struct dwc2_host_chan *chan)
1329 u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
1330 u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
1336 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1338 if (chan->do_ping) {
1339 if (hsotg->core_params->dma_enable <= 0) {
1341 dev_vdbg(hsotg->dev, "ping, no DMA\n");
1342 dwc2_hc_do_ping(hsotg, chan);
1343 chan->xfer_started = 1;
1347 dev_vdbg(hsotg->dev, "ping, DMA\n");
1348 hctsiz |= TSIZ_DOPNG;
1352 if (chan->do_split) {
1354 dev_vdbg(hsotg->dev, "split\n");
1357 if (chan->complete_split && !chan->ep_is_in)
1359 * For CSPLIT OUT Transfer, set the size to 0 so the
1360 * core doesn't expect any data written to the FIFO
1363 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1364 chan->xfer_len = chan->max_packet;
1365 else if (!chan->ep_is_in && chan->xfer_len > 188)
1366 chan->xfer_len = 188;
1368 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1372 dev_vdbg(hsotg->dev, "no split\n");
1374 * Ensure that the transfer length and packet count will fit
1375 * in the widths allocated for them in the HCTSIZn register
1377 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1378 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1380 * Make sure the transfer size is no larger than one
1381 * (micro)frame's worth of data. (A check was done
1382 * when the periodic transfer was accepted to ensure
1383 * that a (micro)frame's worth of data can be
1384 * programmed into a channel.)
1386 u32 max_periodic_len =
1387 chan->multi_count * chan->max_packet;
1389 if (chan->xfer_len > max_periodic_len)
1390 chan->xfer_len = max_periodic_len;
1391 } else if (chan->xfer_len > max_hc_xfer_size) {
1393 * Make sure that xfer_len is a multiple of max packet
1397 max_hc_xfer_size - chan->max_packet + 1;
1400 if (chan->xfer_len > 0) {
1401 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1403 if (num_packets > max_hc_pkt_count) {
1404 num_packets = max_hc_pkt_count;
1405 chan->xfer_len = num_packets * chan->max_packet;
1408 /* Need 1 packet for transfer length of 0 */
1414 * Always program an integral # of max packets for IN
1417 chan->xfer_len = num_packets * chan->max_packet;
1419 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1420 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1422 * Make sure that the multi_count field matches the
1423 * actual transfer length
1425 chan->multi_count = num_packets;
1427 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1428 dwc2_set_pid_isoc(chan);
1430 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1434 chan->start_pkt_count = num_packets;
1435 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1436 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1437 TSIZ_SC_MC_PID_MASK;
1438 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1440 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1441 hctsiz, chan->hc_num);
1443 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1445 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1446 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1447 TSIZ_XFERSIZE_SHIFT);
1448 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1449 (hctsiz & TSIZ_PKTCNT_MASK) >>
1451 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1452 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1453 TSIZ_SC_MC_PID_SHIFT);
1456 if (hsotg->core_params->dma_enable > 0) {
1457 dma_addr_t dma_addr;
1459 if (chan->align_buf) {
1461 dev_vdbg(hsotg->dev, "align_buf\n");
1462 dma_addr = chan->align_buf;
1464 dma_addr = chan->xfer_dma;
1466 writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
1468 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1469 (unsigned long)dma_addr, chan->hc_num);
1472 /* Start the split */
1473 if (chan->do_split) {
1474 u32 hcsplt = readl(hsotg->regs + HCSPLT(chan->hc_num));
1476 hcsplt |= HCSPLT_SPLTENA;
1477 writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1480 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1481 hcchar &= ~HCCHAR_MULTICNT_MASK;
1482 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1483 HCCHAR_MULTICNT_MASK;
1484 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1486 if (hcchar & HCCHAR_CHDIS)
1487 dev_warn(hsotg->dev,
1488 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1489 __func__, chan->hc_num, hcchar);
1491 /* Set host channel enable after all other setup is complete */
1492 hcchar |= HCCHAR_CHENA;
1493 hcchar &= ~HCCHAR_CHDIS;
1496 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1497 (hcchar & HCCHAR_MULTICNT_MASK) >>
1498 HCCHAR_MULTICNT_SHIFT);
1500 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1502 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1505 chan->xfer_started = 1;
1508 if (hsotg->core_params->dma_enable <= 0 &&
1509 !chan->ep_is_in && chan->xfer_len > 0)
1510 /* Load OUT packet into the appropriate Tx FIFO */
1511 dwc2_hc_write_packet(hsotg, chan);
1515 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1516 * host channel and starts the transfer in Descriptor DMA mode
1518 * @hsotg: Programming view of DWC_otg controller
1519 * @chan: Information needed to initialize the host channel
1521 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1522 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1523 * with micro-frame bitmap.
1525 * Initializes HCDMA register with descriptor list address and CTD value then
1526 * starts the transfer via enabling the channel.
1528 void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1529 struct dwc2_host_chan *chan)
1536 hctsiz |= TSIZ_DOPNG;
1538 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1539 dwc2_set_pid_isoc(chan);
1541 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1542 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1543 TSIZ_SC_MC_PID_MASK;
1545 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1546 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1548 /* Non-zero only for high-speed interrupt endpoints */
1549 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1552 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1554 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1555 chan->data_pid_start);
1556 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1559 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1561 hc_dma = (u32)chan->desc_list_addr & HCDMA_DMA_ADDR_MASK;
1563 /* Always start from first descriptor */
1564 hc_dma &= ~HCDMA_CTD_MASK;
1565 writel(hc_dma, hsotg->regs + HCDMA(chan->hc_num));
1567 dev_vdbg(hsotg->dev, "Wrote %08x to HCDMA(%d)\n",
1568 hc_dma, chan->hc_num);
1570 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1571 hcchar &= ~HCCHAR_MULTICNT_MASK;
1572 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1573 HCCHAR_MULTICNT_MASK;
1575 if (hcchar & HCCHAR_CHDIS)
1576 dev_warn(hsotg->dev,
1577 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1578 __func__, chan->hc_num, hcchar);
1580 /* Set host channel enable after all other setup is complete */
1581 hcchar |= HCCHAR_CHENA;
1582 hcchar &= ~HCCHAR_CHDIS;
1585 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1586 (hcchar & HCCHAR_MULTICNT_MASK) >>
1587 HCCHAR_MULTICNT_SHIFT);
1589 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1591 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1594 chan->xfer_started = 1;
1599 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
1600 * a previous call to dwc2_hc_start_transfer()
1602 * @hsotg: Programming view of DWC_otg controller
1603 * @chan: Information needed to initialize the host channel
1605 * The caller must ensure there is sufficient space in the request queue and Tx
1606 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
1607 * the controller acts autonomously to complete transfers programmed to a host
1610 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
1611 * if there is any data remaining to be queued. For an IN transfer, another
1612 * data packet is always requested. For the SETUP phase of a control transfer,
1613 * this function does nothing.
1615 * Return: 1 if a new request is queued, 0 if no more requests are required
1618 int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
1619 struct dwc2_host_chan *chan)
1622 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1626 /* SPLITs always queue just once per channel */
1629 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
1630 /* SETUPs are queued only once since they can't be NAK'd */
1633 if (chan->ep_is_in) {
1635 * Always queue another request for other IN transfers. If
1636 * back-to-back INs are issued and NAKs are received for both,
1637 * the driver may still be processing the first NAK when the
1638 * second NAK is received. When the interrupt handler clears
1639 * the NAK interrupt for the first NAK, the second NAK will
1640 * not be seen. So we can't depend on the NAK interrupt
1641 * handler to requeue a NAK'd request. Instead, IN requests
1642 * are issued each time this function is called. When the
1643 * transfer completes, the extra requests for the channel will
1646 u32 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1648 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1649 hcchar |= HCCHAR_CHENA;
1650 hcchar &= ~HCCHAR_CHDIS;
1652 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
1654 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1661 if (chan->xfer_count < chan->xfer_len) {
1662 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1663 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1664 u32 hcchar = readl(hsotg->regs +
1665 HCCHAR(chan->hc_num));
1667 dwc2_hc_set_even_odd_frame(hsotg, chan,
1671 /* Load OUT packet into the appropriate Tx FIFO */
1672 dwc2_hc_write_packet(hsotg, chan);
1681 * dwc2_hc_do_ping() - Starts a PING transfer
1683 * @hsotg: Programming view of DWC_otg controller
1684 * @chan: Information needed to initialize the host channel
1686 * This function should only be called in Slave mode. The Do Ping bit is set in
1687 * the HCTSIZ register, then the channel is enabled.
1689 void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1695 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1699 hctsiz = TSIZ_DOPNG;
1700 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
1701 writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1703 hcchar = readl(hsotg->regs + HCCHAR(chan->hc_num));
1704 hcchar |= HCCHAR_CHENA;
1705 hcchar &= ~HCCHAR_CHDIS;
1706 writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1710 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
1711 * the HFIR register according to PHY type and speed
1713 * @hsotg: Programming view of DWC_otg controller
1715 * NOTE: The caller can modify the value of the HFIR register only after the
1716 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
1719 u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
1723 int clock = 60; /* default value */
1725 usbcfg = readl(hsotg->regs + GUSBCFG);
1726 hprt0 = readl(hsotg->regs + HPRT0);
1728 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
1729 !(usbcfg & GUSBCFG_PHYIF16))
1731 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
1732 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
1734 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
1735 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
1737 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
1738 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
1740 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
1741 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
1743 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
1744 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
1746 if ((usbcfg & GUSBCFG_PHYSEL) &&
1747 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
1750 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
1751 /* High speed case */
1755 return 1000 * clock;
1759 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
1762 * @core_if: Programming view of DWC_otg controller
1763 * @dest: Destination buffer for the packet
1764 * @bytes: Number of bytes to copy to the destination
1766 void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
1768 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
1769 u32 *data_buf = (u32 *)dest;
1770 int word_count = (bytes + 3) / 4;
1774 * Todo: Account for the case where dest is not dword aligned. This
1775 * requires reading data from the FIFO into a u32 temp buffer, then
1776 * moving it into the data buffer.
1779 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
1781 for (i = 0; i < word_count; i++, data_buf++)
1782 *data_buf = readl(fifo);
1786 * dwc2_dump_host_registers() - Prints the host registers
1788 * @hsotg: Programming view of DWC_otg controller
1790 * NOTE: This function will be removed once the peripheral controller code
1791 * is integrated and the driver is stable
1793 void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
1799 dev_dbg(hsotg->dev, "Host Global Registers\n");
1800 addr = hsotg->regs + HCFG;
1801 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
1802 (unsigned long)addr, readl(addr));
1803 addr = hsotg->regs + HFIR;
1804 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
1805 (unsigned long)addr, readl(addr));
1806 addr = hsotg->regs + HFNUM;
1807 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
1808 (unsigned long)addr, readl(addr));
1809 addr = hsotg->regs + HPTXSTS;
1810 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
1811 (unsigned long)addr, readl(addr));
1812 addr = hsotg->regs + HAINT;
1813 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
1814 (unsigned long)addr, readl(addr));
1815 addr = hsotg->regs + HAINTMSK;
1816 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
1817 (unsigned long)addr, readl(addr));
1818 if (hsotg->core_params->dma_desc_enable > 0) {
1819 addr = hsotg->regs + HFLBADDR;
1820 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
1821 (unsigned long)addr, readl(addr));
1824 addr = hsotg->regs + HPRT0;
1825 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
1826 (unsigned long)addr, readl(addr));
1828 for (i = 0; i < hsotg->core_params->host_channels; i++) {
1829 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
1830 addr = hsotg->regs + HCCHAR(i);
1831 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
1832 (unsigned long)addr, readl(addr));
1833 addr = hsotg->regs + HCSPLT(i);
1834 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
1835 (unsigned long)addr, readl(addr));
1836 addr = hsotg->regs + HCINT(i);
1837 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
1838 (unsigned long)addr, readl(addr));
1839 addr = hsotg->regs + HCINTMSK(i);
1840 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
1841 (unsigned long)addr, readl(addr));
1842 addr = hsotg->regs + HCTSIZ(i);
1843 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
1844 (unsigned long)addr, readl(addr));
1845 addr = hsotg->regs + HCDMA(i);
1846 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
1847 (unsigned long)addr, readl(addr));
1848 if (hsotg->core_params->dma_desc_enable > 0) {
1849 addr = hsotg->regs + HCDMAB(i);
1850 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
1851 (unsigned long)addr, readl(addr));
1858 * dwc2_dump_global_registers() - Prints the core global registers
1860 * @hsotg: Programming view of DWC_otg controller
1862 * NOTE: This function will be removed once the peripheral controller code
1863 * is integrated and the driver is stable
1865 void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
1870 dev_dbg(hsotg->dev, "Core Global Registers\n");
1871 addr = hsotg->regs + GOTGCTL;
1872 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
1873 (unsigned long)addr, readl(addr));
1874 addr = hsotg->regs + GOTGINT;
1875 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
1876 (unsigned long)addr, readl(addr));
1877 addr = hsotg->regs + GAHBCFG;
1878 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
1879 (unsigned long)addr, readl(addr));
1880 addr = hsotg->regs + GUSBCFG;
1881 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
1882 (unsigned long)addr, readl(addr));
1883 addr = hsotg->regs + GRSTCTL;
1884 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
1885 (unsigned long)addr, readl(addr));
1886 addr = hsotg->regs + GINTSTS;
1887 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
1888 (unsigned long)addr, readl(addr));
1889 addr = hsotg->regs + GINTMSK;
1890 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
1891 (unsigned long)addr, readl(addr));
1892 addr = hsotg->regs + GRXSTSR;
1893 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
1894 (unsigned long)addr, readl(addr));
1895 addr = hsotg->regs + GRXFSIZ;
1896 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
1897 (unsigned long)addr, readl(addr));
1898 addr = hsotg->regs + GNPTXFSIZ;
1899 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
1900 (unsigned long)addr, readl(addr));
1901 addr = hsotg->regs + GNPTXSTS;
1902 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
1903 (unsigned long)addr, readl(addr));
1904 addr = hsotg->regs + GI2CCTL;
1905 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
1906 (unsigned long)addr, readl(addr));
1907 addr = hsotg->regs + GPVNDCTL;
1908 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
1909 (unsigned long)addr, readl(addr));
1910 addr = hsotg->regs + GGPIO;
1911 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
1912 (unsigned long)addr, readl(addr));
1913 addr = hsotg->regs + GUID;
1914 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
1915 (unsigned long)addr, readl(addr));
1916 addr = hsotg->regs + GSNPSID;
1917 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
1918 (unsigned long)addr, readl(addr));
1919 addr = hsotg->regs + GHWCFG1;
1920 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
1921 (unsigned long)addr, readl(addr));
1922 addr = hsotg->regs + GHWCFG2;
1923 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
1924 (unsigned long)addr, readl(addr));
1925 addr = hsotg->regs + GHWCFG3;
1926 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
1927 (unsigned long)addr, readl(addr));
1928 addr = hsotg->regs + GHWCFG4;
1929 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
1930 (unsigned long)addr, readl(addr));
1931 addr = hsotg->regs + GLPMCFG;
1932 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
1933 (unsigned long)addr, readl(addr));
1934 addr = hsotg->regs + GPWRDN;
1935 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
1936 (unsigned long)addr, readl(addr));
1937 addr = hsotg->regs + GDFIFOCFG;
1938 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
1939 (unsigned long)addr, readl(addr));
1940 addr = hsotg->regs + HPTXFSIZ;
1941 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
1942 (unsigned long)addr, readl(addr));
1944 addr = hsotg->regs + PCGCTL;
1945 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
1946 (unsigned long)addr, readl(addr));
1951 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
1953 * @hsotg: Programming view of DWC_otg controller
1954 * @num: Tx FIFO to flush
1956 void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
1961 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
1963 greset = GRSTCTL_TXFFLSH;
1964 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
1965 writel(greset, hsotg->regs + GRSTCTL);
1968 greset = readl(hsotg->regs + GRSTCTL);
1969 if (++count > 10000) {
1970 dev_warn(hsotg->dev,
1971 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
1973 readl(hsotg->regs + GNPTXSTS));
1977 } while (greset & GRSTCTL_TXFFLSH);
1979 /* Wait for at least 3 PHY Clocks */
1984 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
1986 * @hsotg: Programming view of DWC_otg controller
1988 void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
1993 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1995 greset = GRSTCTL_RXFFLSH;
1996 writel(greset, hsotg->regs + GRSTCTL);
1999 greset = readl(hsotg->regs + GRSTCTL);
2000 if (++count > 10000) {
2001 dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
2006 } while (greset & GRSTCTL_RXFFLSH);
2008 /* Wait for at least 3 PHY Clocks */
2012 #define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
2014 /* Parameter access functions */
2015 void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
2020 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
2021 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
2024 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
2025 switch (hsotg->hw_params.op_mode) {
2026 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2027 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2028 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2029 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2036 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
2047 "%d invalid for otg_cap parameter. Check HW configuration.\n",
2049 switch (hsotg->hw_params.op_mode) {
2050 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2051 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
2053 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2054 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2055 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2056 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
2059 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
2062 dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
2065 hsotg->core_params->otg_cap = val;
2068 void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
2072 if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
2080 "%d invalid for dma_enable parameter. Check HW configuration.\n",
2082 val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
2083 dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
2086 hsotg->core_params->dma_enable = val;
2089 void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
2093 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
2094 !hsotg->hw_params.dma_desc_enable))
2102 "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
2104 val = (hsotg->core_params->dma_enable > 0 &&
2105 hsotg->hw_params.dma_desc_enable);
2106 dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
2109 hsotg->core_params->dma_desc_enable = val;
2112 void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
2115 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2118 "Wrong value for host_support_fs_low_power\n");
2120 "host_support_fs_low_power must be 0 or 1\n");
2124 "Setting host_support_fs_low_power to %d\n", val);
2127 hsotg->core_params->host_support_fs_ls_low_power = val;
2130 void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
2134 if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
2142 "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
2144 val = hsotg->hw_params.enable_dynamic_fifo;
2145 dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
2148 hsotg->core_params->enable_dynamic_fifo = val;
2151 void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2155 if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
2161 "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
2163 val = hsotg->hw_params.host_rx_fifo_size;
2164 dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
2167 hsotg->core_params->host_rx_fifo_size = val;
2170 void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2174 if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
2180 "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
2182 val = hsotg->hw_params.host_nperio_tx_fifo_size;
2183 dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
2187 hsotg->core_params->host_nperio_tx_fifo_size = val;
2190 void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2194 if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
2200 "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
2202 val = hsotg->hw_params.host_perio_tx_fifo_size;
2203 dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
2207 hsotg->core_params->host_perio_tx_fifo_size = val;
2210 void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
2214 if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
2220 "%d invalid for max_transfer_size. Check HW configuration.\n",
2222 val = hsotg->hw_params.max_transfer_size;
2223 dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
2226 hsotg->core_params->max_transfer_size = val;
2229 void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
2233 if (val < 15 || val > hsotg->hw_params.max_packet_count)
2239 "%d invalid for max_packet_count. Check HW configuration.\n",
2241 val = hsotg->hw_params.max_packet_count;
2242 dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
2245 hsotg->core_params->max_packet_count = val;
2248 void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
2252 if (val < 1 || val > hsotg->hw_params.host_channels)
2258 "%d invalid for host_channels. Check HW configuration.\n",
2260 val = hsotg->hw_params.host_channels;
2261 dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
2264 hsotg->core_params->host_channels = val;
2267 void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
2270 u32 hs_phy_type, fs_phy_type;
2272 if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
2273 DWC2_PHY_TYPE_PARAM_ULPI)) {
2275 dev_err(hsotg->dev, "Wrong value for phy_type\n");
2276 dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
2282 hs_phy_type = hsotg->hw_params.hs_phy_type;
2283 fs_phy_type = hsotg->hw_params.fs_phy_type;
2284 if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
2285 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2286 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2288 else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
2289 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
2290 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2292 else if (val == DWC2_PHY_TYPE_PARAM_FS &&
2293 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2299 "%d invalid for phy_type. Check HW configuration.\n",
2301 val = DWC2_PHY_TYPE_PARAM_FS;
2302 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
2303 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2304 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
2305 val = DWC2_PHY_TYPE_PARAM_UTMI;
2307 val = DWC2_PHY_TYPE_PARAM_ULPI;
2309 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
2312 hsotg->core_params->phy_type = val;
2315 static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
2317 return hsotg->core_params->phy_type;
2320 void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
2324 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2326 dev_err(hsotg->dev, "Wrong value for speed parameter\n");
2327 dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
2332 if (val == DWC2_SPEED_PARAM_HIGH &&
2333 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2339 "%d invalid for speed parameter. Check HW configuration.\n",
2341 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
2342 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
2343 dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
2346 hsotg->core_params->speed = val;
2349 void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
2353 if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
2354 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
2357 "Wrong value for host_ls_low_power_phy_clk parameter\n");
2359 "host_ls_low_power_phy_clk must be 0 or 1\n");
2364 if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
2365 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2371 "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
2373 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
2374 ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
2375 : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
2376 dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
2380 hsotg->core_params->host_ls_low_power_phy_clk = val;
2383 void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
2385 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2387 dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
2388 dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
2391 dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
2394 hsotg->core_params->phy_ulpi_ddr = val;
2397 void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
2399 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2402 "Wrong value for phy_ulpi_ext_vbus\n");
2404 "phy_ulpi_ext_vbus must be 0 or 1\n");
2407 dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
2410 hsotg->core_params->phy_ulpi_ext_vbus = val;
2413 void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
2417 switch (hsotg->hw_params.utmi_phy_data_width) {
2418 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
2421 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
2422 valid = (val == 16);
2424 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
2425 valid = (val == 8 || val == 16);
2432 "%d invalid for phy_utmi_width. Check HW configuration.\n",
2435 val = (hsotg->hw_params.utmi_phy_data_width ==
2436 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
2437 dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
2440 hsotg->core_params->phy_utmi_width = val;
2443 void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
2445 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2447 dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
2448 dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
2451 dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
2454 hsotg->core_params->ulpi_fs_ls = val;
2457 void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
2459 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2461 dev_err(hsotg->dev, "Wrong value for ts_dline\n");
2462 dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
2465 dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
2468 hsotg->core_params->ts_dline = val;
2471 void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
2475 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2477 dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
2478 dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
2484 if (val == 1 && !(hsotg->hw_params.i2c_enable))
2490 "%d invalid for i2c_enable. Check HW configuration.\n",
2492 val = hsotg->hw_params.i2c_enable;
2493 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
2496 hsotg->core_params->i2c_enable = val;
2499 void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
2503 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2506 "Wrong value for en_multiple_tx_fifo,\n");
2508 "en_multiple_tx_fifo must be 0 or 1\n");
2513 if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
2519 "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
2521 val = hsotg->hw_params.en_multiple_tx_fifo;
2522 dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
2525 hsotg->core_params->en_multiple_tx_fifo = val;
2528 void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
2532 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2535 "'%d' invalid for parameter reload_ctl\n", val);
2536 dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
2541 if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
2547 "%d invalid for parameter reload_ctl. Check HW configuration.\n",
2549 val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
2550 dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
2553 hsotg->core_params->reload_ctl = val;
2556 void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
2559 hsotg->core_params->ahbcfg = val;
2561 hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
2562 GAHBCFG_HBSTLEN_SHIFT;
2565 void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
2567 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2570 "'%d' invalid for parameter otg_ver\n", val);
2572 "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
2575 dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
2578 hsotg->core_params->otg_ver = val;
2581 static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
2583 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2586 "'%d' invalid for parameter uframe_sched\n",
2588 dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
2591 dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
2594 hsotg->core_params->uframe_sched = val;
2598 * This function is called during module intialization to pass module parameters
2599 * for the DWC_otg core.
2601 void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
2602 const struct dwc2_core_params *params)
2604 dev_dbg(hsotg->dev, "%s()\n", __func__);
2606 dwc2_set_param_otg_cap(hsotg, params->otg_cap);
2607 dwc2_set_param_dma_enable(hsotg, params->dma_enable);
2608 dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
2609 dwc2_set_param_host_support_fs_ls_low_power(hsotg,
2610 params->host_support_fs_ls_low_power);
2611 dwc2_set_param_enable_dynamic_fifo(hsotg,
2612 params->enable_dynamic_fifo);
2613 dwc2_set_param_host_rx_fifo_size(hsotg,
2614 params->host_rx_fifo_size);
2615 dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
2616 params->host_nperio_tx_fifo_size);
2617 dwc2_set_param_host_perio_tx_fifo_size(hsotg,
2618 params->host_perio_tx_fifo_size);
2619 dwc2_set_param_max_transfer_size(hsotg,
2620 params->max_transfer_size);
2621 dwc2_set_param_max_packet_count(hsotg,
2622 params->max_packet_count);
2623 dwc2_set_param_host_channels(hsotg, params->host_channels);
2624 dwc2_set_param_phy_type(hsotg, params->phy_type);
2625 dwc2_set_param_speed(hsotg, params->speed);
2626 dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
2627 params->host_ls_low_power_phy_clk);
2628 dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
2629 dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
2630 params->phy_ulpi_ext_vbus);
2631 dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
2632 dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
2633 dwc2_set_param_ts_dline(hsotg, params->ts_dline);
2634 dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
2635 dwc2_set_param_en_multiple_tx_fifo(hsotg,
2636 params->en_multiple_tx_fifo);
2637 dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
2638 dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
2639 dwc2_set_param_otg_ver(hsotg, params->otg_ver);
2640 dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
2644 * During device initialization, read various hardware configuration
2645 * registers and interpret the contents.
2647 int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
2649 struct dwc2_hw_params *hw = &hsotg->hw_params;
2651 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
2652 u32 hptxfsiz, grxfsiz, gnptxfsiz;
2656 * Attempt to ensure this device is really a DWC_otg Controller.
2657 * Read and verify the GSNPSID register contents. The value should be
2658 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
2659 * as in "OTG version 2.xx" or "OTG version 3.xx".
2661 hw->snpsid = readl(hsotg->regs + GSNPSID);
2662 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
2663 (hw->snpsid & 0xfffff000) != 0x4f543000) {
2664 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
2669 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
2670 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
2671 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
2673 hwcfg1 = readl(hsotg->regs + GHWCFG1);
2674 hwcfg2 = readl(hsotg->regs + GHWCFG2);
2675 hwcfg3 = readl(hsotg->regs + GHWCFG3);
2676 hwcfg4 = readl(hsotg->regs + GHWCFG4);
2677 grxfsiz = readl(hsotg->regs + GRXFSIZ);
2679 dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
2680 dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
2681 dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
2682 dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
2683 dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
2685 /* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
2686 gusbcfg = readl(hsotg->regs + GUSBCFG);
2687 gusbcfg |= GUSBCFG_FORCEHOSTMODE;
2688 writel(gusbcfg, hsotg->regs + GUSBCFG);
2689 usleep_range(100000, 150000);
2691 gnptxfsiz = readl(hsotg->regs + GNPTXFSIZ);
2692 hptxfsiz = readl(hsotg->regs + HPTXFSIZ);
2693 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
2694 dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
2695 gusbcfg = readl(hsotg->regs + GUSBCFG);
2696 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
2697 writel(gusbcfg, hsotg->regs + GUSBCFG);
2698 usleep_range(100000, 150000);
2701 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
2702 GHWCFG2_OP_MODE_SHIFT;
2703 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
2704 GHWCFG2_ARCHITECTURE_SHIFT;
2705 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
2706 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
2707 GHWCFG2_NUM_HOST_CHAN_SHIFT);
2708 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
2709 GHWCFG2_HS_PHY_TYPE_SHIFT;
2710 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
2711 GHWCFG2_FS_PHY_TYPE_SHIFT;
2712 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
2713 GHWCFG2_NUM_DEV_EP_SHIFT;
2714 hw->nperio_tx_q_depth =
2715 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
2716 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
2717 hw->host_perio_tx_q_depth =
2718 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
2719 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
2720 hw->dev_token_q_depth =
2721 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
2722 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
2725 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
2726 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
2727 hw->max_transfer_size = (1 << (width + 11)) - 1;
2728 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
2729 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
2730 hw->max_packet_count = (1 << (width + 4)) - 1;
2731 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
2732 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
2733 GHWCFG3_DFIFO_DEPTH_SHIFT;
2736 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
2737 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
2738 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
2739 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
2740 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
2741 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
2742 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
2745 hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
2746 GRXFSIZ_DEPTH_SHIFT;
2747 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
2748 FIFOSIZE_DEPTH_SHIFT;
2749 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
2750 FIFOSIZE_DEPTH_SHIFT;
2752 dev_dbg(hsotg->dev, "Detected values from hardware:\n");
2753 dev_dbg(hsotg->dev, " op_mode=%d\n",
2755 dev_dbg(hsotg->dev, " arch=%d\n",
2757 dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
2758 hw->dma_desc_enable);
2759 dev_dbg(hsotg->dev, " power_optimized=%d\n",
2760 hw->power_optimized);
2761 dev_dbg(hsotg->dev, " i2c_enable=%d\n",
2763 dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
2765 dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
2767 dev_dbg(hsotg->dev, " utmi_phy_data_wdith=%d\n",
2768 hw->utmi_phy_data_width);
2769 dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
2771 dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
2772 hw->num_dev_perio_in_ep);
2773 dev_dbg(hsotg->dev, " host_channels=%d\n",
2775 dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
2776 hw->max_transfer_size);
2777 dev_dbg(hsotg->dev, " max_packet_count=%d\n",
2778 hw->max_packet_count);
2779 dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
2780 hw->nperio_tx_q_depth);
2781 dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
2782 hw->host_perio_tx_q_depth);
2783 dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
2784 hw->dev_token_q_depth);
2785 dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
2786 hw->enable_dynamic_fifo);
2787 dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
2788 hw->en_multiple_tx_fifo);
2789 dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
2790 hw->total_fifo_size);
2791 dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
2792 hw->host_rx_fifo_size);
2793 dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
2794 hw->host_nperio_tx_fifo_size);
2795 dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
2796 hw->host_perio_tx_fifo_size);
2797 dev_dbg(hsotg->dev, "\n");
2802 u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
2804 return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
2807 bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
2809 if (readl(hsotg->regs + GSNPSID) == 0xffffffff)
2816 * dwc2_enable_global_interrupts() - Enables the controller's Global
2817 * Interrupt in the AHB Config register
2819 * @hsotg: Programming view of DWC_otg controller
2821 void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
2823 u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
2825 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
2826 writel(ahbcfg, hsotg->regs + GAHBCFG);
2830 * dwc2_disable_global_interrupts() - Disables the controller's Global
2831 * Interrupt in the AHB Config register
2833 * @hsotg: Programming view of DWC_otg controller
2835 void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
2837 u32 ahbcfg = readl(hsotg->regs + GAHBCFG);
2839 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
2840 writel(ahbcfg, hsotg->regs + GAHBCFG);
2843 MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
2844 MODULE_AUTHOR("Synopsys, Inc.");
2845 MODULE_LICENSE("Dual BSD/GPL");