blob: 8eecfd227e068b0b14aa7329189dd381aff756a2 [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
Florian Fainelliebb2ac42017-01-20 12:36:31 -080064static void bcm_sf2_brcm_hdr_setup(struct bcm_sf2_priv *priv, int port)
Florian Fainellib6d045d2014-09-24 17:05:20 -070065{
Florian Fainelliebb2ac42017-01-20 12:36:31 -080066 u32 reg, val;
Florian Fainelli246d7f72014-08-27 17:04:56 -070067
68 /* Resolve which bit controls the Broadcom tag */
69 switch (port) {
70 case 8:
71 val = BRCM_HDR_EN_P8;
72 break;
73 case 7:
74 val = BRCM_HDR_EN_P7;
75 break;
76 case 5:
77 val = BRCM_HDR_EN_P5;
78 break;
79 default:
80 val = 0;
81 break;
82 }
83
84 /* Enable Broadcom tags for IMP port */
85 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
86 reg |= val;
87 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
88
89 /* Enable reception Broadcom tag for CPU TX (switch RX) to
90 * allow us to tag outgoing frames
91 */
92 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
93 reg &= ~(1 << port);
94 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
95
96 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
97 * allow delivering frames to the per-port net_devices
98 */
99 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
100 reg &= ~(1 << port);
101 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800102}
103
104static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
105{
106 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
107 u32 reg, offset;
108
109 if (priv->type == BCM7445_DEVICE_ID)
110 offset = CORE_STS_OVERRIDE_IMP;
111 else
112 offset = CORE_STS_OVERRIDE_IMP2;
113
114 /* Enable the port memories */
115 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
116 reg &= ~P_TXQ_PSM_VDD(port);
117 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
118
119 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
120 reg = core_readl(priv, CORE_IMP_CTL);
121 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
122 reg &= ~(RX_DIS | TX_DIS);
123 core_writel(priv, reg, CORE_IMP_CTL);
124
125 /* Enable forwarding */
126 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
127
128 /* Enable IMP port in dumb mode */
129 reg = core_readl(priv, CORE_SWITCH_CTRL);
130 reg |= MII_DUMB_FWDG_EN;
131 core_writel(priv, reg, CORE_SWITCH_CTRL);
132
133 bcm_sf2_brcm_hdr_setup(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700134
135 /* Force link status for IMP port */
Florian Fainelli0fe99332017-01-20 12:36:30 -0800136 reg = core_readl(priv, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700137 reg |= (MII_SW_OR | LINK_STS);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800138 core_writel(priv, reg, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700139}
140
Florian Fainelli450b05c2014-09-24 17:05:22 -0700141static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
142{
Florian Fainellif4589952016-08-26 12:18:33 -0700143 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700144 u32 reg;
145
146 reg = core_readl(priv, CORE_EEE_EN_CTRL);
147 if (enable)
148 reg |= 1 << port;
149 else
150 reg &= ~(1 << port);
151 core_writel(priv, reg, CORE_EEE_EN_CTRL);
152}
153
Florian Fainellib0836682015-02-05 11:40:41 -0800154static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
155{
Florian Fainellif4589952016-08-26 12:18:33 -0700156 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -0800157 u32 reg;
158
Florian Fainelli9af197a2015-02-05 11:40:42 -0800159 reg = reg_readl(priv, REG_SPHY_CNTRL);
160 if (enable) {
161 reg |= PHY_RESET;
162 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
163 reg_writel(priv, reg, REG_SPHY_CNTRL);
164 udelay(21);
165 reg = reg_readl(priv, REG_SPHY_CNTRL);
166 reg &= ~PHY_RESET;
167 } else {
168 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
169 reg_writel(priv, reg, REG_SPHY_CNTRL);
170 mdelay(1);
171 reg |= CK25_DIS;
172 }
173 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800174
Florian Fainelli9af197a2015-02-05 11:40:42 -0800175 /* Use PHY-driven LED signaling */
176 if (!enable) {
177 reg = reg_readl(priv, REG_LED_CNTRL(0));
178 reg |= SPDLNK_SRC_SEL;
179 reg_writel(priv, reg, REG_LED_CNTRL(0));
180 }
Florian Fainellib0836682015-02-05 11:40:41 -0800181}
182
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700183static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
184 int port)
185{
186 unsigned int off;
187
188 switch (port) {
189 case 7:
190 off = P7_IRQ_OFF;
191 break;
192 case 0:
193 /* Port 0 interrupts are located on the first bank */
194 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
195 return;
196 default:
197 off = P_IRQ_OFF(port);
198 break;
199 }
200
201 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
202}
203
204static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
205 int port)
206{
207 unsigned int off;
208
209 switch (port) {
210 case 7:
211 off = P7_IRQ_OFF;
212 break;
213 case 0:
214 /* Port 0 interrupts are located on the first bank */
215 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
216 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
217 return;
218 default:
219 off = P_IRQ_OFF(port);
220 break;
221 }
222
223 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
224 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
225}
226
Florian Fainellib6d045d2014-09-24 17:05:20 -0700227static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
228 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700229{
Florian Fainellif4589952016-08-26 12:18:33 -0700230 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700231 s8 cpu_port = ds->dst[ds->index].cpu_port;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700232 u32 reg;
233
234 /* Clear the memory power down */
235 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
236 reg &= ~P_TXQ_PSM_VDD(port);
237 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
238
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800239 /* Enable Broadcom tags for that port if requested */
240 if (priv->brcm_tag_mask & BIT(port))
241 bcm_sf2_brcm_hdr_setup(priv, port);
242
Florian Fainelli246d7f72014-08-27 17:04:56 -0700243 /* Clear the Rx and Tx disable bits and set to no spanning tree */
244 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
245
Florian Fainelli9af197a2015-02-05 11:40:42 -0800246 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700247 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800248 bcm_sf2_gphy_enable_set(ds, true);
249 if (phy) {
250 /* if phy_stop() has been called before, phy
251 * will be in halted state, and phy_start()
252 * will call resume.
253 *
254 * the resume path does not configure back
255 * autoneg settings, and since we hard reset
256 * the phy manually here, we need to reset the
257 * state machine also.
258 */
259 phy->state = PHY_READY;
260 phy_init_hw(phy);
261 }
262 }
263
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700264 /* Enable MoCA port interrupts to get notified */
265 if (port == priv->moca_port)
266 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700267
Florian Fainelli12f460f2015-02-24 13:15:34 -0800268 /* Set this port, and only this one to be in the default VLAN,
269 * if member of a bridge, restore its membership prior to
270 * bringing down this port.
271 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700272 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
273 reg &= ~PORT_VLAN_CTRL_MASK;
274 reg |= (1 << port);
Florian Fainelli02154922016-09-10 12:39:03 -0700275 reg |= priv->dev->ports[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700276 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700277
278 bcm_sf2_imp_vlan_setup(ds, cpu_port);
279
Florian Fainelli450b05c2014-09-24 17:05:22 -0700280 /* If EEE was enabled, restore it */
281 if (priv->port_sts[port].eee.eee_enabled)
282 bcm_sf2_eee_enable_set(ds, port, true);
283
Florian Fainellib6d045d2014-09-24 17:05:20 -0700284 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700285}
286
Florian Fainellib6d045d2014-09-24 17:05:20 -0700287static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
288 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700289{
Florian Fainellif4589952016-08-26 12:18:33 -0700290 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700291 u32 off, reg;
292
Florian Fainelli96e65d72014-09-18 17:31:25 -0700293 if (priv->wol_ports_mask & (1 << port))
294 return;
295
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700296 if (port == priv->moca_port)
297 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700298
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700299 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800300 bcm_sf2_gphy_enable_set(ds, false);
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{
Florian Fainellif4589952016-08-26 12:18:33 -0700322 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700323 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{
Florian Fainellif4589952016-08-26 12:18:33 -0700340 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700341 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{
Florian Fainellif4589952016-08-26 12:18:33 -0700355 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700356 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 Fainelli461cd1b02016-06-07 16:32:43 -0700371static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
372 int regnum, u16 val)
373{
374 int ret = 0;
375 u32 reg;
376
377 reg = reg_readl(priv, REG_SWITCH_CNTRL);
378 reg |= MDIO_MASTER_SEL;
379 reg_writel(priv, reg, REG_SWITCH_CNTRL);
380
381 /* Page << 8 | offset */
382 reg = 0x70;
383 reg <<= 2;
384 core_writel(priv, addr, reg);
385
386 /* Page << 8 | offset */
387 reg = 0x80 << 8 | regnum << 1;
388 reg <<= 2;
389
390 if (op)
391 ret = core_readl(priv, reg);
392 else
393 core_writel(priv, val, reg);
394
395 reg = reg_readl(priv, REG_SWITCH_CNTRL);
396 reg &= ~MDIO_MASTER_SEL;
397 reg_writel(priv, reg, REG_SWITCH_CNTRL);
398
399 return ret & 0xffff;
400}
401
402static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
403{
404 struct bcm_sf2_priv *priv = bus->priv;
405
406 /* Intercept reads from Broadcom pseudo-PHY address, else, send
407 * them to our master MDIO bus controller
408 */
409 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
410 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
411 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800412 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700413}
414
415static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
416 u16 val)
417{
418 struct bcm_sf2_priv *priv = bus->priv;
419
420 /* Intercept writes to the Broadcom pseudo-PHY address, else,
421 * send them to our master MDIO bus controller
422 */
423 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
424 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
425 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800426 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700427
428 return 0;
429}
430
Florian Fainelli246d7f72014-08-27 17:04:56 -0700431static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
432{
433 struct bcm_sf2_priv *priv = dev_id;
434
435 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
436 ~priv->irq0_mask;
437 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
438
439 return IRQ_HANDLED;
440}
441
442static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
443{
444 struct bcm_sf2_priv *priv = dev_id;
445
446 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
447 ~priv->irq1_mask;
448 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
449
450 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
451 priv->port_sts[7].link = 1;
452 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
453 priv->port_sts[7].link = 0;
454
455 return IRQ_HANDLED;
456}
457
Florian Fainelli33f84612014-11-25 18:08:49 -0800458static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
459{
460 unsigned int timeout = 1000;
461 u32 reg;
462
463 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
464 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
465 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
466
467 do {
468 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
469 if (!(reg & SOFTWARE_RESET))
470 break;
471
472 usleep_range(1000, 2000);
473 } while (timeout-- > 0);
474
475 if (timeout == 0)
476 return -ETIMEDOUT;
477
478 return 0;
479}
480
Florian Fainelli691c9a82015-01-20 16:42:00 -0800481static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
482{
Florian Fainellif01d5982016-08-25 15:23:41 -0700483 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800484 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700485 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800486 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800487}
488
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700489static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
490 struct device_node *dn)
491{
492 struct device_node *port;
493 const char *phy_mode_str;
494 int mode;
495 unsigned int port_num;
496 int ret;
497
498 priv->moca_port = -1;
499
500 for_each_available_child_of_node(dn, port) {
501 if (of_property_read_u32(port, "reg", &port_num))
502 continue;
503
504 /* Internal PHYs get assigned a specific 'phy-mode' property
505 * value: "internal" to help flag them before MDIO probing
506 * has completed, since they might be turned off at that
507 * time
508 */
509 mode = of_get_phy_mode(port);
510 if (mode < 0) {
511 ret = of_property_read_string(port, "phy-mode",
512 &phy_mode_str);
513 if (ret < 0)
514 continue;
515
516 if (!strcasecmp(phy_mode_str, "internal"))
517 priv->int_phy_mask |= 1 << port_num;
518 }
519
520 if (mode == PHY_INTERFACE_MODE_MOCA)
521 priv->moca_port = port_num;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800522
523 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
524 priv->brcm_tag_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700525 }
526}
527
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700528static int bcm_sf2_mdio_register(struct dsa_switch *ds)
529{
Florian Fainellif4589952016-08-26 12:18:33 -0700530 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700531 struct device_node *dn;
532 static int index;
533 int err;
534
535 /* Find our integrated MDIO bus node */
536 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
537 priv->master_mii_bus = of_mdio_find_bus(dn);
538 if (!priv->master_mii_bus)
539 return -EPROBE_DEFER;
540
541 get_device(&priv->master_mii_bus->dev);
542 priv->master_mii_dn = dn;
543
544 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
545 if (!priv->slave_mii_bus)
546 return -ENOMEM;
547
548 priv->slave_mii_bus->priv = priv;
549 priv->slave_mii_bus->name = "sf2 slave mii";
550 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
551 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
552 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
553 index++);
554 priv->slave_mii_bus->dev.of_node = dn;
555
556 /* Include the pseudo-PHY address to divert reads towards our
557 * workaround. This is only required for 7445D0, since 7445E0
558 * disconnects the internal switch pseudo-PHY such that we can use the
559 * regular SWITCH_MDIO master controller instead.
560 *
561 * Here we flag the pseudo PHY as needing special treatment and would
562 * otherwise make all other PHY read/writes go to the master MDIO bus
563 * controller that comes with this switch backed by the "mdio-unimac"
564 * driver.
565 */
566 if (of_machine_is_compatible("brcm,bcm7445d0"))
567 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
568 else
569 priv->indir_phy_mask = 0;
570
571 ds->phys_mii_mask = priv->indir_phy_mask;
572 ds->slave_mii_bus = priv->slave_mii_bus;
573 priv->slave_mii_bus->parent = ds->dev->parent;
574 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
575
576 if (dn)
577 err = of_mdiobus_register(priv->slave_mii_bus, dn);
578 else
579 err = mdiobus_register(priv->slave_mii_bus);
580
581 if (err)
582 of_node_put(dn);
583
584 return err;
585}
586
587static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
588{
589 mdiobus_unregister(priv->slave_mii_bus);
590 if (priv->master_mii_dn)
591 of_node_put(priv->master_mii_dn);
592}
593
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700594static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
595{
Florian Fainellif4589952016-08-26 12:18:33 -0700596 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700597
598 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
599 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
600 * the REG_PHY_REVISION register layout is.
601 */
602
603 return priv->hw_params.gphy_rev;
604}
605
Florian Fainelli246d7f72014-08-27 17:04:56 -0700606static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
607 struct phy_device *phydev)
608{
Florian Fainellif4589952016-08-26 12:18:33 -0700609 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli76da8702016-11-22 11:40:58 -0800610 struct ethtool_eee *p = &priv->port_sts[port].eee;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700611 u32 id_mode_dis = 0, port_mode;
612 const char *str = NULL;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800613 u32 reg, offset;
614
615 if (priv->type == BCM7445_DEVICE_ID)
616 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
617 else
618 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700619
620 switch (phydev->interface) {
621 case PHY_INTERFACE_MODE_RGMII:
622 str = "RGMII (no delay)";
623 id_mode_dis = 1;
624 case PHY_INTERFACE_MODE_RGMII_TXID:
625 if (!str)
626 str = "RGMII (TX delay)";
627 port_mode = EXT_GPHY;
628 break;
629 case PHY_INTERFACE_MODE_MII:
630 str = "MII";
631 port_mode = EXT_EPHY;
632 break;
633 case PHY_INTERFACE_MODE_REVMII:
634 str = "Reverse MII";
635 port_mode = EXT_REVMII;
636 break;
637 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700638 /* All other PHYs: internal and MoCA */
639 goto force_link;
640 }
641
642 /* If the link is down, just disable the interface to conserve power */
643 if (!phydev->link) {
644 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
645 reg &= ~RGMII_MODE_EN;
646 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700647 goto force_link;
648 }
649
650 /* Clear id_mode_dis bit, and the existing port mode, but
651 * make sure we enable the RGMII block for data to pass
652 */
653 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
654 reg &= ~ID_MODE_DIS;
655 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
656 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
657
658 reg |= port_mode | RGMII_MODE_EN;
659 if (id_mode_dis)
660 reg |= ID_MODE_DIS;
661
662 if (phydev->pause) {
663 if (phydev->asym_pause)
664 reg |= TX_PAUSE_EN;
665 reg |= RX_PAUSE_EN;
666 }
667
668 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
669
670 pr_info("Port %d configured for %s\n", port, str);
671
672force_link:
673 /* Force link settings detected from the PHY */
674 reg = SW_OVERRIDE;
675 switch (phydev->speed) {
676 case SPEED_1000:
677 reg |= SPDSTS_1000 << SPEED_SHIFT;
678 break;
679 case SPEED_100:
680 reg |= SPDSTS_100 << SPEED_SHIFT;
681 break;
682 }
683
684 if (phydev->link)
685 reg |= LINK_STS;
686 if (phydev->duplex == DUPLEX_FULL)
687 reg |= DUPLX_MODE;
688
Florian Fainelli0fe99332017-01-20 12:36:30 -0800689 core_writel(priv, reg, offset);
Florian Fainelli76da8702016-11-22 11:40:58 -0800690
691 if (!phydev->is_pseudo_fixed_link)
692 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700693}
694
695static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
696 struct fixed_phy_status *status)
697{
Florian Fainellif4589952016-08-26 12:18:33 -0700698 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800699 u32 duplex, pause, offset;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700700 u32 reg;
701
Florian Fainelli0fe99332017-01-20 12:36:30 -0800702 if (priv->type == BCM7445_DEVICE_ID)
703 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
704 else
705 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
706
Florian Fainelli246d7f72014-08-27 17:04:56 -0700707 duplex = core_readl(priv, CORE_DUPSTS);
708 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700709
710 status->link = 0;
711
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700712 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700713 * which means that we need to force the link at the port override
714 * level to get the data to flow. We do use what the interrupt handler
715 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -0800716 *
717 * For the other ports, we just force the link status, since this is
718 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700719 */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700720 if (port == priv->moca_port) {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700721 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -0700722 /* For MoCA interfaces, also force a link down notification
723 * since some version of the user-space daemon (mocad) use
724 * cmd->autoneg to force the link, which messes up the PHY
725 * state machine and make it go in PHY_FORCING state instead.
726 */
727 if (!status->link)
Andrew Lunnc8b09802016-06-04 21:16:57 +0200728 netif_carrier_off(ds->ports[port].netdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700729 status->duplex = 1;
730 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -0800731 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700732 status->duplex = !!(duplex & (1 << port));
733 }
734
Florian Fainelli0fe99332017-01-20 12:36:30 -0800735 reg = core_readl(priv, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800736 reg |= SW_OVERRIDE;
737 if (status->link)
738 reg |= LINK_STS;
739 else
740 reg &= ~LINK_STS;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800741 core_writel(priv, reg, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800742
Florian Fainelli246d7f72014-08-27 17:04:56 -0700743 if ((pause & (1 << port)) &&
744 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
745 status->asym_pause = 1;
746 status->pause = 1;
747 }
748
749 if (pause & (1 << port))
750 status->pause = 1;
751}
752
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700753static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
754{
Florian Fainellif4589952016-08-26 12:18:33 -0700755 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700756 unsigned int port;
757
Florian Fainelli691c9a82015-01-20 16:42:00 -0800758 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700759
760 /* Disable all ports physically present including the IMP
761 * port, the other ones have already been disabled during
762 * bcm_sf2_sw_setup
763 */
764 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200765 if ((1 << port) & ds->enabled_port_mask ||
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700766 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700767 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700768 }
769
770 return 0;
771}
772
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700773static int bcm_sf2_sw_resume(struct dsa_switch *ds)
774{
Florian Fainellif4589952016-08-26 12:18:33 -0700775 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700776 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700777 int ret;
778
779 ret = bcm_sf2_sw_rst(priv);
780 if (ret) {
781 pr_err("%s: failed to software reset switch\n", __func__);
782 return ret;
783 }
784
Florian Fainellib0836682015-02-05 11:40:41 -0800785 if (priv->hw_params.num_gphy == 1)
786 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700787
788 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200789 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700790 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700791 else if (dsa_is_cpu_port(ds, port))
792 bcm_sf2_imp_setup(ds, port);
793 }
794
795 return 0;
796}
797
Florian Fainelli96e65d72014-09-18 17:31:25 -0700798static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
799 struct ethtool_wolinfo *wol)
800{
801 struct net_device *p = ds->dst[ds->index].master_netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700802 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700803 struct ethtool_wolinfo pwol;
804
805 /* Get the parent device WoL settings */
806 p->ethtool_ops->get_wol(p, &pwol);
807
808 /* Advertise the parent device supported settings */
809 wol->supported = pwol.supported;
810 memset(&wol->sopass, 0, sizeof(wol->sopass));
811
812 if (pwol.wolopts & WAKE_MAGICSECURE)
813 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
814
815 if (priv->wol_ports_mask & (1 << port))
816 wol->wolopts = pwol.wolopts;
817 else
818 wol->wolopts = 0;
819}
820
821static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
822 struct ethtool_wolinfo *wol)
823{
824 struct net_device *p = ds->dst[ds->index].master_netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700825 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700826 s8 cpu_port = ds->dst[ds->index].cpu_port;
827 struct ethtool_wolinfo pwol;
828
829 p->ethtool_ops->get_wol(p, &pwol);
830 if (wol->wolopts & ~pwol.supported)
831 return -EINVAL;
832
833 if (wol->wolopts)
834 priv->wol_ports_mask |= (1 << port);
835 else
836 priv->wol_ports_mask &= ~(1 << port);
837
838 /* If we have at least one port enabled, make sure the CPU port
839 * is also enabled. If the CPU port is the last one enabled, we disable
840 * it since this configuration does not make sense.
841 */
842 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
843 priv->wol_ports_mask |= (1 << cpu_port);
844 else
845 priv->wol_ports_mask &= ~(1 << cpu_port);
846
847 return p->ethtool_ops->set_wol(p, wol);
848}
849
Florian Fainellide0b9d32016-08-26 12:18:34 -0700850static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
Florian Fainelli9c57a772016-06-09 17:42:08 -0700851{
Florian Fainellide0b9d32016-08-26 12:18:34 -0700852 unsigned int timeout = 10;
853 u32 reg;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700854
Florian Fainellide0b9d32016-08-26 12:18:34 -0700855 do {
856 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
857 if (!(reg & ARLA_VTBL_STDN))
858 return 0;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700859
Florian Fainellide0b9d32016-08-26 12:18:34 -0700860 usleep_range(1000, 2000);
861 } while (timeout--);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700862
Florian Fainellide0b9d32016-08-26 12:18:34 -0700863 return -ETIMEDOUT;
864}
Florian Fainelli9c57a772016-06-09 17:42:08 -0700865
Florian Fainellide0b9d32016-08-26 12:18:34 -0700866static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
867{
868 core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
869
870 return bcm_sf2_vlan_op_wait(priv);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700871}
872
873static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
874{
Florian Fainellif4589952016-08-26 12:18:33 -0700875 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700876 unsigned int port;
877
878 /* Clear all VLANs */
879 bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
880
881 for (port = 0; port < priv->hw_params.num_ports; port++) {
882 if (!((1 << port) & ds->enabled_port_mask))
883 continue;
884
885 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
886 }
887}
888
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700889static int bcm_sf2_sw_setup(struct dsa_switch *ds)
890{
Florian Fainellif4589952016-08-26 12:18:33 -0700891 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700892 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700893
894 /* Enable all valid ports and disable those unused */
895 for (port = 0; port < priv->hw_params.num_ports; port++) {
896 /* IMP port receives special treatment */
897 if ((1 << port) & ds->enabled_port_mask)
898 bcm_sf2_port_setup(ds, port, NULL);
899 else if (dsa_is_cpu_port(ds, port))
900 bcm_sf2_imp_setup(ds, port);
901 else
902 bcm_sf2_port_disable(ds, port, NULL);
903 }
904
905 bcm_sf2_sw_configure_vlan(ds);
906
907 return 0;
908}
909
Florian Fainellif4589952016-08-26 12:18:33 -0700910/* The SWITCH_CORE register space is managed by b53 but operates on a page +
911 * register basis so we need to translate that into an address that the
912 * bus-glue understands.
913 */
914#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
915
916static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
917 u8 *val)
918{
919 struct bcm_sf2_priv *priv = dev->priv;
920
921 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
922
923 return 0;
924}
925
926static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
927 u16 *val)
928{
929 struct bcm_sf2_priv *priv = dev->priv;
930
931 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
932
933 return 0;
934}
935
936static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
937 u32 *val)
938{
939 struct bcm_sf2_priv *priv = dev->priv;
940
941 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
942
943 return 0;
944}
945
946static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
947 u64 *val)
948{
949 struct bcm_sf2_priv *priv = dev->priv;
950
951 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
952
953 return 0;
954}
955
956static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
957 u8 value)
958{
959 struct bcm_sf2_priv *priv = dev->priv;
960
961 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
962
963 return 0;
964}
965
966static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
967 u16 value)
968{
969 struct bcm_sf2_priv *priv = dev->priv;
970
971 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
972
973 return 0;
974}
975
976static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
977 u32 value)
978{
979 struct bcm_sf2_priv *priv = dev->priv;
980
981 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
982
983 return 0;
984}
985
986static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
987 u64 value)
988{
989 struct bcm_sf2_priv *priv = dev->priv;
990
991 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
992
993 return 0;
994}
995
Wei Yongjun0e26e5b2016-09-15 02:24:13 +0000996static struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -0700997 .read8 = bcm_sf2_core_read8,
998 .read16 = bcm_sf2_core_read16,
999 .read32 = bcm_sf2_core_read32,
1000 .read48 = bcm_sf2_core_read64,
1001 .read64 = bcm_sf2_core_read64,
1002 .write8 = bcm_sf2_core_write8,
1003 .write16 = bcm_sf2_core_write16,
1004 .write32 = bcm_sf2_core_write32,
1005 .write48 = bcm_sf2_core_write64,
1006 .write64 = bcm_sf2_core_write64,
1007};
1008
Florian Fainellia82f67a2017-01-08 14:52:08 -08001009static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli73095cb2017-01-08 14:52:06 -08001010 .get_tag_protocol = bcm_sf2_sw_get_tag_protocol,
1011 .setup = bcm_sf2_sw_setup,
1012 .get_strings = b53_get_strings,
1013 .get_ethtool_stats = b53_get_ethtool_stats,
1014 .get_sset_count = b53_get_sset_count,
1015 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
1016 .adjust_link = bcm_sf2_sw_adjust_link,
1017 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
1018 .suspend = bcm_sf2_sw_suspend,
1019 .resume = bcm_sf2_sw_resume,
1020 .get_wol = bcm_sf2_sw_get_wol,
1021 .set_wol = bcm_sf2_sw_set_wol,
1022 .port_enable = bcm_sf2_port_setup,
1023 .port_disable = bcm_sf2_port_disable,
1024 .get_eee = bcm_sf2_sw_get_eee,
1025 .set_eee = bcm_sf2_sw_set_eee,
1026 .port_bridge_join = b53_br_join,
1027 .port_bridge_leave = b53_br_leave,
1028 .port_stp_state_set = b53_br_set_stp_state,
1029 .port_fast_age = b53_br_fast_age,
1030 .port_vlan_filtering = b53_vlan_filtering,
1031 .port_vlan_prepare = b53_vlan_prepare,
1032 .port_vlan_add = b53_vlan_add,
1033 .port_vlan_del = b53_vlan_del,
1034 .port_vlan_dump = b53_vlan_dump,
1035 .port_fdb_prepare = b53_fdb_prepare,
1036 .port_fdb_dump = b53_fdb_dump,
1037 .port_fdb_add = b53_fdb_add,
1038 .port_fdb_del = b53_fdb_del,
1039};
1040
Florian Fainellia78e86e2017-01-20 12:36:29 -08001041struct bcm_sf2_of_data {
1042 u32 type;
1043 const u16 *reg_offsets;
1044 unsigned int core_reg_align;
1045};
1046
1047/* Register offsets for the SWITCH_REG_* block */
1048static const u16 bcm_sf2_7445_reg_offsets[] = {
1049 [REG_SWITCH_CNTRL] = 0x00,
1050 [REG_SWITCH_STATUS] = 0x04,
1051 [REG_DIR_DATA_WRITE] = 0x08,
1052 [REG_DIR_DATA_READ] = 0x0C,
1053 [REG_SWITCH_REVISION] = 0x18,
1054 [REG_PHY_REVISION] = 0x1C,
1055 [REG_SPHY_CNTRL] = 0x2C,
1056 [REG_RGMII_0_CNTRL] = 0x34,
1057 [REG_RGMII_1_CNTRL] = 0x40,
1058 [REG_RGMII_2_CNTRL] = 0x4c,
1059 [REG_LED_0_CNTRL] = 0x90,
1060 [REG_LED_1_CNTRL] = 0x94,
1061 [REG_LED_2_CNTRL] = 0x98,
1062};
1063
1064static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1065 .type = BCM7445_DEVICE_ID,
1066 .core_reg_align = 0,
1067 .reg_offsets = bcm_sf2_7445_reg_offsets,
1068};
1069
Florian Fainelli0fe99332017-01-20 12:36:30 -08001070static const u16 bcm_sf2_7278_reg_offsets[] = {
1071 [REG_SWITCH_CNTRL] = 0x00,
1072 [REG_SWITCH_STATUS] = 0x04,
1073 [REG_DIR_DATA_WRITE] = 0x08,
1074 [REG_DIR_DATA_READ] = 0x0c,
1075 [REG_SWITCH_REVISION] = 0x10,
1076 [REG_PHY_REVISION] = 0x14,
1077 [REG_SPHY_CNTRL] = 0x24,
1078 [REG_RGMII_0_CNTRL] = 0xe0,
1079 [REG_RGMII_1_CNTRL] = 0xec,
1080 [REG_RGMII_2_CNTRL] = 0xf8,
1081 [REG_LED_0_CNTRL] = 0x40,
1082 [REG_LED_1_CNTRL] = 0x4c,
1083 [REG_LED_2_CNTRL] = 0x58,
1084};
1085
1086static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1087 .type = BCM7278_DEVICE_ID,
1088 .core_reg_align = 1,
1089 .reg_offsets = bcm_sf2_7278_reg_offsets,
1090};
1091
Florian Fainellia78e86e2017-01-20 12:36:29 -08001092static const struct of_device_id bcm_sf2_of_match[] = {
1093 { .compatible = "brcm,bcm7445-switch-v4.0",
1094 .data = &bcm_sf2_7445_data
1095 },
Florian Fainelli0fe99332017-01-20 12:36:30 -08001096 { .compatible = "brcm,bcm7278-switch-v4.0",
1097 .data = &bcm_sf2_7278_data
1098 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001099 { /* sentinel */ },
1100};
1101MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1102
Florian Fainellid9338022016-08-18 15:30:14 -07001103static int bcm_sf2_sw_probe(struct platform_device *pdev)
1104{
1105 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1106 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001107 const struct of_device_id *of_id = NULL;
1108 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001109 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001110 struct dsa_switch_ops *ops;
Florian Fainellid9338022016-08-18 15:30:14 -07001111 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001112 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001113 struct dsa_switch *ds;
1114 void __iomem **base;
Florian Fainelli4bd11672016-08-18 15:30:15 -07001115 struct resource *r;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001116 unsigned int i;
1117 u32 reg, rev;
1118 int ret;
1119
Florian Fainellif4589952016-08-26 12:18:33 -07001120 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1121 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001122 return -ENOMEM;
1123
Florian Fainellia4c61b92017-01-07 21:01:56 -08001124 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1125 if (!ops)
1126 return -ENOMEM;
1127
Florian Fainellif4589952016-08-26 12:18:33 -07001128 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1129 if (!dev)
1130 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001131
Florian Fainellif4589952016-08-26 12:18:33 -07001132 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1133 if (!pdata)
1134 return -ENOMEM;
1135
Florian Fainellia78e86e2017-01-20 12:36:29 -08001136 of_id = of_match_node(bcm_sf2_of_match, dn);
1137 if (!of_id || !of_id->data)
1138 return -EINVAL;
1139
1140 data = of_id->data;
1141
1142 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1143 priv->type = data->type;
1144 priv->reg_offsets = data->reg_offsets;
1145 priv->core_reg_align = data->core_reg_align;
1146
Florian Fainellif4589952016-08-26 12:18:33 -07001147 /* Auto-detection using standard registers will not work, so
1148 * provide an indication of what kind of device we are for
1149 * b53_common to work with
1150 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001151 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001152 dev->pdata = pdata;
1153
1154 priv->dev = dev;
1155 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001156 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001157
1158 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001159
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001160 spin_lock_init(&priv->indir_lock);
1161 mutex_init(&priv->stats_mutex);
1162
Florian Fainellid9338022016-08-18 15:30:14 -07001163 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001164
1165 priv->irq0 = irq_of_parse_and_map(dn, 0);
1166 priv->irq1 = irq_of_parse_and_map(dn, 1);
1167
1168 base = &priv->core;
1169 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainelli4bd11672016-08-18 15:30:15 -07001170 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1171 *base = devm_ioremap_resource(&pdev->dev, r);
1172 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001173 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001174 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001175 }
1176 base++;
1177 }
1178
1179 ret = bcm_sf2_sw_rst(priv);
1180 if (ret) {
1181 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001182 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001183 }
1184
1185 ret = bcm_sf2_mdio_register(ds);
1186 if (ret) {
1187 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001188 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001189 }
1190
1191 /* Disable all interrupts and request them */
1192 bcm_sf2_intr_disable(priv);
1193
Florian Fainelli4bd11672016-08-18 15:30:15 -07001194 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1195 "switch_0", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001196 if (ret < 0) {
1197 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001198 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001199 }
1200
Florian Fainelli4bd11672016-08-18 15:30:15 -07001201 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1202 "switch_1", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001203 if (ret < 0) {
1204 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001205 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001206 }
1207
1208 /* Reset the MIB counters */
1209 reg = core_readl(priv, CORE_GMNCFGCFG);
1210 reg |= RST_MIB_CNT;
1211 core_writel(priv, reg, CORE_GMNCFGCFG);
1212 reg &= ~RST_MIB_CNT;
1213 core_writel(priv, reg, CORE_GMNCFGCFG);
1214
1215 /* Get the maximum number of ports for this switch */
1216 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1217 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1218 priv->hw_params.num_ports = DSA_MAX_PORTS;
1219
1220 /* Assume a single GPHY setup if we can't read that property */
1221 if (of_property_read_u32(dn, "brcm,num-gphy",
1222 &priv->hw_params.num_gphy))
1223 priv->hw_params.num_gphy = 1;
1224
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001225 rev = reg_readl(priv, REG_SWITCH_REVISION);
1226 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1227 SWITCH_TOP_REV_MASK;
1228 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1229
1230 rev = reg_readl(priv, REG_PHY_REVISION);
1231 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1232
Florian Fainellif4589952016-08-26 12:18:33 -07001233 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001234 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001235 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001236
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001237 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1238 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1239 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1240 priv->core, priv->irq0, priv->irq1);
1241
1242 return 0;
1243
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001244out_mdio:
1245 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001246 return ret;
1247}
1248
Florian Fainellid9338022016-08-18 15:30:14 -07001249static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001250{
Florian Fainellif4589952016-08-26 12:18:33 -07001251 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001252
1253 /* Disable all ports and interrupts */
1254 priv->wol_ports_mask = 0;
Florian Fainellif4589952016-08-26 12:18:33 -07001255 bcm_sf2_sw_suspend(priv->dev->ds);
1256 dsa_unregister_switch(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001257 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001258
1259 return 0;
1260}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001261
Florian Fainelli2399d612016-10-20 09:32:19 -07001262static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1263{
1264 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1265
1266 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1267 * successful MDIO bus scan to occur. If we did turn off the GPHY
1268 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001269 *
1270 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001271 */
1272 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001273 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001274}
1275
Florian Fainellid9338022016-08-18 15:30:14 -07001276#ifdef CONFIG_PM_SLEEP
1277static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001278{
Florian Fainellid9338022016-08-18 15:30:14 -07001279 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001280 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001281
Florian Fainellif4589952016-08-26 12:18:33 -07001282 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001283}
Florian Fainellid9338022016-08-18 15:30:14 -07001284
1285static int bcm_sf2_resume(struct device *dev)
1286{
1287 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001288 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001289
Florian Fainellif4589952016-08-26 12:18:33 -07001290 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001291}
1292#endif /* CONFIG_PM_SLEEP */
1293
1294static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1295 bcm_sf2_suspend, bcm_sf2_resume);
1296
Florian Fainellid9338022016-08-18 15:30:14 -07001297
1298static struct platform_driver bcm_sf2_driver = {
1299 .probe = bcm_sf2_sw_probe,
1300 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001301 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001302 .driver = {
1303 .name = "brcm-sf2",
1304 .of_match_table = bcm_sf2_of_match,
1305 .pm = &bcm_sf2_pm_ops,
1306 },
1307};
1308module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001309
1310MODULE_AUTHOR("Broadcom Corporation");
1311MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1312MODULE_LICENSE("GPL");
1313MODULE_ALIAS("platform:brcm-sf2");