1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip KSZ9477 switch driver main logic
5 * Copyright (C) 2017-2019 Microchip Technology Inc.
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/iopoll.h>
11 #include <linux/platform_data/microchip-ksz.h>
12 #include <linux/phy.h>
13 #include <linux/if_bridge.h>
14 #include <linux/if_vlan.h>
16 #include <net/switchdev.h>
18 #include "ksz9477_reg.h"
19 #include "ksz_common.h"
21 /* Used with variable features to indicate capabilities. */
22 #define GBIT_SUPPORT BIT(0)
23 #define NEW_XMII BIT(1)
24 #define IS_9893 BIT(2)
28 char string[ETH_GSTRING_LEN];
29 } ksz9477_mib_names[TOTAL_SWITCH_COUNTER_NUM] = {
31 { 0x01, "rx_undersize" },
32 { 0x02, "rx_fragments" },
33 { 0x03, "rx_oversize" },
34 { 0x04, "rx_jabbers" },
35 { 0x05, "rx_symbol_err" },
36 { 0x06, "rx_crc_err" },
37 { 0x07, "rx_align_err" },
38 { 0x08, "rx_mac_ctrl" },
43 { 0x0D, "rx_64_or_less" },
44 { 0x0E, "rx_65_127" },
45 { 0x0F, "rx_128_255" },
46 { 0x10, "rx_256_511" },
47 { 0x11, "rx_512_1023" },
48 { 0x12, "rx_1024_1522" },
49 { 0x13, "rx_1523_2000" },
52 { 0x16, "tx_late_col" },
57 { 0x1B, "tx_deferred" },
58 { 0x1C, "tx_total_col" },
59 { 0x1D, "tx_exc_col" },
60 { 0x1E, "tx_single_col" },
61 { 0x1F, "tx_mult_col" },
64 { 0x82, "rx_discards" },
65 { 0x83, "tx_discards" },
68 struct ksz9477_stats_raw {
107 static void ksz9477_r_mib_stats64(struct ksz_device *dev, int port)
109 struct rtnl_link_stats64 *stats;
110 struct ksz9477_stats_raw *raw;
111 struct ksz_port_mib *mib;
113 mib = &dev->ports[port].mib;
114 stats = &mib->stats64;
115 raw = (struct ksz9477_stats_raw *)mib->counters;
117 spin_lock(&mib->stats64_lock);
119 stats->rx_packets = raw->rx_bcast + raw->rx_mcast + raw->rx_ucast;
120 stats->tx_packets = raw->tx_bcast + raw->tx_mcast + raw->tx_ucast;
122 /* HW counters are counting bytes + FCS which is not acceptable
123 * for rtnl_link_stats64 interface
125 stats->rx_bytes = raw->rx_total - stats->rx_packets * ETH_FCS_LEN;
126 stats->tx_bytes = raw->tx_total - stats->tx_packets * ETH_FCS_LEN;
128 stats->rx_length_errors = raw->rx_undersize + raw->rx_fragments +
131 stats->rx_crc_errors = raw->rx_crc_err;
132 stats->rx_frame_errors = raw->rx_align_err;
133 stats->rx_dropped = raw->rx_discards;
134 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
135 stats->rx_frame_errors + stats->rx_dropped;
137 stats->tx_window_errors = raw->tx_late_col;
138 stats->tx_fifo_errors = raw->tx_discards;
139 stats->tx_aborted_errors = raw->tx_exc_col;
140 stats->tx_errors = stats->tx_window_errors + stats->tx_fifo_errors +
141 stats->tx_aborted_errors;
143 stats->multicast = raw->rx_mcast;
144 stats->collisions = raw->tx_total_col;
146 spin_unlock(&mib->stats64_lock);
149 static void ksz9477_get_stats64(struct dsa_switch *ds, int port,
150 struct rtnl_link_stats64 *s)
152 struct ksz_device *dev = ds->priv;
153 struct ksz_port_mib *mib;
155 mib = &dev->ports[port].mib;
157 spin_lock(&mib->stats64_lock);
158 memcpy(s, &mib->stats64, sizeof(*s));
159 spin_unlock(&mib->stats64_lock);
162 static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
164 regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
167 static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
170 regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
171 bits, set ? bits : 0);
174 static void ksz9477_cfg32(struct ksz_device *dev, u32 addr, u32 bits, bool set)
176 regmap_update_bits(dev->regmap[2], addr, bits, set ? bits : 0);
179 static void ksz9477_port_cfg32(struct ksz_device *dev, int port, int offset,
182 regmap_update_bits(dev->regmap[2], PORT_CTRL_ADDR(port, offset),
183 bits, set ? bits : 0);
186 static int ksz9477_change_mtu(struct dsa_switch *ds, int port, int mtu)
188 struct ksz_device *dev = ds->priv;
189 u16 frame_size, max_frame = 0;
192 frame_size = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
194 /* Cache the per-port MTU setting */
195 dev->ports[port].max_frame = frame_size;
197 for (i = 0; i < dev->port_cnt; i++)
198 max_frame = max(max_frame, dev->ports[i].max_frame);
200 return regmap_update_bits(dev->regmap[1], REG_SW_MTU__2,
201 REG_SW_MTU_MASK, max_frame);
204 static int ksz9477_max_mtu(struct dsa_switch *ds, int port)
206 return KSZ9477_MAX_FRAME_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN;
209 static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev)
213 return regmap_read_poll_timeout(dev->regmap[0], REG_SW_VLAN_CTRL,
214 val, !(val & VLAN_START), 10, 1000);
217 static int ksz9477_get_vlan_table(struct ksz_device *dev, u16 vid,
222 mutex_lock(&dev->vlan_mutex);
224 ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
225 ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START);
227 /* wait to be cleared */
228 ret = ksz9477_wait_vlan_ctrl_ready(dev);
230 dev_dbg(dev->dev, "Failed to read vlan table\n");
234 ksz_read32(dev, REG_SW_VLAN_ENTRY__4, &vlan_table[0]);
235 ksz_read32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, &vlan_table[1]);
236 ksz_read32(dev, REG_SW_VLAN_ENTRY_PORTS__4, &vlan_table[2]);
238 ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
241 mutex_unlock(&dev->vlan_mutex);
246 static int ksz9477_set_vlan_table(struct ksz_device *dev, u16 vid,
251 mutex_lock(&dev->vlan_mutex);
253 ksz_write32(dev, REG_SW_VLAN_ENTRY__4, vlan_table[0]);
254 ksz_write32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, vlan_table[1]);
255 ksz_write32(dev, REG_SW_VLAN_ENTRY_PORTS__4, vlan_table[2]);
257 ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
258 ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE);
260 /* wait to be cleared */
261 ret = ksz9477_wait_vlan_ctrl_ready(dev);
263 dev_dbg(dev->dev, "Failed to write vlan table\n");
267 ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
269 /* update vlan cache table */
270 dev->vlan_cache[vid].table[0] = vlan_table[0];
271 dev->vlan_cache[vid].table[1] = vlan_table[1];
272 dev->vlan_cache[vid].table[2] = vlan_table[2];
275 mutex_unlock(&dev->vlan_mutex);
280 static void ksz9477_read_table(struct ksz_device *dev, u32 *table)
282 ksz_read32(dev, REG_SW_ALU_VAL_A, &table[0]);
283 ksz_read32(dev, REG_SW_ALU_VAL_B, &table[1]);
284 ksz_read32(dev, REG_SW_ALU_VAL_C, &table[2]);
285 ksz_read32(dev, REG_SW_ALU_VAL_D, &table[3]);
288 static void ksz9477_write_table(struct ksz_device *dev, u32 *table)
290 ksz_write32(dev, REG_SW_ALU_VAL_A, table[0]);
291 ksz_write32(dev, REG_SW_ALU_VAL_B, table[1]);
292 ksz_write32(dev, REG_SW_ALU_VAL_C, table[2]);
293 ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]);
296 static int ksz9477_wait_alu_ready(struct ksz_device *dev)
300 return regmap_read_poll_timeout(dev->regmap[2], REG_SW_ALU_CTRL__4,
301 val, !(val & ALU_START), 10, 1000);
304 static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev)
308 return regmap_read_poll_timeout(dev->regmap[2],
309 REG_SW_ALU_STAT_CTRL__4,
310 val, !(val & ALU_STAT_START),
314 static int ksz9477_reset_switch(struct ksz_device *dev)
320 ksz_cfg(dev, REG_SW_OPERATION, SW_RESET, true);
322 /* turn off SPI DO Edge select */
323 regmap_update_bits(dev->regmap[0], REG_SW_GLOBAL_SERIAL_CTRL_0,
324 SPI_AUTO_EDGE_DETECTION, 0);
326 /* default configuration */
327 ksz_read8(dev, REG_SW_LUE_CTRL_1, &data8);
328 data8 = SW_AGING_ENABLE | SW_LINK_AUTO_AGING |
329 SW_SRC_ADDR_FILTER | SW_FLUSH_STP_TABLE | SW_FLUSH_MSTP_TABLE;
330 ksz_write8(dev, REG_SW_LUE_CTRL_1, data8);
332 /* disable interrupts */
333 ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK);
334 ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0x7F);
335 ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
337 /* set broadcast storm protection 10% rate */
338 regmap_update_bits(dev->regmap[1], REG_SW_MAC_CTRL_2,
339 BROADCAST_STORM_RATE,
340 (BROADCAST_STORM_VALUE *
341 BROADCAST_STORM_PROT_RATE) / 100);
343 data8 = SW_ENABLE_REFCLKO;
344 if (dev->synclko_disable)
346 else if (dev->synclko_125)
347 data8 = SW_ENABLE_REFCLKO | SW_REFCLKO_IS_125MHZ;
348 ksz_write8(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1, data8);
353 static void ksz9477_r_mib_cnt(struct ksz_device *dev, int port, u16 addr,
356 struct ksz_port *p = &dev->ports[port];
361 /* retain the flush/freeze bit */
362 data = p->freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
363 data |= MIB_COUNTER_READ;
364 data |= (addr << MIB_COUNTER_INDEX_S);
365 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, data);
367 ret = regmap_read_poll_timeout(dev->regmap[2],
368 PORT_CTRL_ADDR(port, REG_PORT_MIB_CTRL_STAT__4),
369 val, !(val & MIB_COUNTER_READ), 10, 1000);
370 /* failed to read MIB. get out of loop */
372 dev_dbg(dev->dev, "Failed to get MIB\n");
376 /* count resets upon read */
377 ksz_pread32(dev, port, REG_PORT_MIB_DATA, &data);
381 static void ksz9477_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
382 u64 *dropped, u64 *cnt)
384 addr = ksz9477_mib_names[addr].index;
385 ksz9477_r_mib_cnt(dev, port, addr, cnt);
388 static void ksz9477_freeze_mib(struct ksz_device *dev, int port, bool freeze)
390 u32 val = freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
391 struct ksz_port *p = &dev->ports[port];
393 /* enable/disable the port for flush/freeze function */
394 mutex_lock(&p->mib.cnt_mutex);
395 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, val);
397 /* used by MIB counter reading code to know freeze is enabled */
399 mutex_unlock(&p->mib.cnt_mutex);
402 static void ksz9477_port_init_cnt(struct ksz_device *dev, int port)
404 struct ksz_port_mib *mib = &dev->ports[port].mib;
406 /* flush all enabled port MIB counters */
407 mutex_lock(&mib->cnt_mutex);
408 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4,
409 MIB_COUNTER_FLUSH_FREEZE);
410 ksz_write8(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FLUSH);
411 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, 0);
412 mutex_unlock(&mib->cnt_mutex);
415 memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
418 static enum dsa_tag_protocol ksz9477_get_tag_protocol(struct dsa_switch *ds,
420 enum dsa_tag_protocol mp)
422 enum dsa_tag_protocol proto = DSA_TAG_PROTO_KSZ9477;
423 struct ksz_device *dev = ds->priv;
425 if (dev->features & IS_9893)
426 proto = DSA_TAG_PROTO_KSZ9893;
430 static int ksz9477_phy_read16(struct dsa_switch *ds, int addr, int reg)
432 struct ksz_device *dev = ds->priv;
435 /* No real PHY after this. Simulate the PHY.
436 * A fixed PHY can be setup in the device tree, but this function is
437 * still called for that port during initialization.
438 * For RGMII PHY there is no way to access it so the fixed PHY should
439 * be used. For SGMII PHY the supporting code will be added later.
441 if (addr >= dev->phy_port_cnt) {
442 struct ksz_port *p = &dev->ports[addr];
467 if (p->phydev.speed == SPEED_1000)
474 ksz_pread16(dev, addr, 0x100 + (reg << 1), &val);
480 static int ksz9477_phy_write16(struct dsa_switch *ds, int addr, int reg,
483 struct ksz_device *dev = ds->priv;
485 /* No real PHY after this. */
486 if (addr >= dev->phy_port_cnt)
489 /* No gigabit support. Do not write to this register. */
490 if (!(dev->features & GBIT_SUPPORT) && reg == MII_CTRL1000)
492 ksz_pwrite16(dev, addr, 0x100 + (reg << 1), val);
497 static void ksz9477_get_strings(struct dsa_switch *ds, int port,
498 u32 stringset, uint8_t *buf)
502 if (stringset != ETH_SS_STATS)
505 for (i = 0; i < TOTAL_SWITCH_COUNTER_NUM; i++) {
506 memcpy(buf + i * ETH_GSTRING_LEN, ksz9477_mib_names[i].string,
511 static void ksz9477_cfg_port_member(struct ksz_device *dev, int port,
514 ksz_pwrite32(dev, port, REG_PORT_VLAN_MEMBERSHIP__4, member);
517 static void ksz9477_port_stp_state_set(struct dsa_switch *ds, int port,
520 struct ksz_device *dev = ds->priv;
521 struct ksz_port *p = &dev->ports[port];
524 ksz_pread8(dev, port, P_STP_CTRL, &data);
525 data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
528 case BR_STATE_DISABLED:
529 data |= PORT_LEARN_DISABLE;
531 case BR_STATE_LISTENING:
532 data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE);
534 case BR_STATE_LEARNING:
535 data |= PORT_RX_ENABLE;
537 case BR_STATE_FORWARDING:
538 data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
540 case BR_STATE_BLOCKING:
541 data |= PORT_LEARN_DISABLE;
544 dev_err(ds->dev, "invalid STP state: %d\n", state);
548 ksz_pwrite8(dev, port, P_STP_CTRL, data);
549 p->stp_state = state;
551 ksz_update_port_member(dev, port);
554 static void ksz9477_flush_dyn_mac_table(struct ksz_device *dev, int port)
558 regmap_update_bits(dev->regmap[0], REG_SW_LUE_CTRL_2,
559 SW_FLUSH_OPTION_M << SW_FLUSH_OPTION_S,
560 SW_FLUSH_OPTION_DYN_MAC << SW_FLUSH_OPTION_S);
562 if (port < dev->port_cnt) {
563 /* flush individual port */
564 ksz_pread8(dev, port, P_STP_CTRL, &data);
565 if (!(data & PORT_LEARN_DISABLE))
566 ksz_pwrite8(dev, port, P_STP_CTRL,
567 data | PORT_LEARN_DISABLE);
568 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
569 ksz_pwrite8(dev, port, P_STP_CTRL, data);
572 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_STP_TABLE, true);
576 static int ksz9477_port_vlan_filtering(struct dsa_switch *ds, int port,
578 struct netlink_ext_ack *extack)
580 struct ksz_device *dev = ds->priv;
583 ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
584 PORT_VLAN_LOOKUP_VID_0, true);
585 ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, true);
587 ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, false);
588 ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
589 PORT_VLAN_LOOKUP_VID_0, false);
595 static int ksz9477_port_vlan_add(struct dsa_switch *ds, int port,
596 const struct switchdev_obj_port_vlan *vlan,
597 struct netlink_ext_ack *extack)
599 struct ksz_device *dev = ds->priv;
601 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
604 err = ksz9477_get_vlan_table(dev, vlan->vid, vlan_table);
606 NL_SET_ERR_MSG_MOD(extack, "Failed to get vlan table");
610 vlan_table[0] = VLAN_VALID | (vlan->vid & VLAN_FID_M);
612 vlan_table[1] |= BIT(port);
614 vlan_table[1] &= ~BIT(port);
615 vlan_table[1] &= ~(BIT(dev->cpu_port));
617 vlan_table[2] |= BIT(port) | BIT(dev->cpu_port);
619 err = ksz9477_set_vlan_table(dev, vlan->vid, vlan_table);
621 NL_SET_ERR_MSG_MOD(extack, "Failed to set vlan table");
626 if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
627 ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, vlan->vid);
632 static int ksz9477_port_vlan_del(struct dsa_switch *ds, int port,
633 const struct switchdev_obj_port_vlan *vlan)
635 struct ksz_device *dev = ds->priv;
636 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
640 ksz_pread16(dev, port, REG_PORT_DEFAULT_VID, &pvid);
643 if (ksz9477_get_vlan_table(dev, vlan->vid, vlan_table)) {
644 dev_dbg(dev->dev, "Failed to get vlan table\n");
648 vlan_table[2] &= ~BIT(port);
650 if (pvid == vlan->vid)
654 vlan_table[1] &= ~BIT(port);
656 if (ksz9477_set_vlan_table(dev, vlan->vid, vlan_table)) {
657 dev_dbg(dev->dev, "Failed to set vlan table\n");
661 ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, pvid);
666 static int ksz9477_port_fdb_add(struct dsa_switch *ds, int port,
667 const unsigned char *addr, u16 vid,
670 struct ksz_device *dev = ds->priv;
675 mutex_lock(&dev->alu_mutex);
677 /* find any entry with mac & vid */
678 data = vid << ALU_FID_INDEX_S;
679 data |= ((addr[0] << 8) | addr[1]);
680 ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
682 data = ((addr[2] << 24) | (addr[3] << 16));
683 data |= ((addr[4] << 8) | addr[5]);
684 ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
686 /* start read operation */
687 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
689 /* wait to be finished */
690 ret = ksz9477_wait_alu_ready(dev);
692 dev_dbg(dev->dev, "Failed to read ALU\n");
697 ksz9477_read_table(dev, alu_table);
699 /* update ALU entry */
700 alu_table[0] = ALU_V_STATIC_VALID;
701 alu_table[1] |= BIT(port);
703 alu_table[1] |= ALU_V_USE_FID;
704 alu_table[2] = (vid << ALU_V_FID_S);
705 alu_table[2] |= ((addr[0] << 8) | addr[1]);
706 alu_table[3] = ((addr[2] << 24) | (addr[3] << 16));
707 alu_table[3] |= ((addr[4] << 8) | addr[5]);
709 ksz9477_write_table(dev, alu_table);
711 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
713 /* wait to be finished */
714 ret = ksz9477_wait_alu_ready(dev);
716 dev_dbg(dev->dev, "Failed to write ALU\n");
719 mutex_unlock(&dev->alu_mutex);
724 static int ksz9477_port_fdb_del(struct dsa_switch *ds, int port,
725 const unsigned char *addr, u16 vid,
728 struct ksz_device *dev = ds->priv;
733 mutex_lock(&dev->alu_mutex);
735 /* read any entry with mac & vid */
736 data = vid << ALU_FID_INDEX_S;
737 data |= ((addr[0] << 8) | addr[1]);
738 ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
740 data = ((addr[2] << 24) | (addr[3] << 16));
741 data |= ((addr[4] << 8) | addr[5]);
742 ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
744 /* start read operation */
745 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
747 /* wait to be finished */
748 ret = ksz9477_wait_alu_ready(dev);
750 dev_dbg(dev->dev, "Failed to read ALU\n");
754 ksz_read32(dev, REG_SW_ALU_VAL_A, &alu_table[0]);
755 if (alu_table[0] & ALU_V_STATIC_VALID) {
756 ksz_read32(dev, REG_SW_ALU_VAL_B, &alu_table[1]);
757 ksz_read32(dev, REG_SW_ALU_VAL_C, &alu_table[2]);
758 ksz_read32(dev, REG_SW_ALU_VAL_D, &alu_table[3]);
760 /* clear forwarding port */
761 alu_table[2] &= ~BIT(port);
763 /* if there is no port to forward, clear table */
764 if ((alu_table[2] & ALU_V_PORT_MAP) == 0) {
777 ksz9477_write_table(dev, alu_table);
779 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
781 /* wait to be finished */
782 ret = ksz9477_wait_alu_ready(dev);
784 dev_dbg(dev->dev, "Failed to write ALU\n");
787 mutex_unlock(&dev->alu_mutex);
792 static void ksz9477_convert_alu(struct alu_struct *alu, u32 *alu_table)
794 alu->is_static = !!(alu_table[0] & ALU_V_STATIC_VALID);
795 alu->is_src_filter = !!(alu_table[0] & ALU_V_SRC_FILTER);
796 alu->is_dst_filter = !!(alu_table[0] & ALU_V_DST_FILTER);
797 alu->prio_age = (alu_table[0] >> ALU_V_PRIO_AGE_CNT_S) &
798 ALU_V_PRIO_AGE_CNT_M;
799 alu->mstp = alu_table[0] & ALU_V_MSTP_M;
801 alu->is_override = !!(alu_table[1] & ALU_V_OVERRIDE);
802 alu->is_use_fid = !!(alu_table[1] & ALU_V_USE_FID);
803 alu->port_forward = alu_table[1] & ALU_V_PORT_MAP;
805 alu->fid = (alu_table[2] >> ALU_V_FID_S) & ALU_V_FID_M;
807 alu->mac[0] = (alu_table[2] >> 8) & 0xFF;
808 alu->mac[1] = alu_table[2] & 0xFF;
809 alu->mac[2] = (alu_table[3] >> 24) & 0xFF;
810 alu->mac[3] = (alu_table[3] >> 16) & 0xFF;
811 alu->mac[4] = (alu_table[3] >> 8) & 0xFF;
812 alu->mac[5] = alu_table[3] & 0xFF;
815 static int ksz9477_port_fdb_dump(struct dsa_switch *ds, int port,
816 dsa_fdb_dump_cb_t *cb, void *data)
818 struct ksz_device *dev = ds->priv;
822 struct alu_struct alu;
825 mutex_lock(&dev->alu_mutex);
827 /* start ALU search */
828 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_START | ALU_SEARCH);
833 ksz_read32(dev, REG_SW_ALU_CTRL__4, &ksz_data);
834 if ((ksz_data & ALU_VALID) || !(ksz_data & ALU_START))
837 } while (timeout-- > 0);
840 dev_dbg(dev->dev, "Failed to search ALU\n");
846 ksz9477_read_table(dev, alu_table);
848 ksz9477_convert_alu(&alu, alu_table);
850 if (alu.port_forward & BIT(port)) {
851 ret = cb(alu.mac, alu.fid, alu.is_static, data);
855 } while (ksz_data & ALU_START);
859 /* stop ALU search */
860 ksz_write32(dev, REG_SW_ALU_CTRL__4, 0);
862 mutex_unlock(&dev->alu_mutex);
867 static int ksz9477_port_mdb_add(struct dsa_switch *ds, int port,
868 const struct switchdev_obj_port_mdb *mdb,
871 struct ksz_device *dev = ds->priv;
878 mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
879 mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
880 mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
882 mutex_lock(&dev->alu_mutex);
884 for (index = 0; index < dev->num_statics; index++) {
885 /* find empty slot first */
886 data = (index << ALU_STAT_INDEX_S) |
887 ALU_STAT_READ | ALU_STAT_START;
888 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
890 /* wait to be finished */
891 err = ksz9477_wait_alu_sta_ready(dev);
893 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
897 /* read ALU static table */
898 ksz9477_read_table(dev, static_table);
900 if (static_table[0] & ALU_V_STATIC_VALID) {
901 /* check this has same vid & mac address */
902 if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
903 ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
904 static_table[3] == mac_lo) {
905 /* found matching one */
909 /* found empty one */
914 /* no available entry */
915 if (index == dev->num_statics) {
921 static_table[0] = ALU_V_STATIC_VALID;
922 static_table[1] |= BIT(port);
924 static_table[1] |= ALU_V_USE_FID;
925 static_table[2] = (mdb->vid << ALU_V_FID_S);
926 static_table[2] |= mac_hi;
927 static_table[3] = mac_lo;
929 ksz9477_write_table(dev, static_table);
931 data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
932 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
934 /* wait to be finished */
935 if (ksz9477_wait_alu_sta_ready(dev))
936 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
939 mutex_unlock(&dev->alu_mutex);
943 static int ksz9477_port_mdb_del(struct dsa_switch *ds, int port,
944 const struct switchdev_obj_port_mdb *mdb,
947 struct ksz_device *dev = ds->priv;
954 mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
955 mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
956 mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
958 mutex_lock(&dev->alu_mutex);
960 for (index = 0; index < dev->num_statics; index++) {
961 /* find empty slot first */
962 data = (index << ALU_STAT_INDEX_S) |
963 ALU_STAT_READ | ALU_STAT_START;
964 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
966 /* wait to be finished */
967 ret = ksz9477_wait_alu_sta_ready(dev);
969 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
973 /* read ALU static table */
974 ksz9477_read_table(dev, static_table);
976 if (static_table[0] & ALU_V_STATIC_VALID) {
977 /* check this has same vid & mac address */
979 if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
980 ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
981 static_table[3] == mac_lo) {
982 /* found matching one */
988 /* no available entry */
989 if (index == dev->num_statics)
993 static_table[1] &= ~BIT(port);
995 if ((static_table[1] & ALU_V_PORT_MAP) == 0) {
1000 static_table[3] = 0;
1003 ksz9477_write_table(dev, static_table);
1005 data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
1006 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
1008 /* wait to be finished */
1009 ret = ksz9477_wait_alu_sta_ready(dev);
1011 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
1014 mutex_unlock(&dev->alu_mutex);
1019 static int ksz9477_port_mirror_add(struct dsa_switch *ds, int port,
1020 struct dsa_mall_mirror_tc_entry *mirror,
1021 bool ingress, struct netlink_ext_ack *extack)
1023 struct ksz_device *dev = ds->priv;
1026 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
1028 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
1030 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
1032 /* configure mirror port */
1033 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1034 PORT_MIRROR_SNIFFER, true);
1036 ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1041 static void ksz9477_port_mirror_del(struct dsa_switch *ds, int port,
1042 struct dsa_mall_mirror_tc_entry *mirror)
1044 struct ksz_device *dev = ds->priv;
1047 if (mirror->ingress)
1048 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
1050 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
1052 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1054 if (!(data & (PORT_MIRROR_RX | PORT_MIRROR_TX)))
1055 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1056 PORT_MIRROR_SNIFFER, false);
1059 static bool ksz9477_get_gbit(struct ksz_device *dev, u8 data)
1063 if (dev->features & NEW_XMII)
1064 gbit = !(data & PORT_MII_NOT_1GBIT);
1066 gbit = !!(data & PORT_MII_1000MBIT_S1);
1070 static void ksz9477_set_gbit(struct ksz_device *dev, bool gbit, u8 *data)
1072 if (dev->features & NEW_XMII) {
1074 *data &= ~PORT_MII_NOT_1GBIT;
1076 *data |= PORT_MII_NOT_1GBIT;
1079 *data |= PORT_MII_1000MBIT_S1;
1081 *data &= ~PORT_MII_1000MBIT_S1;
1085 static int ksz9477_get_xmii(struct ksz_device *dev, u8 data)
1089 if (dev->features & NEW_XMII) {
1090 switch (data & PORT_MII_SEL_M) {
1104 switch (data & PORT_MII_SEL_M) {
1105 case PORT_MII_SEL_S1:
1108 case PORT_RMII_SEL_S1:
1111 case PORT_GMII_SEL_S1:
1121 static void ksz9477_set_xmii(struct ksz_device *dev, int mode, u8 *data)
1125 if (dev->features & NEW_XMII) {
1128 xmii = PORT_MII_SEL;
1131 xmii = PORT_RMII_SEL;
1134 xmii = PORT_GMII_SEL;
1137 xmii = PORT_RGMII_SEL;
1143 xmii = PORT_MII_SEL_S1;
1146 xmii = PORT_RMII_SEL_S1;
1149 xmii = PORT_GMII_SEL_S1;
1152 xmii = PORT_RGMII_SEL_S1;
1156 *data &= ~PORT_MII_SEL_M;
1160 static phy_interface_t ksz9477_get_interface(struct ksz_device *dev, int port)
1162 phy_interface_t interface;
1167 if (port < dev->phy_port_cnt)
1168 return PHY_INTERFACE_MODE_NA;
1169 ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
1170 gbit = ksz9477_get_gbit(dev, data8);
1171 mode = ksz9477_get_xmii(dev, data8);
1174 interface = PHY_INTERFACE_MODE_GMII;
1179 interface = PHY_INTERFACE_MODE_MII;
1182 interface = PHY_INTERFACE_MODE_RMII;
1185 interface = PHY_INTERFACE_MODE_RGMII;
1186 if (data8 & PORT_RGMII_ID_EG_ENABLE)
1187 interface = PHY_INTERFACE_MODE_RGMII_TXID;
1188 if (data8 & PORT_RGMII_ID_IG_ENABLE) {
1189 interface = PHY_INTERFACE_MODE_RGMII_RXID;
1190 if (data8 & PORT_RGMII_ID_EG_ENABLE)
1191 interface = PHY_INTERFACE_MODE_RGMII_ID;
1198 static void ksz9477_port_mmd_write(struct ksz_device *dev, int port,
1199 u8 dev_addr, u16 reg_addr, u16 val)
1201 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP,
1202 MMD_SETUP(PORT_MMD_OP_INDEX, dev_addr));
1203 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, reg_addr);
1204 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP,
1205 MMD_SETUP(PORT_MMD_OP_DATA_NO_INCR, dev_addr));
1206 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, val);
1209 static void ksz9477_phy_errata_setup(struct ksz_device *dev, int port)
1211 /* Apply PHY settings to address errata listed in
1212 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
1213 * Silicon Errata and Data Sheet Clarification documents:
1215 * Register settings are needed to improve PHY receive performance
1217 ksz9477_port_mmd_write(dev, port, 0x01, 0x6f, 0xdd0b);
1218 ksz9477_port_mmd_write(dev, port, 0x01, 0x8f, 0x6032);
1219 ksz9477_port_mmd_write(dev, port, 0x01, 0x9d, 0x248c);
1220 ksz9477_port_mmd_write(dev, port, 0x01, 0x75, 0x0060);
1221 ksz9477_port_mmd_write(dev, port, 0x01, 0xd3, 0x7777);
1222 ksz9477_port_mmd_write(dev, port, 0x1c, 0x06, 0x3008);
1223 ksz9477_port_mmd_write(dev, port, 0x1c, 0x08, 0x2001);
1225 /* Transmit waveform amplitude can be improved
1226 * (1000BASE-T, 100BASE-TX, 10BASE-Te)
1228 ksz9477_port_mmd_write(dev, port, 0x1c, 0x04, 0x00d0);
1230 /* Energy Efficient Ethernet (EEE) feature select must
1231 * be manually disabled (except on KSZ8565 which is 100Mbit)
1233 if (dev->features & GBIT_SUPPORT)
1234 ksz9477_port_mmd_write(dev, port, 0x07, 0x3c, 0x0000);
1236 /* Register settings are required to meet data sheet
1237 * supply current specifications
1239 ksz9477_port_mmd_write(dev, port, 0x1c, 0x13, 0x6eff);
1240 ksz9477_port_mmd_write(dev, port, 0x1c, 0x14, 0xe6ff);
1241 ksz9477_port_mmd_write(dev, port, 0x1c, 0x15, 0x6eff);
1242 ksz9477_port_mmd_write(dev, port, 0x1c, 0x16, 0xe6ff);
1243 ksz9477_port_mmd_write(dev, port, 0x1c, 0x17, 0x00ff);
1244 ksz9477_port_mmd_write(dev, port, 0x1c, 0x18, 0x43ff);
1245 ksz9477_port_mmd_write(dev, port, 0x1c, 0x19, 0xc3ff);
1246 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1a, 0x6fff);
1247 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1b, 0x07ff);
1248 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1c, 0x0fff);
1249 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1d, 0xe7ff);
1250 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1e, 0xefff);
1251 ksz9477_port_mmd_write(dev, port, 0x1c, 0x20, 0xeeee);
1254 static void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1256 struct ksz_port *p = &dev->ports[port];
1257 struct dsa_switch *ds = dev->ds;
1261 /* enable tag tail for host port */
1263 ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_TAIL_TAG_ENABLE,
1266 ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_MAC_LOOPBACK, false);
1268 /* set back pressure */
1269 ksz_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE, true);
1271 /* enable broadcast storm limit */
1272 ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1274 /* disable DiffServ priority */
1275 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_PRIO_ENABLE, false);
1277 /* replace priority */
1278 ksz_port_cfg(dev, port, REG_PORT_MRI_MAC_CTRL, PORT_USER_PRIO_CEILING,
1280 ksz9477_port_cfg32(dev, port, REG_PORT_MTI_QUEUE_CTRL_0__4,
1281 MTI_PVID_REPLACE, false);
1283 /* enable 802.1p priority */
1284 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true);
1286 if (port < dev->phy_port_cnt) {
1287 /* do not force flow control */
1288 ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
1289 PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
1292 if (dev->phy_errata_9477)
1293 ksz9477_phy_errata_setup(dev, port);
1295 /* force flow control */
1296 ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
1297 PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
1300 /* configure MAC to 1G & RGMII mode */
1301 ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
1302 switch (p->interface) {
1303 case PHY_INTERFACE_MODE_MII:
1304 ksz9477_set_xmii(dev, 0, &data8);
1305 ksz9477_set_gbit(dev, false, &data8);
1306 p->phydev.speed = SPEED_100;
1308 case PHY_INTERFACE_MODE_RMII:
1309 ksz9477_set_xmii(dev, 1, &data8);
1310 ksz9477_set_gbit(dev, false, &data8);
1311 p->phydev.speed = SPEED_100;
1313 case PHY_INTERFACE_MODE_GMII:
1314 ksz9477_set_xmii(dev, 2, &data8);
1315 ksz9477_set_gbit(dev, true, &data8);
1316 p->phydev.speed = SPEED_1000;
1319 ksz9477_set_xmii(dev, 3, &data8);
1320 ksz9477_set_gbit(dev, true, &data8);
1321 data8 &= ~PORT_RGMII_ID_IG_ENABLE;
1322 data8 &= ~PORT_RGMII_ID_EG_ENABLE;
1323 if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1324 p->interface == PHY_INTERFACE_MODE_RGMII_RXID)
1325 data8 |= PORT_RGMII_ID_IG_ENABLE;
1326 if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1327 p->interface == PHY_INTERFACE_MODE_RGMII_TXID)
1328 data8 |= PORT_RGMII_ID_EG_ENABLE;
1329 /* On KSZ9893, disable RGMII in-band status support */
1330 if (dev->features & IS_9893)
1331 data8 &= ~PORT_MII_MAC_MODE;
1332 p->phydev.speed = SPEED_1000;
1335 ksz_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8);
1336 p->phydev.duplex = 1;
1340 member = dsa_user_ports(ds);
1342 member = BIT(dsa_upstream_port(ds, port));
1344 ksz9477_cfg_port_member(dev, port, member);
1346 /* clear pending interrupts */
1347 if (port < dev->phy_port_cnt)
1348 ksz_pread16(dev, port, REG_PORT_PHY_INT_ENABLE, &data16);
1351 static void ksz9477_config_cpu_port(struct dsa_switch *ds)
1353 struct ksz_device *dev = ds->priv;
1357 for (i = 0; i < dev->port_cnt; i++) {
1358 if (dsa_is_cpu_port(ds, i) && (dev->cpu_ports & (1 << i))) {
1359 phy_interface_t interface;
1360 const char *prev_msg;
1361 const char *prev_mode;
1366 /* Read from XMII register to determine host port
1367 * interface. If set specifically in device tree
1368 * note the difference to help debugging.
1370 interface = ksz9477_get_interface(dev, i);
1371 if (!p->interface) {
1372 if (dev->compat_interface) {
1374 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
1375 "Please update your device tree.\n",
1377 p->interface = dev->compat_interface;
1379 p->interface = interface;
1382 if (interface && interface != p->interface) {
1383 prev_msg = " instead of ";
1384 prev_mode = phy_modes(interface);
1390 "Port%d: using phy mode %s%s%s\n",
1392 phy_modes(p->interface),
1396 /* enable cpu port */
1397 ksz9477_port_setup(dev, i, true);
1402 for (i = 0; i < dev->port_cnt; i++) {
1403 if (i == dev->cpu_port)
1407 ksz9477_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1409 if (i < dev->phy_port_cnt)
1411 if (dev->chip_id == 0x00947700 && i == 6) {
1414 /* SGMII PHY detection code is not implemented yet. */
1420 static int ksz9477_setup(struct dsa_switch *ds)
1422 struct ksz_device *dev = ds->priv;
1425 dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table),
1426 dev->num_vlans, GFP_KERNEL);
1427 if (!dev->vlan_cache)
1430 ret = ksz9477_reset_switch(dev);
1432 dev_err(ds->dev, "failed to reset switch\n");
1436 /* Required for port partitioning. */
1437 ksz9477_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY,
1440 /* Do not work correctly with tail tagging. */
1441 ksz_cfg(dev, REG_SW_MAC_CTRL_0, SW_CHECK_LENGTH, false);
1443 /* Enable REG_SW_MTU__2 reg by setting SW_JUMBO_PACKET */
1444 ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_JUMBO_PACKET, true);
1446 /* Now we can configure default MTU value */
1447 ret = regmap_update_bits(dev->regmap[1], REG_SW_MTU__2, REG_SW_MTU_MASK,
1448 VLAN_ETH_FRAME_LEN + ETH_FCS_LEN);
1452 ksz9477_config_cpu_port(ds);
1454 ksz_cfg(dev, REG_SW_MAC_CTRL_1, MULTICAST_STORM_DISABLE, true);
1456 /* queue based egress rate limit */
1457 ksz_cfg(dev, REG_SW_MAC_CTRL_5, SW_OUT_RATE_LIMIT_QUEUE_BASED, true);
1459 /* enable global MIB counter freeze function */
1460 ksz_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
1463 ksz_cfg(dev, REG_SW_OPERATION, SW_START, true);
1465 ksz_init_mib_timer(dev);
1467 ds->configure_vlan_while_not_filtering = false;
1472 static const struct dsa_switch_ops ksz9477_switch_ops = {
1473 .get_tag_protocol = ksz9477_get_tag_protocol,
1474 .setup = ksz9477_setup,
1475 .phy_read = ksz9477_phy_read16,
1476 .phy_write = ksz9477_phy_write16,
1477 .phylink_mac_link_down = ksz_mac_link_down,
1478 .port_enable = ksz_enable_port,
1479 .get_strings = ksz9477_get_strings,
1480 .get_ethtool_stats = ksz_get_ethtool_stats,
1481 .get_sset_count = ksz_sset_count,
1482 .port_bridge_join = ksz_port_bridge_join,
1483 .port_bridge_leave = ksz_port_bridge_leave,
1484 .port_stp_state_set = ksz9477_port_stp_state_set,
1485 .port_fast_age = ksz_port_fast_age,
1486 .port_vlan_filtering = ksz9477_port_vlan_filtering,
1487 .port_vlan_add = ksz9477_port_vlan_add,
1488 .port_vlan_del = ksz9477_port_vlan_del,
1489 .port_fdb_dump = ksz9477_port_fdb_dump,
1490 .port_fdb_add = ksz9477_port_fdb_add,
1491 .port_fdb_del = ksz9477_port_fdb_del,
1492 .port_mdb_add = ksz9477_port_mdb_add,
1493 .port_mdb_del = ksz9477_port_mdb_del,
1494 .port_mirror_add = ksz9477_port_mirror_add,
1495 .port_mirror_del = ksz9477_port_mirror_del,
1496 .get_stats64 = ksz9477_get_stats64,
1497 .port_change_mtu = ksz9477_change_mtu,
1498 .port_max_mtu = ksz9477_max_mtu,
1501 static u32 ksz9477_get_port_addr(int port, int offset)
1503 return PORT_CTRL_ADDR(port, offset);
1506 static int ksz9477_switch_detect(struct ksz_device *dev)
1514 /* turn off SPI DO Edge select */
1515 ret = ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8);
1519 data8 &= ~SPI_AUTO_EDGE_DETECTION;
1520 ret = ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8);
1525 ret = ksz_read32(dev, REG_CHIP_ID0__1, &id32);
1528 ret = ksz_read8(dev, REG_GLOBAL_OPTIONS, &data8);
1532 /* Number of ports can be reduced depending on chip. */
1533 dev->phy_port_cnt = 5;
1535 /* Default capability is gigabit capable. */
1536 dev->features = GBIT_SUPPORT;
1538 dev_dbg(dev->dev, "Switch detect: ID=%08x%02x\n", id32, data8);
1539 id_hi = (u8)(id32 >> 16);
1540 id_lo = (u8)(id32 >> 8);
1541 if ((id_lo & 0xf) == 3) {
1542 /* Chip is from KSZ9893 design. */
1543 dev_info(dev->dev, "Found KSZ9893\n");
1544 dev->features |= IS_9893;
1546 /* Chip does not support gigabit. */
1547 if (data8 & SW_QW_ABLE)
1548 dev->features &= ~GBIT_SUPPORT;
1549 dev->phy_port_cnt = 2;
1551 dev_info(dev->dev, "Found KSZ9477 or compatible\n");
1552 /* Chip uses new XMII register definitions. */
1553 dev->features |= NEW_XMII;
1555 /* Chip does not support gigabit. */
1556 if (!(data8 & SW_GIGABIT_ABLE))
1557 dev->features &= ~GBIT_SUPPORT;
1560 /* Change chip id to known ones so it can be matched against them. */
1561 id32 = (id_hi << 16) | (id_lo << 8);
1563 dev->chip_id = id32;
1568 struct ksz_chip_data {
1570 const char *dev_name;
1576 bool phy_errata_9477;
1579 static const struct ksz_chip_data ksz9477_switch_chips[] = {
1581 .chip_id = 0x00947700,
1582 .dev_name = "KSZ9477",
1586 .cpu_ports = 0x7F, /* can be configured as cpu port */
1587 .port_cnt = 7, /* total physical port count */
1588 .phy_errata_9477 = true,
1591 .chip_id = 0x00989700,
1592 .dev_name = "KSZ9897",
1596 .cpu_ports = 0x7F, /* can be configured as cpu port */
1597 .port_cnt = 7, /* total physical port count */
1598 .phy_errata_9477 = true,
1601 .chip_id = 0x00989300,
1602 .dev_name = "KSZ9893",
1606 .cpu_ports = 0x07, /* can be configured as cpu port */
1607 .port_cnt = 3, /* total port count */
1610 .chip_id = 0x00956700,
1611 .dev_name = "KSZ9567",
1615 .cpu_ports = 0x7F, /* can be configured as cpu port */
1616 .port_cnt = 7, /* total physical port count */
1617 .phy_errata_9477 = true,
1621 static int ksz9477_switch_init(struct ksz_device *dev)
1625 dev->ds->ops = &ksz9477_switch_ops;
1627 for (i = 0; i < ARRAY_SIZE(ksz9477_switch_chips); i++) {
1628 const struct ksz_chip_data *chip = &ksz9477_switch_chips[i];
1630 if (dev->chip_id == chip->chip_id) {
1631 dev->name = chip->dev_name;
1632 dev->num_vlans = chip->num_vlans;
1633 dev->num_alus = chip->num_alus;
1634 dev->num_statics = chip->num_statics;
1635 dev->port_cnt = chip->port_cnt;
1636 dev->cpu_ports = chip->cpu_ports;
1637 dev->phy_errata_9477 = chip->phy_errata_9477;
1643 /* no switch found */
1647 dev->port_mask = (1 << dev->port_cnt) - 1;
1649 dev->reg_mib_cnt = SWITCH_COUNTER_NUM;
1650 dev->mib_cnt = TOTAL_SWITCH_COUNTER_NUM;
1652 dev->ports = devm_kzalloc(dev->dev,
1653 dev->port_cnt * sizeof(struct ksz_port),
1657 for (i = 0; i < dev->port_cnt; i++) {
1658 spin_lock_init(&dev->ports[i].mib.stats64_lock);
1659 mutex_init(&dev->ports[i].mib.cnt_mutex);
1660 dev->ports[i].mib.counters =
1661 devm_kzalloc(dev->dev,
1663 (TOTAL_SWITCH_COUNTER_NUM + 1),
1665 if (!dev->ports[i].mib.counters)
1669 /* set the real number of ports */
1670 dev->ds->num_ports = dev->port_cnt;
1675 static void ksz9477_switch_exit(struct ksz_device *dev)
1677 ksz9477_reset_switch(dev);
1680 static const struct ksz_dev_ops ksz9477_dev_ops = {
1681 .get_port_addr = ksz9477_get_port_addr,
1682 .cfg_port_member = ksz9477_cfg_port_member,
1683 .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table,
1684 .port_setup = ksz9477_port_setup,
1685 .r_mib_cnt = ksz9477_r_mib_cnt,
1686 .r_mib_pkt = ksz9477_r_mib_pkt,
1687 .r_mib_stat64 = ksz9477_r_mib_stats64,
1688 .freeze_mib = ksz9477_freeze_mib,
1689 .port_init_cnt = ksz9477_port_init_cnt,
1690 .shutdown = ksz9477_reset_switch,
1691 .detect = ksz9477_switch_detect,
1692 .init = ksz9477_switch_init,
1693 .exit = ksz9477_switch_exit,
1696 int ksz9477_switch_register(struct ksz_device *dev)
1699 struct phy_device *phydev;
1701 ret = ksz_switch_register(dev, &ksz9477_dev_ops);
1705 for (i = 0; i < dev->phy_port_cnt; ++i) {
1706 if (!dsa_is_user_port(dev->ds, i))
1709 phydev = dsa_to_port(dev->ds, i)->slave->phydev;
1711 /* The MAC actually cannot run in 1000 half-duplex mode. */
1712 phy_remove_link_mode(phydev,
1713 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1715 /* PHY does not support gigabit. */
1716 if (!(dev->features & GBIT_SUPPORT))
1717 phy_remove_link_mode(phydev,
1718 ETHTOOL_LINK_MODE_1000baseT_Full_BIT);
1722 EXPORT_SYMBOL(ksz9477_switch_register);
1724 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
1725 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch DSA Driver");
1726 MODULE_LICENSE("GPL");