Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

net: dsa: microchip: break KSZ9477 DSA driver into two files

Break KSZ9477 DSA driver into two files in preparation to add more KSZ
switch drivers.
Add common functions in ksz_common.h so that other KSZ switch drivers
can access code in ksz_common.c.
Add ksz_spi.h for common functions used by KSZ switch SPI drivers.

Signed-off-by: Tristram Ha <Tristram.Ha@microchip.com>
Reviewed-by: Woojung Huh <Woojung.Huh@microchip.com>
Reviewed-by: Pavel Machek <pavel@ucw.cz>
Reviewed-by: Florian Fainelli <f.fainelli@gmail.com>
Reviewed-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: David S. Miller <davem@davemloft.net>

authored by

Tristram Ha and committed by
David S. Miller
c2e86691 74a7194f

+1926 -1265
+4
drivers/net/dsa/microchip/Kconfig
··· 1 + config NET_DSA_MICROCHIP_KSZ_COMMON 2 + tristate 3 + 1 4 menuconfig NET_DSA_MICROCHIP_KSZ9477 2 5 tristate "Microchip KSZ9477 series switch support" 3 6 depends on NET_DSA 4 7 select NET_DSA_TAG_KSZ 8 + select NET_DSA_MICROCHIP_KSZ_COMMON 5 9 help 6 10 This driver adds support for Microchip KSZ9477 switch chips. 7 11
+2 -1
drivers/net/dsa/microchip/Makefile
··· 1 - obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477) += ksz_common.o 1 + obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON) += ksz_common.o 2 + obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477) += ksz9477.o 2 3 obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ9477_SPI) += ksz9477_spi.o
+1316
drivers/net/dsa/microchip/ksz9477.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* 3 + * Microchip KSZ9477 switch driver main logic 4 + * 5 + * Copyright (C) 2017-2018 Microchip Technology Inc. 6 + */ 7 + 8 + #include <linux/delay.h> 9 + #include <linux/export.h> 10 + #include <linux/gpio.h> 11 + #include <linux/kernel.h> 12 + #include <linux/module.h> 13 + #include <linux/platform_data/microchip-ksz.h> 14 + #include <linux/phy.h> 15 + #include <linux/etherdevice.h> 16 + #include <linux/if_bridge.h> 17 + #include <net/dsa.h> 18 + #include <net/switchdev.h> 19 + 20 + #include "ksz_priv.h" 21 + #include "ksz_common.h" 22 + #include "ksz_9477_reg.h" 23 + 24 + static const struct { 25 + int index; 26 + char string[ETH_GSTRING_LEN]; 27 + } ksz9477_mib_names[TOTAL_SWITCH_COUNTER_NUM] = { 28 + { 0x00, "rx_hi" }, 29 + { 0x01, "rx_undersize" }, 30 + { 0x02, "rx_fragments" }, 31 + { 0x03, "rx_oversize" }, 32 + { 0x04, "rx_jabbers" }, 33 + { 0x05, "rx_symbol_err" }, 34 + { 0x06, "rx_crc_err" }, 35 + { 0x07, "rx_align_err" }, 36 + { 0x08, "rx_mac_ctrl" }, 37 + { 0x09, "rx_pause" }, 38 + { 0x0A, "rx_bcast" }, 39 + { 0x0B, "rx_mcast" }, 40 + { 0x0C, "rx_ucast" }, 41 + { 0x0D, "rx_64_or_less" }, 42 + { 0x0E, "rx_65_127" }, 43 + { 0x0F, "rx_128_255" }, 44 + { 0x10, "rx_256_511" }, 45 + { 0x11, "rx_512_1023" }, 46 + { 0x12, "rx_1024_1522" }, 47 + { 0x13, "rx_1523_2000" }, 48 + { 0x14, "rx_2001" }, 49 + { 0x15, "tx_hi" }, 50 + { 0x16, "tx_late_col" }, 51 + { 0x17, "tx_pause" }, 52 + { 0x18, "tx_bcast" }, 53 + { 0x19, "tx_mcast" }, 54 + { 0x1A, "tx_ucast" }, 55 + { 0x1B, "tx_deferred" }, 56 + { 0x1C, "tx_total_col" }, 57 + { 0x1D, "tx_exc_col" }, 58 + { 0x1E, "tx_single_col" }, 59 + { 0x1F, "tx_mult_col" }, 60 + { 0x80, "rx_total" }, 61 + { 0x81, "tx_total" }, 62 + { 0x82, "rx_discards" }, 63 + { 0x83, "tx_discards" }, 64 + }; 65 + 66 + static void ksz9477_cfg32(struct ksz_device *dev, u32 addr, u32 bits, bool set) 67 + { 68 + u32 data; 69 + 70 + ksz_read32(dev, addr, &data); 71 + if (set) 72 + data |= bits; 73 + else 74 + data &= ~bits; 75 + ksz_write32(dev, addr, data); 76 + } 77 + 78 + static void ksz9477_port_cfg32(struct ksz_device *dev, int port, int offset, 79 + u32 bits, bool set) 80 + { 81 + u32 addr; 82 + u32 data; 83 + 84 + addr = PORT_CTRL_ADDR(port, offset); 85 + ksz_read32(dev, addr, &data); 86 + 87 + if (set) 88 + data |= bits; 89 + else 90 + data &= ~bits; 91 + 92 + ksz_write32(dev, addr, data); 93 + } 94 + 95 + static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev, u32 waiton, 96 + int timeout) 97 + { 98 + u8 data; 99 + 100 + do { 101 + ksz_read8(dev, REG_SW_VLAN_CTRL, &data); 102 + if (!(data & waiton)) 103 + break; 104 + usleep_range(1, 10); 105 + } while (timeout-- > 0); 106 + 107 + if (timeout <= 0) 108 + return -ETIMEDOUT; 109 + 110 + return 0; 111 + } 112 + 113 + static int ksz9477_get_vlan_table(struct ksz_device *dev, u16 vid, 114 + u32 *vlan_table) 115 + { 116 + int ret; 117 + 118 + mutex_lock(&dev->vlan_mutex); 119 + 120 + ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M); 121 + ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START); 122 + 123 + /* wait to be cleared */ 124 + ret = ksz9477_wait_vlan_ctrl_ready(dev, VLAN_START, 1000); 125 + if (ret < 0) { 126 + dev_dbg(dev->dev, "Failed to read vlan table\n"); 127 + goto exit; 128 + } 129 + 130 + ksz_read32(dev, REG_SW_VLAN_ENTRY__4, &vlan_table[0]); 131 + ksz_read32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, &vlan_table[1]); 132 + ksz_read32(dev, REG_SW_VLAN_ENTRY_PORTS__4, &vlan_table[2]); 133 + 134 + ksz_write8(dev, REG_SW_VLAN_CTRL, 0); 135 + 136 + exit: 137 + mutex_unlock(&dev->vlan_mutex); 138 + 139 + return ret; 140 + } 141 + 142 + static int ksz9477_set_vlan_table(struct ksz_device *dev, u16 vid, 143 + u32 *vlan_table) 144 + { 145 + int ret; 146 + 147 + mutex_lock(&dev->vlan_mutex); 148 + 149 + ksz_write32(dev, REG_SW_VLAN_ENTRY__4, vlan_table[0]); 150 + ksz_write32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, vlan_table[1]); 151 + ksz_write32(dev, REG_SW_VLAN_ENTRY_PORTS__4, vlan_table[2]); 152 + 153 + ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M); 154 + ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE); 155 + 156 + /* wait to be cleared */ 157 + ret = ksz9477_wait_vlan_ctrl_ready(dev, VLAN_START, 1000); 158 + if (ret < 0) { 159 + dev_dbg(dev->dev, "Failed to write vlan table\n"); 160 + goto exit; 161 + } 162 + 163 + ksz_write8(dev, REG_SW_VLAN_CTRL, 0); 164 + 165 + /* update vlan cache table */ 166 + dev->vlan_cache[vid].table[0] = vlan_table[0]; 167 + dev->vlan_cache[vid].table[1] = vlan_table[1]; 168 + dev->vlan_cache[vid].table[2] = vlan_table[2]; 169 + 170 + exit: 171 + mutex_unlock(&dev->vlan_mutex); 172 + 173 + return ret; 174 + } 175 + 176 + static void ksz9477_read_table(struct ksz_device *dev, u32 *table) 177 + { 178 + ksz_read32(dev, REG_SW_ALU_VAL_A, &table[0]); 179 + ksz_read32(dev, REG_SW_ALU_VAL_B, &table[1]); 180 + ksz_read32(dev, REG_SW_ALU_VAL_C, &table[2]); 181 + ksz_read32(dev, REG_SW_ALU_VAL_D, &table[3]); 182 + } 183 + 184 + static void ksz9477_write_table(struct ksz_device *dev, u32 *table) 185 + { 186 + ksz_write32(dev, REG_SW_ALU_VAL_A, table[0]); 187 + ksz_write32(dev, REG_SW_ALU_VAL_B, table[1]); 188 + ksz_write32(dev, REG_SW_ALU_VAL_C, table[2]); 189 + ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]); 190 + } 191 + 192 + static int ksz9477_wait_alu_ready(struct ksz_device *dev, u32 waiton, 193 + int timeout) 194 + { 195 + u32 data; 196 + 197 + do { 198 + ksz_read32(dev, REG_SW_ALU_CTRL__4, &data); 199 + if (!(data & waiton)) 200 + break; 201 + usleep_range(1, 10); 202 + } while (timeout-- > 0); 203 + 204 + if (timeout <= 0) 205 + return -ETIMEDOUT; 206 + 207 + return 0; 208 + } 209 + 210 + static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev, u32 waiton, 211 + int timeout) 212 + { 213 + u32 data; 214 + 215 + do { 216 + ksz_read32(dev, REG_SW_ALU_STAT_CTRL__4, &data); 217 + if (!(data & waiton)) 218 + break; 219 + usleep_range(1, 10); 220 + } while (timeout-- > 0); 221 + 222 + if (timeout <= 0) 223 + return -ETIMEDOUT; 224 + 225 + return 0; 226 + } 227 + 228 + static int ksz9477_reset_switch(struct ksz_device *dev) 229 + { 230 + u8 data8; 231 + u16 data16; 232 + u32 data32; 233 + 234 + /* reset switch */ 235 + ksz_cfg(dev, REG_SW_OPERATION, SW_RESET, true); 236 + 237 + /* turn off SPI DO Edge select */ 238 + ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8); 239 + data8 &= ~SPI_AUTO_EDGE_DETECTION; 240 + ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8); 241 + 242 + /* default configuration */ 243 + ksz_read8(dev, REG_SW_LUE_CTRL_1, &data8); 244 + data8 = SW_AGING_ENABLE | SW_LINK_AUTO_AGING | 245 + SW_SRC_ADDR_FILTER | SW_FLUSH_STP_TABLE | SW_FLUSH_MSTP_TABLE; 246 + ksz_write8(dev, REG_SW_LUE_CTRL_1, data8); 247 + 248 + /* disable interrupts */ 249 + ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK); 250 + ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0x7F); 251 + ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32); 252 + 253 + /* set broadcast storm protection 10% rate */ 254 + ksz_read16(dev, REG_SW_MAC_CTRL_2, &data16); 255 + data16 &= ~BROADCAST_STORM_RATE; 256 + data16 |= (BROADCAST_STORM_VALUE * BROADCAST_STORM_PROT_RATE) / 100; 257 + ksz_write16(dev, REG_SW_MAC_CTRL_2, data16); 258 + 259 + return 0; 260 + } 261 + 262 + static enum dsa_tag_protocol ksz9477_get_tag_protocol(struct dsa_switch *ds, 263 + int port) 264 + { 265 + return DSA_TAG_PROTO_KSZ; 266 + } 267 + 268 + static int ksz9477_phy_read16(struct dsa_switch *ds, int addr, int reg) 269 + { 270 + struct ksz_device *dev = ds->priv; 271 + u16 val = 0xffff; 272 + 273 + /* No real PHY after this. Simulate the PHY. 274 + * A fixed PHY can be setup in the device tree, but this function is 275 + * still called for that port during initialization. 276 + * For RGMII PHY there is no way to access it so the fixed PHY should 277 + * be used. For SGMII PHY the supporting code will be added later. 278 + */ 279 + if (addr >= dev->phy_port_cnt) { 280 + struct ksz_port *p = &dev->ports[addr]; 281 + 282 + switch (reg) { 283 + case MII_BMCR: 284 + val = 0x1140; 285 + break; 286 + case MII_BMSR: 287 + val = 0x796d; 288 + break; 289 + case MII_PHYSID1: 290 + val = 0x0022; 291 + break; 292 + case MII_PHYSID2: 293 + val = 0x1631; 294 + break; 295 + case MII_ADVERTISE: 296 + val = 0x05e1; 297 + break; 298 + case MII_LPA: 299 + val = 0xc5e1; 300 + break; 301 + case MII_CTRL1000: 302 + val = 0x0700; 303 + break; 304 + case MII_STAT1000: 305 + if (p->phydev.speed == SPEED_1000) 306 + val = 0x3800; 307 + else 308 + val = 0; 309 + break; 310 + } 311 + } else { 312 + ksz_pread16(dev, addr, 0x100 + (reg << 1), &val); 313 + } 314 + 315 + return val; 316 + } 317 + 318 + static int ksz9477_phy_write16(struct dsa_switch *ds, int addr, int reg, 319 + u16 val) 320 + { 321 + struct ksz_device *dev = ds->priv; 322 + 323 + /* No real PHY after this. */ 324 + if (addr >= dev->phy_port_cnt) 325 + return 0; 326 + ksz_pwrite16(dev, addr, 0x100 + (reg << 1), val); 327 + 328 + return 0; 329 + } 330 + 331 + static void ksz9477_get_strings(struct dsa_switch *ds, int port, 332 + u32 stringset, uint8_t *buf) 333 + { 334 + int i; 335 + 336 + if (stringset != ETH_SS_STATS) 337 + return; 338 + 339 + for (i = 0; i < TOTAL_SWITCH_COUNTER_NUM; i++) { 340 + memcpy(buf + i * ETH_GSTRING_LEN, ksz9477_mib_names[i].string, 341 + ETH_GSTRING_LEN); 342 + } 343 + } 344 + 345 + static void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, 346 + uint64_t *buf) 347 + { 348 + struct ksz_device *dev = ds->priv; 349 + int i; 350 + u32 data; 351 + int timeout; 352 + 353 + mutex_lock(&dev->stats_mutex); 354 + 355 + for (i = 0; i < TOTAL_SWITCH_COUNTER_NUM; i++) { 356 + data = MIB_COUNTER_READ; 357 + data |= ((ksz9477_mib_names[i].index & 0xFF) << 358 + MIB_COUNTER_INDEX_S); 359 + ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, data); 360 + 361 + timeout = 1000; 362 + do { 363 + ksz_pread32(dev, port, REG_PORT_MIB_CTRL_STAT__4, 364 + &data); 365 + usleep_range(1, 10); 366 + if (!(data & MIB_COUNTER_READ)) 367 + break; 368 + } while (timeout-- > 0); 369 + 370 + /* failed to read MIB. get out of loop */ 371 + if (!timeout) { 372 + dev_dbg(dev->dev, "Failed to get MIB\n"); 373 + break; 374 + } 375 + 376 + /* count resets upon read */ 377 + ksz_pread32(dev, port, REG_PORT_MIB_DATA, &data); 378 + 379 + dev->mib_value[i] += (uint64_t)data; 380 + buf[i] = dev->mib_value[i]; 381 + } 382 + 383 + mutex_unlock(&dev->stats_mutex); 384 + } 385 + 386 + static void ksz9477_cfg_port_member(struct ksz_device *dev, int port, 387 + u8 member) 388 + { 389 + ksz_pwrite32(dev, port, REG_PORT_VLAN_MEMBERSHIP__4, member); 390 + dev->ports[port].member = member; 391 + } 392 + 393 + static void ksz9477_port_stp_state_set(struct dsa_switch *ds, int port, 394 + u8 state) 395 + { 396 + struct ksz_device *dev = ds->priv; 397 + struct ksz_port *p = &dev->ports[port]; 398 + u8 data; 399 + int member = -1; 400 + 401 + ksz_pread8(dev, port, P_STP_CTRL, &data); 402 + data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE); 403 + 404 + switch (state) { 405 + case BR_STATE_DISABLED: 406 + data |= PORT_LEARN_DISABLE; 407 + if (port != dev->cpu_port) 408 + member = 0; 409 + break; 410 + case BR_STATE_LISTENING: 411 + data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE); 412 + if (port != dev->cpu_port && 413 + p->stp_state == BR_STATE_DISABLED) 414 + member = dev->host_mask | p->vid_member; 415 + break; 416 + case BR_STATE_LEARNING: 417 + data |= PORT_RX_ENABLE; 418 + break; 419 + case BR_STATE_FORWARDING: 420 + data |= (PORT_TX_ENABLE | PORT_RX_ENABLE); 421 + 422 + /* This function is also used internally. */ 423 + if (port == dev->cpu_port) 424 + break; 425 + 426 + member = dev->host_mask | p->vid_member; 427 + 428 + /* Port is a member of a bridge. */ 429 + if (dev->br_member & (1 << port)) { 430 + dev->member |= (1 << port); 431 + member = dev->member; 432 + } 433 + break; 434 + case BR_STATE_BLOCKING: 435 + data |= PORT_LEARN_DISABLE; 436 + if (port != dev->cpu_port && 437 + p->stp_state == BR_STATE_DISABLED) 438 + member = dev->host_mask | p->vid_member; 439 + break; 440 + default: 441 + dev_err(ds->dev, "invalid STP state: %d\n", state); 442 + return; 443 + } 444 + 445 + ksz_pwrite8(dev, port, P_STP_CTRL, data); 446 + p->stp_state = state; 447 + if (data & PORT_RX_ENABLE) 448 + dev->rx_ports |= (1 << port); 449 + else 450 + dev->rx_ports &= ~(1 << port); 451 + if (data & PORT_TX_ENABLE) 452 + dev->tx_ports |= (1 << port); 453 + else 454 + dev->tx_ports &= ~(1 << port); 455 + 456 + /* Port membership may share register with STP state. */ 457 + if (member >= 0 && member != p->member) 458 + ksz9477_cfg_port_member(dev, port, (u8)member); 459 + 460 + /* Check if forwarding needs to be updated. */ 461 + if (state != BR_STATE_FORWARDING) { 462 + if (dev->br_member & (1 << port)) 463 + dev->member &= ~(1 << port); 464 + } 465 + 466 + /* When topology has changed the function ksz_update_port_member 467 + * should be called to modify port forwarding behavior. However 468 + * as the offload_fwd_mark indication cannot be reported here 469 + * the switch forwarding function is not enabled. 470 + */ 471 + } 472 + 473 + static void ksz9477_flush_dyn_mac_table(struct ksz_device *dev, int port) 474 + { 475 + u8 data; 476 + 477 + ksz_read8(dev, REG_SW_LUE_CTRL_2, &data); 478 + data &= ~(SW_FLUSH_OPTION_M << SW_FLUSH_OPTION_S); 479 + data |= (SW_FLUSH_OPTION_DYN_MAC << SW_FLUSH_OPTION_S); 480 + ksz_write8(dev, REG_SW_LUE_CTRL_2, data); 481 + if (port < dev->mib_port_cnt) { 482 + /* flush individual port */ 483 + ksz_pread8(dev, port, P_STP_CTRL, &data); 484 + if (!(data & PORT_LEARN_DISABLE)) 485 + ksz_pwrite8(dev, port, P_STP_CTRL, 486 + data | PORT_LEARN_DISABLE); 487 + ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true); 488 + ksz_pwrite8(dev, port, P_STP_CTRL, data); 489 + } else { 490 + /* flush all */ 491 + ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_STP_TABLE, true); 492 + } 493 + } 494 + 495 + static int ksz9477_port_vlan_filtering(struct dsa_switch *ds, int port, 496 + bool flag) 497 + { 498 + struct ksz_device *dev = ds->priv; 499 + 500 + if (flag) { 501 + ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL, 502 + PORT_VLAN_LOOKUP_VID_0, true); 503 + ksz9477_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY, 504 + true); 505 + ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, true); 506 + } else { 507 + ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, false); 508 + ksz9477_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY, 509 + false); 510 + ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL, 511 + PORT_VLAN_LOOKUP_VID_0, false); 512 + } 513 + 514 + return 0; 515 + } 516 + 517 + static void ksz9477_port_vlan_add(struct dsa_switch *ds, int port, 518 + const struct switchdev_obj_port_vlan *vlan) 519 + { 520 + struct ksz_device *dev = ds->priv; 521 + u32 vlan_table[3]; 522 + u16 vid; 523 + bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 524 + 525 + for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 526 + if (ksz9477_get_vlan_table(dev, vid, vlan_table)) { 527 + dev_dbg(dev->dev, "Failed to get vlan table\n"); 528 + return; 529 + } 530 + 531 + vlan_table[0] = VLAN_VALID | (vid & VLAN_FID_M); 532 + if (untagged) 533 + vlan_table[1] |= BIT(port); 534 + else 535 + vlan_table[1] &= ~BIT(port); 536 + vlan_table[1] &= ~(BIT(dev->cpu_port)); 537 + 538 + vlan_table[2] |= BIT(port) | BIT(dev->cpu_port); 539 + 540 + if (ksz9477_set_vlan_table(dev, vid, vlan_table)) { 541 + dev_dbg(dev->dev, "Failed to set vlan table\n"); 542 + return; 543 + } 544 + 545 + /* change PVID */ 546 + if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 547 + ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, vid); 548 + } 549 + } 550 + 551 + static int ksz9477_port_vlan_del(struct dsa_switch *ds, int port, 552 + const struct switchdev_obj_port_vlan *vlan) 553 + { 554 + struct ksz_device *dev = ds->priv; 555 + bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 556 + u32 vlan_table[3]; 557 + u16 vid; 558 + u16 pvid; 559 + 560 + ksz_pread16(dev, port, REG_PORT_DEFAULT_VID, &pvid); 561 + pvid = pvid & 0xFFF; 562 + 563 + for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 564 + if (ksz9477_get_vlan_table(dev, vid, vlan_table)) { 565 + dev_dbg(dev->dev, "Failed to get vlan table\n"); 566 + return -ETIMEDOUT; 567 + } 568 + 569 + vlan_table[2] &= ~BIT(port); 570 + 571 + if (pvid == vid) 572 + pvid = 1; 573 + 574 + if (untagged) 575 + vlan_table[1] &= ~BIT(port); 576 + 577 + if (ksz9477_set_vlan_table(dev, vid, vlan_table)) { 578 + dev_dbg(dev->dev, "Failed to set vlan table\n"); 579 + return -ETIMEDOUT; 580 + } 581 + } 582 + 583 + ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, pvid); 584 + 585 + return 0; 586 + } 587 + 588 + static int ksz9477_port_fdb_add(struct dsa_switch *ds, int port, 589 + const unsigned char *addr, u16 vid) 590 + { 591 + struct ksz_device *dev = ds->priv; 592 + u32 alu_table[4]; 593 + u32 data; 594 + int ret = 0; 595 + 596 + mutex_lock(&dev->alu_mutex); 597 + 598 + /* find any entry with mac & vid */ 599 + data = vid << ALU_FID_INDEX_S; 600 + data |= ((addr[0] << 8) | addr[1]); 601 + ksz_write32(dev, REG_SW_ALU_INDEX_0, data); 602 + 603 + data = ((addr[2] << 24) | (addr[3] << 16)); 604 + data |= ((addr[4] << 8) | addr[5]); 605 + ksz_write32(dev, REG_SW_ALU_INDEX_1, data); 606 + 607 + /* start read operation */ 608 + ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START); 609 + 610 + /* wait to be finished */ 611 + ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000); 612 + if (ret < 0) { 613 + dev_dbg(dev->dev, "Failed to read ALU\n"); 614 + goto exit; 615 + } 616 + 617 + /* read ALU entry */ 618 + ksz9477_read_table(dev, alu_table); 619 + 620 + /* update ALU entry */ 621 + alu_table[0] = ALU_V_STATIC_VALID; 622 + alu_table[1] |= BIT(port); 623 + if (vid) 624 + alu_table[1] |= ALU_V_USE_FID; 625 + alu_table[2] = (vid << ALU_V_FID_S); 626 + alu_table[2] |= ((addr[0] << 8) | addr[1]); 627 + alu_table[3] = ((addr[2] << 24) | (addr[3] << 16)); 628 + alu_table[3] |= ((addr[4] << 8) | addr[5]); 629 + 630 + ksz9477_write_table(dev, alu_table); 631 + 632 + ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START); 633 + 634 + /* wait to be finished */ 635 + ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000); 636 + if (ret < 0) 637 + dev_dbg(dev->dev, "Failed to write ALU\n"); 638 + 639 + exit: 640 + mutex_unlock(&dev->alu_mutex); 641 + 642 + return ret; 643 + } 644 + 645 + static int ksz9477_port_fdb_del(struct dsa_switch *ds, int port, 646 + const unsigned char *addr, u16 vid) 647 + { 648 + struct ksz_device *dev = ds->priv; 649 + u32 alu_table[4]; 650 + u32 data; 651 + int ret = 0; 652 + 653 + mutex_lock(&dev->alu_mutex); 654 + 655 + /* read any entry with mac & vid */ 656 + data = vid << ALU_FID_INDEX_S; 657 + data |= ((addr[0] << 8) | addr[1]); 658 + ksz_write32(dev, REG_SW_ALU_INDEX_0, data); 659 + 660 + data = ((addr[2] << 24) | (addr[3] << 16)); 661 + data |= ((addr[4] << 8) | addr[5]); 662 + ksz_write32(dev, REG_SW_ALU_INDEX_1, data); 663 + 664 + /* start read operation */ 665 + ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START); 666 + 667 + /* wait to be finished */ 668 + ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000); 669 + if (ret < 0) { 670 + dev_dbg(dev->dev, "Failed to read ALU\n"); 671 + goto exit; 672 + } 673 + 674 + ksz_read32(dev, REG_SW_ALU_VAL_A, &alu_table[0]); 675 + if (alu_table[0] & ALU_V_STATIC_VALID) { 676 + ksz_read32(dev, REG_SW_ALU_VAL_B, &alu_table[1]); 677 + ksz_read32(dev, REG_SW_ALU_VAL_C, &alu_table[2]); 678 + ksz_read32(dev, REG_SW_ALU_VAL_D, &alu_table[3]); 679 + 680 + /* clear forwarding port */ 681 + alu_table[2] &= ~BIT(port); 682 + 683 + /* if there is no port to forward, clear table */ 684 + if ((alu_table[2] & ALU_V_PORT_MAP) == 0) { 685 + alu_table[0] = 0; 686 + alu_table[1] = 0; 687 + alu_table[2] = 0; 688 + alu_table[3] = 0; 689 + } 690 + } else { 691 + alu_table[0] = 0; 692 + alu_table[1] = 0; 693 + alu_table[2] = 0; 694 + alu_table[3] = 0; 695 + } 696 + 697 + ksz9477_write_table(dev, alu_table); 698 + 699 + ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START); 700 + 701 + /* wait to be finished */ 702 + ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000); 703 + if (ret < 0) 704 + dev_dbg(dev->dev, "Failed to write ALU\n"); 705 + 706 + exit: 707 + mutex_unlock(&dev->alu_mutex); 708 + 709 + return ret; 710 + } 711 + 712 + static void ksz9477_convert_alu(struct alu_struct *alu, u32 *alu_table) 713 + { 714 + alu->is_static = !!(alu_table[0] & ALU_V_STATIC_VALID); 715 + alu->is_src_filter = !!(alu_table[0] & ALU_V_SRC_FILTER); 716 + alu->is_dst_filter = !!(alu_table[0] & ALU_V_DST_FILTER); 717 + alu->prio_age = (alu_table[0] >> ALU_V_PRIO_AGE_CNT_S) & 718 + ALU_V_PRIO_AGE_CNT_M; 719 + alu->mstp = alu_table[0] & ALU_V_MSTP_M; 720 + 721 + alu->is_override = !!(alu_table[1] & ALU_V_OVERRIDE); 722 + alu->is_use_fid = !!(alu_table[1] & ALU_V_USE_FID); 723 + alu->port_forward = alu_table[1] & ALU_V_PORT_MAP; 724 + 725 + alu->fid = (alu_table[2] >> ALU_V_FID_S) & ALU_V_FID_M; 726 + 727 + alu->mac[0] = (alu_table[2] >> 8) & 0xFF; 728 + alu->mac[1] = alu_table[2] & 0xFF; 729 + alu->mac[2] = (alu_table[3] >> 24) & 0xFF; 730 + alu->mac[3] = (alu_table[3] >> 16) & 0xFF; 731 + alu->mac[4] = (alu_table[3] >> 8) & 0xFF; 732 + alu->mac[5] = alu_table[3] & 0xFF; 733 + } 734 + 735 + static int ksz9477_port_fdb_dump(struct dsa_switch *ds, int port, 736 + dsa_fdb_dump_cb_t *cb, void *data) 737 + { 738 + struct ksz_device *dev = ds->priv; 739 + int ret = 0; 740 + u32 ksz_data; 741 + u32 alu_table[4]; 742 + struct alu_struct alu; 743 + int timeout; 744 + 745 + mutex_lock(&dev->alu_mutex); 746 + 747 + /* start ALU search */ 748 + ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_START | ALU_SEARCH); 749 + 750 + do { 751 + timeout = 1000; 752 + do { 753 + ksz_read32(dev, REG_SW_ALU_CTRL__4, &ksz_data); 754 + if ((ksz_data & ALU_VALID) || !(ksz_data & ALU_START)) 755 + break; 756 + usleep_range(1, 10); 757 + } while (timeout-- > 0); 758 + 759 + if (!timeout) { 760 + dev_dbg(dev->dev, "Failed to search ALU\n"); 761 + ret = -ETIMEDOUT; 762 + goto exit; 763 + } 764 + 765 + /* read ALU table */ 766 + ksz9477_read_table(dev, alu_table); 767 + 768 + ksz9477_convert_alu(&alu, alu_table); 769 + 770 + if (alu.port_forward & BIT(port)) { 771 + ret = cb(alu.mac, alu.fid, alu.is_static, data); 772 + if (ret) 773 + goto exit; 774 + } 775 + } while (ksz_data & ALU_START); 776 + 777 + exit: 778 + 779 + /* stop ALU search */ 780 + ksz_write32(dev, REG_SW_ALU_CTRL__4, 0); 781 + 782 + mutex_unlock(&dev->alu_mutex); 783 + 784 + return ret; 785 + } 786 + 787 + static void ksz9477_port_mdb_add(struct dsa_switch *ds, int port, 788 + const struct switchdev_obj_port_mdb *mdb) 789 + { 790 + struct ksz_device *dev = ds->priv; 791 + u32 static_table[4]; 792 + u32 data; 793 + int index; 794 + u32 mac_hi, mac_lo; 795 + 796 + mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]); 797 + mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16)); 798 + mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]); 799 + 800 + mutex_lock(&dev->alu_mutex); 801 + 802 + for (index = 0; index < dev->num_statics; index++) { 803 + /* find empty slot first */ 804 + data = (index << ALU_STAT_INDEX_S) | 805 + ALU_STAT_READ | ALU_STAT_START; 806 + ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data); 807 + 808 + /* wait to be finished */ 809 + if (ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000) < 0) { 810 + dev_dbg(dev->dev, "Failed to read ALU STATIC\n"); 811 + goto exit; 812 + } 813 + 814 + /* read ALU static table */ 815 + ksz9477_read_table(dev, static_table); 816 + 817 + if (static_table[0] & ALU_V_STATIC_VALID) { 818 + /* check this has same vid & mac address */ 819 + if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) && 820 + ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) && 821 + static_table[3] == mac_lo) { 822 + /* found matching one */ 823 + break; 824 + } 825 + } else { 826 + /* found empty one */ 827 + break; 828 + } 829 + } 830 + 831 + /* no available entry */ 832 + if (index == dev->num_statics) 833 + goto exit; 834 + 835 + /* add entry */ 836 + static_table[0] = ALU_V_STATIC_VALID; 837 + static_table[1] |= BIT(port); 838 + if (mdb->vid) 839 + static_table[1] |= ALU_V_USE_FID; 840 + static_table[2] = (mdb->vid << ALU_V_FID_S); 841 + static_table[2] |= mac_hi; 842 + static_table[3] = mac_lo; 843 + 844 + ksz9477_write_table(dev, static_table); 845 + 846 + data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START; 847 + ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data); 848 + 849 + /* wait to be finished */ 850 + if (ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000) < 0) 851 + dev_dbg(dev->dev, "Failed to read ALU STATIC\n"); 852 + 853 + exit: 854 + mutex_unlock(&dev->alu_mutex); 855 + } 856 + 857 + static int ksz9477_port_mdb_del(struct dsa_switch *ds, int port, 858 + const struct switchdev_obj_port_mdb *mdb) 859 + { 860 + struct ksz_device *dev = ds->priv; 861 + u32 static_table[4]; 862 + u32 data; 863 + int index; 864 + int ret = 0; 865 + u32 mac_hi, mac_lo; 866 + 867 + mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]); 868 + mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16)); 869 + mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]); 870 + 871 + mutex_lock(&dev->alu_mutex); 872 + 873 + for (index = 0; index < dev->num_statics; index++) { 874 + /* find empty slot first */ 875 + data = (index << ALU_STAT_INDEX_S) | 876 + ALU_STAT_READ | ALU_STAT_START; 877 + ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data); 878 + 879 + /* wait to be finished */ 880 + ret = ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000); 881 + if (ret < 0) { 882 + dev_dbg(dev->dev, "Failed to read ALU STATIC\n"); 883 + goto exit; 884 + } 885 + 886 + /* read ALU static table */ 887 + ksz9477_read_table(dev, static_table); 888 + 889 + if (static_table[0] & ALU_V_STATIC_VALID) { 890 + /* check this has same vid & mac address */ 891 + 892 + if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) && 893 + ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) && 894 + static_table[3] == mac_lo) { 895 + /* found matching one */ 896 + break; 897 + } 898 + } 899 + } 900 + 901 + /* no available entry */ 902 + if (index == dev->num_statics) 903 + goto exit; 904 + 905 + /* clear port */ 906 + static_table[1] &= ~BIT(port); 907 + 908 + if ((static_table[1] & ALU_V_PORT_MAP) == 0) { 909 + /* delete entry */ 910 + static_table[0] = 0; 911 + static_table[1] = 0; 912 + static_table[2] = 0; 913 + static_table[3] = 0; 914 + } 915 + 916 + ksz9477_write_table(dev, static_table); 917 + 918 + data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START; 919 + ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data); 920 + 921 + /* wait to be finished */ 922 + ret = ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000); 923 + if (ret < 0) 924 + dev_dbg(dev->dev, "Failed to read ALU STATIC\n"); 925 + 926 + exit: 927 + mutex_unlock(&dev->alu_mutex); 928 + 929 + return ret; 930 + } 931 + 932 + static int ksz9477_port_mirror_add(struct dsa_switch *ds, int port, 933 + struct dsa_mall_mirror_tc_entry *mirror, 934 + bool ingress) 935 + { 936 + struct ksz_device *dev = ds->priv; 937 + 938 + if (ingress) 939 + ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true); 940 + else 941 + ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true); 942 + 943 + ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false); 944 + 945 + /* configure mirror port */ 946 + ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 947 + PORT_MIRROR_SNIFFER, true); 948 + 949 + ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false); 950 + 951 + return 0; 952 + } 953 + 954 + static void ksz9477_port_mirror_del(struct dsa_switch *ds, int port, 955 + struct dsa_mall_mirror_tc_entry *mirror) 956 + { 957 + struct ksz_device *dev = ds->priv; 958 + u8 data; 959 + 960 + if (mirror->ingress) 961 + ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false); 962 + else 963 + ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false); 964 + 965 + ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 966 + 967 + if (!(data & (PORT_MIRROR_RX | PORT_MIRROR_TX))) 968 + ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 969 + PORT_MIRROR_SNIFFER, false); 970 + } 971 + 972 + static void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port) 973 + { 974 + u8 data8; 975 + u8 member; 976 + u16 data16; 977 + struct ksz_port *p = &dev->ports[port]; 978 + 979 + /* enable tag tail for host port */ 980 + if (cpu_port) 981 + ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_TAIL_TAG_ENABLE, 982 + true); 983 + 984 + ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_MAC_LOOPBACK, false); 985 + 986 + /* set back pressure */ 987 + ksz_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE, true); 988 + 989 + /* enable broadcast storm limit */ 990 + ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true); 991 + 992 + /* disable DiffServ priority */ 993 + ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_PRIO_ENABLE, false); 994 + 995 + /* replace priority */ 996 + ksz_port_cfg(dev, port, REG_PORT_MRI_MAC_CTRL, PORT_USER_PRIO_CEILING, 997 + false); 998 + ksz9477_port_cfg32(dev, port, REG_PORT_MTI_QUEUE_CTRL_0__4, 999 + MTI_PVID_REPLACE, false); 1000 + 1001 + /* enable 802.1p priority */ 1002 + ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true); 1003 + 1004 + if (port < dev->phy_port_cnt) { 1005 + /* do not force flow control */ 1006 + ksz_port_cfg(dev, port, REG_PORT_CTRL_0, 1007 + PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL, 1008 + false); 1009 + 1010 + } else { 1011 + /* force flow control */ 1012 + ksz_port_cfg(dev, port, REG_PORT_CTRL_0, 1013 + PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL, 1014 + true); 1015 + 1016 + /* configure MAC to 1G & RGMII mode */ 1017 + ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8); 1018 + data8 &= ~PORT_MII_NOT_1GBIT; 1019 + data8 &= ~PORT_MII_SEL_M; 1020 + switch (dev->interface) { 1021 + case PHY_INTERFACE_MODE_MII: 1022 + data8 |= PORT_MII_NOT_1GBIT; 1023 + data8 |= PORT_MII_SEL; 1024 + p->phydev.speed = SPEED_100; 1025 + break; 1026 + case PHY_INTERFACE_MODE_RMII: 1027 + data8 |= PORT_MII_NOT_1GBIT; 1028 + data8 |= PORT_RMII_SEL; 1029 + p->phydev.speed = SPEED_100; 1030 + break; 1031 + case PHY_INTERFACE_MODE_GMII: 1032 + data8 |= PORT_GMII_SEL; 1033 + p->phydev.speed = SPEED_1000; 1034 + break; 1035 + default: 1036 + data8 &= ~PORT_RGMII_ID_IG_ENABLE; 1037 + data8 &= ~PORT_RGMII_ID_EG_ENABLE; 1038 + if (dev->interface == PHY_INTERFACE_MODE_RGMII_ID || 1039 + dev->interface == PHY_INTERFACE_MODE_RGMII_RXID) 1040 + data8 |= PORT_RGMII_ID_IG_ENABLE; 1041 + if (dev->interface == PHY_INTERFACE_MODE_RGMII_ID || 1042 + dev->interface == PHY_INTERFACE_MODE_RGMII_TXID) 1043 + data8 |= PORT_RGMII_ID_EG_ENABLE; 1044 + data8 |= PORT_RGMII_SEL; 1045 + p->phydev.speed = SPEED_1000; 1046 + break; 1047 + } 1048 + ksz_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8); 1049 + p->phydev.duplex = 1; 1050 + } 1051 + if (cpu_port) { 1052 + member = dev->port_mask; 1053 + dev->on_ports = dev->host_mask; 1054 + dev->live_ports = dev->host_mask; 1055 + } else { 1056 + member = dev->host_mask | p->vid_member; 1057 + dev->on_ports |= (1 << port); 1058 + 1059 + /* Link was detected before port is enabled. */ 1060 + if (p->phydev.link) 1061 + dev->live_ports |= (1 << port); 1062 + } 1063 + ksz9477_cfg_port_member(dev, port, member); 1064 + 1065 + /* clear pending interrupts */ 1066 + if (port < dev->phy_port_cnt) 1067 + ksz_pread16(dev, port, REG_PORT_PHY_INT_ENABLE, &data16); 1068 + } 1069 + 1070 + static void ksz9477_config_cpu_port(struct dsa_switch *ds) 1071 + { 1072 + struct ksz_device *dev = ds->priv; 1073 + struct ksz_port *p; 1074 + int i; 1075 + 1076 + ds->num_ports = dev->port_cnt; 1077 + 1078 + for (i = 0; i < dev->port_cnt; i++) { 1079 + if (dsa_is_cpu_port(ds, i) && (dev->cpu_ports & (1 << i))) { 1080 + dev->cpu_port = i; 1081 + dev->host_mask = (1 << dev->cpu_port); 1082 + dev->port_mask |= dev->host_mask; 1083 + 1084 + /* enable cpu port */ 1085 + ksz9477_port_setup(dev, i, true); 1086 + p = &dev->ports[dev->cpu_port]; 1087 + p->vid_member = dev->port_mask; 1088 + p->on = 1; 1089 + } 1090 + } 1091 + 1092 + dev->member = dev->host_mask; 1093 + 1094 + for (i = 0; i < dev->mib_port_cnt; i++) { 1095 + if (i == dev->cpu_port) 1096 + continue; 1097 + p = &dev->ports[i]; 1098 + 1099 + /* Initialize to non-zero so that ksz_cfg_port_member() will 1100 + * be called. 1101 + */ 1102 + p->vid_member = (1 << i); 1103 + p->member = dev->port_mask; 1104 + ksz9477_port_stp_state_set(ds, i, BR_STATE_DISABLED); 1105 + p->on = 1; 1106 + if (i < dev->phy_port_cnt) 1107 + p->phy = 1; 1108 + if (dev->chip_id == 0x00947700 && i == 6) { 1109 + p->sgmii = 1; 1110 + 1111 + /* SGMII PHY detection code is not implemented yet. */ 1112 + p->phy = 0; 1113 + } 1114 + } 1115 + } 1116 + 1117 + static int ksz9477_setup(struct dsa_switch *ds) 1118 + { 1119 + struct ksz_device *dev = ds->priv; 1120 + int ret = 0; 1121 + 1122 + dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table), 1123 + dev->num_vlans, GFP_KERNEL); 1124 + if (!dev->vlan_cache) 1125 + return -ENOMEM; 1126 + 1127 + ret = ksz9477_reset_switch(dev); 1128 + if (ret) { 1129 + dev_err(ds->dev, "failed to reset switch\n"); 1130 + return ret; 1131 + } 1132 + 1133 + /* accept packet up to 2000bytes */ 1134 + ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_LEGAL_PACKET_DISABLE, true); 1135 + 1136 + ksz9477_config_cpu_port(ds); 1137 + 1138 + ksz_cfg(dev, REG_SW_MAC_CTRL_1, MULTICAST_STORM_DISABLE, true); 1139 + 1140 + /* queue based egress rate limit */ 1141 + ksz_cfg(dev, REG_SW_MAC_CTRL_5, SW_OUT_RATE_LIMIT_QUEUE_BASED, true); 1142 + 1143 + /* start switch */ 1144 + ksz_cfg(dev, REG_SW_OPERATION, SW_START, true); 1145 + 1146 + return 0; 1147 + } 1148 + 1149 + static const struct dsa_switch_ops ksz9477_switch_ops = { 1150 + .get_tag_protocol = ksz9477_get_tag_protocol, 1151 + .setup = ksz9477_setup, 1152 + .phy_read = ksz9477_phy_read16, 1153 + .phy_write = ksz9477_phy_write16, 1154 + .port_enable = ksz_enable_port, 1155 + .port_disable = ksz_disable_port, 1156 + .get_strings = ksz9477_get_strings, 1157 + .get_ethtool_stats = ksz_get_ethtool_stats, 1158 + .get_sset_count = ksz_sset_count, 1159 + .port_bridge_join = ksz_port_bridge_join, 1160 + .port_bridge_leave = ksz_port_bridge_leave, 1161 + .port_stp_state_set = ksz9477_port_stp_state_set, 1162 + .port_fast_age = ksz_port_fast_age, 1163 + .port_vlan_filtering = ksz9477_port_vlan_filtering, 1164 + .port_vlan_prepare = ksz_port_vlan_prepare, 1165 + .port_vlan_add = ksz9477_port_vlan_add, 1166 + .port_vlan_del = ksz9477_port_vlan_del, 1167 + .port_fdb_dump = ksz9477_port_fdb_dump, 1168 + .port_fdb_add = ksz9477_port_fdb_add, 1169 + .port_fdb_del = ksz9477_port_fdb_del, 1170 + .port_mdb_prepare = ksz_port_mdb_prepare, 1171 + .port_mdb_add = ksz9477_port_mdb_add, 1172 + .port_mdb_del = ksz9477_port_mdb_del, 1173 + .port_mirror_add = ksz9477_port_mirror_add, 1174 + .port_mirror_del = ksz9477_port_mirror_del, 1175 + }; 1176 + 1177 + static u32 ksz9477_get_port_addr(int port, int offset) 1178 + { 1179 + return PORT_CTRL_ADDR(port, offset); 1180 + } 1181 + 1182 + static int ksz9477_switch_detect(struct ksz_device *dev) 1183 + { 1184 + u8 data8; 1185 + u32 id32; 1186 + int ret; 1187 + 1188 + /* turn off SPI DO Edge select */ 1189 + ret = ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8); 1190 + if (ret) 1191 + return ret; 1192 + 1193 + data8 &= ~SPI_AUTO_EDGE_DETECTION; 1194 + ret = ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8); 1195 + if (ret) 1196 + return ret; 1197 + 1198 + /* read chip id */ 1199 + ret = ksz_read32(dev, REG_CHIP_ID0__1, &id32); 1200 + if (ret) 1201 + return ret; 1202 + 1203 + /* Number of ports can be reduced depending on chip. */ 1204 + dev->mib_port_cnt = TOTAL_PORT_NUM; 1205 + dev->phy_port_cnt = 5; 1206 + 1207 + dev->chip_id = id32; 1208 + 1209 + return 0; 1210 + } 1211 + 1212 + struct ksz_chip_data { 1213 + u32 chip_id; 1214 + const char *dev_name; 1215 + int num_vlans; 1216 + int num_alus; 1217 + int num_statics; 1218 + int cpu_ports; 1219 + int port_cnt; 1220 + }; 1221 + 1222 + static const struct ksz_chip_data ksz9477_switch_chips[] = { 1223 + { 1224 + .chip_id = 0x00947700, 1225 + .dev_name = "KSZ9477", 1226 + .num_vlans = 4096, 1227 + .num_alus = 4096, 1228 + .num_statics = 16, 1229 + .cpu_ports = 0x7F, /* can be configured as cpu port */ 1230 + .port_cnt = 7, /* total physical port count */ 1231 + }, 1232 + { 1233 + .chip_id = 0x00989700, 1234 + .dev_name = "KSZ9897", 1235 + .num_vlans = 4096, 1236 + .num_alus = 4096, 1237 + .num_statics = 16, 1238 + .cpu_ports = 0x7F, /* can be configured as cpu port */ 1239 + .port_cnt = 7, /* total physical port count */ 1240 + }, 1241 + }; 1242 + 1243 + static int ksz9477_switch_init(struct ksz_device *dev) 1244 + { 1245 + int i; 1246 + 1247 + dev->ds->ops = &ksz9477_switch_ops; 1248 + 1249 + for (i = 0; i < ARRAY_SIZE(ksz9477_switch_chips); i++) { 1250 + const struct ksz_chip_data *chip = &ksz9477_switch_chips[i]; 1251 + 1252 + if (dev->chip_id == chip->chip_id) { 1253 + dev->name = chip->dev_name; 1254 + dev->num_vlans = chip->num_vlans; 1255 + dev->num_alus = chip->num_alus; 1256 + dev->num_statics = chip->num_statics; 1257 + dev->port_cnt = chip->port_cnt; 1258 + dev->cpu_ports = chip->cpu_ports; 1259 + 1260 + break; 1261 + } 1262 + } 1263 + 1264 + /* no switch found */ 1265 + if (!dev->port_cnt) 1266 + return -ENODEV; 1267 + 1268 + dev->port_mask = (1 << dev->port_cnt) - 1; 1269 + 1270 + dev->reg_mib_cnt = SWITCH_COUNTER_NUM; 1271 + dev->mib_cnt = TOTAL_SWITCH_COUNTER_NUM; 1272 + 1273 + i = dev->mib_port_cnt; 1274 + dev->ports = devm_kzalloc(dev->dev, sizeof(struct ksz_port) * i, 1275 + GFP_KERNEL); 1276 + if (!dev->ports) 1277 + return -ENOMEM; 1278 + for (i = 0; i < dev->mib_port_cnt; i++) { 1279 + dev->ports[i].mib.counters = 1280 + devm_kzalloc(dev->dev, 1281 + sizeof(u64) * 1282 + (TOTAL_SWITCH_COUNTER_NUM + 1), 1283 + GFP_KERNEL); 1284 + if (!dev->ports[i].mib.counters) 1285 + return -ENOMEM; 1286 + } 1287 + dev->interface = PHY_INTERFACE_MODE_RGMII_TXID; 1288 + 1289 + return 0; 1290 + } 1291 + 1292 + static void ksz9477_switch_exit(struct ksz_device *dev) 1293 + { 1294 + ksz9477_reset_switch(dev); 1295 + } 1296 + 1297 + static const struct ksz_dev_ops ksz9477_dev_ops = { 1298 + .get_port_addr = ksz9477_get_port_addr, 1299 + .cfg_port_member = ksz9477_cfg_port_member, 1300 + .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table, 1301 + .port_setup = ksz9477_port_setup, 1302 + .shutdown = ksz9477_reset_switch, 1303 + .detect = ksz9477_switch_detect, 1304 + .init = ksz9477_switch_init, 1305 + .exit = ksz9477_switch_exit, 1306 + }; 1307 + 1308 + int ksz9477_switch_register(struct ksz_device *dev) 1309 + { 1310 + return ksz_switch_register(dev, &ksz9477_dev_ops); 1311 + } 1312 + EXPORT_SYMBOL(ksz9477_switch_register); 1313 + 1314 + MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>"); 1315 + MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch DSA Driver"); 1316 + MODULE_LICENSE("GPL");
+57 -86
drivers/net/dsa/microchip/ksz9477_spi.c
··· 1 1 // SPDX-License-Identifier: GPL-2.0 2 2 /* 3 - * Microchip KSZ series register access through SPI 3 + * Microchip KSZ9477 series register access through SPI 4 4 * 5 5 * Copyright (C) 2017-2018 Microchip Technology Inc. 6 6 */ ··· 13 13 #include <linux/spi/spi.h> 14 14 15 15 #include "ksz_priv.h" 16 + #include "ksz_spi.h" 16 17 17 18 /* SPI frame opcodes */ 18 19 #define KS_SPIOP_RD 3 ··· 23 22 #define SPI_ADDR_MASK (BIT(SPI_ADDR_SHIFT) - 1) 24 23 #define SPI_TURNAROUND_SHIFT 5 25 24 26 - static int ksz_spi_read_reg(struct spi_device *spi, u32 reg, u8 *val, 27 - unsigned int len) 25 + /* Enough to read all switch port registers. */ 26 + #define SPI_TX_BUF_LEN 0x100 27 + 28 + static int ksz9477_spi_read_reg(struct spi_device *spi, u32 reg, u8 *val, 29 + unsigned int len) 28 30 { 29 31 u32 txbuf; 30 32 int ret; ··· 41 37 return ret; 42 38 } 43 39 40 + static int ksz9477_spi_write_reg(struct spi_device *spi, u32 reg, u8 *val, 41 + unsigned int len) 42 + { 43 + u32 *txbuf = (u32 *)val; 44 + 45 + *txbuf = reg & SPI_ADDR_MASK; 46 + *txbuf |= (KS_SPIOP_WR << SPI_ADDR_SHIFT); 47 + *txbuf <<= SPI_TURNAROUND_SHIFT; 48 + *txbuf = cpu_to_be32(*txbuf); 49 + 50 + return spi_write(spi, txbuf, 4 + len); 51 + } 52 + 44 53 static int ksz_spi_read(struct ksz_device *dev, u32 reg, u8 *data, 45 54 unsigned int len) 46 55 { 47 56 struct spi_device *spi = dev->priv; 48 57 49 - return ksz_spi_read_reg(spi, reg, data, len); 58 + return ksz9477_spi_read_reg(spi, reg, data, len); 50 59 } 51 60 52 - static int ksz_spi_read8(struct ksz_device *dev, u32 reg, u8 *val) 61 + static int ksz_spi_write(struct ksz_device *dev, u32 reg, void *data, 62 + unsigned int len) 53 63 { 54 - return ksz_spi_read(dev, reg, val, 1); 55 - } 64 + struct spi_device *spi = dev->priv; 56 65 57 - static int ksz_spi_read16(struct ksz_device *dev, u32 reg, u16 *val) 58 - { 59 - int ret = ksz_spi_read(dev, reg, (u8 *)val, 2); 60 - 61 - if (!ret) 62 - *val = be16_to_cpu(*val); 63 - 64 - return ret; 66 + if (len > SPI_TX_BUF_LEN) 67 + len = SPI_TX_BUF_LEN; 68 + memcpy(&dev->txbuf[4], data, len); 69 + return ksz9477_spi_write_reg(spi, reg, dev->txbuf, len); 65 70 } 66 71 67 72 static int ksz_spi_read24(struct ksz_device *dev, u32 reg, u32 *val) ··· 88 75 return ret; 89 76 } 90 77 91 - static int ksz_spi_read32(struct ksz_device *dev, u32 reg, u32 *val) 92 - { 93 - int ret = ksz_spi_read(dev, reg, (u8 *)val, 4); 94 - 95 - if (!ret) 96 - *val = be32_to_cpu(*val); 97 - 98 - return ret; 99 - } 100 - 101 - static int ksz_spi_write_reg(struct spi_device *spi, u32 reg, u8 *val, 102 - unsigned int len) 103 - { 104 - u32 txbuf; 105 - u8 data[12]; 106 - int i; 107 - 108 - txbuf = reg & SPI_ADDR_MASK; 109 - txbuf |= (KS_SPIOP_WR << SPI_ADDR_SHIFT); 110 - txbuf <<= SPI_TURNAROUND_SHIFT; 111 - txbuf = cpu_to_be32(txbuf); 112 - 113 - data[0] = txbuf & 0xFF; 114 - data[1] = (txbuf & 0xFF00) >> 8; 115 - data[2] = (txbuf & 0xFF0000) >> 16; 116 - data[3] = (txbuf & 0xFF000000) >> 24; 117 - for (i = 0; i < len; i++) 118 - data[i + 4] = val[i]; 119 - 120 - return spi_write(spi, &data, 4 + len); 121 - } 122 - 123 - static int ksz_spi_write8(struct ksz_device *dev, u32 reg, u8 value) 124 - { 125 - struct spi_device *spi = dev->priv; 126 - 127 - return ksz_spi_write_reg(spi, reg, &value, 1); 128 - } 129 - 130 - static int ksz_spi_write16(struct ksz_device *dev, u32 reg, u16 value) 131 - { 132 - struct spi_device *spi = dev->priv; 133 - 134 - value = cpu_to_be16(value); 135 - return ksz_spi_write_reg(spi, reg, (u8 *)&value, 2); 136 - } 137 - 138 78 static int ksz_spi_write24(struct ksz_device *dev, u32 reg, u32 value) 139 79 { 140 - struct spi_device *spi = dev->priv; 141 - 142 80 /* make it to big endian 24bit from MSB */ 143 81 value <<= 8; 144 82 value = cpu_to_be32(value); 145 - return ksz_spi_write_reg(spi, reg, (u8 *)&value, 3); 83 + return ksz_spi_write(dev, reg, &value, 3); 146 84 } 147 85 148 - static int ksz_spi_write32(struct ksz_device *dev, u32 reg, u32 value) 149 - { 150 - struct spi_device *spi = dev->priv; 151 - 152 - value = cpu_to_be32(value); 153 - return ksz_spi_write_reg(spi, reg, (u8 *)&value, 4); 154 - } 155 - 156 - static const struct ksz_io_ops ksz_spi_ops = { 86 + static const struct ksz_io_ops ksz9477_spi_ops = { 157 87 .read8 = ksz_spi_read8, 158 88 .read16 = ksz_spi_read16, 159 89 .read24 = ksz_spi_read24, ··· 105 149 .write16 = ksz_spi_write16, 106 150 .write24 = ksz_spi_write24, 107 151 .write32 = ksz_spi_write32, 152 + .get = ksz_spi_get, 153 + .set = ksz_spi_set, 108 154 }; 109 155 110 - static int ksz_spi_probe(struct spi_device *spi) 156 + static int ksz9477_spi_probe(struct spi_device *spi) 111 157 { 112 158 struct ksz_device *dev; 113 159 int ret; 114 160 115 - dev = ksz_switch_alloc(&spi->dev, &ksz_spi_ops, spi); 161 + dev = ksz_switch_alloc(&spi->dev, &ksz9477_spi_ops, spi); 116 162 if (!dev) 117 163 return -ENOMEM; 118 164 119 165 if (spi->dev.platform_data) 120 166 dev->pdata = spi->dev.platform_data; 121 167 122 - ret = ksz_switch_register(dev); 168 + dev->txbuf = devm_kzalloc(dev->dev, 4 + SPI_TX_BUF_LEN, GFP_KERNEL); 169 + 170 + ret = ksz9477_switch_register(dev); 171 + 172 + /* Main DSA driver may not be started yet. */ 123 173 if (ret) 124 174 return ret; 125 175 ··· 134 172 return 0; 135 173 } 136 174 137 - static int ksz_spi_remove(struct spi_device *spi) 175 + static int ksz9477_spi_remove(struct spi_device *spi) 138 176 { 139 177 struct ksz_device *dev = spi_get_drvdata(spi); 140 178 ··· 144 182 return 0; 145 183 } 146 184 147 - static const struct of_device_id ksz_dt_ids[] = { 185 + static void ksz9477_spi_shutdown(struct spi_device *spi) 186 + { 187 + struct ksz_device *dev = spi_get_drvdata(spi); 188 + 189 + if (dev && dev->dev_ops->shutdown) 190 + dev->dev_ops->shutdown(dev); 191 + } 192 + 193 + static const struct of_device_id ksz9477_dt_ids[] = { 148 194 { .compatible = "microchip,ksz9477" }, 149 195 { .compatible = "microchip,ksz9897" }, 150 196 {}, 151 197 }; 152 - MODULE_DEVICE_TABLE(of, ksz_dt_ids); 198 + MODULE_DEVICE_TABLE(of, ksz9477_dt_ids); 153 199 154 - static struct spi_driver ksz_spi_driver = { 200 + static struct spi_driver ksz9477_spi_driver = { 155 201 .driver = { 156 202 .name = "ksz9477-switch", 157 203 .owner = THIS_MODULE, 158 - .of_match_table = of_match_ptr(ksz_dt_ids), 204 + .of_match_table = of_match_ptr(ksz9477_dt_ids), 159 205 }, 160 - .probe = ksz_spi_probe, 161 - .remove = ksz_spi_remove, 206 + .probe = ksz9477_spi_probe, 207 + .remove = ksz9477_spi_remove, 208 + .shutdown = ksz9477_spi_shutdown, 162 209 }; 163 210 164 - module_spi_driver(ksz_spi_driver); 211 + module_spi_driver(ksz9477_spi_driver); 165 212 166 213 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>"); 167 - MODULE_DESCRIPTION("Microchip KSZ Series Switch SPI access Driver"); 214 + MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch SPI access Driver"); 168 215 MODULE_LICENSE("GPL");
+167 -1049
drivers/net/dsa/microchip/ksz_common.c
··· 14 14 #include <linux/phy.h> 15 15 #include <linux/etherdevice.h> 16 16 #include <linux/if_bridge.h> 17 + #include <linux/of_net.h> 17 18 #include <net/dsa.h> 18 19 #include <net/switchdev.h> 19 20 20 21 #include "ksz_priv.h" 21 22 22 - static const struct { 23 - int index; 24 - char string[ETH_GSTRING_LEN]; 25 - } mib_names[TOTAL_SWITCH_COUNTER_NUM] = { 26 - { 0x00, "rx_hi" }, 27 - { 0x01, "rx_undersize" }, 28 - { 0x02, "rx_fragments" }, 29 - { 0x03, "rx_oversize" }, 30 - { 0x04, "rx_jabbers" }, 31 - { 0x05, "rx_symbol_err" }, 32 - { 0x06, "rx_crc_err" }, 33 - { 0x07, "rx_align_err" }, 34 - { 0x08, "rx_mac_ctrl" }, 35 - { 0x09, "rx_pause" }, 36 - { 0x0A, "rx_bcast" }, 37 - { 0x0B, "rx_mcast" }, 38 - { 0x0C, "rx_ucast" }, 39 - { 0x0D, "rx_64_or_less" }, 40 - { 0x0E, "rx_65_127" }, 41 - { 0x0F, "rx_128_255" }, 42 - { 0x10, "rx_256_511" }, 43 - { 0x11, "rx_512_1023" }, 44 - { 0x12, "rx_1024_1522" }, 45 - { 0x13, "rx_1523_2000" }, 46 - { 0x14, "rx_2001" }, 47 - { 0x15, "tx_hi" }, 48 - { 0x16, "tx_late_col" }, 49 - { 0x17, "tx_pause" }, 50 - { 0x18, "tx_bcast" }, 51 - { 0x19, "tx_mcast" }, 52 - { 0x1A, "tx_ucast" }, 53 - { 0x1B, "tx_deferred" }, 54 - { 0x1C, "tx_total_col" }, 55 - { 0x1D, "tx_exc_col" }, 56 - { 0x1E, "tx_single_col" }, 57 - { 0x1F, "tx_mult_col" }, 58 - { 0x80, "rx_total" }, 59 - { 0x81, "tx_total" }, 60 - { 0x82, "rx_discards" }, 61 - { 0x83, "tx_discards" }, 62 - }; 63 - 64 - static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set) 23 + void ksz_update_port_member(struct ksz_device *dev, int port) 65 24 { 66 - u8 data; 67 - 68 - ksz_read8(dev, addr, &data); 69 - if (set) 70 - data |= bits; 71 - else 72 - data &= ~bits; 73 - ksz_write8(dev, addr, data); 74 - } 75 - 76 - static void ksz_cfg32(struct ksz_device *dev, u32 addr, u32 bits, bool set) 77 - { 78 - u32 data; 79 - 80 - ksz_read32(dev, addr, &data); 81 - if (set) 82 - data |= bits; 83 - else 84 - data &= ~bits; 85 - ksz_write32(dev, addr, data); 86 - } 87 - 88 - static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits, 89 - bool set) 90 - { 91 - u32 addr; 92 - u8 data; 93 - 94 - addr = PORT_CTRL_ADDR(port, offset); 95 - ksz_read8(dev, addr, &data); 96 - 97 - if (set) 98 - data |= bits; 99 - else 100 - data &= ~bits; 101 - 102 - ksz_write8(dev, addr, data); 103 - } 104 - 105 - static void ksz_port_cfg32(struct ksz_device *dev, int port, int offset, 106 - u32 bits, bool set) 107 - { 108 - u32 addr; 109 - u32 data; 110 - 111 - addr = PORT_CTRL_ADDR(port, offset); 112 - ksz_read32(dev, addr, &data); 113 - 114 - if (set) 115 - data |= bits; 116 - else 117 - data &= ~bits; 118 - 119 - ksz_write32(dev, addr, data); 120 - } 121 - 122 - static int wait_vlan_ctrl_ready(struct ksz_device *dev, u32 waiton, int timeout) 123 - { 124 - u8 data; 125 - 126 - do { 127 - ksz_read8(dev, REG_SW_VLAN_CTRL, &data); 128 - if (!(data & waiton)) 129 - break; 130 - usleep_range(1, 10); 131 - } while (timeout-- > 0); 132 - 133 - if (timeout <= 0) 134 - return -ETIMEDOUT; 135 - 136 - return 0; 137 - } 138 - 139 - static int get_vlan_table(struct dsa_switch *ds, u16 vid, u32 *vlan_table) 140 - { 141 - struct ksz_device *dev = ds->priv; 142 - int ret; 143 - 144 - mutex_lock(&dev->vlan_mutex); 145 - 146 - ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M); 147 - ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START); 148 - 149 - /* wait to be cleared */ 150 - ret = wait_vlan_ctrl_ready(dev, VLAN_START, 1000); 151 - if (ret < 0) { 152 - dev_dbg(dev->dev, "Failed to read vlan table\n"); 153 - goto exit; 154 - } 155 - 156 - ksz_read32(dev, REG_SW_VLAN_ENTRY__4, &vlan_table[0]); 157 - ksz_read32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, &vlan_table[1]); 158 - ksz_read32(dev, REG_SW_VLAN_ENTRY_PORTS__4, &vlan_table[2]); 159 - 160 - ksz_write8(dev, REG_SW_VLAN_CTRL, 0); 161 - 162 - exit: 163 - mutex_unlock(&dev->vlan_mutex); 164 - 165 - return ret; 166 - } 167 - 168 - static int set_vlan_table(struct dsa_switch *ds, u16 vid, u32 *vlan_table) 169 - { 170 - struct ksz_device *dev = ds->priv; 171 - int ret; 172 - 173 - mutex_lock(&dev->vlan_mutex); 174 - 175 - ksz_write32(dev, REG_SW_VLAN_ENTRY__4, vlan_table[0]); 176 - ksz_write32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, vlan_table[1]); 177 - ksz_write32(dev, REG_SW_VLAN_ENTRY_PORTS__4, vlan_table[2]); 178 - 179 - ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M); 180 - ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE); 181 - 182 - /* wait to be cleared */ 183 - ret = wait_vlan_ctrl_ready(dev, VLAN_START, 1000); 184 - if (ret < 0) { 185 - dev_dbg(dev->dev, "Failed to write vlan table\n"); 186 - goto exit; 187 - } 188 - 189 - ksz_write8(dev, REG_SW_VLAN_CTRL, 0); 190 - 191 - /* update vlan cache table */ 192 - dev->vlan_cache[vid].table[0] = vlan_table[0]; 193 - dev->vlan_cache[vid].table[1] = vlan_table[1]; 194 - dev->vlan_cache[vid].table[2] = vlan_table[2]; 195 - 196 - exit: 197 - mutex_unlock(&dev->vlan_mutex); 198 - 199 - return ret; 200 - } 201 - 202 - static void read_table(struct dsa_switch *ds, u32 *table) 203 - { 204 - struct ksz_device *dev = ds->priv; 205 - 206 - ksz_read32(dev, REG_SW_ALU_VAL_A, &table[0]); 207 - ksz_read32(dev, REG_SW_ALU_VAL_B, &table[1]); 208 - ksz_read32(dev, REG_SW_ALU_VAL_C, &table[2]); 209 - ksz_read32(dev, REG_SW_ALU_VAL_D, &table[3]); 210 - } 211 - 212 - static void write_table(struct dsa_switch *ds, u32 *table) 213 - { 214 - struct ksz_device *dev = ds->priv; 215 - 216 - ksz_write32(dev, REG_SW_ALU_VAL_A, table[0]); 217 - ksz_write32(dev, REG_SW_ALU_VAL_B, table[1]); 218 - ksz_write32(dev, REG_SW_ALU_VAL_C, table[2]); 219 - ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]); 220 - } 221 - 222 - static int wait_alu_ready(struct ksz_device *dev, u32 waiton, int timeout) 223 - { 224 - u32 data; 225 - 226 - do { 227 - ksz_read32(dev, REG_SW_ALU_CTRL__4, &data); 228 - if (!(data & waiton)) 229 - break; 230 - usleep_range(1, 10); 231 - } while (timeout-- > 0); 232 - 233 - if (timeout <= 0) 234 - return -ETIMEDOUT; 235 - 236 - return 0; 237 - } 238 - 239 - static int wait_alu_sta_ready(struct ksz_device *dev, u32 waiton, int timeout) 240 - { 241 - u32 data; 242 - 243 - do { 244 - ksz_read32(dev, REG_SW_ALU_STAT_CTRL__4, &data); 245 - if (!(data & waiton)) 246 - break; 247 - usleep_range(1, 10); 248 - } while (timeout-- > 0); 249 - 250 - if (timeout <= 0) 251 - return -ETIMEDOUT; 252 - 253 - return 0; 254 - } 255 - 256 - static int ksz9477_reset_switch(struct ksz_device *dev) 257 - { 258 - u8 data8; 259 - u16 data16; 260 - u32 data32; 261 - 262 - /* reset switch */ 263 - ksz_cfg(dev, REG_SW_OPERATION, SW_RESET, true); 264 - 265 - /* turn off SPI DO Edge select */ 266 - ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8); 267 - data8 &= ~SPI_AUTO_EDGE_DETECTION; 268 - ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8); 269 - 270 - /* default configuration */ 271 - ksz_read8(dev, REG_SW_LUE_CTRL_1, &data8); 272 - data8 = SW_AGING_ENABLE | SW_LINK_AUTO_AGING | 273 - SW_SRC_ADDR_FILTER | SW_FLUSH_STP_TABLE | SW_FLUSH_MSTP_TABLE; 274 - ksz_write8(dev, REG_SW_LUE_CTRL_1, data8); 275 - 276 - /* disable interrupts */ 277 - ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK); 278 - ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0x7F); 279 - ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32); 280 - 281 - /* set broadcast storm protection 10% rate */ 282 - ksz_read16(dev, REG_SW_MAC_CTRL_2, &data16); 283 - data16 &= ~BROADCAST_STORM_RATE; 284 - data16 |= (BROADCAST_STORM_VALUE * BROADCAST_STORM_PROT_RATE) / 100; 285 - ksz_write16(dev, REG_SW_MAC_CTRL_2, data16); 286 - 287 - return 0; 288 - } 289 - 290 - static void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port) 291 - { 292 - u8 data8; 293 - u16 data16; 294 - 295 - /* enable tag tail for host port */ 296 - if (cpu_port) 297 - ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_TAIL_TAG_ENABLE, 298 - true); 299 - 300 - ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_MAC_LOOPBACK, false); 301 - 302 - /* set back pressure */ 303 - ksz_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE, true); 304 - 305 - /* set flow control */ 306 - ksz_port_cfg(dev, port, REG_PORT_CTRL_0, 307 - PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL, true); 308 - 309 - /* enable broadcast storm limit */ 310 - ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true); 311 - 312 - /* disable DiffServ priority */ 313 - ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_PRIO_ENABLE, false); 314 - 315 - /* replace priority */ 316 - ksz_port_cfg(dev, port, REG_PORT_MRI_MAC_CTRL, PORT_USER_PRIO_CEILING, 317 - false); 318 - ksz_port_cfg32(dev, port, REG_PORT_MTI_QUEUE_CTRL_0__4, 319 - MTI_PVID_REPLACE, false); 320 - 321 - /* enable 802.1p priority */ 322 - ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true); 323 - 324 - /* configure MAC to 1G & RGMII mode */ 325 - ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8); 326 - data8 |= PORT_RGMII_ID_EG_ENABLE; 327 - data8 &= ~PORT_MII_NOT_1GBIT; 328 - data8 &= ~PORT_MII_SEL_M; 329 - data8 |= PORT_RGMII_SEL; 330 - ksz_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8); 331 - 332 - /* clear pending interrupts */ 333 - ksz_pread16(dev, port, REG_PORT_PHY_INT_ENABLE, &data16); 334 - } 335 - 336 - static void ksz9477_config_cpu_port(struct dsa_switch *ds) 337 - { 338 - struct ksz_device *dev = ds->priv; 25 + struct ksz_port *p; 339 26 int i; 340 27 341 - ds->num_ports = dev->port_cnt; 28 + for (i = 0; i < dev->port_cnt; i++) { 29 + if (i == port || i == dev->cpu_port) 30 + continue; 31 + p = &dev->ports[i]; 32 + if (!(dev->member & (1 << i))) 33 + continue; 342 34 343 - for (i = 0; i < ds->num_ports; i++) { 344 - if (dsa_is_cpu_port(ds, i) && (dev->cpu_ports & (1 << i))) { 345 - dev->cpu_port = i; 346 - 347 - /* enable cpu port */ 348 - ksz9477_port_setup(dev, i, true); 349 - } 35 + /* Port is a member of the bridge and is forwarding. */ 36 + if (p->stp_state == BR_STATE_FORWARDING && 37 + p->member != dev->member) 38 + dev->dev_ops->cfg_port_member(dev, i, dev->member); 350 39 } 351 40 } 41 + EXPORT_SYMBOL_GPL(ksz_update_port_member); 352 42 353 - static int ksz9477_setup(struct dsa_switch *ds) 43 + int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg) 354 44 { 355 45 struct ksz_device *dev = ds->priv; 356 - int ret = 0; 46 + u16 val = 0xffff; 357 47 358 - dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table), 359 - dev->num_vlans, GFP_KERNEL); 360 - if (!dev->vlan_cache) 361 - return -ENOMEM; 362 - 363 - ret = ksz9477_reset_switch(dev); 364 - if (ret) { 365 - dev_err(ds->dev, "failed to reset switch\n"); 366 - return ret; 367 - } 368 - 369 - /* accept packet up to 2000bytes */ 370 - ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_LEGAL_PACKET_DISABLE, true); 371 - 372 - ksz9477_config_cpu_port(ds); 373 - 374 - ksz_cfg(dev, REG_SW_MAC_CTRL_1, MULTICAST_STORM_DISABLE, true); 375 - 376 - /* queue based egress rate limit */ 377 - ksz_cfg(dev, REG_SW_MAC_CTRL_5, SW_OUT_RATE_LIMIT_QUEUE_BASED, true); 378 - 379 - /* start switch */ 380 - ksz_cfg(dev, REG_SW_OPERATION, SW_START, true); 381 - 382 - return 0; 383 - } 384 - 385 - static enum dsa_tag_protocol ksz9477_get_tag_protocol(struct dsa_switch *ds, 386 - int port) 387 - { 388 - return DSA_TAG_PROTO_KSZ; 389 - } 390 - 391 - static int ksz9477_phy_read16(struct dsa_switch *ds, int addr, int reg) 392 - { 393 - struct ksz_device *dev = ds->priv; 394 - u16 val = 0; 395 - 396 - ksz_pread16(dev, addr, 0x100 + (reg << 1), &val); 48 + dev->dev_ops->r_phy(dev, addr, reg, &val); 397 49 398 50 return val; 399 51 } 52 + EXPORT_SYMBOL_GPL(ksz_phy_read16); 400 53 401 - static int ksz9477_phy_write16(struct dsa_switch *ds, int addr, int reg, 402 - u16 val) 54 + int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val) 403 55 { 404 56 struct ksz_device *dev = ds->priv; 405 57 406 - ksz_pwrite16(dev, addr, 0x100 + (reg << 1), val); 58 + dev->dev_ops->w_phy(dev, addr, reg, val); 407 59 408 60 return 0; 409 61 } 62 + EXPORT_SYMBOL_GPL(ksz_phy_write16); 410 63 411 - static int ksz_enable_port(struct dsa_switch *ds, int port, 412 - struct phy_device *phy) 64 + int ksz_sset_count(struct dsa_switch *ds, int port, int sset) 413 65 { 414 66 struct ksz_device *dev = ds->priv; 415 67 416 - /* setup slave port */ 417 - ksz9477_port_setup(dev, port, false); 418 - 419 - return 0; 420 - } 421 - 422 - static void ksz_disable_port(struct dsa_switch *ds, int port, 423 - struct phy_device *phy) 424 - { 425 - struct ksz_device *dev = ds->priv; 426 - 427 - /* there is no port disable */ 428 - ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_MAC_LOOPBACK, true); 429 - } 430 - 431 - static int ksz_sset_count(struct dsa_switch *ds, int port, int sset) 432 - { 433 68 if (sset != ETH_SS_STATS) 434 69 return 0; 435 70 436 - return TOTAL_SWITCH_COUNTER_NUM; 71 + return dev->mib_cnt; 437 72 } 73 + EXPORT_SYMBOL_GPL(ksz_sset_count); 438 74 439 - static void ksz9477_get_strings(struct dsa_switch *ds, int port, 440 - u32 stringset, uint8_t *buf) 441 - { 442 - int i; 443 - 444 - if (stringset != ETH_SS_STATS) 445 - return; 446 - 447 - for (i = 0; i < TOTAL_SWITCH_COUNTER_NUM; i++) { 448 - memcpy(buf + i * ETH_GSTRING_LEN, mib_names[i].string, 449 - ETH_GSTRING_LEN); 450 - } 451 - } 452 - 453 - static void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, 454 - uint64_t *buf) 455 - { 456 - struct ksz_device *dev = ds->priv; 457 - int i; 458 - u32 data; 459 - int timeout; 460 - 461 - mutex_lock(&dev->stats_mutex); 462 - 463 - for (i = 0; i < TOTAL_SWITCH_COUNTER_NUM; i++) { 464 - data = MIB_COUNTER_READ; 465 - data |= ((mib_names[i].index & 0xFF) << MIB_COUNTER_INDEX_S); 466 - ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, data); 467 - 468 - timeout = 1000; 469 - do { 470 - ksz_pread32(dev, port, REG_PORT_MIB_CTRL_STAT__4, 471 - &data); 472 - usleep_range(1, 10); 473 - if (!(data & MIB_COUNTER_READ)) 474 - break; 475 - } while (timeout-- > 0); 476 - 477 - /* failed to read MIB. get out of loop */ 478 - if (!timeout) { 479 - dev_dbg(dev->dev, "Failed to get MIB\n"); 480 - break; 481 - } 482 - 483 - /* count resets upon read */ 484 - ksz_pread32(dev, port, REG_PORT_MIB_DATA, &data); 485 - 486 - dev->mib_value[i] += (uint64_t)data; 487 - buf[i] = dev->mib_value[i]; 488 - } 489 - 490 - mutex_unlock(&dev->stats_mutex); 491 - } 492 - 493 - static void ksz9477_port_stp_state_set(struct dsa_switch *ds, int port, 494 - u8 state) 495 - { 496 - struct ksz_device *dev = ds->priv; 497 - u8 data; 498 - 499 - ksz_pread8(dev, port, P_STP_CTRL, &data); 500 - data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE); 501 - 502 - switch (state) { 503 - case BR_STATE_DISABLED: 504 - data |= PORT_LEARN_DISABLE; 505 - break; 506 - case BR_STATE_LISTENING: 507 - data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE); 508 - break; 509 - case BR_STATE_LEARNING: 510 - data |= PORT_RX_ENABLE; 511 - break; 512 - case BR_STATE_FORWARDING: 513 - data |= (PORT_TX_ENABLE | PORT_RX_ENABLE); 514 - break; 515 - case BR_STATE_BLOCKING: 516 - data |= PORT_LEARN_DISABLE; 517 - break; 518 - default: 519 - dev_err(ds->dev, "invalid STP state: %d\n", state); 520 - return; 521 - } 522 - 523 - ksz_pwrite8(dev, port, P_STP_CTRL, data); 524 - } 525 - 526 - static void ksz_port_fast_age(struct dsa_switch *ds, int port) 527 - { 528 - struct ksz_device *dev = ds->priv; 529 - u8 data8; 530 - 531 - ksz_read8(dev, REG_SW_LUE_CTRL_1, &data8); 532 - data8 |= SW_FAST_AGING; 533 - ksz_write8(dev, REG_SW_LUE_CTRL_1, data8); 534 - 535 - data8 &= ~SW_FAST_AGING; 536 - ksz_write8(dev, REG_SW_LUE_CTRL_1, data8); 537 - } 538 - 539 - static int ksz9477_port_vlan_filtering(struct dsa_switch *ds, int port, 540 - bool flag) 75 + int ksz_port_bridge_join(struct dsa_switch *ds, int port, 76 + struct net_device *br) 541 77 { 542 78 struct ksz_device *dev = ds->priv; 543 79 544 - if (flag) { 545 - ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL, 546 - PORT_VLAN_LOOKUP_VID_0, true); 547 - ksz_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY, true); 548 - ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, true); 549 - } else { 550 - ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, false); 551 - ksz_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY, false); 552 - ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL, 553 - PORT_VLAN_LOOKUP_VID_0, false); 554 - } 80 + dev->br_member |= (1 << port); 81 + 82 + /* port_stp_state_set() will be called after to put the port in 83 + * appropriate state so there is no need to do anything. 84 + */ 555 85 556 86 return 0; 557 87 } 88 + EXPORT_SYMBOL_GPL(ksz_port_bridge_join); 558 89 559 - static int ksz_port_vlan_prepare(struct dsa_switch *ds, int port, 560 - const struct switchdev_obj_port_vlan *vlan) 90 + void ksz_port_bridge_leave(struct dsa_switch *ds, int port, 91 + struct net_device *br) 92 + { 93 + struct ksz_device *dev = ds->priv; 94 + 95 + dev->br_member &= ~(1 << port); 96 + dev->member &= ~(1 << port); 97 + 98 + /* port_stp_state_set() will be called after to put the port in 99 + * forwarding state so there is no need to do anything. 100 + */ 101 + } 102 + EXPORT_SYMBOL_GPL(ksz_port_bridge_leave); 103 + 104 + void ksz_port_fast_age(struct dsa_switch *ds, int port) 105 + { 106 + struct ksz_device *dev = ds->priv; 107 + 108 + dev->dev_ops->flush_dyn_mac_table(dev, port); 109 + } 110 + EXPORT_SYMBOL_GPL(ksz_port_fast_age); 111 + 112 + int ksz_port_vlan_prepare(struct dsa_switch *ds, int port, 113 + const struct switchdev_obj_port_vlan *vlan) 561 114 { 562 115 /* nothing needed */ 563 116 564 117 return 0; 565 118 } 119 + EXPORT_SYMBOL_GPL(ksz_port_vlan_prepare); 566 120 567 - static void ksz9477_port_vlan_add(struct dsa_switch *ds, int port, 568 - const struct switchdev_obj_port_vlan *vlan) 569 - { 570 - struct ksz_device *dev = ds->priv; 571 - u32 vlan_table[3]; 572 - u16 vid; 573 - bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 574 - 575 - for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 576 - if (get_vlan_table(ds, vid, vlan_table)) { 577 - dev_dbg(dev->dev, "Failed to get vlan table\n"); 578 - return; 579 - } 580 - 581 - vlan_table[0] = VLAN_VALID | (vid & VLAN_FID_M); 582 - if (untagged) 583 - vlan_table[1] |= BIT(port); 584 - else 585 - vlan_table[1] &= ~BIT(port); 586 - vlan_table[1] &= ~(BIT(dev->cpu_port)); 587 - 588 - vlan_table[2] |= BIT(port) | BIT(dev->cpu_port); 589 - 590 - if (set_vlan_table(ds, vid, vlan_table)) { 591 - dev_dbg(dev->dev, "Failed to set vlan table\n"); 592 - return; 593 - } 594 - 595 - /* change PVID */ 596 - if (vlan->flags & BRIDGE_VLAN_INFO_PVID) 597 - ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, vid); 598 - } 599 - } 600 - 601 - static int ksz9477_port_vlan_del(struct dsa_switch *ds, int port, 602 - const struct switchdev_obj_port_vlan *vlan) 603 - { 604 - struct ksz_device *dev = ds->priv; 605 - bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 606 - u32 vlan_table[3]; 607 - u16 vid; 608 - u16 pvid; 609 - 610 - ksz_pread16(dev, port, REG_PORT_DEFAULT_VID, &pvid); 611 - pvid = pvid & 0xFFF; 612 - 613 - for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) { 614 - if (get_vlan_table(ds, vid, vlan_table)) { 615 - dev_dbg(dev->dev, "Failed to get vlan table\n"); 616 - return -ETIMEDOUT; 617 - } 618 - 619 - vlan_table[2] &= ~BIT(port); 620 - 621 - if (pvid == vid) 622 - pvid = 1; 623 - 624 - if (untagged) 625 - vlan_table[1] &= ~BIT(port); 626 - 627 - if (set_vlan_table(ds, vid, vlan_table)) { 628 - dev_dbg(dev->dev, "Failed to set vlan table\n"); 629 - return -ETIMEDOUT; 630 - } 631 - } 632 - 633 - ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, pvid); 634 - 635 - return 0; 636 - } 637 - 638 - struct alu_struct { 639 - /* entry 1 */ 640 - u8 is_static:1; 641 - u8 is_src_filter:1; 642 - u8 is_dst_filter:1; 643 - u8 prio_age:3; 644 - u32 _reserv_0_1:23; 645 - u8 mstp:3; 646 - /* entry 2 */ 647 - u8 is_override:1; 648 - u8 is_use_fid:1; 649 - u32 _reserv_1_1:23; 650 - u8 port_forward:7; 651 - /* entry 3 & 4*/ 652 - u32 _reserv_2_1:9; 653 - u8 fid:7; 654 - u8 mac[ETH_ALEN]; 655 - }; 656 - 657 - static int ksz9477_port_fdb_add(struct dsa_switch *ds, int port, 658 - const unsigned char *addr, u16 vid) 659 - { 660 - struct ksz_device *dev = ds->priv; 661 - u32 alu_table[4]; 662 - u32 data; 663 - int ret = 0; 664 - 665 - mutex_lock(&dev->alu_mutex); 666 - 667 - /* find any entry with mac & vid */ 668 - data = vid << ALU_FID_INDEX_S; 669 - data |= ((addr[0] << 8) | addr[1]); 670 - ksz_write32(dev, REG_SW_ALU_INDEX_0, data); 671 - 672 - data = ((addr[2] << 24) | (addr[3] << 16)); 673 - data |= ((addr[4] << 8) | addr[5]); 674 - ksz_write32(dev, REG_SW_ALU_INDEX_1, data); 675 - 676 - /* start read operation */ 677 - ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START); 678 - 679 - /* wait to be finished */ 680 - ret = wait_alu_ready(dev, ALU_START, 1000); 681 - if (ret < 0) { 682 - dev_dbg(dev->dev, "Failed to read ALU\n"); 683 - goto exit; 684 - } 685 - 686 - /* read ALU entry */ 687 - read_table(ds, alu_table); 688 - 689 - /* update ALU entry */ 690 - alu_table[0] = ALU_V_STATIC_VALID; 691 - alu_table[1] |= BIT(port); 692 - if (vid) 693 - alu_table[1] |= ALU_V_USE_FID; 694 - alu_table[2] = (vid << ALU_V_FID_S); 695 - alu_table[2] |= ((addr[0] << 8) | addr[1]); 696 - alu_table[3] = ((addr[2] << 24) | (addr[3] << 16)); 697 - alu_table[3] |= ((addr[4] << 8) | addr[5]); 698 - 699 - write_table(ds, alu_table); 700 - 701 - ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START); 702 - 703 - /* wait to be finished */ 704 - ret = wait_alu_ready(dev, ALU_START, 1000); 705 - if (ret < 0) 706 - dev_dbg(dev->dev, "Failed to write ALU\n"); 707 - 708 - exit: 709 - mutex_unlock(&dev->alu_mutex); 710 - 711 - return ret; 712 - } 713 - 714 - static int ksz9477_port_fdb_del(struct dsa_switch *ds, int port, 715 - const unsigned char *addr, u16 vid) 716 - { 717 - struct ksz_device *dev = ds->priv; 718 - u32 alu_table[4]; 719 - u32 data; 720 - int ret = 0; 721 - 722 - mutex_lock(&dev->alu_mutex); 723 - 724 - /* read any entry with mac & vid */ 725 - data = vid << ALU_FID_INDEX_S; 726 - data |= ((addr[0] << 8) | addr[1]); 727 - ksz_write32(dev, REG_SW_ALU_INDEX_0, data); 728 - 729 - data = ((addr[2] << 24) | (addr[3] << 16)); 730 - data |= ((addr[4] << 8) | addr[5]); 731 - ksz_write32(dev, REG_SW_ALU_INDEX_1, data); 732 - 733 - /* start read operation */ 734 - ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START); 735 - 736 - /* wait to be finished */ 737 - ret = wait_alu_ready(dev, ALU_START, 1000); 738 - if (ret < 0) { 739 - dev_dbg(dev->dev, "Failed to read ALU\n"); 740 - goto exit; 741 - } 742 - 743 - ksz_read32(dev, REG_SW_ALU_VAL_A, &alu_table[0]); 744 - if (alu_table[0] & ALU_V_STATIC_VALID) { 745 - ksz_read32(dev, REG_SW_ALU_VAL_B, &alu_table[1]); 746 - ksz_read32(dev, REG_SW_ALU_VAL_C, &alu_table[2]); 747 - ksz_read32(dev, REG_SW_ALU_VAL_D, &alu_table[3]); 748 - 749 - /* clear forwarding port */ 750 - alu_table[2] &= ~BIT(port); 751 - 752 - /* if there is no port to forward, clear table */ 753 - if ((alu_table[2] & ALU_V_PORT_MAP) == 0) { 754 - alu_table[0] = 0; 755 - alu_table[1] = 0; 756 - alu_table[2] = 0; 757 - alu_table[3] = 0; 758 - } 759 - } else { 760 - alu_table[0] = 0; 761 - alu_table[1] = 0; 762 - alu_table[2] = 0; 763 - alu_table[3] = 0; 764 - } 765 - 766 - write_table(ds, alu_table); 767 - 768 - ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START); 769 - 770 - /* wait to be finished */ 771 - ret = wait_alu_ready(dev, ALU_START, 1000); 772 - if (ret < 0) 773 - dev_dbg(dev->dev, "Failed to write ALU\n"); 774 - 775 - exit: 776 - mutex_unlock(&dev->alu_mutex); 777 - 778 - return ret; 779 - } 780 - 781 - static void convert_alu(struct alu_struct *alu, u32 *alu_table) 782 - { 783 - alu->is_static = !!(alu_table[0] & ALU_V_STATIC_VALID); 784 - alu->is_src_filter = !!(alu_table[0] & ALU_V_SRC_FILTER); 785 - alu->is_dst_filter = !!(alu_table[0] & ALU_V_DST_FILTER); 786 - alu->prio_age = (alu_table[0] >> ALU_V_PRIO_AGE_CNT_S) & 787 - ALU_V_PRIO_AGE_CNT_M; 788 - alu->mstp = alu_table[0] & ALU_V_MSTP_M; 789 - 790 - alu->is_override = !!(alu_table[1] & ALU_V_OVERRIDE); 791 - alu->is_use_fid = !!(alu_table[1] & ALU_V_USE_FID); 792 - alu->port_forward = alu_table[1] & ALU_V_PORT_MAP; 793 - 794 - alu->fid = (alu_table[2] >> ALU_V_FID_S) & ALU_V_FID_M; 795 - 796 - alu->mac[0] = (alu_table[2] >> 8) & 0xFF; 797 - alu->mac[1] = alu_table[2] & 0xFF; 798 - alu->mac[2] = (alu_table[3] >> 24) & 0xFF; 799 - alu->mac[3] = (alu_table[3] >> 16) & 0xFF; 800 - alu->mac[4] = (alu_table[3] >> 8) & 0xFF; 801 - alu->mac[5] = alu_table[3] & 0xFF; 802 - } 803 - 804 - static int ksz9477_port_fdb_dump(struct dsa_switch *ds, int port, 805 - dsa_fdb_dump_cb_t *cb, void *data) 121 + int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb, 122 + void *data) 806 123 { 807 124 struct ksz_device *dev = ds->priv; 808 125 int ret = 0; 809 - u32 ksz_data; 810 - u32 alu_table[4]; 126 + u16 i = 0; 127 + u16 entries = 0; 128 + u8 timestamp = 0; 129 + u8 fid; 130 + u8 member; 811 131 struct alu_struct alu; 812 - int timeout; 813 - 814 - mutex_lock(&dev->alu_mutex); 815 - 816 - /* start ALU search */ 817 - ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_START | ALU_SEARCH); 818 132 819 133 do { 820 - timeout = 1000; 821 - do { 822 - ksz_read32(dev, REG_SW_ALU_CTRL__4, &ksz_data); 823 - if ((ksz_data & ALU_VALID) || !(ksz_data & ALU_START)) 824 - break; 825 - usleep_range(1, 10); 826 - } while (timeout-- > 0); 827 - 828 - if (!timeout) { 829 - dev_dbg(dev->dev, "Failed to search ALU\n"); 830 - ret = -ETIMEDOUT; 831 - goto exit; 832 - } 833 - 834 - /* read ALU table */ 835 - read_table(ds, alu_table); 836 - 837 - convert_alu(&alu, alu_table); 838 - 839 - if (alu.port_forward & BIT(port)) { 134 + alu.is_static = false; 135 + ret = dev->dev_ops->r_dyn_mac_table(dev, i, alu.mac, &fid, 136 + &member, &timestamp, 137 + &entries); 138 + if (!ret && (member & BIT(port))) { 840 139 ret = cb(alu.mac, alu.fid, alu.is_static, data); 841 140 if (ret) 842 - goto exit; 141 + break; 843 142 } 844 - } while (ksz_data & ALU_START); 845 - 846 - exit: 847 - 848 - /* stop ALU search */ 849 - ksz_write32(dev, REG_SW_ALU_CTRL__4, 0); 850 - 851 - mutex_unlock(&dev->alu_mutex); 143 + i++; 144 + } while (i < entries); 145 + if (i >= entries) 146 + ret = 0; 852 147 853 148 return ret; 854 149 } 150 + EXPORT_SYMBOL_GPL(ksz_port_fdb_dump); 855 151 856 - static int ksz_port_mdb_prepare(struct dsa_switch *ds, int port, 857 - const struct switchdev_obj_port_mdb *mdb) 152 + int ksz_port_mdb_prepare(struct dsa_switch *ds, int port, 153 + const struct switchdev_obj_port_mdb *mdb) 858 154 { 859 155 /* nothing to do */ 860 156 return 0; 861 157 } 158 + EXPORT_SYMBOL_GPL(ksz_port_mdb_prepare); 862 159 863 - static void ksz9477_port_mdb_add(struct dsa_switch *ds, int port, 864 - const struct switchdev_obj_port_mdb *mdb) 160 + void ksz_port_mdb_add(struct dsa_switch *ds, int port, 161 + const struct switchdev_obj_port_mdb *mdb) 865 162 { 866 163 struct ksz_device *dev = ds->priv; 867 - u32 static_table[4]; 868 - u32 data; 164 + struct alu_struct alu; 869 165 int index; 870 - u32 mac_hi, mac_lo; 166 + int empty = 0; 871 167 872 - mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]); 873 - mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16)); 874 - mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]); 168 + alu.port_forward = 0; 169 + for (index = 0; index < dev->num_statics; index++) { 170 + if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) { 171 + /* Found one already in static MAC table. */ 172 + if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) && 173 + alu.fid == mdb->vid) 174 + break; 175 + /* Remember the first empty entry. */ 176 + } else if (!empty) { 177 + empty = index + 1; 178 + } 179 + } 875 180 876 - mutex_lock(&dev->alu_mutex); 181 + /* no available entry */ 182 + if (index == dev->num_statics && !empty) 183 + return; 184 + 185 + /* add entry */ 186 + if (index == dev->num_statics) { 187 + index = empty - 1; 188 + memset(&alu, 0, sizeof(alu)); 189 + memcpy(alu.mac, mdb->addr, ETH_ALEN); 190 + alu.is_static = true; 191 + } 192 + alu.port_forward |= BIT(port); 193 + if (mdb->vid) { 194 + alu.is_use_fid = true; 195 + 196 + /* Need a way to map VID to FID. */ 197 + alu.fid = mdb->vid; 198 + } 199 + dev->dev_ops->w_sta_mac_table(dev, index, &alu); 200 + } 201 + EXPORT_SYMBOL_GPL(ksz_port_mdb_add); 202 + 203 + int ksz_port_mdb_del(struct dsa_switch *ds, int port, 204 + const struct switchdev_obj_port_mdb *mdb) 205 + { 206 + struct ksz_device *dev = ds->priv; 207 + struct alu_struct alu; 208 + int index; 209 + int ret = 0; 877 210 878 211 for (index = 0; index < dev->num_statics; index++) { 879 - /* find empty slot first */ 880 - data = (index << ALU_STAT_INDEX_S) | 881 - ALU_STAT_READ | ALU_STAT_START; 882 - ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data); 883 - 884 - /* wait to be finished */ 885 - if (wait_alu_sta_ready(dev, ALU_STAT_START, 1000) < 0) { 886 - dev_dbg(dev->dev, "Failed to read ALU STATIC\n"); 887 - goto exit; 888 - } 889 - 890 - /* read ALU static table */ 891 - read_table(ds, static_table); 892 - 893 - if (static_table[0] & ALU_V_STATIC_VALID) { 894 - /* check this has same vid & mac address */ 895 - if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) && 896 - ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) && 897 - static_table[3] == mac_lo) { 898 - /* found matching one */ 212 + if (!dev->dev_ops->r_sta_mac_table(dev, index, &alu)) { 213 + /* Found one already in static MAC table. */ 214 + if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) && 215 + alu.fid == mdb->vid) 899 216 break; 900 - } 901 - } else { 902 - /* found empty one */ 903 - break; 904 217 } 905 218 } 906 219 ··· 221 908 if (index == dev->num_statics) 222 909 goto exit; 223 910 224 - /* add entry */ 225 - static_table[0] = ALU_V_STATIC_VALID; 226 - static_table[1] |= BIT(port); 227 - if (mdb->vid) 228 - static_table[1] |= ALU_V_USE_FID; 229 - static_table[2] = (mdb->vid << ALU_V_FID_S); 230 - static_table[2] |= mac_hi; 231 - static_table[3] = mac_lo; 232 - 233 - write_table(ds, static_table); 234 - 235 - data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START; 236 - ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data); 237 - 238 - /* wait to be finished */ 239 - if (wait_alu_sta_ready(dev, ALU_STAT_START, 1000) < 0) 240 - dev_dbg(dev->dev, "Failed to read ALU STATIC\n"); 241 - 242 - exit: 243 - mutex_unlock(&dev->alu_mutex); 244 - } 245 - 246 - static int ksz9477_port_mdb_del(struct dsa_switch *ds, int port, 247 - const struct switchdev_obj_port_mdb *mdb) 248 - { 249 - struct ksz_device *dev = ds->priv; 250 - u32 static_table[4]; 251 - u32 data; 252 - int index; 253 - int ret = 0; 254 - u32 mac_hi, mac_lo; 255 - 256 - mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]); 257 - mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16)); 258 - mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]); 259 - 260 - mutex_lock(&dev->alu_mutex); 261 - 262 - for (index = 0; index < dev->num_statics; index++) { 263 - /* find empty slot first */ 264 - data = (index << ALU_STAT_INDEX_S) | 265 - ALU_STAT_READ | ALU_STAT_START; 266 - ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data); 267 - 268 - /* wait to be finished */ 269 - ret = wait_alu_sta_ready(dev, ALU_STAT_START, 1000); 270 - if (ret < 0) { 271 - dev_dbg(dev->dev, "Failed to read ALU STATIC\n"); 272 - goto exit; 273 - } 274 - 275 - /* read ALU static table */ 276 - read_table(ds, static_table); 277 - 278 - if (static_table[0] & ALU_V_STATIC_VALID) { 279 - /* check this has same vid & mac address */ 280 - 281 - if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) && 282 - ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) && 283 - static_table[3] == mac_lo) { 284 - /* found matching one */ 285 - break; 286 - } 287 - } 288 - } 289 - 290 - /* no available entry */ 291 - if (index == dev->num_statics) { 292 - ret = -EINVAL; 293 - goto exit; 294 - } 295 - 296 911 /* clear port */ 297 - static_table[1] &= ~BIT(port); 298 - 299 - if ((static_table[1] & ALU_V_PORT_MAP) == 0) { 300 - /* delete entry */ 301 - static_table[0] = 0; 302 - static_table[1] = 0; 303 - static_table[2] = 0; 304 - static_table[3] = 0; 305 - } 306 - 307 - write_table(ds, static_table); 308 - 309 - data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START; 310 - ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data); 311 - 312 - /* wait to be finished */ 313 - ret = wait_alu_sta_ready(dev, ALU_STAT_START, 1000); 314 - if (ret < 0) 315 - dev_dbg(dev->dev, "Failed to read ALU STATIC\n"); 912 + alu.port_forward &= ~BIT(port); 913 + if (!alu.port_forward) 914 + alu.is_static = false; 915 + dev->dev_ops->w_sta_mac_table(dev, index, &alu); 316 916 317 917 exit: 318 - mutex_unlock(&dev->alu_mutex); 319 - 320 918 return ret; 321 919 } 920 + EXPORT_SYMBOL_GPL(ksz_port_mdb_del); 322 921 323 - static int ksz9477_port_mirror_add(struct dsa_switch *ds, int port, 324 - struct dsa_mall_mirror_tc_entry *mirror, 325 - bool ingress) 922 + int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy) 326 923 { 327 924 struct ksz_device *dev = ds->priv; 328 925 329 - if (ingress) 330 - ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true); 331 - else 332 - ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true); 926 + /* setup slave port */ 927 + dev->dev_ops->port_setup(dev, port, false); 333 928 334 - ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false); 335 - 336 - /* configure mirror port */ 337 - ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 338 - PORT_MIRROR_SNIFFER, true); 339 - 340 - ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false); 929 + /* port_stp_state_set() will be called after to enable the port so 930 + * there is no need to do anything. 931 + */ 341 932 342 933 return 0; 343 934 } 935 + EXPORT_SYMBOL_GPL(ksz_enable_port); 344 936 345 - static void ksz9477_port_mirror_del(struct dsa_switch *ds, int port, 346 - struct dsa_mall_mirror_tc_entry *mirror) 937 + void ksz_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy) 347 938 { 348 939 struct ksz_device *dev = ds->priv; 349 - u8 data; 350 940 351 - if (mirror->ingress) 352 - ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false); 353 - else 354 - ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false); 941 + dev->on_ports &= ~(1 << port); 942 + dev->live_ports &= ~(1 << port); 355 943 356 - ksz_pread8(dev, port, P_MIRROR_CTRL, &data); 357 - 358 - if (!(data & (PORT_MIRROR_RX | PORT_MIRROR_TX))) 359 - ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL, 360 - PORT_MIRROR_SNIFFER, false); 944 + /* port_stp_state_set() will be called after to disable the port so 945 + * there is no need to do anything. 946 + */ 361 947 } 362 - 363 - static const struct dsa_switch_ops ksz_switch_ops = { 364 - .get_tag_protocol = ksz9477_get_tag_protocol, 365 - .setup = ksz9477_setup, 366 - .phy_read = ksz9477_phy_read16, 367 - .phy_write = ksz9477_phy_write16, 368 - .port_enable = ksz_enable_port, 369 - .port_disable = ksz_disable_port, 370 - .get_strings = ksz9477_get_strings, 371 - .get_ethtool_stats = ksz_get_ethtool_stats, 372 - .get_sset_count = ksz_sset_count, 373 - .port_stp_state_set = ksz9477_port_stp_state_set, 374 - .port_fast_age = ksz_port_fast_age, 375 - .port_vlan_filtering = ksz9477_port_vlan_filtering, 376 - .port_vlan_prepare = ksz_port_vlan_prepare, 377 - .port_vlan_add = ksz9477_port_vlan_add, 378 - .port_vlan_del = ksz9477_port_vlan_del, 379 - .port_fdb_dump = ksz9477_port_fdb_dump, 380 - .port_fdb_add = ksz9477_port_fdb_add, 381 - .port_fdb_del = ksz9477_port_fdb_del, 382 - .port_mdb_prepare = ksz_port_mdb_prepare, 383 - .port_mdb_add = ksz9477_port_mdb_add, 384 - .port_mdb_del = ksz9477_port_mdb_del, 385 - .port_mirror_add = ksz9477_port_mirror_add, 386 - .port_mirror_del = ksz9477_port_mirror_del, 387 - }; 388 - 389 - struct ksz_chip_data { 390 - u32 chip_id; 391 - const char *dev_name; 392 - int num_vlans; 393 - int num_alus; 394 - int num_statics; 395 - int cpu_ports; 396 - int port_cnt; 397 - }; 398 - 399 - static const struct ksz_chip_data ksz9477_switch_chips[] = { 400 - { 401 - .chip_id = 0x00947700, 402 - .dev_name = "KSZ9477", 403 - .num_vlans = 4096, 404 - .num_alus = 4096, 405 - .num_statics = 16, 406 - .cpu_ports = 0x7F, /* can be configured as cpu port */ 407 - .port_cnt = 7, /* total physical port count */ 408 - }, 409 - { 410 - .chip_id = 0x00989700, 411 - .dev_name = "KSZ9897", 412 - .num_vlans = 4096, 413 - .num_alus = 4096, 414 - .num_statics = 16, 415 - .cpu_ports = 0x7F, /* can be configured as cpu port */ 416 - .port_cnt = 7, /* total physical port count */ 417 - }, 418 - }; 419 - 420 - static int ksz9477_switch_init(struct ksz_device *dev) 421 - { 422 - int i; 423 - 424 - dev->ds->ops = &ksz_switch_ops; 425 - 426 - for (i = 0; i < ARRAY_SIZE(ksz9477_switch_chips); i++) { 427 - const struct ksz_chip_data *chip = &ksz9477_switch_chips[i]; 428 - 429 - if (dev->chip_id == chip->chip_id) { 430 - dev->name = chip->dev_name; 431 - dev->num_vlans = chip->num_vlans; 432 - dev->num_alus = chip->num_alus; 433 - dev->num_statics = chip->num_statics; 434 - dev->port_cnt = chip->port_cnt; 435 - dev->cpu_ports = chip->cpu_ports; 436 - 437 - break; 438 - } 439 - } 440 - 441 - /* no switch found */ 442 - if (!dev->port_cnt) 443 - return -ENODEV; 444 - 445 - return 0; 446 - } 948 + EXPORT_SYMBOL_GPL(ksz_disable_port); 447 949 448 950 struct ksz_device *ksz_switch_alloc(struct device *base, 449 951 const struct ksz_io_ops *ops, ··· 286 1158 } 287 1159 EXPORT_SYMBOL(ksz_switch_alloc); 288 1160 289 - int ksz_switch_detect(struct ksz_device *dev) 290 - { 291 - u8 data8; 292 - u32 id32; 293 - int ret; 294 - 295 - /* turn off SPI DO Edge select */ 296 - ret = ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8); 297 - if (ret) 298 - return ret; 299 - 300 - data8 &= ~SPI_AUTO_EDGE_DETECTION; 301 - ret = ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8); 302 - if (ret) 303 - return ret; 304 - 305 - /* read chip id */ 306 - ret = ksz_read32(dev, REG_CHIP_ID0__1, &id32); 307 - if (ret) 308 - return ret; 309 - 310 - dev->chip_id = id32; 311 - 312 - return 0; 313 - } 314 - EXPORT_SYMBOL(ksz_switch_detect); 315 - 316 - int ksz_switch_register(struct ksz_device *dev) 1161 + int ksz_switch_register(struct ksz_device *dev, 1162 + const struct ksz_dev_ops *ops) 317 1163 { 318 1164 int ret; 319 1165 ··· 299 1197 mutex_init(&dev->alu_mutex); 300 1198 mutex_init(&dev->vlan_mutex); 301 1199 302 - if (ksz_switch_detect(dev)) 1200 + dev->dev_ops = ops; 1201 + 1202 + if (dev->dev_ops->detect(dev)) 303 1203 return -EINVAL; 304 1204 305 - ret = ksz9477_switch_init(dev); 1205 + ret = dev->dev_ops->init(dev); 306 1206 if (ret) 307 1207 return ret; 308 1208 309 - return dsa_register_switch(dev->ds); 1209 + dev->interface = PHY_INTERFACE_MODE_MII; 1210 + if (dev->dev->of_node) { 1211 + ret = of_get_phy_mode(dev->dev->of_node); 1212 + if (ret >= 0) 1213 + dev->interface = ret; 1214 + } 1215 + 1216 + ret = dsa_register_switch(dev->ds); 1217 + if (ret) { 1218 + dev->dev_ops->exit(dev); 1219 + return ret; 1220 + } 1221 + 1222 + return 0; 310 1223 } 311 1224 EXPORT_SYMBOL(ksz_switch_register); 312 1225 313 1226 void ksz_switch_remove(struct ksz_device *dev) 314 1227 { 1228 + dev->dev_ops->exit(dev); 315 1229 dsa_unregister_switch(dev->ds); 316 1230 } 317 1231 EXPORT_SYMBOL(ksz_switch_remove);
+214
drivers/net/dsa/microchip/ksz_common.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 2 + * Microchip switch driver common header 3 + * 4 + * Copyright (C) 2017-2018 Microchip Technology Inc. 5 + */ 6 + 7 + #ifndef __KSZ_COMMON_H 8 + #define __KSZ_COMMON_H 9 + 10 + void ksz_update_port_member(struct ksz_device *dev, int port); 11 + 12 + /* Common DSA access functions */ 13 + 14 + int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg); 15 + int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val); 16 + int ksz_sset_count(struct dsa_switch *ds, int port, int sset); 17 + int ksz_port_bridge_join(struct dsa_switch *ds, int port, 18 + struct net_device *br); 19 + void ksz_port_bridge_leave(struct dsa_switch *ds, int port, 20 + struct net_device *br); 21 + void ksz_port_fast_age(struct dsa_switch *ds, int port); 22 + int ksz_port_vlan_prepare(struct dsa_switch *ds, int port, 23 + const struct switchdev_obj_port_vlan *vlan); 24 + int ksz_port_fdb_dump(struct dsa_switch *ds, int port, dsa_fdb_dump_cb_t *cb, 25 + void *data); 26 + int ksz_port_mdb_prepare(struct dsa_switch *ds, int port, 27 + const struct switchdev_obj_port_mdb *mdb); 28 + void ksz_port_mdb_add(struct dsa_switch *ds, int port, 29 + const struct switchdev_obj_port_mdb *mdb); 30 + int ksz_port_mdb_del(struct dsa_switch *ds, int port, 31 + const struct switchdev_obj_port_mdb *mdb); 32 + int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy); 33 + void ksz_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy); 34 + 35 + /* Common register access functions */ 36 + 37 + static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val) 38 + { 39 + int ret; 40 + 41 + mutex_lock(&dev->reg_mutex); 42 + ret = dev->ops->read8(dev, reg, val); 43 + mutex_unlock(&dev->reg_mutex); 44 + 45 + return ret; 46 + } 47 + 48 + static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val) 49 + { 50 + int ret; 51 + 52 + mutex_lock(&dev->reg_mutex); 53 + ret = dev->ops->read16(dev, reg, val); 54 + mutex_unlock(&dev->reg_mutex); 55 + 56 + return ret; 57 + } 58 + 59 + static inline int ksz_read24(struct ksz_device *dev, u32 reg, u32 *val) 60 + { 61 + int ret; 62 + 63 + mutex_lock(&dev->reg_mutex); 64 + ret = dev->ops->read24(dev, reg, val); 65 + mutex_unlock(&dev->reg_mutex); 66 + 67 + return ret; 68 + } 69 + 70 + static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val) 71 + { 72 + int ret; 73 + 74 + mutex_lock(&dev->reg_mutex); 75 + ret = dev->ops->read32(dev, reg, val); 76 + mutex_unlock(&dev->reg_mutex); 77 + 78 + return ret; 79 + } 80 + 81 + static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value) 82 + { 83 + int ret; 84 + 85 + mutex_lock(&dev->reg_mutex); 86 + ret = dev->ops->write8(dev, reg, value); 87 + mutex_unlock(&dev->reg_mutex); 88 + 89 + return ret; 90 + } 91 + 92 + static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value) 93 + { 94 + int ret; 95 + 96 + mutex_lock(&dev->reg_mutex); 97 + ret = dev->ops->write16(dev, reg, value); 98 + mutex_unlock(&dev->reg_mutex); 99 + 100 + return ret; 101 + } 102 + 103 + static inline int ksz_write24(struct ksz_device *dev, u32 reg, u32 value) 104 + { 105 + int ret; 106 + 107 + mutex_lock(&dev->reg_mutex); 108 + ret = dev->ops->write24(dev, reg, value); 109 + mutex_unlock(&dev->reg_mutex); 110 + 111 + return ret; 112 + } 113 + 114 + static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value) 115 + { 116 + int ret; 117 + 118 + mutex_lock(&dev->reg_mutex); 119 + ret = dev->ops->write32(dev, reg, value); 120 + mutex_unlock(&dev->reg_mutex); 121 + 122 + return ret; 123 + } 124 + 125 + static inline int ksz_get(struct ksz_device *dev, u32 reg, void *data, 126 + size_t len) 127 + { 128 + int ret; 129 + 130 + mutex_lock(&dev->reg_mutex); 131 + ret = dev->ops->get(dev, reg, data, len); 132 + mutex_unlock(&dev->reg_mutex); 133 + 134 + return ret; 135 + } 136 + 137 + static inline int ksz_set(struct ksz_device *dev, u32 reg, void *data, 138 + size_t len) 139 + { 140 + int ret; 141 + 142 + mutex_lock(&dev->reg_mutex); 143 + ret = dev->ops->set(dev, reg, data, len); 144 + mutex_unlock(&dev->reg_mutex); 145 + 146 + return ret; 147 + } 148 + 149 + static inline void ksz_pread8(struct ksz_device *dev, int port, int offset, 150 + u8 *data) 151 + { 152 + ksz_read8(dev, dev->dev_ops->get_port_addr(port, offset), data); 153 + } 154 + 155 + static inline void ksz_pread16(struct ksz_device *dev, int port, int offset, 156 + u16 *data) 157 + { 158 + ksz_read16(dev, dev->dev_ops->get_port_addr(port, offset), data); 159 + } 160 + 161 + static inline void ksz_pread32(struct ksz_device *dev, int port, int offset, 162 + u32 *data) 163 + { 164 + ksz_read32(dev, dev->dev_ops->get_port_addr(port, offset), data); 165 + } 166 + 167 + static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset, 168 + u8 data) 169 + { 170 + ksz_write8(dev, dev->dev_ops->get_port_addr(port, offset), data); 171 + } 172 + 173 + static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset, 174 + u16 data) 175 + { 176 + ksz_write16(dev, dev->dev_ops->get_port_addr(port, offset), data); 177 + } 178 + 179 + static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset, 180 + u32 data) 181 + { 182 + ksz_write32(dev, dev->dev_ops->get_port_addr(port, offset), data); 183 + } 184 + 185 + static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set) 186 + { 187 + u8 data; 188 + 189 + ksz_read8(dev, addr, &data); 190 + if (set) 191 + data |= bits; 192 + else 193 + data &= ~bits; 194 + ksz_write8(dev, addr, data); 195 + } 196 + 197 + static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits, 198 + bool set) 199 + { 200 + u32 addr; 201 + u8 data; 202 + 203 + addr = dev->dev_ops->get_port_addr(port, offset); 204 + ksz_read8(dev, addr, &data); 205 + 206 + if (set) 207 + data |= bits; 208 + else 209 + data &= ~bits; 210 + 211 + ksz_write8(dev, addr, data); 212 + } 213 + 214 + #endif
+97 -129
drivers/net/dsa/microchip/ksz_priv.h
··· 22 22 u32 table[3]; 23 23 }; 24 24 25 + struct ksz_port_mib { 26 + u8 cnt_ptr; 27 + u64 *counters; 28 + }; 29 + 30 + struct ksz_port { 31 + u16 member; 32 + u16 vid_member; 33 + int stp_state; 34 + struct phy_device phydev; 35 + 36 + u32 on:1; /* port is not disabled by hardware */ 37 + u32 phy:1; /* port has a PHY */ 38 + u32 fiber:1; /* port is fiber */ 39 + u32 sgmii:1; /* port is SGMII */ 40 + u32 force:1; 41 + u32 link_just_down:1; /* link just goes down */ 42 + 43 + struct ksz_port_mib mib; 44 + }; 45 + 25 46 struct ksz_device { 26 47 struct dsa_switch *ds; 27 48 struct ksz_platform_data *pdata; ··· 53 32 struct mutex alu_mutex; /* ALU access */ 54 33 struct mutex vlan_mutex; /* vlan access */ 55 34 const struct ksz_io_ops *ops; 35 + const struct ksz_dev_ops *dev_ops; 56 36 57 37 struct device *dev; 58 38 ··· 66 44 int num_statics; 67 45 int cpu_port; /* port connected to CPU */ 68 46 int cpu_ports; /* port bitmap can be cpu port */ 47 + int phy_port_cnt; 69 48 int port_cnt; 49 + int reg_mib_cnt; 50 + int mib_cnt; 51 + int mib_port_cnt; 52 + int last_port; /* ports after that not used */ 53 + phy_interface_t interface; 54 + u32 regs_size; 70 55 71 56 struct vlan_table *vlan_cache; 72 57 73 58 u64 mib_value[TOTAL_SWITCH_COUNTER_NUM]; 59 + 60 + u8 *txbuf; 61 + 62 + struct ksz_port *ports; 63 + struct timer_list mib_read_timer; 64 + struct work_struct mib_read; 65 + unsigned long mib_read_interval; 66 + u16 br_member; 67 + u16 member; 68 + u16 live_ports; 69 + u16 on_ports; /* ports enabled by DSA */ 70 + u16 rx_ports; 71 + u16 tx_ports; 72 + u16 mirror_rx; 73 + u16 mirror_tx; 74 + u32 features; /* chip specific features */ 75 + u32 overrides; /* chip functions set by user */ 76 + u16 host_mask; 77 + u16 port_mask; 74 78 }; 75 79 76 80 struct ksz_io_ops { ··· 108 60 int (*write16)(struct ksz_device *dev, u32 reg, u16 value); 109 61 int (*write24)(struct ksz_device *dev, u32 reg, u32 value); 110 62 int (*write32)(struct ksz_device *dev, u32 reg, u32 value); 111 - int (*phy_read16)(struct ksz_device *dev, int addr, int reg, 112 - u16 *value); 113 - int (*phy_write16)(struct ksz_device *dev, int addr, int reg, 114 - u16 value); 63 + int (*get)(struct ksz_device *dev, u32 reg, void *data, size_t len); 64 + int (*set)(struct ksz_device *dev, u32 reg, void *data, size_t len); 65 + }; 66 + 67 + struct alu_struct { 68 + /* entry 1 */ 69 + u8 is_static:1; 70 + u8 is_src_filter:1; 71 + u8 is_dst_filter:1; 72 + u8 prio_age:3; 73 + u32 _reserv_0_1:23; 74 + u8 mstp:3; 75 + /* entry 2 */ 76 + u8 is_override:1; 77 + u8 is_use_fid:1; 78 + u32 _reserv_1_1:23; 79 + u8 port_forward:7; 80 + /* entry 3 & 4*/ 81 + u32 _reserv_2_1:9; 82 + u8 fid:7; 83 + u8 mac[ETH_ALEN]; 84 + }; 85 + 86 + struct ksz_dev_ops { 87 + u32 (*get_port_addr)(int port, int offset); 88 + void (*cfg_port_member)(struct ksz_device *dev, int port, u8 member); 89 + void (*flush_dyn_mac_table)(struct ksz_device *dev, int port); 90 + void (*port_setup)(struct ksz_device *dev, int port, bool cpu_port); 91 + void (*r_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 *val); 92 + void (*w_phy)(struct ksz_device *dev, u16 phy, u16 reg, u16 val); 93 + int (*r_dyn_mac_table)(struct ksz_device *dev, u16 addr, u8 *mac_addr, 94 + u8 *fid, u8 *src_port, u8 *timestamp, 95 + u16 *entries); 96 + int (*r_sta_mac_table)(struct ksz_device *dev, u16 addr, 97 + struct alu_struct *alu); 98 + void (*w_sta_mac_table)(struct ksz_device *dev, u16 addr, 99 + struct alu_struct *alu); 100 + void (*r_mib_cnt)(struct ksz_device *dev, int port, u16 addr, 101 + u64 *cnt); 102 + void (*r_mib_pkt)(struct ksz_device *dev, int port, u16 addr, 103 + u64 *dropped, u64 *cnt); 104 + void (*port_init_cnt)(struct ksz_device *dev, int port); 105 + int (*shutdown)(struct ksz_device *dev); 106 + int (*detect)(struct ksz_device *dev); 107 + int (*init)(struct ksz_device *dev); 108 + void (*exit)(struct ksz_device *dev); 115 109 }; 116 110 117 111 struct ksz_device *ksz_switch_alloc(struct device *base, 118 112 const struct ksz_io_ops *ops, void *priv); 119 - int ksz_switch_detect(struct ksz_device *dev); 120 - int ksz_switch_register(struct ksz_device *dev); 113 + int ksz_switch_register(struct ksz_device *dev, 114 + const struct ksz_dev_ops *ops); 121 115 void ksz_switch_remove(struct ksz_device *dev); 122 116 123 - static inline int ksz_read8(struct ksz_device *dev, u32 reg, u8 *val) 124 - { 125 - int ret; 126 - 127 - mutex_lock(&dev->reg_mutex); 128 - ret = dev->ops->read8(dev, reg, val); 129 - mutex_unlock(&dev->reg_mutex); 130 - 131 - return ret; 132 - } 133 - 134 - static inline int ksz_read16(struct ksz_device *dev, u32 reg, u16 *val) 135 - { 136 - int ret; 137 - 138 - mutex_lock(&dev->reg_mutex); 139 - ret = dev->ops->read16(dev, reg, val); 140 - mutex_unlock(&dev->reg_mutex); 141 - 142 - return ret; 143 - } 144 - 145 - static inline int ksz_read24(struct ksz_device *dev, u32 reg, u32 *val) 146 - { 147 - int ret; 148 - 149 - mutex_lock(&dev->reg_mutex); 150 - ret = dev->ops->read24(dev, reg, val); 151 - mutex_unlock(&dev->reg_mutex); 152 - 153 - return ret; 154 - } 155 - 156 - static inline int ksz_read32(struct ksz_device *dev, u32 reg, u32 *val) 157 - { 158 - int ret; 159 - 160 - mutex_lock(&dev->reg_mutex); 161 - ret = dev->ops->read32(dev, reg, val); 162 - mutex_unlock(&dev->reg_mutex); 163 - 164 - return ret; 165 - } 166 - 167 - static inline int ksz_write8(struct ksz_device *dev, u32 reg, u8 value) 168 - { 169 - int ret; 170 - 171 - mutex_lock(&dev->reg_mutex); 172 - ret = dev->ops->write8(dev, reg, value); 173 - mutex_unlock(&dev->reg_mutex); 174 - 175 - return ret; 176 - } 177 - 178 - static inline int ksz_write16(struct ksz_device *dev, u32 reg, u16 value) 179 - { 180 - int ret; 181 - 182 - mutex_lock(&dev->reg_mutex); 183 - ret = dev->ops->write16(dev, reg, value); 184 - mutex_unlock(&dev->reg_mutex); 185 - 186 - return ret; 187 - } 188 - 189 - static inline int ksz_write24(struct ksz_device *dev, u32 reg, u32 value) 190 - { 191 - int ret; 192 - 193 - mutex_lock(&dev->reg_mutex); 194 - ret = dev->ops->write24(dev, reg, value); 195 - mutex_unlock(&dev->reg_mutex); 196 - 197 - return ret; 198 - } 199 - 200 - static inline int ksz_write32(struct ksz_device *dev, u32 reg, u32 value) 201 - { 202 - int ret; 203 - 204 - mutex_lock(&dev->reg_mutex); 205 - ret = dev->ops->write32(dev, reg, value); 206 - mutex_unlock(&dev->reg_mutex); 207 - 208 - return ret; 209 - } 210 - 211 - static inline void ksz_pread8(struct ksz_device *dev, int port, int offset, 212 - u8 *data) 213 - { 214 - ksz_read8(dev, PORT_CTRL_ADDR(port, offset), data); 215 - } 216 - 217 - static inline void ksz_pread16(struct ksz_device *dev, int port, int offset, 218 - u16 *data) 219 - { 220 - ksz_read16(dev, PORT_CTRL_ADDR(port, offset), data); 221 - } 222 - 223 - static inline void ksz_pread32(struct ksz_device *dev, int port, int offset, 224 - u32 *data) 225 - { 226 - ksz_read32(dev, PORT_CTRL_ADDR(port, offset), data); 227 - } 228 - 229 - static inline void ksz_pwrite8(struct ksz_device *dev, int port, int offset, 230 - u8 data) 231 - { 232 - ksz_write8(dev, PORT_CTRL_ADDR(port, offset), data); 233 - } 234 - 235 - static inline void ksz_pwrite16(struct ksz_device *dev, int port, int offset, 236 - u16 data) 237 - { 238 - ksz_write16(dev, PORT_CTRL_ADDR(port, offset), data); 239 - } 240 - 241 - static inline void ksz_pwrite32(struct ksz_device *dev, int port, int offset, 242 - u32 data) 243 - { 244 - ksz_write32(dev, PORT_CTRL_ADDR(port, offset), data); 245 - } 117 + int ksz9477_switch_register(struct ksz_device *dev); 246 118 247 119 #endif
+69
drivers/net/dsa/microchip/ksz_spi.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 2 + * Microchip KSZ series SPI access common header 3 + * 4 + * Copyright (C) 2017-2018 Microchip Technology Inc. 5 + * Tristram Ha <Tristram.Ha@microchip.com> 6 + */ 7 + 8 + #ifndef __KSZ_SPI_H 9 + #define __KSZ_SPI_H 10 + 11 + /* Chip dependent SPI access */ 12 + static int ksz_spi_read(struct ksz_device *dev, u32 reg, u8 *data, 13 + unsigned int len); 14 + static int ksz_spi_write(struct ksz_device *dev, u32 reg, void *data, 15 + unsigned int len); 16 + 17 + static int ksz_spi_read8(struct ksz_device *dev, u32 reg, u8 *val) 18 + { 19 + return ksz_spi_read(dev, reg, val, 1); 20 + } 21 + 22 + static int ksz_spi_read16(struct ksz_device *dev, u32 reg, u16 *val) 23 + { 24 + int ret = ksz_spi_read(dev, reg, (u8 *)val, 2); 25 + 26 + if (!ret) 27 + *val = be16_to_cpu(*val); 28 + 29 + return ret; 30 + } 31 + 32 + static int ksz_spi_read32(struct ksz_device *dev, u32 reg, u32 *val) 33 + { 34 + int ret = ksz_spi_read(dev, reg, (u8 *)val, 4); 35 + 36 + if (!ret) 37 + *val = be32_to_cpu(*val); 38 + 39 + return ret; 40 + } 41 + 42 + static int ksz_spi_write8(struct ksz_device *dev, u32 reg, u8 value) 43 + { 44 + return ksz_spi_write(dev, reg, &value, 1); 45 + } 46 + 47 + static int ksz_spi_write16(struct ksz_device *dev, u32 reg, u16 value) 48 + { 49 + value = cpu_to_be16(value); 50 + return ksz_spi_write(dev, reg, &value, 2); 51 + } 52 + 53 + static int ksz_spi_write32(struct ksz_device *dev, u32 reg, u32 value) 54 + { 55 + value = cpu_to_be32(value); 56 + return ksz_spi_write(dev, reg, &value, 4); 57 + } 58 + 59 + static int ksz_spi_get(struct ksz_device *dev, u32 reg, void *data, size_t len) 60 + { 61 + return ksz_spi_read(dev, reg, data, len); 62 + } 63 + 64 + static int ksz_spi_set(struct ksz_device *dev, u32 reg, void *data, size_t len) 65 + { 66 + return ksz_spi_write(dev, reg, data, len); 67 + } 68 + 69 + #endif