blob: 637072da3acfb4d6d4aea6c2d35057f1ec3a34f2 [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>
17#include <linux/of.h>
18#include <linux/phy.h>
19#include <linux/phy_fixed.h>
20#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>
31#include <net/switchdev.h>
Florian Fainellif4589952016-08-26 12:18:33 -070032#include <linux/platform_data/b53.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070033
34#include "bcm_sf2.h"
35#include "bcm_sf2_regs.h"
Florian Fainellif4589952016-08-26 12:18:33 -070036#include "b53/b53_priv.h"
37#include "b53/b53_regs.h"
Florian Fainelli246d7f72014-08-27 17:04:56 -070038
Andrew Lunn7b314362016-08-22 16:01:01 +020039static enum dsa_tag_protocol bcm_sf2_sw_get_tag_protocol(struct dsa_switch *ds)
40{
41 return DSA_TAG_PROTO_BRCM;
42}
43
Florian Fainellib6d045d2014-09-24 17:05:20 -070044static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
Florian Fainelli246d7f72014-08-27 17:04:56 -070045{
Florian Fainellif4589952016-08-26 12:18:33 -070046 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -070047 unsigned int i;
Florian Fainellib6d045d2014-09-24 17:05:20 -070048 u32 reg;
49
50 /* Enable the IMP Port to be in the same VLAN as the other ports
51 * on a per-port basis such that we only have Port i and IMP in
52 * the same VLAN.
53 */
54 for (i = 0; i < priv->hw_params.num_ports; i++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +020055 if (!((1 << i) & ds->enabled_port_mask))
Florian Fainellib6d045d2014-09-24 17:05:20 -070056 continue;
57
58 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
59 reg |= (1 << cpu_port);
60 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
61 }
62}
63
Florian Fainelliebb2ac42017-01-20 12:36:31 -080064static void bcm_sf2_brcm_hdr_setup(struct bcm_sf2_priv *priv, int port)
Florian Fainellib6d045d2014-09-24 17:05:20 -070065{
Florian Fainelliebb2ac42017-01-20 12:36:31 -080066 u32 reg, val;
Florian Fainelli246d7f72014-08-27 17:04:56 -070067
68 /* Resolve which bit controls the Broadcom tag */
69 switch (port) {
70 case 8:
71 val = BRCM_HDR_EN_P8;
72 break;
73 case 7:
74 val = BRCM_HDR_EN_P7;
75 break;
76 case 5:
77 val = BRCM_HDR_EN_P5;
78 break;
79 default:
80 val = 0;
81 break;
82 }
83
84 /* Enable Broadcom tags for IMP port */
85 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
86 reg |= val;
87 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
88
89 /* Enable reception Broadcom tag for CPU TX (switch RX) to
90 * allow us to tag outgoing frames
91 */
92 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
93 reg &= ~(1 << port);
94 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
95
96 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
97 * allow delivering frames to the per-port net_devices
98 */
99 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
100 reg &= ~(1 << port);
101 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800102}
103
104static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
105{
106 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
107 u32 reg, offset;
108
109 if (priv->type == BCM7445_DEVICE_ID)
110 offset = CORE_STS_OVERRIDE_IMP;
111 else
112 offset = CORE_STS_OVERRIDE_IMP2;
113
114 /* Enable the port memories */
115 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
116 reg &= ~P_TXQ_PSM_VDD(port);
117 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
118
119 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
120 reg = core_readl(priv, CORE_IMP_CTL);
121 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
122 reg &= ~(RX_DIS | TX_DIS);
123 core_writel(priv, reg, CORE_IMP_CTL);
124
125 /* Enable forwarding */
126 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
127
128 /* Enable IMP port in dumb mode */
129 reg = core_readl(priv, CORE_SWITCH_CTRL);
130 reg |= MII_DUMB_FWDG_EN;
131 core_writel(priv, reg, CORE_SWITCH_CTRL);
132
133 bcm_sf2_brcm_hdr_setup(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700134
135 /* Force link status for IMP port */
Florian Fainelli0fe99332017-01-20 12:36:30 -0800136 reg = core_readl(priv, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700137 reg |= (MII_SW_OR | LINK_STS);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800138 core_writel(priv, reg, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700139}
140
Florian Fainelli450b05c2014-09-24 17:05:22 -0700141static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
142{
Florian Fainellif4589952016-08-26 12:18:33 -0700143 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700144 u32 reg;
145
146 reg = core_readl(priv, CORE_EEE_EN_CTRL);
147 if (enable)
148 reg |= 1 << port;
149 else
150 reg &= ~(1 << port);
151 core_writel(priv, reg, CORE_EEE_EN_CTRL);
152}
153
Florian Fainellib0836682015-02-05 11:40:41 -0800154static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
155{
Florian Fainellif4589952016-08-26 12:18:33 -0700156 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -0800157 u32 reg;
158
Florian Fainelli9af197a2015-02-05 11:40:42 -0800159 reg = reg_readl(priv, REG_SPHY_CNTRL);
160 if (enable) {
161 reg |= PHY_RESET;
162 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
163 reg_writel(priv, reg, REG_SPHY_CNTRL);
164 udelay(21);
165 reg = reg_readl(priv, REG_SPHY_CNTRL);
166 reg &= ~PHY_RESET;
167 } else {
168 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
169 reg_writel(priv, reg, REG_SPHY_CNTRL);
170 mdelay(1);
171 reg |= CK25_DIS;
172 }
173 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800174
Florian Fainelli9af197a2015-02-05 11:40:42 -0800175 /* Use PHY-driven LED signaling */
176 if (!enable) {
177 reg = reg_readl(priv, REG_LED_CNTRL(0));
178 reg |= SPDLNK_SRC_SEL;
179 reg_writel(priv, reg, REG_LED_CNTRL(0));
180 }
Florian Fainellib0836682015-02-05 11:40:41 -0800181}
182
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700183static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
184 int port)
185{
186 unsigned int off;
187
188 switch (port) {
189 case 7:
190 off = P7_IRQ_OFF;
191 break;
192 case 0:
193 /* Port 0 interrupts are located on the first bank */
194 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
195 return;
196 default:
197 off = P_IRQ_OFF(port);
198 break;
199 }
200
201 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
202}
203
204static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
205 int port)
206{
207 unsigned int off;
208
209 switch (port) {
210 case 7:
211 off = P7_IRQ_OFF;
212 break;
213 case 0:
214 /* Port 0 interrupts are located on the first bank */
215 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
216 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
217 return;
218 default:
219 off = P_IRQ_OFF(port);
220 break;
221 }
222
223 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
224 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
225}
226
Florian Fainellib6d045d2014-09-24 17:05:20 -0700227static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
228 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700229{
Florian Fainellif4589952016-08-26 12:18:33 -0700230 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700231 s8 cpu_port = ds->dst[ds->index].cpu_port;
Florian Fainellie1b91472017-01-30 09:48:41 -0800232 unsigned int i;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700233 u32 reg;
234
235 /* Clear the memory power down */
236 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
237 reg &= ~P_TXQ_PSM_VDD(port);
238 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
239
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800240 /* Enable Broadcom tags for that port if requested */
241 if (priv->brcm_tag_mask & BIT(port))
242 bcm_sf2_brcm_hdr_setup(priv, port);
243
Florian Fainellie1b91472017-01-30 09:48:41 -0800244 /* Configure Traffic Class to QoS mapping, allow each priority to map
245 * to a different queue number
246 */
247 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
248 for (i = 0; i < 8; i++)
249 reg |= i << (PRT_TO_QID_SHIFT * i);
250 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
251
Florian Fainelli246d7f72014-08-27 17:04:56 -0700252 /* Clear the Rx and Tx disable bits and set to no spanning tree */
253 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
254
Florian Fainelli9af197a2015-02-05 11:40:42 -0800255 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700256 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800257 bcm_sf2_gphy_enable_set(ds, true);
258 if (phy) {
259 /* if phy_stop() has been called before, phy
260 * will be in halted state, and phy_start()
261 * will call resume.
262 *
263 * the resume path does not configure back
264 * autoneg settings, and since we hard reset
265 * the phy manually here, we need to reset the
266 * state machine also.
267 */
268 phy->state = PHY_READY;
269 phy_init_hw(phy);
270 }
271 }
272
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700273 /* Enable MoCA port interrupts to get notified */
274 if (port == priv->moca_port)
275 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700276
Florian Fainelli12f460f2015-02-24 13:15:34 -0800277 /* Set this port, and only this one to be in the default VLAN,
278 * if member of a bridge, restore its membership prior to
279 * bringing down this port.
280 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700281 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
282 reg &= ~PORT_VLAN_CTRL_MASK;
283 reg |= (1 << port);
Florian Fainelli02154922016-09-10 12:39:03 -0700284 reg |= priv->dev->ports[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700285 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700286
287 bcm_sf2_imp_vlan_setup(ds, cpu_port);
288
Florian Fainelli450b05c2014-09-24 17:05:22 -0700289 /* If EEE was enabled, restore it */
290 if (priv->port_sts[port].eee.eee_enabled)
291 bcm_sf2_eee_enable_set(ds, port, true);
292
Florian Fainellib6d045d2014-09-24 17:05:20 -0700293 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700294}
295
Florian Fainellib6d045d2014-09-24 17:05:20 -0700296static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
297 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700298{
Florian Fainellif4589952016-08-26 12:18:33 -0700299 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700300 u32 off, reg;
301
Florian Fainelli96e65d72014-09-18 17:31:25 -0700302 if (priv->wol_ports_mask & (1 << port))
303 return;
304
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700305 if (port == priv->moca_port)
306 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700307
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700308 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800309 bcm_sf2_gphy_enable_set(ds, false);
310
Florian Fainelli246d7f72014-08-27 17:04:56 -0700311 if (dsa_is_cpu_port(ds, port))
312 off = CORE_IMP_CTL;
313 else
314 off = CORE_G_PCTL_PORT(port);
315
316 reg = core_readl(priv, off);
317 reg |= RX_DIS | TX_DIS;
318 core_writel(priv, reg, off);
319
320 /* Power down the port memory */
321 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
322 reg |= P_TXQ_PSM_VDD(port);
323 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
324}
325
Florian Fainelli450b05c2014-09-24 17:05:22 -0700326/* Returns 0 if EEE was not enabled, or 1 otherwise
327 */
328static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
329 struct phy_device *phy)
330{
Florian Fainellif4589952016-08-26 12:18:33 -0700331 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700332 struct ethtool_eee *p = &priv->port_sts[port].eee;
333 int ret;
334
335 p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
336
337 ret = phy_init_eee(phy, 0);
338 if (ret)
339 return 0;
340
341 bcm_sf2_eee_enable_set(ds, port, true);
342
343 return 1;
344}
345
346static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
347 struct ethtool_eee *e)
348{
Florian Fainellif4589952016-08-26 12:18:33 -0700349 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700350 struct ethtool_eee *p = &priv->port_sts[port].eee;
351 u32 reg;
352
353 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
354 e->eee_enabled = p->eee_enabled;
355 e->eee_active = !!(reg & (1 << port));
356
357 return 0;
358}
359
360static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
361 struct phy_device *phydev,
362 struct ethtool_eee *e)
363{
Florian Fainellif4589952016-08-26 12:18:33 -0700364 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700365 struct ethtool_eee *p = &priv->port_sts[port].eee;
366
367 p->eee_enabled = e->eee_enabled;
368
369 if (!p->eee_enabled) {
370 bcm_sf2_eee_enable_set(ds, port, false);
371 } else {
372 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
373 if (!p->eee_enabled)
374 return -EOPNOTSUPP;
375 }
376
377 return 0;
378}
379
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700380static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
381 int regnum, u16 val)
382{
383 int ret = 0;
384 u32 reg;
385
386 reg = reg_readl(priv, REG_SWITCH_CNTRL);
387 reg |= MDIO_MASTER_SEL;
388 reg_writel(priv, reg, REG_SWITCH_CNTRL);
389
390 /* Page << 8 | offset */
391 reg = 0x70;
392 reg <<= 2;
393 core_writel(priv, addr, reg);
394
395 /* Page << 8 | offset */
396 reg = 0x80 << 8 | regnum << 1;
397 reg <<= 2;
398
399 if (op)
400 ret = core_readl(priv, reg);
401 else
402 core_writel(priv, val, reg);
403
404 reg = reg_readl(priv, REG_SWITCH_CNTRL);
405 reg &= ~MDIO_MASTER_SEL;
406 reg_writel(priv, reg, REG_SWITCH_CNTRL);
407
408 return ret & 0xffff;
409}
410
411static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
412{
413 struct bcm_sf2_priv *priv = bus->priv;
414
415 /* Intercept reads from Broadcom pseudo-PHY address, else, send
416 * them to our master MDIO bus controller
417 */
418 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
419 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
420 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800421 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700422}
423
424static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
425 u16 val)
426{
427 struct bcm_sf2_priv *priv = bus->priv;
428
429 /* Intercept writes to the Broadcom pseudo-PHY address, else,
430 * send them to our master MDIO bus controller
431 */
432 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
433 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
434 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800435 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700436
437 return 0;
438}
439
Florian Fainelli246d7f72014-08-27 17:04:56 -0700440static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
441{
442 struct bcm_sf2_priv *priv = dev_id;
443
444 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
445 ~priv->irq0_mask;
446 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
447
448 return IRQ_HANDLED;
449}
450
451static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
452{
453 struct bcm_sf2_priv *priv = dev_id;
454
455 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
456 ~priv->irq1_mask;
457 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
458
459 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
460 priv->port_sts[7].link = 1;
461 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
462 priv->port_sts[7].link = 0;
463
464 return IRQ_HANDLED;
465}
466
Florian Fainelli33f84612014-11-25 18:08:49 -0800467static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
468{
469 unsigned int timeout = 1000;
470 u32 reg;
471
472 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
473 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
474 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
475
476 do {
477 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
478 if (!(reg & SOFTWARE_RESET))
479 break;
480
481 usleep_range(1000, 2000);
482 } while (timeout-- > 0);
483
484 if (timeout == 0)
485 return -ETIMEDOUT;
486
487 return 0;
488}
489
Florian Fainelli691c9a82015-01-20 16:42:00 -0800490static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
491{
Florian Fainellif01d5982016-08-25 15:23:41 -0700492 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800493 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700494 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800495 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800496}
497
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700498static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
499 struct device_node *dn)
500{
501 struct device_node *port;
502 const char *phy_mode_str;
503 int mode;
504 unsigned int port_num;
505 int ret;
506
507 priv->moca_port = -1;
508
509 for_each_available_child_of_node(dn, port) {
510 if (of_property_read_u32(port, "reg", &port_num))
511 continue;
512
513 /* Internal PHYs get assigned a specific 'phy-mode' property
514 * value: "internal" to help flag them before MDIO probing
515 * has completed, since they might be turned off at that
516 * time
517 */
518 mode = of_get_phy_mode(port);
519 if (mode < 0) {
520 ret = of_property_read_string(port, "phy-mode",
521 &phy_mode_str);
522 if (ret < 0)
523 continue;
524
525 if (!strcasecmp(phy_mode_str, "internal"))
526 priv->int_phy_mask |= 1 << port_num;
527 }
528
529 if (mode == PHY_INTERFACE_MODE_MOCA)
530 priv->moca_port = port_num;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800531
532 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
533 priv->brcm_tag_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700534 }
535}
536
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700537static int bcm_sf2_mdio_register(struct dsa_switch *ds)
538{
Florian Fainellif4589952016-08-26 12:18:33 -0700539 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700540 struct device_node *dn;
541 static int index;
542 int err;
543
544 /* Find our integrated MDIO bus node */
545 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
546 priv->master_mii_bus = of_mdio_find_bus(dn);
547 if (!priv->master_mii_bus)
548 return -EPROBE_DEFER;
549
550 get_device(&priv->master_mii_bus->dev);
551 priv->master_mii_dn = dn;
552
553 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
554 if (!priv->slave_mii_bus)
555 return -ENOMEM;
556
557 priv->slave_mii_bus->priv = priv;
558 priv->slave_mii_bus->name = "sf2 slave mii";
559 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
560 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
561 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
562 index++);
563 priv->slave_mii_bus->dev.of_node = dn;
564
565 /* Include the pseudo-PHY address to divert reads towards our
566 * workaround. This is only required for 7445D0, since 7445E0
567 * disconnects the internal switch pseudo-PHY such that we can use the
568 * regular SWITCH_MDIO master controller instead.
569 *
570 * Here we flag the pseudo PHY as needing special treatment and would
571 * otherwise make all other PHY read/writes go to the master MDIO bus
572 * controller that comes with this switch backed by the "mdio-unimac"
573 * driver.
574 */
575 if (of_machine_is_compatible("brcm,bcm7445d0"))
576 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
577 else
578 priv->indir_phy_mask = 0;
579
580 ds->phys_mii_mask = priv->indir_phy_mask;
581 ds->slave_mii_bus = priv->slave_mii_bus;
582 priv->slave_mii_bus->parent = ds->dev->parent;
583 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
584
585 if (dn)
586 err = of_mdiobus_register(priv->slave_mii_bus, dn);
587 else
588 err = mdiobus_register(priv->slave_mii_bus);
589
590 if (err)
591 of_node_put(dn);
592
593 return err;
594}
595
596static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
597{
598 mdiobus_unregister(priv->slave_mii_bus);
599 if (priv->master_mii_dn)
600 of_node_put(priv->master_mii_dn);
601}
602
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700603static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
604{
Florian Fainellif4589952016-08-26 12:18:33 -0700605 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700606
607 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
608 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
609 * the REG_PHY_REVISION register layout is.
610 */
611
612 return priv->hw_params.gphy_rev;
613}
614
Florian Fainelli246d7f72014-08-27 17:04:56 -0700615static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
616 struct phy_device *phydev)
617{
Florian Fainellif4589952016-08-26 12:18:33 -0700618 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli76da8702016-11-22 11:40:58 -0800619 struct ethtool_eee *p = &priv->port_sts[port].eee;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700620 u32 id_mode_dis = 0, port_mode;
621 const char *str = NULL;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800622 u32 reg, offset;
623
624 if (priv->type == BCM7445_DEVICE_ID)
625 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
626 else
627 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700628
629 switch (phydev->interface) {
630 case PHY_INTERFACE_MODE_RGMII:
631 str = "RGMII (no delay)";
632 id_mode_dis = 1;
633 case PHY_INTERFACE_MODE_RGMII_TXID:
634 if (!str)
635 str = "RGMII (TX delay)";
636 port_mode = EXT_GPHY;
637 break;
638 case PHY_INTERFACE_MODE_MII:
639 str = "MII";
640 port_mode = EXT_EPHY;
641 break;
642 case PHY_INTERFACE_MODE_REVMII:
643 str = "Reverse MII";
644 port_mode = EXT_REVMII;
645 break;
646 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700647 /* All other PHYs: internal and MoCA */
648 goto force_link;
649 }
650
651 /* If the link is down, just disable the interface to conserve power */
652 if (!phydev->link) {
653 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
654 reg &= ~RGMII_MODE_EN;
655 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700656 goto force_link;
657 }
658
659 /* Clear id_mode_dis bit, and the existing port mode, but
660 * make sure we enable the RGMII block for data to pass
661 */
662 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
663 reg &= ~ID_MODE_DIS;
664 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
665 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
666
667 reg |= port_mode | RGMII_MODE_EN;
668 if (id_mode_dis)
669 reg |= ID_MODE_DIS;
670
671 if (phydev->pause) {
672 if (phydev->asym_pause)
673 reg |= TX_PAUSE_EN;
674 reg |= RX_PAUSE_EN;
675 }
676
677 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
678
679 pr_info("Port %d configured for %s\n", port, str);
680
681force_link:
682 /* Force link settings detected from the PHY */
683 reg = SW_OVERRIDE;
684 switch (phydev->speed) {
685 case SPEED_1000:
686 reg |= SPDSTS_1000 << SPEED_SHIFT;
687 break;
688 case SPEED_100:
689 reg |= SPDSTS_100 << SPEED_SHIFT;
690 break;
691 }
692
693 if (phydev->link)
694 reg |= LINK_STS;
695 if (phydev->duplex == DUPLEX_FULL)
696 reg |= DUPLX_MODE;
697
Florian Fainelli0fe99332017-01-20 12:36:30 -0800698 core_writel(priv, reg, offset);
Florian Fainelli76da8702016-11-22 11:40:58 -0800699
700 if (!phydev->is_pseudo_fixed_link)
701 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700702}
703
704static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
705 struct fixed_phy_status *status)
706{
Florian Fainellif4589952016-08-26 12:18:33 -0700707 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800708 u32 duplex, pause, offset;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700709 u32 reg;
710
Florian Fainelli0fe99332017-01-20 12:36:30 -0800711 if (priv->type == BCM7445_DEVICE_ID)
712 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
713 else
714 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
715
Florian Fainelli246d7f72014-08-27 17:04:56 -0700716 duplex = core_readl(priv, CORE_DUPSTS);
717 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700718
719 status->link = 0;
720
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700721 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700722 * which means that we need to force the link at the port override
723 * level to get the data to flow. We do use what the interrupt handler
724 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -0800725 *
726 * For the other ports, we just force the link status, since this is
727 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700728 */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700729 if (port == priv->moca_port) {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700730 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -0700731 /* For MoCA interfaces, also force a link down notification
732 * since some version of the user-space daemon (mocad) use
733 * cmd->autoneg to force the link, which messes up the PHY
734 * state machine and make it go in PHY_FORCING state instead.
735 */
736 if (!status->link)
Andrew Lunnc8b09802016-06-04 21:16:57 +0200737 netif_carrier_off(ds->ports[port].netdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700738 status->duplex = 1;
739 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -0800740 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700741 status->duplex = !!(duplex & (1 << port));
742 }
743
Florian Fainelli0fe99332017-01-20 12:36:30 -0800744 reg = core_readl(priv, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800745 reg |= SW_OVERRIDE;
746 if (status->link)
747 reg |= LINK_STS;
748 else
749 reg &= ~LINK_STS;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800750 core_writel(priv, reg, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800751
Florian Fainelli246d7f72014-08-27 17:04:56 -0700752 if ((pause & (1 << port)) &&
753 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
754 status->asym_pause = 1;
755 status->pause = 1;
756 }
757
758 if (pause & (1 << port))
759 status->pause = 1;
760}
761
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700762static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
763{
Florian Fainellif4589952016-08-26 12:18:33 -0700764 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700765 unsigned int port;
766
Florian Fainelli691c9a82015-01-20 16:42:00 -0800767 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700768
769 /* Disable all ports physically present including the IMP
770 * port, the other ones have already been disabled during
771 * bcm_sf2_sw_setup
772 */
773 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200774 if ((1 << port) & ds->enabled_port_mask ||
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700775 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700776 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700777 }
778
779 return 0;
780}
781
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700782static int bcm_sf2_sw_resume(struct dsa_switch *ds)
783{
Florian Fainellif4589952016-08-26 12:18:33 -0700784 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700785 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700786 int ret;
787
788 ret = bcm_sf2_sw_rst(priv);
789 if (ret) {
790 pr_err("%s: failed to software reset switch\n", __func__);
791 return ret;
792 }
793
Florian Fainellib0836682015-02-05 11:40:41 -0800794 if (priv->hw_params.num_gphy == 1)
795 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700796
797 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200798 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700799 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700800 else if (dsa_is_cpu_port(ds, port))
801 bcm_sf2_imp_setup(ds, port);
802 }
803
804 return 0;
805}
806
Florian Fainelli96e65d72014-09-18 17:31:25 -0700807static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
808 struct ethtool_wolinfo *wol)
809{
810 struct net_device *p = ds->dst[ds->index].master_netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700811 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700812 struct ethtool_wolinfo pwol;
813
814 /* Get the parent device WoL settings */
815 p->ethtool_ops->get_wol(p, &pwol);
816
817 /* Advertise the parent device supported settings */
818 wol->supported = pwol.supported;
819 memset(&wol->sopass, 0, sizeof(wol->sopass));
820
821 if (pwol.wolopts & WAKE_MAGICSECURE)
822 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
823
824 if (priv->wol_ports_mask & (1 << port))
825 wol->wolopts = pwol.wolopts;
826 else
827 wol->wolopts = 0;
828}
829
830static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
831 struct ethtool_wolinfo *wol)
832{
833 struct net_device *p = ds->dst[ds->index].master_netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700834 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700835 s8 cpu_port = ds->dst[ds->index].cpu_port;
836 struct ethtool_wolinfo pwol;
837
838 p->ethtool_ops->get_wol(p, &pwol);
839 if (wol->wolopts & ~pwol.supported)
840 return -EINVAL;
841
842 if (wol->wolopts)
843 priv->wol_ports_mask |= (1 << port);
844 else
845 priv->wol_ports_mask &= ~(1 << port);
846
847 /* If we have at least one port enabled, make sure the CPU port
848 * is also enabled. If the CPU port is the last one enabled, we disable
849 * it since this configuration does not make sense.
850 */
851 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
852 priv->wol_ports_mask |= (1 << cpu_port);
853 else
854 priv->wol_ports_mask &= ~(1 << cpu_port);
855
856 return p->ethtool_ops->set_wol(p, wol);
857}
858
Florian Fainellide0b9d32016-08-26 12:18:34 -0700859static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
Florian Fainelli9c57a772016-06-09 17:42:08 -0700860{
Florian Fainellide0b9d32016-08-26 12:18:34 -0700861 unsigned int timeout = 10;
862 u32 reg;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700863
Florian Fainellide0b9d32016-08-26 12:18:34 -0700864 do {
865 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
866 if (!(reg & ARLA_VTBL_STDN))
867 return 0;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700868
Florian Fainellide0b9d32016-08-26 12:18:34 -0700869 usleep_range(1000, 2000);
870 } while (timeout--);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700871
Florian Fainellide0b9d32016-08-26 12:18:34 -0700872 return -ETIMEDOUT;
873}
Florian Fainelli9c57a772016-06-09 17:42:08 -0700874
Florian Fainellide0b9d32016-08-26 12:18:34 -0700875static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
876{
877 core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
878
879 return bcm_sf2_vlan_op_wait(priv);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700880}
881
882static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
883{
Florian Fainellif4589952016-08-26 12:18:33 -0700884 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700885 unsigned int port;
886
887 /* Clear all VLANs */
888 bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
889
890 for (port = 0; port < priv->hw_params.num_ports; port++) {
891 if (!((1 << port) & ds->enabled_port_mask))
892 continue;
893
894 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
895 }
896}
897
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700898static int bcm_sf2_sw_setup(struct dsa_switch *ds)
899{
Florian Fainellif4589952016-08-26 12:18:33 -0700900 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700901 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700902
903 /* Enable all valid ports and disable those unused */
904 for (port = 0; port < priv->hw_params.num_ports; port++) {
905 /* IMP port receives special treatment */
906 if ((1 << port) & ds->enabled_port_mask)
907 bcm_sf2_port_setup(ds, port, NULL);
908 else if (dsa_is_cpu_port(ds, port))
909 bcm_sf2_imp_setup(ds, port);
910 else
911 bcm_sf2_port_disable(ds, port, NULL);
912 }
913
914 bcm_sf2_sw_configure_vlan(ds);
915
916 return 0;
917}
918
Florian Fainellif4589952016-08-26 12:18:33 -0700919/* The SWITCH_CORE register space is managed by b53 but operates on a page +
920 * register basis so we need to translate that into an address that the
921 * bus-glue understands.
922 */
923#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
924
925static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
926 u8 *val)
927{
928 struct bcm_sf2_priv *priv = dev->priv;
929
930 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
931
932 return 0;
933}
934
935static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
936 u16 *val)
937{
938 struct bcm_sf2_priv *priv = dev->priv;
939
940 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
941
942 return 0;
943}
944
945static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
946 u32 *val)
947{
948 struct bcm_sf2_priv *priv = dev->priv;
949
950 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
951
952 return 0;
953}
954
955static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
956 u64 *val)
957{
958 struct bcm_sf2_priv *priv = dev->priv;
959
960 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
961
962 return 0;
963}
964
965static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
966 u8 value)
967{
968 struct bcm_sf2_priv *priv = dev->priv;
969
970 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
971
972 return 0;
973}
974
975static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
976 u16 value)
977{
978 struct bcm_sf2_priv *priv = dev->priv;
979
980 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
981
982 return 0;
983}
984
985static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
986 u32 value)
987{
988 struct bcm_sf2_priv *priv = dev->priv;
989
990 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
991
992 return 0;
993}
994
995static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
996 u64 value)
997{
998 struct bcm_sf2_priv *priv = dev->priv;
999
1000 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1001
1002 return 0;
1003}
1004
Wei Yongjun0e26e5b2016-09-15 02:24:13 +00001005static struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -07001006 .read8 = bcm_sf2_core_read8,
1007 .read16 = bcm_sf2_core_read16,
1008 .read32 = bcm_sf2_core_read32,
1009 .read48 = bcm_sf2_core_read64,
1010 .read64 = bcm_sf2_core_read64,
1011 .write8 = bcm_sf2_core_write8,
1012 .write16 = bcm_sf2_core_write16,
1013 .write32 = bcm_sf2_core_write32,
1014 .write48 = bcm_sf2_core_write64,
1015 .write64 = bcm_sf2_core_write64,
1016};
1017
Florian Fainellia82f67a2017-01-08 14:52:08 -08001018static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli73095cb2017-01-08 14:52:06 -08001019 .get_tag_protocol = bcm_sf2_sw_get_tag_protocol,
1020 .setup = bcm_sf2_sw_setup,
1021 .get_strings = b53_get_strings,
1022 .get_ethtool_stats = b53_get_ethtool_stats,
1023 .get_sset_count = b53_get_sset_count,
1024 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
1025 .adjust_link = bcm_sf2_sw_adjust_link,
1026 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
1027 .suspend = bcm_sf2_sw_suspend,
1028 .resume = bcm_sf2_sw_resume,
1029 .get_wol = bcm_sf2_sw_get_wol,
1030 .set_wol = bcm_sf2_sw_set_wol,
1031 .port_enable = bcm_sf2_port_setup,
1032 .port_disable = bcm_sf2_port_disable,
1033 .get_eee = bcm_sf2_sw_get_eee,
1034 .set_eee = bcm_sf2_sw_set_eee,
1035 .port_bridge_join = b53_br_join,
1036 .port_bridge_leave = b53_br_leave,
1037 .port_stp_state_set = b53_br_set_stp_state,
1038 .port_fast_age = b53_br_fast_age,
1039 .port_vlan_filtering = b53_vlan_filtering,
1040 .port_vlan_prepare = b53_vlan_prepare,
1041 .port_vlan_add = b53_vlan_add,
1042 .port_vlan_del = b53_vlan_del,
1043 .port_vlan_dump = b53_vlan_dump,
1044 .port_fdb_prepare = b53_fdb_prepare,
1045 .port_fdb_dump = b53_fdb_dump,
1046 .port_fdb_add = b53_fdb_add,
1047 .port_fdb_del = b53_fdb_del,
1048};
1049
Florian Fainellia78e86e2017-01-20 12:36:29 -08001050struct bcm_sf2_of_data {
1051 u32 type;
1052 const u16 *reg_offsets;
1053 unsigned int core_reg_align;
1054};
1055
1056/* Register offsets for the SWITCH_REG_* block */
1057static const u16 bcm_sf2_7445_reg_offsets[] = {
1058 [REG_SWITCH_CNTRL] = 0x00,
1059 [REG_SWITCH_STATUS] = 0x04,
1060 [REG_DIR_DATA_WRITE] = 0x08,
1061 [REG_DIR_DATA_READ] = 0x0C,
1062 [REG_SWITCH_REVISION] = 0x18,
1063 [REG_PHY_REVISION] = 0x1C,
1064 [REG_SPHY_CNTRL] = 0x2C,
1065 [REG_RGMII_0_CNTRL] = 0x34,
1066 [REG_RGMII_1_CNTRL] = 0x40,
1067 [REG_RGMII_2_CNTRL] = 0x4c,
1068 [REG_LED_0_CNTRL] = 0x90,
1069 [REG_LED_1_CNTRL] = 0x94,
1070 [REG_LED_2_CNTRL] = 0x98,
1071};
1072
1073static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1074 .type = BCM7445_DEVICE_ID,
1075 .core_reg_align = 0,
1076 .reg_offsets = bcm_sf2_7445_reg_offsets,
1077};
1078
Florian Fainelli0fe99332017-01-20 12:36:30 -08001079static const u16 bcm_sf2_7278_reg_offsets[] = {
1080 [REG_SWITCH_CNTRL] = 0x00,
1081 [REG_SWITCH_STATUS] = 0x04,
1082 [REG_DIR_DATA_WRITE] = 0x08,
1083 [REG_DIR_DATA_READ] = 0x0c,
1084 [REG_SWITCH_REVISION] = 0x10,
1085 [REG_PHY_REVISION] = 0x14,
1086 [REG_SPHY_CNTRL] = 0x24,
1087 [REG_RGMII_0_CNTRL] = 0xe0,
1088 [REG_RGMII_1_CNTRL] = 0xec,
1089 [REG_RGMII_2_CNTRL] = 0xf8,
1090 [REG_LED_0_CNTRL] = 0x40,
1091 [REG_LED_1_CNTRL] = 0x4c,
1092 [REG_LED_2_CNTRL] = 0x58,
1093};
1094
1095static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1096 .type = BCM7278_DEVICE_ID,
1097 .core_reg_align = 1,
1098 .reg_offsets = bcm_sf2_7278_reg_offsets,
1099};
1100
Florian Fainellia78e86e2017-01-20 12:36:29 -08001101static const struct of_device_id bcm_sf2_of_match[] = {
1102 { .compatible = "brcm,bcm7445-switch-v4.0",
1103 .data = &bcm_sf2_7445_data
1104 },
Florian Fainelli0fe99332017-01-20 12:36:30 -08001105 { .compatible = "brcm,bcm7278-switch-v4.0",
1106 .data = &bcm_sf2_7278_data
1107 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001108 { /* sentinel */ },
1109};
1110MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1111
Florian Fainellid9338022016-08-18 15:30:14 -07001112static int bcm_sf2_sw_probe(struct platform_device *pdev)
1113{
1114 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1115 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001116 const struct of_device_id *of_id = NULL;
1117 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001118 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001119 struct dsa_switch_ops *ops;
Florian Fainellid9338022016-08-18 15:30:14 -07001120 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001121 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001122 struct dsa_switch *ds;
1123 void __iomem **base;
Florian Fainelli4bd11672016-08-18 15:30:15 -07001124 struct resource *r;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001125 unsigned int i;
1126 u32 reg, rev;
1127 int ret;
1128
Florian Fainellif4589952016-08-26 12:18:33 -07001129 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1130 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001131 return -ENOMEM;
1132
Florian Fainellia4c61b92017-01-07 21:01:56 -08001133 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1134 if (!ops)
1135 return -ENOMEM;
1136
Florian Fainellif4589952016-08-26 12:18:33 -07001137 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1138 if (!dev)
1139 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001140
Florian Fainellif4589952016-08-26 12:18:33 -07001141 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1142 if (!pdata)
1143 return -ENOMEM;
1144
Florian Fainellia78e86e2017-01-20 12:36:29 -08001145 of_id = of_match_node(bcm_sf2_of_match, dn);
1146 if (!of_id || !of_id->data)
1147 return -EINVAL;
1148
1149 data = of_id->data;
1150
1151 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1152 priv->type = data->type;
1153 priv->reg_offsets = data->reg_offsets;
1154 priv->core_reg_align = data->core_reg_align;
1155
Florian Fainellif4589952016-08-26 12:18:33 -07001156 /* Auto-detection using standard registers will not work, so
1157 * provide an indication of what kind of device we are for
1158 * b53_common to work with
1159 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001160 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001161 dev->pdata = pdata;
1162
1163 priv->dev = dev;
1164 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001165 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001166
1167 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001168
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001169 spin_lock_init(&priv->indir_lock);
1170 mutex_init(&priv->stats_mutex);
1171
Florian Fainellid9338022016-08-18 15:30:14 -07001172 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001173
1174 priv->irq0 = irq_of_parse_and_map(dn, 0);
1175 priv->irq1 = irq_of_parse_and_map(dn, 1);
1176
1177 base = &priv->core;
1178 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainelli4bd11672016-08-18 15:30:15 -07001179 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1180 *base = devm_ioremap_resource(&pdev->dev, r);
1181 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001182 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001183 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001184 }
1185 base++;
1186 }
1187
1188 ret = bcm_sf2_sw_rst(priv);
1189 if (ret) {
1190 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001191 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001192 }
1193
1194 ret = bcm_sf2_mdio_register(ds);
1195 if (ret) {
1196 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001197 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001198 }
1199
1200 /* Disable all interrupts and request them */
1201 bcm_sf2_intr_disable(priv);
1202
Florian Fainelli4bd11672016-08-18 15:30:15 -07001203 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1204 "switch_0", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001205 if (ret < 0) {
1206 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001207 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001208 }
1209
Florian Fainelli4bd11672016-08-18 15:30:15 -07001210 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1211 "switch_1", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001212 if (ret < 0) {
1213 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001214 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001215 }
1216
1217 /* Reset the MIB counters */
1218 reg = core_readl(priv, CORE_GMNCFGCFG);
1219 reg |= RST_MIB_CNT;
1220 core_writel(priv, reg, CORE_GMNCFGCFG);
1221 reg &= ~RST_MIB_CNT;
1222 core_writel(priv, reg, CORE_GMNCFGCFG);
1223
1224 /* Get the maximum number of ports for this switch */
1225 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1226 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1227 priv->hw_params.num_ports = DSA_MAX_PORTS;
1228
1229 /* Assume a single GPHY setup if we can't read that property */
1230 if (of_property_read_u32(dn, "brcm,num-gphy",
1231 &priv->hw_params.num_gphy))
1232 priv->hw_params.num_gphy = 1;
1233
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001234 rev = reg_readl(priv, REG_SWITCH_REVISION);
1235 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1236 SWITCH_TOP_REV_MASK;
1237 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1238
1239 rev = reg_readl(priv, REG_PHY_REVISION);
1240 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1241
Florian Fainellif4589952016-08-26 12:18:33 -07001242 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001243 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001244 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001245
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001246 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1247 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1248 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1249 priv->core, priv->irq0, priv->irq1);
1250
1251 return 0;
1252
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001253out_mdio:
1254 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001255 return ret;
1256}
1257
Florian Fainellid9338022016-08-18 15:30:14 -07001258static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001259{
Florian Fainellif4589952016-08-26 12:18:33 -07001260 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001261
1262 /* Disable all ports and interrupts */
1263 priv->wol_ports_mask = 0;
Florian Fainellif4589952016-08-26 12:18:33 -07001264 bcm_sf2_sw_suspend(priv->dev->ds);
1265 dsa_unregister_switch(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001266 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001267
1268 return 0;
1269}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001270
Florian Fainelli2399d612016-10-20 09:32:19 -07001271static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1272{
1273 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1274
1275 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1276 * successful MDIO bus scan to occur. If we did turn off the GPHY
1277 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001278 *
1279 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001280 */
1281 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001282 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001283}
1284
Florian Fainellid9338022016-08-18 15:30:14 -07001285#ifdef CONFIG_PM_SLEEP
1286static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001287{
Florian Fainellid9338022016-08-18 15:30:14 -07001288 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001289 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001290
Florian Fainellif4589952016-08-26 12:18:33 -07001291 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001292}
Florian Fainellid9338022016-08-18 15:30:14 -07001293
1294static int bcm_sf2_resume(struct device *dev)
1295{
1296 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001297 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001298
Florian Fainellif4589952016-08-26 12:18:33 -07001299 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001300}
1301#endif /* CONFIG_PM_SLEEP */
1302
1303static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1304 bcm_sf2_suspend, bcm_sf2_resume);
1305
Florian Fainellid9338022016-08-18 15:30:14 -07001306
1307static struct platform_driver bcm_sf2_driver = {
1308 .probe = bcm_sf2_sw_probe,
1309 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001310 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001311 .driver = {
1312 .name = "brcm-sf2",
1313 .of_match_table = bcm_sf2_of_match,
1314 .pm = &bcm_sf2_pm_ops,
1315 },
1316};
1317module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001318
1319MODULE_AUTHOR("Broadcom Corporation");
1320MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1321MODULE_LICENSE("GPL");
1322MODULE_ALIAS("platform:brcm-sf2");