2 * Marvell 88e6xxx Ethernet switch single-chip support
4 * Copyright (c) 2008 Marvell Semiconductor
6 * Copyright (c) 2015 CMC Electronics, Inc.
7 * Added support for VLAN Table Unit operations
9 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/delay.h>
18 #include <linux/etherdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/if_bridge.h>
21 #include <linux/jiffies.h>
22 #include <linux/list.h>
23 #include <linux/mdio.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/of_mdio.h>
27 #include <linux/netdevice.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/phy.h>
31 #include <net/switchdev.h>
32 #include "mv88e6xxx.h"
34 static void assert_reg_lock(struct mv88e6xxx_chip *chip)
36 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
37 dev_err(chip->dev, "Switch registers lock not held!\n");
42 /* The switch ADDR[4:1] configuration pins define the chip SMI device address
43 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
45 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
46 * is the only device connected to the SMI master. In this mode it responds to
47 * all 32 possible SMI addresses, and thus maps directly the internal devices.
49 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
50 * multiple devices to share the SMI interface. In this mode it responds to only
51 * 2 registers, used to indirectly access the internal SMI devices.
54 static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
55 int addr, int reg, u16 *val)
60 return chip->smi_ops->read(chip, addr, reg, val);
63 static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
64 int addr, int reg, u16 val)
69 return chip->smi_ops->write(chip, addr, reg, val);
72 static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
73 int addr, int reg, u16 *val)
77 ret = mdiobus_read_nested(chip->bus, addr, reg);
86 static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
87 int addr, int reg, u16 val)
91 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
98 static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
99 .read = mv88e6xxx_smi_single_chip_read,
100 .write = mv88e6xxx_smi_single_chip_write,
103 static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
108 for (i = 0; i < 16; i++) {
109 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
113 if ((ret & SMI_CMD_BUSY) == 0)
120 static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
121 int addr, int reg, u16 *val)
125 /* Wait for the bus to become free. */
126 ret = mv88e6xxx_smi_multi_chip_wait(chip);
130 /* Transmit the read command. */
131 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
132 SMI_CMD_OP_22_READ | (addr << 5) | reg);
136 /* Wait for the read command to complete. */
137 ret = mv88e6xxx_smi_multi_chip_wait(chip);
142 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
151 static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
152 int addr, int reg, u16 val)
156 /* Wait for the bus to become free. */
157 ret = mv88e6xxx_smi_multi_chip_wait(chip);
161 /* Transmit the data to write. */
162 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
166 /* Transmit the write command. */
167 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
168 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
172 /* Wait for the write command to complete. */
173 ret = mv88e6xxx_smi_multi_chip_wait(chip);
180 static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
181 .read = mv88e6xxx_smi_multi_chip_read,
182 .write = mv88e6xxx_smi_multi_chip_write,
185 static int mv88e6xxx_read(struct mv88e6xxx_chip *chip,
186 int addr, int reg, u16 *val)
190 assert_reg_lock(chip);
192 err = mv88e6xxx_smi_read(chip, addr, reg, val);
196 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
202 static int mv88e6xxx_write(struct mv88e6xxx_chip *chip,
203 int addr, int reg, u16 val)
207 assert_reg_lock(chip);
209 err = mv88e6xxx_smi_write(chip, addr, reg, val);
213 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
219 /* Indirect write to single pointer-data register with an Update bit */
220 static int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg,
226 /* Wait until the previous operation is completed */
227 for (i = 0; i < 16; ++i) {
228 err = mv88e6xxx_read(chip, addr, reg, &val);
232 if (!(val & BIT(15)))
239 /* Set the Update bit to trigger a write operation */
240 val = BIT(15) | update;
242 return mv88e6xxx_write(chip, addr, reg, val);
245 static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
250 err = mv88e6xxx_read(chip, addr, reg, &val);
257 static int mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
261 mutex_lock(&chip->reg_lock);
262 ret = _mv88e6xxx_reg_read(chip, addr, reg);
263 mutex_unlock(&chip->reg_lock);
268 static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
271 return mv88e6xxx_write(chip, addr, reg, val);
274 static int mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
279 mutex_lock(&chip->reg_lock);
280 ret = _mv88e6xxx_reg_write(chip, addr, reg, val);
281 mutex_unlock(&chip->reg_lock);
286 static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
288 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
291 err = mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
292 (addr[0] << 8) | addr[1]);
296 err = mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
297 (addr[2] << 8) | addr[3]);
301 return mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
302 (addr[4] << 8) | addr[5]);
305 static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
307 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
311 for (i = 0; i < 6; i++) {
314 /* Write the MAC address byte. */
315 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
316 GLOBAL2_SWITCH_MAC_BUSY |
321 /* Wait for the write to complete. */
322 for (j = 0; j < 16; j++) {
323 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2,
328 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
338 static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
340 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
342 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SWITCH_MAC))
343 return mv88e6xxx_set_addr_indirect(ds, addr);
345 return mv88e6xxx_set_addr_direct(ds, addr);
348 static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_chip *chip,
349 int addr, int regnum)
352 return _mv88e6xxx_reg_read(chip, addr, regnum);
356 static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_chip *chip,
357 int addr, int regnum, u16 val)
360 return _mv88e6xxx_reg_write(chip, addr, regnum, val);
364 static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
367 unsigned long timeout;
369 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
373 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
374 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
378 timeout = jiffies + 1 * HZ;
379 while (time_before(jiffies, timeout)) {
380 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
384 usleep_range(1000, 2000);
385 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
386 GLOBAL_STATUS_PPU_POLLING)
393 static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
396 unsigned long timeout;
398 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
402 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
403 ret | GLOBAL_CONTROL_PPU_ENABLE);
407 timeout = jiffies + 1 * HZ;
408 while (time_before(jiffies, timeout)) {
409 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
413 usleep_range(1000, 2000);
414 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
415 GLOBAL_STATUS_PPU_POLLING)
422 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
424 struct mv88e6xxx_chip *chip;
426 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
428 mutex_lock(&chip->reg_lock);
430 if (mutex_trylock(&chip->ppu_mutex)) {
431 if (mv88e6xxx_ppu_enable(chip) == 0)
432 chip->ppu_disabled = 0;
433 mutex_unlock(&chip->ppu_mutex);
436 mutex_unlock(&chip->reg_lock);
439 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
441 struct mv88e6xxx_chip *chip = (void *)_ps;
443 schedule_work(&chip->ppu_work);
446 static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
450 mutex_lock(&chip->ppu_mutex);
452 /* If the PHY polling unit is enabled, disable it so that
453 * we can access the PHY registers. If it was already
454 * disabled, cancel the timer that is going to re-enable
457 if (!chip->ppu_disabled) {
458 ret = mv88e6xxx_ppu_disable(chip);
460 mutex_unlock(&chip->ppu_mutex);
463 chip->ppu_disabled = 1;
465 del_timer(&chip->ppu_timer);
472 static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
474 /* Schedule a timer to re-enable the PHY polling unit. */
475 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
476 mutex_unlock(&chip->ppu_mutex);
479 static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
481 mutex_init(&chip->ppu_mutex);
482 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
483 init_timer(&chip->ppu_timer);
484 chip->ppu_timer.data = (unsigned long)chip;
485 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
488 static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_chip *chip, int addr,
493 ret = mv88e6xxx_ppu_access_get(chip);
495 ret = _mv88e6xxx_reg_read(chip, addr, regnum);
496 mv88e6xxx_ppu_access_put(chip);
502 static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_chip *chip, int addr,
507 ret = mv88e6xxx_ppu_access_get(chip);
509 ret = _mv88e6xxx_reg_write(chip, addr, regnum, val);
510 mv88e6xxx_ppu_access_put(chip);
516 static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
518 return chip->info->family == MV88E6XXX_FAMILY_6065;
521 static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
523 return chip->info->family == MV88E6XXX_FAMILY_6095;
526 static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
528 return chip->info->family == MV88E6XXX_FAMILY_6097;
531 static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
533 return chip->info->family == MV88E6XXX_FAMILY_6165;
536 static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
538 return chip->info->family == MV88E6XXX_FAMILY_6185;
541 static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
543 return chip->info->family == MV88E6XXX_FAMILY_6320;
546 static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
548 return chip->info->family == MV88E6XXX_FAMILY_6351;
551 static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
553 return chip->info->family == MV88E6XXX_FAMILY_6352;
556 static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
558 return chip->info->num_databases;
561 static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
563 /* Does the device have dedicated FID registers for ATU and VTU ops? */
564 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
565 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
571 /* We expect the switch to perform auto negotiation if there is a real
572 * phy. However, in the case of a fixed link phy, we force the port
573 * settings from the fixed link settings.
575 static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
576 struct phy_device *phydev)
578 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
582 if (!phy_is_pseudo_fixed_link(phydev))
585 mutex_lock(&chip->reg_lock);
587 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
591 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
592 PORT_PCS_CTRL_FORCE_LINK |
593 PORT_PCS_CTRL_DUPLEX_FULL |
594 PORT_PCS_CTRL_FORCE_DUPLEX |
595 PORT_PCS_CTRL_UNFORCED);
597 reg |= PORT_PCS_CTRL_FORCE_LINK;
599 reg |= PORT_PCS_CTRL_LINK_UP;
601 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
604 switch (phydev->speed) {
606 reg |= PORT_PCS_CTRL_1000;
609 reg |= PORT_PCS_CTRL_100;
612 reg |= PORT_PCS_CTRL_10;
615 pr_info("Unknown speed");
619 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
620 if (phydev->duplex == DUPLEX_FULL)
621 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
623 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
624 (port >= chip->info->num_ports - 2)) {
625 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
626 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
627 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
628 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
629 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
630 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
631 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
633 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
636 mutex_unlock(&chip->reg_lock);
639 static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
644 for (i = 0; i < 10; i++) {
645 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
646 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
653 static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
657 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
658 port = (port + 1) << 5;
660 /* Snapshot the hardware statistics counters for this port. */
661 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
662 GLOBAL_STATS_OP_CAPTURE_PORT |
663 GLOBAL_STATS_OP_HIST_RX_TX | port);
667 /* Wait for the snapshotting to complete. */
668 ret = _mv88e6xxx_stats_wait(chip);
675 static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
683 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
684 GLOBAL_STATS_OP_READ_CAPTURED |
685 GLOBAL_STATS_OP_HIST_RX_TX | stat);
689 ret = _mv88e6xxx_stats_wait(chip);
693 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
699 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
706 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
707 { "in_good_octets", 8, 0x00, BANK0, },
708 { "in_bad_octets", 4, 0x02, BANK0, },
709 { "in_unicast", 4, 0x04, BANK0, },
710 { "in_broadcasts", 4, 0x06, BANK0, },
711 { "in_multicasts", 4, 0x07, BANK0, },
712 { "in_pause", 4, 0x16, BANK0, },
713 { "in_undersize", 4, 0x18, BANK0, },
714 { "in_fragments", 4, 0x19, BANK0, },
715 { "in_oversize", 4, 0x1a, BANK0, },
716 { "in_jabber", 4, 0x1b, BANK0, },
717 { "in_rx_error", 4, 0x1c, BANK0, },
718 { "in_fcs_error", 4, 0x1d, BANK0, },
719 { "out_octets", 8, 0x0e, BANK0, },
720 { "out_unicast", 4, 0x10, BANK0, },
721 { "out_broadcasts", 4, 0x13, BANK0, },
722 { "out_multicasts", 4, 0x12, BANK0, },
723 { "out_pause", 4, 0x15, BANK0, },
724 { "excessive", 4, 0x11, BANK0, },
725 { "collisions", 4, 0x1e, BANK0, },
726 { "deferred", 4, 0x05, BANK0, },
727 { "single", 4, 0x14, BANK0, },
728 { "multiple", 4, 0x17, BANK0, },
729 { "out_fcs_error", 4, 0x03, BANK0, },
730 { "late", 4, 0x1f, BANK0, },
731 { "hist_64bytes", 4, 0x08, BANK0, },
732 { "hist_65_127bytes", 4, 0x09, BANK0, },
733 { "hist_128_255bytes", 4, 0x0a, BANK0, },
734 { "hist_256_511bytes", 4, 0x0b, BANK0, },
735 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
736 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
737 { "sw_in_discards", 4, 0x10, PORT, },
738 { "sw_in_filtered", 2, 0x12, PORT, },
739 { "sw_out_filtered", 2, 0x13, PORT, },
740 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
741 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
742 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
743 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
744 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
745 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
746 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
747 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
748 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
749 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
750 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
751 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
769 struct mv88e6xxx_hw_stat *stat)
771 switch (stat->type) {
775 return mv88e6xxx_6320_family(chip);
777 return mv88e6xxx_6095_family(chip) ||
778 mv88e6xxx_6185_family(chip) ||
779 mv88e6xxx_6097_family(chip) ||
780 mv88e6xxx_6165_family(chip) ||
781 mv88e6xxx_6351_family(chip) ||
782 mv88e6xxx_6352_family(chip);
787 static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
788 struct mv88e6xxx_hw_stat *s,
798 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
803 if (s->sizeof_stat == 4) {
804 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
813 _mv88e6xxx_stats_read(chip, s->reg, &low);
814 if (s->sizeof_stat == 8)
815 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
817 value = (((u64)high) << 16) | low;
821 static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
824 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
825 struct mv88e6xxx_hw_stat *stat;
828 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
829 stat = &mv88e6xxx_hw_stats[i];
830 if (mv88e6xxx_has_stat(chip, stat)) {
831 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
838 static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
840 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
841 struct mv88e6xxx_hw_stat *stat;
844 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
845 stat = &mv88e6xxx_hw_stats[i];
846 if (mv88e6xxx_has_stat(chip, stat))
852 static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
855 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
856 struct mv88e6xxx_hw_stat *stat;
860 mutex_lock(&chip->reg_lock);
862 ret = _mv88e6xxx_stats_snapshot(chip, port);
864 mutex_unlock(&chip->reg_lock);
867 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
868 stat = &mv88e6xxx_hw_stats[i];
869 if (mv88e6xxx_has_stat(chip, stat)) {
870 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
875 mutex_unlock(&chip->reg_lock);
878 static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
880 return 32 * sizeof(u16);
883 static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
884 struct ethtool_regs *regs, void *_p)
886 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
892 memset(p, 0xff, 32 * sizeof(u16));
894 mutex_lock(&chip->reg_lock);
896 for (i = 0; i < 32; i++) {
899 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
904 mutex_unlock(&chip->reg_lock);
907 static int _mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg, int offset,
910 unsigned long timeout = jiffies + HZ / 10;
912 while (time_before(jiffies, timeout)) {
915 ret = _mv88e6xxx_reg_read(chip, reg, offset);
921 usleep_range(1000, 2000);
926 static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg,
927 int offset, u16 mask)
931 mutex_lock(&chip->reg_lock);
932 ret = _mv88e6xxx_wait(chip, reg, offset, mask);
933 mutex_unlock(&chip->reg_lock);
938 static int mv88e6xxx_mdio_wait(struct mv88e6xxx_chip *chip)
940 return _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
941 GLOBAL2_SMI_OP_BUSY);
944 static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
946 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
948 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
949 GLOBAL2_EEPROM_OP_LOAD);
952 static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
954 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
956 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
957 GLOBAL2_EEPROM_OP_BUSY);
960 static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
962 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
965 mutex_lock(&chip->eeprom_mutex);
967 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
968 GLOBAL2_EEPROM_OP_READ |
969 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
973 ret = mv88e6xxx_eeprom_busy_wait(ds);
977 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
979 mutex_unlock(&chip->eeprom_mutex);
983 static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
985 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
987 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
988 return chip->eeprom_len;
993 static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
994 struct ethtool_eeprom *eeprom, u8 *data)
996 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1001 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
1004 offset = eeprom->offset;
1008 eeprom->magic = 0xc3ec4951;
1010 ret = mv88e6xxx_eeprom_load_wait(ds);
1017 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1021 *data++ = (word >> 8) & 0xff;
1031 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1035 *data++ = word & 0xff;
1036 *data++ = (word >> 8) & 0xff;
1046 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1050 *data++ = word & 0xff;
1060 static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
1062 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1065 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
1069 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
1075 static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
1078 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1081 mutex_lock(&chip->eeprom_mutex);
1083 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
1087 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
1088 GLOBAL2_EEPROM_OP_WRITE |
1089 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
1093 ret = mv88e6xxx_eeprom_busy_wait(ds);
1095 mutex_unlock(&chip->eeprom_mutex);
1099 static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
1100 struct ethtool_eeprom *eeprom, u8 *data)
1102 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1107 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
1110 if (eeprom->magic != 0xc3ec4951)
1113 ret = mv88e6xxx_eeprom_is_readonly(ds);
1117 offset = eeprom->offset;
1121 ret = mv88e6xxx_eeprom_load_wait(ds);
1128 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1132 word = (*data++ << 8) | (word & 0xff);
1134 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1147 word |= *data++ << 8;
1149 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1161 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1165 word = (word & 0xff00) | *data++;
1167 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1179 static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
1181 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
1182 GLOBAL_ATU_OP_BUSY);
1185 static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_chip *chip,
1186 int addr, int regnum)
1190 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
1191 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1196 ret = mv88e6xxx_mdio_wait(chip);
1200 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1205 static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_chip *chip,
1206 int addr, int regnum, u16 val)
1210 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
1214 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
1215 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1218 return mv88e6xxx_mdio_wait(chip);
1221 static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1222 struct ethtool_eee *e)
1224 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1227 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
1230 mutex_lock(&chip->reg_lock);
1232 reg = mv88e6xxx_mdio_read_indirect(chip, port, 16);
1236 e->eee_enabled = !!(reg & 0x0200);
1237 e->tx_lpi_enabled = !!(reg & 0x0100);
1239 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
1243 e->eee_active = !!(reg & PORT_STATUS_EEE);
1247 mutex_unlock(&chip->reg_lock);
1251 static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1252 struct phy_device *phydev, struct ethtool_eee *e)
1254 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1258 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
1261 mutex_lock(&chip->reg_lock);
1263 ret = mv88e6xxx_mdio_read_indirect(chip, port, 16);
1267 reg = ret & ~0x0300;
1270 if (e->tx_lpi_enabled)
1273 ret = mv88e6xxx_mdio_write_indirect(chip, port, 16, reg);
1275 mutex_unlock(&chip->reg_lock);
1280 static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
1284 if (mv88e6xxx_has_fid_reg(chip)) {
1285 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
1289 } else if (mv88e6xxx_num_databases(chip) == 256) {
1290 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
1291 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
1295 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1297 ((fid << 8) & 0xf000));
1301 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1305 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1309 return _mv88e6xxx_atu_wait(chip);
1312 static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
1313 struct mv88e6xxx_atu_entry *entry)
1315 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1317 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1318 unsigned int mask, shift;
1321 data |= GLOBAL_ATU_DATA_TRUNK;
1322 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1323 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1325 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1326 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1329 data |= (entry->portv_trunkid << shift) & mask;
1332 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1335 static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
1336 struct mv88e6xxx_atu_entry *entry,
1342 err = _mv88e6xxx_atu_wait(chip);
1346 err = _mv88e6xxx_atu_data_write(chip, entry);
1351 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1352 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1354 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1355 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1358 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
1361 static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
1362 u16 fid, bool static_too)
1364 struct mv88e6xxx_atu_entry entry = {
1366 .state = 0, /* EntryState bits must be 0 */
1369 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1372 static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
1373 int from_port, int to_port, bool static_too)
1375 struct mv88e6xxx_atu_entry entry = {
1380 /* EntryState bits must be 0xF */
1381 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1383 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1384 entry.portv_trunkid = (to_port & 0x0f) << 4;
1385 entry.portv_trunkid |= from_port & 0x0f;
1387 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1390 static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
1391 int port, bool static_too)
1393 /* Destination port 0xF means remove the entries */
1394 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
1397 static const char * const mv88e6xxx_port_state_names[] = {
1398 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1399 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1400 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1401 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1404 static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
1407 struct dsa_switch *ds = chip->ds;
1411 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
1415 oldstate = reg & PORT_CONTROL_STATE_MASK;
1417 if (oldstate != state) {
1418 /* Flush forwarding database if we're moving a port
1419 * from Learning or Forwarding state to Disabled or
1420 * Blocking or Listening state.
1422 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1423 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1424 (state == PORT_CONTROL_STATE_DISABLED ||
1425 state == PORT_CONTROL_STATE_BLOCKING)) {
1426 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
1431 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1432 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
1437 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1438 mv88e6xxx_port_state_names[state],
1439 mv88e6xxx_port_state_names[oldstate]);
1445 static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
1447 struct net_device *bridge = chip->ports[port].bridge_dev;
1448 const u16 mask = (1 << chip->info->num_ports) - 1;
1449 struct dsa_switch *ds = chip->ds;
1450 u16 output_ports = 0;
1454 /* allow CPU port or DSA link(s) to send frames to every port */
1455 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1456 output_ports = mask;
1458 for (i = 0; i < chip->info->num_ports; ++i) {
1459 /* allow sending frames to every group member */
1460 if (bridge && chip->ports[i].bridge_dev == bridge)
1461 output_ports |= BIT(i);
1463 /* allow sending frames to CPU port and DSA link(s) */
1464 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1465 output_ports |= BIT(i);
1469 /* prevent frames from going back out of the port they came in on */
1470 output_ports &= ~BIT(port);
1472 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1477 reg |= output_ports & mask;
1479 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
1482 static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1485 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1490 case BR_STATE_DISABLED:
1491 stp_state = PORT_CONTROL_STATE_DISABLED;
1493 case BR_STATE_BLOCKING:
1494 case BR_STATE_LISTENING:
1495 stp_state = PORT_CONTROL_STATE_BLOCKING;
1497 case BR_STATE_LEARNING:
1498 stp_state = PORT_CONTROL_STATE_LEARNING;
1500 case BR_STATE_FORWARDING:
1502 stp_state = PORT_CONTROL_STATE_FORWARDING;
1506 mutex_lock(&chip->reg_lock);
1507 err = _mv88e6xxx_port_state(chip, port, stp_state);
1508 mutex_unlock(&chip->reg_lock);
1511 netdev_err(ds->ports[port].netdev,
1512 "failed to update state to %s\n",
1513 mv88e6xxx_port_state_names[stp_state]);
1516 static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
1519 struct dsa_switch *ds = chip->ds;
1523 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
1527 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1530 ret &= ~PORT_DEFAULT_VLAN_MASK;
1531 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1533 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
1534 PORT_DEFAULT_VLAN, ret);
1538 netdev_dbg(ds->ports[port].netdev,
1539 "DefaultVID %d (was %d)\n", *new, pvid);
1548 static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
1549 int port, u16 *pvid)
1551 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
1554 static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
1557 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
1560 static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
1562 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1563 GLOBAL_VTU_OP_BUSY);
1566 static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
1570 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
1574 return _mv88e6xxx_vtu_wait(chip);
1577 static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
1581 ret = _mv88e6xxx_vtu_wait(chip);
1585 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
1588 static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
1589 struct mv88e6xxx_vtu_stu_entry *entry,
1590 unsigned int nibble_offset)
1596 for (i = 0; i < 3; ++i) {
1597 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1598 GLOBAL_VTU_DATA_0_3 + i);
1605 for (i = 0; i < chip->info->num_ports; ++i) {
1606 unsigned int shift = (i % 4) * 4 + nibble_offset;
1607 u16 reg = regs[i / 4];
1609 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1615 static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
1616 struct mv88e6xxx_vtu_stu_entry *entry)
1618 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
1621 static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
1622 struct mv88e6xxx_vtu_stu_entry *entry)
1624 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
1627 static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
1628 struct mv88e6xxx_vtu_stu_entry *entry,
1629 unsigned int nibble_offset)
1631 u16 regs[3] = { 0 };
1635 for (i = 0; i < chip->info->num_ports; ++i) {
1636 unsigned int shift = (i % 4) * 4 + nibble_offset;
1637 u8 data = entry->data[i];
1639 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1642 for (i = 0; i < 3; ++i) {
1643 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
1644 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1652 static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
1653 struct mv88e6xxx_vtu_stu_entry *entry)
1655 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
1658 static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
1659 struct mv88e6xxx_vtu_stu_entry *entry)
1661 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
1664 static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
1666 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
1667 vid & GLOBAL_VTU_VID_MASK);
1670 static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
1671 struct mv88e6xxx_vtu_stu_entry *entry)
1673 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1676 ret = _mv88e6xxx_vtu_wait(chip);
1680 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1684 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1688 next.vid = ret & GLOBAL_VTU_VID_MASK;
1689 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1692 ret = mv88e6xxx_vtu_data_read(chip, &next);
1696 if (mv88e6xxx_has_fid_reg(chip)) {
1697 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1702 next.fid = ret & GLOBAL_VTU_FID_MASK;
1703 } else if (mv88e6xxx_num_databases(chip) == 256) {
1704 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1705 * VTU DBNum[3:0] are located in VTU Operation 3:0
1707 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1712 next.fid = (ret & 0xf00) >> 4;
1713 next.fid |= ret & 0xf;
1716 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1717 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1722 next.sid = ret & GLOBAL_VTU_SID_MASK;
1730 static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1731 struct switchdev_obj_port_vlan *vlan,
1732 int (*cb)(struct switchdev_obj *obj))
1734 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1735 struct mv88e6xxx_vtu_stu_entry next;
1739 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1742 mutex_lock(&chip->reg_lock);
1744 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
1748 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1753 err = _mv88e6xxx_vtu_getnext(chip, &next);
1760 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1763 /* reinit and dump this VLAN obj */
1764 vlan->vid_begin = next.vid;
1765 vlan->vid_end = next.vid;
1768 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1769 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1771 if (next.vid == pvid)
1772 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1774 err = cb(&vlan->obj);
1777 } while (next.vid < GLOBAL_VTU_VID_MASK);
1780 mutex_unlock(&chip->reg_lock);
1785 static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
1786 struct mv88e6xxx_vtu_stu_entry *entry)
1788 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1792 ret = _mv88e6xxx_vtu_wait(chip);
1799 /* Write port member tags */
1800 ret = mv88e6xxx_vtu_data_write(chip, entry);
1804 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1805 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1806 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1812 if (mv88e6xxx_has_fid_reg(chip)) {
1813 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1814 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1818 } else if (mv88e6xxx_num_databases(chip) == 256) {
1819 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1820 * VTU DBNum[3:0] are located in VTU Operation 3:0
1822 op |= (entry->fid & 0xf0) << 8;
1823 op |= entry->fid & 0xf;
1826 reg = GLOBAL_VTU_VID_VALID;
1828 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1829 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1833 return _mv88e6xxx_vtu_cmd(chip, op);
1836 static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
1837 struct mv88e6xxx_vtu_stu_entry *entry)
1839 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1842 ret = _mv88e6xxx_vtu_wait(chip);
1846 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1847 sid & GLOBAL_VTU_SID_MASK);
1851 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1855 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
1859 next.sid = ret & GLOBAL_VTU_SID_MASK;
1861 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1865 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1868 ret = mv88e6xxx_stu_data_read(chip, &next);
1877 static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
1878 struct mv88e6xxx_vtu_stu_entry *entry)
1883 ret = _mv88e6xxx_vtu_wait(chip);
1890 /* Write port states */
1891 ret = mv88e6xxx_stu_data_write(chip, entry);
1895 reg = GLOBAL_VTU_VID_VALID;
1897 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1901 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1902 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1906 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1909 static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
1912 struct dsa_switch *ds = chip->ds;
1917 if (mv88e6xxx_num_databases(chip) == 4096)
1919 else if (mv88e6xxx_num_databases(chip) == 256)
1924 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1925 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1929 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1932 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1933 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1935 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
1941 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1942 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
1946 fid |= (ret & upper_mask) << 4;
1950 ret |= (*new >> 4) & upper_mask;
1952 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
1957 netdev_dbg(ds->ports[port].netdev,
1958 "FID %d (was %d)\n", *new, fid);
1967 static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
1970 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
1973 static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
1976 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
1979 static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
1981 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1982 struct mv88e6xxx_vtu_stu_entry vlan;
1985 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1987 /* Set every FID bit used by the (un)bridged ports */
1988 for (i = 0; i < chip->info->num_ports; ++i) {
1989 err = _mv88e6xxx_port_fid_get(chip, i, fid);
1993 set_bit(*fid, fid_bitmap);
1996 /* Set every FID bit used by the VLAN entries */
1997 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
2002 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2009 set_bit(vlan.fid, fid_bitmap);
2010 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2012 /* The reset value 0x000 is used to indicate that multiple address
2013 * databases are not needed. Return the next positive available.
2015 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
2016 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
2019 /* Clear the database */
2020 return _mv88e6xxx_atu_flush(chip, *fid, true);
2023 static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
2024 struct mv88e6xxx_vtu_stu_entry *entry)
2026 struct dsa_switch *ds = chip->ds;
2027 struct mv88e6xxx_vtu_stu_entry vlan = {
2033 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
2037 /* exclude all ports except the CPU and DSA ports */
2038 for (i = 0; i < chip->info->num_ports; ++i)
2039 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
2040 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
2041 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2043 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
2044 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
2045 struct mv88e6xxx_vtu_stu_entry vstp;
2047 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
2048 * implemented, only one STU entry is needed to cover all VTU
2049 * entries. Thus, validate the SID 0.
2052 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
2056 if (vstp.sid != vlan.sid || !vstp.valid) {
2057 memset(&vstp, 0, sizeof(vstp));
2059 vstp.sid = vlan.sid;
2061 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
2071 static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
2072 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
2079 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
2083 err = _mv88e6xxx_vtu_getnext(chip, entry);
2087 if (entry->vid != vid || !entry->valid) {
2090 /* -ENOENT would've been more appropriate, but switchdev expects
2091 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
2094 err = _mv88e6xxx_vtu_new(chip, vid, entry);
2100 static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
2101 u16 vid_begin, u16 vid_end)
2103 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2104 struct mv88e6xxx_vtu_stu_entry vlan;
2110 mutex_lock(&chip->reg_lock);
2112 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
2117 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2124 if (vlan.vid > vid_end)
2127 for (i = 0; i < chip->info->num_ports; ++i) {
2128 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2132 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2135 if (chip->ports[i].bridge_dev ==
2136 chip->ports[port].bridge_dev)
2137 break; /* same bridge, check next VLAN */
2139 netdev_warn(ds->ports[port].netdev,
2140 "hardware VLAN %d already used by %s\n",
2142 netdev_name(chip->ports[i].bridge_dev));
2146 } while (vlan.vid < vid_end);
2149 mutex_unlock(&chip->reg_lock);
2154 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2155 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2156 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2157 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2158 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2161 static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2162 bool vlan_filtering)
2164 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2165 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2166 PORT_CONTROL_2_8021Q_DISABLED;
2169 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2172 mutex_lock(&chip->reg_lock);
2174 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
2178 old = ret & PORT_CONTROL_2_8021Q_MASK;
2181 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2182 ret |= new & PORT_CONTROL_2_8021Q_MASK;
2184 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
2189 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
2190 mv88e6xxx_port_8021q_mode_names[new],
2191 mv88e6xxx_port_8021q_mode_names[old]);
2196 mutex_unlock(&chip->reg_lock);
2202 mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2203 const struct switchdev_obj_port_vlan *vlan,
2204 struct switchdev_trans *trans)
2206 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2209 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2212 /* If the requested port doesn't belong to the same bridge as the VLAN
2213 * members, do not support it (yet) and fallback to software VLAN.
2215 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2220 /* We don't need any dynamic resource from the kernel (yet),
2221 * so skip the prepare phase.
2226 static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
2227 u16 vid, bool untagged)
2229 struct mv88e6xxx_vtu_stu_entry vlan;
2232 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
2236 vlan.data[port] = untagged ?
2237 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2238 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2240 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2243 static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2244 const struct switchdev_obj_port_vlan *vlan,
2245 struct switchdev_trans *trans)
2247 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2248 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2249 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2252 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2255 mutex_lock(&chip->reg_lock);
2257 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
2258 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
2259 netdev_err(ds->ports[port].netdev,
2260 "failed to add VLAN %d%c\n",
2261 vid, untagged ? 'u' : 't');
2263 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
2264 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
2267 mutex_unlock(&chip->reg_lock);
2270 static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
2273 struct dsa_switch *ds = chip->ds;
2274 struct mv88e6xxx_vtu_stu_entry vlan;
2277 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2281 /* Tell switchdev if this VLAN is handled in software */
2282 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2285 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2287 /* keep the VLAN unless all ports are excluded */
2289 for (i = 0; i < chip->info->num_ports; ++i) {
2290 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
2293 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
2299 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2303 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
2306 static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2307 const struct switchdev_obj_port_vlan *vlan)
2309 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2313 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2316 mutex_lock(&chip->reg_lock);
2318 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
2322 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2323 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
2328 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
2335 mutex_unlock(&chip->reg_lock);
2340 static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
2341 const unsigned char *addr)
2345 for (i = 0; i < 3; i++) {
2346 ret = _mv88e6xxx_reg_write(
2347 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2348 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2356 static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
2357 unsigned char *addr)
2361 for (i = 0; i < 3; i++) {
2362 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
2363 GLOBAL_ATU_MAC_01 + i);
2366 addr[i * 2] = ret >> 8;
2367 addr[i * 2 + 1] = ret & 0xff;
2373 static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
2374 struct mv88e6xxx_atu_entry *entry)
2378 ret = _mv88e6xxx_atu_wait(chip);
2382 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
2386 ret = _mv88e6xxx_atu_data_write(chip, entry);
2390 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2393 static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
2394 const unsigned char *addr, u16 vid,
2397 struct mv88e6xxx_atu_entry entry = { 0 };
2398 struct mv88e6xxx_vtu_stu_entry vlan;
2401 /* Null VLAN ID corresponds to the port private database */
2403 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
2405 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2409 entry.fid = vlan.fid;
2410 entry.state = state;
2411 ether_addr_copy(entry.mac, addr);
2412 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2413 entry.trunk = false;
2414 entry.portv_trunkid = BIT(port);
2417 return _mv88e6xxx_atu_load(chip, &entry);
2420 static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2421 const struct switchdev_obj_port_fdb *fdb,
2422 struct switchdev_trans *trans)
2424 /* We don't need any dynamic resource from the kernel (yet),
2425 * so skip the prepare phase.
2430 static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2431 const struct switchdev_obj_port_fdb *fdb,
2432 struct switchdev_trans *trans)
2434 int state = is_multicast_ether_addr(fdb->addr) ?
2435 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2436 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2437 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2439 mutex_lock(&chip->reg_lock);
2440 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
2441 netdev_err(ds->ports[port].netdev,
2442 "failed to load MAC address\n");
2443 mutex_unlock(&chip->reg_lock);
2446 static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2447 const struct switchdev_obj_port_fdb *fdb)
2449 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2452 mutex_lock(&chip->reg_lock);
2453 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
2454 GLOBAL_ATU_DATA_STATE_UNUSED);
2455 mutex_unlock(&chip->reg_lock);
2460 static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2461 struct mv88e6xxx_atu_entry *entry)
2463 struct mv88e6xxx_atu_entry next = { 0 };
2468 ret = _mv88e6xxx_atu_wait(chip);
2472 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2476 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
2480 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
2484 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2485 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2486 unsigned int mask, shift;
2488 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2490 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2491 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2494 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2495 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2498 next.portv_trunkid = (ret & mask) >> shift;
2505 static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
2506 u16 fid, u16 vid, int port,
2507 struct switchdev_obj_port_fdb *fdb,
2508 int (*cb)(struct switchdev_obj *obj))
2510 struct mv88e6xxx_atu_entry addr = {
2511 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2515 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
2520 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
2524 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2527 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2528 bool is_static = addr.state ==
2529 (is_multicast_ether_addr(addr.mac) ?
2530 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2531 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2534 ether_addr_copy(fdb->addr, addr.mac);
2535 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2537 err = cb(&fdb->obj);
2541 } while (!is_broadcast_ether_addr(addr.mac));
2546 static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2547 struct switchdev_obj_port_fdb *fdb,
2548 int (*cb)(struct switchdev_obj *obj))
2550 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2551 struct mv88e6xxx_vtu_stu_entry vlan = {
2552 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2557 mutex_lock(&chip->reg_lock);
2559 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2560 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2564 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
2568 /* Dump VLANs' Filtering Information Databases */
2569 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2574 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2581 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2585 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2588 mutex_unlock(&chip->reg_lock);
2593 static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2594 struct net_device *bridge)
2596 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2599 mutex_lock(&chip->reg_lock);
2601 /* Assign the bridge and remap each port's VLANTable */
2602 chip->ports[port].bridge_dev = bridge;
2604 for (i = 0; i < chip->info->num_ports; ++i) {
2605 if (chip->ports[i].bridge_dev == bridge) {
2606 err = _mv88e6xxx_port_based_vlan_map(chip, i);
2612 mutex_unlock(&chip->reg_lock);
2617 static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2619 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2620 struct net_device *bridge = chip->ports[port].bridge_dev;
2623 mutex_lock(&chip->reg_lock);
2625 /* Unassign the bridge and remap each port's VLANTable */
2626 chip->ports[port].bridge_dev = NULL;
2628 for (i = 0; i < chip->info->num_ports; ++i)
2629 if (i == port || chip->ports[i].bridge_dev == bridge)
2630 if (_mv88e6xxx_port_based_vlan_map(chip, i))
2631 netdev_warn(ds->ports[i].netdev,
2632 "failed to remap\n");
2634 mutex_unlock(&chip->reg_lock);
2637 static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_chip *chip,
2638 int port, int page, int reg, int val)
2642 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
2644 goto restore_page_0;
2646 ret = mv88e6xxx_mdio_write_indirect(chip, port, reg, val);
2648 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
2653 static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_chip *chip,
2654 int port, int page, int reg)
2658 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
2660 goto restore_page_0;
2662 ret = mv88e6xxx_mdio_read_indirect(chip, port, reg);
2664 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
2669 static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
2671 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
2672 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2673 struct gpio_desc *gpiod = chip->reset;
2674 unsigned long timeout;
2678 /* Set all ports to the disabled state. */
2679 for (i = 0; i < chip->info->num_ports; i++) {
2680 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
2684 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
2690 /* Wait for transmit queues to drain. */
2691 usleep_range(2000, 4000);
2693 /* If there is a gpio connected to the reset pin, toggle it */
2695 gpiod_set_value_cansleep(gpiod, 1);
2696 usleep_range(10000, 20000);
2697 gpiod_set_value_cansleep(gpiod, 0);
2698 usleep_range(10000, 20000);
2701 /* Reset the switch. Keep the PPU active if requested. The PPU
2702 * needs to be active to support indirect phy register access
2703 * through global registers 0x18 and 0x19.
2706 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
2708 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
2712 /* Wait up to one second for reset to complete. */
2713 timeout = jiffies + 1 * HZ;
2714 while (time_before(jiffies, timeout)) {
2715 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
2719 if ((ret & is_reset) == is_reset)
2721 usleep_range(1000, 2000);
2723 if (time_after(jiffies, timeout))
2731 static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_chip *chip)
2735 ret = _mv88e6xxx_mdio_page_read(chip, REG_FIBER_SERDES,
2736 PAGE_FIBER_SERDES, MII_BMCR);
2740 if (ret & BMCR_PDOWN) {
2742 ret = _mv88e6xxx_mdio_page_write(chip, REG_FIBER_SERDES,
2743 PAGE_FIBER_SERDES, MII_BMCR,
2750 static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
2752 struct dsa_switch *ds = chip->ds;
2756 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2757 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2758 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2759 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
2760 /* MAC Forcing register: don't force link, speed,
2761 * duplex or flow control state to any particular
2762 * values on physical ports, but force the CPU port
2763 * and all DSA ports to their maximum bandwidth and
2766 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
2767 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2768 reg &= ~PORT_PCS_CTRL_UNFORCED;
2769 reg |= PORT_PCS_CTRL_FORCE_LINK |
2770 PORT_PCS_CTRL_LINK_UP |
2771 PORT_PCS_CTRL_DUPLEX_FULL |
2772 PORT_PCS_CTRL_FORCE_DUPLEX;
2773 if (mv88e6xxx_6065_family(chip))
2774 reg |= PORT_PCS_CTRL_100;
2776 reg |= PORT_PCS_CTRL_1000;
2778 reg |= PORT_PCS_CTRL_UNFORCED;
2781 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2782 PORT_PCS_CTRL, reg);
2787 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2788 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2789 * tunneling, determine priority by looking at 802.1p and IP
2790 * priority fields (IP prio has precedence), and set STP state
2793 * If this is the CPU link, use DSA or EDSA tagging depending
2794 * on which tagging mode was configured.
2796 * If this is a link to another switch, use DSA tagging mode.
2798 * If this is the upstream port for this switch, enable
2799 * forwarding of unknown unicasts and multicasts.
2802 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2803 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2804 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2805 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
2806 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2807 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2808 PORT_CONTROL_STATE_FORWARDING;
2809 if (dsa_is_cpu_port(ds, port)) {
2810 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
2811 reg |= PORT_CONTROL_DSA_TAG;
2812 if (mv88e6xxx_6352_family(chip) ||
2813 mv88e6xxx_6351_family(chip) ||
2814 mv88e6xxx_6165_family(chip) ||
2815 mv88e6xxx_6097_family(chip) ||
2816 mv88e6xxx_6320_family(chip)) {
2817 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2818 PORT_CONTROL_FORWARD_UNKNOWN |
2819 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2822 if (mv88e6xxx_6352_family(chip) ||
2823 mv88e6xxx_6351_family(chip) ||
2824 mv88e6xxx_6165_family(chip) ||
2825 mv88e6xxx_6097_family(chip) ||
2826 mv88e6xxx_6095_family(chip) ||
2827 mv88e6xxx_6065_family(chip) ||
2828 mv88e6xxx_6185_family(chip) ||
2829 mv88e6xxx_6320_family(chip)) {
2830 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2833 if (dsa_is_dsa_port(ds, port)) {
2834 if (mv88e6xxx_6095_family(chip) ||
2835 mv88e6xxx_6185_family(chip))
2836 reg |= PORT_CONTROL_DSA_TAG;
2837 if (mv88e6xxx_6352_family(chip) ||
2838 mv88e6xxx_6351_family(chip) ||
2839 mv88e6xxx_6165_family(chip) ||
2840 mv88e6xxx_6097_family(chip) ||
2841 mv88e6xxx_6320_family(chip)) {
2842 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2845 if (port == dsa_upstream_port(ds))
2846 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2847 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2850 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2856 /* If this port is connected to a SerDes, make sure the SerDes is not
2859 if (mv88e6xxx_6352_family(chip)) {
2860 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
2863 ret &= PORT_STATUS_CMODE_MASK;
2864 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2865 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2866 (ret == PORT_STATUS_CMODE_SGMII)) {
2867 ret = mv88e6xxx_power_on_serdes(chip);
2873 /* Port Control 2: don't force a good FCS, set the maximum frame size to
2874 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2875 * untagged frames on this port, do a destination address lookup on all
2876 * received packets as usual, disable ARP mirroring and don't send a
2877 * copy of all transmitted/received frames on this port to the CPU.
2880 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2881 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2882 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2883 mv88e6xxx_6185_family(chip))
2884 reg = PORT_CONTROL_2_MAP_DA;
2886 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2887 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
2888 reg |= PORT_CONTROL_2_JUMBO_10240;
2890 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
2891 /* Set the upstream port this port should use */
2892 reg |= dsa_upstream_port(ds);
2893 /* enable forwarding of unknown multicast addresses to
2896 if (port == dsa_upstream_port(ds))
2897 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2900 reg |= PORT_CONTROL_2_8021Q_DISABLED;
2903 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2904 PORT_CONTROL_2, reg);
2909 /* Port Association Vector: when learning source addresses
2910 * of packets, add the address to the address database using
2911 * a port bitmap that has only the bit for this port set and
2912 * the other bits clear.
2915 /* Disable learning for CPU port */
2916 if (dsa_is_cpu_port(ds, port))
2919 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2924 /* Egress rate control 2: disable egress rate control. */
2925 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
2930 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2931 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2932 mv88e6xxx_6320_family(chip)) {
2933 /* Do not limit the period of time that this port can
2934 * be paused for by the remote end or the period of
2935 * time that this port can pause the remote end.
2937 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2938 PORT_PAUSE_CTRL, 0x0000);
2942 /* Port ATU control: disable limiting the number of
2943 * address database entries that this port is allowed
2946 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2947 PORT_ATU_CONTROL, 0x0000);
2948 /* Priority Override: disable DA, SA and VTU priority
2951 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2952 PORT_PRI_OVERRIDE, 0x0000);
2956 /* Port Ethertype: use the Ethertype DSA Ethertype
2959 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2960 PORT_ETH_TYPE, ETH_P_EDSA);
2963 /* Tag Remap: use an identity 802.1p prio -> switch
2966 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2967 PORT_TAG_REGMAP_0123, 0x3210);
2971 /* Tag Remap 2: use an identity 802.1p prio -> switch
2974 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2975 PORT_TAG_REGMAP_4567, 0x7654);
2980 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2981 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2982 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2983 mv88e6xxx_6320_family(chip)) {
2984 /* Rate Control: disable ingress rate limiting. */
2985 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2986 PORT_RATE_CONTROL, 0x0001);
2991 /* Port Control 1: disable trunking, disable sending
2992 * learning messages to this port.
2994 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2999 /* Port based VLAN map: give each port the same default address
3000 * database, and allow bidirectional communication between the
3001 * CPU and DSA port(s), and the other ports.
3003 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
3007 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
3011 /* Default VLAN ID and priority: don't set a default VLAN
3012 * ID, and set the default packet priority to zero.
3014 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
3022 static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
3024 struct dsa_switch *ds = chip->ds;
3025 u32 upstream_port = dsa_upstream_port(ds);
3029 /* Enable the PHY Polling Unit if present, don't discard any packets,
3030 * and mask all interrupt sources.
3033 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
3034 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
3035 reg |= GLOBAL_CONTROL_PPU_ENABLE;
3037 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
3041 /* Configure the upstream port, and configure it as the port to which
3042 * ingress and egress and ARP monitor frames are to be sent.
3044 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
3045 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
3046 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
3047 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
3052 /* Disable remote management, and set the switch's DSA device number. */
3053 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
3054 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
3055 (ds->index & 0x1f));
3059 /* Set the default address aging time to 5 minutes, and
3060 * enable address learn messages to be sent to all message
3063 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
3064 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
3068 /* Clear all the VTU and STU entries */
3069 err = _mv88e6xxx_vtu_stu_flush(chip);
3073 /* Clear all ATU entries */
3074 err = _mv88e6xxx_atu_flush(chip, 0, true);
3078 /* Configure the IP ToS mapping registers. */
3079 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
3082 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
3085 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
3088 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
3091 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
3094 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
3097 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
3100 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
3104 /* Configure the IEEE 802.1p priority mapping register. */
3105 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
3109 /* Clear the statistics counters for all ports */
3110 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
3111 GLOBAL_STATS_OP_FLUSH_ALL);
3115 /* Wait for the flush to complete. */
3116 err = _mv88e6xxx_stats_wait(chip);
3123 static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
3124 int target, int port)
3126 u16 val = (target << 8) | (port & 0xf);
3128 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
3131 static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
3136 /* Initialize the routing port to the 32 possible target devices */
3137 for (target = 0; target < 32; ++target) {
3140 if (target < DSA_MAX_SWITCHES) {
3141 port = chip->ds->rtable[target];
3142 if (port == DSA_RTABLE_NONE)
3146 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
3154 static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
3155 bool hask, u16 mask)
3157 const u16 port_mask = BIT(chip->info->num_ports) - 1;
3158 u16 val = (num << 12) | (mask & port_mask);
3161 val |= GLOBAL2_TRUNK_MASK_HASK;
3163 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
3166 static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
3169 const u16 port_mask = BIT(chip->info->num_ports) - 1;
3170 u16 val = (id << 11) | (map & port_mask);
3172 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
3175 static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
3177 const u16 port_mask = BIT(chip->info->num_ports) - 1;
3180 /* Clear all eight possible Trunk Mask vectors */
3181 for (i = 0; i < 8; ++i) {
3182 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
3187 /* Clear all sixteen possible Trunk ID routing vectors */
3188 for (i = 0; i < 16; ++i) {
3189 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
3197 static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3203 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3204 /* Consider the frames with reserved multicast destination
3205 * addresses matching 01:80:c2:00:00:2x as MGMT.
3207 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3213 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3214 /* Consider the frames with reserved multicast destination
3215 * addresses matching 01:80:c2:00:00:0x as MGMT.
3217 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3223 /* Ignore removed tag data on doubly tagged packets, disable
3224 * flow control messages, force flow control priority to the
3225 * highest, and send all special multicast frames to the CPU
3226 * port at the highest priority.
3228 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3229 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3230 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3231 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3232 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
3236 /* Program the DSA routing table. */
3237 err = mv88e6xxx_g2_set_device_mapping(chip);
3241 /* Clear all trunk masks and mapping. */
3242 err = mv88e6xxx_g2_clear_trunk(chip);
3246 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
3247 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
3248 mv88e6xxx_6320_family(chip)) {
3249 /* Initialise cross-chip port VLAN table to reset
3252 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3253 GLOBAL2_PVT_ADDR, 0x9000);
3257 /* Clear the priority override table. */
3258 for (i = 0; i < 16; i++) {
3259 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3260 GLOBAL2_PRIO_OVERRIDE,
3267 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
3268 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
3269 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
3270 mv88e6xxx_6320_family(chip)) {
3271 /* Disable ingress rate limiting by resetting all
3272 * ingress rate limit registers to their initial
3275 for (i = 0; i < chip->info->num_ports; i++) {
3276 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL2,
3287 static int mv88e6xxx_setup(struct dsa_switch *ds)
3289 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3294 ds->slave_mii_bus = chip->mdio_bus;
3296 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
3297 mutex_init(&chip->eeprom_mutex);
3299 mutex_lock(&chip->reg_lock);
3301 err = mv88e6xxx_switch_reset(chip);
3305 /* Setup Switch Port Registers */
3306 for (i = 0; i < chip->info->num_ports; i++) {
3307 err = mv88e6xxx_setup_port(chip, i);
3312 /* Setup Switch Global 1 Registers */
3313 err = mv88e6xxx_g1_setup(chip);
3317 /* Setup Switch Global 2 Registers */
3318 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3319 err = mv88e6xxx_g2_setup(chip);
3325 mutex_unlock(&chip->reg_lock);
3330 static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3333 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3336 mutex_lock(&chip->reg_lock);
3337 ret = _mv88e6xxx_mdio_page_read(chip, port, page, reg);
3338 mutex_unlock(&chip->reg_lock);
3343 static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3346 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3349 mutex_lock(&chip->reg_lock);
3350 ret = _mv88e6xxx_mdio_page_write(chip, port, page, reg, val);
3351 mutex_unlock(&chip->reg_lock);
3356 static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_chip *chip, int port)
3358 if (port >= 0 && port < chip->info->num_ports)
3363 static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
3365 struct mv88e6xxx_chip *chip = bus->priv;
3366 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
3372 mutex_lock(&chip->reg_lock);
3374 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3375 ret = mv88e6xxx_mdio_read_ppu(chip, addr, regnum);
3376 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3377 ret = mv88e6xxx_mdio_read_indirect(chip, addr, regnum);
3379 ret = mv88e6xxx_mdio_read_direct(chip, addr, regnum);
3381 mutex_unlock(&chip->reg_lock);
3385 static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
3388 struct mv88e6xxx_chip *chip = bus->priv;
3389 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
3395 mutex_lock(&chip->reg_lock);
3397 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3398 ret = mv88e6xxx_mdio_write_ppu(chip, addr, regnum, val);
3399 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3400 ret = mv88e6xxx_mdio_write_indirect(chip, addr, regnum, val);
3402 ret = mv88e6xxx_mdio_write_direct(chip, addr, regnum, val);
3404 mutex_unlock(&chip->reg_lock);
3408 static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
3409 struct device_node *np)
3412 struct mii_bus *bus;
3415 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3416 mv88e6xxx_ppu_state_init(chip);
3419 chip->mdio_np = of_get_child_by_name(np, "mdio");
3421 bus = devm_mdiobus_alloc(chip->dev);
3425 bus->priv = (void *)chip;
3427 bus->name = np->full_name;
3428 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3430 bus->name = "mv88e6xxx SMI";
3431 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3434 bus->read = mv88e6xxx_mdio_read;
3435 bus->write = mv88e6xxx_mdio_write;
3436 bus->parent = chip->dev;
3439 err = of_mdiobus_register(bus, chip->mdio_np);
3441 err = mdiobus_register(bus);
3443 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
3446 chip->mdio_bus = bus;
3452 of_node_put(chip->mdio_np);
3457 static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
3460 struct mii_bus *bus = chip->mdio_bus;
3462 mdiobus_unregister(bus);
3465 of_node_put(chip->mdio_np);
3468 #ifdef CONFIG_NET_DSA_HWMON
3470 static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3472 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3478 mutex_lock(&chip->reg_lock);
3480 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x6);
3484 /* Enable temperature sensor */
3485 ret = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
3489 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret | (1 << 5));
3493 /* Wait for temperature to stabilize */
3494 usleep_range(10000, 12000);
3496 val = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
3502 /* Disable temperature sensor */
3503 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret & ~(1 << 5));
3507 *temp = ((val & 0x1f) - 5) * 5;
3510 mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x0);
3511 mutex_unlock(&chip->reg_lock);
3515 static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3517 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3518 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3523 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
3527 *temp = (ret & 0xff) - 25;
3532 static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3534 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3536 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
3539 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
3540 return mv88e63xx_get_temp(ds, temp);
3542 return mv88e61xx_get_temp(ds, temp);
3545 static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3547 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3548 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3551 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3556 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3560 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3565 static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3567 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3568 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3571 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3574 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3577 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3578 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3579 (ret & 0xe0ff) | (temp << 8));
3582 static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3584 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3585 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3588 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3593 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3597 *alarm = !!(ret & 0x40);
3601 #endif /* CONFIG_NET_DSA_HWMON */
3603 static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3605 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3606 .family = MV88E6XXX_FAMILY_6097,
3607 .name = "Marvell 88E6085",
3608 .num_databases = 4096,
3610 .port_base_addr = 0x10,
3611 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3615 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3616 .family = MV88E6XXX_FAMILY_6095,
3617 .name = "Marvell 88E6095/88E6095F",
3618 .num_databases = 256,
3620 .port_base_addr = 0x10,
3621 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3625 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3626 .family = MV88E6XXX_FAMILY_6165,
3627 .name = "Marvell 88E6123",
3628 .num_databases = 4096,
3630 .port_base_addr = 0x10,
3631 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3635 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3636 .family = MV88E6XXX_FAMILY_6185,
3637 .name = "Marvell 88E6131",
3638 .num_databases = 256,
3640 .port_base_addr = 0x10,
3641 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3645 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3646 .family = MV88E6XXX_FAMILY_6165,
3647 .name = "Marvell 88E6161",
3648 .num_databases = 4096,
3650 .port_base_addr = 0x10,
3651 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3655 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3656 .family = MV88E6XXX_FAMILY_6165,
3657 .name = "Marvell 88E6165",
3658 .num_databases = 4096,
3660 .port_base_addr = 0x10,
3661 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3665 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3666 .family = MV88E6XXX_FAMILY_6351,
3667 .name = "Marvell 88E6171",
3668 .num_databases = 4096,
3670 .port_base_addr = 0x10,
3671 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3675 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3676 .family = MV88E6XXX_FAMILY_6352,
3677 .name = "Marvell 88E6172",
3678 .num_databases = 4096,
3680 .port_base_addr = 0x10,
3681 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3685 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3686 .family = MV88E6XXX_FAMILY_6351,
3687 .name = "Marvell 88E6175",
3688 .num_databases = 4096,
3690 .port_base_addr = 0x10,
3691 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3695 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3696 .family = MV88E6XXX_FAMILY_6352,
3697 .name = "Marvell 88E6176",
3698 .num_databases = 4096,
3700 .port_base_addr = 0x10,
3701 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3705 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3706 .family = MV88E6XXX_FAMILY_6185,
3707 .name = "Marvell 88E6185",
3708 .num_databases = 256,
3710 .port_base_addr = 0x10,
3711 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3715 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3716 .family = MV88E6XXX_FAMILY_6352,
3717 .name = "Marvell 88E6240",
3718 .num_databases = 4096,
3720 .port_base_addr = 0x10,
3721 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3725 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3726 .family = MV88E6XXX_FAMILY_6320,
3727 .name = "Marvell 88E6320",
3728 .num_databases = 4096,
3730 .port_base_addr = 0x10,
3731 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3735 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3736 .family = MV88E6XXX_FAMILY_6320,
3737 .name = "Marvell 88E6321",
3738 .num_databases = 4096,
3740 .port_base_addr = 0x10,
3741 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3745 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3746 .family = MV88E6XXX_FAMILY_6351,
3747 .name = "Marvell 88E6350",
3748 .num_databases = 4096,
3750 .port_base_addr = 0x10,
3751 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3755 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3756 .family = MV88E6XXX_FAMILY_6351,
3757 .name = "Marvell 88E6351",
3758 .num_databases = 4096,
3760 .port_base_addr = 0x10,
3761 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3765 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3766 .family = MV88E6XXX_FAMILY_6352,
3767 .name = "Marvell 88E6352",
3768 .num_databases = 4096,
3770 .port_base_addr = 0x10,
3771 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3775 static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
3779 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3780 if (mv88e6xxx_table[i].prod_num == prod_num)
3781 return &mv88e6xxx_table[i];
3786 static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
3788 const struct mv88e6xxx_info *info;
3789 int id, prod_num, rev;
3791 id = mv88e6xxx_reg_read(chip, chip->info->port_base_addr,
3796 prod_num = (id & 0xfff0) >> 4;
3799 info = mv88e6xxx_lookup_info(prod_num);
3803 /* Update the compatible info with the probed one */
3806 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3807 chip->info->prod_num, chip->info->name, rev);
3812 static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
3814 struct mv88e6xxx_chip *chip;
3816 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3822 mutex_init(&chip->reg_lock);
3827 static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
3828 struct mii_bus *bus, int sw_addr)
3830 /* ADDR[0] pin is unavailable externally and considered zero */
3835 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3836 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_MULTI_CHIP))
3837 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3842 chip->sw_addr = sw_addr;
3847 static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3848 struct device *host_dev, int sw_addr,
3851 struct mv88e6xxx_chip *chip;
3852 struct mii_bus *bus;
3855 bus = dsa_host_dev_to_mii_bus(host_dev);
3859 chip = mv88e6xxx_alloc_chip(dsa_dev);
3863 /* Legacy SMI probing will only support chips similar to 88E6085 */
3864 chip->info = &mv88e6xxx_table[MV88E6085];
3866 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
3870 err = mv88e6xxx_detect(chip);
3874 err = mv88e6xxx_mdio_register(chip, NULL);
3880 return chip->info->name;
3882 devm_kfree(dsa_dev, chip);
3887 static struct dsa_switch_driver mv88e6xxx_switch_driver = {
3888 .tag_protocol = DSA_TAG_PROTO_EDSA,
3889 .probe = mv88e6xxx_drv_probe,
3890 .setup = mv88e6xxx_setup,
3891 .set_addr = mv88e6xxx_set_addr,
3892 .adjust_link = mv88e6xxx_adjust_link,
3893 .get_strings = mv88e6xxx_get_strings,
3894 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3895 .get_sset_count = mv88e6xxx_get_sset_count,
3896 .set_eee = mv88e6xxx_set_eee,
3897 .get_eee = mv88e6xxx_get_eee,
3898 #ifdef CONFIG_NET_DSA_HWMON
3899 .get_temp = mv88e6xxx_get_temp,
3900 .get_temp_limit = mv88e6xxx_get_temp_limit,
3901 .set_temp_limit = mv88e6xxx_set_temp_limit,
3902 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3904 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
3905 .get_eeprom = mv88e6xxx_get_eeprom,
3906 .set_eeprom = mv88e6xxx_set_eeprom,
3907 .get_regs_len = mv88e6xxx_get_regs_len,
3908 .get_regs = mv88e6xxx_get_regs,
3909 .port_bridge_join = mv88e6xxx_port_bridge_join,
3910 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3911 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3912 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3913 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3914 .port_vlan_add = mv88e6xxx_port_vlan_add,
3915 .port_vlan_del = mv88e6xxx_port_vlan_del,
3916 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3917 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3918 .port_fdb_add = mv88e6xxx_port_fdb_add,
3919 .port_fdb_del = mv88e6xxx_port_fdb_del,
3920 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3923 static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
3924 struct device_node *np)
3926 struct device *dev = chip->dev;
3927 struct dsa_switch *ds;
3929 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3935 ds->drv = &mv88e6xxx_switch_driver;
3937 dev_set_drvdata(dev, ds);
3939 return dsa_register_switch(ds, np);
3942 static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
3944 dsa_unregister_switch(chip->ds);
3947 static int mv88e6xxx_probe(struct mdio_device *mdiodev)
3949 struct device *dev = &mdiodev->dev;
3950 struct device_node *np = dev->of_node;
3951 const struct mv88e6xxx_info *compat_info;
3952 struct mv88e6xxx_chip *chip;
3956 compat_info = of_device_get_match_data(dev);
3960 chip = mv88e6xxx_alloc_chip(dev);
3964 chip->info = compat_info;
3966 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
3970 err = mv88e6xxx_detect(chip);
3974 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3975 if (IS_ERR(chip->reset))
3976 return PTR_ERR(chip->reset);
3978 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM) &&
3979 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
3980 chip->eeprom_len = eeprom_len;
3982 err = mv88e6xxx_mdio_register(chip, np);
3986 err = mv88e6xxx_register_switch(chip, np);
3988 mv88e6xxx_mdio_unregister(chip);
3995 static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3997 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
3998 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
4000 mv88e6xxx_unregister_switch(chip);
4001 mv88e6xxx_mdio_unregister(chip);
4004 static const struct of_device_id mv88e6xxx_of_match[] = {
4006 .compatible = "marvell,mv88e6085",
4007 .data = &mv88e6xxx_table[MV88E6085],
4012 MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4014 static struct mdio_driver mv88e6xxx_driver = {
4015 .probe = mv88e6xxx_probe,
4016 .remove = mv88e6xxx_remove,
4018 .name = "mv88e6085",
4019 .of_match_table = mv88e6xxx_of_match,
4023 static int __init mv88e6xxx_init(void)
4025 register_switch_driver(&mv88e6xxx_switch_driver);
4026 return mdio_driver_register(&mv88e6xxx_driver);
4028 module_init(mv88e6xxx_init);
4030 static void __exit mv88e6xxx_cleanup(void)
4032 mdio_driver_unregister(&mv88e6xxx_driver);
4033 unregister_switch_driver(&mv88e6xxx_switch_driver);
4035 module_exit(mv88e6xxx_cleanup);
4037 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4038 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4039 MODULE_LICENSE("GPL");