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> 13b6459415SJakub Kicinski #include <linux/if_vlan.h> 14e66f840cSTristram Ha #include <linux/kernel.h> 15e66f840cSTristram Ha #include <linux/module.h> 16e66f840cSTristram Ha #include <linux/platform_data/microchip-ksz.h> 17e66f840cSTristram Ha #include <linux/phy.h> 18e66f840cSTristram Ha #include <linux/etherdevice.h> 19e66f840cSTristram Ha #include <linux/if_bridge.h> 20ec4b94f9SMichael Grzeschik #include <linux/micrel_phy.h> 21e66f840cSTristram Ha #include <net/dsa.h> 22e66f840cSTristram Ha #include <net/switchdev.h> 232c709e0bSMichael Grzeschik #include <linux/phylink.h> 24e66f840cSTristram Ha 25e66f840cSTristram Ha #include "ksz_common.h" 26e66f840cSTristram Ha #include "ksz8795_reg.h" 279f73e112SMichael Grzeschik #include "ksz8.h" 289f73e112SMichael Grzeschik 299f73e112SMichael Grzeschik static const u8 ksz8795_regs[] = { 309f73e112SMichael Grzeschik [REG_IND_CTRL_0] = 0x6E, 319f73e112SMichael Grzeschik [REG_IND_DATA_8] = 0x70, 329f73e112SMichael Grzeschik [REG_IND_DATA_CHECK] = 0x72, 339f73e112SMichael Grzeschik [REG_IND_DATA_HI] = 0x71, 349f73e112SMichael Grzeschik [REG_IND_DATA_LO] = 0x75, 359f73e112SMichael Grzeschik [REG_IND_MIB_CHECK] = 0x74, 367b6e6235SOleksij Rempel [REG_IND_BYTE] = 0xA0, 379f73e112SMichael Grzeschik [P_FORCE_CTRL] = 0x0C, 389f73e112SMichael Grzeschik [P_LINK_STATUS] = 0x0E, 399f73e112SMichael Grzeschik [P_LOCAL_CTRL] = 0x07, 409f73e112SMichael Grzeschik [P_NEG_RESTART_CTRL] = 0x0D, 419f73e112SMichael Grzeschik [P_REMOTE_STATUS] = 0x08, 429f73e112SMichael Grzeschik [P_SPEED_STATUS] = 0x09, 439f73e112SMichael Grzeschik [S_TAIL_TAG_CTRL] = 0x0C, 449f73e112SMichael Grzeschik }; 459f73e112SMichael Grzeschik 469f73e112SMichael Grzeschik static const u32 ksz8795_masks[] = { 479f73e112SMichael Grzeschik [PORT_802_1P_REMAPPING] = BIT(7), 489f73e112SMichael Grzeschik [SW_TAIL_TAG_ENABLE] = BIT(1), 499f73e112SMichael Grzeschik [MIB_COUNTER_OVERFLOW] = BIT(6), 509f73e112SMichael Grzeschik [MIB_COUNTER_VALID] = BIT(5), 519f73e112SMichael Grzeschik [VLAN_TABLE_FID] = GENMASK(6, 0), 529f73e112SMichael Grzeschik [VLAN_TABLE_MEMBERSHIP] = GENMASK(11, 7), 539f73e112SMichael Grzeschik [VLAN_TABLE_VALID] = BIT(12), 549f73e112SMichael Grzeschik [STATIC_MAC_TABLE_VALID] = BIT(21), 559f73e112SMichael Grzeschik [STATIC_MAC_TABLE_USE_FID] = BIT(23), 569f73e112SMichael Grzeschik [STATIC_MAC_TABLE_FID] = GENMASK(30, 24), 579f73e112SMichael Grzeschik [STATIC_MAC_TABLE_OVERRIDE] = BIT(26), 589f73e112SMichael Grzeschik [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(24, 20), 599f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(6, 0), 609f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(8), 619f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7), 629f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 29), 639f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_FID] = GENMASK(26, 20), 649f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(26, 24), 659f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(28, 27), 669f73e112SMichael Grzeschik }; 679f73e112SMichael Grzeschik 689f73e112SMichael Grzeschik static const u8 ksz8795_shifts[] = { 699f73e112SMichael Grzeschik [VLAN_TABLE_MEMBERSHIP_S] = 7, 709f73e112SMichael Grzeschik [VLAN_TABLE] = 16, 719f73e112SMichael Grzeschik [STATIC_MAC_FWD_PORTS] = 16, 729f73e112SMichael Grzeschik [STATIC_MAC_FID] = 24, 739f73e112SMichael Grzeschik [DYNAMIC_MAC_ENTRIES_H] = 3, 749f73e112SMichael Grzeschik [DYNAMIC_MAC_ENTRIES] = 29, 759f73e112SMichael Grzeschik [DYNAMIC_MAC_FID] = 16, 769f73e112SMichael Grzeschik [DYNAMIC_MAC_TIMESTAMP] = 27, 779f73e112SMichael Grzeschik [DYNAMIC_MAC_SRC_PORT] = 24, 789f73e112SMichael Grzeschik }; 79e66f840cSTristram Ha 804b20a07eSOleksij Rempel static const u8 ksz8863_regs[] = { 814b20a07eSOleksij Rempel [REG_IND_CTRL_0] = 0x79, 824b20a07eSOleksij Rempel [REG_IND_DATA_8] = 0x7B, 834b20a07eSOleksij Rempel [REG_IND_DATA_CHECK] = 0x7B, 844b20a07eSOleksij Rempel [REG_IND_DATA_HI] = 0x7C, 854b20a07eSOleksij Rempel [REG_IND_DATA_LO] = 0x80, 864b20a07eSOleksij Rempel [REG_IND_MIB_CHECK] = 0x80, 874b20a07eSOleksij Rempel [P_FORCE_CTRL] = 0x0C, 884b20a07eSOleksij Rempel [P_LINK_STATUS] = 0x0E, 894b20a07eSOleksij Rempel [P_LOCAL_CTRL] = 0x0C, 904b20a07eSOleksij Rempel [P_NEG_RESTART_CTRL] = 0x0D, 914b20a07eSOleksij Rempel [P_REMOTE_STATUS] = 0x0E, 924b20a07eSOleksij Rempel [P_SPEED_STATUS] = 0x0F, 934b20a07eSOleksij Rempel [S_TAIL_TAG_CTRL] = 0x03, 944b20a07eSOleksij Rempel }; 954b20a07eSOleksij Rempel 964b20a07eSOleksij Rempel static const u32 ksz8863_masks[] = { 974b20a07eSOleksij Rempel [PORT_802_1P_REMAPPING] = BIT(3), 984b20a07eSOleksij Rempel [SW_TAIL_TAG_ENABLE] = BIT(6), 994b20a07eSOleksij Rempel [MIB_COUNTER_OVERFLOW] = BIT(7), 1004b20a07eSOleksij Rempel [MIB_COUNTER_VALID] = BIT(6), 1014b20a07eSOleksij Rempel [VLAN_TABLE_FID] = GENMASK(15, 12), 1024b20a07eSOleksij Rempel [VLAN_TABLE_MEMBERSHIP] = GENMASK(18, 16), 1034b20a07eSOleksij Rempel [VLAN_TABLE_VALID] = BIT(19), 1044b20a07eSOleksij Rempel [STATIC_MAC_TABLE_VALID] = BIT(19), 1054b20a07eSOleksij Rempel [STATIC_MAC_TABLE_USE_FID] = BIT(21), 1064b20a07eSOleksij Rempel [STATIC_MAC_TABLE_FID] = GENMASK(29, 26), 1074b20a07eSOleksij Rempel [STATIC_MAC_TABLE_OVERRIDE] = BIT(20), 1084b20a07eSOleksij Rempel [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(18, 16), 1094b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(5, 0), 1104b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(7), 1114b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7), 1124b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 28), 1134b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_FID] = GENMASK(19, 16), 1144b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(21, 20), 1154b20a07eSOleksij Rempel [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(23, 22), 1164b20a07eSOleksij Rempel }; 1174b20a07eSOleksij Rempel 1184b20a07eSOleksij Rempel static u8 ksz8863_shifts[] = { 1194b20a07eSOleksij Rempel [VLAN_TABLE_MEMBERSHIP_S] = 16, 1204b20a07eSOleksij Rempel [STATIC_MAC_FWD_PORTS] = 16, 1214b20a07eSOleksij Rempel [STATIC_MAC_FID] = 22, 1224b20a07eSOleksij Rempel [DYNAMIC_MAC_ENTRIES_H] = 3, 1234b20a07eSOleksij Rempel [DYNAMIC_MAC_ENTRIES] = 24, 1244b20a07eSOleksij Rempel [DYNAMIC_MAC_FID] = 16, 1254b20a07eSOleksij Rempel [DYNAMIC_MAC_TIMESTAMP] = 24, 1264b20a07eSOleksij Rempel [DYNAMIC_MAC_SRC_PORT] = 20, 1274b20a07eSOleksij Rempel }; 1284b20a07eSOleksij Rempel 1294b20a07eSOleksij Rempel struct mib_names { 130e66f840cSTristram Ha char string[ETH_GSTRING_LEN]; 1314b20a07eSOleksij Rempel }; 1324b20a07eSOleksij Rempel 1334b20a07eSOleksij Rempel static const struct mib_names ksz87xx_mib_names[] = { 134e66f840cSTristram Ha { "rx_hi" }, 135e66f840cSTristram Ha { "rx_undersize" }, 136e66f840cSTristram Ha { "rx_fragments" }, 137e66f840cSTristram Ha { "rx_oversize" }, 138e66f840cSTristram Ha { "rx_jabbers" }, 139e66f840cSTristram Ha { "rx_symbol_err" }, 140e66f840cSTristram Ha { "rx_crc_err" }, 141e66f840cSTristram Ha { "rx_align_err" }, 142e66f840cSTristram Ha { "rx_mac_ctrl" }, 143e66f840cSTristram Ha { "rx_pause" }, 144e66f840cSTristram Ha { "rx_bcast" }, 145e66f840cSTristram Ha { "rx_mcast" }, 146e66f840cSTristram Ha { "rx_ucast" }, 147e66f840cSTristram Ha { "rx_64_or_less" }, 148e66f840cSTristram Ha { "rx_65_127" }, 149e66f840cSTristram Ha { "rx_128_255" }, 150e66f840cSTristram Ha { "rx_256_511" }, 151e66f840cSTristram Ha { "rx_512_1023" }, 152e66f840cSTristram Ha { "rx_1024_1522" }, 153e66f840cSTristram Ha { "rx_1523_2000" }, 154e66f840cSTristram Ha { "rx_2001" }, 155e66f840cSTristram Ha { "tx_hi" }, 156e66f840cSTristram Ha { "tx_late_col" }, 157e66f840cSTristram Ha { "tx_pause" }, 158e66f840cSTristram Ha { "tx_bcast" }, 159e66f840cSTristram Ha { "tx_mcast" }, 160e66f840cSTristram Ha { "tx_ucast" }, 161e66f840cSTristram Ha { "tx_deferred" }, 162e66f840cSTristram Ha { "tx_total_col" }, 163e66f840cSTristram Ha { "tx_exc_col" }, 164e66f840cSTristram Ha { "tx_single_col" }, 165e66f840cSTristram Ha { "tx_mult_col" }, 166e66f840cSTristram Ha { "rx_total" }, 167e66f840cSTristram Ha { "tx_total" }, 168e66f840cSTristram Ha { "rx_discards" }, 169e66f840cSTristram Ha { "tx_discards" }, 170e66f840cSTristram Ha }; 171e66f840cSTristram Ha 1724b20a07eSOleksij Rempel static const struct mib_names ksz88xx_mib_names[] = { 1734b20a07eSOleksij Rempel { "rx" }, 1744b20a07eSOleksij Rempel { "rx_hi" }, 1754b20a07eSOleksij Rempel { "rx_undersize" }, 1764b20a07eSOleksij Rempel { "rx_fragments" }, 1774b20a07eSOleksij Rempel { "rx_oversize" }, 1784b20a07eSOleksij Rempel { "rx_jabbers" }, 1794b20a07eSOleksij Rempel { "rx_symbol_err" }, 1804b20a07eSOleksij Rempel { "rx_crc_err" }, 1814b20a07eSOleksij Rempel { "rx_align_err" }, 1824b20a07eSOleksij Rempel { "rx_mac_ctrl" }, 1834b20a07eSOleksij Rempel { "rx_pause" }, 1844b20a07eSOleksij Rempel { "rx_bcast" }, 1854b20a07eSOleksij Rempel { "rx_mcast" }, 1864b20a07eSOleksij Rempel { "rx_ucast" }, 1874b20a07eSOleksij Rempel { "rx_64_or_less" }, 1884b20a07eSOleksij Rempel { "rx_65_127" }, 1894b20a07eSOleksij Rempel { "rx_128_255" }, 1904b20a07eSOleksij Rempel { "rx_256_511" }, 1914b20a07eSOleksij Rempel { "rx_512_1023" }, 1924b20a07eSOleksij Rempel { "rx_1024_1522" }, 1934b20a07eSOleksij Rempel { "tx" }, 1944b20a07eSOleksij Rempel { "tx_hi" }, 1954b20a07eSOleksij Rempel { "tx_late_col" }, 1964b20a07eSOleksij Rempel { "tx_pause" }, 1974b20a07eSOleksij Rempel { "tx_bcast" }, 1984b20a07eSOleksij Rempel { "tx_mcast" }, 1994b20a07eSOleksij Rempel { "tx_ucast" }, 2004b20a07eSOleksij Rempel { "tx_deferred" }, 2014b20a07eSOleksij Rempel { "tx_total_col" }, 2024b20a07eSOleksij Rempel { "tx_exc_col" }, 2034b20a07eSOleksij Rempel { "tx_single_col" }, 2044b20a07eSOleksij Rempel { "tx_mult_col" }, 2054b20a07eSOleksij Rempel { "rx_discards" }, 2064b20a07eSOleksij Rempel { "tx_discards" }, 2074b20a07eSOleksij Rempel }; 2084b20a07eSOleksij Rempel 2094b20a07eSOleksij Rempel static bool ksz_is_ksz88x3(struct ksz_device *dev) 2104b20a07eSOleksij Rempel { 2114b20a07eSOleksij Rempel return dev->chip_id == 0x8830; 2124b20a07eSOleksij Rempel } 2134b20a07eSOleksij Rempel 214e66f840cSTristram Ha static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set) 215e66f840cSTristram Ha { 216e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0); 217e66f840cSTristram Ha } 218e66f840cSTristram Ha 219e66f840cSTristram Ha static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits, 220e66f840cSTristram Ha bool set) 221e66f840cSTristram Ha { 222e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset), 223e66f840cSTristram Ha bits, set ? bits : 0); 224e66f840cSTristram Ha } 225e66f840cSTristram Ha 2267b6e6235SOleksij Rempel static int ksz8_ind_write8(struct ksz_device *dev, u8 table, u16 addr, u8 data) 2277b6e6235SOleksij Rempel { 2287b6e6235SOleksij Rempel struct ksz8 *ksz8 = dev->priv; 2297b6e6235SOleksij Rempel const u8 *regs = ksz8->regs; 2307b6e6235SOleksij Rempel u16 ctrl_addr; 2317b6e6235SOleksij Rempel int ret = 0; 2327b6e6235SOleksij Rempel 2337b6e6235SOleksij Rempel mutex_lock(&dev->alu_mutex); 2347b6e6235SOleksij Rempel 2357b6e6235SOleksij Rempel ctrl_addr = IND_ACC_TABLE(table) | addr; 2367b6e6235SOleksij Rempel ret = ksz_write8(dev, regs[REG_IND_BYTE], data); 2377b6e6235SOleksij Rempel if (!ret) 2387b6e6235SOleksij Rempel ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 2397b6e6235SOleksij Rempel 2407b6e6235SOleksij Rempel mutex_unlock(&dev->alu_mutex); 2417b6e6235SOleksij Rempel 2427b6e6235SOleksij Rempel return ret; 2437b6e6235SOleksij Rempel } 2447b6e6235SOleksij Rempel 2454b5baca0SMichael Grzeschik static int ksz8_reset_switch(struct ksz_device *dev) 246e66f840cSTristram Ha { 2474b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 2484b20a07eSOleksij Rempel /* reset switch */ 2494b20a07eSOleksij Rempel ksz_cfg(dev, KSZ8863_REG_SW_RESET, 2504b20a07eSOleksij Rempel KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, true); 2514b20a07eSOleksij Rempel ksz_cfg(dev, KSZ8863_REG_SW_RESET, 2524b20a07eSOleksij Rempel KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, false); 2534b20a07eSOleksij Rempel } else { 254e66f840cSTristram Ha /* reset switch */ 255e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 256e66f840cSTristram Ha SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S); 257e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0); 2584b20a07eSOleksij Rempel } 259e66f840cSTristram Ha 260e66f840cSTristram Ha return 0; 261e66f840cSTristram Ha } 262e66f840cSTristram Ha 263e66f840cSTristram Ha static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue) 264e66f840cSTristram Ha { 265e66f840cSTristram Ha u8 hi, lo; 266e66f840cSTristram Ha 267e66f840cSTristram Ha /* Number of queues can only be 1, 2, or 4. */ 268e66f840cSTristram Ha switch (queue) { 269e66f840cSTristram Ha case 4: 270e66f840cSTristram Ha case 3: 271e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_4; 272e66f840cSTristram Ha break; 273e66f840cSTristram Ha case 2: 274e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_2; 275e66f840cSTristram Ha break; 276e66f840cSTristram Ha default: 277e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_1; 278e66f840cSTristram Ha } 279e66f840cSTristram Ha ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo); 280e66f840cSTristram Ha ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi); 281e66f840cSTristram Ha lo &= ~PORT_QUEUE_SPLIT_L; 282e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_2) 283e66f840cSTristram Ha lo |= PORT_QUEUE_SPLIT_L; 284e66f840cSTristram Ha hi &= ~PORT_QUEUE_SPLIT_H; 285e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_4) 286e66f840cSTristram Ha hi |= PORT_QUEUE_SPLIT_H; 287e66f840cSTristram Ha ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo); 288e66f840cSTristram Ha ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi); 289e66f840cSTristram Ha 290e66f840cSTristram Ha /* Default is port based for egress rate limit. */ 291e66f840cSTristram Ha if (queue != PORT_QUEUE_SPLIT_1) 292e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED, 293e66f840cSTristram Ha true); 294e66f840cSTristram Ha } 295e66f840cSTristram Ha 2964b5baca0SMichael Grzeschik static void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt) 297e66f840cSTristram Ha { 2989f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 2999f73e112SMichael Grzeschik const u32 *masks; 3009f73e112SMichael Grzeschik const u8 *regs; 301e66f840cSTristram Ha u16 ctrl_addr; 302e66f840cSTristram Ha u32 data; 303e66f840cSTristram Ha u8 check; 304e66f840cSTristram Ha int loop; 305e66f840cSTristram Ha 3069f73e112SMichael Grzeschik masks = ksz8->masks; 3079f73e112SMichael Grzeschik regs = ksz8->regs; 3089f73e112SMichael Grzeschik 30931b62c78SMichael Grzeschik ctrl_addr = addr + dev->reg_mib_cnt * port; 310e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 311e66f840cSTristram Ha 312e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 3139f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 314e66f840cSTristram Ha 315e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 316e66f840cSTristram Ha * slow SPI speed. 317e66f840cSTristram Ha */ 318e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 3199f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check); 320e66f840cSTristram Ha 3219f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_VALID]) { 3229f73e112SMichael Grzeschik ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 3239f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) 324e66f840cSTristram Ha *cnt += MIB_COUNTER_VALUE + 1; 325e66f840cSTristram Ha *cnt += data & MIB_COUNTER_VALUE; 326e66f840cSTristram Ha break; 327e66f840cSTristram Ha } 328e66f840cSTristram Ha } 329e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 330e66f840cSTristram Ha } 331e66f840cSTristram Ha 3324b20a07eSOleksij Rempel static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 333e66f840cSTristram Ha u64 *dropped, u64 *cnt) 334e66f840cSTristram Ha { 3359f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 3369f73e112SMichael Grzeschik const u32 *masks; 3379f73e112SMichael Grzeschik const u8 *regs; 338e66f840cSTristram Ha u16 ctrl_addr; 339e66f840cSTristram Ha u32 data; 340e66f840cSTristram Ha u8 check; 341e66f840cSTristram Ha int loop; 342e66f840cSTristram Ha 3439f73e112SMichael Grzeschik masks = ksz8->masks; 3449f73e112SMichael Grzeschik regs = ksz8->regs; 3459f73e112SMichael Grzeschik 34631b62c78SMichael Grzeschik addr -= dev->reg_mib_cnt; 3474b20a07eSOleksij Rempel ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port; 3484b20a07eSOleksij Rempel ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0; 349e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 350e66f840cSTristram Ha 351e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 3529f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 353e66f840cSTristram Ha 354e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 355e66f840cSTristram Ha * slow SPI speed. 356e66f840cSTristram Ha */ 357e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 3589f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check); 359e66f840cSTristram Ha 3609f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_VALID]) { 3619f73e112SMichael Grzeschik ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 362e66f840cSTristram Ha if (addr < 2) { 363e66f840cSTristram Ha u64 total; 364e66f840cSTristram Ha 365e66f840cSTristram Ha total = check & MIB_TOTAL_BYTES_H; 366e66f840cSTristram Ha total <<= 32; 367e66f840cSTristram Ha *cnt += total; 368e66f840cSTristram Ha *cnt += data; 3699f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) { 370e66f840cSTristram Ha total = MIB_TOTAL_BYTES_H + 1; 371e66f840cSTristram Ha total <<= 32; 372e66f840cSTristram Ha *cnt += total; 373e66f840cSTristram Ha } 374e66f840cSTristram Ha } else { 3759f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) 376e66f840cSTristram Ha *cnt += MIB_PACKET_DROPPED + 1; 377e66f840cSTristram Ha *cnt += data & MIB_PACKET_DROPPED; 378e66f840cSTristram Ha } 379e66f840cSTristram Ha break; 380e66f840cSTristram Ha } 381e66f840cSTristram Ha } 382e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 383e66f840cSTristram Ha } 384e66f840cSTristram Ha 3854b20a07eSOleksij Rempel static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 3864b20a07eSOleksij Rempel u64 *dropped, u64 *cnt) 3874b20a07eSOleksij Rempel { 3884b20a07eSOleksij Rempel struct ksz8 *ksz8 = dev->priv; 3894b20a07eSOleksij Rempel const u8 *regs = ksz8->regs; 3904b20a07eSOleksij Rempel u32 *last = (u32 *)dropped; 3914b20a07eSOleksij Rempel u16 ctrl_addr; 3924b20a07eSOleksij Rempel u32 data; 3934b20a07eSOleksij Rempel u32 cur; 3944b20a07eSOleksij Rempel 3954b20a07eSOleksij Rempel addr -= dev->reg_mib_cnt; 3964b20a07eSOleksij Rempel ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 : 3974b20a07eSOleksij Rempel KSZ8863_MIB_PACKET_DROPPED_RX_0; 3984b20a07eSOleksij Rempel ctrl_addr += port; 3994b20a07eSOleksij Rempel ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 4004b20a07eSOleksij Rempel 4014b20a07eSOleksij Rempel mutex_lock(&dev->alu_mutex); 4024b20a07eSOleksij Rempel ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 4034b20a07eSOleksij Rempel ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 4044b20a07eSOleksij Rempel mutex_unlock(&dev->alu_mutex); 4054b20a07eSOleksij Rempel 4064b20a07eSOleksij Rempel data &= MIB_PACKET_DROPPED; 4074b20a07eSOleksij Rempel cur = last[addr]; 4084b20a07eSOleksij Rempel if (data != cur) { 4094b20a07eSOleksij Rempel last[addr] = data; 4104b20a07eSOleksij Rempel if (data < cur) 4114b20a07eSOleksij Rempel data += MIB_PACKET_DROPPED + 1; 4124b20a07eSOleksij Rempel data -= cur; 4134b20a07eSOleksij Rempel *cnt += data; 4144b20a07eSOleksij Rempel } 4154b20a07eSOleksij Rempel } 4164b20a07eSOleksij Rempel 4174b20a07eSOleksij Rempel static void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 4184b20a07eSOleksij Rempel u64 *dropped, u64 *cnt) 4194b20a07eSOleksij Rempel { 4204b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 4214b20a07eSOleksij Rempel ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt); 4224b20a07eSOleksij Rempel else 4234b20a07eSOleksij Rempel ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt); 4244b20a07eSOleksij Rempel } 4254b20a07eSOleksij Rempel 4264b5baca0SMichael Grzeschik static void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze) 427e66f840cSTristram Ha { 4284b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 4294b20a07eSOleksij Rempel return; 4304b20a07eSOleksij Rempel 431e66f840cSTristram Ha /* enable the port for flush/freeze function */ 432e66f840cSTristram Ha if (freeze) 433e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 434e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze); 435e66f840cSTristram Ha 436e66f840cSTristram Ha /* disable the port after freeze is done */ 437e66f840cSTristram Ha if (!freeze) 438e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 439e66f840cSTristram Ha } 440e66f840cSTristram Ha 4414b5baca0SMichael Grzeschik static void ksz8_port_init_cnt(struct ksz_device *dev, int port) 442e66f840cSTristram Ha { 443e66f840cSTristram Ha struct ksz_port_mib *mib = &dev->ports[port].mib; 4444b20a07eSOleksij Rempel u64 *dropped; 445e66f840cSTristram Ha 4464b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) { 447e66f840cSTristram Ha /* flush all enabled port MIB counters */ 448e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 449e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true); 450e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 4514b20a07eSOleksij Rempel } 452e66f840cSTristram Ha 453e66f840cSTristram Ha mib->cnt_ptr = 0; 454e66f840cSTristram Ha 455e66f840cSTristram Ha /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */ 456e66f840cSTristram Ha while (mib->cnt_ptr < dev->reg_mib_cnt) { 457e66f840cSTristram Ha dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr, 458e66f840cSTristram Ha &mib->counters[mib->cnt_ptr]); 459e66f840cSTristram Ha ++mib->cnt_ptr; 460e66f840cSTristram Ha } 461e66f840cSTristram Ha 4624b20a07eSOleksij Rempel /* last one in storage */ 4634b20a07eSOleksij Rempel dropped = &mib->counters[dev->mib_cnt]; 4644b20a07eSOleksij Rempel 465e66f840cSTristram Ha /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */ 466e66f840cSTristram Ha while (mib->cnt_ptr < dev->mib_cnt) { 467e66f840cSTristram Ha dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr, 4684b20a07eSOleksij Rempel dropped, &mib->counters[mib->cnt_ptr]); 469e66f840cSTristram Ha ++mib->cnt_ptr; 470e66f840cSTristram Ha } 471e66f840cSTristram Ha mib->cnt_ptr = 0; 472e66f840cSTristram Ha memset(mib->counters, 0, dev->mib_cnt * sizeof(u64)); 473e66f840cSTristram Ha } 474e66f840cSTristram Ha 4754b5baca0SMichael Grzeschik static void ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data) 476e66f840cSTristram Ha { 4779f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 4789f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 479e66f840cSTristram Ha u16 ctrl_addr; 480e66f840cSTristram Ha 481e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr; 482e66f840cSTristram Ha 483e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 4849f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 4859f73e112SMichael Grzeschik ksz_read64(dev, regs[REG_IND_DATA_HI], data); 486e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 487e66f840cSTristram Ha } 488e66f840cSTristram Ha 4894b5baca0SMichael Grzeschik static void ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data) 490e66f840cSTristram Ha { 4919f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 4929f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 493e66f840cSTristram Ha u16 ctrl_addr; 494e66f840cSTristram Ha 495e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table) | addr; 496e66f840cSTristram Ha 497e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 4989f73e112SMichael Grzeschik ksz_write64(dev, regs[REG_IND_DATA_HI], data); 4999f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 500e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 501e66f840cSTristram Ha } 502e66f840cSTristram Ha 5034b5baca0SMichael Grzeschik static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data) 504e66f840cSTristram Ha { 5059f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 506e66f840cSTristram Ha int timeout = 100; 5079f73e112SMichael Grzeschik const u32 *masks; 5089f73e112SMichael Grzeschik const u8 *regs; 5099f73e112SMichael Grzeschik 5109f73e112SMichael Grzeschik masks = ksz8->masks; 5119f73e112SMichael Grzeschik regs = ksz8->regs; 512e66f840cSTristram Ha 513e66f840cSTristram Ha do { 5149f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_DATA_CHECK], data); 515e66f840cSTristram Ha timeout--; 5169f73e112SMichael Grzeschik } while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout); 517e66f840cSTristram Ha 518e66f840cSTristram Ha /* Entry is not ready for accessing. */ 5199f73e112SMichael Grzeschik if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) { 520e66f840cSTristram Ha return -EAGAIN; 521e66f840cSTristram Ha /* Entry is ready for accessing. */ 522e66f840cSTristram Ha } else { 5239f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_DATA_8], data); 524e66f840cSTristram Ha 525e66f840cSTristram Ha /* There is no valid entry in the table. */ 5269f73e112SMichael Grzeschik if (*data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY]) 527e66f840cSTristram Ha return -ENXIO; 528e66f840cSTristram Ha } 529e66f840cSTristram Ha return 0; 530e66f840cSTristram Ha } 531e66f840cSTristram Ha 5324b5baca0SMichael Grzeschik static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, 533e66f840cSTristram Ha u8 *mac_addr, u8 *fid, u8 *src_port, 534e66f840cSTristram Ha u8 *timestamp, u16 *entries) 535e66f840cSTristram Ha { 5369f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 537e66f840cSTristram Ha u32 data_hi, data_lo; 5389f73e112SMichael Grzeschik const u8 *shifts; 5399f73e112SMichael Grzeschik const u32 *masks; 5409f73e112SMichael Grzeschik const u8 *regs; 541e66f840cSTristram Ha u16 ctrl_addr; 542e66f840cSTristram Ha u8 data; 543e66f840cSTristram Ha int rc; 544e66f840cSTristram Ha 5459f73e112SMichael Grzeschik shifts = ksz8->shifts; 5469f73e112SMichael Grzeschik masks = ksz8->masks; 5479f73e112SMichael Grzeschik regs = ksz8->regs; 5489f73e112SMichael Grzeschik 549e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr; 550e66f840cSTristram Ha 551e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 5529f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 553e66f840cSTristram Ha 5544b5baca0SMichael Grzeschik rc = ksz8_valid_dyn_entry(dev, &data); 555e66f840cSTristram Ha if (rc == -EAGAIN) { 556e66f840cSTristram Ha if (addr == 0) 557e66f840cSTristram Ha *entries = 0; 558e66f840cSTristram Ha } else if (rc == -ENXIO) { 559e66f840cSTristram Ha *entries = 0; 560e66f840cSTristram Ha /* At least one valid entry in the table. */ 561e66f840cSTristram Ha } else { 562e66f840cSTristram Ha u64 buf = 0; 563e66f840cSTristram Ha int cnt; 564e66f840cSTristram Ha 5659f73e112SMichael Grzeschik ksz_read64(dev, regs[REG_IND_DATA_HI], &buf); 566e66f840cSTristram Ha data_hi = (u32)(buf >> 32); 567e66f840cSTristram Ha data_lo = (u32)buf; 568e66f840cSTristram Ha 569e66f840cSTristram Ha /* Check out how many valid entry in the table. */ 5709f73e112SMichael Grzeschik cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H]; 5719f73e112SMichael Grzeschik cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H]; 5729f73e112SMichael Grzeschik cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >> 5739f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_ENTRIES]; 574e66f840cSTristram Ha *entries = cnt + 1; 575e66f840cSTristram Ha 5769f73e112SMichael Grzeschik *fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >> 5779f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_FID]; 5789f73e112SMichael Grzeschik *src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >> 5799f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_SRC_PORT]; 5809f73e112SMichael Grzeschik *timestamp = (data_hi & masks[DYNAMIC_MAC_TABLE_TIMESTAMP]) >> 5819f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_TIMESTAMP]; 582e66f840cSTristram Ha 583e66f840cSTristram Ha mac_addr[5] = (u8)data_lo; 584e66f840cSTristram Ha mac_addr[4] = (u8)(data_lo >> 8); 585e66f840cSTristram Ha mac_addr[3] = (u8)(data_lo >> 16); 586e66f840cSTristram Ha mac_addr[2] = (u8)(data_lo >> 24); 587e66f840cSTristram Ha 588e66f840cSTristram Ha mac_addr[1] = (u8)data_hi; 589e66f840cSTristram Ha mac_addr[0] = (u8)(data_hi >> 8); 590e66f840cSTristram Ha rc = 0; 591e66f840cSTristram Ha } 592e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 593e66f840cSTristram Ha 594e66f840cSTristram Ha return rc; 595e66f840cSTristram Ha } 596e66f840cSTristram Ha 5974b5baca0SMichael Grzeschik static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr, 598e66f840cSTristram Ha struct alu_struct *alu) 599e66f840cSTristram Ha { 6009f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 601e66f840cSTristram Ha u32 data_hi, data_lo; 6029f73e112SMichael Grzeschik const u8 *shifts; 6039f73e112SMichael Grzeschik const u32 *masks; 604e66f840cSTristram Ha u64 data; 605e66f840cSTristram Ha 6069f73e112SMichael Grzeschik shifts = ksz8->shifts; 6079f73e112SMichael Grzeschik masks = ksz8->masks; 6089f73e112SMichael Grzeschik 6094b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data); 610e66f840cSTristram Ha data_hi = data >> 32; 611e66f840cSTristram Ha data_lo = (u32)data; 6129f73e112SMichael Grzeschik if (data_hi & (masks[STATIC_MAC_TABLE_VALID] | 6139f73e112SMichael Grzeschik masks[STATIC_MAC_TABLE_OVERRIDE])) { 614e66f840cSTristram Ha alu->mac[5] = (u8)data_lo; 615e66f840cSTristram Ha alu->mac[4] = (u8)(data_lo >> 8); 616e66f840cSTristram Ha alu->mac[3] = (u8)(data_lo >> 16); 617e66f840cSTristram Ha alu->mac[2] = (u8)(data_lo >> 24); 618e66f840cSTristram Ha alu->mac[1] = (u8)data_hi; 619e66f840cSTristram Ha alu->mac[0] = (u8)(data_hi >> 8); 6209f73e112SMichael Grzeschik alu->port_forward = 6219f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >> 6229f73e112SMichael Grzeschik shifts[STATIC_MAC_FWD_PORTS]; 623e66f840cSTristram Ha alu->is_override = 6249f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0; 625e66f840cSTristram Ha data_hi >>= 1; 6269f73e112SMichael Grzeschik alu->is_static = true; 6279f73e112SMichael Grzeschik alu->is_use_fid = 6289f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0; 6299f73e112SMichael Grzeschik alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >> 6309f73e112SMichael Grzeschik shifts[STATIC_MAC_FID]; 631e66f840cSTristram Ha return 0; 632e66f840cSTristram Ha } 633e66f840cSTristram Ha return -ENXIO; 634e66f840cSTristram Ha } 635e66f840cSTristram Ha 6364b5baca0SMichael Grzeschik static void ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr, 637e66f840cSTristram Ha struct alu_struct *alu) 638e66f840cSTristram Ha { 6399f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 640e66f840cSTristram Ha u32 data_hi, data_lo; 6419f73e112SMichael Grzeschik const u8 *shifts; 6429f73e112SMichael Grzeschik const u32 *masks; 643e66f840cSTristram Ha u64 data; 644e66f840cSTristram Ha 6459f73e112SMichael Grzeschik shifts = ksz8->shifts; 6469f73e112SMichael Grzeschik masks = ksz8->masks; 6479f73e112SMichael Grzeschik 648e66f840cSTristram Ha data_lo = ((u32)alu->mac[2] << 24) | 649e66f840cSTristram Ha ((u32)alu->mac[3] << 16) | 650e66f840cSTristram Ha ((u32)alu->mac[4] << 8) | alu->mac[5]; 651e66f840cSTristram Ha data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1]; 6529f73e112SMichael Grzeschik data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS]; 653e66f840cSTristram Ha 654e66f840cSTristram Ha if (alu->is_override) 6559f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE]; 656e66f840cSTristram Ha if (alu->is_use_fid) { 6579f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_USE_FID]; 6589f73e112SMichael Grzeschik data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID]; 659e66f840cSTristram Ha } 660e66f840cSTristram Ha if (alu->is_static) 6619f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_VALID]; 662e66f840cSTristram Ha else 6639f73e112SMichael Grzeschik data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE]; 664e66f840cSTristram Ha 665e66f840cSTristram Ha data = (u64)data_hi << 32 | data_lo; 6664b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data); 667e66f840cSTristram Ha } 668e66f840cSTristram Ha 6699f73e112SMichael Grzeschik static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid, 6709f73e112SMichael Grzeschik u8 *member, u8 *valid) 671e66f840cSTristram Ha { 6729f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 6739f73e112SMichael Grzeschik const u8 *shifts; 6749f73e112SMichael Grzeschik const u32 *masks; 6759f73e112SMichael Grzeschik 6769f73e112SMichael Grzeschik shifts = ksz8->shifts; 6779f73e112SMichael Grzeschik masks = ksz8->masks; 6789f73e112SMichael Grzeschik 6799f73e112SMichael Grzeschik *fid = vlan & masks[VLAN_TABLE_FID]; 6809f73e112SMichael Grzeschik *member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >> 6819f73e112SMichael Grzeschik shifts[VLAN_TABLE_MEMBERSHIP_S]; 6829f73e112SMichael Grzeschik *valid = !!(vlan & masks[VLAN_TABLE_VALID]); 683e66f840cSTristram Ha } 684e66f840cSTristram Ha 6859f73e112SMichael Grzeschik static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid, 6869f73e112SMichael Grzeschik u16 *vlan) 687e66f840cSTristram Ha { 6889f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 6899f73e112SMichael Grzeschik const u8 *shifts; 6909f73e112SMichael Grzeschik const u32 *masks; 6919f73e112SMichael Grzeschik 6929f73e112SMichael Grzeschik shifts = ksz8->shifts; 6939f73e112SMichael Grzeschik masks = ksz8->masks; 6949f73e112SMichael Grzeschik 695e66f840cSTristram Ha *vlan = fid; 6969f73e112SMichael Grzeschik *vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S]; 697e66f840cSTristram Ha if (valid) 6989f73e112SMichael Grzeschik *vlan |= masks[VLAN_TABLE_VALID]; 699e66f840cSTristram Ha } 700e66f840cSTristram Ha 7014b5baca0SMichael Grzeschik static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr) 702e66f840cSTristram Ha { 7039f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 7049f73e112SMichael Grzeschik const u8 *shifts; 705e66f840cSTristram Ha u64 data; 706e66f840cSTristram Ha int i; 707e66f840cSTristram Ha 7089f73e112SMichael Grzeschik shifts = ksz8->shifts; 7099f73e112SMichael Grzeschik 7104b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &data); 711411d466dSBen Hutchings addr *= 4; 712411d466dSBen Hutchings for (i = 0; i < 4; i++) { 713e66f840cSTristram Ha dev->vlan_cache[addr + i].table[0] = (u16)data; 7149f73e112SMichael Grzeschik data >>= shifts[VLAN_TABLE]; 715e66f840cSTristram Ha } 716e66f840cSTristram Ha } 717e66f840cSTristram Ha 7184b5baca0SMichael Grzeschik static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan) 719e66f840cSTristram Ha { 720e66f840cSTristram Ha int index; 721e66f840cSTristram Ha u16 *data; 722e66f840cSTristram Ha u16 addr; 723e66f840cSTristram Ha u64 buf; 724e66f840cSTristram Ha 725e66f840cSTristram Ha data = (u16 *)&buf; 726411d466dSBen Hutchings addr = vid / 4; 727e66f840cSTristram Ha index = vid & 3; 7284b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 729e66f840cSTristram Ha *vlan = data[index]; 730e66f840cSTristram Ha } 731e66f840cSTristram Ha 7324b5baca0SMichael Grzeschik static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan) 733e66f840cSTristram Ha { 734e66f840cSTristram Ha int index; 735e66f840cSTristram Ha u16 *data; 736e66f840cSTristram Ha u16 addr; 737e66f840cSTristram Ha u64 buf; 738e66f840cSTristram Ha 739e66f840cSTristram Ha data = (u16 *)&buf; 740411d466dSBen Hutchings addr = vid / 4; 741e66f840cSTristram Ha index = vid & 3; 7424b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 743e66f840cSTristram Ha data[index] = vlan; 744e66f840cSTristram Ha dev->vlan_cache[vid].table[0] = vlan; 7454b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_VLAN, addr, buf); 746e66f840cSTristram Ha } 747e66f840cSTristram Ha 7484b5baca0SMichael Grzeschik static void ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val) 749e66f840cSTristram Ha { 7509f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 751e66f840cSTristram Ha u8 restart, speed, ctrl, link; 7529f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 753e66f840cSTristram Ha int processed = true; 75436838050SOleksij Rempel u8 val1, val2; 755e66f840cSTristram Ha u16 data = 0; 756e66f840cSTristram Ha u8 p = phy; 757e66f840cSTristram Ha 758e66f840cSTristram Ha switch (reg) { 759ec4b94f9SMichael Grzeschik case MII_BMCR: 7609f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart); 7619f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed); 7629f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl); 763e66f840cSTristram Ha if (restart & PORT_PHY_LOOPBACK) 764ec4b94f9SMichael Grzeschik data |= BMCR_LOOPBACK; 765e66f840cSTristram Ha if (ctrl & PORT_FORCE_100_MBIT) 766ec4b94f9SMichael Grzeschik data |= BMCR_SPEED100; 7674b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 7684b20a07eSOleksij Rempel if ((ctrl & PORT_AUTO_NEG_ENABLE)) 769ec4b94f9SMichael Grzeschik data |= BMCR_ANENABLE; 7704b20a07eSOleksij Rempel } else { 771e66f840cSTristram Ha if (!(ctrl & PORT_AUTO_NEG_DISABLE)) 772ec4b94f9SMichael Grzeschik data |= BMCR_ANENABLE; 7734b20a07eSOleksij Rempel } 774e66f840cSTristram Ha if (restart & PORT_POWER_DOWN) 775ec4b94f9SMichael Grzeschik data |= BMCR_PDOWN; 776e66f840cSTristram Ha if (restart & PORT_AUTO_NEG_RESTART) 777ec4b94f9SMichael Grzeschik data |= BMCR_ANRESTART; 778e66f840cSTristram Ha if (ctrl & PORT_FORCE_FULL_DUPLEX) 779ec4b94f9SMichael Grzeschik data |= BMCR_FULLDPLX; 780e66f840cSTristram Ha if (speed & PORT_HP_MDIX) 781ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_HP_MDIX; 782e66f840cSTristram Ha if (restart & PORT_FORCE_MDIX) 783ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_FORCE_MDI; 784e66f840cSTristram Ha if (restart & PORT_AUTO_MDIX_DISABLE) 785ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_AUTO_MDIX; 786e66f840cSTristram Ha if (restart & PORT_TX_DISABLE) 787ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_TRANSMIT; 788e66f840cSTristram Ha if (restart & PORT_LED_OFF) 789ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_LED; 790e66f840cSTristram Ha break; 791ec4b94f9SMichael Grzeschik case MII_BMSR: 7929f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LINK_STATUS], &link); 793ec4b94f9SMichael Grzeschik data = BMSR_100FULL | 794ec4b94f9SMichael Grzeschik BMSR_100HALF | 795ec4b94f9SMichael Grzeschik BMSR_10FULL | 796ec4b94f9SMichael Grzeschik BMSR_10HALF | 797ec4b94f9SMichael Grzeschik BMSR_ANEGCAPABLE; 798e66f840cSTristram Ha if (link & PORT_AUTO_NEG_COMPLETE) 799ec4b94f9SMichael Grzeschik data |= BMSR_ANEGCOMPLETE; 800e66f840cSTristram Ha if (link & PORT_STAT_LINK_GOOD) 801ec4b94f9SMichael Grzeschik data |= BMSR_LSTATUS; 802e66f840cSTristram Ha break; 803ec4b94f9SMichael Grzeschik case MII_PHYSID1: 804e66f840cSTristram Ha data = KSZ8795_ID_HI; 805e66f840cSTristram Ha break; 806ec4b94f9SMichael Grzeschik case MII_PHYSID2: 8074b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 8084b20a07eSOleksij Rempel data = KSZ8863_ID_LO; 8094b20a07eSOleksij Rempel else 810e66f840cSTristram Ha data = KSZ8795_ID_LO; 811e66f840cSTristram Ha break; 812ec4b94f9SMichael Grzeschik case MII_ADVERTISE: 8139f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl); 814ec4b94f9SMichael Grzeschik data = ADVERTISE_CSMA; 815e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_SYM_PAUSE) 816ec4b94f9SMichael Grzeschik data |= ADVERTISE_PAUSE_CAP; 817e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX_FD) 818ec4b94f9SMichael Grzeschik data |= ADVERTISE_100FULL; 819e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX) 820ec4b94f9SMichael Grzeschik data |= ADVERTISE_100HALF; 821e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT_FD) 822ec4b94f9SMichael Grzeschik data |= ADVERTISE_10FULL; 823e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT) 824ec4b94f9SMichael Grzeschik data |= ADVERTISE_10HALF; 825e66f840cSTristram Ha break; 826ec4b94f9SMichael Grzeschik case MII_LPA: 8279f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link); 828ec4b94f9SMichael Grzeschik data = LPA_SLCT; 829e66f840cSTristram Ha if (link & PORT_REMOTE_SYM_PAUSE) 830ec4b94f9SMichael Grzeschik data |= LPA_PAUSE_CAP; 831e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX_FD) 832ec4b94f9SMichael Grzeschik data |= LPA_100FULL; 833e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX) 834ec4b94f9SMichael Grzeschik data |= LPA_100HALF; 835e66f840cSTristram Ha if (link & PORT_REMOTE_10BT_FD) 836ec4b94f9SMichael Grzeschik data |= LPA_10FULL; 837e66f840cSTristram Ha if (link & PORT_REMOTE_10BT) 838ec4b94f9SMichael Grzeschik data |= LPA_10HALF; 839ec4b94f9SMichael Grzeschik if (data & ~LPA_SLCT) 840ec4b94f9SMichael Grzeschik data |= LPA_LPACK; 841e66f840cSTristram Ha break; 84236838050SOleksij Rempel case PHY_REG_LINK_MD: 84336838050SOleksij Rempel ksz_pread8(dev, p, REG_PORT_LINK_MD_CTRL, &val1); 84436838050SOleksij Rempel ksz_pread8(dev, p, REG_PORT_LINK_MD_RESULT, &val2); 84536838050SOleksij Rempel if (val1 & PORT_START_CABLE_DIAG) 84636838050SOleksij Rempel data |= PHY_START_CABLE_DIAG; 84736838050SOleksij Rempel 84836838050SOleksij Rempel if (val1 & PORT_CABLE_10M_SHORT) 84936838050SOleksij Rempel data |= PHY_CABLE_10M_SHORT; 85036838050SOleksij Rempel 85136838050SOleksij Rempel data |= FIELD_PREP(PHY_CABLE_DIAG_RESULT_M, 85236838050SOleksij Rempel FIELD_GET(PORT_CABLE_DIAG_RESULT_M, val1)); 85336838050SOleksij Rempel 85436838050SOleksij Rempel data |= FIELD_PREP(PHY_CABLE_FAULT_COUNTER_M, 85536838050SOleksij Rempel (FIELD_GET(PORT_CABLE_FAULT_COUNTER_H, val1) << 8) | 85636838050SOleksij Rempel FIELD_GET(PORT_CABLE_FAULT_COUNTER_L, val2)); 85736838050SOleksij Rempel break; 85852939393SOleksij Rempel case PHY_REG_PHY_CTRL: 85952939393SOleksij Rempel ksz_pread8(dev, p, regs[P_LINK_STATUS], &link); 86052939393SOleksij Rempel if (link & PORT_MDIX_STATUS) 86152939393SOleksij Rempel data |= KSZ886X_CTRL_MDIX_STAT; 86252939393SOleksij Rempel break; 863e66f840cSTristram Ha default: 864e66f840cSTristram Ha processed = false; 865e66f840cSTristram Ha break; 866e66f840cSTristram Ha } 867e66f840cSTristram Ha if (processed) 868e66f840cSTristram Ha *val = data; 869e66f840cSTristram Ha } 870e66f840cSTristram Ha 8714b5baca0SMichael Grzeschik static void ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val) 872e66f840cSTristram Ha { 8739f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 874e66f840cSTristram Ha u8 restart, speed, ctrl, data; 8759f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 8769f73e112SMichael Grzeschik u8 p = phy; 877e66f840cSTristram Ha 878e66f840cSTristram Ha switch (reg) { 879ec4b94f9SMichael Grzeschik case MII_BMCR: 880e66f840cSTristram Ha 881e66f840cSTristram Ha /* Do not support PHY reset function. */ 882ec4b94f9SMichael Grzeschik if (val & BMCR_RESET) 883e66f840cSTristram Ha break; 8849f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed); 885e66f840cSTristram Ha data = speed; 886ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_HP_MDIX) 887e66f840cSTristram Ha data |= PORT_HP_MDIX; 888e66f840cSTristram Ha else 889e66f840cSTristram Ha data &= ~PORT_HP_MDIX; 890e66f840cSTristram Ha if (data != speed) 8919f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_SPEED_STATUS], data); 8929f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl); 893e66f840cSTristram Ha data = ctrl; 8944b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 895ec4b94f9SMichael Grzeschik if ((val & BMCR_ANENABLE)) 8964b20a07eSOleksij Rempel data |= PORT_AUTO_NEG_ENABLE; 8974b20a07eSOleksij Rempel else 8984b20a07eSOleksij Rempel data &= ~PORT_AUTO_NEG_ENABLE; 8994b20a07eSOleksij Rempel } else { 900ec4b94f9SMichael Grzeschik if (!(val & BMCR_ANENABLE)) 901e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 902e66f840cSTristram Ha else 903e66f840cSTristram Ha data &= ~PORT_AUTO_NEG_DISABLE; 904e66f840cSTristram Ha 905e66f840cSTristram Ha /* Fiber port does not support auto-negotiation. */ 906e66f840cSTristram Ha if (dev->ports[p].fiber) 907e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 9084b20a07eSOleksij Rempel } 9094b20a07eSOleksij Rempel 910ec4b94f9SMichael Grzeschik if (val & BMCR_SPEED100) 911e66f840cSTristram Ha data |= PORT_FORCE_100_MBIT; 912e66f840cSTristram Ha else 913e66f840cSTristram Ha data &= ~PORT_FORCE_100_MBIT; 914ec4b94f9SMichael Grzeschik if (val & BMCR_FULLDPLX) 915e66f840cSTristram Ha data |= PORT_FORCE_FULL_DUPLEX; 916e66f840cSTristram Ha else 917e66f840cSTristram Ha data &= ~PORT_FORCE_FULL_DUPLEX; 918e66f840cSTristram Ha if (data != ctrl) 9199f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_FORCE_CTRL], data); 9209f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart); 921e66f840cSTristram Ha data = restart; 922ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_LED) 923e66f840cSTristram Ha data |= PORT_LED_OFF; 924e66f840cSTristram Ha else 925e66f840cSTristram Ha data &= ~PORT_LED_OFF; 926ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_TRANSMIT) 927e66f840cSTristram Ha data |= PORT_TX_DISABLE; 928e66f840cSTristram Ha else 929e66f840cSTristram Ha data &= ~PORT_TX_DISABLE; 930ec4b94f9SMichael Grzeschik if (val & BMCR_ANRESTART) 931e66f840cSTristram Ha data |= PORT_AUTO_NEG_RESTART; 932e66f840cSTristram Ha else 933e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_RESTART); 934ec4b94f9SMichael Grzeschik if (val & BMCR_PDOWN) 935e66f840cSTristram Ha data |= PORT_POWER_DOWN; 936e66f840cSTristram Ha else 937e66f840cSTristram Ha data &= ~PORT_POWER_DOWN; 938ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX) 939e66f840cSTristram Ha data |= PORT_AUTO_MDIX_DISABLE; 940e66f840cSTristram Ha else 941e66f840cSTristram Ha data &= ~PORT_AUTO_MDIX_DISABLE; 942ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_FORCE_MDI) 943e66f840cSTristram Ha data |= PORT_FORCE_MDIX; 944e66f840cSTristram Ha else 945e66f840cSTristram Ha data &= ~PORT_FORCE_MDIX; 946ec4b94f9SMichael Grzeschik if (val & BMCR_LOOPBACK) 947e66f840cSTristram Ha data |= PORT_PHY_LOOPBACK; 948e66f840cSTristram Ha else 949e66f840cSTristram Ha data &= ~PORT_PHY_LOOPBACK; 950e66f840cSTristram Ha if (data != restart) 9519f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_NEG_RESTART_CTRL], data); 952e66f840cSTristram Ha break; 953ec4b94f9SMichael Grzeschik case MII_ADVERTISE: 9549f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl); 955e66f840cSTristram Ha data = ctrl; 956e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_SYM_PAUSE | 957e66f840cSTristram Ha PORT_AUTO_NEG_100BTX_FD | 958e66f840cSTristram Ha PORT_AUTO_NEG_100BTX | 959e66f840cSTristram Ha PORT_AUTO_NEG_10BT_FD | 960e66f840cSTristram Ha PORT_AUTO_NEG_10BT); 961ec4b94f9SMichael Grzeschik if (val & ADVERTISE_PAUSE_CAP) 962e66f840cSTristram Ha data |= PORT_AUTO_NEG_SYM_PAUSE; 963ec4b94f9SMichael Grzeschik if (val & ADVERTISE_100FULL) 964e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX_FD; 965ec4b94f9SMichael Grzeschik if (val & ADVERTISE_100HALF) 966e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX; 967ec4b94f9SMichael Grzeschik if (val & ADVERTISE_10FULL) 968e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT_FD; 969ec4b94f9SMichael Grzeschik if (val & ADVERTISE_10HALF) 970e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT; 971e66f840cSTristram Ha if (data != ctrl) 9729f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data); 973e66f840cSTristram Ha break; 97436838050SOleksij Rempel case PHY_REG_LINK_MD: 97536838050SOleksij Rempel if (val & PHY_START_CABLE_DIAG) 97636838050SOleksij Rempel ksz_port_cfg(dev, p, REG_PORT_LINK_MD_CTRL, PORT_START_CABLE_DIAG, true); 97736838050SOleksij Rempel break; 978e66f840cSTristram Ha default: 979e66f840cSTristram Ha break; 980e66f840cSTristram Ha } 981e66f840cSTristram Ha } 982e66f840cSTristram Ha 9834b5baca0SMichael Grzeschik static enum dsa_tag_protocol ksz8_get_tag_protocol(struct dsa_switch *ds, 9844d776482SFlorian Fainelli int port, 9854d776482SFlorian Fainelli enum dsa_tag_protocol mp) 986e66f840cSTristram Ha { 9874b20a07eSOleksij Rempel struct ksz_device *dev = ds->priv; 9884b20a07eSOleksij Rempel 9894b20a07eSOleksij Rempel /* ksz88x3 uses the same tag schema as KSZ9893 */ 9904b20a07eSOleksij Rempel return ksz_is_ksz88x3(dev) ? 9914b20a07eSOleksij Rempel DSA_TAG_PROTO_KSZ9893 : DSA_TAG_PROTO_KSZ8795; 992e66f840cSTristram Ha } 993e66f840cSTristram Ha 99449011e0cSOleksij Rempel static u32 ksz8_sw_get_phy_flags(struct dsa_switch *ds, int port) 99549011e0cSOleksij Rempel { 99649011e0cSOleksij Rempel /* Silicon Errata Sheet (DS80000830A): 99749011e0cSOleksij Rempel * Port 1 does not work with LinkMD Cable-Testing. 99849011e0cSOleksij Rempel * Port 1 does not respond to received PAUSE control frames. 99949011e0cSOleksij Rempel */ 100049011e0cSOleksij Rempel if (!port) 100149011e0cSOleksij Rempel return MICREL_KSZ8_P1_ERRATA; 100249011e0cSOleksij Rempel 100349011e0cSOleksij Rempel return 0; 100449011e0cSOleksij Rempel } 100549011e0cSOleksij Rempel 10064b5baca0SMichael Grzeschik static void ksz8_get_strings(struct dsa_switch *ds, int port, 1007e66f840cSTristram Ha u32 stringset, uint8_t *buf) 1008e66f840cSTristram Ha { 100965fe1acfSMichael Grzeschik struct ksz_device *dev = ds->priv; 1010e66f840cSTristram Ha int i; 1011e66f840cSTristram Ha 101265fe1acfSMichael Grzeschik for (i = 0; i < dev->mib_cnt; i++) { 10134b20a07eSOleksij Rempel memcpy(buf + i * ETH_GSTRING_LEN, 10144b20a07eSOleksij Rempel dev->mib_names[i].string, ETH_GSTRING_LEN); 1015e66f840cSTristram Ha } 1016e66f840cSTristram Ha } 1017e66f840cSTristram Ha 10184b5baca0SMichael Grzeschik static void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member) 1019e66f840cSTristram Ha { 1020e66f840cSTristram Ha u8 data; 1021e66f840cSTristram Ha 1022e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 1023e66f840cSTristram Ha data &= ~PORT_VLAN_MEMBERSHIP; 1024e66f840cSTristram Ha data |= (member & dev->port_mask); 1025e66f840cSTristram Ha ksz_pwrite8(dev, port, P_MIRROR_CTRL, data); 1026e66f840cSTristram Ha } 1027e66f840cSTristram Ha 10284b5baca0SMichael Grzeschik static void ksz8_port_stp_state_set(struct dsa_switch *ds, int port, u8 state) 1029e66f840cSTristram Ha { 1030de6dd626SArun Ramadoss ksz_port_stp_state_set(ds, port, state, P_STP_CTRL); 1031e66f840cSTristram Ha } 1032e66f840cSTristram Ha 10334b5baca0SMichael Grzeschik static void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port) 1034e66f840cSTristram Ha { 1035241ed719SMichael Grzeschik u8 learn[DSA_MAX_PORTS]; 1036e66f840cSTristram Ha int first, index, cnt; 1037e66f840cSTristram Ha struct ksz_port *p; 1038e66f840cSTristram Ha 1039*462d5250SArun Ramadoss if ((uint)port < dev->info->port_cnt) { 1040e66f840cSTristram Ha first = port; 1041e66f840cSTristram Ha cnt = port + 1; 1042e66f840cSTristram Ha } else { 1043e66f840cSTristram Ha /* Flush all ports. */ 1044e66f840cSTristram Ha first = 0; 1045*462d5250SArun Ramadoss cnt = dev->info->port_cnt; 1046e66f840cSTristram Ha } 1047e66f840cSTristram Ha for (index = first; index < cnt; index++) { 1048e66f840cSTristram Ha p = &dev->ports[index]; 1049e66f840cSTristram Ha if (!p->on) 1050e66f840cSTristram Ha continue; 1051e66f840cSTristram Ha ksz_pread8(dev, index, P_STP_CTRL, &learn[index]); 1052e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 1053e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, 1054e66f840cSTristram Ha learn[index] | PORT_LEARN_DISABLE); 1055e66f840cSTristram Ha } 1056e66f840cSTristram Ha ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true); 1057e66f840cSTristram Ha for (index = first; index < cnt; index++) { 1058e66f840cSTristram Ha p = &dev->ports[index]; 1059e66f840cSTristram Ha if (!p->on) 1060e66f840cSTristram Ha continue; 1061e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 1062e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, learn[index]); 1063e66f840cSTristram Ha } 1064e66f840cSTristram Ha } 1065e66f840cSTristram Ha 10664b5baca0SMichael Grzeschik static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag, 106789153ed6SVladimir Oltean struct netlink_ext_ack *extack) 1068e66f840cSTristram Ha { 1069e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1070e66f840cSTristram Ha 10714b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 10724b20a07eSOleksij Rempel return -ENOTSUPP; 10734b20a07eSOleksij Rempel 107416484413SBen Hutchings /* Discard packets with VID not enabled on the switch */ 1075e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag); 1076e66f840cSTristram Ha 107716484413SBen Hutchings /* Discard packets with VID not enabled on the ingress port */ 107816484413SBen Hutchings for (port = 0; port < dev->phy_port_cnt; ++port) 107916484413SBen Hutchings ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER, 108016484413SBen Hutchings flag); 108116484413SBen Hutchings 1082e66f840cSTristram Ha return 0; 1083e66f840cSTristram Ha } 1084e66f840cSTristram Ha 1085ef3b02a1SBen Hutchings static void ksz8_port_enable_pvid(struct ksz_device *dev, int port, bool state) 1086ef3b02a1SBen Hutchings { 1087ef3b02a1SBen Hutchings if (ksz_is_ksz88x3(dev)) { 1088ef3b02a1SBen Hutchings ksz_cfg(dev, REG_SW_INSERT_SRC_PVID, 1089ef3b02a1SBen Hutchings 0x03 << (4 - 2 * port), state); 1090ef3b02a1SBen Hutchings } else { 1091ef3b02a1SBen Hutchings ksz_pwrite8(dev, port, REG_PORT_CTRL_12, state ? 0x0f : 0x00); 1092ef3b02a1SBen Hutchings } 1093ef3b02a1SBen Hutchings } 1094ef3b02a1SBen Hutchings 10954b5baca0SMichael Grzeschik static int ksz8_port_vlan_add(struct dsa_switch *ds, int port, 109631046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 109731046a5fSVladimir Oltean struct netlink_ext_ack *extack) 1098e66f840cSTristram Ha { 1099e66f840cSTristram Ha bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 1100e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 11018f4f58f8SBen Hutchings struct ksz_port *p = &dev->ports[port]; 1102b7a9e0daSVladimir Oltean u16 data, new_pvid = 0; 1103e66f840cSTristram Ha u8 fid, member, valid; 1104e66f840cSTristram Ha 11054b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 11064b20a07eSOleksij Rempel return -ENOTSUPP; 11074b20a07eSOleksij Rempel 11088f4f58f8SBen Hutchings /* If a VLAN is added with untagged flag different from the 11098f4f58f8SBen Hutchings * port's Remove Tag flag, we need to change the latter. 11108f4f58f8SBen Hutchings * Ignore VID 0, which is always untagged. 11119130c2d3SBen Hutchings * Ignore CPU port, which will always be tagged. 11128f4f58f8SBen Hutchings */ 11139130c2d3SBen Hutchings if (untagged != p->remove_tag && vlan->vid != 0 && 11149130c2d3SBen Hutchings port != dev->cpu_port) { 11158f4f58f8SBen Hutchings unsigned int vid; 11168f4f58f8SBen Hutchings 11178f4f58f8SBen Hutchings /* Reject attempts to add a VLAN that requires the 11188f4f58f8SBen Hutchings * Remove Tag flag to be changed, unless there are no 11198f4f58f8SBen Hutchings * other VLANs currently configured. 11208f4f58f8SBen Hutchings */ 1121*462d5250SArun Ramadoss for (vid = 1; vid < dev->info->num_vlans; ++vid) { 11228f4f58f8SBen Hutchings /* Skip the VID we are going to add or reconfigure */ 11238f4f58f8SBen Hutchings if (vid == vlan->vid) 11248f4f58f8SBen Hutchings continue; 11258f4f58f8SBen Hutchings 11268f4f58f8SBen Hutchings ksz8_from_vlan(dev, dev->vlan_cache[vid].table[0], 11278f4f58f8SBen Hutchings &fid, &member, &valid); 11288f4f58f8SBen Hutchings if (valid && (member & BIT(port))) 11298f4f58f8SBen Hutchings return -EINVAL; 11308f4f58f8SBen Hutchings } 11318f4f58f8SBen Hutchings 1132e66f840cSTristram Ha ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged); 11338f4f58f8SBen Hutchings p->remove_tag = untagged; 11348f4f58f8SBen Hutchings } 1135e66f840cSTristram Ha 11364b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 11379f73e112SMichael Grzeschik ksz8_from_vlan(dev, data, &fid, &member, &valid); 1138e66f840cSTristram Ha 1139e66f840cSTristram Ha /* First time to setup the VLAN entry. */ 1140e66f840cSTristram Ha if (!valid) { 1141e66f840cSTristram Ha /* Need to find a way to map VID to FID. */ 1142e66f840cSTristram Ha fid = 1; 1143e66f840cSTristram Ha valid = 1; 1144e66f840cSTristram Ha } 1145e66f840cSTristram Ha member |= BIT(port); 1146e66f840cSTristram Ha 11479f73e112SMichael Grzeschik ksz8_to_vlan(dev, fid, member, valid, &data); 11484b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 1149e66f840cSTristram Ha 1150e66f840cSTristram Ha /* change PVID */ 1151e66f840cSTristram Ha if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 1152b7a9e0daSVladimir Oltean new_pvid = vlan->vid; 1153e66f840cSTristram Ha 1154e66f840cSTristram Ha if (new_pvid) { 1155b7a9e0daSVladimir Oltean u16 vid; 1156b7a9e0daSVladimir Oltean 1157e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid); 1158ef3b02a1SBen Hutchings vid &= ~VLAN_VID_MASK; 1159e66f840cSTristram Ha vid |= new_pvid; 1160e66f840cSTristram Ha ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid); 1161ef3b02a1SBen Hutchings 1162ef3b02a1SBen Hutchings ksz8_port_enable_pvid(dev, port, true); 1163e66f840cSTristram Ha } 11641958d581SVladimir Oltean 11651958d581SVladimir Oltean return 0; 1166e66f840cSTristram Ha } 1167e66f840cSTristram Ha 11684b5baca0SMichael Grzeschik static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, 1169e66f840cSTristram Ha const struct switchdev_obj_port_vlan *vlan) 1170e66f840cSTristram Ha { 1171e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1172ef3b02a1SBen Hutchings u16 data, pvid; 1173e66f840cSTristram Ha u8 fid, member, valid; 1174e66f840cSTristram Ha 11754b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 11764b20a07eSOleksij Rempel return -ENOTSUPP; 11774b20a07eSOleksij Rempel 1178e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid); 1179e66f840cSTristram Ha pvid = pvid & 0xFFF; 1180e66f840cSTristram Ha 11814b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 11829f73e112SMichael Grzeschik ksz8_from_vlan(dev, data, &fid, &member, &valid); 1183e66f840cSTristram Ha 1184e66f840cSTristram Ha member &= ~BIT(port); 1185e66f840cSTristram Ha 1186e66f840cSTristram Ha /* Invalidate the entry if no more member. */ 1187e66f840cSTristram Ha if (!member) { 1188e66f840cSTristram Ha fid = 0; 1189e66f840cSTristram Ha valid = 0; 1190e66f840cSTristram Ha } 1191e66f840cSTristram Ha 11929f73e112SMichael Grzeschik ksz8_to_vlan(dev, fid, member, valid, &data); 11934b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 1194e66f840cSTristram Ha 1195ef3b02a1SBen Hutchings if (pvid == vlan->vid) 1196ef3b02a1SBen Hutchings ksz8_port_enable_pvid(dev, port, false); 1197e66f840cSTristram Ha 1198e66f840cSTristram Ha return 0; 1199e66f840cSTristram Ha } 1200e66f840cSTristram Ha 12014b5baca0SMichael Grzeschik static int ksz8_port_mirror_add(struct dsa_switch *ds, int port, 1202e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror, 12030148bb50SVladimir Oltean bool ingress, struct netlink_ext_ack *extack) 1204e66f840cSTristram Ha { 1205e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1206e66f840cSTristram Ha 1207e66f840cSTristram Ha if (ingress) { 1208e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true); 1209e66f840cSTristram Ha dev->mirror_rx |= BIT(port); 1210e66f840cSTristram Ha } else { 1211e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true); 1212e66f840cSTristram Ha dev->mirror_tx |= BIT(port); 1213e66f840cSTristram Ha } 1214e66f840cSTristram Ha 1215e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false); 1216e66f840cSTristram Ha 1217e66f840cSTristram Ha /* configure mirror port */ 1218e66f840cSTristram Ha if (dev->mirror_rx || dev->mirror_tx) 1219e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 1220e66f840cSTristram Ha PORT_MIRROR_SNIFFER, true); 1221e66f840cSTristram Ha 1222e66f840cSTristram Ha return 0; 1223e66f840cSTristram Ha } 1224e66f840cSTristram Ha 12254b5baca0SMichael Grzeschik static void ksz8_port_mirror_del(struct dsa_switch *ds, int port, 1226e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror) 1227e66f840cSTristram Ha { 1228e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1229e66f840cSTristram Ha u8 data; 1230e66f840cSTristram Ha 1231e66f840cSTristram Ha if (mirror->ingress) { 1232e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false); 1233e66f840cSTristram Ha dev->mirror_rx &= ~BIT(port); 1234e66f840cSTristram Ha } else { 1235e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false); 1236e66f840cSTristram Ha dev->mirror_tx &= ~BIT(port); 1237e66f840cSTristram Ha } 1238e66f840cSTristram Ha 1239e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 1240e66f840cSTristram Ha 1241e66f840cSTristram Ha if (!dev->mirror_rx && !dev->mirror_tx) 1242e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 1243e66f840cSTristram Ha PORT_MIRROR_SNIFFER, false); 1244e66f840cSTristram Ha } 1245e66f840cSTristram Ha 1246c2ac4d2aSMichael Grzeschik static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port) 1247e66f840cSTristram Ha { 1248e66f840cSTristram Ha struct ksz_port *p = &dev->ports[port]; 1249c2ac4d2aSMichael Grzeschik u8 data8; 1250e66f840cSTristram Ha 1251edecfa98SHelmut Grohne if (!p->interface && dev->compat_interface) { 1252edecfa98SHelmut Grohne dev_warn(dev->dev, 1253edecfa98SHelmut Grohne "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. " 1254edecfa98SHelmut Grohne "Please update your device tree.\n", 1255edecfa98SHelmut Grohne port); 1256edecfa98SHelmut Grohne p->interface = dev->compat_interface; 1257edecfa98SHelmut Grohne } 1258edecfa98SHelmut Grohne 1259e66f840cSTristram Ha /* Configure MII interface for proper network communication. */ 1260e66f840cSTristram Ha ksz_read8(dev, REG_PORT_5_CTRL_6, &data8); 1261e66f840cSTristram Ha data8 &= ~PORT_INTERFACE_TYPE; 1262e66f840cSTristram Ha data8 &= ~PORT_GMII_1GPS_MODE; 1263edecfa98SHelmut Grohne switch (p->interface) { 1264e66f840cSTristram Ha case PHY_INTERFACE_MODE_MII: 1265e66f840cSTristram Ha p->phydev.speed = SPEED_100; 1266e66f840cSTristram Ha break; 1267e66f840cSTristram Ha case PHY_INTERFACE_MODE_RMII: 1268e66f840cSTristram Ha data8 |= PORT_INTERFACE_RMII; 1269e66f840cSTristram Ha p->phydev.speed = SPEED_100; 1270e66f840cSTristram Ha break; 1271e66f840cSTristram Ha case PHY_INTERFACE_MODE_GMII: 1272e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 1273e66f840cSTristram Ha data8 |= PORT_INTERFACE_GMII; 1274e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 1275e66f840cSTristram Ha break; 1276e66f840cSTristram Ha default: 1277e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_IN_ENABLE; 1278e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_OUT_ENABLE; 1279edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 1280edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_RXID) 1281e66f840cSTristram Ha data8 |= PORT_RGMII_ID_IN_ENABLE; 1282edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 1283edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_TXID) 1284e66f840cSTristram Ha data8 |= PORT_RGMII_ID_OUT_ENABLE; 1285e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 1286e66f840cSTristram Ha data8 |= PORT_INTERFACE_RGMII; 1287e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 1288e66f840cSTristram Ha break; 1289e66f840cSTristram Ha } 1290e66f840cSTristram Ha ksz_write8(dev, REG_PORT_5_CTRL_6, data8); 1291e66f840cSTristram Ha p->phydev.duplex = 1; 1292c2ac4d2aSMichael Grzeschik } 1293c2ac4d2aSMichael Grzeschik 1294c2ac4d2aSMichael Grzeschik static void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port) 1295c2ac4d2aSMichael Grzeschik { 1296b3612ccdSOleksij Rempel struct dsa_switch *ds = dev->ds; 12979f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 12989f73e112SMichael Grzeschik const u32 *masks; 1299c2ac4d2aSMichael Grzeschik u8 member; 1300c2ac4d2aSMichael Grzeschik 13019f73e112SMichael Grzeschik masks = ksz8->masks; 13029f73e112SMichael Grzeschik 1303c2ac4d2aSMichael Grzeschik /* enable broadcast storm limit */ 1304c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true); 1305c2ac4d2aSMichael Grzeschik 13064b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) 1307c2ac4d2aSMichael Grzeschik ksz8795_set_prio_queue(dev, port, 4); 1308c2ac4d2aSMichael Grzeschik 1309c2ac4d2aSMichael Grzeschik /* disable DiffServ priority */ 1310c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false); 1311c2ac4d2aSMichael Grzeschik 1312c2ac4d2aSMichael Grzeschik /* replace priority */ 13139f73e112SMichael Grzeschik ksz_port_cfg(dev, port, P_802_1P_CTRL, 13149f73e112SMichael Grzeschik masks[PORT_802_1P_REMAPPING], false); 1315c2ac4d2aSMichael Grzeschik 1316c2ac4d2aSMichael Grzeschik /* enable 802.1p priority */ 1317c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true); 1318c2ac4d2aSMichael Grzeschik 1319c2ac4d2aSMichael Grzeschik if (cpu_port) { 13204b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) 1321c2ac4d2aSMichael Grzeschik ksz8795_cpu_interface_select(dev, port); 1322e66f840cSTristram Ha 1323b3612ccdSOleksij Rempel member = dsa_user_ports(ds); 1324e66f840cSTristram Ha } else { 1325b3612ccdSOleksij Rempel member = BIT(dsa_upstream_port(ds, port)); 1326e66f840cSTristram Ha } 1327b3612ccdSOleksij Rempel 13284b5baca0SMichael Grzeschik ksz8_cfg_port_member(dev, port, member); 1329e66f840cSTristram Ha } 1330e66f840cSTristram Ha 13314b5baca0SMichael Grzeschik static void ksz8_config_cpu_port(struct dsa_switch *ds) 1332e66f840cSTristram Ha { 1333e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 13349f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 13359f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 1336e66f840cSTristram Ha struct ksz_port *p; 13379f73e112SMichael Grzeschik const u32 *masks; 1338e66f840cSTristram Ha u8 remote; 1339e66f840cSTristram Ha int i; 1340e66f840cSTristram Ha 13419f73e112SMichael Grzeschik masks = ksz8->masks; 13429f73e112SMichael Grzeschik 1343e66f840cSTristram Ha /* Switch marks the maximum frame with extra byte as oversize. */ 1344e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true); 13459f73e112SMichael Grzeschik ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true); 1346e66f840cSTristram Ha 1347e66f840cSTristram Ha p = &dev->ports[dev->cpu_port]; 1348e66f840cSTristram Ha p->on = 1; 1349e66f840cSTristram Ha 13504b5baca0SMichael Grzeschik ksz8_port_setup(dev, dev->cpu_port, true); 1351e66f840cSTristram Ha 13524ce2a984SMichael Grzeschik for (i = 0; i < dev->phy_port_cnt; i++) { 1353e66f840cSTristram Ha p = &dev->ports[i]; 1354e66f840cSTristram Ha 13554b5baca0SMichael Grzeschik ksz8_port_stp_state_set(ds, i, BR_STATE_DISABLED); 1356e66f840cSTristram Ha 1357e66f840cSTristram Ha /* Last port may be disabled. */ 13584ce2a984SMichael Grzeschik if (i == dev->phy_port_cnt) 1359e66f840cSTristram Ha break; 1360e66f840cSTristram Ha p->on = 1; 1361e66f840cSTristram Ha p->phy = 1; 1362e66f840cSTristram Ha } 1363e66f840cSTristram Ha for (i = 0; i < dev->phy_port_cnt; i++) { 1364e66f840cSTristram Ha p = &dev->ports[i]; 1365e66f840cSTristram Ha if (!p->on) 1366e66f840cSTristram Ha continue; 13674b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) { 13689f73e112SMichael Grzeschik ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote); 1369e66f840cSTristram Ha if (remote & PORT_FIBER_MODE) 1370e66f840cSTristram Ha p->fiber = 1; 13714b20a07eSOleksij Rempel } 1372e66f840cSTristram Ha if (p->fiber) 1373e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1374e66f840cSTristram Ha true); 1375e66f840cSTristram Ha else 1376e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1377e66f840cSTristram Ha false); 1378e66f840cSTristram Ha } 1379e66f840cSTristram Ha } 1380e66f840cSTristram Ha 13817b6e6235SOleksij Rempel static int ksz8_handle_global_errata(struct dsa_switch *ds) 13827b6e6235SOleksij Rempel { 13837b6e6235SOleksij Rempel struct ksz_device *dev = ds->priv; 13847b6e6235SOleksij Rempel int ret = 0; 13857b6e6235SOleksij Rempel 13867b6e6235SOleksij Rempel /* KSZ87xx Errata DS80000687C. 13877b6e6235SOleksij Rempel * Module 2: Link drops with some EEE link partners. 13887b6e6235SOleksij Rempel * An issue with the EEE next page exchange between the 13897b6e6235SOleksij Rempel * KSZ879x/KSZ877x/KSZ876x and some EEE link partners may result in 13907b6e6235SOleksij Rempel * the link dropping. 13917b6e6235SOleksij Rempel */ 1392*462d5250SArun Ramadoss if (dev->info->ksz87xx_eee_link_erratum) 13937b6e6235SOleksij Rempel ret = ksz8_ind_write8(dev, TABLE_EEE, REG_IND_EEE_GLOB2_HI, 0); 13947b6e6235SOleksij Rempel 13957b6e6235SOleksij Rempel return ret; 13967b6e6235SOleksij Rempel } 13977b6e6235SOleksij Rempel 13984b5baca0SMichael Grzeschik static int ksz8_setup(struct dsa_switch *ds) 1399e66f840cSTristram Ha { 1400e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1401e66f840cSTristram Ha struct alu_struct alu; 1402e66f840cSTristram Ha int i, ret = 0; 1403e66f840cSTristram Ha 1404e66f840cSTristram Ha dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table), 1405*462d5250SArun Ramadoss dev->info->num_vlans, GFP_KERNEL); 1406e66f840cSTristram Ha if (!dev->vlan_cache) 1407e66f840cSTristram Ha return -ENOMEM; 1408e66f840cSTristram Ha 14094b5baca0SMichael Grzeschik ret = ksz8_reset_switch(dev); 1410e66f840cSTristram Ha if (ret) { 1411e66f840cSTristram Ha dev_err(ds->dev, "failed to reset switch\n"); 1412e66f840cSTristram Ha return ret; 1413e66f840cSTristram Ha } 1414e66f840cSTristram Ha 1415e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true); 1416e66f840cSTristram Ha 1417e66f840cSTristram Ha /* Enable automatic fast aging when link changed detected. */ 1418e66f840cSTristram Ha ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true); 1419e66f840cSTristram Ha 1420e66f840cSTristram Ha /* Enable aggressive back off algorithm in half duplex mode. */ 1421e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1, 1422e66f840cSTristram Ha SW_AGGR_BACKOFF, SW_AGGR_BACKOFF); 1423e66f840cSTristram Ha 1424e66f840cSTristram Ha /* 1425e66f840cSTristram Ha * Make sure unicast VLAN boundary is set as default and 1426e66f840cSTristram Ha * enable no excessive collision drop. 1427e66f840cSTristram Ha */ 1428e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2, 1429e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP, 1430e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP); 1431e66f840cSTristram Ha 14324b5baca0SMichael Grzeschik ksz8_config_cpu_port(ds); 1433e66f840cSTristram Ha 1434e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true); 1435e66f840cSTristram Ha 1436e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false); 1437e66f840cSTristram Ha 1438e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false); 1439e66f840cSTristram Ha 1440ef3b02a1SBen Hutchings if (!ksz_is_ksz88x3(dev)) 1441ef3b02a1SBen Hutchings ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true); 1442ef3b02a1SBen Hutchings 1443e66f840cSTristram Ha /* set broadcast storm protection 10% rate */ 1444e66f840cSTristram Ha regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL, 1445e66f840cSTristram Ha BROADCAST_STORM_RATE, 1446e66f840cSTristram Ha (BROADCAST_STORM_VALUE * 1447e66f840cSTristram Ha BROADCAST_STORM_PROT_RATE) / 100); 1448e66f840cSTristram Ha 1449*462d5250SArun Ramadoss for (i = 0; i < (dev->info->num_vlans / 4); i++) 14504b5baca0SMichael Grzeschik ksz8_r_vlan_entries(dev, i); 1451e66f840cSTristram Ha 1452e66f840cSTristram Ha /* Setup STP address for STP operation. */ 1453e66f840cSTristram Ha memset(&alu, 0, sizeof(alu)); 1454e66f840cSTristram Ha ether_addr_copy(alu.mac, eth_stp_addr); 1455e66f840cSTristram Ha alu.is_static = true; 1456e66f840cSTristram Ha alu.is_override = true; 1457e66f840cSTristram Ha alu.port_forward = dev->host_mask; 1458e66f840cSTristram Ha 14594b5baca0SMichael Grzeschik ksz8_w_sta_mac_table(dev, 0, &alu); 1460e66f840cSTristram Ha 1461e66f840cSTristram Ha ksz_init_mib_timer(dev); 1462e66f840cSTristram Ha 14630ee2af4eSVladimir Oltean ds->configure_vlan_while_not_filtering = false; 14640ee2af4eSVladimir Oltean 14657b6e6235SOleksij Rempel return ksz8_handle_global_errata(ds); 1466e66f840cSTristram Ha } 1467e66f840cSTristram Ha 146882fdbb91SRussell King (Oracle) static void ksz8_get_caps(struct dsa_switch *ds, int port, 146982fdbb91SRussell King (Oracle) struct phylink_config *config) 14702c709e0bSMichael Grzeschik { 14712c709e0bSMichael Grzeschik struct ksz_device *dev = ds->priv; 14722c709e0bSMichael Grzeschik 14732c709e0bSMichael Grzeschik if (port == dev->cpu_port) { 147482fdbb91SRussell King (Oracle) __set_bit(PHY_INTERFACE_MODE_RMII, 147582fdbb91SRussell King (Oracle) config->supported_interfaces); 147682fdbb91SRussell King (Oracle) __set_bit(PHY_INTERFACE_MODE_MII, 147782fdbb91SRussell King (Oracle) config->supported_interfaces); 14782c709e0bSMichael Grzeschik } else { 147982fdbb91SRussell King (Oracle) __set_bit(PHY_INTERFACE_MODE_INTERNAL, 148082fdbb91SRussell King (Oracle) config->supported_interfaces); 14812c709e0bSMichael Grzeschik } 14822c709e0bSMichael Grzeschik 148382fdbb91SRussell King (Oracle) config->mac_capabilities = MAC_10 | MAC_100; 14842c709e0bSMichael Grzeschik 14852c709e0bSMichael Grzeschik /* Silicon Errata Sheet (DS80000830A): 14862c709e0bSMichael Grzeschik * "Port 1 does not respond to received flow control PAUSE frames" 14872c709e0bSMichael Grzeschik * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3 14882c709e0bSMichael Grzeschik * switches. 14892c709e0bSMichael Grzeschik */ 14902c709e0bSMichael Grzeschik if (!ksz_is_ksz88x3(dev) || port) 149182fdbb91SRussell King (Oracle) config->mac_capabilities |= MAC_SYM_PAUSE; 14922c709e0bSMichael Grzeschik 14932c709e0bSMichael Grzeschik /* Asym pause is not supported on KSZ8863 and KSZ8873 */ 14942c709e0bSMichael Grzeschik if (!ksz_is_ksz88x3(dev)) 149582fdbb91SRussell King (Oracle) config->mac_capabilities |= MAC_ASYM_PAUSE; 14962c709e0bSMichael Grzeschik } 14972c709e0bSMichael Grzeschik 14984b5baca0SMichael Grzeschik static const struct dsa_switch_ops ksz8_switch_ops = { 14994b5baca0SMichael Grzeschik .get_tag_protocol = ksz8_get_tag_protocol, 150049011e0cSOleksij Rempel .get_phy_flags = ksz8_sw_get_phy_flags, 15014b5baca0SMichael Grzeschik .setup = ksz8_setup, 1502e66f840cSTristram Ha .phy_read = ksz_phy_read16, 1503e66f840cSTristram Ha .phy_write = ksz_phy_write16, 150482fdbb91SRussell King (Oracle) .phylink_get_caps = ksz8_get_caps, 1505143a102eSCodrin Ciubotariu .phylink_mac_link_down = ksz_mac_link_down, 1506e66f840cSTristram Ha .port_enable = ksz_enable_port, 15074b5baca0SMichael Grzeschik .get_strings = ksz8_get_strings, 1508e66f840cSTristram Ha .get_ethtool_stats = ksz_get_ethtool_stats, 1509e66f840cSTristram Ha .get_sset_count = ksz_sset_count, 1510e66f840cSTristram Ha .port_bridge_join = ksz_port_bridge_join, 1511e66f840cSTristram Ha .port_bridge_leave = ksz_port_bridge_leave, 15124b5baca0SMichael Grzeschik .port_stp_state_set = ksz8_port_stp_state_set, 1513e66f840cSTristram Ha .port_fast_age = ksz_port_fast_age, 15144b5baca0SMichael Grzeschik .port_vlan_filtering = ksz8_port_vlan_filtering, 15154b5baca0SMichael Grzeschik .port_vlan_add = ksz8_port_vlan_add, 15164b5baca0SMichael Grzeschik .port_vlan_del = ksz8_port_vlan_del, 1517e66f840cSTristram Ha .port_fdb_dump = ksz_port_fdb_dump, 1518e66f840cSTristram Ha .port_mdb_add = ksz_port_mdb_add, 1519e66f840cSTristram Ha .port_mdb_del = ksz_port_mdb_del, 15204b5baca0SMichael Grzeschik .port_mirror_add = ksz8_port_mirror_add, 15214b5baca0SMichael Grzeschik .port_mirror_del = ksz8_port_mirror_del, 1522e66f840cSTristram Ha }; 1523e66f840cSTristram Ha 15244b5baca0SMichael Grzeschik static u32 ksz8_get_port_addr(int port, int offset) 1525e66f840cSTristram Ha { 1526e66f840cSTristram Ha return PORT_CTRL_ADDR(port, offset); 1527e66f840cSTristram Ha } 1528e66f840cSTristram Ha 15294b5baca0SMichael Grzeschik static int ksz8_switch_detect(struct ksz_device *dev) 1530e66f840cSTristram Ha { 1531e66f840cSTristram Ha u8 id1, id2; 1532e66f840cSTristram Ha u16 id16; 1533e66f840cSTristram Ha int ret; 1534e66f840cSTristram Ha 1535e66f840cSTristram Ha /* read chip id */ 1536e66f840cSTristram Ha ret = ksz_read16(dev, REG_CHIP_ID0, &id16); 1537e66f840cSTristram Ha if (ret) 1538e66f840cSTristram Ha return ret; 1539e66f840cSTristram Ha 1540e66f840cSTristram Ha id1 = id16 >> 8; 1541e66f840cSTristram Ha id2 = id16 & SW_CHIP_ID_M; 15424b20a07eSOleksij Rempel 15434b20a07eSOleksij Rempel switch (id1) { 15444b20a07eSOleksij Rempel case KSZ87_FAMILY_ID: 15454b20a07eSOleksij Rempel if ((id2 != CHIP_ID_94 && id2 != CHIP_ID_95)) 1546e66f840cSTristram Ha return -ENODEV; 1547e66f840cSTristram Ha 1548e66f840cSTristram Ha if (id2 == CHIP_ID_95) { 1549e66f840cSTristram Ha u8 val; 1550e66f840cSTristram Ha 1551e66f840cSTristram Ha id2 = 0x95; 15524b20a07eSOleksij Rempel ksz_read8(dev, REG_PORT_STATUS_0, &val); 1553e66f840cSTristram Ha if (val & PORT_FIBER_MODE) 1554e66f840cSTristram Ha id2 = 0x65; 1555e66f840cSTristram Ha } else if (id2 == CHIP_ID_94) { 1556e66f840cSTristram Ha id2 = 0x94; 1557e66f840cSTristram Ha } 15584b20a07eSOleksij Rempel break; 15594b20a07eSOleksij Rempel case KSZ88_FAMILY_ID: 15604b20a07eSOleksij Rempel if (id2 != CHIP_ID_63) 15614b20a07eSOleksij Rempel return -ENODEV; 15624b20a07eSOleksij Rempel break; 15634b20a07eSOleksij Rempel default: 15644b20a07eSOleksij Rempel dev_err(dev->dev, "invalid family id: %d\n", id1); 15654b20a07eSOleksij Rempel return -ENODEV; 15664b20a07eSOleksij Rempel } 1567e66f840cSTristram Ha id16 &= ~0xff; 1568e66f840cSTristram Ha id16 |= id2; 1569e66f840cSTristram Ha dev->chip_id = id16; 1570e66f840cSTristram Ha 1571e66f840cSTristram Ha return 0; 1572e66f840cSTristram Ha } 1573e66f840cSTristram Ha 15744b5baca0SMichael Grzeschik static int ksz8_switch_init(struct ksz_device *dev) 1575e66f840cSTristram Ha { 15769f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 1577e66f840cSTristram Ha int i; 1578e66f840cSTristram Ha 15794b5baca0SMichael Grzeschik dev->ds->ops = &ksz8_switch_ops; 1580e66f840cSTristram Ha 1581*462d5250SArun Ramadoss dev->cpu_port = fls(dev->info->cpu_ports) - 1; 1582*462d5250SArun Ramadoss dev->host_mask = dev->info->cpu_ports; 1583*462d5250SArun Ramadoss dev->phy_port_cnt = dev->info->port_cnt - 1; 1584*462d5250SArun Ramadoss dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | dev->info->cpu_ports; 1585e66f840cSTristram Ha 15864b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 15874b20a07eSOleksij Rempel ksz8->regs = ksz8863_regs; 15884b20a07eSOleksij Rempel ksz8->masks = ksz8863_masks; 15894b20a07eSOleksij Rempel ksz8->shifts = ksz8863_shifts; 15904b20a07eSOleksij Rempel dev->mib_cnt = ARRAY_SIZE(ksz88xx_mib_names); 15914b20a07eSOleksij Rempel dev->mib_names = ksz88xx_mib_names; 15924b20a07eSOleksij Rempel } else { 15939f73e112SMichael Grzeschik ksz8->regs = ksz8795_regs; 15949f73e112SMichael Grzeschik ksz8->masks = ksz8795_masks; 15959f73e112SMichael Grzeschik ksz8->shifts = ksz8795_shifts; 15964b20a07eSOleksij Rempel dev->mib_cnt = ARRAY_SIZE(ksz87xx_mib_names); 15974b20a07eSOleksij Rempel dev->mib_names = ksz87xx_mib_names; 15984b20a07eSOleksij Rempel } 15999f73e112SMichael Grzeschik 16004b20a07eSOleksij Rempel dev->reg_mib_cnt = MIB_COUNTER_NUM; 1601e66f840cSTristram Ha 1602c9f4633bSMichael Grzeschik dev->ports = devm_kzalloc(dev->dev, 1603*462d5250SArun Ramadoss dev->info->port_cnt * sizeof(struct ksz_port), 1604e66f840cSTristram Ha GFP_KERNEL); 1605e66f840cSTristram Ha if (!dev->ports) 1606e66f840cSTristram Ha return -ENOMEM; 1607*462d5250SArun Ramadoss for (i = 0; i < dev->info->port_cnt; i++) { 1608e66f840cSTristram Ha mutex_init(&dev->ports[i].mib.cnt_mutex); 1609e66f840cSTristram Ha dev->ports[i].mib.counters = 1610e66f840cSTristram Ha devm_kzalloc(dev->dev, 1611e66f840cSTristram Ha sizeof(u64) * 161265fe1acfSMichael Grzeschik (dev->mib_cnt + 1), 1613e66f840cSTristram Ha GFP_KERNEL); 1614e66f840cSTristram Ha if (!dev->ports[i].mib.counters) 1615e66f840cSTristram Ha return -ENOMEM; 1616e66f840cSTristram Ha } 1617e66f840cSTristram Ha 1618af199a1aSCodrin Ciubotariu /* set the real number of ports */ 1619*462d5250SArun Ramadoss dev->ds->num_ports = dev->info->port_cnt; 1620af199a1aSCodrin Ciubotariu 16219130c2d3SBen Hutchings /* We rely on software untagging on the CPU port, so that we 16229130c2d3SBen Hutchings * can support both tagged and untagged VLANs 16239130c2d3SBen Hutchings */ 16249130c2d3SBen Hutchings dev->ds->untag_bridge_pvid = true; 16259130c2d3SBen Hutchings 162616484413SBen Hutchings /* VLAN filtering is partly controlled by the global VLAN 162716484413SBen Hutchings * Enable flag 162816484413SBen Hutchings */ 162916484413SBen Hutchings dev->ds->vlan_filtering_is_global = true; 163016484413SBen Hutchings 1631e66f840cSTristram Ha return 0; 1632e66f840cSTristram Ha } 1633e66f840cSTristram Ha 16344b5baca0SMichael Grzeschik static void ksz8_switch_exit(struct ksz_device *dev) 1635e66f840cSTristram Ha { 16364b5baca0SMichael Grzeschik ksz8_reset_switch(dev); 1637e66f840cSTristram Ha } 1638e66f840cSTristram Ha 16394b5baca0SMichael Grzeschik static const struct ksz_dev_ops ksz8_dev_ops = { 16404b5baca0SMichael Grzeschik .get_port_addr = ksz8_get_port_addr, 16414b5baca0SMichael Grzeschik .cfg_port_member = ksz8_cfg_port_member, 16424b5baca0SMichael Grzeschik .flush_dyn_mac_table = ksz8_flush_dyn_mac_table, 16434b5baca0SMichael Grzeschik .port_setup = ksz8_port_setup, 16444b5baca0SMichael Grzeschik .r_phy = ksz8_r_phy, 16454b5baca0SMichael Grzeschik .w_phy = ksz8_w_phy, 16464b5baca0SMichael Grzeschik .r_dyn_mac_table = ksz8_r_dyn_mac_table, 16474b5baca0SMichael Grzeschik .r_sta_mac_table = ksz8_r_sta_mac_table, 16484b5baca0SMichael Grzeschik .w_sta_mac_table = ksz8_w_sta_mac_table, 16494b5baca0SMichael Grzeschik .r_mib_cnt = ksz8_r_mib_cnt, 16504b5baca0SMichael Grzeschik .r_mib_pkt = ksz8_r_mib_pkt, 16514b5baca0SMichael Grzeschik .freeze_mib = ksz8_freeze_mib, 16524b5baca0SMichael Grzeschik .port_init_cnt = ksz8_port_init_cnt, 16534b5baca0SMichael Grzeschik .shutdown = ksz8_reset_switch, 16544b5baca0SMichael Grzeschik .detect = ksz8_switch_detect, 16554b5baca0SMichael Grzeschik .init = ksz8_switch_init, 16564b5baca0SMichael Grzeschik .exit = ksz8_switch_exit, 1657e66f840cSTristram Ha }; 1658e66f840cSTristram Ha 16594b5baca0SMichael Grzeschik int ksz8_switch_register(struct ksz_device *dev) 1660e66f840cSTristram Ha { 16614b5baca0SMichael Grzeschik return ksz_switch_register(dev, &ksz8_dev_ops); 1662e66f840cSTristram Ha } 16634b5baca0SMichael Grzeschik EXPORT_SYMBOL(ksz8_switch_register); 1664e66f840cSTristram Ha 1665e66f840cSTristram Ha MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>"); 1666e66f840cSTristram Ha MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver"); 1667e66f840cSTristram Ha MODULE_LICENSE("GPL"); 1668