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 static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
222 int addr = phy; /* PHY devices addresses start at 0x0 */
227 return chip->phy_ops->read(chip, addr, reg, val);
230 static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
233 int addr = phy; /* PHY devices addresses start at 0x0 */
238 return chip->phy_ops->write(chip, addr, reg, val);
241 static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
243 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
246 return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
249 static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
253 /* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
254 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
256 dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
261 static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
262 u8 page, int reg, u16 *val)
266 /* There is no paging for registers 22 */
270 err = mv88e6xxx_phy_page_get(chip, phy, page);
272 err = mv88e6xxx_phy_read(chip, phy, reg, val);
273 mv88e6xxx_phy_page_put(chip, phy);
279 static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
280 u8 page, int reg, u16 val)
284 /* There is no paging for registers 22 */
288 err = mv88e6xxx_phy_page_get(chip, phy, page);
290 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
291 mv88e6xxx_phy_page_put(chip, phy);
297 static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
299 return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
303 static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
305 return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
309 static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg,
312 unsigned long timeout = jiffies + HZ / 10;
314 while (time_before(jiffies, timeout)) {
318 err = mv88e6xxx_read(chip, addr, reg, &val);
325 usleep_range(1000, 2000);
331 /* Indirect write to single pointer-data register with an Update bit */
332 static int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg,
338 /* Wait until the previous operation is completed */
339 for (i = 0; i < 16; ++i) {
340 err = mv88e6xxx_read(chip, addr, reg, &val);
344 if (!(val & BIT(15)))
351 /* Set the Update bit to trigger a write operation */
352 val = BIT(15) | update;
354 return mv88e6xxx_write(chip, addr, reg, val);
357 static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
362 err = mv88e6xxx_read(chip, addr, reg, &val);
369 static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
372 return mv88e6xxx_write(chip, addr, reg, val);
375 static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
378 unsigned long timeout;
380 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
384 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
385 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
389 timeout = jiffies + 1 * HZ;
390 while (time_before(jiffies, timeout)) {
391 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
395 usleep_range(1000, 2000);
396 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
397 GLOBAL_STATUS_PPU_POLLING)
404 static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
407 unsigned long timeout;
409 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
413 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
414 ret | GLOBAL_CONTROL_PPU_ENABLE);
418 timeout = jiffies + 1 * HZ;
419 while (time_before(jiffies, timeout)) {
420 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
424 usleep_range(1000, 2000);
425 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
426 GLOBAL_STATUS_PPU_POLLING)
433 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
435 struct mv88e6xxx_chip *chip;
437 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
439 mutex_lock(&chip->reg_lock);
441 if (mutex_trylock(&chip->ppu_mutex)) {
442 if (mv88e6xxx_ppu_enable(chip) == 0)
443 chip->ppu_disabled = 0;
444 mutex_unlock(&chip->ppu_mutex);
447 mutex_unlock(&chip->reg_lock);
450 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
452 struct mv88e6xxx_chip *chip = (void *)_ps;
454 schedule_work(&chip->ppu_work);
457 static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
461 mutex_lock(&chip->ppu_mutex);
463 /* If the PHY polling unit is enabled, disable it so that
464 * we can access the PHY registers. If it was already
465 * disabled, cancel the timer that is going to re-enable
468 if (!chip->ppu_disabled) {
469 ret = mv88e6xxx_ppu_disable(chip);
471 mutex_unlock(&chip->ppu_mutex);
474 chip->ppu_disabled = 1;
476 del_timer(&chip->ppu_timer);
483 static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
485 /* Schedule a timer to re-enable the PHY polling unit. */
486 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
487 mutex_unlock(&chip->ppu_mutex);
490 static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
492 mutex_init(&chip->ppu_mutex);
493 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
494 init_timer(&chip->ppu_timer);
495 chip->ppu_timer.data = (unsigned long)chip;
496 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
499 static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
504 err = mv88e6xxx_ppu_access_get(chip);
506 err = mv88e6xxx_read(chip, addr, reg, val);
507 mv88e6xxx_ppu_access_put(chip);
513 static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
518 err = mv88e6xxx_ppu_access_get(chip);
520 err = mv88e6xxx_write(chip, addr, reg, val);
521 mv88e6xxx_ppu_access_put(chip);
527 static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
528 .read = mv88e6xxx_phy_ppu_read,
529 .write = mv88e6xxx_phy_ppu_write,
532 static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
534 return chip->info->family == MV88E6XXX_FAMILY_6065;
537 static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
539 return chip->info->family == MV88E6XXX_FAMILY_6095;
542 static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
544 return chip->info->family == MV88E6XXX_FAMILY_6097;
547 static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
549 return chip->info->family == MV88E6XXX_FAMILY_6165;
552 static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
554 return chip->info->family == MV88E6XXX_FAMILY_6185;
557 static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
559 return chip->info->family == MV88E6XXX_FAMILY_6320;
562 static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
564 return chip->info->family == MV88E6XXX_FAMILY_6351;
567 static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
569 return chip->info->family == MV88E6XXX_FAMILY_6352;
572 static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
574 return chip->info->num_databases;
577 static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
579 /* Does the device have dedicated FID registers for ATU and VTU ops? */
580 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
581 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
587 /* We expect the switch to perform auto negotiation if there is a real
588 * phy. However, in the case of a fixed link phy, we force the port
589 * settings from the fixed link settings.
591 static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
592 struct phy_device *phydev)
594 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
598 if (!phy_is_pseudo_fixed_link(phydev))
601 mutex_lock(&chip->reg_lock);
603 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
607 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
608 PORT_PCS_CTRL_FORCE_LINK |
609 PORT_PCS_CTRL_DUPLEX_FULL |
610 PORT_PCS_CTRL_FORCE_DUPLEX |
611 PORT_PCS_CTRL_UNFORCED);
613 reg |= PORT_PCS_CTRL_FORCE_LINK;
615 reg |= PORT_PCS_CTRL_LINK_UP;
617 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
620 switch (phydev->speed) {
622 reg |= PORT_PCS_CTRL_1000;
625 reg |= PORT_PCS_CTRL_100;
628 reg |= PORT_PCS_CTRL_10;
631 pr_info("Unknown speed");
635 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
636 if (phydev->duplex == DUPLEX_FULL)
637 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
639 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
640 (port >= chip->info->num_ports - 2)) {
641 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
642 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
643 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
644 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
645 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
646 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
647 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
649 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
652 mutex_unlock(&chip->reg_lock);
655 static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
660 for (i = 0; i < 10; i++) {
661 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
662 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
669 static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
673 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
674 port = (port + 1) << 5;
676 /* Snapshot the hardware statistics counters for this port. */
677 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
678 GLOBAL_STATS_OP_CAPTURE_PORT |
679 GLOBAL_STATS_OP_HIST_RX_TX | port);
683 /* Wait for the snapshotting to complete. */
684 ret = _mv88e6xxx_stats_wait(chip);
691 static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
699 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
700 GLOBAL_STATS_OP_READ_CAPTURED |
701 GLOBAL_STATS_OP_HIST_RX_TX | stat);
705 ret = _mv88e6xxx_stats_wait(chip);
709 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
715 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
722 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
723 { "in_good_octets", 8, 0x00, BANK0, },
724 { "in_bad_octets", 4, 0x02, BANK0, },
725 { "in_unicast", 4, 0x04, BANK0, },
726 { "in_broadcasts", 4, 0x06, BANK0, },
727 { "in_multicasts", 4, 0x07, BANK0, },
728 { "in_pause", 4, 0x16, BANK0, },
729 { "in_undersize", 4, 0x18, BANK0, },
730 { "in_fragments", 4, 0x19, BANK0, },
731 { "in_oversize", 4, 0x1a, BANK0, },
732 { "in_jabber", 4, 0x1b, BANK0, },
733 { "in_rx_error", 4, 0x1c, BANK0, },
734 { "in_fcs_error", 4, 0x1d, BANK0, },
735 { "out_octets", 8, 0x0e, BANK0, },
736 { "out_unicast", 4, 0x10, BANK0, },
737 { "out_broadcasts", 4, 0x13, BANK0, },
738 { "out_multicasts", 4, 0x12, BANK0, },
739 { "out_pause", 4, 0x15, BANK0, },
740 { "excessive", 4, 0x11, BANK0, },
741 { "collisions", 4, 0x1e, BANK0, },
742 { "deferred", 4, 0x05, BANK0, },
743 { "single", 4, 0x14, BANK0, },
744 { "multiple", 4, 0x17, BANK0, },
745 { "out_fcs_error", 4, 0x03, BANK0, },
746 { "late", 4, 0x1f, BANK0, },
747 { "hist_64bytes", 4, 0x08, BANK0, },
748 { "hist_65_127bytes", 4, 0x09, BANK0, },
749 { "hist_128_255bytes", 4, 0x0a, BANK0, },
750 { "hist_256_511bytes", 4, 0x0b, BANK0, },
751 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
752 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
753 { "sw_in_discards", 4, 0x10, PORT, },
754 { "sw_in_filtered", 2, 0x12, PORT, },
755 { "sw_out_filtered", 2, 0x13, PORT, },
756 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
770 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
771 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
772 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
773 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
774 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
775 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
776 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
777 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
778 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
779 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
780 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
781 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
784 static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
785 struct mv88e6xxx_hw_stat *stat)
787 switch (stat->type) {
791 return mv88e6xxx_6320_family(chip);
793 return mv88e6xxx_6095_family(chip) ||
794 mv88e6xxx_6185_family(chip) ||
795 mv88e6xxx_6097_family(chip) ||
796 mv88e6xxx_6165_family(chip) ||
797 mv88e6xxx_6351_family(chip) ||
798 mv88e6xxx_6352_family(chip);
803 static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
804 struct mv88e6xxx_hw_stat *s,
814 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
819 if (s->sizeof_stat == 4) {
820 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
829 _mv88e6xxx_stats_read(chip, s->reg, &low);
830 if (s->sizeof_stat == 8)
831 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
833 value = (((u64)high) << 16) | low;
837 static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
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)) {
847 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
854 static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
856 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
857 struct mv88e6xxx_hw_stat *stat;
860 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
861 stat = &mv88e6xxx_hw_stats[i];
862 if (mv88e6xxx_has_stat(chip, stat))
868 static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
871 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
872 struct mv88e6xxx_hw_stat *stat;
876 mutex_lock(&chip->reg_lock);
878 ret = _mv88e6xxx_stats_snapshot(chip, port);
880 mutex_unlock(&chip->reg_lock);
883 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
884 stat = &mv88e6xxx_hw_stats[i];
885 if (mv88e6xxx_has_stat(chip, stat)) {
886 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
891 mutex_unlock(&chip->reg_lock);
894 static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
896 return 32 * sizeof(u16);
899 static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
900 struct ethtool_regs *regs, void *_p)
902 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
908 memset(p, 0xff, 32 * sizeof(u16));
910 mutex_lock(&chip->reg_lock);
912 for (i = 0; i < 32; i++) {
915 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
920 mutex_unlock(&chip->reg_lock);
923 static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
925 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
929 static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
930 struct ethtool_eee *e)
932 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
936 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
939 mutex_lock(&chip->reg_lock);
941 err = mv88e6xxx_phy_read(chip, port, 16, ®);
945 e->eee_enabled = !!(reg & 0x0200);
946 e->tx_lpi_enabled = !!(reg & 0x0100);
948 err = mv88e6xxx_read(chip, REG_PORT(port), PORT_STATUS, ®);
952 e->eee_active = !!(reg & PORT_STATUS_EEE);
954 mutex_unlock(&chip->reg_lock);
959 static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
960 struct phy_device *phydev, struct ethtool_eee *e)
962 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
966 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
969 mutex_lock(&chip->reg_lock);
971 err = mv88e6xxx_phy_read(chip, port, 16, ®);
978 if (e->tx_lpi_enabled)
981 err = mv88e6xxx_phy_write(chip, port, 16, reg);
983 mutex_unlock(&chip->reg_lock);
988 static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
992 if (mv88e6xxx_has_fid_reg(chip)) {
993 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
997 } else if (mv88e6xxx_num_databases(chip) == 256) {
998 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
999 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
1003 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1005 ((fid << 8) & 0xf000));
1009 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1013 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1017 return _mv88e6xxx_atu_wait(chip);
1020 static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
1021 struct mv88e6xxx_atu_entry *entry)
1023 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1025 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1026 unsigned int mask, shift;
1029 data |= GLOBAL_ATU_DATA_TRUNK;
1030 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1031 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1033 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1034 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1037 data |= (entry->portv_trunkid << shift) & mask;
1040 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1043 static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
1044 struct mv88e6xxx_atu_entry *entry,
1050 err = _mv88e6xxx_atu_wait(chip);
1054 err = _mv88e6xxx_atu_data_write(chip, entry);
1059 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1060 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1062 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1063 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1066 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
1069 static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
1070 u16 fid, bool static_too)
1072 struct mv88e6xxx_atu_entry entry = {
1074 .state = 0, /* EntryState bits must be 0 */
1077 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1080 static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
1081 int from_port, int to_port, bool static_too)
1083 struct mv88e6xxx_atu_entry entry = {
1088 /* EntryState bits must be 0xF */
1089 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1091 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1092 entry.portv_trunkid = (to_port & 0x0f) << 4;
1093 entry.portv_trunkid |= from_port & 0x0f;
1095 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1098 static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
1099 int port, bool static_too)
1101 /* Destination port 0xF means remove the entries */
1102 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
1105 static const char * const mv88e6xxx_port_state_names[] = {
1106 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1107 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1108 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1109 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1112 static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
1115 struct dsa_switch *ds = chip->ds;
1119 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
1123 oldstate = reg & PORT_CONTROL_STATE_MASK;
1125 if (oldstate != state) {
1126 /* Flush forwarding database if we're moving a port
1127 * from Learning or Forwarding state to Disabled or
1128 * Blocking or Listening state.
1130 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1131 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1132 (state == PORT_CONTROL_STATE_DISABLED ||
1133 state == PORT_CONTROL_STATE_BLOCKING)) {
1134 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
1139 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1140 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
1145 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1146 mv88e6xxx_port_state_names[state],
1147 mv88e6xxx_port_state_names[oldstate]);
1153 static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
1155 struct net_device *bridge = chip->ports[port].bridge_dev;
1156 const u16 mask = (1 << chip->info->num_ports) - 1;
1157 struct dsa_switch *ds = chip->ds;
1158 u16 output_ports = 0;
1162 /* allow CPU port or DSA link(s) to send frames to every port */
1163 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1164 output_ports = mask;
1166 for (i = 0; i < chip->info->num_ports; ++i) {
1167 /* allow sending frames to every group member */
1168 if (bridge && chip->ports[i].bridge_dev == bridge)
1169 output_ports |= BIT(i);
1171 /* allow sending frames to CPU port and DSA link(s) */
1172 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1173 output_ports |= BIT(i);
1177 /* prevent frames from going back out of the port they came in on */
1178 output_ports &= ~BIT(port);
1180 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1185 reg |= output_ports & mask;
1187 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
1190 static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1193 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1198 case BR_STATE_DISABLED:
1199 stp_state = PORT_CONTROL_STATE_DISABLED;
1201 case BR_STATE_BLOCKING:
1202 case BR_STATE_LISTENING:
1203 stp_state = PORT_CONTROL_STATE_BLOCKING;
1205 case BR_STATE_LEARNING:
1206 stp_state = PORT_CONTROL_STATE_LEARNING;
1208 case BR_STATE_FORWARDING:
1210 stp_state = PORT_CONTROL_STATE_FORWARDING;
1214 mutex_lock(&chip->reg_lock);
1215 err = _mv88e6xxx_port_state(chip, port, stp_state);
1216 mutex_unlock(&chip->reg_lock);
1219 netdev_err(ds->ports[port].netdev,
1220 "failed to update state to %s\n",
1221 mv88e6xxx_port_state_names[stp_state]);
1224 static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
1227 struct dsa_switch *ds = chip->ds;
1231 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
1235 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1238 ret &= ~PORT_DEFAULT_VLAN_MASK;
1239 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1241 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
1242 PORT_DEFAULT_VLAN, ret);
1246 netdev_dbg(ds->ports[port].netdev,
1247 "DefaultVID %d (was %d)\n", *new, pvid);
1256 static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
1257 int port, u16 *pvid)
1259 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
1262 static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
1265 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
1268 static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
1270 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1271 GLOBAL_VTU_OP_BUSY);
1274 static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
1278 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
1282 return _mv88e6xxx_vtu_wait(chip);
1285 static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
1289 ret = _mv88e6xxx_vtu_wait(chip);
1293 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
1296 static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
1297 struct mv88e6xxx_vtu_stu_entry *entry,
1298 unsigned int nibble_offset)
1304 for (i = 0; i < 3; ++i) {
1305 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1306 GLOBAL_VTU_DATA_0_3 + i);
1313 for (i = 0; i < chip->info->num_ports; ++i) {
1314 unsigned int shift = (i % 4) * 4 + nibble_offset;
1315 u16 reg = regs[i / 4];
1317 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1323 static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
1324 struct mv88e6xxx_vtu_stu_entry *entry)
1326 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
1329 static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
1330 struct mv88e6xxx_vtu_stu_entry *entry)
1332 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
1335 static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
1336 struct mv88e6xxx_vtu_stu_entry *entry,
1337 unsigned int nibble_offset)
1339 u16 regs[3] = { 0 };
1343 for (i = 0; i < chip->info->num_ports; ++i) {
1344 unsigned int shift = (i % 4) * 4 + nibble_offset;
1345 u8 data = entry->data[i];
1347 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1350 for (i = 0; i < 3; ++i) {
1351 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
1352 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1360 static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
1361 struct mv88e6xxx_vtu_stu_entry *entry)
1363 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
1366 static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
1367 struct mv88e6xxx_vtu_stu_entry *entry)
1369 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
1372 static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
1374 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
1375 vid & GLOBAL_VTU_VID_MASK);
1378 static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
1379 struct mv88e6xxx_vtu_stu_entry *entry)
1381 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1384 ret = _mv88e6xxx_vtu_wait(chip);
1388 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1392 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1396 next.vid = ret & GLOBAL_VTU_VID_MASK;
1397 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1400 ret = mv88e6xxx_vtu_data_read(chip, &next);
1404 if (mv88e6xxx_has_fid_reg(chip)) {
1405 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1410 next.fid = ret & GLOBAL_VTU_FID_MASK;
1411 } else if (mv88e6xxx_num_databases(chip) == 256) {
1412 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1413 * VTU DBNum[3:0] are located in VTU Operation 3:0
1415 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1420 next.fid = (ret & 0xf00) >> 4;
1421 next.fid |= ret & 0xf;
1424 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1425 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1430 next.sid = ret & GLOBAL_VTU_SID_MASK;
1438 static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1439 struct switchdev_obj_port_vlan *vlan,
1440 int (*cb)(struct switchdev_obj *obj))
1442 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1443 struct mv88e6xxx_vtu_stu_entry next;
1447 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1450 mutex_lock(&chip->reg_lock);
1452 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
1456 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1461 err = _mv88e6xxx_vtu_getnext(chip, &next);
1468 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1471 /* reinit and dump this VLAN obj */
1472 vlan->vid_begin = next.vid;
1473 vlan->vid_end = next.vid;
1476 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1477 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1479 if (next.vid == pvid)
1480 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1482 err = cb(&vlan->obj);
1485 } while (next.vid < GLOBAL_VTU_VID_MASK);
1488 mutex_unlock(&chip->reg_lock);
1493 static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
1494 struct mv88e6xxx_vtu_stu_entry *entry)
1496 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1500 ret = _mv88e6xxx_vtu_wait(chip);
1507 /* Write port member tags */
1508 ret = mv88e6xxx_vtu_data_write(chip, entry);
1512 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1513 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1514 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1520 if (mv88e6xxx_has_fid_reg(chip)) {
1521 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1522 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1526 } else if (mv88e6xxx_num_databases(chip) == 256) {
1527 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1528 * VTU DBNum[3:0] are located in VTU Operation 3:0
1530 op |= (entry->fid & 0xf0) << 8;
1531 op |= entry->fid & 0xf;
1534 reg = GLOBAL_VTU_VID_VALID;
1536 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1537 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1541 return _mv88e6xxx_vtu_cmd(chip, op);
1544 static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
1545 struct mv88e6xxx_vtu_stu_entry *entry)
1547 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1550 ret = _mv88e6xxx_vtu_wait(chip);
1554 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1555 sid & GLOBAL_VTU_SID_MASK);
1559 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1563 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
1567 next.sid = ret & GLOBAL_VTU_SID_MASK;
1569 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1573 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1576 ret = mv88e6xxx_stu_data_read(chip, &next);
1585 static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
1586 struct mv88e6xxx_vtu_stu_entry *entry)
1591 ret = _mv88e6xxx_vtu_wait(chip);
1598 /* Write port states */
1599 ret = mv88e6xxx_stu_data_write(chip, entry);
1603 reg = GLOBAL_VTU_VID_VALID;
1605 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1609 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1610 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1614 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1617 static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
1620 struct dsa_switch *ds = chip->ds;
1625 if (mv88e6xxx_num_databases(chip) == 4096)
1627 else if (mv88e6xxx_num_databases(chip) == 256)
1632 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1633 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1637 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1640 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1641 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1643 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
1649 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1650 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
1654 fid |= (ret & upper_mask) << 4;
1658 ret |= (*new >> 4) & upper_mask;
1660 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
1665 netdev_dbg(ds->ports[port].netdev,
1666 "FID %d (was %d)\n", *new, fid);
1675 static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
1678 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
1681 static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
1684 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
1687 static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
1689 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1690 struct mv88e6xxx_vtu_stu_entry vlan;
1693 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1695 /* Set every FID bit used by the (un)bridged ports */
1696 for (i = 0; i < chip->info->num_ports; ++i) {
1697 err = _mv88e6xxx_port_fid_get(chip, i, fid);
1701 set_bit(*fid, fid_bitmap);
1704 /* Set every FID bit used by the VLAN entries */
1705 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1710 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1717 set_bit(vlan.fid, fid_bitmap);
1718 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1720 /* The reset value 0x000 is used to indicate that multiple address
1721 * databases are not needed. Return the next positive available.
1723 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1724 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
1727 /* Clear the database */
1728 return _mv88e6xxx_atu_flush(chip, *fid, true);
1731 static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
1732 struct mv88e6xxx_vtu_stu_entry *entry)
1734 struct dsa_switch *ds = chip->ds;
1735 struct mv88e6xxx_vtu_stu_entry vlan = {
1741 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
1745 /* exclude all ports except the CPU and DSA ports */
1746 for (i = 0; i < chip->info->num_ports; ++i)
1747 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1748 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1749 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1751 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1752 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
1753 struct mv88e6xxx_vtu_stu_entry vstp;
1755 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1756 * implemented, only one STU entry is needed to cover all VTU
1757 * entries. Thus, validate the SID 0.
1760 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
1764 if (vstp.sid != vlan.sid || !vstp.valid) {
1765 memset(&vstp, 0, sizeof(vstp));
1767 vstp.sid = vlan.sid;
1769 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
1779 static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
1780 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1787 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
1791 err = _mv88e6xxx_vtu_getnext(chip, entry);
1795 if (entry->vid != vid || !entry->valid) {
1798 /* -ENOENT would've been more appropriate, but switchdev expects
1799 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1802 err = _mv88e6xxx_vtu_new(chip, vid, entry);
1808 static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1809 u16 vid_begin, u16 vid_end)
1811 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1812 struct mv88e6xxx_vtu_stu_entry vlan;
1818 mutex_lock(&chip->reg_lock);
1820 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
1825 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1832 if (vlan.vid > vid_end)
1835 for (i = 0; i < chip->info->num_ports; ++i) {
1836 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1840 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1843 if (chip->ports[i].bridge_dev ==
1844 chip->ports[port].bridge_dev)
1845 break; /* same bridge, check next VLAN */
1847 netdev_warn(ds->ports[port].netdev,
1848 "hardware VLAN %d already used by %s\n",
1850 netdev_name(chip->ports[i].bridge_dev));
1854 } while (vlan.vid < vid_end);
1857 mutex_unlock(&chip->reg_lock);
1862 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1863 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1864 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1865 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1866 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1869 static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1870 bool vlan_filtering)
1872 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1873 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1874 PORT_CONTROL_2_8021Q_DISABLED;
1877 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1880 mutex_lock(&chip->reg_lock);
1882 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
1886 old = ret & PORT_CONTROL_2_8021Q_MASK;
1889 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1890 ret |= new & PORT_CONTROL_2_8021Q_MASK;
1892 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
1897 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
1898 mv88e6xxx_port_8021q_mode_names[new],
1899 mv88e6xxx_port_8021q_mode_names[old]);
1904 mutex_unlock(&chip->reg_lock);
1910 mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1911 const struct switchdev_obj_port_vlan *vlan,
1912 struct switchdev_trans *trans)
1914 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1917 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1920 /* If the requested port doesn't belong to the same bridge as the VLAN
1921 * members, do not support it (yet) and fallback to software VLAN.
1923 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1928 /* We don't need any dynamic resource from the kernel (yet),
1929 * so skip the prepare phase.
1934 static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
1935 u16 vid, bool untagged)
1937 struct mv88e6xxx_vtu_stu_entry vlan;
1940 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
1944 vlan.data[port] = untagged ?
1945 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1946 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1948 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
1951 static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1952 const struct switchdev_obj_port_vlan *vlan,
1953 struct switchdev_trans *trans)
1955 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1956 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1957 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1960 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1963 mutex_lock(&chip->reg_lock);
1965 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
1966 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
1967 netdev_err(ds->ports[port].netdev,
1968 "failed to add VLAN %d%c\n",
1969 vid, untagged ? 'u' : 't');
1971 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
1972 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
1975 mutex_unlock(&chip->reg_lock);
1978 static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
1981 struct dsa_switch *ds = chip->ds;
1982 struct mv88e6xxx_vtu_stu_entry vlan;
1985 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
1989 /* Tell switchdev if this VLAN is handled in software */
1990 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1993 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1995 /* keep the VLAN unless all ports are excluded */
1997 for (i = 0; i < chip->info->num_ports; ++i) {
1998 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
2001 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
2007 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2011 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
2014 static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2015 const struct switchdev_obj_port_vlan *vlan)
2017 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2021 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2024 mutex_lock(&chip->reg_lock);
2026 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
2030 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2031 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
2036 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
2043 mutex_unlock(&chip->reg_lock);
2048 static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
2049 const unsigned char *addr)
2053 for (i = 0; i < 3; i++) {
2054 ret = _mv88e6xxx_reg_write(
2055 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2056 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2064 static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
2065 unsigned char *addr)
2069 for (i = 0; i < 3; i++) {
2070 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
2071 GLOBAL_ATU_MAC_01 + i);
2074 addr[i * 2] = ret >> 8;
2075 addr[i * 2 + 1] = ret & 0xff;
2081 static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
2082 struct mv88e6xxx_atu_entry *entry)
2086 ret = _mv88e6xxx_atu_wait(chip);
2090 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
2094 ret = _mv88e6xxx_atu_data_write(chip, entry);
2098 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2101 static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
2102 const unsigned char *addr, u16 vid,
2105 struct mv88e6xxx_atu_entry entry = { 0 };
2106 struct mv88e6xxx_vtu_stu_entry vlan;
2109 /* Null VLAN ID corresponds to the port private database */
2111 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
2113 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2117 entry.fid = vlan.fid;
2118 entry.state = state;
2119 ether_addr_copy(entry.mac, addr);
2120 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2121 entry.trunk = false;
2122 entry.portv_trunkid = BIT(port);
2125 return _mv88e6xxx_atu_load(chip, &entry);
2128 static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2129 const struct switchdev_obj_port_fdb *fdb,
2130 struct switchdev_trans *trans)
2132 /* We don't need any dynamic resource from the kernel (yet),
2133 * so skip the prepare phase.
2138 static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2139 const struct switchdev_obj_port_fdb *fdb,
2140 struct switchdev_trans *trans)
2142 int state = is_multicast_ether_addr(fdb->addr) ?
2143 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2144 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2145 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2147 mutex_lock(&chip->reg_lock);
2148 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
2149 netdev_err(ds->ports[port].netdev,
2150 "failed to load MAC address\n");
2151 mutex_unlock(&chip->reg_lock);
2154 static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2155 const struct switchdev_obj_port_fdb *fdb)
2157 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2160 mutex_lock(&chip->reg_lock);
2161 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
2162 GLOBAL_ATU_DATA_STATE_UNUSED);
2163 mutex_unlock(&chip->reg_lock);
2168 static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2169 struct mv88e6xxx_atu_entry *entry)
2171 struct mv88e6xxx_atu_entry next = { 0 };
2176 ret = _mv88e6xxx_atu_wait(chip);
2180 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2184 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
2188 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
2192 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2193 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2194 unsigned int mask, shift;
2196 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2198 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2199 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2202 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2203 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2206 next.portv_trunkid = (ret & mask) >> shift;
2213 static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
2214 u16 fid, u16 vid, int port,
2215 struct switchdev_obj_port_fdb *fdb,
2216 int (*cb)(struct switchdev_obj *obj))
2218 struct mv88e6xxx_atu_entry addr = {
2219 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2223 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
2228 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
2232 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2235 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2236 bool is_static = addr.state ==
2237 (is_multicast_ether_addr(addr.mac) ?
2238 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2239 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2242 ether_addr_copy(fdb->addr, addr.mac);
2243 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2245 err = cb(&fdb->obj);
2249 } while (!is_broadcast_ether_addr(addr.mac));
2254 static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2255 struct switchdev_obj_port_fdb *fdb,
2256 int (*cb)(struct switchdev_obj *obj))
2258 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2259 struct mv88e6xxx_vtu_stu_entry vlan = {
2260 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2265 mutex_lock(&chip->reg_lock);
2267 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2268 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2272 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
2276 /* Dump VLANs' Filtering Information Databases */
2277 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2282 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2289 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2293 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2296 mutex_unlock(&chip->reg_lock);
2301 static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2302 struct net_device *bridge)
2304 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2307 mutex_lock(&chip->reg_lock);
2309 /* Assign the bridge and remap each port's VLANTable */
2310 chip->ports[port].bridge_dev = bridge;
2312 for (i = 0; i < chip->info->num_ports; ++i) {
2313 if (chip->ports[i].bridge_dev == bridge) {
2314 err = _mv88e6xxx_port_based_vlan_map(chip, i);
2320 mutex_unlock(&chip->reg_lock);
2325 static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2327 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2328 struct net_device *bridge = chip->ports[port].bridge_dev;
2331 mutex_lock(&chip->reg_lock);
2333 /* Unassign the bridge and remap each port's VLANTable */
2334 chip->ports[port].bridge_dev = NULL;
2336 for (i = 0; i < chip->info->num_ports; ++i)
2337 if (i == port || chip->ports[i].bridge_dev == bridge)
2338 if (_mv88e6xxx_port_based_vlan_map(chip, i))
2339 netdev_warn(ds->ports[i].netdev,
2340 "failed to remap\n");
2342 mutex_unlock(&chip->reg_lock);
2345 static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
2347 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
2348 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2349 struct gpio_desc *gpiod = chip->reset;
2350 unsigned long timeout;
2354 /* Set all ports to the disabled state. */
2355 for (i = 0; i < chip->info->num_ports; i++) {
2356 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
2360 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
2366 /* Wait for transmit queues to drain. */
2367 usleep_range(2000, 4000);
2369 /* If there is a gpio connected to the reset pin, toggle it */
2371 gpiod_set_value_cansleep(gpiod, 1);
2372 usleep_range(10000, 20000);
2373 gpiod_set_value_cansleep(gpiod, 0);
2374 usleep_range(10000, 20000);
2377 /* Reset the switch. Keep the PPU active if requested. The PPU
2378 * needs to be active to support indirect phy register access
2379 * through global registers 0x18 and 0x19.
2382 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
2384 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
2388 /* Wait up to one second for reset to complete. */
2389 timeout = jiffies + 1 * HZ;
2390 while (time_before(jiffies, timeout)) {
2391 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
2395 if ((ret & is_reset) == is_reset)
2397 usleep_range(1000, 2000);
2399 if (time_after(jiffies, timeout))
2407 static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
2412 /* Clear Power Down bit */
2413 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2417 if (val & BMCR_PDOWN) {
2419 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
2425 static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port,
2428 int addr = chip->info->port_base_addr + port;
2430 if (port >= chip->info->num_ports)
2433 return mv88e6xxx_read(chip, addr, reg, val);
2436 static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
2438 struct dsa_switch *ds = chip->ds;
2442 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2443 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2444 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2445 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
2446 /* MAC Forcing register: don't force link, speed,
2447 * duplex or flow control state to any particular
2448 * values on physical ports, but force the CPU port
2449 * and all DSA ports to their maximum bandwidth and
2452 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
2453 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2454 reg &= ~PORT_PCS_CTRL_UNFORCED;
2455 reg |= PORT_PCS_CTRL_FORCE_LINK |
2456 PORT_PCS_CTRL_LINK_UP |
2457 PORT_PCS_CTRL_DUPLEX_FULL |
2458 PORT_PCS_CTRL_FORCE_DUPLEX;
2459 if (mv88e6xxx_6065_family(chip))
2460 reg |= PORT_PCS_CTRL_100;
2462 reg |= PORT_PCS_CTRL_1000;
2464 reg |= PORT_PCS_CTRL_UNFORCED;
2467 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2468 PORT_PCS_CTRL, reg);
2473 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2474 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2475 * tunneling, determine priority by looking at 802.1p and IP
2476 * priority fields (IP prio has precedence), and set STP state
2479 * If this is the CPU link, use DSA or EDSA tagging depending
2480 * on which tagging mode was configured.
2482 * If this is a link to another switch, use DSA tagging mode.
2484 * If this is the upstream port for this switch, enable
2485 * forwarding of unknown unicasts and multicasts.
2488 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2489 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2490 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2491 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
2492 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2493 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2494 PORT_CONTROL_STATE_FORWARDING;
2495 if (dsa_is_cpu_port(ds, port)) {
2496 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
2497 reg |= PORT_CONTROL_DSA_TAG;
2498 if (mv88e6xxx_6352_family(chip) ||
2499 mv88e6xxx_6351_family(chip) ||
2500 mv88e6xxx_6165_family(chip) ||
2501 mv88e6xxx_6097_family(chip) ||
2502 mv88e6xxx_6320_family(chip)) {
2503 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2504 PORT_CONTROL_FORWARD_UNKNOWN |
2505 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2508 if (mv88e6xxx_6352_family(chip) ||
2509 mv88e6xxx_6351_family(chip) ||
2510 mv88e6xxx_6165_family(chip) ||
2511 mv88e6xxx_6097_family(chip) ||
2512 mv88e6xxx_6095_family(chip) ||
2513 mv88e6xxx_6065_family(chip) ||
2514 mv88e6xxx_6185_family(chip) ||
2515 mv88e6xxx_6320_family(chip)) {
2516 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2519 if (dsa_is_dsa_port(ds, port)) {
2520 if (mv88e6xxx_6095_family(chip) ||
2521 mv88e6xxx_6185_family(chip))
2522 reg |= PORT_CONTROL_DSA_TAG;
2523 if (mv88e6xxx_6352_family(chip) ||
2524 mv88e6xxx_6351_family(chip) ||
2525 mv88e6xxx_6165_family(chip) ||
2526 mv88e6xxx_6097_family(chip) ||
2527 mv88e6xxx_6320_family(chip)) {
2528 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2531 if (port == dsa_upstream_port(ds))
2532 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2533 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2536 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2542 /* If this port is connected to a SerDes, make sure the SerDes is not
2545 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
2546 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
2549 ret &= PORT_STATUS_CMODE_MASK;
2550 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2551 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2552 (ret == PORT_STATUS_CMODE_SGMII)) {
2553 ret = mv88e6xxx_serdes_power_on(chip);
2559 /* Port Control 2: don't force a good FCS, set the maximum frame size to
2560 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2561 * untagged frames on this port, do a destination address lookup on all
2562 * received packets as usual, disable ARP mirroring and don't send a
2563 * copy of all transmitted/received frames on this port to the CPU.
2566 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2567 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2568 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2569 mv88e6xxx_6185_family(chip))
2570 reg = PORT_CONTROL_2_MAP_DA;
2572 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2573 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
2574 reg |= PORT_CONTROL_2_JUMBO_10240;
2576 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
2577 /* Set the upstream port this port should use */
2578 reg |= dsa_upstream_port(ds);
2579 /* enable forwarding of unknown multicast addresses to
2582 if (port == dsa_upstream_port(ds))
2583 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2586 reg |= PORT_CONTROL_2_8021Q_DISABLED;
2589 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2590 PORT_CONTROL_2, reg);
2595 /* Port Association Vector: when learning source addresses
2596 * of packets, add the address to the address database using
2597 * a port bitmap that has only the bit for this port set and
2598 * the other bits clear.
2601 /* Disable learning for CPU port */
2602 if (dsa_is_cpu_port(ds, port))
2605 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2610 /* Egress rate control 2: disable egress rate control. */
2611 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
2616 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2617 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2618 mv88e6xxx_6320_family(chip)) {
2619 /* Do not limit the period of time that this port can
2620 * be paused for by the remote end or the period of
2621 * time that this port can pause the remote end.
2623 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2624 PORT_PAUSE_CTRL, 0x0000);
2628 /* Port ATU control: disable limiting the number of
2629 * address database entries that this port is allowed
2632 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2633 PORT_ATU_CONTROL, 0x0000);
2634 /* Priority Override: disable DA, SA and VTU priority
2637 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2638 PORT_PRI_OVERRIDE, 0x0000);
2642 /* Port Ethertype: use the Ethertype DSA Ethertype
2645 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2646 PORT_ETH_TYPE, ETH_P_EDSA);
2649 /* Tag Remap: use an identity 802.1p prio -> switch
2652 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2653 PORT_TAG_REGMAP_0123, 0x3210);
2657 /* Tag Remap 2: use an identity 802.1p prio -> switch
2660 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2661 PORT_TAG_REGMAP_4567, 0x7654);
2666 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2667 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2668 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2669 mv88e6xxx_6320_family(chip)) {
2670 /* Rate Control: disable ingress rate limiting. */
2671 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2672 PORT_RATE_CONTROL, 0x0001);
2677 /* Port Control 1: disable trunking, disable sending
2678 * learning messages to this port.
2680 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2685 /* Port based VLAN map: give each port the same default address
2686 * database, and allow bidirectional communication between the
2687 * CPU and DSA port(s), and the other ports.
2689 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
2693 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
2697 /* Default VLAN ID and priority: don't set a default VLAN
2698 * ID, and set the default packet priority to zero.
2700 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
2708 static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2712 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2713 (addr[0] << 8) | addr[1]);
2717 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2718 (addr[2] << 8) | addr[3]);
2722 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2723 (addr[4] << 8) | addr[5]);
2726 static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2729 const unsigned int coeff = chip->info->age_time_coeff;
2730 const unsigned int min = 0x01 * coeff;
2731 const unsigned int max = 0xff * coeff;
2736 if (msecs < min || msecs > max)
2739 /* Round to nearest multiple of coeff */
2740 age_time = (msecs + coeff / 2) / coeff;
2742 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2746 /* AgeTime is 11:4 bits */
2748 val |= age_time << 4;
2750 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2753 static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2754 unsigned int ageing_time)
2756 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2759 mutex_lock(&chip->reg_lock);
2760 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2761 mutex_unlock(&chip->reg_lock);
2766 static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
2768 struct dsa_switch *ds = chip->ds;
2769 u32 upstream_port = dsa_upstream_port(ds);
2773 /* Enable the PHY Polling Unit if present, don't discard any packets,
2774 * and mask all interrupt sources.
2777 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2778 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
2779 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2781 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
2785 /* Configure the upstream port, and configure it as the port to which
2786 * ingress and egress and ARP monitor frames are to be sent.
2788 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2789 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2790 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2791 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2796 /* Disable remote management, and set the switch's DSA device number. */
2797 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
2798 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2799 (ds->index & 0x1f));
2803 /* Clear all the VTU and STU entries */
2804 err = _mv88e6xxx_vtu_stu_flush(chip);
2808 /* Set the default address aging time to 5 minutes, and
2809 * enable address learn messages to be sent to all message
2812 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2813 GLOBAL_ATU_CONTROL_LEARN2ALL);
2817 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2821 /* Clear all ATU entries */
2822 err = _mv88e6xxx_atu_flush(chip, 0, true);
2826 /* Configure the IP ToS mapping registers. */
2827 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2830 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2833 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2836 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2839 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2842 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2845 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2848 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2852 /* Configure the IEEE 802.1p priority mapping register. */
2853 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2857 /* Clear the statistics counters for all ports */
2858 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2859 GLOBAL_STATS_OP_FLUSH_ALL);
2863 /* Wait for the flush to complete. */
2864 err = _mv88e6xxx_stats_wait(chip);
2871 static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
2872 int target, int port)
2874 u16 val = (target << 8) | (port & 0xf);
2876 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
2879 static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
2884 /* Initialize the routing port to the 32 possible target devices */
2885 for (target = 0; target < 32; ++target) {
2888 if (target < DSA_MAX_SWITCHES) {
2889 port = chip->ds->rtable[target];
2890 if (port == DSA_RTABLE_NONE)
2894 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
2902 static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
2903 bool hask, u16 mask)
2905 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2906 u16 val = (num << 12) | (mask & port_mask);
2909 val |= GLOBAL2_TRUNK_MASK_HASK;
2911 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
2914 static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
2917 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2918 u16 val = (id << 11) | (map & port_mask);
2920 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
2923 static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
2925 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2928 /* Clear all eight possible Trunk Mask vectors */
2929 for (i = 0; i < 8; ++i) {
2930 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
2935 /* Clear all sixteen possible Trunk ID routing vectors */
2936 for (i = 0; i < 16; ++i) {
2937 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
2945 static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
2949 /* Init all Ingress Rate Limit resources of all ports */
2950 for (port = 0; port < chip->info->num_ports; ++port) {
2951 /* XXX newer chips (like 88E6390) have different 2-bit ops */
2952 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2953 GLOBAL2_IRL_CMD_OP_INIT_ALL |
2958 /* Wait for the operation to complete */
2959 err = mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2960 GLOBAL2_IRL_CMD_BUSY);
2968 /* Indirect write to the Switch MAC/WoL/WoF register */
2969 static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
2970 unsigned int pointer, u8 data)
2972 u16 val = (pointer << 8) | data;
2974 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
2977 static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2981 for (i = 0; i < 6; i++) {
2982 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
2990 static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
2993 u16 val = (pointer << 8) | (data & 0x7);
2995 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
2998 static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
3002 /* Clear all sixteen possible Priority Override entries */
3003 for (i = 0; i < 16; i++) {
3004 err = mv88e6xxx_g2_pot_write(chip, i, 0);
3012 static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
3014 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD,
3015 GLOBAL2_EEPROM_CMD_BUSY |
3016 GLOBAL2_EEPROM_CMD_RUNNING);
3019 static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3023 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, cmd);
3027 return mv88e6xxx_g2_eeprom_wait(chip);
3030 static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
3033 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
3036 err = mv88e6xxx_g2_eeprom_wait(chip);
3040 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3044 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3047 static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
3050 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
3053 err = mv88e6xxx_g2_eeprom_wait(chip);
3057 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3061 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3064 static int mv88e6xxx_g2_smi_phy_wait(struct mv88e6xxx_chip *chip)
3066 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD,
3067 GLOBAL2_SMI_PHY_CMD_BUSY);
3070 static int mv88e6xxx_g2_smi_phy_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3074 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD, cmd);
3078 return mv88e6xxx_g2_smi_phy_wait(chip);
3081 static int mv88e6xxx_g2_smi_phy_read(struct mv88e6xxx_chip *chip, int addr,
3084 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_READ_DATA | (addr << 5) | reg;
3087 err = mv88e6xxx_g2_smi_phy_wait(chip);
3091 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3095 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3098 static int mv88e6xxx_g2_smi_phy_write(struct mv88e6xxx_chip *chip, int addr,
3101 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_WRITE_DATA | (addr << 5) | reg;
3104 err = mv88e6xxx_g2_smi_phy_wait(chip);
3108 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3112 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3115 static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3116 .read = mv88e6xxx_g2_smi_phy_read,
3117 .write = mv88e6xxx_g2_smi_phy_write,
3120 static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3125 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3126 /* Consider the frames with reserved multicast destination
3127 * addresses matching 01:80:c2:00:00:2x as MGMT.
3129 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3135 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3136 /* Consider the frames with reserved multicast destination
3137 * addresses matching 01:80:c2:00:00:0x as MGMT.
3139 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3145 /* Ignore removed tag data on doubly tagged packets, disable
3146 * flow control messages, force flow control priority to the
3147 * highest, and send all special multicast frames to the CPU
3148 * port at the highest priority.
3150 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3151 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3152 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3153 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3154 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
3158 /* Program the DSA routing table. */
3159 err = mv88e6xxx_g2_set_device_mapping(chip);
3163 /* Clear all trunk masks and mapping. */
3164 err = mv88e6xxx_g2_clear_trunk(chip);
3168 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3169 /* Disable ingress rate limiting by resetting all per port
3170 * ingress rate limit resources to their initial state.
3172 err = mv88e6xxx_g2_clear_irl(chip);
3177 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3178 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3179 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3180 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3185 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
3186 /* Clear the priority override table. */
3187 err = mv88e6xxx_g2_clear_pot(chip);
3195 static int mv88e6xxx_setup(struct dsa_switch *ds)
3197 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3202 ds->slave_mii_bus = chip->mdio_bus;
3204 mutex_lock(&chip->reg_lock);
3206 err = mv88e6xxx_switch_reset(chip);
3210 /* Setup Switch Port Registers */
3211 for (i = 0; i < chip->info->num_ports; i++) {
3212 err = mv88e6xxx_setup_port(chip, i);
3217 /* Setup Switch Global 1 Registers */
3218 err = mv88e6xxx_g1_setup(chip);
3222 /* Setup Switch Global 2 Registers */
3223 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3224 err = mv88e6xxx_g2_setup(chip);
3230 mutex_unlock(&chip->reg_lock);
3235 static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3237 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3240 mutex_lock(&chip->reg_lock);
3242 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3243 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3244 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3246 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3248 mutex_unlock(&chip->reg_lock);
3253 static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
3255 struct mv88e6xxx_chip *chip = bus->priv;
3259 if (phy >= chip->info->num_ports)
3262 mutex_lock(&chip->reg_lock);
3263 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
3264 mutex_unlock(&chip->reg_lock);
3266 return err ? err : val;
3269 static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
3271 struct mv88e6xxx_chip *chip = bus->priv;
3274 if (phy >= chip->info->num_ports)
3277 mutex_lock(&chip->reg_lock);
3278 err = mv88e6xxx_phy_write(chip, phy, reg, val);
3279 mutex_unlock(&chip->reg_lock);
3284 static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
3285 struct device_node *np)
3288 struct mii_bus *bus;
3292 chip->mdio_np = of_get_child_by_name(np, "mdio");
3294 bus = devm_mdiobus_alloc(chip->dev);
3298 bus->priv = (void *)chip;
3300 bus->name = np->full_name;
3301 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3303 bus->name = "mv88e6xxx SMI";
3304 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3307 bus->read = mv88e6xxx_mdio_read;
3308 bus->write = mv88e6xxx_mdio_write;
3309 bus->parent = chip->dev;
3312 err = of_mdiobus_register(bus, chip->mdio_np);
3314 err = mdiobus_register(bus);
3316 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
3319 chip->mdio_bus = bus;
3325 of_node_put(chip->mdio_np);
3330 static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
3333 struct mii_bus *bus = chip->mdio_bus;
3335 mdiobus_unregister(bus);
3338 of_node_put(chip->mdio_np);
3341 #ifdef CONFIG_NET_DSA_HWMON
3343 static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3345 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3351 mutex_lock(&chip->reg_lock);
3353 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
3357 /* Enable temperature sensor */
3358 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3362 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
3366 /* Wait for temperature to stabilize */
3367 usleep_range(10000, 12000);
3369 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3373 /* Disable temperature sensor */
3374 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
3378 *temp = ((val & 0x1f) - 5) * 5;
3381 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
3382 mutex_unlock(&chip->reg_lock);
3386 static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3388 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3389 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3395 mutex_lock(&chip->reg_lock);
3396 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3397 mutex_unlock(&chip->reg_lock);
3401 *temp = (val & 0xff) - 25;
3406 static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3408 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3410 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
3413 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
3414 return mv88e63xx_get_temp(ds, temp);
3416 return mv88e61xx_get_temp(ds, temp);
3419 static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3421 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3422 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3426 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3431 mutex_lock(&chip->reg_lock);
3432 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3433 mutex_unlock(&chip->reg_lock);
3437 *temp = (((val >> 8) & 0x1f) * 5) - 25;
3442 static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3444 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3445 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3449 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3452 mutex_lock(&chip->reg_lock);
3453 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3456 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3457 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3458 (val & 0xe0ff) | (temp << 8));
3460 mutex_unlock(&chip->reg_lock);
3465 static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3467 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3468 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3472 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3477 mutex_lock(&chip->reg_lock);
3478 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3479 mutex_unlock(&chip->reg_lock);
3483 *alarm = !!(val & 0x40);
3487 #endif /* CONFIG_NET_DSA_HWMON */
3489 static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3491 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3493 return chip->eeprom_len;
3496 static int mv88e6xxx_get_eeprom16(struct mv88e6xxx_chip *chip,
3497 struct ethtool_eeprom *eeprom, u8 *data)
3499 unsigned int offset = eeprom->offset;
3500 unsigned int len = eeprom->len;
3507 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3511 *data++ = (val >> 8) & 0xff;
3519 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3523 *data++ = val & 0xff;
3524 *data++ = (val >> 8) & 0xff;
3532 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3536 *data++ = val & 0xff;
3546 static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3547 struct ethtool_eeprom *eeprom, u8 *data)
3549 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3552 mutex_lock(&chip->reg_lock);
3554 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3555 err = mv88e6xxx_get_eeprom16(chip, eeprom, data);
3559 mutex_unlock(&chip->reg_lock);
3564 eeprom->magic = 0xc3ec4951;
3569 static int mv88e6xxx_set_eeprom16(struct mv88e6xxx_chip *chip,
3570 struct ethtool_eeprom *eeprom, u8 *data)
3572 unsigned int offset = eeprom->offset;
3573 unsigned int len = eeprom->len;
3577 /* Ensure the RO WriteEn bit is set */
3578 err = mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, &val);
3582 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
3588 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3592 val = (*data++ << 8) | (val & 0xff);
3594 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3605 val |= *data++ << 8;
3607 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3617 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3621 val = (val & 0xff00) | *data++;
3623 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3635 static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3636 struct ethtool_eeprom *eeprom, u8 *data)
3638 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3641 if (eeprom->magic != 0xc3ec4951)
3644 mutex_lock(&chip->reg_lock);
3646 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3647 err = mv88e6xxx_set_eeprom16(chip, eeprom, data);
3651 mutex_unlock(&chip->reg_lock);
3656 static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3658 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3659 .family = MV88E6XXX_FAMILY_6097,
3660 .name = "Marvell 88E6085",
3661 .num_databases = 4096,
3663 .port_base_addr = 0x10,
3664 .age_time_coeff = 15000,
3665 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3669 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3670 .family = MV88E6XXX_FAMILY_6095,
3671 .name = "Marvell 88E6095/88E6095F",
3672 .num_databases = 256,
3674 .port_base_addr = 0x10,
3675 .age_time_coeff = 15000,
3676 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3680 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3681 .family = MV88E6XXX_FAMILY_6165,
3682 .name = "Marvell 88E6123",
3683 .num_databases = 4096,
3685 .port_base_addr = 0x10,
3686 .age_time_coeff = 15000,
3687 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3691 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3692 .family = MV88E6XXX_FAMILY_6185,
3693 .name = "Marvell 88E6131",
3694 .num_databases = 256,
3696 .port_base_addr = 0x10,
3697 .age_time_coeff = 15000,
3698 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3702 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3703 .family = MV88E6XXX_FAMILY_6165,
3704 .name = "Marvell 88E6161",
3705 .num_databases = 4096,
3707 .port_base_addr = 0x10,
3708 .age_time_coeff = 15000,
3709 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3713 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3714 .family = MV88E6XXX_FAMILY_6165,
3715 .name = "Marvell 88E6165",
3716 .num_databases = 4096,
3718 .port_base_addr = 0x10,
3719 .age_time_coeff = 15000,
3720 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3724 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3725 .family = MV88E6XXX_FAMILY_6351,
3726 .name = "Marvell 88E6171",
3727 .num_databases = 4096,
3729 .port_base_addr = 0x10,
3730 .age_time_coeff = 15000,
3731 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3735 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3736 .family = MV88E6XXX_FAMILY_6352,
3737 .name = "Marvell 88E6172",
3738 .num_databases = 4096,
3740 .port_base_addr = 0x10,
3741 .age_time_coeff = 15000,
3742 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3746 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3747 .family = MV88E6XXX_FAMILY_6351,
3748 .name = "Marvell 88E6175",
3749 .num_databases = 4096,
3751 .port_base_addr = 0x10,
3752 .age_time_coeff = 15000,
3753 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3757 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3758 .family = MV88E6XXX_FAMILY_6352,
3759 .name = "Marvell 88E6176",
3760 .num_databases = 4096,
3762 .port_base_addr = 0x10,
3763 .age_time_coeff = 15000,
3764 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3768 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3769 .family = MV88E6XXX_FAMILY_6185,
3770 .name = "Marvell 88E6185",
3771 .num_databases = 256,
3773 .port_base_addr = 0x10,
3774 .age_time_coeff = 15000,
3775 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3779 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3780 .family = MV88E6XXX_FAMILY_6352,
3781 .name = "Marvell 88E6240",
3782 .num_databases = 4096,
3784 .port_base_addr = 0x10,
3785 .age_time_coeff = 15000,
3786 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3790 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3791 .family = MV88E6XXX_FAMILY_6320,
3792 .name = "Marvell 88E6320",
3793 .num_databases = 4096,
3795 .port_base_addr = 0x10,
3796 .age_time_coeff = 15000,
3797 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3801 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3802 .family = MV88E6XXX_FAMILY_6320,
3803 .name = "Marvell 88E6321",
3804 .num_databases = 4096,
3806 .port_base_addr = 0x10,
3807 .age_time_coeff = 15000,
3808 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3812 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3813 .family = MV88E6XXX_FAMILY_6351,
3814 .name = "Marvell 88E6350",
3815 .num_databases = 4096,
3817 .port_base_addr = 0x10,
3818 .age_time_coeff = 15000,
3819 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3823 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3824 .family = MV88E6XXX_FAMILY_6351,
3825 .name = "Marvell 88E6351",
3826 .num_databases = 4096,
3828 .port_base_addr = 0x10,
3829 .age_time_coeff = 15000,
3830 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3834 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3835 .family = MV88E6XXX_FAMILY_6352,
3836 .name = "Marvell 88E6352",
3837 .num_databases = 4096,
3839 .port_base_addr = 0x10,
3840 .age_time_coeff = 15000,
3841 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3845 static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
3849 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3850 if (mv88e6xxx_table[i].prod_num == prod_num)
3851 return &mv88e6xxx_table[i];
3856 static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
3858 const struct mv88e6xxx_info *info;
3859 unsigned int prod_num, rev;
3863 mutex_lock(&chip->reg_lock);
3864 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3865 mutex_unlock(&chip->reg_lock);
3869 prod_num = (id & 0xfff0) >> 4;
3872 info = mv88e6xxx_lookup_info(prod_num);
3876 /* Update the compatible info with the probed one */
3879 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3880 chip->info->prod_num, chip->info->name, rev);
3885 static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
3887 struct mv88e6xxx_chip *chip;
3889 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3895 mutex_init(&chip->reg_lock);
3900 static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3901 .read = mv88e6xxx_read,
3902 .write = mv88e6xxx_write,
3905 static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3907 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3908 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3909 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3910 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3911 mv88e6xxx_ppu_state_init(chip);
3913 chip->phy_ops = &mv88e6xxx_phy_ops;
3917 static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
3918 struct mii_bus *bus, int sw_addr)
3920 /* ADDR[0] pin is unavailable externally and considered zero */
3925 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3926 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
3927 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3932 chip->sw_addr = sw_addr;
3937 static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3938 struct device *host_dev, int sw_addr,
3941 struct mv88e6xxx_chip *chip;
3942 struct mii_bus *bus;
3945 bus = dsa_host_dev_to_mii_bus(host_dev);
3949 chip = mv88e6xxx_alloc_chip(dsa_dev);
3953 /* Legacy SMI probing will only support chips similar to 88E6085 */
3954 chip->info = &mv88e6xxx_table[MV88E6085];
3956 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
3960 err = mv88e6xxx_detect(chip);
3964 mv88e6xxx_phy_init(chip);
3966 err = mv88e6xxx_mdio_register(chip, NULL);
3972 return chip->info->name;
3974 devm_kfree(dsa_dev, chip);
3979 static struct dsa_switch_driver mv88e6xxx_switch_driver = {
3980 .tag_protocol = DSA_TAG_PROTO_EDSA,
3981 .probe = mv88e6xxx_drv_probe,
3982 .setup = mv88e6xxx_setup,
3983 .set_addr = mv88e6xxx_set_addr,
3984 .adjust_link = mv88e6xxx_adjust_link,
3985 .get_strings = mv88e6xxx_get_strings,
3986 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3987 .get_sset_count = mv88e6xxx_get_sset_count,
3988 .set_eee = mv88e6xxx_set_eee,
3989 .get_eee = mv88e6xxx_get_eee,
3990 #ifdef CONFIG_NET_DSA_HWMON
3991 .get_temp = mv88e6xxx_get_temp,
3992 .get_temp_limit = mv88e6xxx_get_temp_limit,
3993 .set_temp_limit = mv88e6xxx_set_temp_limit,
3994 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3996 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
3997 .get_eeprom = mv88e6xxx_get_eeprom,
3998 .set_eeprom = mv88e6xxx_set_eeprom,
3999 .get_regs_len = mv88e6xxx_get_regs_len,
4000 .get_regs = mv88e6xxx_get_regs,
4001 .set_ageing_time = mv88e6xxx_set_ageing_time,
4002 .port_bridge_join = mv88e6xxx_port_bridge_join,
4003 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
4004 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
4005 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4006 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4007 .port_vlan_add = mv88e6xxx_port_vlan_add,
4008 .port_vlan_del = mv88e6xxx_port_vlan_del,
4009 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4010 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4011 .port_fdb_add = mv88e6xxx_port_fdb_add,
4012 .port_fdb_del = mv88e6xxx_port_fdb_del,
4013 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
4016 static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
4017 struct device_node *np)
4019 struct device *dev = chip->dev;
4020 struct dsa_switch *ds;
4022 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4028 ds->drv = &mv88e6xxx_switch_driver;
4030 dev_set_drvdata(dev, ds);
4032 return dsa_register_switch(ds, np);
4035 static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
4037 dsa_unregister_switch(chip->ds);
4040 static int mv88e6xxx_probe(struct mdio_device *mdiodev)
4042 struct device *dev = &mdiodev->dev;
4043 struct device_node *np = dev->of_node;
4044 const struct mv88e6xxx_info *compat_info;
4045 struct mv88e6xxx_chip *chip;
4049 compat_info = of_device_get_match_data(dev);
4053 chip = mv88e6xxx_alloc_chip(dev);
4057 chip->info = compat_info;
4059 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
4063 err = mv88e6xxx_detect(chip);
4067 mv88e6xxx_phy_init(chip);
4069 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4070 if (IS_ERR(chip->reset))
4071 return PTR_ERR(chip->reset);
4073 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
4074 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
4075 chip->eeprom_len = eeprom_len;
4077 err = mv88e6xxx_mdio_register(chip, np);
4081 err = mv88e6xxx_register_switch(chip, np);
4083 mv88e6xxx_mdio_unregister(chip);
4090 static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4092 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
4093 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
4095 mv88e6xxx_unregister_switch(chip);
4096 mv88e6xxx_mdio_unregister(chip);
4099 static const struct of_device_id mv88e6xxx_of_match[] = {
4101 .compatible = "marvell,mv88e6085",
4102 .data = &mv88e6xxx_table[MV88E6085],
4107 MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4109 static struct mdio_driver mv88e6xxx_driver = {
4110 .probe = mv88e6xxx_probe,
4111 .remove = mv88e6xxx_remove,
4113 .name = "mv88e6085",
4114 .of_match_table = mv88e6xxx_of_match,
4118 static int __init mv88e6xxx_init(void)
4120 register_switch_driver(&mv88e6xxx_switch_driver);
4121 return mdio_driver_register(&mv88e6xxx_driver);
4123 module_init(mv88e6xxx_init);
4125 static void __exit mv88e6xxx_cleanup(void)
4127 mdio_driver_unregister(&mv88e6xxx_driver);
4128 unregister_switch_driver(&mv88e6xxx_switch_driver);
4130 module_exit(mv88e6xxx_cleanup);
4132 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4133 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4134 MODULE_LICENSE("GPL");