1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
4 * Copyright (C) 2014 Marek Vasut <marex@denx.de>
12 #include <generic-phy.h>
18 #include <usbroothubdes.h>
20 #include <asm/cache.h>
22 #include <dm/device_compat.h>
23 #include <linux/delay.h>
24 #include <power/regulator.h>
29 /* Use only HC channel 0. */
30 #define DWC2_HC_CHANNEL 0
32 #define DWC2_STATUS_BUF_SIZE 64
33 #define DWC2_DATA_BUF_SIZE (CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
36 #define MAX_ENDPOINT 16
39 #if CONFIG_IS_ENABLED(DM_USB)
40 uint8_t aligned_buffer[DWC2_DATA_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
41 uint8_t status_buffer[DWC2_STATUS_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
42 #ifdef CONFIG_DM_REGULATOR
43 struct udevice *vbus_supply;
48 uint8_t *aligned_buffer;
49 uint8_t *status_buffer;
51 u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
52 u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
53 struct dwc2_core_regs *regs;
57 * The hnp/srp capability must be disabled if the platform
58 * does't support hnp/srp. Otherwise the force mode can't work.
63 struct reset_ctl_bulk resets;
66 #if !CONFIG_IS_ENABLED(DM_USB)
67 /* We need cacheline-aligned buffers for DMA transfers and dcache support */
68 DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
70 DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
73 static struct dwc2_priv local;
81 * Initializes the FSLSPClkSel field of the HCFG register
82 * depending on the PHY type.
84 static void init_fslspclksel(struct dwc2_core_regs *regs)
88 #if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
89 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
91 /* High speed PHY running at full speed or high speed */
92 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
95 #ifdef CONFIG_DWC2_ULPI_FS_LS
96 uint32_t hwcfg2 = readl(®s->ghwcfg2);
97 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
98 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
99 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
100 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
102 if (hval == 2 && fval == 1)
103 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
106 clrsetbits_le32(®s->host_regs.hcfg,
107 DWC2_HCFG_FSLSPCLKSEL_MASK,
108 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
114 * @param regs Programming view of DWC_otg controller.
115 * @param num Tx FIFO to flush.
117 static void dwc_otg_flush_tx_fifo(struct udevice *dev,
118 struct dwc2_core_regs *regs, const int num)
122 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
124 ret = wait_for_bit_le32(®s->grstctl, DWC2_GRSTCTL_TXFFLSH,
127 dev_info(dev, "%s: Timeout!\n", __func__);
129 /* Wait for 3 PHY Clocks */
136 * @param regs Programming view of DWC_otg controller.
138 static void dwc_otg_flush_rx_fifo(struct udevice *dev,
139 struct dwc2_core_regs *regs)
143 writel(DWC2_GRSTCTL_RXFFLSH, ®s->grstctl);
144 ret = wait_for_bit_le32(®s->grstctl, DWC2_GRSTCTL_RXFFLSH,
147 dev_info(dev, "%s: Timeout!\n", __func__);
149 /* Wait for 3 PHY Clocks */
154 * Do core a soft reset of the core. Be careful with this because it
155 * resets all the internal state machines of the core.
157 static void dwc_otg_core_reset(struct udevice *dev,
158 struct dwc2_core_regs *regs)
162 /* Wait for AHB master IDLE state. */
163 ret = wait_for_bit_le32(®s->grstctl, DWC2_GRSTCTL_AHBIDLE,
166 dev_info(dev, "%s: Timeout!\n", __func__);
168 /* Core Soft Reset */
169 writel(DWC2_GRSTCTL_CSFTRST, ®s->grstctl);
170 ret = wait_for_bit_le32(®s->grstctl, DWC2_GRSTCTL_CSFTRST,
173 dev_info(dev, "%s: Timeout!\n", __func__);
176 * Wait for core to come out of reset.
177 * NOTE: This long sleep is _very_ important, otherwise the core will
178 * not stay in host mode after a connector ID change!
183 #if CONFIG_IS_ENABLED(DM_USB) && defined(CONFIG_DM_REGULATOR)
184 static int dwc_vbus_supply_init(struct udevice *dev)
186 struct dwc2_priv *priv = dev_get_priv(dev);
189 ret = device_get_supply_regulator(dev, "vbus-supply",
192 debug("%s: No vbus supply\n", dev->name);
196 ret = regulator_set_enable(priv->vbus_supply, true);
198 dev_err(dev, "Error enabling vbus supply\n");
205 static int dwc_vbus_supply_exit(struct udevice *dev)
207 struct dwc2_priv *priv = dev_get_priv(dev);
210 if (priv->vbus_supply) {
211 ret = regulator_set_enable(priv->vbus_supply, false);
213 dev_err(dev, "Error disabling vbus supply\n");
221 static int dwc_vbus_supply_init(struct udevice *dev)
226 #if CONFIG_IS_ENABLED(DM_USB)
227 static int dwc_vbus_supply_exit(struct udevice *dev)
235 * This function initializes the DWC_otg controller registers for
238 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
239 * request queues. Host channels are reset to ensure that they are ready for
240 * performing transfers.
242 * @param dev USB Device (NULL if driver model is not being used)
243 * @param regs Programming view of DWC_otg controller
246 static void dwc_otg_core_host_init(struct udevice *dev,
247 struct dwc2_core_regs *regs)
249 uint32_t nptxfifosize = 0;
250 uint32_t ptxfifosize = 0;
252 int i, ret, num_channels;
254 /* Restart the Phy Clock */
255 writel(0, ®s->pcgcctl);
257 /* Initialize Host Configuration Register */
258 init_fslspclksel(regs);
259 #ifdef CONFIG_DWC2_DFLT_SPEED_FULL
260 setbits_le32(®s->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
263 /* Configure data FIFO sizes */
264 #ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
265 if (readl(®s->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
267 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, ®s->grxfsiz);
269 /* Non-periodic Tx FIFO */
270 nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
271 DWC2_FIFOSIZE_DEPTH_OFFSET;
272 nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
273 DWC2_FIFOSIZE_STARTADDR_OFFSET;
274 writel(nptxfifosize, ®s->gnptxfsiz);
276 /* Periodic Tx FIFO */
277 ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
278 DWC2_FIFOSIZE_DEPTH_OFFSET;
279 ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
280 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
281 DWC2_FIFOSIZE_STARTADDR_OFFSET;
282 writel(ptxfifosize, ®s->hptxfsiz);
286 /* Clear Host Set HNP Enable in the OTG Control Register */
287 clrbits_le32(®s->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
289 /* Make sure the FIFOs are flushed. */
290 dwc_otg_flush_tx_fifo(dev, regs, 0x10); /* All Tx FIFOs */
291 dwc_otg_flush_rx_fifo(dev, regs);
293 /* Flush out any leftover queued requests. */
294 num_channels = readl(®s->ghwcfg2);
295 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
296 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
299 for (i = 0; i < num_channels; i++)
300 clrsetbits_le32(®s->hc_regs[i].hcchar,
301 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
304 /* Halt all channels to put them into a known state. */
305 for (i = 0; i < num_channels; i++) {
306 clrsetbits_le32(®s->hc_regs[i].hcchar,
308 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
309 ret = wait_for_bit_le32(®s->hc_regs[i].hcchar,
310 DWC2_HCCHAR_CHEN, false, 1000, false);
312 dev_info(dev, "%s: Timeout!\n", __func__);
315 /* Turn on the vbus power. */
316 if (readl(®s->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
317 hprt0 = readl(®s->hprt0);
318 hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
319 hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
320 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
321 hprt0 |= DWC2_HPRT0_PRTPWR;
322 writel(hprt0, ®s->hprt0);
327 dwc_vbus_supply_init(dev);
331 * This function initializes the DWC_otg controller registers and
332 * prepares the core for device mode or host mode operation.
334 * @param regs Programming view of the DWC_otg controller
336 static void dwc_otg_core_init(struct udevice *dev)
338 struct dwc2_priv *priv = dev_get_priv(dev);
339 struct dwc2_core_regs *regs = priv->regs;
342 uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
344 /* Common Initialization */
345 usbcfg = readl(®s->gusbcfg);
347 /* Program the ULPI External VBUS bit if needed */
348 if (priv->ext_vbus) {
349 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
350 if (!priv->oc_disable) {
351 usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
352 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
355 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
358 /* Set external TS Dline pulsing */
359 #ifdef CONFIG_DWC2_TS_DLINE
360 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
362 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
364 writel(usbcfg, ®s->gusbcfg);
366 /* Reset the Controller */
367 dwc_otg_core_reset(dev, regs);
370 * This programming sequence needs to happen in FS mode before
371 * any other programming occurs
373 #if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
374 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
375 /* If FS mode with FS PHY */
376 setbits_le32(®s->gusbcfg, DWC2_GUSBCFG_PHYSEL);
378 /* Reset after a PHY select */
379 dwc_otg_core_reset(dev, regs);
382 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
383 * Also do this on HNP Dev/Host mode switches (done in dev_init
386 if (readl(®s->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
387 init_fslspclksel(regs);
389 #ifdef CONFIG_DWC2_I2C_ENABLE
390 /* Program GUSBCFG.OtgUtmifsSel to I2C */
391 setbits_le32(®s->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
393 /* Program GI2CCTL.I2CEn */
394 clrsetbits_le32(®s->gi2cctl, DWC2_GI2CCTL_I2CEN |
395 DWC2_GI2CCTL_I2CDEVADDR_MASK,
396 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
397 setbits_le32(®s->gi2cctl, DWC2_GI2CCTL_I2CEN);
401 /* High speed PHY. */
404 * HS PHY parameters. These parameters are preserved during
405 * soft reset so only program the first time. Do a soft reset
406 * immediately after setting phyif.
408 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
409 usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
411 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
412 #ifdef CONFIG_DWC2_PHY_ULPI_DDR
413 usbcfg |= DWC2_GUSBCFG_DDRSEL;
415 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
417 } else { /* UTMI+ interface */
418 #if (CONFIG_DWC2_UTMI_WIDTH == 16)
419 usbcfg |= DWC2_GUSBCFG_PHYIF;
423 writel(usbcfg, ®s->gusbcfg);
425 /* Reset after setting the PHY parameters */
426 dwc_otg_core_reset(dev, regs);
429 usbcfg = readl(®s->gusbcfg);
430 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
431 #ifdef CONFIG_DWC2_ULPI_FS_LS
432 uint32_t hwcfg2 = readl(®s->ghwcfg2);
433 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
434 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
435 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
436 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
437 if (hval == 2 && fval == 1) {
438 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
439 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
442 if (priv->hnp_srp_disable)
443 usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
445 writel(usbcfg, ®s->gusbcfg);
447 /* Program the GAHBCFG Register. */
448 switch (readl(®s->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
449 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
451 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
452 while (brst_sz > 1) {
453 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
454 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
458 #ifdef CONFIG_DWC2_DMA_ENABLE
459 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
463 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
464 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
465 #ifdef CONFIG_DWC2_DMA_ENABLE
466 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
471 writel(ahbcfg, ®s->gahbcfg);
473 /* Program the capabilities in GUSBCFG Register */
476 if (!priv->hnp_srp_disable)
477 usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
478 #ifdef CONFIG_DWC2_IC_USB_CAP
479 usbcfg |= DWC2_GUSBCFG_IC_USB_CAP;
482 setbits_le32(®s->gusbcfg, usbcfg);
486 * Prepares a host channel for transferring packets to/from a specific
487 * endpoint. The HCCHARn register is set up with the characteristics specified
488 * in _hc. Host channel interrupts that may need to be serviced while this
489 * transfer is in progress are enabled.
491 * @param regs Programming view of DWC_otg controller
492 * @param hc Information needed to initialize the host channel
494 static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
495 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
496 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
498 struct dwc2_hc_regs *hc_regs = ®s->hc_regs[hc_num];
499 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
500 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
501 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
502 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
503 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
505 if (dev->speed == USB_SPEED_LOW)
506 hcchar |= DWC2_HCCHAR_LSPDDEV;
509 * Program the HCCHARn register with the endpoint characteristics
510 * for the current transfer.
512 writel(hcchar, &hc_regs->hcchar);
514 /* Program the HCSPLIT register, default to no SPLIT */
515 writel(0, &hc_regs->hcsplt);
518 static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
519 uint8_t hub_devnum, uint8_t hub_port)
523 hcsplt = DWC2_HCSPLT_SPLTENA;
524 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
525 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
527 /* Program the HCSPLIT register for SPLITs */
528 writel(hcsplt, &hc_regs->hcsplt);
532 * DWC2 to USB API interface
534 /* Direction: In ; Request: Status */
535 static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
536 struct usb_device *dev, void *buffer,
537 int txlen, struct devrequest *cmd)
540 uint32_t port_status = 0;
541 uint32_t port_change = 0;
545 switch (cmd->requesttype & ~USB_DIR_IN) {
547 *(uint16_t *)buffer = cpu_to_le16(1);
550 case USB_RECIP_INTERFACE:
551 case USB_RECIP_ENDPOINT:
552 *(uint16_t *)buffer = cpu_to_le16(0);
556 *(uint32_t *)buffer = cpu_to_le32(0);
559 case USB_RECIP_OTHER | USB_TYPE_CLASS:
560 hprt0 = readl(®s->hprt0);
561 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
562 port_status |= USB_PORT_STAT_CONNECTION;
563 if (hprt0 & DWC2_HPRT0_PRTENA)
564 port_status |= USB_PORT_STAT_ENABLE;
565 if (hprt0 & DWC2_HPRT0_PRTSUSP)
566 port_status |= USB_PORT_STAT_SUSPEND;
567 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
568 port_status |= USB_PORT_STAT_OVERCURRENT;
569 if (hprt0 & DWC2_HPRT0_PRTRST)
570 port_status |= USB_PORT_STAT_RESET;
571 if (hprt0 & DWC2_HPRT0_PRTPWR)
572 port_status |= USB_PORT_STAT_POWER;
574 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
575 port_status |= USB_PORT_STAT_LOW_SPEED;
576 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
577 DWC2_HPRT0_PRTSPD_HIGH)
578 port_status |= USB_PORT_STAT_HIGH_SPEED;
580 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
581 port_change |= USB_PORT_STAT_C_ENABLE;
582 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
583 port_change |= USB_PORT_STAT_C_CONNECTION;
584 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
585 port_change |= USB_PORT_STAT_C_OVERCURRENT;
587 *(uint32_t *)buffer = cpu_to_le32(port_status |
588 (port_change << 16));
592 puts("unsupported root hub command\n");
593 stat = USB_ST_STALLED;
596 dev->act_len = min(len, txlen);
602 /* Direction: In ; Request: Descriptor */
603 static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
604 void *buffer, int txlen,
605 struct devrequest *cmd)
607 unsigned char data[32];
611 uint16_t wValue = cpu_to_le16(cmd->value);
612 uint16_t wLength = cpu_to_le16(cmd->length);
614 switch (cmd->requesttype & ~USB_DIR_IN) {
616 switch (wValue & 0xff00) {
617 case 0x0100: /* device descriptor */
618 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
619 memcpy(buffer, root_hub_dev_des, len);
621 case 0x0200: /* configuration descriptor */
622 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
623 memcpy(buffer, root_hub_config_des, len);
625 case 0x0300: /* string descriptors */
626 switch (wValue & 0xff) {
628 len = min3(txlen, (int)sizeof(root_hub_str_index0),
630 memcpy(buffer, root_hub_str_index0, len);
633 len = min3(txlen, (int)sizeof(root_hub_str_index1),
635 memcpy(buffer, root_hub_str_index1, len);
640 stat = USB_ST_STALLED;
645 /* Root port config, set 1 port and nothing else. */
648 data[0] = 9; /* min length; */
650 data[2] = dsc & RH_A_NDP;
656 else if (dsc & RH_A_OCPM)
659 /* corresponds to data[4-7] */
660 data[5] = (dsc & RH_A_POTPGT) >> 24;
661 data[7] = dsc & RH_B_DR;
666 data[8] = (dsc & RH_B_DR) >> 8;
671 len = min3(txlen, (int)data[0], (int)wLength);
672 memcpy(buffer, data, len);
675 puts("unsupported root hub command\n");
676 stat = USB_ST_STALLED;
679 dev->act_len = min(len, txlen);
685 /* Direction: In ; Request: Configuration */
686 static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
687 void *buffer, int txlen,
688 struct devrequest *cmd)
693 switch (cmd->requesttype & ~USB_DIR_IN) {
695 *(uint8_t *)buffer = 0x01;
699 puts("unsupported root hub command\n");
700 stat = USB_ST_STALLED;
703 dev->act_len = min(len, txlen);
710 static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
711 struct usb_device *dev, void *buffer,
712 int txlen, struct devrequest *cmd)
714 switch (cmd->request) {
715 case USB_REQ_GET_STATUS:
716 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
718 case USB_REQ_GET_DESCRIPTOR:
719 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
721 case USB_REQ_GET_CONFIGURATION:
722 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
725 puts("unsupported root hub command\n");
726 return USB_ST_STALLED;
731 static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
732 struct usb_device *dev,
733 void *buffer, int txlen,
734 struct devrequest *cmd)
736 struct dwc2_core_regs *regs = priv->regs;
739 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
740 uint16_t wValue = cpu_to_le16(cmd->value);
742 switch (bmrtype_breq & ~USB_DIR_IN) {
743 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
744 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
747 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
749 case USB_PORT_FEAT_C_CONNECTION:
750 setbits_le32(®s->hprt0, DWC2_HPRT0_PRTCONNDET);
755 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
757 case USB_PORT_FEAT_SUSPEND:
760 case USB_PORT_FEAT_RESET:
761 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
762 DWC2_HPRT0_PRTCONNDET |
763 DWC2_HPRT0_PRTENCHNG |
764 DWC2_HPRT0_PRTOVRCURRCHNG,
767 clrbits_le32(®s->hprt0, DWC2_HPRT0_PRTRST);
770 case USB_PORT_FEAT_POWER:
771 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
772 DWC2_HPRT0_PRTCONNDET |
773 DWC2_HPRT0_PRTENCHNG |
774 DWC2_HPRT0_PRTOVRCURRCHNG,
778 case USB_PORT_FEAT_ENABLE:
782 case (USB_REQ_SET_ADDRESS << 8):
783 priv->root_hub_devnum = wValue;
785 case (USB_REQ_SET_CONFIGURATION << 8):
788 puts("unsupported root hub command\n");
789 stat = USB_ST_STALLED;
792 len = min(len, txlen);
800 static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
801 unsigned long pipe, void *buffer, int txlen,
802 struct devrequest *cmd)
806 if (usb_pipeint(pipe)) {
807 puts("Root-Hub submit IRQ: NOT implemented\n");
811 if (cmd->requesttype & USB_DIR_IN)
812 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
814 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
821 int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
824 uint32_t hcint, hctsiz;
826 ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
831 hcint = readl(&hc_regs->hcint);
832 hctsiz = readl(&hc_regs->hctsiz);
833 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
834 DWC2_HCTSIZ_XFERSIZE_OFFSET;
835 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
837 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
840 if (hcint & DWC2_HCINT_XFERCOMP)
843 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
846 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
850 static int dwc2_eptype[] = {
851 DWC2_HCCHAR_EPTYPE_ISOC,
852 DWC2_HCCHAR_EPTYPE_INTR,
853 DWC2_HCCHAR_EPTYPE_CONTROL,
854 DWC2_HCCHAR_EPTYPE_BULK,
857 static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
858 u8 *pid, int in, void *buffer, int num_packets,
859 int xfer_len, int *actual_len, int odd_frame)
864 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
865 *pid, xfer_len, num_packets);
867 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
868 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
869 (*pid << DWC2_HCTSIZ_PID_OFFSET),
874 invalidate_dcache_range(
875 (uintptr_t)aligned_buffer,
876 (uintptr_t)aligned_buffer +
877 roundup(xfer_len, ARCH_DMA_MINALIGN));
879 memcpy(aligned_buffer, buffer, xfer_len);
881 (uintptr_t)aligned_buffer,
882 (uintptr_t)aligned_buffer +
883 roundup(xfer_len, ARCH_DMA_MINALIGN));
887 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
889 /* Clear old interrupt conditions for this host channel. */
890 writel(0x3fff, &hc_regs->hcint);
892 /* Set host channel enable after all other setup is complete. */
893 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
894 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
896 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
897 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
900 ret = wait_for_chhltd(hc_regs, &sub, pid);
907 invalidate_dcache_range((unsigned long)aligned_buffer,
908 (unsigned long)aligned_buffer +
909 roundup(xfer_len, ARCH_DMA_MINALIGN));
911 memcpy(buffer, aligned_buffer, xfer_len);
913 *actual_len = xfer_len;
918 int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
919 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
921 struct dwc2_core_regs *regs = priv->regs;
922 struct dwc2_hc_regs *hc_regs = ®s->hc_regs[DWC2_HC_CHANNEL];
923 struct dwc2_host_regs *host_regs = ®s->host_regs;
924 int devnum = usb_pipedevice(pipe);
925 int ep = usb_pipeendpoint(pipe);
926 int max = usb_maxpacket(dev, pipe);
927 int eptype = dwc2_eptype[usb_pipetype(pipe)];
931 int complete_split = 0;
933 uint32_t num_packets;
934 int stop_transfer = 0;
935 uint32_t max_xfer_len;
936 int ssplit_frame_num = 0;
938 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
941 max_xfer_len = CONFIG_DWC2_MAX_PACKET_COUNT * max;
942 if (max_xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
943 max_xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE;
944 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
945 max_xfer_len = DWC2_DATA_BUF_SIZE;
947 /* Make sure that max_xfer_len is a multiple of max packet size. */
948 num_packets = max_xfer_len / max;
949 max_xfer_len = num_packets * max;
951 /* Initialize channel */
952 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
955 /* Check if the target is a FS/LS device behind a HS hub */
956 if (dev->speed != USB_SPEED_HIGH) {
959 uint32_t hprt0 = readl(®s->hprt0);
960 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
961 DWC2_HPRT0_PRTSPD_HIGH) {
962 usb_find_usb2_hub_address_port(dev, &hub_addr,
964 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
976 xfer_len = len - done;
978 if (xfer_len > max_xfer_len)
979 xfer_len = max_xfer_len;
980 else if (xfer_len > max)
981 num_packets = (xfer_len + max - 1) / max;
986 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
988 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
990 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
991 int uframe_num = readl(&host_regs->hfnum);
992 if (!(uframe_num & 0x1))
996 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
997 in, (char *)buffer + done, num_packets,
998 xfer_len, &actual_len, odd_frame);
1000 hcint = readl(&hc_regs->hcint);
1001 if (complete_split) {
1003 if (hcint & DWC2_HCINT_NYET) {
1005 int frame_num = DWC2_HFNUM_MAX_FRNUM &
1006 readl(&host_regs->hfnum);
1007 if (((frame_num - ssplit_frame_num) &
1008 DWC2_HFNUM_MAX_FRNUM) > 4)
1012 } else if (do_split) {
1013 if (hcint & DWC2_HCINT_ACK) {
1014 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
1015 readl(&host_regs->hfnum);
1024 if (actual_len < xfer_len)
1029 /* Transactions are done when when either all data is transferred or
1030 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1033 } while (((done < len) && !stop_transfer) || complete_split);
1035 writel(0, &hc_regs->hcintmsk);
1036 writel(0xFFFFFFFF, &hc_regs->hcint);
1039 dev->act_len = done;
1044 /* U-Boot USB transmission interface */
1045 int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
1046 unsigned long pipe, void *buffer, int len)
1048 int devnum = usb_pipedevice(pipe);
1049 int ep = usb_pipeendpoint(pipe);
1052 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
1057 if (usb_pipein(pipe))
1058 pid = &priv->in_data_toggle[devnum][ep];
1060 pid = &priv->out_data_toggle[devnum][ep];
1062 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
1065 static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
1066 unsigned long pipe, void *buffer, int len,
1067 struct devrequest *setup)
1069 int devnum = usb_pipedevice(pipe);
1072 /* For CONTROL endpoint pid should start with DATA1 */
1073 int status_direction;
1075 if (devnum == priv->root_hub_devnum) {
1077 dev->speed = USB_SPEED_HIGH;
1078 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
1083 pid = DWC2_HC_PID_SETUP;
1085 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
1086 } while (ret == -EAGAIN);
1093 pid = DWC2_HC_PID_DATA1;
1095 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
1097 act_len += dev->act_len;
1098 buffer += dev->act_len;
1099 len -= dev->act_len;
1100 } while (ret == -EAGAIN);
1103 status_direction = usb_pipeout(pipe);
1105 /* No-data CONTROL always ends with an IN transaction */
1106 status_direction = 1;
1110 pid = DWC2_HC_PID_DATA1;
1112 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1113 priv->status_buffer, 0);
1114 } while (ret == -EAGAIN);
1118 dev->act_len = act_len;
1123 int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
1124 unsigned long pipe, void *buffer, int len, int interval,
1127 unsigned long timeout;
1130 /* FIXME: what is interval? */
1132 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1134 if (get_timer(0) > timeout) {
1135 #if CONFIG_IS_ENABLED(DM_USB)
1137 "Timeout poll on interrupt endpoint\n");
1139 log_err("Timeout poll on interrupt endpoint\n");
1143 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
1144 if ((ret != -EAGAIN) || nonblock)
1149 static int dwc2_reset(struct udevice *dev)
1152 struct dwc2_priv *priv = dev_get_priv(dev);
1154 ret = reset_get_bulk(dev, &priv->resets);
1156 dev_warn(dev, "Can't get reset: %d\n", ret);
1157 /* Return 0 if error due to !CONFIG_DM_RESET and reset
1158 * DT property is not present.
1160 if (ret == -ENOENT || ret == -ENOTSUPP)
1166 /* force reset to clear all IP register */
1167 reset_assert_bulk(&priv->resets);
1168 ret = reset_deassert_bulk(&priv->resets);
1170 reset_release_bulk(&priv->resets);
1171 dev_err(dev, "Failed to reset: %d\n", ret);
1178 static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
1180 struct dwc2_core_regs *regs = priv->regs;
1185 ret = dwc2_reset(dev);
1189 snpsid = readl(®s->gsnpsid);
1190 dev_info(dev, "Core Release: %x.%03x\n",
1191 snpsid >> 12 & 0xf, snpsid & 0xfff);
1193 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1194 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
1195 dev_info(dev, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1200 #ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
1206 dwc_otg_core_init(dev);
1207 dwc_otg_core_host_init(dev, regs);
1209 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
1210 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1211 DWC2_HPRT0_PRTOVRCURRCHNG,
1214 clrbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
1215 DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
1218 for (i = 0; i < MAX_DEVICE; i++) {
1219 for (j = 0; j < MAX_ENDPOINT; j++) {
1220 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1221 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1226 * Add a 1 second delay here. This gives the host controller
1227 * a bit time before the comminucation with the USB devices
1228 * is started (the bus is scanned) and fixes the USB detection
1229 * problems with some problematic USB keys.
1231 if (readl(®s->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
1234 printf("USB DWC2\n");
1239 static void dwc2_uninit_common(struct dwc2_core_regs *regs)
1241 /* Put everything in reset. */
1242 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
1243 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1244 DWC2_HPRT0_PRTOVRCURRCHNG,
1248 #if !CONFIG_IS_ENABLED(DM_USB)
1249 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1250 int len, struct devrequest *setup)
1252 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1255 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1258 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1261 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1262 int len, int interval, bool nonblock)
1264 return _submit_int_msg(&local, dev, pipe, buffer, len, interval,
1268 /* U-Boot USB control interface */
1269 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1271 struct dwc2_priv *priv = &local;
1273 memset(priv, '\0', sizeof(*priv));
1274 priv->root_hub_devnum = 0;
1275 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1276 priv->aligned_buffer = aligned_buffer_addr;
1277 priv->status_buffer = status_buffer_addr;
1279 /* board-dependant init */
1280 if (board_usb_init(index, USB_INIT_HOST))
1283 return dwc2_init_common(NULL, priv);
1286 int usb_lowlevel_stop(int index)
1288 dwc2_uninit_common(local.regs);
1294 #if CONFIG_IS_ENABLED(DM_USB)
1295 static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1296 unsigned long pipe, void *buffer, int length,
1297 struct devrequest *setup)
1299 struct dwc2_priv *priv = dev_get_priv(dev);
1301 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1302 dev->name, udev, udev->dev->name, udev->portnr);
1304 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1307 static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1308 unsigned long pipe, void *buffer, int length)
1310 struct dwc2_priv *priv = dev_get_priv(dev);
1312 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1314 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1317 static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1318 unsigned long pipe, void *buffer, int length,
1319 int interval, bool nonblock)
1321 struct dwc2_priv *priv = dev_get_priv(dev);
1323 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1325 return _submit_int_msg(priv, udev, pipe, buffer, length, interval,
1329 static int dwc2_usb_ofdata_to_platdata(struct udevice *dev)
1331 struct dwc2_priv *priv = dev_get_priv(dev);
1333 priv->regs = dev_read_addr_ptr(dev);
1337 priv->oc_disable = dev_read_bool(dev, "disable-over-current");
1338 priv->hnp_srp_disable = dev_read_bool(dev, "hnp-srp-disable");
1343 static int dwc2_setup_phy(struct udevice *dev)
1345 struct dwc2_priv *priv = dev_get_priv(dev);
1348 ret = generic_phy_get_by_index(dev, 0, &priv->phy);
1351 return 0; /* no PHY, nothing to do */
1352 dev_err(dev, "Failed to get USB PHY: %d.\n", ret);
1356 ret = generic_phy_init(&priv->phy);
1358 dev_dbg(dev, "Failed to init USB PHY: %d.\n", ret);
1362 ret = generic_phy_power_on(&priv->phy);
1364 dev_dbg(dev, "Failed to power on USB PHY: %d.\n", ret);
1365 generic_phy_exit(&priv->phy);
1372 static int dwc2_shutdown_phy(struct udevice *dev)
1374 struct dwc2_priv *priv = dev_get_priv(dev);
1377 /* PHY is not valid when generic_phy_get_by_index() = -ENOENT */
1378 if (!generic_phy_valid(&priv->phy))
1379 return 0; /* no PHY, nothing to do */
1381 ret = generic_phy_power_off(&priv->phy);
1383 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1387 ret = generic_phy_exit(&priv->phy);
1389 dev_dbg(dev, "Failed to power off USB PHY: %d.\n", ret);
1396 static int dwc2_clk_init(struct udevice *dev)
1398 struct dwc2_priv *priv = dev_get_priv(dev);
1401 ret = clk_get_bulk(dev, &priv->clks);
1402 if (ret == -ENOSYS || ret == -ENOENT)
1407 ret = clk_enable_bulk(&priv->clks);
1409 clk_release_bulk(&priv->clks);
1416 static int dwc2_usb_probe(struct udevice *dev)
1418 struct dwc2_priv *priv = dev_get_priv(dev);
1419 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
1422 bus_priv->desc_before_addr = true;
1424 ret = dwc2_clk_init(dev);
1428 ret = dwc2_setup_phy(dev);
1432 return dwc2_init_common(dev, priv);
1435 static int dwc2_usb_remove(struct udevice *dev)
1437 struct dwc2_priv *priv = dev_get_priv(dev);
1440 ret = dwc_vbus_supply_exit(dev);
1444 ret = dwc2_shutdown_phy(dev);
1446 dev_dbg(dev, "Failed to shutdown USB PHY: %d.\n", ret);
1450 dwc2_uninit_common(priv->regs);
1452 reset_release_bulk(&priv->resets);
1453 clk_disable_bulk(&priv->clks);
1454 clk_release_bulk(&priv->clks);
1459 struct dm_usb_ops dwc2_usb_ops = {
1460 .control = dwc2_submit_control_msg,
1461 .bulk = dwc2_submit_bulk_msg,
1462 .interrupt = dwc2_submit_int_msg,
1465 static const struct udevice_id dwc2_usb_ids[] = {
1466 { .compatible = "brcm,bcm2835-usb" },
1467 { .compatible = "brcm,bcm2708-usb" },
1468 { .compatible = "snps,dwc2" },
1472 U_BOOT_DRIVER(usb_dwc2) = {
1475 .of_match = dwc2_usb_ids,
1476 .ofdata_to_platdata = dwc2_usb_ofdata_to_platdata,
1477 .probe = dwc2_usb_probe,
1478 .remove = dwc2_usb_remove,
1479 .ops = &dwc2_usb_ops,
1480 .priv_auto_alloc_size = sizeof(struct dwc2_priv),
1481 .flags = DM_FLAG_ALLOC_PRIV_DMA,