xref: /linux/drivers/net/dsa/microchip/lan937x_main.c (revision f313936261ac18a2527fc0752cf988950587d0ce)
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>
10a50b3536SArun Ramadoss #include <linux/of_mdio.h>
1155ab6ffaSArun Ramadoss #include <linux/if_bridge.h>
12ab882368SArun Ramadoss #include <linux/if_vlan.h>
1355ab6ffaSArun Ramadoss #include <linux/math.h>
1455ab6ffaSArun Ramadoss #include <net/dsa.h>
1555ab6ffaSArun Ramadoss #include <net/switchdev.h>
1655ab6ffaSArun Ramadoss 
1755ab6ffaSArun Ramadoss #include "lan937x_reg.h"
1855ab6ffaSArun Ramadoss #include "ksz_common.h"
1955ab6ffaSArun Ramadoss #include "lan937x.h"
2055ab6ffaSArun Ramadoss 
2155ab6ffaSArun Ramadoss static int lan937x_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
2255ab6ffaSArun Ramadoss {
2355ab6ffaSArun Ramadoss 	return regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
2455ab6ffaSArun Ramadoss }
2555ab6ffaSArun Ramadoss 
2655ab6ffaSArun Ramadoss static int lan937x_port_cfg(struct ksz_device *dev, int port, int offset,
2755ab6ffaSArun Ramadoss 			    u8 bits, bool set)
2855ab6ffaSArun Ramadoss {
2955ab6ffaSArun Ramadoss 	return regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
3055ab6ffaSArun Ramadoss 				  bits, set ? bits : 0);
3155ab6ffaSArun Ramadoss }
3255ab6ffaSArun Ramadoss 
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 
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 
58ffaf1de2SArun Ramadoss 	/* get register address based on the logical port */
59ffaf1de2SArun Ramadoss 	temp = PORT_CTRL_ADDR(addr, (addr_base + (reg << 2)));
60ffaf1de2SArun Ramadoss 
61ffaf1de2SArun Ramadoss 	return ksz_write16(dev, REG_VPHY_IND_ADDR__2, temp);
62ffaf1de2SArun Ramadoss }
63ffaf1de2SArun Ramadoss 
64ffaf1de2SArun Ramadoss static int lan937x_internal_phy_write(struct ksz_device *dev, int addr, int reg,
65ffaf1de2SArun Ramadoss 				      u16 val)
66ffaf1de2SArun Ramadoss {
67ffaf1de2SArun Ramadoss 	unsigned int value;
68ffaf1de2SArun Ramadoss 	int ret;
69ffaf1de2SArun Ramadoss 
70ffaf1de2SArun Ramadoss 	/* Check for internal phy port */
71ffaf1de2SArun Ramadoss 	if (!dev->info->internal_phy[addr])
72ffaf1de2SArun Ramadoss 		return -EOPNOTSUPP;
73ffaf1de2SArun Ramadoss 
74ffaf1de2SArun Ramadoss 	ret = lan937x_vphy_ind_addr_wr(dev, addr, reg);
75ffaf1de2SArun Ramadoss 	if (ret < 0)
76ffaf1de2SArun Ramadoss 		return ret;
77ffaf1de2SArun Ramadoss 
78ffaf1de2SArun Ramadoss 	/* Write the data to be written to the VPHY reg */
79ffaf1de2SArun Ramadoss 	ret = ksz_write16(dev, REG_VPHY_IND_DATA__2, val);
80ffaf1de2SArun Ramadoss 	if (ret < 0)
81ffaf1de2SArun Ramadoss 		return ret;
82ffaf1de2SArun Ramadoss 
83ffaf1de2SArun Ramadoss 	/* Write the Write En and Busy bit */
84ffaf1de2SArun Ramadoss 	ret = ksz_write16(dev, REG_VPHY_IND_CTRL__2,
85ffaf1de2SArun Ramadoss 			  (VPHY_IND_WRITE | VPHY_IND_BUSY));
86ffaf1de2SArun Ramadoss 	if (ret < 0)
87ffaf1de2SArun Ramadoss 		return ret;
88ffaf1de2SArun Ramadoss 
89ffaf1de2SArun Ramadoss 	ret = regmap_read_poll_timeout(dev->regmap[1], REG_VPHY_IND_CTRL__2,
90ffaf1de2SArun Ramadoss 				       value, !(value & VPHY_IND_BUSY), 10,
91ffaf1de2SArun Ramadoss 				       1000);
92ffaf1de2SArun Ramadoss 	if (ret < 0) {
93ffaf1de2SArun Ramadoss 		dev_err(dev->dev, "Failed to write phy register\n");
94ffaf1de2SArun Ramadoss 		return ret;
95ffaf1de2SArun Ramadoss 	}
96ffaf1de2SArun Ramadoss 
97ffaf1de2SArun Ramadoss 	return 0;
98ffaf1de2SArun Ramadoss }
99ffaf1de2SArun Ramadoss 
100ffaf1de2SArun Ramadoss static int lan937x_internal_phy_read(struct ksz_device *dev, int addr, int reg,
101ffaf1de2SArun Ramadoss 				     u16 *val)
102ffaf1de2SArun Ramadoss {
103ffaf1de2SArun Ramadoss 	unsigned int value;
104ffaf1de2SArun Ramadoss 	int ret;
105ffaf1de2SArun Ramadoss 
106ffaf1de2SArun Ramadoss 	/* Check for internal phy port, return 0xffff for non-existent phy */
107ffaf1de2SArun Ramadoss 	if (!dev->info->internal_phy[addr])
108ffaf1de2SArun Ramadoss 		return 0xffff;
109ffaf1de2SArun Ramadoss 
110ffaf1de2SArun Ramadoss 	ret = lan937x_vphy_ind_addr_wr(dev, addr, reg);
111ffaf1de2SArun Ramadoss 	if (ret < 0)
112ffaf1de2SArun Ramadoss 		return ret;
113ffaf1de2SArun Ramadoss 
114ffaf1de2SArun Ramadoss 	/* Write Read and Busy bit to start the transaction */
115ffaf1de2SArun Ramadoss 	ret = ksz_write16(dev, REG_VPHY_IND_CTRL__2, VPHY_IND_BUSY);
116ffaf1de2SArun Ramadoss 	if (ret < 0)
117ffaf1de2SArun Ramadoss 		return ret;
118ffaf1de2SArun Ramadoss 
119ffaf1de2SArun Ramadoss 	ret = regmap_read_poll_timeout(dev->regmap[1], REG_VPHY_IND_CTRL__2,
120ffaf1de2SArun Ramadoss 				       value, !(value & VPHY_IND_BUSY), 10,
121ffaf1de2SArun Ramadoss 				       1000);
122ffaf1de2SArun Ramadoss 	if (ret < 0) {
123ffaf1de2SArun Ramadoss 		dev_err(dev->dev, "Failed to read phy register\n");
124ffaf1de2SArun Ramadoss 		return ret;
125ffaf1de2SArun Ramadoss 	}
126ffaf1de2SArun Ramadoss 
127ffaf1de2SArun Ramadoss 	/* Read the VPHY register which has the PHY data */
128ffaf1de2SArun Ramadoss 	return ksz_read16(dev, REG_VPHY_IND_DATA__2, val);
129ffaf1de2SArun Ramadoss }
130ffaf1de2SArun Ramadoss 
1318f420456SOleksij Rempel int lan937x_r_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 *data)
132ffaf1de2SArun Ramadoss {
1338f420456SOleksij Rempel 	return lan937x_internal_phy_read(dev, addr, reg, data);
134ffaf1de2SArun Ramadoss }
135ffaf1de2SArun Ramadoss 
1368f420456SOleksij Rempel int lan937x_w_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 val)
137ffaf1de2SArun Ramadoss {
1388f420456SOleksij Rempel 	return lan937x_internal_phy_write(dev, addr, reg, val);
139ffaf1de2SArun Ramadoss }
140ffaf1de2SArun Ramadoss 
141a50b3536SArun Ramadoss static int lan937x_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
142a50b3536SArun Ramadoss {
143a50b3536SArun Ramadoss 	struct ksz_device *dev = bus->priv;
144a50b3536SArun Ramadoss 	u16 val;
145a50b3536SArun Ramadoss 	int ret;
146a50b3536SArun Ramadoss 
147a50b3536SArun Ramadoss 	if (regnum & MII_ADDR_C45)
148a50b3536SArun Ramadoss 		return -EOPNOTSUPP;
149a50b3536SArun Ramadoss 
150a50b3536SArun Ramadoss 	ret = lan937x_internal_phy_read(dev, addr, regnum, &val);
151a50b3536SArun Ramadoss 	if (ret < 0)
152a50b3536SArun Ramadoss 		return ret;
153a50b3536SArun Ramadoss 
154a50b3536SArun Ramadoss 	return val;
155a50b3536SArun Ramadoss }
156a50b3536SArun Ramadoss 
157a50b3536SArun Ramadoss static int lan937x_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
158a50b3536SArun Ramadoss 				 u16 val)
159a50b3536SArun Ramadoss {
160a50b3536SArun Ramadoss 	struct ksz_device *dev = bus->priv;
161a50b3536SArun Ramadoss 
162a50b3536SArun Ramadoss 	if (regnum & MII_ADDR_C45)
163a50b3536SArun Ramadoss 		return -EOPNOTSUPP;
164a50b3536SArun Ramadoss 
165a50b3536SArun Ramadoss 	return lan937x_internal_phy_write(dev, addr, regnum, val);
166a50b3536SArun Ramadoss }
167a50b3536SArun Ramadoss 
168a50b3536SArun Ramadoss static int lan937x_mdio_register(struct ksz_device *dev)
169a50b3536SArun Ramadoss {
170a50b3536SArun Ramadoss 	struct dsa_switch *ds = dev->ds;
171a50b3536SArun Ramadoss 	struct device_node *mdio_np;
172a50b3536SArun Ramadoss 	struct mii_bus *bus;
173a50b3536SArun Ramadoss 	int ret;
174a50b3536SArun Ramadoss 
175a50b3536SArun Ramadoss 	mdio_np = of_get_child_by_name(dev->dev->of_node, "mdio");
176a50b3536SArun Ramadoss 	if (!mdio_np) {
177a50b3536SArun Ramadoss 		dev_err(ds->dev, "no MDIO bus node\n");
178a50b3536SArun Ramadoss 		return -ENODEV;
179a50b3536SArun Ramadoss 	}
180a50b3536SArun Ramadoss 
181a50b3536SArun Ramadoss 	bus = devm_mdiobus_alloc(ds->dev);
182a50b3536SArun Ramadoss 	if (!bus) {
183a50b3536SArun Ramadoss 		of_node_put(mdio_np);
184a50b3536SArun Ramadoss 		return -ENOMEM;
185a50b3536SArun Ramadoss 	}
186a50b3536SArun Ramadoss 
187a50b3536SArun Ramadoss 	bus->priv = dev;
188a50b3536SArun Ramadoss 	bus->read = lan937x_sw_mdio_read;
189a50b3536SArun Ramadoss 	bus->write = lan937x_sw_mdio_write;
190a50b3536SArun Ramadoss 	bus->name = "lan937x slave smi";
191a50b3536SArun Ramadoss 	snprintf(bus->id, MII_BUS_ID_SIZE, "SMI-%d", ds->index);
192a50b3536SArun Ramadoss 	bus->parent = ds->dev;
193a50b3536SArun Ramadoss 	bus->phy_mask = ~ds->phys_mii_mask;
194a50b3536SArun Ramadoss 
195a50b3536SArun Ramadoss 	ds->slave_mii_bus = bus;
196a50b3536SArun Ramadoss 
197a50b3536SArun Ramadoss 	ret = devm_of_mdiobus_register(ds->dev, bus, mdio_np);
198a50b3536SArun Ramadoss 	if (ret) {
199a50b3536SArun Ramadoss 		dev_err(ds->dev, "unable to register MDIO bus %s\n",
200a50b3536SArun Ramadoss 			bus->id);
201a50b3536SArun Ramadoss 	}
202a50b3536SArun Ramadoss 
203a50b3536SArun Ramadoss 	of_node_put(mdio_np);
204a50b3536SArun Ramadoss 
205a50b3536SArun Ramadoss 	return ret;
206a50b3536SArun Ramadoss }
207a50b3536SArun Ramadoss 
20855ab6ffaSArun Ramadoss int lan937x_reset_switch(struct ksz_device *dev)
20955ab6ffaSArun Ramadoss {
21055ab6ffaSArun Ramadoss 	u32 data32;
21155ab6ffaSArun Ramadoss 	int ret;
21255ab6ffaSArun Ramadoss 
21355ab6ffaSArun Ramadoss 	/* reset switch */
21455ab6ffaSArun Ramadoss 	ret = lan937x_cfg(dev, REG_SW_OPERATION, SW_RESET, true);
21555ab6ffaSArun Ramadoss 	if (ret < 0)
21655ab6ffaSArun Ramadoss 		return ret;
21755ab6ffaSArun Ramadoss 
21855ab6ffaSArun Ramadoss 	/* Enable Auto Aging */
21955ab6ffaSArun Ramadoss 	ret = lan937x_cfg(dev, REG_SW_LUE_CTRL_1, SW_LINK_AUTO_AGING, true);
22055ab6ffaSArun Ramadoss 	if (ret < 0)
22155ab6ffaSArun Ramadoss 		return ret;
22255ab6ffaSArun Ramadoss 
22355ab6ffaSArun Ramadoss 	/* disable interrupts */
22455ab6ffaSArun Ramadoss 	ret = ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK);
22555ab6ffaSArun Ramadoss 	if (ret < 0)
22655ab6ffaSArun Ramadoss 		return ret;
22755ab6ffaSArun Ramadoss 
228*f3139362SArun Ramadoss 	ret = ksz_write32(dev, REG_SW_INT_STATUS__4, POR_READY_INT);
229*f3139362SArun Ramadoss 	if (ret < 0)
230*f3139362SArun Ramadoss 		return ret;
231*f3139362SArun Ramadoss 
23255ab6ffaSArun Ramadoss 	ret = ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0xFF);
23355ab6ffaSArun Ramadoss 	if (ret < 0)
23455ab6ffaSArun Ramadoss 		return ret;
23555ab6ffaSArun Ramadoss 
23655ab6ffaSArun Ramadoss 	return ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
23755ab6ffaSArun Ramadoss }
23855ab6ffaSArun Ramadoss 
23955ab6ffaSArun Ramadoss void lan937x_port_setup(struct ksz_device *dev, int port, bool cpu_port)
24055ab6ffaSArun Ramadoss {
2418560664fSArun Ramadoss 	const u32 *masks = dev->info->masks;
2428560664fSArun Ramadoss 	const u16 *regs = dev->info->regs;
24355ab6ffaSArun Ramadoss 	struct dsa_switch *ds = dev->ds;
24455ab6ffaSArun Ramadoss 	u8 member;
24555ab6ffaSArun Ramadoss 
24655ab6ffaSArun Ramadoss 	/* enable tag tail for host port */
24755ab6ffaSArun Ramadoss 	if (cpu_port)
24855ab6ffaSArun Ramadoss 		lan937x_port_cfg(dev, port, REG_PORT_CTRL_0,
24955ab6ffaSArun Ramadoss 				 PORT_TAIL_TAG_ENABLE, true);
25055ab6ffaSArun Ramadoss 
25155ab6ffaSArun Ramadoss 	/* disable frame check length field */
25255ab6ffaSArun Ramadoss 	lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_0, PORT_CHECK_LENGTH,
25355ab6ffaSArun Ramadoss 			 false);
25455ab6ffaSArun Ramadoss 
25555ab6ffaSArun Ramadoss 	/* set back pressure for half duplex */
25655ab6ffaSArun Ramadoss 	lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE,
25755ab6ffaSArun Ramadoss 			 true);
25855ab6ffaSArun Ramadoss 
25955ab6ffaSArun Ramadoss 	/* enable 802.1p priority */
26055ab6ffaSArun Ramadoss 	lan937x_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true);
26155ab6ffaSArun Ramadoss 
26255ab6ffaSArun Ramadoss 	if (!dev->info->internal_phy[port])
2638560664fSArun Ramadoss 		lan937x_port_cfg(dev, port, regs[P_XMII_CTRL_0],
2648560664fSArun Ramadoss 				 masks[P_MII_TX_FLOW_CTRL] |
2658560664fSArun Ramadoss 				 masks[P_MII_RX_FLOW_CTRL],
26655ab6ffaSArun Ramadoss 				 true);
26755ab6ffaSArun Ramadoss 
26855ab6ffaSArun Ramadoss 	if (cpu_port)
26955ab6ffaSArun Ramadoss 		member = dsa_user_ports(ds);
27055ab6ffaSArun Ramadoss 	else
27155ab6ffaSArun Ramadoss 		member = BIT(dsa_upstream_port(ds, port));
27255ab6ffaSArun Ramadoss 
27355ab6ffaSArun Ramadoss 	dev->dev_ops->cfg_port_member(dev, port, member);
27455ab6ffaSArun Ramadoss }
27555ab6ffaSArun Ramadoss 
27655ab6ffaSArun Ramadoss void lan937x_config_cpu_port(struct dsa_switch *ds)
27755ab6ffaSArun Ramadoss {
27855ab6ffaSArun Ramadoss 	struct ksz_device *dev = ds->priv;
27955ab6ffaSArun Ramadoss 	struct dsa_port *dp;
28055ab6ffaSArun Ramadoss 
28155ab6ffaSArun Ramadoss 	dsa_switch_for_each_cpu_port(dp, ds) {
28255ab6ffaSArun Ramadoss 		if (dev->info->cpu_ports & (1 << dp->index)) {
28355ab6ffaSArun Ramadoss 			dev->cpu_port = dp->index;
28455ab6ffaSArun Ramadoss 
28555ab6ffaSArun Ramadoss 			/* enable cpu port */
28655ab6ffaSArun Ramadoss 			lan937x_port_setup(dev, dp->index, true);
28755ab6ffaSArun Ramadoss 		}
28855ab6ffaSArun Ramadoss 	}
28955ab6ffaSArun Ramadoss 
29055ab6ffaSArun Ramadoss 	dsa_switch_for_each_user_port(dp, ds) {
29155ab6ffaSArun Ramadoss 		ksz_port_stp_state_set(ds, dp->index, BR_STATE_DISABLED);
29255ab6ffaSArun Ramadoss 	}
29355ab6ffaSArun Ramadoss }
29455ab6ffaSArun Ramadoss 
295ab882368SArun Ramadoss int lan937x_change_mtu(struct ksz_device *dev, int port, int new_mtu)
296ab882368SArun Ramadoss {
297ab882368SArun Ramadoss 	struct dsa_switch *ds = dev->ds;
298ab882368SArun Ramadoss 	int ret;
299ab882368SArun Ramadoss 
300ab882368SArun Ramadoss 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
301ab882368SArun Ramadoss 
302ab882368SArun Ramadoss 	if (dsa_is_cpu_port(ds, port))
303ab882368SArun Ramadoss 		new_mtu += LAN937X_TAG_LEN;
304ab882368SArun Ramadoss 
305ab882368SArun Ramadoss 	if (new_mtu >= FR_MIN_SIZE)
306ab882368SArun Ramadoss 		ret = lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_0,
307ab882368SArun Ramadoss 				       PORT_JUMBO_PACKET, true);
308ab882368SArun Ramadoss 	else
309ab882368SArun Ramadoss 		ret = lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_0,
310ab882368SArun Ramadoss 				       PORT_JUMBO_PACKET, false);
311ab882368SArun Ramadoss 	if (ret < 0) {
312ab882368SArun Ramadoss 		dev_err(ds->dev, "failed to enable jumbo\n");
313ab882368SArun Ramadoss 		return ret;
314ab882368SArun Ramadoss 	}
315ab882368SArun Ramadoss 
316ab882368SArun Ramadoss 	/* Write the frame size in PORT_MAX_FR_SIZE register */
317ab882368SArun Ramadoss 	ksz_pwrite16(dev, port, PORT_MAX_FR_SIZE, new_mtu);
318ab882368SArun Ramadoss 
319ab882368SArun Ramadoss 	return 0;
320ab882368SArun Ramadoss }
321ab882368SArun Ramadoss 
322b19ac41fSArun Ramadoss static void lan937x_set_tune_adj(struct ksz_device *dev, int port,
323b19ac41fSArun Ramadoss 				 u16 reg, u8 val)
324b19ac41fSArun Ramadoss {
325b19ac41fSArun Ramadoss 	u16 data16;
326b19ac41fSArun Ramadoss 
327b19ac41fSArun Ramadoss 	ksz_pread16(dev, port, reg, &data16);
328b19ac41fSArun Ramadoss 
329b19ac41fSArun Ramadoss 	/* Update tune Adjust */
330b19ac41fSArun Ramadoss 	data16 |= FIELD_PREP(PORT_TUNE_ADJ, val);
331b19ac41fSArun Ramadoss 	ksz_pwrite16(dev, port, reg, data16);
332b19ac41fSArun Ramadoss 
333b19ac41fSArun Ramadoss 	/* write DLL reset to take effect */
334b19ac41fSArun Ramadoss 	data16 |= PORT_DLL_RESET;
335b19ac41fSArun Ramadoss 	ksz_pwrite16(dev, port, reg, data16);
336b19ac41fSArun Ramadoss }
337b19ac41fSArun Ramadoss 
338b19ac41fSArun Ramadoss static void lan937x_set_rgmii_tx_delay(struct ksz_device *dev, int port)
339b19ac41fSArun Ramadoss {
340b19ac41fSArun Ramadoss 	u8 val;
341b19ac41fSArun Ramadoss 
342b19ac41fSArun Ramadoss 	/* Apply different codes based on the ports as per characterization
343b19ac41fSArun Ramadoss 	 * results
344b19ac41fSArun Ramadoss 	 */
345b19ac41fSArun Ramadoss 	val = (port == LAN937X_RGMII_1_PORT) ? RGMII_1_TX_DELAY_2NS :
346b19ac41fSArun Ramadoss 		RGMII_2_TX_DELAY_2NS;
347b19ac41fSArun Ramadoss 
348b19ac41fSArun Ramadoss 	lan937x_set_tune_adj(dev, port, REG_PORT_XMII_CTRL_5, val);
349b19ac41fSArun Ramadoss }
350b19ac41fSArun Ramadoss 
351b19ac41fSArun Ramadoss static void lan937x_set_rgmii_rx_delay(struct ksz_device *dev, int port)
352b19ac41fSArun Ramadoss {
353b19ac41fSArun Ramadoss 	u8 val;
354b19ac41fSArun Ramadoss 
355b19ac41fSArun Ramadoss 	val = (port == LAN937X_RGMII_1_PORT) ? RGMII_1_RX_DELAY_2NS :
356b19ac41fSArun Ramadoss 		RGMII_2_RX_DELAY_2NS;
357b19ac41fSArun Ramadoss 
358b19ac41fSArun Ramadoss 	lan937x_set_tune_adj(dev, port, REG_PORT_XMII_CTRL_4, val);
359b19ac41fSArun Ramadoss }
360b19ac41fSArun Ramadoss 
361c14e878dSArun Ramadoss void lan937x_phylink_get_caps(struct ksz_device *dev, int port,
362c14e878dSArun Ramadoss 			      struct phylink_config *config)
363c14e878dSArun Ramadoss {
364c14e878dSArun Ramadoss 	config->mac_capabilities = MAC_100FD;
365c14e878dSArun Ramadoss 
366c14e878dSArun Ramadoss 	if (dev->info->supports_rgmii[port]) {
367c14e878dSArun Ramadoss 		/* MII/RMII/RGMII ports */
368c14e878dSArun Ramadoss 		config->mac_capabilities |= MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
369c14e878dSArun Ramadoss 					    MAC_100HD | MAC_10 | MAC_1000FD;
370c14e878dSArun Ramadoss 	}
371c14e878dSArun Ramadoss }
372c14e878dSArun Ramadoss 
373b19ac41fSArun Ramadoss void lan937x_setup_rgmii_delay(struct ksz_device *dev, int port)
374b19ac41fSArun Ramadoss {
375b19ac41fSArun Ramadoss 	struct ksz_port *p = &dev->ports[port];
376b19ac41fSArun Ramadoss 
377b19ac41fSArun Ramadoss 	if (p->rgmii_tx_val) {
378b19ac41fSArun Ramadoss 		lan937x_set_rgmii_tx_delay(dev, port);
379b19ac41fSArun Ramadoss 		dev_info(dev->dev, "Applied rgmii tx delay for the port %d\n",
380b19ac41fSArun Ramadoss 			 port);
381b19ac41fSArun Ramadoss 	}
382b19ac41fSArun Ramadoss 
383b19ac41fSArun Ramadoss 	if (p->rgmii_rx_val) {
384b19ac41fSArun Ramadoss 		lan937x_set_rgmii_rx_delay(dev, port);
385b19ac41fSArun Ramadoss 		dev_info(dev->dev, "Applied rgmii rx delay for the port %d\n",
386b19ac41fSArun Ramadoss 			 port);
387b19ac41fSArun Ramadoss 	}
388b19ac41fSArun Ramadoss }
389b19ac41fSArun Ramadoss 
39055ab6ffaSArun Ramadoss int lan937x_setup(struct dsa_switch *ds)
39155ab6ffaSArun Ramadoss {
39255ab6ffaSArun Ramadoss 	struct ksz_device *dev = ds->priv;
393ffaf1de2SArun Ramadoss 	int ret;
394ffaf1de2SArun Ramadoss 
395ffaf1de2SArun Ramadoss 	/* enable Indirect Access from SPI to the VPHY registers */
396ffaf1de2SArun Ramadoss 	ret = lan937x_enable_spi_indirect_access(dev);
397ffaf1de2SArun Ramadoss 	if (ret < 0) {
398ffaf1de2SArun Ramadoss 		dev_err(dev->dev, "failed to enable spi indirect access");
399ffaf1de2SArun Ramadoss 		return ret;
400ffaf1de2SArun Ramadoss 	}
40155ab6ffaSArun Ramadoss 
402a50b3536SArun Ramadoss 	ret = lan937x_mdio_register(dev);
403a50b3536SArun Ramadoss 	if (ret < 0) {
404a50b3536SArun Ramadoss 		dev_err(dev->dev, "failed to register the mdio");
405a50b3536SArun Ramadoss 		return ret;
406a50b3536SArun Ramadoss 	}
407a50b3536SArun Ramadoss 
40855ab6ffaSArun Ramadoss 	/* The VLAN aware is a global setting. Mixed vlan
40955ab6ffaSArun Ramadoss 	 * filterings are not supported.
41055ab6ffaSArun Ramadoss 	 */
41155ab6ffaSArun Ramadoss 	ds->vlan_filtering_is_global = true;
41255ab6ffaSArun Ramadoss 
41355ab6ffaSArun Ramadoss 	/* Enable aggressive back off for half duplex & UNH mode */
41455ab6ffaSArun Ramadoss 	lan937x_cfg(dev, REG_SW_MAC_CTRL_0,
41555ab6ffaSArun Ramadoss 		    (SW_PAUSE_UNH_MODE | SW_NEW_BACKOFF | SW_AGGR_BACKOFF),
41655ab6ffaSArun Ramadoss 		    true);
41755ab6ffaSArun Ramadoss 
41855ab6ffaSArun Ramadoss 	/* If NO_EXC_COLLISION_DROP bit is set, the switch will not drop
41955ab6ffaSArun Ramadoss 	 * packets when 16 or more collisions occur
42055ab6ffaSArun Ramadoss 	 */
42155ab6ffaSArun Ramadoss 	lan937x_cfg(dev, REG_SW_MAC_CTRL_1, NO_EXC_COLLISION_DROP, true);
42255ab6ffaSArun Ramadoss 
42355ab6ffaSArun Ramadoss 	/* enable global MIB counter freeze function */
42455ab6ffaSArun Ramadoss 	lan937x_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
42555ab6ffaSArun Ramadoss 
42655ab6ffaSArun Ramadoss 	/* disable CLK125 & CLK25, 1: disable, 0: enable */
42755ab6ffaSArun Ramadoss 	lan937x_cfg(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1,
42855ab6ffaSArun Ramadoss 		    (SW_CLK125_ENB | SW_CLK25_ENB), true);
42955ab6ffaSArun Ramadoss 
43055ab6ffaSArun Ramadoss 	return 0;
43155ab6ffaSArun Ramadoss }
43255ab6ffaSArun Ramadoss 
43355ab6ffaSArun Ramadoss int lan937x_switch_init(struct ksz_device *dev)
43455ab6ffaSArun Ramadoss {
43555ab6ffaSArun Ramadoss 	dev->port_mask = (1 << dev->info->port_cnt) - 1;
43655ab6ffaSArun Ramadoss 
43755ab6ffaSArun Ramadoss 	return 0;
43855ab6ffaSArun Ramadoss }
43955ab6ffaSArun Ramadoss 
44055ab6ffaSArun Ramadoss void lan937x_switch_exit(struct ksz_device *dev)
44155ab6ffaSArun Ramadoss {
44255ab6ffaSArun Ramadoss 	lan937x_reset_switch(dev);
44355ab6ffaSArun Ramadoss }
44455ab6ffaSArun Ramadoss 
44555ab6ffaSArun Ramadoss MODULE_AUTHOR("Arun Ramadoss <arun.ramadoss@microchip.com>");
44655ab6ffaSArun Ramadoss MODULE_DESCRIPTION("Microchip LAN937x Series Switch DSA Driver");
44755ab6ffaSArun Ramadoss MODULE_LICENSE("GPL");
448