blob: 634e44ee8d0d5a957bb9a505b18212078f4bfc9b [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 Fainelli246d7f72014-08-27 17:04:56 -070026
27#include "bcm_sf2.h"
28#include "bcm_sf2_regs.h"
29
30/* String, offset, and register size in bytes if different from 4 bytes */
31static const struct bcm_sf2_hw_stats bcm_sf2_mib[] = {
32 { "TxOctets", 0x000, 8 },
33 { "TxDropPkts", 0x020 },
34 { "TxQPKTQ0", 0x030 },
35 { "TxBroadcastPkts", 0x040 },
36 { "TxMulticastPkts", 0x050 },
37 { "TxUnicastPKts", 0x060 },
38 { "TxCollisions", 0x070 },
39 { "TxSingleCollision", 0x080 },
40 { "TxMultipleCollision", 0x090 },
41 { "TxDeferredCollision", 0x0a0 },
42 { "TxLateCollision", 0x0b0 },
43 { "TxExcessiveCollision", 0x0c0 },
44 { "TxFrameInDisc", 0x0d0 },
45 { "TxPausePkts", 0x0e0 },
46 { "TxQPKTQ1", 0x0f0 },
47 { "TxQPKTQ2", 0x100 },
48 { "TxQPKTQ3", 0x110 },
49 { "TxQPKTQ4", 0x120 },
50 { "TxQPKTQ5", 0x130 },
51 { "RxOctets", 0x140, 8 },
52 { "RxUndersizePkts", 0x160 },
53 { "RxPausePkts", 0x170 },
54 { "RxPkts64Octets", 0x180 },
55 { "RxPkts65to127Octets", 0x190 },
56 { "RxPkts128to255Octets", 0x1a0 },
57 { "RxPkts256to511Octets", 0x1b0 },
58 { "RxPkts512to1023Octets", 0x1c0 },
59 { "RxPkts1024toMaxPktsOctets", 0x1d0 },
60 { "RxOversizePkts", 0x1e0 },
61 { "RxJabbers", 0x1f0 },
62 { "RxAlignmentErrors", 0x200 },
63 { "RxFCSErrors", 0x210 },
64 { "RxGoodOctets", 0x220, 8 },
65 { "RxDropPkts", 0x240 },
66 { "RxUnicastPkts", 0x250 },
67 { "RxMulticastPkts", 0x260 },
68 { "RxBroadcastPkts", 0x270 },
69 { "RxSAChanges", 0x280 },
70 { "RxFragments", 0x290 },
71 { "RxJumboPkt", 0x2a0 },
72 { "RxSymblErr", 0x2b0 },
73 { "InRangeErrCount", 0x2c0 },
74 { "OutRangeErrCount", 0x2d0 },
75 { "EEELpiEvent", 0x2e0 },
76 { "EEELpiDuration", 0x2f0 },
77 { "RxDiscard", 0x300, 8 },
78 { "TxQPKTQ6", 0x320 },
79 { "TxQPKTQ7", 0x330 },
80 { "TxPkts64Octets", 0x340 },
81 { "TxPkts65to127Octets", 0x350 },
82 { "TxPkts128to255Octets", 0x360 },
83 { "TxPkts256to511Ocets", 0x370 },
84 { "TxPkts512to1023Ocets", 0x380 },
85 { "TxPkts1024toMaxPktOcets", 0x390 },
86};
87
88#define BCM_SF2_STATS_SIZE ARRAY_SIZE(bcm_sf2_mib)
89
90static void bcm_sf2_sw_get_strings(struct dsa_switch *ds,
91 int port, uint8_t *data)
92{
93 unsigned int i;
94
95 for (i = 0; i < BCM_SF2_STATS_SIZE; i++)
96 memcpy(data + i * ETH_GSTRING_LEN,
97 bcm_sf2_mib[i].string, ETH_GSTRING_LEN);
98}
99
100static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds,
101 int port, uint64_t *data)
102{
103 struct bcm_sf2_priv *priv = ds_to_priv(ds);
104 const struct bcm_sf2_hw_stats *s;
105 unsigned int i;
106 u64 val = 0;
107 u32 offset;
108
109 mutex_lock(&priv->stats_mutex);
110
111 /* Now fetch the per-port counters */
112 for (i = 0; i < BCM_SF2_STATS_SIZE; i++) {
113 s = &bcm_sf2_mib[i];
114
115 /* Do a latched 64-bit read if needed */
116 offset = s->reg + CORE_P_MIB_OFFSET(port);
117 if (s->sizeof_stat == 8)
118 val = core_readq(priv, offset);
119 else
120 val = core_readl(priv, offset);
121
122 data[i] = (u64)val;
123 }
124
125 mutex_unlock(&priv->stats_mutex);
126}
127
128static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds)
129{
130 return BCM_SF2_STATS_SIZE;
131}
132
Alexander Duyckb4d23942014-09-15 13:00:27 -0400133static char *bcm_sf2_sw_probe(struct device *host_dev, int sw_addr)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700134{
135 return "Broadcom Starfighter 2";
136}
137
Florian Fainellib6d045d2014-09-24 17:05:20 -0700138static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700139{
140 struct bcm_sf2_priv *priv = ds_to_priv(ds);
141 unsigned int i;
Florian Fainellib6d045d2014-09-24 17:05:20 -0700142 u32 reg;
143
144 /* Enable the IMP Port to be in the same VLAN as the other ports
145 * on a per-port basis such that we only have Port i and IMP in
146 * the same VLAN.
147 */
148 for (i = 0; i < priv->hw_params.num_ports; i++) {
149 if (!((1 << i) & ds->phys_port_mask))
150 continue;
151
152 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
153 reg |= (1 << cpu_port);
154 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
155 }
156}
157
158static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
159{
160 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700161 u32 reg, val;
162
163 /* Enable the port memories */
164 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
165 reg &= ~P_TXQ_PSM_VDD(port);
166 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
167
168 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
169 reg = core_readl(priv, CORE_IMP_CTL);
170 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
171 reg &= ~(RX_DIS | TX_DIS);
172 core_writel(priv, reg, CORE_IMP_CTL);
173
174 /* Enable forwarding */
175 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
176
177 /* Enable IMP port in dumb mode */
178 reg = core_readl(priv, CORE_SWITCH_CTRL);
179 reg |= MII_DUMB_FWDG_EN;
180 core_writel(priv, reg, CORE_SWITCH_CTRL);
181
182 /* Resolve which bit controls the Broadcom tag */
183 switch (port) {
184 case 8:
185 val = BRCM_HDR_EN_P8;
186 break;
187 case 7:
188 val = BRCM_HDR_EN_P7;
189 break;
190 case 5:
191 val = BRCM_HDR_EN_P5;
192 break;
193 default:
194 val = 0;
195 break;
196 }
197
198 /* Enable Broadcom tags for IMP port */
199 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
200 reg |= val;
201 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
202
203 /* Enable reception Broadcom tag for CPU TX (switch RX) to
204 * allow us to tag outgoing frames
205 */
206 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
207 reg &= ~(1 << port);
208 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
209
210 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
211 * allow delivering frames to the per-port net_devices
212 */
213 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
214 reg &= ~(1 << port);
215 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
216
217 /* Force link status for IMP port */
218 reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
219 reg |= (MII_SW_OR | LINK_STS);
220 core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700221}
222
Florian Fainellib6d045d2014-09-24 17:05:20 -0700223static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
224 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700225{
226 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700227 s8 cpu_port = ds->dst[ds->index].cpu_port;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700228 u32 reg;
229
230 /* Clear the memory power down */
231 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
232 reg &= ~P_TXQ_PSM_VDD(port);
233 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
234
235 /* Clear the Rx and Tx disable bits and set to no spanning tree */
236 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
237
238 /* Enable port 7 interrupts to get notified */
239 if (port == 7)
240 intrl2_1_mask_clear(priv, P_IRQ_MASK(P7_IRQ_OFF));
241
242 /* Set this port, and only this one to be in the default VLAN */
243 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
244 reg &= ~PORT_VLAN_CTRL_MASK;
245 reg |= (1 << port);
246 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700247
248 bcm_sf2_imp_vlan_setup(ds, cpu_port);
249
250 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700251}
252
Florian Fainellib6d045d2014-09-24 17:05:20 -0700253static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
254 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700255{
256 struct bcm_sf2_priv *priv = ds_to_priv(ds);
257 u32 off, reg;
258
Florian Fainelli96e65d72014-09-18 17:31:25 -0700259 if (priv->wol_ports_mask & (1 << port))
260 return;
261
Florian Fainellib6d045d2014-09-24 17:05:20 -0700262 if (port == 7) {
263 intrl2_1_mask_set(priv, P_IRQ_MASK(P7_IRQ_OFF));
264 intrl2_1_writel(priv, P_IRQ_MASK(P7_IRQ_OFF), INTRL2_CPU_CLEAR);
265 }
266
Florian Fainelli246d7f72014-08-27 17:04:56 -0700267 if (dsa_is_cpu_port(ds, port))
268 off = CORE_IMP_CTL;
269 else
270 off = CORE_G_PCTL_PORT(port);
271
272 reg = core_readl(priv, off);
273 reg |= RX_DIS | TX_DIS;
274 core_writel(priv, reg, off);
275
276 /* Power down the port memory */
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
282static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
283{
284 struct bcm_sf2_priv *priv = dev_id;
285
286 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
287 ~priv->irq0_mask;
288 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
289
290 return IRQ_HANDLED;
291}
292
293static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
294{
295 struct bcm_sf2_priv *priv = dev_id;
296
297 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
298 ~priv->irq1_mask;
299 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
300
301 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
302 priv->port_sts[7].link = 1;
303 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
304 priv->port_sts[7].link = 0;
305
306 return IRQ_HANDLED;
307}
308
309static int bcm_sf2_sw_setup(struct dsa_switch *ds)
310{
311 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
312 struct bcm_sf2_priv *priv = ds_to_priv(ds);
313 struct device_node *dn;
314 void __iomem **base;
315 unsigned int port;
316 unsigned int i;
317 u32 reg, rev;
318 int ret;
319
320 spin_lock_init(&priv->indir_lock);
321 mutex_init(&priv->stats_mutex);
322
323 /* All the interesting properties are at the parent device_node
324 * level
325 */
326 dn = ds->pd->of_node->parent;
327
328 priv->irq0 = irq_of_parse_and_map(dn, 0);
329 priv->irq1 = irq_of_parse_and_map(dn, 1);
330
331 base = &priv->core;
332 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
333 *base = of_iomap(dn, i);
334 if (*base == NULL) {
335 pr_err("unable to find register: %s\n", reg_names[i]);
336 return -ENODEV;
337 }
338 base++;
339 }
340
341 /* Disable all interrupts and request them */
342 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
343 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
344 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
345 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
346 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
347 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
348
349 ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
350 "switch_0", priv);
351 if (ret < 0) {
352 pr_err("failed to request switch_0 IRQ\n");
353 goto out_unmap;
354 }
355
356 ret = request_irq(priv->irq1, bcm_sf2_switch_1_isr, 0,
357 "switch_1", priv);
358 if (ret < 0) {
359 pr_err("failed to request switch_1 IRQ\n");
360 goto out_free_irq0;
361 }
362
363 /* Reset the MIB counters */
364 reg = core_readl(priv, CORE_GMNCFGCFG);
365 reg |= RST_MIB_CNT;
366 core_writel(priv, reg, CORE_GMNCFGCFG);
367 reg &= ~RST_MIB_CNT;
368 core_writel(priv, reg, CORE_GMNCFGCFG);
369
370 /* Get the maximum number of ports for this switch */
371 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
372 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
373 priv->hw_params.num_ports = DSA_MAX_PORTS;
374
375 /* Assume a single GPHY setup if we can't read that property */
376 if (of_property_read_u32(dn, "brcm,num-gphy",
377 &priv->hw_params.num_gphy))
378 priv->hw_params.num_gphy = 1;
379
380 /* Enable all valid ports and disable those unused */
381 for (port = 0; port < priv->hw_params.num_ports; port++) {
382 /* IMP port receives special treatment */
383 if ((1 << port) & ds->phys_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700384 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700385 else if (dsa_is_cpu_port(ds, port))
386 bcm_sf2_imp_setup(ds, port);
387 else
Florian Fainellib6d045d2014-09-24 17:05:20 -0700388 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700389 }
390
391 /* Include the pseudo-PHY address and the broadcast PHY address to
392 * divert reads towards our workaround
393 */
394 ds->phys_mii_mask |= ((1 << 30) | (1 << 0));
395
396 rev = reg_readl(priv, REG_SWITCH_REVISION);
397 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
398 SWITCH_TOP_REV_MASK;
399 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
400
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700401 rev = reg_readl(priv, REG_PHY_REVISION);
402 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
403
Florian Fainelli246d7f72014-08-27 17:04:56 -0700404 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
405 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
406 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
407 priv->core, priv->irq0, priv->irq1);
408
409 return 0;
410
411out_free_irq0:
412 free_irq(priv->irq0, priv);
413out_unmap:
414 base = &priv->core;
415 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
416 iounmap(*base);
417 base++;
418 }
419 return ret;
420}
421
422static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
423{
424 return 0;
425}
426
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700427static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
428{
429 struct bcm_sf2_priv *priv = ds_to_priv(ds);
430
431 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
432 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
433 * the REG_PHY_REVISION register layout is.
434 */
435
436 return priv->hw_params.gphy_rev;
437}
438
Florian Fainelli246d7f72014-08-27 17:04:56 -0700439static int bcm_sf2_sw_indir_rw(struct dsa_switch *ds, int op, int addr,
440 int regnum, u16 val)
441{
442 struct bcm_sf2_priv *priv = ds_to_priv(ds);
443 int ret = 0;
444 u32 reg;
445
446 reg = reg_readl(priv, REG_SWITCH_CNTRL);
447 reg |= MDIO_MASTER_SEL;
448 reg_writel(priv, reg, REG_SWITCH_CNTRL);
449
450 /* Page << 8 | offset */
451 reg = 0x70;
452 reg <<= 2;
453 core_writel(priv, addr, reg);
454
455 /* Page << 8 | offset */
456 reg = 0x80 << 8 | regnum << 1;
457 reg <<= 2;
458
459 if (op)
460 ret = core_readl(priv, reg);
461 else
462 core_writel(priv, val, reg);
463
464 reg = reg_readl(priv, REG_SWITCH_CNTRL);
465 reg &= ~MDIO_MASTER_SEL;
466 reg_writel(priv, reg, REG_SWITCH_CNTRL);
467
468 return ret & 0xffff;
469}
470
471static int bcm_sf2_sw_phy_read(struct dsa_switch *ds, int addr, int regnum)
472{
473 /* Intercept reads from the MDIO broadcast address or Broadcom
474 * pseudo-PHY address
475 */
476 switch (addr) {
477 case 0:
478 case 30:
479 return bcm_sf2_sw_indir_rw(ds, 1, addr, regnum, 0);
480 default:
481 return 0xffff;
482 }
483}
484
485static int bcm_sf2_sw_phy_write(struct dsa_switch *ds, int addr, int regnum,
486 u16 val)
487{
488 /* Intercept writes to the MDIO broadcast address or Broadcom
489 * pseudo-PHY address
490 */
491 switch (addr) {
492 case 0:
493 case 30:
494 bcm_sf2_sw_indir_rw(ds, 0, addr, regnum, val);
495 break;
496 }
497
498 return 0;
499}
500
501static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
502 struct phy_device *phydev)
503{
504 struct bcm_sf2_priv *priv = ds_to_priv(ds);
505 u32 id_mode_dis = 0, port_mode;
506 const char *str = NULL;
507 u32 reg;
508
509 switch (phydev->interface) {
510 case PHY_INTERFACE_MODE_RGMII:
511 str = "RGMII (no delay)";
512 id_mode_dis = 1;
513 case PHY_INTERFACE_MODE_RGMII_TXID:
514 if (!str)
515 str = "RGMII (TX delay)";
516 port_mode = EXT_GPHY;
517 break;
518 case PHY_INTERFACE_MODE_MII:
519 str = "MII";
520 port_mode = EXT_EPHY;
521 break;
522 case PHY_INTERFACE_MODE_REVMII:
523 str = "Reverse MII";
524 port_mode = EXT_REVMII;
525 break;
526 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700527 /* All other PHYs: internal and MoCA */
528 goto force_link;
529 }
530
531 /* If the link is down, just disable the interface to conserve power */
532 if (!phydev->link) {
533 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
534 reg &= ~RGMII_MODE_EN;
535 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700536 goto force_link;
537 }
538
539 /* Clear id_mode_dis bit, and the existing port mode, but
540 * make sure we enable the RGMII block for data to pass
541 */
542 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
543 reg &= ~ID_MODE_DIS;
544 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
545 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
546
547 reg |= port_mode | RGMII_MODE_EN;
548 if (id_mode_dis)
549 reg |= ID_MODE_DIS;
550
551 if (phydev->pause) {
552 if (phydev->asym_pause)
553 reg |= TX_PAUSE_EN;
554 reg |= RX_PAUSE_EN;
555 }
556
557 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
558
559 pr_info("Port %d configured for %s\n", port, str);
560
561force_link:
562 /* Force link settings detected from the PHY */
563 reg = SW_OVERRIDE;
564 switch (phydev->speed) {
565 case SPEED_1000:
566 reg |= SPDSTS_1000 << SPEED_SHIFT;
567 break;
568 case SPEED_100:
569 reg |= SPDSTS_100 << SPEED_SHIFT;
570 break;
571 }
572
573 if (phydev->link)
574 reg |= LINK_STS;
575 if (phydev->duplex == DUPLEX_FULL)
576 reg |= DUPLX_MODE;
577
578 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
579}
580
581static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
582 struct fixed_phy_status *status)
583{
584 struct bcm_sf2_priv *priv = ds_to_priv(ds);
585 u32 link, duplex, pause, speed;
586 u32 reg;
587
588 link = core_readl(priv, CORE_LNKSTS);
589 duplex = core_readl(priv, CORE_DUPSTS);
590 pause = core_readl(priv, CORE_PAUSESTS);
591 speed = core_readl(priv, CORE_SPDSTS);
592
593 speed >>= (port * SPDSTS_SHIFT);
594 speed &= SPDSTS_MASK;
595
596 status->link = 0;
597
598 /* Port 7 is special as we do not get link status from CORE_LNKSTS,
599 * which means that we need to force the link at the port override
600 * level to get the data to flow. We do use what the interrupt handler
601 * did determine before.
602 */
603 if (port == 7) {
604 status->link = priv->port_sts[port].link;
605 reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(7));
606 reg |= SW_OVERRIDE;
607 if (status->link)
608 reg |= LINK_STS;
609 else
610 reg &= ~LINK_STS;
611 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(7));
612 status->duplex = 1;
613 } else {
614 status->link = !!(link & (1 << port));
615 status->duplex = !!(duplex & (1 << port));
616 }
617
618 switch (speed) {
619 case SPDSTS_10:
620 status->speed = SPEED_10;
621 break;
622 case SPDSTS_100:
623 status->speed = SPEED_100;
624 break;
625 case SPDSTS_1000:
626 status->speed = SPEED_1000;
627 break;
628 }
629
630 if ((pause & (1 << port)) &&
631 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
632 status->asym_pause = 1;
633 status->pause = 1;
634 }
635
636 if (pause & (1 << port))
637 status->pause = 1;
638}
639
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700640static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
641{
642 struct bcm_sf2_priv *priv = ds_to_priv(ds);
643 unsigned int port;
644
645 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
646 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
647 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
648 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
649 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
650 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
651
652 /* Disable all ports physically present including the IMP
653 * port, the other ones have already been disabled during
654 * bcm_sf2_sw_setup
655 */
656 for (port = 0; port < DSA_MAX_PORTS; port++) {
657 if ((1 << port) & ds->phys_port_mask ||
658 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700659 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700660 }
661
662 return 0;
663}
664
665static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
666{
667 unsigned int timeout = 1000;
668 u32 reg;
669
670 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
671 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
672 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
673
674 do {
675 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
676 if (!(reg & SOFTWARE_RESET))
677 break;
678
679 usleep_range(1000, 2000);
680 } while (timeout-- > 0);
681
682 if (timeout == 0)
683 return -ETIMEDOUT;
684
685 return 0;
686}
687
688static int bcm_sf2_sw_resume(struct dsa_switch *ds)
689{
690 struct bcm_sf2_priv *priv = ds_to_priv(ds);
691 unsigned int port;
692 u32 reg;
693 int ret;
694
695 ret = bcm_sf2_sw_rst(priv);
696 if (ret) {
697 pr_err("%s: failed to software reset switch\n", __func__);
698 return ret;
699 }
700
701 /* Reinitialize the single GPHY */
702 if (priv->hw_params.num_gphy == 1) {
703 reg = reg_readl(priv, REG_SPHY_CNTRL);
704 reg |= PHY_RESET;
705 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS);
706 reg_writel(priv, reg, REG_SPHY_CNTRL);
707 udelay(21);
708 reg = reg_readl(priv, REG_SPHY_CNTRL);
709 reg &= ~PHY_RESET;
710 reg_writel(priv, reg, REG_SPHY_CNTRL);
711 }
712
713 for (port = 0; port < DSA_MAX_PORTS; port++) {
714 if ((1 << port) & ds->phys_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700715 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700716 else if (dsa_is_cpu_port(ds, port))
717 bcm_sf2_imp_setup(ds, port);
718 }
719
720 return 0;
721}
722
Florian Fainelli96e65d72014-09-18 17:31:25 -0700723static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
724 struct ethtool_wolinfo *wol)
725{
726 struct net_device *p = ds->dst[ds->index].master_netdev;
727 struct bcm_sf2_priv *priv = ds_to_priv(ds);
728 struct ethtool_wolinfo pwol;
729
730 /* Get the parent device WoL settings */
731 p->ethtool_ops->get_wol(p, &pwol);
732
733 /* Advertise the parent device supported settings */
734 wol->supported = pwol.supported;
735 memset(&wol->sopass, 0, sizeof(wol->sopass));
736
737 if (pwol.wolopts & WAKE_MAGICSECURE)
738 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
739
740 if (priv->wol_ports_mask & (1 << port))
741 wol->wolopts = pwol.wolopts;
742 else
743 wol->wolopts = 0;
744}
745
746static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
747 struct ethtool_wolinfo *wol)
748{
749 struct net_device *p = ds->dst[ds->index].master_netdev;
750 struct bcm_sf2_priv *priv = ds_to_priv(ds);
751 s8 cpu_port = ds->dst[ds->index].cpu_port;
752 struct ethtool_wolinfo pwol;
753
754 p->ethtool_ops->get_wol(p, &pwol);
755 if (wol->wolopts & ~pwol.supported)
756 return -EINVAL;
757
758 if (wol->wolopts)
759 priv->wol_ports_mask |= (1 << port);
760 else
761 priv->wol_ports_mask &= ~(1 << port);
762
763 /* If we have at least one port enabled, make sure the CPU port
764 * is also enabled. If the CPU port is the last one enabled, we disable
765 * it since this configuration does not make sense.
766 */
767 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
768 priv->wol_ports_mask |= (1 << cpu_port);
769 else
770 priv->wol_ports_mask &= ~(1 << cpu_port);
771
772 return p->ethtool_ops->set_wol(p, wol);
773}
774
Florian Fainelli246d7f72014-08-27 17:04:56 -0700775static struct dsa_switch_driver bcm_sf2_switch_driver = {
Florian Fainelliac7a04c2014-09-11 21:18:09 -0700776 .tag_protocol = DSA_TAG_PROTO_BRCM,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700777 .priv_size = sizeof(struct bcm_sf2_priv),
778 .probe = bcm_sf2_sw_probe,
779 .setup = bcm_sf2_sw_setup,
780 .set_addr = bcm_sf2_sw_set_addr,
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700781 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700782 .phy_read = bcm_sf2_sw_phy_read,
783 .phy_write = bcm_sf2_sw_phy_write,
784 .get_strings = bcm_sf2_sw_get_strings,
785 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
786 .get_sset_count = bcm_sf2_sw_get_sset_count,
787 .adjust_link = bcm_sf2_sw_adjust_link,
788 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700789 .suspend = bcm_sf2_sw_suspend,
790 .resume = bcm_sf2_sw_resume,
Florian Fainelli96e65d72014-09-18 17:31:25 -0700791 .get_wol = bcm_sf2_sw_get_wol,
792 .set_wol = bcm_sf2_sw_set_wol,
Florian Fainellib6d045d2014-09-24 17:05:20 -0700793 .port_enable = bcm_sf2_port_setup,
794 .port_disable = bcm_sf2_port_disable,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700795};
796
797static int __init bcm_sf2_init(void)
798{
799 register_switch_driver(&bcm_sf2_switch_driver);
800
801 return 0;
802}
803module_init(bcm_sf2_init);
804
805static void __exit bcm_sf2_exit(void)
806{
807 unregister_switch_driver(&bcm_sf2_switch_driver);
808}
809module_exit(bcm_sf2_exit);
810
811MODULE_AUTHOR("Broadcom Corporation");
812MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
813MODULE_LICENSE("GPL");
814MODULE_ALIAS("platform:brcm-sf2");