xref: /linux/drivers/net/dsa/microchip/lan937x_main.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
155ab6ffaSArun Ramadoss // SPDX-License-Identifier: GPL-2.0
255ab6ffaSArun Ramadoss /* Microchip LAN937X switch driver main logic
355ab6ffaSArun Ramadoss  * Copyright (C) 2019-2022 Microchip Technology Inc.
455ab6ffaSArun Ramadoss  */
555ab6ffaSArun Ramadoss #include <linux/kernel.h>
655ab6ffaSArun Ramadoss #include <linux/module.h>
755ab6ffaSArun Ramadoss #include <linux/iopoll.h>
855ab6ffaSArun Ramadoss #include <linux/phy.h>
955ab6ffaSArun Ramadoss #include <linux/of_net.h>
1055ab6ffaSArun Ramadoss #include <linux/if_bridge.h>
11ab882368SArun Ramadoss #include <linux/if_vlan.h>
1255ab6ffaSArun Ramadoss #include <linux/math.h>
1355ab6ffaSArun Ramadoss #include <net/dsa.h>
1455ab6ffaSArun Ramadoss #include <net/switchdev.h>
1555ab6ffaSArun Ramadoss 
1655ab6ffaSArun Ramadoss #include "lan937x_reg.h"
1755ab6ffaSArun Ramadoss #include "ksz_common.h"
18e30f33a5SArun Ramadoss #include "ksz9477.h"
1955ab6ffaSArun Ramadoss #include "lan937x.h"
2055ab6ffaSArun Ramadoss 
lan937x_cfg(struct ksz_device * dev,u32 addr,u8 bits,bool set)2155ab6ffaSArun Ramadoss static int lan937x_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
2255ab6ffaSArun Ramadoss {
23b8311f46SVladimir Oltean 	return regmap_update_bits(ksz_regmap_8(dev), addr, bits, set ? bits : 0);
2455ab6ffaSArun Ramadoss }
2555ab6ffaSArun Ramadoss 
lan937x_port_cfg(struct ksz_device * dev,int port,int offset,u8 bits,bool set)2655ab6ffaSArun Ramadoss static int lan937x_port_cfg(struct ksz_device *dev, int port, int offset,
2755ab6ffaSArun Ramadoss 			    u8 bits, bool set)
2855ab6ffaSArun Ramadoss {
29b8311f46SVladimir Oltean 	return regmap_update_bits(ksz_regmap_8(dev), PORT_CTRL_ADDR(port, offset),
3055ab6ffaSArun Ramadoss 				  bits, set ? bits : 0);
3155ab6ffaSArun Ramadoss }
3255ab6ffaSArun Ramadoss 
lan937x_enable_spi_indirect_access(struct ksz_device * dev)33ffaf1de2SArun Ramadoss static int lan937x_enable_spi_indirect_access(struct ksz_device *dev)
34ffaf1de2SArun Ramadoss {
35ffaf1de2SArun Ramadoss 	u16 data16;
36ffaf1de2SArun Ramadoss 	int ret;
37ffaf1de2SArun Ramadoss 
38ffaf1de2SArun Ramadoss 	/* Enable Phy access through SPI */
39ffaf1de2SArun Ramadoss 	ret = lan937x_cfg(dev, REG_GLOBAL_CTRL_0, SW_PHY_REG_BLOCK, false);
40ffaf1de2SArun Ramadoss 	if (ret < 0)
41ffaf1de2SArun Ramadoss 		return ret;
42ffaf1de2SArun Ramadoss 
43ffaf1de2SArun Ramadoss 	ret = ksz_read16(dev, REG_VPHY_SPECIAL_CTRL__2, &data16);
44ffaf1de2SArun Ramadoss 	if (ret < 0)
45ffaf1de2SArun Ramadoss 		return ret;
46ffaf1de2SArun Ramadoss 
47ffaf1de2SArun Ramadoss 	/* Allow SPI access */
48ffaf1de2SArun Ramadoss 	data16 |= VPHY_SPI_INDIRECT_ENABLE;
49ffaf1de2SArun Ramadoss 
50ffaf1de2SArun Ramadoss 	return ksz_write16(dev, REG_VPHY_SPECIAL_CTRL__2, data16);
51ffaf1de2SArun Ramadoss }
52ffaf1de2SArun Ramadoss 
lan937x_vphy_ind_addr_wr(struct ksz_device * dev,int addr,int reg)53ffaf1de2SArun Ramadoss static int lan937x_vphy_ind_addr_wr(struct ksz_device *dev, int addr, int reg)
54ffaf1de2SArun Ramadoss {
55ffaf1de2SArun Ramadoss 	u16 addr_base = REG_PORT_T1_PHY_CTRL_BASE;
56ffaf1de2SArun Ramadoss 	u16 temp;
57ffaf1de2SArun Ramadoss 
58*5483cbfdSOleksij Rempel 	if (is_lan937x_tx_phy(dev, addr))
598d7330b3SLucas Stach 		addr_base = REG_PORT_TX_PHY_CTRL_BASE;
608d7330b3SLucas Stach 
61ffaf1de2SArun Ramadoss 	/* get register address based on the logical port */
62ffaf1de2SArun Ramadoss 	temp = PORT_CTRL_ADDR(addr, (addr_base + (reg << 2)));
63ffaf1de2SArun Ramadoss 
64ffaf1de2SArun Ramadoss 	return ksz_write16(dev, REG_VPHY_IND_ADDR__2, temp);
65ffaf1de2SArun Ramadoss }
66ffaf1de2SArun Ramadoss 
lan937x_internal_phy_write(struct ksz_device * dev,int addr,int reg,u16 val)67ffaf1de2SArun Ramadoss static int lan937x_internal_phy_write(struct ksz_device *dev, int addr, int reg,
68ffaf1de2SArun Ramadoss 				      u16 val)
69ffaf1de2SArun Ramadoss {
70ffaf1de2SArun Ramadoss 	unsigned int value;
71ffaf1de2SArun Ramadoss 	int ret;
72ffaf1de2SArun Ramadoss 
73ffaf1de2SArun Ramadoss 	/* Check for internal phy port */
74ffaf1de2SArun Ramadoss 	if (!dev->info->internal_phy[addr])
75ffaf1de2SArun Ramadoss 		return -EOPNOTSUPP;
76ffaf1de2SArun Ramadoss 
77ffaf1de2SArun Ramadoss 	ret = lan937x_vphy_ind_addr_wr(dev, addr, reg);
78ffaf1de2SArun Ramadoss 	if (ret < 0)
79ffaf1de2SArun Ramadoss 		return ret;
80ffaf1de2SArun Ramadoss 
81ffaf1de2SArun Ramadoss 	/* Write the data to be written to the VPHY reg */
82ffaf1de2SArun Ramadoss 	ret = ksz_write16(dev, REG_VPHY_IND_DATA__2, val);
83ffaf1de2SArun Ramadoss 	if (ret < 0)
84ffaf1de2SArun Ramadoss 		return ret;
85ffaf1de2SArun Ramadoss 
86ffaf1de2SArun Ramadoss 	/* Write the Write En and Busy bit */
87ffaf1de2SArun Ramadoss 	ret = ksz_write16(dev, REG_VPHY_IND_CTRL__2,
88ffaf1de2SArun Ramadoss 			  (VPHY_IND_WRITE | VPHY_IND_BUSY));
89ffaf1de2SArun Ramadoss 	if (ret < 0)
90ffaf1de2SArun Ramadoss 		return ret;
91ffaf1de2SArun Ramadoss 
92b8311f46SVladimir Oltean 	ret = regmap_read_poll_timeout(ksz_regmap_16(dev), REG_VPHY_IND_CTRL__2,
93ffaf1de2SArun Ramadoss 				       value, !(value & VPHY_IND_BUSY), 10,
94ffaf1de2SArun Ramadoss 				       1000);
95ffaf1de2SArun Ramadoss 	if (ret < 0) {
96ffaf1de2SArun Ramadoss 		dev_err(dev->dev, "Failed to write phy register\n");
97ffaf1de2SArun Ramadoss 		return ret;
98ffaf1de2SArun Ramadoss 	}
99ffaf1de2SArun Ramadoss 
100ffaf1de2SArun Ramadoss 	return 0;
101ffaf1de2SArun Ramadoss }
102ffaf1de2SArun Ramadoss 
lan937x_internal_phy_read(struct ksz_device * dev,int addr,int reg,u16 * val)103ffaf1de2SArun Ramadoss static int lan937x_internal_phy_read(struct ksz_device *dev, int addr, int reg,
104ffaf1de2SArun Ramadoss 				     u16 *val)
105ffaf1de2SArun Ramadoss {
106ffaf1de2SArun Ramadoss 	unsigned int value;
107ffaf1de2SArun Ramadoss 	int ret;
108ffaf1de2SArun Ramadoss 
109ffaf1de2SArun Ramadoss 	/* Check for internal phy port, return 0xffff for non-existent phy */
110ffaf1de2SArun Ramadoss 	if (!dev->info->internal_phy[addr])
111ffaf1de2SArun Ramadoss 		return 0xffff;
112ffaf1de2SArun Ramadoss 
113ffaf1de2SArun Ramadoss 	ret = lan937x_vphy_ind_addr_wr(dev, addr, reg);
114ffaf1de2SArun Ramadoss 	if (ret < 0)
115ffaf1de2SArun Ramadoss 		return ret;
116ffaf1de2SArun Ramadoss 
117ffaf1de2SArun Ramadoss 	/* Write Read and Busy bit to start the transaction */
118ffaf1de2SArun Ramadoss 	ret = ksz_write16(dev, REG_VPHY_IND_CTRL__2, VPHY_IND_BUSY);
119ffaf1de2SArun Ramadoss 	if (ret < 0)
120ffaf1de2SArun Ramadoss 		return ret;
121ffaf1de2SArun Ramadoss 
122b8311f46SVladimir Oltean 	ret = regmap_read_poll_timeout(ksz_regmap_16(dev), REG_VPHY_IND_CTRL__2,
123ffaf1de2SArun Ramadoss 				       value, !(value & VPHY_IND_BUSY), 10,
124ffaf1de2SArun Ramadoss 				       1000);
125ffaf1de2SArun Ramadoss 	if (ret < 0) {
126ffaf1de2SArun Ramadoss 		dev_err(dev->dev, "Failed to read phy register\n");
127ffaf1de2SArun Ramadoss 		return ret;
128ffaf1de2SArun Ramadoss 	}
129ffaf1de2SArun Ramadoss 
130ffaf1de2SArun Ramadoss 	/* Read the VPHY register which has the PHY data */
131ffaf1de2SArun Ramadoss 	return ksz_read16(dev, REG_VPHY_IND_DATA__2, val);
132ffaf1de2SArun Ramadoss }
133ffaf1de2SArun Ramadoss 
lan937x_r_phy(struct ksz_device * dev,u16 addr,u16 reg,u16 * data)1348f420456SOleksij Rempel int lan937x_r_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 *data)
135ffaf1de2SArun Ramadoss {
1368f420456SOleksij Rempel 	return lan937x_internal_phy_read(dev, addr, reg, data);
137ffaf1de2SArun Ramadoss }
138ffaf1de2SArun Ramadoss 
lan937x_w_phy(struct ksz_device * dev,u16 addr,u16 reg,u16 val)1398f420456SOleksij Rempel int lan937x_w_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 val)
140ffaf1de2SArun Ramadoss {
1418f420456SOleksij Rempel 	return lan937x_internal_phy_write(dev, addr, reg, val);
142ffaf1de2SArun Ramadoss }
143ffaf1de2SArun Ramadoss 
lan937x_reset_switch(struct ksz_device * dev)14455ab6ffaSArun Ramadoss int lan937x_reset_switch(struct ksz_device *dev)
14555ab6ffaSArun Ramadoss {
14655ab6ffaSArun Ramadoss 	u32 data32;
14755ab6ffaSArun Ramadoss 	int ret;
14855ab6ffaSArun Ramadoss 
14955ab6ffaSArun Ramadoss 	/* reset switch */
15055ab6ffaSArun Ramadoss 	ret = lan937x_cfg(dev, REG_SW_OPERATION, SW_RESET, true);
15155ab6ffaSArun Ramadoss 	if (ret < 0)
15255ab6ffaSArun Ramadoss 		return ret;
15355ab6ffaSArun Ramadoss 
15455ab6ffaSArun Ramadoss 	/* Enable Auto Aging */
15555ab6ffaSArun Ramadoss 	ret = lan937x_cfg(dev, REG_SW_LUE_CTRL_1, SW_LINK_AUTO_AGING, true);
15655ab6ffaSArun Ramadoss 	if (ret < 0)
15755ab6ffaSArun Ramadoss 		return ret;
15855ab6ffaSArun Ramadoss 
15955ab6ffaSArun Ramadoss 	/* disable interrupts */
16055ab6ffaSArun Ramadoss 	ret = ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK);
16155ab6ffaSArun Ramadoss 	if (ret < 0)
16255ab6ffaSArun Ramadoss 		return ret;
16355ab6ffaSArun Ramadoss 
164f3139362SArun Ramadoss 	ret = ksz_write32(dev, REG_SW_INT_STATUS__4, POR_READY_INT);
165f3139362SArun Ramadoss 	if (ret < 0)
166f3139362SArun Ramadoss 		return ret;
167f3139362SArun Ramadoss 
16855ab6ffaSArun Ramadoss 	ret = ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0xFF);
16955ab6ffaSArun Ramadoss 	if (ret < 0)
17055ab6ffaSArun Ramadoss 		return ret;
17155ab6ffaSArun Ramadoss 
17255ab6ffaSArun Ramadoss 	return ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
17355ab6ffaSArun Ramadoss }
17455ab6ffaSArun Ramadoss 
lan937x_port_setup(struct ksz_device * dev,int port,bool cpu_port)17555ab6ffaSArun Ramadoss void lan937x_port_setup(struct ksz_device *dev, int port, bool cpu_port)
17655ab6ffaSArun Ramadoss {
1778560664fSArun Ramadoss 	const u32 *masks = dev->info->masks;
1788560664fSArun Ramadoss 	const u16 *regs = dev->info->regs;
17955ab6ffaSArun Ramadoss 	struct dsa_switch *ds = dev->ds;
18055ab6ffaSArun Ramadoss 	u8 member;
18155ab6ffaSArun Ramadoss 
18255ab6ffaSArun Ramadoss 	/* enable tag tail for host port */
18355ab6ffaSArun Ramadoss 	if (cpu_port)
18455ab6ffaSArun Ramadoss 		lan937x_port_cfg(dev, port, REG_PORT_CTRL_0,
18555ab6ffaSArun Ramadoss 				 PORT_TAIL_TAG_ENABLE, true);
18655ab6ffaSArun Ramadoss 
187e30f33a5SArun Ramadoss 	/* Enable the Port Queue split */
188e30f33a5SArun Ramadoss 	ksz9477_port_queue_split(dev, port);
189e30f33a5SArun Ramadoss 
19055ab6ffaSArun Ramadoss 	/* set back pressure for half duplex */
19155ab6ffaSArun Ramadoss 	lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE,
19255ab6ffaSArun Ramadoss 			 true);
19355ab6ffaSArun Ramadoss 
19455ab6ffaSArun Ramadoss 	/* enable 802.1p priority */
19555ab6ffaSArun Ramadoss 	lan937x_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true);
19655ab6ffaSArun Ramadoss 
19755ab6ffaSArun Ramadoss 	if (!dev->info->internal_phy[port])
1988560664fSArun Ramadoss 		lan937x_port_cfg(dev, port, regs[P_XMII_CTRL_0],
1998560664fSArun Ramadoss 				 masks[P_MII_TX_FLOW_CTRL] |
2008560664fSArun Ramadoss 				 masks[P_MII_RX_FLOW_CTRL],
20155ab6ffaSArun Ramadoss 				 true);
20255ab6ffaSArun Ramadoss 
20355ab6ffaSArun Ramadoss 	if (cpu_port)
20455ab6ffaSArun Ramadoss 		member = dsa_user_ports(ds);
20555ab6ffaSArun Ramadoss 	else
20655ab6ffaSArun Ramadoss 		member = BIT(dsa_upstream_port(ds, port));
20755ab6ffaSArun Ramadoss 
20855ab6ffaSArun Ramadoss 	dev->dev_ops->cfg_port_member(dev, port, member);
20955ab6ffaSArun Ramadoss }
21055ab6ffaSArun Ramadoss 
lan937x_config_cpu_port(struct dsa_switch * ds)21155ab6ffaSArun Ramadoss void lan937x_config_cpu_port(struct dsa_switch *ds)
21255ab6ffaSArun Ramadoss {
21355ab6ffaSArun Ramadoss 	struct ksz_device *dev = ds->priv;
21455ab6ffaSArun Ramadoss 	struct dsa_port *dp;
21555ab6ffaSArun Ramadoss 
21655ab6ffaSArun Ramadoss 	dsa_switch_for_each_cpu_port(dp, ds) {
21755ab6ffaSArun Ramadoss 		if (dev->info->cpu_ports & (1 << dp->index)) {
21855ab6ffaSArun Ramadoss 			dev->cpu_port = dp->index;
21955ab6ffaSArun Ramadoss 
22055ab6ffaSArun Ramadoss 			/* enable cpu port */
22155ab6ffaSArun Ramadoss 			lan937x_port_setup(dev, dp->index, true);
22255ab6ffaSArun Ramadoss 		}
22355ab6ffaSArun Ramadoss 	}
22455ab6ffaSArun Ramadoss 
22555ab6ffaSArun Ramadoss 	dsa_switch_for_each_user_port(dp, ds) {
22655ab6ffaSArun Ramadoss 		ksz_port_stp_state_set(ds, dp->index, BR_STATE_DISABLED);
22755ab6ffaSArun Ramadoss 	}
22855ab6ffaSArun Ramadoss }
22955ab6ffaSArun Ramadoss 
lan937x_change_mtu(struct ksz_device * dev,int port,int new_mtu)230ab882368SArun Ramadoss int lan937x_change_mtu(struct ksz_device *dev, int port, int new_mtu)
231ab882368SArun Ramadoss {
232ab882368SArun Ramadoss 	struct dsa_switch *ds = dev->ds;
233ab882368SArun Ramadoss 	int ret;
234ab882368SArun Ramadoss 
235ab882368SArun Ramadoss 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
236ab882368SArun Ramadoss 
237ab882368SArun Ramadoss 	if (dsa_is_cpu_port(ds, port))
238ab882368SArun Ramadoss 		new_mtu += LAN937X_TAG_LEN;
239ab882368SArun Ramadoss 
240ab882368SArun Ramadoss 	if (new_mtu >= FR_MIN_SIZE)
241ab882368SArun Ramadoss 		ret = lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_0,
242ab882368SArun Ramadoss 				       PORT_JUMBO_PACKET, true);
243ab882368SArun Ramadoss 	else
244ab882368SArun Ramadoss 		ret = lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_0,
245ab882368SArun Ramadoss 				       PORT_JUMBO_PACKET, false);
246ab882368SArun Ramadoss 	if (ret < 0) {
247ab882368SArun Ramadoss 		dev_err(ds->dev, "failed to enable jumbo\n");
248ab882368SArun Ramadoss 		return ret;
249ab882368SArun Ramadoss 	}
250ab882368SArun Ramadoss 
251ab882368SArun Ramadoss 	/* Write the frame size in PORT_MAX_FR_SIZE register */
252e06999c3SRakesh Sankaranarayanan 	ret = ksz_pwrite16(dev, port, PORT_MAX_FR_SIZE, new_mtu);
253e06999c3SRakesh Sankaranarayanan 	if (ret) {
254e06999c3SRakesh Sankaranarayanan 		dev_err(ds->dev, "failed to update mtu for port %d\n", port);
255e06999c3SRakesh Sankaranarayanan 		return ret;
256e06999c3SRakesh Sankaranarayanan 	}
257ab882368SArun Ramadoss 
258ab882368SArun Ramadoss 	return 0;
259ab882368SArun Ramadoss }
260ab882368SArun Ramadoss 
lan937x_set_ageing_time(struct ksz_device * dev,unsigned int msecs)2612c119d99SArun Ramadoss int lan937x_set_ageing_time(struct ksz_device *dev, unsigned int msecs)
2622c119d99SArun Ramadoss {
2632c119d99SArun Ramadoss 	u32 secs = msecs / 1000;
2642c119d99SArun Ramadoss 	u32 value;
2652c119d99SArun Ramadoss 	int ret;
2662c119d99SArun Ramadoss 
2672c119d99SArun Ramadoss 	value = FIELD_GET(SW_AGE_PERIOD_7_0_M, secs);
2682c119d99SArun Ramadoss 
2692c119d99SArun Ramadoss 	ret = ksz_write8(dev, REG_SW_AGE_PERIOD__1, value);
2702c119d99SArun Ramadoss 	if (ret < 0)
2712c119d99SArun Ramadoss 		return ret;
2722c119d99SArun Ramadoss 
2732c119d99SArun Ramadoss 	value = FIELD_GET(SW_AGE_PERIOD_19_8_M, secs);
2742c119d99SArun Ramadoss 
2752c119d99SArun Ramadoss 	return ksz_write16(dev, REG_SW_AGE_PERIOD__2, value);
2762c119d99SArun Ramadoss }
2772c119d99SArun Ramadoss 
lan937x_set_tune_adj(struct ksz_device * dev,int port,u16 reg,u8 val)278b19ac41fSArun Ramadoss static void lan937x_set_tune_adj(struct ksz_device *dev, int port,
279b19ac41fSArun Ramadoss 				 u16 reg, u8 val)
280b19ac41fSArun Ramadoss {
281b19ac41fSArun Ramadoss 	u16 data16;
282b19ac41fSArun Ramadoss 
283b19ac41fSArun Ramadoss 	ksz_pread16(dev, port, reg, &data16);
284b19ac41fSArun Ramadoss 
285b19ac41fSArun Ramadoss 	/* Update tune Adjust */
286b19ac41fSArun Ramadoss 	data16 |= FIELD_PREP(PORT_TUNE_ADJ, val);
287b19ac41fSArun Ramadoss 	ksz_pwrite16(dev, port, reg, data16);
288b19ac41fSArun Ramadoss 
289b19ac41fSArun Ramadoss 	/* write DLL reset to take effect */
290b19ac41fSArun Ramadoss 	data16 |= PORT_DLL_RESET;
291b19ac41fSArun Ramadoss 	ksz_pwrite16(dev, port, reg, data16);
292b19ac41fSArun Ramadoss }
293b19ac41fSArun Ramadoss 
lan937x_set_rgmii_tx_delay(struct ksz_device * dev,int port)294b19ac41fSArun Ramadoss static void lan937x_set_rgmii_tx_delay(struct ksz_device *dev, int port)
295b19ac41fSArun Ramadoss {
296b19ac41fSArun Ramadoss 	u8 val;
297b19ac41fSArun Ramadoss 
298b19ac41fSArun Ramadoss 	/* Apply different codes based on the ports as per characterization
299b19ac41fSArun Ramadoss 	 * results
300b19ac41fSArun Ramadoss 	 */
301b19ac41fSArun Ramadoss 	val = (port == LAN937X_RGMII_1_PORT) ? RGMII_1_TX_DELAY_2NS :
302b19ac41fSArun Ramadoss 		RGMII_2_TX_DELAY_2NS;
303b19ac41fSArun Ramadoss 
304b19ac41fSArun Ramadoss 	lan937x_set_tune_adj(dev, port, REG_PORT_XMII_CTRL_5, val);
305b19ac41fSArun Ramadoss }
306b19ac41fSArun Ramadoss 
lan937x_set_rgmii_rx_delay(struct ksz_device * dev,int port)307b19ac41fSArun Ramadoss static void lan937x_set_rgmii_rx_delay(struct ksz_device *dev, int port)
308b19ac41fSArun Ramadoss {
309b19ac41fSArun Ramadoss 	u8 val;
310b19ac41fSArun Ramadoss 
311b19ac41fSArun Ramadoss 	val = (port == LAN937X_RGMII_1_PORT) ? RGMII_1_RX_DELAY_2NS :
312b19ac41fSArun Ramadoss 		RGMII_2_RX_DELAY_2NS;
313b19ac41fSArun Ramadoss 
314b19ac41fSArun Ramadoss 	lan937x_set_tune_adj(dev, port, REG_PORT_XMII_CTRL_4, val);
315b19ac41fSArun Ramadoss }
316b19ac41fSArun Ramadoss 
lan937x_phylink_get_caps(struct ksz_device * dev,int port,struct phylink_config * config)317c14e878dSArun Ramadoss void lan937x_phylink_get_caps(struct ksz_device *dev, int port,
318c14e878dSArun Ramadoss 			      struct phylink_config *config)
319c14e878dSArun Ramadoss {
320c14e878dSArun Ramadoss 	config->mac_capabilities = MAC_100FD;
321c14e878dSArun Ramadoss 
322c14e878dSArun Ramadoss 	if (dev->info->supports_rgmii[port]) {
323c14e878dSArun Ramadoss 		/* MII/RMII/RGMII ports */
324c14e878dSArun Ramadoss 		config->mac_capabilities |= MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
325c14e878dSArun Ramadoss 					    MAC_100HD | MAC_10 | MAC_1000FD;
326*5483cbfdSOleksij Rempel 	} else if (is_lan937x_tx_phy(dev, port)) {
327*5483cbfdSOleksij Rempel 		config->mac_capabilities |= MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
328*5483cbfdSOleksij Rempel 					    MAC_100HD | MAC_10;
329c14e878dSArun Ramadoss 	}
330c14e878dSArun Ramadoss }
331c14e878dSArun Ramadoss 
lan937x_setup_rgmii_delay(struct ksz_device * dev,int port)332b19ac41fSArun Ramadoss void lan937x_setup_rgmii_delay(struct ksz_device *dev, int port)
333b19ac41fSArun Ramadoss {
334b19ac41fSArun Ramadoss 	struct ksz_port *p = &dev->ports[port];
335b19ac41fSArun Ramadoss 
336b19ac41fSArun Ramadoss 	if (p->rgmii_tx_val) {
337b19ac41fSArun Ramadoss 		lan937x_set_rgmii_tx_delay(dev, port);
338b19ac41fSArun Ramadoss 		dev_info(dev->dev, "Applied rgmii tx delay for the port %d\n",
339b19ac41fSArun Ramadoss 			 port);
340b19ac41fSArun Ramadoss 	}
341b19ac41fSArun Ramadoss 
342b19ac41fSArun Ramadoss 	if (p->rgmii_rx_val) {
343b19ac41fSArun Ramadoss 		lan937x_set_rgmii_rx_delay(dev, port);
344b19ac41fSArun Ramadoss 		dev_info(dev->dev, "Applied rgmii rx delay for the port %d\n",
345b19ac41fSArun Ramadoss 			 port);
346b19ac41fSArun Ramadoss 	}
347b19ac41fSArun Ramadoss }
348b19ac41fSArun Ramadoss 
lan937x_tc_cbs_set_cinc(struct ksz_device * dev,int port,u32 val)34971d7920fSArun Ramadoss int lan937x_tc_cbs_set_cinc(struct ksz_device *dev, int port, u32 val)
35071d7920fSArun Ramadoss {
35171d7920fSArun Ramadoss 	return ksz_pwrite32(dev, port, REG_PORT_MTI_CREDIT_INCREMENT, val);
35271d7920fSArun Ramadoss }
35371d7920fSArun Ramadoss 
lan937x_switch_init(struct ksz_device * dev)354c9cd961cSArun Ramadoss int lan937x_switch_init(struct ksz_device *dev)
355c9cd961cSArun Ramadoss {
356c9cd961cSArun Ramadoss 	dev->port_mask = (1 << dev->info->port_cnt) - 1;
357c9cd961cSArun Ramadoss 
358c9cd961cSArun Ramadoss 	return 0;
359c9cd961cSArun Ramadoss }
360c9cd961cSArun Ramadoss 
lan937x_setup(struct dsa_switch * ds)36155ab6ffaSArun Ramadoss int lan937x_setup(struct dsa_switch *ds)
36255ab6ffaSArun Ramadoss {
36355ab6ffaSArun Ramadoss 	struct ksz_device *dev = ds->priv;
364ffaf1de2SArun Ramadoss 	int ret;
365ffaf1de2SArun Ramadoss 
366ffaf1de2SArun Ramadoss 	/* enable Indirect Access from SPI to the VPHY registers */
367ffaf1de2SArun Ramadoss 	ret = lan937x_enable_spi_indirect_access(dev);
368ffaf1de2SArun Ramadoss 	if (ret < 0) {
369ffaf1de2SArun Ramadoss 		dev_err(dev->dev, "failed to enable spi indirect access");
370ffaf1de2SArun Ramadoss 		return ret;
371ffaf1de2SArun Ramadoss 	}
37255ab6ffaSArun Ramadoss 
37355ab6ffaSArun Ramadoss 	/* The VLAN aware is a global setting. Mixed vlan
37455ab6ffaSArun Ramadoss 	 * filterings are not supported.
37555ab6ffaSArun Ramadoss 	 */
37655ab6ffaSArun Ramadoss 	ds->vlan_filtering_is_global = true;
37755ab6ffaSArun Ramadoss 
37855ab6ffaSArun Ramadoss 	/* Enable aggressive back off for half duplex & UNH mode */
379aa77b112SOleksij Rempel 	ret = lan937x_cfg(dev, REG_SW_MAC_CTRL_0, (SW_PAUSE_UNH_MODE |
380aa77b112SOleksij Rempel 						   SW_NEW_BACKOFF |
381aa77b112SOleksij Rempel 						   SW_AGGR_BACKOFF), true);
382aa77b112SOleksij Rempel 	if (ret < 0)
383aa77b112SOleksij Rempel 		return ret;
38455ab6ffaSArun Ramadoss 
38555ab6ffaSArun Ramadoss 	/* If NO_EXC_COLLISION_DROP bit is set, the switch will not drop
38655ab6ffaSArun Ramadoss 	 * packets when 16 or more collisions occur
38755ab6ffaSArun Ramadoss 	 */
388aa77b112SOleksij Rempel 	ret = lan937x_cfg(dev, REG_SW_MAC_CTRL_1, NO_EXC_COLLISION_DROP, true);
389aa77b112SOleksij Rempel 	if (ret < 0)
390aa77b112SOleksij Rempel 		return ret;
39155ab6ffaSArun Ramadoss 
39255ab6ffaSArun Ramadoss 	/* enable global MIB counter freeze function */
393aa77b112SOleksij Rempel 	ret = lan937x_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
394aa77b112SOleksij Rempel 	if (ret < 0)
395aa77b112SOleksij Rempel 		return ret;
39655ab6ffaSArun Ramadoss 
39755ab6ffaSArun Ramadoss 	/* disable CLK125 & CLK25, 1: disable, 0: enable */
398aa77b112SOleksij Rempel 	ret = lan937x_cfg(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1,
39955ab6ffaSArun Ramadoss 			  (SW_CLK125_ENB | SW_CLK25_ENB), true);
400aa77b112SOleksij Rempel 	if (ret < 0)
401aa77b112SOleksij Rempel 		return ret;
40255ab6ffaSArun Ramadoss 
4032e3ed20cSLucas Stach 	/* Disable global VPHY support. Related to CPU interface only? */
404aa77b112SOleksij Rempel 	return ksz_rmw32(dev, REG_SW_CFG_STRAP_OVR, SW_VPHY_DISABLE,
405aa77b112SOleksij Rempel 			 SW_VPHY_DISABLE);
40655ab6ffaSArun Ramadoss }
40755ab6ffaSArun Ramadoss 
lan937x_teardown(struct dsa_switch * ds)408c9cd961cSArun Ramadoss void lan937x_teardown(struct dsa_switch *ds)
40955ab6ffaSArun Ramadoss {
41055ab6ffaSArun Ramadoss 
41155ab6ffaSArun Ramadoss }
41255ab6ffaSArun Ramadoss 
lan937x_switch_exit(struct ksz_device * dev)41355ab6ffaSArun Ramadoss void lan937x_switch_exit(struct ksz_device *dev)
41455ab6ffaSArun Ramadoss {
41555ab6ffaSArun Ramadoss 	lan937x_reset_switch(dev);
41655ab6ffaSArun Ramadoss }
41755ab6ffaSArun Ramadoss 
41855ab6ffaSArun Ramadoss MODULE_AUTHOR("Arun Ramadoss <arun.ramadoss@microchip.com>");
41955ab6ffaSArun Ramadoss MODULE_DESCRIPTION("Microchip LAN937x Series Switch DSA Driver");
42055ab6ffaSArun Ramadoss MODULE_LICENSE("GPL");
421