xref: /openbmc/linux/drivers/net/dsa/microchip/ksz_common.h (revision dc1c596edba5e656256c2d6e6922246c7803f2de)
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;
28c4748ff6SOleksij Rempel 	struct ethtool_pause_stats pause_stats;
29a7f4f13aSOleksij Rempel 	struct spinlock stats64_lock;
306a7abc61SMarek Vasut };
316a7abc61SMarek Vasut 
32a530e6f2SArun Ramadoss struct ksz_mib_names {
33a530e6f2SArun Ramadoss 	int index;
34a530e6f2SArun Ramadoss 	char string[ETH_GSTRING_LEN];
35a530e6f2SArun Ramadoss };
36a530e6f2SArun Ramadoss 
37462d5250SArun Ramadoss struct ksz_chip_data {
38462d5250SArun Ramadoss 	u32 chip_id;
39462d5250SArun Ramadoss 	const char *dev_name;
40462d5250SArun Ramadoss 	int num_vlans;
41462d5250SArun Ramadoss 	int num_alus;
42462d5250SArun Ramadoss 	int num_statics;
43462d5250SArun Ramadoss 	int cpu_ports;
44462d5250SArun Ramadoss 	int port_cnt;
456ec23aaaSArun Ramadoss 	const struct ksz_dev_ops *ops;
46462d5250SArun Ramadoss 	bool phy_errata_9477;
47462d5250SArun Ramadoss 	bool ksz87xx_eee_link_erratum;
48a530e6f2SArun Ramadoss 	const struct ksz_mib_names *mib_names;
49a530e6f2SArun Ramadoss 	int mib_cnt;
50a530e6f2SArun Ramadoss 	u8 reg_mib_cnt;
51a02579dfSArun Ramadoss 	const u16 *regs;
52d23a5e18SArun Ramadoss 	const u32 *masks;
5334e48383SArun Ramadoss 	const u8 *shifts;
54aa5b8b73SArun Ramadoss 	const u8 *xmii_ctrl0;
5546f80fa8SArun Ramadoss 	const u8 *xmii_ctrl1;
56e593df51SArun Ramadoss 	int stp_ctrl_reg;
571ca6437fSArun Ramadoss 	int broadcast_ctrl_reg;
580abab9f3SArun Ramadoss 	int multicast_ctrl_reg;
59ad08ac18SArun Ramadoss 	int start_ctrl_reg;
6065ac79e1SArun Ramadoss 	bool supports_mii[KSZ_MAX_NUM_PORTS];
6165ac79e1SArun Ramadoss 	bool supports_rmii[KSZ_MAX_NUM_PORTS];
6265ac79e1SArun Ramadoss 	bool supports_rgmii[KSZ_MAX_NUM_PORTS];
6365ac79e1SArun Ramadoss 	bool internal_phy[KSZ_MAX_NUM_PORTS];
64462d5250SArun Ramadoss };
65462d5250SArun Ramadoss 
666a7abc61SMarek Vasut struct ksz_port {
678f4f58f8SBen Hutchings 	bool remove_tag;		/* Remove Tag flag set, for ksz8795 only */
686a7abc61SMarek Vasut 	int stp_state;
696a7abc61SMarek Vasut 	struct phy_device phydev;
706a7abc61SMarek Vasut 
716a7abc61SMarek Vasut 	u32 on:1;			/* port is not disabled by hardware */
726a7abc61SMarek Vasut 	u32 phy:1;			/* port has a PHY */
736a7abc61SMarek Vasut 	u32 fiber:1;			/* port is fiber */
746a7abc61SMarek Vasut 	u32 sgmii:1;			/* port is SGMII */
756a7abc61SMarek Vasut 	u32 force:1;
766a7abc61SMarek Vasut 	u32 read:1;			/* read MIB counters in background */
776a7abc61SMarek Vasut 	u32 freeze:1;			/* MIB counter freeze is enabled */
786a7abc61SMarek Vasut 
796a7abc61SMarek Vasut 	struct ksz_port_mib mib;
80edecfa98SHelmut Grohne 	phy_interface_t interface;
81e18058eaSOleksij Rempel 	u16 max_frame;
826a7abc61SMarek Vasut };
836a7abc61SMarek Vasut 
846a7abc61SMarek Vasut struct ksz_device {
856a7abc61SMarek Vasut 	struct dsa_switch *ds;
866a7abc61SMarek Vasut 	struct ksz_platform_data *pdata;
87462d5250SArun Ramadoss 	const struct ksz_chip_data *info;
886a7abc61SMarek Vasut 
896a7abc61SMarek Vasut 	struct mutex dev_mutex;		/* device access */
90013572a2SMarek Vasut 	struct mutex regmap_mutex;	/* regmap access */
916a7abc61SMarek Vasut 	struct mutex alu_mutex;		/* ALU access */
926a7abc61SMarek Vasut 	struct mutex vlan_mutex;	/* vlan access */
936a7abc61SMarek Vasut 	const struct ksz_dev_ops *dev_ops;
946a7abc61SMarek Vasut 
956a7abc61SMarek Vasut 	struct device *dev;
966a7abc61SMarek Vasut 	struct regmap *regmap[3];
976a7abc61SMarek Vasut 
986a7abc61SMarek Vasut 	void *priv;
996a7abc61SMarek Vasut 
1006a7abc61SMarek Vasut 	struct gpio_desc *reset_gpio;	/* Optional reset GPIO */
1016a7abc61SMarek Vasut 
1026a7abc61SMarek Vasut 	/* chip specific data */
1036a7abc61SMarek Vasut 	u32 chip_id;
10491a98917SArun Ramadoss 	u8 chip_rev;
1056a7abc61SMarek Vasut 	int cpu_port;			/* port connected to CPU */
1066a7abc61SMarek Vasut 	int phy_port_cnt;
107edecfa98SHelmut Grohne 	phy_interface_t compat_interface;
1086a7abc61SMarek Vasut 	bool synclko_125;
10948bf8b8aSRobert Hancock 	bool synclko_disable;
1106a7abc61SMarek Vasut 
1116a7abc61SMarek Vasut 	struct vlan_table *vlan_cache;
1126a7abc61SMarek Vasut 
1136a7abc61SMarek Vasut 	struct ksz_port *ports;
114469b390eSGeorge McCollister 	struct delayed_work mib_read;
1156a7abc61SMarek Vasut 	unsigned long mib_read_interval;
1166a7abc61SMarek Vasut 	u16 mirror_rx;
1176a7abc61SMarek Vasut 	u16 mirror_tx;
1186a7abc61SMarek Vasut 	u32 features;			/* chip specific features */
1196a7abc61SMarek Vasut 	u16 port_mask;
1206a7abc61SMarek Vasut };
1216a7abc61SMarek Vasut 
122462d5250SArun Ramadoss /* List of supported models */
123462d5250SArun Ramadoss enum ksz_model {
124462d5250SArun Ramadoss 	KSZ8795,
125462d5250SArun Ramadoss 	KSZ8794,
126462d5250SArun Ramadoss 	KSZ8765,
127462d5250SArun Ramadoss 	KSZ8830,
128462d5250SArun Ramadoss 	KSZ9477,
129462d5250SArun Ramadoss 	KSZ9897,
130462d5250SArun Ramadoss 	KSZ9893,
131462d5250SArun Ramadoss 	KSZ9567,
132462d5250SArun Ramadoss 	LAN9370,
133462d5250SArun Ramadoss 	LAN9371,
134462d5250SArun Ramadoss 	LAN9372,
135462d5250SArun Ramadoss 	LAN9373,
136462d5250SArun Ramadoss 	LAN9374,
137462d5250SArun Ramadoss };
138462d5250SArun Ramadoss 
139462d5250SArun Ramadoss enum ksz_chip_id {
140462d5250SArun Ramadoss 	KSZ8795_CHIP_ID = 0x8795,
141462d5250SArun Ramadoss 	KSZ8794_CHIP_ID = 0x8794,
142462d5250SArun Ramadoss 	KSZ8765_CHIP_ID = 0x8765,
143462d5250SArun Ramadoss 	KSZ8830_CHIP_ID = 0x8830,
144462d5250SArun Ramadoss 	KSZ9477_CHIP_ID = 0x00947700,
145462d5250SArun Ramadoss 	KSZ9897_CHIP_ID = 0x00989700,
146462d5250SArun Ramadoss 	KSZ9893_CHIP_ID = 0x00989300,
147462d5250SArun Ramadoss 	KSZ9567_CHIP_ID = 0x00956700,
148462d5250SArun Ramadoss 	LAN9370_CHIP_ID = 0x00937000,
149462d5250SArun Ramadoss 	LAN9371_CHIP_ID = 0x00937100,
150462d5250SArun Ramadoss 	LAN9372_CHIP_ID = 0x00937200,
151462d5250SArun Ramadoss 	LAN9373_CHIP_ID = 0x00937300,
152462d5250SArun Ramadoss 	LAN9374_CHIP_ID = 0x00937400,
153462d5250SArun Ramadoss };
154462d5250SArun Ramadoss 
155486f9ca7SArun Ramadoss enum ksz_regs {
156486f9ca7SArun Ramadoss 	REG_IND_CTRL_0,
157486f9ca7SArun Ramadoss 	REG_IND_DATA_8,
158486f9ca7SArun Ramadoss 	REG_IND_DATA_CHECK,
159486f9ca7SArun Ramadoss 	REG_IND_DATA_HI,
160486f9ca7SArun Ramadoss 	REG_IND_DATA_LO,
161486f9ca7SArun Ramadoss 	REG_IND_MIB_CHECK,
162486f9ca7SArun Ramadoss 	REG_IND_BYTE,
163486f9ca7SArun Ramadoss 	P_FORCE_CTRL,
164486f9ca7SArun Ramadoss 	P_LINK_STATUS,
165486f9ca7SArun Ramadoss 	P_LOCAL_CTRL,
166486f9ca7SArun Ramadoss 	P_NEG_RESTART_CTRL,
167486f9ca7SArun Ramadoss 	P_REMOTE_STATUS,
168486f9ca7SArun Ramadoss 	P_SPEED_STATUS,
169486f9ca7SArun Ramadoss 	S_TAIL_TAG_CTRL,
1706877102fSArun Ramadoss 	P_STP_CTRL,
1719d95329cSArun Ramadoss 	S_START_CTRL,
1729d95329cSArun Ramadoss 	S_BROADCAST_CTRL,
1739d95329cSArun Ramadoss 	S_MULTICAST_CTRL,
174aa5b8b73SArun Ramadoss 	P_XMII_CTRL_0,
17546f80fa8SArun Ramadoss 	P_XMII_CTRL_1,
176486f9ca7SArun Ramadoss };
177486f9ca7SArun Ramadoss 
178d23a5e18SArun Ramadoss enum ksz_masks {
179d23a5e18SArun Ramadoss 	PORT_802_1P_REMAPPING,
180d23a5e18SArun Ramadoss 	SW_TAIL_TAG_ENABLE,
181d23a5e18SArun Ramadoss 	MIB_COUNTER_OVERFLOW,
182d23a5e18SArun Ramadoss 	MIB_COUNTER_VALID,
183d23a5e18SArun Ramadoss 	VLAN_TABLE_FID,
184d23a5e18SArun Ramadoss 	VLAN_TABLE_MEMBERSHIP,
185d23a5e18SArun Ramadoss 	VLAN_TABLE_VALID,
186d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_VALID,
187d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_USE_FID,
188d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_FID,
189d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_OVERRIDE,
190d23a5e18SArun Ramadoss 	STATIC_MAC_TABLE_FWD_PORTS,
191d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_ENTRIES_H,
192d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_MAC_EMPTY,
193d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_NOT_READY,
194d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_ENTRIES,
195d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_FID,
196d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_SRC_PORT,
197d23a5e18SArun Ramadoss 	DYNAMIC_MAC_TABLE_TIMESTAMP,
198457c182aSArun Ramadoss 	ALU_STAT_WRITE,
199457c182aSArun Ramadoss 	ALU_STAT_READ,
2008560664fSArun Ramadoss 	P_MII_TX_FLOW_CTRL,
2018560664fSArun Ramadoss 	P_MII_RX_FLOW_CTRL,
202d23a5e18SArun Ramadoss };
203d23a5e18SArun Ramadoss 
20434e48383SArun Ramadoss enum ksz_shifts {
20534e48383SArun Ramadoss 	VLAN_TABLE_MEMBERSHIP_S,
20634e48383SArun Ramadoss 	VLAN_TABLE,
20734e48383SArun Ramadoss 	STATIC_MAC_FWD_PORTS,
20834e48383SArun Ramadoss 	STATIC_MAC_FID,
20934e48383SArun Ramadoss 	DYNAMIC_MAC_ENTRIES_H,
21034e48383SArun Ramadoss 	DYNAMIC_MAC_ENTRIES,
21134e48383SArun Ramadoss 	DYNAMIC_MAC_FID,
21234e48383SArun Ramadoss 	DYNAMIC_MAC_TIMESTAMP,
21334e48383SArun Ramadoss 	DYNAMIC_MAC_SRC_PORT,
214457c182aSArun Ramadoss 	ALU_STAT_INDEX,
21534e48383SArun Ramadoss };
21634e48383SArun Ramadoss 
217aa5b8b73SArun Ramadoss enum ksz_xmii_ctrl0 {
218aa5b8b73SArun Ramadoss 	P_MII_100MBIT,
219aa5b8b73SArun Ramadoss 	P_MII_10MBIT,
2208560664fSArun Ramadoss 	P_MII_FULL_DUPLEX,
2218560664fSArun Ramadoss 	P_MII_HALF_DUPLEX,
222aa5b8b73SArun Ramadoss };
223aa5b8b73SArun Ramadoss 
22446f80fa8SArun Ramadoss enum ksz_xmii_ctrl1 {
225*dc1c596eSArun Ramadoss 	P_RGMII_SEL,
226*dc1c596eSArun Ramadoss 	P_RMII_SEL,
227*dc1c596eSArun Ramadoss 	P_GMII_SEL,
228*dc1c596eSArun Ramadoss 	P_MII_SEL,
22946f80fa8SArun Ramadoss 	P_GMII_1GBIT,
23046f80fa8SArun Ramadoss 	P_GMII_NOT_1GBIT,
23146f80fa8SArun Ramadoss };
23246f80fa8SArun Ramadoss 
2336a7abc61SMarek Vasut struct alu_struct {
2346a7abc61SMarek Vasut 	/* entry 1 */
2356a7abc61SMarek Vasut 	u8	is_static:1;
2366a7abc61SMarek Vasut 	u8	is_src_filter:1;
2376a7abc61SMarek Vasut 	u8	is_dst_filter:1;
2386a7abc61SMarek Vasut 	u8	prio_age:3;
2396a7abc61SMarek Vasut 	u32	_reserv_0_1:23;
2406a7abc61SMarek Vasut 	u8	mstp:3;
2416a7abc61SMarek Vasut 	/* entry 2 */
2426a7abc61SMarek Vasut 	u8	is_override:1;
2436a7abc61SMarek Vasut 	u8	is_use_fid:1;
2446a7abc61SMarek Vasut 	u32	_reserv_1_1:23;
2456a7abc61SMarek Vasut 	u8	port_forward:7;
2466a7abc61SMarek Vasut 	/* entry 3 & 4*/
2476a7abc61SMarek Vasut 	u32	_reserv_2_1:9;
2486a7abc61SMarek Vasut 	u8	fid:7;
2496a7abc61SMarek Vasut 	u8	mac[ETH_ALEN];
2506a7abc61SMarek Vasut };
2516a7abc61SMarek Vasut 
2526a7abc61SMarek Vasut struct ksz_dev_ops {
253d2822e68SArun Ramadoss 	int (*setup)(struct dsa_switch *ds);
2546a7abc61SMarek Vasut 	u32 (*get_port_addr)(int port, int offset);
2556a7abc61SMarek Vasut 	void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member);
2566a7abc61SMarek Vasut 	void (*flush_dyn_mac_table)(struct ksz_device *dev, int port);
2576a7abc61SMarek Vasut 	void (*port_cleanup)(struct ksz_device *dev, int port);
2586a7abc61SMarek Vasut 	void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port);
2596a7abc61SMarek Vasut 	void (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val);
2606a7abc61SMarek Vasut 	void (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val);
2616a7abc61SMarek Vasut 	void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr,
2626a7abc61SMarek Vasut 			  u64 *cnt);
2636a7abc61SMarek Vasut 	void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr,
2646a7abc61SMarek Vasut 			  u64 *dropped, u64 *cnt);
265a7f4f13aSOleksij Rempel 	void (*r_mib_stat64)(struct ksz_device *dev, int port);
266f0d997e3SArun Ramadoss 	int  (*vlan_filtering)(struct ksz_device *dev, int port,
267f0d997e3SArun Ramadoss 			       bool flag, struct netlink_ext_ack *extack);
268f0d997e3SArun Ramadoss 	int  (*vlan_add)(struct ksz_device *dev, int port,
269f0d997e3SArun Ramadoss 			 const struct switchdev_obj_port_vlan *vlan,
270f0d997e3SArun Ramadoss 			 struct netlink_ext_ack *extack);
271f0d997e3SArun Ramadoss 	int  (*vlan_del)(struct ksz_device *dev, int port,
272f0d997e3SArun Ramadoss 			 const struct switchdev_obj_port_vlan *vlan);
27300a298bbSArun Ramadoss 	int (*mirror_add)(struct ksz_device *dev, int port,
27400a298bbSArun Ramadoss 			  struct dsa_mall_mirror_tc_entry *mirror,
27500a298bbSArun Ramadoss 			  bool ingress, struct netlink_ext_ack *extack);
27600a298bbSArun Ramadoss 	void (*mirror_del)(struct ksz_device *dev, int port,
27700a298bbSArun Ramadoss 			   struct dsa_mall_mirror_tc_entry *mirror);
278e587be75SArun Ramadoss 	int (*fdb_add)(struct ksz_device *dev, int port,
279e587be75SArun Ramadoss 		       const unsigned char *addr, u16 vid, struct dsa_db db);
280e587be75SArun Ramadoss 	int (*fdb_del)(struct ksz_device *dev, int port,
281e587be75SArun Ramadoss 		       const unsigned char *addr, u16 vid, struct dsa_db db);
282e587be75SArun Ramadoss 	int (*fdb_dump)(struct ksz_device *dev, int port,
283e587be75SArun Ramadoss 			dsa_fdb_dump_cb_t *cb, void *data);
284980c7d17SArun Ramadoss 	int (*mdb_add)(struct ksz_device *dev, int port,
285980c7d17SArun Ramadoss 		       const struct switchdev_obj_port_mdb *mdb,
286980c7d17SArun Ramadoss 		       struct dsa_db db);
287980c7d17SArun Ramadoss 	int (*mdb_del)(struct ksz_device *dev, int port,
288980c7d17SArun Ramadoss 		       const struct switchdev_obj_port_mdb *mdb,
289980c7d17SArun Ramadoss 		       struct dsa_db db);
2907012033cSArun Ramadoss 	void (*get_caps)(struct ksz_device *dev, int port,
2917012033cSArun Ramadoss 			 struct phylink_config *config);
2921fe94f54SArun Ramadoss 	int (*change_mtu)(struct ksz_device *dev, int port, int mtu);
2931fe94f54SArun Ramadoss 	int (*max_mtu)(struct ksz_device *dev, int port);
2946a7abc61SMarek Vasut 	void (*freeze_mib)(struct ksz_device *dev, int port, bool freeze);
2956a7abc61SMarek Vasut 	void (*port_init_cnt)(struct ksz_device *dev, int port);
296a0cb1aa4SArun Ramadoss 	void (*phylink_mac_config)(struct ksz_device *dev, int port,
297a0cb1aa4SArun Ramadoss 				   unsigned int mode,
298a0cb1aa4SArun Ramadoss 				   const struct phylink_link_state *state);
299f597d3adSArun Ramadoss 	void (*phylink_mac_link_up)(struct ksz_device *dev, int port,
300f597d3adSArun Ramadoss 				    unsigned int mode,
301f597d3adSArun Ramadoss 				    phy_interface_t interface,
302f597d3adSArun Ramadoss 				    struct phy_device *phydev, int speed,
303f597d3adSArun Ramadoss 				    int duplex, bool tx_pause, bool rx_pause);
304fb9324beSArun Ramadoss 	void (*config_cpu_port)(struct dsa_switch *ds);
305331d64f7SArun Ramadoss 	int (*enable_stp_addr)(struct ksz_device *dev);
306673b196fSArun Ramadoss 	int (*reset)(struct ksz_device *dev);
3076a7abc61SMarek Vasut 	int (*init)(struct ksz_device *dev);
3086a7abc61SMarek Vasut 	void (*exit)(struct ksz_device *dev);
3096a7abc61SMarek Vasut };
3106a7abc61SMarek Vasut 
3116a7abc61SMarek Vasut struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
3126ec23aaaSArun Ramadoss int ksz_switch_register(struct ksz_device *dev);
3136a7abc61SMarek Vasut void ksz_switch_remove(struct ksz_device *dev);
3146a7abc61SMarek Vasut 
3157c6ff470STristram Ha void ksz_init_mib_timer(struct ksz_device *dev);
316c6101dd7SArun Ramadoss void ksz_r_mib_stats64(struct ksz_device *dev, int port);
317e593df51SArun Ramadoss void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state);
31846f80fa8SArun Ramadoss bool ksz_get_gbit(struct ksz_device *dev, int port);
31946f80fa8SArun Ramadoss void ksz_set_gbit(struct ksz_device *dev, int port, bool gbit);
320*dc1c596eSArun Ramadoss void ksz_set_xmii(struct ksz_device *dev, int port, phy_interface_t interface);
3211958eee8SArun Ramadoss extern const struct ksz_chip_data ksz_switch_chips[];
322c2e86691STristram Ha 
323c2e86691STristram Ha /* Common register access functions */
324c2e86691STristram Ha 
325c2e86691STristram Ha static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val)
326c2e86691STristram Ha {
327ee394feaSMarek Vasut 	unsigned int value;
328ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[0], reg, &value);
329c2e86691STristram Ha 
330ee394feaSMarek Vasut 	*val = value;
331c2e86691STristram Ha 	return ret;
332c2e86691STristram Ha }
333c2e86691STristram Ha 
334c2e86691STristram Ha static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val)
335c2e86691STristram Ha {
336ee394feaSMarek Vasut 	unsigned int value;
337ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[1], reg, &value);
338c2e86691STristram Ha 
339ee394feaSMarek Vasut 	*val = value;
340c2e86691STristram Ha 	return ret;
341c2e86691STristram Ha }
342c2e86691STristram Ha 
343c2e86691STristram Ha static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val)
344c2e86691STristram Ha {
345ee394feaSMarek Vasut 	unsigned int value;
346ee394feaSMarek Vasut 	int ret = regmap_read(dev->regmap[2], reg, &value);
347c2e86691STristram Ha 
348ee394feaSMarek Vasut 	*val = value;
349c2e86691STristram Ha 	return ret;
350c2e86691STristram Ha }
351c2e86691STristram Ha 
352e66f840cSTristram Ha static inline int ksz_read64(struct ksz_device *dev, u32 reg, u64 *val)
353e66f840cSTristram Ha {
354e66f840cSTristram Ha 	u32 value[2];
355e66f840cSTristram Ha 	int ret;
356e66f840cSTristram Ha 
357e66f840cSTristram Ha 	ret = regmap_bulk_read(dev->regmap[2], reg, value, 2);
358c34f674cSBen Hutchings 	if (!ret)
359c34f674cSBen Hutchings 		*val = (u64)value[0] << 32 | value[1];
360e66f840cSTristram Ha 
361e66f840cSTristram Ha 	return ret;
362e66f840cSTristram Ha }
363e66f840cSTristram Ha 
364c2e86691STristram Ha static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value)
365c2e86691STristram Ha {
366ee394feaSMarek Vasut 	return regmap_write(dev->regmap[0], reg, value);
367c2e86691STristram Ha }
368c2e86691STristram Ha 
369c2e86691STristram Ha static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value)
370c2e86691STristram Ha {
371ee394feaSMarek Vasut 	return regmap_write(dev->regmap[1], reg, value);
372c2e86691STristram Ha }
373c2e86691STristram Ha 
374c2e86691STristram Ha static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value)
375c2e86691STristram Ha {
376ee394feaSMarek Vasut 	return regmap_write(dev->regmap[2], reg, value);
377c2e86691STristram Ha }
378c2e86691STristram Ha 
379e66f840cSTristram Ha static inline int ksz_write64(struct ksz_device *dev, u32 reg, u64 value)
380e66f840cSTristram Ha {
381e66f840cSTristram Ha 	u32 val[2];
382e66f840cSTristram Ha 
383e66f840cSTristram Ha 	/* Ick! ToDo: Add 64bit R/W to regmap on 32bit systems */
384e66f840cSTristram Ha 	value = swab64(value);
385e66f840cSTristram Ha 	val[0] = swab32(value & 0xffffffffULL);
386e66f840cSTristram Ha 	val[1] = swab32(value >> 32ULL);
387e66f840cSTristram Ha 
388e66f840cSTristram Ha 	return regmap_bulk_write(dev->regmap[2], reg, val, 2);
389e66f840cSTristram Ha }
390e66f840cSTristram Ha 
391c2e86691STristram Ha static inline void ksz_pread8(struct ksz_device *dev, int port, int offset,
392c2e86691STristram Ha 			      u8 *data)
393c2e86691STristram Ha {
394c2e86691STristram Ha 	ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data);
395c2e86691STristram Ha }
396c2e86691STristram Ha 
397c2e86691STristram Ha static inline void ksz_pread16(struct ksz_device *dev, int port, int offset,
398c2e86691STristram Ha 			       u16 *data)
399c2e86691STristram Ha {
400c2e86691STristram Ha 	ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data);
401c2e86691STristram Ha }
402c2e86691STristram Ha 
403c2e86691STristram Ha static inline void ksz_pread32(struct ksz_device *dev, int port, int offset,
404c2e86691STristram Ha 			       u32 *data)
405c2e86691STristram Ha {
406c2e86691STristram Ha 	ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data);
407c2e86691STristram Ha }
408c2e86691STristram Ha 
409c2e86691STristram Ha static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset,
410c2e86691STristram Ha 			       u8 data)
411c2e86691STristram Ha {
412c2e86691STristram Ha 	ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data);
413c2e86691STristram Ha }
414c2e86691STristram Ha 
415c2e86691STristram Ha static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset,
416c2e86691STristram Ha 				u16 data)
417c2e86691STristram Ha {
418c2e86691STristram Ha 	ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset), data);
419c2e86691STristram Ha }
420c2e86691STristram Ha 
421c2e86691STristram Ha static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset,
422c2e86691STristram Ha 				u32 data)
423c2e86691STristram Ha {
424c2e86691STristram Ha 	ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data);
425c2e86691STristram Ha }
426c2e86691STristram Ha 
4278560664fSArun Ramadoss static inline void ksz_prmw8(struct ksz_device *dev, int port, int offset,
4288560664fSArun Ramadoss 			     u8 mask, u8 val)
4298560664fSArun Ramadoss {
4308560664fSArun Ramadoss 	regmap_update_bits(dev->regmap[0],
4318560664fSArun Ramadoss 			   dev->dev_ops->get_port_addr(port, offset),
4328560664fSArun Ramadoss 			   mask, val);
4338560664fSArun Ramadoss }
4348560664fSArun Ramadoss 
435013572a2SMarek Vasut static inline void ksz_regmap_lock(void *__mtx)
436013572a2SMarek Vasut {
437013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
438013572a2SMarek Vasut 	mutex_lock(mtx);
439013572a2SMarek Vasut }
440013572a2SMarek Vasut 
441013572a2SMarek Vasut static inline void ksz_regmap_unlock(void *__mtx)
442013572a2SMarek Vasut {
443013572a2SMarek Vasut 	struct mutex *mtx = __mtx;
444013572a2SMarek Vasut 	mutex_unlock(mtx);
445013572a2SMarek Vasut }
446013572a2SMarek Vasut 
44799b16df0SArun Ramadoss static inline int is_lan937x(struct ksz_device *dev)
44899b16df0SArun Ramadoss {
44999b16df0SArun Ramadoss 	return dev->chip_id == LAN9370_CHIP_ID ||
45099b16df0SArun Ramadoss 		dev->chip_id == LAN9371_CHIP_ID ||
45199b16df0SArun Ramadoss 		dev->chip_id == LAN9372_CHIP_ID ||
45299b16df0SArun Ramadoss 		dev->chip_id == LAN9373_CHIP_ID ||
45399b16df0SArun Ramadoss 		dev->chip_id == LAN9374_CHIP_ID;
45499b16df0SArun Ramadoss }
45599b16df0SArun Ramadoss 
456de6dd626SArun Ramadoss /* STP State Defines */
457de6dd626SArun Ramadoss #define PORT_TX_ENABLE			BIT(2)
458de6dd626SArun Ramadoss #define PORT_RX_ENABLE			BIT(1)
459de6dd626SArun Ramadoss #define PORT_LEARN_DISABLE		BIT(0)
460de6dd626SArun Ramadoss 
46191a98917SArun Ramadoss /* Switch ID Defines */
46291a98917SArun Ramadoss #define REG_CHIP_ID0			0x00
46391a98917SArun Ramadoss 
46491a98917SArun Ramadoss #define SW_FAMILY_ID_M			GENMASK(15, 8)
46591a98917SArun Ramadoss #define KSZ87_FAMILY_ID			0x87
46691a98917SArun Ramadoss #define KSZ88_FAMILY_ID			0x88
46791a98917SArun Ramadoss 
46891a98917SArun Ramadoss #define KSZ8_PORT_STATUS_0		0x08
46991a98917SArun Ramadoss #define KSZ8_PORT_FIBER_MODE		BIT(7)
47091a98917SArun Ramadoss 
47191a98917SArun Ramadoss #define SW_CHIP_ID_M			GENMASK(7, 4)
47291a98917SArun Ramadoss #define KSZ87_CHIP_ID_94		0x6
47391a98917SArun Ramadoss #define KSZ87_CHIP_ID_95		0x9
47491a98917SArun Ramadoss #define KSZ88_CHIP_ID_63		0x3
47591a98917SArun Ramadoss 
47691a98917SArun Ramadoss #define SW_REV_ID_M			GENMASK(7, 4)
47791a98917SArun Ramadoss 
4781ca6437fSArun Ramadoss /* Driver set switch broadcast storm protection at 10% rate. */
4791ca6437fSArun Ramadoss #define BROADCAST_STORM_PROT_RATE	10
4801ca6437fSArun Ramadoss 
4811ca6437fSArun Ramadoss /* 148,800 frames * 67 ms / 100 */
4821ca6437fSArun Ramadoss #define BROADCAST_STORM_VALUE		9969
4831ca6437fSArun Ramadoss 
4841ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE_HI		0x07
4851ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE_LO		0xFF
4861ca6437fSArun Ramadoss #define BROADCAST_STORM_RATE		0x07FF
4871ca6437fSArun Ramadoss 
4880abab9f3SArun Ramadoss #define MULTICAST_STORM_DISABLE		BIT(6)
4890abab9f3SArun Ramadoss 
490ad08ac18SArun Ramadoss #define SW_START			0x01
491ad08ac18SArun Ramadoss 
49246f80fa8SArun Ramadoss /* xMII configuration */
4938560664fSArun Ramadoss #define P_MII_DUPLEX_M			BIT(6)
494aa5b8b73SArun Ramadoss #define P_MII_100MBIT_M			BIT(4)
495aa5b8b73SArun Ramadoss 
49646f80fa8SArun Ramadoss #define P_GMII_1GBIT_M			BIT(6)
497*dc1c596eSArun Ramadoss #define P_RGMII_ID_IG_ENABLE		BIT(4)
498*dc1c596eSArun Ramadoss #define P_RGMII_ID_EG_ENABLE		BIT(3)
499*dc1c596eSArun Ramadoss #define P_MII_SEL_M			0x3
50046f80fa8SArun Ramadoss 
501255b59adSMarek Vasut /* Regmap tables generation */
502255b59adSMarek Vasut #define KSZ_SPI_OP_RD		3
503255b59adSMarek Vasut #define KSZ_SPI_OP_WR		2
504255b59adSMarek Vasut 
50520e03777STristram Ha #define swabnot_used(x)		0
50620e03777STristram Ha 
507255b59adSMarek Vasut #define KSZ_SPI_OP_FLAG_MASK(opcode, swp, regbits, regpad)		\
508255b59adSMarek Vasut 	swab##swp((opcode) << ((regbits) + (regpad)))
509255b59adSMarek Vasut 
510255b59adSMarek Vasut #define KSZ_REGMAP_ENTRY(width, swp, regbits, regpad, regalign)		\
511255b59adSMarek Vasut 	{								\
5125f81d545SGeorge McCollister 		.name = #width,						\
513255b59adSMarek Vasut 		.val_bits = (width),					\
514a3aa6e65SMarek Vasut 		.reg_stride = 1,					\
515255b59adSMarek Vasut 		.reg_bits = (regbits) + (regalign),			\
516255b59adSMarek Vasut 		.pad_bits = (regpad),					\
517255b59adSMarek Vasut 		.max_register = BIT(regbits) - 1,			\
518255b59adSMarek Vasut 		.cache_type = REGCACHE_NONE,				\
519255b59adSMarek Vasut 		.read_flag_mask =					\
520255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_RD, swp,	\
521255b59adSMarek Vasut 					     regbits, regpad),		\
522255b59adSMarek Vasut 		.write_flag_mask =					\
523255b59adSMarek Vasut 			KSZ_SPI_OP_FLAG_MASK(KSZ_SPI_OP_WR, swp,	\
524255b59adSMarek Vasut 					     regbits, regpad),		\
525013572a2SMarek Vasut 		.lock = ksz_regmap_lock,				\
526013572a2SMarek Vasut 		.unlock = ksz_regmap_unlock,				\
527255b59adSMarek Vasut 		.reg_format_endian = REGMAP_ENDIAN_BIG,			\
528255b59adSMarek Vasut 		.val_format_endian = REGMAP_ENDIAN_BIG			\
529255b59adSMarek Vasut 	}
530255b59adSMarek Vasut 
531255b59adSMarek Vasut #define KSZ_REGMAP_TABLE(ksz, swp, regbits, regpad, regalign)		\
532255b59adSMarek Vasut 	static const struct regmap_config ksz##_regmap_config[] = {	\
533255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(8, swp, (regbits), (regpad), (regalign)), \
534255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(16, swp, (regbits), (regpad), (regalign)), \
535255b59adSMarek Vasut 		KSZ_REGMAP_ENTRY(32, swp, (regbits), (regpad), (regalign)), \
536255b59adSMarek Vasut 	}
537255b59adSMarek Vasut 
538c2e86691STristram Ha #endif
539