2 * Marvell 88e6xxx Ethernet switch single-chip support
4 * Copyright (c) 2008 Marvell Semiconductor
6 * Copyright (c) 2015 CMC Electronics, Inc.
7 * Added support for VLAN Table Unit operations
9 * Copyright (c) 2016 Andrew Lunn <andrew@lunn.ch>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
17 #include <linux/delay.h>
18 #include <linux/etherdevice.h>
19 #include <linux/ethtool.h>
20 #include <linux/if_bridge.h>
21 #include <linux/jiffies.h>
22 #include <linux/list.h>
23 #include <linux/mdio.h>
24 #include <linux/module.h>
25 #include <linux/of_device.h>
26 #include <linux/of_mdio.h>
27 #include <linux/netdevice.h>
28 #include <linux/gpio/consumer.h>
29 #include <linux/phy.h>
31 #include <net/switchdev.h>
32 #include "mv88e6xxx.h"
34 static void assert_reg_lock(struct mv88e6xxx_chip *chip)
36 if (unlikely(!mutex_is_locked(&chip->reg_lock))) {
37 dev_err(chip->dev, "Switch registers lock not held!\n");
42 /* The switch ADDR[4:1] configuration pins define the chip SMI device address
43 * (ADDR[0] is always zero, thus only even SMI addresses can be strapped).
45 * When ADDR is all zero, the chip uses Single-chip Addressing Mode, assuming it
46 * is the only device connected to the SMI master. In this mode it responds to
47 * all 32 possible SMI addresses, and thus maps directly the internal devices.
49 * When ADDR is non-zero, the chip uses Multi-chip Addressing Mode, allowing
50 * multiple devices to share the SMI interface. In this mode it responds to only
51 * 2 registers, used to indirectly access the internal SMI devices.
54 static int mv88e6xxx_smi_read(struct mv88e6xxx_chip *chip,
55 int addr, int reg, u16 *val)
60 return chip->smi_ops->read(chip, addr, reg, val);
63 static int mv88e6xxx_smi_write(struct mv88e6xxx_chip *chip,
64 int addr, int reg, u16 val)
69 return chip->smi_ops->write(chip, addr, reg, val);
72 static int mv88e6xxx_smi_single_chip_read(struct mv88e6xxx_chip *chip,
73 int addr, int reg, u16 *val)
77 ret = mdiobus_read_nested(chip->bus, addr, reg);
86 static int mv88e6xxx_smi_single_chip_write(struct mv88e6xxx_chip *chip,
87 int addr, int reg, u16 val)
91 ret = mdiobus_write_nested(chip->bus, addr, reg, val);
98 static const struct mv88e6xxx_ops mv88e6xxx_smi_single_chip_ops = {
99 .read = mv88e6xxx_smi_single_chip_read,
100 .write = mv88e6xxx_smi_single_chip_write,
103 static int mv88e6xxx_smi_multi_chip_wait(struct mv88e6xxx_chip *chip)
108 for (i = 0; i < 16; i++) {
109 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_CMD);
113 if ((ret & SMI_CMD_BUSY) == 0)
120 static int mv88e6xxx_smi_multi_chip_read(struct mv88e6xxx_chip *chip,
121 int addr, int reg, u16 *val)
125 /* Wait for the bus to become free. */
126 ret = mv88e6xxx_smi_multi_chip_wait(chip);
130 /* Transmit the read command. */
131 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
132 SMI_CMD_OP_22_READ | (addr << 5) | reg);
136 /* Wait for the read command to complete. */
137 ret = mv88e6xxx_smi_multi_chip_wait(chip);
142 ret = mdiobus_read_nested(chip->bus, chip->sw_addr, SMI_DATA);
151 static int mv88e6xxx_smi_multi_chip_write(struct mv88e6xxx_chip *chip,
152 int addr, int reg, u16 val)
156 /* Wait for the bus to become free. */
157 ret = mv88e6xxx_smi_multi_chip_wait(chip);
161 /* Transmit the data to write. */
162 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_DATA, val);
166 /* Transmit the write command. */
167 ret = mdiobus_write_nested(chip->bus, chip->sw_addr, SMI_CMD,
168 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
172 /* Wait for the write command to complete. */
173 ret = mv88e6xxx_smi_multi_chip_wait(chip);
180 static const struct mv88e6xxx_ops mv88e6xxx_smi_multi_chip_ops = {
181 .read = mv88e6xxx_smi_multi_chip_read,
182 .write = mv88e6xxx_smi_multi_chip_write,
185 static int mv88e6xxx_read(struct mv88e6xxx_chip *chip,
186 int addr, int reg, u16 *val)
190 assert_reg_lock(chip);
192 err = mv88e6xxx_smi_read(chip, addr, reg, val);
196 dev_dbg(chip->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
202 static int mv88e6xxx_write(struct mv88e6xxx_chip *chip,
203 int addr, int reg, u16 val)
207 assert_reg_lock(chip);
209 err = mv88e6xxx_smi_write(chip, addr, reg, val);
213 dev_dbg(chip->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
219 /* Indirect write to single pointer-data register with an Update bit */
220 static int mv88e6xxx_update(struct mv88e6xxx_chip *chip, int addr, int reg,
226 /* Wait until the previous operation is completed */
227 for (i = 0; i < 16; ++i) {
228 err = mv88e6xxx_read(chip, addr, reg, &val);
232 if (!(val & BIT(15)))
239 /* Set the Update bit to trigger a write operation */
240 val = BIT(15) | update;
242 return mv88e6xxx_write(chip, addr, reg, val);
245 static int _mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
250 err = mv88e6xxx_read(chip, addr, reg, &val);
257 static int mv88e6xxx_reg_read(struct mv88e6xxx_chip *chip, int addr, int reg)
261 mutex_lock(&chip->reg_lock);
262 ret = _mv88e6xxx_reg_read(chip, addr, reg);
263 mutex_unlock(&chip->reg_lock);
268 static int _mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
271 return mv88e6xxx_write(chip, addr, reg, val);
274 static int mv88e6xxx_reg_write(struct mv88e6xxx_chip *chip, int addr,
279 mutex_lock(&chip->reg_lock);
280 ret = _mv88e6xxx_reg_write(chip, addr, reg, val);
281 mutex_unlock(&chip->reg_lock);
286 static int mv88e6xxx_mdio_read_direct(struct mv88e6xxx_chip *chip,
287 int addr, int regnum)
290 return _mv88e6xxx_reg_read(chip, addr, regnum);
294 static int mv88e6xxx_mdio_write_direct(struct mv88e6xxx_chip *chip,
295 int addr, int regnum, u16 val)
298 return _mv88e6xxx_reg_write(chip, addr, regnum, val);
302 static int mv88e6xxx_ppu_disable(struct mv88e6xxx_chip *chip)
305 unsigned long timeout;
307 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
311 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
312 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
316 timeout = jiffies + 1 * HZ;
317 while (time_before(jiffies, timeout)) {
318 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
322 usleep_range(1000, 2000);
323 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
324 GLOBAL_STATUS_PPU_POLLING)
331 static int mv88e6xxx_ppu_enable(struct mv88e6xxx_chip *chip)
334 unsigned long timeout;
336 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_CONTROL);
340 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL,
341 ret | GLOBAL_CONTROL_PPU_ENABLE);
345 timeout = jiffies + 1 * HZ;
346 while (time_before(jiffies, timeout)) {
347 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATUS);
351 usleep_range(1000, 2000);
352 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
353 GLOBAL_STATUS_PPU_POLLING)
360 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
362 struct mv88e6xxx_chip *chip;
364 chip = container_of(ugly, struct mv88e6xxx_chip, ppu_work);
366 mutex_lock(&chip->reg_lock);
368 if (mutex_trylock(&chip->ppu_mutex)) {
369 if (mv88e6xxx_ppu_enable(chip) == 0)
370 chip->ppu_disabled = 0;
371 mutex_unlock(&chip->ppu_mutex);
374 mutex_unlock(&chip->reg_lock);
377 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
379 struct mv88e6xxx_chip *chip = (void *)_ps;
381 schedule_work(&chip->ppu_work);
384 static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_chip *chip)
388 mutex_lock(&chip->ppu_mutex);
390 /* If the PHY polling unit is enabled, disable it so that
391 * we can access the PHY registers. If it was already
392 * disabled, cancel the timer that is going to re-enable
395 if (!chip->ppu_disabled) {
396 ret = mv88e6xxx_ppu_disable(chip);
398 mutex_unlock(&chip->ppu_mutex);
401 chip->ppu_disabled = 1;
403 del_timer(&chip->ppu_timer);
410 static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_chip *chip)
412 /* Schedule a timer to re-enable the PHY polling unit. */
413 mod_timer(&chip->ppu_timer, jiffies + msecs_to_jiffies(10));
414 mutex_unlock(&chip->ppu_mutex);
417 static void mv88e6xxx_ppu_state_init(struct mv88e6xxx_chip *chip)
419 mutex_init(&chip->ppu_mutex);
420 INIT_WORK(&chip->ppu_work, mv88e6xxx_ppu_reenable_work);
421 init_timer(&chip->ppu_timer);
422 chip->ppu_timer.data = (unsigned long)chip;
423 chip->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
426 static int mv88e6xxx_mdio_read_ppu(struct mv88e6xxx_chip *chip, int addr,
431 ret = mv88e6xxx_ppu_access_get(chip);
433 ret = _mv88e6xxx_reg_read(chip, addr, regnum);
434 mv88e6xxx_ppu_access_put(chip);
440 static int mv88e6xxx_mdio_write_ppu(struct mv88e6xxx_chip *chip, int addr,
445 ret = mv88e6xxx_ppu_access_get(chip);
447 ret = _mv88e6xxx_reg_write(chip, addr, regnum, val);
448 mv88e6xxx_ppu_access_put(chip);
454 static bool mv88e6xxx_6065_family(struct mv88e6xxx_chip *chip)
456 return chip->info->family == MV88E6XXX_FAMILY_6065;
459 static bool mv88e6xxx_6095_family(struct mv88e6xxx_chip *chip)
461 return chip->info->family == MV88E6XXX_FAMILY_6095;
464 static bool mv88e6xxx_6097_family(struct mv88e6xxx_chip *chip)
466 return chip->info->family == MV88E6XXX_FAMILY_6097;
469 static bool mv88e6xxx_6165_family(struct mv88e6xxx_chip *chip)
471 return chip->info->family == MV88E6XXX_FAMILY_6165;
474 static bool mv88e6xxx_6185_family(struct mv88e6xxx_chip *chip)
476 return chip->info->family == MV88E6XXX_FAMILY_6185;
479 static bool mv88e6xxx_6320_family(struct mv88e6xxx_chip *chip)
481 return chip->info->family == MV88E6XXX_FAMILY_6320;
484 static bool mv88e6xxx_6351_family(struct mv88e6xxx_chip *chip)
486 return chip->info->family == MV88E6XXX_FAMILY_6351;
489 static bool mv88e6xxx_6352_family(struct mv88e6xxx_chip *chip)
491 return chip->info->family == MV88E6XXX_FAMILY_6352;
494 static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_chip *chip)
496 return chip->info->num_databases;
499 static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_chip *chip)
501 /* Does the device have dedicated FID registers for ATU and VTU ops? */
502 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
503 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip))
509 /* We expect the switch to perform auto negotiation if there is a real
510 * phy. However, in the case of a fixed link phy, we force the port
511 * settings from the fixed link settings.
513 static void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
514 struct phy_device *phydev)
516 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
520 if (!phy_is_pseudo_fixed_link(phydev))
523 mutex_lock(&chip->reg_lock);
525 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
529 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
530 PORT_PCS_CTRL_FORCE_LINK |
531 PORT_PCS_CTRL_DUPLEX_FULL |
532 PORT_PCS_CTRL_FORCE_DUPLEX |
533 PORT_PCS_CTRL_UNFORCED);
535 reg |= PORT_PCS_CTRL_FORCE_LINK;
537 reg |= PORT_PCS_CTRL_LINK_UP;
539 if (mv88e6xxx_6065_family(chip) && phydev->speed > SPEED_100)
542 switch (phydev->speed) {
544 reg |= PORT_PCS_CTRL_1000;
547 reg |= PORT_PCS_CTRL_100;
550 reg |= PORT_PCS_CTRL_10;
553 pr_info("Unknown speed");
557 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
558 if (phydev->duplex == DUPLEX_FULL)
559 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
561 if ((mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip)) &&
562 (port >= chip->info->num_ports - 2)) {
563 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
564 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
565 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
566 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
567 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
568 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
569 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
571 _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_PCS_CTRL, reg);
574 mutex_unlock(&chip->reg_lock);
577 static int _mv88e6xxx_stats_wait(struct mv88e6xxx_chip *chip)
582 for (i = 0; i < 10; i++) {
583 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_OP);
584 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
591 static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_chip *chip, int port)
595 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
596 port = (port + 1) << 5;
598 /* Snapshot the hardware statistics counters for this port. */
599 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
600 GLOBAL_STATS_OP_CAPTURE_PORT |
601 GLOBAL_STATS_OP_HIST_RX_TX | port);
605 /* Wait for the snapshotting to complete. */
606 ret = _mv88e6xxx_stats_wait(chip);
613 static void _mv88e6xxx_stats_read(struct mv88e6xxx_chip *chip,
621 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
622 GLOBAL_STATS_OP_READ_CAPTURED |
623 GLOBAL_STATS_OP_HIST_RX_TX | stat);
627 ret = _mv88e6xxx_stats_wait(chip);
631 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
637 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
644 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
645 { "in_good_octets", 8, 0x00, BANK0, },
646 { "in_bad_octets", 4, 0x02, BANK0, },
647 { "in_unicast", 4, 0x04, BANK0, },
648 { "in_broadcasts", 4, 0x06, BANK0, },
649 { "in_multicasts", 4, 0x07, BANK0, },
650 { "in_pause", 4, 0x16, BANK0, },
651 { "in_undersize", 4, 0x18, BANK0, },
652 { "in_fragments", 4, 0x19, BANK0, },
653 { "in_oversize", 4, 0x1a, BANK0, },
654 { "in_jabber", 4, 0x1b, BANK0, },
655 { "in_rx_error", 4, 0x1c, BANK0, },
656 { "in_fcs_error", 4, 0x1d, BANK0, },
657 { "out_octets", 8, 0x0e, BANK0, },
658 { "out_unicast", 4, 0x10, BANK0, },
659 { "out_broadcasts", 4, 0x13, BANK0, },
660 { "out_multicasts", 4, 0x12, BANK0, },
661 { "out_pause", 4, 0x15, BANK0, },
662 { "excessive", 4, 0x11, BANK0, },
663 { "collisions", 4, 0x1e, BANK0, },
664 { "deferred", 4, 0x05, BANK0, },
665 { "single", 4, 0x14, BANK0, },
666 { "multiple", 4, 0x17, BANK0, },
667 { "out_fcs_error", 4, 0x03, BANK0, },
668 { "late", 4, 0x1f, BANK0, },
669 { "hist_64bytes", 4, 0x08, BANK0, },
670 { "hist_65_127bytes", 4, 0x09, BANK0, },
671 { "hist_128_255bytes", 4, 0x0a, BANK0, },
672 { "hist_256_511bytes", 4, 0x0b, BANK0, },
673 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
674 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
675 { "sw_in_discards", 4, 0x10, PORT, },
676 { "sw_in_filtered", 2, 0x12, PORT, },
677 { "sw_out_filtered", 2, 0x13, PORT, },
678 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
679 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
680 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
681 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
682 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
683 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
684 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
685 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
686 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
687 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
688 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
689 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
690 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
691 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
692 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
693 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
694 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
695 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
696 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
697 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
698 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
699 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
700 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
701 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
702 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
703 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
706 static bool mv88e6xxx_has_stat(struct mv88e6xxx_chip *chip,
707 struct mv88e6xxx_hw_stat *stat)
709 switch (stat->type) {
713 return mv88e6xxx_6320_family(chip);
715 return mv88e6xxx_6095_family(chip) ||
716 mv88e6xxx_6185_family(chip) ||
717 mv88e6xxx_6097_family(chip) ||
718 mv88e6xxx_6165_family(chip) ||
719 mv88e6xxx_6351_family(chip) ||
720 mv88e6xxx_6352_family(chip);
725 static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_chip *chip,
726 struct mv88e6xxx_hw_stat *s,
736 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), s->reg);
741 if (s->sizeof_stat == 4) {
742 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port),
751 _mv88e6xxx_stats_read(chip, s->reg, &low);
752 if (s->sizeof_stat == 8)
753 _mv88e6xxx_stats_read(chip, s->reg + 1, &high);
755 value = (((u64)high) << 16) | low;
759 static void mv88e6xxx_get_strings(struct dsa_switch *ds, int port,
762 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
763 struct mv88e6xxx_hw_stat *stat;
766 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
767 stat = &mv88e6xxx_hw_stats[i];
768 if (mv88e6xxx_has_stat(chip, stat)) {
769 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
776 static int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
778 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
779 struct mv88e6xxx_hw_stat *stat;
782 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
783 stat = &mv88e6xxx_hw_stats[i];
784 if (mv88e6xxx_has_stat(chip, stat))
790 static void mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds, int port,
793 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
794 struct mv88e6xxx_hw_stat *stat;
798 mutex_lock(&chip->reg_lock);
800 ret = _mv88e6xxx_stats_snapshot(chip, port);
802 mutex_unlock(&chip->reg_lock);
805 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
806 stat = &mv88e6xxx_hw_stats[i];
807 if (mv88e6xxx_has_stat(chip, stat)) {
808 data[j] = _mv88e6xxx_get_ethtool_stat(chip, stat, port);
813 mutex_unlock(&chip->reg_lock);
816 static int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
818 return 32 * sizeof(u16);
821 static void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
822 struct ethtool_regs *regs, void *_p)
824 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
830 memset(p, 0xff, 32 * sizeof(u16));
832 mutex_lock(&chip->reg_lock);
834 for (i = 0; i < 32; i++) {
837 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), i);
842 mutex_unlock(&chip->reg_lock);
845 static int _mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg, int offset,
848 unsigned long timeout = jiffies + HZ / 10;
850 while (time_before(jiffies, timeout)) {
853 ret = _mv88e6xxx_reg_read(chip, reg, offset);
859 usleep_range(1000, 2000);
864 static int mv88e6xxx_wait(struct mv88e6xxx_chip *chip, int reg,
865 int offset, u16 mask)
869 mutex_lock(&chip->reg_lock);
870 ret = _mv88e6xxx_wait(chip, reg, offset, mask);
871 mutex_unlock(&chip->reg_lock);
876 static int mv88e6xxx_mdio_wait(struct mv88e6xxx_chip *chip)
878 return _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
879 GLOBAL2_SMI_OP_BUSY);
882 static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
884 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
886 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
887 GLOBAL2_EEPROM_OP_LOAD);
890 static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
892 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
894 return mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
895 GLOBAL2_EEPROM_OP_BUSY);
898 static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
900 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
903 mutex_lock(&chip->eeprom_mutex);
905 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
906 GLOBAL2_EEPROM_OP_READ |
907 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
911 ret = mv88e6xxx_eeprom_busy_wait(ds);
915 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
917 mutex_unlock(&chip->eeprom_mutex);
921 static int mv88e6xxx_get_eeprom_len(struct dsa_switch *ds)
923 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
925 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
926 return chip->eeprom_len;
931 static int mv88e6xxx_get_eeprom(struct dsa_switch *ds,
932 struct ethtool_eeprom *eeprom, u8 *data)
934 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
939 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
942 offset = eeprom->offset;
946 eeprom->magic = 0xc3ec4951;
948 ret = mv88e6xxx_eeprom_load_wait(ds);
955 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
959 *data++ = (word >> 8) & 0xff;
969 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
973 *data++ = word & 0xff;
974 *data++ = (word >> 8) & 0xff;
984 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
988 *data++ = word & 0xff;
998 static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
1000 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1003 ret = mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
1007 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
1013 static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
1016 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1019 mutex_lock(&chip->eeprom_mutex);
1021 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
1025 ret = mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
1026 GLOBAL2_EEPROM_OP_WRITE |
1027 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
1031 ret = mv88e6xxx_eeprom_busy_wait(ds);
1033 mutex_unlock(&chip->eeprom_mutex);
1037 static int mv88e6xxx_set_eeprom(struct dsa_switch *ds,
1038 struct ethtool_eeprom *eeprom, u8 *data)
1040 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1045 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
1048 if (eeprom->magic != 0xc3ec4951)
1051 ret = mv88e6xxx_eeprom_is_readonly(ds);
1055 offset = eeprom->offset;
1059 ret = mv88e6xxx_eeprom_load_wait(ds);
1066 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1070 word = (*data++ << 8) | (word & 0xff);
1072 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1085 word |= *data++ << 8;
1087 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1099 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1103 word = (word & 0xff00) | *data++;
1105 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1117 static int _mv88e6xxx_atu_wait(struct mv88e6xxx_chip *chip)
1119 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_ATU_OP,
1120 GLOBAL_ATU_OP_BUSY);
1123 static int mv88e6xxx_mdio_read_indirect(struct mv88e6xxx_chip *chip,
1124 int addr, int regnum)
1128 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
1129 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1134 ret = mv88e6xxx_mdio_wait(chip);
1138 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1143 static int mv88e6xxx_mdio_write_indirect(struct mv88e6xxx_chip *chip,
1144 int addr, int regnum, u16 val)
1148 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
1152 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL2, GLOBAL2_SMI_OP,
1153 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1156 return mv88e6xxx_mdio_wait(chip);
1159 static int mv88e6xxx_get_eee(struct dsa_switch *ds, int port,
1160 struct ethtool_eee *e)
1162 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1165 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
1168 mutex_lock(&chip->reg_lock);
1170 reg = mv88e6xxx_mdio_read_indirect(chip, port, 16);
1174 e->eee_enabled = !!(reg & 0x0200);
1175 e->tx_lpi_enabled = !!(reg & 0x0100);
1177 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
1181 e->eee_active = !!(reg & PORT_STATUS_EEE);
1185 mutex_unlock(&chip->reg_lock);
1189 static int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1190 struct phy_device *phydev, struct ethtool_eee *e)
1192 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1196 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEE))
1199 mutex_lock(&chip->reg_lock);
1201 ret = mv88e6xxx_mdio_read_indirect(chip, port, 16);
1205 reg = ret & ~0x0300;
1208 if (e->tx_lpi_enabled)
1211 ret = mv88e6xxx_mdio_write_indirect(chip, port, 16, reg);
1213 mutex_unlock(&chip->reg_lock);
1218 static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_chip *chip, u16 fid, u16 cmd)
1222 if (mv88e6xxx_has_fid_reg(chip)) {
1223 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_FID,
1227 } else if (mv88e6xxx_num_databases(chip) == 256) {
1228 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
1229 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL);
1233 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1235 ((fid << 8) & 0xf000));
1239 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1243 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1247 return _mv88e6xxx_atu_wait(chip);
1250 static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_chip *chip,
1251 struct mv88e6xxx_atu_entry *entry)
1253 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1255 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1256 unsigned int mask, shift;
1259 data |= GLOBAL_ATU_DATA_TRUNK;
1260 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1261 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1263 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1264 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1267 data |= (entry->portv_trunkid << shift) & mask;
1270 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1273 static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_chip *chip,
1274 struct mv88e6xxx_atu_entry *entry,
1280 err = _mv88e6xxx_atu_wait(chip);
1284 err = _mv88e6xxx_atu_data_write(chip, entry);
1289 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1290 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1292 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1293 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1296 return _mv88e6xxx_atu_cmd(chip, entry->fid, op);
1299 static int _mv88e6xxx_atu_flush(struct mv88e6xxx_chip *chip,
1300 u16 fid, bool static_too)
1302 struct mv88e6xxx_atu_entry entry = {
1304 .state = 0, /* EntryState bits must be 0 */
1307 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1310 static int _mv88e6xxx_atu_move(struct mv88e6xxx_chip *chip, u16 fid,
1311 int from_port, int to_port, bool static_too)
1313 struct mv88e6xxx_atu_entry entry = {
1318 /* EntryState bits must be 0xF */
1319 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1321 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1322 entry.portv_trunkid = (to_port & 0x0f) << 4;
1323 entry.portv_trunkid |= from_port & 0x0f;
1325 return _mv88e6xxx_atu_flush_move(chip, &entry, static_too);
1328 static int _mv88e6xxx_atu_remove(struct mv88e6xxx_chip *chip, u16 fid,
1329 int port, bool static_too)
1331 /* Destination port 0xF means remove the entries */
1332 return _mv88e6xxx_atu_move(chip, fid, port, 0x0f, static_too);
1335 static const char * const mv88e6xxx_port_state_names[] = {
1336 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1337 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1338 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1339 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1342 static int _mv88e6xxx_port_state(struct mv88e6xxx_chip *chip, int port,
1345 struct dsa_switch *ds = chip->ds;
1349 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL);
1353 oldstate = reg & PORT_CONTROL_STATE_MASK;
1355 if (oldstate != state) {
1356 /* Flush forwarding database if we're moving a port
1357 * from Learning or Forwarding state to Disabled or
1358 * Blocking or Listening state.
1360 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1361 oldstate == PORT_CONTROL_STATE_FORWARDING) &&
1362 (state == PORT_CONTROL_STATE_DISABLED ||
1363 state == PORT_CONTROL_STATE_BLOCKING)) {
1364 ret = _mv88e6xxx_atu_remove(chip, 0, port, false);
1369 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1370 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL,
1375 netdev_dbg(ds->ports[port].netdev, "PortState %s (was %s)\n",
1376 mv88e6xxx_port_state_names[state],
1377 mv88e6xxx_port_state_names[oldstate]);
1383 static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_chip *chip, int port)
1385 struct net_device *bridge = chip->ports[port].bridge_dev;
1386 const u16 mask = (1 << chip->info->num_ports) - 1;
1387 struct dsa_switch *ds = chip->ds;
1388 u16 output_ports = 0;
1392 /* allow CPU port or DSA link(s) to send frames to every port */
1393 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1394 output_ports = mask;
1396 for (i = 0; i < chip->info->num_ports; ++i) {
1397 /* allow sending frames to every group member */
1398 if (bridge && chip->ports[i].bridge_dev == bridge)
1399 output_ports |= BIT(i);
1401 /* allow sending frames to CPU port and DSA link(s) */
1402 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1403 output_ports |= BIT(i);
1407 /* prevent frames from going back out of the port they came in on */
1408 output_ports &= ~BIT(port);
1410 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1415 reg |= output_ports & mask;
1417 return _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN, reg);
1420 static void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port,
1423 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1428 case BR_STATE_DISABLED:
1429 stp_state = PORT_CONTROL_STATE_DISABLED;
1431 case BR_STATE_BLOCKING:
1432 case BR_STATE_LISTENING:
1433 stp_state = PORT_CONTROL_STATE_BLOCKING;
1435 case BR_STATE_LEARNING:
1436 stp_state = PORT_CONTROL_STATE_LEARNING;
1438 case BR_STATE_FORWARDING:
1440 stp_state = PORT_CONTROL_STATE_FORWARDING;
1444 mutex_lock(&chip->reg_lock);
1445 err = _mv88e6xxx_port_state(chip, port, stp_state);
1446 mutex_unlock(&chip->reg_lock);
1449 netdev_err(ds->ports[port].netdev,
1450 "failed to update state to %s\n",
1451 mv88e6xxx_port_state_names[stp_state]);
1454 static int _mv88e6xxx_port_pvid(struct mv88e6xxx_chip *chip, int port,
1457 struct dsa_switch *ds = chip->ds;
1461 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_DEFAULT_VLAN);
1465 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1468 ret &= ~PORT_DEFAULT_VLAN_MASK;
1469 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1471 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
1472 PORT_DEFAULT_VLAN, ret);
1476 netdev_dbg(ds->ports[port].netdev,
1477 "DefaultVID %d (was %d)\n", *new, pvid);
1486 static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_chip *chip,
1487 int port, u16 *pvid)
1489 return _mv88e6xxx_port_pvid(chip, port, NULL, pvid);
1492 static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_chip *chip,
1495 return _mv88e6xxx_port_pvid(chip, port, &pvid, NULL);
1498 static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_chip *chip)
1500 return _mv88e6xxx_wait(chip, REG_GLOBAL, GLOBAL_VTU_OP,
1501 GLOBAL_VTU_OP_BUSY);
1504 static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_chip *chip, u16 op)
1508 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_OP, op);
1512 return _mv88e6xxx_vtu_wait(chip);
1515 static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_chip *chip)
1519 ret = _mv88e6xxx_vtu_wait(chip);
1523 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_FLUSH_ALL);
1526 static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_chip *chip,
1527 struct mv88e6xxx_vtu_stu_entry *entry,
1528 unsigned int nibble_offset)
1534 for (i = 0; i < 3; ++i) {
1535 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1536 GLOBAL_VTU_DATA_0_3 + i);
1543 for (i = 0; i < chip->info->num_ports; ++i) {
1544 unsigned int shift = (i % 4) * 4 + nibble_offset;
1545 u16 reg = regs[i / 4];
1547 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1553 static int mv88e6xxx_vtu_data_read(struct mv88e6xxx_chip *chip,
1554 struct mv88e6xxx_vtu_stu_entry *entry)
1556 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 0);
1559 static int mv88e6xxx_stu_data_read(struct mv88e6xxx_chip *chip,
1560 struct mv88e6xxx_vtu_stu_entry *entry)
1562 return _mv88e6xxx_vtu_stu_data_read(chip, entry, 2);
1565 static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_chip *chip,
1566 struct mv88e6xxx_vtu_stu_entry *entry,
1567 unsigned int nibble_offset)
1569 u16 regs[3] = { 0 };
1573 for (i = 0; i < chip->info->num_ports; ++i) {
1574 unsigned int shift = (i % 4) * 4 + nibble_offset;
1575 u8 data = entry->data[i];
1577 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1580 for (i = 0; i < 3; ++i) {
1581 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL,
1582 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1590 static int mv88e6xxx_vtu_data_write(struct mv88e6xxx_chip *chip,
1591 struct mv88e6xxx_vtu_stu_entry *entry)
1593 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 0);
1596 static int mv88e6xxx_stu_data_write(struct mv88e6xxx_chip *chip,
1597 struct mv88e6xxx_vtu_stu_entry *entry)
1599 return _mv88e6xxx_vtu_stu_data_write(chip, entry, 2);
1602 static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_chip *chip, u16 vid)
1604 return _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID,
1605 vid & GLOBAL_VTU_VID_MASK);
1608 static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_chip *chip,
1609 struct mv88e6xxx_vtu_stu_entry *entry)
1611 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1614 ret = _mv88e6xxx_vtu_wait(chip);
1618 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_VTU_GET_NEXT);
1622 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1626 next.vid = ret & GLOBAL_VTU_VID_MASK;
1627 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1630 ret = mv88e6xxx_vtu_data_read(chip, &next);
1634 if (mv88e6xxx_has_fid_reg(chip)) {
1635 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1640 next.fid = ret & GLOBAL_VTU_FID_MASK;
1641 } else if (mv88e6xxx_num_databases(chip) == 256) {
1642 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1643 * VTU DBNum[3:0] are located in VTU Operation 3:0
1645 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1650 next.fid = (ret & 0xf00) >> 4;
1651 next.fid |= ret & 0xf;
1654 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1655 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
1660 next.sid = ret & GLOBAL_VTU_SID_MASK;
1668 static int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1669 struct switchdev_obj_port_vlan *vlan,
1670 int (*cb)(struct switchdev_obj *obj))
1672 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
1673 struct mv88e6xxx_vtu_stu_entry next;
1677 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
1680 mutex_lock(&chip->reg_lock);
1682 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
1686 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1691 err = _mv88e6xxx_vtu_getnext(chip, &next);
1698 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1701 /* reinit and dump this VLAN obj */
1702 vlan->vid_begin = next.vid;
1703 vlan->vid_end = next.vid;
1706 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1707 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1709 if (next.vid == pvid)
1710 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1712 err = cb(&vlan->obj);
1715 } while (next.vid < GLOBAL_VTU_VID_MASK);
1718 mutex_unlock(&chip->reg_lock);
1723 static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_chip *chip,
1724 struct mv88e6xxx_vtu_stu_entry *entry)
1726 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1730 ret = _mv88e6xxx_vtu_wait(chip);
1737 /* Write port member tags */
1738 ret = mv88e6xxx_vtu_data_write(chip, entry);
1742 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_STU)) {
1743 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1744 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1750 if (mv88e6xxx_has_fid_reg(chip)) {
1751 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1752 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_FID,
1756 } else if (mv88e6xxx_num_databases(chip) == 256) {
1757 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1758 * VTU DBNum[3:0] are located in VTU Operation 3:0
1760 op |= (entry->fid & 0xf0) << 8;
1761 op |= entry->fid & 0xf;
1764 reg = GLOBAL_VTU_VID_VALID;
1766 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1767 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1771 return _mv88e6xxx_vtu_cmd(chip, op);
1774 static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_chip *chip, u8 sid,
1775 struct mv88e6xxx_vtu_stu_entry *entry)
1777 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1780 ret = _mv88e6xxx_vtu_wait(chip);
1784 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID,
1785 sid & GLOBAL_VTU_SID_MASK);
1789 ret = _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_GET_NEXT);
1793 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_SID);
1797 next.sid = ret & GLOBAL_VTU_SID_MASK;
1799 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_VTU_VID);
1803 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1806 ret = mv88e6xxx_stu_data_read(chip, &next);
1815 static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_chip *chip,
1816 struct mv88e6xxx_vtu_stu_entry *entry)
1821 ret = _mv88e6xxx_vtu_wait(chip);
1828 /* Write port states */
1829 ret = mv88e6xxx_stu_data_write(chip, entry);
1833 reg = GLOBAL_VTU_VID_VALID;
1835 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1839 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1840 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1844 return _mv88e6xxx_vtu_cmd(chip, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1847 static int _mv88e6xxx_port_fid(struct mv88e6xxx_chip *chip, int port,
1850 struct dsa_switch *ds = chip->ds;
1855 if (mv88e6xxx_num_databases(chip) == 4096)
1857 else if (mv88e6xxx_num_databases(chip) == 256)
1862 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1863 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_BASE_VLAN);
1867 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1870 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1871 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1873 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_BASE_VLAN,
1879 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1880 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_1);
1884 fid |= (ret & upper_mask) << 4;
1888 ret |= (*new >> 4) & upper_mask;
1890 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
1895 netdev_dbg(ds->ports[port].netdev,
1896 "FID %d (was %d)\n", *new, fid);
1905 static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_chip *chip,
1908 return _mv88e6xxx_port_fid(chip, port, NULL, fid);
1911 static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_chip *chip,
1914 return _mv88e6xxx_port_fid(chip, port, &fid, NULL);
1917 static int _mv88e6xxx_fid_new(struct mv88e6xxx_chip *chip, u16 *fid)
1919 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1920 struct mv88e6xxx_vtu_stu_entry vlan;
1923 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1925 /* Set every FID bit used by the (un)bridged ports */
1926 for (i = 0; i < chip->info->num_ports; ++i) {
1927 err = _mv88e6xxx_port_fid_get(chip, i, fid);
1931 set_bit(*fid, fid_bitmap);
1934 /* Set every FID bit used by the VLAN entries */
1935 err = _mv88e6xxx_vtu_vid_write(chip, GLOBAL_VTU_VID_MASK);
1940 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
1947 set_bit(vlan.fid, fid_bitmap);
1948 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1950 /* The reset value 0x000 is used to indicate that multiple address
1951 * databases are not needed. Return the next positive available.
1953 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1954 if (unlikely(*fid >= mv88e6xxx_num_databases(chip)))
1957 /* Clear the database */
1958 return _mv88e6xxx_atu_flush(chip, *fid, true);
1961 static int _mv88e6xxx_vtu_new(struct mv88e6xxx_chip *chip, u16 vid,
1962 struct mv88e6xxx_vtu_stu_entry *entry)
1964 struct dsa_switch *ds = chip->ds;
1965 struct mv88e6xxx_vtu_stu_entry vlan = {
1971 err = _mv88e6xxx_fid_new(chip, &vlan.fid);
1975 /* exclude all ports except the CPU and DSA ports */
1976 for (i = 0; i < chip->info->num_ports; ++i)
1977 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1978 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1979 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1981 if (mv88e6xxx_6097_family(chip) || mv88e6xxx_6165_family(chip) ||
1982 mv88e6xxx_6351_family(chip) || mv88e6xxx_6352_family(chip)) {
1983 struct mv88e6xxx_vtu_stu_entry vstp;
1985 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1986 * implemented, only one STU entry is needed to cover all VTU
1987 * entries. Thus, validate the SID 0.
1990 err = _mv88e6xxx_stu_getnext(chip, GLOBAL_VTU_SID_MASK, &vstp);
1994 if (vstp.sid != vlan.sid || !vstp.valid) {
1995 memset(&vstp, 0, sizeof(vstp));
1997 vstp.sid = vlan.sid;
1999 err = _mv88e6xxx_stu_loadpurge(chip, &vstp);
2009 static int _mv88e6xxx_vtu_get(struct mv88e6xxx_chip *chip, u16 vid,
2010 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
2017 err = _mv88e6xxx_vtu_vid_write(chip, vid - 1);
2021 err = _mv88e6xxx_vtu_getnext(chip, entry);
2025 if (entry->vid != vid || !entry->valid) {
2028 /* -ENOENT would've been more appropriate, but switchdev expects
2029 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
2032 err = _mv88e6xxx_vtu_new(chip, vid, entry);
2038 static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
2039 u16 vid_begin, u16 vid_end)
2041 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2042 struct mv88e6xxx_vtu_stu_entry vlan;
2048 mutex_lock(&chip->reg_lock);
2050 err = _mv88e6xxx_vtu_vid_write(chip, vid_begin - 1);
2055 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2062 if (vlan.vid > vid_end)
2065 for (i = 0; i < chip->info->num_ports; ++i) {
2066 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
2070 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2073 if (chip->ports[i].bridge_dev ==
2074 chip->ports[port].bridge_dev)
2075 break; /* same bridge, check next VLAN */
2077 netdev_warn(ds->ports[port].netdev,
2078 "hardware VLAN %d already used by %s\n",
2080 netdev_name(chip->ports[i].bridge_dev));
2084 } while (vlan.vid < vid_end);
2087 mutex_unlock(&chip->reg_lock);
2092 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2093 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2094 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2095 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2096 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2099 static int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2100 bool vlan_filtering)
2102 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2103 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2104 PORT_CONTROL_2_8021Q_DISABLED;
2107 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2110 mutex_lock(&chip->reg_lock);
2112 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_CONTROL_2);
2116 old = ret & PORT_CONTROL_2_8021Q_MASK;
2119 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2120 ret |= new & PORT_CONTROL_2_8021Q_MASK;
2122 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_2,
2127 netdev_dbg(ds->ports[port].netdev, "802.1Q Mode %s (was %s)\n",
2128 mv88e6xxx_port_8021q_mode_names[new],
2129 mv88e6xxx_port_8021q_mode_names[old]);
2134 mutex_unlock(&chip->reg_lock);
2140 mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2141 const struct switchdev_obj_port_vlan *vlan,
2142 struct switchdev_trans *trans)
2144 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2147 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2150 /* If the requested port doesn't belong to the same bridge as the VLAN
2151 * members, do not support it (yet) and fallback to software VLAN.
2153 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2158 /* We don't need any dynamic resource from the kernel (yet),
2159 * so skip the prepare phase.
2164 static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_chip *chip, int port,
2165 u16 vid, bool untagged)
2167 struct mv88e6xxx_vtu_stu_entry vlan;
2170 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, true);
2174 vlan.data[port] = untagged ?
2175 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2176 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2178 return _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2181 static void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2182 const struct switchdev_obj_port_vlan *vlan,
2183 struct switchdev_trans *trans)
2185 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2186 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2187 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2190 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2193 mutex_lock(&chip->reg_lock);
2195 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
2196 if (_mv88e6xxx_port_vlan_add(chip, port, vid, untagged))
2197 netdev_err(ds->ports[port].netdev,
2198 "failed to add VLAN %d%c\n",
2199 vid, untagged ? 'u' : 't');
2201 if (pvid && _mv88e6xxx_port_pvid_set(chip, port, vlan->vid_end))
2202 netdev_err(ds->ports[port].netdev, "failed to set PVID %d\n",
2205 mutex_unlock(&chip->reg_lock);
2208 static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_chip *chip,
2211 struct dsa_switch *ds = chip->ds;
2212 struct mv88e6xxx_vtu_stu_entry vlan;
2215 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2219 /* Tell switchdev if this VLAN is handled in software */
2220 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2223 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2225 /* keep the VLAN unless all ports are excluded */
2227 for (i = 0; i < chip->info->num_ports; ++i) {
2228 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
2231 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
2237 err = _mv88e6xxx_vtu_loadpurge(chip, &vlan);
2241 return _mv88e6xxx_atu_remove(chip, vlan.fid, port, false);
2244 static int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2245 const struct switchdev_obj_port_vlan *vlan)
2247 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2251 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_VTU))
2254 mutex_lock(&chip->reg_lock);
2256 err = _mv88e6xxx_port_pvid_get(chip, port, &pvid);
2260 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2261 err = _mv88e6xxx_port_vlan_del(chip, port, vid);
2266 err = _mv88e6xxx_port_pvid_set(chip, port, 0);
2273 mutex_unlock(&chip->reg_lock);
2278 static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_chip *chip,
2279 const unsigned char *addr)
2283 for (i = 0; i < 3; i++) {
2284 ret = _mv88e6xxx_reg_write(
2285 chip, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2286 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2294 static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_chip *chip,
2295 unsigned char *addr)
2299 for (i = 0; i < 3; i++) {
2300 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL,
2301 GLOBAL_ATU_MAC_01 + i);
2304 addr[i * 2] = ret >> 8;
2305 addr[i * 2 + 1] = ret & 0xff;
2311 static int _mv88e6xxx_atu_load(struct mv88e6xxx_chip *chip,
2312 struct mv88e6xxx_atu_entry *entry)
2316 ret = _mv88e6xxx_atu_wait(chip);
2320 ret = _mv88e6xxx_atu_mac_write(chip, entry->mac);
2324 ret = _mv88e6xxx_atu_data_write(chip, entry);
2328 return _mv88e6xxx_atu_cmd(chip, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2331 static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_chip *chip, int port,
2332 const unsigned char *addr, u16 vid,
2335 struct mv88e6xxx_atu_entry entry = { 0 };
2336 struct mv88e6xxx_vtu_stu_entry vlan;
2339 /* Null VLAN ID corresponds to the port private database */
2341 err = _mv88e6xxx_port_fid_get(chip, port, &vlan.fid);
2343 err = _mv88e6xxx_vtu_get(chip, vid, &vlan, false);
2347 entry.fid = vlan.fid;
2348 entry.state = state;
2349 ether_addr_copy(entry.mac, addr);
2350 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2351 entry.trunk = false;
2352 entry.portv_trunkid = BIT(port);
2355 return _mv88e6xxx_atu_load(chip, &entry);
2358 static int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2359 const struct switchdev_obj_port_fdb *fdb,
2360 struct switchdev_trans *trans)
2362 /* We don't need any dynamic resource from the kernel (yet),
2363 * so skip the prepare phase.
2368 static void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2369 const struct switchdev_obj_port_fdb *fdb,
2370 struct switchdev_trans *trans)
2372 int state = is_multicast_ether_addr(fdb->addr) ?
2373 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2374 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2375 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2377 mutex_lock(&chip->reg_lock);
2378 if (_mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid, state))
2379 netdev_err(ds->ports[port].netdev,
2380 "failed to load MAC address\n");
2381 mutex_unlock(&chip->reg_lock);
2384 static int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2385 const struct switchdev_obj_port_fdb *fdb)
2387 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2390 mutex_lock(&chip->reg_lock);
2391 ret = _mv88e6xxx_port_fdb_load(chip, port, fdb->addr, fdb->vid,
2392 GLOBAL_ATU_DATA_STATE_UNUSED);
2393 mutex_unlock(&chip->reg_lock);
2398 static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_chip *chip, u16 fid,
2399 struct mv88e6xxx_atu_entry *entry)
2401 struct mv88e6xxx_atu_entry next = { 0 };
2406 ret = _mv88e6xxx_atu_wait(chip);
2410 ret = _mv88e6xxx_atu_cmd(chip, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2414 ret = _mv88e6xxx_atu_mac_read(chip, next.mac);
2418 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, GLOBAL_ATU_DATA);
2422 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2423 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2424 unsigned int mask, shift;
2426 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2428 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2429 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2432 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2433 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2436 next.portv_trunkid = (ret & mask) >> shift;
2443 static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_chip *chip,
2444 u16 fid, u16 vid, int port,
2445 struct switchdev_obj_port_fdb *fdb,
2446 int (*cb)(struct switchdev_obj *obj))
2448 struct mv88e6xxx_atu_entry addr = {
2449 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2453 err = _mv88e6xxx_atu_mac_write(chip, addr.mac);
2458 err = _mv88e6xxx_atu_getnext(chip, fid, &addr);
2462 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2465 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2466 bool is_static = addr.state ==
2467 (is_multicast_ether_addr(addr.mac) ?
2468 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2469 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2472 ether_addr_copy(fdb->addr, addr.mac);
2473 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2475 err = cb(&fdb->obj);
2479 } while (!is_broadcast_ether_addr(addr.mac));
2484 static int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2485 struct switchdev_obj_port_fdb *fdb,
2486 int (*cb)(struct switchdev_obj *obj))
2488 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2489 struct mv88e6xxx_vtu_stu_entry vlan = {
2490 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2495 mutex_lock(&chip->reg_lock);
2497 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2498 err = _mv88e6xxx_port_fid_get(chip, port, &fid);
2502 err = _mv88e6xxx_port_fdb_dump_one(chip, fid, 0, port, fdb, cb);
2506 /* Dump VLANs' Filtering Information Databases */
2507 err = _mv88e6xxx_vtu_vid_write(chip, vlan.vid);
2512 err = _mv88e6xxx_vtu_getnext(chip, &vlan);
2519 err = _mv88e6xxx_port_fdb_dump_one(chip, vlan.fid, vlan.vid,
2523 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2526 mutex_unlock(&chip->reg_lock);
2531 static int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2532 struct net_device *bridge)
2534 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2537 mutex_lock(&chip->reg_lock);
2539 /* Assign the bridge and remap each port's VLANTable */
2540 chip->ports[port].bridge_dev = bridge;
2542 for (i = 0; i < chip->info->num_ports; ++i) {
2543 if (chip->ports[i].bridge_dev == bridge) {
2544 err = _mv88e6xxx_port_based_vlan_map(chip, i);
2550 mutex_unlock(&chip->reg_lock);
2555 static void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2557 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
2558 struct net_device *bridge = chip->ports[port].bridge_dev;
2561 mutex_lock(&chip->reg_lock);
2563 /* Unassign the bridge and remap each port's VLANTable */
2564 chip->ports[port].bridge_dev = NULL;
2566 for (i = 0; i < chip->info->num_ports; ++i)
2567 if (i == port || chip->ports[i].bridge_dev == bridge)
2568 if (_mv88e6xxx_port_based_vlan_map(chip, i))
2569 netdev_warn(ds->ports[i].netdev,
2570 "failed to remap\n");
2572 mutex_unlock(&chip->reg_lock);
2575 static int _mv88e6xxx_mdio_page_write(struct mv88e6xxx_chip *chip,
2576 int port, int page, int reg, int val)
2580 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
2582 goto restore_page_0;
2584 ret = mv88e6xxx_mdio_write_indirect(chip, port, reg, val);
2586 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
2591 static int _mv88e6xxx_mdio_page_read(struct mv88e6xxx_chip *chip,
2592 int port, int page, int reg)
2596 ret = mv88e6xxx_mdio_write_indirect(chip, port, 0x16, page);
2598 goto restore_page_0;
2600 ret = mv88e6xxx_mdio_read_indirect(chip, port, reg);
2602 mv88e6xxx_mdio_write_indirect(chip, port, 0x16, 0x0);
2607 static int mv88e6xxx_switch_reset(struct mv88e6xxx_chip *chip)
2609 bool ppu_active = mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE);
2610 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2611 struct gpio_desc *gpiod = chip->reset;
2612 unsigned long timeout;
2616 /* Set all ports to the disabled state. */
2617 for (i = 0; i < chip->info->num_ports; i++) {
2618 ret = _mv88e6xxx_reg_read(chip, REG_PORT(i), PORT_CONTROL);
2622 ret = _mv88e6xxx_reg_write(chip, REG_PORT(i), PORT_CONTROL,
2628 /* Wait for transmit queues to drain. */
2629 usleep_range(2000, 4000);
2631 /* If there is a gpio connected to the reset pin, toggle it */
2633 gpiod_set_value_cansleep(gpiod, 1);
2634 usleep_range(10000, 20000);
2635 gpiod_set_value_cansleep(gpiod, 0);
2636 usleep_range(10000, 20000);
2639 /* Reset the switch. Keep the PPU active if requested. The PPU
2640 * needs to be active to support indirect phy register access
2641 * through global registers 0x18 and 0x19.
2644 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc000);
2646 ret = _mv88e6xxx_reg_write(chip, REG_GLOBAL, 0x04, 0xc400);
2650 /* Wait up to one second for reset to complete. */
2651 timeout = jiffies + 1 * HZ;
2652 while (time_before(jiffies, timeout)) {
2653 ret = _mv88e6xxx_reg_read(chip, REG_GLOBAL, 0x00);
2657 if ((ret & is_reset) == is_reset)
2659 usleep_range(1000, 2000);
2661 if (time_after(jiffies, timeout))
2669 static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_chip *chip)
2673 ret = _mv88e6xxx_mdio_page_read(chip, REG_FIBER_SERDES,
2674 PAGE_FIBER_SERDES, MII_BMCR);
2678 if (ret & BMCR_PDOWN) {
2680 ret = _mv88e6xxx_mdio_page_write(chip, REG_FIBER_SERDES,
2681 PAGE_FIBER_SERDES, MII_BMCR,
2688 static int mv88e6xxx_setup_port(struct mv88e6xxx_chip *chip, int port)
2690 struct dsa_switch *ds = chip->ds;
2694 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2695 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2696 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2697 mv88e6xxx_6065_family(chip) || mv88e6xxx_6320_family(chip)) {
2698 /* MAC Forcing register: don't force link, speed,
2699 * duplex or flow control state to any particular
2700 * values on physical ports, but force the CPU port
2701 * and all DSA ports to their maximum bandwidth and
2704 reg = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_PCS_CTRL);
2705 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2706 reg &= ~PORT_PCS_CTRL_UNFORCED;
2707 reg |= PORT_PCS_CTRL_FORCE_LINK |
2708 PORT_PCS_CTRL_LINK_UP |
2709 PORT_PCS_CTRL_DUPLEX_FULL |
2710 PORT_PCS_CTRL_FORCE_DUPLEX;
2711 if (mv88e6xxx_6065_family(chip))
2712 reg |= PORT_PCS_CTRL_100;
2714 reg |= PORT_PCS_CTRL_1000;
2716 reg |= PORT_PCS_CTRL_UNFORCED;
2719 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2720 PORT_PCS_CTRL, reg);
2725 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2726 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2727 * tunneling, determine priority by looking at 802.1p and IP
2728 * priority fields (IP prio has precedence), and set STP state
2731 * If this is the CPU link, use DSA or EDSA tagging depending
2732 * on which tagging mode was configured.
2734 * If this is a link to another switch, use DSA tagging mode.
2736 * If this is the upstream port for this switch, enable
2737 * forwarding of unknown unicasts and multicasts.
2740 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2741 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2742 mv88e6xxx_6095_family(chip) || mv88e6xxx_6065_family(chip) ||
2743 mv88e6xxx_6185_family(chip) || mv88e6xxx_6320_family(chip))
2744 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2745 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2746 PORT_CONTROL_STATE_FORWARDING;
2747 if (dsa_is_cpu_port(ds, port)) {
2748 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip))
2749 reg |= PORT_CONTROL_DSA_TAG;
2750 if (mv88e6xxx_6352_family(chip) ||
2751 mv88e6xxx_6351_family(chip) ||
2752 mv88e6xxx_6165_family(chip) ||
2753 mv88e6xxx_6097_family(chip) ||
2754 mv88e6xxx_6320_family(chip)) {
2755 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA |
2756 PORT_CONTROL_FORWARD_UNKNOWN |
2757 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2760 if (mv88e6xxx_6352_family(chip) ||
2761 mv88e6xxx_6351_family(chip) ||
2762 mv88e6xxx_6165_family(chip) ||
2763 mv88e6xxx_6097_family(chip) ||
2764 mv88e6xxx_6095_family(chip) ||
2765 mv88e6xxx_6065_family(chip) ||
2766 mv88e6xxx_6185_family(chip) ||
2767 mv88e6xxx_6320_family(chip)) {
2768 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2771 if (dsa_is_dsa_port(ds, port)) {
2772 if (mv88e6xxx_6095_family(chip) ||
2773 mv88e6xxx_6185_family(chip))
2774 reg |= PORT_CONTROL_DSA_TAG;
2775 if (mv88e6xxx_6352_family(chip) ||
2776 mv88e6xxx_6351_family(chip) ||
2777 mv88e6xxx_6165_family(chip) ||
2778 mv88e6xxx_6097_family(chip) ||
2779 mv88e6xxx_6320_family(chip)) {
2780 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2783 if (port == dsa_upstream_port(ds))
2784 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2785 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2788 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2794 /* If this port is connected to a SerDes, make sure the SerDes is not
2797 if (mv88e6xxx_6352_family(chip)) {
2798 ret = _mv88e6xxx_reg_read(chip, REG_PORT(port), PORT_STATUS);
2801 ret &= PORT_STATUS_CMODE_MASK;
2802 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2803 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2804 (ret == PORT_STATUS_CMODE_SGMII)) {
2805 ret = mv88e6xxx_power_on_serdes(chip);
2811 /* Port Control 2: don't force a good FCS, set the maximum frame size to
2812 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2813 * untagged frames on this port, do a destination address lookup on all
2814 * received packets as usual, disable ARP mirroring and don't send a
2815 * copy of all transmitted/received frames on this port to the CPU.
2818 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2819 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2820 mv88e6xxx_6095_family(chip) || mv88e6xxx_6320_family(chip) ||
2821 mv88e6xxx_6185_family(chip))
2822 reg = PORT_CONTROL_2_MAP_DA;
2824 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2825 mv88e6xxx_6165_family(chip) || mv88e6xxx_6320_family(chip))
2826 reg |= PORT_CONTROL_2_JUMBO_10240;
2828 if (mv88e6xxx_6095_family(chip) || mv88e6xxx_6185_family(chip)) {
2829 /* Set the upstream port this port should use */
2830 reg |= dsa_upstream_port(ds);
2831 /* enable forwarding of unknown multicast addresses to
2834 if (port == dsa_upstream_port(ds))
2835 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2838 reg |= PORT_CONTROL_2_8021Q_DISABLED;
2841 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2842 PORT_CONTROL_2, reg);
2847 /* Port Association Vector: when learning source addresses
2848 * of packets, add the address to the address database using
2849 * a port bitmap that has only the bit for this port set and
2850 * the other bits clear.
2853 /* Disable learning for CPU port */
2854 if (dsa_is_cpu_port(ds, port))
2857 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_ASSOC_VECTOR,
2862 /* Egress rate control 2: disable egress rate control. */
2863 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_RATE_CONTROL_2,
2868 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2869 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2870 mv88e6xxx_6320_family(chip)) {
2871 /* Do not limit the period of time that this port can
2872 * be paused for by the remote end or the period of
2873 * time that this port can pause the remote end.
2875 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2876 PORT_PAUSE_CTRL, 0x0000);
2880 /* Port ATU control: disable limiting the number of
2881 * address database entries that this port is allowed
2884 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2885 PORT_ATU_CONTROL, 0x0000);
2886 /* Priority Override: disable DA, SA and VTU priority
2889 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2890 PORT_PRI_OVERRIDE, 0x0000);
2894 /* Port Ethertype: use the Ethertype DSA Ethertype
2897 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2898 PORT_ETH_TYPE, ETH_P_EDSA);
2901 /* Tag Remap: use an identity 802.1p prio -> switch
2904 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2905 PORT_TAG_REGMAP_0123, 0x3210);
2909 /* Tag Remap 2: use an identity 802.1p prio -> switch
2912 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2913 PORT_TAG_REGMAP_4567, 0x7654);
2918 if (mv88e6xxx_6352_family(chip) || mv88e6xxx_6351_family(chip) ||
2919 mv88e6xxx_6165_family(chip) || mv88e6xxx_6097_family(chip) ||
2920 mv88e6xxx_6185_family(chip) || mv88e6xxx_6095_family(chip) ||
2921 mv88e6xxx_6320_family(chip)) {
2922 /* Rate Control: disable ingress rate limiting. */
2923 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port),
2924 PORT_RATE_CONTROL, 0x0001);
2929 /* Port Control 1: disable trunking, disable sending
2930 * learning messages to this port.
2932 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_CONTROL_1,
2937 /* Port based VLAN map: give each port the same default address
2938 * database, and allow bidirectional communication between the
2939 * CPU and DSA port(s), and the other ports.
2941 ret = _mv88e6xxx_port_fid_set(chip, port, 0);
2945 ret = _mv88e6xxx_port_based_vlan_map(chip, port);
2949 /* Default VLAN ID and priority: don't set a default VLAN
2950 * ID, and set the default packet priority to zero.
2952 ret = _mv88e6xxx_reg_write(chip, REG_PORT(port), PORT_DEFAULT_VLAN,
2960 static int mv88e6xxx_g1_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
2964 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_01,
2965 (addr[0] << 8) | addr[1]);
2969 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_23,
2970 (addr[2] << 8) | addr[3]);
2974 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_MAC_45,
2975 (addr[4] << 8) | addr[5]);
2978 static int mv88e6xxx_g1_set_age_time(struct mv88e6xxx_chip *chip,
2981 const unsigned int coeff = chip->info->age_time_coeff;
2982 const unsigned int min = 0x01 * coeff;
2983 const unsigned int max = 0xff * coeff;
2988 if (msecs < min || msecs > max)
2991 /* Round to nearest multiple of coeff */
2992 age_time = (msecs + coeff / 2) / coeff;
2994 err = mv88e6xxx_read(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, &val);
2998 /* AgeTime is 11:4 bits */
3000 val |= age_time << 4;
3002 return mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL, val);
3005 static int mv88e6xxx_set_ageing_time(struct dsa_switch *ds,
3006 unsigned int ageing_time)
3008 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3011 mutex_lock(&chip->reg_lock);
3012 err = mv88e6xxx_g1_set_age_time(chip, ageing_time);
3013 mutex_unlock(&chip->reg_lock);
3018 static int mv88e6xxx_g1_setup(struct mv88e6xxx_chip *chip)
3020 struct dsa_switch *ds = chip->ds;
3021 u32 upstream_port = dsa_upstream_port(ds);
3025 /* Enable the PHY Polling Unit if present, don't discard any packets,
3026 * and mask all interrupt sources.
3029 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU) ||
3030 mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU_ACTIVE))
3031 reg |= GLOBAL_CONTROL_PPU_ENABLE;
3033 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL, reg);
3037 /* Configure the upstream port, and configure it as the port to which
3038 * ingress and egress and ARP monitor frames are to be sent.
3040 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
3041 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
3042 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
3043 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_MONITOR_CONTROL,
3048 /* Disable remote management, and set the switch's DSA device number. */
3049 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_CONTROL_2,
3050 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
3051 (ds->index & 0x1f));
3055 /* Clear all the VTU and STU entries */
3056 err = _mv88e6xxx_vtu_stu_flush(chip);
3060 /* Set the default address aging time to 5 minutes, and
3061 * enable address learn messages to be sent to all message
3064 err = mv88e6xxx_write(chip, REG_GLOBAL, GLOBAL_ATU_CONTROL,
3065 GLOBAL_ATU_CONTROL_LEARN2ALL);
3069 err = mv88e6xxx_g1_set_age_time(chip, 300000);
3073 /* Clear all ATU entries */
3074 err = _mv88e6xxx_atu_flush(chip, 0, true);
3078 /* Configure the IP ToS mapping registers. */
3079 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
3082 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
3085 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
3088 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
3091 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
3094 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
3097 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
3100 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
3104 /* Configure the IEEE 802.1p priority mapping register. */
3105 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
3109 /* Clear the statistics counters for all ports */
3110 err = _mv88e6xxx_reg_write(chip, REG_GLOBAL, GLOBAL_STATS_OP,
3111 GLOBAL_STATS_OP_FLUSH_ALL);
3115 /* Wait for the flush to complete. */
3116 err = _mv88e6xxx_stats_wait(chip);
3123 static int mv88e6xxx_g2_device_mapping_write(struct mv88e6xxx_chip *chip,
3124 int target, int port)
3126 u16 val = (target << 8) | (port & 0xf);
3128 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_DEVICE_MAPPING, val);
3131 static int mv88e6xxx_g2_set_device_mapping(struct mv88e6xxx_chip *chip)
3136 /* Initialize the routing port to the 32 possible target devices */
3137 for (target = 0; target < 32; ++target) {
3140 if (target < DSA_MAX_SWITCHES) {
3141 port = chip->ds->rtable[target];
3142 if (port == DSA_RTABLE_NONE)
3146 err = mv88e6xxx_g2_device_mapping_write(chip, target, port);
3154 static int mv88e6xxx_g2_trunk_mask_write(struct mv88e6xxx_chip *chip, int num,
3155 bool hask, u16 mask)
3157 const u16 port_mask = BIT(chip->info->num_ports) - 1;
3158 u16 val = (num << 12) | (mask & port_mask);
3161 val |= GLOBAL2_TRUNK_MASK_HASK;
3163 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MASK, val);
3166 static int mv88e6xxx_g2_trunk_mapping_write(struct mv88e6xxx_chip *chip, int id,
3169 const u16 port_mask = BIT(chip->info->num_ports) - 1;
3170 u16 val = (id << 11) | (map & port_mask);
3172 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_TRUNK_MAPPING, val);
3175 static int mv88e6xxx_g2_clear_trunk(struct mv88e6xxx_chip *chip)
3177 const u16 port_mask = BIT(chip->info->num_ports) - 1;
3180 /* Clear all eight possible Trunk Mask vectors */
3181 for (i = 0; i < 8; ++i) {
3182 err = mv88e6xxx_g2_trunk_mask_write(chip, i, false, port_mask);
3187 /* Clear all sixteen possible Trunk ID routing vectors */
3188 for (i = 0; i < 16; ++i) {
3189 err = mv88e6xxx_g2_trunk_mapping_write(chip, i, 0);
3197 static int mv88e6xxx_g2_clear_irl(struct mv88e6xxx_chip *chip)
3201 /* Init all Ingress Rate Limit resources of all ports */
3202 for (port = 0; port < chip->info->num_ports; ++port) {
3203 /* XXX newer chips (like 88E6390) have different 2-bit ops */
3204 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
3205 GLOBAL2_IRL_CMD_OP_INIT_ALL |
3210 /* Wait for the operation to complete */
3211 err = _mv88e6xxx_wait(chip, REG_GLOBAL2, GLOBAL2_IRL_CMD,
3212 GLOBAL2_IRL_CMD_BUSY);
3220 /* Indirect write to the Switch MAC/WoL/WoF register */
3221 static int mv88e6xxx_g2_switch_mac_write(struct mv88e6xxx_chip *chip,
3222 unsigned int pointer, u8 data)
3224 u16 val = (pointer << 8) | data;
3226 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MAC, val);
3229 static int mv88e6xxx_g2_set_switch_mac(struct mv88e6xxx_chip *chip, u8 *addr)
3233 for (i = 0; i < 6; i++) {
3234 err = mv88e6xxx_g2_switch_mac_write(chip, i, addr[i]);
3242 static int mv88e6xxx_g2_pot_write(struct mv88e6xxx_chip *chip, int pointer,
3245 u16 val = (pointer << 8) | (data & 0x7);
3247 return mv88e6xxx_update(chip, REG_GLOBAL2, GLOBAL2_PRIO_OVERRIDE, val);
3250 static int mv88e6xxx_g2_clear_pot(struct mv88e6xxx_chip *chip)
3254 /* Clear all sixteen possible Priority Override entries */
3255 for (i = 0; i < 16; i++) {
3256 err = mv88e6xxx_g2_pot_write(chip, i, 0);
3264 static int mv88e6xxx_g2_setup(struct mv88e6xxx_chip *chip)
3269 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X)) {
3270 /* Consider the frames with reserved multicast destination
3271 * addresses matching 01:80:c2:00:00:2x as MGMT.
3273 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_2X,
3279 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X)) {
3280 /* Consider the frames with reserved multicast destination
3281 * addresses matching 01:80:c2:00:00:0x as MGMT.
3283 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X,
3289 /* Ignore removed tag data on doubly tagged packets, disable
3290 * flow control messages, force flow control priority to the
3291 * highest, and send all special multicast frames to the CPU
3292 * port at the highest priority.
3294 reg = GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI | (0x7 << 4);
3295 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_0X) ||
3296 mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_MGMT_EN_2X))
3297 reg |= GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x7;
3298 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT, reg);
3302 /* Program the DSA routing table. */
3303 err = mv88e6xxx_g2_set_device_mapping(chip);
3307 /* Clear all trunk masks and mapping. */
3308 err = mv88e6xxx_g2_clear_trunk(chip);
3312 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_IRL)) {
3313 /* Disable ingress rate limiting by resetting all per port
3314 * ingress rate limit resources to their initial state.
3316 err = mv88e6xxx_g2_clear_irl(chip);
3321 if (mv88e6xxx_has(chip, MV88E6XXX_FLAGS_PVT)) {
3322 /* Initialize Cross-chip Port VLAN Table to reset defaults */
3323 err = mv88e6xxx_write(chip, REG_GLOBAL2, GLOBAL2_PVT_ADDR,
3324 GLOBAL2_PVT_ADDR_OP_INIT_ONES);
3329 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_POT)) {
3330 /* Clear the priority override table. */
3331 err = mv88e6xxx_g2_clear_pot(chip);
3339 static int mv88e6xxx_setup(struct dsa_switch *ds)
3341 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3346 ds->slave_mii_bus = chip->mdio_bus;
3348 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM))
3349 mutex_init(&chip->eeprom_mutex);
3351 mutex_lock(&chip->reg_lock);
3353 err = mv88e6xxx_switch_reset(chip);
3357 /* Setup Switch Port Registers */
3358 for (i = 0; i < chip->info->num_ports; i++) {
3359 err = mv88e6xxx_setup_port(chip, i);
3364 /* Setup Switch Global 1 Registers */
3365 err = mv88e6xxx_g1_setup(chip);
3369 /* Setup Switch Global 2 Registers */
3370 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_GLOBAL2)) {
3371 err = mv88e6xxx_g2_setup(chip);
3377 mutex_unlock(&chip->reg_lock);
3382 static int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
3384 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3387 mutex_lock(&chip->reg_lock);
3389 /* Has an indirect Switch MAC/WoL/WoF register in Global 2? */
3390 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_G2_SWITCH_MAC))
3391 err = mv88e6xxx_g2_set_switch_mac(chip, addr);
3393 err = mv88e6xxx_g1_set_switch_mac(chip, addr);
3395 mutex_unlock(&chip->reg_lock);
3400 static int mv88e6xxx_mdio_page_read(struct dsa_switch *ds, int port, int page,
3403 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3406 mutex_lock(&chip->reg_lock);
3407 ret = _mv88e6xxx_mdio_page_read(chip, port, page, reg);
3408 mutex_unlock(&chip->reg_lock);
3413 static int mv88e6xxx_mdio_page_write(struct dsa_switch *ds, int port, int page,
3416 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3419 mutex_lock(&chip->reg_lock);
3420 ret = _mv88e6xxx_mdio_page_write(chip, port, page, reg, val);
3421 mutex_unlock(&chip->reg_lock);
3426 static int mv88e6xxx_port_to_mdio_addr(struct mv88e6xxx_chip *chip, int port)
3428 if (port >= 0 && port < chip->info->num_ports)
3433 static int mv88e6xxx_mdio_read(struct mii_bus *bus, int port, int regnum)
3435 struct mv88e6xxx_chip *chip = bus->priv;
3436 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
3442 mutex_lock(&chip->reg_lock);
3444 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3445 ret = mv88e6xxx_mdio_read_ppu(chip, addr, regnum);
3446 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3447 ret = mv88e6xxx_mdio_read_indirect(chip, addr, regnum);
3449 ret = mv88e6xxx_mdio_read_direct(chip, addr, regnum);
3451 mutex_unlock(&chip->reg_lock);
3455 static int mv88e6xxx_mdio_write(struct mii_bus *bus, int port, int regnum,
3458 struct mv88e6xxx_chip *chip = bus->priv;
3459 int addr = mv88e6xxx_port_to_mdio_addr(chip, port);
3465 mutex_lock(&chip->reg_lock);
3467 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3468 ret = mv88e6xxx_mdio_write_ppu(chip, addr, regnum, val);
3469 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_SMI_PHY))
3470 ret = mv88e6xxx_mdio_write_indirect(chip, addr, regnum, val);
3472 ret = mv88e6xxx_mdio_write_direct(chip, addr, regnum, val);
3474 mutex_unlock(&chip->reg_lock);
3478 static int mv88e6xxx_mdio_register(struct mv88e6xxx_chip *chip,
3479 struct device_node *np)
3482 struct mii_bus *bus;
3485 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_PPU))
3486 mv88e6xxx_ppu_state_init(chip);
3489 chip->mdio_np = of_get_child_by_name(np, "mdio");
3491 bus = devm_mdiobus_alloc(chip->dev);
3495 bus->priv = (void *)chip;
3497 bus->name = np->full_name;
3498 snprintf(bus->id, MII_BUS_ID_SIZE, "%s", np->full_name);
3500 bus->name = "mv88e6xxx SMI";
3501 snprintf(bus->id, MII_BUS_ID_SIZE, "mv88e6xxx-%d", index++);
3504 bus->read = mv88e6xxx_mdio_read;
3505 bus->write = mv88e6xxx_mdio_write;
3506 bus->parent = chip->dev;
3509 err = of_mdiobus_register(bus, chip->mdio_np);
3511 err = mdiobus_register(bus);
3513 dev_err(chip->dev, "Cannot register MDIO bus (%d)\n", err);
3516 chip->mdio_bus = bus;
3522 of_node_put(chip->mdio_np);
3527 static void mv88e6xxx_mdio_unregister(struct mv88e6xxx_chip *chip)
3530 struct mii_bus *bus = chip->mdio_bus;
3532 mdiobus_unregister(bus);
3535 of_node_put(chip->mdio_np);
3538 #ifdef CONFIG_NET_DSA_HWMON
3540 static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3542 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3548 mutex_lock(&chip->reg_lock);
3550 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x6);
3554 /* Enable temperature sensor */
3555 ret = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
3559 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret | (1 << 5));
3563 /* Wait for temperature to stabilize */
3564 usleep_range(10000, 12000);
3566 val = mv88e6xxx_mdio_read_direct(chip, 0x0, 0x1a);
3572 /* Disable temperature sensor */
3573 ret = mv88e6xxx_mdio_write_direct(chip, 0x0, 0x1a, ret & ~(1 << 5));
3577 *temp = ((val & 0x1f) - 5) * 5;
3580 mv88e6xxx_mdio_write_direct(chip, 0x0, 0x16, 0x0);
3581 mutex_unlock(&chip->reg_lock);
3585 static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3587 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3588 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3593 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 27);
3597 *temp = (ret & 0xff) - 25;
3602 static int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3604 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3606 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP))
3609 if (mv88e6xxx_6320_family(chip) || mv88e6xxx_6352_family(chip))
3610 return mv88e63xx_get_temp(ds, temp);
3612 return mv88e61xx_get_temp(ds, temp);
3615 static int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3617 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3618 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3621 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3626 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3630 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3635 static int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3637 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3638 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3641 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3644 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3647 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3648 return mv88e6xxx_mdio_page_write(ds, phy, 6, 26,
3649 (ret & 0xe0ff) | (temp << 8));
3652 static int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3654 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
3655 int phy = mv88e6xxx_6320_family(chip) ? 3 : 0;
3658 if (!mv88e6xxx_has(chip, MV88E6XXX_FLAG_TEMP_LIMIT))
3663 ret = mv88e6xxx_mdio_page_read(ds, phy, 6, 26);
3667 *alarm = !!(ret & 0x40);
3671 #endif /* CONFIG_NET_DSA_HWMON */
3673 static const struct mv88e6xxx_info mv88e6xxx_table[] = {
3675 .prod_num = PORT_SWITCH_ID_PROD_NUM_6085,
3676 .family = MV88E6XXX_FAMILY_6097,
3677 .name = "Marvell 88E6085",
3678 .num_databases = 4096,
3680 .port_base_addr = 0x10,
3681 .age_time_coeff = 15000,
3682 .flags = MV88E6XXX_FLAGS_FAMILY_6097,
3686 .prod_num = PORT_SWITCH_ID_PROD_NUM_6095,
3687 .family = MV88E6XXX_FAMILY_6095,
3688 .name = "Marvell 88E6095/88E6095F",
3689 .num_databases = 256,
3691 .port_base_addr = 0x10,
3692 .age_time_coeff = 15000,
3693 .flags = MV88E6XXX_FLAGS_FAMILY_6095,
3697 .prod_num = PORT_SWITCH_ID_PROD_NUM_6123,
3698 .family = MV88E6XXX_FAMILY_6165,
3699 .name = "Marvell 88E6123",
3700 .num_databases = 4096,
3702 .port_base_addr = 0x10,
3703 .age_time_coeff = 15000,
3704 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3708 .prod_num = PORT_SWITCH_ID_PROD_NUM_6131,
3709 .family = MV88E6XXX_FAMILY_6185,
3710 .name = "Marvell 88E6131",
3711 .num_databases = 256,
3713 .port_base_addr = 0x10,
3714 .age_time_coeff = 15000,
3715 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3719 .prod_num = PORT_SWITCH_ID_PROD_NUM_6161,
3720 .family = MV88E6XXX_FAMILY_6165,
3721 .name = "Marvell 88E6161",
3722 .num_databases = 4096,
3724 .port_base_addr = 0x10,
3725 .age_time_coeff = 15000,
3726 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3730 .prod_num = PORT_SWITCH_ID_PROD_NUM_6165,
3731 .family = MV88E6XXX_FAMILY_6165,
3732 .name = "Marvell 88E6165",
3733 .num_databases = 4096,
3735 .port_base_addr = 0x10,
3736 .age_time_coeff = 15000,
3737 .flags = MV88E6XXX_FLAGS_FAMILY_6165,
3741 .prod_num = PORT_SWITCH_ID_PROD_NUM_6171,
3742 .family = MV88E6XXX_FAMILY_6351,
3743 .name = "Marvell 88E6171",
3744 .num_databases = 4096,
3746 .port_base_addr = 0x10,
3747 .age_time_coeff = 15000,
3748 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3752 .prod_num = PORT_SWITCH_ID_PROD_NUM_6172,
3753 .family = MV88E6XXX_FAMILY_6352,
3754 .name = "Marvell 88E6172",
3755 .num_databases = 4096,
3757 .port_base_addr = 0x10,
3758 .age_time_coeff = 15000,
3759 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3763 .prod_num = PORT_SWITCH_ID_PROD_NUM_6175,
3764 .family = MV88E6XXX_FAMILY_6351,
3765 .name = "Marvell 88E6175",
3766 .num_databases = 4096,
3768 .port_base_addr = 0x10,
3769 .age_time_coeff = 15000,
3770 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3774 .prod_num = PORT_SWITCH_ID_PROD_NUM_6176,
3775 .family = MV88E6XXX_FAMILY_6352,
3776 .name = "Marvell 88E6176",
3777 .num_databases = 4096,
3779 .port_base_addr = 0x10,
3780 .age_time_coeff = 15000,
3781 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3785 .prod_num = PORT_SWITCH_ID_PROD_NUM_6185,
3786 .family = MV88E6XXX_FAMILY_6185,
3787 .name = "Marvell 88E6185",
3788 .num_databases = 256,
3790 .port_base_addr = 0x10,
3791 .age_time_coeff = 15000,
3792 .flags = MV88E6XXX_FLAGS_FAMILY_6185,
3796 .prod_num = PORT_SWITCH_ID_PROD_NUM_6240,
3797 .family = MV88E6XXX_FAMILY_6352,
3798 .name = "Marvell 88E6240",
3799 .num_databases = 4096,
3801 .port_base_addr = 0x10,
3802 .age_time_coeff = 15000,
3803 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3807 .prod_num = PORT_SWITCH_ID_PROD_NUM_6320,
3808 .family = MV88E6XXX_FAMILY_6320,
3809 .name = "Marvell 88E6320",
3810 .num_databases = 4096,
3812 .port_base_addr = 0x10,
3813 .age_time_coeff = 15000,
3814 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3818 .prod_num = PORT_SWITCH_ID_PROD_NUM_6321,
3819 .family = MV88E6XXX_FAMILY_6320,
3820 .name = "Marvell 88E6321",
3821 .num_databases = 4096,
3823 .port_base_addr = 0x10,
3824 .age_time_coeff = 15000,
3825 .flags = MV88E6XXX_FLAGS_FAMILY_6320,
3829 .prod_num = PORT_SWITCH_ID_PROD_NUM_6350,
3830 .family = MV88E6XXX_FAMILY_6351,
3831 .name = "Marvell 88E6350",
3832 .num_databases = 4096,
3834 .port_base_addr = 0x10,
3835 .age_time_coeff = 15000,
3836 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3840 .prod_num = PORT_SWITCH_ID_PROD_NUM_6351,
3841 .family = MV88E6XXX_FAMILY_6351,
3842 .name = "Marvell 88E6351",
3843 .num_databases = 4096,
3845 .port_base_addr = 0x10,
3846 .age_time_coeff = 15000,
3847 .flags = MV88E6XXX_FLAGS_FAMILY_6351,
3851 .prod_num = PORT_SWITCH_ID_PROD_NUM_6352,
3852 .family = MV88E6XXX_FAMILY_6352,
3853 .name = "Marvell 88E6352",
3854 .num_databases = 4096,
3856 .port_base_addr = 0x10,
3857 .age_time_coeff = 15000,
3858 .flags = MV88E6XXX_FLAGS_FAMILY_6352,
3862 static const struct mv88e6xxx_info *mv88e6xxx_lookup_info(unsigned int prod_num)
3866 for (i = 0; i < ARRAY_SIZE(mv88e6xxx_table); ++i)
3867 if (mv88e6xxx_table[i].prod_num == prod_num)
3868 return &mv88e6xxx_table[i];
3873 static int mv88e6xxx_detect(struct mv88e6xxx_chip *chip)
3875 const struct mv88e6xxx_info *info;
3876 int id, prod_num, rev;
3878 id = mv88e6xxx_reg_read(chip, chip->info->port_base_addr,
3883 prod_num = (id & 0xfff0) >> 4;
3886 info = mv88e6xxx_lookup_info(prod_num);
3890 /* Update the compatible info with the probed one */
3893 dev_info(chip->dev, "switch 0x%x detected: %s, revision %u\n",
3894 chip->info->prod_num, chip->info->name, rev);
3899 static struct mv88e6xxx_chip *mv88e6xxx_alloc_chip(struct device *dev)
3901 struct mv88e6xxx_chip *chip;
3903 chip = devm_kzalloc(dev, sizeof(*chip), GFP_KERNEL);
3909 mutex_init(&chip->reg_lock);
3914 static int mv88e6xxx_smi_init(struct mv88e6xxx_chip *chip,
3915 struct mii_bus *bus, int sw_addr)
3917 /* ADDR[0] pin is unavailable externally and considered zero */
3922 chip->smi_ops = &mv88e6xxx_smi_single_chip_ops;
3923 else if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_MULTI_CHIP))
3924 chip->smi_ops = &mv88e6xxx_smi_multi_chip_ops;
3929 chip->sw_addr = sw_addr;
3934 static const char *mv88e6xxx_drv_probe(struct device *dsa_dev,
3935 struct device *host_dev, int sw_addr,
3938 struct mv88e6xxx_chip *chip;
3939 struct mii_bus *bus;
3942 bus = dsa_host_dev_to_mii_bus(host_dev);
3946 chip = mv88e6xxx_alloc_chip(dsa_dev);
3950 /* Legacy SMI probing will only support chips similar to 88E6085 */
3951 chip->info = &mv88e6xxx_table[MV88E6085];
3953 err = mv88e6xxx_smi_init(chip, bus, sw_addr);
3957 err = mv88e6xxx_detect(chip);
3961 err = mv88e6xxx_mdio_register(chip, NULL);
3967 return chip->info->name;
3969 devm_kfree(dsa_dev, chip);
3974 static struct dsa_switch_driver mv88e6xxx_switch_driver = {
3975 .tag_protocol = DSA_TAG_PROTO_EDSA,
3976 .probe = mv88e6xxx_drv_probe,
3977 .setup = mv88e6xxx_setup,
3978 .set_addr = mv88e6xxx_set_addr,
3979 .adjust_link = mv88e6xxx_adjust_link,
3980 .get_strings = mv88e6xxx_get_strings,
3981 .get_ethtool_stats = mv88e6xxx_get_ethtool_stats,
3982 .get_sset_count = mv88e6xxx_get_sset_count,
3983 .set_eee = mv88e6xxx_set_eee,
3984 .get_eee = mv88e6xxx_get_eee,
3985 #ifdef CONFIG_NET_DSA_HWMON
3986 .get_temp = mv88e6xxx_get_temp,
3987 .get_temp_limit = mv88e6xxx_get_temp_limit,
3988 .set_temp_limit = mv88e6xxx_set_temp_limit,
3989 .get_temp_alarm = mv88e6xxx_get_temp_alarm,
3991 .get_eeprom_len = mv88e6xxx_get_eeprom_len,
3992 .get_eeprom = mv88e6xxx_get_eeprom,
3993 .set_eeprom = mv88e6xxx_set_eeprom,
3994 .get_regs_len = mv88e6xxx_get_regs_len,
3995 .get_regs = mv88e6xxx_get_regs,
3996 .set_ageing_time = mv88e6xxx_set_ageing_time,
3997 .port_bridge_join = mv88e6xxx_port_bridge_join,
3998 .port_bridge_leave = mv88e6xxx_port_bridge_leave,
3999 .port_stp_state_set = mv88e6xxx_port_stp_state_set,
4000 .port_vlan_filtering = mv88e6xxx_port_vlan_filtering,
4001 .port_vlan_prepare = mv88e6xxx_port_vlan_prepare,
4002 .port_vlan_add = mv88e6xxx_port_vlan_add,
4003 .port_vlan_del = mv88e6xxx_port_vlan_del,
4004 .port_vlan_dump = mv88e6xxx_port_vlan_dump,
4005 .port_fdb_prepare = mv88e6xxx_port_fdb_prepare,
4006 .port_fdb_add = mv88e6xxx_port_fdb_add,
4007 .port_fdb_del = mv88e6xxx_port_fdb_del,
4008 .port_fdb_dump = mv88e6xxx_port_fdb_dump,
4011 static int mv88e6xxx_register_switch(struct mv88e6xxx_chip *chip,
4012 struct device_node *np)
4014 struct device *dev = chip->dev;
4015 struct dsa_switch *ds;
4017 ds = devm_kzalloc(dev, sizeof(*ds), GFP_KERNEL);
4023 ds->drv = &mv88e6xxx_switch_driver;
4025 dev_set_drvdata(dev, ds);
4027 return dsa_register_switch(ds, np);
4030 static void mv88e6xxx_unregister_switch(struct mv88e6xxx_chip *chip)
4032 dsa_unregister_switch(chip->ds);
4035 static int mv88e6xxx_probe(struct mdio_device *mdiodev)
4037 struct device *dev = &mdiodev->dev;
4038 struct device_node *np = dev->of_node;
4039 const struct mv88e6xxx_info *compat_info;
4040 struct mv88e6xxx_chip *chip;
4044 compat_info = of_device_get_match_data(dev);
4048 chip = mv88e6xxx_alloc_chip(dev);
4052 chip->info = compat_info;
4054 err = mv88e6xxx_smi_init(chip, mdiodev->bus, mdiodev->addr);
4058 err = mv88e6xxx_detect(chip);
4062 chip->reset = devm_gpiod_get_optional(dev, "reset", GPIOD_ASIS);
4063 if (IS_ERR(chip->reset))
4064 return PTR_ERR(chip->reset);
4066 if (mv88e6xxx_has(chip, MV88E6XXX_FLAG_EEPROM) &&
4067 !of_property_read_u32(np, "eeprom-length", &eeprom_len))
4068 chip->eeprom_len = eeprom_len;
4070 err = mv88e6xxx_mdio_register(chip, np);
4074 err = mv88e6xxx_register_switch(chip, np);
4076 mv88e6xxx_mdio_unregister(chip);
4083 static void mv88e6xxx_remove(struct mdio_device *mdiodev)
4085 struct dsa_switch *ds = dev_get_drvdata(&mdiodev->dev);
4086 struct mv88e6xxx_chip *chip = ds_to_priv(ds);
4088 mv88e6xxx_unregister_switch(chip);
4089 mv88e6xxx_mdio_unregister(chip);
4092 static const struct of_device_id mv88e6xxx_of_match[] = {
4094 .compatible = "marvell,mv88e6085",
4095 .data = &mv88e6xxx_table[MV88E6085],
4100 MODULE_DEVICE_TABLE(of, mv88e6xxx_of_match);
4102 static struct mdio_driver mv88e6xxx_driver = {
4103 .probe = mv88e6xxx_probe,
4104 .remove = mv88e6xxx_remove,
4106 .name = "mv88e6085",
4107 .of_match_table = mv88e6xxx_of_match,
4111 static int __init mv88e6xxx_init(void)
4113 register_switch_driver(&mv88e6xxx_switch_driver);
4114 return mdio_driver_register(&mv88e6xxx_driver);
4116 module_init(mv88e6xxx_init);
4118 static void __exit mv88e6xxx_cleanup(void)
4120 mdio_driver_unregister(&mv88e6xxx_driver);
4121 unregister_switch_driver(&mv88e6xxx_switch_driver);
4123 module_exit(mv88e6xxx_cleanup);
4125 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
4126 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
4127 MODULE_LICENSE("GPL");