blob: ac621f44237a97d852f6383d69031d35f7eaf124 [file] [log] [blame]
Florian Fainelli246d7f72014-08-27 17:04:56 -07001/*
2 * Broadcom Starfighter 2 DSA switch driver
3 *
4 * Copyright (C) 2014, Broadcom Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/list.h>
13#include <linux/module.h>
14#include <linux/netdevice.h>
15#include <linux/interrupt.h>
16#include <linux/platform_device.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070017#include <linux/phy.h>
18#include <linux/phy_fixed.h>
Florian Fainellibc0cb652018-05-10 13:17:33 -070019#include <linux/phylink.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070020#include <linux/mii.h>
21#include <linux/of.h>
22#include <linux/of_irq.h>
23#include <linux/of_address.h>
Florian Fainelli8b7c94e2015-10-23 12:11:08 -070024#include <linux/of_net.h>
Florian Fainelli461cd1b02016-06-07 16:32:43 -070025#include <linux/of_mdio.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070026#include <net/dsa.h>
Florian Fainelli96e65d72014-09-18 17:31:25 -070027#include <linux/ethtool.h>
Florian Fainelli12f460f2015-02-24 13:15:34 -080028#include <linux/if_bridge.h>
Florian Fainelliaafc66f2015-06-10 18:08:01 -070029#include <linux/brcmphy.h>
Florian Fainelli680060d2015-10-23 11:38:07 -070030#include <linux/etherdevice.h>
Florian Fainellif4589952016-08-26 12:18:33 -070031#include <linux/platform_data/b53.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070032
33#include "bcm_sf2.h"
34#include "bcm_sf2_regs.h"
Florian Fainellif4589952016-08-26 12:18:33 -070035#include "b53/b53_priv.h"
36#include "b53/b53_regs.h"
Florian Fainelli246d7f72014-08-27 17:04:56 -070037
Florian Fainelliebb2ac42017-01-20 12:36:31 -080038static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
39{
40 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellic837fc82017-09-03 20:27:03 -070041 unsigned int i;
Florian Fainelliebb2ac42017-01-20 12:36:31 -080042 u32 reg, offset;
43
44 if (priv->type == BCM7445_DEVICE_ID)
45 offset = CORE_STS_OVERRIDE_IMP;
46 else
47 offset = CORE_STS_OVERRIDE_IMP2;
48
49 /* Enable the port memories */
50 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
51 reg &= ~P_TXQ_PSM_VDD(port);
52 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
53
54 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
55 reg = core_readl(priv, CORE_IMP_CTL);
56 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
57 reg &= ~(RX_DIS | TX_DIS);
58 core_writel(priv, reg, CORE_IMP_CTL);
59
60 /* Enable forwarding */
61 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
62
63 /* Enable IMP port in dumb mode */
64 reg = core_readl(priv, CORE_SWITCH_CTRL);
65 reg |= MII_DUMB_FWDG_EN;
66 core_writel(priv, reg, CORE_SWITCH_CTRL);
67
Florian Fainellic837fc82017-09-03 20:27:03 -070068 /* Configure Traffic Class to QoS mapping, allow each priority to map
69 * to a different queue number
70 */
71 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
72 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
73 reg |= i << (PRT_TO_QID_SHIFT * i);
74 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
75
Florian Fainellib409a9e2017-09-19 10:46:48 -070076 b53_brcm_hdr_setup(ds, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -070077
78 /* Force link status for IMP port */
Florian Fainelli0fe99332017-01-20 12:36:30 -080079 reg = core_readl(priv, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -070080 reg |= (MII_SW_OR | LINK_STS);
Florian Fainelli0fe99332017-01-20 12:36:30 -080081 core_writel(priv, reg, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -070082}
83
Florian Fainellib0836682015-02-05 11:40:41 -080084static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
85{
Florian Fainellif4589952016-08-26 12:18:33 -070086 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -080087 u32 reg;
88
Florian Fainelli9af197a2015-02-05 11:40:42 -080089 reg = reg_readl(priv, REG_SPHY_CNTRL);
90 if (enable) {
91 reg |= PHY_RESET;
Florian Fainelli4b52d012017-11-21 17:37:46 -080092 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | IDDQ_GLOBAL_PWR | CK25_DIS);
Florian Fainelli9af197a2015-02-05 11:40:42 -080093 reg_writel(priv, reg, REG_SPHY_CNTRL);
94 udelay(21);
95 reg = reg_readl(priv, REG_SPHY_CNTRL);
96 reg &= ~PHY_RESET;
97 } else {
98 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
99 reg_writel(priv, reg, REG_SPHY_CNTRL);
100 mdelay(1);
101 reg |= CK25_DIS;
102 }
103 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800104
Florian Fainelli9af197a2015-02-05 11:40:42 -0800105 /* Use PHY-driven LED signaling */
106 if (!enable) {
107 reg = reg_readl(priv, REG_LED_CNTRL(0));
108 reg |= SPDLNK_SRC_SEL;
109 reg_writel(priv, reg, REG_LED_CNTRL(0));
110 }
Florian Fainellib0836682015-02-05 11:40:41 -0800111}
112
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700113static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
114 int port)
115{
116 unsigned int off;
117
118 switch (port) {
119 case 7:
120 off = P7_IRQ_OFF;
121 break;
122 case 0:
123 /* Port 0 interrupts are located on the first bank */
124 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
125 return;
126 default:
127 off = P_IRQ_OFF(port);
128 break;
129 }
130
131 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
132}
133
134static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
135 int port)
136{
137 unsigned int off;
138
139 switch (port) {
140 case 7:
141 off = P7_IRQ_OFF;
142 break;
143 case 0:
144 /* Port 0 interrupts are located on the first bank */
145 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
146 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
147 return;
148 default:
149 off = P_IRQ_OFF(port);
150 break;
151 }
152
153 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
154 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
155}
156
Florian Fainellib6d045d2014-09-24 17:05:20 -0700157static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
158 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700159{
Florian Fainellif4589952016-08-26 12:18:33 -0700160 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellie1b91472017-01-30 09:48:41 -0800161 unsigned int i;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700162 u32 reg;
163
164 /* Clear the memory power down */
165 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
166 reg &= ~P_TXQ_PSM_VDD(port);
167 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
168
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800169 /* Enable Broadcom tags for that port if requested */
170 if (priv->brcm_tag_mask & BIT(port))
Florian Fainellib409a9e2017-09-19 10:46:48 -0700171 b53_brcm_hdr_setup(ds, port);
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800172
Florian Fainellie1b91472017-01-30 09:48:41 -0800173 /* Configure Traffic Class to QoS mapping, allow each priority to map
174 * to a different queue number
175 */
176 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
Florian Fainelli181183772017-09-03 20:27:02 -0700177 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
Florian Fainellie1b91472017-01-30 09:48:41 -0800178 reg |= i << (PRT_TO_QID_SHIFT * i);
179 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
180
Florian Fainelli9af197a2015-02-05 11:40:42 -0800181 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700182 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800183 bcm_sf2_gphy_enable_set(ds, true);
184 if (phy) {
185 /* if phy_stop() has been called before, phy
186 * will be in halted state, and phy_start()
187 * will call resume.
188 *
189 * the resume path does not configure back
190 * autoneg settings, and since we hard reset
191 * the phy manually here, we need to reset the
192 * state machine also.
193 */
194 phy->state = PHY_READY;
195 phy_init_hw(phy);
196 }
197 }
198
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700199 /* Enable MoCA port interrupts to get notified */
200 if (port == priv->moca_port)
201 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700202
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700203 /* Set per-queue pause threshold to 32 */
204 core_writel(priv, 32, CORE_TXQ_THD_PAUSE_QN_PORT(port));
205
206 /* Set ACB threshold to 24 */
207 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++) {
208 reg = acb_readl(priv, ACB_QUEUE_CFG(port *
209 SF2_NUM_EGRESS_QUEUES + i));
210 reg &= ~XOFF_THRESHOLD_MASK;
211 reg |= 24;
212 acb_writel(priv, reg, ACB_QUEUE_CFG(port *
213 SF2_NUM_EGRESS_QUEUES + i));
214 }
215
Florian Fainellif86ad772017-09-19 10:46:54 -0700216 return b53_enable_port(ds, port, phy);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700217}
218
Florian Fainellib6d045d2014-09-24 17:05:20 -0700219static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
220 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700221{
Florian Fainellif4589952016-08-26 12:18:33 -0700222 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700223 u32 off, reg;
224
Florian Fainelli96e65d72014-09-18 17:31:25 -0700225 if (priv->wol_ports_mask & (1 << port))
226 return;
227
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700228 if (port == priv->moca_port)
229 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700230
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700231 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800232 bcm_sf2_gphy_enable_set(ds, false);
233
Florian Fainelli246d7f72014-08-27 17:04:56 -0700234 if (dsa_is_cpu_port(ds, port))
235 off = CORE_IMP_CTL;
236 else
237 off = CORE_G_PCTL_PORT(port);
238
Florian Fainellif86ad772017-09-19 10:46:54 -0700239 b53_disable_port(ds, port, phy);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700240
241 /* Power down the port memory */
242 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
243 reg |= P_TXQ_PSM_VDD(port);
244 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
245}
246
Florian Fainelli450b05c2014-09-24 17:05:22 -0700247
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700248static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
249 int regnum, u16 val)
250{
251 int ret = 0;
252 u32 reg;
253
254 reg = reg_readl(priv, REG_SWITCH_CNTRL);
255 reg |= MDIO_MASTER_SEL;
256 reg_writel(priv, reg, REG_SWITCH_CNTRL);
257
258 /* Page << 8 | offset */
259 reg = 0x70;
260 reg <<= 2;
261 core_writel(priv, addr, reg);
262
263 /* Page << 8 | offset */
264 reg = 0x80 << 8 | regnum << 1;
265 reg <<= 2;
266
267 if (op)
268 ret = core_readl(priv, reg);
269 else
270 core_writel(priv, val, reg);
271
272 reg = reg_readl(priv, REG_SWITCH_CNTRL);
273 reg &= ~MDIO_MASTER_SEL;
274 reg_writel(priv, reg, REG_SWITCH_CNTRL);
275
276 return ret & 0xffff;
277}
278
279static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
280{
281 struct bcm_sf2_priv *priv = bus->priv;
282
283 /* Intercept reads from Broadcom pseudo-PHY address, else, send
284 * them to our master MDIO bus controller
285 */
286 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
287 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
288 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800289 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700290}
291
292static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
293 u16 val)
294{
295 struct bcm_sf2_priv *priv = bus->priv;
296
297 /* Intercept writes to the Broadcom pseudo-PHY address, else,
298 * send them to our master MDIO bus controller
299 */
300 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
301 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
302 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800303 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700304
305 return 0;
306}
307
Florian Fainelli246d7f72014-08-27 17:04:56 -0700308static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
309{
Florian Fainellibc0cb652018-05-10 13:17:33 -0700310 struct dsa_switch *ds = dev_id;
311 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700312
313 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
314 ~priv->irq0_mask;
315 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
316
317 return IRQ_HANDLED;
318}
319
320static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
321{
Florian Fainellibc0cb652018-05-10 13:17:33 -0700322 struct dsa_switch *ds = dev_id;
323 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700324
325 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
326 ~priv->irq1_mask;
327 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
328
Florian Fainellibc0cb652018-05-10 13:17:33 -0700329 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF)) {
330 priv->port_sts[7].link = true;
331 dsa_port_phylink_mac_change(ds, 7, true);
332 }
333 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF)) {
334 priv->port_sts[7].link = false;
335 dsa_port_phylink_mac_change(ds, 7, false);
336 }
Florian Fainelli246d7f72014-08-27 17:04:56 -0700337
338 return IRQ_HANDLED;
339}
340
Florian Fainelli33f84612014-11-25 18:08:49 -0800341static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
342{
343 unsigned int timeout = 1000;
344 u32 reg;
345
346 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
347 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
348 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
349
350 do {
351 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
352 if (!(reg & SOFTWARE_RESET))
353 break;
354
355 usleep_range(1000, 2000);
356 } while (timeout-- > 0);
357
358 if (timeout == 0)
359 return -ETIMEDOUT;
360
361 return 0;
362}
363
Florian Fainelli691c9a82015-01-20 16:42:00 -0800364static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
365{
Florian Fainellif01d5982016-08-25 15:23:41 -0700366 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800367 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700368 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800369 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800370}
371
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700372static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
373 struct device_node *dn)
374{
375 struct device_node *port;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700376 int mode;
377 unsigned int port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700378
379 priv->moca_port = -1;
380
381 for_each_available_child_of_node(dn, port) {
382 if (of_property_read_u32(port, "reg", &port_num))
383 continue;
384
385 /* Internal PHYs get assigned a specific 'phy-mode' property
386 * value: "internal" to help flag them before MDIO probing
387 * has completed, since they might be turned off at that
388 * time
389 */
390 mode = of_get_phy_mode(port);
Florian Fainellibedd00c2017-06-23 10:33:16 -0700391 if (mode < 0)
392 continue;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700393
Florian Fainellibedd00c2017-06-23 10:33:16 -0700394 if (mode == PHY_INTERFACE_MODE_INTERNAL)
395 priv->int_phy_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700396
397 if (mode == PHY_INTERFACE_MODE_MOCA)
398 priv->moca_port = port_num;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800399
400 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
401 priv->brcm_tag_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700402 }
403}
404
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700405static int bcm_sf2_mdio_register(struct dsa_switch *ds)
406{
Florian Fainellif4589952016-08-26 12:18:33 -0700407 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700408 struct device_node *dn;
409 static int index;
410 int err;
411
412 /* Find our integrated MDIO bus node */
413 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
414 priv->master_mii_bus = of_mdio_find_bus(dn);
415 if (!priv->master_mii_bus)
416 return -EPROBE_DEFER;
417
418 get_device(&priv->master_mii_bus->dev);
419 priv->master_mii_dn = dn;
420
421 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
422 if (!priv->slave_mii_bus)
423 return -ENOMEM;
424
425 priv->slave_mii_bus->priv = priv;
426 priv->slave_mii_bus->name = "sf2 slave mii";
427 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
428 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
429 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
430 index++);
431 priv->slave_mii_bus->dev.of_node = dn;
432
433 /* Include the pseudo-PHY address to divert reads towards our
434 * workaround. This is only required for 7445D0, since 7445E0
435 * disconnects the internal switch pseudo-PHY such that we can use the
436 * regular SWITCH_MDIO master controller instead.
437 *
438 * Here we flag the pseudo PHY as needing special treatment and would
439 * otherwise make all other PHY read/writes go to the master MDIO bus
440 * controller that comes with this switch backed by the "mdio-unimac"
441 * driver.
442 */
443 if (of_machine_is_compatible("brcm,bcm7445d0"))
444 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
445 else
446 priv->indir_phy_mask = 0;
447
448 ds->phys_mii_mask = priv->indir_phy_mask;
449 ds->slave_mii_bus = priv->slave_mii_bus;
450 priv->slave_mii_bus->parent = ds->dev->parent;
451 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
452
453 if (dn)
454 err = of_mdiobus_register(priv->slave_mii_bus, dn);
455 else
456 err = mdiobus_register(priv->slave_mii_bus);
457
458 if (err)
459 of_node_put(dn);
460
461 return err;
462}
463
464static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
465{
466 mdiobus_unregister(priv->slave_mii_bus);
467 if (priv->master_mii_dn)
468 of_node_put(priv->master_mii_dn);
469}
470
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700471static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
472{
Florian Fainellif4589952016-08-26 12:18:33 -0700473 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700474
475 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
476 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
477 * the REG_PHY_REVISION register layout is.
478 */
479
480 return priv->hw_params.gphy_rev;
481}
482
Florian Fainellibc0cb652018-05-10 13:17:33 -0700483static void bcm_sf2_sw_validate(struct dsa_switch *ds, int port,
484 unsigned long *supported,
485 struct phylink_link_state *state)
486{
487 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
488
489 if (!phy_interface_mode_is_rgmii(state->interface) &&
490 state->interface != PHY_INTERFACE_MODE_MII &&
491 state->interface != PHY_INTERFACE_MODE_REVMII &&
492 state->interface != PHY_INTERFACE_MODE_GMII &&
493 state->interface != PHY_INTERFACE_MODE_INTERNAL &&
494 state->interface != PHY_INTERFACE_MODE_MOCA) {
495 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
496 dev_err(ds->dev,
497 "Unsupported interface: %d\n", state->interface);
498 return;
499 }
500
501 /* Allow all the expected bits */
502 phylink_set(mask, Autoneg);
503 phylink_set_port_modes(mask);
504 phylink_set(mask, Pause);
505 phylink_set(mask, Asym_Pause);
506
507 /* With the exclusion of MII and Reverse MII, we support Gigabit,
508 * including Half duplex
509 */
510 if (state->interface != PHY_INTERFACE_MODE_MII &&
511 state->interface != PHY_INTERFACE_MODE_REVMII) {
512 phylink_set(mask, 1000baseT_Full);
513 phylink_set(mask, 1000baseT_Half);
514 }
515
516 phylink_set(mask, 10baseT_Half);
517 phylink_set(mask, 10baseT_Full);
518 phylink_set(mask, 100baseT_Half);
519 phylink_set(mask, 100baseT_Full);
520
521 bitmap_and(supported, supported, mask,
522 __ETHTOOL_LINK_MODE_MASK_NBITS);
523 bitmap_and(state->advertising, state->advertising, mask,
524 __ETHTOOL_LINK_MODE_MASK_NBITS);
525}
526
527static void bcm_sf2_sw_mac_config(struct dsa_switch *ds, int port,
528 unsigned int mode,
529 const struct phylink_link_state *state)
530{
531 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
532 u32 id_mode_dis = 0, port_mode;
533 u32 reg, offset;
534
535 if (priv->type == BCM7445_DEVICE_ID)
536 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
537 else
538 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
539
540 switch (state->interface) {
541 case PHY_INTERFACE_MODE_RGMII:
542 id_mode_dis = 1;
543 /* fallthrough */
544 case PHY_INTERFACE_MODE_RGMII_TXID:
545 port_mode = EXT_GPHY;
546 break;
547 case PHY_INTERFACE_MODE_MII:
548 port_mode = EXT_EPHY;
549 break;
550 case PHY_INTERFACE_MODE_REVMII:
551 port_mode = EXT_REVMII;
552 break;
553 default:
554 /* all other PHYs: internal and MoCA */
555 goto force_link;
556 }
557
558 /* Clear id_mode_dis bit, and the existing port mode, let
559 * RGMII_MODE_EN bet set by mac_link_{up,down}
560 */
561 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
562 reg &= ~ID_MODE_DIS;
563 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
564 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
565
566 reg |= port_mode;
567 if (id_mode_dis)
568 reg |= ID_MODE_DIS;
569
570 if (state->pause & MLO_PAUSE_TXRX_MASK) {
571 if (state->pause & MLO_PAUSE_TX)
572 reg |= TX_PAUSE_EN;
573 reg |= RX_PAUSE_EN;
574 }
575
576 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
577
578force_link:
579 /* Force link settings detected from the PHY */
580 reg = SW_OVERRIDE;
581 switch (state->speed) {
582 case SPEED_1000:
583 reg |= SPDSTS_1000 << SPEED_SHIFT;
584 break;
585 case SPEED_100:
586 reg |= SPDSTS_100 << SPEED_SHIFT;
587 break;
588 }
589
590 if (state->link)
591 reg |= LINK_STS;
592 if (state->duplex == DUPLEX_FULL)
593 reg |= DUPLX_MODE;
594
595 core_writel(priv, reg, offset);
596}
597
598static void bcm_sf2_sw_mac_link_set(struct dsa_switch *ds, int port,
599 phy_interface_t interface, bool link)
600{
601 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
602 u32 reg;
603
604 if (!phy_interface_mode_is_rgmii(interface) &&
605 interface != PHY_INTERFACE_MODE_MII &&
606 interface != PHY_INTERFACE_MODE_REVMII)
607 return;
608
609 /* If the link is down, just disable the interface to conserve power */
610 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
611 if (link)
612 reg |= RGMII_MODE_EN;
613 else
614 reg &= ~RGMII_MODE_EN;
615 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
616}
617
618static void bcm_sf2_sw_mac_link_down(struct dsa_switch *ds, int port,
619 unsigned int mode,
620 phy_interface_t interface)
621{
622 bcm_sf2_sw_mac_link_set(ds, port, interface, false);
623}
624
625static void bcm_sf2_sw_mac_link_up(struct dsa_switch *ds, int port,
626 unsigned int mode,
627 phy_interface_t interface,
628 struct phy_device *phydev)
629{
630 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
631 struct ethtool_eee *p = &priv->dev->ports[port].eee;
632
633 bcm_sf2_sw_mac_link_set(ds, port, interface, true);
634
635 if (mode == MLO_AN_PHY && phydev)
636 p->eee_enabled = b53_eee_init(ds, port, phydev);
637}
638
639static void bcm_sf2_sw_fixed_state(struct dsa_switch *ds, int port,
640 struct phylink_link_state *status)
641{
642 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
643
644 status->link = false;
645
646 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
647 * which means that we need to force the link at the port override
648 * level to get the data to flow. We do use what the interrupt handler
649 * did determine before.
650 *
651 * For the other ports, we just force the link status, since this is
652 * a fixed PHY device.
653 */
654 if (port == priv->moca_port) {
655 status->link = priv->port_sts[port].link;
656 /* For MoCA interfaces, also force a link down notification
657 * since some version of the user-space daemon (mocad) use
658 * cmd->autoneg to force the link, which messes up the PHY
659 * state machine and make it go in PHY_FORCING state instead.
660 */
661 if (!status->link)
662 netif_carrier_off(ds->ports[port].slave);
663 status->duplex = DUPLEX_FULL;
664 } else {
665 status->link = true;
666 }
667}
668
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700669static void bcm_sf2_enable_acb(struct dsa_switch *ds)
670{
671 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
672 u32 reg;
673
674 /* Enable ACB globally */
675 reg = acb_readl(priv, ACB_CONTROL);
676 reg |= (ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
677 acb_writel(priv, reg, ACB_CONTROL);
678 reg &= ~(ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
679 reg |= ACB_EN | ACB_ALGORITHM;
680 acb_writel(priv, reg, ACB_CONTROL);
681}
682
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700683static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
684{
Florian Fainellif4589952016-08-26 12:18:33 -0700685 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700686 unsigned int port;
687
Florian Fainelli691c9a82015-01-20 16:42:00 -0800688 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700689
690 /* Disable all ports physically present including the IMP
691 * port, the other ones have already been disabled during
692 * bcm_sf2_sw_setup
693 */
694 for (port = 0; port < DSA_MAX_PORTS; port++) {
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400695 if (dsa_is_user_port(ds, port) || dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700696 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700697 }
698
699 return 0;
700}
701
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700702static int bcm_sf2_sw_resume(struct dsa_switch *ds)
703{
Florian Fainellif4589952016-08-26 12:18:33 -0700704 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700705 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700706 int ret;
707
708 ret = bcm_sf2_sw_rst(priv);
709 if (ret) {
710 pr_err("%s: failed to software reset switch\n", __func__);
711 return ret;
712 }
713
Florian Fainellib0836682015-02-05 11:40:41 -0800714 if (priv->hw_params.num_gphy == 1)
715 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700716
717 for (port = 0; port < DSA_MAX_PORTS; port++) {
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400718 if (dsa_is_user_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700719 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700720 else if (dsa_is_cpu_port(ds, port))
721 bcm_sf2_imp_setup(ds, port);
722 }
723
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700724 bcm_sf2_enable_acb(ds);
725
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700726 return 0;
727}
728
Florian Fainelli96e65d72014-09-18 17:31:25 -0700729static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
730 struct ethtool_wolinfo *wol)
731{
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400732 struct net_device *p = ds->ports[port].cpu_dp->master;
Florian Fainellif4589952016-08-26 12:18:33 -0700733 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700734 struct ethtool_wolinfo pwol;
735
736 /* Get the parent device WoL settings */
737 p->ethtool_ops->get_wol(p, &pwol);
738
739 /* Advertise the parent device supported settings */
740 wol->supported = pwol.supported;
741 memset(&wol->sopass, 0, sizeof(wol->sopass));
742
743 if (pwol.wolopts & WAKE_MAGICSECURE)
744 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
745
746 if (priv->wol_ports_mask & (1 << port))
747 wol->wolopts = pwol.wolopts;
748 else
749 wol->wolopts = 0;
750}
751
752static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
753 struct ethtool_wolinfo *wol)
754{
Vivien Didelotf8b8b1c2017-10-16 11:12:18 -0400755 struct net_device *p = ds->ports[port].cpu_dp->master;
Florian Fainellif4589952016-08-26 12:18:33 -0700756 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot0abfd492017-09-20 12:28:05 -0400757 s8 cpu_port = ds->ports[port].cpu_dp->index;
Florian Fainelli96e65d72014-09-18 17:31:25 -0700758 struct ethtool_wolinfo pwol;
759
760 p->ethtool_ops->get_wol(p, &pwol);
761 if (wol->wolopts & ~pwol.supported)
762 return -EINVAL;
763
764 if (wol->wolopts)
765 priv->wol_ports_mask |= (1 << port);
766 else
767 priv->wol_ports_mask &= ~(1 << port);
768
769 /* If we have at least one port enabled, make sure the CPU port
770 * is also enabled. If the CPU port is the last one enabled, we disable
771 * it since this configuration does not make sense.
772 */
773 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
774 priv->wol_ports_mask |= (1 << cpu_port);
775 else
776 priv->wol_ports_mask &= ~(1 << cpu_port);
777
778 return p->ethtool_ops->set_wol(p, wol);
779}
780
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700781static int bcm_sf2_sw_setup(struct dsa_switch *ds)
782{
Florian Fainellif4589952016-08-26 12:18:33 -0700783 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700784 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700785
Florian Fainelli21a27742017-09-28 11:19:06 -0700786 /* Enable all valid ports and disable those unused */
Florian Fainellid9338022016-08-18 15:30:14 -0700787 for (port = 0; port < priv->hw_params.num_ports; port++) {
Florian Fainelli21a27742017-09-28 11:19:06 -0700788 /* IMP port receives special treatment */
Vivien Didelot4a5b85f2017-10-26 11:22:55 -0400789 if (dsa_is_user_port(ds, port))
Florian Fainelli21a27742017-09-28 11:19:06 -0700790 bcm_sf2_port_setup(ds, port, NULL);
791 else if (dsa_is_cpu_port(ds, port))
Florian Fainellid9338022016-08-18 15:30:14 -0700792 bcm_sf2_imp_setup(ds, port);
Florian Fainelli21a27742017-09-28 11:19:06 -0700793 else
Florian Fainellid9338022016-08-18 15:30:14 -0700794 bcm_sf2_port_disable(ds, port, NULL);
795 }
796
Florian Fainelli5c1a6ea2017-10-27 15:56:01 -0700797 b53_configure_vlan(ds);
Florian Fainelli32e47ff2017-10-11 10:57:51 -0700798 bcm_sf2_enable_acb(ds);
Florian Fainellid9338022016-08-18 15:30:14 -0700799
800 return 0;
801}
802
Florian Fainellif4589952016-08-26 12:18:33 -0700803/* The SWITCH_CORE register space is managed by b53 but operates on a page +
804 * register basis so we need to translate that into an address that the
805 * bus-glue understands.
806 */
807#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
808
809static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
810 u8 *val)
811{
812 struct bcm_sf2_priv *priv = dev->priv;
813
814 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
815
816 return 0;
817}
818
819static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
820 u16 *val)
821{
822 struct bcm_sf2_priv *priv = dev->priv;
823
824 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
825
826 return 0;
827}
828
829static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
830 u32 *val)
831{
832 struct bcm_sf2_priv *priv = dev->priv;
833
834 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
835
836 return 0;
837}
838
839static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
840 u64 *val)
841{
842 struct bcm_sf2_priv *priv = dev->priv;
843
844 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
845
846 return 0;
847}
848
849static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
850 u8 value)
851{
852 struct bcm_sf2_priv *priv = dev->priv;
853
854 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
855
856 return 0;
857}
858
859static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
860 u16 value)
861{
862 struct bcm_sf2_priv *priv = dev->priv;
863
864 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
865
866 return 0;
867}
868
869static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
870 u32 value)
871{
872 struct bcm_sf2_priv *priv = dev->priv;
873
874 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
875
876 return 0;
877}
878
879static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
880 u64 value)
881{
882 struct bcm_sf2_priv *priv = dev->priv;
883
884 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
885
886 return 0;
887}
888
Bhumika Goyal7e3108f2017-08-29 22:17:52 +0530889static const struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -0700890 .read8 = bcm_sf2_core_read8,
891 .read16 = bcm_sf2_core_read16,
892 .read32 = bcm_sf2_core_read32,
893 .read48 = bcm_sf2_core_read64,
894 .read64 = bcm_sf2_core_read64,
895 .write8 = bcm_sf2_core_write8,
896 .write16 = bcm_sf2_core_write16,
897 .write32 = bcm_sf2_core_write32,
898 .write48 = bcm_sf2_core_write64,
899 .write64 = bcm_sf2_core_write64,
900};
901
Florian Fainellia82f67a2017-01-08 14:52:08 -0800902static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli9f668162017-11-30 09:55:35 -0800903 .get_tag_protocol = b53_get_tag_protocol,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800904 .setup = bcm_sf2_sw_setup,
905 .get_strings = b53_get_strings,
906 .get_ethtool_stats = b53_get_ethtool_stats,
907 .get_sset_count = b53_get_sset_count,
Florian Fainellic7d28c92018-04-25 12:12:53 -0700908 .get_ethtool_phy_stats = b53_get_ethtool_phy_stats,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800909 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
Florian Fainellibc0cb652018-05-10 13:17:33 -0700910 .phylink_validate = bcm_sf2_sw_validate,
911 .phylink_mac_config = bcm_sf2_sw_mac_config,
912 .phylink_mac_link_down = bcm_sf2_sw_mac_link_down,
913 .phylink_mac_link_up = bcm_sf2_sw_mac_link_up,
914 .phylink_fixed_state = bcm_sf2_sw_fixed_state,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800915 .suspend = bcm_sf2_sw_suspend,
916 .resume = bcm_sf2_sw_resume,
917 .get_wol = bcm_sf2_sw_get_wol,
918 .set_wol = bcm_sf2_sw_set_wol,
919 .port_enable = bcm_sf2_port_setup,
920 .port_disable = bcm_sf2_port_disable,
Florian Fainelli22256b02017-09-19 10:46:50 -0700921 .get_mac_eee = b53_get_mac_eee,
922 .set_mac_eee = b53_set_mac_eee,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800923 .port_bridge_join = b53_br_join,
924 .port_bridge_leave = b53_br_leave,
925 .port_stp_state_set = b53_br_set_stp_state,
926 .port_fast_age = b53_br_fast_age,
927 .port_vlan_filtering = b53_vlan_filtering,
928 .port_vlan_prepare = b53_vlan_prepare,
929 .port_vlan_add = b53_vlan_add,
930 .port_vlan_del = b53_vlan_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800931 .port_fdb_dump = b53_fdb_dump,
932 .port_fdb_add = b53_fdb_add,
933 .port_fdb_del = b53_fdb_del,
Florian Fainelli73181662017-01-30 09:48:43 -0800934 .get_rxnfc = bcm_sf2_get_rxnfc,
935 .set_rxnfc = bcm_sf2_set_rxnfc,
Florian Fainelliec960de2017-01-30 12:41:43 -0800936 .port_mirror_add = b53_mirror_add,
937 .port_mirror_del = b53_mirror_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800938};
939
Florian Fainellia78e86e2017-01-20 12:36:29 -0800940struct bcm_sf2_of_data {
941 u32 type;
942 const u16 *reg_offsets;
943 unsigned int core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -0700944 unsigned int num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -0800945};
946
947/* Register offsets for the SWITCH_REG_* block */
948static const u16 bcm_sf2_7445_reg_offsets[] = {
949 [REG_SWITCH_CNTRL] = 0x00,
950 [REG_SWITCH_STATUS] = 0x04,
951 [REG_DIR_DATA_WRITE] = 0x08,
952 [REG_DIR_DATA_READ] = 0x0C,
953 [REG_SWITCH_REVISION] = 0x18,
954 [REG_PHY_REVISION] = 0x1C,
955 [REG_SPHY_CNTRL] = 0x2C,
956 [REG_RGMII_0_CNTRL] = 0x34,
957 [REG_RGMII_1_CNTRL] = 0x40,
958 [REG_RGMII_2_CNTRL] = 0x4c,
959 [REG_LED_0_CNTRL] = 0x90,
960 [REG_LED_1_CNTRL] = 0x94,
961 [REG_LED_2_CNTRL] = 0x98,
962};
963
964static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
965 .type = BCM7445_DEVICE_ID,
966 .core_reg_align = 0,
967 .reg_offsets = bcm_sf2_7445_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -0700968 .num_cfp_rules = 256,
Florian Fainellia78e86e2017-01-20 12:36:29 -0800969};
970
Florian Fainelli0fe99332017-01-20 12:36:30 -0800971static const u16 bcm_sf2_7278_reg_offsets[] = {
972 [REG_SWITCH_CNTRL] = 0x00,
973 [REG_SWITCH_STATUS] = 0x04,
974 [REG_DIR_DATA_WRITE] = 0x08,
975 [REG_DIR_DATA_READ] = 0x0c,
976 [REG_SWITCH_REVISION] = 0x10,
977 [REG_PHY_REVISION] = 0x14,
978 [REG_SPHY_CNTRL] = 0x24,
979 [REG_RGMII_0_CNTRL] = 0xe0,
980 [REG_RGMII_1_CNTRL] = 0xec,
981 [REG_RGMII_2_CNTRL] = 0xf8,
982 [REG_LED_0_CNTRL] = 0x40,
983 [REG_LED_1_CNTRL] = 0x4c,
984 [REG_LED_2_CNTRL] = 0x58,
985};
986
987static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
988 .type = BCM7278_DEVICE_ID,
989 .core_reg_align = 1,
990 .reg_offsets = bcm_sf2_7278_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -0700991 .num_cfp_rules = 128,
Florian Fainelli0fe99332017-01-20 12:36:30 -0800992};
993
Florian Fainellia78e86e2017-01-20 12:36:29 -0800994static const struct of_device_id bcm_sf2_of_match[] = {
995 { .compatible = "brcm,bcm7445-switch-v4.0",
996 .data = &bcm_sf2_7445_data
997 },
Florian Fainelli0fe99332017-01-20 12:36:30 -0800998 { .compatible = "brcm,bcm7278-switch-v4.0",
999 .data = &bcm_sf2_7278_data
1000 },
Florian Fainelli3b07d782017-12-14 17:59:40 -08001001 { .compatible = "brcm,bcm7278-switch-v4.8",
1002 .data = &bcm_sf2_7278_data
1003 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001004 { /* sentinel */ },
1005};
1006MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1007
Florian Fainellid9338022016-08-18 15:30:14 -07001008static int bcm_sf2_sw_probe(struct platform_device *pdev)
1009{
1010 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1011 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001012 const struct of_device_id *of_id = NULL;
1013 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001014 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001015 struct dsa_switch_ops *ops;
Florian Fainellid9338022016-08-18 15:30:14 -07001016 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001017 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001018 struct dsa_switch *ds;
1019 void __iomem **base;
Florian Fainelli4bd11672016-08-18 15:30:15 -07001020 struct resource *r;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001021 unsigned int i;
1022 u32 reg, rev;
1023 int ret;
1024
Florian Fainellif4589952016-08-26 12:18:33 -07001025 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1026 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001027 return -ENOMEM;
1028
Florian Fainellia4c61b92017-01-07 21:01:56 -08001029 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1030 if (!ops)
1031 return -ENOMEM;
1032
Florian Fainellif4589952016-08-26 12:18:33 -07001033 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1034 if (!dev)
1035 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001036
Florian Fainellif4589952016-08-26 12:18:33 -07001037 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1038 if (!pdata)
1039 return -ENOMEM;
1040
Florian Fainellia78e86e2017-01-20 12:36:29 -08001041 of_id = of_match_node(bcm_sf2_of_match, dn);
1042 if (!of_id || !of_id->data)
1043 return -EINVAL;
1044
1045 data = of_id->data;
1046
1047 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1048 priv->type = data->type;
1049 priv->reg_offsets = data->reg_offsets;
1050 priv->core_reg_align = data->core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001051 priv->num_cfp_rules = data->num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001052
Florian Fainellif4589952016-08-26 12:18:33 -07001053 /* Auto-detection using standard registers will not work, so
1054 * provide an indication of what kind of device we are for
1055 * b53_common to work with
1056 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001057 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001058 dev->pdata = pdata;
1059
1060 priv->dev = dev;
1061 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001062 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001063
Florian Fainelli181183772017-09-03 20:27:02 -07001064 /* Advertise the 8 egress queues */
1065 ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1066
Florian Fainellif4589952016-08-26 12:18:33 -07001067 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001068
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001069 spin_lock_init(&priv->indir_lock);
1070 mutex_init(&priv->stats_mutex);
Florian Fainelli73181662017-01-30 09:48:43 -08001071 mutex_init(&priv->cfp.lock);
1072
1073 /* CFP rule #0 cannot be used for specific classifications, flag it as
1074 * permanently used
1075 */
1076 set_bit(0, priv->cfp.used);
Florian Fainelliba0696c2017-10-20 14:39:47 -07001077 set_bit(0, priv->cfp.unique);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001078
Florian Fainellid9338022016-08-18 15:30:14 -07001079 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001080
1081 priv->irq0 = irq_of_parse_and_map(dn, 0);
1082 priv->irq1 = irq_of_parse_and_map(dn, 1);
1083
1084 base = &priv->core;
1085 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainelli4bd11672016-08-18 15:30:15 -07001086 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1087 *base = devm_ioremap_resource(&pdev->dev, r);
1088 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001089 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001090 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001091 }
1092 base++;
1093 }
1094
1095 ret = bcm_sf2_sw_rst(priv);
1096 if (ret) {
1097 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001098 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001099 }
1100
1101 ret = bcm_sf2_mdio_register(ds);
1102 if (ret) {
1103 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001104 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001105 }
1106
Florian Fainelli73181662017-01-30 09:48:43 -08001107 ret = bcm_sf2_cfp_rst(priv);
1108 if (ret) {
1109 pr_err("failed to reset CFP\n");
1110 goto out_mdio;
1111 }
1112
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001113 /* Disable all interrupts and request them */
1114 bcm_sf2_intr_disable(priv);
1115
Florian Fainelli4bd11672016-08-18 15:30:15 -07001116 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
Florian Fainellibc0cb652018-05-10 13:17:33 -07001117 "switch_0", ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001118 if (ret < 0) {
1119 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001120 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001121 }
1122
Florian Fainelli4bd11672016-08-18 15:30:15 -07001123 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
Florian Fainellibc0cb652018-05-10 13:17:33 -07001124 "switch_1", ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001125 if (ret < 0) {
1126 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001127 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001128 }
1129
1130 /* Reset the MIB counters */
1131 reg = core_readl(priv, CORE_GMNCFGCFG);
1132 reg |= RST_MIB_CNT;
1133 core_writel(priv, reg, CORE_GMNCFGCFG);
1134 reg &= ~RST_MIB_CNT;
1135 core_writel(priv, reg, CORE_GMNCFGCFG);
1136
1137 /* Get the maximum number of ports for this switch */
1138 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1139 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1140 priv->hw_params.num_ports = DSA_MAX_PORTS;
1141
1142 /* Assume a single GPHY setup if we can't read that property */
1143 if (of_property_read_u32(dn, "brcm,num-gphy",
1144 &priv->hw_params.num_gphy))
1145 priv->hw_params.num_gphy = 1;
1146
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001147 rev = reg_readl(priv, REG_SWITCH_REVISION);
1148 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1149 SWITCH_TOP_REV_MASK;
1150 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1151
1152 rev = reg_readl(priv, REG_PHY_REVISION);
1153 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1154
Florian Fainellif4589952016-08-26 12:18:33 -07001155 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001156 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001157 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001158
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001159 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1160 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1161 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1162 priv->core, priv->irq0, priv->irq1);
1163
1164 return 0;
1165
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001166out_mdio:
1167 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001168 return ret;
1169}
1170
Florian Fainellid9338022016-08-18 15:30:14 -07001171static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001172{
Florian Fainellif4589952016-08-26 12:18:33 -07001173 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001174
1175 /* Disable all ports and interrupts */
1176 priv->wol_ports_mask = 0;
Florian Fainellif4589952016-08-26 12:18:33 -07001177 bcm_sf2_sw_suspend(priv->dev->ds);
1178 dsa_unregister_switch(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001179 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001180
1181 return 0;
1182}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001183
Florian Fainelli2399d612016-10-20 09:32:19 -07001184static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1185{
1186 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1187
1188 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1189 * successful MDIO bus scan to occur. If we did turn off the GPHY
1190 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001191 *
1192 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001193 */
1194 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001195 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001196}
1197
Florian Fainellid9338022016-08-18 15:30:14 -07001198#ifdef CONFIG_PM_SLEEP
1199static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001200{
Florian Fainellid9338022016-08-18 15:30:14 -07001201 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001202 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001203
Florian Fainellif4589952016-08-26 12:18:33 -07001204 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001205}
Florian Fainellid9338022016-08-18 15:30:14 -07001206
1207static int bcm_sf2_resume(struct device *dev)
1208{
1209 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001210 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001211
Florian Fainellif4589952016-08-26 12:18:33 -07001212 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001213}
1214#endif /* CONFIG_PM_SLEEP */
1215
1216static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1217 bcm_sf2_suspend, bcm_sf2_resume);
1218
Florian Fainellid9338022016-08-18 15:30:14 -07001219
1220static struct platform_driver bcm_sf2_driver = {
1221 .probe = bcm_sf2_sw_probe,
1222 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001223 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001224 .driver = {
1225 .name = "brcm-sf2",
1226 .of_match_table = bcm_sf2_of_match,
1227 .pm = &bcm_sf2_pm_ops,
1228 },
1229};
1230module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001231
1232MODULE_AUTHOR("Broadcom Corporation");
1233MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1234MODULE_LICENSE("GPL");
1235MODULE_ALIAS("platform:brcm-sf2");