1 /* SPDX-License-Identifier: GPL-2.0
2  * Microchip switch driver common header
3  *
4  * Copyright (C) 2017-2019 Microchip Technology Inc.
5  */
6 
7 #ifndef __KSZ_COMMON_H
8 #define __KSZ_COMMON_H
9 
10 void ksz_port_cleanup(struct ksz_device *dev, int port);
11 void ksz_update_port_member(struct ksz_device *dev, int port);
12 void ksz_init_mib_timer(struct ksz_device *dev);
13 
14 /* Common DSA access functions */
15 
16 int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg);
17 int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val);
18 void ksz_adjust_link(struct dsa_switch *ds, int port,
19 		     struct phy_device *phydev);
20 int ksz_sset_count(struct dsa_switch *ds, int port, int sset);
21 void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf);
22 int ksz_port_bridge_join(struct dsa_switch *ds, int port,
23 			 struct net_device *br);
24 void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
25 			   struct net_device *br);
26 void ksz_port_fast_age(struct dsa_switch *ds, int port);
27 int ksz_port_vlan_prepare(struct dsa_switch *ds, int port,
28 			  const struct switchdev_obj_port_vlan *vlan);
29 int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
30 		      void *data);
31 int ksz_port_mdb_prepare(struct dsa_switch *ds, int port,
32 			 const struct switchdev_obj_port_mdb *mdb);
33 void ksz_port_mdb_add(struct dsa_switch *ds, int port,
34 		      const struct switchdev_obj_port_mdb *mdb);
35 int ksz_port_mdb_del(struct dsa_switch *ds, int port,
36 		     const struct switchdev_obj_port_mdb *mdb);
37 int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy);
38 void ksz_disable_port(struct dsa_switch *ds, int port);
39 
40 /* Common register access functions */
41 
42 static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
43 {
44 	int ret;
45 
46 	mutex_lock(&dev->reg_mutex);
47 	ret = dev->ops->read8(dev, reg, val);
48 	mutex_unlock(&dev->reg_mutex);
49 
50 	return ret;
51 }
52 
53 static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
54 {
55 	int ret;
56 
57 	mutex_lock(&dev->reg_mutex);
58 	ret = dev->ops->read16(dev, reg, val);
59 	mutex_unlock(&dev->reg_mutex);
60 
61 	return ret;
62 }
63 
64 static inline int ksz_read24(struct ksz_device *dev, u32 reg, u32 *val)
65 {
66 	int ret;
67 
68 	mutex_lock(&dev->reg_mutex);
69 	ret = dev->ops->read24(dev, reg, val);
70 	mutex_unlock(&dev->reg_mutex);
71 
72 	return ret;
73 }
74 
75 static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
76 {
77 	int ret;
78 
79 	mutex_lock(&dev->reg_mutex);
80 	ret = dev->ops->read32(dev, reg, val);
81 	mutex_unlock(&dev->reg_mutex);
82 
83 	return ret;
84 }
85 
86 static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
87 {
88 	int ret;
89 
90 	mutex_lock(&dev->reg_mutex);
91 	ret = dev->ops->write8(dev, reg, value);
92 	mutex_unlock(&dev->reg_mutex);
93 
94 	return ret;
95 }
96 
97 static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
98 {
99 	int ret;
100 
101 	mutex_lock(&dev->reg_mutex);
102 	ret = dev->ops->write16(dev, reg, value);
103 	mutex_unlock(&dev->reg_mutex);
104 
105 	return ret;
106 }
107 
108 static inline int ksz_write24(struct ksz_device *dev, u32 reg, u32 value)
109 {
110 	int ret;
111 
112 	mutex_lock(&dev->reg_mutex);
113 	ret = dev->ops->write24(dev, reg, value);
114 	mutex_unlock(&dev->reg_mutex);
115 
116 	return ret;
117 }
118 
119 static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
120 {
121 	int ret;
122 
123 	mutex_lock(&dev->reg_mutex);
124 	ret = dev->ops->write32(dev, reg, value);
125 	mutex_unlock(&dev->reg_mutex);
126 
127 	return ret;
128 }
129 
130 static inline int ksz_get(struct ksz_device *dev, u32 reg, void *data,
131 			  size_t len)
132 {
133 	int ret;
134 
135 	mutex_lock(&dev->reg_mutex);
136 	ret = dev->ops->get(dev, reg, data, len);
137 	mutex_unlock(&dev->reg_mutex);
138 
139 	return ret;
140 }
141 
142 static inline int ksz_set(struct ksz_device *dev, u32 reg, void *data,
143 			  size_t len)
144 {
145 	int ret;
146 
147 	mutex_lock(&dev->reg_mutex);
148 	ret = dev->ops->set(dev, reg, data, len);
149 	mutex_unlock(&dev->reg_mutex);
150 
151 	return ret;
152 }
153 
154 static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
155 			      u8 *data)
156 {
157 	ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
158 }
159 
160 static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
161 			       u16 *data)
162 {
163 	ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
164 }
165 
166 static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
167 			       u32 *data)
168 {
169 	ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
170 }
171 
172 static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
173 			       u8 data)
174 {
175 	ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
176 }
177 
178 static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
179 				u16 data)
180 {
181 	ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset), data);
182 }
183 
184 static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
185 				u32 data)
186 {
187 	ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data);
188 }
189 
190 static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
191 {
192 	u8 data;
193 
194 	ksz_read8(dev, addr, &data);
195 	if (set)
196 		data |= bits;
197 	else
198 		data &= ~bits;
199 	ksz_write8(dev, addr, data);
200 }
201 
202 static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
203 			 bool set)
204 {
205 	u32 addr;
206 	u8 data;
207 
208 	addr = dev->dev_ops->get_port_addr(port, offset);
209 	ksz_read8(dev, addr, &data);
210 
211 	if (set)
212 		data |= bits;
213 	else
214 		data &= ~bits;
215 
216 	ksz_write8(dev, addr, data);
217 }
218 
219 struct ksz_poll_ctx {
220 	struct ksz_device *dev;
221 	int port;
222 	int offset;
223 };
224 
225 static inline u32 ksz_pread32_poll(struct ksz_poll_ctx *ctx)
226 {
227 	u32 data;
228 
229 	ksz_pread32(ctx->dev, ctx->port, ctx->offset, &data);
230 	return data;
231 }
232 
233 #endif
234