1e66f840cSTristram Ha // SPDX-License-Identifier: GPL-2.0 2e66f840cSTristram Ha /* 3e66f840cSTristram Ha * Microchip KSZ8795 switch driver 4e66f840cSTristram Ha * 5e66f840cSTristram Ha * Copyright (C) 2017 Microchip Technology Inc. 6e66f840cSTristram Ha * Tristram Ha <Tristram.Ha@microchip.com> 7e66f840cSTristram Ha */ 8e66f840cSTristram Ha 936838050SOleksij Rempel #include <linux/bitfield.h> 10e66f840cSTristram Ha #include <linux/delay.h> 11e66f840cSTristram Ha #include <linux/export.h> 12e66f840cSTristram Ha #include <linux/gpio.h> 13e66f840cSTristram Ha #include <linux/kernel.h> 14e66f840cSTristram Ha #include <linux/module.h> 15e66f840cSTristram Ha #include <linux/platform_data/microchip-ksz.h> 16e66f840cSTristram Ha #include <linux/phy.h> 17e66f840cSTristram Ha #include <linux/etherdevice.h> 18e66f840cSTristram Ha #include <linux/if_bridge.h> 19ec4b94f9SMichael Grzeschik #include <linux/micrel_phy.h> 20e66f840cSTristram Ha #include <net/dsa.h> 21e66f840cSTristram Ha #include <net/switchdev.h> 222c709e0bSMichael Grzeschik #include <linux/phylink.h> 23e66f840cSTristram Ha 24e66f840cSTristram Ha #include "ksz_common.h" 25e66f840cSTristram Ha #include "ksz8795_reg.h" 269f73e112SMichael Grzeschik #include "ksz8.h" 279f73e112SMichael Grzeschik 289f73e112SMichael Grzeschik static const u8 ksz8795_regs[] = { 299f73e112SMichael Grzeschik [REG_IND_CTRL_0] = 0x6E, 309f73e112SMichael Grzeschik [REG_IND_DATA_8] = 0x70, 319f73e112SMichael Grzeschik [REG_IND_DATA_CHECK] = 0x72, 329f73e112SMichael Grzeschik [REG_IND_DATA_HI] = 0x71, 339f73e112SMichael Grzeschik [REG_IND_DATA_LO] = 0x75, 349f73e112SMichael Grzeschik [REG_IND_MIB_CHECK] = 0x74, 359f73e112SMichael Grzeschik [P_FORCE_CTRL] = 0x0C, 369f73e112SMichael Grzeschik [P_LINK_STATUS] = 0x0E, 379f73e112SMichael Grzeschik [P_LOCAL_CTRL] = 0x07, 389f73e112SMichael Grzeschik [P_NEG_RESTART_CTRL] = 0x0D, 399f73e112SMichael Grzeschik [P_REMOTE_STATUS] = 0x08, 409f73e112SMichael Grzeschik [P_SPEED_STATUS] = 0x09, 419f73e112SMichael Grzeschik [S_TAIL_TAG_CTRL] = 0x0C, 429f73e112SMichael Grzeschik }; 439f73e112SMichael Grzeschik 449f73e112SMichael Grzeschik static const u32 ksz8795_masks[] = { 459f73e112SMichael Grzeschik [PORT_802_1P_REMAPPING] = BIT(7), 469f73e112SMichael Grzeschik [SW_TAIL_TAG_ENABLE] = BIT(1), 479f73e112SMichael Grzeschik [MIB_COUNTER_OVERFLOW] = BIT(6), 489f73e112SMichael Grzeschik [MIB_COUNTER_VALID] = BIT(5), 499f73e112SMichael Grzeschik [VLAN_TABLE_FID] = GENMASK(6, 0), 509f73e112SMichael Grzeschik [VLAN_TABLE_MEMBERSHIP] = GENMASK(11, 7), 519f73e112SMichael Grzeschik [VLAN_TABLE_VALID] = BIT(12), 529f73e112SMichael Grzeschik [STATIC_MAC_TABLE_VALID] = BIT(21), 539f73e112SMichael Grzeschik [STATIC_MAC_TABLE_USE_FID] = BIT(23), 549f73e112SMichael Grzeschik [STATIC_MAC_TABLE_FID] = GENMASK(30, 24), 559f73e112SMichael Grzeschik [STATIC_MAC_TABLE_OVERRIDE] = BIT(26), 569f73e112SMichael Grzeschik [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(24, 20), 579f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(6, 0), 589f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(8), 599f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7), 609f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 29), 619f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_FID] = GENMASK(26, 20), 629f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(26, 24), 639f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(28, 27), 649f73e112SMichael Grzeschik }; 659f73e112SMichael Grzeschik 669f73e112SMichael Grzeschik static const u8 ksz8795_shifts[] = { 679f73e112SMichael Grzeschik [VLAN_TABLE_MEMBERSHIP_S] = 7, 689f73e112SMichael Grzeschik [VLAN_TABLE] = 16, 699f73e112SMichael Grzeschik [STATIC_MAC_FWD_PORTS] = 16, 709f73e112SMichael Grzeschik [STATIC_MAC_FID] = 24, 719f73e112SMichael Grzeschik [DYNAMIC_MAC_ENTRIES_H] = 3, 729f73e112SMichael Grzeschik [DYNAMIC_MAC_ENTRIES] = 29, 739f73e112SMichael Grzeschik [DYNAMIC_MAC_FID] = 16, 749f73e112SMichael Grzeschik [DYNAMIC_MAC_TIMESTAMP] = 27, 759f73e112SMichael Grzeschik [DYNAMIC_MAC_SRC_PORT] = 24, 769f73e112SMichael Grzeschik }; 77e66f840cSTristram Ha 784b20a07eSOleksij Rempel static const u8 ksz8863_regs[] = { 794b20a07eSOleksij Rempel [REG_IND_CTRL_0] = 0x79, 804b20a07eSOleksij Rempel [REG_IND_DATA_8] = 0x7B, 814b20a07eSOleksij Rempel [REG_IND_DATA_CHECK] = 0x7B, 824b20a07eSOleksij Rempel [REG_IND_DATA_HI] = 0x7C, 834b20a07eSOleksij Rempel [REG_IND_DATA_LO] = 0x80, 844b20a07eSOleksij Rempel [REG_IND_MIB_CHECK] = 0x80, 854b20a07eSOleksij Rempel [P_FORCE_CTRL] = 0x0C, 864b20a07eSOleksij Rempel [P_LINK_STATUS] = 0x0E, 874b20a07eSOleksij Rempel [P_LOCAL_CTRL] = 0x0C, 884b20a07eSOleksij Rempel [P_NEG_RESTART_CTRL] = 0x0D, 894b20a07eSOleksij Rempel [P_REMOTE_STATUS] = 0x0E, 904b20a07eSOleksij Rempel [P_SPEED_STATUS] = 0x0F, 914b20a07eSOleksij Rempel [S_TAIL_TAG_CTRL] = 0x03, 924b20a07eSOleksij Rempel }; 934b20a07eSOleksij Rempel 944b20a07eSOleksij Rempel static const u32 ksz8863_masks[] = { 954b20a07eSOleksij Rempel [PORT_802_1P_REMAPPING] = BIT(3), 964b20a07eSOleksij Rempel [SW_TAIL_TAG_ENABLE] = BIT(6), 974b20a07eSOleksij Rempel [MIB_COUNTER_OVERFLOW] = BIT(7), 984b20a07eSOleksij Rempel [MIB_COUNTER_VALID] = BIT(6), 994b20a07eSOleksij Rempel [VLAN_TABLE_FID] = GENMASK(15, 12), 1004b20a07eSOleksij Rempel [VLAN_TABLE_MEMBERSHIP] = GENMASK(18, 16), 1014b20a07eSOleksij Rempel [VLAN_TABLE_VALID] = BIT(19), 1024b20a07eSOleksij Rempel [STATIC_MAC_TABLE_VALID] = BIT(19), 1034b20a07eSOleksij Rempel [STATIC_MAC_TABLE_USE_FID] = BIT(21), 1044b20a07eSOleksij Rempel [STATIC_MAC_TABLE_FID] = GENMASK(29, 26), 1054b20a07eSOleksij Rempel [STATIC_MAC_TABLE_OVERRIDE] = BIT(20), 1064b20a07eSOleksij Rempel [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(18, 16), 1074b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(5, 0), 1084b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(7), 1094b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7), 1104b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 28), 1114b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_FID] = GENMASK(19, 16), 1124b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(21, 20), 1134b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(23, 22), 1144b20a07eSOleksij Rempel }; 1154b20a07eSOleksij Rempel 1164b20a07eSOleksij Rempel static u8 ksz8863_shifts[] = { 1174b20a07eSOleksij Rempel [VLAN_TABLE_MEMBERSHIP_S] = 16, 1184b20a07eSOleksij Rempel [STATIC_MAC_FWD_PORTS] = 16, 1194b20a07eSOleksij Rempel [STATIC_MAC_FID] = 22, 1204b20a07eSOleksij Rempel [DYNAMIC_MAC_ENTRIES_H] = 3, 1214b20a07eSOleksij Rempel [DYNAMIC_MAC_ENTRIES] = 24, 1224b20a07eSOleksij Rempel [DYNAMIC_MAC_FID] = 16, 1234b20a07eSOleksij Rempel [DYNAMIC_MAC_TIMESTAMP] = 24, 1244b20a07eSOleksij Rempel [DYNAMIC_MAC_SRC_PORT] = 20, 1254b20a07eSOleksij Rempel }; 1264b20a07eSOleksij Rempel 1274b20a07eSOleksij Rempel struct mib_names { 128e66f840cSTristram Ha char string[ETH_GSTRING_LEN]; 1294b20a07eSOleksij Rempel }; 1304b20a07eSOleksij Rempel 1314b20a07eSOleksij Rempel static const struct mib_names ksz87xx_mib_names[] = { 132e66f840cSTristram Ha { "rx_hi" }, 133e66f840cSTristram Ha { "rx_undersize" }, 134e66f840cSTristram Ha { "rx_fragments" }, 135e66f840cSTristram Ha { "rx_oversize" }, 136e66f840cSTristram Ha { "rx_jabbers" }, 137e66f840cSTristram Ha { "rx_symbol_err" }, 138e66f840cSTristram Ha { "rx_crc_err" }, 139e66f840cSTristram Ha { "rx_align_err" }, 140e66f840cSTristram Ha { "rx_mac_ctrl" }, 141e66f840cSTristram Ha { "rx_pause" }, 142e66f840cSTristram Ha { "rx_bcast" }, 143e66f840cSTristram Ha { "rx_mcast" }, 144e66f840cSTristram Ha { "rx_ucast" }, 145e66f840cSTristram Ha { "rx_64_or_less" }, 146e66f840cSTristram Ha { "rx_65_127" }, 147e66f840cSTristram Ha { "rx_128_255" }, 148e66f840cSTristram Ha { "rx_256_511" }, 149e66f840cSTristram Ha { "rx_512_1023" }, 150e66f840cSTristram Ha { "rx_1024_1522" }, 151e66f840cSTristram Ha { "rx_1523_2000" }, 152e66f840cSTristram Ha { "rx_2001" }, 153e66f840cSTristram Ha { "tx_hi" }, 154e66f840cSTristram Ha { "tx_late_col" }, 155e66f840cSTristram Ha { "tx_pause" }, 156e66f840cSTristram Ha { "tx_bcast" }, 157e66f840cSTristram Ha { "tx_mcast" }, 158e66f840cSTristram Ha { "tx_ucast" }, 159e66f840cSTristram Ha { "tx_deferred" }, 160e66f840cSTristram Ha { "tx_total_col" }, 161e66f840cSTristram Ha { "tx_exc_col" }, 162e66f840cSTristram Ha { "tx_single_col" }, 163e66f840cSTristram Ha { "tx_mult_col" }, 164e66f840cSTristram Ha { "rx_total" }, 165e66f840cSTristram Ha { "tx_total" }, 166e66f840cSTristram Ha { "rx_discards" }, 167e66f840cSTristram Ha { "tx_discards" }, 168e66f840cSTristram Ha }; 169e66f840cSTristram Ha 1704b20a07eSOleksij Rempel static const struct mib_names ksz88xx_mib_names[] = { 1714b20a07eSOleksij Rempel { "rx" }, 1724b20a07eSOleksij Rempel { "rx_hi" }, 1734b20a07eSOleksij Rempel { "rx_undersize" }, 1744b20a07eSOleksij Rempel { "rx_fragments" }, 1754b20a07eSOleksij Rempel { "rx_oversize" }, 1764b20a07eSOleksij Rempel { "rx_jabbers" }, 1774b20a07eSOleksij Rempel { "rx_symbol_err" }, 1784b20a07eSOleksij Rempel { "rx_crc_err" }, 1794b20a07eSOleksij Rempel { "rx_align_err" }, 1804b20a07eSOleksij Rempel { "rx_mac_ctrl" }, 1814b20a07eSOleksij Rempel { "rx_pause" }, 1824b20a07eSOleksij Rempel { "rx_bcast" }, 1834b20a07eSOleksij Rempel { "rx_mcast" }, 1844b20a07eSOleksij Rempel { "rx_ucast" }, 1854b20a07eSOleksij Rempel { "rx_64_or_less" }, 1864b20a07eSOleksij Rempel { "rx_65_127" }, 1874b20a07eSOleksij Rempel { "rx_128_255" }, 1884b20a07eSOleksij Rempel { "rx_256_511" }, 1894b20a07eSOleksij Rempel { "rx_512_1023" }, 1904b20a07eSOleksij Rempel { "rx_1024_1522" }, 1914b20a07eSOleksij Rempel { "tx" }, 1924b20a07eSOleksij Rempel { "tx_hi" }, 1934b20a07eSOleksij Rempel { "tx_late_col" }, 1944b20a07eSOleksij Rempel { "tx_pause" }, 1954b20a07eSOleksij Rempel { "tx_bcast" }, 1964b20a07eSOleksij Rempel { "tx_mcast" }, 1974b20a07eSOleksij Rempel { "tx_ucast" }, 1984b20a07eSOleksij Rempel { "tx_deferred" }, 1994b20a07eSOleksij Rempel { "tx_total_col" }, 2004b20a07eSOleksij Rempel { "tx_exc_col" }, 2014b20a07eSOleksij Rempel { "tx_single_col" }, 2024b20a07eSOleksij Rempel { "tx_mult_col" }, 2034b20a07eSOleksij Rempel { "rx_discards" }, 2044b20a07eSOleksij Rempel { "tx_discards" }, 2054b20a07eSOleksij Rempel }; 2064b20a07eSOleksij Rempel 2074b20a07eSOleksij Rempel static bool ksz_is_ksz88x3(struct ksz_device *dev) 2084b20a07eSOleksij Rempel { 2094b20a07eSOleksij Rempel return dev->chip_id == 0x8830; 2104b20a07eSOleksij Rempel } 2114b20a07eSOleksij Rempel 212e66f840cSTristram Ha static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set) 213e66f840cSTristram Ha { 214e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0); 215e66f840cSTristram Ha } 216e66f840cSTristram Ha 217e66f840cSTristram Ha static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits, 218e66f840cSTristram Ha bool set) 219e66f840cSTristram Ha { 220e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset), 221e66f840cSTristram Ha bits, set ? bits : 0); 222e66f840cSTristram Ha } 223e66f840cSTristram Ha 2244b5baca0SMichael Grzeschik static int ksz8_reset_switch(struct ksz_device *dev) 225e66f840cSTristram Ha { 2264b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 2274b20a07eSOleksij Rempel /* reset switch */ 2284b20a07eSOleksij Rempel ksz_cfg(dev, KSZ8863_REG_SW_RESET, 2294b20a07eSOleksij Rempel KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, true); 2304b20a07eSOleksij Rempel ksz_cfg(dev, KSZ8863_REG_SW_RESET, 2314b20a07eSOleksij Rempel KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, false); 2324b20a07eSOleksij Rempel } else { 233e66f840cSTristram Ha /* reset switch */ 234e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 235e66f840cSTristram Ha SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S); 236e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0); 2374b20a07eSOleksij Rempel } 238e66f840cSTristram Ha 239e66f840cSTristram Ha return 0; 240e66f840cSTristram Ha } 241e66f840cSTristram Ha 242e66f840cSTristram Ha static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue) 243e66f840cSTristram Ha { 244e66f840cSTristram Ha u8 hi, lo; 245e66f840cSTristram Ha 246e66f840cSTristram Ha /* Number of queues can only be 1, 2, or 4. */ 247e66f840cSTristram Ha switch (queue) { 248e66f840cSTristram Ha case 4: 249e66f840cSTristram Ha case 3: 250e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_4; 251e66f840cSTristram Ha break; 252e66f840cSTristram Ha case 2: 253e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_2; 254e66f840cSTristram Ha break; 255e66f840cSTristram Ha default: 256e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_1; 257e66f840cSTristram Ha } 258e66f840cSTristram Ha ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo); 259e66f840cSTristram Ha ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi); 260e66f840cSTristram Ha lo &= ~PORT_QUEUE_SPLIT_L; 261e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_2) 262e66f840cSTristram Ha lo |= PORT_QUEUE_SPLIT_L; 263e66f840cSTristram Ha hi &= ~PORT_QUEUE_SPLIT_H; 264e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_4) 265e66f840cSTristram Ha hi |= PORT_QUEUE_SPLIT_H; 266e66f840cSTristram Ha ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo); 267e66f840cSTristram Ha ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi); 268e66f840cSTristram Ha 269e66f840cSTristram Ha /* Default is port based for egress rate limit. */ 270e66f840cSTristram Ha if (queue != PORT_QUEUE_SPLIT_1) 271e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED, 272e66f840cSTristram Ha true); 273e66f840cSTristram Ha } 274e66f840cSTristram Ha 2754b5baca0SMichael Grzeschik static void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt) 276e66f840cSTristram Ha { 2779f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 2789f73e112SMichael Grzeschik const u32 *masks; 2799f73e112SMichael Grzeschik const u8 *regs; 280e66f840cSTristram Ha u16 ctrl_addr; 281e66f840cSTristram Ha u32 data; 282e66f840cSTristram Ha u8 check; 283e66f840cSTristram Ha int loop; 284e66f840cSTristram Ha 2859f73e112SMichael Grzeschik masks = ksz8->masks; 2869f73e112SMichael Grzeschik regs = ksz8->regs; 2879f73e112SMichael Grzeschik 28831b62c78SMichael Grzeschik ctrl_addr = addr + dev->reg_mib_cnt * port; 289e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 290e66f840cSTristram Ha 291e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 2929f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 293e66f840cSTristram Ha 294e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 295e66f840cSTristram Ha * slow SPI speed. 296e66f840cSTristram Ha */ 297e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 2989f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check); 299e66f840cSTristram Ha 3009f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_VALID]) { 3019f73e112SMichael Grzeschik ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 3029f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) 303e66f840cSTristram Ha *cnt += MIB_COUNTER_VALUE + 1; 304e66f840cSTristram Ha *cnt += data & MIB_COUNTER_VALUE; 305e66f840cSTristram Ha break; 306e66f840cSTristram Ha } 307e66f840cSTristram Ha } 308e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 309e66f840cSTristram Ha } 310e66f840cSTristram Ha 3114b20a07eSOleksij Rempel static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 312e66f840cSTristram Ha u64 *dropped, u64 *cnt) 313e66f840cSTristram Ha { 3149f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 3159f73e112SMichael Grzeschik const u32 *masks; 3169f73e112SMichael Grzeschik const u8 *regs; 317e66f840cSTristram Ha u16 ctrl_addr; 318e66f840cSTristram Ha u32 data; 319e66f840cSTristram Ha u8 check; 320e66f840cSTristram Ha int loop; 321e66f840cSTristram Ha 3229f73e112SMichael Grzeschik masks = ksz8->masks; 3239f73e112SMichael Grzeschik regs = ksz8->regs; 3249f73e112SMichael Grzeschik 32531b62c78SMichael Grzeschik addr -= dev->reg_mib_cnt; 3264b20a07eSOleksij Rempel ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port; 3274b20a07eSOleksij Rempel ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0; 328e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 329e66f840cSTristram Ha 330e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 3319f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 332e66f840cSTristram Ha 333e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 334e66f840cSTristram Ha * slow SPI speed. 335e66f840cSTristram Ha */ 336e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 3379f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check); 338e66f840cSTristram Ha 3399f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_VALID]) { 3409f73e112SMichael Grzeschik ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 341e66f840cSTristram Ha if (addr < 2) { 342e66f840cSTristram Ha u64 total; 343e66f840cSTristram Ha 344e66f840cSTristram Ha total = check & MIB_TOTAL_BYTES_H; 345e66f840cSTristram Ha total <<= 32; 346e66f840cSTristram Ha *cnt += total; 347e66f840cSTristram Ha *cnt += data; 3489f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) { 349e66f840cSTristram Ha total = MIB_TOTAL_BYTES_H + 1; 350e66f840cSTristram Ha total <<= 32; 351e66f840cSTristram Ha *cnt += total; 352e66f840cSTristram Ha } 353e66f840cSTristram Ha } else { 3549f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) 355e66f840cSTristram Ha *cnt += MIB_PACKET_DROPPED + 1; 356e66f840cSTristram Ha *cnt += data & MIB_PACKET_DROPPED; 357e66f840cSTristram Ha } 358e66f840cSTristram Ha break; 359e66f840cSTristram Ha } 360e66f840cSTristram Ha } 361e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 362e66f840cSTristram Ha } 363e66f840cSTristram Ha 3644b20a07eSOleksij Rempel static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 3654b20a07eSOleksij Rempel u64 *dropped, u64 *cnt) 3664b20a07eSOleksij Rempel { 3674b20a07eSOleksij Rempel struct ksz8 *ksz8 = dev->priv; 3684b20a07eSOleksij Rempel const u8 *regs = ksz8->regs; 3694b20a07eSOleksij Rempel u32 *last = (u32 *)dropped; 3704b20a07eSOleksij Rempel u16 ctrl_addr; 3714b20a07eSOleksij Rempel u32 data; 3724b20a07eSOleksij Rempel u32 cur; 3734b20a07eSOleksij Rempel 3744b20a07eSOleksij Rempel addr -= dev->reg_mib_cnt; 3754b20a07eSOleksij Rempel ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 : 3764b20a07eSOleksij Rempel KSZ8863_MIB_PACKET_DROPPED_RX_0; 3774b20a07eSOleksij Rempel ctrl_addr += port; 3784b20a07eSOleksij Rempel ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 3794b20a07eSOleksij Rempel 3804b20a07eSOleksij Rempel mutex_lock(&dev->alu_mutex); 3814b20a07eSOleksij Rempel ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 3824b20a07eSOleksij Rempel ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 3834b20a07eSOleksij Rempel mutex_unlock(&dev->alu_mutex); 3844b20a07eSOleksij Rempel 3854b20a07eSOleksij Rempel data &= MIB_PACKET_DROPPED; 3864b20a07eSOleksij Rempel cur = last[addr]; 3874b20a07eSOleksij Rempel if (data != cur) { 3884b20a07eSOleksij Rempel last[addr] = data; 3894b20a07eSOleksij Rempel if (data < cur) 3904b20a07eSOleksij Rempel data += MIB_PACKET_DROPPED + 1; 3914b20a07eSOleksij Rempel data -= cur; 3924b20a07eSOleksij Rempel *cnt += data; 3934b20a07eSOleksij Rempel } 3944b20a07eSOleksij Rempel } 3954b20a07eSOleksij Rempel 3964b20a07eSOleksij Rempel static void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 3974b20a07eSOleksij Rempel u64 *dropped, u64 *cnt) 3984b20a07eSOleksij Rempel { 3994b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 4004b20a07eSOleksij Rempel ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt); 4014b20a07eSOleksij Rempel else 4024b20a07eSOleksij Rempel ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt); 4034b20a07eSOleksij Rempel } 4044b20a07eSOleksij Rempel 4054b5baca0SMichael Grzeschik static void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze) 406e66f840cSTristram Ha { 4074b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 4084b20a07eSOleksij Rempel return; 4094b20a07eSOleksij Rempel 410e66f840cSTristram Ha /* enable the port for flush/freeze function */ 411e66f840cSTristram Ha if (freeze) 412e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 413e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze); 414e66f840cSTristram Ha 415e66f840cSTristram Ha /* disable the port after freeze is done */ 416e66f840cSTristram Ha if (!freeze) 417e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 418e66f840cSTristram Ha } 419e66f840cSTristram Ha 4204b5baca0SMichael Grzeschik static void ksz8_port_init_cnt(struct ksz_device *dev, int port) 421e66f840cSTristram Ha { 422e66f840cSTristram Ha struct ksz_port_mib *mib = &dev->ports[port].mib; 4234b20a07eSOleksij Rempel u64 *dropped; 424e66f840cSTristram Ha 4254b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) { 426e66f840cSTristram Ha /* flush all enabled port MIB counters */ 427e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 428e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true); 429e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 4304b20a07eSOleksij Rempel } 431e66f840cSTristram Ha 432e66f840cSTristram Ha mib->cnt_ptr = 0; 433e66f840cSTristram Ha 434e66f840cSTristram Ha /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */ 435e66f840cSTristram Ha while (mib->cnt_ptr < dev->reg_mib_cnt) { 436e66f840cSTristram Ha dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr, 437e66f840cSTristram Ha &mib->counters[mib->cnt_ptr]); 438e66f840cSTristram Ha ++mib->cnt_ptr; 439e66f840cSTristram Ha } 440e66f840cSTristram Ha 4414b20a07eSOleksij Rempel /* last one in storage */ 4424b20a07eSOleksij Rempel dropped = &mib->counters[dev->mib_cnt]; 4434b20a07eSOleksij Rempel 444e66f840cSTristram Ha /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */ 445e66f840cSTristram Ha while (mib->cnt_ptr < dev->mib_cnt) { 446e66f840cSTristram Ha dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr, 4474b20a07eSOleksij Rempel dropped, &mib->counters[mib->cnt_ptr]); 448e66f840cSTristram Ha ++mib->cnt_ptr; 449e66f840cSTristram Ha } 450e66f840cSTristram Ha mib->cnt_ptr = 0; 451e66f840cSTristram Ha memset(mib->counters, 0, dev->mib_cnt * sizeof(u64)); 452e66f840cSTristram Ha } 453e66f840cSTristram Ha 4544b5baca0SMichael Grzeschik static void ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data) 455e66f840cSTristram Ha { 4569f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 4579f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 458e66f840cSTristram Ha u16 ctrl_addr; 459e66f840cSTristram Ha 460e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr; 461e66f840cSTristram Ha 462e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 4639f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 4649f73e112SMichael Grzeschik ksz_read64(dev, regs[REG_IND_DATA_HI], data); 465e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 466e66f840cSTristram Ha } 467e66f840cSTristram Ha 4684b5baca0SMichael Grzeschik static void ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data) 469e66f840cSTristram Ha { 4709f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 4719f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 472e66f840cSTristram Ha u16 ctrl_addr; 473e66f840cSTristram Ha 474e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table) | addr; 475e66f840cSTristram Ha 476e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 4779f73e112SMichael Grzeschik ksz_write64(dev, regs[REG_IND_DATA_HI], data); 4789f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 479e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 480e66f840cSTristram Ha } 481e66f840cSTristram Ha 4824b5baca0SMichael Grzeschik static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data) 483e66f840cSTristram Ha { 4849f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 485e66f840cSTristram Ha int timeout = 100; 4869f73e112SMichael Grzeschik const u32 *masks; 4879f73e112SMichael Grzeschik const u8 *regs; 4889f73e112SMichael Grzeschik 4899f73e112SMichael Grzeschik masks = ksz8->masks; 4909f73e112SMichael Grzeschik regs = ksz8->regs; 491e66f840cSTristram Ha 492e66f840cSTristram Ha do { 4939f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_DATA_CHECK], data); 494e66f840cSTristram Ha timeout--; 4959f73e112SMichael Grzeschik } while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout); 496e66f840cSTristram Ha 497e66f840cSTristram Ha /* Entry is not ready for accessing. */ 4989f73e112SMichael Grzeschik if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) { 499e66f840cSTristram Ha return -EAGAIN; 500e66f840cSTristram Ha /* Entry is ready for accessing. */ 501e66f840cSTristram Ha } else { 5029f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_DATA_8], data); 503e66f840cSTristram Ha 504e66f840cSTristram Ha /* There is no valid entry in the table. */ 5059f73e112SMichael Grzeschik if (*data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY]) 506e66f840cSTristram Ha return -ENXIO; 507e66f840cSTristram Ha } 508e66f840cSTristram Ha return 0; 509e66f840cSTristram Ha } 510e66f840cSTristram Ha 5114b5baca0SMichael Grzeschik static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, 512e66f840cSTristram Ha u8 *mac_addr, u8 *fid, u8 *src_port, 513e66f840cSTristram Ha u8 *timestamp, u16 *entries) 514e66f840cSTristram Ha { 5159f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 516e66f840cSTristram Ha u32 data_hi, data_lo; 5179f73e112SMichael Grzeschik const u8 *shifts; 5189f73e112SMichael Grzeschik const u32 *masks; 5199f73e112SMichael Grzeschik const u8 *regs; 520e66f840cSTristram Ha u16 ctrl_addr; 521e66f840cSTristram Ha u8 data; 522e66f840cSTristram Ha int rc; 523e66f840cSTristram Ha 5249f73e112SMichael Grzeschik shifts = ksz8->shifts; 5259f73e112SMichael Grzeschik masks = ksz8->masks; 5269f73e112SMichael Grzeschik regs = ksz8->regs; 5279f73e112SMichael Grzeschik 528e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr; 529e66f840cSTristram Ha 530e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 5319f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 532e66f840cSTristram Ha 5334b5baca0SMichael Grzeschik rc = ksz8_valid_dyn_entry(dev, &data); 534e66f840cSTristram Ha if (rc == -EAGAIN) { 535e66f840cSTristram Ha if (addr == 0) 536e66f840cSTristram Ha *entries = 0; 537e66f840cSTristram Ha } else if (rc == -ENXIO) { 538e66f840cSTristram Ha *entries = 0; 539e66f840cSTristram Ha /* At least one valid entry in the table. */ 540e66f840cSTristram Ha } else { 541e66f840cSTristram Ha u64 buf = 0; 542e66f840cSTristram Ha int cnt; 543e66f840cSTristram Ha 5449f73e112SMichael Grzeschik ksz_read64(dev, regs[REG_IND_DATA_HI], &buf); 545e66f840cSTristram Ha data_hi = (u32)(buf >> 32); 546e66f840cSTristram Ha data_lo = (u32)buf; 547e66f840cSTristram Ha 548e66f840cSTristram Ha /* Check out how many valid entry in the table. */ 5499f73e112SMichael Grzeschik cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H]; 5509f73e112SMichael Grzeschik cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H]; 5519f73e112SMichael Grzeschik cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >> 5529f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_ENTRIES]; 553e66f840cSTristram Ha *entries = cnt + 1; 554e66f840cSTristram Ha 5559f73e112SMichael Grzeschik *fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >> 5569f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_FID]; 5579f73e112SMichael Grzeschik *src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >> 5589f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_SRC_PORT]; 5599f73e112SMichael Grzeschik *timestamp = (data_hi & masks[DYNAMIC_MAC_TABLE_TIMESTAMP]) >> 5609f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_TIMESTAMP]; 561e66f840cSTristram Ha 562e66f840cSTristram Ha mac_addr[5] = (u8)data_lo; 563e66f840cSTristram Ha mac_addr[4] = (u8)(data_lo >> 8); 564e66f840cSTristram Ha mac_addr[3] = (u8)(data_lo >> 16); 565e66f840cSTristram Ha mac_addr[2] = (u8)(data_lo >> 24); 566e66f840cSTristram Ha 567e66f840cSTristram Ha mac_addr[1] = (u8)data_hi; 568e66f840cSTristram Ha mac_addr[0] = (u8)(data_hi >> 8); 569e66f840cSTristram Ha rc = 0; 570e66f840cSTristram Ha } 571e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 572e66f840cSTristram Ha 573e66f840cSTristram Ha return rc; 574e66f840cSTristram Ha } 575e66f840cSTristram Ha 5764b5baca0SMichael Grzeschik static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr, 577e66f840cSTristram Ha struct alu_struct *alu) 578e66f840cSTristram Ha { 5799f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 580e66f840cSTristram Ha u32 data_hi, data_lo; 5819f73e112SMichael Grzeschik const u8 *shifts; 5829f73e112SMichael Grzeschik const u32 *masks; 583e66f840cSTristram Ha u64 data; 584e66f840cSTristram Ha 5859f73e112SMichael Grzeschik shifts = ksz8->shifts; 5869f73e112SMichael Grzeschik masks = ksz8->masks; 5879f73e112SMichael Grzeschik 5884b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data); 589e66f840cSTristram Ha data_hi = data >> 32; 590e66f840cSTristram Ha data_lo = (u32)data; 5919f73e112SMichael Grzeschik if (data_hi & (masks[STATIC_MAC_TABLE_VALID] | 5929f73e112SMichael Grzeschik masks[STATIC_MAC_TABLE_OVERRIDE])) { 593e66f840cSTristram Ha alu->mac[5] = (u8)data_lo; 594e66f840cSTristram Ha alu->mac[4] = (u8)(data_lo >> 8); 595e66f840cSTristram Ha alu->mac[3] = (u8)(data_lo >> 16); 596e66f840cSTristram Ha alu->mac[2] = (u8)(data_lo >> 24); 597e66f840cSTristram Ha alu->mac[1] = (u8)data_hi; 598e66f840cSTristram Ha alu->mac[0] = (u8)(data_hi >> 8); 5999f73e112SMichael Grzeschik alu->port_forward = 6009f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >> 6019f73e112SMichael Grzeschik shifts[STATIC_MAC_FWD_PORTS]; 602e66f840cSTristram Ha alu->is_override = 6039f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0; 604e66f840cSTristram Ha data_hi >>= 1; 6059f73e112SMichael Grzeschik alu->is_static = true; 6069f73e112SMichael Grzeschik alu->is_use_fid = 6079f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0; 6089f73e112SMichael Grzeschik alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >> 6099f73e112SMichael Grzeschik shifts[STATIC_MAC_FID]; 610e66f840cSTristram Ha return 0; 611e66f840cSTristram Ha } 612e66f840cSTristram Ha return -ENXIO; 613e66f840cSTristram Ha } 614e66f840cSTristram Ha 6154b5baca0SMichael Grzeschik static void ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr, 616e66f840cSTristram Ha struct alu_struct *alu) 617e66f840cSTristram Ha { 6189f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 619e66f840cSTristram Ha u32 data_hi, data_lo; 6209f73e112SMichael Grzeschik const u8 *shifts; 6219f73e112SMichael Grzeschik const u32 *masks; 622e66f840cSTristram Ha u64 data; 623e66f840cSTristram Ha 6249f73e112SMichael Grzeschik shifts = ksz8->shifts; 6259f73e112SMichael Grzeschik masks = ksz8->masks; 6269f73e112SMichael Grzeschik 627e66f840cSTristram Ha data_lo = ((u32)alu->mac[2] << 24) | 628e66f840cSTristram Ha ((u32)alu->mac[3] << 16) | 629e66f840cSTristram Ha ((u32)alu->mac[4] << 8) | alu->mac[5]; 630e66f840cSTristram Ha data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1]; 6319f73e112SMichael Grzeschik data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS]; 632e66f840cSTristram Ha 633e66f840cSTristram Ha if (alu->is_override) 6349f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE]; 635e66f840cSTristram Ha if (alu->is_use_fid) { 6369f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_USE_FID]; 6379f73e112SMichael Grzeschik data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID]; 638e66f840cSTristram Ha } 639e66f840cSTristram Ha if (alu->is_static) 6409f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_VALID]; 641e66f840cSTristram Ha else 6429f73e112SMichael Grzeschik data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE]; 643e66f840cSTristram Ha 644e66f840cSTristram Ha data = (u64)data_hi << 32 | data_lo; 6454b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data); 646e66f840cSTristram Ha } 647e66f840cSTristram Ha 6489f73e112SMichael Grzeschik static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid, 6499f73e112SMichael Grzeschik u8 *member, u8 *valid) 650e66f840cSTristram Ha { 6519f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 6529f73e112SMichael Grzeschik const u8 *shifts; 6539f73e112SMichael Grzeschik const u32 *masks; 6549f73e112SMichael Grzeschik 6559f73e112SMichael Grzeschik shifts = ksz8->shifts; 6569f73e112SMichael Grzeschik masks = ksz8->masks; 6579f73e112SMichael Grzeschik 6589f73e112SMichael Grzeschik *fid = vlan & masks[VLAN_TABLE_FID]; 6599f73e112SMichael Grzeschik *member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >> 6609f73e112SMichael Grzeschik shifts[VLAN_TABLE_MEMBERSHIP_S]; 6619f73e112SMichael Grzeschik *valid = !!(vlan & masks[VLAN_TABLE_VALID]); 662e66f840cSTristram Ha } 663e66f840cSTristram Ha 6649f73e112SMichael Grzeschik static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid, 6659f73e112SMichael Grzeschik u16 *vlan) 666e66f840cSTristram Ha { 6679f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 6689f73e112SMichael Grzeschik const u8 *shifts; 6699f73e112SMichael Grzeschik const u32 *masks; 6709f73e112SMichael Grzeschik 6719f73e112SMichael Grzeschik shifts = ksz8->shifts; 6729f73e112SMichael Grzeschik masks = ksz8->masks; 6739f73e112SMichael Grzeschik 674e66f840cSTristram Ha *vlan = fid; 6759f73e112SMichael Grzeschik *vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S]; 676e66f840cSTristram Ha if (valid) 6779f73e112SMichael Grzeschik *vlan |= masks[VLAN_TABLE_VALID]; 678e66f840cSTristram Ha } 679e66f840cSTristram Ha 6804b5baca0SMichael Grzeschik static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr) 681e66f840cSTristram Ha { 6829f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 6839f73e112SMichael Grzeschik const u8 *shifts; 684e66f840cSTristram Ha u64 data; 685e66f840cSTristram Ha int i; 686e66f840cSTristram Ha 6879f73e112SMichael Grzeschik shifts = ksz8->shifts; 6889f73e112SMichael Grzeschik 6894b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &data); 690*411d466dSBen Hutchings addr *= 4; 691*411d466dSBen Hutchings for (i = 0; i < 4; i++) { 692e66f840cSTristram Ha dev->vlan_cache[addr + i].table[0] = (u16)data; 6939f73e112SMichael Grzeschik data >>= shifts[VLAN_TABLE]; 694e66f840cSTristram Ha } 695e66f840cSTristram Ha } 696e66f840cSTristram Ha 6974b5baca0SMichael Grzeschik static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan) 698e66f840cSTristram Ha { 699e66f840cSTristram Ha int index; 700e66f840cSTristram Ha u16 *data; 701e66f840cSTristram Ha u16 addr; 702e66f840cSTristram Ha u64 buf; 703e66f840cSTristram Ha 704e66f840cSTristram Ha data = (u16 *)&buf; 705*411d466dSBen Hutchings addr = vid / 4; 706e66f840cSTristram Ha index = vid & 3; 7074b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 708e66f840cSTristram Ha *vlan = data[index]; 709e66f840cSTristram Ha } 710e66f840cSTristram Ha 7114b5baca0SMichael Grzeschik static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan) 712e66f840cSTristram Ha { 713e66f840cSTristram Ha int index; 714e66f840cSTristram Ha u16 *data; 715e66f840cSTristram Ha u16 addr; 716e66f840cSTristram Ha u64 buf; 717e66f840cSTristram Ha 718e66f840cSTristram Ha data = (u16 *)&buf; 719*411d466dSBen Hutchings addr = vid / 4; 720e66f840cSTristram Ha index = vid & 3; 7214b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 722e66f840cSTristram Ha data[index] = vlan; 723e66f840cSTristram Ha dev->vlan_cache[vid].table[0] = vlan; 7244b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_VLAN, addr, buf); 725e66f840cSTristram Ha } 726e66f840cSTristram Ha 7274b5baca0SMichael Grzeschik static void ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val) 728e66f840cSTristram Ha { 7299f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 730e66f840cSTristram Ha u8 restart, speed, ctrl, link; 7319f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 732e66f840cSTristram Ha int processed = true; 73336838050SOleksij Rempel u8 val1, val2; 734e66f840cSTristram Ha u16 data = 0; 735e66f840cSTristram Ha u8 p = phy; 736e66f840cSTristram Ha 737e66f840cSTristram Ha switch (reg) { 738ec4b94f9SMichael Grzeschik case MII_BMCR: 7399f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart); 7409f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed); 7419f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl); 742e66f840cSTristram Ha if (restart & PORT_PHY_LOOPBACK) 743ec4b94f9SMichael Grzeschik data |= BMCR_LOOPBACK; 744e66f840cSTristram Ha if (ctrl & PORT_FORCE_100_MBIT) 745ec4b94f9SMichael Grzeschik data |= BMCR_SPEED100; 7464b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 7474b20a07eSOleksij Rempel if ((ctrl & PORT_AUTO_NEG_ENABLE)) 748ec4b94f9SMichael Grzeschik data |= BMCR_ANENABLE; 7494b20a07eSOleksij Rempel } else { 750e66f840cSTristram Ha if (!(ctrl & PORT_AUTO_NEG_DISABLE)) 751ec4b94f9SMichael Grzeschik data |= BMCR_ANENABLE; 7524b20a07eSOleksij Rempel } 753e66f840cSTristram Ha if (restart & PORT_POWER_DOWN) 754ec4b94f9SMichael Grzeschik data |= BMCR_PDOWN; 755e66f840cSTristram Ha if (restart & PORT_AUTO_NEG_RESTART) 756ec4b94f9SMichael Grzeschik data |= BMCR_ANRESTART; 757e66f840cSTristram Ha if (ctrl & PORT_FORCE_FULL_DUPLEX) 758ec4b94f9SMichael Grzeschik data |= BMCR_FULLDPLX; 759e66f840cSTristram Ha if (speed & PORT_HP_MDIX) 760ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_HP_MDIX; 761e66f840cSTristram Ha if (restart & PORT_FORCE_MDIX) 762ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_FORCE_MDI; 763e66f840cSTristram Ha if (restart & PORT_AUTO_MDIX_DISABLE) 764ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_AUTO_MDIX; 765e66f840cSTristram Ha if (restart & PORT_TX_DISABLE) 766ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_TRANSMIT; 767e66f840cSTristram Ha if (restart & PORT_LED_OFF) 768ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_LED; 769e66f840cSTristram Ha break; 770ec4b94f9SMichael Grzeschik case MII_BMSR: 7719f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LINK_STATUS], &link); 772ec4b94f9SMichael Grzeschik data = BMSR_100FULL | 773ec4b94f9SMichael Grzeschik BMSR_100HALF | 774ec4b94f9SMichael Grzeschik BMSR_10FULL | 775ec4b94f9SMichael Grzeschik BMSR_10HALF | 776ec4b94f9SMichael Grzeschik BMSR_ANEGCAPABLE; 777e66f840cSTristram Ha if (link & PORT_AUTO_NEG_COMPLETE) 778ec4b94f9SMichael Grzeschik data |= BMSR_ANEGCOMPLETE; 779e66f840cSTristram Ha if (link & PORT_STAT_LINK_GOOD) 780ec4b94f9SMichael Grzeschik data |= BMSR_LSTATUS; 781e66f840cSTristram Ha break; 782ec4b94f9SMichael Grzeschik case MII_PHYSID1: 783e66f840cSTristram Ha data = KSZ8795_ID_HI; 784e66f840cSTristram Ha break; 785ec4b94f9SMichael Grzeschik case MII_PHYSID2: 7864b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 7874b20a07eSOleksij Rempel data = KSZ8863_ID_LO; 7884b20a07eSOleksij Rempel else 789e66f840cSTristram Ha data = KSZ8795_ID_LO; 790e66f840cSTristram Ha break; 791ec4b94f9SMichael Grzeschik case MII_ADVERTISE: 7929f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl); 793ec4b94f9SMichael Grzeschik data = ADVERTISE_CSMA; 794e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_SYM_PAUSE) 795ec4b94f9SMichael Grzeschik data |= ADVERTISE_PAUSE_CAP; 796e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX_FD) 797ec4b94f9SMichael Grzeschik data |= ADVERTISE_100FULL; 798e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX) 799ec4b94f9SMichael Grzeschik data |= ADVERTISE_100HALF; 800e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT_FD) 801ec4b94f9SMichael Grzeschik data |= ADVERTISE_10FULL; 802e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT) 803ec4b94f9SMichael Grzeschik data |= ADVERTISE_10HALF; 804e66f840cSTristram Ha break; 805ec4b94f9SMichael Grzeschik case MII_LPA: 8069f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link); 807ec4b94f9SMichael Grzeschik data = LPA_SLCT; 808e66f840cSTristram Ha if (link & PORT_REMOTE_SYM_PAUSE) 809ec4b94f9SMichael Grzeschik data |= LPA_PAUSE_CAP; 810e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX_FD) 811ec4b94f9SMichael Grzeschik data |= LPA_100FULL; 812e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX) 813ec4b94f9SMichael Grzeschik data |= LPA_100HALF; 814e66f840cSTristram Ha if (link & PORT_REMOTE_10BT_FD) 815ec4b94f9SMichael Grzeschik data |= LPA_10FULL; 816e66f840cSTristram Ha if (link & PORT_REMOTE_10BT) 817ec4b94f9SMichael Grzeschik data |= LPA_10HALF; 818ec4b94f9SMichael Grzeschik if (data & ~LPA_SLCT) 819ec4b94f9SMichael Grzeschik data |= LPA_LPACK; 820e66f840cSTristram Ha break; 82136838050SOleksij Rempel case PHY_REG_LINK_MD: 82236838050SOleksij Rempel ksz_pread8(dev, p, REG_PORT_LINK_MD_CTRL, &val1); 82336838050SOleksij Rempel ksz_pread8(dev, p, REG_PORT_LINK_MD_RESULT, &val2); 82436838050SOleksij Rempel if (val1 & PORT_START_CABLE_DIAG) 82536838050SOleksij Rempel data |= PHY_START_CABLE_DIAG; 82636838050SOleksij Rempel 82736838050SOleksij Rempel if (val1 & PORT_CABLE_10M_SHORT) 82836838050SOleksij Rempel data |= PHY_CABLE_10M_SHORT; 82936838050SOleksij Rempel 83036838050SOleksij Rempel data |= FIELD_PREP(PHY_CABLE_DIAG_RESULT_M, 83136838050SOleksij Rempel FIELD_GET(PORT_CABLE_DIAG_RESULT_M, val1)); 83236838050SOleksij Rempel 83336838050SOleksij Rempel data |= FIELD_PREP(PHY_CABLE_FAULT_COUNTER_M, 83436838050SOleksij Rempel (FIELD_GET(PORT_CABLE_FAULT_COUNTER_H, val1) << 8) | 83536838050SOleksij Rempel FIELD_GET(PORT_CABLE_FAULT_COUNTER_L, val2)); 83636838050SOleksij Rempel break; 83752939393SOleksij Rempel case PHY_REG_PHY_CTRL: 83852939393SOleksij Rempel ksz_pread8(dev, p, regs[P_LINK_STATUS], &link); 83952939393SOleksij Rempel if (link & PORT_MDIX_STATUS) 84052939393SOleksij Rempel data |= KSZ886X_CTRL_MDIX_STAT; 84152939393SOleksij Rempel break; 842e66f840cSTristram Ha default: 843e66f840cSTristram Ha processed = false; 844e66f840cSTristram Ha break; 845e66f840cSTristram Ha } 846e66f840cSTristram Ha if (processed) 847e66f840cSTristram Ha *val = data; 848e66f840cSTristram Ha } 849e66f840cSTristram Ha 8504b5baca0SMichael Grzeschik static void ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val) 851e66f840cSTristram Ha { 8529f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 853e66f840cSTristram Ha u8 restart, speed, ctrl, data; 8549f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 8559f73e112SMichael Grzeschik u8 p = phy; 856e66f840cSTristram Ha 857e66f840cSTristram Ha switch (reg) { 858ec4b94f9SMichael Grzeschik case MII_BMCR: 859e66f840cSTristram Ha 860e66f840cSTristram Ha /* Do not support PHY reset function. */ 861ec4b94f9SMichael Grzeschik if (val & BMCR_RESET) 862e66f840cSTristram Ha break; 8639f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed); 864e66f840cSTristram Ha data = speed; 865ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_HP_MDIX) 866e66f840cSTristram Ha data |= PORT_HP_MDIX; 867e66f840cSTristram Ha else 868e66f840cSTristram Ha data &= ~PORT_HP_MDIX; 869e66f840cSTristram Ha if (data != speed) 8709f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_SPEED_STATUS], data); 8719f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl); 872e66f840cSTristram Ha data = ctrl; 8734b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 874ec4b94f9SMichael Grzeschik if ((val & BMCR_ANENABLE)) 8754b20a07eSOleksij Rempel data |= PORT_AUTO_NEG_ENABLE; 8764b20a07eSOleksij Rempel else 8774b20a07eSOleksij Rempel data &= ~PORT_AUTO_NEG_ENABLE; 8784b20a07eSOleksij Rempel } else { 879ec4b94f9SMichael Grzeschik if (!(val & BMCR_ANENABLE)) 880e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 881e66f840cSTristram Ha else 882e66f840cSTristram Ha data &= ~PORT_AUTO_NEG_DISABLE; 883e66f840cSTristram Ha 884e66f840cSTristram Ha /* Fiber port does not support auto-negotiation. */ 885e66f840cSTristram Ha if (dev->ports[p].fiber) 886e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 8874b20a07eSOleksij Rempel } 8884b20a07eSOleksij Rempel 889ec4b94f9SMichael Grzeschik if (val & BMCR_SPEED100) 890e66f840cSTristram Ha data |= PORT_FORCE_100_MBIT; 891e66f840cSTristram Ha else 892e66f840cSTristram Ha data &= ~PORT_FORCE_100_MBIT; 893ec4b94f9SMichael Grzeschik if (val & BMCR_FULLDPLX) 894e66f840cSTristram Ha data |= PORT_FORCE_FULL_DUPLEX; 895e66f840cSTristram Ha else 896e66f840cSTristram Ha data &= ~PORT_FORCE_FULL_DUPLEX; 897e66f840cSTristram Ha if (data != ctrl) 8989f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_FORCE_CTRL], data); 8999f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart); 900e66f840cSTristram Ha data = restart; 901ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_LED) 902e66f840cSTristram Ha data |= PORT_LED_OFF; 903e66f840cSTristram Ha else 904e66f840cSTristram Ha data &= ~PORT_LED_OFF; 905ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_TRANSMIT) 906e66f840cSTristram Ha data |= PORT_TX_DISABLE; 907e66f840cSTristram Ha else 908e66f840cSTristram Ha data &= ~PORT_TX_DISABLE; 909ec4b94f9SMichael Grzeschik if (val & BMCR_ANRESTART) 910e66f840cSTristram Ha data |= PORT_AUTO_NEG_RESTART; 911e66f840cSTristram Ha else 912e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_RESTART); 913ec4b94f9SMichael Grzeschik if (val & BMCR_PDOWN) 914e66f840cSTristram Ha data |= PORT_POWER_DOWN; 915e66f840cSTristram Ha else 916e66f840cSTristram Ha data &= ~PORT_POWER_DOWN; 917ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX) 918e66f840cSTristram Ha data |= PORT_AUTO_MDIX_DISABLE; 919e66f840cSTristram Ha else 920e66f840cSTristram Ha data &= ~PORT_AUTO_MDIX_DISABLE; 921ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_FORCE_MDI) 922e66f840cSTristram Ha data |= PORT_FORCE_MDIX; 923e66f840cSTristram Ha else 924e66f840cSTristram Ha data &= ~PORT_FORCE_MDIX; 925ec4b94f9SMichael Grzeschik if (val & BMCR_LOOPBACK) 926e66f840cSTristram Ha data |= PORT_PHY_LOOPBACK; 927e66f840cSTristram Ha else 928e66f840cSTristram Ha data &= ~PORT_PHY_LOOPBACK; 929e66f840cSTristram Ha if (data != restart) 9309f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_NEG_RESTART_CTRL], data); 931e66f840cSTristram Ha break; 932ec4b94f9SMichael Grzeschik case MII_ADVERTISE: 9339f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl); 934e66f840cSTristram Ha data = ctrl; 935e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_SYM_PAUSE | 936e66f840cSTristram Ha PORT_AUTO_NEG_100BTX_FD | 937e66f840cSTristram Ha PORT_AUTO_NEG_100BTX | 938e66f840cSTristram Ha PORT_AUTO_NEG_10BT_FD | 939e66f840cSTristram Ha PORT_AUTO_NEG_10BT); 940ec4b94f9SMichael Grzeschik if (val & ADVERTISE_PAUSE_CAP) 941e66f840cSTristram Ha data |= PORT_AUTO_NEG_SYM_PAUSE; 942ec4b94f9SMichael Grzeschik if (val & ADVERTISE_100FULL) 943e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX_FD; 944ec4b94f9SMichael Grzeschik if (val & ADVERTISE_100HALF) 945e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX; 946ec4b94f9SMichael Grzeschik if (val & ADVERTISE_10FULL) 947e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT_FD; 948ec4b94f9SMichael Grzeschik if (val & ADVERTISE_10HALF) 949e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT; 950e66f840cSTristram Ha if (data != ctrl) 9519f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data); 952e66f840cSTristram Ha break; 95336838050SOleksij Rempel case PHY_REG_LINK_MD: 95436838050SOleksij Rempel if (val & PHY_START_CABLE_DIAG) 95536838050SOleksij Rempel ksz_port_cfg(dev, p, REG_PORT_LINK_MD_CTRL, PORT_START_CABLE_DIAG, true); 95636838050SOleksij Rempel break; 957e66f840cSTristram Ha default: 958e66f840cSTristram Ha break; 959e66f840cSTristram Ha } 960e66f840cSTristram Ha } 961e66f840cSTristram Ha 9624b5baca0SMichael Grzeschik static enum dsa_tag_protocol ksz8_get_tag_protocol(struct dsa_switch *ds, 9634d776482SFlorian Fainelli int port, 9644d776482SFlorian Fainelli enum dsa_tag_protocol mp) 965e66f840cSTristram Ha { 9664b20a07eSOleksij Rempel struct ksz_device *dev = ds->priv; 9674b20a07eSOleksij Rempel 9684b20a07eSOleksij Rempel /* ksz88x3 uses the same tag schema as KSZ9893 */ 9694b20a07eSOleksij Rempel return ksz_is_ksz88x3(dev) ? 9704b20a07eSOleksij Rempel DSA_TAG_PROTO_KSZ9893 : DSA_TAG_PROTO_KSZ8795; 971e66f840cSTristram Ha } 972e66f840cSTristram Ha 97349011e0cSOleksij Rempel static u32 ksz8_sw_get_phy_flags(struct dsa_switch *ds, int port) 97449011e0cSOleksij Rempel { 97549011e0cSOleksij Rempel /* Silicon Errata Sheet (DS80000830A): 97649011e0cSOleksij Rempel * Port 1 does not work with LinkMD Cable-Testing. 97749011e0cSOleksij Rempel * Port 1 does not respond to received PAUSE control frames. 97849011e0cSOleksij Rempel */ 97949011e0cSOleksij Rempel if (!port) 98049011e0cSOleksij Rempel return MICREL_KSZ8_P1_ERRATA; 98149011e0cSOleksij Rempel 98249011e0cSOleksij Rempel return 0; 98349011e0cSOleksij Rempel } 98449011e0cSOleksij Rempel 9854b5baca0SMichael Grzeschik static void ksz8_get_strings(struct dsa_switch *ds, int port, 986e66f840cSTristram Ha u32 stringset, uint8_t *buf) 987e66f840cSTristram Ha { 98865fe1acfSMichael Grzeschik struct ksz_device *dev = ds->priv; 989e66f840cSTristram Ha int i; 990e66f840cSTristram Ha 99165fe1acfSMichael Grzeschik for (i = 0; i < dev->mib_cnt; i++) { 9924b20a07eSOleksij Rempel memcpy(buf + i * ETH_GSTRING_LEN, 9934b20a07eSOleksij Rempel dev->mib_names[i].string, ETH_GSTRING_LEN); 994e66f840cSTristram Ha } 995e66f840cSTristram Ha } 996e66f840cSTristram Ha 9974b5baca0SMichael Grzeschik static void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member) 998e66f840cSTristram Ha { 999e66f840cSTristram Ha u8 data; 1000e66f840cSTristram Ha 1001e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 1002e66f840cSTristram Ha data &= ~PORT_VLAN_MEMBERSHIP; 1003e66f840cSTristram Ha data |= (member & dev->port_mask); 1004e66f840cSTristram Ha ksz_pwrite8(dev, port, P_MIRROR_CTRL, data); 1005e66f840cSTristram Ha dev->ports[port].member = member; 1006e66f840cSTristram Ha } 1007e66f840cSTristram Ha 10084b5baca0SMichael Grzeschik static void ksz8_port_stp_state_set(struct dsa_switch *ds, int port, u8 state) 1009e66f840cSTristram Ha { 1010e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1011e66f840cSTristram Ha int forward = dev->member; 1012e66f840cSTristram Ha struct ksz_port *p; 1013e66f840cSTristram Ha int member = -1; 1014e66f840cSTristram Ha u8 data; 1015e66f840cSTristram Ha 1016e66f840cSTristram Ha p = &dev->ports[port]; 1017e66f840cSTristram Ha 1018e66f840cSTristram Ha ksz_pread8(dev, port, P_STP_CTRL, &data); 1019e66f840cSTristram Ha data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE); 1020e66f840cSTristram Ha 1021e66f840cSTristram Ha switch (state) { 1022e66f840cSTristram Ha case BR_STATE_DISABLED: 1023e66f840cSTristram Ha data |= PORT_LEARN_DISABLE; 10244ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt) 1025e66f840cSTristram Ha member = 0; 1026e66f840cSTristram Ha break; 1027e66f840cSTristram Ha case BR_STATE_LISTENING: 1028e66f840cSTristram Ha data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE); 10294ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt && 1030e66f840cSTristram Ha p->stp_state == BR_STATE_DISABLED) 1031e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 1032e66f840cSTristram Ha break; 1033e66f840cSTristram Ha case BR_STATE_LEARNING: 1034e66f840cSTristram Ha data |= PORT_RX_ENABLE; 1035e66f840cSTristram Ha break; 1036e66f840cSTristram Ha case BR_STATE_FORWARDING: 1037e66f840cSTristram Ha data |= (PORT_TX_ENABLE | PORT_RX_ENABLE); 1038e66f840cSTristram Ha 1039e66f840cSTristram Ha /* This function is also used internally. */ 1040e66f840cSTristram Ha if (port == dev->cpu_port) 1041e66f840cSTristram Ha break; 1042e66f840cSTristram Ha 1043e66f840cSTristram Ha /* Port is a member of a bridge. */ 1044e66f840cSTristram Ha if (dev->br_member & BIT(port)) { 1045e66f840cSTristram Ha dev->member |= BIT(port); 1046e66f840cSTristram Ha member = dev->member; 1047e66f840cSTristram Ha } else { 1048e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 1049e66f840cSTristram Ha } 1050e66f840cSTristram Ha break; 1051e66f840cSTristram Ha case BR_STATE_BLOCKING: 1052e66f840cSTristram Ha data |= PORT_LEARN_DISABLE; 10534ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt && 1054e66f840cSTristram Ha p->stp_state == BR_STATE_DISABLED) 1055e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 1056e66f840cSTristram Ha break; 1057e66f840cSTristram Ha default: 1058e66f840cSTristram Ha dev_err(ds->dev, "invalid STP state: %d\n", state); 1059e66f840cSTristram Ha return; 1060e66f840cSTristram Ha } 1061e66f840cSTristram Ha 1062e66f840cSTristram Ha ksz_pwrite8(dev, port, P_STP_CTRL, data); 1063e66f840cSTristram Ha p->stp_state = state; 1064e66f840cSTristram Ha /* Port membership may share register with STP state. */ 1065e66f840cSTristram Ha if (member >= 0 && member != p->member) 10664b5baca0SMichael Grzeschik ksz8_cfg_port_member(dev, port, (u8)member); 1067e66f840cSTristram Ha 1068e66f840cSTristram Ha /* Check if forwarding needs to be updated. */ 1069e66f840cSTristram Ha if (state != BR_STATE_FORWARDING) { 1070e66f840cSTristram Ha if (dev->br_member & BIT(port)) 1071e66f840cSTristram Ha dev->member &= ~BIT(port); 1072e66f840cSTristram Ha } 1073e66f840cSTristram Ha 1074e66f840cSTristram Ha /* When topology has changed the function ksz_update_port_member 1075e66f840cSTristram Ha * should be called to modify port forwarding behavior. 1076e66f840cSTristram Ha */ 1077e66f840cSTristram Ha if (forward != dev->member) 1078e66f840cSTristram Ha ksz_update_port_member(dev, port); 1079e66f840cSTristram Ha } 1080e66f840cSTristram Ha 10814b5baca0SMichael Grzeschik static void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port) 1082e66f840cSTristram Ha { 1083241ed719SMichael Grzeschik u8 learn[DSA_MAX_PORTS]; 1084e66f840cSTristram Ha int first, index, cnt; 1085e66f840cSTristram Ha struct ksz_port *p; 1086e66f840cSTristram Ha 1087241ed719SMichael Grzeschik if ((uint)port < dev->port_cnt) { 1088e66f840cSTristram Ha first = port; 1089e66f840cSTristram Ha cnt = port + 1; 1090e66f840cSTristram Ha } else { 1091e66f840cSTristram Ha /* Flush all ports. */ 1092e66f840cSTristram Ha first = 0; 1093c9f4633bSMichael Grzeschik cnt = dev->port_cnt; 1094e66f840cSTristram Ha } 1095e66f840cSTristram Ha for (index = first; index < cnt; index++) { 1096e66f840cSTristram Ha p = &dev->ports[index]; 1097e66f840cSTristram Ha if (!p->on) 1098e66f840cSTristram Ha continue; 1099e66f840cSTristram Ha ksz_pread8(dev, index, P_STP_CTRL, &learn[index]); 1100e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 1101e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, 1102e66f840cSTristram Ha learn[index] | PORT_LEARN_DISABLE); 1103e66f840cSTristram Ha } 1104e66f840cSTristram Ha ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true); 1105e66f840cSTristram Ha for (index = first; index < cnt; index++) { 1106e66f840cSTristram Ha p = &dev->ports[index]; 1107e66f840cSTristram Ha if (!p->on) 1108e66f840cSTristram Ha continue; 1109e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 1110e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, learn[index]); 1111e66f840cSTristram Ha } 1112e66f840cSTristram Ha } 1113e66f840cSTristram Ha 11144b5baca0SMichael Grzeschik static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag, 111589153ed6SVladimir Oltean struct netlink_ext_ack *extack) 1116e66f840cSTristram Ha { 1117e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1118e66f840cSTristram Ha 11194b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 11204b20a07eSOleksij Rempel return -ENOTSUPP; 11214b20a07eSOleksij Rempel 112216484413SBen Hutchings /* Discard packets with VID not enabled on the switch */ 1123e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag); 1124e66f840cSTristram Ha 112516484413SBen Hutchings /* Discard packets with VID not enabled on the ingress port */ 112616484413SBen Hutchings for (port = 0; port < dev->phy_port_cnt; ++port) 112716484413SBen Hutchings ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER, 112816484413SBen Hutchings flag); 112916484413SBen Hutchings 1130e66f840cSTristram Ha return 0; 1131e66f840cSTristram Ha } 1132e66f840cSTristram Ha 1133ef3b02a1SBen Hutchings static void ksz8_port_enable_pvid(struct ksz_device *dev, int port, bool state) 1134ef3b02a1SBen Hutchings { 1135ef3b02a1SBen Hutchings if (ksz_is_ksz88x3(dev)) { 1136ef3b02a1SBen Hutchings ksz_cfg(dev, REG_SW_INSERT_SRC_PVID, 1137ef3b02a1SBen Hutchings 0x03 << (4 - 2 * port), state); 1138ef3b02a1SBen Hutchings } else { 1139ef3b02a1SBen Hutchings ksz_pwrite8(dev, port, REG_PORT_CTRL_12, state ? 0x0f : 0x00); 1140ef3b02a1SBen Hutchings } 1141ef3b02a1SBen Hutchings } 1142ef3b02a1SBen Hutchings 11434b5baca0SMichael Grzeschik static int ksz8_port_vlan_add(struct dsa_switch *ds, int port, 114431046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 114531046a5fSVladimir Oltean struct netlink_ext_ack *extack) 1146e66f840cSTristram Ha { 1147e66f840cSTristram Ha bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 1148e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 11498f4f58f8SBen Hutchings struct ksz_port *p = &dev->ports[port]; 1150b7a9e0daSVladimir Oltean u16 data, new_pvid = 0; 1151e66f840cSTristram Ha u8 fid, member, valid; 1152e66f840cSTristram Ha 11534b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 11544b20a07eSOleksij Rempel return -ENOTSUPP; 11554b20a07eSOleksij Rempel 11568f4f58f8SBen Hutchings /* If a VLAN is added with untagged flag different from the 11578f4f58f8SBen Hutchings * port's Remove Tag flag, we need to change the latter. 11588f4f58f8SBen Hutchings * Ignore VID 0, which is always untagged. 11599130c2d3SBen Hutchings * Ignore CPU port, which will always be tagged. 11608f4f58f8SBen Hutchings */ 11619130c2d3SBen Hutchings if (untagged != p->remove_tag && vlan->vid != 0 && 11629130c2d3SBen Hutchings port != dev->cpu_port) { 11638f4f58f8SBen Hutchings unsigned int vid; 11648f4f58f8SBen Hutchings 11658f4f58f8SBen Hutchings /* Reject attempts to add a VLAN that requires the 11668f4f58f8SBen Hutchings * Remove Tag flag to be changed, unless there are no 11678f4f58f8SBen Hutchings * other VLANs currently configured. 11688f4f58f8SBen Hutchings */ 11698f4f58f8SBen Hutchings for (vid = 1; vid < dev->num_vlans; ++vid) { 11708f4f58f8SBen Hutchings /* Skip the VID we are going to add or reconfigure */ 11718f4f58f8SBen Hutchings if (vid == vlan->vid) 11728f4f58f8SBen Hutchings continue; 11738f4f58f8SBen Hutchings 11748f4f58f8SBen Hutchings ksz8_from_vlan(dev, dev->vlan_cache[vid].table[0], 11758f4f58f8SBen Hutchings &fid, &member, &valid); 11768f4f58f8SBen Hutchings if (valid && (member & BIT(port))) 11778f4f58f8SBen Hutchings return -EINVAL; 11788f4f58f8SBen Hutchings } 11798f4f58f8SBen Hutchings 1180e66f840cSTristram Ha ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged); 11818f4f58f8SBen Hutchings p->remove_tag = untagged; 11828f4f58f8SBen Hutchings } 1183e66f840cSTristram Ha 11844b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 11859f73e112SMichael Grzeschik ksz8_from_vlan(dev, data, &fid, &member, &valid); 1186e66f840cSTristram Ha 1187e66f840cSTristram Ha /* First time to setup the VLAN entry. */ 1188e66f840cSTristram Ha if (!valid) { 1189e66f840cSTristram Ha /* Need to find a way to map VID to FID. */ 1190e66f840cSTristram Ha fid = 1; 1191e66f840cSTristram Ha valid = 1; 1192e66f840cSTristram Ha } 1193e66f840cSTristram Ha member |= BIT(port); 1194e66f840cSTristram Ha 11959f73e112SMichael Grzeschik ksz8_to_vlan(dev, fid, member, valid, &data); 11964b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 1197e66f840cSTristram Ha 1198e66f840cSTristram Ha /* change PVID */ 1199e66f840cSTristram Ha if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 1200b7a9e0daSVladimir Oltean new_pvid = vlan->vid; 1201e66f840cSTristram Ha 1202e66f840cSTristram Ha if (new_pvid) { 1203b7a9e0daSVladimir Oltean u16 vid; 1204b7a9e0daSVladimir Oltean 1205e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid); 1206ef3b02a1SBen Hutchings vid &= ~VLAN_VID_MASK; 1207e66f840cSTristram Ha vid |= new_pvid; 1208e66f840cSTristram Ha ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid); 1209ef3b02a1SBen Hutchings 1210ef3b02a1SBen Hutchings ksz8_port_enable_pvid(dev, port, true); 1211e66f840cSTristram Ha } 12121958d581SVladimir Oltean 12131958d581SVladimir Oltean return 0; 1214e66f840cSTristram Ha } 1215e66f840cSTristram Ha 12164b5baca0SMichael Grzeschik static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, 1217e66f840cSTristram Ha const struct switchdev_obj_port_vlan *vlan) 1218e66f840cSTristram Ha { 1219e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1220ef3b02a1SBen Hutchings u16 data, pvid; 1221e66f840cSTristram Ha u8 fid, member, valid; 1222e66f840cSTristram Ha 12234b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 12244b20a07eSOleksij Rempel return -ENOTSUPP; 12254b20a07eSOleksij Rempel 1226e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid); 1227e66f840cSTristram Ha pvid = pvid & 0xFFF; 1228e66f840cSTristram Ha 12294b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 12309f73e112SMichael Grzeschik ksz8_from_vlan(dev, data, &fid, &member, &valid); 1231e66f840cSTristram Ha 1232e66f840cSTristram Ha member &= ~BIT(port); 1233e66f840cSTristram Ha 1234e66f840cSTristram Ha /* Invalidate the entry if no more member. */ 1235e66f840cSTristram Ha if (!member) { 1236e66f840cSTristram Ha fid = 0; 1237e66f840cSTristram Ha valid = 0; 1238e66f840cSTristram Ha } 1239e66f840cSTristram Ha 12409f73e112SMichael Grzeschik ksz8_to_vlan(dev, fid, member, valid, &data); 12414b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 1242e66f840cSTristram Ha 1243ef3b02a1SBen Hutchings if (pvid == vlan->vid) 1244ef3b02a1SBen Hutchings ksz8_port_enable_pvid(dev, port, false); 1245e66f840cSTristram Ha 1246e66f840cSTristram Ha return 0; 1247e66f840cSTristram Ha } 1248e66f840cSTristram Ha 12494b5baca0SMichael Grzeschik static int ksz8_port_mirror_add(struct dsa_switch *ds, int port, 1250e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror, 1251e66f840cSTristram Ha bool ingress) 1252e66f840cSTristram Ha { 1253e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1254e66f840cSTristram Ha 1255e66f840cSTristram Ha if (ingress) { 1256e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true); 1257e66f840cSTristram Ha dev->mirror_rx |= BIT(port); 1258e66f840cSTristram Ha } else { 1259e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true); 1260e66f840cSTristram Ha dev->mirror_tx |= BIT(port); 1261e66f840cSTristram Ha } 1262e66f840cSTristram Ha 1263e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false); 1264e66f840cSTristram Ha 1265e66f840cSTristram Ha /* configure mirror port */ 1266e66f840cSTristram Ha if (dev->mirror_rx || dev->mirror_tx) 1267e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 1268e66f840cSTristram Ha PORT_MIRROR_SNIFFER, true); 1269e66f840cSTristram Ha 1270e66f840cSTristram Ha return 0; 1271e66f840cSTristram Ha } 1272e66f840cSTristram Ha 12734b5baca0SMichael Grzeschik static void ksz8_port_mirror_del(struct dsa_switch *ds, int port, 1274e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror) 1275e66f840cSTristram Ha { 1276e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1277e66f840cSTristram Ha u8 data; 1278e66f840cSTristram Ha 1279e66f840cSTristram Ha if (mirror->ingress) { 1280e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false); 1281e66f840cSTristram Ha dev->mirror_rx &= ~BIT(port); 1282e66f840cSTristram Ha } else { 1283e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false); 1284e66f840cSTristram Ha dev->mirror_tx &= ~BIT(port); 1285e66f840cSTristram Ha } 1286e66f840cSTristram Ha 1287e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 1288e66f840cSTristram Ha 1289e66f840cSTristram Ha if (!dev->mirror_rx && !dev->mirror_tx) 1290e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 1291e66f840cSTristram Ha PORT_MIRROR_SNIFFER, false); 1292e66f840cSTristram Ha } 1293e66f840cSTristram Ha 1294c2ac4d2aSMichael Grzeschik static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port) 1295e66f840cSTristram Ha { 1296e66f840cSTristram Ha struct ksz_port *p = &dev->ports[port]; 1297c2ac4d2aSMichael Grzeschik u8 data8; 1298e66f840cSTristram Ha 1299edecfa98SHelmut Grohne if (!p->interface && dev->compat_interface) { 1300edecfa98SHelmut Grohne dev_warn(dev->dev, 1301edecfa98SHelmut Grohne "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. " 1302edecfa98SHelmut Grohne "Please update your device tree.\n", 1303edecfa98SHelmut Grohne port); 1304edecfa98SHelmut Grohne p->interface = dev->compat_interface; 1305edecfa98SHelmut Grohne } 1306edecfa98SHelmut Grohne 1307e66f840cSTristram Ha /* Configure MII interface for proper network communication. */ 1308e66f840cSTristram Ha ksz_read8(dev, REG_PORT_5_CTRL_6, &data8); 1309e66f840cSTristram Ha data8 &= ~PORT_INTERFACE_TYPE; 1310e66f840cSTristram Ha data8 &= ~PORT_GMII_1GPS_MODE; 1311edecfa98SHelmut Grohne switch (p->interface) { 1312e66f840cSTristram Ha case PHY_INTERFACE_MODE_MII: 1313e66f840cSTristram Ha p->phydev.speed = SPEED_100; 1314e66f840cSTristram Ha break; 1315e66f840cSTristram Ha case PHY_INTERFACE_MODE_RMII: 1316e66f840cSTristram Ha data8 |= PORT_INTERFACE_RMII; 1317e66f840cSTristram Ha p->phydev.speed = SPEED_100; 1318e66f840cSTristram Ha break; 1319e66f840cSTristram Ha case PHY_INTERFACE_MODE_GMII: 1320e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 1321e66f840cSTristram Ha data8 |= PORT_INTERFACE_GMII; 1322e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 1323e66f840cSTristram Ha break; 1324e66f840cSTristram Ha default: 1325e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_IN_ENABLE; 1326e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_OUT_ENABLE; 1327edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 1328edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_RXID) 1329e66f840cSTristram Ha data8 |= PORT_RGMII_ID_IN_ENABLE; 1330edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 1331edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_TXID) 1332e66f840cSTristram Ha data8 |= PORT_RGMII_ID_OUT_ENABLE; 1333e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 1334e66f840cSTristram Ha data8 |= PORT_INTERFACE_RGMII; 1335e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 1336e66f840cSTristram Ha break; 1337e66f840cSTristram Ha } 1338e66f840cSTristram Ha ksz_write8(dev, REG_PORT_5_CTRL_6, data8); 1339e66f840cSTristram Ha p->phydev.duplex = 1; 1340c2ac4d2aSMichael Grzeschik } 1341c2ac4d2aSMichael Grzeschik 1342c2ac4d2aSMichael Grzeschik static void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port) 1343c2ac4d2aSMichael Grzeschik { 1344c2ac4d2aSMichael Grzeschik struct ksz_port *p = &dev->ports[port]; 13459f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 13469f73e112SMichael Grzeschik const u32 *masks; 1347c2ac4d2aSMichael Grzeschik u8 member; 1348c2ac4d2aSMichael Grzeschik 13499f73e112SMichael Grzeschik masks = ksz8->masks; 13509f73e112SMichael Grzeschik 1351c2ac4d2aSMichael Grzeschik /* enable broadcast storm limit */ 1352c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true); 1353c2ac4d2aSMichael Grzeschik 13544b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) 1355c2ac4d2aSMichael Grzeschik ksz8795_set_prio_queue(dev, port, 4); 1356c2ac4d2aSMichael Grzeschik 1357c2ac4d2aSMichael Grzeschik /* disable DiffServ priority */ 1358c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false); 1359c2ac4d2aSMichael Grzeschik 1360c2ac4d2aSMichael Grzeschik /* replace priority */ 13619f73e112SMichael Grzeschik ksz_port_cfg(dev, port, P_802_1P_CTRL, 13629f73e112SMichael Grzeschik masks[PORT_802_1P_REMAPPING], false); 1363c2ac4d2aSMichael Grzeschik 1364c2ac4d2aSMichael Grzeschik /* enable 802.1p priority */ 1365c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true); 1366c2ac4d2aSMichael Grzeschik 1367c2ac4d2aSMichael Grzeschik if (cpu_port) { 13684b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) 1369c2ac4d2aSMichael Grzeschik ksz8795_cpu_interface_select(dev, port); 1370e66f840cSTristram Ha 1371e66f840cSTristram Ha member = dev->port_mask; 1372e66f840cSTristram Ha } else { 1373e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 1374e66f840cSTristram Ha } 13754b5baca0SMichael Grzeschik ksz8_cfg_port_member(dev, port, member); 1376e66f840cSTristram Ha } 1377e66f840cSTristram Ha 13784b5baca0SMichael Grzeschik static void ksz8_config_cpu_port(struct dsa_switch *ds) 1379e66f840cSTristram Ha { 1380e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 13819f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 13829f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 1383e66f840cSTristram Ha struct ksz_port *p; 13849f73e112SMichael Grzeschik const u32 *masks; 1385e66f840cSTristram Ha u8 remote; 1386e66f840cSTristram Ha int i; 1387e66f840cSTristram Ha 13889f73e112SMichael Grzeschik masks = ksz8->masks; 13899f73e112SMichael Grzeschik 1390e66f840cSTristram Ha /* Switch marks the maximum frame with extra byte as oversize. */ 1391e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true); 13929f73e112SMichael Grzeschik ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true); 1393e66f840cSTristram Ha 1394e66f840cSTristram Ha p = &dev->ports[dev->cpu_port]; 1395e66f840cSTristram Ha p->vid_member = dev->port_mask; 1396e66f840cSTristram Ha p->on = 1; 1397e66f840cSTristram Ha 13984b5baca0SMichael Grzeschik ksz8_port_setup(dev, dev->cpu_port, true); 1399e66f840cSTristram Ha dev->member = dev->host_mask; 1400e66f840cSTristram Ha 14014ce2a984SMichael Grzeschik for (i = 0; i < dev->phy_port_cnt; i++) { 1402e66f840cSTristram Ha p = &dev->ports[i]; 1403e66f840cSTristram Ha 1404e66f840cSTristram Ha /* Initialize to non-zero so that ksz_cfg_port_member() will 1405e66f840cSTristram Ha * be called. 1406e66f840cSTristram Ha */ 1407e66f840cSTristram Ha p->vid_member = BIT(i); 1408e66f840cSTristram Ha p->member = dev->port_mask; 14094b5baca0SMichael Grzeschik ksz8_port_stp_state_set(ds, i, BR_STATE_DISABLED); 1410e66f840cSTristram Ha 1411e66f840cSTristram Ha /* Last port may be disabled. */ 14124ce2a984SMichael Grzeschik if (i == dev->phy_port_cnt) 1413e66f840cSTristram Ha break; 1414e66f840cSTristram Ha p->on = 1; 1415e66f840cSTristram Ha p->phy = 1; 1416e66f840cSTristram Ha } 1417e66f840cSTristram Ha for (i = 0; i < dev->phy_port_cnt; i++) { 1418e66f840cSTristram Ha p = &dev->ports[i]; 1419e66f840cSTristram Ha if (!p->on) 1420e66f840cSTristram Ha continue; 14214b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) { 14229f73e112SMichael Grzeschik ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote); 1423e66f840cSTristram Ha if (remote & PORT_FIBER_MODE) 1424e66f840cSTristram Ha p->fiber = 1; 14254b20a07eSOleksij Rempel } 1426e66f840cSTristram Ha if (p->fiber) 1427e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1428e66f840cSTristram Ha true); 1429e66f840cSTristram Ha else 1430e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1431e66f840cSTristram Ha false); 1432e66f840cSTristram Ha } 1433e66f840cSTristram Ha } 1434e66f840cSTristram Ha 14354b5baca0SMichael Grzeschik static int ksz8_setup(struct dsa_switch *ds) 1436e66f840cSTristram Ha { 1437e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1438e66f840cSTristram Ha struct alu_struct alu; 1439e66f840cSTristram Ha int i, ret = 0; 1440e66f840cSTristram Ha 1441e66f840cSTristram Ha dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table), 1442e66f840cSTristram Ha dev->num_vlans, GFP_KERNEL); 1443e66f840cSTristram Ha if (!dev->vlan_cache) 1444e66f840cSTristram Ha return -ENOMEM; 1445e66f840cSTristram Ha 14464b5baca0SMichael Grzeschik ret = ksz8_reset_switch(dev); 1447e66f840cSTristram Ha if (ret) { 1448e66f840cSTristram Ha dev_err(ds->dev, "failed to reset switch\n"); 1449e66f840cSTristram Ha return ret; 1450e66f840cSTristram Ha } 1451e66f840cSTristram Ha 1452e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true); 1453e66f840cSTristram Ha 1454e66f840cSTristram Ha /* Enable automatic fast aging when link changed detected. */ 1455e66f840cSTristram Ha ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true); 1456e66f840cSTristram Ha 1457e66f840cSTristram Ha /* Enable aggressive back off algorithm in half duplex mode. */ 1458e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1, 1459e66f840cSTristram Ha SW_AGGR_BACKOFF, SW_AGGR_BACKOFF); 1460e66f840cSTristram Ha 1461e66f840cSTristram Ha /* 1462e66f840cSTristram Ha * Make sure unicast VLAN boundary is set as default and 1463e66f840cSTristram Ha * enable no excessive collision drop. 1464e66f840cSTristram Ha */ 1465e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2, 1466e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP, 1467e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP); 1468e66f840cSTristram Ha 14694b5baca0SMichael Grzeschik ksz8_config_cpu_port(ds); 1470e66f840cSTristram Ha 1471e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true); 1472e66f840cSTristram Ha 1473e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false); 1474e66f840cSTristram Ha 1475e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false); 1476e66f840cSTristram Ha 1477ef3b02a1SBen Hutchings if (!ksz_is_ksz88x3(dev)) 1478ef3b02a1SBen Hutchings ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true); 1479ef3b02a1SBen Hutchings 1480e66f840cSTristram Ha /* set broadcast storm protection 10% rate */ 1481e66f840cSTristram Ha regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL, 1482e66f840cSTristram Ha BROADCAST_STORM_RATE, 1483e66f840cSTristram Ha (BROADCAST_STORM_VALUE * 1484e66f840cSTristram Ha BROADCAST_STORM_PROT_RATE) / 100); 1485e66f840cSTristram Ha 148602ffbb02SMichael Grzeschik for (i = 0; i < (dev->num_vlans / 4); i++) 14874b5baca0SMichael Grzeschik ksz8_r_vlan_entries(dev, i); 1488e66f840cSTristram Ha 1489e66f840cSTristram Ha /* Setup STP address for STP operation. */ 1490e66f840cSTristram Ha memset(&alu, 0, sizeof(alu)); 1491e66f840cSTristram Ha ether_addr_copy(alu.mac, eth_stp_addr); 1492e66f840cSTristram Ha alu.is_static = true; 1493e66f840cSTristram Ha alu.is_override = true; 1494e66f840cSTristram Ha alu.port_forward = dev->host_mask; 1495e66f840cSTristram Ha 14964b5baca0SMichael Grzeschik ksz8_w_sta_mac_table(dev, 0, &alu); 1497e66f840cSTristram Ha 1498e66f840cSTristram Ha ksz_init_mib_timer(dev); 1499e66f840cSTristram Ha 15000ee2af4eSVladimir Oltean ds->configure_vlan_while_not_filtering = false; 15010ee2af4eSVladimir Oltean 1502e66f840cSTristram Ha return 0; 1503e66f840cSTristram Ha } 1504e66f840cSTristram Ha 15052c709e0bSMichael Grzeschik static void ksz8_validate(struct dsa_switch *ds, int port, 15062c709e0bSMichael Grzeschik unsigned long *supported, 15072c709e0bSMichael Grzeschik struct phylink_link_state *state) 15082c709e0bSMichael Grzeschik { 15092c709e0bSMichael Grzeschik __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 15102c709e0bSMichael Grzeschik struct ksz_device *dev = ds->priv; 15112c709e0bSMichael Grzeschik 15122c709e0bSMichael Grzeschik if (port == dev->cpu_port) { 15132c709e0bSMichael Grzeschik if (state->interface != PHY_INTERFACE_MODE_RMII && 15142c709e0bSMichael Grzeschik state->interface != PHY_INTERFACE_MODE_MII && 15152c709e0bSMichael Grzeschik state->interface != PHY_INTERFACE_MODE_NA) 15162c709e0bSMichael Grzeschik goto unsupported; 15172c709e0bSMichael Grzeschik } else { 15182c709e0bSMichael Grzeschik if (state->interface != PHY_INTERFACE_MODE_INTERNAL && 15192c709e0bSMichael Grzeschik state->interface != PHY_INTERFACE_MODE_NA) 15202c709e0bSMichael Grzeschik goto unsupported; 15212c709e0bSMichael Grzeschik } 15222c709e0bSMichael Grzeschik 15232c709e0bSMichael Grzeschik /* Allow all the expected bits */ 15242c709e0bSMichael Grzeschik phylink_set_port_modes(mask); 15252c709e0bSMichael Grzeschik phylink_set(mask, Autoneg); 15262c709e0bSMichael Grzeschik 15272c709e0bSMichael Grzeschik /* Silicon Errata Sheet (DS80000830A): 15282c709e0bSMichael Grzeschik * "Port 1 does not respond to received flow control PAUSE frames" 15292c709e0bSMichael Grzeschik * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3 15302c709e0bSMichael Grzeschik * switches. 15312c709e0bSMichael Grzeschik */ 15322c709e0bSMichael Grzeschik if (!ksz_is_ksz88x3(dev) || port) 15332c709e0bSMichael Grzeschik phylink_set(mask, Pause); 15342c709e0bSMichael Grzeschik 15352c709e0bSMichael Grzeschik /* Asym pause is not supported on KSZ8863 and KSZ8873 */ 15362c709e0bSMichael Grzeschik if (!ksz_is_ksz88x3(dev)) 15372c709e0bSMichael Grzeschik phylink_set(mask, Asym_Pause); 15382c709e0bSMichael Grzeschik 15392c709e0bSMichael Grzeschik /* 10M and 100M are only supported */ 15402c709e0bSMichael Grzeschik phylink_set(mask, 10baseT_Half); 15412c709e0bSMichael Grzeschik phylink_set(mask, 10baseT_Full); 15422c709e0bSMichael Grzeschik phylink_set(mask, 100baseT_Half); 15432c709e0bSMichael Grzeschik phylink_set(mask, 100baseT_Full); 15442c709e0bSMichael Grzeschik 15452c709e0bSMichael Grzeschik bitmap_and(supported, supported, mask, 15462c709e0bSMichael Grzeschik __ETHTOOL_LINK_MODE_MASK_NBITS); 15472c709e0bSMichael Grzeschik bitmap_and(state->advertising, state->advertising, mask, 15482c709e0bSMichael Grzeschik __ETHTOOL_LINK_MODE_MASK_NBITS); 15492c709e0bSMichael Grzeschik 15502c709e0bSMichael Grzeschik return; 15512c709e0bSMichael Grzeschik 15522c709e0bSMichael Grzeschik unsupported: 15532c709e0bSMichael Grzeschik bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS); 15542c709e0bSMichael Grzeschik dev_err(ds->dev, "Unsupported interface: %s, port: %d\n", 15552c709e0bSMichael Grzeschik phy_modes(state->interface), port); 15562c709e0bSMichael Grzeschik } 15572c709e0bSMichael Grzeschik 15584b5baca0SMichael Grzeschik static const struct dsa_switch_ops ksz8_switch_ops = { 15594b5baca0SMichael Grzeschik .get_tag_protocol = ksz8_get_tag_protocol, 156049011e0cSOleksij Rempel .get_phy_flags = ksz8_sw_get_phy_flags, 15614b5baca0SMichael Grzeschik .setup = ksz8_setup, 1562e66f840cSTristram Ha .phy_read = ksz_phy_read16, 1563e66f840cSTristram Ha .phy_write = ksz_phy_write16, 15642c709e0bSMichael Grzeschik .phylink_validate = ksz8_validate, 1565143a102eSCodrin Ciubotariu .phylink_mac_link_down = ksz_mac_link_down, 1566e66f840cSTristram Ha .port_enable = ksz_enable_port, 15674b5baca0SMichael Grzeschik .get_strings = ksz8_get_strings, 1568e66f840cSTristram Ha .get_ethtool_stats = ksz_get_ethtool_stats, 1569e66f840cSTristram Ha .get_sset_count = ksz_sset_count, 1570e66f840cSTristram Ha .port_bridge_join = ksz_port_bridge_join, 1571e66f840cSTristram Ha .port_bridge_leave = ksz_port_bridge_leave, 15724b5baca0SMichael Grzeschik .port_stp_state_set = ksz8_port_stp_state_set, 1573e66f840cSTristram Ha .port_fast_age = ksz_port_fast_age, 15744b5baca0SMichael Grzeschik .port_vlan_filtering = ksz8_port_vlan_filtering, 15754b5baca0SMichael Grzeschik .port_vlan_add = ksz8_port_vlan_add, 15764b5baca0SMichael Grzeschik .port_vlan_del = ksz8_port_vlan_del, 1577e66f840cSTristram Ha .port_fdb_dump = ksz_port_fdb_dump, 1578e66f840cSTristram Ha .port_mdb_add = ksz_port_mdb_add, 1579e66f840cSTristram Ha .port_mdb_del = ksz_port_mdb_del, 15804b5baca0SMichael Grzeschik .port_mirror_add = ksz8_port_mirror_add, 15814b5baca0SMichael Grzeschik .port_mirror_del = ksz8_port_mirror_del, 1582e66f840cSTristram Ha }; 1583e66f840cSTristram Ha 15844b5baca0SMichael Grzeschik static u32 ksz8_get_port_addr(int port, int offset) 1585e66f840cSTristram Ha { 1586e66f840cSTristram Ha return PORT_CTRL_ADDR(port, offset); 1587e66f840cSTristram Ha } 1588e66f840cSTristram Ha 15894b5baca0SMichael Grzeschik static int ksz8_switch_detect(struct ksz_device *dev) 1590e66f840cSTristram Ha { 1591e66f840cSTristram Ha u8 id1, id2; 1592e66f840cSTristram Ha u16 id16; 1593e66f840cSTristram Ha int ret; 1594e66f840cSTristram Ha 1595e66f840cSTristram Ha /* read chip id */ 1596e66f840cSTristram Ha ret = ksz_read16(dev, REG_CHIP_ID0, &id16); 1597e66f840cSTristram Ha if (ret) 1598e66f840cSTristram Ha return ret; 1599e66f840cSTristram Ha 1600e66f840cSTristram Ha id1 = id16 >> 8; 1601e66f840cSTristram Ha id2 = id16 & SW_CHIP_ID_M; 16024b20a07eSOleksij Rempel 16034b20a07eSOleksij Rempel switch (id1) { 16044b20a07eSOleksij Rempel case KSZ87_FAMILY_ID: 16054b20a07eSOleksij Rempel if ((id2 != CHIP_ID_94 && id2 != CHIP_ID_95)) 1606e66f840cSTristram Ha return -ENODEV; 1607e66f840cSTristram Ha 1608e66f840cSTristram Ha if (id2 == CHIP_ID_95) { 1609e66f840cSTristram Ha u8 val; 1610e66f840cSTristram Ha 1611e66f840cSTristram Ha id2 = 0x95; 16124b20a07eSOleksij Rempel ksz_read8(dev, REG_PORT_STATUS_0, &val); 1613e66f840cSTristram Ha if (val & PORT_FIBER_MODE) 1614e66f840cSTristram Ha id2 = 0x65; 1615e66f840cSTristram Ha } else if (id2 == CHIP_ID_94) { 1616e66f840cSTristram Ha id2 = 0x94; 1617e66f840cSTristram Ha } 16184b20a07eSOleksij Rempel break; 16194b20a07eSOleksij Rempel case KSZ88_FAMILY_ID: 16204b20a07eSOleksij Rempel if (id2 != CHIP_ID_63) 16214b20a07eSOleksij Rempel return -ENODEV; 16224b20a07eSOleksij Rempel break; 16234b20a07eSOleksij Rempel default: 16244b20a07eSOleksij Rempel dev_err(dev->dev, "invalid family id: %d\n", id1); 16254b20a07eSOleksij Rempel return -ENODEV; 16264b20a07eSOleksij Rempel } 1627e66f840cSTristram Ha id16 &= ~0xff; 1628e66f840cSTristram Ha id16 |= id2; 1629e66f840cSTristram Ha dev->chip_id = id16; 1630e66f840cSTristram Ha 1631e66f840cSTristram Ha return 0; 1632e66f840cSTristram Ha } 1633e66f840cSTristram Ha 1634e66f840cSTristram Ha struct ksz_chip_data { 1635e66f840cSTristram Ha u16 chip_id; 1636e66f840cSTristram Ha const char *dev_name; 1637e66f840cSTristram Ha int num_vlans; 1638e66f840cSTristram Ha int num_alus; 1639e66f840cSTristram Ha int num_statics; 1640e66f840cSTristram Ha int cpu_ports; 1641e66f840cSTristram Ha int port_cnt; 1642e66f840cSTristram Ha }; 1643e66f840cSTristram Ha 16444b5baca0SMichael Grzeschik static const struct ksz_chip_data ksz8_switch_chips[] = { 1645e66f840cSTristram Ha { 1646e66f840cSTristram Ha .chip_id = 0x8795, 1647e66f840cSTristram Ha .dev_name = "KSZ8795", 1648e66f840cSTristram Ha .num_vlans = 4096, 1649e66f840cSTristram Ha .num_alus = 0, 1650e66f840cSTristram Ha .num_statics = 8, 1651e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 165294374dd1SMichael Grzeschik .port_cnt = 5, /* total cpu and user ports */ 1653e66f840cSTristram Ha }, 1654e66f840cSTristram Ha { 1655c369d7fcSMarek Vasut /* 1656c369d7fcSMarek Vasut * WARNING 1657c369d7fcSMarek Vasut * ======= 1658c369d7fcSMarek Vasut * KSZ8794 is similar to KSZ8795, except the port map 1659c369d7fcSMarek Vasut * contains a gap between external and CPU ports, the 1660c369d7fcSMarek Vasut * port map is NOT continuous. The per-port register 1661c369d7fcSMarek Vasut * map is shifted accordingly too, i.e. registers at 1662c369d7fcSMarek Vasut * offset 0x40 are NOT used on KSZ8794 and they ARE 1663c369d7fcSMarek Vasut * used on KSZ8795 for external port 3. 1664c369d7fcSMarek Vasut * external cpu 1665c369d7fcSMarek Vasut * KSZ8794 0,1,2 4 1666c369d7fcSMarek Vasut * KSZ8795 0,1,2,3 4 1667c369d7fcSMarek Vasut * KSZ8765 0,1,2,3 4 1668c369d7fcSMarek Vasut */ 1669e66f840cSTristram Ha .chip_id = 0x8794, 1670e66f840cSTristram Ha .dev_name = "KSZ8794", 1671e66f840cSTristram Ha .num_vlans = 4096, 1672e66f840cSTristram Ha .num_alus = 0, 1673e66f840cSTristram Ha .num_statics = 8, 1674e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 167594374dd1SMichael Grzeschik .port_cnt = 4, /* total cpu and user ports */ 1676e66f840cSTristram Ha }, 1677e66f840cSTristram Ha { 1678e66f840cSTristram Ha .chip_id = 0x8765, 1679e66f840cSTristram Ha .dev_name = "KSZ8765", 1680e66f840cSTristram Ha .num_vlans = 4096, 1681e66f840cSTristram Ha .num_alus = 0, 1682e66f840cSTristram Ha .num_statics = 8, 1683e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 168494374dd1SMichael Grzeschik .port_cnt = 5, /* total cpu and user ports */ 1685e66f840cSTristram Ha }, 16864b20a07eSOleksij Rempel { 16874b20a07eSOleksij Rempel .chip_id = 0x8830, 16884b20a07eSOleksij Rempel .dev_name = "KSZ8863/KSZ8873", 16894b20a07eSOleksij Rempel .num_vlans = 16, 16904b20a07eSOleksij Rempel .num_alus = 0, 16914b20a07eSOleksij Rempel .num_statics = 8, 16924b20a07eSOleksij Rempel .cpu_ports = 0x4, /* can be configured as cpu port */ 16934b20a07eSOleksij Rempel .port_cnt = 3, 16944b20a07eSOleksij Rempel }, 1695e66f840cSTristram Ha }; 1696e66f840cSTristram Ha 16974b5baca0SMichael Grzeschik static int ksz8_switch_init(struct ksz_device *dev) 1698e66f840cSTristram Ha { 16999f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 1700e66f840cSTristram Ha int i; 1701e66f840cSTristram Ha 17024b5baca0SMichael Grzeschik dev->ds->ops = &ksz8_switch_ops; 1703e66f840cSTristram Ha 17044b5baca0SMichael Grzeschik for (i = 0; i < ARRAY_SIZE(ksz8_switch_chips); i++) { 17054b5baca0SMichael Grzeschik const struct ksz_chip_data *chip = &ksz8_switch_chips[i]; 1706e66f840cSTristram Ha 1707e66f840cSTristram Ha if (dev->chip_id == chip->chip_id) { 1708e66f840cSTristram Ha dev->name = chip->dev_name; 1709e66f840cSTristram Ha dev->num_vlans = chip->num_vlans; 1710e66f840cSTristram Ha dev->num_alus = chip->num_alus; 1711e66f840cSTristram Ha dev->num_statics = chip->num_statics; 1712c369d7fcSMarek Vasut dev->port_cnt = fls(chip->cpu_ports); 1713c369d7fcSMarek Vasut dev->cpu_port = fls(chip->cpu_ports) - 1; 1714c369d7fcSMarek Vasut dev->phy_port_cnt = dev->port_cnt - 1; 1715e66f840cSTristram Ha dev->cpu_ports = chip->cpu_ports; 1716c369d7fcSMarek Vasut dev->host_mask = chip->cpu_ports; 1717c369d7fcSMarek Vasut dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | 1718c369d7fcSMarek Vasut chip->cpu_ports; 1719e66f840cSTristram Ha break; 1720e66f840cSTristram Ha } 1721e66f840cSTristram Ha } 1722e66f840cSTristram Ha 1723e66f840cSTristram Ha /* no switch found */ 1724e66f840cSTristram Ha if (!dev->cpu_ports) 1725e66f840cSTristram Ha return -ENODEV; 1726e66f840cSTristram Ha 17274b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 17284b20a07eSOleksij Rempel ksz8->regs = ksz8863_regs; 17294b20a07eSOleksij Rempel ksz8->masks = ksz8863_masks; 17304b20a07eSOleksij Rempel ksz8->shifts = ksz8863_shifts; 17314b20a07eSOleksij Rempel dev->mib_cnt = ARRAY_SIZE(ksz88xx_mib_names); 17324b20a07eSOleksij Rempel dev->mib_names = ksz88xx_mib_names; 17334b20a07eSOleksij Rempel } else { 17349f73e112SMichael Grzeschik ksz8->regs = ksz8795_regs; 17359f73e112SMichael Grzeschik ksz8->masks = ksz8795_masks; 17369f73e112SMichael Grzeschik ksz8->shifts = ksz8795_shifts; 17374b20a07eSOleksij Rempel dev->mib_cnt = ARRAY_SIZE(ksz87xx_mib_names); 17384b20a07eSOleksij Rempel dev->mib_names = ksz87xx_mib_names; 17394b20a07eSOleksij Rempel } 17409f73e112SMichael Grzeschik 17414b20a07eSOleksij Rempel dev->reg_mib_cnt = MIB_COUNTER_NUM; 1742e66f840cSTristram Ha 1743c9f4633bSMichael Grzeschik dev->ports = devm_kzalloc(dev->dev, 1744c9f4633bSMichael Grzeschik dev->port_cnt * sizeof(struct ksz_port), 1745e66f840cSTristram Ha GFP_KERNEL); 1746e66f840cSTristram Ha if (!dev->ports) 1747e66f840cSTristram Ha return -ENOMEM; 1748c9f4633bSMichael Grzeschik for (i = 0; i < dev->port_cnt; i++) { 1749e66f840cSTristram Ha mutex_init(&dev->ports[i].mib.cnt_mutex); 1750e66f840cSTristram Ha dev->ports[i].mib.counters = 1751e66f840cSTristram Ha devm_kzalloc(dev->dev, 1752e66f840cSTristram Ha sizeof(u64) * 175365fe1acfSMichael Grzeschik (dev->mib_cnt + 1), 1754e66f840cSTristram Ha GFP_KERNEL); 1755e66f840cSTristram Ha if (!dev->ports[i].mib.counters) 1756e66f840cSTristram Ha return -ENOMEM; 1757e66f840cSTristram Ha } 1758e66f840cSTristram Ha 1759af199a1aSCodrin Ciubotariu /* set the real number of ports */ 176094374dd1SMichael Grzeschik dev->ds->num_ports = dev->port_cnt; 1761af199a1aSCodrin Ciubotariu 17629130c2d3SBen Hutchings /* We rely on software untagging on the CPU port, so that we 17639130c2d3SBen Hutchings * can support both tagged and untagged VLANs 17649130c2d3SBen Hutchings */ 17659130c2d3SBen Hutchings dev->ds->untag_bridge_pvid = true; 17669130c2d3SBen Hutchings 176716484413SBen Hutchings /* VLAN filtering is partly controlled by the global VLAN 176816484413SBen Hutchings * Enable flag 176916484413SBen Hutchings */ 177016484413SBen Hutchings dev->ds->vlan_filtering_is_global = true; 177116484413SBen Hutchings 1772e66f840cSTristram Ha return 0; 1773e66f840cSTristram Ha } 1774e66f840cSTristram Ha 17754b5baca0SMichael Grzeschik static void ksz8_switch_exit(struct ksz_device *dev) 1776e66f840cSTristram Ha { 17774b5baca0SMichael Grzeschik ksz8_reset_switch(dev); 1778e66f840cSTristram Ha } 1779e66f840cSTristram Ha 17804b5baca0SMichael Grzeschik static const struct ksz_dev_ops ksz8_dev_ops = { 17814b5baca0SMichael Grzeschik .get_port_addr = ksz8_get_port_addr, 17824b5baca0SMichael Grzeschik .cfg_port_member = ksz8_cfg_port_member, 17834b5baca0SMichael Grzeschik .flush_dyn_mac_table = ksz8_flush_dyn_mac_table, 17844b5baca0SMichael Grzeschik .port_setup = ksz8_port_setup, 17854b5baca0SMichael Grzeschik .r_phy = ksz8_r_phy, 17864b5baca0SMichael Grzeschik .w_phy = ksz8_w_phy, 17874b5baca0SMichael Grzeschik .r_dyn_mac_table = ksz8_r_dyn_mac_table, 17884b5baca0SMichael Grzeschik .r_sta_mac_table = ksz8_r_sta_mac_table, 17894b5baca0SMichael Grzeschik .w_sta_mac_table = ksz8_w_sta_mac_table, 17904b5baca0SMichael Grzeschik .r_mib_cnt = ksz8_r_mib_cnt, 17914b5baca0SMichael Grzeschik .r_mib_pkt = ksz8_r_mib_pkt, 17924b5baca0SMichael Grzeschik .freeze_mib = ksz8_freeze_mib, 17934b5baca0SMichael Grzeschik .port_init_cnt = ksz8_port_init_cnt, 17944b5baca0SMichael Grzeschik .shutdown = ksz8_reset_switch, 17954b5baca0SMichael Grzeschik .detect = ksz8_switch_detect, 17964b5baca0SMichael Grzeschik .init = ksz8_switch_init, 17974b5baca0SMichael Grzeschik .exit = ksz8_switch_exit, 1798e66f840cSTristram Ha }; 1799e66f840cSTristram Ha 18004b5baca0SMichael Grzeschik int ksz8_switch_register(struct ksz_device *dev) 1801e66f840cSTristram Ha { 18024b5baca0SMichael Grzeschik return ksz_switch_register(dev, &ksz8_dev_ops); 1803e66f840cSTristram Ha } 18044b5baca0SMichael Grzeschik EXPORT_SYMBOL(ksz8_switch_register); 1805e66f840cSTristram Ha 1806e66f840cSTristram Ha MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>"); 1807e66f840cSTristram Ha MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver"); 1808e66f840cSTristram Ha MODULE_LICENSE("GPL"); 1809