1 /*
2  * Copyright (c) 2023 Nordic Semiconductor ASA
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef ZEPHYR_DRIVERS_USB_UDC_DWC2_H
8 #define ZEPHYR_DRIVERS_USB_UDC_DWC2_H
9 
10 #include <stdint.h>
11 #include <zephyr/device.h>
12 #include <zephyr/drivers/usb/udc.h>
13 #include <usb_dwc2_hw.h>
14 
15 /* Vendor quirks per driver instance */
16 struct dwc2_vendor_quirks {
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 */
28 	int (*irq_clear)(const struct device *dev);
29 	/* Called on driver pre-init */
30 	int (*caps)(const struct device *dev);
31 	/* Called while waiting for bits that require PHY to be clocked */
32 	int (*is_phy_clk_off)(const struct device *dev);
33 };
34 
35 /* Driver configuration per instance */
36 struct udc_dwc2_config {
37 	size_t num_in_eps;
38 	size_t num_out_eps;
39 	struct udc_ep_config *ep_cfg_in;
40 	struct udc_ep_config *ep_cfg_out;
41 	struct usb_dwc2_reg *const base;
42 	/* Pointer to pin control configuration or NULL */
43 	struct pinctrl_dev_config *const pcfg;
44 	/* Pointer to vendor quirks or NULL */
45 	struct dwc2_vendor_quirks *const quirks;
46 	void (*make_thread)(const struct device *dev);
47 	void (*irq_enable_func)(const struct device *dev);
48 	void (*irq_disable_func)(const struct device *dev);
49 	uint32_t ghwcfg1;
50 	uint32_t ghwcfg2;
51 	uint32_t ghwcfg4;
52 };
53 
54 #define DWC2_QUIRK_FUNC_DEFINE(fname)						\
55 static inline int dwc2_quirk_##fname(const struct device *dev)			\
56 {										\
57 	const struct udc_dwc2_config *const config = dev->config;		\
58 	struct dwc2_vendor_quirks *quirks = config->quirks;			\
59 										\
60 	if (quirks != NULL && config->quirks->fname != NULL) {			\
61 		return quirks->fname(dev);					\
62 	}									\
63 										\
64 	return 0;								\
65 }
66 
67 DWC2_QUIRK_FUNC_DEFINE(init)
68 DWC2_QUIRK_FUNC_DEFINE(pre_enable)
69 DWC2_QUIRK_FUNC_DEFINE(post_enable)
70 DWC2_QUIRK_FUNC_DEFINE(disable)
71 DWC2_QUIRK_FUNC_DEFINE(shutdown)
72 DWC2_QUIRK_FUNC_DEFINE(irq_clear)
73 DWC2_QUIRK_FUNC_DEFINE(caps)
74 DWC2_QUIRK_FUNC_DEFINE(is_phy_clk_off)
75 
76 #endif /* ZEPHYR_DRIVERS_USB_UDC_DWC2_H */
77