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 static bool ksz_is_ksz88x3(struct ksz_device *dev) 1304b20a07eSOleksij Rempel { 1314b20a07eSOleksij Rempel return dev->chip_id == 0x8830; 1324b20a07eSOleksij Rempel } 1334b20a07eSOleksij Rempel 134e66f840cSTristram Ha static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set) 135e66f840cSTristram Ha { 136e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0); 137e66f840cSTristram Ha } 138e66f840cSTristram Ha 139e66f840cSTristram Ha static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits, 140e66f840cSTristram Ha bool set) 141e66f840cSTristram Ha { 142e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset), 143e66f840cSTristram Ha bits, set ? bits : 0); 144e66f840cSTristram Ha } 145e66f840cSTristram Ha 1467b6e6235SOleksij Rempel static int ksz8_ind_write8(struct ksz_device *dev, u8 table, u16 addr, u8 data) 1477b6e6235SOleksij Rempel { 1487b6e6235SOleksij Rempel struct ksz8 *ksz8 = dev->priv; 1497b6e6235SOleksij Rempel const u8 *regs = ksz8->regs; 1507b6e6235SOleksij Rempel u16 ctrl_addr; 1517b6e6235SOleksij Rempel int ret = 0; 1527b6e6235SOleksij Rempel 1537b6e6235SOleksij Rempel mutex_lock(&dev->alu_mutex); 1547b6e6235SOleksij Rempel 1557b6e6235SOleksij Rempel ctrl_addr = IND_ACC_TABLE(table) | addr; 1567b6e6235SOleksij Rempel ret = ksz_write8(dev, regs[REG_IND_BYTE], data); 1577b6e6235SOleksij Rempel if (!ret) 1587b6e6235SOleksij Rempel ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 1597b6e6235SOleksij Rempel 1607b6e6235SOleksij Rempel mutex_unlock(&dev->alu_mutex); 1617b6e6235SOleksij Rempel 1627b6e6235SOleksij Rempel return ret; 1637b6e6235SOleksij Rempel } 1647b6e6235SOleksij Rempel 1654b5baca0SMichael Grzeschik static int ksz8_reset_switch(struct ksz_device *dev) 166e66f840cSTristram Ha { 1674b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 1684b20a07eSOleksij Rempel /* reset switch */ 1694b20a07eSOleksij Rempel ksz_cfg(dev, KSZ8863_REG_SW_RESET, 1704b20a07eSOleksij Rempel KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, true); 1714b20a07eSOleksij Rempel ksz_cfg(dev, KSZ8863_REG_SW_RESET, 1724b20a07eSOleksij Rempel KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, false); 1734b20a07eSOleksij Rempel } else { 174e66f840cSTristram Ha /* reset switch */ 175e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 176e66f840cSTristram Ha SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S); 177e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0); 1784b20a07eSOleksij Rempel } 179e66f840cSTristram Ha 180e66f840cSTristram Ha return 0; 181e66f840cSTristram Ha } 182e66f840cSTristram Ha 183e66f840cSTristram Ha static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue) 184e66f840cSTristram Ha { 185e66f840cSTristram Ha u8 hi, lo; 186e66f840cSTristram Ha 187e66f840cSTristram Ha /* Number of queues can only be 1, 2, or 4. */ 188e66f840cSTristram Ha switch (queue) { 189e66f840cSTristram Ha case 4: 190e66f840cSTristram Ha case 3: 191e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_4; 192e66f840cSTristram Ha break; 193e66f840cSTristram Ha case 2: 194e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_2; 195e66f840cSTristram Ha break; 196e66f840cSTristram Ha default: 197e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_1; 198e66f840cSTristram Ha } 199e66f840cSTristram Ha ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo); 200e66f840cSTristram Ha ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi); 201e66f840cSTristram Ha lo &= ~PORT_QUEUE_SPLIT_L; 202e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_2) 203e66f840cSTristram Ha lo |= PORT_QUEUE_SPLIT_L; 204e66f840cSTristram Ha hi &= ~PORT_QUEUE_SPLIT_H; 205e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_4) 206e66f840cSTristram Ha hi |= PORT_QUEUE_SPLIT_H; 207e66f840cSTristram Ha ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo); 208e66f840cSTristram Ha ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi); 209e66f840cSTristram Ha 210e66f840cSTristram Ha /* Default is port based for egress rate limit. */ 211e66f840cSTristram Ha if (queue != PORT_QUEUE_SPLIT_1) 212e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED, 213e66f840cSTristram Ha true); 214e66f840cSTristram Ha } 215e66f840cSTristram Ha 2164b5baca0SMichael Grzeschik static void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt) 217e66f840cSTristram Ha { 2189f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 2199f73e112SMichael Grzeschik const u32 *masks; 2209f73e112SMichael Grzeschik const u8 *regs; 221e66f840cSTristram Ha u16 ctrl_addr; 222e66f840cSTristram Ha u32 data; 223e66f840cSTristram Ha u8 check; 224e66f840cSTristram Ha int loop; 225e66f840cSTristram Ha 2269f73e112SMichael Grzeschik masks = ksz8->masks; 2279f73e112SMichael Grzeschik regs = ksz8->regs; 2289f73e112SMichael Grzeschik 229*a530e6f2SArun Ramadoss ctrl_addr = addr + dev->info->reg_mib_cnt * port; 230e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 231e66f840cSTristram Ha 232e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 2339f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 234e66f840cSTristram Ha 235e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 236e66f840cSTristram Ha * slow SPI speed. 237e66f840cSTristram Ha */ 238e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 2399f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check); 240e66f840cSTristram Ha 2419f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_VALID]) { 2429f73e112SMichael Grzeschik ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 2439f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) 244e66f840cSTristram Ha *cnt += MIB_COUNTER_VALUE + 1; 245e66f840cSTristram Ha *cnt += data & MIB_COUNTER_VALUE; 246e66f840cSTristram Ha break; 247e66f840cSTristram Ha } 248e66f840cSTristram Ha } 249e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 250e66f840cSTristram Ha } 251e66f840cSTristram Ha 2524b20a07eSOleksij Rempel static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 253e66f840cSTristram Ha u64 *dropped, u64 *cnt) 254e66f840cSTristram Ha { 2559f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 2569f73e112SMichael Grzeschik const u32 *masks; 2579f73e112SMichael Grzeschik const u8 *regs; 258e66f840cSTristram Ha u16 ctrl_addr; 259e66f840cSTristram Ha u32 data; 260e66f840cSTristram Ha u8 check; 261e66f840cSTristram Ha int loop; 262e66f840cSTristram Ha 2639f73e112SMichael Grzeschik masks = ksz8->masks; 2649f73e112SMichael Grzeschik regs = ksz8->regs; 2659f73e112SMichael Grzeschik 266*a530e6f2SArun Ramadoss addr -= dev->info->reg_mib_cnt; 2674b20a07eSOleksij Rempel ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port; 2684b20a07eSOleksij Rempel ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0; 269e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 270e66f840cSTristram Ha 271e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 2729f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 273e66f840cSTristram Ha 274e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 275e66f840cSTristram Ha * slow SPI speed. 276e66f840cSTristram Ha */ 277e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 2789f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check); 279e66f840cSTristram Ha 2809f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_VALID]) { 2819f73e112SMichael Grzeschik ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 282e66f840cSTristram Ha if (addr < 2) { 283e66f840cSTristram Ha u64 total; 284e66f840cSTristram Ha 285e66f840cSTristram Ha total = check & MIB_TOTAL_BYTES_H; 286e66f840cSTristram Ha total <<= 32; 287e66f840cSTristram Ha *cnt += total; 288e66f840cSTristram Ha *cnt += data; 2899f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) { 290e66f840cSTristram Ha total = MIB_TOTAL_BYTES_H + 1; 291e66f840cSTristram Ha total <<= 32; 292e66f840cSTristram Ha *cnt += total; 293e66f840cSTristram Ha } 294e66f840cSTristram Ha } else { 2959f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) 296e66f840cSTristram Ha *cnt += MIB_PACKET_DROPPED + 1; 297e66f840cSTristram Ha *cnt += data & MIB_PACKET_DROPPED; 298e66f840cSTristram Ha } 299e66f840cSTristram Ha break; 300e66f840cSTristram Ha } 301e66f840cSTristram Ha } 302e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 303e66f840cSTristram Ha } 304e66f840cSTristram Ha 3054b20a07eSOleksij Rempel static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 3064b20a07eSOleksij Rempel u64 *dropped, u64 *cnt) 3074b20a07eSOleksij Rempel { 3084b20a07eSOleksij Rempel struct ksz8 *ksz8 = dev->priv; 3094b20a07eSOleksij Rempel const u8 *regs = ksz8->regs; 3104b20a07eSOleksij Rempel u32 *last = (u32 *)dropped; 3114b20a07eSOleksij Rempel u16 ctrl_addr; 3124b20a07eSOleksij Rempel u32 data; 3134b20a07eSOleksij Rempel u32 cur; 3144b20a07eSOleksij Rempel 315*a530e6f2SArun Ramadoss addr -= dev->info->reg_mib_cnt; 3164b20a07eSOleksij Rempel ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 : 3174b20a07eSOleksij Rempel KSZ8863_MIB_PACKET_DROPPED_RX_0; 3184b20a07eSOleksij Rempel ctrl_addr += port; 3194b20a07eSOleksij Rempel ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 3204b20a07eSOleksij Rempel 3214b20a07eSOleksij Rempel mutex_lock(&dev->alu_mutex); 3224b20a07eSOleksij Rempel ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 3234b20a07eSOleksij Rempel ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 3244b20a07eSOleksij Rempel mutex_unlock(&dev->alu_mutex); 3254b20a07eSOleksij Rempel 3264b20a07eSOleksij Rempel data &= MIB_PACKET_DROPPED; 3274b20a07eSOleksij Rempel cur = last[addr]; 3284b20a07eSOleksij Rempel if (data != cur) { 3294b20a07eSOleksij Rempel last[addr] = data; 3304b20a07eSOleksij Rempel if (data < cur) 3314b20a07eSOleksij Rempel data += MIB_PACKET_DROPPED + 1; 3324b20a07eSOleksij Rempel data -= cur; 3334b20a07eSOleksij Rempel *cnt += data; 3344b20a07eSOleksij Rempel } 3354b20a07eSOleksij Rempel } 3364b20a07eSOleksij Rempel 3374b20a07eSOleksij Rempel static void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 3384b20a07eSOleksij Rempel u64 *dropped, u64 *cnt) 3394b20a07eSOleksij Rempel { 3404b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 3414b20a07eSOleksij Rempel ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt); 3424b20a07eSOleksij Rempel else 3434b20a07eSOleksij Rempel ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt); 3444b20a07eSOleksij Rempel } 3454b20a07eSOleksij Rempel 3464b5baca0SMichael Grzeschik static void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze) 347e66f840cSTristram Ha { 3484b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 3494b20a07eSOleksij Rempel return; 3504b20a07eSOleksij Rempel 351e66f840cSTristram Ha /* enable the port for flush/freeze function */ 352e66f840cSTristram Ha if (freeze) 353e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 354e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze); 355e66f840cSTristram Ha 356e66f840cSTristram Ha /* disable the port after freeze is done */ 357e66f840cSTristram Ha if (!freeze) 358e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 359e66f840cSTristram Ha } 360e66f840cSTristram Ha 3614b5baca0SMichael Grzeschik static void ksz8_port_init_cnt(struct ksz_device *dev, int port) 362e66f840cSTristram Ha { 363e66f840cSTristram Ha struct ksz_port_mib *mib = &dev->ports[port].mib; 3644b20a07eSOleksij Rempel u64 *dropped; 365e66f840cSTristram Ha 3664b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) { 367e66f840cSTristram Ha /* flush all enabled port MIB counters */ 368e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 369e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true); 370e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 3714b20a07eSOleksij Rempel } 372e66f840cSTristram Ha 373e66f840cSTristram Ha mib->cnt_ptr = 0; 374e66f840cSTristram Ha 375e66f840cSTristram Ha /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */ 376*a530e6f2SArun Ramadoss while (mib->cnt_ptr < dev->info->reg_mib_cnt) { 377e66f840cSTristram Ha dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr, 378e66f840cSTristram Ha &mib->counters[mib->cnt_ptr]); 379e66f840cSTristram Ha ++mib->cnt_ptr; 380e66f840cSTristram Ha } 381e66f840cSTristram Ha 3824b20a07eSOleksij Rempel /* last one in storage */ 383*a530e6f2SArun Ramadoss dropped = &mib->counters[dev->info->mib_cnt]; 3844b20a07eSOleksij Rempel 385e66f840cSTristram Ha /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */ 386*a530e6f2SArun Ramadoss while (mib->cnt_ptr < dev->info->mib_cnt) { 387e66f840cSTristram Ha dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr, 3884b20a07eSOleksij Rempel dropped, &mib->counters[mib->cnt_ptr]); 389e66f840cSTristram Ha ++mib->cnt_ptr; 390e66f840cSTristram Ha } 391e66f840cSTristram Ha mib->cnt_ptr = 0; 392*a530e6f2SArun Ramadoss memset(mib->counters, 0, dev->info->mib_cnt * sizeof(u64)); 393e66f840cSTristram Ha } 394e66f840cSTristram Ha 3954b5baca0SMichael Grzeschik static void ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data) 396e66f840cSTristram Ha { 3979f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 3989f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 399e66f840cSTristram Ha u16 ctrl_addr; 400e66f840cSTristram Ha 401e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr; 402e66f840cSTristram Ha 403e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 4049f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 4059f73e112SMichael Grzeschik ksz_read64(dev, regs[REG_IND_DATA_HI], data); 406e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 407e66f840cSTristram Ha } 408e66f840cSTristram Ha 4094b5baca0SMichael Grzeschik static void ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data) 410e66f840cSTristram Ha { 4119f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 4129f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 413e66f840cSTristram Ha u16 ctrl_addr; 414e66f840cSTristram Ha 415e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table) | addr; 416e66f840cSTristram Ha 417e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 4189f73e112SMichael Grzeschik ksz_write64(dev, regs[REG_IND_DATA_HI], data); 4199f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 420e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 421e66f840cSTristram Ha } 422e66f840cSTristram Ha 4234b5baca0SMichael Grzeschik static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data) 424e66f840cSTristram Ha { 4259f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 426e66f840cSTristram Ha int timeout = 100; 4279f73e112SMichael Grzeschik const u32 *masks; 4289f73e112SMichael Grzeschik const u8 *regs; 4299f73e112SMichael Grzeschik 4309f73e112SMichael Grzeschik masks = ksz8->masks; 4319f73e112SMichael Grzeschik regs = ksz8->regs; 432e66f840cSTristram Ha 433e66f840cSTristram Ha do { 4349f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_DATA_CHECK], data); 435e66f840cSTristram Ha timeout--; 4369f73e112SMichael Grzeschik } while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout); 437e66f840cSTristram Ha 438e66f840cSTristram Ha /* Entry is not ready for accessing. */ 4399f73e112SMichael Grzeschik if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) { 440e66f840cSTristram Ha return -EAGAIN; 441e66f840cSTristram Ha /* Entry is ready for accessing. */ 442e66f840cSTristram Ha } else { 4439f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_DATA_8], data); 444e66f840cSTristram Ha 445e66f840cSTristram Ha /* There is no valid entry in the table. */ 4469f73e112SMichael Grzeschik if (*data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY]) 447e66f840cSTristram Ha return -ENXIO; 448e66f840cSTristram Ha } 449e66f840cSTristram Ha return 0; 450e66f840cSTristram Ha } 451e66f840cSTristram Ha 4524b5baca0SMichael Grzeschik static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, 453e66f840cSTristram Ha u8 *mac_addr, u8 *fid, u8 *src_port, 454e66f840cSTristram Ha u8 *timestamp, u16 *entries) 455e66f840cSTristram Ha { 4569f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 457e66f840cSTristram Ha u32 data_hi, data_lo; 4589f73e112SMichael Grzeschik const u8 *shifts; 4599f73e112SMichael Grzeschik const u32 *masks; 4609f73e112SMichael Grzeschik const u8 *regs; 461e66f840cSTristram Ha u16 ctrl_addr; 462e66f840cSTristram Ha u8 data; 463e66f840cSTristram Ha int rc; 464e66f840cSTristram Ha 4659f73e112SMichael Grzeschik shifts = ksz8->shifts; 4669f73e112SMichael Grzeschik masks = ksz8->masks; 4679f73e112SMichael Grzeschik regs = ksz8->regs; 4689f73e112SMichael Grzeschik 469e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr; 470e66f840cSTristram Ha 471e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 4729f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 473e66f840cSTristram Ha 4744b5baca0SMichael Grzeschik rc = ksz8_valid_dyn_entry(dev, &data); 475e66f840cSTristram Ha if (rc == -EAGAIN) { 476e66f840cSTristram Ha if (addr == 0) 477e66f840cSTristram Ha *entries = 0; 478e66f840cSTristram Ha } else if (rc == -ENXIO) { 479e66f840cSTristram Ha *entries = 0; 480e66f840cSTristram Ha /* At least one valid entry in the table. */ 481e66f840cSTristram Ha } else { 482e66f840cSTristram Ha u64 buf = 0; 483e66f840cSTristram Ha int cnt; 484e66f840cSTristram Ha 4859f73e112SMichael Grzeschik ksz_read64(dev, regs[REG_IND_DATA_HI], &buf); 486e66f840cSTristram Ha data_hi = (u32)(buf >> 32); 487e66f840cSTristram Ha data_lo = (u32)buf; 488e66f840cSTristram Ha 489e66f840cSTristram Ha /* Check out how many valid entry in the table. */ 4909f73e112SMichael Grzeschik cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H]; 4919f73e112SMichael Grzeschik cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H]; 4929f73e112SMichael Grzeschik cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >> 4939f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_ENTRIES]; 494e66f840cSTristram Ha *entries = cnt + 1; 495e66f840cSTristram Ha 4969f73e112SMichael Grzeschik *fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >> 4979f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_FID]; 4989f73e112SMichael Grzeschik *src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >> 4999f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_SRC_PORT]; 5009f73e112SMichael Grzeschik *timestamp = (data_hi & masks[DYNAMIC_MAC_TABLE_TIMESTAMP]) >> 5019f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_TIMESTAMP]; 502e66f840cSTristram Ha 503e66f840cSTristram Ha mac_addr[5] = (u8)data_lo; 504e66f840cSTristram Ha mac_addr[4] = (u8)(data_lo >> 8); 505e66f840cSTristram Ha mac_addr[3] = (u8)(data_lo >> 16); 506e66f840cSTristram Ha mac_addr[2] = (u8)(data_lo >> 24); 507e66f840cSTristram Ha 508e66f840cSTristram Ha mac_addr[1] = (u8)data_hi; 509e66f840cSTristram Ha mac_addr[0] = (u8)(data_hi >> 8); 510e66f840cSTristram Ha rc = 0; 511e66f840cSTristram Ha } 512e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 513e66f840cSTristram Ha 514e66f840cSTristram Ha return rc; 515e66f840cSTristram Ha } 516e66f840cSTristram Ha 5174b5baca0SMichael Grzeschik static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr, 518e66f840cSTristram Ha struct alu_struct *alu) 519e66f840cSTristram Ha { 5209f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 521e66f840cSTristram Ha u32 data_hi, data_lo; 5229f73e112SMichael Grzeschik const u8 *shifts; 5239f73e112SMichael Grzeschik const u32 *masks; 524e66f840cSTristram Ha u64 data; 525e66f840cSTristram Ha 5269f73e112SMichael Grzeschik shifts = ksz8->shifts; 5279f73e112SMichael Grzeschik masks = ksz8->masks; 5289f73e112SMichael Grzeschik 5294b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data); 530e66f840cSTristram Ha data_hi = data >> 32; 531e66f840cSTristram Ha data_lo = (u32)data; 5329f73e112SMichael Grzeschik if (data_hi & (masks[STATIC_MAC_TABLE_VALID] | 5339f73e112SMichael Grzeschik masks[STATIC_MAC_TABLE_OVERRIDE])) { 534e66f840cSTristram Ha alu->mac[5] = (u8)data_lo; 535e66f840cSTristram Ha alu->mac[4] = (u8)(data_lo >> 8); 536e66f840cSTristram Ha alu->mac[3] = (u8)(data_lo >> 16); 537e66f840cSTristram Ha alu->mac[2] = (u8)(data_lo >> 24); 538e66f840cSTristram Ha alu->mac[1] = (u8)data_hi; 539e66f840cSTristram Ha alu->mac[0] = (u8)(data_hi >> 8); 5409f73e112SMichael Grzeschik alu->port_forward = 5419f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >> 5429f73e112SMichael Grzeschik shifts[STATIC_MAC_FWD_PORTS]; 543e66f840cSTristram Ha alu->is_override = 5449f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0; 545e66f840cSTristram Ha data_hi >>= 1; 5469f73e112SMichael Grzeschik alu->is_static = true; 5479f73e112SMichael Grzeschik alu->is_use_fid = 5489f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0; 5499f73e112SMichael Grzeschik alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >> 5509f73e112SMichael Grzeschik shifts[STATIC_MAC_FID]; 551e66f840cSTristram Ha return 0; 552e66f840cSTristram Ha } 553e66f840cSTristram Ha return -ENXIO; 554e66f840cSTristram Ha } 555e66f840cSTristram Ha 5564b5baca0SMichael Grzeschik static void ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr, 557e66f840cSTristram Ha struct alu_struct *alu) 558e66f840cSTristram Ha { 5599f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 560e66f840cSTristram Ha u32 data_hi, data_lo; 5619f73e112SMichael Grzeschik const u8 *shifts; 5629f73e112SMichael Grzeschik const u32 *masks; 563e66f840cSTristram Ha u64 data; 564e66f840cSTristram Ha 5659f73e112SMichael Grzeschik shifts = ksz8->shifts; 5669f73e112SMichael Grzeschik masks = ksz8->masks; 5679f73e112SMichael Grzeschik 568e66f840cSTristram Ha data_lo = ((u32)alu->mac[2] << 24) | 569e66f840cSTristram Ha ((u32)alu->mac[3] << 16) | 570e66f840cSTristram Ha ((u32)alu->mac[4] << 8) | alu->mac[5]; 571e66f840cSTristram Ha data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1]; 5729f73e112SMichael Grzeschik data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS]; 573e66f840cSTristram Ha 574e66f840cSTristram Ha if (alu->is_override) 5759f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE]; 576e66f840cSTristram Ha if (alu->is_use_fid) { 5779f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_USE_FID]; 5789f73e112SMichael Grzeschik data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID]; 579e66f840cSTristram Ha } 580e66f840cSTristram Ha if (alu->is_static) 5819f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_VALID]; 582e66f840cSTristram Ha else 5839f73e112SMichael Grzeschik data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE]; 584e66f840cSTristram Ha 585e66f840cSTristram Ha data = (u64)data_hi << 32 | data_lo; 5864b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data); 587e66f840cSTristram Ha } 588e66f840cSTristram Ha 5899f73e112SMichael Grzeschik static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid, 5909f73e112SMichael Grzeschik u8 *member, u8 *valid) 591e66f840cSTristram Ha { 5929f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 5939f73e112SMichael Grzeschik const u8 *shifts; 5949f73e112SMichael Grzeschik const u32 *masks; 5959f73e112SMichael Grzeschik 5969f73e112SMichael Grzeschik shifts = ksz8->shifts; 5979f73e112SMichael Grzeschik masks = ksz8->masks; 5989f73e112SMichael Grzeschik 5999f73e112SMichael Grzeschik *fid = vlan & masks[VLAN_TABLE_FID]; 6009f73e112SMichael Grzeschik *member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >> 6019f73e112SMichael Grzeschik shifts[VLAN_TABLE_MEMBERSHIP_S]; 6029f73e112SMichael Grzeschik *valid = !!(vlan & masks[VLAN_TABLE_VALID]); 603e66f840cSTristram Ha } 604e66f840cSTristram Ha 6059f73e112SMichael Grzeschik static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid, 6069f73e112SMichael Grzeschik u16 *vlan) 607e66f840cSTristram Ha { 6089f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 6099f73e112SMichael Grzeschik const u8 *shifts; 6109f73e112SMichael Grzeschik const u32 *masks; 6119f73e112SMichael Grzeschik 6129f73e112SMichael Grzeschik shifts = ksz8->shifts; 6139f73e112SMichael Grzeschik masks = ksz8->masks; 6149f73e112SMichael Grzeschik 615e66f840cSTristram Ha *vlan = fid; 6169f73e112SMichael Grzeschik *vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S]; 617e66f840cSTristram Ha if (valid) 6189f73e112SMichael Grzeschik *vlan |= masks[VLAN_TABLE_VALID]; 619e66f840cSTristram Ha } 620e66f840cSTristram Ha 6214b5baca0SMichael Grzeschik static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr) 622e66f840cSTristram Ha { 6239f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 6249f73e112SMichael Grzeschik const u8 *shifts; 625e66f840cSTristram Ha u64 data; 626e66f840cSTristram Ha int i; 627e66f840cSTristram Ha 6289f73e112SMichael Grzeschik shifts = ksz8->shifts; 6299f73e112SMichael Grzeschik 6304b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &data); 631411d466dSBen Hutchings addr *= 4; 632411d466dSBen Hutchings for (i = 0; i < 4; i++) { 633e66f840cSTristram Ha dev->vlan_cache[addr + i].table[0] = (u16)data; 6349f73e112SMichael Grzeschik data >>= shifts[VLAN_TABLE]; 635e66f840cSTristram Ha } 636e66f840cSTristram Ha } 637e66f840cSTristram Ha 6384b5baca0SMichael Grzeschik static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan) 639e66f840cSTristram Ha { 640e66f840cSTristram Ha int index; 641e66f840cSTristram Ha u16 *data; 642e66f840cSTristram Ha u16 addr; 643e66f840cSTristram Ha u64 buf; 644e66f840cSTristram Ha 645e66f840cSTristram Ha data = (u16 *)&buf; 646411d466dSBen Hutchings addr = vid / 4; 647e66f840cSTristram Ha index = vid & 3; 6484b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 649e66f840cSTristram Ha *vlan = data[index]; 650e66f840cSTristram Ha } 651e66f840cSTristram Ha 6524b5baca0SMichael Grzeschik static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan) 653e66f840cSTristram Ha { 654e66f840cSTristram Ha int index; 655e66f840cSTristram Ha u16 *data; 656e66f840cSTristram Ha u16 addr; 657e66f840cSTristram Ha u64 buf; 658e66f840cSTristram Ha 659e66f840cSTristram Ha data = (u16 *)&buf; 660411d466dSBen Hutchings addr = vid / 4; 661e66f840cSTristram Ha index = vid & 3; 6624b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 663e66f840cSTristram Ha data[index] = vlan; 664e66f840cSTristram Ha dev->vlan_cache[vid].table[0] = vlan; 6654b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_VLAN, addr, buf); 666e66f840cSTristram Ha } 667e66f840cSTristram Ha 6684b5baca0SMichael Grzeschik static void ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val) 669e66f840cSTristram Ha { 6709f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 671e66f840cSTristram Ha u8 restart, speed, ctrl, link; 6729f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 673e66f840cSTristram Ha int processed = true; 67436838050SOleksij Rempel u8 val1, val2; 675e66f840cSTristram Ha u16 data = 0; 676e66f840cSTristram Ha u8 p = phy; 677e66f840cSTristram Ha 678e66f840cSTristram Ha switch (reg) { 679ec4b94f9SMichael Grzeschik case MII_BMCR: 6809f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart); 6819f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed); 6829f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl); 683e66f840cSTristram Ha if (restart & PORT_PHY_LOOPBACK) 684ec4b94f9SMichael Grzeschik data |= BMCR_LOOPBACK; 685e66f840cSTristram Ha if (ctrl & PORT_FORCE_100_MBIT) 686ec4b94f9SMichael Grzeschik data |= BMCR_SPEED100; 6874b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 6884b20a07eSOleksij Rempel if ((ctrl & PORT_AUTO_NEG_ENABLE)) 689ec4b94f9SMichael Grzeschik data |= BMCR_ANENABLE; 6904b20a07eSOleksij Rempel } else { 691e66f840cSTristram Ha if (!(ctrl & PORT_AUTO_NEG_DISABLE)) 692ec4b94f9SMichael Grzeschik data |= BMCR_ANENABLE; 6934b20a07eSOleksij Rempel } 694e66f840cSTristram Ha if (restart & PORT_POWER_DOWN) 695ec4b94f9SMichael Grzeschik data |= BMCR_PDOWN; 696e66f840cSTristram Ha if (restart & PORT_AUTO_NEG_RESTART) 697ec4b94f9SMichael Grzeschik data |= BMCR_ANRESTART; 698e66f840cSTristram Ha if (ctrl & PORT_FORCE_FULL_DUPLEX) 699ec4b94f9SMichael Grzeschik data |= BMCR_FULLDPLX; 700e66f840cSTristram Ha if (speed & PORT_HP_MDIX) 701ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_HP_MDIX; 702e66f840cSTristram Ha if (restart & PORT_FORCE_MDIX) 703ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_FORCE_MDI; 704e66f840cSTristram Ha if (restart & PORT_AUTO_MDIX_DISABLE) 705ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_AUTO_MDIX; 706e66f840cSTristram Ha if (restart & PORT_TX_DISABLE) 707ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_TRANSMIT; 708e66f840cSTristram Ha if (restart & PORT_LED_OFF) 709ec4b94f9SMichael Grzeschik data |= KSZ886X_BMCR_DISABLE_LED; 710e66f840cSTristram Ha break; 711ec4b94f9SMichael Grzeschik case MII_BMSR: 7129f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LINK_STATUS], &link); 713ec4b94f9SMichael Grzeschik data = BMSR_100FULL | 714ec4b94f9SMichael Grzeschik BMSR_100HALF | 715ec4b94f9SMichael Grzeschik BMSR_10FULL | 716ec4b94f9SMichael Grzeschik BMSR_10HALF | 717ec4b94f9SMichael Grzeschik BMSR_ANEGCAPABLE; 718e66f840cSTristram Ha if (link & PORT_AUTO_NEG_COMPLETE) 719ec4b94f9SMichael Grzeschik data |= BMSR_ANEGCOMPLETE; 720e66f840cSTristram Ha if (link & PORT_STAT_LINK_GOOD) 721ec4b94f9SMichael Grzeschik data |= BMSR_LSTATUS; 722e66f840cSTristram Ha break; 723ec4b94f9SMichael Grzeschik case MII_PHYSID1: 724e66f840cSTristram Ha data = KSZ8795_ID_HI; 725e66f840cSTristram Ha break; 726ec4b94f9SMichael Grzeschik case MII_PHYSID2: 7274b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 7284b20a07eSOleksij Rempel data = KSZ8863_ID_LO; 7294b20a07eSOleksij Rempel else 730e66f840cSTristram Ha data = KSZ8795_ID_LO; 731e66f840cSTristram Ha break; 732ec4b94f9SMichael Grzeschik case MII_ADVERTISE: 7339f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl); 734ec4b94f9SMichael Grzeschik data = ADVERTISE_CSMA; 735e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_SYM_PAUSE) 736ec4b94f9SMichael Grzeschik data |= ADVERTISE_PAUSE_CAP; 737e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX_FD) 738ec4b94f9SMichael Grzeschik data |= ADVERTISE_100FULL; 739e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX) 740ec4b94f9SMichael Grzeschik data |= ADVERTISE_100HALF; 741e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT_FD) 742ec4b94f9SMichael Grzeschik data |= ADVERTISE_10FULL; 743e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT) 744ec4b94f9SMichael Grzeschik data |= ADVERTISE_10HALF; 745e66f840cSTristram Ha break; 746ec4b94f9SMichael Grzeschik case MII_LPA: 7479f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link); 748ec4b94f9SMichael Grzeschik data = LPA_SLCT; 749e66f840cSTristram Ha if (link & PORT_REMOTE_SYM_PAUSE) 750ec4b94f9SMichael Grzeschik data |= LPA_PAUSE_CAP; 751e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX_FD) 752ec4b94f9SMichael Grzeschik data |= LPA_100FULL; 753e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX) 754ec4b94f9SMichael Grzeschik data |= LPA_100HALF; 755e66f840cSTristram Ha if (link & PORT_REMOTE_10BT_FD) 756ec4b94f9SMichael Grzeschik data |= LPA_10FULL; 757e66f840cSTristram Ha if (link & PORT_REMOTE_10BT) 758ec4b94f9SMichael Grzeschik data |= LPA_10HALF; 759ec4b94f9SMichael Grzeschik if (data & ~LPA_SLCT) 760ec4b94f9SMichael Grzeschik data |= LPA_LPACK; 761e66f840cSTristram Ha break; 76236838050SOleksij Rempel case PHY_REG_LINK_MD: 76336838050SOleksij Rempel ksz_pread8(dev, p, REG_PORT_LINK_MD_CTRL, &val1); 76436838050SOleksij Rempel ksz_pread8(dev, p, REG_PORT_LINK_MD_RESULT, &val2); 76536838050SOleksij Rempel if (val1 & PORT_START_CABLE_DIAG) 76636838050SOleksij Rempel data |= PHY_START_CABLE_DIAG; 76736838050SOleksij Rempel 76836838050SOleksij Rempel if (val1 & PORT_CABLE_10M_SHORT) 76936838050SOleksij Rempel data |= PHY_CABLE_10M_SHORT; 77036838050SOleksij Rempel 77136838050SOleksij Rempel data |= FIELD_PREP(PHY_CABLE_DIAG_RESULT_M, 77236838050SOleksij Rempel FIELD_GET(PORT_CABLE_DIAG_RESULT_M, val1)); 77336838050SOleksij Rempel 77436838050SOleksij Rempel data |= FIELD_PREP(PHY_CABLE_FAULT_COUNTER_M, 77536838050SOleksij Rempel (FIELD_GET(PORT_CABLE_FAULT_COUNTER_H, val1) << 8) | 77636838050SOleksij Rempel FIELD_GET(PORT_CABLE_FAULT_COUNTER_L, val2)); 77736838050SOleksij Rempel break; 77852939393SOleksij Rempel case PHY_REG_PHY_CTRL: 77952939393SOleksij Rempel ksz_pread8(dev, p, regs[P_LINK_STATUS], &link); 78052939393SOleksij Rempel if (link & PORT_MDIX_STATUS) 78152939393SOleksij Rempel data |= KSZ886X_CTRL_MDIX_STAT; 78252939393SOleksij Rempel break; 783e66f840cSTristram Ha default: 784e66f840cSTristram Ha processed = false; 785e66f840cSTristram Ha break; 786e66f840cSTristram Ha } 787e66f840cSTristram Ha if (processed) 788e66f840cSTristram Ha *val = data; 789e66f840cSTristram Ha } 790e66f840cSTristram Ha 7914b5baca0SMichael Grzeschik static void ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val) 792e66f840cSTristram Ha { 7939f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 794e66f840cSTristram Ha u8 restart, speed, ctrl, data; 7959f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 7969f73e112SMichael Grzeschik u8 p = phy; 797e66f840cSTristram Ha 798e66f840cSTristram Ha switch (reg) { 799ec4b94f9SMichael Grzeschik case MII_BMCR: 800e66f840cSTristram Ha 801e66f840cSTristram Ha /* Do not support PHY reset function. */ 802ec4b94f9SMichael Grzeschik if (val & BMCR_RESET) 803e66f840cSTristram Ha break; 8049f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed); 805e66f840cSTristram Ha data = speed; 806ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_HP_MDIX) 807e66f840cSTristram Ha data |= PORT_HP_MDIX; 808e66f840cSTristram Ha else 809e66f840cSTristram Ha data &= ~PORT_HP_MDIX; 810e66f840cSTristram Ha if (data != speed) 8119f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_SPEED_STATUS], data); 8129f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl); 813e66f840cSTristram Ha data = ctrl; 8144b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 815ec4b94f9SMichael Grzeschik if ((val & BMCR_ANENABLE)) 8164b20a07eSOleksij Rempel data |= PORT_AUTO_NEG_ENABLE; 8174b20a07eSOleksij Rempel else 8184b20a07eSOleksij Rempel data &= ~PORT_AUTO_NEG_ENABLE; 8194b20a07eSOleksij Rempel } else { 820ec4b94f9SMichael Grzeschik if (!(val & BMCR_ANENABLE)) 821e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 822e66f840cSTristram Ha else 823e66f840cSTristram Ha data &= ~PORT_AUTO_NEG_DISABLE; 824e66f840cSTristram Ha 825e66f840cSTristram Ha /* Fiber port does not support auto-negotiation. */ 826e66f840cSTristram Ha if (dev->ports[p].fiber) 827e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 8284b20a07eSOleksij Rempel } 8294b20a07eSOleksij Rempel 830ec4b94f9SMichael Grzeschik if (val & BMCR_SPEED100) 831e66f840cSTristram Ha data |= PORT_FORCE_100_MBIT; 832e66f840cSTristram Ha else 833e66f840cSTristram Ha data &= ~PORT_FORCE_100_MBIT; 834ec4b94f9SMichael Grzeschik if (val & BMCR_FULLDPLX) 835e66f840cSTristram Ha data |= PORT_FORCE_FULL_DUPLEX; 836e66f840cSTristram Ha else 837e66f840cSTristram Ha data &= ~PORT_FORCE_FULL_DUPLEX; 838e66f840cSTristram Ha if (data != ctrl) 8399f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_FORCE_CTRL], data); 8409f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart); 841e66f840cSTristram Ha data = restart; 842ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_LED) 843e66f840cSTristram Ha data |= PORT_LED_OFF; 844e66f840cSTristram Ha else 845e66f840cSTristram Ha data &= ~PORT_LED_OFF; 846ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_TRANSMIT) 847e66f840cSTristram Ha data |= PORT_TX_DISABLE; 848e66f840cSTristram Ha else 849e66f840cSTristram Ha data &= ~PORT_TX_DISABLE; 850ec4b94f9SMichael Grzeschik if (val & BMCR_ANRESTART) 851e66f840cSTristram Ha data |= PORT_AUTO_NEG_RESTART; 852e66f840cSTristram Ha else 853e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_RESTART); 854ec4b94f9SMichael Grzeschik if (val & BMCR_PDOWN) 855e66f840cSTristram Ha data |= PORT_POWER_DOWN; 856e66f840cSTristram Ha else 857e66f840cSTristram Ha data &= ~PORT_POWER_DOWN; 858ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX) 859e66f840cSTristram Ha data |= PORT_AUTO_MDIX_DISABLE; 860e66f840cSTristram Ha else 861e66f840cSTristram Ha data &= ~PORT_AUTO_MDIX_DISABLE; 862ec4b94f9SMichael Grzeschik if (val & KSZ886X_BMCR_FORCE_MDI) 863e66f840cSTristram Ha data |= PORT_FORCE_MDIX; 864e66f840cSTristram Ha else 865e66f840cSTristram Ha data &= ~PORT_FORCE_MDIX; 866ec4b94f9SMichael Grzeschik if (val & BMCR_LOOPBACK) 867e66f840cSTristram Ha data |= PORT_PHY_LOOPBACK; 868e66f840cSTristram Ha else 869e66f840cSTristram Ha data &= ~PORT_PHY_LOOPBACK; 870e66f840cSTristram Ha if (data != restart) 8719f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_NEG_RESTART_CTRL], data); 872e66f840cSTristram Ha break; 873ec4b94f9SMichael Grzeschik case MII_ADVERTISE: 8749f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl); 875e66f840cSTristram Ha data = ctrl; 876e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_SYM_PAUSE | 877e66f840cSTristram Ha PORT_AUTO_NEG_100BTX_FD | 878e66f840cSTristram Ha PORT_AUTO_NEG_100BTX | 879e66f840cSTristram Ha PORT_AUTO_NEG_10BT_FD | 880e66f840cSTristram Ha PORT_AUTO_NEG_10BT); 881ec4b94f9SMichael Grzeschik if (val & ADVERTISE_PAUSE_CAP) 882e66f840cSTristram Ha data |= PORT_AUTO_NEG_SYM_PAUSE; 883ec4b94f9SMichael Grzeschik if (val & ADVERTISE_100FULL) 884e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX_FD; 885ec4b94f9SMichael Grzeschik if (val & ADVERTISE_100HALF) 886e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX; 887ec4b94f9SMichael Grzeschik if (val & ADVERTISE_10FULL) 888e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT_FD; 889ec4b94f9SMichael Grzeschik if (val & ADVERTISE_10HALF) 890e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT; 891e66f840cSTristram Ha if (data != ctrl) 8929f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data); 893e66f840cSTristram Ha break; 89436838050SOleksij Rempel case PHY_REG_LINK_MD: 89536838050SOleksij Rempel if (val & PHY_START_CABLE_DIAG) 89636838050SOleksij Rempel ksz_port_cfg(dev, p, REG_PORT_LINK_MD_CTRL, PORT_START_CABLE_DIAG, true); 89736838050SOleksij Rempel break; 898e66f840cSTristram Ha default: 899e66f840cSTristram Ha break; 900e66f840cSTristram Ha } 901e66f840cSTristram Ha } 902e66f840cSTristram Ha 9034b5baca0SMichael Grzeschik static enum dsa_tag_protocol ksz8_get_tag_protocol(struct dsa_switch *ds, 9044d776482SFlorian Fainelli int port, 9054d776482SFlorian Fainelli enum dsa_tag_protocol mp) 906e66f840cSTristram Ha { 9074b20a07eSOleksij Rempel struct ksz_device *dev = ds->priv; 9084b20a07eSOleksij Rempel 9094b20a07eSOleksij Rempel /* ksz88x3 uses the same tag schema as KSZ9893 */ 9104b20a07eSOleksij Rempel return ksz_is_ksz88x3(dev) ? 9114b20a07eSOleksij Rempel DSA_TAG_PROTO_KSZ9893 : DSA_TAG_PROTO_KSZ8795; 912e66f840cSTristram Ha } 913e66f840cSTristram Ha 91449011e0cSOleksij Rempel static u32 ksz8_sw_get_phy_flags(struct dsa_switch *ds, int port) 91549011e0cSOleksij Rempel { 91649011e0cSOleksij Rempel /* Silicon Errata Sheet (DS80000830A): 91749011e0cSOleksij Rempel * Port 1 does not work with LinkMD Cable-Testing. 91849011e0cSOleksij Rempel * Port 1 does not respond to received PAUSE control frames. 91949011e0cSOleksij Rempel */ 92049011e0cSOleksij Rempel if (!port) 92149011e0cSOleksij Rempel return MICREL_KSZ8_P1_ERRATA; 92249011e0cSOleksij Rempel 92349011e0cSOleksij Rempel return 0; 92449011e0cSOleksij Rempel } 92549011e0cSOleksij Rempel 9264b5baca0SMichael Grzeschik static void ksz8_get_strings(struct dsa_switch *ds, int port, 927e66f840cSTristram Ha u32 stringset, uint8_t *buf) 928e66f840cSTristram Ha { 92965fe1acfSMichael Grzeschik struct ksz_device *dev = ds->priv; 930e66f840cSTristram Ha int i; 931e66f840cSTristram Ha 932*a530e6f2SArun Ramadoss for (i = 0; i < dev->info->mib_cnt; i++) { 9334b20a07eSOleksij Rempel memcpy(buf + i * ETH_GSTRING_LEN, 934*a530e6f2SArun Ramadoss dev->info->mib_names[i].string, ETH_GSTRING_LEN); 935e66f840cSTristram Ha } 936e66f840cSTristram Ha } 937e66f840cSTristram Ha 9384b5baca0SMichael Grzeschik static void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member) 939e66f840cSTristram Ha { 940e66f840cSTristram Ha u8 data; 941e66f840cSTristram Ha 942e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 943e66f840cSTristram Ha data &= ~PORT_VLAN_MEMBERSHIP; 944e66f840cSTristram Ha data |= (member & dev->port_mask); 945e66f840cSTristram Ha ksz_pwrite8(dev, port, P_MIRROR_CTRL, data); 946e66f840cSTristram Ha } 947e66f840cSTristram Ha 9484b5baca0SMichael Grzeschik static void ksz8_port_stp_state_set(struct dsa_switch *ds, int port, u8 state) 949e66f840cSTristram Ha { 950de6dd626SArun Ramadoss ksz_port_stp_state_set(ds, port, state, P_STP_CTRL); 951e66f840cSTristram Ha } 952e66f840cSTristram Ha 9534b5baca0SMichael Grzeschik static void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port) 954e66f840cSTristram Ha { 955241ed719SMichael Grzeschik u8 learn[DSA_MAX_PORTS]; 956e66f840cSTristram Ha int first, index, cnt; 957e66f840cSTristram Ha struct ksz_port *p; 958e66f840cSTristram Ha 959462d5250SArun Ramadoss if ((uint)port < dev->info->port_cnt) { 960e66f840cSTristram Ha first = port; 961e66f840cSTristram Ha cnt = port + 1; 962e66f840cSTristram Ha } else { 963e66f840cSTristram Ha /* Flush all ports. */ 964e66f840cSTristram Ha first = 0; 965462d5250SArun Ramadoss cnt = dev->info->port_cnt; 966e66f840cSTristram Ha } 967e66f840cSTristram Ha for (index = first; index < cnt; index++) { 968e66f840cSTristram Ha p = &dev->ports[index]; 969e66f840cSTristram Ha if (!p->on) 970e66f840cSTristram Ha continue; 971e66f840cSTristram Ha ksz_pread8(dev, index, P_STP_CTRL, &learn[index]); 972e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 973e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, 974e66f840cSTristram Ha learn[index] | PORT_LEARN_DISABLE); 975e66f840cSTristram Ha } 976e66f840cSTristram Ha ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true); 977e66f840cSTristram Ha for (index = first; index < cnt; index++) { 978e66f840cSTristram Ha p = &dev->ports[index]; 979e66f840cSTristram Ha if (!p->on) 980e66f840cSTristram Ha continue; 981e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 982e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, learn[index]); 983e66f840cSTristram Ha } 984e66f840cSTristram Ha } 985e66f840cSTristram Ha 9864b5baca0SMichael Grzeschik static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag, 98789153ed6SVladimir Oltean struct netlink_ext_ack *extack) 988e66f840cSTristram Ha { 989e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 990e66f840cSTristram Ha 9914b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 9924b20a07eSOleksij Rempel return -ENOTSUPP; 9934b20a07eSOleksij Rempel 99416484413SBen Hutchings /* Discard packets with VID not enabled on the switch */ 995e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag); 996e66f840cSTristram Ha 99716484413SBen Hutchings /* Discard packets with VID not enabled on the ingress port */ 99816484413SBen Hutchings for (port = 0; port < dev->phy_port_cnt; ++port) 99916484413SBen Hutchings ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER, 100016484413SBen Hutchings flag); 100116484413SBen Hutchings 1002e66f840cSTristram Ha return 0; 1003e66f840cSTristram Ha } 1004e66f840cSTristram Ha 1005ef3b02a1SBen Hutchings static void ksz8_port_enable_pvid(struct ksz_device *dev, int port, bool state) 1006ef3b02a1SBen Hutchings { 1007ef3b02a1SBen Hutchings if (ksz_is_ksz88x3(dev)) { 1008ef3b02a1SBen Hutchings ksz_cfg(dev, REG_SW_INSERT_SRC_PVID, 1009ef3b02a1SBen Hutchings 0x03 << (4 - 2 * port), state); 1010ef3b02a1SBen Hutchings } else { 1011ef3b02a1SBen Hutchings ksz_pwrite8(dev, port, REG_PORT_CTRL_12, state ? 0x0f : 0x00); 1012ef3b02a1SBen Hutchings } 1013ef3b02a1SBen Hutchings } 1014ef3b02a1SBen Hutchings 10154b5baca0SMichael Grzeschik static int ksz8_port_vlan_add(struct dsa_switch *ds, int port, 101631046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 101731046a5fSVladimir Oltean struct netlink_ext_ack *extack) 1018e66f840cSTristram Ha { 1019e66f840cSTristram Ha bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 1020e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 10218f4f58f8SBen Hutchings struct ksz_port *p = &dev->ports[port]; 1022b7a9e0daSVladimir Oltean u16 data, new_pvid = 0; 1023e66f840cSTristram Ha u8 fid, member, valid; 1024e66f840cSTristram Ha 10254b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 10264b20a07eSOleksij Rempel return -ENOTSUPP; 10274b20a07eSOleksij Rempel 10288f4f58f8SBen Hutchings /* If a VLAN is added with untagged flag different from the 10298f4f58f8SBen Hutchings * port's Remove Tag flag, we need to change the latter. 10308f4f58f8SBen Hutchings * Ignore VID 0, which is always untagged. 10319130c2d3SBen Hutchings * Ignore CPU port, which will always be tagged. 10328f4f58f8SBen Hutchings */ 10339130c2d3SBen Hutchings if (untagged != p->remove_tag && vlan->vid != 0 && 10349130c2d3SBen Hutchings port != dev->cpu_port) { 10358f4f58f8SBen Hutchings unsigned int vid; 10368f4f58f8SBen Hutchings 10378f4f58f8SBen Hutchings /* Reject attempts to add a VLAN that requires the 10388f4f58f8SBen Hutchings * Remove Tag flag to be changed, unless there are no 10398f4f58f8SBen Hutchings * other VLANs currently configured. 10408f4f58f8SBen Hutchings */ 1041462d5250SArun Ramadoss for (vid = 1; vid < dev->info->num_vlans; ++vid) { 10428f4f58f8SBen Hutchings /* Skip the VID we are going to add or reconfigure */ 10438f4f58f8SBen Hutchings if (vid == vlan->vid) 10448f4f58f8SBen Hutchings continue; 10458f4f58f8SBen Hutchings 10468f4f58f8SBen Hutchings ksz8_from_vlan(dev, dev->vlan_cache[vid].table[0], 10478f4f58f8SBen Hutchings &fid, &member, &valid); 10488f4f58f8SBen Hutchings if (valid && (member & BIT(port))) 10498f4f58f8SBen Hutchings return -EINVAL; 10508f4f58f8SBen Hutchings } 10518f4f58f8SBen Hutchings 1052e66f840cSTristram Ha ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged); 10538f4f58f8SBen Hutchings p->remove_tag = untagged; 10548f4f58f8SBen Hutchings } 1055e66f840cSTristram Ha 10564b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 10579f73e112SMichael Grzeschik ksz8_from_vlan(dev, data, &fid, &member, &valid); 1058e66f840cSTristram Ha 1059e66f840cSTristram Ha /* First time to setup the VLAN entry. */ 1060e66f840cSTristram Ha if (!valid) { 1061e66f840cSTristram Ha /* Need to find a way to map VID to FID. */ 1062e66f840cSTristram Ha fid = 1; 1063e66f840cSTristram Ha valid = 1; 1064e66f840cSTristram Ha } 1065e66f840cSTristram Ha member |= BIT(port); 1066e66f840cSTristram Ha 10679f73e112SMichael Grzeschik ksz8_to_vlan(dev, fid, member, valid, &data); 10684b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 1069e66f840cSTristram Ha 1070e66f840cSTristram Ha /* change PVID */ 1071e66f840cSTristram Ha if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 1072b7a9e0daSVladimir Oltean new_pvid = vlan->vid; 1073e66f840cSTristram Ha 1074e66f840cSTristram Ha if (new_pvid) { 1075b7a9e0daSVladimir Oltean u16 vid; 1076b7a9e0daSVladimir Oltean 1077e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid); 1078ef3b02a1SBen Hutchings vid &= ~VLAN_VID_MASK; 1079e66f840cSTristram Ha vid |= new_pvid; 1080e66f840cSTristram Ha ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid); 1081ef3b02a1SBen Hutchings 1082ef3b02a1SBen Hutchings ksz8_port_enable_pvid(dev, port, true); 1083e66f840cSTristram Ha } 10841958d581SVladimir Oltean 10851958d581SVladimir Oltean return 0; 1086e66f840cSTristram Ha } 1087e66f840cSTristram Ha 10884b5baca0SMichael Grzeschik static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, 1089e66f840cSTristram Ha const struct switchdev_obj_port_vlan *vlan) 1090e66f840cSTristram Ha { 1091e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1092ef3b02a1SBen Hutchings u16 data, pvid; 1093e66f840cSTristram Ha u8 fid, member, valid; 1094e66f840cSTristram Ha 10954b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) 10964b20a07eSOleksij Rempel return -ENOTSUPP; 10974b20a07eSOleksij Rempel 1098e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid); 1099e66f840cSTristram Ha pvid = pvid & 0xFFF; 1100e66f840cSTristram Ha 11014b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 11029f73e112SMichael Grzeschik ksz8_from_vlan(dev, data, &fid, &member, &valid); 1103e66f840cSTristram Ha 1104e66f840cSTristram Ha member &= ~BIT(port); 1105e66f840cSTristram Ha 1106e66f840cSTristram Ha /* Invalidate the entry if no more member. */ 1107e66f840cSTristram Ha if (!member) { 1108e66f840cSTristram Ha fid = 0; 1109e66f840cSTristram Ha valid = 0; 1110e66f840cSTristram Ha } 1111e66f840cSTristram Ha 11129f73e112SMichael Grzeschik ksz8_to_vlan(dev, fid, member, valid, &data); 11134b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 1114e66f840cSTristram Ha 1115ef3b02a1SBen Hutchings if (pvid == vlan->vid) 1116ef3b02a1SBen Hutchings ksz8_port_enable_pvid(dev, port, false); 1117e66f840cSTristram Ha 1118e66f840cSTristram Ha return 0; 1119e66f840cSTristram Ha } 1120e66f840cSTristram Ha 11214b5baca0SMichael Grzeschik static int ksz8_port_mirror_add(struct dsa_switch *ds, int port, 1122e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror, 11230148bb50SVladimir Oltean bool ingress, struct netlink_ext_ack *extack) 1124e66f840cSTristram Ha { 1125e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1126e66f840cSTristram Ha 1127e66f840cSTristram Ha if (ingress) { 1128e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true); 1129e66f840cSTristram Ha dev->mirror_rx |= BIT(port); 1130e66f840cSTristram Ha } else { 1131e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true); 1132e66f840cSTristram Ha dev->mirror_tx |= BIT(port); 1133e66f840cSTristram Ha } 1134e66f840cSTristram Ha 1135e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false); 1136e66f840cSTristram Ha 1137e66f840cSTristram Ha /* configure mirror port */ 1138e66f840cSTristram Ha if (dev->mirror_rx || dev->mirror_tx) 1139e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 1140e66f840cSTristram Ha PORT_MIRROR_SNIFFER, true); 1141e66f840cSTristram Ha 1142e66f840cSTristram Ha return 0; 1143e66f840cSTristram Ha } 1144e66f840cSTristram Ha 11454b5baca0SMichael Grzeschik static void ksz8_port_mirror_del(struct dsa_switch *ds, int port, 1146e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror) 1147e66f840cSTristram Ha { 1148e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1149e66f840cSTristram Ha u8 data; 1150e66f840cSTristram Ha 1151e66f840cSTristram Ha if (mirror->ingress) { 1152e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false); 1153e66f840cSTristram Ha dev->mirror_rx &= ~BIT(port); 1154e66f840cSTristram Ha } else { 1155e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false); 1156e66f840cSTristram Ha dev->mirror_tx &= ~BIT(port); 1157e66f840cSTristram Ha } 1158e66f840cSTristram Ha 1159e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 1160e66f840cSTristram Ha 1161e66f840cSTristram Ha if (!dev->mirror_rx && !dev->mirror_tx) 1162e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 1163e66f840cSTristram Ha PORT_MIRROR_SNIFFER, false); 1164e66f840cSTristram Ha } 1165e66f840cSTristram Ha 1166c2ac4d2aSMichael Grzeschik static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port) 1167e66f840cSTristram Ha { 1168e66f840cSTristram Ha struct ksz_port *p = &dev->ports[port]; 1169c2ac4d2aSMichael Grzeschik u8 data8; 1170e66f840cSTristram Ha 1171edecfa98SHelmut Grohne if (!p->interface && dev->compat_interface) { 1172edecfa98SHelmut Grohne dev_warn(dev->dev, 1173edecfa98SHelmut Grohne "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. " 1174edecfa98SHelmut Grohne "Please update your device tree.\n", 1175edecfa98SHelmut Grohne port); 1176edecfa98SHelmut Grohne p->interface = dev->compat_interface; 1177edecfa98SHelmut Grohne } 1178edecfa98SHelmut Grohne 1179e66f840cSTristram Ha /* Configure MII interface for proper network communication. */ 1180e66f840cSTristram Ha ksz_read8(dev, REG_PORT_5_CTRL_6, &data8); 1181e66f840cSTristram Ha data8 &= ~PORT_INTERFACE_TYPE; 1182e66f840cSTristram Ha data8 &= ~PORT_GMII_1GPS_MODE; 1183edecfa98SHelmut Grohne switch (p->interface) { 1184e66f840cSTristram Ha case PHY_INTERFACE_MODE_MII: 1185e66f840cSTristram Ha p->phydev.speed = SPEED_100; 1186e66f840cSTristram Ha break; 1187e66f840cSTristram Ha case PHY_INTERFACE_MODE_RMII: 1188e66f840cSTristram Ha data8 |= PORT_INTERFACE_RMII; 1189e66f840cSTristram Ha p->phydev.speed = SPEED_100; 1190e66f840cSTristram Ha break; 1191e66f840cSTristram Ha case PHY_INTERFACE_MODE_GMII: 1192e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 1193e66f840cSTristram Ha data8 |= PORT_INTERFACE_GMII; 1194e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 1195e66f840cSTristram Ha break; 1196e66f840cSTristram Ha default: 1197e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_IN_ENABLE; 1198e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_OUT_ENABLE; 1199edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 1200edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_RXID) 1201e66f840cSTristram Ha data8 |= PORT_RGMII_ID_IN_ENABLE; 1202edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 1203edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_TXID) 1204e66f840cSTristram Ha data8 |= PORT_RGMII_ID_OUT_ENABLE; 1205e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 1206e66f840cSTristram Ha data8 |= PORT_INTERFACE_RGMII; 1207e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 1208e66f840cSTristram Ha break; 1209e66f840cSTristram Ha } 1210e66f840cSTristram Ha ksz_write8(dev, REG_PORT_5_CTRL_6, data8); 1211e66f840cSTristram Ha p->phydev.duplex = 1; 1212c2ac4d2aSMichael Grzeschik } 1213c2ac4d2aSMichael Grzeschik 1214c2ac4d2aSMichael Grzeschik static void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port) 1215c2ac4d2aSMichael Grzeschik { 1216b3612ccdSOleksij Rempel struct dsa_switch *ds = dev->ds; 12179f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 12189f73e112SMichael Grzeschik const u32 *masks; 1219c2ac4d2aSMichael Grzeschik u8 member; 1220c2ac4d2aSMichael Grzeschik 12219f73e112SMichael Grzeschik masks = ksz8->masks; 12229f73e112SMichael Grzeschik 1223c2ac4d2aSMichael Grzeschik /* enable broadcast storm limit */ 1224c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true); 1225c2ac4d2aSMichael Grzeschik 12264b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) 1227c2ac4d2aSMichael Grzeschik ksz8795_set_prio_queue(dev, port, 4); 1228c2ac4d2aSMichael Grzeschik 1229c2ac4d2aSMichael Grzeschik /* disable DiffServ priority */ 1230c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false); 1231c2ac4d2aSMichael Grzeschik 1232c2ac4d2aSMichael Grzeschik /* replace priority */ 12339f73e112SMichael Grzeschik ksz_port_cfg(dev, port, P_802_1P_CTRL, 12349f73e112SMichael Grzeschik masks[PORT_802_1P_REMAPPING], false); 1235c2ac4d2aSMichael Grzeschik 1236c2ac4d2aSMichael Grzeschik /* enable 802.1p priority */ 1237c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true); 1238c2ac4d2aSMichael Grzeschik 1239c2ac4d2aSMichael Grzeschik if (cpu_port) { 12404b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) 1241c2ac4d2aSMichael Grzeschik ksz8795_cpu_interface_select(dev, port); 1242e66f840cSTristram Ha 1243b3612ccdSOleksij Rempel member = dsa_user_ports(ds); 1244e66f840cSTristram Ha } else { 1245b3612ccdSOleksij Rempel member = BIT(dsa_upstream_port(ds, port)); 1246e66f840cSTristram Ha } 1247b3612ccdSOleksij Rempel 12484b5baca0SMichael Grzeschik ksz8_cfg_port_member(dev, port, member); 1249e66f840cSTristram Ha } 1250e66f840cSTristram Ha 12514b5baca0SMichael Grzeschik static void ksz8_config_cpu_port(struct dsa_switch *ds) 1252e66f840cSTristram Ha { 1253e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 12549f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 12559f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 1256e66f840cSTristram Ha struct ksz_port *p; 12579f73e112SMichael Grzeschik const u32 *masks; 1258e66f840cSTristram Ha u8 remote; 1259e66f840cSTristram Ha int i; 1260e66f840cSTristram Ha 12619f73e112SMichael Grzeschik masks = ksz8->masks; 12629f73e112SMichael Grzeschik 1263e66f840cSTristram Ha /* Switch marks the maximum frame with extra byte as oversize. */ 1264e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true); 12659f73e112SMichael Grzeschik ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true); 1266e66f840cSTristram Ha 1267e66f840cSTristram Ha p = &dev->ports[dev->cpu_port]; 1268e66f840cSTristram Ha p->on = 1; 1269e66f840cSTristram Ha 12704b5baca0SMichael Grzeschik ksz8_port_setup(dev, dev->cpu_port, true); 1271e66f840cSTristram Ha 12724ce2a984SMichael Grzeschik for (i = 0; i < dev->phy_port_cnt; i++) { 1273e66f840cSTristram Ha p = &dev->ports[i]; 1274e66f840cSTristram Ha 12754b5baca0SMichael Grzeschik ksz8_port_stp_state_set(ds, i, BR_STATE_DISABLED); 1276e66f840cSTristram Ha 1277e66f840cSTristram Ha /* Last port may be disabled. */ 12784ce2a984SMichael Grzeschik if (i == dev->phy_port_cnt) 1279e66f840cSTristram Ha break; 1280e66f840cSTristram Ha p->on = 1; 1281e66f840cSTristram Ha p->phy = 1; 1282e66f840cSTristram Ha } 1283e66f840cSTristram Ha for (i = 0; i < dev->phy_port_cnt; i++) { 1284e66f840cSTristram Ha p = &dev->ports[i]; 1285e66f840cSTristram Ha if (!p->on) 1286e66f840cSTristram Ha continue; 12874b20a07eSOleksij Rempel if (!ksz_is_ksz88x3(dev)) { 12889f73e112SMichael Grzeschik ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote); 1289e66f840cSTristram Ha if (remote & PORT_FIBER_MODE) 1290e66f840cSTristram Ha p->fiber = 1; 12914b20a07eSOleksij Rempel } 1292e66f840cSTristram Ha if (p->fiber) 1293e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1294e66f840cSTristram Ha true); 1295e66f840cSTristram Ha else 1296e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1297e66f840cSTristram Ha false); 1298e66f840cSTristram Ha } 1299e66f840cSTristram Ha } 1300e66f840cSTristram Ha 13017b6e6235SOleksij Rempel static int ksz8_handle_global_errata(struct dsa_switch *ds) 13027b6e6235SOleksij Rempel { 13037b6e6235SOleksij Rempel struct ksz_device *dev = ds->priv; 13047b6e6235SOleksij Rempel int ret = 0; 13057b6e6235SOleksij Rempel 13067b6e6235SOleksij Rempel /* KSZ87xx Errata DS80000687C. 13077b6e6235SOleksij Rempel * Module 2: Link drops with some EEE link partners. 13087b6e6235SOleksij Rempel * An issue with the EEE next page exchange between the 13097b6e6235SOleksij Rempel * KSZ879x/KSZ877x/KSZ876x and some EEE link partners may result in 13107b6e6235SOleksij Rempel * the link dropping. 13117b6e6235SOleksij Rempel */ 1312462d5250SArun Ramadoss if (dev->info->ksz87xx_eee_link_erratum) 13137b6e6235SOleksij Rempel ret = ksz8_ind_write8(dev, TABLE_EEE, REG_IND_EEE_GLOB2_HI, 0); 13147b6e6235SOleksij Rempel 13157b6e6235SOleksij Rempel return ret; 13167b6e6235SOleksij Rempel } 13177b6e6235SOleksij Rempel 13184b5baca0SMichael Grzeschik static int ksz8_setup(struct dsa_switch *ds) 1319e66f840cSTristram Ha { 1320e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1321e66f840cSTristram Ha struct alu_struct alu; 1322e66f840cSTristram Ha int i, ret = 0; 1323e66f840cSTristram Ha 1324e66f840cSTristram Ha dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table), 1325462d5250SArun Ramadoss dev->info->num_vlans, GFP_KERNEL); 1326e66f840cSTristram Ha if (!dev->vlan_cache) 1327e66f840cSTristram Ha return -ENOMEM; 1328e66f840cSTristram Ha 13294b5baca0SMichael Grzeschik ret = ksz8_reset_switch(dev); 1330e66f840cSTristram Ha if (ret) { 1331e66f840cSTristram Ha dev_err(ds->dev, "failed to reset switch\n"); 1332e66f840cSTristram Ha return ret; 1333e66f840cSTristram Ha } 1334e66f840cSTristram Ha 1335e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true); 1336e66f840cSTristram Ha 1337e66f840cSTristram Ha /* Enable automatic fast aging when link changed detected. */ 1338e66f840cSTristram Ha ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true); 1339e66f840cSTristram Ha 1340e66f840cSTristram Ha /* Enable aggressive back off algorithm in half duplex mode. */ 1341e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1, 1342e66f840cSTristram Ha SW_AGGR_BACKOFF, SW_AGGR_BACKOFF); 1343e66f840cSTristram Ha 1344e66f840cSTristram Ha /* 1345e66f840cSTristram Ha * Make sure unicast VLAN boundary is set as default and 1346e66f840cSTristram Ha * enable no excessive collision drop. 1347e66f840cSTristram Ha */ 1348e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2, 1349e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP, 1350e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP); 1351e66f840cSTristram Ha 13524b5baca0SMichael Grzeschik ksz8_config_cpu_port(ds); 1353e66f840cSTristram Ha 1354e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true); 1355e66f840cSTristram Ha 1356e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false); 1357e66f840cSTristram Ha 1358e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false); 1359e66f840cSTristram Ha 1360ef3b02a1SBen Hutchings if (!ksz_is_ksz88x3(dev)) 1361ef3b02a1SBen Hutchings ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true); 1362ef3b02a1SBen Hutchings 1363e66f840cSTristram Ha /* set broadcast storm protection 10% rate */ 1364e66f840cSTristram Ha regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL, 1365e66f840cSTristram Ha BROADCAST_STORM_RATE, 1366e66f840cSTristram Ha (BROADCAST_STORM_VALUE * 1367e66f840cSTristram Ha BROADCAST_STORM_PROT_RATE) / 100); 1368e66f840cSTristram Ha 1369462d5250SArun Ramadoss for (i = 0; i < (dev->info->num_vlans / 4); i++) 13704b5baca0SMichael Grzeschik ksz8_r_vlan_entries(dev, i); 1371e66f840cSTristram Ha 1372e66f840cSTristram Ha /* Setup STP address for STP operation. */ 1373e66f840cSTristram Ha memset(&alu, 0, sizeof(alu)); 1374e66f840cSTristram Ha ether_addr_copy(alu.mac, eth_stp_addr); 1375e66f840cSTristram Ha alu.is_static = true; 1376e66f840cSTristram Ha alu.is_override = true; 1377e66f840cSTristram Ha alu.port_forward = dev->host_mask; 1378e66f840cSTristram Ha 13794b5baca0SMichael Grzeschik ksz8_w_sta_mac_table(dev, 0, &alu); 1380e66f840cSTristram Ha 1381e66f840cSTristram Ha ksz_init_mib_timer(dev); 1382e66f840cSTristram Ha 13830ee2af4eSVladimir Oltean ds->configure_vlan_while_not_filtering = false; 13840ee2af4eSVladimir Oltean 13857b6e6235SOleksij Rempel return ksz8_handle_global_errata(ds); 1386e66f840cSTristram Ha } 1387e66f840cSTristram Ha 138882fdbb91SRussell King (Oracle) static void ksz8_get_caps(struct dsa_switch *ds, int port, 138982fdbb91SRussell King (Oracle) struct phylink_config *config) 13902c709e0bSMichael Grzeschik { 13912c709e0bSMichael Grzeschik struct ksz_device *dev = ds->priv; 13922c709e0bSMichael Grzeschik 13932c709e0bSMichael Grzeschik if (port == dev->cpu_port) { 139482fdbb91SRussell King (Oracle) __set_bit(PHY_INTERFACE_MODE_RMII, 139582fdbb91SRussell King (Oracle) config->supported_interfaces); 139682fdbb91SRussell King (Oracle) __set_bit(PHY_INTERFACE_MODE_MII, 139782fdbb91SRussell King (Oracle) config->supported_interfaces); 13982c709e0bSMichael Grzeschik } else { 139982fdbb91SRussell King (Oracle) __set_bit(PHY_INTERFACE_MODE_INTERNAL, 140082fdbb91SRussell King (Oracle) config->supported_interfaces); 14012c709e0bSMichael Grzeschik } 14022c709e0bSMichael Grzeschik 140382fdbb91SRussell King (Oracle) config->mac_capabilities = MAC_10 | MAC_100; 14042c709e0bSMichael Grzeschik 14052c709e0bSMichael Grzeschik /* Silicon Errata Sheet (DS80000830A): 14062c709e0bSMichael Grzeschik * "Port 1 does not respond to received flow control PAUSE frames" 14072c709e0bSMichael Grzeschik * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3 14082c709e0bSMichael Grzeschik * switches. 14092c709e0bSMichael Grzeschik */ 14102c709e0bSMichael Grzeschik if (!ksz_is_ksz88x3(dev) || port) 141182fdbb91SRussell King (Oracle) config->mac_capabilities |= MAC_SYM_PAUSE; 14122c709e0bSMichael Grzeschik 14132c709e0bSMichael Grzeschik /* Asym pause is not supported on KSZ8863 and KSZ8873 */ 14142c709e0bSMichael Grzeschik if (!ksz_is_ksz88x3(dev)) 141582fdbb91SRussell King (Oracle) config->mac_capabilities |= MAC_ASYM_PAUSE; 14162c709e0bSMichael Grzeschik } 14172c709e0bSMichael Grzeschik 14184b5baca0SMichael Grzeschik static const struct dsa_switch_ops ksz8_switch_ops = { 14194b5baca0SMichael Grzeschik .get_tag_protocol = ksz8_get_tag_protocol, 142049011e0cSOleksij Rempel .get_phy_flags = ksz8_sw_get_phy_flags, 14214b5baca0SMichael Grzeschik .setup = ksz8_setup, 1422e66f840cSTristram Ha .phy_read = ksz_phy_read16, 1423e66f840cSTristram Ha .phy_write = ksz_phy_write16, 142482fdbb91SRussell King (Oracle) .phylink_get_caps = ksz8_get_caps, 1425143a102eSCodrin Ciubotariu .phylink_mac_link_down = ksz_mac_link_down, 1426e66f840cSTristram Ha .port_enable = ksz_enable_port, 14274b5baca0SMichael Grzeschik .get_strings = ksz8_get_strings, 1428e66f840cSTristram Ha .get_ethtool_stats = ksz_get_ethtool_stats, 1429e66f840cSTristram Ha .get_sset_count = ksz_sset_count, 1430e66f840cSTristram Ha .port_bridge_join = ksz_port_bridge_join, 1431e66f840cSTristram Ha .port_bridge_leave = ksz_port_bridge_leave, 14324b5baca0SMichael Grzeschik .port_stp_state_set = ksz8_port_stp_state_set, 1433e66f840cSTristram Ha .port_fast_age = ksz_port_fast_age, 14344b5baca0SMichael Grzeschik .port_vlan_filtering = ksz8_port_vlan_filtering, 14354b5baca0SMichael Grzeschik .port_vlan_add = ksz8_port_vlan_add, 14364b5baca0SMichael Grzeschik .port_vlan_del = ksz8_port_vlan_del, 1437e66f840cSTristram Ha .port_fdb_dump = ksz_port_fdb_dump, 1438e66f840cSTristram Ha .port_mdb_add = ksz_port_mdb_add, 1439e66f840cSTristram Ha .port_mdb_del = ksz_port_mdb_del, 14404b5baca0SMichael Grzeschik .port_mirror_add = ksz8_port_mirror_add, 14414b5baca0SMichael Grzeschik .port_mirror_del = ksz8_port_mirror_del, 1442e66f840cSTristram Ha }; 1443e66f840cSTristram Ha 14444b5baca0SMichael Grzeschik static u32 ksz8_get_port_addr(int port, int offset) 1445e66f840cSTristram Ha { 1446e66f840cSTristram Ha return PORT_CTRL_ADDR(port, offset); 1447e66f840cSTristram Ha } 1448e66f840cSTristram Ha 14494b5baca0SMichael Grzeschik static int ksz8_switch_detect(struct ksz_device *dev) 1450e66f840cSTristram Ha { 1451e66f840cSTristram Ha u8 id1, id2; 1452e66f840cSTristram Ha u16 id16; 1453e66f840cSTristram Ha int ret; 1454e66f840cSTristram Ha 1455e66f840cSTristram Ha /* read chip id */ 1456e66f840cSTristram Ha ret = ksz_read16(dev, REG_CHIP_ID0, &id16); 1457e66f840cSTristram Ha if (ret) 1458e66f840cSTristram Ha return ret; 1459e66f840cSTristram Ha 1460e66f840cSTristram Ha id1 = id16 >> 8; 1461e66f840cSTristram Ha id2 = id16 & SW_CHIP_ID_M; 14624b20a07eSOleksij Rempel 14634b20a07eSOleksij Rempel switch (id1) { 14644b20a07eSOleksij Rempel case KSZ87_FAMILY_ID: 14654b20a07eSOleksij Rempel if ((id2 != CHIP_ID_94 && id2 != CHIP_ID_95)) 1466e66f840cSTristram Ha return -ENODEV; 1467e66f840cSTristram Ha 1468e66f840cSTristram Ha if (id2 == CHIP_ID_95) { 1469e66f840cSTristram Ha u8 val; 1470e66f840cSTristram Ha 1471e66f840cSTristram Ha id2 = 0x95; 14724b20a07eSOleksij Rempel ksz_read8(dev, REG_PORT_STATUS_0, &val); 1473e66f840cSTristram Ha if (val & PORT_FIBER_MODE) 1474e66f840cSTristram Ha id2 = 0x65; 1475e66f840cSTristram Ha } else if (id2 == CHIP_ID_94) { 1476e66f840cSTristram Ha id2 = 0x94; 1477e66f840cSTristram Ha } 14784b20a07eSOleksij Rempel break; 14794b20a07eSOleksij Rempel case KSZ88_FAMILY_ID: 14804b20a07eSOleksij Rempel if (id2 != CHIP_ID_63) 14814b20a07eSOleksij Rempel return -ENODEV; 14824b20a07eSOleksij Rempel break; 14834b20a07eSOleksij Rempel default: 14844b20a07eSOleksij Rempel dev_err(dev->dev, "invalid family id: %d\n", id1); 14854b20a07eSOleksij Rempel return -ENODEV; 14864b20a07eSOleksij Rempel } 1487e66f840cSTristram Ha id16 &= ~0xff; 1488e66f840cSTristram Ha id16 |= id2; 1489e66f840cSTristram Ha dev->chip_id = id16; 1490e66f840cSTristram Ha 1491e66f840cSTristram Ha return 0; 1492e66f840cSTristram Ha } 1493e66f840cSTristram Ha 14944b5baca0SMichael Grzeschik static int ksz8_switch_init(struct ksz_device *dev) 1495e66f840cSTristram Ha { 14969f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 1497e66f840cSTristram Ha int i; 1498e66f840cSTristram Ha 14994b5baca0SMichael Grzeschik dev->ds->ops = &ksz8_switch_ops; 1500e66f840cSTristram Ha 1501462d5250SArun Ramadoss dev->cpu_port = fls(dev->info->cpu_ports) - 1; 1502462d5250SArun Ramadoss dev->host_mask = dev->info->cpu_ports; 1503462d5250SArun Ramadoss dev->phy_port_cnt = dev->info->port_cnt - 1; 1504462d5250SArun Ramadoss dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | dev->info->cpu_ports; 1505e66f840cSTristram Ha 15064b20a07eSOleksij Rempel if (ksz_is_ksz88x3(dev)) { 15074b20a07eSOleksij Rempel ksz8->regs = ksz8863_regs; 15084b20a07eSOleksij Rempel ksz8->masks = ksz8863_masks; 15094b20a07eSOleksij Rempel ksz8->shifts = ksz8863_shifts; 15104b20a07eSOleksij Rempel } else { 15119f73e112SMichael Grzeschik ksz8->regs = ksz8795_regs; 15129f73e112SMichael Grzeschik ksz8->masks = ksz8795_masks; 15139f73e112SMichael Grzeschik ksz8->shifts = ksz8795_shifts; 15144b20a07eSOleksij Rempel } 15159f73e112SMichael Grzeschik 1516c9f4633bSMichael Grzeschik dev->ports = devm_kzalloc(dev->dev, 1517462d5250SArun Ramadoss dev->info->port_cnt * sizeof(struct ksz_port), 1518e66f840cSTristram Ha GFP_KERNEL); 1519e66f840cSTristram Ha if (!dev->ports) 1520e66f840cSTristram Ha return -ENOMEM; 1521462d5250SArun Ramadoss for (i = 0; i < dev->info->port_cnt; i++) { 1522e66f840cSTristram Ha mutex_init(&dev->ports[i].mib.cnt_mutex); 1523e66f840cSTristram Ha dev->ports[i].mib.counters = 1524e66f840cSTristram Ha devm_kzalloc(dev->dev, 1525e66f840cSTristram Ha sizeof(u64) * 1526*a530e6f2SArun Ramadoss (dev->info->mib_cnt + 1), 1527e66f840cSTristram Ha GFP_KERNEL); 1528e66f840cSTristram Ha if (!dev->ports[i].mib.counters) 1529e66f840cSTristram Ha return -ENOMEM; 1530e66f840cSTristram Ha } 1531e66f840cSTristram Ha 1532af199a1aSCodrin Ciubotariu /* set the real number of ports */ 1533462d5250SArun Ramadoss dev->ds->num_ports = dev->info->port_cnt; 1534af199a1aSCodrin Ciubotariu 15359130c2d3SBen Hutchings /* We rely on software untagging on the CPU port, so that we 15369130c2d3SBen Hutchings * can support both tagged and untagged VLANs 15379130c2d3SBen Hutchings */ 15389130c2d3SBen Hutchings dev->ds->untag_bridge_pvid = true; 15399130c2d3SBen Hutchings 154016484413SBen Hutchings /* VLAN filtering is partly controlled by the global VLAN 154116484413SBen Hutchings * Enable flag 154216484413SBen Hutchings */ 154316484413SBen Hutchings dev->ds->vlan_filtering_is_global = true; 154416484413SBen Hutchings 1545e66f840cSTristram Ha return 0; 1546e66f840cSTristram Ha } 1547e66f840cSTristram Ha 15484b5baca0SMichael Grzeschik static void ksz8_switch_exit(struct ksz_device *dev) 1549e66f840cSTristram Ha { 15504b5baca0SMichael Grzeschik ksz8_reset_switch(dev); 1551e66f840cSTristram Ha } 1552e66f840cSTristram Ha 15534b5baca0SMichael Grzeschik static const struct ksz_dev_ops ksz8_dev_ops = { 15544b5baca0SMichael Grzeschik .get_port_addr = ksz8_get_port_addr, 15554b5baca0SMichael Grzeschik .cfg_port_member = ksz8_cfg_port_member, 15564b5baca0SMichael Grzeschik .flush_dyn_mac_table = ksz8_flush_dyn_mac_table, 15574b5baca0SMichael Grzeschik .port_setup = ksz8_port_setup, 15584b5baca0SMichael Grzeschik .r_phy = ksz8_r_phy, 15594b5baca0SMichael Grzeschik .w_phy = ksz8_w_phy, 15604b5baca0SMichael Grzeschik .r_dyn_mac_table = ksz8_r_dyn_mac_table, 15614b5baca0SMichael Grzeschik .r_sta_mac_table = ksz8_r_sta_mac_table, 15624b5baca0SMichael Grzeschik .w_sta_mac_table = ksz8_w_sta_mac_table, 15634b5baca0SMichael Grzeschik .r_mib_cnt = ksz8_r_mib_cnt, 15644b5baca0SMichael Grzeschik .r_mib_pkt = ksz8_r_mib_pkt, 15654b5baca0SMichael Grzeschik .freeze_mib = ksz8_freeze_mib, 15664b5baca0SMichael Grzeschik .port_init_cnt = ksz8_port_init_cnt, 15674b5baca0SMichael Grzeschik .shutdown = ksz8_reset_switch, 15684b5baca0SMichael Grzeschik .detect = ksz8_switch_detect, 15694b5baca0SMichael Grzeschik .init = ksz8_switch_init, 15704b5baca0SMichael Grzeschik .exit = ksz8_switch_exit, 1571e66f840cSTristram Ha }; 1572e66f840cSTristram Ha 15734b5baca0SMichael Grzeschik int ksz8_switch_register(struct ksz_device *dev) 1574e66f840cSTristram Ha { 15754b5baca0SMichael Grzeschik return ksz_switch_register(dev, &ksz8_dev_ops); 1576e66f840cSTristram Ha } 15774b5baca0SMichael Grzeschik EXPORT_SYMBOL(ksz8_switch_register); 1578e66f840cSTristram Ha 1579e66f840cSTristram Ha MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>"); 1580e66f840cSTristram Ha MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver"); 1581e66f840cSTristram Ha MODULE_LICENSE("GPL"); 1582