blob: ddd477354e00d413b54578f2ce1db48839759f51 [file] [log] [blame]
Florian Fainelli246d7f72014-08-27 17:04:56 -07001/*
2 * Broadcom Starfighter 2 DSA switch driver
3 *
4 * Copyright (C) 2014, Broadcom Corporation
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 */
11
12#include <linux/list.h>
13#include <linux/module.h>
14#include <linux/netdevice.h>
15#include <linux/interrupt.h>
16#include <linux/platform_device.h>
17#include <linux/of.h>
18#include <linux/phy.h>
19#include <linux/phy_fixed.h>
20#include <linux/mii.h>
21#include <linux/of.h>
22#include <linux/of_irq.h>
23#include <linux/of_address.h>
Florian Fainelli8b7c94e2015-10-23 12:11:08 -070024#include <linux/of_net.h>
Florian Fainelli461cd1b02016-06-07 16:32:43 -070025#include <linux/of_mdio.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070026#include <net/dsa.h>
Florian Fainelli96e65d72014-09-18 17:31:25 -070027#include <linux/ethtool.h>
Florian Fainelli12f460f2015-02-24 13:15:34 -080028#include <linux/if_bridge.h>
Florian Fainelliaafc66f2015-06-10 18:08:01 -070029#include <linux/brcmphy.h>
Florian Fainelli680060d2015-10-23 11:38:07 -070030#include <linux/etherdevice.h>
31#include <net/switchdev.h>
Florian Fainellif4589952016-08-26 12:18:33 -070032#include <linux/platform_data/b53.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070033
34#include "bcm_sf2.h"
35#include "bcm_sf2_regs.h"
Florian Fainellif4589952016-08-26 12:18:33 -070036#include "b53/b53_priv.h"
37#include "b53/b53_regs.h"
Florian Fainelli246d7f72014-08-27 17:04:56 -070038
Andrew Lunn7b314362016-08-22 16:01:01 +020039static enum dsa_tag_protocol bcm_sf2_sw_get_tag_protocol(struct dsa_switch *ds)
40{
41 return DSA_TAG_PROTO_BRCM;
42}
43
Florian Fainellib6d045d2014-09-24 17:05:20 -070044static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
Florian Fainelli246d7f72014-08-27 17:04:56 -070045{
Florian Fainellif4589952016-08-26 12:18:33 -070046 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -070047 unsigned int i;
Florian Fainellib6d045d2014-09-24 17:05:20 -070048 u32 reg;
49
50 /* Enable the IMP Port to be in the same VLAN as the other ports
51 * on a per-port basis such that we only have Port i and IMP in
52 * the same VLAN.
53 */
54 for (i = 0; i < priv->hw_params.num_ports; i++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +020055 if (!((1 << i) & ds->enabled_port_mask))
Florian Fainellib6d045d2014-09-24 17:05:20 -070056 continue;
57
58 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
59 reg |= (1 << cpu_port);
60 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
61 }
62}
63
64static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
65{
Florian Fainellif4589952016-08-26 12:18:33 -070066 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -070067 u32 reg, val;
68
69 /* Enable the port memories */
70 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
71 reg &= ~P_TXQ_PSM_VDD(port);
72 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
73
74 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
75 reg = core_readl(priv, CORE_IMP_CTL);
76 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
77 reg &= ~(RX_DIS | TX_DIS);
78 core_writel(priv, reg, CORE_IMP_CTL);
79
80 /* Enable forwarding */
81 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
82
83 /* Enable IMP port in dumb mode */
84 reg = core_readl(priv, CORE_SWITCH_CTRL);
85 reg |= MII_DUMB_FWDG_EN;
86 core_writel(priv, reg, CORE_SWITCH_CTRL);
87
88 /* Resolve which bit controls the Broadcom tag */
89 switch (port) {
90 case 8:
91 val = BRCM_HDR_EN_P8;
92 break;
93 case 7:
94 val = BRCM_HDR_EN_P7;
95 break;
96 case 5:
97 val = BRCM_HDR_EN_P5;
98 break;
99 default:
100 val = 0;
101 break;
102 }
103
104 /* Enable Broadcom tags for IMP port */
105 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
106 reg |= val;
107 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
108
109 /* Enable reception Broadcom tag for CPU TX (switch RX) to
110 * allow us to tag outgoing frames
111 */
112 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
113 reg &= ~(1 << port);
114 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
115
116 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
117 * allow delivering frames to the per-port net_devices
118 */
119 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
120 reg &= ~(1 << port);
121 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
122
123 /* Force link status for IMP port */
124 reg = core_readl(priv, CORE_STS_OVERRIDE_IMP);
125 reg |= (MII_SW_OR | LINK_STS);
126 core_writel(priv, reg, CORE_STS_OVERRIDE_IMP);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700127}
128
Florian Fainelli450b05c2014-09-24 17:05:22 -0700129static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
130{
Florian Fainellif4589952016-08-26 12:18:33 -0700131 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700132 u32 reg;
133
134 reg = core_readl(priv, CORE_EEE_EN_CTRL);
135 if (enable)
136 reg |= 1 << port;
137 else
138 reg &= ~(1 << port);
139 core_writel(priv, reg, CORE_EEE_EN_CTRL);
140}
141
Florian Fainellib0836682015-02-05 11:40:41 -0800142static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
143{
Florian Fainellif4589952016-08-26 12:18:33 -0700144 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -0800145 u32 reg;
146
Florian Fainelli9af197a2015-02-05 11:40:42 -0800147 reg = reg_readl(priv, REG_SPHY_CNTRL);
148 if (enable) {
149 reg |= PHY_RESET;
150 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
151 reg_writel(priv, reg, REG_SPHY_CNTRL);
152 udelay(21);
153 reg = reg_readl(priv, REG_SPHY_CNTRL);
154 reg &= ~PHY_RESET;
155 } else {
156 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
157 reg_writel(priv, reg, REG_SPHY_CNTRL);
158 mdelay(1);
159 reg |= CK25_DIS;
160 }
161 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800162
Florian Fainelli9af197a2015-02-05 11:40:42 -0800163 /* Use PHY-driven LED signaling */
164 if (!enable) {
165 reg = reg_readl(priv, REG_LED_CNTRL(0));
166 reg |= SPDLNK_SRC_SEL;
167 reg_writel(priv, reg, REG_LED_CNTRL(0));
168 }
Florian Fainellib0836682015-02-05 11:40:41 -0800169}
170
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700171static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
172 int port)
173{
174 unsigned int off;
175
176 switch (port) {
177 case 7:
178 off = P7_IRQ_OFF;
179 break;
180 case 0:
181 /* Port 0 interrupts are located on the first bank */
182 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
183 return;
184 default:
185 off = P_IRQ_OFF(port);
186 break;
187 }
188
189 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
190}
191
192static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
193 int port)
194{
195 unsigned int off;
196
197 switch (port) {
198 case 7:
199 off = P7_IRQ_OFF;
200 break;
201 case 0:
202 /* Port 0 interrupts are located on the first bank */
203 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
204 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
205 return;
206 default:
207 off = P_IRQ_OFF(port);
208 break;
209 }
210
211 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
212 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
213}
214
Florian Fainellib6d045d2014-09-24 17:05:20 -0700215static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
216 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700217{
Florian Fainellif4589952016-08-26 12:18:33 -0700218 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700219 s8 cpu_port = ds->dst[ds->index].cpu_port;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700220 u32 reg;
221
222 /* Clear the memory power down */
223 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
224 reg &= ~P_TXQ_PSM_VDD(port);
225 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
226
227 /* Clear the Rx and Tx disable bits and set to no spanning tree */
228 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
229
Florian Fainelli9af197a2015-02-05 11:40:42 -0800230 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700231 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800232 bcm_sf2_gphy_enable_set(ds, true);
233 if (phy) {
234 /* if phy_stop() has been called before, phy
235 * will be in halted state, and phy_start()
236 * will call resume.
237 *
238 * the resume path does not configure back
239 * autoneg settings, and since we hard reset
240 * the phy manually here, we need to reset the
241 * state machine also.
242 */
243 phy->state = PHY_READY;
244 phy_init_hw(phy);
245 }
246 }
247
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700248 /* Enable MoCA port interrupts to get notified */
249 if (port == priv->moca_port)
250 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700251
Florian Fainelli12f460f2015-02-24 13:15:34 -0800252 /* Set this port, and only this one to be in the default VLAN,
253 * if member of a bridge, restore its membership prior to
254 * bringing down this port.
255 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700256 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
257 reg &= ~PORT_VLAN_CTRL_MASK;
258 reg |= (1 << port);
Florian Fainelli12f460f2015-02-24 13:15:34 -0800259 reg |= priv->port_sts[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700260 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700261
262 bcm_sf2_imp_vlan_setup(ds, cpu_port);
263
Florian Fainelli450b05c2014-09-24 17:05:22 -0700264 /* If EEE was enabled, restore it */
265 if (priv->port_sts[port].eee.eee_enabled)
266 bcm_sf2_eee_enable_set(ds, port, true);
267
Florian Fainellib6d045d2014-09-24 17:05:20 -0700268 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700269}
270
Florian Fainellib6d045d2014-09-24 17:05:20 -0700271static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
272 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700273{
Florian Fainellif4589952016-08-26 12:18:33 -0700274 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700275 u32 off, reg;
276
Florian Fainelli96e65d72014-09-18 17:31:25 -0700277 if (priv->wol_ports_mask & (1 << port))
278 return;
279
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700280 if (port == priv->moca_port)
281 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700282
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700283 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800284 bcm_sf2_gphy_enable_set(ds, false);
285
Florian Fainelli246d7f72014-08-27 17:04:56 -0700286 if (dsa_is_cpu_port(ds, port))
287 off = CORE_IMP_CTL;
288 else
289 off = CORE_G_PCTL_PORT(port);
290
291 reg = core_readl(priv, off);
292 reg |= RX_DIS | TX_DIS;
293 core_writel(priv, reg, off);
294
295 /* Power down the port memory */
296 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
297 reg |= P_TXQ_PSM_VDD(port);
298 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
299}
300
Florian Fainelli450b05c2014-09-24 17:05:22 -0700301/* Returns 0 if EEE was not enabled, or 1 otherwise
302 */
303static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
304 struct phy_device *phy)
305{
Florian Fainellif4589952016-08-26 12:18:33 -0700306 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700307 struct ethtool_eee *p = &priv->port_sts[port].eee;
308 int ret;
309
310 p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
311
312 ret = phy_init_eee(phy, 0);
313 if (ret)
314 return 0;
315
316 bcm_sf2_eee_enable_set(ds, port, true);
317
318 return 1;
319}
320
321static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
322 struct ethtool_eee *e)
323{
Florian Fainellif4589952016-08-26 12:18:33 -0700324 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700325 struct ethtool_eee *p = &priv->port_sts[port].eee;
326 u32 reg;
327
328 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
329 e->eee_enabled = p->eee_enabled;
330 e->eee_active = !!(reg & (1 << port));
331
332 return 0;
333}
334
335static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
336 struct phy_device *phydev,
337 struct ethtool_eee *e)
338{
Florian Fainellif4589952016-08-26 12:18:33 -0700339 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700340 struct ethtool_eee *p = &priv->port_sts[port].eee;
341
342 p->eee_enabled = e->eee_enabled;
343
344 if (!p->eee_enabled) {
345 bcm_sf2_eee_enable_set(ds, port, false);
346 } else {
347 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
348 if (!p->eee_enabled)
349 return -EOPNOTSUPP;
350 }
351
352 return 0;
353}
354
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700355static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
356 int regnum, u16 val)
357{
358 int ret = 0;
359 u32 reg;
360
361 reg = reg_readl(priv, REG_SWITCH_CNTRL);
362 reg |= MDIO_MASTER_SEL;
363 reg_writel(priv, reg, REG_SWITCH_CNTRL);
364
365 /* Page << 8 | offset */
366 reg = 0x70;
367 reg <<= 2;
368 core_writel(priv, addr, reg);
369
370 /* Page << 8 | offset */
371 reg = 0x80 << 8 | regnum << 1;
372 reg <<= 2;
373
374 if (op)
375 ret = core_readl(priv, reg);
376 else
377 core_writel(priv, val, reg);
378
379 reg = reg_readl(priv, REG_SWITCH_CNTRL);
380 reg &= ~MDIO_MASTER_SEL;
381 reg_writel(priv, reg, REG_SWITCH_CNTRL);
382
383 return ret & 0xffff;
384}
385
386static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
387{
388 struct bcm_sf2_priv *priv = bus->priv;
389
390 /* Intercept reads from Broadcom pseudo-PHY address, else, send
391 * them to our master MDIO bus controller
392 */
393 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
394 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
395 else
396 return mdiobus_read(priv->master_mii_bus, addr, regnum);
397}
398
399static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
400 u16 val)
401{
402 struct bcm_sf2_priv *priv = bus->priv;
403
404 /* Intercept writes to the Broadcom pseudo-PHY address, else,
405 * send them to our master MDIO bus controller
406 */
407 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
408 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
409 else
410 mdiobus_write(priv->master_mii_bus, addr, regnum, val);
411
412 return 0;
413}
414
Florian Fainelli246d7f72014-08-27 17:04:56 -0700415static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
416{
417 struct bcm_sf2_priv *priv = dev_id;
418
419 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
420 ~priv->irq0_mask;
421 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
422
423 return IRQ_HANDLED;
424}
425
426static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
427{
428 struct bcm_sf2_priv *priv = dev_id;
429
430 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
431 ~priv->irq1_mask;
432 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
433
434 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
435 priv->port_sts[7].link = 1;
436 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
437 priv->port_sts[7].link = 0;
438
439 return IRQ_HANDLED;
440}
441
Florian Fainelli33f84612014-11-25 18:08:49 -0800442static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
443{
444 unsigned int timeout = 1000;
445 u32 reg;
446
447 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
448 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
449 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
450
451 do {
452 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
453 if (!(reg & SOFTWARE_RESET))
454 break;
455
456 usleep_range(1000, 2000);
457 } while (timeout-- > 0);
458
459 if (timeout == 0)
460 return -ETIMEDOUT;
461
462 return 0;
463}
464
Florian Fainelli691c9a82015-01-20 16:42:00 -0800465static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
466{
467 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
468 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
469 intrl2_0_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
470 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_MASK_SET);
471 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
472 intrl2_1_writel(priv, 0, INTRL2_CPU_MASK_CLEAR);
473}
474
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700475static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
476 struct device_node *dn)
477{
478 struct device_node *port;
479 const char *phy_mode_str;
480 int mode;
481 unsigned int port_num;
482 int ret;
483
484 priv->moca_port = -1;
485
486 for_each_available_child_of_node(dn, port) {
487 if (of_property_read_u32(port, "reg", &port_num))
488 continue;
489
490 /* Internal PHYs get assigned a specific 'phy-mode' property
491 * value: "internal" to help flag them before MDIO probing
492 * has completed, since they might be turned off at that
493 * time
494 */
495 mode = of_get_phy_mode(port);
496 if (mode < 0) {
497 ret = of_property_read_string(port, "phy-mode",
498 &phy_mode_str);
499 if (ret < 0)
500 continue;
501
502 if (!strcasecmp(phy_mode_str, "internal"))
503 priv->int_phy_mask |= 1 << port_num;
504 }
505
506 if (mode == PHY_INTERFACE_MODE_MOCA)
507 priv->moca_port = port_num;
508 }
509}
510
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700511static int bcm_sf2_mdio_register(struct dsa_switch *ds)
512{
Florian Fainellif4589952016-08-26 12:18:33 -0700513 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700514 struct device_node *dn;
515 static int index;
516 int err;
517
518 /* Find our integrated MDIO bus node */
519 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
520 priv->master_mii_bus = of_mdio_find_bus(dn);
521 if (!priv->master_mii_bus)
522 return -EPROBE_DEFER;
523
524 get_device(&priv->master_mii_bus->dev);
525 priv->master_mii_dn = dn;
526
527 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
528 if (!priv->slave_mii_bus)
529 return -ENOMEM;
530
531 priv->slave_mii_bus->priv = priv;
532 priv->slave_mii_bus->name = "sf2 slave mii";
533 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
534 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
535 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
536 index++);
537 priv->slave_mii_bus->dev.of_node = dn;
538
539 /* Include the pseudo-PHY address to divert reads towards our
540 * workaround. This is only required for 7445D0, since 7445E0
541 * disconnects the internal switch pseudo-PHY such that we can use the
542 * regular SWITCH_MDIO master controller instead.
543 *
544 * Here we flag the pseudo PHY as needing special treatment and would
545 * otherwise make all other PHY read/writes go to the master MDIO bus
546 * controller that comes with this switch backed by the "mdio-unimac"
547 * driver.
548 */
549 if (of_machine_is_compatible("brcm,bcm7445d0"))
550 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
551 else
552 priv->indir_phy_mask = 0;
553
554 ds->phys_mii_mask = priv->indir_phy_mask;
555 ds->slave_mii_bus = priv->slave_mii_bus;
556 priv->slave_mii_bus->parent = ds->dev->parent;
557 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
558
559 if (dn)
560 err = of_mdiobus_register(priv->slave_mii_bus, dn);
561 else
562 err = mdiobus_register(priv->slave_mii_bus);
563
564 if (err)
565 of_node_put(dn);
566
567 return err;
568}
569
570static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
571{
572 mdiobus_unregister(priv->slave_mii_bus);
573 if (priv->master_mii_dn)
574 of_node_put(priv->master_mii_dn);
575}
576
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700577static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
578{
Florian Fainellif4589952016-08-26 12:18:33 -0700579 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700580
581 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
582 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
583 * the REG_PHY_REVISION register layout is.
584 */
585
586 return priv->hw_params.gphy_rev;
587}
588
Florian Fainelli246d7f72014-08-27 17:04:56 -0700589static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
590 struct phy_device *phydev)
591{
Florian Fainellif4589952016-08-26 12:18:33 -0700592 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700593 u32 id_mode_dis = 0, port_mode;
594 const char *str = NULL;
595 u32 reg;
596
597 switch (phydev->interface) {
598 case PHY_INTERFACE_MODE_RGMII:
599 str = "RGMII (no delay)";
600 id_mode_dis = 1;
601 case PHY_INTERFACE_MODE_RGMII_TXID:
602 if (!str)
603 str = "RGMII (TX delay)";
604 port_mode = EXT_GPHY;
605 break;
606 case PHY_INTERFACE_MODE_MII:
607 str = "MII";
608 port_mode = EXT_EPHY;
609 break;
610 case PHY_INTERFACE_MODE_REVMII:
611 str = "Reverse MII";
612 port_mode = EXT_REVMII;
613 break;
614 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700615 /* All other PHYs: internal and MoCA */
616 goto force_link;
617 }
618
619 /* If the link is down, just disable the interface to conserve power */
620 if (!phydev->link) {
621 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
622 reg &= ~RGMII_MODE_EN;
623 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700624 goto force_link;
625 }
626
627 /* Clear id_mode_dis bit, and the existing port mode, but
628 * make sure we enable the RGMII block for data to pass
629 */
630 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
631 reg &= ~ID_MODE_DIS;
632 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
633 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
634
635 reg |= port_mode | RGMII_MODE_EN;
636 if (id_mode_dis)
637 reg |= ID_MODE_DIS;
638
639 if (phydev->pause) {
640 if (phydev->asym_pause)
641 reg |= TX_PAUSE_EN;
642 reg |= RX_PAUSE_EN;
643 }
644
645 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
646
647 pr_info("Port %d configured for %s\n", port, str);
648
649force_link:
650 /* Force link settings detected from the PHY */
651 reg = SW_OVERRIDE;
652 switch (phydev->speed) {
653 case SPEED_1000:
654 reg |= SPDSTS_1000 << SPEED_SHIFT;
655 break;
656 case SPEED_100:
657 reg |= SPDSTS_100 << SPEED_SHIFT;
658 break;
659 }
660
661 if (phydev->link)
662 reg |= LINK_STS;
663 if (phydev->duplex == DUPLEX_FULL)
664 reg |= DUPLX_MODE;
665
666 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
667}
668
669static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
670 struct fixed_phy_status *status)
671{
Florian Fainellif4589952016-08-26 12:18:33 -0700672 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellid2eac982015-07-20 17:49:55 -0700673 u32 duplex, pause;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700674 u32 reg;
675
Florian Fainelli246d7f72014-08-27 17:04:56 -0700676 duplex = core_readl(priv, CORE_DUPSTS);
677 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700678
679 status->link = 0;
680
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700681 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700682 * which means that we need to force the link at the port override
683 * level to get the data to flow. We do use what the interrupt handler
684 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -0800685 *
686 * For the other ports, we just force the link status, since this is
687 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700688 */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700689 if (port == priv->moca_port) {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700690 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -0700691 /* For MoCA interfaces, also force a link down notification
692 * since some version of the user-space daemon (mocad) use
693 * cmd->autoneg to force the link, which messes up the PHY
694 * state machine and make it go in PHY_FORCING state instead.
695 */
696 if (!status->link)
Andrew Lunnc8b09802016-06-04 21:16:57 +0200697 netif_carrier_off(ds->ports[port].netdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700698 status->duplex = 1;
699 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -0800700 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700701 status->duplex = !!(duplex & (1 << port));
702 }
703
Florian Fainelli7855f672014-12-11 18:12:42 -0800704 reg = core_readl(priv, CORE_STS_OVERRIDE_GMIIP_PORT(port));
705 reg |= SW_OVERRIDE;
706 if (status->link)
707 reg |= LINK_STS;
708 else
709 reg &= ~LINK_STS;
710 core_writel(priv, reg, CORE_STS_OVERRIDE_GMIIP_PORT(port));
711
Florian Fainelli246d7f72014-08-27 17:04:56 -0700712 if ((pause & (1 << port)) &&
713 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
714 status->asym_pause = 1;
715 status->pause = 1;
716 }
717
718 if (pause & (1 << port))
719 status->pause = 1;
720}
721
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700722static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
723{
Florian Fainellif4589952016-08-26 12:18:33 -0700724 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700725 unsigned int port;
726
Florian Fainelli691c9a82015-01-20 16:42:00 -0800727 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700728
729 /* Disable all ports physically present including the IMP
730 * port, the other ones have already been disabled during
731 * bcm_sf2_sw_setup
732 */
733 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200734 if ((1 << port) & ds->enabled_port_mask ||
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700735 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700736 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700737 }
738
739 return 0;
740}
741
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700742static int bcm_sf2_sw_resume(struct dsa_switch *ds)
743{
Florian Fainellif4589952016-08-26 12:18:33 -0700744 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700745 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700746 int ret;
747
748 ret = bcm_sf2_sw_rst(priv);
749 if (ret) {
750 pr_err("%s: failed to software reset switch\n", __func__);
751 return ret;
752 }
753
Florian Fainellib0836682015-02-05 11:40:41 -0800754 if (priv->hw_params.num_gphy == 1)
755 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700756
757 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200758 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700759 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700760 else if (dsa_is_cpu_port(ds, port))
761 bcm_sf2_imp_setup(ds, port);
762 }
763
764 return 0;
765}
766
Florian Fainelli96e65d72014-09-18 17:31:25 -0700767static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
768 struct ethtool_wolinfo *wol)
769{
770 struct net_device *p = ds->dst[ds->index].master_netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700771 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700772 struct ethtool_wolinfo pwol;
773
774 /* Get the parent device WoL settings */
775 p->ethtool_ops->get_wol(p, &pwol);
776
777 /* Advertise the parent device supported settings */
778 wol->supported = pwol.supported;
779 memset(&wol->sopass, 0, sizeof(wol->sopass));
780
781 if (pwol.wolopts & WAKE_MAGICSECURE)
782 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
783
784 if (priv->wol_ports_mask & (1 << port))
785 wol->wolopts = pwol.wolopts;
786 else
787 wol->wolopts = 0;
788}
789
790static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
791 struct ethtool_wolinfo *wol)
792{
793 struct net_device *p = ds->dst[ds->index].master_netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700794 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700795 s8 cpu_port = ds->dst[ds->index].cpu_port;
796 struct ethtool_wolinfo pwol;
797
798 p->ethtool_ops->get_wol(p, &pwol);
799 if (wol->wolopts & ~pwol.supported)
800 return -EINVAL;
801
802 if (wol->wolopts)
803 priv->wol_ports_mask |= (1 << port);
804 else
805 priv->wol_ports_mask &= ~(1 << port);
806
807 /* If we have at least one port enabled, make sure the CPU port
808 * is also enabled. If the CPU port is the last one enabled, we disable
809 * it since this configuration does not make sense.
810 */
811 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
812 priv->wol_ports_mask |= (1 << cpu_port);
813 else
814 priv->wol_ports_mask &= ~(1 << cpu_port);
815
816 return p->ethtool_ops->set_wol(p, wol);
817}
818
Florian Fainellide0b9d32016-08-26 12:18:34 -0700819static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
Florian Fainelli9c57a772016-06-09 17:42:08 -0700820{
Florian Fainellide0b9d32016-08-26 12:18:34 -0700821 unsigned int timeout = 10;
822 u32 reg;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700823
Florian Fainellide0b9d32016-08-26 12:18:34 -0700824 do {
825 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
826 if (!(reg & ARLA_VTBL_STDN))
827 return 0;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700828
Florian Fainellide0b9d32016-08-26 12:18:34 -0700829 usleep_range(1000, 2000);
830 } while (timeout--);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700831
Florian Fainellide0b9d32016-08-26 12:18:34 -0700832 return -ETIMEDOUT;
833}
Florian Fainelli9c57a772016-06-09 17:42:08 -0700834
Florian Fainellide0b9d32016-08-26 12:18:34 -0700835static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
836{
837 core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
838
839 return bcm_sf2_vlan_op_wait(priv);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700840}
841
842static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
843{
Florian Fainellif4589952016-08-26 12:18:33 -0700844 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700845 unsigned int port;
846
847 /* Clear all VLANs */
848 bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
849
850 for (port = 0; port < priv->hw_params.num_ports; port++) {
851 if (!((1 << port) & ds->enabled_port_mask))
852 continue;
853
854 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
855 }
856}
857
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700858static int bcm_sf2_sw_setup(struct dsa_switch *ds)
859{
Florian Fainellif4589952016-08-26 12:18:33 -0700860 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700861 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700862
863 /* Enable all valid ports and disable those unused */
864 for (port = 0; port < priv->hw_params.num_ports; port++) {
865 /* IMP port receives special treatment */
866 if ((1 << port) & ds->enabled_port_mask)
867 bcm_sf2_port_setup(ds, port, NULL);
868 else if (dsa_is_cpu_port(ds, port))
869 bcm_sf2_imp_setup(ds, port);
870 else
871 bcm_sf2_port_disable(ds, port, NULL);
872 }
873
874 bcm_sf2_sw_configure_vlan(ds);
875
876 return 0;
877}
878
Florian Fainellif4589952016-08-26 12:18:33 -0700879/* The SWITCH_CORE register space is managed by b53 but operates on a page +
880 * register basis so we need to translate that into an address that the
881 * bus-glue understands.
882 */
883#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
884
885static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
886 u8 *val)
887{
888 struct bcm_sf2_priv *priv = dev->priv;
889
890 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
891
892 return 0;
893}
894
895static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
896 u16 *val)
897{
898 struct bcm_sf2_priv *priv = dev->priv;
899
900 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
901
902 return 0;
903}
904
905static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
906 u32 *val)
907{
908 struct bcm_sf2_priv *priv = dev->priv;
909
910 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
911
912 return 0;
913}
914
915static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
916 u64 *val)
917{
918 struct bcm_sf2_priv *priv = dev->priv;
919
920 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
921
922 return 0;
923}
924
925static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
926 u8 value)
927{
928 struct bcm_sf2_priv *priv = dev->priv;
929
930 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
931
932 return 0;
933}
934
935static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
936 u16 value)
937{
938 struct bcm_sf2_priv *priv = dev->priv;
939
940 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
941
942 return 0;
943}
944
945static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
946 u32 value)
947{
948 struct bcm_sf2_priv *priv = dev->priv;
949
950 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
951
952 return 0;
953}
954
955static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
956 u64 value)
957{
958 struct bcm_sf2_priv *priv = dev->priv;
959
960 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
961
962 return 0;
963}
964
965struct b53_io_ops bcm_sf2_io_ops = {
966 .read8 = bcm_sf2_core_read8,
967 .read16 = bcm_sf2_core_read16,
968 .read32 = bcm_sf2_core_read32,
969 .read48 = bcm_sf2_core_read64,
970 .read64 = bcm_sf2_core_read64,
971 .write8 = bcm_sf2_core_write8,
972 .write16 = bcm_sf2_core_write16,
973 .write32 = bcm_sf2_core_write32,
974 .write48 = bcm_sf2_core_write64,
975 .write64 = bcm_sf2_core_write64,
976};
977
Florian Fainellid9338022016-08-18 15:30:14 -0700978static int bcm_sf2_sw_probe(struct platform_device *pdev)
979{
980 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
981 struct device_node *dn = pdev->dev.of_node;
Florian Fainellif4589952016-08-26 12:18:33 -0700982 struct b53_platform_data *pdata;
Florian Fainellid9338022016-08-18 15:30:14 -0700983 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -0700984 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -0700985 struct dsa_switch *ds;
986 void __iomem **base;
Florian Fainelli4bd11672016-08-18 15:30:15 -0700987 struct resource *r;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700988 unsigned int i;
989 u32 reg, rev;
990 int ret;
991
Florian Fainellif4589952016-08-26 12:18:33 -0700992 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
993 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -0700994 return -ENOMEM;
995
Florian Fainellif4589952016-08-26 12:18:33 -0700996 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
997 if (!dev)
998 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -0700999
Florian Fainellif4589952016-08-26 12:18:33 -07001000 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1001 if (!pdata)
1002 return -ENOMEM;
1003
1004 /* Auto-detection using standard registers will not work, so
1005 * provide an indication of what kind of device we are for
1006 * b53_common to work with
1007 */
1008 pdata->chip_id = BCM7445_DEVICE_ID;
1009 dev->pdata = pdata;
1010
1011 priv->dev = dev;
1012 ds = dev->ds;
1013
1014 /* Override the parts that are non-standard wrt. normal b53 devices */
1015 ds->ops->get_tag_protocol = bcm_sf2_sw_get_tag_protocol;
1016 ds->ops->setup = bcm_sf2_sw_setup;
1017 ds->ops->get_phy_flags = bcm_sf2_sw_get_phy_flags;
1018 ds->ops->adjust_link = bcm_sf2_sw_adjust_link;
1019 ds->ops->fixed_link_update = bcm_sf2_sw_fixed_link_update;
1020 ds->ops->suspend = bcm_sf2_sw_suspend;
1021 ds->ops->resume = bcm_sf2_sw_resume;
1022 ds->ops->get_wol = bcm_sf2_sw_get_wol;
1023 ds->ops->set_wol = bcm_sf2_sw_set_wol;
1024 ds->ops->port_enable = bcm_sf2_port_setup;
1025 ds->ops->port_disable = bcm_sf2_port_disable;
1026 ds->ops->get_eee = bcm_sf2_sw_get_eee;
1027 ds->ops->set_eee = bcm_sf2_sw_set_eee;
1028
1029 /* Avoid having DSA free our slave MDIO bus (checking for
1030 * ds->slave_mii_bus and ds->ops->phy_read being non-NULL)
1031 */
1032 ds->ops->phy_read = NULL;
1033
1034 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001035
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001036 spin_lock_init(&priv->indir_lock);
1037 mutex_init(&priv->stats_mutex);
1038
Florian Fainellid9338022016-08-18 15:30:14 -07001039 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001040
1041 priv->irq0 = irq_of_parse_and_map(dn, 0);
1042 priv->irq1 = irq_of_parse_and_map(dn, 1);
1043
1044 base = &priv->core;
1045 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainelli4bd11672016-08-18 15:30:15 -07001046 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1047 *base = devm_ioremap_resource(&pdev->dev, r);
1048 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001049 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001050 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001051 }
1052 base++;
1053 }
1054
1055 ret = bcm_sf2_sw_rst(priv);
1056 if (ret) {
1057 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001058 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001059 }
1060
1061 ret = bcm_sf2_mdio_register(ds);
1062 if (ret) {
1063 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001064 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001065 }
1066
1067 /* Disable all interrupts and request them */
1068 bcm_sf2_intr_disable(priv);
1069
Florian Fainelli4bd11672016-08-18 15:30:15 -07001070 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1071 "switch_0", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001072 if (ret < 0) {
1073 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001074 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001075 }
1076
Florian Fainelli4bd11672016-08-18 15:30:15 -07001077 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1078 "switch_1", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001079 if (ret < 0) {
1080 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001081 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001082 }
1083
1084 /* Reset the MIB counters */
1085 reg = core_readl(priv, CORE_GMNCFGCFG);
1086 reg |= RST_MIB_CNT;
1087 core_writel(priv, reg, CORE_GMNCFGCFG);
1088 reg &= ~RST_MIB_CNT;
1089 core_writel(priv, reg, CORE_GMNCFGCFG);
1090
1091 /* Get the maximum number of ports for this switch */
1092 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1093 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1094 priv->hw_params.num_ports = DSA_MAX_PORTS;
1095
1096 /* Assume a single GPHY setup if we can't read that property */
1097 if (of_property_read_u32(dn, "brcm,num-gphy",
1098 &priv->hw_params.num_gphy))
1099 priv->hw_params.num_gphy = 1;
1100
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001101 rev = reg_readl(priv, REG_SWITCH_REVISION);
1102 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1103 SWITCH_TOP_REV_MASK;
1104 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1105
1106 rev = reg_readl(priv, REG_PHY_REVISION);
1107 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1108
Florian Fainellif4589952016-08-26 12:18:33 -07001109 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001110 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001111 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001112
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001113 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1114 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1115 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1116 priv->core, priv->irq0, priv->irq1);
1117
1118 return 0;
1119
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001120out_mdio:
1121 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001122 return ret;
1123}
1124
Florian Fainellid9338022016-08-18 15:30:14 -07001125static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001126{
Florian Fainellif4589952016-08-26 12:18:33 -07001127 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001128
1129 /* Disable all ports and interrupts */
1130 priv->wol_ports_mask = 0;
Florian Fainellif4589952016-08-26 12:18:33 -07001131 bcm_sf2_sw_suspend(priv->dev->ds);
1132 dsa_unregister_switch(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001133 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001134
1135 return 0;
1136}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001137
Florian Fainellid9338022016-08-18 15:30:14 -07001138#ifdef CONFIG_PM_SLEEP
1139static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001140{
Florian Fainellid9338022016-08-18 15:30:14 -07001141 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001142 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001143
Florian Fainellif4589952016-08-26 12:18:33 -07001144 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001145}
Florian Fainellid9338022016-08-18 15:30:14 -07001146
1147static int bcm_sf2_resume(struct device *dev)
1148{
1149 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001150 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001151
Florian Fainellif4589952016-08-26 12:18:33 -07001152 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001153}
1154#endif /* CONFIG_PM_SLEEP */
1155
1156static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1157 bcm_sf2_suspend, bcm_sf2_resume);
1158
1159static const struct of_device_id bcm_sf2_of_match[] = {
1160 { .compatible = "brcm,bcm7445-switch-v4.0" },
1161 { /* sentinel */ },
1162};
1163
1164static struct platform_driver bcm_sf2_driver = {
1165 .probe = bcm_sf2_sw_probe,
1166 .remove = bcm_sf2_sw_remove,
1167 .driver = {
1168 .name = "brcm-sf2",
1169 .of_match_table = bcm_sf2_of_match,
1170 .pm = &bcm_sf2_pm_ops,
1171 },
1172};
1173module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001174
1175MODULE_AUTHOR("Broadcom Corporation");
1176MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1177MODULE_LICENSE("GPL");
1178MODULE_ALIAS("platform:brcm-sf2");