2 * net/dsa/mv88e6xxx.c - Marvell 88e6xxx switch chip support
3 * Copyright (c) 2008 Marvell Semiconductor
5 * Copyright (c) 2015 CMC Electronics, Inc.
6 * Added support for VLAN Table Unit operations
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
14 #include <linux/delay.h>
15 #include <linux/etherdevice.h>
16 #include <linux/ethtool.h>
17 #include <linux/if_bridge.h>
18 #include <linux/jiffies.h>
19 #include <linux/list.h>
20 #include <linux/module.h>
21 #include <linux/netdevice.h>
22 #include <linux/gpio/consumer.h>
23 #include <linux/phy.h>
25 #include <net/switchdev.h>
26 #include "mv88e6xxx.h"
28 static void assert_smi_lock(struct mv88e6xxx_priv_state *ps)
30 if (unlikely(!mutex_is_locked(&ps->smi_mutex))) {
31 dev_err(ps->dev, "SMI lock not held!\n");
36 /* If the switch's ADDR[4:0] strap pins are strapped to zero, it will
37 * use all 32 SMI bus addresses on its SMI bus, and all switch registers
38 * will be directly accessible on some {device address,register address}
39 * pair. If the ADDR[4:0] pins are not strapped to zero, the switch
40 * will only respond to SMI transactions to that specific address, and
41 * an indirect addressing mechanism needs to be used to access its
44 static int mv88e6xxx_reg_wait_ready(struct mii_bus *bus, int sw_addr)
49 for (i = 0; i < 16; i++) {
50 ret = mdiobus_read_nested(bus, sw_addr, SMI_CMD);
54 if ((ret & SMI_CMD_BUSY) == 0)
61 static int __mv88e6xxx_reg_read(struct mii_bus *bus, int sw_addr, int addr,
67 return mdiobus_read_nested(bus, addr, reg);
69 /* Wait for the bus to become free. */
70 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
74 /* Transmit the read command. */
75 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
76 SMI_CMD_OP_22_READ | (addr << 5) | reg);
80 /* Wait for the read command to complete. */
81 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
86 ret = mdiobus_read_nested(bus, sw_addr, SMI_DATA);
93 static int _mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps,
100 ret = __mv88e6xxx_reg_read(ps->bus, ps->sw_addr, addr, reg);
104 dev_dbg(ps->dev, "<- addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
110 int mv88e6xxx_reg_read(struct mv88e6xxx_priv_state *ps, int addr, int reg)
114 mutex_lock(&ps->smi_mutex);
115 ret = _mv88e6xxx_reg_read(ps, addr, reg);
116 mutex_unlock(&ps->smi_mutex);
121 static int __mv88e6xxx_reg_write(struct mii_bus *bus, int sw_addr, int addr,
127 return mdiobus_write_nested(bus, addr, reg, val);
129 /* Wait for the bus to become free. */
130 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
134 /* Transmit the data to write. */
135 ret = mdiobus_write_nested(bus, sw_addr, SMI_DATA, val);
139 /* Transmit the write command. */
140 ret = mdiobus_write_nested(bus, sw_addr, SMI_CMD,
141 SMI_CMD_OP_22_WRITE | (addr << 5) | reg);
145 /* Wait for the write command to complete. */
146 ret = mv88e6xxx_reg_wait_ready(bus, sw_addr);
153 static int _mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
158 dev_dbg(ps->dev, "-> addr: 0x%.2x reg: 0x%.2x val: 0x%.4x\n",
161 return __mv88e6xxx_reg_write(ps->bus, ps->sw_addr, addr, reg, val);
164 int mv88e6xxx_reg_write(struct mv88e6xxx_priv_state *ps, int addr,
169 mutex_lock(&ps->smi_mutex);
170 ret = _mv88e6xxx_reg_write(ps, addr, reg, val);
171 mutex_unlock(&ps->smi_mutex);
176 static int mv88e6xxx_set_addr_direct(struct dsa_switch *ds, u8 *addr)
178 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
181 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_01,
182 (addr[0] << 8) | addr[1]);
186 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_23,
187 (addr[2] << 8) | addr[3]);
191 return mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MAC_45,
192 (addr[4] << 8) | addr[5]);
195 static int mv88e6xxx_set_addr_indirect(struct dsa_switch *ds, u8 *addr)
197 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
201 for (i = 0; i < 6; i++) {
204 /* Write the MAC address byte. */
205 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MAC,
206 GLOBAL2_SWITCH_MAC_BUSY |
211 /* Wait for the write to complete. */
212 for (j = 0; j < 16; j++) {
213 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2,
218 if ((ret & GLOBAL2_SWITCH_MAC_BUSY) == 0)
228 int mv88e6xxx_set_addr(struct dsa_switch *ds, u8 *addr)
230 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
232 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SWITCH_MAC))
233 return mv88e6xxx_set_addr_indirect(ds, addr);
235 return mv88e6xxx_set_addr_direct(ds, addr);
238 static int _mv88e6xxx_phy_read(struct mv88e6xxx_priv_state *ps, int addr,
242 return _mv88e6xxx_reg_read(ps, addr, regnum);
246 static int _mv88e6xxx_phy_write(struct mv88e6xxx_priv_state *ps, int addr,
250 return _mv88e6xxx_reg_write(ps, addr, regnum, val);
254 static int mv88e6xxx_ppu_disable(struct mv88e6xxx_priv_state *ps)
257 unsigned long timeout;
259 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
263 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
264 ret & ~GLOBAL_CONTROL_PPU_ENABLE);
268 timeout = jiffies + 1 * HZ;
269 while (time_before(jiffies, timeout)) {
270 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
274 usleep_range(1000, 2000);
275 if ((ret & GLOBAL_STATUS_PPU_MASK) !=
276 GLOBAL_STATUS_PPU_POLLING)
283 static int mv88e6xxx_ppu_enable(struct mv88e6xxx_priv_state *ps)
286 unsigned long timeout;
288 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_CONTROL);
292 err = mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL,
293 ret | GLOBAL_CONTROL_PPU_ENABLE);
297 timeout = jiffies + 1 * HZ;
298 while (time_before(jiffies, timeout)) {
299 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATUS);
303 usleep_range(1000, 2000);
304 if ((ret & GLOBAL_STATUS_PPU_MASK) ==
305 GLOBAL_STATUS_PPU_POLLING)
312 static void mv88e6xxx_ppu_reenable_work(struct work_struct *ugly)
314 struct mv88e6xxx_priv_state *ps;
316 ps = container_of(ugly, struct mv88e6xxx_priv_state, ppu_work);
317 if (mutex_trylock(&ps->ppu_mutex)) {
318 if (mv88e6xxx_ppu_enable(ps) == 0)
319 ps->ppu_disabled = 0;
320 mutex_unlock(&ps->ppu_mutex);
324 static void mv88e6xxx_ppu_reenable_timer(unsigned long _ps)
326 struct mv88e6xxx_priv_state *ps = (void *)_ps;
328 schedule_work(&ps->ppu_work);
331 static int mv88e6xxx_ppu_access_get(struct mv88e6xxx_priv_state *ps)
335 mutex_lock(&ps->ppu_mutex);
337 /* If the PHY polling unit is enabled, disable it so that
338 * we can access the PHY registers. If it was already
339 * disabled, cancel the timer that is going to re-enable
342 if (!ps->ppu_disabled) {
343 ret = mv88e6xxx_ppu_disable(ps);
345 mutex_unlock(&ps->ppu_mutex);
348 ps->ppu_disabled = 1;
350 del_timer(&ps->ppu_timer);
357 static void mv88e6xxx_ppu_access_put(struct mv88e6xxx_priv_state *ps)
359 /* Schedule a timer to re-enable the PHY polling unit. */
360 mod_timer(&ps->ppu_timer, jiffies + msecs_to_jiffies(10));
361 mutex_unlock(&ps->ppu_mutex);
364 void mv88e6xxx_ppu_state_init(struct mv88e6xxx_priv_state *ps)
366 mutex_init(&ps->ppu_mutex);
367 INIT_WORK(&ps->ppu_work, mv88e6xxx_ppu_reenable_work);
368 init_timer(&ps->ppu_timer);
369 ps->ppu_timer.data = (unsigned long)ps;
370 ps->ppu_timer.function = mv88e6xxx_ppu_reenable_timer;
373 static int mv88e6xxx_phy_read_ppu(struct mv88e6xxx_priv_state *ps, int addr,
378 ret = mv88e6xxx_ppu_access_get(ps);
380 ret = _mv88e6xxx_reg_read(ps, addr, regnum);
381 mv88e6xxx_ppu_access_put(ps);
387 static int mv88e6xxx_phy_write_ppu(struct mv88e6xxx_priv_state *ps, int addr,
392 ret = mv88e6xxx_ppu_access_get(ps);
394 ret = _mv88e6xxx_reg_write(ps, addr, regnum, val);
395 mv88e6xxx_ppu_access_put(ps);
401 static bool mv88e6xxx_6065_family(struct mv88e6xxx_priv_state *ps)
403 return ps->info->family == MV88E6XXX_FAMILY_6065;
406 static bool mv88e6xxx_6095_family(struct mv88e6xxx_priv_state *ps)
408 return ps->info->family == MV88E6XXX_FAMILY_6095;
411 static bool mv88e6xxx_6097_family(struct mv88e6xxx_priv_state *ps)
413 return ps->info->family == MV88E6XXX_FAMILY_6097;
416 static bool mv88e6xxx_6165_family(struct mv88e6xxx_priv_state *ps)
418 return ps->info->family == MV88E6XXX_FAMILY_6165;
421 static bool mv88e6xxx_6185_family(struct mv88e6xxx_priv_state *ps)
423 return ps->info->family == MV88E6XXX_FAMILY_6185;
426 static bool mv88e6xxx_6320_family(struct mv88e6xxx_priv_state *ps)
428 return ps->info->family == MV88E6XXX_FAMILY_6320;
431 static bool mv88e6xxx_6351_family(struct mv88e6xxx_priv_state *ps)
433 return ps->info->family == MV88E6XXX_FAMILY_6351;
436 static bool mv88e6xxx_6352_family(struct mv88e6xxx_priv_state *ps)
438 return ps->info->family == MV88E6XXX_FAMILY_6352;
441 static unsigned int mv88e6xxx_num_databases(struct mv88e6xxx_priv_state *ps)
443 return ps->info->num_databases;
446 static bool mv88e6xxx_has_fid_reg(struct mv88e6xxx_priv_state *ps)
448 /* Does the device have dedicated FID registers for ATU and VTU ops? */
449 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
450 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps))
456 static bool mv88e6xxx_has_stu(struct mv88e6xxx_priv_state *ps)
458 /* Does the device have STU and dedicated SID registers for VTU ops? */
459 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
460 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps))
466 /* We expect the switch to perform auto negotiation if there is a real
467 * phy. However, in the case of a fixed link phy, we force the port
468 * settings from the fixed link settings.
470 void mv88e6xxx_adjust_link(struct dsa_switch *ds, int port,
471 struct phy_device *phydev)
473 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
477 if (!phy_is_pseudo_fixed_link(phydev))
480 mutex_lock(&ps->smi_mutex);
482 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
486 reg = ret & ~(PORT_PCS_CTRL_LINK_UP |
487 PORT_PCS_CTRL_FORCE_LINK |
488 PORT_PCS_CTRL_DUPLEX_FULL |
489 PORT_PCS_CTRL_FORCE_DUPLEX |
490 PORT_PCS_CTRL_UNFORCED);
492 reg |= PORT_PCS_CTRL_FORCE_LINK;
494 reg |= PORT_PCS_CTRL_LINK_UP;
496 if (mv88e6xxx_6065_family(ps) && phydev->speed > SPEED_100)
499 switch (phydev->speed) {
501 reg |= PORT_PCS_CTRL_1000;
504 reg |= PORT_PCS_CTRL_100;
507 reg |= PORT_PCS_CTRL_10;
510 pr_info("Unknown speed");
514 reg |= PORT_PCS_CTRL_FORCE_DUPLEX;
515 if (phydev->duplex == DUPLEX_FULL)
516 reg |= PORT_PCS_CTRL_DUPLEX_FULL;
518 if ((mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps)) &&
519 (port >= ps->info->num_ports - 2)) {
520 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
521 reg |= PORT_PCS_CTRL_RGMII_DELAY_RXCLK;
522 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
523 reg |= PORT_PCS_CTRL_RGMII_DELAY_TXCLK;
524 if (phydev->interface == PHY_INTERFACE_MODE_RGMII_ID)
525 reg |= (PORT_PCS_CTRL_RGMII_DELAY_RXCLK |
526 PORT_PCS_CTRL_RGMII_DELAY_TXCLK);
528 _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_PCS_CTRL, reg);
531 mutex_unlock(&ps->smi_mutex);
534 static int _mv88e6xxx_stats_wait(struct mv88e6xxx_priv_state *ps)
539 for (i = 0; i < 10; i++) {
540 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_OP);
541 if ((ret & GLOBAL_STATS_OP_BUSY) == 0)
548 static int _mv88e6xxx_stats_snapshot(struct mv88e6xxx_priv_state *ps,
553 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
554 port = (port + 1) << 5;
556 /* Snapshot the hardware statistics counters for this port. */
557 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
558 GLOBAL_STATS_OP_CAPTURE_PORT |
559 GLOBAL_STATS_OP_HIST_RX_TX | port);
563 /* Wait for the snapshotting to complete. */
564 ret = _mv88e6xxx_stats_wait(ps);
571 static void _mv88e6xxx_stats_read(struct mv88e6xxx_priv_state *ps,
579 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
580 GLOBAL_STATS_OP_READ_CAPTURED |
581 GLOBAL_STATS_OP_HIST_RX_TX | stat);
585 ret = _mv88e6xxx_stats_wait(ps);
589 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_32);
595 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_STATS_COUNTER_01);
602 static struct mv88e6xxx_hw_stat mv88e6xxx_hw_stats[] = {
603 { "in_good_octets", 8, 0x00, BANK0, },
604 { "in_bad_octets", 4, 0x02, BANK0, },
605 { "in_unicast", 4, 0x04, BANK0, },
606 { "in_broadcasts", 4, 0x06, BANK0, },
607 { "in_multicasts", 4, 0x07, BANK0, },
608 { "in_pause", 4, 0x16, BANK0, },
609 { "in_undersize", 4, 0x18, BANK0, },
610 { "in_fragments", 4, 0x19, BANK0, },
611 { "in_oversize", 4, 0x1a, BANK0, },
612 { "in_jabber", 4, 0x1b, BANK0, },
613 { "in_rx_error", 4, 0x1c, BANK0, },
614 { "in_fcs_error", 4, 0x1d, BANK0, },
615 { "out_octets", 8, 0x0e, BANK0, },
616 { "out_unicast", 4, 0x10, BANK0, },
617 { "out_broadcasts", 4, 0x13, BANK0, },
618 { "out_multicasts", 4, 0x12, BANK0, },
619 { "out_pause", 4, 0x15, BANK0, },
620 { "excessive", 4, 0x11, BANK0, },
621 { "collisions", 4, 0x1e, BANK0, },
622 { "deferred", 4, 0x05, BANK0, },
623 { "single", 4, 0x14, BANK0, },
624 { "multiple", 4, 0x17, BANK0, },
625 { "out_fcs_error", 4, 0x03, BANK0, },
626 { "late", 4, 0x1f, BANK0, },
627 { "hist_64bytes", 4, 0x08, BANK0, },
628 { "hist_65_127bytes", 4, 0x09, BANK0, },
629 { "hist_128_255bytes", 4, 0x0a, BANK0, },
630 { "hist_256_511bytes", 4, 0x0b, BANK0, },
631 { "hist_512_1023bytes", 4, 0x0c, BANK0, },
632 { "hist_1024_max_bytes", 4, 0x0d, BANK0, },
633 { "sw_in_discards", 4, 0x10, PORT, },
634 { "sw_in_filtered", 2, 0x12, PORT, },
635 { "sw_out_filtered", 2, 0x13, PORT, },
636 { "in_discards", 4, 0x00 | GLOBAL_STATS_OP_BANK_1, BANK1, },
637 { "in_filtered", 4, 0x01 | GLOBAL_STATS_OP_BANK_1, BANK1, },
638 { "in_accepted", 4, 0x02 | GLOBAL_STATS_OP_BANK_1, BANK1, },
639 { "in_bad_accepted", 4, 0x03 | GLOBAL_STATS_OP_BANK_1, BANK1, },
640 { "in_good_avb_class_a", 4, 0x04 | GLOBAL_STATS_OP_BANK_1, BANK1, },
641 { "in_good_avb_class_b", 4, 0x05 | GLOBAL_STATS_OP_BANK_1, BANK1, },
642 { "in_bad_avb_class_a", 4, 0x06 | GLOBAL_STATS_OP_BANK_1, BANK1, },
643 { "in_bad_avb_class_b", 4, 0x07 | GLOBAL_STATS_OP_BANK_1, BANK1, },
644 { "tcam_counter_0", 4, 0x08 | GLOBAL_STATS_OP_BANK_1, BANK1, },
645 { "tcam_counter_1", 4, 0x09 | GLOBAL_STATS_OP_BANK_1, BANK1, },
646 { "tcam_counter_2", 4, 0x0a | GLOBAL_STATS_OP_BANK_1, BANK1, },
647 { "tcam_counter_3", 4, 0x0b | GLOBAL_STATS_OP_BANK_1, BANK1, },
648 { "in_da_unknown", 4, 0x0e | GLOBAL_STATS_OP_BANK_1, BANK1, },
649 { "in_management", 4, 0x0f | GLOBAL_STATS_OP_BANK_1, BANK1, },
650 { "out_queue_0", 4, 0x10 | GLOBAL_STATS_OP_BANK_1, BANK1, },
651 { "out_queue_1", 4, 0x11 | GLOBAL_STATS_OP_BANK_1, BANK1, },
652 { "out_queue_2", 4, 0x12 | GLOBAL_STATS_OP_BANK_1, BANK1, },
653 { "out_queue_3", 4, 0x13 | GLOBAL_STATS_OP_BANK_1, BANK1, },
654 { "out_queue_4", 4, 0x14 | GLOBAL_STATS_OP_BANK_1, BANK1, },
655 { "out_queue_5", 4, 0x15 | GLOBAL_STATS_OP_BANK_1, BANK1, },
656 { "out_queue_6", 4, 0x16 | GLOBAL_STATS_OP_BANK_1, BANK1, },
657 { "out_queue_7", 4, 0x17 | GLOBAL_STATS_OP_BANK_1, BANK1, },
658 { "out_cut_through", 4, 0x18 | GLOBAL_STATS_OP_BANK_1, BANK1, },
659 { "out_octets_a", 4, 0x1a | GLOBAL_STATS_OP_BANK_1, BANK1, },
660 { "out_octets_b", 4, 0x1b | GLOBAL_STATS_OP_BANK_1, BANK1, },
661 { "out_management", 4, 0x1f | GLOBAL_STATS_OP_BANK_1, BANK1, },
664 static bool mv88e6xxx_has_stat(struct mv88e6xxx_priv_state *ps,
665 struct mv88e6xxx_hw_stat *stat)
667 switch (stat->type) {
671 return mv88e6xxx_6320_family(ps);
673 return mv88e6xxx_6095_family(ps) ||
674 mv88e6xxx_6185_family(ps) ||
675 mv88e6xxx_6097_family(ps) ||
676 mv88e6xxx_6165_family(ps) ||
677 mv88e6xxx_6351_family(ps) ||
678 mv88e6xxx_6352_family(ps);
683 static uint64_t _mv88e6xxx_get_ethtool_stat(struct mv88e6xxx_priv_state *ps,
684 struct mv88e6xxx_hw_stat *s,
694 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), s->reg);
699 if (s->sizeof_stat == 4) {
700 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port),
709 _mv88e6xxx_stats_read(ps, s->reg, &low);
710 if (s->sizeof_stat == 8)
711 _mv88e6xxx_stats_read(ps, s->reg + 1, &high);
713 value = (((u64)high) << 16) | low;
717 void mv88e6xxx_get_strings(struct dsa_switch *ds, int port, uint8_t *data)
719 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
720 struct mv88e6xxx_hw_stat *stat;
723 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
724 stat = &mv88e6xxx_hw_stats[i];
725 if (mv88e6xxx_has_stat(ps, stat)) {
726 memcpy(data + j * ETH_GSTRING_LEN, stat->string,
733 int mv88e6xxx_get_sset_count(struct dsa_switch *ds)
735 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
736 struct mv88e6xxx_hw_stat *stat;
739 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
740 stat = &mv88e6xxx_hw_stats[i];
741 if (mv88e6xxx_has_stat(ps, stat))
748 mv88e6xxx_get_ethtool_stats(struct dsa_switch *ds,
749 int port, uint64_t *data)
751 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
752 struct mv88e6xxx_hw_stat *stat;
756 mutex_lock(&ps->smi_mutex);
758 ret = _mv88e6xxx_stats_snapshot(ps, port);
760 mutex_unlock(&ps->smi_mutex);
763 for (i = 0, j = 0; i < ARRAY_SIZE(mv88e6xxx_hw_stats); i++) {
764 stat = &mv88e6xxx_hw_stats[i];
765 if (mv88e6xxx_has_stat(ps, stat)) {
766 data[j] = _mv88e6xxx_get_ethtool_stat(ps, stat, port);
771 mutex_unlock(&ps->smi_mutex);
774 int mv88e6xxx_get_regs_len(struct dsa_switch *ds, int port)
776 return 32 * sizeof(u16);
779 void mv88e6xxx_get_regs(struct dsa_switch *ds, int port,
780 struct ethtool_regs *regs, void *_p)
782 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
788 memset(p, 0xff, 32 * sizeof(u16));
790 mutex_lock(&ps->smi_mutex);
792 for (i = 0; i < 32; i++) {
795 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), i);
800 mutex_unlock(&ps->smi_mutex);
803 static int _mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg, int offset,
806 unsigned long timeout = jiffies + HZ / 10;
808 while (time_before(jiffies, timeout)) {
811 ret = _mv88e6xxx_reg_read(ps, reg, offset);
817 usleep_range(1000, 2000);
822 static int mv88e6xxx_wait(struct mv88e6xxx_priv_state *ps, int reg,
823 int offset, u16 mask)
827 mutex_lock(&ps->smi_mutex);
828 ret = _mv88e6xxx_wait(ps, reg, offset, mask);
829 mutex_unlock(&ps->smi_mutex);
834 static int _mv88e6xxx_phy_wait(struct mv88e6xxx_priv_state *ps)
836 return _mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
837 GLOBAL2_SMI_OP_BUSY);
840 static int mv88e6xxx_eeprom_load_wait(struct dsa_switch *ds)
842 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
844 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
845 GLOBAL2_EEPROM_OP_LOAD);
848 static int mv88e6xxx_eeprom_busy_wait(struct dsa_switch *ds)
850 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
852 return mv88e6xxx_wait(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
853 GLOBAL2_EEPROM_OP_BUSY);
856 static int mv88e6xxx_read_eeprom_word(struct dsa_switch *ds, int addr)
858 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
861 mutex_lock(&ps->eeprom_mutex);
863 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
864 GLOBAL2_EEPROM_OP_READ |
865 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
869 ret = mv88e6xxx_eeprom_busy_wait(ds);
873 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA);
875 mutex_unlock(&ps->eeprom_mutex);
879 int mv88e6xxx_get_eeprom(struct dsa_switch *ds, struct ethtool_eeprom *eeprom,
882 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
887 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
890 offset = eeprom->offset;
894 eeprom->magic = 0xc3ec4951;
896 ret = mv88e6xxx_eeprom_load_wait(ds);
903 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
907 *data++ = (word >> 8) & 0xff;
917 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
921 *data++ = word & 0xff;
922 *data++ = (word >> 8) & 0xff;
932 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
936 *data++ = word & 0xff;
946 static int mv88e6xxx_eeprom_is_readonly(struct dsa_switch *ds)
948 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
951 ret = mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP);
955 if (!(ret & GLOBAL2_EEPROM_OP_WRITE_EN))
961 static int mv88e6xxx_write_eeprom_word(struct dsa_switch *ds, int addr,
964 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
967 mutex_lock(&ps->eeprom_mutex);
969 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_DATA, data);
973 ret = mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_EEPROM_OP,
974 GLOBAL2_EEPROM_OP_WRITE |
975 (addr & GLOBAL2_EEPROM_OP_ADDR_MASK));
979 ret = mv88e6xxx_eeprom_busy_wait(ds);
981 mutex_unlock(&ps->eeprom_mutex);
985 int mv88e6xxx_set_eeprom(struct dsa_switch *ds, struct ethtool_eeprom *eeprom,
988 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
993 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
996 if (eeprom->magic != 0xc3ec4951)
999 ret = mv88e6xxx_eeprom_is_readonly(ds);
1003 offset = eeprom->offset;
1007 ret = mv88e6xxx_eeprom_load_wait(ds);
1014 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1018 word = (*data++ << 8) | (word & 0xff);
1020 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1033 word |= *data++ << 8;
1035 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1047 word = mv88e6xxx_read_eeprom_word(ds, offset >> 1);
1051 word = (word & 0xff00) | *data++;
1053 ret = mv88e6xxx_write_eeprom_word(ds, offset >> 1, word);
1065 static int _mv88e6xxx_atu_wait(struct mv88e6xxx_priv_state *ps)
1067 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_ATU_OP,
1068 GLOBAL_ATU_OP_BUSY);
1071 static int _mv88e6xxx_phy_read_indirect(struct mv88e6xxx_priv_state *ps,
1072 int addr, int regnum)
1076 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
1077 GLOBAL2_SMI_OP_22_READ | (addr << 5) |
1082 ret = _mv88e6xxx_phy_wait(ps);
1086 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA);
1091 static int _mv88e6xxx_phy_write_indirect(struct mv88e6xxx_priv_state *ps,
1092 int addr, int regnum, u16 val)
1096 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_DATA, val);
1100 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SMI_OP,
1101 GLOBAL2_SMI_OP_22_WRITE | (addr << 5) |
1104 return _mv88e6xxx_phy_wait(ps);
1107 int mv88e6xxx_get_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
1109 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1112 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1115 mutex_lock(&ps->smi_mutex);
1117 reg = _mv88e6xxx_phy_read_indirect(ps, port, 16);
1121 e->eee_enabled = !!(reg & 0x0200);
1122 e->tx_lpi_enabled = !!(reg & 0x0100);
1124 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
1128 e->eee_active = !!(reg & PORT_STATUS_EEE);
1132 mutex_unlock(&ps->smi_mutex);
1136 int mv88e6xxx_set_eee(struct dsa_switch *ds, int port,
1137 struct phy_device *phydev, struct ethtool_eee *e)
1139 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1143 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEE))
1146 mutex_lock(&ps->smi_mutex);
1148 ret = _mv88e6xxx_phy_read_indirect(ps, port, 16);
1152 reg = ret & ~0x0300;
1155 if (e->tx_lpi_enabled)
1158 ret = _mv88e6xxx_phy_write_indirect(ps, port, 16, reg);
1160 mutex_unlock(&ps->smi_mutex);
1165 static int _mv88e6xxx_atu_cmd(struct mv88e6xxx_priv_state *ps, u16 fid, u16 cmd)
1169 if (mv88e6xxx_has_fid_reg(ps)) {
1170 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_FID, fid);
1173 } else if (mv88e6xxx_num_databases(ps) == 256) {
1174 /* ATU DBNum[7:4] are located in ATU Control 15:12 */
1175 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL);
1179 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
1181 ((fid << 8) & 0xf000));
1185 /* ATU DBNum[3:0] are located in ATU Operation 3:0 */
1189 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_OP, cmd);
1193 return _mv88e6xxx_atu_wait(ps);
1196 static int _mv88e6xxx_atu_data_write(struct mv88e6xxx_priv_state *ps,
1197 struct mv88e6xxx_atu_entry *entry)
1199 u16 data = entry->state & GLOBAL_ATU_DATA_STATE_MASK;
1201 if (entry->state != GLOBAL_ATU_DATA_STATE_UNUSED) {
1202 unsigned int mask, shift;
1205 data |= GLOBAL_ATU_DATA_TRUNK;
1206 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
1207 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
1209 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
1210 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
1213 data |= (entry->portv_trunkid << shift) & mask;
1216 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_DATA, data);
1219 static int _mv88e6xxx_atu_flush_move(struct mv88e6xxx_priv_state *ps,
1220 struct mv88e6xxx_atu_entry *entry,
1226 err = _mv88e6xxx_atu_wait(ps);
1230 err = _mv88e6xxx_atu_data_write(ps, entry);
1235 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL_DB :
1236 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC_DB;
1238 op = static_too ? GLOBAL_ATU_OP_FLUSH_MOVE_ALL :
1239 GLOBAL_ATU_OP_FLUSH_MOVE_NON_STATIC;
1242 return _mv88e6xxx_atu_cmd(ps, entry->fid, op);
1245 static int _mv88e6xxx_atu_flush(struct mv88e6xxx_priv_state *ps,
1246 u16 fid, bool static_too)
1248 struct mv88e6xxx_atu_entry entry = {
1250 .state = 0, /* EntryState bits must be 0 */
1253 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
1256 static int _mv88e6xxx_atu_move(struct mv88e6xxx_priv_state *ps, u16 fid,
1257 int from_port, int to_port, bool static_too)
1259 struct mv88e6xxx_atu_entry entry = {
1264 /* EntryState bits must be 0xF */
1265 entry.state = GLOBAL_ATU_DATA_STATE_MASK;
1267 /* ToPort and FromPort are respectively in PortVec bits 7:4 and 3:0 */
1268 entry.portv_trunkid = (to_port & 0x0f) << 4;
1269 entry.portv_trunkid |= from_port & 0x0f;
1271 return _mv88e6xxx_atu_flush_move(ps, &entry, static_too);
1274 static int _mv88e6xxx_atu_remove(struct mv88e6xxx_priv_state *ps, u16 fid,
1275 int port, bool static_too)
1277 /* Destination port 0xF means remove the entries */
1278 return _mv88e6xxx_atu_move(ps, fid, port, 0x0f, static_too);
1281 static const char * const mv88e6xxx_port_state_names[] = {
1282 [PORT_CONTROL_STATE_DISABLED] = "Disabled",
1283 [PORT_CONTROL_STATE_BLOCKING] = "Blocking/Listening",
1284 [PORT_CONTROL_STATE_LEARNING] = "Learning",
1285 [PORT_CONTROL_STATE_FORWARDING] = "Forwarding",
1288 static int _mv88e6xxx_port_state(struct mv88e6xxx_priv_state *ps, int port,
1291 struct dsa_switch *ds = ps->ds;
1295 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL);
1299 oldstate = reg & PORT_CONTROL_STATE_MASK;
1301 if (oldstate != state) {
1302 /* Flush forwarding database if we're moving a port
1303 * from Learning or Forwarding state to Disabled or
1304 * Blocking or Listening state.
1306 if ((oldstate == PORT_CONTROL_STATE_LEARNING ||
1307 oldstate == PORT_CONTROL_STATE_FORWARDING)
1308 && (state == PORT_CONTROL_STATE_DISABLED ||
1309 state == PORT_CONTROL_STATE_BLOCKING)) {
1310 ret = _mv88e6xxx_atu_remove(ps, 0, port, false);
1315 reg = (reg & ~PORT_CONTROL_STATE_MASK) | state;
1316 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL,
1321 netdev_dbg(ds->ports[port], "PortState %s (was %s)\n",
1322 mv88e6xxx_port_state_names[state],
1323 mv88e6xxx_port_state_names[oldstate]);
1329 static int _mv88e6xxx_port_based_vlan_map(struct mv88e6xxx_priv_state *ps,
1332 struct net_device *bridge = ps->ports[port].bridge_dev;
1333 const u16 mask = (1 << ps->info->num_ports) - 1;
1334 struct dsa_switch *ds = ps->ds;
1335 u16 output_ports = 0;
1339 /* allow CPU port or DSA link(s) to send frames to every port */
1340 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
1341 output_ports = mask;
1343 for (i = 0; i < ps->info->num_ports; ++i) {
1344 /* allow sending frames to every group member */
1345 if (bridge && ps->ports[i].bridge_dev == bridge)
1346 output_ports |= BIT(i);
1348 /* allow sending frames to CPU port and DSA link(s) */
1349 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
1350 output_ports |= BIT(i);
1354 /* prevent frames from going back out of the port they came in on */
1355 output_ports &= ~BIT(port);
1357 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
1362 reg |= output_ports & mask;
1364 return _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN, reg);
1367 void mv88e6xxx_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1369 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1372 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_PORTSTATE))
1376 case BR_STATE_DISABLED:
1377 stp_state = PORT_CONTROL_STATE_DISABLED;
1379 case BR_STATE_BLOCKING:
1380 case BR_STATE_LISTENING:
1381 stp_state = PORT_CONTROL_STATE_BLOCKING;
1383 case BR_STATE_LEARNING:
1384 stp_state = PORT_CONTROL_STATE_LEARNING;
1386 case BR_STATE_FORWARDING:
1388 stp_state = PORT_CONTROL_STATE_FORWARDING;
1392 /* mv88e6xxx_port_stp_state_set may be called with softirqs disabled,
1393 * so we can not update the port state directly but need to schedule it.
1395 ps->ports[port].state = stp_state;
1396 set_bit(port, ps->port_state_update_mask);
1397 schedule_work(&ps->bridge_work);
1400 static int _mv88e6xxx_port_pvid(struct mv88e6xxx_priv_state *ps, int port,
1403 struct dsa_switch *ds = ps->ds;
1407 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_DEFAULT_VLAN);
1411 pvid = ret & PORT_DEFAULT_VLAN_MASK;
1414 ret &= ~PORT_DEFAULT_VLAN_MASK;
1415 ret |= *new & PORT_DEFAULT_VLAN_MASK;
1417 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
1418 PORT_DEFAULT_VLAN, ret);
1422 netdev_dbg(ds->ports[port], "DefaultVID %d (was %d)\n", *new,
1432 static int _mv88e6xxx_port_pvid_get(struct mv88e6xxx_priv_state *ps,
1433 int port, u16 *pvid)
1435 return _mv88e6xxx_port_pvid(ps, port, NULL, pvid);
1438 static int _mv88e6xxx_port_pvid_set(struct mv88e6xxx_priv_state *ps,
1441 return _mv88e6xxx_port_pvid(ps, port, &pvid, NULL);
1444 static int _mv88e6xxx_vtu_wait(struct mv88e6xxx_priv_state *ps)
1446 return _mv88e6xxx_wait(ps, REG_GLOBAL, GLOBAL_VTU_OP,
1447 GLOBAL_VTU_OP_BUSY);
1450 static int _mv88e6xxx_vtu_cmd(struct mv88e6xxx_priv_state *ps, u16 op)
1454 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_OP, op);
1458 return _mv88e6xxx_vtu_wait(ps);
1461 static int _mv88e6xxx_vtu_stu_flush(struct mv88e6xxx_priv_state *ps)
1465 ret = _mv88e6xxx_vtu_wait(ps);
1469 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_FLUSH_ALL);
1472 static int _mv88e6xxx_vtu_stu_data_read(struct mv88e6xxx_priv_state *ps,
1473 struct mv88e6xxx_vtu_stu_entry *entry,
1474 unsigned int nibble_offset)
1480 for (i = 0; i < 3; ++i) {
1481 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
1482 GLOBAL_VTU_DATA_0_3 + i);
1489 for (i = 0; i < ps->info->num_ports; ++i) {
1490 unsigned int shift = (i % 4) * 4 + nibble_offset;
1491 u16 reg = regs[i / 4];
1493 entry->data[i] = (reg >> shift) & GLOBAL_VTU_STU_DATA_MASK;
1499 static int _mv88e6xxx_vtu_stu_data_write(struct mv88e6xxx_priv_state *ps,
1500 struct mv88e6xxx_vtu_stu_entry *entry,
1501 unsigned int nibble_offset)
1503 u16 regs[3] = { 0 };
1507 for (i = 0; i < ps->info->num_ports; ++i) {
1508 unsigned int shift = (i % 4) * 4 + nibble_offset;
1509 u8 data = entry->data[i];
1511 regs[i / 4] |= (data & GLOBAL_VTU_STU_DATA_MASK) << shift;
1514 for (i = 0; i < 3; ++i) {
1515 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL,
1516 GLOBAL_VTU_DATA_0_3 + i, regs[i]);
1524 static int _mv88e6xxx_vtu_vid_write(struct mv88e6xxx_priv_state *ps, u16 vid)
1526 return _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID,
1527 vid & GLOBAL_VTU_VID_MASK);
1530 static int _mv88e6xxx_vtu_getnext(struct mv88e6xxx_priv_state *ps,
1531 struct mv88e6xxx_vtu_stu_entry *entry)
1533 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1536 ret = _mv88e6xxx_vtu_wait(ps);
1540 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_VTU_GET_NEXT);
1544 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
1548 next.vid = ret & GLOBAL_VTU_VID_MASK;
1549 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1552 ret = _mv88e6xxx_vtu_stu_data_read(ps, &next, 0);
1556 if (mv88e6xxx_has_fid_reg(ps)) {
1557 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
1562 next.fid = ret & GLOBAL_VTU_FID_MASK;
1563 } else if (mv88e6xxx_num_databases(ps) == 256) {
1564 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1565 * VTU DBNum[3:0] are located in VTU Operation 3:0
1567 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
1572 next.fid = (ret & 0xf00) >> 4;
1573 next.fid |= ret & 0xf;
1576 if (mv88e6xxx_has_stu(ps)) {
1577 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
1582 next.sid = ret & GLOBAL_VTU_SID_MASK;
1590 int mv88e6xxx_port_vlan_dump(struct dsa_switch *ds, int port,
1591 struct switchdev_obj_port_vlan *vlan,
1592 int (*cb)(struct switchdev_obj *obj))
1594 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1595 struct mv88e6xxx_vtu_stu_entry next;
1599 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
1602 mutex_lock(&ps->smi_mutex);
1604 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
1608 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
1613 err = _mv88e6xxx_vtu_getnext(ps, &next);
1620 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1623 /* reinit and dump this VLAN obj */
1624 vlan->vid_begin = vlan->vid_end = next.vid;
1627 if (next.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED)
1628 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
1630 if (next.vid == pvid)
1631 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
1633 err = cb(&vlan->obj);
1636 } while (next.vid < GLOBAL_VTU_VID_MASK);
1639 mutex_unlock(&ps->smi_mutex);
1644 static int _mv88e6xxx_vtu_loadpurge(struct mv88e6xxx_priv_state *ps,
1645 struct mv88e6xxx_vtu_stu_entry *entry)
1647 u16 op = GLOBAL_VTU_OP_VTU_LOAD_PURGE;
1651 ret = _mv88e6xxx_vtu_wait(ps);
1658 /* Write port member tags */
1659 ret = _mv88e6xxx_vtu_stu_data_write(ps, entry, 0);
1663 if (mv88e6xxx_has_stu(ps)) {
1664 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1665 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1670 if (mv88e6xxx_has_fid_reg(ps)) {
1671 reg = entry->fid & GLOBAL_VTU_FID_MASK;
1672 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_FID, reg);
1675 } else if (mv88e6xxx_num_databases(ps) == 256) {
1676 /* VTU DBNum[7:4] are located in VTU Operation 11:8, and
1677 * VTU DBNum[3:0] are located in VTU Operation 3:0
1679 op |= (entry->fid & 0xf0) << 8;
1680 op |= entry->fid & 0xf;
1683 reg = GLOBAL_VTU_VID_VALID;
1685 reg |= entry->vid & GLOBAL_VTU_VID_MASK;
1686 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1690 return _mv88e6xxx_vtu_cmd(ps, op);
1693 static int _mv88e6xxx_stu_getnext(struct mv88e6xxx_priv_state *ps, u8 sid,
1694 struct mv88e6xxx_vtu_stu_entry *entry)
1696 struct mv88e6xxx_vtu_stu_entry next = { 0 };
1699 ret = _mv88e6xxx_vtu_wait(ps);
1703 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID,
1704 sid & GLOBAL_VTU_SID_MASK);
1708 ret = _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_GET_NEXT);
1712 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_SID);
1716 next.sid = ret & GLOBAL_VTU_SID_MASK;
1718 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_VTU_VID);
1722 next.valid = !!(ret & GLOBAL_VTU_VID_VALID);
1725 ret = _mv88e6xxx_vtu_stu_data_read(ps, &next, 2);
1734 static int _mv88e6xxx_stu_loadpurge(struct mv88e6xxx_priv_state *ps,
1735 struct mv88e6xxx_vtu_stu_entry *entry)
1740 ret = _mv88e6xxx_vtu_wait(ps);
1747 /* Write port states */
1748 ret = _mv88e6xxx_vtu_stu_data_write(ps, entry, 2);
1752 reg = GLOBAL_VTU_VID_VALID;
1754 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_VID, reg);
1758 reg = entry->sid & GLOBAL_VTU_SID_MASK;
1759 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_VTU_SID, reg);
1763 return _mv88e6xxx_vtu_cmd(ps, GLOBAL_VTU_OP_STU_LOAD_PURGE);
1766 static int _mv88e6xxx_port_fid(struct mv88e6xxx_priv_state *ps, int port,
1769 struct dsa_switch *ds = ps->ds;
1774 if (mv88e6xxx_num_databases(ps) == 4096)
1776 else if (mv88e6xxx_num_databases(ps) == 256)
1781 /* Port's default FID bits 3:0 are located in reg 0x06, offset 12 */
1782 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_BASE_VLAN);
1786 fid = (ret & PORT_BASE_VLAN_FID_3_0_MASK) >> 12;
1789 ret &= ~PORT_BASE_VLAN_FID_3_0_MASK;
1790 ret |= (*new << 12) & PORT_BASE_VLAN_FID_3_0_MASK;
1792 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_BASE_VLAN,
1798 /* Port's default FID bits 11:4 are located in reg 0x05, offset 0 */
1799 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_1);
1803 fid |= (ret & upper_mask) << 4;
1807 ret |= (*new >> 4) & upper_mask;
1809 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1,
1814 netdev_dbg(ds->ports[port], "FID %d (was %d)\n", *new, fid);
1823 static int _mv88e6xxx_port_fid_get(struct mv88e6xxx_priv_state *ps,
1826 return _mv88e6xxx_port_fid(ps, port, NULL, fid);
1829 static int _mv88e6xxx_port_fid_set(struct mv88e6xxx_priv_state *ps,
1832 return _mv88e6xxx_port_fid(ps, port, &fid, NULL);
1835 static int _mv88e6xxx_fid_new(struct mv88e6xxx_priv_state *ps, u16 *fid)
1837 DECLARE_BITMAP(fid_bitmap, MV88E6XXX_N_FID);
1838 struct mv88e6xxx_vtu_stu_entry vlan;
1841 bitmap_zero(fid_bitmap, MV88E6XXX_N_FID);
1843 /* Set every FID bit used by the (un)bridged ports */
1844 for (i = 0; i < ps->info->num_ports; ++i) {
1845 err = _mv88e6xxx_port_fid_get(ps, i, fid);
1849 set_bit(*fid, fid_bitmap);
1852 /* Set every FID bit used by the VLAN entries */
1853 err = _mv88e6xxx_vtu_vid_write(ps, GLOBAL_VTU_VID_MASK);
1858 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
1865 set_bit(vlan.fid, fid_bitmap);
1866 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
1868 /* The reset value 0x000 is used to indicate that multiple address
1869 * databases are not needed. Return the next positive available.
1871 *fid = find_next_zero_bit(fid_bitmap, MV88E6XXX_N_FID, 1);
1872 if (unlikely(*fid >= mv88e6xxx_num_databases(ps)))
1875 /* Clear the database */
1876 return _mv88e6xxx_atu_flush(ps, *fid, true);
1879 static int _mv88e6xxx_vtu_new(struct mv88e6xxx_priv_state *ps, u16 vid,
1880 struct mv88e6xxx_vtu_stu_entry *entry)
1882 struct dsa_switch *ds = ps->ds;
1883 struct mv88e6xxx_vtu_stu_entry vlan = {
1889 err = _mv88e6xxx_fid_new(ps, &vlan.fid);
1893 /* exclude all ports except the CPU and DSA ports */
1894 for (i = 0; i < ps->info->num_ports; ++i)
1895 vlan.data[i] = dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i)
1896 ? GLOBAL_VTU_DATA_MEMBER_TAG_UNMODIFIED
1897 : GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
1899 if (mv88e6xxx_6097_family(ps) || mv88e6xxx_6165_family(ps) ||
1900 mv88e6xxx_6351_family(ps) || mv88e6xxx_6352_family(ps)) {
1901 struct mv88e6xxx_vtu_stu_entry vstp;
1903 /* Adding a VTU entry requires a valid STU entry. As VSTP is not
1904 * implemented, only one STU entry is needed to cover all VTU
1905 * entries. Thus, validate the SID 0.
1908 err = _mv88e6xxx_stu_getnext(ps, GLOBAL_VTU_SID_MASK, &vstp);
1912 if (vstp.sid != vlan.sid || !vstp.valid) {
1913 memset(&vstp, 0, sizeof(vstp));
1915 vstp.sid = vlan.sid;
1917 err = _mv88e6xxx_stu_loadpurge(ps, &vstp);
1927 static int _mv88e6xxx_vtu_get(struct mv88e6xxx_priv_state *ps, u16 vid,
1928 struct mv88e6xxx_vtu_stu_entry *entry, bool creat)
1935 err = _mv88e6xxx_vtu_vid_write(ps, vid - 1);
1939 err = _mv88e6xxx_vtu_getnext(ps, entry);
1943 if (entry->vid != vid || !entry->valid) {
1946 /* -ENOENT would've been more appropriate, but switchdev expects
1947 * -EOPNOTSUPP to inform bridge about an eventual software VLAN.
1950 err = _mv88e6xxx_vtu_new(ps, vid, entry);
1956 static int mv88e6xxx_port_check_hw_vlan(struct dsa_switch *ds, int port,
1957 u16 vid_begin, u16 vid_end)
1959 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
1960 struct mv88e6xxx_vtu_stu_entry vlan;
1966 mutex_lock(&ps->smi_mutex);
1968 err = _mv88e6xxx_vtu_vid_write(ps, vid_begin - 1);
1973 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
1980 if (vlan.vid > vid_end)
1983 for (i = 0; i < ps->info->num_ports; ++i) {
1984 if (dsa_is_dsa_port(ds, i) || dsa_is_cpu_port(ds, i))
1988 GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
1991 if (ps->ports[i].bridge_dev ==
1992 ps->ports[port].bridge_dev)
1993 break; /* same bridge, check next VLAN */
1995 netdev_warn(ds->ports[port],
1996 "hardware VLAN %d already used by %s\n",
1998 netdev_name(ps->ports[i].bridge_dev));
2002 } while (vlan.vid < vid_end);
2005 mutex_unlock(&ps->smi_mutex);
2010 static const char * const mv88e6xxx_port_8021q_mode_names[] = {
2011 [PORT_CONTROL_2_8021Q_DISABLED] = "Disabled",
2012 [PORT_CONTROL_2_8021Q_FALLBACK] = "Fallback",
2013 [PORT_CONTROL_2_8021Q_CHECK] = "Check",
2014 [PORT_CONTROL_2_8021Q_SECURE] = "Secure",
2017 int mv88e6xxx_port_vlan_filtering(struct dsa_switch *ds, int port,
2018 bool vlan_filtering)
2020 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2021 u16 old, new = vlan_filtering ? PORT_CONTROL_2_8021Q_SECURE :
2022 PORT_CONTROL_2_8021Q_DISABLED;
2025 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2028 mutex_lock(&ps->smi_mutex);
2030 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_CONTROL_2);
2034 old = ret & PORT_CONTROL_2_8021Q_MASK;
2037 ret &= ~PORT_CONTROL_2_8021Q_MASK;
2038 ret |= new & PORT_CONTROL_2_8021Q_MASK;
2040 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_2,
2045 netdev_dbg(ds->ports[port], "802.1Q Mode %s (was %s)\n",
2046 mv88e6xxx_port_8021q_mode_names[new],
2047 mv88e6xxx_port_8021q_mode_names[old]);
2052 mutex_unlock(&ps->smi_mutex);
2057 int mv88e6xxx_port_vlan_prepare(struct dsa_switch *ds, int port,
2058 const struct switchdev_obj_port_vlan *vlan,
2059 struct switchdev_trans *trans)
2061 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2064 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2067 /* If the requested port doesn't belong to the same bridge as the VLAN
2068 * members, do not support it (yet) and fallback to software VLAN.
2070 err = mv88e6xxx_port_check_hw_vlan(ds, port, vlan->vid_begin,
2075 /* We don't need any dynamic resource from the kernel (yet),
2076 * so skip the prepare phase.
2081 static int _mv88e6xxx_port_vlan_add(struct mv88e6xxx_priv_state *ps, int port,
2082 u16 vid, bool untagged)
2084 struct mv88e6xxx_vtu_stu_entry vlan;
2087 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, true);
2091 vlan.data[port] = untagged ?
2092 GLOBAL_VTU_DATA_MEMBER_TAG_UNTAGGED :
2093 GLOBAL_VTU_DATA_MEMBER_TAG_TAGGED;
2095 return _mv88e6xxx_vtu_loadpurge(ps, &vlan);
2098 void mv88e6xxx_port_vlan_add(struct dsa_switch *ds, int port,
2099 const struct switchdev_obj_port_vlan *vlan,
2100 struct switchdev_trans *trans)
2102 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2103 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
2104 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
2107 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2110 mutex_lock(&ps->smi_mutex);
2112 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid)
2113 if (_mv88e6xxx_port_vlan_add(ps, port, vid, untagged))
2114 netdev_err(ds->ports[port], "failed to add VLAN %d%c\n",
2115 vid, untagged ? 'u' : 't');
2117 if (pvid && _mv88e6xxx_port_pvid_set(ps, port, vlan->vid_end))
2118 netdev_err(ds->ports[port], "failed to set PVID %d\n",
2121 mutex_unlock(&ps->smi_mutex);
2124 static int _mv88e6xxx_port_vlan_del(struct mv88e6xxx_priv_state *ps,
2127 struct dsa_switch *ds = ps->ds;
2128 struct mv88e6xxx_vtu_stu_entry vlan;
2131 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
2135 /* Tell switchdev if this VLAN is handled in software */
2136 if (vlan.data[port] == GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER)
2139 vlan.data[port] = GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER;
2141 /* keep the VLAN unless all ports are excluded */
2143 for (i = 0; i < ps->info->num_ports; ++i) {
2144 if (dsa_is_cpu_port(ds, i) || dsa_is_dsa_port(ds, i))
2147 if (vlan.data[i] != GLOBAL_VTU_DATA_MEMBER_TAG_NON_MEMBER) {
2153 err = _mv88e6xxx_vtu_loadpurge(ps, &vlan);
2157 return _mv88e6xxx_atu_remove(ps, vlan.fid, port, false);
2160 int mv88e6xxx_port_vlan_del(struct dsa_switch *ds, int port,
2161 const struct switchdev_obj_port_vlan *vlan)
2163 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2167 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VTU))
2170 mutex_lock(&ps->smi_mutex);
2172 err = _mv88e6xxx_port_pvid_get(ps, port, &pvid);
2176 for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) {
2177 err = _mv88e6xxx_port_vlan_del(ps, port, vid);
2182 err = _mv88e6xxx_port_pvid_set(ps, port, 0);
2189 mutex_unlock(&ps->smi_mutex);
2194 static int _mv88e6xxx_atu_mac_write(struct mv88e6xxx_priv_state *ps,
2195 const unsigned char *addr)
2199 for (i = 0; i < 3; i++) {
2200 ret = _mv88e6xxx_reg_write(
2201 ps, REG_GLOBAL, GLOBAL_ATU_MAC_01 + i,
2202 (addr[i * 2] << 8) | addr[i * 2 + 1]);
2210 static int _mv88e6xxx_atu_mac_read(struct mv88e6xxx_priv_state *ps,
2211 unsigned char *addr)
2215 for (i = 0; i < 3; i++) {
2216 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL,
2217 GLOBAL_ATU_MAC_01 + i);
2220 addr[i * 2] = ret >> 8;
2221 addr[i * 2 + 1] = ret & 0xff;
2227 static int _mv88e6xxx_atu_load(struct mv88e6xxx_priv_state *ps,
2228 struct mv88e6xxx_atu_entry *entry)
2232 ret = _mv88e6xxx_atu_wait(ps);
2236 ret = _mv88e6xxx_atu_mac_write(ps, entry->mac);
2240 ret = _mv88e6xxx_atu_data_write(ps, entry);
2244 return _mv88e6xxx_atu_cmd(ps, entry->fid, GLOBAL_ATU_OP_LOAD_DB);
2247 static int _mv88e6xxx_port_fdb_load(struct mv88e6xxx_priv_state *ps, int port,
2248 const unsigned char *addr, u16 vid,
2251 struct mv88e6xxx_atu_entry entry = { 0 };
2252 struct mv88e6xxx_vtu_stu_entry vlan;
2255 /* Null VLAN ID corresponds to the port private database */
2257 err = _mv88e6xxx_port_fid_get(ps, port, &vlan.fid);
2259 err = _mv88e6xxx_vtu_get(ps, vid, &vlan, false);
2263 entry.fid = vlan.fid;
2264 entry.state = state;
2265 ether_addr_copy(entry.mac, addr);
2266 if (state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2267 entry.trunk = false;
2268 entry.portv_trunkid = BIT(port);
2271 return _mv88e6xxx_atu_load(ps, &entry);
2274 int mv88e6xxx_port_fdb_prepare(struct dsa_switch *ds, int port,
2275 const struct switchdev_obj_port_fdb *fdb,
2276 struct switchdev_trans *trans)
2278 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2280 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2283 /* We don't need any dynamic resource from the kernel (yet),
2284 * so skip the prepare phase.
2289 void mv88e6xxx_port_fdb_add(struct dsa_switch *ds, int port,
2290 const struct switchdev_obj_port_fdb *fdb,
2291 struct switchdev_trans *trans)
2293 int state = is_multicast_ether_addr(fdb->addr) ?
2294 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2295 GLOBAL_ATU_DATA_STATE_UC_STATIC;
2296 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2298 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2301 mutex_lock(&ps->smi_mutex);
2302 if (_mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid, state))
2303 netdev_err(ds->ports[port], "failed to load MAC address\n");
2304 mutex_unlock(&ps->smi_mutex);
2307 int mv88e6xxx_port_fdb_del(struct dsa_switch *ds, int port,
2308 const struct switchdev_obj_port_fdb *fdb)
2310 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2313 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2316 mutex_lock(&ps->smi_mutex);
2317 ret = _mv88e6xxx_port_fdb_load(ps, port, fdb->addr, fdb->vid,
2318 GLOBAL_ATU_DATA_STATE_UNUSED);
2319 mutex_unlock(&ps->smi_mutex);
2324 static int _mv88e6xxx_atu_getnext(struct mv88e6xxx_priv_state *ps, u16 fid,
2325 struct mv88e6xxx_atu_entry *entry)
2327 struct mv88e6xxx_atu_entry next = { 0 };
2332 ret = _mv88e6xxx_atu_wait(ps);
2336 ret = _mv88e6xxx_atu_cmd(ps, fid, GLOBAL_ATU_OP_GET_NEXT_DB);
2340 ret = _mv88e6xxx_atu_mac_read(ps, next.mac);
2344 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, GLOBAL_ATU_DATA);
2348 next.state = ret & GLOBAL_ATU_DATA_STATE_MASK;
2349 if (next.state != GLOBAL_ATU_DATA_STATE_UNUSED) {
2350 unsigned int mask, shift;
2352 if (ret & GLOBAL_ATU_DATA_TRUNK) {
2354 mask = GLOBAL_ATU_DATA_TRUNK_ID_MASK;
2355 shift = GLOBAL_ATU_DATA_TRUNK_ID_SHIFT;
2358 mask = GLOBAL_ATU_DATA_PORT_VECTOR_MASK;
2359 shift = GLOBAL_ATU_DATA_PORT_VECTOR_SHIFT;
2362 next.portv_trunkid = (ret & mask) >> shift;
2369 static int _mv88e6xxx_port_fdb_dump_one(struct mv88e6xxx_priv_state *ps,
2370 u16 fid, u16 vid, int port,
2371 struct switchdev_obj_port_fdb *fdb,
2372 int (*cb)(struct switchdev_obj *obj))
2374 struct mv88e6xxx_atu_entry addr = {
2375 .mac = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff },
2379 err = _mv88e6xxx_atu_mac_write(ps, addr.mac);
2384 err = _mv88e6xxx_atu_getnext(ps, fid, &addr);
2388 if (addr.state == GLOBAL_ATU_DATA_STATE_UNUSED)
2391 if (!addr.trunk && addr.portv_trunkid & BIT(port)) {
2392 bool is_static = addr.state ==
2393 (is_multicast_ether_addr(addr.mac) ?
2394 GLOBAL_ATU_DATA_STATE_MC_STATIC :
2395 GLOBAL_ATU_DATA_STATE_UC_STATIC);
2398 ether_addr_copy(fdb->addr, addr.mac);
2399 fdb->ndm_state = is_static ? NUD_NOARP : NUD_REACHABLE;
2401 err = cb(&fdb->obj);
2405 } while (!is_broadcast_ether_addr(addr.mac));
2410 int mv88e6xxx_port_fdb_dump(struct dsa_switch *ds, int port,
2411 struct switchdev_obj_port_fdb *fdb,
2412 int (*cb)(struct switchdev_obj *obj))
2414 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2415 struct mv88e6xxx_vtu_stu_entry vlan = {
2416 .vid = GLOBAL_VTU_VID_MASK, /* all ones */
2421 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_ATU))
2424 mutex_lock(&ps->smi_mutex);
2426 /* Dump port's default Filtering Information Database (VLAN ID 0) */
2427 err = _mv88e6xxx_port_fid_get(ps, port, &fid);
2431 err = _mv88e6xxx_port_fdb_dump_one(ps, fid, 0, port, fdb, cb);
2435 /* Dump VLANs' Filtering Information Databases */
2436 err = _mv88e6xxx_vtu_vid_write(ps, vlan.vid);
2441 err = _mv88e6xxx_vtu_getnext(ps, &vlan);
2448 err = _mv88e6xxx_port_fdb_dump_one(ps, vlan.fid, vlan.vid, port,
2452 } while (vlan.vid < GLOBAL_VTU_VID_MASK);
2455 mutex_unlock(&ps->smi_mutex);
2460 int mv88e6xxx_port_bridge_join(struct dsa_switch *ds, int port,
2461 struct net_device *bridge)
2463 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2466 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2469 mutex_lock(&ps->smi_mutex);
2471 /* Assign the bridge and remap each port's VLANTable */
2472 ps->ports[port].bridge_dev = bridge;
2474 for (i = 0; i < ps->info->num_ports; ++i) {
2475 if (ps->ports[i].bridge_dev == bridge) {
2476 err = _mv88e6xxx_port_based_vlan_map(ps, i);
2482 mutex_unlock(&ps->smi_mutex);
2487 void mv88e6xxx_port_bridge_leave(struct dsa_switch *ds, int port)
2489 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2490 struct net_device *bridge = ps->ports[port].bridge_dev;
2493 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_VLANTABLE))
2496 mutex_lock(&ps->smi_mutex);
2498 /* Unassign the bridge and remap each port's VLANTable */
2499 ps->ports[port].bridge_dev = NULL;
2501 for (i = 0; i < ps->info->num_ports; ++i)
2502 if (i == port || ps->ports[i].bridge_dev == bridge)
2503 if (_mv88e6xxx_port_based_vlan_map(ps, i))
2504 netdev_warn(ds->ports[i], "failed to remap\n");
2506 mutex_unlock(&ps->smi_mutex);
2509 static void mv88e6xxx_bridge_work(struct work_struct *work)
2511 struct mv88e6xxx_priv_state *ps;
2512 struct dsa_switch *ds;
2515 ps = container_of(work, struct mv88e6xxx_priv_state, bridge_work);
2518 mutex_lock(&ps->smi_mutex);
2520 for (port = 0; port < ps->info->num_ports; ++port)
2521 if (test_and_clear_bit(port, ps->port_state_update_mask) &&
2522 _mv88e6xxx_port_state(ps, port, ps->ports[port].state))
2523 netdev_warn(ds->ports[port],
2524 "failed to update state to %s\n",
2525 mv88e6xxx_port_state_names[ps->ports[port].state]);
2527 mutex_unlock(&ps->smi_mutex);
2530 static int _mv88e6xxx_phy_page_write(struct mv88e6xxx_priv_state *ps,
2531 int port, int page, int reg, int val)
2535 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
2537 goto restore_page_0;
2539 ret = _mv88e6xxx_phy_write_indirect(ps, port, reg, val);
2541 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
2546 static int _mv88e6xxx_phy_page_read(struct mv88e6xxx_priv_state *ps,
2547 int port, int page, int reg)
2551 ret = _mv88e6xxx_phy_write_indirect(ps, port, 0x16, page);
2553 goto restore_page_0;
2555 ret = _mv88e6xxx_phy_read_indirect(ps, port, reg);
2557 _mv88e6xxx_phy_write_indirect(ps, port, 0x16, 0x0);
2562 static int mv88e6xxx_switch_reset(struct mv88e6xxx_priv_state *ps)
2564 bool ppu_active = mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE);
2565 u16 is_reset = (ppu_active ? 0x8800 : 0xc800);
2566 struct gpio_desc *gpiod = ps->ds->pd->reset;
2567 unsigned long timeout;
2571 /* Set all ports to the disabled state. */
2572 for (i = 0; i < ps->info->num_ports; i++) {
2573 ret = _mv88e6xxx_reg_read(ps, REG_PORT(i), PORT_CONTROL);
2577 ret = _mv88e6xxx_reg_write(ps, REG_PORT(i), PORT_CONTROL,
2583 /* Wait for transmit queues to drain. */
2584 usleep_range(2000, 4000);
2586 /* If there is a gpio connected to the reset pin, toggle it */
2588 gpiod_set_value_cansleep(gpiod, 1);
2589 usleep_range(10000, 20000);
2590 gpiod_set_value_cansleep(gpiod, 0);
2591 usleep_range(10000, 20000);
2594 /* Reset the switch. Keep the PPU active if requested. The PPU
2595 * needs to be active to support indirect phy register access
2596 * through global registers 0x18 and 0x19.
2599 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc000);
2601 ret = _mv88e6xxx_reg_write(ps, REG_GLOBAL, 0x04, 0xc400);
2605 /* Wait up to one second for reset to complete. */
2606 timeout = jiffies + 1 * HZ;
2607 while (time_before(jiffies, timeout)) {
2608 ret = _mv88e6xxx_reg_read(ps, REG_GLOBAL, 0x00);
2612 if ((ret & is_reset) == is_reset)
2614 usleep_range(1000, 2000);
2616 if (time_after(jiffies, timeout))
2624 static int mv88e6xxx_power_on_serdes(struct mv88e6xxx_priv_state *ps)
2628 ret = _mv88e6xxx_phy_page_read(ps, REG_FIBER_SERDES, PAGE_FIBER_SERDES,
2633 if (ret & BMCR_PDOWN) {
2635 ret = _mv88e6xxx_phy_page_write(ps, REG_FIBER_SERDES,
2636 PAGE_FIBER_SERDES, MII_BMCR,
2643 static int mv88e6xxx_setup_port(struct dsa_switch *ds, int port)
2645 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2649 mutex_lock(&ps->smi_mutex);
2651 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2652 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2653 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2654 mv88e6xxx_6065_family(ps) || mv88e6xxx_6320_family(ps)) {
2655 /* MAC Forcing register: don't force link, speed,
2656 * duplex or flow control state to any particular
2657 * values on physical ports, but force the CPU port
2658 * and all DSA ports to their maximum bandwidth and
2661 reg = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_PCS_CTRL);
2662 if (dsa_is_cpu_port(ds, port) || dsa_is_dsa_port(ds, port)) {
2663 reg &= ~PORT_PCS_CTRL_UNFORCED;
2664 reg |= PORT_PCS_CTRL_FORCE_LINK |
2665 PORT_PCS_CTRL_LINK_UP |
2666 PORT_PCS_CTRL_DUPLEX_FULL |
2667 PORT_PCS_CTRL_FORCE_DUPLEX;
2668 if (mv88e6xxx_6065_family(ps))
2669 reg |= PORT_PCS_CTRL_100;
2671 reg |= PORT_PCS_CTRL_1000;
2673 reg |= PORT_PCS_CTRL_UNFORCED;
2676 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2677 PORT_PCS_CTRL, reg);
2682 /* Port Control: disable Drop-on-Unlock, disable Drop-on-Lock,
2683 * disable Header mode, enable IGMP/MLD snooping, disable VLAN
2684 * tunneling, determine priority by looking at 802.1p and IP
2685 * priority fields (IP prio has precedence), and set STP state
2688 * If this is the CPU link, use DSA or EDSA tagging depending
2689 * on which tagging mode was configured.
2691 * If this is a link to another switch, use DSA tagging mode.
2693 * If this is the upstream port for this switch, enable
2694 * forwarding of unknown unicasts and multicasts.
2697 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2698 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2699 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2700 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps))
2701 reg = PORT_CONTROL_IGMP_MLD_SNOOP |
2702 PORT_CONTROL_USE_TAG | PORT_CONTROL_USE_IP |
2703 PORT_CONTROL_STATE_FORWARDING;
2704 if (dsa_is_cpu_port(ds, port)) {
2705 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
2706 reg |= PORT_CONTROL_DSA_TAG;
2707 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2708 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2709 mv88e6xxx_6320_family(ps)) {
2710 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2711 reg |= PORT_CONTROL_FRAME_ETHER_TYPE_DSA;
2713 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2714 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2715 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2718 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2719 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2720 mv88e6xxx_6095_family(ps) || mv88e6xxx_6065_family(ps) ||
2721 mv88e6xxx_6185_family(ps) || mv88e6xxx_6320_family(ps)) {
2722 if (ds->dst->tag_protocol == DSA_TAG_PROTO_EDSA)
2723 reg |= PORT_CONTROL_EGRESS_ADD_TAG;
2726 if (dsa_is_dsa_port(ds, port)) {
2727 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps))
2728 reg |= PORT_CONTROL_DSA_TAG;
2729 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2730 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2731 mv88e6xxx_6320_family(ps)) {
2732 reg |= PORT_CONTROL_FRAME_MODE_DSA;
2735 if (port == dsa_upstream_port(ds))
2736 reg |= PORT_CONTROL_FORWARD_UNKNOWN |
2737 PORT_CONTROL_FORWARD_UNKNOWN_MC;
2740 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2746 /* If this port is connected to a SerDes, make sure the SerDes is not
2749 if (mv88e6xxx_6352_family(ps)) {
2750 ret = _mv88e6xxx_reg_read(ps, REG_PORT(port), PORT_STATUS);
2753 ret &= PORT_STATUS_CMODE_MASK;
2754 if ((ret == PORT_STATUS_CMODE_100BASE_X) ||
2755 (ret == PORT_STATUS_CMODE_1000BASE_X) ||
2756 (ret == PORT_STATUS_CMODE_SGMII)) {
2757 ret = mv88e6xxx_power_on_serdes(ps);
2763 /* Port Control 2: don't force a good FCS, set the maximum frame size to
2764 * 10240 bytes, disable 802.1q tags checking, don't discard tagged or
2765 * untagged frames on this port, do a destination address lookup on all
2766 * received packets as usual, disable ARP mirroring and don't send a
2767 * copy of all transmitted/received frames on this port to the CPU.
2770 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2771 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2772 mv88e6xxx_6095_family(ps) || mv88e6xxx_6320_family(ps) ||
2773 mv88e6xxx_6185_family(ps))
2774 reg = PORT_CONTROL_2_MAP_DA;
2776 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2777 mv88e6xxx_6165_family(ps) || mv88e6xxx_6320_family(ps))
2778 reg |= PORT_CONTROL_2_JUMBO_10240;
2780 if (mv88e6xxx_6095_family(ps) || mv88e6xxx_6185_family(ps)) {
2781 /* Set the upstream port this port should use */
2782 reg |= dsa_upstream_port(ds);
2783 /* enable forwarding of unknown multicast addresses to
2786 if (port == dsa_upstream_port(ds))
2787 reg |= PORT_CONTROL_2_FORWARD_UNKNOWN;
2790 reg |= PORT_CONTROL_2_8021Q_DISABLED;
2793 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2794 PORT_CONTROL_2, reg);
2799 /* Port Association Vector: when learning source addresses
2800 * of packets, add the address to the address database using
2801 * a port bitmap that has only the bit for this port set and
2802 * the other bits clear.
2805 /* Disable learning for CPU port */
2806 if (dsa_is_cpu_port(ds, port))
2809 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_ASSOC_VECTOR, reg);
2813 /* Egress rate control 2: disable egress rate control. */
2814 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_RATE_CONTROL_2,
2819 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2820 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2821 mv88e6xxx_6320_family(ps)) {
2822 /* Do not limit the period of time that this port can
2823 * be paused for by the remote end or the period of
2824 * time that this port can pause the remote end.
2826 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2827 PORT_PAUSE_CTRL, 0x0000);
2831 /* Port ATU control: disable limiting the number of
2832 * address database entries that this port is allowed
2835 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2836 PORT_ATU_CONTROL, 0x0000);
2837 /* Priority Override: disable DA, SA and VTU priority
2840 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2841 PORT_PRI_OVERRIDE, 0x0000);
2845 /* Port Ethertype: use the Ethertype DSA Ethertype
2848 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2849 PORT_ETH_TYPE, ETH_P_EDSA);
2852 /* Tag Remap: use an identity 802.1p prio -> switch
2855 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2856 PORT_TAG_REGMAP_0123, 0x3210);
2860 /* Tag Remap 2: use an identity 802.1p prio -> switch
2863 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2864 PORT_TAG_REGMAP_4567, 0x7654);
2869 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
2870 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
2871 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
2872 mv88e6xxx_6320_family(ps)) {
2873 /* Rate Control: disable ingress rate limiting. */
2874 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port),
2875 PORT_RATE_CONTROL, 0x0001);
2880 /* Port Control 1: disable trunking, disable sending
2881 * learning messages to this port.
2883 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_CONTROL_1, 0x0000);
2887 /* Port based VLAN map: give each port the same default address
2888 * database, and allow bidirectional communication between the
2889 * CPU and DSA port(s), and the other ports.
2891 ret = _mv88e6xxx_port_fid_set(ps, port, 0);
2895 ret = _mv88e6xxx_port_based_vlan_map(ps, port);
2899 /* Default VLAN ID and priority: don't set a default VLAN
2900 * ID, and set the default packet priority to zero.
2902 ret = _mv88e6xxx_reg_write(ps, REG_PORT(port), PORT_DEFAULT_VLAN,
2905 mutex_unlock(&ps->smi_mutex);
2909 int mv88e6xxx_setup_ports(struct dsa_switch *ds)
2911 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
2915 for (i = 0; i < ps->info->num_ports; i++) {
2916 ret = mv88e6xxx_setup_port(ds, i);
2923 static int mv88e6xxx_setup_global(struct mv88e6xxx_priv_state *ps)
2925 struct dsa_switch *ds = ps->ds;
2926 u32 upstream_port = dsa_upstream_port(ds);
2931 /* Enable the PHY Polling Unit if present, don't discard any packets,
2932 * and mask all interrupt sources.
2935 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU) ||
2936 mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU_ACTIVE))
2937 reg |= GLOBAL_CONTROL_PPU_ENABLE;
2939 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL, reg);
2943 /* Configure the upstream port, and configure it as the port to which
2944 * ingress and egress and ARP monitor frames are to be sent.
2946 reg = upstream_port << GLOBAL_MONITOR_CONTROL_INGRESS_SHIFT |
2947 upstream_port << GLOBAL_MONITOR_CONTROL_EGRESS_SHIFT |
2948 upstream_port << GLOBAL_MONITOR_CONTROL_ARP_SHIFT;
2949 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_MONITOR_CONTROL, reg);
2953 /* Disable remote management, and set the switch's DSA device number. */
2954 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_CONTROL_2,
2955 GLOBAL_CONTROL_2_MULTIPLE_CASCADE |
2956 (ds->index & 0x1f));
2960 /* Set the default address aging time to 5 minutes, and
2961 * enable address learn messages to be sent to all message
2964 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_ATU_CONTROL,
2965 0x0140 | GLOBAL_ATU_CONTROL_LEARN2ALL);
2969 /* Configure the IP ToS mapping registers. */
2970 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_0, 0x0000);
2973 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_1, 0x0000);
2976 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_2, 0x5555);
2979 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_3, 0x5555);
2982 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_4, 0xaaaa);
2985 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_5, 0xaaaa);
2988 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_6, 0xffff);
2991 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IP_PRI_7, 0xffff);
2995 /* Configure the IEEE 802.1p priority mapping register. */
2996 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_IEEE_PRI, 0xfa41);
3000 /* Send all frames with destination addresses matching
3001 * 01:80:c2:00:00:0x to the CPU port.
3003 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_MGMT_EN_0X, 0xffff);
3007 /* Ignore removed tag data on doubly tagged packets, disable
3008 * flow control messages, force flow control priority to the
3009 * highest, and send all special multicast frames to the CPU
3010 * port at the highest priority.
3012 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_SWITCH_MGMT,
3013 0x7 | GLOBAL2_SWITCH_MGMT_RSVD2CPU | 0x70 |
3014 GLOBAL2_SWITCH_MGMT_FORCE_FLOW_CTRL_PRI);
3018 /* Program the DSA routing table. */
3019 for (i = 0; i < 32; i++) {
3022 if (ps->ds->pd->rtable &&
3023 i != ps->ds->index && i < ps->ds->dst->pd->nr_chips)
3024 nexthop = ps->ds->pd->rtable[i] & 0x1f;
3026 err = _mv88e6xxx_reg_write(
3028 GLOBAL2_DEVICE_MAPPING,
3029 GLOBAL2_DEVICE_MAPPING_UPDATE |
3030 (i << GLOBAL2_DEVICE_MAPPING_TARGET_SHIFT) | nexthop);
3035 /* Clear all trunk masks. */
3036 for (i = 0; i < 8; i++) {
3037 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2, GLOBAL2_TRUNK_MASK,
3039 (i << GLOBAL2_TRUNK_MASK_NUM_SHIFT) |
3040 ((1 << ps->info->num_ports) - 1));
3045 /* Clear all trunk mappings. */
3046 for (i = 0; i < 16; i++) {
3047 err = _mv88e6xxx_reg_write(
3049 GLOBAL2_TRUNK_MAPPING,
3050 GLOBAL2_TRUNK_MAPPING_UPDATE |
3051 (i << GLOBAL2_TRUNK_MAPPING_ID_SHIFT));
3056 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3057 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3058 mv88e6xxx_6320_family(ps)) {
3059 /* Send all frames with destination addresses matching
3060 * 01:80:c2:00:00:2x to the CPU port.
3062 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3063 GLOBAL2_MGMT_EN_2X, 0xffff);
3067 /* Initialise cross-chip port VLAN table to reset
3070 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3071 GLOBAL2_PVT_ADDR, 0x9000);
3075 /* Clear the priority override table. */
3076 for (i = 0; i < 16; i++) {
3077 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3078 GLOBAL2_PRIO_OVERRIDE,
3085 if (mv88e6xxx_6352_family(ps) || mv88e6xxx_6351_family(ps) ||
3086 mv88e6xxx_6165_family(ps) || mv88e6xxx_6097_family(ps) ||
3087 mv88e6xxx_6185_family(ps) || mv88e6xxx_6095_family(ps) ||
3088 mv88e6xxx_6320_family(ps)) {
3089 /* Disable ingress rate limiting by resetting all
3090 * ingress rate limit registers to their initial
3093 for (i = 0; i < ps->info->num_ports; i++) {
3094 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL2,
3102 /* Clear the statistics counters for all ports */
3103 err = _mv88e6xxx_reg_write(ps, REG_GLOBAL, GLOBAL_STATS_OP,
3104 GLOBAL_STATS_OP_FLUSH_ALL);
3108 /* Wait for the flush to complete. */
3109 err = _mv88e6xxx_stats_wait(ps);
3113 /* Clear all ATU entries */
3114 err = _mv88e6xxx_atu_flush(ps, 0, true);
3118 /* Clear all the VTU and STU entries */
3119 err = _mv88e6xxx_vtu_stu_flush(ps);
3126 int mv88e6xxx_setup_common(struct mv88e6xxx_priv_state *ps)
3130 mutex_init(&ps->smi_mutex);
3132 INIT_WORK(&ps->bridge_work, mv88e6xxx_bridge_work);
3134 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_EEPROM))
3135 mutex_init(&ps->eeprom_mutex);
3137 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3138 mv88e6xxx_ppu_state_init(ps);
3140 mutex_lock(&ps->smi_mutex);
3142 err = mv88e6xxx_switch_reset(ps);
3146 err = mv88e6xxx_setup_global(ps);
3149 mutex_unlock(&ps->smi_mutex);
3154 int mv88e6xxx_phy_page_read(struct dsa_switch *ds, int port, int page, int reg)
3156 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3159 mutex_lock(&ps->smi_mutex);
3160 ret = _mv88e6xxx_phy_page_read(ps, port, page, reg);
3161 mutex_unlock(&ps->smi_mutex);
3166 int mv88e6xxx_phy_page_write(struct dsa_switch *ds, int port, int page,
3169 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3172 mutex_lock(&ps->smi_mutex);
3173 ret = _mv88e6xxx_phy_page_write(ps, port, page, reg, val);
3174 mutex_unlock(&ps->smi_mutex);
3179 static int mv88e6xxx_port_to_phy_addr(struct mv88e6xxx_priv_state *ps,
3182 if (port >= 0 && port < ps->info->num_ports)
3188 mv88e6xxx_phy_read(struct dsa_switch *ds, int port, int regnum)
3190 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3191 int addr = mv88e6xxx_port_to_phy_addr(ps, port);
3197 mutex_lock(&ps->smi_mutex);
3199 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3200 ret = mv88e6xxx_phy_read_ppu(ps, addr, regnum);
3201 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3202 ret = _mv88e6xxx_phy_read_indirect(ps, addr, regnum);
3204 ret = _mv88e6xxx_phy_read(ps, addr, regnum);
3206 mutex_unlock(&ps->smi_mutex);
3211 mv88e6xxx_phy_write(struct dsa_switch *ds, int port, int regnum, u16 val)
3213 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3214 int addr = mv88e6xxx_port_to_phy_addr(ps, port);
3220 mutex_lock(&ps->smi_mutex);
3222 if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_PPU))
3223 ret = mv88e6xxx_phy_write_ppu(ps, addr, regnum, val);
3224 else if (mv88e6xxx_has(ps, MV88E6XXX_FLAG_SMI_PHY))
3225 ret = _mv88e6xxx_phy_write_indirect(ps, addr, regnum, val);
3227 ret = _mv88e6xxx_phy_write(ps, addr, regnum, val);
3229 mutex_unlock(&ps->smi_mutex);
3233 #ifdef CONFIG_NET_DSA_HWMON
3235 static int mv88e61xx_get_temp(struct dsa_switch *ds, int *temp)
3237 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3243 mutex_lock(&ps->smi_mutex);
3245 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x6);
3249 /* Enable temperature sensor */
3250 ret = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
3254 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x1a, ret | (1 << 5));
3258 /* Wait for temperature to stabilize */
3259 usleep_range(10000, 12000);
3261 val = _mv88e6xxx_phy_read(ps, 0x0, 0x1a);
3267 /* Disable temperature sensor */
3268 ret = _mv88e6xxx_phy_write(ps, 0x0, 0x1a, ret & ~(1 << 5));
3272 *temp = ((val & 0x1f) - 5) * 5;
3275 _mv88e6xxx_phy_write(ps, 0x0, 0x16, 0x0);
3276 mutex_unlock(&ps->smi_mutex);
3280 static int mv88e63xx_get_temp(struct dsa_switch *ds, int *temp)
3282 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3283 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
3288 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 27);
3292 *temp = (ret & 0xff) - 25;
3297 int mv88e6xxx_get_temp(struct dsa_switch *ds, int *temp)
3299 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3301 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP))
3304 if (mv88e6xxx_6320_family(ps) || mv88e6xxx_6352_family(ps))
3305 return mv88e63xx_get_temp(ds, temp);
3307 return mv88e61xx_get_temp(ds, temp);
3310 int mv88e6xxx_get_temp_limit(struct dsa_switch *ds, int *temp)
3312 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3313 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
3316 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
3321 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3325 *temp = (((ret >> 8) & 0x1f) * 5) - 25;
3330 int mv88e6xxx_set_temp_limit(struct dsa_switch *ds, int temp)
3332 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3333 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
3336 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
3339 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3342 temp = clamp_val(DIV_ROUND_CLOSEST(temp, 5) + 5, 0, 0x1f);
3343 return mv88e6xxx_phy_page_write(ds, phy, 6, 26,
3344 (ret & 0xe0ff) | (temp << 8));
3347 int mv88e6xxx_get_temp_alarm(struct dsa_switch *ds, bool *alarm)
3349 struct mv88e6xxx_priv_state *ps = ds_to_priv(ds);
3350 int phy = mv88e6xxx_6320_family(ps) ? 3 : 0;
3353 if (!mv88e6xxx_has(ps, MV88E6XXX_FLAG_TEMP_LIMIT))
3358 ret = mv88e6xxx_phy_page_read(ds, phy, 6, 26);
3362 *alarm = !!(ret & 0x40);
3366 #endif /* CONFIG_NET_DSA_HWMON */
3368 static const struct mv88e6xxx_info *
3369 mv88e6xxx_lookup_info(unsigned int prod_num, const struct mv88e6xxx_info *table,
3374 for (i = 0; i < num; ++i)
3375 if (table[i].prod_num == prod_num)
3381 const char *mv88e6xxx_drv_probe(struct device *dsa_dev, struct device *host_dev,
3382 int sw_addr, void **priv,
3383 const struct mv88e6xxx_info *table,
3386 const struct mv88e6xxx_info *info;
3387 struct mv88e6xxx_priv_state *ps;
3388 struct mii_bus *bus;
3390 int id, prod_num, rev;
3392 bus = dsa_host_dev_to_mii_bus(host_dev);
3396 id = __mv88e6xxx_reg_read(bus, sw_addr, REG_PORT(0), PORT_SWITCH_ID);
3400 prod_num = (id & 0xfff0) >> 4;
3403 info = mv88e6xxx_lookup_info(prod_num, table, num);
3409 ps = devm_kzalloc(dsa_dev, sizeof(*ps), GFP_KERNEL);
3414 ps->sw_addr = sw_addr;
3419 dev_info(&ps->bus->dev, "switch 0x%x probed: %s, revision %u\n",
3420 prod_num, name, rev);
3425 static int __init mv88e6xxx_init(void)
3427 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3428 register_switch_driver(&mv88e6131_switch_driver);
3430 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3431 register_switch_driver(&mv88e6123_switch_driver);
3433 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3434 register_switch_driver(&mv88e6352_switch_driver);
3436 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3437 register_switch_driver(&mv88e6171_switch_driver);
3441 module_init(mv88e6xxx_init);
3443 static void __exit mv88e6xxx_cleanup(void)
3445 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6171)
3446 unregister_switch_driver(&mv88e6171_switch_driver);
3448 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6352)
3449 unregister_switch_driver(&mv88e6352_switch_driver);
3451 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6123)
3452 unregister_switch_driver(&mv88e6123_switch_driver);
3454 #if IS_ENABLED(CONFIG_NET_DSA_MV88E6131)
3455 unregister_switch_driver(&mv88e6131_switch_driver);
3458 module_exit(mv88e6xxx_cleanup);
3460 MODULE_AUTHOR("Lennert Buytenhek <buytenh@wantstofly.org>");
3461 MODULE_DESCRIPTION("Driver for Marvell 88E6XXX ethernet switch chips");
3462 MODULE_LICENSE("GPL");