xref: /openbmc/linux/drivers/net/dsa/microchip/ksz_common.h (revision 978f1f72460cb2dc742a35bae4de50b6b9ac653f)
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>
16c9cd961cSArun Ramadoss #include <linux/irq.h>
176a7abc61SMarek Vasut 
1865ac79e1SArun Ramadoss #define KSZ_MAX_NUM_PORTS 8
1965ac79e1SArun Ramadoss 
20f3c16545SArun Ramadoss struct ksz_device;
21f3c16545SArun Ramadoss 
226a7abc61SMarek Vasut struct vlan_table {
236a7abc61SMarek Vasut 	u32 table[3];
246a7abc61SMarek Vasut };
256a7abc61SMarek Vasut 
266a7abc61SMarek Vasut struct ksz_port_mib {
276a7abc61SMarek Vasut 	struct mutex cnt_mutex;		/* structure access */
286a7abc61SMarek Vasut 	u8 cnt_ptr;
296a7abc61SMarek Vasut 	u64 *counters;
30a7f4f13aSOleksij Rempel 	struct rtnl_link_stats64 stats64;
31c4748ff6SOleksij Rempel 	struct ethtool_pause_stats pause_stats;
32a7f4f13aSOleksij Rempel 	struct spinlock stats64_lock;
336a7abc61SMarek Vasut };
346a7abc61SMarek Vasut 
35a530e6f2SArun Ramadoss struct ksz_mib_names {
36a530e6f2SArun Ramadoss 	int index;
37a530e6f2SArun Ramadoss 	char string[ETH_GSTRING_LEN];
38a530e6f2SArun Ramadoss };
39a530e6f2SArun Ramadoss 
40462d5250SArun Ramadoss struct ksz_chip_data {
41462d5250SArun Ramadoss 	u32 chip_id;
42462d5250SArun Ramadoss 	const char *dev_name;
43462d5250SArun Ramadoss 	int num_vlans;
44462d5250SArun Ramadoss 	int num_alus;
45462d5250SArun Ramadoss 	int num_statics;
46462d5250SArun Ramadoss 	int cpu_ports;
47462d5250SArun Ramadoss 	int port_cnt;
48*978f1f72SArun Ramadoss 	u8 port_nirqs;
496ec23aaaSArun Ramadoss 	const struct ksz_dev_ops *ops;
50462d5250SArun Ramadoss 	bool phy_errata_9477;
51462d5250SArun Ramadoss 	bool ksz87xx_eee_link_erratum;
52a530e6f2SArun Ramadoss 	const struct ksz_mib_names *mib_names;
53a530e6f2SArun Ramadoss 	int mib_cnt;
54a530e6f2SArun Ramadoss 	u8 reg_mib_cnt;
55a02579dfSArun Ramadoss 	const u16 *regs;
56d23a5e18SArun Ramadoss 	const u32 *masks;
5734e48383SArun Ramadoss 	const u8 *shifts;
58aa5b8b73SArun Ramadoss 	const u8 *xmii_ctrl0;
5946f80fa8SArun Ramadoss 	const u8 *xmii_ctrl1;
60e593df51SArun Ramadoss 	int stp_ctrl_reg;
611ca6437fSArun Ramadoss 	int broadcast_ctrl_reg;
620abab9f3SArun Ramadoss 	int multicast_ctrl_reg;
63ad08ac18SArun Ramadoss 	int start_ctrl_reg;
6465ac79e1SArun Ramadoss 	bool supports_mii[KSZ_MAX_NUM_PORTS];
6565ac79e1SArun Ramadoss 	bool supports_rmii[KSZ_MAX_NUM_PORTS];
6665ac79e1SArun Ramadoss 	bool supports_rgmii[KSZ_MAX_NUM_PORTS];
6765ac79e1SArun Ramadoss 	bool internal_phy[KSZ_MAX_NUM_PORTS];
68505bf320SOleksij Rempel 	bool gbit_capable[KSZ_MAX_NUM_PORTS];
69ec6ba50cSOleksij Rempel 	const struct regmap_access_table *wr_table;
70ec6ba50cSOleksij Rempel 	const struct regmap_access_table *rd_table;
71462d5250SArun Ramadoss };
72462d5250SArun Ramadoss 
73c9cd961cSArun Ramadoss struct ksz_irq {
74c9cd961cSArun Ramadoss 	u16 masked;
75c9cd961cSArun Ramadoss 	struct irq_chip chip;
76c9cd961cSArun Ramadoss 	struct irq_domain *domain;
77c9cd961cSArun Ramadoss 	int nirqs;
78c9cd961cSArun Ramadoss 	char name[16];
79c9cd961cSArun Ramadoss };
80c9cd961cSArun Ramadoss 
816a7abc61SMarek Vasut struct ksz_port {
828f4f58f8SBen Hutchings 	bool remove_tag;		/* Remove Tag flag set, for ksz8795 only */
8315f7cfaeSVladimir Oltean 	bool learning;
846a7abc61SMarek Vasut 	int stp_state;
856a7abc61SMarek Vasut 	struct phy_device phydev;
866a7abc61SMarek Vasut 
876a7abc61SMarek Vasut 	u32 on:1;			/* port is not disabled by hardware */
886a7abc61SMarek Vasut 	u32 fiber:1;			/* port is fiber */
896a7abc61SMarek Vasut 	u32 force:1;
906a7abc61SMarek Vasut 	u32 read:1;			/* read MIB counters in background */
916a7abc61SMarek Vasut 	u32 freeze:1;			/* MIB counter freeze is enabled */
926a7abc61SMarek Vasut 
936a7abc61SMarek Vasut 	struct ksz_port_mib mib;
94edecfa98SHelmut Grohne 	phy_interface_t interface;
95e18058eaSOleksij Rempel 	u16 max_frame;
96b19ac41fSArun Ramadoss 	u32 rgmii_tx_val;
97b19ac41fSArun Ramadoss 	u32 rgmii_rx_val;
98f3c16545SArun Ramadoss 	struct ksz_device *ksz_dev;
99c9cd961cSArun Ramadoss 	struct ksz_irq pirq;
100f3c16545SArun Ramadoss 	u8 num;
1016a7abc61SMarek Vasut };
1026a7abc61SMarek Vasut 
1036a7abc61SMarek Vasut struct ksz_device {
1046a7abc61SMarek Vasut 	struct dsa_switch *ds;
1056a7abc61SMarek Vasut 	struct ksz_platform_data *pdata;
106462d5250SArun Ramadoss 	const struct ksz_chip_data *info;
1076a7abc61SMarek Vasut 
1086a7abc61SMarek Vasut 	struct mutex dev_mutex;		/* device access */
109013572a2SMarek Vasut 	struct mutex regmap_mutex;	/* regmap access */
1106a7abc61SMarek Vasut 	struct mutex alu_mutex;		/* ALU access */
1116a7abc61SMarek Vasut 	struct mutex vlan_mutex;	/* vlan access */
1126a7abc61SMarek Vasut 	const struct ksz_dev_ops *dev_ops;
1136a7abc61SMarek Vasut 
1146a7abc61SMarek Vasut 	struct device *dev;
1156a7abc61SMarek Vasut 	struct regmap *regmap[3];
1166a7abc61SMarek Vasut 
1176a7abc61SMarek Vasut 	void *priv;
118c9cd961cSArun Ramadoss 	int irq;
1196a7abc61SMarek Vasut 
1206a7abc61SMarek Vasut 	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
1216a7abc61SMarek Vasut 
1226a7abc61SMarek Vasut 	/* chip specific data */
1236a7abc61SMarek Vasut 	u32 chip_id;
12491a98917SArun Ramadoss 	u8 chip_rev;
1256a7abc61SMarek Vasut 	int cpu_port;			/* port connected to CPU */
1266a7abc61SMarek Vasut 	int phy_port_cnt;
127edecfa98SHelmut Grohne 	phy_interface_t compat_interface;
1286a7abc61SMarek Vasut 	bool synclko_125;
12948bf8b8aSRobert Hancock 	bool synclko_disable;
1306a7abc61SMarek Vasut 
1316a7abc61SMarek Vasut 	struct vlan_table *vlan_cache;
1326a7abc61SMarek Vasut 
1336a7abc61SMarek Vasut 	struct ksz_port *ports;
134469b390eSGeorge McCollister 	struct delayed_work mib_read;
1356a7abc61SMarek Vasut 	unsigned long mib_read_interval;
1366a7abc61SMarek Vasut 	u16 mirror_rx;
1376a7abc61SMarek Vasut 	u16 mirror_tx;
1386a7abc61SMarek Vasut 	u16 port_mask;
139c9cd961cSArun Ramadoss 	struct mutex lock_irq;		/* IRQ Access */
140c9cd961cSArun Ramadoss 	struct ksz_irq girq;
1416a7abc61SMarek Vasut };
1426a7abc61SMarek Vasut 
143462d5250SArun Ramadoss /* List of supported models */
144462d5250SArun Ramadoss enum ksz_model {
145b4490809SOleksij Rempel 	KSZ8563,
146462d5250SArun Ramadoss 	KSZ8795,
147462d5250SArun Ramadoss 	KSZ8794,
148462d5250SArun Ramadoss 	KSZ8765,
149462d5250SArun Ramadoss 	KSZ8830,
150462d5250SArun Ramadoss 	KSZ9477,
1512eb3ff3cSRomain Naour 	KSZ9896,
152462d5250SArun Ramadoss 	KSZ9897,
153462d5250SArun Ramadoss 	KSZ9893,
154462d5250SArun Ramadoss 	KSZ9567,
155462d5250SArun Ramadoss 	LAN9370,
156462d5250SArun Ramadoss 	LAN9371,
157462d5250SArun Ramadoss 	LAN9372,
158462d5250SArun Ramadoss 	LAN9373,
159462d5250SArun Ramadoss 	LAN9374,
160462d5250SArun Ramadoss };
161462d5250SArun Ramadoss 
162462d5250SArun Ramadoss enum ksz_chip_id {
163b4490809SOleksij Rempel 	KSZ8563_CHIP_ID = 0x8563,
164462d5250SArun Ramadoss 	KSZ8795_CHIP_ID = 0x8795,
165462d5250SArun Ramadoss 	KSZ8794_CHIP_ID = 0x8794,
166462d5250SArun Ramadoss 	KSZ8765_CHIP_ID = 0x8765,
167462d5250SArun Ramadoss 	KSZ8830_CHIP_ID = 0x8830,
168462d5250SArun Ramadoss 	KSZ9477_CHIP_ID = 0x00947700,
1692eb3ff3cSRomain Naour 	KSZ9896_CHIP_ID = 0x00989600,
170462d5250SArun Ramadoss 	KSZ9897_CHIP_ID = 0x00989700,
171462d5250SArun Ramadoss 	KSZ9893_CHIP_ID = 0x00989300,
172462d5250SArun Ramadoss 	KSZ9567_CHIP_ID = 0x00956700,
173462d5250SArun Ramadoss 	LAN9370_CHIP_ID = 0x00937000,
174462d5250SArun Ramadoss 	LAN9371_CHIP_ID = 0x00937100,
175462d5250SArun Ramadoss 	LAN9372_CHIP_ID = 0x00937200,
176462d5250SArun Ramadoss 	LAN9373_CHIP_ID = 0x00937300,
177462d5250SArun Ramadoss 	LAN9374_CHIP_ID = 0x00937400,
178462d5250SArun Ramadoss };
179462d5250SArun Ramadoss 
180486f9ca7SArun Ramadoss enum ksz_regs {
181486f9ca7SArun Ramadoss 	REG_IND_CTRL_0,
182486f9ca7SArun Ramadoss 	REG_IND_DATA_8,
183486f9ca7SArun Ramadoss 	REG_IND_DATA_CHECK,
184486f9ca7SArun Ramadoss 	REG_IND_DATA_HI,
185486f9ca7SArun Ramadoss 	REG_IND_DATA_LO,
186486f9ca7SArun Ramadoss 	REG_IND_MIB_CHECK,
187486f9ca7SArun Ramadoss 	REG_IND_BYTE,
188486f9ca7SArun Ramadoss 	P_FORCE_CTRL,
189486f9ca7SArun Ramadoss 	P_LINK_STATUS,
190486f9ca7SArun Ramadoss 	P_LOCAL_CTRL,
191486f9ca7SArun Ramadoss 	P_NEG_RESTART_CTRL,
192486f9ca7SArun Ramadoss 	P_REMOTE_STATUS,
193486f9ca7SArun Ramadoss 	P_SPEED_STATUS,
194486f9ca7SArun Ramadoss 	S_TAIL_TAG_CTRL,
1956877102fSArun Ramadoss 	P_STP_CTRL,
1969d95329cSArun Ramadoss 	S_START_CTRL,
1979d95329cSArun Ramadoss 	S_BROADCAST_CTRL,
1989d95329cSArun Ramadoss 	S_MULTICAST_CTRL,
199aa5b8b73SArun Ramadoss 	P_XMII_CTRL_0,
20046f80fa8SArun Ramadoss 	P_XMII_CTRL_1,
201486f9ca7SArun Ramadoss };
202486f9ca7SArun Ramadoss 
203d23a5e18SArun Ramadoss enum ksz_masks {
204d23a5e18SArun Ramadoss 	PORT_802_1P_REMAPPING,
205d23a5e18SArun Ramadoss 	SW_TAIL_TAG_ENABLE,
206d23a5e18SArun Ramadoss 	MIB_COUNTER_OVERFLOW,
207d23a5e18SArun Ramadoss 	MIB_COUNTER_VALID,
208d23a5e18SArun Ramadoss 	VLAN_TABLE_FID,
209d23a5e18SArun Ramadoss 	VLAN_TABLE_MEMBERSHIP,
210d23a5e18SArun Ramadoss 	VLAN_TABLE_VALID,
211d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_VALID,
212d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_USE_FID,
213d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_FID,
214d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_OVERRIDE,
215d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_FWD_PORTS,
216d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_ENTRIES_H,
217d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_MAC_EMPTY,
218d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_NOT_READY,
219d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_ENTRIES,
220d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_FID,
221d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_SRC_PORT,
222d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_TIMESTAMP,
223457c182aSArun Ramadoss 	ALU_STAT_WRITE,
224457c182aSArun Ramadoss 	ALU_STAT_READ,
2258560664fSArun Ramadoss 	P_MII_TX_FLOW_CTRL,
2268560664fSArun Ramadoss 	P_MII_RX_FLOW_CTRL,
227d23a5e18SArun Ramadoss };
228d23a5e18SArun Ramadoss 
22934e48383SArun Ramadoss enum ksz_shifts {
23034e48383SArun Ramadoss 	VLAN_TABLE_MEMBERSHIP_S,
23134e48383SArun Ramadoss 	VLAN_TABLE,
23234e48383SArun Ramadoss 	STATIC_MAC_FWD_PORTS,
23334e48383SArun Ramadoss 	STATIC_MAC_FID,
23434e48383SArun Ramadoss 	DYNAMIC_MAC_ENTRIES_H,
23534e48383SArun Ramadoss 	DYNAMIC_MAC_ENTRIES,
23634e48383SArun Ramadoss 	DYNAMIC_MAC_FID,
23734e48383SArun Ramadoss 	DYNAMIC_MAC_TIMESTAMP,
23834e48383SArun Ramadoss 	DYNAMIC_MAC_SRC_PORT,
239457c182aSArun Ramadoss 	ALU_STAT_INDEX,
24034e48383SArun Ramadoss };
24134e48383SArun Ramadoss 
242aa5b8b73SArun Ramadoss enum ksz_xmii_ctrl0 {
243aa5b8b73SArun Ramadoss 	P_MII_100MBIT,
244aa5b8b73SArun Ramadoss 	P_MII_10MBIT,
2458560664fSArun Ramadoss 	P_MII_FULL_DUPLEX,
2468560664fSArun Ramadoss 	P_MII_HALF_DUPLEX,
247aa5b8b73SArun Ramadoss };
248aa5b8b73SArun Ramadoss 
24946f80fa8SArun Ramadoss enum ksz_xmii_ctrl1 {
250dc1c596eSArun Ramadoss 	P_RGMII_SEL,
251dc1c596eSArun Ramadoss 	P_RMII_SEL,
252dc1c596eSArun Ramadoss 	P_GMII_SEL,
253dc1c596eSArun Ramadoss 	P_MII_SEL,
25446f80fa8SArun Ramadoss 	P_GMII_1GBIT,
25546f80fa8SArun Ramadoss 	P_GMII_NOT_1GBIT,
25646f80fa8SArun Ramadoss };
25746f80fa8SArun Ramadoss 
2586a7abc61SMarek Vasut struct alu_struct {
2596a7abc61SMarek Vasut 	/* entry 1 */
2606a7abc61SMarek Vasut 	u8	is_static:1;
2616a7abc61SMarek Vasut 	u8	is_src_filter:1;
2626a7abc61SMarek Vasut 	u8	is_dst_filter:1;
2636a7abc61SMarek Vasut 	u8	prio_age:3;
2646a7abc61SMarek Vasut 	u32	_reserv_0_1:23;
2656a7abc61SMarek Vasut 	u8	mstp:3;
2666a7abc61SMarek Vasut 	/* entry 2 */
2676a7abc61SMarek Vasut 	u8	is_override:1;
2686a7abc61SMarek Vasut 	u8	is_use_fid:1;
2696a7abc61SMarek Vasut 	u32	_reserv_1_1:23;
2706a7abc61SMarek Vasut 	u8	port_forward:7;
2716a7abc61SMarek Vasut 	/* entry 3 & 4*/
2726a7abc61SMarek Vasut 	u32	_reserv_2_1:9;
2736a7abc61SMarek Vasut 	u8	fid:7;
2746a7abc61SMarek Vasut 	u8	mac[ETH_ALEN];
2756a7abc61SMarek Vasut };
2766a7abc61SMarek Vasut 
2776a7abc61SMarek Vasut struct ksz_dev_ops {
278d2822e68SArun Ramadoss 	int (*setup)(struct dsa_switch *ds);
279c9cd961cSArun Ramadoss 	void (*teardown)(struct dsa_switch *ds);
2806a7abc61SMarek Vasut 	u32 (*get_port_addr)(int port, int offset);
2816a7abc61SMarek Vasut 	void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
2826a7abc61SMarek Vasut 	void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
2836a7abc61SMarek Vasut 	void (*port_cleanup)(struct ksz_device *dev, int port);
2846a7abc61SMarek Vasut 	void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
2852c119d99SArun Ramadoss 	int (*set_ageing_time)(struct ksz_device *dev, unsigned int msecs);
2868f420456SOleksij Rempel 	int (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
2878f420456SOleksij Rempel 	int (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
2886a7abc61SMarek Vasut 	void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
2896a7abc61SMarek Vasut 			  u64 *cnt);
2906a7abc61SMarek Vasut 	void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
2916a7abc61SMarek Vasut 			  u64 *dropped, u64 *cnt);
292a7f4f13aSOleksij Rempel 	void (*r_mib_stat64)(struct ksz_device *dev, int port);
293f0d997e3SArun Ramadoss 	int  (*vlan_filtering)(struct ksz_device *dev, int port,
294f0d997e3SArun Ramadoss 			       bool flag, struct netlink_ext_ack *extack);
295f0d997e3SArun Ramadoss 	int  (*vlan_add)(struct ksz_device *dev, int port,
296f0d997e3SArun Ramadoss 			 const struct switchdev_obj_port_vlan *vlan,
297f0d997e3SArun Ramadoss 			 struct netlink_ext_ack *extack);
298f0d997e3SArun Ramadoss 	int  (*vlan_del)(struct ksz_device *dev, int port,
299f0d997e3SArun Ramadoss 			 const struct switchdev_obj_port_vlan *vlan);
30000a298bbSArun Ramadoss 	int (*mirror_add)(struct ksz_device *dev, int port,
30100a298bbSArun Ramadoss 			  struct dsa_mall_mirror_tc_entry *mirror,
30200a298bbSArun Ramadoss 			  bool ingress, struct netlink_ext_ack *extack);
30300a298bbSArun Ramadoss 	void (*mirror_del)(struct ksz_device *dev, int port,
30400a298bbSArun Ramadoss 			   struct dsa_mall_mirror_tc_entry *mirror);
305e587be75SArun Ramadoss 	int (*fdb_add)(struct ksz_device *dev, int port,
306e587be75SArun Ramadoss 		       const unsigned char *addr, u16 vid, struct dsa_db db);
307e587be75SArun Ramadoss 	int (*fdb_del)(struct ksz_device *dev, int port,
308e587be75SArun Ramadoss 		       const unsigned char *addr, u16 vid, struct dsa_db db);
309e587be75SArun Ramadoss 	int (*fdb_dump)(struct ksz_device *dev, int port,
310e587be75SArun Ramadoss 			dsa_fdb_dump_cb_t *cb, void *data);
311980c7d17SArun Ramadoss 	int (*mdb_add)(struct ksz_device *dev, int port,
312980c7d17SArun Ramadoss 		       const struct switchdev_obj_port_mdb *mdb,
313980c7d17SArun Ramadoss 		       struct dsa_db db);
314980c7d17SArun Ramadoss 	int (*mdb_del)(struct ksz_device *dev, int port,
315980c7d17SArun Ramadoss 		       const struct switchdev_obj_port_mdb *mdb,
316980c7d17SArun Ramadoss 		       struct dsa_db db);
3177012033cSArun Ramadoss 	void (*get_caps)(struct ksz_device *dev, int port,
3187012033cSArun Ramadoss 			 struct phylink_config *config);
3191fe94f54SArun Ramadoss 	int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
3201fe94f54SArun Ramadoss 	int (*max_mtu)(struct ksz_device *dev, int port);
3216a7abc61SMarek Vasut 	void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
3226a7abc61SMarek Vasut 	void (*port_init_cnt)(struct ksz_device *dev, int port);
323a0cb1aa4SArun Ramadoss 	void (*phylink_mac_config)(struct ksz_device *dev, int port,
324a0cb1aa4SArun Ramadoss 				   unsigned int mode,
325a0cb1aa4SArun Ramadoss 				   const struct phylink_link_state *state);
326f597d3adSArun Ramadoss 	void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
327f597d3adSArun Ramadoss 				    unsigned int mode,
328f597d3adSArun Ramadoss 				    phy_interface_t interface,
329f597d3adSArun Ramadoss 				    struct phy_device *phydev, int speed,
330f597d3adSArun Ramadoss 				    int duplex, bool tx_pause, bool rx_pause);
331b19ac41fSArun Ramadoss 	void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
332fb9324beSArun Ramadoss 	void (*config_cpu_port)(struct dsa_switch *ds);
333331d64f7SArun Ramadoss 	int (*enable_stp_addr)(struct ksz_device *dev);
334673b196fSArun Ramadoss 	int (*reset)(struct ksz_device *dev);
3356a7abc61SMarek Vasut 	int (*init)(struct ksz_device *dev);
3366a7abc61SMarek Vasut 	void (*exit)(struct ksz_device *dev);
3376a7abc61SMarek Vasut };
3386a7abc61SMarek Vasut 
3396a7abc61SMarek Vasut struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
3406ec23aaaSArun Ramadoss int ksz_switch_register(struct ksz_device *dev);
3416a7abc61SMarek Vasut void ksz_switch_remove(struct ksz_device *dev);
3426a7abc61SMarek Vasut 
3437c6ff470STristram Ha void ksz_init_mib_timer(struct ksz_device *dev);
344c6101dd7SArun Ramadoss void ksz_r_mib_stats64(struct ksz_device *dev, int port);
345e593df51SArun Ramadoss void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
34646f80fa8SArun Ramadoss bool ksz_get_gbit(struct ksz_device *dev, int port);
3470ab7f6bfSArun Ramadoss phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit);
3481958eee8SArun Ramadoss extern const struct ksz_chip_data ksz_switch_chips[];
349c2e86691STristram Ha 
350c2e86691STristram Ha /* Common register access functions */
351c2e86691STristram Ha 
352c2e86691STristram Ha static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
353c2e86691STristram Ha {
354ee394feaSMarek Vasut 	unsigned int value;
355ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[0], reg, &value);
356c2e86691STristram Ha 
357ec6ba50cSOleksij Rempel 	if (ret)
358ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't read 8bit reg: 0x%x %pe\n", reg,
359ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
360ec6ba50cSOleksij Rempel 
361ee394feaSMarek Vasut 	*val = value;
362c2e86691STristram Ha 	return ret;
363c2e86691STristram Ha }
364c2e86691STristram Ha 
365c2e86691STristram Ha static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
366c2e86691STristram Ha {
367ee394feaSMarek Vasut 	unsigned int value;
368ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[1], reg, &value);
369c2e86691STristram Ha 
370ec6ba50cSOleksij Rempel 	if (ret)
371ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't read 16bit reg: 0x%x %pe\n", reg,
372ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
373ec6ba50cSOleksij Rempel 
374ee394feaSMarek Vasut 	*val = value;
375c2e86691STristram Ha 	return ret;
376c2e86691STristram Ha }
377c2e86691STristram Ha 
378c2e86691STristram Ha static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
379c2e86691STristram Ha {
380ee394feaSMarek Vasut 	unsigned int value;
381ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[2], reg, &value);
382c2e86691STristram Ha 
383ec6ba50cSOleksij Rempel 	if (ret)
384ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't read 32bit reg: 0x%x %pe\n", reg,
385ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
386ec6ba50cSOleksij Rempel 
387ee394feaSMarek Vasut 	*val = value;
388c2e86691STristram Ha 	return ret;
389c2e86691STristram Ha }
390c2e86691STristram Ha 
391e66f840cSTristram Ha static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
392e66f840cSTristram Ha {
393e66f840cSTristram Ha 	u32 value[2];
394e66f840cSTristram Ha 	int ret;
395e66f840cSTristram Ha 
396e66f840cSTristram Ha 	ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
397ec6ba50cSOleksij Rempel 	if (ret)
398ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't read 64bit reg: 0x%x %pe\n", reg,
399ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
400ec6ba50cSOleksij Rempel 	else
401c34f674cSBen Hutchings 		*val = (u64)value[0] << 32 | value[1];
402e66f840cSTristram Ha 
403e66f840cSTristram Ha 	return ret;
404e66f840cSTristram Ha }
405e66f840cSTristram Ha 
406c2e86691STristram Ha static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
407c2e86691STristram Ha {
408ec6ba50cSOleksij Rempel 	int ret;
409ec6ba50cSOleksij Rempel 
410ec6ba50cSOleksij Rempel 	ret = regmap_write(dev->regmap[0], reg, value);
411ec6ba50cSOleksij Rempel 	if (ret)
412ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't write 8bit reg: 0x%x %pe\n", reg,
413ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
414ec6ba50cSOleksij Rempel 
415ec6ba50cSOleksij Rempel 	return ret;
416c2e86691STristram Ha }
417c2e86691STristram Ha 
418c2e86691STristram Ha static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
419c2e86691STristram Ha {
420ec6ba50cSOleksij Rempel 	int ret;
421ec6ba50cSOleksij Rempel 
422ec6ba50cSOleksij Rempel 	ret = regmap_write(dev->regmap[1], reg, value);
423ec6ba50cSOleksij Rempel 	if (ret)
424ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't write 16bit reg: 0x%x %pe\n", reg,
425ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
426ec6ba50cSOleksij Rempel 
427ec6ba50cSOleksij Rempel 	return ret;
428c2e86691STristram Ha }
429c2e86691STristram Ha 
430c2e86691STristram Ha static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
431c2e86691STristram Ha {
432ec6ba50cSOleksij Rempel 	int ret;
433ec6ba50cSOleksij Rempel 
434ec6ba50cSOleksij Rempel 	ret = regmap_write(dev->regmap[2], reg, value);
435ec6ba50cSOleksij Rempel 	if (ret)
436ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't write 32bit reg: 0x%x %pe\n", reg,
437ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
438ec6ba50cSOleksij Rempel 
439ec6ba50cSOleksij Rempel 	return ret;
440c2e86691STristram Ha }
441c2e86691STristram Ha 
442e66f840cSTristram Ha static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
443e66f840cSTristram Ha {
444e66f840cSTristram Ha 	u32 val[2];
445e66f840cSTristram Ha 
446e66f840cSTristram Ha 	/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
447e66f840cSTristram Ha 	value = swab64(value);
448e66f840cSTristram Ha 	val[0] = swab32(value & 0xffffffffULL);
449e66f840cSTristram Ha 	val[1] = swab32(value >> 32ULL);
450e66f840cSTristram Ha 
451e66f840cSTristram Ha 	return regmap_bulk_write(dev->regmap[2], reg, val, 2);
452e66f840cSTristram Ha }
453e66f840cSTristram Ha 
454d38bc3b4SOleksij Rempel static inline int ksz_pread8(struct ksz_device *dev, int port, int offset,
455c2e86691STristram Ha 			     u8 *data)
456c2e86691STristram Ha {
457d38bc3b4SOleksij Rempel 	return ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
458c2e86691STristram Ha }
459c2e86691STristram Ha 
460d38bc3b4SOleksij Rempel static inline int ksz_pread16(struct ksz_device *dev, int port, int offset,
461c2e86691STristram Ha 			      u16 *data)
462c2e86691STristram Ha {
463d38bc3b4SOleksij Rempel 	return ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
464c2e86691STristram Ha }
465c2e86691STristram Ha 
466d38bc3b4SOleksij Rempel static inline int ksz_pread32(struct ksz_device *dev, int port, int offset,
467c2e86691STristram Ha 			      u32 *data)
468c2e86691STristram Ha {
469d38bc3b4SOleksij Rempel 	return ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
470c2e86691STristram Ha }
471c2e86691STristram Ha 
472d38bc3b4SOleksij Rempel static inline int ksz_pwrite8(struct ksz_device *dev, int port, int offset,
473c2e86691STristram Ha 			      u8 data)
474c2e86691STristram Ha {
475d38bc3b4SOleksij Rempel 	return ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
476c2e86691STristram Ha }
477c2e86691STristram Ha 
478d38bc3b4SOleksij Rempel static inline int ksz_pwrite16(struct ksz_device *dev, int port, int offset,
479c2e86691STristram Ha 			       u16 data)
480c2e86691STristram Ha {
481d38bc3b4SOleksij Rempel 	return ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset),
482d38bc3b4SOleksij Rempel 			   data);
483c2e86691STristram Ha }
484c2e86691STristram Ha 
485d38bc3b4SOleksij Rempel static inline int ksz_pwrite32(struct ksz_device *dev, int port, int offset,
486c2e86691STristram Ha 			       u32 data)
487c2e86691STristram Ha {
488d38bc3b4SOleksij Rempel 	return ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset),
489d38bc3b4SOleksij Rempel 			   data);
490c2e86691STristram Ha }
491c2e86691STristram Ha 
4928560664fSArun Ramadoss static inline void ksz_prmw8(struct ksz_device *dev, int port, int offset,
4938560664fSArun Ramadoss 			     u8 mask, u8 val)
4948560664fSArun Ramadoss {
4958560664fSArun Ramadoss 	regmap_update_bits(dev->regmap[0],
4968560664fSArun Ramadoss 			   dev->dev_ops->get_port_addr(port, offset),
4978560664fSArun Ramadoss 			   mask, val);
4988560664fSArun Ramadoss }
4998560664fSArun Ramadoss 
500013572a2SMarek Vasut static inline void ksz_regmap_lock(void *__mtx)
501013572a2SMarek Vasut {
502013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
503013572a2SMarek Vasut 	mutex_lock(mtx);
504013572a2SMarek Vasut }
505013572a2SMarek Vasut 
506013572a2SMarek Vasut static inline void ksz_regmap_unlock(void *__mtx)
507013572a2SMarek Vasut {
508013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
509013572a2SMarek Vasut 	mutex_unlock(mtx);
510013572a2SMarek Vasut }
511013572a2SMarek Vasut 
512f3d890f5SArun Ramadoss static inline bool ksz_is_ksz88x3(struct ksz_device *dev)
513f3d890f5SArun Ramadoss {
514f3d890f5SArun Ramadoss 	return dev->chip_id == KSZ8830_CHIP_ID;
515f3d890f5SArun Ramadoss }
516f3d890f5SArun Ramadoss 
51799b16df0SArun Ramadoss static inline int is_lan937x(struct ksz_device *dev)
51899b16df0SArun Ramadoss {
51999b16df0SArun Ramadoss 	return dev->chip_id == LAN9370_CHIP_ID ||
52099b16df0SArun Ramadoss 		dev->chip_id == LAN9371_CHIP_ID ||
52199b16df0SArun Ramadoss 		dev->chip_id == LAN9372_CHIP_ID ||
52299b16df0SArun Ramadoss 		dev->chip_id == LAN9373_CHIP_ID ||
52399b16df0SArun Ramadoss 		dev->chip_id == LAN9374_CHIP_ID;
52499b16df0SArun Ramadoss }
52599b16df0SArun Ramadoss 
526de6dd626SArun Ramadoss /* STP State Defines */
527de6dd626SArun Ramadoss #define PORT_TX_ENABLE			BIT(2)
528de6dd626SArun Ramadoss #define PORT_RX_ENABLE			BIT(1)
529de6dd626SArun Ramadoss #define PORT_LEARN_DISABLE		BIT(0)
530de6dd626SArun Ramadoss 
53191a98917SArun Ramadoss /* Switch ID Defines */
53291a98917SArun Ramadoss #define REG_CHIP_ID0			0x00
53391a98917SArun Ramadoss 
53491a98917SArun Ramadoss #define SW_FAMILY_ID_M			GENMASK(15, 8)
53591a98917SArun Ramadoss #define KSZ87_FAMILY_ID			0x87
53691a98917SArun Ramadoss #define KSZ88_FAMILY_ID			0x88
53791a98917SArun Ramadoss 
53891a98917SArun Ramadoss #define KSZ8_PORT_STATUS_0		0x08
53991a98917SArun Ramadoss #define KSZ8_PORT_FIBER_MODE		BIT(7)
54091a98917SArun Ramadoss 
54191a98917SArun Ramadoss #define SW_CHIP_ID_M			GENMASK(7, 4)
54291a98917SArun Ramadoss #define KSZ87_CHIP_ID_94		0x6
54391a98917SArun Ramadoss #define KSZ87_CHIP_ID_95		0x9
54491a98917SArun Ramadoss #define KSZ88_CHIP_ID_63		0x3
54591a98917SArun Ramadoss 
54691a98917SArun Ramadoss #define SW_REV_ID_M			GENMASK(7, 4)
54791a98917SArun Ramadoss 
548b4490809SOleksij Rempel /* KSZ9893, KSZ9563, KSZ8563 specific register  */
549b4490809SOleksij Rempel #define REG_CHIP_ID4			0x0f
550b4490809SOleksij Rempel #define SKU_ID_KSZ8563			0x3c
551b4490809SOleksij Rempel 
5521ca6437fSArun Ramadoss /* Driver set switch broadcast storm protection at 10% rate. */
5531ca6437fSArun Ramadoss #define BROADCAST_STORM_PROT_RATE	10
5541ca6437fSArun Ramadoss 
5551ca6437fSArun Ramadoss /* 148,800 frames * 67 ms / 100 */
5561ca6437fSArun Ramadoss #define BROADCAST_STORM_VALUE		9969
5571ca6437fSArun Ramadoss 
5581ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE_HI		0x07
5591ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE_LO		0xFF
5601ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE		0x07FF
5611ca6437fSArun Ramadoss 
5620abab9f3SArun Ramadoss #define MULTICAST_STORM_DISABLE		BIT(6)
5630abab9f3SArun Ramadoss 
564ad08ac18SArun Ramadoss #define SW_START			0x01
565ad08ac18SArun Ramadoss 
56646f80fa8SArun Ramadoss /* xMII configuration */
5678560664fSArun Ramadoss #define P_MII_DUPLEX_M			BIT(6)
568aa5b8b73SArun Ramadoss #define P_MII_100MBIT_M			BIT(4)
569aa5b8b73SArun Ramadoss 
57046f80fa8SArun Ramadoss #define P_GMII_1GBIT_M			BIT(6)
571dc1c596eSArun Ramadoss #define P_RGMII_ID_IG_ENABLE		BIT(4)
572dc1c596eSArun Ramadoss #define P_RGMII_ID_EG_ENABLE		BIT(3)
5730ab7f6bfSArun Ramadoss #define P_MII_MAC_MODE			BIT(2)
574dc1c596eSArun Ramadoss #define P_MII_SEL_M			0x3
57546f80fa8SArun Ramadoss 
576255b59adSMarek Vasut /* Regmap tables generation */
577255b59adSMarek Vasut #define KSZ_SPI_OP_RD		3
578255b59adSMarek Vasut #define KSZ_SPI_OP_WR		2
579255b59adSMarek Vasut 
58020e03777STristram Ha #define swabnot_used(x)		0
58120e03777STristram Ha 
582255b59adSMarek Vasut #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)		\
583255b59adSMarek Vasut 	swab##swp((opcode) << ((regbits) + (regpad)))
584255b59adSMarek Vasut 
585255b59adSMarek Vasut #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)		\
586255b59adSMarek Vasut 	{								\
5875f81d545SGeorge McCollister 		.name = #width,						\
588255b59adSMarek Vasut 		.val_bits = (width),					\
589a3aa6e65SMarek Vasut 		.reg_stride = 1,					\
590255b59adSMarek Vasut 		.reg_bits = (regbits) + (regalign),			\
591255b59adSMarek Vasut 		.pad_bits = (regpad),					\
592255b59adSMarek Vasut 		.max_register = BIT(regbits) - 1,			\
593255b59adSMarek Vasut 		.cache_type = REGCACHE_NONE,				\
594255b59adSMarek Vasut 		.read_flag_mask =					\
595255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,	\
596255b59adSMarek Vasut 					     regbits, regpad),		\
597255b59adSMarek Vasut 		.write_flag_mask =					\
598255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
599255b59adSMarek Vasut 					     regbits, regpad),		\
600013572a2SMarek Vasut 		.lock = ksz_regmap_lock,				\
601013572a2SMarek Vasut 		.unlock = ksz_regmap_unlock,				\
602255b59adSMarek Vasut 		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
603255b59adSMarek Vasut 		.val_format_endian = REGMAP_ENDIAN_BIG			\
604255b59adSMarek Vasut 	}
605255b59adSMarek Vasut 
606255b59adSMarek Vasut #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)		\
607255b59adSMarek Vasut 	static const struct regmap_config ksz##_regmap_config[] = {	\
608255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
609255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
610255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
611255b59adSMarek Vasut 	}
612255b59adSMarek Vasut 
613c2e86691STristram Ha #endif
614