Skip to content

Commit efb286d

Browse files
jfischer-nofabiobaltieri
authored andcommitted
drivers: udc_dwc2: rework vendor quirks
Rework and rename vendor quirks to better reflect where they intended to be called. Number of quirks probably not final and will be trimmed later. Signed-off-by: Johann Fischer <[email protected]>
1 parent 67cdccc commit efb286d

File tree

3 files changed

+82
-31
lines changed

3 files changed

+82
-31
lines changed

drivers/usb/udc/udc_dwc2.c

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
#include "udc_common.h"
88
#include "udc_dwc2.h"
9-
#include "udc_dwc2_vendor_quirks.h"
109

1110
#include <string.h>
1211
#include <stdio.h>
@@ -22,6 +21,7 @@
2221

2322
#include <zephyr/logging/log.h>
2423
LOG_MODULE_REGISTER(udc_dwc2, CONFIG_UDC_DRIVER_LOG_LEVEL);
24+
#include "udc_dwc2_vendor_quirks.h"
2525

2626
enum dwc2_drv_event_type {
2727
/* Trigger next transfer, must not be used for control OUT */
@@ -904,9 +904,7 @@ static void udc_dwc2_isr_handler(const struct device *dev)
904904
}
905905
}
906906

907-
if (config->quirks != NULL && config->quirks->irq_clear != NULL) {
908-
config->quirks->irq_clear(dev);
909-
}
907+
(void)dwc2_quirk_irq_clear(dev);
910908
}
911909

912910
static int udc_dwc2_ep_enqueue(const struct device *dev,
@@ -1523,18 +1521,21 @@ static int udc_dwc2_enable(const struct device *dev)
15231521
struct usb_dwc2_reg *const base = dwc2_get_base(dev);
15241522
int err;
15251523

1524+
err = dwc2_quirk_pre_enable(dev);
1525+
if (err) {
1526+
LOG_ERR("Quirk pre enable failed %d", err);
1527+
return err;
1528+
}
1529+
15261530
err = udc_dwc2_init_controller(dev);
15271531
if (err) {
15281532
return err;
15291533
}
15301534

1531-
/* Call vendor-specific function to enable peripheral */
1532-
if (config->quirks != NULL && config->quirks->pwr_on != NULL) {
1533-
LOG_DBG("Enable vendor power");
1534-
err = config->quirks->pwr_on(dev);
1535-
if (err) {
1536-
return err;
1537-
}
1535+
err = dwc2_quirk_post_enable(dev);
1536+
if (err) {
1537+
LOG_ERR("Quirk post enable failed %d", err);
1538+
return err;
15381539
}
15391540

15401541
/* Enable global interrupt */
@@ -1553,6 +1554,7 @@ static int udc_dwc2_disable(const struct device *dev)
15531554
const struct udc_dwc2_config *const config = dev->config;
15541555
struct usb_dwc2_reg *const base = dwc2_get_base(dev);
15551556
mem_addr_t dctl_reg = (mem_addr_t)&base->dctl;
1557+
int err;
15561558

15571559
/* Enable soft disconnect */
15581560
sys_set_bits(dctl_reg, USB_DWC2_DCTL_SFTDISCON);
@@ -1571,27 +1573,38 @@ static int udc_dwc2_disable(const struct device *dev)
15711573
return -EIO;
15721574
}
15731575

1576+
err = dwc2_quirk_disable(dev);
1577+
if (err) {
1578+
LOG_ERR("Quirk disable failed %d", err);
1579+
return err;
1580+
}
1581+
15741582
return 0;
15751583
}
15761584

