Skip to content

Commit 17d7d47

Browse files
author
Josuah Demangeon
committed
flatten most initialization in one init() function
1 parent 966315a commit 17d7d47

File tree

1 file changed

+48
-76
lines changed

1 file changed

+48
-76
lines changed

drivers/usb/uhc/uhc_dwc2.c

Lines changed: 48 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,12 @@ static int uhc_dwc2_power_on(const struct device *dev)
587587
return -EINVAL;
588588
}
589589

590-
static inline int uhc_dwc2_config_phy(const struct device *dev)
590+
static inline void uhc_dwc2_init_gusbcfg(const struct device *dev)
591591
{
592592
const struct uhc_dwc2_config *const config = dev->config;
593593
struct usb_dwc2_reg *const dwc2 = config->base;
594594

595595
/* Init PHY based on the speed */
596-
int ret;
597-
598596
if (UHC_DWC2_HSPHYTYPE(config) != 0) {
599597
uint32_t gusbcfg = sys_read32((mem_addr_t)&dwc2->gusbcfg);
600598

@@ -625,52 +623,12 @@ static inline int uhc_dwc2_config_phy(const struct device *dev)
625623
}
626624
}
627625
sys_write32(gusbcfg, (mem_addr_t)&dwc2->gusbcfg);
628-
629-
ret = uhc_dwc2_quirk_phy_pre_select(dev);
630-
if (ret) {
631-
LOG_ERR("Quirk PHY pre select failed %d", ret);
632-
return ret;
633-
}
634-
635-
/* Reset core after selecting PHY */
636-
ret = dwc2_core_reset(dev);
637-
if (ret) {
638-
LOG_ERR("DWC2 core reset failed after PHY init: %d", ret);
639-
return ret;
640-
}
641-
642-
ret = uhc_dwc2_quirk_phy_post_select(dev);
643-
if (ret) {
644-
LOG_ERR("Quirk PHY post select failed %d", ret);
645-
return ret;
646-
}
647626
} else {
648627
sys_set_bits((mem_addr_t)&dwc2->gusbcfg, USB_DWC2_GUSBCFG_PHYSEL_USB11);
649-
650-
ret = uhc_dwc2_quirk_phy_pre_select(dev);
651-
if (ret) {
652-
LOG_ERR("Quirk PHY pre select failed %d", ret);
653-
return ret;
654-
}
655-
656-
/* Reset core after selecting PHY */
657-
ret = dwc2_core_reset(dev);
658-
if (ret) {
659-
LOG_ERR("DWC2 core reset failed after PHY init: %d", ret);
660-
return ret;
661-
}
662-
663-
ret = uhc_dwc2_quirk_phy_post_select(dev);
664-
if (ret) {
665-
LOG_ERR("Quirk PHY post select failed %d", ret);
666-
return ret;
667-
}
668628
}
669-
670-
return ret;
671629
}
672630

673-
static inline void uhc_dwc2_set_defaults(const struct device *dev)
631+
static inline void uhc_dwc2_init_gahbcfg(const struct device *dev)
674632
{
675633
const struct uhc_dwc2_config *const config = dev->config;
676634
struct usb_dwc2_reg *const dwc2 = config->base;
@@ -713,35 +671,6 @@ static inline void uhc_dwc2_set_defaults(const struct device *dev)
713671
sys_set_bits((mem_addr_t)&dwc2->gahbcfg, USB_DWC2_GAHBCFG_GLBINTRMASK);
714672
}
715673

716-
static int uhc_dwc2_init_controller(const struct device *dev)
717-
{
718-
const struct uhc_dwc2_config *const config = dev->config;
719-
struct uhc_dwc2_data *priv = uhc_get_private(dev);
720-
int ret;
721-
722-
/* Pre-calculate FIFO settings */
723-
uhc_dwc2_config_fifo_fixed_dma(dev);
724-
725-
/* Config PHY */
726-
ret = uhc_dwc2_config_phy(dev);
727-
if (ret) {
728-
LOG_ERR("Failed to configure DWC2 PHY: %d", ret);
729-
return ret;
730-
}
731-
732-
/* Set defaults */
733-
uhc_dwc2_set_defaults(dev);
734-
735-
/* Update the port state and flags */
736-
priv->port_state = UHC_PORT_STATE_NOT_POWERED;
737-
priv->last_event = UHC_PORT_EVENT_NONE;
738-
739-
/* TODO: Clear all the flags and channels */
740-
priv->pending_channel_intrs_msk = 0;
741-
742-
return 0;
743-
}
744-
745674
static enum uhc_port_event uhc_dwc2_decode_hprt(const struct device *dev,
746675
enum uhc_dwc2_core_event core_event)
747676
{
@@ -1459,6 +1388,8 @@ static inline int uhc_dwc2_port_reset(const struct device *dev)
14591388
return ret;
14601389
}
14611390

