xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 79d5511cc0eedbb2e49b543507d795f927db311b)
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 
482*79d5511cSVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv)
483*79d5511cSVladimir Oltean {
484*79d5511cSVladimir Oltean 	struct sja1105_avb_params_entry *avb;
485*79d5511cSVladimir Oltean 	struct sja1105_table *table;
486*79d5511cSVladimir Oltean 
487*79d5511cSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
488*79d5511cSVladimir Oltean 
489*79d5511cSVladimir Oltean 	/* Discard previous AVB Parameters Table */
490*79d5511cSVladimir Oltean 	if (table->entry_count) {
491*79d5511cSVladimir Oltean 		kfree(table->entries);
492*79d5511cSVladimir Oltean 		table->entry_count = 0;
493*79d5511cSVladimir Oltean 	}
494*79d5511cSVladimir Oltean 
495*79d5511cSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
496*79d5511cSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
497*79d5511cSVladimir Oltean 	if (!table->entries)
498*79d5511cSVladimir Oltean 		return -ENOMEM;
499*79d5511cSVladimir Oltean 
500*79d5511cSVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
501*79d5511cSVladimir Oltean 
502*79d5511cSVladimir Oltean 	avb = table->entries;
503*79d5511cSVladimir Oltean 
504*79d5511cSVladimir Oltean 	/* Configure the MAC addresses for meta frames */
505*79d5511cSVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
506*79d5511cSVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
507*79d5511cSVladimir Oltean 
508*79d5511cSVladimir Oltean 	return 0;
509*79d5511cSVladimir Oltean }
510*79d5511cSVladimir Oltean 
5118aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
5128aa9ebccSVladimir Oltean 
51309c1b412SVladimir Oltean static void sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
5148aa9ebccSVladimir Oltean 				  int index)
5158aa9ebccSVladimir Oltean {
5168aa9ebccSVladimir Oltean 	policing[index].sharindx = index;
5178aa9ebccSVladimir Oltean 	policing[index].smax = 65535; /* Burst size in bytes */
5188aa9ebccSVladimir Oltean 	policing[index].rate = SJA1105_RATE_MBPS(1000);
5198aa9ebccSVladimir Oltean 	policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
5208aa9ebccSVladimir Oltean 	policing[index].partition = 0;
5218aa9ebccSVladimir Oltean }
5228aa9ebccSVladimir Oltean 
5238aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
5248aa9ebccSVladimir Oltean {
5258aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
5268aa9ebccSVladimir Oltean 	struct sja1105_table *table;
5278aa9ebccSVladimir Oltean 	int i, j, k;
5288aa9ebccSVladimir Oltean 
5298aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
5308aa9ebccSVladimir Oltean 
5318aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
5328aa9ebccSVladimir Oltean 	if (table->entry_count) {
5338aa9ebccSVladimir Oltean 		kfree(table->entries);
5348aa9ebccSVladimir Oltean 		table->entry_count = 0;
5358aa9ebccSVladimir Oltean 	}
5368aa9ebccSVladimir Oltean 
5378aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
5388aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
5398aa9ebccSVladimir Oltean 	if (!table->entries)
5408aa9ebccSVladimir Oltean 		return -ENOMEM;
5418aa9ebccSVladimir Oltean 
5428aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
5438aa9ebccSVladimir Oltean 
5448aa9ebccSVladimir Oltean 	policing = table->entries;
5458aa9ebccSVladimir Oltean 
5468aa9ebccSVladimir Oltean 	/* k sweeps through all unicast policers (0-39).
5478aa9ebccSVladimir Oltean 	 * bcast sweeps through policers 40-44.
5488aa9ebccSVladimir Oltean 	 */
5498aa9ebccSVladimir Oltean 	for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) {
5508aa9ebccSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i;
5518aa9ebccSVladimir Oltean 
5528aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++, k++)
5538aa9ebccSVladimir Oltean 			sja1105_setup_policer(policing, k);
5548aa9ebccSVladimir Oltean 
5558aa9ebccSVladimir Oltean 		/* Set up this port's policer for broadcast traffic */
5568aa9ebccSVladimir Oltean 		sja1105_setup_policer(policing, bcast);
5578aa9ebccSVladimir Oltean 	}
5588aa9ebccSVladimir Oltean 	return 0;
5598aa9ebccSVladimir Oltean }
5608aa9ebccSVladimir Oltean 
5618aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
5628aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
5638aa9ebccSVladimir Oltean {
5648aa9ebccSVladimir Oltean 	int rc;
5658aa9ebccSVladimir Oltean 
5668aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
5678aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
5688aa9ebccSVladimir Oltean 					priv->info->static_ops,
5698aa9ebccSVladimir Oltean 					priv->info->device_id);
5708aa9ebccSVladimir Oltean 	if (rc)
5718aa9ebccSVladimir Oltean 		return rc;
5728aa9ebccSVladimir Oltean 
5738aa9ebccSVladimir Oltean 	/* Build static configuration */
5748aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
5758aa9ebccSVladimir Oltean 	if (rc < 0)
5768aa9ebccSVladimir Oltean 		return rc;
5778aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
5788aa9ebccSVladimir Oltean 	if (rc < 0)
5798aa9ebccSVladimir Oltean 		return rc;
5808aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
5818aa9ebccSVladimir Oltean 	if (rc < 0)
5828aa9ebccSVladimir Oltean 		return rc;
5838aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
5848aa9ebccSVladimir Oltean 	if (rc < 0)
5858aa9ebccSVladimir Oltean 		return rc;
5868aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
5878aa9ebccSVladimir Oltean 	if (rc < 0)
5888aa9ebccSVladimir Oltean 		return rc;
5898aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
5908aa9ebccSVladimir Oltean 	if (rc < 0)
5918aa9ebccSVladimir Oltean 		return rc;
5928aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
5938aa9ebccSVladimir Oltean 	if (rc < 0)
5948aa9ebccSVladimir Oltean 		return rc;
5958aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
5968aa9ebccSVladimir Oltean 	if (rc < 0)
5978aa9ebccSVladimir Oltean 		return rc;
5988aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
5998aa9ebccSVladimir Oltean 	if (rc < 0)
6008aa9ebccSVladimir Oltean 		return rc;
601*79d5511cSVladimir Oltean 	rc = sja1105_init_avb_params(priv);
602*79d5511cSVladimir Oltean 	if (rc < 0)
603*79d5511cSVladimir Oltean 		return rc;
6048aa9ebccSVladimir Oltean 
6058aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
6068aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
6078aa9ebccSVladimir Oltean }
6088aa9ebccSVladimir Oltean 
609f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
610f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
611f5b8631cSVladimir Oltean {
612f5b8631cSVladimir Oltean 	int i;
613f5b8631cSVladimir Oltean 
614f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
6159bca3a0aSOleksij Rempel 		if (ports[i].role == XMII_MAC)
616f5b8631cSVladimir Oltean 			continue;
617f5b8631cSVladimir Oltean 
6189bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
6199bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
620f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
621f5b8631cSVladimir Oltean 
6229bca3a0aSOleksij Rempel 		if (ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
6239bca3a0aSOleksij Rempel 		    ports[i].phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
624f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
625f5b8631cSVladimir Oltean 
626f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
627f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
628f5b8631cSVladimir Oltean 			return -EINVAL;
629f5b8631cSVladimir Oltean 	}
630f5b8631cSVladimir Oltean 	return 0;
631f5b8631cSVladimir Oltean }
632f5b8631cSVladimir Oltean 
6338aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
6348aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
6358aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
6368aa9ebccSVladimir Oltean {
6378aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6388aa9ebccSVladimir Oltean 	struct device_node *child;
6398aa9ebccSVladimir Oltean 
64027afe0d3SVladimir Oltean 	for_each_available_child_of_node(ports_node, child) {
6418aa9ebccSVladimir Oltean 		struct device_node *phy_node;
6420c65b2b9SAndrew Lunn 		phy_interface_t phy_mode;
6438aa9ebccSVladimir Oltean 		u32 index;
6440c65b2b9SAndrew Lunn 		int err;
6458aa9ebccSVladimir Oltean 
6468aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
6478aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
6488aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
6498aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
6507ba771e3SNishka Dasgupta 			of_node_put(child);
6518aa9ebccSVladimir Oltean 			return -ENODEV;
6528aa9ebccSVladimir Oltean 		}
6538aa9ebccSVladimir Oltean 
6548aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
6550c65b2b9SAndrew Lunn 		err = of_get_phy_mode(child, &phy_mode);
6560c65b2b9SAndrew Lunn 		if (err) {
6578aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
6588aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
6598aa9ebccSVladimir Oltean 				index);
6607ba771e3SNishka Dasgupta 			of_node_put(child);
6618aa9ebccSVladimir Oltean 			return -ENODEV;
6628aa9ebccSVladimir Oltean 		}
6638aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
6648aa9ebccSVladimir Oltean 
6658aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
6668aa9ebccSVladimir Oltean 		if (!phy_node) {
6678aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
6688aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
6698aa9ebccSVladimir Oltean 					"properties missing!\n");
6707ba771e3SNishka Dasgupta 				of_node_put(child);
6718aa9ebccSVladimir Oltean 				return -ENODEV;
6728aa9ebccSVladimir Oltean 			}
6738aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
6748aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
6758aa9ebccSVladimir Oltean 			 */
6768aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6778aa9ebccSVladimir Oltean 		} else {
6788aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
6798aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6808aa9ebccSVladimir Oltean 			of_node_put(phy_node);
6818aa9ebccSVladimir Oltean 		}
6828aa9ebccSVladimir Oltean 
6838aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
6848aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
6858aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6868aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
6878aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6888aa9ebccSVladimir Oltean 	}
6898aa9ebccSVladimir Oltean 
6908aa9ebccSVladimir Oltean 	return 0;
6918aa9ebccSVladimir Oltean }
6928aa9ebccSVladimir Oltean 
6938aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
6948aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
6958aa9ebccSVladimir Oltean {
6968aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6978aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
6988aa9ebccSVladimir Oltean 	struct device_node *ports_node;
6998aa9ebccSVladimir Oltean 	int rc;
7008aa9ebccSVladimir Oltean 
7018aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
7028aa9ebccSVladimir Oltean 	if (!ports_node) {
7038aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
7048aa9ebccSVladimir Oltean 		return -ENODEV;
7058aa9ebccSVladimir Oltean 	}
7068aa9ebccSVladimir Oltean 
7078aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
7088aa9ebccSVladimir Oltean 	of_node_put(ports_node);
7098aa9ebccSVladimir Oltean 
7108aa9ebccSVladimir Oltean 	return rc;
7118aa9ebccSVladimir Oltean }
7128aa9ebccSVladimir Oltean 
713ffe10e67SVladimir Oltean static int sja1105_sgmii_read(struct sja1105_private *priv, int pcs_reg)
714ffe10e67SVladimir Oltean {
715ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
716ffe10e67SVladimir Oltean 	u32 val;
717ffe10e67SVladimir Oltean 	int rc;
718ffe10e67SVladimir Oltean 
719ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->sgmii + pcs_reg, &val,
720ffe10e67SVladimir Oltean 			      NULL);
721ffe10e67SVladimir Oltean 	if (rc < 0)
722ffe10e67SVladimir Oltean 		return rc;
723ffe10e67SVladimir Oltean 
724ffe10e67SVladimir Oltean 	return val;
725ffe10e67SVladimir Oltean }
726ffe10e67SVladimir Oltean 
727ffe10e67SVladimir Oltean static int sja1105_sgmii_write(struct sja1105_private *priv, int pcs_reg,
728ffe10e67SVladimir Oltean 			       u16 pcs_val)
729ffe10e67SVladimir Oltean {
730ffe10e67SVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
731ffe10e67SVladimir Oltean 	u32 val = pcs_val;
732ffe10e67SVladimir Oltean 	int rc;
733ffe10e67SVladimir Oltean 
734ffe10e67SVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_WRITE, regs->sgmii + pcs_reg, &val,
735ffe10e67SVladimir Oltean 			      NULL);
736ffe10e67SVladimir Oltean 	if (rc < 0)
737ffe10e67SVladimir Oltean 		return rc;
738ffe10e67SVladimir Oltean 
739ffe10e67SVladimir Oltean 	return val;
740ffe10e67SVladimir Oltean }
741ffe10e67SVladimir Oltean 
742ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_config(struct sja1105_private *priv,
743ffe10e67SVladimir Oltean 				     bool an_enabled, bool an_master)
744ffe10e67SVladimir Oltean {
745ffe10e67SVladimir Oltean 	u16 ac = SJA1105_AC_AUTONEG_MODE_SGMII;
746ffe10e67SVladimir Oltean 
747ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_1: Enable vendor-specific MMD1, allow the PHY to
748ffe10e67SVladimir Oltean 	 * stop the clock during LPI mode, make the MAC reconfigure
749ffe10e67SVladimir Oltean 	 * autonomously after PCS autoneg is done, flush the internal FIFOs.
750ffe10e67SVladimir Oltean 	 */
751ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC1, SJA1105_DC1_EN_VSMMD1 |
752ffe10e67SVladimir Oltean 					       SJA1105_DC1_CLOCK_STOP_EN |
753ffe10e67SVladimir Oltean 					       SJA1105_DC1_MAC_AUTO_SW |
754ffe10e67SVladimir Oltean 					       SJA1105_DC1_INIT);
755ffe10e67SVladimir Oltean 	/* DIGITAL_CONTROL_2: No polarity inversion for TX and RX lanes */
756ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_DC2, SJA1105_DC2_TX_POL_INV_DISABLE);
757ffe10e67SVladimir Oltean 	/* AUTONEG_CONTROL: Use SGMII autoneg */
758ffe10e67SVladimir Oltean 	if (an_master)
759ffe10e67SVladimir Oltean 		ac |= SJA1105_AC_PHY_MODE | SJA1105_AC_SGMII_LINK;
760ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, SJA1105_AC, ac);
761ffe10e67SVladimir Oltean 	/* BASIC_CONTROL: enable in-band AN now, if requested. Otherwise,
762ffe10e67SVladimir Oltean 	 * sja1105_sgmii_pcs_force_speed must be called later for the link
763ffe10e67SVladimir Oltean 	 * to become operational.
764ffe10e67SVladimir Oltean 	 */
765ffe10e67SVladimir Oltean 	if (an_enabled)
766ffe10e67SVladimir Oltean 		sja1105_sgmii_write(priv, MII_BMCR,
767ffe10e67SVladimir Oltean 				    BMCR_ANENABLE | BMCR_ANRESTART);
768ffe10e67SVladimir Oltean }
769ffe10e67SVladimir Oltean 
770ffe10e67SVladimir Oltean static void sja1105_sgmii_pcs_force_speed(struct sja1105_private *priv,
771ffe10e67SVladimir Oltean 					  int speed)
772ffe10e67SVladimir Oltean {
773ffe10e67SVladimir Oltean 	int pcs_speed;
774ffe10e67SVladimir Oltean 
775ffe10e67SVladimir Oltean 	switch (speed) {
776ffe10e67SVladimir Oltean 	case SPEED_1000:
777ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED1000;
778ffe10e67SVladimir Oltean 		break;
779ffe10e67SVladimir Oltean 	case SPEED_100:
780ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED100;
781ffe10e67SVladimir Oltean 		break;
782ffe10e67SVladimir Oltean 	case SPEED_10:
783ffe10e67SVladimir Oltean 		pcs_speed = BMCR_SPEED10;
784ffe10e67SVladimir Oltean 		break;
785ffe10e67SVladimir Oltean 	default:
786ffe10e67SVladimir Oltean 		dev_err(priv->ds->dev, "Invalid speed %d\n", speed);
787ffe10e67SVladimir Oltean 		return;
788ffe10e67SVladimir Oltean 	}
789ffe10e67SVladimir Oltean 	sja1105_sgmii_write(priv, MII_BMCR, pcs_speed | BMCR_FULLDPLX);
790ffe10e67SVladimir Oltean }
791ffe10e67SVladimir Oltean 
792c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
7938aa9ebccSVladimir Oltean static int sja1105_speed[] = {
794c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
795c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
796c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
797c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
7988aa9ebccSVladimir Oltean };
7998aa9ebccSVladimir Oltean 
8008400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
8018aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
8028400cff6SVladimir Oltean 				      int speed_mbps)
8038aa9ebccSVladimir Oltean {
8048aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
8058aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
8068aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
8078aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
8088aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
8098aa9ebccSVladimir Oltean 	int rc;
8108aa9ebccSVladimir Oltean 
8118400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
8128400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
8138400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
8148400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
8158400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
8168400cff6SVladimir Oltean 	 */
8178aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
8188400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
8198aa9ebccSVladimir Oltean 
820f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
821c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
822a979a0abSVladimir Oltean 		/* PHYLINK called sja1105_mac_config() to inform us about
823a979a0abSVladimir Oltean 		 * the state->interface, but AN has not completed and the
824a979a0abSVladimir Oltean 		 * speed is not yet valid. UM10944.pdf says that setting
825a979a0abSVladimir Oltean 		 * SJA1105_SPEED_AUTO at runtime disables the port, so that is
826a979a0abSVladimir Oltean 		 * ok for power consumption in case AN will never complete -
827a979a0abSVladimir Oltean 		 * otherwise PHYLINK should come back with a new update.
828a979a0abSVladimir Oltean 		 */
829f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
830f4cfcfbdSVladimir Oltean 		break;
831c44d0535SVladimir Oltean 	case SPEED_10:
832f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
833f4cfcfbdSVladimir Oltean 		break;
834c44d0535SVladimir Oltean 	case SPEED_100:
835f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
836f4cfcfbdSVladimir Oltean 		break;
837c44d0535SVladimir Oltean 	case SPEED_1000:
838f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
839f4cfcfbdSVladimir Oltean 		break;
840f4cfcfbdSVladimir Oltean 	default:
8418aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
8428aa9ebccSVladimir Oltean 		return -EINVAL;
8438aa9ebccSVladimir Oltean 	}
8448aa9ebccSVladimir Oltean 
8458400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
8468400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
8478400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
8488400cff6SVladimir Oltean 	 * we want auto during upload phase).
849ffe10e67SVladimir Oltean 	 * Actually for the SGMII port, the MAC is fixed at 1 Gbps and
850ffe10e67SVladimir Oltean 	 * we need to configure the PCS only (if even that).
8518aa9ebccSVladimir Oltean 	 */
852ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port))
853ffe10e67SVladimir Oltean 		mac[port].speed = SJA1105_SPEED_1000MBPS;
854ffe10e67SVladimir Oltean 	else
8558aa9ebccSVladimir Oltean 		mac[port].speed = speed;
8568aa9ebccSVladimir Oltean 
8578aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
8588400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
8598400cff6SVladimir Oltean 					  &mac[port], true);
8608aa9ebccSVladimir Oltean 	if (rc < 0) {
8618aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
8628aa9ebccSVladimir Oltean 		return rc;
8638aa9ebccSVladimir Oltean 	}
8648aa9ebccSVladimir Oltean 
8658aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
8668aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
8678aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
8688aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
8698aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
8708aa9ebccSVladimir Oltean 	 */
8718aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
8728aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
8738aa9ebccSVladimir Oltean 		return 0;
8748aa9ebccSVladimir Oltean 
8758aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
8768aa9ebccSVladimir Oltean }
8778aa9ebccSVladimir Oltean 
87839710229SVladimir Oltean /* The SJA1105 MAC programming model is through the static config (the xMII
87939710229SVladimir Oltean  * Mode table cannot be dynamically reconfigured), and we have to program
88039710229SVladimir Oltean  * that early (earlier than PHYLINK calls us, anyway).
88139710229SVladimir Oltean  * So just error out in case the connected PHY attempts to change the initial
88239710229SVladimir Oltean  * system interface MII protocol from what is defined in the DT, at least for
88339710229SVladimir Oltean  * now.
88439710229SVladimir Oltean  */
88539710229SVladimir Oltean static bool sja1105_phy_mode_mismatch(struct sja1105_private *priv, int port,
88639710229SVladimir Oltean 				      phy_interface_t interface)
88739710229SVladimir Oltean {
88839710229SVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
88939710229SVladimir Oltean 	sja1105_phy_interface_t phy_mode;
89039710229SVladimir Oltean 
89139710229SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
89239710229SVladimir Oltean 	phy_mode = mii->xmii_mode[port];
89339710229SVladimir Oltean 
89439710229SVladimir Oltean 	switch (interface) {
89539710229SVladimir Oltean 	case PHY_INTERFACE_MODE_MII:
89639710229SVladimir Oltean 		return (phy_mode != XMII_MODE_MII);
89739710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RMII:
89839710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RMII);
89939710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII:
90039710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_ID:
90139710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_RXID:
90239710229SVladimir Oltean 	case PHY_INTERFACE_MODE_RGMII_TXID:
90339710229SVladimir Oltean 		return (phy_mode != XMII_MODE_RGMII);
904ffe10e67SVladimir Oltean 	case PHY_INTERFACE_MODE_SGMII:
905ffe10e67SVladimir Oltean 		return (phy_mode != XMII_MODE_SGMII);
90639710229SVladimir Oltean 	default:
90739710229SVladimir Oltean 		return true;
90839710229SVladimir Oltean 	}
90939710229SVladimir Oltean }
91039710229SVladimir Oltean 
911af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
912ffe10e67SVladimir Oltean 			       unsigned int mode,
913af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
9148aa9ebccSVladimir Oltean {
9158aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
916ffe10e67SVladimir Oltean 	bool is_sgmii = sja1105_supports_sgmii(priv, port);
9178aa9ebccSVladimir Oltean 
918ec8582d1SVladimir Oltean 	if (sja1105_phy_mode_mismatch(priv, port, state->interface)) {
919ec8582d1SVladimir Oltean 		dev_err(ds->dev, "Changing PHY mode to %s not supported!\n",
920ec8582d1SVladimir Oltean 			phy_modes(state->interface));
92139710229SVladimir Oltean 		return;
922ec8582d1SVladimir Oltean 	}
92339710229SVladimir Oltean 
924ffe10e67SVladimir Oltean 	if (phylink_autoneg_inband(mode) && !is_sgmii) {
9259f971573SVladimir Oltean 		dev_err(ds->dev, "In-band AN not supported!\n");
9269f971573SVladimir Oltean 		return;
9279f971573SVladimir Oltean 	}
928ffe10e67SVladimir Oltean 
929ffe10e67SVladimir Oltean 	if (is_sgmii)
930ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, phylink_autoneg_inband(mode),
931ffe10e67SVladimir Oltean 					 false);
9328400cff6SVladimir Oltean }
9338400cff6SVladimir Oltean 
9348400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
9358400cff6SVladimir Oltean 				  unsigned int mode,
9368400cff6SVladimir Oltean 				  phy_interface_t interface)
9378400cff6SVladimir Oltean {
9388400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
9398400cff6SVladimir Oltean }
9408400cff6SVladimir Oltean 
9418400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
9428400cff6SVladimir Oltean 				unsigned int mode,
9438400cff6SVladimir Oltean 				phy_interface_t interface,
9445b502a7bSRussell King 				struct phy_device *phydev,
9455b502a7bSRussell King 				int speed, int duplex,
9465b502a7bSRussell King 				bool tx_pause, bool rx_pause)
9478400cff6SVladimir Oltean {
948ec8582d1SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
949ec8582d1SVladimir Oltean 
950ec8582d1SVladimir Oltean 	sja1105_adjust_port_config(priv, port, speed);
951ec8582d1SVladimir Oltean 
952ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, port) && !phylink_autoneg_inband(mode))
953ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_force_speed(priv, speed);
954ffe10e67SVladimir Oltean 
955ec8582d1SVladimir Oltean 	sja1105_inhibit_tx(priv, BIT(port), false);
9568aa9ebccSVladimir Oltean }
9578aa9ebccSVladimir Oltean 
958ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
959ad9f299aSVladimir Oltean 				     unsigned long *supported,
960ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
961ad9f299aSVladimir Oltean {
962ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
963ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
964ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
965ad9f299aSVladimir Oltean 	 */
966ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
967ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
968ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
969ad9f299aSVladimir Oltean 
970ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
971ad9f299aSVladimir Oltean 
97239710229SVladimir Oltean 	/* include/linux/phylink.h says:
97339710229SVladimir Oltean 	 *     When @state->interface is %PHY_INTERFACE_MODE_NA, phylink
97439710229SVladimir Oltean 	 *     expects the MAC driver to return all supported link modes.
97539710229SVladimir Oltean 	 */
97639710229SVladimir Oltean 	if (state->interface != PHY_INTERFACE_MODE_NA &&
97739710229SVladimir Oltean 	    sja1105_phy_mode_mismatch(priv, port, state->interface)) {
97839710229SVladimir Oltean 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
97939710229SVladimir Oltean 		return;
98039710229SVladimir Oltean 	}
98139710229SVladimir Oltean 
982ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
983ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
984ad9f299aSVladimir Oltean 	 */
985ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
986ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
987ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
988ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
989ca68e138SOleksij Rempel 	phylink_set(mask, 100baseT1_Full);
990ffe10e67SVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII ||
991ffe10e67SVladimir Oltean 	    mii->xmii_mode[port] == XMII_MODE_SGMII)
992ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
993ad9f299aSVladimir Oltean 
994ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
995ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
996ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
997ad9f299aSVladimir Oltean }
998ad9f299aSVladimir Oltean 
999ffe10e67SVladimir Oltean static int sja1105_mac_pcs_get_state(struct dsa_switch *ds, int port,
1000ffe10e67SVladimir Oltean 				     struct phylink_link_state *state)
1001ffe10e67SVladimir Oltean {
1002ffe10e67SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1003ffe10e67SVladimir Oltean 	int ais;
1004ffe10e67SVladimir Oltean 
1005ffe10e67SVladimir Oltean 	/* Read the vendor-specific AUTONEG_INTR_STATUS register */
1006ffe10e67SVladimir Oltean 	ais = sja1105_sgmii_read(priv, SJA1105_AIS);
1007ffe10e67SVladimir Oltean 	if (ais < 0)
1008ffe10e67SVladimir Oltean 		return ais;
1009ffe10e67SVladimir Oltean 
1010ffe10e67SVladimir Oltean 	switch (SJA1105_AIS_SPEED(ais)) {
1011ffe10e67SVladimir Oltean 	case 0:
1012ffe10e67SVladimir Oltean 		state->speed = SPEED_10;
1013ffe10e67SVladimir Oltean 		break;
1014ffe10e67SVladimir Oltean 	case 1:
1015ffe10e67SVladimir Oltean 		state->speed = SPEED_100;
1016ffe10e67SVladimir Oltean 		break;
1017ffe10e67SVladimir Oltean 	case 2:
1018ffe10e67SVladimir Oltean 		state->speed = SPEED_1000;
1019ffe10e67SVladimir Oltean 		break;
1020ffe10e67SVladimir Oltean 	default:
1021ffe10e67SVladimir Oltean 		dev_err(ds->dev, "Invalid SGMII PCS speed %lu\n",
1022ffe10e67SVladimir Oltean 			SJA1105_AIS_SPEED(ais));
1023ffe10e67SVladimir Oltean 	}
1024ffe10e67SVladimir Oltean 	state->duplex = SJA1105_AIS_DUPLEX_MODE(ais);
1025ffe10e67SVladimir Oltean 	state->an_complete = SJA1105_AIS_COMPLETE(ais);
1026ffe10e67SVladimir Oltean 	state->link = SJA1105_AIS_LINK_STATUS(ais);
1027ffe10e67SVladimir Oltean 
1028ffe10e67SVladimir Oltean 	return 0;
1029ffe10e67SVladimir Oltean }
1030ffe10e67SVladimir Oltean 
103160f6053fSVladimir Oltean static int
103260f6053fSVladimir Oltean sja1105_find_static_fdb_entry(struct sja1105_private *priv, int port,
103360f6053fSVladimir Oltean 			      const struct sja1105_l2_lookup_entry *requested)
103460f6053fSVladimir Oltean {
103560f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
103660f6053fSVladimir Oltean 	struct sja1105_table *table;
103760f6053fSVladimir Oltean 	int i;
103860f6053fSVladimir Oltean 
103960f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
104060f6053fSVladimir Oltean 	l2_lookup = table->entries;
104160f6053fSVladimir Oltean 
104260f6053fSVladimir Oltean 	for (i = 0; i < table->entry_count; i++)
104360f6053fSVladimir Oltean 		if (l2_lookup[i].macaddr == requested->macaddr &&
104460f6053fSVladimir Oltean 		    l2_lookup[i].vlanid == requested->vlanid &&
104560f6053fSVladimir Oltean 		    l2_lookup[i].destports & BIT(port))
104660f6053fSVladimir Oltean 			return i;
104760f6053fSVladimir Oltean 
104860f6053fSVladimir Oltean 	return -1;
104960f6053fSVladimir Oltean }
105060f6053fSVladimir Oltean 
105160f6053fSVladimir Oltean /* We want FDB entries added statically through the bridge command to persist
105260f6053fSVladimir Oltean  * across switch resets, which are a common thing during normal SJA1105
105360f6053fSVladimir Oltean  * operation. So we have to back them up in the static configuration tables
105460f6053fSVladimir Oltean  * and hence apply them on next static config upload... yay!
105560f6053fSVladimir Oltean  */
105660f6053fSVladimir Oltean static int
105760f6053fSVladimir Oltean sja1105_static_fdb_change(struct sja1105_private *priv, int port,
105860f6053fSVladimir Oltean 			  const struct sja1105_l2_lookup_entry *requested,
105960f6053fSVladimir Oltean 			  bool keep)
106060f6053fSVladimir Oltean {
106160f6053fSVladimir Oltean 	struct sja1105_l2_lookup_entry *l2_lookup;
106260f6053fSVladimir Oltean 	struct sja1105_table *table;
106360f6053fSVladimir Oltean 	int rc, match;
106460f6053fSVladimir Oltean 
106560f6053fSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
106660f6053fSVladimir Oltean 
106760f6053fSVladimir Oltean 	match = sja1105_find_static_fdb_entry(priv, port, requested);
106860f6053fSVladimir Oltean 	if (match < 0) {
106960f6053fSVladimir Oltean 		/* Can't delete a missing entry. */
107060f6053fSVladimir Oltean 		if (!keep)
107160f6053fSVladimir Oltean 			return 0;
107260f6053fSVladimir Oltean 
107360f6053fSVladimir Oltean 		/* No match => new entry */
107460f6053fSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
107560f6053fSVladimir Oltean 		if (rc)
107660f6053fSVladimir Oltean 			return rc;
107760f6053fSVladimir Oltean 
107860f6053fSVladimir Oltean 		match = table->entry_count - 1;
107960f6053fSVladimir Oltean 	}
108060f6053fSVladimir Oltean 
108160f6053fSVladimir Oltean 	/* Assign pointer after the resize (it may be new memory) */
108260f6053fSVladimir Oltean 	l2_lookup = table->entries;
108360f6053fSVladimir Oltean 
108460f6053fSVladimir Oltean 	/* We have a match.
108560f6053fSVladimir Oltean 	 * If the job was to add this FDB entry, it's already done (mostly
108660f6053fSVladimir Oltean 	 * anyway, since the port forwarding mask may have changed, case in
108760f6053fSVladimir Oltean 	 * which we update it).
108860f6053fSVladimir Oltean 	 * Otherwise we have to delete it.
108960f6053fSVladimir Oltean 	 */
109060f6053fSVladimir Oltean 	if (keep) {
109160f6053fSVladimir Oltean 		l2_lookup[match] = *requested;
109260f6053fSVladimir Oltean 		return 0;
109360f6053fSVladimir Oltean 	}
109460f6053fSVladimir Oltean 
109560f6053fSVladimir Oltean 	/* To remove, the strategy is to overwrite the element with
109660f6053fSVladimir Oltean 	 * the last one, and then reduce the array size by 1
109760f6053fSVladimir Oltean 	 */
109860f6053fSVladimir Oltean 	l2_lookup[match] = l2_lookup[table->entry_count - 1];
109960f6053fSVladimir Oltean 	return sja1105_table_resize(table, table->entry_count - 1);
110060f6053fSVladimir Oltean }
110160f6053fSVladimir Oltean 
1102291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
1103291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
1104291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
1105291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
1106291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
1107291d1e72SVladimir Oltean  */
110809c1b412SVladimir Oltean static int sja1105et_fdb_index(int bin, int way)
1109291d1e72SVladimir Oltean {
1110291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
1111291d1e72SVladimir Oltean }
1112291d1e72SVladimir Oltean 
11139dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
1114291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
1115291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
1116291d1e72SVladimir Oltean 					 int *last_unused)
1117291d1e72SVladimir Oltean {
1118291d1e72SVladimir Oltean 	int way;
1119291d1e72SVladimir Oltean 
1120291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
1121291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1122291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1123291d1e72SVladimir Oltean 
1124291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
1125291d1e72SVladimir Oltean 		 * into the return value
1126291d1e72SVladimir Oltean 		 */
1127291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1128291d1e72SVladimir Oltean 						index, &l2_lookup)) {
1129291d1e72SVladimir Oltean 			if (last_unused)
1130291d1e72SVladimir Oltean 				*last_unused = way;
1131291d1e72SVladimir Oltean 			continue;
1132291d1e72SVladimir Oltean 		}
1133291d1e72SVladimir Oltean 
1134291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
1135291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
1136291d1e72SVladimir Oltean 			if (match)
1137291d1e72SVladimir Oltean 				*match = l2_lookup;
1138291d1e72SVladimir Oltean 			return way;
1139291d1e72SVladimir Oltean 		}
1140291d1e72SVladimir Oltean 	}
1141291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
1142291d1e72SVladimir Oltean 	return -1;
1143291d1e72SVladimir Oltean }
1144291d1e72SVladimir Oltean 
11459dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
1146291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1147291d1e72SVladimir Oltean {
1148291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1149291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1150291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1151291d1e72SVladimir Oltean 	int last_unused = -1;
115260f6053fSVladimir Oltean 	int bin, way, rc;
1153291d1e72SVladimir Oltean 
11549dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
1155291d1e72SVladimir Oltean 
11569dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1157291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
1158291d1e72SVladimir Oltean 	if (way >= 0) {
1159291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
1160291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
1161291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
1162291d1e72SVladimir Oltean 		 */
1163291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
1164291d1e72SVladimir Oltean 			return 0;
1165291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
1166291d1e72SVladimir Oltean 	} else {
1167291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
1168291d1e72SVladimir Oltean 
1169291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
1170291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
1171291d1e72SVladimir Oltean 		 */
1172291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
1173291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
1174291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
1175291d1e72SVladimir Oltean 
1176291d1e72SVladimir Oltean 		if (last_unused >= 0) {
1177291d1e72SVladimir Oltean 			way = last_unused;
1178291d1e72SVladimir Oltean 		} else {
1179291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
1180291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
1181291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
1182291d1e72SVladimir Oltean 			 * distribution function:
1183291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
1184291d1e72SVladimir Oltean 			 */
1185291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
1186291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
1187291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
1188291d1e72SVladimir Oltean 				 bin, addr, way);
1189291d1e72SVladimir Oltean 			/* Evict entry */
1190291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1191291d1e72SVladimir Oltean 						     index, NULL, false);
1192291d1e72SVladimir Oltean 		}
1193291d1e72SVladimir Oltean 	}
1194291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
1195291d1e72SVladimir Oltean 
119660f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1197291d1e72SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
1198291d1e72SVladimir Oltean 					  true);
119960f6053fSVladimir Oltean 	if (rc < 0)
120060f6053fSVladimir Oltean 		return rc;
120160f6053fSVladimir Oltean 
120260f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
1203291d1e72SVladimir Oltean }
1204291d1e72SVladimir Oltean 
12059dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
1206291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
1207291d1e72SVladimir Oltean {
1208291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
1209291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
121060f6053fSVladimir Oltean 	int index, bin, way, rc;
1211291d1e72SVladimir Oltean 	bool keep;
1212291d1e72SVladimir Oltean 
12139dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
12149dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
1215291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
1216291d1e72SVladimir Oltean 	if (way < 0)
1217291d1e72SVladimir Oltean 		return 0;
1218291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
1219291d1e72SVladimir Oltean 
1220291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
1221291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
1222291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
1223291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
1224291d1e72SVladimir Oltean 	 */
1225291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
12267752e937SVladimir Oltean 
1227291d1e72SVladimir Oltean 	if (l2_lookup.destports)
1228291d1e72SVladimir Oltean 		keep = true;
1229291d1e72SVladimir Oltean 	else
1230291d1e72SVladimir Oltean 		keep = false;
1231291d1e72SVladimir Oltean 
123260f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
1233291d1e72SVladimir Oltean 					  index, &l2_lookup, keep);
123460f6053fSVladimir Oltean 	if (rc < 0)
123560f6053fSVladimir Oltean 		return rc;
123660f6053fSVladimir Oltean 
123760f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
1238291d1e72SVladimir Oltean }
1239291d1e72SVladimir Oltean 
12409dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
12419dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
12429dfa6911SVladimir Oltean {
12431da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
12441da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12451da73821SVladimir Oltean 	int rc, i;
12461da73821SVladimir Oltean 
12471da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
12481da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
12491da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
12501da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
12511da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
125268bb8ea8SVivien Didelot 	if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
12531da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
12541da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
12556d7c7d94SVladimir Oltean 	} else {
12566d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
12576d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
12586d7c7d94SVladimir Oltean 	}
12591da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
12601da73821SVladimir Oltean 
12611da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
12621da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
12631da73821SVladimir Oltean 	if (rc == 0) {
12641da73821SVladimir Oltean 		/* Found and this port is already in the entry's
12651da73821SVladimir Oltean 		 * port mask => job done
12661da73821SVladimir Oltean 		 */
12671da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
12681da73821SVladimir Oltean 			return 0;
12691da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
12701da73821SVladimir Oltean 		 * found something.
12711da73821SVladimir Oltean 		 */
12721da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
12731da73821SVladimir Oltean 		goto skip_finding_an_index;
12741da73821SVladimir Oltean 	}
12751da73821SVladimir Oltean 
12761da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
12771da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
12781da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
12791da73821SVladimir Oltean 	 */
12801da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
12811da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
12821da73821SVladimir Oltean 						 i, NULL);
12831da73821SVladimir Oltean 		if (rc < 0)
12841da73821SVladimir Oltean 			break;
12851da73821SVladimir Oltean 	}
12861da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
12871da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
12881da73821SVladimir Oltean 		return -EINVAL;
12891da73821SVladimir Oltean 	}
129017ae6555SVladimir Oltean 	l2_lookup.lockeds = true;
12911da73821SVladimir Oltean 	l2_lookup.index = i;
12921da73821SVladimir Oltean 
12931da73821SVladimir Oltean skip_finding_an_index:
129460f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
12951da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup,
12961da73821SVladimir Oltean 					  true);
129760f6053fSVladimir Oltean 	if (rc < 0)
129860f6053fSVladimir Oltean 		return rc;
129960f6053fSVladimir Oltean 
130060f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, true);
13019dfa6911SVladimir Oltean }
13029dfa6911SVladimir Oltean 
13039dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
13049dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
13059dfa6911SVladimir Oltean {
13061da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
13071da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13081da73821SVladimir Oltean 	bool keep;
13091da73821SVladimir Oltean 	int rc;
13101da73821SVladimir Oltean 
13111da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
13121da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
13131da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
13141da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
131568bb8ea8SVivien Didelot 	if (dsa_port_is_vlan_filtering(dsa_to_port(ds, port))) {
13161da73821SVladimir Oltean 		l2_lookup.mask_vlanid = VLAN_VID_MASK;
13171da73821SVladimir Oltean 		l2_lookup.mask_iotag = BIT(0);
13186d7c7d94SVladimir Oltean 	} else {
13196d7c7d94SVladimir Oltean 		l2_lookup.mask_vlanid = 0;
13206d7c7d94SVladimir Oltean 		l2_lookup.mask_iotag = 0;
13216d7c7d94SVladimir Oltean 	}
13221da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
13231da73821SVladimir Oltean 
13241da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
13251da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
13261da73821SVladimir Oltean 	if (rc < 0)
13271da73821SVladimir Oltean 		return 0;
13281da73821SVladimir Oltean 
13291da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
13301da73821SVladimir Oltean 
13311da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
13321da73821SVladimir Oltean 	 * or if we remove it completely.
13331da73821SVladimir Oltean 	 */
13341da73821SVladimir Oltean 	if (l2_lookup.destports)
13351da73821SVladimir Oltean 		keep = true;
13361da73821SVladimir Oltean 	else
13371da73821SVladimir Oltean 		keep = false;
13381da73821SVladimir Oltean 
133960f6053fSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
13401da73821SVladimir Oltean 					  l2_lookup.index, &l2_lookup, keep);
134160f6053fSVladimir Oltean 	if (rc < 0)
134260f6053fSVladimir Oltean 		return rc;
134360f6053fSVladimir Oltean 
134460f6053fSVladimir Oltean 	return sja1105_static_fdb_change(priv, port, &l2_lookup, keep);
13459dfa6911SVladimir Oltean }
13469dfa6911SVladimir Oltean 
13479dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
13489dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
13499dfa6911SVladimir Oltean {
13509dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1351b3ee526aSVladimir Oltean 
13526d7c7d94SVladimir Oltean 	/* dsa_8021q is in effect when the bridge's vlan_filtering isn't,
13536d7c7d94SVladimir Oltean 	 * so the switch still does some VLAN processing internally.
13546d7c7d94SVladimir Oltean 	 * But Shared VLAN Learning (SVL) is also active, and it will take
13556d7c7d94SVladimir Oltean 	 * care of autonomous forwarding between the unique pvid's of each
13566d7c7d94SVladimir Oltean 	 * port.  Here we just make sure that users can't add duplicate FDB
13576d7c7d94SVladimir Oltean 	 * entries when in this mode - the actual VID doesn't matter except
13586d7c7d94SVladimir Oltean 	 * for what gets printed in 'bridge fdb show'.  In the case of zero,
13596d7c7d94SVladimir Oltean 	 * no VID gets printed at all.
136093647594SVladimir Oltean 	 */
136168bb8ea8SVivien Didelot 	if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
13626d7c7d94SVladimir Oltean 		vid = 0;
136393647594SVladimir Oltean 
13646d7c7d94SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
13659dfa6911SVladimir Oltean }
13669dfa6911SVladimir Oltean 
13679dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
13689dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
13699dfa6911SVladimir Oltean {
13709dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
13719dfa6911SVladimir Oltean 
137268bb8ea8SVivien Didelot 	if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
13736d7c7d94SVladimir Oltean 		vid = 0;
13746d7c7d94SVladimir Oltean 
1375b3ee526aSVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
13769dfa6911SVladimir Oltean }
13779dfa6911SVladimir Oltean 
1378291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1379291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1380291d1e72SVladimir Oltean {
1381291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1382291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1383291d1e72SVladimir Oltean 	int i;
1384291d1e72SVladimir Oltean 
1385291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1386291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1387291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1388291d1e72SVladimir Oltean 		int rc;
1389291d1e72SVladimir Oltean 
1390291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1391291d1e72SVladimir Oltean 						 i, &l2_lookup);
1392291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1393def84604SVladimir Oltean 		if (rc == -ENOENT)
1394291d1e72SVladimir Oltean 			continue;
1395291d1e72SVladimir Oltean 		if (rc) {
1396291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1397291d1e72SVladimir Oltean 			return rc;
1398291d1e72SVladimir Oltean 		}
1399291d1e72SVladimir Oltean 
1400291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1401291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1402291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1403291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1404291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1405291d1e72SVladimir Oltean 		 */
1406291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1407291d1e72SVladimir Oltean 			continue;
1408291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
140993647594SVladimir Oltean 
14106d7c7d94SVladimir Oltean 		/* We need to hide the dsa_8021q VLANs from the user. */
141168bb8ea8SVivien Didelot 		if (!dsa_port_is_vlan_filtering(dsa_to_port(ds, port)))
14126d7c7d94SVladimir Oltean 			l2_lookup.vlanid = 0;
141317ae6555SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, l2_lookup.lockeds, data);
1414291d1e72SVladimir Oltean 	}
1415291d1e72SVladimir Oltean 	return 0;
1416291d1e72SVladimir Oltean }
1417291d1e72SVladimir Oltean 
1418291d1e72SVladimir Oltean /* This callback needs to be present */
1419291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1420291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1421291d1e72SVladimir Oltean {
1422291d1e72SVladimir Oltean 	return 0;
1423291d1e72SVladimir Oltean }
1424291d1e72SVladimir Oltean 
1425291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1426291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1427291d1e72SVladimir Oltean {
1428291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1429291d1e72SVladimir Oltean }
1430291d1e72SVladimir Oltean 
1431291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1432291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1433291d1e72SVladimir Oltean {
1434291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1435291d1e72SVladimir Oltean }
1436291d1e72SVladimir Oltean 
14378aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
14388aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
14398aa9ebccSVladimir Oltean {
14408aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
14418aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14428aa9ebccSVladimir Oltean 	int i, rc;
14438aa9ebccSVladimir Oltean 
14448aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
14458aa9ebccSVladimir Oltean 
14468aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
14478aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
14488aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
14498aa9ebccSVladimir Oltean 		 */
14508aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
14518aa9ebccSVladimir Oltean 			continue;
14528aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
14538aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
14548aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
14558aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
14568aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
14578aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
14588aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
14598aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
14608aa9ebccSVladimir Oltean 		 */
14618aa9ebccSVladimir Oltean 		if (i == port)
14628aa9ebccSVladimir Oltean 			continue;
14638aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
14648aa9ebccSVladimir Oltean 			continue;
14658aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
14668aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
14678aa9ebccSVladimir Oltean 
14688aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
14698aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
14708aa9ebccSVladimir Oltean 		if (rc < 0)
14718aa9ebccSVladimir Oltean 			return rc;
14728aa9ebccSVladimir Oltean 	}
14738aa9ebccSVladimir Oltean 
14748aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
14758aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
14768aa9ebccSVladimir Oltean }
14778aa9ebccSVladimir Oltean 
1478640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1479640f763fSVladimir Oltean 					 u8 state)
1480640f763fSVladimir Oltean {
1481640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1482640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1483640f763fSVladimir Oltean 
1484640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1485640f763fSVladimir Oltean 
1486640f763fSVladimir Oltean 	switch (state) {
1487640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1488640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1489640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1490640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1491640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1492640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1493640f763fSVladimir Oltean 		 */
1494640f763fSVladimir Oltean 		mac[port].ingress   = false;
1495640f763fSVladimir Oltean 		mac[port].egress    = false;
1496640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1497640f763fSVladimir Oltean 		break;
1498640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1499640f763fSVladimir Oltean 		mac[port].ingress   = true;
1500640f763fSVladimir Oltean 		mac[port].egress    = false;
1501640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1502640f763fSVladimir Oltean 		break;
1503640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1504640f763fSVladimir Oltean 		mac[port].ingress   = true;
1505640f763fSVladimir Oltean 		mac[port].egress    = false;
1506640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1507640f763fSVladimir Oltean 		break;
1508640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1509640f763fSVladimir Oltean 		mac[port].ingress   = true;
1510640f763fSVladimir Oltean 		mac[port].egress    = true;
1511640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1512640f763fSVladimir Oltean 		break;
1513640f763fSVladimir Oltean 	default:
1514640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1515640f763fSVladimir Oltean 		return;
1516640f763fSVladimir Oltean 	}
1517640f763fSVladimir Oltean 
1518640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1519640f763fSVladimir Oltean 				     &mac[port], true);
1520640f763fSVladimir Oltean }
1521640f763fSVladimir Oltean 
15228aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
15238aa9ebccSVladimir Oltean 			       struct net_device *br)
15248aa9ebccSVladimir Oltean {
15258aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
15268aa9ebccSVladimir Oltean }
15278aa9ebccSVladimir Oltean 
15288aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
15298aa9ebccSVladimir Oltean 				 struct net_device *br)
15308aa9ebccSVladimir Oltean {
15318aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
15328aa9ebccSVladimir Oltean }
15338aa9ebccSVladimir Oltean 
15342eea1fa8SVladimir Oltean static const char * const sja1105_reset_reasons[] = {
15352eea1fa8SVladimir Oltean 	[SJA1105_VLAN_FILTERING] = "VLAN filtering",
15362eea1fa8SVladimir Oltean 	[SJA1105_RX_HWTSTAMPING] = "RX timestamping",
15372eea1fa8SVladimir Oltean 	[SJA1105_AGEING_TIME] = "Ageing time",
15382eea1fa8SVladimir Oltean 	[SJA1105_SCHEDULING] = "Time-aware scheduling",
15392eea1fa8SVladimir Oltean };
15402eea1fa8SVladimir Oltean 
15416666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
15426666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
15436666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
15446666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
15456666cebcSVladimir Oltean  * such that this operation is relatively seamless.
15466666cebcSVladimir Oltean  */
15472eea1fa8SVladimir Oltean int sja1105_static_config_reload(struct sja1105_private *priv,
15482eea1fa8SVladimir Oltean 				 enum sja1105_reset_reason reason)
15496666cebcSVladimir Oltean {
15506cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_before;
15516cf99c13SVladimir Oltean 	struct ptp_system_timestamp ptp_sts_after;
15526666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
15536666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
15546cf99c13SVladimir Oltean 	struct dsa_switch *ds = priv->ds;
15556cf99c13SVladimir Oltean 	s64 t1, t2, t3, t4;
15566cf99c13SVladimir Oltean 	s64 t12, t34;
1557ffe10e67SVladimir Oltean 	u16 bmcr = 0;
15586666cebcSVladimir Oltean 	int rc, i;
15596cf99c13SVladimir Oltean 	s64 now;
15606666cebcSVladimir Oltean 
1561af580ae2SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1562af580ae2SVladimir Oltean 
15636666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
15646666cebcSVladimir Oltean 
15658400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
15668400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
15678400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
15688400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
15696666cebcSVladimir Oltean 	 */
15706666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
15716666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
15726666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
15736666cebcSVladimir Oltean 	}
15746666cebcSVladimir Oltean 
1575ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT))
1576ffe10e67SVladimir Oltean 		bmcr = sja1105_sgmii_read(priv, MII_BMCR);
1577ffe10e67SVladimir Oltean 
15786cf99c13SVladimir Oltean 	/* No PTP operations can run right now */
15796cf99c13SVladimir Oltean 	mutex_lock(&priv->ptp_data.lock);
15806cf99c13SVladimir Oltean 
15816cf99c13SVladimir Oltean 	rc = __sja1105_ptp_gettimex(ds, &now, &ptp_sts_before);
15826cf99c13SVladimir Oltean 	if (rc < 0)
15836cf99c13SVladimir Oltean 		goto out_unlock_ptp;
15846cf99c13SVladimir Oltean 
15856666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
15866666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
15876666cebcSVladimir Oltean 	if (rc < 0)
15886cf99c13SVladimir Oltean 		goto out_unlock_ptp;
15896cf99c13SVladimir Oltean 
15906cf99c13SVladimir Oltean 	rc = __sja1105_ptp_settime(ds, 0, &ptp_sts_after);
15916cf99c13SVladimir Oltean 	if (rc < 0)
15926cf99c13SVladimir Oltean 		goto out_unlock_ptp;
15936cf99c13SVladimir Oltean 
15946cf99c13SVladimir Oltean 	t1 = timespec64_to_ns(&ptp_sts_before.pre_ts);
15956cf99c13SVladimir Oltean 	t2 = timespec64_to_ns(&ptp_sts_before.post_ts);
15966cf99c13SVladimir Oltean 	t3 = timespec64_to_ns(&ptp_sts_after.pre_ts);
15976cf99c13SVladimir Oltean 	t4 = timespec64_to_ns(&ptp_sts_after.post_ts);
15986cf99c13SVladimir Oltean 	/* Mid point, corresponds to pre-reset PTPCLKVAL */
15996cf99c13SVladimir Oltean 	t12 = t1 + (t2 - t1) / 2;
16006cf99c13SVladimir Oltean 	/* Mid point, corresponds to post-reset PTPCLKVAL, aka 0 */
16016cf99c13SVladimir Oltean 	t34 = t3 + (t4 - t3) / 2;
16026cf99c13SVladimir Oltean 	/* Advance PTPCLKVAL by the time it took since its readout */
16036cf99c13SVladimir Oltean 	now += (t34 - t12);
16046cf99c13SVladimir Oltean 
16056cf99c13SVladimir Oltean 	__sja1105_ptp_adjtime(ds, now);
16066cf99c13SVladimir Oltean 
16076cf99c13SVladimir Oltean out_unlock_ptp:
16086cf99c13SVladimir Oltean 	mutex_unlock(&priv->ptp_data.lock);
16096666cebcSVladimir Oltean 
16102eea1fa8SVladimir Oltean 	dev_info(priv->ds->dev,
16112eea1fa8SVladimir Oltean 		 "Reset switch and programmed static config. Reason: %s\n",
16122eea1fa8SVladimir Oltean 		 sja1105_reset_reasons[reason]);
16132eea1fa8SVladimir Oltean 
16146666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
16156666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
16166666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
16176666cebcSVladimir Oltean 	 */
16186666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
16196666cebcSVladimir Oltean 	if (rc < 0)
16206666cebcSVladimir Oltean 		goto out;
16216666cebcSVladimir Oltean 
16226666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
16238400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
16246666cebcSVladimir Oltean 		if (rc < 0)
16256666cebcSVladimir Oltean 			goto out;
16266666cebcSVladimir Oltean 	}
1627ffe10e67SVladimir Oltean 
1628ffe10e67SVladimir Oltean 	if (sja1105_supports_sgmii(priv, SJA1105_SGMII_PORT)) {
1629ffe10e67SVladimir Oltean 		bool an_enabled = !!(bmcr & BMCR_ANENABLE);
1630ffe10e67SVladimir Oltean 
1631ffe10e67SVladimir Oltean 		sja1105_sgmii_pcs_config(priv, an_enabled, false);
1632ffe10e67SVladimir Oltean 
1633ffe10e67SVladimir Oltean 		if (!an_enabled) {
1634ffe10e67SVladimir Oltean 			int speed = SPEED_UNKNOWN;
1635ffe10e67SVladimir Oltean 
1636ffe10e67SVladimir Oltean 			if (bmcr & BMCR_SPEED1000)
1637ffe10e67SVladimir Oltean 				speed = SPEED_1000;
1638ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED100)
1639ffe10e67SVladimir Oltean 				speed = SPEED_100;
1640ffe10e67SVladimir Oltean 			else if (bmcr & BMCR_SPEED10)
1641ffe10e67SVladimir Oltean 				speed = SPEED_10;
1642ffe10e67SVladimir Oltean 
1643ffe10e67SVladimir Oltean 			sja1105_sgmii_pcs_force_speed(priv, speed);
1644ffe10e67SVladimir Oltean 		}
1645ffe10e67SVladimir Oltean 	}
16466666cebcSVladimir Oltean out:
1647af580ae2SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1648af580ae2SVladimir Oltean 
16496666cebcSVladimir Oltean 	return rc;
16506666cebcSVladimir Oltean }
16516666cebcSVladimir Oltean 
16526666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
16536666cebcSVladimir Oltean {
16546666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
16556666cebcSVladimir Oltean 
16566666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
16576666cebcSVladimir Oltean 
16586666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
16596666cebcSVladimir Oltean 
16606666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
16616666cebcSVladimir Oltean 					   &mac[port], true);
16626666cebcSVladimir Oltean }
16636666cebcSVladimir Oltean 
16646666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
16656666cebcSVladimir Oltean {
16666666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
16676666cebcSVladimir Oltean 	int count, i;
16686666cebcSVladimir Oltean 
16696666cebcSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
16706666cebcSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
16716666cebcSVladimir Oltean 
16726666cebcSVladimir Oltean 	for (i = 0; i < count; i++)
16736666cebcSVladimir Oltean 		if (vlan[i].vlanid == vid)
16746666cebcSVladimir Oltean 			return i;
16756666cebcSVladimir Oltean 
16766666cebcSVladimir Oltean 	/* Return an invalid entry index if not found */
16776666cebcSVladimir Oltean 	return -1;
16786666cebcSVladimir Oltean }
16796666cebcSVladimir Oltean 
16806666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
16816666cebcSVladimir Oltean 			      bool enabled, bool untagged)
16826666cebcSVladimir Oltean {
16836666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
16846666cebcSVladimir Oltean 	struct sja1105_table *table;
16856666cebcSVladimir Oltean 	bool keep = true;
16866666cebcSVladimir Oltean 	int match, rc;
16876666cebcSVladimir Oltean 
16886666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
16896666cebcSVladimir Oltean 
16906666cebcSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
16916666cebcSVladimir Oltean 	if (match < 0) {
16926666cebcSVladimir Oltean 		/* Can't delete a missing entry. */
16936666cebcSVladimir Oltean 		if (!enabled)
16946666cebcSVladimir Oltean 			return 0;
16956666cebcSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
16966666cebcSVladimir Oltean 		if (rc)
16976666cebcSVladimir Oltean 			return rc;
16986666cebcSVladimir Oltean 		match = table->entry_count - 1;
16996666cebcSVladimir Oltean 	}
17006666cebcSVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
17016666cebcSVladimir Oltean 	vlan = table->entries;
17026666cebcSVladimir Oltean 	vlan[match].vlanid = vid;
17036666cebcSVladimir Oltean 	if (enabled) {
17046666cebcSVladimir Oltean 		vlan[match].vlan_bc |= BIT(port);
17056666cebcSVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
17066666cebcSVladimir Oltean 	} else {
17076666cebcSVladimir Oltean 		vlan[match].vlan_bc &= ~BIT(port);
17086666cebcSVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
17096666cebcSVladimir Oltean 	}
17106666cebcSVladimir Oltean 	/* Also unset tag_port if removing this VLAN was requested,
17116666cebcSVladimir Oltean 	 * just so we don't have a confusing bitmap (no practical purpose).
17126666cebcSVladimir Oltean 	 */
17136666cebcSVladimir Oltean 	if (untagged || !enabled)
17146666cebcSVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
17156666cebcSVladimir Oltean 	else
17166666cebcSVladimir Oltean 		vlan[match].tag_port |= BIT(port);
17176666cebcSVladimir Oltean 	/* If there's no port left as member of this VLAN,
17186666cebcSVladimir Oltean 	 * it's time for it to go.
17196666cebcSVladimir Oltean 	 */
17206666cebcSVladimir Oltean 	if (!vlan[match].vmemb_port)
17216666cebcSVladimir Oltean 		keep = false;
17226666cebcSVladimir Oltean 
17236666cebcSVladimir Oltean 	dev_dbg(priv->ds->dev,
17246666cebcSVladimir Oltean 		"%s: port %d, vid %llu, broadcast domain 0x%llx, "
17256666cebcSVladimir Oltean 		"port members 0x%llx, tagged ports 0x%llx, keep %d\n",
17266666cebcSVladimir Oltean 		__func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
17276666cebcSVladimir Oltean 		vlan[match].vmemb_port, vlan[match].tag_port, keep);
17286666cebcSVladimir Oltean 
17296666cebcSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
17306666cebcSVladimir Oltean 					  &vlan[match], keep);
17316666cebcSVladimir Oltean 	if (rc < 0)
17326666cebcSVladimir Oltean 		return rc;
17336666cebcSVladimir Oltean 
17346666cebcSVladimir Oltean 	if (!keep)
17356666cebcSVladimir Oltean 		return sja1105_table_delete_entry(table, match);
17366666cebcSVladimir Oltean 
17376666cebcSVladimir Oltean 	return 0;
17386666cebcSVladimir Oltean }
17396666cebcSVladimir Oltean 
1740227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1741227d07a0SVladimir Oltean {
1742227d07a0SVladimir Oltean 	int rc, i;
1743227d07a0SVladimir Oltean 
1744227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1745227d07a0SVladimir Oltean 		rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1746227d07a0SVladimir Oltean 		if (rc < 0) {
1747227d07a0SVladimir Oltean 			dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1748227d07a0SVladimir Oltean 				i, rc);
1749227d07a0SVladimir Oltean 			return rc;
1750227d07a0SVladimir Oltean 		}
1751227d07a0SVladimir Oltean 	}
1752227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1753227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1754227d07a0SVladimir Oltean 	return 0;
1755227d07a0SVladimir Oltean }
1756227d07a0SVladimir Oltean 
17578aa9ebccSVladimir Oltean static enum dsa_tag_protocol
17584d776482SFlorian Fainelli sja1105_get_tag_protocol(struct dsa_switch *ds, int port,
17594d776482SFlorian Fainelli 			 enum dsa_tag_protocol mp)
17608aa9ebccSVladimir Oltean {
1761227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
17628aa9ebccSVladimir Oltean }
17638aa9ebccSVladimir Oltean 
17646666cebcSVladimir Oltean /* This callback needs to be present */
17656666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
17666666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
17676666cebcSVladimir Oltean {
17686666cebcSVladimir Oltean 	return 0;
17696666cebcSVladimir Oltean }
17706666cebcSVladimir Oltean 
1771070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
1772070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
1773070ca3bbSVladimir Oltean  * So a switch reset is required.
1774070ca3bbSVladimir Oltean  */
17756666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
17766666cebcSVladimir Oltean {
17776d7c7d94SVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
1778070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
17796666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1780070ca3bbSVladimir Oltean 	struct sja1105_table *table;
1781070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
17826666cebcSVladimir Oltean 	int rc;
17836666cebcSVladimir Oltean 
1784070ca3bbSVladimir Oltean 	if (enabled) {
17856666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
178654fa49eeSVladimir Oltean 		tpid  = ETH_P_8021Q;
178754fa49eeSVladimir Oltean 		tpid2 = ETH_P_8021AD;
1788070ca3bbSVladimir Oltean 	} else {
17896666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
1790070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
1791070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
1792070ca3bbSVladimir Oltean 	}
1793070ca3bbSVladimir Oltean 
1794070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1795070ca3bbSVladimir Oltean 	general_params = table->entries;
1796f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
179754fa49eeSVladimir Oltean 	general_params->tpid = tpid;
179854fa49eeSVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
1799070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
180042824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
180142824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
180242824463SVladimir Oltean 	 */
180342824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
180442824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
1805070ca3bbSVladimir Oltean 
18066d7c7d94SVladimir Oltean 	/* VLAN filtering => independent VLAN learning.
18076d7c7d94SVladimir Oltean 	 * No VLAN filtering => shared VLAN learning.
18086d7c7d94SVladimir Oltean 	 *
18096d7c7d94SVladimir Oltean 	 * In shared VLAN learning mode, untagged traffic still gets
18106d7c7d94SVladimir Oltean 	 * pvid-tagged, and the FDB table gets populated with entries
18116d7c7d94SVladimir Oltean 	 * containing the "real" (pvid or from VLAN tag) VLAN ID.
18126d7c7d94SVladimir Oltean 	 * However the switch performs a masked L2 lookup in the FDB,
18136d7c7d94SVladimir Oltean 	 * effectively only looking up a frame's DMAC (and not VID) for the
18146d7c7d94SVladimir Oltean 	 * forwarding decision.
18156d7c7d94SVladimir Oltean 	 *
18166d7c7d94SVladimir Oltean 	 * This is extremely convenient for us, because in modes with
18176d7c7d94SVladimir Oltean 	 * vlan_filtering=0, dsa_8021q actually installs unique pvid's into
18186d7c7d94SVladimir Oltean 	 * each front panel port. This is good for identification but breaks
18196d7c7d94SVladimir Oltean 	 * learning badly - the VID of the learnt FDB entry is unique, aka
18206d7c7d94SVladimir Oltean 	 * no frames coming from any other port are going to have it. So
18216d7c7d94SVladimir Oltean 	 * for forwarding purposes, this is as though learning was broken
18226d7c7d94SVladimir Oltean 	 * (all frames get flooded).
18236d7c7d94SVladimir Oltean 	 */
18246d7c7d94SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
18256d7c7d94SVladimir Oltean 	l2_lookup_params = table->entries;
18266d7c7d94SVladimir Oltean 	l2_lookup_params->shared_learn = !enabled;
18276d7c7d94SVladimir Oltean 
18282eea1fa8SVladimir Oltean 	rc = sja1105_static_config_reload(priv, SJA1105_VLAN_FILTERING);
18296666cebcSVladimir Oltean 	if (rc)
18306666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
18316666cebcSVladimir Oltean 
1832227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
1833227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
1834227d07a0SVladimir Oltean 	 * the two configurations are mutually exclusive.
1835227d07a0SVladimir Oltean 	 */
1836227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, !enabled);
18376666cebcSVladimir Oltean }
18386666cebcSVladimir Oltean 
18396666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
18406666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
18416666cebcSVladimir Oltean {
18426666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18436666cebcSVladimir Oltean 	u16 vid;
18446666cebcSVladimir Oltean 	int rc;
18456666cebcSVladimir Oltean 
18466666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
18476666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
18486666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
18496666cebcSVladimir Oltean 		if (rc < 0) {
18506666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
18516666cebcSVladimir Oltean 				vid, port, rc);
18526666cebcSVladimir Oltean 			return;
18536666cebcSVladimir Oltean 		}
18546666cebcSVladimir Oltean 		if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
18556666cebcSVladimir Oltean 			rc = sja1105_pvid_apply(ds->priv, port, vid);
18566666cebcSVladimir Oltean 			if (rc < 0) {
18576666cebcSVladimir Oltean 				dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
18586666cebcSVladimir Oltean 					vid, port, rc);
18596666cebcSVladimir Oltean 				return;
18606666cebcSVladimir Oltean 			}
18616666cebcSVladimir Oltean 		}
18626666cebcSVladimir Oltean 	}
18636666cebcSVladimir Oltean }
18646666cebcSVladimir Oltean 
18656666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
18666666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
18676666cebcSVladimir Oltean {
18686666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
18696666cebcSVladimir Oltean 	u16 vid;
18706666cebcSVladimir Oltean 	int rc;
18716666cebcSVladimir Oltean 
18726666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
18736666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
18746666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
18756666cebcSVladimir Oltean 		if (rc < 0) {
18766666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
18776666cebcSVladimir Oltean 				vid, port, rc);
18786666cebcSVladimir Oltean 			return rc;
18796666cebcSVladimir Oltean 		}
18806666cebcSVladimir Oltean 	}
18816666cebcSVladimir Oltean 	return 0;
18826666cebcSVladimir Oltean }
18836666cebcSVladimir Oltean 
18848aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
18858aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
18868aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
18878aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
18888aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
18898aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
18908aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
18918aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
18928aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
18938aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
18948aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
18958aa9ebccSVladimir Oltean  */
18968aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
18978aa9ebccSVladimir Oltean {
18988aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
18998aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
19008aa9ebccSVladimir Oltean 	int rc;
19018aa9ebccSVladimir Oltean 
19028aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
19038aa9ebccSVladimir Oltean 	if (rc < 0) {
19048aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
19058aa9ebccSVladimir Oltean 		return rc;
19068aa9ebccSVladimir Oltean 	}
1907f5b8631cSVladimir Oltean 
1908f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
1909f5b8631cSVladimir Oltean 	 * and we can't apply them.
1910f5b8631cSVladimir Oltean 	 */
1911f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
1912f5b8631cSVladimir Oltean 	if (rc < 0) {
1913f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
1914f5b8631cSVladimir Oltean 		return rc;
1915f5b8631cSVladimir Oltean 	}
1916f5b8631cSVladimir Oltean 
191761c77126SVladimir Oltean 	rc = sja1105_ptp_clock_register(ds);
1918bb77f36aSVladimir Oltean 	if (rc < 0) {
1919bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
1920bb77f36aSVladimir Oltean 		return rc;
1921bb77f36aSVladimir Oltean 	}
19228aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
19238aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
19248aa9ebccSVladimir Oltean 	if (rc < 0) {
19258aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
19268aa9ebccSVladimir Oltean 		return rc;
19278aa9ebccSVladimir Oltean 	}
19288aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
19298aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
19308aa9ebccSVladimir Oltean 	if (rc < 0) {
19318aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
19328aa9ebccSVladimir Oltean 		return rc;
19338aa9ebccSVladimir Oltean 	}
19346666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
19356666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
19366666cebcSVladimir Oltean 	 * EtherType is.
19376666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
19386666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
19396666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
19406666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
19416666cebcSVladimir Oltean 	 */
19426666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
19438aa9ebccSVladimir Oltean 
19445f06c63bSVladimir Oltean 	/* Advertise the 8 egress queues */
19455f06c63bSVladimir Oltean 	ds->num_tx_queues = SJA1105_NUM_TC;
19465f06c63bSVladimir Oltean 
1947227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
1948227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
1949227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
1950227d07a0SVladimir Oltean 	 */
1951227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
1952227d07a0SVladimir Oltean }
1953227d07a0SVladimir Oltean 
1954f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
1955f3097be2SVladimir Oltean {
1956f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1957a68578c2SVladimir Oltean 	int port;
1958a68578c2SVladimir Oltean 
1959a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
1960a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
1961a68578c2SVladimir Oltean 
1962a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
1963a68578c2SVladimir Oltean 			continue;
1964a68578c2SVladimir Oltean 
196552c0d4e3SVladimir Oltean 		if (sp->xmit_worker)
1966a68578c2SVladimir Oltean 			kthread_destroy_worker(sp->xmit_worker);
1967a68578c2SVladimir Oltean 	}
1968f3097be2SVladimir Oltean 
1969317ab5b8SVladimir Oltean 	sja1105_tas_teardown(ds);
197061c77126SVladimir Oltean 	sja1105_ptp_clock_unregister(ds);
19716cb0abbdSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
1972f3097be2SVladimir Oltean }
1973f3097be2SVladimir Oltean 
1974e9bf9694SVladimir Oltean static int sja1105_port_enable(struct dsa_switch *ds, int port,
1975e9bf9694SVladimir Oltean 			       struct phy_device *phy)
1976e9bf9694SVladimir Oltean {
1977e9bf9694SVladimir Oltean 	struct net_device *slave;
1978e9bf9694SVladimir Oltean 
1979e9bf9694SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1980e9bf9694SVladimir Oltean 		return 0;
1981e9bf9694SVladimir Oltean 
198268bb8ea8SVivien Didelot 	slave = dsa_to_port(ds, port)->slave;
1983e9bf9694SVladimir Oltean 
1984e9bf9694SVladimir Oltean 	slave->features &= ~NETIF_F_HW_VLAN_CTAG_FILTER;
1985e9bf9694SVladimir Oltean 
1986e9bf9694SVladimir Oltean 	return 0;
1987e9bf9694SVladimir Oltean }
1988e9bf9694SVladimir Oltean 
1989a68578c2SVladimir Oltean static void sja1105_port_disable(struct dsa_switch *ds, int port)
1990a68578c2SVladimir Oltean {
1991a68578c2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1992a68578c2SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
1993a68578c2SVladimir Oltean 
1994a68578c2SVladimir Oltean 	if (!dsa_is_user_port(ds, port))
1995a68578c2SVladimir Oltean 		return;
1996a68578c2SVladimir Oltean 
1997a68578c2SVladimir Oltean 	kthread_cancel_work_sync(&sp->xmit_work);
1998a68578c2SVladimir Oltean 	skb_queue_purge(&sp->xmit_queue);
1999a68578c2SVladimir Oltean }
2000a68578c2SVladimir Oltean 
2001227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
200247ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
2003227d07a0SVladimir Oltean {
2004227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
2005227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
2006227d07a0SVladimir Oltean 	struct ethhdr *hdr;
2007227d07a0SVladimir Oltean 	int timeout = 10;
2008227d07a0SVladimir Oltean 	int rc;
2009227d07a0SVladimir Oltean 
2010227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
2011227d07a0SVladimir Oltean 
2012227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
2013227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
2014227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
201547ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
201647ed985eSVladimir Oltean 	mgmt_route.takets = takets;
2017227d07a0SVladimir Oltean 
2018227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2019227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
2020227d07a0SVladimir Oltean 	if (rc < 0) {
2021227d07a0SVladimir Oltean 		kfree_skb(skb);
2022227d07a0SVladimir Oltean 		return rc;
2023227d07a0SVladimir Oltean 	}
2024227d07a0SVladimir Oltean 
2025227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
202668bb8ea8SVivien Didelot 	dsa_enqueue_skb(skb, dsa_to_port(ds, port)->slave);
2027227d07a0SVladimir Oltean 
2028227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
2029227d07a0SVladimir Oltean 	do {
2030227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
2031227d07a0SVladimir Oltean 						 slot, &mgmt_route);
2032227d07a0SVladimir Oltean 		if (rc < 0) {
2033227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
2034227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
2035227d07a0SVladimir Oltean 			continue;
2036227d07a0SVladimir Oltean 		}
2037227d07a0SVladimir Oltean 
2038227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
2039227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
2040227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
2041227d07a0SVladimir Oltean 		 */
2042227d07a0SVladimir Oltean 		cpu_relax();
2043227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
2044227d07a0SVladimir Oltean 
2045227d07a0SVladimir Oltean 	if (!timeout) {
2046227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
2047227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
20482a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
20492a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
2050227d07a0SVladimir Oltean 		 */
2051227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
2052227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
2053227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
2054227d07a0SVladimir Oltean 	}
2055227d07a0SVladimir Oltean 
2056227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
2057227d07a0SVladimir Oltean }
2058227d07a0SVladimir Oltean 
2059a68578c2SVladimir Oltean #define work_to_port(work) \
2060a68578c2SVladimir Oltean 		container_of((work), struct sja1105_port, xmit_work)
2061a68578c2SVladimir Oltean #define tagger_to_sja1105(t) \
2062a68578c2SVladimir Oltean 		container_of((t), struct sja1105_private, tagger_data)
2063a68578c2SVladimir Oltean 
2064227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
2065227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
2066227d07a0SVladimir Oltean  * lock on the bus)
2067227d07a0SVladimir Oltean  */
2068a68578c2SVladimir Oltean static void sja1105_port_deferred_xmit(struct kthread_work *work)
2069227d07a0SVladimir Oltean {
2070a68578c2SVladimir Oltean 	struct sja1105_port *sp = work_to_port(work);
2071a68578c2SVladimir Oltean 	struct sja1105_tagger_data *tagger_data = sp->data;
2072a68578c2SVladimir Oltean 	struct sja1105_private *priv = tagger_to_sja1105(tagger_data);
2073a68578c2SVladimir Oltean 	int port = sp - priv->ports;
2074a68578c2SVladimir Oltean 	struct sk_buff *skb;
2075a68578c2SVladimir Oltean 
2076a68578c2SVladimir Oltean 	while ((skb = skb_dequeue(&sp->xmit_queue)) != NULL) {
2077a68578c2SVladimir Oltean 		struct sk_buff *clone = DSA_SKB_CB(skb)->clone;
2078227d07a0SVladimir Oltean 
2079227d07a0SVladimir Oltean 		mutex_lock(&priv->mgmt_lock);
2080227d07a0SVladimir Oltean 
2081a68578c2SVladimir Oltean 		sja1105_mgmt_xmit(priv->ds, port, 0, skb, !!clone);
2082a68578c2SVladimir Oltean 
208347ed985eSVladimir Oltean 		/* The clone, if there, was made by dsa_skb_tx_timestamp */
2084a68578c2SVladimir Oltean 		if (clone)
2085a68578c2SVladimir Oltean 			sja1105_ptp_txtstamp_skb(priv->ds, port, clone);
2086227d07a0SVladimir Oltean 
2087227d07a0SVladimir Oltean 		mutex_unlock(&priv->mgmt_lock);
2088a68578c2SVladimir Oltean 	}
20898aa9ebccSVladimir Oltean }
20908aa9ebccSVladimir Oltean 
20918456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
20928456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
20938456721dSVladimir Oltean  */
20948456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
20958456721dSVladimir Oltean 				   unsigned int ageing_time)
20968456721dSVladimir Oltean {
20978456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
20988456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
20998456721dSVladimir Oltean 	struct sja1105_table *table;
21008456721dSVladimir Oltean 	unsigned int maxage;
21018456721dSVladimir Oltean 
21028456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
21038456721dSVladimir Oltean 	l2_lookup_params = table->entries;
21048456721dSVladimir Oltean 
21058456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
21068456721dSVladimir Oltean 
21078456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
21088456721dSVladimir Oltean 		return 0;
21098456721dSVladimir Oltean 
21108456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
21118456721dSVladimir Oltean 
21122eea1fa8SVladimir Oltean 	return sja1105_static_config_reload(priv, SJA1105_AGEING_TIME);
21138456721dSVladimir Oltean }
21148456721dSVladimir Oltean 
2115317ab5b8SVladimir Oltean static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
2116317ab5b8SVladimir Oltean 				 enum tc_setup_type type,
2117317ab5b8SVladimir Oltean 				 void *type_data)
2118317ab5b8SVladimir Oltean {
2119317ab5b8SVladimir Oltean 	switch (type) {
2120317ab5b8SVladimir Oltean 	case TC_SETUP_QDISC_TAPRIO:
2121317ab5b8SVladimir Oltean 		return sja1105_setup_tc_taprio(ds, port, type_data);
2122317ab5b8SVladimir Oltean 	default:
2123317ab5b8SVladimir Oltean 		return -EOPNOTSUPP;
2124317ab5b8SVladimir Oltean 	}
2125317ab5b8SVladimir Oltean }
2126317ab5b8SVladimir Oltean 
2127511e6ca0SVladimir Oltean /* We have a single mirror (@to) port, but can configure ingress and egress
2128511e6ca0SVladimir Oltean  * mirroring on all other (@from) ports.
2129511e6ca0SVladimir Oltean  * We need to allow mirroring rules only as long as the @to port is always the
2130511e6ca0SVladimir Oltean  * same, and we need to unset the @to port from mirr_port only when there is no
2131511e6ca0SVladimir Oltean  * mirroring rule that references it.
2132511e6ca0SVladimir Oltean  */
2133511e6ca0SVladimir Oltean static int sja1105_mirror_apply(struct sja1105_private *priv, int from, int to,
2134511e6ca0SVladimir Oltean 				bool ingress, bool enabled)
2135511e6ca0SVladimir Oltean {
2136511e6ca0SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
2137511e6ca0SVladimir Oltean 	struct sja1105_mac_config_entry *mac;
2138511e6ca0SVladimir Oltean 	struct sja1105_table *table;
2139511e6ca0SVladimir Oltean 	bool already_enabled;
2140511e6ca0SVladimir Oltean 	u64 new_mirr_port;
2141511e6ca0SVladimir Oltean 	int rc;
2142511e6ca0SVladimir Oltean 
2143511e6ca0SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
2144511e6ca0SVladimir Oltean 	general_params = table->entries;
2145511e6ca0SVladimir Oltean 
2146511e6ca0SVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
2147511e6ca0SVladimir Oltean 
2148511e6ca0SVladimir Oltean 	already_enabled = (general_params->mirr_port != SJA1105_NUM_PORTS);
2149511e6ca0SVladimir Oltean 	if (already_enabled && enabled && general_params->mirr_port != to) {
2150511e6ca0SVladimir Oltean 		dev_err(priv->ds->dev,
2151511e6ca0SVladimir Oltean 			"Delete mirroring rules towards port %llu first\n",
2152511e6ca0SVladimir Oltean 			general_params->mirr_port);
2153511e6ca0SVladimir Oltean 		return -EBUSY;
2154511e6ca0SVladimir Oltean 	}
2155511e6ca0SVladimir Oltean 
2156511e6ca0SVladimir Oltean 	new_mirr_port = to;
2157511e6ca0SVladimir Oltean 	if (!enabled) {
2158511e6ca0SVladimir Oltean 		bool keep = false;
2159511e6ca0SVladimir Oltean 		int port;
2160511e6ca0SVladimir Oltean 
2161511e6ca0SVladimir Oltean 		/* Anybody still referencing mirr_port? */
2162511e6ca0SVladimir Oltean 		for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2163511e6ca0SVladimir Oltean 			if (mac[port].ing_mirr || mac[port].egr_mirr) {
2164511e6ca0SVladimir Oltean 				keep = true;
2165511e6ca0SVladimir Oltean 				break;
2166511e6ca0SVladimir Oltean 			}
2167511e6ca0SVladimir Oltean 		}
2168511e6ca0SVladimir Oltean 		/* Unset already_enabled for next time */
2169511e6ca0SVladimir Oltean 		if (!keep)
2170511e6ca0SVladimir Oltean 			new_mirr_port = SJA1105_NUM_PORTS;
2171511e6ca0SVladimir Oltean 	}
2172511e6ca0SVladimir Oltean 	if (new_mirr_port != general_params->mirr_port) {
2173511e6ca0SVladimir Oltean 		general_params->mirr_port = new_mirr_port;
2174511e6ca0SVladimir Oltean 
2175511e6ca0SVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_GENERAL_PARAMS,
2176511e6ca0SVladimir Oltean 						  0, general_params, true);
2177511e6ca0SVladimir Oltean 		if (rc < 0)
2178511e6ca0SVladimir Oltean 			return rc;
2179511e6ca0SVladimir Oltean 	}
2180511e6ca0SVladimir Oltean 
2181511e6ca0SVladimir Oltean 	if (ingress)
2182511e6ca0SVladimir Oltean 		mac[from].ing_mirr = enabled;
2183511e6ca0SVladimir Oltean 	else
2184511e6ca0SVladimir Oltean 		mac[from].egr_mirr = enabled;
2185511e6ca0SVladimir Oltean 
2186511e6ca0SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, from,
2187511e6ca0SVladimir Oltean 					    &mac[from], true);
2188511e6ca0SVladimir Oltean }
2189511e6ca0SVladimir Oltean 
2190511e6ca0SVladimir Oltean static int sja1105_mirror_add(struct dsa_switch *ds, int port,
2191511e6ca0SVladimir Oltean 			      struct dsa_mall_mirror_tc_entry *mirror,
2192511e6ca0SVladimir Oltean 			      bool ingress)
2193511e6ca0SVladimir Oltean {
2194511e6ca0SVladimir Oltean 	return sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2195511e6ca0SVladimir Oltean 				    ingress, true);
2196511e6ca0SVladimir Oltean }
2197511e6ca0SVladimir Oltean 
2198511e6ca0SVladimir Oltean static void sja1105_mirror_del(struct dsa_switch *ds, int port,
2199511e6ca0SVladimir Oltean 			       struct dsa_mall_mirror_tc_entry *mirror)
2200511e6ca0SVladimir Oltean {
2201511e6ca0SVladimir Oltean 	sja1105_mirror_apply(ds->priv, port, mirror->to_local_port,
2202511e6ca0SVladimir Oltean 			     mirror->ingress, false);
2203511e6ca0SVladimir Oltean }
2204511e6ca0SVladimir Oltean 
22058aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
22068aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
22078aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
2208f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
22098456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
2210ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
2211ffe10e67SVladimir Oltean 	.phylink_mac_link_state	= sja1105_mac_pcs_get_state,
2212af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
22138400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
22148400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
221552c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
221652c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
221752c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
2218bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
2219e9bf9694SVladimir Oltean 	.port_enable		= sja1105_port_enable,
2220a68578c2SVladimir Oltean 	.port_disable		= sja1105_port_disable,
2221291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
2222291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
2223291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
22248aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
22258aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
2226640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
22276666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
22286666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
22296666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
22306666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
2231291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
2232291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
2233291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
2234a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
2235a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
2236f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
223747ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
2238317ab5b8SVladimir Oltean 	.port_setup_tc		= sja1105_port_setup_tc,
2239511e6ca0SVladimir Oltean 	.port_mirror_add	= sja1105_mirror_add,
2240511e6ca0SVladimir Oltean 	.port_mirror_del	= sja1105_mirror_del,
22418aa9ebccSVladimir Oltean };
22428aa9ebccSVladimir Oltean 
22438aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
22448aa9ebccSVladimir Oltean {
22458aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
22468aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
22478aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
2248dff79620SVladimir Oltean 	u32 device_id;
22498aa9ebccSVladimir Oltean 	u64 part_no;
22508aa9ebccSVladimir Oltean 	int rc;
22518aa9ebccSVladimir Oltean 
225234d76e9fSVladimir Oltean 	rc = sja1105_xfer_u32(priv, SPI_READ, regs->device_id, &device_id,
225334d76e9fSVladimir Oltean 			      NULL);
22548aa9ebccSVladimir Oltean 	if (rc < 0)
22558aa9ebccSVladimir Oltean 		return rc;
22568aa9ebccSVladimir Oltean 
22578aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
2258dff79620SVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%x\n",
22598aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
22608aa9ebccSVladimir Oltean 		return -ENODEV;
22618aa9ebccSVladimir Oltean 	}
22628aa9ebccSVladimir Oltean 
22631bd44870SVladimir Oltean 	rc = sja1105_xfer_buf(priv, SPI_READ, regs->prod_id, prod_id,
22641bd44870SVladimir Oltean 			      SJA1105_SIZE_DEVICE_ID);
22658aa9ebccSVladimir Oltean 	if (rc < 0)
22668aa9ebccSVladimir Oltean 		return rc;
22678aa9ebccSVladimir Oltean 
22688aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
22698aa9ebccSVladimir Oltean 
22708aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
22718aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
22728aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
22738aa9ebccSVladimir Oltean 		return -ENODEV;
22748aa9ebccSVladimir Oltean 	}
22758aa9ebccSVladimir Oltean 
22768aa9ebccSVladimir Oltean 	return 0;
22778aa9ebccSVladimir Oltean }
22788aa9ebccSVladimir Oltean 
22798aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
22808aa9ebccSVladimir Oltean {
2281844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
22828aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
22838aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
22848aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
2285a68578c2SVladimir Oltean 	int rc, port;
22868aa9ebccSVladimir Oltean 
22878aa9ebccSVladimir Oltean 	if (!dev->of_node) {
22888aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
22898aa9ebccSVladimir Oltean 		return -EINVAL;
22908aa9ebccSVladimir Oltean 	}
22918aa9ebccSVladimir Oltean 
22928aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
22938aa9ebccSVladimir Oltean 	if (!priv)
22948aa9ebccSVladimir Oltean 		return -ENOMEM;
22958aa9ebccSVladimir Oltean 
22968aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
22978aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
22988aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
22998aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
23008aa9ebccSVladimir Oltean 	else
23018aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
23028aa9ebccSVladimir Oltean 
23038aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
23048aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
23058aa9ebccSVladimir Oltean 	 */
23068aa9ebccSVladimir Oltean 	priv->spidev = spi;
23078aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
23088aa9ebccSVladimir Oltean 
23098aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
23108aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
23118aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
23128aa9ebccSVladimir Oltean 	if (rc < 0) {
23138aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
23148aa9ebccSVladimir Oltean 		return rc;
23158aa9ebccSVladimir Oltean 	}
23168aa9ebccSVladimir Oltean 
23178aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
23188aa9ebccSVladimir Oltean 
23198aa9ebccSVladimir Oltean 	/* Detect hardware device */
23208aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
23218aa9ebccSVladimir Oltean 	if (rc < 0) {
23228aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
23238aa9ebccSVladimir Oltean 		return rc;
23248aa9ebccSVladimir Oltean 	}
23258aa9ebccSVladimir Oltean 
23268aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
23278aa9ebccSVladimir Oltean 
23287e99e347SVivien Didelot 	ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
23298aa9ebccSVladimir Oltean 	if (!ds)
23308aa9ebccSVladimir Oltean 		return -ENOMEM;
23318aa9ebccSVladimir Oltean 
23327e99e347SVivien Didelot 	ds->dev = dev;
23337e99e347SVivien Didelot 	ds->num_ports = SJA1105_NUM_PORTS;
23348aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
23358aa9ebccSVladimir Oltean 	ds->priv = priv;
23368aa9ebccSVladimir Oltean 	priv->ds = ds;
23378aa9ebccSVladimir Oltean 
2338844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
2339844d7edcSVladimir Oltean 
2340d5a619bfSVivien Didelot 	mutex_init(&priv->ptp_data.lock);
2341d5a619bfSVivien Didelot 	mutex_init(&priv->mgmt_lock);
2342d5a619bfSVivien Didelot 
2343d5a619bfSVivien Didelot 	sja1105_tas_setup(ds);
2344d5a619bfSVivien Didelot 
2345d5a619bfSVivien Didelot 	rc = dsa_register_switch(priv->ds);
2346d5a619bfSVivien Didelot 	if (rc)
2347d5a619bfSVivien Didelot 		return rc;
2348d5a619bfSVivien Didelot 
2349227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
2350a68578c2SVladimir Oltean 	for (port = 0; port < SJA1105_NUM_PORTS; port++) {
2351a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2352a68578c2SVladimir Oltean 		struct dsa_port *dp = dsa_to_port(ds, port);
2353a68578c2SVladimir Oltean 		struct net_device *slave;
2354227d07a0SVladimir Oltean 
2355a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2356a68578c2SVladimir Oltean 			continue;
2357a68578c2SVladimir Oltean 
2358a68578c2SVladimir Oltean 		dp->priv = sp;
2359a68578c2SVladimir Oltean 		sp->dp = dp;
2360844d7edcSVladimir Oltean 		sp->data = tagger_data;
2361a68578c2SVladimir Oltean 		slave = dp->slave;
2362a68578c2SVladimir Oltean 		kthread_init_work(&sp->xmit_work, sja1105_port_deferred_xmit);
2363a68578c2SVladimir Oltean 		sp->xmit_worker = kthread_create_worker(0, "%s_xmit",
2364a68578c2SVladimir Oltean 							slave->name);
2365a68578c2SVladimir Oltean 		if (IS_ERR(sp->xmit_worker)) {
2366a68578c2SVladimir Oltean 			rc = PTR_ERR(sp->xmit_worker);
2367a68578c2SVladimir Oltean 			dev_err(ds->dev,
2368a68578c2SVladimir Oltean 				"failed to create deferred xmit thread: %d\n",
2369a68578c2SVladimir Oltean 				rc);
2370a68578c2SVladimir Oltean 			goto out;
2371a68578c2SVladimir Oltean 		}
2372a68578c2SVladimir Oltean 		skb_queue_head_init(&sp->xmit_queue);
2373227d07a0SVladimir Oltean 	}
2374227d07a0SVladimir Oltean 
2375d5a619bfSVivien Didelot 	return 0;
2376a68578c2SVladimir Oltean out:
2377a68578c2SVladimir Oltean 	while (port-- > 0) {
2378a68578c2SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[port];
2379a68578c2SVladimir Oltean 
2380a68578c2SVladimir Oltean 		if (!dsa_is_user_port(ds, port))
2381a68578c2SVladimir Oltean 			continue;
2382a68578c2SVladimir Oltean 
2383a68578c2SVladimir Oltean 		kthread_destroy_worker(sp->xmit_worker);
2384a68578c2SVladimir Oltean 	}
2385a68578c2SVladimir Oltean 	return rc;
23868aa9ebccSVladimir Oltean }
23878aa9ebccSVladimir Oltean 
23888aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
23898aa9ebccSVladimir Oltean {
23908aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
23918aa9ebccSVladimir Oltean 
23928aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
23938aa9ebccSVladimir Oltean 	return 0;
23948aa9ebccSVladimir Oltean }
23958aa9ebccSVladimir Oltean 
23968aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
23978aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
23988aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
23998aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
24008aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
24018aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
24028aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
24038aa9ebccSVladimir Oltean 	{ /* sentinel */ },
24048aa9ebccSVladimir Oltean };
24058aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
24068aa9ebccSVladimir Oltean 
24078aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
24088aa9ebccSVladimir Oltean 	.driver = {
24098aa9ebccSVladimir Oltean 		.name  = "sja1105",
24108aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
24118aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
24128aa9ebccSVladimir Oltean 	},
24138aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
24148aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
24158aa9ebccSVladimir Oltean };
24168aa9ebccSVladimir Oltean 
24178aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
24188aa9ebccSVladimir Oltean 
24198aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
24208aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
24218aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
24228aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
2423