xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 747e5eb31d59d047972a0dab03e5430fe4264332)
18aa9ebccSVladimir Oltean // SPDX-License-Identifier: GPL-2.0
28aa9ebccSVladimir Oltean /* Copyright (c) 2018, Sensor-Technik Wiedemann GmbH
38aa9ebccSVladimir Oltean  * Copyright (c) 2018-2019, Vladimir Oltean <olteanv@gmail.com>
48aa9ebccSVladimir Oltean  */
58aa9ebccSVladimir Oltean 
68aa9ebccSVladimir Oltean #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
78aa9ebccSVladimir Oltean 
88aa9ebccSVladimir Oltean #include <linux/delay.h>
98aa9ebccSVladimir Oltean #include <linux/module.h>
108aa9ebccSVladimir Oltean #include <linux/printk.h>
118aa9ebccSVladimir Oltean #include <linux/spi/spi.h>
128aa9ebccSVladimir Oltean #include <linux/errno.h>
138aa9ebccSVladimir Oltean #include <linux/gpio/consumer.h>
14ad9f299aSVladimir Oltean #include <linux/phylink.h>
158aa9ebccSVladimir Oltean #include <linux/of.h>
168aa9ebccSVladimir Oltean #include <linux/of_net.h>
178aa9ebccSVladimir Oltean #include <linux/of_mdio.h>
188aa9ebccSVladimir Oltean #include <linux/of_device.h>
198aa9ebccSVladimir Oltean #include <linux/netdev_features.h>
208aa9ebccSVladimir Oltean #include <linux/netdevice.h>
218aa9ebccSVladimir Oltean #include <linux/if_bridge.h>
228aa9ebccSVladimir Oltean #include <linux/if_ether.h>
23227d07a0SVladimir Oltean #include <linux/dsa/8021q.h>
248aa9ebccSVladimir Oltean #include "sja1105.h"
25ffe10e67SVladimir Oltean #include "sja1105_sgmii.h"
26317ab5b8SVladimir Oltean #include "sja1105_tas.h"
278aa9ebccSVladimir Oltean 
288aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
298aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
308aa9ebccSVladimir Oltean {
318aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
328aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
338aa9ebccSVladimir Oltean 	msleep(pulse_len);
348aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
358aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
368aa9ebccSVladimir Oltean 	msleep(startup_delay);
378aa9ebccSVladimir Oltean }
388aa9ebccSVladimir Oltean 
398aa9ebccSVladimir Oltean static void
408aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
418aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
428aa9ebccSVladimir Oltean {
438aa9ebccSVladimir Oltean 	if (allow) {
448aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
458aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
468aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
478aa9ebccSVladimir Oltean 	} else {
488aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
498aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
508aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
518aa9ebccSVladimir Oltean 	}
528aa9ebccSVladimir Oltean }
538aa9ebccSVladimir Oltean 
548aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
558aa9ebccSVladimir Oltean  * settings into sja1105_setup
568aa9ebccSVladimir Oltean  */
578aa9ebccSVladimir Oltean struct sja1105_dt_port {
588aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
598aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
608aa9ebccSVladimir Oltean };
618aa9ebccSVladimir Oltean 
628aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
638aa9ebccSVladimir Oltean {
648aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
658aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
668aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
678aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
688aa9ebccSVladimir Oltean 		 */
698aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
708aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
718aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
728aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
738aa9ebccSVladimir Oltean 		.ifg = 0,
748aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
751fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
768aa9ebccSVladimir Oltean 		 */
778aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
788aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
798aa9ebccSVladimir Oltean 		.tp_delin = 0,
808aa9ebccSVladimir Oltean 		.tp_delout = 0,
818aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
828aa9ebccSVladimir Oltean 		.maxage = 0xFF,
838aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
848aa9ebccSVladimir Oltean 		.vlanprio = 0,
85e3502b82SVladimir Oltean 		.vlanid = 1,
868aa9ebccSVladimir Oltean 		.ing_mirr = false,
878aa9ebccSVladimir Oltean 		.egr_mirr = false,
888aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
898aa9ebccSVladimir Oltean 		.drpnona664 = false,
908aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
918aa9ebccSVladimir Oltean 		.drpdtag = false,
928aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
938aa9ebccSVladimir Oltean 		.drpuntag = false,
948aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
958aa9ebccSVladimir Oltean 		.retag = false,
96640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
97640f763fSVladimir Oltean 		 * STP will enable it.
98640f763fSVladimir Oltean 		 */
99640f763fSVladimir Oltean 		.dyn_learn = false,
1008aa9ebccSVladimir Oltean 		.egress = false,
1018aa9ebccSVladimir Oltean 		.ingress = false,
1028aa9ebccSVladimir Oltean 	};
1038aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1048aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1058aa9ebccSVladimir Oltean 	int i;
1068aa9ebccSVladimir Oltean 
1078aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1088aa9ebccSVladimir Oltean 
1098aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1108aa9ebccSVladimir Oltean 	if (table->entry_count) {
1118aa9ebccSVladimir Oltean 		kfree(table->entries);
1128aa9ebccSVladimir Oltean 		table->entry_count = 0;
1138aa9ebccSVladimir Oltean 	}
1148aa9ebccSVladimir Oltean 
1158aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1168aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1178aa9ebccSVladimir Oltean 	if (!table->entries)
1188aa9ebccSVladimir Oltean 		return -ENOMEM;
1198aa9ebccSVladimir Oltean 
1208aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1218aa9ebccSVladimir Oltean 
1228aa9ebccSVladimir Oltean 	mac = table->entries;
1238aa9ebccSVladimir Oltean 
124640f763fSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1258aa9ebccSVladimir Oltean 		mac[i] = default_mac;
126640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
127640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
128640f763fSVladimir Oltean 			 * set the I/O parameters statically.
129640f763fSVladimir Oltean 			 */
130640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
131640f763fSVladimir Oltean 			mac[i].ingress = true;
132640f763fSVladimir Oltean 			mac[i].egress = true;
133640f763fSVladimir Oltean 		}
134640f763fSVladimir Oltean 	}
1358aa9ebccSVladimir Oltean 
1368aa9ebccSVladimir Oltean 	return 0;
1378aa9ebccSVladimir Oltean }
1388aa9ebccSVladimir Oltean 
139ffe10e67SVladimir Oltean static bool sja1105_supports_sgmii(struct sja1105_private *priv, int port)
140ffe10e67SVladimir Oltean {
141ffe10e67SVladimir Oltean 	if (priv->info->part_no != SJA1105R_PART_NO &&
142ffe10e67SVladimir Oltean 	    priv->info->part_no != SJA1105S_PART_NO)
143ffe10e67SVladimir Oltean 		return false;
144ffe10e67SVladimir Oltean 
145ffe10e67SVladimir Oltean 	if (port != SJA1105_SGMII_PORT)
146ffe10e67SVladimir Oltean 		return false;
147ffe10e67SVladimir Oltean 
148ffe10e67SVladimir Oltean 	if (dsa_is_unused_port(priv->ds, port))
149ffe10e67SVladimir Oltean 		return false;
150ffe10e67SVladimir Oltean 
151ffe10e67SVladimir Oltean 	return true;
152ffe10e67SVladimir Oltean }
153ffe10e67SVladimir Oltean 
1548aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1558aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1568aa9ebccSVladimir Oltean {
1578aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1588aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1598aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1608aa9ebccSVladimir Oltean 	int i;
1618aa9ebccSVladimir Oltean 
1628aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1638aa9ebccSVladimir Oltean 
1648aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1658aa9ebccSVladimir Oltean 	if (table->entry_count) {
1668aa9ebccSVladimir Oltean 		kfree(table->entries);
1678aa9ebccSVladimir Oltean 		table->entry_count = 0;
1688aa9ebccSVladimir Oltean 	}
1698aa9ebccSVladimir Oltean 
1708aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1718aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1728aa9ebccSVladimir Oltean 	if (!table->entries)
1738aa9ebccSVladimir Oltean 		return -ENOMEM;
1748aa9ebccSVladimir Oltean 
1751fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
1768aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1778aa9ebccSVladimir Oltean 
1788aa9ebccSVladimir Oltean 	mii = table->entries;
1798aa9ebccSVladimir Oltean 
1808aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
181ee9d0cb6SVladimir Oltean 		if (dsa_is_unused_port(priv->ds, i))
182ee9d0cb6SVladimir Oltean 			continue;
183ee9d0cb6SVladimir Oltean 
1848aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1858aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1868aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1878aa9ebccSVladimir Oltean 			break;
1888aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1898aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1908aa9ebccSVladimir Oltean 			break;
1918aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1928aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1938aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1948aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1958aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1968aa9ebccSVladimir Oltean 			break;
197ffe10e67SVladimir Oltean 		case PHY_INTERFACE_MODE_SGMII:
198ffe10e67SVladimir Oltean 			if (!sja1105_supports_sgmii(priv, i))
199ffe10e67SVladimir Oltean 				return -EINVAL;
200ffe10e67SVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_SGMII;
201ffe10e67SVladimir Oltean 			break;
2028aa9ebccSVladimir Oltean 		default:
2038aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
2048aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
2058aa9ebccSVladimir Oltean 		}
2068aa9ebccSVladimir Oltean 
207ffe10e67SVladimir Oltean 		/* Even though the SerDes port is able to drive SGMII autoneg
208ffe10e67SVladimir Oltean 		 * like a PHY would, from the perspective of the XMII tables,
209ffe10e67SVladimir Oltean 		 * the SGMII port should always be put in MAC mode.
210ffe10e67SVladimir Oltean 		 */
211ffe10e67SVladimir Oltean 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_SGMII)
212ffe10e67SVladimir Oltean 			mii->phy_mac[i] = XMII_MAC;
213ffe10e67SVladimir Oltean 		else
2148aa9ebccSVladimir Oltean 			mii->phy_mac[i] = ports[i].role;
2158aa9ebccSVladimir Oltean 	}
2168aa9ebccSVladimir Oltean 	return 0;
2178aa9ebccSVladimir Oltean }
2188aa9ebccSVladimir Oltean 
2198aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
2208aa9ebccSVladimir Oltean {
2218aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2228aa9ebccSVladimir Oltean 
2238aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
2248aa9ebccSVladimir Oltean 
225291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
226291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
227291d1e72SVladimir Oltean 	 */
2288aa9ebccSVladimir Oltean 	if (table->entry_count) {
2298aa9ebccSVladimir Oltean 		kfree(table->entries);
2308aa9ebccSVladimir Oltean 		table->entry_count = 0;
2318aa9ebccSVladimir Oltean 	}
2328aa9ebccSVladimir Oltean 	return 0;
2338aa9ebccSVladimir Oltean }
2348aa9ebccSVladimir Oltean 
2358aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2368aa9ebccSVladimir Oltean {
2378aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2386c56e167SVladimir Oltean 	u64 max_fdb_entries = SJA1105_MAX_L2_LOOKUP_COUNT / SJA1105_NUM_PORTS;
2398aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2408456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2418456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2428aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2438aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2441da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2451da73821SVladimir Oltean 		.start_dynspc = 0,
2466c56e167SVladimir Oltean 		.maxaddrp = {max_fdb_entries, max_fdb_entries, max_fdb_entries,
2476c56e167SVladimir Oltean 			     max_fdb_entries, max_fdb_entries, },
2488aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2498aa9ebccSVladimir Oltean 		.poly = 0x97,
2508aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2518aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2528aa9ebccSVladimir Oltean 		 */
2536d7c7d94SVladimir Oltean 		.shared_learn = true,
2548aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2558aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2568aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2578aa9ebccSVladimir Oltean 		 */
2588aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2598aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2608aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2618aa9ebccSVladimir Oltean 		 */
2628aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2631da73821SVladimir Oltean 		/* P/Q/R/S only */
2641da73821SVladimir Oltean 		.use_static = true,
2651da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2661da73821SVladimir Oltean 		 * dynamic FDB entries
2671da73821SVladimir Oltean 		 */
2681da73821SVladimir Oltean 		.owr_dyn = true,
2691da73821SVladimir Oltean 		.drpnolearn = true,
2708aa9ebccSVladimir Oltean 	};
2718aa9ebccSVladimir Oltean 
2728aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2738aa9ebccSVladimir Oltean 
2748aa9ebccSVladimir Oltean 	if (table->entry_count) {
2758aa9ebccSVladimir Oltean 		kfree(table->entries);
2768aa9ebccSVladimir Oltean 		table->entry_count = 0;
2778aa9ebccSVladimir Oltean 	}
2788aa9ebccSVladimir Oltean 
2798aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2808aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2818aa9ebccSVladimir Oltean 	if (!table->entries)
2828aa9ebccSVladimir Oltean 		return -ENOMEM;
2838aa9ebccSVladimir Oltean 
2848aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2858aa9ebccSVladimir Oltean 
2868aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2878aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2888aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2898aa9ebccSVladimir Oltean 
2908aa9ebccSVladimir Oltean 	return 0;
2918aa9ebccSVladimir Oltean }
2928aa9ebccSVladimir Oltean 
2938aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2948aa9ebccSVladimir Oltean {
2958aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2968aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2978aa9ebccSVladimir Oltean 		.ving_mirr = 0,
2988aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
2998aa9ebccSVladimir Oltean 		.vmemb_port = 0,
3008aa9ebccSVladimir Oltean 		.vlan_bc = 0,
3018aa9ebccSVladimir Oltean 		.tag_port = 0,
302e3502b82SVladimir Oltean 		.vlanid = 1,
3038aa9ebccSVladimir Oltean 	};
3048aa9ebccSVladimir Oltean 	int i;
3058aa9ebccSVladimir Oltean 
3068aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
3078aa9ebccSVladimir Oltean 
308e3502b82SVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 1.
3096666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
3106666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
3118aa9ebccSVladimir Oltean 	 */
3128aa9ebccSVladimir Oltean 	if (table->entry_count) {
3138aa9ebccSVladimir Oltean 		kfree(table->entries);
3148aa9ebccSVladimir Oltean 		table->entry_count = 0;
3158aa9ebccSVladimir Oltean 	}
3168aa9ebccSVladimir Oltean 
3178aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
3188aa9ebccSVladimir Oltean 				 GFP_KERNEL);
3198aa9ebccSVladimir Oltean 	if (!table->entries)
3208aa9ebccSVladimir Oltean 		return -ENOMEM;
3218aa9ebccSVladimir Oltean 
3228aa9ebccSVladimir Oltean 	table->entry_count = 1;
3238aa9ebccSVladimir Oltean 
324e3502b82SVladimir Oltean 	/* VLAN 1: all DT-defined ports are members; no restrictions on
3258aa9ebccSVladimir Oltean 	 * forwarding; always transmit priority-tagged frames as untagged.
3268aa9ebccSVladimir Oltean 	 */
3278aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3288aa9ebccSVladimir Oltean 		pvid.vmemb_port |= BIT(i);
3298aa9ebccSVladimir Oltean 		pvid.vlan_bc |= BIT(i);
3308aa9ebccSVladimir Oltean 		pvid.tag_port &= ~BIT(i);
3318aa9ebccSVladimir Oltean 	}
3328aa9ebccSVladimir Oltean 
3338aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3348aa9ebccSVladimir Oltean 	return 0;
3358aa9ebccSVladimir Oltean }
3368aa9ebccSVladimir Oltean 
3378aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
3388aa9ebccSVladimir Oltean {
3398aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
3408aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3418aa9ebccSVladimir Oltean 	int i, j;
3428aa9ebccSVladimir Oltean 
3438aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
3448aa9ebccSVladimir Oltean 
3458aa9ebccSVladimir Oltean 	if (table->entry_count) {
3468aa9ebccSVladimir Oltean 		kfree(table->entries);
3478aa9ebccSVladimir Oltean 		table->entry_count = 0;
3488aa9ebccSVladimir Oltean 	}
3498aa9ebccSVladimir Oltean 
3508aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3518aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3528aa9ebccSVladimir Oltean 	if (!table->entries)
3538aa9ebccSVladimir Oltean 		return -ENOMEM;
3548aa9ebccSVladimir Oltean 
3558aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3568aa9ebccSVladimir Oltean 
3578aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3588aa9ebccSVladimir Oltean 
3598aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3608aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3618aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3628aa9ebccSVladimir Oltean 
3638aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3648aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3658aa9ebccSVladimir Oltean 
3668aa9ebccSVladimir Oltean 		if (i == upstream)
3678aa9ebccSVladimir Oltean 			continue;
3688aa9ebccSVladimir Oltean 
3698aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3708aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3718aa9ebccSVladimir Oltean 	}
3728aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3738aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3748aa9ebccSVladimir Oltean 	 */
3758aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3768aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3778aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
3788aa9ebccSVladimir Oltean 
3798aa9ebccSVladimir Oltean 	return 0;
3808aa9ebccSVladimir Oltean }
3818aa9ebccSVladimir Oltean 
3828aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
3838aa9ebccSVladimir Oltean {
3848aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
3858aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
3868aa9ebccSVladimir Oltean 		.max_dynp = 0,
3878aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
3888aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
3898aa9ebccSVladimir Oltean 	};
3908aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3918aa9ebccSVladimir Oltean 
3928aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
3938aa9ebccSVladimir Oltean 
3948aa9ebccSVladimir Oltean 	if (table->entry_count) {
3958aa9ebccSVladimir Oltean 		kfree(table->entries);
3968aa9ebccSVladimir Oltean 		table->entry_count = 0;
3978aa9ebccSVladimir Oltean 	}
3988aa9ebccSVladimir Oltean 
3998aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
4008aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4018aa9ebccSVladimir Oltean 	if (!table->entries)
4028aa9ebccSVladimir Oltean 		return -ENOMEM;
4038aa9ebccSVladimir Oltean 
4048aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
4058aa9ebccSVladimir Oltean 
4068aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4078aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
4088aa9ebccSVladimir Oltean 				default_l2fwd_params;
4098aa9ebccSVladimir Oltean 
4108aa9ebccSVladimir Oltean 	return 0;
4118aa9ebccSVladimir Oltean }
4128aa9ebccSVladimir Oltean 
4138aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
4148aa9ebccSVladimir Oltean {
4158aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
416511e6ca0SVladimir Oltean 		/* Allow dynamic changing of the mirror port */
417511e6ca0SVladimir Oltean 		.mirr_ptacu = true,
4188aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
4195f06c63bSVladimir Oltean 		/* Priority queue for link-local management frames
4205f06c63bSVladimir Oltean 		 * (both ingress to and egress from CPU - PTP, STP etc)
4215f06c63bSVladimir Oltean 		 */
42208fde09aSVladimir Oltean 		.hostprio = 7,
4238aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
4248aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
42542824463SVladimir Oltean 		.incl_srcpt1 = false,
4268aa9ebccSVladimir Oltean 		.send_meta1  = false,
4278aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
4288aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
42942824463SVladimir Oltean 		.incl_srcpt0 = false,
4308aa9ebccSVladimir Oltean 		.send_meta0  = false,
4318aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
4328aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
4338aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
4348aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
4358aa9ebccSVladimir Oltean 		 */
4368aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
437511e6ca0SVladimir Oltean 		/* Default to an invalid value */
438511e6ca0SVladimir Oltean 		.mirr_port = SJA1105_NUM_PORTS,
4398aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
4408aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
4418aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
4428aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
4438aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
4448aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
4458aa9ebccSVladimir Oltean 		 */
4468aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
4478aa9ebccSVladimir Oltean 		/* No TTEthernet */
4488aa9ebccSVladimir Oltean 		.vllupformat = 0,
4498aa9ebccSVladimir Oltean 		.vlmarker = 0,
4508aa9ebccSVladimir Oltean 		.vlmask = 0,
4518aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
4528aa9ebccSVladimir Oltean 		.ignore2stf = 0,
4536666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
4546666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
4556666cebcSVladimir Oltean 		 */
4566666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
4576666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
4588aa9ebccSVladimir Oltean 	};
4598aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4608aa9ebccSVladimir Oltean 
4618aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
4628aa9ebccSVladimir Oltean 
4638aa9ebccSVladimir Oltean 	if (table->entry_count) {
4648aa9ebccSVladimir Oltean 		kfree(table->entries);
4658aa9ebccSVladimir Oltean 		table->entry_count = 0;
4668aa9ebccSVladimir Oltean 	}
4678aa9ebccSVladimir Oltean 
4688aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
4698aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4708aa9ebccSVladimir Oltean 	if (!table->entries)
4718aa9ebccSVladimir Oltean 		return -ENOMEM;
4728aa9ebccSVladimir Oltean 
4738aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
4748aa9ebccSVladimir Oltean 
4758aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4768aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
4778aa9ebccSVladimir Oltean 				default_general_params;
4788aa9ebccSVladimir Oltean 
4798aa9ebccSVladimir Oltean 	return 0;
4808aa9ebccSVladimir Oltean }
4818aa9ebccSVladimir Oltean 
48279d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
48379d5511cSVladimir Oltean {
48479d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
48579d5511cSVladimir Oltean 	struct sja1105_table *table;
48679d5511cSVladimir Oltean 
48779d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
48879d5511cSVladimir Oltean 
48979d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
49079d5511cSVladimir Oltean 	if (table->entry_count) {
49179d5511cSVladimir Oltean 		kfree(table->entries);
49279d5511cSVladimir Oltean 		table->entry_count = 0;
49379d5511cSVladimir Oltean 	}
49479d5511cSVladimir Oltean 
49579d5511cSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
49679d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
49779d5511cSVladimir Oltean 	if (!table->entries)
49879d5511cSVladimir Oltean 		return -ENOMEM;
49979d5511cSVladimir Oltean 
50079d5511cSVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
50179d5511cSVladimir Oltean 
50279d5511cSVladimir Oltean 	avb = table->entries;
50379d5511cSVladimir Oltean 
50479d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
50579d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
50679d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
507*747e5eb3SVladimir Oltean 	/* On P/Q/R/S, configure the direction of the PTP_CLK pin as input by
508*747e5eb3SVladimir Oltean 	 * default. This is because there might be boards with a hardware
509*747e5eb3SVladimir Oltean 	 * layout where enabling the pin as output might cause an electrical
510*747e5eb3SVladimir Oltean 	 * clash. On E/T the pin is always an output, which the board designers
511*747e5eb3SVladimir Oltean 	 * probably already knew, so even if there are going to be electrical
512*747e5eb3SVladimir Oltean 	 * issues, there's nothing we can do.
513*747e5eb3SVladimir Oltean 	 */
514*747e5eb3SVladimir Oltean 	avb->cas_master = false;
51579d5511cSVladimir Oltean 
51679d5511cSVladimir Oltean 	return 0;
51779d5511cSVladimir Oltean }
51879d5511cSVladimir Oltean 
5198aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
5208aa9ebccSVladimir Oltean 
52109c1b412SVladimir Oltean static void sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
5228aa9ebccSVladimir Oltean 				  int index)
5238aa9ebccSVladimir Oltean {
5248aa9ebccSVladimir Oltean 	policing[index].sharindx = index;
5258aa9ebccSVladimir Oltean 	policing[index].smax = 65535; /* Burst size in bytes */
5268aa9ebccSVladimir Oltean 	policing[index].rate = SJA1105_RATE_MBPS(1000);
5278aa9ebccSVladimir Oltean 	policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
5288aa9ebccSVladimir Oltean 	policing[index].partition = 0;
5298aa9ebccSVladimir Oltean }
5308aa9ebccSVladimir Oltean 
5318aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
5328aa9ebccSVladimir Oltean {
5338aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
5348aa9ebccSVladimir Oltean 	struct sja1105_table *table;
5358aa9ebccSVladimir Oltean 	int i, j, k;
5368aa9ebccSVladimir Oltean 
5378aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
5388aa9ebccSVladimir Oltean 
5398aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
5408aa9ebccSVladimir Oltean 	if (table->entry_count) {
5418aa9ebccSVladimir Oltean 		kfree(table->entries);
5428aa9ebccSVladimir Oltean 		table->entry_count = 0;
5438aa9ebccSVladimir Oltean 	}
5448aa9ebccSVladimir Oltean 
5458aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
5468aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5478aa9ebccSVladimir Oltean 	if (!table->entries)
5488aa9ebccSVladimir Oltean 		return -ENOMEM;
5498aa9ebccSVladimir Oltean 
5508aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
5518aa9ebccSVladimir Oltean 
5528aa9ebccSVladimir Oltean 	policing = table->entries;
5538aa9ebccSVladimir Oltean 
5548aa9ebccSVladimir Oltean 	/* k sweeps through all unicast policers (0-39).
5558aa9ebccSVladimir Oltean 	 * bcast sweeps through policers 40-44.
5568aa9ebccSVladimir Oltean 	 */
5578aa9ebccSVladimir Oltean 	for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) {
5588aa9ebccSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i;
5598aa9ebccSVladimir Oltean 
5608aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++, k++)
5618aa9ebccSVladimir Oltean 			sja1105_setup_policer(policing, k);
5628aa9ebccSVladimir Oltean 
5638aa9ebccSVladimir Oltean 		/* Set up this port's policer for broadcast traffic */
5648aa9ebccSVladimir Oltean 		sja1105_setup_policer(policing, bcast);
5658aa9ebccSVladimir Oltean 	}
5668aa9ebccSVladimir Oltean 	return 0;
5678aa9ebccSVladimir Oltean }
5688aa9ebccSVladimir Oltean 
5698aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
5708aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
5718aa9ebccSVladimir Oltean {
5728aa9ebccSVladimir Oltean 	int rc;
5738aa9ebccSVladimir Oltean 
5748aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
5758aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
5768aa9ebccSVladimir Oltean 					priv->info->static_ops,
5778aa9ebccSVladimir Oltean 					priv->info->device_id);
5788aa9ebccSVladimir Oltean 	if (rc)
5798aa9ebccSVladimir Oltean 		return rc;
5808aa9ebccSVladimir Oltean 
5818aa9ebccSVladimir Oltean 	/* Build static configuration */
5828aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
5838aa9ebccSVladimir Oltean 	if (rc < 0)
5848aa9ebccSVladimir Oltean 		return rc;
5858aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
5868aa9ebccSVladimir Oltean 	if (rc < 0)
5878aa9ebccSVladimir Oltean 		return rc;
5888aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
5898aa9ebccSVladimir Oltean 	if (rc < 0)
5908aa9ebccSVladimir Oltean 		return rc;
5918aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
5928aa9ebccSVladimir Oltean 	if (rc < 0)
5938aa9ebccSVladimir Oltean 		return rc;
5948aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
5958aa9ebccSVladimir Oltean 	if (rc < 0)
5968aa9ebccSVladimir Oltean 		return rc;
5978aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
5988aa9ebccSVladimir Oltean 	if (rc < 0)
5998aa9ebccSVladimir Oltean 		return rc;
6008aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
6018aa9ebccSVladimir Oltean 	if (rc < 0)
6028aa9ebccSVladimir Oltean 		return rc;
6038aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
6048aa9ebccSVladimir Oltean 	if (rc < 0)
6058aa9ebccSVladimir Oltean 		return rc;
6068aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
6078aa9ebccSVladimir Oltean 	if (rc < 0)
6088aa9ebccSVladimir Oltean 		return rc;
60979d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
61079d5511cSVladimir Oltean 	if (rc < 0)
61179d5511cSVladimir Oltean 		return rc;
6128aa9ebccSVladimir Oltean 
6138aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
6148aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
6158aa9ebccSVladimir Oltean }
6168aa9ebccSVladimir Oltean 
617f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
618f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
619f5b8631cSVladimir Oltean {
620f5b8631cSVladimir Oltean 	int i;
621f5b8631cSVladimir Oltean 
622f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
6239bca3a0aSOleksij Rempel 		if (ports[i].role == XMII_MAC)
624f5b8631cSVladimir Oltean 			continue;
625f5b8631cSVladimir Oltean 
6269bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
6279bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
628f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
629f5b8631cSVladimir Oltean 
6309bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
6319bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
632f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
633f5b8631cSVladimir Oltean 
634f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
635f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
636f5b8631cSVladimir Oltean 			return -EINVAL;
637f5b8631cSVladimir Oltean 	}
638f5b8631cSVladimir Oltean 	return 0;
639f5b8631cSVladimir Oltean }
640f5b8631cSVladimir Oltean 
6418aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
6428aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
6438aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
6448aa9ebccSVladimir Oltean {
6458aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6468aa9ebccSVladimir Oltean 	struct device_node *child;
6478aa9ebccSVladimir Oltean 
64827afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
6498aa9ebccSVladimir Oltean 		struct device_node *phy_node;
6500c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
6518aa9ebccSVladimir Oltean 		u32 index;
6520c65b2b9SAndrew Lunn 		int err;
6538aa9ebccSVladimir Oltean 
6548aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
6558aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
6568aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
6578aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
6587ba771e3SNishka Dasgupta 			of_node_put(child);
6598aa9ebccSVladimir Oltean 			return -ENODEV;
6608aa9ebccSVladimir Oltean 		}
6618aa9ebccSVladimir Oltean 
6628aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
6630c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
6640c65b2b9SAndrew Lunn 		if (err) {
6658aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
6668aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
6678aa9ebccSVladimir Oltean 				index);
6687ba771e3SNishka Dasgupta 			of_node_put(child);
6698aa9ebccSVladimir Oltean 			return -ENODEV;
6708aa9ebccSVladimir Oltean 		}
6718aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
6728aa9ebccSVladimir Oltean 
6738aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
6748aa9ebccSVladimir Oltean 		if (!phy_node) {
6758aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
6768aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
6778aa9ebccSVladimir Oltean 					"properties missing!\n");
6787ba771e3SNishka Dasgupta 				of_node_put(child);
6798aa9ebccSVladimir Oltean 				return -ENODEV;
6808aa9ebccSVladimir Oltean 			}
6818aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
6828aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
6838aa9ebccSVladimir Oltean 			 */
6848aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6858aa9ebccSVladimir Oltean 		} else {
6868aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
6878aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6888aa9ebccSVladimir Oltean 			of_node_put(phy_node);
6898aa9ebccSVladimir Oltean 		}
6908aa9ebccSVladimir Oltean 
6918aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
6928aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
6938aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6948aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
6958aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6968aa9ebccSVladimir Oltean 	}
6978aa9ebccSVladimir Oltean 
6988aa9ebccSVladimir Oltean 	return 0;
6998aa9ebccSVladimir Oltean }
7008aa9ebccSVladimir Oltean 
7018aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
7028aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
7038aa9ebccSVladimir Oltean {
7048aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
7058aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
7068aa9ebccSVladimir Oltean 	struct device_node *ports_node;
7078aa9ebccSVladimir Oltean 	int rc;
7088aa9ebccSVladimir Oltean 
7098aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
7108aa9ebccSVladimir Oltean 	if (!ports_node) {
7118aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
7128aa9ebccSVladimir Oltean 		return -ENODEV;
7138aa9ebccSVladimir Oltean 	}
7148aa9ebccSVladimir Oltean 
7158aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
7168aa9ebccSVladimir Oltean 	of_node_put(ports_node);
7178aa9ebccSVladimir Oltean 
7188aa9ebccSVladimir Oltean 	return rc;
7198aa9ebccSVladimir Oltean }
7208aa9ebccSVladimir Oltean 
721ffe10e67SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
722ffe10e67SVladimir Oltean {
723ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
724ffe10e67SVladimir Oltean 	u32 val;
725ffe10e67SVladimir Oltean 	int rc;
726ffe10e67SVladimir Oltean 
727ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
728ffe10e67SVladimir Oltean 			      NULL);
729ffe10e67SVladimir Oltean 	if (rc < 0)
730ffe10e67SVladimir Oltean 		return rc;
731ffe10e67SVladimir Oltean 
732ffe10e67SVladimir Oltean 	return val;
733ffe10e67SVladimir Oltean }
734ffe10e67SVladimir Oltean 
735ffe10e67SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
736ffe10e67SVladimir Oltean 			       u16 pcs_val)
737ffe10e67SVladimir Oltean {
738ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
739ffe10e67SVladimir Oltean 	u32 val = pcs_val;
740ffe10e67SVladimir Oltean 	int rc;
741ffe10e67SVladimir Oltean 
742ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
743ffe10e67SVladimir Oltean 			      NULL);
744ffe10e67SVladimir Oltean 	if (rc < 0)
745ffe10e67SVladimir Oltean 		return rc;
746ffe10e67SVladimir Oltean 
747ffe10e67SVladimir Oltean 	return val;
748ffe10e67SVladimir Oltean }
749ffe10e67SVladimir Oltean 
750ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
751ffe10e67SVladimir Oltean 				     bool an_enabled, bool an_master)
752ffe10e67SVladimir Oltean {
753ffe10e67SVladimir Oltean 	u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
754ffe10e67SVladimir Oltean 
755ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
756ffe10e67SVladimir Oltean 	 * stop the clock during LPI mode, make the MAC reconfigure
757ffe10e67SVladimir Oltean 	 * autonomously after PCS autoneg is done, flush the internal FIFOs.
758ffe10e67SVladimir Oltean 	 */
759ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
760ffe10e67SVladimir Oltean 					       SJA1105_DC1_CLOCK_STOP_EN |
761ffe10e67SVladimir Oltean 					       SJA1105_DC1_MAC_AUTO_SW |
762ffe10e67SVladimir Oltean 					       SJA1105_DC1_INIT);
763ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
764ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
765ffe10e67SVladimir Oltean 	/* AUTONEG_CONTROL: Use SGMII autoneg */
766ffe10e67SVladimir Oltean 	if (an_master)
767ffe10e67SVladimir Oltean 		ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
768ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_AC, ac);
769ffe10e67SVladimir Oltean 	/* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
770ffe10e67SVladimir Oltean 	 * sja1105_sgmii_pcs_force_speed must be called later for the link
771ffe10e67SVladimir Oltean 	 * to become operational.
772ffe10e67SVladimir Oltean 	 */
773ffe10e67SVladimir Oltean 	if (an_enabled)
774ffe10e67SVladimir Oltean 		sja1105_sgmii_write(priv, MII_BMCR,
775ffe10e67SVladimir Oltean 				    BMCR_ANENABLE | BMCR_ANRESTART);
776ffe10e67SVladimir Oltean }
777ffe10e67SVladimir Oltean 
778ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
779ffe10e67SVladimir Oltean 					  int speed)
780ffe10e67SVladimir Oltean {
781ffe10e67SVladimir Oltean 	int pcs_speed;
782ffe10e67SVladimir Oltean 
783ffe10e67SVladimir Oltean 	switch (speed) {
784ffe10e67SVladimir Oltean 	case SPEED_1000:
785ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED1000;
786ffe10e67SVladimir Oltean 		break;
787ffe10e67SVladimir Oltean 	case SPEED_100:
788ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED100;
789ffe10e67SVladimir Oltean 		break;
790ffe10e67SVladimir Oltean 	case SPEED_10:
791ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED10;
792ffe10e67SVladimir Oltean 		break;
793ffe10e67SVladimir Oltean 	default:
794ffe10e67SVladimir Oltean 		dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
795ffe10e67SVladimir Oltean 		return;
796ffe10e67SVladimir Oltean 	}
797ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
798ffe10e67SVladimir Oltean }
799ffe10e67SVladimir Oltean 
800c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
8018aa9ebccSVladimir Oltean static int sja1105_speed[] = {
802c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
803c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
804c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
805c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
8068aa9ebccSVladimir Oltean };
8078aa9ebccSVladimir Oltean 
8088400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
8098aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
8108400cff6SVladimir Oltean 				      int speed_mbps)
8118aa9ebccSVladimir Oltean {
8128aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
8138aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
8148aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
8158aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
8168aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
8178aa9ebccSVladimir Oltean 	int rc;
8188aa9ebccSVladimir Oltean 
8198400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
8208400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
8218400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
8228400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
8238400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
8248400cff6SVladimir Oltean 	 */
8258aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
8268400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
8278aa9ebccSVladimir Oltean 
828f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
829c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
830a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
831a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
832a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
833a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
834a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
835a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
836a979a0abSVladimir Oltean 		 */
837f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
838f4cfcfbdSVladimir Oltean 		break;
839c44d0535SVladimir Oltean 	case SPEED_10:
840f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
841f4cfcfbdSVladimir Oltean 		break;
842c44d0535SVladimir Oltean 	case SPEED_100:
843f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
844f4cfcfbdSVladimir Oltean 		break;
845c44d0535SVladimir Oltean 	case SPEED_1000:
846f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
847f4cfcfbdSVladimir Oltean 		break;
848f4cfcfbdSVladimir Oltean 	default:
8498aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
8508aa9ebccSVladimir Oltean 		return -EINVAL;
8518aa9ebccSVladimir Oltean 	}
8528aa9ebccSVladimir Oltean 
8538400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
8548400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
8558400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
8568400cff6SVladimir Oltean 	 * we want auto during upload phase).
857ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
858ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
8598aa9ebccSVladimir Oltean 	 */
860ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port))
861ffe10e67SVladimir Oltean 		mac[port].speed = SJA1105_SPEED_1000MBPS;
862ffe10e67SVladimir Oltean 	else
8638aa9ebccSVladimir Oltean 		mac[port].speed = speed;
8648aa9ebccSVladimir Oltean 
8658aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
8668400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
8678400cff6SVladimir Oltean 					  &mac[port], true);
8688aa9ebccSVladimir Oltean 	if (rc < 0) {
8698aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
8708aa9ebccSVladimir Oltean 		return rc;
8718aa9ebccSVladimir Oltean 	}
8728aa9ebccSVladimir Oltean 
8738aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
8748aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
8758aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
8768aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
8778aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
8788aa9ebccSVladimir Oltean 	 */
8798aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
8808aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
8818aa9ebccSVladimir Oltean 		return 0;
8828aa9ebccSVladimir Oltean 
8838aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
8848aa9ebccSVladimir Oltean }
8858aa9ebccSVladimir Oltean 
88639710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
88739710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
88839710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
88939710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
89039710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
89139710229SVladimir Oltean  * now.
89239710229SVladimir Oltean  */
89339710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
89439710229SVladimir Oltean 				      phy_interface_t interface)
89539710229SVladimir Oltean {
89639710229SVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
89739710229SVladimir Oltean 	sja1105_phy_interface_t phy_mode;
89839710229SVladimir Oltean 
89939710229SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
90039710229SVladimir Oltean 	phy_mode = mii->xmii_mode[port];
90139710229SVladimir Oltean 
90239710229SVladimir Oltean 	switch (interface) {
90339710229SVladimir Oltean 	case PHY_INTERFACE_MODE_MII:
90439710229SVladimir Oltean 		return (phy_mode != XMII_MODE_MII);
90539710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RMII:
90639710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RMII);
90739710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII:
90839710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_ID:
90939710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_RXID:
91039710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_TXID:
91139710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RGMII);
912ffe10e67SVladimir Oltean 	case PHY_INTERFACE_MODE_SGMII:
913ffe10e67SVladimir Oltean 		return (phy_mode != XMII_MODE_SGMII);
91439710229SVladimir Oltean 	default:
91539710229SVladimir Oltean 		return true;
91639710229SVladimir Oltean 	}
91739710229SVladimir Oltean }
91839710229SVladimir Oltean 
919af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
920ffe10e67SVladimir Oltean 			       unsigned int mode,
921af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
9228aa9ebccSVladimir Oltean {
9238aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
924ffe10e67SVladimir Oltean 	bool is_sgmii = sja1105_supports_sgmii(priv, port);
9258aa9ebccSVladimir Oltean 
926ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
927ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
928ec8582d1SVladimir Oltean 			phy_modes(state->interface));
92939710229SVladimir Oltean 		return;
930ec8582d1SVladimir Oltean 	}
93139710229SVladimir Oltean 
932ffe10e67SVladimir Oltean 	if (phylink_autoneg_inband(mode) && !is_sgmii) {
9339f971573SVladimir Oltean 		dev_err(ds->dev, "In-band AN not supported!\n");
9349f971573SVladimir Oltean 		return;
9359f971573SVladimir Oltean 	}
936ffe10e67SVladimir Oltean 
937ffe10e67SVladimir Oltean 	if (is_sgmii)
938ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
939ffe10e67SVladimir Oltean 					 false);
9408400cff6SVladimir Oltean }
9418400cff6SVladimir Oltean 
9428400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
9438400cff6SVladimir Oltean 				  unsigned int mode,
9448400cff6SVladimir Oltean 				  phy_interface_t interface)
9458400cff6SVladimir Oltean {
9468400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
9478400cff6SVladimir Oltean }
9488400cff6SVladimir Oltean 
9498400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
9508400cff6SVladimir Oltean 				unsigned int mode,
9518400cff6SVladimir Oltean 				phy_interface_t interface,
9525b502a7bSRussell King 				struct phy_device *phydev,
9535b502a7bSRussell King 				int speed, int duplex,
9545b502a7bSRussell King 				bool tx_pause, bool rx_pause)
9558400cff6SVladimir Oltean {
956ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
957ec8582d1SVladimir Oltean 
958ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
959ec8582d1SVladimir Oltean 
960ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
961ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_force_speed(priv, speed);
962ffe10e67SVladimir Oltean 
963ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
9648aa9ebccSVladimir Oltean }
9658aa9ebccSVladimir Oltean 
966ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
967ad9f299aSVladimir Oltean 				     unsigned long *supported,
968ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
969ad9f299aSVladimir Oltean {
970ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
971ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
972ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
973ad9f299aSVladimir Oltean 	 */
974ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
975ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
976ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
977ad9f299aSVladimir Oltean 
978ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
979ad9f299aSVladimir Oltean 
98039710229SVladimir Oltean 	/* include/linux/phylink.h says:
98139710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
98239710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
98339710229SVladimir Oltean 	 */
98439710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
98539710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
98639710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
98739710229SVladimir Oltean 		return;
98839710229SVladimir Oltean 	}
98939710229SVladimir Oltean 
990ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
991ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
992ad9f299aSVladimir Oltean 	 */
993ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
994ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
995ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
996ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
997ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
998ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
999ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
1000ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
1001ad9f299aSVladimir Oltean 
1002ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
1003ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
1004ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1005ad9f299aSVladimir Oltean }
1006ad9f299aSVladimir Oltean 
1007ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1008ffe10e67SVladimir Oltean 				     struct phylink_link_state *state)
1009ffe10e67SVladimir Oltean {
1010ffe10e67SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1011ffe10e67SVladimir Oltean 	int ais;
1012ffe10e67SVladimir Oltean 
1013ffe10e67SVladimir Oltean 	/* Read the vendor-specific AUTONEG_INTR_STATUS register */
1014ffe10e67SVladimir Oltean 	ais = sja1105_sgmii_read(priv, SJA1105_AIS);
1015ffe10e67SVladimir Oltean 	if (ais < 0)
1016ffe10e67SVladimir Oltean 		return ais;
1017ffe10e67SVladimir Oltean 
1018ffe10e67SVladimir Oltean 	switch (SJA1105_AIS_SPEED(ais)) {
1019ffe10e67SVladimir Oltean 	case 0:
1020ffe10e67SVladimir Oltean 		state->speed = SPEED_10;
1021ffe10e67SVladimir Oltean 		break;
1022ffe10e67SVladimir Oltean 	case 1:
1023ffe10e67SVladimir Oltean 		state->speed = SPEED_100;
1024ffe10e67SVladimir Oltean 		break;
1025ffe10e67SVladimir Oltean 	case 2:
1026ffe10e67SVladimir Oltean 		state->speed = SPEED_1000;
1027ffe10e67SVladimir Oltean 		break;
1028ffe10e67SVladimir Oltean 	default:
1029ffe10e67SVladimir Oltean 		dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1030ffe10e67SVladimir Oltean 			SJA1105_AIS_SPEED(ais));
1031ffe10e67SVladimir Oltean 	}
1032ffe10e67SVladimir Oltean 	state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1033ffe10e67SVladimir Oltean 	state->an_complete = SJA1105_AIS_COMPLETE(ais);
1034ffe10e67SVladimir Oltean 	state->link = SJA1105_AIS_LINK_STATUS(ais);
1035ffe10e67SVladimir Oltean 
1036ffe10e67SVladimir Oltean 	return 0;
1037ffe10e67SVladimir Oltean }
1038ffe10e67SVladimir Oltean 
103960f6053fSVladimir Oltean static int
104060f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
104160f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
104260f6053fSVladimir Oltean {
104360f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
104460f6053fSVladimir Oltean 	struct sja1105_table *table;
104560f6053fSVladimir Oltean 	int i;
104660f6053fSVladimir Oltean 
104760f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
104860f6053fSVladimir Oltean 	l2_lookup = table->entries;
104960f6053fSVladimir Oltean 
105060f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
105160f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
105260f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
105360f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
105460f6053fSVladimir Oltean 			return i;
105560f6053fSVladimir Oltean 
105660f6053fSVladimir Oltean 	return -1;
105760f6053fSVladimir Oltean }
105860f6053fSVladimir Oltean 
105960f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
106060f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
106160f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
106260f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
106360f6053fSVladimir Oltean  */
106460f6053fSVladimir Oltean static int
106560f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
106660f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
106760f6053fSVladimir Oltean 			  bool keep)
106860f6053fSVladimir Oltean {
106960f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
107060f6053fSVladimir Oltean 	struct sja1105_table *table;
107160f6053fSVladimir Oltean 	int rc, match;
107260f6053fSVladimir Oltean 
107360f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
107460f6053fSVladimir Oltean 
107560f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
107660f6053fSVladimir Oltean 	if (match < 0) {
107760f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
107860f6053fSVladimir Oltean 		if (!keep)
107960f6053fSVladimir Oltean 			return 0;
108060f6053fSVladimir Oltean 
108160f6053fSVladimir Oltean 		/* No match => new entry */
108260f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
108360f6053fSVladimir Oltean 		if (rc)
108460f6053fSVladimir Oltean 			return rc;
108560f6053fSVladimir Oltean 
108660f6053fSVladimir Oltean 		match = table->entry_count - 1;
108760f6053fSVladimir Oltean 	}
108860f6053fSVladimir Oltean 
108960f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
109060f6053fSVladimir Oltean 	l2_lookup = table->entries;
109160f6053fSVladimir Oltean 
109260f6053fSVladimir Oltean 	/* We have a match.
109360f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
109460f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
109560f6053fSVladimir Oltean 	 * which we update it).
109660f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
109760f6053fSVladimir Oltean 	 */
109860f6053fSVladimir Oltean 	if (keep) {
109960f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
110060f6053fSVladimir Oltean 		return 0;
110160f6053fSVladimir Oltean 	}
110260f6053fSVladimir Oltean 
110360f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
110460f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
110560f6053fSVladimir Oltean 	 */
110660f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
110760f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
110860f6053fSVladimir Oltean }
110960f6053fSVladimir Oltean 
1110291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1111291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1112291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1113291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1114291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1115291d1e72SVladimir Oltean  */
111609c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1117291d1e72SVladimir Oltean {
1118291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1119291d1e72SVladimir Oltean }
1120291d1e72SVladimir Oltean 
11219dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1122291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1123291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1124291d1e72SVladimir Oltean 					 int *last_unused)
1125291d1e72SVladimir Oltean {
1126291d1e72SVladimir Oltean 	int way;
1127291d1e72SVladimir Oltean 
1128291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1129291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1130291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1131291d1e72SVladimir Oltean 
1132291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1133291d1e72SVladimir Oltean 		 * into the return value
1134291d1e72SVladimir Oltean 		 */
1135291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1136291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1137291d1e72SVladimir Oltean 			if (last_unused)
1138291d1e72SVladimir Oltean 				*last_unused = way;
1139291d1e72SVladimir Oltean 			continue;
1140291d1e72SVladimir Oltean 		}
1141291d1e72SVladimir Oltean 
1142291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1143291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1144291d1e72SVladimir Oltean 			if (match)
1145291d1e72SVladimir Oltean 				*match = l2_lookup;
1146291d1e72SVladimir Oltean 			return way;
1147291d1e72SVladimir Oltean 		}
1148291d1e72SVladimir Oltean 	}
1149291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1150291d1e72SVladimir Oltean 	return -1;
1151291d1e72SVladimir Oltean }
1152291d1e72SVladimir Oltean 
11539dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1154291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1155291d1e72SVladimir Oltean {
1156291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1157291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1158291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1159291d1e72SVladimir Oltean 	int last_unused = -1;
116060f6053fSVladimir Oltean 	int bin, way, rc;
1161291d1e72SVladimir Oltean 
11629dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1163291d1e72SVladimir Oltean 
11649dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1165291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1166291d1e72SVladimir Oltean 	if (way >= 0) {
1167291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1168291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1169291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1170291d1e72SVladimir Oltean 		 */
1171291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1172291d1e72SVladimir Oltean 			return 0;
1173291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1174291d1e72SVladimir Oltean 	} else {
1175291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1176291d1e72SVladimir Oltean 
1177291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1178291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1179291d1e72SVladimir Oltean 		 */
1180291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1181291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1182291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1183291d1e72SVladimir Oltean 
1184291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1185291d1e72SVladimir Oltean 			way = last_unused;
1186291d1e72SVladimir Oltean 		} else {
1187291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1188291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1189291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1190291d1e72SVladimir Oltean 			 * distribution function:
1191291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1192291d1e72SVladimir Oltean 			 */
1193291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1194291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1195291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1196291d1e72SVladimir Oltean 				 bin, addr, way);
1197291d1e72SVladimir Oltean 			/* Evict entry */
1198291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1199291d1e72SVladimir Oltean 						     index, NULL, false);
1200291d1e72SVladimir Oltean 		}
1201291d1e72SVladimir Oltean 	}
1202291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1203291d1e72SVladimir Oltean 
120460f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1205291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1206291d1e72SVladimir Oltean 					  true);
120760f6053fSVladimir Oltean 	if (rc < 0)
120860f6053fSVladimir Oltean 		return rc;
120960f6053fSVladimir Oltean 
121060f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1211291d1e72SVladimir Oltean }
1212291d1e72SVladimir Oltean 
12139dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1214291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1215291d1e72SVladimir Oltean {
1216291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1217291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
121860f6053fSVladimir Oltean 	int index, bin, way, rc;
1219291d1e72SVladimir Oltean 	bool keep;
1220291d1e72SVladimir Oltean 
12219dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
12229dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1223291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1224291d1e72SVladimir Oltean 	if (way < 0)
1225291d1e72SVladimir Oltean 		return 0;
1226291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1227291d1e72SVladimir Oltean 
1228291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1229291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1230291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1231291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1232291d1e72SVladimir Oltean 	 */
1233291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
12347752e937SVladimir Oltean 
1235291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1236291d1e72SVladimir Oltean 		keep = true;
1237291d1e72SVladimir Oltean 	else
1238291d1e72SVladimir Oltean 		keep = false;
1239291d1e72SVladimir Oltean 
124060f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1241291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
124260f6053fSVladimir Oltean 	if (rc < 0)
124360f6053fSVladimir Oltean 		return rc;
124460f6053fSVladimir Oltean 
124560f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1246291d1e72SVladimir Oltean }
1247291d1e72SVladimir Oltean 
12489dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
12499dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
12509dfa6911SVladimir Oltean {
12511da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
12521da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12531da73821SVladimir Oltean 	int rc, i;
12541da73821SVladimir Oltean 
12551da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
12561da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
12571da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
12581da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
12591da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
126068bb8ea8SVivien Didelot 	if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
12611da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
12621da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
12636d7c7d94SVladimir Oltean 	} else {
12646d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
12656d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
12666d7c7d94SVladimir Oltean 	}
12671da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
12681da73821SVladimir Oltean 
12691da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
12701da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
12711da73821SVladimir Oltean 	if (rc == 0) {
12721da73821SVladimir Oltean 		/* Found and this port is already in the entry's
12731da73821SVladimir Oltean 		 * port mask => job done
12741da73821SVladimir Oltean 		 */
12751da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
12761da73821SVladimir Oltean 			return 0;
12771da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
12781da73821SVladimir Oltean 		 * found something.
12791da73821SVladimir Oltean 		 */
12801da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
12811da73821SVladimir Oltean 		goto skip_finding_an_index;
12821da73821SVladimir Oltean 	}
12831da73821SVladimir Oltean 
12841da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
12851da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
12861da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
12871da73821SVladimir Oltean 	 */
12881da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
12891da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
12901da73821SVladimir Oltean 						 i, NULL);
12911da73821SVladimir Oltean 		if (rc < 0)
12921da73821SVladimir Oltean 			break;
12931da73821SVladimir Oltean 	}
12941da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
12951da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
12961da73821SVladimir Oltean 		return -EINVAL;
12971da73821SVladimir Oltean 	}
129817ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
12991da73821SVladimir Oltean 	l2_lookup.index = i;
13001da73821SVladimir Oltean 
13011da73821SVladimir Oltean skip_finding_an_index:
130260f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
13031da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
13041da73821SVladimir Oltean 					  true);
130560f6053fSVladimir Oltean 	if (rc < 0)
130660f6053fSVladimir Oltean 		return rc;
130760f6053fSVladimir Oltean 
130860f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
13099dfa6911SVladimir Oltean }
13109dfa6911SVladimir Oltean 
13119dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
13129dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
13139dfa6911SVladimir Oltean {
13141da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
13151da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13161da73821SVladimir Oltean 	bool keep;
13171da73821SVladimir Oltean 	int rc;
13181da73821SVladimir Oltean 
13191da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13201da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13211da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13221da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
132368bb8ea8SVivien Didelot 	if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
13241da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13251da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13266d7c7d94SVladimir Oltean 	} else {
13276d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13286d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13296d7c7d94SVladimir Oltean 	}
13301da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13311da73821SVladimir Oltean 
13321da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13331da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
13341da73821SVladimir Oltean 	if (rc < 0)
13351da73821SVladimir Oltean 		return 0;
13361da73821SVladimir Oltean 
13371da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
13381da73821SVladimir Oltean 
13391da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
13401da73821SVladimir Oltean 	 * or if we remove it completely.
13411da73821SVladimir Oltean 	 */
13421da73821SVladimir Oltean 	if (l2_lookup.destports)
13431da73821SVladimir Oltean 		keep = true;
13441da73821SVladimir Oltean 	else
13451da73821SVladimir Oltean 		keep = false;
13461da73821SVladimir Oltean 
134760f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
13481da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
134960f6053fSVladimir Oltean 	if (rc < 0)
135060f6053fSVladimir Oltean 		return rc;
135160f6053fSVladimir Oltean 
135260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
13539dfa6911SVladimir Oltean }
13549dfa6911SVladimir Oltean 
13559dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
13569dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
13579dfa6911SVladimir Oltean {
13589dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1359b3ee526aSVladimir Oltean 
13606d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
13616d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
13626d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
13636d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
13646d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
13656d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
13666d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
13676d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
136893647594SVladimir Oltean 	 */
136968bb8ea8SVivien Didelot 	if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
13706d7c7d94SVladimir Oltean 		vid = 0;
137193647594SVladimir Oltean 
13726d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
13739dfa6911SVladimir Oltean }
13749dfa6911SVladimir Oltean 
13759dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
13769dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
13779dfa6911SVladimir Oltean {
13789dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13799dfa6911SVladimir Oltean 
138068bb8ea8SVivien Didelot 	if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
13816d7c7d94SVladimir Oltean 		vid = 0;
13826d7c7d94SVladimir Oltean 
1383b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
13849dfa6911SVladimir Oltean }
13859dfa6911SVladimir Oltean 
1386291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1387291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1388291d1e72SVladimir Oltean {
1389291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1390291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1391291d1e72SVladimir Oltean 	int i;
1392291d1e72SVladimir Oltean 
1393291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1394291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1395291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1396291d1e72SVladimir Oltean 		int rc;
1397291d1e72SVladimir Oltean 
1398291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1399291d1e72SVladimir Oltean 						 i, &l2_lookup);
1400291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1401def84604SVladimir Oltean 		if (rc == -ENOENT)
1402291d1e72SVladimir Oltean 			continue;
1403291d1e72SVladimir Oltean 		if (rc) {
1404291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1405291d1e72SVladimir Oltean 			return rc;
1406291d1e72SVladimir Oltean 		}
1407291d1e72SVladimir Oltean 
1408291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1409291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1410291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1411291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1412291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1413291d1e72SVladimir Oltean 		 */
1414291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1415291d1e72SVladimir Oltean 			continue;
1416291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
141793647594SVladimir Oltean 
14186d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
141968bb8ea8SVivien Didelot 		if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
14206d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
142117ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1422291d1e72SVladimir Oltean 	}
1423291d1e72SVladimir Oltean 	return 0;
1424291d1e72SVladimir Oltean }
1425291d1e72SVladimir Oltean 
1426291d1e72SVladimir Oltean /* This callback needs to be present */
1427291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1428291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1429291d1e72SVladimir Oltean {
1430291d1e72SVladimir Oltean 	return 0;
1431291d1e72SVladimir Oltean }
1432291d1e72SVladimir Oltean 
1433291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1434291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1435291d1e72SVladimir Oltean {
1436291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1437291d1e72SVladimir Oltean }
1438291d1e72SVladimir Oltean 
1439291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1440291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1441291d1e72SVladimir Oltean {
1442291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1443291d1e72SVladimir Oltean }
1444291d1e72SVladimir Oltean 
14458aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
14468aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
14478aa9ebccSVladimir Oltean {
14488aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
14498aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14508aa9ebccSVladimir Oltean 	int i, rc;
14518aa9ebccSVladimir Oltean 
14528aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
14538aa9ebccSVladimir Oltean 
14548aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
14558aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
14568aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
14578aa9ebccSVladimir Oltean 		 */
14588aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
14598aa9ebccSVladimir Oltean 			continue;
14608aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
14618aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
14628aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
14638aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
14648aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
14658aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
14668aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
14678aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
14688aa9ebccSVladimir Oltean 		 */
14698aa9ebccSVladimir Oltean 		if (i == port)
14708aa9ebccSVladimir Oltean 			continue;
14718aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
14728aa9ebccSVladimir Oltean 			continue;
14738aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
14748aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
14758aa9ebccSVladimir Oltean 
14768aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
14778aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
14788aa9ebccSVladimir Oltean 		if (rc < 0)
14798aa9ebccSVladimir Oltean 			return rc;
14808aa9ebccSVladimir Oltean 	}
14818aa9ebccSVladimir Oltean 
14828aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
14838aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
14848aa9ebccSVladimir Oltean }
14858aa9ebccSVladimir Oltean 
1486640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1487640f763fSVladimir Oltean 					 u8 state)
1488640f763fSVladimir Oltean {
1489640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1490640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1491640f763fSVladimir Oltean 
1492640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1493640f763fSVladimir Oltean 
1494640f763fSVladimir Oltean 	switch (state) {
1495640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1496640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1497640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1498640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1499640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1500640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1501640f763fSVladimir Oltean 		 */
1502640f763fSVladimir Oltean 		mac[port].ingress   = false;
1503640f763fSVladimir Oltean 		mac[port].egress    = false;
1504640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1505640f763fSVladimir Oltean 		break;
1506640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1507640f763fSVladimir Oltean 		mac[port].ingress   = true;
1508640f763fSVladimir Oltean 		mac[port].egress    = false;
1509640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1510640f763fSVladimir Oltean 		break;
1511640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1512640f763fSVladimir Oltean 		mac[port].ingress   = true;
1513640f763fSVladimir Oltean 		mac[port].egress    = false;
1514640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1515640f763fSVladimir Oltean 		break;
1516640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1517640f763fSVladimir Oltean 		mac[port].ingress   = true;
1518640f763fSVladimir Oltean 		mac[port].egress    = true;
1519640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1520640f763fSVladimir Oltean 		break;
1521640f763fSVladimir Oltean 	default:
1522640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1523640f763fSVladimir Oltean 		return;
1524640f763fSVladimir Oltean 	}
1525640f763fSVladimir Oltean 
1526640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1527640f763fSVladimir Oltean 				     &mac[port], true);
1528640f763fSVladimir Oltean }
1529640f763fSVladimir Oltean 
15308aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
15318aa9ebccSVladimir Oltean 			       struct net_device *br)
15328aa9ebccSVladimir Oltean {
15338aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
15348aa9ebccSVladimir Oltean }
15358aa9ebccSVladimir Oltean 
15368aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
15378aa9ebccSVladimir Oltean 				 struct net_device *br)
15388aa9ebccSVladimir Oltean {
15398aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
15408aa9ebccSVladimir Oltean }
15418aa9ebccSVladimir Oltean 
15422eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
15432eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
15442eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
15452eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
15462eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
15472eea1fa8SVladimir Oltean };
15482eea1fa8SVladimir Oltean 
15496666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
15506666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
15516666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
15526666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
15536666cebcSVladimir Oltean  * such that this operation is relatively seamless.
15546666cebcSVladimir Oltean  */
15552eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
15562eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
15576666cebcSVladimir Oltean {
15586cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
15596cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
15606666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
15616666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
15626cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
15636cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
15646cf99c13SVladimir Oltean 	s64 t12, t34;
1565ffe10e67SVladimir Oltean 	u16 bmcr = 0;
15666666cebcSVladimir Oltean 	int rc, i;
15676cf99c13SVladimir Oltean 	s64 now;
15686666cebcSVladimir Oltean 
1569af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1570af580ae2SVladimir Oltean 
15716666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
15726666cebcSVladimir Oltean 
15738400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
15748400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
15758400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
15768400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
15776666cebcSVladimir Oltean 	 */
15786666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
15796666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
15806666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
15816666cebcSVladimir Oltean 	}
15826666cebcSVladimir Oltean 
1583ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1584ffe10e67SVladimir Oltean 		bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1585ffe10e67SVladimir Oltean 
15866cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
15876cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
15886cf99c13SVladimir Oltean 
15896cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
15906cf99c13SVladimir Oltean 	if (rc < 0)
15916cf99c13SVladimir Oltean 		goto out_unlock_ptp;
15926cf99c13SVladimir Oltean 
15936666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
15946666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
15956666cebcSVladimir Oltean 	if (rc < 0)
15966cf99c13SVladimir Oltean 		goto out_unlock_ptp;
15976cf99c13SVladimir Oltean 
15986cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
15996cf99c13SVladimir Oltean 	if (rc < 0)
16006cf99c13SVladimir Oltean 		goto out_unlock_ptp;
16016cf99c13SVladimir Oltean 
16026cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
16036cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
16046cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
16056cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
16066cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
16076cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
16086cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
16096cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
16106cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
16116cf99c13SVladimir Oltean 	now += (t34 - t12);
16126cf99c13SVladimir Oltean 
16136cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
16146cf99c13SVladimir Oltean 
16156cf99c13SVladimir Oltean out_unlock_ptp:
16166cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
16176666cebcSVladimir Oltean 
16182eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
16192eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
16202eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
16212eea1fa8SVladimir Oltean 
16226666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
16236666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
16246666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
16256666cebcSVladimir Oltean 	 */
16266666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
16276666cebcSVladimir Oltean 	if (rc < 0)
16286666cebcSVladimir Oltean 		goto out;
16296666cebcSVladimir Oltean 
16306666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
16318400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
16326666cebcSVladimir Oltean 		if (rc < 0)
16336666cebcSVladimir Oltean 			goto out;
16346666cebcSVladimir Oltean 	}
1635ffe10e67SVladimir Oltean 
1636ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1637ffe10e67SVladimir Oltean 		bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1638ffe10e67SVladimir Oltean 
1639ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, an_enabled, false);
1640ffe10e67SVladimir Oltean 
1641ffe10e67SVladimir Oltean 		if (!an_enabled) {
1642ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1643ffe10e67SVladimir Oltean 
1644ffe10e67SVladimir Oltean 			if (bmcr & BMCR_SPEED1000)
1645ffe10e67SVladimir Oltean 				speed = SPEED_1000;
1646ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED100)
1647ffe10e67SVladimir Oltean 				speed = SPEED_100;
1648ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED10)
1649ffe10e67SVladimir Oltean 				speed = SPEED_10;
1650ffe10e67SVladimir Oltean 
1651ffe10e67SVladimir Oltean 			sja1105_sgmii_pcs_force_speed(priv, speed);
1652ffe10e67SVladimir Oltean 		}
1653ffe10e67SVladimir Oltean 	}
16546666cebcSVladimir Oltean out:
1655af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1656af580ae2SVladimir Oltean 
16576666cebcSVladimir Oltean 	return rc;
16586666cebcSVladimir Oltean }
16596666cebcSVladimir Oltean 
16606666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
16616666cebcSVladimir Oltean {
16626666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
16636666cebcSVladimir Oltean 
16646666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
16656666cebcSVladimir Oltean 
16666666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
16676666cebcSVladimir Oltean 
16686666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
16696666cebcSVladimir Oltean 					   &mac[port], true);
16706666cebcSVladimir Oltean }
16716666cebcSVladimir Oltean 
16726666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
16736666cebcSVladimir Oltean {
16746666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
16756666cebcSVladimir Oltean 	int count, i;
16766666cebcSVladimir Oltean 
16776666cebcSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
16786666cebcSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
16796666cebcSVladimir Oltean 
16806666cebcSVladimir Oltean 	for (i = 0; i < count; i++)
16816666cebcSVladimir Oltean 		if (vlan[i].vlanid == vid)
16826666cebcSVladimir Oltean 			return i;
16836666cebcSVladimir Oltean 
16846666cebcSVladimir Oltean 	/* Return an invalid entry index if not found */
16856666cebcSVladimir Oltean 	return -1;
16866666cebcSVladimir Oltean }
16876666cebcSVladimir Oltean 
16886666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
16896666cebcSVladimir Oltean 			      bool enabled, bool untagged)
16906666cebcSVladimir Oltean {
16916666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
16926666cebcSVladimir Oltean 	struct sja1105_table *table;
16936666cebcSVladimir Oltean 	bool keep = true;
16946666cebcSVladimir Oltean 	int match, rc;
16956666cebcSVladimir Oltean 
16966666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
16976666cebcSVladimir Oltean 
16986666cebcSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
16996666cebcSVladimir Oltean 	if (match < 0) {
17006666cebcSVladimir Oltean 		/* Can't delete a missing entry. */
17016666cebcSVladimir Oltean 		if (!enabled)
17026666cebcSVladimir Oltean 			return 0;
17036666cebcSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
17046666cebcSVladimir Oltean 		if (rc)
17056666cebcSVladimir Oltean 			return rc;
17066666cebcSVladimir Oltean 		match = table->entry_count - 1;
17076666cebcSVladimir Oltean 	}
17086666cebcSVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
17096666cebcSVladimir Oltean 	vlan = table->entries;
17106666cebcSVladimir Oltean 	vlan[match].vlanid = vid;
17116666cebcSVladimir Oltean 	if (enabled) {
17126666cebcSVladimir Oltean 		vlan[match].vlan_bc |= BIT(port);
17136666cebcSVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
17146666cebcSVladimir Oltean 	} else {
17156666cebcSVladimir Oltean 		vlan[match].vlan_bc &= ~BIT(port);
17166666cebcSVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
17176666cebcSVladimir Oltean 	}
17186666cebcSVladimir Oltean 	/* Also unset tag_port if removing this VLAN was requested,
17196666cebcSVladimir Oltean 	 * just so we don't have a confusing bitmap (no practical purpose).
17206666cebcSVladimir Oltean 	 */
17216666cebcSVladimir Oltean 	if (untagged || !enabled)
17226666cebcSVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
17236666cebcSVladimir Oltean 	else
17246666cebcSVladimir Oltean 		vlan[match].tag_port |= BIT(port);
17256666cebcSVladimir Oltean 	/* If there's no port left as member of this VLAN,
17266666cebcSVladimir Oltean 	 * it's time for it to go.
17276666cebcSVladimir Oltean 	 */
17286666cebcSVladimir Oltean 	if (!vlan[match].vmemb_port)
17296666cebcSVladimir Oltean 		keep = false;
17306666cebcSVladimir Oltean 
17316666cebcSVladimir Oltean 	dev_dbg(priv->ds->dev,
17326666cebcSVladimir Oltean 		"%s: port %d, vid %llu, broadcast domain 0x%llx, "
17336666cebcSVladimir Oltean 		"port members 0x%llx, tagged ports 0x%llx, keep %d\n",
17346666cebcSVladimir Oltean 		__func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
17356666cebcSVladimir Oltean 		vlan[match].vmemb_port, vlan[match].tag_port, keep);
17366666cebcSVladimir Oltean 
17376666cebcSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
17386666cebcSVladimir Oltean 					  &vlan[match], keep);
17396666cebcSVladimir Oltean 	if (rc < 0)
17406666cebcSVladimir Oltean 		return rc;
17416666cebcSVladimir Oltean 
17426666cebcSVladimir Oltean 	if (!keep)
17436666cebcSVladimir Oltean 		return sja1105_table_delete_entry(table, match);
17446666cebcSVladimir Oltean 
17456666cebcSVladimir Oltean 	return 0;
17466666cebcSVladimir Oltean }
17476666cebcSVladimir Oltean 
1748227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1749227d07a0SVladimir Oltean {
1750227d07a0SVladimir Oltean 	int rc, i;
1751227d07a0SVladimir Oltean 
1752227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1753227d07a0SVladimir Oltean 		rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1754227d07a0SVladimir Oltean 		if (rc < 0) {
1755227d07a0SVladimir Oltean 			dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1756227d07a0SVladimir Oltean 				i, rc);
1757227d07a0SVladimir Oltean 			return rc;
1758227d07a0SVladimir Oltean 		}
1759227d07a0SVladimir Oltean 	}
1760227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1761227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1762227d07a0SVladimir Oltean 	return 0;
1763227d07a0SVladimir Oltean }
1764227d07a0SVladimir Oltean 
17658aa9ebccSVladimir Oltean static enum dsa_tag_protocol
17664d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
17674d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
17688aa9ebccSVladimir Oltean {
1769227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
17708aa9ebccSVladimir Oltean }
17718aa9ebccSVladimir Oltean 
17726666cebcSVladimir Oltean /* This callback needs to be present */
17736666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
17746666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
17756666cebcSVladimir Oltean {
17766666cebcSVladimir Oltean 	return 0;
17776666cebcSVladimir Oltean }
17786666cebcSVladimir Oltean 
1779070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
1780070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
1781070ca3bbSVladimir Oltean  * So a switch reset is required.
1782070ca3bbSVladimir Oltean  */
17836666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
17846666cebcSVladimir Oltean {
17856d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
1786070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
17876666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1788070ca3bbSVladimir Oltean 	struct sja1105_table *table;
1789070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
17906666cebcSVladimir Oltean 	int rc;
17916666cebcSVladimir Oltean 
1792070ca3bbSVladimir Oltean 	if (enabled) {
17936666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
179454fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
179554fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
1796070ca3bbSVladimir Oltean 	} else {
17976666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
1798070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
1799070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
1800070ca3bbSVladimir Oltean 	}
1801070ca3bbSVladimir Oltean 
1802070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1803070ca3bbSVladimir Oltean 	general_params = table->entries;
1804f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
180554fa49eeSVladimir Oltean 	general_params->tpid = tpid;
180654fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
1807070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
180842824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
180942824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
181042824463SVladimir Oltean 	 */
181142824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
181242824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
1813070ca3bbSVladimir Oltean 
18146d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
18156d7c7d94SVladimir Oltean 	 * No VLAN filtering => shared VLAN learning.
18166d7c7d94SVladimir Oltean 	 *
18176d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
18186d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
18196d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
18206d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
18216d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
18226d7c7d94SVladimir Oltean 	 * forwarding decision.
18236d7c7d94SVladimir Oltean 	 *
18246d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
18256d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
18266d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
18276d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
18286d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
18296d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
18306d7c7d94SVladimir Oltean 	 * (all frames get flooded).
18316d7c7d94SVladimir Oltean 	 */
18326d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
18336d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
18346d7c7d94SVladimir Oltean 	l2_lookup_params->shared_learn = !enabled;
18356d7c7d94SVladimir Oltean 
18362eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
18376666cebcSVladimir Oltean 	if (rc)
18386666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
18396666cebcSVladimir Oltean 
1840227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
1841227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
1842227d07a0SVladimir Oltean 	 * the two configurations are mutually exclusive.
1843227d07a0SVladimir Oltean 	 */
1844227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, !enabled);
18456666cebcSVladimir Oltean }
18466666cebcSVladimir Oltean 
18476666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
18486666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
18496666cebcSVladimir Oltean {
18506666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18516666cebcSVladimir Oltean 	u16 vid;
18526666cebcSVladimir Oltean 	int rc;
18536666cebcSVladimir Oltean 
18546666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
18556666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
18566666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
18576666cebcSVladimir Oltean 		if (rc < 0) {
18586666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
18596666cebcSVladimir Oltean 				vid, port, rc);
18606666cebcSVladimir Oltean 			return;
18616666cebcSVladimir Oltean 		}
18626666cebcSVladimir Oltean 		if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
18636666cebcSVladimir Oltean 			rc = sja1105_pvid_apply(ds->priv, port, vid);
18646666cebcSVladimir Oltean 			if (rc < 0) {
18656666cebcSVladimir Oltean 				dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
18666666cebcSVladimir Oltean 					vid, port, rc);
18676666cebcSVladimir Oltean 				return;
18686666cebcSVladimir Oltean 			}
18696666cebcSVladimir Oltean 		}
18706666cebcSVladimir Oltean 	}
18716666cebcSVladimir Oltean }
18726666cebcSVladimir Oltean 
18736666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
18746666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
18756666cebcSVladimir Oltean {
18766666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18776666cebcSVladimir Oltean 	u16 vid;
18786666cebcSVladimir Oltean 	int rc;
18796666cebcSVladimir Oltean 
18806666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
18816666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
18826666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
18836666cebcSVladimir Oltean 		if (rc < 0) {
18846666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
18856666cebcSVladimir Oltean 				vid, port, rc);
18866666cebcSVladimir Oltean 			return rc;
18876666cebcSVladimir Oltean 		}
18886666cebcSVladimir Oltean 	}
18896666cebcSVladimir Oltean 	return 0;
18906666cebcSVladimir Oltean }
18916666cebcSVladimir Oltean 
18928aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
18938aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
18948aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
18958aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
18968aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
18978aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
18988aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
18998aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
19008aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
19018aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
19028aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
19038aa9ebccSVladimir Oltean  */
19048aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
19058aa9ebccSVladimir Oltean {
19068aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
19078aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19088aa9ebccSVladimir Oltean 	int rc;
19098aa9ebccSVladimir Oltean 
19108aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
19118aa9ebccSVladimir Oltean 	if (rc < 0) {
19128aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
19138aa9ebccSVladimir Oltean 		return rc;
19148aa9ebccSVladimir Oltean 	}
1915f5b8631cSVladimir Oltean 
1916f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
1917f5b8631cSVladimir Oltean 	 * and we can't apply them.
1918f5b8631cSVladimir Oltean 	 */
1919f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
1920f5b8631cSVladimir Oltean 	if (rc < 0) {
1921f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
1922f5b8631cSVladimir Oltean 		return rc;
1923f5b8631cSVladimir Oltean 	}
1924f5b8631cSVladimir Oltean 
192561c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
1926bb77f36aSVladimir Oltean 	if (rc < 0) {
1927bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
1928bb77f36aSVladimir Oltean 		return rc;
1929bb77f36aSVladimir Oltean 	}
19308aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
19318aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
19328aa9ebccSVladimir Oltean 	if (rc < 0) {
19338aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
19348aa9ebccSVladimir Oltean 		return rc;
19358aa9ebccSVladimir Oltean 	}
19368aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
19378aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
19388aa9ebccSVladimir Oltean 	if (rc < 0) {
19398aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
19408aa9ebccSVladimir Oltean 		return rc;
19418aa9ebccSVladimir Oltean 	}
19426666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
19436666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
19446666cebcSVladimir Oltean 	 * EtherType is.
19456666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
19466666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
19476666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
19486666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
19496666cebcSVladimir Oltean 	 */
19506666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
19518aa9ebccSVladimir Oltean 
19525f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
19535f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
19545f06c63bSVladimir Oltean 
1955227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
1956227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
1957227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
1958227d07a0SVladimir Oltean 	 */
1959227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
1960227d07a0SVladimir Oltean }
1961227d07a0SVladimir Oltean 
1962f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
1963f3097be2SVladimir Oltean {
1964f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1965a68578c2SVladimir Oltean 	int port;
1966a68578c2SVladimir Oltean 
1967a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
1968a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
1969a68578c2SVladimir Oltean 
1970a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1971a68578c2SVladimir Oltean 			continue;
1972a68578c2SVladimir Oltean 
197352c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
1974a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
1975a68578c2SVladimir Oltean 	}
1976f3097be2SVladimir Oltean 
1977317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
197861c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
19796cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
1980f3097be2SVladimir Oltean }
1981f3097be2SVladimir Oltean 
1982e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port,
1983e9bf9694SVladimir Oltean 			       struct phy_device *phy)
1984e9bf9694SVladimir Oltean {
1985e9bf9694SVladimir Oltean 	struct net_device *slave;
1986e9bf9694SVladimir Oltean 
1987e9bf9694SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1988e9bf9694SVladimir Oltean 		return 0;
1989e9bf9694SVladimir Oltean 
199068bb8ea8SVivien Didelot 	slave = dsa_to_port(ds, port)->slave;
1991e9bf9694SVladimir Oltean 
1992e9bf9694SVladimir Oltean 	slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1993e9bf9694SVladimir Oltean 
1994e9bf9694SVladimir Oltean 	return 0;
1995e9bf9694SVladimir Oltean }
1996e9bf9694SVladimir Oltean 
1997a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
1998a68578c2SVladimir Oltean {
1999a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2000a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
2001a68578c2SVladimir Oltean 
2002a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
2003a68578c2SVladimir Oltean 		return;
2004a68578c2SVladimir Oltean 
2005a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
2006a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
2007a68578c2SVladimir Oltean }
2008a68578c2SVladimir Oltean 
2009227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
201047ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2011227d07a0SVladimir Oltean {
2012227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2013227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2014227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2015227d07a0SVladimir Oltean 	int timeout = 10;
2016227d07a0SVladimir Oltean 	int rc;
2017227d07a0SVladimir Oltean 
2018227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2019227d07a0SVladimir Oltean 
2020227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2021227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2022227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
202347ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
202447ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2025227d07a0SVladimir Oltean 
2026227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2027227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2028227d07a0SVladimir Oltean 	if (rc < 0) {
2029227d07a0SVladimir Oltean 		kfree_skb(skb);
2030227d07a0SVladimir Oltean 		return rc;
2031227d07a0SVladimir Oltean 	}
2032227d07a0SVladimir Oltean 
2033227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
203468bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2035227d07a0SVladimir Oltean 
2036227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2037227d07a0SVladimir Oltean 	do {
2038227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2039227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2040227d07a0SVladimir Oltean 		if (rc < 0) {
2041227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2042227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2043227d07a0SVladimir Oltean 			continue;
2044227d07a0SVladimir Oltean 		}
2045227d07a0SVladimir Oltean 
2046227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2047227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2048227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2049227d07a0SVladimir Oltean 		 */
2050227d07a0SVladimir Oltean 		cpu_relax();
2051227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2052227d07a0SVladimir Oltean 
2053227d07a0SVladimir Oltean 	if (!timeout) {
2054227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2055227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
20562a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
20572a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2058227d07a0SVladimir Oltean 		 */
2059227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2060227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2061227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2062227d07a0SVladimir Oltean 	}
2063227d07a0SVladimir Oltean 
2064227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2065227d07a0SVladimir Oltean }
2066227d07a0SVladimir Oltean 
2067a68578c2SVladimir Oltean #define work_to_port(work) \
2068a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2069a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2070a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2071a68578c2SVladimir Oltean 
2072227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2073227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2074227d07a0SVladimir Oltean  * lock on the bus)
2075227d07a0SVladimir Oltean  */
2076a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2077227d07a0SVladimir Oltean {
2078a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2079a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2080a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2081a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2082a68578c2SVladimir Oltean 	struct sk_buff *skb;
2083a68578c2SVladimir Oltean 
2084a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2085a68578c2SVladimir Oltean 		struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
2086227d07a0SVladimir Oltean 
2087227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2088227d07a0SVladimir Oltean 
2089a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2090a68578c2SVladimir Oltean 
209147ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2092a68578c2SVladimir Oltean 		if (clone)
2093a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2094227d07a0SVladimir Oltean 
2095227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2096a68578c2SVladimir Oltean 	}
20978aa9ebccSVladimir Oltean }
20988aa9ebccSVladimir Oltean 
20998456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
21008456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
21018456721dSVladimir Oltean  */
21028456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
21038456721dSVladimir Oltean 				   unsigned int ageing_time)
21048456721dSVladimir Oltean {
21058456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
21068456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
21078456721dSVladimir Oltean 	struct sja1105_table *table;
21088456721dSVladimir Oltean 	unsigned int maxage;
21098456721dSVladimir Oltean 
21108456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
21118456721dSVladimir Oltean 	l2_lookup_params = table->entries;
21128456721dSVladimir Oltean 
21138456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
21148456721dSVladimir Oltean 
21158456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
21168456721dSVladimir Oltean 		return 0;
21178456721dSVladimir Oltean 
21188456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
21198456721dSVladimir Oltean 
21202eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
21218456721dSVladimir Oltean }
21228456721dSVladimir Oltean 
2123317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2124317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2125317ab5b8SVladimir Oltean 				 void *type_data)
2126317ab5b8SVladimir Oltean {
2127317ab5b8SVladimir Oltean 	switch (type) {
2128317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2129317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
2130317ab5b8SVladimir Oltean 	default:
2131317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2132317ab5b8SVladimir Oltean 	}
2133317ab5b8SVladimir Oltean }
2134317ab5b8SVladimir Oltean 
2135511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2136511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2137511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2138511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2139511e6ca0SVladimir Oltean  * mirroring rule that references it.
2140511e6ca0SVladimir Oltean  */
2141511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2142511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2143511e6ca0SVladimir Oltean {
2144511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2145511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2146511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2147511e6ca0SVladimir Oltean 	bool already_enabled;
2148511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2149511e6ca0SVladimir Oltean 	int rc;
2150511e6ca0SVladimir Oltean 
2151511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2152511e6ca0SVladimir Oltean 	general_params = table->entries;
2153511e6ca0SVladimir Oltean 
2154511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2155511e6ca0SVladimir Oltean 
2156511e6ca0SVladimir Oltean 	already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
2157511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2158511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2159511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2160511e6ca0SVladimir Oltean 			general_params->mirr_port);
2161511e6ca0SVladimir Oltean 		return -EBUSY;
2162511e6ca0SVladimir Oltean 	}
2163511e6ca0SVladimir Oltean 
2164511e6ca0SVladimir Oltean 	new_mirr_port = to;
2165511e6ca0SVladimir Oltean 	if (!enabled) {
2166511e6ca0SVladimir Oltean 		bool keep = false;
2167511e6ca0SVladimir Oltean 		int port;
2168511e6ca0SVladimir Oltean 
2169511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2170511e6ca0SVladimir Oltean 		for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2171511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2172511e6ca0SVladimir Oltean 				keep = true;
2173511e6ca0SVladimir Oltean 				break;
2174511e6ca0SVladimir Oltean 			}
2175511e6ca0SVladimir Oltean 		}
2176511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2177511e6ca0SVladimir Oltean 		if (!keep)
2178511e6ca0SVladimir Oltean 			new_mirr_port = SJA1105_NUM_PORTS;
2179511e6ca0SVladimir Oltean 	}
2180511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2181511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2182511e6ca0SVladimir Oltean 
2183511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2184511e6ca0SVladimir Oltean 						  0, general_params, true);
2185511e6ca0SVladimir Oltean 		if (rc < 0)
2186511e6ca0SVladimir Oltean 			return rc;
2187511e6ca0SVladimir Oltean 	}
2188511e6ca0SVladimir Oltean 
2189511e6ca0SVladimir Oltean 	if (ingress)
2190511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2191511e6ca0SVladimir Oltean 	else
2192511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2193511e6ca0SVladimir Oltean 
2194511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2195511e6ca0SVladimir Oltean 					    &mac[from], true);
2196511e6ca0SVladimir Oltean }
2197511e6ca0SVladimir Oltean 
2198511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2199511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2200511e6ca0SVladimir Oltean 			      bool ingress)
2201511e6ca0SVladimir Oltean {
2202511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2203511e6ca0SVladimir Oltean 				    ingress, true);
2204511e6ca0SVladimir Oltean }
2205511e6ca0SVladimir Oltean 
2206511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2207511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2208511e6ca0SVladimir Oltean {
2209511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2210511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2211511e6ca0SVladimir Oltean }
2212511e6ca0SVladimir Oltean 
22138aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
22148aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
22158aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2216f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
22178456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2218ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2219ffe10e67SVladimir Oltean 	.phylink_mac_link_state	= sja1105_mac_pcs_get_state,
2220af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
22218400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
22228400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
222352c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
222452c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
222552c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2226bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2227e9bf9694SVladimir Oltean 	.port_enable		= sja1105_port_enable,
2228a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2229291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2230291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2231291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
22328aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
22338aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
2234640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
22356666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
22366666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
22376666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
22386666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
2239291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
2240291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2241291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2242a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2243a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2244f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
224547ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2246317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2247511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2248511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
22498aa9ebccSVladimir Oltean };
22508aa9ebccSVladimir Oltean 
22518aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
22528aa9ebccSVladimir Oltean {
22538aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
22548aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
22558aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2256dff79620SVladimir Oltean 	u32 device_id;
22578aa9ebccSVladimir Oltean 	u64 part_no;
22588aa9ebccSVladimir Oltean 	int rc;
22598aa9ebccSVladimir Oltean 
226034d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
226134d76e9fSVladimir Oltean 			      NULL);
22628aa9ebccSVladimir Oltean 	if (rc < 0)
22638aa9ebccSVladimir Oltean 		return rc;
22648aa9ebccSVladimir Oltean 
22658aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
2266dff79620SVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n",
22678aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
22688aa9ebccSVladimir Oltean 		return -ENODEV;
22698aa9ebccSVladimir Oltean 	}
22708aa9ebccSVladimir Oltean 
22711bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
22721bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
22738aa9ebccSVladimir Oltean 	if (rc < 0)
22748aa9ebccSVladimir Oltean 		return rc;
22758aa9ebccSVladimir Oltean 
22768aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
22778aa9ebccSVladimir Oltean 
22788aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
22798aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
22808aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
22818aa9ebccSVladimir Oltean 		return -ENODEV;
22828aa9ebccSVladimir Oltean 	}
22838aa9ebccSVladimir Oltean 
22848aa9ebccSVladimir Oltean 	return 0;
22858aa9ebccSVladimir Oltean }
22868aa9ebccSVladimir Oltean 
22878aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
22888aa9ebccSVladimir Oltean {
2289844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
22908aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
22918aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
22928aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2293a68578c2SVladimir Oltean 	int rc, port;
22948aa9ebccSVladimir Oltean 
22958aa9ebccSVladimir Oltean 	if (!dev->of_node) {
22968aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
22978aa9ebccSVladimir Oltean 		return -EINVAL;
22988aa9ebccSVladimir Oltean 	}
22998aa9ebccSVladimir Oltean 
23008aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
23018aa9ebccSVladimir Oltean 	if (!priv)
23028aa9ebccSVladimir Oltean 		return -ENOMEM;
23038aa9ebccSVladimir Oltean 
23048aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
23058aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
23068aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
23078aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
23088aa9ebccSVladimir Oltean 	else
23098aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
23108aa9ebccSVladimir Oltean 
23118aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
23128aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
23138aa9ebccSVladimir Oltean 	 */
23148aa9ebccSVladimir Oltean 	priv->spidev = spi;
23158aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
23168aa9ebccSVladimir Oltean 
23178aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
23188aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
23198aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
23208aa9ebccSVladimir Oltean 	if (rc < 0) {
23218aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
23228aa9ebccSVladimir Oltean 		return rc;
23238aa9ebccSVladimir Oltean 	}
23248aa9ebccSVladimir Oltean 
23258aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
23268aa9ebccSVladimir Oltean 
23278aa9ebccSVladimir Oltean 	/* Detect hardware device */
23288aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
23298aa9ebccSVladimir Oltean 	if (rc < 0) {
23308aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
23318aa9ebccSVladimir Oltean 		return rc;
23328aa9ebccSVladimir Oltean 	}
23338aa9ebccSVladimir Oltean 
23348aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
23358aa9ebccSVladimir Oltean 
23367e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
23378aa9ebccSVladimir Oltean 	if (!ds)
23388aa9ebccSVladimir Oltean 		return -ENOMEM;
23398aa9ebccSVladimir Oltean 
23407e99e347SVivien Didelot 	ds->dev = dev;
23417e99e347SVivien Didelot 	ds->num_ports = SJA1105_NUM_PORTS;
23428aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
23438aa9ebccSVladimir Oltean 	ds->priv = priv;
23448aa9ebccSVladimir Oltean 	priv->ds = ds;
23458aa9ebccSVladimir Oltean 
2346844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
2347844d7edcSVladimir Oltean 
2348d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
2349d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
2350d5a619bfSVivien Didelot 
2351d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
2352d5a619bfSVivien Didelot 
2353d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
2354d5a619bfSVivien Didelot 	if (rc)
2355d5a619bfSVivien Didelot 		return rc;
2356d5a619bfSVivien Didelot 
2357227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
2358a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2359a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2360a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
2361a68578c2SVladimir Oltean 		struct net_device *slave;
2362227d07a0SVladimir Oltean 
2363a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2364a68578c2SVladimir Oltean 			continue;
2365a68578c2SVladimir Oltean 
2366a68578c2SVladimir Oltean 		dp->priv = sp;
2367a68578c2SVladimir Oltean 		sp->dp = dp;
2368844d7edcSVladimir Oltean 		sp->data = tagger_data;
2369a68578c2SVladimir Oltean 		slave = dp->slave;
2370a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
2371a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
2372a68578c2SVladimir Oltean 							slave->name);
2373a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
2374a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
2375a68578c2SVladimir Oltean 			dev_err(ds->dev,
2376a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
2377a68578c2SVladimir Oltean 				rc);
2378a68578c2SVladimir Oltean 			goto out;
2379a68578c2SVladimir Oltean 		}
2380a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
2381227d07a0SVladimir Oltean 	}
2382227d07a0SVladimir Oltean 
2383d5a619bfSVivien Didelot 	return 0;
2384a68578c2SVladimir Oltean out:
2385a68578c2SVladimir Oltean 	while (port-- > 0) {
2386a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2387a68578c2SVladimir Oltean 
2388a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2389a68578c2SVladimir Oltean 			continue;
2390a68578c2SVladimir Oltean 
2391a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
2392a68578c2SVladimir Oltean 	}
2393a68578c2SVladimir Oltean 	return rc;
23948aa9ebccSVladimir Oltean }
23958aa9ebccSVladimir Oltean 
23968aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
23978aa9ebccSVladimir Oltean {
23988aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
23998aa9ebccSVladimir Oltean 
24008aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
24018aa9ebccSVladimir Oltean 	return 0;
24028aa9ebccSVladimir Oltean }
24038aa9ebccSVladimir Oltean 
24048aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
24058aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
24068aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
24078aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
24088aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
24098aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
24108aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
24118aa9ebccSVladimir Oltean 	{ /* sentinel */ },
24128aa9ebccSVladimir Oltean };
24138aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
24148aa9ebccSVladimir Oltean 
24158aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
24168aa9ebccSVladimir Oltean 	.driver = {
24178aa9ebccSVladimir Oltean 		.name  = "sja1105",
24188aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
24198aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
24208aa9ebccSVladimir Oltean 	},
24218aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
24228aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
24238aa9ebccSVladimir Oltean };
24248aa9ebccSVladimir Oltean 
24258aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
24268aa9ebccSVladimir Oltean 
24278aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
24288aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
24298aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
24308aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
2431