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>
33 #include "mv88e6xxx.h"
37 static void assert_reg_lock(struct mv88e6xxx_chip *chip)
39 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
40 dev_err(chip->dev, "Switch registers lock not held!\n");
45 /* The switch ADDR[4:1] configuration pins define the chip SMI device address
46 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
48 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
49 * is the only device connected to the SMI master. In this mode it responds to
50 * all 32 possible SMI addresses, and thus maps directly the internal devices.
52 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
53 * multiple devices to share the SMI interface. In this mode it responds to only
54 * 2 registers, used to indirectly access the internal SMI devices.
57 static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
58 int addr, int reg, u16 *val)
63 return chip->smi_ops->read(chip, addr, reg, val);
66 static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
67 int addr, int reg, u16 val)
72 return chip->smi_ops->write(chip, addr, reg, val);
75 static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
76 int addr, int reg, u16 *val)
80 ret = mdiobus_read_nested(chip->bus, addr, reg);
89 static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
90 int addr, int reg, u16 val)
94 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
101 static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
102 .read = mv88e6xxx_smi_single_chip_read,
103 .write = mv88e6xxx_smi_single_chip_write,
106 static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
111 for (i = 0; i < 16; i++) {
112 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
116 if ((ret & SMI_CMD_BUSY) == 0)
123 static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
124 int addr, int reg, u16 *val)
128 /* Wait for the bus to become free. */
129 ret = mv88e6xxx_smi_multi_chip_wait(chip);
133 /* Transmit the read command. */
134 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
135 SMI_CMD_OP_22_READ | (addr << 5) | reg);
139 /* Wait for the read command to complete. */
140 ret = mv88e6xxx_smi_multi_chip_wait(chip);
145 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
154 static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
155 int addr, int reg, u16 val)
159 /* Wait for the bus to become free. */
160 ret = mv88e6xxx_smi_multi_chip_wait(chip);
164 /* Transmit the data to write. */
165 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
169 /* Transmit the write command. */
170 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
171 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
175 /* Wait for the write command to complete. */
176 ret = mv88e6xxx_smi_multi_chip_wait(chip);
183 static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
184 .read = mv88e6xxx_smi_multi_chip_read,
185 .write = mv88e6xxx_smi_multi_chip_write,
188 int mv88e6xxx_read(struct mv88e6xxx_chip *chip, int addr, int reg, u16 *val)
192 assert_reg_lock(chip);
194 err = mv88e6xxx_smi_read(chip, addr, reg, val);
198 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
204 int mv88e6xxx_write(struct mv88e6xxx_chip *chip, int addr, int reg, u16 val)
208 assert_reg_lock(chip);
210 err = mv88e6xxx_smi_write(chip, addr, reg, val);
214 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
220 static int mv88e6xxx_port_read(struct mv88e6xxx_chip *chip, int port, int reg,
223 int addr = chip->info->port_base_addr + port;
225 return mv88e6xxx_read(chip, addr, reg, val);
228 static int mv88e6xxx_port_write(struct mv88e6xxx_chip *chip, int port, int reg,
231 int addr = chip->info->port_base_addr + port;
233 return mv88e6xxx_write(chip, addr, reg, val);
236 static int mv88e6xxx_phy_read(struct mv88e6xxx_chip *chip, int phy,
239 int addr = phy; /* PHY devices addresses start at 0x0 */
244 return chip->phy_ops->read(chip, addr, reg, val);
247 static int mv88e6xxx_phy_write(struct mv88e6xxx_chip *chip, int phy,
250 int addr = phy; /* PHY devices addresses start at 0x0 */
255 return chip->phy_ops->write(chip, addr, reg, val);
258 static int mv88e6xxx_phy_page_get(struct mv88e6xxx_chip *chip, int phy, u8 page)
260 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_PHY_PAGE))
263 return mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
266 static void mv88e6xxx_phy_page_put(struct mv88e6xxx_chip *chip, int phy)
270 /* Restore PHY page Copper 0x0 for access via the registered MDIO bus */
271 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, PHY_PAGE_COPPER);
273 dev_err(chip->dev, "failed to restore PHY %d page Copper (%d)\n",
278 static int mv88e6xxx_phy_page_read(struct mv88e6xxx_chip *chip, int phy,
279 u8 page, int reg, u16 *val)
283 /* There is no paging for registers 22 */
287 err = mv88e6xxx_phy_page_get(chip, phy, page);
289 err = mv88e6xxx_phy_read(chip, phy, reg, val);
290 mv88e6xxx_phy_page_put(chip, phy);
296 static int mv88e6xxx_phy_page_write(struct mv88e6xxx_chip *chip, int phy,
297 u8 page, int reg, u16 val)
301 /* There is no paging for registers 22 */
305 err = mv88e6xxx_phy_page_get(chip, phy, page);
307 err = mv88e6xxx_phy_write(chip, phy, PHY_PAGE, page);
308 mv88e6xxx_phy_page_put(chip, phy);
314 static int mv88e6xxx_serdes_read(struct mv88e6xxx_chip *chip, int reg, u16 *val)
316 return mv88e6xxx_phy_page_read(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
320 static int mv88e6xxx_serdes_write(struct mv88e6xxx_chip *chip, int reg, u16 val)
322 return mv88e6xxx_phy_page_write(chip, ADDR_SERDES, SERDES_PAGE_FIBER,
326 int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int addr, int reg, u16 mask)
330 for (i = 0; i < 16; i++) {
334 err = mv88e6xxx_read(chip, addr, reg, &val);
341 usleep_range(1000, 2000);
344 dev_err(chip->dev, "Timeout while waiting for switch\n");
348 /* Indirect write to single pointer-data register with an Update bit */
349 int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg, u16 update)
354 /* Wait until the previous operation is completed */
355 err = mv88e6xxx_wait(chip, addr, reg, BIT(15));
359 /* Set the Update bit to trigger a write operation */
360 val = BIT(15) | update;
362 return mv88e6xxx_write(chip, addr, reg, val);
365 static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
370 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
374 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
375 val & ~GLOBAL_CONTROL_PPU_ENABLE);
379 for (i = 0; i < 16; i++) {
380 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
384 usleep_range(1000, 2000);
385 if ((val & GLOBAL_STATUS_PPU_MASK) != GLOBAL_STATUS_PPU_POLLING)
392 static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
397 err = mv88e6xxx_g1_read(chip, GLOBAL_CONTROL, &val);
401 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL,
402 val | GLOBAL_CONTROL_PPU_ENABLE);
406 for (i = 0; i < 16; i++) {
407 err = mv88e6xxx_g1_read(chip, GLOBAL_STATUS, &val);
411 usleep_range(1000, 2000);
412 if ((val & GLOBAL_STATUS_PPU_MASK) == GLOBAL_STATUS_PPU_POLLING)
419 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
421 struct mv88e6xxx_chip *chip;
423 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
425 mutex_lock(&chip->reg_lock);
427 if (mutex_trylock(&chip->ppu_mutex)) {
428 if (mv88e6xxx_ppu_enable(chip) == 0)
429 chip->ppu_disabled = 0;
430 mutex_unlock(&chip->ppu_mutex);
433 mutex_unlock(&chip->reg_lock);
436 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
438 struct mv88e6xxx_chip *chip = (void *)_ps;
440 schedule_work(&chip->ppu_work);
443 static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
447 mutex_lock(&chip->ppu_mutex);
449 /* If the PHY polling unit is enabled, disable it so that
450 * we can access the PHY registers. If it was already
451 * disabled, cancel the timer that is going to re-enable
454 if (!chip->ppu_disabled) {
455 ret = mv88e6xxx_ppu_disable(chip);
457 mutex_unlock(&chip->ppu_mutex);
460 chip->ppu_disabled = 1;
462 del_timer(&chip->ppu_timer);
469 static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
471 /* Schedule a timer to re-enable the PHY polling unit. */
472 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
473 mutex_unlock(&chip->ppu_mutex);
476 static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
478 mutex_init(&chip->ppu_mutex);
479 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
480 init_timer(&chip->ppu_timer);
481 chip->ppu_timer.data = (unsigned long)chip;
482 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
485 static void mv88e6xxx_ppu_state_destroy(struct mv88e6xxx_chip *chip)
487 del_timer_sync(&chip->ppu_timer);
490 static int mv88e6xxx_phy_ppu_read(struct mv88e6xxx_chip *chip, int addr,
495 err = mv88e6xxx_ppu_access_get(chip);
497 err = mv88e6xxx_read(chip, addr, reg, val);
498 mv88e6xxx_ppu_access_put(chip);
504 static int mv88e6xxx_phy_ppu_write(struct mv88e6xxx_chip *chip, int addr,
509 err = mv88e6xxx_ppu_access_get(chip);
511 err = mv88e6xxx_write(chip, addr, reg, val);
512 mv88e6xxx_ppu_access_put(chip);
518 static const struct mv88e6xxx_ops mv88e6xxx_phy_ppu_ops = {
519 .read = mv88e6xxx_phy_ppu_read,
520 .write = mv88e6xxx_phy_ppu_write,
523 static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
525 return chip->info->family == MV88E6XXX_FAMILY_6065;
528 static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
530 return chip->info->family == MV88E6XXX_FAMILY_6095;
533 static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
535 return chip->info->family == MV88E6XXX_FAMILY_6097;
538 static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
540 return chip->info->family == MV88E6XXX_FAMILY_6165;
543 static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
545 return chip->info->family == MV88E6XXX_FAMILY_6185;
548 static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
550 return chip->info->family == MV88E6XXX_FAMILY_6320;
553 static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
555 return chip->info->family == MV88E6XXX_FAMILY_6351;
558 static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
560 return chip->info->family == MV88E6XXX_FAMILY_6352;
563 static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
565 return chip->info->num_databases;
568 static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
570 /* Does the device have dedicated FID registers for ATU and VTU ops? */
571 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
572 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
578 /* We expect the switch to perform auto negotiation if there is a real
579 * phy. However, in the case of a fixed link phy, we force the port
580 * settings from the fixed link settings.
582 static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
583 struct phy_device *phydev)
585 struct mv88e6xxx_chip *chip = ds->priv;
589 if (!phy_is_pseudo_fixed_link(phydev))
592 mutex_lock(&chip->reg_lock);
594 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, ®);
598 reg &= ~(PORT_PCS_CTRL_LINK_UP |
599 PORT_PCS_CTRL_FORCE_LINK |
600 PORT_PCS_CTRL_DUPLEX_FULL |
601 PORT_PCS_CTRL_FORCE_DUPLEX |
602 PORT_PCS_CTRL_UNFORCED);
604 reg |= PORT_PCS_CTRL_FORCE_LINK;
606 reg |= PORT_PCS_CTRL_LINK_UP;
608 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
611 switch (phydev->speed) {
613 reg |= PORT_PCS_CTRL_1000;
616 reg |= PORT_PCS_CTRL_100;
619 reg |= PORT_PCS_CTRL_10;
622 pr_info("Unknown speed");
626 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
627 if (phydev->duplex == DUPLEX_FULL)
628 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
630 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
631 (port >= chip->info->num_ports - 2)) {
632 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
633 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
634 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
635 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
636 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
637 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
638 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
640 mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
643 mutex_unlock(&chip->reg_lock);
646 static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
651 for (i = 0; i < 10; i++) {
652 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_OP, &val);
653 if ((val & GLOBAL_STATS_OP_BUSY) == 0)
660 static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
664 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
665 port = (port + 1) << 5;
667 /* Snapshot the hardware statistics counters for this port. */
668 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
669 GLOBAL_STATS_OP_CAPTURE_PORT |
670 GLOBAL_STATS_OP_HIST_RX_TX | port);
674 /* Wait for the snapshotting to complete. */
675 return _mv88e6xxx_stats_wait(chip);
678 static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
687 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
688 GLOBAL_STATS_OP_READ_CAPTURED |
689 GLOBAL_STATS_OP_HIST_RX_TX | stat);
693 err = _mv88e6xxx_stats_wait(chip);
697 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_32, ®);
703 err = mv88e6xxx_g1_read(chip, GLOBAL_STATS_COUNTER_01, ®);
710 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
711 { "in_good_octets", 8, 0x00, BANK0, },
712 { "in_bad_octets", 4, 0x02, BANK0, },
713 { "in_unicast", 4, 0x04, BANK0, },
714 { "in_broadcasts", 4, 0x06, BANK0, },
715 { "in_multicasts", 4, 0x07, BANK0, },
716 { "in_pause", 4, 0x16, BANK0, },
717 { "in_undersize", 4, 0x18, BANK0, },
718 { "in_fragments", 4, 0x19, BANK0, },
719 { "in_oversize", 4, 0x1a, BANK0, },
720 { "in_jabber", 4, 0x1b, BANK0, },
721 { "in_rx_error", 4, 0x1c, BANK0, },
722 { "in_fcs_error", 4, 0x1d, BANK0, },
723 { "out_octets", 8, 0x0e, BANK0, },
724 { "out_unicast", 4, 0x10, BANK0, },
725 { "out_broadcasts", 4, 0x13, BANK0, },
726 { "out_multicasts", 4, 0x12, BANK0, },
727 { "out_pause", 4, 0x15, BANK0, },
728 { "excessive", 4, 0x11, BANK0, },
729 { "collisions", 4, 0x1e, BANK0, },
730 { "deferred", 4, 0x05, BANK0, },
731 { "single", 4, 0x14, BANK0, },
732 { "multiple", 4, 0x17, BANK0, },
733 { "out_fcs_error", 4, 0x03, BANK0, },
734 { "late", 4, 0x1f, BANK0, },
735 { "hist_64bytes", 4, 0x08, BANK0, },
736 { "hist_65_127bytes", 4, 0x09, BANK0, },
737 { "hist_128_255bytes", 4, 0x0a, BANK0, },
738 { "hist_256_511bytes", 4, 0x0b, BANK0, },
739 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
740 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
741 { "sw_in_discards", 4, 0x10, PORT, },
742 { "sw_in_filtered", 2, 0x12, PORT, },
743 { "sw_out_filtered", 2, 0x13, PORT, },
744 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
745 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
746 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
747 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
748 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
749 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
750 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
751 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
752 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
753 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
754 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
755 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
756 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
757 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
758 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
759 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
760 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
761 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
762 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
763 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
764 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
765 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
766 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
767 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
768 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
769 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
772 static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
773 struct mv88e6xxx_hw_stat *stat)
775 switch (stat->type) {
779 return mv88e6xxx_6320_family(chip);
781 return mv88e6xxx_6095_family(chip) ||
782 mv88e6xxx_6185_family(chip) ||
783 mv88e6xxx_6097_family(chip) ||
784 mv88e6xxx_6165_family(chip) ||
785 mv88e6xxx_6351_family(chip) ||
786 mv88e6xxx_6352_family(chip);
791 static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
792 struct mv88e6xxx_hw_stat *s,
803 err = mv88e6xxx_port_read(chip, port, s->reg, ®);
808 if (s->sizeof_stat == 4) {
809 err = mv88e6xxx_port_read(chip, port, s->reg + 1, ®);
817 _mv88e6xxx_stats_read(chip, s->reg, &low);
818 if (s->sizeof_stat == 8)
819 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
821 value = (((u64)high) << 16) | low;
825 static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
828 struct mv88e6xxx_chip *chip = ds->priv;
829 struct mv88e6xxx_hw_stat *stat;
832 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
833 stat = &mv88e6xxx_hw_stats[i];
834 if (mv88e6xxx_has_stat(chip, stat)) {
835 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
842 static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
844 struct mv88e6xxx_chip *chip = ds->priv;
845 struct mv88e6xxx_hw_stat *stat;
848 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
849 stat = &mv88e6xxx_hw_stats[i];
850 if (mv88e6xxx_has_stat(chip, stat))
856 static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
859 struct mv88e6xxx_chip *chip = ds->priv;
860 struct mv88e6xxx_hw_stat *stat;
864 mutex_lock(&chip->reg_lock);
866 ret = _mv88e6xxx_stats_snapshot(chip, port);
868 mutex_unlock(&chip->reg_lock);
871 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
872 stat = &mv88e6xxx_hw_stats[i];
873 if (mv88e6xxx_has_stat(chip, stat)) {
874 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
879 mutex_unlock(&chip->reg_lock);
882 static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
884 return 32 * sizeof(u16);
887 static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
888 struct ethtool_regs *regs, void *_p)
890 struct mv88e6xxx_chip *chip = ds->priv;
898 memset(p, 0xff, 32 * sizeof(u16));
900 mutex_lock(&chip->reg_lock);
902 for (i = 0; i < 32; i++) {
904 err = mv88e6xxx_port_read(chip, port, i, ®);
909 mutex_unlock(&chip->reg_lock);
912 static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
914 return mv88e6xxx_g1_wait(chip, GLOBAL_ATU_OP, GLOBAL_ATU_OP_BUSY);
917 static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
918 struct ethtool_eee *e)
920 struct mv88e6xxx_chip *chip = ds->priv;
924 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
927 mutex_lock(&chip->reg_lock);
929 err = mv88e6xxx_phy_read(chip, port, 16, ®);
933 e->eee_enabled = !!(reg & 0x0200);
934 e->tx_lpi_enabled = !!(reg & 0x0100);
936 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, ®);
940 e->eee_active = !!(reg & PORT_STATUS_EEE);
942 mutex_unlock(&chip->reg_lock);
947 static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
948 struct phy_device *phydev, struct ethtool_eee *e)
950 struct mv88e6xxx_chip *chip = ds->priv;
954 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
957 mutex_lock(&chip->reg_lock);
959 err = mv88e6xxx_phy_read(chip, port, 16, ®);
966 if (e->tx_lpi_enabled)
969 err = mv88e6xxx_phy_write(chip, port, 16, reg);
971 mutex_unlock(&chip->reg_lock);
976 static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
981 if (mv88e6xxx_has_fid_reg(chip)) {
982 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_FID, fid);
985 } else if (mv88e6xxx_num_databases(chip) == 256) {
986 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
987 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
991 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
992 (val & 0xfff) | ((fid << 8) & 0xf000));
996 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1000 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_OP, cmd);
1004 return _mv88e6xxx_atu_wait(chip);
1007 static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
1008 struct mv88e6xxx_atu_entry *entry)
1010 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1012 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1013 unsigned int mask, shift;
1016 data |= GLOBAL_ATU_DATA_TRUNK;
1017 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1018 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1020 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1021 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1024 data |= (entry->portv_trunkid << shift) & mask;
1027 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_DATA, data);
1030 static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
1031 struct mv88e6xxx_atu_entry *entry,
1037 err = _mv88e6xxx_atu_wait(chip);
1041 err = _mv88e6xxx_atu_data_write(chip, entry);
1046 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1047 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1049 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1050 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1053 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
1056 static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
1057 u16 fid, bool static_too)
1059 struct mv88e6xxx_atu_entry entry = {
1061 .state = 0, /* EntryState bits must be 0 */
1064 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1067 static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
1068 int from_port, int to_port, bool static_too)
1070 struct mv88e6xxx_atu_entry entry = {
1075 /* EntryState bits must be 0xF */
1076 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1078 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1079 entry.portv_trunkid = (to_port & 0x0f) << 4;
1080 entry.portv_trunkid |= from_port & 0x0f;
1082 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1085 static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
1086 int port, bool static_too)
1088 /* Destination port 0xF means remove the entries */
1089 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
1092 static const char * const mv88e6xxx_port_state_names[] = {
1093 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1094 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1095 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1096 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1099 static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
1102 struct dsa_switch *ds = chip->ds;
1107 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL, ®);
1111 oldstate = reg & PORT_CONTROL_STATE_MASK;
1113 reg &= ~PORT_CONTROL_STATE_MASK;
1116 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
1120 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1121 mv88e6xxx_port_state_names[state],
1122 mv88e6xxx_port_state_names[oldstate]);
1127 static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
1129 struct net_device *bridge = chip->ports[port].bridge_dev;
1130 const u16 mask = (1 << chip->info->num_ports) - 1;
1131 struct dsa_switch *ds = chip->ds;
1132 u16 output_ports = 0;
1137 /* allow CPU port or DSA link(s) to send frames to every port */
1138 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1139 output_ports = mask;
1141 for (i = 0; i < chip->info->num_ports; ++i) {
1142 /* allow sending frames to every group member */
1143 if (bridge && chip->ports[i].bridge_dev == bridge)
1144 output_ports |= BIT(i);
1146 /* allow sending frames to CPU port and DSA link(s) */
1147 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1148 output_ports |= BIT(i);
1152 /* prevent frames from going back out of the port they came in on */
1153 output_ports &= ~BIT(port);
1155 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, ®);
1160 reg |= output_ports & mask;
1162 return mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
1165 static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1168 struct mv88e6xxx_chip *chip = ds->priv;
1173 case BR_STATE_DISABLED:
1174 stp_state = PORT_CONTROL_STATE_DISABLED;
1176 case BR_STATE_BLOCKING:
1177 case BR_STATE_LISTENING:
1178 stp_state = PORT_CONTROL_STATE_BLOCKING;
1180 case BR_STATE_LEARNING:
1181 stp_state = PORT_CONTROL_STATE_LEARNING;
1183 case BR_STATE_FORWARDING:
1185 stp_state = PORT_CONTROL_STATE_FORWARDING;
1189 mutex_lock(&chip->reg_lock);
1190 err = _mv88e6xxx_port_state(chip, port, stp_state);
1191 mutex_unlock(&chip->reg_lock);
1194 netdev_err(ds->ports[port].netdev,
1195 "failed to update state to %s\n",
1196 mv88e6xxx_port_state_names[stp_state]);
1199 static void mv88e6xxx_port_fast_age(struct dsa_switch *ds, int port)
1201 struct mv88e6xxx_chip *chip = ds->priv;
1204 mutex_lock(&chip->reg_lock);
1205 err = _mv88e6xxx_atu_remove(chip, 0, port, false);
1206 mutex_unlock(&chip->reg_lock);
1209 netdev_err(ds->ports[port].netdev, "failed to flush ATU\n");
1212 static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
1215 struct dsa_switch *ds = chip->ds;
1219 err = mv88e6xxx_port_read(chip, port, PORT_DEFAULT_VLAN, ®);
1223 pvid = reg & PORT_DEFAULT_VLAN_MASK;
1226 reg &= ~PORT_DEFAULT_VLAN_MASK;
1227 reg |= *new & PORT_DEFAULT_VLAN_MASK;
1229 err = mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, reg);
1233 netdev_dbg(ds->ports[port].netdev,
1234 "DefaultVID %d (was %d)\n", *new, pvid);
1243 static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
1244 int port, u16 *pvid)
1246 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
1249 static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
1252 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
1255 static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
1257 return mv88e6xxx_g1_wait(chip, GLOBAL_VTU_OP, GLOBAL_VTU_OP_BUSY);
1260 static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
1264 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_OP, op);
1268 return _mv88e6xxx_vtu_wait(chip);
1271 static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
1275 ret = _mv88e6xxx_vtu_wait(chip);
1279 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
1282 static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
1283 struct mv88e6xxx_vtu_stu_entry *entry,
1284 unsigned int nibble_offset)
1289 for (i = 0; i < 3; ++i) {
1290 u16 *reg = ®s[i];
1292 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1297 for (i = 0; i < chip->info->num_ports; ++i) {
1298 unsigned int shift = (i % 4) * 4 + nibble_offset;
1299 u16 reg = regs[i / 4];
1301 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1307 static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
1308 struct mv88e6xxx_vtu_stu_entry *entry)
1310 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
1313 static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
1314 struct mv88e6xxx_vtu_stu_entry *entry)
1316 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
1319 static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
1320 struct mv88e6xxx_vtu_stu_entry *entry,
1321 unsigned int nibble_offset)
1323 u16 regs[3] = { 0 };
1326 for (i = 0; i < chip->info->num_ports; ++i) {
1327 unsigned int shift = (i % 4) * 4 + nibble_offset;
1328 u8 data = entry->data[i];
1330 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1333 for (i = 0; i < 3; ++i) {
1336 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_DATA_0_3 + i, reg);
1344 static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
1345 struct mv88e6xxx_vtu_stu_entry *entry)
1347 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
1350 static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
1351 struct mv88e6xxx_vtu_stu_entry *entry)
1353 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
1356 static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
1358 return mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID,
1359 vid & GLOBAL_VTU_VID_MASK);
1362 static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
1363 struct mv88e6xxx_vtu_stu_entry *entry)
1365 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1369 err = _mv88e6xxx_vtu_wait(chip);
1373 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1377 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1381 next.vid = val & GLOBAL_VTU_VID_MASK;
1382 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
1385 err = mv88e6xxx_vtu_data_read(chip, &next);
1389 if (mv88e6xxx_has_fid_reg(chip)) {
1390 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_FID, &val);
1394 next.fid = val & GLOBAL_VTU_FID_MASK;
1395 } else if (mv88e6xxx_num_databases(chip) == 256) {
1396 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1397 * VTU DBNum[3:0] are located in VTU Operation 3:0
1399 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_OP, &val);
1403 next.fid = (val & 0xf00) >> 4;
1404 next.fid |= val & 0xf;
1407 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1408 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1412 next.sid = val & GLOBAL_VTU_SID_MASK;
1420 static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1421 struct switchdev_obj_port_vlan *vlan,
1422 int (*cb)(struct switchdev_obj *obj))
1424 struct mv88e6xxx_chip *chip = ds->priv;
1425 struct mv88e6xxx_vtu_stu_entry next;
1429 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1432 mutex_lock(&chip->reg_lock);
1434 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
1438 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1443 err = _mv88e6xxx_vtu_getnext(chip, &next);
1450 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1453 /* reinit and dump this VLAN obj */
1454 vlan->vid_begin = next.vid;
1455 vlan->vid_end = next.vid;
1458 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1459 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1461 if (next.vid == pvid)
1462 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1464 err = cb(&vlan->obj);
1467 } while (next.vid < GLOBAL_VTU_VID_MASK);
1470 mutex_unlock(&chip->reg_lock);
1475 static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
1476 struct mv88e6xxx_vtu_stu_entry *entry)
1478 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1482 err = _mv88e6xxx_vtu_wait(chip);
1489 /* Write port member tags */
1490 err = mv88e6xxx_vtu_data_write(chip, entry);
1494 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1495 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1496 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1501 if (mv88e6xxx_has_fid_reg(chip)) {
1502 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1503 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_FID, reg);
1506 } else if (mv88e6xxx_num_databases(chip) == 256) {
1507 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1508 * VTU DBNum[3:0] are located in VTU Operation 3:0
1510 op |= (entry->fid & 0xf0) << 8;
1511 op |= entry->fid & 0xf;
1514 reg = GLOBAL_VTU_VID_VALID;
1516 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1517 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1521 return _mv88e6xxx_vtu_cmd(chip, op);
1524 static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
1525 struct mv88e6xxx_vtu_stu_entry *entry)
1527 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1531 err = _mv88e6xxx_vtu_wait(chip);
1535 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID,
1536 sid & GLOBAL_VTU_SID_MASK);
1540 err = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1544 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_SID, &val);
1548 next.sid = val & GLOBAL_VTU_SID_MASK;
1550 err = mv88e6xxx_g1_read(chip, GLOBAL_VTU_VID, &val);
1554 next.valid = !!(val & GLOBAL_VTU_VID_VALID);
1557 err = mv88e6xxx_stu_data_read(chip, &next);
1566 static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
1567 struct mv88e6xxx_vtu_stu_entry *entry)
1572 err = _mv88e6xxx_vtu_wait(chip);
1579 /* Write port states */
1580 err = mv88e6xxx_stu_data_write(chip, entry);
1584 reg = GLOBAL_VTU_VID_VALID;
1586 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_VID, reg);
1590 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1591 err = mv88e6xxx_g1_write(chip, GLOBAL_VTU_SID, reg);
1595 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1598 static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
1601 struct dsa_switch *ds = chip->ds;
1607 if (mv88e6xxx_num_databases(chip) == 4096)
1609 else if (mv88e6xxx_num_databases(chip) == 256)
1614 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1615 err = mv88e6xxx_port_read(chip, port, PORT_BASE_VLAN, ®);
1619 fid = (reg & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1622 reg &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1623 reg |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1625 err = mv88e6xxx_port_write(chip, port, PORT_BASE_VLAN, reg);
1630 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1631 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_1, ®);
1635 fid |= (reg & upper_mask) << 4;
1639 reg |= (*new >> 4) & upper_mask;
1641 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, reg);
1645 netdev_dbg(ds->ports[port].netdev,
1646 "FID %d (was %d)\n", *new, fid);
1655 static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
1658 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
1661 static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
1664 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
1667 static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
1669 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1670 struct mv88e6xxx_vtu_stu_entry vlan;
1673 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1675 /* Set every FID bit used by the (un)bridged ports */
1676 for (i = 0; i < chip->info->num_ports; ++i) {
1677 err = _mv88e6xxx_port_fid_get(chip, i, fid);
1681 set_bit(*fid, fid_bitmap);
1684 /* Set every FID bit used by the VLAN entries */
1685 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1690 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1697 set_bit(vlan.fid, fid_bitmap);
1698 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1700 /* The reset value 0x000 is used to indicate that multiple address
1701 * databases are not needed. Return the next positive available.
1703 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1704 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
1707 /* Clear the database */
1708 return _mv88e6xxx_atu_flush(chip, *fid, true);
1711 static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
1712 struct mv88e6xxx_vtu_stu_entry *entry)
1714 struct dsa_switch *ds = chip->ds;
1715 struct mv88e6xxx_vtu_stu_entry vlan = {
1721 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
1725 /* exclude all ports except the CPU and DSA ports */
1726 for (i = 0; i < chip->info->num_ports; ++i)
1727 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1728 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1729 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1731 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1732 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
1733 struct mv88e6xxx_vtu_stu_entry vstp;
1735 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1736 * implemented, only one STU entry is needed to cover all VTU
1737 * entries. Thus, validate the SID 0.
1740 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
1744 if (vstp.sid != vlan.sid || !vstp.valid) {
1745 memset(&vstp, 0, sizeof(vstp));
1747 vstp.sid = vlan.sid;
1749 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
1759 static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
1760 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1767 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
1771 err = _mv88e6xxx_vtu_getnext(chip, entry);
1775 if (entry->vid != vid || !entry->valid) {
1778 /* -ENOENT would've been more appropriate, but switchdev expects
1779 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1782 err = _mv88e6xxx_vtu_new(chip, vid, entry);
1788 static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1789 u16 vid_begin, u16 vid_end)
1791 struct mv88e6xxx_chip *chip = ds->priv;
1792 struct mv88e6xxx_vtu_stu_entry vlan;
1798 mutex_lock(&chip->reg_lock);
1800 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
1805 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1812 if (vlan.vid > vid_end)
1815 for (i = 0; i < chip->info->num_ports; ++i) {
1816 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1820 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1823 if (chip->ports[i].bridge_dev ==
1824 chip->ports[port].bridge_dev)
1825 break; /* same bridge, check next VLAN */
1827 netdev_warn(ds->ports[port].netdev,
1828 "hardware VLAN %d already used by %s\n",
1830 netdev_name(chip->ports[i].bridge_dev));
1834 } while (vlan.vid < vid_end);
1837 mutex_unlock(&chip->reg_lock);
1842 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
1843 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
1844 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
1845 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
1846 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
1849 static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
1850 bool vlan_filtering)
1852 struct mv88e6xxx_chip *chip = ds->priv;
1853 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
1854 PORT_CONTROL_2_8021Q_DISABLED;
1858 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1861 mutex_lock(&chip->reg_lock);
1863 err = mv88e6xxx_port_read(chip, port, PORT_CONTROL_2, ®);
1867 old = reg & PORT_CONTROL_2_8021Q_MASK;
1870 reg &= ~PORT_CONTROL_2_8021Q_MASK;
1871 reg |= new & PORT_CONTROL_2_8021Q_MASK;
1873 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
1877 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
1878 mv88e6xxx_port_8021q_mode_names[new],
1879 mv88e6xxx_port_8021q_mode_names[old]);
1884 mutex_unlock(&chip->reg_lock);
1890 mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
1891 const struct switchdev_obj_port_vlan *vlan,
1892 struct switchdev_trans *trans)
1894 struct mv88e6xxx_chip *chip = ds->priv;
1897 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1900 /* If the requested port doesn't belong to the same bridge as the VLAN
1901 * members, do not support it (yet) and fallback to software VLAN.
1903 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
1908 /* We don't need any dynamic resource from the kernel (yet),
1909 * so skip the prepare phase.
1914 static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
1915 u16 vid, bool untagged)
1917 struct mv88e6xxx_vtu_stu_entry vlan;
1920 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
1924 vlan.data[port] = untagged ?
1925 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
1926 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
1928 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
1931 static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
1932 const struct switchdev_obj_port_vlan *vlan,
1933 struct switchdev_trans *trans)
1935 struct mv88e6xxx_chip *chip = ds->priv;
1936 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1937 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1940 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1943 mutex_lock(&chip->reg_lock);
1945 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
1946 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
1947 netdev_err(ds->ports[port].netdev,
1948 "failed to add VLAN %d%c\n",
1949 vid, untagged ? 'u' : 't');
1951 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
1952 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
1955 mutex_unlock(&chip->reg_lock);
1958 static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
1961 struct dsa_switch *ds = chip->ds;
1962 struct mv88e6xxx_vtu_stu_entry vlan;
1965 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
1969 /* Tell switchdev if this VLAN is handled in software */
1970 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1973 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1975 /* keep the VLAN unless all ports are excluded */
1977 for (i = 0; i < chip->info->num_ports; ++i) {
1978 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1981 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
1987 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
1991 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
1994 static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
1995 const struct switchdev_obj_port_vlan *vlan)
1997 struct mv88e6xxx_chip *chip = ds->priv;
2001 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2004 mutex_lock(&chip->reg_lock);
2006 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
2010 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2011 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
2016 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
2023 mutex_unlock(&chip->reg_lock);
2028 static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
2029 const unsigned char *addr)
2033 for (i = 0; i < 3; i++) {
2034 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_MAC_01 + i,
2035 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2043 static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
2044 unsigned char *addr)
2049 for (i = 0; i < 3; i++) {
2050 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_MAC_01 + i, &val);
2054 addr[i * 2] = val >> 8;
2055 addr[i * 2 + 1] = val & 0xff;
2061 static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
2062 struct mv88e6xxx_atu_entry *entry)
2066 ret = _mv88e6xxx_atu_wait(chip);
2070 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
2074 ret = _mv88e6xxx_atu_data_write(chip, entry);
2078 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2081 static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2082 struct mv88e6xxx_atu_entry *entry);
2084 static int mv88e6xxx_atu_get(struct mv88e6xxx_chip *chip, int fid,
2085 const u8 *addr, struct mv88e6xxx_atu_entry *entry)
2087 struct mv88e6xxx_atu_entry next;
2090 eth_broadcast_addr(next.mac);
2092 err = _mv88e6xxx_atu_mac_write(chip, next.mac);
2097 err = _mv88e6xxx_atu_getnext(chip, fid, &next);
2101 if (next.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2104 if (ether_addr_equal(next.mac, addr)) {
2108 } while (!is_broadcast_ether_addr(next.mac));
2110 memset(entry, 0, sizeof(*entry));
2112 ether_addr_copy(entry->mac, addr);
2117 static int mv88e6xxx_port_db_load_purge(struct mv88e6xxx_chip *chip, int port,
2118 const unsigned char *addr, u16 vid,
2121 struct mv88e6xxx_vtu_stu_entry vlan;
2122 struct mv88e6xxx_atu_entry entry;
2125 /* Null VLAN ID corresponds to the port private database */
2127 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
2129 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2133 err = mv88e6xxx_atu_get(chip, vlan.fid, addr, &entry);
2137 /* Purge the ATU entry only if no port is using it anymore */
2138 if (state == GLOBAL_ATU_DATA_STATE_UNUSED) {
2139 entry.portv_trunkid &= ~BIT(port);
2140 if (!entry.portv_trunkid)
2141 entry.state = GLOBAL_ATU_DATA_STATE_UNUSED;
2143 entry.portv_trunkid |= BIT(port);
2144 entry.state = state;
2147 return _mv88e6xxx_atu_load(chip, &entry);
2150 static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2151 const struct switchdev_obj_port_fdb *fdb,
2152 struct switchdev_trans *trans)
2154 /* We don't need any dynamic resource from the kernel (yet),
2155 * so skip the prepare phase.
2160 static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2161 const struct switchdev_obj_port_fdb *fdb,
2162 struct switchdev_trans *trans)
2164 struct mv88e6xxx_chip *chip = ds->priv;
2166 mutex_lock(&chip->reg_lock);
2167 if (mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2168 GLOBAL_ATU_DATA_STATE_UC_STATIC))
2169 netdev_err(ds->ports[port].netdev, "failed to load unicast MAC address\n");
2170 mutex_unlock(&chip->reg_lock);
2173 static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2174 const struct switchdev_obj_port_fdb *fdb)
2176 struct mv88e6xxx_chip *chip = ds->priv;
2179 mutex_lock(&chip->reg_lock);
2180 err = mv88e6xxx_port_db_load_purge(chip, port, fdb->addr, fdb->vid,
2181 GLOBAL_ATU_DATA_STATE_UNUSED);
2182 mutex_unlock(&chip->reg_lock);
2187 static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2188 struct mv88e6xxx_atu_entry *entry)
2190 struct mv88e6xxx_atu_entry next = { 0 };
2196 err = _mv88e6xxx_atu_wait(chip);
2200 err = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2204 err = _mv88e6xxx_atu_mac_read(chip, next.mac);
2208 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_DATA, &val);
2212 next.state = val & GLOBAL_ATU_DATA_STATE_MASK;
2213 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2214 unsigned int mask, shift;
2216 if (val & GLOBAL_ATU_DATA_TRUNK) {
2218 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2219 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2222 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2223 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2226 next.portv_trunkid = (val & mask) >> shift;
2233 static int mv88e6xxx_port_db_dump_fid(struct mv88e6xxx_chip *chip,
2234 u16 fid, u16 vid, int port,
2235 struct switchdev_obj *obj,
2236 int (*cb)(struct switchdev_obj *obj))
2238 struct mv88e6xxx_atu_entry addr = {
2239 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2243 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
2248 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
2252 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2255 if (addr.trunk || (addr.portv_trunkid & BIT(port)) == 0)
2258 if (obj->id == SWITCHDEV_OBJ_ID_PORT_FDB) {
2259 struct switchdev_obj_port_fdb *fdb;
2261 if (!is_unicast_ether_addr(addr.mac))
2264 fdb = SWITCHDEV_OBJ_PORT_FDB(obj);
2266 ether_addr_copy(fdb->addr, addr.mac);
2267 if (addr.state == GLOBAL_ATU_DATA_STATE_UC_STATIC)
2268 fdb->ndm_state = NUD_NOARP;
2270 fdb->ndm_state = NUD_REACHABLE;
2271 } else if (obj->id == SWITCHDEV_OBJ_ID_PORT_MDB) {
2272 struct switchdev_obj_port_mdb *mdb;
2274 if (!is_multicast_ether_addr(addr.mac))
2277 mdb = SWITCHDEV_OBJ_PORT_MDB(obj);
2279 ether_addr_copy(mdb->addr, addr.mac);
2287 } while (!is_broadcast_ether_addr(addr.mac));
2292 static int mv88e6xxx_port_db_dump(struct mv88e6xxx_chip *chip, int port,
2293 struct switchdev_obj *obj,
2294 int (*cb)(struct switchdev_obj *obj))
2296 struct mv88e6xxx_vtu_stu_entry vlan = {
2297 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2302 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2303 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2307 err = mv88e6xxx_port_db_dump_fid(chip, fid, 0, port, obj, cb);
2311 /* Dump VLANs' Filtering Information Databases */
2312 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2317 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2324 err = mv88e6xxx_port_db_dump_fid(chip, vlan.fid, vlan.vid, port,
2328 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2333 static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2334 struct switchdev_obj_port_fdb *fdb,
2335 int (*cb)(struct switchdev_obj *obj))
2337 struct mv88e6xxx_chip *chip = ds->priv;
2340 mutex_lock(&chip->reg_lock);
2341 err = mv88e6xxx_port_db_dump(chip, port, &fdb->obj, cb);
2342 mutex_unlock(&chip->reg_lock);
2347 static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2348 struct net_device *bridge)
2350 struct mv88e6xxx_chip *chip = ds->priv;
2353 mutex_lock(&chip->reg_lock);
2355 /* Assign the bridge and remap each port's VLANTable */
2356 chip->ports[port].bridge_dev = bridge;
2358 for (i = 0; i < chip->info->num_ports; ++i) {
2359 if (chip->ports[i].bridge_dev == bridge) {
2360 err = _mv88e6xxx_port_based_vlan_map(chip, i);
2366 mutex_unlock(&chip->reg_lock);
2371 static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2373 struct mv88e6xxx_chip *chip = ds->priv;
2374 struct net_device *bridge = chip->ports[port].bridge_dev;
2377 mutex_lock(&chip->reg_lock);
2379 /* Unassign the bridge and remap each port's VLANTable */
2380 chip->ports[port].bridge_dev = NULL;
2382 for (i = 0; i < chip->info->num_ports; ++i)
2383 if (i == port || chip->ports[i].bridge_dev == bridge)
2384 if (_mv88e6xxx_port_based_vlan_map(chip, i))
2385 netdev_warn(ds->ports[i].netdev,
2386 "failed to remap\n");
2388 mutex_unlock(&chip->reg_lock);
2391 static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
2393 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
2394 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2395 struct gpio_desc *gpiod = chip->reset;
2396 unsigned long timeout;
2401 /* Set all ports to the disabled state. */
2402 for (i = 0; i < chip->info->num_ports; i++) {
2403 err = mv88e6xxx_port_read(chip, i, PORT_CONTROL, ®);
2407 err = mv88e6xxx_port_write(chip, i, PORT_CONTROL,
2413 /* Wait for transmit queues to drain. */
2414 usleep_range(2000, 4000);
2416 /* If there is a gpio connected to the reset pin, toggle it */
2418 gpiod_set_value_cansleep(gpiod, 1);
2419 usleep_range(10000, 20000);
2420 gpiod_set_value_cansleep(gpiod, 0);
2421 usleep_range(10000, 20000);
2424 /* Reset the switch. Keep the PPU active if requested. The PPU
2425 * needs to be active to support indirect phy register access
2426 * through global registers 0x18 and 0x19.
2429 err = mv88e6xxx_g1_write(chip, 0x04, 0xc000);
2431 err = mv88e6xxx_g1_write(chip, 0x04, 0xc400);
2435 /* Wait up to one second for reset to complete. */
2436 timeout = jiffies + 1 * HZ;
2437 while (time_before(jiffies, timeout)) {
2438 err = mv88e6xxx_g1_read(chip, 0x00, ®);
2442 if ((reg & is_reset) == is_reset)
2444 usleep_range(1000, 2000);
2446 if (time_after(jiffies, timeout))
2454 static int mv88e6xxx_serdes_power_on(struct mv88e6xxx_chip *chip)
2459 /* Clear Power Down bit */
2460 err = mv88e6xxx_serdes_read(chip, MII_BMCR, &val);
2464 if (val & BMCR_PDOWN) {
2466 err = mv88e6xxx_serdes_write(chip, MII_BMCR, val);
2472 static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
2474 struct dsa_switch *ds = chip->ds;
2478 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2479 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2480 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2481 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
2482 /* MAC Forcing register: don't force link, speed,
2483 * duplex or flow control state to any particular
2484 * values on physical ports, but force the CPU port
2485 * and all DSA ports to their maximum bandwidth and
2488 err = mv88e6xxx_port_read(chip, port, PORT_PCS_CTRL, ®);
2489 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2490 reg &= ~PORT_PCS_CTRL_UNFORCED;
2491 reg |= PORT_PCS_CTRL_FORCE_LINK |
2492 PORT_PCS_CTRL_LINK_UP |
2493 PORT_PCS_CTRL_DUPLEX_FULL |
2494 PORT_PCS_CTRL_FORCE_DUPLEX;
2495 if (mv88e6xxx_6065_family(chip))
2496 reg |= PORT_PCS_CTRL_100;
2498 reg |= PORT_PCS_CTRL_1000;
2500 reg |= PORT_PCS_CTRL_UNFORCED;
2503 err = mv88e6xxx_port_write(chip, port, PORT_PCS_CTRL, reg);
2508 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2509 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2510 * tunneling, determine priority by looking at 802.1p and IP
2511 * priority fields (IP prio has precedence), and set STP state
2514 * If this is the CPU link, use DSA or EDSA tagging depending
2515 * on which tagging mode was configured.
2517 * If this is a link to another switch, use DSA tagging mode.
2519 * If this is the upstream port for this switch, enable
2520 * forwarding of unknown unicasts and multicasts.
2523 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2524 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2525 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2526 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
2527 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2528 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2529 PORT_CONTROL_STATE_FORWARDING;
2530 if (dsa_is_cpu_port(ds, port)) {
2531 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
2532 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2533 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2535 reg |= PORT_CONTROL_DSA_TAG;
2536 reg |= PORT_CONTROL_EGRESS_ADD_TAG |
2537 PORT_CONTROL_FORWARD_UNKNOWN;
2539 if (dsa_is_dsa_port(ds, port)) {
2540 if (mv88e6xxx_6095_family(chip) ||
2541 mv88e6xxx_6185_family(chip))
2542 reg |= PORT_CONTROL_DSA_TAG;
2543 if (mv88e6xxx_6352_family(chip) ||
2544 mv88e6xxx_6351_family(chip) ||
2545 mv88e6xxx_6165_family(chip) ||
2546 mv88e6xxx_6097_family(chip) ||
2547 mv88e6xxx_6320_family(chip)) {
2548 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2551 if (port == dsa_upstream_port(ds))
2552 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2553 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2556 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL, reg);
2561 /* If this port is connected to a SerDes, make sure the SerDes is not
2564 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SERDES)) {
2565 err = mv88e6xxx_port_read(chip, port, PORT_STATUS, ®);
2568 reg &= PORT_STATUS_CMODE_MASK;
2569 if ((reg == PORT_STATUS_CMODE_100BASE_X) ||
2570 (reg == PORT_STATUS_CMODE_1000BASE_X) ||
2571 (reg == PORT_STATUS_CMODE_SGMII)) {
2572 err = mv88e6xxx_serdes_power_on(chip);
2578 /* Port Control 2: don't force a good FCS, set the maximum frame size to
2579 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2580 * untagged frames on this port, do a destination address lookup on all
2581 * received packets as usual, disable ARP mirroring and don't send a
2582 * copy of all transmitted/received frames on this port to the CPU.
2585 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2586 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2587 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2588 mv88e6xxx_6185_family(chip))
2589 reg = PORT_CONTROL_2_MAP_DA;
2591 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2592 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
2593 reg |= PORT_CONTROL_2_JUMBO_10240;
2595 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
2596 /* Set the upstream port this port should use */
2597 reg |= dsa_upstream_port(ds);
2598 /* enable forwarding of unknown multicast addresses to
2601 if (port == dsa_upstream_port(ds))
2602 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2605 reg |= PORT_CONTROL_2_8021Q_DISABLED;
2608 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_2, reg);
2613 /* Port Association Vector: when learning source addresses
2614 * of packets, add the address to the address database using
2615 * a port bitmap that has only the bit for this port set and
2616 * the other bits clear.
2619 /* Disable learning for CPU port */
2620 if (dsa_is_cpu_port(ds, port))
2623 err = mv88e6xxx_port_write(chip, port, PORT_ASSOC_VECTOR, reg);
2627 /* Egress rate control 2: disable egress rate control. */
2628 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL_2, 0x0000);
2632 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2633 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2634 mv88e6xxx_6320_family(chip)) {
2635 /* Do not limit the period of time that this port can
2636 * be paused for by the remote end or the period of
2637 * time that this port can pause the remote end.
2639 err = mv88e6xxx_port_write(chip, port, PORT_PAUSE_CTRL, 0x0000);
2643 /* Port ATU control: disable limiting the number of
2644 * address database entries that this port is allowed
2647 err = mv88e6xxx_port_write(chip, port, PORT_ATU_CONTROL,
2649 /* Priority Override: disable DA, SA and VTU priority
2652 err = mv88e6xxx_port_write(chip, port, PORT_PRI_OVERRIDE,
2657 /* Port Ethertype: use the Ethertype DSA Ethertype
2660 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA)) {
2661 err = mv88e6xxx_port_write(chip, port, PORT_ETH_TYPE,
2667 /* Tag Remap: use an identity 802.1p prio -> switch
2670 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_0123,
2675 /* Tag Remap 2: use an identity 802.1p prio -> switch
2678 err = mv88e6xxx_port_write(chip, port, PORT_TAG_REGMAP_4567,
2684 /* Rate Control: disable ingress rate limiting. */
2685 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2686 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2687 mv88e6xxx_6320_family(chip)) {
2688 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2692 } else if (mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip)) {
2693 err = mv88e6xxx_port_write(chip, port, PORT_RATE_CONTROL,
2699 /* Port Control 1: disable trunking, disable sending
2700 * learning messages to this port.
2702 err = mv88e6xxx_port_write(chip, port, PORT_CONTROL_1, 0x0000);
2706 /* Port based VLAN map: give each port the same default address
2707 * database, and allow bidirectional communication between the
2708 * CPU and DSA port(s), and the other ports.
2710 err = _mv88e6xxx_port_fid_set(chip, port, 0);
2714 err = _mv88e6xxx_port_based_vlan_map(chip, port);
2718 /* Default VLAN ID and priority: don't set a default VLAN
2719 * ID, and set the default packet priority to zero.
2721 return mv88e6xxx_port_write(chip, port, PORT_DEFAULT_VLAN, 0x0000);
2724 int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2728 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_01, (addr[0] << 8) | addr[1]);
2732 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_23, (addr[2] << 8) | addr[3]);
2736 err = mv88e6xxx_g1_write(chip, GLOBAL_MAC_45, (addr[4] << 8) | addr[5]);
2743 static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2746 const unsigned int coeff = chip->info->age_time_coeff;
2747 const unsigned int min = 0x01 * coeff;
2748 const unsigned int max = 0xff * coeff;
2753 if (msecs < min || msecs > max)
2756 /* Round to nearest multiple of coeff */
2757 age_time = (msecs + coeff / 2) / coeff;
2759 err = mv88e6xxx_g1_read(chip, GLOBAL_ATU_CONTROL, &val);
2763 /* AgeTime is 11:4 bits */
2765 val |= age_time << 4;
2767 return mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL, val);
2770 static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
2771 unsigned int ageing_time)
2773 struct mv88e6xxx_chip *chip = ds->priv;
2776 mutex_lock(&chip->reg_lock);
2777 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
2778 mutex_unlock(&chip->reg_lock);
2783 static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
2785 struct dsa_switch *ds = chip->ds;
2786 u32 upstream_port = dsa_upstream_port(ds);
2790 /* Enable the PHY Polling Unit if present, don't discard any packets,
2791 * and mask all interrupt sources.
2794 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
2795 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
2796 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2798 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL, reg);
2802 /* Configure the upstream port, and configure it as the port to which
2803 * ingress and egress and ARP monitor frames are to be sent.
2805 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2806 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2807 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2808 err = mv88e6xxx_g1_write(chip, GLOBAL_MONITOR_CONTROL, reg);
2812 /* Disable remote management, and set the switch's DSA device number. */
2813 err = mv88e6xxx_g1_write(chip, GLOBAL_CONTROL_2,
2814 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2815 (ds->index & 0x1f));
2819 /* Clear all the VTU and STU entries */
2820 err = _mv88e6xxx_vtu_stu_flush(chip);
2824 /* Set the default address aging time to 5 minutes, and
2825 * enable address learn messages to be sent to all message
2828 err = mv88e6xxx_g1_write(chip, GLOBAL_ATU_CONTROL,
2829 GLOBAL_ATU_CONTROL_LEARN2ALL);
2833 err = mv88e6xxx_g1_set_age_time(chip, 300000);
2837 /* Clear all ATU entries */
2838 err = _mv88e6xxx_atu_flush(chip, 0, true);
2842 /* Configure the IP ToS mapping registers. */
2843 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_0, 0x0000);
2846 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_1, 0x0000);
2849 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_2, 0x5555);
2852 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_3, 0x5555);
2855 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_4, 0xaaaa);
2858 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_5, 0xaaaa);
2861 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_6, 0xffff);
2864 err = mv88e6xxx_g1_write(chip, GLOBAL_IP_PRI_7, 0xffff);
2868 /* Configure the IEEE 802.1p priority mapping register. */
2869 err = mv88e6xxx_g1_write(chip, GLOBAL_IEEE_PRI, 0xfa41);
2873 /* Clear the statistics counters for all ports */
2874 err = mv88e6xxx_g1_write(chip, GLOBAL_STATS_OP,
2875 GLOBAL_STATS_OP_FLUSH_ALL);
2879 /* Wait for the flush to complete. */
2880 err = _mv88e6xxx_stats_wait(chip);
2887 static int mv88e6xxx_setup(struct dsa_switch *ds)
2889 struct mv88e6xxx_chip *chip = ds->priv;
2894 ds->slave_mii_bus = chip->mdio_bus;
2896 mutex_lock(&chip->reg_lock);
2898 err = mv88e6xxx_switch_reset(chip);
2902 /* Setup Switch Port Registers */
2903 for (i = 0; i < chip->info->num_ports; i++) {
2904 err = mv88e6xxx_setup_port(chip, i);
2909 /* Setup Switch Global 1 Registers */
2910 err = mv88e6xxx_g1_setup(chip);
2914 /* Setup Switch Global 2 Registers */
2915 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
2916 err = mv88e6xxx_g2_setup(chip);
2922 mutex_unlock(&chip->reg_lock);
2927 static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
2929 struct mv88e6xxx_chip *chip = ds->priv;
2932 mutex_lock(&chip->reg_lock);
2934 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
2935 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
2936 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
2938 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
2940 mutex_unlock(&chip->reg_lock);
2945 static int mv88e6xxx_mdio_read(struct mii_bus *bus, int phy, int reg)
2947 struct mv88e6xxx_chip *chip = bus->priv;
2951 if (phy >= chip->info->num_ports)
2954 mutex_lock(&chip->reg_lock);
2955 err = mv88e6xxx_phy_read(chip, phy, reg, &val);
2956 mutex_unlock(&chip->reg_lock);
2958 return err ? err : val;
2961 static int mv88e6xxx_mdio_write(struct mii_bus *bus, int phy, int reg, u16 val)
2963 struct mv88e6xxx_chip *chip = bus->priv;
2966 if (phy >= chip->info->num_ports)
2969 mutex_lock(&chip->reg_lock);
2970 err = mv88e6xxx_phy_write(chip, phy, reg, val);
2971 mutex_unlock(&chip->reg_lock);
2976 static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
2977 struct device_node *np)
2980 struct mii_bus *bus;
2984 chip->mdio_np = of_get_child_by_name(np, "mdio");
2986 bus = devm_mdiobus_alloc(chip->dev);
2990 bus->priv = (void *)chip;
2992 bus->name = np->full_name;
2993 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
2995 bus->name = "mv88e6xxx SMI";
2996 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
2999 bus->read = mv88e6xxx_mdio_read;
3000 bus->write = mv88e6xxx_mdio_write;
3001 bus->parent = chip->dev;
3004 err = of_mdiobus_register(bus, chip->mdio_np);
3006 err = mdiobus_register(bus);
3008 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
3011 chip->mdio_bus = bus;
3017 of_node_put(chip->mdio_np);
3022 static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
3025 struct mii_bus *bus = chip->mdio_bus;
3027 mdiobus_unregister(bus);
3030 of_node_put(chip->mdio_np);
3033 #ifdef CONFIG_NET_DSA_HWMON
3035 static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3037 struct mv88e6xxx_chip *chip = ds->priv;
3043 mutex_lock(&chip->reg_lock);
3045 ret = mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x6);
3049 /* Enable temperature sensor */
3050 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3054 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val | (1 << 5));
3058 /* Wait for temperature to stabilize */
3059 usleep_range(10000, 12000);
3061 ret = mv88e6xxx_phy_read(chip, 0x0, 0x1a, &val);
3065 /* Disable temperature sensor */
3066 ret = mv88e6xxx_phy_write(chip, 0x0, 0x1a, val & ~(1 << 5));
3070 *temp = ((val & 0x1f) - 5) * 5;
3073 mv88e6xxx_phy_write(chip, 0x0, 0x16, 0x0);
3074 mutex_unlock(&chip->reg_lock);
3078 static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3080 struct mv88e6xxx_chip *chip = ds->priv;
3081 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3087 mutex_lock(&chip->reg_lock);
3088 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 27, &val);
3089 mutex_unlock(&chip->reg_lock);
3093 *temp = (val & 0xff) - 25;
3098 static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3100 struct mv88e6xxx_chip *chip = ds->priv;
3102 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
3105 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
3106 return mv88e63xx_get_temp(ds, temp);
3108 return mv88e61xx_get_temp(ds, temp);
3111 static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3113 struct mv88e6xxx_chip *chip = ds->priv;
3114 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3118 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3123 mutex_lock(&chip->reg_lock);
3124 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3125 mutex_unlock(&chip->reg_lock);
3129 *temp = (((val >> 8) & 0x1f) * 5) - 25;
3134 static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3136 struct mv88e6xxx_chip *chip = ds->priv;
3137 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3141 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3144 mutex_lock(&chip->reg_lock);
3145 err = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3148 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3149 err = mv88e6xxx_phy_page_write(chip, phy, 6, 26,
3150 (val & 0xe0ff) | (temp << 8));
3152 mutex_unlock(&chip->reg_lock);
3157 static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3159 struct mv88e6xxx_chip *chip = ds->priv;
3160 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3164 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3169 mutex_lock(&chip->reg_lock);
3170 ret = mv88e6xxx_phy_page_read(chip, phy, 6, 26, &val);
3171 mutex_unlock(&chip->reg_lock);
3175 *alarm = !!(val & 0x40);
3179 #endif /* CONFIG_NET_DSA_HWMON */
3181 static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
3183 struct mv88e6xxx_chip *chip = ds->priv;
3185 return chip->eeprom_len;
3188 static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
3189 struct ethtool_eeprom *eeprom, u8 *data)
3191 struct mv88e6xxx_chip *chip = ds->priv;
3194 mutex_lock(&chip->reg_lock);
3196 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3197 err = mv88e6xxx_g2_get_eeprom16(chip, eeprom, data);
3201 mutex_unlock(&chip->reg_lock);
3206 eeprom->magic = 0xc3ec4951;
3211 static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
3212 struct ethtool_eeprom *eeprom, u8 *data)
3214 struct mv88e6xxx_chip *chip = ds->priv;
3217 if (eeprom->magic != 0xc3ec4951)
3220 mutex_lock(&chip->reg_lock);
3222 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16))
3223 err = mv88e6xxx_g2_set_eeprom16(chip, eeprom, data);
3227 mutex_unlock(&chip->reg_lock);
3232 static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3234 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3235 .family = MV88E6XXX_FAMILY_6097,
3236 .name = "Marvell 88E6085",
3237 .num_databases = 4096,
3239 .port_base_addr = 0x10,
3240 .global1_addr = 0x1b,
3241 .age_time_coeff = 15000,
3242 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3246 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3247 .family = MV88E6XXX_FAMILY_6095,
3248 .name = "Marvell 88E6095/88E6095F",
3249 .num_databases = 256,
3251 .port_base_addr = 0x10,
3252 .global1_addr = 0x1b,
3253 .age_time_coeff = 15000,
3254 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3258 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3259 .family = MV88E6XXX_FAMILY_6165,
3260 .name = "Marvell 88E6123",
3261 .num_databases = 4096,
3263 .port_base_addr = 0x10,
3264 .global1_addr = 0x1b,
3265 .age_time_coeff = 15000,
3266 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3270 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3271 .family = MV88E6XXX_FAMILY_6185,
3272 .name = "Marvell 88E6131",
3273 .num_databases = 256,
3275 .port_base_addr = 0x10,
3276 .global1_addr = 0x1b,
3277 .age_time_coeff = 15000,
3278 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3282 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3283 .family = MV88E6XXX_FAMILY_6165,
3284 .name = "Marvell 88E6161",
3285 .num_databases = 4096,
3287 .port_base_addr = 0x10,
3288 .global1_addr = 0x1b,
3289 .age_time_coeff = 15000,
3290 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3294 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3295 .family = MV88E6XXX_FAMILY_6165,
3296 .name = "Marvell 88E6165",
3297 .num_databases = 4096,
3299 .port_base_addr = 0x10,
3300 .global1_addr = 0x1b,
3301 .age_time_coeff = 15000,
3302 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3306 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3307 .family = MV88E6XXX_FAMILY_6351,
3308 .name = "Marvell 88E6171",
3309 .num_databases = 4096,
3311 .port_base_addr = 0x10,
3312 .global1_addr = 0x1b,
3313 .age_time_coeff = 15000,
3314 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3318 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3319 .family = MV88E6XXX_FAMILY_6352,
3320 .name = "Marvell 88E6172",
3321 .num_databases = 4096,
3323 .port_base_addr = 0x10,
3324 .global1_addr = 0x1b,
3325 .age_time_coeff = 15000,
3326 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3330 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3331 .family = MV88E6XXX_FAMILY_6351,
3332 .name = "Marvell 88E6175",
3333 .num_databases = 4096,
3335 .port_base_addr = 0x10,
3336 .global1_addr = 0x1b,
3337 .age_time_coeff = 15000,
3338 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3342 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3343 .family = MV88E6XXX_FAMILY_6352,
3344 .name = "Marvell 88E6176",
3345 .num_databases = 4096,
3347 .port_base_addr = 0x10,
3348 .global1_addr = 0x1b,
3349 .age_time_coeff = 15000,
3350 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3354 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3355 .family = MV88E6XXX_FAMILY_6185,
3356 .name = "Marvell 88E6185",
3357 .num_databases = 256,
3359 .port_base_addr = 0x10,
3360 .global1_addr = 0x1b,
3361 .age_time_coeff = 15000,
3362 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3366 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3367 .family = MV88E6XXX_FAMILY_6352,
3368 .name = "Marvell 88E6240",
3369 .num_databases = 4096,
3371 .port_base_addr = 0x10,
3372 .global1_addr = 0x1b,
3373 .age_time_coeff = 15000,
3374 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3378 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3379 .family = MV88E6XXX_FAMILY_6320,
3380 .name = "Marvell 88E6320",
3381 .num_databases = 4096,
3383 .port_base_addr = 0x10,
3384 .global1_addr = 0x1b,
3385 .age_time_coeff = 15000,
3386 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3390 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3391 .family = MV88E6XXX_FAMILY_6320,
3392 .name = "Marvell 88E6321",
3393 .num_databases = 4096,
3395 .port_base_addr = 0x10,
3396 .global1_addr = 0x1b,
3397 .age_time_coeff = 15000,
3398 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3402 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3403 .family = MV88E6XXX_FAMILY_6351,
3404 .name = "Marvell 88E6350",
3405 .num_databases = 4096,
3407 .port_base_addr = 0x10,
3408 .global1_addr = 0x1b,
3409 .age_time_coeff = 15000,
3410 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3414 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3415 .family = MV88E6XXX_FAMILY_6351,
3416 .name = "Marvell 88E6351",
3417 .num_databases = 4096,
3419 .port_base_addr = 0x10,
3420 .global1_addr = 0x1b,
3421 .age_time_coeff = 15000,
3422 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3426 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3427 .family = MV88E6XXX_FAMILY_6352,
3428 .name = "Marvell 88E6352",
3429 .num_databases = 4096,
3431 .port_base_addr = 0x10,
3432 .global1_addr = 0x1b,
3433 .age_time_coeff = 15000,
3434 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3438 static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
3442 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3443 if (mv88e6xxx_table[i].prod_num == prod_num)
3444 return &mv88e6xxx_table[i];
3449 static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
3451 const struct mv88e6xxx_info *info;
3452 unsigned int prod_num, rev;
3456 mutex_lock(&chip->reg_lock);
3457 err = mv88e6xxx_port_read(chip, 0, PORT_SWITCH_ID, &id);
3458 mutex_unlock(&chip->reg_lock);
3462 prod_num = (id & 0xfff0) >> 4;
3465 info = mv88e6xxx_lookup_info(prod_num);
3469 /* Update the compatible info with the probed one */
3472 err = mv88e6xxx_g2_require(chip);
3476 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3477 chip->info->prod_num, chip->info->name, rev);
3482 static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
3484 struct mv88e6xxx_chip *chip;
3486 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3492 mutex_init(&chip->reg_lock);
3497 static const struct mv88e6xxx_ops mv88e6xxx_g2_smi_phy_ops = {
3498 .read = mv88e6xxx_g2_smi_phy_read,
3499 .write = mv88e6xxx_g2_smi_phy_write,
3502 static const struct mv88e6xxx_ops mv88e6xxx_phy_ops = {
3503 .read = mv88e6xxx_read,
3504 .write = mv88e6xxx_write,
3507 static void mv88e6xxx_phy_init(struct mv88e6xxx_chip *chip)
3509 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_SMI_PHY)) {
3510 chip->phy_ops = &mv88e6xxx_g2_smi_phy_ops;
3511 } else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3512 chip->phy_ops = &mv88e6xxx_phy_ppu_ops;
3513 mv88e6xxx_ppu_state_init(chip);
3515 chip->phy_ops = &mv88e6xxx_phy_ops;
3519 static void mv88e6xxx_phy_destroy(struct mv88e6xxx_chip *chip)
3521 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU)) {
3522 mv88e6xxx_ppu_state_destroy(chip);
3526 static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
3527 struct mii_bus *bus, int sw_addr)
3529 /* ADDR[0] pin is unavailable externally and considered zero */
3534 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3535 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_MULTI_CHIP))
3536 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3541 chip->sw_addr = sw_addr;
3546 static enum dsa_tag_protocol mv88e6xxx_get_tag_protocol(struct dsa_switch *ds)
3548 struct mv88e6xxx_chip *chip = ds->priv;
3550 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EDSA))
3551 return DSA_TAG_PROTO_EDSA;
3553 return DSA_TAG_PROTO_DSA;
3556 static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3557 struct device *host_dev, int sw_addr,
3560 struct mv88e6xxx_chip *chip;
3561 struct mii_bus *bus;
3564 bus = dsa_host_dev_to_mii_bus(host_dev);
3568 chip = mv88e6xxx_alloc_chip(dsa_dev);
3572 /* Legacy SMI probing will only support chips similar to 88E6085 */
3573 chip->info = &mv88e6xxx_table[MV88E6085];
3575 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
3579 err = mv88e6xxx_detect(chip);
3583 mv88e6xxx_phy_init(chip);
3585 err = mv88e6xxx_mdio_register(chip, NULL);
3591 return chip->info->name;
3593 devm_kfree(dsa_dev, chip);
3598 static int mv88e6xxx_port_mdb_prepare(struct dsa_switch *ds, int port,
3599 const struct switchdev_obj_port_mdb *mdb,
3600 struct switchdev_trans *trans)
3602 /* We don't need any dynamic resource from the kernel (yet),
3603 * so skip the prepare phase.
3609 static void mv88e6xxx_port_mdb_add(struct dsa_switch *ds, int port,
3610 const struct switchdev_obj_port_mdb *mdb,
3611 struct switchdev_trans *trans)
3613 struct mv88e6xxx_chip *chip = ds->priv;
3615 mutex_lock(&chip->reg_lock);
3616 if (mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3617 GLOBAL_ATU_DATA_STATE_MC_STATIC))
3618 netdev_err(ds->ports[port].netdev, "failed to load multicast MAC address\n");
3619 mutex_unlock(&chip->reg_lock);
3622 static int mv88e6xxx_port_mdb_del(struct dsa_switch *ds, int port,
3623 const struct switchdev_obj_port_mdb *mdb)
3625 struct mv88e6xxx_chip *chip = ds->priv;
3628 mutex_lock(&chip->reg_lock);
3629 err = mv88e6xxx_port_db_load_purge(chip, port, mdb->addr, mdb->vid,
3630 GLOBAL_ATU_DATA_STATE_UNUSED);
3631 mutex_unlock(&chip->reg_lock);
3636 static int mv88e6xxx_port_mdb_dump(struct dsa_switch *ds, int port,
3637 struct switchdev_obj_port_mdb *mdb,
3638 int (*cb)(struct switchdev_obj *obj))
3640 struct mv88e6xxx_chip *chip = ds->priv;
3643 mutex_lock(&chip->reg_lock);
3644 err = mv88e6xxx_port_db_dump(chip, port, &mdb->obj, cb);
3645 mutex_unlock(&chip->reg_lock);
3650 static struct dsa_switch_ops mv88e6xxx_switch_ops = {
3651 .probe = mv88e6xxx_drv_probe,
3652 .get_tag_protocol = mv88e6xxx_get_tag_protocol,
3653 .setup = mv88e6xxx_setup,
3654 .set_addr = mv88e6xxx_set_addr,
3655 .adjust_link = mv88e6xxx_adjust_link,
3656 .get_strings = mv88e6xxx_get_strings,
3657 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3658 .get_sset_count = mv88e6xxx_get_sset_count,
3659 .set_eee = mv88e6xxx_set_eee,
3660 .get_eee = mv88e6xxx_get_eee,
3661 #ifdef CONFIG_NET_DSA_HWMON
3662 .get_temp = mv88e6xxx_get_temp,
3663 .get_temp_limit = mv88e6xxx_get_temp_limit,
3664 .set_temp_limit = mv88e6xxx_set_temp_limit,
3665 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3667 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
3668 .get_eeprom = mv88e6xxx_get_eeprom,
3669 .set_eeprom = mv88e6xxx_set_eeprom,
3670 .get_regs_len = mv88e6xxx_get_regs_len,
3671 .get_regs = mv88e6xxx_get_regs,
3672 .set_ageing_time = mv88e6xxx_set_ageing_time,
3673 .port_bridge_join = mv88e6xxx_port_bridge_join,
3674 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3675 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
3676 .port_fast_age = mv88e6xxx_port_fast_age,
3677 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
3678 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
3679 .port_vlan_add = mv88e6xxx_port_vlan_add,
3680 .port_vlan_del = mv88e6xxx_port_vlan_del,
3681 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
3682 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
3683 .port_fdb_add = mv88e6xxx_port_fdb_add,
3684 .port_fdb_del = mv88e6xxx_port_fdb_del,
3685 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
3686 .port_mdb_prepare = mv88e6xxx_port_mdb_prepare,
3687 .port_mdb_add = mv88e6xxx_port_mdb_add,
3688 .port_mdb_del = mv88e6xxx_port_mdb_del,
3689 .port_mdb_dump = mv88e6xxx_port_mdb_dump,
3692 static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
3693 struct device_node *np)
3695 struct device *dev = chip->dev;
3696 struct dsa_switch *ds;
3698 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
3704 ds->ops = &mv88e6xxx_switch_ops;
3706 dev_set_drvdata(dev, ds);
3708 return dsa_register_switch(ds, np);
3711 static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
3713 dsa_unregister_switch(chip->ds);
3716 static int mv88e6xxx_probe(struct mdio_device *mdiodev)
3718 struct device *dev = &mdiodev->dev;
3719 struct device_node *np = dev->of_node;
3720 const struct mv88e6xxx_info *compat_info;
3721 struct mv88e6xxx_chip *chip;
3725 compat_info = of_device_get_match_data(dev);
3729 chip = mv88e6xxx_alloc_chip(dev);
3733 chip->info = compat_info;
3735 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
3739 err = mv88e6xxx_detect(chip);
3743 mv88e6xxx_phy_init(chip);
3745 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
3746 if (IS_ERR(chip->reset))
3747 return PTR_ERR(chip->reset);
3749 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_EEPROM16) &&
3750 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
3751 chip->eeprom_len = eeprom_len;
3753 err = mv88e6xxx_mdio_register(chip, np);
3757 err = mv88e6xxx_register_switch(chip, np);
3759 mv88e6xxx_mdio_unregister(chip);
3766 static void mv88e6xxx_remove(struct mdio_device *mdiodev)
3768 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
3769 struct mv88e6xxx_chip *chip = ds->priv;
3771 mv88e6xxx_phy_destroy(chip);
3772 mv88e6xxx_unregister_switch(chip);
3773 mv88e6xxx_mdio_unregister(chip);
3776 static const struct of_device_id mv88e6xxx_of_match[] = {
3778 .compatible = "marvell,mv88e6085",
3779 .data = &mv88e6xxx_table[MV88E6085],
3784 MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
3786 static struct mdio_driver mv88e6xxx_driver = {
3787 .probe = mv88e6xxx_probe,
3788 .remove = mv88e6xxx_remove,
3790 .name = "mv88e6085",
3791 .of_match_table = mv88e6xxx_of_match,
3795 static int __init mv88e6xxx_init(void)
3797 register_switch_driver(&mv88e6xxx_switch_ops);
3798 return mdio_driver_register(&mv88e6xxx_driver);
3800 module_init(mv88e6xxx_init);
3802 static void __exit mv88e6xxx_cleanup(void)
3804 mdio_driver_unregister(&mv88e6xxx_driver);
3805 unregister_switch_driver(&mv88e6xxx_switch_ops);
3807 module_exit(mv88e6xxx_cleanup);
3809 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3810 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3811 MODULE_LICENSE("GPL");