2 * Broadcom Starfighter 2 DSA switch driver
4 * Copyright (C) 2014, Broadcom Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
12 #include <linux/list.h>
13 #include <linux/module.h>
14 #include <linux/netdevice.h>
15 #include <linux/interrupt.h>
16 #include <linux/platform_device.h>
18 #include <linux/phy.h>
19 #include <linux/phy_fixed.h>
20 #include <linux/mii.h>
22 #include <linux/of_irq.h>
23 #include <linux/of_address.h>
25 #include <linux/ethtool.h>
26 #include <linux/if_bridge.h>
27 #include <linux/brcmphy.h>
28 #include <linux/etherdevice.h>
29 #include <net/switchdev.h>
32 #include "bcm_sf2_regs.h"
34 /* String, offset, and register size in bytes if different from 4 bytes */
35 static const struct bcm_sf2_hw_stats bcm_sf2_mib[] = {
36 { "TxOctets", 0x000, 8 },
37 { "TxDropPkts", 0x020 },
38 { "TxQPKTQ0", 0x030 },
39 { "TxBroadcastPkts", 0x040 },
40 { "TxMulticastPkts", 0x050 },
41 { "TxUnicastPKts", 0x060 },
42 { "TxCollisions", 0x070 },
43 { "TxSingleCollision", 0x080 },
44 { "TxMultipleCollision", 0x090 },
45 { "TxDeferredCollision", 0x0a0 },
46 { "TxLateCollision", 0x0b0 },
47 { "TxExcessiveCollision", 0x0c0 },
48 { "TxFrameInDisc", 0x0d0 },
49 { "TxPausePkts", 0x0e0 },
50 { "TxQPKTQ1", 0x0f0 },
51 { "TxQPKTQ2", 0x100 },
52 { "TxQPKTQ3", 0x110 },
53 { "TxQPKTQ4", 0x120 },
54 { "TxQPKTQ5", 0x130 },
55 { "RxOctets", 0x140, 8 },
56 { "RxUndersizePkts", 0x160 },
57 { "RxPausePkts", 0x170 },
58 { "RxPkts64Octets", 0x180 },
59 { "RxPkts65to127Octets", 0x190 },
60 { "RxPkts128to255Octets", 0x1a0 },
61 { "RxPkts256to511Octets", 0x1b0 },
62 { "RxPkts512to1023Octets", 0x1c0 },
63 { "RxPkts1024toMaxPktsOctets", 0x1d0 },
64 { "RxOversizePkts", 0x1e0 },
65 { "RxJabbers", 0x1f0 },
66 { "RxAlignmentErrors", 0x200 },
67 { "RxFCSErrors", 0x210 },
68 { "RxGoodOctets", 0x220, 8 },
69 { "RxDropPkts", 0x240 },
70 { "RxUnicastPkts", 0x250 },
71 { "RxMulticastPkts", 0x260 },
72 { "RxBroadcastPkts", 0x270 },
73 { "RxSAChanges", 0x280 },
74 { "RxFragments", 0x290 },
75 { "RxJumboPkt", 0x2a0 },
76 { "RxSymblErr", 0x2b0 },
77 { "InRangeErrCount", 0x2c0 },
78 { "OutRangeErrCount", 0x2d0 },
79 { "EEELpiEvent", 0x2e0 },
80 { "EEELpiDuration", 0x2f0 },
81 { "RxDiscard", 0x300, 8 },
82 { "TxQPKTQ6", 0x320 },
83 { "TxQPKTQ7", 0x330 },
84 { "TxPkts64Octets", 0x340 },
85 { "TxPkts65to127Octets", 0x350 },
86 { "TxPkts128to255Octets", 0x360 },
87 { "TxPkts256to511Ocets", 0x370 },
88 { "TxPkts512to1023Ocets", 0x380 },
89 { "TxPkts1024toMaxPktOcets", 0x390 },
92 #define BCM_SF2_STATS_SIZE ARRAY_SIZE(bcm_sf2_mib)
94 static void bcm_sf2_sw_get_strings(struct dsa_switch *ds,
95 int port, uint8_t *data)
99 for (i = 0; i < BCM_SF2_STATS_SIZE; i++)
100 memcpy(data + i * ETH_GSTRING_LEN,
101 bcm_sf2_mib[i].string, ETH_GSTRING_LEN);
104 static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds,
105 int port, uint64_t *data)
107 struct bcm_sf2_priv *priv = ds_to_priv(ds);
108 const struct bcm_sf2_hw_stats *s;
113 mutex_lock(&priv->stats_mutex);
115 /* Now fetch the per-port counters */
116 for (i = 0; i < BCM_SF2_STATS_SIZE; i++) {
119 /* Do a latched 64-bit read if needed */
120 offset = s->reg + CORE_P_MIB_OFFSET(port);
121 if (s->sizeof_stat == 8)
122 val = core_readq(priv, offset);
124 val = core_readl(priv, offset);
129 mutex_unlock(&priv->stats_mutex);
132 static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds)
134 return BCM_SF2_STATS_SIZE;
137 static char *bcm_sf2_sw_probe(struct device *host_dev, int sw_addr)
139 return "Broadcom Starfighter 2";
142 static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
144 struct bcm_sf2_priv *priv = ds_to_priv(ds);
148 /* Enable the IMP Port to be in the same VLAN as the other ports
149 * on a per-port basis such that we only have Port i and IMP in
152 for (i = 0; i < priv->hw_params.num_ports; i++) {
153 if (!((1 << i) & ds->phys_port_mask))
156 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
157 reg |= (1 << cpu_port);
158 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
162 static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
164 struct bcm_sf2_priv *priv = ds_to_priv(ds);
167 /* Enable the port memories */
168 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
169 reg &= ~P_TXQ_PSM_VDD(port);
170 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
172 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
173 reg = core_readl(priv, CORE_IMP_CTL);
174 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
175 reg &= ~(RX_DIS | TX_DIS);
176 core_writel(priv, reg, CORE_IMP_CTL);
178 /* Enable forwarding */
179 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
181 /* Enable IMP port in dumb mode */
182 reg = core_readl(priv, CORE_SWITCH_CTRL);
183 reg |= MII_DUMB_FWDG_EN;
184 core_writel(priv, reg, CORE_SWITCH_CTRL);
186 /* Resolve which bit controls the Broadcom tag */
189 val = BRCM_HDR_EN_P8;
192 val = BRCM_HDR_EN_P7;
195 val = BRCM_HDR_EN_P5;
202 /* Enable Broadcom tags for IMP port */
203 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
205 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
207 /* Enable reception Broadcom tag for CPU TX (switch RX) to
208 * allow us to tag outgoing frames
210 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
212 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
214 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
215 * allow delivering frames to the per-port net_devices
217 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
219 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
221 /* Force link status for IMP port */
222 reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
223 reg |= (MII_SW_OR | LINK_STS);
224 core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
227 static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
229 struct bcm_sf2_priv *priv = ds_to_priv(ds);
232 reg = core_readl(priv, CORE_EEE_EN_CTRL);
237 core_writel(priv, reg, CORE_EEE_EN_CTRL);
240 static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
242 struct bcm_sf2_priv *priv = ds_to_priv(ds);
245 reg = reg_readl(priv, REG_SPHY_CNTRL);
248 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
249 reg_writel(priv, reg, REG_SPHY_CNTRL);
251 reg = reg_readl(priv, REG_SPHY_CNTRL);
254 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
255 reg_writel(priv, reg, REG_SPHY_CNTRL);
259 reg_writel(priv, reg, REG_SPHY_CNTRL);
261 /* Use PHY-driven LED signaling */
263 reg = reg_readl(priv, REG_LED_CNTRL(0));
264 reg |= SPDLNK_SRC_SEL;
265 reg_writel(priv, reg, REG_LED_CNTRL(0));
269 static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
270 struct phy_device *phy)
272 struct bcm_sf2_priv *priv = ds_to_priv(ds);
273 s8 cpu_port = ds->dst[ds->index].cpu_port;
276 /* Clear the memory power down */
277 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
278 reg &= ~P_TXQ_PSM_VDD(port);
279 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
281 /* Clear the Rx and Tx disable bits and set to no spanning tree */
282 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
284 /* Re-enable the GPHY and re-apply workarounds */
285 if (port == 0 && priv->hw_params.num_gphy == 1) {
286 bcm_sf2_gphy_enable_set(ds, true);
288 /* if phy_stop() has been called before, phy
289 * will be in halted state, and phy_start()
292 * the resume path does not configure back
293 * autoneg settings, and since we hard reset
294 * the phy manually here, we need to reset the
295 * state machine also.
297 phy->state = PHY_READY;
302 /* Enable port 7 interrupts to get notified */
304 intrl2_1_mask_clear(priv, P_IRQ_MASK(P7_IRQ_OFF));
306 /* Set this port, and only this one to be in the default VLAN,
307 * if member of a bridge, restore its membership prior to
308 * bringing down this port.
310 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
311 reg &= ~PORT_VLAN_CTRL_MASK;
313 reg |= priv->port_sts[port].vlan_ctl_mask;
314 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
316 bcm_sf2_imp_vlan_setup(ds, cpu_port);
318 /* If EEE was enabled, restore it */
319 if (priv->port_sts[port].eee.eee_enabled)
320 bcm_sf2_eee_enable_set(ds, port, true);
325 static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
326 struct phy_device *phy)
328 struct bcm_sf2_priv *priv = ds_to_priv(ds);
331 if (priv->wol_ports_mask & (1 << port))
335 intrl2_1_mask_set(priv, P_IRQ_MASK(P7_IRQ_OFF));
336 intrl2_1_writel(priv, P_IRQ_MASK(P7_IRQ_OFF), INTRL2_CPU_CLEAR);
339 if (port == 0 && priv->hw_params.num_gphy == 1)
340 bcm_sf2_gphy_enable_set(ds, false);
342 if (dsa_is_cpu_port(ds, port))
345 off = CORE_G_PCTL_PORT(port);
347 reg = core_readl(priv, off);
348 reg |= RX_DIS | TX_DIS;
349 core_writel(priv, reg, off);
351 /* Power down the port memory */
352 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
353 reg |= P_TXQ_PSM_VDD(port);
354 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
357 /* Returns 0 if EEE was not enabled, or 1 otherwise
359 static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
360 struct phy_device *phy)
362 struct bcm_sf2_priv *priv = ds_to_priv(ds);
363 struct ethtool_eee *p = &priv->port_sts[port].eee;
366 p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
368 ret = phy_init_eee(phy, 0);
372 bcm_sf2_eee_enable_set(ds, port, true);
377 static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
378 struct ethtool_eee *e)
380 struct bcm_sf2_priv *priv = ds_to_priv(ds);
381 struct ethtool_eee *p = &priv->port_sts[port].eee;
384 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
385 e->eee_enabled = p->eee_enabled;
386 e->eee_active = !!(reg & (1 << port));
391 static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
392 struct phy_device *phydev,
393 struct ethtool_eee *e)
395 struct bcm_sf2_priv *priv = ds_to_priv(ds);
396 struct ethtool_eee *p = &priv->port_sts[port].eee;
398 p->eee_enabled = e->eee_enabled;
400 if (!p->eee_enabled) {
401 bcm_sf2_eee_enable_set(ds, port, false);
403 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
411 /* Fast-ageing of ARL entries for a given port, equivalent to an ARL
412 * flush for that port.
414 static int bcm_sf2_sw_fast_age_port(struct dsa_switch *ds, int port)
416 struct bcm_sf2_priv *priv = ds_to_priv(ds);
417 unsigned int timeout = 1000;
420 core_writel(priv, port, CORE_FAST_AGE_PORT);
422 reg = core_readl(priv, CORE_FAST_AGE_CTRL);
423 reg |= EN_AGE_PORT | EN_AGE_DYNAMIC | FAST_AGE_STR_DONE;
424 core_writel(priv, reg, CORE_FAST_AGE_CTRL);
427 reg = core_readl(priv, CORE_FAST_AGE_CTRL);
428 if (!(reg & FAST_AGE_STR_DONE))
437 core_writel(priv, 0, CORE_FAST_AGE_CTRL);
442 static int bcm_sf2_sw_br_join(struct dsa_switch *ds, int port,
445 struct bcm_sf2_priv *priv = ds_to_priv(ds);
449 p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
451 for (i = 0; i < priv->hw_params.num_ports; i++) {
452 if (!((1 << i) & br_port_mask))
455 /* Add this local port to the remote port VLAN control
456 * membership and update the remote port bitmask
458 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
460 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
461 priv->port_sts[i].vlan_ctl_mask = reg;
466 /* Configure the local port VLAN control membership to include
467 * remote ports and update the local port bitmask
469 core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
470 priv->port_sts[port].vlan_ctl_mask = p_ctl;
475 static int bcm_sf2_sw_br_leave(struct dsa_switch *ds, int port,
478 struct bcm_sf2_priv *priv = ds_to_priv(ds);
482 p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
484 for (i = 0; i < priv->hw_params.num_ports; i++) {
485 /* Don't touch the remaining ports */
486 if (!((1 << i) & br_port_mask))
489 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
491 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
492 priv->port_sts[port].vlan_ctl_mask = reg;
494 /* Prevent self removal to preserve isolation */
499 core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
500 priv->port_sts[port].vlan_ctl_mask = p_ctl;
505 static int bcm_sf2_sw_br_set_stp_state(struct dsa_switch *ds, int port,
508 struct bcm_sf2_priv *priv = ds_to_priv(ds);
509 u8 hw_state, cur_hw_state;
513 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
514 cur_hw_state = reg & (G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
517 case BR_STATE_DISABLED:
518 hw_state = G_MISTP_DIS_STATE;
520 case BR_STATE_LISTENING:
521 hw_state = G_MISTP_LISTEN_STATE;
523 case BR_STATE_LEARNING:
524 hw_state = G_MISTP_LEARN_STATE;
526 case BR_STATE_FORWARDING:
527 hw_state = G_MISTP_FWD_STATE;
529 case BR_STATE_BLOCKING:
530 hw_state = G_MISTP_BLOCK_STATE;
533 pr_err("%s: invalid STP state: %d\n", __func__, state);
537 /* Fast-age ARL entries if we are moving a port from Learning or
538 * Forwarding (cur_hw_state) state to Disabled, Blocking or Listening
541 if (cur_hw_state != hw_state) {
542 if (cur_hw_state >= G_MISTP_LEARN_STATE &&
543 hw_state <= G_MISTP_LISTEN_STATE) {
544 ret = bcm_sf2_sw_fast_age_port(ds, port);
546 pr_err("%s: fast-ageing failed\n", __func__);
552 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
553 reg &= ~(G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
555 core_writel(priv, reg, CORE_G_PCTL_PORT(port));
560 /* Address Resolution Logic routines */
561 static int bcm_sf2_arl_op_wait(struct bcm_sf2_priv *priv)
563 unsigned int timeout = 10;
567 reg = core_readl(priv, CORE_ARLA_RWCTL);
568 if (!(reg & ARL_STRTDN))
571 usleep_range(1000, 2000);
577 static int bcm_sf2_arl_rw_op(struct bcm_sf2_priv *priv, unsigned int op)
584 cmd = core_readl(priv, CORE_ARLA_RWCTL);
585 cmd &= ~IVL_SVL_SELECT;
591 core_writel(priv, cmd, CORE_ARLA_RWCTL);
593 return bcm_sf2_arl_op_wait(priv);
596 static int bcm_sf2_arl_read(struct bcm_sf2_priv *priv, u64 mac,
597 u16 vid, struct bcm_sf2_arl_entry *ent, u8 *idx,
603 ret = bcm_sf2_arl_op_wait(priv);
607 /* Read the 4 bins */
608 for (i = 0; i < 4; i++) {
612 mac_vid = core_readq(priv, CORE_ARLA_MACVID_ENTRY(i));
613 fwd_entry = core_readl(priv, CORE_ARLA_FWD_ENTRY(i));
614 bcm_sf2_arl_to_entry(ent, mac_vid, fwd_entry);
616 if (ent->is_valid && is_valid) {
621 /* This is the MAC we just deleted */
622 if (!is_valid && (mac_vid & mac))
629 static int bcm_sf2_arl_op(struct bcm_sf2_priv *priv, int op, int port,
630 const unsigned char *addr, u16 vid, bool is_valid)
632 struct bcm_sf2_arl_entry ent;
634 u64 mac, mac_vid = 0;
638 /* Convert the array into a 64-bit MAC */
639 mac = bcm_sf2_mac_to_u64(addr);
641 /* Perform a read for the given MAC and VID */
642 core_writeq(priv, mac, CORE_ARLA_MAC);
643 core_writel(priv, vid, CORE_ARLA_VID);
645 /* Issue a read operation for this MAC */
646 ret = bcm_sf2_arl_rw_op(priv, 1);
650 ret = bcm_sf2_arl_read(priv, mac, vid, &ent, &idx, is_valid);
651 /* If this is a read, just finish now */
655 /* We could not find a matching MAC, so reset to a new entry */
661 memset(&ent, 0, sizeof(ent));
663 ent.is_valid = is_valid;
665 ent.is_static = true;
666 memcpy(ent.mac, addr, ETH_ALEN);
667 bcm_sf2_arl_from_entry(&mac_vid, &fwd_entry, &ent);
669 core_writeq(priv, mac_vid, CORE_ARLA_MACVID_ENTRY(idx));
670 core_writel(priv, fwd_entry, CORE_ARLA_FWD_ENTRY(idx));
672 ret = bcm_sf2_arl_rw_op(priv, 0);
676 /* Re-read the entry to check */
677 return bcm_sf2_arl_read(priv, mac, vid, &ent, &idx, is_valid);
680 static int bcm_sf2_sw_fdb_prepare(struct dsa_switch *ds, int port,
681 const struct switchdev_obj_port_fdb *fdb,
682 struct switchdev_trans *trans)
684 /* We do not need to do anything specific here yet */
688 static int bcm_sf2_sw_fdb_add(struct dsa_switch *ds, int port,
689 const struct switchdev_obj_port_fdb *fdb,
690 struct switchdev_trans *trans)
692 struct bcm_sf2_priv *priv = ds_to_priv(ds);
694 return bcm_sf2_arl_op(priv, 0, port, fdb->addr, fdb->vid, true);
697 static int bcm_sf2_sw_fdb_del(struct dsa_switch *ds, int port,
698 const struct switchdev_obj_port_fdb *fdb)
700 struct bcm_sf2_priv *priv = ds_to_priv(ds);
702 return bcm_sf2_arl_op(priv, 0, port, fdb->addr, fdb->vid, false);
705 static int bcm_sf2_arl_search_wait(struct bcm_sf2_priv *priv)
707 unsigned timeout = 1000;
711 reg = core_readl(priv, CORE_ARLA_SRCH_CTL);
712 if (!(reg & ARLA_SRCH_STDN))
715 if (reg & ARLA_SRCH_VLID)
718 usleep_range(1000, 2000);
724 static void bcm_sf2_arl_search_rd(struct bcm_sf2_priv *priv, u8 idx,
725 struct bcm_sf2_arl_entry *ent)
730 mac_vid = core_readq(priv, CORE_ARLA_SRCH_RSLT_MACVID(idx));
731 fwd_entry = core_readl(priv, CORE_ARLA_SRCH_RSLT(idx));
732 bcm_sf2_arl_to_entry(ent, mac_vid, fwd_entry);
735 static int bcm_sf2_sw_fdb_copy(struct net_device *dev, int port,
736 const struct bcm_sf2_arl_entry *ent,
737 struct switchdev_obj_port_fdb *fdb,
738 int (*cb)(struct switchdev_obj *obj))
743 if (port != ent->port)
746 ether_addr_copy(fdb->addr, ent->mac);
748 fdb->ndm_state = ent->is_static ? NUD_NOARP : NUD_REACHABLE;
750 return cb(&fdb->obj);
753 static int bcm_sf2_sw_fdb_dump(struct dsa_switch *ds, int port,
754 struct switchdev_obj_port_fdb *fdb,
755 int (*cb)(struct switchdev_obj *obj))
757 struct bcm_sf2_priv *priv = ds_to_priv(ds);
758 struct net_device *dev = ds->ports[port];
759 struct bcm_sf2_arl_entry results[2];
760 unsigned int count = 0;
763 /* Start search operation */
764 core_writel(priv, ARLA_SRCH_STDN, CORE_ARLA_SRCH_CTL);
767 ret = bcm_sf2_arl_search_wait(priv);
771 /* Read both entries, then return their values back */
772 bcm_sf2_arl_search_rd(priv, 0, &results[0]);
773 ret = bcm_sf2_sw_fdb_copy(dev, port, &results[0], fdb, cb);
777 bcm_sf2_arl_search_rd(priv, 1, &results[1]);
778 ret = bcm_sf2_sw_fdb_copy(dev, port, &results[1], fdb, cb);
782 if (!results[0].is_valid && !results[1].is_valid)
785 } while (count++ < CORE_ARLA_NUM_ENTRIES);
790 static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
792 struct bcm_sf2_priv *priv = dev_id;
794 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
796 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
801 static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
803 struct bcm_sf2_priv *priv = dev_id;
805 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
807 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
809 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
810 priv->port_sts[7].link = 1;
811 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
812 priv->port_sts[7].link = 0;
817 static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
819 unsigned int timeout = 1000;
822 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
823 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
824 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
827 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
828 if (!(reg & SOFTWARE_RESET))
831 usleep_range(1000, 2000);
832 } while (timeout-- > 0);
840 static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
842 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
843 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
844 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
845 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
846 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
847 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
850 static int bcm_sf2_sw_setup(struct dsa_switch *ds)
852 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
853 struct bcm_sf2_priv *priv = ds_to_priv(ds);
854 struct device_node *dn;
861 spin_lock_init(&priv->indir_lock);
862 mutex_init(&priv->stats_mutex);
864 /* All the interesting properties are at the parent device_node
867 dn = ds->pd->of_node->parent;
869 priv->irq0 = irq_of_parse_and_map(dn, 0);
870 priv->irq1 = irq_of_parse_and_map(dn, 1);
873 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
874 *base = of_iomap(dn, i);
876 pr_err("unable to find register: %s\n", reg_names[i]);
883 ret = bcm_sf2_sw_rst(priv);
885 pr_err("unable to software reset switch: %d\n", ret);
889 /* Disable all interrupts and request them */
890 bcm_sf2_intr_disable(priv);
892 ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
895 pr_err("failed to request switch_0 IRQ\n");
899 ret = request_irq(priv->irq1, bcm_sf2_switch_1_isr, 0,
902 pr_err("failed to request switch_1 IRQ\n");
906 /* Reset the MIB counters */
907 reg = core_readl(priv, CORE_GMNCFGCFG);
909 core_writel(priv, reg, CORE_GMNCFGCFG);
911 core_writel(priv, reg, CORE_GMNCFGCFG);
913 /* Get the maximum number of ports for this switch */
914 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
915 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
916 priv->hw_params.num_ports = DSA_MAX_PORTS;
918 /* Assume a single GPHY setup if we can't read that property */
919 if (of_property_read_u32(dn, "brcm,num-gphy",
920 &priv->hw_params.num_gphy))
921 priv->hw_params.num_gphy = 1;
923 /* Enable all valid ports and disable those unused */
924 for (port = 0; port < priv->hw_params.num_ports; port++) {
925 /* IMP port receives special treatment */
926 if ((1 << port) & ds->phys_port_mask)
927 bcm_sf2_port_setup(ds, port, NULL);
928 else if (dsa_is_cpu_port(ds, port))
929 bcm_sf2_imp_setup(ds, port);
931 bcm_sf2_port_disable(ds, port, NULL);
934 /* Include the pseudo-PHY address and the broadcast PHY address to
935 * divert reads towards our workaround. This is only required for
936 * 7445D0, since 7445E0 disconnects the internal switch pseudo-PHY such
937 * that we can use the regular SWITCH_MDIO master controller instead.
939 * By default, DSA initializes ds->phys_mii_mask to ds->phys_port_mask
940 * to have a 1:1 mapping between Port address and PHY address in order
941 * to utilize the slave_mii_bus instance to read from Port PHYs. This is
942 * not what we want here, so we initialize phys_mii_mask 0 to always
943 * utilize the "master" MDIO bus backed by the "mdio-unimac" driver.
945 if (of_machine_is_compatible("brcm,bcm7445d0"))
946 ds->phys_mii_mask |= ((1 << BRCM_PSEUDO_PHY_ADDR) | (1 << 0));
948 ds->phys_mii_mask = 0;
950 rev = reg_readl(priv, REG_SWITCH_REVISION);
951 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
953 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
955 rev = reg_readl(priv, REG_PHY_REVISION);
956 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
958 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
959 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
960 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
961 priv->core, priv->irq0, priv->irq1);
966 free_irq(priv->irq0, priv);
969 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
977 static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
982 static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
984 struct bcm_sf2_priv *priv = ds_to_priv(ds);
986 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
987 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
988 * the REG_PHY_REVISION register layout is.
991 return priv->hw_params.gphy_rev;
994 static int bcm_sf2_sw_indir_rw(struct dsa_switch *ds, int op, int addr,
997 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1001 reg = reg_readl(priv, REG_SWITCH_CNTRL);
1002 reg |= MDIO_MASTER_SEL;
1003 reg_writel(priv, reg, REG_SWITCH_CNTRL);
1005 /* Page << 8 | offset */
1008 core_writel(priv, addr, reg);
1010 /* Page << 8 | offset */
1011 reg = 0x80 << 8 | regnum << 1;
1015 ret = core_readl(priv, reg);
1017 core_writel(priv, val, reg);
1019 reg = reg_readl(priv, REG_SWITCH_CNTRL);
1020 reg &= ~MDIO_MASTER_SEL;
1021 reg_writel(priv, reg, REG_SWITCH_CNTRL);
1023 return ret & 0xffff;
1026 static int bcm_sf2_sw_phy_read(struct dsa_switch *ds, int addr, int regnum)
1028 /* Intercept reads from the MDIO broadcast address or Broadcom
1029 * pseudo-PHY address
1033 case BRCM_PSEUDO_PHY_ADDR:
1034 return bcm_sf2_sw_indir_rw(ds, 1, addr, regnum, 0);
1040 static int bcm_sf2_sw_phy_write(struct dsa_switch *ds, int addr, int regnum,
1043 /* Intercept writes to the MDIO broadcast address or Broadcom
1044 * pseudo-PHY address
1048 case BRCM_PSEUDO_PHY_ADDR:
1049 bcm_sf2_sw_indir_rw(ds, 0, addr, regnum, val);
1056 static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
1057 struct phy_device *phydev)
1059 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1060 u32 id_mode_dis = 0, port_mode;
1061 const char *str = NULL;
1064 switch (phydev->interface) {
1065 case PHY_INTERFACE_MODE_RGMII:
1066 str = "RGMII (no delay)";
1068 case PHY_INTERFACE_MODE_RGMII_TXID:
1070 str = "RGMII (TX delay)";
1071 port_mode = EXT_GPHY;
1073 case PHY_INTERFACE_MODE_MII:
1075 port_mode = EXT_EPHY;
1077 case PHY_INTERFACE_MODE_REVMII:
1078 str = "Reverse MII";
1079 port_mode = EXT_REVMII;
1082 /* All other PHYs: internal and MoCA */
1086 /* If the link is down, just disable the interface to conserve power */
1087 if (!phydev->link) {
1088 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
1089 reg &= ~RGMII_MODE_EN;
1090 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
1094 /* Clear id_mode_dis bit, and the existing port mode, but
1095 * make sure we enable the RGMII block for data to pass
1097 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
1098 reg &= ~ID_MODE_DIS;
1099 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
1100 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
1102 reg |= port_mode | RGMII_MODE_EN;
1106 if (phydev->pause) {
1107 if (phydev->asym_pause)
1112 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
1114 pr_info("Port %d configured for %s\n", port, str);
1117 /* Force link settings detected from the PHY */
1119 switch (phydev->speed) {
1121 reg |= SPDSTS_1000 << SPEED_SHIFT;
1124 reg |= SPDSTS_100 << SPEED_SHIFT;
1130 if (phydev->duplex == DUPLEX_FULL)
1133 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1136 static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
1137 struct fixed_phy_status *status)
1139 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1143 duplex = core_readl(priv, CORE_DUPSTS);
1144 pause = core_readl(priv, CORE_PAUSESTS);
1148 /* Port 7 is special as we do not get link status from CORE_LNKSTS,
1149 * which means that we need to force the link at the port override
1150 * level to get the data to flow. We do use what the interrupt handler
1151 * did determine before.
1153 * For the other ports, we just force the link status, since this is
1154 * a fixed PHY device.
1157 status->link = priv->port_sts[port].link;
1158 /* For MoCA interfaces, also force a link down notification
1159 * since some version of the user-space daemon (mocad) use
1160 * cmd->autoneg to force the link, which messes up the PHY
1161 * state machine and make it go in PHY_FORCING state instead.
1164 netif_carrier_off(ds->ports[port]);
1168 status->duplex = !!(duplex & (1 << port));
1171 reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1177 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1179 if ((pause & (1 << port)) &&
1180 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
1181 status->asym_pause = 1;
1185 if (pause & (1 << port))
1189 static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
1191 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1194 bcm_sf2_intr_disable(priv);
1196 /* Disable all ports physically present including the IMP
1197 * port, the other ones have already been disabled during
1200 for (port = 0; port < DSA_MAX_PORTS; port++) {
1201 if ((1 << port) & ds->phys_port_mask ||
1202 dsa_is_cpu_port(ds, port))
1203 bcm_sf2_port_disable(ds, port, NULL);
1209 static int bcm_sf2_sw_resume(struct dsa_switch *ds)
1211 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1215 ret = bcm_sf2_sw_rst(priv);
1217 pr_err("%s: failed to software reset switch\n", __func__);
1221 if (priv->hw_params.num_gphy == 1)
1222 bcm_sf2_gphy_enable_set(ds, true);
1224 for (port = 0; port < DSA_MAX_PORTS; port++) {
1225 if ((1 << port) & ds->phys_port_mask)
1226 bcm_sf2_port_setup(ds, port, NULL);
1227 else if (dsa_is_cpu_port(ds, port))
1228 bcm_sf2_imp_setup(ds, port);
1234 static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
1235 struct ethtool_wolinfo *wol)
1237 struct net_device *p = ds->dst[ds->index].master_netdev;
1238 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1239 struct ethtool_wolinfo pwol;
1241 /* Get the parent device WoL settings */
1242 p->ethtool_ops->get_wol(p, &pwol);
1244 /* Advertise the parent device supported settings */
1245 wol->supported = pwol.supported;
1246 memset(&wol->sopass, 0, sizeof(wol->sopass));
1248 if (pwol.wolopts & WAKE_MAGICSECURE)
1249 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
1251 if (priv->wol_ports_mask & (1 << port))
1252 wol->wolopts = pwol.wolopts;
1257 static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
1258 struct ethtool_wolinfo *wol)
1260 struct net_device *p = ds->dst[ds->index].master_netdev;
1261 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1262 s8 cpu_port = ds->dst[ds->index].cpu_port;
1263 struct ethtool_wolinfo pwol;
1265 p->ethtool_ops->get_wol(p, &pwol);
1266 if (wol->wolopts & ~pwol.supported)
1270 priv->wol_ports_mask |= (1 << port);
1272 priv->wol_ports_mask &= ~(1 << port);
1274 /* If we have at least one port enabled, make sure the CPU port
1275 * is also enabled. If the CPU port is the last one enabled, we disable
1276 * it since this configuration does not make sense.
1278 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
1279 priv->wol_ports_mask |= (1 << cpu_port);
1281 priv->wol_ports_mask &= ~(1 << cpu_port);
1283 return p->ethtool_ops->set_wol(p, wol);
1286 static struct dsa_switch_driver bcm_sf2_switch_driver = {
1287 .tag_protocol = DSA_TAG_PROTO_BRCM,
1288 .priv_size = sizeof(struct bcm_sf2_priv),
1289 .probe = bcm_sf2_sw_probe,
1290 .setup = bcm_sf2_sw_setup,
1291 .set_addr = bcm_sf2_sw_set_addr,
1292 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
1293 .phy_read = bcm_sf2_sw_phy_read,
1294 .phy_write = bcm_sf2_sw_phy_write,
1295 .get_strings = bcm_sf2_sw_get_strings,
1296 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
1297 .get_sset_count = bcm_sf2_sw_get_sset_count,
1298 .adjust_link = bcm_sf2_sw_adjust_link,
1299 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
1300 .suspend = bcm_sf2_sw_suspend,
1301 .resume = bcm_sf2_sw_resume,
1302 .get_wol = bcm_sf2_sw_get_wol,
1303 .set_wol = bcm_sf2_sw_set_wol,
1304 .port_enable = bcm_sf2_port_setup,
1305 .port_disable = bcm_sf2_port_disable,
1306 .get_eee = bcm_sf2_sw_get_eee,
1307 .set_eee = bcm_sf2_sw_set_eee,
1308 .port_join_bridge = bcm_sf2_sw_br_join,
1309 .port_leave_bridge = bcm_sf2_sw_br_leave,
1310 .port_stp_update = bcm_sf2_sw_br_set_stp_state,
1311 .port_fdb_prepare = bcm_sf2_sw_fdb_prepare,
1312 .port_fdb_add = bcm_sf2_sw_fdb_add,
1313 .port_fdb_del = bcm_sf2_sw_fdb_del,
1314 .port_fdb_dump = bcm_sf2_sw_fdb_dump,
1317 static int __init bcm_sf2_init(void)
1319 register_switch_driver(&bcm_sf2_switch_driver);
1323 module_init(bcm_sf2_init);
1325 static void __exit bcm_sf2_exit(void)
1327 unregister_switch_driver(&bcm_sf2_switch_driver);
1329 module_exit(bcm_sf2_exit);
1331 MODULE_AUTHOR("Broadcom Corporation");
1332 MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1333 MODULE_LICENSE("GPL");
1334 MODULE_ALIAS("platform:brcm-sf2");