blob: 2daca9f0b8ca8299915f5491364287c20ff2225b [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>
17#include <linux/of.h>
18#include <linux/of_irq.h>
19#include <linux/of_address.h>
Florian Fainelli8b7c94e2015-10-23 12:11:08 -070020#include <linux/of_net.h>
Florian Fainelli461cd1b02016-06-07 16:32:43 -070021#include <linux/of_mdio.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070022#include <net/dsa.h>
Florian Fainelli96e65d72014-09-18 17:31:25 -070023#include <linux/ethtool.h>
Florian Fainelli12f460f2015-02-24 13:15:34 -080024#include <linux/if_bridge.h>
Florian Fainelliaafc66f2015-06-10 18:08:01 -070025#include <linux/brcmphy.h>
Florian Fainelli680060d2015-10-23 11:38:07 -070026#include <linux/etherdevice.h>
Florian Fainellif4589952016-08-26 12:18:33 -070027#include <linux/platform_data/b53.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070028
29#include "bcm_sf2.h"
30#include "bcm_sf2_regs.h"
Florian Fainellif4589952016-08-26 12:18:33 -070031#include "b53/b53_priv.h"
32#include "b53/b53_regs.h"
Florian Fainelli246d7f72014-08-27 17:04:56 -070033
Florian Fainelliebb2ac42017-01-20 12:36:31 -080034static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
35{
36 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellic837fc82017-09-03 20:27:03 -070037 unsigned int i;
Florian Fainelliebb2ac42017-01-20 12:36:31 -080038 u32 reg, offset;
39
Florian Fainelliebb2ac42017-01-20 12:36:31 -080040 /* Enable the port memories */
41 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
42 reg &= ~P_TXQ_PSM_VDD(port);
43 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
44
Florian Fainelliebb2ac42017-01-20 12:36:31 -080045 /* Enable forwarding */
46 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
47
48 /* Enable IMP port in dumb mode */
49 reg = core_readl(priv, CORE_SWITCH_CTRL);
50 reg |= MII_DUMB_FWDG_EN;
51 core_writel(priv, reg, CORE_SWITCH_CTRL);
52
Florian Fainellic837fc82017-09-03 20:27:03 -070053 /* Configure Traffic Class to QoS mapping, allow each priority to map
54 * to a different queue number
55 */
56 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
57 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
58 reg |= i << (PRT_TO_QID_SHIFT * i);
59 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
60
Florian Fainellib409a9e2017-09-19 10:46:48 -070061 b53_brcm_hdr_setup(ds, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -070062
Florian Fainelli5fc0f212019-10-31 15:54:05 -070063 if (port == 8) {
64 if (priv->type == BCM7445_DEVICE_ID)
65 offset = CORE_STS_OVERRIDE_IMP;
66 else
67 offset = CORE_STS_OVERRIDE_IMP2;
68
69 /* Force link status for IMP port */
70 reg = core_readl(priv, offset);
Florian Fainellide34d702020-02-06 11:23:52 -080071 reg |= (MII_SW_OR | LINK_STS);
72 if (priv->type == BCM7278_DEVICE_ID)
73 reg |= GMII_SPEED_UP_2G;
Florian Fainelli5fc0f212019-10-31 15:54:05 -070074 core_writel(priv, reg, offset);
75
76 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
77 reg = core_readl(priv, CORE_IMP_CTL);
78 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
79 reg &= ~(RX_DIS | TX_DIS);
80 core_writel(priv, reg, CORE_IMP_CTL);
81 } else {
82 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
83 reg &= ~(RX_DIS | TX_DIS);
84 core_writel(priv, reg, CORE_G_PCTL_PORT(port));
85 }
Florian Fainelli246d7f72014-08-27 17:04:56 -070086}
87
Florian Fainellib0836682015-02-05 11:40:41 -080088static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
89{
Florian Fainellif4589952016-08-26 12:18:33 -070090 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -080091 u32 reg;
92
Florian Fainelli9af197a2015-02-05 11:40:42 -080093 reg = reg_readl(priv, REG_SPHY_CNTRL);
94 if (enable) {
95 reg |= PHY_RESET;
Florian Fainelli4b52d012017-11-21 17:37:46 -080096 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | IDDQ_GLOBAL_PWR | CK25_DIS);
Florian Fainelli9af197a2015-02-05 11:40:42 -080097 reg_writel(priv, reg, REG_SPHY_CNTRL);
98 udelay(21);
99 reg = reg_readl(priv, REG_SPHY_CNTRL);
100 reg &= ~PHY_RESET;
101 } else {
102 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
103 reg_writel(priv, reg, REG_SPHY_CNTRL);
104 mdelay(1);
105 reg |= CK25_DIS;
106 }
107 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800108
Florian Fainelli9af197a2015-02-05 11:40:42 -0800109 /* Use PHY-driven LED signaling */
110 if (!enable) {
111 reg = reg_readl(priv, REG_LED_CNTRL(0));
112 reg |= SPDLNK_SRC_SEL;
113 reg_writel(priv, reg, REG_LED_CNTRL(0));
114 }
Florian Fainellib0836682015-02-05 11:40:41 -0800115}
116
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700117static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
118 int port)
119{
120 unsigned int off;
121
122 switch (port) {
123 case 7:
124 off = P7_IRQ_OFF;
125 break;
126 case 0:
127 /* Port 0 interrupts are located on the first bank */
128 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
129 return;
130 default:
131 off = P_IRQ_OFF(port);
132 break;
133 }
134
135 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
136}
137
138static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
139 int port)
140{
141 unsigned int off;
142
143 switch (port) {
144 case 7:
145 off = P7_IRQ_OFF;
146 break;
147 case 0:
148 /* Port 0 interrupts are located on the first bank */
149 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
150 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
151 return;
152 default:
153 off = P_IRQ_OFF(port);
154 break;
155 }
156
157 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
158 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
159}
160
Florian Fainellib6d045d2014-09-24 17:05:20 -0700161static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
162 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700163{
Florian Fainellif4589952016-08-26 12:18:33 -0700164 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellie1b91472017-01-30 09:48:41 -0800165 unsigned int i;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700166 u32 reg;
167
Vivien Didelot74be4ba2019-08-19 16:00:49 -0400168 if (!dsa_is_user_port(ds, port))
169 return 0;
170
Florian Fainelli246d7f72014-08-27 17:04:56 -0700171 /* Clear the memory power down */
172 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
173 reg &= ~P_TXQ_PSM_VDD(port);
174 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
175
Florian Fainellic0e68202018-08-03 11:08:42 -0700176 /* Enable learning */
177 reg = core_readl(priv, CORE_DIS_LEARN);
178 reg &= ~BIT(port);
179 core_writel(priv, reg, CORE_DIS_LEARN);
180
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800181 /* Enable Broadcom tags for that port if requested */
182 if (priv->brcm_tag_mask & BIT(port))
Florian Fainellib409a9e2017-09-19 10:46:48 -0700183 b53_brcm_hdr_setup(ds, port);
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800184
Florian Fainellie1b91472017-01-30 09:48:41 -0800185 /* Configure Traffic Class to QoS mapping, allow each priority to map
186 * to a different queue number
187 */
188 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
Florian Fainelli181183772017-09-03 20:27:02 -0700189 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
Florian Fainellie1b91472017-01-30 09:48:41 -0800190 reg |= i << (PRT_TO_QID_SHIFT * i);
191 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
192
Florian Fainelli9af197a2015-02-05 11:40:42 -0800193 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700194 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800195 bcm_sf2_gphy_enable_set(ds, true);
196 if (phy) {
197 /* if phy_stop() has been called before, phy
198 * will be in halted state, and phy_start()
199 * will call resume.
200 *
201 * the resume path does not configure back
202 * autoneg settings, and since we hard reset
203 * the phy manually here, we need to reset the
204 * state machine also.
205 */
206 phy->state = PHY_READY;
207 phy_init_hw(phy);
208 }
209 }
210
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700211 /* Enable MoCA port interrupts to get notified */
212 if (port == priv->moca_port)
213 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700214
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700215 /* Set per-queue pause threshold to 32 */
216 core_writel(priv, 32, CORE_TXQ_THD_PAUSE_QN_PORT(port));
217
218 /* Set ACB threshold to 24 */
219 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++) {
220 reg = acb_readl(priv, ACB_QUEUE_CFG(port *
221 SF2_NUM_EGRESS_QUEUES + i));
222 reg &= ~XOFF_THRESHOLD_MASK;
223 reg |= 24;
224 acb_writel(priv, reg, ACB_QUEUE_CFG(port *
225 SF2_NUM_EGRESS_QUEUES + i));
226 }
227
Florian Fainellif86ad772017-09-19 10:46:54 -0700228 return b53_enable_port(ds, port, phy);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700229}
230
Andrew Lunn75104db2019-02-24 20:44:43 +0100231static void bcm_sf2_port_disable(struct dsa_switch *ds, int port)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700232{
Florian Fainellif4589952016-08-26 12:18:33 -0700233 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Colin Ian King5c17a072018-07-04 07:54:36 +0100234 u32 reg;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700235
Florian Fainellic0e68202018-08-03 11:08:42 -0700236 /* Disable learning while in WoL mode */
237 if (priv->wol_ports_mask & (1 << port)) {
238 reg = core_readl(priv, CORE_DIS_LEARN);
239 reg |= BIT(port);
240 core_writel(priv, reg, CORE_DIS_LEARN);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700241 return;
Florian Fainellic0e68202018-08-03 11:08:42 -0700242 }
Florian Fainelli96e65d72014-09-18 17:31:25 -0700243
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700244 if (port == priv->moca_port)
245 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700246
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700247 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800248 bcm_sf2_gphy_enable_set(ds, false);
249
Andrew Lunn75104db2019-02-24 20:44:43 +0100250 b53_disable_port(ds, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700251
252 /* Power down the port memory */
253 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
254 reg |= P_TXQ_PSM_VDD(port);
255 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
256}
257
Florian Fainelli450b05c2014-09-24 17:05:22 -0700258
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700259static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
260 int regnum, u16 val)
261{
262 int ret = 0;
263 u32 reg;
264
265 reg = reg_readl(priv, REG_SWITCH_CNTRL);
266 reg |= MDIO_MASTER_SEL;
267 reg_writel(priv, reg, REG_SWITCH_CNTRL);
268
269 /* Page << 8 | offset */
270 reg = 0x70;
271 reg <<= 2;
272 core_writel(priv, addr, reg);
273
274 /* Page << 8 | offset */
275 reg = 0x80 << 8 | regnum << 1;
276 reg <<= 2;
277
278 if (op)
279 ret = core_readl(priv, reg);
280 else
281 core_writel(priv, val, reg);
282
283 reg = reg_readl(priv, REG_SWITCH_CNTRL);
284 reg &= ~MDIO_MASTER_SEL;
285 reg_writel(priv, reg, REG_SWITCH_CNTRL);
286
287 return ret & 0xffff;
288}
289
290static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
291{
292 struct bcm_sf2_priv *priv = bus->priv;
293
294 /* Intercept reads from Broadcom pseudo-PHY address, else, send
295 * them to our master MDIO bus controller
296 */
297 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
298 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
299 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800300 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700301}
302
303static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
304 u16 val)
305{
306 struct bcm_sf2_priv *priv = bus->priv;
307
308 /* Intercept writes to the Broadcom pseudo-PHY address, else,
309 * send them to our master MDIO bus controller
310 */
311 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
Kangjie Lue49505f2018-12-25 22:08:18 -0600312 return bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700313 else
Kangjie Lue49505f2018-12-25 22:08:18 -0600314 return mdiobus_write_nested(priv->master_mii_bus, addr,
315 regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700316}
317
Florian Fainelli246d7f72014-08-27 17:04:56 -0700318static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
319{
Florian Fainellibc0cb652018-05-10 13:17:33 -0700320 struct dsa_switch *ds = dev_id;
321 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700322
323 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
324 ~priv->irq0_mask;
325 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
326
327 return IRQ_HANDLED;
328}
329
330static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
331{
Florian Fainellibc0cb652018-05-10 13:17:33 -0700332 struct dsa_switch *ds = dev_id;
333 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700334
335 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
336 ~priv->irq1_mask;
337 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
338
Florian Fainellibc0cb652018-05-10 13:17:33 -0700339 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF)) {
340 priv->port_sts[7].link = true;
341 dsa_port_phylink_mac_change(ds, 7, true);
342 }
343 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF)) {
344 priv->port_sts[7].link = false;
345 dsa_port_phylink_mac_change(ds, 7, false);
346 }
Florian Fainelli246d7f72014-08-27 17:04:56 -0700347
348 return IRQ_HANDLED;
349}
350
Florian Fainelli33f84612014-11-25 18:08:49 -0800351static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
352{
353 unsigned int timeout = 1000;
354 u32 reg;
Florian Fainellieee87e42019-11-04 13:51:39 -0800355 int ret;
356
357 /* The watchdog reset does not work on 7278, we need to hit the
358 * "external" reset line through the reset controller.
359 */
360 if (priv->type == BCM7278_DEVICE_ID && !IS_ERR(priv->rcdev)) {
361 ret = reset_control_assert(priv->rcdev);
362 if (ret)
363 return ret;
364
365 return reset_control_deassert(priv->rcdev);
366 }
Florian Fainelli33f84612014-11-25 18:08:49 -0800367
368 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
369 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
370 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
371
372 do {
373 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
374 if (!(reg & SOFTWARE_RESET))
375 break;
376
377 usleep_range(1000, 2000);
378 } while (timeout-- > 0);
379
380 if (timeout == 0)
381 return -ETIMEDOUT;
382
383 return 0;
384}
385
Florian Fainelli691c9a82015-01-20 16:42:00 -0800386static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
387{
Florian Fainellif01d5982016-08-25 15:23:41 -0700388 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800389 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700390 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800391 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800392}
393
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700394static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
395 struct device_node *dn)
396{
397 struct device_node *port;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700398 unsigned int port_num;
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100399 phy_interface_t mode;
400 int err;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700401
402 priv->moca_port = -1;
403
404 for_each_available_child_of_node(dn, port) {
405 if (of_property_read_u32(port, "reg", &port_num))
406 continue;
407
408 /* Internal PHYs get assigned a specific 'phy-mode' property
409 * value: "internal" to help flag them before MDIO probing
410 * has completed, since they might be turned off at that
411 * time
412 */
Andrew Lunn0c65b2b2019-11-04 02:40:33 +0100413 err = of_get_phy_mode(port, &mode);
414 if (err)
Florian Fainellibedd00c2017-06-23 10:33:16 -0700415 continue;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700416
Florian Fainellibedd00c2017-06-23 10:33:16 -0700417 if (mode == PHY_INTERFACE_MODE_INTERNAL)
418 priv->int_phy_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700419
420 if (mode == PHY_INTERFACE_MODE_MOCA)
421 priv->moca_port = port_num;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800422
423 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
424 priv->brcm_tag_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700425 }
426}
427
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700428static int bcm_sf2_mdio_register(struct dsa_switch *ds)
429{
Florian Fainellif4589952016-08-26 12:18:33 -0700430 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700431 struct device_node *dn;
432 static int index;
433 int err;
434
435 /* Find our integrated MDIO bus node */
436 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
437 priv->master_mii_bus = of_mdio_find_bus(dn);
438 if (!priv->master_mii_bus)
439 return -EPROBE_DEFER;
440
441 get_device(&priv->master_mii_bus->dev);
442 priv->master_mii_dn = dn;
443
444 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
445 if (!priv->slave_mii_bus)
446 return -ENOMEM;
447
448 priv->slave_mii_bus->priv = priv;
449 priv->slave_mii_bus->name = "sf2 slave mii";
450 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
451 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
452 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
453 index++);
454 priv->slave_mii_bus->dev.of_node = dn;
455
456 /* Include the pseudo-PHY address to divert reads towards our
457 * workaround. This is only required for 7445D0, since 7445E0
458 * disconnects the internal switch pseudo-PHY such that we can use the
459 * regular SWITCH_MDIO master controller instead.
460 *
461 * Here we flag the pseudo PHY as needing special treatment and would
462 * otherwise make all other PHY read/writes go to the master MDIO bus
463 * controller that comes with this switch backed by the "mdio-unimac"
464 * driver.
465 */
466 if (of_machine_is_compatible("brcm,bcm7445d0"))
467 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
468 else
469 priv->indir_phy_mask = 0;
470
471 ds->phys_mii_mask = priv->indir_phy_mask;
472 ds->slave_mii_bus = priv->slave_mii_bus;
473 priv->slave_mii_bus->parent = ds->dev->parent;
474 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
475
Florian Fainelli00e798c2018-05-15 16:56:19 -0700476 err = of_mdiobus_register(priv->slave_mii_bus, dn);
477 if (err && dn)
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700478 of_node_put(dn);
479
480 return err;
481}
482
483static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
484{
485 mdiobus_unregister(priv->slave_mii_bus);
zhong jiang1ddc5d32018-09-16 21:22:31 +0800486 of_node_put(priv->master_mii_dn);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700487}
488
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700489static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
490{
Florian Fainellif4589952016-08-26 12:18:33 -0700491 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700492
493 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
494 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
495 * the REG_PHY_REVISION register layout is.
496 */
497
498 return priv->hw_params.gphy_rev;
499}
500
Florian Fainellibc0cb652018-05-10 13:17:33 -0700501static void bcm_sf2_sw_validate(struct dsa_switch *ds, int port,
502 unsigned long *supported,
503 struct phylink_link_state *state)
504{
Florian Fainelli738a2e42019-08-21 17:07:46 -0700505 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700506 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
507
508 if (!phy_interface_mode_is_rgmii(state->interface) &&
509 state->interface != PHY_INTERFACE_MODE_MII &&
510 state->interface != PHY_INTERFACE_MODE_REVMII &&
511 state->interface != PHY_INTERFACE_MODE_GMII &&
512 state->interface != PHY_INTERFACE_MODE_INTERNAL &&
513 state->interface != PHY_INTERFACE_MODE_MOCA) {
514 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
Florian Fainelli738a2e42019-08-21 17:07:46 -0700515 if (port != core_readl(priv, CORE_IMP0_PRT_ID))
516 dev_err(ds->dev,
517 "Unsupported interface: %d for port %d\n",
518 state->interface, port);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700519 return;
520 }
521
522 /* Allow all the expected bits */
523 phylink_set(mask, Autoneg);
524 phylink_set_port_modes(mask);
525 phylink_set(mask, Pause);
526 phylink_set(mask, Asym_Pause);
527
528 /* With the exclusion of MII and Reverse MII, we support Gigabit,
529 * including Half duplex
530 */
531 if (state->interface != PHY_INTERFACE_MODE_MII &&
532 state->interface != PHY_INTERFACE_MODE_REVMII) {
533 phylink_set(mask, 1000baseT_Full);
534 phylink_set(mask, 1000baseT_Half);
535 }
536
537 phylink_set(mask, 10baseT_Half);
538 phylink_set(mask, 10baseT_Full);
539 phylink_set(mask, 100baseT_Half);
540 phylink_set(mask, 100baseT_Full);
541
542 bitmap_and(supported, supported, mask,
543 __ETHTOOL_LINK_MODE_MASK_NBITS);
544 bitmap_and(state->advertising, state->advertising, mask,
545 __ETHTOOL_LINK_MODE_MASK_NBITS);
546}
547
548static void bcm_sf2_sw_mac_config(struct dsa_switch *ds, int port,
549 unsigned int mode,
550 const struct phylink_link_state *state)
551{
552 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
553 u32 id_mode_dis = 0, port_mode;
554 u32 reg, offset;
555
Florian Fainelli738a2e42019-08-21 17:07:46 -0700556 if (port == core_readl(priv, CORE_IMP0_PRT_ID))
557 return;
558
Florian Fainellibc0cb652018-05-10 13:17:33 -0700559 if (priv->type == BCM7445_DEVICE_ID)
560 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
561 else
562 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
563
564 switch (state->interface) {
565 case PHY_INTERFACE_MODE_RGMII:
566 id_mode_dis = 1;
567 /* fallthrough */
568 case PHY_INTERFACE_MODE_RGMII_TXID:
569 port_mode = EXT_GPHY;
570 break;
571 case PHY_INTERFACE_MODE_MII:
572 port_mode = EXT_EPHY;
573 break;
574 case PHY_INTERFACE_MODE_REVMII:
575 port_mode = EXT_REVMII;
576 break;
577 default:
578 /* all other PHYs: internal and MoCA */
579 goto force_link;
580 }
581
582 /* Clear id_mode_dis bit, and the existing port mode, let
583 * RGMII_MODE_EN bet set by mac_link_{up,down}
584 */
585 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
586 reg &= ~ID_MODE_DIS;
587 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
588 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
589
590 reg |= port_mode;
591 if (id_mode_dis)
592 reg |= ID_MODE_DIS;
593
594 if (state->pause & MLO_PAUSE_TXRX_MASK) {
595 if (state->pause & MLO_PAUSE_TX)
596 reg |= TX_PAUSE_EN;
597 reg |= RX_PAUSE_EN;
598 }
599
600 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
601
602force_link:
603 /* Force link settings detected from the PHY */
604 reg = SW_OVERRIDE;
605 switch (state->speed) {
606 case SPEED_1000:
607 reg |= SPDSTS_1000 << SPEED_SHIFT;
608 break;
609 case SPEED_100:
610 reg |= SPDSTS_100 << SPEED_SHIFT;
611 break;
612 }
613
614 if (state->link)
615 reg |= LINK_STS;
616 if (state->duplex == DUPLEX_FULL)
617 reg |= DUPLX_MODE;
618
619 core_writel(priv, reg, offset);
620}
621
622static void bcm_sf2_sw_mac_link_set(struct dsa_switch *ds, int port,
623 phy_interface_t interface, bool link)
624{
625 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
626 u32 reg;
627
628 if (!phy_interface_mode_is_rgmii(interface) &&
629 interface != PHY_INTERFACE_MODE_MII &&
630 interface != PHY_INTERFACE_MODE_REVMII)
631 return;
632
633 /* If the link is down, just disable the interface to conserve power */
634 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
635 if (link)
636 reg |= RGMII_MODE_EN;
637 else
638 reg &= ~RGMII_MODE_EN;
639 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
640}
641
642static void bcm_sf2_sw_mac_link_down(struct dsa_switch *ds, int port,
643 unsigned int mode,
644 phy_interface_t interface)
645{
646 bcm_sf2_sw_mac_link_set(ds, port, interface, false);
647}
648
649static void bcm_sf2_sw_mac_link_up(struct dsa_switch *ds, int port,
650 unsigned int mode,
651 phy_interface_t interface,
Russell King5b502a72020-02-26 10:23:46 +0000652 struct phy_device *phydev,
653 int speed, int duplex,
654 bool tx_pause, bool rx_pause)
Florian Fainellibc0cb652018-05-10 13:17:33 -0700655{
656 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
657 struct ethtool_eee *p = &priv->dev->ports[port].eee;
658
659 bcm_sf2_sw_mac_link_set(ds, port, interface, true);
660
661 if (mode == MLO_AN_PHY && phydev)
662 p->eee_enabled = b53_eee_init(ds, port, phydev);
663}
664
665static void bcm_sf2_sw_fixed_state(struct dsa_switch *ds, int port,
666 struct phylink_link_state *status)
667{
668 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
669
670 status->link = false;
671
672 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
673 * which means that we need to force the link at the port override
674 * level to get the data to flow. We do use what the interrupt handler
675 * did determine before.
676 *
677 * For the other ports, we just force the link status, since this is
678 * a fixed PHY device.
679 */
680 if (port == priv->moca_port) {
681 status->link = priv->port_sts[port].link;
682 /* For MoCA interfaces, also force a link down notification
683 * since some version of the user-space daemon (mocad) use
684 * cmd->autoneg to force the link, which messes up the PHY
685 * state machine and make it go in PHY_FORCING state instead.
686 */
687 if (!status->link)
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400688 netif_carrier_off(dsa_to_port(ds, port)->slave);
Florian Fainellibc0cb652018-05-10 13:17:33 -0700689 status->duplex = DUPLEX_FULL;
690 } else {
691 status->link = true;
692 }
693}
694
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700695static void bcm_sf2_enable_acb(struct dsa_switch *ds)
696{
697 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
698 u32 reg;
699
700 /* Enable ACB globally */
701 reg = acb_readl(priv, ACB_CONTROL);
702 reg |= (ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
703 acb_writel(priv, reg, ACB_CONTROL);
704 reg &= ~(ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
705 reg |= ACB_EN | ACB_ALGORITHM;
706 acb_writel(priv, reg, ACB_CONTROL);
707}
708
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700709static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
710{
Florian Fainellif4589952016-08-26 12:18:33 -0700711 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700712 unsigned int port;
713
Florian Fainelli691c9a82015-01-20 16:42:00 -0800714 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700715
716 /* Disable all ports physically present including the IMP
717 * port, the other ones have already been disabled during
718 * bcm_sf2_sw_setup
719 */
Dan Carpenter8d6ea932019-02-13 11:23:04 +0300720 for (port = 0; port < ds->num_ports; port++) {
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400721 if (dsa_is_user_port(ds, port) || dsa_is_cpu_port(ds, port))
Andrew Lunn75104db2019-02-24 20:44:43 +0100722 bcm_sf2_port_disable(ds, port);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700723 }
724
725 return 0;
726}
727
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700728static int bcm_sf2_sw_resume(struct dsa_switch *ds)
729{
Florian Fainellif4589952016-08-26 12:18:33 -0700730 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700731 int ret;
732
733 ret = bcm_sf2_sw_rst(priv);
734 if (ret) {
735 pr_err("%s: failed to software reset switch\n", __func__);
736 return ret;
737 }
738
Florian Fainelli1c0130f2018-11-06 12:58:39 -0800739 ret = bcm_sf2_cfp_resume(ds);
740 if (ret)
741 return ret;
742
Florian Fainellib0836682015-02-05 11:40:41 -0800743 if (priv->hw_params.num_gphy == 1)
744 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700745
Florian Fainelliabd01ba2018-10-09 16:48:58 -0700746 ds->ops->setup(ds);
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700747
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700748 return 0;
749}
750
Florian Fainelli96e65d72014-09-18 17:31:25 -0700751static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
752 struct ethtool_wolinfo *wol)
753{
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400754 struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
Florian Fainellif4589952016-08-26 12:18:33 -0700755 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellic3152ec2019-02-15 12:16:52 -0800756 struct ethtool_wolinfo pwol = { };
Florian Fainelli96e65d72014-09-18 17:31:25 -0700757
758 /* Get the parent device WoL settings */
Florian Fainellic3152ec2019-02-15 12:16:52 -0800759 if (p->ethtool_ops->get_wol)
760 p->ethtool_ops->get_wol(p, &pwol);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700761
762 /* Advertise the parent device supported settings */
763 wol->supported = pwol.supported;
764 memset(&wol->sopass, 0, sizeof(wol->sopass));
765
766 if (pwol.wolopts & WAKE_MAGICSECURE)
767 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
768
769 if (priv->wol_ports_mask & (1 << port))
770 wol->wolopts = pwol.wolopts;
771 else
772 wol->wolopts = 0;
773}
774
775static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
776 struct ethtool_wolinfo *wol)
777{
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400778 struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
Florian Fainellif4589952016-08-26 12:18:33 -0700779 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot68bb8ea2019-10-21 16:51:15 -0400780 s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
Florian Fainellic3152ec2019-02-15 12:16:52 -0800781 struct ethtool_wolinfo pwol = { };
Florian Fainelli96e65d72014-09-18 17:31:25 -0700782
Florian Fainellic3152ec2019-02-15 12:16:52 -0800783 if (p->ethtool_ops->get_wol)
784 p->ethtool_ops->get_wol(p, &pwol);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700785 if (wol->wolopts & ~pwol.supported)
786 return -EINVAL;
787
788 if (wol->wolopts)
789 priv->wol_ports_mask |= (1 << port);
790 else
791 priv->wol_ports_mask &= ~(1 << port);
792
793 /* If we have at least one port enabled, make sure the CPU port
794 * is also enabled. If the CPU port is the last one enabled, we disable
795 * it since this configuration does not make sense.
796 */
797 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
798 priv->wol_ports_mask |= (1 << cpu_port);
799 else
800 priv->wol_ports_mask &= ~(1 << cpu_port);
801
802 return p->ethtool_ops->set_wol(p, wol);
803}
804
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700805static int bcm_sf2_sw_setup(struct dsa_switch *ds)
806{
Florian Fainellif4589952016-08-26 12:18:33 -0700807 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700808 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700809
Florian Fainelli21a27742017-09-28 11:19:06 -0700810 /* Enable all valid ports and disable those unused */
Florian Fainellid9338022016-08-18 15:30:14 -0700811 for (port = 0; port < priv->hw_params.num_ports; port++) {
Florian Fainelli21a27742017-09-28 11:19:06 -0700812 /* IMP port receives special treatment */
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400813 if (dsa_is_user_port(ds, port))
Florian Fainelli21a27742017-09-28 11:19:06 -0700814 bcm_sf2_port_setup(ds, port, NULL);
815 else if (dsa_is_cpu_port(ds, port))
Florian Fainellid9338022016-08-18 15:30:14 -0700816 bcm_sf2_imp_setup(ds, port);
Florian Fainelli21a27742017-09-28 11:19:06 -0700817 else
Andrew Lunn75104db2019-02-24 20:44:43 +0100818 bcm_sf2_port_disable(ds, port);
Florian Fainellid9338022016-08-18 15:30:14 -0700819 }
820
Florian Fainelli5c1a6ea2017-10-27 15:56:01 -0700821 b53_configure_vlan(ds);
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700822 bcm_sf2_enable_acb(ds);
Florian Fainellid9338022016-08-18 15:30:14 -0700823
824 return 0;
825}
826
Florian Fainellif4589952016-08-26 12:18:33 -0700827/* The SWITCH_CORE register space is managed by b53 but operates on a page +
828 * register basis so we need to translate that into an address that the
829 * bus-glue understands.
830 */
831#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
832
833static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
834 u8 *val)
835{
836 struct bcm_sf2_priv *priv = dev->priv;
837
838 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
839
840 return 0;
841}
842
843static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
844 u16 *val)
845{
846 struct bcm_sf2_priv *priv = dev->priv;
847
848 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
849
850 return 0;
851}
852
853static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
854 u32 *val)
855{
856 struct bcm_sf2_priv *priv = dev->priv;
857
858 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
859
860 return 0;
861}
862
863static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
864 u64 *val)
865{
866 struct bcm_sf2_priv *priv = dev->priv;
867
868 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
869
870 return 0;
871}
872
873static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
874 u8 value)
875{
876 struct bcm_sf2_priv *priv = dev->priv;
877
878 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
879
880 return 0;
881}
882
883static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
884 u16 value)
885{
886 struct bcm_sf2_priv *priv = dev->priv;
887
888 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
889
890 return 0;
891}
892
893static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
894 u32 value)
895{
896 struct bcm_sf2_priv *priv = dev->priv;
897
898 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
899
900 return 0;
901}
902
903static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
904 u64 value)
905{
906 struct bcm_sf2_priv *priv = dev->priv;
907
908 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
909
910 return 0;
911}
912
Bhumika Goyal7e3108f2017-08-29 22:17:52 +0530913static const struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -0700914 .read8 = bcm_sf2_core_read8,
915 .read16 = bcm_sf2_core_read16,
916 .read32 = bcm_sf2_core_read32,
917 .read48 = bcm_sf2_core_read64,
918 .read64 = bcm_sf2_core_read64,
919 .write8 = bcm_sf2_core_write8,
920 .write16 = bcm_sf2_core_write16,
921 .write32 = bcm_sf2_core_write32,
922 .write48 = bcm_sf2_core_write64,
923 .write64 = bcm_sf2_core_write64,
924};
925
Florian Fainellibadd62c2019-02-06 12:45:58 -0800926static void bcm_sf2_sw_get_strings(struct dsa_switch *ds, int port,
927 u32 stringset, uint8_t *data)
928{
Florian Fainellif4ae9c02019-02-06 12:45:59 -0800929 int cnt = b53_get_sset_count(ds, port, stringset);
930
Florian Fainellibadd62c2019-02-06 12:45:58 -0800931 b53_get_strings(ds, port, stringset, data);
Florian Fainellif4ae9c02019-02-06 12:45:59 -0800932 bcm_sf2_cfp_get_strings(ds, port, stringset,
933 data + cnt * ETH_GSTRING_LEN);
Florian Fainellibadd62c2019-02-06 12:45:58 -0800934}
935
936static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds, int port,
937 uint64_t *data)
938{
Florian Fainellif4ae9c02019-02-06 12:45:59 -0800939 int cnt = b53_get_sset_count(ds, port, ETH_SS_STATS);
940
Florian Fainellibadd62c2019-02-06 12:45:58 -0800941 b53_get_ethtool_stats(ds, port, data);
Florian Fainellif4ae9c02019-02-06 12:45:59 -0800942 bcm_sf2_cfp_get_ethtool_stats(ds, port, data + cnt);
Florian Fainellibadd62c2019-02-06 12:45:58 -0800943}
944
945static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds, int port,
946 int sset)
947{
Florian Fainellif4ae9c02019-02-06 12:45:59 -0800948 int cnt = b53_get_sset_count(ds, port, sset);
949
950 if (cnt < 0)
951 return cnt;
952
953 cnt += bcm_sf2_cfp_get_sset_count(ds, port, sset);
954
955 return cnt;
Florian Fainellibadd62c2019-02-06 12:45:58 -0800956}
957
Florian Fainellia82f67a2017-01-08 14:52:08 -0800958static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli9f668162017-11-30 09:55:35 -0800959 .get_tag_protocol = b53_get_tag_protocol,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800960 .setup = bcm_sf2_sw_setup,
Florian Fainellibadd62c2019-02-06 12:45:58 -0800961 .get_strings = bcm_sf2_sw_get_strings,
962 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
963 .get_sset_count = bcm_sf2_sw_get_sset_count,
Florian Fainellic7d28c92018-04-25 12:12:53 -0700964 .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800965 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
Florian Fainellibc0cb652018-05-10 13:17:33 -0700966 .phylink_validate = bcm_sf2_sw_validate,
967 .phylink_mac_config = bcm_sf2_sw_mac_config,
968 .phylink_mac_link_down = bcm_sf2_sw_mac_link_down,
969 .phylink_mac_link_up = bcm_sf2_sw_mac_link_up,
970 .phylink_fixed_state = bcm_sf2_sw_fixed_state,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800971 .suspend = bcm_sf2_sw_suspend,
972 .resume = bcm_sf2_sw_resume,
973 .get_wol = bcm_sf2_sw_get_wol,
974 .set_wol = bcm_sf2_sw_set_wol,
975 .port_enable = bcm_sf2_port_setup,
976 .port_disable = bcm_sf2_port_disable,
Florian Fainelli22256b02017-09-19 10:46:50 -0700977 .get_mac_eee = b53_get_mac_eee,
978 .set_mac_eee = b53_set_mac_eee,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800979 .port_bridge_join = b53_br_join,
980 .port_bridge_leave = b53_br_leave,
981 .port_stp_state_set = b53_br_set_stp_state,
982 .port_fast_age = b53_br_fast_age,
983 .port_vlan_filtering = b53_vlan_filtering,
984 .port_vlan_prepare = b53_vlan_prepare,
985 .port_vlan_add = b53_vlan_add,
986 .port_vlan_del = b53_vlan_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800987 .port_fdb_dump = b53_fdb_dump,
988 .port_fdb_add = b53_fdb_add,
989 .port_fdb_del = b53_fdb_del,
Florian Fainelli73181662017-01-30 09:48:43 -0800990 .get_rxnfc = bcm_sf2_get_rxnfc,
991 .set_rxnfc = bcm_sf2_set_rxnfc,
Florian Fainelliec960de2017-01-30 12:41:43 -0800992 .port_mirror_add = b53_mirror_add,
993 .port_mirror_del = b53_mirror_del,
Florian Fainelli29bb5e82019-10-24 12:45:08 -0700994 .port_mdb_prepare = b53_mdb_prepare,
995 .port_mdb_add = b53_mdb_add,
996 .port_mdb_del = b53_mdb_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800997};
998
Florian Fainellia78e86e2017-01-20 12:36:29 -0800999struct bcm_sf2_of_data {
1000 u32 type;
1001 const u16 *reg_offsets;
1002 unsigned int core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001003 unsigned int num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001004};
1005
1006/* Register offsets for the SWITCH_REG_* block */
1007static const u16 bcm_sf2_7445_reg_offsets[] = {
1008 [REG_SWITCH_CNTRL] = 0x00,
1009 [REG_SWITCH_STATUS] = 0x04,
1010 [REG_DIR_DATA_WRITE] = 0x08,
1011 [REG_DIR_DATA_READ] = 0x0C,
1012 [REG_SWITCH_REVISION] = 0x18,
1013 [REG_PHY_REVISION] = 0x1C,
1014 [REG_SPHY_CNTRL] = 0x2C,
1015 [REG_RGMII_0_CNTRL] = 0x34,
1016 [REG_RGMII_1_CNTRL] = 0x40,
1017 [REG_RGMII_2_CNTRL] = 0x4c,
1018 [REG_LED_0_CNTRL] = 0x90,
1019 [REG_LED_1_CNTRL] = 0x94,
1020 [REG_LED_2_CNTRL] = 0x98,
1021};
1022
1023static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1024 .type = BCM7445_DEVICE_ID,
1025 .core_reg_align = 0,
1026 .reg_offsets = bcm_sf2_7445_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001027 .num_cfp_rules = 256,
Florian Fainellia78e86e2017-01-20 12:36:29 -08001028};
1029
Florian Fainelli0fe99332017-01-20 12:36:30 -08001030static const u16 bcm_sf2_7278_reg_offsets[] = {
1031 [REG_SWITCH_CNTRL] = 0x00,
1032 [REG_SWITCH_STATUS] = 0x04,
1033 [REG_DIR_DATA_WRITE] = 0x08,
1034 [REG_DIR_DATA_READ] = 0x0c,
1035 [REG_SWITCH_REVISION] = 0x10,
1036 [REG_PHY_REVISION] = 0x14,
1037 [REG_SPHY_CNTRL] = 0x24,
1038 [REG_RGMII_0_CNTRL] = 0xe0,
1039 [REG_RGMII_1_CNTRL] = 0xec,
1040 [REG_RGMII_2_CNTRL] = 0xf8,
1041 [REG_LED_0_CNTRL] = 0x40,
1042 [REG_LED_1_CNTRL] = 0x4c,
1043 [REG_LED_2_CNTRL] = 0x58,
1044};
1045
1046static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1047 .type = BCM7278_DEVICE_ID,
1048 .core_reg_align = 1,
1049 .reg_offsets = bcm_sf2_7278_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001050 .num_cfp_rules = 128,
Florian Fainelli0fe99332017-01-20 12:36:30 -08001051};
1052
Florian Fainellia78e86e2017-01-20 12:36:29 -08001053static const struct of_device_id bcm_sf2_of_match[] = {
1054 { .compatible = "brcm,bcm7445-switch-v4.0",
1055 .data = &bcm_sf2_7445_data
1056 },
Florian Fainelli0fe99332017-01-20 12:36:30 -08001057 { .compatible = "brcm,bcm7278-switch-v4.0",
1058 .data = &bcm_sf2_7278_data
1059 },
Florian Fainelli3b07d782017-12-14 17:59:40 -08001060 { .compatible = "brcm,bcm7278-switch-v4.8",
1061 .data = &bcm_sf2_7278_data
1062 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001063 { /* sentinel */ },
1064};
1065MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1066
Florian Fainellid9338022016-08-18 15:30:14 -07001067static int bcm_sf2_sw_probe(struct platform_device *pdev)
1068{
1069 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1070 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001071 const struct of_device_id *of_id = NULL;
1072 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001073 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001074 struct dsa_switch_ops *ops;
Florian Fainellid9338022016-08-18 15:30:14 -07001075 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001076 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001077 struct dsa_switch *ds;
1078 void __iomem **base;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001079 unsigned int i;
1080 u32 reg, rev;
1081 int ret;
1082
Florian Fainellif4589952016-08-26 12:18:33 -07001083 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1084 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001085 return -ENOMEM;
1086
Florian Fainellia4c61b92017-01-07 21:01:56 -08001087 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1088 if (!ops)
1089 return -ENOMEM;
1090
Florian Fainellif4589952016-08-26 12:18:33 -07001091 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1092 if (!dev)
1093 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001094
Florian Fainellif4589952016-08-26 12:18:33 -07001095 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1096 if (!pdata)
1097 return -ENOMEM;
1098
Florian Fainellia78e86e2017-01-20 12:36:29 -08001099 of_id = of_match_node(bcm_sf2_of_match, dn);
1100 if (!of_id || !of_id->data)
1101 return -EINVAL;
1102
1103 data = of_id->data;
1104
1105 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1106 priv->type = data->type;
1107 priv->reg_offsets = data->reg_offsets;
1108 priv->core_reg_align = data->core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001109 priv->num_cfp_rules = data->num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001110
Florian Fainellieee87e42019-11-04 13:51:39 -08001111 priv->rcdev = devm_reset_control_get_optional_exclusive(&pdev->dev,
1112 "switch");
1113 if (PTR_ERR(priv->rcdev) == -EPROBE_DEFER)
1114 return PTR_ERR(priv->rcdev);
1115
Florian Fainellif4589952016-08-26 12:18:33 -07001116 /* Auto-detection using standard registers will not work, so
1117 * provide an indication of what kind of device we are for
1118 * b53_common to work with
1119 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001120 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001121 dev->pdata = pdata;
1122
1123 priv->dev = dev;
1124 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001125 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001126
Florian Fainelli181183772017-09-03 20:27:02 -07001127 /* Advertise the 8 egress queues */
1128 ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1129
Florian Fainellif4589952016-08-26 12:18:33 -07001130 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001131
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001132 spin_lock_init(&priv->indir_lock);
Florian Fainelli73181662017-01-30 09:48:43 -08001133 mutex_init(&priv->cfp.lock);
Florian Fainelliae7a5af2018-11-06 12:58:37 -08001134 INIT_LIST_HEAD(&priv->cfp.rules_list);
Florian Fainelli73181662017-01-30 09:48:43 -08001135
1136 /* CFP rule #0 cannot be used for specific classifications, flag it as
1137 * permanently used
1138 */
1139 set_bit(0, priv->cfp.used);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001140 set_bit(0, priv->cfp.unique);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001141
Florian Fainellid9338022016-08-18 15:30:14 -07001142 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001143
1144 priv->irq0 = irq_of_parse_and_map(dn, 0);
1145 priv->irq1 = irq_of_parse_and_map(dn, 1);
1146
1147 base = &priv->core;
1148 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
YueHaibing42376782019-08-01 20:29:11 +08001149 *base = devm_platform_ioremap_resource(pdev, i);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001150 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001151 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001152 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001153 }
1154 base++;
1155 }
1156
1157 ret = bcm_sf2_sw_rst(priv);
1158 if (ret) {
1159 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001160 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001161 }
1162
Florian Fainellic04a17d2018-11-06 15:15:16 -08001163 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1164
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001165 ret = bcm_sf2_mdio_register(ds);
1166 if (ret) {
1167 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001168 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001169 }
1170
Florian Fainellic04a17d2018-11-06 15:15:16 -08001171 bcm_sf2_gphy_enable_set(priv->dev->ds, false);
1172
Florian Fainelli73181662017-01-30 09:48:43 -08001173 ret = bcm_sf2_cfp_rst(priv);
1174 if (ret) {
1175 pr_err("failed to reset CFP\n");
1176 goto out_mdio;
1177 }
1178
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001179 /* Disable all interrupts and request them */
1180 bcm_sf2_intr_disable(priv);
1181
Florian Fainelli4bd11672016-08-18 15:30:15 -07001182 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
Florian Fainellibc0cb652018-05-10 13:17:33 -07001183 "switch_0", ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001184 if (ret < 0) {
1185 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001186 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001187 }
1188
Florian Fainelli4bd11672016-08-18 15:30:15 -07001189 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
Florian Fainellibc0cb652018-05-10 13:17:33 -07001190 "switch_1", ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001191 if (ret < 0) {
1192 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001193 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001194 }
1195
1196 /* Reset the MIB counters */
1197 reg = core_readl(priv, CORE_GMNCFGCFG);
1198 reg |= RST_MIB_CNT;
1199 core_writel(priv, reg, CORE_GMNCFGCFG);
1200 reg &= ~RST_MIB_CNT;
1201 core_writel(priv, reg, CORE_GMNCFGCFG);
1202
1203 /* Get the maximum number of ports for this switch */
1204 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1205 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1206 priv->hw_params.num_ports = DSA_MAX_PORTS;
1207
1208 /* Assume a single GPHY setup if we can't read that property */
1209 if (of_property_read_u32(dn, "brcm,num-gphy",
1210 &priv->hw_params.num_gphy))
1211 priv->hw_params.num_gphy = 1;
1212
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001213 rev = reg_readl(priv, REG_SWITCH_REVISION);
1214 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1215 SWITCH_TOP_REV_MASK;
1216 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1217
1218 rev = reg_readl(priv, REG_PHY_REVISION);
1219 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1220
Florian Fainellif4589952016-08-26 12:18:33 -07001221 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001222 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001223 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001224
Florian Fainellifbb7bc42019-03-20 09:45:16 -07001225 dev_info(&pdev->dev,
1226 "Starfighter 2 top: %x.%02x, core: %x.%02x, IRQs: %d, %d\n",
1227 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1228 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1229 priv->irq0, priv->irq1);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001230
1231 return 0;
1232
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001233out_mdio:
1234 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001235 return ret;
1236}
1237
Florian Fainellid9338022016-08-18 15:30:14 -07001238static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001239{
Florian Fainellif4589952016-08-26 12:18:33 -07001240 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001241
Florian Fainellid9338022016-08-18 15:30:14 -07001242 priv->wol_ports_mask = 0;
Florian Fainellie6840002019-11-02 20:17:39 -07001243 /* Disable interrupts */
1244 bcm_sf2_intr_disable(priv);
Florian Fainellif4589952016-08-26 12:18:33 -07001245 dsa_unregister_switch(priv->dev->ds);
Florian Fainelliae7a5af2018-11-06 12:58:37 -08001246 bcm_sf2_cfp_exit(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001247 bcm_sf2_mdio_unregister(priv);
Florian Fainellieee87e42019-11-04 13:51:39 -08001248 if (priv->type == BCM7278_DEVICE_ID && !IS_ERR(priv->rcdev))
1249 reset_control_assert(priv->rcdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001250
1251 return 0;
1252}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001253
Florian Fainelli2399d612016-10-20 09:32:19 -07001254static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1255{
1256 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1257
1258 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1259 * successful MDIO bus scan to occur. If we did turn off the GPHY
1260 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001261 *
1262 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001263 */
1264 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001265 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001266}
1267
Florian Fainellid9338022016-08-18 15:30:14 -07001268#ifdef CONFIG_PM_SLEEP
1269static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001270{
Wolfram Sang63382e02018-10-21 22:00:12 +02001271 struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001272
Florian Fainellif4589952016-08-26 12:18:33 -07001273 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001274}
Florian Fainellid9338022016-08-18 15:30:14 -07001275
1276static int bcm_sf2_resume(struct device *dev)
1277{
Wolfram Sang63382e02018-10-21 22:00:12 +02001278 struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001279
Florian Fainellif4589952016-08-26 12:18:33 -07001280 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001281}
1282#endif /* CONFIG_PM_SLEEP */
1283
1284static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1285 bcm_sf2_suspend, bcm_sf2_resume);
1286
Florian Fainellid9338022016-08-18 15:30:14 -07001287
1288static struct platform_driver bcm_sf2_driver = {
1289 .probe = bcm_sf2_sw_probe,
1290 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001291 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001292 .driver = {
1293 .name = "brcm-sf2",
1294 .of_match_table = bcm_sf2_of_match,
1295 .pm = &bcm_sf2_pm_ops,
1296 },
1297};
1298module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001299
1300MODULE_AUTHOR("Broadcom Corporation");
1301MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1302MODULE_LICENSE("GPL");
1303MODULE_ALIAS("platform:brcm-sf2");