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