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
20 #include <dwc3-uboot.h>
21 #include <dm/device_compat.h>
22 #include <dm/devres.h>
23 #include <linux/bug.h>
24 #include <linux/delay.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/err.h>
27 #include <linux/ioport.h>
29 #include <generic-phy.h>
30 #include <linux/usb/ch9.h>
31 #include <linux/usb/gadget.h>
32 #include <linux/bitfield.h>
33 #include <linux/math64.h>
39 #include "linux-compat.h"
41 #define NSEC_PER_SEC 1000000000L
43 static LIST_HEAD(dwc3_list);
44 /* -------------------------------------------------------------------------- */
46 static void dwc3_set_mode(struct dwc3 *dwc, u32 mode)
50 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
51 reg &= ~(DWC3_GCTL_PRTCAPDIR(DWC3_GCTL_PRTCAP_OTG));
52 reg |= DWC3_GCTL_PRTCAPDIR(mode);
53 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
57 * dwc3_core_soft_reset - Issues core soft reset and PHY reset
58 * @dwc: pointer to our context structure
60 static int dwc3_core_soft_reset(struct dwc3 *dwc)
64 /* Before Resetting PHY, put Core in Reset */
65 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
66 reg |= DWC3_GCTL_CORESOFTRESET;
67 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
69 /* Assert USB3 PHY reset */
70 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
71 reg |= DWC3_GUSB3PIPECTL_PHYSOFTRST;
72 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
74 /* Assert USB2 PHY reset */
75 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
76 reg |= DWC3_GUSB2PHYCFG_PHYSOFTRST;
77 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
81 /* Clear USB3 PHY reset */
82 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
83 reg &= ~DWC3_GUSB3PIPECTL_PHYSOFTRST;
84 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
86 /* Clear USB2 PHY reset */
87 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
88 reg &= ~DWC3_GUSB2PHYCFG_PHYSOFTRST;
89 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
93 /* After PHYs are stable we can take Core out of reset state */
94 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
95 reg &= ~DWC3_GCTL_CORESOFTRESET;
96 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
102 * dwc3_frame_length_adjustment - Adjusts frame length if required
103 * @dwc3: Pointer to our controller context structure
104 * @fladj: Value of GFLADJ_30MHZ to adjust frame length
106 static void dwc3_frame_length_adjustment(struct dwc3 *dwc, u32 fladj)
110 if (dwc->revision < DWC3_REVISION_250A)
116 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
117 reg &= ~DWC3_GFLADJ_30MHZ_MASK;
118 reg |= DWC3_GFLADJ_30MHZ_SDBND_SEL | fladj;
119 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
123 * dwc3_ref_clk_period - Reference clock period configuration
124 * Default reference clock period depends on hardware
125 * configuration. For systems with reference clock that differs
126 * from the default, this will set clock period in DWC3_GUCTL
128 * @dwc: Pointer to our controller context structure
129 * @ref_clk_per: reference clock period in ns
131 static void dwc3_ref_clk_period(struct dwc3 *dwc)
133 unsigned long period;
140 rate = clk_get_rate(dwc->ref_clk);
143 period = NSEC_PER_SEC / rate;
148 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
149 reg &= ~DWC3_GUCTL_REFCLKPER_MASK;
150 reg |= FIELD_PREP(DWC3_GUCTL_REFCLKPER_MASK, period);
151 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
153 if (dwc->revision <= DWC3_REVISION_250A)
157 * The calculation below is
159 * 125000 * (NSEC_PER_SEC / (rate * period) - 1)
161 * but rearranged for fixed-point arithmetic. The division must be
162 * 64-bit because 125000 * NSEC_PER_SEC doesn't fit in 32 bits (and
163 * neither does rate * period).
165 * Note that rate * period ~= NSEC_PER_SECOND, minus the number of
166 * nanoseconds of error caused by the truncation which happened during
167 * the division when calculating rate or period (whichever one was
168 * derived from the other). We first calculate the relative error, then
169 * scale it to units of 8 ppm.
171 fladj = div64_u64(125000ULL * NSEC_PER_SEC, (u64)rate * period);
175 * The documented 240MHz constant is scaled by 2 to get PLS1 as well.
177 decr = 480000000 / rate;
179 reg = dwc3_readl(dwc->regs, DWC3_GFLADJ);
180 reg &= ~DWC3_GFLADJ_REFCLK_FLADJ_MASK
181 & ~DWC3_GFLADJ_240MHZDECR
182 & ~DWC3_GFLADJ_240MHZDECR_PLS1;
183 reg |= FIELD_PREP(DWC3_GFLADJ_REFCLK_FLADJ_MASK, fladj)
184 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR, decr >> 1)
185 | FIELD_PREP(DWC3_GFLADJ_240MHZDECR_PLS1, decr & 1);
186 dwc3_writel(dwc->regs, DWC3_GFLADJ, reg);
190 * dwc3_free_one_event_buffer - Frees one event buffer
191 * @dwc: Pointer to our controller context structure
192 * @evt: Pointer to event buffer to be freed
194 static void dwc3_free_one_event_buffer(struct dwc3 *dwc,
195 struct dwc3_event_buffer *evt)
197 dma_free_coherent(evt->buf);
201 * dwc3_alloc_one_event_buffer - Allocates one event buffer structure
202 * @dwc: Pointer to our controller context structure
203 * @length: size of the event buffer
205 * Returns a pointer to the allocated event buffer structure on success
206 * otherwise ERR_PTR(errno).
208 static struct dwc3_event_buffer *dwc3_alloc_one_event_buffer(struct dwc3 *dwc,
211 struct dwc3_event_buffer *evt;
213 evt = devm_kzalloc((struct udevice *)dwc->dev, sizeof(*evt),
216 return ERR_PTR(-ENOMEM);
219 evt->length = length;
220 evt->buf = dma_alloc_coherent(length,
221 (unsigned long *)&evt->dma);
223 return ERR_PTR(-ENOMEM);
225 dwc3_flush_cache((uintptr_t)evt->buf, evt->length);
231 * dwc3_free_event_buffers - frees all allocated event buffers
232 * @dwc: Pointer to our controller context structure
234 static void dwc3_free_event_buffers(struct dwc3 *dwc)
236 struct dwc3_event_buffer *evt;
239 for (i = 0; i < dwc->num_event_buffers; i++) {
240 evt = dwc->ev_buffs[i];
242 dwc3_free_one_event_buffer(dwc, evt);
247 * dwc3_alloc_event_buffers - Allocates @num event buffers of size @length
248 * @dwc: pointer to our controller context structure
249 * @length: size of event buffer
251 * Returns 0 on success otherwise negative errno. In the error case, dwc
252 * may contain some buffers allocated but not all which were requested.
254 static int dwc3_alloc_event_buffers(struct dwc3 *dwc, unsigned length)
259 num = DWC3_NUM_INT(dwc->hwparams.hwparams1);
260 dwc->num_event_buffers = num;
262 dwc->ev_buffs = memalign(CONFIG_SYS_CACHELINE_SIZE,
263 sizeof(*dwc->ev_buffs) * num);
267 for (i = 0; i < num; i++) {
268 struct dwc3_event_buffer *evt;
270 evt = dwc3_alloc_one_event_buffer(dwc, length);
272 dev_err(dwc->dev, "can't allocate event buffer\n");
275 dwc->ev_buffs[i] = evt;
282 * dwc3_event_buffers_setup - setup our allocated event buffers
283 * @dwc: pointer to our controller context structure
285 * Returns 0 on success otherwise negative errno.
287 static int dwc3_event_buffers_setup(struct dwc3 *dwc)
289 struct dwc3_event_buffer *evt;
292 for (n = 0; n < dwc->num_event_buffers; n++) {
293 evt = dwc->ev_buffs[n];
294 dev_dbg(dwc->dev, "Event buf %p dma %08llx length %d\n",
295 evt->buf, (unsigned long long) evt->dma,
300 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n),
301 lower_32_bits(evt->dma));
302 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n),
303 upper_32_bits(evt->dma));
304 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n),
305 DWC3_GEVNTSIZ_SIZE(evt->length));
306 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
312 static void dwc3_event_buffers_cleanup(struct dwc3 *dwc)
314 struct dwc3_event_buffer *evt;
317 for (n = 0; n < dwc->num_event_buffers; n++) {
318 evt = dwc->ev_buffs[n];
322 dwc3_writel(dwc->regs, DWC3_GEVNTADRLO(n), 0);
323 dwc3_writel(dwc->regs, DWC3_GEVNTADRHI(n), 0);
324 dwc3_writel(dwc->regs, DWC3_GEVNTSIZ(n), DWC3_GEVNTSIZ_INTMASK
325 | DWC3_GEVNTSIZ_SIZE(0));
326 dwc3_writel(dwc->regs, DWC3_GEVNTCOUNT(n), 0);
330 static int dwc3_alloc_scratch_buffers(struct dwc3 *dwc)
332 if (!dwc->has_hibernation)
335 if (!dwc->nr_scratch)
338 dwc->scratchbuf = kmalloc_array(dwc->nr_scratch,
339 DWC3_SCRATCHBUF_SIZE, GFP_KERNEL);
340 if (!dwc->scratchbuf)
346 static int dwc3_setup_scratch_buffers(struct dwc3 *dwc)
348 dma_addr_t scratch_addr;
352 if (!dwc->has_hibernation)
355 if (!dwc->nr_scratch)
358 scratch_addr = dma_map_single(dwc->scratchbuf,
359 dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
361 if (dma_mapping_error(dwc->dev, scratch_addr)) {
362 dev_err(dwc->dev, "failed to map scratch buffer\n");
367 dwc->scratch_addr = scratch_addr;
369 param = lower_32_bits(scratch_addr);
371 ret = dwc3_send_gadget_generic_command(dwc,
372 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_LO, param);
376 param = upper_32_bits(scratch_addr);
378 ret = dwc3_send_gadget_generic_command(dwc,
379 DWC3_DGCMD_SET_SCRATCHPAD_ADDR_HI, param);
386 dma_unmap_single(scratch_addr, dwc->nr_scratch * DWC3_SCRATCHBUF_SIZE,
393 static void dwc3_free_scratch_buffers(struct dwc3 *dwc)
395 if (!dwc->has_hibernation)
398 if (!dwc->nr_scratch)
401 dma_unmap_single(dwc->scratch_addr, dwc->nr_scratch *
402 DWC3_SCRATCHBUF_SIZE, DMA_BIDIRECTIONAL);
403 kfree(dwc->scratchbuf);
406 static void dwc3_core_num_eps(struct dwc3 *dwc)
408 struct dwc3_hwparams *parms = &dwc->hwparams;
410 dwc->num_in_eps = DWC3_NUM_IN_EPS(parms);
411 dwc->num_out_eps = DWC3_NUM_EPS(parms) - dwc->num_in_eps;
413 dev_vdbg(dwc->dev, "found %d IN and %d OUT endpoints\n",
414 dwc->num_in_eps, dwc->num_out_eps);
417 static void dwc3_cache_hwparams(struct dwc3 *dwc)
419 struct dwc3_hwparams *parms = &dwc->hwparams;
421 parms->hwparams0 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS0);
422 parms->hwparams1 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS1);
423 parms->hwparams2 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS2);
424 parms->hwparams3 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS3);
425 parms->hwparams4 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS4);
426 parms->hwparams5 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS5);
427 parms->hwparams6 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS6);
428 parms->hwparams7 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS7);
429 parms->hwparams8 = dwc3_readl(dwc->regs, DWC3_GHWPARAMS8);
432 static void dwc3_hsphy_mode_setup(struct dwc3 *dwc)
434 enum usb_phy_interface hsphy_mode = dwc->hsphy_mode;
437 /* Set dwc3 usb2 phy config */
438 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
440 switch (hsphy_mode) {
441 case USBPHY_INTERFACE_MODE_UTMI:
442 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
443 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
444 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_8_BIT) |
445 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_8_BIT);
447 case USBPHY_INTERFACE_MODE_UTMIW:
448 reg &= ~(DWC3_GUSB2PHYCFG_PHYIF_MASK |
449 DWC3_GUSB2PHYCFG_USBTRDTIM_MASK);
450 reg |= DWC3_GUSB2PHYCFG_PHYIF(UTMI_PHYIF_16_BIT) |
451 DWC3_GUSB2PHYCFG_USBTRDTIM(USBTRDTIM_UTMI_16_BIT);
457 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
461 * dwc3_phy_setup - Configure USB PHY Interface of DWC3 Core
462 * @dwc: Pointer to our controller context structure
464 static void dwc3_phy_setup(struct dwc3 *dwc)
468 reg = dwc3_readl(dwc->regs, DWC3_GUSB3PIPECTL(0));
471 * Above 1.94a, it is recommended to set DWC3_GUSB3PIPECTL_SUSPHY
472 * to '0' during coreConsultant configuration. So default value
473 * will be '0' when the core is reset. Application needs to set it
474 * to '1' after the core initialization is completed.
476 if (dwc->revision > DWC3_REVISION_194A)
477 reg |= DWC3_GUSB3PIPECTL_SUSPHY;
479 if (dwc->u2ss_inp3_quirk)
480 reg |= DWC3_GUSB3PIPECTL_U2SSINP3OK;
482 if (dwc->req_p1p2p3_quirk)
483 reg |= DWC3_GUSB3PIPECTL_REQP1P2P3;
485 if (dwc->del_p1p2p3_quirk)
486 reg |= DWC3_GUSB3PIPECTL_DEP1P2P3_EN;
488 if (dwc->del_phy_power_chg_quirk)
489 reg |= DWC3_GUSB3PIPECTL_DEPOCHANGE;
491 if (dwc->lfps_filter_quirk)
492 reg |= DWC3_GUSB3PIPECTL_LFPSFILT;
494 if (dwc->rx_detect_poll_quirk)
495 reg |= DWC3_GUSB3PIPECTL_RX_DETOPOLL;
497 if (dwc->tx_de_emphasis_quirk)
498 reg |= DWC3_GUSB3PIPECTL_TX_DEEPH(dwc->tx_de_emphasis);
500 if (dwc->dis_u3_susphy_quirk)
501 reg &= ~DWC3_GUSB3PIPECTL_SUSPHY;
503 if (dwc->dis_del_phy_power_chg_quirk)
504 reg &= ~DWC3_GUSB3PIPECTL_DEPOCHANGE;
506 dwc3_writel(dwc->regs, DWC3_GUSB3PIPECTL(0), reg);
508 dwc3_hsphy_mode_setup(dwc);
512 reg = dwc3_readl(dwc->regs, DWC3_GUSB2PHYCFG(0));
515 * Above 1.94a, it is recommended to set DWC3_GUSB2PHYCFG_SUSPHY to
516 * '0' during coreConsultant configuration. So default value will
517 * be '0' when the core is reset. Application needs to set it to
518 * '1' after the core initialization is completed.
520 if (dwc->revision > DWC3_REVISION_194A)
521 reg |= DWC3_GUSB2PHYCFG_SUSPHY;
523 if (dwc->dis_u2_susphy_quirk)
524 reg &= ~DWC3_GUSB2PHYCFG_SUSPHY;
526 if (dwc->dis_enblslpm_quirk)
527 reg &= ~DWC3_GUSB2PHYCFG_ENBLSLPM;
529 if (dwc->dis_u2_freeclk_exists_quirk)
530 reg &= ~DWC3_GUSB2PHYCFG_U2_FREECLK_EXISTS;
532 dwc3_writel(dwc->regs, DWC3_GUSB2PHYCFG(0), reg);
537 /* set global incr burst type configuration registers */
538 static void dwc3_set_incr_burst_type(struct dwc3 *dwc)
540 struct udevice *dev = dwc->dev;
543 if (!dwc->incrx_size)
546 cfg = dwc3_readl(dwc->regs, DWC3_GSBUSCFG0);
548 /* Enable Undefined Length INCR Burst and Enable INCRx Burst */
549 cfg &= ~DWC3_GSBUSCFG0_INCRBRST_MASK;
551 cfg |= DWC3_GSBUSCFG0_INCRBRSTENA;
552 switch (dwc->incrx_size) {
554 cfg |= DWC3_GSBUSCFG0_INCR256BRSTENA;
557 cfg |= DWC3_GSBUSCFG0_INCR128BRSTENA;
560 cfg |= DWC3_GSBUSCFG0_INCR64BRSTENA;
563 cfg |= DWC3_GSBUSCFG0_INCR32BRSTENA;
566 cfg |= DWC3_GSBUSCFG0_INCR16BRSTENA;
569 cfg |= DWC3_GSBUSCFG0_INCR8BRSTENA;
572 cfg |= DWC3_GSBUSCFG0_INCR4BRSTENA;
577 dev_err(dev, "Invalid property\n");
581 dwc3_writel(dwc->regs, DWC3_GSBUSCFG0, cfg);
585 * dwc3_core_init - Low-level initialization of DWC3 Core
586 * @dwc: Pointer to our controller context structure
588 * Returns 0 on success otherwise negative errno.
590 static int dwc3_core_init(struct dwc3 *dwc)
592 unsigned long timeout;
593 u32 hwparams4 = dwc->hwparams.hwparams4;
597 reg = dwc3_readl(dwc->regs, DWC3_GSNPSID);
598 /* This should read as U3 followed by revision number */
599 if ((reg & DWC3_GSNPSID_MASK) != 0x55330000) {
600 dev_err(dwc->dev, "this is not a DesignWare USB3 DRD Core\n");
606 /* Handle USB2.0-only core configuration */
607 if (DWC3_GHWPARAMS3_SSPHY_IFC(dwc->hwparams.hwparams3) ==
608 DWC3_GHWPARAMS3_SSPHY_IFC_DIS) {
609 if (dwc->maximum_speed == USB_SPEED_SUPER)
610 dwc->maximum_speed = USB_SPEED_HIGH;
613 /* issue device SoftReset too */
615 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_CSFTRST);
617 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
618 if (!(reg & DWC3_DCTL_CSFTRST))
623 dev_err(dwc->dev, "Reset Timed Out\n");
630 ret = dwc3_core_soft_reset(dwc);
634 reg = dwc3_readl(dwc->regs, DWC3_GCTL);
635 reg &= ~DWC3_GCTL_SCALEDOWN_MASK;
637 switch (DWC3_GHWPARAMS1_EN_PWROPT(dwc->hwparams.hwparams1)) {
638 case DWC3_GHWPARAMS1_EN_PWROPT_CLK:
640 * WORKAROUND: DWC3 revisions between 2.10a and 2.50a have an
641 * issue which would cause xHCI compliance tests to fail.
643 * Because of that we cannot enable clock gating on such
648 * STAR#9000588375: Clock Gating, SOF Issues when ref_clk-Based
651 if ((dwc->dr_mode == USB_DR_MODE_HOST ||
652 dwc->dr_mode == USB_DR_MODE_OTG) &&
653 (dwc->revision >= DWC3_REVISION_210A &&
654 dwc->revision <= DWC3_REVISION_250A))
655 reg |= DWC3_GCTL_DSBLCLKGTNG | DWC3_GCTL_SOFITPSYNC;
657 reg &= ~DWC3_GCTL_DSBLCLKGTNG;
659 case DWC3_GHWPARAMS1_EN_PWROPT_HIB:
660 /* enable hibernation here */
661 dwc->nr_scratch = DWC3_GHWPARAMS4_HIBER_SCRATCHBUFS(hwparams4);
664 * REVISIT Enabling this bit so that host-mode hibernation
665 * will work. Device-mode hibernation is not yet implemented.
667 reg |= DWC3_GCTL_GBLHIBERNATIONEN;
670 dev_dbg(dwc->dev, "No power optimization available\n");
673 /* check if current dwc3 is on simulation board */
674 if (dwc->hwparams.hwparams6 & DWC3_GHWPARAMS6_EN_FPGA) {
675 dev_dbg(dwc->dev, "it is on FPGA board\n");
679 if(dwc->disable_scramble_quirk && !dwc->is_fpga)
681 "disable_scramble cannot be used on non-FPGA builds\n");
683 if (dwc->disable_scramble_quirk && dwc->is_fpga)
684 reg |= DWC3_GCTL_DISSCRAMBLE;
686 reg &= ~DWC3_GCTL_DISSCRAMBLE;
688 if (dwc->u2exit_lfps_quirk)
689 reg |= DWC3_GCTL_U2EXIT_LFPS;
692 * WORKAROUND: DWC3 revisions <1.90a have a bug
693 * where the device can fail to connect at SuperSpeed
694 * and falls back to high-speed mode which causes
695 * the device to enter a Connect/Disconnect loop
697 if (dwc->revision < DWC3_REVISION_190A)
698 reg |= DWC3_GCTL_U2RSTECN;
700 dwc3_core_num_eps(dwc);
702 dwc3_writel(dwc->regs, DWC3_GCTL, reg);
704 ret = dwc3_alloc_scratch_buffers(dwc);
708 ret = dwc3_setup_scratch_buffers(dwc);
712 /* Adjust Frame Length */
713 dwc3_frame_length_adjustment(dwc, dwc->fladj);
715 /* Adjust Reference Clock Period */
716 dwc3_ref_clk_period(dwc);
718 dwc3_set_incr_burst_type(dwc);
723 dwc3_free_scratch_buffers(dwc);
729 static void dwc3_core_exit(struct dwc3 *dwc)
731 dwc3_free_scratch_buffers(dwc);
734 static int dwc3_core_init_mode(struct dwc3 *dwc)
738 switch (dwc->dr_mode) {
739 case USB_DR_MODE_PERIPHERAL:
740 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
741 ret = dwc3_gadget_init(dwc);
743 dev_err(dwc->dev, "failed to initialize gadget\n");
747 case USB_DR_MODE_HOST:
748 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_HOST);
749 ret = dwc3_host_init(dwc);
751 dev_err(dwc->dev, "failed to initialize host\n");
755 case USB_DR_MODE_OTG:
756 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_OTG);
757 ret = dwc3_host_init(dwc);
759 dev_err(dwc->dev, "failed to initialize host\n");
763 ret = dwc3_gadget_init(dwc);
765 dev_err(dwc->dev, "failed to initialize gadget\n");
771 "Unsupported mode of operation %d\n", dwc->dr_mode);
778 static void dwc3_gadget_run(struct dwc3 *dwc)
780 dwc3_writel(dwc->regs, DWC3_DCTL, DWC3_DCTL_RUN_STOP);
784 static void dwc3_core_stop(struct dwc3 *dwc)
788 reg = dwc3_readl(dwc->regs, DWC3_DCTL);
789 dwc3_writel(dwc->regs, DWC3_DCTL, reg & ~(DWC3_DCTL_RUN_STOP));
792 static void dwc3_core_exit_mode(struct dwc3 *dwc)
794 switch (dwc->dr_mode) {
795 case USB_DR_MODE_PERIPHERAL:
796 dwc3_gadget_exit(dwc);
798 case USB_DR_MODE_HOST:
801 case USB_DR_MODE_OTG:
803 dwc3_gadget_exit(dwc);
811 * switch back to peripheral mode
812 * This enables the phy to enter idle and then, if enabled, suspend.
814 dwc3_set_mode(dwc, DWC3_GCTL_PRTCAP_DEVICE);
815 dwc3_gadget_run(dwc);
818 #define DWC3_ALIGN_MASK (16 - 1)
821 * dwc3_uboot_init - dwc3 core uboot initialization code
822 * @dwc3_dev: struct dwc3_device containing initialization data
824 * Entry point for dwc3 driver (equivalent to dwc3_probe in linux
825 * kernel driver). Pointer to dwc3_device should be passed containing
826 * base address and other initialization data. Returns '0' on success and
827 * a negative value on failure.
829 * Generally called from board_usb_init() implemented in board file.
831 int dwc3_uboot_init(struct dwc3_device *dwc3_dev)
834 struct device *dev = NULL;
835 u8 lpm_nyet_threshold;
843 mem = devm_kzalloc((struct udevice *)dev,
844 sizeof(*dwc) + DWC3_ALIGN_MASK, GFP_KERNEL);
848 dwc = PTR_ALIGN(mem, DWC3_ALIGN_MASK + 1);
851 dwc->regs = (void *)(uintptr_t)(dwc3_dev->base +
852 DWC3_GLOBALS_REGS_START);
854 /* default to highest possible threshold */
855 lpm_nyet_threshold = 0xff;
857 /* default to -3.5dB de-emphasis */
861 * default to assert utmi_sleep_n and use maximum allowed HIRD
862 * threshold value of 0b1100
866 dwc->maximum_speed = dwc3_dev->maximum_speed;
867 dwc->has_lpm_erratum = dwc3_dev->has_lpm_erratum;
868 if (dwc3_dev->lpm_nyet_threshold)
869 lpm_nyet_threshold = dwc3_dev->lpm_nyet_threshold;
870 dwc->is_utmi_l1_suspend = dwc3_dev->is_utmi_l1_suspend;
871 if (dwc3_dev->hird_threshold)
872 hird_threshold = dwc3_dev->hird_threshold;
874 dwc->needs_fifo_resize = dwc3_dev->tx_fifo_resize;
875 dwc->dr_mode = dwc3_dev->dr_mode;
877 dwc->disable_scramble_quirk = dwc3_dev->disable_scramble_quirk;
878 dwc->u2exit_lfps_quirk = dwc3_dev->u2exit_lfps_quirk;
879 dwc->u2ss_inp3_quirk = dwc3_dev->u2ss_inp3_quirk;
880 dwc->req_p1p2p3_quirk = dwc3_dev->req_p1p2p3_quirk;
881 dwc->del_p1p2p3_quirk = dwc3_dev->del_p1p2p3_quirk;
882 dwc->del_phy_power_chg_quirk = dwc3_dev->del_phy_power_chg_quirk;
883 dwc->lfps_filter_quirk = dwc3_dev->lfps_filter_quirk;
884 dwc->rx_detect_poll_quirk = dwc3_dev->rx_detect_poll_quirk;
885 dwc->dis_u3_susphy_quirk = dwc3_dev->dis_u3_susphy_quirk;
886 dwc->dis_u2_susphy_quirk = dwc3_dev->dis_u2_susphy_quirk;
887 dwc->dis_del_phy_power_chg_quirk = dwc3_dev->dis_del_phy_power_chg_quirk;
888 dwc->dis_tx_ipgap_linecheck_quirk = dwc3_dev->dis_tx_ipgap_linecheck_quirk;
889 dwc->dis_enblslpm_quirk = dwc3_dev->dis_enblslpm_quirk;
890 dwc->dis_u2_freeclk_exists_quirk = dwc3_dev->dis_u2_freeclk_exists_quirk;
892 dwc->tx_de_emphasis_quirk = dwc3_dev->tx_de_emphasis_quirk;
893 if (dwc3_dev->tx_de_emphasis)
894 tx_de_emphasis = dwc3_dev->tx_de_emphasis;
896 /* default to superspeed if no maximum_speed passed */
897 if (dwc->maximum_speed == USB_SPEED_UNKNOWN)
898 dwc->maximum_speed = USB_SPEED_SUPER;
900 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
901 dwc->tx_de_emphasis = tx_de_emphasis;
903 dwc->hird_threshold = hird_threshold
904 | (dwc->is_utmi_l1_suspend << 4);
906 dwc->hsphy_mode = dwc3_dev->hsphy_mode;
908 dwc->index = dwc3_dev->index;
910 dwc3_cache_hwparams(dwc);
912 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
914 dev_err(dwc->dev, "failed to allocate event buffers\n");
918 if (!IS_ENABLED(CONFIG_USB_DWC3_GADGET))
919 dwc->dr_mode = USB_DR_MODE_HOST;
920 else if (!IS_ENABLED(CONFIG_USB_HOST))
921 dwc->dr_mode = USB_DR_MODE_PERIPHERAL;
923 if (dwc->dr_mode == USB_DR_MODE_UNKNOWN)
924 dwc->dr_mode = USB_DR_MODE_OTG;
926 ret = dwc3_core_init(dwc);
928 dev_err(dwc->dev, "failed to initialize core\n");
932 ret = dwc3_event_buffers_setup(dwc);
934 dev_err(dwc->dev, "failed to setup event buffers\n");
938 ret = dwc3_core_init_mode(dwc);
942 list_add_tail(&dwc->list, &dwc3_list);
947 dwc3_event_buffers_cleanup(dwc);
953 dwc3_free_event_buffers(dwc);
959 * dwc3_uboot_exit - dwc3 core uboot cleanup code
960 * @index: index of this controller
962 * Performs cleanup of memory allocated in dwc3_uboot_init and other misc
963 * cleanups (equivalent to dwc3_remove in linux). index of _this_ controller
964 * should be passed and should match with the index passed in
965 * dwc3_device during init.
967 * Generally called from board file.
969 void dwc3_uboot_exit(int index)
973 list_for_each_entry(dwc, &dwc3_list, list) {
974 if (dwc->index != index)
977 dwc3_core_exit_mode(dwc);
978 dwc3_event_buffers_cleanup(dwc);
979 dwc3_free_event_buffers(dwc);
981 list_del(&dwc->list);
988 * dwc3_uboot_handle_interrupt - handle dwc3 core interrupt
989 * @index: index of this controller
991 * Invokes dwc3 gadget interrupts.
993 * Generally called from board file.
995 void dwc3_uboot_handle_interrupt(int index)
997 struct dwc3 *dwc = NULL;
999 list_for_each_entry(dwc, &dwc3_list, list) {
1000 if (dwc->index != index)
1003 dwc3_gadget_uboot_handle_interrupt(dwc);
1008 MODULE_ALIAS("platform:dwc3");
1009 MODULE_AUTHOR("Felipe Balbi <balbi@ti.com>");
1010 MODULE_LICENSE("GPL v2");
1011 MODULE_DESCRIPTION("DesignWare USB3 DRD Controller Driver");
1013 #if CONFIG_IS_ENABLED(PHY) && CONFIG_IS_ENABLED(DM_USB)
1014 int dwc3_setup_phy(struct udevice *dev, struct phy_bulk *phys)
1018 ret = generic_phy_get_bulk(dev, phys);
1022 ret = generic_phy_init_bulk(phys);
1026 ret = generic_phy_power_on_bulk(phys);
1028 generic_phy_exit_bulk(phys);
1033 int dwc3_shutdown_phy(struct udevice *dev, struct phy_bulk *phys)
1037 ret = generic_phy_power_off_bulk(phys);
1038 ret |= generic_phy_exit_bulk(phys);
1043 #if CONFIG_IS_ENABLED(DM_USB)
1044 void dwc3_of_parse(struct dwc3 *dwc)
1047 struct udevice *dev = dwc->dev;
1048 u8 lpm_nyet_threshold;
1054 /* default to highest possible threshold */
1055 lpm_nyet_threshold = 0xff;
1057 /* default to -3.5dB de-emphasis */
1061 * default to assert utmi_sleep_n and use maximum allowed HIRD
1062 * threshold value of 0b1100
1064 hird_threshold = 12;
1066 dwc->hsphy_mode = usb_get_phy_mode(dev_ofnode(dev));
1068 dwc->has_lpm_erratum = dev_read_bool(dev,
1069 "snps,has-lpm-erratum");
1070 tmp = dev_read_u8_array_ptr(dev, "snps,lpm-nyet-threshold", 1);
1072 lpm_nyet_threshold = *tmp;
1074 dwc->is_utmi_l1_suspend = dev_read_bool(dev,
1075 "snps,is-utmi-l1-suspend");
1076 tmp = dev_read_u8_array_ptr(dev, "snps,hird-threshold", 1);
1078 hird_threshold = *tmp;
1080 dwc->disable_scramble_quirk = dev_read_bool(dev,
1081 "snps,disable_scramble_quirk");
1082 dwc->u2exit_lfps_quirk = dev_read_bool(dev,
1083 "snps,u2exit_lfps_quirk");
1084 dwc->u2ss_inp3_quirk = dev_read_bool(dev,
1085 "snps,u2ss_inp3_quirk");
1086 dwc->req_p1p2p3_quirk = dev_read_bool(dev,
1087 "snps,req_p1p2p3_quirk");
1088 dwc->del_p1p2p3_quirk = dev_read_bool(dev,
1089 "snps,del_p1p2p3_quirk");
1090 dwc->del_phy_power_chg_quirk = dev_read_bool(dev,
1091 "snps,del_phy_power_chg_quirk");
1092 dwc->lfps_filter_quirk = dev_read_bool(dev,
1093 "snps,lfps_filter_quirk");
1094 dwc->rx_detect_poll_quirk = dev_read_bool(dev,
1095 "snps,rx_detect_poll_quirk");
1096 dwc->dis_u3_susphy_quirk = dev_read_bool(dev,
1097 "snps,dis_u3_susphy_quirk");
1098 dwc->dis_u2_susphy_quirk = dev_read_bool(dev,
1099 "snps,dis_u2_susphy_quirk");
1100 dwc->dis_del_phy_power_chg_quirk = dev_read_bool(dev,
1101 "snps,dis-del-phy-power-chg-quirk");
1102 dwc->dis_tx_ipgap_linecheck_quirk = dev_read_bool(dev,
1103 "snps,dis-tx-ipgap-linecheck-quirk");
1104 dwc->dis_enblslpm_quirk = dev_read_bool(dev,
1105 "snps,dis_enblslpm_quirk");
1106 dwc->dis_u2_freeclk_exists_quirk = dev_read_bool(dev,
1107 "snps,dis-u2-freeclk-exists-quirk");
1108 dwc->tx_de_emphasis_quirk = dev_read_bool(dev,
1109 "snps,tx_de_emphasis_quirk");
1110 tmp = dev_read_u8_array_ptr(dev, "snps,tx_de_emphasis", 1);
1112 tx_de_emphasis = *tmp;
1114 dwc->lpm_nyet_threshold = lpm_nyet_threshold;
1115 dwc->tx_de_emphasis = tx_de_emphasis;
1117 dwc->hird_threshold = hird_threshold
1118 | (dwc->is_utmi_l1_suspend << 4);
1120 dev_read_u32(dev, "snps,quirk-frame-length-adjustment", &dwc->fladj);
1123 * Handle property "snps,incr-burst-type-adjustment".
1124 * Get the number of value from this property:
1125 * result <= 0, means this property is not supported.
1126 * result = 1, means INCRx burst mode supported.
1127 * result > 1, means undefined length burst mode supported.
1129 dwc->incrx_mode = INCRX_BURST_MODE;
1130 dwc->incrx_size = 0;
1131 for (i = 0; i < 8; i++) {
1132 if (dev_read_u32_index(dev, "snps,incr-burst-type-adjustment",
1136 dwc->incrx_mode = INCRX_UNDEF_LENGTH_BURST_MODE;
1137 dwc->incrx_size = max(dwc->incrx_size, val);
1141 int dwc3_init(struct dwc3 *dwc)
1146 dwc3_cache_hwparams(dwc);
1148 ret = dwc3_alloc_event_buffers(dwc, DWC3_EVENT_BUFFERS_SIZE);
1150 dev_err(dwc->dev, "failed to allocate event buffers\n");
1154 ret = dwc3_core_init(dwc);
1156 dev_err(dwc->dev, "failed to initialize core\n");
1160 ret = dwc3_event_buffers_setup(dwc);
1162 dev_err(dwc->dev, "failed to setup event buffers\n");
1166 if (dwc->revision >= DWC3_REVISION_250A) {
1167 reg = dwc3_readl(dwc->regs, DWC3_GUCTL1);
1170 * Enable hardware control of sending remote wakeup
1171 * in HS when the device is in the L1 state.
1173 if (dwc->revision >= DWC3_REVISION_290A)
1174 reg |= DWC3_GUCTL1_DEV_L1_EXIT_BY_HW;
1176 if (dwc->dis_tx_ipgap_linecheck_quirk)
1177 reg |= DWC3_GUCTL1_TX_IPGAP_LINECHECK_DIS;
1179 dwc3_writel(dwc->regs, DWC3_GUCTL1, reg);
1182 if (dwc->dr_mode == USB_DR_MODE_HOST ||
1183 dwc->dr_mode == USB_DR_MODE_OTG) {
1184 reg = dwc3_readl(dwc->regs, DWC3_GUCTL);
1186 reg |= DWC3_GUCTL_HSTINAUTORETRY;
1188 dwc3_writel(dwc->regs, DWC3_GUCTL, reg);
1191 ret = dwc3_core_init_mode(dwc);
1198 dwc3_event_buffers_cleanup(dwc);
1201 dwc3_core_exit(dwc);
1204 dwc3_free_event_buffers(dwc);
1209 void dwc3_remove(struct dwc3 *dwc)
1211 dwc3_core_exit_mode(dwc);
1212 dwc3_event_buffers_cleanup(dwc);
1213 dwc3_free_event_buffers(dwc);
1214 dwc3_core_stop(dwc);
1215 dwc3_core_exit(dwc);