1 /* SPDX-License-Identifier: GPL-2.0-only */
2 /*
3  * Copyright (C) 2014-2017 Broadcom
4  */
5 
6 #ifndef _USB_BRCM_COMMON_INIT_H
7 #define _USB_BRCM_COMMON_INIT_H
8 
9 #include <linux/regmap.h>
10 
11 #define USB_CTLR_MODE_HOST 0
12 #define USB_CTLR_MODE_DEVICE 1
13 #define USB_CTLR_MODE_DRD 2
14 #define USB_CTLR_MODE_TYPEC_PD 3
15 
16 enum brcmusb_reg_sel {
17 	BRCM_REGS_CTRL = 0,
18 	BRCM_REGS_XHCI_EC,
19 	BRCM_REGS_XHCI_GBL,
20 	BRCM_REGS_USB_PHY,
21 	BRCM_REGS_USB_MDIO,
22 	BRCM_REGS_BDC_EC,
23 	BRCM_REGS_MAX
24 };
25 
26 #define USB_CTRL_REG(base, reg)	((void __iomem *)base + USB_CTRL_##reg)
27 #define USB_XHCI_EC_REG(base, reg) ((void __iomem *)base + USB_XHCI_EC_##reg)
28 #define USB_CTRL_MASK(reg, field) \
29 	USB_CTRL_##reg##_##field##_MASK
30 #define USB_CTRL_SET(base, reg, field)	\
31 	brcm_usb_ctrl_set(USB_CTRL_REG(base, reg),	\
32 			  USB_CTRL_##reg##_##field##_MASK)
33 #define USB_CTRL_UNSET(base, reg, field)	\
34 	brcm_usb_ctrl_unset(USB_CTRL_REG(base, reg),		\
35 			    USB_CTRL_##reg##_##field##_MASK)
36 
37 struct  brcm_usb_init_params;
38 
39 struct brcm_usb_init_ops {
40 	void (*init_ipp)(struct brcm_usb_init_params *params);
41 	void (*init_common)(struct brcm_usb_init_params *params);
42 	void (*init_eohci)(struct brcm_usb_init_params *params);
43 	void (*init_xhci)(struct brcm_usb_init_params *params);
44 	void (*uninit_common)(struct brcm_usb_init_params *params);
45 	void (*uninit_eohci)(struct brcm_usb_init_params *params);
46 	void (*uninit_xhci)(struct brcm_usb_init_params *params);
47 	int  (*get_dual_select)(struct brcm_usb_init_params *params);
48 	void (*set_dual_select)(struct brcm_usb_init_params *params, int mode);
49 };
50 
51 struct  brcm_usb_init_params {
52 	void __iomem *regs[BRCM_REGS_MAX];
53 	int ioc;
54 	int ipp;
55 	int mode;
56 	u32 family_id;
57 	u32 product_id;
58 	int selected_family;
59 	const char *family_name;
60 	const u32 *usb_reg_bits_map;
61 	const struct brcm_usb_init_ops *ops;
62 	struct regmap *syscon_piarbctl;
63 	bool wake_enabled;
64 	bool suspend_with_clocks;
65 };
66 
67 void brcm_usb_dvr_init_7445(struct brcm_usb_init_params *params);
68 void brcm_usb_dvr_init_7216(struct brcm_usb_init_params *params);
69 void brcm_usb_dvr_init_7211b0(struct brcm_usb_init_params *params);
70 
71 static inline u32 brcm_usb_readl(void __iomem *addr)
72 {
73 	/*
74 	 * MIPS endianness is configured by boot strap, which also reverses all
75 	 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
76 	 * endian I/O).
77 	 *
78 	 * Other architectures (e.g., ARM) either do not support big endian, or
79 	 * else leave I/O in little endian mode.
80 	 */
81 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(__BIG_ENDIAN))
82 		return __raw_readl(addr);
83 	else
84 		return readl_relaxed(addr);
85 }
86 
87 static inline void brcm_usb_writel(u32 val, void __iomem *addr)
88 {
89 	/* See brcmnand_readl() comments */
90 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(__BIG_ENDIAN))
91 		__raw_writel(val, addr);
92 	else
93 		writel_relaxed(val, addr);
94 }
95 
96 static inline void brcm_usb_ctrl_unset(void __iomem *reg, u32 mask)
97 {
98 	brcm_usb_writel(brcm_usb_readl(reg) & ~(mask), reg);
99 };
100 
101 static inline void brcm_usb_ctrl_set(void __iomem *reg, u32 mask)
102 {
103 	brcm_usb_writel(brcm_usb_readl(reg) | (mask), reg);
104 };
105 
106 static inline void brcm_usb_init_ipp(struct brcm_usb_init_params *ini)
107 {
108 	if (ini->ops->init_ipp)
109 		ini->ops->init_ipp(ini);
110 }
111 
112 static inline void brcm_usb_init_common(struct brcm_usb_init_params *ini)
113 {
114 	if (ini->ops->init_common)
115 		ini->ops->init_common(ini);
116 }
117 
118 static inline void brcm_usb_init_eohci(struct brcm_usb_init_params *ini)
119 {
120 	if (ini->ops->init_eohci)
121 		ini->ops->init_eohci(ini);
122 }
123 
124 static inline void brcm_usb_init_xhci(struct brcm_usb_init_params *ini)
125 {
126 	if (ini->ops->init_xhci)
127 		ini->ops->init_xhci(ini);
128 }
129 
130 static inline void brcm_usb_uninit_common(struct brcm_usb_init_params *ini)
131 {
132 	if (ini->ops->uninit_common)
133 		ini->ops->uninit_common(ini);
134 }
135 
136 static inline void brcm_usb_uninit_eohci(struct brcm_usb_init_params *ini)
137 {
138 	if (ini->ops->uninit_eohci)
139 		ini->ops->uninit_eohci(ini);
140 }
141 
142 static inline void brcm_usb_uninit_xhci(struct brcm_usb_init_params *ini)
143 {
144 	if (ini->ops->uninit_xhci)
145 		ini->ops->uninit_xhci(ini);
146 }
147 
148 static inline int brcm_usb_get_dual_select(struct brcm_usb_init_params *ini)
149 {
150 	if (ini->ops->get_dual_select)
151 		return ini->ops->get_dual_select(ini);
152 	return 0;
153 }
154 
155 static inline void brcm_usb_set_dual_select(struct brcm_usb_init_params *ini,
156 	int mode)
157 {
158 	if (ini->ops->set_dual_select)
159 		ini->ops->set_dual_select(ini, mode);
160 }
161 
162 #endif /* _USB_BRCM_COMMON_INIT_H */
163