xref: /openbmc/linux/drivers/net/dsa/microchip/ksz_common.h (revision e587be759e6e4f5a257d1f2bd7f2883a6397a6e8)
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 
1765ac79e1SArun Ramadoss #define KSZ_MAX_NUM_PORTS 8
1865ac79e1SArun Ramadoss 
196a7abc61SMarek Vasut struct vlan_table {
206a7abc61SMarek Vasut 	u32 table[3];
216a7abc61SMarek Vasut };
226a7abc61SMarek Vasut 
236a7abc61SMarek Vasut struct ksz_port_mib {
246a7abc61SMarek Vasut 	struct mutex cnt_mutex;		/* structure access */
256a7abc61SMarek Vasut 	u8 cnt_ptr;
266a7abc61SMarek Vasut 	u64 *counters;
27a7f4f13aSOleksij Rempel 	struct rtnl_link_stats64 stats64;
28a7f4f13aSOleksij Rempel 	struct spinlock stats64_lock;
296a7abc61SMarek Vasut };
306a7abc61SMarek Vasut 
31a530e6f2SArun Ramadoss struct ksz_mib_names {
32a530e6f2SArun Ramadoss 	int index;
33a530e6f2SArun Ramadoss 	char string[ETH_GSTRING_LEN];
34a530e6f2SArun Ramadoss };
35a530e6f2SArun Ramadoss 
36462d5250SArun Ramadoss struct ksz_chip_data {
37462d5250SArun Ramadoss 	u32 chip_id;
38462d5250SArun Ramadoss 	const char *dev_name;
39462d5250SArun Ramadoss 	int num_vlans;
40462d5250SArun Ramadoss 	int num_alus;
41462d5250SArun Ramadoss 	int num_statics;
42462d5250SArun Ramadoss 	int cpu_ports;
43462d5250SArun Ramadoss 	int port_cnt;
44462d5250SArun Ramadoss 	bool phy_errata_9477;
45462d5250SArun Ramadoss 	bool ksz87xx_eee_link_erratum;
46a530e6f2SArun Ramadoss 	const struct ksz_mib_names *mib_names;
47a530e6f2SArun Ramadoss 	int mib_cnt;
48a530e6f2SArun Ramadoss 	u8 reg_mib_cnt;
49e593df51SArun Ramadoss 	int stp_ctrl_reg;
5065ac79e1SArun Ramadoss 	bool supports_mii[KSZ_MAX_NUM_PORTS];
5165ac79e1SArun Ramadoss 	bool supports_rmii[KSZ_MAX_NUM_PORTS];
5265ac79e1SArun Ramadoss 	bool supports_rgmii[KSZ_MAX_NUM_PORTS];
5365ac79e1SArun Ramadoss 	bool internal_phy[KSZ_MAX_NUM_PORTS];
54462d5250SArun Ramadoss };
55462d5250SArun Ramadoss 
566a7abc61SMarek Vasut struct ksz_port {
578f4f58f8SBen Hutchings 	bool remove_tag;		/* Remove Tag flag set, for ksz8795 only */
586a7abc61SMarek Vasut 	int stp_state;
596a7abc61SMarek Vasut 	struct phy_device phydev;
606a7abc61SMarek Vasut 
616a7abc61SMarek Vasut 	u32 on:1;			/* port is not disabled by hardware */
626a7abc61SMarek Vasut 	u32 phy:1;			/* port has a PHY */
636a7abc61SMarek Vasut 	u32 fiber:1;			/* port is fiber */
646a7abc61SMarek Vasut 	u32 sgmii:1;			/* port is SGMII */
656a7abc61SMarek Vasut 	u32 force:1;
666a7abc61SMarek Vasut 	u32 read:1;			/* read MIB counters in background */
676a7abc61SMarek Vasut 	u32 freeze:1;			/* MIB counter freeze is enabled */
686a7abc61SMarek Vasut 
696a7abc61SMarek Vasut 	struct ksz_port_mib mib;
70edecfa98SHelmut Grohne 	phy_interface_t interface;
71e18058eaSOleksij Rempel 	u16 max_frame;
726a7abc61SMarek Vasut };
736a7abc61SMarek Vasut 
746a7abc61SMarek Vasut struct ksz_device {
756a7abc61SMarek Vasut 	struct dsa_switch *ds;
766a7abc61SMarek Vasut 	struct ksz_platform_data *pdata;
77462d5250SArun Ramadoss 	const struct ksz_chip_data *info;
786a7abc61SMarek Vasut 
796a7abc61SMarek Vasut 	struct mutex dev_mutex;		/* device access */
80013572a2SMarek Vasut 	struct mutex regmap_mutex;	/* regmap access */
816a7abc61SMarek Vasut 	struct mutex alu_mutex;		/* ALU access */
826a7abc61SMarek Vasut 	struct mutex vlan_mutex;	/* vlan access */
836a7abc61SMarek Vasut 	const struct ksz_dev_ops *dev_ops;
846a7abc61SMarek Vasut 
856a7abc61SMarek Vasut 	struct device *dev;
866a7abc61SMarek Vasut 	struct regmap *regmap[3];
876a7abc61SMarek Vasut 
886a7abc61SMarek Vasut 	void *priv;
896a7abc61SMarek Vasut 
906a7abc61SMarek Vasut 	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
916a7abc61SMarek Vasut 
926a7abc61SMarek Vasut 	/* chip specific data */
936a7abc61SMarek Vasut 	u32 chip_id;
9491a98917SArun Ramadoss 	u8 chip_rev;
956a7abc61SMarek Vasut 	int cpu_port;			/* port connected to CPU */
966a7abc61SMarek Vasut 	int phy_port_cnt;
97edecfa98SHelmut Grohne 	phy_interface_t compat_interface;
986a7abc61SMarek Vasut 	bool synclko_125;
9948bf8b8aSRobert Hancock 	bool synclko_disable;
1006a7abc61SMarek Vasut 
1016a7abc61SMarek Vasut 	struct vlan_table *vlan_cache;
1026a7abc61SMarek Vasut 
1036a7abc61SMarek Vasut 	struct ksz_port *ports;
104469b390eSGeorge McCollister 	struct delayed_work mib_read;
1056a7abc61SMarek Vasut 	unsigned long mib_read_interval;
1066a7abc61SMarek Vasut 	u16 mirror_rx;
1076a7abc61SMarek Vasut 	u16 mirror_tx;
1086a7abc61SMarek Vasut 	u32 features;			/* chip specific features */
1096a7abc61SMarek Vasut 	u16 port_mask;
1106a7abc61SMarek Vasut };
1116a7abc61SMarek Vasut 
112462d5250SArun Ramadoss /* List of supported models */
113462d5250SArun Ramadoss enum ksz_model {
114462d5250SArun Ramadoss 	KSZ8795,
115462d5250SArun Ramadoss 	KSZ8794,
116462d5250SArun Ramadoss 	KSZ8765,
117462d5250SArun Ramadoss 	KSZ8830,
118462d5250SArun Ramadoss 	KSZ9477,
119462d5250SArun Ramadoss 	KSZ9897,
120462d5250SArun Ramadoss 	KSZ9893,
121462d5250SArun Ramadoss 	KSZ9567,
122462d5250SArun Ramadoss 	LAN9370,
123462d5250SArun Ramadoss 	LAN9371,
124462d5250SArun Ramadoss 	LAN9372,
125462d5250SArun Ramadoss 	LAN9373,
126462d5250SArun Ramadoss 	LAN9374,
127462d5250SArun Ramadoss };
128462d5250SArun Ramadoss 
129462d5250SArun Ramadoss enum ksz_chip_id {
130462d5250SArun Ramadoss 	KSZ8795_CHIP_ID = 0x8795,
131462d5250SArun Ramadoss 	KSZ8794_CHIP_ID = 0x8794,
132462d5250SArun Ramadoss 	KSZ8765_CHIP_ID = 0x8765,
133462d5250SArun Ramadoss 	KSZ8830_CHIP_ID = 0x8830,
134462d5250SArun Ramadoss 	KSZ9477_CHIP_ID = 0x00947700,
135462d5250SArun Ramadoss 	KSZ9897_CHIP_ID = 0x00989700,
136462d5250SArun Ramadoss 	KSZ9893_CHIP_ID = 0x00989300,
137462d5250SArun Ramadoss 	KSZ9567_CHIP_ID = 0x00956700,
138462d5250SArun Ramadoss 	LAN9370_CHIP_ID = 0x00937000,
139462d5250SArun Ramadoss 	LAN9371_CHIP_ID = 0x00937100,
140462d5250SArun Ramadoss 	LAN9372_CHIP_ID = 0x00937200,
141462d5250SArun Ramadoss 	LAN9373_CHIP_ID = 0x00937300,
142462d5250SArun Ramadoss 	LAN9374_CHIP_ID = 0x00937400,
143462d5250SArun Ramadoss };
144462d5250SArun Ramadoss 
1456a7abc61SMarek Vasut struct alu_struct {
1466a7abc61SMarek Vasut 	/* entry 1 */
1476a7abc61SMarek Vasut 	u8	is_static:1;
1486a7abc61SMarek Vasut 	u8	is_src_filter:1;
1496a7abc61SMarek Vasut 	u8	is_dst_filter:1;
1506a7abc61SMarek Vasut 	u8	prio_age:3;
1516a7abc61SMarek Vasut 	u32	_reserv_0_1:23;
1526a7abc61SMarek Vasut 	u8	mstp:3;
1536a7abc61SMarek Vasut 	/* entry 2 */
1546a7abc61SMarek Vasut 	u8	is_override:1;
1556a7abc61SMarek Vasut 	u8	is_use_fid:1;
1566a7abc61SMarek Vasut 	u32	_reserv_1_1:23;
1576a7abc61SMarek Vasut 	u8	port_forward:7;
1586a7abc61SMarek Vasut 	/* entry 3 & 4*/
1596a7abc61SMarek Vasut 	u32	_reserv_2_1:9;
1606a7abc61SMarek Vasut 	u8	fid:7;
1616a7abc61SMarek Vasut 	u8	mac[ETH_ALEN];
1626a7abc61SMarek Vasut };
1636a7abc61SMarek Vasut 
1646a7abc61SMarek Vasut struct ksz_dev_ops {
1656a7abc61SMarek Vasut 	u32 (*get_port_addr)(int port, int offset);
1666a7abc61SMarek Vasut 	void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
1676a7abc61SMarek Vasut 	void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
1686a7abc61SMarek Vasut 	void (*port_cleanup)(struct ksz_device *dev, int port);
1696a7abc61SMarek Vasut 	void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
1706a7abc61SMarek Vasut 	void (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
1716a7abc61SMarek Vasut 	void (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
1726a7abc61SMarek Vasut 	void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
1736a7abc61SMarek Vasut 			  u64 *cnt);
1746a7abc61SMarek Vasut 	void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
1756a7abc61SMarek Vasut 			  u64 *dropped, u64 *cnt);
176a7f4f13aSOleksij Rempel 	void (*r_mib_stat64)(struct ksz_device *dev, int port);
177f0d997e3SArun Ramadoss 	int  (*vlan_filtering)(struct ksz_device *dev, int port,
178f0d997e3SArun Ramadoss 			       bool flag, struct netlink_ext_ack *extack);
179f0d997e3SArun Ramadoss 	int  (*vlan_add)(struct ksz_device *dev, int port,
180f0d997e3SArun Ramadoss 			 const struct switchdev_obj_port_vlan *vlan,
181f0d997e3SArun Ramadoss 			 struct netlink_ext_ack *extack);
182f0d997e3SArun Ramadoss 	int  (*vlan_del)(struct ksz_device *dev, int port,
183f0d997e3SArun Ramadoss 			 const struct switchdev_obj_port_vlan *vlan);
18400a298bbSArun Ramadoss 	int (*mirror_add)(struct ksz_device *dev, int port,
18500a298bbSArun Ramadoss 			  struct dsa_mall_mirror_tc_entry *mirror,
18600a298bbSArun Ramadoss 			  bool ingress, struct netlink_ext_ack *extack);
18700a298bbSArun Ramadoss 	void (*mirror_del)(struct ksz_device *dev, int port,
18800a298bbSArun Ramadoss 			   struct dsa_mall_mirror_tc_entry *mirror);
189*e587be75SArun Ramadoss 	int (*fdb_add)(struct ksz_device *dev, int port,
190*e587be75SArun Ramadoss 		       const unsigned char *addr, u16 vid, struct dsa_db db);
191*e587be75SArun Ramadoss 	int (*fdb_del)(struct ksz_device *dev, int port,
192*e587be75SArun Ramadoss 		       const unsigned char *addr, u16 vid, struct dsa_db db);
193*e587be75SArun Ramadoss 	int (*fdb_dump)(struct ksz_device *dev, int port,
194*e587be75SArun Ramadoss 			dsa_fdb_dump_cb_t *cb, void *data);
195980c7d17SArun Ramadoss 	int (*mdb_add)(struct ksz_device *dev, int port,
196980c7d17SArun Ramadoss 		       const struct switchdev_obj_port_mdb *mdb,
197980c7d17SArun Ramadoss 		       struct dsa_db db);
198980c7d17SArun Ramadoss 	int (*mdb_del)(struct ksz_device *dev, int port,
199980c7d17SArun Ramadoss 		       const struct switchdev_obj_port_mdb *mdb,
200980c7d17SArun Ramadoss 		       struct dsa_db db);
2017012033cSArun Ramadoss 	void (*get_caps)(struct ksz_device *dev, int port,
2027012033cSArun Ramadoss 			 struct phylink_config *config);
2036a7abc61SMarek Vasut 	void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
2046a7abc61SMarek Vasut 	void (*port_init_cnt)(struct ksz_device *dev, int port);
2056a7abc61SMarek Vasut 	int (*shutdown)(struct ksz_device *dev);
2066a7abc61SMarek Vasut 	int (*init)(struct ksz_device *dev);
2076a7abc61SMarek Vasut 	void (*exit)(struct ksz_device *dev);
2086a7abc61SMarek Vasut };
2096a7abc61SMarek Vasut 
2106a7abc61SMarek Vasut struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
2116a7abc61SMarek Vasut int ksz_switch_register(struct ksz_device *dev,
2126a7abc61SMarek Vasut 			const struct ksz_dev_ops *ops);
2136a7abc61SMarek Vasut void ksz_switch_remove(struct ksz_device *dev);
2146a7abc61SMarek Vasut 
2154b5baca0SMichael Grzeschik int ksz8_switch_register(struct ksz_device *dev);
2166a7abc61SMarek Vasut int ksz9477_switch_register(struct ksz_device *dev);
217ee394feaSMarek Vasut 
2187c6ff470STristram Ha void ksz_init_mib_timer(struct ksz_device *dev);
219c6101dd7SArun Ramadoss void ksz_r_mib_stats64(struct ksz_device *dev, int port);
220c6101dd7SArun Ramadoss void ksz_get_stats64(struct dsa_switch *ds, int port,
221c6101dd7SArun Ramadoss 		     struct rtnl_link_stats64 *s);
22265ac79e1SArun Ramadoss void ksz_phylink_get_caps(struct dsa_switch *ds, int port,
22365ac79e1SArun Ramadoss 			  struct phylink_config *config);
224eee16b14SArun Ramadoss extern const struct ksz_chip_data ksz_switch_chips[];
225c2e86691STristram Ha 
226c2e86691STristram Ha /* Common DSA access functions */
227c2e86691STristram Ha 
228c2e86691STristram Ha int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg);
229c2e86691STristram Ha int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val);
230143a102eSCodrin Ciubotariu void ksz_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
231143a102eSCodrin Ciubotariu 		       phy_interface_t interface);
232c2e86691STristram Ha int ksz_sset_count(struct dsa_switch *ds, int port, int sset);
2337c6ff470STristram Ha void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, uint64_t *buf);
234c2e86691STristram Ha int ksz_port_bridge_join(struct dsa_switch *ds, int port,
23506b9cce4SVladimir Oltean 			 struct dsa_bridge bridge, bool *tx_fwd_offload,
23606b9cce4SVladimir Oltean 			 struct netlink_ext_ack *extack);
237c2e86691STristram Ha void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
238d3eed0e5SVladimir Oltean 			   struct dsa_bridge bridge);
239e593df51SArun Ramadoss void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
240c2e86691STristram Ha void ksz_port_fast_age(struct dsa_switch *ds, int port);
241*e587be75SArun Ramadoss int ksz_port_fdb_add(struct dsa_switch *ds, int port,
242*e587be75SArun Ramadoss 		     const unsigned char *addr, u16 vid, struct dsa_db db);
243*e587be75SArun Ramadoss int ksz_port_fdb_del(struct dsa_switch *ds, int port,
244*e587be75SArun Ramadoss 		     const unsigned char *addr, u16 vid, struct dsa_db db);
245c2e86691STristram Ha int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb,
246c2e86691STristram Ha 		      void *data);
247a52b2da7SVladimir Oltean int ksz_port_mdb_add(struct dsa_switch *ds, int port,
248c2693363SVladimir Oltean 		     const struct switchdev_obj_port_mdb *mdb,
249c2693363SVladimir Oltean 		     struct dsa_db db);
250c2e86691STristram Ha int ksz_port_mdb_del(struct dsa_switch *ds, int port,
251c2693363SVladimir Oltean 		     const struct switchdev_obj_port_mdb *mdb,
252c2693363SVladimir Oltean 		     struct dsa_db db);
253c2e86691STristram Ha int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy);
254997d2126SArun Ramadoss void ksz_get_strings(struct dsa_switch *ds, int port,
255997d2126SArun Ramadoss 		     u32 stringset, uint8_t *buf);
256534a0431SArun Ramadoss enum dsa_tag_protocol ksz_get_tag_protocol(struct dsa_switch *ds,
257534a0431SArun Ramadoss 					   int port, enum dsa_tag_protocol mp);
258f0d997e3SArun Ramadoss int ksz_port_vlan_filtering(struct dsa_switch *ds, int port,
259f0d997e3SArun Ramadoss 			    bool flag, struct netlink_ext_ack *extack);
260f0d997e3SArun Ramadoss int ksz_port_vlan_add(struct dsa_switch *ds, int port,
261f0d997e3SArun Ramadoss 		      const struct switchdev_obj_port_vlan *vlan,
262f0d997e3SArun Ramadoss 		      struct netlink_ext_ack *extack);
263f0d997e3SArun Ramadoss int ksz_port_vlan_del(struct dsa_switch *ds, int port,
264f0d997e3SArun Ramadoss 		      const struct switchdev_obj_port_vlan *vlan);
26500a298bbSArun Ramadoss int ksz_port_mirror_add(struct dsa_switch *ds, int port,
26600a298bbSArun Ramadoss 			struct dsa_mall_mirror_tc_entry *mirror,
26700a298bbSArun Ramadoss 			bool ingress, struct netlink_ext_ack *extack);
26800a298bbSArun Ramadoss void ksz_port_mirror_del(struct dsa_switch *ds, int port,
26900a298bbSArun Ramadoss 			 struct dsa_mall_mirror_tc_entry *mirror);
270c2e86691STristram Ha 
271c2e86691STristram Ha /* Common register access functions */
272c2e86691STristram Ha 
273c2e86691STristram Ha static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
274c2e86691STristram Ha {
275ee394feaSMarek Vasut 	unsigned int value;
276ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[0], reg, &value);
277c2e86691STristram Ha 
278ee394feaSMarek Vasut 	*val = value;
279c2e86691STristram Ha 	return ret;
280c2e86691STristram Ha }
281c2e86691STristram Ha 
282c2e86691STristram Ha static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
283c2e86691STristram Ha {
284ee394feaSMarek Vasut 	unsigned int value;
285ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[1], reg, &value);
286c2e86691STristram Ha 
287ee394feaSMarek Vasut 	*val = value;
288c2e86691STristram Ha 	return ret;
289c2e86691STristram Ha }
290c2e86691STristram Ha 
291c2e86691STristram Ha static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
292c2e86691STristram Ha {
293ee394feaSMarek Vasut 	unsigned int value;
294ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[2], reg, &value);
295c2e86691STristram Ha 
296ee394feaSMarek Vasut 	*val = value;
297c2e86691STristram Ha 	return ret;
298c2e86691STristram Ha }
299c2e86691STristram Ha 
300e66f840cSTristram Ha static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
301e66f840cSTristram Ha {
302e66f840cSTristram Ha 	u32 value[2];
303e66f840cSTristram Ha 	int ret;
304e66f840cSTristram Ha 
305e66f840cSTristram Ha 	ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
306c34f674cSBen Hutchings 	if (!ret)
307c34f674cSBen Hutchings 		*val = (u64)value[0] << 32 | value[1];
308e66f840cSTristram Ha 
309e66f840cSTristram Ha 	return ret;
310e66f840cSTristram Ha }
311e66f840cSTristram Ha 
312c2e86691STristram Ha static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
313c2e86691STristram Ha {
314ee394feaSMarek Vasut 	return regmap_write(dev->regmap[0], reg, value);
315c2e86691STristram Ha }
316c2e86691STristram Ha 
317c2e86691STristram Ha static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
318c2e86691STristram Ha {
319ee394feaSMarek Vasut 	return regmap_write(dev->regmap[1], reg, value);
320c2e86691STristram Ha }
321c2e86691STristram Ha 
322c2e86691STristram Ha static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
323c2e86691STristram Ha {
324ee394feaSMarek Vasut 	return regmap_write(dev->regmap[2], reg, value);
325c2e86691STristram Ha }
326c2e86691STristram Ha 
327e66f840cSTristram Ha static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
328e66f840cSTristram Ha {
329e66f840cSTristram Ha 	u32 val[2];
330e66f840cSTristram Ha 
331e66f840cSTristram Ha 	/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
332e66f840cSTristram Ha 	value = swab64(value);
333e66f840cSTristram Ha 	val[0] = swab32(value & 0xffffffffULL);
334e66f840cSTristram Ha 	val[1] = swab32(value >> 32ULL);
335e66f840cSTristram Ha 
336e66f840cSTristram Ha 	return regmap_bulk_write(dev->regmap[2], reg, val, 2);
337e66f840cSTristram Ha }
338e66f840cSTristram Ha 
339c2e86691STristram Ha static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
340c2e86691STristram Ha 			      u8 *data)
341c2e86691STristram Ha {
342c2e86691STristram Ha 	ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
343c2e86691STristram Ha }
344c2e86691STristram Ha 
345c2e86691STristram Ha static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
346c2e86691STristram Ha 			       u16 *data)
347c2e86691STristram Ha {
348c2e86691STristram Ha 	ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
349c2e86691STristram Ha }
350c2e86691STristram Ha 
351c2e86691STristram Ha static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
352c2e86691STristram Ha 			       u32 *data)
353c2e86691STristram Ha {
354c2e86691STristram Ha 	ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
355c2e86691STristram Ha }
356c2e86691STristram Ha 
357c2e86691STristram Ha static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
358c2e86691STristram Ha 			       u8 data)
359c2e86691STristram Ha {
360c2e86691STristram Ha 	ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
361c2e86691STristram Ha }
362c2e86691STristram Ha 
363c2e86691STristram Ha static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
364c2e86691STristram Ha 				u16 data)
365c2e86691STristram Ha {
366c2e86691STristram Ha 	ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset), data);
367c2e86691STristram Ha }
368c2e86691STristram Ha 
369c2e86691STristram Ha static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
370c2e86691STristram Ha 				u32 data)
371c2e86691STristram Ha {
372c2e86691STristram Ha 	ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data);
373c2e86691STristram Ha }
374c2e86691STristram Ha 
375013572a2SMarek Vasut static inline void ksz_regmap_lock(void *__mtx)
376013572a2SMarek Vasut {
377013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
378013572a2SMarek Vasut 	mutex_lock(mtx);
379013572a2SMarek Vasut }
380013572a2SMarek Vasut 
381013572a2SMarek Vasut static inline void ksz_regmap_unlock(void *__mtx)
382013572a2SMarek Vasut {
383013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
384013572a2SMarek Vasut 	mutex_unlock(mtx);
385013572a2SMarek Vasut }
386013572a2SMarek Vasut 
387de6dd626SArun Ramadoss /* STP State Defines */
388de6dd626SArun Ramadoss #define PORT_TX_ENABLE			BIT(2)
389de6dd626SArun Ramadoss #define PORT_RX_ENABLE			BIT(1)
390de6dd626SArun Ramadoss #define PORT_LEARN_DISABLE		BIT(0)
391de6dd626SArun Ramadoss 
39291a98917SArun Ramadoss /* Switch ID Defines */
39391a98917SArun Ramadoss #define REG_CHIP_ID0			0x00
39491a98917SArun Ramadoss 
39591a98917SArun Ramadoss #define SW_FAMILY_ID_M			GENMASK(15, 8)
39691a98917SArun Ramadoss #define KSZ87_FAMILY_ID			0x87
39791a98917SArun Ramadoss #define KSZ88_FAMILY_ID			0x88
39891a98917SArun Ramadoss 
39991a98917SArun Ramadoss #define KSZ8_PORT_STATUS_0		0x08
40091a98917SArun Ramadoss #define KSZ8_PORT_FIBER_MODE		BIT(7)
40191a98917SArun Ramadoss 
40291a98917SArun Ramadoss #define SW_CHIP_ID_M			GENMASK(7, 4)
40391a98917SArun Ramadoss #define KSZ87_CHIP_ID_94		0x6
40491a98917SArun Ramadoss #define KSZ87_CHIP_ID_95		0x9
40591a98917SArun Ramadoss #define KSZ88_CHIP_ID_63		0x3
40691a98917SArun Ramadoss 
40791a98917SArun Ramadoss #define SW_REV_ID_M			GENMASK(7, 4)
40891a98917SArun Ramadoss 
409255b59adSMarek Vasut /* Regmap tables generation */
410255b59adSMarek Vasut #define KSZ_SPI_OP_RD		3
411255b59adSMarek Vasut #define KSZ_SPI_OP_WR		2
412255b59adSMarek Vasut 
41320e03777STristram Ha #define swabnot_used(x)		0
41420e03777STristram Ha 
415255b59adSMarek Vasut #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)		\
416255b59adSMarek Vasut 	swab##swp((opcode) << ((regbits) + (regpad)))
417255b59adSMarek Vasut 
418255b59adSMarek Vasut #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)		\
419255b59adSMarek Vasut 	{								\
4205f81d545SGeorge McCollister 		.name = #width,						\
421255b59adSMarek Vasut 		.val_bits = (width),					\
422a3aa6e65SMarek Vasut 		.reg_stride = 1,					\
423255b59adSMarek Vasut 		.reg_bits = (regbits) + (regalign),			\
424255b59adSMarek Vasut 		.pad_bits = (regpad),					\
425255b59adSMarek Vasut 		.max_register = BIT(regbits) - 1,			\
426255b59adSMarek Vasut 		.cache_type = REGCACHE_NONE,				\
427255b59adSMarek Vasut 		.read_flag_mask =					\
428255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,	\
429255b59adSMarek Vasut 					     regbits, regpad),		\
430255b59adSMarek Vasut 		.write_flag_mask =					\
431255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
432255b59adSMarek Vasut 					     regbits, regpad),		\
433013572a2SMarek Vasut 		.lock = ksz_regmap_lock,				\
434013572a2SMarek Vasut 		.unlock = ksz_regmap_unlock,				\
435255b59adSMarek Vasut 		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
436255b59adSMarek Vasut 		.val_format_endian = REGMAP_ENDIAN_BIG			\
437255b59adSMarek Vasut 	}
438255b59adSMarek Vasut 
439255b59adSMarek Vasut #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)		\
440255b59adSMarek Vasut 	static const struct regmap_config ksz##_regmap_config[] = {	\
441255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
442255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
443255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
444255b59adSMarek Vasut 	}
445255b59adSMarek Vasut 
446c2e86691STristram Ha #endif
447