Skip to content

Commit aa033fb

Browse files
author
Josuah Demangeon
committed
flatten the init sequence to just one function
1 parent 17d7d47 commit aa033fb

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

drivers/usb/uhc/uhc_dwc2.c

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -376,43 +376,42 @@ 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+
return -EIO;
388+
}
386389

387390
/* 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) {
391+
while ((sys_read32((mem_addr_t)&dwc2->grstctl) & USB_DWC2_GRSTCTL_AHBIDLE) == 0) {
392+
if (sys_timepoint_expired(timepoint)) {
392393
LOG_ERR("Wait for AHB idle timeout, GRSTCTL 0x%08x",
393394
sys_read32((mem_addr_t)&dwc2->grstctl));
394395
return -EIO;
395396
}
397+
398+
k_busy_wait(1);
396399
}
397400

398401
/* Apply Core Soft Reset */
399402
sys_write32(USB_DWC2_GRSTCTL_CSFTRST, (mem_addr_t)&dwc2->grstctl);
400403

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

409413
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));
414+
}
416415

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

19071906
/* Reset core after selecting PHY */
1908-
ret = dwc2_core_reset(dev);
1907+
ret = dwc2_core_reset(dev, K_MSEC(10));
19091908
if (ret) {
19101909
LOG_ERR("DWC2 core reset failed after PHY init: %d", ret);
19111910
return ret;

0 commit comments

Comments
 (0)