xref: /openbmc/linux/drivers/net/dsa/microchip/ksz_common.h (revision cc13ab18b201ab630f03511060ba289b70052959)
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 
18eac1ea20SChristian Eggers #include "ksz_ptp.h"
19eac1ea20SChristian Eggers 
2065ac79e1SArun Ramadoss #define KSZ_MAX_NUM_PORTS 8
2165ac79e1SArun Ramadoss 
22f3c16545SArun Ramadoss struct ksz_device;
23*cc13ab18SArun Ramadoss struct ksz_port;
24f3c16545SArun Ramadoss 
256a7abc61SMarek Vasut struct vlan_table {
266a7abc61SMarek Vasut 	u32 table[3];
276a7abc61SMarek Vasut };
286a7abc61SMarek Vasut 
296a7abc61SMarek Vasut struct ksz_port_mib {
306a7abc61SMarek Vasut 	struct mutex cnt_mutex;		/* structure access */
316a7abc61SMarek Vasut 	u8 cnt_ptr;
326a7abc61SMarek Vasut 	u64 *counters;
33a7f4f13aSOleksij Rempel 	struct rtnl_link_stats64 stats64;
34c4748ff6SOleksij Rempel 	struct ethtool_pause_stats pause_stats;
35a7f4f13aSOleksij Rempel 	struct spinlock stats64_lock;
366a7abc61SMarek Vasut };
376a7abc61SMarek Vasut 
38a530e6f2SArun Ramadoss struct ksz_mib_names {
39a530e6f2SArun Ramadoss 	int index;
40a530e6f2SArun Ramadoss 	char string[ETH_GSTRING_LEN];
41a530e6f2SArun Ramadoss };
42a530e6f2SArun Ramadoss 
43462d5250SArun Ramadoss struct ksz_chip_data {
44462d5250SArun Ramadoss 	u32 chip_id;
45462d5250SArun Ramadoss 	const char *dev_name;
46462d5250SArun Ramadoss 	int num_vlans;
47462d5250SArun Ramadoss 	int num_alus;
48462d5250SArun Ramadoss 	int num_statics;
49462d5250SArun Ramadoss 	int cpu_ports;
50462d5250SArun Ramadoss 	int port_cnt;
51978f1f72SArun Ramadoss 	u8 port_nirqs;
526ec23aaaSArun Ramadoss 	const struct ksz_dev_ops *ops;
53462d5250SArun Ramadoss 	bool phy_errata_9477;
54462d5250SArun Ramadoss 	bool ksz87xx_eee_link_erratum;
55a530e6f2SArun Ramadoss 	const struct ksz_mib_names *mib_names;
56a530e6f2SArun Ramadoss 	int mib_cnt;
57a530e6f2SArun Ramadoss 	u8 reg_mib_cnt;
58a02579dfSArun Ramadoss 	const u16 *regs;
59d23a5e18SArun Ramadoss 	const u32 *masks;
6034e48383SArun Ramadoss 	const u8 *shifts;
61aa5b8b73SArun Ramadoss 	const u8 *xmii_ctrl0;
6246f80fa8SArun Ramadoss 	const u8 *xmii_ctrl1;
63e593df51SArun Ramadoss 	int stp_ctrl_reg;
641ca6437fSArun Ramadoss 	int broadcast_ctrl_reg;
650abab9f3SArun Ramadoss 	int multicast_ctrl_reg;
66ad08ac18SArun Ramadoss 	int start_ctrl_reg;
6765ac79e1SArun Ramadoss 	bool supports_mii[KSZ_MAX_NUM_PORTS];
6865ac79e1SArun Ramadoss 	bool supports_rmii[KSZ_MAX_NUM_PORTS];
6965ac79e1SArun Ramadoss 	bool supports_rgmii[KSZ_MAX_NUM_PORTS];
7065ac79e1SArun Ramadoss 	bool internal_phy[KSZ_MAX_NUM_PORTS];
71505bf320SOleksij Rempel 	bool gbit_capable[KSZ_MAX_NUM_PORTS];
72ec6ba50cSOleksij Rempel 	const struct regmap_access_table *wr_table;
73ec6ba50cSOleksij Rempel 	const struct regmap_access_table *rd_table;
74462d5250SArun Ramadoss };
75462d5250SArun Ramadoss 
76c9cd961cSArun Ramadoss struct ksz_irq {
77c9cd961cSArun Ramadoss 	u16 masked;
78e1add7ddSArun Ramadoss 	u16 reg_mask;
79e1add7ddSArun Ramadoss 	u16 reg_status;
80c9cd961cSArun Ramadoss 	struct irq_domain *domain;
81c9cd961cSArun Ramadoss 	int nirqs;
82e1add7ddSArun Ramadoss 	int irq_num;
83c9cd961cSArun Ramadoss 	char name[16];
84e1add7ddSArun Ramadoss 	struct ksz_device *dev;
85c9cd961cSArun Ramadoss };
86c9cd961cSArun Ramadoss 
87*cc13ab18SArun Ramadoss struct ksz_ptp_irq {
88*cc13ab18SArun Ramadoss 	struct ksz_port *port;
89*cc13ab18SArun Ramadoss 	u16 ts_reg;
90*cc13ab18SArun Ramadoss 	char name[16];
91*cc13ab18SArun Ramadoss 	int num;
92*cc13ab18SArun Ramadoss };
93*cc13ab18SArun Ramadoss 
946a7abc61SMarek Vasut struct ksz_port {
958f4f58f8SBen Hutchings 	bool remove_tag;		/* Remove Tag flag set, for ksz8795 only */
9615f7cfaeSVladimir Oltean 	bool learning;
976a7abc61SMarek Vasut 	int stp_state;
986a7abc61SMarek Vasut 	struct phy_device phydev;
996a7abc61SMarek Vasut 
1006a7abc61SMarek Vasut 	u32 on:1;			/* port is not disabled by hardware */
1016a7abc61SMarek Vasut 	u32 fiber:1;			/* port is fiber */
1026a7abc61SMarek Vasut 	u32 force:1;
1036a7abc61SMarek Vasut 	u32 read:1;			/* read MIB counters in background */
1046a7abc61SMarek Vasut 	u32 freeze:1;			/* MIB counter freeze is enabled */
1056a7abc61SMarek Vasut 
1066a7abc61SMarek Vasut 	struct ksz_port_mib mib;
107edecfa98SHelmut Grohne 	phy_interface_t interface;
108b19ac41fSArun Ramadoss 	u32 rgmii_tx_val;
109b19ac41fSArun Ramadoss 	u32 rgmii_rx_val;
110f3c16545SArun Ramadoss 	struct ksz_device *ksz_dev;
111c9cd961cSArun Ramadoss 	struct ksz_irq pirq;
112f3c16545SArun Ramadoss 	u8 num;
113c59e12a1SChristian Eggers #if IS_ENABLED(CONFIG_NET_DSA_MICROCHIP_KSZ_PTP)
114c59e12a1SChristian Eggers 	struct hwtstamp_config tstamp_config;
115c2977c61SArun Ramadoss 	bool hwts_tx_en;
116c2977c61SArun Ramadoss 	bool hwts_rx_en;
117*cc13ab18SArun Ramadoss 	struct ksz_irq ptpirq;
118*cc13ab18SArun Ramadoss 	struct ksz_ptp_irq ptpmsg_irq[3];
119c59e12a1SChristian Eggers #endif
1206a7abc61SMarek Vasut };
1216a7abc61SMarek Vasut 
1226a7abc61SMarek Vasut struct ksz_device {
1236a7abc61SMarek Vasut 	struct dsa_switch *ds;
1246a7abc61SMarek Vasut 	struct ksz_platform_data *pdata;
125462d5250SArun Ramadoss 	const struct ksz_chip_data *info;
1266a7abc61SMarek Vasut 
1276a7abc61SMarek Vasut 	struct mutex dev_mutex;		/* device access */
128013572a2SMarek Vasut 	struct mutex regmap_mutex;	/* regmap access */
1296a7abc61SMarek Vasut 	struct mutex alu_mutex;		/* ALU access */
1306a7abc61SMarek Vasut 	struct mutex vlan_mutex;	/* vlan access */
1316a7abc61SMarek Vasut 	const struct ksz_dev_ops *dev_ops;
1326a7abc61SMarek Vasut 
1336a7abc61SMarek Vasut 	struct device *dev;
1346a7abc61SMarek Vasut 	struct regmap *regmap[3];
1356a7abc61SMarek Vasut 
1366a7abc61SMarek Vasut 	void *priv;
137c9cd961cSArun Ramadoss 	int irq;
1386a7abc61SMarek Vasut 
1396a7abc61SMarek Vasut 	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
1406a7abc61SMarek Vasut 
1416a7abc61SMarek Vasut 	/* chip specific data */
1426a7abc61SMarek Vasut 	u32 chip_id;
14391a98917SArun Ramadoss 	u8 chip_rev;
1446a7abc61SMarek Vasut 	int cpu_port;			/* port connected to CPU */
1456a7abc61SMarek Vasut 	int phy_port_cnt;
146edecfa98SHelmut Grohne 	phy_interface_t compat_interface;
1476a7abc61SMarek Vasut 	bool synclko_125;
14848bf8b8aSRobert Hancock 	bool synclko_disable;
1496a7abc61SMarek Vasut 
1506a7abc61SMarek Vasut 	struct vlan_table *vlan_cache;
1516a7abc61SMarek Vasut 
1526a7abc61SMarek Vasut 	struct ksz_port *ports;
153469b390eSGeorge McCollister 	struct delayed_work mib_read;
1546a7abc61SMarek Vasut 	unsigned long mib_read_interval;
1556a7abc61SMarek Vasut 	u16 mirror_rx;
1566a7abc61SMarek Vasut 	u16 mirror_tx;
1576a7abc61SMarek Vasut 	u16 port_mask;
158c9cd961cSArun Ramadoss 	struct mutex lock_irq;		/* IRQ Access */
159c9cd961cSArun Ramadoss 	struct ksz_irq girq;
160eac1ea20SChristian Eggers 	struct ksz_ptp_data ptp_data;
1616a7abc61SMarek Vasut };
1626a7abc61SMarek Vasut 
163462d5250SArun Ramadoss /* List of supported models */
164462d5250SArun Ramadoss enum ksz_model {
165b4490809SOleksij Rempel 	KSZ8563,
166462d5250SArun Ramadoss 	KSZ8795,
167462d5250SArun Ramadoss 	KSZ8794,
168462d5250SArun Ramadoss 	KSZ8765,
169462d5250SArun Ramadoss 	KSZ8830,
170462d5250SArun Ramadoss 	KSZ9477,
1712eb3ff3cSRomain Naour 	KSZ9896,
172462d5250SArun Ramadoss 	KSZ9897,
173462d5250SArun Ramadoss 	KSZ9893,
174ef912fe4SRakesh Sankaranarayanan 	KSZ9563,
175462d5250SArun Ramadoss 	KSZ9567,
176462d5250SArun Ramadoss 	LAN9370,
177462d5250SArun Ramadoss 	LAN9371,
178462d5250SArun Ramadoss 	LAN9372,
179462d5250SArun Ramadoss 	LAN9373,
180462d5250SArun Ramadoss 	LAN9374,
181462d5250SArun Ramadoss };
182462d5250SArun Ramadoss 
183462d5250SArun Ramadoss enum ksz_chip_id {
184b4490809SOleksij Rempel 	KSZ8563_CHIP_ID = 0x8563,
185462d5250SArun Ramadoss 	KSZ8795_CHIP_ID = 0x8795,
186462d5250SArun Ramadoss 	KSZ8794_CHIP_ID = 0x8794,
187462d5250SArun Ramadoss 	KSZ8765_CHIP_ID = 0x8765,
188462d5250SArun Ramadoss 	KSZ8830_CHIP_ID = 0x8830,
189462d5250SArun Ramadoss 	KSZ9477_CHIP_ID = 0x00947700,
1902eb3ff3cSRomain Naour 	KSZ9896_CHIP_ID = 0x00989600,
191462d5250SArun Ramadoss 	KSZ9897_CHIP_ID = 0x00989700,
192462d5250SArun Ramadoss 	KSZ9893_CHIP_ID = 0x00989300,
193ef912fe4SRakesh Sankaranarayanan 	KSZ9563_CHIP_ID = 0x00956300,
194462d5250SArun Ramadoss 	KSZ9567_CHIP_ID = 0x00956700,
195462d5250SArun Ramadoss 	LAN9370_CHIP_ID = 0x00937000,
196462d5250SArun Ramadoss 	LAN9371_CHIP_ID = 0x00937100,
197462d5250SArun Ramadoss 	LAN9372_CHIP_ID = 0x00937200,
198462d5250SArun Ramadoss 	LAN9373_CHIP_ID = 0x00937300,
199462d5250SArun Ramadoss 	LAN9374_CHIP_ID = 0x00937400,
200462d5250SArun Ramadoss };
201462d5250SArun Ramadoss 
202486f9ca7SArun Ramadoss enum ksz_regs {
203486f9ca7SArun Ramadoss 	REG_IND_CTRL_0,
204486f9ca7SArun Ramadoss 	REG_IND_DATA_8,
205486f9ca7SArun Ramadoss 	REG_IND_DATA_CHECK,
206486f9ca7SArun Ramadoss 	REG_IND_DATA_HI,
207486f9ca7SArun Ramadoss 	REG_IND_DATA_LO,
208486f9ca7SArun Ramadoss 	REG_IND_MIB_CHECK,
209486f9ca7SArun Ramadoss 	REG_IND_BYTE,
210486f9ca7SArun Ramadoss 	P_FORCE_CTRL,
211486f9ca7SArun Ramadoss 	P_LINK_STATUS,
212486f9ca7SArun Ramadoss 	P_LOCAL_CTRL,
213486f9ca7SArun Ramadoss 	P_NEG_RESTART_CTRL,
214486f9ca7SArun Ramadoss 	P_REMOTE_STATUS,
215486f9ca7SArun Ramadoss 	P_SPEED_STATUS,
216486f9ca7SArun Ramadoss 	S_TAIL_TAG_CTRL,
2176877102fSArun Ramadoss 	P_STP_CTRL,
2189d95329cSArun Ramadoss 	S_START_CTRL,
2199d95329cSArun Ramadoss 	S_BROADCAST_CTRL,
2209d95329cSArun Ramadoss 	S_MULTICAST_CTRL,
221aa5b8b73SArun Ramadoss 	P_XMII_CTRL_0,
22246f80fa8SArun Ramadoss 	P_XMII_CTRL_1,
223486f9ca7SArun Ramadoss };
224486f9ca7SArun Ramadoss 
225d23a5e18SArun Ramadoss enum ksz_masks {
226d23a5e18SArun Ramadoss 	PORT_802_1P_REMAPPING,
227d23a5e18SArun Ramadoss 	SW_TAIL_TAG_ENABLE,
228d23a5e18SArun Ramadoss 	MIB_COUNTER_OVERFLOW,
229d23a5e18SArun Ramadoss 	MIB_COUNTER_VALID,
230d23a5e18SArun Ramadoss 	VLAN_TABLE_FID,
231d23a5e18SArun Ramadoss 	VLAN_TABLE_MEMBERSHIP,
232d23a5e18SArun Ramadoss 	VLAN_TABLE_VALID,
233d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_VALID,
234d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_USE_FID,
235d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_FID,
236d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_OVERRIDE,
237d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_FWD_PORTS,
238d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_ENTRIES_H,
239d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_MAC_EMPTY,
240d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_NOT_READY,
241d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_ENTRIES,
242d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_FID,
243d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_SRC_PORT,
244d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_TIMESTAMP,
245457c182aSArun Ramadoss 	ALU_STAT_WRITE,
246457c182aSArun Ramadoss 	ALU_STAT_READ,
2478560664fSArun Ramadoss 	P_MII_TX_FLOW_CTRL,
2488560664fSArun Ramadoss 	P_MII_RX_FLOW_CTRL,
249d23a5e18SArun Ramadoss };
250d23a5e18SArun Ramadoss 
25134e48383SArun Ramadoss enum ksz_shifts {
25234e48383SArun Ramadoss 	VLAN_TABLE_MEMBERSHIP_S,
25334e48383SArun Ramadoss 	VLAN_TABLE,
25434e48383SArun Ramadoss 	STATIC_MAC_FWD_PORTS,
25534e48383SArun Ramadoss 	STATIC_MAC_FID,
25634e48383SArun Ramadoss 	DYNAMIC_MAC_ENTRIES_H,
25734e48383SArun Ramadoss 	DYNAMIC_MAC_ENTRIES,
25834e48383SArun Ramadoss 	DYNAMIC_MAC_FID,
25934e48383SArun Ramadoss 	DYNAMIC_MAC_TIMESTAMP,
26034e48383SArun Ramadoss 	DYNAMIC_MAC_SRC_PORT,
261457c182aSArun Ramadoss 	ALU_STAT_INDEX,
26234e48383SArun Ramadoss };
26334e48383SArun Ramadoss 
264aa5b8b73SArun Ramadoss enum ksz_xmii_ctrl0 {
265aa5b8b73SArun Ramadoss 	P_MII_100MBIT,
266aa5b8b73SArun Ramadoss 	P_MII_10MBIT,
2678560664fSArun Ramadoss 	P_MII_FULL_DUPLEX,
2688560664fSArun Ramadoss 	P_MII_HALF_DUPLEX,
269aa5b8b73SArun Ramadoss };
270aa5b8b73SArun Ramadoss 
27146f80fa8SArun Ramadoss enum ksz_xmii_ctrl1 {
272dc1c596eSArun Ramadoss 	P_RGMII_SEL,
273dc1c596eSArun Ramadoss 	P_RMII_SEL,
274dc1c596eSArun Ramadoss 	P_GMII_SEL,
275dc1c596eSArun Ramadoss 	P_MII_SEL,
27646f80fa8SArun Ramadoss 	P_GMII_1GBIT,
27746f80fa8SArun Ramadoss 	P_GMII_NOT_1GBIT,
27846f80fa8SArun Ramadoss };
27946f80fa8SArun Ramadoss 
2806a7abc61SMarek Vasut struct alu_struct {
2816a7abc61SMarek Vasut 	/* entry 1 */
2826a7abc61SMarek Vasut 	u8	is_static:1;
2836a7abc61SMarek Vasut 	u8	is_src_filter:1;
2846a7abc61SMarek Vasut 	u8	is_dst_filter:1;
2856a7abc61SMarek Vasut 	u8	prio_age:3;
2866a7abc61SMarek Vasut 	u32	_reserv_0_1:23;
2876a7abc61SMarek Vasut 	u8	mstp:3;
2886a7abc61SMarek Vasut 	/* entry 2 */
2896a7abc61SMarek Vasut 	u8	is_override:1;
2906a7abc61SMarek Vasut 	u8	is_use_fid:1;
2916a7abc61SMarek Vasut 	u32	_reserv_1_1:23;
2926a7abc61SMarek Vasut 	u8	port_forward:7;
2936a7abc61SMarek Vasut 	/* entry 3 & 4*/
2946a7abc61SMarek Vasut 	u32	_reserv_2_1:9;
2956a7abc61SMarek Vasut 	u8	fid:7;
2966a7abc61SMarek Vasut 	u8	mac[ETH_ALEN];
2976a7abc61SMarek Vasut };
2986a7abc61SMarek Vasut 
2996a7abc61SMarek Vasut struct ksz_dev_ops {
300d2822e68SArun Ramadoss 	int (*setup)(struct dsa_switch *ds);
301c9cd961cSArun Ramadoss 	void (*teardown)(struct dsa_switch *ds);
3026a7abc61SMarek Vasut 	u32 (*get_port_addr)(int port, int offset);
3036a7abc61SMarek Vasut 	void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
3046a7abc61SMarek Vasut 	void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
3056a7abc61SMarek Vasut 	void (*port_cleanup)(struct ksz_device *dev, int port);
3066a7abc61SMarek Vasut 	void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
3072c119d99SArun Ramadoss 	int (*set_ageing_time)(struct ksz_device *dev, unsigned int msecs);
3088f420456SOleksij Rempel 	int (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
3098f420456SOleksij Rempel 	int (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
3106a7abc61SMarek Vasut 	void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
3116a7abc61SMarek Vasut 			  u64 *cnt);
3126a7abc61SMarek Vasut 	void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
3136a7abc61SMarek Vasut 			  u64 *dropped, u64 *cnt);
314a7f4f13aSOleksij Rempel 	void (*r_mib_stat64)(struct ksz_device *dev, int port);
315f0d997e3SArun Ramadoss 	int  (*vlan_filtering)(struct ksz_device *dev, int port,
316f0d997e3SArun Ramadoss 			       bool flag, struct netlink_ext_ack *extack);
317f0d997e3SArun Ramadoss 	int  (*vlan_add)(struct ksz_device *dev, int port,
318f0d997e3SArun Ramadoss 			 const struct switchdev_obj_port_vlan *vlan,
319f0d997e3SArun Ramadoss 			 struct netlink_ext_ack *extack);
320f0d997e3SArun Ramadoss 	int  (*vlan_del)(struct ksz_device *dev, int port,
321f0d997e3SArun Ramadoss 			 const struct switchdev_obj_port_vlan *vlan);
32200a298bbSArun Ramadoss 	int (*mirror_add)(struct ksz_device *dev, int port,
32300a298bbSArun Ramadoss 			  struct dsa_mall_mirror_tc_entry *mirror,
32400a298bbSArun Ramadoss 			  bool ingress, struct netlink_ext_ack *extack);
32500a298bbSArun Ramadoss 	void (*mirror_del)(struct ksz_device *dev, int port,
32600a298bbSArun Ramadoss 			   struct dsa_mall_mirror_tc_entry *mirror);
327e587be75SArun Ramadoss 	int (*fdb_add)(struct ksz_device *dev, int port,
328e587be75SArun Ramadoss 		       const unsigned char *addr, u16 vid, struct dsa_db db);
329e587be75SArun Ramadoss 	int (*fdb_del)(struct ksz_device *dev, int port,
330e587be75SArun Ramadoss 		       const unsigned char *addr, u16 vid, struct dsa_db db);
331e587be75SArun Ramadoss 	int (*fdb_dump)(struct ksz_device *dev, int port,
332e587be75SArun Ramadoss 			dsa_fdb_dump_cb_t *cb, void *data);
333980c7d17SArun Ramadoss 	int (*mdb_add)(struct ksz_device *dev, int port,
334980c7d17SArun Ramadoss 		       const struct switchdev_obj_port_mdb *mdb,
335980c7d17SArun Ramadoss 		       struct dsa_db db);
336980c7d17SArun Ramadoss 	int (*mdb_del)(struct ksz_device *dev, int port,
337980c7d17SArun Ramadoss 		       const struct switchdev_obj_port_mdb *mdb,
338980c7d17SArun Ramadoss 		       struct dsa_db db);
3397012033cSArun Ramadoss 	void (*get_caps)(struct ksz_device *dev, int port,
3407012033cSArun Ramadoss 			 struct phylink_config *config);
3411fe94f54SArun Ramadoss 	int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
3426a7abc61SMarek Vasut 	void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
3436a7abc61SMarek Vasut 	void (*port_init_cnt)(struct ksz_device *dev, int port);
344a0cb1aa4SArun Ramadoss 	void (*phylink_mac_config)(struct ksz_device *dev, int port,
345a0cb1aa4SArun Ramadoss 				   unsigned int mode,
346a0cb1aa4SArun Ramadoss 				   const struct phylink_link_state *state);
347f597d3adSArun Ramadoss 	void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
348f597d3adSArun Ramadoss 				    unsigned int mode,
349f597d3adSArun Ramadoss 				    phy_interface_t interface,
350f597d3adSArun Ramadoss 				    struct phy_device *phydev, int speed,
351f597d3adSArun Ramadoss 				    int duplex, bool tx_pause, bool rx_pause);
352b19ac41fSArun Ramadoss 	void (*setup_rgmii_delay)(struct ksz_device *dev, int port);
353fb9324beSArun Ramadoss 	void (*config_cpu_port)(struct dsa_switch *ds);
354331d64f7SArun Ramadoss 	int (*enable_stp_addr)(struct ksz_device *dev);
355673b196fSArun Ramadoss 	int (*reset)(struct ksz_device *dev);
3566a7abc61SMarek Vasut 	int (*init)(struct ksz_device *dev);
3576a7abc61SMarek Vasut 	void (*exit)(struct ksz_device *dev);
3586a7abc61SMarek Vasut };
3596a7abc61SMarek Vasut 
3606a7abc61SMarek Vasut struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
3616ec23aaaSArun Ramadoss int ksz_switch_register(struct ksz_device *dev);
3626a7abc61SMarek Vasut void ksz_switch_remove(struct ksz_device *dev);
3636a7abc61SMarek Vasut 
3647c6ff470STristram Ha void ksz_init_mib_timer(struct ksz_device *dev);
365c6101dd7SArun Ramadoss void ksz_r_mib_stats64(struct ksz_device *dev, int port);
366bde55dd9SOleksij Rempel void ksz88xx_r_mib_stats64(struct ksz_device *dev, int port);
367e593df51SArun Ramadoss void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
36846f80fa8SArun Ramadoss bool ksz_get_gbit(struct ksz_device *dev, int port);
3690ab7f6bfSArun Ramadoss phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit);
3701958eee8SArun Ramadoss extern const struct ksz_chip_data ksz_switch_chips[];
371c2e86691STristram Ha 
372c2e86691STristram Ha /* Common register access functions */
373c2e86691STristram Ha 
374c2e86691STristram Ha static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
375c2e86691STristram Ha {
376ee394feaSMarek Vasut 	unsigned int value;
377ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[0], reg, &value);
378c2e86691STristram Ha 
379ec6ba50cSOleksij Rempel 	if (ret)
380ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't read 8bit reg: 0x%x %pe\n", reg,
381ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
382ec6ba50cSOleksij Rempel 
383ee394feaSMarek Vasut 	*val = value;
384c2e86691STristram Ha 	return ret;
385c2e86691STristram Ha }
386c2e86691STristram Ha 
387c2e86691STristram Ha static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
388c2e86691STristram Ha {
389ee394feaSMarek Vasut 	unsigned int value;
390ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[1], reg, &value);
391c2e86691STristram Ha 
392ec6ba50cSOleksij Rempel 	if (ret)
393ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't read 16bit reg: 0x%x %pe\n", reg,
394ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
395ec6ba50cSOleksij Rempel 
396ee394feaSMarek Vasut 	*val = value;
397c2e86691STristram Ha 	return ret;
398c2e86691STristram Ha }
399c2e86691STristram Ha 
400c2e86691STristram Ha static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
401c2e86691STristram Ha {
402ee394feaSMarek Vasut 	unsigned int value;
403ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[2], reg, &value);
404c2e86691STristram Ha 
405ec6ba50cSOleksij Rempel 	if (ret)
406ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't read 32bit reg: 0x%x %pe\n", reg,
407ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
408ec6ba50cSOleksij Rempel 
409ee394feaSMarek Vasut 	*val = value;
410c2e86691STristram Ha 	return ret;
411c2e86691STristram Ha }
412c2e86691STristram Ha 
413e66f840cSTristram Ha static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
414e66f840cSTristram Ha {
415e66f840cSTristram Ha 	u32 value[2];
416e66f840cSTristram Ha 	int ret;
417e66f840cSTristram Ha 
418e66f840cSTristram Ha 	ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
419ec6ba50cSOleksij Rempel 	if (ret)
420ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't read 64bit reg: 0x%x %pe\n", reg,
421ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
422ec6ba50cSOleksij Rempel 	else
423c34f674cSBen Hutchings 		*val = (u64)value[0] << 32 | value[1];
424e66f840cSTristram Ha 
425e66f840cSTristram Ha 	return ret;
426e66f840cSTristram Ha }
427e66f840cSTristram Ha 
428c2e86691STristram Ha static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
429c2e86691STristram Ha {
430ec6ba50cSOleksij Rempel 	int ret;
431ec6ba50cSOleksij Rempel 
432ec6ba50cSOleksij Rempel 	ret = regmap_write(dev->regmap[0], reg, value);
433ec6ba50cSOleksij Rempel 	if (ret)
434ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't write 8bit reg: 0x%x %pe\n", reg,
435ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
436ec6ba50cSOleksij Rempel 
437ec6ba50cSOleksij Rempel 	return ret;
438c2e86691STristram Ha }
439c2e86691STristram Ha 
440c2e86691STristram Ha static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
441c2e86691STristram Ha {
442ec6ba50cSOleksij Rempel 	int ret;
443ec6ba50cSOleksij Rempel 
444ec6ba50cSOleksij Rempel 	ret = regmap_write(dev->regmap[1], reg, value);
445ec6ba50cSOleksij Rempel 	if (ret)
446ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't write 16bit reg: 0x%x %pe\n", reg,
447ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
448ec6ba50cSOleksij Rempel 
449ec6ba50cSOleksij Rempel 	return ret;
450c2e86691STristram Ha }
451c2e86691STristram Ha 
452c2e86691STristram Ha static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
453c2e86691STristram Ha {
454ec6ba50cSOleksij Rempel 	int ret;
455ec6ba50cSOleksij Rempel 
456ec6ba50cSOleksij Rempel 	ret = regmap_write(dev->regmap[2], reg, value);
457ec6ba50cSOleksij Rempel 	if (ret)
458ec6ba50cSOleksij Rempel 		dev_err(dev->dev, "can't write 32bit reg: 0x%x %pe\n", reg,
459ec6ba50cSOleksij Rempel 			ERR_PTR(ret));
460ec6ba50cSOleksij Rempel 
461ec6ba50cSOleksij Rempel 	return ret;
462c2e86691STristram Ha }
463c2e86691STristram Ha 
464eac1ea20SChristian Eggers static inline int ksz_rmw16(struct ksz_device *dev, u32 reg, u16 mask,
465eac1ea20SChristian Eggers 			    u16 value)
466eac1ea20SChristian Eggers {
467eac1ea20SChristian Eggers 	int ret;
468eac1ea20SChristian Eggers 
469eac1ea20SChristian Eggers 	ret = regmap_update_bits(dev->regmap[1], reg, mask, value);
470eac1ea20SChristian Eggers 	if (ret)
471eac1ea20SChristian Eggers 		dev_err(dev->dev, "can't rmw 16bit reg 0x%x: %pe\n", reg,
472eac1ea20SChristian Eggers 			ERR_PTR(ret));
473eac1ea20SChristian Eggers 
474eac1ea20SChristian Eggers 	return ret;
475eac1ea20SChristian Eggers }
476eac1ea20SChristian Eggers 
477e66f840cSTristram Ha static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
478e66f840cSTristram Ha {
479e66f840cSTristram Ha 	u32 val[2];
480e66f840cSTristram Ha 
481e66f840cSTristram Ha 	/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
482e66f840cSTristram Ha 	value = swab64(value);
483e66f840cSTristram Ha 	val[0] = swab32(value & 0xffffffffULL);
484e66f840cSTristram Ha 	val[1] = swab32(value >> 32ULL);
485e66f840cSTristram Ha 
486e66f840cSTristram Ha 	return regmap_bulk_write(dev->regmap[2], reg, val, 2);
487e66f840cSTristram Ha }
488e66f840cSTristram Ha 
4896f1b986aSOleksij Rempel static inline int ksz_rmw8(struct ksz_device *dev, int offset, u8 mask, u8 val)
4906f1b986aSOleksij Rempel {
4916f1b986aSOleksij Rempel 	return regmap_update_bits(dev->regmap[0], offset, mask, val);
4926f1b986aSOleksij Rempel }
4936f1b986aSOleksij Rempel 
494d38bc3b4SOleksij Rempel static inline int ksz_pread8(struct ksz_device *dev, int port, int offset,
495c2e86691STristram Ha 			     u8 *data)
496c2e86691STristram Ha {
497d38bc3b4SOleksij Rempel 	return ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
498c2e86691STristram Ha }
499c2e86691STristram Ha 
500d38bc3b4SOleksij Rempel static inline int ksz_pread16(struct ksz_device *dev, int port, int offset,
501c2e86691STristram Ha 			      u16 *data)
502c2e86691STristram Ha {
503d38bc3b4SOleksij Rempel 	return ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
504c2e86691STristram Ha }
505c2e86691STristram Ha 
506d38bc3b4SOleksij Rempel static inline int ksz_pread32(struct ksz_device *dev, int port, int offset,
507c2e86691STristram Ha 			      u32 *data)
508c2e86691STristram Ha {
509d38bc3b4SOleksij Rempel 	return ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
510c2e86691STristram Ha }
511c2e86691STristram Ha 
512d38bc3b4SOleksij Rempel static inline int ksz_pwrite8(struct ksz_device *dev, int port, int offset,
513c2e86691STristram Ha 			      u8 data)
514c2e86691STristram Ha {
515d38bc3b4SOleksij Rempel 	return ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
516c2e86691STristram Ha }
517c2e86691STristram Ha 
518d38bc3b4SOleksij Rempel static inline int ksz_pwrite16(struct ksz_device *dev, int port, int offset,
519c2e86691STristram Ha 			       u16 data)
520c2e86691STristram Ha {
521d38bc3b4SOleksij Rempel 	return ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset),
522d38bc3b4SOleksij Rempel 			   data);
523c2e86691STristram Ha }
524c2e86691STristram Ha 
525d38bc3b4SOleksij Rempel static inline int ksz_pwrite32(struct ksz_device *dev, int port, int offset,
526c2e86691STristram Ha 			       u32 data)
527c2e86691STristram Ha {
528d38bc3b4SOleksij Rempel 	return ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset),
529d38bc3b4SOleksij Rempel 			   data);
530c2e86691STristram Ha }
531c2e86691STristram Ha 
5328560664fSArun Ramadoss static inline void ksz_prmw8(struct ksz_device *dev, int port, int offset,
5338560664fSArun Ramadoss 			     u8 mask, u8 val)
5348560664fSArun Ramadoss {
5358560664fSArun Ramadoss 	regmap_update_bits(dev->regmap[0],
5368560664fSArun Ramadoss 			   dev->dev_ops->get_port_addr(port, offset),
5378560664fSArun Ramadoss 			   mask, val);
5388560664fSArun Ramadoss }
5398560664fSArun Ramadoss 
540013572a2SMarek Vasut static inline void ksz_regmap_lock(void *__mtx)
541013572a2SMarek Vasut {
542013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
543013572a2SMarek Vasut 	mutex_lock(mtx);
544013572a2SMarek Vasut }
545013572a2SMarek Vasut 
546013572a2SMarek Vasut static inline void ksz_regmap_unlock(void *__mtx)
547013572a2SMarek Vasut {
548013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
549013572a2SMarek Vasut 	mutex_unlock(mtx);
550013572a2SMarek Vasut }
551013572a2SMarek Vasut 
552f3d890f5SArun Ramadoss static inline bool ksz_is_ksz88x3(struct ksz_device *dev)
553f3d890f5SArun Ramadoss {
554f3d890f5SArun Ramadoss 	return dev->chip_id == KSZ8830_CHIP_ID;
555f3d890f5SArun Ramadoss }
556f3d890f5SArun Ramadoss 
55799b16df0SArun Ramadoss static inline int is_lan937x(struct ksz_device *dev)
55899b16df0SArun Ramadoss {
55999b16df0SArun Ramadoss 	return dev->chip_id == LAN9370_CHIP_ID ||
56099b16df0SArun Ramadoss 		dev->chip_id == LAN9371_CHIP_ID ||
56199b16df0SArun Ramadoss 		dev->chip_id == LAN9372_CHIP_ID ||
56299b16df0SArun Ramadoss 		dev->chip_id == LAN9373_CHIP_ID ||
56399b16df0SArun Ramadoss 		dev->chip_id == LAN9374_CHIP_ID;
56499b16df0SArun Ramadoss }
56599b16df0SArun Ramadoss 
566de6dd626SArun Ramadoss /* STP State Defines */
567de6dd626SArun Ramadoss #define PORT_TX_ENABLE			BIT(2)
568de6dd626SArun Ramadoss #define PORT_RX_ENABLE			BIT(1)
569de6dd626SArun Ramadoss #define PORT_LEARN_DISABLE		BIT(0)
570de6dd626SArun Ramadoss 
57191a98917SArun Ramadoss /* Switch ID Defines */
57291a98917SArun Ramadoss #define REG_CHIP_ID0			0x00
57391a98917SArun Ramadoss 
57491a98917SArun Ramadoss #define SW_FAMILY_ID_M			GENMASK(15, 8)
57591a98917SArun Ramadoss #define KSZ87_FAMILY_ID			0x87
57691a98917SArun Ramadoss #define KSZ88_FAMILY_ID			0x88
57791a98917SArun Ramadoss 
57891a98917SArun Ramadoss #define KSZ8_PORT_STATUS_0		0x08
57991a98917SArun Ramadoss #define KSZ8_PORT_FIBER_MODE		BIT(7)
58091a98917SArun Ramadoss 
58191a98917SArun Ramadoss #define SW_CHIP_ID_M			GENMASK(7, 4)
58291a98917SArun Ramadoss #define KSZ87_CHIP_ID_94		0x6
58391a98917SArun Ramadoss #define KSZ87_CHIP_ID_95		0x9
58491a98917SArun Ramadoss #define KSZ88_CHIP_ID_63		0x3
58591a98917SArun Ramadoss 
58691a98917SArun Ramadoss #define SW_REV_ID_M			GENMASK(7, 4)
58791a98917SArun Ramadoss 
588b4490809SOleksij Rempel /* KSZ9893, KSZ9563, KSZ8563 specific register  */
589b4490809SOleksij Rempel #define REG_CHIP_ID4			0x0f
590b4490809SOleksij Rempel #define SKU_ID_KSZ8563			0x3c
591ef912fe4SRakesh Sankaranarayanan #define SKU_ID_KSZ9563			0x1c
592b4490809SOleksij Rempel 
5931ca6437fSArun Ramadoss /* Driver set switch broadcast storm protection at 10% rate. */
5941ca6437fSArun Ramadoss #define BROADCAST_STORM_PROT_RATE	10
5951ca6437fSArun Ramadoss 
5961ca6437fSArun Ramadoss /* 148,800 frames * 67 ms / 100 */
5971ca6437fSArun Ramadoss #define BROADCAST_STORM_VALUE		9969
5981ca6437fSArun Ramadoss 
5991ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE_HI		0x07
6001ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE_LO		0xFF
6011ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE		0x07FF
6021ca6437fSArun Ramadoss 
6030abab9f3SArun Ramadoss #define MULTICAST_STORM_DISABLE		BIT(6)
6040abab9f3SArun Ramadoss 
605ad08ac18SArun Ramadoss #define SW_START			0x01
606ad08ac18SArun Ramadoss 
60746f80fa8SArun Ramadoss /* xMII configuration */
6088560664fSArun Ramadoss #define P_MII_DUPLEX_M			BIT(6)
609aa5b8b73SArun Ramadoss #define P_MII_100MBIT_M			BIT(4)
610aa5b8b73SArun Ramadoss 
61146f80fa8SArun Ramadoss #define P_GMII_1GBIT_M			BIT(6)
612dc1c596eSArun Ramadoss #define P_RGMII_ID_IG_ENABLE		BIT(4)
613dc1c596eSArun Ramadoss #define P_RGMII_ID_EG_ENABLE		BIT(3)
6140ab7f6bfSArun Ramadoss #define P_MII_MAC_MODE			BIT(2)
615dc1c596eSArun Ramadoss #define P_MII_SEL_M			0x3
61646f80fa8SArun Ramadoss 
617ff319a64SArun Ramadoss /* Interrupt */
618e1add7ddSArun Ramadoss #define REG_SW_PORT_INT_STATUS__1	0x001B
619e1add7ddSArun Ramadoss #define REG_SW_PORT_INT_MASK__1		0x001F
620ff319a64SArun Ramadoss 
621ff319a64SArun Ramadoss #define REG_PORT_INT_STATUS		0x001B
622ff319a64SArun Ramadoss #define REG_PORT_INT_MASK		0x001F
623ff319a64SArun Ramadoss 
624ff319a64SArun Ramadoss #define PORT_SRC_PHY_INT		1
625*cc13ab18SArun Ramadoss #define PORT_SRC_PTP_INT		2
626ff319a64SArun Ramadoss 
62729d1e85fSOleksij Rempel #define KSZ8795_HUGE_PACKET_SIZE	2000
62829d1e85fSOleksij Rempel #define KSZ8863_HUGE_PACKET_SIZE	1916
62929d1e85fSOleksij Rempel #define KSZ8863_NORMAL_PACKET_SIZE	1536
63029d1e85fSOleksij Rempel #define KSZ8_LEGAL_PACKET_SIZE		1518
631838c19f8SOleksij Rempel #define KSZ9477_MAX_FRAME_SIZE		9000
632838c19f8SOleksij Rempel 
633255b59adSMarek Vasut /* Regmap tables generation */
634255b59adSMarek Vasut #define KSZ_SPI_OP_RD		3
635255b59adSMarek Vasut #define KSZ_SPI_OP_WR		2
636255b59adSMarek Vasut 
63720e03777STristram Ha #define swabnot_used(x)		0
63820e03777STristram Ha 
639255b59adSMarek Vasut #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)		\
640255b59adSMarek Vasut 	swab##swp((opcode) << ((regbits) + (regpad)))
641255b59adSMarek Vasut 
642255b59adSMarek Vasut #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)		\
643255b59adSMarek Vasut 	{								\
6445f81d545SGeorge McCollister 		.name = #width,						\
645255b59adSMarek Vasut 		.val_bits = (width),					\
646a3aa6e65SMarek Vasut 		.reg_stride = 1,					\
647255b59adSMarek Vasut 		.reg_bits = (regbits) + (regalign),			\
648255b59adSMarek Vasut 		.pad_bits = (regpad),					\
649255b59adSMarek Vasut 		.max_register = BIT(regbits) - 1,			\
650255b59adSMarek Vasut 		.cache_type = REGCACHE_NONE,				\
651255b59adSMarek Vasut 		.read_flag_mask =					\
652255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,	\
653255b59adSMarek Vasut 					     regbits, regpad),		\
654255b59adSMarek Vasut 		.write_flag_mask =					\
655255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
656255b59adSMarek Vasut 					     regbits, regpad),		\
657013572a2SMarek Vasut 		.lock = ksz_regmap_lock,				\
658013572a2SMarek Vasut 		.unlock = ksz_regmap_unlock,				\
659255b59adSMarek Vasut 		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
660255b59adSMarek Vasut 		.val_format_endian = REGMAP_ENDIAN_BIG			\
661255b59adSMarek Vasut 	}
662255b59adSMarek Vasut 
663255b59adSMarek Vasut #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)		\
664255b59adSMarek Vasut 	static const struct regmap_config ksz##_regmap_config[] = {	\
665255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
666255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
667255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
668255b59adSMarek Vasut 	}
669255b59adSMarek Vasut 
670c2e86691STristram Ha #endif
671