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> 148aa9ebccSVladimir Oltean #include <linux/of.h> 158aa9ebccSVladimir Oltean #include <linux/of_net.h> 168aa9ebccSVladimir Oltean #include <linux/of_mdio.h> 178aa9ebccSVladimir Oltean #include <linux/of_device.h> 188aa9ebccSVladimir Oltean #include <linux/netdev_features.h> 198aa9ebccSVladimir Oltean #include <linux/netdevice.h> 208aa9ebccSVladimir Oltean #include <linux/if_bridge.h> 218aa9ebccSVladimir Oltean #include <linux/if_ether.h> 228aa9ebccSVladimir Oltean #include "sja1105.h" 238aa9ebccSVladimir Oltean 248aa9ebccSVladimir Oltean static void sja1105_hw_reset(struct gpio_desc *gpio, unsigned int pulse_len, 258aa9ebccSVladimir Oltean unsigned int startup_delay) 268aa9ebccSVladimir Oltean { 278aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 1); 288aa9ebccSVladimir Oltean /* Wait for minimum reset pulse length */ 298aa9ebccSVladimir Oltean msleep(pulse_len); 308aa9ebccSVladimir Oltean gpiod_set_value_cansleep(gpio, 0); 318aa9ebccSVladimir Oltean /* Wait until chip is ready after reset */ 328aa9ebccSVladimir Oltean msleep(startup_delay); 338aa9ebccSVladimir Oltean } 348aa9ebccSVladimir Oltean 358aa9ebccSVladimir Oltean static void 368aa9ebccSVladimir Oltean sja1105_port_allow_traffic(struct sja1105_l2_forwarding_entry *l2_fwd, 378aa9ebccSVladimir Oltean int from, int to, bool allow) 388aa9ebccSVladimir Oltean { 398aa9ebccSVladimir Oltean if (allow) { 408aa9ebccSVladimir Oltean l2_fwd[from].bc_domain |= BIT(to); 418aa9ebccSVladimir Oltean l2_fwd[from].reach_port |= BIT(to); 428aa9ebccSVladimir Oltean l2_fwd[from].fl_domain |= BIT(to); 438aa9ebccSVladimir Oltean } else { 448aa9ebccSVladimir Oltean l2_fwd[from].bc_domain &= ~BIT(to); 458aa9ebccSVladimir Oltean l2_fwd[from].reach_port &= ~BIT(to); 468aa9ebccSVladimir Oltean l2_fwd[from].fl_domain &= ~BIT(to); 478aa9ebccSVladimir Oltean } 488aa9ebccSVladimir Oltean } 498aa9ebccSVladimir Oltean 508aa9ebccSVladimir Oltean /* Structure used to temporarily transport device tree 518aa9ebccSVladimir Oltean * settings into sja1105_setup 528aa9ebccSVladimir Oltean */ 538aa9ebccSVladimir Oltean struct sja1105_dt_port { 548aa9ebccSVladimir Oltean phy_interface_t phy_mode; 558aa9ebccSVladimir Oltean sja1105_mii_role_t role; 568aa9ebccSVladimir Oltean }; 578aa9ebccSVladimir Oltean 588aa9ebccSVladimir Oltean static int sja1105_init_mac_settings(struct sja1105_private *priv) 598aa9ebccSVladimir Oltean { 608aa9ebccSVladimir Oltean struct sja1105_mac_config_entry default_mac = { 618aa9ebccSVladimir Oltean /* Enable all 8 priority queues on egress. 628aa9ebccSVladimir Oltean * Every queue i holds top[i] - base[i] frames. 638aa9ebccSVladimir Oltean * Sum of top[i] - base[i] is 511 (max hardware limit). 648aa9ebccSVladimir Oltean */ 658aa9ebccSVladimir Oltean .top = {0x3F, 0x7F, 0xBF, 0xFF, 0x13F, 0x17F, 0x1BF, 0x1FF}, 668aa9ebccSVladimir Oltean .base = {0x0, 0x40, 0x80, 0xC0, 0x100, 0x140, 0x180, 0x1C0}, 678aa9ebccSVladimir Oltean .enabled = {true, true, true, true, true, true, true, true}, 688aa9ebccSVladimir Oltean /* Keep standard IFG of 12 bytes on egress. */ 698aa9ebccSVladimir Oltean .ifg = 0, 708aa9ebccSVladimir Oltean /* Always put the MAC speed in automatic mode, where it can be 718aa9ebccSVladimir Oltean * retrieved from the PHY object through phylib and 728aa9ebccSVladimir Oltean * sja1105_adjust_port_config. 738aa9ebccSVladimir Oltean */ 748aa9ebccSVladimir Oltean .speed = SJA1105_SPEED_AUTO, 758aa9ebccSVladimir Oltean /* No static correction for 1-step 1588 events */ 768aa9ebccSVladimir Oltean .tp_delin = 0, 778aa9ebccSVladimir Oltean .tp_delout = 0, 788aa9ebccSVladimir Oltean /* Disable aging for critical TTEthernet traffic */ 798aa9ebccSVladimir Oltean .maxage = 0xFF, 808aa9ebccSVladimir Oltean /* Internal VLAN (pvid) to apply to untagged ingress */ 818aa9ebccSVladimir Oltean .vlanprio = 0, 828aa9ebccSVladimir Oltean .vlanid = 0, 838aa9ebccSVladimir Oltean .ing_mirr = false, 848aa9ebccSVladimir Oltean .egr_mirr = false, 858aa9ebccSVladimir Oltean /* Don't drop traffic with other EtherType than ETH_P_IP */ 868aa9ebccSVladimir Oltean .drpnona664 = false, 878aa9ebccSVladimir Oltean /* Don't drop double-tagged traffic */ 888aa9ebccSVladimir Oltean .drpdtag = false, 898aa9ebccSVladimir Oltean /* Don't drop untagged traffic */ 908aa9ebccSVladimir Oltean .drpuntag = false, 918aa9ebccSVladimir Oltean /* Don't retag 802.1p (VID 0) traffic with the pvid */ 928aa9ebccSVladimir Oltean .retag = false, 938aa9ebccSVladimir Oltean /* Enable learning and I/O on user ports by default. */ 948aa9ebccSVladimir Oltean .dyn_learn = true, 958aa9ebccSVladimir Oltean .egress = false, 968aa9ebccSVladimir Oltean .ingress = false, 978aa9ebccSVladimir Oltean }; 988aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 998aa9ebccSVladimir Oltean struct sja1105_table *table; 1008aa9ebccSVladimir Oltean int i; 1018aa9ebccSVladimir Oltean 1028aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_MAC_CONFIG]; 1038aa9ebccSVladimir Oltean 1048aa9ebccSVladimir Oltean /* Discard previous MAC Configuration Table */ 1058aa9ebccSVladimir Oltean if (table->entry_count) { 1068aa9ebccSVladimir Oltean kfree(table->entries); 1078aa9ebccSVladimir Oltean table->entry_count = 0; 1088aa9ebccSVladimir Oltean } 1098aa9ebccSVladimir Oltean 1108aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_NUM_PORTS, 1118aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1128aa9ebccSVladimir Oltean if (!table->entries) 1138aa9ebccSVladimir Oltean return -ENOMEM; 1148aa9ebccSVladimir Oltean 1158aa9ebccSVladimir Oltean /* Override table based on phylib DT bindings */ 1168aa9ebccSVladimir Oltean table->entry_count = SJA1105_NUM_PORTS; 1178aa9ebccSVladimir Oltean 1188aa9ebccSVladimir Oltean mac = table->entries; 1198aa9ebccSVladimir Oltean 1208aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) 1218aa9ebccSVladimir Oltean mac[i] = default_mac; 1228aa9ebccSVladimir Oltean 1238aa9ebccSVladimir Oltean return 0; 1248aa9ebccSVladimir Oltean } 1258aa9ebccSVladimir Oltean 1268aa9ebccSVladimir Oltean static int sja1105_init_mii_settings(struct sja1105_private *priv, 1278aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 1288aa9ebccSVladimir Oltean { 1298aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 1308aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 1318aa9ebccSVladimir Oltean struct sja1105_table *table; 1328aa9ebccSVladimir Oltean int i; 1338aa9ebccSVladimir Oltean 1348aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_XMII_PARAMS]; 1358aa9ebccSVladimir Oltean 1368aa9ebccSVladimir Oltean /* Discard previous xMII Mode Parameters Table */ 1378aa9ebccSVladimir Oltean if (table->entry_count) { 1388aa9ebccSVladimir Oltean kfree(table->entries); 1398aa9ebccSVladimir Oltean table->entry_count = 0; 1408aa9ebccSVladimir Oltean } 1418aa9ebccSVladimir Oltean 1428aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_XMII_PARAMS_COUNT, 1438aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 1448aa9ebccSVladimir Oltean if (!table->entries) 1458aa9ebccSVladimir Oltean return -ENOMEM; 1468aa9ebccSVladimir Oltean 1478aa9ebccSVladimir Oltean /* Override table based on phylib DT bindings */ 1488aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_XMII_PARAMS_COUNT; 1498aa9ebccSVladimir Oltean 1508aa9ebccSVladimir Oltean mii = table->entries; 1518aa9ebccSVladimir Oltean 1528aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 1538aa9ebccSVladimir Oltean switch (ports[i].phy_mode) { 1548aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_MII: 1558aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_MII; 1568aa9ebccSVladimir Oltean break; 1578aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RMII: 1588aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RMII; 1598aa9ebccSVladimir Oltean break; 1608aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII: 1618aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_ID: 1628aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_RXID: 1638aa9ebccSVladimir Oltean case PHY_INTERFACE_MODE_RGMII_TXID: 1648aa9ebccSVladimir Oltean mii->xmii_mode[i] = XMII_MODE_RGMII; 1658aa9ebccSVladimir Oltean break; 1668aa9ebccSVladimir Oltean default: 1678aa9ebccSVladimir Oltean dev_err(dev, "Unsupported PHY mode %s!\n", 1688aa9ebccSVladimir Oltean phy_modes(ports[i].phy_mode)); 1698aa9ebccSVladimir Oltean } 1708aa9ebccSVladimir Oltean 1718aa9ebccSVladimir Oltean mii->phy_mac[i] = ports[i].role; 1728aa9ebccSVladimir Oltean } 1738aa9ebccSVladimir Oltean return 0; 1748aa9ebccSVladimir Oltean } 1758aa9ebccSVladimir Oltean 1768aa9ebccSVladimir Oltean static int sja1105_init_static_fdb(struct sja1105_private *priv) 1778aa9ebccSVladimir Oltean { 1788aa9ebccSVladimir Oltean struct sja1105_table *table; 1798aa9ebccSVladimir Oltean 1808aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP]; 1818aa9ebccSVladimir Oltean 182291d1e72SVladimir Oltean /* We only populate the FDB table through dynamic 183291d1e72SVladimir Oltean * L2 Address Lookup entries 184291d1e72SVladimir Oltean */ 1858aa9ebccSVladimir Oltean if (table->entry_count) { 1868aa9ebccSVladimir Oltean kfree(table->entries); 1878aa9ebccSVladimir Oltean table->entry_count = 0; 1888aa9ebccSVladimir Oltean } 1898aa9ebccSVladimir Oltean return 0; 1908aa9ebccSVladimir Oltean } 1918aa9ebccSVladimir Oltean 1928aa9ebccSVladimir Oltean static int sja1105_init_l2_lookup_params(struct sja1105_private *priv) 1938aa9ebccSVladimir Oltean { 1948aa9ebccSVladimir Oltean struct sja1105_table *table; 1958aa9ebccSVladimir Oltean struct sja1105_l2_lookup_params_entry default_l2_lookup_params = { 1968aa9ebccSVladimir Oltean /* TODO Learned FDB entries are never forgotten */ 1978aa9ebccSVladimir Oltean .maxage = 0, 1988aa9ebccSVladimir Oltean /* All entries within a FDB bin are available for learning */ 1998aa9ebccSVladimir Oltean .dyn_tbsz = SJA1105ET_FDB_BIN_SIZE, 2008aa9ebccSVladimir Oltean /* 2^8 + 2^5 + 2^3 + 2^2 + 2^1 + 1 in Koopman notation */ 2018aa9ebccSVladimir Oltean .poly = 0x97, 2028aa9ebccSVladimir Oltean /* This selects between Independent VLAN Learning (IVL) and 2038aa9ebccSVladimir Oltean * Shared VLAN Learning (SVL) 2048aa9ebccSVladimir Oltean */ 2058aa9ebccSVladimir Oltean .shared_learn = false, 2068aa9ebccSVladimir Oltean /* Don't discard management traffic based on ENFPORT - 2078aa9ebccSVladimir Oltean * we don't perform SMAC port enforcement anyway, so 2088aa9ebccSVladimir Oltean * what we are setting here doesn't matter. 2098aa9ebccSVladimir Oltean */ 2108aa9ebccSVladimir Oltean .no_enf_hostprt = false, 2118aa9ebccSVladimir Oltean /* Don't learn SMAC for mac_fltres1 and mac_fltres0. 2128aa9ebccSVladimir Oltean * Maybe correlate with no_linklocal_learn from bridge driver? 2138aa9ebccSVladimir Oltean */ 2148aa9ebccSVladimir Oltean .no_mgmt_learn = true, 2158aa9ebccSVladimir Oltean }; 2168aa9ebccSVladimir Oltean 2178aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_LOOKUP_PARAMS]; 2188aa9ebccSVladimir Oltean 2198aa9ebccSVladimir Oltean if (table->entry_count) { 2208aa9ebccSVladimir Oltean kfree(table->entries); 2218aa9ebccSVladimir Oltean table->entry_count = 0; 2228aa9ebccSVladimir Oltean } 2238aa9ebccSVladimir Oltean 2248aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT, 2258aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2268aa9ebccSVladimir Oltean if (!table->entries) 2278aa9ebccSVladimir Oltean return -ENOMEM; 2288aa9ebccSVladimir Oltean 2298aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_LOOKUP_PARAMS_COUNT; 2308aa9ebccSVladimir Oltean 2318aa9ebccSVladimir Oltean /* This table only has a single entry */ 2328aa9ebccSVladimir Oltean ((struct sja1105_l2_lookup_params_entry *)table->entries)[0] = 2338aa9ebccSVladimir Oltean default_l2_lookup_params; 2348aa9ebccSVladimir Oltean 2358aa9ebccSVladimir Oltean return 0; 2368aa9ebccSVladimir Oltean } 2378aa9ebccSVladimir Oltean 2388aa9ebccSVladimir Oltean static int sja1105_init_static_vlan(struct sja1105_private *priv) 2398aa9ebccSVladimir Oltean { 2408aa9ebccSVladimir Oltean struct sja1105_table *table; 2418aa9ebccSVladimir Oltean struct sja1105_vlan_lookup_entry pvid = { 2428aa9ebccSVladimir Oltean .ving_mirr = 0, 2438aa9ebccSVladimir Oltean .vegr_mirr = 0, 2448aa9ebccSVladimir Oltean .vmemb_port = 0, 2458aa9ebccSVladimir Oltean .vlan_bc = 0, 2468aa9ebccSVladimir Oltean .tag_port = 0, 2478aa9ebccSVladimir Oltean .vlanid = 0, 2488aa9ebccSVladimir Oltean }; 2498aa9ebccSVladimir Oltean int i; 2508aa9ebccSVladimir Oltean 2518aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_VLAN_LOOKUP]; 2528aa9ebccSVladimir Oltean 2538aa9ebccSVladimir Oltean /* The static VLAN table will only contain the initial pvid of 0. 2548aa9ebccSVladimir Oltean */ 2558aa9ebccSVladimir Oltean if (table->entry_count) { 2568aa9ebccSVladimir Oltean kfree(table->entries); 2578aa9ebccSVladimir Oltean table->entry_count = 0; 2588aa9ebccSVladimir Oltean } 2598aa9ebccSVladimir Oltean 2608aa9ebccSVladimir Oltean table->entries = kcalloc(1, table->ops->unpacked_entry_size, 2618aa9ebccSVladimir Oltean GFP_KERNEL); 2628aa9ebccSVladimir Oltean if (!table->entries) 2638aa9ebccSVladimir Oltean return -ENOMEM; 2648aa9ebccSVladimir Oltean 2658aa9ebccSVladimir Oltean table->entry_count = 1; 2668aa9ebccSVladimir Oltean 2678aa9ebccSVladimir Oltean /* VLAN ID 0: all DT-defined ports are members; no restrictions on 2688aa9ebccSVladimir Oltean * forwarding; always transmit priority-tagged frames as untagged. 2698aa9ebccSVladimir Oltean */ 2708aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 2718aa9ebccSVladimir Oltean pvid.vmemb_port |= BIT(i); 2728aa9ebccSVladimir Oltean pvid.vlan_bc |= BIT(i); 2738aa9ebccSVladimir Oltean pvid.tag_port &= ~BIT(i); 2748aa9ebccSVladimir Oltean } 2758aa9ebccSVladimir Oltean 2768aa9ebccSVladimir Oltean ((struct sja1105_vlan_lookup_entry *)table->entries)[0] = pvid; 2778aa9ebccSVladimir Oltean return 0; 2788aa9ebccSVladimir Oltean } 2798aa9ebccSVladimir Oltean 2808aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding(struct sja1105_private *priv) 2818aa9ebccSVladimir Oltean { 2828aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2fwd; 2838aa9ebccSVladimir Oltean struct sja1105_table *table; 2848aa9ebccSVladimir Oltean int i, j; 2858aa9ebccSVladimir Oltean 2868aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING]; 2878aa9ebccSVladimir Oltean 2888aa9ebccSVladimir Oltean if (table->entry_count) { 2898aa9ebccSVladimir Oltean kfree(table->entries); 2908aa9ebccSVladimir Oltean table->entry_count = 0; 2918aa9ebccSVladimir Oltean } 2928aa9ebccSVladimir Oltean 2938aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_COUNT, 2948aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 2958aa9ebccSVladimir Oltean if (!table->entries) 2968aa9ebccSVladimir Oltean return -ENOMEM; 2978aa9ebccSVladimir Oltean 2988aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_COUNT; 2998aa9ebccSVladimir Oltean 3008aa9ebccSVladimir Oltean l2fwd = table->entries; 3018aa9ebccSVladimir Oltean 3028aa9ebccSVladimir Oltean /* First 5 entries define the forwarding rules */ 3038aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 3048aa9ebccSVladimir Oltean unsigned int upstream = dsa_upstream_port(priv->ds, i); 3058aa9ebccSVladimir Oltean 3068aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++) 3078aa9ebccSVladimir Oltean l2fwd[i].vlan_pmap[j] = j; 3088aa9ebccSVladimir Oltean 3098aa9ebccSVladimir Oltean if (i == upstream) 3108aa9ebccSVladimir Oltean continue; 3118aa9ebccSVladimir Oltean 3128aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, i, upstream, true); 3138aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2fwd, upstream, i, true); 3148aa9ebccSVladimir Oltean } 3158aa9ebccSVladimir Oltean /* Next 8 entries define VLAN PCP mapping from ingress to egress. 3168aa9ebccSVladimir Oltean * Create a one-to-one mapping. 3178aa9ebccSVladimir Oltean */ 3188aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_TC; i++) 3198aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_PORTS; j++) 3208aa9ebccSVladimir Oltean l2fwd[SJA1105_NUM_PORTS + i].vlan_pmap[j] = i; 3218aa9ebccSVladimir Oltean 3228aa9ebccSVladimir Oltean return 0; 3238aa9ebccSVladimir Oltean } 3248aa9ebccSVladimir Oltean 3258aa9ebccSVladimir Oltean static int sja1105_init_l2_forwarding_params(struct sja1105_private *priv) 3268aa9ebccSVladimir Oltean { 3278aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_params_entry default_l2fwd_params = { 3288aa9ebccSVladimir Oltean /* Disallow dynamic reconfiguration of vlan_pmap */ 3298aa9ebccSVladimir Oltean .max_dynp = 0, 3308aa9ebccSVladimir Oltean /* Use a single memory partition for all ingress queues */ 3318aa9ebccSVladimir Oltean .part_spc = { SJA1105_MAX_FRAME_MEMORY, 0, 0, 0, 0, 0, 0, 0 }, 3328aa9ebccSVladimir Oltean }; 3338aa9ebccSVladimir Oltean struct sja1105_table *table; 3348aa9ebccSVladimir Oltean 3358aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_FORWARDING_PARAMS]; 3368aa9ebccSVladimir Oltean 3378aa9ebccSVladimir Oltean if (table->entry_count) { 3388aa9ebccSVladimir Oltean kfree(table->entries); 3398aa9ebccSVladimir Oltean table->entry_count = 0; 3408aa9ebccSVladimir Oltean } 3418aa9ebccSVladimir Oltean 3428aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 3438aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 3448aa9ebccSVladimir Oltean if (!table->entries) 3458aa9ebccSVladimir Oltean return -ENOMEM; 3468aa9ebccSVladimir Oltean 3478aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT; 3488aa9ebccSVladimir Oltean 3498aa9ebccSVladimir Oltean /* This table only has a single entry */ 3508aa9ebccSVladimir Oltean ((struct sja1105_l2_forwarding_params_entry *)table->entries)[0] = 3518aa9ebccSVladimir Oltean default_l2fwd_params; 3528aa9ebccSVladimir Oltean 3538aa9ebccSVladimir Oltean return 0; 3548aa9ebccSVladimir Oltean } 3558aa9ebccSVladimir Oltean 3568aa9ebccSVladimir Oltean static int sja1105_init_general_params(struct sja1105_private *priv) 3578aa9ebccSVladimir Oltean { 3588aa9ebccSVladimir Oltean struct sja1105_general_params_entry default_general_params = { 3598aa9ebccSVladimir Oltean /* Disallow dynamic changing of the mirror port */ 3608aa9ebccSVladimir Oltean .mirr_ptacu = 0, 3618aa9ebccSVladimir Oltean .switchid = priv->ds->index, 3628aa9ebccSVladimir Oltean /* Priority queue for link-local frames trapped to CPU */ 3638aa9ebccSVladimir Oltean .hostprio = 0, 3648aa9ebccSVladimir Oltean .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 3658aa9ebccSVladimir Oltean .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 3668aa9ebccSVladimir Oltean .incl_srcpt1 = true, 3678aa9ebccSVladimir Oltean .send_meta1 = false, 3688aa9ebccSVladimir Oltean .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 3698aa9ebccSVladimir Oltean .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 3708aa9ebccSVladimir Oltean .incl_srcpt0 = true, 3718aa9ebccSVladimir Oltean .send_meta0 = false, 3728aa9ebccSVladimir Oltean /* The destination for traffic matching mac_fltres1 and 3738aa9ebccSVladimir Oltean * mac_fltres0 on all ports except host_port. Such traffic 3748aa9ebccSVladimir Oltean * receieved on host_port itself would be dropped, except 3758aa9ebccSVladimir Oltean * by installing a temporary 'management route' 3768aa9ebccSVladimir Oltean */ 3778aa9ebccSVladimir Oltean .host_port = dsa_upstream_port(priv->ds, 0), 3788aa9ebccSVladimir Oltean /* Same as host port */ 3798aa9ebccSVladimir Oltean .mirr_port = dsa_upstream_port(priv->ds, 0), 3808aa9ebccSVladimir Oltean /* Link-local traffic received on casc_port will be forwarded 3818aa9ebccSVladimir Oltean * to host_port without embedding the source port and device ID 3828aa9ebccSVladimir Oltean * info in the destination MAC address (presumably because it 3838aa9ebccSVladimir Oltean * is a cascaded port and a downstream SJA switch already did 3848aa9ebccSVladimir Oltean * that). Default to an invalid port (to disable the feature) 3858aa9ebccSVladimir Oltean * and overwrite this if we find any DSA (cascaded) ports. 3868aa9ebccSVladimir Oltean */ 3878aa9ebccSVladimir Oltean .casc_port = SJA1105_NUM_PORTS, 3888aa9ebccSVladimir Oltean /* No TTEthernet */ 3898aa9ebccSVladimir Oltean .vllupformat = 0, 3908aa9ebccSVladimir Oltean .vlmarker = 0, 3918aa9ebccSVladimir Oltean .vlmask = 0, 3928aa9ebccSVladimir Oltean /* Only update correctionField for 1-step PTP (L2 transport) */ 3938aa9ebccSVladimir Oltean .ignore2stf = 0, 3948aa9ebccSVladimir Oltean .tpid = ETH_P_8021Q, 3958aa9ebccSVladimir Oltean .tpid2 = ETH_P_8021Q, 3968aa9ebccSVladimir Oltean }; 3978aa9ebccSVladimir Oltean struct sja1105_table *table; 3988aa9ebccSVladimir Oltean int i; 3998aa9ebccSVladimir Oltean 4008aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) 4018aa9ebccSVladimir Oltean if (dsa_is_dsa_port(priv->ds, i)) 4028aa9ebccSVladimir Oltean default_general_params.casc_port = i; 4038aa9ebccSVladimir Oltean 4048aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 4058aa9ebccSVladimir Oltean 4068aa9ebccSVladimir Oltean if (table->entry_count) { 4078aa9ebccSVladimir Oltean kfree(table->entries); 4088aa9ebccSVladimir Oltean table->entry_count = 0; 4098aa9ebccSVladimir Oltean } 4108aa9ebccSVladimir Oltean 4118aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_GENERAL_PARAMS_COUNT, 4128aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4138aa9ebccSVladimir Oltean if (!table->entries) 4148aa9ebccSVladimir Oltean return -ENOMEM; 4158aa9ebccSVladimir Oltean 4168aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_GENERAL_PARAMS_COUNT; 4178aa9ebccSVladimir Oltean 4188aa9ebccSVladimir Oltean /* This table only has a single entry */ 4198aa9ebccSVladimir Oltean ((struct sja1105_general_params_entry *)table->entries)[0] = 4208aa9ebccSVladimir Oltean default_general_params; 4218aa9ebccSVladimir Oltean 4228aa9ebccSVladimir Oltean return 0; 4238aa9ebccSVladimir Oltean } 4248aa9ebccSVladimir Oltean 4258aa9ebccSVladimir Oltean #define SJA1105_RATE_MBPS(speed) (((speed) * 64000) / 1000) 4268aa9ebccSVladimir Oltean 4278aa9ebccSVladimir Oltean static inline void 4288aa9ebccSVladimir Oltean sja1105_setup_policer(struct sja1105_l2_policing_entry *policing, 4298aa9ebccSVladimir Oltean int index) 4308aa9ebccSVladimir Oltean { 4318aa9ebccSVladimir Oltean policing[index].sharindx = index; 4328aa9ebccSVladimir Oltean policing[index].smax = 65535; /* Burst size in bytes */ 4338aa9ebccSVladimir Oltean policing[index].rate = SJA1105_RATE_MBPS(1000); 4348aa9ebccSVladimir Oltean policing[index].maxlen = ETH_FRAME_LEN + VLAN_HLEN + ETH_FCS_LEN; 4358aa9ebccSVladimir Oltean policing[index].partition = 0; 4368aa9ebccSVladimir Oltean } 4378aa9ebccSVladimir Oltean 4388aa9ebccSVladimir Oltean static int sja1105_init_l2_policing(struct sja1105_private *priv) 4398aa9ebccSVladimir Oltean { 4408aa9ebccSVladimir Oltean struct sja1105_l2_policing_entry *policing; 4418aa9ebccSVladimir Oltean struct sja1105_table *table; 4428aa9ebccSVladimir Oltean int i, j, k; 4438aa9ebccSVladimir Oltean 4448aa9ebccSVladimir Oltean table = &priv->static_config.tables[BLK_IDX_L2_POLICING]; 4458aa9ebccSVladimir Oltean 4468aa9ebccSVladimir Oltean /* Discard previous L2 Policing Table */ 4478aa9ebccSVladimir Oltean if (table->entry_count) { 4488aa9ebccSVladimir Oltean kfree(table->entries); 4498aa9ebccSVladimir Oltean table->entry_count = 0; 4508aa9ebccSVladimir Oltean } 4518aa9ebccSVladimir Oltean 4528aa9ebccSVladimir Oltean table->entries = kcalloc(SJA1105_MAX_L2_POLICING_COUNT, 4538aa9ebccSVladimir Oltean table->ops->unpacked_entry_size, GFP_KERNEL); 4548aa9ebccSVladimir Oltean if (!table->entries) 4558aa9ebccSVladimir Oltean return -ENOMEM; 4568aa9ebccSVladimir Oltean 4578aa9ebccSVladimir Oltean table->entry_count = SJA1105_MAX_L2_POLICING_COUNT; 4588aa9ebccSVladimir Oltean 4598aa9ebccSVladimir Oltean policing = table->entries; 4608aa9ebccSVladimir Oltean 4618aa9ebccSVladimir Oltean /* k sweeps through all unicast policers (0-39). 4628aa9ebccSVladimir Oltean * bcast sweeps through policers 40-44. 4638aa9ebccSVladimir Oltean */ 4648aa9ebccSVladimir Oltean for (i = 0, k = 0; i < SJA1105_NUM_PORTS; i++) { 4658aa9ebccSVladimir Oltean int bcast = (SJA1105_NUM_PORTS * SJA1105_NUM_TC) + i; 4668aa9ebccSVladimir Oltean 4678aa9ebccSVladimir Oltean for (j = 0; j < SJA1105_NUM_TC; j++, k++) 4688aa9ebccSVladimir Oltean sja1105_setup_policer(policing, k); 4698aa9ebccSVladimir Oltean 4708aa9ebccSVladimir Oltean /* Set up this port's policer for broadcast traffic */ 4718aa9ebccSVladimir Oltean sja1105_setup_policer(policing, bcast); 4728aa9ebccSVladimir Oltean } 4738aa9ebccSVladimir Oltean return 0; 4748aa9ebccSVladimir Oltean } 4758aa9ebccSVladimir Oltean 4768aa9ebccSVladimir Oltean static int sja1105_static_config_load(struct sja1105_private *priv, 4778aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 4788aa9ebccSVladimir Oltean { 4798aa9ebccSVladimir Oltean int rc; 4808aa9ebccSVladimir Oltean 4818aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 4828aa9ebccSVladimir Oltean rc = sja1105_static_config_init(&priv->static_config, 4838aa9ebccSVladimir Oltean priv->info->static_ops, 4848aa9ebccSVladimir Oltean priv->info->device_id); 4858aa9ebccSVladimir Oltean if (rc) 4868aa9ebccSVladimir Oltean return rc; 4878aa9ebccSVladimir Oltean 4888aa9ebccSVladimir Oltean /* Build static configuration */ 4898aa9ebccSVladimir Oltean rc = sja1105_init_mac_settings(priv); 4908aa9ebccSVladimir Oltean if (rc < 0) 4918aa9ebccSVladimir Oltean return rc; 4928aa9ebccSVladimir Oltean rc = sja1105_init_mii_settings(priv, ports); 4938aa9ebccSVladimir Oltean if (rc < 0) 4948aa9ebccSVladimir Oltean return rc; 4958aa9ebccSVladimir Oltean rc = sja1105_init_static_fdb(priv); 4968aa9ebccSVladimir Oltean if (rc < 0) 4978aa9ebccSVladimir Oltean return rc; 4988aa9ebccSVladimir Oltean rc = sja1105_init_static_vlan(priv); 4998aa9ebccSVladimir Oltean if (rc < 0) 5008aa9ebccSVladimir Oltean return rc; 5018aa9ebccSVladimir Oltean rc = sja1105_init_l2_lookup_params(priv); 5028aa9ebccSVladimir Oltean if (rc < 0) 5038aa9ebccSVladimir Oltean return rc; 5048aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding(priv); 5058aa9ebccSVladimir Oltean if (rc < 0) 5068aa9ebccSVladimir Oltean return rc; 5078aa9ebccSVladimir Oltean rc = sja1105_init_l2_forwarding_params(priv); 5088aa9ebccSVladimir Oltean if (rc < 0) 5098aa9ebccSVladimir Oltean return rc; 5108aa9ebccSVladimir Oltean rc = sja1105_init_l2_policing(priv); 5118aa9ebccSVladimir Oltean if (rc < 0) 5128aa9ebccSVladimir Oltean return rc; 5138aa9ebccSVladimir Oltean rc = sja1105_init_general_params(priv); 5148aa9ebccSVladimir Oltean if (rc < 0) 5158aa9ebccSVladimir Oltean return rc; 5168aa9ebccSVladimir Oltean 5178aa9ebccSVladimir Oltean /* Send initial configuration to hardware via SPI */ 5188aa9ebccSVladimir Oltean return sja1105_static_config_upload(priv); 5198aa9ebccSVladimir Oltean } 5208aa9ebccSVladimir Oltean 521*f5b8631cSVladimir Oltean static int sja1105_parse_rgmii_delays(struct sja1105_private *priv, 522*f5b8631cSVladimir Oltean const struct sja1105_dt_port *ports) 523*f5b8631cSVladimir Oltean { 524*f5b8631cSVladimir Oltean int i; 525*f5b8631cSVladimir Oltean 526*f5b8631cSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 527*f5b8631cSVladimir Oltean if (ports->role == XMII_MAC) 528*f5b8631cSVladimir Oltean continue; 529*f5b8631cSVladimir Oltean 530*f5b8631cSVladimir Oltean if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || 531*f5b8631cSVladimir Oltean ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 532*f5b8631cSVladimir Oltean priv->rgmii_rx_delay[i] = true; 533*f5b8631cSVladimir Oltean 534*f5b8631cSVladimir Oltean if (ports->phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || 535*f5b8631cSVladimir Oltean ports->phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 536*f5b8631cSVladimir Oltean priv->rgmii_tx_delay[i] = true; 537*f5b8631cSVladimir Oltean 538*f5b8631cSVladimir Oltean if ((priv->rgmii_rx_delay[i] || priv->rgmii_tx_delay[i]) && 539*f5b8631cSVladimir Oltean !priv->info->setup_rgmii_delay) 540*f5b8631cSVladimir Oltean return -EINVAL; 541*f5b8631cSVladimir Oltean } 542*f5b8631cSVladimir Oltean return 0; 543*f5b8631cSVladimir Oltean } 544*f5b8631cSVladimir Oltean 5458aa9ebccSVladimir Oltean static int sja1105_parse_ports_node(struct sja1105_private *priv, 5468aa9ebccSVladimir Oltean struct sja1105_dt_port *ports, 5478aa9ebccSVladimir Oltean struct device_node *ports_node) 5488aa9ebccSVladimir Oltean { 5498aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 5508aa9ebccSVladimir Oltean struct device_node *child; 5518aa9ebccSVladimir Oltean 5528aa9ebccSVladimir Oltean for_each_child_of_node(ports_node, child) { 5538aa9ebccSVladimir Oltean struct device_node *phy_node; 5548aa9ebccSVladimir Oltean int phy_mode; 5558aa9ebccSVladimir Oltean u32 index; 5568aa9ebccSVladimir Oltean 5578aa9ebccSVladimir Oltean /* Get switch port number from DT */ 5588aa9ebccSVladimir Oltean if (of_property_read_u32(child, "reg", &index) < 0) { 5598aa9ebccSVladimir Oltean dev_err(dev, "Port number not defined in device tree " 5608aa9ebccSVladimir Oltean "(property \"reg\")\n"); 5618aa9ebccSVladimir Oltean return -ENODEV; 5628aa9ebccSVladimir Oltean } 5638aa9ebccSVladimir Oltean 5648aa9ebccSVladimir Oltean /* Get PHY mode from DT */ 5658aa9ebccSVladimir Oltean phy_mode = of_get_phy_mode(child); 5668aa9ebccSVladimir Oltean if (phy_mode < 0) { 5678aa9ebccSVladimir Oltean dev_err(dev, "Failed to read phy-mode or " 5688aa9ebccSVladimir Oltean "phy-interface-type property for port %d\n", 5698aa9ebccSVladimir Oltean index); 5708aa9ebccSVladimir Oltean return -ENODEV; 5718aa9ebccSVladimir Oltean } 5728aa9ebccSVladimir Oltean ports[index].phy_mode = phy_mode; 5738aa9ebccSVladimir Oltean 5748aa9ebccSVladimir Oltean phy_node = of_parse_phandle(child, "phy-handle", 0); 5758aa9ebccSVladimir Oltean if (!phy_node) { 5768aa9ebccSVladimir Oltean if (!of_phy_is_fixed_link(child)) { 5778aa9ebccSVladimir Oltean dev_err(dev, "phy-handle or fixed-link " 5788aa9ebccSVladimir Oltean "properties missing!\n"); 5798aa9ebccSVladimir Oltean return -ENODEV; 5808aa9ebccSVladimir Oltean } 5818aa9ebccSVladimir Oltean /* phy-handle is missing, but fixed-link isn't. 5828aa9ebccSVladimir Oltean * So it's a fixed link. Default to PHY role. 5838aa9ebccSVladimir Oltean */ 5848aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 5858aa9ebccSVladimir Oltean } else { 5868aa9ebccSVladimir Oltean /* phy-handle present => put port in MAC role */ 5878aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 5888aa9ebccSVladimir Oltean of_node_put(phy_node); 5898aa9ebccSVladimir Oltean } 5908aa9ebccSVladimir Oltean 5918aa9ebccSVladimir Oltean /* The MAC/PHY role can be overridden with explicit bindings */ 5928aa9ebccSVladimir Oltean if (of_property_read_bool(child, "sja1105,role-mac")) 5938aa9ebccSVladimir Oltean ports[index].role = XMII_MAC; 5948aa9ebccSVladimir Oltean else if (of_property_read_bool(child, "sja1105,role-phy")) 5958aa9ebccSVladimir Oltean ports[index].role = XMII_PHY; 5968aa9ebccSVladimir Oltean } 5978aa9ebccSVladimir Oltean 5988aa9ebccSVladimir Oltean return 0; 5998aa9ebccSVladimir Oltean } 6008aa9ebccSVladimir Oltean 6018aa9ebccSVladimir Oltean static int sja1105_parse_dt(struct sja1105_private *priv, 6028aa9ebccSVladimir Oltean struct sja1105_dt_port *ports) 6038aa9ebccSVladimir Oltean { 6048aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 6058aa9ebccSVladimir Oltean struct device_node *switch_node = dev->of_node; 6068aa9ebccSVladimir Oltean struct device_node *ports_node; 6078aa9ebccSVladimir Oltean int rc; 6088aa9ebccSVladimir Oltean 6098aa9ebccSVladimir Oltean ports_node = of_get_child_by_name(switch_node, "ports"); 6108aa9ebccSVladimir Oltean if (!ports_node) { 6118aa9ebccSVladimir Oltean dev_err(dev, "Incorrect bindings: absent \"ports\" node\n"); 6128aa9ebccSVladimir Oltean return -ENODEV; 6138aa9ebccSVladimir Oltean } 6148aa9ebccSVladimir Oltean 6158aa9ebccSVladimir Oltean rc = sja1105_parse_ports_node(priv, ports, ports_node); 6168aa9ebccSVladimir Oltean of_node_put(ports_node); 6178aa9ebccSVladimir Oltean 6188aa9ebccSVladimir Oltean return rc; 6198aa9ebccSVladimir Oltean } 6208aa9ebccSVladimir Oltean 6218aa9ebccSVladimir Oltean /* Convert back and forth MAC speed from Mbps to SJA1105 encoding */ 6228aa9ebccSVladimir Oltean static int sja1105_speed[] = { 6238aa9ebccSVladimir Oltean [SJA1105_SPEED_AUTO] = 0, 6248aa9ebccSVladimir Oltean [SJA1105_SPEED_10MBPS] = 10, 6258aa9ebccSVladimir Oltean [SJA1105_SPEED_100MBPS] = 100, 6268aa9ebccSVladimir Oltean [SJA1105_SPEED_1000MBPS] = 1000, 6278aa9ebccSVladimir Oltean }; 6288aa9ebccSVladimir Oltean 6298aa9ebccSVladimir Oltean static sja1105_speed_t sja1105_get_speed_cfg(unsigned int speed_mbps) 6308aa9ebccSVladimir Oltean { 6318aa9ebccSVladimir Oltean int i; 6328aa9ebccSVladimir Oltean 6338aa9ebccSVladimir Oltean for (i = SJA1105_SPEED_AUTO; i <= SJA1105_SPEED_1000MBPS; i++) 6348aa9ebccSVladimir Oltean if (sja1105_speed[i] == speed_mbps) 6358aa9ebccSVladimir Oltean return i; 6368aa9ebccSVladimir Oltean return -EINVAL; 6378aa9ebccSVladimir Oltean } 6388aa9ebccSVladimir Oltean 6398aa9ebccSVladimir Oltean /* Set link speed and enable/disable traffic I/O in the MAC configuration 6408aa9ebccSVladimir Oltean * for a specific port. 6418aa9ebccSVladimir Oltean * 6428aa9ebccSVladimir Oltean * @speed_mbps: If 0, leave the speed unchanged, else adapt MAC to PHY speed. 6438aa9ebccSVladimir Oltean * @enabled: Manage Rx and Tx settings for this port. Overrides the static 6448aa9ebccSVladimir Oltean * configuration settings. 6458aa9ebccSVladimir Oltean */ 6468aa9ebccSVladimir Oltean static int sja1105_adjust_port_config(struct sja1105_private *priv, int port, 6478aa9ebccSVladimir Oltean int speed_mbps, bool enabled) 6488aa9ebccSVladimir Oltean { 6498aa9ebccSVladimir Oltean struct sja1105_xmii_params_entry *mii; 6508aa9ebccSVladimir Oltean struct sja1105_mac_config_entry *mac; 6518aa9ebccSVladimir Oltean struct device *dev = priv->ds->dev; 6528aa9ebccSVladimir Oltean sja1105_phy_interface_t phy_mode; 6538aa9ebccSVladimir Oltean sja1105_speed_t speed; 6548aa9ebccSVladimir Oltean int rc; 6558aa9ebccSVladimir Oltean 6568aa9ebccSVladimir Oltean mii = priv->static_config.tables[BLK_IDX_XMII_PARAMS].entries; 6578aa9ebccSVladimir Oltean mac = priv->static_config.tables[BLK_IDX_MAC_CONFIG].entries; 6588aa9ebccSVladimir Oltean 6598aa9ebccSVladimir Oltean speed = sja1105_get_speed_cfg(speed_mbps); 6608aa9ebccSVladimir Oltean if (speed_mbps && speed < 0) { 6618aa9ebccSVladimir Oltean dev_err(dev, "Invalid speed %iMbps\n", speed_mbps); 6628aa9ebccSVladimir Oltean return -EINVAL; 6638aa9ebccSVladimir Oltean } 6648aa9ebccSVladimir Oltean 6658aa9ebccSVladimir Oltean /* If requested, overwrite SJA1105_SPEED_AUTO from the static MAC 6668aa9ebccSVladimir Oltean * configuration table, since this will be used for the clocking setup, 6678aa9ebccSVladimir Oltean * and we no longer need to store it in the static config (already told 6688aa9ebccSVladimir Oltean * hardware we want auto during upload phase). 6698aa9ebccSVladimir Oltean */ 6708aa9ebccSVladimir Oltean if (speed_mbps) 6718aa9ebccSVladimir Oltean mac[port].speed = speed; 6728aa9ebccSVladimir Oltean else 6738aa9ebccSVladimir Oltean mac[port].speed = SJA1105_SPEED_AUTO; 6748aa9ebccSVladimir Oltean 6758aa9ebccSVladimir Oltean /* On P/Q/R/S, one can read from the device via the MAC reconfiguration 6768aa9ebccSVladimir Oltean * tables. On E/T, MAC reconfig tables are not readable, only writable. 6778aa9ebccSVladimir Oltean * We have to *know* what the MAC looks like. For the sake of keeping 6788aa9ebccSVladimir Oltean * the code common, we'll use the static configuration tables as a 6798aa9ebccSVladimir Oltean * reasonable approximation for both E/T and P/Q/R/S. 6808aa9ebccSVladimir Oltean */ 6818aa9ebccSVladimir Oltean mac[port].ingress = enabled; 6828aa9ebccSVladimir Oltean mac[port].egress = enabled; 6838aa9ebccSVladimir Oltean 6848aa9ebccSVladimir Oltean /* Write to the dynamic reconfiguration tables */ 6858aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_MAC_CONFIG, 6868aa9ebccSVladimir Oltean port, &mac[port], true); 6878aa9ebccSVladimir Oltean if (rc < 0) { 6888aa9ebccSVladimir Oltean dev_err(dev, "Failed to write MAC config: %d\n", rc); 6898aa9ebccSVladimir Oltean return rc; 6908aa9ebccSVladimir Oltean } 6918aa9ebccSVladimir Oltean 6928aa9ebccSVladimir Oltean /* Reconfigure the PLLs for the RGMII interfaces (required 125 MHz at 6938aa9ebccSVladimir Oltean * gigabit, 25 MHz at 100 Mbps and 2.5 MHz at 10 Mbps). For MII and 6948aa9ebccSVladimir Oltean * RMII no change of the clock setup is required. Actually, changing 6958aa9ebccSVladimir Oltean * the clock setup does interrupt the clock signal for a certain time 6968aa9ebccSVladimir Oltean * which causes trouble for all PHYs relying on this signal. 6978aa9ebccSVladimir Oltean */ 6988aa9ebccSVladimir Oltean if (!enabled) 6998aa9ebccSVladimir Oltean return 0; 7008aa9ebccSVladimir Oltean 7018aa9ebccSVladimir Oltean phy_mode = mii->xmii_mode[port]; 7028aa9ebccSVladimir Oltean if (phy_mode != XMII_MODE_RGMII) 7038aa9ebccSVladimir Oltean return 0; 7048aa9ebccSVladimir Oltean 7058aa9ebccSVladimir Oltean return sja1105_clocking_setup_port(priv, port); 7068aa9ebccSVladimir Oltean } 7078aa9ebccSVladimir Oltean 7088aa9ebccSVladimir Oltean static void sja1105_adjust_link(struct dsa_switch *ds, int port, 7098aa9ebccSVladimir Oltean struct phy_device *phydev) 7108aa9ebccSVladimir Oltean { 7118aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 7128aa9ebccSVladimir Oltean 7138aa9ebccSVladimir Oltean if (!phydev->link) 7148aa9ebccSVladimir Oltean sja1105_adjust_port_config(priv, port, 0, false); 7158aa9ebccSVladimir Oltean else 7168aa9ebccSVladimir Oltean sja1105_adjust_port_config(priv, port, phydev->speed, true); 7178aa9ebccSVladimir Oltean } 7188aa9ebccSVladimir Oltean 719291d1e72SVladimir Oltean /* First-generation switches have a 4-way set associative TCAM that 720291d1e72SVladimir Oltean * holds the FDB entries. An FDB index spans from 0 to 1023 and is comprised of 721291d1e72SVladimir Oltean * a "bin" (grouping of 4 entries) and a "way" (an entry within a bin). 722291d1e72SVladimir Oltean * For the placement of a newly learnt FDB entry, the switch selects the bin 723291d1e72SVladimir Oltean * based on a hash function, and the way within that bin incrementally. 724291d1e72SVladimir Oltean */ 725291d1e72SVladimir Oltean static inline int sja1105et_fdb_index(int bin, int way) 726291d1e72SVladimir Oltean { 727291d1e72SVladimir Oltean return bin * SJA1105ET_FDB_BIN_SIZE + way; 728291d1e72SVladimir Oltean } 729291d1e72SVladimir Oltean 730291d1e72SVladimir Oltean static int sja1105_is_fdb_entry_in_bin(struct sja1105_private *priv, int bin, 731291d1e72SVladimir Oltean const u8 *addr, u16 vid, 732291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry *match, 733291d1e72SVladimir Oltean int *last_unused) 734291d1e72SVladimir Oltean { 735291d1e72SVladimir Oltean int way; 736291d1e72SVladimir Oltean 737291d1e72SVladimir Oltean for (way = 0; way < SJA1105ET_FDB_BIN_SIZE; way++) { 738291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 739291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 740291d1e72SVladimir Oltean 741291d1e72SVladimir Oltean /* Skip unused entries, optionally marking them 742291d1e72SVladimir Oltean * into the return value 743291d1e72SVladimir Oltean */ 744291d1e72SVladimir Oltean if (sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 745291d1e72SVladimir Oltean index, &l2_lookup)) { 746291d1e72SVladimir Oltean if (last_unused) 747291d1e72SVladimir Oltean *last_unused = way; 748291d1e72SVladimir Oltean continue; 749291d1e72SVladimir Oltean } 750291d1e72SVladimir Oltean 751291d1e72SVladimir Oltean if (l2_lookup.macaddr == ether_addr_to_u64(addr) && 752291d1e72SVladimir Oltean l2_lookup.vlanid == vid) { 753291d1e72SVladimir Oltean if (match) 754291d1e72SVladimir Oltean *match = l2_lookup; 755291d1e72SVladimir Oltean return way; 756291d1e72SVladimir Oltean } 757291d1e72SVladimir Oltean } 758291d1e72SVladimir Oltean /* Return an invalid entry index if not found */ 759291d1e72SVladimir Oltean return -1; 760291d1e72SVladimir Oltean } 761291d1e72SVladimir Oltean 762291d1e72SVladimir Oltean static int sja1105_fdb_add(struct dsa_switch *ds, int port, 763291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 764291d1e72SVladimir Oltean { 765291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 766291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 767291d1e72SVladimir Oltean struct device *dev = ds->dev; 768291d1e72SVladimir Oltean int last_unused = -1; 769291d1e72SVladimir Oltean int bin, way; 770291d1e72SVladimir Oltean 771291d1e72SVladimir Oltean bin = sja1105_fdb_hash(priv, addr, vid); 772291d1e72SVladimir Oltean 773291d1e72SVladimir Oltean way = sja1105_is_fdb_entry_in_bin(priv, bin, addr, vid, 774291d1e72SVladimir Oltean &l2_lookup, &last_unused); 775291d1e72SVladimir Oltean if (way >= 0) { 776291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination 777291d1e72SVladimir Oltean * mask? If yes, we need to do nothing. If not, we need 778291d1e72SVladimir Oltean * to rewrite the entry by adding this port to it. 779291d1e72SVladimir Oltean */ 780291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 781291d1e72SVladimir Oltean return 0; 782291d1e72SVladimir Oltean l2_lookup.destports |= BIT(port); 783291d1e72SVladimir Oltean } else { 784291d1e72SVladimir Oltean int index = sja1105et_fdb_index(bin, way); 785291d1e72SVladimir Oltean 786291d1e72SVladimir Oltean /* We don't have an FDB entry. We construct a new one and 787291d1e72SVladimir Oltean * try to find a place for it within the FDB table. 788291d1e72SVladimir Oltean */ 789291d1e72SVladimir Oltean l2_lookup.macaddr = ether_addr_to_u64(addr); 790291d1e72SVladimir Oltean l2_lookup.destports = BIT(port); 791291d1e72SVladimir Oltean l2_lookup.vlanid = vid; 792291d1e72SVladimir Oltean 793291d1e72SVladimir Oltean if (last_unused >= 0) { 794291d1e72SVladimir Oltean way = last_unused; 795291d1e72SVladimir Oltean } else { 796291d1e72SVladimir Oltean /* Bin is full, need to evict somebody. 797291d1e72SVladimir Oltean * Choose victim at random. If you get these messages 798291d1e72SVladimir Oltean * often, you may need to consider changing the 799291d1e72SVladimir Oltean * distribution function: 800291d1e72SVladimir Oltean * static_config[BLK_IDX_L2_LOOKUP_PARAMS].entries->poly 801291d1e72SVladimir Oltean */ 802291d1e72SVladimir Oltean get_random_bytes(&way, sizeof(u8)); 803291d1e72SVladimir Oltean way %= SJA1105ET_FDB_BIN_SIZE; 804291d1e72SVladimir Oltean dev_warn(dev, "Warning, FDB bin %d full while adding entry for %pM. Evicting entry %u.\n", 805291d1e72SVladimir Oltean bin, addr, way); 806291d1e72SVladimir Oltean /* Evict entry */ 807291d1e72SVladimir Oltean sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 808291d1e72SVladimir Oltean index, NULL, false); 809291d1e72SVladimir Oltean } 810291d1e72SVladimir Oltean } 811291d1e72SVladimir Oltean l2_lookup.index = sja1105et_fdb_index(bin, way); 812291d1e72SVladimir Oltean 813291d1e72SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 814291d1e72SVladimir Oltean l2_lookup.index, &l2_lookup, 815291d1e72SVladimir Oltean true); 816291d1e72SVladimir Oltean } 817291d1e72SVladimir Oltean 818291d1e72SVladimir Oltean static int sja1105_fdb_del(struct dsa_switch *ds, int port, 819291d1e72SVladimir Oltean const unsigned char *addr, u16 vid) 820291d1e72SVladimir Oltean { 821291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 822291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 823291d1e72SVladimir Oltean int index, bin, way; 824291d1e72SVladimir Oltean bool keep; 825291d1e72SVladimir Oltean 826291d1e72SVladimir Oltean bin = sja1105_fdb_hash(priv, addr, vid); 827291d1e72SVladimir Oltean way = sja1105_is_fdb_entry_in_bin(priv, bin, addr, vid, 828291d1e72SVladimir Oltean &l2_lookup, NULL); 829291d1e72SVladimir Oltean if (way < 0) 830291d1e72SVladimir Oltean return 0; 831291d1e72SVladimir Oltean index = sja1105et_fdb_index(bin, way); 832291d1e72SVladimir Oltean 833291d1e72SVladimir Oltean /* We have an FDB entry. Is our port in the destination mask? If yes, 834291d1e72SVladimir Oltean * we need to remove it. If the resulting port mask becomes empty, we 835291d1e72SVladimir Oltean * need to completely evict the FDB entry. 836291d1e72SVladimir Oltean * Otherwise we just write it back. 837291d1e72SVladimir Oltean */ 838291d1e72SVladimir Oltean if (l2_lookup.destports & BIT(port)) 839291d1e72SVladimir Oltean l2_lookup.destports &= ~BIT(port); 840291d1e72SVladimir Oltean if (l2_lookup.destports) 841291d1e72SVladimir Oltean keep = true; 842291d1e72SVladimir Oltean else 843291d1e72SVladimir Oltean keep = false; 844291d1e72SVladimir Oltean 845291d1e72SVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_LOOKUP, 846291d1e72SVladimir Oltean index, &l2_lookup, keep); 847291d1e72SVladimir Oltean } 848291d1e72SVladimir Oltean 849291d1e72SVladimir Oltean static int sja1105_fdb_dump(struct dsa_switch *ds, int port, 850291d1e72SVladimir Oltean dsa_fdb_dump_cb_t *cb, void *data) 851291d1e72SVladimir Oltean { 852291d1e72SVladimir Oltean struct sja1105_private *priv = ds->priv; 853291d1e72SVladimir Oltean struct device *dev = ds->dev; 854291d1e72SVladimir Oltean int i; 855291d1e72SVladimir Oltean 856291d1e72SVladimir Oltean for (i = 0; i < SJA1105_MAX_L2_LOOKUP_COUNT; i++) { 857291d1e72SVladimir Oltean struct sja1105_l2_lookup_entry l2_lookup = {0}; 858291d1e72SVladimir Oltean u8 macaddr[ETH_ALEN]; 859291d1e72SVladimir Oltean int rc; 860291d1e72SVladimir Oltean 861291d1e72SVladimir Oltean rc = sja1105_dynamic_config_read(priv, BLK_IDX_L2_LOOKUP, 862291d1e72SVladimir Oltean i, &l2_lookup); 863291d1e72SVladimir Oltean /* No fdb entry at i, not an issue */ 864291d1e72SVladimir Oltean if (rc == -EINVAL) 865291d1e72SVladimir Oltean continue; 866291d1e72SVladimir Oltean if (rc) { 867291d1e72SVladimir Oltean dev_err(dev, "Failed to dump FDB: %d\n", rc); 868291d1e72SVladimir Oltean return rc; 869291d1e72SVladimir Oltean } 870291d1e72SVladimir Oltean 871291d1e72SVladimir Oltean /* FDB dump callback is per port. This means we have to 872291d1e72SVladimir Oltean * disregard a valid entry if it's not for this port, even if 873291d1e72SVladimir Oltean * only to revisit it later. This is inefficient because the 874291d1e72SVladimir Oltean * 1024-sized FDB table needs to be traversed 4 times through 875291d1e72SVladimir Oltean * SPI during a 'bridge fdb show' command. 876291d1e72SVladimir Oltean */ 877291d1e72SVladimir Oltean if (!(l2_lookup.destports & BIT(port))) 878291d1e72SVladimir Oltean continue; 879291d1e72SVladimir Oltean u64_to_ether_addr(l2_lookup.macaddr, macaddr); 880291d1e72SVladimir Oltean cb(macaddr, l2_lookup.vlanid, false, data); 881291d1e72SVladimir Oltean } 882291d1e72SVladimir Oltean return 0; 883291d1e72SVladimir Oltean } 884291d1e72SVladimir Oltean 885291d1e72SVladimir Oltean /* This callback needs to be present */ 886291d1e72SVladimir Oltean static int sja1105_mdb_prepare(struct dsa_switch *ds, int port, 887291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 888291d1e72SVladimir Oltean { 889291d1e72SVladimir Oltean return 0; 890291d1e72SVladimir Oltean } 891291d1e72SVladimir Oltean 892291d1e72SVladimir Oltean static void sja1105_mdb_add(struct dsa_switch *ds, int port, 893291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 894291d1e72SVladimir Oltean { 895291d1e72SVladimir Oltean sja1105_fdb_add(ds, port, mdb->addr, mdb->vid); 896291d1e72SVladimir Oltean } 897291d1e72SVladimir Oltean 898291d1e72SVladimir Oltean static int sja1105_mdb_del(struct dsa_switch *ds, int port, 899291d1e72SVladimir Oltean const struct switchdev_obj_port_mdb *mdb) 900291d1e72SVladimir Oltean { 901291d1e72SVladimir Oltean return sja1105_fdb_del(ds, port, mdb->addr, mdb->vid); 902291d1e72SVladimir Oltean } 903291d1e72SVladimir Oltean 9048aa9ebccSVladimir Oltean static int sja1105_bridge_member(struct dsa_switch *ds, int port, 9058aa9ebccSVladimir Oltean struct net_device *br, bool member) 9068aa9ebccSVladimir Oltean { 9078aa9ebccSVladimir Oltean struct sja1105_l2_forwarding_entry *l2_fwd; 9088aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 9098aa9ebccSVladimir Oltean int i, rc; 9108aa9ebccSVladimir Oltean 9118aa9ebccSVladimir Oltean l2_fwd = priv->static_config.tables[BLK_IDX_L2_FORWARDING].entries; 9128aa9ebccSVladimir Oltean 9138aa9ebccSVladimir Oltean for (i = 0; i < SJA1105_NUM_PORTS; i++) { 9148aa9ebccSVladimir Oltean /* Add this port to the forwarding matrix of the 9158aa9ebccSVladimir Oltean * other ports in the same bridge, and viceversa. 9168aa9ebccSVladimir Oltean */ 9178aa9ebccSVladimir Oltean if (!dsa_is_user_port(ds, i)) 9188aa9ebccSVladimir Oltean continue; 9198aa9ebccSVladimir Oltean /* For the ports already under the bridge, only one thing needs 9208aa9ebccSVladimir Oltean * to be done, and that is to add this port to their 9218aa9ebccSVladimir Oltean * reachability domain. So we can perform the SPI write for 9228aa9ebccSVladimir Oltean * them immediately. However, for this port itself (the one 9238aa9ebccSVladimir Oltean * that is new to the bridge), we need to add all other ports 9248aa9ebccSVladimir Oltean * to its reachability domain. So we do that incrementally in 9258aa9ebccSVladimir Oltean * this loop, and perform the SPI write only at the end, once 9268aa9ebccSVladimir Oltean * the domain contains all other bridge ports. 9278aa9ebccSVladimir Oltean */ 9288aa9ebccSVladimir Oltean if (i == port) 9298aa9ebccSVladimir Oltean continue; 9308aa9ebccSVladimir Oltean if (dsa_to_port(ds, i)->bridge_dev != br) 9318aa9ebccSVladimir Oltean continue; 9328aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, i, port, member); 9338aa9ebccSVladimir Oltean sja1105_port_allow_traffic(l2_fwd, port, i, member); 9348aa9ebccSVladimir Oltean 9358aa9ebccSVladimir Oltean rc = sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 9368aa9ebccSVladimir Oltean i, &l2_fwd[i], true); 9378aa9ebccSVladimir Oltean if (rc < 0) 9388aa9ebccSVladimir Oltean return rc; 9398aa9ebccSVladimir Oltean } 9408aa9ebccSVladimir Oltean 9418aa9ebccSVladimir Oltean return sja1105_dynamic_config_write(priv, BLK_IDX_L2_FORWARDING, 9428aa9ebccSVladimir Oltean port, &l2_fwd[port], true); 9438aa9ebccSVladimir Oltean } 9448aa9ebccSVladimir Oltean 9458aa9ebccSVladimir Oltean static int sja1105_bridge_join(struct dsa_switch *ds, int port, 9468aa9ebccSVladimir Oltean struct net_device *br) 9478aa9ebccSVladimir Oltean { 9488aa9ebccSVladimir Oltean return sja1105_bridge_member(ds, port, br, true); 9498aa9ebccSVladimir Oltean } 9508aa9ebccSVladimir Oltean 9518aa9ebccSVladimir Oltean static void sja1105_bridge_leave(struct dsa_switch *ds, int port, 9528aa9ebccSVladimir Oltean struct net_device *br) 9538aa9ebccSVladimir Oltean { 9548aa9ebccSVladimir Oltean sja1105_bridge_member(ds, port, br, false); 9558aa9ebccSVladimir Oltean } 9568aa9ebccSVladimir Oltean 9578aa9ebccSVladimir Oltean static enum dsa_tag_protocol 9588aa9ebccSVladimir Oltean sja1105_get_tag_protocol(struct dsa_switch *ds, int port) 9598aa9ebccSVladimir Oltean { 9608aa9ebccSVladimir Oltean return DSA_TAG_PROTO_NONE; 9618aa9ebccSVladimir Oltean } 9628aa9ebccSVladimir Oltean 9638aa9ebccSVladimir Oltean /* The programming model for the SJA1105 switch is "all-at-once" via static 9648aa9ebccSVladimir Oltean * configuration tables. Some of these can be dynamically modified at runtime, 9658aa9ebccSVladimir Oltean * but not the xMII mode parameters table. 9668aa9ebccSVladimir Oltean * Furthermode, some PHYs may not have crystals for generating their clocks 9678aa9ebccSVladimir Oltean * (e.g. RMII). Instead, their 50MHz clock is supplied via the SJA1105 port's 9688aa9ebccSVladimir Oltean * ref_clk pin. So port clocking needs to be initialized early, before 9698aa9ebccSVladimir Oltean * connecting to PHYs is attempted, otherwise they won't respond through MDIO. 9708aa9ebccSVladimir Oltean * Setting correct PHY link speed does not matter now. 9718aa9ebccSVladimir Oltean * But dsa_slave_phy_setup is called later than sja1105_setup, so the PHY 9728aa9ebccSVladimir Oltean * bindings are not yet parsed by DSA core. We need to parse early so that we 9738aa9ebccSVladimir Oltean * can populate the xMII mode parameters table. 9748aa9ebccSVladimir Oltean */ 9758aa9ebccSVladimir Oltean static int sja1105_setup(struct dsa_switch *ds) 9768aa9ebccSVladimir Oltean { 9778aa9ebccSVladimir Oltean struct sja1105_dt_port ports[SJA1105_NUM_PORTS]; 9788aa9ebccSVladimir Oltean struct sja1105_private *priv = ds->priv; 9798aa9ebccSVladimir Oltean int rc; 9808aa9ebccSVladimir Oltean 9818aa9ebccSVladimir Oltean rc = sja1105_parse_dt(priv, ports); 9828aa9ebccSVladimir Oltean if (rc < 0) { 9838aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to parse DT: %d\n", rc); 9848aa9ebccSVladimir Oltean return rc; 9858aa9ebccSVladimir Oltean } 986*f5b8631cSVladimir Oltean 987*f5b8631cSVladimir Oltean /* Error out early if internal delays are required through DT 988*f5b8631cSVladimir Oltean * and we can't apply them. 989*f5b8631cSVladimir Oltean */ 990*f5b8631cSVladimir Oltean rc = sja1105_parse_rgmii_delays(priv, ports); 991*f5b8631cSVladimir Oltean if (rc < 0) { 992*f5b8631cSVladimir Oltean dev_err(ds->dev, "RGMII delay not supported\n"); 993*f5b8631cSVladimir Oltean return rc; 994*f5b8631cSVladimir Oltean } 995*f5b8631cSVladimir Oltean 9968aa9ebccSVladimir Oltean /* Create and send configuration down to device */ 9978aa9ebccSVladimir Oltean rc = sja1105_static_config_load(priv, ports); 9988aa9ebccSVladimir Oltean if (rc < 0) { 9998aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to load static config: %d\n", rc); 10008aa9ebccSVladimir Oltean return rc; 10018aa9ebccSVladimir Oltean } 10028aa9ebccSVladimir Oltean /* Configure the CGU (PHY link modes and speeds) */ 10038aa9ebccSVladimir Oltean rc = sja1105_clocking_setup(priv); 10048aa9ebccSVladimir Oltean if (rc < 0) { 10058aa9ebccSVladimir Oltean dev_err(ds->dev, "Failed to configure MII clocking: %d\n", rc); 10068aa9ebccSVladimir Oltean return rc; 10078aa9ebccSVladimir Oltean } 10088aa9ebccSVladimir Oltean 10098aa9ebccSVladimir Oltean return 0; 10108aa9ebccSVladimir Oltean } 10118aa9ebccSVladimir Oltean 10128aa9ebccSVladimir Oltean static const struct dsa_switch_ops sja1105_switch_ops = { 10138aa9ebccSVladimir Oltean .get_tag_protocol = sja1105_get_tag_protocol, 10148aa9ebccSVladimir Oltean .setup = sja1105_setup, 10158aa9ebccSVladimir Oltean .adjust_link = sja1105_adjust_link, 1016291d1e72SVladimir Oltean .port_fdb_dump = sja1105_fdb_dump, 1017291d1e72SVladimir Oltean .port_fdb_add = sja1105_fdb_add, 1018291d1e72SVladimir Oltean .port_fdb_del = sja1105_fdb_del, 10198aa9ebccSVladimir Oltean .port_bridge_join = sja1105_bridge_join, 10208aa9ebccSVladimir Oltean .port_bridge_leave = sja1105_bridge_leave, 1021291d1e72SVladimir Oltean .port_mdb_prepare = sja1105_mdb_prepare, 1022291d1e72SVladimir Oltean .port_mdb_add = sja1105_mdb_add, 1023291d1e72SVladimir Oltean .port_mdb_del = sja1105_mdb_del, 10248aa9ebccSVladimir Oltean }; 10258aa9ebccSVladimir Oltean 10268aa9ebccSVladimir Oltean static int sja1105_check_device_id(struct sja1105_private *priv) 10278aa9ebccSVladimir Oltean { 10288aa9ebccSVladimir Oltean const struct sja1105_regs *regs = priv->info->regs; 10298aa9ebccSVladimir Oltean u8 prod_id[SJA1105_SIZE_DEVICE_ID] = {0}; 10308aa9ebccSVladimir Oltean struct device *dev = &priv->spidev->dev; 10318aa9ebccSVladimir Oltean u64 device_id; 10328aa9ebccSVladimir Oltean u64 part_no; 10338aa9ebccSVladimir Oltean int rc; 10348aa9ebccSVladimir Oltean 10358aa9ebccSVladimir Oltean rc = sja1105_spi_send_int(priv, SPI_READ, regs->device_id, 10368aa9ebccSVladimir Oltean &device_id, SJA1105_SIZE_DEVICE_ID); 10378aa9ebccSVladimir Oltean if (rc < 0) 10388aa9ebccSVladimir Oltean return rc; 10398aa9ebccSVladimir Oltean 10408aa9ebccSVladimir Oltean if (device_id != priv->info->device_id) { 10418aa9ebccSVladimir Oltean dev_err(dev, "Expected device ID 0x%llx but read 0x%llx\n", 10428aa9ebccSVladimir Oltean priv->info->device_id, device_id); 10438aa9ebccSVladimir Oltean return -ENODEV; 10448aa9ebccSVladimir Oltean } 10458aa9ebccSVladimir Oltean 10468aa9ebccSVladimir Oltean rc = sja1105_spi_send_packed_buf(priv, SPI_READ, regs->prod_id, 10478aa9ebccSVladimir Oltean prod_id, SJA1105_SIZE_DEVICE_ID); 10488aa9ebccSVladimir Oltean if (rc < 0) 10498aa9ebccSVladimir Oltean return rc; 10508aa9ebccSVladimir Oltean 10518aa9ebccSVladimir Oltean sja1105_unpack(prod_id, &part_no, 19, 4, SJA1105_SIZE_DEVICE_ID); 10528aa9ebccSVladimir Oltean 10538aa9ebccSVladimir Oltean if (part_no != priv->info->part_no) { 10548aa9ebccSVladimir Oltean dev_err(dev, "Expected part number 0x%llx but read 0x%llx\n", 10558aa9ebccSVladimir Oltean priv->info->part_no, part_no); 10568aa9ebccSVladimir Oltean return -ENODEV; 10578aa9ebccSVladimir Oltean } 10588aa9ebccSVladimir Oltean 10598aa9ebccSVladimir Oltean return 0; 10608aa9ebccSVladimir Oltean } 10618aa9ebccSVladimir Oltean 10628aa9ebccSVladimir Oltean static int sja1105_probe(struct spi_device *spi) 10638aa9ebccSVladimir Oltean { 10648aa9ebccSVladimir Oltean struct device *dev = &spi->dev; 10658aa9ebccSVladimir Oltean struct sja1105_private *priv; 10668aa9ebccSVladimir Oltean struct dsa_switch *ds; 10678aa9ebccSVladimir Oltean int rc; 10688aa9ebccSVladimir Oltean 10698aa9ebccSVladimir Oltean if (!dev->of_node) { 10708aa9ebccSVladimir Oltean dev_err(dev, "No DTS bindings for SJA1105 driver\n"); 10718aa9ebccSVladimir Oltean return -EINVAL; 10728aa9ebccSVladimir Oltean } 10738aa9ebccSVladimir Oltean 10748aa9ebccSVladimir Oltean priv = devm_kzalloc(dev, sizeof(struct sja1105_private), GFP_KERNEL); 10758aa9ebccSVladimir Oltean if (!priv) 10768aa9ebccSVladimir Oltean return -ENOMEM; 10778aa9ebccSVladimir Oltean 10788aa9ebccSVladimir Oltean /* Configure the optional reset pin and bring up switch */ 10798aa9ebccSVladimir Oltean priv->reset_gpio = devm_gpiod_get(dev, "reset", GPIOD_OUT_HIGH); 10808aa9ebccSVladimir Oltean if (IS_ERR(priv->reset_gpio)) 10818aa9ebccSVladimir Oltean dev_dbg(dev, "reset-gpios not defined, ignoring\n"); 10828aa9ebccSVladimir Oltean else 10838aa9ebccSVladimir Oltean sja1105_hw_reset(priv->reset_gpio, 1, 1); 10848aa9ebccSVladimir Oltean 10858aa9ebccSVladimir Oltean /* Populate our driver private structure (priv) based on 10868aa9ebccSVladimir Oltean * the device tree node that was probed (spi) 10878aa9ebccSVladimir Oltean */ 10888aa9ebccSVladimir Oltean priv->spidev = spi; 10898aa9ebccSVladimir Oltean spi_set_drvdata(spi, priv); 10908aa9ebccSVladimir Oltean 10918aa9ebccSVladimir Oltean /* Configure the SPI bus */ 10928aa9ebccSVladimir Oltean spi->bits_per_word = 8; 10938aa9ebccSVladimir Oltean rc = spi_setup(spi); 10948aa9ebccSVladimir Oltean if (rc < 0) { 10958aa9ebccSVladimir Oltean dev_err(dev, "Could not init SPI\n"); 10968aa9ebccSVladimir Oltean return rc; 10978aa9ebccSVladimir Oltean } 10988aa9ebccSVladimir Oltean 10998aa9ebccSVladimir Oltean priv->info = of_device_get_match_data(dev); 11008aa9ebccSVladimir Oltean 11018aa9ebccSVladimir Oltean /* Detect hardware device */ 11028aa9ebccSVladimir Oltean rc = sja1105_check_device_id(priv); 11038aa9ebccSVladimir Oltean if (rc < 0) { 11048aa9ebccSVladimir Oltean dev_err(dev, "Device ID check failed: %d\n", rc); 11058aa9ebccSVladimir Oltean return rc; 11068aa9ebccSVladimir Oltean } 11078aa9ebccSVladimir Oltean 11088aa9ebccSVladimir Oltean dev_info(dev, "Probed switch chip: %s\n", priv->info->name); 11098aa9ebccSVladimir Oltean 11108aa9ebccSVladimir Oltean ds = dsa_switch_alloc(dev, SJA1105_NUM_PORTS); 11118aa9ebccSVladimir Oltean if (!ds) 11128aa9ebccSVladimir Oltean return -ENOMEM; 11138aa9ebccSVladimir Oltean 11148aa9ebccSVladimir Oltean ds->ops = &sja1105_switch_ops; 11158aa9ebccSVladimir Oltean ds->priv = priv; 11168aa9ebccSVladimir Oltean priv->ds = ds; 11178aa9ebccSVladimir Oltean 11188aa9ebccSVladimir Oltean return dsa_register_switch(priv->ds); 11198aa9ebccSVladimir Oltean } 11208aa9ebccSVladimir Oltean 11218aa9ebccSVladimir Oltean static int sja1105_remove(struct spi_device *spi) 11228aa9ebccSVladimir Oltean { 11238aa9ebccSVladimir Oltean struct sja1105_private *priv = spi_get_drvdata(spi); 11248aa9ebccSVladimir Oltean 11258aa9ebccSVladimir Oltean dsa_unregister_switch(priv->ds); 11268aa9ebccSVladimir Oltean sja1105_static_config_free(&priv->static_config); 11278aa9ebccSVladimir Oltean return 0; 11288aa9ebccSVladimir Oltean } 11298aa9ebccSVladimir Oltean 11308aa9ebccSVladimir Oltean static const struct of_device_id sja1105_dt_ids[] = { 11318aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105e", .data = &sja1105e_info }, 11328aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105t", .data = &sja1105t_info }, 11338aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105p", .data = &sja1105p_info }, 11348aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105q", .data = &sja1105q_info }, 11358aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105r", .data = &sja1105r_info }, 11368aa9ebccSVladimir Oltean { .compatible = "nxp,sja1105s", .data = &sja1105s_info }, 11378aa9ebccSVladimir Oltean { /* sentinel */ }, 11388aa9ebccSVladimir Oltean }; 11398aa9ebccSVladimir Oltean MODULE_DEVICE_TABLE(of, sja1105_dt_ids); 11408aa9ebccSVladimir Oltean 11418aa9ebccSVladimir Oltean static struct spi_driver sja1105_driver = { 11428aa9ebccSVladimir Oltean .driver = { 11438aa9ebccSVladimir Oltean .name = "sja1105", 11448aa9ebccSVladimir Oltean .owner = THIS_MODULE, 11458aa9ebccSVladimir Oltean .of_match_table = of_match_ptr(sja1105_dt_ids), 11468aa9ebccSVladimir Oltean }, 11478aa9ebccSVladimir Oltean .probe = sja1105_probe, 11488aa9ebccSVladimir Oltean .remove = sja1105_remove, 11498aa9ebccSVladimir Oltean }; 11508aa9ebccSVladimir Oltean 11518aa9ebccSVladimir Oltean module_spi_driver(sja1105_driver); 11528aa9ebccSVladimir Oltean 11538aa9ebccSVladimir Oltean MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 11548aa9ebccSVladimir Oltean MODULE_AUTHOR("Georg Waibel <georg.waibel@sensor-technik.de>"); 11558aa9ebccSVladimir Oltean MODULE_DESCRIPTION("SJA1105 Driver"); 11568aa9ebccSVladimir Oltean MODULE_LICENSE("GPL v2"); 1157