1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip switch driver main logic
5 * Copyright (C) 2017-2019 Microchip Technology Inc.
8 #include <linux/delay.h>
9 #include <linux/export.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/platform_data/microchip-ksz.h>
14 #include <linux/phy.h>
15 #include <linux/etherdevice.h>
16 #include <linux/if_bridge.h>
17 #include <linux/of_device.h>
18 #include <linux/of_net.h>
19 #include <linux/micrel_phy.h>
21 #include <net/switchdev.h>
23 #include "ksz_common.h"
28 #define MIB_COUNTER_NUM 0x20
30 struct ksz_stats_raw {
69 static const struct ksz_mib_names ksz88xx_mib_names[] = {
72 { 0x02, "rx_undersize" },
73 { 0x03, "rx_fragments" },
74 { 0x04, "rx_oversize" },
75 { 0x05, "rx_jabbers" },
76 { 0x06, "rx_symbol_err" },
77 { 0x07, "rx_crc_err" },
78 { 0x08, "rx_align_err" },
79 { 0x09, "rx_mac_ctrl" },
84 { 0x0e, "rx_64_or_less" },
85 { 0x0f, "rx_65_127" },
86 { 0x10, "rx_128_255" },
87 { 0x11, "rx_256_511" },
88 { 0x12, "rx_512_1023" },
89 { 0x13, "rx_1024_1522" },
92 { 0x16, "tx_late_col" },
97 { 0x1b, "tx_deferred" },
98 { 0x1c, "tx_total_col" },
99 { 0x1d, "tx_exc_col" },
100 { 0x1e, "tx_single_col" },
101 { 0x1f, "tx_mult_col" },
102 { 0x100, "rx_discards" },
103 { 0x101, "tx_discards" },
106 static const struct ksz_mib_names ksz9477_mib_names[] = {
108 { 0x01, "rx_undersize" },
109 { 0x02, "rx_fragments" },
110 { 0x03, "rx_oversize" },
111 { 0x04, "rx_jabbers" },
112 { 0x05, "rx_symbol_err" },
113 { 0x06, "rx_crc_err" },
114 { 0x07, "rx_align_err" },
115 { 0x08, "rx_mac_ctrl" },
116 { 0x09, "rx_pause" },
117 { 0x0A, "rx_bcast" },
118 { 0x0B, "rx_mcast" },
119 { 0x0C, "rx_ucast" },
120 { 0x0D, "rx_64_or_less" },
121 { 0x0E, "rx_65_127" },
122 { 0x0F, "rx_128_255" },
123 { 0x10, "rx_256_511" },
124 { 0x11, "rx_512_1023" },
125 { 0x12, "rx_1024_1522" },
126 { 0x13, "rx_1523_2000" },
129 { 0x16, "tx_late_col" },
130 { 0x17, "tx_pause" },
131 { 0x18, "tx_bcast" },
132 { 0x19, "tx_mcast" },
133 { 0x1A, "tx_ucast" },
134 { 0x1B, "tx_deferred" },
135 { 0x1C, "tx_total_col" },
136 { 0x1D, "tx_exc_col" },
137 { 0x1E, "tx_single_col" },
138 { 0x1F, "tx_mult_col" },
139 { 0x80, "rx_total" },
140 { 0x81, "tx_total" },
141 { 0x82, "rx_discards" },
142 { 0x83, "tx_discards" },
145 static const struct ksz_dev_ops ksz8_dev_ops = {
147 .get_port_addr = ksz8_get_port_addr,
148 .cfg_port_member = ksz8_cfg_port_member,
149 .flush_dyn_mac_table = ksz8_flush_dyn_mac_table,
150 .port_setup = ksz8_port_setup,
153 .r_mib_cnt = ksz8_r_mib_cnt,
154 .r_mib_pkt = ksz8_r_mib_pkt,
155 .freeze_mib = ksz8_freeze_mib,
156 .port_init_cnt = ksz8_port_init_cnt,
157 .fdb_dump = ksz8_fdb_dump,
158 .mdb_add = ksz8_mdb_add,
159 .mdb_del = ksz8_mdb_del,
160 .vlan_filtering = ksz8_port_vlan_filtering,
161 .vlan_add = ksz8_port_vlan_add,
162 .vlan_del = ksz8_port_vlan_del,
163 .mirror_add = ksz8_port_mirror_add,
164 .mirror_del = ksz8_port_mirror_del,
165 .get_caps = ksz8_get_caps,
166 .config_cpu_port = ksz8_config_cpu_port,
167 .enable_stp_addr = ksz8_enable_stp_addr,
168 .reset = ksz8_reset_switch,
169 .init = ksz8_switch_init,
170 .exit = ksz8_switch_exit,
173 static const struct ksz_dev_ops ksz9477_dev_ops = {
174 .setup = ksz9477_setup,
175 .get_port_addr = ksz9477_get_port_addr,
176 .cfg_port_member = ksz9477_cfg_port_member,
177 .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table,
178 .port_setup = ksz9477_port_setup,
179 .r_phy = ksz9477_r_phy,
180 .w_phy = ksz9477_w_phy,
181 .r_mib_cnt = ksz9477_r_mib_cnt,
182 .r_mib_pkt = ksz9477_r_mib_pkt,
183 .r_mib_stat64 = ksz_r_mib_stats64,
184 .freeze_mib = ksz9477_freeze_mib,
185 .port_init_cnt = ksz9477_port_init_cnt,
186 .vlan_filtering = ksz9477_port_vlan_filtering,
187 .vlan_add = ksz9477_port_vlan_add,
188 .vlan_del = ksz9477_port_vlan_del,
189 .mirror_add = ksz9477_port_mirror_add,
190 .mirror_del = ksz9477_port_mirror_del,
191 .get_caps = ksz9477_get_caps,
192 .fdb_dump = ksz9477_fdb_dump,
193 .fdb_add = ksz9477_fdb_add,
194 .fdb_del = ksz9477_fdb_del,
195 .mdb_add = ksz9477_mdb_add,
196 .mdb_del = ksz9477_mdb_del,
197 .change_mtu = ksz9477_change_mtu,
198 .max_mtu = ksz9477_max_mtu,
199 .config_cpu_port = ksz9477_config_cpu_port,
200 .enable_stp_addr = ksz9477_enable_stp_addr,
201 .reset = ksz9477_reset_switch,
202 .init = ksz9477_switch_init,
203 .exit = ksz9477_switch_exit,
206 static const struct ksz_dev_ops lan937x_dev_ops = {
207 .setup = lan937x_setup,
208 .get_port_addr = ksz9477_get_port_addr,
209 .cfg_port_member = ksz9477_cfg_port_member,
210 .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table,
211 .port_setup = lan937x_port_setup,
212 .r_phy = lan937x_r_phy,
213 .w_phy = lan937x_w_phy,
214 .r_mib_cnt = ksz9477_r_mib_cnt,
215 .r_mib_pkt = ksz9477_r_mib_pkt,
216 .r_mib_stat64 = ksz_r_mib_stats64,
217 .freeze_mib = ksz9477_freeze_mib,
218 .port_init_cnt = ksz9477_port_init_cnt,
219 .vlan_filtering = ksz9477_port_vlan_filtering,
220 .vlan_add = ksz9477_port_vlan_add,
221 .vlan_del = ksz9477_port_vlan_del,
222 .mirror_add = ksz9477_port_mirror_add,
223 .mirror_del = ksz9477_port_mirror_del,
224 .get_caps = lan937x_phylink_get_caps,
225 .setup_rgmii_delay = lan937x_setup_rgmii_delay,
226 .fdb_dump = ksz9477_fdb_dump,
227 .fdb_add = ksz9477_fdb_add,
228 .fdb_del = ksz9477_fdb_del,
229 .mdb_add = ksz9477_mdb_add,
230 .mdb_del = ksz9477_mdb_del,
231 .change_mtu = lan937x_change_mtu,
232 .max_mtu = ksz9477_max_mtu,
233 .config_cpu_port = lan937x_config_cpu_port,
234 .enable_stp_addr = ksz9477_enable_stp_addr,
235 .reset = lan937x_reset_switch,
236 .init = lan937x_switch_init,
237 .exit = lan937x_switch_exit,
240 static const u16 ksz8795_regs[] = {
241 [REG_IND_CTRL_0] = 0x6E,
242 [REG_IND_DATA_8] = 0x70,
243 [REG_IND_DATA_CHECK] = 0x72,
244 [REG_IND_DATA_HI] = 0x71,
245 [REG_IND_DATA_LO] = 0x75,
246 [REG_IND_MIB_CHECK] = 0x74,
247 [REG_IND_BYTE] = 0xA0,
248 [P_FORCE_CTRL] = 0x0C,
249 [P_LINK_STATUS] = 0x0E,
250 [P_LOCAL_CTRL] = 0x07,
251 [P_NEG_RESTART_CTRL] = 0x0D,
252 [P_REMOTE_STATUS] = 0x08,
253 [P_SPEED_STATUS] = 0x09,
254 [S_TAIL_TAG_CTRL] = 0x0C,
256 [S_START_CTRL] = 0x01,
257 [S_BROADCAST_CTRL] = 0x06,
258 [S_MULTICAST_CTRL] = 0x04,
259 [P_XMII_CTRL_0] = 0x06,
260 [P_XMII_CTRL_1] = 0x56,
263 static const u32 ksz8795_masks[] = {
264 [PORT_802_1P_REMAPPING] = BIT(7),
265 [SW_TAIL_TAG_ENABLE] = BIT(1),
266 [MIB_COUNTER_OVERFLOW] = BIT(6),
267 [MIB_COUNTER_VALID] = BIT(5),
268 [VLAN_TABLE_FID] = GENMASK(6, 0),
269 [VLAN_TABLE_MEMBERSHIP] = GENMASK(11, 7),
270 [VLAN_TABLE_VALID] = BIT(12),
271 [STATIC_MAC_TABLE_VALID] = BIT(21),
272 [STATIC_MAC_TABLE_USE_FID] = BIT(23),
273 [STATIC_MAC_TABLE_FID] = GENMASK(30, 24),
274 [STATIC_MAC_TABLE_OVERRIDE] = BIT(26),
275 [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(24, 20),
276 [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(6, 0),
277 [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(8),
278 [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7),
279 [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 29),
280 [DYNAMIC_MAC_TABLE_FID] = GENMASK(26, 20),
281 [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(26, 24),
282 [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(28, 27),
283 [P_MII_TX_FLOW_CTRL] = BIT(5),
284 [P_MII_RX_FLOW_CTRL] = BIT(5),
287 static const u8 ksz8795_xmii_ctrl0[] = {
290 [P_MII_FULL_DUPLEX] = 0,
291 [P_MII_HALF_DUPLEX] = 1,
294 static const u8 ksz8795_xmii_ctrl1[] = {
300 [P_GMII_NOT_1GBIT] = 0,
303 static const u8 ksz8795_shifts[] = {
304 [VLAN_TABLE_MEMBERSHIP_S] = 7,
306 [STATIC_MAC_FWD_PORTS] = 16,
307 [STATIC_MAC_FID] = 24,
308 [DYNAMIC_MAC_ENTRIES_H] = 3,
309 [DYNAMIC_MAC_ENTRIES] = 29,
310 [DYNAMIC_MAC_FID] = 16,
311 [DYNAMIC_MAC_TIMESTAMP] = 27,
312 [DYNAMIC_MAC_SRC_PORT] = 24,
315 static const u16 ksz8863_regs[] = {
316 [REG_IND_CTRL_0] = 0x79,
317 [REG_IND_DATA_8] = 0x7B,
318 [REG_IND_DATA_CHECK] = 0x7B,
319 [REG_IND_DATA_HI] = 0x7C,
320 [REG_IND_DATA_LO] = 0x80,
321 [REG_IND_MIB_CHECK] = 0x80,
322 [P_FORCE_CTRL] = 0x0C,
323 [P_LINK_STATUS] = 0x0E,
324 [P_LOCAL_CTRL] = 0x0C,
325 [P_NEG_RESTART_CTRL] = 0x0D,
326 [P_REMOTE_STATUS] = 0x0E,
327 [P_SPEED_STATUS] = 0x0F,
328 [S_TAIL_TAG_CTRL] = 0x03,
330 [S_START_CTRL] = 0x01,
331 [S_BROADCAST_CTRL] = 0x06,
332 [S_MULTICAST_CTRL] = 0x04,
335 static const u32 ksz8863_masks[] = {
336 [PORT_802_1P_REMAPPING] = BIT(3),
337 [SW_TAIL_TAG_ENABLE] = BIT(6),
338 [MIB_COUNTER_OVERFLOW] = BIT(7),
339 [MIB_COUNTER_VALID] = BIT(6),
340 [VLAN_TABLE_FID] = GENMASK(15, 12),
341 [VLAN_TABLE_MEMBERSHIP] = GENMASK(18, 16),
342 [VLAN_TABLE_VALID] = BIT(19),
343 [STATIC_MAC_TABLE_VALID] = BIT(19),
344 [STATIC_MAC_TABLE_USE_FID] = BIT(21),
345 [STATIC_MAC_TABLE_FID] = GENMASK(29, 26),
346 [STATIC_MAC_TABLE_OVERRIDE] = BIT(20),
347 [STATIC_MAC_TABLE_FWD_PORTS] = GENMASK(18, 16),
348 [DYNAMIC_MAC_TABLE_ENTRIES_H] = GENMASK(5, 0),
349 [DYNAMIC_MAC_TABLE_MAC_EMPTY] = BIT(7),
350 [DYNAMIC_MAC_TABLE_NOT_READY] = BIT(7),
351 [DYNAMIC_MAC_TABLE_ENTRIES] = GENMASK(31, 28),
352 [DYNAMIC_MAC_TABLE_FID] = GENMASK(19, 16),
353 [DYNAMIC_MAC_TABLE_SRC_PORT] = GENMASK(21, 20),
354 [DYNAMIC_MAC_TABLE_TIMESTAMP] = GENMASK(23, 22),
357 static u8 ksz8863_shifts[] = {
358 [VLAN_TABLE_MEMBERSHIP_S] = 16,
359 [STATIC_MAC_FWD_PORTS] = 16,
360 [STATIC_MAC_FID] = 22,
361 [DYNAMIC_MAC_ENTRIES_H] = 3,
362 [DYNAMIC_MAC_ENTRIES] = 24,
363 [DYNAMIC_MAC_FID] = 16,
364 [DYNAMIC_MAC_TIMESTAMP] = 24,
365 [DYNAMIC_MAC_SRC_PORT] = 20,
368 static const u16 ksz9477_regs[] = {
369 [P_STP_CTRL] = 0x0B04,
370 [S_START_CTRL] = 0x0300,
371 [S_BROADCAST_CTRL] = 0x0332,
372 [S_MULTICAST_CTRL] = 0x0331,
373 [P_XMII_CTRL_0] = 0x0300,
374 [P_XMII_CTRL_1] = 0x0301,
377 static const u32 ksz9477_masks[] = {
378 [ALU_STAT_WRITE] = 0,
380 [P_MII_TX_FLOW_CTRL] = BIT(5),
381 [P_MII_RX_FLOW_CTRL] = BIT(3),
384 static const u8 ksz9477_shifts[] = {
385 [ALU_STAT_INDEX] = 16,
388 static const u8 ksz9477_xmii_ctrl0[] = {
391 [P_MII_FULL_DUPLEX] = 1,
392 [P_MII_HALF_DUPLEX] = 0,
395 static const u8 ksz9477_xmii_ctrl1[] = {
401 [P_GMII_NOT_1GBIT] = 1,
404 static const u32 lan937x_masks[] = {
405 [ALU_STAT_WRITE] = 1,
407 [P_MII_TX_FLOW_CTRL] = BIT(5),
408 [P_MII_RX_FLOW_CTRL] = BIT(3),
411 static const u8 lan937x_shifts[] = {
412 [ALU_STAT_INDEX] = 8,
415 const struct ksz_chip_data ksz_switch_chips[] = {
417 .chip_id = KSZ8795_CHIP_ID,
418 .dev_name = "KSZ8795",
422 .cpu_ports = 0x10, /* can be configured as cpu port */
423 .port_cnt = 5, /* total cpu and user ports */
424 .ops = &ksz8_dev_ops,
425 .ksz87xx_eee_link_erratum = true,
426 .mib_names = ksz9477_mib_names,
427 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
428 .reg_mib_cnt = MIB_COUNTER_NUM,
429 .regs = ksz8795_regs,
430 .masks = ksz8795_masks,
431 .shifts = ksz8795_shifts,
432 .xmii_ctrl0 = ksz8795_xmii_ctrl0,
433 .xmii_ctrl1 = ksz8795_xmii_ctrl1,
434 .supports_mii = {false, false, false, false, true},
435 .supports_rmii = {false, false, false, false, true},
436 .supports_rgmii = {false, false, false, false, true},
437 .internal_phy = {true, true, true, true, false},
443 * KSZ8794 is similar to KSZ8795, except the port map
444 * contains a gap between external and CPU ports, the
445 * port map is NOT continuous. The per-port register
446 * map is shifted accordingly too, i.e. registers at
447 * offset 0x40 are NOT used on KSZ8794 and they ARE
448 * used on KSZ8795 for external port 3.
453 * port_cnt is configured as 5, even though it is 4
455 .chip_id = KSZ8794_CHIP_ID,
456 .dev_name = "KSZ8794",
460 .cpu_ports = 0x10, /* can be configured as cpu port */
461 .port_cnt = 5, /* total cpu and user ports */
462 .ops = &ksz8_dev_ops,
463 .ksz87xx_eee_link_erratum = true,
464 .mib_names = ksz9477_mib_names,
465 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
466 .reg_mib_cnt = MIB_COUNTER_NUM,
467 .regs = ksz8795_regs,
468 .masks = ksz8795_masks,
469 .shifts = ksz8795_shifts,
470 .xmii_ctrl0 = ksz8795_xmii_ctrl0,
471 .xmii_ctrl1 = ksz8795_xmii_ctrl1,
472 .supports_mii = {false, false, false, false, true},
473 .supports_rmii = {false, false, false, false, true},
474 .supports_rgmii = {false, false, false, false, true},
475 .internal_phy = {true, true, true, false, false},
479 .chip_id = KSZ8765_CHIP_ID,
480 .dev_name = "KSZ8765",
484 .cpu_ports = 0x10, /* can be configured as cpu port */
485 .port_cnt = 5, /* total cpu and user ports */
486 .ops = &ksz8_dev_ops,
487 .ksz87xx_eee_link_erratum = true,
488 .mib_names = ksz9477_mib_names,
489 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
490 .reg_mib_cnt = MIB_COUNTER_NUM,
491 .regs = ksz8795_regs,
492 .masks = ksz8795_masks,
493 .shifts = ksz8795_shifts,
494 .xmii_ctrl0 = ksz8795_xmii_ctrl0,
495 .xmii_ctrl1 = ksz8795_xmii_ctrl1,
496 .supports_mii = {false, false, false, false, true},
497 .supports_rmii = {false, false, false, false, true},
498 .supports_rgmii = {false, false, false, false, true},
499 .internal_phy = {true, true, true, true, false},
503 .chip_id = KSZ8830_CHIP_ID,
504 .dev_name = "KSZ8863/KSZ8873",
508 .cpu_ports = 0x4, /* can be configured as cpu port */
510 .ops = &ksz8_dev_ops,
511 .mib_names = ksz88xx_mib_names,
512 .mib_cnt = ARRAY_SIZE(ksz88xx_mib_names),
513 .reg_mib_cnt = MIB_COUNTER_NUM,
514 .regs = ksz8863_regs,
515 .masks = ksz8863_masks,
516 .shifts = ksz8863_shifts,
517 .supports_mii = {false, false, true},
518 .supports_rmii = {false, false, true},
519 .internal_phy = {true, true, false},
523 .chip_id = KSZ9477_CHIP_ID,
524 .dev_name = "KSZ9477",
528 .cpu_ports = 0x7F, /* can be configured as cpu port */
529 .port_cnt = 7, /* total physical port count */
530 .ops = &ksz9477_dev_ops,
531 .phy_errata_9477 = true,
532 .mib_names = ksz9477_mib_names,
533 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
534 .reg_mib_cnt = MIB_COUNTER_NUM,
535 .regs = ksz9477_regs,
536 .masks = ksz9477_masks,
537 .shifts = ksz9477_shifts,
538 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
539 .xmii_ctrl1 = ksz9477_xmii_ctrl1,
540 .supports_mii = {false, false, false, false,
542 .supports_rmii = {false, false, false, false,
544 .supports_rgmii = {false, false, false, false,
546 .internal_phy = {true, true, true, true,
551 .chip_id = KSZ9897_CHIP_ID,
552 .dev_name = "KSZ9897",
556 .cpu_ports = 0x7F, /* can be configured as cpu port */
557 .port_cnt = 7, /* total physical port count */
558 .ops = &ksz9477_dev_ops,
559 .phy_errata_9477 = true,
560 .mib_names = ksz9477_mib_names,
561 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
562 .reg_mib_cnt = MIB_COUNTER_NUM,
563 .regs = ksz9477_regs,
564 .masks = ksz9477_masks,
565 .shifts = ksz9477_shifts,
566 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
567 .xmii_ctrl1 = ksz9477_xmii_ctrl1,
568 .supports_mii = {false, false, false, false,
570 .supports_rmii = {false, false, false, false,
572 .supports_rgmii = {false, false, false, false,
574 .internal_phy = {true, true, true, true,
579 .chip_id = KSZ9893_CHIP_ID,
580 .dev_name = "KSZ9893",
584 .cpu_ports = 0x07, /* can be configured as cpu port */
585 .port_cnt = 3, /* total port count */
586 .ops = &ksz9477_dev_ops,
587 .mib_names = ksz9477_mib_names,
588 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
589 .reg_mib_cnt = MIB_COUNTER_NUM,
590 .regs = ksz9477_regs,
591 .masks = ksz9477_masks,
592 .shifts = ksz9477_shifts,
593 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
594 .xmii_ctrl1 = ksz8795_xmii_ctrl1, /* Same as ksz8795 */
595 .supports_mii = {false, false, true},
596 .supports_rmii = {false, false, true},
597 .supports_rgmii = {false, false, true},
598 .internal_phy = {true, true, false},
602 .chip_id = KSZ9567_CHIP_ID,
603 .dev_name = "KSZ9567",
607 .cpu_ports = 0x7F, /* can be configured as cpu port */
608 .port_cnt = 7, /* total physical port count */
609 .ops = &ksz9477_dev_ops,
610 .phy_errata_9477 = true,
611 .mib_names = ksz9477_mib_names,
612 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
613 .reg_mib_cnt = MIB_COUNTER_NUM,
614 .regs = ksz9477_regs,
615 .masks = ksz9477_masks,
616 .shifts = ksz9477_shifts,
617 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
618 .xmii_ctrl1 = ksz9477_xmii_ctrl1,
619 .supports_mii = {false, false, false, false,
621 .supports_rmii = {false, false, false, false,
623 .supports_rgmii = {false, false, false, false,
625 .internal_phy = {true, true, true, true,
630 .chip_id = LAN9370_CHIP_ID,
631 .dev_name = "LAN9370",
635 .cpu_ports = 0x10, /* can be configured as cpu port */
636 .port_cnt = 5, /* total physical port count */
637 .ops = &lan937x_dev_ops,
638 .mib_names = ksz9477_mib_names,
639 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
640 .reg_mib_cnt = MIB_COUNTER_NUM,
641 .regs = ksz9477_regs,
642 .masks = lan937x_masks,
643 .shifts = lan937x_shifts,
644 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
645 .xmii_ctrl1 = ksz9477_xmii_ctrl1,
646 .supports_mii = {false, false, false, false, true},
647 .supports_rmii = {false, false, false, false, true},
648 .supports_rgmii = {false, false, false, false, true},
649 .internal_phy = {true, true, true, true, false},
653 .chip_id = LAN9371_CHIP_ID,
654 .dev_name = "LAN9371",
658 .cpu_ports = 0x30, /* can be configured as cpu port */
659 .port_cnt = 6, /* total physical port count */
660 .ops = &lan937x_dev_ops,
661 .mib_names = ksz9477_mib_names,
662 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
663 .reg_mib_cnt = MIB_COUNTER_NUM,
664 .regs = ksz9477_regs,
665 .masks = lan937x_masks,
666 .shifts = lan937x_shifts,
667 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
668 .xmii_ctrl1 = ksz9477_xmii_ctrl1,
669 .supports_mii = {false, false, false, false, true, true},
670 .supports_rmii = {false, false, false, false, true, true},
671 .supports_rgmii = {false, false, false, false, true, true},
672 .internal_phy = {true, true, true, true, false, false},
676 .chip_id = LAN9372_CHIP_ID,
677 .dev_name = "LAN9372",
681 .cpu_ports = 0x30, /* can be configured as cpu port */
682 .port_cnt = 8, /* total physical port count */
683 .ops = &lan937x_dev_ops,
684 .mib_names = ksz9477_mib_names,
685 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
686 .reg_mib_cnt = MIB_COUNTER_NUM,
687 .regs = ksz9477_regs,
688 .masks = lan937x_masks,
689 .shifts = lan937x_shifts,
690 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
691 .xmii_ctrl1 = ksz9477_xmii_ctrl1,
692 .supports_mii = {false, false, false, false,
693 true, true, false, false},
694 .supports_rmii = {false, false, false, false,
695 true, true, false, false},
696 .supports_rgmii = {false, false, false, false,
697 true, true, false, false},
698 .internal_phy = {true, true, true, true,
699 false, false, true, true},
703 .chip_id = LAN9373_CHIP_ID,
704 .dev_name = "LAN9373",
708 .cpu_ports = 0x38, /* can be configured as cpu port */
709 .port_cnt = 5, /* total physical port count */
710 .ops = &lan937x_dev_ops,
711 .mib_names = ksz9477_mib_names,
712 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
713 .reg_mib_cnt = MIB_COUNTER_NUM,
714 .regs = ksz9477_regs,
715 .masks = lan937x_masks,
716 .shifts = lan937x_shifts,
717 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
718 .xmii_ctrl1 = ksz9477_xmii_ctrl1,
719 .supports_mii = {false, false, false, false,
720 true, true, false, false},
721 .supports_rmii = {false, false, false, false,
722 true, true, false, false},
723 .supports_rgmii = {false, false, false, false,
724 true, true, false, false},
725 .internal_phy = {true, true, true, false,
726 false, false, true, true},
730 .chip_id = LAN9374_CHIP_ID,
731 .dev_name = "LAN9374",
735 .cpu_ports = 0x30, /* can be configured as cpu port */
736 .port_cnt = 8, /* total physical port count */
737 .ops = &lan937x_dev_ops,
738 .mib_names = ksz9477_mib_names,
739 .mib_cnt = ARRAY_SIZE(ksz9477_mib_names),
740 .reg_mib_cnt = MIB_COUNTER_NUM,
741 .regs = ksz9477_regs,
742 .masks = lan937x_masks,
743 .shifts = lan937x_shifts,
744 .xmii_ctrl0 = ksz9477_xmii_ctrl0,
745 .xmii_ctrl1 = ksz9477_xmii_ctrl1,
746 .supports_mii = {false, false, false, false,
747 true, true, false, false},
748 .supports_rmii = {false, false, false, false,
749 true, true, false, false},
750 .supports_rgmii = {false, false, false, false,
751 true, true, false, false},
752 .internal_phy = {true, true, true, true,
753 false, false, true, true},
756 EXPORT_SYMBOL_GPL(ksz_switch_chips);
758 static const struct ksz_chip_data *ksz_lookup_info(unsigned int prod_num)
762 for (i = 0; i < ARRAY_SIZE(ksz_switch_chips); i++) {
763 const struct ksz_chip_data *chip = &ksz_switch_chips[i];
765 if (chip->chip_id == prod_num)
772 static int ksz_check_device_id(struct ksz_device *dev)
774 const struct ksz_chip_data *dt_chip_data;
776 dt_chip_data = of_device_get_match_data(dev->dev);
778 /* Check for Device Tree and Chip ID */
779 if (dt_chip_data->chip_id != dev->chip_id) {
781 "Device tree specifies chip %s but found %s, please fix it!\n",
782 dt_chip_data->dev_name, dev->info->dev_name);
789 static void ksz_phylink_get_caps(struct dsa_switch *ds, int port,
790 struct phylink_config *config)
792 struct ksz_device *dev = ds->priv;
794 config->legacy_pre_march2020 = false;
796 if (dev->info->supports_mii[port])
797 __set_bit(PHY_INTERFACE_MODE_MII, config->supported_interfaces);
799 if (dev->info->supports_rmii[port])
800 __set_bit(PHY_INTERFACE_MODE_RMII,
801 config->supported_interfaces);
803 if (dev->info->supports_rgmii[port])
804 phy_interface_set_rgmii(config->supported_interfaces);
806 if (dev->info->internal_phy[port])
807 __set_bit(PHY_INTERFACE_MODE_INTERNAL,
808 config->supported_interfaces);
810 if (dev->dev_ops->get_caps)
811 dev->dev_ops->get_caps(dev, port, config);
814 void ksz_r_mib_stats64(struct ksz_device *dev, int port)
816 struct ethtool_pause_stats *pstats;
817 struct rtnl_link_stats64 *stats;
818 struct ksz_stats_raw *raw;
819 struct ksz_port_mib *mib;
821 mib = &dev->ports[port].mib;
822 stats = &mib->stats64;
823 pstats = &mib->pause_stats;
824 raw = (struct ksz_stats_raw *)mib->counters;
826 spin_lock(&mib->stats64_lock);
828 stats->rx_packets = raw->rx_bcast + raw->rx_mcast + raw->rx_ucast +
830 stats->tx_packets = raw->tx_bcast + raw->tx_mcast + raw->tx_ucast +
833 /* HW counters are counting bytes + FCS which is not acceptable
834 * for rtnl_link_stats64 interface
836 stats->rx_bytes = raw->rx_total - stats->rx_packets * ETH_FCS_LEN;
837 stats->tx_bytes = raw->tx_total - stats->tx_packets * ETH_FCS_LEN;
839 stats->rx_length_errors = raw->rx_undersize + raw->rx_fragments +
842 stats->rx_crc_errors = raw->rx_crc_err;
843 stats->rx_frame_errors = raw->rx_align_err;
844 stats->rx_dropped = raw->rx_discards;
845 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
846 stats->rx_frame_errors + stats->rx_dropped;
848 stats->tx_window_errors = raw->tx_late_col;
849 stats->tx_fifo_errors = raw->tx_discards;
850 stats->tx_aborted_errors = raw->tx_exc_col;
851 stats->tx_errors = stats->tx_window_errors + stats->tx_fifo_errors +
852 stats->tx_aborted_errors;
854 stats->multicast = raw->rx_mcast;
855 stats->collisions = raw->tx_total_col;
857 pstats->tx_pause_frames = raw->tx_pause;
858 pstats->rx_pause_frames = raw->rx_pause;
860 spin_unlock(&mib->stats64_lock);
863 static void ksz_get_stats64(struct dsa_switch *ds, int port,
864 struct rtnl_link_stats64 *s)
866 struct ksz_device *dev = ds->priv;
867 struct ksz_port_mib *mib;
869 mib = &dev->ports[port].mib;
871 spin_lock(&mib->stats64_lock);
872 memcpy(s, &mib->stats64, sizeof(*s));
873 spin_unlock(&mib->stats64_lock);
876 static void ksz_get_pause_stats(struct dsa_switch *ds, int port,
877 struct ethtool_pause_stats *pause_stats)
879 struct ksz_device *dev = ds->priv;
880 struct ksz_port_mib *mib;
882 mib = &dev->ports[port].mib;
884 spin_lock(&mib->stats64_lock);
885 memcpy(pause_stats, &mib->pause_stats, sizeof(*pause_stats));
886 spin_unlock(&mib->stats64_lock);
889 static void ksz_get_strings(struct dsa_switch *ds, int port,
890 u32 stringset, uint8_t *buf)
892 struct ksz_device *dev = ds->priv;
895 if (stringset != ETH_SS_STATS)
898 for (i = 0; i < dev->info->mib_cnt; i++) {
899 memcpy(buf + i * ETH_GSTRING_LEN,
900 dev->info->mib_names[i].string, ETH_GSTRING_LEN);
904 static void ksz_update_port_member(struct ksz_device *dev, int port)
906 struct ksz_port *p = &dev->ports[port];
907 struct dsa_switch *ds = dev->ds;
908 u8 port_member = 0, cpu_port;
909 const struct dsa_port *dp;
912 if (!dsa_is_user_port(ds, port))
915 dp = dsa_to_port(ds, port);
916 cpu_port = BIT(dsa_upstream_port(ds, port));
918 for (i = 0; i < ds->num_ports; i++) {
919 const struct dsa_port *other_dp = dsa_to_port(ds, i);
920 struct ksz_port *other_p = &dev->ports[i];
923 if (!dsa_is_user_port(ds, i))
927 if (!dsa_port_bridge_same(dp, other_dp))
929 if (other_p->stp_state != BR_STATE_FORWARDING)
932 if (p->stp_state == BR_STATE_FORWARDING) {
934 port_member |= BIT(i);
937 /* Retain port [i]'s relationship to other ports than [port] */
938 for (j = 0; j < ds->num_ports; j++) {
939 const struct dsa_port *third_dp;
940 struct ksz_port *third_p;
946 if (!dsa_is_user_port(ds, j))
948 third_p = &dev->ports[j];
949 if (third_p->stp_state != BR_STATE_FORWARDING)
951 third_dp = dsa_to_port(ds, j);
952 if (dsa_port_bridge_same(other_dp, third_dp))
956 dev->dev_ops->cfg_port_member(dev, i, val | cpu_port);
959 dev->dev_ops->cfg_port_member(dev, port, port_member | cpu_port);
962 static int ksz_setup(struct dsa_switch *ds)
964 struct ksz_device *dev = ds->priv;
968 regs = dev->info->regs;
970 dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table),
971 dev->info->num_vlans, GFP_KERNEL);
972 if (!dev->vlan_cache)
975 ret = dev->dev_ops->reset(dev);
977 dev_err(ds->dev, "failed to reset switch\n");
981 /* set broadcast storm protection 10% rate */
982 regmap_update_bits(dev->regmap[1], regs[S_BROADCAST_CTRL],
983 BROADCAST_STORM_RATE,
984 (BROADCAST_STORM_VALUE *
985 BROADCAST_STORM_PROT_RATE) / 100);
987 dev->dev_ops->config_cpu_port(ds);
989 dev->dev_ops->enable_stp_addr(dev);
991 regmap_update_bits(dev->regmap[0], regs[S_MULTICAST_CTRL],
992 MULTICAST_STORM_DISABLE, MULTICAST_STORM_DISABLE);
994 ksz_init_mib_timer(dev);
996 ds->configure_vlan_while_not_filtering = false;
998 if (dev->dev_ops->setup) {
999 ret = dev->dev_ops->setup(ds);
1005 regmap_update_bits(dev->regmap[0], regs[S_START_CTRL],
1006 SW_START, SW_START);
1011 static void port_r_cnt(struct ksz_device *dev, int port)
1013 struct ksz_port_mib *mib = &dev->ports[port].mib;
1016 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
1017 while (mib->cnt_ptr < dev->info->reg_mib_cnt) {
1018 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
1019 &mib->counters[mib->cnt_ptr]);
1023 /* last one in storage */
1024 dropped = &mib->counters[dev->info->mib_cnt];
1026 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
1027 while (mib->cnt_ptr < dev->info->mib_cnt) {
1028 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
1029 dropped, &mib->counters[mib->cnt_ptr]);
1035 static void ksz_mib_read_work(struct work_struct *work)
1037 struct ksz_device *dev = container_of(work, struct ksz_device,
1039 struct ksz_port_mib *mib;
1043 for (i = 0; i < dev->info->port_cnt; i++) {
1044 if (dsa_is_unused_port(dev->ds, i))
1049 mutex_lock(&mib->cnt_mutex);
1051 /* Only read MIB counters when the port is told to do.
1052 * If not, read only dropped counters when link is not up.
1055 const struct dsa_port *dp = dsa_to_port(dev->ds, i);
1057 if (!netif_carrier_ok(dp->slave))
1058 mib->cnt_ptr = dev->info->reg_mib_cnt;
1063 if (dev->dev_ops->r_mib_stat64)
1064 dev->dev_ops->r_mib_stat64(dev, i);
1066 mutex_unlock(&mib->cnt_mutex);
1069 schedule_delayed_work(&dev->mib_read, dev->mib_read_interval);
1072 void ksz_init_mib_timer(struct ksz_device *dev)
1076 INIT_DELAYED_WORK(&dev->mib_read, ksz_mib_read_work);
1078 for (i = 0; i < dev->info->port_cnt; i++) {
1079 struct ksz_port_mib *mib = &dev->ports[i].mib;
1081 dev->dev_ops->port_init_cnt(dev, i);
1084 memset(mib->counters, 0, dev->info->mib_cnt * sizeof(u64));
1088 static int ksz_phy_read16(struct dsa_switch *ds, int addr, int reg)
1090 struct ksz_device *dev = ds->priv;
1093 dev->dev_ops->r_phy(dev, addr, reg, &val);
1098 static int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val)
1100 struct ksz_device *dev = ds->priv;
1102 dev->dev_ops->w_phy(dev, addr, reg, val);
1107 static u32 ksz_get_phy_flags(struct dsa_switch *ds, int port)
1109 struct ksz_device *dev = ds->priv;
1111 if (dev->chip_id == KSZ8830_CHIP_ID) {
1112 /* Silicon Errata Sheet (DS80000830A):
1113 * Port 1 does not work with LinkMD Cable-Testing.
1114 * Port 1 does not respond to received PAUSE control frames.
1117 return MICREL_KSZ8_P1_ERRATA;
1123 static void ksz_mac_link_down(struct dsa_switch *ds, int port,
1124 unsigned int mode, phy_interface_t interface)
1126 struct ksz_device *dev = ds->priv;
1127 struct ksz_port *p = &dev->ports[port];
1129 /* Read all MIB counters when the link is going down. */
1132 if (dev->mib_read_interval)
1133 schedule_delayed_work(&dev->mib_read, 0);
1136 static int ksz_sset_count(struct dsa_switch *ds, int port, int sset)
1138 struct ksz_device *dev = ds->priv;
1140 if (sset != ETH_SS_STATS)
1143 return dev->info->mib_cnt;
1146 static void ksz_get_ethtool_stats(struct dsa_switch *ds, int port,
1149 const struct dsa_port *dp = dsa_to_port(ds, port);
1150 struct ksz_device *dev = ds->priv;
1151 struct ksz_port_mib *mib;
1153 mib = &dev->ports[port].mib;
1154 mutex_lock(&mib->cnt_mutex);
1156 /* Only read dropped counters if no link. */
1157 if (!netif_carrier_ok(dp->slave))
1158 mib->cnt_ptr = dev->info->reg_mib_cnt;
1159 port_r_cnt(dev, port);
1160 memcpy(buf, mib->counters, dev->info->mib_cnt * sizeof(u64));
1161 mutex_unlock(&mib->cnt_mutex);
1164 static int ksz_port_bridge_join(struct dsa_switch *ds, int port,
1165 struct dsa_bridge bridge,
1166 bool *tx_fwd_offload,
1167 struct netlink_ext_ack *extack)
1169 /* port_stp_state_set() will be called after to put the port in
1170 * appropriate state so there is no need to do anything.
1176 static void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
1177 struct dsa_bridge bridge)
1179 /* port_stp_state_set() will be called after to put the port in
1180 * forwarding state so there is no need to do anything.
1184 static void ksz_port_fast_age(struct dsa_switch *ds, int port)
1186 struct ksz_device *dev = ds->priv;
1188 dev->dev_ops->flush_dyn_mac_table(dev, port);
1191 static int ksz_port_fdb_add(struct dsa_switch *ds, int port,
1192 const unsigned char *addr, u16 vid,
1195 struct ksz_device *dev = ds->priv;
1197 if (!dev->dev_ops->fdb_add)
1200 return dev->dev_ops->fdb_add(dev, port, addr, vid, db);
1203 static int ksz_port_fdb_del(struct dsa_switch *ds, int port,
1204 const unsigned char *addr,
1205 u16 vid, struct dsa_db db)
1207 struct ksz_device *dev = ds->priv;
1209 if (!dev->dev_ops->fdb_del)
1212 return dev->dev_ops->fdb_del(dev, port, addr, vid, db);
1215 static int ksz_port_fdb_dump(struct dsa_switch *ds, int port,
1216 dsa_fdb_dump_cb_t *cb, void *data)
1218 struct ksz_device *dev = ds->priv;
1220 if (!dev->dev_ops->fdb_dump)
1223 return dev->dev_ops->fdb_dump(dev, port, cb, data);
1226 static int ksz_port_mdb_add(struct dsa_switch *ds, int port,
1227 const struct switchdev_obj_port_mdb *mdb,
1230 struct ksz_device *dev = ds->priv;
1232 if (!dev->dev_ops->mdb_add)
1235 return dev->dev_ops->mdb_add(dev, port, mdb, db);
1238 static int ksz_port_mdb_del(struct dsa_switch *ds, int port,
1239 const struct switchdev_obj_port_mdb *mdb,
1242 struct ksz_device *dev = ds->priv;
1244 if (!dev->dev_ops->mdb_del)
1247 return dev->dev_ops->mdb_del(dev, port, mdb, db);
1250 static int ksz_enable_port(struct dsa_switch *ds, int port,
1251 struct phy_device *phy)
1253 struct ksz_device *dev = ds->priv;
1255 if (!dsa_is_user_port(ds, port))
1258 /* setup slave port */
1259 dev->dev_ops->port_setup(dev, port, false);
1261 /* port_stp_state_set() will be called after to enable the port so
1262 * there is no need to do anything.
1268 void ksz_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1270 struct ksz_device *dev = ds->priv;
1275 regs = dev->info->regs;
1277 ksz_pread8(dev, port, regs[P_STP_CTRL], &data);
1278 data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
1281 case BR_STATE_DISABLED:
1282 data |= PORT_LEARN_DISABLE;
1284 case BR_STATE_LISTENING:
1285 data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE);
1287 case BR_STATE_LEARNING:
1288 data |= PORT_RX_ENABLE;
1290 case BR_STATE_FORWARDING:
1291 data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
1293 case BR_STATE_BLOCKING:
1294 data |= PORT_LEARN_DISABLE;
1297 dev_err(ds->dev, "invalid STP state: %d\n", state);
1301 ksz_pwrite8(dev, port, regs[P_STP_CTRL], data);
1303 p = &dev->ports[port];
1304 p->stp_state = state;
1306 ksz_update_port_member(dev, port);
1309 static enum dsa_tag_protocol ksz_get_tag_protocol(struct dsa_switch *ds,
1311 enum dsa_tag_protocol mp)
1313 struct ksz_device *dev = ds->priv;
1314 enum dsa_tag_protocol proto = DSA_TAG_PROTO_NONE;
1316 if (dev->chip_id == KSZ8795_CHIP_ID ||
1317 dev->chip_id == KSZ8794_CHIP_ID ||
1318 dev->chip_id == KSZ8765_CHIP_ID)
1319 proto = DSA_TAG_PROTO_KSZ8795;
1321 if (dev->chip_id == KSZ8830_CHIP_ID ||
1322 dev->chip_id == KSZ9893_CHIP_ID)
1323 proto = DSA_TAG_PROTO_KSZ9893;
1325 if (dev->chip_id == KSZ9477_CHIP_ID ||
1326 dev->chip_id == KSZ9897_CHIP_ID ||
1327 dev->chip_id == KSZ9567_CHIP_ID)
1328 proto = DSA_TAG_PROTO_KSZ9477;
1330 if (is_lan937x(dev))
1331 proto = DSA_TAG_PROTO_LAN937X_VALUE;
1336 static int ksz_port_vlan_filtering(struct dsa_switch *ds, int port,
1337 bool flag, struct netlink_ext_ack *extack)
1339 struct ksz_device *dev = ds->priv;
1341 if (!dev->dev_ops->vlan_filtering)
1344 return dev->dev_ops->vlan_filtering(dev, port, flag, extack);
1347 static int ksz_port_vlan_add(struct dsa_switch *ds, int port,
1348 const struct switchdev_obj_port_vlan *vlan,
1349 struct netlink_ext_ack *extack)
1351 struct ksz_device *dev = ds->priv;
1353 if (!dev->dev_ops->vlan_add)
1356 return dev->dev_ops->vlan_add(dev, port, vlan, extack);
1359 static int ksz_port_vlan_del(struct dsa_switch *ds, int port,
1360 const struct switchdev_obj_port_vlan *vlan)
1362 struct ksz_device *dev = ds->priv;
1364 if (!dev->dev_ops->vlan_del)
1367 return dev->dev_ops->vlan_del(dev, port, vlan);
1370 static int ksz_port_mirror_add(struct dsa_switch *ds, int port,
1371 struct dsa_mall_mirror_tc_entry *mirror,
1372 bool ingress, struct netlink_ext_ack *extack)
1374 struct ksz_device *dev = ds->priv;
1376 if (!dev->dev_ops->mirror_add)
1379 return dev->dev_ops->mirror_add(dev, port, mirror, ingress, extack);
1382 static void ksz_port_mirror_del(struct dsa_switch *ds, int port,
1383 struct dsa_mall_mirror_tc_entry *mirror)
1385 struct ksz_device *dev = ds->priv;
1387 if (dev->dev_ops->mirror_del)
1388 dev->dev_ops->mirror_del(dev, port, mirror);
1391 static int ksz_change_mtu(struct dsa_switch *ds, int port, int mtu)
1393 struct ksz_device *dev = ds->priv;
1395 if (!dev->dev_ops->change_mtu)
1398 return dev->dev_ops->change_mtu(dev, port, mtu);
1401 static int ksz_max_mtu(struct dsa_switch *ds, int port)
1403 struct ksz_device *dev = ds->priv;
1405 if (!dev->dev_ops->max_mtu)
1408 return dev->dev_ops->max_mtu(dev, port);
1411 static void ksz_set_xmii(struct ksz_device *dev, int port,
1412 phy_interface_t interface)
1414 const u8 *bitval = dev->info->xmii_ctrl1;
1415 struct ksz_port *p = &dev->ports[port];
1416 const u16 *regs = dev->info->regs;
1419 ksz_pread8(dev, port, regs[P_XMII_CTRL_1], &data8);
1421 data8 &= ~(P_MII_SEL_M | P_RGMII_ID_IG_ENABLE |
1422 P_RGMII_ID_EG_ENABLE);
1424 switch (interface) {
1425 case PHY_INTERFACE_MODE_MII:
1426 data8 |= bitval[P_MII_SEL];
1428 case PHY_INTERFACE_MODE_RMII:
1429 data8 |= bitval[P_RMII_SEL];
1431 case PHY_INTERFACE_MODE_GMII:
1432 data8 |= bitval[P_GMII_SEL];
1434 case PHY_INTERFACE_MODE_RGMII:
1435 case PHY_INTERFACE_MODE_RGMII_ID:
1436 case PHY_INTERFACE_MODE_RGMII_TXID:
1437 case PHY_INTERFACE_MODE_RGMII_RXID:
1438 data8 |= bitval[P_RGMII_SEL];
1439 /* On KSZ9893, disable RGMII in-band status support */
1440 if (dev->features & IS_9893)
1441 data8 &= ~P_MII_MAC_MODE;
1444 dev_err(dev->dev, "Unsupported interface '%s' for port %d\n",
1445 phy_modes(interface), port);
1449 if (p->rgmii_tx_val)
1450 data8 |= P_RGMII_ID_EG_ENABLE;
1452 if (p->rgmii_rx_val)
1453 data8 |= P_RGMII_ID_IG_ENABLE;
1455 /* Write the updated value */
1456 ksz_pwrite8(dev, port, regs[P_XMII_CTRL_1], data8);
1459 phy_interface_t ksz_get_xmii(struct ksz_device *dev, int port, bool gbit)
1461 const u8 *bitval = dev->info->xmii_ctrl1;
1462 const u16 *regs = dev->info->regs;
1463 phy_interface_t interface;
1467 ksz_pread8(dev, port, regs[P_XMII_CTRL_1], &data8);
1469 val = FIELD_GET(P_MII_SEL_M, data8);
1471 if (val == bitval[P_MII_SEL]) {
1473 interface = PHY_INTERFACE_MODE_GMII;
1475 interface = PHY_INTERFACE_MODE_MII;
1476 } else if (val == bitval[P_RMII_SEL]) {
1477 interface = PHY_INTERFACE_MODE_RGMII;
1479 interface = PHY_INTERFACE_MODE_RGMII;
1480 if (data8 & P_RGMII_ID_EG_ENABLE)
1481 interface = PHY_INTERFACE_MODE_RGMII_TXID;
1482 if (data8 & P_RGMII_ID_IG_ENABLE) {
1483 interface = PHY_INTERFACE_MODE_RGMII_RXID;
1484 if (data8 & P_RGMII_ID_EG_ENABLE)
1485 interface = PHY_INTERFACE_MODE_RGMII_ID;
1492 static void ksz_phylink_mac_config(struct dsa_switch *ds, int port,
1494 const struct phylink_link_state *state)
1496 struct ksz_device *dev = ds->priv;
1498 if (ksz_is_ksz88x3(dev))
1502 if (dev->info->internal_phy[port])
1505 if (phylink_autoneg_inband(mode)) {
1506 dev_err(dev->dev, "In-band AN not supported!\n");
1510 ksz_set_xmii(dev, port, state->interface);
1512 if (dev->dev_ops->phylink_mac_config)
1513 dev->dev_ops->phylink_mac_config(dev, port, mode, state);
1515 if (dev->dev_ops->setup_rgmii_delay)
1516 dev->dev_ops->setup_rgmii_delay(dev, port);
1519 bool ksz_get_gbit(struct ksz_device *dev, int port)
1521 const u8 *bitval = dev->info->xmii_ctrl1;
1522 const u16 *regs = dev->info->regs;
1527 ksz_pread8(dev, port, regs[P_XMII_CTRL_1], &data8);
1529 val = FIELD_GET(P_GMII_1GBIT_M, data8);
1531 if (val == bitval[P_GMII_1GBIT])
1537 static void ksz_set_gbit(struct ksz_device *dev, int port, bool gbit)
1539 const u8 *bitval = dev->info->xmii_ctrl1;
1540 const u16 *regs = dev->info->regs;
1543 ksz_pread8(dev, port, regs[P_XMII_CTRL_1], &data8);
1545 data8 &= ~P_GMII_1GBIT_M;
1548 data8 |= FIELD_PREP(P_GMII_1GBIT_M, bitval[P_GMII_1GBIT]);
1550 data8 |= FIELD_PREP(P_GMII_1GBIT_M, bitval[P_GMII_NOT_1GBIT]);
1552 /* Write the updated value */
1553 ksz_pwrite8(dev, port, regs[P_XMII_CTRL_1], data8);
1556 static void ksz_set_100_10mbit(struct ksz_device *dev, int port, int speed)
1558 const u8 *bitval = dev->info->xmii_ctrl0;
1559 const u16 *regs = dev->info->regs;
1562 ksz_pread8(dev, port, regs[P_XMII_CTRL_0], &data8);
1564 data8 &= ~P_MII_100MBIT_M;
1566 if (speed == SPEED_100)
1567 data8 |= FIELD_PREP(P_MII_100MBIT_M, bitval[P_MII_100MBIT]);
1569 data8 |= FIELD_PREP(P_MII_100MBIT_M, bitval[P_MII_10MBIT]);
1571 /* Write the updated value */
1572 ksz_pwrite8(dev, port, regs[P_XMII_CTRL_0], data8);
1575 static void ksz_port_set_xmii_speed(struct ksz_device *dev, int port, int speed)
1577 if (speed == SPEED_1000)
1578 ksz_set_gbit(dev, port, true);
1580 ksz_set_gbit(dev, port, false);
1582 if (speed == SPEED_100 || speed == SPEED_10)
1583 ksz_set_100_10mbit(dev, port, speed);
1586 static void ksz_duplex_flowctrl(struct ksz_device *dev, int port, int duplex,
1587 bool tx_pause, bool rx_pause)
1589 const u8 *bitval = dev->info->xmii_ctrl0;
1590 const u32 *masks = dev->info->masks;
1591 const u16 *regs = dev->info->regs;
1595 mask = P_MII_DUPLEX_M | masks[P_MII_TX_FLOW_CTRL] |
1596 masks[P_MII_RX_FLOW_CTRL];
1598 if (duplex == DUPLEX_FULL)
1599 val = FIELD_PREP(P_MII_DUPLEX_M, bitval[P_MII_FULL_DUPLEX]);
1601 val = FIELD_PREP(P_MII_DUPLEX_M, bitval[P_MII_HALF_DUPLEX]);
1604 val |= masks[P_MII_TX_FLOW_CTRL];
1607 val |= masks[P_MII_RX_FLOW_CTRL];
1609 ksz_prmw8(dev, port, regs[P_XMII_CTRL_0], mask, val);
1612 static void ksz_phylink_mac_link_up(struct dsa_switch *ds, int port,
1614 phy_interface_t interface,
1615 struct phy_device *phydev, int speed,
1616 int duplex, bool tx_pause, bool rx_pause)
1618 struct ksz_device *dev = ds->priv;
1621 p = &dev->ports[port];
1624 if (dev->info->internal_phy[port])
1627 p->phydev.speed = speed;
1629 ksz_port_set_xmii_speed(dev, port, speed);
1631 ksz_duplex_flowctrl(dev, port, duplex, tx_pause, rx_pause);
1633 if (dev->dev_ops->phylink_mac_link_up)
1634 dev->dev_ops->phylink_mac_link_up(dev, port, mode, interface,
1635 phydev, speed, duplex,
1636 tx_pause, rx_pause);
1639 static int ksz_switch_detect(struct ksz_device *dev)
1647 ret = ksz_read16(dev, REG_CHIP_ID0, &id16);
1651 id1 = FIELD_GET(SW_FAMILY_ID_M, id16);
1652 id2 = FIELD_GET(SW_CHIP_ID_M, id16);
1655 case KSZ87_FAMILY_ID:
1656 if (id2 == KSZ87_CHIP_ID_95) {
1659 dev->chip_id = KSZ8795_CHIP_ID;
1661 ksz_read8(dev, KSZ8_PORT_STATUS_0, &val);
1662 if (val & KSZ8_PORT_FIBER_MODE)
1663 dev->chip_id = KSZ8765_CHIP_ID;
1664 } else if (id2 == KSZ87_CHIP_ID_94) {
1665 dev->chip_id = KSZ8794_CHIP_ID;
1670 case KSZ88_FAMILY_ID:
1671 if (id2 == KSZ88_CHIP_ID_63)
1672 dev->chip_id = KSZ8830_CHIP_ID;
1677 ret = ksz_read32(dev, REG_CHIP_ID0, &id32);
1681 dev->chip_rev = FIELD_GET(SW_REV_ID_M, id32);
1685 case KSZ9477_CHIP_ID:
1686 case KSZ9897_CHIP_ID:
1687 case KSZ9893_CHIP_ID:
1688 case KSZ9567_CHIP_ID:
1689 case LAN9370_CHIP_ID:
1690 case LAN9371_CHIP_ID:
1691 case LAN9372_CHIP_ID:
1692 case LAN9373_CHIP_ID:
1693 case LAN9374_CHIP_ID:
1694 dev->chip_id = id32;
1698 "unsupported switch detected %x)\n", id32);
1705 static const struct dsa_switch_ops ksz_switch_ops = {
1706 .get_tag_protocol = ksz_get_tag_protocol,
1707 .get_phy_flags = ksz_get_phy_flags,
1709 .phy_read = ksz_phy_read16,
1710 .phy_write = ksz_phy_write16,
1711 .phylink_get_caps = ksz_phylink_get_caps,
1712 .phylink_mac_config = ksz_phylink_mac_config,
1713 .phylink_mac_link_up = ksz_phylink_mac_link_up,
1714 .phylink_mac_link_down = ksz_mac_link_down,
1715 .port_enable = ksz_enable_port,
1716 .get_strings = ksz_get_strings,
1717 .get_ethtool_stats = ksz_get_ethtool_stats,
1718 .get_sset_count = ksz_sset_count,
1719 .port_bridge_join = ksz_port_bridge_join,
1720 .port_bridge_leave = ksz_port_bridge_leave,
1721 .port_stp_state_set = ksz_port_stp_state_set,
1722 .port_fast_age = ksz_port_fast_age,
1723 .port_vlan_filtering = ksz_port_vlan_filtering,
1724 .port_vlan_add = ksz_port_vlan_add,
1725 .port_vlan_del = ksz_port_vlan_del,
1726 .port_fdb_dump = ksz_port_fdb_dump,
1727 .port_fdb_add = ksz_port_fdb_add,
1728 .port_fdb_del = ksz_port_fdb_del,
1729 .port_mdb_add = ksz_port_mdb_add,
1730 .port_mdb_del = ksz_port_mdb_del,
1731 .port_mirror_add = ksz_port_mirror_add,
1732 .port_mirror_del = ksz_port_mirror_del,
1733 .get_stats64 = ksz_get_stats64,
1734 .get_pause_stats = ksz_get_pause_stats,
1735 .port_change_mtu = ksz_change_mtu,
1736 .port_max_mtu = ksz_max_mtu,
1739 struct ksz_device *ksz_switch_alloc(struct device *base, void *priv)
1741 struct dsa_switch *ds;
1742 struct ksz_device *swdev;
1744 ds = devm_kzalloc(base, sizeof(*ds), GFP_KERNEL);
1749 ds->num_ports = DSA_MAX_PORTS;
1750 ds->ops = &ksz_switch_ops;
1752 swdev = devm_kzalloc(base, sizeof(*swdev), GFP_KERNEL);
1764 EXPORT_SYMBOL(ksz_switch_alloc);
1766 static void ksz_parse_rgmii_delay(struct ksz_device *dev, int port_num,
1767 struct device_node *port_dn)
1769 phy_interface_t phy_mode = dev->ports[port_num].interface;
1770 int rx_delay = -1, tx_delay = -1;
1772 if (!phy_interface_mode_is_rgmii(phy_mode))
1775 of_property_read_u32(port_dn, "rx-internal-delay-ps", &rx_delay);
1776 of_property_read_u32(port_dn, "tx-internal-delay-ps", &tx_delay);
1778 if (rx_delay == -1 && tx_delay == -1) {
1780 "Port %d interpreting RGMII delay settings based on \"phy-mode\" property, "
1781 "please update device tree to specify \"rx-internal-delay-ps\" and "
1782 "\"tx-internal-delay-ps\"",
1785 if (phy_mode == PHY_INTERFACE_MODE_RGMII_RXID ||
1786 phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
1789 if (phy_mode == PHY_INTERFACE_MODE_RGMII_TXID ||
1790 phy_mode == PHY_INTERFACE_MODE_RGMII_ID)
1799 dev->ports[port_num].rgmii_rx_val = rx_delay;
1800 dev->ports[port_num].rgmii_tx_val = tx_delay;
1803 int ksz_switch_register(struct ksz_device *dev)
1805 const struct ksz_chip_data *info;
1806 struct device_node *port, *ports;
1807 phy_interface_t interface;
1808 unsigned int port_num;
1813 dev->chip_id = dev->pdata->chip_id;
1815 dev->reset_gpio = devm_gpiod_get_optional(dev->dev, "reset",
1817 if (IS_ERR(dev->reset_gpio))
1818 return PTR_ERR(dev->reset_gpio);
1820 if (dev->reset_gpio) {
1821 gpiod_set_value_cansleep(dev->reset_gpio, 1);
1822 usleep_range(10000, 12000);
1823 gpiod_set_value_cansleep(dev->reset_gpio, 0);
1827 mutex_init(&dev->dev_mutex);
1828 mutex_init(&dev->regmap_mutex);
1829 mutex_init(&dev->alu_mutex);
1830 mutex_init(&dev->vlan_mutex);
1832 ret = ksz_switch_detect(dev);
1836 info = ksz_lookup_info(dev->chip_id);
1840 /* Update the compatible info with the probed one */
1843 dev_info(dev->dev, "found switch: %s, rev %i\n",
1844 dev->info->dev_name, dev->chip_rev);
1846 ret = ksz_check_device_id(dev);
1850 dev->dev_ops = dev->info->ops;
1852 ret = dev->dev_ops->init(dev);
1856 dev->ports = devm_kzalloc(dev->dev,
1857 dev->info->port_cnt * sizeof(struct ksz_port),
1862 for (i = 0; i < dev->info->port_cnt; i++) {
1863 spin_lock_init(&dev->ports[i].mib.stats64_lock);
1864 mutex_init(&dev->ports[i].mib.cnt_mutex);
1865 dev->ports[i].mib.counters =
1866 devm_kzalloc(dev->dev,
1867 sizeof(u64) * (dev->info->mib_cnt + 1),
1869 if (!dev->ports[i].mib.counters)
1873 /* set the real number of ports */
1874 dev->ds->num_ports = dev->info->port_cnt;
1876 /* Host port interface will be self detected, or specifically set in
1879 for (port_num = 0; port_num < dev->info->port_cnt; ++port_num)
1880 dev->ports[port_num].interface = PHY_INTERFACE_MODE_NA;
1881 if (dev->dev->of_node) {
1882 ret = of_get_phy_mode(dev->dev->of_node, &interface);
1884 dev->compat_interface = interface;
1885 ports = of_get_child_by_name(dev->dev->of_node, "ethernet-ports");
1887 ports = of_get_child_by_name(dev->dev->of_node, "ports");
1889 for_each_available_child_of_node(ports, port) {
1890 if (of_property_read_u32(port, "reg",
1893 if (!(dev->port_mask & BIT(port_num))) {
1898 of_get_phy_mode(port,
1899 &dev->ports[port_num].interface);
1901 ksz_parse_rgmii_delay(dev, port_num, port);
1905 dev->synclko_125 = of_property_read_bool(dev->dev->of_node,
1906 "microchip,synclko-125");
1907 dev->synclko_disable = of_property_read_bool(dev->dev->of_node,
1908 "microchip,synclko-disable");
1909 if (dev->synclko_125 && dev->synclko_disable) {
1910 dev_err(dev->dev, "inconsistent synclko settings\n");
1915 ret = dsa_register_switch(dev->ds);
1917 dev->dev_ops->exit(dev);
1921 /* Read MIB counters every 30 seconds to avoid overflow. */
1922 dev->mib_read_interval = msecs_to_jiffies(5000);
1924 /* Start the MIB timer. */
1925 schedule_delayed_work(&dev->mib_read, 0);
1929 EXPORT_SYMBOL(ksz_switch_register);
1931 void ksz_switch_remove(struct ksz_device *dev)
1934 if (dev->mib_read_interval) {
1935 dev->mib_read_interval = 0;
1936 cancel_delayed_work_sync(&dev->mib_read);
1939 dev->dev_ops->exit(dev);
1940 dsa_unregister_switch(dev->ds);
1942 if (dev->reset_gpio)
1943 gpiod_set_value_cansleep(dev->reset_gpio, 1);
1946 EXPORT_SYMBOL(ksz_switch_remove);
1949 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver");
1950 MODULE_LICENSE("GPL");