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);
49 };
50 
51 struct  brcm_usb_init_params {
52 	void __iomem *regs[BRCM_REGS_MAX];
53 	int ioc;
54 	int ipp;
55 	int supported_port_modes;
56 	int port_mode;
57 	u32 family_id;
58 	u32 product_id;
59 	int selected_family;
60 	const char *family_name;
61 	const u32 *usb_reg_bits_map;
62 	const struct brcm_usb_init_ops *ops;
63 	struct regmap *syscon_piarbctl;
64 	bool wake_enabled;
65 };
66 
67 void brcm_usb_dvr_init_4908(struct brcm_usb_init_params *params);
68 void brcm_usb_dvr_init_7445(struct brcm_usb_init_params *params);
69 void brcm_usb_dvr_init_7216(struct brcm_usb_init_params *params);
70 void brcm_usb_dvr_init_7211b0(struct brcm_usb_init_params *params);
71 
72 static inline u32 brcm_usb_readl(void __iomem *addr)
73 {
74 	/*
75 	 * MIPS endianness is configured by boot strap, which also reverses all
76 	 * bus endianness (i.e., big-endian CPU + big endian bus ==> native
77 	 * endian I/O).
78 	 *
79 	 * Other architectures (e.g., ARM) either do not support big endian, or
80 	 * else leave I/O in little endian mode.
81 	 */
82 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
83 		return __raw_readl(addr);
84 	else
85 		return readl_relaxed(addr);
86 }
87 
88 static inline void brcm_usb_writel(u32 val, void __iomem *addr)
89 {
90 	/* See brcmnand_readl() comments */
91 	if (IS_ENABLED(CONFIG_MIPS) && IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
92 		__raw_writel(val, addr);
93 	else
94 		writel_relaxed(val, addr);
95 }
96 
97 static inline void brcm_usb_ctrl_unset(void __iomem *reg, u32 mask)
98 {
99 	brcm_usb_writel(brcm_usb_readl(reg) & ~(mask), reg);
100 };
101 
102 static inline void brcm_usb_ctrl_set(void __iomem *reg, u32 mask)
103 {
104 	brcm_usb_writel(brcm_usb_readl(reg) | (mask), reg);
105 };
106 
107 static inline void brcm_usb_init_ipp(struct brcm_usb_init_params *ini)
108 {
109 	if (ini->ops->init_ipp)
110 		ini->ops->init_ipp(ini);
111 }
112 
113 static inline void brcm_usb_init_common(struct brcm_usb_init_params *ini)
114 {
115 	if (ini->ops->init_common)
116 		ini->ops->init_common(ini);
117 }
118 
119 static inline void brcm_usb_init_eohci(struct brcm_usb_init_params *ini)
120 {
121 	if (ini->ops->init_eohci)
122 		ini->ops->init_eohci(ini);
123 }
124 
125 static inline void brcm_usb_init_xhci(struct brcm_usb_init_params *ini)
126 {
127 	if (ini->ops->init_xhci)
128 		ini->ops->init_xhci(ini);
129 }
130 
131 static inline void brcm_usb_uninit_common(struct brcm_usb_init_params *ini)
132 {
133 	if (ini->ops->uninit_common)
134 		ini->ops->uninit_common(ini);
135 }
136 
137 static inline void brcm_usb_uninit_eohci(struct brcm_usb_init_params *ini)
138 {
139 	if (ini->ops->uninit_eohci)
140 		ini->ops->uninit_eohci(ini);
141 }
142 
143 static inline void brcm_usb_uninit_xhci(struct brcm_usb_init_params *ini)
144 {
145 	if (ini->ops->uninit_xhci)
146 		ini->ops->uninit_xhci(ini);
147 }
148 
149 static inline int brcm_usb_get_dual_select(struct brcm_usb_init_params *ini)
150 {
151 	if (ini->ops->get_dual_select)
152 		return ini->ops->get_dual_select(ini);
153 	return 0;
154 }
155 
156 static inline void brcm_usb_set_dual_select(struct brcm_usb_init_params *ini)
157 {
158 	if (ini->ops->set_dual_select)
159 		ini->ops->set_dual_select(ini);
160 }
161 
162 #endif /* _USB_BRCM_COMMON_INIT_H */
163