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,
314 for (i = 0; i < 16; i++) {
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)
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 for (i = 0; i < 16; i++) {
390 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
394 usleep_range(1000, 2000);
395 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
396 GLOBAL_STATUS_PPU_POLLING)
403 static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
407 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
411 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
412 ret | GLOBAL_CONTROL_PPU_ENABLE);
416 for (i = 0; i < 16; i++) {
417 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
421 usleep_range(1000, 2000);
422 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
423 GLOBAL_STATUS_PPU_POLLING)
430 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
432 struct mv88e6xxx_chip *chip;
434 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
436 mutex_lock(&chip->reg_lock);
438 if (mutex_trylock(&chip->ppu_mutex)) {
439 if (mv88e6xxx_ppu_enable(chip) == 0)
440 chip->ppu_disabled = 0;
441 mutex_unlock(&chip->ppu_mutex);
444 mutex_unlock(&chip->reg_lock);
447 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
449 struct mv88e6xxx_chip *chip = (void *)_ps;
451 schedule_work(&chip->ppu_work);
454 static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
458 mutex_lock(&chip->ppu_mutex);
460 /* If the PHY polling unit is enabled, disable it so that
461 * we can access the PHY registers. If it was already
462 * disabled, cancel the timer that is going to re-enable
465 if (!chip->ppu_disabled) {
466 ret = mv88e6xxx_ppu_disable(chip);
468 mutex_unlock(&chip->ppu_mutex);
471 chip->ppu_disabled = 1;
473 del_timer(&chip->ppu_timer);
480 static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
482 /* Schedule a timer to re-enable the PHY polling unit. */
483 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
484 mutex_unlock(&chip->ppu_mutex);
487 static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
489 mutex_init(&chip->ppu_mutex);
490 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
491 init_timer(&chip->ppu_timer);
492 chip->ppu_timer.data = (unsigned long)chip;
493 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
496 static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
501 err = mv88e6xxx_ppu_access_get(chip);
503 err = mv88e6xxx_read(chip, addr, reg, val);
504 mv88e6xxx_ppu_access_put(chip);
510 static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
515 err = mv88e6xxx_ppu_access_get(chip);
517 err = mv88e6xxx_write(chip, addr, reg, val);
518 mv88e6xxx_ppu_access_put(chip);
524 static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
525 .read = mv88e6xxx_phy_ppu_read,
526 .write = mv88e6xxx_phy_ppu_write,
529 static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
531 return chip->info->family == MV88E6XXX_FAMILY_6065;
534 static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
536 return chip->info->family == MV88E6XXX_FAMILY_6095;
539 static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
541 return chip->info->family == MV88E6XXX_FAMILY_6097;
544 static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
546 return chip->info->family == MV88E6XXX_FAMILY_6165;
549 static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
551 return chip->info->family == MV88E6XXX_FAMILY_6185;
554 static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
556 return chip->info->family == MV88E6XXX_FAMILY_6320;
559 static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
561 return chip->info->family == MV88E6XXX_FAMILY_6351;
564 static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
566 return chip->info->family == MV88E6XXX_FAMILY_6352;
569 static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
571 return chip->info->num_databases;
574 static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
576 /* Does the device have dedicated FID registers for ATU and VTU ops? */
577 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
578 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
584 /* We expect the switch to perform auto negotiation if there is a real
585 * phy. However, in the case of a fixed link phy, we force the port
586 * settings from the fixed link settings.
588 static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
589 struct phy_device *phydev)
591 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
595 if (!phy_is_pseudo_fixed_link(phydev))
598 mutex_lock(&chip->reg_lock);
600 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
604 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
605 PORT_PCS_CTRL_FORCE_LINK |
606 PORT_PCS_CTRL_DUPLEX_FULL |
607 PORT_PCS_CTRL_FORCE_DUPLEX |
608 PORT_PCS_CTRL_UNFORCED);
610 reg |= PORT_PCS_CTRL_FORCE_LINK;
612 reg |= PORT_PCS_CTRL_LINK_UP;
614 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
617 switch (phydev->speed) {
619 reg |= PORT_PCS_CTRL_1000;
622 reg |= PORT_PCS_CTRL_100;
625 reg |= PORT_PCS_CTRL_10;
628 pr_info("Unknown speed");
632 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
633 if (phydev->duplex == DUPLEX_FULL)
634 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
636 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
637 (port >= chip->info->num_ports - 2)) {
638 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
639 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
640 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
641 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
642 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
643 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
644 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
646 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
649 mutex_unlock(&chip->reg_lock);
652 static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
657 for (i = 0; i < 10; i++) {
658 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
659 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
666 static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
670 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
671 port = (port + 1) << 5;
673 /* Snapshot the hardware statistics counters for this port. */
674 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
675 GLOBAL_STATS_OP_CAPTURE_PORT |
676 GLOBAL_STATS_OP_HIST_RX_TX | port);
680 /* Wait for the snapshotting to complete. */
681 ret = _mv88e6xxx_stats_wait(chip);
688 static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
696 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
697 GLOBAL_STATS_OP_READ_CAPTURED |
698 GLOBAL_STATS_OP_HIST_RX_TX | stat);
702 ret = _mv88e6xxx_stats_wait(chip);
706 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
712 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
719 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
720 { "in_good_octets", 8, 0x00, BANK0, },
721 { "in_bad_octets", 4, 0x02, BANK0, },
722 { "in_unicast", 4, 0x04, BANK0, },
723 { "in_broadcasts", 4, 0x06, BANK0, },
724 { "in_multicasts", 4, 0x07, BANK0, },
725 { "in_pause", 4, 0x16, BANK0, },
726 { "in_undersize", 4, 0x18, BANK0, },
727 { "in_fragments", 4, 0x19, BANK0, },
728 { "in_oversize", 4, 0x1a, BANK0, },
729 { "in_jabber", 4, 0x1b, BANK0, },
730 { "in_rx_error", 4, 0x1c, BANK0, },
731 { "in_fcs_error", 4, 0x1d, BANK0, },
732 { "out_octets", 8, 0x0e, BANK0, },
733 { "out_unicast", 4, 0x10, BANK0, },
734 { "out_broadcasts", 4, 0x13, BANK0, },
735 { "out_multicasts", 4, 0x12, BANK0, },
736 { "out_pause", 4, 0x15, BANK0, },
737 { "excessive", 4, 0x11, BANK0, },
738 { "collisions", 4, 0x1e, BANK0, },
739 { "deferred", 4, 0x05, BANK0, },
740 { "single", 4, 0x14, BANK0, },
741 { "multiple", 4, 0x17, BANK0, },
742 { "out_fcs_error", 4, 0x03, BANK0, },
743 { "late", 4, 0x1f, BANK0, },
744 { "hist_64bytes", 4, 0x08, BANK0, },
745 { "hist_65_127bytes", 4, 0x09, BANK0, },
746 { "hist_128_255bytes", 4, 0x0a, BANK0, },
747 { "hist_256_511bytes", 4, 0x0b, BANK0, },
748 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
749 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
750 { "sw_in_discards", 4, 0x10, PORT, },
751 { "sw_in_filtered", 2, 0x12, PORT, },
752 { "sw_out_filtered", 2, 0x13, PORT, },
753 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
770 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
771 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
772 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
773 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
774 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
775 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
776 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
777 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
778 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
781 static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
782 struct mv88e6xxx_hw_stat *stat)
784 switch (stat->type) {
788 return mv88e6xxx_6320_family(chip);
790 return mv88e6xxx_6095_family(chip) ||
791 mv88e6xxx_6185_family(chip) ||
792 mv88e6xxx_6097_family(chip) ||
793 mv88e6xxx_6165_family(chip) ||
794 mv88e6xxx_6351_family(chip) ||
795 mv88e6xxx_6352_family(chip);
800 static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
801 struct mv88e6xxx_hw_stat *s,
811 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
816 if (s->sizeof_stat == 4) {
817 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
826 _mv88e6xxx_stats_read(chip, s->reg, &low);
827 if (s->sizeof_stat == 8)
828 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
830 value = (((u64)high) << 16) | low;
834 static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
837 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
838 struct mv88e6xxx_hw_stat *stat;
841 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
842 stat = &mv88e6xxx_hw_stats[i];
843 if (mv88e6xxx_has_stat(chip, stat)) {
844 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
851 static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
853 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
854 struct mv88e6xxx_hw_stat *stat;
857 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
858 stat = &mv88e6xxx_hw_stats[i];
859 if (mv88e6xxx_has_stat(chip, stat))
865 static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
868 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
869 struct mv88e6xxx_hw_stat *stat;
873 mutex_lock(&chip->reg_lock);
875 ret = _mv88e6xxx_stats_snapshot(chip, port);
877 mutex_unlock(&chip->reg_lock);
880 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
881 stat = &mv88e6xxx_hw_stats[i];
882 if (mv88e6xxx_has_stat(chip, stat)) {
883 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
888 mutex_unlock(&chip->reg_lock);
891 static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
893 return 32 * sizeof(u16);
896 static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
897 struct ethtool_regs *regs, void *_p)
899 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
905 memset(p, 0xff, 32 * sizeof(u16));
907 mutex_lock(&chip->reg_lock);
909 for (i = 0; i < 32; i++) {
912 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
917 mutex_unlock(&chip->reg_lock);
920 static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
922 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
926 static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
927 struct ethtool_eee *e)
929 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
933 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
936 mutex_lock(&chip->reg_lock);
938 err = mv88e6xxx_phy_read(chip, port, 16, ®);
942 e->eee_enabled = !!(reg & 0x0200);
943 e->tx_lpi_enabled = !!(reg & 0x0100);
945 err = mv88e6xxx_read(chip, REG_PORT(port), PORT_STATUS, ®);
949 e->eee_active = !!(reg & PORT_STATUS_EEE);
951 mutex_unlock(&chip->reg_lock);
956 static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
957 struct phy_device *phydev, struct ethtool_eee *e)
959 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
963 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
966 mutex_lock(&chip->reg_lock);
968 err = mv88e6xxx_phy_read(chip, port, 16, ®);
975 if (e->tx_lpi_enabled)
978 err = mv88e6xxx_phy_write(chip, port, 16, reg);
980 mutex_unlock(&chip->reg_lock);
985 static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
989 if (mv88e6xxx_has_fid_reg(chip)) {
990 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
994 } else if (mv88e6xxx_num_databases(chip) == 256) {
995 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
996 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
1000 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1002 ((fid << 8) & 0xf000));
1006 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1010 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1014 return _mv88e6xxx_atu_wait(chip);
1017 static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
1018 struct mv88e6xxx_atu_entry *entry)
1020 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1022 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1023 unsigned int mask, shift;
1026 data |= GLOBAL_ATU_DATA_TRUNK;
1027 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1028 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1030 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1031 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1034 data |= (entry->portv_trunkid << shift) & mask;
1037 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1040 static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
1041 struct mv88e6xxx_atu_entry *entry,
1047 err = _mv88e6xxx_atu_wait(chip);
1051 err = _mv88e6xxx_atu_data_write(chip, entry);
1056 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1057 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1059 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1060 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1063 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
1066 static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
1067 u16 fid, bool static_too)
1069 struct mv88e6xxx_atu_entry entry = {
1071 .state = 0, /* EntryState bits must be 0 */
1074 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1077 static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
1078 int from_port, int to_port, bool static_too)
1080 struct mv88e6xxx_atu_entry entry = {
1085 /* EntryState bits must be 0xF */
1086 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1088 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1089 entry.portv_trunkid = (to_port & 0x0f) << 4;
1090 entry.portv_trunkid |= from_port & 0x0f;
1092 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1095 static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
1096 int port, bool static_too)
1098 /* Destination port 0xF means remove the entries */
1099 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
1102 static const char * const mv88e6xxx_port_state_names[] = {
1103 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1104 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1105 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1106 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1109 static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
1112 struct dsa_switch *ds = chip->ds;
1116 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
1120 oldstate = reg & PORT_CONTROL_STATE_MASK;
1122 if (oldstate != state) {
1123 /* Flush forwarding database if we're moving a port
1124 * from Learning or Forwarding state to Disabled or
1125 * Blocking or Listening state.
1127 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1128 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1129 (state == PORT_CONTROL_STATE_DISABLED ||
1130 state == PORT_CONTROL_STATE_BLOCKING)) {
1131 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
1136 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1137 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
1142 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1143 mv88e6xxx_port_state_names[state],
1144 mv88e6xxx_port_state_names[oldstate]);
1150 static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
1152 struct net_device *bridge = chip->ports[port].bridge_dev;
1153 const u16 mask = (1 << chip->info->num_ports) - 1;
1154 struct dsa_switch *ds = chip->ds;
1155 u16 output_ports = 0;
1159 /* allow CPU port or DSA link(s) to send frames to every port */
1160 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1161 output_ports = mask;
1163 for (i = 0; i < chip->info->num_ports; ++i) {
1164 /* allow sending frames to every group member */
1165 if (bridge && chip->ports[i].bridge_dev == bridge)
1166 output_ports |= BIT(i);
1168 /* allow sending frames to CPU port and DSA link(s) */
1169 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1170 output_ports |= BIT(i);
1174 /* prevent frames from going back out of the port they came in on */
1175 output_ports &= ~BIT(port);
1177 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1182 reg |= output_ports & mask;
1184 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
1187 static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1190 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1195 case BR_STATE_DISABLED:
1196 stp_state = PORT_CONTROL_STATE_DISABLED;
1198 case BR_STATE_BLOCKING:
1199 case BR_STATE_LISTENING:
1200 stp_state = PORT_CONTROL_STATE_BLOCKING;
1202 case BR_STATE_LEARNING:
1203 stp_state = PORT_CONTROL_STATE_LEARNING;
1205 case BR_STATE_FORWARDING:
1207 stp_state = PORT_CONTROL_STATE_FORWARDING;
1211 mutex_lock(&chip->reg_lock);
1212 err = _mv88e6xxx_port_state(chip, port, stp_state);
1213 mutex_unlock(&chip->reg_lock);
1216 netdev_err(ds->ports[port].netdev,
1217 "failed to update state to %s\n",
1218 mv88e6xxx_port_state_names[stp_state]);
1221 static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
1224 struct dsa_switch *ds = chip->ds;
1228 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
1232 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1235 ret &= ~PORT_DEFAULT_VLAN_MASK;
1236 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1238 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
1239 PORT_DEFAULT_VLAN, ret);
1243 netdev_dbg(ds->ports[port].netdev,
1244 "DefaultVID %d (was %d)\n", *new, pvid);
1253 static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
1254 int port, u16 *pvid)
1256 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
1259 static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
1262 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
1265 static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
1267 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1268 GLOBAL_VTU_OP_BUSY);
1271 static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
1275 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
1279 return _mv88e6xxx_vtu_wait(chip);
1282 static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
1286 ret = _mv88e6xxx_vtu_wait(chip);
1290 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
1293 static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
1294 struct mv88e6xxx_vtu_stu_entry *entry,
1295 unsigned int nibble_offset)
1301 for (i = 0; i < 3; ++i) {
1302 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1303 GLOBAL_VTU_DATA_0_3 + i);
1310 for (i = 0; i < chip->info->num_ports; ++i) {
1311 unsigned int shift = (i % 4) * 4 + nibble_offset;
1312 u16 reg = regs[i / 4];
1314 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1320 static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
1321 struct mv88e6xxx_vtu_stu_entry *entry)
1323 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
1326 static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
1327 struct mv88e6xxx_vtu_stu_entry *entry)
1329 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
1332 static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
1333 struct mv88e6xxx_vtu_stu_entry *entry,
1334 unsigned int nibble_offset)
1336 u16 regs[3] = { 0 };
1340 for (i = 0; i < chip->info->num_ports; ++i) {
1341 unsigned int shift = (i % 4) * 4 + nibble_offset;
1342 u8 data = entry->data[i];
1344 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1347 for (i = 0; i < 3; ++i) {
1348 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
1349 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1357 static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
1358 struct mv88e6xxx_vtu_stu_entry *entry)
1360 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
1363 static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
1364 struct mv88e6xxx_vtu_stu_entry *entry)
1366 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
1369 static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
1371 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
1372 vid & GLOBAL_VTU_VID_MASK);
1375 static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
1376 struct mv88e6xxx_vtu_stu_entry *entry)
1378 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1381 ret = _mv88e6xxx_vtu_wait(chip);
1385 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1389 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1393 next.vid = ret & GLOBAL_VTU_VID_MASK;
1394 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1397 ret = mv88e6xxx_vtu_data_read(chip, &next);
1401 if (mv88e6xxx_has_fid_reg(chip)) {
1402 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1407 next.fid = ret & GLOBAL_VTU_FID_MASK;
1408 } else if (mv88e6xxx_num_databases(chip) == 256) {
1409 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1410 * VTU DBNum[3:0] are located in VTU Operation 3:0
1412 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1417 next.fid = (ret & 0xf00) >> 4;
1418 next.fid |= ret & 0xf;
1421 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1422 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1427 next.sid = ret & GLOBAL_VTU_SID_MASK;
1435 static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1436 struct switchdev_obj_port_vlan *vlan,
1437 int (*cb)(struct switchdev_obj *obj))
1439 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1440 struct mv88e6xxx_vtu_stu_entry next;
1444 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1447 mutex_lock(&chip->reg_lock);
1449 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
1453 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1458 err = _mv88e6xxx_vtu_getnext(chip, &next);
1465 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1468 /* reinit and dump this VLAN obj */
1469 vlan->vid_begin = next.vid;
1470 vlan->vid_end = next.vid;
1473 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1474 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1476 if (next.vid == pvid)
1477 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1479 err = cb(&vlan->obj);
1482 } while (next.vid < GLOBAL_VTU_VID_MASK);
1485 mutex_unlock(&chip->reg_lock);
1490 static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
1491 struct mv88e6xxx_vtu_stu_entry *entry)
1493 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1497 ret = _mv88e6xxx_vtu_wait(chip);
1504 /* Write port member tags */
1505 ret = mv88e6xxx_vtu_data_write(chip, entry);
1509 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1510 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1511 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1517 if (mv88e6xxx_has_fid_reg(chip)) {
1518 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1519 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1523 } else if (mv88e6xxx_num_databases(chip) == 256) {
1524 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1525 * VTU DBNum[3:0] are located in VTU Operation 3:0
1527 op |= (entry->fid & 0xf0) << 8;
1528 op |= entry->fid & 0xf;
1531 reg = GLOBAL_VTU_VID_VALID;
1533 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1534 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1538 return _mv88e6xxx_vtu_cmd(chip, op);
1541 static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
1542 struct mv88e6xxx_vtu_stu_entry *entry)
1544 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1547 ret = _mv88e6xxx_vtu_wait(chip);
1551 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1552 sid & GLOBAL_VTU_SID_MASK);
1556 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1560 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
1564 next.sid = ret & GLOBAL_VTU_SID_MASK;
1566 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1570 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1573 ret = mv88e6xxx_stu_data_read(chip, &next);
1582 static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
1583 struct mv88e6xxx_vtu_stu_entry *entry)
1588 ret = _mv88e6xxx_vtu_wait(chip);
1595 /* Write port states */
1596 ret = mv88e6xxx_stu_data_write(chip, entry);
1600 reg = GLOBAL_VTU_VID_VALID;
1602 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1606 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1607 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1611 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1614 static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
1617 struct dsa_switch *ds = chip->ds;
1622 if (mv88e6xxx_num_databases(chip) == 4096)
1624 else if (mv88e6xxx_num_databases(chip) == 256)
1629 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1630 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1634 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1637 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1638 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1640 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
1646 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1647 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
1651 fid |= (ret & upper_mask) << 4;
1655 ret |= (*new >> 4) & upper_mask;
1657 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
1662 netdev_dbg(ds->ports[port].netdev,
1663 "FID %d (was %d)\n", *new, fid);
1672 static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
1675 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
1678 static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
1681 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
1684 static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
1686 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1687 struct mv88e6xxx_vtu_stu_entry vlan;
1690 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1692 /* Set every FID bit used by the (un)bridged ports */
1693 for (i = 0; i < chip->info->num_ports; ++i) {
1694 err = _mv88e6xxx_port_fid_get(chip, i, fid);
1698 set_bit(*fid, fid_bitmap);
1701 /* Set every FID bit used by the VLAN entries */
1702 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1707 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1714 set_bit(vlan.fid, fid_bitmap);
1715 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1717 /* The reset value 0x000 is used to indicate that multiple address
1718 * databases are not needed. Return the next positive available.
1720 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1721 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
1724 /* Clear the database */
1725 return _mv88e6xxx_atu_flush(chip, *fid, true);
1728 static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
1729 struct mv88e6xxx_vtu_stu_entry *entry)
1731 struct dsa_switch *ds = chip->ds;
1732 struct mv88e6xxx_vtu_stu_entry vlan = {
1738 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
1742 /* exclude all ports except the CPU and DSA ports */
1743 for (i = 0; i < chip->info->num_ports; ++i)
1744 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1745 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1746 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1748 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1749 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
1750 struct mv88e6xxx_vtu_stu_entry vstp;
1752 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1753 * implemented, only one STU entry is needed to cover all VTU
1754 * entries. Thus, validate the SID 0.
1757 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
1761 if (vstp.sid != vlan.sid || !vstp.valid) {
1762 memset(&vstp, 0, sizeof(vstp));
1764 vstp.sid = vlan.sid;
1766 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
1776 static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
1777 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1784 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
1788 err = _mv88e6xxx_vtu_getnext(chip, entry);
1792 if (entry->vid != vid || !entry->valid) {
1795 /* -ENOENT would've been more appropriate, but switchdev expects
1796 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1799 err = _mv88e6xxx_vtu_new(chip, vid, entry);
1805 static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1806 u16 vid_begin, u16 vid_end)
1808 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1809 struct mv88e6xxx_vtu_stu_entry vlan;
1815 mutex_lock(&chip->reg_lock);
1817 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
1822 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1829 if (vlan.vid > vid_end)
1832 for (i = 0; i < chip->info->num_ports; ++i) {
1833 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1837 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1840 if (chip->ports[i].bridge_dev ==
1841 chip->ports[port].bridge_dev)
1842 break; /* same bridge, check next VLAN */
1844 netdev_warn(ds->ports[port].netdev,
1845 "hardware VLAN %d already used by %s\n",
1847 netdev_name(chip->ports[i].bridge_dev));
1851 } while (vlan.vid < vid_end);
1854 mutex_unlock(&chip->reg_lock);
1859 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1860 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1861 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1862 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1863 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1866 static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1867 bool vlan_filtering)
1869 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1870 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1871 PORT_CONTROL_2_8021Q_DISABLED;
1874 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1877 mutex_lock(&chip->reg_lock);
1879 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
1883 old = ret & PORT_CONTROL_2_8021Q_MASK;
1886 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1887 ret |= new & PORT_CONTROL_2_8021Q_MASK;
1889 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
1894 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
1895 mv88e6xxx_port_8021q_mode_names[new],
1896 mv88e6xxx_port_8021q_mode_names[old]);
1901 mutex_unlock(&chip->reg_lock);
1907 mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1908 const struct switchdev_obj_port_vlan *vlan,
1909 struct switchdev_trans *trans)
1911 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1914 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1917 /* If the requested port doesn't belong to the same bridge as the VLAN
1918 * members, do not support it (yet) and fallback to software VLAN.
1920 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1925 /* We don't need any dynamic resource from the kernel (yet),
1926 * so skip the prepare phase.
1931 static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
1932 u16 vid, bool untagged)
1934 struct mv88e6xxx_vtu_stu_entry vlan;
1937 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
1941 vlan.data[port] = untagged ?
1942 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1943 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1945 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
1948 static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1949 const struct switchdev_obj_port_vlan *vlan,
1950 struct switchdev_trans *trans)
1952 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1953 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1954 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1957 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1960 mutex_lock(&chip->reg_lock);
1962 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
1963 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
1964 netdev_err(ds->ports[port].netdev,
1965 "failed to add VLAN %d%c\n",
1966 vid, untagged ? 'u' : 't');
1968 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
1969 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
1972 mutex_unlock(&chip->reg_lock);
1975 static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
1978 struct dsa_switch *ds = chip->ds;
1979 struct mv88e6xxx_vtu_stu_entry vlan;
1982 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
1986 /* Tell switchdev if this VLAN is handled in software */
1987 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1990 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1992 /* keep the VLAN unless all ports are excluded */
1994 for (i = 0; i < chip->info->num_ports; ++i) {
1995 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1998 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
2004 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2008 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
2011 static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2012 const struct switchdev_obj_port_vlan *vlan)
2014 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2018 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2021 mutex_lock(&chip->reg_lock);
2023 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
2027 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2028 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
2033 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
2040 mutex_unlock(&chip->reg_lock);
2045 static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
2046 const unsigned char *addr)
2050 for (i = 0; i < 3; i++) {
2051 ret = _mv88e6xxx_reg_write(
2052 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2053 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2061 static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
2062 unsigned char *addr)
2066 for (i = 0; i < 3; i++) {
2067 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
2068 GLOBAL_ATU_MAC_01 + i);
2071 addr[i * 2] = ret >> 8;
2072 addr[i * 2 + 1] = ret & 0xff;
2078 static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
2079 struct mv88e6xxx_atu_entry *entry)
2083 ret = _mv88e6xxx_atu_wait(chip);
2087 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
2091 ret = _mv88e6xxx_atu_data_write(chip, entry);
2095 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2098 static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
2099 const unsigned char *addr, u16 vid,
2102 struct mv88e6xxx_atu_entry entry = { 0 };
2103 struct mv88e6xxx_vtu_stu_entry vlan;
2106 /* Null VLAN ID corresponds to the port private database */
2108 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
2110 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2114 entry.fid = vlan.fid;
2115 entry.state = state;
2116 ether_addr_copy(entry.mac, addr);
2117 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2118 entry.trunk = false;
2119 entry.portv_trunkid = BIT(port);
2122 return _mv88e6xxx_atu_load(chip, &entry);
2125 static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2126 const struct switchdev_obj_port_fdb *fdb,
2127 struct switchdev_trans *trans)
2129 /* We don't need any dynamic resource from the kernel (yet),
2130 * so skip the prepare phase.
2135 static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2136 const struct switchdev_obj_port_fdb *fdb,
2137 struct switchdev_trans *trans)
2139 int state = is_multicast_ether_addr(fdb->addr) ?
2140 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2141 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2142 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2144 mutex_lock(&chip->reg_lock);
2145 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
2146 netdev_err(ds->ports[port].netdev,
2147 "failed to load MAC address\n");
2148 mutex_unlock(&chip->reg_lock);
2151 static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2152 const struct switchdev_obj_port_fdb *fdb)
2154 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2157 mutex_lock(&chip->reg_lock);
2158 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
2159 GLOBAL_ATU_DATA_STATE_UNUSED);
2160 mutex_unlock(&chip->reg_lock);
2165 static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2166 struct mv88e6xxx_atu_entry *entry)
2168 struct mv88e6xxx_atu_entry next = { 0 };
2173 ret = _mv88e6xxx_atu_wait(chip);
2177 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2181 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
2185 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
2189 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2190 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2191 unsigned int mask, shift;
2193 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2195 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2196 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2199 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2200 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2203 next.portv_trunkid = (ret & mask) >> shift;
2210 static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
2211 u16 fid, u16 vid, int port,
2212 struct switchdev_obj_port_fdb *fdb,
2213 int (*cb)(struct switchdev_obj *obj))
2215 struct mv88e6xxx_atu_entry addr = {
2216 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2220 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
2225 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
2229 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2232 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2233 bool is_static = addr.state ==
2234 (is_multicast_ether_addr(addr.mac) ?
2235 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2236 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2239 ether_addr_copy(fdb->addr, addr.mac);
2240 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2242 err = cb(&fdb->obj);
2246 } while (!is_broadcast_ether_addr(addr.mac));
2251 static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2252 struct switchdev_obj_port_fdb *fdb,
2253 int (*cb)(struct switchdev_obj *obj))
2255 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2256 struct mv88e6xxx_vtu_stu_entry vlan = {
2257 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2262 mutex_lock(&chip->reg_lock);
2264 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2265 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2269 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
2273 /* Dump VLANs' Filtering Information Databases */
2274 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2279 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2286 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2290 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2293 mutex_unlock(&chip->reg_lock);
2298 static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2299 struct net_device *bridge)
2301 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2304 mutex_lock(&chip->reg_lock);
2306 /* Assign the bridge and remap each port's VLANTable */
2307 chip->ports[port].bridge_dev = bridge;
2309 for (i = 0; i < chip->info->num_ports; ++i) {
2310 if (chip->ports[i].bridge_dev == bridge) {
2311 err = _mv88e6xxx_port_based_vlan_map(chip, i);
2317 mutex_unlock(&chip->reg_lock);
2322 static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2324 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2325 struct net_device *bridge = chip->ports[port].bridge_dev;
2328 mutex_lock(&chip->reg_lock);
2330 /* Unassign the bridge and remap each port's VLANTable */
2331 chip->ports[port].bridge_dev = NULL;
2333 for (i = 0; i < chip->info->num_ports; ++i)
2334 if (i == port || chip->ports[i].bridge_dev == bridge)
2335 if (_mv88e6xxx_port_based_vlan_map(chip, i))
2336 netdev_warn(ds->ports[i].netdev,
2337 "failed to remap\n");
2339 mutex_unlock(&chip->reg_lock);
2342 static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
2344 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
2345 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2346 struct gpio_desc *gpiod = chip->reset;
2347 unsigned long timeout;
2351 /* Set all ports to the disabled state. */
2352 for (i = 0; i < chip->info->num_ports; i++) {
2353 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
2357 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
2363 /* Wait for transmit queues to drain. */
2364 usleep_range(2000, 4000);
2366 /* If there is a gpio connected to the reset pin, toggle it */
2368 gpiod_set_value_cansleep(gpiod, 1);
2369 usleep_range(10000, 20000);
2370 gpiod_set_value_cansleep(gpiod, 0);
2371 usleep_range(10000, 20000);
2374 /* Reset the switch. Keep the PPU active if requested. The PPU
2375 * needs to be active to support indirect phy register access
2376 * through global registers 0x18 and 0x19.
2379 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
2381 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
2385 /* Wait up to one second for reset to complete. */
2386 timeout = jiffies + 1 * HZ;
2387 while (time_before(jiffies, timeout)) {
2388 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
2392 if ((ret & is_reset) == is_reset)
2394 usleep_range(1000, 2000);
2396 if (time_after(jiffies, timeout))
2404 static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
2409 /* Clear Power Down bit */
2410 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2414 if (val & BMCR_PDOWN) {
2416 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
2422 static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port,
2425 int addr = chip->info->port_base_addr + port;
2427 if (port >= chip->info->num_ports)
2430 return mv88e6xxx_read(chip, addr, reg, val);
2433 static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
2435 struct dsa_switch *ds = chip->ds;
2439 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2440 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2441 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2442 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
2443 /* MAC Forcing register: don't force link, speed,
2444 * duplex or flow control state to any particular
2445 * values on physical ports, but force the CPU port
2446 * and all DSA ports to their maximum bandwidth and
2449 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
2450 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2451 reg &= ~PORT_PCS_CTRL_UNFORCED;
2452 reg |= PORT_PCS_CTRL_FORCE_LINK |
2453 PORT_PCS_CTRL_LINK_UP |
2454 PORT_PCS_CTRL_DUPLEX_FULL |
2455 PORT_PCS_CTRL_FORCE_DUPLEX;
2456 if (mv88e6xxx_6065_family(chip))
2457 reg |= PORT_PCS_CTRL_100;
2459 reg |= PORT_PCS_CTRL_1000;
2461 reg |= PORT_PCS_CTRL_UNFORCED;
2464 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2465 PORT_PCS_CTRL, reg);
2470 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2471 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2472 * tunneling, determine priority by looking at 802.1p and IP
2473 * priority fields (IP prio has precedence), and set STP state
2476 * If this is the CPU link, use DSA or EDSA tagging depending
2477 * on which tagging mode was configured.
2479 * If this is a link to another switch, use DSA tagging mode.
2481 * If this is the upstream port for this switch, enable
2482 * forwarding of unknown unicasts and multicasts.
2485 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2486 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2487 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2488 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
2489 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2490 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2491 PORT_CONTROL_STATE_FORWARDING;
2492 if (dsa_is_cpu_port(ds, port)) {
2493 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
2494 reg |= PORT_CONTROL_DSA_TAG;
2495 if (mv88e6xxx_6352_family(chip) ||
2496 mv88e6xxx_6351_family(chip) ||
2497 mv88e6xxx_6165_family(chip) ||
2498 mv88e6xxx_6097_family(chip) ||
2499 mv88e6xxx_6320_family(chip)) {
2500 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2501 PORT_CONTROL_FORWARD_UNKNOWN |
2502 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2505 if (mv88e6xxx_6352_family(chip) ||
2506 mv88e6xxx_6351_family(chip) ||
2507 mv88e6xxx_6165_family(chip) ||
2508 mv88e6xxx_6097_family(chip) ||
2509 mv88e6xxx_6095_family(chip) ||
2510 mv88e6xxx_6065_family(chip) ||
2511 mv88e6xxx_6185_family(chip) ||
2512 mv88e6xxx_6320_family(chip)) {
2513 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2516 if (dsa_is_dsa_port(ds, port)) {
2517 if (mv88e6xxx_6095_family(chip) ||
2518 mv88e6xxx_6185_family(chip))
2519 reg |= PORT_CONTROL_DSA_TAG;
2520 if (mv88e6xxx_6352_family(chip) ||
2521 mv88e6xxx_6351_family(chip) ||
2522 mv88e6xxx_6165_family(chip) ||
2523 mv88e6xxx_6097_family(chip) ||
2524 mv88e6xxx_6320_family(chip)) {
2525 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2528 if (port == dsa_upstream_port(ds))
2529 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2530 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2533 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2539 /* If this port is connected to a SerDes, make sure the SerDes is not
2542 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
2543 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
2546 ret &= PORT_STATUS_CMODE_MASK;
2547 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2548 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2549 (ret == PORT_STATUS_CMODE_SGMII)) {
2550 ret = mv88e6xxx_serdes_power_on(chip);
2556 /* Port Control 2: don't force a good FCS, set the maximum frame size to
2557 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2558 * untagged frames on this port, do a destination address lookup on all
2559 * received packets as usual, disable ARP mirroring and don't send a
2560 * copy of all transmitted/received frames on this port to the CPU.
2563 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2564 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2565 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2566 mv88e6xxx_6185_family(chip))
2567 reg = PORT_CONTROL_2_MAP_DA;
2569 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2570 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
2571 reg |= PORT_CONTROL_2_JUMBO_10240;
2573 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
2574 /* Set the upstream port this port should use */
2575 reg |= dsa_upstream_port(ds);
2576 /* enable forwarding of unknown multicast addresses to
2579 if (port == dsa_upstream_port(ds))
2580 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2583 reg |= PORT_CONTROL_2_8021Q_DISABLED;
2586 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2587 PORT_CONTROL_2, reg);
2592 /* Port Association Vector: when learning source addresses
2593 * of packets, add the address to the address database using
2594 * a port bitmap that has only the bit for this port set and
2595 * the other bits clear.
2598 /* Disable learning for CPU port */
2599 if (dsa_is_cpu_port(ds, port))
2602 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2607 /* Egress rate control 2: disable egress rate control. */
2608 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
2613 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2614 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2615 mv88e6xxx_6320_family(chip)) {
2616 /* Do not limit the period of time that this port can
2617 * be paused for by the remote end or the period of
2618 * time that this port can pause the remote end.
2620 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2621 PORT_PAUSE_CTRL, 0x0000);
2625 /* Port ATU control: disable limiting the number of
2626 * address database entries that this port is allowed
2629 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2630 PORT_ATU_CONTROL, 0x0000);
2631 /* Priority Override: disable DA, SA and VTU priority
2634 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2635 PORT_PRI_OVERRIDE, 0x0000);
2639 /* Port Ethertype: use the Ethertype DSA Ethertype
2642 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2643 PORT_ETH_TYPE, ETH_P_EDSA);
2646 /* Tag Remap: use an identity 802.1p prio -> switch
2649 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2650 PORT_TAG_REGMAP_0123, 0x3210);
2654 /* Tag Remap 2: use an identity 802.1p prio -> switch
2657 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2658 PORT_TAG_REGMAP_4567, 0x7654);
2663 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2664 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2665 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2666 mv88e6xxx_6320_family(chip)) {
2667 /* Rate Control: disable ingress rate limiting. */
2668 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2669 PORT_RATE_CONTROL, 0x0001);
2674 /* Port Control 1: disable trunking, disable sending
2675 * learning messages to this port.
2677 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2682 /* Port based VLAN map: give each port the same default address
2683 * database, and allow bidirectional communication between the
2684 * CPU and DSA port(s), and the other ports.
2686 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
2690 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
2694 /* Default VLAN ID and priority: don't set a default VLAN
2695 * ID, and set the default packet priority to zero.
2697 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
2705 static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2709 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2710 (addr[0] << 8) | addr[1]);
2714 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2715 (addr[2] << 8) | addr[3]);
2719 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2720 (addr[4] << 8) | addr[5]);
2723 static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2726 const unsigned int coeff = chip->info->age_time_coeff;
2727 const unsigned int min = 0x01 * coeff;
2728 const unsigned int max = 0xff * coeff;
2733 if (msecs < min || msecs > max)
2736 /* Round to nearest multiple of coeff */
2737 age_time = (msecs + coeff / 2) / coeff;
2739 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2743 /* AgeTime is 11:4 bits */
2745 val |= age_time << 4;
2747 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2750 static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2751 unsigned int ageing_time)
2753 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2756 mutex_lock(&chip->reg_lock);
2757 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2758 mutex_unlock(&chip->reg_lock);
2763 static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
2765 struct dsa_switch *ds = chip->ds;
2766 u32 upstream_port = dsa_upstream_port(ds);
2770 /* Enable the PHY Polling Unit if present, don't discard any packets,
2771 * and mask all interrupt sources.
2774 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2775 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
2776 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2778 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
2782 /* Configure the upstream port, and configure it as the port to which
2783 * ingress and egress and ARP monitor frames are to be sent.
2785 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2786 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2787 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2788 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2793 /* Disable remote management, and set the switch's DSA device number. */
2794 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
2795 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2796 (ds->index & 0x1f));
2800 /* Clear all the VTU and STU entries */
2801 err = _mv88e6xxx_vtu_stu_flush(chip);
2805 /* Set the default address aging time to 5 minutes, and
2806 * enable address learn messages to be sent to all message
2809 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2810 GLOBAL_ATU_CONTROL_LEARN2ALL);
2814 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2818 /* Clear all ATU entries */
2819 err = _mv88e6xxx_atu_flush(chip, 0, true);
2823 /* Configure the IP ToS mapping registers. */
2824 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2827 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2830 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2833 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2836 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2839 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2842 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2845 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2849 /* Configure the IEEE 802.1p priority mapping register. */
2850 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2854 /* Clear the statistics counters for all ports */
2855 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2856 GLOBAL_STATS_OP_FLUSH_ALL);
2860 /* Wait for the flush to complete. */
2861 err = _mv88e6xxx_stats_wait(chip);
2868 static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
2869 int target, int port)
2871 u16 val = (target << 8) | (port & 0xf);
2873 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
2876 static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
2881 /* Initialize the routing port to the 32 possible target devices */
2882 for (target = 0; target < 32; ++target) {
2885 if (target < DSA_MAX_SWITCHES) {
2886 port = chip->ds->rtable[target];
2887 if (port == DSA_RTABLE_NONE)
2891 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
2899 static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
2900 bool hask, u16 mask)
2902 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2903 u16 val = (num << 12) | (mask & port_mask);
2906 val |= GLOBAL2_TRUNK_MASK_HASK;
2908 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
2911 static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
2914 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2915 u16 val = (id << 11) | (map & port_mask);
2917 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
2920 static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
2922 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2925 /* Clear all eight possible Trunk Mask vectors */
2926 for (i = 0; i < 8; ++i) {
2927 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
2932 /* Clear all sixteen possible Trunk ID routing vectors */
2933 for (i = 0; i < 16; ++i) {
2934 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
2942 static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
2946 /* Init all Ingress Rate Limit resources of all ports */
2947 for (port = 0; port < chip->info->num_ports; ++port) {
2948 /* XXX newer chips (like 88E6390) have different 2-bit ops */
2949 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2950 GLOBAL2_IRL_CMD_OP_INIT_ALL |
2955 /* Wait for the operation to complete */
2956 err = mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2957 GLOBAL2_IRL_CMD_BUSY);
2965 /* Indirect write to the Switch MAC/WoL/WoF register */
2966 static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
2967 unsigned int pointer, u8 data)
2969 u16 val = (pointer << 8) | data;
2971 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
2974 static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2978 for (i = 0; i < 6; i++) {
2979 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
2987 static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
2990 u16 val = (pointer << 8) | (data & 0x7);
2992 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
2995 static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
2999 /* Clear all sixteen possible Priority Override entries */
3000 for (i = 0; i < 16; i++) {
3001 err = mv88e6xxx_g2_pot_write(chip, i, 0);
3009 static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
3011 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD,
3012 GLOBAL2_EEPROM_CMD_BUSY |
3013 GLOBAL2_EEPROM_CMD_RUNNING);
3016 static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3020 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, cmd);
3024 return mv88e6xxx_g2_eeprom_wait(chip);
3027 static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
3030 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
3033 err = mv88e6xxx_g2_eeprom_wait(chip);
3037 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3041 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3044 static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
3047 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
3050 err = mv88e6xxx_g2_eeprom_wait(chip);
3054 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3058 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3061 static int mv88e6xxx_g2_smi_phy_wait(struct mv88e6xxx_chip *chip)
3063 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD,
3064 GLOBAL2_SMI_PHY_CMD_BUSY);
3067 static int mv88e6xxx_g2_smi_phy_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3071 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD, cmd);
3075 return mv88e6xxx_g2_smi_phy_wait(chip);
3078 static int mv88e6xxx_g2_smi_phy_read(struct mv88e6xxx_chip *chip, int addr,
3081 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_READ_DATA | (addr << 5) | reg;
3084 err = mv88e6xxx_g2_smi_phy_wait(chip);
3088 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3092 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3095 static int mv88e6xxx_g2_smi_phy_write(struct mv88e6xxx_chip *chip, int addr,
3098 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_WRITE_DATA | (addr << 5) | reg;
3101 err = mv88e6xxx_g2_smi_phy_wait(chip);
3105 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3109 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3112 static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3113 .read = mv88e6xxx_g2_smi_phy_read,
3114 .write = mv88e6xxx_g2_smi_phy_write,
3117 static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3122 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3123 /* Consider the frames with reserved multicast destination
3124 * addresses matching 01:80:c2:00:00:2x as MGMT.
3126 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3132 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3133 /* Consider the frames with reserved multicast destination
3134 * addresses matching 01:80:c2:00:00:0x as MGMT.
3136 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3142 /* Ignore removed tag data on doubly tagged packets, disable
3143 * flow control messages, force flow control priority to the
3144 * highest, and send all special multicast frames to the CPU
3145 * port at the highest priority.
3147 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3148 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3149 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3150 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3151 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
3155 /* Program the DSA routing table. */
3156 err = mv88e6xxx_g2_set_device_mapping(chip);
3160 /* Clear all trunk masks and mapping. */
3161 err = mv88e6xxx_g2_clear_trunk(chip);
3165 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3166 /* Disable ingress rate limiting by resetting all per port
3167 * ingress rate limit resources to their initial state.
3169 err = mv88e6xxx_g2_clear_irl(chip);
3174 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3175 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3176 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3177 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3182 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
3183 /* Clear the priority override table. */
3184 err = mv88e6xxx_g2_clear_pot(chip);
3192 static int mv88e6xxx_setup(struct dsa_switch *ds)
3194 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3199 ds->slave_mii_bus = chip->mdio_bus;
3201 mutex_lock(&chip->reg_lock);
3203 err = mv88e6xxx_switch_reset(chip);
3207 /* Setup Switch Port Registers */
3208 for (i = 0; i < chip->info->num_ports; i++) {
3209 err = mv88e6xxx_setup_port(chip, i);
3214 /* Setup Switch Global 1 Registers */
3215 err = mv88e6xxx_g1_setup(chip);
3219 /* Setup Switch Global 2 Registers */
3220 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3221 err = mv88e6xxx_g2_setup(chip);
3227 mutex_unlock(&chip->reg_lock);
3232 static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3234 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3237 mutex_lock(&chip->reg_lock);
3239 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3240 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3241 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3243 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3245 mutex_unlock(&chip->reg_lock);
3250 static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
3252 struct mv88e6xxx_chip *chip = bus->priv;
3256 if (phy >= chip->info->num_ports)
3259 mutex_lock(&chip->reg_lock);
3260 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
3261 mutex_unlock(&chip->reg_lock);
3263 return err ? err : val;
3266 static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
3268 struct mv88e6xxx_chip *chip = bus->priv;
3271 if (phy >= chip->info->num_ports)
3274 mutex_lock(&chip->reg_lock);
3275 err = mv88e6xxx_phy_write(chip, phy, reg, val);
3276 mutex_unlock(&chip->reg_lock);
3281 static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
3282 struct device_node *np)
3285 struct mii_bus *bus;
3289 chip->mdio_np = of_get_child_by_name(np, "mdio");
3291 bus = devm_mdiobus_alloc(chip->dev);
3295 bus->priv = (void *)chip;
3297 bus->name = np->full_name;
3298 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3300 bus->name = "mv88e6xxx SMI";
3301 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3304 bus->read = mv88e6xxx_mdio_read;
3305 bus->write = mv88e6xxx_mdio_write;
3306 bus->parent = chip->dev;
3309 err = of_mdiobus_register(bus, chip->mdio_np);
3311 err = mdiobus_register(bus);
3313 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
3316 chip->mdio_bus = bus;
3322 of_node_put(chip->mdio_np);
3327 static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
3330 struct mii_bus *bus = chip->mdio_bus;
3332 mdiobus_unregister(bus);
3335 of_node_put(chip->mdio_np);
3338 #ifdef CONFIG_NET_DSA_HWMON
3340 static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3342 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3348 mutex_lock(&chip->reg_lock);
3350 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
3354 /* Enable temperature sensor */
3355 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3359 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
3363 /* Wait for temperature to stabilize */
3364 usleep_range(10000, 12000);
3366 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3370 /* Disable temperature sensor */
3371 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
3375 *temp = ((val & 0x1f) - 5) * 5;
3378 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
3379 mutex_unlock(&chip->reg_lock);
3383 static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3385 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3386 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3392 mutex_lock(&chip->reg_lock);
3393 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3394 mutex_unlock(&chip->reg_lock);
3398 *temp = (val & 0xff) - 25;
3403 static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3405 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3407 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
3410 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
3411 return mv88e63xx_get_temp(ds, temp);
3413 return mv88e61xx_get_temp(ds, temp);
3416 static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3418 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3419 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3423 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3428 mutex_lock(&chip->reg_lock);
3429 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3430 mutex_unlock(&chip->reg_lock);
3434 *temp = (((val >> 8) & 0x1f) * 5) - 25;
3439 static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3441 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3442 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3446 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3449 mutex_lock(&chip->reg_lock);
3450 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3453 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3454 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3455 (val & 0xe0ff) | (temp << 8));
3457 mutex_unlock(&chip->reg_lock);
3462 static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3464 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3465 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3469 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3474 mutex_lock(&chip->reg_lock);
3475 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3476 mutex_unlock(&chip->reg_lock);
3480 *alarm = !!(val & 0x40);
3484 #endif /* CONFIG_NET_DSA_HWMON */
3486 static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3488 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3490 return chip->eeprom_len;
3493 static int mv88e6xxx_get_eeprom16(struct mv88e6xxx_chip *chip,
3494 struct ethtool_eeprom *eeprom, u8 *data)
3496 unsigned int offset = eeprom->offset;
3497 unsigned int len = eeprom->len;
3504 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3508 *data++ = (val >> 8) & 0xff;
3516 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3520 *data++ = val & 0xff;
3521 *data++ = (val >> 8) & 0xff;
3529 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3533 *data++ = val & 0xff;
3543 static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3544 struct ethtool_eeprom *eeprom, u8 *data)
3546 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3549 mutex_lock(&chip->reg_lock);
3551 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3552 err = mv88e6xxx_get_eeprom16(chip, eeprom, data);
3556 mutex_unlock(&chip->reg_lock);
3561 eeprom->magic = 0xc3ec4951;
3566 static int mv88e6xxx_set_eeprom16(struct mv88e6xxx_chip *chip,
3567 struct ethtool_eeprom *eeprom, u8 *data)
3569 unsigned int offset = eeprom->offset;
3570 unsigned int len = eeprom->len;
3574 /* Ensure the RO WriteEn bit is set */
3575 err = mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, &val);
3579 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
3585 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3589 val = (*data++ << 8) | (val & 0xff);
3591 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3602 val |= *data++ << 8;
3604 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3614 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3618 val = (val & 0xff00) | *data++;
3620 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3632 static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3633 struct ethtool_eeprom *eeprom, u8 *data)
3635 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3638 if (eeprom->magic != 0xc3ec4951)
3641 mutex_lock(&chip->reg_lock);
3643 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3644 err = mv88e6xxx_set_eeprom16(chip, eeprom, data);
3648 mutex_unlock(&chip->reg_lock);
3653 static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3655 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3656 .family = MV88E6XXX_FAMILY_6097,
3657 .name = "Marvell 88E6085",
3658 .num_databases = 4096,
3660 .port_base_addr = 0x10,
3661 .age_time_coeff = 15000,
3662 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3666 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3667 .family = MV88E6XXX_FAMILY_6095,
3668 .name = "Marvell 88E6095/88E6095F",
3669 .num_databases = 256,
3671 .port_base_addr = 0x10,
3672 .age_time_coeff = 15000,
3673 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3677 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3678 .family = MV88E6XXX_FAMILY_6165,
3679 .name = "Marvell 88E6123",
3680 .num_databases = 4096,
3682 .port_base_addr = 0x10,
3683 .age_time_coeff = 15000,
3684 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3688 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3689 .family = MV88E6XXX_FAMILY_6185,
3690 .name = "Marvell 88E6131",
3691 .num_databases = 256,
3693 .port_base_addr = 0x10,
3694 .age_time_coeff = 15000,
3695 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3699 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3700 .family = MV88E6XXX_FAMILY_6165,
3701 .name = "Marvell 88E6161",
3702 .num_databases = 4096,
3704 .port_base_addr = 0x10,
3705 .age_time_coeff = 15000,
3706 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3710 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3711 .family = MV88E6XXX_FAMILY_6165,
3712 .name = "Marvell 88E6165",
3713 .num_databases = 4096,
3715 .port_base_addr = 0x10,
3716 .age_time_coeff = 15000,
3717 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3721 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3722 .family = MV88E6XXX_FAMILY_6351,
3723 .name = "Marvell 88E6171",
3724 .num_databases = 4096,
3726 .port_base_addr = 0x10,
3727 .age_time_coeff = 15000,
3728 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3732 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3733 .family = MV88E6XXX_FAMILY_6352,
3734 .name = "Marvell 88E6172",
3735 .num_databases = 4096,
3737 .port_base_addr = 0x10,
3738 .age_time_coeff = 15000,
3739 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3743 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3744 .family = MV88E6XXX_FAMILY_6351,
3745 .name = "Marvell 88E6175",
3746 .num_databases = 4096,
3748 .port_base_addr = 0x10,
3749 .age_time_coeff = 15000,
3750 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3754 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3755 .family = MV88E6XXX_FAMILY_6352,
3756 .name = "Marvell 88E6176",
3757 .num_databases = 4096,
3759 .port_base_addr = 0x10,
3760 .age_time_coeff = 15000,
3761 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3765 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3766 .family = MV88E6XXX_FAMILY_6185,
3767 .name = "Marvell 88E6185",
3768 .num_databases = 256,
3770 .port_base_addr = 0x10,
3771 .age_time_coeff = 15000,
3772 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3776 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3777 .family = MV88E6XXX_FAMILY_6352,
3778 .name = "Marvell 88E6240",
3779 .num_databases = 4096,
3781 .port_base_addr = 0x10,
3782 .age_time_coeff = 15000,
3783 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3787 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3788 .family = MV88E6XXX_FAMILY_6320,
3789 .name = "Marvell 88E6320",
3790 .num_databases = 4096,
3792 .port_base_addr = 0x10,
3793 .age_time_coeff = 15000,
3794 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3798 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3799 .family = MV88E6XXX_FAMILY_6320,
3800 .name = "Marvell 88E6321",
3801 .num_databases = 4096,
3803 .port_base_addr = 0x10,
3804 .age_time_coeff = 15000,
3805 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3809 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3810 .family = MV88E6XXX_FAMILY_6351,
3811 .name = "Marvell 88E6350",
3812 .num_databases = 4096,
3814 .port_base_addr = 0x10,
3815 .age_time_coeff = 15000,
3816 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3820 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3821 .family = MV88E6XXX_FAMILY_6351,
3822 .name = "Marvell 88E6351",
3823 .num_databases = 4096,
3825 .port_base_addr = 0x10,
3826 .age_time_coeff = 15000,
3827 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3831 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3832 .family = MV88E6XXX_FAMILY_6352,
3833 .name = "Marvell 88E6352",
3834 .num_databases = 4096,
3836 .port_base_addr = 0x10,
3837 .age_time_coeff = 15000,
3838 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3842 static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
3846 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3847 if (mv88e6xxx_table[i].prod_num == prod_num)
3848 return &mv88e6xxx_table[i];
3853 static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
3855 const struct mv88e6xxx_info *info;
3856 unsigned int prod_num, rev;
3860 mutex_lock(&chip->reg_lock);
3861 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3862 mutex_unlock(&chip->reg_lock);
3866 prod_num = (id & 0xfff0) >> 4;
3869 info = mv88e6xxx_lookup_info(prod_num);
3873 /* Update the compatible info with the probed one */
3876 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3877 chip->info->prod_num, chip->info->name, rev);
3882 static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
3884 struct mv88e6xxx_chip *chip;
3886 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3892 mutex_init(&chip->reg_lock);
3897 static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3898 .read = mv88e6xxx_read,
3899 .write = mv88e6xxx_write,
3902 static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3904 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3905 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3906 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3907 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3908 mv88e6xxx_ppu_state_init(chip);
3910 chip->phy_ops = &mv88e6xxx_phy_ops;
3914 static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
3915 struct mii_bus *bus, int sw_addr)
3917 /* ADDR[0] pin is unavailable externally and considered zero */
3922 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3923 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
3924 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3929 chip->sw_addr = sw_addr;
3934 static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3935 struct device *host_dev, int sw_addr,
3938 struct mv88e6xxx_chip *chip;
3939 struct mii_bus *bus;
3942 bus = dsa_host_dev_to_mii_bus(host_dev);
3946 chip = mv88e6xxx_alloc_chip(dsa_dev);
3950 /* Legacy SMI probing will only support chips similar to 88E6085 */
3951 chip->info = &mv88e6xxx_table[MV88E6085];
3953 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
3957 err = mv88e6xxx_detect(chip);
3961 mv88e6xxx_phy_init(chip);
3963 err = mv88e6xxx_mdio_register(chip, NULL);
3969 return chip->info->name;
3971 devm_kfree(dsa_dev, chip);
3976 static struct dsa_switch_driver mv88e6xxx_switch_driver = {
3977 .tag_protocol = DSA_TAG_PROTO_EDSA,
3978 .probe = mv88e6xxx_drv_probe,
3979 .setup = mv88e6xxx_setup,
3980 .set_addr = mv88e6xxx_set_addr,
3981 .adjust_link = mv88e6xxx_adjust_link,
3982 .get_strings = mv88e6xxx_get_strings,
3983 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3984 .get_sset_count = mv88e6xxx_get_sset_count,
3985 .set_eee = mv88e6xxx_set_eee,
3986 .get_eee = mv88e6xxx_get_eee,
3987 #ifdef CONFIG_NET_DSA_HWMON
3988 .get_temp = mv88e6xxx_get_temp,
3989 .get_temp_limit = mv88e6xxx_get_temp_limit,
3990 .set_temp_limit = mv88e6xxx_set_temp_limit,
3991 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3993 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
3994 .get_eeprom = mv88e6xxx_get_eeprom,
3995 .set_eeprom = mv88e6xxx_set_eeprom,
3996 .get_regs_len = mv88e6xxx_get_regs_len,
3997 .get_regs = mv88e6xxx_get_regs,
3998 .set_ageing_time = mv88e6xxx_set_ageing_time,
3999 .port_bridge_join = mv88e6xxx_port_bridge_join,
4000 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
4001 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
4002 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4003 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4004 .port_vlan_add = mv88e6xxx_port_vlan_add,
4005 .port_vlan_del = mv88e6xxx_port_vlan_del,
4006 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4007 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4008 .port_fdb_add = mv88e6xxx_port_fdb_add,
4009 .port_fdb_del = mv88e6xxx_port_fdb_del,
4010 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
4013 static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
4014 struct device_node *np)
4016 struct device *dev = chip->dev;
4017 struct dsa_switch *ds;
4019 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4025 ds->drv = &mv88e6xxx_switch_driver;
4027 dev_set_drvdata(dev, ds);
4029 return dsa_register_switch(ds, np);
4032 static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
4034 dsa_unregister_switch(chip->ds);
4037 static int mv88e6xxx_probe(struct mdio_device *mdiodev)
4039 struct device *dev = &mdiodev->dev;
4040 struct device_node *np = dev->of_node;
4041 const struct mv88e6xxx_info *compat_info;
4042 struct mv88e6xxx_chip *chip;
4046 compat_info = of_device_get_match_data(dev);
4050 chip = mv88e6xxx_alloc_chip(dev);
4054 chip->info = compat_info;
4056 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
4060 err = mv88e6xxx_detect(chip);
4064 mv88e6xxx_phy_init(chip);
4066 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4067 if (IS_ERR(chip->reset))
4068 return PTR_ERR(chip->reset);
4070 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
4071 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
4072 chip->eeprom_len = eeprom_len;
4074 err = mv88e6xxx_mdio_register(chip, np);
4078 err = mv88e6xxx_register_switch(chip, np);
4080 mv88e6xxx_mdio_unregister(chip);
4087 static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4089 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
4090 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
4092 mv88e6xxx_unregister_switch(chip);
4093 mv88e6xxx_mdio_unregister(chip);
4096 static const struct of_device_id mv88e6xxx_of_match[] = {
4098 .compatible = "marvell,mv88e6085",
4099 .data = &mv88e6xxx_table[MV88E6085],
4104 MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4106 static struct mdio_driver mv88e6xxx_driver = {
4107 .probe = mv88e6xxx_probe,
4108 .remove = mv88e6xxx_remove,
4110 .name = "mv88e6085",
4111 .of_match_table = mv88e6xxx_of_match,
4115 static int __init mv88e6xxx_init(void)
4117 register_switch_driver(&mv88e6xxx_switch_driver);
4118 return mdio_driver_register(&mv88e6xxx_driver);
4120 module_init(mv88e6xxx_init);
4122 static void __exit mv88e6xxx_cleanup(void)
4124 mdio_driver_unregister(&mv88e6xxx_driver);
4125 unregister_switch_driver(&mv88e6xxx_switch_driver);
4127 module_exit(mv88e6xxx_cleanup);
4129 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4130 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4131 MODULE_LICENSE("GPL");