2 * Copyright (C) 2012 Oleksandr Tymoshenko <gonzo@freebsd.org>
3 * Copyright (C) 2014 Marek Vasut <marex@denx.de>
5 * SPDX-License-Identifier: GPL-2.0+
15 #include <usbroothubdes.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 (64 * 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);
35 uint8_t *aligned_buffer;
36 uint8_t *status_buffer;
38 u8 in_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
39 u8 out_data_toggle[MAX_DEVICE][MAX_ENDPOINT];
40 struct dwc2_core_regs *regs;
45 /* We need cacheline-aligned buffers for DMA transfers and dcache support */
46 DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr, DWC2_DATA_BUF_SIZE,
48 DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr, DWC2_STATUS_BUF_SIZE,
51 static struct dwc2_priv local;
59 * Initializes the FSLSPClkSel field of the HCFG register
60 * depending on the PHY type.
62 static void init_fslspclksel(struct dwc2_core_regs *regs)
66 #if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
67 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
69 /* High speed PHY running at full speed or high speed */
70 phyclk = DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ;
73 #ifdef CONFIG_DWC2_ULPI_FS_LS
74 uint32_t hwcfg2 = readl(®s->ghwcfg2);
75 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
76 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
77 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
78 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
80 if (hval == 2 && fval == 1)
81 phyclk = DWC2_HCFG_FSLSPCLKSEL_48_MHZ; /* Full speed PHY */
84 clrsetbits_le32(®s->host_regs.hcfg,
85 DWC2_HCFG_FSLSPCLKSEL_MASK,
86 phyclk << DWC2_HCFG_FSLSPCLKSEL_OFFSET);
92 * @param regs Programming view of DWC_otg controller.
93 * @param num Tx FIFO to flush.
95 static void dwc_otg_flush_tx_fifo(struct dwc2_core_regs *regs, const int num)
99 writel(DWC2_GRSTCTL_TXFFLSH | (num << DWC2_GRSTCTL_TXFNUM_OFFSET),
101 ret = wait_for_bit(__func__, ®s->grstctl, DWC2_GRSTCTL_TXFFLSH,
104 printf("%s: Timeout!\n", __func__);
106 /* Wait for 3 PHY Clocks */
113 * @param regs Programming view of DWC_otg controller.
115 static void dwc_otg_flush_rx_fifo(struct dwc2_core_regs *regs)
119 writel(DWC2_GRSTCTL_RXFFLSH, ®s->grstctl);
120 ret = wait_for_bit(__func__, ®s->grstctl, DWC2_GRSTCTL_RXFFLSH,
123 printf("%s: Timeout!\n", __func__);
125 /* Wait for 3 PHY Clocks */
130 * Do core a soft reset of the core. Be careful with this because it
131 * resets all the internal state machines of the core.
133 static void dwc_otg_core_reset(struct dwc2_core_regs *regs)
137 /* Wait for AHB master IDLE state. */
138 ret = wait_for_bit(__func__, ®s->grstctl, DWC2_GRSTCTL_AHBIDLE,
141 printf("%s: Timeout!\n", __func__);
143 /* Core Soft Reset */
144 writel(DWC2_GRSTCTL_CSFTRST, ®s->grstctl);
145 ret = wait_for_bit(__func__, ®s->grstctl, DWC2_GRSTCTL_CSFTRST,
148 printf("%s: Timeout!\n", __func__);
151 * Wait for core to come out of reset.
152 * NOTE: This long sleep is _very_ important, otherwise the core will
153 * not stay in host mode after a connector ID change!
159 * This function initializes the DWC_otg controller registers for
162 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
163 * request queues. Host channels are reset to ensure that they are ready for
164 * performing transfers.
166 * @param regs Programming view of DWC_otg controller
169 static void dwc_otg_core_host_init(struct dwc2_core_regs *regs)
171 uint32_t nptxfifosize = 0;
172 uint32_t ptxfifosize = 0;
174 int i, ret, num_channels;
176 /* Restart the Phy Clock */
177 writel(0, ®s->pcgcctl);
179 /* Initialize Host Configuration Register */
180 init_fslspclksel(regs);
181 #ifdef CONFIG_DWC2_DFLT_SPEED_FULL
182 setbits_le32(®s->host_regs.hcfg, DWC2_HCFG_FSLSSUPP);
185 /* Configure data FIFO sizes */
186 #ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
187 if (readl(®s->ghwcfg2) & DWC2_HWCFG2_DYNAMIC_FIFO) {
189 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE, ®s->grxfsiz);
191 /* Non-periodic Tx FIFO */
192 nptxfifosize |= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE <<
193 DWC2_FIFOSIZE_DEPTH_OFFSET;
194 nptxfifosize |= CONFIG_DWC2_HOST_RX_FIFO_SIZE <<
195 DWC2_FIFOSIZE_STARTADDR_OFFSET;
196 writel(nptxfifosize, ®s->gnptxfsiz);
198 /* Periodic Tx FIFO */
199 ptxfifosize |= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE <<
200 DWC2_FIFOSIZE_DEPTH_OFFSET;
201 ptxfifosize |= (CONFIG_DWC2_HOST_RX_FIFO_SIZE +
202 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE) <<
203 DWC2_FIFOSIZE_STARTADDR_OFFSET;
204 writel(ptxfifosize, ®s->hptxfsiz);
208 /* Clear Host Set HNP Enable in the OTG Control Register */
209 clrbits_le32(®s->gotgctl, DWC2_GOTGCTL_HSTSETHNPEN);
211 /* Make sure the FIFOs are flushed. */
212 dwc_otg_flush_tx_fifo(regs, 0x10); /* All Tx FIFOs */
213 dwc_otg_flush_rx_fifo(regs);
215 /* Flush out any leftover queued requests. */
216 num_channels = readl(®s->ghwcfg2);
217 num_channels &= DWC2_HWCFG2_NUM_HOST_CHAN_MASK;
218 num_channels >>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET;
221 for (i = 0; i < num_channels; i++)
222 clrsetbits_le32(®s->hc_regs[i].hcchar,
223 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_EPDIR,
226 /* Halt all channels to put them into a known state. */
227 for (i = 0; i < num_channels; i++) {
228 clrsetbits_le32(®s->hc_regs[i].hcchar,
230 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS);
231 ret = wait_for_bit(__func__, ®s->hc_regs[i].hcchar,
232 DWC2_HCCHAR_CHEN, false, 1000, false);
234 printf("%s: Timeout!\n", __func__);
237 /* Turn on the vbus power. */
238 if (readl(®s->gintsts) & DWC2_GINTSTS_CURMODE_HOST) {
239 hprt0 = readl(®s->hprt0);
240 hprt0 &= ~(DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET);
241 hprt0 &= ~(DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG);
242 if (!(hprt0 & DWC2_HPRT0_PRTPWR)) {
243 hprt0 |= DWC2_HPRT0_PRTPWR;
244 writel(hprt0, ®s->hprt0);
250 * This function initializes the DWC_otg controller registers and
251 * prepares the core for device mode or host mode operation.
253 * @param regs Programming view of the DWC_otg controller
255 static void dwc_otg_core_init(struct dwc2_core_regs *regs)
259 uint8_t brst_sz = CONFIG_DWC2_DMA_BURST_SIZE;
261 /* Common Initialization */
262 usbcfg = readl(®s->gusbcfg);
264 /* Program the ULPI External VBUS bit if needed */
265 #ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
266 usbcfg |= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
268 usbcfg &= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV;
271 /* Set external TS Dline pulsing */
272 #ifdef CONFIG_DWC2_TS_DLINE
273 usbcfg |= DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
275 usbcfg &= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE;
277 writel(usbcfg, ®s->gusbcfg);
279 /* Reset the Controller */
280 dwc_otg_core_reset(regs);
283 * This programming sequence needs to happen in FS mode before
284 * any other programming occurs
286 #if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
287 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
288 /* If FS mode with FS PHY */
289 setbits_le32(®s->gusbcfg, DWC2_GUSBCFG_PHYSEL);
291 /* Reset after a PHY select */
292 dwc_otg_core_reset(regs);
295 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
296 * Also do this on HNP Dev/Host mode switches (done in dev_init
299 if (readl(®s->gintsts) & DWC2_GINTSTS_CURMODE_HOST)
300 init_fslspclksel(regs);
302 #ifdef CONFIG_DWC2_I2C_ENABLE
303 /* Program GUSBCFG.OtgUtmifsSel to I2C */
304 setbits_le32(®s->gusbcfg, DWC2_GUSBCFG_OTGUTMIFSSEL);
306 /* Program GI2CCTL.I2CEn */
307 clrsetbits_le32(®s->gi2cctl, DWC2_GI2CCTL_I2CEN |
308 DWC2_GI2CCTL_I2CDEVADDR_MASK,
309 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET);
310 setbits_le32(®s->gi2cctl, DWC2_GI2CCTL_I2CEN);
314 /* High speed PHY. */
317 * HS PHY parameters. These parameters are preserved during
318 * soft reset so only program the first time. Do a soft reset
319 * immediately after setting phyif.
321 usbcfg &= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL | DWC2_GUSBCFG_PHYIF);
322 usbcfg |= CONFIG_DWC2_PHY_TYPE << DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET;
324 if (usbcfg & DWC2_GUSBCFG_ULPI_UTMI_SEL) { /* ULPI interface */
325 #ifdef CONFIG_DWC2_PHY_ULPI_DDR
326 usbcfg |= DWC2_GUSBCFG_DDRSEL;
328 usbcfg &= ~DWC2_GUSBCFG_DDRSEL;
330 } else { /* UTMI+ interface */
331 #if (CONFIG_DWC2_UTMI_PHY_WIDTH == 16)
332 usbcfg |= DWC2_GUSBCFG_PHYIF;
336 writel(usbcfg, ®s->gusbcfg);
338 /* Reset after setting the PHY parameters */
339 dwc_otg_core_reset(regs);
342 usbcfg = readl(®s->gusbcfg);
343 usbcfg &= ~(DWC2_GUSBCFG_ULPI_FSLS | DWC2_GUSBCFG_ULPI_CLK_SUS_M);
344 #ifdef CONFIG_DWC2_ULPI_FS_LS
345 uint32_t hwcfg2 = readl(®s->ghwcfg2);
346 uint32_t hval = (ghwcfg2 & DWC2_HWCFG2_HS_PHY_TYPE_MASK) >>
347 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET;
348 uint32_t fval = (ghwcfg2 & DWC2_HWCFG2_FS_PHY_TYPE_MASK) >>
349 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET;
350 if (hval == 2 && fval == 1) {
351 usbcfg |= DWC2_GUSBCFG_ULPI_FSLS;
352 usbcfg |= DWC2_GUSBCFG_ULPI_CLK_SUS_M;
355 writel(usbcfg, ®s->gusbcfg);
357 /* Program the GAHBCFG Register. */
358 switch (readl(®s->ghwcfg2) & DWC2_HWCFG2_ARCHITECTURE_MASK) {
359 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY:
361 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA:
362 while (brst_sz > 1) {
363 ahbcfg |= ahbcfg + (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET);
364 ahbcfg &= DWC2_GAHBCFG_HBURSTLEN_MASK;
368 #ifdef CONFIG_DWC2_DMA_ENABLE
369 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
373 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA:
374 ahbcfg |= DWC2_GAHBCFG_HBURSTLEN_INCR4;
375 #ifdef CONFIG_DWC2_DMA_ENABLE
376 ahbcfg |= DWC2_GAHBCFG_DMAENABLE;
381 writel(ahbcfg, ®s->gahbcfg);
383 /* Program the GUSBCFG register for HNP/SRP. */
384 setbits_le32(®s->gusbcfg, DWC2_GUSBCFG_HNPCAP | DWC2_GUSBCFG_SRPCAP);
386 #ifdef CONFIG_DWC2_IC_USB_CAP
387 setbits_le32(®s->gusbcfg, DWC2_GUSBCFG_IC_USB_CAP);
392 * Prepares a host channel for transferring packets to/from a specific
393 * endpoint. The HCCHARn register is set up with the characteristics specified
394 * in _hc. Host channel interrupts that may need to be serviced while this
395 * transfer is in progress are enabled.
397 * @param regs Programming view of DWC_otg controller
398 * @param hc Information needed to initialize the host channel
400 static void dwc_otg_hc_init(struct dwc2_core_regs *regs, uint8_t hc_num,
401 struct usb_device *dev, uint8_t dev_addr, uint8_t ep_num,
402 uint8_t ep_is_in, uint8_t ep_type, uint16_t max_packet)
404 struct dwc2_hc_regs *hc_regs = ®s->hc_regs[hc_num];
405 uint32_t hcchar = (dev_addr << DWC2_HCCHAR_DEVADDR_OFFSET) |
406 (ep_num << DWC2_HCCHAR_EPNUM_OFFSET) |
407 (ep_is_in << DWC2_HCCHAR_EPDIR_OFFSET) |
408 (ep_type << DWC2_HCCHAR_EPTYPE_OFFSET) |
409 (max_packet << DWC2_HCCHAR_MPS_OFFSET);
411 if (dev->speed == USB_SPEED_LOW)
412 hcchar |= DWC2_HCCHAR_LSPDDEV;
415 * Program the HCCHARn register with the endpoint characteristics
416 * for the current transfer.
418 writel(hcchar, &hc_regs->hcchar);
420 /* Program the HCSPLIT register, default to no SPLIT */
421 writel(0, &hc_regs->hcsplt);
424 static void dwc_otg_hc_init_split(struct dwc2_hc_regs *hc_regs,
425 uint8_t hub_devnum, uint8_t hub_port)
429 hcsplt = DWC2_HCSPLT_SPLTENA;
430 hcsplt |= hub_devnum << DWC2_HCSPLT_HUBADDR_OFFSET;
431 hcsplt |= hub_port << DWC2_HCSPLT_PRTADDR_OFFSET;
433 /* Program the HCSPLIT register for SPLITs */
434 writel(hcsplt, &hc_regs->hcsplt);
438 * DWC2 to USB API interface
440 /* Direction: In ; Request: Status */
441 static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs *regs,
442 struct usb_device *dev, void *buffer,
443 int txlen, struct devrequest *cmd)
446 uint32_t port_status = 0;
447 uint32_t port_change = 0;
451 switch (cmd->requesttype & ~USB_DIR_IN) {
453 *(uint16_t *)buffer = cpu_to_le16(1);
456 case USB_RECIP_INTERFACE:
457 case USB_RECIP_ENDPOINT:
458 *(uint16_t *)buffer = cpu_to_le16(0);
462 *(uint32_t *)buffer = cpu_to_le32(0);
465 case USB_RECIP_OTHER | USB_TYPE_CLASS:
466 hprt0 = readl(®s->hprt0);
467 if (hprt0 & DWC2_HPRT0_PRTCONNSTS)
468 port_status |= USB_PORT_STAT_CONNECTION;
469 if (hprt0 & DWC2_HPRT0_PRTENA)
470 port_status |= USB_PORT_STAT_ENABLE;
471 if (hprt0 & DWC2_HPRT0_PRTSUSP)
472 port_status |= USB_PORT_STAT_SUSPEND;
473 if (hprt0 & DWC2_HPRT0_PRTOVRCURRACT)
474 port_status |= USB_PORT_STAT_OVERCURRENT;
475 if (hprt0 & DWC2_HPRT0_PRTRST)
476 port_status |= USB_PORT_STAT_RESET;
477 if (hprt0 & DWC2_HPRT0_PRTPWR)
478 port_status |= USB_PORT_STAT_POWER;
480 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) == DWC2_HPRT0_PRTSPD_LOW)
481 port_status |= USB_PORT_STAT_LOW_SPEED;
482 else if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
483 DWC2_HPRT0_PRTSPD_HIGH)
484 port_status |= USB_PORT_STAT_HIGH_SPEED;
486 if (hprt0 & DWC2_HPRT0_PRTENCHNG)
487 port_change |= USB_PORT_STAT_C_ENABLE;
488 if (hprt0 & DWC2_HPRT0_PRTCONNDET)
489 port_change |= USB_PORT_STAT_C_CONNECTION;
490 if (hprt0 & DWC2_HPRT0_PRTOVRCURRCHNG)
491 port_change |= USB_PORT_STAT_C_OVERCURRENT;
493 *(uint32_t *)buffer = cpu_to_le32(port_status |
494 (port_change << 16));
498 puts("unsupported root hub command\n");
499 stat = USB_ST_STALLED;
502 dev->act_len = min(len, txlen);
508 /* Direction: In ; Request: Descriptor */
509 static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device *dev,
510 void *buffer, int txlen,
511 struct devrequest *cmd)
513 unsigned char data[32];
517 uint16_t wValue = cpu_to_le16(cmd->value);
518 uint16_t wLength = cpu_to_le16(cmd->length);
520 switch (cmd->requesttype & ~USB_DIR_IN) {
522 switch (wValue & 0xff00) {
523 case 0x0100: /* device descriptor */
524 len = min3(txlen, (int)sizeof(root_hub_dev_des), (int)wLength);
525 memcpy(buffer, root_hub_dev_des, len);
527 case 0x0200: /* configuration descriptor */
528 len = min3(txlen, (int)sizeof(root_hub_config_des), (int)wLength);
529 memcpy(buffer, root_hub_config_des, len);
531 case 0x0300: /* string descriptors */
532 switch (wValue & 0xff) {
534 len = min3(txlen, (int)sizeof(root_hub_str_index0),
536 memcpy(buffer, root_hub_str_index0, len);
539 len = min3(txlen, (int)sizeof(root_hub_str_index1),
541 memcpy(buffer, root_hub_str_index1, len);
546 stat = USB_ST_STALLED;
551 /* Root port config, set 1 port and nothing else. */
554 data[0] = 9; /* min length; */
556 data[2] = dsc & RH_A_NDP;
562 else if (dsc & RH_A_OCPM)
565 /* corresponds to data[4-7] */
566 data[5] = (dsc & RH_A_POTPGT) >> 24;
567 data[7] = dsc & RH_B_DR;
572 data[8] = (dsc & RH_B_DR) >> 8;
577 len = min3(txlen, (int)data[0], (int)wLength);
578 memcpy(buffer, data, len);
581 puts("unsupported root hub command\n");
582 stat = USB_ST_STALLED;
585 dev->act_len = min(len, txlen);
591 /* Direction: In ; Request: Configuration */
592 static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device *dev,
593 void *buffer, int txlen,
594 struct devrequest *cmd)
599 switch (cmd->requesttype & ~USB_DIR_IN) {
601 *(uint8_t *)buffer = 0x01;
605 puts("unsupported root hub command\n");
606 stat = USB_ST_STALLED;
609 dev->act_len = min(len, txlen);
616 static int dwc_otg_submit_rh_msg_in(struct dwc2_priv *priv,
617 struct usb_device *dev, void *buffer,
618 int txlen, struct devrequest *cmd)
620 switch (cmd->request) {
621 case USB_REQ_GET_STATUS:
622 return dwc_otg_submit_rh_msg_in_status(priv->regs, dev, buffer,
624 case USB_REQ_GET_DESCRIPTOR:
625 return dwc_otg_submit_rh_msg_in_descriptor(dev, buffer,
627 case USB_REQ_GET_CONFIGURATION:
628 return dwc_otg_submit_rh_msg_in_configuration(dev, buffer,
631 puts("unsupported root hub command\n");
632 return USB_ST_STALLED;
637 static int dwc_otg_submit_rh_msg_out(struct dwc2_priv *priv,
638 struct usb_device *dev,
639 void *buffer, int txlen,
640 struct devrequest *cmd)
642 struct dwc2_core_regs *regs = priv->regs;
645 uint16_t bmrtype_breq = cmd->requesttype | (cmd->request << 8);
646 uint16_t wValue = cpu_to_le16(cmd->value);
648 switch (bmrtype_breq & ~USB_DIR_IN) {
649 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_ENDPOINT:
650 case (USB_REQ_CLEAR_FEATURE << 8) | USB_TYPE_CLASS:
653 case (USB_REQ_CLEAR_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
655 case USB_PORT_FEAT_C_CONNECTION:
656 setbits_le32(®s->hprt0, DWC2_HPRT0_PRTCONNDET);
661 case (USB_REQ_SET_FEATURE << 8) | USB_RECIP_OTHER | USB_TYPE_CLASS:
663 case USB_PORT_FEAT_SUSPEND:
666 case USB_PORT_FEAT_RESET:
667 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
668 DWC2_HPRT0_PRTCONNDET |
669 DWC2_HPRT0_PRTENCHNG |
670 DWC2_HPRT0_PRTOVRCURRCHNG,
673 clrbits_le32(®s->hprt0, DWC2_HPRT0_PRTRST);
676 case USB_PORT_FEAT_POWER:
677 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
678 DWC2_HPRT0_PRTCONNDET |
679 DWC2_HPRT0_PRTENCHNG |
680 DWC2_HPRT0_PRTOVRCURRCHNG,
684 case USB_PORT_FEAT_ENABLE:
688 case (USB_REQ_SET_ADDRESS << 8):
689 priv->root_hub_devnum = wValue;
691 case (USB_REQ_SET_CONFIGURATION << 8):
694 puts("unsupported root hub command\n");
695 stat = USB_ST_STALLED;
698 len = min(len, txlen);
706 static int dwc_otg_submit_rh_msg(struct dwc2_priv *priv, struct usb_device *dev,
707 unsigned long pipe, void *buffer, int txlen,
708 struct devrequest *cmd)
712 if (usb_pipeint(pipe)) {
713 puts("Root-Hub submit IRQ: NOT implemented\n");
717 if (cmd->requesttype & USB_DIR_IN)
718 stat = dwc_otg_submit_rh_msg_in(priv, dev, buffer, txlen, cmd);
720 stat = dwc_otg_submit_rh_msg_out(priv, dev, buffer, txlen, cmd);
727 int wait_for_chhltd(struct dwc2_hc_regs *hc_regs, uint32_t *sub, u8 *toggle)
730 uint32_t hcint, hctsiz;
732 ret = wait_for_bit(__func__, &hc_regs->hcint, DWC2_HCINT_CHHLTD, true,
737 hcint = readl(&hc_regs->hcint);
738 hctsiz = readl(&hc_regs->hctsiz);
739 *sub = (hctsiz & DWC2_HCTSIZ_XFERSIZE_MASK) >>
740 DWC2_HCTSIZ_XFERSIZE_OFFSET;
741 *toggle = (hctsiz & DWC2_HCTSIZ_PID_MASK) >> DWC2_HCTSIZ_PID_OFFSET;
743 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__, hcint, *sub,
746 if (hcint & DWC2_HCINT_XFERCOMP)
749 if (hcint & (DWC2_HCINT_NAK | DWC2_HCINT_FRMOVRUN))
752 debug("%s: Error (HCINT=%08x)\n", __func__, hcint);
756 static int dwc2_eptype[] = {
757 DWC2_HCCHAR_EPTYPE_ISOC,
758 DWC2_HCCHAR_EPTYPE_INTR,
759 DWC2_HCCHAR_EPTYPE_CONTROL,
760 DWC2_HCCHAR_EPTYPE_BULK,
763 static int transfer_chunk(struct dwc2_hc_regs *hc_regs, void *aligned_buffer,
764 u8 *pid, int in, void *buffer, int num_packets,
765 int xfer_len, int *actual_len, int odd_frame)
770 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__,
771 *pid, xfer_len, num_packets);
773 writel((xfer_len << DWC2_HCTSIZ_XFERSIZE_OFFSET) |
774 (num_packets << DWC2_HCTSIZ_PKTCNT_OFFSET) |
775 (*pid << DWC2_HCTSIZ_PID_OFFSET),
778 if (!in && xfer_len) {
779 memcpy(aligned_buffer, buffer, xfer_len);
781 flush_dcache_range((unsigned long)aligned_buffer,
782 (unsigned long)aligned_buffer +
783 roundup(xfer_len, ARCH_DMA_MINALIGN));
786 writel(phys_to_bus((unsigned long)aligned_buffer), &hc_regs->hcdma);
788 /* Clear old interrupt conditions for this host channel. */
789 writel(0x3fff, &hc_regs->hcint);
791 /* Set host channel enable after all other setup is complete. */
792 clrsetbits_le32(&hc_regs->hcchar, DWC2_HCCHAR_MULTICNT_MASK |
793 DWC2_HCCHAR_CHEN | DWC2_HCCHAR_CHDIS |
795 (1 << DWC2_HCCHAR_MULTICNT_OFFSET) |
796 (odd_frame << DWC2_HCCHAR_ODDFRM_OFFSET) |
799 ret = wait_for_chhltd(hc_regs, &sub, pid);
806 invalidate_dcache_range((unsigned long)aligned_buffer,
807 (unsigned long)aligned_buffer +
808 roundup(xfer_len, ARCH_DMA_MINALIGN));
810 memcpy(buffer, aligned_buffer, xfer_len);
812 *actual_len = xfer_len;
817 int chunk_msg(struct dwc2_priv *priv, struct usb_device *dev,
818 unsigned long pipe, u8 *pid, int in, void *buffer, int len)
820 struct dwc2_core_regs *regs = priv->regs;
821 struct dwc2_hc_regs *hc_regs = ®s->hc_regs[DWC2_HC_CHANNEL];
822 struct dwc2_host_regs *host_regs = ®s->host_regs;
823 int devnum = usb_pipedevice(pipe);
824 int ep = usb_pipeendpoint(pipe);
825 int max = usb_maxpacket(dev, pipe);
826 int eptype = dwc2_eptype[usb_pipetype(pipe)];
830 int complete_split = 0;
832 uint32_t num_packets;
833 int stop_transfer = 0;
834 uint32_t max_xfer_len;
835 int ssplit_frame_num = 0;
837 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__, pipe, *pid,
840 max_xfer_len = CONFIG_DWC2_MAX_PACKET_COUNT * max;
841 if (max_xfer_len > CONFIG_DWC2_MAX_TRANSFER_SIZE)
842 max_xfer_len = CONFIG_DWC2_MAX_TRANSFER_SIZE;
843 if (max_xfer_len > DWC2_DATA_BUF_SIZE)
844 max_xfer_len = DWC2_DATA_BUF_SIZE;
846 /* Make sure that max_xfer_len is a multiple of max packet size. */
847 num_packets = max_xfer_len / max;
848 max_xfer_len = num_packets * max;
850 /* Initialize channel */
851 dwc_otg_hc_init(regs, DWC2_HC_CHANNEL, dev, devnum, ep, in,
854 /* Check if the target is a FS/LS device behind a HS hub */
855 if (dev->speed != USB_SPEED_HIGH) {
858 uint32_t hprt0 = readl(®s->hprt0);
859 if ((hprt0 & DWC2_HPRT0_PRTSPD_MASK) ==
860 DWC2_HPRT0_PRTSPD_HIGH) {
861 usb_find_usb2_hub_address_port(dev, &hub_addr,
863 dwc_otg_hc_init_split(hc_regs, hub_addr, hub_port);
875 xfer_len = len - done;
877 if (xfer_len > max_xfer_len)
878 xfer_len = max_xfer_len;
879 else if (xfer_len > max)
880 num_packets = (xfer_len + max - 1) / max;
885 setbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
887 clrbits_le32(&hc_regs->hcsplt, DWC2_HCSPLT_COMPSPLT);
889 if (eptype == DWC2_HCCHAR_EPTYPE_INTR) {
890 int uframe_num = readl(&host_regs->hfnum);
891 if (!(uframe_num & 0x1))
895 ret = transfer_chunk(hc_regs, priv->aligned_buffer, pid,
896 in, (char *)buffer + done, num_packets,
897 xfer_len, &actual_len, odd_frame);
899 hcint = readl(&hc_regs->hcint);
900 if (complete_split) {
902 if (hcint & DWC2_HCINT_NYET) {
904 int frame_num = DWC2_HFNUM_MAX_FRNUM &
905 readl(&host_regs->hfnum);
906 if (((frame_num - ssplit_frame_num) &
907 DWC2_HFNUM_MAX_FRNUM) > 4)
911 } else if (do_split) {
912 if (hcint & DWC2_HCINT_ACK) {
913 ssplit_frame_num = DWC2_HFNUM_MAX_FRNUM &
914 readl(&host_regs->hfnum);
923 if (actual_len < xfer_len)
928 /* Transactions are done when when either all data is transferred or
929 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
932 } while (((done < len) && !stop_transfer) || complete_split);
934 writel(0, &hc_regs->hcintmsk);
935 writel(0xFFFFFFFF, &hc_regs->hcint);
943 /* U-Boot USB transmission interface */
944 int _submit_bulk_msg(struct dwc2_priv *priv, struct usb_device *dev,
945 unsigned long pipe, void *buffer, int len)
947 int devnum = usb_pipedevice(pipe);
948 int ep = usb_pipeendpoint(pipe);
951 if ((devnum >= MAX_DEVICE) || (devnum == priv->root_hub_devnum)) {
956 if (usb_pipein(pipe))
957 pid = &priv->in_data_toggle[devnum][ep];
959 pid = &priv->out_data_toggle[devnum][ep];
961 return chunk_msg(priv, dev, pipe, pid, usb_pipein(pipe), buffer, len);
964 static int _submit_control_msg(struct dwc2_priv *priv, struct usb_device *dev,
965 unsigned long pipe, void *buffer, int len,
966 struct devrequest *setup)
968 int devnum = usb_pipedevice(pipe);
971 /* For CONTROL endpoint pid should start with DATA1 */
972 int status_direction;
974 if (devnum == priv->root_hub_devnum) {
976 dev->speed = USB_SPEED_HIGH;
977 return dwc_otg_submit_rh_msg(priv, dev, pipe, buffer, len,
982 pid = DWC2_HC_PID_SETUP;
984 ret = chunk_msg(priv, dev, pipe, &pid, 0, setup, 8);
985 } while (ret == -EAGAIN);
992 pid = DWC2_HC_PID_DATA1;
994 ret = chunk_msg(priv, dev, pipe, &pid, usb_pipein(pipe),
996 act_len += dev->act_len;
997 buffer += dev->act_len;
999 } while (ret == -EAGAIN);
1002 status_direction = usb_pipeout(pipe);
1004 /* No-data CONTROL always ends with an IN transaction */
1005 status_direction = 1;
1009 pid = DWC2_HC_PID_DATA1;
1011 ret = chunk_msg(priv, dev, pipe, &pid, status_direction,
1012 priv->status_buffer, 0);
1013 } while (ret == -EAGAIN);
1017 dev->act_len = act_len;
1022 int _submit_int_msg(struct dwc2_priv *priv, struct usb_device *dev,
1023 unsigned long pipe, void *buffer, int len, int interval)
1025 unsigned long timeout;
1028 /* FIXME: what is interval? */
1030 timeout = get_timer(0) + USB_TIMEOUT_MS(pipe);
1032 if (get_timer(0) > timeout) {
1033 printf("Timeout poll on interrupt endpoint\n");
1036 ret = _submit_bulk_msg(priv, dev, pipe, buffer, len);
1042 static int dwc2_init_common(struct dwc2_priv *priv)
1044 struct dwc2_core_regs *regs = priv->regs;
1048 snpsid = readl(®s->gsnpsid);
1049 printf("Core Release: %x.%03x\n", snpsid >> 12 & 0xf, snpsid & 0xfff);
1051 if ((snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_2xx &&
1052 (snpsid & DWC2_SNPSID_DEVID_MASK) != DWC2_SNPSID_DEVID_VER_3xx) {
1053 printf("SNPSID invalid (not DWC2 OTG device): %08x\n", snpsid);
1057 dwc_otg_core_init(regs);
1058 dwc_otg_core_host_init(regs);
1060 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
1061 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1062 DWC2_HPRT0_PRTOVRCURRCHNG,
1065 clrbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA | DWC2_HPRT0_PRTCONNDET |
1066 DWC2_HPRT0_PRTENCHNG | DWC2_HPRT0_PRTOVRCURRCHNG |
1069 for (i = 0; i < MAX_DEVICE; i++) {
1070 for (j = 0; j < MAX_ENDPOINT; j++) {
1071 priv->in_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1072 priv->out_data_toggle[i][j] = DWC2_HC_PID_DATA0;
1079 static void dwc2_uninit_common(struct dwc2_core_regs *regs)
1081 /* Put everything in reset. */
1082 clrsetbits_le32(®s->hprt0, DWC2_HPRT0_PRTENA |
1083 DWC2_HPRT0_PRTCONNDET | DWC2_HPRT0_PRTENCHNG |
1084 DWC2_HPRT0_PRTOVRCURRCHNG,
1088 #ifndef CONFIG_DM_USB
1089 int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1090 int len, struct devrequest *setup)
1092 return _submit_control_msg(&local, dev, pipe, buffer, len, setup);
1095 int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1098 return _submit_bulk_msg(&local, dev, pipe, buffer, len);
1101 int submit_int_msg(struct usb_device *dev, unsigned long pipe, void *buffer,
1102 int len, int interval)
1104 return _submit_int_msg(&local, dev, pipe, buffer, len, interval);
1107 /* U-Boot USB control interface */
1108 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1110 struct dwc2_priv *priv = &local;
1112 memset(priv, '\0', sizeof(*priv));
1113 priv->root_hub_devnum = 0;
1114 priv->regs = (struct dwc2_core_regs *)CONFIG_USB_DWC2_REG_ADDR;
1115 priv->aligned_buffer = aligned_buffer_addr;
1116 priv->status_buffer = status_buffer_addr;
1118 /* board-dependant init */
1119 if (board_usb_init(index, USB_INIT_HOST))
1122 return dwc2_init_common(priv);
1125 int usb_lowlevel_stop(int index)
1127 dwc2_uninit_common(local.regs);
1133 #ifdef CONFIG_DM_USB
1134 static int dwc2_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1135 unsigned long pipe, void *buffer, int length,
1136 struct devrequest *setup)
1138 struct dwc2_priv *priv = dev_get_priv(dev);
1140 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1141 dev->name, udev, udev->dev->name, udev->portnr);
1143 return _submit_control_msg(priv, udev, pipe, buffer, length, setup);
1146 static int dwc2_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1147 unsigned long pipe, void *buffer, int length)
1149 struct dwc2_priv *priv = dev_get_priv(dev);
1151 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1153 return _submit_bulk_msg(priv, udev, pipe, buffer, length);
1156 static int dwc2_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1157 unsigned long pipe, void *buffer, int length,
1160 struct dwc2_priv *priv = dev_get_priv(dev);
1162 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1164 return _submit_int_msg(priv, udev, pipe, buffer, length, interval);
1167 static int dwc2_usb_ofdata_to_platdata(struct udevice *dev)
1169 struct dwc2_priv *priv = dev_get_priv(dev);
1172 addr = dev_get_addr(dev);
1173 if (addr == FDT_ADDR_T_NONE)
1175 priv->regs = (struct dwc2_core_regs *)addr;
1180 static int dwc2_usb_probe(struct udevice *dev)
1182 struct dwc2_priv *priv = dev_get_priv(dev);
1184 return dwc2_init_common(priv);
1187 static int dwc2_usb_remove(struct udevice *dev)
1189 struct dwc2_priv *priv = dev_get_priv(dev);
1191 dwc2_uninit_common(priv->regs);
1196 struct dm_usb_ops dwc2_usb_ops = {
1197 .control = dwc2_submit_control_msg,
1198 .bulk = dwc2_submit_bulk_msg,
1199 .interrupt = dwc2_submit_int_msg,
1202 static const struct udevice_id dwc2_usb_ids[] = {
1203 { .compatible = "brcm,bcm2835-usb" },
1204 { .compatible = "snps,dwc2" },
1208 U_BOOT_DRIVER(usb_dwc2) = {
1211 .of_match = dwc2_usb_ids,
1212 .ofdata_to_platdata = dwc2_usb_ofdata_to_platdata,
1213 .probe = dwc2_usb_probe,
1214 .remove = dwc2_usb_remove,
1215 .ops = &dwc2_usb_ops,
1216 .priv_auto_alloc_size = sizeof(struct dwc2_priv),
1217 .flags = DM_FLAG_ALLOC_PRIV_DMA,