1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Microchip switch driver main logic 4 * 5 * Copyright (C) 2017-2024 Microchip Technology Inc. 6 */ 7 8 #include <linux/delay.h> 9 #include <linux/dsa/ksz_common.h> 10 #include <linux/export.h> 11 #include <linux/gpio/consumer.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/platform_data/microchip-ksz.h> 15 #include <linux/phy.h> 16 #include <linux/etherdevice.h> 17 #include <linux/if_bridge.h> 18 #include <linux/if_vlan.h> 19 #include <linux/if_hsr.h> 20 #include <linux/irq.h> 21 #include <linux/irqdomain.h> 22 #include <linux/of.h> 23 #include <linux/of_mdio.h> 24 #include <linux/of_net.h> 25 #include <linux/micrel_phy.h> 26 #include <net/dsa.h> 27 #include <net/ieee8021q.h> 28 #include <net/pkt_cls.h> 29 #include <net/switchdev.h> 30 31 #include "ksz_common.h" 32 #include "ksz_dcb.h" 33 #include "ksz_ptp.h" 34 #include "ksz8.h" 35 #include "ksz9477.h" 36 #include "lan937x.h" 37 38 #define MIB_COUNTER_NUM 0x20 39 40 struct ksz_stats_raw { 41 u64 rx_hi; 42 u64 rx_undersize; 43 u64 rx_fragments; 44 u64 rx_oversize; 45 u64 rx_jabbers; 46 u64 rx_symbol_err; 47 u64 rx_crc_err; 48 u64 rx_align_err; 49 u64 rx_mac_ctrl; 50 u64 rx_pause; 51 u64 rx_bcast; 52 u64 rx_mcast; 53 u64 rx_ucast; 54 u64 rx_64_or_less; 55 u64 rx_65_127; 56 u64 rx_128_255; 57 u64 rx_256_511; 58 u64 rx_512_1023; 59 u64 rx_1024_1522; 60 u64 rx_1523_2000; 61 u64 rx_2001; 62 u64 tx_hi; 63 u64 tx_late_col; 64 u64 tx_pause; 65 u64 tx_bcast; 66 u64 tx_mcast; 67 u64 tx_ucast; 68 u64 tx_deferred; 69 u64 tx_total_col; 70 u64 tx_exc_col; 71 u64 tx_single_col; 72 u64 tx_mult_col; 73 u64 rx_total; 74 u64 tx_total; 75 u64 rx_discards; 76 u64 tx_discards; 77 }; 78 79 struct ksz88xx_stats_raw { 80 u64 rx; 81 u64 rx_hi; 82 u64 rx_undersize; 83 u64 rx_fragments; 84 u64 rx_oversize; 85 u64 rx_jabbers; 86 u64 rx_symbol_err; 87 u64 rx_crc_err; 88 u64 rx_align_err; 89 u64 rx_mac_ctrl; 90 u64 rx_pause; 91 u64 rx_bcast; 92 u64 rx_mcast; 93 u64 rx_ucast; 94 u64 rx_64_or_less; 95 u64 rx_65_127; 96 u64 rx_128_255; 97 u64 rx_256_511; 98 u64 rx_512_1023; 99 u64 rx_1024_1522; 100 u64 tx; 101 u64 tx_hi; 102 u64 tx_late_col; 103 u64 tx_pause; 104 u64 tx_bcast; 105 u64 tx_mcast; 106 u64 tx_ucast; 107 u64 tx_deferred; 108 u64 tx_total_col; 109 u64 tx_exc_col; 110 u64 tx_single_col; 111 u64 tx_mult_col; 112 u64 rx_discards; 113 u64 tx_discards; 114 }; 115 116 static const struct ksz_mib_names ksz88xx_mib_names[] = { 117 { 0x00, "rx" }, 118 { 0x01, "rx_hi" }, 119 { 0x02, "rx_undersize" }, 120 { 0x03, "rx_fragments" }, 121 { 0x04, "rx_oversize" }, 122 { 0x05, "rx_jabbers" }, 123 { 0x06, "rx_symbol_err" }, 124 { 0x07, "rx_crc_err" }, 125 { 0x08, "rx_align_err" }, 126 { 0x09, "rx_mac_ctrl" }, 127 { 0x0a, "rx_pause" }, 128 { 0x0b, "rx_bcast" }, 129 { 0x0c, "rx_mcast" }, 130 { 0x0d, "rx_ucast" }, 131 { 0x0e, "rx_64_or_less" }, 132 { 0x0f, "rx_65_127" }, 133 { 0x10, "rx_128_255" }, 134 { 0x11, "rx_256_511" }, 135 { 0x12, "rx_512_1023" }, 136 { 0x13, "rx_1024_1522" }, 137 { 0x14, "tx" }, 138 { 0x15, "tx_hi" }, 139 { 0x16, "tx_late_col" }, 140 { 0x17, "tx_pause" }, 141 { 0x18, "tx_bcast" }, 142 { 0x19, "tx_mcast" }, 143 { 0x1a, "tx_ucast" }, 144 { 0x1b, "tx_deferred" }, 145 { 0x1c, "tx_total_col" }, 146 { 0x1d, "tx_exc_col" }, 147 { 0x1e, "tx_single_col" }, 148 { 0x1f, "tx_mult_col" }, 149 { 0x100, "rx_discards" }, 150 { 0x101, "tx_discards" }, 151 }; 152 153 static const struct ksz_mib_names ksz9477_mib_names[] = { 154 { 0x00, "rx_hi" }, 155 { 0x01, "rx_undersize" }, 156 { 0x02, "rx_fragments" }, 157 { 0x03, "rx_oversize" }, 158 { 0x04, "rx_jabbers" }, 159 { 0x05, "rx_symbol_err" }, 160 { 0x06, "rx_crc_err" }, 161 { 0x07, "rx_align_err" }, 162 { 0x08, "rx_mac_ctrl" }, 163 { 0x09, "rx_pause" }, 164 { 0x0A, "rx_bcast" }, 165 { 0x0B, "rx_mcast" }, 166 { 0x0C, "rx_ucast" }, 167 { 0x0D, "rx_64_or_less" }, 168 { 0x0E, "rx_65_127" }, 169 { 0x0F, "rx_128_255" }, 170 { 0x10, "rx_256_511" }, 171 { 0x11, "rx_512_1023" }, 172 { 0x12, "rx_1024_1522" }, 173 { 0x13, "rx_1523_2000" }, 174 { 0x14, "rx_2001" }, 175 { 0x15, "tx_hi" }, 176 { 0x16, "tx_late_col" }, 177 { 0x17, "tx_pause" }, 178 { 0x18, "tx_bcast" }, 179 { 0x19, "tx_mcast" }, 180 { 0x1A, "tx_ucast" }, 181 { 0x1B, "tx_deferred" }, 182 { 0x1C, "tx_total_col" }, 183 { 0x1D, "tx_exc_col" }, 184 { 0x1E, "tx_single_col" }, 185 { 0x1F, "tx_mult_col" }, 186 { 0x80, "rx_total" }, 187 { 0x81, "tx_total" }, 188 { 0x82, "rx_discards" }, 189 { 0x83, "tx_discards" }, 190 }; 191 192 struct ksz_driver_strength_prop { 193 const char *name; 194 int offset; 195 int value; 196 }; 197 198 enum ksz_driver_strength_type { 199 KSZ_DRIVER_STRENGTH_HI, 200 KSZ_DRIVER_STRENGTH_LO, 201 KSZ_DRIVER_STRENGTH_IO, 202 }; 203 204 /** 205 * struct ksz_drive_strength - drive strength mapping 206 * @reg_val: register value 207 * @microamp: microamp value 208 */ 209 struct ksz_drive_strength { 210 u32 reg_val; 211 u32 microamp; 212 }; 213 214 /* ksz9477_drive_strengths - Drive strength mapping for KSZ9477 variants 215 * 216 * This values are not documented in KSZ9477 variants but confirmed by 217 * Microchip that KSZ9477, KSZ9567, KSZ8567, KSZ9897, KSZ9896, KSZ9563, KSZ9893 218 * and KSZ8563 are using same register (drive strength) settings like KSZ8795. 219 * 220 * Documentation in KSZ8795CLX provides more information with some 221 * recommendations: 222 * - for high speed signals 223 * 1. 4 mA or 8 mA is often used for MII, RMII, and SPI interface with using 224 * 2.5V or 3.3V VDDIO. 225 * 2. 12 mA or 16 mA is often used for MII, RMII, and SPI interface with 226 * using 1.8V VDDIO. 227 * 3. 20 mA or 24 mA is often used for GMII/RGMII interface with using 2.5V 228 * or 3.3V VDDIO. 229 * 4. 28 mA is often used for GMII/RGMII interface with using 1.8V VDDIO. 230 * 5. In same interface, the heavy loading should use higher one of the 231 * drive current strength. 232 * - for low speed signals 233 * 1. 3.3V VDDIO, use either 4 mA or 8 mA. 234 * 2. 2.5V VDDIO, use either 8 mA or 12 mA. 235 * 3. 1.8V VDDIO, use either 12 mA or 16 mA. 236 * 4. If it is heavy loading, can use higher drive current strength. 237 */ 238 static const struct ksz_drive_strength ksz9477_drive_strengths[] = { 239 { SW_DRIVE_STRENGTH_2MA, 2000 }, 240 { SW_DRIVE_STRENGTH_4MA, 4000 }, 241 { SW_DRIVE_STRENGTH_8MA, 8000 }, 242 { SW_DRIVE_STRENGTH_12MA, 12000 }, 243 { SW_DRIVE_STRENGTH_16MA, 16000 }, 244 { SW_DRIVE_STRENGTH_20MA, 20000 }, 245 { SW_DRIVE_STRENGTH_24MA, 24000 }, 246 { SW_DRIVE_STRENGTH_28MA, 28000 }, 247 }; 248 249 /* ksz88x3_drive_strengths - Drive strength mapping for KSZ8863, KSZ8873, .. 250 * variants. 251 * This values are documented in KSZ8873 and KSZ8863 datasheets. 252 */ 253 static const struct ksz_drive_strength ksz88x3_drive_strengths[] = { 254 { 0, 8000 }, 255 { KSZ8873_DRIVE_STRENGTH_16MA, 16000 }, 256 }; 257 258 static void ksz88x3_phylink_mac_config(struct phylink_config *config, 259 unsigned int mode, 260 const struct phylink_link_state *state); 261 static void ksz_phylink_mac_config(struct phylink_config *config, 262 unsigned int mode, 263 const struct phylink_link_state *state); 264 static void ksz_phylink_mac_link_down(struct phylink_config *config, 265 unsigned int mode, 266 phy_interface_t interface); 267 268 static const struct phylink_mac_ops ksz88x3_phylink_mac_ops = { 269 .mac_config = ksz88x3_phylink_mac_config, 270 .mac_link_down = ksz_phylink_mac_link_down, 271 .mac_link_up = ksz8_phylink_mac_link_up, 272 }; 273 274 static const struct phylink_mac_ops ksz8_phylink_mac_ops = { 275 .mac_config = ksz_phylink_mac_config, 276 .mac_link_down = ksz_phylink_mac_link_down, 277 .mac_link_up = ksz8_phylink_mac_link_up, 278 }; 279 280 static const struct ksz_dev_ops ksz88xx_dev_ops = { 281 .setup = ksz8_setup, 282 .get_port_addr = ksz8_get_port_addr, 283 .cfg_port_member = ksz8_cfg_port_member, 284 .flush_dyn_mac_table = ksz8_flush_dyn_mac_table, 285 .port_setup = ksz8_port_setup, 286 .r_phy = ksz8_r_phy, 287 .w_phy = ksz8_w_phy, 288 .r_mib_cnt = ksz8_r_mib_cnt, 289 .r_mib_pkt = ksz8_r_mib_pkt, 290 .r_mib_stat64 = ksz88xx_r_mib_stats64, 291 .freeze_mib = ksz8_freeze_mib, 292 .port_init_cnt = ksz8_port_init_cnt, 293 .fdb_dump = ksz8_fdb_dump, 294 .fdb_add = ksz8_fdb_add, 295 .fdb_del = ksz8_fdb_del, 296 .mdb_add = ksz8_mdb_add, 297 .mdb_del = ksz8_mdb_del, 298 .vlan_filtering = ksz8_port_vlan_filtering, 299 .vlan_add = ksz8_port_vlan_add, 300 .vlan_del = ksz8_port_vlan_del, 301 .mirror_add = ksz8_port_mirror_add, 302 .mirror_del = ksz8_port_mirror_del, 303 .get_caps = ksz8_get_caps, 304 .config_cpu_port = ksz8_config_cpu_port, 305 .enable_stp_addr = ksz8_enable_stp_addr, 306 .reset = ksz8_reset_switch, 307 .init = ksz8_switch_init, 308 .exit = ksz8_switch_exit, 309 .change_mtu = ksz8_change_mtu, 310 .pme_write8 = ksz8_pme_write8, 311 .pme_pread8 = ksz8_pme_pread8, 312 .pme_pwrite8 = ksz8_pme_pwrite8, 313 }; 314 315 static const struct ksz_dev_ops ksz87xx_dev_ops = { 316 .setup = ksz8_setup, 317 .get_port_addr = ksz8_get_port_addr, 318 .cfg_port_member = ksz8_cfg_port_member, 319 .flush_dyn_mac_table = ksz8_flush_dyn_mac_table, 320 .port_setup = ksz8_port_setup, 321 .r_phy = ksz8_r_phy, 322 .w_phy = ksz8_w_phy, 323 .r_mib_cnt = ksz8_r_mib_cnt, 324 .r_mib_pkt = ksz8_r_mib_pkt, 325 .r_mib_stat64 = ksz_r_mib_stats64, 326 .freeze_mib = ksz8_freeze_mib, 327 .port_init_cnt = ksz8_port_init_cnt, 328 .fdb_dump = ksz8_fdb_dump, 329 .fdb_add = ksz8_fdb_add, 330 .fdb_del = ksz8_fdb_del, 331 .mdb_add = ksz8_mdb_add, 332 .mdb_del = ksz8_mdb_del, 333 .vlan_filtering = ksz8_port_vlan_filtering, 334 .vlan_add = ksz8_port_vlan_add, 335 .vlan_del = ksz8_port_vlan_del, 336 .mirror_add = ksz8_port_mirror_add, 337 .mirror_del = ksz8_port_mirror_del, 338 .get_caps = ksz8_get_caps, 339 .config_cpu_port = ksz8_config_cpu_port, 340 .enable_stp_addr = ksz8_enable_stp_addr, 341 .reset = ksz8_reset_switch, 342 .init = ksz8_switch_init, 343 .exit = ksz8_switch_exit, 344 .change_mtu = ksz8_change_mtu, 345 .pme_write8 = ksz8_pme_write8, 346 .pme_pread8 = ksz8_pme_pread8, 347 .pme_pwrite8 = ksz8_pme_pwrite8, 348 }; 349 350 static void ksz9477_phylink_mac_link_up(struct phylink_config *config, 351 struct phy_device *phydev, 352 unsigned int mode, 353 phy_interface_t interface, 354 int speed, int duplex, bool tx_pause, 355 bool rx_pause); 356 357 static const struct phylink_mac_ops ksz9477_phylink_mac_ops = { 358 .mac_config = ksz_phylink_mac_config, 359 .mac_link_down = ksz_phylink_mac_link_down, 360 .mac_link_up = ksz9477_phylink_mac_link_up, 361 }; 362 363 static const struct ksz_dev_ops ksz9477_dev_ops = { 364 .setup = ksz9477_setup, 365 .get_port_addr = ksz9477_get_port_addr, 366 .cfg_port_member = ksz9477_cfg_port_member, 367 .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table, 368 .port_setup = ksz9477_port_setup, 369 .set_ageing_time = ksz9477_set_ageing_time, 370 .r_phy = ksz9477_r_phy, 371 .w_phy = ksz9477_w_phy, 372 .r_mib_cnt = ksz9477_r_mib_cnt, 373 .r_mib_pkt = ksz9477_r_mib_pkt, 374 .r_mib_stat64 = ksz_r_mib_stats64, 375 .freeze_mib = ksz9477_freeze_mib, 376 .port_init_cnt = ksz9477_port_init_cnt, 377 .vlan_filtering = ksz9477_port_vlan_filtering, 378 .vlan_add = ksz9477_port_vlan_add, 379 .vlan_del = ksz9477_port_vlan_del, 380 .mirror_add = ksz9477_port_mirror_add, 381 .mirror_del = ksz9477_port_mirror_del, 382 .get_caps = ksz9477_get_caps, 383 .fdb_dump = ksz9477_fdb_dump, 384 .fdb_add = ksz9477_fdb_add, 385 .fdb_del = ksz9477_fdb_del, 386 .mdb_add = ksz9477_mdb_add, 387 .mdb_del = ksz9477_mdb_del, 388 .change_mtu = ksz9477_change_mtu, 389 .pme_write8 = ksz_write8, 390 .pme_pread8 = ksz_pread8, 391 .pme_pwrite8 = ksz_pwrite8, 392 .config_cpu_port = ksz9477_config_cpu_port, 393 .tc_cbs_set_cinc = ksz9477_tc_cbs_set_cinc, 394 .enable_stp_addr = ksz9477_enable_stp_addr, 395 .reset = ksz9477_reset_switch, 396 .init = ksz9477_switch_init, 397 .exit = ksz9477_switch_exit, 398 }; 399 400 static const struct phylink_mac_ops lan937x_phylink_mac_ops = { 401 .mac_config = ksz_phylink_mac_config, 402 .mac_link_down = ksz_phylink_mac_link_down, 403 .mac_link_up = ksz9477_phylink_mac_link_up, 404 }; 405 406 static const struct ksz_dev_ops lan937x_dev_ops = { 407 .setup = lan937x_setup, 408 .teardown = lan937x_teardown, 409 .get_port_addr = ksz9477_get_port_addr, 410 .cfg_port_member = ksz9477_cfg_port_member, 411 .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table, 412 .port_setup = lan937x_port_setup, 413 .set_ageing_time = lan937x_set_ageing_time, 414 .mdio_bus_preinit = lan937x_mdio_bus_preinit, 415 .create_phy_addr_map = lan937x_create_phy_addr_map, 416 .r_phy = lan937x_r_phy, 417 .w_phy = lan937x_w_phy, 418 .r_mib_cnt = ksz9477_r_mib_cnt, 419 .r_mib_pkt = ksz9477_r_mib_pkt, 420 .r_mib_stat64 = ksz_r_mib_stats64, 421 .freeze_mib = ksz9477_freeze_mib, 422 .port_init_cnt = ksz9477_port_init_cnt, 423 .vlan_filtering = ksz9477_port_vlan_filtering, 424 .vlan_add = ksz9477_port_vlan_add, 425 .vlan_del = ksz9477_port_vlan_del, 426 .mirror_add = ksz9477_port_mirror_add, 427 .mirror_del = ksz9477_port_mirror_del, 428 .get_caps = lan937x_phylink_get_caps, 429 .setup_rgmii_delay = lan937x_setup_rgmii_delay, 430 .fdb_dump = ksz9477_fdb_dump, 431 .fdb_add = ksz9477_fdb_add, 432 .fdb_del = ksz9477_fdb_del, 433 .mdb_add = ksz9477_mdb_add, 434 .mdb_del = ksz9477_mdb_del, 435 .change_mtu = lan937x_change_mtu, 436 .config_cpu_port = lan937x_config_cpu_port, 437 .tc_cbs_set_cinc = lan937x_tc_cbs_set_cinc, 438 .enable_stp_addr = ksz9477_enable_stp_addr, 439 .reset = lan937x_reset_switch, 440 .init = lan937x_switch_init, 441 .exit = lan937x_switch_exit, 442 }; 443 444 static const u16 ksz8795_regs[] = { 445 [REG_SW_MAC_ADDR] = 0x68, 446 [REG_IND_CTRL_0] = 0x6E, 447 [REG_IND_DATA_8] = 0x70, 448 [REG_IND_DATA_CHECK] = 0x72, 449 [REG_IND_DATA_HI] = 0x71, 450 [REG_IND_DATA_LO] = 0x75, 451 [REG_IND_MIB_CHECK] = 0x74, 452 [REG_IND_BYTE] = 0xA0, 453 [P_FORCE_CTRL] = 0x0C, 454 [P_LINK_STATUS] = 0x0E, 455 [P_LOCAL_CTRL] = 0x07, 456 [P_NEG_RESTART_CTRL] = 0x0D, 457 [P_REMOTE_STATUS] = 0x08, 458 [P_SPEED_STATUS] = 0x09, 459 [S_TAIL_TAG_CTRL] = 0x0C, 460 [P_STP_CTRL] = 0x02, 461 [S_START_CTRL] = 0x01, 462 [S_BROADCAST_CTRL] = 0x06, 463 [S_MULTICAST_CTRL] = 0x04, 464 [P_XMII_CTRL_0] = 0x06, 465 [P_XMII_CTRL_1] = 0x06, 466 [REG_SW_PME_CTRL] = 0x8003, 467 [REG_PORT_PME_STATUS] = 0x8003, 468 [REG_PORT_PME_CTRL] = 0x8007, 469 }; 470 471 static const u32 ksz8795_masks[] = { 472 [PORT_802_1P_REMAPPING] = BIT(7), 473 [SW_TAIL_TAG_ENABLE] = BIT(1), 474 [MIB_COUNTER_OVERFLOW] = BIT(6), 475 [MIB_COUNTER_VALID] = BIT(5), 476 [VLAN_TABLE_FID] = GENMASK(6, 0), 477 [VLAN_TABLE_MEMBERSHIP] = GENMASK(11, 7), 478 [VLAN_TABLE_VALID] = BIT(12), 479 [STATIC_MAC_TABLE_VALID] = BIT(21), 480 [STATIC_MAC_TABLE_USE_FID] = BIT(23), 481 [STATIC_MAC_TABLE_FID] = GENMASK(30, 24), 482 [STATIC_MAC_TABLE_OVERRIDE] = BIT(22), 483 [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(20, 16), 484 [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(6, 0), 485 [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(7), 486 [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7), 487 [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 29), 488 [DYNAMIC_MAC_TABLE_FID] = GENMASK(22, 16), 489 [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(26, 24), 490 [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(28, 27), 491 [P_MII_TX_FLOW_CTRL] = BIT(5), 492 [P_MII_RX_FLOW_CTRL] = BIT(5), 493 }; 494 495 static const u8 ksz8795_xmii_ctrl0[] = { 496 [P_MII_100MBIT] = 0, 497 [P_MII_10MBIT] = 1, 498 [P_MII_FULL_DUPLEX] = 0, 499 [P_MII_HALF_DUPLEX] = 1, 500 }; 501 502 static const u8 ksz8795_xmii_ctrl1[] = { 503 [P_RGMII_SEL] = 3, 504 [P_GMII_SEL] = 2, 505 [P_RMII_SEL] = 1, 506 [P_MII_SEL] = 0, 507 [P_GMII_1GBIT] = 1, 508 [P_GMII_NOT_1GBIT] = 0, 509 }; 510 511 static const u8 ksz8795_shifts[] = { 512 [VLAN_TABLE_MEMBERSHIP_S] = 7, 513 [VLAN_TABLE] = 16, 514 [STATIC_MAC_FWD_PORTS] = 16, 515 [STATIC_MAC_FID] = 24, 516 [DYNAMIC_MAC_ENTRIES_H] = 3, 517 [DYNAMIC_MAC_ENTRIES] = 29, 518 [DYNAMIC_MAC_FID] = 16, 519 [DYNAMIC_MAC_TIMESTAMP] = 27, 520 [DYNAMIC_MAC_SRC_PORT] = 24, 521 }; 522 523 static const u16 ksz8863_regs[] = { 524 [REG_SW_MAC_ADDR] = 0x70, 525 [REG_IND_CTRL_0] = 0x79, 526 [REG_IND_DATA_8] = 0x7B, 527 [REG_IND_DATA_CHECK] = 0x7B, 528 [REG_IND_DATA_HI] = 0x7C, 529 [REG_IND_DATA_LO] = 0x80, 530 [REG_IND_MIB_CHECK] = 0x80, 531 [P_FORCE_CTRL] = 0x0C, 532 [P_LINK_STATUS] = 0x0E, 533 [P_LOCAL_CTRL] = 0x0C, 534 [P_NEG_RESTART_CTRL] = 0x0D, 535 [P_REMOTE_STATUS] = 0x0E, 536 [P_SPEED_STATUS] = 0x0F, 537 [S_TAIL_TAG_CTRL] = 0x03, 538 [P_STP_CTRL] = 0x02, 539 [S_START_CTRL] = 0x01, 540 [S_BROADCAST_CTRL] = 0x06, 541 [S_MULTICAST_CTRL] = 0x04, 542 }; 543 544 static const u32 ksz8863_masks[] = { 545 [PORT_802_1P_REMAPPING] = BIT(3), 546 [SW_TAIL_TAG_ENABLE] = BIT(6), 547 [MIB_COUNTER_OVERFLOW] = BIT(7), 548 [MIB_COUNTER_VALID] = BIT(6), 549 [VLAN_TABLE_FID] = GENMASK(15, 12), 550 [VLAN_TABLE_MEMBERSHIP] = GENMASK(18, 16), 551 [VLAN_TABLE_VALID] = BIT(19), 552 [STATIC_MAC_TABLE_VALID] = BIT(19), 553 [STATIC_MAC_TABLE_USE_FID] = BIT(21), 554 [STATIC_MAC_TABLE_FID] = GENMASK(25, 22), 555 [STATIC_MAC_TABLE_OVERRIDE] = BIT(20), 556 [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(18, 16), 557 [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(1, 0), 558 [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(2), 559 [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7), 560 [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 24), 561 [DYNAMIC_MAC_TABLE_FID] = GENMASK(19, 16), 562 [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(21, 20), 563 [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(23, 22), 564 }; 565 566 static u8 ksz8863_shifts[] = { 567 [VLAN_TABLE_MEMBERSHIP_S] = 16, 568 [STATIC_MAC_FWD_PORTS] = 16, 569 [STATIC_MAC_FID] = 22, 570 [DYNAMIC_MAC_ENTRIES_H] = 8, 571 [DYNAMIC_MAC_ENTRIES] = 24, 572 [DYNAMIC_MAC_FID] = 16, 573 [DYNAMIC_MAC_TIMESTAMP] = 22, 574 [DYNAMIC_MAC_SRC_PORT] = 20, 575 }; 576 577 static const u16 ksz8895_regs[] = { 578 [REG_SW_MAC_ADDR] = 0x68, 579 [REG_IND_CTRL_0] = 0x6E, 580 [REG_IND_DATA_8] = 0x70, 581 [REG_IND_DATA_CHECK] = 0x72, 582 [REG_IND_DATA_HI] = 0x71, 583 [REG_IND_DATA_LO] = 0x75, 584 [REG_IND_MIB_CHECK] = 0x75, 585 [P_FORCE_CTRL] = 0x0C, 586 [P_LINK_STATUS] = 0x0E, 587 [P_LOCAL_CTRL] = 0x0C, 588 [P_NEG_RESTART_CTRL] = 0x0D, 589 [P_REMOTE_STATUS] = 0x0E, 590 [P_SPEED_STATUS] = 0x09, 591 [S_TAIL_TAG_CTRL] = 0x0C, 592 [P_STP_CTRL] = 0x02, 593 [S_START_CTRL] = 0x01, 594 [S_BROADCAST_CTRL] = 0x06, 595 [S_MULTICAST_CTRL] = 0x04, 596 }; 597 598 static const u32 ksz8895_masks[] = { 599 [PORT_802_1P_REMAPPING] = BIT(7), 600 [SW_TAIL_TAG_ENABLE] = BIT(1), 601 [MIB_COUNTER_OVERFLOW] = BIT(7), 602 [MIB_COUNTER_VALID] = BIT(6), 603 [VLAN_TABLE_FID] = GENMASK(6, 0), 604 [VLAN_TABLE_MEMBERSHIP] = GENMASK(11, 7), 605 [VLAN_TABLE_VALID] = BIT(12), 606 [STATIC_MAC_TABLE_VALID] = BIT(21), 607 [STATIC_MAC_TABLE_USE_FID] = BIT(23), 608 [STATIC_MAC_TABLE_FID] = GENMASK(30, 24), 609 [STATIC_MAC_TABLE_OVERRIDE] = BIT(22), 610 [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(20, 16), 611 [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(6, 0), 612 [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(7), 613 [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7), 614 [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 29), 615 [DYNAMIC_MAC_TABLE_FID] = GENMASK(22, 16), 616 [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(26, 24), 617 [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(28, 27), 618 }; 619 620 static const u8 ksz8895_shifts[] = { 621 [VLAN_TABLE_MEMBERSHIP_S] = 7, 622 [VLAN_TABLE] = 13, 623 [STATIC_MAC_FWD_PORTS] = 16, 624 [STATIC_MAC_FID] = 24, 625 [DYNAMIC_MAC_ENTRIES_H] = 3, 626 [DYNAMIC_MAC_ENTRIES] = 29, 627 [DYNAMIC_MAC_FID] = 16, 628 [DYNAMIC_MAC_TIMESTAMP] = 27, 629 [DYNAMIC_MAC_SRC_PORT] = 24, 630 }; 631 632 static const u16 ksz9477_regs[] = { 633 [REG_SW_MAC_ADDR] = 0x0302, 634 [P_STP_CTRL] = 0x0B04, 635 [S_START_CTRL] = 0x0300, 636 [S_BROADCAST_CTRL] = 0x0332, 637 [S_MULTICAST_CTRL] = 0x0331, 638 [P_XMII_CTRL_0] = 0x0300, 639 [P_XMII_CTRL_1] = 0x0301, 640 [REG_SW_PME_CTRL] = 0x0006, 641 [REG_PORT_PME_STATUS] = 0x0013, 642 [REG_PORT_PME_CTRL] = 0x0017, 643 }; 644 645 static const u32 ksz9477_masks[] = { 646 [ALU_STAT_WRITE] = 0, 647 [ALU_STAT_READ] = 1, 648 [P_MII_TX_FLOW_CTRL] = BIT(5), 649 [P_MII_RX_FLOW_CTRL] = BIT(3), 650 }; 651 652 static const u8 ksz9477_shifts[] = { 653 [ALU_STAT_INDEX] = 16, 654 }; 655 656 static const u8 ksz9477_xmii_ctrl0[] = { 657 [P_MII_100MBIT] = 1, 658 [P_MII_10MBIT] = 0, 659 [P_MII_FULL_DUPLEX] = 1, 660 [P_MII_HALF_DUPLEX] = 0, 661 }; 662 663 static const u8 ksz9477_xmii_ctrl1[] = { 664 [P_RGMII_SEL] = 0, 665 [P_RMII_SEL] = 1, 666 [P_GMII_SEL] = 2, 667 [P_MII_SEL] = 3, 668 [P_GMII_1GBIT] = 0, 669 [P_GMII_NOT_1GBIT] = 1, 670 }; 671 672 static const u32 lan937x_masks[] = { 673 [ALU_STAT_WRITE] = 1, 674 [ALU_STAT_READ] = 2, 675 [P_MII_TX_FLOW_CTRL] = BIT(5), 676 [P_MII_RX_FLOW_CTRL] = BIT(3), 677 }; 678 679 static const u8 lan937x_shifts[] = { 680 [ALU_STAT_INDEX] = 8, 681 }; 682 683 static const struct regmap_range ksz8563_valid_regs[] = { 684 regmap_reg_range(0x0000, 0x0003), 685 regmap_reg_range(0x0006, 0x0006), 686 regmap_reg_range(0x000f, 0x001f), 687 regmap_reg_range(0x0100, 0x0100), 688 regmap_reg_range(0x0104, 0x0107), 689 regmap_reg_range(0x010d, 0x010d), 690 regmap_reg_range(0x0110, 0x0113), 691 regmap_reg_range(0x0120, 0x012b), 692 regmap_reg_range(0x0201, 0x0201), 693 regmap_reg_range(0x0210, 0x0213), 694 regmap_reg_range(0x0300, 0x0300), 695 regmap_reg_range(0x0302, 0x031b), 696 regmap_reg_range(0x0320, 0x032b), 697 regmap_reg_range(0x0330, 0x0336), 698 regmap_reg_range(0x0338, 0x033e), 699 regmap_reg_range(0x0340, 0x035f), 700 regmap_reg_range(0x0370, 0x0370), 701 regmap_reg_range(0x0378, 0x0378), 702 regmap_reg_range(0x037c, 0x037d), 703 regmap_reg_range(0x0390, 0x0393), 704 regmap_reg_range(0x0400, 0x040e), 705 regmap_reg_range(0x0410, 0x042f), 706 regmap_reg_range(0x0500, 0x0519), 707 regmap_reg_range(0x0520, 0x054b), 708 regmap_reg_range(0x0550, 0x05b3), 709 710 /* port 1 */ 711 regmap_reg_range(0x1000, 0x1001), 712 regmap_reg_range(0x1004, 0x100b), 713 regmap_reg_range(0x1013, 0x1013), 714 regmap_reg_range(0x1017, 0x1017), 715 regmap_reg_range(0x101b, 0x101b), 716 regmap_reg_range(0x101f, 0x1021), 717 regmap_reg_range(0x1030, 0x1030), 718 regmap_reg_range(0x1100, 0x1111), 719 regmap_reg_range(0x111a, 0x111d), 720 regmap_reg_range(0x1122, 0x1127), 721 regmap_reg_range(0x112a, 0x112b), 722 regmap_reg_range(0x1136, 0x1139), 723 regmap_reg_range(0x113e, 0x113f), 724 regmap_reg_range(0x1400, 0x1401), 725 regmap_reg_range(0x1403, 0x1403), 726 regmap_reg_range(0x1410, 0x1417), 727 regmap_reg_range(0x1420, 0x1423), 728 regmap_reg_range(0x1500, 0x1507), 729 regmap_reg_range(0x1600, 0x1612), 730 regmap_reg_range(0x1800, 0x180f), 731 regmap_reg_range(0x1900, 0x1907), 732 regmap_reg_range(0x1914, 0x191b), 733 regmap_reg_range(0x1a00, 0x1a03), 734 regmap_reg_range(0x1a04, 0x1a08), 735 regmap_reg_range(0x1b00, 0x1b01), 736 regmap_reg_range(0x1b04, 0x1b04), 737 regmap_reg_range(0x1c00, 0x1c05), 738 regmap_reg_range(0x1c08, 0x1c1b), 739 740 /* port 2 */ 741 regmap_reg_range(0x2000, 0x2001), 742 regmap_reg_range(0x2004, 0x200b), 743 regmap_reg_range(0x2013, 0x2013), 744 regmap_reg_range(0x2017, 0x2017), 745 regmap_reg_range(0x201b, 0x201b), 746 regmap_reg_range(0x201f, 0x2021), 747 regmap_reg_range(0x2030, 0x2030), 748 regmap_reg_range(0x2100, 0x2111), 749 regmap_reg_range(0x211a, 0x211d), 750 regmap_reg_range(0x2122, 0x2127), 751 regmap_reg_range(0x212a, 0x212b), 752 regmap_reg_range(0x2136, 0x2139), 753 regmap_reg_range(0x213e, 0x213f), 754 regmap_reg_range(0x2400, 0x2401), 755 regmap_reg_range(0x2403, 0x2403), 756 regmap_reg_range(0x2410, 0x2417), 757 regmap_reg_range(0x2420, 0x2423), 758 regmap_reg_range(0x2500, 0x2507), 759 regmap_reg_range(0x2600, 0x2612), 760 regmap_reg_range(0x2800, 0x280f), 761 regmap_reg_range(0x2900, 0x2907), 762 regmap_reg_range(0x2914, 0x291b), 763 regmap_reg_range(0x2a00, 0x2a03), 764 regmap_reg_range(0x2a04, 0x2a08), 765 regmap_reg_range(0x2b00, 0x2b01), 766 regmap_reg_range(0x2b04, 0x2b04), 767 regmap_reg_range(0x2c00, 0x2c05), 768 regmap_reg_range(0x2c08, 0x2c1b), 769 770 /* port 3 */ 771 regmap_reg_range(0x3000, 0x3001), 772 regmap_reg_range(0x3004, 0x300b), 773 regmap_reg_range(0x3013, 0x3013), 774 regmap_reg_range(0x3017, 0x3017), 775 regmap_reg_range(0x301b, 0x301b), 776 regmap_reg_range(0x301f, 0x3021), 777 regmap_reg_range(0x3030, 0x3030), 778 regmap_reg_range(0x3300, 0x3301), 779 regmap_reg_range(0x3303, 0x3303), 780 regmap_reg_range(0x3400, 0x3401), 781 regmap_reg_range(0x3403, 0x3403), 782 regmap_reg_range(0x3410, 0x3417), 783 regmap_reg_range(0x3420, 0x3423), 784 regmap_reg_range(0x3500, 0x3507), 785 regmap_reg_range(0x3600, 0x3612), 786 regmap_reg_range(0x3800, 0x380f), 787 regmap_reg_range(0x3900, 0x3907), 788 regmap_reg_range(0x3914, 0x391b), 789 regmap_reg_range(0x3a00, 0x3a03), 790 regmap_reg_range(0x3a04, 0x3a08), 791 regmap_reg_range(0x3b00, 0x3b01), 792 regmap_reg_range(0x3b04, 0x3b04), 793 regmap_reg_range(0x3c00, 0x3c05), 794 regmap_reg_range(0x3c08, 0x3c1b), 795 }; 796 797 static const struct regmap_access_table ksz8563_register_set = { 798 .yes_ranges = ksz8563_valid_regs, 799 .n_yes_ranges = ARRAY_SIZE(ksz8563_valid_regs), 800 }; 801 802 static const struct regmap_range ksz9477_valid_regs[] = { 803 regmap_reg_range(0x0000, 0x0003), 804 regmap_reg_range(0x0006, 0x0006), 805 regmap_reg_range(0x0010, 0x001f), 806 regmap_reg_range(0x0100, 0x0100), 807 regmap_reg_range(0x0103, 0x0107), 808 regmap_reg_range(0x010d, 0x010d), 809 regmap_reg_range(0x0110, 0x0113), 810 regmap_reg_range(0x0120, 0x012b), 811 regmap_reg_range(0x0201, 0x0201), 812 regmap_reg_range(0x0210, 0x0213), 813 regmap_reg_range(0x0300, 0x0300), 814 regmap_reg_range(0x0302, 0x031b), 815 regmap_reg_range(0x0320, 0x032b), 816 regmap_reg_range(0x0330, 0x0336), 817 regmap_reg_range(0x0338, 0x033b), 818 regmap_reg_range(0x033e, 0x033e), 819 regmap_reg_range(0x0340, 0x035f), 820 regmap_reg_range(0x0370, 0x0370), 821 regmap_reg_range(0x0378, 0x0378), 822 regmap_reg_range(0x037c, 0x037d), 823 regmap_reg_range(0x0390, 0x0393), 824 regmap_reg_range(0x0400, 0x040e), 825 regmap_reg_range(0x0410, 0x042f), 826 regmap_reg_range(0x0444, 0x044b), 827 regmap_reg_range(0x0450, 0x046f), 828 regmap_reg_range(0x0500, 0x0519), 829 regmap_reg_range(0x0520, 0x054b), 830 regmap_reg_range(0x0550, 0x05b3), 831 regmap_reg_range(0x0604, 0x060b), 832 regmap_reg_range(0x0610, 0x0612), 833 regmap_reg_range(0x0614, 0x062c), 834 regmap_reg_range(0x0640, 0x0645), 835 regmap_reg_range(0x0648, 0x064d), 836 837 /* port 1 */ 838 regmap_reg_range(0x1000, 0x1001), 839 regmap_reg_range(0x1013, 0x1013), 840 regmap_reg_range(0x1017, 0x1017), 841 regmap_reg_range(0x101b, 0x101b), 842 regmap_reg_range(0x101f, 0x1020), 843 regmap_reg_range(0x1030, 0x1030), 844 regmap_reg_range(0x1100, 0x1115), 845 regmap_reg_range(0x111a, 0x111f), 846 regmap_reg_range(0x1120, 0x112b), 847 regmap_reg_range(0x1134, 0x113b), 848 regmap_reg_range(0x113c, 0x113f), 849 regmap_reg_range(0x1400, 0x1401), 850 regmap_reg_range(0x1403, 0x1403), 851 regmap_reg_range(0x1410, 0x1417), 852 regmap_reg_range(0x1420, 0x1423), 853 regmap_reg_range(0x1500, 0x1507), 854 regmap_reg_range(0x1600, 0x1613), 855 regmap_reg_range(0x1800, 0x180f), 856 regmap_reg_range(0x1820, 0x1827), 857 regmap_reg_range(0x1830, 0x1837), 858 regmap_reg_range(0x1840, 0x184b), 859 regmap_reg_range(0x1900, 0x1907), 860 regmap_reg_range(0x1914, 0x191b), 861 regmap_reg_range(0x1920, 0x1920), 862 regmap_reg_range(0x1923, 0x1927), 863 regmap_reg_range(0x1a00, 0x1a03), 864 regmap_reg_range(0x1a04, 0x1a07), 865 regmap_reg_range(0x1b00, 0x1b01), 866 regmap_reg_range(0x1b04, 0x1b04), 867 regmap_reg_range(0x1c00, 0x1c05), 868 regmap_reg_range(0x1c08, 0x1c1b), 869 870 /* port 2 */ 871 regmap_reg_range(0x2000, 0x2001), 872 regmap_reg_range(0x2013, 0x2013), 873 regmap_reg_range(0x2017, 0x2017), 874 regmap_reg_range(0x201b, 0x201b), 875 regmap_reg_range(0x201f, 0x2020), 876 regmap_reg_range(0x2030, 0x2030), 877 regmap_reg_range(0x2100, 0x2115), 878 regmap_reg_range(0x211a, 0x211f), 879 regmap_reg_range(0x2120, 0x212b), 880 regmap_reg_range(0x2134, 0x213b), 881 regmap_reg_range(0x213c, 0x213f), 882 regmap_reg_range(0x2400, 0x2401), 883 regmap_reg_range(0x2403, 0x2403), 884 regmap_reg_range(0x2410, 0x2417), 885 regmap_reg_range(0x2420, 0x2423), 886 regmap_reg_range(0x2500, 0x2507), 887 regmap_reg_range(0x2600, 0x2613), 888 regmap_reg_range(0x2800, 0x280f), 889 regmap_reg_range(0x2820, 0x2827), 890 regmap_reg_range(0x2830, 0x2837), 891 regmap_reg_range(0x2840, 0x284b), 892 regmap_reg_range(0x2900, 0x2907), 893 regmap_reg_range(0x2914, 0x291b), 894 regmap_reg_range(0x2920, 0x2920), 895 regmap_reg_range(0x2923, 0x2927), 896 regmap_reg_range(0x2a00, 0x2a03), 897 regmap_reg_range(0x2a04, 0x2a07), 898 regmap_reg_range(0x2b00, 0x2b01), 899 regmap_reg_range(0x2b04, 0x2b04), 900 regmap_reg_range(0x2c00, 0x2c05), 901 regmap_reg_range(0x2c08, 0x2c1b), 902 903 /* port 3 */ 904 regmap_reg_range(0x3000, 0x3001), 905 regmap_reg_range(0x3013, 0x3013), 906 regmap_reg_range(0x3017, 0x3017), 907 regmap_reg_range(0x301b, 0x301b), 908 regmap_reg_range(0x301f, 0x3020), 909 regmap_reg_range(0x3030, 0x3030), 910 regmap_reg_range(0x3100, 0x3115), 911 regmap_reg_range(0x311a, 0x311f), 912 regmap_reg_range(0x3120, 0x312b), 913 regmap_reg_range(0x3134, 0x313b), 914 regmap_reg_range(0x313c, 0x313f), 915 regmap_reg_range(0x3400, 0x3401), 916 regmap_reg_range(0x3403, 0x3403), 917 regmap_reg_range(0x3410, 0x3417), 918 regmap_reg_range(0x3420, 0x3423), 919 regmap_reg_range(0x3500, 0x3507), 920 regmap_reg_range(0x3600, 0x3613), 921 regmap_reg_range(0x3800, 0x380f), 922 regmap_reg_range(0x3820, 0x3827), 923 regmap_reg_range(0x3830, 0x3837), 924 regmap_reg_range(0x3840, 0x384b), 925 regmap_reg_range(0x3900, 0x3907), 926 regmap_reg_range(0x3914, 0x391b), 927 regmap_reg_range(0x3920, 0x3920), 928 regmap_reg_range(0x3923, 0x3927), 929 regmap_reg_range(0x3a00, 0x3a03), 930 regmap_reg_range(0x3a04, 0x3a07), 931 regmap_reg_range(0x3b00, 0x3b01), 932 regmap_reg_range(0x3b04, 0x3b04), 933 regmap_reg_range(0x3c00, 0x3c05), 934 regmap_reg_range(0x3c08, 0x3c1b), 935 936 /* port 4 */ 937 regmap_reg_range(0x4000, 0x4001), 938 regmap_reg_range(0x4013, 0x4013), 939 regmap_reg_range(0x4017, 0x4017), 940 regmap_reg_range(0x401b, 0x401b), 941 regmap_reg_range(0x401f, 0x4020), 942 regmap_reg_range(0x4030, 0x4030), 943 regmap_reg_range(0x4100, 0x4115), 944 regmap_reg_range(0x411a, 0x411f), 945 regmap_reg_range(0x4120, 0x412b), 946 regmap_reg_range(0x4134, 0x413b), 947 regmap_reg_range(0x413c, 0x413f), 948 regmap_reg_range(0x4400, 0x4401), 949 regmap_reg_range(0x4403, 0x4403), 950 regmap_reg_range(0x4410, 0x4417), 951 regmap_reg_range(0x4420, 0x4423), 952 regmap_reg_range(0x4500, 0x4507), 953 regmap_reg_range(0x4600, 0x4613), 954 regmap_reg_range(0x4800, 0x480f), 955 regmap_reg_range(0x4820, 0x4827), 956 regmap_reg_range(0x4830, 0x4837), 957 regmap_reg_range(0x4840, 0x484b), 958 regmap_reg_range(0x4900, 0x4907), 959 regmap_reg_range(0x4914, 0x491b), 960 regmap_reg_range(0x4920, 0x4920), 961 regmap_reg_range(0x4923, 0x4927), 962 regmap_reg_range(0x4a00, 0x4a03), 963 regmap_reg_range(0x4a04, 0x4a07), 964 regmap_reg_range(0x4b00, 0x4b01), 965 regmap_reg_range(0x4b04, 0x4b04), 966 regmap_reg_range(0x4c00, 0x4c05), 967 regmap_reg_range(0x4c08, 0x4c1b), 968 969 /* port 5 */ 970 regmap_reg_range(0x5000, 0x5001), 971 regmap_reg_range(0x5013, 0x5013), 972 regmap_reg_range(0x5017, 0x5017), 973 regmap_reg_range(0x501b, 0x501b), 974 regmap_reg_range(0x501f, 0x5020), 975 regmap_reg_range(0x5030, 0x5030), 976 regmap_reg_range(0x5100, 0x5115), 977 regmap_reg_range(0x511a, 0x511f), 978 regmap_reg_range(0x5120, 0x512b), 979 regmap_reg_range(0x5134, 0x513b), 980 regmap_reg_range(0x513c, 0x513f), 981 regmap_reg_range(0x5400, 0x5401), 982 regmap_reg_range(0x5403, 0x5403), 983 regmap_reg_range(0x5410, 0x5417), 984 regmap_reg_range(0x5420, 0x5423), 985 regmap_reg_range(0x5500, 0x5507), 986 regmap_reg_range(0x5600, 0x5613), 987 regmap_reg_range(0x5800, 0x580f), 988 regmap_reg_range(0x5820, 0x5827), 989 regmap_reg_range(0x5830, 0x5837), 990 regmap_reg_range(0x5840, 0x584b), 991 regmap_reg_range(0x5900, 0x5907), 992 regmap_reg_range(0x5914, 0x591b), 993 regmap_reg_range(0x5920, 0x5920), 994 regmap_reg_range(0x5923, 0x5927), 995 regmap_reg_range(0x5a00, 0x5a03), 996 regmap_reg_range(0x5a04, 0x5a07), 997 regmap_reg_range(0x5b00, 0x5b01), 998 regmap_reg_range(0x5b04, 0x5b04), 999 regmap_reg_range(0x5c00, 0x5c05), 1000 regmap_reg_range(0x5c08, 0x5c1b), 1001 1002 /* port 6 */ 1003 regmap_reg_range(0x6000, 0x6001), 1004 regmap_reg_range(0x6013, 0x6013), 1005 regmap_reg_range(0x6017, 0x6017), 1006 regmap_reg_range(0x601b, 0x601b), 1007 regmap_reg_range(0x601f, 0x6020), 1008 regmap_reg_range(0x6030, 0x6030), 1009 regmap_reg_range(0x6300, 0x6301), 1010 regmap_reg_range(0x6400, 0x6401), 1011 regmap_reg_range(0x6403, 0x6403), 1012 regmap_reg_range(0x6410, 0x6417), 1013 regmap_reg_range(0x6420, 0x6423), 1014 regmap_reg_range(0x6500, 0x6507), 1015 regmap_reg_range(0x6600, 0x6613), 1016 regmap_reg_range(0x6800, 0x680f), 1017 regmap_reg_range(0x6820, 0x6827), 1018 regmap_reg_range(0x6830, 0x6837), 1019 regmap_reg_range(0x6840, 0x684b), 1020 regmap_reg_range(0x6900, 0x6907), 1021 regmap_reg_range(0x6914, 0x691b), 1022 regmap_reg_range(0x6920, 0x6920), 1023 regmap_reg_range(0x6923, 0x6927), 1024 regmap_reg_range(0x6a00, 0x6a03), 1025 regmap_reg_range(0x6a04, 0x6a07), 1026 regmap_reg_range(0x6b00, 0x6b01), 1027 regmap_reg_range(0x6b04, 0x6b04), 1028 regmap_reg_range(0x6c00, 0x6c05), 1029 regmap_reg_range(0x6c08, 0x6c1b), 1030 1031 /* port 7 */ 1032 regmap_reg_range(0x7000, 0x7001), 1033 regmap_reg_range(0x7013, 0x7013), 1034 regmap_reg_range(0x7017, 0x7017), 1035 regmap_reg_range(0x701b, 0x701b), 1036 regmap_reg_range(0x701f, 0x7020), 1037 regmap_reg_range(0x7030, 0x7030), 1038 regmap_reg_range(0x7200, 0x7203), 1039 regmap_reg_range(0x7206, 0x7207), 1040 regmap_reg_range(0x7300, 0x7301), 1041 regmap_reg_range(0x7400, 0x7401), 1042 regmap_reg_range(0x7403, 0x7403), 1043 regmap_reg_range(0x7410, 0x7417), 1044 regmap_reg_range(0x7420, 0x7423), 1045 regmap_reg_range(0x7500, 0x7507), 1046 regmap_reg_range(0x7600, 0x7613), 1047 regmap_reg_range(0x7800, 0x780f), 1048 regmap_reg_range(0x7820, 0x7827), 1049 regmap_reg_range(0x7830, 0x7837), 1050 regmap_reg_range(0x7840, 0x784b), 1051 regmap_reg_range(0x7900, 0x7907), 1052 regmap_reg_range(0x7914, 0x791b), 1053 regmap_reg_range(0x7920, 0x7920), 1054 regmap_reg_range(0x7923, 0x7927), 1055 regmap_reg_range(0x7a00, 0x7a03), 1056 regmap_reg_range(0x7a04, 0x7a07), 1057 regmap_reg_range(0x7b00, 0x7b01), 1058 regmap_reg_range(0x7b04, 0x7b04), 1059 regmap_reg_range(0x7c00, 0x7c05), 1060 regmap_reg_range(0x7c08, 0x7c1b), 1061 }; 1062 1063 static const struct regmap_access_table ksz9477_register_set = { 1064 .yes_ranges = ksz9477_valid_regs, 1065 .n_yes_ranges = ARRAY_SIZE(ksz9477_valid_regs), 1066 }; 1067 1068 static const struct regmap_range ksz9896_valid_regs[] = { 1069 regmap_reg_range(0x0000, 0x0003), 1070 regmap_reg_range(0x0006, 0x0006), 1071 regmap_reg_range(0x0010, 0x001f), 1072 regmap_reg_range(0x0100, 0x0100), 1073 regmap_reg_range(0x0103, 0x0107), 1074 regmap_reg_range(0x010d, 0x010d), 1075 regmap_reg_range(0x0110, 0x0113), 1076 regmap_reg_range(0x0120, 0x0127), 1077 regmap_reg_range(0x0201, 0x0201), 1078 regmap_reg_range(0x0210, 0x0213), 1079 regmap_reg_range(0x0300, 0x0300), 1080 regmap_reg_range(0x0302, 0x030b), 1081 regmap_reg_range(0x0310, 0x031b), 1082 regmap_reg_range(0x0320, 0x032b), 1083 regmap_reg_range(0x0330, 0x0336), 1084 regmap_reg_range(0x0338, 0x033b), 1085 regmap_reg_range(0x033e, 0x033e), 1086 regmap_reg_range(0x0340, 0x035f), 1087 regmap_reg_range(0x0370, 0x0370), 1088 regmap_reg_range(0x0378, 0x0378), 1089 regmap_reg_range(0x037c, 0x037d), 1090 regmap_reg_range(0x0390, 0x0393), 1091 regmap_reg_range(0x0400, 0x040e), 1092 regmap_reg_range(0x0410, 0x042f), 1093 1094 /* port 1 */ 1095 regmap_reg_range(0x1000, 0x1001), 1096 regmap_reg_range(0x1013, 0x1013), 1097 regmap_reg_range(0x1017, 0x1017), 1098 regmap_reg_range(0x101b, 0x101b), 1099 regmap_reg_range(0x101f, 0x1020), 1100 regmap_reg_range(0x1030, 0x1030), 1101 regmap_reg_range(0x1100, 0x1115), 1102 regmap_reg_range(0x111a, 0x111f), 1103 regmap_reg_range(0x1122, 0x1127), 1104 regmap_reg_range(0x112a, 0x112b), 1105 regmap_reg_range(0x1136, 0x1139), 1106 regmap_reg_range(0x113e, 0x113f), 1107 regmap_reg_range(0x1400, 0x1401), 1108 regmap_reg_range(0x1403, 0x1403), 1109 regmap_reg_range(0x1410, 0x1417), 1110 regmap_reg_range(0x1420, 0x1423), 1111 regmap_reg_range(0x1500, 0x1507), 1112 regmap_reg_range(0x1600, 0x1612), 1113 regmap_reg_range(0x1800, 0x180f), 1114 regmap_reg_range(0x1820, 0x1827), 1115 regmap_reg_range(0x1830, 0x1837), 1116 regmap_reg_range(0x1840, 0x184b), 1117 regmap_reg_range(0x1900, 0x1907), 1118 regmap_reg_range(0x1914, 0x1915), 1119 regmap_reg_range(0x1a00, 0x1a03), 1120 regmap_reg_range(0x1a04, 0x1a07), 1121 regmap_reg_range(0x1b00, 0x1b01), 1122 regmap_reg_range(0x1b04, 0x1b04), 1123 1124 /* port 2 */ 1125 regmap_reg_range(0x2000, 0x2001), 1126 regmap_reg_range(0x2013, 0x2013), 1127 regmap_reg_range(0x2017, 0x2017), 1128 regmap_reg_range(0x201b, 0x201b), 1129 regmap_reg_range(0x201f, 0x2020), 1130 regmap_reg_range(0x2030, 0x2030), 1131 regmap_reg_range(0x2100, 0x2115), 1132 regmap_reg_range(0x211a, 0x211f), 1133 regmap_reg_range(0x2122, 0x2127), 1134 regmap_reg_range(0x212a, 0x212b), 1135 regmap_reg_range(0x2136, 0x2139), 1136 regmap_reg_range(0x213e, 0x213f), 1137 regmap_reg_range(0x2400, 0x2401), 1138 regmap_reg_range(0x2403, 0x2403), 1139 regmap_reg_range(0x2410, 0x2417), 1140 regmap_reg_range(0x2420, 0x2423), 1141 regmap_reg_range(0x2500, 0x2507), 1142 regmap_reg_range(0x2600, 0x2612), 1143 regmap_reg_range(0x2800, 0x280f), 1144 regmap_reg_range(0x2820, 0x2827), 1145 regmap_reg_range(0x2830, 0x2837), 1146 regmap_reg_range(0x2840, 0x284b), 1147 regmap_reg_range(0x2900, 0x2907), 1148 regmap_reg_range(0x2914, 0x2915), 1149 regmap_reg_range(0x2a00, 0x2a03), 1150 regmap_reg_range(0x2a04, 0x2a07), 1151 regmap_reg_range(0x2b00, 0x2b01), 1152 regmap_reg_range(0x2b04, 0x2b04), 1153 1154 /* port 3 */ 1155 regmap_reg_range(0x3000, 0x3001), 1156 regmap_reg_range(0x3013, 0x3013), 1157 regmap_reg_range(0x3017, 0x3017), 1158 regmap_reg_range(0x301b, 0x301b), 1159 regmap_reg_range(0x301f, 0x3020), 1160 regmap_reg_range(0x3030, 0x3030), 1161 regmap_reg_range(0x3100, 0x3115), 1162 regmap_reg_range(0x311a, 0x311f), 1163 regmap_reg_range(0x3122, 0x3127), 1164 regmap_reg_range(0x312a, 0x312b), 1165 regmap_reg_range(0x3136, 0x3139), 1166 regmap_reg_range(0x313e, 0x313f), 1167 regmap_reg_range(0x3400, 0x3401), 1168 regmap_reg_range(0x3403, 0x3403), 1169 regmap_reg_range(0x3410, 0x3417), 1170 regmap_reg_range(0x3420, 0x3423), 1171 regmap_reg_range(0x3500, 0x3507), 1172 regmap_reg_range(0x3600, 0x3612), 1173 regmap_reg_range(0x3800, 0x380f), 1174 regmap_reg_range(0x3820, 0x3827), 1175 regmap_reg_range(0x3830, 0x3837), 1176 regmap_reg_range(0x3840, 0x384b), 1177 regmap_reg_range(0x3900, 0x3907), 1178 regmap_reg_range(0x3914, 0x3915), 1179 regmap_reg_range(0x3a00, 0x3a03), 1180 regmap_reg_range(0x3a04, 0x3a07), 1181 regmap_reg_range(0x3b00, 0x3b01), 1182 regmap_reg_range(0x3b04, 0x3b04), 1183 1184 /* port 4 */ 1185 regmap_reg_range(0x4000, 0x4001), 1186 regmap_reg_range(0x4013, 0x4013), 1187 regmap_reg_range(0x4017, 0x4017), 1188 regmap_reg_range(0x401b, 0x401b), 1189 regmap_reg_range(0x401f, 0x4020), 1190 regmap_reg_range(0x4030, 0x4030), 1191 regmap_reg_range(0x4100, 0x4115), 1192 regmap_reg_range(0x411a, 0x411f), 1193 regmap_reg_range(0x4122, 0x4127), 1194 regmap_reg_range(0x412a, 0x412b), 1195 regmap_reg_range(0x4136, 0x4139), 1196 regmap_reg_range(0x413e, 0x413f), 1197 regmap_reg_range(0x4400, 0x4401), 1198 regmap_reg_range(0x4403, 0x4403), 1199 regmap_reg_range(0x4410, 0x4417), 1200 regmap_reg_range(0x4420, 0x4423), 1201 regmap_reg_range(0x4500, 0x4507), 1202 regmap_reg_range(0x4600, 0x4612), 1203 regmap_reg_range(0x4800, 0x480f), 1204 regmap_reg_range(0x4820, 0x4827), 1205 regmap_reg_range(0x4830, 0x4837), 1206 regmap_reg_range(0x4840, 0x484b), 1207 regmap_reg_range(0x4900, 0x4907), 1208 regmap_reg_range(0x4914, 0x4915), 1209 regmap_reg_range(0x4a00, 0x4a03), 1210 regmap_reg_range(0x4a04, 0x4a07), 1211 regmap_reg_range(0x4b00, 0x4b01), 1212 regmap_reg_range(0x4b04, 0x4b04), 1213 1214 /* port 5 */ 1215 regmap_reg_range(0x5000, 0x5001), 1216 regmap_reg_range(0x5013, 0x5013), 1217 regmap_reg_range(0x5017, 0x5017), 1218 regmap_reg_range(0x501b, 0x501b), 1219 regmap_reg_range(0x501f, 0x5020), 1220 regmap_reg_range(0x5030, 0x5030), 1221 regmap_reg_range(0x5100, 0x5115), 1222 regmap_reg_range(0x511a, 0x511f), 1223 regmap_reg_range(0x5122, 0x5127), 1224 regmap_reg_range(0x512a, 0x512b), 1225 regmap_reg_range(0x5136, 0x5139), 1226 regmap_reg_range(0x513e, 0x513f), 1227 regmap_reg_range(0x5400, 0x5401), 1228 regmap_reg_range(0x5403, 0x5403), 1229 regmap_reg_range(0x5410, 0x5417), 1230 regmap_reg_range(0x5420, 0x5423), 1231 regmap_reg_range(0x5500, 0x5507), 1232 regmap_reg_range(0x5600, 0x5612), 1233 regmap_reg_range(0x5800, 0x580f), 1234 regmap_reg_range(0x5820, 0x5827), 1235 regmap_reg_range(0x5830, 0x5837), 1236 regmap_reg_range(0x5840, 0x584b), 1237 regmap_reg_range(0x5900, 0x5907), 1238 regmap_reg_range(0x5914, 0x5915), 1239 regmap_reg_range(0x5a00, 0x5a03), 1240 regmap_reg_range(0x5a04, 0x5a07), 1241 regmap_reg_range(0x5b00, 0x5b01), 1242 regmap_reg_range(0x5b04, 0x5b04), 1243 1244 /* port 6 */ 1245 regmap_reg_range(0x6000, 0x6001), 1246 regmap_reg_range(0x6013, 0x6013), 1247 regmap_reg_range(0x6017, 0x6017), 1248 regmap_reg_range(0x601b, 0x601b), 1249 regmap_reg_range(0x601f, 0x6020), 1250 regmap_reg_range(0x6030, 0x6030), 1251 regmap_reg_range(0x6100, 0x6115), 1252 regmap_reg_range(0x611a, 0x611f), 1253 regmap_reg_range(0x6122, 0x6127), 1254 regmap_reg_range(0x612a, 0x612b), 1255 regmap_reg_range(0x6136, 0x6139), 1256 regmap_reg_range(0x613e, 0x613f), 1257 regmap_reg_range(0x6300, 0x6301), 1258 regmap_reg_range(0x6400, 0x6401), 1259 regmap_reg_range(0x6403, 0x6403), 1260 regmap_reg_range(0x6410, 0x6417), 1261 regmap_reg_range(0x6420, 0x6423), 1262 regmap_reg_range(0x6500, 0x6507), 1263 regmap_reg_range(0x6600, 0x6612), 1264 regmap_reg_range(0x6800, 0x680f), 1265 regmap_reg_range(0x6820, 0x6827), 1266 regmap_reg_range(0x6830, 0x6837), 1267 regmap_reg_range(0x6840, 0x684b), 1268 regmap_reg_range(0x6900, 0x6907), 1269 regmap_reg_range(0x6914, 0x6915), 1270 regmap_reg_range(0x6a00, 0x6a03), 1271 regmap_reg_range(0x6a04, 0x6a07), 1272 regmap_reg_range(0x6b00, 0x6b01), 1273 regmap_reg_range(0x6b04, 0x6b04), 1274 }; 1275 1276 static const struct regmap_access_table ksz9896_register_set = { 1277 .yes_ranges = ksz9896_valid_regs, 1278 .n_yes_ranges = ARRAY_SIZE(ksz9896_valid_regs), 1279 }; 1280 1281 static const struct regmap_range ksz8873_valid_regs[] = { 1282 regmap_reg_range(0x00, 0x01), 1283 /* global control register */ 1284 regmap_reg_range(0x02, 0x0f), 1285 1286 /* port registers */ 1287 regmap_reg_range(0x10, 0x1d), 1288 regmap_reg_range(0x1e, 0x1f), 1289 regmap_reg_range(0x20, 0x2d), 1290 regmap_reg_range(0x2e, 0x2f), 1291 regmap_reg_range(0x30, 0x39), 1292 regmap_reg_range(0x3f, 0x3f), 1293 1294 /* advanced control registers */ 1295 regmap_reg_range(0x60, 0x6f), 1296 regmap_reg_range(0x70, 0x75), 1297 regmap_reg_range(0x76, 0x78), 1298 regmap_reg_range(0x79, 0x7a), 1299 regmap_reg_range(0x7b, 0x83), 1300 regmap_reg_range(0x8e, 0x99), 1301 regmap_reg_range(0x9a, 0xa5), 1302 regmap_reg_range(0xa6, 0xa6), 1303 regmap_reg_range(0xa7, 0xaa), 1304 regmap_reg_range(0xab, 0xae), 1305 regmap_reg_range(0xaf, 0xba), 1306 regmap_reg_range(0xbb, 0xbc), 1307 regmap_reg_range(0xbd, 0xbd), 1308 regmap_reg_range(0xc0, 0xc0), 1309 regmap_reg_range(0xc2, 0xc2), 1310 regmap_reg_range(0xc3, 0xc3), 1311 regmap_reg_range(0xc4, 0xc4), 1312 regmap_reg_range(0xc6, 0xc6), 1313 }; 1314 1315 static const struct regmap_access_table ksz8873_register_set = { 1316 .yes_ranges = ksz8873_valid_regs, 1317 .n_yes_ranges = ARRAY_SIZE(ksz8873_valid_regs), 1318 }; 1319 1320 const struct ksz_chip_data ksz_switch_chips[] = { 1321 [KSZ8563] = { 1322 .chip_id = KSZ8563_CHIP_ID, 1323 .dev_name = "KSZ8563", 1324 .num_vlans = 4096, 1325 .num_alus = 4096, 1326 .num_statics = 16, 1327 .cpu_ports = 0x07, /* can be configured as cpu port */ 1328 .port_cnt = 3, /* total port count */ 1329 .port_nirqs = 3, 1330 .num_tx_queues = 4, 1331 .num_ipms = 8, 1332 .tc_cbs_supported = true, 1333 .ops = &ksz9477_dev_ops, 1334 .phylink_mac_ops = &ksz9477_phylink_mac_ops, 1335 .mib_names = ksz9477_mib_names, 1336 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1337 .reg_mib_cnt = MIB_COUNTER_NUM, 1338 .regs = ksz9477_regs, 1339 .masks = ksz9477_masks, 1340 .shifts = ksz9477_shifts, 1341 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1342 .xmii_ctrl1 = ksz8795_xmii_ctrl1, /* Same as ksz8795 */ 1343 .supports_mii = {false, false, true}, 1344 .supports_rmii = {false, false, true}, 1345 .supports_rgmii = {false, false, true}, 1346 .internal_phy = {true, true, false}, 1347 .gbit_capable = {false, false, true}, 1348 .wr_table = &ksz8563_register_set, 1349 .rd_table = &ksz8563_register_set, 1350 }, 1351 1352 [KSZ8795] = { 1353 .chip_id = KSZ8795_CHIP_ID, 1354 .dev_name = "KSZ8795", 1355 .num_vlans = 4096, 1356 .num_alus = 0, 1357 .num_statics = 32, 1358 .cpu_ports = 0x10, /* can be configured as cpu port */ 1359 .port_cnt = 5, /* total cpu and user ports */ 1360 .num_tx_queues = 4, 1361 .num_ipms = 4, 1362 .ops = &ksz87xx_dev_ops, 1363 .phylink_mac_ops = &ksz8_phylink_mac_ops, 1364 .ksz87xx_eee_link_erratum = true, 1365 .mib_names = ksz9477_mib_names, 1366 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1367 .reg_mib_cnt = MIB_COUNTER_NUM, 1368 .regs = ksz8795_regs, 1369 .masks = ksz8795_masks, 1370 .shifts = ksz8795_shifts, 1371 .xmii_ctrl0 = ksz8795_xmii_ctrl0, 1372 .xmii_ctrl1 = ksz8795_xmii_ctrl1, 1373 .supports_mii = {false, false, false, false, true}, 1374 .supports_rmii = {false, false, false, false, true}, 1375 .supports_rgmii = {false, false, false, false, true}, 1376 .internal_phy = {true, true, true, true, false}, 1377 }, 1378 1379 [KSZ8794] = { 1380 /* WARNING 1381 * ======= 1382 * KSZ8794 is similar to KSZ8795, except the port map 1383 * contains a gap between external and CPU ports, the 1384 * port map is NOT continuous. The per-port register 1385 * map is shifted accordingly too, i.e. registers at 1386 * offset 0x40 are NOT used on KSZ8794 and they ARE 1387 * used on KSZ8795 for external port 3. 1388 * external cpu 1389 * KSZ8794 0,1,2 4 1390 * KSZ8795 0,1,2,3 4 1391 * KSZ8765 0,1,2,3 4 1392 * port_cnt is configured as 5, even though it is 4 1393 */ 1394 .chip_id = KSZ8794_CHIP_ID, 1395 .dev_name = "KSZ8794", 1396 .num_vlans = 4096, 1397 .num_alus = 0, 1398 .num_statics = 32, 1399 .cpu_ports = 0x10, /* can be configured as cpu port */ 1400 .port_cnt = 5, /* total cpu and user ports */ 1401 .num_tx_queues = 4, 1402 .num_ipms = 4, 1403 .ops = &ksz87xx_dev_ops, 1404 .phylink_mac_ops = &ksz8_phylink_mac_ops, 1405 .ksz87xx_eee_link_erratum = true, 1406 .mib_names = ksz9477_mib_names, 1407 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1408 .reg_mib_cnt = MIB_COUNTER_NUM, 1409 .regs = ksz8795_regs, 1410 .masks = ksz8795_masks, 1411 .shifts = ksz8795_shifts, 1412 .xmii_ctrl0 = ksz8795_xmii_ctrl0, 1413 .xmii_ctrl1 = ksz8795_xmii_ctrl1, 1414 .supports_mii = {false, false, false, false, true}, 1415 .supports_rmii = {false, false, false, false, true}, 1416 .supports_rgmii = {false, false, false, false, true}, 1417 .internal_phy = {true, true, true, false, false}, 1418 }, 1419 1420 [KSZ8765] = { 1421 .chip_id = KSZ8765_CHIP_ID, 1422 .dev_name = "KSZ8765", 1423 .num_vlans = 4096, 1424 .num_alus = 0, 1425 .num_statics = 32, 1426 .cpu_ports = 0x10, /* can be configured as cpu port */ 1427 .port_cnt = 5, /* total cpu and user ports */ 1428 .num_tx_queues = 4, 1429 .num_ipms = 4, 1430 .ops = &ksz87xx_dev_ops, 1431 .phylink_mac_ops = &ksz8_phylink_mac_ops, 1432 .ksz87xx_eee_link_erratum = true, 1433 .mib_names = ksz9477_mib_names, 1434 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1435 .reg_mib_cnt = MIB_COUNTER_NUM, 1436 .regs = ksz8795_regs, 1437 .masks = ksz8795_masks, 1438 .shifts = ksz8795_shifts, 1439 .xmii_ctrl0 = ksz8795_xmii_ctrl0, 1440 .xmii_ctrl1 = ksz8795_xmii_ctrl1, 1441 .supports_mii = {false, false, false, false, true}, 1442 .supports_rmii = {false, false, false, false, true}, 1443 .supports_rgmii = {false, false, false, false, true}, 1444 .internal_phy = {true, true, true, true, false}, 1445 }, 1446 1447 [KSZ88X3] = { 1448 .chip_id = KSZ88X3_CHIP_ID, 1449 .dev_name = "KSZ8863/KSZ8873", 1450 .num_vlans = 16, 1451 .num_alus = 0, 1452 .num_statics = 8, 1453 .cpu_ports = 0x4, /* can be configured as cpu port */ 1454 .port_cnt = 3, 1455 .num_tx_queues = 4, 1456 .num_ipms = 4, 1457 .ops = &ksz88xx_dev_ops, 1458 .phylink_mac_ops = &ksz88x3_phylink_mac_ops, 1459 .mib_names = ksz88xx_mib_names, 1460 .mib_cnt = ARRAY_SIZE(ksz88xx_mib_names), 1461 .reg_mib_cnt = MIB_COUNTER_NUM, 1462 .regs = ksz8863_regs, 1463 .masks = ksz8863_masks, 1464 .shifts = ksz8863_shifts, 1465 .supports_mii = {false, false, true}, 1466 .supports_rmii = {false, false, true}, 1467 .internal_phy = {true, true, false}, 1468 .wr_table = &ksz8873_register_set, 1469 .rd_table = &ksz8873_register_set, 1470 }, 1471 1472 [KSZ8864] = { 1473 /* WARNING 1474 * ======= 1475 * KSZ8864 is similar to KSZ8895, except the first port 1476 * does not exist. 1477 * external cpu 1478 * KSZ8864 1,2,3 4 1479 * KSZ8895 0,1,2,3 4 1480 * port_cnt is configured as 5, even though it is 4 1481 */ 1482 .chip_id = KSZ8864_CHIP_ID, 1483 .dev_name = "KSZ8864", 1484 .num_vlans = 4096, 1485 .num_alus = 0, 1486 .num_statics = 32, 1487 .cpu_ports = 0x10, /* can be configured as cpu port */ 1488 .port_cnt = 5, /* total cpu and user ports */ 1489 .num_tx_queues = 4, 1490 .num_ipms = 4, 1491 .ops = &ksz88xx_dev_ops, 1492 .phylink_mac_ops = &ksz88x3_phylink_mac_ops, 1493 .mib_names = ksz88xx_mib_names, 1494 .mib_cnt = ARRAY_SIZE(ksz88xx_mib_names), 1495 .reg_mib_cnt = MIB_COUNTER_NUM, 1496 .regs = ksz8895_regs, 1497 .masks = ksz8895_masks, 1498 .shifts = ksz8895_shifts, 1499 .supports_mii = {false, false, false, false, true}, 1500 .supports_rmii = {false, false, false, false, true}, 1501 .internal_phy = {false, true, true, true, false}, 1502 }, 1503 1504 [KSZ8895] = { 1505 .chip_id = KSZ8895_CHIP_ID, 1506 .dev_name = "KSZ8895", 1507 .num_vlans = 4096, 1508 .num_alus = 0, 1509 .num_statics = 32, 1510 .cpu_ports = 0x10, /* can be configured as cpu port */ 1511 .port_cnt = 5, /* total cpu and user ports */ 1512 .num_tx_queues = 4, 1513 .num_ipms = 4, 1514 .ops = &ksz88xx_dev_ops, 1515 .phylink_mac_ops = &ksz88x3_phylink_mac_ops, 1516 .mib_names = ksz88xx_mib_names, 1517 .mib_cnt = ARRAY_SIZE(ksz88xx_mib_names), 1518 .reg_mib_cnt = MIB_COUNTER_NUM, 1519 .regs = ksz8895_regs, 1520 .masks = ksz8895_masks, 1521 .shifts = ksz8895_shifts, 1522 .supports_mii = {false, false, false, false, true}, 1523 .supports_rmii = {false, false, false, false, true}, 1524 .internal_phy = {true, true, true, true, false}, 1525 }, 1526 1527 [KSZ9477] = { 1528 .chip_id = KSZ9477_CHIP_ID, 1529 .dev_name = "KSZ9477", 1530 .num_vlans = 4096, 1531 .num_alus = 4096, 1532 .num_statics = 16, 1533 .cpu_ports = 0x7F, /* can be configured as cpu port */ 1534 .port_cnt = 7, /* total physical port count */ 1535 .port_nirqs = 4, 1536 .num_tx_queues = 4, 1537 .num_ipms = 8, 1538 .tc_cbs_supported = true, 1539 .ops = &ksz9477_dev_ops, 1540 .phylink_mac_ops = &ksz9477_phylink_mac_ops, 1541 .phy_errata_9477 = true, 1542 .mib_names = ksz9477_mib_names, 1543 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1544 .reg_mib_cnt = MIB_COUNTER_NUM, 1545 .regs = ksz9477_regs, 1546 .masks = ksz9477_masks, 1547 .shifts = ksz9477_shifts, 1548 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1549 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1550 .supports_mii = {false, false, false, false, 1551 false, true, false}, 1552 .supports_rmii = {false, false, false, false, 1553 false, true, false}, 1554 .supports_rgmii = {false, false, false, false, 1555 false, true, false}, 1556 .internal_phy = {true, true, true, true, 1557 true, false, false}, 1558 .gbit_capable = {true, true, true, true, true, true, true}, 1559 .wr_table = &ksz9477_register_set, 1560 .rd_table = &ksz9477_register_set, 1561 }, 1562 1563 [KSZ9896] = { 1564 .chip_id = KSZ9896_CHIP_ID, 1565 .dev_name = "KSZ9896", 1566 .num_vlans = 4096, 1567 .num_alus = 4096, 1568 .num_statics = 16, 1569 .cpu_ports = 0x3F, /* can be configured as cpu port */ 1570 .port_cnt = 6, /* total physical port count */ 1571 .port_nirqs = 2, 1572 .num_tx_queues = 4, 1573 .num_ipms = 8, 1574 .ops = &ksz9477_dev_ops, 1575 .phylink_mac_ops = &ksz9477_phylink_mac_ops, 1576 .phy_errata_9477 = true, 1577 .mib_names = ksz9477_mib_names, 1578 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1579 .reg_mib_cnt = MIB_COUNTER_NUM, 1580 .regs = ksz9477_regs, 1581 .masks = ksz9477_masks, 1582 .shifts = ksz9477_shifts, 1583 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1584 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1585 .supports_mii = {false, false, false, false, 1586 false, true}, 1587 .supports_rmii = {false, false, false, false, 1588 false, true}, 1589 .supports_rgmii = {false, false, false, false, 1590 false, true}, 1591 .internal_phy = {true, true, true, true, 1592 true, false}, 1593 .gbit_capable = {true, true, true, true, true, true}, 1594 .wr_table = &ksz9896_register_set, 1595 .rd_table = &ksz9896_register_set, 1596 }, 1597 1598 [KSZ9897] = { 1599 .chip_id = KSZ9897_CHIP_ID, 1600 .dev_name = "KSZ9897", 1601 .num_vlans = 4096, 1602 .num_alus = 4096, 1603 .num_statics = 16, 1604 .cpu_ports = 0x7F, /* can be configured as cpu port */ 1605 .port_cnt = 7, /* total physical port count */ 1606 .port_nirqs = 2, 1607 .num_tx_queues = 4, 1608 .num_ipms = 8, 1609 .ops = &ksz9477_dev_ops, 1610 .phylink_mac_ops = &ksz9477_phylink_mac_ops, 1611 .phy_errata_9477 = true, 1612 .mib_names = ksz9477_mib_names, 1613 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1614 .reg_mib_cnt = MIB_COUNTER_NUM, 1615 .regs = ksz9477_regs, 1616 .masks = ksz9477_masks, 1617 .shifts = ksz9477_shifts, 1618 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1619 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1620 .supports_mii = {false, false, false, false, 1621 false, true, true}, 1622 .supports_rmii = {false, false, false, false, 1623 false, true, true}, 1624 .supports_rgmii = {false, false, false, false, 1625 false, true, true}, 1626 .internal_phy = {true, true, true, true, 1627 true, false, false}, 1628 .gbit_capable = {true, true, true, true, true, true, true}, 1629 }, 1630 1631 [KSZ9893] = { 1632 .chip_id = KSZ9893_CHIP_ID, 1633 .dev_name = "KSZ9893", 1634 .num_vlans = 4096, 1635 .num_alus = 4096, 1636 .num_statics = 16, 1637 .cpu_ports = 0x07, /* can be configured as cpu port */ 1638 .port_cnt = 3, /* total port count */ 1639 .port_nirqs = 2, 1640 .num_tx_queues = 4, 1641 .num_ipms = 8, 1642 .ops = &ksz9477_dev_ops, 1643 .phylink_mac_ops = &ksz9477_phylink_mac_ops, 1644 .mib_names = ksz9477_mib_names, 1645 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1646 .reg_mib_cnt = MIB_COUNTER_NUM, 1647 .regs = ksz9477_regs, 1648 .masks = ksz9477_masks, 1649 .shifts = ksz9477_shifts, 1650 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1651 .xmii_ctrl1 = ksz8795_xmii_ctrl1, /* Same as ksz8795 */ 1652 .supports_mii = {false, false, true}, 1653 .supports_rmii = {false, false, true}, 1654 .supports_rgmii = {false, false, true}, 1655 .internal_phy = {true, true, false}, 1656 .gbit_capable = {true, true, true}, 1657 }, 1658 1659 [KSZ9563] = { 1660 .chip_id = KSZ9563_CHIP_ID, 1661 .dev_name = "KSZ9563", 1662 .num_vlans = 4096, 1663 .num_alus = 4096, 1664 .num_statics = 16, 1665 .cpu_ports = 0x07, /* can be configured as cpu port */ 1666 .port_cnt = 3, /* total port count */ 1667 .port_nirqs = 3, 1668 .num_tx_queues = 4, 1669 .num_ipms = 8, 1670 .tc_cbs_supported = true, 1671 .ops = &ksz9477_dev_ops, 1672 .phylink_mac_ops = &ksz9477_phylink_mac_ops, 1673 .mib_names = ksz9477_mib_names, 1674 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1675 .reg_mib_cnt = MIB_COUNTER_NUM, 1676 .regs = ksz9477_regs, 1677 .masks = ksz9477_masks, 1678 .shifts = ksz9477_shifts, 1679 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1680 .xmii_ctrl1 = ksz8795_xmii_ctrl1, /* Same as ksz8795 */ 1681 .supports_mii = {false, false, true}, 1682 .supports_rmii = {false, false, true}, 1683 .supports_rgmii = {false, false, true}, 1684 .internal_phy = {true, true, false}, 1685 .gbit_capable = {true, true, true}, 1686 }, 1687 1688 [KSZ8567] = { 1689 .chip_id = KSZ8567_CHIP_ID, 1690 .dev_name = "KSZ8567", 1691 .num_vlans = 4096, 1692 .num_alus = 4096, 1693 .num_statics = 16, 1694 .cpu_ports = 0x7F, /* can be configured as cpu port */ 1695 .port_cnt = 7, /* total port count */ 1696 .port_nirqs = 3, 1697 .num_tx_queues = 4, 1698 .num_ipms = 8, 1699 .tc_cbs_supported = true, 1700 .ops = &ksz9477_dev_ops, 1701 .phylink_mac_ops = &ksz9477_phylink_mac_ops, 1702 .phy_errata_9477 = true, 1703 .mib_names = ksz9477_mib_names, 1704 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1705 .reg_mib_cnt = MIB_COUNTER_NUM, 1706 .regs = ksz9477_regs, 1707 .masks = ksz9477_masks, 1708 .shifts = ksz9477_shifts, 1709 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1710 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1711 .supports_mii = {false, false, false, false, 1712 false, true, true}, 1713 .supports_rmii = {false, false, false, false, 1714 false, true, true}, 1715 .supports_rgmii = {false, false, false, false, 1716 false, true, true}, 1717 .internal_phy = {true, true, true, true, 1718 true, false, false}, 1719 .gbit_capable = {false, false, false, false, false, 1720 true, true}, 1721 }, 1722 1723 [KSZ9567] = { 1724 .chip_id = KSZ9567_CHIP_ID, 1725 .dev_name = "KSZ9567", 1726 .num_vlans = 4096, 1727 .num_alus = 4096, 1728 .num_statics = 16, 1729 .cpu_ports = 0x7F, /* can be configured as cpu port */ 1730 .port_cnt = 7, /* total physical port count */ 1731 .port_nirqs = 3, 1732 .num_tx_queues = 4, 1733 .num_ipms = 8, 1734 .tc_cbs_supported = true, 1735 .ops = &ksz9477_dev_ops, 1736 .mib_names = ksz9477_mib_names, 1737 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1738 .reg_mib_cnt = MIB_COUNTER_NUM, 1739 .regs = ksz9477_regs, 1740 .masks = ksz9477_masks, 1741 .shifts = ksz9477_shifts, 1742 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1743 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1744 .supports_mii = {false, false, false, false, 1745 false, true, true}, 1746 .supports_rmii = {false, false, false, false, 1747 false, true, true}, 1748 .supports_rgmii = {false, false, false, false, 1749 false, true, true}, 1750 .internal_phy = {true, true, true, true, 1751 true, false, false}, 1752 .gbit_capable = {true, true, true, true, true, true, true}, 1753 }, 1754 1755 [LAN9370] = { 1756 .chip_id = LAN9370_CHIP_ID, 1757 .dev_name = "LAN9370", 1758 .num_vlans = 4096, 1759 .num_alus = 1024, 1760 .num_statics = 256, 1761 .cpu_ports = 0x10, /* can be configured as cpu port */ 1762 .port_cnt = 5, /* total physical port count */ 1763 .port_nirqs = 6, 1764 .num_tx_queues = 8, 1765 .num_ipms = 8, 1766 .tc_cbs_supported = true, 1767 .phy_side_mdio_supported = true, 1768 .ops = &lan937x_dev_ops, 1769 .phylink_mac_ops = &lan937x_phylink_mac_ops, 1770 .mib_names = ksz9477_mib_names, 1771 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1772 .reg_mib_cnt = MIB_COUNTER_NUM, 1773 .regs = ksz9477_regs, 1774 .masks = lan937x_masks, 1775 .shifts = lan937x_shifts, 1776 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1777 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1778 .supports_mii = {false, false, false, false, true}, 1779 .supports_rmii = {false, false, false, false, true}, 1780 .supports_rgmii = {false, false, false, false, true}, 1781 .internal_phy = {true, true, true, true, false}, 1782 }, 1783 1784 [LAN9371] = { 1785 .chip_id = LAN9371_CHIP_ID, 1786 .dev_name = "LAN9371", 1787 .num_vlans = 4096, 1788 .num_alus = 1024, 1789 .num_statics = 256, 1790 .cpu_ports = 0x30, /* can be configured as cpu port */ 1791 .port_cnt = 6, /* total physical port count */ 1792 .port_nirqs = 6, 1793 .num_tx_queues = 8, 1794 .num_ipms = 8, 1795 .tc_cbs_supported = true, 1796 .phy_side_mdio_supported = true, 1797 .ops = &lan937x_dev_ops, 1798 .phylink_mac_ops = &lan937x_phylink_mac_ops, 1799 .mib_names = ksz9477_mib_names, 1800 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1801 .reg_mib_cnt = MIB_COUNTER_NUM, 1802 .regs = ksz9477_regs, 1803 .masks = lan937x_masks, 1804 .shifts = lan937x_shifts, 1805 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1806 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1807 .supports_mii = {false, false, false, false, true, true}, 1808 .supports_rmii = {false, false, false, false, true, true}, 1809 .supports_rgmii = {false, false, false, false, true, true}, 1810 .internal_phy = {true, true, true, true, false, false}, 1811 }, 1812 1813 [LAN9372] = { 1814 .chip_id = LAN9372_CHIP_ID, 1815 .dev_name = "LAN9372", 1816 .num_vlans = 4096, 1817 .num_alus = 1024, 1818 .num_statics = 256, 1819 .cpu_ports = 0x30, /* can be configured as cpu port */ 1820 .port_cnt = 8, /* total physical port count */ 1821 .port_nirqs = 6, 1822 .num_tx_queues = 8, 1823 .num_ipms = 8, 1824 .tc_cbs_supported = true, 1825 .phy_side_mdio_supported = true, 1826 .ops = &lan937x_dev_ops, 1827 .phylink_mac_ops = &lan937x_phylink_mac_ops, 1828 .mib_names = ksz9477_mib_names, 1829 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1830 .reg_mib_cnt = MIB_COUNTER_NUM, 1831 .regs = ksz9477_regs, 1832 .masks = lan937x_masks, 1833 .shifts = lan937x_shifts, 1834 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1835 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1836 .supports_mii = {false, false, false, false, 1837 true, true, false, false}, 1838 .supports_rmii = {false, false, false, false, 1839 true, true, false, false}, 1840 .supports_rgmii = {false, false, false, false, 1841 true, true, false, false}, 1842 .internal_phy = {true, true, true, true, 1843 false, false, true, true}, 1844 }, 1845 1846 [LAN9373] = { 1847 .chip_id = LAN9373_CHIP_ID, 1848 .dev_name = "LAN9373", 1849 .num_vlans = 4096, 1850 .num_alus = 1024, 1851 .num_statics = 256, 1852 .cpu_ports = 0x38, /* can be configured as cpu port */ 1853 .port_cnt = 5, /* total physical port count */ 1854 .port_nirqs = 6, 1855 .num_tx_queues = 8, 1856 .num_ipms = 8, 1857 .tc_cbs_supported = true, 1858 .phy_side_mdio_supported = true, 1859 .ops = &lan937x_dev_ops, 1860 .phylink_mac_ops = &lan937x_phylink_mac_ops, 1861 .mib_names = ksz9477_mib_names, 1862 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1863 .reg_mib_cnt = MIB_COUNTER_NUM, 1864 .regs = ksz9477_regs, 1865 .masks = lan937x_masks, 1866 .shifts = lan937x_shifts, 1867 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1868 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1869 .supports_mii = {false, false, false, false, 1870 true, true, false, false}, 1871 .supports_rmii = {false, false, false, false, 1872 true, true, false, false}, 1873 .supports_rgmii = {false, false, false, false, 1874 true, true, false, false}, 1875 .internal_phy = {true, true, true, false, 1876 false, false, true, true}, 1877 }, 1878 1879 [LAN9374] = { 1880 .chip_id = LAN9374_CHIP_ID, 1881 .dev_name = "LAN9374", 1882 .num_vlans = 4096, 1883 .num_alus = 1024, 1884 .num_statics = 256, 1885 .cpu_ports = 0x30, /* can be configured as cpu port */ 1886 .port_cnt = 8, /* total physical port count */ 1887 .port_nirqs = 6, 1888 .num_tx_queues = 8, 1889 .num_ipms = 8, 1890 .tc_cbs_supported = true, 1891 .phy_side_mdio_supported = true, 1892 .ops = &lan937x_dev_ops, 1893 .phylink_mac_ops = &lan937x_phylink_mac_ops, 1894 .mib_names = ksz9477_mib_names, 1895 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1896 .reg_mib_cnt = MIB_COUNTER_NUM, 1897 .regs = ksz9477_regs, 1898 .masks = lan937x_masks, 1899 .shifts = lan937x_shifts, 1900 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1901 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1902 .supports_mii = {false, false, false, false, 1903 true, true, false, false}, 1904 .supports_rmii = {false, false, false, false, 1905 true, true, false, false}, 1906 .supports_rgmii = {false, false, false, false, 1907 true, true, false, false}, 1908 .internal_phy = {true, true, true, true, 1909 false, false, true, true}, 1910 }, 1911 1912 [LAN9646] = { 1913 .chip_id = LAN9646_CHIP_ID, 1914 .dev_name = "LAN9646", 1915 .num_vlans = 4096, 1916 .num_alus = 4096, 1917 .num_statics = 16, 1918 .cpu_ports = 0x7F, /* can be configured as cpu port */ 1919 .port_cnt = 7, /* total physical port count */ 1920 .port_nirqs = 4, 1921 .num_tx_queues = 4, 1922 .num_ipms = 8, 1923 .ops = &ksz9477_dev_ops, 1924 .phylink_mac_ops = &ksz9477_phylink_mac_ops, 1925 .phy_errata_9477 = true, 1926 .mib_names = ksz9477_mib_names, 1927 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names), 1928 .reg_mib_cnt = MIB_COUNTER_NUM, 1929 .regs = ksz9477_regs, 1930 .masks = ksz9477_masks, 1931 .shifts = ksz9477_shifts, 1932 .xmii_ctrl0 = ksz9477_xmii_ctrl0, 1933 .xmii_ctrl1 = ksz9477_xmii_ctrl1, 1934 .supports_mii = {false, false, false, false, 1935 false, true, true}, 1936 .supports_rmii = {false, false, false, false, 1937 false, true, true}, 1938 .supports_rgmii = {false, false, false, false, 1939 false, true, true}, 1940 .internal_phy = {true, true, true, true, 1941 true, false, false}, 1942 .gbit_capable = {true, true, true, true, true, true, true}, 1943 .wr_table = &ksz9477_register_set, 1944 .rd_table = &ksz9477_register_set, 1945 }, 1946 }; 1947 EXPORT_SYMBOL_GPL(ksz_switch_chips); 1948 1949 static const struct ksz_chip_data *ksz_lookup_info(unsigned int prod_num) 1950 { 1951 int i; 1952 1953 for (i = 0; i < ARRAY_SIZE(ksz_switch_chips); i++) { 1954 const struct ksz_chip_data *chip = &ksz_switch_chips[i]; 1955 1956 if (chip->chip_id == prod_num) 1957 return chip; 1958 } 1959 1960 return NULL; 1961 } 1962 1963 static int ksz_check_device_id(struct ksz_device *dev) 1964 { 1965 const struct ksz_chip_data *expected_chip_data; 1966 u32 expected_chip_id; 1967 1968 if (dev->pdata) { 1969 expected_chip_id = dev->pdata->chip_id; 1970 expected_chip_data = ksz_lookup_info(expected_chip_id); 1971 if (WARN_ON(!expected_chip_data)) 1972 return -ENODEV; 1973 } else { 1974 expected_chip_data = of_device_get_match_data(dev->dev); 1975 expected_chip_id = expected_chip_data->chip_id; 1976 } 1977 1978 if (expected_chip_id != dev->chip_id) { 1979 dev_err(dev->dev, 1980 "Device tree specifies chip %s but found %s, please fix it!\n", 1981 expected_chip_data->dev_name, dev->info->dev_name); 1982 return -ENODEV; 1983 } 1984 1985 return 0; 1986 } 1987 1988 static void ksz_phylink_get_caps(struct dsa_switch *ds, int port, 1989 struct phylink_config *config) 1990 { 1991 struct ksz_device *dev = ds->priv; 1992 1993 if (dev->info->supports_mii[port]) 1994 __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces); 1995 1996 if (dev->info->supports_rmii[port]) 1997 __set_bit(PHY_INTERFACE_MODE_RMII, 1998 config->supported_interfaces); 1999 2000 if (dev->info->supports_rgmii[port]) 2001 phy_interface_set_rgmii(config->supported_interfaces); 2002 2003 if (dev->info->internal_phy[port]) { 2004 __set_bit(PHY_INTERFACE_MODE_INTERNAL, 2005 config->supported_interfaces); 2006 /* Compatibility for phylib's default interface type when the 2007 * phy-mode property is absent 2008 */ 2009 __set_bit(PHY_INTERFACE_MODE_GMII, 2010 config->supported_interfaces); 2011 } 2012 2013 if (dev->dev_ops->get_caps) 2014 dev->dev_ops->get_caps(dev, port, config); 2015 } 2016 2017 void ksz_r_mib_stats64(struct ksz_device *dev, int port) 2018 { 2019 struct ethtool_pause_stats *pstats; 2020 struct rtnl_link_stats64 *stats; 2021 struct ksz_stats_raw *raw; 2022 struct ksz_port_mib *mib; 2023 int ret; 2024 2025 mib = &dev->ports[port].mib; 2026 stats = &mib->stats64; 2027 pstats = &mib->pause_stats; 2028 raw = (struct ksz_stats_raw *)mib->counters; 2029 2030 spin_lock(&mib->stats64_lock); 2031 2032 stats->rx_packets = raw->rx_bcast + raw->rx_mcast + raw->rx_ucast + 2033 raw->rx_pause; 2034 stats->tx_packets = raw->tx_bcast + raw->tx_mcast + raw->tx_ucast + 2035 raw->tx_pause; 2036 2037 /* HW counters are counting bytes + FCS which is not acceptable 2038 * for rtnl_link_stats64 interface 2039 */ 2040 stats->rx_bytes = raw->rx_total - stats->rx_packets * ETH_FCS_LEN; 2041 stats->tx_bytes = raw->tx_total - stats->tx_packets * ETH_FCS_LEN; 2042 2043 stats->rx_length_errors = raw->rx_undersize + raw->rx_fragments + 2044 raw->rx_oversize; 2045 2046 stats->rx_crc_errors = raw->rx_crc_err; 2047 stats->rx_frame_errors = raw->rx_align_err; 2048 stats->rx_dropped = raw->rx_discards; 2049 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors + 2050 stats->rx_frame_errors + stats->rx_dropped; 2051 2052 stats->tx_window_errors = raw->tx_late_col; 2053 stats->tx_fifo_errors = raw->tx_discards; 2054 stats->tx_aborted_errors = raw->tx_exc_col; 2055 stats->tx_errors = stats->tx_window_errors + stats->tx_fifo_errors + 2056 stats->tx_aborted_errors; 2057 2058 stats->multicast = raw->rx_mcast; 2059 stats->collisions = raw->tx_total_col; 2060 2061 pstats->tx_pause_frames = raw->tx_pause; 2062 pstats->rx_pause_frames = raw->rx_pause; 2063 2064 spin_unlock(&mib->stats64_lock); 2065 2066 if (dev->info->phy_errata_9477) { 2067 ret = ksz9477_errata_monitor(dev, port, raw->tx_late_col); 2068 if (ret) 2069 dev_err(dev->dev, "Failed to monitor transmission halt\n"); 2070 } 2071 } 2072 2073 void ksz88xx_r_mib_stats64(struct ksz_device *dev, int port) 2074 { 2075 struct ethtool_pause_stats *pstats; 2076 struct rtnl_link_stats64 *stats; 2077 struct ksz88xx_stats_raw *raw; 2078 struct ksz_port_mib *mib; 2079 2080 mib = &dev->ports[port].mib; 2081 stats = &mib->stats64; 2082 pstats = &mib->pause_stats; 2083 raw = (struct ksz88xx_stats_raw *)mib->counters; 2084 2085 spin_lock(&mib->stats64_lock); 2086 2087 stats->rx_packets = raw->rx_bcast + raw->rx_mcast + raw->rx_ucast + 2088 raw->rx_pause; 2089 stats->tx_packets = raw->tx_bcast + raw->tx_mcast + raw->tx_ucast + 2090 raw->tx_pause; 2091 2092 /* HW counters are counting bytes + FCS which is not acceptable 2093 * for rtnl_link_stats64 interface 2094 */ 2095 stats->rx_bytes = raw->rx + raw->rx_hi - stats->rx_packets * ETH_FCS_LEN; 2096 stats->tx_bytes = raw->tx + raw->tx_hi - stats->tx_packets * ETH_FCS_LEN; 2097 2098 stats->rx_length_errors = raw->rx_undersize + raw->rx_fragments + 2099 raw->rx_oversize; 2100 2101 stats->rx_crc_errors = raw->rx_crc_err; 2102 stats->rx_frame_errors = raw->rx_align_err; 2103 stats->rx_dropped = raw->rx_discards; 2104 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors + 2105 stats->rx_frame_errors + stats->rx_dropped; 2106 2107 stats->tx_window_errors = raw->tx_late_col; 2108 stats->tx_fifo_errors = raw->tx_discards; 2109 stats->tx_aborted_errors = raw->tx_exc_col; 2110 stats->tx_errors = stats->tx_window_errors + stats->tx_fifo_errors + 2111 stats->tx_aborted_errors; 2112 2113 stats->multicast = raw->rx_mcast; 2114 stats->collisions = raw->tx_total_col; 2115 2116 pstats->tx_pause_frames = raw->tx_pause; 2117 pstats->rx_pause_frames = raw->rx_pause; 2118 2119 spin_unlock(&mib->stats64_lock); 2120 } 2121 2122 static void ksz_get_stats64(struct dsa_switch *ds, int port, 2123 struct rtnl_link_stats64 *s) 2124 { 2125 struct ksz_device *dev = ds->priv; 2126 struct ksz_port_mib *mib; 2127 2128 mib = &dev->ports[port].mib; 2129 2130 spin_lock(&mib->stats64_lock); 2131 memcpy(s, &mib->stats64, sizeof(*s)); 2132 spin_unlock(&mib->stats64_lock); 2133 } 2134 2135 static void ksz_get_pause_stats(struct dsa_switch *ds, int port, 2136 struct ethtool_pause_stats *pause_stats) 2137 { 2138 struct ksz_device *dev = ds->priv; 2139 struct ksz_port_mib *mib; 2140 2141 mib = &dev->ports[port].mib; 2142 2143 spin_lock(&mib->stats64_lock); 2144 memcpy(pause_stats, &mib->pause_stats, sizeof(*pause_stats)); 2145 spin_unlock(&mib->stats64_lock); 2146 } 2147 2148 static void ksz_get_strings(struct dsa_switch *ds, int port, 2149 u32 stringset, uint8_t *buf) 2150 { 2151 struct ksz_device *dev = ds->priv; 2152 int i; 2153 2154 if (stringset != ETH_SS_STATS) 2155 return; 2156 2157 for (i = 0; i < dev->info->mib_cnt; i++) 2158 ethtool_puts(&buf, dev->info->mib_names[i].string); 2159 } 2160 2161 /** 2162 * ksz_update_port_member - Adjust port forwarding rules based on STP state and 2163 * isolation settings. 2164 * @dev: A pointer to the struct ksz_device representing the device. 2165 * @port: The port number to adjust. 2166 * 2167 * This function dynamically adjusts the port membership configuration for a 2168 * specified port and other device ports, based on Spanning Tree Protocol (STP) 2169 * states and port isolation settings. Each port, including the CPU port, has a 2170 * membership register, represented as a bitfield, where each bit corresponds 2171 * to a port number. A set bit indicates permission to forward frames to that 2172 * port. This function iterates over all ports, updating the membership register 2173 * to reflect current forwarding permissions: 2174 * 2175 * 1. Forwards frames only to ports that are part of the same bridge group and 2176 * in the BR_STATE_FORWARDING state. 2177 * 2. Takes into account the isolation status of ports; ports in the 2178 * BR_STATE_FORWARDING state with BR_ISOLATED configuration will not forward 2179 * frames to each other, even if they are in the same bridge group. 2180 * 3. Ensures that the CPU port is included in the membership based on its 2181 * upstream port configuration, allowing for management and control traffic 2182 * to flow as required. 2183 */ 2184 static void ksz_update_port_member(struct ksz_device *dev, int port) 2185 { 2186 struct ksz_port *p = &dev->ports[port]; 2187 struct dsa_switch *ds = dev->ds; 2188 u8 port_member = 0, cpu_port; 2189 const struct dsa_port *dp; 2190 int i, j; 2191 2192 if (!dsa_is_user_port(ds, port)) 2193 return; 2194 2195 dp = dsa_to_port(ds, port); 2196 cpu_port = BIT(dsa_upstream_port(ds, port)); 2197 2198 for (i = 0; i < ds->num_ports; i++) { 2199 const struct dsa_port *other_dp = dsa_to_port(ds, i); 2200 struct ksz_port *other_p = &dev->ports[i]; 2201 u8 val = 0; 2202 2203 if (!dsa_is_user_port(ds, i)) 2204 continue; 2205 if (port == i) 2206 continue; 2207 if (!dsa_port_bridge_same(dp, other_dp)) 2208 continue; 2209 if (other_p->stp_state != BR_STATE_FORWARDING) 2210 continue; 2211 2212 /* At this point we know that "port" and "other" port [i] are in 2213 * the same bridge group and that "other" port [i] is in 2214 * forwarding stp state. If "port" is also in forwarding stp 2215 * state, we can allow forwarding from port [port] to port [i]. 2216 * Except if both ports are isolated. 2217 */ 2218 if (p->stp_state == BR_STATE_FORWARDING && 2219 !(p->isolated && other_p->isolated)) { 2220 val |= BIT(port); 2221 port_member |= BIT(i); 2222 } 2223 2224 /* Retain port [i]'s relationship to other ports than [port] */ 2225 for (j = 0; j < ds->num_ports; j++) { 2226 const struct dsa_port *third_dp; 2227 struct ksz_port *third_p; 2228 2229 if (j == i) 2230 continue; 2231 if (j == port) 2232 continue; 2233 if (!dsa_is_user_port(ds, j)) 2234 continue; 2235 third_p = &dev->ports[j]; 2236 if (third_p->stp_state != BR_STATE_FORWARDING) 2237 continue; 2238 2239 third_dp = dsa_to_port(ds, j); 2240 2241 /* Now we updating relation of the "other" port [i] to 2242 * the "third" port [j]. We already know that "other" 2243 * port [i] is in forwarding stp state and that "third" 2244 * port [j] is in forwarding stp state too. 2245 * We need to check if "other" port [i] and "third" port 2246 * [j] are in the same bridge group and not isolated 2247 * before allowing forwarding from port [i] to port [j]. 2248 */ 2249 if (dsa_port_bridge_same(other_dp, third_dp) && 2250 !(other_p->isolated && third_p->isolated)) 2251 val |= BIT(j); 2252 } 2253 2254 dev->dev_ops->cfg_port_member(dev, i, val | cpu_port); 2255 } 2256 2257 dev->dev_ops->cfg_port_member(dev, port, port_member | cpu_port); 2258 } 2259 2260 static int ksz_sw_mdio_read(struct mii_bus *bus, int addr, int regnum) 2261 { 2262 struct ksz_device *dev = bus->priv; 2263 u16 val; 2264 int ret; 2265 2266 ret = dev->dev_ops->r_phy(dev, addr, regnum, &val); 2267 if (ret < 0) 2268 return ret; 2269 2270 return val; 2271 } 2272 2273 static int ksz_sw_mdio_write(struct mii_bus *bus, int addr, int regnum, 2274 u16 val) 2275 { 2276 struct ksz_device *dev = bus->priv; 2277 2278 return dev->dev_ops->w_phy(dev, addr, regnum, val); 2279 } 2280 2281 /** 2282 * ksz_parent_mdio_read - Read data from a PHY register on the parent MDIO bus. 2283 * @bus: MDIO bus structure. 2284 * @addr: PHY address on the parent MDIO bus. 2285 * @regnum: Register number to read. 2286 * 2287 * This function provides a direct read operation on the parent MDIO bus for 2288 * accessing PHY registers. By bypassing SPI or I2C, it uses the parent MDIO bus 2289 * to retrieve data from the PHY registers at the specified address and register 2290 * number. 2291 * 2292 * Return: Value of the PHY register, or a negative error code on failure. 2293 */ 2294 static int ksz_parent_mdio_read(struct mii_bus *bus, int addr, int regnum) 2295 { 2296 struct ksz_device *dev = bus->priv; 2297 2298 return mdiobus_read_nested(dev->parent_mdio_bus, addr, regnum); 2299 } 2300 2301 /** 2302 * ksz_parent_mdio_write - Write data to a PHY register on the parent MDIO bus. 2303 * @bus: MDIO bus structure. 2304 * @addr: PHY address on the parent MDIO bus. 2305 * @regnum: Register number to write to. 2306 * @val: Value to write to the PHY register. 2307 * 2308 * This function provides a direct write operation on the parent MDIO bus for 2309 * accessing PHY registers. Bypassing SPI or I2C, it uses the parent MDIO bus 2310 * to modify the PHY register values at the specified address. 2311 * 2312 * Return: 0 on success, or a negative error code on failure. 2313 */ 2314 static int ksz_parent_mdio_write(struct mii_bus *bus, int addr, int regnum, 2315 u16 val) 2316 { 2317 struct ksz_device *dev = bus->priv; 2318 2319 return mdiobus_write_nested(dev->parent_mdio_bus, addr, regnum, val); 2320 } 2321 2322 /** 2323 * ksz_phy_addr_to_port - Map a PHY address to the corresponding switch port. 2324 * @dev: Pointer to device structure. 2325 * @addr: PHY address to map to a port. 2326 * 2327 * This function finds the corresponding switch port for a given PHY address by 2328 * iterating over all user ports on the device. It checks if a port's PHY 2329 * address in `phy_addr_map` matches the specified address and if the port 2330 * contains an internal PHY. If a match is found, the index of the port is 2331 * returned. 2332 * 2333 * Return: Port index on success, or -EINVAL if no matching port is found. 2334 */ 2335 static int ksz_phy_addr_to_port(struct ksz_device *dev, int addr) 2336 { 2337 struct dsa_switch *ds = dev->ds; 2338 struct dsa_port *dp; 2339 2340 dsa_switch_for_each_user_port(dp, ds) { 2341 if (dev->info->internal_phy[dp->index] && 2342 dev->phy_addr_map[dp->index] == addr) 2343 return dp->index; 2344 } 2345 2346 return -EINVAL; 2347 } 2348 2349 /** 2350 * ksz_irq_phy_setup - Configure IRQs for PHYs in the KSZ device. 2351 * @dev: Pointer to the KSZ device structure. 2352 * 2353 * Sets up IRQs for each active PHY connected to the KSZ switch by mapping the 2354 * appropriate IRQs for each PHY and assigning them to the `user_mii_bus` in 2355 * the DSA switch structure. Each IRQ is mapped based on the port's IRQ domain. 2356 * 2357 * Return: 0 on success, or a negative error code on failure. 2358 */ 2359 static int ksz_irq_phy_setup(struct ksz_device *dev) 2360 { 2361 struct dsa_switch *ds = dev->ds; 2362 int phy, port; 2363 int irq; 2364 int ret; 2365 2366 for (phy = 0; phy < PHY_MAX_ADDR; phy++) { 2367 if (BIT(phy) & ds->phys_mii_mask) { 2368 port = ksz_phy_addr_to_port(dev, phy); 2369 if (port < 0) { 2370 ret = port; 2371 goto out; 2372 } 2373 2374 irq = irq_find_mapping(dev->ports[port].pirq.domain, 2375 PORT_SRC_PHY_INT); 2376 if (irq < 0) { 2377 ret = irq; 2378 goto out; 2379 } 2380 ds->user_mii_bus->irq[phy] = irq; 2381 } 2382 } 2383 return 0; 2384 out: 2385 while (phy--) 2386 if (BIT(phy) & ds->phys_mii_mask) 2387 irq_dispose_mapping(ds->user_mii_bus->irq[phy]); 2388 2389 return ret; 2390 } 2391 2392 /** 2393 * ksz_irq_phy_free - Release IRQ mappings for PHYs in the KSZ device. 2394 * @dev: Pointer to the KSZ device structure. 2395 * 2396 * Releases any IRQ mappings previously assigned to active PHYs in the KSZ 2397 * switch by disposing of each mapped IRQ in the `user_mii_bus` structure. 2398 */ 2399 static void ksz_irq_phy_free(struct ksz_device *dev) 2400 { 2401 struct dsa_switch *ds = dev->ds; 2402 int phy; 2403 2404 for (phy = 0; phy < PHY_MAX_ADDR; phy++) 2405 if (BIT(phy) & ds->phys_mii_mask) 2406 irq_dispose_mapping(ds->user_mii_bus->irq[phy]); 2407 } 2408 2409 /** 2410 * ksz_parse_dt_phy_config - Parse and validate PHY configuration from DT 2411 * @dev: pointer to the KSZ device structure 2412 * @bus: pointer to the MII bus structure 2413 * @mdio_np: pointer to the MDIO node in the device tree 2414 * 2415 * This function parses and validates PHY configurations for each user port 2416 * defined in the device tree for a KSZ switch device. It verifies that the 2417 * `phy-handle` properties are correctly set and that the internal PHYs match 2418 * expected addresses and parent nodes. Sets up the PHY mask in the MII bus if 2419 * all validations pass. Logs error messages for any mismatches or missing data. 2420 * 2421 * Return: 0 on success, or a negative error code on failure. 2422 */ 2423 static int ksz_parse_dt_phy_config(struct ksz_device *dev, struct mii_bus *bus, 2424 struct device_node *mdio_np) 2425 { 2426 struct device_node *phy_node, *phy_parent_node; 2427 bool phys_are_valid = true; 2428 struct dsa_port *dp; 2429 u32 phy_addr; 2430 int ret; 2431 2432 dsa_switch_for_each_user_port(dp, dev->ds) { 2433 if (!dev->info->internal_phy[dp->index]) 2434 continue; 2435 2436 phy_node = of_parse_phandle(dp->dn, "phy-handle", 0); 2437 if (!phy_node) { 2438 dev_err(dev->dev, "failed to parse phy-handle for port %d.\n", 2439 dp->index); 2440 phys_are_valid = false; 2441 continue; 2442 } 2443 2444 phy_parent_node = of_get_parent(phy_node); 2445 if (!phy_parent_node) { 2446 dev_err(dev->dev, "failed to get PHY-parent node for port %d\n", 2447 dp->index); 2448 phys_are_valid = false; 2449 } else if (phy_parent_node != mdio_np) { 2450 dev_err(dev->dev, "PHY-parent node mismatch for port %d, expected %pOF, got %pOF\n", 2451 dp->index, mdio_np, phy_parent_node); 2452 phys_are_valid = false; 2453 } else { 2454 ret = of_property_read_u32(phy_node, "reg", &phy_addr); 2455 if (ret < 0) { 2456 dev_err(dev->dev, "failed to read PHY address for port %d. Error %d\n", 2457 dp->index, ret); 2458 phys_are_valid = false; 2459 } else if (phy_addr != dev->phy_addr_map[dp->index]) { 2460 dev_err(dev->dev, "PHY address mismatch for port %d, expected 0x%x, got 0x%x\n", 2461 dp->index, dev->phy_addr_map[dp->index], 2462 phy_addr); 2463 phys_are_valid = false; 2464 } else { 2465 bus->phy_mask |= BIT(phy_addr); 2466 } 2467 } 2468 2469 of_node_put(phy_node); 2470 of_node_put(phy_parent_node); 2471 } 2472 2473 if (!phys_are_valid) 2474 return -EINVAL; 2475 2476 return 0; 2477 } 2478 2479 /** 2480 * ksz_mdio_register - Register and configure the MDIO bus for the KSZ device. 2481 * @dev: Pointer to the KSZ device structure. 2482 * 2483 * This function sets up and registers an MDIO bus for the KSZ switch device, 2484 * allowing access to its internal PHYs. If the device supports side MDIO, 2485 * the function will configure the external MDIO controller specified by the 2486 * "mdio-parent-bus" device tree property to directly manage internal PHYs. 2487 * Otherwise, SPI or I2C access is set up for PHY access. 2488 * 2489 * Return: 0 on success, or a negative error code on failure. 2490 */ 2491 static int ksz_mdio_register(struct ksz_device *dev) 2492 { 2493 struct device_node *parent_bus_node; 2494 struct mii_bus *parent_bus = NULL; 2495 struct dsa_switch *ds = dev->ds; 2496 struct device_node *mdio_np; 2497 struct mii_bus *bus; 2498 int ret, i; 2499 2500 mdio_np = of_get_child_by_name(dev->dev->of_node, "mdio"); 2501 if (!mdio_np) 2502 return 0; 2503 2504 parent_bus_node = of_parse_phandle(mdio_np, "mdio-parent-bus", 0); 2505 if (parent_bus_node && !dev->info->phy_side_mdio_supported) { 2506 dev_err(dev->dev, "Side MDIO bus is not supported for this HW, ignoring 'mdio-parent-bus' property.\n"); 2507 ret = -EINVAL; 2508 2509 goto put_mdio_node; 2510 } else if (parent_bus_node) { 2511 parent_bus = of_mdio_find_bus(parent_bus_node); 2512 if (!parent_bus) { 2513 ret = -EPROBE_DEFER; 2514 2515 goto put_mdio_node; 2516 } 2517 2518 dev->parent_mdio_bus = parent_bus; 2519 } 2520 2521 bus = devm_mdiobus_alloc(ds->dev); 2522 if (!bus) { 2523 ret = -ENOMEM; 2524 goto put_mdio_node; 2525 } 2526 2527 if (dev->dev_ops->mdio_bus_preinit) { 2528 ret = dev->dev_ops->mdio_bus_preinit(dev, !!parent_bus); 2529 if (ret) 2530 goto put_mdio_node; 2531 } 2532 2533 if (dev->dev_ops->create_phy_addr_map) { 2534 ret = dev->dev_ops->create_phy_addr_map(dev, !!parent_bus); 2535 if (ret) 2536 goto put_mdio_node; 2537 } else { 2538 for (i = 0; i < dev->info->port_cnt; i++) 2539 dev->phy_addr_map[i] = i; 2540 } 2541 2542 bus->priv = dev; 2543 if (parent_bus) { 2544 bus->read = ksz_parent_mdio_read; 2545 bus->write = ksz_parent_mdio_write; 2546 bus->name = "KSZ side MDIO"; 2547 snprintf(bus->id, MII_BUS_ID_SIZE, "ksz-side-mdio-%d", 2548 ds->index); 2549 } else { 2550 bus->read = ksz_sw_mdio_read; 2551 bus->write = ksz_sw_mdio_write; 2552 bus->name = "ksz user smi"; 2553 snprintf(bus->id, MII_BUS_ID_SIZE, "SMI-%d", ds->index); 2554 } 2555 2556 ret = ksz_parse_dt_phy_config(dev, bus, mdio_np); 2557 if (ret) 2558 goto put_mdio_node; 2559 2560 ds->phys_mii_mask = bus->phy_mask; 2561 bus->parent = ds->dev; 2562 2563 ds->user_mii_bus = bus; 2564 2565 if (dev->irq > 0) { 2566 ret = ksz_irq_phy_setup(dev); 2567 if (ret) 2568 goto put_mdio_node; 2569 } 2570 2571 ret = devm_of_mdiobus_register(ds->dev, bus, mdio_np); 2572 if (ret) { 2573 dev_err(ds->dev, "unable to register MDIO bus %s\n", 2574 bus->id); 2575 if (dev->irq > 0) 2576 ksz_irq_phy_free(dev); 2577 } 2578 2579 put_mdio_node: 2580 of_node_put(mdio_np); 2581 of_node_put(parent_bus_node); 2582 2583 return ret; 2584 } 2585 2586 static void ksz_irq_mask(struct irq_data *d) 2587 { 2588 struct ksz_irq *kirq = irq_data_get_irq_chip_data(d); 2589 2590 kirq->masked |= BIT(d->hwirq); 2591 } 2592 2593 static void ksz_irq_unmask(struct irq_data *d) 2594 { 2595 struct ksz_irq *kirq = irq_data_get_irq_chip_data(d); 2596 2597 kirq->masked &= ~BIT(d->hwirq); 2598 } 2599 2600 static void ksz_irq_bus_lock(struct irq_data *d) 2601 { 2602 struct ksz_irq *kirq = irq_data_get_irq_chip_data(d); 2603 2604 mutex_lock(&kirq->dev->lock_irq); 2605 } 2606 2607 static void ksz_irq_bus_sync_unlock(struct irq_data *d) 2608 { 2609 struct ksz_irq *kirq = irq_data_get_irq_chip_data(d); 2610 struct ksz_device *dev = kirq->dev; 2611 int ret; 2612 2613 ret = ksz_write8(dev, kirq->reg_mask, kirq->masked); 2614 if (ret) 2615 dev_err(dev->dev, "failed to change IRQ mask\n"); 2616 2617 mutex_unlock(&dev->lock_irq); 2618 } 2619 2620 static const struct irq_chip ksz_irq_chip = { 2621 .name = "ksz-irq", 2622 .irq_mask = ksz_irq_mask, 2623 .irq_unmask = ksz_irq_unmask, 2624 .irq_bus_lock = ksz_irq_bus_lock, 2625 .irq_bus_sync_unlock = ksz_irq_bus_sync_unlock, 2626 }; 2627 2628 static int ksz_irq_domain_map(struct irq_domain *d, 2629 unsigned int irq, irq_hw_number_t hwirq) 2630 { 2631 irq_set_chip_data(irq, d->host_data); 2632 irq_set_chip_and_handler(irq, &ksz_irq_chip, handle_level_irq); 2633 irq_set_noprobe(irq); 2634 2635 return 0; 2636 } 2637 2638 static const struct irq_domain_ops ksz_irq_domain_ops = { 2639 .map = ksz_irq_domain_map, 2640 .xlate = irq_domain_xlate_twocell, 2641 }; 2642 2643 static void ksz_irq_free(struct ksz_irq *kirq) 2644 { 2645 int irq, virq; 2646 2647 free_irq(kirq->irq_num, kirq); 2648 2649 for (irq = 0; irq < kirq->nirqs; irq++) { 2650 virq = irq_find_mapping(kirq->domain, irq); 2651 irq_dispose_mapping(virq); 2652 } 2653 2654 irq_domain_remove(kirq->domain); 2655 } 2656 2657 static irqreturn_t ksz_irq_thread_fn(int irq, void *dev_id) 2658 { 2659 struct ksz_irq *kirq = dev_id; 2660 unsigned int nhandled = 0; 2661 struct ksz_device *dev; 2662 unsigned int sub_irq; 2663 u8 data; 2664 int ret; 2665 u8 n; 2666 2667 dev = kirq->dev; 2668 2669 /* Read interrupt status register */ 2670 ret = ksz_read8(dev, kirq->reg_status, &data); 2671 if (ret) 2672 goto out; 2673 2674 for (n = 0; n < kirq->nirqs; ++n) { 2675 if (data & BIT(n)) { 2676 sub_irq = irq_find_mapping(kirq->domain, n); 2677 handle_nested_irq(sub_irq); 2678 ++nhandled; 2679 } 2680 } 2681 out: 2682 return (nhandled > 0 ? IRQ_HANDLED : IRQ_NONE); 2683 } 2684 2685 static int ksz_irq_common_setup(struct ksz_device *dev, struct ksz_irq *kirq) 2686 { 2687 int ret, n; 2688 2689 kirq->dev = dev; 2690 kirq->masked = ~0; 2691 2692 kirq->domain = irq_domain_add_simple(dev->dev->of_node, kirq->nirqs, 0, 2693 &ksz_irq_domain_ops, kirq); 2694 if (!kirq->domain) 2695 return -ENOMEM; 2696 2697 for (n = 0; n < kirq->nirqs; n++) 2698 irq_create_mapping(kirq->domain, n); 2699 2700 ret = request_threaded_irq(kirq->irq_num, NULL, ksz_irq_thread_fn, 2701 IRQF_ONESHOT, kirq->name, kirq); 2702 if (ret) 2703 goto out; 2704 2705 return 0; 2706 2707 out: 2708 ksz_irq_free(kirq); 2709 2710 return ret; 2711 } 2712 2713 static int ksz_girq_setup(struct ksz_device *dev) 2714 { 2715 struct ksz_irq *girq = &dev->girq; 2716 2717 girq->nirqs = dev->info->port_cnt; 2718 girq->reg_mask = REG_SW_PORT_INT_MASK__1; 2719 girq->reg_status = REG_SW_PORT_INT_STATUS__1; 2720 snprintf(girq->name, sizeof(girq->name), "global_port_irq"); 2721 2722 girq->irq_num = dev->irq; 2723 2724 return ksz_irq_common_setup(dev, girq); 2725 } 2726 2727 static int ksz_pirq_setup(struct ksz_device *dev, u8 p) 2728 { 2729 struct ksz_irq *pirq = &dev->ports[p].pirq; 2730 2731 pirq->nirqs = dev->info->port_nirqs; 2732 pirq->reg_mask = dev->dev_ops->get_port_addr(p, REG_PORT_INT_MASK); 2733 pirq->reg_status = dev->dev_ops->get_port_addr(p, REG_PORT_INT_STATUS); 2734 snprintf(pirq->name, sizeof(pirq->name), "port_irq-%d", p); 2735 2736 pirq->irq_num = irq_find_mapping(dev->girq.domain, p); 2737 if (pirq->irq_num < 0) 2738 return pirq->irq_num; 2739 2740 return ksz_irq_common_setup(dev, pirq); 2741 } 2742 2743 static int ksz_parse_drive_strength(struct ksz_device *dev); 2744 2745 static int ksz_setup(struct dsa_switch *ds) 2746 { 2747 struct ksz_device *dev = ds->priv; 2748 struct dsa_port *dp; 2749 struct ksz_port *p; 2750 const u16 *regs; 2751 int ret; 2752 2753 regs = dev->info->regs; 2754 2755 dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table), 2756 dev->info->num_vlans, GFP_KERNEL); 2757 if (!dev->vlan_cache) 2758 return -ENOMEM; 2759 2760 ret = dev->dev_ops->reset(dev); 2761 if (ret) { 2762 dev_err(ds->dev, "failed to reset switch\n"); 2763 return ret; 2764 } 2765 2766 ret = ksz_parse_drive_strength(dev); 2767 if (ret) 2768 return ret; 2769 2770 /* set broadcast storm protection 10% rate */ 2771 regmap_update_bits(ksz_regmap_16(dev), regs[S_BROADCAST_CTRL], 2772 BROADCAST_STORM_RATE, 2773 (BROADCAST_STORM_VALUE * 2774 BROADCAST_STORM_PROT_RATE) / 100); 2775 2776 dev->dev_ops->config_cpu_port(ds); 2777 2778 dev->dev_ops->enable_stp_addr(dev); 2779 2780 ds->num_tx_queues = dev->info->num_tx_queues; 2781 2782 regmap_update_bits(ksz_regmap_8(dev), regs[S_MULTICAST_CTRL], 2783 MULTICAST_STORM_DISABLE, MULTICAST_STORM_DISABLE); 2784 2785 ksz_init_mib_timer(dev); 2786 2787 ds->configure_vlan_while_not_filtering = false; 2788 ds->dscp_prio_mapping_is_global = true; 2789 2790 if (dev->dev_ops->setup) { 2791 ret = dev->dev_ops->setup(ds); 2792 if (ret) 2793 return ret; 2794 } 2795 2796 /* Start with learning disabled on standalone user ports, and enabled 2797 * on the CPU port. In lack of other finer mechanisms, learning on the 2798 * CPU port will avoid flooding bridge local addresses on the network 2799 * in some cases. 2800 */ 2801 p = &dev->ports[dev->cpu_port]; 2802 p->learning = true; 2803 2804 if (dev->irq > 0) { 2805 ret = ksz_girq_setup(dev); 2806 if (ret) 2807 return ret; 2808 2809 dsa_switch_for_each_user_port(dp, dev->ds) { 2810 ret = ksz_pirq_setup(dev, dp->index); 2811 if (ret) 2812 goto out_girq; 2813 2814 ret = ksz_ptp_irq_setup(ds, dp->index); 2815 if (ret) 2816 goto out_pirq; 2817 } 2818 } 2819 2820 ret = ksz_ptp_clock_register(ds); 2821 if (ret) { 2822 dev_err(dev->dev, "Failed to register PTP clock: %d\n", ret); 2823 goto out_ptpirq; 2824 } 2825 2826 ret = ksz_mdio_register(dev); 2827 if (ret < 0) { 2828 dev_err(dev->dev, "failed to register the mdio"); 2829 goto out_ptp_clock_unregister; 2830 } 2831 2832 ret = ksz_dcb_init(dev); 2833 if (ret) 2834 goto out_ptp_clock_unregister; 2835 2836 /* start switch */ 2837 regmap_update_bits(ksz_regmap_8(dev), regs[S_START_CTRL], 2838 SW_START, SW_START); 2839 2840 return 0; 2841 2842 out_ptp_clock_unregister: 2843 ksz_ptp_clock_unregister(ds); 2844 out_ptpirq: 2845 if (dev->irq > 0) 2846 dsa_switch_for_each_user_port(dp, dev->ds) 2847 ksz_ptp_irq_free(ds, dp->index); 2848 out_pirq: 2849 if (dev->irq > 0) 2850 dsa_switch_for_each_user_port(dp, dev->ds) 2851 ksz_irq_free(&dev->ports[dp->index].pirq); 2852 out_girq: 2853 if (dev->irq > 0) 2854 ksz_irq_free(&dev->girq); 2855 2856 return ret; 2857 } 2858 2859 static void ksz_teardown(struct dsa_switch *ds) 2860 { 2861 struct ksz_device *dev = ds->priv; 2862 struct dsa_port *dp; 2863 2864 ksz_ptp_clock_unregister(ds); 2865 2866 if (dev->irq > 0) { 2867 dsa_switch_for_each_user_port(dp, dev->ds) { 2868 ksz_ptp_irq_free(ds, dp->index); 2869 2870 ksz_irq_free(&dev->ports[dp->index].pirq); 2871 } 2872 2873 ksz_irq_free(&dev->girq); 2874 } 2875 2876 if (dev->dev_ops->teardown) 2877 dev->dev_ops->teardown(ds); 2878 } 2879 2880 static void port_r_cnt(struct ksz_device *dev, int port) 2881 { 2882 struct ksz_port_mib *mib = &dev->ports[port].mib; 2883 u64 *dropped; 2884 2885 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */ 2886 while (mib->cnt_ptr < dev->info->reg_mib_cnt) { 2887 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr, 2888 &mib->counters[mib->cnt_ptr]); 2889 ++mib->cnt_ptr; 2890 } 2891 2892 /* last one in storage */ 2893 dropped = &mib->counters[dev->info->mib_cnt]; 2894 2895 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */ 2896 while (mib->cnt_ptr < dev->info->mib_cnt) { 2897 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr, 2898 dropped, &mib->counters[mib->cnt_ptr]); 2899 ++mib->cnt_ptr; 2900 } 2901 mib->cnt_ptr = 0; 2902 } 2903 2904 static void ksz_mib_read_work(struct work_struct *work) 2905 { 2906 struct ksz_device *dev = container_of(work, struct ksz_device, 2907 mib_read.work); 2908 struct ksz_port_mib *mib; 2909 struct ksz_port *p; 2910 int i; 2911 2912 for (i = 0; i < dev->info->port_cnt; i++) { 2913 if (dsa_is_unused_port(dev->ds, i)) 2914 continue; 2915 2916 p = &dev->ports[i]; 2917 mib = &p->mib; 2918 mutex_lock(&mib->cnt_mutex); 2919 2920 /* Only read MIB counters when the port is told to do. 2921 * If not, read only dropped counters when link is not up. 2922 */ 2923 if (!p->read) { 2924 const struct dsa_port *dp = dsa_to_port(dev->ds, i); 2925 2926 if (!netif_carrier_ok(dp->user)) 2927 mib->cnt_ptr = dev->info->reg_mib_cnt; 2928 } 2929 port_r_cnt(dev, i); 2930 p->read = false; 2931 2932 if (dev->dev_ops->r_mib_stat64) 2933 dev->dev_ops->r_mib_stat64(dev, i); 2934 2935 mutex_unlock(&mib->cnt_mutex); 2936 } 2937 2938 schedule_delayed_work(&dev->mib_read, dev->mib_read_interval); 2939 } 2940 2941 void ksz_init_mib_timer(struct ksz_device *dev) 2942 { 2943 int i; 2944 2945 INIT_DELAYED_WORK(&dev->mib_read, ksz_mib_read_work); 2946 2947 for (i = 0; i < dev->info->port_cnt; i++) { 2948 struct ksz_port_mib *mib = &dev->ports[i].mib; 2949 2950 dev->dev_ops->port_init_cnt(dev, i); 2951 2952 mib->cnt_ptr = 0; 2953 memset(mib->counters, 0, dev->info->mib_cnt * sizeof(u64)); 2954 } 2955 } 2956 2957 static int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg) 2958 { 2959 struct ksz_device *dev = ds->priv; 2960 u16 val = 0xffff; 2961 int ret; 2962 2963 ret = dev->dev_ops->r_phy(dev, addr, reg, &val); 2964 if (ret) 2965 return ret; 2966 2967 return val; 2968 } 2969 2970 static int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val) 2971 { 2972 struct ksz_device *dev = ds->priv; 2973 int ret; 2974 2975 ret = dev->dev_ops->w_phy(dev, addr, reg, val); 2976 if (ret) 2977 return ret; 2978 2979 return 0; 2980 } 2981 2982 static u32 ksz_get_phy_flags(struct dsa_switch *ds, int port) 2983 { 2984 struct ksz_device *dev = ds->priv; 2985 2986 switch (dev->chip_id) { 2987 case KSZ88X3_CHIP_ID: 2988 /* Silicon Errata Sheet (DS80000830A): 2989 * Port 1 does not work with LinkMD Cable-Testing. 2990 * Port 1 does not respond to received PAUSE control frames. 2991 */ 2992 if (!port) 2993 return MICREL_KSZ8_P1_ERRATA; 2994 break; 2995 case KSZ8567_CHIP_ID: 2996 /* KSZ8567R Errata DS80000752C Module 4 */ 2997 case KSZ8765_CHIP_ID: 2998 case KSZ8794_CHIP_ID: 2999 case KSZ8795_CHIP_ID: 3000 /* KSZ879x/KSZ877x/KSZ876x Errata DS80000687C Module 2 */ 3001 case KSZ9477_CHIP_ID: 3002 /* KSZ9477S Errata DS80000754A Module 4 */ 3003 case KSZ9567_CHIP_ID: 3004 /* KSZ9567S Errata DS80000756A Module 4 */ 3005 case KSZ9896_CHIP_ID: 3006 /* KSZ9896C Errata DS80000757A Module 3 */ 3007 case KSZ9897_CHIP_ID: 3008 case LAN9646_CHIP_ID: 3009 /* KSZ9897R Errata DS80000758C Module 4 */ 3010 /* Energy Efficient Ethernet (EEE) feature select must be manually disabled 3011 * The EEE feature is enabled by default, but it is not fully 3012 * operational. It must be manually disabled through register 3013 * controls. If not disabled, the PHY ports can auto-negotiate 3014 * to enable EEE, and this feature can cause link drops when 3015 * linked to another device supporting EEE. 3016 * 3017 * The same item appears in the errata for all switches above. 3018 */ 3019 return MICREL_NO_EEE; 3020 } 3021 3022 return 0; 3023 } 3024 3025 static void ksz_phylink_mac_link_down(struct phylink_config *config, 3026 unsigned int mode, 3027 phy_interface_t interface) 3028 { 3029 struct dsa_port *dp = dsa_phylink_to_port(config); 3030 struct ksz_device *dev = dp->ds->priv; 3031 3032 /* Read all MIB counters when the link is going down. */ 3033 dev->ports[dp->index].read = true; 3034 /* timer started */ 3035 if (dev->mib_read_interval) 3036 schedule_delayed_work(&dev->mib_read, 0); 3037 } 3038 3039 static int ksz_sset_count(struct dsa_switch *ds, int port, int sset) 3040 { 3041 struct ksz_device *dev = ds->priv; 3042 3043 if (sset != ETH_SS_STATS) 3044 return 0; 3045 3046 return dev->info->mib_cnt; 3047 } 3048 3049 static void ksz_get_ethtool_stats(struct dsa_switch *ds, int port, 3050 uint64_t *buf) 3051 { 3052 const struct dsa_port *dp = dsa_to_port(ds, port); 3053 struct ksz_device *dev = ds->priv; 3054 struct ksz_port_mib *mib; 3055 3056 mib = &dev->ports[port].mib; 3057 mutex_lock(&mib->cnt_mutex); 3058 3059 /* Only read dropped counters if no link. */ 3060 if (!netif_carrier_ok(dp->user)) 3061 mib->cnt_ptr = dev->info->reg_mib_cnt; 3062 port_r_cnt(dev, port); 3063 memcpy(buf, mib->counters, dev->info->mib_cnt * sizeof(u64)); 3064 mutex_unlock(&mib->cnt_mutex); 3065 } 3066 3067 static int ksz_port_bridge_join(struct dsa_switch *ds, int port, 3068 struct dsa_bridge bridge, 3069 bool *tx_fwd_offload, 3070 struct netlink_ext_ack *extack) 3071 { 3072 /* port_stp_state_set() will be called after to put the port in 3073 * appropriate state so there is no need to do anything. 3074 */ 3075 3076 return 0; 3077 } 3078 3079 static void ksz_port_bridge_leave(struct dsa_switch *ds, int port, 3080 struct dsa_bridge bridge) 3081 { 3082 /* port_stp_state_set() will be called after to put the port in 3083 * forwarding state so there is no need to do anything. 3084 */ 3085 } 3086 3087 static void ksz_port_fast_age(struct dsa_switch *ds, int port) 3088 { 3089 struct ksz_device *dev = ds->priv; 3090 3091 dev->dev_ops->flush_dyn_mac_table(dev, port); 3092 } 3093 3094 static int ksz_set_ageing_time(struct dsa_switch *ds, unsigned int msecs) 3095 { 3096 struct ksz_device *dev = ds->priv; 3097 3098 if (!dev->dev_ops->set_ageing_time) 3099 return -EOPNOTSUPP; 3100 3101 return dev->dev_ops->set_ageing_time(dev, msecs); 3102 } 3103 3104 static int ksz_port_fdb_add(struct dsa_switch *ds, int port, 3105 const unsigned char *addr, u16 vid, 3106 struct dsa_db db) 3107 { 3108 struct ksz_device *dev = ds->priv; 3109 3110 if (!dev->dev_ops->fdb_add) 3111 return -EOPNOTSUPP; 3112 3113 return dev->dev_ops->fdb_add(dev, port, addr, vid, db); 3114 } 3115 3116 static int ksz_port_fdb_del(struct dsa_switch *ds, int port, 3117 const unsigned char *addr, 3118 u16 vid, struct dsa_db db) 3119 { 3120 struct ksz_device *dev = ds->priv; 3121 3122 if (!dev->dev_ops->fdb_del) 3123 return -EOPNOTSUPP; 3124 3125 return dev->dev_ops->fdb_del(dev, port, addr, vid, db); 3126 } 3127 3128 static int ksz_port_fdb_dump(struct dsa_switch *ds, int port, 3129 dsa_fdb_dump_cb_t *cb, void *data) 3130 { 3131 struct ksz_device *dev = ds->priv; 3132 3133 if (!dev->dev_ops->fdb_dump) 3134 return -EOPNOTSUPP; 3135 3136 return dev->dev_ops->fdb_dump(dev, port, cb, data); 3137 } 3138 3139 static int ksz_port_mdb_add(struct dsa_switch *ds, int port, 3140 const struct switchdev_obj_port_mdb *mdb, 3141 struct dsa_db db) 3142 { 3143 struct ksz_device *dev = ds->priv; 3144 3145 if (!dev->dev_ops->mdb_add) 3146 return -EOPNOTSUPP; 3147 3148 return dev->dev_ops->mdb_add(dev, port, mdb, db); 3149 } 3150 3151 static int ksz_port_mdb_del(struct dsa_switch *ds, int port, 3152 const struct switchdev_obj_port_mdb *mdb, 3153 struct dsa_db db) 3154 { 3155 struct ksz_device *dev = ds->priv; 3156 3157 if (!dev->dev_ops->mdb_del) 3158 return -EOPNOTSUPP; 3159 3160 return dev->dev_ops->mdb_del(dev, port, mdb, db); 3161 } 3162 3163 static int ksz9477_set_default_prio_queue_mapping(struct ksz_device *dev, 3164 int port) 3165 { 3166 u32 queue_map = 0; 3167 int ipm; 3168 3169 for (ipm = 0; ipm < dev->info->num_ipms; ipm++) { 3170 int queue; 3171 3172 /* Traffic Type (TT) is corresponding to the Internal Priority 3173 * Map (IPM) in the switch. Traffic Class (TC) is 3174 * corresponding to the queue in the switch. 3175 */ 3176 queue = ieee8021q_tt_to_tc(ipm, dev->info->num_tx_queues); 3177 if (queue < 0) 3178 return queue; 3179 3180 queue_map |= queue << (ipm * KSZ9477_PORT_TC_MAP_S); 3181 } 3182 3183 return ksz_pwrite32(dev, port, KSZ9477_PORT_MRI_TC_MAP__4, queue_map); 3184 } 3185 3186 static int ksz_port_setup(struct dsa_switch *ds, int port) 3187 { 3188 struct ksz_device *dev = ds->priv; 3189 int ret; 3190 3191 if (!dsa_is_user_port(ds, port)) 3192 return 0; 3193 3194 /* setup user port */ 3195 dev->dev_ops->port_setup(dev, port, false); 3196 3197 if (!is_ksz8(dev)) { 3198 ret = ksz9477_set_default_prio_queue_mapping(dev, port); 3199 if (ret) 3200 return ret; 3201 } 3202 3203 /* port_stp_state_set() will be called after to enable the port so 3204 * there is no need to do anything. 3205 */ 3206 3207 return ksz_dcb_init_port(dev, port); 3208 } 3209 3210 void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state) 3211 { 3212 struct ksz_device *dev = ds->priv; 3213 struct ksz_port *p; 3214 const u16 *regs; 3215 u8 data; 3216 3217 regs = dev->info->regs; 3218 3219 ksz_pread8(dev, port, regs[P_STP_CTRL], &data); 3220 data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE); 3221 3222 p = &dev->ports[port]; 3223 3224 switch (state) { 3225 case BR_STATE_DISABLED: 3226 data |= PORT_LEARN_DISABLE; 3227 break; 3228 case BR_STATE_LISTENING: 3229 data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE); 3230 break; 3231 case BR_STATE_LEARNING: 3232 data |= PORT_RX_ENABLE; 3233 if (!p->learning) 3234 data |= PORT_LEARN_DISABLE; 3235 break; 3236 case BR_STATE_FORWARDING: 3237 data |= (PORT_TX_ENABLE | PORT_RX_ENABLE); 3238 if (!p->learning) 3239 data |= PORT_LEARN_DISABLE; 3240 break; 3241 case BR_STATE_BLOCKING: 3242 data |= PORT_LEARN_DISABLE; 3243 break; 3244 default: 3245 dev_err(ds->dev, "invalid STP state: %d\n", state); 3246 return; 3247 } 3248 3249 ksz_pwrite8(dev, port, regs[P_STP_CTRL], data); 3250 3251 p->stp_state = state; 3252 3253 ksz_update_port_member(dev, port); 3254 } 3255 3256 static void ksz_port_teardown(struct dsa_switch *ds, int port) 3257 { 3258 struct ksz_device *dev = ds->priv; 3259 3260 switch (dev->chip_id) { 3261 case KSZ8563_CHIP_ID: 3262 case KSZ8567_CHIP_ID: 3263 case KSZ9477_CHIP_ID: 3264 case KSZ9563_CHIP_ID: 3265 case KSZ9567_CHIP_ID: 3266 case KSZ9893_CHIP_ID: 3267 case KSZ9896_CHIP_ID: 3268 case KSZ9897_CHIP_ID: 3269 case LAN9646_CHIP_ID: 3270 if (dsa_is_user_port(ds, port)) 3271 ksz9477_port_acl_free(dev, port); 3272 } 3273 } 3274 3275 static int ksz_port_pre_bridge_flags(struct dsa_switch *ds, int port, 3276 struct switchdev_brport_flags flags, 3277 struct netlink_ext_ack *extack) 3278 { 3279 if (flags.mask & ~(BR_LEARNING | BR_ISOLATED)) 3280 return -EINVAL; 3281 3282 return 0; 3283 } 3284 3285 static int ksz_port_bridge_flags(struct dsa_switch *ds, int port, 3286 struct switchdev_brport_flags flags, 3287 struct netlink_ext_ack *extack) 3288 { 3289 struct ksz_device *dev = ds->priv; 3290 struct ksz_port *p = &dev->ports[port]; 3291 3292 if (flags.mask & (BR_LEARNING | BR_ISOLATED)) { 3293 if (flags.mask & BR_LEARNING) 3294 p->learning = !!(flags.val & BR_LEARNING); 3295 3296 if (flags.mask & BR_ISOLATED) 3297 p->isolated = !!(flags.val & BR_ISOLATED); 3298 3299 /* Make the change take effect immediately */ 3300 ksz_port_stp_state_set(ds, port, p->stp_state); 3301 } 3302 3303 return 0; 3304 } 3305 3306 static enum dsa_tag_protocol ksz_get_tag_protocol(struct dsa_switch *ds, 3307 int port, 3308 enum dsa_tag_protocol mp) 3309 { 3310 struct ksz_device *dev = ds->priv; 3311 enum dsa_tag_protocol proto = DSA_TAG_PROTO_NONE; 3312 3313 if (ksz_is_ksz87xx(dev) || ksz_is_8895_family(dev)) 3314 proto = DSA_TAG_PROTO_KSZ8795; 3315 3316 if (dev->chip_id == KSZ88X3_CHIP_ID || 3317 dev->chip_id == KSZ8563_CHIP_ID || 3318 dev->chip_id == KSZ9893_CHIP_ID || 3319 dev->chip_id == KSZ9563_CHIP_ID) 3320 proto = DSA_TAG_PROTO_KSZ9893; 3321 3322 if (dev->chip_id == KSZ8567_CHIP_ID || 3323 dev->chip_id == KSZ9477_CHIP_ID || 3324 dev->chip_id == KSZ9896_CHIP_ID || 3325 dev->chip_id == KSZ9897_CHIP_ID || 3326 dev->chip_id == KSZ9567_CHIP_ID || 3327 dev->chip_id == LAN9646_CHIP_ID) 3328 proto = DSA_TAG_PROTO_KSZ9477; 3329 3330 if (is_lan937x(dev)) 3331 proto = DSA_TAG_PROTO_LAN937X; 3332 3333 return proto; 3334 } 3335 3336 static int ksz_connect_tag_protocol(struct dsa_switch *ds, 3337 enum dsa_tag_protocol proto) 3338 { 3339 struct ksz_tagger_data *tagger_data; 3340 3341 switch (proto) { 3342 case DSA_TAG_PROTO_KSZ8795: 3343 return 0; 3344 case DSA_TAG_PROTO_KSZ9893: 3345 case DSA_TAG_PROTO_KSZ9477: 3346 case DSA_TAG_PROTO_LAN937X: 3347 tagger_data = ksz_tagger_data(ds); 3348 tagger_data->xmit_work_fn = ksz_port_deferred_xmit; 3349 return 0; 3350 default: 3351 return -EPROTONOSUPPORT; 3352 } 3353 } 3354 3355 static int ksz_port_vlan_filtering(struct dsa_switch *ds, int port, 3356 bool flag, struct netlink_ext_ack *extack) 3357 { 3358 struct ksz_device *dev = ds->priv; 3359 3360 if (!dev->dev_ops->vlan_filtering) 3361 return -EOPNOTSUPP; 3362 3363 return dev->dev_ops->vlan_filtering(dev, port, flag, extack); 3364 } 3365 3366 static int ksz_port_vlan_add(struct dsa_switch *ds, int port, 3367 const struct switchdev_obj_port_vlan *vlan, 3368 struct netlink_ext_ack *extack) 3369 { 3370 struct ksz_device *dev = ds->priv; 3371 3372 if (!dev->dev_ops->vlan_add) 3373 return -EOPNOTSUPP; 3374 3375 return dev->dev_ops->vlan_add(dev, port, vlan, extack); 3376 } 3377 3378 static int ksz_port_vlan_del(struct dsa_switch *ds, int port, 3379 const struct switchdev_obj_port_vlan *vlan) 3380 { 3381 struct ksz_device *dev = ds->priv; 3382 3383 if (!dev->dev_ops->vlan_del) 3384 return -EOPNOTSUPP; 3385 3386 return dev->dev_ops->vlan_del(dev, port, vlan); 3387 } 3388 3389 static int ksz_port_mirror_add(struct dsa_switch *ds, int port, 3390 struct dsa_mall_mirror_tc_entry *mirror, 3391 bool ingress, struct netlink_ext_ack *extack) 3392 { 3393 struct ksz_device *dev = ds->priv; 3394 3395 if (!dev->dev_ops->mirror_add) 3396 return -EOPNOTSUPP; 3397 3398 return dev->dev_ops->mirror_add(dev, port, mirror, ingress, extack); 3399 } 3400 3401 static void ksz_port_mirror_del(struct dsa_switch *ds, int port, 3402 struct dsa_mall_mirror_tc_entry *mirror) 3403 { 3404 struct ksz_device *dev = ds->priv; 3405 3406 if (dev->dev_ops->mirror_del) 3407 dev->dev_ops->mirror_del(dev, port, mirror); 3408 } 3409 3410 static int ksz_change_mtu(struct dsa_switch *ds, int port, int mtu) 3411 { 3412 struct ksz_device *dev = ds->priv; 3413 3414 if (!dev->dev_ops->change_mtu) 3415 return -EOPNOTSUPP; 3416 3417 return dev->dev_ops->change_mtu(dev, port, mtu); 3418 } 3419 3420 static int ksz_max_mtu(struct dsa_switch *ds, int port) 3421 { 3422 struct ksz_device *dev = ds->priv; 3423 3424 switch (dev->chip_id) { 3425 case KSZ8795_CHIP_ID: 3426 case KSZ8794_CHIP_ID: 3427 case KSZ8765_CHIP_ID: 3428 return KSZ8795_HUGE_PACKET_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN; 3429 case KSZ88X3_CHIP_ID: 3430 case KSZ8864_CHIP_ID: 3431 case KSZ8895_CHIP_ID: 3432 return KSZ8863_HUGE_PACKET_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN; 3433 case KSZ8563_CHIP_ID: 3434 case KSZ8567_CHIP_ID: 3435 case KSZ9477_CHIP_ID: 3436 case KSZ9563_CHIP_ID: 3437 case KSZ9567_CHIP_ID: 3438 case KSZ9893_CHIP_ID: 3439 case KSZ9896_CHIP_ID: 3440 case KSZ9897_CHIP_ID: 3441 case LAN9370_CHIP_ID: 3442 case LAN9371_CHIP_ID: 3443 case LAN9372_CHIP_ID: 3444 case LAN9373_CHIP_ID: 3445 case LAN9374_CHIP_ID: 3446 case LAN9646_CHIP_ID: 3447 return KSZ9477_MAX_FRAME_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN; 3448 } 3449 3450 return -EOPNOTSUPP; 3451 } 3452 3453 static int ksz_validate_eee(struct dsa_switch *ds, int port) 3454 { 3455 struct ksz_device *dev = ds->priv; 3456 3457 if (!dev->info->internal_phy[port]) 3458 return -EOPNOTSUPP; 3459 3460 switch (dev->chip_id) { 3461 case KSZ8563_CHIP_ID: 3462 case KSZ8567_CHIP_ID: 3463 case KSZ9477_CHIP_ID: 3464 case KSZ9563_CHIP_ID: 3465 case KSZ9567_CHIP_ID: 3466 case KSZ9893_CHIP_ID: 3467 case KSZ9896_CHIP_ID: 3468 case KSZ9897_CHIP_ID: 3469 case LAN9646_CHIP_ID: 3470 return 0; 3471 } 3472 3473 return -EOPNOTSUPP; 3474 } 3475 3476 static int ksz_get_mac_eee(struct dsa_switch *ds, int port, 3477 struct ethtool_keee *e) 3478 { 3479 int ret; 3480 3481 ret = ksz_validate_eee(ds, port); 3482 if (ret) 3483 return ret; 3484 3485 /* There is no documented control of Tx LPI configuration. */ 3486 e->tx_lpi_enabled = true; 3487 3488 /* There is no documented control of Tx LPI timer. According to tests 3489 * Tx LPI timer seems to be set by default to minimal value. 3490 */ 3491 e->tx_lpi_timer = 0; 3492 3493 return 0; 3494 } 3495 3496 static int ksz_set_mac_eee(struct dsa_switch *ds, int port, 3497 struct ethtool_keee *e) 3498 { 3499 struct ksz_device *dev = ds->priv; 3500 int ret; 3501 3502 ret = ksz_validate_eee(ds, port); 3503 if (ret) 3504 return ret; 3505 3506 if (!e->tx_lpi_enabled) { 3507 dev_err(dev->dev, "Disabling EEE Tx LPI is not supported\n"); 3508 return -EINVAL; 3509 } 3510 3511 if (e->tx_lpi_timer) { 3512 dev_err(dev->dev, "Setting EEE Tx LPI timer is not supported\n"); 3513 return -EINVAL; 3514 } 3515 3516 return 0; 3517 } 3518 3519 static void ksz_set_xmii(struct ksz_device *dev, int port, 3520 phy_interface_t interface) 3521 { 3522 const u8 *bitval = dev->info->xmii_ctrl1; 3523 struct ksz_port *p = &dev->ports[port]; 3524 const u16 *regs = dev->info->regs; 3525 u8 data8; 3526 3527 ksz_pread8(dev, port, regs[P_XMII_CTRL_1], &data8); 3528 3529 data8 &= ~(P_MII_SEL_M | P_RGMII_ID_IG_ENABLE | 3530 P_RGMII_ID_EG_ENABLE); 3531 3532 switch (interface) { 3533 case PHY_INTERFACE_MODE_MII: 3534 data8 |= bitval[P_MII_SEL]; 3535 break; 3536 case PHY_INTERFACE_MODE_RMII: 3537 data8 |= bitval[P_RMII_SEL]; 3538 break; 3539 case PHY_INTERFACE_MODE_GMII: 3540 data8 |= bitval[P_GMII_SEL]; 3541 break; 3542 case PHY_INTERFACE_MODE_RGMII: 3543 case PHY_INTERFACE_MODE_RGMII_ID: 3544 case PHY_INTERFACE_MODE_RGMII_TXID: 3545 case PHY_INTERFACE_MODE_RGMII_RXID: 3546 data8 |= bitval[P_RGMII_SEL]; 3547 /* On KSZ9893, disable RGMII in-band status support */ 3548 if (dev->chip_id == KSZ9893_CHIP_ID || 3549 dev->chip_id == KSZ8563_CHIP_ID || 3550 dev->chip_id == KSZ9563_CHIP_ID || 3551 is_lan937x(dev)) 3552 data8 &= ~P_MII_MAC_MODE; 3553 break; 3554 default: 3555 dev_err(dev->dev, "Unsupported interface '%s' for port %d\n", 3556 phy_modes(interface), port); 3557 return; 3558 } 3559 3560 if (p->rgmii_tx_val) 3561 data8 |= P_RGMII_ID_EG_ENABLE; 3562 3563 if (p->rgmii_rx_val) 3564 data8 |= P_RGMII_ID_IG_ENABLE; 3565 3566 /* Write the updated value */ 3567 ksz_pwrite8(dev, port, regs[P_XMII_CTRL_1], data8); 3568 } 3569 3570 phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit) 3571 { 3572 const u8 *bitval = dev->info->xmii_ctrl1; 3573 const u16 *regs = dev->info->regs; 3574 phy_interface_t interface; 3575 u8 data8; 3576 u8 val; 3577 3578 ksz_pread8(dev, port, regs[P_XMII_CTRL_1], &data8); 3579 3580 val = FIELD_GET(P_MII_SEL_M, data8); 3581 3582 if (val == bitval[P_MII_SEL]) { 3583 if (gbit) 3584 interface = PHY_INTERFACE_MODE_GMII; 3585 else 3586 interface = PHY_INTERFACE_MODE_MII; 3587 } else if (val == bitval[P_RMII_SEL]) { 3588 interface = PHY_INTERFACE_MODE_RMII; 3589 } else { 3590 interface = PHY_INTERFACE_MODE_RGMII; 3591 if (data8 & P_RGMII_ID_EG_ENABLE) 3592 interface = PHY_INTERFACE_MODE_RGMII_TXID; 3593 if (data8 & P_RGMII_ID_IG_ENABLE) { 3594 interface = PHY_INTERFACE_MODE_RGMII_RXID; 3595 if (data8 & P_RGMII_ID_EG_ENABLE) 3596 interface = PHY_INTERFACE_MODE_RGMII_ID; 3597 } 3598 } 3599 3600 return interface; 3601 } 3602 3603 static void ksz88x3_phylink_mac_config(struct phylink_config *config, 3604 unsigned int mode, 3605 const struct phylink_link_state *state) 3606 { 3607 struct dsa_port *dp = dsa_phylink_to_port(config); 3608 struct ksz_device *dev = dp->ds->priv; 3609 3610 dev->ports[dp->index].manual_flow = !(state->pause & MLO_PAUSE_AN); 3611 } 3612 3613 static void ksz_phylink_mac_config(struct phylink_config *config, 3614 unsigned int mode, 3615 const struct phylink_link_state *state) 3616 { 3617 struct dsa_port *dp = dsa_phylink_to_port(config); 3618 struct ksz_device *dev = dp->ds->priv; 3619 int port = dp->index; 3620 3621 /* Internal PHYs */ 3622 if (dev->info->internal_phy[port]) 3623 return; 3624 3625 if (phylink_autoneg_inband(mode)) { 3626 dev_err(dev->dev, "In-band AN not supported!\n"); 3627 return; 3628 } 3629 3630 ksz_set_xmii(dev, port, state->interface); 3631 3632 if (dev->dev_ops->setup_rgmii_delay) 3633 dev->dev_ops->setup_rgmii_delay(dev, port); 3634 } 3635 3636 bool ksz_get_gbit(struct ksz_device *dev, int port) 3637 { 3638 const u8 *bitval = dev->info->xmii_ctrl1; 3639 const u16 *regs = dev->info->regs; 3640 bool gbit = false; 3641 u8 data8; 3642 bool val; 3643 3644 ksz_pread8(dev, port, regs[P_XMII_CTRL_1], &data8); 3645 3646 val = FIELD_GET(P_GMII_1GBIT_M, data8); 3647 3648 if (val == bitval[P_GMII_1GBIT]) 3649 gbit = true; 3650 3651 return gbit; 3652 } 3653 3654 static void ksz_set_gbit(struct ksz_device *dev, int port, bool gbit) 3655 { 3656 const u8 *bitval = dev->info->xmii_ctrl1; 3657 const u16 *regs = dev->info->regs; 3658 u8 data8; 3659 3660 ksz_pread8(dev, port, regs[P_XMII_CTRL_1], &data8); 3661 3662 data8 &= ~P_GMII_1GBIT_M; 3663 3664 if (gbit) 3665 data8 |= FIELD_PREP(P_GMII_1GBIT_M, bitval[P_GMII_1GBIT]); 3666 else 3667 data8 |= FIELD_PREP(P_GMII_1GBIT_M, bitval[P_GMII_NOT_1GBIT]); 3668 3669 /* Write the updated value */ 3670 ksz_pwrite8(dev, port, regs[P_XMII_CTRL_1], data8); 3671 } 3672 3673 static void ksz_set_100_10mbit(struct ksz_device *dev, int port, int speed) 3674 { 3675 const u8 *bitval = dev->info->xmii_ctrl0; 3676 const u16 *regs = dev->info->regs; 3677 u8 data8; 3678 3679 ksz_pread8(dev, port, regs[P_XMII_CTRL_0], &data8); 3680 3681 data8 &= ~P_MII_100MBIT_M; 3682 3683 if (speed == SPEED_100) 3684 data8 |= FIELD_PREP(P_MII_100MBIT_M, bitval[P_MII_100MBIT]); 3685 else 3686 data8 |= FIELD_PREP(P_MII_100MBIT_M, bitval[P_MII_10MBIT]); 3687 3688 /* Write the updated value */ 3689 ksz_pwrite8(dev, port, regs[P_XMII_CTRL_0], data8); 3690 } 3691 3692 static void ksz_port_set_xmii_speed(struct ksz_device *dev, int port, int speed) 3693 { 3694 if (speed == SPEED_1000) 3695 ksz_set_gbit(dev, port, true); 3696 else 3697 ksz_set_gbit(dev, port, false); 3698 3699 if (speed == SPEED_100 || speed == SPEED_10) 3700 ksz_set_100_10mbit(dev, port, speed); 3701 } 3702 3703 static void ksz_duplex_flowctrl(struct ksz_device *dev, int port, int duplex, 3704 bool tx_pause, bool rx_pause) 3705 { 3706 const u8 *bitval = dev->info->xmii_ctrl0; 3707 const u32 *masks = dev->info->masks; 3708 const u16 *regs = dev->info->regs; 3709 u8 mask; 3710 u8 val; 3711 3712 mask = P_MII_DUPLEX_M | masks[P_MII_TX_FLOW_CTRL] | 3713 masks[P_MII_RX_FLOW_CTRL]; 3714 3715 if (duplex == DUPLEX_FULL) 3716 val = FIELD_PREP(P_MII_DUPLEX_M, bitval[P_MII_FULL_DUPLEX]); 3717 else 3718 val = FIELD_PREP(P_MII_DUPLEX_M, bitval[P_MII_HALF_DUPLEX]); 3719 3720 if (tx_pause) 3721 val |= masks[P_MII_TX_FLOW_CTRL]; 3722 3723 if (rx_pause) 3724 val |= masks[P_MII_RX_FLOW_CTRL]; 3725 3726 ksz_prmw8(dev, port, regs[P_XMII_CTRL_0], mask, val); 3727 } 3728 3729 static void ksz9477_phylink_mac_link_up(struct phylink_config *config, 3730 struct phy_device *phydev, 3731 unsigned int mode, 3732 phy_interface_t interface, 3733 int speed, int duplex, bool tx_pause, 3734 bool rx_pause) 3735 { 3736 struct dsa_port *dp = dsa_phylink_to_port(config); 3737 struct ksz_device *dev = dp->ds->priv; 3738 int port = dp->index; 3739 struct ksz_port *p; 3740 3741 p = &dev->ports[port]; 3742 3743 /* Internal PHYs */ 3744 if (dev->info->internal_phy[port]) 3745 return; 3746 3747 p->phydev.speed = speed; 3748 3749 ksz_port_set_xmii_speed(dev, port, speed); 3750 3751 ksz_duplex_flowctrl(dev, port, duplex, tx_pause, rx_pause); 3752 } 3753 3754 static int ksz_switch_detect(struct ksz_device *dev) 3755 { 3756 u8 id1, id2, id4; 3757 u16 id16; 3758 u32 id32; 3759 int ret; 3760 3761 /* read chip id */ 3762 ret = ksz_read16(dev, REG_CHIP_ID0, &id16); 3763 if (ret) 3764 return ret; 3765 3766 id1 = FIELD_GET(SW_FAMILY_ID_M, id16); 3767 id2 = FIELD_GET(SW_CHIP_ID_M, id16); 3768 3769 switch (id1) { 3770 case KSZ87_FAMILY_ID: 3771 if (id2 == KSZ87_CHIP_ID_95) { 3772 u8 val; 3773 3774 dev->chip_id = KSZ8795_CHIP_ID; 3775 3776 ksz_read8(dev, KSZ8_PORT_STATUS_0, &val); 3777 if (val & KSZ8_PORT_FIBER_MODE) 3778 dev->chip_id = KSZ8765_CHIP_ID; 3779 } else if (id2 == KSZ87_CHIP_ID_94) { 3780 dev->chip_id = KSZ8794_CHIP_ID; 3781 } else { 3782 return -ENODEV; 3783 } 3784 break; 3785 case KSZ88_FAMILY_ID: 3786 if (id2 == KSZ88_CHIP_ID_63) 3787 dev->chip_id = KSZ88X3_CHIP_ID; 3788 else 3789 return -ENODEV; 3790 break; 3791 case KSZ8895_FAMILY_ID: 3792 if (id2 == KSZ8895_CHIP_ID_95 || 3793 id2 == KSZ8895_CHIP_ID_95R) 3794 dev->chip_id = KSZ8895_CHIP_ID; 3795 else 3796 return -ENODEV; 3797 ret = ksz_read8(dev, REG_KSZ8864_CHIP_ID, &id4); 3798 if (ret) 3799 return ret; 3800 if (id4 & SW_KSZ8864) 3801 dev->chip_id = KSZ8864_CHIP_ID; 3802 break; 3803 default: 3804 ret = ksz_read32(dev, REG_CHIP_ID0, &id32); 3805 if (ret) 3806 return ret; 3807 3808 dev->chip_rev = FIELD_GET(SW_REV_ID_M, id32); 3809 id32 &= ~0xFF; 3810 3811 switch (id32) { 3812 case KSZ9477_CHIP_ID: 3813 case KSZ9896_CHIP_ID: 3814 case KSZ9897_CHIP_ID: 3815 case KSZ9567_CHIP_ID: 3816 case KSZ8567_CHIP_ID: 3817 case LAN9370_CHIP_ID: 3818 case LAN9371_CHIP_ID: 3819 case LAN9372_CHIP_ID: 3820 case LAN9373_CHIP_ID: 3821 case LAN9374_CHIP_ID: 3822 3823 /* LAN9646 does not have its own chip id. */ 3824 if (dev->chip_id != LAN9646_CHIP_ID) 3825 dev->chip_id = id32; 3826 break; 3827 case KSZ9893_CHIP_ID: 3828 ret = ksz_read8(dev, REG_CHIP_ID4, 3829 &id4); 3830 if (ret) 3831 return ret; 3832 3833 if (id4 == SKU_ID_KSZ8563) 3834 dev->chip_id = KSZ8563_CHIP_ID; 3835 else if (id4 == SKU_ID_KSZ9563) 3836 dev->chip_id = KSZ9563_CHIP_ID; 3837 else 3838 dev->chip_id = KSZ9893_CHIP_ID; 3839 3840 break; 3841 default: 3842 dev_err(dev->dev, 3843 "unsupported switch detected %x)\n", id32); 3844 return -ENODEV; 3845 } 3846 } 3847 return 0; 3848 } 3849 3850 static int ksz_cls_flower_add(struct dsa_switch *ds, int port, 3851 struct flow_cls_offload *cls, bool ingress) 3852 { 3853 struct ksz_device *dev = ds->priv; 3854 3855 switch (dev->chip_id) { 3856 case KSZ8563_CHIP_ID: 3857 case KSZ8567_CHIP_ID: 3858 case KSZ9477_CHIP_ID: 3859 case KSZ9563_CHIP_ID: 3860 case KSZ9567_CHIP_ID: 3861 case KSZ9893_CHIP_ID: 3862 case KSZ9896_CHIP_ID: 3863 case KSZ9897_CHIP_ID: 3864 case LAN9646_CHIP_ID: 3865 return ksz9477_cls_flower_add(ds, port, cls, ingress); 3866 } 3867 3868 return -EOPNOTSUPP; 3869 } 3870 3871 static int ksz_cls_flower_del(struct dsa_switch *ds, int port, 3872 struct flow_cls_offload *cls, bool ingress) 3873 { 3874 struct ksz_device *dev = ds->priv; 3875 3876 switch (dev->chip_id) { 3877 case KSZ8563_CHIP_ID: 3878 case KSZ8567_CHIP_ID: 3879 case KSZ9477_CHIP_ID: 3880 case KSZ9563_CHIP_ID: 3881 case KSZ9567_CHIP_ID: 3882 case KSZ9893_CHIP_ID: 3883 case KSZ9896_CHIP_ID: 3884 case KSZ9897_CHIP_ID: 3885 case LAN9646_CHIP_ID: 3886 return ksz9477_cls_flower_del(ds, port, cls, ingress); 3887 } 3888 3889 return -EOPNOTSUPP; 3890 } 3891 3892 /* Bandwidth is calculated by idle slope/transmission speed. Then the Bandwidth 3893 * is converted to Hex-decimal using the successive multiplication method. On 3894 * every step, integer part is taken and decimal part is carry forwarded. 3895 */ 3896 static int cinc_cal(s32 idle_slope, s32 send_slope, u32 *bw) 3897 { 3898 u32 cinc = 0; 3899 u32 txrate; 3900 u32 rate; 3901 u8 temp; 3902 u8 i; 3903 3904 txrate = idle_slope - send_slope; 3905 3906 if (!txrate) 3907 return -EINVAL; 3908 3909 rate = idle_slope; 3910 3911 /* 24 bit register */ 3912 for (i = 0; i < 6; i++) { 3913 rate = rate * 16; 3914 3915 temp = rate / txrate; 3916 3917 rate %= txrate; 3918 3919 cinc = ((cinc << 4) | temp); 3920 } 3921 3922 *bw = cinc; 3923 3924 return 0; 3925 } 3926 3927 static int ksz_setup_tc_mode(struct ksz_device *dev, int port, u8 scheduler, 3928 u8 shaper) 3929 { 3930 return ksz_pwrite8(dev, port, REG_PORT_MTI_QUEUE_CTRL_0, 3931 FIELD_PREP(MTI_SCHEDULE_MODE_M, scheduler) | 3932 FIELD_PREP(MTI_SHAPING_M, shaper)); 3933 } 3934 3935 static int ksz_setup_tc_cbs(struct dsa_switch *ds, int port, 3936 struct tc_cbs_qopt_offload *qopt) 3937 { 3938 struct ksz_device *dev = ds->priv; 3939 int ret; 3940 u32 bw; 3941 3942 if (!dev->info->tc_cbs_supported) 3943 return -EOPNOTSUPP; 3944 3945 if (qopt->queue > dev->info->num_tx_queues) 3946 return -EINVAL; 3947 3948 /* Queue Selection */ 3949 ret = ksz_pwrite32(dev, port, REG_PORT_MTI_QUEUE_INDEX__4, qopt->queue); 3950 if (ret) 3951 return ret; 3952 3953 if (!qopt->enable) 3954 return ksz_setup_tc_mode(dev, port, MTI_SCHEDULE_WRR, 3955 MTI_SHAPING_OFF); 3956 3957 /* High Credit */ 3958 ret = ksz_pwrite16(dev, port, REG_PORT_MTI_HI_WATER_MARK, 3959 qopt->hicredit); 3960 if (ret) 3961 return ret; 3962 3963 /* Low Credit */ 3964 ret = ksz_pwrite16(dev, port, REG_PORT_MTI_LO_WATER_MARK, 3965 qopt->locredit); 3966 if (ret) 3967 return ret; 3968 3969 /* Credit Increment Register */ 3970 ret = cinc_cal(qopt->idleslope, qopt->sendslope, &bw); 3971 if (ret) 3972 return ret; 3973 3974 if (dev->dev_ops->tc_cbs_set_cinc) { 3975 ret = dev->dev_ops->tc_cbs_set_cinc(dev, port, bw); 3976 if (ret) 3977 return ret; 3978 } 3979 3980 return ksz_setup_tc_mode(dev, port, MTI_SCHEDULE_STRICT_PRIO, 3981 MTI_SHAPING_SRP); 3982 } 3983 3984 static int ksz_disable_egress_rate_limit(struct ksz_device *dev, int port) 3985 { 3986 int queue, ret; 3987 3988 /* Configuration will not take effect until the last Port Queue X 3989 * Egress Limit Control Register is written. 3990 */ 3991 for (queue = 0; queue < dev->info->num_tx_queues; queue++) { 3992 ret = ksz_pwrite8(dev, port, KSZ9477_REG_PORT_OUT_RATE_0 + queue, 3993 KSZ9477_OUT_RATE_NO_LIMIT); 3994 if (ret) 3995 return ret; 3996 } 3997 3998 return 0; 3999 } 4000 4001 static int ksz_ets_band_to_queue(struct tc_ets_qopt_offload_replace_params *p, 4002 int band) 4003 { 4004 /* Compared to queues, bands prioritize packets differently. In strict 4005 * priority mode, the lowest priority is assigned to Queue 0 while the 4006 * highest priority is given to Band 0. 4007 */ 4008 return p->bands - 1 - band; 4009 } 4010 4011 static int ksz_queue_set_strict(struct ksz_device *dev, int port, int queue) 4012 { 4013 int ret; 4014 4015 ret = ksz_pwrite32(dev, port, REG_PORT_MTI_QUEUE_INDEX__4, queue); 4016 if (ret) 4017 return ret; 4018 4019 return ksz_setup_tc_mode(dev, port, MTI_SCHEDULE_STRICT_PRIO, 4020 MTI_SHAPING_OFF); 4021 } 4022 4023 static int ksz_queue_set_wrr(struct ksz_device *dev, int port, int queue, 4024 int weight) 4025 { 4026 int ret; 4027 4028 ret = ksz_pwrite32(dev, port, REG_PORT_MTI_QUEUE_INDEX__4, queue); 4029 if (ret) 4030 return ret; 4031 4032 ret = ksz_setup_tc_mode(dev, port, MTI_SCHEDULE_WRR, 4033 MTI_SHAPING_OFF); 4034 if (ret) 4035 return ret; 4036 4037 return ksz_pwrite8(dev, port, KSZ9477_PORT_MTI_QUEUE_CTRL_1, weight); 4038 } 4039 4040 static int ksz_tc_ets_add(struct ksz_device *dev, int port, 4041 struct tc_ets_qopt_offload_replace_params *p) 4042 { 4043 int ret, band, tc_prio; 4044 u32 queue_map = 0; 4045 4046 /* In order to ensure proper prioritization, it is necessary to set the 4047 * rate limit for the related queue to zero. Otherwise strict priority 4048 * or WRR mode will not work. This is a hardware limitation. 4049 */ 4050 ret = ksz_disable_egress_rate_limit(dev, port); 4051 if (ret) 4052 return ret; 4053 4054 /* Configure queue scheduling mode for all bands. Currently only strict 4055 * prio mode is supported. 4056 */ 4057 for (band = 0; band < p->bands; band++) { 4058 int queue = ksz_ets_band_to_queue(p, band); 4059 4060 ret = ksz_queue_set_strict(dev, port, queue); 4061 if (ret) 4062 return ret; 4063 } 4064 4065 /* Configure the mapping between traffic classes and queues. Note: 4066 * priomap variable support 16 traffic classes, but the chip can handle 4067 * only 8 classes. 4068 */ 4069 for (tc_prio = 0; tc_prio < ARRAY_SIZE(p->priomap); tc_prio++) { 4070 int queue; 4071 4072 if (tc_prio >= dev->info->num_ipms) 4073 break; 4074 4075 queue = ksz_ets_band_to_queue(p, p->priomap[tc_prio]); 4076 queue_map |= queue << (tc_prio * KSZ9477_PORT_TC_MAP_S); 4077 } 4078 4079 return ksz_pwrite32(dev, port, KSZ9477_PORT_MRI_TC_MAP__4, queue_map); 4080 } 4081 4082 static int ksz_tc_ets_del(struct ksz_device *dev, int port) 4083 { 4084 int ret, queue; 4085 4086 /* To restore the default chip configuration, set all queues to use the 4087 * WRR scheduler with a weight of 1. 4088 */ 4089 for (queue = 0; queue < dev->info->num_tx_queues; queue++) { 4090 ret = ksz_queue_set_wrr(dev, port, queue, 4091 KSZ9477_DEFAULT_WRR_WEIGHT); 4092 if (ret) 4093 return ret; 4094 } 4095 4096 /* Revert the queue mapping for TC-priority to its default setting on 4097 * the chip. 4098 */ 4099 return ksz9477_set_default_prio_queue_mapping(dev, port); 4100 } 4101 4102 static int ksz_tc_ets_validate(struct ksz_device *dev, int port, 4103 struct tc_ets_qopt_offload_replace_params *p) 4104 { 4105 int band; 4106 4107 /* Since it is not feasible to share one port among multiple qdisc, 4108 * the user must configure all available queues appropriately. 4109 */ 4110 if (p->bands != dev->info->num_tx_queues) { 4111 dev_err(dev->dev, "Not supported amount of bands. It should be %d\n", 4112 dev->info->num_tx_queues); 4113 return -EOPNOTSUPP; 4114 } 4115 4116 for (band = 0; band < p->bands; ++band) { 4117 /* The KSZ switches utilize a weighted round robin configuration 4118 * where a certain number of packets can be transmitted from a 4119 * queue before the next queue is serviced. For more information 4120 * on this, refer to section 5.2.8.4 of the KSZ8565R 4121 * documentation on the Port Transmit Queue Control 1 Register. 4122 * However, the current ETS Qdisc implementation (as of February 4123 * 2023) assigns a weight to each queue based on the number of 4124 * bytes or extrapolated bandwidth in percentages. Since this 4125 * differs from the KSZ switches' method and we don't want to 4126 * fake support by converting bytes to packets, it is better to 4127 * return an error instead. 4128 */ 4129 if (p->quanta[band]) { 4130 dev_err(dev->dev, "Quanta/weights configuration is not supported.\n"); 4131 return -EOPNOTSUPP; 4132 } 4133 } 4134 4135 return 0; 4136 } 4137 4138 static int ksz_tc_setup_qdisc_ets(struct dsa_switch *ds, int port, 4139 struct tc_ets_qopt_offload *qopt) 4140 { 4141 struct ksz_device *dev = ds->priv; 4142 int ret; 4143 4144 if (is_ksz8(dev)) 4145 return -EOPNOTSUPP; 4146 4147 if (qopt->parent != TC_H_ROOT) { 4148 dev_err(dev->dev, "Parent should be \"root\"\n"); 4149 return -EOPNOTSUPP; 4150 } 4151 4152 switch (qopt->command) { 4153 case TC_ETS_REPLACE: 4154 ret = ksz_tc_ets_validate(dev, port, &qopt->replace_params); 4155 if (ret) 4156 return ret; 4157 4158 return ksz_tc_ets_add(dev, port, &qopt->replace_params); 4159 case TC_ETS_DESTROY: 4160 return ksz_tc_ets_del(dev, port); 4161 case TC_ETS_STATS: 4162 case TC_ETS_GRAFT: 4163 return -EOPNOTSUPP; 4164 } 4165 4166 return -EOPNOTSUPP; 4167 } 4168 4169 static int ksz_setup_tc(struct dsa_switch *ds, int port, 4170 enum tc_setup_type type, void *type_data) 4171 { 4172 switch (type) { 4173 case TC_SETUP_QDISC_CBS: 4174 return ksz_setup_tc_cbs(ds, port, type_data); 4175 case TC_SETUP_QDISC_ETS: 4176 return ksz_tc_setup_qdisc_ets(ds, port, type_data); 4177 default: 4178 return -EOPNOTSUPP; 4179 } 4180 } 4181 4182 /** 4183 * ksz_handle_wake_reason - Handle wake reason on a specified port. 4184 * @dev: The device structure. 4185 * @port: The port number. 4186 * 4187 * This function reads the PME (Power Management Event) status register of a 4188 * specified port to determine the wake reason. If there is no wake event, it 4189 * returns early. Otherwise, it logs the wake reason which could be due to a 4190 * "Magic Packet", "Link Up", or "Energy Detect" event. The PME status register 4191 * is then cleared to acknowledge the handling of the wake event. 4192 * 4193 * Return: 0 on success, or an error code on failure. 4194 */ 4195 int ksz_handle_wake_reason(struct ksz_device *dev, int port) 4196 { 4197 const struct ksz_dev_ops *ops = dev->dev_ops; 4198 const u16 *regs = dev->info->regs; 4199 u8 pme_status; 4200 int ret; 4201 4202 ret = ops->pme_pread8(dev, port, regs[REG_PORT_PME_STATUS], 4203 &pme_status); 4204 if (ret) 4205 return ret; 4206 4207 if (!pme_status) 4208 return 0; 4209 4210 dev_dbg(dev->dev, "Wake event on port %d due to:%s%s%s\n", port, 4211 pme_status & PME_WOL_MAGICPKT ? " \"Magic Packet\"" : "", 4212 pme_status & PME_WOL_LINKUP ? " \"Link Up\"" : "", 4213 pme_status & PME_WOL_ENERGY ? " \"Energy detect\"" : ""); 4214 4215 return ops->pme_pwrite8(dev, port, regs[REG_PORT_PME_STATUS], 4216 pme_status); 4217 } 4218 4219 /** 4220 * ksz_get_wol - Get Wake-on-LAN settings for a specified port. 4221 * @ds: The dsa_switch structure. 4222 * @port: The port number. 4223 * @wol: Pointer to ethtool Wake-on-LAN settings structure. 4224 * 4225 * This function checks the device PME wakeup_source flag and chip_id. 4226 * If enabled and supported, it sets the supported and active WoL 4227 * flags. 4228 */ 4229 static void ksz_get_wol(struct dsa_switch *ds, int port, 4230 struct ethtool_wolinfo *wol) 4231 { 4232 struct ksz_device *dev = ds->priv; 4233 const u16 *regs = dev->info->regs; 4234 u8 pme_ctrl; 4235 int ret; 4236 4237 if (!is_ksz9477(dev) && !ksz_is_ksz87xx(dev)) 4238 return; 4239 4240 if (!dev->wakeup_source) 4241 return; 4242 4243 wol->supported = WAKE_PHY; 4244 4245 /* Check if the current MAC address on this port can be set 4246 * as global for WAKE_MAGIC support. The result may vary 4247 * dynamically based on other ports configurations. 4248 */ 4249 if (ksz_is_port_mac_global_usable(dev->ds, port)) 4250 wol->supported |= WAKE_MAGIC; 4251 4252 ret = dev->dev_ops->pme_pread8(dev, port, regs[REG_PORT_PME_CTRL], 4253 &pme_ctrl); 4254 if (ret) 4255 return; 4256 4257 if (pme_ctrl & PME_WOL_MAGICPKT) 4258 wol->wolopts |= WAKE_MAGIC; 4259 if (pme_ctrl & (PME_WOL_LINKUP | PME_WOL_ENERGY)) 4260 wol->wolopts |= WAKE_PHY; 4261 } 4262 4263 /** 4264 * ksz_set_wol - Set Wake-on-LAN settings for a specified port. 4265 * @ds: The dsa_switch structure. 4266 * @port: The port number. 4267 * @wol: Pointer to ethtool Wake-on-LAN settings structure. 4268 * 4269 * This function configures Wake-on-LAN (WoL) settings for a specified 4270 * port. It validates the provided WoL options, checks if PME is 4271 * enabled and supported, clears any previous wake reasons, and sets 4272 * the Magic Packet flag in the port's PME control register if 4273 * specified. 4274 * 4275 * Return: 0 on success, or other error codes on failure. 4276 */ 4277 static int ksz_set_wol(struct dsa_switch *ds, int port, 4278 struct ethtool_wolinfo *wol) 4279 { 4280 u8 pme_ctrl = 0, pme_ctrl_old = 0; 4281 struct ksz_device *dev = ds->priv; 4282 const u16 *regs = dev->info->regs; 4283 bool magic_switched_off; 4284 bool magic_switched_on; 4285 int ret; 4286 4287 if (wol->wolopts & ~(WAKE_PHY | WAKE_MAGIC)) 4288 return -EINVAL; 4289 4290 if (!is_ksz9477(dev) && !ksz_is_ksz87xx(dev)) 4291 return -EOPNOTSUPP; 4292 4293 if (!dev->wakeup_source) 4294 return -EOPNOTSUPP; 4295 4296 ret = ksz_handle_wake_reason(dev, port); 4297 if (ret) 4298 return ret; 4299 4300 if (wol->wolopts & WAKE_MAGIC) 4301 pme_ctrl |= PME_WOL_MAGICPKT; 4302 if (wol->wolopts & WAKE_PHY) 4303 pme_ctrl |= PME_WOL_LINKUP | PME_WOL_ENERGY; 4304 4305 ret = dev->dev_ops->pme_pread8(dev, port, regs[REG_PORT_PME_CTRL], 4306 &pme_ctrl_old); 4307 if (ret) 4308 return ret; 4309 4310 if (pme_ctrl_old == pme_ctrl) 4311 return 0; 4312 4313 magic_switched_off = (pme_ctrl_old & PME_WOL_MAGICPKT) && 4314 !(pme_ctrl & PME_WOL_MAGICPKT); 4315 magic_switched_on = !(pme_ctrl_old & PME_WOL_MAGICPKT) && 4316 (pme_ctrl & PME_WOL_MAGICPKT); 4317 4318 /* To keep reference count of MAC address, we should do this 4319 * operation only on change of WOL settings. 4320 */ 4321 if (magic_switched_on) { 4322 ret = ksz_switch_macaddr_get(dev->ds, port, NULL); 4323 if (ret) 4324 return ret; 4325 } else if (magic_switched_off) { 4326 ksz_switch_macaddr_put(dev->ds); 4327 } 4328 4329 ret = dev->dev_ops->pme_pwrite8(dev, port, regs[REG_PORT_PME_CTRL], 4330 pme_ctrl); 4331 if (ret) { 4332 if (magic_switched_on) 4333 ksz_switch_macaddr_put(dev->ds); 4334 return ret; 4335 } 4336 4337 return 0; 4338 } 4339 4340 /** 4341 * ksz_wol_pre_shutdown - Prepares the switch device for shutdown while 4342 * considering Wake-on-LAN (WoL) settings. 4343 * @dev: The switch device structure. 4344 * @wol_enabled: Pointer to a boolean which will be set to true if WoL is 4345 * enabled on any port. 4346 * 4347 * This function prepares the switch device for a safe shutdown while taking 4348 * into account the Wake-on-LAN (WoL) settings on the user ports. It updates 4349 * the wol_enabled flag accordingly to reflect whether WoL is active on any 4350 * port. 4351 */ 4352 static void ksz_wol_pre_shutdown(struct ksz_device *dev, bool *wol_enabled) 4353 { 4354 const struct ksz_dev_ops *ops = dev->dev_ops; 4355 const u16 *regs = dev->info->regs; 4356 u8 pme_pin_en = PME_ENABLE; 4357 struct dsa_port *dp; 4358 int ret; 4359 4360 *wol_enabled = false; 4361 4362 if (!is_ksz9477(dev) && !ksz_is_ksz87xx(dev)) 4363 return; 4364 4365 if (!dev->wakeup_source) 4366 return; 4367 4368 dsa_switch_for_each_user_port(dp, dev->ds) { 4369 u8 pme_ctrl = 0; 4370 4371 ret = ops->pme_pread8(dev, dp->index, 4372 regs[REG_PORT_PME_CTRL], &pme_ctrl); 4373 if (!ret && pme_ctrl) 4374 *wol_enabled = true; 4375 4376 /* make sure there are no pending wake events which would 4377 * prevent the device from going to sleep/shutdown. 4378 */ 4379 ksz_handle_wake_reason(dev, dp->index); 4380 } 4381 4382 /* Now we are save to enable PME pin. */ 4383 if (*wol_enabled) { 4384 if (dev->pme_active_high) 4385 pme_pin_en |= PME_POLARITY; 4386 ops->pme_write8(dev, regs[REG_SW_PME_CTRL], pme_pin_en); 4387 if (ksz_is_ksz87xx(dev)) 4388 ksz_write8(dev, KSZ87XX_REG_INT_EN, KSZ87XX_INT_PME_MASK); 4389 } 4390 } 4391 4392 static int ksz_port_set_mac_address(struct dsa_switch *ds, int port, 4393 const unsigned char *addr) 4394 { 4395 struct dsa_port *dp = dsa_to_port(ds, port); 4396 struct ethtool_wolinfo wol; 4397 4398 if (dp->hsr_dev) { 4399 dev_err(ds->dev, 4400 "Cannot change MAC address on port %d with active HSR offload\n", 4401 port); 4402 return -EBUSY; 4403 } 4404 4405 /* Need to initialize variable as the code to fill in settings may 4406 * not be executed. 4407 */ 4408 wol.wolopts = 0; 4409 4410 ksz_get_wol(ds, dp->index, &wol); 4411 if (wol.wolopts & WAKE_MAGIC) { 4412 dev_err(ds->dev, 4413 "Cannot change MAC address on port %d with active Wake on Magic Packet\n", 4414 port); 4415 return -EBUSY; 4416 } 4417 4418 return 0; 4419 } 4420 4421 /** 4422 * ksz_is_port_mac_global_usable - Check if the MAC address on a given port 4423 * can be used as a global address. 4424 * @ds: Pointer to the DSA switch structure. 4425 * @port: The port number on which the MAC address is to be checked. 4426 * 4427 * This function examines the MAC address set on the specified port and 4428 * determines if it can be used as a global address for the switch. 4429 * 4430 * Return: true if the port's MAC address can be used as a global address, false 4431 * otherwise. 4432 */ 4433 bool ksz_is_port_mac_global_usable(struct dsa_switch *ds, int port) 4434 { 4435 struct net_device *user = dsa_to_port(ds, port)->user; 4436 const unsigned char *addr = user->dev_addr; 4437 struct ksz_switch_macaddr *switch_macaddr; 4438 struct ksz_device *dev = ds->priv; 4439 4440 ASSERT_RTNL(); 4441 4442 switch_macaddr = dev->switch_macaddr; 4443 if (switch_macaddr && !ether_addr_equal(switch_macaddr->addr, addr)) 4444 return false; 4445 4446 return true; 4447 } 4448 4449 /** 4450 * ksz_switch_macaddr_get - Program the switch's MAC address register. 4451 * @ds: DSA switch instance. 4452 * @port: Port number. 4453 * @extack: Netlink extended acknowledgment. 4454 * 4455 * This function programs the switch's MAC address register with the MAC address 4456 * of the requesting user port. This single address is used by the switch for 4457 * multiple features like HSR self-address filtering and WoL. Other user ports 4458 * can share ownership of this address as long as their MAC address is the same. 4459 * The MAC addresses of user ports must not change while they have ownership of 4460 * the switch MAC address. 4461 * 4462 * Return: 0 on success, or other error codes on failure. 4463 */ 4464 int ksz_switch_macaddr_get(struct dsa_switch *ds, int port, 4465 struct netlink_ext_ack *extack) 4466 { 4467 struct net_device *user = dsa_to_port(ds, port)->user; 4468 const unsigned char *addr = user->dev_addr; 4469 struct ksz_switch_macaddr *switch_macaddr; 4470 struct ksz_device *dev = ds->priv; 4471 const u16 *regs = dev->info->regs; 4472 int i, ret; 4473 4474 /* Make sure concurrent MAC address changes are blocked */ 4475 ASSERT_RTNL(); 4476 4477 switch_macaddr = dev->switch_macaddr; 4478 if (switch_macaddr) { 4479 if (!ether_addr_equal(switch_macaddr->addr, addr)) { 4480 NL_SET_ERR_MSG_FMT_MOD(extack, 4481 "Switch already configured for MAC address %pM", 4482 switch_macaddr->addr); 4483 return -EBUSY; 4484 } 4485 4486 refcount_inc(&switch_macaddr->refcount); 4487 return 0; 4488 } 4489 4490 switch_macaddr = kzalloc(sizeof(*switch_macaddr), GFP_KERNEL); 4491 if (!switch_macaddr) 4492 return -ENOMEM; 4493 4494 ether_addr_copy(switch_macaddr->addr, addr); 4495 refcount_set(&switch_macaddr->refcount, 1); 4496 dev->switch_macaddr = switch_macaddr; 4497 4498 /* Program the switch MAC address to hardware */ 4499 for (i = 0; i < ETH_ALEN; i++) { 4500 ret = ksz_write8(dev, regs[REG_SW_MAC_ADDR] + i, addr[i]); 4501 if (ret) 4502 goto macaddr_drop; 4503 } 4504 4505 return 0; 4506 4507 macaddr_drop: 4508 dev->switch_macaddr = NULL; 4509 refcount_set(&switch_macaddr->refcount, 0); 4510 kfree(switch_macaddr); 4511 4512 return ret; 4513 } 4514 4515 void ksz_switch_macaddr_put(struct dsa_switch *ds) 4516 { 4517 struct ksz_switch_macaddr *switch_macaddr; 4518 struct ksz_device *dev = ds->priv; 4519 const u16 *regs = dev->info->regs; 4520 int i; 4521 4522 /* Make sure concurrent MAC address changes are blocked */ 4523 ASSERT_RTNL(); 4524 4525 switch_macaddr = dev->switch_macaddr; 4526 if (!refcount_dec_and_test(&switch_macaddr->refcount)) 4527 return; 4528 4529 for (i = 0; i < ETH_ALEN; i++) 4530 ksz_write8(dev, regs[REG_SW_MAC_ADDR] + i, 0); 4531 4532 dev->switch_macaddr = NULL; 4533 kfree(switch_macaddr); 4534 } 4535 4536 static int ksz_hsr_join(struct dsa_switch *ds, int port, struct net_device *hsr, 4537 struct netlink_ext_ack *extack) 4538 { 4539 struct ksz_device *dev = ds->priv; 4540 enum hsr_version ver; 4541 int ret; 4542 4543 ret = hsr_get_version(hsr, &ver); 4544 if (ret) 4545 return ret; 4546 4547 if (dev->chip_id != KSZ9477_CHIP_ID) { 4548 NL_SET_ERR_MSG_MOD(extack, "Chip does not support HSR offload"); 4549 return -EOPNOTSUPP; 4550 } 4551 4552 /* KSZ9477 can support HW offloading of only 1 HSR device */ 4553 if (dev->hsr_dev && hsr != dev->hsr_dev) { 4554 NL_SET_ERR_MSG_MOD(extack, "Offload supported for a single HSR"); 4555 return -EOPNOTSUPP; 4556 } 4557 4558 /* KSZ9477 only supports HSR v0 and v1 */ 4559 if (!(ver == HSR_V0 || ver == HSR_V1)) { 4560 NL_SET_ERR_MSG_MOD(extack, "Only HSR v0 and v1 supported"); 4561 return -EOPNOTSUPP; 4562 } 4563 4564 /* KSZ9477 can only perform HSR offloading for up to two ports */ 4565 if (hweight8(dev->hsr_ports) >= 2) { 4566 NL_SET_ERR_MSG_MOD(extack, 4567 "Cannot offload more than two ports - using software HSR"); 4568 return -EOPNOTSUPP; 4569 } 4570 4571 /* Self MAC address filtering, to avoid frames traversing 4572 * the HSR ring more than once. 4573 */ 4574 ret = ksz_switch_macaddr_get(ds, port, extack); 4575 if (ret) 4576 return ret; 4577 4578 ksz9477_hsr_join(ds, port, hsr); 4579 dev->hsr_dev = hsr; 4580 dev->hsr_ports |= BIT(port); 4581 4582 return 0; 4583 } 4584 4585 static int ksz_hsr_leave(struct dsa_switch *ds, int port, 4586 struct net_device *hsr) 4587 { 4588 struct ksz_device *dev = ds->priv; 4589 4590 WARN_ON(dev->chip_id != KSZ9477_CHIP_ID); 4591 4592 ksz9477_hsr_leave(ds, port, hsr); 4593 dev->hsr_ports &= ~BIT(port); 4594 if (!dev->hsr_ports) 4595 dev->hsr_dev = NULL; 4596 4597 ksz_switch_macaddr_put(ds); 4598 4599 return 0; 4600 } 4601 4602 static const struct dsa_switch_ops ksz_switch_ops = { 4603 .get_tag_protocol = ksz_get_tag_protocol, 4604 .connect_tag_protocol = ksz_connect_tag_protocol, 4605 .get_phy_flags = ksz_get_phy_flags, 4606 .setup = ksz_setup, 4607 .teardown = ksz_teardown, 4608 .phy_read = ksz_phy_read16, 4609 .phy_write = ksz_phy_write16, 4610 .phylink_get_caps = ksz_phylink_get_caps, 4611 .port_setup = ksz_port_setup, 4612 .set_ageing_time = ksz_set_ageing_time, 4613 .get_strings = ksz_get_strings, 4614 .get_ethtool_stats = ksz_get_ethtool_stats, 4615 .get_sset_count = ksz_sset_count, 4616 .port_bridge_join = ksz_port_bridge_join, 4617 .port_bridge_leave = ksz_port_bridge_leave, 4618 .port_hsr_join = ksz_hsr_join, 4619 .port_hsr_leave = ksz_hsr_leave, 4620 .port_set_mac_address = ksz_port_set_mac_address, 4621 .port_stp_state_set = ksz_port_stp_state_set, 4622 .port_teardown = ksz_port_teardown, 4623 .port_pre_bridge_flags = ksz_port_pre_bridge_flags, 4624 .port_bridge_flags = ksz_port_bridge_flags, 4625 .port_fast_age = ksz_port_fast_age, 4626 .port_vlan_filtering = ksz_port_vlan_filtering, 4627 .port_vlan_add = ksz_port_vlan_add, 4628 .port_vlan_del = ksz_port_vlan_del, 4629 .port_fdb_dump = ksz_port_fdb_dump, 4630 .port_fdb_add = ksz_port_fdb_add, 4631 .port_fdb_del = ksz_port_fdb_del, 4632 .port_mdb_add = ksz_port_mdb_add, 4633 .port_mdb_del = ksz_port_mdb_del, 4634 .port_mirror_add = ksz_port_mirror_add, 4635 .port_mirror_del = ksz_port_mirror_del, 4636 .get_stats64 = ksz_get_stats64, 4637 .get_pause_stats = ksz_get_pause_stats, 4638 .port_change_mtu = ksz_change_mtu, 4639 .port_max_mtu = ksz_max_mtu, 4640 .get_wol = ksz_get_wol, 4641 .set_wol = ksz_set_wol, 4642 .get_ts_info = ksz_get_ts_info, 4643 .port_hwtstamp_get = ksz_hwtstamp_get, 4644 .port_hwtstamp_set = ksz_hwtstamp_set, 4645 .port_txtstamp = ksz_port_txtstamp, 4646 .port_rxtstamp = ksz_port_rxtstamp, 4647 .cls_flower_add = ksz_cls_flower_add, 4648 .cls_flower_del = ksz_cls_flower_del, 4649 .port_setup_tc = ksz_setup_tc, 4650 .get_mac_eee = ksz_get_mac_eee, 4651 .set_mac_eee = ksz_set_mac_eee, 4652 .port_get_default_prio = ksz_port_get_default_prio, 4653 .port_set_default_prio = ksz_port_set_default_prio, 4654 .port_get_dscp_prio = ksz_port_get_dscp_prio, 4655 .port_add_dscp_prio = ksz_port_add_dscp_prio, 4656 .port_del_dscp_prio = ksz_port_del_dscp_prio, 4657 .port_get_apptrust = ksz_port_get_apptrust, 4658 .port_set_apptrust = ksz_port_set_apptrust, 4659 }; 4660 4661 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv) 4662 { 4663 struct dsa_switch *ds; 4664 struct ksz_device *swdev; 4665 4666 ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL); 4667 if (!ds) 4668 return NULL; 4669 4670 ds->dev = base; 4671 ds->num_ports = DSA_MAX_PORTS; 4672 ds->ops = &ksz_switch_ops; 4673 4674 swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL); 4675 if (!swdev) 4676 return NULL; 4677 4678 ds->priv = swdev; 4679 swdev->dev = base; 4680 4681 swdev->ds = ds; 4682 swdev->priv = priv; 4683 4684 return swdev; 4685 } 4686 EXPORT_SYMBOL(ksz_switch_alloc); 4687 4688 /** 4689 * ksz_switch_shutdown - Shutdown routine for the switch device. 4690 * @dev: The switch device structure. 4691 * 4692 * This function is responsible for initiating a shutdown sequence for the 4693 * switch device. It invokes the reset operation defined in the device 4694 * operations, if available, to reset the switch. Subsequently, it calls the 4695 * DSA framework's shutdown function to ensure a proper shutdown of the DSA 4696 * switch. 4697 */ 4698 void ksz_switch_shutdown(struct ksz_device *dev) 4699 { 4700 bool wol_enabled = false; 4701 4702 ksz_wol_pre_shutdown(dev, &wol_enabled); 4703 4704 if (dev->dev_ops->reset && !wol_enabled) 4705 dev->dev_ops->reset(dev); 4706 4707 dsa_switch_shutdown(dev->ds); 4708 } 4709 EXPORT_SYMBOL(ksz_switch_shutdown); 4710 4711 static void ksz_parse_rgmii_delay(struct ksz_device *dev, int port_num, 4712 struct device_node *port_dn) 4713 { 4714 phy_interface_t phy_mode = dev->ports[port_num].interface; 4715 int rx_delay = -1, tx_delay = -1; 4716 4717 if (!phy_interface_mode_is_rgmii(phy_mode)) 4718 return; 4719 4720 of_property_read_u32(port_dn, "rx-internal-delay-ps", &rx_delay); 4721 of_property_read_u32(port_dn, "tx-internal-delay-ps", &tx_delay); 4722 4723 if (rx_delay == -1 && tx_delay == -1) { 4724 dev_warn(dev->dev, 4725 "Port %d interpreting RGMII delay settings based on \"phy-mode\" property, " 4726 "please update device tree to specify \"rx-internal-delay-ps\" and " 4727 "\"tx-internal-delay-ps\"", 4728 port_num); 4729 4730 if (phy_mode == PHY_INTERFACE_MODE_RGMII_RXID || 4731 phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 4732 rx_delay = 2000; 4733 4734 if (phy_mode == PHY_INTERFACE_MODE_RGMII_TXID || 4735 phy_mode == PHY_INTERFACE_MODE_RGMII_ID) 4736 tx_delay = 2000; 4737 } 4738 4739 if (rx_delay < 0) 4740 rx_delay = 0; 4741 if (tx_delay < 0) 4742 tx_delay = 0; 4743 4744 dev->ports[port_num].rgmii_rx_val = rx_delay; 4745 dev->ports[port_num].rgmii_tx_val = tx_delay; 4746 } 4747 4748 /** 4749 * ksz_drive_strength_to_reg() - Convert drive strength value to corresponding 4750 * register value. 4751 * @array: The array of drive strength values to search. 4752 * @array_size: The size of the array. 4753 * @microamp: The drive strength value in microamp to be converted. 4754 * 4755 * This function searches the array of drive strength values for the given 4756 * microamp value and returns the corresponding register value for that drive. 4757 * 4758 * Returns: If found, the corresponding register value for that drive strength 4759 * is returned. Otherwise, -EINVAL is returned indicating an invalid value. 4760 */ 4761 static int ksz_drive_strength_to_reg(const struct ksz_drive_strength *array, 4762 size_t array_size, int microamp) 4763 { 4764 int i; 4765 4766 for (i = 0; i < array_size; i++) { 4767 if (array[i].microamp == microamp) 4768 return array[i].reg_val; 4769 } 4770 4771 return -EINVAL; 4772 } 4773 4774 /** 4775 * ksz_drive_strength_error() - Report invalid drive strength value 4776 * @dev: ksz device 4777 * @array: The array of drive strength values to search. 4778 * @array_size: The size of the array. 4779 * @microamp: Invalid drive strength value in microamp 4780 * 4781 * This function logs an error message when an unsupported drive strength value 4782 * is detected. It lists out all the supported drive strength values for 4783 * reference in the error message. 4784 */ 4785 static void ksz_drive_strength_error(struct ksz_device *dev, 4786 const struct ksz_drive_strength *array, 4787 size_t array_size, int microamp) 4788 { 4789 char supported_values[100]; 4790 size_t remaining_size; 4791 int added_len; 4792 char *ptr; 4793 int i; 4794 4795 remaining_size = sizeof(supported_values); 4796 ptr = supported_values; 4797 4798 for (i = 0; i < array_size; i++) { 4799 added_len = snprintf(ptr, remaining_size, 4800 i == 0 ? "%d" : ", %d", array[i].microamp); 4801 4802 if (added_len >= remaining_size) 4803 break; 4804 4805 ptr += added_len; 4806 remaining_size -= added_len; 4807 } 4808 4809 dev_err(dev->dev, "Invalid drive strength %d, supported values are %s\n", 4810 microamp, supported_values); 4811 } 4812 4813 /** 4814 * ksz9477_drive_strength_write() - Set the drive strength for specific KSZ9477 4815 * chip variants. 4816 * @dev: ksz device 4817 * @props: Array of drive strength properties to be applied 4818 * @num_props: Number of properties in the array 4819 * 4820 * This function configures the drive strength for various KSZ9477 chip variants 4821 * based on the provided properties. It handles chip-specific nuances and 4822 * ensures only valid drive strengths are written to the respective chip. 4823 * 4824 * Return: 0 on successful configuration, a negative error code on failure. 4825 */ 4826 static int ksz9477_drive_strength_write(struct ksz_device *dev, 4827 struct ksz_driver_strength_prop *props, 4828 int num_props) 4829 { 4830 size_t array_size = ARRAY_SIZE(ksz9477_drive_strengths); 4831 int i, ret, reg; 4832 u8 mask = 0; 4833 u8 val = 0; 4834 4835 if (props[KSZ_DRIVER_STRENGTH_IO].value != -1) 4836 dev_warn(dev->dev, "%s is not supported by this chip variant\n", 4837 props[KSZ_DRIVER_STRENGTH_IO].name); 4838 4839 if (dev->chip_id == KSZ8795_CHIP_ID || 4840 dev->chip_id == KSZ8794_CHIP_ID || 4841 dev->chip_id == KSZ8765_CHIP_ID) 4842 reg = KSZ8795_REG_SW_CTRL_20; 4843 else 4844 reg = KSZ9477_REG_SW_IO_STRENGTH; 4845 4846 for (i = 0; i < num_props; i++) { 4847 if (props[i].value == -1) 4848 continue; 4849 4850 ret = ksz_drive_strength_to_reg(ksz9477_drive_strengths, 4851 array_size, props[i].value); 4852 if (ret < 0) { 4853 ksz_drive_strength_error(dev, ksz9477_drive_strengths, 4854 array_size, props[i].value); 4855 return ret; 4856 } 4857 4858 mask |= SW_DRIVE_STRENGTH_M << props[i].offset; 4859 val |= ret << props[i].offset; 4860 } 4861 4862 return ksz_rmw8(dev, reg, mask, val); 4863 } 4864 4865 /** 4866 * ksz88x3_drive_strength_write() - Set the drive strength configuration for 4867 * KSZ8863 compatible chip variants. 4868 * @dev: ksz device 4869 * @props: Array of drive strength properties to be set 4870 * @num_props: Number of properties in the array 4871 * 4872 * This function applies the specified drive strength settings to KSZ88X3 chip 4873 * variants (KSZ8873, KSZ8863). 4874 * It ensures the configurations align with what the chip variant supports and 4875 * warns or errors out on unsupported settings. 4876 * 4877 * Return: 0 on success, error code otherwise 4878 */ 4879 static int ksz88x3_drive_strength_write(struct ksz_device *dev, 4880 struct ksz_driver_strength_prop *props, 4881 int num_props) 4882 { 4883 size_t array_size = ARRAY_SIZE(ksz88x3_drive_strengths); 4884 int microamp; 4885 int i, ret; 4886 4887 for (i = 0; i < num_props; i++) { 4888 if (props[i].value == -1 || i == KSZ_DRIVER_STRENGTH_IO) 4889 continue; 4890 4891 dev_warn(dev->dev, "%s is not supported by this chip variant\n", 4892 props[i].name); 4893 } 4894 4895 microamp = props[KSZ_DRIVER_STRENGTH_IO].value; 4896 ret = ksz_drive_strength_to_reg(ksz88x3_drive_strengths, array_size, 4897 microamp); 4898 if (ret < 0) { 4899 ksz_drive_strength_error(dev, ksz88x3_drive_strengths, 4900 array_size, microamp); 4901 return ret; 4902 } 4903 4904 return ksz_rmw8(dev, KSZ8873_REG_GLOBAL_CTRL_12, 4905 KSZ8873_DRIVE_STRENGTH_16MA, ret); 4906 } 4907 4908 /** 4909 * ksz_parse_drive_strength() - Extract and apply drive strength configurations 4910 * from device tree properties. 4911 * @dev: ksz device 4912 * 4913 * This function reads the specified drive strength properties from the 4914 * device tree, validates against the supported chip variants, and sets 4915 * them accordingly. An error should be critical here, as the drive strength 4916 * settings are crucial for EMI compliance. 4917 * 4918 * Return: 0 on success, error code otherwise 4919 */ 4920 static int ksz_parse_drive_strength(struct ksz_device *dev) 4921 { 4922 struct ksz_driver_strength_prop of_props[] = { 4923 [KSZ_DRIVER_STRENGTH_HI] = { 4924 .name = "microchip,hi-drive-strength-microamp", 4925 .offset = SW_HI_SPEED_DRIVE_STRENGTH_S, 4926 .value = -1, 4927 }, 4928 [KSZ_DRIVER_STRENGTH_LO] = { 4929 .name = "microchip,lo-drive-strength-microamp", 4930 .offset = SW_LO_SPEED_DRIVE_STRENGTH_S, 4931 .value = -1, 4932 }, 4933 [KSZ_DRIVER_STRENGTH_IO] = { 4934 .name = "microchip,io-drive-strength-microamp", 4935 .offset = 0, /* don't care */ 4936 .value = -1, 4937 }, 4938 }; 4939 struct device_node *np = dev->dev->of_node; 4940 bool have_any_prop = false; 4941 int i, ret; 4942 4943 for (i = 0; i < ARRAY_SIZE(of_props); i++) { 4944 ret = of_property_read_u32(np, of_props[i].name, 4945 &of_props[i].value); 4946 if (ret && ret != -EINVAL) 4947 dev_warn(dev->dev, "Failed to read %s\n", 4948 of_props[i].name); 4949 if (ret) 4950 continue; 4951 4952 have_any_prop = true; 4953 } 4954 4955 if (!have_any_prop) 4956 return 0; 4957 4958 switch (dev->chip_id) { 4959 case KSZ88X3_CHIP_ID: 4960 return ksz88x3_drive_strength_write(dev, of_props, 4961 ARRAY_SIZE(of_props)); 4962 case KSZ8795_CHIP_ID: 4963 case KSZ8794_CHIP_ID: 4964 case KSZ8765_CHIP_ID: 4965 case KSZ8563_CHIP_ID: 4966 case KSZ8567_CHIP_ID: 4967 case KSZ9477_CHIP_ID: 4968 case KSZ9563_CHIP_ID: 4969 case KSZ9567_CHIP_ID: 4970 case KSZ9893_CHIP_ID: 4971 case KSZ9896_CHIP_ID: 4972 case KSZ9897_CHIP_ID: 4973 case LAN9646_CHIP_ID: 4974 return ksz9477_drive_strength_write(dev, of_props, 4975 ARRAY_SIZE(of_props)); 4976 default: 4977 for (i = 0; i < ARRAY_SIZE(of_props); i++) { 4978 if (of_props[i].value == -1) 4979 continue; 4980 4981 dev_warn(dev->dev, "%s is not supported by this chip variant\n", 4982 of_props[i].name); 4983 } 4984 } 4985 4986 return 0; 4987 } 4988 4989 int ksz_switch_register(struct ksz_device *dev) 4990 { 4991 const struct ksz_chip_data *info; 4992 struct device_node *ports; 4993 phy_interface_t interface; 4994 unsigned int port_num; 4995 int ret; 4996 int i; 4997 4998 dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset", 4999 GPIOD_OUT_LOW); 5000 if (IS_ERR(dev->reset_gpio)) 5001 return PTR_ERR(dev->reset_gpio); 5002 5003 if (dev->reset_gpio) { 5004 gpiod_set_value_cansleep(dev->reset_gpio, 1); 5005 usleep_range(10000, 12000); 5006 gpiod_set_value_cansleep(dev->reset_gpio, 0); 5007 msleep(100); 5008 } 5009 5010 mutex_init(&dev->dev_mutex); 5011 mutex_init(&dev->regmap_mutex); 5012 mutex_init(&dev->alu_mutex); 5013 mutex_init(&dev->vlan_mutex); 5014 5015 ret = ksz_switch_detect(dev); 5016 if (ret) 5017 return ret; 5018 5019 info = ksz_lookup_info(dev->chip_id); 5020 if (!info) 5021 return -ENODEV; 5022 5023 /* Update the compatible info with the probed one */ 5024 dev->info = info; 5025 5026 dev_info(dev->dev, "found switch: %s, rev %i\n", 5027 dev->info->dev_name, dev->chip_rev); 5028 5029 ret = ksz_check_device_id(dev); 5030 if (ret) 5031 return ret; 5032 5033 dev->dev_ops = dev->info->ops; 5034 5035 ret = dev->dev_ops->init(dev); 5036 if (ret) 5037 return ret; 5038 5039 dev->ports = devm_kzalloc(dev->dev, 5040 dev->info->port_cnt * sizeof(struct ksz_port), 5041 GFP_KERNEL); 5042 if (!dev->ports) 5043 return -ENOMEM; 5044 5045 for (i = 0; i < dev->info->port_cnt; i++) { 5046 spin_lock_init(&dev->ports[i].mib.stats64_lock); 5047 mutex_init(&dev->ports[i].mib.cnt_mutex); 5048 dev->ports[i].mib.counters = 5049 devm_kzalloc(dev->dev, 5050 sizeof(u64) * (dev->info->mib_cnt + 1), 5051 GFP_KERNEL); 5052 if (!dev->ports[i].mib.counters) 5053 return -ENOMEM; 5054 5055 dev->ports[i].ksz_dev = dev; 5056 dev->ports[i].num = i; 5057 } 5058 5059 /* set the real number of ports */ 5060 dev->ds->num_ports = dev->info->port_cnt; 5061 5062 /* set the phylink ops */ 5063 dev->ds->phylink_mac_ops = dev->info->phylink_mac_ops; 5064 5065 /* Host port interface will be self detected, or specifically set in 5066 * device tree. 5067 */ 5068 for (port_num = 0; port_num < dev->info->port_cnt; ++port_num) 5069 dev->ports[port_num].interface = PHY_INTERFACE_MODE_NA; 5070 if (dev->dev->of_node) { 5071 ret = of_get_phy_mode(dev->dev->of_node, &interface); 5072 if (ret == 0) 5073 dev->compat_interface = interface; 5074 ports = of_get_child_by_name(dev->dev->of_node, "ethernet-ports"); 5075 if (!ports) 5076 ports = of_get_child_by_name(dev->dev->of_node, "ports"); 5077 if (ports) { 5078 for_each_available_child_of_node_scoped(ports, port) { 5079 if (of_property_read_u32(port, "reg", 5080 &port_num)) 5081 continue; 5082 if (!(dev->port_mask & BIT(port_num))) { 5083 of_node_put(ports); 5084 return -EINVAL; 5085 } 5086 of_get_phy_mode(port, 5087 &dev->ports[port_num].interface); 5088 5089 ksz_parse_rgmii_delay(dev, port_num, port); 5090 } 5091 of_node_put(ports); 5092 } 5093 dev->synclko_125 = of_property_read_bool(dev->dev->of_node, 5094 "microchip,synclko-125"); 5095 dev->synclko_disable = of_property_read_bool(dev->dev->of_node, 5096 "microchip,synclko-disable"); 5097 if (dev->synclko_125 && dev->synclko_disable) { 5098 dev_err(dev->dev, "inconsistent synclko settings\n"); 5099 return -EINVAL; 5100 } 5101 5102 dev->wakeup_source = of_property_read_bool(dev->dev->of_node, 5103 "wakeup-source"); 5104 dev->pme_active_high = of_property_read_bool(dev->dev->of_node, 5105 "microchip,pme-active-high"); 5106 } 5107 5108 ret = dsa_register_switch(dev->ds); 5109 if (ret) { 5110 dev->dev_ops->exit(dev); 5111 return ret; 5112 } 5113 5114 /* Read MIB counters every 30 seconds to avoid overflow. */ 5115 dev->mib_read_interval = msecs_to_jiffies(5000); 5116 5117 /* Start the MIB timer. */ 5118 schedule_delayed_work(&dev->mib_read, 0); 5119 5120 return ret; 5121 } 5122 EXPORT_SYMBOL(ksz_switch_register); 5123 5124 void ksz_switch_remove(struct ksz_device *dev) 5125 { 5126 /* timer started */ 5127 if (dev->mib_read_interval) { 5128 dev->mib_read_interval = 0; 5129 cancel_delayed_work_sync(&dev->mib_read); 5130 } 5131 5132 dev->dev_ops->exit(dev); 5133 dsa_unregister_switch(dev->ds); 5134 5135 if (dev->reset_gpio) 5136 gpiod_set_value_cansleep(dev->reset_gpio, 1); 5137 5138 } 5139 EXPORT_SYMBOL(ksz_switch_remove); 5140 5141 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>"); 5142 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver"); 5143 MODULE_LICENSE("GPL"); 5144