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 9e66f840cSTristram Ha #include <linux/delay.h> 10e66f840cSTristram Ha #include <linux/export.h> 11e66f840cSTristram Ha #include <linux/gpio.h> 12e66f840cSTristram Ha #include <linux/kernel.h> 13e66f840cSTristram Ha #include <linux/module.h> 14e66f840cSTristram Ha #include <linux/platform_data/microchip-ksz.h> 15e66f840cSTristram Ha #include <linux/phy.h> 16e66f840cSTristram Ha #include <linux/etherdevice.h> 17e66f840cSTristram Ha #include <linux/if_bridge.h> 18e66f840cSTristram Ha #include <net/dsa.h> 19e66f840cSTristram Ha #include <net/switchdev.h> 20e66f840cSTristram Ha 21e66f840cSTristram Ha #include "ksz_common.h" 22e66f840cSTristram Ha #include "ksz8795_reg.h" 23*9f73e112SMichael Grzeschik #include "ksz8.h" 24*9f73e112SMichael Grzeschik 25*9f73e112SMichael Grzeschik static const u8 ksz8795_regs[] = { 26*9f73e112SMichael Grzeschik [REG_IND_CTRL_0] = 0x6E, 27*9f73e112SMichael Grzeschik [REG_IND_DATA_8] = 0x70, 28*9f73e112SMichael Grzeschik [REG_IND_DATA_CHECK] = 0x72, 29*9f73e112SMichael Grzeschik [REG_IND_DATA_HI] = 0x71, 30*9f73e112SMichael Grzeschik [REG_IND_DATA_LO] = 0x75, 31*9f73e112SMichael Grzeschik [REG_IND_MIB_CHECK] = 0x74, 32*9f73e112SMichael Grzeschik [P_FORCE_CTRL] = 0x0C, 33*9f73e112SMichael Grzeschik [P_LINK_STATUS] = 0x0E, 34*9f73e112SMichael Grzeschik [P_LOCAL_CTRL] = 0x07, 35*9f73e112SMichael Grzeschik [P_NEG_RESTART_CTRL] = 0x0D, 36*9f73e112SMichael Grzeschik [P_REMOTE_STATUS] = 0x08, 37*9f73e112SMichael Grzeschik [P_SPEED_STATUS] = 0x09, 38*9f73e112SMichael Grzeschik [S_TAIL_TAG_CTRL] = 0x0C, 39*9f73e112SMichael Grzeschik }; 40*9f73e112SMichael Grzeschik 41*9f73e112SMichael Grzeschik static const u32 ksz8795_masks[] = { 42*9f73e112SMichael Grzeschik [PORT_802_1P_REMAPPING] = BIT(7), 43*9f73e112SMichael Grzeschik [SW_TAIL_TAG_ENABLE] = BIT(1), 44*9f73e112SMichael Grzeschik [MIB_COUNTER_OVERFLOW] = BIT(6), 45*9f73e112SMichael Grzeschik [MIB_COUNTER_VALID] = BIT(5), 46*9f73e112SMichael Grzeschik [VLAN_TABLE_FID] = GENMASK(6, 0), 47*9f73e112SMichael Grzeschik [VLAN_TABLE_MEMBERSHIP] = GENMASK(11, 7), 48*9f73e112SMichael Grzeschik [VLAN_TABLE_VALID] = BIT(12), 49*9f73e112SMichael Grzeschik [STATIC_MAC_TABLE_VALID] = BIT(21), 50*9f73e112SMichael Grzeschik [STATIC_MAC_TABLE_USE_FID] = BIT(23), 51*9f73e112SMichael Grzeschik [STATIC_MAC_TABLE_FID] = GENMASK(30, 24), 52*9f73e112SMichael Grzeschik [STATIC_MAC_TABLE_OVERRIDE] = BIT(26), 53*9f73e112SMichael Grzeschik [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(24, 20), 54*9f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(6, 0), 55*9f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(8), 56*9f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7), 57*9f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 29), 58*9f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_FID] = GENMASK(26, 20), 59*9f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(26, 24), 60*9f73e112SMichael Grzeschik [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(28, 27), 61*9f73e112SMichael Grzeschik }; 62*9f73e112SMichael Grzeschik 63*9f73e112SMichael Grzeschik static const u8 ksz8795_shifts[] = { 64*9f73e112SMichael Grzeschik [VLAN_TABLE_MEMBERSHIP_S] = 7, 65*9f73e112SMichael Grzeschik [VLAN_TABLE] = 16, 66*9f73e112SMichael Grzeschik [STATIC_MAC_FWD_PORTS] = 16, 67*9f73e112SMichael Grzeschik [STATIC_MAC_FID] = 24, 68*9f73e112SMichael Grzeschik [DYNAMIC_MAC_ENTRIES_H] = 3, 69*9f73e112SMichael Grzeschik [DYNAMIC_MAC_ENTRIES] = 29, 70*9f73e112SMichael Grzeschik [DYNAMIC_MAC_FID] = 16, 71*9f73e112SMichael Grzeschik [DYNAMIC_MAC_TIMESTAMP] = 27, 72*9f73e112SMichael Grzeschik [DYNAMIC_MAC_SRC_PORT] = 24, 73*9f73e112SMichael Grzeschik }; 74e66f840cSTristram Ha 75e66f840cSTristram Ha static const struct { 76e66f840cSTristram Ha char string[ETH_GSTRING_LEN]; 7765fe1acfSMichael Grzeschik } mib_names[] = { 78e66f840cSTristram Ha { "rx_hi" }, 79e66f840cSTristram Ha { "rx_undersize" }, 80e66f840cSTristram Ha { "rx_fragments" }, 81e66f840cSTristram Ha { "rx_oversize" }, 82e66f840cSTristram Ha { "rx_jabbers" }, 83e66f840cSTristram Ha { "rx_symbol_err" }, 84e66f840cSTristram Ha { "rx_crc_err" }, 85e66f840cSTristram Ha { "rx_align_err" }, 86e66f840cSTristram Ha { "rx_mac_ctrl" }, 87e66f840cSTristram Ha { "rx_pause" }, 88e66f840cSTristram Ha { "rx_bcast" }, 89e66f840cSTristram Ha { "rx_mcast" }, 90e66f840cSTristram Ha { "rx_ucast" }, 91e66f840cSTristram Ha { "rx_64_or_less" }, 92e66f840cSTristram Ha { "rx_65_127" }, 93e66f840cSTristram Ha { "rx_128_255" }, 94e66f840cSTristram Ha { "rx_256_511" }, 95e66f840cSTristram Ha { "rx_512_1023" }, 96e66f840cSTristram Ha { "rx_1024_1522" }, 97e66f840cSTristram Ha { "rx_1523_2000" }, 98e66f840cSTristram Ha { "rx_2001" }, 99e66f840cSTristram Ha { "tx_hi" }, 100e66f840cSTristram Ha { "tx_late_col" }, 101e66f840cSTristram Ha { "tx_pause" }, 102e66f840cSTristram Ha { "tx_bcast" }, 103e66f840cSTristram Ha { "tx_mcast" }, 104e66f840cSTristram Ha { "tx_ucast" }, 105e66f840cSTristram Ha { "tx_deferred" }, 106e66f840cSTristram Ha { "tx_total_col" }, 107e66f840cSTristram Ha { "tx_exc_col" }, 108e66f840cSTristram Ha { "tx_single_col" }, 109e66f840cSTristram Ha { "tx_mult_col" }, 110e66f840cSTristram Ha { "rx_total" }, 111e66f840cSTristram Ha { "tx_total" }, 112e66f840cSTristram Ha { "rx_discards" }, 113e66f840cSTristram Ha { "tx_discards" }, 114e66f840cSTristram Ha }; 115e66f840cSTristram Ha 116e66f840cSTristram Ha static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set) 117e66f840cSTristram Ha { 118e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0); 119e66f840cSTristram Ha } 120e66f840cSTristram Ha 121e66f840cSTristram Ha static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits, 122e66f840cSTristram Ha bool set) 123e66f840cSTristram Ha { 124e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset), 125e66f840cSTristram Ha bits, set ? bits : 0); 126e66f840cSTristram Ha } 127e66f840cSTristram Ha 1284b5baca0SMichael Grzeschik static int ksz8_reset_switch(struct ksz_device *dev) 129e66f840cSTristram Ha { 130e66f840cSTristram Ha /* reset switch */ 131e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 132e66f840cSTristram Ha SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S); 133e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0); 134e66f840cSTristram Ha 135e66f840cSTristram Ha return 0; 136e66f840cSTristram Ha } 137e66f840cSTristram Ha 138e66f840cSTristram Ha static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue) 139e66f840cSTristram Ha { 140e66f840cSTristram Ha u8 hi, lo; 141e66f840cSTristram Ha 142e66f840cSTristram Ha /* Number of queues can only be 1, 2, or 4. */ 143e66f840cSTristram Ha switch (queue) { 144e66f840cSTristram Ha case 4: 145e66f840cSTristram Ha case 3: 146e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_4; 147e66f840cSTristram Ha break; 148e66f840cSTristram Ha case 2: 149e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_2; 150e66f840cSTristram Ha break; 151e66f840cSTristram Ha default: 152e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_1; 153e66f840cSTristram Ha } 154e66f840cSTristram Ha ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo); 155e66f840cSTristram Ha ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi); 156e66f840cSTristram Ha lo &= ~PORT_QUEUE_SPLIT_L; 157e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_2) 158e66f840cSTristram Ha lo |= PORT_QUEUE_SPLIT_L; 159e66f840cSTristram Ha hi &= ~PORT_QUEUE_SPLIT_H; 160e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_4) 161e66f840cSTristram Ha hi |= PORT_QUEUE_SPLIT_H; 162e66f840cSTristram Ha ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo); 163e66f840cSTristram Ha ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi); 164e66f840cSTristram Ha 165e66f840cSTristram Ha /* Default is port based for egress rate limit. */ 166e66f840cSTristram Ha if (queue != PORT_QUEUE_SPLIT_1) 167e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED, 168e66f840cSTristram Ha true); 169e66f840cSTristram Ha } 170e66f840cSTristram Ha 1714b5baca0SMichael Grzeschik static void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt) 172e66f840cSTristram Ha { 173*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 174*9f73e112SMichael Grzeschik const u32 *masks; 175*9f73e112SMichael Grzeschik const u8 *regs; 176e66f840cSTristram Ha u16 ctrl_addr; 177e66f840cSTristram Ha u32 data; 178e66f840cSTristram Ha u8 check; 179e66f840cSTristram Ha int loop; 180e66f840cSTristram Ha 181*9f73e112SMichael Grzeschik masks = ksz8->masks; 182*9f73e112SMichael Grzeschik regs = ksz8->regs; 183*9f73e112SMichael Grzeschik 18431b62c78SMichael Grzeschik ctrl_addr = addr + dev->reg_mib_cnt * port; 185e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 186e66f840cSTristram Ha 187e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 188*9f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 189e66f840cSTristram Ha 190e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 191e66f840cSTristram Ha * slow SPI speed. 192e66f840cSTristram Ha */ 193e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 194*9f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check); 195e66f840cSTristram Ha 196*9f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_VALID]) { 197*9f73e112SMichael Grzeschik ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 198*9f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) 199e66f840cSTristram Ha *cnt += MIB_COUNTER_VALUE + 1; 200e66f840cSTristram Ha *cnt += data & MIB_COUNTER_VALUE; 201e66f840cSTristram Ha break; 202e66f840cSTristram Ha } 203e66f840cSTristram Ha } 204e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 205e66f840cSTristram Ha } 206e66f840cSTristram Ha 2074b5baca0SMichael Grzeschik static void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 208e66f840cSTristram Ha u64 *dropped, u64 *cnt) 209e66f840cSTristram Ha { 210*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 211*9f73e112SMichael Grzeschik const u32 *masks; 212*9f73e112SMichael Grzeschik const u8 *regs; 213e66f840cSTristram Ha u16 ctrl_addr; 214e66f840cSTristram Ha u32 data; 215e66f840cSTristram Ha u8 check; 216e66f840cSTristram Ha int loop; 217e66f840cSTristram Ha 218*9f73e112SMichael Grzeschik masks = ksz8->masks; 219*9f73e112SMichael Grzeschik regs = ksz8->regs; 220*9f73e112SMichael Grzeschik 22131b62c78SMichael Grzeschik addr -= dev->reg_mib_cnt; 222e66f840cSTristram Ha ctrl_addr = (KS_MIB_TOTAL_RX_1 - KS_MIB_TOTAL_RX_0) * port; 223e66f840cSTristram Ha ctrl_addr += addr + KS_MIB_TOTAL_RX_0; 224e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 225e66f840cSTristram Ha 226e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 227*9f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 228e66f840cSTristram Ha 229e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 230e66f840cSTristram Ha * slow SPI speed. 231e66f840cSTristram Ha */ 232e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 233*9f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check); 234e66f840cSTristram Ha 235*9f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_VALID]) { 236*9f73e112SMichael Grzeschik ksz_read32(dev, regs[REG_IND_DATA_LO], &data); 237e66f840cSTristram Ha if (addr < 2) { 238e66f840cSTristram Ha u64 total; 239e66f840cSTristram Ha 240e66f840cSTristram Ha total = check & MIB_TOTAL_BYTES_H; 241e66f840cSTristram Ha total <<= 32; 242e66f840cSTristram Ha *cnt += total; 243e66f840cSTristram Ha *cnt += data; 244*9f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) { 245e66f840cSTristram Ha total = MIB_TOTAL_BYTES_H + 1; 246e66f840cSTristram Ha total <<= 32; 247e66f840cSTristram Ha *cnt += total; 248e66f840cSTristram Ha } 249e66f840cSTristram Ha } else { 250*9f73e112SMichael Grzeschik if (check & masks[MIB_COUNTER_OVERFLOW]) 251e66f840cSTristram Ha *cnt += MIB_PACKET_DROPPED + 1; 252e66f840cSTristram Ha *cnt += data & MIB_PACKET_DROPPED; 253e66f840cSTristram Ha } 254e66f840cSTristram Ha break; 255e66f840cSTristram Ha } 256e66f840cSTristram Ha } 257e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 258e66f840cSTristram Ha } 259e66f840cSTristram Ha 2604b5baca0SMichael Grzeschik static void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze) 261e66f840cSTristram Ha { 262e66f840cSTristram Ha /* enable the port for flush/freeze function */ 263e66f840cSTristram Ha if (freeze) 264e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 265e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze); 266e66f840cSTristram Ha 267e66f840cSTristram Ha /* disable the port after freeze is done */ 268e66f840cSTristram Ha if (!freeze) 269e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 270e66f840cSTristram Ha } 271e66f840cSTristram Ha 2724b5baca0SMichael Grzeschik static void ksz8_port_init_cnt(struct ksz_device *dev, int port) 273e66f840cSTristram Ha { 274e66f840cSTristram Ha struct ksz_port_mib *mib = &dev->ports[port].mib; 275e66f840cSTristram Ha 276e66f840cSTristram Ha /* flush all enabled port MIB counters */ 277e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 278e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true); 279e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 280e66f840cSTristram Ha 281e66f840cSTristram Ha mib->cnt_ptr = 0; 282e66f840cSTristram Ha 283e66f840cSTristram Ha /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */ 284e66f840cSTristram Ha while (mib->cnt_ptr < dev->reg_mib_cnt) { 285e66f840cSTristram Ha dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr, 286e66f840cSTristram Ha &mib->counters[mib->cnt_ptr]); 287e66f840cSTristram Ha ++mib->cnt_ptr; 288e66f840cSTristram Ha } 289e66f840cSTristram Ha 290e66f840cSTristram Ha /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */ 291e66f840cSTristram Ha while (mib->cnt_ptr < dev->mib_cnt) { 292e66f840cSTristram Ha dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr, 293e66f840cSTristram Ha NULL, &mib->counters[mib->cnt_ptr]); 294e66f840cSTristram Ha ++mib->cnt_ptr; 295e66f840cSTristram Ha } 296e66f840cSTristram Ha mib->cnt_ptr = 0; 297e66f840cSTristram Ha memset(mib->counters, 0, dev->mib_cnt * sizeof(u64)); 298e66f840cSTristram Ha } 299e66f840cSTristram Ha 3004b5baca0SMichael Grzeschik static void ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data) 301e66f840cSTristram Ha { 302*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 303*9f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 304e66f840cSTristram Ha u16 ctrl_addr; 305e66f840cSTristram Ha 306e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr; 307e66f840cSTristram Ha 308e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 309*9f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 310*9f73e112SMichael Grzeschik ksz_read64(dev, regs[REG_IND_DATA_HI], data); 311e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 312e66f840cSTristram Ha } 313e66f840cSTristram Ha 3144b5baca0SMichael Grzeschik static void ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data) 315e66f840cSTristram Ha { 316*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 317*9f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 318e66f840cSTristram Ha u16 ctrl_addr; 319e66f840cSTristram Ha 320e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table) | addr; 321e66f840cSTristram Ha 322e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 323*9f73e112SMichael Grzeschik ksz_write64(dev, regs[REG_IND_DATA_HI], data); 324*9f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 325e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 326e66f840cSTristram Ha } 327e66f840cSTristram Ha 3284b5baca0SMichael Grzeschik static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data) 329e66f840cSTristram Ha { 330*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 331e66f840cSTristram Ha int timeout = 100; 332*9f73e112SMichael Grzeschik const u32 *masks; 333*9f73e112SMichael Grzeschik const u8 *regs; 334*9f73e112SMichael Grzeschik 335*9f73e112SMichael Grzeschik masks = ksz8->masks; 336*9f73e112SMichael Grzeschik regs = ksz8->regs; 337e66f840cSTristram Ha 338e66f840cSTristram Ha do { 339*9f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_DATA_CHECK], data); 340e66f840cSTristram Ha timeout--; 341*9f73e112SMichael Grzeschik } while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout); 342e66f840cSTristram Ha 343e66f840cSTristram Ha /* Entry is not ready for accessing. */ 344*9f73e112SMichael Grzeschik if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) { 345e66f840cSTristram Ha return -EAGAIN; 346e66f840cSTristram Ha /* Entry is ready for accessing. */ 347e66f840cSTristram Ha } else { 348*9f73e112SMichael Grzeschik ksz_read8(dev, regs[REG_IND_DATA_8], data); 349e66f840cSTristram Ha 350e66f840cSTristram Ha /* There is no valid entry in the table. */ 351*9f73e112SMichael Grzeschik if (*data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY]) 352e66f840cSTristram Ha return -ENXIO; 353e66f840cSTristram Ha } 354e66f840cSTristram Ha return 0; 355e66f840cSTristram Ha } 356e66f840cSTristram Ha 3574b5baca0SMichael Grzeschik static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, 358e66f840cSTristram Ha u8 *mac_addr, u8 *fid, u8 *src_port, 359e66f840cSTristram Ha u8 *timestamp, u16 *entries) 360e66f840cSTristram Ha { 361*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 362e66f840cSTristram Ha u32 data_hi, data_lo; 363*9f73e112SMichael Grzeschik const u8 *shifts; 364*9f73e112SMichael Grzeschik const u32 *masks; 365*9f73e112SMichael Grzeschik const u8 *regs; 366e66f840cSTristram Ha u16 ctrl_addr; 367e66f840cSTristram Ha u8 data; 368e66f840cSTristram Ha int rc; 369e66f840cSTristram Ha 370*9f73e112SMichael Grzeschik shifts = ksz8->shifts; 371*9f73e112SMichael Grzeschik masks = ksz8->masks; 372*9f73e112SMichael Grzeschik regs = ksz8->regs; 373*9f73e112SMichael Grzeschik 374e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr; 375e66f840cSTristram Ha 376e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 377*9f73e112SMichael Grzeschik ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr); 378e66f840cSTristram Ha 3794b5baca0SMichael Grzeschik rc = ksz8_valid_dyn_entry(dev, &data); 380e66f840cSTristram Ha if (rc == -EAGAIN) { 381e66f840cSTristram Ha if (addr == 0) 382e66f840cSTristram Ha *entries = 0; 383e66f840cSTristram Ha } else if (rc == -ENXIO) { 384e66f840cSTristram Ha *entries = 0; 385e66f840cSTristram Ha /* At least one valid entry in the table. */ 386e66f840cSTristram Ha } else { 387e66f840cSTristram Ha u64 buf = 0; 388e66f840cSTristram Ha int cnt; 389e66f840cSTristram Ha 390*9f73e112SMichael Grzeschik ksz_read64(dev, regs[REG_IND_DATA_HI], &buf); 391e66f840cSTristram Ha data_hi = (u32)(buf >> 32); 392e66f840cSTristram Ha data_lo = (u32)buf; 393e66f840cSTristram Ha 394e66f840cSTristram Ha /* Check out how many valid entry in the table. */ 395*9f73e112SMichael Grzeschik cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H]; 396*9f73e112SMichael Grzeschik cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H]; 397*9f73e112SMichael Grzeschik cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >> 398*9f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_ENTRIES]; 399e66f840cSTristram Ha *entries = cnt + 1; 400e66f840cSTristram Ha 401*9f73e112SMichael Grzeschik *fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >> 402*9f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_FID]; 403*9f73e112SMichael Grzeschik *src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >> 404*9f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_SRC_PORT]; 405*9f73e112SMichael Grzeschik *timestamp = (data_hi & masks[DYNAMIC_MAC_TABLE_TIMESTAMP]) >> 406*9f73e112SMichael Grzeschik shifts[DYNAMIC_MAC_TIMESTAMP]; 407e66f840cSTristram Ha 408e66f840cSTristram Ha mac_addr[5] = (u8)data_lo; 409e66f840cSTristram Ha mac_addr[4] = (u8)(data_lo >> 8); 410e66f840cSTristram Ha mac_addr[3] = (u8)(data_lo >> 16); 411e66f840cSTristram Ha mac_addr[2] = (u8)(data_lo >> 24); 412e66f840cSTristram Ha 413e66f840cSTristram Ha mac_addr[1] = (u8)data_hi; 414e66f840cSTristram Ha mac_addr[0] = (u8)(data_hi >> 8); 415e66f840cSTristram Ha rc = 0; 416e66f840cSTristram Ha } 417e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 418e66f840cSTristram Ha 419e66f840cSTristram Ha return rc; 420e66f840cSTristram Ha } 421e66f840cSTristram Ha 4224b5baca0SMichael Grzeschik static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr, 423e66f840cSTristram Ha struct alu_struct *alu) 424e66f840cSTristram Ha { 425*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 426e66f840cSTristram Ha u32 data_hi, data_lo; 427*9f73e112SMichael Grzeschik const u8 *shifts; 428*9f73e112SMichael Grzeschik const u32 *masks; 429e66f840cSTristram Ha u64 data; 430e66f840cSTristram Ha 431*9f73e112SMichael Grzeschik shifts = ksz8->shifts; 432*9f73e112SMichael Grzeschik masks = ksz8->masks; 433*9f73e112SMichael Grzeschik 4344b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data); 435e66f840cSTristram Ha data_hi = data >> 32; 436e66f840cSTristram Ha data_lo = (u32)data; 437*9f73e112SMichael Grzeschik if (data_hi & (masks[STATIC_MAC_TABLE_VALID] | 438*9f73e112SMichael Grzeschik masks[STATIC_MAC_TABLE_OVERRIDE])) { 439e66f840cSTristram Ha alu->mac[5] = (u8)data_lo; 440e66f840cSTristram Ha alu->mac[4] = (u8)(data_lo >> 8); 441e66f840cSTristram Ha alu->mac[3] = (u8)(data_lo >> 16); 442e66f840cSTristram Ha alu->mac[2] = (u8)(data_lo >> 24); 443e66f840cSTristram Ha alu->mac[1] = (u8)data_hi; 444e66f840cSTristram Ha alu->mac[0] = (u8)(data_hi >> 8); 445*9f73e112SMichael Grzeschik alu->port_forward = 446*9f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >> 447*9f73e112SMichael Grzeschik shifts[STATIC_MAC_FWD_PORTS]; 448e66f840cSTristram Ha alu->is_override = 449*9f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0; 450e66f840cSTristram Ha data_hi >>= 1; 451*9f73e112SMichael Grzeschik alu->is_static = true; 452*9f73e112SMichael Grzeschik alu->is_use_fid = 453*9f73e112SMichael Grzeschik (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0; 454*9f73e112SMichael Grzeschik alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >> 455*9f73e112SMichael Grzeschik shifts[STATIC_MAC_FID]; 456e66f840cSTristram Ha return 0; 457e66f840cSTristram Ha } 458e66f840cSTristram Ha return -ENXIO; 459e66f840cSTristram Ha } 460e66f840cSTristram Ha 4614b5baca0SMichael Grzeschik static void ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr, 462e66f840cSTristram Ha struct alu_struct *alu) 463e66f840cSTristram Ha { 464*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 465e66f840cSTristram Ha u32 data_hi, data_lo; 466*9f73e112SMichael Grzeschik const u8 *shifts; 467*9f73e112SMichael Grzeschik const u32 *masks; 468e66f840cSTristram Ha u64 data; 469e66f840cSTristram Ha 470*9f73e112SMichael Grzeschik shifts = ksz8->shifts; 471*9f73e112SMichael Grzeschik masks = ksz8->masks; 472*9f73e112SMichael Grzeschik 473e66f840cSTristram Ha data_lo = ((u32)alu->mac[2] << 24) | 474e66f840cSTristram Ha ((u32)alu->mac[3] << 16) | 475e66f840cSTristram Ha ((u32)alu->mac[4] << 8) | alu->mac[5]; 476e66f840cSTristram Ha data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1]; 477*9f73e112SMichael Grzeschik data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS]; 478e66f840cSTristram Ha 479e66f840cSTristram Ha if (alu->is_override) 480*9f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE]; 481e66f840cSTristram Ha if (alu->is_use_fid) { 482*9f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_USE_FID]; 483*9f73e112SMichael Grzeschik data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID]; 484e66f840cSTristram Ha } 485e66f840cSTristram Ha if (alu->is_static) 486*9f73e112SMichael Grzeschik data_hi |= masks[STATIC_MAC_TABLE_VALID]; 487e66f840cSTristram Ha else 488*9f73e112SMichael Grzeschik data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE]; 489e66f840cSTristram Ha 490e66f840cSTristram Ha data = (u64)data_hi << 32 | data_lo; 4914b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data); 492e66f840cSTristram Ha } 493e66f840cSTristram Ha 494*9f73e112SMichael Grzeschik static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid, 495*9f73e112SMichael Grzeschik u8 *member, u8 *valid) 496e66f840cSTristram Ha { 497*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 498*9f73e112SMichael Grzeschik const u8 *shifts; 499*9f73e112SMichael Grzeschik const u32 *masks; 500*9f73e112SMichael Grzeschik 501*9f73e112SMichael Grzeschik shifts = ksz8->shifts; 502*9f73e112SMichael Grzeschik masks = ksz8->masks; 503*9f73e112SMichael Grzeschik 504*9f73e112SMichael Grzeschik *fid = vlan & masks[VLAN_TABLE_FID]; 505*9f73e112SMichael Grzeschik *member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >> 506*9f73e112SMichael Grzeschik shifts[VLAN_TABLE_MEMBERSHIP_S]; 507*9f73e112SMichael Grzeschik *valid = !!(vlan & masks[VLAN_TABLE_VALID]); 508e66f840cSTristram Ha } 509e66f840cSTristram Ha 510*9f73e112SMichael Grzeschik static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid, 511*9f73e112SMichael Grzeschik u16 *vlan) 512e66f840cSTristram Ha { 513*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 514*9f73e112SMichael Grzeschik const u8 *shifts; 515*9f73e112SMichael Grzeschik const u32 *masks; 516*9f73e112SMichael Grzeschik 517*9f73e112SMichael Grzeschik shifts = ksz8->shifts; 518*9f73e112SMichael Grzeschik masks = ksz8->masks; 519*9f73e112SMichael Grzeschik 520e66f840cSTristram Ha *vlan = fid; 521*9f73e112SMichael Grzeschik *vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S]; 522e66f840cSTristram Ha if (valid) 523*9f73e112SMichael Grzeschik *vlan |= masks[VLAN_TABLE_VALID]; 524e66f840cSTristram Ha } 525e66f840cSTristram Ha 5264b5baca0SMichael Grzeschik static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr) 527e66f840cSTristram Ha { 528*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 529*9f73e112SMichael Grzeschik const u8 *shifts; 530e66f840cSTristram Ha u64 data; 531e66f840cSTristram Ha int i; 532e66f840cSTristram Ha 533*9f73e112SMichael Grzeschik shifts = ksz8->shifts; 534*9f73e112SMichael Grzeschik 5354b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &data); 5364ce2a984SMichael Grzeschik addr *= dev->phy_port_cnt; 5374ce2a984SMichael Grzeschik for (i = 0; i < dev->phy_port_cnt; i++) { 538e66f840cSTristram Ha dev->vlan_cache[addr + i].table[0] = (u16)data; 539*9f73e112SMichael Grzeschik data >>= shifts[VLAN_TABLE]; 540e66f840cSTristram Ha } 541e66f840cSTristram Ha } 542e66f840cSTristram Ha 5434b5baca0SMichael Grzeschik static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan) 544e66f840cSTristram Ha { 545e66f840cSTristram Ha int index; 546e66f840cSTristram Ha u16 *data; 547e66f840cSTristram Ha u16 addr; 548e66f840cSTristram Ha u64 buf; 549e66f840cSTristram Ha 550e66f840cSTristram Ha data = (u16 *)&buf; 5514ce2a984SMichael Grzeschik addr = vid / dev->phy_port_cnt; 552e66f840cSTristram Ha index = vid & 3; 5534b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 554e66f840cSTristram Ha *vlan = data[index]; 555e66f840cSTristram Ha } 556e66f840cSTristram Ha 5574b5baca0SMichael Grzeschik static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan) 558e66f840cSTristram Ha { 559e66f840cSTristram Ha int index; 560e66f840cSTristram Ha u16 *data; 561e66f840cSTristram Ha u16 addr; 562e66f840cSTristram Ha u64 buf; 563e66f840cSTristram Ha 564e66f840cSTristram Ha data = (u16 *)&buf; 5654ce2a984SMichael Grzeschik addr = vid / dev->phy_port_cnt; 566e66f840cSTristram Ha index = vid & 3; 5674b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 568e66f840cSTristram Ha data[index] = vlan; 569e66f840cSTristram Ha dev->vlan_cache[vid].table[0] = vlan; 5704b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_VLAN, addr, buf); 571e66f840cSTristram Ha } 572e66f840cSTristram Ha 5734b5baca0SMichael Grzeschik static void ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val) 574e66f840cSTristram Ha { 575*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 576e66f840cSTristram Ha u8 restart, speed, ctrl, link; 577*9f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 578e66f840cSTristram Ha int processed = true; 579e66f840cSTristram Ha u16 data = 0; 580e66f840cSTristram Ha u8 p = phy; 581e66f840cSTristram Ha 582e66f840cSTristram Ha switch (reg) { 583e66f840cSTristram Ha case PHY_REG_CTRL: 584*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart); 585*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed); 586*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl); 587e66f840cSTristram Ha if (restart & PORT_PHY_LOOPBACK) 588e66f840cSTristram Ha data |= PHY_LOOPBACK; 589e66f840cSTristram Ha if (ctrl & PORT_FORCE_100_MBIT) 590e66f840cSTristram Ha data |= PHY_SPEED_100MBIT; 591e66f840cSTristram Ha if (!(ctrl & PORT_AUTO_NEG_DISABLE)) 592e66f840cSTristram Ha data |= PHY_AUTO_NEG_ENABLE; 593e66f840cSTristram Ha if (restart & PORT_POWER_DOWN) 594e66f840cSTristram Ha data |= PHY_POWER_DOWN; 595e66f840cSTristram Ha if (restart & PORT_AUTO_NEG_RESTART) 596e66f840cSTristram Ha data |= PHY_AUTO_NEG_RESTART; 597e66f840cSTristram Ha if (ctrl & PORT_FORCE_FULL_DUPLEX) 598e66f840cSTristram Ha data |= PHY_FULL_DUPLEX; 599e66f840cSTristram Ha if (speed & PORT_HP_MDIX) 600e66f840cSTristram Ha data |= PHY_HP_MDIX; 601e66f840cSTristram Ha if (restart & PORT_FORCE_MDIX) 602e66f840cSTristram Ha data |= PHY_FORCE_MDIX; 603e66f840cSTristram Ha if (restart & PORT_AUTO_MDIX_DISABLE) 604e66f840cSTristram Ha data |= PHY_AUTO_MDIX_DISABLE; 605e66f840cSTristram Ha if (restart & PORT_TX_DISABLE) 606e66f840cSTristram Ha data |= PHY_TRANSMIT_DISABLE; 607e66f840cSTristram Ha if (restart & PORT_LED_OFF) 608e66f840cSTristram Ha data |= PHY_LED_DISABLE; 609e66f840cSTristram Ha break; 610e66f840cSTristram Ha case PHY_REG_STATUS: 611*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LINK_STATUS], &link); 612e66f840cSTristram Ha data = PHY_100BTX_FD_CAPABLE | 613e66f840cSTristram Ha PHY_100BTX_CAPABLE | 614e66f840cSTristram Ha PHY_10BT_FD_CAPABLE | 615e66f840cSTristram Ha PHY_10BT_CAPABLE | 616e66f840cSTristram Ha PHY_AUTO_NEG_CAPABLE; 617e66f840cSTristram Ha if (link & PORT_AUTO_NEG_COMPLETE) 618e66f840cSTristram Ha data |= PHY_AUTO_NEG_ACKNOWLEDGE; 619e66f840cSTristram Ha if (link & PORT_STAT_LINK_GOOD) 620e66f840cSTristram Ha data |= PHY_LINK_STATUS; 621e66f840cSTristram Ha break; 622e66f840cSTristram Ha case PHY_REG_ID_1: 623e66f840cSTristram Ha data = KSZ8795_ID_HI; 624e66f840cSTristram Ha break; 625e66f840cSTristram Ha case PHY_REG_ID_2: 626e66f840cSTristram Ha data = KSZ8795_ID_LO; 627e66f840cSTristram Ha break; 628e66f840cSTristram Ha case PHY_REG_AUTO_NEGOTIATION: 629*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl); 630e66f840cSTristram Ha data = PHY_AUTO_NEG_802_3; 631e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_SYM_PAUSE) 632e66f840cSTristram Ha data |= PHY_AUTO_NEG_SYM_PAUSE; 633e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX_FD) 634e66f840cSTristram Ha data |= PHY_AUTO_NEG_100BTX_FD; 635e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX) 636e66f840cSTristram Ha data |= PHY_AUTO_NEG_100BTX; 637e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT_FD) 638e66f840cSTristram Ha data |= PHY_AUTO_NEG_10BT_FD; 639e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT) 640e66f840cSTristram Ha data |= PHY_AUTO_NEG_10BT; 641e66f840cSTristram Ha break; 642e66f840cSTristram Ha case PHY_REG_REMOTE_CAPABILITY: 643*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link); 644e66f840cSTristram Ha data = PHY_AUTO_NEG_802_3; 645e66f840cSTristram Ha if (link & PORT_REMOTE_SYM_PAUSE) 646e66f840cSTristram Ha data |= PHY_AUTO_NEG_SYM_PAUSE; 647e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX_FD) 648e66f840cSTristram Ha data |= PHY_AUTO_NEG_100BTX_FD; 649e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX) 650e66f840cSTristram Ha data |= PHY_AUTO_NEG_100BTX; 651e66f840cSTristram Ha if (link & PORT_REMOTE_10BT_FD) 652e66f840cSTristram Ha data |= PHY_AUTO_NEG_10BT_FD; 653e66f840cSTristram Ha if (link & PORT_REMOTE_10BT) 654e66f840cSTristram Ha data |= PHY_AUTO_NEG_10BT; 655e66f840cSTristram Ha if (data & ~PHY_AUTO_NEG_802_3) 656e66f840cSTristram Ha data |= PHY_REMOTE_ACKNOWLEDGE_NOT; 657e66f840cSTristram Ha break; 658e66f840cSTristram Ha default: 659e66f840cSTristram Ha processed = false; 660e66f840cSTristram Ha break; 661e66f840cSTristram Ha } 662e66f840cSTristram Ha if (processed) 663e66f840cSTristram Ha *val = data; 664e66f840cSTristram Ha } 665e66f840cSTristram Ha 6664b5baca0SMichael Grzeschik static void ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val) 667e66f840cSTristram Ha { 668*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 669e66f840cSTristram Ha u8 restart, speed, ctrl, data; 670*9f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 671*9f73e112SMichael Grzeschik u8 p = phy; 672e66f840cSTristram Ha 673e66f840cSTristram Ha switch (reg) { 674e66f840cSTristram Ha case PHY_REG_CTRL: 675e66f840cSTristram Ha 676e66f840cSTristram Ha /* Do not support PHY reset function. */ 677e66f840cSTristram Ha if (val & PHY_RESET) 678e66f840cSTristram Ha break; 679*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed); 680e66f840cSTristram Ha data = speed; 681e66f840cSTristram Ha if (val & PHY_HP_MDIX) 682e66f840cSTristram Ha data |= PORT_HP_MDIX; 683e66f840cSTristram Ha else 684e66f840cSTristram Ha data &= ~PORT_HP_MDIX; 685e66f840cSTristram Ha if (data != speed) 686*9f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_SPEED_STATUS], data); 687*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl); 688e66f840cSTristram Ha data = ctrl; 689e66f840cSTristram Ha if (!(val & PHY_AUTO_NEG_ENABLE)) 690e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 691e66f840cSTristram Ha else 692e66f840cSTristram Ha data &= ~PORT_AUTO_NEG_DISABLE; 693e66f840cSTristram Ha 694e66f840cSTristram Ha /* Fiber port does not support auto-negotiation. */ 695e66f840cSTristram Ha if (dev->ports[p].fiber) 696e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 697e66f840cSTristram Ha if (val & PHY_SPEED_100MBIT) 698e66f840cSTristram Ha data |= PORT_FORCE_100_MBIT; 699e66f840cSTristram Ha else 700e66f840cSTristram Ha data &= ~PORT_FORCE_100_MBIT; 701e66f840cSTristram Ha if (val & PHY_FULL_DUPLEX) 702e66f840cSTristram Ha data |= PORT_FORCE_FULL_DUPLEX; 703e66f840cSTristram Ha else 704e66f840cSTristram Ha data &= ~PORT_FORCE_FULL_DUPLEX; 705e66f840cSTristram Ha if (data != ctrl) 706*9f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_FORCE_CTRL], data); 707*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart); 708e66f840cSTristram Ha data = restart; 709e66f840cSTristram Ha if (val & PHY_LED_DISABLE) 710e66f840cSTristram Ha data |= PORT_LED_OFF; 711e66f840cSTristram Ha else 712e66f840cSTristram Ha data &= ~PORT_LED_OFF; 713e66f840cSTristram Ha if (val & PHY_TRANSMIT_DISABLE) 714e66f840cSTristram Ha data |= PORT_TX_DISABLE; 715e66f840cSTristram Ha else 716e66f840cSTristram Ha data &= ~PORT_TX_DISABLE; 717e66f840cSTristram Ha if (val & PHY_AUTO_NEG_RESTART) 718e66f840cSTristram Ha data |= PORT_AUTO_NEG_RESTART; 719e66f840cSTristram Ha else 720e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_RESTART); 721e66f840cSTristram Ha if (val & PHY_POWER_DOWN) 722e66f840cSTristram Ha data |= PORT_POWER_DOWN; 723e66f840cSTristram Ha else 724e66f840cSTristram Ha data &= ~PORT_POWER_DOWN; 725e66f840cSTristram Ha if (val & PHY_AUTO_MDIX_DISABLE) 726e66f840cSTristram Ha data |= PORT_AUTO_MDIX_DISABLE; 727e66f840cSTristram Ha else 728e66f840cSTristram Ha data &= ~PORT_AUTO_MDIX_DISABLE; 729e66f840cSTristram Ha if (val & PHY_FORCE_MDIX) 730e66f840cSTristram Ha data |= PORT_FORCE_MDIX; 731e66f840cSTristram Ha else 732e66f840cSTristram Ha data &= ~PORT_FORCE_MDIX; 733e66f840cSTristram Ha if (val & PHY_LOOPBACK) 734e66f840cSTristram Ha data |= PORT_PHY_LOOPBACK; 735e66f840cSTristram Ha else 736e66f840cSTristram Ha data &= ~PORT_PHY_LOOPBACK; 737e66f840cSTristram Ha if (data != restart) 738*9f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_NEG_RESTART_CTRL], data); 739e66f840cSTristram Ha break; 740e66f840cSTristram Ha case PHY_REG_AUTO_NEGOTIATION: 741*9f73e112SMichael Grzeschik ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl); 742e66f840cSTristram Ha data = ctrl; 743e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_SYM_PAUSE | 744e66f840cSTristram Ha PORT_AUTO_NEG_100BTX_FD | 745e66f840cSTristram Ha PORT_AUTO_NEG_100BTX | 746e66f840cSTristram Ha PORT_AUTO_NEG_10BT_FD | 747e66f840cSTristram Ha PORT_AUTO_NEG_10BT); 748e66f840cSTristram Ha if (val & PHY_AUTO_NEG_SYM_PAUSE) 749e66f840cSTristram Ha data |= PORT_AUTO_NEG_SYM_PAUSE; 750e66f840cSTristram Ha if (val & PHY_AUTO_NEG_100BTX_FD) 751e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX_FD; 752e66f840cSTristram Ha if (val & PHY_AUTO_NEG_100BTX) 753e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX; 754e66f840cSTristram Ha if (val & PHY_AUTO_NEG_10BT_FD) 755e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT_FD; 756e66f840cSTristram Ha if (val & PHY_AUTO_NEG_10BT) 757e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT; 758e66f840cSTristram Ha if (data != ctrl) 759*9f73e112SMichael Grzeschik ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data); 760e66f840cSTristram Ha break; 761e66f840cSTristram Ha default: 762e66f840cSTristram Ha break; 763e66f840cSTristram Ha } 764e66f840cSTristram Ha } 765e66f840cSTristram Ha 7664b5baca0SMichael Grzeschik static enum dsa_tag_protocol ksz8_get_tag_protocol(struct dsa_switch *ds, 7674d776482SFlorian Fainelli int port, 7684d776482SFlorian Fainelli enum dsa_tag_protocol mp) 769e66f840cSTristram Ha { 770e66f840cSTristram Ha return DSA_TAG_PROTO_KSZ8795; 771e66f840cSTristram Ha } 772e66f840cSTristram Ha 7734b5baca0SMichael Grzeschik static void ksz8_get_strings(struct dsa_switch *ds, int port, 774e66f840cSTristram Ha u32 stringset, uint8_t *buf) 775e66f840cSTristram Ha { 77665fe1acfSMichael Grzeschik struct ksz_device *dev = ds->priv; 777e66f840cSTristram Ha int i; 778e66f840cSTristram Ha 77965fe1acfSMichael Grzeschik for (i = 0; i < dev->mib_cnt; i++) { 780e66f840cSTristram Ha memcpy(buf + i * ETH_GSTRING_LEN, mib_names[i].string, 781e66f840cSTristram Ha ETH_GSTRING_LEN); 782e66f840cSTristram Ha } 783e66f840cSTristram Ha } 784e66f840cSTristram Ha 7854b5baca0SMichael Grzeschik static void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member) 786e66f840cSTristram Ha { 787e66f840cSTristram Ha u8 data; 788e66f840cSTristram Ha 789e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 790e66f840cSTristram Ha data &= ~PORT_VLAN_MEMBERSHIP; 791e66f840cSTristram Ha data |= (member & dev->port_mask); 792e66f840cSTristram Ha ksz_pwrite8(dev, port, P_MIRROR_CTRL, data); 793e66f840cSTristram Ha dev->ports[port].member = member; 794e66f840cSTristram Ha } 795e66f840cSTristram Ha 7964b5baca0SMichael Grzeschik static void ksz8_port_stp_state_set(struct dsa_switch *ds, int port, u8 state) 797e66f840cSTristram Ha { 798e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 799e66f840cSTristram Ha int forward = dev->member; 800e66f840cSTristram Ha struct ksz_port *p; 801e66f840cSTristram Ha int member = -1; 802e66f840cSTristram Ha u8 data; 803e66f840cSTristram Ha 804e66f840cSTristram Ha p = &dev->ports[port]; 805e66f840cSTristram Ha 806e66f840cSTristram Ha ksz_pread8(dev, port, P_STP_CTRL, &data); 807e66f840cSTristram Ha data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE); 808e66f840cSTristram Ha 809e66f840cSTristram Ha switch (state) { 810e66f840cSTristram Ha case BR_STATE_DISABLED: 811e66f840cSTristram Ha data |= PORT_LEARN_DISABLE; 8124ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt) 813e66f840cSTristram Ha member = 0; 814e66f840cSTristram Ha break; 815e66f840cSTristram Ha case BR_STATE_LISTENING: 816e66f840cSTristram Ha data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE); 8174ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt && 818e66f840cSTristram Ha p->stp_state == BR_STATE_DISABLED) 819e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 820e66f840cSTristram Ha break; 821e66f840cSTristram Ha case BR_STATE_LEARNING: 822e66f840cSTristram Ha data |= PORT_RX_ENABLE; 823e66f840cSTristram Ha break; 824e66f840cSTristram Ha case BR_STATE_FORWARDING: 825e66f840cSTristram Ha data |= (PORT_TX_ENABLE | PORT_RX_ENABLE); 826e66f840cSTristram Ha 827e66f840cSTristram Ha /* This function is also used internally. */ 828e66f840cSTristram Ha if (port == dev->cpu_port) 829e66f840cSTristram Ha break; 830e66f840cSTristram Ha 831e66f840cSTristram Ha /* Port is a member of a bridge. */ 832e66f840cSTristram Ha if (dev->br_member & BIT(port)) { 833e66f840cSTristram Ha dev->member |= BIT(port); 834e66f840cSTristram Ha member = dev->member; 835e66f840cSTristram Ha } else { 836e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 837e66f840cSTristram Ha } 838e66f840cSTristram Ha break; 839e66f840cSTristram Ha case BR_STATE_BLOCKING: 840e66f840cSTristram Ha data |= PORT_LEARN_DISABLE; 8414ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt && 842e66f840cSTristram Ha p->stp_state == BR_STATE_DISABLED) 843e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 844e66f840cSTristram Ha break; 845e66f840cSTristram Ha default: 846e66f840cSTristram Ha dev_err(ds->dev, "invalid STP state: %d\n", state); 847e66f840cSTristram Ha return; 848e66f840cSTristram Ha } 849e66f840cSTristram Ha 850e66f840cSTristram Ha ksz_pwrite8(dev, port, P_STP_CTRL, data); 851e66f840cSTristram Ha p->stp_state = state; 852e66f840cSTristram Ha /* Port membership may share register with STP state. */ 853e66f840cSTristram Ha if (member >= 0 && member != p->member) 8544b5baca0SMichael Grzeschik ksz8_cfg_port_member(dev, port, (u8)member); 855e66f840cSTristram Ha 856e66f840cSTristram Ha /* Check if forwarding needs to be updated. */ 857e66f840cSTristram Ha if (state != BR_STATE_FORWARDING) { 858e66f840cSTristram Ha if (dev->br_member & BIT(port)) 859e66f840cSTristram Ha dev->member &= ~BIT(port); 860e66f840cSTristram Ha } 861e66f840cSTristram Ha 862e66f840cSTristram Ha /* When topology has changed the function ksz_update_port_member 863e66f840cSTristram Ha * should be called to modify port forwarding behavior. 864e66f840cSTristram Ha */ 865e66f840cSTristram Ha if (forward != dev->member) 866e66f840cSTristram Ha ksz_update_port_member(dev, port); 867e66f840cSTristram Ha } 868e66f840cSTristram Ha 8694b5baca0SMichael Grzeschik static void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port) 870e66f840cSTristram Ha { 871241ed719SMichael Grzeschik u8 learn[DSA_MAX_PORTS]; 872e66f840cSTristram Ha int first, index, cnt; 873e66f840cSTristram Ha struct ksz_port *p; 874e66f840cSTristram Ha 875241ed719SMichael Grzeschik if ((uint)port < dev->port_cnt) { 876e66f840cSTristram Ha first = port; 877e66f840cSTristram Ha cnt = port + 1; 878e66f840cSTristram Ha } else { 879e66f840cSTristram Ha /* Flush all ports. */ 880e66f840cSTristram Ha first = 0; 881c9f4633bSMichael Grzeschik cnt = dev->port_cnt; 882e66f840cSTristram Ha } 883e66f840cSTristram Ha for (index = first; index < cnt; index++) { 884e66f840cSTristram Ha p = &dev->ports[index]; 885e66f840cSTristram Ha if (!p->on) 886e66f840cSTristram Ha continue; 887e66f840cSTristram Ha ksz_pread8(dev, index, P_STP_CTRL, &learn[index]); 888e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 889e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, 890e66f840cSTristram Ha learn[index] | PORT_LEARN_DISABLE); 891e66f840cSTristram Ha } 892e66f840cSTristram Ha ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true); 893e66f840cSTristram Ha for (index = first; index < cnt; index++) { 894e66f840cSTristram Ha p = &dev->ports[index]; 895e66f840cSTristram Ha if (!p->on) 896e66f840cSTristram Ha continue; 897e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 898e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, learn[index]); 899e66f840cSTristram Ha } 900e66f840cSTristram Ha } 901e66f840cSTristram Ha 9024b5baca0SMichael Grzeschik static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag, 90389153ed6SVladimir Oltean struct netlink_ext_ack *extack) 904e66f840cSTristram Ha { 905e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 906e66f840cSTristram Ha 907e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag); 908e66f840cSTristram Ha 909e66f840cSTristram Ha return 0; 910e66f840cSTristram Ha } 911e66f840cSTristram Ha 9124b5baca0SMichael Grzeschik static int ksz8_port_vlan_add(struct dsa_switch *ds, int port, 91331046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 91431046a5fSVladimir Oltean struct netlink_ext_ack *extack) 915e66f840cSTristram Ha { 916e66f840cSTristram Ha bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 917e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 918b7a9e0daSVladimir Oltean u16 data, new_pvid = 0; 919e66f840cSTristram Ha u8 fid, member, valid; 920e66f840cSTristram Ha 921e66f840cSTristram Ha ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged); 922e66f840cSTristram Ha 9234b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 924*9f73e112SMichael Grzeschik ksz8_from_vlan(dev, data, &fid, &member, &valid); 925e66f840cSTristram Ha 926e66f840cSTristram Ha /* First time to setup the VLAN entry. */ 927e66f840cSTristram Ha if (!valid) { 928e66f840cSTristram Ha /* Need to find a way to map VID to FID. */ 929e66f840cSTristram Ha fid = 1; 930e66f840cSTristram Ha valid = 1; 931e66f840cSTristram Ha } 932e66f840cSTristram Ha member |= BIT(port); 933e66f840cSTristram Ha 934*9f73e112SMichael Grzeschik ksz8_to_vlan(dev, fid, member, valid, &data); 9354b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 936e66f840cSTristram Ha 937e66f840cSTristram Ha /* change PVID */ 938e66f840cSTristram Ha if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 939b7a9e0daSVladimir Oltean new_pvid = vlan->vid; 940e66f840cSTristram Ha 941e66f840cSTristram Ha if (new_pvid) { 942b7a9e0daSVladimir Oltean u16 vid; 943b7a9e0daSVladimir Oltean 944e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid); 945e66f840cSTristram Ha vid &= 0xfff; 946e66f840cSTristram Ha vid |= new_pvid; 947e66f840cSTristram Ha ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid); 948e66f840cSTristram Ha } 9491958d581SVladimir Oltean 9501958d581SVladimir Oltean return 0; 951e66f840cSTristram Ha } 952e66f840cSTristram Ha 9534b5baca0SMichael Grzeschik static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, 954e66f840cSTristram Ha const struct switchdev_obj_port_vlan *vlan) 955e66f840cSTristram Ha { 956e66f840cSTristram Ha bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 957e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 958b7a9e0daSVladimir Oltean u16 data, pvid, new_pvid = 0; 959e66f840cSTristram Ha u8 fid, member, valid; 960e66f840cSTristram Ha 961e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid); 962e66f840cSTristram Ha pvid = pvid & 0xFFF; 963e66f840cSTristram Ha 964e66f840cSTristram Ha ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged); 965e66f840cSTristram Ha 9664b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 967*9f73e112SMichael Grzeschik ksz8_from_vlan(dev, data, &fid, &member, &valid); 968e66f840cSTristram Ha 969e66f840cSTristram Ha member &= ~BIT(port); 970e66f840cSTristram Ha 971e66f840cSTristram Ha /* Invalidate the entry if no more member. */ 972e66f840cSTristram Ha if (!member) { 973e66f840cSTristram Ha fid = 0; 974e66f840cSTristram Ha valid = 0; 975e66f840cSTristram Ha } 976e66f840cSTristram Ha 977b7a9e0daSVladimir Oltean if (pvid == vlan->vid) 978e66f840cSTristram Ha new_pvid = 1; 979e66f840cSTristram Ha 980*9f73e112SMichael Grzeschik ksz8_to_vlan(dev, fid, member, valid, &data); 9814b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 982e66f840cSTristram Ha 983e66f840cSTristram Ha if (new_pvid != pvid) 984e66f840cSTristram Ha ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, pvid); 985e66f840cSTristram Ha 986e66f840cSTristram Ha return 0; 987e66f840cSTristram Ha } 988e66f840cSTristram Ha 9894b5baca0SMichael Grzeschik static int ksz8_port_mirror_add(struct dsa_switch *ds, int port, 990e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror, 991e66f840cSTristram Ha bool ingress) 992e66f840cSTristram Ha { 993e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 994e66f840cSTristram Ha 995e66f840cSTristram Ha if (ingress) { 996e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true); 997e66f840cSTristram Ha dev->mirror_rx |= BIT(port); 998e66f840cSTristram Ha } else { 999e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true); 1000e66f840cSTristram Ha dev->mirror_tx |= BIT(port); 1001e66f840cSTristram Ha } 1002e66f840cSTristram Ha 1003e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false); 1004e66f840cSTristram Ha 1005e66f840cSTristram Ha /* configure mirror port */ 1006e66f840cSTristram Ha if (dev->mirror_rx || dev->mirror_tx) 1007e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 1008e66f840cSTristram Ha PORT_MIRROR_SNIFFER, true); 1009e66f840cSTristram Ha 1010e66f840cSTristram Ha return 0; 1011e66f840cSTristram Ha } 1012e66f840cSTristram Ha 10134b5baca0SMichael Grzeschik static void ksz8_port_mirror_del(struct dsa_switch *ds, int port, 1014e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror) 1015e66f840cSTristram Ha { 1016e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1017e66f840cSTristram Ha u8 data; 1018e66f840cSTristram Ha 1019e66f840cSTristram Ha if (mirror->ingress) { 1020e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false); 1021e66f840cSTristram Ha dev->mirror_rx &= ~BIT(port); 1022e66f840cSTristram Ha } else { 1023e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false); 1024e66f840cSTristram Ha dev->mirror_tx &= ~BIT(port); 1025e66f840cSTristram Ha } 1026e66f840cSTristram Ha 1027e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 1028e66f840cSTristram Ha 1029e66f840cSTristram Ha if (!dev->mirror_rx && !dev->mirror_tx) 1030e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 1031e66f840cSTristram Ha PORT_MIRROR_SNIFFER, false); 1032e66f840cSTristram Ha } 1033e66f840cSTristram Ha 1034c2ac4d2aSMichael Grzeschik static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port) 1035e66f840cSTristram Ha { 1036e66f840cSTristram Ha struct ksz_port *p = &dev->ports[port]; 1037c2ac4d2aSMichael Grzeschik u8 data8; 1038e66f840cSTristram Ha 1039edecfa98SHelmut Grohne if (!p->interface && dev->compat_interface) { 1040edecfa98SHelmut Grohne dev_warn(dev->dev, 1041edecfa98SHelmut Grohne "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. " 1042edecfa98SHelmut Grohne "Please update your device tree.\n", 1043edecfa98SHelmut Grohne port); 1044edecfa98SHelmut Grohne p->interface = dev->compat_interface; 1045edecfa98SHelmut Grohne } 1046edecfa98SHelmut Grohne 1047e66f840cSTristram Ha /* Configure MII interface for proper network communication. */ 1048e66f840cSTristram Ha ksz_read8(dev, REG_PORT_5_CTRL_6, &data8); 1049e66f840cSTristram Ha data8 &= ~PORT_INTERFACE_TYPE; 1050e66f840cSTristram Ha data8 &= ~PORT_GMII_1GPS_MODE; 1051edecfa98SHelmut Grohne switch (p->interface) { 1052e66f840cSTristram Ha case PHY_INTERFACE_MODE_MII: 1053e66f840cSTristram Ha p->phydev.speed = SPEED_100; 1054e66f840cSTristram Ha break; 1055e66f840cSTristram Ha case PHY_INTERFACE_MODE_RMII: 1056e66f840cSTristram Ha data8 |= PORT_INTERFACE_RMII; 1057e66f840cSTristram Ha p->phydev.speed = SPEED_100; 1058e66f840cSTristram Ha break; 1059e66f840cSTristram Ha case PHY_INTERFACE_MODE_GMII: 1060e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 1061e66f840cSTristram Ha data8 |= PORT_INTERFACE_GMII; 1062e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 1063e66f840cSTristram Ha break; 1064e66f840cSTristram Ha default: 1065e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_IN_ENABLE; 1066e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_OUT_ENABLE; 1067edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 1068edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_RXID) 1069e66f840cSTristram Ha data8 |= PORT_RGMII_ID_IN_ENABLE; 1070edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 1071edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_TXID) 1072e66f840cSTristram Ha data8 |= PORT_RGMII_ID_OUT_ENABLE; 1073e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 1074e66f840cSTristram Ha data8 |= PORT_INTERFACE_RGMII; 1075e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 1076e66f840cSTristram Ha break; 1077e66f840cSTristram Ha } 1078e66f840cSTristram Ha ksz_write8(dev, REG_PORT_5_CTRL_6, data8); 1079e66f840cSTristram Ha p->phydev.duplex = 1; 1080c2ac4d2aSMichael Grzeschik } 1081c2ac4d2aSMichael Grzeschik 1082c2ac4d2aSMichael Grzeschik static void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port) 1083c2ac4d2aSMichael Grzeschik { 1084c2ac4d2aSMichael Grzeschik struct ksz_port *p = &dev->ports[port]; 1085*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 1086*9f73e112SMichael Grzeschik const u32 *masks; 1087c2ac4d2aSMichael Grzeschik u8 member; 1088c2ac4d2aSMichael Grzeschik 1089*9f73e112SMichael Grzeschik masks = ksz8->masks; 1090*9f73e112SMichael Grzeschik 1091c2ac4d2aSMichael Grzeschik /* enable broadcast storm limit */ 1092c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true); 1093c2ac4d2aSMichael Grzeschik 1094c2ac4d2aSMichael Grzeschik ksz8795_set_prio_queue(dev, port, 4); 1095c2ac4d2aSMichael Grzeschik 1096c2ac4d2aSMichael Grzeschik /* disable DiffServ priority */ 1097c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false); 1098c2ac4d2aSMichael Grzeschik 1099c2ac4d2aSMichael Grzeschik /* replace priority */ 1100*9f73e112SMichael Grzeschik ksz_port_cfg(dev, port, P_802_1P_CTRL, 1101*9f73e112SMichael Grzeschik masks[PORT_802_1P_REMAPPING], false); 1102c2ac4d2aSMichael Grzeschik 1103c2ac4d2aSMichael Grzeschik /* enable 802.1p priority */ 1104c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true); 1105c2ac4d2aSMichael Grzeschik 1106c2ac4d2aSMichael Grzeschik if (cpu_port) { 1107c2ac4d2aSMichael Grzeschik ksz8795_cpu_interface_select(dev, port); 1108e66f840cSTristram Ha 1109e66f840cSTristram Ha member = dev->port_mask; 1110e66f840cSTristram Ha } else { 1111e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 1112e66f840cSTristram Ha } 11134b5baca0SMichael Grzeschik ksz8_cfg_port_member(dev, port, member); 1114e66f840cSTristram Ha } 1115e66f840cSTristram Ha 11164b5baca0SMichael Grzeschik static void ksz8_config_cpu_port(struct dsa_switch *ds) 1117e66f840cSTristram Ha { 1118e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1119*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 1120*9f73e112SMichael Grzeschik const u8 *regs = ksz8->regs; 1121e66f840cSTristram Ha struct ksz_port *p; 1122*9f73e112SMichael Grzeschik const u32 *masks; 1123e66f840cSTristram Ha u8 remote; 1124e66f840cSTristram Ha int i; 1125e66f840cSTristram Ha 1126*9f73e112SMichael Grzeschik masks = ksz8->masks; 1127*9f73e112SMichael Grzeschik 1128e66f840cSTristram Ha /* Switch marks the maximum frame with extra byte as oversize. */ 1129e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true); 1130*9f73e112SMichael Grzeschik ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true); 1131e66f840cSTristram Ha 1132e66f840cSTristram Ha p = &dev->ports[dev->cpu_port]; 1133e66f840cSTristram Ha p->vid_member = dev->port_mask; 1134e66f840cSTristram Ha p->on = 1; 1135e66f840cSTristram Ha 11364b5baca0SMichael Grzeschik ksz8_port_setup(dev, dev->cpu_port, true); 1137e66f840cSTristram Ha dev->member = dev->host_mask; 1138e66f840cSTristram Ha 11394ce2a984SMichael Grzeschik for (i = 0; i < dev->phy_port_cnt; i++) { 1140e66f840cSTristram Ha p = &dev->ports[i]; 1141e66f840cSTristram Ha 1142e66f840cSTristram Ha /* Initialize to non-zero so that ksz_cfg_port_member() will 1143e66f840cSTristram Ha * be called. 1144e66f840cSTristram Ha */ 1145e66f840cSTristram Ha p->vid_member = BIT(i); 1146e66f840cSTristram Ha p->member = dev->port_mask; 11474b5baca0SMichael Grzeschik ksz8_port_stp_state_set(ds, i, BR_STATE_DISABLED); 1148e66f840cSTristram Ha 1149e66f840cSTristram Ha /* Last port may be disabled. */ 11504ce2a984SMichael Grzeschik if (i == dev->phy_port_cnt) 1151e66f840cSTristram Ha break; 1152e66f840cSTristram Ha p->on = 1; 1153e66f840cSTristram Ha p->phy = 1; 1154e66f840cSTristram Ha } 1155e66f840cSTristram Ha for (i = 0; i < dev->phy_port_cnt; i++) { 1156e66f840cSTristram Ha p = &dev->ports[i]; 1157e66f840cSTristram Ha if (!p->on) 1158e66f840cSTristram Ha continue; 1159*9f73e112SMichael Grzeschik ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote); 1160e66f840cSTristram Ha if (remote & PORT_FIBER_MODE) 1161e66f840cSTristram Ha p->fiber = 1; 1162e66f840cSTristram Ha if (p->fiber) 1163e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1164e66f840cSTristram Ha true); 1165e66f840cSTristram Ha else 1166e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1167e66f840cSTristram Ha false); 1168e66f840cSTristram Ha } 1169e66f840cSTristram Ha } 1170e66f840cSTristram Ha 11714b5baca0SMichael Grzeschik static int ksz8_setup(struct dsa_switch *ds) 1172e66f840cSTristram Ha { 1173e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1174e66f840cSTristram Ha struct alu_struct alu; 1175e66f840cSTristram Ha int i, ret = 0; 1176e66f840cSTristram Ha 1177e66f840cSTristram Ha dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table), 1178e66f840cSTristram Ha dev->num_vlans, GFP_KERNEL); 1179e66f840cSTristram Ha if (!dev->vlan_cache) 1180e66f840cSTristram Ha return -ENOMEM; 1181e66f840cSTristram Ha 11824b5baca0SMichael Grzeschik ret = ksz8_reset_switch(dev); 1183e66f840cSTristram Ha if (ret) { 1184e66f840cSTristram Ha dev_err(ds->dev, "failed to reset switch\n"); 1185e66f840cSTristram Ha return ret; 1186e66f840cSTristram Ha } 1187e66f840cSTristram Ha 1188e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true); 1189e66f840cSTristram Ha 1190e66f840cSTristram Ha /* Enable automatic fast aging when link changed detected. */ 1191e66f840cSTristram Ha ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true); 1192e66f840cSTristram Ha 1193e66f840cSTristram Ha /* Enable aggressive back off algorithm in half duplex mode. */ 1194e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1, 1195e66f840cSTristram Ha SW_AGGR_BACKOFF, SW_AGGR_BACKOFF); 1196e66f840cSTristram Ha 1197e66f840cSTristram Ha /* 1198e66f840cSTristram Ha * Make sure unicast VLAN boundary is set as default and 1199e66f840cSTristram Ha * enable no excessive collision drop. 1200e66f840cSTristram Ha */ 1201e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2, 1202e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP, 1203e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP); 1204e66f840cSTristram Ha 12054b5baca0SMichael Grzeschik ksz8_config_cpu_port(ds); 1206e66f840cSTristram Ha 1207e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true); 1208e66f840cSTristram Ha 1209e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false); 1210e66f840cSTristram Ha 1211e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false); 1212e66f840cSTristram Ha 1213e66f840cSTristram Ha /* set broadcast storm protection 10% rate */ 1214e66f840cSTristram Ha regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL, 1215e66f840cSTristram Ha BROADCAST_STORM_RATE, 1216e66f840cSTristram Ha (BROADCAST_STORM_VALUE * 1217e66f840cSTristram Ha BROADCAST_STORM_PROT_RATE) / 100); 1218e66f840cSTristram Ha 121902ffbb02SMichael Grzeschik for (i = 0; i < (dev->num_vlans / 4); i++) 12204b5baca0SMichael Grzeschik ksz8_r_vlan_entries(dev, i); 1221e66f840cSTristram Ha 1222e66f840cSTristram Ha /* Setup STP address for STP operation. */ 1223e66f840cSTristram Ha memset(&alu, 0, sizeof(alu)); 1224e66f840cSTristram Ha ether_addr_copy(alu.mac, eth_stp_addr); 1225e66f840cSTristram Ha alu.is_static = true; 1226e66f840cSTristram Ha alu.is_override = true; 1227e66f840cSTristram Ha alu.port_forward = dev->host_mask; 1228e66f840cSTristram Ha 12294b5baca0SMichael Grzeschik ksz8_w_sta_mac_table(dev, 0, &alu); 1230e66f840cSTristram Ha 1231e66f840cSTristram Ha ksz_init_mib_timer(dev); 1232e66f840cSTristram Ha 12330ee2af4eSVladimir Oltean ds->configure_vlan_while_not_filtering = false; 12340ee2af4eSVladimir Oltean 1235e66f840cSTristram Ha return 0; 1236e66f840cSTristram Ha } 1237e66f840cSTristram Ha 12384b5baca0SMichael Grzeschik static const struct dsa_switch_ops ksz8_switch_ops = { 12394b5baca0SMichael Grzeschik .get_tag_protocol = ksz8_get_tag_protocol, 12404b5baca0SMichael Grzeschik .setup = ksz8_setup, 1241e66f840cSTristram Ha .phy_read = ksz_phy_read16, 1242e66f840cSTristram Ha .phy_write = ksz_phy_write16, 1243143a102eSCodrin Ciubotariu .phylink_mac_link_down = ksz_mac_link_down, 1244e66f840cSTristram Ha .port_enable = ksz_enable_port, 12454b5baca0SMichael Grzeschik .get_strings = ksz8_get_strings, 1246e66f840cSTristram Ha .get_ethtool_stats = ksz_get_ethtool_stats, 1247e66f840cSTristram Ha .get_sset_count = ksz_sset_count, 1248e66f840cSTristram Ha .port_bridge_join = ksz_port_bridge_join, 1249e66f840cSTristram Ha .port_bridge_leave = ksz_port_bridge_leave, 12504b5baca0SMichael Grzeschik .port_stp_state_set = ksz8_port_stp_state_set, 1251e66f840cSTristram Ha .port_fast_age = ksz_port_fast_age, 12524b5baca0SMichael Grzeschik .port_vlan_filtering = ksz8_port_vlan_filtering, 12534b5baca0SMichael Grzeschik .port_vlan_add = ksz8_port_vlan_add, 12544b5baca0SMichael Grzeschik .port_vlan_del = ksz8_port_vlan_del, 1255e66f840cSTristram Ha .port_fdb_dump = ksz_port_fdb_dump, 1256e66f840cSTristram Ha .port_mdb_add = ksz_port_mdb_add, 1257e66f840cSTristram Ha .port_mdb_del = ksz_port_mdb_del, 12584b5baca0SMichael Grzeschik .port_mirror_add = ksz8_port_mirror_add, 12594b5baca0SMichael Grzeschik .port_mirror_del = ksz8_port_mirror_del, 1260e66f840cSTristram Ha }; 1261e66f840cSTristram Ha 12624b5baca0SMichael Grzeschik static u32 ksz8_get_port_addr(int port, int offset) 1263e66f840cSTristram Ha { 1264e66f840cSTristram Ha return PORT_CTRL_ADDR(port, offset); 1265e66f840cSTristram Ha } 1266e66f840cSTristram Ha 12674b5baca0SMichael Grzeschik static int ksz8_switch_detect(struct ksz_device *dev) 1268e66f840cSTristram Ha { 1269e66f840cSTristram Ha u8 id1, id2; 1270e66f840cSTristram Ha u16 id16; 1271e66f840cSTristram Ha int ret; 1272e66f840cSTristram Ha 1273e66f840cSTristram Ha /* read chip id */ 1274e66f840cSTristram Ha ret = ksz_read16(dev, REG_CHIP_ID0, &id16); 1275e66f840cSTristram Ha if (ret) 1276e66f840cSTristram Ha return ret; 1277e66f840cSTristram Ha 1278e66f840cSTristram Ha id1 = id16 >> 8; 1279e66f840cSTristram Ha id2 = id16 & SW_CHIP_ID_M; 1280e66f840cSTristram Ha if (id1 != FAMILY_ID || 1281e66f840cSTristram Ha (id2 != CHIP_ID_94 && id2 != CHIP_ID_95)) 1282e66f840cSTristram Ha return -ENODEV; 1283e66f840cSTristram Ha 1284e66f840cSTristram Ha if (id2 == CHIP_ID_95) { 1285e66f840cSTristram Ha u8 val; 1286e66f840cSTristram Ha 1287e66f840cSTristram Ha id2 = 0x95; 1288e66f840cSTristram Ha ksz_read8(dev, REG_PORT_1_STATUS_0, &val); 1289e66f840cSTristram Ha if (val & PORT_FIBER_MODE) 1290e66f840cSTristram Ha id2 = 0x65; 1291e66f840cSTristram Ha } else if (id2 == CHIP_ID_94) { 1292e66f840cSTristram Ha id2 = 0x94; 1293e66f840cSTristram Ha } 1294e66f840cSTristram Ha id16 &= ~0xff; 1295e66f840cSTristram Ha id16 |= id2; 1296e66f840cSTristram Ha dev->chip_id = id16; 1297e66f840cSTristram Ha 1298e66f840cSTristram Ha return 0; 1299e66f840cSTristram Ha } 1300e66f840cSTristram Ha 1301e66f840cSTristram Ha struct ksz_chip_data { 1302e66f840cSTristram Ha u16 chip_id; 1303e66f840cSTristram Ha const char *dev_name; 1304e66f840cSTristram Ha int num_vlans; 1305e66f840cSTristram Ha int num_alus; 1306e66f840cSTristram Ha int num_statics; 1307e66f840cSTristram Ha int cpu_ports; 1308e66f840cSTristram Ha int port_cnt; 1309e66f840cSTristram Ha }; 1310e66f840cSTristram Ha 13114b5baca0SMichael Grzeschik static const struct ksz_chip_data ksz8_switch_chips[] = { 1312e66f840cSTristram Ha { 1313e66f840cSTristram Ha .chip_id = 0x8795, 1314e66f840cSTristram Ha .dev_name = "KSZ8795", 1315e66f840cSTristram Ha .num_vlans = 4096, 1316e66f840cSTristram Ha .num_alus = 0, 1317e66f840cSTristram Ha .num_statics = 8, 1318e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 131994374dd1SMichael Grzeschik .port_cnt = 5, /* total cpu and user ports */ 1320e66f840cSTristram Ha }, 1321e66f840cSTristram Ha { 1322c369d7fcSMarek Vasut /* 1323c369d7fcSMarek Vasut * WARNING 1324c369d7fcSMarek Vasut * ======= 1325c369d7fcSMarek Vasut * KSZ8794 is similar to KSZ8795, except the port map 1326c369d7fcSMarek Vasut * contains a gap between external and CPU ports, the 1327c369d7fcSMarek Vasut * port map is NOT continuous. The per-port register 1328c369d7fcSMarek Vasut * map is shifted accordingly too, i.e. registers at 1329c369d7fcSMarek Vasut * offset 0x40 are NOT used on KSZ8794 and they ARE 1330c369d7fcSMarek Vasut * used on KSZ8795 for external port 3. 1331c369d7fcSMarek Vasut * external cpu 1332c369d7fcSMarek Vasut * KSZ8794 0,1,2 4 1333c369d7fcSMarek Vasut * KSZ8795 0,1,2,3 4 1334c369d7fcSMarek Vasut * KSZ8765 0,1,2,3 4 1335c369d7fcSMarek Vasut */ 1336e66f840cSTristram Ha .chip_id = 0x8794, 1337e66f840cSTristram Ha .dev_name = "KSZ8794", 1338e66f840cSTristram Ha .num_vlans = 4096, 1339e66f840cSTristram Ha .num_alus = 0, 1340e66f840cSTristram Ha .num_statics = 8, 1341e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 134294374dd1SMichael Grzeschik .port_cnt = 4, /* total cpu and user ports */ 1343e66f840cSTristram Ha }, 1344e66f840cSTristram Ha { 1345e66f840cSTristram Ha .chip_id = 0x8765, 1346e66f840cSTristram Ha .dev_name = "KSZ8765", 1347e66f840cSTristram Ha .num_vlans = 4096, 1348e66f840cSTristram Ha .num_alus = 0, 1349e66f840cSTristram Ha .num_statics = 8, 1350e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 135194374dd1SMichael Grzeschik .port_cnt = 5, /* total cpu and user ports */ 1352e66f840cSTristram Ha }, 1353e66f840cSTristram Ha }; 1354e66f840cSTristram Ha 13554b5baca0SMichael Grzeschik static int ksz8_switch_init(struct ksz_device *dev) 1356e66f840cSTristram Ha { 1357*9f73e112SMichael Grzeschik struct ksz8 *ksz8 = dev->priv; 1358e66f840cSTristram Ha int i; 1359e66f840cSTristram Ha 13604b5baca0SMichael Grzeschik dev->ds->ops = &ksz8_switch_ops; 1361e66f840cSTristram Ha 13624b5baca0SMichael Grzeschik for (i = 0; i < ARRAY_SIZE(ksz8_switch_chips); i++) { 13634b5baca0SMichael Grzeschik const struct ksz_chip_data *chip = &ksz8_switch_chips[i]; 1364e66f840cSTristram Ha 1365e66f840cSTristram Ha if (dev->chip_id == chip->chip_id) { 1366e66f840cSTristram Ha dev->name = chip->dev_name; 1367e66f840cSTristram Ha dev->num_vlans = chip->num_vlans; 1368e66f840cSTristram Ha dev->num_alus = chip->num_alus; 1369e66f840cSTristram Ha dev->num_statics = chip->num_statics; 1370c369d7fcSMarek Vasut dev->port_cnt = fls(chip->cpu_ports); 1371c369d7fcSMarek Vasut dev->cpu_port = fls(chip->cpu_ports) - 1; 1372c369d7fcSMarek Vasut dev->phy_port_cnt = dev->port_cnt - 1; 1373e66f840cSTristram Ha dev->cpu_ports = chip->cpu_ports; 1374c369d7fcSMarek Vasut dev->host_mask = chip->cpu_ports; 1375c369d7fcSMarek Vasut dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | 1376c369d7fcSMarek Vasut chip->cpu_ports; 1377e66f840cSTristram Ha break; 1378e66f840cSTristram Ha } 1379e66f840cSTristram Ha } 1380e66f840cSTristram Ha 1381e66f840cSTristram Ha /* no switch found */ 1382e66f840cSTristram Ha if (!dev->cpu_ports) 1383e66f840cSTristram Ha return -ENODEV; 1384e66f840cSTristram Ha 1385*9f73e112SMichael Grzeschik ksz8->regs = ksz8795_regs; 1386*9f73e112SMichael Grzeschik ksz8->masks = ksz8795_masks; 1387*9f73e112SMichael Grzeschik ksz8->shifts = ksz8795_shifts; 1388*9f73e112SMichael Grzeschik 138931b62c78SMichael Grzeschik dev->reg_mib_cnt = KSZ8795_COUNTER_NUM; 139065fe1acfSMichael Grzeschik dev->mib_cnt = ARRAY_SIZE(mib_names); 1391e66f840cSTristram Ha 1392c9f4633bSMichael Grzeschik dev->ports = devm_kzalloc(dev->dev, 1393c9f4633bSMichael Grzeschik dev->port_cnt * sizeof(struct ksz_port), 1394e66f840cSTristram Ha GFP_KERNEL); 1395e66f840cSTristram Ha if (!dev->ports) 1396e66f840cSTristram Ha return -ENOMEM; 1397c9f4633bSMichael Grzeschik for (i = 0; i < dev->port_cnt; i++) { 1398e66f840cSTristram Ha mutex_init(&dev->ports[i].mib.cnt_mutex); 1399e66f840cSTristram Ha dev->ports[i].mib.counters = 1400e66f840cSTristram Ha devm_kzalloc(dev->dev, 1401e66f840cSTristram Ha sizeof(u64) * 140265fe1acfSMichael Grzeschik (dev->mib_cnt + 1), 1403e66f840cSTristram Ha GFP_KERNEL); 1404e66f840cSTristram Ha if (!dev->ports[i].mib.counters) 1405e66f840cSTristram Ha return -ENOMEM; 1406e66f840cSTristram Ha } 1407e66f840cSTristram Ha 1408af199a1aSCodrin Ciubotariu /* set the real number of ports */ 140994374dd1SMichael Grzeschik dev->ds->num_ports = dev->port_cnt; 1410af199a1aSCodrin Ciubotariu 1411e66f840cSTristram Ha return 0; 1412e66f840cSTristram Ha } 1413e66f840cSTristram Ha 14144b5baca0SMichael Grzeschik static void ksz8_switch_exit(struct ksz_device *dev) 1415e66f840cSTristram Ha { 14164b5baca0SMichael Grzeschik ksz8_reset_switch(dev); 1417e66f840cSTristram Ha } 1418e66f840cSTristram Ha 14194b5baca0SMichael Grzeschik static const struct ksz_dev_ops ksz8_dev_ops = { 14204b5baca0SMichael Grzeschik .get_port_addr = ksz8_get_port_addr, 14214b5baca0SMichael Grzeschik .cfg_port_member = ksz8_cfg_port_member, 14224b5baca0SMichael Grzeschik .flush_dyn_mac_table = ksz8_flush_dyn_mac_table, 14234b5baca0SMichael Grzeschik .port_setup = ksz8_port_setup, 14244b5baca0SMichael Grzeschik .r_phy = ksz8_r_phy, 14254b5baca0SMichael Grzeschik .w_phy = ksz8_w_phy, 14264b5baca0SMichael Grzeschik .r_dyn_mac_table = ksz8_r_dyn_mac_table, 14274b5baca0SMichael Grzeschik .r_sta_mac_table = ksz8_r_sta_mac_table, 14284b5baca0SMichael Grzeschik .w_sta_mac_table = ksz8_w_sta_mac_table, 14294b5baca0SMichael Grzeschik .r_mib_cnt = ksz8_r_mib_cnt, 14304b5baca0SMichael Grzeschik .r_mib_pkt = ksz8_r_mib_pkt, 14314b5baca0SMichael Grzeschik .freeze_mib = ksz8_freeze_mib, 14324b5baca0SMichael Grzeschik .port_init_cnt = ksz8_port_init_cnt, 14334b5baca0SMichael Grzeschik .shutdown = ksz8_reset_switch, 14344b5baca0SMichael Grzeschik .detect = ksz8_switch_detect, 14354b5baca0SMichael Grzeschik .init = ksz8_switch_init, 14364b5baca0SMichael Grzeschik .exit = ksz8_switch_exit, 1437e66f840cSTristram Ha }; 1438e66f840cSTristram Ha 14394b5baca0SMichael Grzeschik int ksz8_switch_register(struct ksz_device *dev) 1440e66f840cSTristram Ha { 14414b5baca0SMichael Grzeschik return ksz_switch_register(dev, &ksz8_dev_ops); 1442e66f840cSTristram Ha } 14434b5baca0SMichael Grzeschik EXPORT_SYMBOL(ksz8_switch_register); 1444e66f840cSTristram Ha 1445e66f840cSTristram Ha MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>"); 1446e66f840cSTristram Ha MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver"); 1447e66f840cSTristram Ha MODULE_LICENSE("GPL"); 1448