xref: /openbmc/linux/drivers/net/dsa/microchip/ksz_common.h (revision de6dd626d7082eda383ec77a5e06093c82122d10)
1b31141d3SNishad Kamdar /* SPDX-License-Identifier: GPL-2.0 */
2b31141d3SNishad Kamdar /* Microchip switch driver common header
3c2e86691STristram Ha  *
47c6ff470STristram Ha  * Copyright (C) 2017-2019 Microchip Technology Inc.
5c2e86691STristram Ha  */
6c2e86691STristram Ha 
7c2e86691STristram Ha #ifndef __KSZ_COMMON_H
8c2e86691STristram Ha #define __KSZ_COMMON_H
9c2e86691STristram Ha 
106a7abc61SMarek Vasut #include <linux/etherdevice.h>
116a7abc61SMarek Vasut #include <linux/kernel.h>
126a7abc61SMarek Vasut #include <linux/mutex.h>
136a7abc61SMarek Vasut #include <linux/phy.h>
14ee394feaSMarek Vasut #include <linux/regmap.h>
156a7abc61SMarek Vasut #include <net/dsa.h>
166a7abc61SMarek Vasut 
176a7abc61SMarek Vasut struct vlan_table {
186a7abc61SMarek Vasut 	u32 table[3];
196a7abc61SMarek Vasut };
206a7abc61SMarek Vasut 
216a7abc61SMarek Vasut struct ksz_port_mib {
226a7abc61SMarek Vasut 	struct mutex cnt_mutex;		/* structure access */
236a7abc61SMarek Vasut 	u8 cnt_ptr;
246a7abc61SMarek Vasut 	u64 *counters;
25a7f4f13aSOleksij Rempel 	struct rtnl_link_stats64 stats64;
26a7f4f13aSOleksij Rempel 	struct spinlock stats64_lock;
276a7abc61SMarek Vasut };
286a7abc61SMarek Vasut 
296a7abc61SMarek Vasut struct ksz_port {
308f4f58f8SBen Hutchings 	bool remove_tag;		/* Remove Tag flag set, for ksz8795 only */
316a7abc61SMarek Vasut 	int stp_state;
326a7abc61SMarek Vasut 	struct phy_device phydev;
336a7abc61SMarek Vasut 
346a7abc61SMarek Vasut 	u32 on:1;			/* port is not disabled by hardware */
356a7abc61SMarek Vasut 	u32 phy:1;			/* port has a PHY */
366a7abc61SMarek Vasut 	u32 fiber:1;			/* port is fiber */
376a7abc61SMarek Vasut 	u32 sgmii:1;			/* port is SGMII */
386a7abc61SMarek Vasut 	u32 force:1;
396a7abc61SMarek Vasut 	u32 read:1;			/* read MIB counters in background */
406a7abc61SMarek Vasut 	u32 freeze:1;			/* MIB counter freeze is enabled */
416a7abc61SMarek Vasut 
426a7abc61SMarek Vasut 	struct ksz_port_mib mib;
43edecfa98SHelmut Grohne 	phy_interface_t interface;
44e18058eaSOleksij Rempel 	u16 max_frame;
456a7abc61SMarek Vasut };
466a7abc61SMarek Vasut 
476a7abc61SMarek Vasut struct ksz_device {
486a7abc61SMarek Vasut 	struct dsa_switch *ds;
496a7abc61SMarek Vasut 	struct ksz_platform_data *pdata;
506a7abc61SMarek Vasut 	const char *name;
516a7abc61SMarek Vasut 
526a7abc61SMarek Vasut 	struct mutex dev_mutex;		/* device access */
53013572a2SMarek Vasut 	struct mutex regmap_mutex;	/* regmap access */
546a7abc61SMarek Vasut 	struct mutex alu_mutex;		/* ALU access */
556a7abc61SMarek Vasut 	struct mutex vlan_mutex;	/* vlan access */
566a7abc61SMarek Vasut 	const struct ksz_dev_ops *dev_ops;
576a7abc61SMarek Vasut 
586a7abc61SMarek Vasut 	struct device *dev;
596a7abc61SMarek Vasut 	struct regmap *regmap[3];
606a7abc61SMarek Vasut 
616a7abc61SMarek Vasut 	void *priv;
626a7abc61SMarek Vasut 
636a7abc61SMarek Vasut 	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
646a7abc61SMarek Vasut 
656a7abc61SMarek Vasut 	/* chip specific data */
666a7abc61SMarek Vasut 	u32 chip_id;
676a7abc61SMarek Vasut 	int num_vlans;
686a7abc61SMarek Vasut 	int num_alus;
696a7abc61SMarek Vasut 	int num_statics;
706a7abc61SMarek Vasut 	int cpu_port;			/* port connected to CPU */
716a7abc61SMarek Vasut 	int cpu_ports;			/* port bitmap can be cpu port */
726a7abc61SMarek Vasut 	int phy_port_cnt;
736a7abc61SMarek Vasut 	int port_cnt;
7412c2bb96SColin Ian King 	u8 reg_mib_cnt;
756a7abc61SMarek Vasut 	int mib_cnt;
764b20a07eSOleksij Rempel 	const struct mib_names *mib_names;
77edecfa98SHelmut Grohne 	phy_interface_t compat_interface;
786a7abc61SMarek Vasut 	u32 regs_size;
796a7abc61SMarek Vasut 	bool phy_errata_9477;
807b6e6235SOleksij Rempel 	bool ksz87xx_eee_link_erratum;
816a7abc61SMarek Vasut 	bool synclko_125;
8248bf8b8aSRobert Hancock 	bool synclko_disable;
836a7abc61SMarek Vasut 
846a7abc61SMarek Vasut 	struct vlan_table *vlan_cache;
856a7abc61SMarek Vasut 
866a7abc61SMarek Vasut 	struct ksz_port *ports;
87469b390eSGeorge McCollister 	struct delayed_work mib_read;
886a7abc61SMarek Vasut 	unsigned long mib_read_interval;
896a7abc61SMarek Vasut 	u16 mirror_rx;
906a7abc61SMarek Vasut 	u16 mirror_tx;
916a7abc61SMarek Vasut 	u32 features;			/* chip specific features */
926a7abc61SMarek Vasut 	u32 overrides;			/* chip functions set by user */
936a7abc61SMarek Vasut 	u16 host_mask;
946a7abc61SMarek Vasut 	u16 port_mask;
956a7abc61SMarek Vasut };
966a7abc61SMarek Vasut 
976a7abc61SMarek Vasut struct alu_struct {
986a7abc61SMarek Vasut 	/* entry 1 */
996a7abc61SMarek Vasut 	u8	is_static:1;
1006a7abc61SMarek Vasut 	u8	is_src_filter:1;
1016a7abc61SMarek Vasut 	u8	is_dst_filter:1;
1026a7abc61SMarek Vasut 	u8	prio_age:3;
1036a7abc61SMarek Vasut 	u32	_reserv_0_1:23;
1046a7abc61SMarek Vasut 	u8	mstp:3;
1056a7abc61SMarek Vasut 	/* entry 2 */
1066a7abc61SMarek Vasut 	u8	is_override:1;
1076a7abc61SMarek Vasut 	u8	is_use_fid:1;
1086a7abc61SMarek Vasut 	u32	_reserv_1_1:23;
1096a7abc61SMarek Vasut 	u8	port_forward:7;
1106a7abc61SMarek Vasut 	/* entry 3 & 4*/
1116a7abc61SMarek Vasut 	u32	_reserv_2_1:9;
1126a7abc61SMarek Vasut 	u8	fid:7;
1136a7abc61SMarek Vasut 	u8	mac[ETH_ALEN];
1146a7abc61SMarek Vasut };
1156a7abc61SMarek Vasut 
1166a7abc61SMarek Vasut struct ksz_dev_ops {
1176a7abc61SMarek Vasut 	u32 (*get_port_addr)(int port, int offset);
1186a7abc61SMarek Vasut 	void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
1196a7abc61SMarek Vasut 	void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
1206a7abc61SMarek Vasut 	void (*port_cleanup)(struct ksz_device *dev, int port);
1216a7abc61SMarek Vasut 	void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
1226a7abc61SMarek Vasut 	void (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
1236a7abc61SMarek Vasut 	void (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
1246a7abc61SMarek Vasut 	int (*r_dyn_mac_table)(struct ksz_device *dev, u16 addr, u8 *mac_addr,
1256a7abc61SMarek Vasut 			       u8 *fid, u8 *src_port, u8 *timestamp,
1266a7abc61SMarek Vasut 			       u16 *entries);
1276a7abc61SMarek Vasut 	int (*r_sta_mac_table)(struct ksz_device *dev, u16 addr,
1286a7abc61SMarek Vasut 			       struct alu_struct *alu);
1296a7abc61SMarek Vasut 	void (*w_sta_mac_table)(struct ksz_device *dev, u16 addr,
1306a7abc61SMarek Vasut 				struct alu_struct *alu);
1316a7abc61SMarek Vasut 	void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
1326a7abc61SMarek Vasut 			  u64 *cnt);
1336a7abc61SMarek Vasut 	void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
1346a7abc61SMarek Vasut 			  u64 *dropped, u64 *cnt);
135a7f4f13aSOleksij Rempel 	void (*r_mib_stat64)(struct ksz_device *dev, int port);
1366a7abc61SMarek Vasut 	void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
1376a7abc61SMarek Vasut 	void (*port_init_cnt)(struct ksz_device *dev, int port);
1386a7abc61SMarek Vasut 	int (*shutdown)(struct ksz_device *dev);
1396a7abc61SMarek Vasut 	int (*detect)(struct ksz_device *dev);
1406a7abc61SMarek Vasut 	int (*init)(struct ksz_device *dev);
1416a7abc61SMarek Vasut 	void (*exit)(struct ksz_device *dev);
1426a7abc61SMarek Vasut };
1436a7abc61SMarek Vasut 
1446a7abc61SMarek Vasut struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
1456a7abc61SMarek Vasut int ksz_switch_register(struct ksz_device *dev,
1466a7abc61SMarek Vasut 			const struct ksz_dev_ops *ops);
1476a7abc61SMarek Vasut void ksz_switch_remove(struct ksz_device *dev);
1486a7abc61SMarek Vasut 
1494b5baca0SMichael Grzeschik int ksz8_switch_register(struct ksz_device *dev);
1506a7abc61SMarek Vasut int ksz9477_switch_register(struct ksz_device *dev);
151ee394feaSMarek Vasut 
152c2e86691STristram Ha void ksz_update_port_member(struct ksz_device *dev, int port);
1537c6ff470STristram Ha void ksz_init_mib_timer(struct ksz_device *dev);
154c2e86691STristram Ha 
155c2e86691STristram Ha /* Common DSA access functions */
156c2e86691STristram Ha 
157c2e86691STristram Ha int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg);
158c2e86691STristram Ha int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val);
159143a102eSCodrin Ciubotariu void ksz_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
160143a102eSCodrin Ciubotariu 		       phy_interface_t interface);
161c2e86691STristram Ha int ksz_sset_count(struct dsa_switch *ds, int port, int sset);
1627c6ff470STristram Ha void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf);
163c2e86691STristram Ha int ksz_port_bridge_join(struct dsa_switch *ds, int port,
16406b9cce4SVladimir Oltean 			 struct dsa_bridge bridge, bool *tx_fwd_offload,
16506b9cce4SVladimir Oltean 			 struct netlink_ext_ack *extack);
166c2e86691STristram Ha void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
167d3eed0e5SVladimir Oltean 			   struct dsa_bridge bridge);
168*de6dd626SArun Ramadoss void ksz_port_stp_state_set(struct dsa_switch *ds, int port,
169*de6dd626SArun Ramadoss 			    u8 state, int reg);
170c2e86691STristram Ha void ksz_port_fast_age(struct dsa_switch *ds, int port);
171c2e86691STristram Ha int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
172c2e86691STristram Ha 		      void *data);
173a52b2da7SVladimir Oltean int ksz_port_mdb_add(struct dsa_switch *ds, int port,
174c2693363SVladimir Oltean 		     const struct switchdev_obj_port_mdb *mdb,
175c2693363SVladimir Oltean 		     struct dsa_db db);
176c2e86691STristram Ha int ksz_port_mdb_del(struct dsa_switch *ds, int port,
177c2693363SVladimir Oltean 		     const struct switchdev_obj_port_mdb *mdb,
178c2693363SVladimir Oltean 		     struct dsa_db db);
179c2e86691STristram Ha int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy);
180c2e86691STristram Ha 
181c2e86691STristram Ha /* Common register access functions */
182c2e86691STristram Ha 
183c2e86691STristram Ha static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
184c2e86691STristram Ha {
185ee394feaSMarek Vasut 	unsigned int value;
186ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[0], reg, &value);
187c2e86691STristram Ha 
188ee394feaSMarek Vasut 	*val = value;
189c2e86691STristram Ha 	return ret;
190c2e86691STristram Ha }
191c2e86691STristram Ha 
192c2e86691STristram Ha static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
193c2e86691STristram Ha {
194ee394feaSMarek Vasut 	unsigned int value;
195ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[1], reg, &value);
196c2e86691STristram Ha 
197ee394feaSMarek Vasut 	*val = value;
198c2e86691STristram Ha 	return ret;
199c2e86691STristram Ha }
200c2e86691STristram Ha 
201c2e86691STristram Ha static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
202c2e86691STristram Ha {
203ee394feaSMarek Vasut 	unsigned int value;
204ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[2], reg, &value);
205c2e86691STristram Ha 
206ee394feaSMarek Vasut 	*val = value;
207c2e86691STristram Ha 	return ret;
208c2e86691STristram Ha }
209c2e86691STristram Ha 
210e66f840cSTristram Ha static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
211e66f840cSTristram Ha {
212e66f840cSTristram Ha 	u32 value[2];
213e66f840cSTristram Ha 	int ret;
214e66f840cSTristram Ha 
215e66f840cSTristram Ha 	ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
216c34f674cSBen Hutchings 	if (!ret)
217c34f674cSBen Hutchings 		*val = (u64)value[0] << 32 | value[1];
218e66f840cSTristram Ha 
219e66f840cSTristram Ha 	return ret;
220e66f840cSTristram Ha }
221e66f840cSTristram Ha 
222c2e86691STristram Ha static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
223c2e86691STristram Ha {
224ee394feaSMarek Vasut 	return regmap_write(dev->regmap[0], reg, value);
225c2e86691STristram Ha }
226c2e86691STristram Ha 
227c2e86691STristram Ha static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
228c2e86691STristram Ha {
229ee394feaSMarek Vasut 	return regmap_write(dev->regmap[1], reg, value);
230c2e86691STristram Ha }
231c2e86691STristram Ha 
232c2e86691STristram Ha static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
233c2e86691STristram Ha {
234ee394feaSMarek Vasut 	return regmap_write(dev->regmap[2], reg, value);
235c2e86691STristram Ha }
236c2e86691STristram Ha 
237e66f840cSTristram Ha static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
238e66f840cSTristram Ha {
239e66f840cSTristram Ha 	u32 val[2];
240e66f840cSTristram Ha 
241e66f840cSTristram Ha 	/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
242e66f840cSTristram Ha 	value = swab64(value);
243e66f840cSTristram Ha 	val[0] = swab32(value & 0xffffffffULL);
244e66f840cSTristram Ha 	val[1] = swab32(value >> 32ULL);
245e66f840cSTristram Ha 
246e66f840cSTristram Ha 	return regmap_bulk_write(dev->regmap[2], reg, val, 2);
247e66f840cSTristram Ha }
248e66f840cSTristram Ha 
249c2e86691STristram Ha static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
250c2e86691STristram Ha 			      u8 *data)
251c2e86691STristram Ha {
252c2e86691STristram Ha 	ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
253c2e86691STristram Ha }
254c2e86691STristram Ha 
255c2e86691STristram Ha static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
256c2e86691STristram Ha 			       u16 *data)
257c2e86691STristram Ha {
258c2e86691STristram Ha 	ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
259c2e86691STristram Ha }
260c2e86691STristram Ha 
261c2e86691STristram Ha static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
262c2e86691STristram Ha 			       u32 *data)
263c2e86691STristram Ha {
264c2e86691STristram Ha 	ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
265c2e86691STristram Ha }
266c2e86691STristram Ha 
267c2e86691STristram Ha static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
268c2e86691STristram Ha 			       u8 data)
269c2e86691STristram Ha {
270c2e86691STristram Ha 	ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
271c2e86691STristram Ha }
272c2e86691STristram Ha 
273c2e86691STristram Ha static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
274c2e86691STristram Ha 				u16 data)
275c2e86691STristram Ha {
276c2e86691STristram Ha 	ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset), data);
277c2e86691STristram Ha }
278c2e86691STristram Ha 
279c2e86691STristram Ha static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
280c2e86691STristram Ha 				u32 data)
281c2e86691STristram Ha {
282c2e86691STristram Ha 	ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data);
283c2e86691STristram Ha }
284c2e86691STristram Ha 
285013572a2SMarek Vasut static inline void ksz_regmap_lock(void *__mtx)
286013572a2SMarek Vasut {
287013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
288013572a2SMarek Vasut 	mutex_lock(mtx);
289013572a2SMarek Vasut }
290013572a2SMarek Vasut 
291013572a2SMarek Vasut static inline void ksz_regmap_unlock(void *__mtx)
292013572a2SMarek Vasut {
293013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
294013572a2SMarek Vasut 	mutex_unlock(mtx);
295013572a2SMarek Vasut }
296013572a2SMarek Vasut 
297*de6dd626SArun Ramadoss /* STP State Defines */
298*de6dd626SArun Ramadoss #define PORT_TX_ENABLE			BIT(2)
299*de6dd626SArun Ramadoss #define PORT_RX_ENABLE			BIT(1)
300*de6dd626SArun Ramadoss #define PORT_LEARN_DISABLE		BIT(0)
301*de6dd626SArun Ramadoss 
302255b59adSMarek Vasut /* Regmap tables generation */
303255b59adSMarek Vasut #define KSZ_SPI_OP_RD		3
304255b59adSMarek Vasut #define KSZ_SPI_OP_WR		2
305255b59adSMarek Vasut 
30620e03777STristram Ha #define swabnot_used(x)		0
30720e03777STristram Ha 
308255b59adSMarek Vasut #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)		\
309255b59adSMarek Vasut 	swab##swp((opcode) << ((regbits) + (regpad)))
310255b59adSMarek Vasut 
311255b59adSMarek Vasut #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)		\
312255b59adSMarek Vasut 	{								\
3135f81d545SGeorge McCollister 		.name = #width,						\
314255b59adSMarek Vasut 		.val_bits = (width),					\
315a3aa6e65SMarek Vasut 		.reg_stride = 1,					\
316255b59adSMarek Vasut 		.reg_bits = (regbits) + (regalign),			\
317255b59adSMarek Vasut 		.pad_bits = (regpad),					\
318255b59adSMarek Vasut 		.max_register = BIT(regbits) - 1,			\
319255b59adSMarek Vasut 		.cache_type = REGCACHE_NONE,				\
320255b59adSMarek Vasut 		.read_flag_mask =					\
321255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,	\
322255b59adSMarek Vasut 					     regbits, regpad),		\
323255b59adSMarek Vasut 		.write_flag_mask =					\
324255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
325255b59adSMarek Vasut 					     regbits, regpad),		\
326013572a2SMarek Vasut 		.lock = ksz_regmap_lock,				\
327013572a2SMarek Vasut 		.unlock = ksz_regmap_unlock,				\
328255b59adSMarek Vasut 		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
329255b59adSMarek Vasut 		.val_format_endian = REGMAP_ENDIAN_BIG			\
330255b59adSMarek Vasut 	}
331255b59adSMarek Vasut 
332255b59adSMarek Vasut #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)		\
333255b59adSMarek Vasut 	static const struct regmap_config ksz##_regmap_config[] = {	\
334255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
335255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
336255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
337255b59adSMarek Vasut 	}
338255b59adSMarek Vasut 
339c2e86691STristram Ha #endif
340