blob: 9150038b60cb4ed8d84c211682e7a02ac2d1f9ba [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Florian Fainelli246d7f72014-08-27 17:04:56 -07002/*
3 * Broadcom Starfighter 2 DSA switch driver
4 *
5 * Copyright (C) 2014, Broadcom Corporation
Florian Fainelli246d7f72014-08-27 17:04:56 -07006 */
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>
Florian Fainelli246d7f72014-08-27 17:04:56 -070013#include <linux/phy.h>
14#include <linux/phy_fixed.h>
Florian Fainellibc0cb652018-05-10 13:17:33 -070015#include <linux/phylink.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070016#include <linux/mii.h>
Florian Fainellie9ec5c32020-09-01 15:59:12 -070017#include <linux/clk.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070018#include <linux/of.h>
19#include <linux/of_irq.h>
20#include <linux/of_address.h>
Florian Fainelli8b7c94e2015-10-23 12:11:08 -070021#include <linux/of_net.h>
Florian Fainelli461cd1b02016-06-07 16:32:43 -070022#include <linux/of_mdio.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070023#include <net/dsa.h>
Florian Fainelli96e65d72014-09-18 17:31:25 -070024#include <linux/ethtool.h>
Florian Fainelli12f460f2015-02-24 13:15:34 -080025#include <linux/if_bridge.h>
Florian Fainelliaafc66f2015-06-10 18:08:01 -070026#include <linux/brcmphy.h>
Florian Fainelli680060d2015-10-23 11:38:07 -070027#include <linux/etherdevice.h>
Florian Fainellif4589952016-08-26 12:18:33 -070028#include <linux/platform_data/b53.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070029
30#include "bcm_sf2.h"
31#include "bcm_sf2_regs.h"
Florian Fainellif4589952016-08-26 12:18:33 -070032#include "b53/b53_priv.h"
33#include "b53/b53_regs.h"
Florian Fainelli246d7f72014-08-27 17:04:56 -070034
Rafał Miłecki55cfeb32021-03-18 09:01:42 +010035static u16 bcm_sf2_reg_rgmii_cntrl(struct bcm_sf2_priv *priv, int port)
36{
37 switch (priv->type) {
38 case BCM4908_DEVICE_ID:
Rafał Miłecki6859d912021-03-18 09:01:43 +010039 switch (port) {
40 case 7:
41 return REG_RGMII_11_CNTRL;
42 default:
43 break;
44 }
Rafał Miłecki55cfeb32021-03-18 09:01:42 +010045 break;
46 default:
47 switch (port) {
48 case 0:
49 return REG_RGMII_0_CNTRL;
50 case 1:
51 return REG_RGMII_1_CNTRL;
52 case 2:
53 return REG_RGMII_2_CNTRL;
54 default:
55 break;
56 }
57 }
58
59 WARN_ONCE(1, "Unsupported port %d\n", port);
60
61 /* RO fallback reg */
62 return REG_SWITCH_STATUS;
63}
64
Florian Fainelli2ee3adc2020-09-01 15:59:13 -070065/* Return the number of active ports, not counting the IMP (CPU) port */
66static unsigned int bcm_sf2_num_active_ports(struct dsa_switch *ds)
67{
68 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
69 unsigned int port, count = 0;
70
71 for (port = 0; port < ARRAY_SIZE(priv->port_sts); port++) {
72 if (dsa_is_cpu_port(ds, port))
73 continue;
74 if (priv->port_sts[port].enabled)
75 count++;
76 }
77
78 return count;
79}
80
81static void bcm_sf2_recalc_clock(struct dsa_switch *ds)
82{
83 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
84 unsigned long new_rate;
85 unsigned int ports_active;
86 /* Frequenty in Mhz */
Colin Ian Kingd978d6d2020-10-20 17:50:29 +010087 static const unsigned long rate_table[] = {
Florian Fainelli2ee3adc2020-09-01 15:59:13 -070088 59220000,
89 60820000,
90 62500000,
91 62500000,
92 };
93
94 ports_active = bcm_sf2_num_active_ports(ds);
95 if (ports_active == 0 || !priv->clk_mdiv)
96 return;
97
98 /* If we overflow our table, just use the recommended operational
99 * frequency
100 */
101 if (ports_active > ARRAY_SIZE(rate_table))
102 new_rate = 90000000;
103 else
104 new_rate = rate_table[ports_active - 1];
105 clk_set_rate(priv->clk_mdiv, new_rate);
106}
107
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800108static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
109{
110 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellic837fc82017-09-03 20:27:03 -0700111 unsigned int i;
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800112 u32 reg, offset;
113
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800114 /* Enable the port memories */
115 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
116 reg &= ~P_TXQ_PSM_VDD(port);
117 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
118
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800119 /* Enable forwarding */
120 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
121
122 /* Enable IMP port in dumb mode */
123 reg = core_readl(priv, CORE_SWITCH_CTRL);
124 reg |= MII_DUMB_FWDG_EN;
125 core_writel(priv, reg, CORE_SWITCH_CTRL);
126
Florian Fainellic837fc82017-09-03 20:27:03 -0700127 /* Configure Traffic Class to QoS mapping, allow each priority to map
128 * to a different queue number
129 */
130 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
131 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
132 reg |= i << (PRT_TO_QID_SHIFT * i);
133 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
134
Florian Fainellib409a9e2017-09-19 10:46:48 -0700135 b53_brcm_hdr_setup(ds, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700136
Florian Fainelli5fc0f212019-10-31 15:54:05 -0700137 if (port == 8) {
Rafał Miłecki73b7a602021-01-06 22:32:02 +0100138 if (priv->type == BCM4908_DEVICE_ID ||
139 priv->type == BCM7445_DEVICE_ID)
Florian Fainelli5fc0f212019-10-31 15:54:05 -0700140 offset = CORE_STS_OVERRIDE_IMP;
141 else
142 offset = CORE_STS_OVERRIDE_IMP2;
143
144 /* Force link status for IMP port */
145 reg = core_readl(priv, offset);
Florian Fainellide34d702020-02-06 11:23:52 -0800146 reg |= (MII_SW_OR | LINK_STS);
Rafał Miłecki8373a0f2021-03-10 13:51:59 +0100147 if (priv->type == BCM4908_DEVICE_ID)
148 reg |= GMII_SPEED_UP_2G;
149 else
150 reg &= ~GMII_SPEED_UP_2G;
Florian Fainelli5fc0f212019-10-31 15:54:05 -0700151 core_writel(priv, reg, offset);
152
153 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
154 reg = core_readl(priv, CORE_IMP_CTL);
155 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
156 reg &= ~(RX_DIS | TX_DIS);
157 core_writel(priv, reg, CORE_IMP_CTL);
158 } else {
159 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
160 reg &= ~(RX_DIS | TX_DIS);
161 core_writel(priv, reg, CORE_G_PCTL_PORT(port));
162 }
Florian Fainelli2ee3adc2020-09-01 15:59:13 -0700163
164 priv->port_sts[port].enabled = true;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700165}
166
Florian Fainellib0836682015-02-05 11:40:41 -0800167static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
168{
Florian Fainellif4589952016-08-26 12:18:33 -0700169 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -0800170 u32 reg;
171
Florian Fainelli9af197a2015-02-05 11:40:42 -0800172 reg = reg_readl(priv, REG_SPHY_CNTRL);
173 if (enable) {
174 reg |= PHY_RESET;
Florian Fainelli4b52d012017-11-21 17:37:46 -0800175 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | IDDQ_GLOBAL_PWR | CK25_DIS);
Florian Fainelli9af197a2015-02-05 11:40:42 -0800176 reg_writel(priv, reg, REG_SPHY_CNTRL);
177 udelay(21);
178 reg = reg_readl(priv, REG_SPHY_CNTRL);
179 reg &= ~PHY_RESET;
180 } else {
181 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
182 reg_writel(priv, reg, REG_SPHY_CNTRL);
183 mdelay(1);
184 reg |= CK25_DIS;
185 }
186 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800187
Florian Fainelli9af197a2015-02-05 11:40:42 -0800188 /* Use PHY-driven LED signaling */
189 if (!enable) {
190 reg = reg_readl(priv, REG_LED_CNTRL(0));
191 reg |= SPDLNK_SRC_SEL;
192 reg_writel(priv, reg, REG_LED_CNTRL(0));
193 }
Florian Fainellib0836682015-02-05 11:40:41 -0800194}
195
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700196static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
197 int port)
198{
199 unsigned int off;
200
201 switch (port) {
202 case 7:
203 off = P7_IRQ_OFF;
204 break;
205 case 0:
206 /* Port 0 interrupts are located on the first bank */
207 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
208 return;
209 default:
210 off = P_IRQ_OFF(port);
211 break;
212 }
213
214 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
215}
216
217static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
218 int port)
219{
220 unsigned int off;
221
222 switch (port) {
223 case 7:
224 off = P7_IRQ_OFF;
225 break;
226 case 0:
227 /* Port 0 interrupts are located on the first bank */
228 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
229 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
230 return;
231 default:
232 off = P_IRQ_OFF(port);
233 break;
234 }
235
236 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
237 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
238}
239
Florian Fainellib6d045d2014-09-24 17:05:20 -0700240static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
241 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700242{
Florian Fainellif4589952016-08-26 12:18:33 -0700243 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellie1b91472017-01-30 09:48:41 -0800244 unsigned int i;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700245 u32 reg;
246
Vivien Didelot74be4ba2019-08-19 16:00:49 -0400247 if (!dsa_is_user_port(ds, port))
248 return 0;
249
Florian Fainelli2ee3adc2020-09-01 15:59:13 -0700250 priv->port_sts[port].enabled = true;
251
252 bcm_sf2_recalc_clock(ds);
253
Florian Fainelli246d7f72014-08-27 17:04:56 -0700254 /* Clear the memory power down */
255 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
256 reg &= ~P_TXQ_PSM_VDD(port);
257 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
258
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800259 /* Enable Broadcom tags for that port if requested */
Florian Fainellif9b38272021-02-22 14:30:10 -0800260 if (priv->brcm_tag_mask & BIT(port))
Florian Fainellib409a9e2017-09-19 10:46:48 -0700261 b53_brcm_hdr_setup(ds, port);
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800262
Florian Fainellie1b91472017-01-30 09:48:41 -0800263 /* Configure Traffic Class to QoS mapping, allow each priority to map
264 * to a different queue number
265 */
266 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
Florian Fainelli181183772017-09-03 20:27:02 -0700267 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
Florian Fainellie1b91472017-01-30 09:48:41 -0800268 reg |= i << (PRT_TO_QID_SHIFT * i);
269 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
270
Florian Fainelli9af197a2015-02-05 11:40:42 -0800271 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700272 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800273 bcm_sf2_gphy_enable_set(ds, true);
274 if (phy) {
275 /* if phy_stop() has been called before, phy
276 * will be in halted state, and phy_start()
277 * will call resume.
278 *
279 * the resume path does not configure back
280 * autoneg settings, and since we hard reset
281 * the phy manually here, we need to reset the
282 * state machine also.
283 */
284 phy->state = PHY_READY;
285 phy_init_hw(phy);
286 }
287 }
288
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700289 /* Enable MoCA port interrupts to get notified */
290 if (port == priv->moca_port)
291 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700292
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700293 /* Set per-queue pause threshold to 32 */
294 core_writel(priv, 32, CORE_TXQ_THD_PAUSE_QN_PORT(port));
295
296 /* Set ACB threshold to 24 */
297 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++) {
298 reg = acb_readl(priv, ACB_QUEUE_CFG(port *
299 SF2_NUM_EGRESS_QUEUES + i));
300 reg &= ~XOFF_THRESHOLD_MASK;
301 reg |= 24;
302 acb_writel(priv, reg, ACB_QUEUE_CFG(port *
303 SF2_NUM_EGRESS_QUEUES + i));
304 }
305
Florian Fainellif86ad772017-09-19 10:46:54 -0700306 return b53_enable_port(ds, port, phy);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700307}
308
Andrew Lunn75104db2019-02-24 20:44:43 +0100309static void bcm_sf2_port_disable(struct dsa_switch *ds, int port)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700310{
Florian Fainellif4589952016-08-26 12:18:33 -0700311 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Colin Ian King5c17a072018-07-04 07:54:36 +0100312 u32 reg;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700313
Florian Fainellic0e68202018-08-03 11:08:42 -0700314 /* Disable learning while in WoL mode */
315 if (priv->wol_ports_mask & (1 << port)) {
316 reg = core_readl(priv, CORE_DIS_LEARN);
317 reg |= BIT(port);
318 core_writel(priv, reg, CORE_DIS_LEARN);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700319 return;
Florian Fainellic0e68202018-08-03 11:08:42 -0700320 }
Florian Fainelli96e65d72014-09-18 17:31:25 -0700321
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700322 if (port == priv->moca_port)
323 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700324
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700325 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800326 bcm_sf2_gphy_enable_set(ds, false);
327
Andrew Lunn75104db2019-02-24 20:44:43 +0100328 b53_disable_port(ds, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700329
330 /* Power down the port memory */
331 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
332 reg |= P_TXQ_PSM_VDD(port);
333 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
Florian Fainelli2ee3adc2020-09-01 15:59:13 -0700334
335 priv->port_sts[port].enabled = false;
336
337 bcm_sf2_recalc_clock(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700338}
339
Florian Fainelli450b05c2014-09-24 17:05:22 -0700340
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700341static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
342 int regnum, u16 val)
343{
344 int ret = 0;
345 u32 reg;
346
347 reg = reg_readl(priv, REG_SWITCH_CNTRL);
348 reg |= MDIO_MASTER_SEL;
349 reg_writel(priv, reg, REG_SWITCH_CNTRL);
350
351 /* Page << 8 | offset */
352 reg = 0x70;
353 reg <<= 2;
354 core_writel(priv, addr, reg);
355
356 /* Page << 8 | offset */
357 reg = 0x80 << 8 | regnum << 1;
358 reg <<= 2;
359
360 if (op)
361 ret = core_readl(priv, reg);
362 else
363 core_writel(priv, val, reg);
364
365 reg = reg_readl(priv, REG_SWITCH_CNTRL);
366 reg &= ~MDIO_MASTER_SEL;
367 reg_writel(priv, reg, REG_SWITCH_CNTRL);
368
369 return ret & 0xffff;
370}
371
372static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
373{
374 struct bcm_sf2_priv *priv = bus->priv;
375
376 /* Intercept reads from Broadcom pseudo-PHY address, else, send
377 * them to our master MDIO bus controller
378 */
379 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
380 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
381 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800382 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700383}
384
385static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
386 u16 val)
387{
388 struct bcm_sf2_priv *priv = bus->priv;
389
390 /* Intercept writes to the Broadcom pseudo-PHY address, else,
391 * send them to our master MDIO bus controller
392 */
393 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
Kangjie Lue49505f2018-12-25 22:08:18 -0600394 return bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700395 else
Kangjie Lue49505f2018-12-25 22:08:18 -0600396 return mdiobus_write_nested(priv->master_mii_bus, addr,
397 regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700398}
399
Florian Fainelli246d7f72014-08-27 17:04:56 -0700400static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
401{
Florian Fainellibc0cb652018-05-10 13:17:33 -0700402 struct dsa_switch *ds = dev_id;
403 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700404
405 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
406 ~priv->irq0_mask;
407 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
408
409 return IRQ_HANDLED;
410}
411
412static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
413{
Florian Fainellibc0cb652018-05-10 13:17:33 -0700414 struct dsa_switch *ds = dev_id;
415 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700416
417 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
418 ~priv->irq1_mask;
419 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
420
Florian Fainellibc0cb652018-05-10 13:17:33 -0700421 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF)) {
422 priv->port_sts[7].link = true;
423 dsa_port_phylink_mac_change(ds, 7, true);
424 }
425 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF)) {
426 priv->port_sts[7].link = false;
427 dsa_port_phylink_mac_change(ds, 7, false);
428 }
Florian Fainelli246d7f72014-08-27 17:04:56 -0700429
430 return IRQ_HANDLED;
431}
432
Florian Fainelli33f84612014-11-25 18:08:49 -0800433static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
434{
435 unsigned int timeout = 1000;
436 u32 reg;
Florian Fainellieee87e42019-11-04 13:51:39 -0800437 int ret;
438
439 /* The watchdog reset does not work on 7278, we need to hit the
440 * "external" reset line through the reset controller.
441 */
Philipp Zabelbf9279c2021-03-05 10:14:48 +0100442 if (priv->type == BCM7278_DEVICE_ID) {
Florian Fainellieee87e42019-11-04 13:51:39 -0800443 ret = reset_control_assert(priv->rcdev);
444 if (ret)
445 return ret;
446
447 return reset_control_deassert(priv->rcdev);
448 }
Florian Fainelli33f84612014-11-25 18:08:49 -0800449
450 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
451 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
452 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
453
454 do {
455 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
456 if (!(reg & SOFTWARE_RESET))
457 break;
458
459 usleep_range(1000, 2000);
460 } while (timeout-- > 0);
461
462 if (timeout == 0)
463 return -ETIMEDOUT;
464
465 return 0;
466}
467
Rafał Miłeckia9349f02021-03-12 11:41:08 +0100468static void bcm_sf2_crossbar_setup(struct bcm_sf2_priv *priv)
469{
470 struct device *dev = priv->dev->ds->dev;
471 int shift;
472 u32 mask;
473 u32 reg;
474 int i;
475
476 mask = BIT(priv->num_crossbar_int_ports) - 1;
477
478 reg = reg_readl(priv, REG_CROSSBAR);
479 switch (priv->type) {
480 case BCM4908_DEVICE_ID:
481 shift = CROSSBAR_BCM4908_INT_P7 * priv->num_crossbar_int_ports;
482 reg &= ~(mask << shift);
483 if (0) /* FIXME */
484 reg |= CROSSBAR_BCM4908_EXT_SERDES << shift;
485 else if (priv->int_phy_mask & BIT(7))
486 reg |= CROSSBAR_BCM4908_EXT_GPHY4 << shift;
487 else if (phy_interface_mode_is_rgmii(priv->port_sts[7].mode))
488 reg |= CROSSBAR_BCM4908_EXT_RGMII << shift;
489 else if (WARN(1, "Invalid port mode\n"))
490 return;
491 break;
492 default:
493 return;
494 }
495 reg_writel(priv, reg, REG_CROSSBAR);
496
497 reg = reg_readl(priv, REG_CROSSBAR);
498 for (i = 0; i < priv->num_crossbar_int_ports; i++) {
499 shift = i * priv->num_crossbar_int_ports;
500
501 dev_dbg(dev, "crossbar int port #%d - ext port #%d\n", i,
502 (reg >> shift) & mask);
503 }
504}
505
Florian Fainelli691c9a82015-01-20 16:42:00 -0800506static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
507{
Florian Fainellif01d5982016-08-25 15:23:41 -0700508 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800509 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700510 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800511 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800512}
513
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700514static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
515 struct device_node *dn)
516{
Rafał Miłecki01488a02021-03-12 11:41:07 +0100517 struct device *dev = priv->dev->ds->dev;
518 struct bcm_sf2_port_status *port_st;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700519 struct device_node *port;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700520 unsigned int port_num;
Florian Fainelli8c280442020-09-22 13:03:55 -0700521 struct property *prop;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100522 int err;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700523
524 priv->moca_port = -1;
525
526 for_each_available_child_of_node(dn, port) {
527 if (of_property_read_u32(port, "reg", &port_num))
528 continue;
529
Rafał Miłecki01488a02021-03-12 11:41:07 +0100530 if (port_num >= DSA_MAX_PORTS) {
531 dev_err(dev, "Invalid port number %d\n", port_num);
532 continue;
533 }
534
535 port_st = &priv->port_sts[port_num];
536
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700537 /* Internal PHYs get assigned a specific 'phy-mode' property
538 * value: "internal" to help flag them before MDIO probing
539 * has completed, since they might be turned off at that
540 * time
541 */
Rafał Miłecki01488a02021-03-12 11:41:07 +0100542 err = of_get_phy_mode(port, &port_st->mode);
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100543 if (err)
Florian Fainellibedd00c2017-06-23 10:33:16 -0700544 continue;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700545
Rafał Miłecki01488a02021-03-12 11:41:07 +0100546 if (port_st->mode == PHY_INTERFACE_MODE_INTERNAL)
Florian Fainellibedd00c2017-06-23 10:33:16 -0700547 priv->int_phy_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700548
Rafał Miłecki01488a02021-03-12 11:41:07 +0100549 if (port_st->mode == PHY_INTERFACE_MODE_MOCA)
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700550 priv->moca_port = port_num;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800551
552 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
553 priv->brcm_tag_mask |= 1 << port_num;
Florian Fainelli8c280442020-09-22 13:03:55 -0700554
555 /* Ensure that port 5 is not picked up as a DSA CPU port
556 * flavour but a regular port instead. We should be using
557 * devlink to be able to set the port flavour.
558 */
559 if (port_num == 5 && priv->type == BCM7278_DEVICE_ID) {
560 prop = of_find_property(port, "ethernet", NULL);
561 if (prop)
562 of_remove_property(port, prop);
563 }
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700564 }
565}
566
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700567static int bcm_sf2_mdio_register(struct dsa_switch *ds)
568{
Florian Fainellif4589952016-08-26 12:18:33 -0700569 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli771089c2020-09-04 14:37:30 -0700570 struct device_node *dn, *child;
571 struct phy_device *phydev;
572 struct property *prop;
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700573 static int index;
Florian Fainelli771089c2020-09-04 14:37:30 -0700574 int err, reg;
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700575
576 /* Find our integrated MDIO bus node */
577 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
578 priv->master_mii_bus = of_mdio_find_bus(dn);
Pan Biancf3c4662021-01-21 04:33:43 -0800579 if (!priv->master_mii_bus) {
580 of_node_put(dn);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700581 return -EPROBE_DEFER;
Pan Biancf3c4662021-01-21 04:33:43 -0800582 }
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700583
584 get_device(&priv->master_mii_bus->dev);
585 priv->master_mii_dn = dn;
586
587 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
Pan Biancf3c4662021-01-21 04:33:43 -0800588 if (!priv->slave_mii_bus) {
589 of_node_put(dn);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700590 return -ENOMEM;
Pan Biancf3c4662021-01-21 04:33:43 -0800591 }
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700592
593 priv->slave_mii_bus->priv = priv;
594 priv->slave_mii_bus->name = "sf2 slave mii";
595 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
596 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
597 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
598 index++);
599 priv->slave_mii_bus->dev.of_node = dn;
600
601 /* Include the pseudo-PHY address to divert reads towards our
602 * workaround. This is only required for 7445D0, since 7445E0
603 * disconnects the internal switch pseudo-PHY such that we can use the
604 * regular SWITCH_MDIO master controller instead.
605 *
606 * Here we flag the pseudo PHY as needing special treatment and would
607 * otherwise make all other PHY read/writes go to the master MDIO bus
608 * controller that comes with this switch backed by the "mdio-unimac"
609 * driver.
610 */
611 if (of_machine_is_compatible("brcm,bcm7445d0"))
Florian Fainelli0fa45ee2020-09-22 13:03:56 -0700612 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR) | (1 << 0);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700613 else
614 priv->indir_phy_mask = 0;
615
616 ds->phys_mii_mask = priv->indir_phy_mask;
617 ds->slave_mii_bus = priv->slave_mii_bus;
618 priv->slave_mii_bus->parent = ds->dev->parent;
619 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
620
Florian Fainelli771089c2020-09-04 14:37:30 -0700621 /* We need to make sure that of_phy_connect() will not work by
622 * removing the 'phandle' and 'linux,phandle' properties and
623 * unregister the existing PHY device that was already registered.
624 */
625 for_each_available_child_of_node(dn, child) {
626 if (of_property_read_u32(child, "reg", &reg) ||
627 reg >= PHY_MAX_ADDR)
628 continue;
629
630 if (!(priv->indir_phy_mask & BIT(reg)))
631 continue;
632
633 prop = of_find_property(child, "phandle", NULL);
634 if (prop)
635 of_remove_property(child, prop);
636
637 prop = of_find_property(child, "linux,phandle", NULL);
638 if (prop)
639 of_remove_property(child, prop);
640
641 phydev = of_phy_find_device(child);
642 if (phydev)
643 phy_device_remove(phydev);
644 }
645
Florian Fainelli536fab52020-04-04 14:35:17 -0700646 err = mdiobus_register(priv->slave_mii_bus);
Florian Fainelli00e798c2018-05-15 16:56:19 -0700647 if (err && dn)
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700648 of_node_put(dn);
649
650 return err;
651}
652
653static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
654{
655 mdiobus_unregister(priv->slave_mii_bus);
zhong jiang1ddc5d32018-09-16 21:22:31 +0800656 of_node_put(priv->master_mii_dn);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700657}
658
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700659static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
660{
Florian Fainellif4589952016-08-26 12:18:33 -0700661 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700662
663 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
664 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
665 * the REG_PHY_REVISION register layout is.
666 */
Florian Fainelli47142ed2021-03-10 14:17:58 -0800667 if (priv->int_phy_mask & BIT(port))
668 return priv->hw_params.gphy_rev;
669 else
670 return 0;
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700671}
672
Florian Fainellibc0cb652018-05-10 13:17:33 -0700673static void bcm_sf2_sw_validate(struct dsa_switch *ds, int port,
674 unsigned long *supported,
675 struct phylink_link_state *state)
676{
Florian Fainelli738a2e42019-08-21 17:07:46 -0700677 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700678 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
679
680 if (!phy_interface_mode_is_rgmii(state->interface) &&
681 state->interface != PHY_INTERFACE_MODE_MII &&
682 state->interface != PHY_INTERFACE_MODE_REVMII &&
683 state->interface != PHY_INTERFACE_MODE_GMII &&
684 state->interface != PHY_INTERFACE_MODE_INTERNAL &&
685 state->interface != PHY_INTERFACE_MODE_MOCA) {
686 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
Florian Fainelli738a2e42019-08-21 17:07:46 -0700687 if (port != core_readl(priv, CORE_IMP0_PRT_ID))
688 dev_err(ds->dev,
689 "Unsupported interface: %d for port %d\n",
690 state->interface, port);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700691 return;
692 }
693
694 /* Allow all the expected bits */
695 phylink_set(mask, Autoneg);
696 phylink_set_port_modes(mask);
697 phylink_set(mask, Pause);
698 phylink_set(mask, Asym_Pause);
699
700 /* With the exclusion of MII and Reverse MII, we support Gigabit,
701 * including Half duplex
702 */
703 if (state->interface != PHY_INTERFACE_MODE_MII &&
704 state->interface != PHY_INTERFACE_MODE_REVMII) {
705 phylink_set(mask, 1000baseT_Full);
706 phylink_set(mask, 1000baseT_Half);
707 }
708
709 phylink_set(mask, 10baseT_Half);
710 phylink_set(mask, 10baseT_Full);
711 phylink_set(mask, 100baseT_Half);
712 phylink_set(mask, 100baseT_Full);
713
714 bitmap_and(supported, supported, mask,
715 __ETHTOOL_LINK_MODE_MASK_NBITS);
716 bitmap_and(state->advertising, state->advertising, mask,
717 __ETHTOOL_LINK_MODE_MASK_NBITS);
718}
719
720static void bcm_sf2_sw_mac_config(struct dsa_switch *ds, int port,
721 unsigned int mode,
722 const struct phylink_link_state *state)
723{
724 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
725 u32 id_mode_dis = 0, port_mode;
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100726 u32 reg_rgmii_ctrl;
Russell King50cc20202020-06-30 11:28:13 +0100727 u32 reg;
Florian Fainellibc0cb652018-05-10 13:17:33 -0700728
Florian Fainelli738a2e42019-08-21 17:07:46 -0700729 if (port == core_readl(priv, CORE_IMP0_PRT_ID))
730 return;
731
Florian Fainellibc0cb652018-05-10 13:17:33 -0700732 switch (state->interface) {
733 case PHY_INTERFACE_MODE_RGMII:
734 id_mode_dis = 1;
Gustavo A. R. Silvadf561f662020-08-23 17:36:59 -0500735 fallthrough;
Florian Fainellibc0cb652018-05-10 13:17:33 -0700736 case PHY_INTERFACE_MODE_RGMII_TXID:
737 port_mode = EXT_GPHY;
738 break;
739 case PHY_INTERFACE_MODE_MII:
740 port_mode = EXT_EPHY;
741 break;
742 case PHY_INTERFACE_MODE_REVMII:
743 port_mode = EXT_REVMII;
744 break;
745 default:
Russell King50cc20202020-06-30 11:28:13 +0100746 /* Nothing required for all other PHYs: internal and MoCA */
747 return;
Florian Fainellibc0cb652018-05-10 13:17:33 -0700748 }
749
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100750 reg_rgmii_ctrl = bcm_sf2_reg_rgmii_cntrl(priv, port);
751
Florian Fainellibc0cb652018-05-10 13:17:33 -0700752 /* Clear id_mode_dis bit, and the existing port mode, let
753 * RGMII_MODE_EN bet set by mac_link_{up,down}
754 */
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100755 reg = reg_readl(priv, reg_rgmii_ctrl);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700756 reg &= ~ID_MODE_DIS;
757 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700758
759 reg |= port_mode;
760 if (id_mode_dis)
761 reg |= ID_MODE_DIS;
762
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100763 reg_writel(priv, reg, reg_rgmii_ctrl);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700764}
765
766static void bcm_sf2_sw_mac_link_set(struct dsa_switch *ds, int port,
767 phy_interface_t interface, bool link)
768{
769 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100770 u32 reg_rgmii_ctrl;
Florian Fainellibc0cb652018-05-10 13:17:33 -0700771 u32 reg;
772
773 if (!phy_interface_mode_is_rgmii(interface) &&
774 interface != PHY_INTERFACE_MODE_MII &&
775 interface != PHY_INTERFACE_MODE_REVMII)
776 return;
777
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100778 reg_rgmii_ctrl = bcm_sf2_reg_rgmii_cntrl(priv, port);
779
Florian Fainellibc0cb652018-05-10 13:17:33 -0700780 /* If the link is down, just disable the interface to conserve power */
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100781 reg = reg_readl(priv, reg_rgmii_ctrl);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700782 if (link)
783 reg |= RGMII_MODE_EN;
784 else
785 reg &= ~RGMII_MODE_EN;
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100786 reg_writel(priv, reg, reg_rgmii_ctrl);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700787}
788
789static void bcm_sf2_sw_mac_link_down(struct dsa_switch *ds, int port,
790 unsigned int mode,
791 phy_interface_t interface)
792{
Russell King2d1f90f2020-06-30 11:28:08 +0100793 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
794 u32 reg, offset;
795
796 if (port != core_readl(priv, CORE_IMP0_PRT_ID)) {
Rafał Miłecki73b7a602021-01-06 22:32:02 +0100797 if (priv->type == BCM4908_DEVICE_ID ||
798 priv->type == BCM7445_DEVICE_ID)
Russell King2d1f90f2020-06-30 11:28:08 +0100799 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
800 else
801 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
802
803 reg = core_readl(priv, offset);
804 reg &= ~LINK_STS;
805 core_writel(priv, reg, offset);
806 }
807
Florian Fainellibc0cb652018-05-10 13:17:33 -0700808 bcm_sf2_sw_mac_link_set(ds, port, interface, false);
809}
810
811static void bcm_sf2_sw_mac_link_up(struct dsa_switch *ds, int port,
812 unsigned int mode,
813 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +0000814 struct phy_device *phydev,
815 int speed, int duplex,
816 bool tx_pause, bool rx_pause)
Florian Fainellibc0cb652018-05-10 13:17:33 -0700817{
818 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
819 struct ethtool_eee *p = &priv->dev->ports[port].eee;
820
821 bcm_sf2_sw_mac_link_set(ds, port, interface, true);
822
Russell King2d1f90f2020-06-30 11:28:08 +0100823 if (port != core_readl(priv, CORE_IMP0_PRT_ID)) {
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100824 u32 reg_rgmii_ctrl;
825 u32 reg, offset;
826
827 reg_rgmii_ctrl = bcm_sf2_reg_rgmii_cntrl(priv, port);
828
Rafał Miłecki73b7a602021-01-06 22:32:02 +0100829 if (priv->type == BCM4908_DEVICE_ID ||
830 priv->type == BCM7445_DEVICE_ID)
Russell King2d1f90f2020-06-30 11:28:08 +0100831 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
832 else
833 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
834
Russell King981015a2020-06-30 11:28:18 +0100835 if (interface == PHY_INTERFACE_MODE_RGMII ||
836 interface == PHY_INTERFACE_MODE_RGMII_TXID ||
837 interface == PHY_INTERFACE_MODE_MII ||
838 interface == PHY_INTERFACE_MODE_REVMII) {
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100839 reg = reg_readl(priv, reg_rgmii_ctrl);
Russell King981015a2020-06-30 11:28:18 +0100840 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
841
842 if (tx_pause)
843 reg |= TX_PAUSE_EN;
844 if (rx_pause)
845 reg |= RX_PAUSE_EN;
846
Rafał Miłecki55cfeb32021-03-18 09:01:42 +0100847 reg_writel(priv, reg, reg_rgmii_ctrl);
Russell King981015a2020-06-30 11:28:18 +0100848 }
849
Russell King50cc20202020-06-30 11:28:13 +0100850 reg = SW_OVERRIDE | LINK_STS;
851 switch (speed) {
852 case SPEED_1000:
853 reg |= SPDSTS_1000 << SPEED_SHIFT;
854 break;
855 case SPEED_100:
856 reg |= SPDSTS_100 << SPEED_SHIFT;
857 break;
858 }
859
860 if (duplex == DUPLEX_FULL)
861 reg |= DUPLX_MODE;
862
Russell King2d1f90f2020-06-30 11:28:08 +0100863 core_writel(priv, reg, offset);
864 }
865
Florian Fainellibc0cb652018-05-10 13:17:33 -0700866 if (mode == MLO_AN_PHY && phydev)
867 p->eee_enabled = b53_eee_init(ds, port, phydev);
868}
869
870static void bcm_sf2_sw_fixed_state(struct dsa_switch *ds, int port,
871 struct phylink_link_state *status)
872{
873 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
874
875 status->link = false;
876
877 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
878 * which means that we need to force the link at the port override
879 * level to get the data to flow. We do use what the interrupt handler
880 * did determine before.
881 *
882 * For the other ports, we just force the link status, since this is
883 * a fixed PHY device.
884 */
885 if (port == priv->moca_port) {
886 status->link = priv->port_sts[port].link;
887 /* For MoCA interfaces, also force a link down notification
888 * since some version of the user-space daemon (mocad) use
889 * cmd->autoneg to force the link, which messes up the PHY
890 * state machine and make it go in PHY_FORCING state instead.
891 */
892 if (!status->link)
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400893 netif_carrier_off(dsa_to_port(ds, port)->slave);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700894 status->duplex = DUPLEX_FULL;
895 } else {
896 status->link = true;
897 }
898}
899
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700900static void bcm_sf2_enable_acb(struct dsa_switch *ds)
901{
902 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
903 u32 reg;
904
905 /* Enable ACB globally */
906 reg = acb_readl(priv, ACB_CONTROL);
907 reg |= (ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
908 acb_writel(priv, reg, ACB_CONTROL);
909 reg &= ~(ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
910 reg |= ACB_EN | ACB_ALGORITHM;
911 acb_writel(priv, reg, ACB_CONTROL);
912}
913
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700914static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
915{
Florian Fainellif4589952016-08-26 12:18:33 -0700916 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700917 unsigned int port;
918
Florian Fainelli691c9a82015-01-20 16:42:00 -0800919 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700920
921 /* Disable all ports physically present including the IMP
922 * port, the other ones have already been disabled during
923 * bcm_sf2_sw_setup
924 */
Dan Carpenter8d6ea932019-02-13 11:23:04 +0300925 for (port = 0; port < ds->num_ports; port++) {
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400926 if (dsa_is_user_port(ds, port) || dsa_is_cpu_port(ds, port))
Andrew Lunn75104db2019-02-24 20:44:43 +0100927 bcm_sf2_port_disable(ds, port);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700928 }
929
Florian Fainellie9ec5c32020-09-01 15:59:12 -0700930 if (!priv->wol_ports_mask)
931 clk_disable_unprepare(priv->clk);
932
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700933 return 0;
934}
935
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700936static int bcm_sf2_sw_resume(struct dsa_switch *ds)
937{
Florian Fainellif4589952016-08-26 12:18:33 -0700938 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700939 int ret;
940
Florian Fainellie9ec5c32020-09-01 15:59:12 -0700941 if (!priv->wol_ports_mask)
942 clk_prepare_enable(priv->clk);
943
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700944 ret = bcm_sf2_sw_rst(priv);
945 if (ret) {
946 pr_err("%s: failed to software reset switch\n", __func__);
947 return ret;
948 }
949
Rafał Miłeckia9349f02021-03-12 11:41:08 +0100950 bcm_sf2_crossbar_setup(priv);
951
Florian Fainelli1c0130f2018-11-06 12:58:39 -0800952 ret = bcm_sf2_cfp_resume(ds);
953 if (ret)
954 return ret;
955
Florian Fainellib0836682015-02-05 11:40:41 -0800956 if (priv->hw_params.num_gphy == 1)
957 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700958
Florian Fainelliabd01ba2018-10-09 16:48:58 -0700959 ds->ops->setup(ds);
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700960
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700961 return 0;
962}
963
Florian Fainelli96e65d72014-09-18 17:31:25 -0700964static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
965 struct ethtool_wolinfo *wol)
966{
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400967 struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
Florian Fainellif4589952016-08-26 12:18:33 -0700968 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellic3152ec2019-02-15 12:16:52 -0800969 struct ethtool_wolinfo pwol = { };
Florian Fainelli96e65d72014-09-18 17:31:25 -0700970
971 /* Get the parent device WoL settings */
Florian Fainellic3152ec2019-02-15 12:16:52 -0800972 if (p->ethtool_ops->get_wol)
973 p->ethtool_ops->get_wol(p, &pwol);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700974
975 /* Advertise the parent device supported settings */
976 wol->supported = pwol.supported;
977 memset(&wol->sopass, 0, sizeof(wol->sopass));
978
979 if (pwol.wolopts & WAKE_MAGICSECURE)
980 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
981
982 if (priv->wol_ports_mask & (1 << port))
983 wol->wolopts = pwol.wolopts;
984 else
985 wol->wolopts = 0;
986}
987
988static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
989 struct ethtool_wolinfo *wol)
990{
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400991 struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
Florian Fainellif4589952016-08-26 12:18:33 -0700992 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400993 s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
Florian Fainellic3152ec2019-02-15 12:16:52 -0800994 struct ethtool_wolinfo pwol = { };
Florian Fainelli96e65d72014-09-18 17:31:25 -0700995
Florian Fainellic3152ec2019-02-15 12:16:52 -0800996 if (p->ethtool_ops->get_wol)
997 p->ethtool_ops->get_wol(p, &pwol);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700998 if (wol->wolopts & ~pwol.supported)
999 return -EINVAL;
1000
1001 if (wol->wolopts)
1002 priv->wol_ports_mask |= (1 << port);
1003 else
1004 priv->wol_ports_mask &= ~(1 << port);
1005
1006 /* If we have at least one port enabled, make sure the CPU port
1007 * is also enabled. If the CPU port is the last one enabled, we disable
1008 * it since this configuration does not make sense.
1009 */
1010 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
1011 priv->wol_ports_mask |= (1 << cpu_port);
1012 else
1013 priv->wol_ports_mask &= ~(1 << cpu_port);
1014
1015 return p->ethtool_ops->set_wol(p, wol);
1016}
1017
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001018static int bcm_sf2_sw_setup(struct dsa_switch *ds)
1019{
Florian Fainellif4589952016-08-26 12:18:33 -07001020 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001021 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -07001022
Florian Fainelli21a27742017-09-28 11:19:06 -07001023 /* Enable all valid ports and disable those unused */
Florian Fainellid9338022016-08-18 15:30:14 -07001024 for (port = 0; port < priv->hw_params.num_ports; port++) {
Florian Fainelli21a27742017-09-28 11:19:06 -07001025 /* IMP port receives special treatment */
Vivien Didelot4a5b85f2017-10-26 11:22:55 -04001026 if (dsa_is_user_port(ds, port))
Florian Fainelli21a27742017-09-28 11:19:06 -07001027 bcm_sf2_port_setup(ds, port, NULL);
1028 else if (dsa_is_cpu_port(ds, port))
Florian Fainellid9338022016-08-18 15:30:14 -07001029 bcm_sf2_imp_setup(ds, port);
Florian Fainelli21a27742017-09-28 11:19:06 -07001030 else
Andrew Lunn75104db2019-02-24 20:44:43 +01001031 bcm_sf2_port_disable(ds, port);
Florian Fainellid9338022016-08-18 15:30:14 -07001032 }
1033
Florian Fainelli5c1a6ea2017-10-27 15:56:01 -07001034 b53_configure_vlan(ds);
Florian Fainelli32e47ff2017-10-11 10:57:51 -07001035 bcm_sf2_enable_acb(ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001036
Florian Fainelli4f6a5ca2020-09-09 10:49:31 -07001037 return b53_setup_devlink_resources(ds);
1038}
1039
1040static void bcm_sf2_sw_teardown(struct dsa_switch *ds)
1041{
1042 dsa_devlink_resources_unregister(ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001043}
1044
Florian Fainellif4589952016-08-26 12:18:33 -07001045/* The SWITCH_CORE register space is managed by b53 but operates on a page +
1046 * register basis so we need to translate that into an address that the
1047 * bus-glue understands.
1048 */
1049#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
1050
1051static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
1052 u8 *val)
1053{
1054 struct bcm_sf2_priv *priv = dev->priv;
1055
1056 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
1057
1058 return 0;
1059}
1060
1061static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
1062 u16 *val)
1063{
1064 struct bcm_sf2_priv *priv = dev->priv;
1065
1066 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
1067
1068 return 0;
1069}
1070
1071static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
1072 u32 *val)
1073{
1074 struct bcm_sf2_priv *priv = dev->priv;
1075
1076 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
1077
1078 return 0;
1079}
1080
1081static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
1082 u64 *val)
1083{
1084 struct bcm_sf2_priv *priv = dev->priv;
1085
1086 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
1087
1088 return 0;
1089}
1090
1091static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
1092 u8 value)
1093{
1094 struct bcm_sf2_priv *priv = dev->priv;
1095
1096 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1097
1098 return 0;
1099}
1100
1101static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
1102 u16 value)
1103{
1104 struct bcm_sf2_priv *priv = dev->priv;
1105
1106 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1107
1108 return 0;
1109}
1110
1111static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
1112 u32 value)
1113{
1114 struct bcm_sf2_priv *priv = dev->priv;
1115
1116 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1117
1118 return 0;
1119}
1120
1121static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
1122 u64 value)
1123{
1124 struct bcm_sf2_priv *priv = dev->priv;
1125
1126 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1127
1128 return 0;
1129}
1130
Bhumika Goyal7e3108f2017-08-29 22:17:52 +05301131static const struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -07001132 .read8 = bcm_sf2_core_read8,
1133 .read16 = bcm_sf2_core_read16,
1134 .read32 = bcm_sf2_core_read32,
1135 .read48 = bcm_sf2_core_read64,
1136 .read64 = bcm_sf2_core_read64,
1137 .write8 = bcm_sf2_core_write8,
1138 .write16 = bcm_sf2_core_write16,
1139 .write32 = bcm_sf2_core_write32,
1140 .write48 = bcm_sf2_core_write64,
1141 .write64 = bcm_sf2_core_write64,
1142};
1143
Florian Fainellibadd62c2019-02-06 12:45:58 -08001144static void bcm_sf2_sw_get_strings(struct dsa_switch *ds, int port,
1145 u32 stringset, uint8_t *data)
1146{
Florian Fainellif4ae9c02019-02-06 12:45:59 -08001147 int cnt = b53_get_sset_count(ds, port, stringset);
1148
Florian Fainellibadd62c2019-02-06 12:45:58 -08001149 b53_get_strings(ds, port, stringset, data);
Florian Fainellif4ae9c02019-02-06 12:45:59 -08001150 bcm_sf2_cfp_get_strings(ds, port, stringset,
1151 data + cnt * ETH_GSTRING_LEN);
Florian Fainellibadd62c2019-02-06 12:45:58 -08001152}
1153
1154static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds, int port,
1155 uint64_t *data)
1156{
Florian Fainellif4ae9c02019-02-06 12:45:59 -08001157 int cnt = b53_get_sset_count(ds, port, ETH_SS_STATS);
1158
Florian Fainellibadd62c2019-02-06 12:45:58 -08001159 b53_get_ethtool_stats(ds, port, data);
Florian Fainellif4ae9c02019-02-06 12:45:59 -08001160 bcm_sf2_cfp_get_ethtool_stats(ds, port, data + cnt);
Florian Fainellibadd62c2019-02-06 12:45:58 -08001161}
1162
1163static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds, int port,
1164 int sset)
1165{
Florian Fainellif4ae9c02019-02-06 12:45:59 -08001166 int cnt = b53_get_sset_count(ds, port, sset);
1167
1168 if (cnt < 0)
1169 return cnt;
1170
1171 cnt += bcm_sf2_cfp_get_sset_count(ds, port, sset);
1172
1173 return cnt;
Florian Fainellibadd62c2019-02-06 12:45:58 -08001174}
1175
Florian Fainellia82f67a2017-01-08 14:52:08 -08001176static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli9f668162017-11-30 09:55:35 -08001177 .get_tag_protocol = b53_get_tag_protocol,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001178 .setup = bcm_sf2_sw_setup,
Florian Fainelli4f6a5ca2020-09-09 10:49:31 -07001179 .teardown = bcm_sf2_sw_teardown,
Florian Fainellibadd62c2019-02-06 12:45:58 -08001180 .get_strings = bcm_sf2_sw_get_strings,
1181 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
1182 .get_sset_count = bcm_sf2_sw_get_sset_count,
Florian Fainellic7d28c92018-04-25 12:12:53 -07001183 .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001184 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
Florian Fainellibc0cb652018-05-10 13:17:33 -07001185 .phylink_validate = bcm_sf2_sw_validate,
1186 .phylink_mac_config = bcm_sf2_sw_mac_config,
1187 .phylink_mac_link_down = bcm_sf2_sw_mac_link_down,
1188 .phylink_mac_link_up = bcm_sf2_sw_mac_link_up,
1189 .phylink_fixed_state = bcm_sf2_sw_fixed_state,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001190 .suspend = bcm_sf2_sw_suspend,
1191 .resume = bcm_sf2_sw_resume,
1192 .get_wol = bcm_sf2_sw_get_wol,
1193 .set_wol = bcm_sf2_sw_set_wol,
1194 .port_enable = bcm_sf2_port_setup,
1195 .port_disable = bcm_sf2_port_disable,
Florian Fainelli22256b02017-09-19 10:46:50 -07001196 .get_mac_eee = b53_get_mac_eee,
1197 .set_mac_eee = b53_set_mac_eee,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001198 .port_bridge_join = b53_br_join,
1199 .port_bridge_leave = b53_br_leave,
Florian Fainellie6dd86e2021-02-22 14:30:09 -08001200 .port_pre_bridge_flags = b53_br_flags_pre,
1201 .port_bridge_flags = b53_br_flags,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001202 .port_stp_state_set = b53_br_set_stp_state,
Florian Fainellie6dd86e2021-02-22 14:30:09 -08001203 .port_set_mrouter = b53_set_mrouter,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001204 .port_fast_age = b53_br_fast_age,
1205 .port_vlan_filtering = b53_vlan_filtering,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001206 .port_vlan_add = b53_vlan_add,
1207 .port_vlan_del = b53_vlan_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001208 .port_fdb_dump = b53_fdb_dump,
1209 .port_fdb_add = b53_fdb_add,
1210 .port_fdb_del = b53_fdb_del,
Florian Fainelli73181662017-01-30 09:48:43 -08001211 .get_rxnfc = bcm_sf2_get_rxnfc,
1212 .set_rxnfc = bcm_sf2_set_rxnfc,
Florian Fainelliec960de2017-01-30 12:41:43 -08001213 .port_mirror_add = b53_mirror_add,
1214 .port_mirror_del = b53_mirror_del,
Florian Fainelli29bb5e82019-10-24 12:45:08 -07001215 .port_mdb_add = b53_mdb_add,
1216 .port_mdb_del = b53_mdb_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001217};
1218
Florian Fainellia78e86e2017-01-20 12:36:29 -08001219struct bcm_sf2_of_data {
1220 u32 type;
1221 const u16 *reg_offsets;
1222 unsigned int core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001223 unsigned int num_cfp_rules;
Rafał Miłeckia9349f02021-03-12 11:41:08 +01001224 unsigned int num_crossbar_int_ports;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001225};
1226
Rafał Miłecki73b7a602021-01-06 22:32:02 +01001227static const u16 bcm_sf2_4908_reg_offsets[] = {
1228 [REG_SWITCH_CNTRL] = 0x00,
1229 [REG_SWITCH_STATUS] = 0x04,
1230 [REG_DIR_DATA_WRITE] = 0x08,
1231 [REG_DIR_DATA_READ] = 0x0c,
1232 [REG_SWITCH_REVISION] = 0x10,
1233 [REG_PHY_REVISION] = 0x14,
1234 [REG_SPHY_CNTRL] = 0x24,
1235 [REG_CROSSBAR] = 0xc8,
Rafał Miłecki6859d912021-03-18 09:01:43 +01001236 [REG_RGMII_11_CNTRL] = 0x014c,
Rafał Miłecki73b7a602021-01-06 22:32:02 +01001237 [REG_LED_0_CNTRL] = 0x40,
1238 [REG_LED_1_CNTRL] = 0x4c,
1239 [REG_LED_2_CNTRL] = 0x58,
1240};
1241
1242static const struct bcm_sf2_of_data bcm_sf2_4908_data = {
1243 .type = BCM4908_DEVICE_ID,
1244 .core_reg_align = 0,
1245 .reg_offsets = bcm_sf2_4908_reg_offsets,
Florian Fainellif4e6d7c2021-03-12 13:11:01 -08001246 .num_cfp_rules = 256,
Rafał Miłeckia9349f02021-03-12 11:41:08 +01001247 .num_crossbar_int_ports = 2,
Rafał Miłecki73b7a602021-01-06 22:32:02 +01001248};
1249
Florian Fainellia78e86e2017-01-20 12:36:29 -08001250/* Register offsets for the SWITCH_REG_* block */
1251static const u16 bcm_sf2_7445_reg_offsets[] = {
1252 [REG_SWITCH_CNTRL] = 0x00,
1253 [REG_SWITCH_STATUS] = 0x04,
1254 [REG_DIR_DATA_WRITE] = 0x08,
1255 [REG_DIR_DATA_READ] = 0x0C,
1256 [REG_SWITCH_REVISION] = 0x18,
1257 [REG_PHY_REVISION] = 0x1C,
1258 [REG_SPHY_CNTRL] = 0x2C,
1259 [REG_RGMII_0_CNTRL] = 0x34,
1260 [REG_RGMII_1_CNTRL] = 0x40,
1261 [REG_RGMII_2_CNTRL] = 0x4c,
1262 [REG_LED_0_CNTRL] = 0x90,
1263 [REG_LED_1_CNTRL] = 0x94,
1264 [REG_LED_2_CNTRL] = 0x98,
1265};
1266
1267static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1268 .type = BCM7445_DEVICE_ID,
1269 .core_reg_align = 0,
1270 .reg_offsets = bcm_sf2_7445_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001271 .num_cfp_rules = 256,
Florian Fainellia78e86e2017-01-20 12:36:29 -08001272};
1273
Florian Fainelli0fe99332017-01-20 12:36:30 -08001274static const u16 bcm_sf2_7278_reg_offsets[] = {
1275 [REG_SWITCH_CNTRL] = 0x00,
1276 [REG_SWITCH_STATUS] = 0x04,
1277 [REG_DIR_DATA_WRITE] = 0x08,
1278 [REG_DIR_DATA_READ] = 0x0c,
1279 [REG_SWITCH_REVISION] = 0x10,
1280 [REG_PHY_REVISION] = 0x14,
1281 [REG_SPHY_CNTRL] = 0x24,
1282 [REG_RGMII_0_CNTRL] = 0xe0,
1283 [REG_RGMII_1_CNTRL] = 0xec,
1284 [REG_RGMII_2_CNTRL] = 0xf8,
1285 [REG_LED_0_CNTRL] = 0x40,
1286 [REG_LED_1_CNTRL] = 0x4c,
1287 [REG_LED_2_CNTRL] = 0x58,
1288};
1289
1290static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1291 .type = BCM7278_DEVICE_ID,
1292 .core_reg_align = 1,
1293 .reg_offsets = bcm_sf2_7278_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001294 .num_cfp_rules = 128,
Florian Fainelli0fe99332017-01-20 12:36:30 -08001295};
1296
Florian Fainellia78e86e2017-01-20 12:36:29 -08001297static const struct of_device_id bcm_sf2_of_match[] = {
Rafał Miłecki73b7a602021-01-06 22:32:02 +01001298 { .compatible = "brcm,bcm4908-switch",
1299 .data = &bcm_sf2_4908_data
1300 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001301 { .compatible = "brcm,bcm7445-switch-v4.0",
1302 .data = &bcm_sf2_7445_data
1303 },
Florian Fainelli0fe99332017-01-20 12:36:30 -08001304 { .compatible = "brcm,bcm7278-switch-v4.0",
1305 .data = &bcm_sf2_7278_data
1306 },
Florian Fainelli3b07d782017-12-14 17:59:40 -08001307 { .compatible = "brcm,bcm7278-switch-v4.8",
1308 .data = &bcm_sf2_7278_data
1309 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001310 { /* sentinel */ },
1311};
1312MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1313
Florian Fainellid9338022016-08-18 15:30:14 -07001314static int bcm_sf2_sw_probe(struct platform_device *pdev)
1315{
1316 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1317 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001318 const struct of_device_id *of_id = NULL;
1319 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001320 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001321 struct dsa_switch_ops *ops;
Florian Fainelliafa3b592020-04-05 13:00:30 -07001322 struct device_node *ports;
Florian Fainellid9338022016-08-18 15:30:14 -07001323 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001324 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001325 struct dsa_switch *ds;
1326 void __iomem **base;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001327 unsigned int i;
1328 u32 reg, rev;
1329 int ret;
1330
Florian Fainellif4589952016-08-26 12:18:33 -07001331 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1332 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001333 return -ENOMEM;
1334
Florian Fainellia4c61b92017-01-07 21:01:56 -08001335 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1336 if (!ops)
1337 return -ENOMEM;
1338
Florian Fainellif4589952016-08-26 12:18:33 -07001339 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1340 if (!dev)
1341 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001342
Florian Fainellif4589952016-08-26 12:18:33 -07001343 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1344 if (!pdata)
1345 return -ENOMEM;
1346
Florian Fainellia78e86e2017-01-20 12:36:29 -08001347 of_id = of_match_node(bcm_sf2_of_match, dn);
1348 if (!of_id || !of_id->data)
1349 return -EINVAL;
1350
1351 data = of_id->data;
1352
1353 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1354 priv->type = data->type;
1355 priv->reg_offsets = data->reg_offsets;
1356 priv->core_reg_align = data->core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001357 priv->num_cfp_rules = data->num_cfp_rules;
Rafał Miłeckia9349f02021-03-12 11:41:08 +01001358 priv->num_crossbar_int_ports = data->num_crossbar_int_ports;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001359
Florian Fainellieee87e42019-11-04 13:51:39 -08001360 priv->rcdev = devm_reset_control_get_optional_exclusive(&pdev->dev,
1361 "switch");
Philipp Zabelbf9279c2021-03-05 10:14:48 +01001362 if (IS_ERR(priv->rcdev))
Florian Fainellieee87e42019-11-04 13:51:39 -08001363 return PTR_ERR(priv->rcdev);
1364
Florian Fainellif4589952016-08-26 12:18:33 -07001365 /* Auto-detection using standard registers will not work, so
1366 * provide an indication of what kind of device we are for
1367 * b53_common to work with
1368 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001369 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001370 dev->pdata = pdata;
1371
1372 priv->dev = dev;
1373 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001374 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001375
Florian Fainelli181183772017-09-03 20:27:02 -07001376 /* Advertise the 8 egress queues */
1377 ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1378
Florian Fainellif4589952016-08-26 12:18:33 -07001379 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001380
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001381 spin_lock_init(&priv->indir_lock);
Florian Fainelli73181662017-01-30 09:48:43 -08001382 mutex_init(&priv->cfp.lock);
Florian Fainelliae7a5af2018-11-06 12:58:37 -08001383 INIT_LIST_HEAD(&priv->cfp.rules_list);
Florian Fainelli73181662017-01-30 09:48:43 -08001384
1385 /* CFP rule #0 cannot be used for specific classifications, flag it as
1386 * permanently used
1387 */
1388 set_bit(0, priv->cfp.used);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001389 set_bit(0, priv->cfp.unique);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001390
Florian Fainelli8dbe4c52020-06-17 20:42:44 -07001391 /* Balance of_node_put() done by of_find_node_by_name() */
1392 of_node_get(dn);
Florian Fainelliafa3b592020-04-05 13:00:30 -07001393 ports = of_find_node_by_name(dn, "ports");
1394 if (ports) {
1395 bcm_sf2_identify_ports(priv, ports);
1396 of_node_put(ports);
1397 }
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001398
1399 priv->irq0 = irq_of_parse_and_map(dn, 0);
1400 priv->irq1 = irq_of_parse_and_map(dn, 1);
1401
1402 base = &priv->core;
1403 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
YueHaibing42376782019-08-01 20:29:11 +08001404 *base = devm_platform_ioremap_resource(pdev, i);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001405 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001406 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001407 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001408 }
1409 base++;
1410 }
1411
Florian Fainellie9ec5c32020-09-01 15:59:12 -07001412 priv->clk = devm_clk_get_optional(&pdev->dev, "sw_switch");
1413 if (IS_ERR(priv->clk))
1414 return PTR_ERR(priv->clk);
1415
1416 clk_prepare_enable(priv->clk);
1417
Florian Fainelli2ee3adc2020-09-01 15:59:13 -07001418 priv->clk_mdiv = devm_clk_get_optional(&pdev->dev, "sw_switch_mdiv");
1419 if (IS_ERR(priv->clk_mdiv)) {
1420 ret = PTR_ERR(priv->clk_mdiv);
1421 goto out_clk;
1422 }
1423
1424 clk_prepare_enable(priv->clk_mdiv);
1425
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001426 ret = bcm_sf2_sw_rst(priv);
1427 if (ret) {
1428 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli2ee3adc2020-09-01 15:59:13 -07001429 goto out_clk_mdiv;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001430 }
1431
Rafał Miłeckia9349f02021-03-12 11:41:08 +01001432 bcm_sf2_crossbar_setup(priv);
1433
Florian Fainellic04a17d2018-11-06 15:15:16 -08001434 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1435
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001436 ret = bcm_sf2_mdio_register(ds);
1437 if (ret) {
1438 pr_err("failed to register MDIO bus\n");
Florian Fainelli2ee3adc2020-09-01 15:59:13 -07001439 goto out_clk_mdiv;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001440 }
1441
Florian Fainellic04a17d2018-11-06 15:15:16 -08001442 bcm_sf2_gphy_enable_set(priv->dev->ds, false);
1443
Florian Fainelli73181662017-01-30 09:48:43 -08001444 ret = bcm_sf2_cfp_rst(priv);
1445 if (ret) {
1446 pr_err("failed to reset CFP\n");
1447 goto out_mdio;
1448 }
1449
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001450 /* Disable all interrupts and request them */
1451 bcm_sf2_intr_disable(priv);
1452
Florian Fainelli4bd11672016-08-18 15:30:15 -07001453 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
Florian Fainellibc0cb652018-05-10 13:17:33 -07001454 "switch_0", ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001455 if (ret < 0) {
1456 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001457 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001458 }
1459
Florian Fainelli4bd11672016-08-18 15:30:15 -07001460 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
Florian Fainellibc0cb652018-05-10 13:17:33 -07001461 "switch_1", ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001462 if (ret < 0) {
1463 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001464 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001465 }
1466
1467 /* Reset the MIB counters */
1468 reg = core_readl(priv, CORE_GMNCFGCFG);
1469 reg |= RST_MIB_CNT;
1470 core_writel(priv, reg, CORE_GMNCFGCFG);
1471 reg &= ~RST_MIB_CNT;
1472 core_writel(priv, reg, CORE_GMNCFGCFG);
1473
1474 /* Get the maximum number of ports for this switch */
1475 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1476 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1477 priv->hw_params.num_ports = DSA_MAX_PORTS;
1478
1479 /* Assume a single GPHY setup if we can't read that property */
1480 if (of_property_read_u32(dn, "brcm,num-gphy",
1481 &priv->hw_params.num_gphy))
1482 priv->hw_params.num_gphy = 1;
1483
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001484 rev = reg_readl(priv, REG_SWITCH_REVISION);
1485 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1486 SWITCH_TOP_REV_MASK;
1487 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1488
1489 rev = reg_readl(priv, REG_PHY_REVISION);
1490 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1491
Florian Fainellif4589952016-08-26 12:18:33 -07001492 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001493 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001494 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001495
Florian Fainellifbb7bc42019-03-20 09:45:16 -07001496 dev_info(&pdev->dev,
1497 "Starfighter 2 top: %x.%02x, core: %x.%02x, IRQs: %d, %d\n",
1498 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1499 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1500 priv->irq0, priv->irq1);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001501
1502 return 0;
1503
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001504out_mdio:
1505 bcm_sf2_mdio_unregister(priv);
Florian Fainelli2ee3adc2020-09-01 15:59:13 -07001506out_clk_mdiv:
1507 clk_disable_unprepare(priv->clk_mdiv);
Florian Fainellie9ec5c32020-09-01 15:59:12 -07001508out_clk:
1509 clk_disable_unprepare(priv->clk);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001510 return ret;
1511}
1512
Florian Fainellid9338022016-08-18 15:30:14 -07001513static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001514{
Florian Fainellif4589952016-08-26 12:18:33 -07001515 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001516
Florian Fainellid9338022016-08-18 15:30:14 -07001517 priv->wol_ports_mask = 0;
Florian Fainellie6840002019-11-02 20:17:39 -07001518 /* Disable interrupts */
1519 bcm_sf2_intr_disable(priv);
Florian Fainellif4589952016-08-26 12:18:33 -07001520 dsa_unregister_switch(priv->dev->ds);
Florian Fainelliae7a5af2018-11-06 12:58:37 -08001521 bcm_sf2_cfp_exit(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001522 bcm_sf2_mdio_unregister(priv);
Florian Fainelli2ee3adc2020-09-01 15:59:13 -07001523 clk_disable_unprepare(priv->clk_mdiv);
Florian Fainellie9ec5c32020-09-01 15:59:12 -07001524 clk_disable_unprepare(priv->clk);
Philipp Zabelbf9279c2021-03-05 10:14:48 +01001525 if (priv->type == BCM7278_DEVICE_ID)
Florian Fainellieee87e42019-11-04 13:51:39 -08001526 reset_control_assert(priv->rcdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001527
1528 return 0;
1529}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001530
Florian Fainelli2399d612016-10-20 09:32:19 -07001531static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1532{
1533 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1534
1535 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1536 * successful MDIO bus scan to occur. If we did turn off the GPHY
1537 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001538 *
1539 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001540 */
1541 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001542 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001543}
1544
Florian Fainellid9338022016-08-18 15:30:14 -07001545#ifdef CONFIG_PM_SLEEP
1546static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001547{
Wolfram Sang63382e02018-10-21 22:00:12 +02001548 struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001549
Florian Fainellif4589952016-08-26 12:18:33 -07001550 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001551}
Florian Fainellid9338022016-08-18 15:30:14 -07001552
1553static int bcm_sf2_resume(struct device *dev)
1554{
Wolfram Sang63382e02018-10-21 22:00:12 +02001555 struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001556
Florian Fainellif4589952016-08-26 12:18:33 -07001557 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001558}
1559#endif /* CONFIG_PM_SLEEP */
1560
1561static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1562 bcm_sf2_suspend, bcm_sf2_resume);
1563
Florian Fainellid9338022016-08-18 15:30:14 -07001564
1565static struct platform_driver bcm_sf2_driver = {
1566 .probe = bcm_sf2_sw_probe,
1567 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001568 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001569 .driver = {
1570 .name = "brcm-sf2",
1571 .of_match_table = bcm_sf2_of_match,
1572 .pm = &bcm_sf2_pm_ops,
1573 },
1574};
1575module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001576
1577MODULE_AUTHOR("Broadcom Corporation");
1578MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1579MODULE_LICENSE("GPL");
1580MODULE_ALIAS("platform:brcm-sf2");