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 	/* Called after hibernation entry sequence */
34 	int (*post_hibernation_entry)(const struct device *dev);
35 	/* Called before hibernation exit sequence */
36 	int (*pre_hibernation_exit)(const struct device *dev);
37 };
38 
39 /* Driver configuration per instance */
40 struct udc_dwc2_config {
41 	size_t num_in_eps;
42 	size_t num_out_eps;
43 	struct udc_ep_config *ep_cfg_in;
44 	struct udc_ep_config *ep_cfg_out;
45 	struct usb_dwc2_reg *const base;
46 	/* Pointer to pin control configuration or NULL */
47 	struct pinctrl_dev_config *const pcfg;
48 	/* Pointer to vendor quirks or NULL */
49 	struct dwc2_vendor_quirks *const quirks;
50 	void (*make_thread)(const struct device *dev);
51 	void (*irq_enable_func)(const struct device *dev);
52 	void (*irq_disable_func)(const struct device *dev);
53 	uint32_t ghwcfg1;
54 	uint32_t ghwcfg2;
55 	uint32_t ghwcfg4;
56 };
57 
58 #define DWC2_QUIRK_FUNC_DEFINE(fname)						\
59 static inline int dwc2_quirk_##fname(const struct device *dev)			\
60 {										\
61 	const struct udc_dwc2_config *const config = dev->config;		\
62 	struct dwc2_vendor_quirks *quirks = config->quirks;			\
63 										\
64 	if (quirks != NULL && config->quirks->fname != NULL) {			\
65 		return quirks->fname(dev);					\
66 	}									\
67 										\
68 	return 0;								\
69 }
70 
71 DWC2_QUIRK_FUNC_DEFINE(init)
72 DWC2_QUIRK_FUNC_DEFINE(pre_enable)
73 DWC2_QUIRK_FUNC_DEFINE(post_enable)
74 DWC2_QUIRK_FUNC_DEFINE(disable)
75 DWC2_QUIRK_FUNC_DEFINE(shutdown)
76 DWC2_QUIRK_FUNC_DEFINE(irq_clear)
77 DWC2_QUIRK_FUNC_DEFINE(caps)
78 DWC2_QUIRK_FUNC_DEFINE(is_phy_clk_off)
79 DWC2_QUIRK_FUNC_DEFINE(post_hibernation_entry)
80 DWC2_QUIRK_FUNC_DEFINE(pre_hibernation_exit)
81 
82 #endif /* ZEPHYR_DRIVERS_USB_UDC_DWC2_H */
83