xref: /linux/drivers/net/dsa/b53/b53_priv.h (revision 0ce92d548b44649a8de706f9bb9e74a4ed2f18a7)
1 /*
2  * B53 common definitions
3  *
4  * Copyright (C) 2011-2013 Jonas Gorski <jogo@openwrt.org>
5  *
6  * Permission to use, copy, modify, and/or distribute this software for any
7  * purpose with or without fee is hereby granted, provided that the above
8  * copyright notice and this permission notice appear in all copies.
9  *
10  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #ifndef __B53_PRIV_H
20 #define __B53_PRIV_H
21 
22 #include <linux/kernel.h>
23 #include <linux/mutex.h>
24 #include <linux/phylink.h>
25 #include <linux/etherdevice.h>
26 #include <net/dsa.h>
27 
28 #include "b53_regs.h"
29 
30 struct b53_device;
31 struct net_device;
32 
33 struct b53_io_ops {
34 	int (*read8)(struct b53_device *dev, u8 page, u8 reg, u8 *value);
35 	int (*read16)(struct b53_device *dev, u8 page, u8 reg, u16 *value);
36 	int (*read32)(struct b53_device *dev, u8 page, u8 reg, u32 *value);
37 	int (*read48)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
38 	int (*read64)(struct b53_device *dev, u8 page, u8 reg, u64 *value);
39 	int (*write8)(struct b53_device *dev, u8 page, u8 reg, u8 value);
40 	int (*write16)(struct b53_device *dev, u8 page, u8 reg, u16 value);
41 	int (*write32)(struct b53_device *dev, u8 page, u8 reg, u32 value);
42 	int (*write48)(struct b53_device *dev, u8 page, u8 reg, u64 value);
43 	int (*write64)(struct b53_device *dev, u8 page, u8 reg, u64 value);
44 	int (*phy_read16)(struct b53_device *dev, int addr, int reg, u16 *value);
45 	int (*phy_write16)(struct b53_device *dev, int addr, int reg, u16 value);
46 	int (*irq_enable)(struct b53_device *dev, int port);
47 	void (*irq_disable)(struct b53_device *dev, int port);
48 	void (*phylink_get_caps)(struct b53_device *dev, int port,
49 				 struct phylink_config *config);
50 	struct phylink_pcs *(*phylink_mac_select_pcs)(struct b53_device *dev,
51 						      int port,
52 						      phy_interface_t interface);
53 	u8 (*serdes_map_lane)(struct b53_device *dev, int port);
54 	void (*serdes_link_set)(struct b53_device *dev, int port,
55 				unsigned int mode, phy_interface_t interface,
56 				bool link_up);
57 };
58 
59 #define B53_INVALID_LANE	0xff
60 
61 enum {
62 	BCM4908_DEVICE_ID = 0x4908,
63 	BCM5325_DEVICE_ID = 0x25,
64 	BCM5365_DEVICE_ID = 0x65,
65 	BCM5389_DEVICE_ID = 0x89,
66 	BCM5395_DEVICE_ID = 0x95,
67 	BCM5397_DEVICE_ID = 0x97,
68 	BCM5398_DEVICE_ID = 0x98,
69 	BCM53101_DEVICE_ID = 0x53101,
70 	BCM53115_DEVICE_ID = 0x53115,
71 	BCM53125_DEVICE_ID = 0x53125,
72 	BCM53128_DEVICE_ID = 0x53128,
73 	BCM63XX_DEVICE_ID = 0x6300,
74 	BCM63268_DEVICE_ID = 0x63268,
75 	BCM53010_DEVICE_ID = 0x53010,
76 	BCM53011_DEVICE_ID = 0x53011,
77 	BCM53012_DEVICE_ID = 0x53012,
78 	BCM53018_DEVICE_ID = 0x53018,
79 	BCM53019_DEVICE_ID = 0x53019,
80 	BCM58XX_DEVICE_ID = 0x5800,
81 	BCM583XX_DEVICE_ID = 0x58300,
82 	BCM7445_DEVICE_ID = 0x7445,
83 	BCM7278_DEVICE_ID = 0x7278,
84 	BCM53134_DEVICE_ID = 0x5075,
85 };
86 
87 enum b53_variant_id {
88 	B53_VARIANT_NONE = 0,
89 	B53_VARIANT_5325E,
90 	B53_VARIANT_5325M,
91 };
92 
93 struct b53_pcs {
94 	struct phylink_pcs pcs;
95 	struct b53_device *dev;
96 	u8 lane;
97 };
98 
99 #define B53_N_PORTS	9
100 #define B53_N_PORTS_25	6
101 #define B53_N_PCS	2
102 
103 struct b53_port {
104 	u16		vlan_ctl_mask;
105 	u16		pvid;
106 	struct ethtool_keee eee;
107 };
108 
109 struct b53_vlan {
110 	u16 members;
111 	u16 untag;
112 	bool valid;
113 };
114 
115 struct b53_device {
116 	struct dsa_switch *ds;
117 	struct b53_platform_data *pdata;
118 	const char *name;
119 
120 	struct mutex reg_mutex;
121 	struct mutex stats_mutex;
122 	struct mutex arl_mutex;
123 	const struct b53_io_ops *ops;
124 
125 	/* chip specific data */
126 	u32 chip_id;
127 	enum b53_variant_id variant_id;
128 	u8 core_rev;
129 	u8 vta_regs[3];
130 	u8 duplex_reg;
131 	u8 jumbo_pm_reg;
132 	u8 jumbo_size_reg;
133 	int reset_gpio;
134 	u8 num_arl_bins;
135 	u16 num_arl_buckets;
136 	enum dsa_tag_protocol tag_protocol;
137 
138 	/* used ports mask */
139 	u16 enabled_ports;
140 	unsigned int imp_port;
141 
142 	/* connect specific data */
143 	u8 current_page;
144 	struct device *dev;
145 	u8 serdes_lane;
146 
147 	/* Master MDIO bus we got probed from */
148 	struct mii_bus *bus;
149 
150 	void *priv;
151 
152 	/* run time configuration */
153 	bool enable_jumbo;
154 
155 	unsigned int num_vlans;
156 	struct b53_vlan *vlans;
157 	bool vlan_enabled;
158 	bool vlan_filtering;
159 	unsigned int num_ports;
160 	struct b53_port *ports;
161 
162 	struct b53_pcs pcs[B53_N_PCS];
163 };
164 
165 #define b53_for_each_port(dev, i) \
166 	for (i = 0; i < B53_N_PORTS; i++) \
167 		if (dev->enabled_ports & BIT(i))
168 
169 
170 static inline int is5325(struct b53_device *dev)
171 {
172 	return dev->chip_id == BCM5325_DEVICE_ID;
173 }
174 
175 static inline int is5325e(struct b53_device *dev)
176 {
177 	return is5325(dev) &&
178 		dev->variant_id == B53_VARIANT_5325E;
179 }
180 
181 static inline int is5325m(struct b53_device *dev)
182 {
183 	return is5325(dev) &&
184 		dev->variant_id == B53_VARIANT_5325M;
185 }
186 
187 static inline int is5365(struct b53_device *dev)
188 {
189 #ifdef CONFIG_BCM47XX
190 	return dev->chip_id == BCM5365_DEVICE_ID;
191 #else
192 	return 0;
193 #endif
194 }
195 
196 static inline int is5397_98(struct b53_device *dev)
197 {
198 	return dev->chip_id == BCM5397_DEVICE_ID ||
199 		dev->chip_id == BCM5398_DEVICE_ID;
200 }
201 
202 static inline int is539x(struct b53_device *dev)
203 {
204 	return dev->chip_id == BCM5395_DEVICE_ID ||
205 		dev->chip_id == BCM5397_DEVICE_ID ||
206 		dev->chip_id == BCM5398_DEVICE_ID;
207 }
208 
209 static inline int is531x5(struct b53_device *dev)
210 {
211 	return dev->chip_id == BCM53115_DEVICE_ID ||
212 		dev->chip_id == BCM53125_DEVICE_ID ||
213 		dev->chip_id == BCM53101_DEVICE_ID ||
214 		dev->chip_id == BCM53128_DEVICE_ID ||
215 		dev->chip_id == BCM53134_DEVICE_ID;
216 }
217 
218 static inline int is63xx(struct b53_device *dev)
219 {
220 	return dev->chip_id == BCM63XX_DEVICE_ID ||
221 		dev->chip_id == BCM63268_DEVICE_ID;
222 }
223 
224 static inline int is63268(struct b53_device *dev)
225 {
226 	return dev->chip_id == BCM63268_DEVICE_ID;
227 }
228 
229 static inline int is5301x(struct b53_device *dev)
230 {
231 	return dev->chip_id == BCM53010_DEVICE_ID ||
232 		dev->chip_id == BCM53011_DEVICE_ID ||
233 		dev->chip_id == BCM53012_DEVICE_ID ||
234 		dev->chip_id == BCM53018_DEVICE_ID ||
235 		dev->chip_id == BCM53019_DEVICE_ID;
236 }
237 
238 static inline int is58xx(struct b53_device *dev)
239 {
240 	return dev->chip_id == BCM58XX_DEVICE_ID ||
241 		dev->chip_id == BCM583XX_DEVICE_ID ||
242 		dev->chip_id == BCM7445_DEVICE_ID ||
243 		dev->chip_id == BCM7278_DEVICE_ID ||
244 		dev->chip_id == BCM53134_DEVICE_ID;
245 }
246 
247 #define B53_63XX_RGMII0	4
248 #define B53_CPU_PORT_25	5
249 #define B53_CPU_PORT	8
250 
251 static inline unsigned int b53_max_arl_entries(struct b53_device *dev)
252 {
253 	return dev->num_arl_buckets * dev->num_arl_bins;
254 }
255 
256 struct b53_device *b53_switch_alloc(struct device *base,
257 				    const struct b53_io_ops *ops,
258 				    void *priv);
259 
260 int b53_switch_detect(struct b53_device *dev);
261 
262 int b53_switch_register(struct b53_device *dev);
263 
264 static inline void b53_switch_remove(struct b53_device *dev)
265 {
266 	dsa_unregister_switch(dev->ds);
267 }
268 
269 static inline void b53_switch_shutdown(struct b53_device *dev)
270 {
271 	dsa_switch_shutdown(dev->ds);
272 }
273 
274 #define b53_build_op(type_op_size, val_type)				\
275 static inline int b53_##type_op_size(struct b53_device *dev, u8 page,	\
276 				     u8 reg, val_type val)		\
277 {									\
278 	int ret;							\
279 									\
280 	mutex_lock(&dev->reg_mutex);					\
281 	ret = dev->ops->type_op_size(dev, page, reg, val);		\
282 	mutex_unlock(&dev->reg_mutex);					\
283 									\
284 	return ret;							\
285 }
286 
287 b53_build_op(read8, u8 *);
288 b53_build_op(read16, u16 *);
289 b53_build_op(read32, u32 *);
290 b53_build_op(read48, u64 *);
291 b53_build_op(read64, u64 *);
292 
293 b53_build_op(write8, u8);
294 b53_build_op(write16, u16);
295 b53_build_op(write32, u32);
296 b53_build_op(write48, u64);
297 b53_build_op(write64, u64);
298 
299 struct b53_arl_entry {
300 	u16 port;
301 	u8 mac[ETH_ALEN];
302 	u16 vid;
303 	u8 is_valid:1;
304 	u8 is_age:1;
305 	u8 is_static:1;
306 };
307 
308 static inline void b53_arl_to_entry(struct b53_arl_entry *ent,
309 				    u64 mac_vid, u32 fwd_entry)
310 {
311 	memset(ent, 0, sizeof(*ent));
312 	ent->port = fwd_entry & ARLTBL_DATA_PORT_ID_MASK;
313 	ent->is_valid = !!(fwd_entry & ARLTBL_VALID);
314 	ent->is_age = !!(fwd_entry & ARLTBL_AGE);
315 	ent->is_static = !!(fwd_entry & ARLTBL_STATIC);
316 	u64_to_ether_addr(mac_vid, ent->mac);
317 	ent->vid = mac_vid >> ARLTBL_VID_S;
318 }
319 
320 static inline void b53_arl_to_entry_25(struct b53_arl_entry *ent,
321 				       u64 mac_vid)
322 {
323 	memset(ent, 0, sizeof(*ent));
324 	ent->port = (mac_vid >> ARLTBL_DATA_PORT_ID_S_25) &
325 		     ARLTBL_DATA_PORT_ID_MASK_25;
326 	ent->is_valid = !!(mac_vid & ARLTBL_VALID_25);
327 	ent->is_age = !!(mac_vid & ARLTBL_AGE_25);
328 	ent->is_static = !!(mac_vid & ARLTBL_STATIC_25);
329 	u64_to_ether_addr(mac_vid, ent->mac);
330 	ent->vid = mac_vid >> ARLTBL_VID_S_65;
331 }
332 
333 static inline void b53_arl_from_entry(u64 *mac_vid, u32 *fwd_entry,
334 				      const struct b53_arl_entry *ent)
335 {
336 	*mac_vid = ether_addr_to_u64(ent->mac);
337 	*mac_vid |= (u64)(ent->vid & ARLTBL_VID_MASK) << ARLTBL_VID_S;
338 	*fwd_entry = ent->port & ARLTBL_DATA_PORT_ID_MASK;
339 	if (ent->is_valid)
340 		*fwd_entry |= ARLTBL_VALID;
341 	if (ent->is_static)
342 		*fwd_entry |= ARLTBL_STATIC;
343 	if (ent->is_age)
344 		*fwd_entry |= ARLTBL_AGE;
345 }
346 
347 static inline void b53_arl_from_entry_25(u64 *mac_vid,
348 					 const struct b53_arl_entry *ent)
349 {
350 	*mac_vid = ether_addr_to_u64(ent->mac);
351 	*mac_vid |= (u64)(ent->port & ARLTBL_DATA_PORT_ID_MASK_25) <<
352 			  ARLTBL_DATA_PORT_ID_S_25;
353 	*mac_vid |= (u64)(ent->vid & ARLTBL_VID_MASK_25) <<
354 			  ARLTBL_VID_S_65;
355 	if (ent->is_valid)
356 		*mac_vid |= ARLTBL_VALID_25;
357 	if (ent->is_static)
358 		*mac_vid |= ARLTBL_STATIC_25;
359 	if (ent->is_age)
360 		*mac_vid |= ARLTBL_AGE_25;
361 }
362 
363 #ifdef CONFIG_BCM47XX
364 
365 #include <linux/bcm47xx_nvram.h>
366 #include <bcm47xx_board.h>
367 static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
368 {
369 	enum bcm47xx_board board = bcm47xx_board_get();
370 
371 	switch (board) {
372 	case BCM47XX_BOARD_LINKSYS_WRT300NV11:
373 	case BCM47XX_BOARD_LINKSYS_WRT310NV1:
374 		return 8;
375 	default:
376 		return bcm47xx_nvram_gpio_pin("robo_reset");
377 	}
378 }
379 #else
380 static inline int b53_switch_get_reset_gpio(struct b53_device *dev)
381 {
382 	return -ENOENT;
383 }
384 #endif
385 
386 /* Exported functions towards other drivers */
387 void b53_imp_vlan_setup(struct dsa_switch *ds, int cpu_port);
388 int b53_configure_vlan(struct dsa_switch *ds);
389 void b53_get_strings(struct dsa_switch *ds, int port, u32 stringset,
390 		     uint8_t *data);
391 void b53_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *data);
392 int b53_get_sset_count(struct dsa_switch *ds, int port, int sset);
393 void b53_get_ethtool_phy_stats(struct dsa_switch *ds, int port, uint64_t *data);
394 int b53_set_ageing_time(struct dsa_switch *ds, unsigned int msecs);
395 int b53_br_join(struct dsa_switch *ds, int port, struct dsa_bridge bridge,
396 		bool *tx_fwd_offload, struct netlink_ext_ack *extack);
397 void b53_br_leave(struct dsa_switch *ds, int port, struct dsa_bridge bridge);
398 void b53_br_set_stp_state(struct dsa_switch *ds, int port, u8 state);
399 void b53_br_fast_age(struct dsa_switch *ds, int port);
400 int b53_br_flags_pre(struct dsa_switch *ds, int port,
401 		     struct switchdev_brport_flags flags,
402 		     struct netlink_ext_ack *extack);
403 int b53_br_flags(struct dsa_switch *ds, int port,
404 		 struct switchdev_brport_flags flags,
405 		 struct netlink_ext_ack *extack);
406 int b53_setup_devlink_resources(struct dsa_switch *ds);
407 void b53_port_event(struct dsa_switch *ds, int port);
408 int b53_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
409 		       struct netlink_ext_ack *extack);
410 int b53_vlan_add(struct dsa_switch *ds, int port,
411 		 const struct switchdev_obj_port_vlan *vlan,
412 		 struct netlink_ext_ack *extack);
413 int b53_vlan_del(struct dsa_switch *ds, int port,
414 		 const struct switchdev_obj_port_vlan *vlan);
415 int b53_fdb_add(struct dsa_switch *ds, int port,
416 		const unsigned char *addr, u16 vid,
417 		struct dsa_db db);
418 int b53_fdb_del(struct dsa_switch *ds, int port,
419 		const unsigned char *addr, u16 vid,
420 		struct dsa_db db);
421 int b53_fdb_dump(struct dsa_switch *ds, int port,
422 		 dsa_fdb_dump_cb_t *cb, void *data);
423 int b53_mdb_add(struct dsa_switch *ds, int port,
424 		const struct switchdev_obj_port_mdb *mdb,
425 		struct dsa_db db);
426 int b53_mdb_del(struct dsa_switch *ds, int port,
427 		const struct switchdev_obj_port_mdb *mdb,
428 		struct dsa_db db);
429 int b53_mirror_add(struct dsa_switch *ds, int port,
430 		   struct dsa_mall_mirror_tc_entry *mirror, bool ingress,
431 		   struct netlink_ext_ack *extack);
432 enum dsa_tag_protocol b53_get_tag_protocol(struct dsa_switch *ds, int port,
433 					   enum dsa_tag_protocol mprot);
434 void b53_mirror_del(struct dsa_switch *ds, int port,
435 		    struct dsa_mall_mirror_tc_entry *mirror);
436 int b53_setup_port(struct dsa_switch *ds, int port);
437 int b53_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy);
438 void b53_disable_port(struct dsa_switch *ds, int port);
439 void b53_brcm_hdr_setup(struct dsa_switch *ds, int port);
440 int b53_eee_init(struct dsa_switch *ds, int port, struct phy_device *phy);
441 bool b53_support_eee(struct dsa_switch *ds, int port);
442 int b53_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_keee *e);
443 
444 #endif
445