xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision 8400cff60b472cc1c698b932e8b93d3bb900c5a0)
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"
258aa9ebccSVladimir Oltean 
268aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
278aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
288aa9ebccSVladimir Oltean {
298aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
308aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
318aa9ebccSVladimir Oltean 	msleep(pulse_len);
328aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
338aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
348aa9ebccSVladimir Oltean 	msleep(startup_delay);
358aa9ebccSVladimir Oltean }
368aa9ebccSVladimir Oltean 
378aa9ebccSVladimir Oltean static void
388aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
398aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
408aa9ebccSVladimir Oltean {
418aa9ebccSVladimir Oltean 	if (allow) {
428aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
438aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
448aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
458aa9ebccSVladimir Oltean 	} else {
468aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
478aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
488aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
498aa9ebccSVladimir Oltean 	}
508aa9ebccSVladimir Oltean }
518aa9ebccSVladimir Oltean 
528aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
538aa9ebccSVladimir Oltean  * settings into sja1105_setup
548aa9ebccSVladimir Oltean  */
558aa9ebccSVladimir Oltean struct sja1105_dt_port {
568aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
578aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
588aa9ebccSVladimir Oltean };
598aa9ebccSVladimir Oltean 
608aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
618aa9ebccSVladimir Oltean {
628aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
638aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
648aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
658aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
668aa9ebccSVladimir Oltean 		 */
678aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
688aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
698aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
708aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
718aa9ebccSVladimir Oltean 		.ifg = 0,
728aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
731fd4a173SVladimir Oltean 		 * adjusted at runtime by PHYLINK.
748aa9ebccSVladimir Oltean 		 */
758aa9ebccSVladimir Oltean 		.speed = SJA1105_SPEED_AUTO,
768aa9ebccSVladimir Oltean 		/* No static correction for 1-step 1588 events */
778aa9ebccSVladimir Oltean 		.tp_delin = 0,
788aa9ebccSVladimir Oltean 		.tp_delout = 0,
798aa9ebccSVladimir Oltean 		/* Disable aging for critical TTEthernet traffic */
808aa9ebccSVladimir Oltean 		.maxage = 0xFF,
818aa9ebccSVladimir Oltean 		/* Internal VLAN (pvid) to apply to untagged ingress */
828aa9ebccSVladimir Oltean 		.vlanprio = 0,
838aa9ebccSVladimir Oltean 		.vlanid = 0,
848aa9ebccSVladimir Oltean 		.ing_mirr = false,
858aa9ebccSVladimir Oltean 		.egr_mirr = false,
868aa9ebccSVladimir Oltean 		/* Don't drop traffic with other EtherType than ETH_P_IP */
878aa9ebccSVladimir Oltean 		.drpnona664 = false,
888aa9ebccSVladimir Oltean 		/* Don't drop double-tagged traffic */
898aa9ebccSVladimir Oltean 		.drpdtag = false,
908aa9ebccSVladimir Oltean 		/* Don't drop untagged traffic */
918aa9ebccSVladimir Oltean 		.drpuntag = false,
928aa9ebccSVladimir Oltean 		/* Don't retag 802.1p (VID 0) traffic with the pvid */
938aa9ebccSVladimir Oltean 		.retag = false,
94640f763fSVladimir Oltean 		/* Disable learning and I/O on user ports by default -
95640f763fSVladimir Oltean 		 * STP will enable it.
96640f763fSVladimir Oltean 		 */
97640f763fSVladimir Oltean 		.dyn_learn = false,
988aa9ebccSVladimir Oltean 		.egress = false,
998aa9ebccSVladimir Oltean 		.ingress = false,
1008aa9ebccSVladimir Oltean 	};
1018aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1028aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1038aa9ebccSVladimir Oltean 	int i;
1048aa9ebccSVladimir Oltean 
1058aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1068aa9ebccSVladimir Oltean 
1078aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1088aa9ebccSVladimir Oltean 	if (table->entry_count) {
1098aa9ebccSVladimir Oltean 		kfree(table->entries);
1108aa9ebccSVladimir Oltean 		table->entry_count = 0;
1118aa9ebccSVladimir Oltean 	}
1128aa9ebccSVladimir Oltean 
1138aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1148aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1158aa9ebccSVladimir Oltean 	if (!table->entries)
1168aa9ebccSVladimir Oltean 		return -ENOMEM;
1178aa9ebccSVladimir Oltean 
1188aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1198aa9ebccSVladimir Oltean 
1208aa9ebccSVladimir Oltean 	mac = table->entries;
1218aa9ebccSVladimir Oltean 
122640f763fSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1238aa9ebccSVladimir Oltean 		mac[i] = default_mac;
124640f763fSVladimir Oltean 		if (i == dsa_upstream_port(priv->ds, i)) {
125640f763fSVladimir Oltean 			/* STP doesn't get called for CPU port, so we need to
126640f763fSVladimir Oltean 			 * set the I/O parameters statically.
127640f763fSVladimir Oltean 			 */
128640f763fSVladimir Oltean 			mac[i].dyn_learn = true;
129640f763fSVladimir Oltean 			mac[i].ingress = true;
130640f763fSVladimir Oltean 			mac[i].egress = true;
131640f763fSVladimir Oltean 		}
132640f763fSVladimir Oltean 	}
1338aa9ebccSVladimir Oltean 
1348aa9ebccSVladimir Oltean 	return 0;
1358aa9ebccSVladimir Oltean }
1368aa9ebccSVladimir Oltean 
1378aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1388aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1398aa9ebccSVladimir Oltean {
1408aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1418aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1428aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1438aa9ebccSVladimir Oltean 	int i;
1448aa9ebccSVladimir Oltean 
1458aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1468aa9ebccSVladimir Oltean 
1478aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1488aa9ebccSVladimir Oltean 	if (table->entry_count) {
1498aa9ebccSVladimir Oltean 		kfree(table->entries);
1508aa9ebccSVladimir Oltean 		table->entry_count = 0;
1518aa9ebccSVladimir Oltean 	}
1528aa9ebccSVladimir Oltean 
1538aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1548aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1558aa9ebccSVladimir Oltean 	if (!table->entries)
1568aa9ebccSVladimir Oltean 		return -ENOMEM;
1578aa9ebccSVladimir Oltean 
1581fd4a173SVladimir Oltean 	/* Override table based on PHYLINK DT bindings */
1598aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1608aa9ebccSVladimir Oltean 
1618aa9ebccSVladimir Oltean 	mii = table->entries;
1628aa9ebccSVladimir Oltean 
1638aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1648aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1658aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1668aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1678aa9ebccSVladimir Oltean 			break;
1688aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1698aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1708aa9ebccSVladimir Oltean 			break;
1718aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1728aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1738aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1748aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1758aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1768aa9ebccSVladimir Oltean 			break;
1778aa9ebccSVladimir Oltean 		default:
1788aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
1798aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
1808aa9ebccSVladimir Oltean 		}
1818aa9ebccSVladimir Oltean 
1828aa9ebccSVladimir Oltean 		mii->phy_mac[i] = ports[i].role;
1838aa9ebccSVladimir Oltean 	}
1848aa9ebccSVladimir Oltean 	return 0;
1858aa9ebccSVladimir Oltean }
1868aa9ebccSVladimir Oltean 
1878aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
1888aa9ebccSVladimir Oltean {
1898aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1908aa9ebccSVladimir Oltean 
1918aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1928aa9ebccSVladimir Oltean 
193291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
194291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
195291d1e72SVladimir Oltean 	 */
1968aa9ebccSVladimir Oltean 	if (table->entry_count) {
1978aa9ebccSVladimir Oltean 		kfree(table->entries);
1988aa9ebccSVladimir Oltean 		table->entry_count = 0;
1998aa9ebccSVladimir Oltean 	}
2008aa9ebccSVladimir Oltean 	return 0;
2018aa9ebccSVladimir Oltean }
2028aa9ebccSVladimir Oltean 
2038aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
2048aa9ebccSVladimir Oltean {
2058aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2068aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
2078456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
2088456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
2098aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2108aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2111da73821SVladimir Oltean 		/* And the P/Q/R/S equivalent setting: */
2121da73821SVladimir Oltean 		.start_dynspc = 0,
2138aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2148aa9ebccSVladimir Oltean 		.poly = 0x97,
2158aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2168aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2178aa9ebccSVladimir Oltean 		 */
2188aa9ebccSVladimir Oltean 		.shared_learn = false,
2198aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2208aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2218aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2228aa9ebccSVladimir Oltean 		 */
2238aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2248aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2258aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2268aa9ebccSVladimir Oltean 		 */
2278aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2281da73821SVladimir Oltean 		/* P/Q/R/S only */
2291da73821SVladimir Oltean 		.use_static = true,
2301da73821SVladimir Oltean 		/* Dynamically learned FDB entries can overwrite other (older)
2311da73821SVladimir Oltean 		 * dynamic FDB entries
2321da73821SVladimir Oltean 		 */
2331da73821SVladimir Oltean 		.owr_dyn = true,
2341da73821SVladimir Oltean 		.drpnolearn = true,
2358aa9ebccSVladimir Oltean 	};
2368aa9ebccSVladimir Oltean 
2378aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2388aa9ebccSVladimir Oltean 
2398aa9ebccSVladimir Oltean 	if (table->entry_count) {
2408aa9ebccSVladimir Oltean 		kfree(table->entries);
2418aa9ebccSVladimir Oltean 		table->entry_count = 0;
2428aa9ebccSVladimir Oltean 	}
2438aa9ebccSVladimir Oltean 
2448aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2458aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2468aa9ebccSVladimir Oltean 	if (!table->entries)
2478aa9ebccSVladimir Oltean 		return -ENOMEM;
2488aa9ebccSVladimir Oltean 
2498aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2508aa9ebccSVladimir Oltean 
2518aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2528aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2538aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2548aa9ebccSVladimir Oltean 
2558aa9ebccSVladimir Oltean 	return 0;
2568aa9ebccSVladimir Oltean }
2578aa9ebccSVladimir Oltean 
2588aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2598aa9ebccSVladimir Oltean {
2608aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2618aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2628aa9ebccSVladimir Oltean 		.ving_mirr = 0,
2638aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
2648aa9ebccSVladimir Oltean 		.vmemb_port = 0,
2658aa9ebccSVladimir Oltean 		.vlan_bc = 0,
2668aa9ebccSVladimir Oltean 		.tag_port = 0,
2678aa9ebccSVladimir Oltean 		.vlanid = 0,
2688aa9ebccSVladimir Oltean 	};
2698aa9ebccSVladimir Oltean 	int i;
2708aa9ebccSVladimir Oltean 
2718aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2728aa9ebccSVladimir Oltean 
2738aa9ebccSVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 0.
2746666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
2756666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
2766666cebcSVladimir Oltean 	 * The pvid of 0 is sufficient to pass traffic while the ports are
2776666cebcSVladimir Oltean 	 * standalone and when vlan_filtering is disabled. When filtering
2786666cebcSVladimir Oltean 	 * gets enabled, the switchdev core sets up the VLAN ID 1 and sets
2796666cebcSVladimir Oltean 	 * it as the new pvid. Actually 'pvid 1' still comes up in 'bridge
2806666cebcSVladimir Oltean 	 * vlan' even when vlan_filtering is off, but it has no effect.
2818aa9ebccSVladimir Oltean 	 */
2828aa9ebccSVladimir Oltean 	if (table->entry_count) {
2838aa9ebccSVladimir Oltean 		kfree(table->entries);
2848aa9ebccSVladimir Oltean 		table->entry_count = 0;
2858aa9ebccSVladimir Oltean 	}
2868aa9ebccSVladimir Oltean 
2878aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
2888aa9ebccSVladimir Oltean 				 GFP_KERNEL);
2898aa9ebccSVladimir Oltean 	if (!table->entries)
2908aa9ebccSVladimir Oltean 		return -ENOMEM;
2918aa9ebccSVladimir Oltean 
2928aa9ebccSVladimir Oltean 	table->entry_count = 1;
2938aa9ebccSVladimir Oltean 
2948aa9ebccSVladimir Oltean 	/* VLAN ID 0: all DT-defined ports are members; no restrictions on
2958aa9ebccSVladimir Oltean 	 * forwarding; always transmit priority-tagged frames as untagged.
2968aa9ebccSVladimir Oltean 	 */
2978aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
2988aa9ebccSVladimir Oltean 		pvid.vmemb_port |= BIT(i);
2998aa9ebccSVladimir Oltean 		pvid.vlan_bc |= BIT(i);
3008aa9ebccSVladimir Oltean 		pvid.tag_port &= ~BIT(i);
3018aa9ebccSVladimir Oltean 	}
3028aa9ebccSVladimir Oltean 
3038aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
3048aa9ebccSVladimir Oltean 	return 0;
3058aa9ebccSVladimir Oltean }
3068aa9ebccSVladimir Oltean 
3078aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
3088aa9ebccSVladimir Oltean {
3098aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
3108aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3118aa9ebccSVladimir Oltean 	int i, j;
3128aa9ebccSVladimir Oltean 
3138aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
3148aa9ebccSVladimir Oltean 
3158aa9ebccSVladimir Oltean 	if (table->entry_count) {
3168aa9ebccSVladimir Oltean 		kfree(table->entries);
3178aa9ebccSVladimir Oltean 		table->entry_count = 0;
3188aa9ebccSVladimir Oltean 	}
3198aa9ebccSVladimir Oltean 
3208aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3218aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3228aa9ebccSVladimir Oltean 	if (!table->entries)
3238aa9ebccSVladimir Oltean 		return -ENOMEM;
3248aa9ebccSVladimir Oltean 
3258aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3268aa9ebccSVladimir Oltean 
3278aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3288aa9ebccSVladimir Oltean 
3298aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3308aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3318aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3328aa9ebccSVladimir Oltean 
3338aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3348aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3358aa9ebccSVladimir Oltean 
3368aa9ebccSVladimir Oltean 		if (i == upstream)
3378aa9ebccSVladimir Oltean 			continue;
3388aa9ebccSVladimir Oltean 
3398aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3408aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3418aa9ebccSVladimir Oltean 	}
3428aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3438aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3448aa9ebccSVladimir Oltean 	 */
3458aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3468aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3478aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
3488aa9ebccSVladimir Oltean 
3498aa9ebccSVladimir Oltean 	return 0;
3508aa9ebccSVladimir Oltean }
3518aa9ebccSVladimir Oltean 
3528aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
3538aa9ebccSVladimir Oltean {
3548aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
3558aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
3568aa9ebccSVladimir Oltean 		.max_dynp = 0,
3578aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
3588aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
3598aa9ebccSVladimir Oltean 	};
3608aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3618aa9ebccSVladimir Oltean 
3628aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
3638aa9ebccSVladimir Oltean 
3648aa9ebccSVladimir Oltean 	if (table->entry_count) {
3658aa9ebccSVladimir Oltean 		kfree(table->entries);
3668aa9ebccSVladimir Oltean 		table->entry_count = 0;
3678aa9ebccSVladimir Oltean 	}
3688aa9ebccSVladimir Oltean 
3698aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
3708aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3718aa9ebccSVladimir Oltean 	if (!table->entries)
3728aa9ebccSVladimir Oltean 		return -ENOMEM;
3738aa9ebccSVladimir Oltean 
3748aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
3758aa9ebccSVladimir Oltean 
3768aa9ebccSVladimir Oltean 	/* This table only has a single entry */
3778aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
3788aa9ebccSVladimir Oltean 				default_l2fwd_params;
3798aa9ebccSVladimir Oltean 
3808aa9ebccSVladimir Oltean 	return 0;
3818aa9ebccSVladimir Oltean }
3828aa9ebccSVladimir Oltean 
3838aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
3848aa9ebccSVladimir Oltean {
3858aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
3868aa9ebccSVladimir Oltean 		/* Disallow dynamic changing of the mirror port */
3878aa9ebccSVladimir Oltean 		.mirr_ptacu = 0,
3888aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
3898aa9ebccSVladimir Oltean 		/* Priority queue for link-local frames trapped to CPU */
39008fde09aSVladimir Oltean 		.hostprio = 7,
3918aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
3928aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
39342824463SVladimir Oltean 		.incl_srcpt1 = false,
3948aa9ebccSVladimir Oltean 		.send_meta1  = false,
3958aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
3968aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
39742824463SVladimir Oltean 		.incl_srcpt0 = false,
3988aa9ebccSVladimir Oltean 		.send_meta0  = false,
3998aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
4008aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
4018aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
4028aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
4038aa9ebccSVladimir Oltean 		 */
4048aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
4058aa9ebccSVladimir Oltean 		/* Same as host port */
4068aa9ebccSVladimir Oltean 		.mirr_port = dsa_upstream_port(priv->ds, 0),
4078aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
4088aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
4098aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
4108aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
4118aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
4128aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
4138aa9ebccSVladimir Oltean 		 */
4148aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
4158aa9ebccSVladimir Oltean 		/* No TTEthernet */
4168aa9ebccSVladimir Oltean 		.vllupformat = 0,
4178aa9ebccSVladimir Oltean 		.vlmarker = 0,
4188aa9ebccSVladimir Oltean 		.vlmask = 0,
4198aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
4208aa9ebccSVladimir Oltean 		.ignore2stf = 0,
4216666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
4226666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
4236666cebcSVladimir Oltean 		 */
4246666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
4256666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
4268aa9ebccSVladimir Oltean 	};
4278aa9ebccSVladimir Oltean 	struct sja1105_table *table;
428227d07a0SVladimir Oltean 	int i, k = 0;
4298aa9ebccSVladimir Oltean 
430227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
4318aa9ebccSVladimir Oltean 		if (dsa_is_dsa_port(priv->ds, i))
4328aa9ebccSVladimir Oltean 			default_general_params.casc_port = i;
433227d07a0SVladimir Oltean 		else if (dsa_is_user_port(priv->ds, i))
434227d07a0SVladimir Oltean 			priv->ports[i].mgmt_slot = k++;
435227d07a0SVladimir Oltean 	}
4368aa9ebccSVladimir Oltean 
4378aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
4388aa9ebccSVladimir Oltean 
4398aa9ebccSVladimir Oltean 	if (table->entry_count) {
4408aa9ebccSVladimir Oltean 		kfree(table->entries);
4418aa9ebccSVladimir Oltean 		table->entry_count = 0;
4428aa9ebccSVladimir Oltean 	}
4438aa9ebccSVladimir Oltean 
4448aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
4458aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4468aa9ebccSVladimir Oltean 	if (!table->entries)
4478aa9ebccSVladimir Oltean 		return -ENOMEM;
4488aa9ebccSVladimir Oltean 
4498aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
4508aa9ebccSVladimir Oltean 
4518aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4528aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
4538aa9ebccSVladimir Oltean 				default_general_params;
4548aa9ebccSVladimir Oltean 
4558aa9ebccSVladimir Oltean 	return 0;
4568aa9ebccSVladimir Oltean }
4578aa9ebccSVladimir Oltean 
4588aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
4598aa9ebccSVladimir Oltean 
4608aa9ebccSVladimir Oltean static inline void
4618aa9ebccSVladimir Oltean sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
4628aa9ebccSVladimir Oltean 		      int index)
4638aa9ebccSVladimir Oltean {
4648aa9ebccSVladimir Oltean 	policing[index].sharindx = index;
4658aa9ebccSVladimir Oltean 	policing[index].smax = 65535; /* Burst size in bytes */
4668aa9ebccSVladimir Oltean 	policing[index].rate = SJA1105_RATE_MBPS(1000);
4678aa9ebccSVladimir Oltean 	policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
4688aa9ebccSVladimir Oltean 	policing[index].partition = 0;
4698aa9ebccSVladimir Oltean }
4708aa9ebccSVladimir Oltean 
4718aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
4728aa9ebccSVladimir Oltean {
4738aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
4748aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4758aa9ebccSVladimir Oltean 	int i, j, k;
4768aa9ebccSVladimir Oltean 
4778aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
4788aa9ebccSVladimir Oltean 
4798aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
4808aa9ebccSVladimir Oltean 	if (table->entry_count) {
4818aa9ebccSVladimir Oltean 		kfree(table->entries);
4828aa9ebccSVladimir Oltean 		table->entry_count = 0;
4838aa9ebccSVladimir Oltean 	}
4848aa9ebccSVladimir Oltean 
4858aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
4868aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4878aa9ebccSVladimir Oltean 	if (!table->entries)
4888aa9ebccSVladimir Oltean 		return -ENOMEM;
4898aa9ebccSVladimir Oltean 
4908aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
4918aa9ebccSVladimir Oltean 
4928aa9ebccSVladimir Oltean 	policing = table->entries;
4938aa9ebccSVladimir Oltean 
4948aa9ebccSVladimir Oltean 	/* k sweeps through all unicast policers (0-39).
4958aa9ebccSVladimir Oltean 	 * bcast sweeps through policers 40-44.
4968aa9ebccSVladimir Oltean 	 */
4978aa9ebccSVladimir Oltean 	for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) {
4988aa9ebccSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i;
4998aa9ebccSVladimir Oltean 
5008aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++, k++)
5018aa9ebccSVladimir Oltean 			sja1105_setup_policer(policing, k);
5028aa9ebccSVladimir Oltean 
5038aa9ebccSVladimir Oltean 		/* Set up this port's policer for broadcast traffic */
5048aa9ebccSVladimir Oltean 		sja1105_setup_policer(policing, bcast);
5058aa9ebccSVladimir Oltean 	}
5068aa9ebccSVladimir Oltean 	return 0;
5078aa9ebccSVladimir Oltean }
5088aa9ebccSVladimir Oltean 
50924c01949SVladimir Oltean static int sja1105_init_avb_params(struct sja1105_private *priv,
51024c01949SVladimir Oltean 				   bool on)
51124c01949SVladimir Oltean {
51224c01949SVladimir Oltean 	struct sja1105_avb_params_entry *avb;
51324c01949SVladimir Oltean 	struct sja1105_table *table;
51424c01949SVladimir Oltean 
51524c01949SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
51624c01949SVladimir Oltean 
51724c01949SVladimir Oltean 	/* Discard previous AVB Parameters Table */
51824c01949SVladimir Oltean 	if (table->entry_count) {
51924c01949SVladimir Oltean 		kfree(table->entries);
52024c01949SVladimir Oltean 		table->entry_count = 0;
52124c01949SVladimir Oltean 	}
52224c01949SVladimir Oltean 
52324c01949SVladimir Oltean 	/* Configure the reception of meta frames only if requested */
52424c01949SVladimir Oltean 	if (!on)
52524c01949SVladimir Oltean 		return 0;
52624c01949SVladimir Oltean 
52724c01949SVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
52824c01949SVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
52924c01949SVladimir Oltean 	if (!table->entries)
53024c01949SVladimir Oltean 		return -ENOMEM;
53124c01949SVladimir Oltean 
53224c01949SVladimir Oltean 	table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
53324c01949SVladimir Oltean 
53424c01949SVladimir Oltean 	avb = table->entries;
53524c01949SVladimir Oltean 
53624c01949SVladimir Oltean 	avb->destmeta = SJA1105_META_DMAC;
53724c01949SVladimir Oltean 	avb->srcmeta  = SJA1105_META_SMAC;
53824c01949SVladimir Oltean 
53924c01949SVladimir Oltean 	return 0;
54024c01949SVladimir Oltean }
54124c01949SVladimir Oltean 
5428aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
5438aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
5448aa9ebccSVladimir Oltean {
5458aa9ebccSVladimir Oltean 	int rc;
5468aa9ebccSVladimir Oltean 
5478aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
5488aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
5498aa9ebccSVladimir Oltean 					priv->info->static_ops,
5508aa9ebccSVladimir Oltean 					priv->info->device_id);
5518aa9ebccSVladimir Oltean 	if (rc)
5528aa9ebccSVladimir Oltean 		return rc;
5538aa9ebccSVladimir Oltean 
5548aa9ebccSVladimir Oltean 	/* Build static configuration */
5558aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
5568aa9ebccSVladimir Oltean 	if (rc < 0)
5578aa9ebccSVladimir Oltean 		return rc;
5588aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
5598aa9ebccSVladimir Oltean 	if (rc < 0)
5608aa9ebccSVladimir Oltean 		return rc;
5618aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
5628aa9ebccSVladimir Oltean 	if (rc < 0)
5638aa9ebccSVladimir Oltean 		return rc;
5648aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
5658aa9ebccSVladimir Oltean 	if (rc < 0)
5668aa9ebccSVladimir Oltean 		return rc;
5678aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
5688aa9ebccSVladimir Oltean 	if (rc < 0)
5698aa9ebccSVladimir Oltean 		return rc;
5708aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
5718aa9ebccSVladimir Oltean 	if (rc < 0)
5728aa9ebccSVladimir Oltean 		return rc;
5738aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
5748aa9ebccSVladimir Oltean 	if (rc < 0)
5758aa9ebccSVladimir Oltean 		return rc;
5768aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
5778aa9ebccSVladimir Oltean 	if (rc < 0)
5788aa9ebccSVladimir Oltean 		return rc;
5798aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
5808aa9ebccSVladimir Oltean 	if (rc < 0)
5818aa9ebccSVladimir Oltean 		return rc;
58224c01949SVladimir Oltean 	rc = sja1105_init_avb_params(priv, false);
58324c01949SVladimir Oltean 	if (rc < 0)
58424c01949SVladimir Oltean 		return rc;
5858aa9ebccSVladimir Oltean 
5868aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
5878aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
5888aa9ebccSVladimir Oltean }
5898aa9ebccSVladimir Oltean 
590f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
591f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
592f5b8631cSVladimir Oltean {
593f5b8631cSVladimir Oltean 	int i;
594f5b8631cSVladimir Oltean 
595f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
596f5b8631cSVladimir Oltean 		if (ports->role == XMII_MAC)
597f5b8631cSVladimir Oltean 			continue;
598f5b8631cSVladimir Oltean 
599f5b8631cSVladimir Oltean 		if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
600f5b8631cSVladimir Oltean 		    ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
601f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
602f5b8631cSVladimir Oltean 
603f5b8631cSVladimir Oltean 		if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
604f5b8631cSVladimir Oltean 		    ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
605f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
606f5b8631cSVladimir Oltean 
607f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
608f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
609f5b8631cSVladimir Oltean 			return -EINVAL;
610f5b8631cSVladimir Oltean 	}
611f5b8631cSVladimir Oltean 	return 0;
612f5b8631cSVladimir Oltean }
613f5b8631cSVladimir Oltean 
6148aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
6158aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
6168aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
6178aa9ebccSVladimir Oltean {
6188aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6198aa9ebccSVladimir Oltean 	struct device_node *child;
6208aa9ebccSVladimir Oltean 
6218aa9ebccSVladimir Oltean 	for_each_child_of_node(ports_node, child) {
6228aa9ebccSVladimir Oltean 		struct device_node *phy_node;
6238aa9ebccSVladimir Oltean 		int phy_mode;
6248aa9ebccSVladimir Oltean 		u32 index;
6258aa9ebccSVladimir Oltean 
6268aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
6278aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
6288aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
6298aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
6308aa9ebccSVladimir Oltean 			return -ENODEV;
6318aa9ebccSVladimir Oltean 		}
6328aa9ebccSVladimir Oltean 
6338aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
6348aa9ebccSVladimir Oltean 		phy_mode = of_get_phy_mode(child);
6358aa9ebccSVladimir Oltean 		if (phy_mode < 0) {
6368aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
6378aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
6388aa9ebccSVladimir Oltean 				index);
6398aa9ebccSVladimir Oltean 			return -ENODEV;
6408aa9ebccSVladimir Oltean 		}
6418aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
6428aa9ebccSVladimir Oltean 
6438aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
6448aa9ebccSVladimir Oltean 		if (!phy_node) {
6458aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
6468aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
6478aa9ebccSVladimir Oltean 					"properties missing!\n");
6488aa9ebccSVladimir Oltean 				return -ENODEV;
6498aa9ebccSVladimir Oltean 			}
6508aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
6518aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
6528aa9ebccSVladimir Oltean 			 */
6538aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6548aa9ebccSVladimir Oltean 		} else {
6558aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
6568aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6578aa9ebccSVladimir Oltean 			of_node_put(phy_node);
6588aa9ebccSVladimir Oltean 		}
6598aa9ebccSVladimir Oltean 
6608aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
6618aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
6628aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6638aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
6648aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6658aa9ebccSVladimir Oltean 	}
6668aa9ebccSVladimir Oltean 
6678aa9ebccSVladimir Oltean 	return 0;
6688aa9ebccSVladimir Oltean }
6698aa9ebccSVladimir Oltean 
6708aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
6718aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
6728aa9ebccSVladimir Oltean {
6738aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6748aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
6758aa9ebccSVladimir Oltean 	struct device_node *ports_node;
6768aa9ebccSVladimir Oltean 	int rc;
6778aa9ebccSVladimir Oltean 
6788aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
6798aa9ebccSVladimir Oltean 	if (!ports_node) {
6808aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
6818aa9ebccSVladimir Oltean 		return -ENODEV;
6828aa9ebccSVladimir Oltean 	}
6838aa9ebccSVladimir Oltean 
6848aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
6858aa9ebccSVladimir Oltean 	of_node_put(ports_node);
6868aa9ebccSVladimir Oltean 
6878aa9ebccSVladimir Oltean 	return rc;
6888aa9ebccSVladimir Oltean }
6898aa9ebccSVladimir Oltean 
690c44d0535SVladimir Oltean /* Convert link speed from SJA1105 to ethtool encoding */
6918aa9ebccSVladimir Oltean static int sja1105_speed[] = {
692c44d0535SVladimir Oltean 	[SJA1105_SPEED_AUTO]		= SPEED_UNKNOWN,
693c44d0535SVladimir Oltean 	[SJA1105_SPEED_10MBPS]		= SPEED_10,
694c44d0535SVladimir Oltean 	[SJA1105_SPEED_100MBPS]		= SPEED_100,
695c44d0535SVladimir Oltean 	[SJA1105_SPEED_1000MBPS]	= SPEED_1000,
6968aa9ebccSVladimir Oltean };
6978aa9ebccSVladimir Oltean 
698*8400cff6SVladimir Oltean /* Set link speed in the MAC configuration for a specific port. */
6998aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
700*8400cff6SVladimir Oltean 				      int speed_mbps)
7018aa9ebccSVladimir Oltean {
7028aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
7038aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
7048aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
7058aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
7068aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
7078aa9ebccSVladimir Oltean 	int rc;
7088aa9ebccSVladimir Oltean 
709*8400cff6SVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
710*8400cff6SVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
711*8400cff6SVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
712*8400cff6SVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
713*8400cff6SVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
714*8400cff6SVladimir Oltean 	 */
7158aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
716*8400cff6SVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
7178aa9ebccSVladimir Oltean 
718f4cfcfbdSVladimir Oltean 	switch (speed_mbps) {
719c44d0535SVladimir Oltean 	case SPEED_UNKNOWN:
720f4cfcfbdSVladimir Oltean 		/* No speed update requested */
721f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_AUTO;
722f4cfcfbdSVladimir Oltean 		break;
723c44d0535SVladimir Oltean 	case SPEED_10:
724f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_10MBPS;
725f4cfcfbdSVladimir Oltean 		break;
726c44d0535SVladimir Oltean 	case SPEED_100:
727f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_100MBPS;
728f4cfcfbdSVladimir Oltean 		break;
729c44d0535SVladimir Oltean 	case SPEED_1000:
730f4cfcfbdSVladimir Oltean 		speed = SJA1105_SPEED_1000MBPS;
731f4cfcfbdSVladimir Oltean 		break;
732f4cfcfbdSVladimir Oltean 	default:
7338aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
7348aa9ebccSVladimir Oltean 		return -EINVAL;
7358aa9ebccSVladimir Oltean 	}
7368aa9ebccSVladimir Oltean 
737*8400cff6SVladimir Oltean 	/* Overwrite SJA1105_SPEED_AUTO from the static MAC configuration
738*8400cff6SVladimir Oltean 	 * table, since this will be used for the clocking setup, and we no
739*8400cff6SVladimir Oltean 	 * longer need to store it in the static config (already told hardware
740*8400cff6SVladimir Oltean 	 * we want auto during upload phase).
7418aa9ebccSVladimir Oltean 	 */
7428aa9ebccSVladimir Oltean 	mac[port].speed = speed;
7438aa9ebccSVladimir Oltean 
7448aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
745*8400cff6SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
746*8400cff6SVladimir Oltean 					  &mac[port], true);
7478aa9ebccSVladimir Oltean 	if (rc < 0) {
7488aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
7498aa9ebccSVladimir Oltean 		return rc;
7508aa9ebccSVladimir Oltean 	}
7518aa9ebccSVladimir Oltean 
7528aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
7538aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
7548aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
7558aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
7568aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
7578aa9ebccSVladimir Oltean 	 */
7588aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
7598aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
7608aa9ebccSVladimir Oltean 		return 0;
7618aa9ebccSVladimir Oltean 
7628aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
7638aa9ebccSVladimir Oltean }
7648aa9ebccSVladimir Oltean 
765af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port,
766af7cd036SVladimir Oltean 			       unsigned int link_an_mode,
767af7cd036SVladimir Oltean 			       const struct phylink_link_state *state)
7688aa9ebccSVladimir Oltean {
7698aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
7708aa9ebccSVladimir Oltean 
771af7cd036SVladimir Oltean 	if (!state->link)
772*8400cff6SVladimir Oltean 		return;
773*8400cff6SVladimir Oltean 
774*8400cff6SVladimir Oltean 	sja1105_adjust_port_config(priv, port, state->speed);
775*8400cff6SVladimir Oltean }
776*8400cff6SVladimir Oltean 
777*8400cff6SVladimir Oltean static void sja1105_mac_link_down(struct dsa_switch *ds, int port,
778*8400cff6SVladimir Oltean 				  unsigned int mode,
779*8400cff6SVladimir Oltean 				  phy_interface_t interface)
780*8400cff6SVladimir Oltean {
781*8400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), true);
782*8400cff6SVladimir Oltean }
783*8400cff6SVladimir Oltean 
784*8400cff6SVladimir Oltean static void sja1105_mac_link_up(struct dsa_switch *ds, int port,
785*8400cff6SVladimir Oltean 				unsigned int mode,
786*8400cff6SVladimir Oltean 				phy_interface_t interface,
787*8400cff6SVladimir Oltean 				struct phy_device *phydev)
788*8400cff6SVladimir Oltean {
789*8400cff6SVladimir Oltean 	sja1105_inhibit_tx(ds->priv, BIT(port), false);
7908aa9ebccSVladimir Oltean }
7918aa9ebccSVladimir Oltean 
792ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
793ad9f299aSVladimir Oltean 				     unsigned long *supported,
794ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
795ad9f299aSVladimir Oltean {
796ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
797ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
798ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
799ad9f299aSVladimir Oltean 	 */
800ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
801ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
802ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
803ad9f299aSVladimir Oltean 
804ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
805ad9f299aSVladimir Oltean 
806ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
807ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
808ad9f299aSVladimir Oltean 	 */
809ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
810ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
811ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
812ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
813ad9f299aSVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII)
814ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
815ad9f299aSVladimir Oltean 
816ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
817ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
818ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
819ad9f299aSVladimir Oltean }
820ad9f299aSVladimir Oltean 
821291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
822291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
823291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
824291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
825291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
826291d1e72SVladimir Oltean  */
827291d1e72SVladimir Oltean static inline int sja1105et_fdb_index(int bin, int way)
828291d1e72SVladimir Oltean {
829291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
830291d1e72SVladimir Oltean }
831291d1e72SVladimir Oltean 
8329dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
833291d1e72SVladimir Oltean 					 const u8 *addr, u16 vid,
834291d1e72SVladimir Oltean 					 struct sja1105_l2_lookup_entry *match,
835291d1e72SVladimir Oltean 					 int *last_unused)
836291d1e72SVladimir Oltean {
837291d1e72SVladimir Oltean 	int way;
838291d1e72SVladimir Oltean 
839291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
840291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
841291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
842291d1e72SVladimir Oltean 
843291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
844291d1e72SVladimir Oltean 		 * into the return value
845291d1e72SVladimir Oltean 		 */
846291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
847291d1e72SVladimir Oltean 						index, &l2_lookup)) {
848291d1e72SVladimir Oltean 			if (last_unused)
849291d1e72SVladimir Oltean 				*last_unused = way;
850291d1e72SVladimir Oltean 			continue;
851291d1e72SVladimir Oltean 		}
852291d1e72SVladimir Oltean 
853291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
854291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
855291d1e72SVladimir Oltean 			if (match)
856291d1e72SVladimir Oltean 				*match = l2_lookup;
857291d1e72SVladimir Oltean 			return way;
858291d1e72SVladimir Oltean 		}
859291d1e72SVladimir Oltean 	}
860291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
861291d1e72SVladimir Oltean 	return -1;
862291d1e72SVladimir Oltean }
863291d1e72SVladimir Oltean 
8649dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port,
865291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
866291d1e72SVladimir Oltean {
867291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
868291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
869291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
870291d1e72SVladimir Oltean 	int last_unused = -1;
871291d1e72SVladimir Oltean 	int bin, way;
872291d1e72SVladimir Oltean 
8739dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
874291d1e72SVladimir Oltean 
8759dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
876291d1e72SVladimir Oltean 					    &l2_lookup, &last_unused);
877291d1e72SVladimir Oltean 	if (way >= 0) {
878291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
879291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
880291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
881291d1e72SVladimir Oltean 		 */
882291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
883291d1e72SVladimir Oltean 			return 0;
884291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
885291d1e72SVladimir Oltean 	} else {
886291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
887291d1e72SVladimir Oltean 
888291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
889291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
890291d1e72SVladimir Oltean 		 */
891291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
892291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
893291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
894291d1e72SVladimir Oltean 
895291d1e72SVladimir Oltean 		if (last_unused >= 0) {
896291d1e72SVladimir Oltean 			way = last_unused;
897291d1e72SVladimir Oltean 		} else {
898291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
899291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
900291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
901291d1e72SVladimir Oltean 			 * distribution function:
902291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
903291d1e72SVladimir Oltean 			 */
904291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
905291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
906291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
907291d1e72SVladimir Oltean 				 bin, addr, way);
908291d1e72SVladimir Oltean 			/* Evict entry */
909291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
910291d1e72SVladimir Oltean 						     index, NULL, false);
911291d1e72SVladimir Oltean 		}
912291d1e72SVladimir Oltean 	}
913291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
914291d1e72SVladimir Oltean 
915291d1e72SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
916291d1e72SVladimir Oltean 					    l2_lookup.index, &l2_lookup,
917291d1e72SVladimir Oltean 					    true);
918291d1e72SVladimir Oltean }
919291d1e72SVladimir Oltean 
9209dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port,
921291d1e72SVladimir Oltean 		      const unsigned char *addr, u16 vid)
922291d1e72SVladimir Oltean {
923291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
924291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
925291d1e72SVladimir Oltean 	int index, bin, way;
926291d1e72SVladimir Oltean 	bool keep;
927291d1e72SVladimir Oltean 
9289dfa6911SVladimir Oltean 	bin = sja1105et_fdb_hash(priv, addr, vid);
9299dfa6911SVladimir Oltean 	way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid,
930291d1e72SVladimir Oltean 					    &l2_lookup, NULL);
931291d1e72SVladimir Oltean 	if (way < 0)
932291d1e72SVladimir Oltean 		return 0;
933291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
934291d1e72SVladimir Oltean 
935291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
936291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
937291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
938291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
939291d1e72SVladimir Oltean 	 */
940291d1e72SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
9417752e937SVladimir Oltean 
942291d1e72SVladimir Oltean 	if (l2_lookup.destports)
943291d1e72SVladimir Oltean 		keep = true;
944291d1e72SVladimir Oltean 	else
945291d1e72SVladimir Oltean 		keep = false;
946291d1e72SVladimir Oltean 
947291d1e72SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
948291d1e72SVladimir Oltean 					    index, &l2_lookup, keep);
949291d1e72SVladimir Oltean }
950291d1e72SVladimir Oltean 
9519dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port,
9529dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
9539dfa6911SVladimir Oltean {
9541da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
9551da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
9561da73821SVladimir Oltean 	int rc, i;
9571da73821SVladimir Oltean 
9581da73821SVladimir Oltean 	/* Search for an existing entry in the FDB table */
9591da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
9601da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
9611da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
9621da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
9631da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
9641da73821SVladimir Oltean 	l2_lookup.mask_iotag = BIT(0);
9651da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
9661da73821SVladimir Oltean 
9671da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
9681da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
9691da73821SVladimir Oltean 	if (rc == 0) {
9701da73821SVladimir Oltean 		/* Found and this port is already in the entry's
9711da73821SVladimir Oltean 		 * port mask => job done
9721da73821SVladimir Oltean 		 */
9731da73821SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
9741da73821SVladimir Oltean 			return 0;
9751da73821SVladimir Oltean 		/* l2_lookup.index is populated by the switch in case it
9761da73821SVladimir Oltean 		 * found something.
9771da73821SVladimir Oltean 		 */
9781da73821SVladimir Oltean 		l2_lookup.destports |= BIT(port);
9791da73821SVladimir Oltean 		goto skip_finding_an_index;
9801da73821SVladimir Oltean 	}
9811da73821SVladimir Oltean 
9821da73821SVladimir Oltean 	/* Not found, so try to find an unused spot in the FDB.
9831da73821SVladimir Oltean 	 * This is slightly inefficient because the strategy is knock-knock at
9841da73821SVladimir Oltean 	 * every possible position from 0 to 1023.
9851da73821SVladimir Oltean 	 */
9861da73821SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
9871da73821SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
9881da73821SVladimir Oltean 						 i, NULL);
9891da73821SVladimir Oltean 		if (rc < 0)
9901da73821SVladimir Oltean 			break;
9911da73821SVladimir Oltean 	}
9921da73821SVladimir Oltean 	if (i == SJA1105_MAX_L2_LOOKUP_COUNT) {
9931da73821SVladimir Oltean 		dev_err(ds->dev, "FDB is full, cannot add entry.\n");
9941da73821SVladimir Oltean 		return -EINVAL;
9951da73821SVladimir Oltean 	}
9961da73821SVladimir Oltean 	l2_lookup.index = i;
9971da73821SVladimir Oltean 
9981da73821SVladimir Oltean skip_finding_an_index:
9991da73821SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
10001da73821SVladimir Oltean 					    l2_lookup.index, &l2_lookup,
10011da73821SVladimir Oltean 					    true);
10029dfa6911SVladimir Oltean }
10039dfa6911SVladimir Oltean 
10049dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port,
10059dfa6911SVladimir Oltean 			const unsigned char *addr, u16 vid)
10069dfa6911SVladimir Oltean {
10071da73821SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
10081da73821SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
10091da73821SVladimir Oltean 	bool keep;
10101da73821SVladimir Oltean 	int rc;
10111da73821SVladimir Oltean 
10121da73821SVladimir Oltean 	l2_lookup.macaddr = ether_addr_to_u64(addr);
10131da73821SVladimir Oltean 	l2_lookup.vlanid = vid;
10141da73821SVladimir Oltean 	l2_lookup.iotag = SJA1105_S_TAG;
10151da73821SVladimir Oltean 	l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0);
10161da73821SVladimir Oltean 	l2_lookup.mask_vlanid = VLAN_VID_MASK;
10171da73821SVladimir Oltean 	l2_lookup.mask_iotag = BIT(0);
10181da73821SVladimir Oltean 	l2_lookup.destports = BIT(port);
10191da73821SVladimir Oltean 
10201da73821SVladimir Oltean 	rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
10211da73821SVladimir Oltean 					 SJA1105_SEARCH, &l2_lookup);
10221da73821SVladimir Oltean 	if (rc < 0)
10231da73821SVladimir Oltean 		return 0;
10241da73821SVladimir Oltean 
10251da73821SVladimir Oltean 	l2_lookup.destports &= ~BIT(port);
10261da73821SVladimir Oltean 
10271da73821SVladimir Oltean 	/* Decide whether we remove just this port from the FDB entry,
10281da73821SVladimir Oltean 	 * or if we remove it completely.
10291da73821SVladimir Oltean 	 */
10301da73821SVladimir Oltean 	if (l2_lookup.destports)
10311da73821SVladimir Oltean 		keep = true;
10321da73821SVladimir Oltean 	else
10331da73821SVladimir Oltean 		keep = false;
10341da73821SVladimir Oltean 
10351da73821SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
10361da73821SVladimir Oltean 					    l2_lookup.index, &l2_lookup, keep);
10379dfa6911SVladimir Oltean }
10389dfa6911SVladimir Oltean 
10399dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
10409dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
10419dfa6911SVladimir Oltean {
10429dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
104393647594SVladimir Oltean 	int rc;
10449dfa6911SVladimir Oltean 
104593647594SVladimir Oltean 	/* Since we make use of VLANs even when the bridge core doesn't tell us
104693647594SVladimir Oltean 	 * to, translate these FDB entries into the correct dsa_8021q ones.
104793647594SVladimir Oltean 	 */
104893647594SVladimir Oltean 	if (!dsa_port_is_vlan_filtering(&ds->ports[port])) {
104993647594SVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, port);
105093647594SVladimir Oltean 		u16 tx_vid = dsa_8021q_tx_vid(ds, port);
105193647594SVladimir Oltean 		u16 rx_vid = dsa_8021q_rx_vid(ds, port);
105293647594SVladimir Oltean 
105393647594SVladimir Oltean 		rc = priv->info->fdb_add_cmd(ds, port, addr, tx_vid);
105493647594SVladimir Oltean 		if (rc < 0)
105593647594SVladimir Oltean 			return rc;
105693647594SVladimir Oltean 		return priv->info->fdb_add_cmd(ds, upstream, addr, rx_vid);
105793647594SVladimir Oltean 	}
10589dfa6911SVladimir Oltean 	return priv->info->fdb_add_cmd(ds, port, addr, vid);
10599dfa6911SVladimir Oltean }
10609dfa6911SVladimir Oltean 
10619dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
10629dfa6911SVladimir Oltean 			   const unsigned char *addr, u16 vid)
10639dfa6911SVladimir Oltean {
10649dfa6911SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
106593647594SVladimir Oltean 	int rc;
10669dfa6911SVladimir Oltean 
106793647594SVladimir Oltean 	/* Since we make use of VLANs even when the bridge core doesn't tell us
106893647594SVladimir Oltean 	 * to, translate these FDB entries into the correct dsa_8021q ones.
106993647594SVladimir Oltean 	 */
107093647594SVladimir Oltean 	if (!dsa_port_is_vlan_filtering(&ds->ports[port])) {
107193647594SVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, port);
107293647594SVladimir Oltean 		u16 tx_vid = dsa_8021q_tx_vid(ds, port);
107393647594SVladimir Oltean 		u16 rx_vid = dsa_8021q_rx_vid(ds, port);
107493647594SVladimir Oltean 
107593647594SVladimir Oltean 		rc = priv->info->fdb_del_cmd(ds, port, addr, tx_vid);
107693647594SVladimir Oltean 		if (rc < 0)
107793647594SVladimir Oltean 			return rc;
107893647594SVladimir Oltean 		return priv->info->fdb_del_cmd(ds, upstream, addr, rx_vid);
107993647594SVladimir Oltean 	}
10809dfa6911SVladimir Oltean 	return priv->info->fdb_del_cmd(ds, port, addr, vid);
10819dfa6911SVladimir Oltean }
10829dfa6911SVladimir Oltean 
1083291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
1084291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
1085291d1e72SVladimir Oltean {
1086291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1087291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
1088291d1e72SVladimir Oltean 	int i;
1089291d1e72SVladimir Oltean 
1090291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
1091291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
1092291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
1093291d1e72SVladimir Oltean 		int rc;
1094291d1e72SVladimir Oltean 
1095291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
1096291d1e72SVladimir Oltean 						 i, &l2_lookup);
1097291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
1098def84604SVladimir Oltean 		if (rc == -ENOENT)
1099291d1e72SVladimir Oltean 			continue;
1100291d1e72SVladimir Oltean 		if (rc) {
1101291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
1102291d1e72SVladimir Oltean 			return rc;
1103291d1e72SVladimir Oltean 		}
1104291d1e72SVladimir Oltean 
1105291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
1106291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
1107291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
1108291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
1109291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
1110291d1e72SVladimir Oltean 		 */
1111291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
1112291d1e72SVladimir Oltean 			continue;
1113291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
111493647594SVladimir Oltean 
111593647594SVladimir Oltean 		/* We need to hide the dsa_8021q VLAN from the user.
111693647594SVladimir Oltean 		 * Convert the TX VID into the pvid that is active in
111793647594SVladimir Oltean 		 * standalone and non-vlan_filtering modes, aka 1.
111893647594SVladimir Oltean 		 * The RX VID is applied on the CPU port, which is not seen by
111993647594SVladimir Oltean 		 * the bridge core anyway, so there's nothing to hide.
112093647594SVladimir Oltean 		 */
112193647594SVladimir Oltean 		if (!dsa_port_is_vlan_filtering(&ds->ports[port]))
112293647594SVladimir Oltean 			l2_lookup.vlanid = 1;
1123291d1e72SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, false, data);
1124291d1e72SVladimir Oltean 	}
1125291d1e72SVladimir Oltean 	return 0;
1126291d1e72SVladimir Oltean }
1127291d1e72SVladimir Oltean 
1128291d1e72SVladimir Oltean /* This callback needs to be present */
1129291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
1130291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
1131291d1e72SVladimir Oltean {
1132291d1e72SVladimir Oltean 	return 0;
1133291d1e72SVladimir Oltean }
1134291d1e72SVladimir Oltean 
1135291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
1136291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
1137291d1e72SVladimir Oltean {
1138291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
1139291d1e72SVladimir Oltean }
1140291d1e72SVladimir Oltean 
1141291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
1142291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
1143291d1e72SVladimir Oltean {
1144291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
1145291d1e72SVladimir Oltean }
1146291d1e72SVladimir Oltean 
11478aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
11488aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
11498aa9ebccSVladimir Oltean {
11508aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
11518aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
11528aa9ebccSVladimir Oltean 	int i, rc;
11538aa9ebccSVladimir Oltean 
11548aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
11558aa9ebccSVladimir Oltean 
11568aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
11578aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
11588aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
11598aa9ebccSVladimir Oltean 		 */
11608aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
11618aa9ebccSVladimir Oltean 			continue;
11628aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
11638aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
11648aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
11658aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
11668aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
11678aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
11688aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
11698aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
11708aa9ebccSVladimir Oltean 		 */
11718aa9ebccSVladimir Oltean 		if (i == port)
11728aa9ebccSVladimir Oltean 			continue;
11738aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
11748aa9ebccSVladimir Oltean 			continue;
11758aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
11768aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
11778aa9ebccSVladimir Oltean 
11788aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
11798aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
11808aa9ebccSVladimir Oltean 		if (rc < 0)
11818aa9ebccSVladimir Oltean 			return rc;
11828aa9ebccSVladimir Oltean 	}
11838aa9ebccSVladimir Oltean 
11848aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
11858aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
11868aa9ebccSVladimir Oltean }
11878aa9ebccSVladimir Oltean 
1188640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port,
1189640f763fSVladimir Oltean 					 u8 state)
1190640f763fSVladimir Oltean {
1191640f763fSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1192640f763fSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1193640f763fSVladimir Oltean 
1194640f763fSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
1195640f763fSVladimir Oltean 
1196640f763fSVladimir Oltean 	switch (state) {
1197640f763fSVladimir Oltean 	case BR_STATE_DISABLED:
1198640f763fSVladimir Oltean 	case BR_STATE_BLOCKING:
1199640f763fSVladimir Oltean 		/* From UM10944 description of DRPDTAG (why put this there?):
1200640f763fSVladimir Oltean 		 * "Management traffic flows to the port regardless of the state
1201640f763fSVladimir Oltean 		 * of the INGRESS flag". So BPDUs are still be allowed to pass.
1202640f763fSVladimir Oltean 		 * At the moment no difference between DISABLED and BLOCKING.
1203640f763fSVladimir Oltean 		 */
1204640f763fSVladimir Oltean 		mac[port].ingress   = false;
1205640f763fSVladimir Oltean 		mac[port].egress    = false;
1206640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1207640f763fSVladimir Oltean 		break;
1208640f763fSVladimir Oltean 	case BR_STATE_LISTENING:
1209640f763fSVladimir Oltean 		mac[port].ingress   = true;
1210640f763fSVladimir Oltean 		mac[port].egress    = false;
1211640f763fSVladimir Oltean 		mac[port].dyn_learn = false;
1212640f763fSVladimir Oltean 		break;
1213640f763fSVladimir Oltean 	case BR_STATE_LEARNING:
1214640f763fSVladimir Oltean 		mac[port].ingress   = true;
1215640f763fSVladimir Oltean 		mac[port].egress    = false;
1216640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1217640f763fSVladimir Oltean 		break;
1218640f763fSVladimir Oltean 	case BR_STATE_FORWARDING:
1219640f763fSVladimir Oltean 		mac[port].ingress   = true;
1220640f763fSVladimir Oltean 		mac[port].egress    = true;
1221640f763fSVladimir Oltean 		mac[port].dyn_learn = true;
1222640f763fSVladimir Oltean 		break;
1223640f763fSVladimir Oltean 	default:
1224640f763fSVladimir Oltean 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1225640f763fSVladimir Oltean 		return;
1226640f763fSVladimir Oltean 	}
1227640f763fSVladimir Oltean 
1228640f763fSVladimir Oltean 	sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
1229640f763fSVladimir Oltean 				     &mac[port], true);
1230640f763fSVladimir Oltean }
1231640f763fSVladimir Oltean 
12328aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
12338aa9ebccSVladimir Oltean 			       struct net_device *br)
12348aa9ebccSVladimir Oltean {
12358aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
12368aa9ebccSVladimir Oltean }
12378aa9ebccSVladimir Oltean 
12388aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
12398aa9ebccSVladimir Oltean 				 struct net_device *br)
12408aa9ebccSVladimir Oltean {
12418aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
12428aa9ebccSVladimir Oltean }
12438aa9ebccSVladimir Oltean 
12446666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
12456666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
12466666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
12476666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
12486666cebcSVladimir Oltean  * such that this operation is relatively seamless.
12496666cebcSVladimir Oltean  */
12506666cebcSVladimir Oltean static int sja1105_static_config_reload(struct sja1105_private *priv)
12516666cebcSVladimir Oltean {
12526666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
12536666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
12546666cebcSVladimir Oltean 	int rc, i;
12556666cebcSVladimir Oltean 
12566666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
12576666cebcSVladimir Oltean 
1258*8400cff6SVladimir Oltean 	/* Back up the dynamic link speed changed by sja1105_adjust_port_config
1259*8400cff6SVladimir Oltean 	 * in order to temporarily restore it to SJA1105_SPEED_AUTO - which the
1260*8400cff6SVladimir Oltean 	 * switch wants to see in the static config in order to allow us to
1261*8400cff6SVladimir Oltean 	 * change it through the dynamic interface later.
12626666cebcSVladimir Oltean 	 */
12636666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
12646666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
12656666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
12666666cebcSVladimir Oltean 	}
12676666cebcSVladimir Oltean 
12686666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
12696666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
12706666cebcSVladimir Oltean 	if (rc < 0)
12716666cebcSVladimir Oltean 		goto out;
12726666cebcSVladimir Oltean 
12736666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
12746666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
12756666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
12766666cebcSVladimir Oltean 	 */
12776666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
12786666cebcSVladimir Oltean 	if (rc < 0)
12796666cebcSVladimir Oltean 		goto out;
12806666cebcSVladimir Oltean 
12816666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1282*8400cff6SVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i]);
12836666cebcSVladimir Oltean 		if (rc < 0)
12846666cebcSVladimir Oltean 			goto out;
12856666cebcSVladimir Oltean 	}
12866666cebcSVladimir Oltean out:
12876666cebcSVladimir Oltean 	return rc;
12886666cebcSVladimir Oltean }
12896666cebcSVladimir Oltean 
12906666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
12916666cebcSVladimir Oltean {
12926666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
12936666cebcSVladimir Oltean 
12946666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
12956666cebcSVladimir Oltean 
12966666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
12976666cebcSVladimir Oltean 
12986666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
12996666cebcSVladimir Oltean 					   &mac[port], true);
13006666cebcSVladimir Oltean }
13016666cebcSVladimir Oltean 
13026666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
13036666cebcSVladimir Oltean {
13046666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
13056666cebcSVladimir Oltean 	int count, i;
13066666cebcSVladimir Oltean 
13076666cebcSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
13086666cebcSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
13096666cebcSVladimir Oltean 
13106666cebcSVladimir Oltean 	for (i = 0; i < count; i++)
13116666cebcSVladimir Oltean 		if (vlan[i].vlanid == vid)
13126666cebcSVladimir Oltean 			return i;
13136666cebcSVladimir Oltean 
13146666cebcSVladimir Oltean 	/* Return an invalid entry index if not found */
13156666cebcSVladimir Oltean 	return -1;
13166666cebcSVladimir Oltean }
13176666cebcSVladimir Oltean 
13186666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
13196666cebcSVladimir Oltean 			      bool enabled, bool untagged)
13206666cebcSVladimir Oltean {
13216666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
13226666cebcSVladimir Oltean 	struct sja1105_table *table;
13236666cebcSVladimir Oltean 	bool keep = true;
13246666cebcSVladimir Oltean 	int match, rc;
13256666cebcSVladimir Oltean 
13266666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
13276666cebcSVladimir Oltean 
13286666cebcSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
13296666cebcSVladimir Oltean 	if (match < 0) {
13306666cebcSVladimir Oltean 		/* Can't delete a missing entry. */
13316666cebcSVladimir Oltean 		if (!enabled)
13326666cebcSVladimir Oltean 			return 0;
13336666cebcSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
13346666cebcSVladimir Oltean 		if (rc)
13356666cebcSVladimir Oltean 			return rc;
13366666cebcSVladimir Oltean 		match = table->entry_count - 1;
13376666cebcSVladimir Oltean 	}
13386666cebcSVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
13396666cebcSVladimir Oltean 	vlan = table->entries;
13406666cebcSVladimir Oltean 	vlan[match].vlanid = vid;
13416666cebcSVladimir Oltean 	if (enabled) {
13426666cebcSVladimir Oltean 		vlan[match].vlan_bc |= BIT(port);
13436666cebcSVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
13446666cebcSVladimir Oltean 	} else {
13456666cebcSVladimir Oltean 		vlan[match].vlan_bc &= ~BIT(port);
13466666cebcSVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
13476666cebcSVladimir Oltean 	}
13486666cebcSVladimir Oltean 	/* Also unset tag_port if removing this VLAN was requested,
13496666cebcSVladimir Oltean 	 * just so we don't have a confusing bitmap (no practical purpose).
13506666cebcSVladimir Oltean 	 */
13516666cebcSVladimir Oltean 	if (untagged || !enabled)
13526666cebcSVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
13536666cebcSVladimir Oltean 	else
13546666cebcSVladimir Oltean 		vlan[match].tag_port |= BIT(port);
13556666cebcSVladimir Oltean 	/* If there's no port left as member of this VLAN,
13566666cebcSVladimir Oltean 	 * it's time for it to go.
13576666cebcSVladimir Oltean 	 */
13586666cebcSVladimir Oltean 	if (!vlan[match].vmemb_port)
13596666cebcSVladimir Oltean 		keep = false;
13606666cebcSVladimir Oltean 
13616666cebcSVladimir Oltean 	dev_dbg(priv->ds->dev,
13626666cebcSVladimir Oltean 		"%s: port %d, vid %llu, broadcast domain 0x%llx, "
13636666cebcSVladimir Oltean 		"port members 0x%llx, tagged ports 0x%llx, keep %d\n",
13646666cebcSVladimir Oltean 		__func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
13656666cebcSVladimir Oltean 		vlan[match].vmemb_port, vlan[match].tag_port, keep);
13666666cebcSVladimir Oltean 
13676666cebcSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
13686666cebcSVladimir Oltean 					  &vlan[match], keep);
13696666cebcSVladimir Oltean 	if (rc < 0)
13706666cebcSVladimir Oltean 		return rc;
13716666cebcSVladimir Oltean 
13726666cebcSVladimir Oltean 	if (!keep)
13736666cebcSVladimir Oltean 		return sja1105_table_delete_entry(table, match);
13746666cebcSVladimir Oltean 
13756666cebcSVladimir Oltean 	return 0;
13766666cebcSVladimir Oltean }
13776666cebcSVladimir Oltean 
1378227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled)
1379227d07a0SVladimir Oltean {
1380227d07a0SVladimir Oltean 	int rc, i;
1381227d07a0SVladimir Oltean 
1382227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1383227d07a0SVladimir Oltean 		rc = dsa_port_setup_8021q_tagging(ds, i, enabled);
1384227d07a0SVladimir Oltean 		if (rc < 0) {
1385227d07a0SVladimir Oltean 			dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n",
1386227d07a0SVladimir Oltean 				i, rc);
1387227d07a0SVladimir Oltean 			return rc;
1388227d07a0SVladimir Oltean 		}
1389227d07a0SVladimir Oltean 	}
1390227d07a0SVladimir Oltean 	dev_info(ds->dev, "%s switch tagging\n",
1391227d07a0SVladimir Oltean 		 enabled ? "Enabled" : "Disabled");
1392227d07a0SVladimir Oltean 	return 0;
1393227d07a0SVladimir Oltean }
1394227d07a0SVladimir Oltean 
13958aa9ebccSVladimir Oltean static enum dsa_tag_protocol
13968aa9ebccSVladimir Oltean sja1105_get_tag_protocol(struct dsa_switch *ds, int port)
13978aa9ebccSVladimir Oltean {
1398227d07a0SVladimir Oltean 	return DSA_TAG_PROTO_SJA1105;
13998aa9ebccSVladimir Oltean }
14008aa9ebccSVladimir Oltean 
14016666cebcSVladimir Oltean /* This callback needs to be present */
14026666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
14036666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
14046666cebcSVladimir Oltean {
14056666cebcSVladimir Oltean 	return 0;
14066666cebcSVladimir Oltean }
14076666cebcSVladimir Oltean 
1408070ca3bbSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
1409070ca3bbSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
1410070ca3bbSVladimir Oltean  * So a switch reset is required.
1411070ca3bbSVladimir Oltean  */
14126666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
14136666cebcSVladimir Oltean {
1414070ca3bbSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
14156666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1416070ca3bbSVladimir Oltean 	struct sja1105_table *table;
1417070ca3bbSVladimir Oltean 	u16 tpid, tpid2;
14186666cebcSVladimir Oltean 	int rc;
14196666cebcSVladimir Oltean 
1420070ca3bbSVladimir Oltean 	if (enabled) {
14216666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
1422f9a1a764SVladimir Oltean 		tpid  = ETH_P_8021AD;
1423f9a1a764SVladimir Oltean 		tpid2 = ETH_P_8021Q;
1424070ca3bbSVladimir Oltean 	} else {
14256666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
1426070ca3bbSVladimir Oltean 		tpid  = ETH_P_SJA1105;
1427070ca3bbSVladimir Oltean 		tpid2 = ETH_P_SJA1105;
1428070ca3bbSVladimir Oltean 	}
1429070ca3bbSVladimir Oltean 
1430070ca3bbSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1431070ca3bbSVladimir Oltean 	general_params = table->entries;
1432f9a1a764SVladimir Oltean 	/* EtherType used to identify outer tagged (S-tag) VLAN traffic */
1433070ca3bbSVladimir Oltean 	general_params->tpid = tpid;
1434f9a1a764SVladimir Oltean 	/* EtherType used to identify inner tagged (C-tag) VLAN traffic */
1435070ca3bbSVladimir Oltean 	general_params->tpid2 = tpid2;
143642824463SVladimir Oltean 	/* When VLAN filtering is on, we need to at least be able to
143742824463SVladimir Oltean 	 * decode management traffic through the "backup plan".
143842824463SVladimir Oltean 	 */
143942824463SVladimir Oltean 	general_params->incl_srcpt1 = enabled;
144042824463SVladimir Oltean 	general_params->incl_srcpt0 = enabled;
1441070ca3bbSVladimir Oltean 
1442070ca3bbSVladimir Oltean 	rc = sja1105_static_config_reload(priv);
14436666cebcSVladimir Oltean 	if (rc)
14446666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
14456666cebcSVladimir Oltean 
1446227d07a0SVladimir Oltean 	/* Switch port identification based on 802.1Q is only passable
1447227d07a0SVladimir Oltean 	 * if we are not under a vlan_filtering bridge. So make sure
1448227d07a0SVladimir Oltean 	 * the two configurations are mutually exclusive.
1449227d07a0SVladimir Oltean 	 */
1450227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, !enabled);
14516666cebcSVladimir Oltean }
14526666cebcSVladimir Oltean 
14536666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
14546666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
14556666cebcSVladimir Oltean {
14566666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14576666cebcSVladimir Oltean 	u16 vid;
14586666cebcSVladimir Oltean 	int rc;
14596666cebcSVladimir Oltean 
14606666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
14616666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
14626666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
14636666cebcSVladimir Oltean 		if (rc < 0) {
14646666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
14656666cebcSVladimir Oltean 				vid, port, rc);
14666666cebcSVladimir Oltean 			return;
14676666cebcSVladimir Oltean 		}
14686666cebcSVladimir Oltean 		if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
14696666cebcSVladimir Oltean 			rc = sja1105_pvid_apply(ds->priv, port, vid);
14706666cebcSVladimir Oltean 			if (rc < 0) {
14716666cebcSVladimir Oltean 				dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
14726666cebcSVladimir Oltean 					vid, port, rc);
14736666cebcSVladimir Oltean 				return;
14746666cebcSVladimir Oltean 			}
14756666cebcSVladimir Oltean 		}
14766666cebcSVladimir Oltean 	}
14776666cebcSVladimir Oltean }
14786666cebcSVladimir Oltean 
14796666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
14806666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
14816666cebcSVladimir Oltean {
14826666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
14836666cebcSVladimir Oltean 	u16 vid;
14846666cebcSVladimir Oltean 	int rc;
14856666cebcSVladimir Oltean 
14866666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
14876666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
14886666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
14896666cebcSVladimir Oltean 		if (rc < 0) {
14906666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
14916666cebcSVladimir Oltean 				vid, port, rc);
14926666cebcSVladimir Oltean 			return rc;
14936666cebcSVladimir Oltean 		}
14946666cebcSVladimir Oltean 	}
14956666cebcSVladimir Oltean 	return 0;
14966666cebcSVladimir Oltean }
14976666cebcSVladimir Oltean 
14988aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
14998aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
15008aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
15018aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
15028aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
15038aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
15048aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
15058aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
15068aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
15078aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
15088aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
15098aa9ebccSVladimir Oltean  */
15108aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
15118aa9ebccSVladimir Oltean {
15128aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
15138aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
15148aa9ebccSVladimir Oltean 	int rc;
15158aa9ebccSVladimir Oltean 
15168aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
15178aa9ebccSVladimir Oltean 	if (rc < 0) {
15188aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
15198aa9ebccSVladimir Oltean 		return rc;
15208aa9ebccSVladimir Oltean 	}
1521f5b8631cSVladimir Oltean 
1522f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
1523f5b8631cSVladimir Oltean 	 * and we can't apply them.
1524f5b8631cSVladimir Oltean 	 */
1525f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
1526f5b8631cSVladimir Oltean 	if (rc < 0) {
1527f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
1528f5b8631cSVladimir Oltean 		return rc;
1529f5b8631cSVladimir Oltean 	}
1530f5b8631cSVladimir Oltean 
1531bb77f36aSVladimir Oltean 	rc = sja1105_ptp_clock_register(priv);
1532bb77f36aSVladimir Oltean 	if (rc < 0) {
1533bb77f36aSVladimir Oltean 		dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
1534bb77f36aSVladimir Oltean 		return rc;
1535bb77f36aSVladimir Oltean 	}
15368aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
15378aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
15388aa9ebccSVladimir Oltean 	if (rc < 0) {
15398aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
15408aa9ebccSVladimir Oltean 		return rc;
15418aa9ebccSVladimir Oltean 	}
15428aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
15438aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
15448aa9ebccSVladimir Oltean 	if (rc < 0) {
15458aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
15468aa9ebccSVladimir Oltean 		return rc;
15478aa9ebccSVladimir Oltean 	}
15486666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
15496666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
15506666cebcSVladimir Oltean 	 * EtherType is.
15516666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
15526666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
15536666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
15546666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
15556666cebcSVladimir Oltean 	 */
15566666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
15578aa9ebccSVladimir Oltean 
1558227d07a0SVladimir Oltean 	/* The DSA/switchdev model brings up switch ports in standalone mode by
1559227d07a0SVladimir Oltean 	 * default, and that means vlan_filtering is 0 since they're not under
1560227d07a0SVladimir Oltean 	 * a bridge, so it's safe to set up switch tagging at this time.
1561227d07a0SVladimir Oltean 	 */
1562227d07a0SVladimir Oltean 	return sja1105_setup_8021q_tagging(ds, true);
1563227d07a0SVladimir Oltean }
1564227d07a0SVladimir Oltean 
1565f3097be2SVladimir Oltean static void sja1105_teardown(struct dsa_switch *ds)
1566f3097be2SVladimir Oltean {
1567f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1568f3097be2SVladimir Oltean 
1569f3097be2SVladimir Oltean 	cancel_work_sync(&priv->tagger_data.rxtstamp_work);
1570f3097be2SVladimir Oltean 	skb_queue_purge(&priv->tagger_data.skb_rxtstamp_queue);
1571f3097be2SVladimir Oltean }
1572f3097be2SVladimir Oltean 
1573227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot,
157447ed985eSVladimir Oltean 			     struct sk_buff *skb, bool takets)
1575227d07a0SVladimir Oltean {
1576227d07a0SVladimir Oltean 	struct sja1105_mgmt_entry mgmt_route = {0};
1577227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1578227d07a0SVladimir Oltean 	struct ethhdr *hdr;
1579227d07a0SVladimir Oltean 	int timeout = 10;
1580227d07a0SVladimir Oltean 	int rc;
1581227d07a0SVladimir Oltean 
1582227d07a0SVladimir Oltean 	hdr = eth_hdr(skb);
1583227d07a0SVladimir Oltean 
1584227d07a0SVladimir Oltean 	mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest);
1585227d07a0SVladimir Oltean 	mgmt_route.destports = BIT(port);
1586227d07a0SVladimir Oltean 	mgmt_route.enfport = 1;
158747ed985eSVladimir Oltean 	mgmt_route.tsreg = 0;
158847ed985eSVladimir Oltean 	mgmt_route.takets = takets;
1589227d07a0SVladimir Oltean 
1590227d07a0SVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1591227d07a0SVladimir Oltean 					  slot, &mgmt_route, true);
1592227d07a0SVladimir Oltean 	if (rc < 0) {
1593227d07a0SVladimir Oltean 		kfree_skb(skb);
1594227d07a0SVladimir Oltean 		return rc;
1595227d07a0SVladimir Oltean 	}
1596227d07a0SVladimir Oltean 
1597227d07a0SVladimir Oltean 	/* Transfer skb to the host port. */
1598227d07a0SVladimir Oltean 	dsa_enqueue_skb(skb, ds->ports[port].slave);
1599227d07a0SVladimir Oltean 
1600227d07a0SVladimir Oltean 	/* Wait until the switch has processed the frame */
1601227d07a0SVladimir Oltean 	do {
1602227d07a0SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE,
1603227d07a0SVladimir Oltean 						 slot, &mgmt_route);
1604227d07a0SVladimir Oltean 		if (rc < 0) {
1605227d07a0SVladimir Oltean 			dev_err_ratelimited(priv->ds->dev,
1606227d07a0SVladimir Oltean 					    "failed to poll for mgmt route\n");
1607227d07a0SVladimir Oltean 			continue;
1608227d07a0SVladimir Oltean 		}
1609227d07a0SVladimir Oltean 
1610227d07a0SVladimir Oltean 		/* UM10944: The ENFPORT flag of the respective entry is
1611227d07a0SVladimir Oltean 		 * cleared when a match is found. The host can use this
1612227d07a0SVladimir Oltean 		 * flag as an acknowledgment.
1613227d07a0SVladimir Oltean 		 */
1614227d07a0SVladimir Oltean 		cpu_relax();
1615227d07a0SVladimir Oltean 	} while (mgmt_route.enfport && --timeout);
1616227d07a0SVladimir Oltean 
1617227d07a0SVladimir Oltean 	if (!timeout) {
1618227d07a0SVladimir Oltean 		/* Clean up the management route so that a follow-up
1619227d07a0SVladimir Oltean 		 * frame may not match on it by mistake.
16202a7e7409SVladimir Oltean 		 * This is only hardware supported on P/Q/R/S - on E/T it is
16212a7e7409SVladimir Oltean 		 * a no-op and we are silently discarding the -EOPNOTSUPP.
1622227d07a0SVladimir Oltean 		 */
1623227d07a0SVladimir Oltean 		sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE,
1624227d07a0SVladimir Oltean 					     slot, &mgmt_route, false);
1625227d07a0SVladimir Oltean 		dev_err_ratelimited(priv->ds->dev, "xmit timed out\n");
1626227d07a0SVladimir Oltean 	}
1627227d07a0SVladimir Oltean 
1628227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
1629227d07a0SVladimir Oltean }
1630227d07a0SVladimir Oltean 
1631227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management
1632227d07a0SVladimir Oltean  * route cannot be done from atomit context (SPI transfer takes a sleepable
1633227d07a0SVladimir Oltean  * lock on the bus)
1634227d07a0SVladimir Oltean  */
1635227d07a0SVladimir Oltean static netdev_tx_t sja1105_port_deferred_xmit(struct dsa_switch *ds, int port,
1636227d07a0SVladimir Oltean 					      struct sk_buff *skb)
1637227d07a0SVladimir Oltean {
1638227d07a0SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1639227d07a0SVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
164047ed985eSVladimir Oltean 	struct skb_shared_hwtstamps shwt = {0};
1641227d07a0SVladimir Oltean 	int slot = sp->mgmt_slot;
164247ed985eSVladimir Oltean 	struct sk_buff *clone;
164347ed985eSVladimir Oltean 	u64 now, ts;
164447ed985eSVladimir Oltean 	int rc;
1645227d07a0SVladimir Oltean 
1646227d07a0SVladimir Oltean 	/* The tragic fact about the switch having 4x2 slots for installing
1647227d07a0SVladimir Oltean 	 * management routes is that all of them except one are actually
1648227d07a0SVladimir Oltean 	 * useless.
1649227d07a0SVladimir Oltean 	 * If 2 slots are simultaneously configured for two BPDUs sent to the
1650227d07a0SVladimir Oltean 	 * same (multicast) DMAC but on different egress ports, the switch
1651227d07a0SVladimir Oltean 	 * would confuse them and redirect first frame it receives on the CPU
1652227d07a0SVladimir Oltean 	 * port towards the port configured on the numerically first slot
1653227d07a0SVladimir Oltean 	 * (therefore wrong port), then second received frame on second slot
1654227d07a0SVladimir Oltean 	 * (also wrong port).
1655227d07a0SVladimir Oltean 	 * So for all practical purposes, there needs to be a lock that
1656227d07a0SVladimir Oltean 	 * prevents that from happening. The slot used here is utterly useless
1657227d07a0SVladimir Oltean 	 * (could have simply been 0 just as fine), but we are doing it
1658227d07a0SVladimir Oltean 	 * nonetheless, in case a smarter idea ever comes up in the future.
1659227d07a0SVladimir Oltean 	 */
1660227d07a0SVladimir Oltean 	mutex_lock(&priv->mgmt_lock);
1661227d07a0SVladimir Oltean 
166247ed985eSVladimir Oltean 	/* The clone, if there, was made by dsa_skb_tx_timestamp */
166347ed985eSVladimir Oltean 	clone = DSA_SKB_CB(skb)->clone;
1664227d07a0SVladimir Oltean 
166547ed985eSVladimir Oltean 	sja1105_mgmt_xmit(ds, port, slot, skb, !!clone);
166647ed985eSVladimir Oltean 
166747ed985eSVladimir Oltean 	if (!clone)
166847ed985eSVladimir Oltean 		goto out;
166947ed985eSVladimir Oltean 
167047ed985eSVladimir Oltean 	skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
167147ed985eSVladimir Oltean 
167247ed985eSVladimir Oltean 	mutex_lock(&priv->ptp_lock);
167347ed985eSVladimir Oltean 
167447ed985eSVladimir Oltean 	now = priv->tstamp_cc.read(&priv->tstamp_cc);
167547ed985eSVladimir Oltean 
167647ed985eSVladimir Oltean 	rc = sja1105_ptpegr_ts_poll(priv, slot, &ts);
167747ed985eSVladimir Oltean 	if (rc < 0) {
167847ed985eSVladimir Oltean 		dev_err(ds->dev, "xmit: timed out polling for tstamp\n");
167947ed985eSVladimir Oltean 		kfree_skb(clone);
168047ed985eSVladimir Oltean 		goto out_unlock_ptp;
168147ed985eSVladimir Oltean 	}
168247ed985eSVladimir Oltean 
168347ed985eSVladimir Oltean 	ts = sja1105_tstamp_reconstruct(priv, now, ts);
168447ed985eSVladimir Oltean 	ts = timecounter_cyc2time(&priv->tstamp_tc, ts);
168547ed985eSVladimir Oltean 
168647ed985eSVladimir Oltean 	shwt.hwtstamp = ns_to_ktime(ts);
168747ed985eSVladimir Oltean 	skb_complete_tx_timestamp(clone, &shwt);
168847ed985eSVladimir Oltean 
168947ed985eSVladimir Oltean out_unlock_ptp:
169047ed985eSVladimir Oltean 	mutex_unlock(&priv->ptp_lock);
169147ed985eSVladimir Oltean out:
1692227d07a0SVladimir Oltean 	mutex_unlock(&priv->mgmt_lock);
1693227d07a0SVladimir Oltean 	return NETDEV_TX_OK;
16948aa9ebccSVladimir Oltean }
16958aa9ebccSVladimir Oltean 
16968456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
16978456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
16988456721dSVladimir Oltean  */
16998456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
17008456721dSVladimir Oltean 				   unsigned int ageing_time)
17018456721dSVladimir Oltean {
17028456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
17038456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
17048456721dSVladimir Oltean 	struct sja1105_table *table;
17058456721dSVladimir Oltean 	unsigned int maxage;
17068456721dSVladimir Oltean 
17078456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
17088456721dSVladimir Oltean 	l2_lookup_params = table->entries;
17098456721dSVladimir Oltean 
17108456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
17118456721dSVladimir Oltean 
17128456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
17138456721dSVladimir Oltean 		return 0;
17148456721dSVladimir Oltean 
17158456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
17168456721dSVladimir Oltean 
17178456721dSVladimir Oltean 	return sja1105_static_config_reload(priv);
17188456721dSVladimir Oltean }
17198456721dSVladimir Oltean 
1720a602afd2SVladimir Oltean /* Caller must hold priv->tagger_data.meta_lock */
1721a602afd2SVladimir Oltean static int sja1105_change_rxtstamping(struct sja1105_private *priv,
1722a602afd2SVladimir Oltean 				      bool on)
1723a602afd2SVladimir Oltean {
1724a602afd2SVladimir Oltean 	struct sja1105_general_params_entry *general_params;
1725a602afd2SVladimir Oltean 	struct sja1105_table *table;
1726a602afd2SVladimir Oltean 	int rc;
1727a602afd2SVladimir Oltean 
1728a602afd2SVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1729a602afd2SVladimir Oltean 	general_params = table->entries;
1730a602afd2SVladimir Oltean 	general_params->send_meta1 = on;
1731a602afd2SVladimir Oltean 	general_params->send_meta0 = on;
1732a602afd2SVladimir Oltean 
1733a602afd2SVladimir Oltean 	rc = sja1105_init_avb_params(priv, on);
1734a602afd2SVladimir Oltean 	if (rc < 0)
1735a602afd2SVladimir Oltean 		return rc;
1736a602afd2SVladimir Oltean 
1737a602afd2SVladimir Oltean 	/* Initialize the meta state machine to a known state */
1738a602afd2SVladimir Oltean 	if (priv->tagger_data.stampable_skb) {
1739a602afd2SVladimir Oltean 		kfree_skb(priv->tagger_data.stampable_skb);
1740a602afd2SVladimir Oltean 		priv->tagger_data.stampable_skb = NULL;
1741a602afd2SVladimir Oltean 	}
1742a602afd2SVladimir Oltean 
1743a602afd2SVladimir Oltean 	return sja1105_static_config_reload(priv);
1744a602afd2SVladimir Oltean }
1745a602afd2SVladimir Oltean 
1746a602afd2SVladimir Oltean static int sja1105_hwtstamp_set(struct dsa_switch *ds, int port,
1747a602afd2SVladimir Oltean 				struct ifreq *ifr)
1748a602afd2SVladimir Oltean {
1749a602afd2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1750a602afd2SVladimir Oltean 	struct hwtstamp_config config;
1751a602afd2SVladimir Oltean 	bool rx_on;
1752a602afd2SVladimir Oltean 	int rc;
1753a602afd2SVladimir Oltean 
1754a602afd2SVladimir Oltean 	if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1755a602afd2SVladimir Oltean 		return -EFAULT;
1756a602afd2SVladimir Oltean 
1757a602afd2SVladimir Oltean 	switch (config.tx_type) {
1758a602afd2SVladimir Oltean 	case HWTSTAMP_TX_OFF:
1759a602afd2SVladimir Oltean 		priv->ports[port].hwts_tx_en = false;
1760a602afd2SVladimir Oltean 		break;
1761a602afd2SVladimir Oltean 	case HWTSTAMP_TX_ON:
1762a602afd2SVladimir Oltean 		priv->ports[port].hwts_tx_en = true;
1763a602afd2SVladimir Oltean 		break;
1764a602afd2SVladimir Oltean 	default:
1765a602afd2SVladimir Oltean 		return -ERANGE;
1766a602afd2SVladimir Oltean 	}
1767a602afd2SVladimir Oltean 
1768a602afd2SVladimir Oltean 	switch (config.rx_filter) {
1769a602afd2SVladimir Oltean 	case HWTSTAMP_FILTER_NONE:
1770a602afd2SVladimir Oltean 		rx_on = false;
1771a602afd2SVladimir Oltean 		break;
1772a602afd2SVladimir Oltean 	default:
1773a602afd2SVladimir Oltean 		rx_on = true;
1774a602afd2SVladimir Oltean 		break;
1775a602afd2SVladimir Oltean 	}
1776a602afd2SVladimir Oltean 
1777a602afd2SVladimir Oltean 	if (rx_on != priv->tagger_data.hwts_rx_en) {
1778a602afd2SVladimir Oltean 		spin_lock(&priv->tagger_data.meta_lock);
1779a602afd2SVladimir Oltean 		rc = sja1105_change_rxtstamping(priv, rx_on);
1780a602afd2SVladimir Oltean 		spin_unlock(&priv->tagger_data.meta_lock);
1781a602afd2SVladimir Oltean 		if (rc < 0) {
1782a602afd2SVladimir Oltean 			dev_err(ds->dev,
1783a602afd2SVladimir Oltean 				"Failed to change RX timestamping: %d\n", rc);
1784a602afd2SVladimir Oltean 			return -EFAULT;
1785a602afd2SVladimir Oltean 		}
1786a602afd2SVladimir Oltean 		priv->tagger_data.hwts_rx_en = rx_on;
1787a602afd2SVladimir Oltean 	}
1788a602afd2SVladimir Oltean 
1789a602afd2SVladimir Oltean 	if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1790a602afd2SVladimir Oltean 		return -EFAULT;
1791a602afd2SVladimir Oltean 	return 0;
1792a602afd2SVladimir Oltean }
1793a602afd2SVladimir Oltean 
1794a602afd2SVladimir Oltean static int sja1105_hwtstamp_get(struct dsa_switch *ds, int port,
1795a602afd2SVladimir Oltean 				struct ifreq *ifr)
1796a602afd2SVladimir Oltean {
1797a602afd2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1798a602afd2SVladimir Oltean 	struct hwtstamp_config config;
1799a602afd2SVladimir Oltean 
1800a602afd2SVladimir Oltean 	config.flags = 0;
1801a602afd2SVladimir Oltean 	if (priv->ports[port].hwts_tx_en)
1802a602afd2SVladimir Oltean 		config.tx_type = HWTSTAMP_TX_ON;
1803a602afd2SVladimir Oltean 	else
1804a602afd2SVladimir Oltean 		config.tx_type = HWTSTAMP_TX_OFF;
1805a602afd2SVladimir Oltean 	if (priv->tagger_data.hwts_rx_en)
1806a602afd2SVladimir Oltean 		config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1807a602afd2SVladimir Oltean 	else
1808a602afd2SVladimir Oltean 		config.rx_filter = HWTSTAMP_FILTER_NONE;
1809a602afd2SVladimir Oltean 
1810a602afd2SVladimir Oltean 	return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1811a602afd2SVladimir Oltean 		-EFAULT : 0;
1812a602afd2SVladimir Oltean }
1813a602afd2SVladimir Oltean 
1814f3097be2SVladimir Oltean #define to_tagger(d) \
1815f3097be2SVladimir Oltean 	container_of((d), struct sja1105_tagger_data, rxtstamp_work)
1816f3097be2SVladimir Oltean #define to_sja1105(d) \
1817f3097be2SVladimir Oltean 	container_of((d), struct sja1105_private, tagger_data)
1818f3097be2SVladimir Oltean 
1819f3097be2SVladimir Oltean static void sja1105_rxtstamp_work(struct work_struct *work)
1820f3097be2SVladimir Oltean {
1821f3097be2SVladimir Oltean 	struct sja1105_tagger_data *data = to_tagger(work);
1822f3097be2SVladimir Oltean 	struct sja1105_private *priv = to_sja1105(data);
1823f3097be2SVladimir Oltean 	struct sk_buff *skb;
1824f3097be2SVladimir Oltean 	u64 now;
1825f3097be2SVladimir Oltean 
1826f3097be2SVladimir Oltean 	mutex_lock(&priv->ptp_lock);
1827f3097be2SVladimir Oltean 
1828f3097be2SVladimir Oltean 	now = priv->tstamp_cc.read(&priv->tstamp_cc);
1829f3097be2SVladimir Oltean 
1830f3097be2SVladimir Oltean 	while ((skb = skb_dequeue(&data->skb_rxtstamp_queue)) != NULL) {
1831f3097be2SVladimir Oltean 		struct skb_shared_hwtstamps *shwt = skb_hwtstamps(skb);
1832f3097be2SVladimir Oltean 		u64 ts;
1833f3097be2SVladimir Oltean 
1834f3097be2SVladimir Oltean 		*shwt = (struct skb_shared_hwtstamps) {0};
1835f3097be2SVladimir Oltean 
1836f3097be2SVladimir Oltean 		ts = SJA1105_SKB_CB(skb)->meta_tstamp;
1837f3097be2SVladimir Oltean 		ts = sja1105_tstamp_reconstruct(priv, now, ts);
1838f3097be2SVladimir Oltean 		ts = timecounter_cyc2time(&priv->tstamp_tc, ts);
1839f3097be2SVladimir Oltean 
1840f3097be2SVladimir Oltean 		shwt->hwtstamp = ns_to_ktime(ts);
1841f3097be2SVladimir Oltean 		netif_rx_ni(skb);
1842f3097be2SVladimir Oltean 	}
1843f3097be2SVladimir Oltean 
1844f3097be2SVladimir Oltean 	mutex_unlock(&priv->ptp_lock);
1845f3097be2SVladimir Oltean }
1846f3097be2SVladimir Oltean 
1847f3097be2SVladimir Oltean /* Called from dsa_skb_defer_rx_timestamp */
1848f3097be2SVladimir Oltean bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
1849f3097be2SVladimir Oltean 			   struct sk_buff *skb, unsigned int type)
1850f3097be2SVladimir Oltean {
1851f3097be2SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
1852f3097be2SVladimir Oltean 	struct sja1105_tagger_data *data = &priv->tagger_data;
1853f3097be2SVladimir Oltean 
1854f3097be2SVladimir Oltean 	if (!data->hwts_rx_en)
1855f3097be2SVladimir Oltean 		return false;
1856f3097be2SVladimir Oltean 
1857f3097be2SVladimir Oltean 	/* We need to read the full PTP clock to reconstruct the Rx
1858f3097be2SVladimir Oltean 	 * timestamp. For that we need a sleepable context.
1859f3097be2SVladimir Oltean 	 */
1860f3097be2SVladimir Oltean 	skb_queue_tail(&data->skb_rxtstamp_queue, skb);
1861f3097be2SVladimir Oltean 	schedule_work(&data->rxtstamp_work);
1862f3097be2SVladimir Oltean 	return true;
1863f3097be2SVladimir Oltean }
1864f3097be2SVladimir Oltean 
186547ed985eSVladimir Oltean /* Called from dsa_skb_tx_timestamp. This callback is just to make DSA clone
186647ed985eSVladimir Oltean  * the skb and have it available in DSA_SKB_CB in the .port_deferred_xmit
186747ed985eSVladimir Oltean  * callback, where we will timestamp it synchronously.
186847ed985eSVladimir Oltean  */
186947ed985eSVladimir Oltean bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
187047ed985eSVladimir Oltean 			   struct sk_buff *skb, unsigned int type)
187147ed985eSVladimir Oltean {
187247ed985eSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
187347ed985eSVladimir Oltean 	struct sja1105_port *sp = &priv->ports[port];
187447ed985eSVladimir Oltean 
187547ed985eSVladimir Oltean 	if (!sp->hwts_tx_en)
187647ed985eSVladimir Oltean 		return false;
187747ed985eSVladimir Oltean 
187847ed985eSVladimir Oltean 	return true;
187947ed985eSVladimir Oltean }
188047ed985eSVladimir Oltean 
18818aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
18828aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
18838aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
1884f3097be2SVladimir Oltean 	.teardown		= sja1105_teardown,
18858456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
1886ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
1887af7cd036SVladimir Oltean 	.phylink_mac_config	= sja1105_mac_config,
1888*8400cff6SVladimir Oltean 	.phylink_mac_link_up	= sja1105_mac_link_up,
1889*8400cff6SVladimir Oltean 	.phylink_mac_link_down	= sja1105_mac_link_down,
189052c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
189152c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
189252c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
1893bb77f36aSVladimir Oltean 	.get_ts_info		= sja1105_get_ts_info,
1894291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
1895291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
1896291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
18978aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
18988aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
1899640f763fSVladimir Oltean 	.port_stp_state_set	= sja1105_bridge_stp_state_set,
19006666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
19016666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
19026666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
19036666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
1904291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
1905291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
1906291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
1907227d07a0SVladimir Oltean 	.port_deferred_xmit	= sja1105_port_deferred_xmit,
1908a602afd2SVladimir Oltean 	.port_hwtstamp_get	= sja1105_hwtstamp_get,
1909a602afd2SVladimir Oltean 	.port_hwtstamp_set	= sja1105_hwtstamp_set,
1910f3097be2SVladimir Oltean 	.port_rxtstamp		= sja1105_port_rxtstamp,
191147ed985eSVladimir Oltean 	.port_txtstamp		= sja1105_port_txtstamp,
19128aa9ebccSVladimir Oltean };
19138aa9ebccSVladimir Oltean 
19148aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
19158aa9ebccSVladimir Oltean {
19168aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
19178aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
19188aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
19198aa9ebccSVladimir Oltean 	u64 device_id;
19208aa9ebccSVladimir Oltean 	u64 part_no;
19218aa9ebccSVladimir Oltean 	int rc;
19228aa9ebccSVladimir Oltean 
19238aa9ebccSVladimir Oltean 	rc = sja1105_spi_send_int(priv, SPI_READ, regs->device_id,
19248aa9ebccSVladimir Oltean 				  &device_id, SJA1105_SIZE_DEVICE_ID);
19258aa9ebccSVladimir Oltean 	if (rc < 0)
19268aa9ebccSVladimir Oltean 		return rc;
19278aa9ebccSVladimir Oltean 
19288aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
19298aa9ebccSVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%llx\n",
19308aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
19318aa9ebccSVladimir Oltean 		return -ENODEV;
19328aa9ebccSVladimir Oltean 	}
19338aa9ebccSVladimir Oltean 
19348aa9ebccSVladimir Oltean 	rc = sja1105_spi_send_packed_buf(priv, SPI_READ, regs->prod_id,
19358aa9ebccSVladimir Oltean 					 prod_id, SJA1105_SIZE_DEVICE_ID);
19368aa9ebccSVladimir Oltean 	if (rc < 0)
19378aa9ebccSVladimir Oltean 		return rc;
19388aa9ebccSVladimir Oltean 
19398aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
19408aa9ebccSVladimir Oltean 
19418aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
19428aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
19438aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
19448aa9ebccSVladimir Oltean 		return -ENODEV;
19458aa9ebccSVladimir Oltean 	}
19468aa9ebccSVladimir Oltean 
19478aa9ebccSVladimir Oltean 	return 0;
19488aa9ebccSVladimir Oltean }
19498aa9ebccSVladimir Oltean 
19508aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
19518aa9ebccSVladimir Oltean {
1952844d7edcSVladimir Oltean 	struct sja1105_tagger_data *tagger_data;
19538aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
19548aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
19558aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
1956227d07a0SVladimir Oltean 	int rc, i;
19578aa9ebccSVladimir Oltean 
19588aa9ebccSVladimir Oltean 	if (!dev->of_node) {
19598aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
19608aa9ebccSVladimir Oltean 		return -EINVAL;
19618aa9ebccSVladimir Oltean 	}
19628aa9ebccSVladimir Oltean 
19638aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
19648aa9ebccSVladimir Oltean 	if (!priv)
19658aa9ebccSVladimir Oltean 		return -ENOMEM;
19668aa9ebccSVladimir Oltean 
19678aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
19688aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
19698aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
19708aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
19718aa9ebccSVladimir Oltean 	else
19728aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
19738aa9ebccSVladimir Oltean 
19748aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
19758aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
19768aa9ebccSVladimir Oltean 	 */
19778aa9ebccSVladimir Oltean 	priv->spidev = spi;
19788aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
19798aa9ebccSVladimir Oltean 
19808aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
19818aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
19828aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
19838aa9ebccSVladimir Oltean 	if (rc < 0) {
19848aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
19858aa9ebccSVladimir Oltean 		return rc;
19868aa9ebccSVladimir Oltean 	}
19878aa9ebccSVladimir Oltean 
19888aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
19898aa9ebccSVladimir Oltean 
19908aa9ebccSVladimir Oltean 	/* Detect hardware device */
19918aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
19928aa9ebccSVladimir Oltean 	if (rc < 0) {
19938aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
19948aa9ebccSVladimir Oltean 		return rc;
19958aa9ebccSVladimir Oltean 	}
19968aa9ebccSVladimir Oltean 
19978aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
19988aa9ebccSVladimir Oltean 
19998aa9ebccSVladimir Oltean 	ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS);
20008aa9ebccSVladimir Oltean 	if (!ds)
20018aa9ebccSVladimir Oltean 		return -ENOMEM;
20028aa9ebccSVladimir Oltean 
20038aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
20048aa9ebccSVladimir Oltean 	ds->priv = priv;
20058aa9ebccSVladimir Oltean 	priv->ds = ds;
20068aa9ebccSVladimir Oltean 
2007844d7edcSVladimir Oltean 	tagger_data = &priv->tagger_data;
2008844d7edcSVladimir Oltean 	skb_queue_head_init(&tagger_data->skb_rxtstamp_queue);
2009f3097be2SVladimir Oltean 	INIT_WORK(&tagger_data->rxtstamp_work, sja1105_rxtstamp_work);
2010844d7edcSVladimir Oltean 
2011227d07a0SVladimir Oltean 	/* Connections between dsa_port and sja1105_port */
2012227d07a0SVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
2013227d07a0SVladimir Oltean 		struct sja1105_port *sp = &priv->ports[i];
2014227d07a0SVladimir Oltean 
2015227d07a0SVladimir Oltean 		ds->ports[i].priv = sp;
2016227d07a0SVladimir Oltean 		sp->dp = &ds->ports[i];
2017844d7edcSVladimir Oltean 		sp->data = tagger_data;
2018227d07a0SVladimir Oltean 	}
2019227d07a0SVladimir Oltean 	mutex_init(&priv->mgmt_lock);
2020227d07a0SVladimir Oltean 
20218aa9ebccSVladimir Oltean 	return dsa_register_switch(priv->ds);
20228aa9ebccSVladimir Oltean }
20238aa9ebccSVladimir Oltean 
20248aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
20258aa9ebccSVladimir Oltean {
20268aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
20278aa9ebccSVladimir Oltean 
2028bb77f36aSVladimir Oltean 	sja1105_ptp_clock_unregister(priv);
20298aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
20308aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
20318aa9ebccSVladimir Oltean 	return 0;
20328aa9ebccSVladimir Oltean }
20338aa9ebccSVladimir Oltean 
20348aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
20358aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
20368aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
20378aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
20388aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
20398aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
20408aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
20418aa9ebccSVladimir Oltean 	{ /* sentinel */ },
20428aa9ebccSVladimir Oltean };
20438aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
20448aa9ebccSVladimir Oltean 
20458aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
20468aa9ebccSVladimir Oltean 	.driver = {
20478aa9ebccSVladimir Oltean 		.name  = "sja1105",
20488aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
20498aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
20508aa9ebccSVladimir Oltean 	},
20518aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
20528aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
20538aa9ebccSVladimir Oltean };
20548aa9ebccSVladimir Oltean 
20558aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
20568aa9ebccSVladimir Oltean 
20578aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
20588aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
20598aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
20608aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
2061