xref: /linux/drivers/net/dsa/microchip/lan937x_main.c (revision 8560664fd32aa055b3c128337356e32e8fc25b5a)
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 
131ffaf1de2SArun Ramadoss void lan937x_r_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 *data)
132ffaf1de2SArun Ramadoss {
133ffaf1de2SArun Ramadoss 	lan937x_internal_phy_read(dev, addr, reg, data);
134ffaf1de2SArun Ramadoss }
135ffaf1de2SArun Ramadoss 
136ffaf1de2SArun Ramadoss void lan937x_w_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 val)
137ffaf1de2SArun Ramadoss {
138ffaf1de2SArun Ramadoss 	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 
22855ab6ffaSArun Ramadoss 	ret = ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0xFF);
22955ab6ffaSArun Ramadoss 	if (ret < 0)
23055ab6ffaSArun Ramadoss 		return ret;
23155ab6ffaSArun Ramadoss 
23255ab6ffaSArun Ramadoss 	return ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
23355ab6ffaSArun Ramadoss }
23455ab6ffaSArun Ramadoss 
23555ab6ffaSArun Ramadoss void lan937x_port_setup(struct ksz_device *dev, int port, bool cpu_port)
23655ab6ffaSArun Ramadoss {
237*8560664fSArun Ramadoss 	const u32 *masks = dev->info->masks;
238*8560664fSArun Ramadoss 	const u16 *regs = dev->info->regs;
23955ab6ffaSArun Ramadoss 	struct dsa_switch *ds = dev->ds;
24055ab6ffaSArun Ramadoss 	u8 member;
24155ab6ffaSArun Ramadoss 
24255ab6ffaSArun Ramadoss 	/* enable tag tail for host port */
24355ab6ffaSArun Ramadoss 	if (cpu_port)
24455ab6ffaSArun Ramadoss 		lan937x_port_cfg(dev, port, REG_PORT_CTRL_0,
24555ab6ffaSArun Ramadoss 				 PORT_TAIL_TAG_ENABLE, true);
24655ab6ffaSArun Ramadoss 
24755ab6ffaSArun Ramadoss 	/* disable frame check length field */
24855ab6ffaSArun Ramadoss 	lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_0, PORT_CHECK_LENGTH,
24955ab6ffaSArun Ramadoss 			 false);
25055ab6ffaSArun Ramadoss 
25155ab6ffaSArun Ramadoss 	/* set back pressure for half duplex */
25255ab6ffaSArun Ramadoss 	lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE,
25355ab6ffaSArun Ramadoss 			 true);
25455ab6ffaSArun Ramadoss 
25555ab6ffaSArun Ramadoss 	/* enable 802.1p priority */
25655ab6ffaSArun Ramadoss 	lan937x_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true);
25755ab6ffaSArun Ramadoss 
25855ab6ffaSArun Ramadoss 	if (!dev->info->internal_phy[port])
259*8560664fSArun Ramadoss 		lan937x_port_cfg(dev, port, regs[P_XMII_CTRL_0],
260*8560664fSArun Ramadoss 				 masks[P_MII_TX_FLOW_CTRL] |
261*8560664fSArun Ramadoss 				 masks[P_MII_RX_FLOW_CTRL],
26255ab6ffaSArun Ramadoss 				 true);
26355ab6ffaSArun Ramadoss 
26455ab6ffaSArun Ramadoss 	if (cpu_port)
26555ab6ffaSArun Ramadoss 		member = dsa_user_ports(ds);
26655ab6ffaSArun Ramadoss 	else
26755ab6ffaSArun Ramadoss 		member = BIT(dsa_upstream_port(ds, port));
26855ab6ffaSArun Ramadoss 
26955ab6ffaSArun Ramadoss 	dev->dev_ops->cfg_port_member(dev, port, member);
27055ab6ffaSArun Ramadoss }
27155ab6ffaSArun Ramadoss 
27255ab6ffaSArun Ramadoss void lan937x_config_cpu_port(struct dsa_switch *ds)
27355ab6ffaSArun Ramadoss {
27455ab6ffaSArun Ramadoss 	struct ksz_device *dev = ds->priv;
27555ab6ffaSArun Ramadoss 	struct dsa_port *dp;
27655ab6ffaSArun Ramadoss 
27755ab6ffaSArun Ramadoss 	dsa_switch_for_each_cpu_port(dp, ds) {
27855ab6ffaSArun Ramadoss 		if (dev->info->cpu_ports & (1 << dp->index)) {
27955ab6ffaSArun Ramadoss 			dev->cpu_port = dp->index;
28055ab6ffaSArun Ramadoss 
28155ab6ffaSArun Ramadoss 			/* enable cpu port */
28255ab6ffaSArun Ramadoss 			lan937x_port_setup(dev, dp->index, true);
28355ab6ffaSArun Ramadoss 		}
28455ab6ffaSArun Ramadoss 	}
28555ab6ffaSArun Ramadoss 
28655ab6ffaSArun Ramadoss 	dsa_switch_for_each_user_port(dp, ds) {
28755ab6ffaSArun Ramadoss 		ksz_port_stp_state_set(ds, dp->index, BR_STATE_DISABLED);
28855ab6ffaSArun Ramadoss 	}
28955ab6ffaSArun Ramadoss }
29055ab6ffaSArun Ramadoss 
291ab882368SArun Ramadoss int lan937x_change_mtu(struct ksz_device *dev, int port, int new_mtu)
292ab882368SArun Ramadoss {
293ab882368SArun Ramadoss 	struct dsa_switch *ds = dev->ds;
294ab882368SArun Ramadoss 	int ret;
295ab882368SArun Ramadoss 
296ab882368SArun Ramadoss 	new_mtu += VLAN_ETH_HLEN + ETH_FCS_LEN;
297ab882368SArun Ramadoss 
298ab882368SArun Ramadoss 	if (dsa_is_cpu_port(ds, port))
299ab882368SArun Ramadoss 		new_mtu += LAN937X_TAG_LEN;
300ab882368SArun Ramadoss 
301ab882368SArun Ramadoss 	if (new_mtu >= FR_MIN_SIZE)
302ab882368SArun Ramadoss 		ret = lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_0,
303ab882368SArun Ramadoss 				       PORT_JUMBO_PACKET, true);
304ab882368SArun Ramadoss 	else
305ab882368SArun Ramadoss 		ret = lan937x_port_cfg(dev, port, REG_PORT_MAC_CTRL_0,
306ab882368SArun Ramadoss 				       PORT_JUMBO_PACKET, false);
307ab882368SArun Ramadoss 	if (ret < 0) {
308ab882368SArun Ramadoss 		dev_err(ds->dev, "failed to enable jumbo\n");
309ab882368SArun Ramadoss 		return ret;
310ab882368SArun Ramadoss 	}
311ab882368SArun Ramadoss 
312ab882368SArun Ramadoss 	/* Write the frame size in PORT_MAX_FR_SIZE register */
313ab882368SArun Ramadoss 	ksz_pwrite16(dev, port, PORT_MAX_FR_SIZE, new_mtu);
314ab882368SArun Ramadoss 
315ab882368SArun Ramadoss 	return 0;
316ab882368SArun Ramadoss }
317ab882368SArun Ramadoss 
318a0cb1aa4SArun Ramadoss static void lan937x_mac_config(struct ksz_device *dev, int port,
319a0cb1aa4SArun Ramadoss 			       phy_interface_t interface)
320a0cb1aa4SArun Ramadoss {
321a0cb1aa4SArun Ramadoss 	u8 data8;
322a0cb1aa4SArun Ramadoss 
323a0cb1aa4SArun Ramadoss 	ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
324a0cb1aa4SArun Ramadoss 
325a0cb1aa4SArun Ramadoss 	/* clear MII selection & set it based on interface later */
326a0cb1aa4SArun Ramadoss 	data8 &= ~PORT_MII_SEL_M;
327a0cb1aa4SArun Ramadoss 
328a0cb1aa4SArun Ramadoss 	/* configure MAC based on interface */
329a0cb1aa4SArun Ramadoss 	switch (interface) {
330a0cb1aa4SArun Ramadoss 	case PHY_INTERFACE_MODE_MII:
33146f80fa8SArun Ramadoss 		ksz_set_gbit(dev, port, false);
332a0cb1aa4SArun Ramadoss 		data8 |= PORT_MII_SEL;
333a0cb1aa4SArun Ramadoss 		break;
334a0cb1aa4SArun Ramadoss 	case PHY_INTERFACE_MODE_RMII:
33546f80fa8SArun Ramadoss 		ksz_set_gbit(dev, port, false);
336a0cb1aa4SArun Ramadoss 		data8 |= PORT_RMII_SEL;
337a0cb1aa4SArun Ramadoss 		break;
338a0cb1aa4SArun Ramadoss 	default:
339a0cb1aa4SArun Ramadoss 		dev_err(dev->dev, "Unsupported interface '%s' for port %d\n",
340a0cb1aa4SArun Ramadoss 			phy_modes(interface), port);
341a0cb1aa4SArun Ramadoss 		return;
342a0cb1aa4SArun Ramadoss 	}
343a0cb1aa4SArun Ramadoss 
344a0cb1aa4SArun Ramadoss 	/* Write the updated value */
345a0cb1aa4SArun Ramadoss 	ksz_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8);
346a0cb1aa4SArun Ramadoss }
347a0cb1aa4SArun Ramadoss 
348f597d3adSArun Ramadoss static void lan937x_config_interface(struct ksz_device *dev, int port,
349f597d3adSArun Ramadoss 				     int speed, int duplex,
350f597d3adSArun Ramadoss 				     bool tx_pause, bool rx_pause)
351f597d3adSArun Ramadoss {
352aa5b8b73SArun Ramadoss 	ksz_port_set_xmii_speed(dev, port, speed);
353f597d3adSArun Ramadoss 
354*8560664fSArun Ramadoss 	ksz_duplex_flowctrl(dev, port, duplex, tx_pause, rx_pause);
355f597d3adSArun Ramadoss }
356f597d3adSArun Ramadoss 
357c14e878dSArun Ramadoss void lan937x_phylink_get_caps(struct ksz_device *dev, int port,
358c14e878dSArun Ramadoss 			      struct phylink_config *config)
359c14e878dSArun Ramadoss {
360c14e878dSArun Ramadoss 	config->mac_capabilities = MAC_100FD;
361c14e878dSArun Ramadoss 
362c14e878dSArun Ramadoss 	if (dev->info->supports_rgmii[port]) {
363c14e878dSArun Ramadoss 		/* MII/RMII/RGMII ports */
364c14e878dSArun Ramadoss 		config->mac_capabilities |= MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
365c14e878dSArun Ramadoss 					    MAC_100HD | MAC_10 | MAC_1000FD;
366c14e878dSArun Ramadoss 	}
367c14e878dSArun Ramadoss }
368c14e878dSArun Ramadoss 
369f597d3adSArun Ramadoss void lan937x_phylink_mac_link_up(struct ksz_device *dev, int port,
370f597d3adSArun Ramadoss 				 unsigned int mode, phy_interface_t interface,
371f597d3adSArun Ramadoss 				 struct phy_device *phydev, int speed,
372f597d3adSArun Ramadoss 				 int duplex, bool tx_pause, bool rx_pause)
373f597d3adSArun Ramadoss {
374f597d3adSArun Ramadoss 	/* Internal PHYs */
375f597d3adSArun Ramadoss 	if (dev->info->internal_phy[port])
376f597d3adSArun Ramadoss 		return;
377f597d3adSArun Ramadoss 
378f597d3adSArun Ramadoss 	lan937x_config_interface(dev, port, speed, duplex,
379f597d3adSArun Ramadoss 				 tx_pause, rx_pause);
380f597d3adSArun Ramadoss }
381f597d3adSArun Ramadoss 
382a0cb1aa4SArun Ramadoss void lan937x_phylink_mac_config(struct ksz_device *dev, int port,
383a0cb1aa4SArun Ramadoss 				unsigned int mode,
384a0cb1aa4SArun Ramadoss 				const struct phylink_link_state *state)
385a0cb1aa4SArun Ramadoss {
386a0cb1aa4SArun Ramadoss 	/* Internal PHYs */
387a0cb1aa4SArun Ramadoss 	if (dev->info->internal_phy[port])
388a0cb1aa4SArun Ramadoss 		return;
389a0cb1aa4SArun Ramadoss 
390a0cb1aa4SArun Ramadoss 	if (phylink_autoneg_inband(mode)) {
391a0cb1aa4SArun Ramadoss 		dev_err(dev->dev, "In-band AN not supported!\n");
392a0cb1aa4SArun Ramadoss 		return;
393a0cb1aa4SArun Ramadoss 	}
394a0cb1aa4SArun Ramadoss 
395a0cb1aa4SArun Ramadoss 	lan937x_mac_config(dev, port, state->interface);
396a0cb1aa4SArun Ramadoss }
397a0cb1aa4SArun Ramadoss 
39855ab6ffaSArun Ramadoss int lan937x_setup(struct dsa_switch *ds)
39955ab6ffaSArun Ramadoss {
40055ab6ffaSArun Ramadoss 	struct ksz_device *dev = ds->priv;
401ffaf1de2SArun Ramadoss 	int ret;
402ffaf1de2SArun Ramadoss 
403ffaf1de2SArun Ramadoss 	/* enable Indirect Access from SPI to the VPHY registers */
404ffaf1de2SArun Ramadoss 	ret = lan937x_enable_spi_indirect_access(dev);
405ffaf1de2SArun Ramadoss 	if (ret < 0) {
406ffaf1de2SArun Ramadoss 		dev_err(dev->dev, "failed to enable spi indirect access");
407ffaf1de2SArun Ramadoss 		return ret;
408ffaf1de2SArun Ramadoss 	}
40955ab6ffaSArun Ramadoss 
410a50b3536SArun Ramadoss 	ret = lan937x_mdio_register(dev);
411a50b3536SArun Ramadoss 	if (ret < 0) {
412a50b3536SArun Ramadoss 		dev_err(dev->dev, "failed to register the mdio");
413a50b3536SArun Ramadoss 		return ret;
414a50b3536SArun Ramadoss 	}
415a50b3536SArun Ramadoss 
41655ab6ffaSArun Ramadoss 	/* The VLAN aware is a global setting. Mixed vlan
41755ab6ffaSArun Ramadoss 	 * filterings are not supported.
41855ab6ffaSArun Ramadoss 	 */
41955ab6ffaSArun Ramadoss 	ds->vlan_filtering_is_global = true;
42055ab6ffaSArun Ramadoss 
42155ab6ffaSArun Ramadoss 	/* Enable aggressive back off for half duplex & UNH mode */
42255ab6ffaSArun Ramadoss 	lan937x_cfg(dev, REG_SW_MAC_CTRL_0,
42355ab6ffaSArun Ramadoss 		    (SW_PAUSE_UNH_MODE | SW_NEW_BACKOFF | SW_AGGR_BACKOFF),
42455ab6ffaSArun Ramadoss 		    true);
42555ab6ffaSArun Ramadoss 
42655ab6ffaSArun Ramadoss 	/* If NO_EXC_COLLISION_DROP bit is set, the switch will not drop
42755ab6ffaSArun Ramadoss 	 * packets when 16 or more collisions occur
42855ab6ffaSArun Ramadoss 	 */
42955ab6ffaSArun Ramadoss 	lan937x_cfg(dev, REG_SW_MAC_CTRL_1, NO_EXC_COLLISION_DROP, true);
43055ab6ffaSArun Ramadoss 
43155ab6ffaSArun Ramadoss 	/* enable global MIB counter freeze function */
43255ab6ffaSArun Ramadoss 	lan937x_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
43355ab6ffaSArun Ramadoss 
43455ab6ffaSArun Ramadoss 	/* disable CLK125 & CLK25, 1: disable, 0: enable */
43555ab6ffaSArun Ramadoss 	lan937x_cfg(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1,
43655ab6ffaSArun Ramadoss 		    (SW_CLK125_ENB | SW_CLK25_ENB), true);
43755ab6ffaSArun Ramadoss 
43855ab6ffaSArun Ramadoss 	return 0;
43955ab6ffaSArun Ramadoss }
44055ab6ffaSArun Ramadoss 
44155ab6ffaSArun Ramadoss int lan937x_switch_init(struct ksz_device *dev)
44255ab6ffaSArun Ramadoss {
44355ab6ffaSArun Ramadoss 	dev->port_mask = (1 << dev->info->port_cnt) - 1;
44455ab6ffaSArun Ramadoss 
44555ab6ffaSArun Ramadoss 	return 0;
44655ab6ffaSArun Ramadoss }
44755ab6ffaSArun Ramadoss 
44855ab6ffaSArun Ramadoss void lan937x_switch_exit(struct ksz_device *dev)
44955ab6ffaSArun Ramadoss {
45055ab6ffaSArun Ramadoss 	lan937x_reset_switch(dev);
45155ab6ffaSArun Ramadoss }
45255ab6ffaSArun Ramadoss 
45355ab6ffaSArun Ramadoss MODULE_AUTHOR("Arun Ramadoss <arun.ramadoss@microchip.com>");
45455ab6ffaSArun Ramadoss MODULE_DESCRIPTION("Microchip LAN937x Series Switch DSA Driver");
45555ab6ffaSArun Ramadoss MODULE_LICENSE("GPL");
456