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>
15 #include <usbroothubdes.h>
18 #include <dm/device_compat.h>
19 #include <power/regulator.h>
24 /* Use only HC channel 0. */
25 #define DWC2_HC_CHANNEL 0
27 #define DWC2_STATUS_BUF_SIZE 64
28 #define DWC2_DATA_BUF_SIZE (CONFIG_USB_DWC2_BUFFER_SIZE * 1024)
31 #define MAX_ENDPOINT 16
34 #if CONFIG_IS_ENABLED(DM_USB)
35 uint8_t aligned_buffer
[DWC2_DATA_BUF_SIZE
] __aligned(ARCH_DMA_MINALIGN
);
36 uint8_t status_buffer
[DWC2_STATUS_BUF_SIZE
] __aligned(ARCH_DMA_MINALIGN
);
37 #ifdef CONFIG_DM_REGULATOR
38 struct udevice
*vbus_supply
;
41 uint8_t *aligned_buffer
;
42 uint8_t *status_buffer
;
44 u8 in_data_toggle
[MAX_DEVICE
][MAX_ENDPOINT
];
45 u8 out_data_toggle
[MAX_DEVICE
][MAX_ENDPOINT
];
46 struct dwc2_core_regs
*regs
;
50 * The hnp/srp capability must be disabled if the platform
51 * does't support hnp/srp. Otherwise the force mode can't work.
56 struct reset_ctl_bulk resets
;
59 #if !CONFIG_IS_ENABLED(DM_USB)
60 /* We need cacheline-aligned buffers for DMA transfers and dcache support */
61 DEFINE_ALIGN_BUFFER(uint8_t, aligned_buffer_addr
, DWC2_DATA_BUF_SIZE
,
63 DEFINE_ALIGN_BUFFER(uint8_t, status_buffer_addr
, DWC2_STATUS_BUF_SIZE
,
66 static struct dwc2_priv local
;
74 * Initializes the FSLSPClkSel field of the HCFG register
75 * depending on the PHY type.
77 static void init_fslspclksel(struct dwc2_core_regs
*regs
)
81 #if (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
82 phyclk
= DWC2_HCFG_FSLSPCLKSEL_48_MHZ
; /* Full speed PHY */
84 /* High speed PHY running at full speed or high speed */
85 phyclk
= DWC2_HCFG_FSLSPCLKSEL_30_60_MHZ
;
88 #ifdef CONFIG_DWC2_ULPI_FS_LS
89 uint32_t hwcfg2
= readl(®s
->ghwcfg2
);
90 uint32_t hval
= (ghwcfg2
& DWC2_HWCFG2_HS_PHY_TYPE_MASK
) >>
91 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET
;
92 uint32_t fval
= (ghwcfg2
& DWC2_HWCFG2_FS_PHY_TYPE_MASK
) >>
93 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET
;
95 if (hval
== 2 && fval
== 1)
96 phyclk
= DWC2_HCFG_FSLSPCLKSEL_48_MHZ
; /* Full speed PHY */
99 clrsetbits_le32(®s
->host_regs
.hcfg
,
100 DWC2_HCFG_FSLSPCLKSEL_MASK
,
101 phyclk
<< DWC2_HCFG_FSLSPCLKSEL_OFFSET
);
107 * @param regs Programming view of DWC_otg controller.
108 * @param num Tx FIFO to flush.
110 static void dwc_otg_flush_tx_fifo(struct dwc2_core_regs
*regs
, const int num
)
114 writel(DWC2_GRSTCTL_TXFFLSH
| (num
<< DWC2_GRSTCTL_TXFNUM_OFFSET
),
116 ret
= wait_for_bit_le32(®s
->grstctl
, DWC2_GRSTCTL_TXFFLSH
,
119 dev_info(dev
, "%s: Timeout!\n", __func__
);
121 /* Wait for 3 PHY Clocks */
128 * @param regs Programming view of DWC_otg controller.
130 static void dwc_otg_flush_rx_fifo(struct dwc2_core_regs
*regs
)
134 writel(DWC2_GRSTCTL_RXFFLSH
, ®s
->grstctl
);
135 ret
= wait_for_bit_le32(®s
->grstctl
, DWC2_GRSTCTL_RXFFLSH
,
138 dev_info(dev
, "%s: Timeout!\n", __func__
);
140 /* Wait for 3 PHY Clocks */
145 * Do core a soft reset of the core. Be careful with this because it
146 * resets all the internal state machines of the core.
148 static void dwc_otg_core_reset(struct dwc2_core_regs
*regs
)
152 /* Wait for AHB master IDLE state. */
153 ret
= wait_for_bit_le32(®s
->grstctl
, DWC2_GRSTCTL_AHBIDLE
,
156 dev_info(dev
, "%s: Timeout!\n", __func__
);
158 /* Core Soft Reset */
159 writel(DWC2_GRSTCTL_CSFTRST
, ®s
->grstctl
);
160 ret
= wait_for_bit_le32(®s
->grstctl
, DWC2_GRSTCTL_CSFTRST
,
163 dev_info(dev
, "%s: Timeout!\n", __func__
);
166 * Wait for core to come out of reset.
167 * NOTE: This long sleep is _very_ important, otherwise the core will
168 * not stay in host mode after a connector ID change!
173 #if CONFIG_IS_ENABLED(DM_USB) && defined(CONFIG_DM_REGULATOR)
174 static int dwc_vbus_supply_init(struct udevice
*dev
)
176 struct dwc2_priv
*priv
= dev_get_priv(dev
);
179 ret
= device_get_supply_regulator(dev
, "vbus-supply",
182 debug("%s: No vbus supply\n", dev
->name
);
186 ret
= regulator_set_enable(priv
->vbus_supply
, true);
188 dev_err(dev
, "Error enabling vbus supply\n");
195 static int dwc_vbus_supply_exit(struct udevice
*dev
)
197 struct dwc2_priv
*priv
= dev_get_priv(dev
);
200 if (priv
->vbus_supply
) {
201 ret
= regulator_set_enable(priv
->vbus_supply
, false);
203 dev_err(dev
, "Error disabling vbus supply\n");
211 static int dwc_vbus_supply_init(struct udevice
*dev
)
216 #if CONFIG_IS_ENABLED(DM_USB)
217 static int dwc_vbus_supply_exit(struct udevice
*dev
)
225 * This function initializes the DWC_otg controller registers for
228 * This function flushes the Tx and Rx FIFOs and it flushes any entries in the
229 * request queues. Host channels are reset to ensure that they are ready for
230 * performing transfers.
232 * @param dev USB Device (NULL if driver model is not being used)
233 * @param regs Programming view of DWC_otg controller
236 static void dwc_otg_core_host_init(struct udevice
*dev
,
237 struct dwc2_core_regs
*regs
)
239 uint32_t nptxfifosize
= 0;
240 uint32_t ptxfifosize
= 0;
242 int i
, ret
, num_channels
;
244 /* Restart the Phy Clock */
245 writel(0, ®s
->pcgcctl
);
247 /* Initialize Host Configuration Register */
248 init_fslspclksel(regs
);
249 #ifdef CONFIG_DWC2_DFLT_SPEED_FULL
250 setbits_le32(®s
->host_regs
.hcfg
, DWC2_HCFG_FSLSSUPP
);
253 /* Configure data FIFO sizes */
254 #ifdef CONFIG_DWC2_ENABLE_DYNAMIC_FIFO
255 if (readl(®s
->ghwcfg2
) & DWC2_HWCFG2_DYNAMIC_FIFO
) {
257 writel(CONFIG_DWC2_HOST_RX_FIFO_SIZE
, ®s
->grxfsiz
);
259 /* Non-periodic Tx FIFO */
260 nptxfifosize
|= CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE
<<
261 DWC2_FIFOSIZE_DEPTH_OFFSET
;
262 nptxfifosize
|= CONFIG_DWC2_HOST_RX_FIFO_SIZE
<<
263 DWC2_FIFOSIZE_STARTADDR_OFFSET
;
264 writel(nptxfifosize
, ®s
->gnptxfsiz
);
266 /* Periodic Tx FIFO */
267 ptxfifosize
|= CONFIG_DWC2_HOST_PERIO_TX_FIFO_SIZE
<<
268 DWC2_FIFOSIZE_DEPTH_OFFSET
;
269 ptxfifosize
|= (CONFIG_DWC2_HOST_RX_FIFO_SIZE
+
270 CONFIG_DWC2_HOST_NPERIO_TX_FIFO_SIZE
) <<
271 DWC2_FIFOSIZE_STARTADDR_OFFSET
;
272 writel(ptxfifosize
, ®s
->hptxfsiz
);
276 /* Clear Host Set HNP Enable in the OTG Control Register */
277 clrbits_le32(®s
->gotgctl
, DWC2_GOTGCTL_HSTSETHNPEN
);
279 /* Make sure the FIFOs are flushed. */
280 dwc_otg_flush_tx_fifo(regs
, 0x10); /* All Tx FIFOs */
281 dwc_otg_flush_rx_fifo(regs
);
283 /* Flush out any leftover queued requests. */
284 num_channels
= readl(®s
->ghwcfg2
);
285 num_channels
&= DWC2_HWCFG2_NUM_HOST_CHAN_MASK
;
286 num_channels
>>= DWC2_HWCFG2_NUM_HOST_CHAN_OFFSET
;
289 for (i
= 0; i
< num_channels
; i
++)
290 clrsetbits_le32(®s
->hc_regs
[i
].hcchar
,
291 DWC2_HCCHAR_CHEN
| DWC2_HCCHAR_EPDIR
,
294 /* Halt all channels to put them into a known state. */
295 for (i
= 0; i
< num_channels
; i
++) {
296 clrsetbits_le32(®s
->hc_regs
[i
].hcchar
,
298 DWC2_HCCHAR_CHEN
| DWC2_HCCHAR_CHDIS
);
299 ret
= wait_for_bit_le32(®s
->hc_regs
[i
].hcchar
,
300 DWC2_HCCHAR_CHEN
, false, 1000, false);
302 dev_info("%s: Timeout!\n", __func__
);
305 /* Turn on the vbus power. */
306 if (readl(®s
->gintsts
) & DWC2_GINTSTS_CURMODE_HOST
) {
307 hprt0
= readl(®s
->hprt0
);
308 hprt0
&= ~(DWC2_HPRT0_PRTENA
| DWC2_HPRT0_PRTCONNDET
);
309 hprt0
&= ~(DWC2_HPRT0_PRTENCHNG
| DWC2_HPRT0_PRTOVRCURRCHNG
);
310 if (!(hprt0
& DWC2_HPRT0_PRTPWR
)) {
311 hprt0
|= DWC2_HPRT0_PRTPWR
;
312 writel(hprt0
, ®s
->hprt0
);
317 dwc_vbus_supply_init(dev
);
321 * This function initializes the DWC_otg controller registers and
322 * prepares the core for device mode or host mode operation.
324 * @param regs Programming view of the DWC_otg controller
326 static void dwc_otg_core_init(struct dwc2_priv
*priv
)
328 struct dwc2_core_regs
*regs
= priv
->regs
;
331 uint8_t brst_sz
= CONFIG_DWC2_DMA_BURST_SIZE
;
333 /* Common Initialization */
334 usbcfg
= readl(®s
->gusbcfg
);
336 /* Program the ULPI External VBUS bit if needed */
337 if (priv
->ext_vbus
) {
338 usbcfg
|= DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV
;
339 if (!priv
->oc_disable
) {
340 usbcfg
|= DWC2_GUSBCFG_ULPI_INT_VBUS_INDICATOR
|
341 DWC2_GUSBCFG_INDICATOR_PASSTHROUGH
;
344 usbcfg
&= ~DWC2_GUSBCFG_ULPI_EXT_VBUS_DRV
;
347 /* Set external TS Dline pulsing */
348 #ifdef CONFIG_DWC2_TS_DLINE
349 usbcfg
|= DWC2_GUSBCFG_TERM_SEL_DL_PULSE
;
351 usbcfg
&= ~DWC2_GUSBCFG_TERM_SEL_DL_PULSE
;
353 writel(usbcfg
, ®s
->gusbcfg
);
355 /* Reset the Controller */
356 dwc_otg_core_reset(regs
);
359 * This programming sequence needs to happen in FS mode before
360 * any other programming occurs
362 #if defined(CONFIG_DWC2_DFLT_SPEED_FULL) && \
363 (CONFIG_DWC2_PHY_TYPE == DWC2_PHY_TYPE_FS)
364 /* If FS mode with FS PHY */
365 setbits_le32(®s
->gusbcfg
, DWC2_GUSBCFG_PHYSEL
);
367 /* Reset after a PHY select */
368 dwc_otg_core_reset(regs
);
371 * Program DCFG.DevSpd or HCFG.FSLSPclkSel to 48Mhz in FS.
372 * Also do this on HNP Dev/Host mode switches (done in dev_init
375 if (readl(®s
->gintsts
) & DWC2_GINTSTS_CURMODE_HOST
)
376 init_fslspclksel(regs
);
378 #ifdef CONFIG_DWC2_I2C_ENABLE
379 /* Program GUSBCFG.OtgUtmifsSel to I2C */
380 setbits_le32(®s
->gusbcfg
, DWC2_GUSBCFG_OTGUTMIFSSEL
);
382 /* Program GI2CCTL.I2CEn */
383 clrsetbits_le32(®s
->gi2cctl
, DWC2_GI2CCTL_I2CEN
|
384 DWC2_GI2CCTL_I2CDEVADDR_MASK
,
385 1 << DWC2_GI2CCTL_I2CDEVADDR_OFFSET
);
386 setbits_le32(®s
->gi2cctl
, DWC2_GI2CCTL_I2CEN
);
390 /* High speed PHY. */
393 * HS PHY parameters. These parameters are preserved during
394 * soft reset so only program the first time. Do a soft reset
395 * immediately after setting phyif.
397 usbcfg
&= ~(DWC2_GUSBCFG_ULPI_UTMI_SEL
| DWC2_GUSBCFG_PHYIF
);
398 usbcfg
|= CONFIG_DWC2_PHY_TYPE
<< DWC2_GUSBCFG_ULPI_UTMI_SEL_OFFSET
;
400 if (usbcfg
& DWC2_GUSBCFG_ULPI_UTMI_SEL
) { /* ULPI interface */
401 #ifdef CONFIG_DWC2_PHY_ULPI_DDR
402 usbcfg
|= DWC2_GUSBCFG_DDRSEL
;
404 usbcfg
&= ~DWC2_GUSBCFG_DDRSEL
;
406 } else { /* UTMI+ interface */
407 #if (CONFIG_DWC2_UTMI_WIDTH == 16)
408 usbcfg
|= DWC2_GUSBCFG_PHYIF
;
412 writel(usbcfg
, ®s
->gusbcfg
);
414 /* Reset after setting the PHY parameters */
415 dwc_otg_core_reset(regs
);
418 usbcfg
= readl(®s
->gusbcfg
);
419 usbcfg
&= ~(DWC2_GUSBCFG_ULPI_FSLS
| DWC2_GUSBCFG_ULPI_CLK_SUS_M
);
420 #ifdef CONFIG_DWC2_ULPI_FS_LS
421 uint32_t hwcfg2
= readl(®s
->ghwcfg2
);
422 uint32_t hval
= (ghwcfg2
& DWC2_HWCFG2_HS_PHY_TYPE_MASK
) >>
423 DWC2_HWCFG2_HS_PHY_TYPE_OFFSET
;
424 uint32_t fval
= (ghwcfg2
& DWC2_HWCFG2_FS_PHY_TYPE_MASK
) >>
425 DWC2_HWCFG2_FS_PHY_TYPE_OFFSET
;
426 if (hval
== 2 && fval
== 1) {
427 usbcfg
|= DWC2_GUSBCFG_ULPI_FSLS
;
428 usbcfg
|= DWC2_GUSBCFG_ULPI_CLK_SUS_M
;
431 if (priv
->hnp_srp_disable
)
432 usbcfg
|= DWC2_GUSBCFG_FORCEHOSTMODE
;
434 writel(usbcfg
, ®s
->gusbcfg
);
436 /* Program the GAHBCFG Register. */
437 switch (readl(®s
->ghwcfg2
) & DWC2_HWCFG2_ARCHITECTURE_MASK
) {
438 case DWC2_HWCFG2_ARCHITECTURE_SLAVE_ONLY
:
440 case DWC2_HWCFG2_ARCHITECTURE_EXT_DMA
:
441 while (brst_sz
> 1) {
442 ahbcfg
|= ahbcfg
+ (1 << DWC2_GAHBCFG_HBURSTLEN_OFFSET
);
443 ahbcfg
&= DWC2_GAHBCFG_HBURSTLEN_MASK
;
447 #ifdef CONFIG_DWC2_DMA_ENABLE
448 ahbcfg
|= DWC2_GAHBCFG_DMAENABLE
;
452 case DWC2_HWCFG2_ARCHITECTURE_INT_DMA
:
453 ahbcfg
|= DWC2_GAHBCFG_HBURSTLEN_INCR4
;
454 #ifdef CONFIG_DWC2_DMA_ENABLE
455 ahbcfg
|= DWC2_GAHBCFG_DMAENABLE
;
460 writel(ahbcfg
, ®s
->gahbcfg
);
462 /* Program the capabilities in GUSBCFG Register */
465 if (!priv
->hnp_srp_disable
)
466 usbcfg
|= DWC2_GUSBCFG_HNPCAP
| DWC2_GUSBCFG_SRPCAP
;
467 #ifdef CONFIG_DWC2_IC_USB_CAP
468 usbcfg
|= DWC2_GUSBCFG_IC_USB_CAP
;
471 setbits_le32(®s
->gusbcfg
, usbcfg
);
475 * Prepares a host channel for transferring packets to/from a specific
476 * endpoint. The HCCHARn register is set up with the characteristics specified
477 * in _hc. Host channel interrupts that may need to be serviced while this
478 * transfer is in progress are enabled.
480 * @param regs Programming view of DWC_otg controller
481 * @param hc Information needed to initialize the host channel
483 static void dwc_otg_hc_init(struct dwc2_core_regs
*regs
, uint8_t hc_num
,
484 struct usb_device
*dev
, uint8_t dev_addr
, uint8_t ep_num
,
485 uint8_t ep_is_in
, uint8_t ep_type
, uint16_t max_packet
)
487 struct dwc2_hc_regs
*hc_regs
= ®s
->hc_regs
[hc_num
];
488 uint32_t hcchar
= (dev_addr
<< DWC2_HCCHAR_DEVADDR_OFFSET
) |
489 (ep_num
<< DWC2_HCCHAR_EPNUM_OFFSET
) |
490 (ep_is_in
<< DWC2_HCCHAR_EPDIR_OFFSET
) |
491 (ep_type
<< DWC2_HCCHAR_EPTYPE_OFFSET
) |
492 (max_packet
<< DWC2_HCCHAR_MPS_OFFSET
);
494 if (dev
->speed
== USB_SPEED_LOW
)
495 hcchar
|= DWC2_HCCHAR_LSPDDEV
;
498 * Program the HCCHARn register with the endpoint characteristics
499 * for the current transfer.
501 writel(hcchar
, &hc_regs
->hcchar
);
503 /* Program the HCSPLIT register, default to no SPLIT */
504 writel(0, &hc_regs
->hcsplt
);
507 static void dwc_otg_hc_init_split(struct dwc2_hc_regs
*hc_regs
,
508 uint8_t hub_devnum
, uint8_t hub_port
)
512 hcsplt
= DWC2_HCSPLT_SPLTENA
;
513 hcsplt
|= hub_devnum
<< DWC2_HCSPLT_HUBADDR_OFFSET
;
514 hcsplt
|= hub_port
<< DWC2_HCSPLT_PRTADDR_OFFSET
;
516 /* Program the HCSPLIT register for SPLITs */
517 writel(hcsplt
, &hc_regs
->hcsplt
);
521 * DWC2 to USB API interface
523 /* Direction: In ; Request: Status */
524 static int dwc_otg_submit_rh_msg_in_status(struct dwc2_core_regs
*regs
,
525 struct usb_device
*dev
, void *buffer
,
526 int txlen
, struct devrequest
*cmd
)
529 uint32_t port_status
= 0;
530 uint32_t port_change
= 0;
534 switch (cmd
->requesttype
& ~USB_DIR_IN
) {
536 *(uint16_t *)buffer
= cpu_to_le16(1);
539 case USB_RECIP_INTERFACE
:
540 case USB_RECIP_ENDPOINT
:
541 *(uint16_t *)buffer
= cpu_to_le16(0);
545 *(uint32_t *)buffer
= cpu_to_le32(0);
548 case USB_RECIP_OTHER
| USB_TYPE_CLASS
:
549 hprt0
= readl(®s
->hprt0
);
550 if (hprt0
& DWC2_HPRT0_PRTCONNSTS
)
551 port_status
|= USB_PORT_STAT_CONNECTION
;
552 if (hprt0
& DWC2_HPRT0_PRTENA
)
553 port_status
|= USB_PORT_STAT_ENABLE
;
554 if (hprt0
& DWC2_HPRT0_PRTSUSP
)
555 port_status
|= USB_PORT_STAT_SUSPEND
;
556 if (hprt0
& DWC2_HPRT0_PRTOVRCURRACT
)
557 port_status
|= USB_PORT_STAT_OVERCURRENT
;
558 if (hprt0
& DWC2_HPRT0_PRTRST
)
559 port_status
|= USB_PORT_STAT_RESET
;
560 if (hprt0
& DWC2_HPRT0_PRTPWR
)
561 port_status
|= USB_PORT_STAT_POWER
;
563 if ((hprt0
& DWC2_HPRT0_PRTSPD_MASK
) == DWC2_HPRT0_PRTSPD_LOW
)
564 port_status
|= USB_PORT_STAT_LOW_SPEED
;
565 else if ((hprt0
& DWC2_HPRT0_PRTSPD_MASK
) ==
566 DWC2_HPRT0_PRTSPD_HIGH
)
567 port_status
|= USB_PORT_STAT_HIGH_SPEED
;
569 if (hprt0
& DWC2_HPRT0_PRTENCHNG
)
570 port_change
|= USB_PORT_STAT_C_ENABLE
;
571 if (hprt0
& DWC2_HPRT0_PRTCONNDET
)
572 port_change
|= USB_PORT_STAT_C_CONNECTION
;
573 if (hprt0
& DWC2_HPRT0_PRTOVRCURRCHNG
)
574 port_change
|= USB_PORT_STAT_C_OVERCURRENT
;
576 *(uint32_t *)buffer
= cpu_to_le32(port_status
|
577 (port_change
<< 16));
581 puts("unsupported root hub command\n");
582 stat
= USB_ST_STALLED
;
585 dev
->act_len
= min(len
, txlen
);
591 /* Direction: In ; Request: Descriptor */
592 static int dwc_otg_submit_rh_msg_in_descriptor(struct usb_device
*dev
,
593 void *buffer
, int txlen
,
594 struct devrequest
*cmd
)
596 unsigned char data
[32];
600 uint16_t wValue
= cpu_to_le16(cmd
->value
);
601 uint16_t wLength
= cpu_to_le16(cmd
->length
);
603 switch (cmd
->requesttype
& ~USB_DIR_IN
) {
605 switch (wValue
& 0xff00) {
606 case 0x0100: /* device descriptor */
607 len
= min3(txlen
, (int)sizeof(root_hub_dev_des
), (int)wLength
);
608 memcpy(buffer
, root_hub_dev_des
, len
);
610 case 0x0200: /* configuration descriptor */
611 len
= min3(txlen
, (int)sizeof(root_hub_config_des
), (int)wLength
);
612 memcpy(buffer
, root_hub_config_des
, len
);
614 case 0x0300: /* string descriptors */
615 switch (wValue
& 0xff) {
617 len
= min3(txlen
, (int)sizeof(root_hub_str_index0
),
619 memcpy(buffer
, root_hub_str_index0
, len
);
622 len
= min3(txlen
, (int)sizeof(root_hub_str_index1
),
624 memcpy(buffer
, root_hub_str_index1
, len
);
629 stat
= USB_ST_STALLED
;
634 /* Root port config, set 1 port and nothing else. */
637 data
[0] = 9; /* min length; */
639 data
[2] = dsc
& RH_A_NDP
;
645 else if (dsc
& RH_A_OCPM
)
648 /* corresponds to data[4-7] */
649 data
[5] = (dsc
& RH_A_POTPGT
) >> 24;
650 data
[7] = dsc
& RH_B_DR
;
655 data
[8] = (dsc
& RH_B_DR
) >> 8;
660 len
= min3(txlen
, (int)data
[0], (int)wLength
);
661 memcpy(buffer
, data
, len
);
664 puts("unsupported root hub command\n");
665 stat
= USB_ST_STALLED
;
668 dev
->act_len
= min(len
, txlen
);
674 /* Direction: In ; Request: Configuration */
675 static int dwc_otg_submit_rh_msg_in_configuration(struct usb_device
*dev
,
676 void *buffer
, int txlen
,
677 struct devrequest
*cmd
)
682 switch (cmd
->requesttype
& ~USB_DIR_IN
) {
684 *(uint8_t *)buffer
= 0x01;
688 puts("unsupported root hub command\n");
689 stat
= USB_ST_STALLED
;
692 dev
->act_len
= min(len
, txlen
);
699 static int dwc_otg_submit_rh_msg_in(struct dwc2_priv
*priv
,
700 struct usb_device
*dev
, void *buffer
,
701 int txlen
, struct devrequest
*cmd
)
703 switch (cmd
->request
) {
704 case USB_REQ_GET_STATUS
:
705 return dwc_otg_submit_rh_msg_in_status(priv
->regs
, dev
, buffer
,
707 case USB_REQ_GET_DESCRIPTOR
:
708 return dwc_otg_submit_rh_msg_in_descriptor(dev
, buffer
,
710 case USB_REQ_GET_CONFIGURATION
:
711 return dwc_otg_submit_rh_msg_in_configuration(dev
, buffer
,
714 puts("unsupported root hub command\n");
715 return USB_ST_STALLED
;
720 static int dwc_otg_submit_rh_msg_out(struct dwc2_priv
*priv
,
721 struct usb_device
*dev
,
722 void *buffer
, int txlen
,
723 struct devrequest
*cmd
)
725 struct dwc2_core_regs
*regs
= priv
->regs
;
728 uint16_t bmrtype_breq
= cmd
->requesttype
| (cmd
->request
<< 8);
729 uint16_t wValue
= cpu_to_le16(cmd
->value
);
731 switch (bmrtype_breq
& ~USB_DIR_IN
) {
732 case (USB_REQ_CLEAR_FEATURE
<< 8) | USB_RECIP_ENDPOINT
:
733 case (USB_REQ_CLEAR_FEATURE
<< 8) | USB_TYPE_CLASS
:
736 case (USB_REQ_CLEAR_FEATURE
<< 8) | USB_RECIP_OTHER
| USB_TYPE_CLASS
:
738 case USB_PORT_FEAT_C_CONNECTION
:
739 setbits_le32(®s
->hprt0
, DWC2_HPRT0_PRTCONNDET
);
744 case (USB_REQ_SET_FEATURE
<< 8) | USB_RECIP_OTHER
| USB_TYPE_CLASS
:
746 case USB_PORT_FEAT_SUSPEND
:
749 case USB_PORT_FEAT_RESET
:
750 clrsetbits_le32(®s
->hprt0
, DWC2_HPRT0_PRTENA
|
751 DWC2_HPRT0_PRTCONNDET
|
752 DWC2_HPRT0_PRTENCHNG
|
753 DWC2_HPRT0_PRTOVRCURRCHNG
,
756 clrbits_le32(®s
->hprt0
, DWC2_HPRT0_PRTRST
);
759 case USB_PORT_FEAT_POWER
:
760 clrsetbits_le32(®s
->hprt0
, DWC2_HPRT0_PRTENA
|
761 DWC2_HPRT0_PRTCONNDET
|
762 DWC2_HPRT0_PRTENCHNG
|
763 DWC2_HPRT0_PRTOVRCURRCHNG
,
767 case USB_PORT_FEAT_ENABLE
:
771 case (USB_REQ_SET_ADDRESS
<< 8):
772 priv
->root_hub_devnum
= wValue
;
774 case (USB_REQ_SET_CONFIGURATION
<< 8):
777 puts("unsupported root hub command\n");
778 stat
= USB_ST_STALLED
;
781 len
= min(len
, txlen
);
789 static int dwc_otg_submit_rh_msg(struct dwc2_priv
*priv
, struct usb_device
*dev
,
790 unsigned long pipe
, void *buffer
, int txlen
,
791 struct devrequest
*cmd
)
795 if (usb_pipeint(pipe
)) {
796 puts("Root-Hub submit IRQ: NOT implemented\n");
800 if (cmd
->requesttype
& USB_DIR_IN
)
801 stat
= dwc_otg_submit_rh_msg_in(priv
, dev
, buffer
, txlen
, cmd
);
803 stat
= dwc_otg_submit_rh_msg_out(priv
, dev
, buffer
, txlen
, cmd
);
810 int wait_for_chhltd(struct dwc2_hc_regs
*hc_regs
, uint32_t *sub
, u8
*toggle
)
813 uint32_t hcint
, hctsiz
;
815 ret
= wait_for_bit_le32(&hc_regs
->hcint
, DWC2_HCINT_CHHLTD
, true,
820 hcint
= readl(&hc_regs
->hcint
);
821 hctsiz
= readl(&hc_regs
->hctsiz
);
822 *sub
= (hctsiz
& DWC2_HCTSIZ_XFERSIZE_MASK
) >>
823 DWC2_HCTSIZ_XFERSIZE_OFFSET
;
824 *toggle
= (hctsiz
& DWC2_HCTSIZ_PID_MASK
) >> DWC2_HCTSIZ_PID_OFFSET
;
826 debug("%s: HCINT=%08x sub=%u toggle=%d\n", __func__
, hcint
, *sub
,
829 if (hcint
& DWC2_HCINT_XFERCOMP
)
832 if (hcint
& (DWC2_HCINT_NAK
| DWC2_HCINT_FRMOVRUN
))
835 debug("%s: Error (HCINT=%08x)\n", __func__
, hcint
);
839 static int dwc2_eptype
[] = {
840 DWC2_HCCHAR_EPTYPE_ISOC
,
841 DWC2_HCCHAR_EPTYPE_INTR
,
842 DWC2_HCCHAR_EPTYPE_CONTROL
,
843 DWC2_HCCHAR_EPTYPE_BULK
,
846 static int transfer_chunk(struct dwc2_hc_regs
*hc_regs
, void *aligned_buffer
,
847 u8
*pid
, int in
, void *buffer
, int num_packets
,
848 int xfer_len
, int *actual_len
, int odd_frame
)
853 debug("%s: chunk: pid %d xfer_len %u pkts %u\n", __func__
,
854 *pid
, xfer_len
, num_packets
);
856 writel((xfer_len
<< DWC2_HCTSIZ_XFERSIZE_OFFSET
) |
857 (num_packets
<< DWC2_HCTSIZ_PKTCNT_OFFSET
) |
858 (*pid
<< DWC2_HCTSIZ_PID_OFFSET
),
863 invalidate_dcache_range(
864 (uintptr_t)aligned_buffer
,
865 (uintptr_t)aligned_buffer
+
866 roundup(xfer_len
, ARCH_DMA_MINALIGN
));
868 memcpy(aligned_buffer
, buffer
, xfer_len
);
870 (uintptr_t)aligned_buffer
,
871 (uintptr_t)aligned_buffer
+
872 roundup(xfer_len
, ARCH_DMA_MINALIGN
));
876 writel(phys_to_bus((unsigned long)aligned_buffer
), &hc_regs
->hcdma
);
878 /* Clear old interrupt conditions for this host channel. */
879 writel(0x3fff, &hc_regs
->hcint
);
881 /* Set host channel enable after all other setup is complete. */
882 clrsetbits_le32(&hc_regs
->hcchar
, DWC2_HCCHAR_MULTICNT_MASK
|
883 DWC2_HCCHAR_CHEN
| DWC2_HCCHAR_CHDIS
|
885 (1 << DWC2_HCCHAR_MULTICNT_OFFSET
) |
886 (odd_frame
<< DWC2_HCCHAR_ODDFRM_OFFSET
) |
889 ret
= wait_for_chhltd(hc_regs
, &sub
, pid
);
896 invalidate_dcache_range((unsigned long)aligned_buffer
,
897 (unsigned long)aligned_buffer
+
898 roundup(xfer_len
, ARCH_DMA_MINALIGN
));
900 memcpy(buffer
, aligned_buffer
, xfer_len
);
902 *actual_len
= xfer_len
;
907 int chunk_msg(struct dwc2_priv
*priv
, struct usb_device
*dev
,
908 unsigned long pipe
, u8
*pid
, int in
, void *buffer
, int len
)
910 struct dwc2_core_regs
*regs
= priv
->regs
;
911 struct dwc2_hc_regs
*hc_regs
= ®s
->hc_regs
[DWC2_HC_CHANNEL
];
912 struct dwc2_host_regs
*host_regs
= ®s
->host_regs
;
913 int devnum
= usb_pipedevice(pipe
);
914 int ep
= usb_pipeendpoint(pipe
);
915 int max
= usb_maxpacket(dev
, pipe
);
916 int eptype
= dwc2_eptype
[usb_pipetype(pipe
)];
920 int complete_split
= 0;
922 uint32_t num_packets
;
923 int stop_transfer
= 0;
924 uint32_t max_xfer_len
;
925 int ssplit_frame_num
= 0;
927 debug("%s: msg: pipe %lx pid %d in %d len %d\n", __func__
, pipe
, *pid
,
930 max_xfer_len
= CONFIG_DWC2_MAX_PACKET_COUNT
* max
;
931 if (max_xfer_len
> CONFIG_DWC2_MAX_TRANSFER_SIZE
)
932 max_xfer_len
= CONFIG_DWC2_MAX_TRANSFER_SIZE
;
933 if (max_xfer_len
> DWC2_DATA_BUF_SIZE
)
934 max_xfer_len
= DWC2_DATA_BUF_SIZE
;
936 /* Make sure that max_xfer_len is a multiple of max packet size. */
937 num_packets
= max_xfer_len
/ max
;
938 max_xfer_len
= num_packets
* max
;
940 /* Initialize channel */
941 dwc_otg_hc_init(regs
, DWC2_HC_CHANNEL
, dev
, devnum
, ep
, in
,
944 /* Check if the target is a FS/LS device behind a HS hub */
945 if (dev
->speed
!= USB_SPEED_HIGH
) {
948 uint32_t hprt0
= readl(®s
->hprt0
);
949 if ((hprt0
& DWC2_HPRT0_PRTSPD_MASK
) ==
950 DWC2_HPRT0_PRTSPD_HIGH
) {
951 usb_find_usb2_hub_address_port(dev
, &hub_addr
,
953 dwc_otg_hc_init_split(hc_regs
, hub_addr
, hub_port
);
965 xfer_len
= len
- done
;
967 if (xfer_len
> max_xfer_len
)
968 xfer_len
= max_xfer_len
;
969 else if (xfer_len
> max
)
970 num_packets
= (xfer_len
+ max
- 1) / max
;
975 setbits_le32(&hc_regs
->hcsplt
, DWC2_HCSPLT_COMPSPLT
);
977 clrbits_le32(&hc_regs
->hcsplt
, DWC2_HCSPLT_COMPSPLT
);
979 if (eptype
== DWC2_HCCHAR_EPTYPE_INTR
) {
980 int uframe_num
= readl(&host_regs
->hfnum
);
981 if (!(uframe_num
& 0x1))
985 ret
= transfer_chunk(hc_regs
, priv
->aligned_buffer
, pid
,
986 in
, (char *)buffer
+ done
, num_packets
,
987 xfer_len
, &actual_len
, odd_frame
);
989 hcint
= readl(&hc_regs
->hcint
);
990 if (complete_split
) {
992 if (hcint
& DWC2_HCINT_NYET
) {
994 int frame_num
= DWC2_HFNUM_MAX_FRNUM
&
995 readl(&host_regs
->hfnum
);
996 if (((frame_num
- ssplit_frame_num
) &
997 DWC2_HFNUM_MAX_FRNUM
) > 4)
1001 } else if (do_split
) {
1002 if (hcint
& DWC2_HCINT_ACK
) {
1003 ssplit_frame_num
= DWC2_HFNUM_MAX_FRNUM
&
1004 readl(&host_regs
->hfnum
);
1013 if (actual_len
< xfer_len
)
1018 /* Transactions are done when when either all data is transferred or
1019 * there is a short transfer. In case of a SPLIT make sure the CSPLIT
1022 } while (((done
< len
) && !stop_transfer
) || complete_split
);
1024 writel(0, &hc_regs
->hcintmsk
);
1025 writel(0xFFFFFFFF, &hc_regs
->hcint
);
1028 dev
->act_len
= done
;
1033 /* U-Boot USB transmission interface */
1034 int _submit_bulk_msg(struct dwc2_priv
*priv
, struct usb_device
*dev
,
1035 unsigned long pipe
, void *buffer
, int len
)
1037 int devnum
= usb_pipedevice(pipe
);
1038 int ep
= usb_pipeendpoint(pipe
);
1041 if ((devnum
>= MAX_DEVICE
) || (devnum
== priv
->root_hub_devnum
)) {
1046 if (usb_pipein(pipe
))
1047 pid
= &priv
->in_data_toggle
[devnum
][ep
];
1049 pid
= &priv
->out_data_toggle
[devnum
][ep
];
1051 return chunk_msg(priv
, dev
, pipe
, pid
, usb_pipein(pipe
), buffer
, len
);
1054 static int _submit_control_msg(struct dwc2_priv
*priv
, struct usb_device
*dev
,
1055 unsigned long pipe
, void *buffer
, int len
,
1056 struct devrequest
*setup
)
1058 int devnum
= usb_pipedevice(pipe
);
1061 /* For CONTROL endpoint pid should start with DATA1 */
1062 int status_direction
;
1064 if (devnum
== priv
->root_hub_devnum
) {
1066 dev
->speed
= USB_SPEED_HIGH
;
1067 return dwc_otg_submit_rh_msg(priv
, dev
, pipe
, buffer
, len
,
1072 pid
= DWC2_HC_PID_SETUP
;
1074 ret
= chunk_msg(priv
, dev
, pipe
, &pid
, 0, setup
, 8);
1075 } while (ret
== -EAGAIN
);
1082 pid
= DWC2_HC_PID_DATA1
;
1084 ret
= chunk_msg(priv
, dev
, pipe
, &pid
, usb_pipein(pipe
),
1086 act_len
+= dev
->act_len
;
1087 buffer
+= dev
->act_len
;
1088 len
-= dev
->act_len
;
1089 } while (ret
== -EAGAIN
);
1092 status_direction
= usb_pipeout(pipe
);
1094 /* No-data CONTROL always ends with an IN transaction */
1095 status_direction
= 1;
1099 pid
= DWC2_HC_PID_DATA1
;
1101 ret
= chunk_msg(priv
, dev
, pipe
, &pid
, status_direction
,
1102 priv
->status_buffer
, 0);
1103 } while (ret
== -EAGAIN
);
1107 dev
->act_len
= act_len
;
1112 int _submit_int_msg(struct dwc2_priv
*priv
, struct usb_device
*dev
,
1113 unsigned long pipe
, void *buffer
, int len
, int interval
,
1116 unsigned long timeout
;
1119 /* FIXME: what is interval? */
1121 timeout
= get_timer(0) + USB_TIMEOUT_MS(pipe
);
1123 if (get_timer(0) > timeout
) {
1124 dev_err(dev
, "Timeout poll on interrupt endpoint\n");
1127 ret
= _submit_bulk_msg(priv
, dev
, pipe
, buffer
, len
);
1128 if ((ret
!= -EAGAIN
) || nonblock
)
1133 static int dwc2_reset(struct udevice
*dev
)
1136 struct dwc2_priv
*priv
= dev_get_priv(dev
);
1138 ret
= reset_get_bulk(dev
, &priv
->resets
);
1140 dev_warn(dev
, "Can't get reset: %d\n", ret
);
1141 /* Return 0 if error due to !CONFIG_DM_RESET and reset
1142 * DT property is not present.
1144 if (ret
== -ENOENT
|| ret
== -ENOTSUPP
)
1150 ret
= reset_deassert_bulk(&priv
->resets
);
1152 reset_release_bulk(&priv
->resets
);
1153 dev_err(dev
, "Failed to reset: %d\n", ret
);
1160 static int dwc2_init_common(struct udevice
*dev
, struct dwc2_priv
*priv
)
1162 struct dwc2_core_regs
*regs
= priv
->regs
;
1167 ret
= dwc2_reset(dev
);
1171 snpsid
= readl(®s
->gsnpsid
);
1172 dev_info(dev
, "Core Release: %x.%03x\n",
1173 snpsid
>> 12 & 0xf, snpsid
& 0xfff);
1175 if ((snpsid
& DWC2_SNPSID_DEVID_MASK
) != DWC2_SNPSID_DEVID_VER_2xx
&&
1176 (snpsid
& DWC2_SNPSID_DEVID_MASK
) != DWC2_SNPSID_DEVID_VER_3xx
) {
1177 dev_info(dev
, "SNPSID invalid (not DWC2 OTG device): %08x\n",
1182 #ifdef CONFIG_DWC2_PHY_ULPI_EXT_VBUS
1188 dwc_otg_core_init(priv
);
1189 dwc_otg_core_host_init(dev
, regs
);
1191 clrsetbits_le32(®s
->hprt0
, DWC2_HPRT0_PRTENA
|
1192 DWC2_HPRT0_PRTCONNDET
| DWC2_HPRT0_PRTENCHNG
|
1193 DWC2_HPRT0_PRTOVRCURRCHNG
,
1196 clrbits_le32(®s
->hprt0
, DWC2_HPRT0_PRTENA
| DWC2_HPRT0_PRTCONNDET
|
1197 DWC2_HPRT0_PRTENCHNG
| DWC2_HPRT0_PRTOVRCURRCHNG
|
1200 for (i
= 0; i
< MAX_DEVICE
; i
++) {
1201 for (j
= 0; j
< MAX_ENDPOINT
; j
++) {
1202 priv
->in_data_toggle
[i
][j
] = DWC2_HC_PID_DATA0
;
1203 priv
->out_data_toggle
[i
][j
] = DWC2_HC_PID_DATA0
;
1208 * Add a 1 second delay here. This gives the host controller
1209 * a bit time before the comminucation with the USB devices
1210 * is started (the bus is scanned) and fixes the USB detection
1211 * problems with some problematic USB keys.
1213 if (readl(®s
->gintsts
) & DWC2_GINTSTS_CURMODE_HOST
)
1219 static void dwc2_uninit_common(struct dwc2_core_regs
*regs
)
1221 /* Put everything in reset. */
1222 clrsetbits_le32(®s
->hprt0
, DWC2_HPRT0_PRTENA
|
1223 DWC2_HPRT0_PRTCONNDET
| DWC2_HPRT0_PRTENCHNG
|
1224 DWC2_HPRT0_PRTOVRCURRCHNG
,
1228 #if !CONFIG_IS_ENABLED(DM_USB)
1229 int submit_control_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
1230 int len
, struct devrequest
*setup
)
1232 return _submit_control_msg(&local
, dev
, pipe
, buffer
, len
, setup
);
1235 int submit_bulk_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
1238 return _submit_bulk_msg(&local
, dev
, pipe
, buffer
, len
);
1241 int submit_int_msg(struct usb_device
*dev
, unsigned long pipe
, void *buffer
,
1242 int len
, int interval
, bool nonblock
)
1244 return _submit_int_msg(&local
, dev
, pipe
, buffer
, len
, interval
,
1248 /* U-Boot USB control interface */
1249 int usb_lowlevel_init(int index
, enum usb_init_type init
, void **controller
)
1251 struct dwc2_priv
*priv
= &local
;
1253 memset(priv
, '\0', sizeof(*priv
));
1254 priv
->root_hub_devnum
= 0;
1255 priv
->regs
= (struct dwc2_core_regs
*)CONFIG_USB_DWC2_REG_ADDR
;
1256 priv
->aligned_buffer
= aligned_buffer_addr
;
1257 priv
->status_buffer
= status_buffer_addr
;
1259 /* board-dependant init */
1260 if (board_usb_init(index
, USB_INIT_HOST
))
1263 return dwc2_init_common(NULL
, priv
);
1266 int usb_lowlevel_stop(int index
)
1268 dwc2_uninit_common(local
.regs
);
1274 #if CONFIG_IS_ENABLED(DM_USB)
1275 static int dwc2_submit_control_msg(struct udevice
*dev
, struct usb_device
*udev
,
1276 unsigned long pipe
, void *buffer
, int length
,
1277 struct devrequest
*setup
)
1279 struct dwc2_priv
*priv
= dev_get_priv(dev
);
1281 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__
,
1282 dev
->name
, udev
, udev
->dev
->name
, udev
->portnr
);
1284 return _submit_control_msg(priv
, udev
, pipe
, buffer
, length
, setup
);
1287 static int dwc2_submit_bulk_msg(struct udevice
*dev
, struct usb_device
*udev
,
1288 unsigned long pipe
, void *buffer
, int length
)
1290 struct dwc2_priv
*priv
= dev_get_priv(dev
);
1292 debug("%s: dev='%s', udev=%p\n", __func__
, dev
->name
, udev
);
1294 return _submit_bulk_msg(priv
, udev
, pipe
, buffer
, length
);
1297 static int dwc2_submit_int_msg(struct udevice
*dev
, struct usb_device
*udev
,
1298 unsigned long pipe
, void *buffer
, int length
,
1299 int interval
, bool nonblock
)
1301 struct dwc2_priv
*priv
= dev_get_priv(dev
);
1303 debug("%s: dev='%s', udev=%p\n", __func__
, dev
->name
, udev
);
1305 return _submit_int_msg(priv
, udev
, pipe
, buffer
, length
, interval
,
1309 static int dwc2_usb_ofdata_to_platdata(struct udevice
*dev
)
1311 struct dwc2_priv
*priv
= dev_get_priv(dev
);
1314 addr
= dev_read_addr(dev
);
1315 if (addr
== FDT_ADDR_T_NONE
)
1317 priv
->regs
= (struct dwc2_core_regs
*)addr
;
1319 priv
->oc_disable
= dev_read_bool(dev
, "disable-over-current");
1320 priv
->hnp_srp_disable
= dev_read_bool(dev
, "hnp-srp-disable");
1325 static int dwc2_usb_probe(struct udevice
*dev
)
1327 struct dwc2_priv
*priv
= dev_get_priv(dev
);
1328 struct usb_bus_priv
*bus_priv
= dev_get_uclass_priv(dev
);
1330 bus_priv
->desc_before_addr
= true;
1332 return dwc2_init_common(dev
, priv
);
1335 static int dwc2_usb_remove(struct udevice
*dev
)
1337 struct dwc2_priv
*priv
= dev_get_priv(dev
);
1340 ret
= dwc_vbus_supply_exit(dev
);
1344 dwc2_uninit_common(priv
->regs
);
1346 reset_release_bulk(&priv
->resets
);
1351 struct dm_usb_ops dwc2_usb_ops
= {
1352 .control
= dwc2_submit_control_msg
,
1353 .bulk
= dwc2_submit_bulk_msg
,
1354 .interrupt
= dwc2_submit_int_msg
,
1357 static const struct udevice_id dwc2_usb_ids
[] = {
1358 { .compatible
= "brcm,bcm2835-usb" },
1359 { .compatible
= "brcm,bcm2708-usb" },
1360 { .compatible
= "snps,dwc2" },
1364 U_BOOT_DRIVER(usb_dwc2
) = {
1367 .of_match
= dwc2_usb_ids
,
1368 .ofdata_to_platdata
= dwc2_usb_ofdata_to_platdata
,
1369 .probe
= dwc2_usb_probe
,
1370 .remove
= dwc2_usb_remove
,
1371 .ops
= &dwc2_usb_ops
,
1372 .priv_auto_alloc_size
= sizeof(struct dwc2_priv
),
1373 .flags
= DM_FLAG_ALLOC_PRIV_DMA
,