1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip KSZ8795 switch driver
5 * Copyright (C) 2017 Microchip Technology Inc.
6 * Tristram Ha <Tristram.Ha@microchip.com>
9 #include <linux/bitfield.h>
10 #include <linux/delay.h>
11 #include <linux/export.h>
12 #include <linux/gpio.h>
13 #include <linux/if_vlan.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/platform_data/microchip-ksz.h>
17 #include <linux/phy.h>
18 #include <linux/etherdevice.h>
19 #include <linux/if_bridge.h>
20 #include <linux/micrel_phy.h>
22 #include <net/switchdev.h>
23 #include <linux/phylink.h>
25 #include "ksz_common.h"
26 #include "ksz8795_reg.h"
29 static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
31 regmap_update_bits(ksz_regmap_8(dev), addr, bits, set ? bits : 0);
34 static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
37 regmap_update_bits(ksz_regmap_8(dev), PORT_CTRL_ADDR(port, offset),
38 bits, set ? bits : 0);
41 static int ksz8_ind_write8(struct ksz_device *dev, u8 table, u16 addr, u8 data)
47 regs = dev->info->regs;
49 mutex_lock(&dev->alu_mutex);
51 ctrl_addr = IND_ACC_TABLE(table) | addr;
52 ret = ksz_write8(dev, regs[REG_IND_BYTE], data);
54 ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
56 mutex_unlock(&dev->alu_mutex);
61 int ksz8_reset_switch(struct ksz_device *dev)
63 if (ksz_is_ksz88x3(dev)) {
65 ksz_cfg(dev, KSZ8863_REG_SW_RESET,
66 KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, true);
67 ksz_cfg(dev, KSZ8863_REG_SW_RESET,
68 KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, false);
71 ksz_write8(dev, REG_POWER_MANAGEMENT_1,
72 SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S);
73 ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0);
79 static int ksz8863_change_mtu(struct ksz_device *dev, int frame_size)
83 if (frame_size <= KSZ8_LEGAL_PACKET_SIZE)
84 ctrl2 |= KSZ8863_LEGAL_PACKET_ENABLE;
85 else if (frame_size > KSZ8863_NORMAL_PACKET_SIZE)
86 ctrl2 |= KSZ8863_HUGE_PACKET_ENABLE;
88 return ksz_rmw8(dev, REG_SW_CTRL_2, KSZ8863_LEGAL_PACKET_ENABLE |
89 KSZ8863_HUGE_PACKET_ENABLE, ctrl2);
92 static int ksz8795_change_mtu(struct ksz_device *dev, int frame_size)
94 u8 ctrl1 = 0, ctrl2 = 0;
97 if (frame_size > KSZ8_LEGAL_PACKET_SIZE)
98 ctrl2 |= SW_LEGAL_PACKET_DISABLE;
99 if (frame_size > KSZ8863_NORMAL_PACKET_SIZE)
100 ctrl1 |= SW_HUGE_PACKET;
102 ret = ksz_rmw8(dev, REG_SW_CTRL_1, SW_HUGE_PACKET, ctrl1);
106 return ksz_rmw8(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, ctrl2);
109 int ksz8_change_mtu(struct ksz_device *dev, int port, int mtu)
113 if (!dsa_is_cpu_port(dev->ds, port))
116 frame_size = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
118 switch (dev->chip_id) {
119 case KSZ8795_CHIP_ID:
120 case KSZ8794_CHIP_ID:
121 case KSZ8765_CHIP_ID:
122 return ksz8795_change_mtu(dev, frame_size);
123 case KSZ8830_CHIP_ID:
124 return ksz8863_change_mtu(dev, frame_size);
130 static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue)
134 /* Number of queues can only be 1, 2, or 4. */
138 queue = PORT_QUEUE_SPLIT_4;
141 queue = PORT_QUEUE_SPLIT_2;
144 queue = PORT_QUEUE_SPLIT_1;
146 ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo);
147 ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi);
148 lo &= ~PORT_QUEUE_SPLIT_L;
149 if (queue & PORT_QUEUE_SPLIT_2)
150 lo |= PORT_QUEUE_SPLIT_L;
151 hi &= ~PORT_QUEUE_SPLIT_H;
152 if (queue & PORT_QUEUE_SPLIT_4)
153 hi |= PORT_QUEUE_SPLIT_H;
154 ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo);
155 ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi);
157 /* Default is port based for egress rate limit. */
158 if (queue != PORT_QUEUE_SPLIT_1)
159 ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED,
163 void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt)
172 masks = dev->info->masks;
173 regs = dev->info->regs;
175 ctrl_addr = addr + dev->info->reg_mib_cnt * port;
176 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
178 mutex_lock(&dev->alu_mutex);
179 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
181 /* It is almost guaranteed to always read the valid bit because of
184 for (loop = 2; loop > 0; loop--) {
185 ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
187 if (check & masks[MIB_COUNTER_VALID]) {
188 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
189 if (check & masks[MIB_COUNTER_OVERFLOW])
190 *cnt += MIB_COUNTER_VALUE + 1;
191 *cnt += data & MIB_COUNTER_VALUE;
195 mutex_unlock(&dev->alu_mutex);
198 static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
199 u64 *dropped, u64 *cnt)
208 masks = dev->info->masks;
209 regs = dev->info->regs;
211 addr -= dev->info->reg_mib_cnt;
212 ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port;
213 ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0;
214 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
216 mutex_lock(&dev->alu_mutex);
217 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
219 /* It is almost guaranteed to always read the valid bit because of
222 for (loop = 2; loop > 0; loop--) {
223 ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
225 if (check & masks[MIB_COUNTER_VALID]) {
226 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
230 total = check & MIB_TOTAL_BYTES_H;
234 if (check & masks[MIB_COUNTER_OVERFLOW]) {
235 total = MIB_TOTAL_BYTES_H + 1;
240 if (check & masks[MIB_COUNTER_OVERFLOW])
241 *cnt += MIB_PACKET_DROPPED + 1;
242 *cnt += data & MIB_PACKET_DROPPED;
247 mutex_unlock(&dev->alu_mutex);
250 static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
251 u64 *dropped, u64 *cnt)
253 u32 *last = (u32 *)dropped;
259 regs = dev->info->regs;
261 addr -= dev->info->reg_mib_cnt;
262 ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 :
263 KSZ8863_MIB_PACKET_DROPPED_RX_0;
265 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
267 mutex_lock(&dev->alu_mutex);
268 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
269 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
270 mutex_unlock(&dev->alu_mutex);
272 data &= MIB_PACKET_DROPPED;
277 data += MIB_PACKET_DROPPED + 1;
283 void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
284 u64 *dropped, u64 *cnt)
286 if (ksz_is_ksz88x3(dev))
287 ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt);
289 ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt);
292 void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze)
294 if (ksz_is_ksz88x3(dev))
297 /* enable the port for flush/freeze function */
299 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
300 ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze);
302 /* disable the port after freeze is done */
304 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
307 void ksz8_port_init_cnt(struct ksz_device *dev, int port)
309 struct ksz_port_mib *mib = &dev->ports[port].mib;
312 if (!ksz_is_ksz88x3(dev)) {
313 /* flush all enabled port MIB counters */
314 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
315 ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true);
316 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
321 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
322 while (mib->cnt_ptr < dev->info->reg_mib_cnt) {
323 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
324 &mib->counters[mib->cnt_ptr]);
328 /* last one in storage */
329 dropped = &mib->counters[dev->info->mib_cnt];
331 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
332 while (mib->cnt_ptr < dev->info->mib_cnt) {
333 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
334 dropped, &mib->counters[mib->cnt_ptr]);
339 static int ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data)
345 regs = dev->info->regs;
347 ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
349 mutex_lock(&dev->alu_mutex);
350 ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
354 ret = ksz_read64(dev, regs[REG_IND_DATA_HI], data);
356 mutex_unlock(&dev->alu_mutex);
361 static int ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data)
367 regs = dev->info->regs;
369 ctrl_addr = IND_ACC_TABLE(table) | addr;
371 mutex_lock(&dev->alu_mutex);
372 ret = ksz_write64(dev, regs[REG_IND_DATA_HI], data);
376 ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
378 mutex_unlock(&dev->alu_mutex);
383 static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data)
389 masks = dev->info->masks;
390 regs = dev->info->regs;
393 ksz_read8(dev, regs[REG_IND_DATA_CHECK], data);
395 } while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout);
397 /* Entry is not ready for accessing. */
398 if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) {
400 /* Entry is ready for accessing. */
402 ksz_read8(dev, regs[REG_IND_DATA_8], data);
404 /* There is no valid entry in the table. */
405 if (*data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY])
411 int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, u8 *mac_addr,
412 u8 *fid, u8 *src_port, u8 *timestamp, u16 *entries)
414 u32 data_hi, data_lo;
422 shifts = dev->info->shifts;
423 masks = dev->info->masks;
424 regs = dev->info->regs;
426 ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr;
428 mutex_lock(&dev->alu_mutex);
429 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
431 rc = ksz8_valid_dyn_entry(dev, &data);
435 } else if (rc == -ENXIO) {
437 /* At least one valid entry in the table. */
442 ksz_read64(dev, regs[REG_IND_DATA_HI], &buf);
443 data_hi = (u32)(buf >> 32);
446 /* Check out how many valid entry in the table. */
447 cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H];
448 cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H];
449 cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >>
450 shifts[DYNAMIC_MAC_ENTRIES];
453 *fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >>
454 shifts[DYNAMIC_MAC_FID];
455 *src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >>
456 shifts[DYNAMIC_MAC_SRC_PORT];
457 *timestamp = (data_hi & masks[DYNAMIC_MAC_TABLE_TIMESTAMP]) >>
458 shifts[DYNAMIC_MAC_TIMESTAMP];
460 mac_addr[5] = (u8)data_lo;
461 mac_addr[4] = (u8)(data_lo >> 8);
462 mac_addr[3] = (u8)(data_lo >> 16);
463 mac_addr[2] = (u8)(data_lo >> 24);
465 mac_addr[1] = (u8)data_hi;
466 mac_addr[0] = (u8)(data_hi >> 8);
469 mutex_unlock(&dev->alu_mutex);
474 static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr,
475 struct alu_struct *alu, bool *valid)
477 u32 data_hi, data_lo;
483 shifts = dev->info->shifts;
484 masks = dev->info->masks;
486 ret = ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data);
490 data_hi = data >> 32;
493 if (!(data_hi & (masks[STATIC_MAC_TABLE_VALID] |
494 masks[STATIC_MAC_TABLE_OVERRIDE]))) {
499 alu->mac[5] = (u8)data_lo;
500 alu->mac[4] = (u8)(data_lo >> 8);
501 alu->mac[3] = (u8)(data_lo >> 16);
502 alu->mac[2] = (u8)(data_lo >> 24);
503 alu->mac[1] = (u8)data_hi;
504 alu->mac[0] = (u8)(data_hi >> 8);
506 (data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >>
507 shifts[STATIC_MAC_FWD_PORTS];
508 alu->is_override = (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0;
510 alu->is_static = true;
511 alu->is_use_fid = (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0;
512 alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >>
513 shifts[STATIC_MAC_FID];
520 static int ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr,
521 struct alu_struct *alu)
523 u32 data_hi, data_lo;
528 shifts = dev->info->shifts;
529 masks = dev->info->masks;
531 data_lo = ((u32)alu->mac[2] << 24) |
532 ((u32)alu->mac[3] << 16) |
533 ((u32)alu->mac[4] << 8) | alu->mac[5];
534 data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1];
535 data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS];
537 if (alu->is_override)
538 data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE];
539 if (alu->is_use_fid) {
540 data_hi |= masks[STATIC_MAC_TABLE_USE_FID];
541 data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID];
544 data_hi |= masks[STATIC_MAC_TABLE_VALID];
546 data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE];
548 data = (u64)data_hi << 32 | data_lo;
550 return ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data);
553 static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid,
554 u8 *member, u8 *valid)
559 shifts = dev->info->shifts;
560 masks = dev->info->masks;
562 *fid = vlan & masks[VLAN_TABLE_FID];
563 *member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >>
564 shifts[VLAN_TABLE_MEMBERSHIP_S];
565 *valid = !!(vlan & masks[VLAN_TABLE_VALID]);
568 static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid,
574 shifts = dev->info->shifts;
575 masks = dev->info->masks;
578 *vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S];
580 *vlan |= masks[VLAN_TABLE_VALID];
583 static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr)
589 shifts = dev->info->shifts;
591 ksz8_r_table(dev, TABLE_VLAN, addr, &data);
593 for (i = 0; i < 4; i++) {
594 dev->vlan_cache[addr + i].table[0] = (u16)data;
595 data >>= shifts[VLAN_TABLE];
599 static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
609 ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
613 static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
623 ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
625 dev->vlan_cache[vid].table[0] = vlan;
626 ksz8_w_table(dev, TABLE_VLAN, addr, buf);
629 int ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val)
631 u8 restart, speed, ctrl, link;
632 int processed = true;
639 regs = dev->info->regs;
643 ret = ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart);
647 ret = ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed);
651 ret = ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl);
655 if (restart & PORT_PHY_LOOPBACK)
656 data |= BMCR_LOOPBACK;
657 if (ctrl & PORT_FORCE_100_MBIT)
658 data |= BMCR_SPEED100;
659 if (ksz_is_ksz88x3(dev)) {
660 if ((ctrl & PORT_AUTO_NEG_ENABLE))
661 data |= BMCR_ANENABLE;
663 if (!(ctrl & PORT_AUTO_NEG_DISABLE))
664 data |= BMCR_ANENABLE;
666 if (restart & PORT_POWER_DOWN)
668 if (restart & PORT_AUTO_NEG_RESTART)
669 data |= BMCR_ANRESTART;
670 if (ctrl & PORT_FORCE_FULL_DUPLEX)
671 data |= BMCR_FULLDPLX;
672 if (speed & PORT_HP_MDIX)
673 data |= KSZ886X_BMCR_HP_MDIX;
674 if (restart & PORT_FORCE_MDIX)
675 data |= KSZ886X_BMCR_FORCE_MDI;
676 if (restart & PORT_AUTO_MDIX_DISABLE)
677 data |= KSZ886X_BMCR_DISABLE_AUTO_MDIX;
678 if (restart & PORT_TX_DISABLE)
679 data |= KSZ886X_BMCR_DISABLE_TRANSMIT;
680 if (restart & PORT_LED_OFF)
681 data |= KSZ886X_BMCR_DISABLE_LED;
684 ret = ksz_pread8(dev, p, regs[P_LINK_STATUS], &link);
688 data = BMSR_100FULL |
693 if (link & PORT_AUTO_NEG_COMPLETE)
694 data |= BMSR_ANEGCOMPLETE;
695 if (link & PORT_STAT_LINK_GOOD)
696 data |= BMSR_LSTATUS;
699 data = KSZ8795_ID_HI;
702 if (ksz_is_ksz88x3(dev))
703 data = KSZ8863_ID_LO;
705 data = KSZ8795_ID_LO;
708 ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
712 data = ADVERTISE_CSMA;
713 if (ctrl & PORT_AUTO_NEG_SYM_PAUSE)
714 data |= ADVERTISE_PAUSE_CAP;
715 if (ctrl & PORT_AUTO_NEG_100BTX_FD)
716 data |= ADVERTISE_100FULL;
717 if (ctrl & PORT_AUTO_NEG_100BTX)
718 data |= ADVERTISE_100HALF;
719 if (ctrl & PORT_AUTO_NEG_10BT_FD)
720 data |= ADVERTISE_10FULL;
721 if (ctrl & PORT_AUTO_NEG_10BT)
722 data |= ADVERTISE_10HALF;
725 ret = ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link);
730 if (link & PORT_REMOTE_SYM_PAUSE)
731 data |= LPA_PAUSE_CAP;
732 if (link & PORT_REMOTE_100BTX_FD)
734 if (link & PORT_REMOTE_100BTX)
736 if (link & PORT_REMOTE_10BT_FD)
738 if (link & PORT_REMOTE_10BT)
740 if (data & ~LPA_SLCT)
743 case PHY_REG_LINK_MD:
744 ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_CTRL, &val1);
748 ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_RESULT, &val2);
752 if (val1 & PORT_START_CABLE_DIAG)
753 data |= PHY_START_CABLE_DIAG;
755 if (val1 & PORT_CABLE_10M_SHORT)
756 data |= PHY_CABLE_10M_SHORT;
758 data |= FIELD_PREP(PHY_CABLE_DIAG_RESULT_M,
759 FIELD_GET(PORT_CABLE_DIAG_RESULT_M, val1));
761 data |= FIELD_PREP(PHY_CABLE_FAULT_COUNTER_M,
762 (FIELD_GET(PORT_CABLE_FAULT_COUNTER_H, val1) << 8) |
763 FIELD_GET(PORT_CABLE_FAULT_COUNTER_L, val2));
765 case PHY_REG_PHY_CTRL:
766 ret = ksz_pread8(dev, p, regs[P_LINK_STATUS], &link);
770 if (link & PORT_MDIX_STATUS)
771 data |= KSZ886X_CTRL_MDIX_STAT;
783 int ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val)
785 u8 restart, speed, ctrl, data;
790 regs = dev->info->regs;
795 /* Do not support PHY reset function. */
796 if (val & BMCR_RESET)
798 ret = ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed);
803 if (val & KSZ886X_BMCR_HP_MDIX)
804 data |= PORT_HP_MDIX;
806 data &= ~PORT_HP_MDIX;
809 ret = ksz_pwrite8(dev, p, regs[P_SPEED_STATUS], data);
814 ret = ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl);
819 if (ksz_is_ksz88x3(dev)) {
820 if ((val & BMCR_ANENABLE))
821 data |= PORT_AUTO_NEG_ENABLE;
823 data &= ~PORT_AUTO_NEG_ENABLE;
825 if (!(val & BMCR_ANENABLE))
826 data |= PORT_AUTO_NEG_DISABLE;
828 data &= ~PORT_AUTO_NEG_DISABLE;
830 /* Fiber port does not support auto-negotiation. */
831 if (dev->ports[p].fiber)
832 data |= PORT_AUTO_NEG_DISABLE;
835 if (val & BMCR_SPEED100)
836 data |= PORT_FORCE_100_MBIT;
838 data &= ~PORT_FORCE_100_MBIT;
839 if (val & BMCR_FULLDPLX)
840 data |= PORT_FORCE_FULL_DUPLEX;
842 data &= ~PORT_FORCE_FULL_DUPLEX;
845 ret = ksz_pwrite8(dev, p, regs[P_FORCE_CTRL], data);
850 ret = ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart);
855 if (val & KSZ886X_BMCR_DISABLE_LED)
856 data |= PORT_LED_OFF;
858 data &= ~PORT_LED_OFF;
859 if (val & KSZ886X_BMCR_DISABLE_TRANSMIT)
860 data |= PORT_TX_DISABLE;
862 data &= ~PORT_TX_DISABLE;
863 if (val & BMCR_ANRESTART)
864 data |= PORT_AUTO_NEG_RESTART;
866 data &= ~(PORT_AUTO_NEG_RESTART);
867 if (val & BMCR_PDOWN)
868 data |= PORT_POWER_DOWN;
870 data &= ~PORT_POWER_DOWN;
871 if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX)
872 data |= PORT_AUTO_MDIX_DISABLE;
874 data &= ~PORT_AUTO_MDIX_DISABLE;
875 if (val & KSZ886X_BMCR_FORCE_MDI)
876 data |= PORT_FORCE_MDIX;
878 data &= ~PORT_FORCE_MDIX;
879 if (val & BMCR_LOOPBACK)
880 data |= PORT_PHY_LOOPBACK;
882 data &= ~PORT_PHY_LOOPBACK;
884 if (data != restart) {
885 ret = ksz_pwrite8(dev, p, regs[P_NEG_RESTART_CTRL],
892 ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
897 data &= ~(PORT_AUTO_NEG_SYM_PAUSE |
898 PORT_AUTO_NEG_100BTX_FD |
899 PORT_AUTO_NEG_100BTX |
900 PORT_AUTO_NEG_10BT_FD |
902 if (val & ADVERTISE_PAUSE_CAP)
903 data |= PORT_AUTO_NEG_SYM_PAUSE;
904 if (val & ADVERTISE_100FULL)
905 data |= PORT_AUTO_NEG_100BTX_FD;
906 if (val & ADVERTISE_100HALF)
907 data |= PORT_AUTO_NEG_100BTX;
908 if (val & ADVERTISE_10FULL)
909 data |= PORT_AUTO_NEG_10BT_FD;
910 if (val & ADVERTISE_10HALF)
911 data |= PORT_AUTO_NEG_10BT;
914 ret = ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data);
919 case PHY_REG_LINK_MD:
920 if (val & PHY_START_CABLE_DIAG)
921 ksz_port_cfg(dev, p, REG_PORT_LINK_MD_CTRL, PORT_START_CABLE_DIAG, true);
930 void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member)
934 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
935 data &= ~PORT_VLAN_MEMBERSHIP;
936 data |= (member & dev->port_mask);
937 ksz_pwrite8(dev, port, P_MIRROR_CTRL, data);
940 void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port)
942 u8 learn[DSA_MAX_PORTS];
943 int first, index, cnt;
946 regs = dev->info->regs;
948 if ((uint)port < dev->info->port_cnt) {
952 /* Flush all ports. */
954 cnt = dev->info->port_cnt;
956 for (index = first; index < cnt; index++) {
957 ksz_pread8(dev, index, regs[P_STP_CTRL], &learn[index]);
958 if (!(learn[index] & PORT_LEARN_DISABLE))
959 ksz_pwrite8(dev, index, regs[P_STP_CTRL],
960 learn[index] | PORT_LEARN_DISABLE);
962 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
963 for (index = first; index < cnt; index++) {
964 if (!(learn[index] & PORT_LEARN_DISABLE))
965 ksz_pwrite8(dev, index, regs[P_STP_CTRL], learn[index]);
969 int ksz8_fdb_dump(struct ksz_device *dev, int port,
970 dsa_fdb_dump_cb_t *cb, void *data)
981 ret = ksz8_r_dyn_mac_table(dev, i, mac, &fid, &src_port,
982 ×tamp, &entries);
983 if (!ret && port == src_port) {
984 ret = cb(mac, fid, false, data);
989 } while (i < entries);
996 static int ksz8_add_sta_mac(struct ksz_device *dev, int port,
997 const unsigned char *addr, u16 vid)
999 struct alu_struct alu;
1003 alu.port_forward = 0;
1004 for (index = 0; index < dev->info->num_statics; index++) {
1007 ret = ksz8_r_sta_mac_table(dev, index, &alu, &valid);
1011 /* Remember the first empty entry. */
1017 if (!memcmp(alu.mac, addr, ETH_ALEN) && alu.fid == vid)
1021 /* no available entry */
1022 if (index == dev->info->num_statics && !empty)
1026 if (index == dev->info->num_statics) {
1028 memset(&alu, 0, sizeof(alu));
1029 memcpy(alu.mac, addr, ETH_ALEN);
1030 alu.is_static = true;
1032 alu.port_forward |= BIT(port);
1034 alu.is_use_fid = true;
1036 /* Need a way to map VID to FID. */
1040 return ksz8_w_sta_mac_table(dev, index, &alu);
1043 static int ksz8_del_sta_mac(struct ksz_device *dev, int port,
1044 const unsigned char *addr, u16 vid)
1046 struct alu_struct alu;
1049 for (index = 0; index < dev->info->num_statics; index++) {
1052 ret = ksz8_r_sta_mac_table(dev, index, &alu, &valid);
1058 if (!memcmp(alu.mac, addr, ETH_ALEN) && alu.fid == vid)
1062 /* no available entry */
1063 if (index == dev->info->num_statics)
1067 alu.port_forward &= ~BIT(port);
1068 if (!alu.port_forward)
1069 alu.is_static = false;
1071 return ksz8_w_sta_mac_table(dev, index, &alu);
1074 int ksz8_mdb_add(struct ksz_device *dev, int port,
1075 const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
1077 return ksz8_add_sta_mac(dev, port, mdb->addr, mdb->vid);
1080 int ksz8_mdb_del(struct ksz_device *dev, int port,
1081 const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
1083 return ksz8_del_sta_mac(dev, port, mdb->addr, mdb->vid);
1086 int ksz8_fdb_add(struct ksz_device *dev, int port, const unsigned char *addr,
1087 u16 vid, struct dsa_db db)
1089 return ksz8_add_sta_mac(dev, port, addr, vid);
1092 int ksz8_fdb_del(struct ksz_device *dev, int port, const unsigned char *addr,
1093 u16 vid, struct dsa_db db)
1095 return ksz8_del_sta_mac(dev, port, addr, vid);
1098 int ksz8_port_vlan_filtering(struct ksz_device *dev, int port, bool flag,
1099 struct netlink_ext_ack *extack)
1101 if (ksz_is_ksz88x3(dev))
1104 /* Discard packets with VID not enabled on the switch */
1105 ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
1107 /* Discard packets with VID not enabled on the ingress port */
1108 for (port = 0; port < dev->phy_port_cnt; ++port)
1109 ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER,
1115 static void ksz8_port_enable_pvid(struct ksz_device *dev, int port, bool state)
1117 if (ksz_is_ksz88x3(dev)) {
1118 ksz_cfg(dev, REG_SW_INSERT_SRC_PVID,
1119 0x03 << (4 - 2 * port), state);
1121 ksz_pwrite8(dev, port, REG_PORT_CTRL_12, state ? 0x0f : 0x00);
1125 int ksz8_port_vlan_add(struct ksz_device *dev, int port,
1126 const struct switchdev_obj_port_vlan *vlan,
1127 struct netlink_ext_ack *extack)
1129 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1130 struct ksz_port *p = &dev->ports[port];
1131 u16 data, new_pvid = 0;
1132 u8 fid, member, valid;
1134 if (ksz_is_ksz88x3(dev))
1137 /* If a VLAN is added with untagged flag different from the
1138 * port's Remove Tag flag, we need to change the latter.
1139 * Ignore VID 0, which is always untagged.
1140 * Ignore CPU port, which will always be tagged.
1142 if (untagged != p->remove_tag && vlan->vid != 0 &&
1143 port != dev->cpu_port) {
1146 /* Reject attempts to add a VLAN that requires the
1147 * Remove Tag flag to be changed, unless there are no
1148 * other VLANs currently configured.
1150 for (vid = 1; vid < dev->info->num_vlans; ++vid) {
1151 /* Skip the VID we are going to add or reconfigure */
1152 if (vid == vlan->vid)
1155 ksz8_from_vlan(dev, dev->vlan_cache[vid].table[0],
1156 &fid, &member, &valid);
1157 if (valid && (member & BIT(port)))
1161 ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
1162 p->remove_tag = untagged;
1165 ksz8_r_vlan_table(dev, vlan->vid, &data);
1166 ksz8_from_vlan(dev, data, &fid, &member, &valid);
1168 /* First time to setup the VLAN entry. */
1170 /* Need to find a way to map VID to FID. */
1174 member |= BIT(port);
1176 ksz8_to_vlan(dev, fid, member, valid, &data);
1177 ksz8_w_vlan_table(dev, vlan->vid, data);
1180 if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
1181 new_pvid = vlan->vid;
1186 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
1187 vid &= ~VLAN_VID_MASK;
1189 ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
1191 ksz8_port_enable_pvid(dev, port, true);
1197 int ksz8_port_vlan_del(struct ksz_device *dev, int port,
1198 const struct switchdev_obj_port_vlan *vlan)
1201 u8 fid, member, valid;
1203 if (ksz_is_ksz88x3(dev))
1206 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
1207 pvid = pvid & 0xFFF;
1209 ksz8_r_vlan_table(dev, vlan->vid, &data);
1210 ksz8_from_vlan(dev, data, &fid, &member, &valid);
1212 member &= ~BIT(port);
1214 /* Invalidate the entry if no more member. */
1220 ksz8_to_vlan(dev, fid, member, valid, &data);
1221 ksz8_w_vlan_table(dev, vlan->vid, data);
1223 if (pvid == vlan->vid)
1224 ksz8_port_enable_pvid(dev, port, false);
1229 int ksz8_port_mirror_add(struct ksz_device *dev, int port,
1230 struct dsa_mall_mirror_tc_entry *mirror,
1231 bool ingress, struct netlink_ext_ack *extack)
1234 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
1235 dev->mirror_rx |= BIT(port);
1237 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
1238 dev->mirror_tx |= BIT(port);
1241 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
1243 /* configure mirror port */
1244 if (dev->mirror_rx || dev->mirror_tx)
1245 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1246 PORT_MIRROR_SNIFFER, true);
1251 void ksz8_port_mirror_del(struct ksz_device *dev, int port,
1252 struct dsa_mall_mirror_tc_entry *mirror)
1256 if (mirror->ingress) {
1257 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
1258 dev->mirror_rx &= ~BIT(port);
1260 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
1261 dev->mirror_tx &= ~BIT(port);
1264 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1266 if (!dev->mirror_rx && !dev->mirror_tx)
1267 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1268 PORT_MIRROR_SNIFFER, false);
1271 static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port)
1273 struct ksz_port *p = &dev->ports[port];
1275 if (!p->interface && dev->compat_interface) {
1277 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
1278 "Please update your device tree.\n",
1280 p->interface = dev->compat_interface;
1284 void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1286 struct dsa_switch *ds = dev->ds;
1290 masks = dev->info->masks;
1292 /* enable broadcast storm limit */
1293 ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1295 if (!ksz_is_ksz88x3(dev))
1296 ksz8795_set_prio_queue(dev, port, 4);
1298 /* disable DiffServ priority */
1299 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false);
1301 /* replace priority */
1302 ksz_port_cfg(dev, port, P_802_1P_CTRL,
1303 masks[PORT_802_1P_REMAPPING], false);
1305 /* enable 802.1p priority */
1306 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true);
1309 if (!ksz_is_ksz88x3(dev))
1310 ksz8795_cpu_interface_select(dev, port);
1312 member = dsa_user_ports(ds);
1314 member = BIT(dsa_upstream_port(ds, port));
1317 ksz8_cfg_port_member(dev, port, member);
1320 void ksz8_config_cpu_port(struct dsa_switch *ds)
1322 struct ksz_device *dev = ds->priv;
1329 masks = dev->info->masks;
1330 regs = dev->info->regs;
1332 ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true);
1334 ksz8_port_setup(dev, dev->cpu_port, true);
1336 for (i = 0; i < dev->phy_port_cnt; i++) {
1337 ksz_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1339 for (i = 0; i < dev->phy_port_cnt; i++) {
1342 if (!ksz_is_ksz88x3(dev)) {
1343 ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote);
1344 if (remote & KSZ8_PORT_FIBER_MODE)
1348 ksz_port_cfg(dev, i, regs[P_STP_CTRL],
1349 PORT_FORCE_FLOW_CTRL, true);
1351 ksz_port_cfg(dev, i, regs[P_STP_CTRL],
1352 PORT_FORCE_FLOW_CTRL, false);
1356 static int ksz8_handle_global_errata(struct dsa_switch *ds)
1358 struct ksz_device *dev = ds->priv;
1361 /* KSZ87xx Errata DS80000687C.
1362 * Module 2: Link drops with some EEE link partners.
1363 * An issue with the EEE next page exchange between the
1364 * KSZ879x/KSZ877x/KSZ876x and some EEE link partners may result in
1365 * the link dropping.
1367 if (dev->info->ksz87xx_eee_link_erratum)
1368 ret = ksz8_ind_write8(dev, TABLE_EEE, REG_IND_EEE_GLOB2_HI, 0);
1373 int ksz8_enable_stp_addr(struct ksz_device *dev)
1375 struct alu_struct alu;
1377 /* Setup STP address for STP operation. */
1378 memset(&alu, 0, sizeof(alu));
1379 ether_addr_copy(alu.mac, eth_stp_addr);
1380 alu.is_static = true;
1381 alu.is_override = true;
1382 alu.port_forward = dev->info->cpu_ports;
1384 return ksz8_w_sta_mac_table(dev, 0, &alu);
1387 int ksz8_setup(struct dsa_switch *ds)
1389 struct ksz_device *dev = ds->priv;
1392 ds->mtu_enforcement_ingress = true;
1394 /* We rely on software untagging on the CPU port, so that we
1395 * can support both tagged and untagged VLANs
1397 ds->untag_bridge_pvid = true;
1399 /* VLAN filtering is partly controlled by the global VLAN
1402 ds->vlan_filtering_is_global = true;
1404 ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true);
1406 /* Enable automatic fast aging when link changed detected. */
1407 ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
1409 /* Enable aggressive back off algorithm in half duplex mode. */
1410 regmap_update_bits(ksz_regmap_8(dev), REG_SW_CTRL_1,
1411 SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
1414 * Make sure unicast VLAN boundary is set as default and
1415 * enable no excessive collision drop.
1417 regmap_update_bits(ksz_regmap_8(dev), REG_SW_CTRL_2,
1418 UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
1419 UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
1421 ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
1423 ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1425 if (!ksz_is_ksz88x3(dev))
1426 ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true);
1428 for (i = 0; i < (dev->info->num_vlans / 4); i++)
1429 ksz8_r_vlan_entries(dev, i);
1431 return ksz8_handle_global_errata(ds);
1434 void ksz8_get_caps(struct ksz_device *dev, int port,
1435 struct phylink_config *config)
1437 config->mac_capabilities = MAC_10 | MAC_100;
1439 /* Silicon Errata Sheet (DS80000830A):
1440 * "Port 1 does not respond to received flow control PAUSE frames"
1441 * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3
1444 if (!ksz_is_ksz88x3(dev) || port)
1445 config->mac_capabilities |= MAC_SYM_PAUSE;
1447 /* Asym pause is not supported on KSZ8863 and KSZ8873 */
1448 if (!ksz_is_ksz88x3(dev))
1449 config->mac_capabilities |= MAC_ASYM_PAUSE;
1452 u32 ksz8_get_port_addr(int port, int offset)
1454 return PORT_CTRL_ADDR(port, offset);
1457 int ksz8_switch_init(struct ksz_device *dev)
1459 dev->cpu_port = fls(dev->info->cpu_ports) - 1;
1460 dev->phy_port_cnt = dev->info->port_cnt - 1;
1461 dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | dev->info->cpu_ports;
1466 void ksz8_switch_exit(struct ksz_device *dev)
1468 ksz8_reset_switch(dev);
1471 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
1472 MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
1473 MODULE_LICENSE("GPL");