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>
24 #include <linux/of_net.h>
25 #include <linux/of_mdio.h>
27 #include <linux/ethtool.h>
28 #include <linux/if_bridge.h>
29 #include <linux/brcmphy.h>
30 #include <linux/etherdevice.h>
31 #include <linux/platform_data/b53.h>
34 #include "bcm_sf2_regs.h"
35 #include "b53/b53_priv.h"
36 #include "b53/b53_regs.h"
38 static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
40 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
44 if (priv->type == BCM7445_DEVICE_ID)
45 offset = CORE_STS_OVERRIDE_IMP;
47 offset = CORE_STS_OVERRIDE_IMP2;
49 /* Enable the port memories */
50 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
51 reg &= ~P_TXQ_PSM_VDD(port);
52 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
54 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
55 reg = core_readl(priv, CORE_IMP_CTL);
56 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
57 reg &= ~(RX_DIS | TX_DIS);
58 core_writel(priv, reg, CORE_IMP_CTL);
60 /* Enable forwarding */
61 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
63 /* Enable IMP port in dumb mode */
64 reg = core_readl(priv, CORE_SWITCH_CTRL);
65 reg |= MII_DUMB_FWDG_EN;
66 core_writel(priv, reg, CORE_SWITCH_CTRL);
68 /* Configure Traffic Class to QoS mapping, allow each priority to map
69 * to a different queue number
71 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
72 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
73 reg |= i << (PRT_TO_QID_SHIFT * i);
74 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
76 b53_brcm_hdr_setup(ds, port);
78 /* Force link status for IMP port */
79 reg = core_readl(priv, offset);
80 reg |= (MII_SW_OR | LINK_STS);
81 core_writel(priv, reg, offset);
84 static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
86 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
89 reg = reg_readl(priv, REG_SPHY_CNTRL);
92 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | IDDQ_GLOBAL_PWR | CK25_DIS);
93 reg_writel(priv, reg, REG_SPHY_CNTRL);
95 reg = reg_readl(priv, REG_SPHY_CNTRL);
98 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
99 reg_writel(priv, reg, REG_SPHY_CNTRL);
103 reg_writel(priv, reg, REG_SPHY_CNTRL);
105 /* Use PHY-driven LED signaling */
107 reg = reg_readl(priv, REG_LED_CNTRL(0));
108 reg |= SPDLNK_SRC_SEL;
109 reg_writel(priv, reg, REG_LED_CNTRL(0));
113 static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
123 /* Port 0 interrupts are located on the first bank */
124 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
127 off = P_IRQ_OFF(port);
131 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
134 static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
144 /* Port 0 interrupts are located on the first bank */
145 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
146 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
149 off = P_IRQ_OFF(port);
153 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
154 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
157 static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
158 struct phy_device *phy)
160 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
164 /* Clear the memory power down */
165 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
166 reg &= ~P_TXQ_PSM_VDD(port);
167 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
169 /* Enable Broadcom tags for that port if requested */
170 if (priv->brcm_tag_mask & BIT(port))
171 b53_brcm_hdr_setup(ds, port);
173 /* Configure Traffic Class to QoS mapping, allow each priority to map
174 * to a different queue number
176 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
177 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
178 reg |= i << (PRT_TO_QID_SHIFT * i);
179 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
181 /* Re-enable the GPHY and re-apply workarounds */
182 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
183 bcm_sf2_gphy_enable_set(ds, true);
185 /* if phy_stop() has been called before, phy
186 * will be in halted state, and phy_start()
189 * the resume path does not configure back
190 * autoneg settings, and since we hard reset
191 * the phy manually here, we need to reset the
192 * state machine also.
194 phy->state = PHY_READY;
199 /* Enable MoCA port interrupts to get notified */
200 if (port == priv->moca_port)
201 bcm_sf2_port_intr_enable(priv, port);
203 /* Set per-queue pause threshold to 32 */
204 core_writel(priv, 32, CORE_TXQ_THD_PAUSE_QN_PORT(port));
206 /* Set ACB threshold to 24 */
207 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++) {
208 reg = acb_readl(priv, ACB_QUEUE_CFG(port *
209 SF2_NUM_EGRESS_QUEUES + i));
210 reg &= ~XOFF_THRESHOLD_MASK;
212 acb_writel(priv, reg, ACB_QUEUE_CFG(port *
213 SF2_NUM_EGRESS_QUEUES + i));
216 return b53_enable_port(ds, port, phy);
219 static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
220 struct phy_device *phy)
222 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
225 if (priv->wol_ports_mask & (1 << port))
228 if (port == priv->moca_port)
229 bcm_sf2_port_intr_disable(priv, port);
231 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
232 bcm_sf2_gphy_enable_set(ds, false);
234 if (dsa_is_cpu_port(ds, port))
237 off = CORE_G_PCTL_PORT(port);
239 b53_disable_port(ds, port, phy);
241 /* Power down the port memory */
242 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
243 reg |= P_TXQ_PSM_VDD(port);
244 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
248 static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
254 reg = reg_readl(priv, REG_SWITCH_CNTRL);
255 reg |= MDIO_MASTER_SEL;
256 reg_writel(priv, reg, REG_SWITCH_CNTRL);
258 /* Page << 8 | offset */
261 core_writel(priv, addr, reg);
263 /* Page << 8 | offset */
264 reg = 0x80 << 8 | regnum << 1;
268 ret = core_readl(priv, reg);
270 core_writel(priv, val, reg);
272 reg = reg_readl(priv, REG_SWITCH_CNTRL);
273 reg &= ~MDIO_MASTER_SEL;
274 reg_writel(priv, reg, REG_SWITCH_CNTRL);
279 static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
281 struct bcm_sf2_priv *priv = bus->priv;
283 /* Intercept reads from Broadcom pseudo-PHY address, else, send
284 * them to our master MDIO bus controller
286 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
287 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
289 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
292 static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
295 struct bcm_sf2_priv *priv = bus->priv;
297 /* Intercept writes to the Broadcom pseudo-PHY address, else,
298 * send them to our master MDIO bus controller
300 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
301 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
303 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
308 static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
310 struct bcm_sf2_priv *priv = dev_id;
312 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
314 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
319 static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
321 struct bcm_sf2_priv *priv = dev_id;
323 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
325 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
327 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
328 priv->port_sts[7].link = 1;
329 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
330 priv->port_sts[7].link = 0;
335 static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
337 unsigned int timeout = 1000;
340 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
341 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
342 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
345 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
346 if (!(reg & SOFTWARE_RESET))
349 usleep_range(1000, 2000);
350 } while (timeout-- > 0);
358 static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
360 intrl2_0_mask_set(priv, 0xffffffff);
361 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
362 intrl2_1_mask_set(priv, 0xffffffff);
363 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
366 static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
367 struct device_node *dn)
369 struct device_node *port;
371 unsigned int port_num;
373 priv->moca_port = -1;
375 for_each_available_child_of_node(dn, port) {
376 if (of_property_read_u32(port, "reg", &port_num))
379 /* Internal PHYs get assigned a specific 'phy-mode' property
380 * value: "internal" to help flag them before MDIO probing
381 * has completed, since they might be turned off at that
384 mode = of_get_phy_mode(port);
388 if (mode == PHY_INTERFACE_MODE_INTERNAL)
389 priv->int_phy_mask |= 1 << port_num;
391 if (mode == PHY_INTERFACE_MODE_MOCA)
392 priv->moca_port = port_num;
394 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
395 priv->brcm_tag_mask |= 1 << port_num;
399 static int bcm_sf2_mdio_register(struct dsa_switch *ds)
401 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
402 struct device_node *dn;
406 /* Find our integrated MDIO bus node */
407 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
408 priv->master_mii_bus = of_mdio_find_bus(dn);
409 if (!priv->master_mii_bus)
410 return -EPROBE_DEFER;
412 get_device(&priv->master_mii_bus->dev);
413 priv->master_mii_dn = dn;
415 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
416 if (!priv->slave_mii_bus)
419 priv->slave_mii_bus->priv = priv;
420 priv->slave_mii_bus->name = "sf2 slave mii";
421 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
422 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
423 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
425 priv->slave_mii_bus->dev.of_node = dn;
427 /* Include the pseudo-PHY address to divert reads towards our
428 * workaround. This is only required for 7445D0, since 7445E0
429 * disconnects the internal switch pseudo-PHY such that we can use the
430 * regular SWITCH_MDIO master controller instead.
432 * Here we flag the pseudo PHY as needing special treatment and would
433 * otherwise make all other PHY read/writes go to the master MDIO bus
434 * controller that comes with this switch backed by the "mdio-unimac"
437 if (of_machine_is_compatible("brcm,bcm7445d0"))
438 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
440 priv->indir_phy_mask = 0;
442 ds->phys_mii_mask = priv->indir_phy_mask;
443 ds->slave_mii_bus = priv->slave_mii_bus;
444 priv->slave_mii_bus->parent = ds->dev->parent;
445 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
448 err = of_mdiobus_register(priv->slave_mii_bus, dn);
450 err = mdiobus_register(priv->slave_mii_bus);
458 static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
460 mdiobus_unregister(priv->slave_mii_bus);
461 if (priv->master_mii_dn)
462 of_node_put(priv->master_mii_dn);
465 static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
467 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
469 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
470 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
471 * the REG_PHY_REVISION register layout is.
474 return priv->hw_params.gphy_rev;
477 static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
478 struct phy_device *phydev)
480 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
481 struct ethtool_eee *p = &priv->dev->ports[port].eee;
482 u32 id_mode_dis = 0, port_mode;
483 const char *str = NULL;
486 if (priv->type == BCM7445_DEVICE_ID)
487 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
489 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
491 switch (phydev->interface) {
492 case PHY_INTERFACE_MODE_RGMII:
493 str = "RGMII (no delay)";
495 case PHY_INTERFACE_MODE_RGMII_TXID:
497 str = "RGMII (TX delay)";
498 port_mode = EXT_GPHY;
500 case PHY_INTERFACE_MODE_MII:
502 port_mode = EXT_EPHY;
504 case PHY_INTERFACE_MODE_REVMII:
506 port_mode = EXT_REVMII;
509 /* All other PHYs: internal and MoCA */
513 /* If the link is down, just disable the interface to conserve power */
515 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
516 reg &= ~RGMII_MODE_EN;
517 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
521 /* Clear id_mode_dis bit, and the existing port mode, but
522 * make sure we enable the RGMII block for data to pass
524 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
526 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
527 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
529 reg |= port_mode | RGMII_MODE_EN;
534 if (phydev->asym_pause)
539 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
541 pr_info("Port %d configured for %s\n", port, str);
544 /* Force link settings detected from the PHY */
546 switch (phydev->speed) {
548 reg |= SPDSTS_1000 << SPEED_SHIFT;
551 reg |= SPDSTS_100 << SPEED_SHIFT;
557 if (phydev->duplex == DUPLEX_FULL)
560 core_writel(priv, reg, offset);
562 if (!phydev->is_pseudo_fixed_link)
563 p->eee_enabled = b53_eee_init(ds, port, phydev);
566 static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
567 struct fixed_phy_status *status)
569 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
570 u32 duplex, pause, offset;
573 if (priv->type == BCM7445_DEVICE_ID)
574 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
576 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
578 duplex = core_readl(priv, CORE_DUPSTS);
579 pause = core_readl(priv, CORE_PAUSESTS);
583 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
584 * which means that we need to force the link at the port override
585 * level to get the data to flow. We do use what the interrupt handler
586 * did determine before.
588 * For the other ports, we just force the link status, since this is
589 * a fixed PHY device.
591 if (port == priv->moca_port) {
592 status->link = priv->port_sts[port].link;
593 /* For MoCA interfaces, also force a link down notification
594 * since some version of the user-space daemon (mocad) use
595 * cmd->autoneg to force the link, which messes up the PHY
596 * state machine and make it go in PHY_FORCING state instead.
599 netif_carrier_off(ds->ports[port].slave);
603 status->duplex = !!(duplex & (1 << port));
606 reg = core_readl(priv, offset);
612 core_writel(priv, reg, offset);
614 if ((pause & (1 << port)) &&
615 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
616 status->asym_pause = 1;
620 if (pause & (1 << port))
624 static void bcm_sf2_enable_acb(struct dsa_switch *ds)
626 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
629 /* Enable ACB globally */
630 reg = acb_readl(priv, ACB_CONTROL);
631 reg |= (ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
632 acb_writel(priv, reg, ACB_CONTROL);
633 reg &= ~(ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
634 reg |= ACB_EN | ACB_ALGORITHM;
635 acb_writel(priv, reg, ACB_CONTROL);
638 static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
640 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
643 bcm_sf2_intr_disable(priv);
645 /* Disable all ports physically present including the IMP
646 * port, the other ones have already been disabled during
649 for (port = 0; port < DSA_MAX_PORTS; port++) {
650 if (dsa_is_user_port(ds, port) || dsa_is_cpu_port(ds, port))
651 bcm_sf2_port_disable(ds, port, NULL);
657 static int bcm_sf2_sw_resume(struct dsa_switch *ds)
659 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
663 ret = bcm_sf2_sw_rst(priv);
665 pr_err("%s: failed to software reset switch\n", __func__);
669 if (priv->hw_params.num_gphy == 1)
670 bcm_sf2_gphy_enable_set(ds, true);
672 for (port = 0; port < DSA_MAX_PORTS; port++) {
673 if (dsa_is_user_port(ds, port))
674 bcm_sf2_port_setup(ds, port, NULL);
675 else if (dsa_is_cpu_port(ds, port))
676 bcm_sf2_imp_setup(ds, port);
679 bcm_sf2_enable_acb(ds);
684 static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
685 struct ethtool_wolinfo *wol)
687 struct net_device *p = ds->ports[port].cpu_dp->master;
688 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
689 struct ethtool_wolinfo pwol;
691 /* Get the parent device WoL settings */
692 p->ethtool_ops->get_wol(p, &pwol);
694 /* Advertise the parent device supported settings */
695 wol->supported = pwol.supported;
696 memset(&wol->sopass, 0, sizeof(wol->sopass));
698 if (pwol.wolopts & WAKE_MAGICSECURE)
699 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
701 if (priv->wol_ports_mask & (1 << port))
702 wol->wolopts = pwol.wolopts;
707 static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
708 struct ethtool_wolinfo *wol)
710 struct net_device *p = ds->ports[port].cpu_dp->master;
711 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
712 s8 cpu_port = ds->ports[port].cpu_dp->index;
713 struct ethtool_wolinfo pwol;
715 p->ethtool_ops->get_wol(p, &pwol);
716 if (wol->wolopts & ~pwol.supported)
720 priv->wol_ports_mask |= (1 << port);
722 priv->wol_ports_mask &= ~(1 << port);
724 /* If we have at least one port enabled, make sure the CPU port
725 * is also enabled. If the CPU port is the last one enabled, we disable
726 * it since this configuration does not make sense.
728 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
729 priv->wol_ports_mask |= (1 << cpu_port);
731 priv->wol_ports_mask &= ~(1 << cpu_port);
733 return p->ethtool_ops->set_wol(p, wol);
736 static int bcm_sf2_sw_setup(struct dsa_switch *ds)
738 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
741 /* Enable all valid ports and disable those unused */
742 for (port = 0; port < priv->hw_params.num_ports; port++) {
743 /* IMP port receives special treatment */
744 if (dsa_is_user_port(ds, port))
745 bcm_sf2_port_setup(ds, port, NULL);
746 else if (dsa_is_cpu_port(ds, port))
747 bcm_sf2_imp_setup(ds, port);
749 bcm_sf2_port_disable(ds, port, NULL);
752 b53_configure_vlan(ds);
753 bcm_sf2_enable_acb(ds);
758 /* The SWITCH_CORE register space is managed by b53 but operates on a page +
759 * register basis so we need to translate that into an address that the
760 * bus-glue understands.
762 #define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
764 static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
767 struct bcm_sf2_priv *priv = dev->priv;
769 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
774 static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
777 struct bcm_sf2_priv *priv = dev->priv;
779 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
784 static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
787 struct bcm_sf2_priv *priv = dev->priv;
789 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
794 static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
797 struct bcm_sf2_priv *priv = dev->priv;
799 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
804 static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
807 struct bcm_sf2_priv *priv = dev->priv;
809 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
814 static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
817 struct bcm_sf2_priv *priv = dev->priv;
819 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
824 static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
827 struct bcm_sf2_priv *priv = dev->priv;
829 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
834 static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
837 struct bcm_sf2_priv *priv = dev->priv;
839 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
844 static const struct b53_io_ops bcm_sf2_io_ops = {
845 .read8 = bcm_sf2_core_read8,
846 .read16 = bcm_sf2_core_read16,
847 .read32 = bcm_sf2_core_read32,
848 .read48 = bcm_sf2_core_read64,
849 .read64 = bcm_sf2_core_read64,
850 .write8 = bcm_sf2_core_write8,
851 .write16 = bcm_sf2_core_write16,
852 .write32 = bcm_sf2_core_write32,
853 .write48 = bcm_sf2_core_write64,
854 .write64 = bcm_sf2_core_write64,
857 static const struct dsa_switch_ops bcm_sf2_ops = {
858 .get_tag_protocol = b53_get_tag_protocol,
859 .setup = bcm_sf2_sw_setup,
860 .get_strings = b53_get_strings,
861 .get_ethtool_stats = b53_get_ethtool_stats,
862 .get_sset_count = b53_get_sset_count,
863 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
864 .adjust_link = bcm_sf2_sw_adjust_link,
865 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
866 .suspend = bcm_sf2_sw_suspend,
867 .resume = bcm_sf2_sw_resume,
868 .get_wol = bcm_sf2_sw_get_wol,
869 .set_wol = bcm_sf2_sw_set_wol,
870 .port_enable = bcm_sf2_port_setup,
871 .port_disable = bcm_sf2_port_disable,
872 .get_mac_eee = b53_get_mac_eee,
873 .set_mac_eee = b53_set_mac_eee,
874 .port_bridge_join = b53_br_join,
875 .port_bridge_leave = b53_br_leave,
876 .port_stp_state_set = b53_br_set_stp_state,
877 .port_fast_age = b53_br_fast_age,
878 .port_vlan_filtering = b53_vlan_filtering,
879 .port_vlan_prepare = b53_vlan_prepare,
880 .port_vlan_add = b53_vlan_add,
881 .port_vlan_del = b53_vlan_del,
882 .port_fdb_dump = b53_fdb_dump,
883 .port_fdb_add = b53_fdb_add,
884 .port_fdb_del = b53_fdb_del,
885 .get_rxnfc = bcm_sf2_get_rxnfc,
886 .set_rxnfc = bcm_sf2_set_rxnfc,
887 .port_mirror_add = b53_mirror_add,
888 .port_mirror_del = b53_mirror_del,
891 struct bcm_sf2_of_data {
893 const u16 *reg_offsets;
894 unsigned int core_reg_align;
895 unsigned int num_cfp_rules;
898 /* Register offsets for the SWITCH_REG_* block */
899 static const u16 bcm_sf2_7445_reg_offsets[] = {
900 [REG_SWITCH_CNTRL] = 0x00,
901 [REG_SWITCH_STATUS] = 0x04,
902 [REG_DIR_DATA_WRITE] = 0x08,
903 [REG_DIR_DATA_READ] = 0x0C,
904 [REG_SWITCH_REVISION] = 0x18,
905 [REG_PHY_REVISION] = 0x1C,
906 [REG_SPHY_CNTRL] = 0x2C,
907 [REG_RGMII_0_CNTRL] = 0x34,
908 [REG_RGMII_1_CNTRL] = 0x40,
909 [REG_RGMII_2_CNTRL] = 0x4c,
910 [REG_LED_0_CNTRL] = 0x90,
911 [REG_LED_1_CNTRL] = 0x94,
912 [REG_LED_2_CNTRL] = 0x98,
915 static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
916 .type = BCM7445_DEVICE_ID,
918 .reg_offsets = bcm_sf2_7445_reg_offsets,
919 .num_cfp_rules = 256,
922 static const u16 bcm_sf2_7278_reg_offsets[] = {
923 [REG_SWITCH_CNTRL] = 0x00,
924 [REG_SWITCH_STATUS] = 0x04,
925 [REG_DIR_DATA_WRITE] = 0x08,
926 [REG_DIR_DATA_READ] = 0x0c,
927 [REG_SWITCH_REVISION] = 0x10,
928 [REG_PHY_REVISION] = 0x14,
929 [REG_SPHY_CNTRL] = 0x24,
930 [REG_RGMII_0_CNTRL] = 0xe0,
931 [REG_RGMII_1_CNTRL] = 0xec,
932 [REG_RGMII_2_CNTRL] = 0xf8,
933 [REG_LED_0_CNTRL] = 0x40,
934 [REG_LED_1_CNTRL] = 0x4c,
935 [REG_LED_2_CNTRL] = 0x58,
938 static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
939 .type = BCM7278_DEVICE_ID,
941 .reg_offsets = bcm_sf2_7278_reg_offsets,
942 .num_cfp_rules = 128,
945 static const struct of_device_id bcm_sf2_of_match[] = {
946 { .compatible = "brcm,bcm7445-switch-v4.0",
947 .data = &bcm_sf2_7445_data
949 { .compatible = "brcm,bcm7278-switch-v4.0",
950 .data = &bcm_sf2_7278_data
954 MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
956 static int bcm_sf2_sw_probe(struct platform_device *pdev)
958 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
959 struct device_node *dn = pdev->dev.of_node;
960 const struct of_device_id *of_id = NULL;
961 const struct bcm_sf2_of_data *data;
962 struct b53_platform_data *pdata;
963 struct dsa_switch_ops *ops;
964 struct bcm_sf2_priv *priv;
965 struct b53_device *dev;
966 struct dsa_switch *ds;
973 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
977 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
981 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
985 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
989 of_id = of_match_node(bcm_sf2_of_match, dn);
990 if (!of_id || !of_id->data)
995 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
996 priv->type = data->type;
997 priv->reg_offsets = data->reg_offsets;
998 priv->core_reg_align = data->core_reg_align;
999 priv->num_cfp_rules = data->num_cfp_rules;
1001 /* Auto-detection using standard registers will not work, so
1002 * provide an indication of what kind of device we are for
1003 * b53_common to work with
1005 pdata->chip_id = priv->type;
1010 ds->ops = &bcm_sf2_ops;
1012 /* Advertise the 8 egress queues */
1013 ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1015 dev_set_drvdata(&pdev->dev, priv);
1017 spin_lock_init(&priv->indir_lock);
1018 mutex_init(&priv->stats_mutex);
1019 mutex_init(&priv->cfp.lock);
1021 /* CFP rule #0 cannot be used for specific classifications, flag it as
1024 set_bit(0, priv->cfp.used);
1025 set_bit(0, priv->cfp.unique);
1027 bcm_sf2_identify_ports(priv, dn->child);
1029 priv->irq0 = irq_of_parse_and_map(dn, 0);
1030 priv->irq1 = irq_of_parse_and_map(dn, 1);
1033 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
1034 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1035 *base = devm_ioremap_resource(&pdev->dev, r);
1036 if (IS_ERR(*base)) {
1037 pr_err("unable to find register: %s\n", reg_names[i]);
1038 return PTR_ERR(*base);
1043 ret = bcm_sf2_sw_rst(priv);
1045 pr_err("unable to software reset switch: %d\n", ret);
1049 ret = bcm_sf2_mdio_register(ds);
1051 pr_err("failed to register MDIO bus\n");
1055 ret = bcm_sf2_cfp_rst(priv);
1057 pr_err("failed to reset CFP\n");
1061 /* Disable all interrupts and request them */
1062 bcm_sf2_intr_disable(priv);
1064 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1067 pr_err("failed to request switch_0 IRQ\n");
1071 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1074 pr_err("failed to request switch_1 IRQ\n");
1078 /* Reset the MIB counters */
1079 reg = core_readl(priv, CORE_GMNCFGCFG);
1081 core_writel(priv, reg, CORE_GMNCFGCFG);
1082 reg &= ~RST_MIB_CNT;
1083 core_writel(priv, reg, CORE_GMNCFGCFG);
1085 /* Get the maximum number of ports for this switch */
1086 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1087 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1088 priv->hw_params.num_ports = DSA_MAX_PORTS;
1090 /* Assume a single GPHY setup if we can't read that property */
1091 if (of_property_read_u32(dn, "brcm,num-gphy",
1092 &priv->hw_params.num_gphy))
1093 priv->hw_params.num_gphy = 1;
1095 rev = reg_readl(priv, REG_SWITCH_REVISION);
1096 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1097 SWITCH_TOP_REV_MASK;
1098 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1100 rev = reg_readl(priv, REG_PHY_REVISION);
1101 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1103 ret = b53_switch_register(dev);
1107 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1108 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1109 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1110 priv->core, priv->irq0, priv->irq1);
1115 bcm_sf2_mdio_unregister(priv);
1119 static int bcm_sf2_sw_remove(struct platform_device *pdev)
1121 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1123 /* Disable all ports and interrupts */
1124 priv->wol_ports_mask = 0;
1125 bcm_sf2_sw_suspend(priv->dev->ds);
1126 dsa_unregister_switch(priv->dev->ds);
1127 bcm_sf2_mdio_unregister(priv);
1132 static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1134 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1136 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1137 * successful MDIO bus scan to occur. If we did turn off the GPHY
1138 * before (e.g: port_disable), this will also power it back on.
1140 * Do not rely on kexec_in_progress, just power the PHY on.
1142 if (priv->hw_params.num_gphy == 1)
1143 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1146 #ifdef CONFIG_PM_SLEEP
1147 static int bcm_sf2_suspend(struct device *dev)
1149 struct platform_device *pdev = to_platform_device(dev);
1150 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1152 return dsa_switch_suspend(priv->dev->ds);
1155 static int bcm_sf2_resume(struct device *dev)
1157 struct platform_device *pdev = to_platform_device(dev);
1158 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1160 return dsa_switch_resume(priv->dev->ds);
1162 #endif /* CONFIG_PM_SLEEP */
1164 static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1165 bcm_sf2_suspend, bcm_sf2_resume);
1168 static struct platform_driver bcm_sf2_driver = {
1169 .probe = bcm_sf2_sw_probe,
1170 .remove = bcm_sf2_sw_remove,
1171 .shutdown = bcm_sf2_sw_shutdown,
1174 .of_match_table = bcm_sf2_of_match,
1175 .pm = &bcm_sf2_pm_ops,
1178 module_platform_driver(bcm_sf2_driver);
1180 MODULE_AUTHOR("Broadcom Corporation");
1181 MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1182 MODULE_LICENSE("GPL");
1183 MODULE_ALIAS("platform:brcm-sf2");