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 	const 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 #include "udc_dwc2_vendor_quirks.h"
59 
60 #define UDC_DWC2_VENDOR_QUIRK_GET(n)						\
61 	COND_CODE_1(DT_NODE_VENDOR_HAS_IDX(DT_DRV_INST(n), 1),			\
62 		    (&dwc2_vendor_quirks_##n),					\
63 		    (NULL))
64 
65 #define DWC2_QUIRK_FUNC_DEFINE(fname)						\
66 static inline int dwc2_quirk_##fname(const struct device *dev)			\
67 {										\
68 	const struct udc_dwc2_config *const config = dev->config;		\
69 	const struct dwc2_vendor_quirks *const quirks =				\
70 		COND_CODE_1(IS_EQ(DT_NUM_INST_STATUS_OKAY(snps_dwc2), 1),	\
71 			(UDC_DWC2_VENDOR_QUIRK_GET(0); ARG_UNUSED(config);),	\
72 			(config->quirks;))					\
73 										\
74 	if (quirks != NULL && quirks->fname != NULL) {				\
75 		return quirks->fname(dev);					\
76 	}									\
77 										\
78 	return 0;								\
79 }
80 
81 DWC2_QUIRK_FUNC_DEFINE(init)
82 DWC2_QUIRK_FUNC_DEFINE(pre_enable)
83 DWC2_QUIRK_FUNC_DEFINE(post_enable)
84 DWC2_QUIRK_FUNC_DEFINE(disable)
85 DWC2_QUIRK_FUNC_DEFINE(shutdown)
86 DWC2_QUIRK_FUNC_DEFINE(irq_clear)
87 DWC2_QUIRK_FUNC_DEFINE(caps)
88 DWC2_QUIRK_FUNC_DEFINE(is_phy_clk_off)
89 DWC2_QUIRK_FUNC_DEFINE(post_hibernation_entry)
90 DWC2_QUIRK_FUNC_DEFINE(pre_hibernation_exit)
91 
92 #endif /* ZEPHYR_DRIVERS_USB_UDC_DWC2_H */
93