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>
14 #include <usbroothubdes.h>
17 #include <power/regulator.h>
21 /* Use only HC channel 0. */
22 #define DWC2_HC_CHANNEL 0
24 #define DWC2_STATUS_BUF_SIZE 64
25 #define DWC2_DATA_BUF_SIZE (CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
28 #define MAX_ENDPOINT 16
32 uint8_t aligned_buffer[DWC2_DATA_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
33 uint8_t status_buffer[DWC2_STATUS_BUF_SIZE] __aligned(ARCH_DMA_MINALIGN);
34 #ifdef CONFIG_DM_REGULATOR
35 struct udevice *vbus_supply;
38 uint8_t *aligned_buffer;
39 uint8_t *status_buffer;
41 u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
42 u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
43 struct dwc2_core_regs *regs;
47 * The hnp/srp capability must be disabled if the platform
48 * does't support hnp/srp. Otherwise the force mode can't work.
55 /* We need cacheline-aligned buffers for DMA transfers and dcache support */
56 DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
58 DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
61 static struct dwc2_priv local;
69 * Initializes the FSLSPClkSel field of the HCFG register
70 * depending on the PHY type.
72 static void init_fslspclksel(struct dwc2_core_regs *regs)
76 #if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
77 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
79 /* High speed PHY running at full speed or high speed */
80 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
83 #ifdef CONFIG_DWC2_ULPI_FS_LS
84 uint32_t hwcfg2 = readl(®s->ghwcfg2);
85 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
86 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
87 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
88 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
90 if (hval == 2 && fval == 1)
91 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
94 clrsetbits_le32(®s->host_regs.hcfg,
95 DWC2_HCFG_FSLSPCLKSEL_MASK,
96 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
102 * @param regs Programming view of DWC_otg controller.
103 * @param num Tx FIFO to flush.
105 static void dwc_otg_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
109 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
111 ret = wait_for_bit_le32(®s->grstctl, DWC2_GRSTCTL_TXFFLSH,
114 dev_info(dev, "%s: Timeout!\n", __func__);
116 /* Wait for 3 PHY Clocks */
123 * @param regs Programming view of DWC_otg controller.
125 static void dwc_otg_flush_rx_fifo(struct dwc2_core_regs *regs)
129 writel(DWC2_GRSTCTL_RXFFLSH, ®s->grstctl);
130 ret = wait_for_bit_le32(®s->grstctl, DWC2_GRSTCTL_RXFFLSH,
133 dev_info(dev, "%s: Timeout!\n", __func__);
135 /* Wait for 3 PHY Clocks */
140 * Do core a soft reset of the core. Be careful with this because it
141 * resets all the internal state machines of the core.
143 static void dwc_otg_core_reset(struct dwc2_core_regs *regs)
147 /* Wait for AHB master IDLE state. */
148 ret = wait_for_bit_le32(®s->grstctl, DWC2_GRSTCTL_AHBIDLE,
151 dev_info(dev, "%s: Timeout!\n", __func__);
153 /* Core Soft Reset */
154 writel(DWC2_GRSTCTL_CSFTRST, ®s->grstctl);
155 ret = wait_for_bit_le32(®s->grstctl, DWC2_GRSTCTL_CSFTRST,
158 dev_info(dev, "%s: Timeout!\n", __func__);
161 * Wait for core to come out of reset.
162 * NOTE: This long sleep is _very_ important, otherwise the core will
163 * not stay in host mode after a connector ID change!
168 #if defined(CONFIG_DM_USB) && defined(CONFIG_DM_REGULATOR)
169 static int dwc_vbus_supply_init(struct udevice *dev)
171 struct dwc2_priv *priv = dev_get_priv(dev);
174 ret = device_get_supply_regulator(dev, "vbus-supply",
177 debug("%s: No vbus supply\n", dev->name);
181 ret = regulator_set_enable(priv->vbus_supply, true);
183 dev_err(dev, "Error enabling vbus supply\n");
190 static int dwc_vbus_supply_exit(struct udevice *dev)
192 struct dwc2_priv *priv = dev_get_priv(dev);
195 if (priv->vbus_supply) {
196 ret = regulator_set_enable(priv->vbus_supply, false);
198 dev_err(dev, "Error disabling vbus supply\n");
206 static int dwc_vbus_supply_init(struct udevice *dev)
211 #if defined(CONFIG_DM_USB)
212 static int dwc_vbus_supply_exit(struct udevice *dev)
220 * This function initializes the DWC_otg controller registers for
223 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
224 * request queues. Host channels are reset to ensure that they are ready for
225 * performing transfers.
227 * @param dev USB Device (NULL if driver model is not being used)
228 * @param regs Programming view of DWC_otg controller
231 static void dwc_otg_core_host_init(struct udevice *dev,
232 struct dwc2_core_regs *regs)
234 uint32_t nptxfifosize = 0;
235 uint32_t ptxfifosize = 0;
237 int i, ret, num_channels;
239 /* Restart the Phy Clock */
240 writel(0, ®s->pcgcctl);
242 /* Initialize Host Configuration Register */
243 init_fslspclksel(regs);
244 #ifdef CONFIG_DWC2_DFLT_SPEED_FULL
245 setbits_le32(®s->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
248 /* Configure data FIFO sizes */
249 #ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
250 if (readl(®s->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
252 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, ®s->grxfsiz);
254 /* Non-periodic Tx FIFO */
255 nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
256 DWC2_FIFOSIZE_DEPTH_OFFSET;
257 nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
258 DWC2_FIFOSIZE_STARTADDR_OFFSET;
259 writel(nptxfifosize, ®s->gnptxfsiz);
261 /* Periodic Tx FIFO */
262 ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
263 DWC2_FIFOSIZE_DEPTH_OFFSET;
264 ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
265 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
266 DWC2_FIFOSIZE_STARTADDR_OFFSET;
267 writel(ptxfifosize, ®s->hptxfsiz);
271 /* Clear Host Set HNP Enable in the OTG Control Register */
272 clrbits_le32(®s->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
274 /* Make sure the FIFOs are flushed. */
275 dwc_otg_flush_tx_fifo(regs, 0x10); /* All Tx FIFOs */
276 dwc_otg_flush_rx_fifo(regs);
278 /* Flush out any leftover queued requests. */
279 num_channels = readl(®s->ghwcfg2);
280 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
281 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
284 for (i = 0; i < num_channels; i++)
285 clrsetbits_le32(®s->hc_regs[i].hcchar,
286 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
289 /* Halt all channels to put them into a known state. */
290 for (i = 0; i < num_channels; i++) {
291 clrsetbits_le32(®s->hc_regs[i].hcchar,
293 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
294 ret = wait_for_bit_le32(®s->hc_regs[i].hcchar,
295 DWC2_HCCHAR_CHEN, false, 1000, false);
297 dev_info("%s: Timeout!\n", __func__);
300 /* Turn on the vbus power. */
301 if (readl(®s->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
302 hprt0 = readl(®s->hprt0);
303 hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
304 hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
305 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
306 hprt0 |= DWC2_HPRT0_PRTPWR;
307 writel(hprt0, ®s->hprt0);
312 dwc_vbus_supply_init(dev);
316 * This function initializes the DWC_otg controller registers and
317 * prepares the core for device mode or host mode operation.
319 * @param regs Programming view of the DWC_otg controller
321 static void dwc_otg_core_init(struct dwc2_priv *priv)
323 struct dwc2_core_regs *regs = priv->regs;
326 uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
328 /* Common Initialization */
329 usbcfg = readl(®s->gusbcfg);
331 /* Program the ULPI External VBUS bit if needed */
332 if (priv->ext_vbus) {
333 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
334 if (!priv->oc_disable) {
335 usbcfg |= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR |
336 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH;
339 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
342 /* Set external TS Dline pulsing */
343 #ifdef CONFIG_DWC2_TS_DLINE
344 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
346 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
348 writel(usbcfg, ®s->gusbcfg);
350 /* Reset the Controller */
351 dwc_otg_core_reset(regs);
354 * This programming sequence needs to happen in FS mode before
355 * any other programming occurs
357 #if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
358 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
359 /* If FS mode with FS PHY */
360 setbits_le32(®s->gusbcfg, DWC2_GUSBCFG_PHYSEL);
362 /* Reset after a PHY select */
363 dwc_otg_core_reset(regs);
366 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
367 * Also do this on HNP Dev/Host mode switches (done in dev_init
370 if (readl(®s->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
371 init_fslspclksel(regs);
373 #ifdef CONFIG_DWC2_I2C_ENABLE
374 /* Program GUSBCFG.OtgUtmifsSel to I2C */
375 setbits_le32(®s->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
377 /* Program GI2CCTL.I2CEn */
378 clrsetbits_le32(®s->gi2cctl, DWC2_GI2CCTL_I2CEN |
379 DWC2_GI2CCTL_I2CDEVADDR_MASK,
380 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
381 setbits_le32(®s->gi2cctl, DWC2_GI2CCTL_I2CEN);
385 /* High speed PHY. */
388 * HS PHY parameters. These parameters are preserved during
389 * soft reset so only program the first time. Do a soft reset
390 * immediately after setting phyif.
392 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
393 usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
395 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
396 #ifdef CONFIG_DWC2_PHY_ULPI_DDR
397 usbcfg |= DWC2_GUSBCFG_DDRSEL;
399 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
401 } else { /* UTMI+ interface */
402 #if (CONFIG_DWC2_UTMI_WIDTH == 16)
403 usbcfg |= DWC2_GUSBCFG_PHYIF;
407 writel(usbcfg, ®s->gusbcfg);
409 /* Reset after setting the PHY parameters */
410 dwc_otg_core_reset(regs);
413 usbcfg = readl(®s->gusbcfg);
414 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
415 #ifdef CONFIG_DWC2_ULPI_FS_LS
416 uint32_t hwcfg2 = readl(®s->ghwcfg2);
417 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
418 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
419 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
420 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
421 if (hval == 2 && fval == 1) {
422 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
423 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
426 if (priv->hnp_srp_disable)
427 usbcfg |= DWC2_GUSBCFG_FORCEHOSTMODE;
429 writel(usbcfg, ®s->gusbcfg);
431 /* Program the GAHBCFG Register. */
432 switch (readl(®s->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
433 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
435 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
436 while (brst_sz > 1) {
437 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
438 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
442 #ifdef CONFIG_DWC2_DMA_ENABLE
443 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
447 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
448 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
449 #ifdef CONFIG_DWC2_DMA_ENABLE
450 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
455 writel(ahbcfg, ®s->gahbcfg);
457 /* Program the capabilities in GUSBCFG Register */
460 if (!priv->hnp_srp_disable)
461 usbcfg |= DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP;
462 #ifdef CONFIG_DWC2_IC_USB_CAP
463 usbcfg |= DWC2_GUSBCFG_IC_USB_CAP;
466 setbits_le32(®s->gusbcfg, usbcfg);
470 * Prepares a host channel for transferring packets to/from a specific
471 * endpoint. The HCCHARn register is set up with the characteristics specified
472 * in _hc. Host channel interrupts that may need to be serviced while this
473 * transfer is in progress are enabled.
475 * @param regs Programming view of DWC_otg controller
476 * @param hc Information needed to initialize the host channel
478 static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
479 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
480 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
482 struct dwc2_hc_regs *hc_regs = ®s->hc_regs[hc_num];
483 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
484 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
485 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
486 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
487 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
489 if (dev->speed == USB_SPEED_LOW)
490 hcchar |= DWC2_HCCHAR_LSPDDEV;
493 * Program the HCCHARn register with the endpoint characteristics
494 * for the current transfer.
496 writel(hcchar, &hc_regs->hcchar);
498 /* Program the HCSPLIT register, default to no SPLIT */
499 writel(0, &hc_regs->hcsplt);
502 static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
503 uint8_t hub_devnum, uint8_t hub_port)
507 hcsplt = DWC2_HCSPLT_SPLTENA;
508 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
509 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
511 /* Program the HCSPLIT register for SPLITs */
512 writel(hcsplt, &hc_regs->hcsplt);
516 * DWC2 to USB API interface
518 /* Direction: In ; Request: Status */
519 static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
520 struct usb_device *dev, void *buffer,
521 int txlen, struct devrequest *cmd)
524 uint32_t port_status = 0;
525 uint32_t port_change = 0;
529 switch (cmd->requesttype & ~USB_DIR_IN) {
531 *(uint16_t *)buffer = cpu_to_le16(1);
534 case USB_RECIP_INTERFACE:
535 case USB_RECIP_ENDPOINT:
536 *(uint16_t *)buffer = cpu_to_le16(0);
540 *(uint32_t *)buffer = cpu_to_le32(0);
543 case USB_RECIP_OTHER | USB_TYPE_CLASS:
544 hprt0 = readl(®s->hprt0);
545 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
546 port_status |= USB_PORT_STAT_CONNECTION;
547 if (hprt0 & DWC2_HPRT0_PRTENA)
548 port_status |= USB_PORT_STAT_ENABLE;
549 if (hprt0 & DWC2_HPRT0_PRTSUSP)
550 port_status |= USB_PORT_STAT_SUSPEND;
551 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
552 port_status |= USB_PORT_STAT_OVERCURRENT;
553 if (hprt0 & DWC2_HPRT0_PRTRST)
554 port_status |= USB_PORT_STAT_RESET;
555 if (hprt0 & DWC2_HPRT0_PRTPWR)
556 port_status |= USB_PORT_STAT_POWER;
558 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
559 port_status |= USB_PORT_STAT_LOW_SPEED;
560 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
561 DWC2_HPRT0_PRTSPD_HIGH)
562 port_status |= USB_PORT_STAT_HIGH_SPEED;
564 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
565 port_change |= USB_PORT_STAT_C_ENABLE;
566 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
567 port_change |= USB_PORT_STAT_C_CONNECTION;
568 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
569 port_change |= USB_PORT_STAT_C_OVERCURRENT;
571 *(uint32_t *)buffer = cpu_to_le32(port_status |
572 (port_change << 16));
576 puts("unsupported root hub command\n");
577 stat = USB_ST_STALLED;
580 dev->act_len = min(len, txlen);
586 /* Direction: In ; Request: Descriptor */
587 static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
588 void *buffer, int txlen,
589 struct devrequest *cmd)
591 unsigned char data[32];
595 uint16_t wValue = cpu_to_le16(cmd->value);
596 uint16_t wLength = cpu_to_le16(cmd->length);
598 switch (cmd->requesttype & ~USB_DIR_IN) {
600 switch (wValue & 0xff00) {
601 case 0x0100: /* device descriptor */
602 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
603 memcpy(buffer, root_hub_dev_des, len);
605 case 0x0200: /* configuration descriptor */
606 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
607 memcpy(buffer, root_hub_config_des, len);
609 case 0x0300: /* string descriptors */
610 switch (wValue & 0xff) {
612 len = min3(txlen, (int)sizeof(root_hub_str_index0),
614 memcpy(buffer, root_hub_str_index0, len);
617 len = min3(txlen, (int)sizeof(root_hub_str_index1),
619 memcpy(buffer, root_hub_str_index1, len);
624 stat = USB_ST_STALLED;
629 /* Root port config, set 1 port and nothing else. */
632 data[0] = 9; /* min length; */
634 data[2] = dsc & RH_A_NDP;
640 else if (dsc & RH_A_OCPM)
643 /* corresponds to data[4-7] */
644 data[5] = (dsc & RH_A_POTPGT) >> 24;
645 data[7] = dsc & RH_B_DR;
650 data[8] = (dsc & RH_B_DR) >> 8;
655 len = min3(txlen, (int)data[0], (int)wLength);
656 memcpy(buffer, data, len);
659 puts("unsupported root hub command\n");
660 stat = USB_ST_STALLED;
663 dev->act_len = min(len, txlen);
669 /* Direction: In ; Request: Configuration */
670 static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
671 void *buffer, int txlen,
672 struct devrequest *cmd)
677 switch (cmd->requesttype & ~USB_DIR_IN) {
679 *(uint8_t *)buffer = 0x01;
683 puts("unsupported root hub command\n");
684 stat = USB_ST_STALLED;
687 dev->act_len = min(len, txlen);
694 static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
695 struct usb_device *dev, void *buffer,
696 int txlen, struct devrequest *cmd)
698 switch (cmd->request) {
699 case USB_REQ_GET_STATUS:
700 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
702 case USB_REQ_GET_DESCRIPTOR:
703 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
705 case USB_REQ_GET_CONFIGURATION:
706 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
709 puts("unsupported root hub command\n");
710 return USB_ST_STALLED;
715 static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
716 struct usb_device *dev,
717 void *buffer, int txlen,
718 struct devrequest *cmd)
720 struct dwc2_core_regs *regs = priv->regs;
723 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
724 uint16_t wValue = cpu_to_le16(cmd->value);
726 switch (bmrtype_breq & ~USB_DIR_IN) {
727 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
728 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
731 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
733 case USB_PORT_FEAT_C_CONNECTION:
734 setbits_le32(®s->hprt0, DWC2_HPRT0_PRTCONNDET);
739 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
741 case USB_PORT_FEAT_SUSPEND:
744 case USB_PORT_FEAT_RESET:
745 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
746 DWC2_HPRT0_PRTCONNDET |
747 DWC2_HPRT0_PRTENCHNG |
748 DWC2_HPRT0_PRTOVRCURRCHNG,
751 clrbits_le32(®s->hprt0, DWC2_HPRT0_PRTRST);
754 case USB_PORT_FEAT_POWER:
755 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
756 DWC2_HPRT0_PRTCONNDET |
757 DWC2_HPRT0_PRTENCHNG |
758 DWC2_HPRT0_PRTOVRCURRCHNG,
762 case USB_PORT_FEAT_ENABLE:
766 case (USB_REQ_SET_ADDRESS << 8):
767 priv->root_hub_devnum = wValue;
769 case (USB_REQ_SET_CONFIGURATION << 8):
772 puts("unsupported root hub command\n");
773 stat = USB_ST_STALLED;
776 len = min(len, txlen);
784 static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
785 unsigned long pipe, void *buffer, int txlen,
786 struct devrequest *cmd)
790 if (usb_pipeint(pipe)) {
791 puts("Root-Hub submit IRQ: NOT implemented\n");
795 if (cmd->requesttype & USB_DIR_IN)
796 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
798 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
805 int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
808 uint32_t hcint, hctsiz;
810 ret = wait_for_bit_le32(&hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
815 hcint = readl(&hc_regs->hcint);
816 hctsiz = readl(&hc_regs->hctsiz);
817 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
818 DWC2_HCTSIZ_XFERSIZE_OFFSET;
819 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
821 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
824 if (hcint & DWC2_HCINT_XFERCOMP)
827 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
830 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
834 static int dwc2_eptype[] = {
835 DWC2_HCCHAR_EPTYPE_ISOC,
836 DWC2_HCCHAR_EPTYPE_INTR,
837 DWC2_HCCHAR_EPTYPE_CONTROL,
838 DWC2_HCCHAR_EPTYPE_BULK,
841 static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
842 u8 *pid, int in, void *buffer, int num_packets,
843 int xfer_len, int *actual_len, int odd_frame)
848 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
849 *pid, xfer_len, num_packets);
851 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
852 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
853 (*pid << DWC2_HCTSIZ_PID_OFFSET),
858 invalidate_dcache_range(
859 (uintptr_t)aligned_buffer,
860 (uintptr_t)aligned_buffer +
861 roundup(xfer_len, ARCH_DMA_MINALIGN));
863 memcpy(aligned_buffer, buffer, xfer_len);
865 (uintptr_t)aligned_buffer,
866 (uintptr_t)aligned_buffer +
867 roundup(xfer_len, ARCH_DMA_MINALIGN));
871 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
873 /* Clear old interrupt conditions for this host channel. */
874 writel(0x3fff, &hc_regs->hcint);
876 /* Set host channel enable after all other setup is complete. */
877 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
878 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
880 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
881 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
884 ret = wait_for_chhltd(hc_regs, &sub, pid);
891 invalidate_dcache_range((unsigned long)aligned_buffer,
892 (unsigned long)aligned_buffer +
893 roundup(xfer_len, ARCH_DMA_MINALIGN));
895 memcpy(buffer, aligned_buffer, xfer_len);
897 *actual_len = xfer_len;
902 int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
903 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
905 struct dwc2_core_regs *regs = priv->regs;
906 struct dwc2_hc_regs *hc_regs = ®s->hc_regs[DWC2_HC_CHANNEL];
907 struct dwc2_host_regs *host_regs = ®s->host_regs;
908 int devnum = usb_pipedevice(pipe);
909 int ep = usb_pipeendpoint(pipe);
910 int max = usb_maxpacket(dev, pipe);
911 int eptype = dwc2_eptype[usb_pipetype(pipe)];
915 int complete_split = 0;
917 uint32_t num_packets;
918 int stop_transfer = 0;
919 uint32_t max_xfer_len;
920 int ssplit_frame_num = 0;
922 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
925 max_xfer_len = CONFIG_DWC2_MAX_PACKET_COUNT * max;
926 if (max_xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
927 max_xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE;
928 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
929 max_xfer_len = DWC2_DATA_BUF_SIZE;
931 /* Make sure that max_xfer_len is a multiple of max packet size. */
932 num_packets = max_xfer_len / max;
933 max_xfer_len = num_packets * max;
935 /* Initialize channel */
936 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
939 /* Check if the target is a FS/LS device behind a HS hub */
940 if (dev->speed != USB_SPEED_HIGH) {
943 uint32_t hprt0 = readl(®s->hprt0);
944 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
945 DWC2_HPRT0_PRTSPD_HIGH) {
946 usb_find_usb2_hub_address_port(dev, &hub_addr,
948 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
960 xfer_len = len - done;
962 if (xfer_len > max_xfer_len)
963 xfer_len = max_xfer_len;
964 else if (xfer_len > max)
965 num_packets = (xfer_len + max - 1) / max;
970 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
972 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
974 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
975 int uframe_num = readl(&host_regs->hfnum);
976 if (!(uframe_num & 0x1))
980 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
981 in, (char *)buffer + done, num_packets,
982 xfer_len, &actual_len, odd_frame);
984 hcint = readl(&hc_regs->hcint);
985 if (complete_split) {
987 if (hcint & DWC2_HCINT_NYET) {
989 int frame_num = DWC2_HFNUM_MAX_FRNUM &
990 readl(&host_regs->hfnum);
991 if (((frame_num - ssplit_frame_num) &
992 DWC2_HFNUM_MAX_FRNUM) > 4)
996 } else if (do_split) {
997 if (hcint & DWC2_HCINT_ACK) {
998 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
999 readl(&host_regs->hfnum);
1008 if (actual_len < xfer_len)
1013 /* Transactions are done when when either all data is transferred or
1014 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1017 } while (((done < len) && !stop_transfer) || complete_split);
1019 writel(0, &hc_regs->hcintmsk);
1020 writel(0xFFFFFFFF, &hc_regs->hcint);
1023 dev->act_len = done;
1028 /* U-Boot USB transmission interface */
1029 int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
1030 unsigned long pipe, void *buffer, int len)
1032 int devnum = usb_pipedevice(pipe);
1033 int ep = usb_pipeendpoint(pipe);
1036 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
1041 if (usb_pipein(pipe))
1042 pid = &priv->in_data_toggle[devnum][ep];
1044 pid = &priv->out_data_toggle[devnum][ep];
1046 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
1049 static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
1050 unsigned long pipe, void *buffer, int len,
1051 struct devrequest *setup)
1053 int devnum = usb_pipedevice(pipe);
1056 /* For CONTROL endpoint pid should start with DATA1 */
1057 int status_direction;
1059 if (devnum == priv->root_hub_devnum) {
1061 dev->speed = USB_SPEED_HIGH;
1062 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
1067 pid = DWC2_HC_PID_SETUP;
1069 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
1070 } while (ret == -EAGAIN);
1077 pid = DWC2_HC_PID_DATA1;
1079 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
1081 act_len += dev->act_len;
1082 buffer += dev->act_len;
1083 len -= dev->act_len;
1084 } while (ret == -EAGAIN);
1087 status_direction = usb_pipeout(pipe);
1089 /* No-data CONTROL always ends with an IN transaction */
1090 status_direction = 1;
1094 pid = DWC2_HC_PID_DATA1;
1096 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1097 priv->status_buffer, 0);
1098 } while (ret == -EAGAIN);
1102 dev->act_len = act_len;
1107 int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
1108 unsigned long pipe, void *buffer, int len, int interval)
1110 unsigned long timeout;
1113 /* FIXME: what is interval? */
1115 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1117 if (get_timer(0) > timeout) {
1118 dev_err(dev, "Timeout poll on interrupt endpoint\n");
1121 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
1127 static int dwc2_init_common(struct udevice *dev, struct dwc2_priv *priv)
1129 struct dwc2_core_regs *regs = priv->regs;
1133 snpsid = readl(®s->gsnpsid);
1134 dev_info(dev, "Core Release: %x.%03x\n",
1135 snpsid >> 12 & 0xf, snpsid & 0xfff);
1137 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1138 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
1139 dev_info(dev, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1144 #ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
1150 dwc_otg_core_init(priv);
1151 dwc_otg_core_host_init(dev, regs);
1153 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
1154 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1155 DWC2_HPRT0_PRTOVRCURRCHNG,
1158 clrbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
1159 DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
1162 for (i = 0; i < MAX_DEVICE; i++) {
1163 for (j = 0; j < MAX_ENDPOINT; j++) {
1164 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1165 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1170 * Add a 1 second delay here. This gives the host controller
1171 * a bit time before the comminucation with the USB devices
1172 * is started (the bus is scanned) and fixes the USB detection
1173 * problems with some problematic USB keys.
1175 if (readl(®s->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
1181 static void dwc2_uninit_common(struct dwc2_core_regs *regs)
1183 /* Put everything in reset. */
1184 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
1185 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1186 DWC2_HPRT0_PRTOVRCURRCHNG,
1190 #ifndef CONFIG_DM_USB
1191 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1192 int len, struct devrequest *setup)
1194 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1197 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1200 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1203 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1204 int len, int interval)
1206 return _submit_int_msg(&local, dev, pipe, buffer, len, interval);
1209 /* U-Boot USB control interface */
1210 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1212 struct dwc2_priv *priv = &local;
1214 memset(priv, '\0', sizeof(*priv));
1215 priv->root_hub_devnum = 0;
1216 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1217 priv->aligned_buffer = aligned_buffer_addr;
1218 priv->status_buffer = status_buffer_addr;
1220 /* board-dependant init */
1221 if (board_usb_init(index, USB_INIT_HOST))
1224 return dwc2_init_common(NULL, priv);
1227 int usb_lowlevel_stop(int index)
1229 dwc2_uninit_common(local.regs);
1235 #ifdef CONFIG_DM_USB
1236 static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1237 unsigned long pipe, void *buffer, int length,
1238 struct devrequest *setup)
1240 struct dwc2_priv *priv = dev_get_priv(dev);
1242 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1243 dev->name, udev, udev->dev->name, udev->portnr);
1245 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1248 static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1249 unsigned long pipe, void *buffer, int length)
1251 struct dwc2_priv *priv = dev_get_priv(dev);
1253 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1255 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1258 static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1259 unsigned long pipe, void *buffer, int length,
1262 struct dwc2_priv *priv = dev_get_priv(dev);
1264 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1266 return _submit_int_msg(priv, udev, pipe, buffer, length, interval);
1269 static int dwc2_usb_ofdata_to_platdata(struct udevice *dev)
1271 struct dwc2_priv *priv = dev_get_priv(dev);
1274 addr = dev_read_addr(dev);
1275 if (addr == FDT_ADDR_T_NONE)
1277 priv->regs = (struct dwc2_core_regs *)addr;
1279 priv->oc_disable = dev_read_bool(dev, "disable-over-current");
1280 priv->hnp_srp_disable = dev_read_bool(dev, "hnp-srp-disable");
1285 static int dwc2_usb_probe(struct udevice *dev)
1287 struct dwc2_priv *priv = dev_get_priv(dev);
1288 struct usb_bus_priv *bus_priv = dev_get_uclass_priv(dev);
1290 bus_priv->desc_before_addr = true;
1292 return dwc2_init_common(dev, priv);
1295 static int dwc2_usb_remove(struct udevice *dev)
1297 struct dwc2_priv *priv = dev_get_priv(dev);
1300 ret = dwc_vbus_supply_exit(dev);
1304 dwc2_uninit_common(priv->regs);
1309 struct dm_usb_ops dwc2_usb_ops = {
1310 .control = dwc2_submit_control_msg,
1311 .bulk = dwc2_submit_bulk_msg,
1312 .interrupt = dwc2_submit_int_msg,
1315 static const struct udevice_id dwc2_usb_ids[] = {
1316 { .compatible = "brcm,bcm2835-usb" },
1317 { .compatible = "snps,dwc2" },
1321 U_BOOT_DRIVER(usb_dwc2) = {
1324 .of_match = dwc2_usb_ids,
1325 .ofdata_to_platdata = dwc2_usb_ofdata_to_platdata,
1326 .probe = dwc2_usb_probe,
1327 .remove = dwc2_usb_remove,
1328 .ops = &dwc2_usb_ops,
1329 .priv_auto_alloc_size = sizeof(struct dwc2_priv),
1330 .flags = DM_FLAG_ALLOC_PRIV_DMA,