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>
59 #if IS_ENABLED(CONFIG_USB_DWC2_HOST) || IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
61 * dwc2_backup_host_registers() - Backup controller host registers.
62 * When suspending usb bus, registers needs to be backuped
63 * if controller power is disabled once suspended.
65 * @hsotg: Programming view of the DWC_otg controller
67 static int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
69 struct dwc2_hregs_backup *hr;
72 dev_dbg(hsotg->dev, "%s\n", __func__);
74 /* Backup Host regs */
75 hr = &hsotg->hr_backup;
76 hr->hcfg = dwc2_readl(hsotg->regs + HCFG);
77 hr->haintmsk = dwc2_readl(hsotg->regs + HAINTMSK);
78 for (i = 0; i < hsotg->core_params->host_channels; ++i)
79 hr->hcintmsk[i] = dwc2_readl(hsotg->regs + HCINTMSK(i));
81 hr->hprt0 = dwc2_read_hprt0(hsotg);
82 hr->hfir = dwc2_readl(hsotg->regs + HFIR);
89 * dwc2_restore_host_registers() - Restore controller host registers.
90 * When resuming usb bus, device registers needs to be restored
91 * if controller power were disabled.
93 * @hsotg: Programming view of the DWC_otg controller
95 static int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
97 struct dwc2_hregs_backup *hr;
100 dev_dbg(hsotg->dev, "%s\n", __func__);
102 /* Restore host regs */
103 hr = &hsotg->hr_backup;
105 dev_err(hsotg->dev, "%s: no host registers to restore\n",
111 dwc2_writel(hr->hcfg, hsotg->regs + HCFG);
112 dwc2_writel(hr->haintmsk, hsotg->regs + HAINTMSK);
114 for (i = 0; i < hsotg->core_params->host_channels; ++i)
115 dwc2_writel(hr->hcintmsk[i], hsotg->regs + HCINTMSK(i));
117 dwc2_writel(hr->hprt0, hsotg->regs + HPRT0);
118 dwc2_writel(hr->hfir, hsotg->regs + HFIR);
119 hsotg->frame_number = 0;
124 static inline int dwc2_backup_host_registers(struct dwc2_hsotg *hsotg)
127 static inline int dwc2_restore_host_registers(struct dwc2_hsotg *hsotg)
131 #if IS_ENABLED(CONFIG_USB_DWC2_PERIPHERAL) || \
132 IS_ENABLED(CONFIG_USB_DWC2_DUAL_ROLE)
134 * dwc2_backup_device_registers() - Backup controller device registers.
135 * When suspending usb bus, registers needs to be backuped
136 * if controller power is disabled once suspended.
138 * @hsotg: Programming view of the DWC_otg controller
140 static int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
142 struct dwc2_dregs_backup *dr;
145 dev_dbg(hsotg->dev, "%s\n", __func__);
147 /* Backup dev regs */
148 dr = &hsotg->dr_backup;
150 dr->dcfg = dwc2_readl(hsotg->regs + DCFG);
151 dr->dctl = dwc2_readl(hsotg->regs + DCTL);
152 dr->daintmsk = dwc2_readl(hsotg->regs + DAINTMSK);
153 dr->diepmsk = dwc2_readl(hsotg->regs + DIEPMSK);
154 dr->doepmsk = dwc2_readl(hsotg->regs + DOEPMSK);
156 for (i = 0; i < hsotg->num_of_eps; i++) {
158 dr->diepctl[i] = dwc2_readl(hsotg->regs + DIEPCTL(i));
160 /* Ensure DATA PID is correctly configured */
161 if (dr->diepctl[i] & DXEPCTL_DPID)
162 dr->diepctl[i] |= DXEPCTL_SETD1PID;
164 dr->diepctl[i] |= DXEPCTL_SETD0PID;
166 dr->dieptsiz[i] = dwc2_readl(hsotg->regs + DIEPTSIZ(i));
167 dr->diepdma[i] = dwc2_readl(hsotg->regs + DIEPDMA(i));
170 dr->doepctl[i] = dwc2_readl(hsotg->regs + DOEPCTL(i));
172 /* Ensure DATA PID is correctly configured */
173 if (dr->doepctl[i] & DXEPCTL_DPID)
174 dr->doepctl[i] |= DXEPCTL_SETD1PID;
176 dr->doepctl[i] |= DXEPCTL_SETD0PID;
178 dr->doeptsiz[i] = dwc2_readl(hsotg->regs + DOEPTSIZ(i));
179 dr->doepdma[i] = dwc2_readl(hsotg->regs + DOEPDMA(i));
186 * dwc2_restore_device_registers() - Restore controller device registers.
187 * When resuming usb bus, device registers needs to be restored
188 * if controller power were disabled.
190 * @hsotg: Programming view of the DWC_otg controller
192 static int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
194 struct dwc2_dregs_backup *dr;
198 dev_dbg(hsotg->dev, "%s\n", __func__);
200 /* Restore dev regs */
201 dr = &hsotg->dr_backup;
203 dev_err(hsotg->dev, "%s: no device registers to restore\n",
209 dwc2_writel(dr->dcfg, hsotg->regs + DCFG);
210 dwc2_writel(dr->dctl, hsotg->regs + DCTL);
211 dwc2_writel(dr->daintmsk, hsotg->regs + DAINTMSK);
212 dwc2_writel(dr->diepmsk, hsotg->regs + DIEPMSK);
213 dwc2_writel(dr->doepmsk, hsotg->regs + DOEPMSK);
215 for (i = 0; i < hsotg->num_of_eps; i++) {
217 dwc2_writel(dr->diepctl[i], hsotg->regs + DIEPCTL(i));
218 dwc2_writel(dr->dieptsiz[i], hsotg->regs + DIEPTSIZ(i));
219 dwc2_writel(dr->diepdma[i], hsotg->regs + DIEPDMA(i));
221 /* Restore OUT EPs */
222 dwc2_writel(dr->doepctl[i], hsotg->regs + DOEPCTL(i));
223 dwc2_writel(dr->doeptsiz[i], hsotg->regs + DOEPTSIZ(i));
224 dwc2_writel(dr->doepdma[i], hsotg->regs + DOEPDMA(i));
227 /* Set the Power-On Programming done bit */
228 dctl = dwc2_readl(hsotg->regs + DCTL);
229 dctl |= DCTL_PWRONPRGDONE;
230 dwc2_writel(dctl, hsotg->regs + DCTL);
235 static inline int dwc2_backup_device_registers(struct dwc2_hsotg *hsotg)
238 static inline int dwc2_restore_device_registers(struct dwc2_hsotg *hsotg)
243 * dwc2_backup_global_registers() - Backup global controller registers.
244 * When suspending usb bus, registers needs to be backuped
245 * if controller power is disabled once suspended.
247 * @hsotg: Programming view of the DWC_otg controller
249 static int dwc2_backup_global_registers(struct dwc2_hsotg *hsotg)
251 struct dwc2_gregs_backup *gr;
254 /* Backup global regs */
255 gr = &hsotg->gr_backup;
257 gr->gotgctl = dwc2_readl(hsotg->regs + GOTGCTL);
258 gr->gintmsk = dwc2_readl(hsotg->regs + GINTMSK);
259 gr->gahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
260 gr->gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
261 gr->grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
262 gr->gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
263 gr->hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
264 gr->gdfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
265 for (i = 0; i < MAX_EPS_CHANNELS; i++)
266 gr->dtxfsiz[i] = dwc2_readl(hsotg->regs + DPTXFSIZN(i));
273 * dwc2_restore_global_registers() - Restore controller global registers.
274 * When resuming usb bus, device registers needs to be restored
275 * if controller power were disabled.
277 * @hsotg: Programming view of the DWC_otg controller
279 static int dwc2_restore_global_registers(struct dwc2_hsotg *hsotg)
281 struct dwc2_gregs_backup *gr;
284 dev_dbg(hsotg->dev, "%s\n", __func__);
286 /* Restore global regs */
287 gr = &hsotg->gr_backup;
289 dev_err(hsotg->dev, "%s: no global registers to restore\n",
295 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
296 dwc2_writel(gr->gotgctl, hsotg->regs + GOTGCTL);
297 dwc2_writel(gr->gintmsk, hsotg->regs + GINTMSK);
298 dwc2_writel(gr->gusbcfg, hsotg->regs + GUSBCFG);
299 dwc2_writel(gr->gahbcfg, hsotg->regs + GAHBCFG);
300 dwc2_writel(gr->grxfsiz, hsotg->regs + GRXFSIZ);
301 dwc2_writel(gr->gnptxfsiz, hsotg->regs + GNPTXFSIZ);
302 dwc2_writel(gr->hptxfsiz, hsotg->regs + HPTXFSIZ);
303 dwc2_writel(gr->gdfifocfg, hsotg->regs + GDFIFOCFG);
304 for (i = 0; i < MAX_EPS_CHANNELS; i++)
305 dwc2_writel(gr->dtxfsiz[i], hsotg->regs + DPTXFSIZN(i));
311 * dwc2_exit_hibernation() - Exit controller from Partial Power Down.
313 * @hsotg: Programming view of the DWC_otg controller
314 * @restore: Controller registers need to be restored
316 int dwc2_exit_hibernation(struct dwc2_hsotg *hsotg, bool restore)
321 if (!hsotg->core_params->hibernation)
324 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
325 pcgcctl &= ~PCGCTL_STOPPCLK;
326 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
328 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
329 pcgcctl &= ~PCGCTL_PWRCLMP;
330 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
332 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
333 pcgcctl &= ~PCGCTL_RSTPDWNMODULE;
334 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
338 ret = dwc2_restore_global_registers(hsotg);
340 dev_err(hsotg->dev, "%s: failed to restore registers\n",
344 if (dwc2_is_host_mode(hsotg)) {
345 ret = dwc2_restore_host_registers(hsotg);
347 dev_err(hsotg->dev, "%s: failed to restore host registers\n",
352 ret = dwc2_restore_device_registers(hsotg);
354 dev_err(hsotg->dev, "%s: failed to restore device registers\n",
365 * dwc2_enter_hibernation() - Put controller in Partial Power Down.
367 * @hsotg: Programming view of the DWC_otg controller
369 int dwc2_enter_hibernation(struct dwc2_hsotg *hsotg)
374 if (!hsotg->core_params->hibernation)
377 /* Backup all registers */
378 ret = dwc2_backup_global_registers(hsotg);
380 dev_err(hsotg->dev, "%s: failed to backup global registers\n",
385 if (dwc2_is_host_mode(hsotg)) {
386 ret = dwc2_backup_host_registers(hsotg);
388 dev_err(hsotg->dev, "%s: failed to backup host registers\n",
393 ret = dwc2_backup_device_registers(hsotg);
395 dev_err(hsotg->dev, "%s: failed to backup device registers\n",
402 * Clear any pending interrupts since dwc2 will not be able to
403 * clear them after entering hibernation.
405 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
407 /* Put the controller in low power state */
408 pcgcctl = dwc2_readl(hsotg->regs + PCGCTL);
410 pcgcctl |= PCGCTL_PWRCLMP;
411 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
414 pcgcctl |= PCGCTL_RSTPDWNMODULE;
415 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
418 pcgcctl |= PCGCTL_STOPPCLK;
419 dwc2_writel(pcgcctl, hsotg->regs + PCGCTL);
425 * dwc2_enable_common_interrupts() - Initializes the commmon interrupts,
426 * used in both device and host modes
428 * @hsotg: Programming view of the DWC_otg controller
430 static void dwc2_enable_common_interrupts(struct dwc2_hsotg *hsotg)
434 /* Clear any pending OTG Interrupts */
435 dwc2_writel(0xffffffff, hsotg->regs + GOTGINT);
437 /* Clear any pending interrupts */
438 dwc2_writel(0xffffffff, hsotg->regs + GINTSTS);
440 /* Enable the interrupts in the GINTMSK */
441 intmsk = GINTSTS_MODEMIS | GINTSTS_OTGINT;
443 if (hsotg->core_params->dma_enable <= 0)
444 intmsk |= GINTSTS_RXFLVL;
445 if (hsotg->core_params->external_id_pin_ctl <= 0)
446 intmsk |= GINTSTS_CONIDSTSCHNG;
448 intmsk |= GINTSTS_WKUPINT | GINTSTS_USBSUSP |
451 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
455 * Initializes the FSLSPClkSel field of the HCFG register depending on the
458 static void dwc2_init_fs_ls_pclk_sel(struct dwc2_hsotg *hsotg)
462 if ((hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
463 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
464 hsotg->core_params->ulpi_fs_ls > 0) ||
465 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
467 val = HCFG_FSLSPCLKSEL_48_MHZ;
469 /* High speed PHY running at full speed or high speed */
470 val = HCFG_FSLSPCLKSEL_30_60_MHZ;
473 dev_dbg(hsotg->dev, "Initializing HCFG.FSLSPClkSel to %08x\n", val);
474 hcfg = dwc2_readl(hsotg->regs + HCFG);
475 hcfg &= ~HCFG_FSLSPCLKSEL_MASK;
476 hcfg |= val << HCFG_FSLSPCLKSEL_SHIFT;
477 dwc2_writel(hcfg, hsotg->regs + HCFG);
481 * Do core a soft reset of the core. Be careful with this because it
482 * resets all the internal state machines of the core.
484 int dwc2_core_reset(struct dwc2_hsotg *hsotg)
489 dev_vdbg(hsotg->dev, "%s()\n", __func__);
491 /* Core Soft Reset */
492 greset = dwc2_readl(hsotg->regs + GRSTCTL);
493 greset |= GRSTCTL_CSFTRST;
494 dwc2_writel(greset, hsotg->regs + GRSTCTL);
497 greset = dwc2_readl(hsotg->regs + GRSTCTL);
500 "%s() HANG! Soft Reset GRSTCTL=%0x\n",
504 } while (greset & GRSTCTL_CSFTRST);
506 /* Wait for AHB master IDLE state */
510 greset = dwc2_readl(hsotg->regs + GRSTCTL);
513 "%s() HANG! AHB Idle GRSTCTL=%0x\n",
517 } while (!(greset & GRSTCTL_AHBIDLE));
523 * Do core a soft reset of the core. Be careful with this because it
524 * resets all the internal state machines of the core.
526 * Additionally this will apply force mode as per the hsotg->dr_mode
529 int dwc2_core_reset_and_force_dr_mode(struct dwc2_hsotg *hsotg)
534 retval = dwc2_core_reset(hsotg);
538 if (hsotg->dr_mode == USB_DR_MODE_HOST) {
539 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
540 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
541 gusbcfg |= GUSBCFG_FORCEHOSTMODE;
542 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
543 } else if (hsotg->dr_mode == USB_DR_MODE_PERIPHERAL) {
544 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
545 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
546 gusbcfg |= GUSBCFG_FORCEDEVMODE;
547 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
548 } else if (hsotg->dr_mode == USB_DR_MODE_OTG) {
549 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
550 gusbcfg &= ~GUSBCFG_FORCEHOSTMODE;
551 gusbcfg &= ~GUSBCFG_FORCEDEVMODE;
552 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
556 * NOTE: This long sleep is _very_ important, otherwise the core will
557 * not stay in host mode after a connector ID change!
559 usleep_range(150000, 160000);
564 static int dwc2_fs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
570 * core_init() is now called on every switch so only call the
571 * following for the first time through
574 dev_dbg(hsotg->dev, "FS PHY selected\n");
576 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
577 if (!(usbcfg & GUSBCFG_PHYSEL)) {
578 usbcfg |= GUSBCFG_PHYSEL;
579 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
581 /* Reset after a PHY select */
582 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
586 "%s: Reset failed, aborting", __func__);
593 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS. Also
594 * do this on HNP Dev/Host mode switches (done in dev_init and
597 if (dwc2_is_host_mode(hsotg))
598 dwc2_init_fs_ls_pclk_sel(hsotg);
600 if (hsotg->core_params->i2c_enable > 0) {
601 dev_dbg(hsotg->dev, "FS PHY enabling I2C\n");
603 /* Program GUSBCFG.OtgUtmiFsSel to I2C */
604 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
605 usbcfg |= GUSBCFG_OTG_UTMI_FS_SEL;
606 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
608 /* Program GI2CCTL.I2CEn */
609 i2cctl = dwc2_readl(hsotg->regs + GI2CCTL);
610 i2cctl &= ~GI2CCTL_I2CDEVADDR_MASK;
611 i2cctl |= 1 << GI2CCTL_I2CDEVADDR_SHIFT;
612 i2cctl &= ~GI2CCTL_I2CEN;
613 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
614 i2cctl |= GI2CCTL_I2CEN;
615 dwc2_writel(i2cctl, hsotg->regs + GI2CCTL);
621 static int dwc2_hs_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
623 u32 usbcfg, usbcfg_old;
629 usbcfg = usbcfg_old = dwc2_readl(hsotg->regs + GUSBCFG);
632 * HS PHY parameters. These parameters are preserved during soft reset
633 * so only program the first time. Do a soft reset immediately after
636 switch (hsotg->core_params->phy_type) {
637 case DWC2_PHY_TYPE_PARAM_ULPI:
639 dev_dbg(hsotg->dev, "HS ULPI PHY selected\n");
640 usbcfg |= GUSBCFG_ULPI_UTMI_SEL;
641 usbcfg &= ~(GUSBCFG_PHYIF16 | GUSBCFG_DDRSEL);
642 if (hsotg->core_params->phy_ulpi_ddr > 0)
643 usbcfg |= GUSBCFG_DDRSEL;
645 case DWC2_PHY_TYPE_PARAM_UTMI:
646 /* UTMI+ interface */
647 dev_dbg(hsotg->dev, "HS UTMI+ PHY selected\n");
648 usbcfg &= ~(GUSBCFG_ULPI_UTMI_SEL | GUSBCFG_PHYIF16);
649 if (hsotg->core_params->phy_utmi_width == 16)
650 usbcfg |= GUSBCFG_PHYIF16;
653 dev_err(hsotg->dev, "FS PHY selected at HS!\n");
657 if (usbcfg != usbcfg_old) {
658 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
660 /* Reset after setting the PHY parameters */
661 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
664 "%s: Reset failed, aborting", __func__);
672 static int dwc2_phy_init(struct dwc2_hsotg *hsotg, bool select_phy)
677 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL &&
678 hsotg->core_params->phy_type == DWC2_PHY_TYPE_PARAM_FS) {
679 /* If FS mode with FS PHY */
680 retval = dwc2_fs_phy_init(hsotg, select_phy);
685 retval = dwc2_hs_phy_init(hsotg, select_phy);
690 if (hsotg->hw_params.hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI &&
691 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED &&
692 hsotg->core_params->ulpi_fs_ls > 0) {
693 dev_dbg(hsotg->dev, "Setting ULPI FSLS\n");
694 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
695 usbcfg |= GUSBCFG_ULPI_FS_LS;
696 usbcfg |= GUSBCFG_ULPI_CLK_SUSP_M;
697 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
699 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
700 usbcfg &= ~GUSBCFG_ULPI_FS_LS;
701 usbcfg &= ~GUSBCFG_ULPI_CLK_SUSP_M;
702 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
708 static int dwc2_gahbcfg_init(struct dwc2_hsotg *hsotg)
710 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
712 switch (hsotg->hw_params.arch) {
713 case GHWCFG2_EXT_DMA_ARCH:
714 dev_err(hsotg->dev, "External DMA Mode not supported\n");
717 case GHWCFG2_INT_DMA_ARCH:
718 dev_dbg(hsotg->dev, "Internal DMA Mode\n");
719 if (hsotg->core_params->ahbcfg != -1) {
720 ahbcfg &= GAHBCFG_CTRL_MASK;
721 ahbcfg |= hsotg->core_params->ahbcfg &
726 case GHWCFG2_SLAVE_ONLY_ARCH:
728 dev_dbg(hsotg->dev, "Slave Only Mode\n");
732 dev_dbg(hsotg->dev, "dma_enable:%d dma_desc_enable:%d\n",
733 hsotg->core_params->dma_enable,
734 hsotg->core_params->dma_desc_enable);
736 if (hsotg->core_params->dma_enable > 0) {
737 if (hsotg->core_params->dma_desc_enable > 0)
738 dev_dbg(hsotg->dev, "Using Descriptor DMA mode\n");
740 dev_dbg(hsotg->dev, "Using Buffer DMA mode\n");
742 dev_dbg(hsotg->dev, "Using Slave mode\n");
743 hsotg->core_params->dma_desc_enable = 0;
746 if (hsotg->core_params->dma_enable > 0)
747 ahbcfg |= GAHBCFG_DMA_EN;
749 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
754 static void dwc2_gusbcfg_init(struct dwc2_hsotg *hsotg)
758 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
759 usbcfg &= ~(GUSBCFG_HNPCAP | GUSBCFG_SRPCAP);
761 switch (hsotg->hw_params.op_mode) {
762 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
763 if (hsotg->core_params->otg_cap ==
764 DWC2_CAP_PARAM_HNP_SRP_CAPABLE)
765 usbcfg |= GUSBCFG_HNPCAP;
766 if (hsotg->core_params->otg_cap !=
767 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
768 usbcfg |= GUSBCFG_SRPCAP;
771 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
772 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
773 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
774 if (hsotg->core_params->otg_cap !=
775 DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE)
776 usbcfg |= GUSBCFG_SRPCAP;
779 case GHWCFG2_OP_MODE_NO_HNP_SRP_CAPABLE:
780 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE:
781 case GHWCFG2_OP_MODE_NO_SRP_CAPABLE_HOST:
786 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
790 * dwc2_core_init() - Initializes the DWC_otg controller registers and
791 * prepares the core for device mode or host mode operation
793 * @hsotg: Programming view of the DWC_otg controller
794 * @initial_setup: If true then this is the first init for this instance.
796 int dwc2_core_init(struct dwc2_hsotg *hsotg, bool initial_setup)
801 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
803 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
805 /* Set ULPI External VBUS bit if needed */
806 usbcfg &= ~GUSBCFG_ULPI_EXT_VBUS_DRV;
807 if (hsotg->core_params->phy_ulpi_ext_vbus ==
808 DWC2_PHY_ULPI_EXTERNAL_VBUS)
809 usbcfg |= GUSBCFG_ULPI_EXT_VBUS_DRV;
811 /* Set external TS Dline pulsing bit if needed */
812 usbcfg &= ~GUSBCFG_TERMSELDLPULSE;
813 if (hsotg->core_params->ts_dline > 0)
814 usbcfg |= GUSBCFG_TERMSELDLPULSE;
816 dwc2_writel(usbcfg, hsotg->regs + GUSBCFG);
819 * Reset the Controller
821 * We only need to reset the controller if this is a re-init.
822 * For the first init we know for sure that earlier code reset us (it
823 * needed to in order to properly detect various parameters).
825 if (!initial_setup) {
826 retval = dwc2_core_reset_and_force_dr_mode(hsotg);
828 dev_err(hsotg->dev, "%s(): Reset failed, aborting\n",
835 * This needs to happen in FS mode before any other programming occurs
837 retval = dwc2_phy_init(hsotg, initial_setup);
841 /* Program the GAHBCFG Register */
842 retval = dwc2_gahbcfg_init(hsotg);
846 /* Program the GUSBCFG register */
847 dwc2_gusbcfg_init(hsotg);
849 /* Program the GOTGCTL register */
850 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
851 otgctl &= ~GOTGCTL_OTGVER;
852 if (hsotg->core_params->otg_ver > 0)
853 otgctl |= GOTGCTL_OTGVER;
854 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
855 dev_dbg(hsotg->dev, "OTG VER PARAM: %d\n", hsotg->core_params->otg_ver);
857 /* Clear the SRP success bit for FS-I2c */
858 hsotg->srp_success = 0;
860 /* Enable common interrupts */
861 dwc2_enable_common_interrupts(hsotg);
864 * Do device or host initialization based on mode during PCD and
867 if (dwc2_is_host_mode(hsotg)) {
868 dev_dbg(hsotg->dev, "Host Mode\n");
869 hsotg->op_state = OTG_STATE_A_HOST;
871 dev_dbg(hsotg->dev, "Device Mode\n");
872 hsotg->op_state = OTG_STATE_B_PERIPHERAL;
879 * dwc2_enable_host_interrupts() - Enables the Host mode interrupts
881 * @hsotg: Programming view of DWC_otg controller
883 void dwc2_enable_host_interrupts(struct dwc2_hsotg *hsotg)
887 dev_dbg(hsotg->dev, "%s()\n", __func__);
889 /* Disable all interrupts */
890 dwc2_writel(0, hsotg->regs + GINTMSK);
891 dwc2_writel(0, hsotg->regs + HAINTMSK);
893 /* Enable the common interrupts */
894 dwc2_enable_common_interrupts(hsotg);
896 /* Enable host mode interrupts without disturbing common interrupts */
897 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
898 intmsk |= GINTSTS_DISCONNINT | GINTSTS_PRTINT | GINTSTS_HCHINT;
899 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
903 * dwc2_disable_host_interrupts() - Disables the Host Mode interrupts
905 * @hsotg: Programming view of DWC_otg controller
907 void dwc2_disable_host_interrupts(struct dwc2_hsotg *hsotg)
909 u32 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
911 /* Disable host mode interrupts without disturbing common interrupts */
912 intmsk &= ~(GINTSTS_SOF | GINTSTS_PRTINT | GINTSTS_HCHINT |
913 GINTSTS_PTXFEMP | GINTSTS_NPTXFEMP | GINTSTS_DISCONNINT);
914 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
918 * dwc2_calculate_dynamic_fifo() - Calculates the default fifo size
919 * For system that have a total fifo depth that is smaller than the default
922 * @hsotg: Programming view of DWC_otg controller
924 static void dwc2_calculate_dynamic_fifo(struct dwc2_hsotg *hsotg)
926 struct dwc2_core_params *params = hsotg->core_params;
927 struct dwc2_hw_params *hw = &hsotg->hw_params;
928 u32 rxfsiz, nptxfsiz, ptxfsiz, total_fifo_size;
930 total_fifo_size = hw->total_fifo_size;
931 rxfsiz = params->host_rx_fifo_size;
932 nptxfsiz = params->host_nperio_tx_fifo_size;
933 ptxfsiz = params->host_perio_tx_fifo_size;
936 * Will use Method 2 defined in the DWC2 spec: minimum FIFO depth
937 * allocation with support for high bandwidth endpoints. Synopsys
938 * defines MPS(Max Packet size) for a periodic EP=1024, and for
939 * non-periodic as 512.
941 if (total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)) {
943 * For Buffer DMA mode/Scatter Gather DMA mode
944 * 2 * ((Largest Packet size / 4) + 1 + 1) + n
945 * with n = number of host channel.
946 * 2 * ((1024/4) + 2) = 516
948 rxfsiz = 516 + hw->host_channels;
951 * min non-periodic tx fifo depth
952 * 2 * (largest non-periodic USB packet used / 4)
958 * min periodic tx fifo depth
959 * (largest packet size*MC)/4
964 params->host_rx_fifo_size = rxfsiz;
965 params->host_nperio_tx_fifo_size = nptxfsiz;
966 params->host_perio_tx_fifo_size = ptxfsiz;
970 * If the summation of RX, NPTX and PTX fifo sizes is still
971 * bigger than the total_fifo_size, then we have a problem.
973 * We won't be able to allocate as many endpoints. Right now,
974 * we're just printing an error message, but ideally this FIFO
975 * allocation algorithm would be improved in the future.
977 * FIXME improve this FIFO allocation algorithm.
979 if (unlikely(total_fifo_size < (rxfsiz + nptxfsiz + ptxfsiz)))
980 dev_err(hsotg->dev, "invalid fifo sizes\n");
983 static void dwc2_config_fifos(struct dwc2_hsotg *hsotg)
985 struct dwc2_core_params *params = hsotg->core_params;
986 u32 nptxfsiz, hptxfsiz, dfifocfg, grxfsiz;
988 if (!params->enable_dynamic_fifo)
991 dwc2_calculate_dynamic_fifo(hsotg);
994 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
995 dev_dbg(hsotg->dev, "initial grxfsiz=%08x\n", grxfsiz);
996 grxfsiz &= ~GRXFSIZ_DEPTH_MASK;
997 grxfsiz |= params->host_rx_fifo_size <<
998 GRXFSIZ_DEPTH_SHIFT & GRXFSIZ_DEPTH_MASK;
999 dwc2_writel(grxfsiz, hsotg->regs + GRXFSIZ);
1000 dev_dbg(hsotg->dev, "new grxfsiz=%08x\n",
1001 dwc2_readl(hsotg->regs + GRXFSIZ));
1003 /* Non-periodic Tx FIFO */
1004 dev_dbg(hsotg->dev, "initial gnptxfsiz=%08x\n",
1005 dwc2_readl(hsotg->regs + GNPTXFSIZ));
1006 nptxfsiz = params->host_nperio_tx_fifo_size <<
1007 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
1008 nptxfsiz |= params->host_rx_fifo_size <<
1009 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
1010 dwc2_writel(nptxfsiz, hsotg->regs + GNPTXFSIZ);
1011 dev_dbg(hsotg->dev, "new gnptxfsiz=%08x\n",
1012 dwc2_readl(hsotg->regs + GNPTXFSIZ));
1014 /* Periodic Tx FIFO */
1015 dev_dbg(hsotg->dev, "initial hptxfsiz=%08x\n",
1016 dwc2_readl(hsotg->regs + HPTXFSIZ));
1017 hptxfsiz = params->host_perio_tx_fifo_size <<
1018 FIFOSIZE_DEPTH_SHIFT & FIFOSIZE_DEPTH_MASK;
1019 hptxfsiz |= (params->host_rx_fifo_size +
1020 params->host_nperio_tx_fifo_size) <<
1021 FIFOSIZE_STARTADDR_SHIFT & FIFOSIZE_STARTADDR_MASK;
1022 dwc2_writel(hptxfsiz, hsotg->regs + HPTXFSIZ);
1023 dev_dbg(hsotg->dev, "new hptxfsiz=%08x\n",
1024 dwc2_readl(hsotg->regs + HPTXFSIZ));
1026 if (hsotg->core_params->en_multiple_tx_fifo > 0 &&
1027 hsotg->hw_params.snpsid <= DWC2_CORE_REV_2_94a) {
1029 * Global DFIFOCFG calculation for Host mode -
1030 * include RxFIFO, NPTXFIFO and HPTXFIFO
1032 dfifocfg = dwc2_readl(hsotg->regs + GDFIFOCFG);
1033 dfifocfg &= ~GDFIFOCFG_EPINFOBASE_MASK;
1034 dfifocfg |= (params->host_rx_fifo_size +
1035 params->host_nperio_tx_fifo_size +
1036 params->host_perio_tx_fifo_size) <<
1037 GDFIFOCFG_EPINFOBASE_SHIFT &
1038 GDFIFOCFG_EPINFOBASE_MASK;
1039 dwc2_writel(dfifocfg, hsotg->regs + GDFIFOCFG);
1044 * dwc2_core_host_init() - Initializes the DWC_otg controller registers for
1047 * @hsotg: Programming view of DWC_otg controller
1049 * This function flushes the Tx and Rx FIFOs and flushes any entries in the
1050 * request queues. Host channels are reset to ensure that they are ready for
1051 * performing transfers.
1053 void dwc2_core_host_init(struct dwc2_hsotg *hsotg)
1055 u32 hcfg, hfir, otgctl;
1057 dev_dbg(hsotg->dev, "%s(%p)\n", __func__, hsotg);
1059 /* Restart the Phy Clock */
1060 dwc2_writel(0, hsotg->regs + PCGCTL);
1062 /* Initialize Host Configuration Register */
1063 dwc2_init_fs_ls_pclk_sel(hsotg);
1064 if (hsotg->core_params->speed == DWC2_SPEED_PARAM_FULL) {
1065 hcfg = dwc2_readl(hsotg->regs + HCFG);
1066 hcfg |= HCFG_FSLSSUPP;
1067 dwc2_writel(hcfg, hsotg->regs + HCFG);
1071 * This bit allows dynamic reloading of the HFIR register during
1072 * runtime. This bit needs to be programmed during initial configuration
1073 * and its value must not be changed during runtime.
1075 if (hsotg->core_params->reload_ctl > 0) {
1076 hfir = dwc2_readl(hsotg->regs + HFIR);
1077 hfir |= HFIR_RLDCTRL;
1078 dwc2_writel(hfir, hsotg->regs + HFIR);
1081 if (hsotg->core_params->dma_desc_enable > 0) {
1082 u32 op_mode = hsotg->hw_params.op_mode;
1083 if (hsotg->hw_params.snpsid < DWC2_CORE_REV_2_90a ||
1084 !hsotg->hw_params.dma_desc_enable ||
1085 op_mode == GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE ||
1086 op_mode == GHWCFG2_OP_MODE_NO_SRP_CAPABLE_DEVICE ||
1087 op_mode == GHWCFG2_OP_MODE_UNDEFINED) {
1089 "Hardware does not support descriptor DMA mode -\n");
1091 "falling back to buffer DMA mode.\n");
1092 hsotg->core_params->dma_desc_enable = 0;
1094 hcfg = dwc2_readl(hsotg->regs + HCFG);
1095 hcfg |= HCFG_DESCDMA;
1096 dwc2_writel(hcfg, hsotg->regs + HCFG);
1100 /* Configure data FIFO sizes */
1101 dwc2_config_fifos(hsotg);
1103 /* TODO - check this */
1104 /* Clear Host Set HNP Enable in the OTG Control Register */
1105 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
1106 otgctl &= ~GOTGCTL_HSTSETHNPEN;
1107 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
1109 /* Make sure the FIFOs are flushed */
1110 dwc2_flush_tx_fifo(hsotg, 0x10 /* all TX FIFOs */);
1111 dwc2_flush_rx_fifo(hsotg);
1113 /* Clear Host Set HNP Enable in the OTG Control Register */
1114 otgctl = dwc2_readl(hsotg->regs + GOTGCTL);
1115 otgctl &= ~GOTGCTL_HSTSETHNPEN;
1116 dwc2_writel(otgctl, hsotg->regs + GOTGCTL);
1118 if (hsotg->core_params->dma_desc_enable <= 0) {
1119 int num_channels, i;
1122 /* Flush out any leftover queued requests */
1123 num_channels = hsotg->core_params->host_channels;
1124 for (i = 0; i < num_channels; i++) {
1125 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1126 hcchar &= ~HCCHAR_CHENA;
1127 hcchar |= HCCHAR_CHDIS;
1128 hcchar &= ~HCCHAR_EPDIR;
1129 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
1132 /* Halt all channels to put them into a known state */
1133 for (i = 0; i < num_channels; i++) {
1136 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1137 hcchar |= HCCHAR_CHENA | HCCHAR_CHDIS;
1138 hcchar &= ~HCCHAR_EPDIR;
1139 dwc2_writel(hcchar, hsotg->regs + HCCHAR(i));
1140 dev_dbg(hsotg->dev, "%s: Halt channel %d\n",
1143 hcchar = dwc2_readl(hsotg->regs + HCCHAR(i));
1144 if (++count > 1000) {
1146 "Unable to clear enable on channel %d\n",
1151 } while (hcchar & HCCHAR_CHENA);
1155 /* Turn on the vbus power */
1156 dev_dbg(hsotg->dev, "Init: Port Power? op_state=%d\n", hsotg->op_state);
1157 if (hsotg->op_state == OTG_STATE_A_HOST) {
1158 u32 hprt0 = dwc2_read_hprt0(hsotg);
1160 dev_dbg(hsotg->dev, "Init: Power Port (%d)\n",
1161 !!(hprt0 & HPRT0_PWR));
1162 if (!(hprt0 & HPRT0_PWR)) {
1164 dwc2_writel(hprt0, hsotg->regs + HPRT0);
1168 dwc2_enable_host_interrupts(hsotg);
1171 static void dwc2_hc_enable_slave_ints(struct dwc2_hsotg *hsotg,
1172 struct dwc2_host_chan *chan)
1174 u32 hcintmsk = HCINTMSK_CHHLTD;
1176 switch (chan->ep_type) {
1177 case USB_ENDPOINT_XFER_CONTROL:
1178 case USB_ENDPOINT_XFER_BULK:
1179 dev_vdbg(hsotg->dev, "control/bulk\n");
1180 hcintmsk |= HCINTMSK_XFERCOMPL;
1181 hcintmsk |= HCINTMSK_STALL;
1182 hcintmsk |= HCINTMSK_XACTERR;
1183 hcintmsk |= HCINTMSK_DATATGLERR;
1184 if (chan->ep_is_in) {
1185 hcintmsk |= HCINTMSK_BBLERR;
1187 hcintmsk |= HCINTMSK_NAK;
1188 hcintmsk |= HCINTMSK_NYET;
1190 hcintmsk |= HCINTMSK_ACK;
1193 if (chan->do_split) {
1194 hcintmsk |= HCINTMSK_NAK;
1195 if (chan->complete_split)
1196 hcintmsk |= HCINTMSK_NYET;
1198 hcintmsk |= HCINTMSK_ACK;
1201 if (chan->error_state)
1202 hcintmsk |= HCINTMSK_ACK;
1205 case USB_ENDPOINT_XFER_INT:
1207 dev_vdbg(hsotg->dev, "intr\n");
1208 hcintmsk |= HCINTMSK_XFERCOMPL;
1209 hcintmsk |= HCINTMSK_NAK;
1210 hcintmsk |= HCINTMSK_STALL;
1211 hcintmsk |= HCINTMSK_XACTERR;
1212 hcintmsk |= HCINTMSK_DATATGLERR;
1213 hcintmsk |= HCINTMSK_FRMOVRUN;
1216 hcintmsk |= HCINTMSK_BBLERR;
1217 if (chan->error_state)
1218 hcintmsk |= HCINTMSK_ACK;
1219 if (chan->do_split) {
1220 if (chan->complete_split)
1221 hcintmsk |= HCINTMSK_NYET;
1223 hcintmsk |= HCINTMSK_ACK;
1227 case USB_ENDPOINT_XFER_ISOC:
1229 dev_vdbg(hsotg->dev, "isoc\n");
1230 hcintmsk |= HCINTMSK_XFERCOMPL;
1231 hcintmsk |= HCINTMSK_FRMOVRUN;
1232 hcintmsk |= HCINTMSK_ACK;
1234 if (chan->ep_is_in) {
1235 hcintmsk |= HCINTMSK_XACTERR;
1236 hcintmsk |= HCINTMSK_BBLERR;
1240 dev_err(hsotg->dev, "## Unknown EP type ##\n");
1244 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1246 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
1249 static void dwc2_hc_enable_dma_ints(struct dwc2_hsotg *hsotg,
1250 struct dwc2_host_chan *chan)
1252 u32 hcintmsk = HCINTMSK_CHHLTD;
1255 * For Descriptor DMA mode core halts the channel on AHB error.
1256 * Interrupt is not required.
1258 if (hsotg->core_params->dma_desc_enable <= 0) {
1260 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1261 hcintmsk |= HCINTMSK_AHBERR;
1264 dev_vdbg(hsotg->dev, "desc DMA enabled\n");
1265 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1266 hcintmsk |= HCINTMSK_XFERCOMPL;
1269 if (chan->error_state && !chan->do_split &&
1270 chan->ep_type != USB_ENDPOINT_XFER_ISOC) {
1272 dev_vdbg(hsotg->dev, "setting ACK\n");
1273 hcintmsk |= HCINTMSK_ACK;
1274 if (chan->ep_is_in) {
1275 hcintmsk |= HCINTMSK_DATATGLERR;
1276 if (chan->ep_type != USB_ENDPOINT_XFER_INT)
1277 hcintmsk |= HCINTMSK_NAK;
1281 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1283 dev_vdbg(hsotg->dev, "set HCINTMSK to %08x\n", hcintmsk);
1286 static void dwc2_hc_enable_ints(struct dwc2_hsotg *hsotg,
1287 struct dwc2_host_chan *chan)
1291 if (hsotg->core_params->dma_enable > 0) {
1293 dev_vdbg(hsotg->dev, "DMA enabled\n");
1294 dwc2_hc_enable_dma_ints(hsotg, chan);
1297 dev_vdbg(hsotg->dev, "DMA disabled\n");
1298 dwc2_hc_enable_slave_ints(hsotg, chan);
1301 /* Enable the top level host channel interrupt */
1302 intmsk = dwc2_readl(hsotg->regs + HAINTMSK);
1303 intmsk |= 1 << chan->hc_num;
1304 dwc2_writel(intmsk, hsotg->regs + HAINTMSK);
1306 dev_vdbg(hsotg->dev, "set HAINTMSK to %08x\n", intmsk);
1308 /* Make sure host channel interrupts are enabled */
1309 intmsk = dwc2_readl(hsotg->regs + GINTMSK);
1310 intmsk |= GINTSTS_HCHINT;
1311 dwc2_writel(intmsk, hsotg->regs + GINTMSK);
1313 dev_vdbg(hsotg->dev, "set GINTMSK to %08x\n", intmsk);
1317 * dwc2_hc_init() - Prepares a host channel for transferring packets to/from
1318 * a specific endpoint
1320 * @hsotg: Programming view of DWC_otg controller
1321 * @chan: Information needed to initialize the host channel
1323 * The HCCHARn register is set up with the characteristics specified in chan.
1324 * Host channel interrupts that may need to be serviced while this transfer is
1325 * in progress are enabled.
1327 void dwc2_hc_init(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1329 u8 hc_num = chan->hc_num;
1335 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1337 /* Clear old interrupt conditions for this host channel */
1338 hcintmsk = 0xffffffff;
1339 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1340 dwc2_writel(hcintmsk, hsotg->regs + HCINT(hc_num));
1342 /* Enable channel interrupts required for this transfer */
1343 dwc2_hc_enable_ints(hsotg, chan);
1346 * Program the HCCHARn register with the endpoint characteristics for
1347 * the current transfer
1349 hcchar = chan->dev_addr << HCCHAR_DEVADDR_SHIFT & HCCHAR_DEVADDR_MASK;
1350 hcchar |= chan->ep_num << HCCHAR_EPNUM_SHIFT & HCCHAR_EPNUM_MASK;
1352 hcchar |= HCCHAR_EPDIR;
1353 if (chan->speed == USB_SPEED_LOW)
1354 hcchar |= HCCHAR_LSPDDEV;
1355 hcchar |= chan->ep_type << HCCHAR_EPTYPE_SHIFT & HCCHAR_EPTYPE_MASK;
1356 hcchar |= chan->max_packet << HCCHAR_MPS_SHIFT & HCCHAR_MPS_MASK;
1357 dwc2_writel(hcchar, hsotg->regs + HCCHAR(hc_num));
1359 dev_vdbg(hsotg->dev, "set HCCHAR(%d) to %08x\n",
1362 dev_vdbg(hsotg->dev, "%s: Channel %d\n",
1364 dev_vdbg(hsotg->dev, " Dev Addr: %d\n",
1366 dev_vdbg(hsotg->dev, " Ep Num: %d\n",
1368 dev_vdbg(hsotg->dev, " Is In: %d\n",
1370 dev_vdbg(hsotg->dev, " Is Low Speed: %d\n",
1371 chan->speed == USB_SPEED_LOW);
1372 dev_vdbg(hsotg->dev, " Ep Type: %d\n",
1374 dev_vdbg(hsotg->dev, " Max Pkt: %d\n",
1378 /* Program the HCSPLT register for SPLITs */
1379 if (chan->do_split) {
1381 dev_vdbg(hsotg->dev,
1382 "Programming HC %d with split --> %s\n",
1384 chan->complete_split ? "CSPLIT" : "SSPLIT");
1385 if (chan->complete_split)
1386 hcsplt |= HCSPLT_COMPSPLT;
1387 hcsplt |= chan->xact_pos << HCSPLT_XACTPOS_SHIFT &
1388 HCSPLT_XACTPOS_MASK;
1389 hcsplt |= chan->hub_addr << HCSPLT_HUBADDR_SHIFT &
1390 HCSPLT_HUBADDR_MASK;
1391 hcsplt |= chan->hub_port << HCSPLT_PRTADDR_SHIFT &
1392 HCSPLT_PRTADDR_MASK;
1394 dev_vdbg(hsotg->dev, " comp split %d\n",
1395 chan->complete_split);
1396 dev_vdbg(hsotg->dev, " xact pos %d\n",
1398 dev_vdbg(hsotg->dev, " hub addr %d\n",
1400 dev_vdbg(hsotg->dev, " hub port %d\n",
1402 dev_vdbg(hsotg->dev, " is_in %d\n",
1404 dev_vdbg(hsotg->dev, " Max Pkt %d\n",
1406 dev_vdbg(hsotg->dev, " xferlen %d\n",
1411 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(hc_num));
1415 * dwc2_hc_halt() - Attempts to halt a host channel
1417 * @hsotg: Controller register interface
1418 * @chan: Host channel to halt
1419 * @halt_status: Reason for halting the channel
1421 * This function should only be called in Slave mode or to abort a transfer in
1422 * either Slave mode or DMA mode. Under normal circumstances in DMA mode, the
1423 * controller halts the channel when the transfer is complete or a condition
1424 * occurs that requires application intervention.
1426 * In slave mode, checks for a free request queue entry, then sets the Channel
1427 * Enable and Channel Disable bits of the Host Channel Characteristics
1428 * register of the specified channel to intiate the halt. If there is no free
1429 * request queue entry, sets only the Channel Disable bit of the HCCHARn
1430 * register to flush requests for this channel. In the latter case, sets a
1431 * flag to indicate that the host channel needs to be halted when a request
1432 * queue slot is open.
1434 * In DMA mode, always sets the Channel Enable and Channel Disable bits of the
1435 * HCCHARn register. The controller ensures there is space in the request
1436 * queue before submitting the halt request.
1438 * Some time may elapse before the core flushes any posted requests for this
1439 * host channel and halts. The Channel Halted interrupt handler completes the
1440 * deactivation of the host channel.
1442 void dwc2_hc_halt(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan,
1443 enum dwc2_halt_status halt_status)
1445 u32 nptxsts, hptxsts, hcchar;
1448 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1449 if (halt_status == DWC2_HC_XFER_NO_HALT_STATUS)
1450 dev_err(hsotg->dev, "!!! halt_status = %d !!!\n", halt_status);
1452 if (halt_status == DWC2_HC_XFER_URB_DEQUEUE ||
1453 halt_status == DWC2_HC_XFER_AHB_ERR) {
1455 * Disable all channel interrupts except Ch Halted. The QTD
1456 * and QH state associated with this transfer has been cleared
1457 * (in the case of URB_DEQUEUE), so the channel needs to be
1458 * shut down carefully to prevent crashes.
1460 u32 hcintmsk = HCINTMSK_CHHLTD;
1462 dev_vdbg(hsotg->dev, "dequeue/error\n");
1463 dwc2_writel(hcintmsk, hsotg->regs + HCINTMSK(chan->hc_num));
1466 * Make sure no other interrupts besides halt are currently
1467 * pending. Handling another interrupt could cause a crash due
1468 * to the QTD and QH state.
1470 dwc2_writel(~hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1473 * Make sure the halt status is set to URB_DEQUEUE or AHB_ERR
1474 * even if the channel was already halted for some other
1477 chan->halt_status = halt_status;
1479 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1480 if (!(hcchar & HCCHAR_CHENA)) {
1482 * The channel is either already halted or it hasn't
1483 * started yet. In DMA mode, the transfer may halt if
1484 * it finishes normally or a condition occurs that
1485 * requires driver intervention. Don't want to halt
1486 * the channel again. In either Slave or DMA mode,
1487 * it's possible that the transfer has been assigned
1488 * to a channel, but not started yet when an URB is
1489 * dequeued. Don't want to halt a channel that hasn't
1495 if (chan->halt_pending) {
1497 * A halt has already been issued for this channel. This might
1498 * happen when a transfer is aborted by a higher level in
1501 dev_vdbg(hsotg->dev,
1502 "*** %s: Channel %d, chan->halt_pending already set ***\n",
1503 __func__, chan->hc_num);
1507 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1509 /* No need to set the bit in DDMA for disabling the channel */
1510 /* TODO check it everywhere channel is disabled */
1511 if (hsotg->core_params->dma_desc_enable <= 0) {
1513 dev_vdbg(hsotg->dev, "desc DMA disabled\n");
1514 hcchar |= HCCHAR_CHENA;
1517 dev_dbg(hsotg->dev, "desc DMA enabled\n");
1519 hcchar |= HCCHAR_CHDIS;
1521 if (hsotg->core_params->dma_enable <= 0) {
1523 dev_vdbg(hsotg->dev, "DMA not enabled\n");
1524 hcchar |= HCCHAR_CHENA;
1526 /* Check for space in the request queue to issue the halt */
1527 if (chan->ep_type == USB_ENDPOINT_XFER_CONTROL ||
1528 chan->ep_type == USB_ENDPOINT_XFER_BULK) {
1529 dev_vdbg(hsotg->dev, "control/bulk\n");
1530 nptxsts = dwc2_readl(hsotg->regs + GNPTXSTS);
1531 if ((nptxsts & TXSTS_QSPCAVAIL_MASK) == 0) {
1532 dev_vdbg(hsotg->dev, "Disabling channel\n");
1533 hcchar &= ~HCCHAR_CHENA;
1537 dev_vdbg(hsotg->dev, "isoc/intr\n");
1538 hptxsts = dwc2_readl(hsotg->regs + HPTXSTS);
1539 if ((hptxsts & TXSTS_QSPCAVAIL_MASK) == 0 ||
1540 hsotg->queuing_high_bandwidth) {
1542 dev_vdbg(hsotg->dev, "Disabling channel\n");
1543 hcchar &= ~HCCHAR_CHENA;
1548 dev_vdbg(hsotg->dev, "DMA enabled\n");
1551 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1552 chan->halt_status = halt_status;
1554 if (hcchar & HCCHAR_CHENA) {
1556 dev_vdbg(hsotg->dev, "Channel enabled\n");
1557 chan->halt_pending = 1;
1558 chan->halt_on_queue = 0;
1561 dev_vdbg(hsotg->dev, "Channel disabled\n");
1562 chan->halt_on_queue = 1;
1566 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1568 dev_vdbg(hsotg->dev, " hcchar: 0x%08x\n",
1570 dev_vdbg(hsotg->dev, " halt_pending: %d\n",
1571 chan->halt_pending);
1572 dev_vdbg(hsotg->dev, " halt_on_queue: %d\n",
1573 chan->halt_on_queue);
1574 dev_vdbg(hsotg->dev, " halt_status: %d\n",
1580 * dwc2_hc_cleanup() - Clears the transfer state for a host channel
1582 * @hsotg: Programming view of DWC_otg controller
1583 * @chan: Identifies the host channel to clean up
1585 * This function is normally called after a transfer is done and the host
1586 * channel is being released
1588 void dwc2_hc_cleanup(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
1592 chan->xfer_started = 0;
1595 * Clear channel interrupt enables and any unhandled channel interrupt
1598 dwc2_writel(0, hsotg->regs + HCINTMSK(chan->hc_num));
1599 hcintmsk = 0xffffffff;
1600 hcintmsk &= ~HCINTMSK_RESERVED14_31;
1601 dwc2_writel(hcintmsk, hsotg->regs + HCINT(chan->hc_num));
1605 * dwc2_hc_set_even_odd_frame() - Sets the channel property that indicates in
1606 * which frame a periodic transfer should occur
1608 * @hsotg: Programming view of DWC_otg controller
1609 * @chan: Identifies the host channel to set up and its properties
1610 * @hcchar: Current value of the HCCHAR register for the specified host channel
1612 * This function has no effect on non-periodic transfers
1614 static void dwc2_hc_set_even_odd_frame(struct dwc2_hsotg *hsotg,
1615 struct dwc2_host_chan *chan, u32 *hcchar)
1617 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1618 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1619 /* 1 if _next_ frame is odd, 0 if it's even */
1620 if (!(dwc2_hcd_get_frame_number(hsotg) & 0x1))
1621 *hcchar |= HCCHAR_ODDFRM;
1625 static void dwc2_set_pid_isoc(struct dwc2_host_chan *chan)
1627 /* Set up the initial PID for the transfer */
1628 if (chan->speed == USB_SPEED_HIGH) {
1629 if (chan->ep_is_in) {
1630 if (chan->multi_count == 1)
1631 chan->data_pid_start = DWC2_HC_PID_DATA0;
1632 else if (chan->multi_count == 2)
1633 chan->data_pid_start = DWC2_HC_PID_DATA1;
1635 chan->data_pid_start = DWC2_HC_PID_DATA2;
1637 if (chan->multi_count == 1)
1638 chan->data_pid_start = DWC2_HC_PID_DATA0;
1640 chan->data_pid_start = DWC2_HC_PID_MDATA;
1643 chan->data_pid_start = DWC2_HC_PID_DATA0;
1648 * dwc2_hc_write_packet() - Writes a packet into the Tx FIFO associated with
1651 * @hsotg: Programming view of DWC_otg controller
1652 * @chan: Information needed to initialize the host channel
1654 * This function should only be called in Slave mode. For a channel associated
1655 * with a non-periodic EP, the non-periodic Tx FIFO is written. For a channel
1656 * associated with a periodic EP, the periodic Tx FIFO is written.
1658 * Upon return the xfer_buf and xfer_count fields in chan are incremented by
1659 * the number of bytes written to the Tx FIFO.
1661 static void dwc2_hc_write_packet(struct dwc2_hsotg *hsotg,
1662 struct dwc2_host_chan *chan)
1665 u32 remaining_count;
1668 u32 __iomem *data_fifo;
1669 u32 *data_buf = (u32 *)chan->xfer_buf;
1672 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1674 data_fifo = (u32 __iomem *)(hsotg->regs + HCFIFO(chan->hc_num));
1676 remaining_count = chan->xfer_len - chan->xfer_count;
1677 if (remaining_count > chan->max_packet)
1678 byte_count = chan->max_packet;
1680 byte_count = remaining_count;
1682 dword_count = (byte_count + 3) / 4;
1684 if (((unsigned long)data_buf & 0x3) == 0) {
1685 /* xfer_buf is DWORD aligned */
1686 for (i = 0; i < dword_count; i++, data_buf++)
1687 dwc2_writel(*data_buf, data_fifo);
1689 /* xfer_buf is not DWORD aligned */
1690 for (i = 0; i < dword_count; i++, data_buf++) {
1691 u32 data = data_buf[0] | data_buf[1] << 8 |
1692 data_buf[2] << 16 | data_buf[3] << 24;
1693 dwc2_writel(data, data_fifo);
1697 chan->xfer_count += byte_count;
1698 chan->xfer_buf += byte_count;
1702 * dwc2_hc_start_transfer() - Does the setup for a data transfer for a host
1703 * channel and starts the transfer
1705 * @hsotg: Programming view of DWC_otg controller
1706 * @chan: Information needed to initialize the host channel. The xfer_len value
1707 * may be reduced to accommodate the max widths of the XferSize and
1708 * PktCnt fields in the HCTSIZn register. The multi_count value may be
1709 * changed to reflect the final xfer_len value.
1711 * This function may be called in either Slave mode or DMA mode. In Slave mode,
1712 * the caller must ensure that there is sufficient space in the request queue
1715 * For an OUT transfer in Slave mode, it loads a data packet into the
1716 * appropriate FIFO. If necessary, additional data packets are loaded in the
1719 * For an IN transfer in Slave mode, a data packet is requested. The data
1720 * packets are unloaded from the Rx FIFO in the Host ISR. If necessary,
1721 * additional data packets are requested in the Host ISR.
1723 * For a PING transfer in Slave mode, the Do Ping bit is set in the HCTSIZ
1724 * register along with a packet count of 1 and the channel is enabled. This
1725 * causes a single PING transaction to occur. Other fields in HCTSIZ are
1726 * simply set to 0 since no data transfer occurs in this case.
1728 * For a PING transfer in DMA mode, the HCTSIZ register is initialized with
1729 * all the information required to perform the subsequent data transfer. In
1730 * addition, the Do Ping bit is set in the HCTSIZ register. In this case, the
1731 * controller performs the entire PING protocol, then starts the data
1734 void dwc2_hc_start_transfer(struct dwc2_hsotg *hsotg,
1735 struct dwc2_host_chan *chan)
1737 u32 max_hc_xfer_size = hsotg->core_params->max_transfer_size;
1738 u16 max_hc_pkt_count = hsotg->core_params->max_packet_count;
1745 dev_vdbg(hsotg->dev, "%s()\n", __func__);
1747 if (chan->do_ping) {
1748 if (hsotg->core_params->dma_enable <= 0) {
1750 dev_vdbg(hsotg->dev, "ping, no DMA\n");
1751 dwc2_hc_do_ping(hsotg, chan);
1752 chan->xfer_started = 1;
1756 dev_vdbg(hsotg->dev, "ping, DMA\n");
1757 hctsiz |= TSIZ_DOPNG;
1761 if (chan->do_split) {
1763 dev_vdbg(hsotg->dev, "split\n");
1766 if (chan->complete_split && !chan->ep_is_in)
1768 * For CSPLIT OUT Transfer, set the size to 0 so the
1769 * core doesn't expect any data written to the FIFO
1772 else if (chan->ep_is_in || chan->xfer_len > chan->max_packet)
1773 chan->xfer_len = chan->max_packet;
1774 else if (!chan->ep_is_in && chan->xfer_len > 188)
1775 chan->xfer_len = 188;
1777 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1780 /* For split set ec_mc for immediate retries */
1781 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1782 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1788 dev_vdbg(hsotg->dev, "no split\n");
1790 * Ensure that the transfer length and packet count will fit
1791 * in the widths allocated for them in the HCTSIZn register
1793 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1794 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
1796 * Make sure the transfer size is no larger than one
1797 * (micro)frame's worth of data. (A check was done
1798 * when the periodic transfer was accepted to ensure
1799 * that a (micro)frame's worth of data can be
1800 * programmed into a channel.)
1802 u32 max_periodic_len =
1803 chan->multi_count * chan->max_packet;
1805 if (chan->xfer_len > max_periodic_len)
1806 chan->xfer_len = max_periodic_len;
1807 } else if (chan->xfer_len > max_hc_xfer_size) {
1809 * Make sure that xfer_len is a multiple of max packet
1813 max_hc_xfer_size - chan->max_packet + 1;
1816 if (chan->xfer_len > 0) {
1817 num_packets = (chan->xfer_len + chan->max_packet - 1) /
1819 if (num_packets > max_hc_pkt_count) {
1820 num_packets = max_hc_pkt_count;
1821 chan->xfer_len = num_packets * chan->max_packet;
1824 /* Need 1 packet for transfer length of 0 */
1830 * Always program an integral # of max packets for IN
1833 chan->xfer_len = num_packets * chan->max_packet;
1835 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
1836 chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1838 * Make sure that the multi_count field matches the
1839 * actual transfer length
1841 chan->multi_count = num_packets;
1843 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1844 dwc2_set_pid_isoc(chan);
1846 hctsiz |= chan->xfer_len << TSIZ_XFERSIZE_SHIFT &
1849 /* The ec_mc gets the multi_count for non-split */
1850 ec_mc = chan->multi_count;
1853 chan->start_pkt_count = num_packets;
1854 hctsiz |= num_packets << TSIZ_PKTCNT_SHIFT & TSIZ_PKTCNT_MASK;
1855 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1856 TSIZ_SC_MC_PID_MASK;
1857 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1859 dev_vdbg(hsotg->dev, "Wrote %08x to HCTSIZ(%d)\n",
1860 hctsiz, chan->hc_num);
1862 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1864 dev_vdbg(hsotg->dev, " Xfer Size: %d\n",
1865 (hctsiz & TSIZ_XFERSIZE_MASK) >>
1866 TSIZ_XFERSIZE_SHIFT);
1867 dev_vdbg(hsotg->dev, " Num Pkts: %d\n",
1868 (hctsiz & TSIZ_PKTCNT_MASK) >>
1870 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1871 (hctsiz & TSIZ_SC_MC_PID_MASK) >>
1872 TSIZ_SC_MC_PID_SHIFT);
1875 if (hsotg->core_params->dma_enable > 0) {
1876 dma_addr_t dma_addr;
1878 if (chan->align_buf) {
1880 dev_vdbg(hsotg->dev, "align_buf\n");
1881 dma_addr = chan->align_buf;
1883 dma_addr = chan->xfer_dma;
1885 dwc2_writel((u32)dma_addr, hsotg->regs + HCDMA(chan->hc_num));
1887 dev_vdbg(hsotg->dev, "Wrote %08lx to HCDMA(%d)\n",
1888 (unsigned long)dma_addr, chan->hc_num);
1891 /* Start the split */
1892 if (chan->do_split) {
1893 u32 hcsplt = dwc2_readl(hsotg->regs + HCSPLT(chan->hc_num));
1895 hcsplt |= HCSPLT_SPLTENA;
1896 dwc2_writel(hcsplt, hsotg->regs + HCSPLT(chan->hc_num));
1899 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1900 hcchar &= ~HCCHAR_MULTICNT_MASK;
1901 hcchar |= (ec_mc << HCCHAR_MULTICNT_SHIFT) & HCCHAR_MULTICNT_MASK;
1902 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
1904 if (hcchar & HCCHAR_CHDIS)
1905 dev_warn(hsotg->dev,
1906 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1907 __func__, chan->hc_num, hcchar);
1909 /* Set host channel enable after all other setup is complete */
1910 hcchar |= HCCHAR_CHENA;
1911 hcchar &= ~HCCHAR_CHDIS;
1914 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
1915 (hcchar & HCCHAR_MULTICNT_MASK) >>
1916 HCCHAR_MULTICNT_SHIFT);
1918 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
1920 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
1923 chan->xfer_started = 1;
1926 if (hsotg->core_params->dma_enable <= 0 &&
1927 !chan->ep_is_in && chan->xfer_len > 0)
1928 /* Load OUT packet into the appropriate Tx FIFO */
1929 dwc2_hc_write_packet(hsotg, chan);
1933 * dwc2_hc_start_transfer_ddma() - Does the setup for a data transfer for a
1934 * host channel and starts the transfer in Descriptor DMA mode
1936 * @hsotg: Programming view of DWC_otg controller
1937 * @chan: Information needed to initialize the host channel
1939 * Initializes HCTSIZ register. For a PING transfer the Do Ping bit is set.
1940 * Sets PID and NTD values. For periodic transfers initializes SCHED_INFO field
1941 * with micro-frame bitmap.
1943 * Initializes HCDMA register with descriptor list address and CTD value then
1944 * starts the transfer via enabling the channel.
1946 void dwc2_hc_start_transfer_ddma(struct dwc2_hsotg *hsotg,
1947 struct dwc2_host_chan *chan)
1953 hctsiz |= TSIZ_DOPNG;
1955 if (chan->ep_type == USB_ENDPOINT_XFER_ISOC)
1956 dwc2_set_pid_isoc(chan);
1958 /* Packet Count and Xfer Size are not used in Descriptor DMA mode */
1959 hctsiz |= chan->data_pid_start << TSIZ_SC_MC_PID_SHIFT &
1960 TSIZ_SC_MC_PID_MASK;
1962 /* 0 - 1 descriptor, 1 - 2 descriptors, etc */
1963 hctsiz |= (chan->ntd - 1) << TSIZ_NTD_SHIFT & TSIZ_NTD_MASK;
1965 /* Non-zero only for high-speed interrupt endpoints */
1966 hctsiz |= chan->schinfo << TSIZ_SCHINFO_SHIFT & TSIZ_SCHINFO_MASK;
1969 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
1971 dev_vdbg(hsotg->dev, " Start PID: %d\n",
1972 chan->data_pid_start);
1973 dev_vdbg(hsotg->dev, " NTD: %d\n", chan->ntd - 1);
1976 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
1978 dma_sync_single_for_device(hsotg->dev, chan->desc_list_addr,
1979 chan->desc_list_sz, DMA_TO_DEVICE);
1981 dwc2_writel(chan->desc_list_addr, hsotg->regs + HCDMA(chan->hc_num));
1984 dev_vdbg(hsotg->dev, "Wrote %pad to HCDMA(%d)\n",
1985 &chan->desc_list_addr, chan->hc_num);
1987 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
1988 hcchar &= ~HCCHAR_MULTICNT_MASK;
1989 hcchar |= chan->multi_count << HCCHAR_MULTICNT_SHIFT &
1990 HCCHAR_MULTICNT_MASK;
1992 if (hcchar & HCCHAR_CHDIS)
1993 dev_warn(hsotg->dev,
1994 "%s: chdis set, channel %d, hcchar 0x%08x\n",
1995 __func__, chan->hc_num, hcchar);
1997 /* Set host channel enable after all other setup is complete */
1998 hcchar |= HCCHAR_CHENA;
1999 hcchar &= ~HCCHAR_CHDIS;
2002 dev_vdbg(hsotg->dev, " Multi Cnt: %d\n",
2003 (hcchar & HCCHAR_MULTICNT_MASK) >>
2004 HCCHAR_MULTICNT_SHIFT);
2006 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2008 dev_vdbg(hsotg->dev, "Wrote %08x to HCCHAR(%d)\n", hcchar,
2011 chan->xfer_started = 1;
2016 * dwc2_hc_continue_transfer() - Continues a data transfer that was started by
2017 * a previous call to dwc2_hc_start_transfer()
2019 * @hsotg: Programming view of DWC_otg controller
2020 * @chan: Information needed to initialize the host channel
2022 * The caller must ensure there is sufficient space in the request queue and Tx
2023 * Data FIFO. This function should only be called in Slave mode. In DMA mode,
2024 * the controller acts autonomously to complete transfers programmed to a host
2027 * For an OUT transfer, a new data packet is loaded into the appropriate FIFO
2028 * if there is any data remaining to be queued. For an IN transfer, another
2029 * data packet is always requested. For the SETUP phase of a control transfer,
2030 * this function does nothing.
2032 * Return: 1 if a new request is queued, 0 if no more requests are required
2035 int dwc2_hc_continue_transfer(struct dwc2_hsotg *hsotg,
2036 struct dwc2_host_chan *chan)
2039 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2043 /* SPLITs always queue just once per channel */
2046 if (chan->data_pid_start == DWC2_HC_PID_SETUP)
2047 /* SETUPs are queued only once since they can't be NAK'd */
2050 if (chan->ep_is_in) {
2052 * Always queue another request for other IN transfers. If
2053 * back-to-back INs are issued and NAKs are received for both,
2054 * the driver may still be processing the first NAK when the
2055 * second NAK is received. When the interrupt handler clears
2056 * the NAK interrupt for the first NAK, the second NAK will
2057 * not be seen. So we can't depend on the NAK interrupt
2058 * handler to requeue a NAK'd request. Instead, IN requests
2059 * are issued each time this function is called. When the
2060 * transfer completes, the extra requests for the channel will
2063 u32 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
2065 dwc2_hc_set_even_odd_frame(hsotg, chan, &hcchar);
2066 hcchar |= HCCHAR_CHENA;
2067 hcchar &= ~HCCHAR_CHDIS;
2069 dev_vdbg(hsotg->dev, " IN xfer: hcchar = 0x%08x\n",
2071 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2078 if (chan->xfer_count < chan->xfer_len) {
2079 if (chan->ep_type == USB_ENDPOINT_XFER_INT ||
2080 chan->ep_type == USB_ENDPOINT_XFER_ISOC) {
2081 u32 hcchar = dwc2_readl(hsotg->regs +
2082 HCCHAR(chan->hc_num));
2084 dwc2_hc_set_even_odd_frame(hsotg, chan,
2088 /* Load OUT packet into the appropriate Tx FIFO */
2089 dwc2_hc_write_packet(hsotg, chan);
2098 * dwc2_hc_do_ping() - Starts a PING transfer
2100 * @hsotg: Programming view of DWC_otg controller
2101 * @chan: Information needed to initialize the host channel
2103 * This function should only be called in Slave mode. The Do Ping bit is set in
2104 * the HCTSIZ register, then the channel is enabled.
2106 void dwc2_hc_do_ping(struct dwc2_hsotg *hsotg, struct dwc2_host_chan *chan)
2112 dev_vdbg(hsotg->dev, "%s: Channel %d\n", __func__,
2116 hctsiz = TSIZ_DOPNG;
2117 hctsiz |= 1 << TSIZ_PKTCNT_SHIFT;
2118 dwc2_writel(hctsiz, hsotg->regs + HCTSIZ(chan->hc_num));
2120 hcchar = dwc2_readl(hsotg->regs + HCCHAR(chan->hc_num));
2121 hcchar |= HCCHAR_CHENA;
2122 hcchar &= ~HCCHAR_CHDIS;
2123 dwc2_writel(hcchar, hsotg->regs + HCCHAR(chan->hc_num));
2127 * dwc2_calc_frame_interval() - Calculates the correct frame Interval value for
2128 * the HFIR register according to PHY type and speed
2130 * @hsotg: Programming view of DWC_otg controller
2132 * NOTE: The caller can modify the value of the HFIR register only after the
2133 * Port Enable bit of the Host Port Control and Status register (HPRT.EnaPort)
2136 u32 dwc2_calc_frame_interval(struct dwc2_hsotg *hsotg)
2140 int clock = 60; /* default value */
2142 usbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
2143 hprt0 = dwc2_readl(hsotg->regs + HPRT0);
2145 if (!(usbcfg & GUSBCFG_PHYSEL) && (usbcfg & GUSBCFG_ULPI_UTMI_SEL) &&
2146 !(usbcfg & GUSBCFG_PHYIF16))
2148 if ((usbcfg & GUSBCFG_PHYSEL) && hsotg->hw_params.fs_phy_type ==
2149 GHWCFG2_FS_PHY_TYPE_SHARED_ULPI)
2151 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2152 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2154 if (!(usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2155 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && !(usbcfg & GUSBCFG_PHYIF16))
2157 if ((usbcfg & GUSBCFG_PHY_LP_CLK_SEL) && !(usbcfg & GUSBCFG_PHYSEL) &&
2158 !(usbcfg & GUSBCFG_ULPI_UTMI_SEL) && (usbcfg & GUSBCFG_PHYIF16))
2160 if ((usbcfg & GUSBCFG_PHYSEL) && !(usbcfg & GUSBCFG_PHYIF16) &&
2161 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_SHARED_UTMI)
2163 if ((usbcfg & GUSBCFG_PHYSEL) &&
2164 hsotg->hw_params.fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2167 if ((hprt0 & HPRT0_SPD_MASK) >> HPRT0_SPD_SHIFT == HPRT0_SPD_HIGH_SPEED)
2168 /* High speed case */
2172 return 1000 * clock;
2176 * dwc2_read_packet() - Reads a packet from the Rx FIFO into the destination
2179 * @core_if: Programming view of DWC_otg controller
2180 * @dest: Destination buffer for the packet
2181 * @bytes: Number of bytes to copy to the destination
2183 void dwc2_read_packet(struct dwc2_hsotg *hsotg, u8 *dest, u16 bytes)
2185 u32 __iomem *fifo = hsotg->regs + HCFIFO(0);
2186 u32 *data_buf = (u32 *)dest;
2187 int word_count = (bytes + 3) / 4;
2191 * Todo: Account for the case where dest is not dword aligned. This
2192 * requires reading data from the FIFO into a u32 temp buffer, then
2193 * moving it into the data buffer.
2196 dev_vdbg(hsotg->dev, "%s(%p,%p,%d)\n", __func__, hsotg, dest, bytes);
2198 for (i = 0; i < word_count; i++, data_buf++)
2199 *data_buf = dwc2_readl(fifo);
2203 * dwc2_dump_host_registers() - Prints the host registers
2205 * @hsotg: Programming view of DWC_otg controller
2207 * NOTE: This function will be removed once the peripheral controller code
2208 * is integrated and the driver is stable
2210 void dwc2_dump_host_registers(struct dwc2_hsotg *hsotg)
2216 dev_dbg(hsotg->dev, "Host Global Registers\n");
2217 addr = hsotg->regs + HCFG;
2218 dev_dbg(hsotg->dev, "HCFG @0x%08lX : 0x%08X\n",
2219 (unsigned long)addr, dwc2_readl(addr));
2220 addr = hsotg->regs + HFIR;
2221 dev_dbg(hsotg->dev, "HFIR @0x%08lX : 0x%08X\n",
2222 (unsigned long)addr, dwc2_readl(addr));
2223 addr = hsotg->regs + HFNUM;
2224 dev_dbg(hsotg->dev, "HFNUM @0x%08lX : 0x%08X\n",
2225 (unsigned long)addr, dwc2_readl(addr));
2226 addr = hsotg->regs + HPTXSTS;
2227 dev_dbg(hsotg->dev, "HPTXSTS @0x%08lX : 0x%08X\n",
2228 (unsigned long)addr, dwc2_readl(addr));
2229 addr = hsotg->regs + HAINT;
2230 dev_dbg(hsotg->dev, "HAINT @0x%08lX : 0x%08X\n",
2231 (unsigned long)addr, dwc2_readl(addr));
2232 addr = hsotg->regs + HAINTMSK;
2233 dev_dbg(hsotg->dev, "HAINTMSK @0x%08lX : 0x%08X\n",
2234 (unsigned long)addr, dwc2_readl(addr));
2235 if (hsotg->core_params->dma_desc_enable > 0) {
2236 addr = hsotg->regs + HFLBADDR;
2237 dev_dbg(hsotg->dev, "HFLBADDR @0x%08lX : 0x%08X\n",
2238 (unsigned long)addr, dwc2_readl(addr));
2241 addr = hsotg->regs + HPRT0;
2242 dev_dbg(hsotg->dev, "HPRT0 @0x%08lX : 0x%08X\n",
2243 (unsigned long)addr, dwc2_readl(addr));
2245 for (i = 0; i < hsotg->core_params->host_channels; i++) {
2246 dev_dbg(hsotg->dev, "Host Channel %d Specific Registers\n", i);
2247 addr = hsotg->regs + HCCHAR(i);
2248 dev_dbg(hsotg->dev, "HCCHAR @0x%08lX : 0x%08X\n",
2249 (unsigned long)addr, dwc2_readl(addr));
2250 addr = hsotg->regs + HCSPLT(i);
2251 dev_dbg(hsotg->dev, "HCSPLT @0x%08lX : 0x%08X\n",
2252 (unsigned long)addr, dwc2_readl(addr));
2253 addr = hsotg->regs + HCINT(i);
2254 dev_dbg(hsotg->dev, "HCINT @0x%08lX : 0x%08X\n",
2255 (unsigned long)addr, dwc2_readl(addr));
2256 addr = hsotg->regs + HCINTMSK(i);
2257 dev_dbg(hsotg->dev, "HCINTMSK @0x%08lX : 0x%08X\n",
2258 (unsigned long)addr, dwc2_readl(addr));
2259 addr = hsotg->regs + HCTSIZ(i);
2260 dev_dbg(hsotg->dev, "HCTSIZ @0x%08lX : 0x%08X\n",
2261 (unsigned long)addr, dwc2_readl(addr));
2262 addr = hsotg->regs + HCDMA(i);
2263 dev_dbg(hsotg->dev, "HCDMA @0x%08lX : 0x%08X\n",
2264 (unsigned long)addr, dwc2_readl(addr));
2265 if (hsotg->core_params->dma_desc_enable > 0) {
2266 addr = hsotg->regs + HCDMAB(i);
2267 dev_dbg(hsotg->dev, "HCDMAB @0x%08lX : 0x%08X\n",
2268 (unsigned long)addr, dwc2_readl(addr));
2275 * dwc2_dump_global_registers() - Prints the core global registers
2277 * @hsotg: Programming view of DWC_otg controller
2279 * NOTE: This function will be removed once the peripheral controller code
2280 * is integrated and the driver is stable
2282 void dwc2_dump_global_registers(struct dwc2_hsotg *hsotg)
2287 dev_dbg(hsotg->dev, "Core Global Registers\n");
2288 addr = hsotg->regs + GOTGCTL;
2289 dev_dbg(hsotg->dev, "GOTGCTL @0x%08lX : 0x%08X\n",
2290 (unsigned long)addr, dwc2_readl(addr));
2291 addr = hsotg->regs + GOTGINT;
2292 dev_dbg(hsotg->dev, "GOTGINT @0x%08lX : 0x%08X\n",
2293 (unsigned long)addr, dwc2_readl(addr));
2294 addr = hsotg->regs + GAHBCFG;
2295 dev_dbg(hsotg->dev, "GAHBCFG @0x%08lX : 0x%08X\n",
2296 (unsigned long)addr, dwc2_readl(addr));
2297 addr = hsotg->regs + GUSBCFG;
2298 dev_dbg(hsotg->dev, "GUSBCFG @0x%08lX : 0x%08X\n",
2299 (unsigned long)addr, dwc2_readl(addr));
2300 addr = hsotg->regs + GRSTCTL;
2301 dev_dbg(hsotg->dev, "GRSTCTL @0x%08lX : 0x%08X\n",
2302 (unsigned long)addr, dwc2_readl(addr));
2303 addr = hsotg->regs + GINTSTS;
2304 dev_dbg(hsotg->dev, "GINTSTS @0x%08lX : 0x%08X\n",
2305 (unsigned long)addr, dwc2_readl(addr));
2306 addr = hsotg->regs + GINTMSK;
2307 dev_dbg(hsotg->dev, "GINTMSK @0x%08lX : 0x%08X\n",
2308 (unsigned long)addr, dwc2_readl(addr));
2309 addr = hsotg->regs + GRXSTSR;
2310 dev_dbg(hsotg->dev, "GRXSTSR @0x%08lX : 0x%08X\n",
2311 (unsigned long)addr, dwc2_readl(addr));
2312 addr = hsotg->regs + GRXFSIZ;
2313 dev_dbg(hsotg->dev, "GRXFSIZ @0x%08lX : 0x%08X\n",
2314 (unsigned long)addr, dwc2_readl(addr));
2315 addr = hsotg->regs + GNPTXFSIZ;
2316 dev_dbg(hsotg->dev, "GNPTXFSIZ @0x%08lX : 0x%08X\n",
2317 (unsigned long)addr, dwc2_readl(addr));
2318 addr = hsotg->regs + GNPTXSTS;
2319 dev_dbg(hsotg->dev, "GNPTXSTS @0x%08lX : 0x%08X\n",
2320 (unsigned long)addr, dwc2_readl(addr));
2321 addr = hsotg->regs + GI2CCTL;
2322 dev_dbg(hsotg->dev, "GI2CCTL @0x%08lX : 0x%08X\n",
2323 (unsigned long)addr, dwc2_readl(addr));
2324 addr = hsotg->regs + GPVNDCTL;
2325 dev_dbg(hsotg->dev, "GPVNDCTL @0x%08lX : 0x%08X\n",
2326 (unsigned long)addr, dwc2_readl(addr));
2327 addr = hsotg->regs + GGPIO;
2328 dev_dbg(hsotg->dev, "GGPIO @0x%08lX : 0x%08X\n",
2329 (unsigned long)addr, dwc2_readl(addr));
2330 addr = hsotg->regs + GUID;
2331 dev_dbg(hsotg->dev, "GUID @0x%08lX : 0x%08X\n",
2332 (unsigned long)addr, dwc2_readl(addr));
2333 addr = hsotg->regs + GSNPSID;
2334 dev_dbg(hsotg->dev, "GSNPSID @0x%08lX : 0x%08X\n",
2335 (unsigned long)addr, dwc2_readl(addr));
2336 addr = hsotg->regs + GHWCFG1;
2337 dev_dbg(hsotg->dev, "GHWCFG1 @0x%08lX : 0x%08X\n",
2338 (unsigned long)addr, dwc2_readl(addr));
2339 addr = hsotg->regs + GHWCFG2;
2340 dev_dbg(hsotg->dev, "GHWCFG2 @0x%08lX : 0x%08X\n",
2341 (unsigned long)addr, dwc2_readl(addr));
2342 addr = hsotg->regs + GHWCFG3;
2343 dev_dbg(hsotg->dev, "GHWCFG3 @0x%08lX : 0x%08X\n",
2344 (unsigned long)addr, dwc2_readl(addr));
2345 addr = hsotg->regs + GHWCFG4;
2346 dev_dbg(hsotg->dev, "GHWCFG4 @0x%08lX : 0x%08X\n",
2347 (unsigned long)addr, dwc2_readl(addr));
2348 addr = hsotg->regs + GLPMCFG;
2349 dev_dbg(hsotg->dev, "GLPMCFG @0x%08lX : 0x%08X\n",
2350 (unsigned long)addr, dwc2_readl(addr));
2351 addr = hsotg->regs + GPWRDN;
2352 dev_dbg(hsotg->dev, "GPWRDN @0x%08lX : 0x%08X\n",
2353 (unsigned long)addr, dwc2_readl(addr));
2354 addr = hsotg->regs + GDFIFOCFG;
2355 dev_dbg(hsotg->dev, "GDFIFOCFG @0x%08lX : 0x%08X\n",
2356 (unsigned long)addr, dwc2_readl(addr));
2357 addr = hsotg->regs + HPTXFSIZ;
2358 dev_dbg(hsotg->dev, "HPTXFSIZ @0x%08lX : 0x%08X\n",
2359 (unsigned long)addr, dwc2_readl(addr));
2361 addr = hsotg->regs + PCGCTL;
2362 dev_dbg(hsotg->dev, "PCGCTL @0x%08lX : 0x%08X\n",
2363 (unsigned long)addr, dwc2_readl(addr));
2368 * dwc2_flush_tx_fifo() - Flushes a Tx FIFO
2370 * @hsotg: Programming view of DWC_otg controller
2371 * @num: Tx FIFO to flush
2373 void dwc2_flush_tx_fifo(struct dwc2_hsotg *hsotg, const int num)
2378 dev_vdbg(hsotg->dev, "Flush Tx FIFO %d\n", num);
2380 greset = GRSTCTL_TXFFLSH;
2381 greset |= num << GRSTCTL_TXFNUM_SHIFT & GRSTCTL_TXFNUM_MASK;
2382 dwc2_writel(greset, hsotg->regs + GRSTCTL);
2385 greset = dwc2_readl(hsotg->regs + GRSTCTL);
2386 if (++count > 10000) {
2387 dev_warn(hsotg->dev,
2388 "%s() HANG! GRSTCTL=%0x GNPTXSTS=0x%08x\n",
2390 dwc2_readl(hsotg->regs + GNPTXSTS));
2394 } while (greset & GRSTCTL_TXFFLSH);
2396 /* Wait for at least 3 PHY Clocks */
2401 * dwc2_flush_rx_fifo() - Flushes the Rx FIFO
2403 * @hsotg: Programming view of DWC_otg controller
2405 void dwc2_flush_rx_fifo(struct dwc2_hsotg *hsotg)
2410 dev_vdbg(hsotg->dev, "%s()\n", __func__);
2412 greset = GRSTCTL_RXFFLSH;
2413 dwc2_writel(greset, hsotg->regs + GRSTCTL);
2416 greset = dwc2_readl(hsotg->regs + GRSTCTL);
2417 if (++count > 10000) {
2418 dev_warn(hsotg->dev, "%s() HANG! GRSTCTL=%0x\n",
2423 } while (greset & GRSTCTL_RXFFLSH);
2425 /* Wait for at least 3 PHY Clocks */
2429 #define DWC2_OUT_OF_BOUNDS(a, b, c) ((a) < (b) || (a) > (c))
2431 /* Parameter access functions */
2432 void dwc2_set_param_otg_cap(struct dwc2_hsotg *hsotg, int val)
2437 case DWC2_CAP_PARAM_HNP_SRP_CAPABLE:
2438 if (hsotg->hw_params.op_mode != GHWCFG2_OP_MODE_HNP_SRP_CAPABLE)
2441 case DWC2_CAP_PARAM_SRP_ONLY_CAPABLE:
2442 switch (hsotg->hw_params.op_mode) {
2443 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2444 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2445 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2446 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2453 case DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE:
2464 "%d invalid for otg_cap parameter. Check HW configuration.\n",
2466 switch (hsotg->hw_params.op_mode) {
2467 case GHWCFG2_OP_MODE_HNP_SRP_CAPABLE:
2468 val = DWC2_CAP_PARAM_HNP_SRP_CAPABLE;
2470 case GHWCFG2_OP_MODE_SRP_ONLY_CAPABLE:
2471 case GHWCFG2_OP_MODE_SRP_CAPABLE_DEVICE:
2472 case GHWCFG2_OP_MODE_SRP_CAPABLE_HOST:
2473 val = DWC2_CAP_PARAM_SRP_ONLY_CAPABLE;
2476 val = DWC2_CAP_PARAM_NO_HNP_SRP_CAPABLE;
2479 dev_dbg(hsotg->dev, "Setting otg_cap to %d\n", val);
2482 hsotg->core_params->otg_cap = val;
2485 void dwc2_set_param_dma_enable(struct dwc2_hsotg *hsotg, int val)
2489 if (val > 0 && hsotg->hw_params.arch == GHWCFG2_SLAVE_ONLY_ARCH)
2497 "%d invalid for dma_enable parameter. Check HW configuration.\n",
2499 val = hsotg->hw_params.arch != GHWCFG2_SLAVE_ONLY_ARCH;
2500 dev_dbg(hsotg->dev, "Setting dma_enable to %d\n", val);
2503 hsotg->core_params->dma_enable = val;
2506 void dwc2_set_param_dma_desc_enable(struct dwc2_hsotg *hsotg, int val)
2510 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
2511 !hsotg->hw_params.dma_desc_enable))
2519 "%d invalid for dma_desc_enable parameter. Check HW configuration.\n",
2521 val = (hsotg->core_params->dma_enable > 0 &&
2522 hsotg->hw_params.dma_desc_enable);
2523 dev_dbg(hsotg->dev, "Setting dma_desc_enable to %d\n", val);
2526 hsotg->core_params->dma_desc_enable = val;
2529 void dwc2_set_param_dma_desc_fs_enable(struct dwc2_hsotg *hsotg, int val)
2533 if (val > 0 && (hsotg->core_params->dma_enable <= 0 ||
2534 !hsotg->hw_params.dma_desc_enable))
2542 "%d invalid for dma_desc_fs_enable parameter. Check HW configuration.\n",
2544 val = (hsotg->core_params->dma_enable > 0 &&
2545 hsotg->hw_params.dma_desc_enable);
2548 hsotg->core_params->dma_desc_fs_enable = val;
2549 dev_dbg(hsotg->dev, "Setting dma_desc_fs_enable to %d\n", val);
2552 void dwc2_set_param_host_support_fs_ls_low_power(struct dwc2_hsotg *hsotg,
2555 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2558 "Wrong value for host_support_fs_low_power\n");
2560 "host_support_fs_low_power must be 0 or 1\n");
2564 "Setting host_support_fs_low_power to %d\n", val);
2567 hsotg->core_params->host_support_fs_ls_low_power = val;
2570 void dwc2_set_param_enable_dynamic_fifo(struct dwc2_hsotg *hsotg, int val)
2574 if (val > 0 && !hsotg->hw_params.enable_dynamic_fifo)
2582 "%d invalid for enable_dynamic_fifo parameter. Check HW configuration.\n",
2584 val = hsotg->hw_params.enable_dynamic_fifo;
2585 dev_dbg(hsotg->dev, "Setting enable_dynamic_fifo to %d\n", val);
2588 hsotg->core_params->enable_dynamic_fifo = val;
2591 void dwc2_set_param_host_rx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2595 if (val < 16 || val > hsotg->hw_params.host_rx_fifo_size)
2601 "%d invalid for host_rx_fifo_size. Check HW configuration.\n",
2603 val = hsotg->hw_params.host_rx_fifo_size;
2604 dev_dbg(hsotg->dev, "Setting host_rx_fifo_size to %d\n", val);
2607 hsotg->core_params->host_rx_fifo_size = val;
2610 void dwc2_set_param_host_nperio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2614 if (val < 16 || val > hsotg->hw_params.host_nperio_tx_fifo_size)
2620 "%d invalid for host_nperio_tx_fifo_size. Check HW configuration.\n",
2622 val = hsotg->hw_params.host_nperio_tx_fifo_size;
2623 dev_dbg(hsotg->dev, "Setting host_nperio_tx_fifo_size to %d\n",
2627 hsotg->core_params->host_nperio_tx_fifo_size = val;
2630 void dwc2_set_param_host_perio_tx_fifo_size(struct dwc2_hsotg *hsotg, int val)
2634 if (val < 16 || val > hsotg->hw_params.host_perio_tx_fifo_size)
2640 "%d invalid for host_perio_tx_fifo_size. Check HW configuration.\n",
2642 val = hsotg->hw_params.host_perio_tx_fifo_size;
2643 dev_dbg(hsotg->dev, "Setting host_perio_tx_fifo_size to %d\n",
2647 hsotg->core_params->host_perio_tx_fifo_size = val;
2650 void dwc2_set_param_max_transfer_size(struct dwc2_hsotg *hsotg, int val)
2654 if (val < 2047 || val > hsotg->hw_params.max_transfer_size)
2660 "%d invalid for max_transfer_size. Check HW configuration.\n",
2662 val = hsotg->hw_params.max_transfer_size;
2663 dev_dbg(hsotg->dev, "Setting max_transfer_size to %d\n", val);
2666 hsotg->core_params->max_transfer_size = val;
2669 void dwc2_set_param_max_packet_count(struct dwc2_hsotg *hsotg, int val)
2673 if (val < 15 || val > hsotg->hw_params.max_packet_count)
2679 "%d invalid for max_packet_count. Check HW configuration.\n",
2681 val = hsotg->hw_params.max_packet_count;
2682 dev_dbg(hsotg->dev, "Setting max_packet_count to %d\n", val);
2685 hsotg->core_params->max_packet_count = val;
2688 void dwc2_set_param_host_channels(struct dwc2_hsotg *hsotg, int val)
2692 if (val < 1 || val > hsotg->hw_params.host_channels)
2698 "%d invalid for host_channels. Check HW configuration.\n",
2700 val = hsotg->hw_params.host_channels;
2701 dev_dbg(hsotg->dev, "Setting host_channels to %d\n", val);
2704 hsotg->core_params->host_channels = val;
2707 void dwc2_set_param_phy_type(struct dwc2_hsotg *hsotg, int val)
2710 u32 hs_phy_type, fs_phy_type;
2712 if (DWC2_OUT_OF_BOUNDS(val, DWC2_PHY_TYPE_PARAM_FS,
2713 DWC2_PHY_TYPE_PARAM_ULPI)) {
2715 dev_err(hsotg->dev, "Wrong value for phy_type\n");
2716 dev_err(hsotg->dev, "phy_type must be 0, 1 or 2\n");
2722 hs_phy_type = hsotg->hw_params.hs_phy_type;
2723 fs_phy_type = hsotg->hw_params.fs_phy_type;
2724 if (val == DWC2_PHY_TYPE_PARAM_UTMI &&
2725 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2726 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2728 else if (val == DWC2_PHY_TYPE_PARAM_ULPI &&
2729 (hs_phy_type == GHWCFG2_HS_PHY_TYPE_ULPI ||
2730 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI))
2732 else if (val == DWC2_PHY_TYPE_PARAM_FS &&
2733 fs_phy_type == GHWCFG2_FS_PHY_TYPE_DEDICATED)
2739 "%d invalid for phy_type. Check HW configuration.\n",
2741 val = DWC2_PHY_TYPE_PARAM_FS;
2742 if (hs_phy_type != GHWCFG2_HS_PHY_TYPE_NOT_SUPPORTED) {
2743 if (hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI ||
2744 hs_phy_type == GHWCFG2_HS_PHY_TYPE_UTMI_ULPI)
2745 val = DWC2_PHY_TYPE_PARAM_UTMI;
2747 val = DWC2_PHY_TYPE_PARAM_ULPI;
2749 dev_dbg(hsotg->dev, "Setting phy_type to %d\n", val);
2752 hsotg->core_params->phy_type = val;
2755 static int dwc2_get_param_phy_type(struct dwc2_hsotg *hsotg)
2757 return hsotg->core_params->phy_type;
2760 void dwc2_set_param_speed(struct dwc2_hsotg *hsotg, int val)
2764 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2766 dev_err(hsotg->dev, "Wrong value for speed parameter\n");
2767 dev_err(hsotg->dev, "max_speed parameter must be 0 or 1\n");
2772 if (val == DWC2_SPEED_PARAM_HIGH &&
2773 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2779 "%d invalid for speed parameter. Check HW configuration.\n",
2781 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS ?
2782 DWC2_SPEED_PARAM_FULL : DWC2_SPEED_PARAM_HIGH;
2783 dev_dbg(hsotg->dev, "Setting speed to %d\n", val);
2786 hsotg->core_params->speed = val;
2789 void dwc2_set_param_host_ls_low_power_phy_clk(struct dwc2_hsotg *hsotg, int val)
2793 if (DWC2_OUT_OF_BOUNDS(val, DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ,
2794 DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ)) {
2797 "Wrong value for host_ls_low_power_phy_clk parameter\n");
2799 "host_ls_low_power_phy_clk must be 0 or 1\n");
2804 if (val == DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ &&
2805 dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS)
2811 "%d invalid for host_ls_low_power_phy_clk. Check HW configuration.\n",
2813 val = dwc2_get_param_phy_type(hsotg) == DWC2_PHY_TYPE_PARAM_FS
2814 ? DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_6MHZ
2815 : DWC2_HOST_LS_LOW_POWER_PHY_CLK_PARAM_48MHZ;
2816 dev_dbg(hsotg->dev, "Setting host_ls_low_power_phy_clk to %d\n",
2820 hsotg->core_params->host_ls_low_power_phy_clk = val;
2823 void dwc2_set_param_phy_ulpi_ddr(struct dwc2_hsotg *hsotg, int val)
2825 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2827 dev_err(hsotg->dev, "Wrong value for phy_ulpi_ddr\n");
2828 dev_err(hsotg->dev, "phy_upli_ddr must be 0 or 1\n");
2831 dev_dbg(hsotg->dev, "Setting phy_upli_ddr to %d\n", val);
2834 hsotg->core_params->phy_ulpi_ddr = val;
2837 void dwc2_set_param_phy_ulpi_ext_vbus(struct dwc2_hsotg *hsotg, int val)
2839 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2842 "Wrong value for phy_ulpi_ext_vbus\n");
2844 "phy_ulpi_ext_vbus must be 0 or 1\n");
2847 dev_dbg(hsotg->dev, "Setting phy_ulpi_ext_vbus to %d\n", val);
2850 hsotg->core_params->phy_ulpi_ext_vbus = val;
2853 void dwc2_set_param_phy_utmi_width(struct dwc2_hsotg *hsotg, int val)
2857 switch (hsotg->hw_params.utmi_phy_data_width) {
2858 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8:
2861 case GHWCFG4_UTMI_PHY_DATA_WIDTH_16:
2862 valid = (val == 16);
2864 case GHWCFG4_UTMI_PHY_DATA_WIDTH_8_OR_16:
2865 valid = (val == 8 || val == 16);
2872 "%d invalid for phy_utmi_width. Check HW configuration.\n",
2875 val = (hsotg->hw_params.utmi_phy_data_width ==
2876 GHWCFG4_UTMI_PHY_DATA_WIDTH_8) ? 8 : 16;
2877 dev_dbg(hsotg->dev, "Setting phy_utmi_width to %d\n", val);
2880 hsotg->core_params->phy_utmi_width = val;
2883 void dwc2_set_param_ulpi_fs_ls(struct dwc2_hsotg *hsotg, int val)
2885 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2887 dev_err(hsotg->dev, "Wrong value for ulpi_fs_ls\n");
2888 dev_err(hsotg->dev, "ulpi_fs_ls must be 0 or 1\n");
2891 dev_dbg(hsotg->dev, "Setting ulpi_fs_ls to %d\n", val);
2894 hsotg->core_params->ulpi_fs_ls = val;
2897 void dwc2_set_param_ts_dline(struct dwc2_hsotg *hsotg, int val)
2899 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2901 dev_err(hsotg->dev, "Wrong value for ts_dline\n");
2902 dev_err(hsotg->dev, "ts_dline must be 0 or 1\n");
2905 dev_dbg(hsotg->dev, "Setting ts_dline to %d\n", val);
2908 hsotg->core_params->ts_dline = val;
2911 void dwc2_set_param_i2c_enable(struct dwc2_hsotg *hsotg, int val)
2915 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2917 dev_err(hsotg->dev, "Wrong value for i2c_enable\n");
2918 dev_err(hsotg->dev, "i2c_enable must be 0 or 1\n");
2924 if (val == 1 && !(hsotg->hw_params.i2c_enable))
2930 "%d invalid for i2c_enable. Check HW configuration.\n",
2932 val = hsotg->hw_params.i2c_enable;
2933 dev_dbg(hsotg->dev, "Setting i2c_enable to %d\n", val);
2936 hsotg->core_params->i2c_enable = val;
2939 void dwc2_set_param_en_multiple_tx_fifo(struct dwc2_hsotg *hsotg, int val)
2943 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2946 "Wrong value for en_multiple_tx_fifo,\n");
2948 "en_multiple_tx_fifo must be 0 or 1\n");
2953 if (val == 1 && !hsotg->hw_params.en_multiple_tx_fifo)
2959 "%d invalid for parameter en_multiple_tx_fifo. Check HW configuration.\n",
2961 val = hsotg->hw_params.en_multiple_tx_fifo;
2962 dev_dbg(hsotg->dev, "Setting en_multiple_tx_fifo to %d\n", val);
2965 hsotg->core_params->en_multiple_tx_fifo = val;
2968 void dwc2_set_param_reload_ctl(struct dwc2_hsotg *hsotg, int val)
2972 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
2975 "'%d' invalid for parameter reload_ctl\n", val);
2976 dev_err(hsotg->dev, "reload_ctl must be 0 or 1\n");
2981 if (val == 1 && hsotg->hw_params.snpsid < DWC2_CORE_REV_2_92a)
2987 "%d invalid for parameter reload_ctl. Check HW configuration.\n",
2989 val = hsotg->hw_params.snpsid >= DWC2_CORE_REV_2_92a;
2990 dev_dbg(hsotg->dev, "Setting reload_ctl to %d\n", val);
2993 hsotg->core_params->reload_ctl = val;
2996 void dwc2_set_param_ahbcfg(struct dwc2_hsotg *hsotg, int val)
2999 hsotg->core_params->ahbcfg = val;
3001 hsotg->core_params->ahbcfg = GAHBCFG_HBSTLEN_INCR4 <<
3002 GAHBCFG_HBSTLEN_SHIFT;
3005 void dwc2_set_param_otg_ver(struct dwc2_hsotg *hsotg, int val)
3007 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3010 "'%d' invalid for parameter otg_ver\n", val);
3012 "otg_ver must be 0 (for OTG 1.3 support) or 1 (for OTG 2.0 support)\n");
3015 dev_dbg(hsotg->dev, "Setting otg_ver to %d\n", val);
3018 hsotg->core_params->otg_ver = val;
3021 static void dwc2_set_param_uframe_sched(struct dwc2_hsotg *hsotg, int val)
3023 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3026 "'%d' invalid for parameter uframe_sched\n",
3028 dev_err(hsotg->dev, "uframe_sched must be 0 or 1\n");
3031 dev_dbg(hsotg->dev, "Setting uframe_sched to %d\n", val);
3034 hsotg->core_params->uframe_sched = val;
3037 static void dwc2_set_param_external_id_pin_ctl(struct dwc2_hsotg *hsotg,
3040 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3043 "'%d' invalid for parameter external_id_pin_ctl\n",
3045 dev_err(hsotg->dev, "external_id_pin_ctl must be 0 or 1\n");
3048 dev_dbg(hsotg->dev, "Setting external_id_pin_ctl to %d\n", val);
3051 hsotg->core_params->external_id_pin_ctl = val;
3054 static void dwc2_set_param_hibernation(struct dwc2_hsotg *hsotg,
3057 if (DWC2_OUT_OF_BOUNDS(val, 0, 1)) {
3060 "'%d' invalid for parameter hibernation\n",
3062 dev_err(hsotg->dev, "hibernation must be 0 or 1\n");
3065 dev_dbg(hsotg->dev, "Setting hibernation to %d\n", val);
3068 hsotg->core_params->hibernation = val;
3072 * This function is called during module intialization to pass module parameters
3073 * for the DWC_otg core.
3075 void dwc2_set_parameters(struct dwc2_hsotg *hsotg,
3076 const struct dwc2_core_params *params)
3078 dev_dbg(hsotg->dev, "%s()\n", __func__);
3080 dwc2_set_param_otg_cap(hsotg, params->otg_cap);
3081 dwc2_set_param_dma_enable(hsotg, params->dma_enable);
3082 dwc2_set_param_dma_desc_enable(hsotg, params->dma_desc_enable);
3083 dwc2_set_param_dma_desc_fs_enable(hsotg, params->dma_desc_fs_enable);
3084 dwc2_set_param_host_support_fs_ls_low_power(hsotg,
3085 params->host_support_fs_ls_low_power);
3086 dwc2_set_param_enable_dynamic_fifo(hsotg,
3087 params->enable_dynamic_fifo);
3088 dwc2_set_param_host_rx_fifo_size(hsotg,
3089 params->host_rx_fifo_size);
3090 dwc2_set_param_host_nperio_tx_fifo_size(hsotg,
3091 params->host_nperio_tx_fifo_size);
3092 dwc2_set_param_host_perio_tx_fifo_size(hsotg,
3093 params->host_perio_tx_fifo_size);
3094 dwc2_set_param_max_transfer_size(hsotg,
3095 params->max_transfer_size);
3096 dwc2_set_param_max_packet_count(hsotg,
3097 params->max_packet_count);
3098 dwc2_set_param_host_channels(hsotg, params->host_channels);
3099 dwc2_set_param_phy_type(hsotg, params->phy_type);
3100 dwc2_set_param_speed(hsotg, params->speed);
3101 dwc2_set_param_host_ls_low_power_phy_clk(hsotg,
3102 params->host_ls_low_power_phy_clk);
3103 dwc2_set_param_phy_ulpi_ddr(hsotg, params->phy_ulpi_ddr);
3104 dwc2_set_param_phy_ulpi_ext_vbus(hsotg,
3105 params->phy_ulpi_ext_vbus);
3106 dwc2_set_param_phy_utmi_width(hsotg, params->phy_utmi_width);
3107 dwc2_set_param_ulpi_fs_ls(hsotg, params->ulpi_fs_ls);
3108 dwc2_set_param_ts_dline(hsotg, params->ts_dline);
3109 dwc2_set_param_i2c_enable(hsotg, params->i2c_enable);
3110 dwc2_set_param_en_multiple_tx_fifo(hsotg,
3111 params->en_multiple_tx_fifo);
3112 dwc2_set_param_reload_ctl(hsotg, params->reload_ctl);
3113 dwc2_set_param_ahbcfg(hsotg, params->ahbcfg);
3114 dwc2_set_param_otg_ver(hsotg, params->otg_ver);
3115 dwc2_set_param_uframe_sched(hsotg, params->uframe_sched);
3116 dwc2_set_param_external_id_pin_ctl(hsotg, params->external_id_pin_ctl);
3117 dwc2_set_param_hibernation(hsotg, params->hibernation);
3121 * During device initialization, read various hardware configuration
3122 * registers and interpret the contents.
3124 int dwc2_get_hwparams(struct dwc2_hsotg *hsotg)
3126 struct dwc2_hw_params *hw = &hsotg->hw_params;
3128 u32 hwcfg1, hwcfg2, hwcfg3, hwcfg4;
3129 u32 hptxfsiz, grxfsiz, gnptxfsiz;
3133 * Attempt to ensure this device is really a DWC_otg Controller.
3134 * Read and verify the GSNPSID register contents. The value should be
3135 * 0x45f42xxx or 0x45f43xxx, which corresponds to either "OT2" or "OT3",
3136 * as in "OTG version 2.xx" or "OTG version 3.xx".
3138 hw->snpsid = dwc2_readl(hsotg->regs + GSNPSID);
3139 if ((hw->snpsid & 0xfffff000) != 0x4f542000 &&
3140 (hw->snpsid & 0xfffff000) != 0x4f543000) {
3141 dev_err(hsotg->dev, "Bad value for GSNPSID: 0x%08x\n",
3146 dev_dbg(hsotg->dev, "Core Release: %1x.%1x%1x%1x (snpsid=%x)\n",
3147 hw->snpsid >> 12 & 0xf, hw->snpsid >> 8 & 0xf,
3148 hw->snpsid >> 4 & 0xf, hw->snpsid & 0xf, hw->snpsid);
3150 hwcfg1 = dwc2_readl(hsotg->regs + GHWCFG1);
3151 hwcfg2 = dwc2_readl(hsotg->regs + GHWCFG2);
3152 hwcfg3 = dwc2_readl(hsotg->regs + GHWCFG3);
3153 hwcfg4 = dwc2_readl(hsotg->regs + GHWCFG4);
3154 grxfsiz = dwc2_readl(hsotg->regs + GRXFSIZ);
3156 dev_dbg(hsotg->dev, "hwcfg1=%08x\n", hwcfg1);
3157 dev_dbg(hsotg->dev, "hwcfg2=%08x\n", hwcfg2);
3158 dev_dbg(hsotg->dev, "hwcfg3=%08x\n", hwcfg3);
3159 dev_dbg(hsotg->dev, "hwcfg4=%08x\n", hwcfg4);
3160 dev_dbg(hsotg->dev, "grxfsiz=%08x\n", grxfsiz);
3162 /* Force host mode to get HPTXFSIZ / GNPTXFSIZ exact power on value */
3163 if (hsotg->dr_mode != USB_DR_MODE_HOST) {
3164 gusbcfg = dwc2_readl(hsotg->regs + GUSBCFG);
3165 dwc2_writel(gusbcfg | GUSBCFG_FORCEHOSTMODE,
3166 hsotg->regs + GUSBCFG);
3167 usleep_range(25000, 50000);
3170 gnptxfsiz = dwc2_readl(hsotg->regs + GNPTXFSIZ);
3171 hptxfsiz = dwc2_readl(hsotg->regs + HPTXFSIZ);
3172 dev_dbg(hsotg->dev, "gnptxfsiz=%08x\n", gnptxfsiz);
3173 dev_dbg(hsotg->dev, "hptxfsiz=%08x\n", hptxfsiz);
3174 if (hsotg->dr_mode != USB_DR_MODE_HOST) {
3175 dwc2_writel(gusbcfg, hsotg->regs + GUSBCFG);
3176 usleep_range(25000, 50000);
3180 hw->op_mode = (hwcfg2 & GHWCFG2_OP_MODE_MASK) >>
3181 GHWCFG2_OP_MODE_SHIFT;
3182 hw->arch = (hwcfg2 & GHWCFG2_ARCHITECTURE_MASK) >>
3183 GHWCFG2_ARCHITECTURE_SHIFT;
3184 hw->enable_dynamic_fifo = !!(hwcfg2 & GHWCFG2_DYNAMIC_FIFO);
3185 hw->host_channels = 1 + ((hwcfg2 & GHWCFG2_NUM_HOST_CHAN_MASK) >>
3186 GHWCFG2_NUM_HOST_CHAN_SHIFT);
3187 hw->hs_phy_type = (hwcfg2 & GHWCFG2_HS_PHY_TYPE_MASK) >>
3188 GHWCFG2_HS_PHY_TYPE_SHIFT;
3189 hw->fs_phy_type = (hwcfg2 & GHWCFG2_FS_PHY_TYPE_MASK) >>
3190 GHWCFG2_FS_PHY_TYPE_SHIFT;
3191 hw->num_dev_ep = (hwcfg2 & GHWCFG2_NUM_DEV_EP_MASK) >>
3192 GHWCFG2_NUM_DEV_EP_SHIFT;
3193 hw->nperio_tx_q_depth =
3194 (hwcfg2 & GHWCFG2_NONPERIO_TX_Q_DEPTH_MASK) >>
3195 GHWCFG2_NONPERIO_TX_Q_DEPTH_SHIFT << 1;
3196 hw->host_perio_tx_q_depth =
3197 (hwcfg2 & GHWCFG2_HOST_PERIO_TX_Q_DEPTH_MASK) >>
3198 GHWCFG2_HOST_PERIO_TX_Q_DEPTH_SHIFT << 1;
3199 hw->dev_token_q_depth =
3200 (hwcfg2 & GHWCFG2_DEV_TOKEN_Q_DEPTH_MASK) >>
3201 GHWCFG2_DEV_TOKEN_Q_DEPTH_SHIFT;
3204 width = (hwcfg3 & GHWCFG3_XFER_SIZE_CNTR_WIDTH_MASK) >>
3205 GHWCFG3_XFER_SIZE_CNTR_WIDTH_SHIFT;
3206 hw->max_transfer_size = (1 << (width + 11)) - 1;
3208 * Clip max_transfer_size to 65535. dwc2_hc_setup_align_buf() allocates
3209 * coherent buffers with this size, and if it's too large we can
3210 * exhaust the coherent DMA pool.
3212 if (hw->max_transfer_size > 65535)
3213 hw->max_transfer_size = 65535;
3214 width = (hwcfg3 & GHWCFG3_PACKET_SIZE_CNTR_WIDTH_MASK) >>
3215 GHWCFG3_PACKET_SIZE_CNTR_WIDTH_SHIFT;
3216 hw->max_packet_count = (1 << (width + 4)) - 1;
3217 hw->i2c_enable = !!(hwcfg3 & GHWCFG3_I2C);
3218 hw->total_fifo_size = (hwcfg3 & GHWCFG3_DFIFO_DEPTH_MASK) >>
3219 GHWCFG3_DFIFO_DEPTH_SHIFT;
3222 hw->en_multiple_tx_fifo = !!(hwcfg4 & GHWCFG4_DED_FIFO_EN);
3223 hw->num_dev_perio_in_ep = (hwcfg4 & GHWCFG4_NUM_DEV_PERIO_IN_EP_MASK) >>
3224 GHWCFG4_NUM_DEV_PERIO_IN_EP_SHIFT;
3225 hw->dma_desc_enable = !!(hwcfg4 & GHWCFG4_DESC_DMA);
3226 hw->power_optimized = !!(hwcfg4 & GHWCFG4_POWER_OPTIMIZ);
3227 hw->utmi_phy_data_width = (hwcfg4 & GHWCFG4_UTMI_PHY_DATA_WIDTH_MASK) >>
3228 GHWCFG4_UTMI_PHY_DATA_WIDTH_SHIFT;
3231 hw->host_rx_fifo_size = (grxfsiz & GRXFSIZ_DEPTH_MASK) >>
3232 GRXFSIZ_DEPTH_SHIFT;
3233 hw->host_nperio_tx_fifo_size = (gnptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3234 FIFOSIZE_DEPTH_SHIFT;
3235 hw->host_perio_tx_fifo_size = (hptxfsiz & FIFOSIZE_DEPTH_MASK) >>
3236 FIFOSIZE_DEPTH_SHIFT;
3238 dev_dbg(hsotg->dev, "Detected values from hardware:\n");
3239 dev_dbg(hsotg->dev, " op_mode=%d\n",
3241 dev_dbg(hsotg->dev, " arch=%d\n",
3243 dev_dbg(hsotg->dev, " dma_desc_enable=%d\n",
3244 hw->dma_desc_enable);
3245 dev_dbg(hsotg->dev, " power_optimized=%d\n",
3246 hw->power_optimized);
3247 dev_dbg(hsotg->dev, " i2c_enable=%d\n",
3249 dev_dbg(hsotg->dev, " hs_phy_type=%d\n",
3251 dev_dbg(hsotg->dev, " fs_phy_type=%d\n",
3253 dev_dbg(hsotg->dev, " utmi_phy_data_width=%d\n",
3254 hw->utmi_phy_data_width);
3255 dev_dbg(hsotg->dev, " num_dev_ep=%d\n",
3257 dev_dbg(hsotg->dev, " num_dev_perio_in_ep=%d\n",
3258 hw->num_dev_perio_in_ep);
3259 dev_dbg(hsotg->dev, " host_channels=%d\n",
3261 dev_dbg(hsotg->dev, " max_transfer_size=%d\n",
3262 hw->max_transfer_size);
3263 dev_dbg(hsotg->dev, " max_packet_count=%d\n",
3264 hw->max_packet_count);
3265 dev_dbg(hsotg->dev, " nperio_tx_q_depth=0x%0x\n",
3266 hw->nperio_tx_q_depth);
3267 dev_dbg(hsotg->dev, " host_perio_tx_q_depth=0x%0x\n",
3268 hw->host_perio_tx_q_depth);
3269 dev_dbg(hsotg->dev, " dev_token_q_depth=0x%0x\n",
3270 hw->dev_token_q_depth);
3271 dev_dbg(hsotg->dev, " enable_dynamic_fifo=%d\n",
3272 hw->enable_dynamic_fifo);
3273 dev_dbg(hsotg->dev, " en_multiple_tx_fifo=%d\n",
3274 hw->en_multiple_tx_fifo);
3275 dev_dbg(hsotg->dev, " total_fifo_size=%d\n",
3276 hw->total_fifo_size);
3277 dev_dbg(hsotg->dev, " host_rx_fifo_size=%d\n",
3278 hw->host_rx_fifo_size);
3279 dev_dbg(hsotg->dev, " host_nperio_tx_fifo_size=%d\n",
3280 hw->host_nperio_tx_fifo_size);
3281 dev_dbg(hsotg->dev, " host_perio_tx_fifo_size=%d\n",
3282 hw->host_perio_tx_fifo_size);
3283 dev_dbg(hsotg->dev, "\n");
3289 * Sets all parameters to the given value.
3291 * Assumes that the dwc2_core_params struct contains only integers.
3293 void dwc2_set_all_params(struct dwc2_core_params *params, int value)
3295 int *p = (int *)params;
3296 size_t size = sizeof(*params) / sizeof(*p);
3299 for (i = 0; i < size; i++)
3304 u16 dwc2_get_otg_version(struct dwc2_hsotg *hsotg)
3306 return hsotg->core_params->otg_ver == 1 ? 0x0200 : 0x0103;
3309 bool dwc2_is_controller_alive(struct dwc2_hsotg *hsotg)
3311 if (dwc2_readl(hsotg->regs + GSNPSID) == 0xffffffff)
3318 * dwc2_enable_global_interrupts() - Enables the controller's Global
3319 * Interrupt in the AHB Config register
3321 * @hsotg: Programming view of DWC_otg controller
3323 void dwc2_enable_global_interrupts(struct dwc2_hsotg *hsotg)
3325 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
3327 ahbcfg |= GAHBCFG_GLBL_INTR_EN;
3328 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
3332 * dwc2_disable_global_interrupts() - Disables the controller's Global
3333 * Interrupt in the AHB Config register
3335 * @hsotg: Programming view of DWC_otg controller
3337 void dwc2_disable_global_interrupts(struct dwc2_hsotg *hsotg)
3339 u32 ahbcfg = dwc2_readl(hsotg->regs + GAHBCFG);
3341 ahbcfg &= ~GAHBCFG_GLBL_INTR_EN;
3342 dwc2_writel(ahbcfg, hsotg->regs + GAHBCFG);
3345 MODULE_DESCRIPTION("DESIGNWARE HS OTG Core");
3346 MODULE_AUTHOR("Synopsys, Inc.");
3347 MODULE_LICENSE("Dual BSD/GPL");