1391+
static int uhc_dwc2_init(const struct device *dev);
1392+
14621393
/*
14631394
* Port recovery is necessary when the port is in an error state and needs to be reset.
14641395
*/
@@ -1478,7 +1409,7 @@ static inline int uhc_dwc2_port_recovery(const struct device *dev)
14781409
}
14791410

14801411
/* Init controller */
1481-
ret = uhc_dwc2_init_controller(dev);
1412+
ret = uhc_dwc2_init(dev);
14821413
if (ret) {
14831414
LOG_ERR("Failed to init controller: %d", ret);
14841415
return ret;
@@ -1919,6 +1850,7 @@ static int uhc_dwc2_preinit(const struct device *dev)
19191850
static int uhc_dwc2_init(const struct device *dev)
19201851
{
19211852
const struct uhc_dwc2_config *const config = dev->config;
1853+
struct uhc_dwc2_data *priv = uhc_get_private(dev);
19221854
struct usb_dwc2_reg *const dwc2 = config->base;
19231855
uint32_t reg;
19241856
int ret;
@@ -1929,6 +1861,8 @@ static int uhc_dwc2_init(const struct device *dev)
19291861
return ret;
19301862
}
19311863

1864+
/* 1. Read hardware configuration registers */
1865+
19321866
reg = sys_read32((mem_addr_t)&dwc2->gsnpsid);
19331867
if (reg != config->gsnpsid) {
19341868
LOG_ERR("Unexpected GSNPSID 0x%08x instead of 0x%08x", reg, config->gsnpsid);
@@ -1964,12 +1898,50 @@ static int uhc_dwc2_init(const struct device *dev)
19641898
return -ENOTSUP;
19651899
}
19661900

1967-
ret = uhc_dwc2_init_controller(dev);
1901+
ret = uhc_dwc2_quirk_phy_pre_select(dev);
1902+
if (ret) {
1903+
LOG_ERR("Quirk PHY pre select failed %d", ret);
1904+
return ret;
1905+
}
1906+
1907+
/* Reset core after selecting PHY */
1908+
ret = dwc2_core_reset(dev);
19681909
if (ret) {
1969-
LOG_ERR("Failed to initialize the USB controller");
1910+
LOG_ERR("DWC2 core reset failed after PHY init: %d", ret);
19701911
return ret;
19711912
}
19721913

1914+
ret = uhc_dwc2_quirk_phy_post_select(dev);
1915+
if (ret) {
1916+
LOG_ERR("Quirk PHY post select failed %d", ret);
1917+
return ret;
1918+
}
1919+
1920+
/* Pre-calculate FIFO settings */
1921+
uhc_dwc2_config_fifo_fixed_dma(dev);
1922+
1923+
/* 2. Program the GAHBCFG register */
1924+
uhc_dwc2_init_gahbcfg(dev);
1925+
1926+
/* 3. Disable RX FIFO level interrupts for the time of the configuration */
1927+
/* TODO */
1928+
1929+
/* 4. Configure the reference clock */
1930+
/* TODO */
1931+
1932+
/* 5. Program the GUSBCFG register */
1933+
uhc_dwc2_init_gusbcfg(dev);
1934+
1935+
/* 6. Disable OTG and mode-mismatch interrupts */
1936+
/* TODO */
1937+
1938+
/* Update the port state and flags */
1939+
priv->port_state = UHC_PORT_STATE_NOT_POWERED;
1940+
priv->last_event = UHC_PORT_EVENT_NONE;
1941+
1942+
/* TODO: Clear all the flags and channels */
1943+
priv->pending_channel_intrs_msk = 0;
1944+
19731945
return 0;
19741946
}
19751947

0 commit comments

Comments
 (0)