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