blob: 571e112c8e343839a197f3610f1906f83aa4efeb [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
239 /* Clear the Rx and Tx disable bits and set to no spanning tree */
240 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
241
Florian Fainelli9af197a2015-02-05 11:40:42 -0800242 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700243 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800244 bcm_sf2_gphy_enable_set(ds, true);
245 if (phy) {
246 /* if phy_stop() has been called before, phy
247 * will be in halted state, and phy_start()
248 * will call resume.
249 *
250 * the resume path does not configure back
251 * autoneg settings, and since we hard reset
252 * the phy manually here, we need to reset the
253 * state machine also.
254 */
255 phy->state = PHY_READY;
256 phy_init_hw(phy);
257 }
258 }
259
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700260 /* Enable MoCA port interrupts to get notified */
261 if (port == priv->moca_port)
262 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700263
Florian Fainelli12f460f2015-02-24 13:15:34 -0800264 /* Set this port, and only this one to be in the default VLAN,
265 * if member of a bridge, restore its membership prior to
266 * bringing down this port.
267 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700268 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
269 reg &= ~PORT_VLAN_CTRL_MASK;
270 reg |= (1 << port);
Florian Fainelli02154922016-09-10 12:39:03 -0700271 reg |= priv->dev->ports[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700272 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700273
274 bcm_sf2_imp_vlan_setup(ds, cpu_port);
275
Florian Fainelli450b05c2014-09-24 17:05:22 -0700276 /* If EEE was enabled, restore it */
277 if (priv->port_sts[port].eee.eee_enabled)
278 bcm_sf2_eee_enable_set(ds, port, true);
279
Florian Fainellib6d045d2014-09-24 17:05:20 -0700280 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700281}
282
Florian Fainellib6d045d2014-09-24 17:05:20 -0700283static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
284 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700285{
Florian Fainellif4589952016-08-26 12:18:33 -0700286 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700287 u32 off, reg;
288
Florian Fainelli96e65d72014-09-18 17:31:25 -0700289 if (priv->wol_ports_mask & (1 << port))
290 return;
291
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700292 if (port == priv->moca_port)
293 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700294
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700295 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800296 bcm_sf2_gphy_enable_set(ds, false);
297
Florian Fainelli246d7f72014-08-27 17:04:56 -0700298 if (dsa_is_cpu_port(ds, port))
299 off = CORE_IMP_CTL;
300 else
301 off = CORE_G_PCTL_PORT(port);
302
303 reg = core_readl(priv, off);
304 reg |= RX_DIS | TX_DIS;
305 core_writel(priv, reg, off);
306
307 /* Power down the port memory */
308 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
309 reg |= P_TXQ_PSM_VDD(port);
310 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
311}
312
Florian Fainelli450b05c2014-09-24 17:05:22 -0700313/* Returns 0 if EEE was not enabled, or 1 otherwise
314 */
315static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
316 struct phy_device *phy)
317{
Florian Fainellif4589952016-08-26 12:18:33 -0700318 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700319 struct ethtool_eee *p = &priv->port_sts[port].eee;
320 int ret;
321
322 p->supported = (SUPPORTED_1000baseT_Full | SUPPORTED_100baseT_Full);
323
324 ret = phy_init_eee(phy, 0);
325 if (ret)
326 return 0;
327
328 bcm_sf2_eee_enable_set(ds, port, true);
329
330 return 1;
331}
332
333static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
334 struct ethtool_eee *e)
335{
Florian Fainellif4589952016-08-26 12:18:33 -0700336 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700337 struct ethtool_eee *p = &priv->port_sts[port].eee;
338 u32 reg;
339
340 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
341 e->eee_enabled = p->eee_enabled;
342 e->eee_active = !!(reg & (1 << port));
343
344 return 0;
345}
346
347static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
348 struct phy_device *phydev,
349 struct ethtool_eee *e)
350{
Florian Fainellif4589952016-08-26 12:18:33 -0700351 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700352 struct ethtool_eee *p = &priv->port_sts[port].eee;
353
354 p->eee_enabled = e->eee_enabled;
355
356 if (!p->eee_enabled) {
357 bcm_sf2_eee_enable_set(ds, port, false);
358 } else {
359 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
360 if (!p->eee_enabled)
361 return -EOPNOTSUPP;
362 }
363
364 return 0;
365}
366
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700367static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
368 int regnum, u16 val)
369{
370 int ret = 0;
371 u32 reg;
372
373 reg = reg_readl(priv, REG_SWITCH_CNTRL);
374 reg |= MDIO_MASTER_SEL;
375 reg_writel(priv, reg, REG_SWITCH_CNTRL);
376
377 /* Page << 8 | offset */
378 reg = 0x70;
379 reg <<= 2;
380 core_writel(priv, addr, reg);
381
382 /* Page << 8 | offset */
383 reg = 0x80 << 8 | regnum << 1;
384 reg <<= 2;
385
386 if (op)
387 ret = core_readl(priv, reg);
388 else
389 core_writel(priv, val, reg);
390
391 reg = reg_readl(priv, REG_SWITCH_CNTRL);
392 reg &= ~MDIO_MASTER_SEL;
393 reg_writel(priv, reg, REG_SWITCH_CNTRL);
394
395 return ret & 0xffff;
396}
397
398static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
399{
400 struct bcm_sf2_priv *priv = bus->priv;
401
402 /* Intercept reads from Broadcom pseudo-PHY address, else, send
403 * them to our master MDIO bus controller
404 */
405 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
406 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
407 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800408 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700409}
410
411static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
412 u16 val)
413{
414 struct bcm_sf2_priv *priv = bus->priv;
415
416 /* Intercept writes to the Broadcom pseudo-PHY address, else,
417 * send them to our master MDIO bus controller
418 */
419 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
420 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
421 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800422 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700423
424 return 0;
425}
426
Florian Fainelli246d7f72014-08-27 17:04:56 -0700427static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
428{
429 struct bcm_sf2_priv *priv = dev_id;
430
431 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
432 ~priv->irq0_mask;
433 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
434
435 return IRQ_HANDLED;
436}
437
438static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
439{
440 struct bcm_sf2_priv *priv = dev_id;
441
442 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
443 ~priv->irq1_mask;
444 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
445
446 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
447 priv->port_sts[7].link = 1;
448 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
449 priv->port_sts[7].link = 0;
450
451 return IRQ_HANDLED;
452}
453
Florian Fainelli33f84612014-11-25 18:08:49 -0800454static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
455{
456 unsigned int timeout = 1000;
457 u32 reg;
458
459 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
460 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
461 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
462
463 do {
464 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
465 if (!(reg & SOFTWARE_RESET))
466 break;
467
468 usleep_range(1000, 2000);
469 } while (timeout-- > 0);
470
471 if (timeout == 0)
472 return -ETIMEDOUT;
473
474 return 0;
475}
476
Florian Fainelli691c9a82015-01-20 16:42:00 -0800477static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
478{
Florian Fainellif01d5982016-08-25 15:23:41 -0700479 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800480 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700481 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800482 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800483}
484
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700485static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
486 struct device_node *dn)
487{
488 struct device_node *port;
489 const char *phy_mode_str;
490 int mode;
491 unsigned int port_num;
492 int ret;
493
494 priv->moca_port = -1;
495
496 for_each_available_child_of_node(dn, port) {
497 if (of_property_read_u32(port, "reg", &port_num))
498 continue;
499
500 /* Internal PHYs get assigned a specific 'phy-mode' property
501 * value: "internal" to help flag them before MDIO probing
502 * has completed, since they might be turned off at that
503 * time
504 */
505 mode = of_get_phy_mode(port);
506 if (mode < 0) {
507 ret = of_property_read_string(port, "phy-mode",
508 &phy_mode_str);
509 if (ret < 0)
510 continue;
511
512 if (!strcasecmp(phy_mode_str, "internal"))
513 priv->int_phy_mask |= 1 << port_num;
514 }
515
516 if (mode == PHY_INTERFACE_MODE_MOCA)
517 priv->moca_port = port_num;
518 }
519}
520
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700521static int bcm_sf2_mdio_register(struct dsa_switch *ds)
522{
Florian Fainellif4589952016-08-26 12:18:33 -0700523 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700524 struct device_node *dn;
525 static int index;
526 int err;
527
528 /* Find our integrated MDIO bus node */
529 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
530 priv->master_mii_bus = of_mdio_find_bus(dn);
531 if (!priv->master_mii_bus)
532 return -EPROBE_DEFER;
533
534 get_device(&priv->master_mii_bus->dev);
535 priv->master_mii_dn = dn;
536
537 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
538 if (!priv->slave_mii_bus)
539 return -ENOMEM;
540
541 priv->slave_mii_bus->priv = priv;
542 priv->slave_mii_bus->name = "sf2 slave mii";
543 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
544 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
545 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
546 index++);
547 priv->slave_mii_bus->dev.of_node = dn;
548
549 /* Include the pseudo-PHY address to divert reads towards our
550 * workaround. This is only required for 7445D0, since 7445E0
551 * disconnects the internal switch pseudo-PHY such that we can use the
552 * regular SWITCH_MDIO master controller instead.
553 *
554 * Here we flag the pseudo PHY as needing special treatment and would
555 * otherwise make all other PHY read/writes go to the master MDIO bus
556 * controller that comes with this switch backed by the "mdio-unimac"
557 * driver.
558 */
559 if (of_machine_is_compatible("brcm,bcm7445d0"))
560 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
561 else
562 priv->indir_phy_mask = 0;
563
564 ds->phys_mii_mask = priv->indir_phy_mask;
565 ds->slave_mii_bus = priv->slave_mii_bus;
566 priv->slave_mii_bus->parent = ds->dev->parent;
567 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
568
569 if (dn)
570 err = of_mdiobus_register(priv->slave_mii_bus, dn);
571 else
572 err = mdiobus_register(priv->slave_mii_bus);
573
574 if (err)
575 of_node_put(dn);
576
577 return err;
578}
579
580static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
581{
582 mdiobus_unregister(priv->slave_mii_bus);
583 if (priv->master_mii_dn)
584 of_node_put(priv->master_mii_dn);
585}
586
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700587static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
588{
Florian Fainellif4589952016-08-26 12:18:33 -0700589 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700590
591 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
592 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
593 * the REG_PHY_REVISION register layout is.
594 */
595
596 return priv->hw_params.gphy_rev;
597}
598
Florian Fainelli246d7f72014-08-27 17:04:56 -0700599static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
600 struct phy_device *phydev)
601{
Florian Fainellif4589952016-08-26 12:18:33 -0700602 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli76da8702016-11-22 11:40:58 -0800603 struct ethtool_eee *p = &priv->port_sts[port].eee;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700604 u32 id_mode_dis = 0, port_mode;
605 const char *str = NULL;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800606 u32 reg, offset;
607
608 if (priv->type == BCM7445_DEVICE_ID)
609 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
610 else
611 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700612
613 switch (phydev->interface) {
614 case PHY_INTERFACE_MODE_RGMII:
615 str = "RGMII (no delay)";
616 id_mode_dis = 1;
617 case PHY_INTERFACE_MODE_RGMII_TXID:
618 if (!str)
619 str = "RGMII (TX delay)";
620 port_mode = EXT_GPHY;
621 break;
622 case PHY_INTERFACE_MODE_MII:
623 str = "MII";
624 port_mode = EXT_EPHY;
625 break;
626 case PHY_INTERFACE_MODE_REVMII:
627 str = "Reverse MII";
628 port_mode = EXT_REVMII;
629 break;
630 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700631 /* All other PHYs: internal and MoCA */
632 goto force_link;
633 }
634
635 /* If the link is down, just disable the interface to conserve power */
636 if (!phydev->link) {
637 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
638 reg &= ~RGMII_MODE_EN;
639 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700640 goto force_link;
641 }
642
643 /* Clear id_mode_dis bit, and the existing port mode, but
644 * make sure we enable the RGMII block for data to pass
645 */
646 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
647 reg &= ~ID_MODE_DIS;
648 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
649 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
650
651 reg |= port_mode | RGMII_MODE_EN;
652 if (id_mode_dis)
653 reg |= ID_MODE_DIS;
654
655 if (phydev->pause) {
656 if (phydev->asym_pause)
657 reg |= TX_PAUSE_EN;
658 reg |= RX_PAUSE_EN;
659 }
660
661 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
662
663 pr_info("Port %d configured for %s\n", port, str);
664
665force_link:
666 /* Force link settings detected from the PHY */
667 reg = SW_OVERRIDE;
668 switch (phydev->speed) {
669 case SPEED_1000:
670 reg |= SPDSTS_1000 << SPEED_SHIFT;
671 break;
672 case SPEED_100:
673 reg |= SPDSTS_100 << SPEED_SHIFT;
674 break;
675 }
676
677 if (phydev->link)
678 reg |= LINK_STS;
679 if (phydev->duplex == DUPLEX_FULL)
680 reg |= DUPLX_MODE;
681
Florian Fainelli0fe99332017-01-20 12:36:30 -0800682 core_writel(priv, reg, offset);
Florian Fainelli76da8702016-11-22 11:40:58 -0800683
684 if (!phydev->is_pseudo_fixed_link)
685 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700686}
687
688static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
689 struct fixed_phy_status *status)
690{
Florian Fainellif4589952016-08-26 12:18:33 -0700691 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800692 u32 duplex, pause, offset;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700693 u32 reg;
694
Florian Fainelli0fe99332017-01-20 12:36:30 -0800695 if (priv->type == BCM7445_DEVICE_ID)
696 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
697 else
698 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
699
Florian Fainelli246d7f72014-08-27 17:04:56 -0700700 duplex = core_readl(priv, CORE_DUPSTS);
701 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700702
703 status->link = 0;
704
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700705 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700706 * which means that we need to force the link at the port override
707 * level to get the data to flow. We do use what the interrupt handler
708 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -0800709 *
710 * For the other ports, we just force the link status, since this is
711 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700712 */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700713 if (port == priv->moca_port) {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700714 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -0700715 /* For MoCA interfaces, also force a link down notification
716 * since some version of the user-space daemon (mocad) use
717 * cmd->autoneg to force the link, which messes up the PHY
718 * state machine and make it go in PHY_FORCING state instead.
719 */
720 if (!status->link)
Andrew Lunnc8b09802016-06-04 21:16:57 +0200721 netif_carrier_off(ds->ports[port].netdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700722 status->duplex = 1;
723 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -0800724 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700725 status->duplex = !!(duplex & (1 << port));
726 }
727
Florian Fainelli0fe99332017-01-20 12:36:30 -0800728 reg = core_readl(priv, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800729 reg |= SW_OVERRIDE;
730 if (status->link)
731 reg |= LINK_STS;
732 else
733 reg &= ~LINK_STS;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800734 core_writel(priv, reg, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800735
Florian Fainelli246d7f72014-08-27 17:04:56 -0700736 if ((pause & (1 << port)) &&
737 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
738 status->asym_pause = 1;
739 status->pause = 1;
740 }
741
742 if (pause & (1 << port))
743 status->pause = 1;
744}
745
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700746static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
747{
Florian Fainellif4589952016-08-26 12:18:33 -0700748 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700749 unsigned int port;
750
Florian Fainelli691c9a82015-01-20 16:42:00 -0800751 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700752
753 /* Disable all ports physically present including the IMP
754 * port, the other ones have already been disabled during
755 * bcm_sf2_sw_setup
756 */
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 Fainelli8cfa9492014-09-18 17:31:23 -0700759 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700760 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700761 }
762
763 return 0;
764}
765
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700766static int bcm_sf2_sw_resume(struct dsa_switch *ds)
767{
Florian Fainellif4589952016-08-26 12:18:33 -0700768 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700769 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700770 int ret;
771
772 ret = bcm_sf2_sw_rst(priv);
773 if (ret) {
774 pr_err("%s: failed to software reset switch\n", __func__);
775 return ret;
776 }
777
Florian Fainellib0836682015-02-05 11:40:41 -0800778 if (priv->hw_params.num_gphy == 1)
779 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700780
781 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200782 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700783 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700784 else if (dsa_is_cpu_port(ds, port))
785 bcm_sf2_imp_setup(ds, port);
786 }
787
788 return 0;
789}
790
Florian Fainelli96e65d72014-09-18 17:31:25 -0700791static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
792 struct ethtool_wolinfo *wol)
793{
794 struct net_device *p = ds->dst[ds->index].master_netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700795 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700796 struct ethtool_wolinfo pwol;
797
798 /* Get the parent device WoL settings */
799 p->ethtool_ops->get_wol(p, &pwol);
800
801 /* Advertise the parent device supported settings */
802 wol->supported = pwol.supported;
803 memset(&wol->sopass, 0, sizeof(wol->sopass));
804
805 if (pwol.wolopts & WAKE_MAGICSECURE)
806 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
807
808 if (priv->wol_ports_mask & (1 << port))
809 wol->wolopts = pwol.wolopts;
810 else
811 wol->wolopts = 0;
812}
813
814static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
815 struct ethtool_wolinfo *wol)
816{
817 struct net_device *p = ds->dst[ds->index].master_netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700818 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700819 s8 cpu_port = ds->dst[ds->index].cpu_port;
820 struct ethtool_wolinfo pwol;
821
822 p->ethtool_ops->get_wol(p, &pwol);
823 if (wol->wolopts & ~pwol.supported)
824 return -EINVAL;
825
826 if (wol->wolopts)
827 priv->wol_ports_mask |= (1 << port);
828 else
829 priv->wol_ports_mask &= ~(1 << port);
830
831 /* If we have at least one port enabled, make sure the CPU port
832 * is also enabled. If the CPU port is the last one enabled, we disable
833 * it since this configuration does not make sense.
834 */
835 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
836 priv->wol_ports_mask |= (1 << cpu_port);
837 else
838 priv->wol_ports_mask &= ~(1 << cpu_port);
839
840 return p->ethtool_ops->set_wol(p, wol);
841}
842
Florian Fainellide0b9d32016-08-26 12:18:34 -0700843static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
Florian Fainelli9c57a772016-06-09 17:42:08 -0700844{
Florian Fainellide0b9d32016-08-26 12:18:34 -0700845 unsigned int timeout = 10;
846 u32 reg;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700847
Florian Fainellide0b9d32016-08-26 12:18:34 -0700848 do {
849 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
850 if (!(reg & ARLA_VTBL_STDN))
851 return 0;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700852
Florian Fainellide0b9d32016-08-26 12:18:34 -0700853 usleep_range(1000, 2000);
854 } while (timeout--);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700855
Florian Fainellide0b9d32016-08-26 12:18:34 -0700856 return -ETIMEDOUT;
857}
Florian Fainelli9c57a772016-06-09 17:42:08 -0700858
Florian Fainellide0b9d32016-08-26 12:18:34 -0700859static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
860{
861 core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
862
863 return bcm_sf2_vlan_op_wait(priv);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700864}
865
866static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
867{
Florian Fainellif4589952016-08-26 12:18:33 -0700868 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700869 unsigned int port;
870
871 /* Clear all VLANs */
872 bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
873
874 for (port = 0; port < priv->hw_params.num_ports; port++) {
875 if (!((1 << port) & ds->enabled_port_mask))
876 continue;
877
878 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
879 }
880}
881
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700882static int bcm_sf2_sw_setup(struct dsa_switch *ds)
883{
Florian Fainellif4589952016-08-26 12:18:33 -0700884 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700885 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700886
887 /* Enable all valid ports and disable those unused */
888 for (port = 0; port < priv->hw_params.num_ports; port++) {
889 /* IMP port receives special treatment */
890 if ((1 << port) & ds->enabled_port_mask)
891 bcm_sf2_port_setup(ds, port, NULL);
892 else if (dsa_is_cpu_port(ds, port))
893 bcm_sf2_imp_setup(ds, port);
894 else
895 bcm_sf2_port_disable(ds, port, NULL);
896 }
897
898 bcm_sf2_sw_configure_vlan(ds);
899
900 return 0;
901}
902
Florian Fainellif4589952016-08-26 12:18:33 -0700903/* The SWITCH_CORE register space is managed by b53 but operates on a page +
904 * register basis so we need to translate that into an address that the
905 * bus-glue understands.
906 */
907#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
908
909static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
910 u8 *val)
911{
912 struct bcm_sf2_priv *priv = dev->priv;
913
914 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
915
916 return 0;
917}
918
919static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
920 u16 *val)
921{
922 struct bcm_sf2_priv *priv = dev->priv;
923
924 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
925
926 return 0;
927}
928
929static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
930 u32 *val)
931{
932 struct bcm_sf2_priv *priv = dev->priv;
933
934 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
935
936 return 0;
937}
938
939static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
940 u64 *val)
941{
942 struct bcm_sf2_priv *priv = dev->priv;
943
944 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
945
946 return 0;
947}
948
949static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
950 u8 value)
951{
952 struct bcm_sf2_priv *priv = dev->priv;
953
954 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
955
956 return 0;
957}
958
959static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
960 u16 value)
961{
962 struct bcm_sf2_priv *priv = dev->priv;
963
964 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
965
966 return 0;
967}
968
969static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
970 u32 value)
971{
972 struct bcm_sf2_priv *priv = dev->priv;
973
974 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
975
976 return 0;
977}
978
979static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
980 u64 value)
981{
982 struct bcm_sf2_priv *priv = dev->priv;
983
984 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
985
986 return 0;
987}
988
Wei Yongjun0e26e5b2016-09-15 02:24:13 +0000989static struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -0700990 .read8 = bcm_sf2_core_read8,
991 .read16 = bcm_sf2_core_read16,
992 .read32 = bcm_sf2_core_read32,
993 .read48 = bcm_sf2_core_read64,
994 .read64 = bcm_sf2_core_read64,
995 .write8 = bcm_sf2_core_write8,
996 .write16 = bcm_sf2_core_write16,
997 .write32 = bcm_sf2_core_write32,
998 .write48 = bcm_sf2_core_write64,
999 .write64 = bcm_sf2_core_write64,
1000};
1001
Florian Fainellia82f67a2017-01-08 14:52:08 -08001002static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli73095cb2017-01-08 14:52:06 -08001003 .get_tag_protocol = bcm_sf2_sw_get_tag_protocol,
1004 .setup = bcm_sf2_sw_setup,
1005 .get_strings = b53_get_strings,
1006 .get_ethtool_stats = b53_get_ethtool_stats,
1007 .get_sset_count = b53_get_sset_count,
1008 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
1009 .adjust_link = bcm_sf2_sw_adjust_link,
1010 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
1011 .suspend = bcm_sf2_sw_suspend,
1012 .resume = bcm_sf2_sw_resume,
1013 .get_wol = bcm_sf2_sw_get_wol,
1014 .set_wol = bcm_sf2_sw_set_wol,
1015 .port_enable = bcm_sf2_port_setup,
1016 .port_disable = bcm_sf2_port_disable,
1017 .get_eee = bcm_sf2_sw_get_eee,
1018 .set_eee = bcm_sf2_sw_set_eee,
1019 .port_bridge_join = b53_br_join,
1020 .port_bridge_leave = b53_br_leave,
1021 .port_stp_state_set = b53_br_set_stp_state,
1022 .port_fast_age = b53_br_fast_age,
1023 .port_vlan_filtering = b53_vlan_filtering,
1024 .port_vlan_prepare = b53_vlan_prepare,
1025 .port_vlan_add = b53_vlan_add,
1026 .port_vlan_del = b53_vlan_del,
1027 .port_vlan_dump = b53_vlan_dump,
1028 .port_fdb_prepare = b53_fdb_prepare,
1029 .port_fdb_dump = b53_fdb_dump,
1030 .port_fdb_add = b53_fdb_add,
1031 .port_fdb_del = b53_fdb_del,
1032};
1033
Florian Fainellia78e86e2017-01-20 12:36:29 -08001034struct bcm_sf2_of_data {
1035 u32 type;
1036 const u16 *reg_offsets;
1037 unsigned int core_reg_align;
1038};
1039
1040/* Register offsets for the SWITCH_REG_* block */
1041static const u16 bcm_sf2_7445_reg_offsets[] = {
1042 [REG_SWITCH_CNTRL] = 0x00,
1043 [REG_SWITCH_STATUS] = 0x04,
1044 [REG_DIR_DATA_WRITE] = 0x08,
1045 [REG_DIR_DATA_READ] = 0x0C,
1046 [REG_SWITCH_REVISION] = 0x18,
1047 [REG_PHY_REVISION] = 0x1C,
1048 [REG_SPHY_CNTRL] = 0x2C,
1049 [REG_RGMII_0_CNTRL] = 0x34,
1050 [REG_RGMII_1_CNTRL] = 0x40,
1051 [REG_RGMII_2_CNTRL] = 0x4c,
1052 [REG_LED_0_CNTRL] = 0x90,
1053 [REG_LED_1_CNTRL] = 0x94,
1054 [REG_LED_2_CNTRL] = 0x98,
1055};
1056
1057static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1058 .type = BCM7445_DEVICE_ID,
1059 .core_reg_align = 0,
1060 .reg_offsets = bcm_sf2_7445_reg_offsets,
1061};
1062
Florian Fainelli0fe99332017-01-20 12:36:30 -08001063static const u16 bcm_sf2_7278_reg_offsets[] = {
1064 [REG_SWITCH_CNTRL] = 0x00,
1065 [REG_SWITCH_STATUS] = 0x04,
1066 [REG_DIR_DATA_WRITE] = 0x08,
1067 [REG_DIR_DATA_READ] = 0x0c,
1068 [REG_SWITCH_REVISION] = 0x10,
1069 [REG_PHY_REVISION] = 0x14,
1070 [REG_SPHY_CNTRL] = 0x24,
1071 [REG_RGMII_0_CNTRL] = 0xe0,
1072 [REG_RGMII_1_CNTRL] = 0xec,
1073 [REG_RGMII_2_CNTRL] = 0xf8,
1074 [REG_LED_0_CNTRL] = 0x40,
1075 [REG_LED_1_CNTRL] = 0x4c,
1076 [REG_LED_2_CNTRL] = 0x58,
1077};
1078
1079static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1080 .type = BCM7278_DEVICE_ID,
1081 .core_reg_align = 1,
1082 .reg_offsets = bcm_sf2_7278_reg_offsets,
1083};
1084
Florian Fainellia78e86e2017-01-20 12:36:29 -08001085static const struct of_device_id bcm_sf2_of_match[] = {
1086 { .compatible = "brcm,bcm7445-switch-v4.0",
1087 .data = &bcm_sf2_7445_data
1088 },
Florian Fainelli0fe99332017-01-20 12:36:30 -08001089 { .compatible = "brcm,bcm7278-switch-v4.0",
1090 .data = &bcm_sf2_7278_data
1091 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001092 { /* sentinel */ },
1093};
1094MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1095
Florian Fainellid9338022016-08-18 15:30:14 -07001096static int bcm_sf2_sw_probe(struct platform_device *pdev)
1097{
1098 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1099 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001100 const struct of_device_id *of_id = NULL;
1101 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001102 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001103 struct dsa_switch_ops *ops;
Florian Fainellid9338022016-08-18 15:30:14 -07001104 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001105 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001106 struct dsa_switch *ds;
1107 void __iomem **base;
Florian Fainelli4bd11672016-08-18 15:30:15 -07001108 struct resource *r;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001109 unsigned int i;
1110 u32 reg, rev;
1111 int ret;
1112
Florian Fainellif4589952016-08-26 12:18:33 -07001113 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1114 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001115 return -ENOMEM;
1116
Florian Fainellia4c61b92017-01-07 21:01:56 -08001117 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1118 if (!ops)
1119 return -ENOMEM;
1120
Florian Fainellif4589952016-08-26 12:18:33 -07001121 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1122 if (!dev)
1123 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001124
Florian Fainellif4589952016-08-26 12:18:33 -07001125 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1126 if (!pdata)
1127 return -ENOMEM;
1128
Florian Fainellia78e86e2017-01-20 12:36:29 -08001129 of_id = of_match_node(bcm_sf2_of_match, dn);
1130 if (!of_id || !of_id->data)
1131 return -EINVAL;
1132
1133 data = of_id->data;
1134
1135 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1136 priv->type = data->type;
1137 priv->reg_offsets = data->reg_offsets;
1138 priv->core_reg_align = data->core_reg_align;
1139
Florian Fainellif4589952016-08-26 12:18:33 -07001140 /* Auto-detection using standard registers will not work, so
1141 * provide an indication of what kind of device we are for
1142 * b53_common to work with
1143 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001144 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001145 dev->pdata = pdata;
1146
1147 priv->dev = dev;
1148 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001149 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001150
1151 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001152
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001153 spin_lock_init(&priv->indir_lock);
1154 mutex_init(&priv->stats_mutex);
1155
Florian Fainellid9338022016-08-18 15:30:14 -07001156 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001157
1158 priv->irq0 = irq_of_parse_and_map(dn, 0);
1159 priv->irq1 = irq_of_parse_and_map(dn, 1);
1160
1161 base = &priv->core;
1162 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainelli4bd11672016-08-18 15:30:15 -07001163 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1164 *base = devm_ioremap_resource(&pdev->dev, r);
1165 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001166 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001167 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001168 }
1169 base++;
1170 }
1171
1172 ret = bcm_sf2_sw_rst(priv);
1173 if (ret) {
1174 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001175 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001176 }
1177
1178 ret = bcm_sf2_mdio_register(ds);
1179 if (ret) {
1180 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001181 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001182 }
1183
1184 /* Disable all interrupts and request them */
1185 bcm_sf2_intr_disable(priv);
1186
Florian Fainelli4bd11672016-08-18 15:30:15 -07001187 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1188 "switch_0", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001189 if (ret < 0) {
1190 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001191 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001192 }
1193
Florian Fainelli4bd11672016-08-18 15:30:15 -07001194 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1195 "switch_1", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001196 if (ret < 0) {
1197 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001198 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001199 }
1200
1201 /* Reset the MIB counters */
1202 reg = core_readl(priv, CORE_GMNCFGCFG);
1203 reg |= RST_MIB_CNT;
1204 core_writel(priv, reg, CORE_GMNCFGCFG);
1205 reg &= ~RST_MIB_CNT;
1206 core_writel(priv, reg, CORE_GMNCFGCFG);
1207
1208 /* Get the maximum number of ports for this switch */
1209 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1210 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1211 priv->hw_params.num_ports = DSA_MAX_PORTS;
1212
1213 /* Assume a single GPHY setup if we can't read that property */
1214 if (of_property_read_u32(dn, "brcm,num-gphy",
1215 &priv->hw_params.num_gphy))
1216 priv->hw_params.num_gphy = 1;
1217
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001218 rev = reg_readl(priv, REG_SWITCH_REVISION);
1219 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1220 SWITCH_TOP_REV_MASK;
1221 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1222
1223 rev = reg_readl(priv, REG_PHY_REVISION);
1224 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1225
Florian Fainellif4589952016-08-26 12:18:33 -07001226 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001227 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001228 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001229
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001230 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1231 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1232 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1233 priv->core, priv->irq0, priv->irq1);
1234
1235 return 0;
1236
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001237out_mdio:
1238 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001239 return ret;
1240}
1241
Florian Fainellid9338022016-08-18 15:30:14 -07001242static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001243{
Florian Fainellif4589952016-08-26 12:18:33 -07001244 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001245
1246 /* Disable all ports and interrupts */
1247 priv->wol_ports_mask = 0;
Florian Fainellif4589952016-08-26 12:18:33 -07001248 bcm_sf2_sw_suspend(priv->dev->ds);
1249 dsa_unregister_switch(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001250 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001251
1252 return 0;
1253}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001254
Florian Fainelli2399d612016-10-20 09:32:19 -07001255static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1256{
1257 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1258
1259 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1260 * successful MDIO bus scan to occur. If we did turn off the GPHY
1261 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001262 *
1263 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001264 */
1265 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001266 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001267}
1268
Florian Fainellid9338022016-08-18 15:30:14 -07001269#ifdef CONFIG_PM_SLEEP
1270static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001271{
Florian Fainellid9338022016-08-18 15:30:14 -07001272 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001273 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001274
Florian Fainellif4589952016-08-26 12:18:33 -07001275 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001276}
Florian Fainellid9338022016-08-18 15:30:14 -07001277
1278static int bcm_sf2_resume(struct device *dev)
1279{
1280 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001281 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001282
Florian Fainellif4589952016-08-26 12:18:33 -07001283 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001284}
1285#endif /* CONFIG_PM_SLEEP */
1286
1287static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1288 bcm_sf2_suspend, bcm_sf2_resume);
1289
Florian Fainellid9338022016-08-18 15:30:14 -07001290
1291static struct platform_driver bcm_sf2_driver = {
1292 .probe = bcm_sf2_sw_probe,
1293 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001294 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001295 .driver = {
1296 .name = "brcm-sf2",
1297 .of_match_table = bcm_sf2_of_match,
1298 .pm = &bcm_sf2_pm_ops,
1299 },
1300};
1301module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001302
1303MODULE_AUTHOR("Broadcom Corporation");
1304MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1305MODULE_LICENSE("GPL");
1306MODULE_ALIAS("platform:brcm-sf2");