blob: d6625783703f2a75d63db5e1e7daca631705c532 [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 Fainelli246d7f72014-08-27 17:04:56 -070032
33#include "bcm_sf2.h"
34#include "bcm_sf2_regs.h"
35
36/* String, offset, and register size in bytes if different from 4 bytes */
37static const struct bcm_sf2_hw_stats bcm_sf2_mib[] = {
38 { "TxOctets", 0x000, 8 },
39 { "TxDropPkts", 0x020 },
40 { "TxQPKTQ0", 0x030 },
41 { "TxBroadcastPkts", 0x040 },
42 { "TxMulticastPkts", 0x050 },
43 { "TxUnicastPKts", 0x060 },
44 { "TxCollisions", 0x070 },
45 { "TxSingleCollision", 0x080 },
46 { "TxMultipleCollision", 0x090 },
47 { "TxDeferredCollision", 0x0a0 },
48 { "TxLateCollision", 0x0b0 },
49 { "TxExcessiveCollision", 0x0c0 },
50 { "TxFrameInDisc", 0x0d0 },
51 { "TxPausePkts", 0x0e0 },
52 { "TxQPKTQ1", 0x0f0 },
53 { "TxQPKTQ2", 0x100 },
54 { "TxQPKTQ3", 0x110 },
55 { "TxQPKTQ4", 0x120 },
56 { "TxQPKTQ5", 0x130 },
57 { "RxOctets", 0x140, 8 },
58 { "RxUndersizePkts", 0x160 },
59 { "RxPausePkts", 0x170 },
60 { "RxPkts64Octets", 0x180 },
61 { "RxPkts65to127Octets", 0x190 },
62 { "RxPkts128to255Octets", 0x1a0 },
63 { "RxPkts256to511Octets", 0x1b0 },
64 { "RxPkts512to1023Octets", 0x1c0 },
65 { "RxPkts1024toMaxPktsOctets", 0x1d0 },
66 { "RxOversizePkts", 0x1e0 },
67 { "RxJabbers", 0x1f0 },
68 { "RxAlignmentErrors", 0x200 },
69 { "RxFCSErrors", 0x210 },
70 { "RxGoodOctets", 0x220, 8 },
71 { "RxDropPkts", 0x240 },
72 { "RxUnicastPkts", 0x250 },
73 { "RxMulticastPkts", 0x260 },
74 { "RxBroadcastPkts", 0x270 },
75 { "RxSAChanges", 0x280 },
76 { "RxFragments", 0x290 },
77 { "RxJumboPkt", 0x2a0 },
78 { "RxSymblErr", 0x2b0 },
79 { "InRangeErrCount", 0x2c0 },
80 { "OutRangeErrCount", 0x2d0 },
81 { "EEELpiEvent", 0x2e0 },
82 { "EEELpiDuration", 0x2f0 },
83 { "RxDiscard", 0x300, 8 },
84 { "TxQPKTQ6", 0x320 },
85 { "TxQPKTQ7", 0x330 },
86 { "TxPkts64Octets", 0x340 },
87 { "TxPkts65to127Octets", 0x350 },
88 { "TxPkts128to255Octets", 0x360 },
89 { "TxPkts256to511Ocets", 0x370 },
90 { "TxPkts512to1023Ocets", 0x380 },
91 { "TxPkts1024toMaxPktOcets", 0x390 },
92};
93
94#define BCM_SF2_STATS_SIZE ARRAY_SIZE(bcm_sf2_mib)
95
96static void bcm_sf2_sw_get_strings(struct dsa_switch *ds,
97 int port, uint8_t *data)
98{
99 unsigned int i;
100
101 for (i = 0; i < BCM_SF2_STATS_SIZE; i++)
102 memcpy(data + i * ETH_GSTRING_LEN,
103 bcm_sf2_mib[i].string, ETH_GSTRING_LEN);
104}
105
106static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds,
107 int port, uint64_t *data)
108{
109 struct bcm_sf2_priv *priv = ds_to_priv(ds);
110 const struct bcm_sf2_hw_stats *s;
111 unsigned int i;
112 u64 val = 0;
113 u32 offset;
114
115 mutex_lock(&priv->stats_mutex);
116
117 /* Now fetch the per-port counters */
118 for (i = 0; i < BCM_SF2_STATS_SIZE; i++) {
119 s = &bcm_sf2_mib[i];
120
121 /* Do a latched 64-bit read if needed */
122 offset = s->reg + CORE_P_MIB_OFFSET(port);
123 if (s->sizeof_stat == 8)
124 val = core_readq(priv, offset);
125 else
126 val = core_readl(priv, offset);
127
128 data[i] = (u64)val;
129 }
130
131 mutex_unlock(&priv->stats_mutex);
132}
133
134static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds)
135{
136 return BCM_SF2_STATS_SIZE;
137}
138
Vivien Didelot0209d142016-04-17 13:23:55 -0400139static const char *bcm_sf2_sw_drv_probe(struct device *dsa_dev,
140 struct device *host_dev, int sw_addr,
141 void **_priv)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700142{
Andrew Lunn7543a6d2016-04-13 02:40:40 +0200143 struct bcm_sf2_priv *priv;
144
145 priv = devm_kzalloc(dsa_dev, sizeof(*priv), GFP_KERNEL);
146 if (!priv)
147 return NULL;
148 *_priv = priv;
149
Florian Fainelli246d7f72014-08-27 17:04:56 -0700150 return "Broadcom Starfighter 2";
151}
152
Florian Fainellib6d045d2014-09-24 17:05:20 -0700153static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700154{
155 struct bcm_sf2_priv *priv = ds_to_priv(ds);
156 unsigned int i;
Florian Fainellib6d045d2014-09-24 17:05:20 -0700157 u32 reg;
158
159 /* Enable the IMP Port to be in the same VLAN as the other ports
160 * on a per-port basis such that we only have Port i and IMP in
161 * the same VLAN.
162 */
163 for (i = 0; i < priv->hw_params.num_ports; i++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200164 if (!((1 << i) & ds->enabled_port_mask))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700165 continue;
166
167 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
168 reg |= (1 << cpu_port);
169 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
170 }
171}
172
173static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
174{
175 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700176 u32 reg, val;
177
178 /* Enable the port memories */
179 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
180 reg &= ~P_TXQ_PSM_VDD(port);
181 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
182
183 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
184 reg = core_readl(priv, CORE_IMP_CTL);
185 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
186 reg &= ~(RX_DIS | TX_DIS);
187 core_writel(priv, reg, CORE_IMP_CTL);
188
189 /* Enable forwarding */
190 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
191
192 /* Enable IMP port in dumb mode */
193 reg = core_readl(priv, CORE_SWITCH_CTRL);
194 reg |= MII_DUMB_FWDG_EN;
195 core_writel(priv, reg, CORE_SWITCH_CTRL);
196
197 /* Resolve which bit controls the Broadcom tag */
198 switch (port) {
199 case 8:
200 val = BRCM_HDR_EN_P8;
201 break;
202 case 7:
203 val = BRCM_HDR_EN_P7;
204 break;
205 case 5:
206 val = BRCM_HDR_EN_P5;
207 break;
208 default:
209 val = 0;
210 break;
211 }
212
213 /* Enable Broadcom tags for IMP port */
214 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
215 reg |= val;
216 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
217
218 /* Enable reception Broadcom tag for CPU TX (switch RX) to
219 * allow us to tag outgoing frames
220 */
221 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
222 reg &= ~(1 << port);
223 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
224
225 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
226 * allow delivering frames to the per-port net_devices
227 */
228 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
229 reg &= ~(1 << port);
230 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
231
232 /* Force link status for IMP port */
233 reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
234 reg |= (MII_SW_OR | LINK_STS);
235 core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700236}
237
Florian Fainelli450b05c2014-09-24 17:05:22 -0700238static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
239{
240 struct bcm_sf2_priv *priv = ds_to_priv(ds);
241 u32 reg;
242
243 reg = core_readl(priv, CORE_EEE_EN_CTRL);
244 if (enable)
245 reg |= 1 << port;
246 else
247 reg &= ~(1 << port);
248 core_writel(priv, reg, CORE_EEE_EN_CTRL);
249}
250
Florian Fainellib0836682015-02-05 11:40:41 -0800251static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
252{
253 struct bcm_sf2_priv *priv = ds_to_priv(ds);
254 u32 reg;
255
Florian Fainelli9af197a2015-02-05 11:40:42 -0800256 reg = reg_readl(priv, REG_SPHY_CNTRL);
257 if (enable) {
258 reg |= PHY_RESET;
259 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
260 reg_writel(priv, reg, REG_SPHY_CNTRL);
261 udelay(21);
262 reg = reg_readl(priv, REG_SPHY_CNTRL);
263 reg &= ~PHY_RESET;
264 } else {
265 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
266 reg_writel(priv, reg, REG_SPHY_CNTRL);
267 mdelay(1);
268 reg |= CK25_DIS;
269 }
270 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800271
Florian Fainelli9af197a2015-02-05 11:40:42 -0800272 /* Use PHY-driven LED signaling */
273 if (!enable) {
274 reg = reg_readl(priv, REG_LED_CNTRL(0));
275 reg |= SPDLNK_SRC_SEL;
276 reg_writel(priv, reg, REG_LED_CNTRL(0));
277 }
Florian Fainellib0836682015-02-05 11:40:41 -0800278}
279
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700280static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
281 int port)
282{
283 unsigned int off;
284
285 switch (port) {
286 case 7:
287 off = P7_IRQ_OFF;
288 break;
289 case 0:
290 /* Port 0 interrupts are located on the first bank */
291 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
292 return;
293 default:
294 off = P_IRQ_OFF(port);
295 break;
296 }
297
298 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
299}
300
301static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
302 int port)
303{
304 unsigned int off;
305
306 switch (port) {
307 case 7:
308 off = P7_IRQ_OFF;
309 break;
310 case 0:
311 /* Port 0 interrupts are located on the first bank */
312 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
313 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
314 return;
315 default:
316 off = P_IRQ_OFF(port);
317 break;
318 }
319
320 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
321 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
322}
323
Florian Fainellib6d045d2014-09-24 17:05:20 -0700324static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
325 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700326{
327 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700328 s8 cpu_port = ds->dst[ds->index].cpu_port;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700329 u32 reg;
330
331 /* Clear the memory power down */
332 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
333 reg &= ~P_TXQ_PSM_VDD(port);
334 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
335
336 /* Clear the Rx and Tx disable bits and set to no spanning tree */
337 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
338
Florian Fainelli9af197a2015-02-05 11:40:42 -0800339 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700340 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800341 bcm_sf2_gphy_enable_set(ds, true);
342 if (phy) {
343 /* if phy_stop() has been called before, phy
344 * will be in halted state, and phy_start()
345 * will call resume.
346 *
347 * the resume path does not configure back
348 * autoneg settings, and since we hard reset
349 * the phy manually here, we need to reset the
350 * state machine also.
351 */
352 phy->state = PHY_READY;
353 phy_init_hw(phy);
354 }
355 }
356
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700357 /* Enable MoCA port interrupts to get notified */
358 if (port == priv->moca_port)
359 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700360
Florian Fainelli12f460f2015-02-24 13:15:34 -0800361 /* Set this port, and only this one to be in the default VLAN,
362 * if member of a bridge, restore its membership prior to
363 * bringing down this port.
364 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700365 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
366 reg &= ~PORT_VLAN_CTRL_MASK;
367 reg |= (1 << port);
Florian Fainelli12f460f2015-02-24 13:15:34 -0800368 reg |= priv->port_sts[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700369 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700370
371 bcm_sf2_imp_vlan_setup(ds, cpu_port);
372
Florian Fainelli450b05c2014-09-24 17:05:22 -0700373 /* If EEE was enabled, restore it */
374 if (priv->port_sts[port].eee.eee_enabled)
375 bcm_sf2_eee_enable_set(ds, port, true);
376
Florian Fainellib6d045d2014-09-24 17:05:20 -0700377 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700378}
379
Florian Fainellib6d045d2014-09-24 17:05:20 -0700380static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
381 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700382{
383 struct bcm_sf2_priv *priv = ds_to_priv(ds);
384 u32 off, reg;
385
Florian Fainelli96e65d72014-09-18 17:31:25 -0700386 if (priv->wol_ports_mask & (1 << port))
387 return;
388
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700389 if (port == priv->moca_port)
390 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700391
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700392 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800393 bcm_sf2_gphy_enable_set(ds, false);
394
Florian Fainelli246d7f72014-08-27 17:04:56 -0700395 if (dsa_is_cpu_port(ds, port))
396 off = CORE_IMP_CTL;
397 else
398 off = CORE_G_PCTL_PORT(port);
399
400 reg = core_readl(priv, off);
401 reg |= RX_DIS | TX_DIS;
402 core_writel(priv, reg, off);
403
404 /* Power down the port memory */
405 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
406 reg |= P_TXQ_PSM_VDD(port);
407 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
408}
409
Florian Fainelli450b05c2014-09-24 17:05:22 -0700410/* Returns 0 if EEE was not enabled, or 1 otherwise
411 */
412static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
413 struct phy_device *phy)
414{
415 struct bcm_sf2_priv *priv = ds_to_priv(ds);
416 struct ethtool_eee *p = &priv->port_sts[port].eee;
417 int ret;
418
419 p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
420
421 ret = phy_init_eee(phy, 0);
422 if (ret)
423 return 0;
424
425 bcm_sf2_eee_enable_set(ds, port, true);
426
427 return 1;
428}
429
430static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
431 struct ethtool_eee *e)
432{
433 struct bcm_sf2_priv *priv = ds_to_priv(ds);
434 struct ethtool_eee *p = &priv->port_sts[port].eee;
435 u32 reg;
436
437 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
438 e->eee_enabled = p->eee_enabled;
439 e->eee_active = !!(reg & (1 << port));
440
441 return 0;
442}
443
444static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
445 struct phy_device *phydev,
446 struct ethtool_eee *e)
447{
448 struct bcm_sf2_priv *priv = ds_to_priv(ds);
449 struct ethtool_eee *p = &priv->port_sts[port].eee;
450
451 p->eee_enabled = e->eee_enabled;
452
453 if (!p->eee_enabled) {
454 bcm_sf2_eee_enable_set(ds, port, false);
455 } else {
456 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
457 if (!p->eee_enabled)
458 return -EOPNOTSUPP;
459 }
460
461 return 0;
462}
463
Florian Fainelli12f460f2015-02-24 13:15:34 -0800464/* Fast-ageing of ARL entries for a given port, equivalent to an ARL
465 * flush for that port.
466 */
467static int bcm_sf2_sw_fast_age_port(struct dsa_switch *ds, int port)
468{
469 struct bcm_sf2_priv *priv = ds_to_priv(ds);
470 unsigned int timeout = 1000;
471 u32 reg;
472
473 core_writel(priv, port, CORE_FAST_AGE_PORT);
474
475 reg = core_readl(priv, CORE_FAST_AGE_CTRL);
Florian Fainelli39797a22015-09-05 13:07:27 -0700476 reg |= EN_AGE_PORT | EN_AGE_DYNAMIC | FAST_AGE_STR_DONE;
Florian Fainelli12f460f2015-02-24 13:15:34 -0800477 core_writel(priv, reg, CORE_FAST_AGE_CTRL);
478
479 do {
480 reg = core_readl(priv, CORE_FAST_AGE_CTRL);
481 if (!(reg & FAST_AGE_STR_DONE))
482 break;
483
484 cpu_relax();
485 } while (timeout--);
486
487 if (!timeout)
488 return -ETIMEDOUT;
489
Florian Fainelli39797a22015-09-05 13:07:27 -0700490 core_writel(priv, 0, CORE_FAST_AGE_CTRL);
491
Florian Fainelli12f460f2015-02-24 13:15:34 -0800492 return 0;
493}
494
495static int bcm_sf2_sw_br_join(struct dsa_switch *ds, int port,
Vivien Didelota6692752016-02-12 12:09:39 -0500496 struct net_device *bridge)
Florian Fainelli12f460f2015-02-24 13:15:34 -0800497{
498 struct bcm_sf2_priv *priv = ds_to_priv(ds);
499 unsigned int i;
500 u32 reg, p_ctl;
501
Vivien Didelota6692752016-02-12 12:09:39 -0500502 priv->port_sts[port].bridge_dev = bridge;
Florian Fainelli12f460f2015-02-24 13:15:34 -0800503 p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
504
505 for (i = 0; i < priv->hw_params.num_ports; i++) {
Vivien Didelota6692752016-02-12 12:09:39 -0500506 if (priv->port_sts[i].bridge_dev != bridge)
Florian Fainelli12f460f2015-02-24 13:15:34 -0800507 continue;
508
509 /* Add this local port to the remote port VLAN control
510 * membership and update the remote port bitmask
511 */
512 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
513 reg |= 1 << port;
514 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
515 priv->port_sts[i].vlan_ctl_mask = reg;
516
517 p_ctl |= 1 << i;
518 }
519
520 /* Configure the local port VLAN control membership to include
521 * remote ports and update the local port bitmask
522 */
523 core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
524 priv->port_sts[port].vlan_ctl_mask = p_ctl;
525
526 return 0;
527}
528
Vivien Didelot16bfa702016-03-13 16:21:33 -0400529static void bcm_sf2_sw_br_leave(struct dsa_switch *ds, int port)
Florian Fainelli12f460f2015-02-24 13:15:34 -0800530{
531 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Vivien Didelota6692752016-02-12 12:09:39 -0500532 struct net_device *bridge = priv->port_sts[port].bridge_dev;
Florian Fainelli12f460f2015-02-24 13:15:34 -0800533 unsigned int i;
534 u32 reg, p_ctl;
535
536 p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
537
538 for (i = 0; i < priv->hw_params.num_ports; i++) {
539 /* Don't touch the remaining ports */
Vivien Didelota6692752016-02-12 12:09:39 -0500540 if (priv->port_sts[i].bridge_dev != bridge)
Florian Fainelli12f460f2015-02-24 13:15:34 -0800541 continue;
542
543 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
544 reg &= ~(1 << port);
545 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
546 priv->port_sts[port].vlan_ctl_mask = reg;
547
548 /* Prevent self removal to preserve isolation */
549 if (port != i)
550 p_ctl &= ~(1 << i);
551 }
552
553 core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
554 priv->port_sts[port].vlan_ctl_mask = p_ctl;
Vivien Didelota6692752016-02-12 12:09:39 -0500555 priv->port_sts[port].bridge_dev = NULL;
Florian Fainelli12f460f2015-02-24 13:15:34 -0800556}
557
Vivien Didelot43c44a92016-04-06 11:55:03 -0400558static void bcm_sf2_sw_br_set_stp_state(struct dsa_switch *ds, int port,
559 u8 state)
Florian Fainelli12f460f2015-02-24 13:15:34 -0800560{
561 struct bcm_sf2_priv *priv = ds_to_priv(ds);
562 u8 hw_state, cur_hw_state;
Florian Fainelli12f460f2015-02-24 13:15:34 -0800563 u32 reg;
564
565 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
Florian Fainelli39797a22015-09-05 13:07:27 -0700566 cur_hw_state = reg & (G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
Florian Fainelli12f460f2015-02-24 13:15:34 -0800567
568 switch (state) {
569 case BR_STATE_DISABLED:
570 hw_state = G_MISTP_DIS_STATE;
571 break;
572 case BR_STATE_LISTENING:
573 hw_state = G_MISTP_LISTEN_STATE;
574 break;
575 case BR_STATE_LEARNING:
576 hw_state = G_MISTP_LEARN_STATE;
577 break;
578 case BR_STATE_FORWARDING:
579 hw_state = G_MISTP_FWD_STATE;
580 break;
581 case BR_STATE_BLOCKING:
582 hw_state = G_MISTP_BLOCK_STATE;
583 break;
584 default:
585 pr_err("%s: invalid STP state: %d\n", __func__, state);
Vivien Didelot43c44a92016-04-06 11:55:03 -0400586 return;
Florian Fainelli12f460f2015-02-24 13:15:34 -0800587 }
588
589 /* Fast-age ARL entries if we are moving a port from Learning or
Florian Fainelli39797a22015-09-05 13:07:27 -0700590 * Forwarding (cur_hw_state) state to Disabled, Blocking or Listening
591 * state (hw_state)
Florian Fainelli12f460f2015-02-24 13:15:34 -0800592 */
593 if (cur_hw_state != hw_state) {
Florian Fainelli39797a22015-09-05 13:07:27 -0700594 if (cur_hw_state >= G_MISTP_LEARN_STATE &&
595 hw_state <= G_MISTP_LISTEN_STATE) {
Vivien Didelot43c44a92016-04-06 11:55:03 -0400596 if (bcm_sf2_sw_fast_age_port(ds, port)) {
Florian Fainelli12f460f2015-02-24 13:15:34 -0800597 pr_err("%s: fast-ageing failed\n", __func__);
Vivien Didelot43c44a92016-04-06 11:55:03 -0400598 return;
Florian Fainelli12f460f2015-02-24 13:15:34 -0800599 }
600 }
601 }
602
603 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
604 reg &= ~(G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
605 reg |= hw_state;
606 core_writel(priv, reg, CORE_G_PCTL_PORT(port));
Florian Fainelli12f460f2015-02-24 13:15:34 -0800607}
608
Florian Fainelli680060d2015-10-23 11:38:07 -0700609/* Address Resolution Logic routines */
610static int bcm_sf2_arl_op_wait(struct bcm_sf2_priv *priv)
611{
612 unsigned int timeout = 10;
613 u32 reg;
614
615 do {
616 reg = core_readl(priv, CORE_ARLA_RWCTL);
617 if (!(reg & ARL_STRTDN))
618 return 0;
619
620 usleep_range(1000, 2000);
621 } while (timeout--);
622
623 return -ETIMEDOUT;
624}
625
626static int bcm_sf2_arl_rw_op(struct bcm_sf2_priv *priv, unsigned int op)
627{
628 u32 cmd;
629
630 if (op > ARL_RW)
631 return -EINVAL;
632
633 cmd = core_readl(priv, CORE_ARLA_RWCTL);
634 cmd &= ~IVL_SVL_SELECT;
635 cmd |= ARL_STRTDN;
636 if (op)
637 cmd |= ARL_RW;
638 else
639 cmd &= ~ARL_RW;
640 core_writel(priv, cmd, CORE_ARLA_RWCTL);
641
642 return bcm_sf2_arl_op_wait(priv);
643}
644
645static int bcm_sf2_arl_read(struct bcm_sf2_priv *priv, u64 mac,
646 u16 vid, struct bcm_sf2_arl_entry *ent, u8 *idx,
647 bool is_valid)
648{
649 unsigned int i;
650 int ret;
651
652 ret = bcm_sf2_arl_op_wait(priv);
653 if (ret)
654 return ret;
655
656 /* Read the 4 bins */
657 for (i = 0; i < 4; i++) {
658 u64 mac_vid;
659 u32 fwd_entry;
660
661 mac_vid = core_readq(priv, CORE_ARLA_MACVID_ENTRY(i));
662 fwd_entry = core_readl(priv, CORE_ARLA_FWD_ENTRY(i));
663 bcm_sf2_arl_to_entry(ent, mac_vid, fwd_entry);
664
665 if (ent->is_valid && is_valid) {
666 *idx = i;
667 return 0;
668 }
669
670 /* This is the MAC we just deleted */
671 if (!is_valid && (mac_vid & mac))
672 return 0;
673 }
674
675 return -ENOENT;
676}
677
678static int bcm_sf2_arl_op(struct bcm_sf2_priv *priv, int op, int port,
679 const unsigned char *addr, u16 vid, bool is_valid)
680{
681 struct bcm_sf2_arl_entry ent;
682 u32 fwd_entry;
683 u64 mac, mac_vid = 0;
684 u8 idx = 0;
685 int ret;
686
687 /* Convert the array into a 64-bit MAC */
688 mac = bcm_sf2_mac_to_u64(addr);
689
690 /* Perform a read for the given MAC and VID */
691 core_writeq(priv, mac, CORE_ARLA_MAC);
692 core_writel(priv, vid, CORE_ARLA_VID);
693
694 /* Issue a read operation for this MAC */
695 ret = bcm_sf2_arl_rw_op(priv, 1);
696 if (ret)
697 return ret;
698
699 ret = bcm_sf2_arl_read(priv, mac, vid, &ent, &idx, is_valid);
700 /* If this is a read, just finish now */
701 if (op)
702 return ret;
703
704 /* We could not find a matching MAC, so reset to a new entry */
705 if (ret) {
706 fwd_entry = 0;
707 idx = 0;
708 }
709
710 memset(&ent, 0, sizeof(ent));
711 ent.port = port;
712 ent.is_valid = is_valid;
713 ent.vid = vid;
714 ent.is_static = true;
715 memcpy(ent.mac, addr, ETH_ALEN);
716 bcm_sf2_arl_from_entry(&mac_vid, &fwd_entry, &ent);
717
718 core_writeq(priv, mac_vid, CORE_ARLA_MACVID_ENTRY(idx));
719 core_writel(priv, fwd_entry, CORE_ARLA_FWD_ENTRY(idx));
720
721 ret = bcm_sf2_arl_rw_op(priv, 0);
722 if (ret)
723 return ret;
724
725 /* Re-read the entry to check */
726 return bcm_sf2_arl_read(priv, mac, vid, &ent, &idx, is_valid);
727}
728
729static int bcm_sf2_sw_fdb_prepare(struct dsa_switch *ds, int port,
730 const struct switchdev_obj_port_fdb *fdb,
731 struct switchdev_trans *trans)
732{
733 /* We do not need to do anything specific here yet */
734 return 0;
735}
736
Vivien Didelot8497aa62016-04-06 11:55:04 -0400737static void bcm_sf2_sw_fdb_add(struct dsa_switch *ds, int port,
738 const struct switchdev_obj_port_fdb *fdb,
739 struct switchdev_trans *trans)
Florian Fainelli680060d2015-10-23 11:38:07 -0700740{
741 struct bcm_sf2_priv *priv = ds_to_priv(ds);
742
Vivien Didelot8497aa62016-04-06 11:55:04 -0400743 if (bcm_sf2_arl_op(priv, 0, port, fdb->addr, fdb->vid, true))
744 pr_err("%s: failed to add MAC address\n", __func__);
Florian Fainelli680060d2015-10-23 11:38:07 -0700745}
746
747static int bcm_sf2_sw_fdb_del(struct dsa_switch *ds, int port,
748 const struct switchdev_obj_port_fdb *fdb)
749{
750 struct bcm_sf2_priv *priv = ds_to_priv(ds);
751
752 return bcm_sf2_arl_op(priv, 0, port, fdb->addr, fdb->vid, false);
753}
754
755static int bcm_sf2_arl_search_wait(struct bcm_sf2_priv *priv)
756{
757 unsigned timeout = 1000;
758 u32 reg;
759
760 do {
761 reg = core_readl(priv, CORE_ARLA_SRCH_CTL);
762 if (!(reg & ARLA_SRCH_STDN))
763 return 0;
764
765 if (reg & ARLA_SRCH_VLID)
766 return 0;
767
768 usleep_range(1000, 2000);
769 } while (timeout--);
770
771 return -ETIMEDOUT;
772}
773
774static void bcm_sf2_arl_search_rd(struct bcm_sf2_priv *priv, u8 idx,
775 struct bcm_sf2_arl_entry *ent)
776{
777 u64 mac_vid;
778 u32 fwd_entry;
779
780 mac_vid = core_readq(priv, CORE_ARLA_SRCH_RSLT_MACVID(idx));
781 fwd_entry = core_readl(priv, CORE_ARLA_SRCH_RSLT(idx));
782 bcm_sf2_arl_to_entry(ent, mac_vid, fwd_entry);
783}
784
785static int bcm_sf2_sw_fdb_copy(struct net_device *dev, int port,
786 const struct bcm_sf2_arl_entry *ent,
787 struct switchdev_obj_port_fdb *fdb,
788 int (*cb)(struct switchdev_obj *obj))
789{
790 if (!ent->is_valid)
791 return 0;
792
793 if (port != ent->port)
794 return 0;
795
796 ether_addr_copy(fdb->addr, ent->mac);
797 fdb->vid = ent->vid;
798 fdb->ndm_state = ent->is_static ? NUD_NOARP : NUD_REACHABLE;
799
800 return cb(&fdb->obj);
801}
802
803static int bcm_sf2_sw_fdb_dump(struct dsa_switch *ds, int port,
804 struct switchdev_obj_port_fdb *fdb,
805 int (*cb)(struct switchdev_obj *obj))
806{
807 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Andrew Lunnc8b09802016-06-04 21:16:57 +0200808 struct net_device *dev = ds->ports[port].netdev;
Florian Fainelli680060d2015-10-23 11:38:07 -0700809 struct bcm_sf2_arl_entry results[2];
810 unsigned int count = 0;
811 int ret;
812
813 /* Start search operation */
814 core_writel(priv, ARLA_SRCH_STDN, CORE_ARLA_SRCH_CTL);
815
816 do {
817 ret = bcm_sf2_arl_search_wait(priv);
818 if (ret)
819 return ret;
820
821 /* Read both entries, then return their values back */
822 bcm_sf2_arl_search_rd(priv, 0, &results[0]);
823 ret = bcm_sf2_sw_fdb_copy(dev, port, &results[0], fdb, cb);
824 if (ret)
825 return ret;
826
827 bcm_sf2_arl_search_rd(priv, 1, &results[1]);
828 ret = bcm_sf2_sw_fdb_copy(dev, port, &results[1], fdb, cb);
829 if (ret)
830 return ret;
831
832 if (!results[0].is_valid && !results[1].is_valid)
833 break;
834
835 } while (count++ < CORE_ARLA_NUM_ENTRIES);
836
837 return 0;
838}
839
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700840static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
841 int regnum, u16 val)
842{
843 int ret = 0;
844 u32 reg;
845
846 reg = reg_readl(priv, REG_SWITCH_CNTRL);
847 reg |= MDIO_MASTER_SEL;
848 reg_writel(priv, reg, REG_SWITCH_CNTRL);
849
850 /* Page << 8 | offset */
851 reg = 0x70;
852 reg <<= 2;
853 core_writel(priv, addr, reg);
854
855 /* Page << 8 | offset */
856 reg = 0x80 << 8 | regnum << 1;
857 reg <<= 2;
858
859 if (op)
860 ret = core_readl(priv, reg);
861 else
862 core_writel(priv, val, reg);
863
864 reg = reg_readl(priv, REG_SWITCH_CNTRL);
865 reg &= ~MDIO_MASTER_SEL;
866 reg_writel(priv, reg, REG_SWITCH_CNTRL);
867
868 return ret & 0xffff;
869}
870
871static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
872{
873 struct bcm_sf2_priv *priv = bus->priv;
874
875 /* Intercept reads from Broadcom pseudo-PHY address, else, send
876 * them to our master MDIO bus controller
877 */
878 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
879 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
880 else
881 return mdiobus_read(priv->master_mii_bus, addr, regnum);
882}
883
884static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
885 u16 val)
886{
887 struct bcm_sf2_priv *priv = bus->priv;
888
889 /* Intercept writes to the Broadcom pseudo-PHY address, else,
890 * send them to our master MDIO bus controller
891 */
892 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
893 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
894 else
895 mdiobus_write(priv->master_mii_bus, addr, regnum, val);
896
897 return 0;
898}
899
Florian Fainelli246d7f72014-08-27 17:04:56 -0700900static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
901{
902 struct bcm_sf2_priv *priv = dev_id;
903
904 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
905 ~priv->irq0_mask;
906 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
907
908 return IRQ_HANDLED;
909}
910
911static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
912{
913 struct bcm_sf2_priv *priv = dev_id;
914
915 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
916 ~priv->irq1_mask;
917 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
918
919 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
920 priv->port_sts[7].link = 1;
921 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
922 priv->port_sts[7].link = 0;
923
924 return IRQ_HANDLED;
925}
926
Florian Fainelli33f84612014-11-25 18:08:49 -0800927static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
928{
929 unsigned int timeout = 1000;
930 u32 reg;
931
932 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
933 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
934 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
935
936 do {
937 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
938 if (!(reg & SOFTWARE_RESET))
939 break;
940
941 usleep_range(1000, 2000);
942 } while (timeout-- > 0);
943
944 if (timeout == 0)
945 return -ETIMEDOUT;
946
947 return 0;
948}
949
Florian Fainelli691c9a82015-01-20 16:42:00 -0800950static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
951{
952 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
953 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
954 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
955 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
956 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
957 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
958}
959
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700960static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
961 struct device_node *dn)
962{
963 struct device_node *port;
964 const char *phy_mode_str;
965 int mode;
966 unsigned int port_num;
967 int ret;
968
969 priv->moca_port = -1;
970
971 for_each_available_child_of_node(dn, port) {
972 if (of_property_read_u32(port, "reg", &port_num))
973 continue;
974
975 /* Internal PHYs get assigned a specific 'phy-mode' property
976 * value: "internal" to help flag them before MDIO probing
977 * has completed, since they might be turned off at that
978 * time
979 */
980 mode = of_get_phy_mode(port);
981 if (mode < 0) {
982 ret = of_property_read_string(port, "phy-mode",
983 &phy_mode_str);
984 if (ret < 0)
985 continue;
986
987 if (!strcasecmp(phy_mode_str, "internal"))
988 priv->int_phy_mask |= 1 << port_num;
989 }
990
991 if (mode == PHY_INTERFACE_MODE_MOCA)
992 priv->moca_port = port_num;
993 }
994}
995
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700996static int bcm_sf2_mdio_register(struct dsa_switch *ds)
997{
998 struct bcm_sf2_priv *priv = ds_to_priv(ds);
999 struct device_node *dn;
1000 static int index;
1001 int err;
1002
1003 /* Find our integrated MDIO bus node */
1004 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
1005 priv->master_mii_bus = of_mdio_find_bus(dn);
1006 if (!priv->master_mii_bus)
1007 return -EPROBE_DEFER;
1008
1009 get_device(&priv->master_mii_bus->dev);
1010 priv->master_mii_dn = dn;
1011
1012 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
1013 if (!priv->slave_mii_bus)
1014 return -ENOMEM;
1015
1016 priv->slave_mii_bus->priv = priv;
1017 priv->slave_mii_bus->name = "sf2 slave mii";
1018 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
1019 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
1020 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
1021 index++);
1022 priv->slave_mii_bus->dev.of_node = dn;
1023
1024 /* Include the pseudo-PHY address to divert reads towards our
1025 * workaround. This is only required for 7445D0, since 7445E0
1026 * disconnects the internal switch pseudo-PHY such that we can use the
1027 * regular SWITCH_MDIO master controller instead.
1028 *
1029 * Here we flag the pseudo PHY as needing special treatment and would
1030 * otherwise make all other PHY read/writes go to the master MDIO bus
1031 * controller that comes with this switch backed by the "mdio-unimac"
1032 * driver.
1033 */
1034 if (of_machine_is_compatible("brcm,bcm7445d0"))
1035 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
1036 else
1037 priv->indir_phy_mask = 0;
1038
1039 ds->phys_mii_mask = priv->indir_phy_mask;
1040 ds->slave_mii_bus = priv->slave_mii_bus;
1041 priv->slave_mii_bus->parent = ds->dev->parent;
1042 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
1043
1044 if (dn)
1045 err = of_mdiobus_register(priv->slave_mii_bus, dn);
1046 else
1047 err = mdiobus_register(priv->slave_mii_bus);
1048
1049 if (err)
1050 of_node_put(dn);
1051
1052 return err;
1053}
1054
1055static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
1056{
1057 mdiobus_unregister(priv->slave_mii_bus);
1058 if (priv->master_mii_dn)
1059 of_node_put(priv->master_mii_dn);
1060}
1061
Florian Fainelli246d7f72014-08-27 17:04:56 -07001062static int bcm_sf2_sw_setup(struct dsa_switch *ds)
1063{
1064 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1065 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1066 struct device_node *dn;
1067 void __iomem **base;
1068 unsigned int port;
1069 unsigned int i;
1070 u32 reg, rev;
1071 int ret;
1072
1073 spin_lock_init(&priv->indir_lock);
1074 mutex_init(&priv->stats_mutex);
1075
1076 /* All the interesting properties are at the parent device_node
1077 * level
1078 */
Andrew Lunnff049552016-05-10 23:27:24 +02001079 dn = ds->cd->of_node->parent;
1080 bcm_sf2_identify_ports(priv, ds->cd->of_node);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001081
1082 priv->irq0 = irq_of_parse_and_map(dn, 0);
1083 priv->irq1 = irq_of_parse_and_map(dn, 1);
1084
1085 base = &priv->core;
1086 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
1087 *base = of_iomap(dn, i);
1088 if (*base == NULL) {
1089 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainellia5660592014-11-25 18:08:48 -08001090 ret = -ENOMEM;
1091 goto out_unmap;
Florian Fainelli246d7f72014-08-27 17:04:56 -07001092 }
1093 base++;
1094 }
1095
Florian Fainelli33f84612014-11-25 18:08:49 -08001096 ret = bcm_sf2_sw_rst(priv);
1097 if (ret) {
1098 pr_err("unable to software reset switch: %d\n", ret);
1099 goto out_unmap;
1100 }
1101
Florian Fainelli461cd1b02016-06-07 16:32:43 -07001102 ret = bcm_sf2_mdio_register(ds);
1103 if (ret) {
1104 pr_err("failed to register MDIO bus\n");
1105 goto out_unmap;
1106 }
1107
Florian Fainelli246d7f72014-08-27 17:04:56 -07001108 /* Disable all interrupts and request them */
Florian Fainelli691c9a82015-01-20 16:42:00 -08001109 bcm_sf2_intr_disable(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001110
1111 ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
1112 "switch_0", priv);
1113 if (ret < 0) {
1114 pr_err("failed to request switch_0 IRQ\n");
1115 goto out_unmap;
1116 }
1117
1118 ret = request_irq(priv->irq1, bcm_sf2_switch_1_isr, 0,
1119 "switch_1", priv);
1120 if (ret < 0) {
1121 pr_err("failed to request switch_1 IRQ\n");
1122 goto out_free_irq0;
1123 }
1124
1125 /* Reset the MIB counters */
1126 reg = core_readl(priv, CORE_GMNCFGCFG);
1127 reg |= RST_MIB_CNT;
1128 core_writel(priv, reg, CORE_GMNCFGCFG);
1129 reg &= ~RST_MIB_CNT;
1130 core_writel(priv, reg, CORE_GMNCFGCFG);
1131
1132 /* Get the maximum number of ports for this switch */
1133 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1134 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1135 priv->hw_params.num_ports = DSA_MAX_PORTS;
1136
1137 /* Assume a single GPHY setup if we can't read that property */
1138 if (of_property_read_u32(dn, "brcm,num-gphy",
1139 &priv->hw_params.num_gphy))
1140 priv->hw_params.num_gphy = 1;
1141
1142 /* Enable all valid ports and disable those unused */
1143 for (port = 0; port < priv->hw_params.num_ports; port++) {
1144 /* IMP port receives special treatment */
Andrew Lunn74c3e2a2016-04-13 02:40:44 +02001145 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -07001146 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001147 else if (dsa_is_cpu_port(ds, port))
1148 bcm_sf2_imp_setup(ds, port);
1149 else
Florian Fainellib6d045d2014-09-24 17:05:20 -07001150 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001151 }
1152
Florian Fainelli246d7f72014-08-27 17:04:56 -07001153 rev = reg_readl(priv, REG_SWITCH_REVISION);
1154 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1155 SWITCH_TOP_REV_MASK;
1156 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1157
Florian Fainelliaa9aef72014-09-19 13:07:55 -07001158 rev = reg_readl(priv, REG_PHY_REVISION);
1159 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1160
Florian Fainelli246d7f72014-08-27 17:04:56 -07001161 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1162 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1163 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1164 priv->core, priv->irq0, priv->irq1);
1165
1166 return 0;
1167
1168out_free_irq0:
1169 free_irq(priv->irq0, priv);
1170out_unmap:
1171 base = &priv->core;
1172 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainellia5660592014-11-25 18:08:48 -08001173 if (*base)
1174 iounmap(*base);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001175 base++;
1176 }
Florian Fainelli461cd1b02016-06-07 16:32:43 -07001177 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001178 return ret;
1179}
1180
1181static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
1182{
1183 return 0;
1184}
1185
Florian Fainelliaa9aef72014-09-19 13:07:55 -07001186static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
1187{
1188 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1189
1190 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
1191 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
1192 * the REG_PHY_REVISION register layout is.
1193 */
1194
1195 return priv->hw_params.gphy_rev;
1196}
1197
Florian Fainelli246d7f72014-08-27 17:04:56 -07001198static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
1199 struct phy_device *phydev)
1200{
1201 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1202 u32 id_mode_dis = 0, port_mode;
1203 const char *str = NULL;
1204 u32 reg;
1205
1206 switch (phydev->interface) {
1207 case PHY_INTERFACE_MODE_RGMII:
1208 str = "RGMII (no delay)";
1209 id_mode_dis = 1;
1210 case PHY_INTERFACE_MODE_RGMII_TXID:
1211 if (!str)
1212 str = "RGMII (TX delay)";
1213 port_mode = EXT_GPHY;
1214 break;
1215 case PHY_INTERFACE_MODE_MII:
1216 str = "MII";
1217 port_mode = EXT_EPHY;
1218 break;
1219 case PHY_INTERFACE_MODE_REVMII:
1220 str = "Reverse MII";
1221 port_mode = EXT_REVMII;
1222 break;
1223 default:
Florian Fainelli7de15572014-09-24 17:05:19 -07001224 /* All other PHYs: internal and MoCA */
1225 goto force_link;
1226 }
1227
1228 /* If the link is down, just disable the interface to conserve power */
1229 if (!phydev->link) {
1230 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
1231 reg &= ~RGMII_MODE_EN;
1232 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -07001233 goto force_link;
1234 }
1235
1236 /* Clear id_mode_dis bit, and the existing port mode, but
1237 * make sure we enable the RGMII block for data to pass
1238 */
1239 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
1240 reg &= ~ID_MODE_DIS;
1241 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
1242 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
1243
1244 reg |= port_mode | RGMII_MODE_EN;
1245 if (id_mode_dis)
1246 reg |= ID_MODE_DIS;
1247
1248 if (phydev->pause) {
1249 if (phydev->asym_pause)
1250 reg |= TX_PAUSE_EN;
1251 reg |= RX_PAUSE_EN;
1252 }
1253
1254 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
1255
1256 pr_info("Port %d configured for %s\n", port, str);
1257
1258force_link:
1259 /* Force link settings detected from the PHY */
1260 reg = SW_OVERRIDE;
1261 switch (phydev->speed) {
1262 case SPEED_1000:
1263 reg |= SPDSTS_1000 << SPEED_SHIFT;
1264 break;
1265 case SPEED_100:
1266 reg |= SPDSTS_100 << SPEED_SHIFT;
1267 break;
1268 }
1269
1270 if (phydev->link)
1271 reg |= LINK_STS;
1272 if (phydev->duplex == DUPLEX_FULL)
1273 reg |= DUPLX_MODE;
1274
1275 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1276}
1277
1278static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
1279 struct fixed_phy_status *status)
1280{
1281 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainellid2eac982015-07-20 17:49:55 -07001282 u32 duplex, pause;
Florian Fainelli246d7f72014-08-27 17:04:56 -07001283 u32 reg;
1284
Florian Fainelli246d7f72014-08-27 17:04:56 -07001285 duplex = core_readl(priv, CORE_DUPSTS);
1286 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001287
1288 status->link = 0;
1289
Florian Fainelli8b7c94e2015-10-23 12:11:08 -07001290 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
Florian Fainelli246d7f72014-08-27 17:04:56 -07001291 * which means that we need to force the link at the port override
1292 * level to get the data to flow. We do use what the interrupt handler
1293 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -08001294 *
1295 * For the other ports, we just force the link status, since this is
1296 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -07001297 */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -07001298 if (port == priv->moca_port) {
Florian Fainelli246d7f72014-08-27 17:04:56 -07001299 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -07001300 /* For MoCA interfaces, also force a link down notification
1301 * since some version of the user-space daemon (mocad) use
1302 * cmd->autoneg to force the link, which messes up the PHY
1303 * state machine and make it go in PHY_FORCING state instead.
1304 */
1305 if (!status->link)
Andrew Lunnc8b09802016-06-04 21:16:57 +02001306 netif_carrier_off(ds->ports[port].netdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001307 status->duplex = 1;
1308 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -08001309 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -07001310 status->duplex = !!(duplex & (1 << port));
1311 }
1312
Florian Fainelli7855f672014-12-11 18:12:42 -08001313 reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1314 reg |= SW_OVERRIDE;
1315 if (status->link)
1316 reg |= LINK_STS;
1317 else
1318 reg &= ~LINK_STS;
1319 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1320
Florian Fainelli246d7f72014-08-27 17:04:56 -07001321 if ((pause & (1 << port)) &&
1322 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
1323 status->asym_pause = 1;
1324 status->pause = 1;
1325 }
1326
1327 if (pause & (1 << port))
1328 status->pause = 1;
1329}
1330
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001331static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
1332{
1333 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1334 unsigned int port;
1335
Florian Fainelli691c9a82015-01-20 16:42:00 -08001336 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001337
1338 /* Disable all ports physically present including the IMP
1339 * port, the other ones have already been disabled during
1340 * bcm_sf2_sw_setup
1341 */
1342 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +02001343 if ((1 << port) & ds->enabled_port_mask ||
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001344 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -07001345 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001346 }
1347
1348 return 0;
1349}
1350
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001351static int bcm_sf2_sw_resume(struct dsa_switch *ds)
1352{
1353 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1354 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001355 int ret;
1356
1357 ret = bcm_sf2_sw_rst(priv);
1358 if (ret) {
1359 pr_err("%s: failed to software reset switch\n", __func__);
1360 return ret;
1361 }
1362
Florian Fainellib0836682015-02-05 11:40:41 -08001363 if (priv->hw_params.num_gphy == 1)
1364 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001365
1366 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +02001367 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -07001368 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001369 else if (dsa_is_cpu_port(ds, port))
1370 bcm_sf2_imp_setup(ds, port);
1371 }
1372
1373 return 0;
1374}
1375
Florian Fainelli96e65d72014-09-18 17:31:25 -07001376static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
1377 struct ethtool_wolinfo *wol)
1378{
1379 struct net_device *p = ds->dst[ds->index].master_netdev;
1380 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1381 struct ethtool_wolinfo pwol;
1382
1383 /* Get the parent device WoL settings */
1384 p->ethtool_ops->get_wol(p, &pwol);
1385
1386 /* Advertise the parent device supported settings */
1387 wol->supported = pwol.supported;
1388 memset(&wol->sopass, 0, sizeof(wol->sopass));
1389
1390 if (pwol.wolopts & WAKE_MAGICSECURE)
1391 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
1392
1393 if (priv->wol_ports_mask & (1 << port))
1394 wol->wolopts = pwol.wolopts;
1395 else
1396 wol->wolopts = 0;
1397}
1398
1399static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
1400 struct ethtool_wolinfo *wol)
1401{
1402 struct net_device *p = ds->dst[ds->index].master_netdev;
1403 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1404 s8 cpu_port = ds->dst[ds->index].cpu_port;
1405 struct ethtool_wolinfo pwol;
1406
1407 p->ethtool_ops->get_wol(p, &pwol);
1408 if (wol->wolopts & ~pwol.supported)
1409 return -EINVAL;
1410
1411 if (wol->wolopts)
1412 priv->wol_ports_mask |= (1 << port);
1413 else
1414 priv->wol_ports_mask &= ~(1 << port);
1415
1416 /* If we have at least one port enabled, make sure the CPU port
1417 * is also enabled. If the CPU port is the last one enabled, we disable
1418 * it since this configuration does not make sense.
1419 */
1420 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
1421 priv->wol_ports_mask |= (1 << cpu_port);
1422 else
1423 priv->wol_ports_mask &= ~(1 << cpu_port);
1424
1425 return p->ethtool_ops->set_wol(p, wol);
1426}
1427
Florian Fainelli246d7f72014-08-27 17:04:56 -07001428static struct dsa_switch_driver bcm_sf2_switch_driver = {
Florian Fainelliac7a04c2014-09-11 21:18:09 -07001429 .tag_protocol = DSA_TAG_PROTO_BRCM,
Andrew Lunne49bad32016-04-13 02:40:43 +02001430 .probe = bcm_sf2_sw_drv_probe,
Florian Fainelli246d7f72014-08-27 17:04:56 -07001431 .setup = bcm_sf2_sw_setup,
1432 .set_addr = bcm_sf2_sw_set_addr,
Florian Fainelliaa9aef72014-09-19 13:07:55 -07001433 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
Florian Fainelli246d7f72014-08-27 17:04:56 -07001434 .get_strings = bcm_sf2_sw_get_strings,
1435 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
1436 .get_sset_count = bcm_sf2_sw_get_sset_count,
1437 .adjust_link = bcm_sf2_sw_adjust_link,
1438 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001439 .suspend = bcm_sf2_sw_suspend,
1440 .resume = bcm_sf2_sw_resume,
Florian Fainelli96e65d72014-09-18 17:31:25 -07001441 .get_wol = bcm_sf2_sw_get_wol,
1442 .set_wol = bcm_sf2_sw_set_wol,
Florian Fainellib6d045d2014-09-24 17:05:20 -07001443 .port_enable = bcm_sf2_port_setup,
1444 .port_disable = bcm_sf2_port_disable,
Florian Fainelli450b05c2014-09-24 17:05:22 -07001445 .get_eee = bcm_sf2_sw_get_eee,
1446 .set_eee = bcm_sf2_sw_set_eee,
Vivien Didelot71327a42016-03-13 16:21:32 -04001447 .port_bridge_join = bcm_sf2_sw_br_join,
1448 .port_bridge_leave = bcm_sf2_sw_br_leave,
Vivien Didelot43c44a92016-04-06 11:55:03 -04001449 .port_stp_state_set = bcm_sf2_sw_br_set_stp_state,
Florian Fainelli680060d2015-10-23 11:38:07 -07001450 .port_fdb_prepare = bcm_sf2_sw_fdb_prepare,
1451 .port_fdb_add = bcm_sf2_sw_fdb_add,
1452 .port_fdb_del = bcm_sf2_sw_fdb_del,
1453 .port_fdb_dump = bcm_sf2_sw_fdb_dump,
Florian Fainelli246d7f72014-08-27 17:04:56 -07001454};
1455
1456static int __init bcm_sf2_init(void)
1457{
1458 register_switch_driver(&bcm_sf2_switch_driver);
1459
1460 return 0;
1461}
1462module_init(bcm_sf2_init);
1463
1464static void __exit bcm_sf2_exit(void)
1465{
1466 unregister_switch_driver(&bcm_sf2_switch_driver);
1467}
1468module_exit(bcm_sf2_exit);
1469
1470MODULE_AUTHOR("Broadcom Corporation");
1471MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1472MODULE_LICENSE("GPL");
1473MODULE_ALIAS("platform:brcm-sf2");