aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/net/dsa
diff options
context:
space:
mode:
authorFlorian Fainelli <f.fainelli@gmail.com>2014-08-27 20:04:56 -0400
committerDavid S. Miller <davem@davemloft.net>2014-08-28 01:59:40 -0400
commit246d7f773c13cac3e3ab1609fd4ffee520242c63 (patch)
tree4c29061faa719384ca97859a168b0c3d8d346948 /drivers/net/dsa
parent5037d532b83d7325a2743dffe82882a64697a8e8 (diff)
net: dsa: add Broadcom SF2 switch driver
Add support for the Broadcom Starfigther 2 switch chip using a DSA driver. This switch driver supports the following features: - configuration of the external switch port interface: MII, RevMII, RGMII and RGMII_NO_ID are supported - support for the per-port MIB counters - support for link interrupts for special ports (e.g: MoCA) - powering up/down of switch memories to conserve power when ports are unused Finally, update the compatible property for the DSA core code to match our switch top-level compatible node. Signed-off-by: Florian Fainelli <f.fainelli@gmail.com> Signed-off-by: David S. Miller <davem@davemloft.net>
Diffstat (limited to 'drivers/net/dsa')
-rw-r--r--drivers/net/dsa/Kconfig11
-rw-r--r--drivers/net/dsa/Makefile1
-rw-r--r--drivers/net/dsa/bcm_sf2.c626
-rw-r--r--drivers/net/dsa/bcm_sf2.h140
-rw-r--r--drivers/net/dsa/bcm_sf2_regs.h227
5 files changed, 1005 insertions, 0 deletions
diff --git a/drivers/net/dsa/Kconfig b/drivers/net/dsa/Kconfig
index b8fe808b7957..c6ee07c6a1b5 100644
--- a/drivers/net/dsa/Kconfig
+++ b/drivers/net/dsa/Kconfig
@@ -36,4 +36,15 @@ config NET_DSA_MV88E6123_61_65
36 This enables support for the Marvell 88E6123/6161/6165 36 This enables support for the Marvell 88E6123/6161/6165
37 ethernet switch chips. 37 ethernet switch chips.
38 38
39config NET_DSA_BCM_SF2
40 tristate "Broadcom Starfighter 2 Ethernet switch support"
41 select NET_DSA
42 select NET_DSA_TAG_BRCM
43 select FIXED_PHY if NET_DSA_BCM_SF2=y
44 select BCM7XXX_PHY
45 select MDIO_BCM_UNIMAC
46 ---help---
47 This enables support for the Broadcom Starfighter 2 Ethernet
48 switch chips.
49
39endmenu 50endmenu
diff --git a/drivers/net/dsa/Makefile b/drivers/net/dsa/Makefile
index f3bda05536cc..dd3cd3b8157f 100644
--- a/drivers/net/dsa/Makefile
+++ b/drivers/net/dsa/Makefile
@@ -7,3 +7,4 @@ endif
7ifdef CONFIG_NET_DSA_MV88E6131 7ifdef CONFIG_NET_DSA_MV88E6131
8mv88e6xxx_drv-y += mv88e6131.o 8mv88e6xxx_drv-y += mv88e6131.o
9endif 9endif
10obj-$(CONFIG_NET_DSA_BCM_SF2) += bcm_sf2.o
diff --git a/drivers/net/dsa/bcm_sf2.c b/drivers/net/dsa/bcm_sf2.c
new file mode 100644
index 000000000000..bb7cb8e283b1
--- /dev/null
+++ b/drivers/net/dsa/bcm_sf2.c
@@ -0,0 +1,626 @@
1/*
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>
24#include <net/dsa.h>
25
26#include "bcm_sf2.h"
27#include "bcm_sf2_regs.h"
28
29/* String, offset, and register size in bytes if different from 4 bytes */
30static const struct bcm_sf2_hw_stats bcm_sf2_mib[] = {
31 { "TxOctets", 0x000, 8 },
32 { "TxDropPkts", 0x020 },
33 { "TxQPKTQ0", 0x030 },
34 { "TxBroadcastPkts", 0x040 },
35 { "TxMulticastPkts", 0x050 },
36 { "TxUnicastPKts", 0x060 },
37 { "TxCollisions", 0x070 },
38 { "TxSingleCollision", 0x080 },
39 { "TxMultipleCollision", 0x090 },
40 { "TxDeferredCollision", 0x0a0 },
41 { "TxLateCollision", 0x0b0 },
42 { "TxExcessiveCollision", 0x0c0 },
43 { "TxFrameInDisc", 0x0d0 },
44 { "TxPausePkts", 0x0e0 },
45 { "TxQPKTQ1", 0x0f0 },
46 { "TxQPKTQ2", 0x100 },
47 { "TxQPKTQ3", 0x110 },
48 { "TxQPKTQ4", 0x120 },
49 { "TxQPKTQ5", 0x130 },
50 { "RxOctets", 0x140, 8 },
51 { "RxUndersizePkts", 0x160 },
52 { "RxPausePkts", 0x170 },
53 { "RxPkts64Octets", 0x180 },
54 { "RxPkts65to127Octets", 0x190 },
55 { "RxPkts128to255Octets", 0x1a0 },
56 { "RxPkts256to511Octets", 0x1b0 },
57 { "RxPkts512to1023Octets", 0x1c0 },
58 { "RxPkts1024toMaxPktsOctets", 0x1d0 },
59 { "RxOversizePkts", 0x1e0 },
60 { "RxJabbers", 0x1f0 },
61 { "RxAlignmentErrors", 0x200 },
62 { "RxFCSErrors", 0x210 },
63 { "RxGoodOctets", 0x220, 8 },
64 { "RxDropPkts", 0x240 },
65 { "RxUnicastPkts", 0x250 },
66 { "RxMulticastPkts", 0x260 },
67 { "RxBroadcastPkts", 0x270 },
68 { "RxSAChanges", 0x280 },
69 { "RxFragments", 0x290 },
70 { "RxJumboPkt", 0x2a0 },
71 { "RxSymblErr", 0x2b0 },
72 { "InRangeErrCount", 0x2c0 },
73 { "OutRangeErrCount", 0x2d0 },
74 { "EEELpiEvent", 0x2e0 },
75 { "EEELpiDuration", 0x2f0 },
76 { "RxDiscard", 0x300, 8 },
77 { "TxQPKTQ6", 0x320 },
78 { "TxQPKTQ7", 0x330 },
79 { "TxPkts64Octets", 0x340 },
80 { "TxPkts65to127Octets", 0x350 },
81 { "TxPkts128to255Octets", 0x360 },
82 { "TxPkts256to511Ocets", 0x370 },
83 { "TxPkts512to1023Ocets", 0x380 },
84 { "TxPkts1024toMaxPktOcets", 0x390 },
85};
86
87#define BCM_SF2_STATS_SIZE ARRAY_SIZE(bcm_sf2_mib)
88
89static void bcm_sf2_sw_get_strings(struct dsa_switch *ds,
90 int port, uint8_t *data)
91{
92 unsigned int i;
93
94 for (i = 0; i < BCM_SF2_STATS_SIZE; i++)
95 memcpy(data + i * ETH_GSTRING_LEN,
96 bcm_sf2_mib[i].string, ETH_GSTRING_LEN);
97}
98
99static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds,
100 int port, uint64_t *data)
101{
102 struct bcm_sf2_priv *priv = ds_to_priv(ds);
103 const struct bcm_sf2_hw_stats *s;
104 unsigned int i;
105 u64 val = 0;
106 u32 offset;
107
108 mutex_lock(&priv->stats_mutex);
109
110 /* Now fetch the per-port counters */
111 for (i = 0; i < BCM_SF2_STATS_SIZE; i++) {
112 s = &bcm_sf2_mib[i];
113
114 /* Do a latched 64-bit read if needed */
115 offset = s->reg + CORE_P_MIB_OFFSET(port);
116 if (s->sizeof_stat == 8)
117 val = core_readq(priv, offset);
118 else
119 val = core_readl(priv, offset);
120
121 data[i] = (u64)val;
122 }
123
124 mutex_unlock(&priv->stats_mutex);
125}
126
127static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds)
128{
129 return BCM_SF2_STATS_SIZE;
130}
131
132static char *bcm_sf2_sw_probe(struct mii_bus *bus, int sw_addr)
133{
134 return "Broadcom Starfighter 2";
135}
136
137static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
138{
139 struct bcm_sf2_priv *priv = ds_to_priv(ds);
140 unsigned int i;
141 u32 reg, val;
142
143 /* Enable the port memories */
144 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
145 reg &= ~P_TXQ_PSM_VDD(port);
146 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
147
148 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
149 reg = core_readl(priv, CORE_IMP_CTL);
150 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
151 reg &= ~(RX_DIS | TX_DIS);
152 core_writel(priv, reg, CORE_IMP_CTL);
153
154 /* Enable forwarding */
155 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
156
157 /* Enable IMP port in dumb mode */
158 reg = core_readl(priv, CORE_SWITCH_CTRL);
159 reg |= MII_DUMB_FWDG_EN;
160 core_writel(priv, reg, CORE_SWITCH_CTRL);
161
162 /* Resolve which bit controls the Broadcom tag */
163 switch (port) {
164 case 8:
165 val = BRCM_HDR_EN_P8;
166 break;
167 case 7:
168 val = BRCM_HDR_EN_P7;
169 break;
170 case 5:
171 val = BRCM_HDR_EN_P5;
172 break;
173 default:
174 val = 0;
175 break;
176 }
177
178 /* Enable Broadcom tags for IMP port */
179 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
180 reg |= val;
181 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
182
183 /* Enable reception Broadcom tag for CPU TX (switch RX) to
184 * allow us to tag outgoing frames
185 */
186 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
187 reg &= ~(1 << port);
188 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
189
190 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
191 * allow delivering frames to the per-port net_devices
192 */
193 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
194 reg &= ~(1 << port);
195 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
196
197 /* Force link status for IMP port */
198 reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
199 reg |= (MII_SW_OR | LINK_STS);
200 core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
201
202 /* Enable the IMP Port to be in the same VLAN as the other ports
203 * on a per-port basis such that we only have Port i and IMP in
204 * the same VLAN.
205 */
206 for (i = 0; i < priv->hw_params.num_ports; i++) {
207 if (!((1 << i) & ds->phys_port_mask))
208 continue;
209
210 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
211 reg |= (1 << port);
212 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
213 }
214}
215
216static void bcm_sf2_port_setup(struct dsa_switch *ds, int port)
217{
218 struct bcm_sf2_priv *priv = ds_to_priv(ds);
219 u32 reg;
220
221 /* Clear the memory power down */
222 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
223 reg &= ~P_TXQ_PSM_VDD(port);
224 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
225
226 /* Clear the Rx and Tx disable bits and set to no spanning tree */
227 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
228
229 /* Enable port 7 interrupts to get notified */
230 if (port == 7)
231 intrl2_1_mask_clear(priv, P_IRQ_MASK(P7_IRQ_OFF));
232
233 /* Set this port, and only this one to be in the default VLAN */
234 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
235 reg &= ~PORT_VLAN_CTRL_MASK;
236 reg |= (1 << port);
237 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
238}
239
240static void bcm_sf2_port_disable(struct dsa_switch *ds, int port)
241{
242 struct bcm_sf2_priv *priv = ds_to_priv(ds);
243 u32 off, reg;
244
245 if (dsa_is_cpu_port(ds, port))
246 off = CORE_IMP_CTL;
247 else
248 off = CORE_G_PCTL_PORT(port);
249
250 reg = core_readl(priv, off);
251 reg |= RX_DIS | TX_DIS;
252 core_writel(priv, reg, off);
253
254 /* Power down the port memory */
255 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
256 reg |= P_TXQ_PSM_VDD(port);
257 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
258}
259
260static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
261{
262 struct bcm_sf2_priv *priv = dev_id;
263
264 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
265 ~priv->irq0_mask;
266 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
267
268 return IRQ_HANDLED;
269}
270
271static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
272{
273 struct bcm_sf2_priv *priv = dev_id;
274
275 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
276 ~priv->irq1_mask;
277 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
278
279 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
280 priv->port_sts[7].link = 1;
281 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
282 priv->port_sts[7].link = 0;
283
284 return IRQ_HANDLED;
285}
286
287static int bcm_sf2_sw_setup(struct dsa_switch *ds)
288{
289 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
290 struct bcm_sf2_priv *priv = ds_to_priv(ds);
291 struct device_node *dn;
292 void __iomem **base;
293 unsigned int port;
294 unsigned int i;
295 u32 reg, rev;
296 int ret;
297
298 spin_lock_init(&priv->indir_lock);
299 mutex_init(&priv->stats_mutex);
300
301 /* All the interesting properties are at the parent device_node
302 * level
303 */
304 dn = ds->pd->of_node->parent;
305
306 priv->irq0 = irq_of_parse_and_map(dn, 0);
307 priv->irq1 = irq_of_parse_and_map(dn, 1);
308
309 base = &priv->core;
310 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
311 *base = of_iomap(dn, i);
312 if (*base == NULL) {
313 pr_err("unable to find register: %s\n", reg_names[i]);
314 return -ENODEV;
315 }
316 base++;
317 }
318
319 /* Disable all interrupts and request them */
320 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
321 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
322 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
323 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
324 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
325 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
326
327 ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
328 "switch_0", priv);
329 if (ret < 0) {
330 pr_err("failed to request switch_0 IRQ\n");
331 goto out_unmap;
332 }
333
334 ret = request_irq(priv->irq1, bcm_sf2_switch_1_isr, 0,
335 "switch_1", priv);
336 if (ret < 0) {
337 pr_err("failed to request switch_1 IRQ\n");
338 goto out_free_irq0;
339 }
340
341 /* Reset the MIB counters */
342 reg = core_readl(priv, CORE_GMNCFGCFG);
343 reg |= RST_MIB_CNT;
344 core_writel(priv, reg, CORE_GMNCFGCFG);
345 reg &= ~RST_MIB_CNT;
346 core_writel(priv, reg, CORE_GMNCFGCFG);
347
348 /* Get the maximum number of ports for this switch */
349 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
350 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
351 priv->hw_params.num_ports = DSA_MAX_PORTS;
352
353 /* Assume a single GPHY setup if we can't read that property */
354 if (of_property_read_u32(dn, "brcm,num-gphy",
355 &priv->hw_params.num_gphy))
356 priv->hw_params.num_gphy = 1;
357
358 /* Enable all valid ports and disable those unused */
359 for (port = 0; port < priv->hw_params.num_ports; port++) {
360 /* IMP port receives special treatment */
361 if ((1 << port) & ds->phys_port_mask)
362 bcm_sf2_port_setup(ds, port);
363 else if (dsa_is_cpu_port(ds, port))
364 bcm_sf2_imp_setup(ds, port);
365 else
366 bcm_sf2_port_disable(ds, port);
367 }
368
369 /* Include the pseudo-PHY address and the broadcast PHY address to
370 * divert reads towards our workaround
371 */
372 ds->phys_mii_mask |= ((1 << 30) | (1 << 0));
373
374 rev = reg_readl(priv, REG_SWITCH_REVISION);
375 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
376 SWITCH_TOP_REV_MASK;
377 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
378
379 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
380 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
381 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
382 priv->core, priv->irq0, priv->irq1);
383
384 return 0;
385
386out_free_irq0:
387 free_irq(priv->irq0, priv);
388out_unmap:
389 base = &priv->core;
390 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
391 iounmap(*base);
392 base++;
393 }
394 return ret;
395}
396
397static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
398{
399 return 0;
400}
401
402static int bcm_sf2_sw_indir_rw(struct dsa_switch *ds, int op, int addr,
403 int regnum, u16 val)
404{
405 struct bcm_sf2_priv *priv = ds_to_priv(ds);
406 int ret = 0;
407 u32 reg;
408
409 reg = reg_readl(priv, REG_SWITCH_CNTRL);
410 reg |= MDIO_MASTER_SEL;
411 reg_writel(priv, reg, REG_SWITCH_CNTRL);
412
413 /* Page << 8 | offset */
414 reg = 0x70;
415 reg <<= 2;
416 core_writel(priv, addr, reg);
417
418 /* Page << 8 | offset */
419 reg = 0x80 << 8 | regnum << 1;
420 reg <<= 2;
421
422 if (op)
423 ret = core_readl(priv, reg);
424 else
425 core_writel(priv, val, reg);
426
427 reg = reg_readl(priv, REG_SWITCH_CNTRL);
428 reg &= ~MDIO_MASTER_SEL;
429 reg_writel(priv, reg, REG_SWITCH_CNTRL);
430
431 return ret & 0xffff;
432}
433
434static int bcm_sf2_sw_phy_read(struct dsa_switch *ds, int addr, int regnum)
435{
436 /* Intercept reads from the MDIO broadcast address or Broadcom
437 * pseudo-PHY address
438 */
439 switch (addr) {
440 case 0:
441 case 30:
442 return bcm_sf2_sw_indir_rw(ds, 1, addr, regnum, 0);
443 default:
444 return 0xffff;
445 }
446}
447
448static int bcm_sf2_sw_phy_write(struct dsa_switch *ds, int addr, int regnum,
449 u16 val)
450{
451 /* Intercept writes to the MDIO broadcast address or Broadcom
452 * pseudo-PHY address
453 */
454 switch (addr) {
455 case 0:
456 case 30:
457 bcm_sf2_sw_indir_rw(ds, 0, addr, regnum, val);
458 break;
459 }
460
461 return 0;
462}
463
464static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
465 struct phy_device *phydev)
466{
467 struct bcm_sf2_priv *priv = ds_to_priv(ds);
468 u32 id_mode_dis = 0, port_mode;
469 const char *str = NULL;
470 u32 reg;
471
472 switch (phydev->interface) {
473 case PHY_INTERFACE_MODE_RGMII:
474 str = "RGMII (no delay)";
475 id_mode_dis = 1;
476 case PHY_INTERFACE_MODE_RGMII_TXID:
477 if (!str)
478 str = "RGMII (TX delay)";
479 port_mode = EXT_GPHY;
480 break;
481 case PHY_INTERFACE_MODE_MII:
482 str = "MII";
483 port_mode = EXT_EPHY;
484 break;
485 case PHY_INTERFACE_MODE_REVMII:
486 str = "Reverse MII";
487 port_mode = EXT_REVMII;
488 break;
489 default:
490 goto force_link;
491 }
492
493 /* Clear id_mode_dis bit, and the existing port mode, but
494 * make sure we enable the RGMII block for data to pass
495 */
496 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
497 reg &= ~ID_MODE_DIS;
498 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
499 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
500
501 reg |= port_mode | RGMII_MODE_EN;
502 if (id_mode_dis)
503 reg |= ID_MODE_DIS;
504
505 if (phydev->pause) {
506 if (phydev->asym_pause)
507 reg |= TX_PAUSE_EN;
508 reg |= RX_PAUSE_EN;
509 }
510
511 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
512
513 pr_info("Port %d configured for %s\n", port, str);
514
515force_link:
516 /* Force link settings detected from the PHY */
517 reg = SW_OVERRIDE;
518 switch (phydev->speed) {
519 case SPEED_1000:
520 reg |= SPDSTS_1000 << SPEED_SHIFT;
521 break;
522 case SPEED_100:
523 reg |= SPDSTS_100 << SPEED_SHIFT;
524 break;
525 }
526
527 if (phydev->link)
528 reg |= LINK_STS;
529 if (phydev->duplex == DUPLEX_FULL)
530 reg |= DUPLX_MODE;
531
532 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
533}
534
535static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
536 struct fixed_phy_status *status)
537{
538 struct bcm_sf2_priv *priv = ds_to_priv(ds);
539 u32 link, duplex, pause, speed;
540 u32 reg;
541
542 link = core_readl(priv, CORE_LNKSTS);
543 duplex = core_readl(priv, CORE_DUPSTS);
544 pause = core_readl(priv, CORE_PAUSESTS);
545 speed = core_readl(priv, CORE_SPDSTS);
546
547 speed >>= (port * SPDSTS_SHIFT);
548 speed &= SPDSTS_MASK;
549
550 status->link = 0;
551
552 /* Port 7 is special as we do not get link status from CORE_LNKSTS,
553 * which means that we need to force the link at the port override
554 * level to get the data to flow. We do use what the interrupt handler
555 * did determine before.
556 */
557 if (port == 7) {
558 status->link = priv->port_sts[port].link;
559 reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(7));
560 reg |= SW_OVERRIDE;
561 if (status->link)
562 reg |= LINK_STS;
563 else
564 reg &= ~LINK_STS;
565 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(7));
566 status->duplex = 1;
567 } else {
568 status->link = !!(link & (1 << port));
569 status->duplex = !!(duplex & (1 << port));
570 }
571
572 switch (speed) {
573 case SPDSTS_10:
574 status->speed = SPEED_10;
575 break;
576 case SPDSTS_100:
577 status->speed = SPEED_100;
578 break;
579 case SPDSTS_1000:
580 status->speed = SPEED_1000;
581 break;
582 }
583
584 if ((pause & (1 << port)) &&
585 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
586 status->asym_pause = 1;
587 status->pause = 1;
588 }
589
590 if (pause & (1 << port))
591 status->pause = 1;
592}
593
594static struct dsa_switch_driver bcm_sf2_switch_driver = {
595 .tag_protocol = htons(ETH_P_BRCMTAG),
596 .priv_size = sizeof(struct bcm_sf2_priv),
597 .probe = bcm_sf2_sw_probe,
598 .setup = bcm_sf2_sw_setup,
599 .set_addr = bcm_sf2_sw_set_addr,
600 .phy_read = bcm_sf2_sw_phy_read,
601 .phy_write = bcm_sf2_sw_phy_write,
602 .get_strings = bcm_sf2_sw_get_strings,
603 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
604 .get_sset_count = bcm_sf2_sw_get_sset_count,
605 .adjust_link = bcm_sf2_sw_adjust_link,
606 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
607};
608
609static int __init bcm_sf2_init(void)
610{
611 register_switch_driver(&bcm_sf2_switch_driver);
612
613 return 0;
614}
615module_init(bcm_sf2_init);
616
617static void __exit bcm_sf2_exit(void)
618{
619 unregister_switch_driver(&bcm_sf2_switch_driver);
620}
621module_exit(bcm_sf2_exit);
622
623MODULE_AUTHOR("Broadcom Corporation");
624MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
625MODULE_LICENSE("GPL");
626MODULE_ALIAS("platform:brcm-sf2");
diff --git a/drivers/net/dsa/bcm_sf2.h b/drivers/net/dsa/bcm_sf2.h
new file mode 100644
index 000000000000..260bab313e58
--- /dev/null
+++ b/drivers/net/dsa/bcm_sf2.h
@@ -0,0 +1,140 @@
1/*
2 * Broadcom Starfighter2 private context
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#ifndef __BCM_SF2_H
13#define __BCM_SF2_H
14
15#include <linux/platform_device.h>
16#include <linux/kernel.h>
17#include <linux/io.h>
18#include <linux/spinlock.h>
19#include <linux/mutex.h>
20#include <linux/mii.h>
21
22#include <net/dsa.h>
23
24#include "bcm_sf2_regs.h"
25
26struct bcm_sf2_hw_params {
27 u16 top_rev;
28 u16 core_rev;
29 u32 num_gphy;
30 u8 num_acb_queue;
31 u8 num_rgmii;
32 u8 num_ports;
33 u8 fcb_pause_override:1;
34 u8 acb_packets_inflight:1;
35};
36
37#define BCM_SF2_REGS_NAME {\
38 "core", "reg", "intrl2_0", "intrl2_1", "fcb", "acb" \
39}
40
41#define BCM_SF2_REGS_NUM 6
42
43struct bcm_sf2_port_status {
44 unsigned int link;
45};
46
47struct bcm_sf2_priv {
48 /* Base registers, keep those in order with BCM_SF2_REGS_NAME */
49 void __iomem *core;
50 void __iomem *reg;
51 void __iomem *intrl2_0;
52 void __iomem *intrl2_1;
53 void __iomem *fcb;
54 void __iomem *acb;
55
56 /* spinlock protecting access to the indirect registers */
57 spinlock_t indir_lock;
58
59 int irq0;
60 int irq1;
61 u32 irq0_stat;
62 u32 irq0_mask;
63 u32 irq1_stat;
64 u32 irq1_mask;
65
66 /* Mutex protecting access to the MIB counters */
67 struct mutex stats_mutex;
68
69 struct bcm_sf2_hw_params hw_params;
70
71 struct bcm_sf2_port_status port_sts[DSA_MAX_PORTS];
72};
73
74struct bcm_sf2_hw_stats {
75 const char *string;
76 u16 reg;
77 u8 sizeof_stat;
78};
79
80#define SF2_IO_MACRO(name) \
81static inline u32 name##_readl(struct bcm_sf2_priv *priv, u32 off) \
82{ \
83 return __raw_readl(priv->name + off); \
84} \
85static inline void name##_writel(struct bcm_sf2_priv *priv, \
86 u32 val, u32 off) \
87{ \
88 __raw_writel(val, priv->name + off); \
89} \
90
91/* Accesses to 64-bits register requires us to latch the hi/lo pairs
92 * using the REG_DIR_DATA_{READ,WRITE} ancillary registers. The 'indir_lock'
93 * spinlock is automatically grabbed and released to provide relative
94 * atomiticy with latched reads/writes.
95 */
96#define SF2_IO64_MACRO(name) \
97static inline u64 name##_readq(struct bcm_sf2_priv *priv, u32 off) \
98{ \
99 u32 indir, dir; \
100 spin_lock(&priv->indir_lock); \
101 indir = reg_readl(priv, REG_DIR_DATA_READ); \
102 dir = __raw_readl(priv->name + off); \
103 spin_unlock(&priv->indir_lock); \
104 return (u64)indir << 32 | dir; \
105} \
106static inline void name##_writeq(struct bcm_sf2_priv *priv, u32 off, \
107 u64 val) \
108{ \
109 spin_lock(&priv->indir_lock); \
110 reg_writel(priv, upper_32_bits(val), REG_DIR_DATA_WRITE); \
111 __raw_writel(lower_32_bits(val), priv->name + off); \
112 spin_unlock(&priv->indir_lock); \
113}
114
115#define SWITCH_INTR_L2(which) \
116static inline void intrl2_##which##_mask_clear(struct bcm_sf2_priv *priv, \
117 u32 mask) \
118{ \
119 intrl2_##which##_writel(priv, mask, INTRL2_CPU_MASK_CLEAR); \
120 priv->irq##which##_mask &= ~(mask); \
121} \
122static inline void intrl2_##which##_mask_set(struct bcm_sf2_priv *priv, \
123 u32 mask) \
124{ \
125 intrl2_## which##_writel(priv, mask, INTRL2_CPU_MASK_SET); \
126 priv->irq##which##_mask |= (mask); \
127} \
128
129SF2_IO_MACRO(core);
130SF2_IO_MACRO(reg);
131SF2_IO64_MACRO(core);
132SF2_IO_MACRO(intrl2_0);
133SF2_IO_MACRO(intrl2_1);
134SF2_IO_MACRO(fcb);
135SF2_IO_MACRO(acb);
136
137SWITCH_INTR_L2(0);
138SWITCH_INTR_L2(1);
139
140#endif /* __BCM_SF2_H */
diff --git a/drivers/net/dsa/bcm_sf2_regs.h b/drivers/net/dsa/bcm_sf2_regs.h
new file mode 100644
index 000000000000..885c231b03b5
--- /dev/null
+++ b/drivers/net/dsa/bcm_sf2_regs.h
@@ -0,0 +1,227 @@
1/*
2 * Broadcom Starfighter 2 switch register defines
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#ifndef __BCM_SF2_REGS_H
12#define __BCM_SF2_REGS_H
13
14/* Register set relative to 'REG' */
15#define REG_SWITCH_CNTRL 0x00
16#define MDIO_MASTER_SEL (1 << 0)
17
18#define REG_SWITCH_STATUS 0x04
19#define REG_DIR_DATA_WRITE 0x08
20#define REG_DIR_DATA_READ 0x0C
21
22#define REG_SWITCH_REVISION 0x18
23#define SF2_REV_MASK 0xffff
24#define SWITCH_TOP_REV_SHIFT 16
25#define SWITCH_TOP_REV_MASK 0xffff
26
27#define REG_PHY_REVISION 0x1C
28
29#define REG_SPHY_CNTRL 0x2C
30#define IDDQ_BIAS (1 << 0)
31#define EXT_PWR_DOWN (1 << 1)
32#define FORCE_DLL_EN (1 << 2)
33#define IDDQ_GLOBAL_PWR (1 << 3)
34#define CK25_DIS (1 << 4)
35#define PHY_RESET (1 << 5)
36#define PHY_PHYAD_SHIFT 8
37#define PHY_PHYAD_MASK 0x1F
38
39#define REG_RGMII_0_BASE 0x34
40#define REG_RGMII_CNTRL 0x00
41#define REG_RGMII_IB_STATUS 0x04
42#define REG_RGMII_RX_CLOCK_DELAY_CNTRL 0x08
43#define REG_RGMII_CNTRL_SIZE 0x0C
44#define REG_RGMII_CNTRL_P(x) (REG_RGMII_0_BASE + \
45 ((x) * REG_RGMII_CNTRL_SIZE))
46/* Relative to REG_RGMII_CNTRL */
47#define RGMII_MODE_EN (1 << 0)
48#define ID_MODE_DIS (1 << 1)
49#define PORT_MODE_SHIFT 2
50#define INT_EPHY (0 << PORT_MODE_SHIFT)
51#define INT_GPHY (1 << PORT_MODE_SHIFT)
52#define EXT_EPHY (2 << PORT_MODE_SHIFT)
53#define EXT_GPHY (3 << PORT_MODE_SHIFT)
54#define EXT_REVMII (4 << PORT_MODE_SHIFT)
55#define PORT_MODE_MASK 0x7
56#define RVMII_REF_SEL (1 << 5)
57#define RX_PAUSE_EN (1 << 6)
58#define TX_PAUSE_EN (1 << 7)
59#define TX_CLK_STOP_EN (1 << 8)
60#define LPI_COUNT_SHIFT 9
61#define LPI_COUNT_MASK 0x3F
62
63/* Register set relative to 'INTRL2_0' and 'INTRL2_1' */
64#define INTRL2_CPU_STATUS 0x00
65#define INTRL2_CPU_SET 0x04
66#define INTRL2_CPU_CLEAR 0x08
67#define INTRL2_CPU_MASK_STATUS 0x0c
68#define INTRL2_CPU_MASK_SET 0x10
69#define INTRL2_CPU_MASK_CLEAR 0x14
70
71/* Shared INTRL2_0 and INTRL2_ interrupt sources macros */
72#define P_LINK_UP_IRQ(x) (1 << (0 + (x)))
73#define P_LINK_DOWN_IRQ(x) (1 << (1 + (x)))
74#define P_ENERGY_ON_IRQ(x) (1 << (2 + (x)))
75#define P_ENERGY_OFF_IRQ(x) (1 << (3 + (x)))
76#define P_GPHY_IRQ(x) (1 << (4 + (x)))
77#define P_NUM_IRQ 5
78#define P_IRQ_MASK(x) (P_LINK_UP_IRQ((x)) | \
79 P_LINK_DOWN_IRQ((x)) | \
80 P_ENERGY_ON_IRQ((x)) | \
81 P_ENERGY_OFF_IRQ((x)) | \
82 P_GPHY_IRQ((x)))
83
84/* INTRL2_0 interrupt sources */
85#define P0_IRQ_OFF 0
86#define MEM_DOUBLE_IRQ (1 << 5)
87#define EEE_LPI_IRQ (1 << 6)
88#define P5_CPU_WAKE_IRQ (1 << 7)
89#define P8_CPU_WAKE_IRQ (1 << 8)
90#define P7_CPU_WAKE_IRQ (1 << 9)
91#define IEEE1588_IRQ (1 << 10)
92#define MDIO_ERR_IRQ (1 << 11)
93#define MDIO_DONE_IRQ (1 << 12)
94#define GISB_ERR_IRQ (1 << 13)
95#define UBUS_ERR_IRQ (1 << 14)
96#define FAILOVER_ON_IRQ (1 << 15)
97#define FAILOVER_OFF_IRQ (1 << 16)
98#define TCAM_SOFT_ERR_IRQ (1 << 17)
99
100/* INTRL2_1 interrupt sources */
101#define P7_IRQ_OFF 0
102#define P_IRQ_OFF(x) ((6 - (x)) * P_NUM_IRQ)
103
104/* Register set relative to 'CORE' */
105#define CORE_G_PCTL_PORT0 0x00000
106#define CORE_G_PCTL_PORT(x) (CORE_G_PCTL_PORT0 + (x * 0x4))
107#define CORE_IMP_CTL 0x00020
108#define RX_DIS (1 << 0)
109#define TX_DIS (1 << 1)
110#define RX_BCST_EN (1 << 2)
111#define RX_MCST_EN (1 << 3)
112#define RX_UCST_EN (1 << 4)
113#define G_MISTP_STATE_SHIFT 5
114#define G_MISTP_NO_STP (0 << G_MISTP_STATE_SHIFT)
115#define G_MISTP_DIS_STATE (1 << G_MISTP_STATE_SHIFT)
116#define G_MISTP_BLOCK_STATE (2 << G_MISTP_STATE_SHIFT)
117#define G_MISTP_LISTEN_STATE (3 << G_MISTP_STATE_SHIFT)
118#define G_MISTP_LEARN_STATE (4 << G_MISTP_STATE_SHIFT)
119#define G_MISTP_FWD_STATE (5 << G_MISTP_STATE_SHIFT)
120#define G_MISTP_STATE_MASK 0x7
121
122#define CORE_SWMODE 0x0002c
123#define SW_FWDG_MODE (1 << 0)
124#define SW_FWDG_EN (1 << 1)
125#define RTRY_LMT_DIS (1 << 2)
126
127#define CORE_STS_OVERRIDE_IMP 0x00038
128#define GMII_SPEED_UP_2G (1 << 6)
129#define MII_SW_OR (1 << 7)
130
131#define CORE_NEW_CTRL 0x00084
132#define IP_MC (1 << 0)
133#define OUTRANGEERR_DISCARD (1 << 1)
134#define INRANGEERR_DISCARD (1 << 2)
135#define CABLE_DIAG_LEN (1 << 3)
136#define OVERRIDE_AUTO_PD_WAR (1 << 4)
137#define EN_AUTO_PD_WAR (1 << 5)
138#define UC_FWD_EN (1 << 6)
139#define MC_FWD_EN (1 << 7)
140
141#define CORE_SWITCH_CTRL 0x00088
142#define MII_DUMB_FWDG_EN (1 << 6)
143
144#define CORE_SFT_LRN_CTRL 0x000f8
145#define SW_LEARN_CNTL(x) (1 << (x))
146
147#define CORE_STS_OVERRIDE_GMIIP_PORT(x) (0x160 + (x) * 4)
148#define LINK_STS (1 << 0)
149#define DUPLX_MODE (1 << 1)
150#define SPEED_SHIFT 2
151#define SPEED_MASK 0x3
152#define RXFLOW_CNTL (1 << 4)
153#define TXFLOW_CNTL (1 << 5)
154#define SW_OVERRIDE (1 << 6)
155
156#define CORE_WATCHDOG_CTRL 0x001e4
157#define SOFTWARE_RESET (1 << 7)
158#define EN_CHIP_RST (1 << 6)
159#define EN_SW_RESET (1 << 4)
160
161#define CORE_LNKSTS 0x00400
162#define LNK_STS_MASK 0x1ff
163
164#define CORE_SPDSTS 0x00410
165#define SPDSTS_10 0
166#define SPDSTS_100 1
167#define SPDSTS_1000 2
168#define SPDSTS_SHIFT 2
169#define SPDSTS_MASK 0x3
170
171#define CORE_DUPSTS 0x00420
172#define CORE_DUPSTS_MASK 0x1ff
173
174#define CORE_PAUSESTS 0x00428
175#define PAUSESTS_TX_PAUSE_SHIFT 9
176
177#define CORE_GMNCFGCFG 0x0800
178#define RST_MIB_CNT (1 << 0)
179#define RXBPDU_EN (1 << 1)
180
181#define CORE_IMP0_PRT_ID 0x0804
182
183#define CORE_BRCM_HDR_CTRL 0x0080c
184#define BRCM_HDR_EN_P8 (1 << 0)
185#define BRCM_HDR_EN_P5 (1 << 1)
186#define BRCM_HDR_EN_P7 (1 << 2)
187
188#define CORE_BRCM_HDR_CTRL2 0x0828
189
190#define CORE_HL_PRTC_CTRL 0x0940
191#define ARP_EN (1 << 0)
192#define RARP_EN (1 << 1)
193#define DHCP_EN (1 << 2)
194#define ICMPV4_EN (1 << 3)
195#define ICMPV6_EN (1 << 4)
196#define ICMPV6_FWD_MODE (1 << 5)
197#define IGMP_DIP_EN (1 << 8)
198#define IGMP_RPTLVE_EN (1 << 9)
199#define IGMP_RTPLVE_FWD_MODE (1 << 10)
200#define IGMP_QRY_EN (1 << 11)
201#define IGMP_QRY_FWD_MODE (1 << 12)
202#define IGMP_UKN_EN (1 << 13)
203#define IGMP_UKN_FWD_MODE (1 << 14)
204#define MLD_RPTDONE_EN (1 << 15)
205#define MLD_RPTDONE_FWD_MODE (1 << 16)
206#define MLD_QRY_EN (1 << 17)
207#define MLD_QRY_FWD_MODE (1 << 18)
208
209#define CORE_RST_MIB_CNT_EN 0x0950
210
211#define CORE_BRCM_HDR_RX_DIS 0x0980
212#define CORE_BRCM_HDR_TX_DIS 0x0988
213
214#define CORE_MEM_PSM_VDD_CTRL 0x2380
215#define P_TXQ_PSM_VDD_SHIFT 2
216#define P_TXQ_PSM_VDD_MASK 0x3
217#define P_TXQ_PSM_VDD(x) (P_TXQ_PSM_VDD_MASK << \
218 ((x) * P_TXQ_PSM_VDD_SHIFT))
219
220#define CORE_P0_MIB_OFFSET 0x8000
221#define P_MIB_SIZE 0x400
222#define CORE_P_MIB_OFFSET(x) (CORE_P0_MIB_OFFSET + (x) * P_MIB_SIZE)
223
224#define CORE_PORT_VLAN_CTL_PORT(x) (0xc400 + ((x) * 0x8))
225#define PORT_VLAN_CTRL_MASK 0x1ff
226
227#endif /* __BCM_SF2_REGS_H */