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>
15 #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 static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
70 regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
73 static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
76 regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
77 bits, set ? bits : 0);
80 static void ksz9477_cfg32(struct ksz_device *dev, u32 addr, u32 bits, bool set)
82 regmap_update_bits(dev->regmap[2], addr, bits, set ? bits : 0);
85 static void ksz9477_port_cfg32(struct ksz_device *dev, int port, int offset,
88 regmap_update_bits(dev->regmap[2], PORT_CTRL_ADDR(port, offset),
89 bits, set ? bits : 0);
92 static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev, u32 waiton,
98 ksz_read8(dev, REG_SW_VLAN_CTRL, &data);
102 } while (timeout-- > 0);
110 static int ksz9477_get_vlan_table(struct ksz_device *dev, u16 vid,
115 mutex_lock(&dev->vlan_mutex);
117 ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
118 ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START);
120 /* wait to be cleared */
121 ret = ksz9477_wait_vlan_ctrl_ready(dev, VLAN_START, 1000);
123 dev_dbg(dev->dev, "Failed to read vlan table\n");
127 ksz_read32(dev, REG_SW_VLAN_ENTRY__4, &vlan_table[0]);
128 ksz_read32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, &vlan_table[1]);
129 ksz_read32(dev, REG_SW_VLAN_ENTRY_PORTS__4, &vlan_table[2]);
131 ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
134 mutex_unlock(&dev->vlan_mutex);
139 static int ksz9477_set_vlan_table(struct ksz_device *dev, u16 vid,
144 mutex_lock(&dev->vlan_mutex);
146 ksz_write32(dev, REG_SW_VLAN_ENTRY__4, vlan_table[0]);
147 ksz_write32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, vlan_table[1]);
148 ksz_write32(dev, REG_SW_VLAN_ENTRY_PORTS__4, vlan_table[2]);
150 ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
151 ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE);
153 /* wait to be cleared */
154 ret = ksz9477_wait_vlan_ctrl_ready(dev, VLAN_START, 1000);
156 dev_dbg(dev->dev, "Failed to write vlan table\n");
160 ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
162 /* update vlan cache table */
163 dev->vlan_cache[vid].table[0] = vlan_table[0];
164 dev->vlan_cache[vid].table[1] = vlan_table[1];
165 dev->vlan_cache[vid].table[2] = vlan_table[2];
168 mutex_unlock(&dev->vlan_mutex);
173 static void ksz9477_read_table(struct ksz_device *dev, u32 *table)
175 ksz_read32(dev, REG_SW_ALU_VAL_A, &table[0]);
176 ksz_read32(dev, REG_SW_ALU_VAL_B, &table[1]);
177 ksz_read32(dev, REG_SW_ALU_VAL_C, &table[2]);
178 ksz_read32(dev, REG_SW_ALU_VAL_D, &table[3]);
181 static void ksz9477_write_table(struct ksz_device *dev, u32 *table)
183 ksz_write32(dev, REG_SW_ALU_VAL_A, table[0]);
184 ksz_write32(dev, REG_SW_ALU_VAL_B, table[1]);
185 ksz_write32(dev, REG_SW_ALU_VAL_C, table[2]);
186 ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]);
189 static int ksz9477_wait_alu_ready(struct ksz_device *dev, u32 waiton,
195 ksz_read32(dev, REG_SW_ALU_CTRL__4, &data);
196 if (!(data & waiton))
199 } while (timeout-- > 0);
207 static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev, u32 waiton,
213 ksz_read32(dev, REG_SW_ALU_STAT_CTRL__4, &data);
214 if (!(data & waiton))
217 } while (timeout-- > 0);
225 static int ksz9477_reset_switch(struct ksz_device *dev)
232 ksz_cfg(dev, REG_SW_OPERATION, SW_RESET, true);
234 /* turn off SPI DO Edge select */
235 ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8);
236 data8 &= ~SPI_AUTO_EDGE_DETECTION;
237 ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8);
239 /* default configuration */
240 ksz_read8(dev, REG_SW_LUE_CTRL_1, &data8);
241 data8 = SW_AGING_ENABLE | SW_LINK_AUTO_AGING |
242 SW_SRC_ADDR_FILTER | SW_FLUSH_STP_TABLE | SW_FLUSH_MSTP_TABLE;
243 ksz_write8(dev, REG_SW_LUE_CTRL_1, data8);
245 /* disable interrupts */
246 ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK);
247 ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0x7F);
248 ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
250 /* set broadcast storm protection 10% rate */
251 ksz_read16(dev, REG_SW_MAC_CTRL_2, &data16);
252 data16 &= ~BROADCAST_STORM_RATE;
253 data16 |= (BROADCAST_STORM_VALUE * BROADCAST_STORM_PROT_RATE) / 100;
254 ksz_write16(dev, REG_SW_MAC_CTRL_2, data16);
256 if (dev->synclko_125)
257 ksz_write8(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1,
258 SW_ENABLE_REFCLKO | SW_REFCLKO_IS_125MHZ);
263 static void ksz9477_r_mib_cnt(struct ksz_device *dev, int port, u16 addr,
266 struct ksz_port *p = &dev->ports[port];
271 /* retain the flush/freeze bit */
272 data = p->freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
273 data |= MIB_COUNTER_READ;
274 data |= (addr << MIB_COUNTER_INDEX_S);
275 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, data);
277 ret = regmap_read_poll_timeout(dev->regmap[2],
278 PORT_CTRL_ADDR(port, REG_PORT_MIB_CTRL_STAT__4),
279 val, !(val & MIB_COUNTER_READ), 10, 1000);
280 /* failed to read MIB. get out of loop */
282 dev_dbg(dev->dev, "Failed to get MIB\n");
286 /* count resets upon read */
287 ksz_pread32(dev, port, REG_PORT_MIB_DATA, &data);
291 static void ksz9477_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
292 u64 *dropped, u64 *cnt)
294 addr = ksz9477_mib_names[addr].index;
295 ksz9477_r_mib_cnt(dev, port, addr, cnt);
298 static void ksz9477_freeze_mib(struct ksz_device *dev, int port, bool freeze)
300 u32 val = freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
301 struct ksz_port *p = &dev->ports[port];
303 /* enable/disable the port for flush/freeze function */
304 mutex_lock(&p->mib.cnt_mutex);
305 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, val);
307 /* used by MIB counter reading code to know freeze is enabled */
309 mutex_unlock(&p->mib.cnt_mutex);
312 static void ksz9477_port_init_cnt(struct ksz_device *dev, int port)
314 struct ksz_port_mib *mib = &dev->ports[port].mib;
316 /* flush all enabled port MIB counters */
317 mutex_lock(&mib->cnt_mutex);
318 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4,
319 MIB_COUNTER_FLUSH_FREEZE);
320 ksz_write8(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FLUSH);
321 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, 0);
322 mutex_unlock(&mib->cnt_mutex);
325 memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
328 static enum dsa_tag_protocol ksz9477_get_tag_protocol(struct dsa_switch *ds,
331 enum dsa_tag_protocol proto = DSA_TAG_PROTO_KSZ9477;
332 struct ksz_device *dev = ds->priv;
334 if (dev->features & IS_9893)
335 proto = DSA_TAG_PROTO_KSZ9893;
339 static int ksz9477_phy_read16(struct dsa_switch *ds, int addr, int reg)
341 struct ksz_device *dev = ds->priv;
344 /* No real PHY after this. Simulate the PHY.
345 * A fixed PHY can be setup in the device tree, but this function is
346 * still called for that port during initialization.
347 * For RGMII PHY there is no way to access it so the fixed PHY should
348 * be used. For SGMII PHY the supporting code will be added later.
350 if (addr >= dev->phy_port_cnt) {
351 struct ksz_port *p = &dev->ports[addr];
376 if (p->phydev.speed == SPEED_1000)
383 ksz_pread16(dev, addr, 0x100 + (reg << 1), &val);
389 static int ksz9477_phy_write16(struct dsa_switch *ds, int addr, int reg,
392 struct ksz_device *dev = ds->priv;
394 /* No real PHY after this. */
395 if (addr >= dev->phy_port_cnt)
398 /* No gigabit support. Do not write to this register. */
399 if (!(dev->features & GBIT_SUPPORT) && reg == MII_CTRL1000)
401 ksz_pwrite16(dev, addr, 0x100 + (reg << 1), val);
406 static void ksz9477_get_strings(struct dsa_switch *ds, int port,
407 u32 stringset, uint8_t *buf)
411 if (stringset != ETH_SS_STATS)
414 for (i = 0; i < TOTAL_SWITCH_COUNTER_NUM; i++) {
415 memcpy(buf + i * ETH_GSTRING_LEN, ksz9477_mib_names[i].string,
420 static void ksz9477_cfg_port_member(struct ksz_device *dev, int port,
423 ksz_pwrite32(dev, port, REG_PORT_VLAN_MEMBERSHIP__4, member);
424 dev->ports[port].member = member;
427 static void ksz9477_port_stp_state_set(struct dsa_switch *ds, int port,
430 struct ksz_device *dev = ds->priv;
431 struct ksz_port *p = &dev->ports[port];
434 int forward = dev->member;
436 ksz_pread8(dev, port, P_STP_CTRL, &data);
437 data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
440 case BR_STATE_DISABLED:
441 data |= PORT_LEARN_DISABLE;
442 if (port != dev->cpu_port)
445 case BR_STATE_LISTENING:
446 data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE);
447 if (port != dev->cpu_port &&
448 p->stp_state == BR_STATE_DISABLED)
449 member = dev->host_mask | p->vid_member;
451 case BR_STATE_LEARNING:
452 data |= PORT_RX_ENABLE;
454 case BR_STATE_FORWARDING:
455 data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
457 /* This function is also used internally. */
458 if (port == dev->cpu_port)
461 member = dev->host_mask | p->vid_member;
462 mutex_lock(&dev->dev_mutex);
464 /* Port is a member of a bridge. */
465 if (dev->br_member & (1 << port)) {
466 dev->member |= (1 << port);
467 member = dev->member;
469 mutex_unlock(&dev->dev_mutex);
471 case BR_STATE_BLOCKING:
472 data |= PORT_LEARN_DISABLE;
473 if (port != dev->cpu_port &&
474 p->stp_state == BR_STATE_DISABLED)
475 member = dev->host_mask | p->vid_member;
478 dev_err(ds->dev, "invalid STP state: %d\n", state);
482 ksz_pwrite8(dev, port, P_STP_CTRL, data);
483 p->stp_state = state;
484 mutex_lock(&dev->dev_mutex);
485 if (data & PORT_RX_ENABLE)
486 dev->rx_ports |= (1 << port);
488 dev->rx_ports &= ~(1 << port);
489 if (data & PORT_TX_ENABLE)
490 dev->tx_ports |= (1 << port);
492 dev->tx_ports &= ~(1 << port);
494 /* Port membership may share register with STP state. */
495 if (member >= 0 && member != p->member)
496 ksz9477_cfg_port_member(dev, port, (u8)member);
498 /* Check if forwarding needs to be updated. */
499 if (state != BR_STATE_FORWARDING) {
500 if (dev->br_member & (1 << port))
501 dev->member &= ~(1 << port);
504 /* When topology has changed the function ksz_update_port_member
505 * should be called to modify port forwarding behavior.
507 if (forward != dev->member)
508 ksz_update_port_member(dev, port);
509 mutex_unlock(&dev->dev_mutex);
512 static void ksz9477_flush_dyn_mac_table(struct ksz_device *dev, int port)
516 ksz_read8(dev, REG_SW_LUE_CTRL_2, &data);
517 data &= ~(SW_FLUSH_OPTION_M << SW_FLUSH_OPTION_S);
518 data |= (SW_FLUSH_OPTION_DYN_MAC << SW_FLUSH_OPTION_S);
519 ksz_write8(dev, REG_SW_LUE_CTRL_2, data);
520 if (port < dev->mib_port_cnt) {
521 /* flush individual port */
522 ksz_pread8(dev, port, P_STP_CTRL, &data);
523 if (!(data & PORT_LEARN_DISABLE))
524 ksz_pwrite8(dev, port, P_STP_CTRL,
525 data | PORT_LEARN_DISABLE);
526 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
527 ksz_pwrite8(dev, port, P_STP_CTRL, data);
530 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_STP_TABLE, true);
534 static int ksz9477_port_vlan_filtering(struct dsa_switch *ds, int port,
537 struct ksz_device *dev = ds->priv;
540 ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
541 PORT_VLAN_LOOKUP_VID_0, true);
542 ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, true);
544 ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, false);
545 ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
546 PORT_VLAN_LOOKUP_VID_0, false);
552 static void ksz9477_port_vlan_add(struct dsa_switch *ds, int port,
553 const struct switchdev_obj_port_vlan *vlan)
555 struct ksz_device *dev = ds->priv;
558 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
560 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
561 if (ksz9477_get_vlan_table(dev, vid, vlan_table)) {
562 dev_dbg(dev->dev, "Failed to get vlan table\n");
566 vlan_table[0] = VLAN_VALID | (vid & VLAN_FID_M);
568 vlan_table[1] |= BIT(port);
570 vlan_table[1] &= ~BIT(port);
571 vlan_table[1] &= ~(BIT(dev->cpu_port));
573 vlan_table[2] |= BIT(port) | BIT(dev->cpu_port);
575 if (ksz9477_set_vlan_table(dev, vid, vlan_table)) {
576 dev_dbg(dev->dev, "Failed to set vlan table\n");
581 if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
582 ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, vid);
586 static int ksz9477_port_vlan_del(struct dsa_switch *ds, int port,
587 const struct switchdev_obj_port_vlan *vlan)
589 struct ksz_device *dev = ds->priv;
590 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
595 ksz_pread16(dev, port, REG_PORT_DEFAULT_VID, &pvid);
598 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
599 if (ksz9477_get_vlan_table(dev, vid, vlan_table)) {
600 dev_dbg(dev->dev, "Failed to get vlan table\n");
604 vlan_table[2] &= ~BIT(port);
610 vlan_table[1] &= ~BIT(port);
612 if (ksz9477_set_vlan_table(dev, vid, vlan_table)) {
613 dev_dbg(dev->dev, "Failed to set vlan table\n");
618 ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, pvid);
623 static int ksz9477_port_fdb_add(struct dsa_switch *ds, int port,
624 const unsigned char *addr, u16 vid)
626 struct ksz_device *dev = ds->priv;
631 mutex_lock(&dev->alu_mutex);
633 /* find any entry with mac & vid */
634 data = vid << ALU_FID_INDEX_S;
635 data |= ((addr[0] << 8) | addr[1]);
636 ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
638 data = ((addr[2] << 24) | (addr[3] << 16));
639 data |= ((addr[4] << 8) | addr[5]);
640 ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
642 /* start read operation */
643 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
645 /* wait to be finished */
646 ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000);
648 dev_dbg(dev->dev, "Failed to read ALU\n");
653 ksz9477_read_table(dev, alu_table);
655 /* update ALU entry */
656 alu_table[0] = ALU_V_STATIC_VALID;
657 alu_table[1] |= BIT(port);
659 alu_table[1] |= ALU_V_USE_FID;
660 alu_table[2] = (vid << ALU_V_FID_S);
661 alu_table[2] |= ((addr[0] << 8) | addr[1]);
662 alu_table[3] = ((addr[2] << 24) | (addr[3] << 16));
663 alu_table[3] |= ((addr[4] << 8) | addr[5]);
665 ksz9477_write_table(dev, alu_table);
667 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
669 /* wait to be finished */
670 ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000);
672 dev_dbg(dev->dev, "Failed to write ALU\n");
675 mutex_unlock(&dev->alu_mutex);
680 static int ksz9477_port_fdb_del(struct dsa_switch *ds, int port,
681 const unsigned char *addr, u16 vid)
683 struct ksz_device *dev = ds->priv;
688 mutex_lock(&dev->alu_mutex);
690 /* read any entry with mac & vid */
691 data = vid << ALU_FID_INDEX_S;
692 data |= ((addr[0] << 8) | addr[1]);
693 ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
695 data = ((addr[2] << 24) | (addr[3] << 16));
696 data |= ((addr[4] << 8) | addr[5]);
697 ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
699 /* start read operation */
700 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
702 /* wait to be finished */
703 ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000);
705 dev_dbg(dev->dev, "Failed to read ALU\n");
709 ksz_read32(dev, REG_SW_ALU_VAL_A, &alu_table[0]);
710 if (alu_table[0] & ALU_V_STATIC_VALID) {
711 ksz_read32(dev, REG_SW_ALU_VAL_B, &alu_table[1]);
712 ksz_read32(dev, REG_SW_ALU_VAL_C, &alu_table[2]);
713 ksz_read32(dev, REG_SW_ALU_VAL_D, &alu_table[3]);
715 /* clear forwarding port */
716 alu_table[2] &= ~BIT(port);
718 /* if there is no port to forward, clear table */
719 if ((alu_table[2] & ALU_V_PORT_MAP) == 0) {
732 ksz9477_write_table(dev, alu_table);
734 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
736 /* wait to be finished */
737 ret = ksz9477_wait_alu_ready(dev, ALU_START, 1000);
739 dev_dbg(dev->dev, "Failed to write ALU\n");
742 mutex_unlock(&dev->alu_mutex);
747 static void ksz9477_convert_alu(struct alu_struct *alu, u32 *alu_table)
749 alu->is_static = !!(alu_table[0] & ALU_V_STATIC_VALID);
750 alu->is_src_filter = !!(alu_table[0] & ALU_V_SRC_FILTER);
751 alu->is_dst_filter = !!(alu_table[0] & ALU_V_DST_FILTER);
752 alu->prio_age = (alu_table[0] >> ALU_V_PRIO_AGE_CNT_S) &
753 ALU_V_PRIO_AGE_CNT_M;
754 alu->mstp = alu_table[0] & ALU_V_MSTP_M;
756 alu->is_override = !!(alu_table[1] & ALU_V_OVERRIDE);
757 alu->is_use_fid = !!(alu_table[1] & ALU_V_USE_FID);
758 alu->port_forward = alu_table[1] & ALU_V_PORT_MAP;
760 alu->fid = (alu_table[2] >> ALU_V_FID_S) & ALU_V_FID_M;
762 alu->mac[0] = (alu_table[2] >> 8) & 0xFF;
763 alu->mac[1] = alu_table[2] & 0xFF;
764 alu->mac[2] = (alu_table[3] >> 24) & 0xFF;
765 alu->mac[3] = (alu_table[3] >> 16) & 0xFF;
766 alu->mac[4] = (alu_table[3] >> 8) & 0xFF;
767 alu->mac[5] = alu_table[3] & 0xFF;
770 static int ksz9477_port_fdb_dump(struct dsa_switch *ds, int port,
771 dsa_fdb_dump_cb_t *cb, void *data)
773 struct ksz_device *dev = ds->priv;
777 struct alu_struct alu;
780 mutex_lock(&dev->alu_mutex);
782 /* start ALU search */
783 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_START | ALU_SEARCH);
788 ksz_read32(dev, REG_SW_ALU_CTRL__4, &ksz_data);
789 if ((ksz_data & ALU_VALID) || !(ksz_data & ALU_START))
792 } while (timeout-- > 0);
795 dev_dbg(dev->dev, "Failed to search ALU\n");
801 ksz9477_read_table(dev, alu_table);
803 ksz9477_convert_alu(&alu, alu_table);
805 if (alu.port_forward & BIT(port)) {
806 ret = cb(alu.mac, alu.fid, alu.is_static, data);
810 } while (ksz_data & ALU_START);
814 /* stop ALU search */
815 ksz_write32(dev, REG_SW_ALU_CTRL__4, 0);
817 mutex_unlock(&dev->alu_mutex);
822 static void ksz9477_port_mdb_add(struct dsa_switch *ds, int port,
823 const struct switchdev_obj_port_mdb *mdb)
825 struct ksz_device *dev = ds->priv;
831 mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
832 mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
833 mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
835 mutex_lock(&dev->alu_mutex);
837 for (index = 0; index < dev->num_statics; index++) {
838 /* find empty slot first */
839 data = (index << ALU_STAT_INDEX_S) |
840 ALU_STAT_READ | ALU_STAT_START;
841 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
843 /* wait to be finished */
844 if (ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000) < 0) {
845 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
849 /* read ALU static table */
850 ksz9477_read_table(dev, static_table);
852 if (static_table[0] & ALU_V_STATIC_VALID) {
853 /* check this has same vid & mac address */
854 if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
855 ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
856 static_table[3] == mac_lo) {
857 /* found matching one */
861 /* found empty one */
866 /* no available entry */
867 if (index == dev->num_statics)
871 static_table[0] = ALU_V_STATIC_VALID;
872 static_table[1] |= BIT(port);
874 static_table[1] |= ALU_V_USE_FID;
875 static_table[2] = (mdb->vid << ALU_V_FID_S);
876 static_table[2] |= mac_hi;
877 static_table[3] = mac_lo;
879 ksz9477_write_table(dev, static_table);
881 data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
882 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
884 /* wait to be finished */
885 if (ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000) < 0)
886 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
889 mutex_unlock(&dev->alu_mutex);
892 static int ksz9477_port_mdb_del(struct dsa_switch *ds, int port,
893 const struct switchdev_obj_port_mdb *mdb)
895 struct ksz_device *dev = ds->priv;
902 mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
903 mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
904 mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
906 mutex_lock(&dev->alu_mutex);
908 for (index = 0; index < dev->num_statics; index++) {
909 /* find empty slot first */
910 data = (index << ALU_STAT_INDEX_S) |
911 ALU_STAT_READ | ALU_STAT_START;
912 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
914 /* wait to be finished */
915 ret = ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000);
917 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
921 /* read ALU static table */
922 ksz9477_read_table(dev, static_table);
924 if (static_table[0] & ALU_V_STATIC_VALID) {
925 /* check this has same vid & mac address */
927 if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
928 ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
929 static_table[3] == mac_lo) {
930 /* found matching one */
936 /* no available entry */
937 if (index == dev->num_statics)
941 static_table[1] &= ~BIT(port);
943 if ((static_table[1] & ALU_V_PORT_MAP) == 0) {
951 ksz9477_write_table(dev, static_table);
953 data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
954 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
956 /* wait to be finished */
957 ret = ksz9477_wait_alu_sta_ready(dev, ALU_STAT_START, 1000);
959 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
962 mutex_unlock(&dev->alu_mutex);
967 static int ksz9477_port_mirror_add(struct dsa_switch *ds, int port,
968 struct dsa_mall_mirror_tc_entry *mirror,
971 struct ksz_device *dev = ds->priv;
974 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
976 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
978 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
980 /* configure mirror port */
981 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
982 PORT_MIRROR_SNIFFER, true);
984 ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
989 static void ksz9477_port_mirror_del(struct dsa_switch *ds, int port,
990 struct dsa_mall_mirror_tc_entry *mirror)
992 struct ksz_device *dev = ds->priv;
996 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
998 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
1000 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1002 if (!(data & (PORT_MIRROR_RX | PORT_MIRROR_TX)))
1003 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1004 PORT_MIRROR_SNIFFER, false);
1007 static void ksz9477_phy_setup(struct ksz_device *dev, int port,
1008 struct phy_device *phy)
1010 /* Only apply to port with PHY. */
1011 if (port >= dev->phy_port_cnt)
1014 /* The MAC actually cannot run in 1000 half-duplex mode. */
1015 phy_remove_link_mode(phy,
1016 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1018 /* PHY does not support gigabit. */
1019 if (!(dev->features & GBIT_SUPPORT))
1020 phy_remove_link_mode(phy,
1021 ETHTOOL_LINK_MODE_1000baseT_Full_BIT);
1024 static bool ksz9477_get_gbit(struct ksz_device *dev, u8 data)
1028 if (dev->features & NEW_XMII)
1029 gbit = !(data & PORT_MII_NOT_1GBIT);
1031 gbit = !!(data & PORT_MII_1000MBIT_S1);
1035 static void ksz9477_set_gbit(struct ksz_device *dev, bool gbit, u8 *data)
1037 if (dev->features & NEW_XMII) {
1039 *data &= ~PORT_MII_NOT_1GBIT;
1041 *data |= PORT_MII_NOT_1GBIT;
1044 *data |= PORT_MII_1000MBIT_S1;
1046 *data &= ~PORT_MII_1000MBIT_S1;
1050 static int ksz9477_get_xmii(struct ksz_device *dev, u8 data)
1054 if (dev->features & NEW_XMII) {
1055 switch (data & PORT_MII_SEL_M) {
1069 switch (data & PORT_MII_SEL_M) {
1070 case PORT_MII_SEL_S1:
1073 case PORT_RMII_SEL_S1:
1076 case PORT_GMII_SEL_S1:
1086 static void ksz9477_set_xmii(struct ksz_device *dev, int mode, u8 *data)
1090 if (dev->features & NEW_XMII) {
1093 xmii = PORT_MII_SEL;
1096 xmii = PORT_RMII_SEL;
1099 xmii = PORT_GMII_SEL;
1102 xmii = PORT_RGMII_SEL;
1108 xmii = PORT_MII_SEL_S1;
1111 xmii = PORT_RMII_SEL_S1;
1114 xmii = PORT_GMII_SEL_S1;
1117 xmii = PORT_RGMII_SEL_S1;
1121 *data &= ~PORT_MII_SEL_M;
1125 static phy_interface_t ksz9477_get_interface(struct ksz_device *dev, int port)
1127 phy_interface_t interface;
1132 if (port < dev->phy_port_cnt)
1133 return PHY_INTERFACE_MODE_NA;
1134 ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
1135 gbit = ksz9477_get_gbit(dev, data8);
1136 mode = ksz9477_get_xmii(dev, data8);
1139 interface = PHY_INTERFACE_MODE_GMII;
1144 interface = PHY_INTERFACE_MODE_MII;
1147 interface = PHY_INTERFACE_MODE_RMII;
1150 interface = PHY_INTERFACE_MODE_RGMII;
1151 if (data8 & PORT_RGMII_ID_EG_ENABLE)
1152 interface = PHY_INTERFACE_MODE_RGMII_TXID;
1153 if (data8 & PORT_RGMII_ID_IG_ENABLE) {
1154 interface = PHY_INTERFACE_MODE_RGMII_RXID;
1155 if (data8 & PORT_RGMII_ID_EG_ENABLE)
1156 interface = PHY_INTERFACE_MODE_RGMII_ID;
1163 static void ksz9477_port_mmd_write(struct ksz_device *dev, int port,
1164 u8 dev_addr, u16 reg_addr, u16 val)
1166 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP,
1167 MMD_SETUP(PORT_MMD_OP_INDEX, dev_addr));
1168 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, reg_addr);
1169 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP,
1170 MMD_SETUP(PORT_MMD_OP_DATA_NO_INCR, dev_addr));
1171 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, val);
1174 static void ksz9477_phy_errata_setup(struct ksz_device *dev, int port)
1176 /* Apply PHY settings to address errata listed in
1177 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
1178 * Silicon Errata and Data Sheet Clarification documents:
1180 * Register settings are needed to improve PHY receive performance
1182 ksz9477_port_mmd_write(dev, port, 0x01, 0x6f, 0xdd0b);
1183 ksz9477_port_mmd_write(dev, port, 0x01, 0x8f, 0x6032);
1184 ksz9477_port_mmd_write(dev, port, 0x01, 0x9d, 0x248c);
1185 ksz9477_port_mmd_write(dev, port, 0x01, 0x75, 0x0060);
1186 ksz9477_port_mmd_write(dev, port, 0x01, 0xd3, 0x7777);
1187 ksz9477_port_mmd_write(dev, port, 0x1c, 0x06, 0x3008);
1188 ksz9477_port_mmd_write(dev, port, 0x1c, 0x08, 0x2001);
1190 /* Transmit waveform amplitude can be improved
1191 * (1000BASE-T, 100BASE-TX, 10BASE-Te)
1193 ksz9477_port_mmd_write(dev, port, 0x1c, 0x04, 0x00d0);
1195 /* Energy Efficient Ethernet (EEE) feature select must
1196 * be manually disabled (except on KSZ8565 which is 100Mbit)
1198 if (dev->features & GBIT_SUPPORT)
1199 ksz9477_port_mmd_write(dev, port, 0x07, 0x3c, 0x0000);
1201 /* Register settings are required to meet data sheet
1202 * supply current specifications
1204 ksz9477_port_mmd_write(dev, port, 0x1c, 0x13, 0x6eff);
1205 ksz9477_port_mmd_write(dev, port, 0x1c, 0x14, 0xe6ff);
1206 ksz9477_port_mmd_write(dev, port, 0x1c, 0x15, 0x6eff);
1207 ksz9477_port_mmd_write(dev, port, 0x1c, 0x16, 0xe6ff);
1208 ksz9477_port_mmd_write(dev, port, 0x1c, 0x17, 0x00ff);
1209 ksz9477_port_mmd_write(dev, port, 0x1c, 0x18, 0x43ff);
1210 ksz9477_port_mmd_write(dev, port, 0x1c, 0x19, 0xc3ff);
1211 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1a, 0x6fff);
1212 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1b, 0x07ff);
1213 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1c, 0x0fff);
1214 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1d, 0xe7ff);
1215 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1e, 0xefff);
1216 ksz9477_port_mmd_write(dev, port, 0x1c, 0x20, 0xeeee);
1219 static void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1224 struct ksz_port *p = &dev->ports[port];
1226 /* enable tag tail for host port */
1228 ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_TAIL_TAG_ENABLE,
1231 ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_MAC_LOOPBACK, false);
1233 /* set back pressure */
1234 ksz_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE, true);
1236 /* enable broadcast storm limit */
1237 ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1239 /* disable DiffServ priority */
1240 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_PRIO_ENABLE, false);
1242 /* replace priority */
1243 ksz_port_cfg(dev, port, REG_PORT_MRI_MAC_CTRL, PORT_USER_PRIO_CEILING,
1245 ksz9477_port_cfg32(dev, port, REG_PORT_MTI_QUEUE_CTRL_0__4,
1246 MTI_PVID_REPLACE, false);
1248 /* enable 802.1p priority */
1249 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true);
1251 if (port < dev->phy_port_cnt) {
1252 /* do not force flow control */
1253 ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
1254 PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
1257 if (dev->phy_errata_9477)
1258 ksz9477_phy_errata_setup(dev, port);
1260 /* force flow control */
1261 ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
1262 PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
1265 /* configure MAC to 1G & RGMII mode */
1266 ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
1267 switch (dev->interface) {
1268 case PHY_INTERFACE_MODE_MII:
1269 ksz9477_set_xmii(dev, 0, &data8);
1270 ksz9477_set_gbit(dev, false, &data8);
1271 p->phydev.speed = SPEED_100;
1273 case PHY_INTERFACE_MODE_RMII:
1274 ksz9477_set_xmii(dev, 1, &data8);
1275 ksz9477_set_gbit(dev, false, &data8);
1276 p->phydev.speed = SPEED_100;
1278 case PHY_INTERFACE_MODE_GMII:
1279 ksz9477_set_xmii(dev, 2, &data8);
1280 ksz9477_set_gbit(dev, true, &data8);
1281 p->phydev.speed = SPEED_1000;
1284 ksz9477_set_xmii(dev, 3, &data8);
1285 ksz9477_set_gbit(dev, true, &data8);
1286 data8 &= ~PORT_RGMII_ID_IG_ENABLE;
1287 data8 &= ~PORT_RGMII_ID_EG_ENABLE;
1288 if (dev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1289 dev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
1290 data8 |= PORT_RGMII_ID_IG_ENABLE;
1291 if (dev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1292 dev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
1293 data8 |= PORT_RGMII_ID_EG_ENABLE;
1294 p->phydev.speed = SPEED_1000;
1297 ksz_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8);
1298 p->phydev.duplex = 1;
1300 mutex_lock(&dev->dev_mutex);
1302 member = dev->port_mask;
1303 dev->on_ports = dev->host_mask;
1304 dev->live_ports = dev->host_mask;
1306 member = dev->host_mask | p->vid_member;
1307 dev->on_ports |= (1 << port);
1309 /* Link was detected before port is enabled. */
1311 dev->live_ports |= (1 << port);
1313 mutex_unlock(&dev->dev_mutex);
1314 ksz9477_cfg_port_member(dev, port, member);
1316 /* clear pending interrupts */
1317 if (port < dev->phy_port_cnt)
1318 ksz_pread16(dev, port, REG_PORT_PHY_INT_ENABLE, &data16);
1321 static void ksz9477_config_cpu_port(struct dsa_switch *ds)
1323 struct ksz_device *dev = ds->priv;
1327 ds->num_ports = dev->port_cnt;
1329 for (i = 0; i < dev->port_cnt; i++) {
1330 if (dsa_is_cpu_port(ds, i) && (dev->cpu_ports & (1 << i))) {
1331 phy_interface_t interface;
1334 dev->host_mask = (1 << dev->cpu_port);
1335 dev->port_mask |= dev->host_mask;
1337 /* Read from XMII register to determine host port
1338 * interface. If set specifically in device tree
1339 * note the difference to help debugging.
1341 interface = ksz9477_get_interface(dev, i);
1342 if (!dev->interface)
1343 dev->interface = interface;
1344 if (interface && interface != dev->interface)
1346 "use %s instead of %s\n",
1347 phy_modes(dev->interface),
1348 phy_modes(interface));
1350 /* enable cpu port */
1351 ksz9477_port_setup(dev, i, true);
1352 p = &dev->ports[dev->cpu_port];
1353 p->vid_member = dev->port_mask;
1358 dev->member = dev->host_mask;
1360 for (i = 0; i < dev->mib_port_cnt; i++) {
1361 if (i == dev->cpu_port)
1365 /* Initialize to non-zero so that ksz_cfg_port_member() will
1368 p->vid_member = (1 << i);
1369 p->member = dev->port_mask;
1370 ksz9477_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1372 if (i < dev->phy_port_cnt)
1374 if (dev->chip_id == 0x00947700 && i == 6) {
1377 /* SGMII PHY detection code is not implemented yet. */
1383 static int ksz9477_setup(struct dsa_switch *ds)
1385 struct ksz_device *dev = ds->priv;
1388 dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table),
1389 dev->num_vlans, GFP_KERNEL);
1390 if (!dev->vlan_cache)
1393 ret = ksz9477_reset_switch(dev);
1395 dev_err(ds->dev, "failed to reset switch\n");
1399 /* Required for port partitioning. */
1400 ksz9477_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY,
1403 /* Do not work correctly with tail tagging. */
1404 ksz_cfg(dev, REG_SW_MAC_CTRL_0, SW_CHECK_LENGTH, false);
1406 /* accept packet up to 2000bytes */
1407 ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_LEGAL_PACKET_DISABLE, true);
1409 ksz9477_config_cpu_port(ds);
1411 ksz_cfg(dev, REG_SW_MAC_CTRL_1, MULTICAST_STORM_DISABLE, true);
1413 /* queue based egress rate limit */
1414 ksz_cfg(dev, REG_SW_MAC_CTRL_5, SW_OUT_RATE_LIMIT_QUEUE_BASED, true);
1416 /* enable global MIB counter freeze function */
1417 ksz_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
1420 ksz_cfg(dev, REG_SW_OPERATION, SW_START, true);
1422 ksz_init_mib_timer(dev);
1427 static const struct dsa_switch_ops ksz9477_switch_ops = {
1428 .get_tag_protocol = ksz9477_get_tag_protocol,
1429 .setup = ksz9477_setup,
1430 .phy_read = ksz9477_phy_read16,
1431 .phy_write = ksz9477_phy_write16,
1432 .adjust_link = ksz_adjust_link,
1433 .port_enable = ksz_enable_port,
1434 .port_disable = ksz_disable_port,
1435 .get_strings = ksz9477_get_strings,
1436 .get_ethtool_stats = ksz_get_ethtool_stats,
1437 .get_sset_count = ksz_sset_count,
1438 .port_bridge_join = ksz_port_bridge_join,
1439 .port_bridge_leave = ksz_port_bridge_leave,
1440 .port_stp_state_set = ksz9477_port_stp_state_set,
1441 .port_fast_age = ksz_port_fast_age,
1442 .port_vlan_filtering = ksz9477_port_vlan_filtering,
1443 .port_vlan_prepare = ksz_port_vlan_prepare,
1444 .port_vlan_add = ksz9477_port_vlan_add,
1445 .port_vlan_del = ksz9477_port_vlan_del,
1446 .port_fdb_dump = ksz9477_port_fdb_dump,
1447 .port_fdb_add = ksz9477_port_fdb_add,
1448 .port_fdb_del = ksz9477_port_fdb_del,
1449 .port_mdb_prepare = ksz_port_mdb_prepare,
1450 .port_mdb_add = ksz9477_port_mdb_add,
1451 .port_mdb_del = ksz9477_port_mdb_del,
1452 .port_mirror_add = ksz9477_port_mirror_add,
1453 .port_mirror_del = ksz9477_port_mirror_del,
1456 static u32 ksz9477_get_port_addr(int port, int offset)
1458 return PORT_CTRL_ADDR(port, offset);
1461 static int ksz9477_switch_detect(struct ksz_device *dev)
1469 /* turn off SPI DO Edge select */
1470 ret = ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8);
1474 data8 &= ~SPI_AUTO_EDGE_DETECTION;
1475 ret = ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8);
1480 ret = ksz_read32(dev, REG_CHIP_ID0__1, &id32);
1483 ret = ksz_read8(dev, REG_GLOBAL_OPTIONS, &data8);
1487 /* Number of ports can be reduced depending on chip. */
1488 dev->mib_port_cnt = TOTAL_PORT_NUM;
1489 dev->phy_port_cnt = 5;
1491 /* Default capability is gigabit capable. */
1492 dev->features = GBIT_SUPPORT;
1494 id_hi = (u8)(id32 >> 16);
1495 id_lo = (u8)(id32 >> 8);
1496 if ((id_lo & 0xf) == 3) {
1497 /* Chip is from KSZ9893 design. */
1498 dev->features |= IS_9893;
1500 /* Chip does not support gigabit. */
1501 if (data8 & SW_QW_ABLE)
1502 dev->features &= ~GBIT_SUPPORT;
1503 dev->mib_port_cnt = 3;
1504 dev->phy_port_cnt = 2;
1506 /* Chip uses new XMII register definitions. */
1507 dev->features |= NEW_XMII;
1509 /* Chip does not support gigabit. */
1510 if (!(data8 & SW_GIGABIT_ABLE))
1511 dev->features &= ~GBIT_SUPPORT;
1514 /* Change chip id to known ones so it can be matched against them. */
1515 id32 = (id_hi << 16) | (id_lo << 8);
1517 dev->chip_id = id32;
1522 struct ksz_chip_data {
1524 const char *dev_name;
1530 bool phy_errata_9477;
1533 static const struct ksz_chip_data ksz9477_switch_chips[] = {
1535 .chip_id = 0x00947700,
1536 .dev_name = "KSZ9477",
1540 .cpu_ports = 0x7F, /* can be configured as cpu port */
1541 .port_cnt = 7, /* total physical port count */
1542 .phy_errata_9477 = true,
1545 .chip_id = 0x00989700,
1546 .dev_name = "KSZ9897",
1550 .cpu_ports = 0x7F, /* can be configured as cpu port */
1551 .port_cnt = 7, /* total physical port count */
1552 .phy_errata_9477 = true,
1555 .chip_id = 0x00989300,
1556 .dev_name = "KSZ9893",
1560 .cpu_ports = 0x07, /* can be configured as cpu port */
1561 .port_cnt = 3, /* total port count */
1565 static int ksz9477_switch_init(struct ksz_device *dev)
1569 dev->ds->ops = &ksz9477_switch_ops;
1571 for (i = 0; i < ARRAY_SIZE(ksz9477_switch_chips); i++) {
1572 const struct ksz_chip_data *chip = &ksz9477_switch_chips[i];
1574 if (dev->chip_id == chip->chip_id) {
1575 dev->name = chip->dev_name;
1576 dev->num_vlans = chip->num_vlans;
1577 dev->num_alus = chip->num_alus;
1578 dev->num_statics = chip->num_statics;
1579 dev->port_cnt = chip->port_cnt;
1580 dev->cpu_ports = chip->cpu_ports;
1581 dev->phy_errata_9477 = chip->phy_errata_9477;
1587 /* no switch found */
1591 dev->port_mask = (1 << dev->port_cnt) - 1;
1593 dev->reg_mib_cnt = SWITCH_COUNTER_NUM;
1594 dev->mib_cnt = TOTAL_SWITCH_COUNTER_NUM;
1596 i = dev->mib_port_cnt;
1597 dev->ports = devm_kzalloc(dev->dev, sizeof(struct ksz_port) * i,
1601 for (i = 0; i < dev->mib_port_cnt; i++) {
1602 mutex_init(&dev->ports[i].mib.cnt_mutex);
1603 dev->ports[i].mib.counters =
1604 devm_kzalloc(dev->dev,
1606 (TOTAL_SWITCH_COUNTER_NUM + 1),
1608 if (!dev->ports[i].mib.counters)
1615 static void ksz9477_switch_exit(struct ksz_device *dev)
1617 ksz9477_reset_switch(dev);
1620 static const struct ksz_dev_ops ksz9477_dev_ops = {
1621 .get_port_addr = ksz9477_get_port_addr,
1622 .cfg_port_member = ksz9477_cfg_port_member,
1623 .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table,
1624 .phy_setup = ksz9477_phy_setup,
1625 .port_setup = ksz9477_port_setup,
1626 .r_mib_cnt = ksz9477_r_mib_cnt,
1627 .r_mib_pkt = ksz9477_r_mib_pkt,
1628 .freeze_mib = ksz9477_freeze_mib,
1629 .port_init_cnt = ksz9477_port_init_cnt,
1630 .shutdown = ksz9477_reset_switch,
1631 .detect = ksz9477_switch_detect,
1632 .init = ksz9477_switch_init,
1633 .exit = ksz9477_switch_exit,
1636 int ksz9477_switch_register(struct ksz_device *dev)
1638 return ksz_switch_register(dev, &ksz9477_dev_ops);
1640 EXPORT_SYMBOL(ksz9477_switch_register);
1642 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
1643 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch DSA Driver");
1644 MODULE_LICENSE("GPL");