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/delay.h>
24 #include <linux/dma-mapping.h>
25 #include <linux/err.h>
26 #include <linux/ioport.h>
28 #include <generic-phy.h>
29 #include <linux/usb/ch9.h>
30 #include <linux/usb/gadget.h>
36 #include "linux-compat.h"
38 static LIST_HEAD(dwc3_list);
39 /* -------------------------------------------------------------------------- */
41 static void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
45 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
46 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
47 reg |= DWC3_GCTL_PRTCAPDIR(mode);
48 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
52 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
53 * @dwc: pointer to our context structure
55 static int dwc3_core_soft_reset(struct dwc3 *dwc)
59 /* Before Resetting PHY, put Core in Reset */
60 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
61 reg |= DWC3_GCTL_CORESOFTRESET;
62 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
64 /* Assert USB3 PHY reset */
65 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
66 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
67 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
69 /* Assert USB2 PHY reset */
70 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
71 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
72 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
76 /* Clear USB3 PHY reset */
77 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
78 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
79 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
81 /* Clear USB2 PHY reset */
82 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
83 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
84 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
88 /* After PHYs are stable we can take Core out of reset state */
89 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
90 reg &= ~DWC3_GCTL_CORESOFTRESET;
91 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
97 * dwc3_frame_length_adjustment - Adjusts frame length if required
98 * @dwc3: Pointer to our controller context structure
99 * @fladj: Value of GFLADJ_30MHZ to adjust frame length
101 static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj)
105 if (dwc->revision < DWC3_REVISION_250A)
111 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
112 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
113 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | fladj;
114 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
118 * dwc3_free_one_event_buffer - Frees one event buffer
119 * @dwc: Pointer to our controller context structure
120 * @evt: Pointer to event buffer to be freed
122 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
123 struct dwc3_event_buffer *evt)
125 dma_free_coherent(evt->buf);
129 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
130 * @dwc: Pointer to our controller context structure
131 * @length: size of the event buffer
133 * Returns a pointer to the allocated event buffer structure on success
134 * otherwise ERR_PTR(errno).
136 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
139 struct dwc3_event_buffer *evt;
141 evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt),
144 return ERR_PTR(-ENOMEM);
147 evt->length = length;
148 evt->buf = dma_alloc_coherent(length,
149 (unsigned long *)&evt->dma);
151 return ERR_PTR(-ENOMEM);
153 dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
159 * dwc3_free_event_buffers - frees all allocated event buffers
160 * @dwc: Pointer to our controller context structure
162 static void dwc3_free_event_buffers(struct dwc3 *dwc)
164 struct dwc3_event_buffer *evt;
167 for (i = 0; i < dwc->num_event_buffers; i++) {
168 evt = dwc->ev_buffs[i];
170 dwc3_free_one_event_buffer(dwc, evt);
175 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
176 * @dwc: pointer to our controller context structure
177 * @length: size of event buffer
179 * Returns 0 on success otherwise negative errno. In the error case, dwc
180 * may contain some buffers allocated but not all which were requested.
182 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
187 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
188 dwc->num_event_buffers = num;
190 dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE,
191 sizeof(*dwc->ev_buffs) * num);
195 for (i = 0; i < num; i++) {
196 struct dwc3_event_buffer *evt;
198 evt = dwc3_alloc_one_event_buffer(dwc, length);
200 dev_err(dwc->dev, "can't allocate event buffer\n");
203 dwc->ev_buffs[i] = evt;
210 * dwc3_event_buffers_setup - setup our allocated event buffers
211 * @dwc: pointer to our controller context structure
213 * Returns 0 on success otherwise negative errno.
215 static int dwc3_event_buffers_setup(struct dwc3 *dwc)
217 struct dwc3_event_buffer *evt;
220 for (n = 0; n < dwc->num_event_buffers; n++) {
221 evt = dwc->ev_buffs[n];
222 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
223 evt->buf, (unsigned long long) evt->dma,
228 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
229 lower_32_bits(evt->dma));
230 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
231 upper_32_bits(evt->dma));
232 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
233 DWC3_GEVNTSIZ_SIZE(evt->length));
234 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
240 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
242 struct dwc3_event_buffer *evt;
245 for (n = 0; n < dwc->num_event_buffers; n++) {
246 evt = dwc->ev_buffs[n];
250 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
251 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
252 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
253 | DWC3_GEVNTSIZ_SIZE(0));
254 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
258 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
260 if (!dwc->has_hibernation)
263 if (!dwc->nr_scratch)
266 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
267 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
268 if (!dwc->scratchbuf)
274 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
276 dma_addr_t scratch_addr;
280 if (!dwc->has_hibernation)
283 if (!dwc->nr_scratch)
286 scratch_addr = dma_map_single(dwc->scratchbuf,
287 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
289 if (dma_mapping_error(dwc->dev, scratch_addr)) {
290 dev_err(dwc->dev, "failed to map scratch buffer\n");
295 dwc->scratch_addr = scratch_addr;
297 param = lower_32_bits(scratch_addr);
299 ret = dwc3_send_gadget_generic_command(dwc,
300 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
304 param = upper_32_bits(scratch_addr);
306 ret = dwc3_send_gadget_generic_command(dwc,
307 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
314 dma_unmap_single(scratch_addr, dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
321 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
323 if (!dwc->has_hibernation)
326 if (!dwc->nr_scratch)
329 dma_unmap_single(dwc->scratch_addr, dwc->nr_scratch *
330 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
331 kfree(dwc->scratchbuf);
334 static void dwc3_core_num_eps(struct dwc3 *dwc)
336 struct dwc3_hwparams *parms = &dwc->hwparams;
338 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
339 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
341 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
342 dwc->num_in_eps, dwc->num_out_eps);
345 static void dwc3_cache_hwparams(struct dwc3 *dwc)
347 struct dwc3_hwparams *parms = &dwc->hwparams;
349 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
350 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
351 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
352 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
353 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
354 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
355 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
356 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
357 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
360 static void dwc3_hsphy_mode_setup(struct dwc3 *dwc)
362 enum usb_phy_interface hsphy_mode = dwc->hsphy_mode;
365 /* Set dwc3 usb2 phy config */
366 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
368 switch (hsphy_mode) {
369 case USBPHY_INTERFACE_MODE_UTMI:
370 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
371 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
372 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
373 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
375 case USBPHY_INTERFACE_MODE_UTMIW:
376 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
377 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
378 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
379 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
385 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
389 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
390 * @dwc: Pointer to our controller context structure
392 static void dwc3_phy_setup(struct dwc3 *dwc)
396 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
399 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
400 * to '0' during coreConsultant configuration. So default value
401 * will be '0' when the core is reset. Application needs to set it
402 * to '1' after the core initialization is completed.
404 if (dwc->revision > DWC3_REVISION_194A)
405 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
407 if (dwc->u2ss_inp3_quirk)
408 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
410 if (dwc->req_p1p2p3_quirk)
411 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
413 if (dwc->del_p1p2p3_quirk)
414 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
416 if (dwc->del_phy_power_chg_quirk)
417 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
419 if (dwc->lfps_filter_quirk)
420 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
422 if (dwc->rx_detect_poll_quirk)
423 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
425 if (dwc->tx_de_emphasis_quirk)
426 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
428 if (dwc->dis_u3_susphy_quirk)
429 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
431 if (dwc->dis_del_phy_power_chg_quirk)
432 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
434 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
436 dwc3_hsphy_mode_setup(dwc);
440 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
443 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
444 * '0' during coreConsultant configuration. So default value will
445 * be '0' when the core is reset. Application needs to set it to
446 * '1' after the core initialization is completed.
448 if (dwc->revision > DWC3_REVISION_194A)
449 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
451 if (dwc->dis_u2_susphy_quirk)
452 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
454 if (dwc->dis_enblslpm_quirk)
455 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
457 if (dwc->dis_u2_freeclk_exists_quirk)
458 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
460 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
465 /* set global incr burst type configuration registers */
466 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
468 struct udevice *dev = dwc->dev;
471 if (!dwc->incrx_size)
474 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
476 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
477 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
479 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
480 switch (dwc->incrx_size) {
482 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
485 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
488 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
491 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
494 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
497 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
500 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
505 dev_err(dev, "Invalid property\n");
509 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
513 * dwc3_core_init - Low-level initialization of DWC3 Core
514 * @dwc: Pointer to our controller context structure
516 * Returns 0 on success otherwise negative errno.
518 static int dwc3_core_init(struct dwc3 *dwc)
520 unsigned long timeout;
521 u32 hwparams4 = dwc->hwparams.hwparams4;
525 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
526 /* This should read as U3 followed by revision number */
527 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
528 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
534 /* Handle USB2.0-only core configuration */
535 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
536 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
537 if (dwc->maximum_speed == USB_SPEED_SUPER)
538 dwc->maximum_speed = USB_SPEED_HIGH;
541 /* issue device SoftReset too */
543 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
545 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
546 if (!(reg & DWC3_DCTL_CSFTRST))
551 dev_err(dwc->dev, "Reset Timed Out\n");
558 ret = dwc3_core_soft_reset(dwc);
562 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
563 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
565 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
566 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
568 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
569 * issue which would cause xHCI compliance tests to fail.
571 * Because of that we cannot enable clock gating on such
576 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
579 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
580 dwc->dr_mode == USB_DR_MODE_OTG) &&
581 (dwc->revision >= DWC3_REVISION_210A &&
582 dwc->revision <= DWC3_REVISION_250A))
583 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
585 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
587 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
588 /* enable hibernation here */
589 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
592 * REVISIT Enabling this bit so that host-mode hibernation
593 * will work. Device-mode hibernation is not yet implemented.
595 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
598 dev_dbg(dwc->dev, "No power optimization available\n");
601 /* check if current dwc3 is on simulation board */
602 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
603 dev_dbg(dwc->dev, "it is on FPGA board\n");
607 if(dwc->disable_scramble_quirk && !dwc->is_fpga)
609 "disable_scramble cannot be used on non-FPGA builds\n");
611 if (dwc->disable_scramble_quirk && dwc->is_fpga)
612 reg |= DWC3_GCTL_DISSCRAMBLE;
614 reg &= ~DWC3_GCTL_DISSCRAMBLE;
616 if (dwc->u2exit_lfps_quirk)
617 reg |= DWC3_GCTL_U2EXIT_LFPS;
620 * WORKAROUND: DWC3 revisions <1.90a have a bug
621 * where the device can fail to connect at SuperSpeed
622 * and falls back to high-speed mode which causes
623 * the device to enter a Connect/Disconnect loop
625 if (dwc->revision < DWC3_REVISION_190A)
626 reg |= DWC3_GCTL_U2RSTECN;
628 dwc3_core_num_eps(dwc);
630 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
632 ret = dwc3_alloc_scratch_buffers(dwc);
636 ret = dwc3_setup_scratch_buffers(dwc);
640 /* Adjust Frame Length */
641 dwc3_frame_length_adjustment(dwc, dwc->fladj);
643 dwc3_set_incr_burst_type(dwc);
648 dwc3_free_scratch_buffers(dwc);
654 static void dwc3_core_exit(struct dwc3 *dwc)
656 dwc3_free_scratch_buffers(dwc);
659 static int dwc3_core_init_mode(struct dwc3 *dwc)
663 switch (dwc->dr_mode) {
664 case USB_DR_MODE_PERIPHERAL:
665 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
666 ret = dwc3_gadget_init(dwc);
668 dev_err(dwc->dev, "failed to initialize gadget\n");
672 case USB_DR_MODE_HOST:
673 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
674 ret = dwc3_host_init(dwc);
676 dev_err(dwc->dev, "failed to initialize host\n");
680 case USB_DR_MODE_OTG:
681 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
682 ret = dwc3_host_init(dwc);
684 dev_err(dwc->dev, "failed to initialize host\n");
688 ret = dwc3_gadget_init(dwc);
690 dev_err(dwc->dev, "failed to initialize gadget\n");
696 "Unsupported mode of operation %d\n", dwc->dr_mode);
703 static void dwc3_gadget_run(struct dwc3 *dwc)
705 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_RUN_STOP);
709 static void dwc3_core_exit_mode(struct dwc3 *dwc)
711 switch (dwc->dr_mode) {
712 case USB_DR_MODE_PERIPHERAL:
713 dwc3_gadget_exit(dwc);
715 case USB_DR_MODE_HOST:
718 case USB_DR_MODE_OTG:
720 dwc3_gadget_exit(dwc);
728 * switch back to peripheral mode
729 * This enables the phy to enter idle and then, if enabled, suspend.
731 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
732 dwc3_gadget_run(dwc);
735 #define DWC3_ALIGN_MASK (16 - 1)
738 * dwc3_uboot_init - dwc3 core uboot initialization code
739 * @dwc3_dev: struct dwc3_device containing initialization data
741 * Entry point for dwc3 driver (equivalent to dwc3_probe in linux
742 * kernel driver). Pointer to dwc3_device should be passed containing
743 * base address and other initialization data. Returns '0' on success and
744 * a negative value on failure.
746 * Generally called from board_usb_init() implemented in board file.
748 int dwc3_uboot_init(struct dwc3_device *dwc3_dev)
751 struct device *dev = NULL;
752 u8 lpm_nyet_threshold;
760 mem = devm_kzalloc((struct udevice *)dev,
761 sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
765 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
768 dwc->regs = (void *)(uintptr_t)(dwc3_dev->base +
769 DWC3_GLOBALS_REGS_START);
771 /* default to highest possible threshold */
772 lpm_nyet_threshold = 0xff;
774 /* default to -3.5dB de-emphasis */
778 * default to assert utmi_sleep_n and use maximum allowed HIRD
779 * threshold value of 0b1100
783 dwc->maximum_speed = dwc3_dev->maximum_speed;
784 dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum;
785 if (dwc3_dev->lpm_nyet_threshold)
786 lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold;
787 dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend;
788 if (dwc3_dev->hird_threshold)
789 hird_threshold = dwc3_dev->hird_threshold;
791 dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize;
792 dwc->dr_mode = dwc3_dev->dr_mode;
794 dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk;
795 dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk;
796 dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk;
797 dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk;
798 dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk;
799 dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk;
800 dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk;
801 dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk;
802 dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk;
803 dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk;
804 dwc->dis_del_phy_power_chg_quirk = dwc3_dev->dis_del_phy_power_chg_quirk;
805 dwc->dis_tx_ipgap_linecheck_quirk = dwc3_dev->dis_tx_ipgap_linecheck_quirk;
806 dwc->dis_enblslpm_quirk = dwc3_dev->dis_enblslpm_quirk;
807 dwc->dis_u2_freeclk_exists_quirk = dwc3_dev->dis_u2_freeclk_exists_quirk;
809 dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk;
810 if (dwc3_dev->tx_de_emphasis)
811 tx_de_emphasis = dwc3_dev->tx_de_emphasis;
813 /* default to superspeed if no maximum_speed passed */
814 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
815 dwc->maximum_speed = USB_SPEED_SUPER;
817 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
818 dwc->tx_de_emphasis = tx_de_emphasis;
820 dwc->hird_threshold = hird_threshold
821 | (dwc->is_utmi_l1_suspend << 4);
823 dwc->hsphy_mode = dwc3_dev->hsphy_mode;
825 dwc->index = dwc3_dev->index;
827 dwc3_cache_hwparams(dwc);
829 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
831 dev_err(dwc->dev, "failed to allocate event buffers\n");
835 if (!IS_ENABLED(CONFIG_USB_DWC3_GADGET))
836 dwc->dr_mode = USB_DR_MODE_HOST;
837 else if (!IS_ENABLED(CONFIG_USB_HOST))
838 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
840 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
841 dwc->dr_mode = USB_DR_MODE_OTG;
843 ret = dwc3_core_init(dwc);
845 dev_err(dwc->dev, "failed to initialize core\n");
849 ret = dwc3_event_buffers_setup(dwc);
851 dev_err(dwc->dev, "failed to setup event buffers\n");
855 ret = dwc3_core_init_mode(dwc);
859 list_add_tail(&dwc->list, &dwc3_list);
864 dwc3_event_buffers_cleanup(dwc);
870 dwc3_free_event_buffers(dwc);
876 * dwc3_uboot_exit - dwc3 core uboot cleanup code
877 * @index: index of this controller
879 * Performs cleanup of memory allocated in dwc3_uboot_init and other misc
880 * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller
881 * should be passed and should match with the index passed in
882 * dwc3_device during init.
884 * Generally called from board file.
886 void dwc3_uboot_exit(int index)
890 list_for_each_entry(dwc, &dwc3_list, list) {
891 if (dwc->index != index)
894 dwc3_core_exit_mode(dwc);
895 dwc3_event_buffers_cleanup(dwc);
896 dwc3_free_event_buffers(dwc);
898 list_del(&dwc->list);
905 * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt
906 * @index: index of this controller
908 * Invokes dwc3 gadget interrupts.
910 * Generally called from board file.
912 void dwc3_uboot_handle_interrupt(int index)
914 struct dwc3 *dwc = NULL;
916 list_for_each_entry(dwc, &dwc3_list, list) {
917 if (dwc->index != index)
920 dwc3_gadget_uboot_handle_interrupt(dwc);
925 MODULE_ALIAS("platform:dwc3");
926 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
927 MODULE_LICENSE("GPL v2");
928 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
930 #if CONFIG_IS_ENABLED(PHY) && CONFIG_IS_ENABLED(DM_USB)
931 int dwc3_setup_phy(struct udevice *dev, struct phy_bulk *phys)
935 ret = generic_phy_get_bulk(dev, phys);
939 ret = generic_phy_init_bulk(phys);
943 ret = generic_phy_power_on_bulk(phys);
945 generic_phy_exit_bulk(phys);
950 int dwc3_shutdown_phy(struct udevice *dev, struct phy_bulk *phys)
954 ret = generic_phy_power_off_bulk(phys);
955 ret |= generic_phy_exit_bulk(phys);
960 #if CONFIG_IS_ENABLED(DM_USB)
961 void dwc3_of_parse(struct dwc3 *dwc)
964 struct udevice *dev = dwc->dev;
965 u8 lpm_nyet_threshold;
971 /* default to highest possible threshold */
972 lpm_nyet_threshold = 0xff;
974 /* default to -3.5dB de-emphasis */
978 * default to assert utmi_sleep_n and use maximum allowed HIRD
979 * threshold value of 0b1100
983 dwc->hsphy_mode = usb_get_phy_mode(dev_ofnode(dev));
985 dwc->has_lpm_erratum = dev_read_bool(dev,
986 "snps,has-lpm-erratum");
987 tmp = dev_read_u8_array_ptr(dev, "snps,lpm-nyet-threshold", 1);
989 lpm_nyet_threshold = *tmp;
991 dwc->is_utmi_l1_suspend = dev_read_bool(dev,
992 "snps,is-utmi-l1-suspend");
993 tmp = dev_read_u8_array_ptr(dev, "snps,hird-threshold", 1);
995 hird_threshold = *tmp;
997 dwc->disable_scramble_quirk = dev_read_bool(dev,
998 "snps,disable_scramble_quirk");
999 dwc->u2exit_lfps_quirk = dev_read_bool(dev,
1000 "snps,u2exit_lfps_quirk");
1001 dwc->u2ss_inp3_quirk = dev_read_bool(dev,
1002 "snps,u2ss_inp3_quirk");
1003 dwc->req_p1p2p3_quirk = dev_read_bool(dev,
1004 "snps,req_p1p2p3_quirk");
1005 dwc->del_p1p2p3_quirk = dev_read_bool(dev,
1006 "snps,del_p1p2p3_quirk");
1007 dwc->del_phy_power_chg_quirk = dev_read_bool(dev,
1008 "snps,del_phy_power_chg_quirk");
1009 dwc->lfps_filter_quirk = dev_read_bool(dev,
1010 "snps,lfps_filter_quirk");
1011 dwc->rx_detect_poll_quirk = dev_read_bool(dev,
1012 "snps,rx_detect_poll_quirk");
1013 dwc->dis_u3_susphy_quirk = dev_read_bool(dev,
1014 "snps,dis_u3_susphy_quirk");
1015 dwc->dis_u2_susphy_quirk = dev_read_bool(dev,
1016 "snps,dis_u2_susphy_quirk");
1017 dwc->dis_del_phy_power_chg_quirk = dev_read_bool(dev,
1018 "snps,dis-del-phy-power-chg-quirk");
1019 dwc->dis_tx_ipgap_linecheck_quirk = dev_read_bool(dev,
1020 "snps,dis-tx-ipgap-linecheck-quirk");
1021 dwc->dis_enblslpm_quirk = dev_read_bool(dev,
1022 "snps,dis_enblslpm_quirk");
1023 dwc->dis_u2_freeclk_exists_quirk = dev_read_bool(dev,
1024 "snps,dis-u2-freeclk-exists-quirk");
1025 dwc->tx_de_emphasis_quirk = dev_read_bool(dev,
1026 "snps,tx_de_emphasis_quirk");
1027 tmp = dev_read_u8_array_ptr(dev, "snps,tx_de_emphasis", 1);
1029 tx_de_emphasis = *tmp;
1031 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1032 dwc->tx_de_emphasis = tx_de_emphasis;
1034 dwc->hird_threshold = hird_threshold
1035 | (dwc->is_utmi_l1_suspend << 4);
1037 dev_read_u32(dev, "snps,quirk-frame-length-adjustment", &dwc->fladj);
1040 * Handle property "snps,incr-burst-type-adjustment".
1041 * Get the number of value from this property:
1042 * result <= 0, means this property is not supported.
1043 * result = 1, means INCRx burst mode supported.
1044 * result > 1, means undefined length burst mode supported.
1046 dwc->incrx_mode = INCRX_BURST_MODE;
1047 dwc->incrx_size = 0;
1048 for (i = 0; i < 8; i++) {
1049 if (dev_read_u32_index(dev, "snps,incr-burst-type-adjustment",
1053 dwc->incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
1054 dwc->incrx_size = max(dwc->incrx_size, val);
1058 int dwc3_init(struct dwc3 *dwc)
1063 dwc3_cache_hwparams(dwc);
1065 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1067 dev_err(dwc->dev, "failed to allocate event buffers\n");
1071 ret = dwc3_core_init(dwc);
1073 dev_err(dwc->dev, "failed to initialize core\n");
1077 ret = dwc3_event_buffers_setup(dwc);
1079 dev_err(dwc->dev, "failed to setup event buffers\n");
1083 if (dwc->revision >= DWC3_REVISION_250A) {
1084 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1087 * Enable hardware control of sending remote wakeup
1088 * in HS when the device is in the L1 state.
1090 if (dwc->revision >= DWC3_REVISION_290A)
1091 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1093 if (dwc->dis_tx_ipgap_linecheck_quirk)
1094 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1096 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1099 if (dwc->dr_mode == USB_DR_MODE_HOST ||
1100 dwc->dr_mode == USB_DR_MODE_OTG) {
1101 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
1103 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1105 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1108 ret = dwc3_core_init_mode(dwc);
1115 dwc3_event_buffers_cleanup(dwc);
1118 dwc3_core_exit(dwc);
1121 dwc3_free_event_buffers(dwc);
1126 void dwc3_remove(struct dwc3 *dwc)
1128 dwc3_core_exit_mode(dwc);
1129 dwc3_event_buffers_cleanup(dwc);
1130 dwc3_free_event_buffers(dwc);
1131 dwc3_core_exit(dwc);