1 // SPDX-License-Identifier: GPL-2.0
3 * core.c - DesignWare USB3 DRD Controller Core file
5 * Copyright (C) 2015 Texas Instruments Incorporated - http://www.ti.com
7 * Authors: Felipe Balbi <balbi@ti.com>,
8 * Sebastian Andrzej Siewior <bigeasy@linutronix.de>
10 * Taken from Linux Kernel v3.19-rc1 (drivers/usb/dwc3/core.c) and ported
13 * commit cd72f890d2 : usb: dwc3: core: enable phy suspend quirk on non-FPGA
19 #include <dwc3-uboot.h>
20 #include <dm/device_compat.h>
21 #include <dm/devres.h>
22 #include <linux/bug.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/err.h>
25 #include <linux/ioport.h>
27 #include <generic-phy.h>
28 #include <linux/usb/ch9.h>
29 #include <linux/usb/gadget.h>
35 #include "linux-compat.h"
37 static LIST_HEAD(dwc3_list);
38 /* -------------------------------------------------------------------------- */
40 static void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
44 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
45 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
46 reg |= DWC3_GCTL_PRTCAPDIR(mode);
47 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
51 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
52 * @dwc: pointer to our context structure
54 static int dwc3_core_soft_reset(struct dwc3 *dwc)
58 /* Before Resetting PHY, put Core in Reset */
59 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
60 reg |= DWC3_GCTL_CORESOFTRESET;
61 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
63 /* Assert USB3 PHY reset */
64 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
65 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
66 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
68 /* Assert USB2 PHY reset */
69 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
70 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
71 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
75 /* Clear USB3 PHY reset */
76 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
77 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
78 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
80 /* Clear USB2 PHY reset */
81 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
82 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
83 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
87 /* After PHYs are stable we can take Core out of reset state */
88 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
89 reg &= ~DWC3_GCTL_CORESOFTRESET;
90 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
96 * dwc3_free_one_event_buffer - Frees one event buffer
97 * @dwc: Pointer to our controller context structure
98 * @evt: Pointer to event buffer to be freed
100 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
101 struct dwc3_event_buffer *evt)
103 dma_free_coherent(evt->buf);
107 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
108 * @dwc: Pointer to our controller context structure
109 * @length: size of the event buffer
111 * Returns a pointer to the allocated event buffer structure on success
112 * otherwise ERR_PTR(errno).
114 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
117 struct dwc3_event_buffer *evt;
119 evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt),
122 return ERR_PTR(-ENOMEM);
125 evt->length = length;
126 evt->buf = dma_alloc_coherent(length,
127 (unsigned long *)&evt->dma);
129 return ERR_PTR(-ENOMEM);
131 dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
137 * dwc3_free_event_buffers - frees all allocated event buffers
138 * @dwc: Pointer to our controller context structure
140 static void dwc3_free_event_buffers(struct dwc3 *dwc)
142 struct dwc3_event_buffer *evt;
145 for (i = 0; i < dwc->num_event_buffers; i++) {
146 evt = dwc->ev_buffs[i];
148 dwc3_free_one_event_buffer(dwc, evt);
153 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
154 * @dwc: pointer to our controller context structure
155 * @length: size of event buffer
157 * Returns 0 on success otherwise negative errno. In the error case, dwc
158 * may contain some buffers allocated but not all which were requested.
160 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
165 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
166 dwc->num_event_buffers = num;
168 dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE,
169 sizeof(*dwc->ev_buffs) * num);
173 for (i = 0; i < num; i++) {
174 struct dwc3_event_buffer *evt;
176 evt = dwc3_alloc_one_event_buffer(dwc, length);
178 dev_err(dwc->dev, "can't allocate event buffer\n");
181 dwc->ev_buffs[i] = evt;
188 * dwc3_event_buffers_setup - setup our allocated event buffers
189 * @dwc: pointer to our controller context structure
191 * Returns 0 on success otherwise negative errno.
193 static int dwc3_event_buffers_setup(struct dwc3 *dwc)
195 struct dwc3_event_buffer *evt;
198 for (n = 0; n < dwc->num_event_buffers; n++) {
199 evt = dwc->ev_buffs[n];
200 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
201 evt->buf, (unsigned long long) evt->dma,
206 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
207 lower_32_bits(evt->dma));
208 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
209 upper_32_bits(evt->dma));
210 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
211 DWC3_GEVNTSIZ_SIZE(evt->length));
212 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
218 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
220 struct dwc3_event_buffer *evt;
223 for (n = 0; n < dwc->num_event_buffers; n++) {
224 evt = dwc->ev_buffs[n];
228 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
229 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
230 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
231 | DWC3_GEVNTSIZ_SIZE(0));
232 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
236 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
238 if (!dwc->has_hibernation)
241 if (!dwc->nr_scratch)
244 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
245 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
246 if (!dwc->scratchbuf)
252 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
254 dma_addr_t scratch_addr;
258 if (!dwc->has_hibernation)
261 if (!dwc->nr_scratch)
264 scratch_addr = dma_map_single(dwc->scratchbuf,
265 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
267 if (dma_mapping_error(dwc->dev, scratch_addr)) {
268 dev_err(dwc->dev, "failed to map scratch buffer\n");
273 dwc->scratch_addr = scratch_addr;
275 param = lower_32_bits(scratch_addr);
277 ret = dwc3_send_gadget_generic_command(dwc,
278 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
282 param = upper_32_bits(scratch_addr);
284 ret = dwc3_send_gadget_generic_command(dwc,
285 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
292 dma_unmap_single(scratch_addr, dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
299 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
301 if (!dwc->has_hibernation)
304 if (!dwc->nr_scratch)
307 dma_unmap_single(dwc->scratch_addr, dwc->nr_scratch *
308 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
309 kfree(dwc->scratchbuf);
312 static void dwc3_core_num_eps(struct dwc3 *dwc)
314 struct dwc3_hwparams *parms = &dwc->hwparams;
316 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
317 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
319 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
320 dwc->num_in_eps, dwc->num_out_eps);
323 static void dwc3_cache_hwparams(struct dwc3 *dwc)
325 struct dwc3_hwparams *parms = &dwc->hwparams;
327 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
328 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
329 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
330 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
331 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
332 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
333 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
334 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
335 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
339 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
340 * @dwc: Pointer to our controller context structure
342 static void dwc3_phy_setup(struct dwc3 *dwc)
346 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
349 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
350 * to '0' during coreConsultant configuration. So default value
351 * will be '0' when the core is reset. Application needs to set it
352 * to '1' after the core initialization is completed.
354 if (dwc->revision > DWC3_REVISION_194A)
355 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
357 if (dwc->u2ss_inp3_quirk)
358 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
360 if (dwc->req_p1p2p3_quirk)
361 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
363 if (dwc->del_p1p2p3_quirk)
364 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
366 if (dwc->del_phy_power_chg_quirk)
367 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
369 if (dwc->lfps_filter_quirk)
370 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
372 if (dwc->rx_detect_poll_quirk)
373 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
375 if (dwc->tx_de_emphasis_quirk)
376 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
378 if (dwc->dis_u3_susphy_quirk)
379 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
381 if (dwc->dis_del_phy_power_chg_quirk)
382 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
384 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
388 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
391 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
392 * '0' during coreConsultant configuration. So default value will
393 * be '0' when the core is reset. Application needs to set it to
394 * '1' after the core initialization is completed.
396 if (dwc->revision > DWC3_REVISION_194A)
397 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
399 if (dwc->dis_u2_susphy_quirk)
400 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
402 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
408 * dwc3_core_init - Low-level initialization of DWC3 Core
409 * @dwc: Pointer to our controller context structure
411 * Returns 0 on success otherwise negative errno.
413 static int dwc3_core_init(struct dwc3 *dwc)
415 unsigned long timeout;
416 u32 hwparams4 = dwc->hwparams.hwparams4;
420 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
421 /* This should read as U3 followed by revision number */
422 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
423 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
429 /* Handle USB2.0-only core configuration */
430 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
431 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
432 if (dwc->maximum_speed == USB_SPEED_SUPER)
433 dwc->maximum_speed = USB_SPEED_HIGH;
436 /* issue device SoftReset too */
438 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
440 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
441 if (!(reg & DWC3_DCTL_CSFTRST))
446 dev_err(dwc->dev, "Reset Timed Out\n");
453 ret = dwc3_core_soft_reset(dwc);
457 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
458 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
460 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
461 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
463 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
464 * issue which would cause xHCI compliance tests to fail.
466 * Because of that we cannot enable clock gating on such
471 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
474 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
475 dwc->dr_mode == USB_DR_MODE_OTG) &&
476 (dwc->revision >= DWC3_REVISION_210A &&
477 dwc->revision <= DWC3_REVISION_250A))
478 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
480 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
482 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
483 /* enable hibernation here */
484 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
487 * REVISIT Enabling this bit so that host-mode hibernation
488 * will work. Device-mode hibernation is not yet implemented.
490 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
493 dev_dbg(dwc->dev, "No power optimization available\n");
496 /* check if current dwc3 is on simulation board */
497 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
498 dev_dbg(dwc->dev, "it is on FPGA board\n");
502 if(dwc->disable_scramble_quirk && !dwc->is_fpga)
504 "disable_scramble cannot be used on non-FPGA builds\n");
506 if (dwc->disable_scramble_quirk && dwc->is_fpga)
507 reg |= DWC3_GCTL_DISSCRAMBLE;
509 reg &= ~DWC3_GCTL_DISSCRAMBLE;
511 if (dwc->u2exit_lfps_quirk)
512 reg |= DWC3_GCTL_U2EXIT_LFPS;
515 * WORKAROUND: DWC3 revisions <1.90a have a bug
516 * where the device can fail to connect at SuperSpeed
517 * and falls back to high-speed mode which causes
518 * the device to enter a Connect/Disconnect loop
520 if (dwc->revision < DWC3_REVISION_190A)
521 reg |= DWC3_GCTL_U2RSTECN;
523 dwc3_core_num_eps(dwc);
525 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
527 ret = dwc3_alloc_scratch_buffers(dwc);
531 ret = dwc3_setup_scratch_buffers(dwc);
538 dwc3_free_scratch_buffers(dwc);
544 static void dwc3_core_exit(struct dwc3 *dwc)
546 dwc3_free_scratch_buffers(dwc);
549 static int dwc3_core_init_mode(struct dwc3 *dwc)
553 switch (dwc->dr_mode) {
554 case USB_DR_MODE_PERIPHERAL:
555 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
556 ret = dwc3_gadget_init(dwc);
558 dev_err(dev, "failed to initialize gadget\n");
562 case USB_DR_MODE_HOST:
563 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
564 ret = dwc3_host_init(dwc);
566 dev_err(dev, "failed to initialize host\n");
570 case USB_DR_MODE_OTG:
571 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
572 ret = dwc3_host_init(dwc);
574 dev_err(dev, "failed to initialize host\n");
578 ret = dwc3_gadget_init(dwc);
580 dev_err(dev, "failed to initialize gadget\n");
585 dev_err(dev, "Unsupported mode of operation %d\n", dwc->dr_mode);
592 static void dwc3_gadget_run(struct dwc3 *dwc)
594 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_RUN_STOP);
598 static void dwc3_core_exit_mode(struct dwc3 *dwc)
600 switch (dwc->dr_mode) {
601 case USB_DR_MODE_PERIPHERAL:
602 dwc3_gadget_exit(dwc);
604 case USB_DR_MODE_HOST:
607 case USB_DR_MODE_OTG:
609 dwc3_gadget_exit(dwc);
617 * switch back to peripheral mode
618 * This enables the phy to enter idle and then, if enabled, suspend.
620 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
621 dwc3_gadget_run(dwc);
624 static void dwc3_uboot_hsphy_mode(struct dwc3_device *dwc3_dev,
627 enum usb_phy_interface hsphy_mode = dwc3_dev->hsphy_mode;
630 /* Set dwc3 usb2 phy config */
631 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
633 switch (hsphy_mode) {
634 case USBPHY_INTERFACE_MODE_UTMI:
635 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
636 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
637 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
638 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
640 case USBPHY_INTERFACE_MODE_UTMIW:
641 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
642 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
643 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
644 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
650 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
653 #define DWC3_ALIGN_MASK (16 - 1)
656 * dwc3_uboot_init - dwc3 core uboot initialization code
657 * @dwc3_dev: struct dwc3_device containing initialization data
659 * Entry point for dwc3 driver (equivalent to dwc3_probe in linux
660 * kernel driver). Pointer to dwc3_device should be passed containing
661 * base address and other initialization data. Returns '0' on success and
662 * a negative value on failure.
664 * Generally called from board_usb_init() implemented in board file.
666 int dwc3_uboot_init(struct dwc3_device *dwc3_dev)
669 struct device *dev = NULL;
670 u8 lpm_nyet_threshold;
678 mem = devm_kzalloc((struct udevice *)dev,
679 sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
683 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
686 dwc->regs = (void *)(uintptr_t)(dwc3_dev->base +
687 DWC3_GLOBALS_REGS_START);
689 /* default to highest possible threshold */
690 lpm_nyet_threshold = 0xff;
692 /* default to -3.5dB de-emphasis */
696 * default to assert utmi_sleep_n and use maximum allowed HIRD
697 * threshold value of 0b1100
701 dwc->maximum_speed = dwc3_dev->maximum_speed;
702 dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum;
703 if (dwc3_dev->lpm_nyet_threshold)
704 lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold;
705 dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend;
706 if (dwc3_dev->hird_threshold)
707 hird_threshold = dwc3_dev->hird_threshold;
709 dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize;
710 dwc->dr_mode = dwc3_dev->dr_mode;
712 dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk;
713 dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk;
714 dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk;
715 dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk;
716 dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk;
717 dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk;
718 dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk;
719 dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk;
720 dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk;
721 dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk;
722 dwc->dis_del_phy_power_chg_quirk = dwc3_dev->dis_del_phy_power_chg_quirk;
724 dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk;
725 if (dwc3_dev->tx_de_emphasis)
726 tx_de_emphasis = dwc3_dev->tx_de_emphasis;
728 /* default to superspeed if no maximum_speed passed */
729 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
730 dwc->maximum_speed = USB_SPEED_SUPER;
732 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
733 dwc->tx_de_emphasis = tx_de_emphasis;
735 dwc->hird_threshold = hird_threshold
736 | (dwc->is_utmi_l1_suspend << 4);
738 dwc->index = dwc3_dev->index;
740 dwc3_cache_hwparams(dwc);
742 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
744 dev_err(dwc->dev, "failed to allocate event buffers\n");
748 if (!IS_ENABLED(CONFIG_USB_DWC3_GADGET))
749 dwc->dr_mode = USB_DR_MODE_HOST;
750 else if (!IS_ENABLED(CONFIG_USB_HOST))
751 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
753 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
754 dwc->dr_mode = USB_DR_MODE_OTG;
756 ret = dwc3_core_init(dwc);
758 dev_err(dev, "failed to initialize core\n");
762 dwc3_uboot_hsphy_mode(dwc3_dev, dwc);
764 ret = dwc3_event_buffers_setup(dwc);
766 dev_err(dwc->dev, "failed to setup event buffers\n");
770 ret = dwc3_core_init_mode(dwc);
774 list_add_tail(&dwc->list, &dwc3_list);
779 dwc3_event_buffers_cleanup(dwc);
785 dwc3_free_event_buffers(dwc);
791 * dwc3_uboot_exit - dwc3 core uboot cleanup code
792 * @index: index of this controller
794 * Performs cleanup of memory allocated in dwc3_uboot_init and other misc
795 * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller
796 * should be passed and should match with the index passed in
797 * dwc3_device during init.
799 * Generally called from board file.
801 void dwc3_uboot_exit(int index)
805 list_for_each_entry(dwc, &dwc3_list, list) {
806 if (dwc->index != index)
809 dwc3_core_exit_mode(dwc);
810 dwc3_event_buffers_cleanup(dwc);
811 dwc3_free_event_buffers(dwc);
813 list_del(&dwc->list);
820 * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt
821 * @index: index of this controller
823 * Invokes dwc3 gadget interrupts.
825 * Generally called from board file.
827 void dwc3_uboot_handle_interrupt(int index)
829 struct dwc3 *dwc = NULL;
831 list_for_each_entry(dwc, &dwc3_list, list) {
832 if (dwc->index != index)
835 dwc3_gadget_uboot_handle_interrupt(dwc);
840 MODULE_ALIAS("platform:dwc3");
841 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
842 MODULE_LICENSE("GPL v2");
843 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
845 #if CONFIG_IS_ENABLED(PHY) && CONFIG_IS_ENABLED(DM_USB)
846 int dwc3_setup_phy(struct udevice *dev, struct phy_bulk *phys)
850 ret = generic_phy_get_bulk(dev, phys);
854 ret = generic_phy_init_bulk(phys);
858 ret = generic_phy_power_on_bulk(phys);
860 generic_phy_exit_bulk(phys);
865 int dwc3_shutdown_phy(struct udevice *dev, struct phy_bulk *phys)
869 ret = generic_phy_power_off_bulk(phys);
870 ret |= generic_phy_exit_bulk(phys);
875 #if CONFIG_IS_ENABLED(DM_USB)
876 void dwc3_of_parse(struct dwc3 *dwc)
879 struct udevice *dev = dwc->dev;
880 u8 lpm_nyet_threshold;
884 /* default to highest possible threshold */
885 lpm_nyet_threshold = 0xff;
887 /* default to -3.5dB de-emphasis */
891 * default to assert utmi_sleep_n and use maximum allowed HIRD
892 * threshold value of 0b1100
896 dwc->has_lpm_erratum = dev_read_bool(dev,
897 "snps,has-lpm-erratum");
898 tmp = dev_read_u8_array_ptr(dev, "snps,lpm-nyet-threshold", 1);
900 lpm_nyet_threshold = *tmp;
902 dwc->is_utmi_l1_suspend = dev_read_bool(dev,
903 "snps,is-utmi-l1-suspend");
904 tmp = dev_read_u8_array_ptr(dev, "snps,hird-threshold", 1);
906 hird_threshold = *tmp;
908 dwc->disable_scramble_quirk = dev_read_bool(dev,
909 "snps,disable_scramble_quirk");
910 dwc->u2exit_lfps_quirk = dev_read_bool(dev,
911 "snps,u2exit_lfps_quirk");
912 dwc->u2ss_inp3_quirk = dev_read_bool(dev,
913 "snps,u2ss_inp3_quirk");
914 dwc->req_p1p2p3_quirk = dev_read_bool(dev,
915 "snps,req_p1p2p3_quirk");
916 dwc->del_p1p2p3_quirk = dev_read_bool(dev,
917 "snps,del_p1p2p3_quirk");
918 dwc->del_phy_power_chg_quirk = dev_read_bool(dev,
919 "snps,del_phy_power_chg_quirk");
920 dwc->lfps_filter_quirk = dev_read_bool(dev,
921 "snps,lfps_filter_quirk");
922 dwc->rx_detect_poll_quirk = dev_read_bool(dev,
923 "snps,rx_detect_poll_quirk");
924 dwc->dis_u3_susphy_quirk = dev_read_bool(dev,
925 "snps,dis_u3_susphy_quirk");
926 dwc->dis_u2_susphy_quirk = dev_read_bool(dev,
927 "snps,dis_u2_susphy_quirk");
928 dwc->dis_del_phy_power_chg_quirk = dev_read_bool(dev,
929 "snps,dis-del-phy-power-chg-quirk");
930 dwc->tx_de_emphasis_quirk = dev_read_bool(dev,
931 "snps,tx_de_emphasis_quirk");
932 tmp = dev_read_u8_array_ptr(dev, "snps,tx_de_emphasis", 1);
934 tx_de_emphasis = *tmp;
936 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
937 dwc->tx_de_emphasis = tx_de_emphasis;
939 dwc->hird_threshold = hird_threshold
940 | (dwc->is_utmi_l1_suspend << 4);
943 int dwc3_init(struct dwc3 *dwc)
947 dwc3_cache_hwparams(dwc);
949 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
951 dev_err(dwc->dev, "failed to allocate event buffers\n");
955 ret = dwc3_core_init(dwc);
957 dev_err(dev, "failed to initialize core\n");
961 ret = dwc3_event_buffers_setup(dwc);
963 dev_err(dwc->dev, "failed to setup event buffers\n");
967 ret = dwc3_core_init_mode(dwc);
974 dwc3_event_buffers_cleanup(dwc);
980 dwc3_free_event_buffers(dwc);
985 void dwc3_remove(struct dwc3 *dwc)
987 dwc3_core_exit_mode(dwc);
988 dwc3_event_buffers_cleanup(dwc);
989 dwc3_free_event_buffers(dwc);