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);
328 dev_err(chip->dev, "Timeout while waiting for switch\n");
332 /* Indirect write to single pointer-data register with an Update bit */
333 static int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg,
339 /* Wait until the previous operation is completed */
340 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
344 /* Set the Update bit to trigger a write operation */
345 val = BIT(15) | update;
347 return mv88e6xxx_write(chip, addr, reg, val);
350 static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
355 err = mv88e6xxx_read(chip, addr, reg, &val);
362 static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
365 return mv88e6xxx_write(chip, addr, reg, val);
368 static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
373 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
377 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
378 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
382 for (i = 0; i < 16; i++) {
383 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
387 usleep_range(1000, 2000);
388 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
389 GLOBAL_STATUS_PPU_POLLING)
396 static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
400 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
404 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
405 ret | GLOBAL_CONTROL_PPU_ENABLE);
409 for (i = 0; i < 16; i++) {
410 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
414 usleep_range(1000, 2000);
415 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
416 GLOBAL_STATUS_PPU_POLLING)
423 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
425 struct mv88e6xxx_chip *chip;
427 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
429 mutex_lock(&chip->reg_lock);
431 if (mutex_trylock(&chip->ppu_mutex)) {
432 if (mv88e6xxx_ppu_enable(chip) == 0)
433 chip->ppu_disabled = 0;
434 mutex_unlock(&chip->ppu_mutex);
437 mutex_unlock(&chip->reg_lock);
440 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
442 struct mv88e6xxx_chip *chip = (void *)_ps;
444 schedule_work(&chip->ppu_work);
447 static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
451 mutex_lock(&chip->ppu_mutex);
453 /* If the PHY polling unit is enabled, disable it so that
454 * we can access the PHY registers. If it was already
455 * disabled, cancel the timer that is going to re-enable
458 if (!chip->ppu_disabled) {
459 ret = mv88e6xxx_ppu_disable(chip);
461 mutex_unlock(&chip->ppu_mutex);
464 chip->ppu_disabled = 1;
466 del_timer(&chip->ppu_timer);
473 static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
475 /* Schedule a timer to re-enable the PHY polling unit. */
476 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
477 mutex_unlock(&chip->ppu_mutex);
480 static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
482 mutex_init(&chip->ppu_mutex);
483 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
484 init_timer(&chip->ppu_timer);
485 chip->ppu_timer.data = (unsigned long)chip;
486 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
489 static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
491 del_timer_sync(&chip->ppu_timer);
494 static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
499 err = mv88e6xxx_ppu_access_get(chip);
501 err = mv88e6xxx_read(chip, addr, reg, val);
502 mv88e6xxx_ppu_access_put(chip);
508 static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
513 err = mv88e6xxx_ppu_access_get(chip);
515 err = mv88e6xxx_write(chip, addr, reg, val);
516 mv88e6xxx_ppu_access_put(chip);
522 static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
523 .read = mv88e6xxx_phy_ppu_read,
524 .write = mv88e6xxx_phy_ppu_write,
527 static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
529 return chip->info->family == MV88E6XXX_FAMILY_6065;
532 static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
534 return chip->info->family == MV88E6XXX_FAMILY_6095;
537 static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
539 return chip->info->family == MV88E6XXX_FAMILY_6097;
542 static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
544 return chip->info->family == MV88E6XXX_FAMILY_6165;
547 static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
549 return chip->info->family == MV88E6XXX_FAMILY_6185;
552 static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
554 return chip->info->family == MV88E6XXX_FAMILY_6320;
557 static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
559 return chip->info->family == MV88E6XXX_FAMILY_6351;
562 static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
564 return chip->info->family == MV88E6XXX_FAMILY_6352;
567 static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
569 return chip->info->num_databases;
572 static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
574 /* Does the device have dedicated FID registers for ATU and VTU ops? */
575 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
576 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
582 /* We expect the switch to perform auto negotiation if there is a real
583 * phy. However, in the case of a fixed link phy, we force the port
584 * settings from the fixed link settings.
586 static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
587 struct phy_device *phydev)
589 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
593 if (!phy_is_pseudo_fixed_link(phydev))
596 mutex_lock(&chip->reg_lock);
598 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
602 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
603 PORT_PCS_CTRL_FORCE_LINK |
604 PORT_PCS_CTRL_DUPLEX_FULL |
605 PORT_PCS_CTRL_FORCE_DUPLEX |
606 PORT_PCS_CTRL_UNFORCED);
608 reg |= PORT_PCS_CTRL_FORCE_LINK;
610 reg |= PORT_PCS_CTRL_LINK_UP;
612 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
615 switch (phydev->speed) {
617 reg |= PORT_PCS_CTRL_1000;
620 reg |= PORT_PCS_CTRL_100;
623 reg |= PORT_PCS_CTRL_10;
626 pr_info("Unknown speed");
630 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
631 if (phydev->duplex == DUPLEX_FULL)
632 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
634 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
635 (port >= chip->info->num_ports - 2)) {
636 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
637 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
638 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
639 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
640 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
641 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
642 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
644 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
647 mutex_unlock(&chip->reg_lock);
650 static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
655 for (i = 0; i < 10; i++) {
656 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
657 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
664 static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
668 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
669 port = (port + 1) << 5;
671 /* Snapshot the hardware statistics counters for this port. */
672 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
673 GLOBAL_STATS_OP_CAPTURE_PORT |
674 GLOBAL_STATS_OP_HIST_RX_TX | port);
678 /* Wait for the snapshotting to complete. */
679 ret = _mv88e6xxx_stats_wait(chip);
686 static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
694 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
695 GLOBAL_STATS_OP_READ_CAPTURED |
696 GLOBAL_STATS_OP_HIST_RX_TX | stat);
700 ret = _mv88e6xxx_stats_wait(chip);
704 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
710 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
717 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
718 { "in_good_octets", 8, 0x00, BANK0, },
719 { "in_bad_octets", 4, 0x02, BANK0, },
720 { "in_unicast", 4, 0x04, BANK0, },
721 { "in_broadcasts", 4, 0x06, BANK0, },
722 { "in_multicasts", 4, 0x07, BANK0, },
723 { "in_pause", 4, 0x16, BANK0, },
724 { "in_undersize", 4, 0x18, BANK0, },
725 { "in_fragments", 4, 0x19, BANK0, },
726 { "in_oversize", 4, 0x1a, BANK0, },
727 { "in_jabber", 4, 0x1b, BANK0, },
728 { "in_rx_error", 4, 0x1c, BANK0, },
729 { "in_fcs_error", 4, 0x1d, BANK0, },
730 { "out_octets", 8, 0x0e, BANK0, },
731 { "out_unicast", 4, 0x10, BANK0, },
732 { "out_broadcasts", 4, 0x13, BANK0, },
733 { "out_multicasts", 4, 0x12, BANK0, },
734 { "out_pause", 4, 0x15, BANK0, },
735 { "excessive", 4, 0x11, BANK0, },
736 { "collisions", 4, 0x1e, BANK0, },
737 { "deferred", 4, 0x05, BANK0, },
738 { "single", 4, 0x14, BANK0, },
739 { "multiple", 4, 0x17, BANK0, },
740 { "out_fcs_error", 4, 0x03, BANK0, },
741 { "late", 4, 0x1f, BANK0, },
742 { "hist_64bytes", 4, 0x08, BANK0, },
743 { "hist_65_127bytes", 4, 0x09, BANK0, },
744 { "hist_128_255bytes", 4, 0x0a, BANK0, },
745 { "hist_256_511bytes", 4, 0x0b, BANK0, },
746 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
747 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
748 { "sw_in_discards", 4, 0x10, PORT, },
749 { "sw_in_filtered", 2, 0x12, PORT, },
750 { "sw_out_filtered", 2, 0x13, PORT, },
751 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
770 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
771 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
772 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
773 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
774 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
775 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
776 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
779 static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
780 struct mv88e6xxx_hw_stat *stat)
782 switch (stat->type) {
786 return mv88e6xxx_6320_family(chip);
788 return mv88e6xxx_6095_family(chip) ||
789 mv88e6xxx_6185_family(chip) ||
790 mv88e6xxx_6097_family(chip) ||
791 mv88e6xxx_6165_family(chip) ||
792 mv88e6xxx_6351_family(chip) ||
793 mv88e6xxx_6352_family(chip);
798 static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
799 struct mv88e6xxx_hw_stat *s,
809 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
814 if (s->sizeof_stat == 4) {
815 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
824 _mv88e6xxx_stats_read(chip, s->reg, &low);
825 if (s->sizeof_stat == 8)
826 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
828 value = (((u64)high) << 16) | low;
832 static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
835 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
836 struct mv88e6xxx_hw_stat *stat;
839 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
840 stat = &mv88e6xxx_hw_stats[i];
841 if (mv88e6xxx_has_stat(chip, stat)) {
842 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
849 static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
851 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
852 struct mv88e6xxx_hw_stat *stat;
855 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
856 stat = &mv88e6xxx_hw_stats[i];
857 if (mv88e6xxx_has_stat(chip, stat))
863 static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
866 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
867 struct mv88e6xxx_hw_stat *stat;
871 mutex_lock(&chip->reg_lock);
873 ret = _mv88e6xxx_stats_snapshot(chip, port);
875 mutex_unlock(&chip->reg_lock);
878 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
879 stat = &mv88e6xxx_hw_stats[i];
880 if (mv88e6xxx_has_stat(chip, stat)) {
881 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
886 mutex_unlock(&chip->reg_lock);
889 static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
891 return 32 * sizeof(u16);
894 static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
895 struct ethtool_regs *regs, void *_p)
897 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
903 memset(p, 0xff, 32 * sizeof(u16));
905 mutex_lock(&chip->reg_lock);
907 for (i = 0; i < 32; i++) {
910 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
915 mutex_unlock(&chip->reg_lock);
918 static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
920 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
924 static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
925 struct ethtool_eee *e)
927 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
931 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
934 mutex_lock(&chip->reg_lock);
936 err = mv88e6xxx_phy_read(chip, port, 16, ®);
940 e->eee_enabled = !!(reg & 0x0200);
941 e->tx_lpi_enabled = !!(reg & 0x0100);
943 err = mv88e6xxx_read(chip, REG_PORT(port), PORT_STATUS, ®);
947 e->eee_active = !!(reg & PORT_STATUS_EEE);
949 mutex_unlock(&chip->reg_lock);
954 static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
955 struct phy_device *phydev, struct ethtool_eee *e)
957 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
961 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
964 mutex_lock(&chip->reg_lock);
966 err = mv88e6xxx_phy_read(chip, port, 16, ®);
973 if (e->tx_lpi_enabled)
976 err = mv88e6xxx_phy_write(chip, port, 16, reg);
978 mutex_unlock(&chip->reg_lock);
983 static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
987 if (mv88e6xxx_has_fid_reg(chip)) {
988 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
992 } else if (mv88e6xxx_num_databases(chip) == 256) {
993 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
994 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
998 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1000 ((fid << 8) & 0xf000));
1004 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1008 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1012 return _mv88e6xxx_atu_wait(chip);
1015 static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
1016 struct mv88e6xxx_atu_entry *entry)
1018 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1020 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1021 unsigned int mask, shift;
1024 data |= GLOBAL_ATU_DATA_TRUNK;
1025 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1026 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1028 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1029 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1032 data |= (entry->portv_trunkid << shift) & mask;
1035 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1038 static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
1039 struct mv88e6xxx_atu_entry *entry,
1045 err = _mv88e6xxx_atu_wait(chip);
1049 err = _mv88e6xxx_atu_data_write(chip, entry);
1054 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1055 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1057 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1058 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1061 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
1064 static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
1065 u16 fid, bool static_too)
1067 struct mv88e6xxx_atu_entry entry = {
1069 .state = 0, /* EntryState bits must be 0 */
1072 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1075 static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
1076 int from_port, int to_port, bool static_too)
1078 struct mv88e6xxx_atu_entry entry = {
1083 /* EntryState bits must be 0xF */
1084 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1086 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1087 entry.portv_trunkid = (to_port & 0x0f) << 4;
1088 entry.portv_trunkid |= from_port & 0x0f;
1090 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1093 static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
1094 int port, bool static_too)
1096 /* Destination port 0xF means remove the entries */
1097 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
1100 static const char * const mv88e6xxx_port_state_names[] = {
1101 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1102 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1103 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1104 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1107 static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
1110 struct dsa_switch *ds = chip->ds;
1114 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
1118 oldstate = reg & PORT_CONTROL_STATE_MASK;
1120 if (oldstate != state) {
1121 /* Flush forwarding database if we're moving a port
1122 * from Learning or Forwarding state to Disabled or
1123 * Blocking or Listening state.
1125 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1126 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1127 (state == PORT_CONTROL_STATE_DISABLED ||
1128 state == PORT_CONTROL_STATE_BLOCKING)) {
1129 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
1134 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1135 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
1140 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1141 mv88e6xxx_port_state_names[state],
1142 mv88e6xxx_port_state_names[oldstate]);
1148 static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
1150 struct net_device *bridge = chip->ports[port].bridge_dev;
1151 const u16 mask = (1 << chip->info->num_ports) - 1;
1152 struct dsa_switch *ds = chip->ds;
1153 u16 output_ports = 0;
1157 /* allow CPU port or DSA link(s) to send frames to every port */
1158 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1159 output_ports = mask;
1161 for (i = 0; i < chip->info->num_ports; ++i) {
1162 /* allow sending frames to every group member */
1163 if (bridge && chip->ports[i].bridge_dev == bridge)
1164 output_ports |= BIT(i);
1166 /* allow sending frames to CPU port and DSA link(s) */
1167 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1168 output_ports |= BIT(i);
1172 /* prevent frames from going back out of the port they came in on */
1173 output_ports &= ~BIT(port);
1175 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1180 reg |= output_ports & mask;
1182 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
1185 static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1188 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1193 case BR_STATE_DISABLED:
1194 stp_state = PORT_CONTROL_STATE_DISABLED;
1196 case BR_STATE_BLOCKING:
1197 case BR_STATE_LISTENING:
1198 stp_state = PORT_CONTROL_STATE_BLOCKING;
1200 case BR_STATE_LEARNING:
1201 stp_state = PORT_CONTROL_STATE_LEARNING;
1203 case BR_STATE_FORWARDING:
1205 stp_state = PORT_CONTROL_STATE_FORWARDING;
1209 mutex_lock(&chip->reg_lock);
1210 err = _mv88e6xxx_port_state(chip, port, stp_state);
1211 mutex_unlock(&chip->reg_lock);
1214 netdev_err(ds->ports[port].netdev,
1215 "failed to update state to %s\n",
1216 mv88e6xxx_port_state_names[stp_state]);
1219 static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
1222 struct dsa_switch *ds = chip->ds;
1226 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
1230 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1233 ret &= ~PORT_DEFAULT_VLAN_MASK;
1234 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1236 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
1237 PORT_DEFAULT_VLAN, ret);
1241 netdev_dbg(ds->ports[port].netdev,
1242 "DefaultVID %d (was %d)\n", *new, pvid);
1251 static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
1252 int port, u16 *pvid)
1254 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
1257 static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
1260 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
1263 static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
1265 return mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1266 GLOBAL_VTU_OP_BUSY);
1269 static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
1273 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
1277 return _mv88e6xxx_vtu_wait(chip);
1280 static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
1284 ret = _mv88e6xxx_vtu_wait(chip);
1288 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
1291 static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
1292 struct mv88e6xxx_vtu_stu_entry *entry,
1293 unsigned int nibble_offset)
1299 for (i = 0; i < 3; ++i) {
1300 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1301 GLOBAL_VTU_DATA_0_3 + i);
1308 for (i = 0; i < chip->info->num_ports; ++i) {
1309 unsigned int shift = (i % 4) * 4 + nibble_offset;
1310 u16 reg = regs[i / 4];
1312 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1318 static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
1319 struct mv88e6xxx_vtu_stu_entry *entry)
1321 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
1324 static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
1325 struct mv88e6xxx_vtu_stu_entry *entry)
1327 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
1330 static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
1331 struct mv88e6xxx_vtu_stu_entry *entry,
1332 unsigned int nibble_offset)
1334 u16 regs[3] = { 0 };
1338 for (i = 0; i < chip->info->num_ports; ++i) {
1339 unsigned int shift = (i % 4) * 4 + nibble_offset;
1340 u8 data = entry->data[i];
1342 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1345 for (i = 0; i < 3; ++i) {
1346 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
1347 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1355 static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
1356 struct mv88e6xxx_vtu_stu_entry *entry)
1358 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
1361 static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
1362 struct mv88e6xxx_vtu_stu_entry *entry)
1364 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
1367 static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
1369 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
1370 vid & GLOBAL_VTU_VID_MASK);
1373 static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
1374 struct mv88e6xxx_vtu_stu_entry *entry)
1376 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1379 ret = _mv88e6xxx_vtu_wait(chip);
1383 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1387 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1391 next.vid = ret & GLOBAL_VTU_VID_MASK;
1392 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1395 ret = mv88e6xxx_vtu_data_read(chip, &next);
1399 if (mv88e6xxx_has_fid_reg(chip)) {
1400 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1405 next.fid = ret & GLOBAL_VTU_FID_MASK;
1406 } else if (mv88e6xxx_num_databases(chip) == 256) {
1407 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1408 * VTU DBNum[3:0] are located in VTU Operation 3:0
1410 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1415 next.fid = (ret & 0xf00) >> 4;
1416 next.fid |= ret & 0xf;
1419 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1420 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1425 next.sid = ret & GLOBAL_VTU_SID_MASK;
1433 static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1434 struct switchdev_obj_port_vlan *vlan,
1435 int (*cb)(struct switchdev_obj *obj))
1437 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1438 struct mv88e6xxx_vtu_stu_entry next;
1442 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1445 mutex_lock(&chip->reg_lock);
1447 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
1451 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1456 err = _mv88e6xxx_vtu_getnext(chip, &next);
1463 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1466 /* reinit and dump this VLAN obj */
1467 vlan->vid_begin = next.vid;
1468 vlan->vid_end = next.vid;
1471 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1472 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1474 if (next.vid == pvid)
1475 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1477 err = cb(&vlan->obj);
1480 } while (next.vid < GLOBAL_VTU_VID_MASK);
1483 mutex_unlock(&chip->reg_lock);
1488 static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
1489 struct mv88e6xxx_vtu_stu_entry *entry)
1491 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1495 ret = _mv88e6xxx_vtu_wait(chip);
1502 /* Write port member tags */
1503 ret = mv88e6xxx_vtu_data_write(chip, entry);
1507 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1508 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1509 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1515 if (mv88e6xxx_has_fid_reg(chip)) {
1516 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1517 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1521 } else if (mv88e6xxx_num_databases(chip) == 256) {
1522 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1523 * VTU DBNum[3:0] are located in VTU Operation 3:0
1525 op |= (entry->fid & 0xf0) << 8;
1526 op |= entry->fid & 0xf;
1529 reg = GLOBAL_VTU_VID_VALID;
1531 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1532 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1536 return _mv88e6xxx_vtu_cmd(chip, op);
1539 static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
1540 struct mv88e6xxx_vtu_stu_entry *entry)
1542 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1545 ret = _mv88e6xxx_vtu_wait(chip);
1549 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1550 sid & GLOBAL_VTU_SID_MASK);
1554 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1558 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
1562 next.sid = ret & GLOBAL_VTU_SID_MASK;
1564 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1568 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1571 ret = mv88e6xxx_stu_data_read(chip, &next);
1580 static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
1581 struct mv88e6xxx_vtu_stu_entry *entry)
1586 ret = _mv88e6xxx_vtu_wait(chip);
1593 /* Write port states */
1594 ret = mv88e6xxx_stu_data_write(chip, entry);
1598 reg = GLOBAL_VTU_VID_VALID;
1600 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1604 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1605 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1609 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1612 static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
1615 struct dsa_switch *ds = chip->ds;
1620 if (mv88e6xxx_num_databases(chip) == 4096)
1622 else if (mv88e6xxx_num_databases(chip) == 256)
1627 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1628 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1632 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1635 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1636 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1638 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
1644 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1645 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
1649 fid |= (ret & upper_mask) << 4;
1653 ret |= (*new >> 4) & upper_mask;
1655 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
1660 netdev_dbg(ds->ports[port].netdev,
1661 "FID %d (was %d)\n", *new, fid);
1670 static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
1673 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
1676 static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
1679 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
1682 static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
1684 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1685 struct mv88e6xxx_vtu_stu_entry vlan;
1688 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1690 /* Set every FID bit used by the (un)bridged ports */
1691 for (i = 0; i < chip->info->num_ports; ++i) {
1692 err = _mv88e6xxx_port_fid_get(chip, i, fid);
1696 set_bit(*fid, fid_bitmap);
1699 /* Set every FID bit used by the VLAN entries */
1700 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1705 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1712 set_bit(vlan.fid, fid_bitmap);
1713 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1715 /* The reset value 0x000 is used to indicate that multiple address
1716 * databases are not needed. Return the next positive available.
1718 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1719 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
1722 /* Clear the database */
1723 return _mv88e6xxx_atu_flush(chip, *fid, true);
1726 static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
1727 struct mv88e6xxx_vtu_stu_entry *entry)
1729 struct dsa_switch *ds = chip->ds;
1730 struct mv88e6xxx_vtu_stu_entry vlan = {
1736 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
1740 /* exclude all ports except the CPU and DSA ports */
1741 for (i = 0; i < chip->info->num_ports; ++i)
1742 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1743 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1744 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1746 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1747 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
1748 struct mv88e6xxx_vtu_stu_entry vstp;
1750 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1751 * implemented, only one STU entry is needed to cover all VTU
1752 * entries. Thus, validate the SID 0.
1755 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
1759 if (vstp.sid != vlan.sid || !vstp.valid) {
1760 memset(&vstp, 0, sizeof(vstp));
1762 vstp.sid = vlan.sid;
1764 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
1774 static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
1775 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1782 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
1786 err = _mv88e6xxx_vtu_getnext(chip, entry);
1790 if (entry->vid != vid || !entry->valid) {
1793 /* -ENOENT would've been more appropriate, but switchdev expects
1794 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1797 err = _mv88e6xxx_vtu_new(chip, vid, entry);
1803 static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1804 u16 vid_begin, u16 vid_end)
1806 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1807 struct mv88e6xxx_vtu_stu_entry vlan;
1813 mutex_lock(&chip->reg_lock);
1815 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
1820 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1827 if (vlan.vid > vid_end)
1830 for (i = 0; i < chip->info->num_ports; ++i) {
1831 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1835 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1838 if (chip->ports[i].bridge_dev ==
1839 chip->ports[port].bridge_dev)
1840 break; /* same bridge, check next VLAN */
1842 netdev_warn(ds->ports[port].netdev,
1843 "hardware VLAN %d already used by %s\n",
1845 netdev_name(chip->ports[i].bridge_dev));
1849 } while (vlan.vid < vid_end);
1852 mutex_unlock(&chip->reg_lock);
1857 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1858 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1859 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1860 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1861 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1864 static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1865 bool vlan_filtering)
1867 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1868 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1869 PORT_CONTROL_2_8021Q_DISABLED;
1872 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1875 mutex_lock(&chip->reg_lock);
1877 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
1881 old = ret & PORT_CONTROL_2_8021Q_MASK;
1884 ret &= ~PORT_CONTROL_2_8021Q_MASK;
1885 ret |= new & PORT_CONTROL_2_8021Q_MASK;
1887 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
1892 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
1893 mv88e6xxx_port_8021q_mode_names[new],
1894 mv88e6xxx_port_8021q_mode_names[old]);
1899 mutex_unlock(&chip->reg_lock);
1905 mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1906 const struct switchdev_obj_port_vlan *vlan,
1907 struct switchdev_trans *trans)
1909 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1912 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1915 /* If the requested port doesn't belong to the same bridge as the VLAN
1916 * members, do not support it (yet) and fallback to software VLAN.
1918 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1923 /* We don't need any dynamic resource from the kernel (yet),
1924 * so skip the prepare phase.
1929 static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
1930 u16 vid, bool untagged)
1932 struct mv88e6xxx_vtu_stu_entry vlan;
1935 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
1939 vlan.data[port] = untagged ?
1940 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1941 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1943 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
1946 static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1947 const struct switchdev_obj_port_vlan *vlan,
1948 struct switchdev_trans *trans)
1950 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1951 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1952 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1955 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1958 mutex_lock(&chip->reg_lock);
1960 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
1961 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
1962 netdev_err(ds->ports[port].netdev,
1963 "failed to add VLAN %d%c\n",
1964 vid, untagged ? 'u' : 't');
1966 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
1967 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
1970 mutex_unlock(&chip->reg_lock);
1973 static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
1976 struct dsa_switch *ds = chip->ds;
1977 struct mv88e6xxx_vtu_stu_entry vlan;
1980 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
1984 /* Tell switchdev if this VLAN is handled in software */
1985 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1988 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1990 /* keep the VLAN unless all ports are excluded */
1992 for (i = 0; i < chip->info->num_ports; ++i) {
1993 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1996 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
2002 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2006 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
2009 static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2010 const struct switchdev_obj_port_vlan *vlan)
2012 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2016 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2019 mutex_lock(&chip->reg_lock);
2021 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
2025 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2026 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
2031 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
2038 mutex_unlock(&chip->reg_lock);
2043 static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
2044 const unsigned char *addr)
2048 for (i = 0; i < 3; i++) {
2049 ret = _mv88e6xxx_reg_write(
2050 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2051 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2059 static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
2060 unsigned char *addr)
2064 for (i = 0; i < 3; i++) {
2065 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
2066 GLOBAL_ATU_MAC_01 + i);
2069 addr[i * 2] = ret >> 8;
2070 addr[i * 2 + 1] = ret & 0xff;
2076 static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
2077 struct mv88e6xxx_atu_entry *entry)
2081 ret = _mv88e6xxx_atu_wait(chip);
2085 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
2089 ret = _mv88e6xxx_atu_data_write(chip, entry);
2093 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2096 static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2097 const unsigned char *addr, u16 vid,
2100 struct mv88e6xxx_atu_entry entry = { 0 };
2101 struct mv88e6xxx_vtu_stu_entry vlan;
2104 /* Null VLAN ID corresponds to the port private database */
2106 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
2108 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2112 entry.fid = vlan.fid;
2113 entry.state = state;
2114 ether_addr_copy(entry.mac, addr);
2115 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2116 entry.trunk = false;
2117 entry.portv_trunkid = BIT(port);
2120 return _mv88e6xxx_atu_load(chip, &entry);
2123 static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2124 const struct switchdev_obj_port_fdb *fdb,
2125 struct switchdev_trans *trans)
2127 /* We don't need any dynamic resource from the kernel (yet),
2128 * so skip the prepare phase.
2133 static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2134 const struct switchdev_obj_port_fdb *fdb,
2135 struct switchdev_trans *trans)
2137 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2139 mutex_lock(&chip->reg_lock);
2140 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2141 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2142 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
2143 mutex_unlock(&chip->reg_lock);
2146 static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2147 const struct switchdev_obj_port_fdb *fdb)
2149 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2152 mutex_lock(&chip->reg_lock);
2153 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2154 GLOBAL_ATU_DATA_STATE_UNUSED);
2155 mutex_unlock(&chip->reg_lock);
2160 static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2161 struct mv88e6xxx_atu_entry *entry)
2163 struct mv88e6xxx_atu_entry next = { 0 };
2168 ret = _mv88e6xxx_atu_wait(chip);
2172 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2176 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
2180 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
2184 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2185 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2186 unsigned int mask, shift;
2188 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2190 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2191 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2194 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2195 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2198 next.portv_trunkid = (ret & mask) >> shift;
2205 static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2206 u16 fid, u16 vid, int port,
2207 struct switchdev_obj *obj,
2208 int (*cb)(struct switchdev_obj *obj))
2210 struct mv88e6xxx_atu_entry addr = {
2211 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2215 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
2220 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
2224 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2227 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2230 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2231 struct switchdev_obj_port_fdb *fdb;
2233 if (!is_unicast_ether_addr(addr.mac))
2236 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
2238 ether_addr_copy(fdb->addr, addr.mac);
2239 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2240 fdb->ndm_state = NUD_NOARP;
2242 fdb->ndm_state = NUD_REACHABLE;
2243 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2244 struct switchdev_obj_port_mdb *mdb;
2246 if (!is_multicast_ether_addr(addr.mac))
2249 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2251 ether_addr_copy(mdb->addr, addr.mac);
2259 } while (!is_broadcast_ether_addr(addr.mac));
2264 static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2265 struct switchdev_obj *obj,
2266 int (*cb)(struct switchdev_obj *obj))
2268 struct mv88e6xxx_vtu_stu_entry vlan = {
2269 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2274 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2275 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2279 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2283 /* Dump VLANs' Filtering Information Databases */
2284 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2289 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2296 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2300 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2305 static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2306 struct switchdev_obj_port_fdb *fdb,
2307 int (*cb)(struct switchdev_obj *obj))
2309 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2312 mutex_lock(&chip->reg_lock);
2313 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
2314 mutex_unlock(&chip->reg_lock);
2319 static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2320 struct net_device *bridge)
2322 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2325 mutex_lock(&chip->reg_lock);
2327 /* Assign the bridge and remap each port's VLANTable */
2328 chip->ports[port].bridge_dev = bridge;
2330 for (i = 0; i < chip->info->num_ports; ++i) {
2331 if (chip->ports[i].bridge_dev == bridge) {
2332 err = _mv88e6xxx_port_based_vlan_map(chip, i);
2338 mutex_unlock(&chip->reg_lock);
2343 static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2345 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2346 struct net_device *bridge = chip->ports[port].bridge_dev;
2349 mutex_lock(&chip->reg_lock);
2351 /* Unassign the bridge and remap each port's VLANTable */
2352 chip->ports[port].bridge_dev = NULL;
2354 for (i = 0; i < chip->info->num_ports; ++i)
2355 if (i == port || chip->ports[i].bridge_dev == bridge)
2356 if (_mv88e6xxx_port_based_vlan_map(chip, i))
2357 netdev_warn(ds->ports[i].netdev,
2358 "failed to remap\n");
2360 mutex_unlock(&chip->reg_lock);
2363 static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
2365 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
2366 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2367 struct gpio_desc *gpiod = chip->reset;
2368 unsigned long timeout;
2372 /* Set all ports to the disabled state. */
2373 for (i = 0; i < chip->info->num_ports; i++) {
2374 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
2378 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
2384 /* Wait for transmit queues to drain. */
2385 usleep_range(2000, 4000);
2387 /* If there is a gpio connected to the reset pin, toggle it */
2389 gpiod_set_value_cansleep(gpiod, 1);
2390 usleep_range(10000, 20000);
2391 gpiod_set_value_cansleep(gpiod, 0);
2392 usleep_range(10000, 20000);
2395 /* Reset the switch. Keep the PPU active if requested. The PPU
2396 * needs to be active to support indirect phy register access
2397 * through global registers 0x18 and 0x19.
2400 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
2402 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
2406 /* Wait up to one second for reset to complete. */
2407 timeout = jiffies + 1 * HZ;
2408 while (time_before(jiffies, timeout)) {
2409 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
2413 if ((ret & is_reset) == is_reset)
2415 usleep_range(1000, 2000);
2417 if (time_after(jiffies, timeout))
2425 static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
2430 /* Clear Power Down bit */
2431 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2435 if (val & BMCR_PDOWN) {
2437 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
2443 static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port,
2446 int addr = chip->info->port_base_addr + port;
2448 if (port >= chip->info->num_ports)
2451 return mv88e6xxx_read(chip, addr, reg, val);
2454 static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
2456 struct dsa_switch *ds = chip->ds;
2460 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2461 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2462 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2463 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
2464 /* MAC Forcing register: don't force link, speed,
2465 * duplex or flow control state to any particular
2466 * values on physical ports, but force the CPU port
2467 * and all DSA ports to their maximum bandwidth and
2470 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
2471 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2472 reg &= ~PORT_PCS_CTRL_UNFORCED;
2473 reg |= PORT_PCS_CTRL_FORCE_LINK |
2474 PORT_PCS_CTRL_LINK_UP |
2475 PORT_PCS_CTRL_DUPLEX_FULL |
2476 PORT_PCS_CTRL_FORCE_DUPLEX;
2477 if (mv88e6xxx_6065_family(chip))
2478 reg |= PORT_PCS_CTRL_100;
2480 reg |= PORT_PCS_CTRL_1000;
2482 reg |= PORT_PCS_CTRL_UNFORCED;
2485 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2486 PORT_PCS_CTRL, reg);
2491 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2492 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2493 * tunneling, determine priority by looking at 802.1p and IP
2494 * priority fields (IP prio has precedence), and set STP state
2497 * If this is the CPU link, use DSA or EDSA tagging depending
2498 * on which tagging mode was configured.
2500 * If this is a link to another switch, use DSA tagging mode.
2502 * If this is the upstream port for this switch, enable
2503 * forwarding of unknown unicasts and multicasts.
2506 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2507 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2508 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2509 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
2510 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2511 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2512 PORT_CONTROL_STATE_FORWARDING;
2513 if (dsa_is_cpu_port(ds, port)) {
2514 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
2515 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2516 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2518 reg |= PORT_CONTROL_DSA_TAG;
2519 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2520 PORT_CONTROL_FORWARD_UNKNOWN;
2522 if (dsa_is_dsa_port(ds, port)) {
2523 if (mv88e6xxx_6095_family(chip) ||
2524 mv88e6xxx_6185_family(chip))
2525 reg |= PORT_CONTROL_DSA_TAG;
2526 if (mv88e6xxx_6352_family(chip) ||
2527 mv88e6xxx_6351_family(chip) ||
2528 mv88e6xxx_6165_family(chip) ||
2529 mv88e6xxx_6097_family(chip) ||
2530 mv88e6xxx_6320_family(chip)) {
2531 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2534 if (port == dsa_upstream_port(ds))
2535 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2536 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2539 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2545 /* If this port is connected to a SerDes, make sure the SerDes is not
2548 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
2549 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
2552 ret &= PORT_STATUS_CMODE_MASK;
2553 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2554 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2555 (ret == PORT_STATUS_CMODE_SGMII)) {
2556 ret = mv88e6xxx_serdes_power_on(chip);
2562 /* Port Control 2: don't force a good FCS, set the maximum frame size to
2563 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2564 * untagged frames on this port, do a destination address lookup on all
2565 * received packets as usual, disable ARP mirroring and don't send a
2566 * copy of all transmitted/received frames on this port to the CPU.
2569 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2570 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2571 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2572 mv88e6xxx_6185_family(chip))
2573 reg = PORT_CONTROL_2_MAP_DA;
2575 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2576 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
2577 reg |= PORT_CONTROL_2_JUMBO_10240;
2579 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
2580 /* Set the upstream port this port should use */
2581 reg |= dsa_upstream_port(ds);
2582 /* enable forwarding of unknown multicast addresses to
2585 if (port == dsa_upstream_port(ds))
2586 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2589 reg |= PORT_CONTROL_2_8021Q_DISABLED;
2592 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2593 PORT_CONTROL_2, reg);
2598 /* Port Association Vector: when learning source addresses
2599 * of packets, add the address to the address database using
2600 * a port bitmap that has only the bit for this port set and
2601 * the other bits clear.
2604 /* Disable learning for CPU port */
2605 if (dsa_is_cpu_port(ds, port))
2608 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2613 /* Egress rate control 2: disable egress rate control. */
2614 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
2619 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2620 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2621 mv88e6xxx_6320_family(chip)) {
2622 /* Do not limit the period of time that this port can
2623 * be paused for by the remote end or the period of
2624 * time that this port can pause the remote end.
2626 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2627 PORT_PAUSE_CTRL, 0x0000);
2631 /* Port ATU control: disable limiting the number of
2632 * address database entries that this port is allowed
2635 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2636 PORT_ATU_CONTROL, 0x0000);
2637 /* Priority Override: disable DA, SA and VTU priority
2640 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2641 PORT_PRI_OVERRIDE, 0x0000);
2645 /* Port Ethertype: use the Ethertype DSA Ethertype
2648 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
2649 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2650 PORT_ETH_TYPE, ETH_P_EDSA);
2655 /* Tag Remap: use an identity 802.1p prio -> switch
2658 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2659 PORT_TAG_REGMAP_0123, 0x3210);
2663 /* Tag Remap 2: use an identity 802.1p prio -> switch
2666 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2667 PORT_TAG_REGMAP_4567, 0x7654);
2672 /* Rate Control: disable ingress rate limiting. */
2673 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2674 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2675 mv88e6xxx_6320_family(chip)) {
2676 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2677 PORT_RATE_CONTROL, 0x0001);
2680 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
2681 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2682 PORT_RATE_CONTROL, 0x0000);
2687 /* Port Control 1: disable trunking, disable sending
2688 * learning messages to this port.
2690 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2695 /* Port based VLAN map: give each port the same default address
2696 * database, and allow bidirectional communication between the
2697 * CPU and DSA port(s), and the other ports.
2699 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
2703 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
2707 /* Default VLAN ID and priority: don't set a default VLAN
2708 * ID, and set the default packet priority to zero.
2710 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
2718 static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2722 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2723 (addr[0] << 8) | addr[1]);
2727 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2728 (addr[2] << 8) | addr[3]);
2732 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2733 (addr[4] << 8) | addr[5]);
2736 static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2739 const unsigned int coeff = chip->info->age_time_coeff;
2740 const unsigned int min = 0x01 * coeff;
2741 const unsigned int max = 0xff * coeff;
2746 if (msecs < min || msecs > max)
2749 /* Round to nearest multiple of coeff */
2750 age_time = (msecs + coeff / 2) / coeff;
2752 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2756 /* AgeTime is 11:4 bits */
2758 val |= age_time << 4;
2760 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
2763 static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2764 unsigned int ageing_time)
2766 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2769 mutex_lock(&chip->reg_lock);
2770 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2771 mutex_unlock(&chip->reg_lock);
2776 static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
2778 struct dsa_switch *ds = chip->ds;
2779 u32 upstream_port = dsa_upstream_port(ds);
2783 /* Enable the PHY Polling Unit if present, don't discard any packets,
2784 * and mask all interrupt sources.
2787 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2788 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
2789 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2791 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
2795 /* Configure the upstream port, and configure it as the port to which
2796 * ingress and egress and ARP monitor frames are to be sent.
2798 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2799 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2800 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2801 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
2806 /* Disable remote management, and set the switch's DSA device number. */
2807 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
2808 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2809 (ds->index & 0x1f));
2813 /* Clear all the VTU and STU entries */
2814 err = _mv88e6xxx_vtu_stu_flush(chip);
2818 /* Set the default address aging time to 5 minutes, and
2819 * enable address learn messages to be sent to all message
2822 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2823 GLOBAL_ATU_CONTROL_LEARN2ALL);
2827 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2831 /* Clear all ATU entries */
2832 err = _mv88e6xxx_atu_flush(chip, 0, true);
2836 /* Configure the IP ToS mapping registers. */
2837 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2840 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2843 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2846 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2849 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2852 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2855 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2858 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2862 /* Configure the IEEE 802.1p priority mapping register. */
2863 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
2867 /* Clear the statistics counters for all ports */
2868 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
2869 GLOBAL_STATS_OP_FLUSH_ALL);
2873 /* Wait for the flush to complete. */
2874 err = _mv88e6xxx_stats_wait(chip);
2881 static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
2882 int target, int port)
2884 u16 val = (target << 8) | (port & 0xf);
2886 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
2889 static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
2894 /* Initialize the routing port to the 32 possible target devices */
2895 for (target = 0; target < 32; ++target) {
2898 if (target < DSA_MAX_SWITCHES) {
2899 port = chip->ds->rtable[target];
2900 if (port == DSA_RTABLE_NONE)
2904 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
2912 static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
2913 bool hask, u16 mask)
2915 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2916 u16 val = (num << 12) | (mask & port_mask);
2919 val |= GLOBAL2_TRUNK_MASK_HASK;
2921 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
2924 static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
2927 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2928 u16 val = (id << 11) | (map & port_mask);
2930 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
2933 static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
2935 const u16 port_mask = BIT(chip->info->num_ports) - 1;
2938 /* Clear all eight possible Trunk Mask vectors */
2939 for (i = 0; i < 8; ++i) {
2940 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
2945 /* Clear all sixteen possible Trunk ID routing vectors */
2946 for (i = 0; i < 16; ++i) {
2947 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
2955 static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
2959 /* Init all Ingress Rate Limit resources of all ports */
2960 for (port = 0; port < chip->info->num_ports; ++port) {
2961 /* XXX newer chips (like 88E6390) have different 2-bit ops */
2962 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2963 GLOBAL2_IRL_CMD_OP_INIT_ALL |
2968 /* Wait for the operation to complete */
2969 err = mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
2970 GLOBAL2_IRL_CMD_BUSY);
2978 /* Indirect write to the Switch MAC/WoL/WoF register */
2979 static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
2980 unsigned int pointer, u8 data)
2982 u16 val = (pointer << 8) | data;
2984 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
2987 static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2991 for (i = 0; i < 6; i++) {
2992 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
3000 static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
3003 u16 val = (pointer << 8) | (data & 0x7);
3005 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
3008 static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
3012 /* Clear all sixteen possible Priority Override entries */
3013 for (i = 0; i < 16; i++) {
3014 err = mv88e6xxx_g2_pot_write(chip, i, 0);
3022 static int mv88e6xxx_g2_eeprom_wait(struct mv88e6xxx_chip *chip)
3024 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD,
3025 GLOBAL2_EEPROM_CMD_BUSY |
3026 GLOBAL2_EEPROM_CMD_RUNNING);
3029 static int mv88e6xxx_g2_eeprom_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3033 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, cmd);
3037 return mv88e6xxx_g2_eeprom_wait(chip);
3040 static int mv88e6xxx_g2_eeprom_read16(struct mv88e6xxx_chip *chip,
3043 u16 cmd = GLOBAL2_EEPROM_CMD_OP_READ | addr;
3046 err = mv88e6xxx_g2_eeprom_wait(chip);
3050 err = mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3054 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3057 static int mv88e6xxx_g2_eeprom_write16(struct mv88e6xxx_chip *chip,
3060 u16 cmd = GLOBAL2_EEPROM_CMD_OP_WRITE | addr;
3063 err = mv88e6xxx_g2_eeprom_wait(chip);
3067 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
3071 return mv88e6xxx_g2_eeprom_cmd(chip, cmd);
3074 static int mv88e6xxx_g2_smi_phy_wait(struct mv88e6xxx_chip *chip)
3076 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD,
3077 GLOBAL2_SMI_PHY_CMD_BUSY);
3080 static int mv88e6xxx_g2_smi_phy_cmd(struct mv88e6xxx_chip *chip, u16 cmd)
3084 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_CMD, cmd);
3088 return mv88e6xxx_g2_smi_phy_wait(chip);
3091 static int mv88e6xxx_g2_smi_phy_read(struct mv88e6xxx_chip *chip, int addr,
3094 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_READ_DATA | (addr << 5) | reg;
3097 err = mv88e6xxx_g2_smi_phy_wait(chip);
3101 err = mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3105 return mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3108 static int mv88e6xxx_g2_smi_phy_write(struct mv88e6xxx_chip *chip, int addr,
3111 u16 cmd = GLOBAL2_SMI_PHY_CMD_OP_22_WRITE_DATA | (addr << 5) | reg;
3114 err = mv88e6xxx_g2_smi_phy_wait(chip);
3118 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SMI_PHY_DATA, val);
3122 return mv88e6xxx_g2_smi_phy_cmd(chip, cmd);
3125 static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3126 .read = mv88e6xxx_g2_smi_phy_read,
3127 .write = mv88e6xxx_g2_smi_phy_write,
3130 static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3135 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3136 /* Consider the frames with reserved multicast destination
3137 * addresses matching 01:80:c2:00:00:2x as MGMT.
3139 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3145 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3146 /* Consider the frames with reserved multicast destination
3147 * addresses matching 01:80:c2:00:00:0x as MGMT.
3149 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3155 /* Ignore removed tag data on doubly tagged packets, disable
3156 * flow control messages, force flow control priority to the
3157 * highest, and send all special multicast frames to the CPU
3158 * port at the highest priority.
3160 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3161 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3162 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3163 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3164 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
3168 /* Program the DSA routing table. */
3169 err = mv88e6xxx_g2_set_device_mapping(chip);
3173 /* Clear all trunk masks and mapping. */
3174 err = mv88e6xxx_g2_clear_trunk(chip);
3178 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3179 /* Disable ingress rate limiting by resetting all per port
3180 * ingress rate limit resources to their initial state.
3182 err = mv88e6xxx_g2_clear_irl(chip);
3187 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3188 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3189 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3190 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3195 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
3196 /* Clear the priority override table. */
3197 err = mv88e6xxx_g2_clear_pot(chip);
3205 static int mv88e6xxx_setup(struct dsa_switch *ds)
3207 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3212 ds->slave_mii_bus = chip->mdio_bus;
3214 mutex_lock(&chip->reg_lock);
3216 err = mv88e6xxx_switch_reset(chip);
3220 /* Setup Switch Port Registers */
3221 for (i = 0; i < chip->info->num_ports; i++) {
3222 err = mv88e6xxx_setup_port(chip, i);
3227 /* Setup Switch Global 1 Registers */
3228 err = mv88e6xxx_g1_setup(chip);
3232 /* Setup Switch Global 2 Registers */
3233 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3234 err = mv88e6xxx_g2_setup(chip);
3240 mutex_unlock(&chip->reg_lock);
3245 static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3247 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3250 mutex_lock(&chip->reg_lock);
3252 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3253 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3254 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3256 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3258 mutex_unlock(&chip->reg_lock);
3263 static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
3265 struct mv88e6xxx_chip *chip = bus->priv;
3269 if (phy >= chip->info->num_ports)
3272 mutex_lock(&chip->reg_lock);
3273 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
3274 mutex_unlock(&chip->reg_lock);
3276 return err ? err : val;
3279 static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
3281 struct mv88e6xxx_chip *chip = bus->priv;
3284 if (phy >= chip->info->num_ports)
3287 mutex_lock(&chip->reg_lock);
3288 err = mv88e6xxx_phy_write(chip, phy, reg, val);
3289 mutex_unlock(&chip->reg_lock);
3294 static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
3295 struct device_node *np)
3298 struct mii_bus *bus;
3302 chip->mdio_np = of_get_child_by_name(np, "mdio");
3304 bus = devm_mdiobus_alloc(chip->dev);
3308 bus->priv = (void *)chip;
3310 bus->name = np->full_name;
3311 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3313 bus->name = "mv88e6xxx SMI";
3314 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3317 bus->read = mv88e6xxx_mdio_read;
3318 bus->write = mv88e6xxx_mdio_write;
3319 bus->parent = chip->dev;
3322 err = of_mdiobus_register(bus, chip->mdio_np);
3324 err = mdiobus_register(bus);
3326 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
3329 chip->mdio_bus = bus;
3335 of_node_put(chip->mdio_np);
3340 static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
3343 struct mii_bus *bus = chip->mdio_bus;
3345 mdiobus_unregister(bus);
3348 of_node_put(chip->mdio_np);
3351 #ifdef CONFIG_NET_DSA_HWMON
3353 static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3355 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3361 mutex_lock(&chip->reg_lock);
3363 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
3367 /* Enable temperature sensor */
3368 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3372 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
3376 /* Wait for temperature to stabilize */
3377 usleep_range(10000, 12000);
3379 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3383 /* Disable temperature sensor */
3384 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
3388 *temp = ((val & 0x1f) - 5) * 5;
3391 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
3392 mutex_unlock(&chip->reg_lock);
3396 static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3398 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3399 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3405 mutex_lock(&chip->reg_lock);
3406 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3407 mutex_unlock(&chip->reg_lock);
3411 *temp = (val & 0xff) - 25;
3416 static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3418 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3420 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
3423 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
3424 return mv88e63xx_get_temp(ds, temp);
3426 return mv88e61xx_get_temp(ds, temp);
3429 static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3431 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3432 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3436 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3441 mutex_lock(&chip->reg_lock);
3442 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3443 mutex_unlock(&chip->reg_lock);
3447 *temp = (((val >> 8) & 0x1f) * 5) - 25;
3452 static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3454 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3455 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3459 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3462 mutex_lock(&chip->reg_lock);
3463 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3466 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3467 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3468 (val & 0xe0ff) | (temp << 8));
3470 mutex_unlock(&chip->reg_lock);
3475 static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3477 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3478 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3482 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3487 mutex_lock(&chip->reg_lock);
3488 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3489 mutex_unlock(&chip->reg_lock);
3493 *alarm = !!(val & 0x40);
3497 #endif /* CONFIG_NET_DSA_HWMON */
3499 static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3501 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3503 return chip->eeprom_len;
3506 static int mv88e6xxx_get_eeprom16(struct mv88e6xxx_chip *chip,
3507 struct ethtool_eeprom *eeprom, u8 *data)
3509 unsigned int offset = eeprom->offset;
3510 unsigned int len = eeprom->len;
3517 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3521 *data++ = (val >> 8) & 0xff;
3529 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3533 *data++ = val & 0xff;
3534 *data++ = (val >> 8) & 0xff;
3542 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3546 *data++ = val & 0xff;
3556 static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3557 struct ethtool_eeprom *eeprom, u8 *data)
3559 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3562 mutex_lock(&chip->reg_lock);
3564 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3565 err = mv88e6xxx_get_eeprom16(chip, eeprom, data);
3569 mutex_unlock(&chip->reg_lock);
3574 eeprom->magic = 0xc3ec4951;
3579 static int mv88e6xxx_set_eeprom16(struct mv88e6xxx_chip *chip,
3580 struct ethtool_eeprom *eeprom, u8 *data)
3582 unsigned int offset = eeprom->offset;
3583 unsigned int len = eeprom->len;
3587 /* Ensure the RO WriteEn bit is set */
3588 err = mv88e6xxx_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_CMD, &val);
3592 if (!(val & GLOBAL2_EEPROM_CMD_WRITE_EN))
3598 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3602 val = (*data++ << 8) | (val & 0xff);
3604 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3615 val |= *data++ << 8;
3617 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3627 err = mv88e6xxx_g2_eeprom_read16(chip, offset >> 1, &val);
3631 val = (val & 0xff00) | *data++;
3633 err = mv88e6xxx_g2_eeprom_write16(chip, offset >> 1, val);
3645 static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3646 struct ethtool_eeprom *eeprom, u8 *data)
3648 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3651 if (eeprom->magic != 0xc3ec4951)
3654 mutex_lock(&chip->reg_lock);
3656 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3657 err = mv88e6xxx_set_eeprom16(chip, eeprom, data);
3661 mutex_unlock(&chip->reg_lock);
3666 static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3668 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3669 .family = MV88E6XXX_FAMILY_6097,
3670 .name = "Marvell 88E6085",
3671 .num_databases = 4096,
3673 .port_base_addr = 0x10,
3674 .age_time_coeff = 15000,
3675 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3679 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3680 .family = MV88E6XXX_FAMILY_6095,
3681 .name = "Marvell 88E6095/88E6095F",
3682 .num_databases = 256,
3684 .port_base_addr = 0x10,
3685 .age_time_coeff = 15000,
3686 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3690 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3691 .family = MV88E6XXX_FAMILY_6165,
3692 .name = "Marvell 88E6123",
3693 .num_databases = 4096,
3695 .port_base_addr = 0x10,
3696 .age_time_coeff = 15000,
3697 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3701 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3702 .family = MV88E6XXX_FAMILY_6185,
3703 .name = "Marvell 88E6131",
3704 .num_databases = 256,
3706 .port_base_addr = 0x10,
3707 .age_time_coeff = 15000,
3708 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3712 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3713 .family = MV88E6XXX_FAMILY_6165,
3714 .name = "Marvell 88E6161",
3715 .num_databases = 4096,
3717 .port_base_addr = 0x10,
3718 .age_time_coeff = 15000,
3719 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3723 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3724 .family = MV88E6XXX_FAMILY_6165,
3725 .name = "Marvell 88E6165",
3726 .num_databases = 4096,
3728 .port_base_addr = 0x10,
3729 .age_time_coeff = 15000,
3730 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3734 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3735 .family = MV88E6XXX_FAMILY_6351,
3736 .name = "Marvell 88E6171",
3737 .num_databases = 4096,
3739 .port_base_addr = 0x10,
3740 .age_time_coeff = 15000,
3741 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3745 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3746 .family = MV88E6XXX_FAMILY_6352,
3747 .name = "Marvell 88E6172",
3748 .num_databases = 4096,
3750 .port_base_addr = 0x10,
3751 .age_time_coeff = 15000,
3752 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3756 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3757 .family = MV88E6XXX_FAMILY_6351,
3758 .name = "Marvell 88E6175",
3759 .num_databases = 4096,
3761 .port_base_addr = 0x10,
3762 .age_time_coeff = 15000,
3763 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3767 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3768 .family = MV88E6XXX_FAMILY_6352,
3769 .name = "Marvell 88E6176",
3770 .num_databases = 4096,
3772 .port_base_addr = 0x10,
3773 .age_time_coeff = 15000,
3774 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3778 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3779 .family = MV88E6XXX_FAMILY_6185,
3780 .name = "Marvell 88E6185",
3781 .num_databases = 256,
3783 .port_base_addr = 0x10,
3784 .age_time_coeff = 15000,
3785 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3789 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3790 .family = MV88E6XXX_FAMILY_6352,
3791 .name = "Marvell 88E6240",
3792 .num_databases = 4096,
3794 .port_base_addr = 0x10,
3795 .age_time_coeff = 15000,
3796 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3800 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3801 .family = MV88E6XXX_FAMILY_6320,
3802 .name = "Marvell 88E6320",
3803 .num_databases = 4096,
3805 .port_base_addr = 0x10,
3806 .age_time_coeff = 15000,
3807 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3811 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3812 .family = MV88E6XXX_FAMILY_6320,
3813 .name = "Marvell 88E6321",
3814 .num_databases = 4096,
3816 .port_base_addr = 0x10,
3817 .age_time_coeff = 15000,
3818 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3822 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3823 .family = MV88E6XXX_FAMILY_6351,
3824 .name = "Marvell 88E6350",
3825 .num_databases = 4096,
3827 .port_base_addr = 0x10,
3828 .age_time_coeff = 15000,
3829 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3833 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3834 .family = MV88E6XXX_FAMILY_6351,
3835 .name = "Marvell 88E6351",
3836 .num_databases = 4096,
3838 .port_base_addr = 0x10,
3839 .age_time_coeff = 15000,
3840 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3844 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3845 .family = MV88E6XXX_FAMILY_6352,
3846 .name = "Marvell 88E6352",
3847 .num_databases = 4096,
3849 .port_base_addr = 0x10,
3850 .age_time_coeff = 15000,
3851 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3855 static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
3859 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3860 if (mv88e6xxx_table[i].prod_num == prod_num)
3861 return &mv88e6xxx_table[i];
3866 static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
3868 const struct mv88e6xxx_info *info;
3869 unsigned int prod_num, rev;
3873 mutex_lock(&chip->reg_lock);
3874 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3875 mutex_unlock(&chip->reg_lock);
3879 prod_num = (id & 0xfff0) >> 4;
3882 info = mv88e6xxx_lookup_info(prod_num);
3886 /* Update the compatible info with the probed one */
3889 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3890 chip->info->prod_num, chip->info->name, rev);
3895 static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
3897 struct mv88e6xxx_chip *chip;
3899 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3905 mutex_init(&chip->reg_lock);
3910 static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3911 .read = mv88e6xxx_read,
3912 .write = mv88e6xxx_write,
3915 static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3917 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3918 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3919 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3920 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3921 mv88e6xxx_ppu_state_init(chip);
3923 chip->phy_ops = &mv88e6xxx_phy_ops;
3927 static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3929 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3930 mv88e6xxx_ppu_state_destroy(chip);
3934 static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
3935 struct mii_bus *bus, int sw_addr)
3937 /* ADDR[0] pin is unavailable externally and considered zero */
3942 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3943 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
3944 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3949 chip->sw_addr = sw_addr;
3954 static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3956 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3958 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3959 return DSA_TAG_PROTO_EDSA;
3961 return DSA_TAG_PROTO_DSA;
3964 static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3965 struct device *host_dev, int sw_addr,
3968 struct mv88e6xxx_chip *chip;
3969 struct mii_bus *bus;
3972 bus = dsa_host_dev_to_mii_bus(host_dev);
3976 chip = mv88e6xxx_alloc_chip(dsa_dev);
3980 /* Legacy SMI probing will only support chips similar to 88E6085 */
3981 chip->info = &mv88e6xxx_table[MV88E6085];
3983 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
3987 err = mv88e6xxx_detect(chip);
3991 mv88e6xxx_phy_init(chip);
3993 err = mv88e6xxx_mdio_register(chip, NULL);
3999 return chip->info->name;
4001 devm_kfree(dsa_dev, chip);
4006 static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
4007 const struct switchdev_obj_port_mdb *mdb,
4008 struct switchdev_trans *trans)
4010 /* We don't need any dynamic resource from the kernel (yet),
4011 * so skip the prepare phase.
4017 static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
4018 const struct switchdev_obj_port_mdb *mdb,
4019 struct switchdev_trans *trans)
4021 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
4023 mutex_lock(&chip->reg_lock);
4024 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
4025 GLOBAL_ATU_DATA_STATE_MC_STATIC))
4026 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
4027 mutex_unlock(&chip->reg_lock);
4030 static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
4031 const struct switchdev_obj_port_mdb *mdb)
4033 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
4036 mutex_lock(&chip->reg_lock);
4037 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
4038 GLOBAL_ATU_DATA_STATE_UNUSED);
4039 mutex_unlock(&chip->reg_lock);
4044 static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
4045 struct switchdev_obj_port_mdb *mdb,
4046 int (*cb)(struct switchdev_obj *obj))
4048 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
4051 mutex_lock(&chip->reg_lock);
4052 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
4053 mutex_unlock(&chip->reg_lock);
4058 static struct dsa_switch_ops mv88e6xxx_switch_ops = {
4059 .probe = mv88e6xxx_drv_probe,
4060 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
4061 .setup = mv88e6xxx_setup,
4062 .set_addr = mv88e6xxx_set_addr,
4063 .adjust_link = mv88e6xxx_adjust_link,
4064 .get_strings = mv88e6xxx_get_strings,
4065 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
4066 .get_sset_count = mv88e6xxx_get_sset_count,
4067 .set_eee = mv88e6xxx_set_eee,
4068 .get_eee = mv88e6xxx_get_eee,
4069 #ifdef CONFIG_NET_DSA_HWMON
4070 .get_temp = mv88e6xxx_get_temp,
4071 .get_temp_limit = mv88e6xxx_get_temp_limit,
4072 .set_temp_limit = mv88e6xxx_set_temp_limit,
4073 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
4075 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
4076 .get_eeprom = mv88e6xxx_get_eeprom,
4077 .set_eeprom = mv88e6xxx_set_eeprom,
4078 .get_regs_len = mv88e6xxx_get_regs_len,
4079 .get_regs = mv88e6xxx_get_regs,
4080 .set_ageing_time = mv88e6xxx_set_ageing_time,
4081 .port_bridge_join = mv88e6xxx_port_bridge_join,
4082 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
4083 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
4084 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4085 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4086 .port_vlan_add = mv88e6xxx_port_vlan_add,
4087 .port_vlan_del = mv88e6xxx_port_vlan_del,
4088 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4089 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4090 .port_fdb_add = mv88e6xxx_port_fdb_add,
4091 .port_fdb_del = mv88e6xxx_port_fdb_del,
4092 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
4093 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
4094 .port_mdb_add = mv88e6xxx_port_mdb_add,
4095 .port_mdb_del = mv88e6xxx_port_mdb_del,
4096 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
4099 static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
4100 struct device_node *np)
4102 struct device *dev = chip->dev;
4103 struct dsa_switch *ds;
4105 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4111 ds->ops = &mv88e6xxx_switch_ops;
4113 dev_set_drvdata(dev, ds);
4115 return dsa_register_switch(ds, np);
4118 static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
4120 dsa_unregister_switch(chip->ds);
4123 static int mv88e6xxx_probe(struct mdio_device *mdiodev)
4125 struct device *dev = &mdiodev->dev;
4126 struct device_node *np = dev->of_node;
4127 const struct mv88e6xxx_info *compat_info;
4128 struct mv88e6xxx_chip *chip;
4132 compat_info = of_device_get_match_data(dev);
4136 chip = mv88e6xxx_alloc_chip(dev);
4140 chip->info = compat_info;
4142 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
4146 err = mv88e6xxx_detect(chip);
4150 mv88e6xxx_phy_init(chip);
4152 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4153 if (IS_ERR(chip->reset))
4154 return PTR_ERR(chip->reset);
4156 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
4157 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
4158 chip->eeprom_len = eeprom_len;
4160 err = mv88e6xxx_mdio_register(chip, np);
4164 err = mv88e6xxx_register_switch(chip, np);
4166 mv88e6xxx_mdio_unregister(chip);
4173 static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4175 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
4176 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
4178 mv88e6xxx_phy_destroy(chip);
4179 mv88e6xxx_unregister_switch(chip);
4180 mv88e6xxx_mdio_unregister(chip);
4183 static const struct of_device_id mv88e6xxx_of_match[] = {
4185 .compatible = "marvell,mv88e6085",
4186 .data = &mv88e6xxx_table[MV88E6085],
4191 MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4193 static struct mdio_driver mv88e6xxx_driver = {
4194 .probe = mv88e6xxx_probe,
4195 .remove = mv88e6xxx_remove,
4197 .name = "mv88e6085",
4198 .of_match_table = mv88e6xxx_of_match,
4202 static int __init mv88e6xxx_init(void)
4204 register_switch_driver(&mv88e6xxx_switch_ops);
4205 return mdio_driver_register(&mv88e6xxx_driver);
4207 module_init(mv88e6xxx_init);
4209 static void __exit mv88e6xxx_cleanup(void)
4211 mdio_driver_unregister(&mv88e6xxx_driver);
4212 unregister_switch_driver(&mv88e6xxx_switch_ops);
4214 module_exit(mv88e6xxx_cleanup);
4216 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4217 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4218 MODULE_LICENSE("GPL");