xref: /openbmc/linux/drivers/net/dsa/sja1105/sja1105_main.c (revision ad9f299a87775d4417534251352d02c83da57326)
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>
14*ad9f299aSVladimir 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>
238aa9ebccSVladimir Oltean #include "sja1105.h"
248aa9ebccSVladimir Oltean 
258aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len,
268aa9ebccSVladimir Oltean 			     unsigned int startup_delay)
278aa9ebccSVladimir Oltean {
288aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 1);
298aa9ebccSVladimir Oltean 	/* Wait for minimum reset pulse length */
308aa9ebccSVladimir Oltean 	msleep(pulse_len);
318aa9ebccSVladimir Oltean 	gpiod_set_value_cansleep(gpio, 0);
328aa9ebccSVladimir Oltean 	/* Wait until chip is ready after reset */
338aa9ebccSVladimir Oltean 	msleep(startup_delay);
348aa9ebccSVladimir Oltean }
358aa9ebccSVladimir Oltean 
368aa9ebccSVladimir Oltean static void
378aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd,
388aa9ebccSVladimir Oltean 			   int from, int to, bool allow)
398aa9ebccSVladimir Oltean {
408aa9ebccSVladimir Oltean 	if (allow) {
418aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  |= BIT(to);
428aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port |= BIT(to);
438aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  |= BIT(to);
448aa9ebccSVladimir Oltean 	} else {
458aa9ebccSVladimir Oltean 		l2_fwd[from].bc_domain  &= ~BIT(to);
468aa9ebccSVladimir Oltean 		l2_fwd[from].reach_port &= ~BIT(to);
478aa9ebccSVladimir Oltean 		l2_fwd[from].fl_domain  &= ~BIT(to);
488aa9ebccSVladimir Oltean 	}
498aa9ebccSVladimir Oltean }
508aa9ebccSVladimir Oltean 
518aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree
528aa9ebccSVladimir Oltean  * settings into sja1105_setup
538aa9ebccSVladimir Oltean  */
548aa9ebccSVladimir Oltean struct sja1105_dt_port {
558aa9ebccSVladimir Oltean 	phy_interface_t phy_mode;
568aa9ebccSVladimir Oltean 	sja1105_mii_role_t role;
578aa9ebccSVladimir Oltean };
588aa9ebccSVladimir Oltean 
598aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv)
608aa9ebccSVladimir Oltean {
618aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry default_mac = {
628aa9ebccSVladimir Oltean 		/* Enable all 8 priority queues on egress.
638aa9ebccSVladimir Oltean 		 * Every queue i holds top[i] - base[i] frames.
648aa9ebccSVladimir Oltean 		 * Sum of top[i] - base[i] is 511 (max hardware limit).
658aa9ebccSVladimir Oltean 		 */
668aa9ebccSVladimir Oltean 		.top  = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF},
678aa9ebccSVladimir Oltean 		.base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0},
688aa9ebccSVladimir Oltean 		.enabled = {true, true, true, true, true, true, true, true},
698aa9ebccSVladimir Oltean 		/* Keep standard IFG of 12 bytes on egress. */
708aa9ebccSVladimir Oltean 		.ifg = 0,
718aa9ebccSVladimir Oltean 		/* Always put the MAC speed in automatic mode, where it can be
728aa9ebccSVladimir Oltean 		 * retrieved from the PHY object through phylib and
738aa9ebccSVladimir Oltean 		 * sja1105_adjust_port_config.
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,
948aa9ebccSVladimir Oltean 		/* Enable learning and I/O on user ports by default. */
958aa9ebccSVladimir Oltean 		.dyn_learn = true,
968aa9ebccSVladimir Oltean 		.egress = false,
978aa9ebccSVladimir Oltean 		.ingress = false,
988aa9ebccSVladimir Oltean 	};
998aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
1008aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1018aa9ebccSVladimir Oltean 	int i;
1028aa9ebccSVladimir Oltean 
1038aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG];
1048aa9ebccSVladimir Oltean 
1058aa9ebccSVladimir Oltean 	/* Discard previous MAC Configuration Table */
1068aa9ebccSVladimir Oltean 	if (table->entry_count) {
1078aa9ebccSVladimir Oltean 		kfree(table->entries);
1088aa9ebccSVladimir Oltean 		table->entry_count = 0;
1098aa9ebccSVladimir Oltean 	}
1108aa9ebccSVladimir Oltean 
1118aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_NUM_PORTS,
1128aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1138aa9ebccSVladimir Oltean 	if (!table->entries)
1148aa9ebccSVladimir Oltean 		return -ENOMEM;
1158aa9ebccSVladimir Oltean 
1168aa9ebccSVladimir Oltean 	/* Override table based on phylib DT bindings */
1178aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_NUM_PORTS;
1188aa9ebccSVladimir Oltean 
1198aa9ebccSVladimir Oltean 	mac = table->entries;
1208aa9ebccSVladimir Oltean 
1218aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++)
1228aa9ebccSVladimir Oltean 		mac[i] = default_mac;
1238aa9ebccSVladimir Oltean 
1248aa9ebccSVladimir Oltean 	return 0;
1258aa9ebccSVladimir Oltean }
1268aa9ebccSVladimir Oltean 
1278aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv,
1288aa9ebccSVladimir Oltean 				     struct sja1105_dt_port *ports)
1298aa9ebccSVladimir Oltean {
1308aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
1318aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
1328aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1338aa9ebccSVladimir Oltean 	int i;
1348aa9ebccSVladimir Oltean 
1358aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS];
1368aa9ebccSVladimir Oltean 
1378aa9ebccSVladimir Oltean 	/* Discard previous xMII Mode Parameters Table */
1388aa9ebccSVladimir Oltean 	if (table->entry_count) {
1398aa9ebccSVladimir Oltean 		kfree(table->entries);
1408aa9ebccSVladimir Oltean 		table->entry_count = 0;
1418aa9ebccSVladimir Oltean 	}
1428aa9ebccSVladimir Oltean 
1438aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT,
1448aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
1458aa9ebccSVladimir Oltean 	if (!table->entries)
1468aa9ebccSVladimir Oltean 		return -ENOMEM;
1478aa9ebccSVladimir Oltean 
1488aa9ebccSVladimir Oltean 	/* Override table based on phylib DT bindings */
1498aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT;
1508aa9ebccSVladimir Oltean 
1518aa9ebccSVladimir Oltean 	mii = table->entries;
1528aa9ebccSVladimir Oltean 
1538aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
1548aa9ebccSVladimir Oltean 		switch (ports[i].phy_mode) {
1558aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_MII:
1568aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_MII;
1578aa9ebccSVladimir Oltean 			break;
1588aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RMII:
1598aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RMII;
1608aa9ebccSVladimir Oltean 			break;
1618aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII:
1628aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_ID:
1638aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_RXID:
1648aa9ebccSVladimir Oltean 		case PHY_INTERFACE_MODE_RGMII_TXID:
1658aa9ebccSVladimir Oltean 			mii->xmii_mode[i] = XMII_MODE_RGMII;
1668aa9ebccSVladimir Oltean 			break;
1678aa9ebccSVladimir Oltean 		default:
1688aa9ebccSVladimir Oltean 			dev_err(dev, "Unsupported PHY mode %s!\n",
1698aa9ebccSVladimir Oltean 				phy_modes(ports[i].phy_mode));
1708aa9ebccSVladimir Oltean 		}
1718aa9ebccSVladimir Oltean 
1728aa9ebccSVladimir Oltean 		mii->phy_mac[i] = ports[i].role;
1738aa9ebccSVladimir Oltean 	}
1748aa9ebccSVladimir Oltean 	return 0;
1758aa9ebccSVladimir Oltean }
1768aa9ebccSVladimir Oltean 
1778aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv)
1788aa9ebccSVladimir Oltean {
1798aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1808aa9ebccSVladimir Oltean 
1818aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP];
1828aa9ebccSVladimir Oltean 
183291d1e72SVladimir Oltean 	/* We only populate the FDB table through dynamic
184291d1e72SVladimir Oltean 	 * L2 Address Lookup entries
185291d1e72SVladimir Oltean 	 */
1868aa9ebccSVladimir Oltean 	if (table->entry_count) {
1878aa9ebccSVladimir Oltean 		kfree(table->entries);
1888aa9ebccSVladimir Oltean 		table->entry_count = 0;
1898aa9ebccSVladimir Oltean 	}
1908aa9ebccSVladimir Oltean 	return 0;
1918aa9ebccSVladimir Oltean }
1928aa9ebccSVladimir Oltean 
1938aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv)
1948aa9ebccSVladimir Oltean {
1958aa9ebccSVladimir Oltean 	struct sja1105_table *table;
1968aa9ebccSVladimir Oltean 	struct sja1105_l2_lookup_params_entry default_l2_lookup_params = {
1978456721dSVladimir Oltean 		/* Learned FDB entries are forgotten after 300 seconds */
1988456721dSVladimir Oltean 		.maxage = SJA1105_AGEING_TIME_MS(300000),
1998aa9ebccSVladimir Oltean 		/* All entries within a FDB bin are available for learning */
2008aa9ebccSVladimir Oltean 		.dyn_tbsz = SJA1105ET_FDB_BIN_SIZE,
2018aa9ebccSVladimir Oltean 		/* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */
2028aa9ebccSVladimir Oltean 		.poly = 0x97,
2038aa9ebccSVladimir Oltean 		/* This selects between Independent VLAN Learning (IVL) and
2048aa9ebccSVladimir Oltean 		 * Shared VLAN Learning (SVL)
2058aa9ebccSVladimir Oltean 		 */
2068aa9ebccSVladimir Oltean 		.shared_learn = false,
2078aa9ebccSVladimir Oltean 		/* Don't discard management traffic based on ENFPORT -
2088aa9ebccSVladimir Oltean 		 * we don't perform SMAC port enforcement anyway, so
2098aa9ebccSVladimir Oltean 		 * what we are setting here doesn't matter.
2108aa9ebccSVladimir Oltean 		 */
2118aa9ebccSVladimir Oltean 		.no_enf_hostprt = false,
2128aa9ebccSVladimir Oltean 		/* Don't learn SMAC for mac_fltres1 and mac_fltres0.
2138aa9ebccSVladimir Oltean 		 * Maybe correlate with no_linklocal_learn from bridge driver?
2148aa9ebccSVladimir Oltean 		 */
2158aa9ebccSVladimir Oltean 		.no_mgmt_learn = true,
2168aa9ebccSVladimir Oltean 	};
2178aa9ebccSVladimir Oltean 
2188aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
2198aa9ebccSVladimir Oltean 
2208aa9ebccSVladimir Oltean 	if (table->entry_count) {
2218aa9ebccSVladimir Oltean 		kfree(table->entries);
2228aa9ebccSVladimir Oltean 		table->entry_count = 0;
2238aa9ebccSVladimir Oltean 	}
2248aa9ebccSVladimir Oltean 
2258aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT,
2268aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
2278aa9ebccSVladimir Oltean 	if (!table->entries)
2288aa9ebccSVladimir Oltean 		return -ENOMEM;
2298aa9ebccSVladimir Oltean 
2308aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT;
2318aa9ebccSVladimir Oltean 
2328aa9ebccSVladimir Oltean 	/* This table only has a single entry */
2338aa9ebccSVladimir Oltean 	((struct sja1105_l2_lookup_params_entry *)table->entries)[0] =
2348aa9ebccSVladimir Oltean 				default_l2_lookup_params;
2358aa9ebccSVladimir Oltean 
2368aa9ebccSVladimir Oltean 	return 0;
2378aa9ebccSVladimir Oltean }
2388aa9ebccSVladimir Oltean 
2398aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv)
2408aa9ebccSVladimir Oltean {
2418aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2428aa9ebccSVladimir Oltean 	struct sja1105_vlan_lookup_entry pvid = {
2438aa9ebccSVladimir Oltean 		.ving_mirr = 0,
2448aa9ebccSVladimir Oltean 		.vegr_mirr = 0,
2458aa9ebccSVladimir Oltean 		.vmemb_port = 0,
2468aa9ebccSVladimir Oltean 		.vlan_bc = 0,
2478aa9ebccSVladimir Oltean 		.tag_port = 0,
2488aa9ebccSVladimir Oltean 		.vlanid = 0,
2498aa9ebccSVladimir Oltean 	};
2508aa9ebccSVladimir Oltean 	int i;
2518aa9ebccSVladimir Oltean 
2528aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
2538aa9ebccSVladimir Oltean 
2548aa9ebccSVladimir Oltean 	/* The static VLAN table will only contain the initial pvid of 0.
2556666cebcSVladimir Oltean 	 * All other VLANs are to be configured through dynamic entries,
2566666cebcSVladimir Oltean 	 * and kept in the static configuration table as backing memory.
2576666cebcSVladimir Oltean 	 * The pvid of 0 is sufficient to pass traffic while the ports are
2586666cebcSVladimir Oltean 	 * standalone and when vlan_filtering is disabled. When filtering
2596666cebcSVladimir Oltean 	 * gets enabled, the switchdev core sets up the VLAN ID 1 and sets
2606666cebcSVladimir Oltean 	 * it as the new pvid. Actually 'pvid 1' still comes up in 'bridge
2616666cebcSVladimir Oltean 	 * vlan' even when vlan_filtering is off, but it has no effect.
2628aa9ebccSVladimir Oltean 	 */
2638aa9ebccSVladimir Oltean 	if (table->entry_count) {
2648aa9ebccSVladimir Oltean 		kfree(table->entries);
2658aa9ebccSVladimir Oltean 		table->entry_count = 0;
2668aa9ebccSVladimir Oltean 	}
2678aa9ebccSVladimir Oltean 
2688aa9ebccSVladimir Oltean 	table->entries = kcalloc(1, table->ops->unpacked_entry_size,
2698aa9ebccSVladimir Oltean 				 GFP_KERNEL);
2708aa9ebccSVladimir Oltean 	if (!table->entries)
2718aa9ebccSVladimir Oltean 		return -ENOMEM;
2728aa9ebccSVladimir Oltean 
2738aa9ebccSVladimir Oltean 	table->entry_count = 1;
2748aa9ebccSVladimir Oltean 
2758aa9ebccSVladimir Oltean 	/* VLAN ID 0: all DT-defined ports are members; no restrictions on
2768aa9ebccSVladimir Oltean 	 * forwarding; always transmit priority-tagged frames as untagged.
2778aa9ebccSVladimir Oltean 	 */
2788aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
2798aa9ebccSVladimir Oltean 		pvid.vmemb_port |= BIT(i);
2808aa9ebccSVladimir Oltean 		pvid.vlan_bc |= BIT(i);
2818aa9ebccSVladimir Oltean 		pvid.tag_port &= ~BIT(i);
2828aa9ebccSVladimir Oltean 	}
2838aa9ebccSVladimir Oltean 
2848aa9ebccSVladimir Oltean 	((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid;
2858aa9ebccSVladimir Oltean 	return 0;
2868aa9ebccSVladimir Oltean }
2878aa9ebccSVladimir Oltean 
2888aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv)
2898aa9ebccSVladimir Oltean {
2908aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2fwd;
2918aa9ebccSVladimir Oltean 	struct sja1105_table *table;
2928aa9ebccSVladimir Oltean 	int i, j;
2938aa9ebccSVladimir Oltean 
2948aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING];
2958aa9ebccSVladimir Oltean 
2968aa9ebccSVladimir Oltean 	if (table->entry_count) {
2978aa9ebccSVladimir Oltean 		kfree(table->entries);
2988aa9ebccSVladimir Oltean 		table->entry_count = 0;
2998aa9ebccSVladimir Oltean 	}
3008aa9ebccSVladimir Oltean 
3018aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT,
3028aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3038aa9ebccSVladimir Oltean 	if (!table->entries)
3048aa9ebccSVladimir Oltean 		return -ENOMEM;
3058aa9ebccSVladimir Oltean 
3068aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT;
3078aa9ebccSVladimir Oltean 
3088aa9ebccSVladimir Oltean 	l2fwd = table->entries;
3098aa9ebccSVladimir Oltean 
3108aa9ebccSVladimir Oltean 	/* First 5 entries define the forwarding rules */
3118aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
3128aa9ebccSVladimir Oltean 		unsigned int upstream = dsa_upstream_port(priv->ds, i);
3138aa9ebccSVladimir Oltean 
3148aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++)
3158aa9ebccSVladimir Oltean 			l2fwd[i].vlan_pmap[j] = j;
3168aa9ebccSVladimir Oltean 
3178aa9ebccSVladimir Oltean 		if (i == upstream)
3188aa9ebccSVladimir Oltean 			continue;
3198aa9ebccSVladimir Oltean 
3208aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, i, upstream, true);
3218aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2fwd, upstream, i, true);
3228aa9ebccSVladimir Oltean 	}
3238aa9ebccSVladimir Oltean 	/* Next 8 entries define VLAN PCP mapping from ingress to egress.
3248aa9ebccSVladimir Oltean 	 * Create a one-to-one mapping.
3258aa9ebccSVladimir Oltean 	 */
3268aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_TC; i++)
3278aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_PORTS; j++)
3288aa9ebccSVladimir Oltean 			l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i;
3298aa9ebccSVladimir Oltean 
3308aa9ebccSVladimir Oltean 	return 0;
3318aa9ebccSVladimir Oltean }
3328aa9ebccSVladimir Oltean 
3338aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv)
3348aa9ebccSVladimir Oltean {
3358aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_params_entry default_l2fwd_params = {
3368aa9ebccSVladimir Oltean 		/* Disallow dynamic reconfiguration of vlan_pmap */
3378aa9ebccSVladimir Oltean 		.max_dynp = 0,
3388aa9ebccSVladimir Oltean 		/* Use a single memory partition for all ingress queues */
3398aa9ebccSVladimir Oltean 		.part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 },
3408aa9ebccSVladimir Oltean 	};
3418aa9ebccSVladimir Oltean 	struct sja1105_table *table;
3428aa9ebccSVladimir Oltean 
3438aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS];
3448aa9ebccSVladimir Oltean 
3458aa9ebccSVladimir Oltean 	if (table->entry_count) {
3468aa9ebccSVladimir Oltean 		kfree(table->entries);
3478aa9ebccSVladimir Oltean 		table->entry_count = 0;
3488aa9ebccSVladimir Oltean 	}
3498aa9ebccSVladimir Oltean 
3508aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT,
3518aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
3528aa9ebccSVladimir Oltean 	if (!table->entries)
3538aa9ebccSVladimir Oltean 		return -ENOMEM;
3548aa9ebccSVladimir Oltean 
3558aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT;
3568aa9ebccSVladimir Oltean 
3578aa9ebccSVladimir Oltean 	/* This table only has a single entry */
3588aa9ebccSVladimir Oltean 	((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] =
3598aa9ebccSVladimir Oltean 				default_l2fwd_params;
3608aa9ebccSVladimir Oltean 
3618aa9ebccSVladimir Oltean 	return 0;
3628aa9ebccSVladimir Oltean }
3638aa9ebccSVladimir Oltean 
3648aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv)
3658aa9ebccSVladimir Oltean {
3668aa9ebccSVladimir Oltean 	struct sja1105_general_params_entry default_general_params = {
3678aa9ebccSVladimir Oltean 		/* Disallow dynamic changing of the mirror port */
3688aa9ebccSVladimir Oltean 		.mirr_ptacu = 0,
3698aa9ebccSVladimir Oltean 		.switchid = priv->ds->index,
3708aa9ebccSVladimir Oltean 		/* Priority queue for link-local frames trapped to CPU */
3718aa9ebccSVladimir Oltean 		.hostprio = 0,
3728aa9ebccSVladimir Oltean 		.mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A,
3738aa9ebccSVladimir Oltean 		.mac_flt1    = SJA1105_LINKLOCAL_FILTER_A_MASK,
3748aa9ebccSVladimir Oltean 		.incl_srcpt1 = true,
3758aa9ebccSVladimir Oltean 		.send_meta1  = false,
3768aa9ebccSVladimir Oltean 		.mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B,
3778aa9ebccSVladimir Oltean 		.mac_flt0    = SJA1105_LINKLOCAL_FILTER_B_MASK,
3788aa9ebccSVladimir Oltean 		.incl_srcpt0 = true,
3798aa9ebccSVladimir Oltean 		.send_meta0  = false,
3808aa9ebccSVladimir Oltean 		/* The destination for traffic matching mac_fltres1 and
3818aa9ebccSVladimir Oltean 		 * mac_fltres0 on all ports except host_port. Such traffic
3828aa9ebccSVladimir Oltean 		 * receieved on host_port itself would be dropped, except
3838aa9ebccSVladimir Oltean 		 * by installing a temporary 'management route'
3848aa9ebccSVladimir Oltean 		 */
3858aa9ebccSVladimir Oltean 		.host_port = dsa_upstream_port(priv->ds, 0),
3868aa9ebccSVladimir Oltean 		/* Same as host port */
3878aa9ebccSVladimir Oltean 		.mirr_port = dsa_upstream_port(priv->ds, 0),
3888aa9ebccSVladimir Oltean 		/* Link-local traffic received on casc_port will be forwarded
3898aa9ebccSVladimir Oltean 		 * to host_port without embedding the source port and device ID
3908aa9ebccSVladimir Oltean 		 * info in the destination MAC address (presumably because it
3918aa9ebccSVladimir Oltean 		 * is a cascaded port and a downstream SJA switch already did
3928aa9ebccSVladimir Oltean 		 * that). Default to an invalid port (to disable the feature)
3938aa9ebccSVladimir Oltean 		 * and overwrite this if we find any DSA (cascaded) ports.
3948aa9ebccSVladimir Oltean 		 */
3958aa9ebccSVladimir Oltean 		.casc_port = SJA1105_NUM_PORTS,
3968aa9ebccSVladimir Oltean 		/* No TTEthernet */
3978aa9ebccSVladimir Oltean 		.vllupformat = 0,
3988aa9ebccSVladimir Oltean 		.vlmarker = 0,
3998aa9ebccSVladimir Oltean 		.vlmask = 0,
4008aa9ebccSVladimir Oltean 		/* Only update correctionField for 1-step PTP (L2 transport) */
4018aa9ebccSVladimir Oltean 		.ignore2stf = 0,
4026666cebcSVladimir Oltean 		/* Forcefully disable VLAN filtering by telling
4036666cebcSVladimir Oltean 		 * the switch that VLAN has a different EtherType.
4046666cebcSVladimir Oltean 		 */
4056666cebcSVladimir Oltean 		.tpid = ETH_P_SJA1105,
4066666cebcSVladimir Oltean 		.tpid2 = ETH_P_SJA1105,
4078aa9ebccSVladimir Oltean 	};
4088aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4098aa9ebccSVladimir Oltean 	int i;
4108aa9ebccSVladimir Oltean 
4118aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++)
4128aa9ebccSVladimir Oltean 		if (dsa_is_dsa_port(priv->ds, i))
4138aa9ebccSVladimir Oltean 			default_general_params.casc_port = i;
4148aa9ebccSVladimir Oltean 
4158aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
4168aa9ebccSVladimir Oltean 
4178aa9ebccSVladimir Oltean 	if (table->entry_count) {
4188aa9ebccSVladimir Oltean 		kfree(table->entries);
4198aa9ebccSVladimir Oltean 		table->entry_count = 0;
4208aa9ebccSVladimir Oltean 	}
4218aa9ebccSVladimir Oltean 
4228aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT,
4238aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4248aa9ebccSVladimir Oltean 	if (!table->entries)
4258aa9ebccSVladimir Oltean 		return -ENOMEM;
4268aa9ebccSVladimir Oltean 
4278aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT;
4288aa9ebccSVladimir Oltean 
4298aa9ebccSVladimir Oltean 	/* This table only has a single entry */
4308aa9ebccSVladimir Oltean 	((struct sja1105_general_params_entry *)table->entries)[0] =
4318aa9ebccSVladimir Oltean 				default_general_params;
4328aa9ebccSVladimir Oltean 
4338aa9ebccSVladimir Oltean 	return 0;
4348aa9ebccSVladimir Oltean }
4358aa9ebccSVladimir Oltean 
4368aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000)
4378aa9ebccSVladimir Oltean 
4388aa9ebccSVladimir Oltean static inline void
4398aa9ebccSVladimir Oltean sja1105_setup_policer(struct sja1105_l2_policing_entry *policing,
4408aa9ebccSVladimir Oltean 		      int index)
4418aa9ebccSVladimir Oltean {
4428aa9ebccSVladimir Oltean 	policing[index].sharindx = index;
4438aa9ebccSVladimir Oltean 	policing[index].smax = 65535; /* Burst size in bytes */
4448aa9ebccSVladimir Oltean 	policing[index].rate = SJA1105_RATE_MBPS(1000);
4458aa9ebccSVladimir Oltean 	policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN;
4468aa9ebccSVladimir Oltean 	policing[index].partition = 0;
4478aa9ebccSVladimir Oltean }
4488aa9ebccSVladimir Oltean 
4498aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv)
4508aa9ebccSVladimir Oltean {
4518aa9ebccSVladimir Oltean 	struct sja1105_l2_policing_entry *policing;
4528aa9ebccSVladimir Oltean 	struct sja1105_table *table;
4538aa9ebccSVladimir Oltean 	int i, j, k;
4548aa9ebccSVladimir Oltean 
4558aa9ebccSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_POLICING];
4568aa9ebccSVladimir Oltean 
4578aa9ebccSVladimir Oltean 	/* Discard previous L2 Policing Table */
4588aa9ebccSVladimir Oltean 	if (table->entry_count) {
4598aa9ebccSVladimir Oltean 		kfree(table->entries);
4608aa9ebccSVladimir Oltean 		table->entry_count = 0;
4618aa9ebccSVladimir Oltean 	}
4628aa9ebccSVladimir Oltean 
4638aa9ebccSVladimir Oltean 	table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT,
4648aa9ebccSVladimir Oltean 				 table->ops->unpacked_entry_size, GFP_KERNEL);
4658aa9ebccSVladimir Oltean 	if (!table->entries)
4668aa9ebccSVladimir Oltean 		return -ENOMEM;
4678aa9ebccSVladimir Oltean 
4688aa9ebccSVladimir Oltean 	table->entry_count = SJA1105_MAX_L2_POLICING_COUNT;
4698aa9ebccSVladimir Oltean 
4708aa9ebccSVladimir Oltean 	policing = table->entries;
4718aa9ebccSVladimir Oltean 
4728aa9ebccSVladimir Oltean 	/* k sweeps through all unicast policers (0-39).
4738aa9ebccSVladimir Oltean 	 * bcast sweeps through policers 40-44.
4748aa9ebccSVladimir Oltean 	 */
4758aa9ebccSVladimir Oltean 	for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) {
4768aa9ebccSVladimir Oltean 		int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i;
4778aa9ebccSVladimir Oltean 
4788aa9ebccSVladimir Oltean 		for (j = 0; j < SJA1105_NUM_TC; j++, k++)
4798aa9ebccSVladimir Oltean 			sja1105_setup_policer(policing, k);
4808aa9ebccSVladimir Oltean 
4818aa9ebccSVladimir Oltean 		/* Set up this port's policer for broadcast traffic */
4828aa9ebccSVladimir Oltean 		sja1105_setup_policer(policing, bcast);
4838aa9ebccSVladimir Oltean 	}
4848aa9ebccSVladimir Oltean 	return 0;
4858aa9ebccSVladimir Oltean }
4868aa9ebccSVladimir Oltean 
4878aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv,
4888aa9ebccSVladimir Oltean 				      struct sja1105_dt_port *ports)
4898aa9ebccSVladimir Oltean {
4908aa9ebccSVladimir Oltean 	int rc;
4918aa9ebccSVladimir Oltean 
4928aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
4938aa9ebccSVladimir Oltean 	rc = sja1105_static_config_init(&priv->static_config,
4948aa9ebccSVladimir Oltean 					priv->info->static_ops,
4958aa9ebccSVladimir Oltean 					priv->info->device_id);
4968aa9ebccSVladimir Oltean 	if (rc)
4978aa9ebccSVladimir Oltean 		return rc;
4988aa9ebccSVladimir Oltean 
4998aa9ebccSVladimir Oltean 	/* Build static configuration */
5008aa9ebccSVladimir Oltean 	rc = sja1105_init_mac_settings(priv);
5018aa9ebccSVladimir Oltean 	if (rc < 0)
5028aa9ebccSVladimir Oltean 		return rc;
5038aa9ebccSVladimir Oltean 	rc = sja1105_init_mii_settings(priv, ports);
5048aa9ebccSVladimir Oltean 	if (rc < 0)
5058aa9ebccSVladimir Oltean 		return rc;
5068aa9ebccSVladimir Oltean 	rc = sja1105_init_static_fdb(priv);
5078aa9ebccSVladimir Oltean 	if (rc < 0)
5088aa9ebccSVladimir Oltean 		return rc;
5098aa9ebccSVladimir Oltean 	rc = sja1105_init_static_vlan(priv);
5108aa9ebccSVladimir Oltean 	if (rc < 0)
5118aa9ebccSVladimir Oltean 		return rc;
5128aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_lookup_params(priv);
5138aa9ebccSVladimir Oltean 	if (rc < 0)
5148aa9ebccSVladimir Oltean 		return rc;
5158aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding(priv);
5168aa9ebccSVladimir Oltean 	if (rc < 0)
5178aa9ebccSVladimir Oltean 		return rc;
5188aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_forwarding_params(priv);
5198aa9ebccSVladimir Oltean 	if (rc < 0)
5208aa9ebccSVladimir Oltean 		return rc;
5218aa9ebccSVladimir Oltean 	rc = sja1105_init_l2_policing(priv);
5228aa9ebccSVladimir Oltean 	if (rc < 0)
5238aa9ebccSVladimir Oltean 		return rc;
5248aa9ebccSVladimir Oltean 	rc = sja1105_init_general_params(priv);
5258aa9ebccSVladimir Oltean 	if (rc < 0)
5268aa9ebccSVladimir Oltean 		return rc;
5278aa9ebccSVladimir Oltean 
5288aa9ebccSVladimir Oltean 	/* Send initial configuration to hardware via SPI */
5298aa9ebccSVladimir Oltean 	return sja1105_static_config_upload(priv);
5308aa9ebccSVladimir Oltean }
5318aa9ebccSVladimir Oltean 
532f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv,
533f5b8631cSVladimir Oltean 				      const struct sja1105_dt_port *ports)
534f5b8631cSVladimir Oltean {
535f5b8631cSVladimir Oltean 	int i;
536f5b8631cSVladimir Oltean 
537f5b8631cSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
538f5b8631cSVladimir Oltean 		if (ports->role == XMII_MAC)
539f5b8631cSVladimir Oltean 			continue;
540f5b8631cSVladimir Oltean 
541f5b8631cSVladimir Oltean 		if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
542f5b8631cSVladimir Oltean 		    ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
543f5b8631cSVladimir Oltean 			priv->rgmii_rx_delay[i] = true;
544f5b8631cSVladimir Oltean 
545f5b8631cSVladimir Oltean 		if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
546f5b8631cSVladimir Oltean 		    ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
547f5b8631cSVladimir Oltean 			priv->rgmii_tx_delay[i] = true;
548f5b8631cSVladimir Oltean 
549f5b8631cSVladimir Oltean 		if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) &&
550f5b8631cSVladimir Oltean 		     !priv->info->setup_rgmii_delay)
551f5b8631cSVladimir Oltean 			return -EINVAL;
552f5b8631cSVladimir Oltean 	}
553f5b8631cSVladimir Oltean 	return 0;
554f5b8631cSVladimir Oltean }
555f5b8631cSVladimir Oltean 
5568aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv,
5578aa9ebccSVladimir Oltean 				    struct sja1105_dt_port *ports,
5588aa9ebccSVladimir Oltean 				    struct device_node *ports_node)
5598aa9ebccSVladimir Oltean {
5608aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
5618aa9ebccSVladimir Oltean 	struct device_node *child;
5628aa9ebccSVladimir Oltean 
5638aa9ebccSVladimir Oltean 	for_each_child_of_node(ports_node, child) {
5648aa9ebccSVladimir Oltean 		struct device_node *phy_node;
5658aa9ebccSVladimir Oltean 		int phy_mode;
5668aa9ebccSVladimir Oltean 		u32 index;
5678aa9ebccSVladimir Oltean 
5688aa9ebccSVladimir Oltean 		/* Get switch port number from DT */
5698aa9ebccSVladimir Oltean 		if (of_property_read_u32(child, "reg", &index) < 0) {
5708aa9ebccSVladimir Oltean 			dev_err(dev, "Port number not defined in device tree "
5718aa9ebccSVladimir Oltean 				"(property \"reg\")\n");
5728aa9ebccSVladimir Oltean 			return -ENODEV;
5738aa9ebccSVladimir Oltean 		}
5748aa9ebccSVladimir Oltean 
5758aa9ebccSVladimir Oltean 		/* Get PHY mode from DT */
5768aa9ebccSVladimir Oltean 		phy_mode = of_get_phy_mode(child);
5778aa9ebccSVladimir Oltean 		if (phy_mode < 0) {
5788aa9ebccSVladimir Oltean 			dev_err(dev, "Failed to read phy-mode or "
5798aa9ebccSVladimir Oltean 				"phy-interface-type property for port %d\n",
5808aa9ebccSVladimir Oltean 				index);
5818aa9ebccSVladimir Oltean 			return -ENODEV;
5828aa9ebccSVladimir Oltean 		}
5838aa9ebccSVladimir Oltean 		ports[index].phy_mode = phy_mode;
5848aa9ebccSVladimir Oltean 
5858aa9ebccSVladimir Oltean 		phy_node = of_parse_phandle(child, "phy-handle", 0);
5868aa9ebccSVladimir Oltean 		if (!phy_node) {
5878aa9ebccSVladimir Oltean 			if (!of_phy_is_fixed_link(child)) {
5888aa9ebccSVladimir Oltean 				dev_err(dev, "phy-handle or fixed-link "
5898aa9ebccSVladimir Oltean 					"properties missing!\n");
5908aa9ebccSVladimir Oltean 				return -ENODEV;
5918aa9ebccSVladimir Oltean 			}
5928aa9ebccSVladimir Oltean 			/* phy-handle is missing, but fixed-link isn't.
5938aa9ebccSVladimir Oltean 			 * So it's a fixed link. Default to PHY role.
5948aa9ebccSVladimir Oltean 			 */
5958aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
5968aa9ebccSVladimir Oltean 		} else {
5978aa9ebccSVladimir Oltean 			/* phy-handle present => put port in MAC role */
5988aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
5998aa9ebccSVladimir Oltean 			of_node_put(phy_node);
6008aa9ebccSVladimir Oltean 		}
6018aa9ebccSVladimir Oltean 
6028aa9ebccSVladimir Oltean 		/* The MAC/PHY role can be overridden with explicit bindings */
6038aa9ebccSVladimir Oltean 		if (of_property_read_bool(child, "sja1105,role-mac"))
6048aa9ebccSVladimir Oltean 			ports[index].role = XMII_MAC;
6058aa9ebccSVladimir Oltean 		else if (of_property_read_bool(child, "sja1105,role-phy"))
6068aa9ebccSVladimir Oltean 			ports[index].role = XMII_PHY;
6078aa9ebccSVladimir Oltean 	}
6088aa9ebccSVladimir Oltean 
6098aa9ebccSVladimir Oltean 	return 0;
6108aa9ebccSVladimir Oltean }
6118aa9ebccSVladimir Oltean 
6128aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv,
6138aa9ebccSVladimir Oltean 			    struct sja1105_dt_port *ports)
6148aa9ebccSVladimir Oltean {
6158aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
6168aa9ebccSVladimir Oltean 	struct device_node *switch_node = dev->of_node;
6178aa9ebccSVladimir Oltean 	struct device_node *ports_node;
6188aa9ebccSVladimir Oltean 	int rc;
6198aa9ebccSVladimir Oltean 
6208aa9ebccSVladimir Oltean 	ports_node = of_get_child_by_name(switch_node, "ports");
6218aa9ebccSVladimir Oltean 	if (!ports_node) {
6228aa9ebccSVladimir Oltean 		dev_err(dev, "Incorrect bindings: absent \"ports\" node\n");
6238aa9ebccSVladimir Oltean 		return -ENODEV;
6248aa9ebccSVladimir Oltean 	}
6258aa9ebccSVladimir Oltean 
6268aa9ebccSVladimir Oltean 	rc = sja1105_parse_ports_node(priv, ports, ports_node);
6278aa9ebccSVladimir Oltean 	of_node_put(ports_node);
6288aa9ebccSVladimir Oltean 
6298aa9ebccSVladimir Oltean 	return rc;
6308aa9ebccSVladimir Oltean }
6318aa9ebccSVladimir Oltean 
6328aa9ebccSVladimir Oltean /* Convert back and forth MAC speed from Mbps to SJA1105 encoding */
6338aa9ebccSVladimir Oltean static int sja1105_speed[] = {
6348aa9ebccSVladimir Oltean 	[SJA1105_SPEED_AUTO]     = 0,
6358aa9ebccSVladimir Oltean 	[SJA1105_SPEED_10MBPS]   = 10,
6368aa9ebccSVladimir Oltean 	[SJA1105_SPEED_100MBPS]  = 100,
6378aa9ebccSVladimir Oltean 	[SJA1105_SPEED_1000MBPS] = 1000,
6388aa9ebccSVladimir Oltean };
6398aa9ebccSVladimir Oltean 
6408aa9ebccSVladimir Oltean static sja1105_speed_t sja1105_get_speed_cfg(unsigned int speed_mbps)
6418aa9ebccSVladimir Oltean {
6428aa9ebccSVladimir Oltean 	int i;
6438aa9ebccSVladimir Oltean 
6448aa9ebccSVladimir Oltean 	for (i = SJA1105_SPEED_AUTO; i <= SJA1105_SPEED_1000MBPS; i++)
6458aa9ebccSVladimir Oltean 		if (sja1105_speed[i] == speed_mbps)
6468aa9ebccSVladimir Oltean 			return i;
6478aa9ebccSVladimir Oltean 	return -EINVAL;
6488aa9ebccSVladimir Oltean }
6498aa9ebccSVladimir Oltean 
6508aa9ebccSVladimir Oltean /* Set link speed and enable/disable traffic I/O in the MAC configuration
6518aa9ebccSVladimir Oltean  * for a specific port.
6528aa9ebccSVladimir Oltean  *
6538aa9ebccSVladimir Oltean  * @speed_mbps: If 0, leave the speed unchanged, else adapt MAC to PHY speed.
6548aa9ebccSVladimir Oltean  * @enabled: Manage Rx and Tx settings for this port. Overrides the static
6558aa9ebccSVladimir Oltean  *	     configuration settings.
6568aa9ebccSVladimir Oltean  */
6578aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port,
6588aa9ebccSVladimir Oltean 				      int speed_mbps, bool enabled)
6598aa9ebccSVladimir Oltean {
6608aa9ebccSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
6618aa9ebccSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
6628aa9ebccSVladimir Oltean 	struct device *dev = priv->ds->dev;
6638aa9ebccSVladimir Oltean 	sja1105_phy_interface_t phy_mode;
6648aa9ebccSVladimir Oltean 	sja1105_speed_t speed;
6658aa9ebccSVladimir Oltean 	int rc;
6668aa9ebccSVladimir Oltean 
6678aa9ebccSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
6688aa9ebccSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
6698aa9ebccSVladimir Oltean 
6708aa9ebccSVladimir Oltean 	speed = sja1105_get_speed_cfg(speed_mbps);
6718aa9ebccSVladimir Oltean 	if (speed_mbps && speed < 0) {
6728aa9ebccSVladimir Oltean 		dev_err(dev, "Invalid speed %iMbps\n", speed_mbps);
6738aa9ebccSVladimir Oltean 		return -EINVAL;
6748aa9ebccSVladimir Oltean 	}
6758aa9ebccSVladimir Oltean 
6768aa9ebccSVladimir Oltean 	/* If requested, overwrite SJA1105_SPEED_AUTO from the static MAC
6778aa9ebccSVladimir Oltean 	 * configuration table, since this will be used for the clocking setup,
6788aa9ebccSVladimir Oltean 	 * and we no longer need to store it in the static config (already told
6798aa9ebccSVladimir Oltean 	 * hardware we want auto during upload phase).
6808aa9ebccSVladimir Oltean 	 */
6818aa9ebccSVladimir Oltean 	if (speed_mbps)
6828aa9ebccSVladimir Oltean 		mac[port].speed = speed;
6838aa9ebccSVladimir Oltean 	else
6848aa9ebccSVladimir Oltean 		mac[port].speed = SJA1105_SPEED_AUTO;
6858aa9ebccSVladimir Oltean 
6868aa9ebccSVladimir Oltean 	/* On P/Q/R/S, one can read from the device via the MAC reconfiguration
6878aa9ebccSVladimir Oltean 	 * tables. On E/T, MAC reconfig tables are not readable, only writable.
6888aa9ebccSVladimir Oltean 	 * We have to *know* what the MAC looks like.  For the sake of keeping
6898aa9ebccSVladimir Oltean 	 * the code common, we'll use the static configuration tables as a
6908aa9ebccSVladimir Oltean 	 * reasonable approximation for both E/T and P/Q/R/S.
6918aa9ebccSVladimir Oltean 	 */
6928aa9ebccSVladimir Oltean 	mac[port].ingress = enabled;
6938aa9ebccSVladimir Oltean 	mac[port].egress  = enabled;
6948aa9ebccSVladimir Oltean 
6958aa9ebccSVladimir Oltean 	/* Write to the dynamic reconfiguration tables */
6968aa9ebccSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG,
6978aa9ebccSVladimir Oltean 					  port, &mac[port], true);
6988aa9ebccSVladimir Oltean 	if (rc < 0) {
6998aa9ebccSVladimir Oltean 		dev_err(dev, "Failed to write MAC config: %d\n", rc);
7008aa9ebccSVladimir Oltean 		return rc;
7018aa9ebccSVladimir Oltean 	}
7028aa9ebccSVladimir Oltean 
7038aa9ebccSVladimir Oltean 	/* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at
7048aa9ebccSVladimir Oltean 	 * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and
7058aa9ebccSVladimir Oltean 	 * RMII no change of the clock setup is required. Actually, changing
7068aa9ebccSVladimir Oltean 	 * the clock setup does interrupt the clock signal for a certain time
7078aa9ebccSVladimir Oltean 	 * which causes trouble for all PHYs relying on this signal.
7088aa9ebccSVladimir Oltean 	 */
7098aa9ebccSVladimir Oltean 	if (!enabled)
7108aa9ebccSVladimir Oltean 		return 0;
7118aa9ebccSVladimir Oltean 
7128aa9ebccSVladimir Oltean 	phy_mode = mii->xmii_mode[port];
7138aa9ebccSVladimir Oltean 	if (phy_mode != XMII_MODE_RGMII)
7148aa9ebccSVladimir Oltean 		return 0;
7158aa9ebccSVladimir Oltean 
7168aa9ebccSVladimir Oltean 	return sja1105_clocking_setup_port(priv, port);
7178aa9ebccSVladimir Oltean }
7188aa9ebccSVladimir Oltean 
7198aa9ebccSVladimir Oltean static void sja1105_adjust_link(struct dsa_switch *ds, int port,
7208aa9ebccSVladimir Oltean 				struct phy_device *phydev)
7218aa9ebccSVladimir Oltean {
7228aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
7238aa9ebccSVladimir Oltean 
7248aa9ebccSVladimir Oltean 	if (!phydev->link)
7258aa9ebccSVladimir Oltean 		sja1105_adjust_port_config(priv, port, 0, false);
7268aa9ebccSVladimir Oltean 	else
7278aa9ebccSVladimir Oltean 		sja1105_adjust_port_config(priv, port, phydev->speed, true);
7288aa9ebccSVladimir Oltean }
7298aa9ebccSVladimir Oltean 
730*ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port,
731*ad9f299aSVladimir Oltean 				     unsigned long *supported,
732*ad9f299aSVladimir Oltean 				     struct phylink_link_state *state)
733*ad9f299aSVladimir Oltean {
734*ad9f299aSVladimir Oltean 	/* Construct a new mask which exhaustively contains all link features
735*ad9f299aSVladimir Oltean 	 * supported by the MAC, and then apply that (logical AND) to what will
736*ad9f299aSVladimir Oltean 	 * be sent to the PHY for "marketing".
737*ad9f299aSVladimir Oltean 	 */
738*ad9f299aSVladimir Oltean 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
739*ad9f299aSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
740*ad9f299aSVladimir Oltean 	struct sja1105_xmii_params_entry *mii;
741*ad9f299aSVladimir Oltean 
742*ad9f299aSVladimir Oltean 	mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries;
743*ad9f299aSVladimir Oltean 
744*ad9f299aSVladimir Oltean 	/* The MAC does not support pause frames, and also doesn't
745*ad9f299aSVladimir Oltean 	 * support half-duplex traffic modes.
746*ad9f299aSVladimir Oltean 	 */
747*ad9f299aSVladimir Oltean 	phylink_set(mask, Autoneg);
748*ad9f299aSVladimir Oltean 	phylink_set(mask, MII);
749*ad9f299aSVladimir Oltean 	phylink_set(mask, 10baseT_Full);
750*ad9f299aSVladimir Oltean 	phylink_set(mask, 100baseT_Full);
751*ad9f299aSVladimir Oltean 	if (mii->xmii_mode[port] == XMII_MODE_RGMII)
752*ad9f299aSVladimir Oltean 		phylink_set(mask, 1000baseT_Full);
753*ad9f299aSVladimir Oltean 
754*ad9f299aSVladimir Oltean 	bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS);
755*ad9f299aSVladimir Oltean 	bitmap_and(state->advertising, state->advertising, mask,
756*ad9f299aSVladimir Oltean 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
757*ad9f299aSVladimir Oltean }
758*ad9f299aSVladimir Oltean 
759291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that
760291d1e72SVladimir Oltean  * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of
761291d1e72SVladimir Oltean  * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin).
762291d1e72SVladimir Oltean  * For the placement of a newly learnt FDB entry, the switch selects the bin
763291d1e72SVladimir Oltean  * based on a hash function, and the way within that bin incrementally.
764291d1e72SVladimir Oltean  */
765291d1e72SVladimir Oltean static inline int sja1105et_fdb_index(int bin, int way)
766291d1e72SVladimir Oltean {
767291d1e72SVladimir Oltean 	return bin * SJA1105ET_FDB_BIN_SIZE + way;
768291d1e72SVladimir Oltean }
769291d1e72SVladimir Oltean 
770291d1e72SVladimir Oltean static int sja1105_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin,
771291d1e72SVladimir Oltean 				       const u8 *addr, u16 vid,
772291d1e72SVladimir Oltean 				       struct sja1105_l2_lookup_entry *match,
773291d1e72SVladimir Oltean 				       int *last_unused)
774291d1e72SVladimir Oltean {
775291d1e72SVladimir Oltean 	int way;
776291d1e72SVladimir Oltean 
777291d1e72SVladimir Oltean 	for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) {
778291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
779291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
780291d1e72SVladimir Oltean 
781291d1e72SVladimir Oltean 		/* Skip unused entries, optionally marking them
782291d1e72SVladimir Oltean 		 * into the return value
783291d1e72SVladimir Oltean 		 */
784291d1e72SVladimir Oltean 		if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
785291d1e72SVladimir Oltean 						index, &l2_lookup)) {
786291d1e72SVladimir Oltean 			if (last_unused)
787291d1e72SVladimir Oltean 				*last_unused = way;
788291d1e72SVladimir Oltean 			continue;
789291d1e72SVladimir Oltean 		}
790291d1e72SVladimir Oltean 
791291d1e72SVladimir Oltean 		if (l2_lookup.macaddr == ether_addr_to_u64(addr) &&
792291d1e72SVladimir Oltean 		    l2_lookup.vlanid == vid) {
793291d1e72SVladimir Oltean 			if (match)
794291d1e72SVladimir Oltean 				*match = l2_lookup;
795291d1e72SVladimir Oltean 			return way;
796291d1e72SVladimir Oltean 		}
797291d1e72SVladimir Oltean 	}
798291d1e72SVladimir Oltean 	/* Return an invalid entry index if not found */
799291d1e72SVladimir Oltean 	return -1;
800291d1e72SVladimir Oltean }
801291d1e72SVladimir Oltean 
802291d1e72SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port,
803291d1e72SVladimir Oltean 			   const unsigned char *addr, u16 vid)
804291d1e72SVladimir Oltean {
805291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
806291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
807291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
808291d1e72SVladimir Oltean 	int last_unused = -1;
809291d1e72SVladimir Oltean 	int bin, way;
810291d1e72SVladimir Oltean 
811291d1e72SVladimir Oltean 	bin = sja1105_fdb_hash(priv, addr, vid);
812291d1e72SVladimir Oltean 
813291d1e72SVladimir Oltean 	way = sja1105_is_fdb_entry_in_bin(priv, bin, addr, vid,
814291d1e72SVladimir Oltean 					  &l2_lookup, &last_unused);
815291d1e72SVladimir Oltean 	if (way >= 0) {
816291d1e72SVladimir Oltean 		/* We have an FDB entry. Is our port in the destination
817291d1e72SVladimir Oltean 		 * mask? If yes, we need to do nothing. If not, we need
818291d1e72SVladimir Oltean 		 * to rewrite the entry by adding this port to it.
819291d1e72SVladimir Oltean 		 */
820291d1e72SVladimir Oltean 		if (l2_lookup.destports & BIT(port))
821291d1e72SVladimir Oltean 			return 0;
822291d1e72SVladimir Oltean 		l2_lookup.destports |= BIT(port);
823291d1e72SVladimir Oltean 	} else {
824291d1e72SVladimir Oltean 		int index = sja1105et_fdb_index(bin, way);
825291d1e72SVladimir Oltean 
826291d1e72SVladimir Oltean 		/* We don't have an FDB entry. We construct a new one and
827291d1e72SVladimir Oltean 		 * try to find a place for it within the FDB table.
828291d1e72SVladimir Oltean 		 */
829291d1e72SVladimir Oltean 		l2_lookup.macaddr = ether_addr_to_u64(addr);
830291d1e72SVladimir Oltean 		l2_lookup.destports = BIT(port);
831291d1e72SVladimir Oltean 		l2_lookup.vlanid = vid;
832291d1e72SVladimir Oltean 
833291d1e72SVladimir Oltean 		if (last_unused >= 0) {
834291d1e72SVladimir Oltean 			way = last_unused;
835291d1e72SVladimir Oltean 		} else {
836291d1e72SVladimir Oltean 			/* Bin is full, need to evict somebody.
837291d1e72SVladimir Oltean 			 * Choose victim at random. If you get these messages
838291d1e72SVladimir Oltean 			 * often, you may need to consider changing the
839291d1e72SVladimir Oltean 			 * distribution function:
840291d1e72SVladimir Oltean 			 * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly
841291d1e72SVladimir Oltean 			 */
842291d1e72SVladimir Oltean 			get_random_bytes(&way, sizeof(u8));
843291d1e72SVladimir Oltean 			way %= SJA1105ET_FDB_BIN_SIZE;
844291d1e72SVladimir Oltean 			dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n",
845291d1e72SVladimir Oltean 				 bin, addr, way);
846291d1e72SVladimir Oltean 			/* Evict entry */
847291d1e72SVladimir Oltean 			sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
848291d1e72SVladimir Oltean 						     index, NULL, false);
849291d1e72SVladimir Oltean 		}
850291d1e72SVladimir Oltean 	}
851291d1e72SVladimir Oltean 	l2_lookup.index = sja1105et_fdb_index(bin, way);
852291d1e72SVladimir Oltean 
853291d1e72SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
854291d1e72SVladimir Oltean 					    l2_lookup.index, &l2_lookup,
855291d1e72SVladimir Oltean 					    true);
856291d1e72SVladimir Oltean }
857291d1e72SVladimir Oltean 
858291d1e72SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port,
859291d1e72SVladimir Oltean 			   const unsigned char *addr, u16 vid)
860291d1e72SVladimir Oltean {
861291d1e72SVladimir Oltean 	struct sja1105_l2_lookup_entry l2_lookup = {0};
862291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
863291d1e72SVladimir Oltean 	int index, bin, way;
864291d1e72SVladimir Oltean 	bool keep;
865291d1e72SVladimir Oltean 
866291d1e72SVladimir Oltean 	bin = sja1105_fdb_hash(priv, addr, vid);
867291d1e72SVladimir Oltean 	way = sja1105_is_fdb_entry_in_bin(priv, bin, addr, vid,
868291d1e72SVladimir Oltean 					  &l2_lookup, NULL);
869291d1e72SVladimir Oltean 	if (way < 0)
870291d1e72SVladimir Oltean 		return 0;
871291d1e72SVladimir Oltean 	index = sja1105et_fdb_index(bin, way);
872291d1e72SVladimir Oltean 
873291d1e72SVladimir Oltean 	/* We have an FDB entry. Is our port in the destination mask? If yes,
874291d1e72SVladimir Oltean 	 * we need to remove it. If the resulting port mask becomes empty, we
875291d1e72SVladimir Oltean 	 * need to completely evict the FDB entry.
876291d1e72SVladimir Oltean 	 * Otherwise we just write it back.
877291d1e72SVladimir Oltean 	 */
878291d1e72SVladimir Oltean 	if (l2_lookup.destports & BIT(port))
879291d1e72SVladimir Oltean 		l2_lookup.destports &= ~BIT(port);
880291d1e72SVladimir Oltean 	if (l2_lookup.destports)
881291d1e72SVladimir Oltean 		keep = true;
882291d1e72SVladimir Oltean 	else
883291d1e72SVladimir Oltean 		keep = false;
884291d1e72SVladimir Oltean 
885291d1e72SVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP,
886291d1e72SVladimir Oltean 					    index, &l2_lookup, keep);
887291d1e72SVladimir Oltean }
888291d1e72SVladimir Oltean 
889291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port,
890291d1e72SVladimir Oltean 			    dsa_fdb_dump_cb_t *cb, void *data)
891291d1e72SVladimir Oltean {
892291d1e72SVladimir Oltean 	struct sja1105_private *priv = ds->priv;
893291d1e72SVladimir Oltean 	struct device *dev = ds->dev;
894291d1e72SVladimir Oltean 	int i;
895291d1e72SVladimir Oltean 
896291d1e72SVladimir Oltean 	for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) {
897291d1e72SVladimir Oltean 		struct sja1105_l2_lookup_entry l2_lookup = {0};
898291d1e72SVladimir Oltean 		u8 macaddr[ETH_ALEN];
899291d1e72SVladimir Oltean 		int rc;
900291d1e72SVladimir Oltean 
901291d1e72SVladimir Oltean 		rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP,
902291d1e72SVladimir Oltean 						 i, &l2_lookup);
903291d1e72SVladimir Oltean 		/* No fdb entry at i, not an issue */
904291d1e72SVladimir Oltean 		if (rc == -EINVAL)
905291d1e72SVladimir Oltean 			continue;
906291d1e72SVladimir Oltean 		if (rc) {
907291d1e72SVladimir Oltean 			dev_err(dev, "Failed to dump FDB: %d\n", rc);
908291d1e72SVladimir Oltean 			return rc;
909291d1e72SVladimir Oltean 		}
910291d1e72SVladimir Oltean 
911291d1e72SVladimir Oltean 		/* FDB dump callback is per port. This means we have to
912291d1e72SVladimir Oltean 		 * disregard a valid entry if it's not for this port, even if
913291d1e72SVladimir Oltean 		 * only to revisit it later. This is inefficient because the
914291d1e72SVladimir Oltean 		 * 1024-sized FDB table needs to be traversed 4 times through
915291d1e72SVladimir Oltean 		 * SPI during a 'bridge fdb show' command.
916291d1e72SVladimir Oltean 		 */
917291d1e72SVladimir Oltean 		if (!(l2_lookup.destports & BIT(port)))
918291d1e72SVladimir Oltean 			continue;
919291d1e72SVladimir Oltean 		u64_to_ether_addr(l2_lookup.macaddr, macaddr);
920291d1e72SVladimir Oltean 		cb(macaddr, l2_lookup.vlanid, false, data);
921291d1e72SVladimir Oltean 	}
922291d1e72SVladimir Oltean 	return 0;
923291d1e72SVladimir Oltean }
924291d1e72SVladimir Oltean 
925291d1e72SVladimir Oltean /* This callback needs to be present */
926291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port,
927291d1e72SVladimir Oltean 			       const struct switchdev_obj_port_mdb *mdb)
928291d1e72SVladimir Oltean {
929291d1e72SVladimir Oltean 	return 0;
930291d1e72SVladimir Oltean }
931291d1e72SVladimir Oltean 
932291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port,
933291d1e72SVladimir Oltean 			    const struct switchdev_obj_port_mdb *mdb)
934291d1e72SVladimir Oltean {
935291d1e72SVladimir Oltean 	sja1105_fdb_add(ds, port, mdb->addr, mdb->vid);
936291d1e72SVladimir Oltean }
937291d1e72SVladimir Oltean 
938291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port,
939291d1e72SVladimir Oltean 			   const struct switchdev_obj_port_mdb *mdb)
940291d1e72SVladimir Oltean {
941291d1e72SVladimir Oltean 	return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid);
942291d1e72SVladimir Oltean }
943291d1e72SVladimir Oltean 
9448aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port,
9458aa9ebccSVladimir Oltean 				 struct net_device *br, bool member)
9468aa9ebccSVladimir Oltean {
9478aa9ebccSVladimir Oltean 	struct sja1105_l2_forwarding_entry *l2_fwd;
9488aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
9498aa9ebccSVladimir Oltean 	int i, rc;
9508aa9ebccSVladimir Oltean 
9518aa9ebccSVladimir Oltean 	l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries;
9528aa9ebccSVladimir Oltean 
9538aa9ebccSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
9548aa9ebccSVladimir Oltean 		/* Add this port to the forwarding matrix of the
9558aa9ebccSVladimir Oltean 		 * other ports in the same bridge, and viceversa.
9568aa9ebccSVladimir Oltean 		 */
9578aa9ebccSVladimir Oltean 		if (!dsa_is_user_port(ds, i))
9588aa9ebccSVladimir Oltean 			continue;
9598aa9ebccSVladimir Oltean 		/* For the ports already under the bridge, only one thing needs
9608aa9ebccSVladimir Oltean 		 * to be done, and that is to add this port to their
9618aa9ebccSVladimir Oltean 		 * reachability domain. So we can perform the SPI write for
9628aa9ebccSVladimir Oltean 		 * them immediately. However, for this port itself (the one
9638aa9ebccSVladimir Oltean 		 * that is new to the bridge), we need to add all other ports
9648aa9ebccSVladimir Oltean 		 * to its reachability domain. So we do that incrementally in
9658aa9ebccSVladimir Oltean 		 * this loop, and perform the SPI write only at the end, once
9668aa9ebccSVladimir Oltean 		 * the domain contains all other bridge ports.
9678aa9ebccSVladimir Oltean 		 */
9688aa9ebccSVladimir Oltean 		if (i == port)
9698aa9ebccSVladimir Oltean 			continue;
9708aa9ebccSVladimir Oltean 		if (dsa_to_port(ds, i)->bridge_dev != br)
9718aa9ebccSVladimir Oltean 			continue;
9728aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, i, port, member);
9738aa9ebccSVladimir Oltean 		sja1105_port_allow_traffic(l2_fwd, port, i, member);
9748aa9ebccSVladimir Oltean 
9758aa9ebccSVladimir Oltean 		rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
9768aa9ebccSVladimir Oltean 						  i, &l2_fwd[i], true);
9778aa9ebccSVladimir Oltean 		if (rc < 0)
9788aa9ebccSVladimir Oltean 			return rc;
9798aa9ebccSVladimir Oltean 	}
9808aa9ebccSVladimir Oltean 
9818aa9ebccSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING,
9828aa9ebccSVladimir Oltean 					    port, &l2_fwd[port], true);
9838aa9ebccSVladimir Oltean }
9848aa9ebccSVladimir Oltean 
9858aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port,
9868aa9ebccSVladimir Oltean 			       struct net_device *br)
9878aa9ebccSVladimir Oltean {
9888aa9ebccSVladimir Oltean 	return sja1105_bridge_member(ds, port, br, true);
9898aa9ebccSVladimir Oltean }
9908aa9ebccSVladimir Oltean 
9918aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port,
9928aa9ebccSVladimir Oltean 				 struct net_device *br)
9938aa9ebccSVladimir Oltean {
9948aa9ebccSVladimir Oltean 	sja1105_bridge_member(ds, port, br, false);
9958aa9ebccSVladimir Oltean }
9968aa9ebccSVladimir Oltean 
9976666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only
9986666cebcSVladimir Oltean  * available through the static configuration, resetting the switch in order
9996666cebcSVladimir Oltean  * to upload the new static config is unavoidable. Back up the settings we
10006666cebcSVladimir Oltean  * modify at runtime (currently only MAC) and restore them after uploading,
10016666cebcSVladimir Oltean  * such that this operation is relatively seamless.
10026666cebcSVladimir Oltean  */
10036666cebcSVladimir Oltean static int sja1105_static_config_reload(struct sja1105_private *priv)
10046666cebcSVladimir Oltean {
10056666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
10066666cebcSVladimir Oltean 	int speed_mbps[SJA1105_NUM_PORTS];
10076666cebcSVladimir Oltean 	int rc, i;
10086666cebcSVladimir Oltean 
10096666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
10106666cebcSVladimir Oltean 
10116666cebcSVladimir Oltean 	/* Back up settings changed by sja1105_adjust_port_config and
10126666cebcSVladimir Oltean 	 * and restore their defaults.
10136666cebcSVladimir Oltean 	 */
10146666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
10156666cebcSVladimir Oltean 		speed_mbps[i] = sja1105_speed[mac[i].speed];
10166666cebcSVladimir Oltean 		mac[i].speed = SJA1105_SPEED_AUTO;
10176666cebcSVladimir Oltean 	}
10186666cebcSVladimir Oltean 
10196666cebcSVladimir Oltean 	/* Reset switch and send updated static configuration */
10206666cebcSVladimir Oltean 	rc = sja1105_static_config_upload(priv);
10216666cebcSVladimir Oltean 	if (rc < 0)
10226666cebcSVladimir Oltean 		goto out;
10236666cebcSVladimir Oltean 
10246666cebcSVladimir Oltean 	/* Configure the CGU (PLLs) for MII and RMII PHYs.
10256666cebcSVladimir Oltean 	 * For these interfaces there is no dynamic configuration
10266666cebcSVladimir Oltean 	 * needed, since PLLs have same settings at all speeds.
10276666cebcSVladimir Oltean 	 */
10286666cebcSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
10296666cebcSVladimir Oltean 	if (rc < 0)
10306666cebcSVladimir Oltean 		goto out;
10316666cebcSVladimir Oltean 
10326666cebcSVladimir Oltean 	for (i = 0; i < SJA1105_NUM_PORTS; i++) {
10336666cebcSVladimir Oltean 		bool enabled = (speed_mbps[i] != 0);
10346666cebcSVladimir Oltean 
10356666cebcSVladimir Oltean 		rc = sja1105_adjust_port_config(priv, i, speed_mbps[i],
10366666cebcSVladimir Oltean 						enabled);
10376666cebcSVladimir Oltean 		if (rc < 0)
10386666cebcSVladimir Oltean 			goto out;
10396666cebcSVladimir Oltean 	}
10406666cebcSVladimir Oltean out:
10416666cebcSVladimir Oltean 	return rc;
10426666cebcSVladimir Oltean }
10436666cebcSVladimir Oltean 
10446666cebcSVladimir Oltean /* The TPID setting belongs to the General Parameters table,
10456666cebcSVladimir Oltean  * which can only be partially reconfigured at runtime (and not the TPID).
10466666cebcSVladimir Oltean  * So a switch reset is required.
10476666cebcSVladimir Oltean  */
10486666cebcSVladimir Oltean static int sja1105_change_tpid(struct sja1105_private *priv,
10496666cebcSVladimir Oltean 			       u16 tpid, u16 tpid2)
10506666cebcSVladimir Oltean {
10516666cebcSVladimir Oltean 	struct sja1105_general_params_entry *general_params;
10526666cebcSVladimir Oltean 	struct sja1105_table *table;
10536666cebcSVladimir Oltean 
10546666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
10556666cebcSVladimir Oltean 	general_params = table->entries;
10566666cebcSVladimir Oltean 	general_params->tpid = tpid;
10576666cebcSVladimir Oltean 	general_params->tpid2 = tpid2;
10586666cebcSVladimir Oltean 	return sja1105_static_config_reload(priv);
10596666cebcSVladimir Oltean }
10606666cebcSVladimir Oltean 
10616666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid)
10626666cebcSVladimir Oltean {
10636666cebcSVladimir Oltean 	struct sja1105_mac_config_entry *mac;
10646666cebcSVladimir Oltean 
10656666cebcSVladimir Oltean 	mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries;
10666666cebcSVladimir Oltean 
10676666cebcSVladimir Oltean 	mac[port].vlanid = pvid;
10686666cebcSVladimir Oltean 
10696666cebcSVladimir Oltean 	return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port,
10706666cebcSVladimir Oltean 					   &mac[port], true);
10716666cebcSVladimir Oltean }
10726666cebcSVladimir Oltean 
10736666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid)
10746666cebcSVladimir Oltean {
10756666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
10766666cebcSVladimir Oltean 	int count, i;
10776666cebcSVladimir Oltean 
10786666cebcSVladimir Oltean 	vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries;
10796666cebcSVladimir Oltean 	count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count;
10806666cebcSVladimir Oltean 
10816666cebcSVladimir Oltean 	for (i = 0; i < count; i++)
10826666cebcSVladimir Oltean 		if (vlan[i].vlanid == vid)
10836666cebcSVladimir Oltean 			return i;
10846666cebcSVladimir Oltean 
10856666cebcSVladimir Oltean 	/* Return an invalid entry index if not found */
10866666cebcSVladimir Oltean 	return -1;
10876666cebcSVladimir Oltean }
10886666cebcSVladimir Oltean 
10896666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid,
10906666cebcSVladimir Oltean 			      bool enabled, bool untagged)
10916666cebcSVladimir Oltean {
10926666cebcSVladimir Oltean 	struct sja1105_vlan_lookup_entry *vlan;
10936666cebcSVladimir Oltean 	struct sja1105_table *table;
10946666cebcSVladimir Oltean 	bool keep = true;
10956666cebcSVladimir Oltean 	int match, rc;
10966666cebcSVladimir Oltean 
10976666cebcSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP];
10986666cebcSVladimir Oltean 
10996666cebcSVladimir Oltean 	match = sja1105_is_vlan_configured(priv, vid);
11006666cebcSVladimir Oltean 	if (match < 0) {
11016666cebcSVladimir Oltean 		/* Can't delete a missing entry. */
11026666cebcSVladimir Oltean 		if (!enabled)
11036666cebcSVladimir Oltean 			return 0;
11046666cebcSVladimir Oltean 		rc = sja1105_table_resize(table, table->entry_count + 1);
11056666cebcSVladimir Oltean 		if (rc)
11066666cebcSVladimir Oltean 			return rc;
11076666cebcSVladimir Oltean 		match = table->entry_count - 1;
11086666cebcSVladimir Oltean 	}
11096666cebcSVladimir Oltean 	/* Assign pointer after the resize (it's new memory) */
11106666cebcSVladimir Oltean 	vlan = table->entries;
11116666cebcSVladimir Oltean 	vlan[match].vlanid = vid;
11126666cebcSVladimir Oltean 	if (enabled) {
11136666cebcSVladimir Oltean 		vlan[match].vlan_bc |= BIT(port);
11146666cebcSVladimir Oltean 		vlan[match].vmemb_port |= BIT(port);
11156666cebcSVladimir Oltean 	} else {
11166666cebcSVladimir Oltean 		vlan[match].vlan_bc &= ~BIT(port);
11176666cebcSVladimir Oltean 		vlan[match].vmemb_port &= ~BIT(port);
11186666cebcSVladimir Oltean 	}
11196666cebcSVladimir Oltean 	/* Also unset tag_port if removing this VLAN was requested,
11206666cebcSVladimir Oltean 	 * just so we don't have a confusing bitmap (no practical purpose).
11216666cebcSVladimir Oltean 	 */
11226666cebcSVladimir Oltean 	if (untagged || !enabled)
11236666cebcSVladimir Oltean 		vlan[match].tag_port &= ~BIT(port);
11246666cebcSVladimir Oltean 	else
11256666cebcSVladimir Oltean 		vlan[match].tag_port |= BIT(port);
11266666cebcSVladimir Oltean 	/* If there's no port left as member of this VLAN,
11276666cebcSVladimir Oltean 	 * it's time for it to go.
11286666cebcSVladimir Oltean 	 */
11296666cebcSVladimir Oltean 	if (!vlan[match].vmemb_port)
11306666cebcSVladimir Oltean 		keep = false;
11316666cebcSVladimir Oltean 
11326666cebcSVladimir Oltean 	dev_dbg(priv->ds->dev,
11336666cebcSVladimir Oltean 		"%s: port %d, vid %llu, broadcast domain 0x%llx, "
11346666cebcSVladimir Oltean 		"port members 0x%llx, tagged ports 0x%llx, keep %d\n",
11356666cebcSVladimir Oltean 		__func__, port, vlan[match].vlanid, vlan[match].vlan_bc,
11366666cebcSVladimir Oltean 		vlan[match].vmemb_port, vlan[match].tag_port, keep);
11376666cebcSVladimir Oltean 
11386666cebcSVladimir Oltean 	rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid,
11396666cebcSVladimir Oltean 					  &vlan[match], keep);
11406666cebcSVladimir Oltean 	if (rc < 0)
11416666cebcSVladimir Oltean 		return rc;
11426666cebcSVladimir Oltean 
11436666cebcSVladimir Oltean 	if (!keep)
11446666cebcSVladimir Oltean 		return sja1105_table_delete_entry(table, match);
11456666cebcSVladimir Oltean 
11466666cebcSVladimir Oltean 	return 0;
11476666cebcSVladimir Oltean }
11486666cebcSVladimir Oltean 
11498aa9ebccSVladimir Oltean static enum dsa_tag_protocol
11508aa9ebccSVladimir Oltean sja1105_get_tag_protocol(struct dsa_switch *ds, int port)
11518aa9ebccSVladimir Oltean {
11528aa9ebccSVladimir Oltean 	return DSA_TAG_PROTO_NONE;
11538aa9ebccSVladimir Oltean }
11548aa9ebccSVladimir Oltean 
11556666cebcSVladimir Oltean /* This callback needs to be present */
11566666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port,
11576666cebcSVladimir Oltean 				const struct switchdev_obj_port_vlan *vlan)
11586666cebcSVladimir Oltean {
11596666cebcSVladimir Oltean 	return 0;
11606666cebcSVladimir Oltean }
11616666cebcSVladimir Oltean 
11626666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled)
11636666cebcSVladimir Oltean {
11646666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
11656666cebcSVladimir Oltean 	int rc;
11666666cebcSVladimir Oltean 
11676666cebcSVladimir Oltean 	if (enabled)
11686666cebcSVladimir Oltean 		/* Enable VLAN filtering. */
11696666cebcSVladimir Oltean 		rc = sja1105_change_tpid(priv, ETH_P_8021Q, ETH_P_8021AD);
11706666cebcSVladimir Oltean 	else
11716666cebcSVladimir Oltean 		/* Disable VLAN filtering. */
11726666cebcSVladimir Oltean 		rc = sja1105_change_tpid(priv, ETH_P_SJA1105, ETH_P_SJA1105);
11736666cebcSVladimir Oltean 	if (rc)
11746666cebcSVladimir Oltean 		dev_err(ds->dev, "Failed to change VLAN Ethertype\n");
11756666cebcSVladimir Oltean 
11766666cebcSVladimir Oltean 	return rc;
11776666cebcSVladimir Oltean }
11786666cebcSVladimir Oltean 
11796666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port,
11806666cebcSVladimir Oltean 			     const struct switchdev_obj_port_vlan *vlan)
11816666cebcSVladimir Oltean {
11826666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
11836666cebcSVladimir Oltean 	u16 vid;
11846666cebcSVladimir Oltean 	int rc;
11856666cebcSVladimir Oltean 
11866666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
11876666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags &
11886666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
11896666cebcSVladimir Oltean 		if (rc < 0) {
11906666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n",
11916666cebcSVladimir Oltean 				vid, port, rc);
11926666cebcSVladimir Oltean 			return;
11936666cebcSVladimir Oltean 		}
11946666cebcSVladimir Oltean 		if (vlan->flags & BRIDGE_VLAN_INFO_PVID) {
11956666cebcSVladimir Oltean 			rc = sja1105_pvid_apply(ds->priv, port, vid);
11966666cebcSVladimir Oltean 			if (rc < 0) {
11976666cebcSVladimir Oltean 				dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n",
11986666cebcSVladimir Oltean 					vid, port, rc);
11996666cebcSVladimir Oltean 				return;
12006666cebcSVladimir Oltean 			}
12016666cebcSVladimir Oltean 		}
12026666cebcSVladimir Oltean 	}
12036666cebcSVladimir Oltean }
12046666cebcSVladimir Oltean 
12056666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port,
12066666cebcSVladimir Oltean 			    const struct switchdev_obj_port_vlan *vlan)
12076666cebcSVladimir Oltean {
12086666cebcSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12096666cebcSVladimir Oltean 	u16 vid;
12106666cebcSVladimir Oltean 	int rc;
12116666cebcSVladimir Oltean 
12126666cebcSVladimir Oltean 	for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
12136666cebcSVladimir Oltean 		rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags &
12146666cebcSVladimir Oltean 					BRIDGE_VLAN_INFO_UNTAGGED);
12156666cebcSVladimir Oltean 		if (rc < 0) {
12166666cebcSVladimir Oltean 			dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n",
12176666cebcSVladimir Oltean 				vid, port, rc);
12186666cebcSVladimir Oltean 			return rc;
12196666cebcSVladimir Oltean 		}
12206666cebcSVladimir Oltean 	}
12216666cebcSVladimir Oltean 	return 0;
12226666cebcSVladimir Oltean }
12236666cebcSVladimir Oltean 
12248aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static
12258aa9ebccSVladimir Oltean  * configuration tables. Some of these can be dynamically modified at runtime,
12268aa9ebccSVladimir Oltean  * but not the xMII mode parameters table.
12278aa9ebccSVladimir Oltean  * Furthermode, some PHYs may not have crystals for generating their clocks
12288aa9ebccSVladimir Oltean  * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's
12298aa9ebccSVladimir Oltean  * ref_clk pin. So port clocking needs to be initialized early, before
12308aa9ebccSVladimir Oltean  * connecting to PHYs is attempted, otherwise they won't respond through MDIO.
12318aa9ebccSVladimir Oltean  * Setting correct PHY link speed does not matter now.
12328aa9ebccSVladimir Oltean  * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY
12338aa9ebccSVladimir Oltean  * bindings are not yet parsed by DSA core. We need to parse early so that we
12348aa9ebccSVladimir Oltean  * can populate the xMII mode parameters table.
12358aa9ebccSVladimir Oltean  */
12368aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds)
12378aa9ebccSVladimir Oltean {
12388aa9ebccSVladimir Oltean 	struct sja1105_dt_port ports[SJA1105_NUM_PORTS];
12398aa9ebccSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12408aa9ebccSVladimir Oltean 	int rc;
12418aa9ebccSVladimir Oltean 
12428aa9ebccSVladimir Oltean 	rc = sja1105_parse_dt(priv, ports);
12438aa9ebccSVladimir Oltean 	if (rc < 0) {
12448aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to parse DT: %d\n", rc);
12458aa9ebccSVladimir Oltean 		return rc;
12468aa9ebccSVladimir Oltean 	}
1247f5b8631cSVladimir Oltean 
1248f5b8631cSVladimir Oltean 	/* Error out early if internal delays are required through DT
1249f5b8631cSVladimir Oltean 	 * and we can't apply them.
1250f5b8631cSVladimir Oltean 	 */
1251f5b8631cSVladimir Oltean 	rc = sja1105_parse_rgmii_delays(priv, ports);
1252f5b8631cSVladimir Oltean 	if (rc < 0) {
1253f5b8631cSVladimir Oltean 		dev_err(ds->dev, "RGMII delay not supported\n");
1254f5b8631cSVladimir Oltean 		return rc;
1255f5b8631cSVladimir Oltean 	}
1256f5b8631cSVladimir Oltean 
12578aa9ebccSVladimir Oltean 	/* Create and send configuration down to device */
12588aa9ebccSVladimir Oltean 	rc = sja1105_static_config_load(priv, ports);
12598aa9ebccSVladimir Oltean 	if (rc < 0) {
12608aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to load static config: %d\n", rc);
12618aa9ebccSVladimir Oltean 		return rc;
12628aa9ebccSVladimir Oltean 	}
12638aa9ebccSVladimir Oltean 	/* Configure the CGU (PHY link modes and speeds) */
12648aa9ebccSVladimir Oltean 	rc = sja1105_clocking_setup(priv);
12658aa9ebccSVladimir Oltean 	if (rc < 0) {
12668aa9ebccSVladimir Oltean 		dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc);
12678aa9ebccSVladimir Oltean 		return rc;
12688aa9ebccSVladimir Oltean 	}
12696666cebcSVladimir Oltean 	/* On SJA1105, VLAN filtering per se is always enabled in hardware.
12706666cebcSVladimir Oltean 	 * The only thing we can do to disable it is lie about what the 802.1Q
12716666cebcSVladimir Oltean 	 * EtherType is.
12726666cebcSVladimir Oltean 	 * So it will still try to apply VLAN filtering, but all ingress
12736666cebcSVladimir Oltean 	 * traffic (except frames received with EtherType of ETH_P_SJA1105)
12746666cebcSVladimir Oltean 	 * will be internally tagged with a distorted VLAN header where the
12756666cebcSVladimir Oltean 	 * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid.
12766666cebcSVladimir Oltean 	 */
12776666cebcSVladimir Oltean 	ds->vlan_filtering_is_global = true;
12788aa9ebccSVladimir Oltean 
12798aa9ebccSVladimir Oltean 	return 0;
12808aa9ebccSVladimir Oltean }
12818aa9ebccSVladimir Oltean 
12828456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table,
12838456721dSVladimir Oltean  * which cannot be reconfigured at runtime. So a switch reset is required.
12848456721dSVladimir Oltean  */
12858456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds,
12868456721dSVladimir Oltean 				   unsigned int ageing_time)
12878456721dSVladimir Oltean {
12888456721dSVladimir Oltean 	struct sja1105_l2_lookup_params_entry *l2_lookup_params;
12898456721dSVladimir Oltean 	struct sja1105_private *priv = ds->priv;
12908456721dSVladimir Oltean 	struct sja1105_table *table;
12918456721dSVladimir Oltean 	unsigned int maxage;
12928456721dSVladimir Oltean 
12938456721dSVladimir Oltean 	table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS];
12948456721dSVladimir Oltean 	l2_lookup_params = table->entries;
12958456721dSVladimir Oltean 
12968456721dSVladimir Oltean 	maxage = SJA1105_AGEING_TIME_MS(ageing_time);
12978456721dSVladimir Oltean 
12988456721dSVladimir Oltean 	if (l2_lookup_params->maxage == maxage)
12998456721dSVladimir Oltean 		return 0;
13008456721dSVladimir Oltean 
13018456721dSVladimir Oltean 	l2_lookup_params->maxage = maxage;
13028456721dSVladimir Oltean 
13038456721dSVladimir Oltean 	return sja1105_static_config_reload(priv);
13048456721dSVladimir Oltean }
13058456721dSVladimir Oltean 
13068aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = {
13078aa9ebccSVladimir Oltean 	.get_tag_protocol	= sja1105_get_tag_protocol,
13088aa9ebccSVladimir Oltean 	.setup			= sja1105_setup,
13098aa9ebccSVladimir Oltean 	.adjust_link		= sja1105_adjust_link,
13108456721dSVladimir Oltean 	.set_ageing_time	= sja1105_set_ageing_time,
1311*ad9f299aSVladimir Oltean 	.phylink_validate	= sja1105_phylink_validate,
131252c34e6eSVladimir Oltean 	.get_strings		= sja1105_get_strings,
131352c34e6eSVladimir Oltean 	.get_ethtool_stats	= sja1105_get_ethtool_stats,
131452c34e6eSVladimir Oltean 	.get_sset_count		= sja1105_get_sset_count,
1315291d1e72SVladimir Oltean 	.port_fdb_dump		= sja1105_fdb_dump,
1316291d1e72SVladimir Oltean 	.port_fdb_add		= sja1105_fdb_add,
1317291d1e72SVladimir Oltean 	.port_fdb_del		= sja1105_fdb_del,
13188aa9ebccSVladimir Oltean 	.port_bridge_join	= sja1105_bridge_join,
13198aa9ebccSVladimir Oltean 	.port_bridge_leave	= sja1105_bridge_leave,
13206666cebcSVladimir Oltean 	.port_vlan_prepare	= sja1105_vlan_prepare,
13216666cebcSVladimir Oltean 	.port_vlan_filtering	= sja1105_vlan_filtering,
13226666cebcSVladimir Oltean 	.port_vlan_add		= sja1105_vlan_add,
13236666cebcSVladimir Oltean 	.port_vlan_del		= sja1105_vlan_del,
1324291d1e72SVladimir Oltean 	.port_mdb_prepare	= sja1105_mdb_prepare,
1325291d1e72SVladimir Oltean 	.port_mdb_add		= sja1105_mdb_add,
1326291d1e72SVladimir Oltean 	.port_mdb_del		= sja1105_mdb_del,
13278aa9ebccSVladimir Oltean };
13288aa9ebccSVladimir Oltean 
13298aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv)
13308aa9ebccSVladimir Oltean {
13318aa9ebccSVladimir Oltean 	const struct sja1105_regs *regs = priv->info->regs;
13328aa9ebccSVladimir Oltean 	u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0};
13338aa9ebccSVladimir Oltean 	struct device *dev = &priv->spidev->dev;
13348aa9ebccSVladimir Oltean 	u64 device_id;
13358aa9ebccSVladimir Oltean 	u64 part_no;
13368aa9ebccSVladimir Oltean 	int rc;
13378aa9ebccSVladimir Oltean 
13388aa9ebccSVladimir Oltean 	rc = sja1105_spi_send_int(priv, SPI_READ, regs->device_id,
13398aa9ebccSVladimir Oltean 				  &device_id, SJA1105_SIZE_DEVICE_ID);
13408aa9ebccSVladimir Oltean 	if (rc < 0)
13418aa9ebccSVladimir Oltean 		return rc;
13428aa9ebccSVladimir Oltean 
13438aa9ebccSVladimir Oltean 	if (device_id != priv->info->device_id) {
13448aa9ebccSVladimir Oltean 		dev_err(dev, "Expected device ID 0x%llx but read 0x%llx\n",
13458aa9ebccSVladimir Oltean 			priv->info->device_id, device_id);
13468aa9ebccSVladimir Oltean 		return -ENODEV;
13478aa9ebccSVladimir Oltean 	}
13488aa9ebccSVladimir Oltean 
13498aa9ebccSVladimir Oltean 	rc = sja1105_spi_send_packed_buf(priv, SPI_READ, regs->prod_id,
13508aa9ebccSVladimir Oltean 					 prod_id, SJA1105_SIZE_DEVICE_ID);
13518aa9ebccSVladimir Oltean 	if (rc < 0)
13528aa9ebccSVladimir Oltean 		return rc;
13538aa9ebccSVladimir Oltean 
13548aa9ebccSVladimir Oltean 	sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID);
13558aa9ebccSVladimir Oltean 
13568aa9ebccSVladimir Oltean 	if (part_no != priv->info->part_no) {
13578aa9ebccSVladimir Oltean 		dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n",
13588aa9ebccSVladimir Oltean 			priv->info->part_no, part_no);
13598aa9ebccSVladimir Oltean 		return -ENODEV;
13608aa9ebccSVladimir Oltean 	}
13618aa9ebccSVladimir Oltean 
13628aa9ebccSVladimir Oltean 	return 0;
13638aa9ebccSVladimir Oltean }
13648aa9ebccSVladimir Oltean 
13658aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi)
13668aa9ebccSVladimir Oltean {
13678aa9ebccSVladimir Oltean 	struct device *dev = &spi->dev;
13688aa9ebccSVladimir Oltean 	struct sja1105_private *priv;
13698aa9ebccSVladimir Oltean 	struct dsa_switch *ds;
13708aa9ebccSVladimir Oltean 	int rc;
13718aa9ebccSVladimir Oltean 
13728aa9ebccSVladimir Oltean 	if (!dev->of_node) {
13738aa9ebccSVladimir Oltean 		dev_err(dev, "No DTS bindings for SJA1105 driver\n");
13748aa9ebccSVladimir Oltean 		return -EINVAL;
13758aa9ebccSVladimir Oltean 	}
13768aa9ebccSVladimir Oltean 
13778aa9ebccSVladimir Oltean 	priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL);
13788aa9ebccSVladimir Oltean 	if (!priv)
13798aa9ebccSVladimir Oltean 		return -ENOMEM;
13808aa9ebccSVladimir Oltean 
13818aa9ebccSVladimir Oltean 	/* Configure the optional reset pin and bring up switch */
13828aa9ebccSVladimir Oltean 	priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH);
13838aa9ebccSVladimir Oltean 	if (IS_ERR(priv->reset_gpio))
13848aa9ebccSVladimir Oltean 		dev_dbg(dev, "reset-gpios not defined, ignoring\n");
13858aa9ebccSVladimir Oltean 	else
13868aa9ebccSVladimir Oltean 		sja1105_hw_reset(priv->reset_gpio, 1, 1);
13878aa9ebccSVladimir Oltean 
13888aa9ebccSVladimir Oltean 	/* Populate our driver private structure (priv) based on
13898aa9ebccSVladimir Oltean 	 * the device tree node that was probed (spi)
13908aa9ebccSVladimir Oltean 	 */
13918aa9ebccSVladimir Oltean 	priv->spidev = spi;
13928aa9ebccSVladimir Oltean 	spi_set_drvdata(spi, priv);
13938aa9ebccSVladimir Oltean 
13948aa9ebccSVladimir Oltean 	/* Configure the SPI bus */
13958aa9ebccSVladimir Oltean 	spi->bits_per_word = 8;
13968aa9ebccSVladimir Oltean 	rc = spi_setup(spi);
13978aa9ebccSVladimir Oltean 	if (rc < 0) {
13988aa9ebccSVladimir Oltean 		dev_err(dev, "Could not init SPI\n");
13998aa9ebccSVladimir Oltean 		return rc;
14008aa9ebccSVladimir Oltean 	}
14018aa9ebccSVladimir Oltean 
14028aa9ebccSVladimir Oltean 	priv->info = of_device_get_match_data(dev);
14038aa9ebccSVladimir Oltean 
14048aa9ebccSVladimir Oltean 	/* Detect hardware device */
14058aa9ebccSVladimir Oltean 	rc = sja1105_check_device_id(priv);
14068aa9ebccSVladimir Oltean 	if (rc < 0) {
14078aa9ebccSVladimir Oltean 		dev_err(dev, "Device ID check failed: %d\n", rc);
14088aa9ebccSVladimir Oltean 		return rc;
14098aa9ebccSVladimir Oltean 	}
14108aa9ebccSVladimir Oltean 
14118aa9ebccSVladimir Oltean 	dev_info(dev, "Probed switch chip: %s\n", priv->info->name);
14128aa9ebccSVladimir Oltean 
14138aa9ebccSVladimir Oltean 	ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS);
14148aa9ebccSVladimir Oltean 	if (!ds)
14158aa9ebccSVladimir Oltean 		return -ENOMEM;
14168aa9ebccSVladimir Oltean 
14178aa9ebccSVladimir Oltean 	ds->ops = &sja1105_switch_ops;
14188aa9ebccSVladimir Oltean 	ds->priv = priv;
14198aa9ebccSVladimir Oltean 	priv->ds = ds;
14208aa9ebccSVladimir Oltean 
14218aa9ebccSVladimir Oltean 	return dsa_register_switch(priv->ds);
14228aa9ebccSVladimir Oltean }
14238aa9ebccSVladimir Oltean 
14248aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi)
14258aa9ebccSVladimir Oltean {
14268aa9ebccSVladimir Oltean 	struct sja1105_private *priv = spi_get_drvdata(spi);
14278aa9ebccSVladimir Oltean 
14288aa9ebccSVladimir Oltean 	dsa_unregister_switch(priv->ds);
14298aa9ebccSVladimir Oltean 	sja1105_static_config_free(&priv->static_config);
14308aa9ebccSVladimir Oltean 	return 0;
14318aa9ebccSVladimir Oltean }
14328aa9ebccSVladimir Oltean 
14338aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = {
14348aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105e", .data = &sja1105e_info },
14358aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105t", .data = &sja1105t_info },
14368aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105p", .data = &sja1105p_info },
14378aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105q", .data = &sja1105q_info },
14388aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105r", .data = &sja1105r_info },
14398aa9ebccSVladimir Oltean 	{ .compatible = "nxp,sja1105s", .data = &sja1105s_info },
14408aa9ebccSVladimir Oltean 	{ /* sentinel */ },
14418aa9ebccSVladimir Oltean };
14428aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids);
14438aa9ebccSVladimir Oltean 
14448aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = {
14458aa9ebccSVladimir Oltean 	.driver = {
14468aa9ebccSVladimir Oltean 		.name  = "sja1105",
14478aa9ebccSVladimir Oltean 		.owner = THIS_MODULE,
14488aa9ebccSVladimir Oltean 		.of_match_table = of_match_ptr(sja1105_dt_ids),
14498aa9ebccSVladimir Oltean 	},
14508aa9ebccSVladimir Oltean 	.probe  = sja1105_probe,
14518aa9ebccSVladimir Oltean 	.remove = sja1105_remove,
14528aa9ebccSVladimir Oltean };
14538aa9ebccSVladimir Oltean 
14548aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver);
14558aa9ebccSVladimir Oltean 
14568aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>");
14578aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>");
14588aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver");
14598aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2");
1460