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" 23e66f840cSTristram Ha 24e66f840cSTristram Ha static const struct { 25e66f840cSTristram Ha char string[ETH_GSTRING_LEN]; 2665fe1acfSMichael Grzeschik } mib_names[] = { 27e66f840cSTristram Ha { "rx_hi" }, 28e66f840cSTristram Ha { "rx_undersize" }, 29e66f840cSTristram Ha { "rx_fragments" }, 30e66f840cSTristram Ha { "rx_oversize" }, 31e66f840cSTristram Ha { "rx_jabbers" }, 32e66f840cSTristram Ha { "rx_symbol_err" }, 33e66f840cSTristram Ha { "rx_crc_err" }, 34e66f840cSTristram Ha { "rx_align_err" }, 35e66f840cSTristram Ha { "rx_mac_ctrl" }, 36e66f840cSTristram Ha { "rx_pause" }, 37e66f840cSTristram Ha { "rx_bcast" }, 38e66f840cSTristram Ha { "rx_mcast" }, 39e66f840cSTristram Ha { "rx_ucast" }, 40e66f840cSTristram Ha { "rx_64_or_less" }, 41e66f840cSTristram Ha { "rx_65_127" }, 42e66f840cSTristram Ha { "rx_128_255" }, 43e66f840cSTristram Ha { "rx_256_511" }, 44e66f840cSTristram Ha { "rx_512_1023" }, 45e66f840cSTristram Ha { "rx_1024_1522" }, 46e66f840cSTristram Ha { "rx_1523_2000" }, 47e66f840cSTristram Ha { "rx_2001" }, 48e66f840cSTristram Ha { "tx_hi" }, 49e66f840cSTristram Ha { "tx_late_col" }, 50e66f840cSTristram Ha { "tx_pause" }, 51e66f840cSTristram Ha { "tx_bcast" }, 52e66f840cSTristram Ha { "tx_mcast" }, 53e66f840cSTristram Ha { "tx_ucast" }, 54e66f840cSTristram Ha { "tx_deferred" }, 55e66f840cSTristram Ha { "tx_total_col" }, 56e66f840cSTristram Ha { "tx_exc_col" }, 57e66f840cSTristram Ha { "tx_single_col" }, 58e66f840cSTristram Ha { "tx_mult_col" }, 59e66f840cSTristram Ha { "rx_total" }, 60e66f840cSTristram Ha { "tx_total" }, 61e66f840cSTristram Ha { "rx_discards" }, 62e66f840cSTristram Ha { "tx_discards" }, 63e66f840cSTristram Ha }; 64e66f840cSTristram Ha 65e66f840cSTristram Ha static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set) 66e66f840cSTristram Ha { 67e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0); 68e66f840cSTristram Ha } 69e66f840cSTristram Ha 70e66f840cSTristram Ha static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits, 71e66f840cSTristram Ha bool set) 72e66f840cSTristram Ha { 73e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset), 74e66f840cSTristram Ha bits, set ? bits : 0); 75e66f840cSTristram Ha } 76e66f840cSTristram Ha 774b5baca0SMichael Grzeschik static int ksz8_reset_switch(struct ksz_device *dev) 78e66f840cSTristram Ha { 79e66f840cSTristram Ha /* reset switch */ 80e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 81e66f840cSTristram Ha SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S); 82e66f840cSTristram Ha ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0); 83e66f840cSTristram Ha 84e66f840cSTristram Ha return 0; 85e66f840cSTristram Ha } 86e66f840cSTristram Ha 87e66f840cSTristram Ha static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue) 88e66f840cSTristram Ha { 89e66f840cSTristram Ha u8 hi, lo; 90e66f840cSTristram Ha 91e66f840cSTristram Ha /* Number of queues can only be 1, 2, or 4. */ 92e66f840cSTristram Ha switch (queue) { 93e66f840cSTristram Ha case 4: 94e66f840cSTristram Ha case 3: 95e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_4; 96e66f840cSTristram Ha break; 97e66f840cSTristram Ha case 2: 98e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_2; 99e66f840cSTristram Ha break; 100e66f840cSTristram Ha default: 101e66f840cSTristram Ha queue = PORT_QUEUE_SPLIT_1; 102e66f840cSTristram Ha } 103e66f840cSTristram Ha ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo); 104e66f840cSTristram Ha ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi); 105e66f840cSTristram Ha lo &= ~PORT_QUEUE_SPLIT_L; 106e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_2) 107e66f840cSTristram Ha lo |= PORT_QUEUE_SPLIT_L; 108e66f840cSTristram Ha hi &= ~PORT_QUEUE_SPLIT_H; 109e66f840cSTristram Ha if (queue & PORT_QUEUE_SPLIT_4) 110e66f840cSTristram Ha hi |= PORT_QUEUE_SPLIT_H; 111e66f840cSTristram Ha ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo); 112e66f840cSTristram Ha ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi); 113e66f840cSTristram Ha 114e66f840cSTristram Ha /* Default is port based for egress rate limit. */ 115e66f840cSTristram Ha if (queue != PORT_QUEUE_SPLIT_1) 116e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED, 117e66f840cSTristram Ha true); 118e66f840cSTristram Ha } 119e66f840cSTristram Ha 1204b5baca0SMichael Grzeschik static void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt) 121e66f840cSTristram Ha { 122e66f840cSTristram Ha u16 ctrl_addr; 123e66f840cSTristram Ha u32 data; 124e66f840cSTristram Ha u8 check; 125e66f840cSTristram Ha int loop; 126e66f840cSTristram Ha 12731b62c78SMichael Grzeschik ctrl_addr = addr + dev->reg_mib_cnt * port; 128e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 129e66f840cSTristram Ha 130e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 131e66f840cSTristram Ha ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr); 132e66f840cSTristram Ha 133e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 134e66f840cSTristram Ha * slow SPI speed. 135e66f840cSTristram Ha */ 136e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 137e66f840cSTristram Ha ksz_read8(dev, REG_IND_MIB_CHECK, &check); 138e66f840cSTristram Ha 139e66f840cSTristram Ha if (check & MIB_COUNTER_VALID) { 140e66f840cSTristram Ha ksz_read32(dev, REG_IND_DATA_LO, &data); 141e66f840cSTristram Ha if (check & MIB_COUNTER_OVERFLOW) 142e66f840cSTristram Ha *cnt += MIB_COUNTER_VALUE + 1; 143e66f840cSTristram Ha *cnt += data & MIB_COUNTER_VALUE; 144e66f840cSTristram Ha break; 145e66f840cSTristram Ha } 146e66f840cSTristram Ha } 147e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 148e66f840cSTristram Ha } 149e66f840cSTristram Ha 1504b5baca0SMichael Grzeschik static void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, 151e66f840cSTristram Ha u64 *dropped, u64 *cnt) 152e66f840cSTristram Ha { 153e66f840cSTristram Ha u16 ctrl_addr; 154e66f840cSTristram Ha u32 data; 155e66f840cSTristram Ha u8 check; 156e66f840cSTristram Ha int loop; 157e66f840cSTristram Ha 15831b62c78SMichael Grzeschik addr -= dev->reg_mib_cnt; 159e66f840cSTristram Ha ctrl_addr = (KS_MIB_TOTAL_RX_1 - KS_MIB_TOTAL_RX_0) * port; 160e66f840cSTristram Ha ctrl_addr += addr + KS_MIB_TOTAL_RX_0; 161e66f840cSTristram Ha ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ); 162e66f840cSTristram Ha 163e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 164e66f840cSTristram Ha ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr); 165e66f840cSTristram Ha 166e66f840cSTristram Ha /* It is almost guaranteed to always read the valid bit because of 167e66f840cSTristram Ha * slow SPI speed. 168e66f840cSTristram Ha */ 169e66f840cSTristram Ha for (loop = 2; loop > 0; loop--) { 170e66f840cSTristram Ha ksz_read8(dev, REG_IND_MIB_CHECK, &check); 171e66f840cSTristram Ha 172e66f840cSTristram Ha if (check & MIB_COUNTER_VALID) { 173e66f840cSTristram Ha ksz_read32(dev, REG_IND_DATA_LO, &data); 174e66f840cSTristram Ha if (addr < 2) { 175e66f840cSTristram Ha u64 total; 176e66f840cSTristram Ha 177e66f840cSTristram Ha total = check & MIB_TOTAL_BYTES_H; 178e66f840cSTristram Ha total <<= 32; 179e66f840cSTristram Ha *cnt += total; 180e66f840cSTristram Ha *cnt += data; 181e66f840cSTristram Ha if (check & MIB_COUNTER_OVERFLOW) { 182e66f840cSTristram Ha total = MIB_TOTAL_BYTES_H + 1; 183e66f840cSTristram Ha total <<= 32; 184e66f840cSTristram Ha *cnt += total; 185e66f840cSTristram Ha } 186e66f840cSTristram Ha } else { 187e66f840cSTristram Ha if (check & MIB_COUNTER_OVERFLOW) 188e66f840cSTristram Ha *cnt += MIB_PACKET_DROPPED + 1; 189e66f840cSTristram Ha *cnt += data & MIB_PACKET_DROPPED; 190e66f840cSTristram Ha } 191e66f840cSTristram Ha break; 192e66f840cSTristram Ha } 193e66f840cSTristram Ha } 194e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 195e66f840cSTristram Ha } 196e66f840cSTristram Ha 1974b5baca0SMichael Grzeschik static void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze) 198e66f840cSTristram Ha { 199e66f840cSTristram Ha /* enable the port for flush/freeze function */ 200e66f840cSTristram Ha if (freeze) 201e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 202e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze); 203e66f840cSTristram Ha 204e66f840cSTristram Ha /* disable the port after freeze is done */ 205e66f840cSTristram Ha if (!freeze) 206e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 207e66f840cSTristram Ha } 208e66f840cSTristram Ha 2094b5baca0SMichael Grzeschik static void ksz8_port_init_cnt(struct ksz_device *dev, int port) 210e66f840cSTristram Ha { 211e66f840cSTristram Ha struct ksz_port_mib *mib = &dev->ports[port].mib; 212e66f840cSTristram Ha 213e66f840cSTristram Ha /* flush all enabled port MIB counters */ 214e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true); 215e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true); 216e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false); 217e66f840cSTristram Ha 218e66f840cSTristram Ha mib->cnt_ptr = 0; 219e66f840cSTristram Ha 220e66f840cSTristram Ha /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */ 221e66f840cSTristram Ha while (mib->cnt_ptr < dev->reg_mib_cnt) { 222e66f840cSTristram Ha dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr, 223e66f840cSTristram Ha &mib->counters[mib->cnt_ptr]); 224e66f840cSTristram Ha ++mib->cnt_ptr; 225e66f840cSTristram Ha } 226e66f840cSTristram Ha 227e66f840cSTristram Ha /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */ 228e66f840cSTristram Ha while (mib->cnt_ptr < dev->mib_cnt) { 229e66f840cSTristram Ha dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr, 230e66f840cSTristram Ha NULL, &mib->counters[mib->cnt_ptr]); 231e66f840cSTristram Ha ++mib->cnt_ptr; 232e66f840cSTristram Ha } 233e66f840cSTristram Ha mib->cnt_ptr = 0; 234e66f840cSTristram Ha memset(mib->counters, 0, dev->mib_cnt * sizeof(u64)); 235e66f840cSTristram Ha } 236e66f840cSTristram Ha 2374b5baca0SMichael Grzeschik static void ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data) 238e66f840cSTristram Ha { 239e66f840cSTristram Ha u16 ctrl_addr; 240e66f840cSTristram Ha 241e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr; 242e66f840cSTristram Ha 243e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 244e66f840cSTristram Ha ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr); 245e66f840cSTristram Ha ksz_read64(dev, REG_IND_DATA_HI, data); 246e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 247e66f840cSTristram Ha } 248e66f840cSTristram Ha 2494b5baca0SMichael Grzeschik static void ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data) 250e66f840cSTristram Ha { 251e66f840cSTristram Ha u16 ctrl_addr; 252e66f840cSTristram Ha 253e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(table) | addr; 254e66f840cSTristram Ha 255e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 256e66f840cSTristram Ha ksz_write64(dev, REG_IND_DATA_HI, data); 257e66f840cSTristram Ha ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr); 258e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 259e66f840cSTristram Ha } 260e66f840cSTristram Ha 2614b5baca0SMichael Grzeschik static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data) 262e66f840cSTristram Ha { 263e66f840cSTristram Ha int timeout = 100; 264e66f840cSTristram Ha 265e66f840cSTristram Ha do { 266e66f840cSTristram Ha ksz_read8(dev, REG_IND_DATA_CHECK, data); 267e66f840cSTristram Ha timeout--; 268e66f840cSTristram Ha } while ((*data & DYNAMIC_MAC_TABLE_NOT_READY) && timeout); 269e66f840cSTristram Ha 270e66f840cSTristram Ha /* Entry is not ready for accessing. */ 271e66f840cSTristram Ha if (*data & DYNAMIC_MAC_TABLE_NOT_READY) { 272e66f840cSTristram Ha return -EAGAIN; 273e66f840cSTristram Ha /* Entry is ready for accessing. */ 274e66f840cSTristram Ha } else { 275e66f840cSTristram Ha ksz_read8(dev, REG_IND_DATA_8, data); 276e66f840cSTristram Ha 277e66f840cSTristram Ha /* There is no valid entry in the table. */ 278e66f840cSTristram Ha if (*data & DYNAMIC_MAC_TABLE_MAC_EMPTY) 279e66f840cSTristram Ha return -ENXIO; 280e66f840cSTristram Ha } 281e66f840cSTristram Ha return 0; 282e66f840cSTristram Ha } 283e66f840cSTristram Ha 2844b5baca0SMichael Grzeschik static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, 285e66f840cSTristram Ha u8 *mac_addr, u8 *fid, u8 *src_port, 286e66f840cSTristram Ha u8 *timestamp, u16 *entries) 287e66f840cSTristram Ha { 288e66f840cSTristram Ha u32 data_hi, data_lo; 289e66f840cSTristram Ha u16 ctrl_addr; 290e66f840cSTristram Ha u8 data; 291e66f840cSTristram Ha int rc; 292e66f840cSTristram Ha 293e66f840cSTristram Ha ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr; 294e66f840cSTristram Ha 295e66f840cSTristram Ha mutex_lock(&dev->alu_mutex); 296e66f840cSTristram Ha ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr); 297e66f840cSTristram Ha 2984b5baca0SMichael Grzeschik rc = ksz8_valid_dyn_entry(dev, &data); 299e66f840cSTristram Ha if (rc == -EAGAIN) { 300e66f840cSTristram Ha if (addr == 0) 301e66f840cSTristram Ha *entries = 0; 302e66f840cSTristram Ha } else if (rc == -ENXIO) { 303e66f840cSTristram Ha *entries = 0; 304e66f840cSTristram Ha /* At least one valid entry in the table. */ 305e66f840cSTristram Ha } else { 306e66f840cSTristram Ha u64 buf = 0; 307e66f840cSTristram Ha int cnt; 308e66f840cSTristram Ha 309e66f840cSTristram Ha ksz_read64(dev, REG_IND_DATA_HI, &buf); 310e66f840cSTristram Ha data_hi = (u32)(buf >> 32); 311e66f840cSTristram Ha data_lo = (u32)buf; 312e66f840cSTristram Ha 313e66f840cSTristram Ha /* Check out how many valid entry in the table. */ 314e66f840cSTristram Ha cnt = data & DYNAMIC_MAC_TABLE_ENTRIES_H; 315e66f840cSTristram Ha cnt <<= DYNAMIC_MAC_ENTRIES_H_S; 316e66f840cSTristram Ha cnt |= (data_hi & DYNAMIC_MAC_TABLE_ENTRIES) >> 317e66f840cSTristram Ha DYNAMIC_MAC_ENTRIES_S; 318e66f840cSTristram Ha *entries = cnt + 1; 319e66f840cSTristram Ha 320e66f840cSTristram Ha *fid = (data_hi & DYNAMIC_MAC_TABLE_FID) >> 321e66f840cSTristram Ha DYNAMIC_MAC_FID_S; 322e66f840cSTristram Ha *src_port = (data_hi & DYNAMIC_MAC_TABLE_SRC_PORT) >> 323e66f840cSTristram Ha DYNAMIC_MAC_SRC_PORT_S; 324e66f840cSTristram Ha *timestamp = (data_hi & DYNAMIC_MAC_TABLE_TIMESTAMP) >> 325e66f840cSTristram Ha DYNAMIC_MAC_TIMESTAMP_S; 326e66f840cSTristram Ha 327e66f840cSTristram Ha mac_addr[5] = (u8)data_lo; 328e66f840cSTristram Ha mac_addr[4] = (u8)(data_lo >> 8); 329e66f840cSTristram Ha mac_addr[3] = (u8)(data_lo >> 16); 330e66f840cSTristram Ha mac_addr[2] = (u8)(data_lo >> 24); 331e66f840cSTristram Ha 332e66f840cSTristram Ha mac_addr[1] = (u8)data_hi; 333e66f840cSTristram Ha mac_addr[0] = (u8)(data_hi >> 8); 334e66f840cSTristram Ha rc = 0; 335e66f840cSTristram Ha } 336e66f840cSTristram Ha mutex_unlock(&dev->alu_mutex); 337e66f840cSTristram Ha 338e66f840cSTristram Ha return rc; 339e66f840cSTristram Ha } 340e66f840cSTristram Ha 3414b5baca0SMichael Grzeschik static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr, 342e66f840cSTristram Ha struct alu_struct *alu) 343e66f840cSTristram Ha { 344e66f840cSTristram Ha u32 data_hi, data_lo; 345e66f840cSTristram Ha u64 data; 346e66f840cSTristram Ha 3474b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data); 348e66f840cSTristram Ha data_hi = data >> 32; 349e66f840cSTristram Ha data_lo = (u32)data; 350e66f840cSTristram Ha if (data_hi & (STATIC_MAC_TABLE_VALID | STATIC_MAC_TABLE_OVERRIDE)) { 351e66f840cSTristram Ha alu->mac[5] = (u8)data_lo; 352e66f840cSTristram Ha alu->mac[4] = (u8)(data_lo >> 8); 353e66f840cSTristram Ha alu->mac[3] = (u8)(data_lo >> 16); 354e66f840cSTristram Ha alu->mac[2] = (u8)(data_lo >> 24); 355e66f840cSTristram Ha alu->mac[1] = (u8)data_hi; 356e66f840cSTristram Ha alu->mac[0] = (u8)(data_hi >> 8); 357e66f840cSTristram Ha alu->port_forward = (data_hi & STATIC_MAC_TABLE_FWD_PORTS) >> 358e66f840cSTristram Ha STATIC_MAC_FWD_PORTS_S; 359e66f840cSTristram Ha alu->is_override = 360e66f840cSTristram Ha (data_hi & STATIC_MAC_TABLE_OVERRIDE) ? 1 : 0; 361e66f840cSTristram Ha data_hi >>= 1; 362e66f840cSTristram Ha alu->is_use_fid = (data_hi & STATIC_MAC_TABLE_USE_FID) ? 1 : 0; 363e66f840cSTristram Ha alu->fid = (data_hi & STATIC_MAC_TABLE_FID) >> 364e66f840cSTristram Ha STATIC_MAC_FID_S; 365e66f840cSTristram Ha return 0; 366e66f840cSTristram Ha } 367e66f840cSTristram Ha return -ENXIO; 368e66f840cSTristram Ha } 369e66f840cSTristram Ha 3704b5baca0SMichael Grzeschik static void ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr, 371e66f840cSTristram Ha struct alu_struct *alu) 372e66f840cSTristram Ha { 373e66f840cSTristram Ha u32 data_hi, data_lo; 374e66f840cSTristram Ha u64 data; 375e66f840cSTristram Ha 376e66f840cSTristram Ha data_lo = ((u32)alu->mac[2] << 24) | 377e66f840cSTristram Ha ((u32)alu->mac[3] << 16) | 378e66f840cSTristram Ha ((u32)alu->mac[4] << 8) | alu->mac[5]; 379e66f840cSTristram Ha data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1]; 380e66f840cSTristram Ha data_hi |= (u32)alu->port_forward << STATIC_MAC_FWD_PORTS_S; 381e66f840cSTristram Ha 382e66f840cSTristram Ha if (alu->is_override) 383e66f840cSTristram Ha data_hi |= STATIC_MAC_TABLE_OVERRIDE; 384e66f840cSTristram Ha if (alu->is_use_fid) { 385e66f840cSTristram Ha data_hi |= STATIC_MAC_TABLE_USE_FID; 386e66f840cSTristram Ha data_hi |= (u32)alu->fid << STATIC_MAC_FID_S; 387e66f840cSTristram Ha } 388e66f840cSTristram Ha if (alu->is_static) 389e66f840cSTristram Ha data_hi |= STATIC_MAC_TABLE_VALID; 390e66f840cSTristram Ha else 391e66f840cSTristram Ha data_hi &= ~STATIC_MAC_TABLE_OVERRIDE; 392e66f840cSTristram Ha 393e66f840cSTristram Ha data = (u64)data_hi << 32 | data_lo; 3944b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data); 395e66f840cSTristram Ha } 396e66f840cSTristram Ha 3974b5baca0SMichael Grzeschik static void ksz8_from_vlan(u16 vlan, u8 *fid, u8 *member, u8 *valid) 398e66f840cSTristram Ha { 399e66f840cSTristram Ha *fid = vlan & VLAN_TABLE_FID; 400e66f840cSTristram Ha *member = (vlan & VLAN_TABLE_MEMBERSHIP) >> VLAN_TABLE_MEMBERSHIP_S; 401e66f840cSTristram Ha *valid = !!(vlan & VLAN_TABLE_VALID); 402e66f840cSTristram Ha } 403e66f840cSTristram Ha 4044b5baca0SMichael Grzeschik static void ksz8_to_vlan(u8 fid, u8 member, u8 valid, u16 *vlan) 405e66f840cSTristram Ha { 406e66f840cSTristram Ha *vlan = fid; 407e66f840cSTristram Ha *vlan |= (u16)member << VLAN_TABLE_MEMBERSHIP_S; 408e66f840cSTristram Ha if (valid) 409e66f840cSTristram Ha *vlan |= VLAN_TABLE_VALID; 410e66f840cSTristram Ha } 411e66f840cSTristram Ha 4124b5baca0SMichael Grzeschik static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr) 413e66f840cSTristram Ha { 414e66f840cSTristram Ha u64 data; 415e66f840cSTristram Ha int i; 416e66f840cSTristram Ha 4174b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &data); 4184ce2a984SMichael Grzeschik addr *= dev->phy_port_cnt; 4194ce2a984SMichael Grzeschik for (i = 0; i < dev->phy_port_cnt; i++) { 420e66f840cSTristram Ha dev->vlan_cache[addr + i].table[0] = (u16)data; 421e66f840cSTristram Ha data >>= VLAN_TABLE_S; 422e66f840cSTristram Ha } 423e66f840cSTristram Ha } 424e66f840cSTristram Ha 4254b5baca0SMichael Grzeschik static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan) 426e66f840cSTristram Ha { 427e66f840cSTristram Ha int index; 428e66f840cSTristram Ha u16 *data; 429e66f840cSTristram Ha u16 addr; 430e66f840cSTristram Ha u64 buf; 431e66f840cSTristram Ha 432e66f840cSTristram Ha data = (u16 *)&buf; 4334ce2a984SMichael Grzeschik addr = vid / dev->phy_port_cnt; 434e66f840cSTristram Ha index = vid & 3; 4354b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 436e66f840cSTristram Ha *vlan = data[index]; 437e66f840cSTristram Ha } 438e66f840cSTristram Ha 4394b5baca0SMichael Grzeschik static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan) 440e66f840cSTristram Ha { 441e66f840cSTristram Ha int index; 442e66f840cSTristram Ha u16 *data; 443e66f840cSTristram Ha u16 addr; 444e66f840cSTristram Ha u64 buf; 445e66f840cSTristram Ha 446e66f840cSTristram Ha data = (u16 *)&buf; 4474ce2a984SMichael Grzeschik addr = vid / dev->phy_port_cnt; 448e66f840cSTristram Ha index = vid & 3; 4494b5baca0SMichael Grzeschik ksz8_r_table(dev, TABLE_VLAN, addr, &buf); 450e66f840cSTristram Ha data[index] = vlan; 451e66f840cSTristram Ha dev->vlan_cache[vid].table[0] = vlan; 4524b5baca0SMichael Grzeschik ksz8_w_table(dev, TABLE_VLAN, addr, buf); 453e66f840cSTristram Ha } 454e66f840cSTristram Ha 4554b5baca0SMichael Grzeschik static void ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val) 456e66f840cSTristram Ha { 457e66f840cSTristram Ha u8 restart, speed, ctrl, link; 458e66f840cSTristram Ha int processed = true; 459e66f840cSTristram Ha u16 data = 0; 460e66f840cSTristram Ha u8 p = phy; 461e66f840cSTristram Ha 462e66f840cSTristram Ha switch (reg) { 463e66f840cSTristram Ha case PHY_REG_CTRL: 464e66f840cSTristram Ha ksz_pread8(dev, p, P_NEG_RESTART_CTRL, &restart); 465e66f840cSTristram Ha ksz_pread8(dev, p, P_SPEED_STATUS, &speed); 466e66f840cSTristram Ha ksz_pread8(dev, p, P_FORCE_CTRL, &ctrl); 467e66f840cSTristram Ha if (restart & PORT_PHY_LOOPBACK) 468e66f840cSTristram Ha data |= PHY_LOOPBACK; 469e66f840cSTristram Ha if (ctrl & PORT_FORCE_100_MBIT) 470e66f840cSTristram Ha data |= PHY_SPEED_100MBIT; 471e66f840cSTristram Ha if (!(ctrl & PORT_AUTO_NEG_DISABLE)) 472e66f840cSTristram Ha data |= PHY_AUTO_NEG_ENABLE; 473e66f840cSTristram Ha if (restart & PORT_POWER_DOWN) 474e66f840cSTristram Ha data |= PHY_POWER_DOWN; 475e66f840cSTristram Ha if (restart & PORT_AUTO_NEG_RESTART) 476e66f840cSTristram Ha data |= PHY_AUTO_NEG_RESTART; 477e66f840cSTristram Ha if (ctrl & PORT_FORCE_FULL_DUPLEX) 478e66f840cSTristram Ha data |= PHY_FULL_DUPLEX; 479e66f840cSTristram Ha if (speed & PORT_HP_MDIX) 480e66f840cSTristram Ha data |= PHY_HP_MDIX; 481e66f840cSTristram Ha if (restart & PORT_FORCE_MDIX) 482e66f840cSTristram Ha data |= PHY_FORCE_MDIX; 483e66f840cSTristram Ha if (restart & PORT_AUTO_MDIX_DISABLE) 484e66f840cSTristram Ha data |= PHY_AUTO_MDIX_DISABLE; 485e66f840cSTristram Ha if (restart & PORT_TX_DISABLE) 486e66f840cSTristram Ha data |= PHY_TRANSMIT_DISABLE; 487e66f840cSTristram Ha if (restart & PORT_LED_OFF) 488e66f840cSTristram Ha data |= PHY_LED_DISABLE; 489e66f840cSTristram Ha break; 490e66f840cSTristram Ha case PHY_REG_STATUS: 491e66f840cSTristram Ha ksz_pread8(dev, p, P_LINK_STATUS, &link); 492e66f840cSTristram Ha data = PHY_100BTX_FD_CAPABLE | 493e66f840cSTristram Ha PHY_100BTX_CAPABLE | 494e66f840cSTristram Ha PHY_10BT_FD_CAPABLE | 495e66f840cSTristram Ha PHY_10BT_CAPABLE | 496e66f840cSTristram Ha PHY_AUTO_NEG_CAPABLE; 497e66f840cSTristram Ha if (link & PORT_AUTO_NEG_COMPLETE) 498e66f840cSTristram Ha data |= PHY_AUTO_NEG_ACKNOWLEDGE; 499e66f840cSTristram Ha if (link & PORT_STAT_LINK_GOOD) 500e66f840cSTristram Ha data |= PHY_LINK_STATUS; 501e66f840cSTristram Ha break; 502e66f840cSTristram Ha case PHY_REG_ID_1: 503e66f840cSTristram Ha data = KSZ8795_ID_HI; 504e66f840cSTristram Ha break; 505e66f840cSTristram Ha case PHY_REG_ID_2: 506e66f840cSTristram Ha data = KSZ8795_ID_LO; 507e66f840cSTristram Ha break; 508e66f840cSTristram Ha case PHY_REG_AUTO_NEGOTIATION: 509e66f840cSTristram Ha ksz_pread8(dev, p, P_LOCAL_CTRL, &ctrl); 510e66f840cSTristram Ha data = PHY_AUTO_NEG_802_3; 511e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_SYM_PAUSE) 512e66f840cSTristram Ha data |= PHY_AUTO_NEG_SYM_PAUSE; 513e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX_FD) 514e66f840cSTristram Ha data |= PHY_AUTO_NEG_100BTX_FD; 515e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_100BTX) 516e66f840cSTristram Ha data |= PHY_AUTO_NEG_100BTX; 517e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT_FD) 518e66f840cSTristram Ha data |= PHY_AUTO_NEG_10BT_FD; 519e66f840cSTristram Ha if (ctrl & PORT_AUTO_NEG_10BT) 520e66f840cSTristram Ha data |= PHY_AUTO_NEG_10BT; 521e66f840cSTristram Ha break; 522e66f840cSTristram Ha case PHY_REG_REMOTE_CAPABILITY: 523e66f840cSTristram Ha ksz_pread8(dev, p, P_REMOTE_STATUS, &link); 524e66f840cSTristram Ha data = PHY_AUTO_NEG_802_3; 525e66f840cSTristram Ha if (link & PORT_REMOTE_SYM_PAUSE) 526e66f840cSTristram Ha data |= PHY_AUTO_NEG_SYM_PAUSE; 527e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX_FD) 528e66f840cSTristram Ha data |= PHY_AUTO_NEG_100BTX_FD; 529e66f840cSTristram Ha if (link & PORT_REMOTE_100BTX) 530e66f840cSTristram Ha data |= PHY_AUTO_NEG_100BTX; 531e66f840cSTristram Ha if (link & PORT_REMOTE_10BT_FD) 532e66f840cSTristram Ha data |= PHY_AUTO_NEG_10BT_FD; 533e66f840cSTristram Ha if (link & PORT_REMOTE_10BT) 534e66f840cSTristram Ha data |= PHY_AUTO_NEG_10BT; 535e66f840cSTristram Ha if (data & ~PHY_AUTO_NEG_802_3) 536e66f840cSTristram Ha data |= PHY_REMOTE_ACKNOWLEDGE_NOT; 537e66f840cSTristram Ha break; 538e66f840cSTristram Ha default: 539e66f840cSTristram Ha processed = false; 540e66f840cSTristram Ha break; 541e66f840cSTristram Ha } 542e66f840cSTristram Ha if (processed) 543e66f840cSTristram Ha *val = data; 544e66f840cSTristram Ha } 545e66f840cSTristram Ha 5464b5baca0SMichael Grzeschik static void ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val) 547e66f840cSTristram Ha { 548e66f840cSTristram Ha u8 p = phy; 549e66f840cSTristram Ha u8 restart, speed, ctrl, data; 550e66f840cSTristram Ha 551e66f840cSTristram Ha switch (reg) { 552e66f840cSTristram Ha case PHY_REG_CTRL: 553e66f840cSTristram Ha 554e66f840cSTristram Ha /* Do not support PHY reset function. */ 555e66f840cSTristram Ha if (val & PHY_RESET) 556e66f840cSTristram Ha break; 557e66f840cSTristram Ha ksz_pread8(dev, p, P_SPEED_STATUS, &speed); 558e66f840cSTristram Ha data = speed; 559e66f840cSTristram Ha if (val & PHY_HP_MDIX) 560e66f840cSTristram Ha data |= PORT_HP_MDIX; 561e66f840cSTristram Ha else 562e66f840cSTristram Ha data &= ~PORT_HP_MDIX; 563e66f840cSTristram Ha if (data != speed) 564e66f840cSTristram Ha ksz_pwrite8(dev, p, P_SPEED_STATUS, data); 565e66f840cSTristram Ha ksz_pread8(dev, p, P_FORCE_CTRL, &ctrl); 566e66f840cSTristram Ha data = ctrl; 567e66f840cSTristram Ha if (!(val & PHY_AUTO_NEG_ENABLE)) 568e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 569e66f840cSTristram Ha else 570e66f840cSTristram Ha data &= ~PORT_AUTO_NEG_DISABLE; 571e66f840cSTristram Ha 572e66f840cSTristram Ha /* Fiber port does not support auto-negotiation. */ 573e66f840cSTristram Ha if (dev->ports[p].fiber) 574e66f840cSTristram Ha data |= PORT_AUTO_NEG_DISABLE; 575e66f840cSTristram Ha if (val & PHY_SPEED_100MBIT) 576e66f840cSTristram Ha data |= PORT_FORCE_100_MBIT; 577e66f840cSTristram Ha else 578e66f840cSTristram Ha data &= ~PORT_FORCE_100_MBIT; 579e66f840cSTristram Ha if (val & PHY_FULL_DUPLEX) 580e66f840cSTristram Ha data |= PORT_FORCE_FULL_DUPLEX; 581e66f840cSTristram Ha else 582e66f840cSTristram Ha data &= ~PORT_FORCE_FULL_DUPLEX; 583e66f840cSTristram Ha if (data != ctrl) 584e66f840cSTristram Ha ksz_pwrite8(dev, p, P_FORCE_CTRL, data); 585e66f840cSTristram Ha ksz_pread8(dev, p, P_NEG_RESTART_CTRL, &restart); 586e66f840cSTristram Ha data = restart; 587e66f840cSTristram Ha if (val & PHY_LED_DISABLE) 588e66f840cSTristram Ha data |= PORT_LED_OFF; 589e66f840cSTristram Ha else 590e66f840cSTristram Ha data &= ~PORT_LED_OFF; 591e66f840cSTristram Ha if (val & PHY_TRANSMIT_DISABLE) 592e66f840cSTristram Ha data |= PORT_TX_DISABLE; 593e66f840cSTristram Ha else 594e66f840cSTristram Ha data &= ~PORT_TX_DISABLE; 595e66f840cSTristram Ha if (val & PHY_AUTO_NEG_RESTART) 596e66f840cSTristram Ha data |= PORT_AUTO_NEG_RESTART; 597e66f840cSTristram Ha else 598e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_RESTART); 599e66f840cSTristram Ha if (val & PHY_POWER_DOWN) 600e66f840cSTristram Ha data |= PORT_POWER_DOWN; 601e66f840cSTristram Ha else 602e66f840cSTristram Ha data &= ~PORT_POWER_DOWN; 603e66f840cSTristram Ha if (val & PHY_AUTO_MDIX_DISABLE) 604e66f840cSTristram Ha data |= PORT_AUTO_MDIX_DISABLE; 605e66f840cSTristram Ha else 606e66f840cSTristram Ha data &= ~PORT_AUTO_MDIX_DISABLE; 607e66f840cSTristram Ha if (val & PHY_FORCE_MDIX) 608e66f840cSTristram Ha data |= PORT_FORCE_MDIX; 609e66f840cSTristram Ha else 610e66f840cSTristram Ha data &= ~PORT_FORCE_MDIX; 611e66f840cSTristram Ha if (val & PHY_LOOPBACK) 612e66f840cSTristram Ha data |= PORT_PHY_LOOPBACK; 613e66f840cSTristram Ha else 614e66f840cSTristram Ha data &= ~PORT_PHY_LOOPBACK; 615e66f840cSTristram Ha if (data != restart) 616e66f840cSTristram Ha ksz_pwrite8(dev, p, P_NEG_RESTART_CTRL, data); 617e66f840cSTristram Ha break; 618e66f840cSTristram Ha case PHY_REG_AUTO_NEGOTIATION: 619e66f840cSTristram Ha ksz_pread8(dev, p, P_LOCAL_CTRL, &ctrl); 620e66f840cSTristram Ha data = ctrl; 621e66f840cSTristram Ha data &= ~(PORT_AUTO_NEG_SYM_PAUSE | 622e66f840cSTristram Ha PORT_AUTO_NEG_100BTX_FD | 623e66f840cSTristram Ha PORT_AUTO_NEG_100BTX | 624e66f840cSTristram Ha PORT_AUTO_NEG_10BT_FD | 625e66f840cSTristram Ha PORT_AUTO_NEG_10BT); 626e66f840cSTristram Ha if (val & PHY_AUTO_NEG_SYM_PAUSE) 627e66f840cSTristram Ha data |= PORT_AUTO_NEG_SYM_PAUSE; 628e66f840cSTristram Ha if (val & PHY_AUTO_NEG_100BTX_FD) 629e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX_FD; 630e66f840cSTristram Ha if (val & PHY_AUTO_NEG_100BTX) 631e66f840cSTristram Ha data |= PORT_AUTO_NEG_100BTX; 632e66f840cSTristram Ha if (val & PHY_AUTO_NEG_10BT_FD) 633e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT_FD; 634e66f840cSTristram Ha if (val & PHY_AUTO_NEG_10BT) 635e66f840cSTristram Ha data |= PORT_AUTO_NEG_10BT; 636e66f840cSTristram Ha if (data != ctrl) 637e66f840cSTristram Ha ksz_pwrite8(dev, p, P_LOCAL_CTRL, data); 638e66f840cSTristram Ha break; 639e66f840cSTristram Ha default: 640e66f840cSTristram Ha break; 641e66f840cSTristram Ha } 642e66f840cSTristram Ha } 643e66f840cSTristram Ha 6444b5baca0SMichael Grzeschik static enum dsa_tag_protocol ksz8_get_tag_protocol(struct dsa_switch *ds, 6454d776482SFlorian Fainelli int port, 6464d776482SFlorian Fainelli enum dsa_tag_protocol mp) 647e66f840cSTristram Ha { 648e66f840cSTristram Ha return DSA_TAG_PROTO_KSZ8795; 649e66f840cSTristram Ha } 650e66f840cSTristram Ha 6514b5baca0SMichael Grzeschik static void ksz8_get_strings(struct dsa_switch *ds, int port, 652e66f840cSTristram Ha u32 stringset, uint8_t *buf) 653e66f840cSTristram Ha { 65465fe1acfSMichael Grzeschik struct ksz_device *dev = ds->priv; 655e66f840cSTristram Ha int i; 656e66f840cSTristram Ha 65765fe1acfSMichael Grzeschik for (i = 0; i < dev->mib_cnt; i++) { 658e66f840cSTristram Ha memcpy(buf + i * ETH_GSTRING_LEN, mib_names[i].string, 659e66f840cSTristram Ha ETH_GSTRING_LEN); 660e66f840cSTristram Ha } 661e66f840cSTristram Ha } 662e66f840cSTristram Ha 6634b5baca0SMichael Grzeschik static void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member) 664e66f840cSTristram Ha { 665e66f840cSTristram Ha u8 data; 666e66f840cSTristram Ha 667e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 668e66f840cSTristram Ha data &= ~PORT_VLAN_MEMBERSHIP; 669e66f840cSTristram Ha data |= (member & dev->port_mask); 670e66f840cSTristram Ha ksz_pwrite8(dev, port, P_MIRROR_CTRL, data); 671e66f840cSTristram Ha dev->ports[port].member = member; 672e66f840cSTristram Ha } 673e66f840cSTristram Ha 6744b5baca0SMichael Grzeschik static void ksz8_port_stp_state_set(struct dsa_switch *ds, int port, u8 state) 675e66f840cSTristram Ha { 676e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 677e66f840cSTristram Ha int forward = dev->member; 678e66f840cSTristram Ha struct ksz_port *p; 679e66f840cSTristram Ha int member = -1; 680e66f840cSTristram Ha u8 data; 681e66f840cSTristram Ha 682e66f840cSTristram Ha p = &dev->ports[port]; 683e66f840cSTristram Ha 684e66f840cSTristram Ha ksz_pread8(dev, port, P_STP_CTRL, &data); 685e66f840cSTristram Ha data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE); 686e66f840cSTristram Ha 687e66f840cSTristram Ha switch (state) { 688e66f840cSTristram Ha case BR_STATE_DISABLED: 689e66f840cSTristram Ha data |= PORT_LEARN_DISABLE; 6904ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt) 691e66f840cSTristram Ha member = 0; 692e66f840cSTristram Ha break; 693e66f840cSTristram Ha case BR_STATE_LISTENING: 694e66f840cSTristram Ha data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE); 6954ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt && 696e66f840cSTristram Ha p->stp_state == BR_STATE_DISABLED) 697e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 698e66f840cSTristram Ha break; 699e66f840cSTristram Ha case BR_STATE_LEARNING: 700e66f840cSTristram Ha data |= PORT_RX_ENABLE; 701e66f840cSTristram Ha break; 702e66f840cSTristram Ha case BR_STATE_FORWARDING: 703e66f840cSTristram Ha data |= (PORT_TX_ENABLE | PORT_RX_ENABLE); 704e66f840cSTristram Ha 705e66f840cSTristram Ha /* This function is also used internally. */ 706e66f840cSTristram Ha if (port == dev->cpu_port) 707e66f840cSTristram Ha break; 708e66f840cSTristram Ha 709e66f840cSTristram Ha /* Port is a member of a bridge. */ 710e66f840cSTristram Ha if (dev->br_member & BIT(port)) { 711e66f840cSTristram Ha dev->member |= BIT(port); 712e66f840cSTristram Ha member = dev->member; 713e66f840cSTristram Ha } else { 714e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 715e66f840cSTristram Ha } 716e66f840cSTristram Ha break; 717e66f840cSTristram Ha case BR_STATE_BLOCKING: 718e66f840cSTristram Ha data |= PORT_LEARN_DISABLE; 7194ce2a984SMichael Grzeschik if (port < dev->phy_port_cnt && 720e66f840cSTristram Ha p->stp_state == BR_STATE_DISABLED) 721e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 722e66f840cSTristram Ha break; 723e66f840cSTristram Ha default: 724e66f840cSTristram Ha dev_err(ds->dev, "invalid STP state: %d\n", state); 725e66f840cSTristram Ha return; 726e66f840cSTristram Ha } 727e66f840cSTristram Ha 728e66f840cSTristram Ha ksz_pwrite8(dev, port, P_STP_CTRL, data); 729e66f840cSTristram Ha p->stp_state = state; 730e66f840cSTristram Ha /* Port membership may share register with STP state. */ 731e66f840cSTristram Ha if (member >= 0 && member != p->member) 7324b5baca0SMichael Grzeschik ksz8_cfg_port_member(dev, port, (u8)member); 733e66f840cSTristram Ha 734e66f840cSTristram Ha /* Check if forwarding needs to be updated. */ 735e66f840cSTristram Ha if (state != BR_STATE_FORWARDING) { 736e66f840cSTristram Ha if (dev->br_member & BIT(port)) 737e66f840cSTristram Ha dev->member &= ~BIT(port); 738e66f840cSTristram Ha } 739e66f840cSTristram Ha 740e66f840cSTristram Ha /* When topology has changed the function ksz_update_port_member 741e66f840cSTristram Ha * should be called to modify port forwarding behavior. 742e66f840cSTristram Ha */ 743e66f840cSTristram Ha if (forward != dev->member) 744e66f840cSTristram Ha ksz_update_port_member(dev, port); 745e66f840cSTristram Ha } 746e66f840cSTristram Ha 7474b5baca0SMichael Grzeschik static void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port) 748e66f840cSTristram Ha { 749241ed719SMichael Grzeschik u8 learn[DSA_MAX_PORTS]; 750e66f840cSTristram Ha int first, index, cnt; 751e66f840cSTristram Ha struct ksz_port *p; 752e66f840cSTristram Ha 753241ed719SMichael Grzeschik if ((uint)port < dev->port_cnt) { 754e66f840cSTristram Ha first = port; 755e66f840cSTristram Ha cnt = port + 1; 756e66f840cSTristram Ha } else { 757e66f840cSTristram Ha /* Flush all ports. */ 758e66f840cSTristram Ha first = 0; 759c9f4633bSMichael Grzeschik cnt = dev->port_cnt; 760e66f840cSTristram Ha } 761e66f840cSTristram Ha for (index = first; index < cnt; index++) { 762e66f840cSTristram Ha p = &dev->ports[index]; 763e66f840cSTristram Ha if (!p->on) 764e66f840cSTristram Ha continue; 765e66f840cSTristram Ha ksz_pread8(dev, index, P_STP_CTRL, &learn[index]); 766e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 767e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, 768e66f840cSTristram Ha learn[index] | PORT_LEARN_DISABLE); 769e66f840cSTristram Ha } 770e66f840cSTristram Ha ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true); 771e66f840cSTristram Ha for (index = first; index < cnt; index++) { 772e66f840cSTristram Ha p = &dev->ports[index]; 773e66f840cSTristram Ha if (!p->on) 774e66f840cSTristram Ha continue; 775e66f840cSTristram Ha if (!(learn[index] & PORT_LEARN_DISABLE)) 776e66f840cSTristram Ha ksz_pwrite8(dev, index, P_STP_CTRL, learn[index]); 777e66f840cSTristram Ha } 778e66f840cSTristram Ha } 779e66f840cSTristram Ha 7804b5baca0SMichael Grzeschik static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag, 78189153ed6SVladimir Oltean struct netlink_ext_ack *extack) 782e66f840cSTristram Ha { 783e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 784e66f840cSTristram Ha 785e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag); 786e66f840cSTristram Ha 787e66f840cSTristram Ha return 0; 788e66f840cSTristram Ha } 789e66f840cSTristram Ha 7904b5baca0SMichael Grzeschik static int ksz8_port_vlan_add(struct dsa_switch *ds, int port, 79131046a5fSVladimir Oltean const struct switchdev_obj_port_vlan *vlan, 79231046a5fSVladimir Oltean struct netlink_ext_ack *extack) 793e66f840cSTristram Ha { 794e66f840cSTristram Ha bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 795e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 796b7a9e0daSVladimir Oltean u16 data, new_pvid = 0; 797e66f840cSTristram Ha u8 fid, member, valid; 798e66f840cSTristram Ha 799e66f840cSTristram Ha ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged); 800e66f840cSTristram Ha 8014b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 8024b5baca0SMichael Grzeschik ksz8_from_vlan(data, &fid, &member, &valid); 803e66f840cSTristram Ha 804e66f840cSTristram Ha /* First time to setup the VLAN entry. */ 805e66f840cSTristram Ha if (!valid) { 806e66f840cSTristram Ha /* Need to find a way to map VID to FID. */ 807e66f840cSTristram Ha fid = 1; 808e66f840cSTristram Ha valid = 1; 809e66f840cSTristram Ha } 810e66f840cSTristram Ha member |= BIT(port); 811e66f840cSTristram Ha 8124b5baca0SMichael Grzeschik ksz8_to_vlan(fid, member, valid, &data); 8134b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 814e66f840cSTristram Ha 815e66f840cSTristram Ha /* change PVID */ 816e66f840cSTristram Ha if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 817b7a9e0daSVladimir Oltean new_pvid = vlan->vid; 818e66f840cSTristram Ha 819e66f840cSTristram Ha if (new_pvid) { 820b7a9e0daSVladimir Oltean u16 vid; 821b7a9e0daSVladimir Oltean 822e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid); 823e66f840cSTristram Ha vid &= 0xfff; 824e66f840cSTristram Ha vid |= new_pvid; 825e66f840cSTristram Ha ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid); 826e66f840cSTristram Ha } 8271958d581SVladimir Oltean 8281958d581SVladimir Oltean return 0; 829e66f840cSTristram Ha } 830e66f840cSTristram Ha 8314b5baca0SMichael Grzeschik static int ksz8_port_vlan_del(struct dsa_switch *ds, int port, 832e66f840cSTristram Ha const struct switchdev_obj_port_vlan *vlan) 833e66f840cSTristram Ha { 834e66f840cSTristram Ha bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 835e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 836b7a9e0daSVladimir Oltean u16 data, pvid, new_pvid = 0; 837e66f840cSTristram Ha u8 fid, member, valid; 838e66f840cSTristram Ha 839e66f840cSTristram Ha ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid); 840e66f840cSTristram Ha pvid = pvid & 0xFFF; 841e66f840cSTristram Ha 842e66f840cSTristram Ha ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged); 843e66f840cSTristram Ha 8444b5baca0SMichael Grzeschik ksz8_r_vlan_table(dev, vlan->vid, &data); 8454b5baca0SMichael Grzeschik ksz8_from_vlan(data, &fid, &member, &valid); 846e66f840cSTristram Ha 847e66f840cSTristram Ha member &= ~BIT(port); 848e66f840cSTristram Ha 849e66f840cSTristram Ha /* Invalidate the entry if no more member. */ 850e66f840cSTristram Ha if (!member) { 851e66f840cSTristram Ha fid = 0; 852e66f840cSTristram Ha valid = 0; 853e66f840cSTristram Ha } 854e66f840cSTristram Ha 855b7a9e0daSVladimir Oltean if (pvid == vlan->vid) 856e66f840cSTristram Ha new_pvid = 1; 857e66f840cSTristram Ha 8584b5baca0SMichael Grzeschik ksz8_to_vlan(fid, member, valid, &data); 8594b5baca0SMichael Grzeschik ksz8_w_vlan_table(dev, vlan->vid, data); 860e66f840cSTristram Ha 861e66f840cSTristram Ha if (new_pvid != pvid) 862e66f840cSTristram Ha ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, pvid); 863e66f840cSTristram Ha 864e66f840cSTristram Ha return 0; 865e66f840cSTristram Ha } 866e66f840cSTristram Ha 8674b5baca0SMichael Grzeschik static int ksz8_port_mirror_add(struct dsa_switch *ds, int port, 868e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror, 869e66f840cSTristram Ha bool ingress) 870e66f840cSTristram Ha { 871e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 872e66f840cSTristram Ha 873e66f840cSTristram Ha if (ingress) { 874e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true); 875e66f840cSTristram Ha dev->mirror_rx |= BIT(port); 876e66f840cSTristram Ha } else { 877e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true); 878e66f840cSTristram Ha dev->mirror_tx |= BIT(port); 879e66f840cSTristram Ha } 880e66f840cSTristram Ha 881e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false); 882e66f840cSTristram Ha 883e66f840cSTristram Ha /* configure mirror port */ 884e66f840cSTristram Ha if (dev->mirror_rx || dev->mirror_tx) 885e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 886e66f840cSTristram Ha PORT_MIRROR_SNIFFER, true); 887e66f840cSTristram Ha 888e66f840cSTristram Ha return 0; 889e66f840cSTristram Ha } 890e66f840cSTristram Ha 8914b5baca0SMichael Grzeschik static void ksz8_port_mirror_del(struct dsa_switch *ds, int port, 892e66f840cSTristram Ha struct dsa_mall_mirror_tc_entry *mirror) 893e66f840cSTristram Ha { 894e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 895e66f840cSTristram Ha u8 data; 896e66f840cSTristram Ha 897e66f840cSTristram Ha if (mirror->ingress) { 898e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false); 899e66f840cSTristram Ha dev->mirror_rx &= ~BIT(port); 900e66f840cSTristram Ha } else { 901e66f840cSTristram Ha ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false); 902e66f840cSTristram Ha dev->mirror_tx &= ~BIT(port); 903e66f840cSTristram Ha } 904e66f840cSTristram Ha 905e66f840cSTristram Ha ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 906e66f840cSTristram Ha 907e66f840cSTristram Ha if (!dev->mirror_rx && !dev->mirror_tx) 908e66f840cSTristram Ha ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 909e66f840cSTristram Ha PORT_MIRROR_SNIFFER, false); 910e66f840cSTristram Ha } 911e66f840cSTristram Ha 912*c2ac4d2aSMichael Grzeschik static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port) 913e66f840cSTristram Ha { 914e66f840cSTristram Ha struct ksz_port *p = &dev->ports[port]; 915*c2ac4d2aSMichael Grzeschik u8 data8; 916e66f840cSTristram Ha 917edecfa98SHelmut Grohne if (!p->interface && dev->compat_interface) { 918edecfa98SHelmut Grohne dev_warn(dev->dev, 919edecfa98SHelmut Grohne "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. " 920edecfa98SHelmut Grohne "Please update your device tree.\n", 921edecfa98SHelmut Grohne port); 922edecfa98SHelmut Grohne p->interface = dev->compat_interface; 923edecfa98SHelmut Grohne } 924edecfa98SHelmut Grohne 925e66f840cSTristram Ha /* Configure MII interface for proper network communication. */ 926e66f840cSTristram Ha ksz_read8(dev, REG_PORT_5_CTRL_6, &data8); 927e66f840cSTristram Ha data8 &= ~PORT_INTERFACE_TYPE; 928e66f840cSTristram Ha data8 &= ~PORT_GMII_1GPS_MODE; 929edecfa98SHelmut Grohne switch (p->interface) { 930e66f840cSTristram Ha case PHY_INTERFACE_MODE_MII: 931e66f840cSTristram Ha p->phydev.speed = SPEED_100; 932e66f840cSTristram Ha break; 933e66f840cSTristram Ha case PHY_INTERFACE_MODE_RMII: 934e66f840cSTristram Ha data8 |= PORT_INTERFACE_RMII; 935e66f840cSTristram Ha p->phydev.speed = SPEED_100; 936e66f840cSTristram Ha break; 937e66f840cSTristram Ha case PHY_INTERFACE_MODE_GMII: 938e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 939e66f840cSTristram Ha data8 |= PORT_INTERFACE_GMII; 940e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 941e66f840cSTristram Ha break; 942e66f840cSTristram Ha default: 943e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_IN_ENABLE; 944e66f840cSTristram Ha data8 &= ~PORT_RGMII_ID_OUT_ENABLE; 945edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 946edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_RXID) 947e66f840cSTristram Ha data8 |= PORT_RGMII_ID_IN_ENABLE; 948edecfa98SHelmut Grohne if (p->interface == PHY_INTERFACE_MODE_RGMII_ID || 949edecfa98SHelmut Grohne p->interface == PHY_INTERFACE_MODE_RGMII_TXID) 950e66f840cSTristram Ha data8 |= PORT_RGMII_ID_OUT_ENABLE; 951e66f840cSTristram Ha data8 |= PORT_GMII_1GPS_MODE; 952e66f840cSTristram Ha data8 |= PORT_INTERFACE_RGMII; 953e66f840cSTristram Ha p->phydev.speed = SPEED_1000; 954e66f840cSTristram Ha break; 955e66f840cSTristram Ha } 956e66f840cSTristram Ha ksz_write8(dev, REG_PORT_5_CTRL_6, data8); 957e66f840cSTristram Ha p->phydev.duplex = 1; 958*c2ac4d2aSMichael Grzeschik } 959*c2ac4d2aSMichael Grzeschik 960*c2ac4d2aSMichael Grzeschik static void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port) 961*c2ac4d2aSMichael Grzeschik { 962*c2ac4d2aSMichael Grzeschik struct ksz_port *p = &dev->ports[port]; 963*c2ac4d2aSMichael Grzeschik u8 member; 964*c2ac4d2aSMichael Grzeschik 965*c2ac4d2aSMichael Grzeschik /* enable broadcast storm limit */ 966*c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true); 967*c2ac4d2aSMichael Grzeschik 968*c2ac4d2aSMichael Grzeschik ksz8795_set_prio_queue(dev, port, 4); 969*c2ac4d2aSMichael Grzeschik 970*c2ac4d2aSMichael Grzeschik /* disable DiffServ priority */ 971*c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false); 972*c2ac4d2aSMichael Grzeschik 973*c2ac4d2aSMichael Grzeschik /* replace priority */ 974*c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_802_1P_CTRL, PORT_802_1P_REMAPPING, false); 975*c2ac4d2aSMichael Grzeschik 976*c2ac4d2aSMichael Grzeschik /* enable 802.1p priority */ 977*c2ac4d2aSMichael Grzeschik ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true); 978*c2ac4d2aSMichael Grzeschik 979*c2ac4d2aSMichael Grzeschik if (cpu_port) { 980*c2ac4d2aSMichael Grzeschik ksz8795_cpu_interface_select(dev, port); 981e66f840cSTristram Ha 982e66f840cSTristram Ha member = dev->port_mask; 983e66f840cSTristram Ha } else { 984e66f840cSTristram Ha member = dev->host_mask | p->vid_member; 985e66f840cSTristram Ha } 9864b5baca0SMichael Grzeschik ksz8_cfg_port_member(dev, port, member); 987e66f840cSTristram Ha } 988e66f840cSTristram Ha 9894b5baca0SMichael Grzeschik static void ksz8_config_cpu_port(struct dsa_switch *ds) 990e66f840cSTristram Ha { 991e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 992e66f840cSTristram Ha struct ksz_port *p; 993e66f840cSTristram Ha u8 remote; 994e66f840cSTristram Ha int i; 995e66f840cSTristram Ha 996e66f840cSTristram Ha /* Switch marks the maximum frame with extra byte as oversize. */ 997e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true); 998e66f840cSTristram Ha ksz_cfg(dev, S_TAIL_TAG_CTRL, SW_TAIL_TAG_ENABLE, true); 999e66f840cSTristram Ha 1000e66f840cSTristram Ha p = &dev->ports[dev->cpu_port]; 1001e66f840cSTristram Ha p->vid_member = dev->port_mask; 1002e66f840cSTristram Ha p->on = 1; 1003e66f840cSTristram Ha 10044b5baca0SMichael Grzeschik ksz8_port_setup(dev, dev->cpu_port, true); 1005e66f840cSTristram Ha dev->member = dev->host_mask; 1006e66f840cSTristram Ha 10074ce2a984SMichael Grzeschik for (i = 0; i < dev->phy_port_cnt; i++) { 1008e66f840cSTristram Ha p = &dev->ports[i]; 1009e66f840cSTristram Ha 1010e66f840cSTristram Ha /* Initialize to non-zero so that ksz_cfg_port_member() will 1011e66f840cSTristram Ha * be called. 1012e66f840cSTristram Ha */ 1013e66f840cSTristram Ha p->vid_member = BIT(i); 1014e66f840cSTristram Ha p->member = dev->port_mask; 10154b5baca0SMichael Grzeschik ksz8_port_stp_state_set(ds, i, BR_STATE_DISABLED); 1016e66f840cSTristram Ha 1017e66f840cSTristram Ha /* Last port may be disabled. */ 10184ce2a984SMichael Grzeschik if (i == dev->phy_port_cnt) 1019e66f840cSTristram Ha break; 1020e66f840cSTristram Ha p->on = 1; 1021e66f840cSTristram Ha p->phy = 1; 1022e66f840cSTristram Ha } 1023e66f840cSTristram Ha for (i = 0; i < dev->phy_port_cnt; i++) { 1024e66f840cSTristram Ha p = &dev->ports[i]; 1025e66f840cSTristram Ha if (!p->on) 1026e66f840cSTristram Ha continue; 1027e66f840cSTristram Ha ksz_pread8(dev, i, P_REMOTE_STATUS, &remote); 1028e66f840cSTristram Ha if (remote & PORT_FIBER_MODE) 1029e66f840cSTristram Ha p->fiber = 1; 1030e66f840cSTristram Ha if (p->fiber) 1031e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1032e66f840cSTristram Ha true); 1033e66f840cSTristram Ha else 1034e66f840cSTristram Ha ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL, 1035e66f840cSTristram Ha false); 1036e66f840cSTristram Ha } 1037e66f840cSTristram Ha } 1038e66f840cSTristram Ha 10394b5baca0SMichael Grzeschik static int ksz8_setup(struct dsa_switch *ds) 1040e66f840cSTristram Ha { 1041e66f840cSTristram Ha struct ksz_device *dev = ds->priv; 1042e66f840cSTristram Ha struct alu_struct alu; 1043e66f840cSTristram Ha int i, ret = 0; 1044e66f840cSTristram Ha 1045e66f840cSTristram Ha dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table), 1046e66f840cSTristram Ha dev->num_vlans, GFP_KERNEL); 1047e66f840cSTristram Ha if (!dev->vlan_cache) 1048e66f840cSTristram Ha return -ENOMEM; 1049e66f840cSTristram Ha 10504b5baca0SMichael Grzeschik ret = ksz8_reset_switch(dev); 1051e66f840cSTristram Ha if (ret) { 1052e66f840cSTristram Ha dev_err(ds->dev, "failed to reset switch\n"); 1053e66f840cSTristram Ha return ret; 1054e66f840cSTristram Ha } 1055e66f840cSTristram Ha 1056e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true); 1057e66f840cSTristram Ha 1058e66f840cSTristram Ha /* Enable automatic fast aging when link changed detected. */ 1059e66f840cSTristram Ha ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true); 1060e66f840cSTristram Ha 1061e66f840cSTristram Ha /* Enable aggressive back off algorithm in half duplex mode. */ 1062e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1, 1063e66f840cSTristram Ha SW_AGGR_BACKOFF, SW_AGGR_BACKOFF); 1064e66f840cSTristram Ha 1065e66f840cSTristram Ha /* 1066e66f840cSTristram Ha * Make sure unicast VLAN boundary is set as default and 1067e66f840cSTristram Ha * enable no excessive collision drop. 1068e66f840cSTristram Ha */ 1069e66f840cSTristram Ha regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2, 1070e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP, 1071e66f840cSTristram Ha UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP); 1072e66f840cSTristram Ha 10734b5baca0SMichael Grzeschik ksz8_config_cpu_port(ds); 1074e66f840cSTristram Ha 1075e66f840cSTristram Ha ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true); 1076e66f840cSTristram Ha 1077e66f840cSTristram Ha ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false); 1078e66f840cSTristram Ha 1079e66f840cSTristram Ha ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false); 1080e66f840cSTristram Ha 1081e66f840cSTristram Ha /* set broadcast storm protection 10% rate */ 1082e66f840cSTristram Ha regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL, 1083e66f840cSTristram Ha BROADCAST_STORM_RATE, 1084e66f840cSTristram Ha (BROADCAST_STORM_VALUE * 1085e66f840cSTristram Ha BROADCAST_STORM_PROT_RATE) / 100); 1086e66f840cSTristram Ha 108702ffbb02SMichael Grzeschik for (i = 0; i < (dev->num_vlans / 4); i++) 10884b5baca0SMichael Grzeschik ksz8_r_vlan_entries(dev, i); 1089e66f840cSTristram Ha 1090e66f840cSTristram Ha /* Setup STP address for STP operation. */ 1091e66f840cSTristram Ha memset(&alu, 0, sizeof(alu)); 1092e66f840cSTristram Ha ether_addr_copy(alu.mac, eth_stp_addr); 1093e66f840cSTristram Ha alu.is_static = true; 1094e66f840cSTristram Ha alu.is_override = true; 1095e66f840cSTristram Ha alu.port_forward = dev->host_mask; 1096e66f840cSTristram Ha 10974b5baca0SMichael Grzeschik ksz8_w_sta_mac_table(dev, 0, &alu); 1098e66f840cSTristram Ha 1099e66f840cSTristram Ha ksz_init_mib_timer(dev); 1100e66f840cSTristram Ha 11010ee2af4eSVladimir Oltean ds->configure_vlan_while_not_filtering = false; 11020ee2af4eSVladimir Oltean 1103e66f840cSTristram Ha return 0; 1104e66f840cSTristram Ha } 1105e66f840cSTristram Ha 11064b5baca0SMichael Grzeschik static const struct dsa_switch_ops ksz8_switch_ops = { 11074b5baca0SMichael Grzeschik .get_tag_protocol = ksz8_get_tag_protocol, 11084b5baca0SMichael Grzeschik .setup = ksz8_setup, 1109e66f840cSTristram Ha .phy_read = ksz_phy_read16, 1110e66f840cSTristram Ha .phy_write = ksz_phy_write16, 1111143a102eSCodrin Ciubotariu .phylink_mac_link_down = ksz_mac_link_down, 1112e66f840cSTristram Ha .port_enable = ksz_enable_port, 11134b5baca0SMichael Grzeschik .get_strings = ksz8_get_strings, 1114e66f840cSTristram Ha .get_ethtool_stats = ksz_get_ethtool_stats, 1115e66f840cSTristram Ha .get_sset_count = ksz_sset_count, 1116e66f840cSTristram Ha .port_bridge_join = ksz_port_bridge_join, 1117e66f840cSTristram Ha .port_bridge_leave = ksz_port_bridge_leave, 11184b5baca0SMichael Grzeschik .port_stp_state_set = ksz8_port_stp_state_set, 1119e66f840cSTristram Ha .port_fast_age = ksz_port_fast_age, 11204b5baca0SMichael Grzeschik .port_vlan_filtering = ksz8_port_vlan_filtering, 11214b5baca0SMichael Grzeschik .port_vlan_add = ksz8_port_vlan_add, 11224b5baca0SMichael Grzeschik .port_vlan_del = ksz8_port_vlan_del, 1123e66f840cSTristram Ha .port_fdb_dump = ksz_port_fdb_dump, 1124e66f840cSTristram Ha .port_mdb_add = ksz_port_mdb_add, 1125e66f840cSTristram Ha .port_mdb_del = ksz_port_mdb_del, 11264b5baca0SMichael Grzeschik .port_mirror_add = ksz8_port_mirror_add, 11274b5baca0SMichael Grzeschik .port_mirror_del = ksz8_port_mirror_del, 1128e66f840cSTristram Ha }; 1129e66f840cSTristram Ha 11304b5baca0SMichael Grzeschik static u32 ksz8_get_port_addr(int port, int offset) 1131e66f840cSTristram Ha { 1132e66f840cSTristram Ha return PORT_CTRL_ADDR(port, offset); 1133e66f840cSTristram Ha } 1134e66f840cSTristram Ha 11354b5baca0SMichael Grzeschik static int ksz8_switch_detect(struct ksz_device *dev) 1136e66f840cSTristram Ha { 1137e66f840cSTristram Ha u8 id1, id2; 1138e66f840cSTristram Ha u16 id16; 1139e66f840cSTristram Ha int ret; 1140e66f840cSTristram Ha 1141e66f840cSTristram Ha /* read chip id */ 1142e66f840cSTristram Ha ret = ksz_read16(dev, REG_CHIP_ID0, &id16); 1143e66f840cSTristram Ha if (ret) 1144e66f840cSTristram Ha return ret; 1145e66f840cSTristram Ha 1146e66f840cSTristram Ha id1 = id16 >> 8; 1147e66f840cSTristram Ha id2 = id16 & SW_CHIP_ID_M; 1148e66f840cSTristram Ha if (id1 != FAMILY_ID || 1149e66f840cSTristram Ha (id2 != CHIP_ID_94 && id2 != CHIP_ID_95)) 1150e66f840cSTristram Ha return -ENODEV; 1151e66f840cSTristram Ha 1152e66f840cSTristram Ha if (id2 == CHIP_ID_95) { 1153e66f840cSTristram Ha u8 val; 1154e66f840cSTristram Ha 1155e66f840cSTristram Ha id2 = 0x95; 1156e66f840cSTristram Ha ksz_read8(dev, REG_PORT_1_STATUS_0, &val); 1157e66f840cSTristram Ha if (val & PORT_FIBER_MODE) 1158e66f840cSTristram Ha id2 = 0x65; 1159e66f840cSTristram Ha } else if (id2 == CHIP_ID_94) { 1160e66f840cSTristram Ha id2 = 0x94; 1161e66f840cSTristram Ha } 1162e66f840cSTristram Ha id16 &= ~0xff; 1163e66f840cSTristram Ha id16 |= id2; 1164e66f840cSTristram Ha dev->chip_id = id16; 1165e66f840cSTristram Ha 1166e66f840cSTristram Ha return 0; 1167e66f840cSTristram Ha } 1168e66f840cSTristram Ha 1169e66f840cSTristram Ha struct ksz_chip_data { 1170e66f840cSTristram Ha u16 chip_id; 1171e66f840cSTristram Ha const char *dev_name; 1172e66f840cSTristram Ha int num_vlans; 1173e66f840cSTristram Ha int num_alus; 1174e66f840cSTristram Ha int num_statics; 1175e66f840cSTristram Ha int cpu_ports; 1176e66f840cSTristram Ha int port_cnt; 1177e66f840cSTristram Ha }; 1178e66f840cSTristram Ha 11794b5baca0SMichael Grzeschik static const struct ksz_chip_data ksz8_switch_chips[] = { 1180e66f840cSTristram Ha { 1181e66f840cSTristram Ha .chip_id = 0x8795, 1182e66f840cSTristram Ha .dev_name = "KSZ8795", 1183e66f840cSTristram Ha .num_vlans = 4096, 1184e66f840cSTristram Ha .num_alus = 0, 1185e66f840cSTristram Ha .num_statics = 8, 1186e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 118794374dd1SMichael Grzeschik .port_cnt = 5, /* total cpu and user ports */ 1188e66f840cSTristram Ha }, 1189e66f840cSTristram Ha { 1190c369d7fcSMarek Vasut /* 1191c369d7fcSMarek Vasut * WARNING 1192c369d7fcSMarek Vasut * ======= 1193c369d7fcSMarek Vasut * KSZ8794 is similar to KSZ8795, except the port map 1194c369d7fcSMarek Vasut * contains a gap between external and CPU ports, the 1195c369d7fcSMarek Vasut * port map is NOT continuous. The per-port register 1196c369d7fcSMarek Vasut * map is shifted accordingly too, i.e. registers at 1197c369d7fcSMarek Vasut * offset 0x40 are NOT used on KSZ8794 and they ARE 1198c369d7fcSMarek Vasut * used on KSZ8795 for external port 3. 1199c369d7fcSMarek Vasut * external cpu 1200c369d7fcSMarek Vasut * KSZ8794 0,1,2 4 1201c369d7fcSMarek Vasut * KSZ8795 0,1,2,3 4 1202c369d7fcSMarek Vasut * KSZ8765 0,1,2,3 4 1203c369d7fcSMarek Vasut */ 1204e66f840cSTristram Ha .chip_id = 0x8794, 1205e66f840cSTristram Ha .dev_name = "KSZ8794", 1206e66f840cSTristram Ha .num_vlans = 4096, 1207e66f840cSTristram Ha .num_alus = 0, 1208e66f840cSTristram Ha .num_statics = 8, 1209e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 121094374dd1SMichael Grzeschik .port_cnt = 4, /* total cpu and user ports */ 1211e66f840cSTristram Ha }, 1212e66f840cSTristram Ha { 1213e66f840cSTristram Ha .chip_id = 0x8765, 1214e66f840cSTristram Ha .dev_name = "KSZ8765", 1215e66f840cSTristram Ha .num_vlans = 4096, 1216e66f840cSTristram Ha .num_alus = 0, 1217e66f840cSTristram Ha .num_statics = 8, 1218e66f840cSTristram Ha .cpu_ports = 0x10, /* can be configured as cpu port */ 121994374dd1SMichael Grzeschik .port_cnt = 5, /* total cpu and user ports */ 1220e66f840cSTristram Ha }, 1221e66f840cSTristram Ha }; 1222e66f840cSTristram Ha 12234b5baca0SMichael Grzeschik static int ksz8_switch_init(struct ksz_device *dev) 1224e66f840cSTristram Ha { 1225e66f840cSTristram Ha int i; 1226e66f840cSTristram Ha 12274b5baca0SMichael Grzeschik dev->ds->ops = &ksz8_switch_ops; 1228e66f840cSTristram Ha 12294b5baca0SMichael Grzeschik for (i = 0; i < ARRAY_SIZE(ksz8_switch_chips); i++) { 12304b5baca0SMichael Grzeschik const struct ksz_chip_data *chip = &ksz8_switch_chips[i]; 1231e66f840cSTristram Ha 1232e66f840cSTristram Ha if (dev->chip_id == chip->chip_id) { 1233e66f840cSTristram Ha dev->name = chip->dev_name; 1234e66f840cSTristram Ha dev->num_vlans = chip->num_vlans; 1235e66f840cSTristram Ha dev->num_alus = chip->num_alus; 1236e66f840cSTristram Ha dev->num_statics = chip->num_statics; 1237c369d7fcSMarek Vasut dev->port_cnt = fls(chip->cpu_ports); 1238c369d7fcSMarek Vasut dev->cpu_port = fls(chip->cpu_ports) - 1; 1239c369d7fcSMarek Vasut dev->phy_port_cnt = dev->port_cnt - 1; 1240e66f840cSTristram Ha dev->cpu_ports = chip->cpu_ports; 1241c369d7fcSMarek Vasut dev->host_mask = chip->cpu_ports; 1242c369d7fcSMarek Vasut dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | 1243c369d7fcSMarek Vasut chip->cpu_ports; 1244e66f840cSTristram Ha break; 1245e66f840cSTristram Ha } 1246e66f840cSTristram Ha } 1247e66f840cSTristram Ha 1248e66f840cSTristram Ha /* no switch found */ 1249e66f840cSTristram Ha if (!dev->cpu_ports) 1250e66f840cSTristram Ha return -ENODEV; 1251e66f840cSTristram Ha 125231b62c78SMichael Grzeschik dev->reg_mib_cnt = KSZ8795_COUNTER_NUM; 125365fe1acfSMichael Grzeschik dev->mib_cnt = ARRAY_SIZE(mib_names); 1254e66f840cSTristram Ha 1255c9f4633bSMichael Grzeschik dev->ports = devm_kzalloc(dev->dev, 1256c9f4633bSMichael Grzeschik dev->port_cnt * sizeof(struct ksz_port), 1257e66f840cSTristram Ha GFP_KERNEL); 1258e66f840cSTristram Ha if (!dev->ports) 1259e66f840cSTristram Ha return -ENOMEM; 1260c9f4633bSMichael Grzeschik for (i = 0; i < dev->port_cnt; i++) { 1261e66f840cSTristram Ha mutex_init(&dev->ports[i].mib.cnt_mutex); 1262e66f840cSTristram Ha dev->ports[i].mib.counters = 1263e66f840cSTristram Ha devm_kzalloc(dev->dev, 1264e66f840cSTristram Ha sizeof(u64) * 126565fe1acfSMichael Grzeschik (dev->mib_cnt + 1), 1266e66f840cSTristram Ha GFP_KERNEL); 1267e66f840cSTristram Ha if (!dev->ports[i].mib.counters) 1268e66f840cSTristram Ha return -ENOMEM; 1269e66f840cSTristram Ha } 1270e66f840cSTristram Ha 1271af199a1aSCodrin Ciubotariu /* set the real number of ports */ 127294374dd1SMichael Grzeschik dev->ds->num_ports = dev->port_cnt; 1273af199a1aSCodrin Ciubotariu 1274e66f840cSTristram Ha return 0; 1275e66f840cSTristram Ha } 1276e66f840cSTristram Ha 12774b5baca0SMichael Grzeschik static void ksz8_switch_exit(struct ksz_device *dev) 1278e66f840cSTristram Ha { 12794b5baca0SMichael Grzeschik ksz8_reset_switch(dev); 1280e66f840cSTristram Ha } 1281e66f840cSTristram Ha 12824b5baca0SMichael Grzeschik static const struct ksz_dev_ops ksz8_dev_ops = { 12834b5baca0SMichael Grzeschik .get_port_addr = ksz8_get_port_addr, 12844b5baca0SMichael Grzeschik .cfg_port_member = ksz8_cfg_port_member, 12854b5baca0SMichael Grzeschik .flush_dyn_mac_table = ksz8_flush_dyn_mac_table, 12864b5baca0SMichael Grzeschik .port_setup = ksz8_port_setup, 12874b5baca0SMichael Grzeschik .r_phy = ksz8_r_phy, 12884b5baca0SMichael Grzeschik .w_phy = ksz8_w_phy, 12894b5baca0SMichael Grzeschik .r_dyn_mac_table = ksz8_r_dyn_mac_table, 12904b5baca0SMichael Grzeschik .r_sta_mac_table = ksz8_r_sta_mac_table, 12914b5baca0SMichael Grzeschik .w_sta_mac_table = ksz8_w_sta_mac_table, 12924b5baca0SMichael Grzeschik .r_mib_cnt = ksz8_r_mib_cnt, 12934b5baca0SMichael Grzeschik .r_mib_pkt = ksz8_r_mib_pkt, 12944b5baca0SMichael Grzeschik .freeze_mib = ksz8_freeze_mib, 12954b5baca0SMichael Grzeschik .port_init_cnt = ksz8_port_init_cnt, 12964b5baca0SMichael Grzeschik .shutdown = ksz8_reset_switch, 12974b5baca0SMichael Grzeschik .detect = ksz8_switch_detect, 12984b5baca0SMichael Grzeschik .init = ksz8_switch_init, 12994b5baca0SMichael Grzeschik .exit = ksz8_switch_exit, 1300e66f840cSTristram Ha }; 1301e66f840cSTristram Ha 13024b5baca0SMichael Grzeschik int ksz8_switch_register(struct ksz_device *dev) 1303e66f840cSTristram Ha { 13044b5baca0SMichael Grzeschik return ksz_switch_register(dev, &ksz8_dev_ops); 1305e66f840cSTristram Ha } 13064b5baca0SMichael Grzeschik EXPORT_SYMBOL(ksz8_switch_register); 1307e66f840cSTristram Ha 1308e66f840cSTristram Ha MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>"); 1309e66f840cSTristram Ha MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver"); 1310e66f840cSTristram Ha MODULE_LICENSE("GPL"); 1311