blob: 4f32b8a530bf7815123729b358e31ca3bdaf8b1e [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>
24#include <net/dsa.h>
Florian Fainelli96e65d72014-09-18 17:31:25 -070025#include <linux/ethtool.h>
Florian Fainelli12f460f2015-02-24 13:15:34 -080026#include <linux/if_bridge.h>
Florian Fainelliaafc66f2015-06-10 18:08:01 -070027#include <linux/brcmphy.h>
Florian Fainelli680060d2015-10-23 11:38:07 -070028#include <linux/etherdevice.h>
29#include <net/switchdev.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070030
31#include "bcm_sf2.h"
32#include "bcm_sf2_regs.h"
33
34/* String, offset, and register size in bytes if different from 4 bytes */
35static const struct bcm_sf2_hw_stats bcm_sf2_mib[] = {
36 { "TxOctets", 0x000, 8 },
37 { "TxDropPkts", 0x020 },
38 { "TxQPKTQ0", 0x030 },
39 { "TxBroadcastPkts", 0x040 },
40 { "TxMulticastPkts", 0x050 },
41 { "TxUnicastPKts", 0x060 },
42 { "TxCollisions", 0x070 },
43 { "TxSingleCollision", 0x080 },
44 { "TxMultipleCollision", 0x090 },
45 { "TxDeferredCollision", 0x0a0 },
46 { "TxLateCollision", 0x0b0 },
47 { "TxExcessiveCollision", 0x0c0 },
48 { "TxFrameInDisc", 0x0d0 },
49 { "TxPausePkts", 0x0e0 },
50 { "TxQPKTQ1", 0x0f0 },
51 { "TxQPKTQ2", 0x100 },
52 { "TxQPKTQ3", 0x110 },
53 { "TxQPKTQ4", 0x120 },
54 { "TxQPKTQ5", 0x130 },
55 { "RxOctets", 0x140, 8 },
56 { "RxUndersizePkts", 0x160 },
57 { "RxPausePkts", 0x170 },
58 { "RxPkts64Octets", 0x180 },
59 { "RxPkts65to127Octets", 0x190 },
60 { "RxPkts128to255Octets", 0x1a0 },
61 { "RxPkts256to511Octets", 0x1b0 },
62 { "RxPkts512to1023Octets", 0x1c0 },
63 { "RxPkts1024toMaxPktsOctets", 0x1d0 },
64 { "RxOversizePkts", 0x1e0 },
65 { "RxJabbers", 0x1f0 },
66 { "RxAlignmentErrors", 0x200 },
67 { "RxFCSErrors", 0x210 },
68 { "RxGoodOctets", 0x220, 8 },
69 { "RxDropPkts", 0x240 },
70 { "RxUnicastPkts", 0x250 },
71 { "RxMulticastPkts", 0x260 },
72 { "RxBroadcastPkts", 0x270 },
73 { "RxSAChanges", 0x280 },
74 { "RxFragments", 0x290 },
75 { "RxJumboPkt", 0x2a0 },
76 { "RxSymblErr", 0x2b0 },
77 { "InRangeErrCount", 0x2c0 },
78 { "OutRangeErrCount", 0x2d0 },
79 { "EEELpiEvent", 0x2e0 },
80 { "EEELpiDuration", 0x2f0 },
81 { "RxDiscard", 0x300, 8 },
82 { "TxQPKTQ6", 0x320 },
83 { "TxQPKTQ7", 0x330 },
84 { "TxPkts64Octets", 0x340 },
85 { "TxPkts65to127Octets", 0x350 },
86 { "TxPkts128to255Octets", 0x360 },
87 { "TxPkts256to511Ocets", 0x370 },
88 { "TxPkts512to1023Ocets", 0x380 },
89 { "TxPkts1024toMaxPktOcets", 0x390 },
90};
91
92#define BCM_SF2_STATS_SIZE ARRAY_SIZE(bcm_sf2_mib)
93
94static void bcm_sf2_sw_get_strings(struct dsa_switch *ds,
95 int port, uint8_t *data)
96{
97 unsigned int i;
98
99 for (i = 0; i < BCM_SF2_STATS_SIZE; i++)
100 memcpy(data + i * ETH_GSTRING_LEN,
101 bcm_sf2_mib[i].string, ETH_GSTRING_LEN);
102}
103
104static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds,
105 int port, uint64_t *data)
106{
107 struct bcm_sf2_priv *priv = ds_to_priv(ds);
108 const struct bcm_sf2_hw_stats *s;
109 unsigned int i;
110 u64 val = 0;
111 u32 offset;
112
113 mutex_lock(&priv->stats_mutex);
114
115 /* Now fetch the per-port counters */
116 for (i = 0; i < BCM_SF2_STATS_SIZE; i++) {
117 s = &bcm_sf2_mib[i];
118
119 /* Do a latched 64-bit read if needed */
120 offset = s->reg + CORE_P_MIB_OFFSET(port);
121 if (s->sizeof_stat == 8)
122 val = core_readq(priv, offset);
123 else
124 val = core_readl(priv, offset);
125
126 data[i] = (u64)val;
127 }
128
129 mutex_unlock(&priv->stats_mutex);
130}
131
132static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds)
133{
134 return BCM_SF2_STATS_SIZE;
135}
136
Alexander Duyckb4d23942014-09-15 13:00:27 -0400137static char *bcm_sf2_sw_probe(struct device *host_dev, int sw_addr)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700138{
139 return "Broadcom Starfighter 2";
140}
141
Florian Fainellib6d045d2014-09-24 17:05:20 -0700142static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700143{
144 struct bcm_sf2_priv *priv = ds_to_priv(ds);
145 unsigned int i;
Florian Fainellib6d045d2014-09-24 17:05:20 -0700146 u32 reg;
147
148 /* Enable the IMP Port to be in the same VLAN as the other ports
149 * on a per-port basis such that we only have Port i and IMP in
150 * the same VLAN.
151 */
152 for (i = 0; i < priv->hw_params.num_ports; i++) {
153 if (!((1 << i) & ds->phys_port_mask))
154 continue;
155
156 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
157 reg |= (1 << cpu_port);
158 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
159 }
160}
161
162static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
163{
164 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700165 u32 reg, val;
166
167 /* Enable the port memories */
168 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
169 reg &= ~P_TXQ_PSM_VDD(port);
170 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
171
172 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
173 reg = core_readl(priv, CORE_IMP_CTL);
174 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
175 reg &= ~(RX_DIS | TX_DIS);
176 core_writel(priv, reg, CORE_IMP_CTL);
177
178 /* Enable forwarding */
179 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
180
181 /* Enable IMP port in dumb mode */
182 reg = core_readl(priv, CORE_SWITCH_CTRL);
183 reg |= MII_DUMB_FWDG_EN;
184 core_writel(priv, reg, CORE_SWITCH_CTRL);
185
186 /* Resolve which bit controls the Broadcom tag */
187 switch (port) {
188 case 8:
189 val = BRCM_HDR_EN_P8;
190 break;
191 case 7:
192 val = BRCM_HDR_EN_P7;
193 break;
194 case 5:
195 val = BRCM_HDR_EN_P5;
196 break;
197 default:
198 val = 0;
199 break;
200 }
201
202 /* Enable Broadcom tags for IMP port */
203 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
204 reg |= val;
205 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
206
207 /* Enable reception Broadcom tag for CPU TX (switch RX) to
208 * allow us to tag outgoing frames
209 */
210 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
211 reg &= ~(1 << port);
212 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
213
214 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
215 * allow delivering frames to the per-port net_devices
216 */
217 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
218 reg &= ~(1 << port);
219 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
220
221 /* Force link status for IMP port */
222 reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
223 reg |= (MII_SW_OR | LINK_STS);
224 core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700225}
226
Florian Fainelli450b05c2014-09-24 17:05:22 -0700227static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
228{
229 struct bcm_sf2_priv *priv = ds_to_priv(ds);
230 u32 reg;
231
232 reg = core_readl(priv, CORE_EEE_EN_CTRL);
233 if (enable)
234 reg |= 1 << port;
235 else
236 reg &= ~(1 << port);
237 core_writel(priv, reg, CORE_EEE_EN_CTRL);
238}
239
Florian Fainellib0836682015-02-05 11:40:41 -0800240static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
241{
242 struct bcm_sf2_priv *priv = ds_to_priv(ds);
243 u32 reg;
244
Florian Fainelli9af197a2015-02-05 11:40:42 -0800245 reg = reg_readl(priv, REG_SPHY_CNTRL);
246 if (enable) {
247 reg |= PHY_RESET;
248 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
249 reg_writel(priv, reg, REG_SPHY_CNTRL);
250 udelay(21);
251 reg = reg_readl(priv, REG_SPHY_CNTRL);
252 reg &= ~PHY_RESET;
253 } else {
254 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
255 reg_writel(priv, reg, REG_SPHY_CNTRL);
256 mdelay(1);
257 reg |= CK25_DIS;
258 }
259 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800260
Florian Fainelli9af197a2015-02-05 11:40:42 -0800261 /* Use PHY-driven LED signaling */
262 if (!enable) {
263 reg = reg_readl(priv, REG_LED_CNTRL(0));
264 reg |= SPDLNK_SRC_SEL;
265 reg_writel(priv, reg, REG_LED_CNTRL(0));
266 }
Florian Fainellib0836682015-02-05 11:40:41 -0800267}
268
Florian Fainellib6d045d2014-09-24 17:05:20 -0700269static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
270 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700271{
272 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700273 s8 cpu_port = ds->dst[ds->index].cpu_port;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700274 u32 reg;
275
276 /* Clear the memory power down */
277 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
278 reg &= ~P_TXQ_PSM_VDD(port);
279 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
280
281 /* Clear the Rx and Tx disable bits and set to no spanning tree */
282 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
283
Florian Fainelli9af197a2015-02-05 11:40:42 -0800284 /* Re-enable the GPHY and re-apply workarounds */
285 if (port == 0 && priv->hw_params.num_gphy == 1) {
286 bcm_sf2_gphy_enable_set(ds, true);
287 if (phy) {
288 /* if phy_stop() has been called before, phy
289 * will be in halted state, and phy_start()
290 * will call resume.
291 *
292 * the resume path does not configure back
293 * autoneg settings, and since we hard reset
294 * the phy manually here, we need to reset the
295 * state machine also.
296 */
297 phy->state = PHY_READY;
298 phy_init_hw(phy);
299 }
300 }
301
Florian Fainelli246d7f72014-08-27 17:04:56 -0700302 /* Enable port 7 interrupts to get notified */
303 if (port == 7)
304 intrl2_1_mask_clear(priv, P_IRQ_MASK(P7_IRQ_OFF));
305
Florian Fainelli12f460f2015-02-24 13:15:34 -0800306 /* Set this port, and only this one to be in the default VLAN,
307 * if member of a bridge, restore its membership prior to
308 * bringing down this port.
309 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700310 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
311 reg &= ~PORT_VLAN_CTRL_MASK;
312 reg |= (1 << port);
Florian Fainelli12f460f2015-02-24 13:15:34 -0800313 reg |= priv->port_sts[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700314 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700315
316 bcm_sf2_imp_vlan_setup(ds, cpu_port);
317
Florian Fainelli450b05c2014-09-24 17:05:22 -0700318 /* If EEE was enabled, restore it */
319 if (priv->port_sts[port].eee.eee_enabled)
320 bcm_sf2_eee_enable_set(ds, port, true);
321
Florian Fainellib6d045d2014-09-24 17:05:20 -0700322 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700323}
324
Florian Fainellib6d045d2014-09-24 17:05:20 -0700325static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
326 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700327{
328 struct bcm_sf2_priv *priv = ds_to_priv(ds);
329 u32 off, reg;
330
Florian Fainelli96e65d72014-09-18 17:31:25 -0700331 if (priv->wol_ports_mask & (1 << port))
332 return;
333
Florian Fainellib6d045d2014-09-24 17:05:20 -0700334 if (port == 7) {
335 intrl2_1_mask_set(priv, P_IRQ_MASK(P7_IRQ_OFF));
336 intrl2_1_writel(priv, P_IRQ_MASK(P7_IRQ_OFF), INTRL2_CPU_CLEAR);
337 }
338
Florian Fainelli9af197a2015-02-05 11:40:42 -0800339 if (port == 0 && priv->hw_params.num_gphy == 1)
340 bcm_sf2_gphy_enable_set(ds, false);
341
Florian Fainelli246d7f72014-08-27 17:04:56 -0700342 if (dsa_is_cpu_port(ds, port))
343 off = CORE_IMP_CTL;
344 else
345 off = CORE_G_PCTL_PORT(port);
346
347 reg = core_readl(priv, off);
348 reg |= RX_DIS | TX_DIS;
349 core_writel(priv, reg, off);
350
351 /* Power down the port memory */
352 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
353 reg |= P_TXQ_PSM_VDD(port);
354 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
355}
356
Florian Fainelli450b05c2014-09-24 17:05:22 -0700357/* Returns 0 if EEE was not enabled, or 1 otherwise
358 */
359static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
360 struct phy_device *phy)
361{
362 struct bcm_sf2_priv *priv = ds_to_priv(ds);
363 struct ethtool_eee *p = &priv->port_sts[port].eee;
364 int ret;
365
366 p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
367
368 ret = phy_init_eee(phy, 0);
369 if (ret)
370 return 0;
371
372 bcm_sf2_eee_enable_set(ds, port, true);
373
374 return 1;
375}
376
377static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
378 struct ethtool_eee *e)
379{
380 struct bcm_sf2_priv *priv = ds_to_priv(ds);
381 struct ethtool_eee *p = &priv->port_sts[port].eee;
382 u32 reg;
383
384 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
385 e->eee_enabled = p->eee_enabled;
386 e->eee_active = !!(reg & (1 << port));
387
388 return 0;
389}
390
391static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
392 struct phy_device *phydev,
393 struct ethtool_eee *e)
394{
395 struct bcm_sf2_priv *priv = ds_to_priv(ds);
396 struct ethtool_eee *p = &priv->port_sts[port].eee;
397
398 p->eee_enabled = e->eee_enabled;
399
400 if (!p->eee_enabled) {
401 bcm_sf2_eee_enable_set(ds, port, false);
402 } else {
403 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
404 if (!p->eee_enabled)
405 return -EOPNOTSUPP;
406 }
407
408 return 0;
409}
410
Florian Fainelli12f460f2015-02-24 13:15:34 -0800411/* Fast-ageing of ARL entries for a given port, equivalent to an ARL
412 * flush for that port.
413 */
414static int bcm_sf2_sw_fast_age_port(struct dsa_switch *ds, int port)
415{
416 struct bcm_sf2_priv *priv = ds_to_priv(ds);
417 unsigned int timeout = 1000;
418 u32 reg;
419
420 core_writel(priv, port, CORE_FAST_AGE_PORT);
421
422 reg = core_readl(priv, CORE_FAST_AGE_CTRL);
Florian Fainelli39797a22015-09-05 13:07:27 -0700423 reg |= EN_AGE_PORT | EN_AGE_DYNAMIC | FAST_AGE_STR_DONE;
Florian Fainelli12f460f2015-02-24 13:15:34 -0800424 core_writel(priv, reg, CORE_FAST_AGE_CTRL);
425
426 do {
427 reg = core_readl(priv, CORE_FAST_AGE_CTRL);
428 if (!(reg & FAST_AGE_STR_DONE))
429 break;
430
431 cpu_relax();
432 } while (timeout--);
433
434 if (!timeout)
435 return -ETIMEDOUT;
436
Florian Fainelli39797a22015-09-05 13:07:27 -0700437 core_writel(priv, 0, CORE_FAST_AGE_CTRL);
438
Florian Fainelli12f460f2015-02-24 13:15:34 -0800439 return 0;
440}
441
442static int bcm_sf2_sw_br_join(struct dsa_switch *ds, int port,
443 u32 br_port_mask)
444{
445 struct bcm_sf2_priv *priv = ds_to_priv(ds);
446 unsigned int i;
447 u32 reg, p_ctl;
448
449 p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
450
451 for (i = 0; i < priv->hw_params.num_ports; i++) {
452 if (!((1 << i) & br_port_mask))
453 continue;
454
455 /* Add this local port to the remote port VLAN control
456 * membership and update the remote port bitmask
457 */
458 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
459 reg |= 1 << port;
460 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
461 priv->port_sts[i].vlan_ctl_mask = reg;
462
463 p_ctl |= 1 << i;
464 }
465
466 /* Configure the local port VLAN control membership to include
467 * remote ports and update the local port bitmask
468 */
469 core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
470 priv->port_sts[port].vlan_ctl_mask = p_ctl;
471
472 return 0;
473}
474
475static int bcm_sf2_sw_br_leave(struct dsa_switch *ds, int port,
476 u32 br_port_mask)
477{
478 struct bcm_sf2_priv *priv = ds_to_priv(ds);
479 unsigned int i;
480 u32 reg, p_ctl;
481
482 p_ctl = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
483
484 for (i = 0; i < priv->hw_params.num_ports; i++) {
485 /* Don't touch the remaining ports */
486 if (!((1 << i) & br_port_mask))
487 continue;
488
489 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
490 reg &= ~(1 << port);
491 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
492 priv->port_sts[port].vlan_ctl_mask = reg;
493
494 /* Prevent self removal to preserve isolation */
495 if (port != i)
496 p_ctl &= ~(1 << i);
497 }
498
499 core_writel(priv, p_ctl, CORE_PORT_VLAN_CTL_PORT(port));
500 priv->port_sts[port].vlan_ctl_mask = p_ctl;
501
502 return 0;
503}
504
505static int bcm_sf2_sw_br_set_stp_state(struct dsa_switch *ds, int port,
506 u8 state)
507{
508 struct bcm_sf2_priv *priv = ds_to_priv(ds);
509 u8 hw_state, cur_hw_state;
510 int ret = 0;
511 u32 reg;
512
513 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
Florian Fainelli39797a22015-09-05 13:07:27 -0700514 cur_hw_state = reg & (G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
Florian Fainelli12f460f2015-02-24 13:15:34 -0800515
516 switch (state) {
517 case BR_STATE_DISABLED:
518 hw_state = G_MISTP_DIS_STATE;
519 break;
520 case BR_STATE_LISTENING:
521 hw_state = G_MISTP_LISTEN_STATE;
522 break;
523 case BR_STATE_LEARNING:
524 hw_state = G_MISTP_LEARN_STATE;
525 break;
526 case BR_STATE_FORWARDING:
527 hw_state = G_MISTP_FWD_STATE;
528 break;
529 case BR_STATE_BLOCKING:
530 hw_state = G_MISTP_BLOCK_STATE;
531 break;
532 default:
533 pr_err("%s: invalid STP state: %d\n", __func__, state);
534 return -EINVAL;
535 }
536
537 /* Fast-age ARL entries if we are moving a port from Learning or
Florian Fainelli39797a22015-09-05 13:07:27 -0700538 * Forwarding (cur_hw_state) state to Disabled, Blocking or Listening
539 * state (hw_state)
Florian Fainelli12f460f2015-02-24 13:15:34 -0800540 */
541 if (cur_hw_state != hw_state) {
Florian Fainelli39797a22015-09-05 13:07:27 -0700542 if (cur_hw_state >= G_MISTP_LEARN_STATE &&
543 hw_state <= G_MISTP_LISTEN_STATE) {
Florian Fainelli12f460f2015-02-24 13:15:34 -0800544 ret = bcm_sf2_sw_fast_age_port(ds, port);
545 if (ret) {
546 pr_err("%s: fast-ageing failed\n", __func__);
547 return ret;
548 }
549 }
550 }
551
552 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
553 reg &= ~(G_MISTP_STATE_MASK << G_MISTP_STATE_SHIFT);
554 reg |= hw_state;
555 core_writel(priv, reg, CORE_G_PCTL_PORT(port));
556
557 return 0;
558}
559
Florian Fainelli680060d2015-10-23 11:38:07 -0700560/* Address Resolution Logic routines */
561static int bcm_sf2_arl_op_wait(struct bcm_sf2_priv *priv)
562{
563 unsigned int timeout = 10;
564 u32 reg;
565
566 do {
567 reg = core_readl(priv, CORE_ARLA_RWCTL);
568 if (!(reg & ARL_STRTDN))
569 return 0;
570
571 usleep_range(1000, 2000);
572 } while (timeout--);
573
574 return -ETIMEDOUT;
575}
576
577static int bcm_sf2_arl_rw_op(struct bcm_sf2_priv *priv, unsigned int op)
578{
579 u32 cmd;
580
581 if (op > ARL_RW)
582 return -EINVAL;
583
584 cmd = core_readl(priv, CORE_ARLA_RWCTL);
585 cmd &= ~IVL_SVL_SELECT;
586 cmd |= ARL_STRTDN;
587 if (op)
588 cmd |= ARL_RW;
589 else
590 cmd &= ~ARL_RW;
591 core_writel(priv, cmd, CORE_ARLA_RWCTL);
592
593 return bcm_sf2_arl_op_wait(priv);
594}
595
596static int bcm_sf2_arl_read(struct bcm_sf2_priv *priv, u64 mac,
597 u16 vid, struct bcm_sf2_arl_entry *ent, u8 *idx,
598 bool is_valid)
599{
600 unsigned int i;
601 int ret;
602
603 ret = bcm_sf2_arl_op_wait(priv);
604 if (ret)
605 return ret;
606
607 /* Read the 4 bins */
608 for (i = 0; i < 4; i++) {
609 u64 mac_vid;
610 u32 fwd_entry;
611
612 mac_vid = core_readq(priv, CORE_ARLA_MACVID_ENTRY(i));
613 fwd_entry = core_readl(priv, CORE_ARLA_FWD_ENTRY(i));
614 bcm_sf2_arl_to_entry(ent, mac_vid, fwd_entry);
615
616 if (ent->is_valid && is_valid) {
617 *idx = i;
618 return 0;
619 }
620
621 /* This is the MAC we just deleted */
622 if (!is_valid && (mac_vid & mac))
623 return 0;
624 }
625
626 return -ENOENT;
627}
628
629static int bcm_sf2_arl_op(struct bcm_sf2_priv *priv, int op, int port,
630 const unsigned char *addr, u16 vid, bool is_valid)
631{
632 struct bcm_sf2_arl_entry ent;
633 u32 fwd_entry;
634 u64 mac, mac_vid = 0;
635 u8 idx = 0;
636 int ret;
637
638 /* Convert the array into a 64-bit MAC */
639 mac = bcm_sf2_mac_to_u64(addr);
640
641 /* Perform a read for the given MAC and VID */
642 core_writeq(priv, mac, CORE_ARLA_MAC);
643 core_writel(priv, vid, CORE_ARLA_VID);
644
645 /* Issue a read operation for this MAC */
646 ret = bcm_sf2_arl_rw_op(priv, 1);
647 if (ret)
648 return ret;
649
650 ret = bcm_sf2_arl_read(priv, mac, vid, &ent, &idx, is_valid);
651 /* If this is a read, just finish now */
652 if (op)
653 return ret;
654
655 /* We could not find a matching MAC, so reset to a new entry */
656 if (ret) {
657 fwd_entry = 0;
658 idx = 0;
659 }
660
661 memset(&ent, 0, sizeof(ent));
662 ent.port = port;
663 ent.is_valid = is_valid;
664 ent.vid = vid;
665 ent.is_static = true;
666 memcpy(ent.mac, addr, ETH_ALEN);
667 bcm_sf2_arl_from_entry(&mac_vid, &fwd_entry, &ent);
668
669 core_writeq(priv, mac_vid, CORE_ARLA_MACVID_ENTRY(idx));
670 core_writel(priv, fwd_entry, CORE_ARLA_FWD_ENTRY(idx));
671
672 ret = bcm_sf2_arl_rw_op(priv, 0);
673 if (ret)
674 return ret;
675
676 /* Re-read the entry to check */
677 return bcm_sf2_arl_read(priv, mac, vid, &ent, &idx, is_valid);
678}
679
680static int bcm_sf2_sw_fdb_prepare(struct dsa_switch *ds, int port,
681 const struct switchdev_obj_port_fdb *fdb,
682 struct switchdev_trans *trans)
683{
684 /* We do not need to do anything specific here yet */
685 return 0;
686}
687
688static int bcm_sf2_sw_fdb_add(struct dsa_switch *ds, int port,
689 const struct switchdev_obj_port_fdb *fdb,
690 struct switchdev_trans *trans)
691{
692 struct bcm_sf2_priv *priv = ds_to_priv(ds);
693
694 return bcm_sf2_arl_op(priv, 0, port, fdb->addr, fdb->vid, true);
695}
696
697static int bcm_sf2_sw_fdb_del(struct dsa_switch *ds, int port,
698 const struct switchdev_obj_port_fdb *fdb)
699{
700 struct bcm_sf2_priv *priv = ds_to_priv(ds);
701
702 return bcm_sf2_arl_op(priv, 0, port, fdb->addr, fdb->vid, false);
703}
704
705static int bcm_sf2_arl_search_wait(struct bcm_sf2_priv *priv)
706{
707 unsigned timeout = 1000;
708 u32 reg;
709
710 do {
711 reg = core_readl(priv, CORE_ARLA_SRCH_CTL);
712 if (!(reg & ARLA_SRCH_STDN))
713 return 0;
714
715 if (reg & ARLA_SRCH_VLID)
716 return 0;
717
718 usleep_range(1000, 2000);
719 } while (timeout--);
720
721 return -ETIMEDOUT;
722}
723
724static void bcm_sf2_arl_search_rd(struct bcm_sf2_priv *priv, u8 idx,
725 struct bcm_sf2_arl_entry *ent)
726{
727 u64 mac_vid;
728 u32 fwd_entry;
729
730 mac_vid = core_readq(priv, CORE_ARLA_SRCH_RSLT_MACVID(idx));
731 fwd_entry = core_readl(priv, CORE_ARLA_SRCH_RSLT(idx));
732 bcm_sf2_arl_to_entry(ent, mac_vid, fwd_entry);
733}
734
735static int bcm_sf2_sw_fdb_copy(struct net_device *dev, int port,
736 const struct bcm_sf2_arl_entry *ent,
737 struct switchdev_obj_port_fdb *fdb,
738 int (*cb)(struct switchdev_obj *obj))
739{
740 if (!ent->is_valid)
741 return 0;
742
743 if (port != ent->port)
744 return 0;
745
746 ether_addr_copy(fdb->addr, ent->mac);
747 fdb->vid = ent->vid;
748 fdb->ndm_state = ent->is_static ? NUD_NOARP : NUD_REACHABLE;
749
750 return cb(&fdb->obj);
751}
752
753static int bcm_sf2_sw_fdb_dump(struct dsa_switch *ds, int port,
754 struct switchdev_obj_port_fdb *fdb,
755 int (*cb)(struct switchdev_obj *obj))
756{
757 struct bcm_sf2_priv *priv = ds_to_priv(ds);
758 struct net_device *dev = ds->ports[port];
759 struct bcm_sf2_arl_entry results[2];
760 unsigned int count = 0;
761 int ret;
762
763 /* Start search operation */
764 core_writel(priv, ARLA_SRCH_STDN, CORE_ARLA_SRCH_CTL);
765
766 do {
767 ret = bcm_sf2_arl_search_wait(priv);
768 if (ret)
769 return ret;
770
771 /* Read both entries, then return their values back */
772 bcm_sf2_arl_search_rd(priv, 0, &results[0]);
773 ret = bcm_sf2_sw_fdb_copy(dev, port, &results[0], fdb, cb);
774 if (ret)
775 return ret;
776
777 bcm_sf2_arl_search_rd(priv, 1, &results[1]);
778 ret = bcm_sf2_sw_fdb_copy(dev, port, &results[1], fdb, cb);
779 if (ret)
780 return ret;
781
782 if (!results[0].is_valid && !results[1].is_valid)
783 break;
784
785 } while (count++ < CORE_ARLA_NUM_ENTRIES);
786
787 return 0;
788}
789
Florian Fainelli246d7f72014-08-27 17:04:56 -0700790static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
791{
792 struct bcm_sf2_priv *priv = dev_id;
793
794 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
795 ~priv->irq0_mask;
796 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
797
798 return IRQ_HANDLED;
799}
800
801static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
802{
803 struct bcm_sf2_priv *priv = dev_id;
804
805 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
806 ~priv->irq1_mask;
807 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
808
809 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
810 priv->port_sts[7].link = 1;
811 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
812 priv->port_sts[7].link = 0;
813
814 return IRQ_HANDLED;
815}
816
Florian Fainelli33f84612014-11-25 18:08:49 -0800817static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
818{
819 unsigned int timeout = 1000;
820 u32 reg;
821
822 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
823 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
824 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
825
826 do {
827 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
828 if (!(reg & SOFTWARE_RESET))
829 break;
830
831 usleep_range(1000, 2000);
832 } while (timeout-- > 0);
833
834 if (timeout == 0)
835 return -ETIMEDOUT;
836
837 return 0;
838}
839
Florian Fainelli691c9a82015-01-20 16:42:00 -0800840static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
841{
842 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
843 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
844 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
845 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
846 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
847 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
848}
849
Florian Fainelli246d7f72014-08-27 17:04:56 -0700850static int bcm_sf2_sw_setup(struct dsa_switch *ds)
851{
852 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
853 struct bcm_sf2_priv *priv = ds_to_priv(ds);
854 struct device_node *dn;
855 void __iomem **base;
856 unsigned int port;
857 unsigned int i;
858 u32 reg, rev;
859 int ret;
860
861 spin_lock_init(&priv->indir_lock);
862 mutex_init(&priv->stats_mutex);
863
864 /* All the interesting properties are at the parent device_node
865 * level
866 */
867 dn = ds->pd->of_node->parent;
868
869 priv->irq0 = irq_of_parse_and_map(dn, 0);
870 priv->irq1 = irq_of_parse_and_map(dn, 1);
871
872 base = &priv->core;
873 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
874 *base = of_iomap(dn, i);
875 if (*base == NULL) {
876 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainellia5660592014-11-25 18:08:48 -0800877 ret = -ENOMEM;
878 goto out_unmap;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700879 }
880 base++;
881 }
882
Florian Fainelli33f84612014-11-25 18:08:49 -0800883 ret = bcm_sf2_sw_rst(priv);
884 if (ret) {
885 pr_err("unable to software reset switch: %d\n", ret);
886 goto out_unmap;
887 }
888
Florian Fainelli246d7f72014-08-27 17:04:56 -0700889 /* Disable all interrupts and request them */
Florian Fainelli691c9a82015-01-20 16:42:00 -0800890 bcm_sf2_intr_disable(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700891
892 ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
893 "switch_0", priv);
894 if (ret < 0) {
895 pr_err("failed to request switch_0 IRQ\n");
896 goto out_unmap;
897 }
898
899 ret = request_irq(priv->irq1, bcm_sf2_switch_1_isr, 0,
900 "switch_1", priv);
901 if (ret < 0) {
902 pr_err("failed to request switch_1 IRQ\n");
903 goto out_free_irq0;
904 }
905
906 /* Reset the MIB counters */
907 reg = core_readl(priv, CORE_GMNCFGCFG);
908 reg |= RST_MIB_CNT;
909 core_writel(priv, reg, CORE_GMNCFGCFG);
910 reg &= ~RST_MIB_CNT;
911 core_writel(priv, reg, CORE_GMNCFGCFG);
912
913 /* Get the maximum number of ports for this switch */
914 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
915 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
916 priv->hw_params.num_ports = DSA_MAX_PORTS;
917
918 /* Assume a single GPHY setup if we can't read that property */
919 if (of_property_read_u32(dn, "brcm,num-gphy",
920 &priv->hw_params.num_gphy))
921 priv->hw_params.num_gphy = 1;
922
923 /* Enable all valid ports and disable those unused */
924 for (port = 0; port < priv->hw_params.num_ports; port++) {
925 /* IMP port receives special treatment */
926 if ((1 << port) & ds->phys_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700927 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700928 else if (dsa_is_cpu_port(ds, port))
929 bcm_sf2_imp_setup(ds, port);
930 else
Florian Fainellib6d045d2014-09-24 17:05:20 -0700931 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700932 }
933
934 /* Include the pseudo-PHY address and the broadcast PHY address to
Florian Fainellib8c6cd12015-07-15 16:09:32 -0700935 * divert reads towards our workaround. This is only required for
936 * 7445D0, since 7445E0 disconnects the internal switch pseudo-PHY such
937 * that we can use the regular SWITCH_MDIO master controller instead.
938 *
939 * By default, DSA initializes ds->phys_mii_mask to ds->phys_port_mask
940 * to have a 1:1 mapping between Port address and PHY address in order
941 * to utilize the slave_mii_bus instance to read from Port PHYs. This is
942 * not what we want here, so we initialize phys_mii_mask 0 to always
943 * utilize the "master" MDIO bus backed by the "mdio-unimac" driver.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700944 */
Florian Fainellib8c6cd12015-07-15 16:09:32 -0700945 if (of_machine_is_compatible("brcm,bcm7445d0"))
946 ds->phys_mii_mask |= ((1 << BRCM_PSEUDO_PHY_ADDR) | (1 << 0));
947 else
948 ds->phys_mii_mask = 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700949
950 rev = reg_readl(priv, REG_SWITCH_REVISION);
951 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
952 SWITCH_TOP_REV_MASK;
953 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
954
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700955 rev = reg_readl(priv, REG_PHY_REVISION);
956 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
957
Florian Fainelli246d7f72014-08-27 17:04:56 -0700958 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
959 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
960 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
961 priv->core, priv->irq0, priv->irq1);
962
963 return 0;
964
965out_free_irq0:
966 free_irq(priv->irq0, priv);
967out_unmap:
968 base = &priv->core;
969 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainellia5660592014-11-25 18:08:48 -0800970 if (*base)
971 iounmap(*base);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700972 base++;
973 }
974 return ret;
975}
976
977static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
978{
979 return 0;
980}
981
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700982static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
983{
984 struct bcm_sf2_priv *priv = ds_to_priv(ds);
985
986 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
987 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
988 * the REG_PHY_REVISION register layout is.
989 */
990
991 return priv->hw_params.gphy_rev;
992}
993
Florian Fainelli246d7f72014-08-27 17:04:56 -0700994static int bcm_sf2_sw_indir_rw(struct dsa_switch *ds, int op, int addr,
995 int regnum, u16 val)
996{
997 struct bcm_sf2_priv *priv = ds_to_priv(ds);
998 int ret = 0;
999 u32 reg;
1000
1001 reg = reg_readl(priv, REG_SWITCH_CNTRL);
1002 reg |= MDIO_MASTER_SEL;
1003 reg_writel(priv, reg, REG_SWITCH_CNTRL);
1004
1005 /* Page << 8 | offset */
1006 reg = 0x70;
1007 reg <<= 2;
1008 core_writel(priv, addr, reg);
1009
1010 /* Page << 8 | offset */
1011 reg = 0x80 << 8 | regnum << 1;
1012 reg <<= 2;
1013
1014 if (op)
1015 ret = core_readl(priv, reg);
1016 else
1017 core_writel(priv, val, reg);
1018
1019 reg = reg_readl(priv, REG_SWITCH_CNTRL);
1020 reg &= ~MDIO_MASTER_SEL;
1021 reg_writel(priv, reg, REG_SWITCH_CNTRL);
1022
1023 return ret & 0xffff;
1024}
1025
1026static int bcm_sf2_sw_phy_read(struct dsa_switch *ds, int addr, int regnum)
1027{
1028 /* Intercept reads from the MDIO broadcast address or Broadcom
1029 * pseudo-PHY address
1030 */
1031 switch (addr) {
1032 case 0:
Florian Fainelliaafc66f2015-06-10 18:08:01 -07001033 case BRCM_PSEUDO_PHY_ADDR:
Florian Fainelli246d7f72014-08-27 17:04:56 -07001034 return bcm_sf2_sw_indir_rw(ds, 1, addr, regnum, 0);
1035 default:
1036 return 0xffff;
1037 }
1038}
1039
1040static int bcm_sf2_sw_phy_write(struct dsa_switch *ds, int addr, int regnum,
1041 u16 val)
1042{
1043 /* Intercept writes to the MDIO broadcast address or Broadcom
1044 * pseudo-PHY address
1045 */
1046 switch (addr) {
1047 case 0:
Florian Fainelliaafc66f2015-06-10 18:08:01 -07001048 case BRCM_PSEUDO_PHY_ADDR:
Florian Fainelli246d7f72014-08-27 17:04:56 -07001049 bcm_sf2_sw_indir_rw(ds, 0, addr, regnum, val);
1050 break;
1051 }
1052
1053 return 0;
1054}
1055
1056static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
1057 struct phy_device *phydev)
1058{
1059 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1060 u32 id_mode_dis = 0, port_mode;
1061 const char *str = NULL;
1062 u32 reg;
1063
1064 switch (phydev->interface) {
1065 case PHY_INTERFACE_MODE_RGMII:
1066 str = "RGMII (no delay)";
1067 id_mode_dis = 1;
1068 case PHY_INTERFACE_MODE_RGMII_TXID:
1069 if (!str)
1070 str = "RGMII (TX delay)";
1071 port_mode = EXT_GPHY;
1072 break;
1073 case PHY_INTERFACE_MODE_MII:
1074 str = "MII";
1075 port_mode = EXT_EPHY;
1076 break;
1077 case PHY_INTERFACE_MODE_REVMII:
1078 str = "Reverse MII";
1079 port_mode = EXT_REVMII;
1080 break;
1081 default:
Florian Fainelli7de15572014-09-24 17:05:19 -07001082 /* All other PHYs: internal and MoCA */
1083 goto force_link;
1084 }
1085
1086 /* If the link is down, just disable the interface to conserve power */
1087 if (!phydev->link) {
1088 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
1089 reg &= ~RGMII_MODE_EN;
1090 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -07001091 goto force_link;
1092 }
1093
1094 /* Clear id_mode_dis bit, and the existing port mode, but
1095 * make sure we enable the RGMII block for data to pass
1096 */
1097 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
1098 reg &= ~ID_MODE_DIS;
1099 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
1100 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
1101
1102 reg |= port_mode | RGMII_MODE_EN;
1103 if (id_mode_dis)
1104 reg |= ID_MODE_DIS;
1105
1106 if (phydev->pause) {
1107 if (phydev->asym_pause)
1108 reg |= TX_PAUSE_EN;
1109 reg |= RX_PAUSE_EN;
1110 }
1111
1112 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
1113
1114 pr_info("Port %d configured for %s\n", port, str);
1115
1116force_link:
1117 /* Force link settings detected from the PHY */
1118 reg = SW_OVERRIDE;
1119 switch (phydev->speed) {
1120 case SPEED_1000:
1121 reg |= SPDSTS_1000 << SPEED_SHIFT;
1122 break;
1123 case SPEED_100:
1124 reg |= SPDSTS_100 << SPEED_SHIFT;
1125 break;
1126 }
1127
1128 if (phydev->link)
1129 reg |= LINK_STS;
1130 if (phydev->duplex == DUPLEX_FULL)
1131 reg |= DUPLX_MODE;
1132
1133 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1134}
1135
1136static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
1137 struct fixed_phy_status *status)
1138{
1139 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainellid2eac982015-07-20 17:49:55 -07001140 u32 duplex, pause;
Florian Fainelli246d7f72014-08-27 17:04:56 -07001141 u32 reg;
1142
Florian Fainelli246d7f72014-08-27 17:04:56 -07001143 duplex = core_readl(priv, CORE_DUPSTS);
1144 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001145
1146 status->link = 0;
1147
1148 /* Port 7 is special as we do not get link status from CORE_LNKSTS,
1149 * which means that we need to force the link at the port override
1150 * level to get the data to flow. We do use what the interrupt handler
1151 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -08001152 *
1153 * For the other ports, we just force the link status, since this is
1154 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -07001155 */
1156 if (port == 7) {
1157 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -07001158 /* For MoCA interfaces, also force a link down notification
1159 * since some version of the user-space daemon (mocad) use
1160 * cmd->autoneg to force the link, which messes up the PHY
1161 * state machine and make it go in PHY_FORCING state instead.
1162 */
1163 if (!status->link)
1164 netif_carrier_off(ds->ports[port]);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001165 status->duplex = 1;
1166 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -08001167 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -07001168 status->duplex = !!(duplex & (1 << port));
1169 }
1170
Florian Fainelli7855f672014-12-11 18:12:42 -08001171 reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1172 reg |= SW_OVERRIDE;
1173 if (status->link)
1174 reg |= LINK_STS;
1175 else
1176 reg &= ~LINK_STS;
1177 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
1178
Florian Fainelli246d7f72014-08-27 17:04:56 -07001179 if ((pause & (1 << port)) &&
1180 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
1181 status->asym_pause = 1;
1182 status->pause = 1;
1183 }
1184
1185 if (pause & (1 << port))
1186 status->pause = 1;
1187}
1188
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001189static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
1190{
1191 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1192 unsigned int port;
1193
Florian Fainelli691c9a82015-01-20 16:42:00 -08001194 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001195
1196 /* Disable all ports physically present including the IMP
1197 * port, the other ones have already been disabled during
1198 * bcm_sf2_sw_setup
1199 */
1200 for (port = 0; port < DSA_MAX_PORTS; port++) {
1201 if ((1 << port) & ds->phys_port_mask ||
1202 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -07001203 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001204 }
1205
1206 return 0;
1207}
1208
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001209static int bcm_sf2_sw_resume(struct dsa_switch *ds)
1210{
1211 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1212 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001213 int ret;
1214
1215 ret = bcm_sf2_sw_rst(priv);
1216 if (ret) {
1217 pr_err("%s: failed to software reset switch\n", __func__);
1218 return ret;
1219 }
1220
Florian Fainellib0836682015-02-05 11:40:41 -08001221 if (priv->hw_params.num_gphy == 1)
1222 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001223
1224 for (port = 0; port < DSA_MAX_PORTS; port++) {
1225 if ((1 << port) & ds->phys_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -07001226 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001227 else if (dsa_is_cpu_port(ds, port))
1228 bcm_sf2_imp_setup(ds, port);
1229 }
1230
1231 return 0;
1232}
1233
Florian Fainelli96e65d72014-09-18 17:31:25 -07001234static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
1235 struct ethtool_wolinfo *wol)
1236{
1237 struct net_device *p = ds->dst[ds->index].master_netdev;
1238 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1239 struct ethtool_wolinfo pwol;
1240
1241 /* Get the parent device WoL settings */
1242 p->ethtool_ops->get_wol(p, &pwol);
1243
1244 /* Advertise the parent device supported settings */
1245 wol->supported = pwol.supported;
1246 memset(&wol->sopass, 0, sizeof(wol->sopass));
1247
1248 if (pwol.wolopts & WAKE_MAGICSECURE)
1249 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
1250
1251 if (priv->wol_ports_mask & (1 << port))
1252 wol->wolopts = pwol.wolopts;
1253 else
1254 wol->wolopts = 0;
1255}
1256
1257static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
1258 struct ethtool_wolinfo *wol)
1259{
1260 struct net_device *p = ds->dst[ds->index].master_netdev;
1261 struct bcm_sf2_priv *priv = ds_to_priv(ds);
1262 s8 cpu_port = ds->dst[ds->index].cpu_port;
1263 struct ethtool_wolinfo pwol;
1264
1265 p->ethtool_ops->get_wol(p, &pwol);
1266 if (wol->wolopts & ~pwol.supported)
1267 return -EINVAL;
1268
1269 if (wol->wolopts)
1270 priv->wol_ports_mask |= (1 << port);
1271 else
1272 priv->wol_ports_mask &= ~(1 << port);
1273
1274 /* If we have at least one port enabled, make sure the CPU port
1275 * is also enabled. If the CPU port is the last one enabled, we disable
1276 * it since this configuration does not make sense.
1277 */
1278 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
1279 priv->wol_ports_mask |= (1 << cpu_port);
1280 else
1281 priv->wol_ports_mask &= ~(1 << cpu_port);
1282
1283 return p->ethtool_ops->set_wol(p, wol);
1284}
1285
Florian Fainelli246d7f72014-08-27 17:04:56 -07001286static struct dsa_switch_driver bcm_sf2_switch_driver = {
Florian Fainelliac7a04c2014-09-11 21:18:09 -07001287 .tag_protocol = DSA_TAG_PROTO_BRCM,
Florian Fainelli246d7f72014-08-27 17:04:56 -07001288 .priv_size = sizeof(struct bcm_sf2_priv),
1289 .probe = bcm_sf2_sw_probe,
1290 .setup = bcm_sf2_sw_setup,
1291 .set_addr = bcm_sf2_sw_set_addr,
Florian Fainelliaa9aef72014-09-19 13:07:55 -07001292 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
Florian Fainelli246d7f72014-08-27 17:04:56 -07001293 .phy_read = bcm_sf2_sw_phy_read,
1294 .phy_write = bcm_sf2_sw_phy_write,
1295 .get_strings = bcm_sf2_sw_get_strings,
1296 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
1297 .get_sset_count = bcm_sf2_sw_get_sset_count,
1298 .adjust_link = bcm_sf2_sw_adjust_link,
1299 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
Florian Fainelli8cfa9492014-09-18 17:31:23 -07001300 .suspend = bcm_sf2_sw_suspend,
1301 .resume = bcm_sf2_sw_resume,
Florian Fainelli96e65d72014-09-18 17:31:25 -07001302 .get_wol = bcm_sf2_sw_get_wol,
1303 .set_wol = bcm_sf2_sw_set_wol,
Florian Fainellib6d045d2014-09-24 17:05:20 -07001304 .port_enable = bcm_sf2_port_setup,
1305 .port_disable = bcm_sf2_port_disable,
Florian Fainelli450b05c2014-09-24 17:05:22 -07001306 .get_eee = bcm_sf2_sw_get_eee,
1307 .set_eee = bcm_sf2_sw_set_eee,
Florian Fainelli12f460f2015-02-24 13:15:34 -08001308 .port_join_bridge = bcm_sf2_sw_br_join,
1309 .port_leave_bridge = bcm_sf2_sw_br_leave,
1310 .port_stp_update = bcm_sf2_sw_br_set_stp_state,
Florian Fainelli680060d2015-10-23 11:38:07 -07001311 .port_fdb_prepare = bcm_sf2_sw_fdb_prepare,
1312 .port_fdb_add = bcm_sf2_sw_fdb_add,
1313 .port_fdb_del = bcm_sf2_sw_fdb_del,
1314 .port_fdb_dump = bcm_sf2_sw_fdb_dump,
Florian Fainelli246d7f72014-08-27 17:04:56 -07001315};
1316
1317static int __init bcm_sf2_init(void)
1318{
1319 register_switch_driver(&bcm_sf2_switch_driver);
1320
1321 return 0;
1322}
1323module_init(bcm_sf2_init);
1324
1325static void __exit bcm_sf2_exit(void)
1326{
1327 unregister_switch_driver(&bcm_sf2_switch_driver);
1328}
1329module_exit(bcm_sf2_exit);
1330
1331MODULE_AUTHOR("Broadcom Corporation");
1332MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1333MODULE_LICENSE("GPL");
1334MODULE_ALIAS("platform:brcm-sf2");