• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Broadcom Starfighter 2 DSA switch driver
4  *
5  * Copyright (C) 2014, Broadcom Corporation
6  */
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>
17 #include <linux/clk.h>
18 #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 
35 /* Return the number of active ports, not counting the IMP (CPU) port */
bcm_sf2_num_active_ports(struct dsa_switch * ds)36 static 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 
bcm_sf2_recalc_clock(struct dsa_switch * ds)51 static 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 
bcm_sf2_imp_setup(struct dsa_switch * ds,int port)78 static 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 
84 	/* 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 
89 	/* 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 
107 	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);
116 		reg &= ~GMII_SPEED_UP_2G;
117 		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 	}
129 
130 	priv->port_sts[port].enabled = true;
131 }
132 
bcm_sf2_gphy_enable_set(struct dsa_switch * ds,bool enable)133 static 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 
bcm_sf2_port_intr_enable(struct bcm_sf2_priv * priv,int port)162 static 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 
bcm_sf2_port_intr_disable(struct bcm_sf2_priv * priv,int port)183 static 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 
bcm_sf2_port_setup(struct dsa_switch * ds,int port,struct phy_device * phy)206 static 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 
213 	if (!dsa_is_user_port(ds, port))
214 		return 0;
215 
216 	priv->port_sts[port].enabled = true;
217 
218 	bcm_sf2_recalc_clock(ds);
219 
220 	/* 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 
225 	/* 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 
bcm_sf2_port_disable(struct dsa_switch * ds,int port)275 static void bcm_sf2_port_disable(struct dsa_switch *ds, int port)
276 {
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 
294 	b53_disable_port(ds, port);
295 
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);
300 
301 	priv->port_sts[port].enabled = false;
302 
303 	bcm_sf2_recalc_clock(ds);
304 }
305 
306 
bcm_sf2_sw_indir_rw(struct bcm_sf2_priv * priv,int op,int addr,int regnum,u16 val)307 static 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 
bcm_sf2_sw_mdio_read(struct mii_bus * bus,int addr,int regnum)338 static 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 
bcm_sf2_sw_mdio_write(struct mii_bus * bus,int addr,int regnum,u16 val)351 static 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))
360 		return bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
361 	else
362 		return mdiobus_write_nested(priv->master_mii_bus, addr,
363 				regnum, val);
364 }
365 
bcm_sf2_switch_0_isr(int irq,void * dev_id)366 static 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 
bcm_sf2_switch_1_isr(int irq,void * dev_id)378 static 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 
bcm_sf2_sw_rst(struct bcm_sf2_priv * priv)399 static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
400 {
401 	unsigned int timeout = 1000;
402 	u32 reg;
403 	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 	}
415 
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 
bcm_sf2_intr_disable(struct bcm_sf2_priv * priv)434 static 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 
bcm_sf2_identify_ports(struct bcm_sf2_priv * priv,struct device_node * dn)442 static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
443 				   struct device_node *dn)
444 {
445 	struct device_node *port;
446 	unsigned int port_num;
447 	struct property *prop;
448 	phy_interface_t mode;
449 	int err;
450 
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 		 */
462 		err = of_get_phy_mode(port, &mode);
463 		if (err)
464 			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;
474 
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 		}
484 	}
485 }
486 
bcm_sf2_mdio_register(struct dsa_switch * ds)487 static int bcm_sf2_mdio_register(struct dsa_switch *ds)
488 {
489 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
490 	struct device_node *dn, *child;
491 	struct phy_device *phydev;
492 	struct property *prop;
493 	static int index;
494 	int err, reg;
495 
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);
499 	if (!priv->master_mii_bus) {
500 		err = -EPROBE_DEFER;
501 		goto err_of_node_put;
502 	}
503 
504 	priv->master_mii_dn = dn;
505 
506 	priv->slave_mii_bus = mdiobus_alloc();
507 	if (!priv->slave_mii_bus) {
508 		err = -ENOMEM;
509 		goto err_put_master_mii_bus_dev;
510 	}
511 
512 	priv->slave_mii_bus->priv = priv;
513 	priv->slave_mii_bus->name = "sf2 slave mii";
514 	priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
515 	priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
516 	snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
517 		 index++);
518 	priv->slave_mii_bus->dev.of_node = dn;
519 
520 	/* Include the pseudo-PHY address to divert reads towards our
521 	 * workaround. This is only required for 7445D0, since 7445E0
522 	 * disconnects the internal switch pseudo-PHY such that we can use the
523 	 * regular SWITCH_MDIO master controller instead.
524 	 *
525 	 * Here we flag the pseudo PHY as needing special treatment and would
526 	 * otherwise make all other PHY read/writes go to the master MDIO bus
527 	 * controller that comes with this switch backed by the "mdio-unimac"
528 	 * driver.
529 	 */
530 	if (of_machine_is_compatible("brcm,bcm7445d0"))
531 		priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR) | (1 << 0);
532 	else
533 		priv->indir_phy_mask = 0;
534 
535 	ds->phys_mii_mask = priv->indir_phy_mask;
536 	ds->slave_mii_bus = priv->slave_mii_bus;
537 	priv->slave_mii_bus->parent = ds->dev->parent;
538 	priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
539 
540 	/* We need to make sure that of_phy_connect() will not work by
541 	 * removing the 'phandle' and 'linux,phandle' properties and
542 	 * unregister the existing PHY device that was already registered.
543 	 */
544 	for_each_available_child_of_node(dn, child) {
545 		if (of_property_read_u32(child, "reg", &reg) ||
546 		    reg >= PHY_MAX_ADDR)
547 			continue;
548 
549 		if (!(priv->indir_phy_mask & BIT(reg)))
550 			continue;
551 
552 		prop = of_find_property(child, "phandle", NULL);
553 		if (prop)
554 			of_remove_property(child, prop);
555 
556 		prop = of_find_property(child, "linux,phandle", NULL);
557 		if (prop)
558 			of_remove_property(child, prop);
559 
560 		phydev = of_phy_find_device(child);
561 		if (phydev)
562 			phy_device_remove(phydev);
563 	}
564 
565 	err = mdiobus_register(priv->slave_mii_bus);
566 	if (err && dn)
567 		goto err_free_slave_mii_bus;
568 
569 	return 0;
570 
571 err_free_slave_mii_bus:
572 	mdiobus_free(priv->slave_mii_bus);
573 err_put_master_mii_bus_dev:
574 	put_device(&priv->master_mii_bus->dev);
575 err_of_node_put:
576 	of_node_put(dn);
577 	return err;
578 }
579 
bcm_sf2_mdio_unregister(struct bcm_sf2_priv * priv)580 static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
581 {
582 	mdiobus_unregister(priv->slave_mii_bus);
583 	mdiobus_free(priv->slave_mii_bus);
584 	put_device(&priv->master_mii_bus->dev);
585 	of_node_put(priv->master_mii_dn);
586 }
587 
bcm_sf2_sw_get_phy_flags(struct dsa_switch * ds,int port)588 static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
589 {
590 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
591 
592 	/* The BCM7xxx PHY driver expects to find the integrated PHY revision
593 	 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
594 	 * the REG_PHY_REVISION register layout is.
595 	 */
596 	if (priv->int_phy_mask & BIT(port))
597 		return priv->hw_params.gphy_rev;
598 	else
599 		return 0;
600 }
601 
bcm_sf2_sw_validate(struct dsa_switch * ds,int port,unsigned long * supported,struct phylink_link_state * state)602 static void bcm_sf2_sw_validate(struct dsa_switch *ds, int port,
603 				unsigned long *supported,
604 				struct phylink_link_state *state)
605 {
606 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
607 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
608 
609 	if (!phy_interface_mode_is_rgmii(state->interface) &&
610 	    state->interface != PHY_INTERFACE_MODE_MII &&
611 	    state->interface != PHY_INTERFACE_MODE_REVMII &&
612 	    state->interface != PHY_INTERFACE_MODE_GMII &&
613 	    state->interface != PHY_INTERFACE_MODE_INTERNAL &&
614 	    state->interface != PHY_INTERFACE_MODE_MOCA) {
615 		bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
616 		if (port != core_readl(priv, CORE_IMP0_PRT_ID))
617 			dev_err(ds->dev,
618 				"Unsupported interface: %d for port %d\n",
619 				state->interface, port);
620 		return;
621 	}
622 
623 	/* Allow all the expected bits */
624 	phylink_set(mask, Autoneg);
625 	phylink_set_port_modes(mask);
626 	phylink_set(mask, Pause);
627 	phylink_set(mask, Asym_Pause);
628 
629 	/* With the exclusion of MII and Reverse MII, we support Gigabit,
630 	 * including Half duplex
631 	 */
632 	if (state->interface != PHY_INTERFACE_MODE_MII &&
633 	    state->interface != PHY_INTERFACE_MODE_REVMII) {
634 		phylink_set(mask, 1000baseT_Full);
635 		phylink_set(mask, 1000baseT_Half);
636 	}
637 
638 	phylink_set(mask, 10baseT_Half);
639 	phylink_set(mask, 10baseT_Full);
640 	phylink_set(mask, 100baseT_Half);
641 	phylink_set(mask, 100baseT_Full);
642 
643 	bitmap_and(supported, supported, mask,
644 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
645 	bitmap_and(state->advertising, state->advertising, mask,
646 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
647 }
648 
bcm_sf2_sw_mac_config(struct dsa_switch * ds,int port,unsigned int mode,const struct phylink_link_state * state)649 static void bcm_sf2_sw_mac_config(struct dsa_switch *ds, int port,
650 				  unsigned int mode,
651 				  const struct phylink_link_state *state)
652 {
653 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
654 	u32 id_mode_dis = 0, port_mode;
655 	u32 reg;
656 
657 	if (port == core_readl(priv, CORE_IMP0_PRT_ID))
658 		return;
659 
660 	switch (state->interface) {
661 	case PHY_INTERFACE_MODE_RGMII:
662 		id_mode_dis = 1;
663 		fallthrough;
664 	case PHY_INTERFACE_MODE_RGMII_TXID:
665 		port_mode = EXT_GPHY;
666 		break;
667 	case PHY_INTERFACE_MODE_MII:
668 		port_mode = EXT_EPHY;
669 		break;
670 	case PHY_INTERFACE_MODE_REVMII:
671 		port_mode = EXT_REVMII;
672 		break;
673 	default:
674 		/* Nothing required for all other PHYs: internal and MoCA */
675 		return;
676 	}
677 
678 	/* Clear id_mode_dis bit, and the existing port mode, let
679 	 * RGMII_MODE_EN bet set by mac_link_{up,down}
680 	 */
681 	reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
682 	reg &= ~ID_MODE_DIS;
683 	reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
684 
685 	reg |= port_mode;
686 	if (id_mode_dis)
687 		reg |= ID_MODE_DIS;
688 
689 	reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
690 }
691 
bcm_sf2_sw_mac_link_set(struct dsa_switch * ds,int port,phy_interface_t interface,bool link)692 static void bcm_sf2_sw_mac_link_set(struct dsa_switch *ds, int port,
693 				    phy_interface_t interface, bool link)
694 {
695 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
696 	u32 reg;
697 
698 	if (!phy_interface_mode_is_rgmii(interface) &&
699 	    interface != PHY_INTERFACE_MODE_MII &&
700 	    interface != PHY_INTERFACE_MODE_REVMII)
701 		return;
702 
703 	/* If the link is down, just disable the interface to conserve power */
704 	reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
705 	if (link)
706 		reg |= RGMII_MODE_EN;
707 	else
708 		reg &= ~RGMII_MODE_EN;
709 	reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
710 }
711 
bcm_sf2_sw_mac_link_down(struct dsa_switch * ds,int port,unsigned int mode,phy_interface_t interface)712 static void bcm_sf2_sw_mac_link_down(struct dsa_switch *ds, int port,
713 				     unsigned int mode,
714 				     phy_interface_t interface)
715 {
716 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
717 	u32 reg, offset;
718 
719 	if (priv->wol_ports_mask & BIT(port))
720 		return;
721 
722 	if (port != core_readl(priv, CORE_IMP0_PRT_ID)) {
723 		if (priv->type == BCM7445_DEVICE_ID)
724 			offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
725 		else
726 			offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
727 
728 		reg = core_readl(priv, offset);
729 		reg &= ~LINK_STS;
730 		core_writel(priv, reg, offset);
731 	}
732 
733 	bcm_sf2_sw_mac_link_set(ds, port, interface, false);
734 }
735 
bcm_sf2_sw_mac_link_up(struct dsa_switch * ds,int port,unsigned int mode,phy_interface_t interface,struct phy_device * phydev,int speed,int duplex,bool tx_pause,bool rx_pause)736 static void bcm_sf2_sw_mac_link_up(struct dsa_switch *ds, int port,
737 				   unsigned int mode,
738 				   phy_interface_t interface,
739 				   struct phy_device *phydev,
740 				   int speed, int duplex,
741 				   bool tx_pause, bool rx_pause)
742 {
743 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
744 	struct ethtool_eee *p = &priv->dev->ports[port].eee;
745 	u32 reg, offset;
746 
747 	bcm_sf2_sw_mac_link_set(ds, port, interface, true);
748 
749 	if (port != core_readl(priv, CORE_IMP0_PRT_ID)) {
750 		if (priv->type == BCM7445_DEVICE_ID)
751 			offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
752 		else
753 			offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
754 
755 		if (interface == PHY_INTERFACE_MODE_RGMII ||
756 		    interface == PHY_INTERFACE_MODE_RGMII_TXID ||
757 		    interface == PHY_INTERFACE_MODE_MII ||
758 		    interface == PHY_INTERFACE_MODE_REVMII) {
759 			reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
760 			reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
761 
762 			if (tx_pause)
763 				reg |= TX_PAUSE_EN;
764 			if (rx_pause)
765 				reg |= RX_PAUSE_EN;
766 
767 			reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
768 		}
769 
770 		reg = SW_OVERRIDE | LINK_STS;
771 		switch (speed) {
772 		case SPEED_1000:
773 			reg |= SPDSTS_1000 << SPEED_SHIFT;
774 			break;
775 		case SPEED_100:
776 			reg |= SPDSTS_100 << SPEED_SHIFT;
777 			break;
778 		}
779 
780 		if (duplex == DUPLEX_FULL)
781 			reg |= DUPLX_MODE;
782 
783 		if (tx_pause)
784 			reg |= TXFLOW_CNTL;
785 		if (rx_pause)
786 			reg |= RXFLOW_CNTL;
787 
788 		core_writel(priv, reg, offset);
789 	}
790 
791 	if (mode == MLO_AN_PHY && phydev)
792 		p->eee_enabled = b53_eee_init(ds, port, phydev);
793 }
794 
bcm_sf2_sw_fixed_state(struct dsa_switch * ds,int port,struct phylink_link_state * status)795 static void bcm_sf2_sw_fixed_state(struct dsa_switch *ds, int port,
796 				   struct phylink_link_state *status)
797 {
798 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
799 
800 	status->link = false;
801 
802 	/* MoCA port is special as we do not get link status from CORE_LNKSTS,
803 	 * which means that we need to force the link at the port override
804 	 * level to get the data to flow. We do use what the interrupt handler
805 	 * did determine before.
806 	 *
807 	 * For the other ports, we just force the link status, since this is
808 	 * a fixed PHY device.
809 	 */
810 	if (port == priv->moca_port) {
811 		status->link = priv->port_sts[port].link;
812 		/* For MoCA interfaces, also force a link down notification
813 		 * since some version of the user-space daemon (mocad) use
814 		 * cmd->autoneg to force the link, which messes up the PHY
815 		 * state machine and make it go in PHY_FORCING state instead.
816 		 */
817 		if (!status->link)
818 			netif_carrier_off(dsa_to_port(ds, port)->slave);
819 		status->duplex = DUPLEX_FULL;
820 	} else {
821 		status->link = true;
822 	}
823 }
824 
bcm_sf2_enable_acb(struct dsa_switch * ds)825 static void bcm_sf2_enable_acb(struct dsa_switch *ds)
826 {
827 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
828 	u32 reg;
829 
830 	/* Enable ACB globally */
831 	reg = acb_readl(priv, ACB_CONTROL);
832 	reg |= (ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
833 	acb_writel(priv, reg, ACB_CONTROL);
834 	reg &= ~(ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
835 	reg |= ACB_EN | ACB_ALGORITHM;
836 	acb_writel(priv, reg, ACB_CONTROL);
837 }
838 
bcm_sf2_sw_suspend(struct dsa_switch * ds)839 static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
840 {
841 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
842 	unsigned int port;
843 
844 	bcm_sf2_intr_disable(priv);
845 
846 	/* Disable all ports physically present including the IMP
847 	 * port, the other ones have already been disabled during
848 	 * bcm_sf2_sw_setup
849 	 */
850 	for (port = 0; port < ds->num_ports; port++) {
851 		if (dsa_is_user_port(ds, port) || dsa_is_cpu_port(ds, port))
852 			bcm_sf2_port_disable(ds, port);
853 	}
854 
855 	if (!priv->wol_ports_mask)
856 		clk_disable_unprepare(priv->clk);
857 
858 	return 0;
859 }
860 
bcm_sf2_sw_resume(struct dsa_switch * ds)861 static int bcm_sf2_sw_resume(struct dsa_switch *ds)
862 {
863 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
864 	int ret;
865 
866 	if (!priv->wol_ports_mask)
867 		clk_prepare_enable(priv->clk);
868 
869 	ret = bcm_sf2_sw_rst(priv);
870 	if (ret) {
871 		pr_err("%s: failed to software reset switch\n", __func__);
872 		return ret;
873 	}
874 
875 	ret = bcm_sf2_cfp_resume(ds);
876 	if (ret)
877 		return ret;
878 
879 	if (priv->hw_params.num_gphy == 1)
880 		bcm_sf2_gphy_enable_set(ds, true);
881 
882 	ds->ops->setup(ds);
883 
884 	return 0;
885 }
886 
bcm_sf2_sw_get_wol(struct dsa_switch * ds,int port,struct ethtool_wolinfo * wol)887 static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
888 			       struct ethtool_wolinfo *wol)
889 {
890 	struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
891 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
892 	struct ethtool_wolinfo pwol = { };
893 
894 	/* Get the parent device WoL settings */
895 	if (p->ethtool_ops->get_wol)
896 		p->ethtool_ops->get_wol(p, &pwol);
897 
898 	/* Advertise the parent device supported settings */
899 	wol->supported = pwol.supported;
900 	memset(&wol->sopass, 0, sizeof(wol->sopass));
901 
902 	if (pwol.wolopts & WAKE_MAGICSECURE)
903 		memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
904 
905 	if (priv->wol_ports_mask & (1 << port))
906 		wol->wolopts = pwol.wolopts;
907 	else
908 		wol->wolopts = 0;
909 }
910 
bcm_sf2_sw_set_wol(struct dsa_switch * ds,int port,struct ethtool_wolinfo * wol)911 static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
912 			      struct ethtool_wolinfo *wol)
913 {
914 	struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
915 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
916 	s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
917 	struct ethtool_wolinfo pwol =  { };
918 
919 	if (p->ethtool_ops->get_wol)
920 		p->ethtool_ops->get_wol(p, &pwol);
921 	if (wol->wolopts & ~pwol.supported)
922 		return -EINVAL;
923 
924 	if (wol->wolopts)
925 		priv->wol_ports_mask |= (1 << port);
926 	else
927 		priv->wol_ports_mask &= ~(1 << port);
928 
929 	/* If we have at least one port enabled, make sure the CPU port
930 	 * is also enabled. If the CPU port is the last one enabled, we disable
931 	 * it since this configuration does not make sense.
932 	 */
933 	if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
934 		priv->wol_ports_mask |= (1 << cpu_port);
935 	else
936 		priv->wol_ports_mask &= ~(1 << cpu_port);
937 
938 	return p->ethtool_ops->set_wol(p, wol);
939 }
940 
bcm_sf2_sw_setup(struct dsa_switch * ds)941 static int bcm_sf2_sw_setup(struct dsa_switch *ds)
942 {
943 	struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
944 	unsigned int port;
945 
946 	/* Enable all valid ports and disable those unused */
947 	for (port = 0; port < priv->hw_params.num_ports; port++) {
948 		/* IMP port receives special treatment */
949 		if (dsa_is_user_port(ds, port))
950 			bcm_sf2_port_setup(ds, port, NULL);
951 		else if (dsa_is_cpu_port(ds, port))
952 			bcm_sf2_imp_setup(ds, port);
953 		else
954 			bcm_sf2_port_disable(ds, port);
955 	}
956 
957 	b53_configure_vlan(ds);
958 	bcm_sf2_enable_acb(ds);
959 
960 	return b53_setup_devlink_resources(ds);
961 }
962 
bcm_sf2_sw_teardown(struct dsa_switch * ds)963 static void bcm_sf2_sw_teardown(struct dsa_switch *ds)
964 {
965 	dsa_devlink_resources_unregister(ds);
966 }
967 
968 /* The SWITCH_CORE register space is managed by b53 but operates on a page +
969  * register basis so we need to translate that into an address that the
970  * bus-glue understands.
971  */
972 #define SF2_PAGE_REG_MKADDR(page, reg)	((page) << 10 | (reg) << 2)
973 
bcm_sf2_core_read8(struct b53_device * dev,u8 page,u8 reg,u8 * val)974 static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
975 			      u8 *val)
976 {
977 	struct bcm_sf2_priv *priv = dev->priv;
978 
979 	*val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
980 
981 	return 0;
982 }
983 
bcm_sf2_core_read16(struct b53_device * dev,u8 page,u8 reg,u16 * val)984 static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
985 			       u16 *val)
986 {
987 	struct bcm_sf2_priv *priv = dev->priv;
988 
989 	*val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
990 
991 	return 0;
992 }
993 
bcm_sf2_core_read32(struct b53_device * dev,u8 page,u8 reg,u32 * val)994 static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
995 			       u32 *val)
996 {
997 	struct bcm_sf2_priv *priv = dev->priv;
998 
999 	*val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
1000 
1001 	return 0;
1002 }
1003 
bcm_sf2_core_read64(struct b53_device * dev,u8 page,u8 reg,u64 * val)1004 static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
1005 			       u64 *val)
1006 {
1007 	struct bcm_sf2_priv *priv = dev->priv;
1008 
1009 	*val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
1010 
1011 	return 0;
1012 }
1013 
bcm_sf2_core_write8(struct b53_device * dev,u8 page,u8 reg,u8 value)1014 static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
1015 			       u8 value)
1016 {
1017 	struct bcm_sf2_priv *priv = dev->priv;
1018 
1019 	core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1020 
1021 	return 0;
1022 }
1023 
bcm_sf2_core_write16(struct b53_device * dev,u8 page,u8 reg,u16 value)1024 static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
1025 				u16 value)
1026 {
1027 	struct bcm_sf2_priv *priv = dev->priv;
1028 
1029 	core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1030 
1031 	return 0;
1032 }
1033 
bcm_sf2_core_write32(struct b53_device * dev,u8 page,u8 reg,u32 value)1034 static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
1035 				u32 value)
1036 {
1037 	struct bcm_sf2_priv *priv = dev->priv;
1038 
1039 	core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1040 
1041 	return 0;
1042 }
1043 
bcm_sf2_core_write64(struct b53_device * dev,u8 page,u8 reg,u64 value)1044 static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
1045 				u64 value)
1046 {
1047 	struct bcm_sf2_priv *priv = dev->priv;
1048 
1049 	core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1050 
1051 	return 0;
1052 }
1053 
1054 static const struct b53_io_ops bcm_sf2_io_ops = {
1055 	.read8	= bcm_sf2_core_read8,
1056 	.read16	= bcm_sf2_core_read16,
1057 	.read32	= bcm_sf2_core_read32,
1058 	.read48	= bcm_sf2_core_read64,
1059 	.read64	= bcm_sf2_core_read64,
1060 	.write8	= bcm_sf2_core_write8,
1061 	.write16 = bcm_sf2_core_write16,
1062 	.write32 = bcm_sf2_core_write32,
1063 	.write48 = bcm_sf2_core_write64,
1064 	.write64 = bcm_sf2_core_write64,
1065 };
1066 
bcm_sf2_sw_get_strings(struct dsa_switch * ds,int port,u32 stringset,uint8_t * data)1067 static void bcm_sf2_sw_get_strings(struct dsa_switch *ds, int port,
1068 				   u32 stringset, uint8_t *data)
1069 {
1070 	int cnt = b53_get_sset_count(ds, port, stringset);
1071 
1072 	b53_get_strings(ds, port, stringset, data);
1073 	bcm_sf2_cfp_get_strings(ds, port, stringset,
1074 				data + cnt * ETH_GSTRING_LEN);
1075 }
1076 
bcm_sf2_sw_get_ethtool_stats(struct dsa_switch * ds,int port,uint64_t * data)1077 static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds, int port,
1078 					 uint64_t *data)
1079 {
1080 	int cnt = b53_get_sset_count(ds, port, ETH_SS_STATS);
1081 
1082 	b53_get_ethtool_stats(ds, port, data);
1083 	bcm_sf2_cfp_get_ethtool_stats(ds, port, data + cnt);
1084 }
1085 
bcm_sf2_sw_get_sset_count(struct dsa_switch * ds,int port,int sset)1086 static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds, int port,
1087 				     int sset)
1088 {
1089 	int cnt = b53_get_sset_count(ds, port, sset);
1090 
1091 	if (cnt < 0)
1092 		return cnt;
1093 
1094 	cnt += bcm_sf2_cfp_get_sset_count(ds, port, sset);
1095 
1096 	return cnt;
1097 }
1098 
1099 static const struct dsa_switch_ops bcm_sf2_ops = {
1100 	.get_tag_protocol	= b53_get_tag_protocol,
1101 	.setup			= bcm_sf2_sw_setup,
1102 	.teardown		= bcm_sf2_sw_teardown,
1103 	.get_strings		= bcm_sf2_sw_get_strings,
1104 	.get_ethtool_stats	= bcm_sf2_sw_get_ethtool_stats,
1105 	.get_sset_count		= bcm_sf2_sw_get_sset_count,
1106 	.get_ethtool_phy_stats	= b53_get_ethtool_phy_stats,
1107 	.get_phy_flags		= bcm_sf2_sw_get_phy_flags,
1108 	.phylink_validate	= bcm_sf2_sw_validate,
1109 	.phylink_mac_config	= bcm_sf2_sw_mac_config,
1110 	.phylink_mac_link_down	= bcm_sf2_sw_mac_link_down,
1111 	.phylink_mac_link_up	= bcm_sf2_sw_mac_link_up,
1112 	.phylink_fixed_state	= bcm_sf2_sw_fixed_state,
1113 	.suspend		= bcm_sf2_sw_suspend,
1114 	.resume			= bcm_sf2_sw_resume,
1115 	.get_wol		= bcm_sf2_sw_get_wol,
1116 	.set_wol		= bcm_sf2_sw_set_wol,
1117 	.port_enable		= bcm_sf2_port_setup,
1118 	.port_disable		= bcm_sf2_port_disable,
1119 	.get_mac_eee		= b53_get_mac_eee,
1120 	.set_mac_eee		= b53_set_mac_eee,
1121 	.port_bridge_join	= b53_br_join,
1122 	.port_bridge_leave	= b53_br_leave,
1123 	.port_stp_state_set	= b53_br_set_stp_state,
1124 	.port_fast_age		= b53_br_fast_age,
1125 	.port_vlan_filtering	= b53_vlan_filtering,
1126 	.port_vlan_prepare	= b53_vlan_prepare,
1127 	.port_vlan_add		= b53_vlan_add,
1128 	.port_vlan_del		= b53_vlan_del,
1129 	.port_fdb_dump		= b53_fdb_dump,
1130 	.port_fdb_add		= b53_fdb_add,
1131 	.port_fdb_del		= b53_fdb_del,
1132 	.get_rxnfc		= bcm_sf2_get_rxnfc,
1133 	.set_rxnfc		= bcm_sf2_set_rxnfc,
1134 	.port_mirror_add	= b53_mirror_add,
1135 	.port_mirror_del	= b53_mirror_del,
1136 	.port_mdb_prepare	= b53_mdb_prepare,
1137 	.port_mdb_add		= b53_mdb_add,
1138 	.port_mdb_del		= b53_mdb_del,
1139 };
1140 
1141 struct bcm_sf2_of_data {
1142 	u32 type;
1143 	const u16 *reg_offsets;
1144 	unsigned int core_reg_align;
1145 	unsigned int num_cfp_rules;
1146 };
1147 
1148 /* Register offsets for the SWITCH_REG_* block */
1149 static const u16 bcm_sf2_7445_reg_offsets[] = {
1150 	[REG_SWITCH_CNTRL]	= 0x00,
1151 	[REG_SWITCH_STATUS]	= 0x04,
1152 	[REG_DIR_DATA_WRITE]	= 0x08,
1153 	[REG_DIR_DATA_READ]	= 0x0C,
1154 	[REG_SWITCH_REVISION]	= 0x18,
1155 	[REG_PHY_REVISION]	= 0x1C,
1156 	[REG_SPHY_CNTRL]	= 0x2C,
1157 	[REG_RGMII_0_CNTRL]	= 0x34,
1158 	[REG_RGMII_1_CNTRL]	= 0x40,
1159 	[REG_RGMII_2_CNTRL]	= 0x4c,
1160 	[REG_LED_0_CNTRL]	= 0x90,
1161 	[REG_LED_1_CNTRL]	= 0x94,
1162 	[REG_LED_2_CNTRL]	= 0x98,
1163 };
1164 
1165 static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1166 	.type		= BCM7445_DEVICE_ID,
1167 	.core_reg_align	= 0,
1168 	.reg_offsets	= bcm_sf2_7445_reg_offsets,
1169 	.num_cfp_rules	= 256,
1170 };
1171 
1172 static const u16 bcm_sf2_7278_reg_offsets[] = {
1173 	[REG_SWITCH_CNTRL]	= 0x00,
1174 	[REG_SWITCH_STATUS]	= 0x04,
1175 	[REG_DIR_DATA_WRITE]	= 0x08,
1176 	[REG_DIR_DATA_READ]	= 0x0c,
1177 	[REG_SWITCH_REVISION]	= 0x10,
1178 	[REG_PHY_REVISION]	= 0x14,
1179 	[REG_SPHY_CNTRL]	= 0x24,
1180 	[REG_RGMII_0_CNTRL]	= 0xe0,
1181 	[REG_RGMII_1_CNTRL]	= 0xec,
1182 	[REG_RGMII_2_CNTRL]	= 0xf8,
1183 	[REG_LED_0_CNTRL]	= 0x40,
1184 	[REG_LED_1_CNTRL]	= 0x4c,
1185 	[REG_LED_2_CNTRL]	= 0x58,
1186 };
1187 
1188 static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1189 	.type		= BCM7278_DEVICE_ID,
1190 	.core_reg_align	= 1,
1191 	.reg_offsets	= bcm_sf2_7278_reg_offsets,
1192 	.num_cfp_rules	= 128,
1193 };
1194 
1195 static const struct of_device_id bcm_sf2_of_match[] = {
1196 	{ .compatible = "brcm,bcm7445-switch-v4.0",
1197 	  .data = &bcm_sf2_7445_data
1198 	},
1199 	{ .compatible = "brcm,bcm7278-switch-v4.0",
1200 	  .data = &bcm_sf2_7278_data
1201 	},
1202 	{ .compatible = "brcm,bcm7278-switch-v4.8",
1203 	  .data = &bcm_sf2_7278_data
1204 	},
1205 	{ /* sentinel */ },
1206 };
1207 MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1208 
bcm_sf2_sw_probe(struct platform_device * pdev)1209 static int bcm_sf2_sw_probe(struct platform_device *pdev)
1210 {
1211 	const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1212 	struct device_node *dn = pdev->dev.of_node;
1213 	const struct of_device_id *of_id = NULL;
1214 	const struct bcm_sf2_of_data *data;
1215 	struct b53_platform_data *pdata;
1216 	struct dsa_switch_ops *ops;
1217 	struct device_node *ports;
1218 	struct bcm_sf2_priv *priv;
1219 	struct b53_device *dev;
1220 	struct dsa_switch *ds;
1221 	void __iomem **base;
1222 	unsigned int i;
1223 	u32 reg, rev;
1224 	int ret;
1225 
1226 	priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1227 	if (!priv)
1228 		return -ENOMEM;
1229 
1230 	ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1231 	if (!ops)
1232 		return -ENOMEM;
1233 
1234 	dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1235 	if (!dev)
1236 		return -ENOMEM;
1237 
1238 	pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1239 	if (!pdata)
1240 		return -ENOMEM;
1241 
1242 	of_id = of_match_node(bcm_sf2_of_match, dn);
1243 	if (!of_id || !of_id->data)
1244 		return -EINVAL;
1245 
1246 	data = of_id->data;
1247 
1248 	/* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1249 	priv->type = data->type;
1250 	priv->reg_offsets = data->reg_offsets;
1251 	priv->core_reg_align = data->core_reg_align;
1252 	priv->num_cfp_rules = data->num_cfp_rules;
1253 
1254 	priv->rcdev = devm_reset_control_get_optional_exclusive(&pdev->dev,
1255 								"switch");
1256 	if (PTR_ERR(priv->rcdev) == -EPROBE_DEFER)
1257 		return PTR_ERR(priv->rcdev);
1258 
1259 	/* Auto-detection using standard registers will not work, so
1260 	 * provide an indication of what kind of device we are for
1261 	 * b53_common to work with
1262 	 */
1263 	pdata->chip_id = priv->type;
1264 	dev->pdata = pdata;
1265 
1266 	priv->dev = dev;
1267 	ds = dev->ds;
1268 	ds->ops = &bcm_sf2_ops;
1269 
1270 	/* Advertise the 8 egress queues */
1271 	ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1272 
1273 	dev_set_drvdata(&pdev->dev, priv);
1274 
1275 	spin_lock_init(&priv->indir_lock);
1276 	mutex_init(&priv->cfp.lock);
1277 	INIT_LIST_HEAD(&priv->cfp.rules_list);
1278 
1279 	/* CFP rule #0 cannot be used for specific classifications, flag it as
1280 	 * permanently used
1281 	 */
1282 	set_bit(0, priv->cfp.used);
1283 	set_bit(0, priv->cfp.unique);
1284 
1285 	/* Balance of_node_put() done by of_find_node_by_name() */
1286 	of_node_get(dn);
1287 	ports = of_find_node_by_name(dn, "ports");
1288 	if (ports) {
1289 		bcm_sf2_identify_ports(priv, ports);
1290 		of_node_put(ports);
1291 	}
1292 
1293 	priv->irq0 = irq_of_parse_and_map(dn, 0);
1294 	priv->irq1 = irq_of_parse_and_map(dn, 1);
1295 
1296 	base = &priv->core;
1297 	for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
1298 		*base = devm_platform_ioremap_resource(pdev, i);
1299 		if (IS_ERR(*base)) {
1300 			pr_err("unable to find register: %s\n", reg_names[i]);
1301 			return PTR_ERR(*base);
1302 		}
1303 		base++;
1304 	}
1305 
1306 	priv->clk = devm_clk_get_optional(&pdev->dev, "sw_switch");
1307 	if (IS_ERR(priv->clk))
1308 		return PTR_ERR(priv->clk);
1309 
1310 	ret = clk_prepare_enable(priv->clk);
1311 	if (ret)
1312 		return ret;
1313 
1314 	priv->clk_mdiv = devm_clk_get_optional(&pdev->dev, "sw_switch_mdiv");
1315 	if (IS_ERR(priv->clk_mdiv)) {
1316 		ret = PTR_ERR(priv->clk_mdiv);
1317 		goto out_clk;
1318 	}
1319 
1320 	ret = clk_prepare_enable(priv->clk_mdiv);
1321 	if (ret)
1322 		goto out_clk;
1323 
1324 	ret = bcm_sf2_sw_rst(priv);
1325 	if (ret) {
1326 		pr_err("unable to software reset switch: %d\n", ret);
1327 		goto out_clk_mdiv;
1328 	}
1329 
1330 	bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1331 
1332 	ret = bcm_sf2_mdio_register(ds);
1333 	if (ret) {
1334 		pr_err("failed to register MDIO bus\n");
1335 		goto out_clk_mdiv;
1336 	}
1337 
1338 	bcm_sf2_gphy_enable_set(priv->dev->ds, false);
1339 
1340 	ret = bcm_sf2_cfp_rst(priv);
1341 	if (ret) {
1342 		pr_err("failed to reset CFP\n");
1343 		goto out_mdio;
1344 	}
1345 
1346 	/* Disable all interrupts and request them */
1347 	bcm_sf2_intr_disable(priv);
1348 
1349 	ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1350 			       "switch_0", ds);
1351 	if (ret < 0) {
1352 		pr_err("failed to request switch_0 IRQ\n");
1353 		goto out_mdio;
1354 	}
1355 
1356 	ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1357 			       "switch_1", ds);
1358 	if (ret < 0) {
1359 		pr_err("failed to request switch_1 IRQ\n");
1360 		goto out_mdio;
1361 	}
1362 
1363 	/* Reset the MIB counters */
1364 	reg = core_readl(priv, CORE_GMNCFGCFG);
1365 	reg |= RST_MIB_CNT;
1366 	core_writel(priv, reg, CORE_GMNCFGCFG);
1367 	reg &= ~RST_MIB_CNT;
1368 	core_writel(priv, reg, CORE_GMNCFGCFG);
1369 
1370 	/* Get the maximum number of ports for this switch */
1371 	priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1372 	if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1373 		priv->hw_params.num_ports = DSA_MAX_PORTS;
1374 
1375 	/* Assume a single GPHY setup if we can't read that property */
1376 	if (of_property_read_u32(dn, "brcm,num-gphy",
1377 				 &priv->hw_params.num_gphy))
1378 		priv->hw_params.num_gphy = 1;
1379 
1380 	rev = reg_readl(priv, REG_SWITCH_REVISION);
1381 	priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1382 					SWITCH_TOP_REV_MASK;
1383 	priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1384 
1385 	rev = reg_readl(priv, REG_PHY_REVISION);
1386 	priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1387 
1388 	ret = b53_switch_register(dev);
1389 	if (ret)
1390 		goto out_mdio;
1391 
1392 	dev_info(&pdev->dev,
1393 		 "Starfighter 2 top: %x.%02x, core: %x.%02x, IRQs: %d, %d\n",
1394 		 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1395 		 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1396 		 priv->irq0, priv->irq1);
1397 
1398 	return 0;
1399 
1400 out_mdio:
1401 	bcm_sf2_mdio_unregister(priv);
1402 out_clk_mdiv:
1403 	clk_disable_unprepare(priv->clk_mdiv);
1404 out_clk:
1405 	clk_disable_unprepare(priv->clk);
1406 	return ret;
1407 }
1408 
bcm_sf2_sw_remove(struct platform_device * pdev)1409 static int bcm_sf2_sw_remove(struct platform_device *pdev)
1410 {
1411 	struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1412 
1413 	priv->wol_ports_mask = 0;
1414 	/* Disable interrupts */
1415 	bcm_sf2_intr_disable(priv);
1416 	dsa_unregister_switch(priv->dev->ds);
1417 	bcm_sf2_cfp_exit(priv->dev->ds);
1418 	bcm_sf2_mdio_unregister(priv);
1419 	clk_disable_unprepare(priv->clk_mdiv);
1420 	clk_disable_unprepare(priv->clk);
1421 	if (priv->type == BCM7278_DEVICE_ID && !IS_ERR(priv->rcdev))
1422 		reset_control_assert(priv->rcdev);
1423 
1424 	return 0;
1425 }
1426 
bcm_sf2_sw_shutdown(struct platform_device * pdev)1427 static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1428 {
1429 	struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1430 
1431 	/* For a kernel about to be kexec'd we want to keep the GPHY on for a
1432 	 * successful MDIO bus scan to occur. If we did turn off the GPHY
1433 	 * before (e.g: port_disable), this will also power it back on.
1434 	 *
1435 	 * Do not rely on kexec_in_progress, just power the PHY on.
1436 	 */
1437 	if (priv->hw_params.num_gphy == 1)
1438 		bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1439 }
1440 
1441 #ifdef CONFIG_PM_SLEEP
bcm_sf2_suspend(struct device * dev)1442 static int bcm_sf2_suspend(struct device *dev)
1443 {
1444 	struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
1445 
1446 	return dsa_switch_suspend(priv->dev->ds);
1447 }
1448 
bcm_sf2_resume(struct device * dev)1449 static int bcm_sf2_resume(struct device *dev)
1450 {
1451 	struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
1452 
1453 	return dsa_switch_resume(priv->dev->ds);
1454 }
1455 #endif /* CONFIG_PM_SLEEP */
1456 
1457 static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1458 			 bcm_sf2_suspend, bcm_sf2_resume);
1459 
1460 
1461 static struct platform_driver bcm_sf2_driver = {
1462 	.probe	= bcm_sf2_sw_probe,
1463 	.remove	= bcm_sf2_sw_remove,
1464 	.shutdown = bcm_sf2_sw_shutdown,
1465 	.driver = {
1466 		.name = "brcm-sf2",
1467 		.of_match_table = bcm_sf2_of_match,
1468 		.pm = &bcm_sf2_pm_ops,
1469 	},
1470 };
1471 module_platform_driver(bcm_sf2_driver);
1472 
1473 MODULE_AUTHOR("Broadcom Corporation");
1474 MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1475 MODULE_LICENSE("GPL");
1476 MODULE_ALIAS("platform:brcm-sf2");
1477