blob: c6563d212476a319c7171e8830a2f6f3c55cf40c [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0-or-later
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00002/*
3 * Broadcom Starfighter 2 DSA switch driver
4 *
5 * Copyright (C) 2014, Broadcom Corporation
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00006 */
7
8#include <linux/list.h>
9#include <linux/module.h>
10#include <linux/netdevice.h>
11#include <linux/interrupt.h>
12#include <linux/platform_device.h>
13#include <linux/phy.h>
14#include <linux/phy_fixed.h>
15#include <linux/phylink.h>
16#include <linux/mii.h>
Olivier Deprez157378f2022-04-04 15:47:50 +020017#include <linux/clk.h>
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000018#include <linux/of.h>
19#include <linux/of_irq.h>
20#include <linux/of_address.h>
21#include <linux/of_net.h>
22#include <linux/of_mdio.h>
23#include <net/dsa.h>
24#include <linux/ethtool.h>
25#include <linux/if_bridge.h>
26#include <linux/brcmphy.h>
27#include <linux/etherdevice.h>
28#include <linux/platform_data/b53.h>
29
30#include "bcm_sf2.h"
31#include "bcm_sf2_regs.h"
32#include "b53/b53_priv.h"
33#include "b53/b53_regs.h"
34
Olivier Deprez157378f2022-04-04 15:47:50 +020035/* Return the number of active ports, not counting the IMP (CPU) port */
36static unsigned int bcm_sf2_num_active_ports(struct dsa_switch *ds)
37{
38 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
39 unsigned int port, count = 0;
40
41 for (port = 0; port < ds->num_ports; port++) {
42 if (dsa_is_cpu_port(ds, port))
43 continue;
44 if (priv->port_sts[port].enabled)
45 count++;
46 }
47
48 return count;
49}
50
51static void bcm_sf2_recalc_clock(struct dsa_switch *ds)
52{
53 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
54 unsigned long new_rate;
55 unsigned int ports_active;
56 /* Frequenty in Mhz */
57 static const unsigned long rate_table[] = {
58 59220000,
59 60820000,
60 62500000,
61 62500000,
62 };
63
64 ports_active = bcm_sf2_num_active_ports(ds);
65 if (ports_active == 0 || !priv->clk_mdiv)
66 return;
67
68 /* If we overflow our table, just use the recommended operational
69 * frequency
70 */
71 if (ports_active > ARRAY_SIZE(rate_table))
72 new_rate = 90000000;
73 else
74 new_rate = rate_table[ports_active - 1];
75 clk_set_rate(priv->clk_mdiv, new_rate);
76}
77
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000078static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
79{
80 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
81 unsigned int i;
82 u32 reg, offset;
83
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000084 /* Enable the port memories */
85 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
86 reg &= ~P_TXQ_PSM_VDD(port);
87 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
88
Andrew Scullb4b6d4a2019-01-02 15:54:55 +000089 /* Enable forwarding */
90 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
91
92 /* Enable IMP port in dumb mode */
93 reg = core_readl(priv, CORE_SWITCH_CTRL);
94 reg |= MII_DUMB_FWDG_EN;
95 core_writel(priv, reg, CORE_SWITCH_CTRL);
96
97 /* Configure Traffic Class to QoS mapping, allow each priority to map
98 * to a different queue number
99 */
100 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
101 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
102 reg |= i << (PRT_TO_QID_SHIFT * i);
103 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
104
105 b53_brcm_hdr_setup(ds, port);
106
David Brazdil0f672f62019-12-10 10:32:29 +0000107 if (port == 8) {
108 if (priv->type == BCM7445_DEVICE_ID)
109 offset = CORE_STS_OVERRIDE_IMP;
110 else
111 offset = CORE_STS_OVERRIDE_IMP2;
112
113 /* Force link status for IMP port */
114 reg = core_readl(priv, offset);
115 reg |= (MII_SW_OR | LINK_STS);
Olivier Deprez0e641232021-09-23 10:07:05 +0200116 reg &= ~GMII_SPEED_UP_2G;
David Brazdil0f672f62019-12-10 10:32:29 +0000117 core_writel(priv, reg, offset);
118
119 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
120 reg = core_readl(priv, CORE_IMP_CTL);
121 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
122 reg &= ~(RX_DIS | TX_DIS);
123 core_writel(priv, reg, CORE_IMP_CTL);
124 } else {
125 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
126 reg &= ~(RX_DIS | TX_DIS);
127 core_writel(priv, reg, CORE_G_PCTL_PORT(port));
128 }
Olivier Deprez157378f2022-04-04 15:47:50 +0200129
130 priv->port_sts[port].enabled = true;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000131}
132
133static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
134{
135 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
136 u32 reg;
137
138 reg = reg_readl(priv, REG_SPHY_CNTRL);
139 if (enable) {
140 reg |= PHY_RESET;
141 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | IDDQ_GLOBAL_PWR | CK25_DIS);
142 reg_writel(priv, reg, REG_SPHY_CNTRL);
143 udelay(21);
144 reg = reg_readl(priv, REG_SPHY_CNTRL);
145 reg &= ~PHY_RESET;
146 } else {
147 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
148 reg_writel(priv, reg, REG_SPHY_CNTRL);
149 mdelay(1);
150 reg |= CK25_DIS;
151 }
152 reg_writel(priv, reg, REG_SPHY_CNTRL);
153
154 /* Use PHY-driven LED signaling */
155 if (!enable) {
156 reg = reg_readl(priv, REG_LED_CNTRL(0));
157 reg |= SPDLNK_SRC_SEL;
158 reg_writel(priv, reg, REG_LED_CNTRL(0));
159 }
160}
161
162static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
163 int port)
164{
165 unsigned int off;
166
167 switch (port) {
168 case 7:
169 off = P7_IRQ_OFF;
170 break;
171 case 0:
172 /* Port 0 interrupts are located on the first bank */
173 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
174 return;
175 default:
176 off = P_IRQ_OFF(port);
177 break;
178 }
179
180 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
181}
182
183static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
184 int port)
185{
186 unsigned int off;
187
188 switch (port) {
189 case 7:
190 off = P7_IRQ_OFF;
191 break;
192 case 0:
193 /* Port 0 interrupts are located on the first bank */
194 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
195 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
196 return;
197 default:
198 off = P_IRQ_OFF(port);
199 break;
200 }
201
202 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
203 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
204}
205
206static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
207 struct phy_device *phy)
208{
209 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
210 unsigned int i;
211 u32 reg;
212
David Brazdil0f672f62019-12-10 10:32:29 +0000213 if (!dsa_is_user_port(ds, port))
214 return 0;
215
Olivier Deprez157378f2022-04-04 15:47:50 +0200216 priv->port_sts[port].enabled = true;
217
218 bcm_sf2_recalc_clock(ds);
219
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000220 /* Clear the memory power down */
221 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
222 reg &= ~P_TXQ_PSM_VDD(port);
223 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
224
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000225 /* Enable Broadcom tags for that port if requested */
226 if (priv->brcm_tag_mask & BIT(port))
227 b53_brcm_hdr_setup(ds, port);
228
229 /* Configure Traffic Class to QoS mapping, allow each priority to map
230 * to a different queue number
231 */
232 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
233 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
234 reg |= i << (PRT_TO_QID_SHIFT * i);
235 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
236
237 /* Re-enable the GPHY and re-apply workarounds */
238 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
239 bcm_sf2_gphy_enable_set(ds, true);
240 if (phy) {
241 /* if phy_stop() has been called before, phy
242 * will be in halted state, and phy_start()
243 * will call resume.
244 *
245 * the resume path does not configure back
246 * autoneg settings, and since we hard reset
247 * the phy manually here, we need to reset the
248 * state machine also.
249 */
250 phy->state = PHY_READY;
251 phy_init_hw(phy);
252 }
253 }
254
255 /* Enable MoCA port interrupts to get notified */
256 if (port == priv->moca_port)
257 bcm_sf2_port_intr_enable(priv, port);
258
259 /* Set per-queue pause threshold to 32 */
260 core_writel(priv, 32, CORE_TXQ_THD_PAUSE_QN_PORT(port));
261
262 /* Set ACB threshold to 24 */
263 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++) {
264 reg = acb_readl(priv, ACB_QUEUE_CFG(port *
265 SF2_NUM_EGRESS_QUEUES + i));
266 reg &= ~XOFF_THRESHOLD_MASK;
267 reg |= 24;
268 acb_writel(priv, reg, ACB_QUEUE_CFG(port *
269 SF2_NUM_EGRESS_QUEUES + i));
270 }
271
272 return b53_enable_port(ds, port, phy);
273}
274
David Brazdil0f672f62019-12-10 10:32:29 +0000275static void bcm_sf2_port_disable(struct dsa_switch *ds, int port)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000276{
277 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
278 u32 reg;
279
280 /* Disable learning while in WoL mode */
281 if (priv->wol_ports_mask & (1 << port)) {
282 reg = core_readl(priv, CORE_DIS_LEARN);
283 reg |= BIT(port);
284 core_writel(priv, reg, CORE_DIS_LEARN);
285 return;
286 }
287
288 if (port == priv->moca_port)
289 bcm_sf2_port_intr_disable(priv, port);
290
291 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
292 bcm_sf2_gphy_enable_set(ds, false);
293
David Brazdil0f672f62019-12-10 10:32:29 +0000294 b53_disable_port(ds, port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000295
296 /* Power down the port memory */
297 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
298 reg |= P_TXQ_PSM_VDD(port);
299 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
Olivier Deprez157378f2022-04-04 15:47:50 +0200300
301 priv->port_sts[port].enabled = false;
302
303 bcm_sf2_recalc_clock(ds);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000304}
305
306
307static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
308 int regnum, u16 val)
309{
310 int ret = 0;
311 u32 reg;
312
313 reg = reg_readl(priv, REG_SWITCH_CNTRL);
314 reg |= MDIO_MASTER_SEL;
315 reg_writel(priv, reg, REG_SWITCH_CNTRL);
316
317 /* Page << 8 | offset */
318 reg = 0x70;
319 reg <<= 2;
320 core_writel(priv, addr, reg);
321
322 /* Page << 8 | offset */
323 reg = 0x80 << 8 | regnum << 1;
324 reg <<= 2;
325
326 if (op)
327 ret = core_readl(priv, reg);
328 else
329 core_writel(priv, val, reg);
330
331 reg = reg_readl(priv, REG_SWITCH_CNTRL);
332 reg &= ~MDIO_MASTER_SEL;
333 reg_writel(priv, reg, REG_SWITCH_CNTRL);
334
335 return ret & 0xffff;
336}
337
338static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
339{
340 struct bcm_sf2_priv *priv = bus->priv;
341
342 /* Intercept reads from Broadcom pseudo-PHY address, else, send
343 * them to our master MDIO bus controller
344 */
345 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
346 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
347 else
348 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
349}
350
351static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
352 u16 val)
353{
354 struct bcm_sf2_priv *priv = bus->priv;
355
356 /* Intercept writes to the Broadcom pseudo-PHY address, else,
357 * send them to our master MDIO bus controller
358 */
359 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
David Brazdil0f672f62019-12-10 10:32:29 +0000360 return bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000361 else
David Brazdil0f672f62019-12-10 10:32:29 +0000362 return mdiobus_write_nested(priv->master_mii_bus, addr,
363 regnum, val);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000364}
365
366static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
367{
368 struct dsa_switch *ds = dev_id;
369 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
370
371 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
372 ~priv->irq0_mask;
373 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
374
375 return IRQ_HANDLED;
376}
377
378static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
379{
380 struct dsa_switch *ds = dev_id;
381 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
382
383 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
384 ~priv->irq1_mask;
385 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
386
387 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF)) {
388 priv->port_sts[7].link = true;
389 dsa_port_phylink_mac_change(ds, 7, true);
390 }
391 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF)) {
392 priv->port_sts[7].link = false;
393 dsa_port_phylink_mac_change(ds, 7, false);
394 }
395
396 return IRQ_HANDLED;
397}
398
399static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
400{
401 unsigned int timeout = 1000;
402 u32 reg;
Olivier Deprez157378f2022-04-04 15:47:50 +0200403 int ret;
404
405 /* The watchdog reset does not work on 7278, we need to hit the
406 * "external" reset line through the reset controller.
407 */
408 if (priv->type == BCM7278_DEVICE_ID && !IS_ERR(priv->rcdev)) {
409 ret = reset_control_assert(priv->rcdev);
410 if (ret)
411 return ret;
412
413 return reset_control_deassert(priv->rcdev);
414 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000415
416 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
417 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
418 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
419
420 do {
421 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
422 if (!(reg & SOFTWARE_RESET))
423 break;
424
425 usleep_range(1000, 2000);
426 } while (timeout-- > 0);
427
428 if (timeout == 0)
429 return -ETIMEDOUT;
430
431 return 0;
432}
433
434static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
435{
436 intrl2_0_mask_set(priv, 0xffffffff);
437 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
438 intrl2_1_mask_set(priv, 0xffffffff);
439 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
440}
441
442static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
443 struct device_node *dn)
444{
445 struct device_node *port;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000446 unsigned int port_num;
Olivier Deprez157378f2022-04-04 15:47:50 +0200447 struct property *prop;
448 phy_interface_t mode;
449 int err;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000450
451 priv->moca_port = -1;
452
453 for_each_available_child_of_node(dn, port) {
454 if (of_property_read_u32(port, "reg", &port_num))
455 continue;
456
457 /* Internal PHYs get assigned a specific 'phy-mode' property
458 * value: "internal" to help flag them before MDIO probing
459 * has completed, since they might be turned off at that
460 * time
461 */
Olivier Deprez157378f2022-04-04 15:47:50 +0200462 err = of_get_phy_mode(port, &mode);
463 if (err)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000464 continue;
465
466 if (mode == PHY_INTERFACE_MODE_INTERNAL)
467 priv->int_phy_mask |= 1 << port_num;
468
469 if (mode == PHY_INTERFACE_MODE_MOCA)
470 priv->moca_port = port_num;
471
472 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
473 priv->brcm_tag_mask |= 1 << port_num;
Olivier Deprez157378f2022-04-04 15:47:50 +0200474
475 /* Ensure that port 5 is not picked up as a DSA CPU port
476 * flavour but a regular port instead. We should be using
477 * devlink to be able to set the port flavour.
478 */
479 if (port_num == 5 && priv->type == BCM7278_DEVICE_ID) {
480 prop = of_find_property(port, "ethernet", NULL);
481 if (prop)
482 of_remove_property(port, prop);
483 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000484 }
485}
486
487static int bcm_sf2_mdio_register(struct dsa_switch *ds)
488{
489 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Olivier Deprez157378f2022-04-04 15:47:50 +0200490 struct device_node *dn, *child;
491 struct phy_device *phydev;
492 struct property *prop;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000493 static int index;
Olivier Deprez157378f2022-04-04 15:47:50 +0200494 int err, reg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000495
496 /* Find our integrated MDIO bus node */
497 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
498 priv->master_mii_bus = of_mdio_find_bus(dn);
Olivier Deprez0e641232021-09-23 10:07:05 +0200499 if (!priv->master_mii_bus) {
500 of_node_put(dn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000501 return -EPROBE_DEFER;
Olivier Deprez0e641232021-09-23 10:07:05 +0200502 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000503
504 get_device(&priv->master_mii_bus->dev);
505 priv->master_mii_dn = dn;
506
Olivier Deprez157378f2022-04-04 15:47:50 +0200507 priv->slave_mii_bus = mdiobus_alloc();
Olivier Deprez0e641232021-09-23 10:07:05 +0200508 if (!priv->slave_mii_bus) {
509 of_node_put(dn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000510 return -ENOMEM;
Olivier Deprez0e641232021-09-23 10:07:05 +0200511 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000512
513 priv->slave_mii_bus->priv = priv;
514 priv->slave_mii_bus->name = "sf2 slave mii";
515 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
516 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
517 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
518 index++);
519 priv->slave_mii_bus->dev.of_node = dn;
520
521 /* Include the pseudo-PHY address to divert reads towards our
522 * workaround. This is only required for 7445D0, since 7445E0
523 * disconnects the internal switch pseudo-PHY such that we can use the
524 * regular SWITCH_MDIO master controller instead.
525 *
526 * Here we flag the pseudo PHY as needing special treatment and would
527 * otherwise make all other PHY read/writes go to the master MDIO bus
528 * controller that comes with this switch backed by the "mdio-unimac"
529 * driver.
530 */
531 if (of_machine_is_compatible("brcm,bcm7445d0"))
Olivier Deprez157378f2022-04-04 15:47:50 +0200532 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR) | (1 << 0);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000533 else
534 priv->indir_phy_mask = 0;
535
536 ds->phys_mii_mask = priv->indir_phy_mask;
537 ds->slave_mii_bus = priv->slave_mii_bus;
538 priv->slave_mii_bus->parent = ds->dev->parent;
539 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
540
Olivier Deprez157378f2022-04-04 15:47:50 +0200541 /* We need to make sure that of_phy_connect() will not work by
542 * removing the 'phandle' and 'linux,phandle' properties and
543 * unregister the existing PHY device that was already registered.
544 */
545 for_each_available_child_of_node(dn, child) {
546 if (of_property_read_u32(child, "reg", &reg) ||
547 reg >= PHY_MAX_ADDR)
548 continue;
549
550 if (!(priv->indir_phy_mask & BIT(reg)))
551 continue;
552
553 prop = of_find_property(child, "phandle", NULL);
554 if (prop)
555 of_remove_property(child, prop);
556
557 prop = of_find_property(child, "linux,phandle", NULL);
558 if (prop)
559 of_remove_property(child, prop);
560
561 phydev = of_phy_find_device(child);
562 if (phydev)
563 phy_device_remove(phydev);
564 }
565
Olivier Deprez0e641232021-09-23 10:07:05 +0200566 err = mdiobus_register(priv->slave_mii_bus);
Olivier Deprez157378f2022-04-04 15:47:50 +0200567 if (err && dn) {
568 mdiobus_free(priv->slave_mii_bus);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000569 of_node_put(dn);
Olivier Deprez157378f2022-04-04 15:47:50 +0200570 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000571
572 return err;
573}
574
575static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
576{
577 mdiobus_unregister(priv->slave_mii_bus);
Olivier Deprez157378f2022-04-04 15:47:50 +0200578 mdiobus_free(priv->slave_mii_bus);
David Brazdil0f672f62019-12-10 10:32:29 +0000579 of_node_put(priv->master_mii_dn);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000580}
581
582static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
583{
584 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
585
586 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
587 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
588 * the REG_PHY_REVISION register layout is.
589 */
Olivier Deprez0e641232021-09-23 10:07:05 +0200590 if (priv->int_phy_mask & BIT(port))
591 return priv->hw_params.gphy_rev;
592 else
593 return 0;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000594}
595
596static void bcm_sf2_sw_validate(struct dsa_switch *ds, int port,
597 unsigned long *supported,
598 struct phylink_link_state *state)
599{
David Brazdil0f672f62019-12-10 10:32:29 +0000600 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000601 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
602
603 if (!phy_interface_mode_is_rgmii(state->interface) &&
604 state->interface != PHY_INTERFACE_MODE_MII &&
605 state->interface != PHY_INTERFACE_MODE_REVMII &&
606 state->interface != PHY_INTERFACE_MODE_GMII &&
607 state->interface != PHY_INTERFACE_MODE_INTERNAL &&
608 state->interface != PHY_INTERFACE_MODE_MOCA) {
609 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
David Brazdil0f672f62019-12-10 10:32:29 +0000610 if (port != core_readl(priv, CORE_IMP0_PRT_ID))
611 dev_err(ds->dev,
612 "Unsupported interface: %d for port %d\n",
613 state->interface, port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000614 return;
615 }
616
617 /* Allow all the expected bits */
618 phylink_set(mask, Autoneg);
619 phylink_set_port_modes(mask);
620 phylink_set(mask, Pause);
621 phylink_set(mask, Asym_Pause);
622
623 /* With the exclusion of MII and Reverse MII, we support Gigabit,
624 * including Half duplex
625 */
626 if (state->interface != PHY_INTERFACE_MODE_MII &&
627 state->interface != PHY_INTERFACE_MODE_REVMII) {
628 phylink_set(mask, 1000baseT_Full);
629 phylink_set(mask, 1000baseT_Half);
630 }
631
632 phylink_set(mask, 10baseT_Half);
633 phylink_set(mask, 10baseT_Full);
634 phylink_set(mask, 100baseT_Half);
635 phylink_set(mask, 100baseT_Full);
636
637 bitmap_and(supported, supported, mask,
638 __ETHTOOL_LINK_MODE_MASK_NBITS);
639 bitmap_and(state->advertising, state->advertising, mask,
640 __ETHTOOL_LINK_MODE_MASK_NBITS);
641}
642
643static void bcm_sf2_sw_mac_config(struct dsa_switch *ds, int port,
644 unsigned int mode,
645 const struct phylink_link_state *state)
646{
647 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
648 u32 id_mode_dis = 0, port_mode;
Olivier Deprez157378f2022-04-04 15:47:50 +0200649 u32 reg;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000650
David Brazdil0f672f62019-12-10 10:32:29 +0000651 if (port == core_readl(priv, CORE_IMP0_PRT_ID))
652 return;
653
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000654 switch (state->interface) {
655 case PHY_INTERFACE_MODE_RGMII:
656 id_mode_dis = 1;
Olivier Deprez157378f2022-04-04 15:47:50 +0200657 fallthrough;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000658 case PHY_INTERFACE_MODE_RGMII_TXID:
659 port_mode = EXT_GPHY;
660 break;
661 case PHY_INTERFACE_MODE_MII:
662 port_mode = EXT_EPHY;
663 break;
664 case PHY_INTERFACE_MODE_REVMII:
665 port_mode = EXT_REVMII;
666 break;
667 default:
Olivier Deprez157378f2022-04-04 15:47:50 +0200668 /* Nothing required for all other PHYs: internal and MoCA */
669 return;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000670 }
671
672 /* Clear id_mode_dis bit, and the existing port mode, let
673 * RGMII_MODE_EN bet set by mac_link_{up,down}
674 */
675 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
676 reg &= ~ID_MODE_DIS;
677 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000678
679 reg |= port_mode;
680 if (id_mode_dis)
681 reg |= ID_MODE_DIS;
682
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000683 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000684}
685
686static void bcm_sf2_sw_mac_link_set(struct dsa_switch *ds, int port,
687 phy_interface_t interface, bool link)
688{
689 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
690 u32 reg;
691
692 if (!phy_interface_mode_is_rgmii(interface) &&
693 interface != PHY_INTERFACE_MODE_MII &&
694 interface != PHY_INTERFACE_MODE_REVMII)
695 return;
696
697 /* If the link is down, just disable the interface to conserve power */
698 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
699 if (link)
700 reg |= RGMII_MODE_EN;
701 else
702 reg &= ~RGMII_MODE_EN;
703 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
704}
705
706static void bcm_sf2_sw_mac_link_down(struct dsa_switch *ds, int port,
707 unsigned int mode,
708 phy_interface_t interface)
709{
Olivier Deprez157378f2022-04-04 15:47:50 +0200710 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
711 u32 reg, offset;
712
Olivier Deprez92d4c212022-12-06 15:05:30 +0100713 if (priv->wol_ports_mask & BIT(port))
714 return;
715
Olivier Deprez157378f2022-04-04 15:47:50 +0200716 if (port != core_readl(priv, CORE_IMP0_PRT_ID)) {
717 if (priv->type == BCM7445_DEVICE_ID)
718 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
719 else
720 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
721
722 reg = core_readl(priv, offset);
723 reg &= ~LINK_STS;
724 core_writel(priv, reg, offset);
725 }
726
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000727 bcm_sf2_sw_mac_link_set(ds, port, interface, false);
728}
729
730static void bcm_sf2_sw_mac_link_up(struct dsa_switch *ds, int port,
731 unsigned int mode,
732 phy_interface_t interface,
Olivier Deprez157378f2022-04-04 15:47:50 +0200733 struct phy_device *phydev,
734 int speed, int duplex,
735 bool tx_pause, bool rx_pause)
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000736{
737 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
738 struct ethtool_eee *p = &priv->dev->ports[port].eee;
Olivier Deprez157378f2022-04-04 15:47:50 +0200739 u32 reg, offset;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000740
741 bcm_sf2_sw_mac_link_set(ds, port, interface, true);
742
Olivier Deprez157378f2022-04-04 15:47:50 +0200743 if (port != core_readl(priv, CORE_IMP0_PRT_ID)) {
744 if (priv->type == BCM7445_DEVICE_ID)
745 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
746 else
747 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
748
749 if (interface == PHY_INTERFACE_MODE_RGMII ||
750 interface == PHY_INTERFACE_MODE_RGMII_TXID ||
751 interface == PHY_INTERFACE_MODE_MII ||
752 interface == PHY_INTERFACE_MODE_REVMII) {
753 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
754 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
755
756 if (tx_pause)
757 reg |= TX_PAUSE_EN;
758 if (rx_pause)
759 reg |= RX_PAUSE_EN;
760
761 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
762 }
763
764 reg = SW_OVERRIDE | LINK_STS;
765 switch (speed) {
766 case SPEED_1000:
767 reg |= SPDSTS_1000 << SPEED_SHIFT;
768 break;
769 case SPEED_100:
770 reg |= SPDSTS_100 << SPEED_SHIFT;
771 break;
772 }
773
774 if (duplex == DUPLEX_FULL)
775 reg |= DUPLX_MODE;
776
Olivier Deprez92d4c212022-12-06 15:05:30 +0100777 if (tx_pause)
778 reg |= TXFLOW_CNTL;
779 if (rx_pause)
780 reg |= RXFLOW_CNTL;
781
Olivier Deprez157378f2022-04-04 15:47:50 +0200782 core_writel(priv, reg, offset);
783 }
784
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000785 if (mode == MLO_AN_PHY && phydev)
786 p->eee_enabled = b53_eee_init(ds, port, phydev);
787}
788
789static void bcm_sf2_sw_fixed_state(struct dsa_switch *ds, int port,
790 struct phylink_link_state *status)
791{
792 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
793
794 status->link = false;
795
796 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
797 * which means that we need to force the link at the port override
798 * level to get the data to flow. We do use what the interrupt handler
799 * did determine before.
800 *
801 * For the other ports, we just force the link status, since this is
802 * a fixed PHY device.
803 */
804 if (port == priv->moca_port) {
805 status->link = priv->port_sts[port].link;
806 /* For MoCA interfaces, also force a link down notification
807 * since some version of the user-space daemon (mocad) use
808 * cmd->autoneg to force the link, which messes up the PHY
809 * state machine and make it go in PHY_FORCING state instead.
810 */
811 if (!status->link)
Olivier Deprez157378f2022-04-04 15:47:50 +0200812 netif_carrier_off(dsa_to_port(ds, port)->slave);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000813 status->duplex = DUPLEX_FULL;
814 } else {
815 status->link = true;
816 }
817}
818
819static void bcm_sf2_enable_acb(struct dsa_switch *ds)
820{
821 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
822 u32 reg;
823
824 /* Enable ACB globally */
825 reg = acb_readl(priv, ACB_CONTROL);
826 reg |= (ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
827 acb_writel(priv, reg, ACB_CONTROL);
828 reg &= ~(ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
829 reg |= ACB_EN | ACB_ALGORITHM;
830 acb_writel(priv, reg, ACB_CONTROL);
831}
832
833static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
834{
835 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
836 unsigned int port;
837
838 bcm_sf2_intr_disable(priv);
839
840 /* Disable all ports physically present including the IMP
841 * port, the other ones have already been disabled during
842 * bcm_sf2_sw_setup
843 */
David Brazdil0f672f62019-12-10 10:32:29 +0000844 for (port = 0; port < ds->num_ports; port++) {
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000845 if (dsa_is_user_port(ds, port) || dsa_is_cpu_port(ds, port))
David Brazdil0f672f62019-12-10 10:32:29 +0000846 bcm_sf2_port_disable(ds, port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000847 }
848
Olivier Deprez157378f2022-04-04 15:47:50 +0200849 if (!priv->wol_ports_mask)
850 clk_disable_unprepare(priv->clk);
851
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000852 return 0;
853}
854
855static int bcm_sf2_sw_resume(struct dsa_switch *ds)
856{
857 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
858 int ret;
859
Olivier Deprez157378f2022-04-04 15:47:50 +0200860 if (!priv->wol_ports_mask)
861 clk_prepare_enable(priv->clk);
862
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000863 ret = bcm_sf2_sw_rst(priv);
864 if (ret) {
865 pr_err("%s: failed to software reset switch\n", __func__);
866 return ret;
867 }
868
David Brazdil0f672f62019-12-10 10:32:29 +0000869 ret = bcm_sf2_cfp_resume(ds);
870 if (ret)
871 return ret;
872
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000873 if (priv->hw_params.num_gphy == 1)
874 bcm_sf2_gphy_enable_set(ds, true);
875
876 ds->ops->setup(ds);
877
878 return 0;
879}
880
881static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
882 struct ethtool_wolinfo *wol)
883{
Olivier Deprez157378f2022-04-04 15:47:50 +0200884 struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000885 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
David Brazdil0f672f62019-12-10 10:32:29 +0000886 struct ethtool_wolinfo pwol = { };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000887
888 /* Get the parent device WoL settings */
David Brazdil0f672f62019-12-10 10:32:29 +0000889 if (p->ethtool_ops->get_wol)
890 p->ethtool_ops->get_wol(p, &pwol);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000891
892 /* Advertise the parent device supported settings */
893 wol->supported = pwol.supported;
894 memset(&wol->sopass, 0, sizeof(wol->sopass));
895
896 if (pwol.wolopts & WAKE_MAGICSECURE)
897 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
898
899 if (priv->wol_ports_mask & (1 << port))
900 wol->wolopts = pwol.wolopts;
901 else
902 wol->wolopts = 0;
903}
904
905static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
906 struct ethtool_wolinfo *wol)
907{
Olivier Deprez157378f2022-04-04 15:47:50 +0200908 struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000909 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Olivier Deprez157378f2022-04-04 15:47:50 +0200910 s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
David Brazdil0f672f62019-12-10 10:32:29 +0000911 struct ethtool_wolinfo pwol = { };
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000912
David Brazdil0f672f62019-12-10 10:32:29 +0000913 if (p->ethtool_ops->get_wol)
914 p->ethtool_ops->get_wol(p, &pwol);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000915 if (wol->wolopts & ~pwol.supported)
916 return -EINVAL;
917
918 if (wol->wolopts)
919 priv->wol_ports_mask |= (1 << port);
920 else
921 priv->wol_ports_mask &= ~(1 << port);
922
923 /* If we have at least one port enabled, make sure the CPU port
924 * is also enabled. If the CPU port is the last one enabled, we disable
925 * it since this configuration does not make sense.
926 */
927 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
928 priv->wol_ports_mask |= (1 << cpu_port);
929 else
930 priv->wol_ports_mask &= ~(1 << cpu_port);
931
932 return p->ethtool_ops->set_wol(p, wol);
933}
934
935static int bcm_sf2_sw_setup(struct dsa_switch *ds)
936{
937 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
938 unsigned int port;
939
940 /* Enable all valid ports and disable those unused */
941 for (port = 0; port < priv->hw_params.num_ports; port++) {
942 /* IMP port receives special treatment */
943 if (dsa_is_user_port(ds, port))
944 bcm_sf2_port_setup(ds, port, NULL);
945 else if (dsa_is_cpu_port(ds, port))
946 bcm_sf2_imp_setup(ds, port);
947 else
David Brazdil0f672f62019-12-10 10:32:29 +0000948 bcm_sf2_port_disable(ds, port);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000949 }
950
951 b53_configure_vlan(ds);
952 bcm_sf2_enable_acb(ds);
953
Olivier Deprez157378f2022-04-04 15:47:50 +0200954 return b53_setup_devlink_resources(ds);
955}
956
957static void bcm_sf2_sw_teardown(struct dsa_switch *ds)
958{
959 dsa_devlink_resources_unregister(ds);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +0000960}
961
962/* The SWITCH_CORE register space is managed by b53 but operates on a page +
963 * register basis so we need to translate that into an address that the
964 * bus-glue understands.
965 */
966#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
967
968static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
969 u8 *val)
970{
971 struct bcm_sf2_priv *priv = dev->priv;
972
973 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
974
975 return 0;
976}
977
978static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
979 u16 *val)
980{
981 struct bcm_sf2_priv *priv = dev->priv;
982
983 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
984
985 return 0;
986}
987
988static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
989 u32 *val)
990{
991 struct bcm_sf2_priv *priv = dev->priv;
992
993 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
994
995 return 0;
996}
997
998static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
999 u64 *val)
1000{
1001 struct bcm_sf2_priv *priv = dev->priv;
1002
1003 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
1004
1005 return 0;
1006}
1007
1008static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
1009 u8 value)
1010{
1011 struct bcm_sf2_priv *priv = dev->priv;
1012
1013 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1014
1015 return 0;
1016}
1017
1018static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
1019 u16 value)
1020{
1021 struct bcm_sf2_priv *priv = dev->priv;
1022
1023 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1024
1025 return 0;
1026}
1027
1028static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
1029 u32 value)
1030{
1031 struct bcm_sf2_priv *priv = dev->priv;
1032
1033 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1034
1035 return 0;
1036}
1037
1038static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
1039 u64 value)
1040{
1041 struct bcm_sf2_priv *priv = dev->priv;
1042
1043 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1044
1045 return 0;
1046}
1047
1048static const struct b53_io_ops bcm_sf2_io_ops = {
1049 .read8 = bcm_sf2_core_read8,
1050 .read16 = bcm_sf2_core_read16,
1051 .read32 = bcm_sf2_core_read32,
1052 .read48 = bcm_sf2_core_read64,
1053 .read64 = bcm_sf2_core_read64,
1054 .write8 = bcm_sf2_core_write8,
1055 .write16 = bcm_sf2_core_write16,
1056 .write32 = bcm_sf2_core_write32,
1057 .write48 = bcm_sf2_core_write64,
1058 .write64 = bcm_sf2_core_write64,
1059};
1060
David Brazdil0f672f62019-12-10 10:32:29 +00001061static void bcm_sf2_sw_get_strings(struct dsa_switch *ds, int port,
1062 u32 stringset, uint8_t *data)
1063{
1064 int cnt = b53_get_sset_count(ds, port, stringset);
1065
1066 b53_get_strings(ds, port, stringset, data);
1067 bcm_sf2_cfp_get_strings(ds, port, stringset,
1068 data + cnt * ETH_GSTRING_LEN);
1069}
1070
1071static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds, int port,
1072 uint64_t *data)
1073{
1074 int cnt = b53_get_sset_count(ds, port, ETH_SS_STATS);
1075
1076 b53_get_ethtool_stats(ds, port, data);
1077 bcm_sf2_cfp_get_ethtool_stats(ds, port, data + cnt);
1078}
1079
1080static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds, int port,
1081 int sset)
1082{
1083 int cnt = b53_get_sset_count(ds, port, sset);
1084
1085 if (cnt < 0)
1086 return cnt;
1087
1088 cnt += bcm_sf2_cfp_get_sset_count(ds, port, sset);
1089
1090 return cnt;
1091}
1092
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001093static const struct dsa_switch_ops bcm_sf2_ops = {
1094 .get_tag_protocol = b53_get_tag_protocol,
1095 .setup = bcm_sf2_sw_setup,
Olivier Deprez157378f2022-04-04 15:47:50 +02001096 .teardown = bcm_sf2_sw_teardown,
David Brazdil0f672f62019-12-10 10:32:29 +00001097 .get_strings = bcm_sf2_sw_get_strings,
1098 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
1099 .get_sset_count = bcm_sf2_sw_get_sset_count,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001100 .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
1101 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
1102 .phylink_validate = bcm_sf2_sw_validate,
1103 .phylink_mac_config = bcm_sf2_sw_mac_config,
1104 .phylink_mac_link_down = bcm_sf2_sw_mac_link_down,
1105 .phylink_mac_link_up = bcm_sf2_sw_mac_link_up,
1106 .phylink_fixed_state = bcm_sf2_sw_fixed_state,
1107 .suspend = bcm_sf2_sw_suspend,
1108 .resume = bcm_sf2_sw_resume,
1109 .get_wol = bcm_sf2_sw_get_wol,
1110 .set_wol = bcm_sf2_sw_set_wol,
1111 .port_enable = bcm_sf2_port_setup,
1112 .port_disable = bcm_sf2_port_disable,
1113 .get_mac_eee = b53_get_mac_eee,
1114 .set_mac_eee = b53_set_mac_eee,
1115 .port_bridge_join = b53_br_join,
1116 .port_bridge_leave = b53_br_leave,
1117 .port_stp_state_set = b53_br_set_stp_state,
1118 .port_fast_age = b53_br_fast_age,
1119 .port_vlan_filtering = b53_vlan_filtering,
1120 .port_vlan_prepare = b53_vlan_prepare,
1121 .port_vlan_add = b53_vlan_add,
1122 .port_vlan_del = b53_vlan_del,
1123 .port_fdb_dump = b53_fdb_dump,
1124 .port_fdb_add = b53_fdb_add,
1125 .port_fdb_del = b53_fdb_del,
1126 .get_rxnfc = bcm_sf2_get_rxnfc,
1127 .set_rxnfc = bcm_sf2_set_rxnfc,
1128 .port_mirror_add = b53_mirror_add,
1129 .port_mirror_del = b53_mirror_del,
Olivier Deprez157378f2022-04-04 15:47:50 +02001130 .port_mdb_prepare = b53_mdb_prepare,
1131 .port_mdb_add = b53_mdb_add,
1132 .port_mdb_del = b53_mdb_del,
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001133};
1134
1135struct bcm_sf2_of_data {
1136 u32 type;
1137 const u16 *reg_offsets;
1138 unsigned int core_reg_align;
1139 unsigned int num_cfp_rules;
1140};
1141
1142/* Register offsets for the SWITCH_REG_* block */
1143static const u16 bcm_sf2_7445_reg_offsets[] = {
1144 [REG_SWITCH_CNTRL] = 0x00,
1145 [REG_SWITCH_STATUS] = 0x04,
1146 [REG_DIR_DATA_WRITE] = 0x08,
1147 [REG_DIR_DATA_READ] = 0x0C,
1148 [REG_SWITCH_REVISION] = 0x18,
1149 [REG_PHY_REVISION] = 0x1C,
1150 [REG_SPHY_CNTRL] = 0x2C,
1151 [REG_RGMII_0_CNTRL] = 0x34,
1152 [REG_RGMII_1_CNTRL] = 0x40,
1153 [REG_RGMII_2_CNTRL] = 0x4c,
1154 [REG_LED_0_CNTRL] = 0x90,
1155 [REG_LED_1_CNTRL] = 0x94,
1156 [REG_LED_2_CNTRL] = 0x98,
1157};
1158
1159static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1160 .type = BCM7445_DEVICE_ID,
1161 .core_reg_align = 0,
1162 .reg_offsets = bcm_sf2_7445_reg_offsets,
1163 .num_cfp_rules = 256,
1164};
1165
1166static const u16 bcm_sf2_7278_reg_offsets[] = {
1167 [REG_SWITCH_CNTRL] = 0x00,
1168 [REG_SWITCH_STATUS] = 0x04,
1169 [REG_DIR_DATA_WRITE] = 0x08,
1170 [REG_DIR_DATA_READ] = 0x0c,
1171 [REG_SWITCH_REVISION] = 0x10,
1172 [REG_PHY_REVISION] = 0x14,
1173 [REG_SPHY_CNTRL] = 0x24,
1174 [REG_RGMII_0_CNTRL] = 0xe0,
1175 [REG_RGMII_1_CNTRL] = 0xec,
1176 [REG_RGMII_2_CNTRL] = 0xf8,
1177 [REG_LED_0_CNTRL] = 0x40,
1178 [REG_LED_1_CNTRL] = 0x4c,
1179 [REG_LED_2_CNTRL] = 0x58,
1180};
1181
1182static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1183 .type = BCM7278_DEVICE_ID,
1184 .core_reg_align = 1,
1185 .reg_offsets = bcm_sf2_7278_reg_offsets,
1186 .num_cfp_rules = 128,
1187};
1188
1189static const struct of_device_id bcm_sf2_of_match[] = {
1190 { .compatible = "brcm,bcm7445-switch-v4.0",
1191 .data = &bcm_sf2_7445_data
1192 },
1193 { .compatible = "brcm,bcm7278-switch-v4.0",
1194 .data = &bcm_sf2_7278_data
1195 },
1196 { .compatible = "brcm,bcm7278-switch-v4.8",
1197 .data = &bcm_sf2_7278_data
1198 },
1199 { /* sentinel */ },
1200};
1201MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1202
1203static int bcm_sf2_sw_probe(struct platform_device *pdev)
1204{
1205 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1206 struct device_node *dn = pdev->dev.of_node;
1207 const struct of_device_id *of_id = NULL;
1208 const struct bcm_sf2_of_data *data;
1209 struct b53_platform_data *pdata;
1210 struct dsa_switch_ops *ops;
Olivier Deprez0e641232021-09-23 10:07:05 +02001211 struct device_node *ports;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001212 struct bcm_sf2_priv *priv;
1213 struct b53_device *dev;
1214 struct dsa_switch *ds;
1215 void __iomem **base;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001216 unsigned int i;
1217 u32 reg, rev;
1218 int ret;
1219
1220 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1221 if (!priv)
1222 return -ENOMEM;
1223
1224 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1225 if (!ops)
1226 return -ENOMEM;
1227
1228 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1229 if (!dev)
1230 return -ENOMEM;
1231
1232 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1233 if (!pdata)
1234 return -ENOMEM;
1235
1236 of_id = of_match_node(bcm_sf2_of_match, dn);
1237 if (!of_id || !of_id->data)
1238 return -EINVAL;
1239
1240 data = of_id->data;
1241
1242 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1243 priv->type = data->type;
1244 priv->reg_offsets = data->reg_offsets;
1245 priv->core_reg_align = data->core_reg_align;
1246 priv->num_cfp_rules = data->num_cfp_rules;
1247
Olivier Deprez157378f2022-04-04 15:47:50 +02001248 priv->rcdev = devm_reset_control_get_optional_exclusive(&pdev->dev,
1249 "switch");
1250 if (PTR_ERR(priv->rcdev) == -EPROBE_DEFER)
1251 return PTR_ERR(priv->rcdev);
1252
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001253 /* Auto-detection using standard registers will not work, so
1254 * provide an indication of what kind of device we are for
1255 * b53_common to work with
1256 */
1257 pdata->chip_id = priv->type;
1258 dev->pdata = pdata;
1259
1260 priv->dev = dev;
1261 ds = dev->ds;
1262 ds->ops = &bcm_sf2_ops;
1263
1264 /* Advertise the 8 egress queues */
1265 ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1266
1267 dev_set_drvdata(&pdev->dev, priv);
1268
1269 spin_lock_init(&priv->indir_lock);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001270 mutex_init(&priv->cfp.lock);
David Brazdil0f672f62019-12-10 10:32:29 +00001271 INIT_LIST_HEAD(&priv->cfp.rules_list);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001272
1273 /* CFP rule #0 cannot be used for specific classifications, flag it as
1274 * permanently used
1275 */
1276 set_bit(0, priv->cfp.used);
1277 set_bit(0, priv->cfp.unique);
1278
Olivier Deprez0e641232021-09-23 10:07:05 +02001279 /* Balance of_node_put() done by of_find_node_by_name() */
1280 of_node_get(dn);
1281 ports = of_find_node_by_name(dn, "ports");
1282 if (ports) {
1283 bcm_sf2_identify_ports(priv, ports);
1284 of_node_put(ports);
1285 }
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001286
1287 priv->irq0 = irq_of_parse_and_map(dn, 0);
1288 priv->irq1 = irq_of_parse_and_map(dn, 1);
1289
1290 base = &priv->core;
1291 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
David Brazdil0f672f62019-12-10 10:32:29 +00001292 *base = devm_platform_ioremap_resource(pdev, i);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001293 if (IS_ERR(*base)) {
1294 pr_err("unable to find register: %s\n", reg_names[i]);
1295 return PTR_ERR(*base);
1296 }
1297 base++;
1298 }
1299
Olivier Deprez157378f2022-04-04 15:47:50 +02001300 priv->clk = devm_clk_get_optional(&pdev->dev, "sw_switch");
1301 if (IS_ERR(priv->clk))
1302 return PTR_ERR(priv->clk);
1303
1304 clk_prepare_enable(priv->clk);
1305
1306 priv->clk_mdiv = devm_clk_get_optional(&pdev->dev, "sw_switch_mdiv");
1307 if (IS_ERR(priv->clk_mdiv)) {
1308 ret = PTR_ERR(priv->clk_mdiv);
1309 goto out_clk;
1310 }
1311
1312 clk_prepare_enable(priv->clk_mdiv);
1313
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001314 ret = bcm_sf2_sw_rst(priv);
1315 if (ret) {
1316 pr_err("unable to software reset switch: %d\n", ret);
Olivier Deprez157378f2022-04-04 15:47:50 +02001317 goto out_clk_mdiv;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001318 }
1319
David Brazdil0f672f62019-12-10 10:32:29 +00001320 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1321
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001322 ret = bcm_sf2_mdio_register(ds);
1323 if (ret) {
1324 pr_err("failed to register MDIO bus\n");
Olivier Deprez157378f2022-04-04 15:47:50 +02001325 goto out_clk_mdiv;
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001326 }
1327
David Brazdil0f672f62019-12-10 10:32:29 +00001328 bcm_sf2_gphy_enable_set(priv->dev->ds, false);
1329
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001330 ret = bcm_sf2_cfp_rst(priv);
1331 if (ret) {
1332 pr_err("failed to reset CFP\n");
1333 goto out_mdio;
1334 }
1335
1336 /* Disable all interrupts and request them */
1337 bcm_sf2_intr_disable(priv);
1338
1339 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1340 "switch_0", ds);
1341 if (ret < 0) {
1342 pr_err("failed to request switch_0 IRQ\n");
1343 goto out_mdio;
1344 }
1345
1346 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1347 "switch_1", ds);
1348 if (ret < 0) {
1349 pr_err("failed to request switch_1 IRQ\n");
1350 goto out_mdio;
1351 }
1352
1353 /* Reset the MIB counters */
1354 reg = core_readl(priv, CORE_GMNCFGCFG);
1355 reg |= RST_MIB_CNT;
1356 core_writel(priv, reg, CORE_GMNCFGCFG);
1357 reg &= ~RST_MIB_CNT;
1358 core_writel(priv, reg, CORE_GMNCFGCFG);
1359
1360 /* Get the maximum number of ports for this switch */
1361 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1362 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1363 priv->hw_params.num_ports = DSA_MAX_PORTS;
1364
1365 /* Assume a single GPHY setup if we can't read that property */
1366 if (of_property_read_u32(dn, "brcm,num-gphy",
1367 &priv->hw_params.num_gphy))
1368 priv->hw_params.num_gphy = 1;
1369
1370 rev = reg_readl(priv, REG_SWITCH_REVISION);
1371 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1372 SWITCH_TOP_REV_MASK;
1373 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1374
1375 rev = reg_readl(priv, REG_PHY_REVISION);
1376 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1377
1378 ret = b53_switch_register(dev);
1379 if (ret)
1380 goto out_mdio;
1381
David Brazdil0f672f62019-12-10 10:32:29 +00001382 dev_info(&pdev->dev,
1383 "Starfighter 2 top: %x.%02x, core: %x.%02x, IRQs: %d, %d\n",
1384 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1385 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1386 priv->irq0, priv->irq1);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001387
1388 return 0;
1389
1390out_mdio:
1391 bcm_sf2_mdio_unregister(priv);
Olivier Deprez157378f2022-04-04 15:47:50 +02001392out_clk_mdiv:
1393 clk_disable_unprepare(priv->clk_mdiv);
1394out_clk:
1395 clk_disable_unprepare(priv->clk);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001396 return ret;
1397}
1398
1399static int bcm_sf2_sw_remove(struct platform_device *pdev)
1400{
1401 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1402
1403 priv->wol_ports_mask = 0;
David Brazdil0f672f62019-12-10 10:32:29 +00001404 /* Disable interrupts */
1405 bcm_sf2_intr_disable(priv);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001406 dsa_unregister_switch(priv->dev->ds);
David Brazdil0f672f62019-12-10 10:32:29 +00001407 bcm_sf2_cfp_exit(priv->dev->ds);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001408 bcm_sf2_mdio_unregister(priv);
Olivier Deprez157378f2022-04-04 15:47:50 +02001409 clk_disable_unprepare(priv->clk_mdiv);
1410 clk_disable_unprepare(priv->clk);
1411 if (priv->type == BCM7278_DEVICE_ID && !IS_ERR(priv->rcdev))
1412 reset_control_assert(priv->rcdev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001413
1414 return 0;
1415}
1416
1417static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1418{
1419 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1420
1421 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1422 * successful MDIO bus scan to occur. If we did turn off the GPHY
1423 * before (e.g: port_disable), this will also power it back on.
1424 *
1425 * Do not rely on kexec_in_progress, just power the PHY on.
1426 */
1427 if (priv->hw_params.num_gphy == 1)
1428 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1429}
1430
1431#ifdef CONFIG_PM_SLEEP
1432static int bcm_sf2_suspend(struct device *dev)
1433{
David Brazdil0f672f62019-12-10 10:32:29 +00001434 struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001435
1436 return dsa_switch_suspend(priv->dev->ds);
1437}
1438
1439static int bcm_sf2_resume(struct device *dev)
1440{
David Brazdil0f672f62019-12-10 10:32:29 +00001441 struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
Andrew Scullb4b6d4a2019-01-02 15:54:55 +00001442
1443 return dsa_switch_resume(priv->dev->ds);
1444}
1445#endif /* CONFIG_PM_SLEEP */
1446
1447static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1448 bcm_sf2_suspend, bcm_sf2_resume);
1449
1450
1451static struct platform_driver bcm_sf2_driver = {
1452 .probe = bcm_sf2_sw_probe,
1453 .remove = bcm_sf2_sw_remove,
1454 .shutdown = bcm_sf2_sw_shutdown,
1455 .driver = {
1456 .name = "brcm-sf2",
1457 .of_match_table = bcm_sf2_of_match,
1458 .pm = &bcm_sf2_pm_ops,
1459 },
1460};
1461module_platform_driver(bcm_sf2_driver);
1462
1463MODULE_AUTHOR("Broadcom Corporation");
1464MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1465MODULE_LICENSE("GPL");
1466MODULE_ALIAS("platform:brcm-sf2");