1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip KSZ8795 switch driver
5 * Copyright (C) 2017 Microchip Technology Inc.
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_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
54 ret = ksz_write8(dev, regs[REG_IND_BYTE], data);
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 int ksz8_port_queue_split(struct ksz_device *dev, int port, int queues)
138 if (ksz_is_ksz88x3(dev)) {
139 mask_4q = KSZ8873_PORT_4QUEUE_SPLIT_EN;
140 mask_2q = KSZ8873_PORT_2QUEUE_SPLIT_EN;
141 reg_4q = REG_PORT_CTRL_0;
142 reg_2q = REG_PORT_CTRL_2;
144 /* KSZ8795 family switches have Weighted Fair Queueing (WFQ)
145 * enabled by default. Enable it for KSZ8873 family switches
146 * too. Default value for KSZ8873 family is strict priority,
147 * which should be enabled by using TC_SETUP_QDISC_ETS, not
150 ret = ksz_rmw8(dev, REG_SW_CTRL_3, WEIGHTED_FAIR_QUEUE_ENABLE,
151 WEIGHTED_FAIR_QUEUE_ENABLE);
155 mask_4q = KSZ8795_PORT_4QUEUE_SPLIT_EN;
156 mask_2q = KSZ8795_PORT_2QUEUE_SPLIT_EN;
157 reg_4q = REG_PORT_CTRL_13;
158 reg_2q = REG_PORT_CTRL_0;
160 /* TODO: this is legacy from initial KSZ8795 driver, should be
161 * moved to appropriate place in the future.
163 ret = ksz_rmw8(dev, REG_SW_CTRL_19,
164 SW_OUT_RATE_LIMIT_QUEUE_BASED,
165 SW_OUT_RATE_LIMIT_QUEUE_BASED);
172 else if (queues == 2)
175 ret = ksz_prmw8(dev, port, reg_4q, mask_4q, data_4q);
179 return ksz_prmw8(dev, port, reg_2q, mask_2q, data_2q);
182 int ksz8_all_queues_split(struct ksz_device *dev, int queues)
184 struct dsa_switch *ds = dev->ds;
185 const struct dsa_port *dp;
187 dsa_switch_for_each_port(dp, ds) {
188 int ret = ksz8_port_queue_split(dev, dp->index, queues);
197 void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt)
206 masks = dev->info->masks;
207 regs = dev->info->regs;
209 ctrl_addr = addr + dev->info->reg_mib_cnt * port;
210 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
212 mutex_lock(&dev->alu_mutex);
213 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
215 /* It is almost guaranteed to always read the valid bit because of
218 for (loop = 2; loop > 0; loop--) {
219 ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
221 if (check & masks[MIB_COUNTER_VALID]) {
222 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
223 if (check & masks[MIB_COUNTER_OVERFLOW])
224 *cnt += MIB_COUNTER_VALUE + 1;
225 *cnt += data & MIB_COUNTER_VALUE;
229 mutex_unlock(&dev->alu_mutex);
232 static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
233 u64 *dropped, u64 *cnt)
242 masks = dev->info->masks;
243 regs = dev->info->regs;
245 addr -= dev->info->reg_mib_cnt;
246 ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port;
247 ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0;
248 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
250 mutex_lock(&dev->alu_mutex);
251 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
253 /* It is almost guaranteed to always read the valid bit because of
256 for (loop = 2; loop > 0; loop--) {
257 ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
259 if (check & masks[MIB_COUNTER_VALID]) {
260 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
264 total = check & MIB_TOTAL_BYTES_H;
268 if (check & masks[MIB_COUNTER_OVERFLOW]) {
269 total = MIB_TOTAL_BYTES_H + 1;
274 if (check & masks[MIB_COUNTER_OVERFLOW])
275 *cnt += MIB_PACKET_DROPPED + 1;
276 *cnt += data & MIB_PACKET_DROPPED;
281 mutex_unlock(&dev->alu_mutex);
284 static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
285 u64 *dropped, u64 *cnt)
287 u32 *last = (u32 *)dropped;
293 regs = dev->info->regs;
295 addr -= dev->info->reg_mib_cnt;
296 ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 :
297 KSZ8863_MIB_PACKET_DROPPED_RX_0;
299 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
301 mutex_lock(&dev->alu_mutex);
302 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
303 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
304 mutex_unlock(&dev->alu_mutex);
306 data &= MIB_PACKET_DROPPED;
311 data += MIB_PACKET_DROPPED + 1;
317 void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
318 u64 *dropped, u64 *cnt)
320 if (ksz_is_ksz88x3(dev))
321 ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt);
323 ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt);
326 void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze)
328 if (ksz_is_ksz88x3(dev))
331 /* enable the port for flush/freeze function */
333 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
334 ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze);
336 /* disable the port after freeze is done */
338 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
341 void ksz8_port_init_cnt(struct ksz_device *dev, int port)
343 struct ksz_port_mib *mib = &dev->ports[port].mib;
346 if (!ksz_is_ksz88x3(dev)) {
347 /* flush all enabled port MIB counters */
348 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
349 ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true);
350 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
355 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
356 while (mib->cnt_ptr < dev->info->reg_mib_cnt) {
357 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
358 &mib->counters[mib->cnt_ptr]);
362 /* last one in storage */
363 dropped = &mib->counters[dev->info->mib_cnt];
365 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
366 while (mib->cnt_ptr < dev->info->mib_cnt) {
367 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
368 dropped, &mib->counters[mib->cnt_ptr]);
373 static int ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data)
379 regs = dev->info->regs;
381 ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
383 mutex_lock(&dev->alu_mutex);
384 ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
388 ret = ksz_read64(dev, regs[REG_IND_DATA_HI], data);
390 mutex_unlock(&dev->alu_mutex);
395 static int ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data)
401 regs = dev->info->regs;
403 ctrl_addr = IND_ACC_TABLE(table) | addr;
405 mutex_lock(&dev->alu_mutex);
406 ret = ksz_write64(dev, regs[REG_IND_DATA_HI], data);
410 ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
412 mutex_unlock(&dev->alu_mutex);
417 static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data)
424 masks = dev->info->masks;
425 regs = dev->info->regs;
428 ret = ksz_read8(dev, regs[REG_IND_DATA_CHECK], data);
433 } while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout);
435 /* Entry is not ready for accessing. */
436 if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY])
439 /* Entry is ready for accessing. */
440 return ksz_read8(dev, regs[REG_IND_DATA_8], data);
443 static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, u8 *mac_addr,
444 u8 *fid, u8 *src_port, u16 *entries)
446 u32 data_hi, data_lo;
456 shifts = dev->info->shifts;
457 masks = dev->info->masks;
458 regs = dev->info->regs;
460 ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr;
462 mutex_lock(&dev->alu_mutex);
463 ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
467 ret = ksz8_valid_dyn_entry(dev, &data);
471 if (data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY]) {
476 ret = ksz_read64(dev, regs[REG_IND_DATA_HI], &buf);
480 data_hi = (u32)(buf >> 32);
483 /* Check out how many valid entry in the table. */
484 cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H];
485 cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H];
486 cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >>
487 shifts[DYNAMIC_MAC_ENTRIES];
490 *fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >>
491 shifts[DYNAMIC_MAC_FID];
492 *src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >>
493 shifts[DYNAMIC_MAC_SRC_PORT];
495 mac_addr[5] = (u8)data_lo;
496 mac_addr[4] = (u8)(data_lo >> 8);
497 mac_addr[3] = (u8)(data_lo >> 16);
498 mac_addr[2] = (u8)(data_lo >> 24);
500 mac_addr[1] = (u8)data_hi;
501 mac_addr[0] = (u8)(data_hi >> 8);
504 mutex_unlock(&dev->alu_mutex);
509 static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr,
510 struct alu_struct *alu, bool *valid)
512 u32 data_hi, data_lo;
518 shifts = dev->info->shifts;
519 masks = dev->info->masks;
521 ret = ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data);
525 data_hi = data >> 32;
528 if (!(data_hi & (masks[STATIC_MAC_TABLE_VALID] |
529 masks[STATIC_MAC_TABLE_OVERRIDE]))) {
534 alu->mac[5] = (u8)data_lo;
535 alu->mac[4] = (u8)(data_lo >> 8);
536 alu->mac[3] = (u8)(data_lo >> 16);
537 alu->mac[2] = (u8)(data_lo >> 24);
538 alu->mac[1] = (u8)data_hi;
539 alu->mac[0] = (u8)(data_hi >> 8);
541 (data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >>
542 shifts[STATIC_MAC_FWD_PORTS];
543 alu->is_override = (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0;
545 /* KSZ8795 family switches have STATIC_MAC_TABLE_USE_FID and
546 * STATIC_MAC_TABLE_FID definitions off by 1 when doing read on the
547 * static MAC table compared to doing write.
549 if (ksz_is_ksz87xx(dev))
551 alu->is_static = true;
552 alu->is_use_fid = (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0;
553 alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >>
554 shifts[STATIC_MAC_FID];
561 static int ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr,
562 struct alu_struct *alu)
564 u32 data_hi, data_lo;
569 shifts = dev->info->shifts;
570 masks = dev->info->masks;
572 data_lo = ((u32)alu->mac[2] << 24) |
573 ((u32)alu->mac[3] << 16) |
574 ((u32)alu->mac[4] << 8) | alu->mac[5];
575 data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1];
576 data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS];
578 if (alu->is_override)
579 data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE];
580 if (alu->is_use_fid) {
581 data_hi |= masks[STATIC_MAC_TABLE_USE_FID];
582 data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID];
585 data_hi |= masks[STATIC_MAC_TABLE_VALID];
587 data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE];
589 data = (u64)data_hi << 32 | data_lo;
591 return ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data);
594 static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid,
595 u8 *member, u8 *valid)
600 shifts = dev->info->shifts;
601 masks = dev->info->masks;
603 *fid = vlan & masks[VLAN_TABLE_FID];
604 *member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >>
605 shifts[VLAN_TABLE_MEMBERSHIP_S];
606 *valid = !!(vlan & masks[VLAN_TABLE_VALID]);
609 static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid,
615 shifts = dev->info->shifts;
616 masks = dev->info->masks;
619 *vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S];
621 *vlan |= masks[VLAN_TABLE_VALID];
624 static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr)
630 shifts = dev->info->shifts;
632 ksz8_r_table(dev, TABLE_VLAN, addr, &data);
634 for (i = 0; i < 4; i++) {
635 dev->vlan_cache[addr + i].table[0] = (u16)data;
636 data >>= shifts[VLAN_TABLE];
640 static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
650 ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
654 static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
664 ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
666 dev->vlan_cache[vid].table[0] = vlan;
667 ksz8_w_table(dev, TABLE_VLAN, addr, buf);
671 * ksz879x_get_loopback - KSZ879x specific function to get loopback
672 * configuration status for a specific port
673 * @dev: Pointer to the device structure
674 * @port: Port number to query
675 * @val: Pointer to store the result
677 * This function reads the SMI registers to determine whether loopback mode
678 * is enabled for a specific port.
680 * Return: 0 on success, error code on failure.
682 static int ksz879x_get_loopback(struct ksz_device *dev, u16 port,
688 ret = ksz_pread8(dev, port, REG_PORT_STATUS_3, &stat3);
692 if (stat3 & PORT_PHY_LOOPBACK)
693 *val |= BMCR_LOOPBACK;
699 * ksz879x_set_loopback - KSZ879x specific function to set loopback mode for
701 * @dev: Pointer to the device structure.
702 * @port: Port number to modify.
703 * @val: Value indicating whether to enable or disable loopback mode.
705 * This function translates loopback bit of the BMCR register into the
706 * corresponding hardware register bit value and writes it to the SMI interface.
708 * Return: 0 on success, error code on failure.
710 static int ksz879x_set_loopback(struct ksz_device *dev, u16 port, u16 val)
714 if (val & BMCR_LOOPBACK)
715 stat3 |= PORT_PHY_LOOPBACK;
717 return ksz_prmw8(dev, port, REG_PORT_STATUS_3, PORT_PHY_LOOPBACK,
722 * ksz8_r_phy_ctrl - Translates and reads from the SMI interface to a MIIM PHY
723 * Control register (Reg. 31).
724 * @dev: The KSZ device instance.
725 * @port: The port number to be read.
726 * @val: The value read from the SMI interface.
728 * This function reads the SMI interface and translates the hardware register
729 * bit values into their corresponding control settings for a MIIM PHY Control
732 * Return: 0 on success, error code on failure.
734 static int ksz8_r_phy_ctrl(struct ksz_device *dev, int port, u16 *val)
736 const u16 *regs = dev->info->regs;
742 ret = ksz_pread8(dev, port, regs[P_LINK_STATUS], ®_val);
746 if (reg_val & PORT_MDIX_STATUS)
747 *val |= KSZ886X_CTRL_MDIX_STAT;
749 ret = ksz_pread8(dev, port, REG_PORT_LINK_MD_CTRL, ®_val);
753 if (reg_val & PORT_FORCE_LINK)
754 *val |= KSZ886X_CTRL_FORCE_LINK;
756 if (reg_val & PORT_POWER_SAVING)
757 *val |= KSZ886X_CTRL_PWRSAVE;
759 if (reg_val & PORT_PHY_REMOTE_LOOPBACK)
760 *val |= KSZ886X_CTRL_REMOTE_LOOPBACK;
766 * ksz8_r_phy_bmcr - Translates and reads from the SMI interface to a MIIM PHY
767 * Basic mode control register (Reg. 0).
768 * @dev: The KSZ device instance.
769 * @port: The port number to be read.
770 * @val: The value read from the SMI interface.
772 * This function reads the SMI interface and translates the hardware register
773 * bit values into their corresponding control settings for a MIIM PHY Basic
774 * mode control register.
776 * MIIM Bit Mapping Comparison between KSZ8794 and KSZ8873
777 * -------------------------------------------------------------------
778 * MIIM Bit | KSZ8794 Reg/Bit | KSZ8873 Reg/Bit
779 * ----------------------------+-----------------------------+----------------
780 * Bit 15 - Soft Reset | 0xF/4 | Not supported
781 * Bit 14 - Loopback | 0xD/0 (MAC), 0xF/7 (PHY) ~ 0xD/0 (PHY)
782 * Bit 13 - Force 100 | 0xC/6 = 0xC/6
783 * Bit 12 - AN Enable | 0xC/7 (reverse logic) ~ 0xC/7
784 * Bit 11 - Power Down | 0xD/3 = 0xD/3
785 * Bit 10 - PHY Isolate | 0xF/5 | Not supported
786 * Bit 9 - Restart AN | 0xD/5 = 0xD/5
787 * Bit 8 - Force Full-Duplex | 0xC/5 = 0xC/5
788 * Bit 7 - Collision Test/Res. | Not supported | Not supported
789 * Bit 6 - Reserved | Not supported | Not supported
790 * Bit 5 - Hp_mdix | 0x9/7 ~ 0xF/7
791 * Bit 4 - Force MDI | 0xD/1 = 0xD/1
792 * Bit 3 - Disable MDIX | 0xD/2 = 0xD/2
793 * Bit 2 - Disable Far-End F. | ???? | 0xD/4
794 * Bit 1 - Disable Transmit | 0xD/6 = 0xD/6
795 * Bit 0 - Disable LED | 0xD/7 = 0xD/7
796 * -------------------------------------------------------------------
798 * Return: 0 on success, error code on failure.
800 static int ksz8_r_phy_bmcr(struct ksz_device *dev, u16 port, u16 *val)
802 const u16 *regs = dev->info->regs;
803 u8 restart, speed, ctrl;
808 ret = ksz_pread8(dev, port, regs[P_NEG_RESTART_CTRL], &restart);
812 ret = ksz_pread8(dev, port, regs[P_SPEED_STATUS], &speed);
816 ret = ksz_pread8(dev, port, regs[P_FORCE_CTRL], &ctrl);
820 if (ctrl & PORT_FORCE_100_MBIT)
821 *val |= BMCR_SPEED100;
823 if (ksz_is_ksz88x3(dev)) {
824 if (restart & KSZ8873_PORT_PHY_LOOPBACK)
825 *val |= BMCR_LOOPBACK;
827 if ((ctrl & PORT_AUTO_NEG_ENABLE))
828 *val |= BMCR_ANENABLE;
830 ret = ksz879x_get_loopback(dev, port, val);
834 if (!(ctrl & PORT_AUTO_NEG_DISABLE))
835 *val |= BMCR_ANENABLE;
838 if (restart & PORT_POWER_DOWN)
841 if (restart & PORT_AUTO_NEG_RESTART)
842 *val |= BMCR_ANRESTART;
844 if (ctrl & PORT_FORCE_FULL_DUPLEX)
845 *val |= BMCR_FULLDPLX;
847 if (speed & PORT_HP_MDIX)
848 *val |= KSZ886X_BMCR_HP_MDIX;
850 if (restart & PORT_FORCE_MDIX)
851 *val |= KSZ886X_BMCR_FORCE_MDI;
853 if (restart & PORT_AUTO_MDIX_DISABLE)
854 *val |= KSZ886X_BMCR_DISABLE_AUTO_MDIX;
856 if (restart & PORT_TX_DISABLE)
857 *val |= KSZ886X_BMCR_DISABLE_TRANSMIT;
859 if (restart & PORT_LED_OFF)
860 *val |= KSZ886X_BMCR_DISABLE_LED;
865 int ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val)
867 u8 ctrl, link, val1, val2;
868 int processed = true;
874 regs = dev->info->regs;
878 ret = ksz8_r_phy_bmcr(dev, p, &data);
883 ret = ksz_pread8(dev, p, regs[P_LINK_STATUS], &link);
887 data = BMSR_100FULL |
892 if (link & PORT_AUTO_NEG_COMPLETE)
893 data |= BMSR_ANEGCOMPLETE;
894 if (link & PORT_STAT_LINK_GOOD)
895 data |= BMSR_LSTATUS;
898 data = KSZ8795_ID_HI;
901 if (ksz_is_ksz88x3(dev))
902 data = KSZ8863_ID_LO;
904 data = KSZ8795_ID_LO;
907 ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
911 data = ADVERTISE_CSMA;
912 if (ctrl & PORT_AUTO_NEG_SYM_PAUSE)
913 data |= ADVERTISE_PAUSE_CAP;
914 if (ctrl & PORT_AUTO_NEG_100BTX_FD)
915 data |= ADVERTISE_100FULL;
916 if (ctrl & PORT_AUTO_NEG_100BTX)
917 data |= ADVERTISE_100HALF;
918 if (ctrl & PORT_AUTO_NEG_10BT_FD)
919 data |= ADVERTISE_10FULL;
920 if (ctrl & PORT_AUTO_NEG_10BT)
921 data |= ADVERTISE_10HALF;
924 ret = ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link);
929 if (link & PORT_REMOTE_SYM_PAUSE)
930 data |= LPA_PAUSE_CAP;
931 if (link & PORT_REMOTE_100BTX_FD)
933 if (link & PORT_REMOTE_100BTX)
935 if (link & PORT_REMOTE_10BT_FD)
937 if (link & PORT_REMOTE_10BT)
939 if (data & ~LPA_SLCT)
942 case PHY_REG_LINK_MD:
943 ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_CTRL, &val1);
947 ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_RESULT, &val2);
951 if (val1 & PORT_START_CABLE_DIAG)
952 data |= PHY_START_CABLE_DIAG;
954 if (val1 & PORT_CABLE_10M_SHORT)
955 data |= PHY_CABLE_10M_SHORT;
957 data |= FIELD_PREP(PHY_CABLE_DIAG_RESULT_M,
958 FIELD_GET(PORT_CABLE_DIAG_RESULT_M, val1));
960 data |= FIELD_PREP(PHY_CABLE_FAULT_COUNTER_M,
961 (FIELD_GET(PORT_CABLE_FAULT_COUNTER_H, val1) << 8) |
962 FIELD_GET(PORT_CABLE_FAULT_COUNTER_L, val2));
964 case PHY_REG_PHY_CTRL:
965 ret = ksz8_r_phy_ctrl(dev, p, &data);
981 * ksz8_w_phy_ctrl - Translates and writes to the SMI interface from a MIIM PHY
982 * Control register (Reg. 31).
983 * @dev: The KSZ device instance.
984 * @port: The port number to be configured.
985 * @val: The register value to be written.
987 * This function translates control settings from a MIIM PHY Control register
988 * into their corresponding hardware register bit values for the SMI
991 * Return: 0 on success, error code on failure.
993 static int ksz8_w_phy_ctrl(struct ksz_device *dev, int port, u16 val)
998 if (val & KSZ886X_CTRL_FORCE_LINK)
999 reg_val |= PORT_FORCE_LINK;
1001 if (val & KSZ886X_CTRL_PWRSAVE)
1002 reg_val |= PORT_POWER_SAVING;
1004 if (val & KSZ886X_CTRL_REMOTE_LOOPBACK)
1005 reg_val |= PORT_PHY_REMOTE_LOOPBACK;
1007 ret = ksz_prmw8(dev, port, REG_PORT_LINK_MD_CTRL, PORT_FORCE_LINK |
1008 PORT_POWER_SAVING | PORT_PHY_REMOTE_LOOPBACK, reg_val);
1013 * ksz8_w_phy_bmcr - Translates and writes to the SMI interface from a MIIM PHY
1014 * Basic mode control register (Reg. 0).
1015 * @dev: The KSZ device instance.
1016 * @port: The port number to be configured.
1017 * @val: The register value to be written.
1019 * This function translates control settings from a MIIM PHY Basic mode control
1020 * register into their corresponding hardware register bit values for the SMI
1023 * MIIM Bit Mapping Comparison between KSZ8794 and KSZ8873
1024 * -------------------------------------------------------------------
1025 * MIIM Bit | KSZ8794 Reg/Bit | KSZ8873 Reg/Bit
1026 * ----------------------------+-----------------------------+----------------
1027 * Bit 15 - Soft Reset | 0xF/4 | Not supported
1028 * Bit 14 - Loopback | 0xD/0 (MAC), 0xF/7 (PHY) ~ 0xD/0 (PHY)
1029 * Bit 13 - Force 100 | 0xC/6 = 0xC/6
1030 * Bit 12 - AN Enable | 0xC/7 (reverse logic) ~ 0xC/7
1031 * Bit 11 - Power Down | 0xD/3 = 0xD/3
1032 * Bit 10 - PHY Isolate | 0xF/5 | Not supported
1033 * Bit 9 - Restart AN | 0xD/5 = 0xD/5
1034 * Bit 8 - Force Full-Duplex | 0xC/5 = 0xC/5
1035 * Bit 7 - Collision Test/Res. | Not supported | Not supported
1036 * Bit 6 - Reserved | Not supported | Not supported
1037 * Bit 5 - Hp_mdix | 0x9/7 ~ 0xF/7
1038 * Bit 4 - Force MDI | 0xD/1 = 0xD/1
1039 * Bit 3 - Disable MDIX | 0xD/2 = 0xD/2
1040 * Bit 2 - Disable Far-End F. | ???? | 0xD/4
1041 * Bit 1 - Disable Transmit | 0xD/6 = 0xD/6
1042 * Bit 0 - Disable LED | 0xD/7 = 0xD/7
1043 * -------------------------------------------------------------------
1045 * Return: 0 on success, error code on failure.
1047 static int ksz8_w_phy_bmcr(struct ksz_device *dev, u16 port, u16 val)
1049 u8 restart, speed, ctrl, restart_mask;
1050 const u16 *regs = dev->info->regs;
1053 /* Do not support PHY reset function. */
1054 if (val & BMCR_RESET)
1058 if (val & KSZ886X_BMCR_HP_MDIX)
1059 speed |= PORT_HP_MDIX;
1061 ret = ksz_prmw8(dev, port, regs[P_SPEED_STATUS], PORT_HP_MDIX, speed);
1066 if (ksz_is_ksz88x3(dev)) {
1067 if ((val & BMCR_ANENABLE))
1068 ctrl |= PORT_AUTO_NEG_ENABLE;
1070 if (!(val & BMCR_ANENABLE))
1071 ctrl |= PORT_AUTO_NEG_DISABLE;
1073 /* Fiber port does not support auto-negotiation. */
1074 if (dev->ports[port].fiber)
1075 ctrl |= PORT_AUTO_NEG_DISABLE;
1078 if (val & BMCR_SPEED100)
1079 ctrl |= PORT_FORCE_100_MBIT;
1081 if (val & BMCR_FULLDPLX)
1082 ctrl |= PORT_FORCE_FULL_DUPLEX;
1084 ret = ksz_prmw8(dev, port, regs[P_FORCE_CTRL], PORT_FORCE_100_MBIT |
1085 /* PORT_AUTO_NEG_ENABLE and PORT_AUTO_NEG_DISABLE are the same
1088 PORT_FORCE_FULL_DUPLEX | PORT_AUTO_NEG_ENABLE, ctrl);
1093 restart_mask = PORT_LED_OFF | PORT_TX_DISABLE | PORT_AUTO_NEG_RESTART |
1094 PORT_POWER_DOWN | PORT_AUTO_MDIX_DISABLE | PORT_FORCE_MDIX;
1096 if (val & KSZ886X_BMCR_DISABLE_LED)
1097 restart |= PORT_LED_OFF;
1099 if (val & KSZ886X_BMCR_DISABLE_TRANSMIT)
1100 restart |= PORT_TX_DISABLE;
1102 if (val & BMCR_ANRESTART)
1103 restart |= PORT_AUTO_NEG_RESTART;
1105 if (val & BMCR_PDOWN)
1106 restart |= PORT_POWER_DOWN;
1108 if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX)
1109 restart |= PORT_AUTO_MDIX_DISABLE;
1111 if (val & KSZ886X_BMCR_FORCE_MDI)
1112 restart |= PORT_FORCE_MDIX;
1114 if (ksz_is_ksz88x3(dev)) {
1115 restart_mask |= KSZ8873_PORT_PHY_LOOPBACK;
1117 if (val & BMCR_LOOPBACK)
1118 restart |= KSZ8873_PORT_PHY_LOOPBACK;
1120 ret = ksz879x_set_loopback(dev, port, val);
1125 return ksz_prmw8(dev, port, regs[P_NEG_RESTART_CTRL], restart_mask,
1129 int ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val)
1136 regs = dev->info->regs;
1140 ret = ksz8_w_phy_bmcr(dev, p, val);
1145 ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
1150 data &= ~(PORT_AUTO_NEG_SYM_PAUSE |
1151 PORT_AUTO_NEG_100BTX_FD |
1152 PORT_AUTO_NEG_100BTX |
1153 PORT_AUTO_NEG_10BT_FD |
1154 PORT_AUTO_NEG_10BT);
1155 if (val & ADVERTISE_PAUSE_CAP)
1156 data |= PORT_AUTO_NEG_SYM_PAUSE;
1157 if (val & ADVERTISE_100FULL)
1158 data |= PORT_AUTO_NEG_100BTX_FD;
1159 if (val & ADVERTISE_100HALF)
1160 data |= PORT_AUTO_NEG_100BTX;
1161 if (val & ADVERTISE_10FULL)
1162 data |= PORT_AUTO_NEG_10BT_FD;
1163 if (val & ADVERTISE_10HALF)
1164 data |= PORT_AUTO_NEG_10BT;
1167 ret = ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data);
1172 case PHY_REG_LINK_MD:
1173 if (val & PHY_START_CABLE_DIAG)
1174 ksz_port_cfg(dev, p, REG_PORT_LINK_MD_CTRL, PORT_START_CABLE_DIAG, true);
1177 case PHY_REG_PHY_CTRL:
1178 ret = ksz8_w_phy_ctrl(dev, p, val);
1189 void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member)
1193 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1194 data &= ~PORT_VLAN_MEMBERSHIP;
1195 data |= (member & dev->port_mask);
1196 ksz_pwrite8(dev, port, P_MIRROR_CTRL, data);
1199 void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port)
1201 u8 learn[DSA_MAX_PORTS];
1202 int first, index, cnt;
1205 regs = dev->info->regs;
1207 if ((uint)port < dev->info->port_cnt) {
1211 /* Flush all ports. */
1213 cnt = dev->info->port_cnt;
1215 for (index = first; index < cnt; index++) {
1216 ksz_pread8(dev, index, regs[P_STP_CTRL], &learn[index]);
1217 if (!(learn[index] & PORT_LEARN_DISABLE))
1218 ksz_pwrite8(dev, index, regs[P_STP_CTRL],
1219 learn[index] | PORT_LEARN_DISABLE);
1221 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
1222 for (index = first; index < cnt; index++) {
1223 if (!(learn[index] & PORT_LEARN_DISABLE))
1224 ksz_pwrite8(dev, index, regs[P_STP_CTRL], learn[index]);
1228 int ksz8_fdb_dump(struct ksz_device *dev, int port,
1229 dsa_fdb_dump_cb_t *cb, void *data)
1236 for (i = 0; i < KSZ8_DYN_MAC_ENTRIES; i++) {
1237 ret = ksz8_r_dyn_mac_table(dev, i, mac, &fid, &src_port,
1245 if (port == src_port) {
1246 ret = cb(mac, fid, false, data);
1255 static int ksz8_add_sta_mac(struct ksz_device *dev, int port,
1256 const unsigned char *addr, u16 vid)
1258 struct alu_struct alu;
1262 alu.port_forward = 0;
1263 for (index = 0; index < dev->info->num_statics; index++) {
1266 ret = ksz8_r_sta_mac_table(dev, index, &alu, &valid);
1270 /* Remember the first empty entry. */
1276 if (!memcmp(alu.mac, addr, ETH_ALEN) && alu.fid == vid)
1280 /* no available entry */
1281 if (index == dev->info->num_statics && !empty)
1285 if (index == dev->info->num_statics) {
1287 memset(&alu, 0, sizeof(alu));
1288 memcpy(alu.mac, addr, ETH_ALEN);
1289 alu.is_static = true;
1291 alu.port_forward |= BIT(port);
1293 alu.is_use_fid = true;
1295 /* Need a way to map VID to FID. */
1299 return ksz8_w_sta_mac_table(dev, index, &alu);
1302 static int ksz8_del_sta_mac(struct ksz_device *dev, int port,
1303 const unsigned char *addr, u16 vid)
1305 struct alu_struct alu;
1308 for (index = 0; index < dev->info->num_statics; index++) {
1311 ret = ksz8_r_sta_mac_table(dev, index, &alu, &valid);
1317 if (!memcmp(alu.mac, addr, ETH_ALEN) && alu.fid == vid)
1321 /* no available entry */
1322 if (index == dev->info->num_statics)
1326 alu.port_forward &= ~BIT(port);
1327 if (!alu.port_forward)
1328 alu.is_static = false;
1330 return ksz8_w_sta_mac_table(dev, index, &alu);
1333 int ksz8_mdb_add(struct ksz_device *dev, int port,
1334 const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
1336 return ksz8_add_sta_mac(dev, port, mdb->addr, mdb->vid);
1339 int ksz8_mdb_del(struct ksz_device *dev, int port,
1340 const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
1342 return ksz8_del_sta_mac(dev, port, mdb->addr, mdb->vid);
1345 int ksz8_fdb_add(struct ksz_device *dev, int port, const unsigned char *addr,
1346 u16 vid, struct dsa_db db)
1348 return ksz8_add_sta_mac(dev, port, addr, vid);
1351 int ksz8_fdb_del(struct ksz_device *dev, int port, const unsigned char *addr,
1352 u16 vid, struct dsa_db db)
1354 return ksz8_del_sta_mac(dev, port, addr, vid);
1357 int ksz8_port_vlan_filtering(struct ksz_device *dev, int port, bool flag,
1358 struct netlink_ext_ack *extack)
1360 if (ksz_is_ksz88x3(dev))
1363 /* Discard packets with VID not enabled on the switch */
1364 ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
1366 /* Discard packets with VID not enabled on the ingress port */
1367 for (port = 0; port < dev->phy_port_cnt; ++port)
1368 ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER,
1374 static void ksz8_port_enable_pvid(struct ksz_device *dev, int port, bool state)
1376 if (ksz_is_ksz88x3(dev)) {
1377 ksz_cfg(dev, REG_SW_INSERT_SRC_PVID,
1378 0x03 << (4 - 2 * port), state);
1380 ksz_pwrite8(dev, port, REG_PORT_CTRL_12, state ? 0x0f : 0x00);
1384 int ksz8_port_vlan_add(struct ksz_device *dev, int port,
1385 const struct switchdev_obj_port_vlan *vlan,
1386 struct netlink_ext_ack *extack)
1388 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1389 struct ksz_port *p = &dev->ports[port];
1390 u16 data, new_pvid = 0;
1391 u8 fid, member, valid;
1393 if (ksz_is_ksz88x3(dev))
1396 /* If a VLAN is added with untagged flag different from the
1397 * port's Remove Tag flag, we need to change the latter.
1398 * Ignore VID 0, which is always untagged.
1399 * Ignore CPU port, which will always be tagged.
1401 if (untagged != p->remove_tag && vlan->vid != 0 &&
1402 port != dev->cpu_port) {
1405 /* Reject attempts to add a VLAN that requires the
1406 * Remove Tag flag to be changed, unless there are no
1407 * other VLANs currently configured.
1409 for (vid = 1; vid < dev->info->num_vlans; ++vid) {
1410 /* Skip the VID we are going to add or reconfigure */
1411 if (vid == vlan->vid)
1414 ksz8_from_vlan(dev, dev->vlan_cache[vid].table[0],
1415 &fid, &member, &valid);
1416 if (valid && (member & BIT(port)))
1420 ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
1421 p->remove_tag = untagged;
1424 ksz8_r_vlan_table(dev, vlan->vid, &data);
1425 ksz8_from_vlan(dev, data, &fid, &member, &valid);
1427 /* First time to setup the VLAN entry. */
1429 /* Need to find a way to map VID to FID. */
1433 member |= BIT(port);
1435 ksz8_to_vlan(dev, fid, member, valid, &data);
1436 ksz8_w_vlan_table(dev, vlan->vid, data);
1439 if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
1440 new_pvid = vlan->vid;
1445 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
1446 vid &= ~VLAN_VID_MASK;
1448 ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
1450 ksz8_port_enable_pvid(dev, port, true);
1456 int ksz8_port_vlan_del(struct ksz_device *dev, int port,
1457 const struct switchdev_obj_port_vlan *vlan)
1460 u8 fid, member, valid;
1462 if (ksz_is_ksz88x3(dev))
1465 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
1466 pvid = pvid & 0xFFF;
1468 ksz8_r_vlan_table(dev, vlan->vid, &data);
1469 ksz8_from_vlan(dev, data, &fid, &member, &valid);
1471 member &= ~BIT(port);
1473 /* Invalidate the entry if no more member. */
1479 ksz8_to_vlan(dev, fid, member, valid, &data);
1480 ksz8_w_vlan_table(dev, vlan->vid, data);
1482 if (pvid == vlan->vid)
1483 ksz8_port_enable_pvid(dev, port, false);
1488 int ksz8_port_mirror_add(struct ksz_device *dev, int port,
1489 struct dsa_mall_mirror_tc_entry *mirror,
1490 bool ingress, struct netlink_ext_ack *extack)
1493 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
1494 dev->mirror_rx |= BIT(port);
1496 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
1497 dev->mirror_tx |= BIT(port);
1500 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
1502 /* configure mirror port */
1503 if (dev->mirror_rx || dev->mirror_tx)
1504 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1505 PORT_MIRROR_SNIFFER, true);
1510 void ksz8_port_mirror_del(struct ksz_device *dev, int port,
1511 struct dsa_mall_mirror_tc_entry *mirror)
1515 if (mirror->ingress) {
1516 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
1517 dev->mirror_rx &= ~BIT(port);
1519 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
1520 dev->mirror_tx &= ~BIT(port);
1523 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1525 if (!dev->mirror_rx && !dev->mirror_tx)
1526 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1527 PORT_MIRROR_SNIFFER, false);
1530 static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port)
1532 struct ksz_port *p = &dev->ports[port];
1534 if (!ksz_is_ksz87xx(dev))
1537 if (!p->interface && dev->compat_interface) {
1539 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
1540 "Please update your device tree.\n",
1542 p->interface = dev->compat_interface;
1546 void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1548 struct dsa_switch *ds = dev->ds;
1553 masks = dev->info->masks;
1555 /* enable broadcast storm limit */
1556 ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1558 /* For KSZ88x3 enable only one queue by default, otherwise we won't
1559 * be able to get rid of PCP prios on Port 2.
1561 if (ksz_is_ksz88x3(dev))
1564 queues = dev->info->num_tx_queues;
1566 ksz8_port_queue_split(dev, port, queues);
1568 /* replace priority */
1569 ksz_port_cfg(dev, port, P_802_1P_CTRL,
1570 masks[PORT_802_1P_REMAPPING], false);
1573 member = dsa_user_ports(ds);
1575 member = BIT(dsa_upstream_port(ds, port));
1577 ksz8_cfg_port_member(dev, port, member);
1580 static void ksz88x3_config_rmii_clk(struct ksz_device *dev)
1582 struct dsa_port *cpu_dp = dsa_to_port(dev->ds, dev->cpu_port);
1583 bool rmii_clk_internal;
1585 if (!ksz_is_ksz88x3(dev))
1588 rmii_clk_internal = of_property_read_bool(cpu_dp->dn,
1589 "microchip,rmii-clk-internal");
1591 ksz_cfg(dev, KSZ88X3_REG_FVID_AND_HOST_MODE,
1592 KSZ88X3_PORT3_RMII_CLK_INTERNAL, rmii_clk_internal);
1595 void ksz8_config_cpu_port(struct dsa_switch *ds)
1597 struct ksz_device *dev = ds->priv;
1604 masks = dev->info->masks;
1605 regs = dev->info->regs;
1607 ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true);
1609 ksz8_port_setup(dev, dev->cpu_port, true);
1611 ksz8795_cpu_interface_select(dev, dev->cpu_port);
1612 ksz88x3_config_rmii_clk(dev);
1614 for (i = 0; i < dev->phy_port_cnt; i++) {
1615 ksz_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1617 for (i = 0; i < dev->phy_port_cnt; i++) {
1620 if (!ksz_is_ksz88x3(dev)) {
1621 ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote);
1622 if (remote & KSZ8_PORT_FIBER_MODE)
1626 ksz_port_cfg(dev, i, regs[P_STP_CTRL],
1627 PORT_FORCE_FLOW_CTRL, true);
1629 ksz_port_cfg(dev, i, regs[P_STP_CTRL],
1630 PORT_FORCE_FLOW_CTRL, false);
1635 * ksz8_phy_port_link_up - Configures ports with integrated PHYs
1636 * @dev: The KSZ device instance.
1637 * @port: The port number to configure.
1638 * @duplex: The desired duplex mode.
1639 * @tx_pause: If true, enables transmit pause.
1640 * @rx_pause: If true, enables receive pause.
1643 * The function configures flow control settings for a given port based on the
1644 * desired settings and current duplex mode.
1646 * According to the KSZ8873 datasheet, the PORT_FORCE_FLOW_CTRL bit in the
1647 * Port Control 2 register (0x1A for Port 1, 0x22 for Port 2, 0x32 for Port 3)
1648 * determines how flow control is handled on the port:
1649 * "1 = will always enable full-duplex flow control on the port, regardless
1651 * 0 = full-duplex flow control is enabled based on AN result."
1653 * This means that the flow control behavior depends on the state of this bit:
1654 * - If PORT_FORCE_FLOW_CTRL is set to 1, the switch will ignore AN results and
1655 * force flow control on the port.
1656 * - If PORT_FORCE_FLOW_CTRL is set to 0, the switch will enable or disable
1657 * flow control based on the AN results.
1659 * However, there is a potential limitation in this configuration. It is
1660 * currently not possible to force disable flow control on a port if we still
1661 * advertise pause support. While such a configuration is not currently
1662 * supported by Linux, and may not make practical sense, it's important to be
1663 * aware of this limitation when working with the KSZ8873 and similar devices.
1665 static void ksz8_phy_port_link_up(struct ksz_device *dev, int port, int duplex,
1666 bool tx_pause, bool rx_pause)
1668 const u16 *regs = dev->info->regs;
1671 /* The KSZ8795 switch differs from the KSZ8873 by supporting
1672 * asymmetric pause control. However, since a single bit is used to
1673 * control both RX and TX pause, we can't enforce asymmetric pause
1674 * control - both TX and RX pause will be either enabled or disabled
1677 * If auto-negotiation is enabled, we usually allow the flow control to
1678 * be determined by the auto-negotiation process based on the
1679 * capabilities of both link partners. However, for KSZ8873, the
1680 * PORT_FORCE_FLOW_CTRL bit may be set by the hardware bootstrap,
1681 * ignoring the auto-negotiation result. Thus, even in auto-negotiation
1682 * mode, we need to ensure that the PORT_FORCE_FLOW_CTRL bit is
1685 * In the absence of pause auto-negotiation, we will enforce symmetric
1686 * pause control for both variants of switches - KSZ8873 and KSZ8795.
1688 * Autoneg Pause Autoneg rx,tx PORT_FORCE_FLOW_CTRL
1690 * 0 1 x 0 (flow control probably disabled)
1691 * x 0 1 1 (flow control force enabled)
1692 * 1 0 0 0 (flow control still depends on
1693 * aneg result due to hardware)
1694 * 0 0 0 0 (flow control probably disabled)
1696 if (dev->ports[port].manual_flow && tx_pause)
1697 sctrl |= PORT_FORCE_FLOW_CTRL;
1699 ksz_prmw8(dev, port, regs[P_STP_CTRL], PORT_FORCE_FLOW_CTRL, sctrl);
1703 * ksz8_cpu_port_link_up - Configures the CPU port of the switch.
1704 * @dev: The KSZ device instance.
1705 * @speed: The desired link speed.
1706 * @duplex: The desired duplex mode.
1707 * @tx_pause: If true, enables transmit pause.
1708 * @rx_pause: If true, enables receive pause.
1711 * The function configures flow control and speed settings for the CPU
1712 * port of the switch based on the desired settings, current duplex mode, and
1715 static void ksz8_cpu_port_link_up(struct ksz_device *dev, int speed, int duplex,
1716 bool tx_pause, bool rx_pause)
1718 const u16 *regs = dev->info->regs;
1721 /* SW_FLOW_CTRL, SW_HALF_DUPLEX, and SW_10_MBIT bits are bootstrappable
1722 * at least on KSZ8873. They can have different values depending on your
1725 if (tx_pause || rx_pause)
1726 ctrl |= SW_FLOW_CTRL;
1728 if (duplex == DUPLEX_HALF)
1729 ctrl |= SW_HALF_DUPLEX;
1731 /* This hardware only supports SPEED_10 and SPEED_100. For SPEED_10
1732 * we need to set the SW_10_MBIT bit. Otherwise, we can leave it 0.
1734 if (speed == SPEED_10)
1737 ksz_rmw8(dev, regs[S_BROADCAST_CTRL], SW_HALF_DUPLEX | SW_FLOW_CTRL |
1741 void ksz8_phylink_mac_link_up(struct phylink_config *config,
1742 struct phy_device *phydev, unsigned int mode,
1743 phy_interface_t interface, int speed, int duplex,
1744 bool tx_pause, bool rx_pause)
1746 struct dsa_port *dp = dsa_phylink_to_port(config);
1747 struct ksz_device *dev = dp->ds->priv;
1748 int port = dp->index;
1750 /* If the port is the CPU port, apply special handling. Only the CPU
1751 * port is configured via global registers.
1753 if (dev->cpu_port == port)
1754 ksz8_cpu_port_link_up(dev, speed, duplex, tx_pause, rx_pause);
1755 else if (dev->info->internal_phy[port])
1756 ksz8_phy_port_link_up(dev, port, duplex, tx_pause, rx_pause);
1759 static int ksz8_handle_global_errata(struct dsa_switch *ds)
1761 struct ksz_device *dev = ds->priv;
1764 /* KSZ87xx Errata DS80000687C.
1765 * Module 2: Link drops with some EEE link partners.
1766 * An issue with the EEE next page exchange between the
1767 * KSZ879x/KSZ877x/KSZ876x and some EEE link partners may result in
1768 * the link dropping.
1770 if (dev->info->ksz87xx_eee_link_erratum)
1771 ret = ksz8_ind_write8(dev, TABLE_EEE, REG_IND_EEE_GLOB2_HI, 0);
1776 int ksz8_enable_stp_addr(struct ksz_device *dev)
1778 struct alu_struct alu;
1780 /* Setup STP address for STP operation. */
1781 memset(&alu, 0, sizeof(alu));
1782 ether_addr_copy(alu.mac, eth_stp_addr);
1783 alu.is_static = true;
1784 alu.is_override = true;
1785 alu.port_forward = dev->info->cpu_ports;
1787 return ksz8_w_sta_mac_table(dev, 0, &alu);
1790 int ksz8_setup(struct dsa_switch *ds)
1792 struct ksz_device *dev = ds->priv;
1795 ds->mtu_enforcement_ingress = true;
1797 /* We rely on software untagging on the CPU port, so that we
1798 * can support both tagged and untagged VLANs
1800 ds->untag_bridge_pvid = true;
1802 /* VLAN filtering is partly controlled by the global VLAN
1805 ds->vlan_filtering_is_global = true;
1807 /* Enable automatic fast aging when link changed detected. */
1808 ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
1810 /* Enable aggressive back off algorithm in half duplex mode. */
1811 regmap_update_bits(ksz_regmap_8(dev), REG_SW_CTRL_1,
1812 SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
1815 * Make sure unicast VLAN boundary is set as default and
1816 * enable no excessive collision drop.
1818 regmap_update_bits(ksz_regmap_8(dev), REG_SW_CTRL_2,
1819 UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
1820 UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
1822 ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
1824 ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1826 if (!ksz_is_ksz88x3(dev))
1827 ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true);
1829 for (i = 0; i < (dev->info->num_vlans / 4); i++)
1830 ksz8_r_vlan_entries(dev, i);
1832 return ksz8_handle_global_errata(ds);
1835 void ksz8_get_caps(struct ksz_device *dev, int port,
1836 struct phylink_config *config)
1838 config->mac_capabilities = MAC_10 | MAC_100;
1840 /* Silicon Errata Sheet (DS80000830A):
1841 * "Port 1 does not respond to received flow control PAUSE frames"
1842 * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3
1845 if (!ksz_is_ksz88x3(dev) || port)
1846 config->mac_capabilities |= MAC_SYM_PAUSE;
1848 /* Asym pause is not supported on KSZ8863 and KSZ8873 */
1849 if (!ksz_is_ksz88x3(dev))
1850 config->mac_capabilities |= MAC_ASYM_PAUSE;
1853 u32 ksz8_get_port_addr(int port, int offset)
1855 return PORT_CTRL_ADDR(port, offset);
1858 int ksz8_switch_init(struct ksz_device *dev)
1860 dev->cpu_port = fls(dev->info->cpu_ports) - 1;
1861 dev->phy_port_cnt = dev->info->port_cnt - 1;
1862 dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | dev->info->cpu_ports;
1867 void ksz8_switch_exit(struct ksz_device *dev)
1869 ksz8_reset_switch(dev);
1873 MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
1874 MODULE_LICENSE("GPL");