15771585
static int udc_dwc2_init(const struct device *dev)
15781586
{
1579-
const struct udc_dwc2_config *const config = dev->config;
15801587
int ret;
15811588

1582-
if (config->quirks != NULL && config->quirks->clk_enable != NULL) {
1583-
LOG_DBG("Enable vendor clock");
1584-
ret = config->quirks->clk_enable(dev);
1585-
if (ret) {
1586-
return ret;
1587-
}
1589+
ret = dwc2_quirk_init(dev);
1590+
if (ret) {
1591+
LOG_ERR("Quirk init failed %d", ret);
1592+
return ret;
15881593
}
15891594

15901595
return dwc2_init_pinctrl(dev);
15911596
}
15921597

15931598
static int udc_dwc2_shutdown(const struct device *dev)
15941599
{
1600+
int ret;
1601+
1602+
ret = dwc2_quirk_shutdown(dev);
1603+
if (ret) {
1604+
LOG_ERR("Quirk shutdown failed %d", ret);
1605+
return ret;
1606+
}
1607+
15951608
return 0;
15961609
}
15971610

drivers/usb/udc/udc_dwc2.h

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,17 @@
1414

1515
/* Vendor quirks per driver instance */
1616
struct dwc2_vendor_quirks {
17-
int (*clk_enable)(const struct device *dev);
18-
int (*clk_disable)(const struct device *dev);
19-
int (*pwr_on)(const struct device *dev);
20-
int (*pwr_off)(const struct device *dev);
17+
/* Called at the beginning of udc_dwc2_init() */
18+
int (*init)(const struct device *dev);
19+
/* Called on udc_dwc2_enable() before the controller is initialized */
20+
int (*pre_enable)(const struct device *dev);
21+
/* Called on udc_dwc2_enable() after the controller is initialized */
22+
int (*post_enable)(const struct device *dev);
23+
/* Called at the end of udc_dwc2_disable() */
24+
int (*disable)(const struct device *dev);
25+
/* Called at the end of udc_dwc2_shutdown() */
26+
int (*shutdown)(const struct device *dev);
27+
/* Called at the end of IRQ handling */
2128
int (*irq_clear)(const struct device *dev);
2229
};
2330

@@ -37,4 +44,24 @@ struct udc_dwc2_config {
3744
void (*irq_disable_func)(const struct device *dev);
3845
};
3946

47+
#define DWC2_QUIRK_FUNC_DEFINE(fname) \
48+
static inline int dwc2_quirk_##fname(const struct device *dev) \
49+
{ \
50+
const struct udc_dwc2_config *const config = dev->config; \
51+
struct dwc2_vendor_quirks *quirks = config->quirks; \
52+
\
53+
if (quirks != NULL && config->quirks->fname != NULL) { \
54+
return quirks->fname(dev); \
55+
} \
56+
\
57+
return 0; \
58+
}
59+
60+
DWC2_QUIRK_FUNC_DEFINE(init)
61+
DWC2_QUIRK_FUNC_DEFINE(pre_enable)
62+
DWC2_QUIRK_FUNC_DEFINE(post_enable)
63+
DWC2_QUIRK_FUNC_DEFINE(disable)
64+
DWC2_QUIRK_FUNC_DEFINE(shutdown)
65+
DWC2_QUIRK_FUNC_DEFINE(irq_clear)
66+
4067
#endif /* ZEPHYR_DRIVERS_USB_UDC_DWC2_H */

drivers/usb/udc/udc_dwc2_vendor_quirks.h

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@
1111

1212
#include <stdint.h>
1313
#include <zephyr/device.h>
14+
15+
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32f4_fsotg)
16+
1417
#include <zephyr/sys/sys_io.h>
1518
#include <zephyr/drivers/clock_control/stm32_clock_control.h>
16-
1719
#include <usb_dwc2_hw.h>
1820

19-
#if DT_HAS_COMPAT_STATUS_OKAY(st_stm32f4_fsotg)
20-
2121
struct usb_dw_stm32_clk {
2222
const struct device *const dev;
2323
const struct stm32_pclken *const pclken;
@@ -26,7 +26,7 @@ struct usb_dw_stm32_clk {
2626

2727
#define DT_DRV_COMPAT snps_dwc2
2828

29-
static inline int clk_enable_stm32f4_fsotg(const struct usb_dw_stm32_clk *const clk)
29+
static inline int stm32f4_fsotg_enable_clk(const struct usb_dw_stm32_clk *const clk)
3030
{
3131
int ret;
3232

@@ -59,7 +59,7 @@ static inline int clk_enable_stm32f4_fsotg(const struct usb_dw_stm32_clk *const
5959
return clock_control_on(clk->dev, (void *)&clk->pclken[0]);
6060
}
6161

62-
static inline int pwr_on_stm32f4_fsotg(const struct device *dev)
62+
static inline int stm32f4_fsotg_enable_phy(const struct device *dev)
6363
{
6464
const struct udc_dwc2_config *const config = dev->config;
6565
mem_addr_t ggpio_reg = (mem_addr_t)&config->base->ggpio;
@@ -69,6 +69,16 @@ static inline int pwr_on_stm32f4_fsotg(const struct device *dev)
6969
return 0;
7070
}
7171

72+
static inline int stm32f4_fsotg_disable_phy(const struct device *dev)
73+
{
74+
const struct udc_dwc2_config *const config = dev->config;
75+
mem_addr_t ggpio_reg = (mem_addr_t)&config->base->ggpio;
76+
77+
sys_clear_bits(ggpio_reg, USB_DWC2_GGPIO_STM32_PWRDWN | USB_DWC2_GGPIO_STM32_VBDEN);
78+
79+
return 0;
80+
}
81+
7282
#define QUIRK_STM32F4_FSOTG_DEFINE(n) \
7383
static const struct stm32_pclken pclken_##n[] = STM32_DT_INST_CLOCKS(n);\
7484
\
@@ -78,14 +88,15 @@ static inline int pwr_on_stm32f4_fsotg(const struct device *dev)
7888
.pclken_len = DT_INST_NUM_CLOCKS(n), \
7989
}; \
8090
\
81-
static int clk_enable_stm32f4_fsotg_##n(const struct device *dev) \
91+
static int stm32f4_fsotg_enable_clk_##n(const struct device *dev) \
8292
{ \
83-
return clk_enable_stm32f4_fsotg(&stm32f4_clk_##n); \
93+
return stm32f4_fsotg_enable_clk(&stm32f4_clk_##n); \
8494
} \
8595
\
8696
struct dwc2_vendor_quirks dwc2_vendor_quirks_##n = { \
87-
.clk_enable = clk_enable_stm32f4_fsotg_##n, \
88-
.pwr_on = pwr_on_stm32f4_fsotg, \
97+
.pre_enable = stm32f4_fsotg_enable_clk_##n, \
98+
.post_enable = stm32f4_fsotg_enable_phy, \
99+
.disable = stm32f4_fsotg_disable_phy, \
89100
.irq_clear = NULL, \
90101
};
91102

0 commit comments

Comments
 (0)