blob: 45c0e2b97f5f541caff984810ea3b1393bcb6dce [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 Fainelli450b05c2014-09-24 17:05:22 -0700223static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
224{
225 struct bcm_sf2_priv *priv = ds_to_priv(ds);
226 u32 reg;
227
228 reg = core_readl(priv, CORE_EEE_EN_CTRL);
229 if (enable)
230 reg |= 1 << port;
231 else
232 reg &= ~(1 << port);
233 core_writel(priv, reg, CORE_EEE_EN_CTRL);
234}
235
Florian Fainellib0836682015-02-05 11:40:41 -0800236static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
237{
238 struct bcm_sf2_priv *priv = ds_to_priv(ds);
239 u32 reg;
240
241 if (!enable)
242 return;
243
244 reg = reg_readl(priv, REG_SPHY_CNTRL);
245 reg |= PHY_RESET;
246 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS);
247 reg_writel(priv, reg, REG_SPHY_CNTRL);
248 udelay(21);
249 reg = reg_readl(priv, REG_SPHY_CNTRL);
250 reg &= ~PHY_RESET;
251 reg_writel(priv, reg, REG_SPHY_CNTRL);
252}
253
Florian Fainellib6d045d2014-09-24 17:05:20 -0700254static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
255 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700256{
257 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700258 s8 cpu_port = ds->dst[ds->index].cpu_port;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700259 u32 reg;
260
261 /* Clear the memory power down */
262 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
263 reg &= ~P_TXQ_PSM_VDD(port);
264 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
265
266 /* Clear the Rx and Tx disable bits and set to no spanning tree */
267 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
268
269 /* Enable port 7 interrupts to get notified */
270 if (port == 7)
271 intrl2_1_mask_clear(priv, P_IRQ_MASK(P7_IRQ_OFF));
272
273 /* Set this port, and only this one to be in the default VLAN */
274 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
275 reg &= ~PORT_VLAN_CTRL_MASK;
276 reg |= (1 << port);
277 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700278
279 bcm_sf2_imp_vlan_setup(ds, cpu_port);
280
Florian Fainelli450b05c2014-09-24 17:05:22 -0700281 /* If EEE was enabled, restore it */
282 if (priv->port_sts[port].eee.eee_enabled)
283 bcm_sf2_eee_enable_set(ds, port, true);
284
Florian Fainellib6d045d2014-09-24 17:05:20 -0700285 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700286}
287
Florian Fainellib6d045d2014-09-24 17:05:20 -0700288static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
289 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700290{
291 struct bcm_sf2_priv *priv = ds_to_priv(ds);
292 u32 off, reg;
293
Florian Fainelli96e65d72014-09-18 17:31:25 -0700294 if (priv->wol_ports_mask & (1 << port))
295 return;
296
Florian Fainellib6d045d2014-09-24 17:05:20 -0700297 if (port == 7) {
298 intrl2_1_mask_set(priv, P_IRQ_MASK(P7_IRQ_OFF));
299 intrl2_1_writel(priv, P_IRQ_MASK(P7_IRQ_OFF), INTRL2_CPU_CLEAR);
300 }
301
Florian Fainelli246d7f72014-08-27 17:04:56 -0700302 if (dsa_is_cpu_port(ds, port))
303 off = CORE_IMP_CTL;
304 else
305 off = CORE_G_PCTL_PORT(port);
306
307 reg = core_readl(priv, off);
308 reg |= RX_DIS | TX_DIS;
309 core_writel(priv, reg, off);
310
311 /* Power down the port memory */
312 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
313 reg |= P_TXQ_PSM_VDD(port);
314 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
315}
316
Florian Fainelli450b05c2014-09-24 17:05:22 -0700317/* Returns 0 if EEE was not enabled, or 1 otherwise
318 */
319static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
320 struct phy_device *phy)
321{
322 struct bcm_sf2_priv *priv = ds_to_priv(ds);
323 struct ethtool_eee *p = &priv->port_sts[port].eee;
324 int ret;
325
326 p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
327
328 ret = phy_init_eee(phy, 0);
329 if (ret)
330 return 0;
331
332 bcm_sf2_eee_enable_set(ds, port, true);
333
334 return 1;
335}
336
337static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
338 struct ethtool_eee *e)
339{
340 struct bcm_sf2_priv *priv = ds_to_priv(ds);
341 struct ethtool_eee *p = &priv->port_sts[port].eee;
342 u32 reg;
343
344 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
345 e->eee_enabled = p->eee_enabled;
346 e->eee_active = !!(reg & (1 << port));
347
348 return 0;
349}
350
351static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
352 struct phy_device *phydev,
353 struct ethtool_eee *e)
354{
355 struct bcm_sf2_priv *priv = ds_to_priv(ds);
356 struct ethtool_eee *p = &priv->port_sts[port].eee;
357
358 p->eee_enabled = e->eee_enabled;
359
360 if (!p->eee_enabled) {
361 bcm_sf2_eee_enable_set(ds, port, false);
362 } else {
363 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
364 if (!p->eee_enabled)
365 return -EOPNOTSUPP;
366 }
367
368 return 0;
369}
370
Florian Fainelli246d7f72014-08-27 17:04:56 -0700371static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
372{
373 struct bcm_sf2_priv *priv = dev_id;
374
375 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
376 ~priv->irq0_mask;
377 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
378
379 return IRQ_HANDLED;
380}
381
382static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
383{
384 struct bcm_sf2_priv *priv = dev_id;
385
386 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
387 ~priv->irq1_mask;
388 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
389
390 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
391 priv->port_sts[7].link = 1;
392 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
393 priv->port_sts[7].link = 0;
394
395 return IRQ_HANDLED;
396}
397
Florian Fainelli33f84612014-11-25 18:08:49 -0800398static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
399{
400 unsigned int timeout = 1000;
401 u32 reg;
402
403 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
404 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
405 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
406
407 do {
408 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
409 if (!(reg & SOFTWARE_RESET))
410 break;
411
412 usleep_range(1000, 2000);
413 } while (timeout-- > 0);
414
415 if (timeout == 0)
416 return -ETIMEDOUT;
417
418 return 0;
419}
420
Florian Fainelli691c9a82015-01-20 16:42:00 -0800421static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
422{
423 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
424 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
425 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
426 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
427 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
428 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
429}
430
Florian Fainelli246d7f72014-08-27 17:04:56 -0700431static int bcm_sf2_sw_setup(struct dsa_switch *ds)
432{
433 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
434 struct bcm_sf2_priv *priv = ds_to_priv(ds);
435 struct device_node *dn;
436 void __iomem **base;
437 unsigned int port;
438 unsigned int i;
439 u32 reg, rev;
440 int ret;
441
442 spin_lock_init(&priv->indir_lock);
443 mutex_init(&priv->stats_mutex);
444
445 /* All the interesting properties are at the parent device_node
446 * level
447 */
448 dn = ds->pd->of_node->parent;
449
450 priv->irq0 = irq_of_parse_and_map(dn, 0);
451 priv->irq1 = irq_of_parse_and_map(dn, 1);
452
453 base = &priv->core;
454 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
455 *base = of_iomap(dn, i);
456 if (*base == NULL) {
457 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainellia5660592014-11-25 18:08:48 -0800458 ret = -ENOMEM;
459 goto out_unmap;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700460 }
461 base++;
462 }
463
Florian Fainelli33f84612014-11-25 18:08:49 -0800464 ret = bcm_sf2_sw_rst(priv);
465 if (ret) {
466 pr_err("unable to software reset switch: %d\n", ret);
467 goto out_unmap;
468 }
469
Florian Fainelli246d7f72014-08-27 17:04:56 -0700470 /* Disable all interrupts and request them */
Florian Fainelli691c9a82015-01-20 16:42:00 -0800471 bcm_sf2_intr_disable(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700472
473 ret = request_irq(priv->irq0, bcm_sf2_switch_0_isr, 0,
474 "switch_0", priv);
475 if (ret < 0) {
476 pr_err("failed to request switch_0 IRQ\n");
477 goto out_unmap;
478 }
479
480 ret = request_irq(priv->irq1, bcm_sf2_switch_1_isr, 0,
481 "switch_1", priv);
482 if (ret < 0) {
483 pr_err("failed to request switch_1 IRQ\n");
484 goto out_free_irq0;
485 }
486
487 /* Reset the MIB counters */
488 reg = core_readl(priv, CORE_GMNCFGCFG);
489 reg |= RST_MIB_CNT;
490 core_writel(priv, reg, CORE_GMNCFGCFG);
491 reg &= ~RST_MIB_CNT;
492 core_writel(priv, reg, CORE_GMNCFGCFG);
493
494 /* Get the maximum number of ports for this switch */
495 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
496 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
497 priv->hw_params.num_ports = DSA_MAX_PORTS;
498
499 /* Assume a single GPHY setup if we can't read that property */
500 if (of_property_read_u32(dn, "brcm,num-gphy",
501 &priv->hw_params.num_gphy))
502 priv->hw_params.num_gphy = 1;
503
504 /* Enable all valid ports and disable those unused */
505 for (port = 0; port < priv->hw_params.num_ports; port++) {
506 /* IMP port receives special treatment */
507 if ((1 << port) & ds->phys_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700508 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700509 else if (dsa_is_cpu_port(ds, port))
510 bcm_sf2_imp_setup(ds, port);
511 else
Florian Fainellib6d045d2014-09-24 17:05:20 -0700512 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700513 }
514
515 /* Include the pseudo-PHY address and the broadcast PHY address to
516 * divert reads towards our workaround
517 */
518 ds->phys_mii_mask |= ((1 << 30) | (1 << 0));
519
520 rev = reg_readl(priv, REG_SWITCH_REVISION);
521 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
522 SWITCH_TOP_REV_MASK;
523 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
524
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700525 rev = reg_readl(priv, REG_PHY_REVISION);
526 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
527
Florian Fainelli246d7f72014-08-27 17:04:56 -0700528 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
529 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
530 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
531 priv->core, priv->irq0, priv->irq1);
532
533 return 0;
534
535out_free_irq0:
536 free_irq(priv->irq0, priv);
537out_unmap:
538 base = &priv->core;
539 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainellia5660592014-11-25 18:08:48 -0800540 if (*base)
541 iounmap(*base);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700542 base++;
543 }
544 return ret;
545}
546
547static int bcm_sf2_sw_set_addr(struct dsa_switch *ds, u8 *addr)
548{
549 return 0;
550}
551
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700552static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
553{
554 struct bcm_sf2_priv *priv = ds_to_priv(ds);
555
556 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
557 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
558 * the REG_PHY_REVISION register layout is.
559 */
560
561 return priv->hw_params.gphy_rev;
562}
563
Florian Fainelli246d7f72014-08-27 17:04:56 -0700564static int bcm_sf2_sw_indir_rw(struct dsa_switch *ds, int op, int addr,
565 int regnum, u16 val)
566{
567 struct bcm_sf2_priv *priv = ds_to_priv(ds);
568 int ret = 0;
569 u32 reg;
570
571 reg = reg_readl(priv, REG_SWITCH_CNTRL);
572 reg |= MDIO_MASTER_SEL;
573 reg_writel(priv, reg, REG_SWITCH_CNTRL);
574
575 /* Page << 8 | offset */
576 reg = 0x70;
577 reg <<= 2;
578 core_writel(priv, addr, reg);
579
580 /* Page << 8 | offset */
581 reg = 0x80 << 8 | regnum << 1;
582 reg <<= 2;
583
584 if (op)
585 ret = core_readl(priv, reg);
586 else
587 core_writel(priv, val, reg);
588
589 reg = reg_readl(priv, REG_SWITCH_CNTRL);
590 reg &= ~MDIO_MASTER_SEL;
591 reg_writel(priv, reg, REG_SWITCH_CNTRL);
592
593 return ret & 0xffff;
594}
595
596static int bcm_sf2_sw_phy_read(struct dsa_switch *ds, int addr, int regnum)
597{
598 /* Intercept reads from the MDIO broadcast address or Broadcom
599 * pseudo-PHY address
600 */
601 switch (addr) {
602 case 0:
603 case 30:
604 return bcm_sf2_sw_indir_rw(ds, 1, addr, regnum, 0);
605 default:
606 return 0xffff;
607 }
608}
609
610static int bcm_sf2_sw_phy_write(struct dsa_switch *ds, int addr, int regnum,
611 u16 val)
612{
613 /* Intercept writes to the MDIO broadcast address or Broadcom
614 * pseudo-PHY address
615 */
616 switch (addr) {
617 case 0:
618 case 30:
619 bcm_sf2_sw_indir_rw(ds, 0, addr, regnum, val);
620 break;
621 }
622
623 return 0;
624}
625
626static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
627 struct phy_device *phydev)
628{
629 struct bcm_sf2_priv *priv = ds_to_priv(ds);
630 u32 id_mode_dis = 0, port_mode;
631 const char *str = NULL;
632 u32 reg;
633
634 switch (phydev->interface) {
635 case PHY_INTERFACE_MODE_RGMII:
636 str = "RGMII (no delay)";
637 id_mode_dis = 1;
638 case PHY_INTERFACE_MODE_RGMII_TXID:
639 if (!str)
640 str = "RGMII (TX delay)";
641 port_mode = EXT_GPHY;
642 break;
643 case PHY_INTERFACE_MODE_MII:
644 str = "MII";
645 port_mode = EXT_EPHY;
646 break;
647 case PHY_INTERFACE_MODE_REVMII:
648 str = "Reverse MII";
649 port_mode = EXT_REVMII;
650 break;
651 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700652 /* All other PHYs: internal and MoCA */
653 goto force_link;
654 }
655
656 /* If the link is down, just disable the interface to conserve power */
657 if (!phydev->link) {
658 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
659 reg &= ~RGMII_MODE_EN;
660 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700661 goto force_link;
662 }
663
664 /* Clear id_mode_dis bit, and the existing port mode, but
665 * make sure we enable the RGMII block for data to pass
666 */
667 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
668 reg &= ~ID_MODE_DIS;
669 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
670 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
671
672 reg |= port_mode | RGMII_MODE_EN;
673 if (id_mode_dis)
674 reg |= ID_MODE_DIS;
675
676 if (phydev->pause) {
677 if (phydev->asym_pause)
678 reg |= TX_PAUSE_EN;
679 reg |= RX_PAUSE_EN;
680 }
681
682 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
683
684 pr_info("Port %d configured for %s\n", port, str);
685
686force_link:
687 /* Force link settings detected from the PHY */
688 reg = SW_OVERRIDE;
689 switch (phydev->speed) {
690 case SPEED_1000:
691 reg |= SPDSTS_1000 << SPEED_SHIFT;
692 break;
693 case SPEED_100:
694 reg |= SPDSTS_100 << SPEED_SHIFT;
695 break;
696 }
697
698 if (phydev->link)
699 reg |= LINK_STS;
700 if (phydev->duplex == DUPLEX_FULL)
701 reg |= DUPLX_MODE;
702
703 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
704}
705
706static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
707 struct fixed_phy_status *status)
708{
709 struct bcm_sf2_priv *priv = ds_to_priv(ds);
Florian Fainelli7855f672014-12-11 18:12:42 -0800710 u32 duplex, pause, speed;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700711 u32 reg;
712
Florian Fainelli246d7f72014-08-27 17:04:56 -0700713 duplex = core_readl(priv, CORE_DUPSTS);
714 pause = core_readl(priv, CORE_PAUSESTS);
715 speed = core_readl(priv, CORE_SPDSTS);
716
717 speed >>= (port * SPDSTS_SHIFT);
718 speed &= SPDSTS_MASK;
719
720 status->link = 0;
721
722 /* Port 7 is special as we do not get link status from CORE_LNKSTS,
723 * which means that we need to force the link at the port override
724 * level to get the data to flow. We do use what the interrupt handler
725 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -0800726 *
727 * For the other ports, we just force the link status, since this is
728 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700729 */
730 if (port == 7) {
731 status->link = priv->port_sts[port].link;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700732 status->duplex = 1;
733 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -0800734 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700735 status->duplex = !!(duplex & (1 << port));
736 }
737
Florian Fainelli7855f672014-12-11 18:12:42 -0800738 reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
739 reg |= SW_OVERRIDE;
740 if (status->link)
741 reg |= LINK_STS;
742 else
743 reg &= ~LINK_STS;
744 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
745
Florian Fainelli246d7f72014-08-27 17:04:56 -0700746 switch (speed) {
747 case SPDSTS_10:
748 status->speed = SPEED_10;
749 break;
750 case SPDSTS_100:
751 status->speed = SPEED_100;
752 break;
753 case SPDSTS_1000:
754 status->speed = SPEED_1000;
755 break;
756 }
757
758 if ((pause & (1 << port)) &&
759 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
760 status->asym_pause = 1;
761 status->pause = 1;
762 }
763
764 if (pause & (1 << port))
765 status->pause = 1;
766}
767
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700768static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
769{
770 struct bcm_sf2_priv *priv = ds_to_priv(ds);
771 unsigned int port;
772
Florian Fainelli691c9a82015-01-20 16:42:00 -0800773 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700774
775 /* Disable all ports physically present including the IMP
776 * port, the other ones have already been disabled during
777 * bcm_sf2_sw_setup
778 */
779 for (port = 0; port < DSA_MAX_PORTS; port++) {
780 if ((1 << port) & ds->phys_port_mask ||
781 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700782 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700783 }
784
785 return 0;
786}
787
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700788static int bcm_sf2_sw_resume(struct dsa_switch *ds)
789{
790 struct bcm_sf2_priv *priv = ds_to_priv(ds);
791 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700792 int ret;
793
794 ret = bcm_sf2_sw_rst(priv);
795 if (ret) {
796 pr_err("%s: failed to software reset switch\n", __func__);
797 return ret;
798 }
799
Florian Fainellib0836682015-02-05 11:40:41 -0800800 if (priv->hw_params.num_gphy == 1)
801 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700802
803 for (port = 0; port < DSA_MAX_PORTS; port++) {
804 if ((1 << port) & ds->phys_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700805 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700806 else if (dsa_is_cpu_port(ds, port))
807 bcm_sf2_imp_setup(ds, port);
808 }
809
810 return 0;
811}
812
Florian Fainelli96e65d72014-09-18 17:31:25 -0700813static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
814 struct ethtool_wolinfo *wol)
815{
816 struct net_device *p = ds->dst[ds->index].master_netdev;
817 struct bcm_sf2_priv *priv = ds_to_priv(ds);
818 struct ethtool_wolinfo pwol;
819
820 /* Get the parent device WoL settings */
821 p->ethtool_ops->get_wol(p, &pwol);
822
823 /* Advertise the parent device supported settings */
824 wol->supported = pwol.supported;
825 memset(&wol->sopass, 0, sizeof(wol->sopass));
826
827 if (pwol.wolopts & WAKE_MAGICSECURE)
828 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
829
830 if (priv->wol_ports_mask & (1 << port))
831 wol->wolopts = pwol.wolopts;
832 else
833 wol->wolopts = 0;
834}
835
836static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
837 struct ethtool_wolinfo *wol)
838{
839 struct net_device *p = ds->dst[ds->index].master_netdev;
840 struct bcm_sf2_priv *priv = ds_to_priv(ds);
841 s8 cpu_port = ds->dst[ds->index].cpu_port;
842 struct ethtool_wolinfo pwol;
843
844 p->ethtool_ops->get_wol(p, &pwol);
845 if (wol->wolopts & ~pwol.supported)
846 return -EINVAL;
847
848 if (wol->wolopts)
849 priv->wol_ports_mask |= (1 << port);
850 else
851 priv->wol_ports_mask &= ~(1 << port);
852
853 /* If we have at least one port enabled, make sure the CPU port
854 * is also enabled. If the CPU port is the last one enabled, we disable
855 * it since this configuration does not make sense.
856 */
857 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
858 priv->wol_ports_mask |= (1 << cpu_port);
859 else
860 priv->wol_ports_mask &= ~(1 << cpu_port);
861
862 return p->ethtool_ops->set_wol(p, wol);
863}
864
Florian Fainelli246d7f72014-08-27 17:04:56 -0700865static struct dsa_switch_driver bcm_sf2_switch_driver = {
Florian Fainelliac7a04c2014-09-11 21:18:09 -0700866 .tag_protocol = DSA_TAG_PROTO_BRCM,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700867 .priv_size = sizeof(struct bcm_sf2_priv),
868 .probe = bcm_sf2_sw_probe,
869 .setup = bcm_sf2_sw_setup,
870 .set_addr = bcm_sf2_sw_set_addr,
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700871 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700872 .phy_read = bcm_sf2_sw_phy_read,
873 .phy_write = bcm_sf2_sw_phy_write,
874 .get_strings = bcm_sf2_sw_get_strings,
875 .get_ethtool_stats = bcm_sf2_sw_get_ethtool_stats,
876 .get_sset_count = bcm_sf2_sw_get_sset_count,
877 .adjust_link = bcm_sf2_sw_adjust_link,
878 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700879 .suspend = bcm_sf2_sw_suspend,
880 .resume = bcm_sf2_sw_resume,
Florian Fainelli96e65d72014-09-18 17:31:25 -0700881 .get_wol = bcm_sf2_sw_get_wol,
882 .set_wol = bcm_sf2_sw_set_wol,
Florian Fainellib6d045d2014-09-24 17:05:20 -0700883 .port_enable = bcm_sf2_port_setup,
884 .port_disable = bcm_sf2_port_disable,
Florian Fainelli450b05c2014-09-24 17:05:22 -0700885 .get_eee = bcm_sf2_sw_get_eee,
886 .set_eee = bcm_sf2_sw_set_eee,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700887};
888
889static int __init bcm_sf2_init(void)
890{
891 register_switch_driver(&bcm_sf2_switch_driver);
892
893 return 0;
894}
895module_init(bcm_sf2_init);
896
897static void __exit bcm_sf2_exit(void)
898{
899 unregister_switch_driver(&bcm_sf2_switch_driver);
900}
901module_exit(bcm_sf2_exit);
902
903MODULE_AUTHOR("Broadcom Corporation");
904MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
905MODULE_LICENSE("GPL");
906MODULE_ALIAS("platform:brcm-sf2");