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 738aa9ebccSVladimir Oltean * retrieved from the PHY object through phylib and 748aa9ebccSVladimir Oltean * sja1105_adjust_port_config. 758aa9ebccSVladimir Oltean */ 768aa9ebccSVladimir Oltean .speed = SJA1105_SPEED_AUTO, 778aa9ebccSVladimir Oltean /* No static correction for 1-step 1588 events */ 788aa9ebccSVladimir Oltean .tp_delin = 0, 798aa9ebccSVladimir Oltean .tp_delout = 0, 808aa9ebccSVladimir Oltean /* Disable aging for critical TTEthernet traffic */ 818aa9ebccSVladimir Oltean .maxage = 0xFF, 828aa9ebccSVladimir Oltean /* Internal VLAN (pvid) to apply to untagged ingress */ 838aa9ebccSVladimir Oltean .vlanprio = 0, 848aa9ebccSVladimir Oltean .vlanid = 0, 858aa9ebccSVladimir Oltean .ing_mirr = false, 868aa9ebccSVladimir Oltean .egr_mirr = false, 878aa9ebccSVladimir Oltean /* Don't drop traffic with other EtherType than ETH_P_IP */ 888aa9ebccSVladimir Oltean .drpnona664 = false, 898aa9ebccSVladimir Oltean /* Don't drop double-tagged traffic */ 908aa9ebccSVladimir Oltean .drpdtag = false, 918aa9ebccSVladimir Oltean /* Don't drop untagged traffic */ 928aa9ebccSVladimir Oltean .drpuntag = false, 938aa9ebccSVladimir Oltean /* Don't retag 802.1p (VID 0) traffic with the pvid */ 948aa9ebccSVladimir Oltean .retag = false, 95640f763fSVladimir Oltean /* Disable learning and I/O on user ports by default - 96640f763fSVladimir Oltean * STP will enable it. 97640f763fSVladimir Oltean */ 98640f763fSVladimir Oltean .dyn_learn = false, 998aa9ebccSVladimir Oltean .egress = false, 1008aa9ebccSVladimir Oltean .ingress = false, 1018aa9ebccSVladimir Oltean }; 1028aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 1038aa9ebccSVladimir Oltean struct sja1105_table *table; 1048aa9ebccSVladimir Oltean int i; 1058aa9ebccSVladimir Oltean 1068aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; 1078aa9ebccSVladimir Oltean 1088aa9ebccSVladimir Oltean /* Discard previous MAC Configuration Table */ 1098aa9ebccSVladimir Oltean if (table->entry_count) { 1108aa9ebccSVladimir Oltean kfree(table->entries); 1118aa9ebccSVladimir Oltean table->entry_count = 0; 1128aa9ebccSVladimir Oltean } 1138aa9ebccSVladimir Oltean 1148aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_NUM_PORTS, 1158aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1168aa9ebccSVladimir Oltean if (!table->entries) 1178aa9ebccSVladimir Oltean return -ENOMEM; 1188aa9ebccSVladimir Oltean 1198aa9ebccSVladimir Oltean /* Override table based on phylib DT bindings */ 1208aa9ebccSVladimir Oltean table->entry_count = SJA1105_NUM_PORTS; 1218aa9ebccSVladimir Oltean 1228aa9ebccSVladimir Oltean mac = table->entries; 1238aa9ebccSVladimir Oltean 124640f763fSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 1258aa9ebccSVladimir Oltean mac[i] = default_mac; 126640f763fSVladimir Oltean if (i == dsa_upstream_port(priv->ds, i)) { 127640f763fSVladimir Oltean /* STP doesn't get called for CPU port, so we need to 128640f763fSVladimir Oltean * set the I/O parameters statically. 129640f763fSVladimir Oltean */ 130640f763fSVladimir Oltean mac[i].dyn_learn = true; 131640f763fSVladimir Oltean mac[i].ingress = true; 132640f763fSVladimir Oltean mac[i].egress = true; 133640f763fSVladimir Oltean } 134640f763fSVladimir Oltean } 1358aa9ebccSVladimir Oltean 1368aa9ebccSVladimir Oltean return 0; 1378aa9ebccSVladimir Oltean } 1388aa9ebccSVladimir Oltean 1398aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv, 1408aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 1418aa9ebccSVladimir Oltean { 1428aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 1438aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1448aa9ebccSVladimir Oltean struct sja1105_table *table; 1458aa9ebccSVladimir Oltean int i; 1468aa9ebccSVladimir Oltean 1478aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 1488aa9ebccSVladimir Oltean 1498aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 1508aa9ebccSVladimir Oltean if (table->entry_count) { 1518aa9ebccSVladimir Oltean kfree(table->entries); 1528aa9ebccSVladimir Oltean table->entry_count = 0; 1538aa9ebccSVladimir Oltean } 1548aa9ebccSVladimir Oltean 1558aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT, 1568aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1578aa9ebccSVladimir Oltean if (!table->entries) 1588aa9ebccSVladimir Oltean return -ENOMEM; 1598aa9ebccSVladimir Oltean 1608aa9ebccSVladimir Oltean /* Override table based on phylib DT bindings */ 1618aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT; 1628aa9ebccSVladimir Oltean 1638aa9ebccSVladimir Oltean mii = table->entries; 1648aa9ebccSVladimir Oltean 1658aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 1668aa9ebccSVladimir Oltean switch (ports[i].phy_mode) { 1678aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 1688aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 1698aa9ebccSVladimir Oltean break; 1708aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 1718aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 1728aa9ebccSVladimir Oltean break; 1738aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 1748aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 1758aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 1768aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 1778aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 1788aa9ebccSVladimir Oltean break; 1798aa9ebccSVladimir Oltean default: 1808aa9ebccSVladimir Oltean dev_err(dev, "Unsupported PHY mode %s!\n", 1818aa9ebccSVladimir Oltean phy_modes(ports[i].phy_mode)); 1828aa9ebccSVladimir Oltean } 1838aa9ebccSVladimir Oltean 1848aa9ebccSVladimir Oltean mii->phy_mac[i] = ports[i].role; 1858aa9ebccSVladimir Oltean } 1868aa9ebccSVladimir Oltean return 0; 1878aa9ebccSVladimir Oltean } 1888aa9ebccSVladimir Oltean 1898aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 1908aa9ebccSVladimir Oltean { 1918aa9ebccSVladimir Oltean struct sja1105_table *table; 1928aa9ebccSVladimir Oltean 1938aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 1948aa9ebccSVladimir Oltean 195291d1e72SVladimir Oltean /* We only populate the FDB table through dynamic 196291d1e72SVladimir Oltean * L2 Address Lookup entries 197291d1e72SVladimir Oltean */ 1988aa9ebccSVladimir Oltean if (table->entry_count) { 1998aa9ebccSVladimir Oltean kfree(table->entries); 2008aa9ebccSVladimir Oltean table->entry_count = 0; 2018aa9ebccSVladimir Oltean } 2028aa9ebccSVladimir Oltean return 0; 2038aa9ebccSVladimir Oltean } 2048aa9ebccSVladimir Oltean 2058aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 2068aa9ebccSVladimir Oltean { 2078aa9ebccSVladimir Oltean struct sja1105_table *table; 2088aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 2098456721dSVladimir Oltean /* Learned FDB entries are forgotten after 300 seconds */ 2108456721dSVladimir Oltean .maxage = SJA1105_AGEING_TIME_MS(300000), 2118aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 2128aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 2131da73821SVladimir Oltean /* And the P/Q/R/S equivalent setting: */ 2141da73821SVladimir Oltean .start_dynspc = 0, 2158aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 2168aa9ebccSVladimir Oltean .poly = 0x97, 2178aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 2188aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 2198aa9ebccSVladimir Oltean */ 2208aa9ebccSVladimir Oltean .shared_learn = false, 2218aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 2228aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 2238aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 2248aa9ebccSVladimir Oltean */ 2258aa9ebccSVladimir Oltean .no_enf_hostprt = false, 2268aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 2278aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 2288aa9ebccSVladimir Oltean */ 2298aa9ebccSVladimir Oltean .no_mgmt_learn = true, 2301da73821SVladimir Oltean /* P/Q/R/S only */ 2311da73821SVladimir Oltean .use_static = true, 2321da73821SVladimir Oltean /* Dynamically learned FDB entries can overwrite other (older) 2331da73821SVladimir Oltean * dynamic FDB entries 2341da73821SVladimir Oltean */ 2351da73821SVladimir Oltean .owr_dyn = true, 2361da73821SVladimir Oltean .drpnolearn = true, 2378aa9ebccSVladimir Oltean }; 2388aa9ebccSVladimir Oltean 2398aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 2408aa9ebccSVladimir Oltean 2418aa9ebccSVladimir Oltean if (table->entry_count) { 2428aa9ebccSVladimir Oltean kfree(table->entries); 2438aa9ebccSVladimir Oltean table->entry_count = 0; 2448aa9ebccSVladimir Oltean } 2458aa9ebccSVladimir Oltean 2468aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT, 2478aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2488aa9ebccSVladimir Oltean if (!table->entries) 2498aa9ebccSVladimir Oltean return -ENOMEM; 2508aa9ebccSVladimir Oltean 2518aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT; 2528aa9ebccSVladimir Oltean 2538aa9ebccSVladimir Oltean /* This table only has a single entry */ 2548aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 2558aa9ebccSVladimir Oltean default_l2_lookup_params; 2568aa9ebccSVladimir Oltean 2578aa9ebccSVladimir Oltean return 0; 2588aa9ebccSVladimir Oltean } 2598aa9ebccSVladimir Oltean 2608aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 2618aa9ebccSVladimir Oltean { 2628aa9ebccSVladimir Oltean struct sja1105_table *table; 2638aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 2648aa9ebccSVladimir Oltean .ving_mirr = 0, 2658aa9ebccSVladimir Oltean .vegr_mirr = 0, 2668aa9ebccSVladimir Oltean .vmemb_port = 0, 2678aa9ebccSVladimir Oltean .vlan_bc = 0, 2688aa9ebccSVladimir Oltean .tag_port = 0, 2698aa9ebccSVladimir Oltean .vlanid = 0, 2708aa9ebccSVladimir Oltean }; 2718aa9ebccSVladimir Oltean int i; 2728aa9ebccSVladimir Oltean 2738aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2748aa9ebccSVladimir Oltean 2758aa9ebccSVladimir Oltean /* The static VLAN table will only contain the initial pvid of 0. 2766666cebcSVladimir Oltean * All other VLANs are to be configured through dynamic entries, 2776666cebcSVladimir Oltean * and kept in the static configuration table as backing memory. 2786666cebcSVladimir Oltean * The pvid of 0 is sufficient to pass traffic while the ports are 2796666cebcSVladimir Oltean * standalone and when vlan_filtering is disabled. When filtering 2806666cebcSVladimir Oltean * gets enabled, the switchdev core sets up the VLAN ID 1 and sets 2816666cebcSVladimir Oltean * it as the new pvid. Actually 'pvid 1' still comes up in 'bridge 2826666cebcSVladimir Oltean * vlan' even when vlan_filtering is off, but it has no effect. 2838aa9ebccSVladimir Oltean */ 2848aa9ebccSVladimir Oltean if (table->entry_count) { 2858aa9ebccSVladimir Oltean kfree(table->entries); 2868aa9ebccSVladimir Oltean table->entry_count = 0; 2878aa9ebccSVladimir Oltean } 2888aa9ebccSVladimir Oltean 2898aa9ebccSVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 2908aa9ebccSVladimir Oltean GFP_KERNEL); 2918aa9ebccSVladimir Oltean if (!table->entries) 2928aa9ebccSVladimir Oltean return -ENOMEM; 2938aa9ebccSVladimir Oltean 2948aa9ebccSVladimir Oltean table->entry_count = 1; 2958aa9ebccSVladimir Oltean 2968aa9ebccSVladimir Oltean /* VLAN ID 0: all DT-defined ports are members; no restrictions on 2978aa9ebccSVladimir Oltean * forwarding; always transmit priority-tagged frames as untagged. 2988aa9ebccSVladimir Oltean */ 2998aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 3008aa9ebccSVladimir Oltean pvid.vmemb_port |= BIT(i); 3018aa9ebccSVladimir Oltean pvid.vlan_bc |= BIT(i); 3028aa9ebccSVladimir Oltean pvid.tag_port &= ~BIT(i); 3038aa9ebccSVladimir Oltean } 3048aa9ebccSVladimir Oltean 3058aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 3068aa9ebccSVladimir Oltean return 0; 3078aa9ebccSVladimir Oltean } 3088aa9ebccSVladimir Oltean 3098aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 3108aa9ebccSVladimir Oltean { 3118aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 3128aa9ebccSVladimir Oltean struct sja1105_table *table; 3138aa9ebccSVladimir Oltean int i, j; 3148aa9ebccSVladimir Oltean 3158aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 3168aa9ebccSVladimir Oltean 3178aa9ebccSVladimir Oltean if (table->entry_count) { 3188aa9ebccSVladimir Oltean kfree(table->entries); 3198aa9ebccSVladimir Oltean table->entry_count = 0; 3208aa9ebccSVladimir Oltean } 3218aa9ebccSVladimir Oltean 3228aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT, 3238aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3248aa9ebccSVladimir Oltean if (!table->entries) 3258aa9ebccSVladimir Oltean return -ENOMEM; 3268aa9ebccSVladimir Oltean 3278aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT; 3288aa9ebccSVladimir Oltean 3298aa9ebccSVladimir Oltean l2fwd = table->entries; 3308aa9ebccSVladimir Oltean 3318aa9ebccSVladimir Oltean /* First 5 entries define the forwarding rules */ 3328aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 3338aa9ebccSVladimir Oltean unsigned int upstream = dsa_upstream_port(priv->ds, i); 3348aa9ebccSVladimir Oltean 3358aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++) 3368aa9ebccSVladimir Oltean l2fwd[i].vlan_pmap[j] = j; 3378aa9ebccSVladimir Oltean 3388aa9ebccSVladimir Oltean if (i == upstream) 3398aa9ebccSVladimir Oltean continue; 3408aa9ebccSVladimir Oltean 3418aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, i, upstream, true); 3428aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, upstream, i, true); 3438aa9ebccSVladimir Oltean } 3448aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 3458aa9ebccSVladimir Oltean * Create a one-to-one mapping. 3468aa9ebccSVladimir Oltean */ 3478aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_TC; i++) 3488aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_PORTS; j++) 3498aa9ebccSVladimir Oltean l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i; 3508aa9ebccSVladimir Oltean 3518aa9ebccSVladimir Oltean return 0; 3528aa9ebccSVladimir Oltean } 3538aa9ebccSVladimir Oltean 3548aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 3558aa9ebccSVladimir Oltean { 3568aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_params_entry default_l2fwd_params = { 3578aa9ebccSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 3588aa9ebccSVladimir Oltean .max_dynp = 0, 3598aa9ebccSVladimir Oltean /* Use a single memory partition for all ingress queues */ 3608aa9ebccSVladimir Oltean .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 }, 3618aa9ebccSVladimir Oltean }; 3628aa9ebccSVladimir Oltean struct sja1105_table *table; 3638aa9ebccSVladimir Oltean 3648aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 3658aa9ebccSVladimir Oltean 3668aa9ebccSVladimir Oltean if (table->entry_count) { 3678aa9ebccSVladimir Oltean kfree(table->entries); 3688aa9ebccSVladimir Oltean table->entry_count = 0; 3698aa9ebccSVladimir Oltean } 3708aa9ebccSVladimir Oltean 3718aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 3728aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3738aa9ebccSVladimir Oltean if (!table->entries) 3748aa9ebccSVladimir Oltean return -ENOMEM; 3758aa9ebccSVladimir Oltean 3768aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT; 3778aa9ebccSVladimir Oltean 3788aa9ebccSVladimir Oltean /* This table only has a single entry */ 3798aa9ebccSVladimir Oltean ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] = 3808aa9ebccSVladimir Oltean default_l2fwd_params; 3818aa9ebccSVladimir Oltean 3828aa9ebccSVladimir Oltean return 0; 3838aa9ebccSVladimir Oltean } 3848aa9ebccSVladimir Oltean 3858aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 3868aa9ebccSVladimir Oltean { 3878aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 3888aa9ebccSVladimir Oltean /* Disallow dynamic changing of the mirror port */ 3898aa9ebccSVladimir Oltean .mirr_ptacu = 0, 3908aa9ebccSVladimir Oltean .switchid = priv->ds->index, 3918aa9ebccSVladimir Oltean /* Priority queue for link-local frames trapped to CPU */ 3928aa9ebccSVladimir Oltean .hostprio = 0, 3938aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 3948aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 3958aa9ebccSVladimir Oltean .incl_srcpt1 = true, 3968aa9ebccSVladimir Oltean .send_meta1 = false, 3978aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 3988aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 3998aa9ebccSVladimir Oltean .incl_srcpt0 = true, 4008aa9ebccSVladimir Oltean .send_meta0 = false, 4018aa9ebccSVladimir Oltean /* The destination for traffic matching mac_fltres1 and 4028aa9ebccSVladimir Oltean * mac_fltres0 on all ports except host_port. Such traffic 4038aa9ebccSVladimir Oltean * receieved on host_port itself would be dropped, except 4048aa9ebccSVladimir Oltean * by installing a temporary 'management route' 4058aa9ebccSVladimir Oltean */ 4068aa9ebccSVladimir Oltean .host_port = dsa_upstream_port(priv->ds, 0), 4078aa9ebccSVladimir Oltean /* Same as host port */ 4088aa9ebccSVladimir Oltean .mirr_port = dsa_upstream_port(priv->ds, 0), 4098aa9ebccSVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 4108aa9ebccSVladimir Oltean * to host_port without embedding the source port and device ID 4118aa9ebccSVladimir Oltean * info in the destination MAC address (presumably because it 4128aa9ebccSVladimir Oltean * is a cascaded port and a downstream SJA switch already did 4138aa9ebccSVladimir Oltean * that). Default to an invalid port (to disable the feature) 4148aa9ebccSVladimir Oltean * and overwrite this if we find any DSA (cascaded) ports. 4158aa9ebccSVladimir Oltean */ 4168aa9ebccSVladimir Oltean .casc_port = SJA1105_NUM_PORTS, 4178aa9ebccSVladimir Oltean /* No TTEthernet */ 4188aa9ebccSVladimir Oltean .vllupformat = 0, 4198aa9ebccSVladimir Oltean .vlmarker = 0, 4208aa9ebccSVladimir Oltean .vlmask = 0, 4218aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 4228aa9ebccSVladimir Oltean .ignore2stf = 0, 4236666cebcSVladimir Oltean /* Forcefully disable VLAN filtering by telling 4246666cebcSVladimir Oltean * the switch that VLAN has a different EtherType. 4256666cebcSVladimir Oltean */ 4266666cebcSVladimir Oltean .tpid = ETH_P_SJA1105, 4276666cebcSVladimir Oltean .tpid2 = ETH_P_SJA1105, 4288aa9ebccSVladimir Oltean }; 4298aa9ebccSVladimir Oltean struct sja1105_table *table; 430227d07a0SVladimir Oltean int i, k = 0; 4318aa9ebccSVladimir Oltean 432227d07a0SVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 4338aa9ebccSVladimir Oltean if (dsa_is_dsa_port(priv->ds, i)) 4348aa9ebccSVladimir Oltean default_general_params.casc_port = i; 435227d07a0SVladimir Oltean else if (dsa_is_user_port(priv->ds, i)) 436227d07a0SVladimir Oltean priv->ports[i].mgmt_slot = k++; 437227d07a0SVladimir Oltean } 4388aa9ebccSVladimir Oltean 4398aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 4408aa9ebccSVladimir Oltean 4418aa9ebccSVladimir Oltean if (table->entry_count) { 4428aa9ebccSVladimir Oltean kfree(table->entries); 4438aa9ebccSVladimir Oltean table->entry_count = 0; 4448aa9ebccSVladimir Oltean } 4458aa9ebccSVladimir Oltean 4468aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT, 4478aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4488aa9ebccSVladimir Oltean if (!table->entries) 4498aa9ebccSVladimir Oltean return -ENOMEM; 4508aa9ebccSVladimir Oltean 4518aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT; 4528aa9ebccSVladimir Oltean 4538aa9ebccSVladimir Oltean /* This table only has a single entry */ 4548aa9ebccSVladimir Oltean ((struct sja1105_general_params_entry *)table->entries)[0] = 4558aa9ebccSVladimir Oltean default_general_params; 4568aa9ebccSVladimir Oltean 4578aa9ebccSVladimir Oltean return 0; 4588aa9ebccSVladimir Oltean } 4598aa9ebccSVladimir Oltean 4608aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 4618aa9ebccSVladimir Oltean 4628aa9ebccSVladimir Oltean static inline void 4638aa9ebccSVladimir Oltean sja1105_setup_policer(struct sja1105_l2_policing_entry *policing, 4648aa9ebccSVladimir Oltean int index) 4658aa9ebccSVladimir Oltean { 4668aa9ebccSVladimir Oltean policing[index].sharindx = index; 4678aa9ebccSVladimir Oltean policing[index].smax = 65535; /* Burst size in bytes */ 4688aa9ebccSVladimir Oltean policing[index].rate = SJA1105_RATE_MBPS(1000); 4698aa9ebccSVladimir Oltean policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN; 4708aa9ebccSVladimir Oltean policing[index].partition = 0; 4718aa9ebccSVladimir Oltean } 4728aa9ebccSVladimir Oltean 4738aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 4748aa9ebccSVladimir Oltean { 4758aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 4768aa9ebccSVladimir Oltean struct sja1105_table *table; 4778aa9ebccSVladimir Oltean int i, j, k; 4788aa9ebccSVladimir Oltean 4798aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 4808aa9ebccSVladimir Oltean 4818aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 4828aa9ebccSVladimir Oltean if (table->entry_count) { 4838aa9ebccSVladimir Oltean kfree(table->entries); 4848aa9ebccSVladimir Oltean table->entry_count = 0; 4858aa9ebccSVladimir Oltean } 4868aa9ebccSVladimir Oltean 4878aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT, 4888aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4898aa9ebccSVladimir Oltean if (!table->entries) 4908aa9ebccSVladimir Oltean return -ENOMEM; 4918aa9ebccSVladimir Oltean 4928aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_POLICING_COUNT; 4938aa9ebccSVladimir Oltean 4948aa9ebccSVladimir Oltean policing = table->entries; 4958aa9ebccSVladimir Oltean 4968aa9ebccSVladimir Oltean /* k sweeps through all unicast policers (0-39). 4978aa9ebccSVladimir Oltean * bcast sweeps through policers 40-44. 4988aa9ebccSVladimir Oltean */ 4998aa9ebccSVladimir Oltean for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) { 5008aa9ebccSVladimir Oltean int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i; 5018aa9ebccSVladimir Oltean 5028aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++, k++) 5038aa9ebccSVladimir Oltean sja1105_setup_policer(policing, k); 5048aa9ebccSVladimir Oltean 5058aa9ebccSVladimir Oltean /* Set up this port's policer for broadcast traffic */ 5068aa9ebccSVladimir Oltean sja1105_setup_policer(policing, bcast); 5078aa9ebccSVladimir Oltean } 5088aa9ebccSVladimir Oltean return 0; 5098aa9ebccSVladimir Oltean } 5108aa9ebccSVladimir Oltean 5118aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv, 5128aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 5138aa9ebccSVladimir Oltean { 5148aa9ebccSVladimir Oltean int rc; 5158aa9ebccSVladimir Oltean 5168aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 5178aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 5188aa9ebccSVladimir Oltean priv->info->static_ops, 5198aa9ebccSVladimir Oltean priv->info->device_id); 5208aa9ebccSVladimir Oltean if (rc) 5218aa9ebccSVladimir Oltean return rc; 5228aa9ebccSVladimir Oltean 5238aa9ebccSVladimir Oltean /* Build static configuration */ 5248aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 5258aa9ebccSVladimir Oltean if (rc < 0) 5268aa9ebccSVladimir Oltean return rc; 5278aa9ebccSVladimir Oltean rc = sja1105_init_mii_settings(priv, ports); 5288aa9ebccSVladimir Oltean if (rc < 0) 5298aa9ebccSVladimir Oltean return rc; 5308aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 5318aa9ebccSVladimir Oltean if (rc < 0) 5328aa9ebccSVladimir Oltean return rc; 5338aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 5348aa9ebccSVladimir Oltean if (rc < 0) 5358aa9ebccSVladimir Oltean return rc; 5368aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 5378aa9ebccSVladimir Oltean if (rc < 0) 5388aa9ebccSVladimir Oltean return rc; 5398aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 5408aa9ebccSVladimir Oltean if (rc < 0) 5418aa9ebccSVladimir Oltean return rc; 5428aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 5438aa9ebccSVladimir Oltean if (rc < 0) 5448aa9ebccSVladimir Oltean return rc; 5458aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 5468aa9ebccSVladimir Oltean if (rc < 0) 5478aa9ebccSVladimir Oltean return rc; 5488aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 5498aa9ebccSVladimir Oltean if (rc < 0) 5508aa9ebccSVladimir Oltean return rc; 5518aa9ebccSVladimir Oltean 5528aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 5538aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 5548aa9ebccSVladimir Oltean } 5558aa9ebccSVladimir Oltean 556f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, 557f5b8631cSVladimir Oltean const struct sja1105_dt_port *ports) 558f5b8631cSVladimir Oltean { 559f5b8631cSVladimir Oltean int i; 560f5b8631cSVladimir Oltean 561f5b8631cSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 562f5b8631cSVladimir Oltean if (ports->role == XMII_MAC) 563f5b8631cSVladimir Oltean continue; 564f5b8631cSVladimir Oltean 565f5b8631cSVladimir Oltean if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || 566f5b8631cSVladimir Oltean ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 567f5b8631cSVladimir Oltean priv->rgmii_rx_delay[i] = true; 568f5b8631cSVladimir Oltean 569f5b8631cSVladimir Oltean if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || 570f5b8631cSVladimir Oltean ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 571f5b8631cSVladimir Oltean priv->rgmii_tx_delay[i] = true; 572f5b8631cSVladimir Oltean 573f5b8631cSVladimir Oltean if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) && 574f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 575f5b8631cSVladimir Oltean return -EINVAL; 576f5b8631cSVladimir Oltean } 577f5b8631cSVladimir Oltean return 0; 578f5b8631cSVladimir Oltean } 579f5b8631cSVladimir Oltean 5808aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 5818aa9ebccSVladimir Oltean struct sja1105_dt_port *ports, 5828aa9ebccSVladimir Oltean struct device_node *ports_node) 5838aa9ebccSVladimir Oltean { 5848aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 5858aa9ebccSVladimir Oltean struct device_node *child; 5868aa9ebccSVladimir Oltean 5878aa9ebccSVladimir Oltean for_each_child_of_node(ports_node, child) { 5888aa9ebccSVladimir Oltean struct device_node *phy_node; 5898aa9ebccSVladimir Oltean int phy_mode; 5908aa9ebccSVladimir Oltean u32 index; 5918aa9ebccSVladimir Oltean 5928aa9ebccSVladimir Oltean /* Get switch port number from DT */ 5938aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 5948aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 5958aa9ebccSVladimir Oltean "(property \"reg\")\n"); 5968aa9ebccSVladimir Oltean return -ENODEV; 5978aa9ebccSVladimir Oltean } 5988aa9ebccSVladimir Oltean 5998aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 6008aa9ebccSVladimir Oltean phy_mode = of_get_phy_mode(child); 6018aa9ebccSVladimir Oltean if (phy_mode < 0) { 6028aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 6038aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 6048aa9ebccSVladimir Oltean index); 6058aa9ebccSVladimir Oltean return -ENODEV; 6068aa9ebccSVladimir Oltean } 6078aa9ebccSVladimir Oltean ports[index].phy_mode = phy_mode; 6088aa9ebccSVladimir Oltean 6098aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 6108aa9ebccSVladimir Oltean if (!phy_node) { 6118aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 6128aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 6138aa9ebccSVladimir Oltean "properties missing!\n"); 6148aa9ebccSVladimir Oltean return -ENODEV; 6158aa9ebccSVladimir Oltean } 6168aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 6178aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 6188aa9ebccSVladimir Oltean */ 6198aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 6208aa9ebccSVladimir Oltean } else { 6218aa9ebccSVladimir Oltean /* phy-handle present => put port in MAC role */ 6228aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 6238aa9ebccSVladimir Oltean of_node_put(phy_node); 6248aa9ebccSVladimir Oltean } 6258aa9ebccSVladimir Oltean 6268aa9ebccSVladimir Oltean /* The MAC/PHY role can be overridden with explicit bindings */ 6278aa9ebccSVladimir Oltean if (of_property_read_bool(child, "sja1105,role-mac")) 6288aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 6298aa9ebccSVladimir Oltean else if (of_property_read_bool(child, "sja1105,role-phy")) 6308aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 6318aa9ebccSVladimir Oltean } 6328aa9ebccSVladimir Oltean 6338aa9ebccSVladimir Oltean return 0; 6348aa9ebccSVladimir Oltean } 6358aa9ebccSVladimir Oltean 6368aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv, 6378aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 6388aa9ebccSVladimir Oltean { 6398aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 6408aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 6418aa9ebccSVladimir Oltean struct device_node *ports_node; 6428aa9ebccSVladimir Oltean int rc; 6438aa9ebccSVladimir Oltean 6448aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 6458aa9ebccSVladimir Oltean if (!ports_node) { 6468aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 6478aa9ebccSVladimir Oltean return -ENODEV; 6488aa9ebccSVladimir Oltean } 6498aa9ebccSVladimir Oltean 6508aa9ebccSVladimir Oltean rc = sja1105_parse_ports_node(priv, ports, ports_node); 6518aa9ebccSVladimir Oltean of_node_put(ports_node); 6528aa9ebccSVladimir Oltean 6538aa9ebccSVladimir Oltean return rc; 6548aa9ebccSVladimir Oltean } 6558aa9ebccSVladimir Oltean 6568aa9ebccSVladimir Oltean /* Convert back and forth MAC speed from Mbps to SJA1105 encoding */ 6578aa9ebccSVladimir Oltean static int sja1105_speed[] = { 6588aa9ebccSVladimir Oltean [SJA1105_SPEED_AUTO] = 0, 6598aa9ebccSVladimir Oltean [SJA1105_SPEED_10MBPS] = 10, 6608aa9ebccSVladimir Oltean [SJA1105_SPEED_100MBPS] = 100, 6618aa9ebccSVladimir Oltean [SJA1105_SPEED_1000MBPS] = 1000, 6628aa9ebccSVladimir Oltean }; 6638aa9ebccSVladimir Oltean 6648aa9ebccSVladimir Oltean static sja1105_speed_t sja1105_get_speed_cfg(unsigned int speed_mbps) 6658aa9ebccSVladimir Oltean { 6668aa9ebccSVladimir Oltean int i; 6678aa9ebccSVladimir Oltean 6688aa9ebccSVladimir Oltean for (i = SJA1105_SPEED_AUTO; i <= SJA1105_SPEED_1000MBPS; i++) 6698aa9ebccSVladimir Oltean if (sja1105_speed[i] == speed_mbps) 6708aa9ebccSVladimir Oltean return i; 6718aa9ebccSVladimir Oltean return -EINVAL; 6728aa9ebccSVladimir Oltean } 6738aa9ebccSVladimir Oltean 6748aa9ebccSVladimir Oltean /* Set link speed and enable/disable traffic I/O in the MAC configuration 6758aa9ebccSVladimir Oltean * for a specific port. 6768aa9ebccSVladimir Oltean * 6778aa9ebccSVladimir Oltean * @speed_mbps: If 0, leave the speed unchanged, else adapt MAC to PHY speed. 678640f763fSVladimir Oltean * @enabled: Manage Rx and Tx settings for this port. If false, overrides the 679640f763fSVladimir Oltean * settings from the STP state, but not persistently (does not 680640f763fSVladimir Oltean * overwrite the static MAC info for this port). 6818aa9ebccSVladimir Oltean */ 6828aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 6838aa9ebccSVladimir Oltean int speed_mbps, bool enabled) 6848aa9ebccSVladimir Oltean { 685640f763fSVladimir Oltean struct sja1105_mac_config_entry dyn_mac; 6868aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 6878aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 6888aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 6898aa9ebccSVladimir Oltean sja1105_phy_interface_t phy_mode; 6908aa9ebccSVladimir Oltean sja1105_speed_t speed; 6918aa9ebccSVladimir Oltean int rc; 6928aa9ebccSVladimir Oltean 6938aa9ebccSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 6948aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 6958aa9ebccSVladimir Oltean 6968aa9ebccSVladimir Oltean speed = sja1105_get_speed_cfg(speed_mbps); 6978aa9ebccSVladimir Oltean if (speed_mbps && speed < 0) { 6988aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 6998aa9ebccSVladimir Oltean return -EINVAL; 7008aa9ebccSVladimir Oltean } 7018aa9ebccSVladimir Oltean 7028aa9ebccSVladimir Oltean /* If requested, overwrite SJA1105_SPEED_AUTO from the static MAC 7038aa9ebccSVladimir Oltean * configuration table, since this will be used for the clocking setup, 7048aa9ebccSVladimir Oltean * and we no longer need to store it in the static config (already told 7058aa9ebccSVladimir Oltean * hardware we want auto during upload phase). 7068aa9ebccSVladimir Oltean */ 7078aa9ebccSVladimir Oltean if (speed_mbps) 7088aa9ebccSVladimir Oltean mac[port].speed = speed; 7098aa9ebccSVladimir Oltean else 7108aa9ebccSVladimir Oltean mac[port].speed = SJA1105_SPEED_AUTO; 7118aa9ebccSVladimir Oltean 7128aa9ebccSVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 7138aa9ebccSVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 7148aa9ebccSVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 7158aa9ebccSVladimir Oltean * the code common, we'll use the static configuration tables as a 7168aa9ebccSVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 7178aa9ebccSVladimir Oltean */ 718640f763fSVladimir Oltean dyn_mac = mac[port]; 719640f763fSVladimir Oltean dyn_mac.ingress = enabled && mac[port].ingress; 720640f763fSVladimir Oltean dyn_mac.egress = enabled && mac[port].egress; 7218aa9ebccSVladimir Oltean 7228aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 7238aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, 724640f763fSVladimir Oltean port, &dyn_mac, true); 7258aa9ebccSVladimir Oltean if (rc < 0) { 7268aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 7278aa9ebccSVladimir Oltean return rc; 7288aa9ebccSVladimir Oltean } 7298aa9ebccSVladimir Oltean 7308aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 7318aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 7328aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 7338aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 7348aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 7358aa9ebccSVladimir Oltean */ 7368aa9ebccSVladimir Oltean if (!enabled) 7378aa9ebccSVladimir Oltean return 0; 7388aa9ebccSVladimir Oltean 7398aa9ebccSVladimir Oltean phy_mode = mii->xmii_mode[port]; 7408aa9ebccSVladimir Oltean if (phy_mode != XMII_MODE_RGMII) 7418aa9ebccSVladimir Oltean return 0; 7428aa9ebccSVladimir Oltean 7438aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 7448aa9ebccSVladimir Oltean } 7458aa9ebccSVladimir Oltean 746af7cd036SVladimir Oltean static void sja1105_mac_config(struct dsa_switch *ds, int port, 747af7cd036SVladimir Oltean unsigned int link_an_mode, 748af7cd036SVladimir Oltean const struct phylink_link_state *state) 7498aa9ebccSVladimir Oltean { 7508aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 7518aa9ebccSVladimir Oltean 752af7cd036SVladimir Oltean if (!state->link) 7538aa9ebccSVladimir Oltean sja1105_adjust_port_config(priv, port, 0, false); 7548aa9ebccSVladimir Oltean else 755af7cd036SVladimir Oltean sja1105_adjust_port_config(priv, port, state->speed, true); 7568aa9ebccSVladimir Oltean } 7578aa9ebccSVladimir Oltean 758ad9f299aSVladimir Oltean static void sja1105_phylink_validate(struct dsa_switch *ds, int port, 759ad9f299aSVladimir Oltean unsigned long *supported, 760ad9f299aSVladimir Oltean struct phylink_link_state *state) 761ad9f299aSVladimir Oltean { 762ad9f299aSVladimir Oltean /* Construct a new mask which exhaustively contains all link features 763ad9f299aSVladimir Oltean * supported by the MAC, and then apply that (logical AND) to what will 764ad9f299aSVladimir Oltean * be sent to the PHY for "marketing". 765ad9f299aSVladimir Oltean */ 766ad9f299aSVladimir Oltean __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 767ad9f299aSVladimir Oltean struct sja1105_private *priv = ds->priv; 768ad9f299aSVladimir Oltean struct sja1105_xmii_params_entry *mii; 769ad9f299aSVladimir Oltean 770ad9f299aSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 771ad9f299aSVladimir Oltean 772ad9f299aSVladimir Oltean /* The MAC does not support pause frames, and also doesn't 773ad9f299aSVladimir Oltean * support half-duplex traffic modes. 774ad9f299aSVladimir Oltean */ 775ad9f299aSVladimir Oltean phylink_set(mask, Autoneg); 776ad9f299aSVladimir Oltean phylink_set(mask, MII); 777ad9f299aSVladimir Oltean phylink_set(mask, 10baseT_Full); 778ad9f299aSVladimir Oltean phylink_set(mask, 100baseT_Full); 779ad9f299aSVladimir Oltean if (mii->xmii_mode[port] == XMII_MODE_RGMII) 780ad9f299aSVladimir Oltean phylink_set(mask, 1000baseT_Full); 781ad9f299aSVladimir Oltean 782ad9f299aSVladimir Oltean bitmap_and(supported, supported, mask, __ETHTOOL_LINK_MODE_MASK_NBITS); 783ad9f299aSVladimir Oltean bitmap_and(state->advertising, state->advertising, mask, 784ad9f299aSVladimir Oltean __ETHTOOL_LINK_MODE_MASK_NBITS); 785ad9f299aSVladimir Oltean } 786ad9f299aSVladimir Oltean 787291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 788291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 789291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 790291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 791291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 792291d1e72SVladimir Oltean */ 793291d1e72SVladimir Oltean static inline int sja1105et_fdb_index(int bin, int way) 794291d1e72SVladimir Oltean { 795291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 796291d1e72SVladimir Oltean } 797291d1e72SVladimir Oltean 7989dfa6911SVladimir Oltean static int sja1105et_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 799291d1e72SVladimir Oltean const u8 *addr, u16 vid, 800291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 801291d1e72SVladimir Oltean int *last_unused) 802291d1e72SVladimir Oltean { 803291d1e72SVladimir Oltean int way; 804291d1e72SVladimir Oltean 805291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 806291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 807291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 808291d1e72SVladimir Oltean 809291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 810291d1e72SVladimir Oltean * into the return value 811291d1e72SVladimir Oltean */ 812291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 813291d1e72SVladimir Oltean index, &l2_lookup)) { 814291d1e72SVladimir Oltean if (last_unused) 815291d1e72SVladimir Oltean *last_unused = way; 816291d1e72SVladimir Oltean continue; 817291d1e72SVladimir Oltean } 818291d1e72SVladimir Oltean 819291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 820291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 821291d1e72SVladimir Oltean if (match) 822291d1e72SVladimir Oltean *match = l2_lookup; 823291d1e72SVladimir Oltean return way; 824291d1e72SVladimir Oltean } 825291d1e72SVladimir Oltean } 826291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 827291d1e72SVladimir Oltean return -1; 828291d1e72SVladimir Oltean } 829291d1e72SVladimir Oltean 8309dfa6911SVladimir Oltean int sja1105et_fdb_add(struct dsa_switch *ds, int port, 831291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 832291d1e72SVladimir Oltean { 833291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 834291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 835291d1e72SVladimir Oltean struct device *dev = ds->dev; 836291d1e72SVladimir Oltean int last_unused = -1; 837291d1e72SVladimir Oltean int bin, way; 838291d1e72SVladimir Oltean 8399dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 840291d1e72SVladimir Oltean 8419dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 842291d1e72SVladimir Oltean &l2_lookup, &last_unused); 843291d1e72SVladimir Oltean if (way >= 0) { 844291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 845291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 846291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 847291d1e72SVladimir Oltean */ 848291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 849291d1e72SVladimir Oltean return 0; 850291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 851291d1e72SVladimir Oltean } else { 852291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 853291d1e72SVladimir Oltean 854291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 855291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 856291d1e72SVladimir Oltean */ 857291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 858291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 859291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 860291d1e72SVladimir Oltean 861291d1e72SVladimir Oltean if (last_unused >= 0) { 862291d1e72SVladimir Oltean way = last_unused; 863291d1e72SVladimir Oltean } else { 864291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 865291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 866291d1e72SVladimir Oltean * often, you may need to consider changing the 867291d1e72SVladimir Oltean * distribution function: 868291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 869291d1e72SVladimir Oltean */ 870291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 871291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 872291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 873291d1e72SVladimir Oltean bin, addr, way); 874291d1e72SVladimir Oltean /* Evict entry */ 875291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 876291d1e72SVladimir Oltean index, NULL, false); 877291d1e72SVladimir Oltean } 878291d1e72SVladimir Oltean } 879291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 880291d1e72SVladimir Oltean 881291d1e72SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 882291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 883291d1e72SVladimir Oltean true); 884291d1e72SVladimir Oltean } 885291d1e72SVladimir Oltean 8869dfa6911SVladimir Oltean int sja1105et_fdb_del(struct dsa_switch *ds, int port, 887291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 888291d1e72SVladimir Oltean { 889291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 890291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 891291d1e72SVladimir Oltean int index, bin, way; 892291d1e72SVladimir Oltean bool keep; 893291d1e72SVladimir Oltean 8949dfa6911SVladimir Oltean bin = sja1105et_fdb_hash(priv, addr, vid); 8959dfa6911SVladimir Oltean way = sja1105et_is_fdb_entry_in_bin(priv, bin, addr, vid, 896291d1e72SVladimir Oltean &l2_lookup, NULL); 897291d1e72SVladimir Oltean if (way < 0) 898291d1e72SVladimir Oltean return 0; 899291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 900291d1e72SVladimir Oltean 901291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 902291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 903291d1e72SVladimir Oltean * need to completely evict the FDB entry. 904291d1e72SVladimir Oltean * Otherwise we just write it back. 905291d1e72SVladimir Oltean */ 906291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 907*7752e937SVladimir Oltean 908291d1e72SVladimir Oltean if (l2_lookup.destports) 909291d1e72SVladimir Oltean keep = true; 910291d1e72SVladimir Oltean else 911291d1e72SVladimir Oltean keep = false; 912291d1e72SVladimir Oltean 913291d1e72SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 914291d1e72SVladimir Oltean index, &l2_lookup, keep); 915291d1e72SVladimir Oltean } 916291d1e72SVladimir Oltean 9179dfa6911SVladimir Oltean int sja1105pqrs_fdb_add(struct dsa_switch *ds, int port, 9189dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 9199dfa6911SVladimir Oltean { 9201da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 9211da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 9221da73821SVladimir Oltean int rc, i; 9231da73821SVladimir Oltean 9241da73821SVladimir Oltean /* Search for an existing entry in the FDB table */ 9251da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 9261da73821SVladimir Oltean l2_lookup.vlanid = vid; 9271da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 9281da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 9291da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 9301da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 9311da73821SVladimir Oltean l2_lookup.destports = BIT(port); 9321da73821SVladimir Oltean 9331da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 9341da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 9351da73821SVladimir Oltean if (rc == 0) { 9361da73821SVladimir Oltean /* Found and this port is already in the entry's 9371da73821SVladimir Oltean * port mask => job done 9381da73821SVladimir Oltean */ 9391da73821SVladimir Oltean if (l2_lookup.destports & BIT(port)) 9401da73821SVladimir Oltean return 0; 9411da73821SVladimir Oltean /* l2_lookup.index is populated by the switch in case it 9421da73821SVladimir Oltean * found something. 9431da73821SVladimir Oltean */ 9441da73821SVladimir Oltean l2_lookup.destports |= BIT(port); 9451da73821SVladimir Oltean goto skip_finding_an_index; 9461da73821SVladimir Oltean } 9471da73821SVladimir Oltean 9481da73821SVladimir Oltean /* Not found, so try to find an unused spot in the FDB. 9491da73821SVladimir Oltean * This is slightly inefficient because the strategy is knock-knock at 9501da73821SVladimir Oltean * every possible position from 0 to 1023. 9511da73821SVladimir Oltean */ 9521da73821SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 9531da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 9541da73821SVladimir Oltean i, NULL); 9551da73821SVladimir Oltean if (rc < 0) 9561da73821SVladimir Oltean break; 9571da73821SVladimir Oltean } 9581da73821SVladimir Oltean if (i == SJA1105_MAX_L2_LOOKUP_COUNT) { 9591da73821SVladimir Oltean dev_err(ds->dev, "FDB is full, cannot add entry.\n"); 9601da73821SVladimir Oltean return -EINVAL; 9611da73821SVladimir Oltean } 9621da73821SVladimir Oltean l2_lookup.index = i; 9631da73821SVladimir Oltean 9641da73821SVladimir Oltean skip_finding_an_index: 9651da73821SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 9661da73821SVladimir Oltean l2_lookup.index, &l2_lookup, 9671da73821SVladimir Oltean true); 9689dfa6911SVladimir Oltean } 9699dfa6911SVladimir Oltean 9709dfa6911SVladimir Oltean int sja1105pqrs_fdb_del(struct dsa_switch *ds, int port, 9719dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 9729dfa6911SVladimir Oltean { 9731da73821SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 9741da73821SVladimir Oltean struct sja1105_private *priv = ds->priv; 9751da73821SVladimir Oltean bool keep; 9761da73821SVladimir Oltean int rc; 9771da73821SVladimir Oltean 9781da73821SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 9791da73821SVladimir Oltean l2_lookup.vlanid = vid; 9801da73821SVladimir Oltean l2_lookup.iotag = SJA1105_S_TAG; 9811da73821SVladimir Oltean l2_lookup.mask_macaddr = GENMASK_ULL(ETH_ALEN * 8 - 1, 0); 9821da73821SVladimir Oltean l2_lookup.mask_vlanid = VLAN_VID_MASK; 9831da73821SVladimir Oltean l2_lookup.mask_iotag = BIT(0); 9841da73821SVladimir Oltean l2_lookup.destports = BIT(port); 9851da73821SVladimir Oltean 9861da73821SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 9871da73821SVladimir Oltean SJA1105_SEARCH, &l2_lookup); 9881da73821SVladimir Oltean if (rc < 0) 9891da73821SVladimir Oltean return 0; 9901da73821SVladimir Oltean 9911da73821SVladimir Oltean l2_lookup.destports &= ~BIT(port); 9921da73821SVladimir Oltean 9931da73821SVladimir Oltean /* Decide whether we remove just this port from the FDB entry, 9941da73821SVladimir Oltean * or if we remove it completely. 9951da73821SVladimir Oltean */ 9961da73821SVladimir Oltean if (l2_lookup.destports) 9971da73821SVladimir Oltean keep = true; 9981da73821SVladimir Oltean else 9991da73821SVladimir Oltean keep = false; 10001da73821SVladimir Oltean 10011da73821SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 10021da73821SVladimir Oltean l2_lookup.index, &l2_lookup, keep); 10039dfa6911SVladimir Oltean } 10049dfa6911SVladimir Oltean 10059dfa6911SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 10069dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 10079dfa6911SVladimir Oltean { 10089dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 10099dfa6911SVladimir Oltean 10109dfa6911SVladimir Oltean return priv->info->fdb_add_cmd(ds, port, addr, vid); 10119dfa6911SVladimir Oltean } 10129dfa6911SVladimir Oltean 10139dfa6911SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 10149dfa6911SVladimir Oltean const unsigned char *addr, u16 vid) 10159dfa6911SVladimir Oltean { 10169dfa6911SVladimir Oltean struct sja1105_private *priv = ds->priv; 10179dfa6911SVladimir Oltean 10189dfa6911SVladimir Oltean return priv->info->fdb_del_cmd(ds, port, addr, vid); 10199dfa6911SVladimir Oltean } 10209dfa6911SVladimir Oltean 1021291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 1022291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 1023291d1e72SVladimir Oltean { 1024291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 1025291d1e72SVladimir Oltean struct device *dev = ds->dev; 1026291d1e72SVladimir Oltean int i; 1027291d1e72SVladimir Oltean 1028291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 1029291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 1030291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 1031291d1e72SVladimir Oltean int rc; 1032291d1e72SVladimir Oltean 1033291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 1034291d1e72SVladimir Oltean i, &l2_lookup); 1035291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 1036def84604SVladimir Oltean if (rc == -ENOENT) 1037291d1e72SVladimir Oltean continue; 1038291d1e72SVladimir Oltean if (rc) { 1039291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 1040291d1e72SVladimir Oltean return rc; 1041291d1e72SVladimir Oltean } 1042291d1e72SVladimir Oltean 1043291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 1044291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 1045291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 1046291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 1047291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 1048291d1e72SVladimir Oltean */ 1049291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 1050291d1e72SVladimir Oltean continue; 1051291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 1052291d1e72SVladimir Oltean cb(macaddr, l2_lookup.vlanid, false, data); 1053291d1e72SVladimir Oltean } 1054291d1e72SVladimir Oltean return 0; 1055291d1e72SVladimir Oltean } 1056291d1e72SVladimir Oltean 1057291d1e72SVladimir Oltean /* This callback needs to be present */ 1058291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port, 1059291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1060291d1e72SVladimir Oltean { 1061291d1e72SVladimir Oltean return 0; 1062291d1e72SVladimir Oltean } 1063291d1e72SVladimir Oltean 1064291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port, 1065291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1066291d1e72SVladimir Oltean { 1067291d1e72SVladimir Oltean sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 1068291d1e72SVladimir Oltean } 1069291d1e72SVladimir Oltean 1070291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 1071291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 1072291d1e72SVladimir Oltean { 1073291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 1074291d1e72SVladimir Oltean } 1075291d1e72SVladimir Oltean 10768aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 10778aa9ebccSVladimir Oltean struct net_device *br, bool member) 10788aa9ebccSVladimir Oltean { 10798aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 10808aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 10818aa9ebccSVladimir Oltean int i, rc; 10828aa9ebccSVladimir Oltean 10838aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 10848aa9ebccSVladimir Oltean 10858aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 10868aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 10878aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 10888aa9ebccSVladimir Oltean */ 10898aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 10908aa9ebccSVladimir Oltean continue; 10918aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 10928aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 10938aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 10948aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 10958aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 10968aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 10978aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 10988aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 10998aa9ebccSVladimir Oltean */ 11008aa9ebccSVladimir Oltean if (i == port) 11018aa9ebccSVladimir Oltean continue; 11028aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 11038aa9ebccSVladimir Oltean continue; 11048aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 11058aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 11068aa9ebccSVladimir Oltean 11078aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 11088aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 11098aa9ebccSVladimir Oltean if (rc < 0) 11108aa9ebccSVladimir Oltean return rc; 11118aa9ebccSVladimir Oltean } 11128aa9ebccSVladimir Oltean 11138aa9ebccSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 11148aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 11158aa9ebccSVladimir Oltean } 11168aa9ebccSVladimir Oltean 1117640f763fSVladimir Oltean static void sja1105_bridge_stp_state_set(struct dsa_switch *ds, int port, 1118640f763fSVladimir Oltean u8 state) 1119640f763fSVladimir Oltean { 1120640f763fSVladimir Oltean struct sja1105_private *priv = ds->priv; 1121640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1122640f763fSVladimir Oltean 1123640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1124640f763fSVladimir Oltean 1125640f763fSVladimir Oltean switch (state) { 1126640f763fSVladimir Oltean case BR_STATE_DISABLED: 1127640f763fSVladimir Oltean case BR_STATE_BLOCKING: 1128640f763fSVladimir Oltean /* From UM10944 description of DRPDTAG (why put this there?): 1129640f763fSVladimir Oltean * "Management traffic flows to the port regardless of the state 1130640f763fSVladimir Oltean * of the INGRESS flag". So BPDUs are still be allowed to pass. 1131640f763fSVladimir Oltean * At the moment no difference between DISABLED and BLOCKING. 1132640f763fSVladimir Oltean */ 1133640f763fSVladimir Oltean mac[port].ingress = false; 1134640f763fSVladimir Oltean mac[port].egress = false; 1135640f763fSVladimir Oltean mac[port].dyn_learn = false; 1136640f763fSVladimir Oltean break; 1137640f763fSVladimir Oltean case BR_STATE_LISTENING: 1138640f763fSVladimir Oltean mac[port].ingress = true; 1139640f763fSVladimir Oltean mac[port].egress = false; 1140640f763fSVladimir Oltean mac[port].dyn_learn = false; 1141640f763fSVladimir Oltean break; 1142640f763fSVladimir Oltean case BR_STATE_LEARNING: 1143640f763fSVladimir Oltean mac[port].ingress = true; 1144640f763fSVladimir Oltean mac[port].egress = false; 1145640f763fSVladimir Oltean mac[port].dyn_learn = true; 1146640f763fSVladimir Oltean break; 1147640f763fSVladimir Oltean case BR_STATE_FORWARDING: 1148640f763fSVladimir Oltean mac[port].ingress = true; 1149640f763fSVladimir Oltean mac[port].egress = true; 1150640f763fSVladimir Oltean mac[port].dyn_learn = true; 1151640f763fSVladimir Oltean break; 1152640f763fSVladimir Oltean default: 1153640f763fSVladimir Oltean dev_err(ds->dev, "invalid STP state: %d\n", state); 1154640f763fSVladimir Oltean return; 1155640f763fSVladimir Oltean } 1156640f763fSVladimir Oltean 1157640f763fSVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 1158640f763fSVladimir Oltean &mac[port], true); 1159640f763fSVladimir Oltean } 1160640f763fSVladimir Oltean 11618aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 11628aa9ebccSVladimir Oltean struct net_device *br) 11638aa9ebccSVladimir Oltean { 11648aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 11658aa9ebccSVladimir Oltean } 11668aa9ebccSVladimir Oltean 11678aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 11688aa9ebccSVladimir Oltean struct net_device *br) 11698aa9ebccSVladimir Oltean { 11708aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 11718aa9ebccSVladimir Oltean } 11728aa9ebccSVladimir Oltean 1173640f763fSVladimir Oltean static u8 sja1105_stp_state_get(struct sja1105_private *priv, int port) 1174640f763fSVladimir Oltean { 1175640f763fSVladimir Oltean struct sja1105_mac_config_entry *mac; 1176640f763fSVladimir Oltean 1177640f763fSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 1178640f763fSVladimir Oltean 1179640f763fSVladimir Oltean if (!mac[port].ingress && !mac[port].egress && !mac[port].dyn_learn) 1180640f763fSVladimir Oltean return BR_STATE_BLOCKING; 1181640f763fSVladimir Oltean if (mac[port].ingress && !mac[port].egress && !mac[port].dyn_learn) 1182640f763fSVladimir Oltean return BR_STATE_LISTENING; 1183640f763fSVladimir Oltean if (mac[port].ingress && !mac[port].egress && mac[port].dyn_learn) 1184640f763fSVladimir Oltean return BR_STATE_LEARNING; 1185640f763fSVladimir Oltean if (mac[port].ingress && mac[port].egress && mac[port].dyn_learn) 1186640f763fSVladimir Oltean return BR_STATE_FORWARDING; 11873b2c4f4dSVladimir Oltean /* This is really an error condition if the MAC was in none of the STP 11883b2c4f4dSVladimir Oltean * states above. But treating the port as disabled does nothing, which 11893b2c4f4dSVladimir Oltean * is adequate, and it also resets the MAC to a known state later on. 11903b2c4f4dSVladimir Oltean */ 11913b2c4f4dSVladimir Oltean return BR_STATE_DISABLED; 1192640f763fSVladimir Oltean } 1193640f763fSVladimir Oltean 11946666cebcSVladimir Oltean /* For situations where we need to change a setting at runtime that is only 11956666cebcSVladimir Oltean * available through the static configuration, resetting the switch in order 11966666cebcSVladimir Oltean * to upload the new static config is unavoidable. Back up the settings we 11976666cebcSVladimir Oltean * modify at runtime (currently only MAC) and restore them after uploading, 11986666cebcSVladimir Oltean * such that this operation is relatively seamless. 11996666cebcSVladimir Oltean */ 12006666cebcSVladimir Oltean static int sja1105_static_config_reload(struct sja1105_private *priv) 12016666cebcSVladimir Oltean { 12026666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 12036666cebcSVladimir Oltean int speed_mbps[SJA1105_NUM_PORTS]; 1204640f763fSVladimir Oltean u8 stp_state[SJA1105_NUM_PORTS]; 12056666cebcSVladimir Oltean int rc, i; 12066666cebcSVladimir Oltean 12076666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 12086666cebcSVladimir Oltean 12096666cebcSVladimir Oltean /* Back up settings changed by sja1105_adjust_port_config and 1210640f763fSVladimir Oltean * sja1105_bridge_stp_state_set and restore their defaults. 12116666cebcSVladimir Oltean */ 12126666cebcSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 12136666cebcSVladimir Oltean speed_mbps[i] = sja1105_speed[mac[i].speed]; 12146666cebcSVladimir Oltean mac[i].speed = SJA1105_SPEED_AUTO; 1215640f763fSVladimir Oltean if (i == dsa_upstream_port(priv->ds, i)) { 1216640f763fSVladimir Oltean mac[i].ingress = true; 1217640f763fSVladimir Oltean mac[i].egress = true; 1218640f763fSVladimir Oltean mac[i].dyn_learn = true; 1219640f763fSVladimir Oltean } else { 1220640f763fSVladimir Oltean stp_state[i] = sja1105_stp_state_get(priv, i); 1221640f763fSVladimir Oltean mac[i].ingress = false; 1222640f763fSVladimir Oltean mac[i].egress = false; 1223640f763fSVladimir Oltean mac[i].dyn_learn = false; 1224640f763fSVladimir Oltean } 12256666cebcSVladimir Oltean } 12266666cebcSVladimir Oltean 12276666cebcSVladimir Oltean /* Reset switch and send updated static configuration */ 12286666cebcSVladimir Oltean rc = sja1105_static_config_upload(priv); 12296666cebcSVladimir Oltean if (rc < 0) 12306666cebcSVladimir Oltean goto out; 12316666cebcSVladimir Oltean 12326666cebcSVladimir Oltean /* Configure the CGU (PLLs) for MII and RMII PHYs. 12336666cebcSVladimir Oltean * For these interfaces there is no dynamic configuration 12346666cebcSVladimir Oltean * needed, since PLLs have same settings at all speeds. 12356666cebcSVladimir Oltean */ 12366666cebcSVladimir Oltean rc = sja1105_clocking_setup(priv); 12376666cebcSVladimir Oltean if (rc < 0) 12386666cebcSVladimir Oltean goto out; 12396666cebcSVladimir Oltean 12406666cebcSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 12416666cebcSVladimir Oltean bool enabled = (speed_mbps[i] != 0); 12426666cebcSVladimir Oltean 1243640f763fSVladimir Oltean if (i != dsa_upstream_port(priv->ds, i)) 1244640f763fSVladimir Oltean sja1105_bridge_stp_state_set(priv->ds, i, stp_state[i]); 1245640f763fSVladimir Oltean 12466666cebcSVladimir Oltean rc = sja1105_adjust_port_config(priv, i, speed_mbps[i], 12476666cebcSVladimir Oltean enabled); 12486666cebcSVladimir Oltean if (rc < 0) 12496666cebcSVladimir Oltean goto out; 12506666cebcSVladimir Oltean } 12516666cebcSVladimir Oltean out: 12526666cebcSVladimir Oltean return rc; 12536666cebcSVladimir Oltean } 12546666cebcSVladimir Oltean 12556666cebcSVladimir Oltean /* The TPID setting belongs to the General Parameters table, 12566666cebcSVladimir Oltean * which can only be partially reconfigured at runtime (and not the TPID). 12576666cebcSVladimir Oltean * So a switch reset is required. 12586666cebcSVladimir Oltean */ 12596666cebcSVladimir Oltean static int sja1105_change_tpid(struct sja1105_private *priv, 12606666cebcSVladimir Oltean u16 tpid, u16 tpid2) 12616666cebcSVladimir Oltean { 12626666cebcSVladimir Oltean struct sja1105_general_params_entry *general_params; 12636666cebcSVladimir Oltean struct sja1105_table *table; 12646666cebcSVladimir Oltean 12656666cebcSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 12666666cebcSVladimir Oltean general_params = table->entries; 12676666cebcSVladimir Oltean general_params->tpid = tpid; 12686666cebcSVladimir Oltean general_params->tpid2 = tpid2; 12696666cebcSVladimir Oltean return sja1105_static_config_reload(priv); 12706666cebcSVladimir Oltean } 12716666cebcSVladimir Oltean 12726666cebcSVladimir Oltean static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 12736666cebcSVladimir Oltean { 12746666cebcSVladimir Oltean struct sja1105_mac_config_entry *mac; 12756666cebcSVladimir Oltean 12766666cebcSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 12776666cebcSVladimir Oltean 12786666cebcSVladimir Oltean mac[port].vlanid = pvid; 12796666cebcSVladimir Oltean 12806666cebcSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, port, 12816666cebcSVladimir Oltean &mac[port], true); 12826666cebcSVladimir Oltean } 12836666cebcSVladimir Oltean 12846666cebcSVladimir Oltean static int sja1105_is_vlan_configured(struct sja1105_private *priv, u16 vid) 12856666cebcSVladimir Oltean { 12866666cebcSVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 12876666cebcSVladimir Oltean int count, i; 12886666cebcSVladimir Oltean 12896666cebcSVladimir Oltean vlan = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entries; 12906666cebcSVladimir Oltean count = priv->static_config.tables[BLK_IDX_VLAN_LOOKUP].entry_count; 12916666cebcSVladimir Oltean 12926666cebcSVladimir Oltean for (i = 0; i < count; i++) 12936666cebcSVladimir Oltean if (vlan[i].vlanid == vid) 12946666cebcSVladimir Oltean return i; 12956666cebcSVladimir Oltean 12966666cebcSVladimir Oltean /* Return an invalid entry index if not found */ 12976666cebcSVladimir Oltean return -1; 12986666cebcSVladimir Oltean } 12996666cebcSVladimir Oltean 13006666cebcSVladimir Oltean static int sja1105_vlan_apply(struct sja1105_private *priv, int port, u16 vid, 13016666cebcSVladimir Oltean bool enabled, bool untagged) 13026666cebcSVladimir Oltean { 13036666cebcSVladimir Oltean struct sja1105_vlan_lookup_entry *vlan; 13046666cebcSVladimir Oltean struct sja1105_table *table; 13056666cebcSVladimir Oltean bool keep = true; 13066666cebcSVladimir Oltean int match, rc; 13076666cebcSVladimir Oltean 13086666cebcSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 13096666cebcSVladimir Oltean 13106666cebcSVladimir Oltean match = sja1105_is_vlan_configured(priv, vid); 13116666cebcSVladimir Oltean if (match < 0) { 13126666cebcSVladimir Oltean /* Can't delete a missing entry. */ 13136666cebcSVladimir Oltean if (!enabled) 13146666cebcSVladimir Oltean return 0; 13156666cebcSVladimir Oltean rc = sja1105_table_resize(table, table->entry_count + 1); 13166666cebcSVladimir Oltean if (rc) 13176666cebcSVladimir Oltean return rc; 13186666cebcSVladimir Oltean match = table->entry_count - 1; 13196666cebcSVladimir Oltean } 13206666cebcSVladimir Oltean /* Assign pointer after the resize (it's new memory) */ 13216666cebcSVladimir Oltean vlan = table->entries; 13226666cebcSVladimir Oltean vlan[match].vlanid = vid; 13236666cebcSVladimir Oltean if (enabled) { 13246666cebcSVladimir Oltean vlan[match].vlan_bc |= BIT(port); 13256666cebcSVladimir Oltean vlan[match].vmemb_port |= BIT(port); 13266666cebcSVladimir Oltean } else { 13276666cebcSVladimir Oltean vlan[match].vlan_bc &= ~BIT(port); 13286666cebcSVladimir Oltean vlan[match].vmemb_port &= ~BIT(port); 13296666cebcSVladimir Oltean } 13306666cebcSVladimir Oltean /* Also unset tag_port if removing this VLAN was requested, 13316666cebcSVladimir Oltean * just so we don't have a confusing bitmap (no practical purpose). 13326666cebcSVladimir Oltean */ 13336666cebcSVladimir Oltean if (untagged || !enabled) 13346666cebcSVladimir Oltean vlan[match].tag_port &= ~BIT(port); 13356666cebcSVladimir Oltean else 13366666cebcSVladimir Oltean vlan[match].tag_port |= BIT(port); 13376666cebcSVladimir Oltean /* If there's no port left as member of this VLAN, 13386666cebcSVladimir Oltean * it's time for it to go. 13396666cebcSVladimir Oltean */ 13406666cebcSVladimir Oltean if (!vlan[match].vmemb_port) 13416666cebcSVladimir Oltean keep = false; 13426666cebcSVladimir Oltean 13436666cebcSVladimir Oltean dev_dbg(priv->ds->dev, 13446666cebcSVladimir Oltean "%s: port %d, vid %llu, broadcast domain 0x%llx, " 13456666cebcSVladimir Oltean "port members 0x%llx, tagged ports 0x%llx, keep %d\n", 13466666cebcSVladimir Oltean __func__, port, vlan[match].vlanid, vlan[match].vlan_bc, 13476666cebcSVladimir Oltean vlan[match].vmemb_port, vlan[match].tag_port, keep); 13486666cebcSVladimir Oltean 13496666cebcSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_VLAN_LOOKUP, vid, 13506666cebcSVladimir Oltean &vlan[match], keep); 13516666cebcSVladimir Oltean if (rc < 0) 13526666cebcSVladimir Oltean return rc; 13536666cebcSVladimir Oltean 13546666cebcSVladimir Oltean if (!keep) 13556666cebcSVladimir Oltean return sja1105_table_delete_entry(table, match); 13566666cebcSVladimir Oltean 13576666cebcSVladimir Oltean return 0; 13586666cebcSVladimir Oltean } 13596666cebcSVladimir Oltean 1360227d07a0SVladimir Oltean static int sja1105_setup_8021q_tagging(struct dsa_switch *ds, bool enabled) 1361227d07a0SVladimir Oltean { 1362227d07a0SVladimir Oltean int rc, i; 1363227d07a0SVladimir Oltean 1364227d07a0SVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 1365227d07a0SVladimir Oltean rc = dsa_port_setup_8021q_tagging(ds, i, enabled); 1366227d07a0SVladimir Oltean if (rc < 0) { 1367227d07a0SVladimir Oltean dev_err(ds->dev, "Failed to setup VLAN tagging for port %d: %d\n", 1368227d07a0SVladimir Oltean i, rc); 1369227d07a0SVladimir Oltean return rc; 1370227d07a0SVladimir Oltean } 1371227d07a0SVladimir Oltean } 1372227d07a0SVladimir Oltean dev_info(ds->dev, "%s switch tagging\n", 1373227d07a0SVladimir Oltean enabled ? "Enabled" : "Disabled"); 1374227d07a0SVladimir Oltean return 0; 1375227d07a0SVladimir Oltean } 1376227d07a0SVladimir Oltean 13778aa9ebccSVladimir Oltean static enum dsa_tag_protocol 13788aa9ebccSVladimir Oltean sja1105_get_tag_protocol(struct dsa_switch *ds, int port) 13798aa9ebccSVladimir Oltean { 1380227d07a0SVladimir Oltean return DSA_TAG_PROTO_SJA1105; 13818aa9ebccSVladimir Oltean } 13828aa9ebccSVladimir Oltean 13836666cebcSVladimir Oltean /* This callback needs to be present */ 13846666cebcSVladimir Oltean static int sja1105_vlan_prepare(struct dsa_switch *ds, int port, 13856666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 13866666cebcSVladimir Oltean { 13876666cebcSVladimir Oltean return 0; 13886666cebcSVladimir Oltean } 13896666cebcSVladimir Oltean 13906666cebcSVladimir Oltean static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) 13916666cebcSVladimir Oltean { 13926666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 13936666cebcSVladimir Oltean int rc; 13946666cebcSVladimir Oltean 13956666cebcSVladimir Oltean if (enabled) 13966666cebcSVladimir Oltean /* Enable VLAN filtering. */ 13976666cebcSVladimir Oltean rc = sja1105_change_tpid(priv, ETH_P_8021Q, ETH_P_8021AD); 13986666cebcSVladimir Oltean else 13996666cebcSVladimir Oltean /* Disable VLAN filtering. */ 14006666cebcSVladimir Oltean rc = sja1105_change_tpid(priv, ETH_P_SJA1105, ETH_P_SJA1105); 14016666cebcSVladimir Oltean if (rc) 14026666cebcSVladimir Oltean dev_err(ds->dev, "Failed to change VLAN Ethertype\n"); 14036666cebcSVladimir Oltean 1404227d07a0SVladimir Oltean /* Switch port identification based on 802.1Q is only passable 1405227d07a0SVladimir Oltean * if we are not under a vlan_filtering bridge. So make sure 1406227d07a0SVladimir Oltean * the two configurations are mutually exclusive. 1407227d07a0SVladimir Oltean */ 1408227d07a0SVladimir Oltean return sja1105_setup_8021q_tagging(ds, !enabled); 14096666cebcSVladimir Oltean } 14106666cebcSVladimir Oltean 14116666cebcSVladimir Oltean static void sja1105_vlan_add(struct dsa_switch *ds, int port, 14126666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 14136666cebcSVladimir Oltean { 14146666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 14156666cebcSVladimir Oltean u16 vid; 14166666cebcSVladimir Oltean int rc; 14176666cebcSVladimir Oltean 14186666cebcSVladimir Oltean for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 14196666cebcSVladimir Oltean rc = sja1105_vlan_apply(priv, port, vid, true, vlan->flags & 14206666cebcSVladimir Oltean BRIDGE_VLAN_INFO_UNTAGGED); 14216666cebcSVladimir Oltean if (rc < 0) { 14226666cebcSVladimir Oltean dev_err(ds->dev, "Failed to add VLAN %d to port %d: %d\n", 14236666cebcSVladimir Oltean vid, port, rc); 14246666cebcSVladimir Oltean return; 14256666cebcSVladimir Oltean } 14266666cebcSVladimir Oltean if (vlan->flags & BRIDGE_VLAN_INFO_PVID) { 14276666cebcSVladimir Oltean rc = sja1105_pvid_apply(ds->priv, port, vid); 14286666cebcSVladimir Oltean if (rc < 0) { 14296666cebcSVladimir Oltean dev_err(ds->dev, "Failed to set pvid %d on port %d: %d\n", 14306666cebcSVladimir Oltean vid, port, rc); 14316666cebcSVladimir Oltean return; 14326666cebcSVladimir Oltean } 14336666cebcSVladimir Oltean } 14346666cebcSVladimir Oltean } 14356666cebcSVladimir Oltean } 14366666cebcSVladimir Oltean 14376666cebcSVladimir Oltean static int sja1105_vlan_del(struct dsa_switch *ds, int port, 14386666cebcSVladimir Oltean const struct switchdev_obj_port_vlan *vlan) 14396666cebcSVladimir Oltean { 14406666cebcSVladimir Oltean struct sja1105_private *priv = ds->priv; 14416666cebcSVladimir Oltean u16 vid; 14426666cebcSVladimir Oltean int rc; 14436666cebcSVladimir Oltean 14446666cebcSVladimir Oltean for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 14456666cebcSVladimir Oltean rc = sja1105_vlan_apply(priv, port, vid, false, vlan->flags & 14466666cebcSVladimir Oltean BRIDGE_VLAN_INFO_UNTAGGED); 14476666cebcSVladimir Oltean if (rc < 0) { 14486666cebcSVladimir Oltean dev_err(ds->dev, "Failed to remove VLAN %d from port %d: %d\n", 14496666cebcSVladimir Oltean vid, port, rc); 14506666cebcSVladimir Oltean return rc; 14516666cebcSVladimir Oltean } 14526666cebcSVladimir Oltean } 14536666cebcSVladimir Oltean return 0; 14546666cebcSVladimir Oltean } 14556666cebcSVladimir Oltean 14568aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 14578aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 14588aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 14598aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 14608aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 14618aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 14628aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 14638aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 14648aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 14658aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 14668aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 14678aa9ebccSVladimir Oltean */ 14688aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 14698aa9ebccSVladimir Oltean { 14708aa9ebccSVladimir Oltean struct sja1105_dt_port ports[SJA1105_NUM_PORTS]; 14718aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 14728aa9ebccSVladimir Oltean int rc; 14738aa9ebccSVladimir Oltean 14748aa9ebccSVladimir Oltean rc = sja1105_parse_dt(priv, ports); 14758aa9ebccSVladimir Oltean if (rc < 0) { 14768aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 14778aa9ebccSVladimir Oltean return rc; 14788aa9ebccSVladimir Oltean } 1479f5b8631cSVladimir Oltean 1480f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 1481f5b8631cSVladimir Oltean * and we can't apply them. 1482f5b8631cSVladimir Oltean */ 1483f5b8631cSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv, ports); 1484f5b8631cSVladimir Oltean if (rc < 0) { 1485f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 1486f5b8631cSVladimir Oltean return rc; 1487f5b8631cSVladimir Oltean } 1488f5b8631cSVladimir Oltean 14898aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 14908aa9ebccSVladimir Oltean rc = sja1105_static_config_load(priv, ports); 14918aa9ebccSVladimir Oltean if (rc < 0) { 14928aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 14938aa9ebccSVladimir Oltean return rc; 14948aa9ebccSVladimir Oltean } 14958aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 14968aa9ebccSVladimir Oltean rc = sja1105_clocking_setup(priv); 14978aa9ebccSVladimir Oltean if (rc < 0) { 14988aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); 14998aa9ebccSVladimir Oltean return rc; 15008aa9ebccSVladimir Oltean } 15016666cebcSVladimir Oltean /* On SJA1105, VLAN filtering per se is always enabled in hardware. 15026666cebcSVladimir Oltean * The only thing we can do to disable it is lie about what the 802.1Q 15036666cebcSVladimir Oltean * EtherType is. 15046666cebcSVladimir Oltean * So it will still try to apply VLAN filtering, but all ingress 15056666cebcSVladimir Oltean * traffic (except frames received with EtherType of ETH_P_SJA1105) 15066666cebcSVladimir Oltean * will be internally tagged with a distorted VLAN header where the 15076666cebcSVladimir Oltean * TPID is ETH_P_SJA1105, and the VLAN ID is the port pvid. 15086666cebcSVladimir Oltean */ 15096666cebcSVladimir Oltean ds->vlan_filtering_is_global = true; 15108aa9ebccSVladimir Oltean 1511227d07a0SVladimir Oltean /* The DSA/switchdev model brings up switch ports in standalone mode by 1512227d07a0SVladimir Oltean * default, and that means vlan_filtering is 0 since they're not under 1513227d07a0SVladimir Oltean * a bridge, so it's safe to set up switch tagging at this time. 1514227d07a0SVladimir Oltean */ 1515227d07a0SVladimir Oltean return sja1105_setup_8021q_tagging(ds, true); 1516227d07a0SVladimir Oltean } 1517227d07a0SVladimir Oltean 1518227d07a0SVladimir Oltean static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 1519227d07a0SVladimir Oltean struct sk_buff *skb) 1520227d07a0SVladimir Oltean { 1521227d07a0SVladimir Oltean struct sja1105_mgmt_entry mgmt_route = {0}; 1522227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 1523227d07a0SVladimir Oltean struct ethhdr *hdr; 1524227d07a0SVladimir Oltean int timeout = 10; 1525227d07a0SVladimir Oltean int rc; 1526227d07a0SVladimir Oltean 1527227d07a0SVladimir Oltean hdr = eth_hdr(skb); 1528227d07a0SVladimir Oltean 1529227d07a0SVladimir Oltean mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 1530227d07a0SVladimir Oltean mgmt_route.destports = BIT(port); 1531227d07a0SVladimir Oltean mgmt_route.enfport = 1; 1532227d07a0SVladimir Oltean 1533227d07a0SVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 1534227d07a0SVladimir Oltean slot, &mgmt_route, true); 1535227d07a0SVladimir Oltean if (rc < 0) { 1536227d07a0SVladimir Oltean kfree_skb(skb); 1537227d07a0SVladimir Oltean return rc; 1538227d07a0SVladimir Oltean } 1539227d07a0SVladimir Oltean 1540227d07a0SVladimir Oltean /* Transfer skb to the host port. */ 1541227d07a0SVladimir Oltean dsa_enqueue_skb(skb, ds->ports[port].slave); 1542227d07a0SVladimir Oltean 1543227d07a0SVladimir Oltean /* Wait until the switch has processed the frame */ 1544227d07a0SVladimir Oltean do { 1545227d07a0SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_MGMT_ROUTE, 1546227d07a0SVladimir Oltean slot, &mgmt_route); 1547227d07a0SVladimir Oltean if (rc < 0) { 1548227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, 1549227d07a0SVladimir Oltean "failed to poll for mgmt route\n"); 1550227d07a0SVladimir Oltean continue; 1551227d07a0SVladimir Oltean } 1552227d07a0SVladimir Oltean 1553227d07a0SVladimir Oltean /* UM10944: The ENFPORT flag of the respective entry is 1554227d07a0SVladimir Oltean * cleared when a match is found. The host can use this 1555227d07a0SVladimir Oltean * flag as an acknowledgment. 1556227d07a0SVladimir Oltean */ 1557227d07a0SVladimir Oltean cpu_relax(); 1558227d07a0SVladimir Oltean } while (mgmt_route.enfport && --timeout); 1559227d07a0SVladimir Oltean 1560227d07a0SVladimir Oltean if (!timeout) { 1561227d07a0SVladimir Oltean /* Clean up the management route so that a follow-up 1562227d07a0SVladimir Oltean * frame may not match on it by mistake. 15632a7e7409SVladimir Oltean * This is only hardware supported on P/Q/R/S - on E/T it is 15642a7e7409SVladimir Oltean * a no-op and we are silently discarding the -EOPNOTSUPP. 1565227d07a0SVladimir Oltean */ 1566227d07a0SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 1567227d07a0SVladimir Oltean slot, &mgmt_route, false); 1568227d07a0SVladimir Oltean dev_err_ratelimited(priv->ds->dev, "xmit timed out\n"); 1569227d07a0SVladimir Oltean } 1570227d07a0SVladimir Oltean 1571227d07a0SVladimir Oltean return NETDEV_TX_OK; 1572227d07a0SVladimir Oltean } 1573227d07a0SVladimir Oltean 1574227d07a0SVladimir Oltean /* Deferred work is unfortunately necessary because setting up the management 1575227d07a0SVladimir Oltean * route cannot be done from atomit context (SPI transfer takes a sleepable 1576227d07a0SVladimir Oltean * lock on the bus) 1577227d07a0SVladimir Oltean */ 1578227d07a0SVladimir Oltean static netdev_tx_t sja1105_port_deferred_xmit(struct dsa_switch *ds, int port, 1579227d07a0SVladimir Oltean struct sk_buff *skb) 1580227d07a0SVladimir Oltean { 1581227d07a0SVladimir Oltean struct sja1105_private *priv = ds->priv; 1582227d07a0SVladimir Oltean struct sja1105_port *sp = &priv->ports[port]; 1583227d07a0SVladimir Oltean int slot = sp->mgmt_slot; 1584227d07a0SVladimir Oltean 1585227d07a0SVladimir Oltean /* The tragic fact about the switch having 4x2 slots for installing 1586227d07a0SVladimir Oltean * management routes is that all of them except one are actually 1587227d07a0SVladimir Oltean * useless. 1588227d07a0SVladimir Oltean * If 2 slots are simultaneously configured for two BPDUs sent to the 1589227d07a0SVladimir Oltean * same (multicast) DMAC but on different egress ports, the switch 1590227d07a0SVladimir Oltean * would confuse them and redirect first frame it receives on the CPU 1591227d07a0SVladimir Oltean * port towards the port configured on the numerically first slot 1592227d07a0SVladimir Oltean * (therefore wrong port), then second received frame on second slot 1593227d07a0SVladimir Oltean * (also wrong port). 1594227d07a0SVladimir Oltean * So for all practical purposes, there needs to be a lock that 1595227d07a0SVladimir Oltean * prevents that from happening. The slot used here is utterly useless 1596227d07a0SVladimir Oltean * (could have simply been 0 just as fine), but we are doing it 1597227d07a0SVladimir Oltean * nonetheless, in case a smarter idea ever comes up in the future. 1598227d07a0SVladimir Oltean */ 1599227d07a0SVladimir Oltean mutex_lock(&priv->mgmt_lock); 1600227d07a0SVladimir Oltean 1601227d07a0SVladimir Oltean sja1105_mgmt_xmit(ds, port, slot, skb); 1602227d07a0SVladimir Oltean 1603227d07a0SVladimir Oltean mutex_unlock(&priv->mgmt_lock); 1604227d07a0SVladimir Oltean return NETDEV_TX_OK; 16058aa9ebccSVladimir Oltean } 16068aa9ebccSVladimir Oltean 16078456721dSVladimir Oltean /* The MAXAGE setting belongs to the L2 Forwarding Parameters table, 16088456721dSVladimir Oltean * which cannot be reconfigured at runtime. So a switch reset is required. 16098456721dSVladimir Oltean */ 16108456721dSVladimir Oltean static int sja1105_set_ageing_time(struct dsa_switch *ds, 16118456721dSVladimir Oltean unsigned int ageing_time) 16128456721dSVladimir Oltean { 16138456721dSVladimir Oltean struct sja1105_l2_lookup_params_entry *l2_lookup_params; 16148456721dSVladimir Oltean struct sja1105_private *priv = ds->priv; 16158456721dSVladimir Oltean struct sja1105_table *table; 16168456721dSVladimir Oltean unsigned int maxage; 16178456721dSVladimir Oltean 16188456721dSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 16198456721dSVladimir Oltean l2_lookup_params = table->entries; 16208456721dSVladimir Oltean 16218456721dSVladimir Oltean maxage = SJA1105_AGEING_TIME_MS(ageing_time); 16228456721dSVladimir Oltean 16238456721dSVladimir Oltean if (l2_lookup_params->maxage == maxage) 16248456721dSVladimir Oltean return 0; 16258456721dSVladimir Oltean 16268456721dSVladimir Oltean l2_lookup_params->maxage = maxage; 16278456721dSVladimir Oltean 16288456721dSVladimir Oltean return sja1105_static_config_reload(priv); 16298456721dSVladimir Oltean } 16308456721dSVladimir Oltean 16318aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 16328aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 16338aa9ebccSVladimir Oltean .setup = sja1105_setup, 16348456721dSVladimir Oltean .set_ageing_time = sja1105_set_ageing_time, 1635ad9f299aSVladimir Oltean .phylink_validate = sja1105_phylink_validate, 1636af7cd036SVladimir Oltean .phylink_mac_config = sja1105_mac_config, 163752c34e6eSVladimir Oltean .get_strings = sja1105_get_strings, 163852c34e6eSVladimir Oltean .get_ethtool_stats = sja1105_get_ethtool_stats, 163952c34e6eSVladimir Oltean .get_sset_count = sja1105_get_sset_count, 1640291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 1641291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 1642291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 16438aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 16448aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 1645640f763fSVladimir Oltean .port_stp_state_set = sja1105_bridge_stp_state_set, 16466666cebcSVladimir Oltean .port_vlan_prepare = sja1105_vlan_prepare, 16476666cebcSVladimir Oltean .port_vlan_filtering = sja1105_vlan_filtering, 16486666cebcSVladimir Oltean .port_vlan_add = sja1105_vlan_add, 16496666cebcSVladimir Oltean .port_vlan_del = sja1105_vlan_del, 1650291d1e72SVladimir Oltean .port_mdb_prepare = sja1105_mdb_prepare, 1651291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 1652291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 1653227d07a0SVladimir Oltean .port_deferred_xmit = sja1105_port_deferred_xmit, 16548aa9ebccSVladimir Oltean }; 16558aa9ebccSVladimir Oltean 16568aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 16578aa9ebccSVladimir Oltean { 16588aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 16598aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 16608aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 16618aa9ebccSVladimir Oltean u64 device_id; 16628aa9ebccSVladimir Oltean u64 part_no; 16638aa9ebccSVladimir Oltean int rc; 16648aa9ebccSVladimir Oltean 16658aa9ebccSVladimir Oltean rc = sja1105_spi_send_int(priv, SPI_READ, regs->device_id, 16668aa9ebccSVladimir Oltean &device_id, SJA1105_SIZE_DEVICE_ID); 16678aa9ebccSVladimir Oltean if (rc < 0) 16688aa9ebccSVladimir Oltean return rc; 16698aa9ebccSVladimir Oltean 16708aa9ebccSVladimir Oltean if (device_id != priv->info->device_id) { 16718aa9ebccSVladimir Oltean dev_err(dev, "Expected device ID 0x%llx but read 0x%llx\n", 16728aa9ebccSVladimir Oltean priv->info->device_id, device_id); 16738aa9ebccSVladimir Oltean return -ENODEV; 16748aa9ebccSVladimir Oltean } 16758aa9ebccSVladimir Oltean 16768aa9ebccSVladimir Oltean rc = sja1105_spi_send_packed_buf(priv, SPI_READ, regs->prod_id, 16778aa9ebccSVladimir Oltean prod_id, SJA1105_SIZE_DEVICE_ID); 16788aa9ebccSVladimir Oltean if (rc < 0) 16798aa9ebccSVladimir Oltean return rc; 16808aa9ebccSVladimir Oltean 16818aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 16828aa9ebccSVladimir Oltean 16838aa9ebccSVladimir Oltean if (part_no != priv->info->part_no) { 16848aa9ebccSVladimir Oltean dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n", 16858aa9ebccSVladimir Oltean priv->info->part_no, part_no); 16868aa9ebccSVladimir Oltean return -ENODEV; 16878aa9ebccSVladimir Oltean } 16888aa9ebccSVladimir Oltean 16898aa9ebccSVladimir Oltean return 0; 16908aa9ebccSVladimir Oltean } 16918aa9ebccSVladimir Oltean 16928aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 16938aa9ebccSVladimir Oltean { 16948aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 16958aa9ebccSVladimir Oltean struct sja1105_private *priv; 16968aa9ebccSVladimir Oltean struct dsa_switch *ds; 1697227d07a0SVladimir Oltean int rc, i; 16988aa9ebccSVladimir Oltean 16998aa9ebccSVladimir Oltean if (!dev->of_node) { 17008aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 17018aa9ebccSVladimir Oltean return -EINVAL; 17028aa9ebccSVladimir Oltean } 17038aa9ebccSVladimir Oltean 17048aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 17058aa9ebccSVladimir Oltean if (!priv) 17068aa9ebccSVladimir Oltean return -ENOMEM; 17078aa9ebccSVladimir Oltean 17088aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 17098aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 17108aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 17118aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 17128aa9ebccSVladimir Oltean else 17138aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 17148aa9ebccSVladimir Oltean 17158aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 17168aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 17178aa9ebccSVladimir Oltean */ 17188aa9ebccSVladimir Oltean priv->spidev = spi; 17198aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 17208aa9ebccSVladimir Oltean 17218aa9ebccSVladimir Oltean /* Configure the SPI bus */ 17228aa9ebccSVladimir Oltean spi->bits_per_word = 8; 17238aa9ebccSVladimir Oltean rc = spi_setup(spi); 17248aa9ebccSVladimir Oltean if (rc < 0) { 17258aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 17268aa9ebccSVladimir Oltean return rc; 17278aa9ebccSVladimir Oltean } 17288aa9ebccSVladimir Oltean 17298aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 17308aa9ebccSVladimir Oltean 17318aa9ebccSVladimir Oltean /* Detect hardware device */ 17328aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 17338aa9ebccSVladimir Oltean if (rc < 0) { 17348aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 17358aa9ebccSVladimir Oltean return rc; 17368aa9ebccSVladimir Oltean } 17378aa9ebccSVladimir Oltean 17388aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 17398aa9ebccSVladimir Oltean 17408aa9ebccSVladimir Oltean ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS); 17418aa9ebccSVladimir Oltean if (!ds) 17428aa9ebccSVladimir Oltean return -ENOMEM; 17438aa9ebccSVladimir Oltean 17448aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 17458aa9ebccSVladimir Oltean ds->priv = priv; 17468aa9ebccSVladimir Oltean priv->ds = ds; 17478aa9ebccSVladimir Oltean 1748227d07a0SVladimir Oltean /* Connections between dsa_port and sja1105_port */ 1749227d07a0SVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 1750227d07a0SVladimir Oltean struct sja1105_port *sp = &priv->ports[i]; 1751227d07a0SVladimir Oltean 1752227d07a0SVladimir Oltean ds->ports[i].priv = sp; 1753227d07a0SVladimir Oltean sp->dp = &ds->ports[i]; 1754227d07a0SVladimir Oltean } 1755227d07a0SVladimir Oltean mutex_init(&priv->mgmt_lock); 1756227d07a0SVladimir Oltean 17578aa9ebccSVladimir Oltean return dsa_register_switch(priv->ds); 17588aa9ebccSVladimir Oltean } 17598aa9ebccSVladimir Oltean 17608aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 17618aa9ebccSVladimir Oltean { 17628aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 17638aa9ebccSVladimir Oltean 17648aa9ebccSVladimir Oltean dsa_unregister_switch(priv->ds); 17658aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 17668aa9ebccSVladimir Oltean return 0; 17678aa9ebccSVladimir Oltean } 17688aa9ebccSVladimir Oltean 17698aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 17708aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 17718aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 17728aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 17738aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 17748aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 17758aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 17768aa9ebccSVladimir Oltean { /* sentinel */ }, 17778aa9ebccSVladimir Oltean }; 17788aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 17798aa9ebccSVladimir Oltean 17808aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 17818aa9ebccSVladimir Oltean .driver = { 17828aa9ebccSVladimir Oltean .name = "sja1105", 17838aa9ebccSVladimir Oltean .owner = THIS_MODULE, 17848aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 17858aa9ebccSVladimir Oltean }, 17868aa9ebccSVladimir Oltean .probe = sja1105_probe, 17878aa9ebccSVladimir Oltean .remove = sja1105_remove, 17888aa9ebccSVladimir Oltean }; 17898aa9ebccSVladimir Oltean 17908aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 17918aa9ebccSVladimir Oltean 17928aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 17938aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 17948aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 17958aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 1796