Skip to content

Commit 85e6c06

Browse files
author
Josuah Demangeon
committed
flatten the init sequence to just one function
Signed-off-by: Josuah Demangeon <[email protected]>
1 parent 17d7d47 commit 85e6c06

File tree

1 file changed

+17
-17
lines changed

1 file changed

+17
-17
lines changed

drivers/usb/uhc/uhc_dwc2.c

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -376,43 +376,43 @@ static inline void dwc2_hal_toggle_power(struct usb_dwc2_reg *const dwc2, bool p
376376
sys_write32(hprt & (~USB_DWC2_HPRT_W1C_MSK), (mem_addr_t)&dwc2->hprt);
377377
}
378378

379-
int dwc2_core_reset(const struct device *dev)
379+
int dwc2_core_reset(const struct device *dev, k_timeout_t timeout)
380380
{
381381
const struct uhc_dwc2_config *const config = dev->config;
382+
k_timepoint_t timepoint = sys_timepoint_calc(timeout);
382383
struct usb_dwc2_reg *const dwc2 = config->base;
383384

384-
const unsigned int csr_timeout_us = 10000UL;
385-
uint32_t cnt = 0UL;
385+
/* Software reset won't finish without PHY clock */
386+
if (uhc_dwc2_quirk_is_phy_clk_off(dev)) {
387+
LOG_ERR("PHY clock is turned off, cannot reset");
388+
return -EIO;
389+
}
386390

387391
/* Check AHB master idle state */
388-
while (!(sys_read32((mem_addr_t)&dwc2->grstctl) & USB_DWC2_GRSTCTL_AHBIDLE)) {
389-
k_busy_wait(1);
390-
391-
if (++cnt > csr_timeout_us) {
392+
while ((sys_read32((mem_addr_t)&dwc2->grstctl) & USB_DWC2_GRSTCTL_AHBIDLE) == 0) {
393+
if (sys_timepoint_expired(timepoint)) {
392394
LOG_ERR("Wait for AHB idle timeout, GRSTCTL 0x%08x",
393395
sys_read32((mem_addr_t)&dwc2->grstctl));
394396
return -EIO;
395397
}
398+
399+
k_busy_wait(1);
396400
}
397401

398402
/* Apply Core Soft Reset */
399403
sys_write32(USB_DWC2_GRSTCTL_CSFTRST, (mem_addr_t)&dwc2->grstctl);
400404

401-
cnt = 0UL;
402-
do {
403-
if (++cnt > csr_timeout_us) {
405+
/* Wait for reset to complete */
406+
while ((sys_read32((mem_addr_t)&dwc2->grstctl) & USB_DWC2_GRSTCTL_CSFTRST) != 0 &&
407+
(sys_read32((mem_addr_t)&dwc2->grstctl) & USB_DWC2_GRSTCTL_CSFTRSTDONE) == 0) {
408+
if (sys_timepoint_expired(timepoint)) {
404409
LOG_ERR("Wait for CSR done timeout, GRSTCTL 0x%08x",
405410
sys_read32((mem_addr_t)&dwc2->grstctl));
406411
return -EIO;
407412
}
408413

409414
k_busy_wait(1);
410-
if (uhc_dwc2_quirk_is_phy_clk_off(dev)) {
411-
/* Software reset won't finish without PHY clock */
412-
return -EIO;
413-
}
414-
} while (sys_read32((mem_addr_t)&dwc2->grstctl) & USB_DWC2_GRSTCTL_CSFTRST &&
415-
!(sys_read32((mem_addr_t)&dwc2->grstctl) & USB_DWC2_GRSTCTL_CSFTRSTDONE));
415+
}
416416

417417
/* CSFTRSTDONE is W1C so the write must have the bit set to clear it */
418418
sys_clear_bits((mem_addr_t)&dwc2->grstctl, USB_DWC2_GRSTCTL_CSFTRST);
@@ -1905,7 +1905,7 @@ static int uhc_dwc2_init(const struct device *dev)
19051905
}
19061906

19071907
/* Reset core after selecting PHY */
1908-
ret = dwc2_core_reset(dev);
1908+
ret = dwc2_core_reset(dev, K_MSEC(10));
19091909
if (ret) {
19101910
LOG_ERR("DWC2 core reset failed after PHY init: %d", ret);
19111911
return ret;

0 commit comments

Comments
 (0)