blob: 49cb51223f7004ba2c92b49cd2813507ae608ee3 [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>
Florian Fainellif4589952016-08-26 12:18:33 -070031#include <linux/platform_data/b53.h>
Florian Fainelli246d7f72014-08-27 17:04:56 -070032
33#include "bcm_sf2.h"
34#include "bcm_sf2_regs.h"
Florian Fainellif4589952016-08-26 12:18:33 -070035#include "b53/b53_priv.h"
36#include "b53/b53_regs.h"
Florian Fainelli246d7f72014-08-27 17:04:56 -070037
Andrew Lunn7b314362016-08-22 16:01:01 +020038static enum dsa_tag_protocol bcm_sf2_sw_get_tag_protocol(struct dsa_switch *ds)
39{
40 return DSA_TAG_PROTO_BRCM;
41}
42
Florian Fainellib6d045d2014-09-24 17:05:20 -070043static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
Florian Fainelli246d7f72014-08-27 17:04:56 -070044{
Florian Fainellif4589952016-08-26 12:18:33 -070045 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -070046 unsigned int i;
Florian Fainellib6d045d2014-09-24 17:05:20 -070047 u32 reg;
48
49 /* Enable the IMP Port to be in the same VLAN as the other ports
50 * on a per-port basis such that we only have Port i and IMP in
51 * the same VLAN.
52 */
53 for (i = 0; i < priv->hw_params.num_ports; i++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +020054 if (!((1 << i) & ds->enabled_port_mask))
Florian Fainellib6d045d2014-09-24 17:05:20 -070055 continue;
56
57 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(i));
58 reg |= (1 << cpu_port);
59 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(i));
60 }
61}
62
Florian Fainelliebb2ac42017-01-20 12:36:31 -080063
64static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
65{
66 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellic837fc82017-09-03 20:27:03 -070067 unsigned int i;
Florian Fainelliebb2ac42017-01-20 12:36:31 -080068 u32 reg, offset;
69
70 if (priv->type == BCM7445_DEVICE_ID)
71 offset = CORE_STS_OVERRIDE_IMP;
72 else
73 offset = CORE_STS_OVERRIDE_IMP2;
74
75 /* Enable the port memories */
76 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
77 reg &= ~P_TXQ_PSM_VDD(port);
78 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
79
80 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
81 reg = core_readl(priv, CORE_IMP_CTL);
82 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
83 reg &= ~(RX_DIS | TX_DIS);
84 core_writel(priv, reg, CORE_IMP_CTL);
85
86 /* Enable forwarding */
87 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
88
89 /* Enable IMP port in dumb mode */
90 reg = core_readl(priv, CORE_SWITCH_CTRL);
91 reg |= MII_DUMB_FWDG_EN;
92 core_writel(priv, reg, CORE_SWITCH_CTRL);
93
Florian Fainellic837fc82017-09-03 20:27:03 -070094 /* Configure Traffic Class to QoS mapping, allow each priority to map
95 * to a different queue number
96 */
97 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
98 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
99 reg |= i << (PRT_TO_QID_SHIFT * i);
100 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
101
Florian Fainellib409a9e2017-09-19 10:46:48 -0700102 b53_brcm_hdr_setup(ds, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700103
104 /* Force link status for IMP port */
Florian Fainelli0fe99332017-01-20 12:36:30 -0800105 reg = core_readl(priv, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700106 reg |= (MII_SW_OR | LINK_STS);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800107 core_writel(priv, reg, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700108}
109
Florian Fainelli450b05c2014-09-24 17:05:22 -0700110static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
111{
Florian Fainellif4589952016-08-26 12:18:33 -0700112 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700113 u32 reg;
114
115 reg = core_readl(priv, CORE_EEE_EN_CTRL);
116 if (enable)
117 reg |= 1 << port;
118 else
119 reg &= ~(1 << port);
120 core_writel(priv, reg, CORE_EEE_EN_CTRL);
121}
122
Florian Fainellib0836682015-02-05 11:40:41 -0800123static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
124{
Florian Fainellif4589952016-08-26 12:18:33 -0700125 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -0800126 u32 reg;
127
Florian Fainelli9af197a2015-02-05 11:40:42 -0800128 reg = reg_readl(priv, REG_SPHY_CNTRL);
129 if (enable) {
130 reg |= PHY_RESET;
131 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
132 reg_writel(priv, reg, REG_SPHY_CNTRL);
133 udelay(21);
134 reg = reg_readl(priv, REG_SPHY_CNTRL);
135 reg &= ~PHY_RESET;
136 } else {
137 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
138 reg_writel(priv, reg, REG_SPHY_CNTRL);
139 mdelay(1);
140 reg |= CK25_DIS;
141 }
142 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800143
Florian Fainelli9af197a2015-02-05 11:40:42 -0800144 /* Use PHY-driven LED signaling */
145 if (!enable) {
146 reg = reg_readl(priv, REG_LED_CNTRL(0));
147 reg |= SPDLNK_SRC_SEL;
148 reg_writel(priv, reg, REG_LED_CNTRL(0));
149 }
Florian Fainellib0836682015-02-05 11:40:41 -0800150}
151
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700152static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
153 int port)
154{
155 unsigned int off;
156
157 switch (port) {
158 case 7:
159 off = P7_IRQ_OFF;
160 break;
161 case 0:
162 /* Port 0 interrupts are located on the first bank */
163 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
164 return;
165 default:
166 off = P_IRQ_OFF(port);
167 break;
168 }
169
170 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
171}
172
173static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
174 int port)
175{
176 unsigned int off;
177
178 switch (port) {
179 case 7:
180 off = P7_IRQ_OFF;
181 break;
182 case 0:
183 /* Port 0 interrupts are located on the first bank */
184 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
185 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
186 return;
187 default:
188 off = P_IRQ_OFF(port);
189 break;
190 }
191
192 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
193 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
194}
195
Florian Fainellib6d045d2014-09-24 17:05:20 -0700196static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
197 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700198{
Florian Fainellif4589952016-08-26 12:18:33 -0700199 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400200 s8 cpu_port = ds->dst->cpu_dp->index;
Florian Fainellie1b91472017-01-30 09:48:41 -0800201 unsigned int i;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700202 u32 reg;
203
204 /* Clear the memory power down */
205 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
206 reg &= ~P_TXQ_PSM_VDD(port);
207 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
208
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800209 /* Enable Broadcom tags for that port if requested */
210 if (priv->brcm_tag_mask & BIT(port))
Florian Fainellib409a9e2017-09-19 10:46:48 -0700211 b53_brcm_hdr_setup(ds, port);
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800212
Florian Fainellie1b91472017-01-30 09:48:41 -0800213 /* Configure Traffic Class to QoS mapping, allow each priority to map
214 * to a different queue number
215 */
216 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
Florian Fainelli181183772017-09-03 20:27:02 -0700217 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
Florian Fainellie1b91472017-01-30 09:48:41 -0800218 reg |= i << (PRT_TO_QID_SHIFT * i);
219 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
220
Florian Fainelli246d7f72014-08-27 17:04:56 -0700221 /* Clear the Rx and Tx disable bits and set to no spanning tree */
222 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
223
Florian Fainelli9af197a2015-02-05 11:40:42 -0800224 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700225 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800226 bcm_sf2_gphy_enable_set(ds, true);
227 if (phy) {
228 /* if phy_stop() has been called before, phy
229 * will be in halted state, and phy_start()
230 * will call resume.
231 *
232 * the resume path does not configure back
233 * autoneg settings, and since we hard reset
234 * the phy manually here, we need to reset the
235 * state machine also.
236 */
237 phy->state = PHY_READY;
238 phy_init_hw(phy);
239 }
240 }
241
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700242 /* Enable MoCA port interrupts to get notified */
243 if (port == priv->moca_port)
244 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700245
Florian Fainelli12f460f2015-02-24 13:15:34 -0800246 /* Set this port, and only this one to be in the default VLAN,
247 * if member of a bridge, restore its membership prior to
248 * bringing down this port.
249 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700250 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
251 reg &= ~PORT_VLAN_CTRL_MASK;
252 reg |= (1 << port);
Florian Fainelli02154922016-09-10 12:39:03 -0700253 reg |= priv->dev->ports[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700254 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700255
256 bcm_sf2_imp_vlan_setup(ds, cpu_port);
257
Florian Fainelli450b05c2014-09-24 17:05:22 -0700258 /* If EEE was enabled, restore it */
259 if (priv->port_sts[port].eee.eee_enabled)
260 bcm_sf2_eee_enable_set(ds, port, true);
261
Florian Fainellib6d045d2014-09-24 17:05:20 -0700262 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700263}
264
Florian Fainellib6d045d2014-09-24 17:05:20 -0700265static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
266 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700267{
Florian Fainellif4589952016-08-26 12:18:33 -0700268 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700269 u32 off, reg;
270
Florian Fainelli96e65d72014-09-18 17:31:25 -0700271 if (priv->wol_ports_mask & (1 << port))
272 return;
273
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700274 if (port == priv->moca_port)
275 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700276
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700277 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800278 bcm_sf2_gphy_enable_set(ds, false);
279
Florian Fainelli246d7f72014-08-27 17:04:56 -0700280 if (dsa_is_cpu_port(ds, port))
281 off = CORE_IMP_CTL;
282 else
283 off = CORE_G_PCTL_PORT(port);
284
285 reg = core_readl(priv, off);
286 reg |= RX_DIS | TX_DIS;
287 core_writel(priv, reg, off);
288
289 /* Power down the port memory */
290 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
291 reg |= P_TXQ_PSM_VDD(port);
292 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
293}
294
Florian Fainelli450b05c2014-09-24 17:05:22 -0700295/* Returns 0 if EEE was not enabled, or 1 otherwise
296 */
297static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
298 struct phy_device *phy)
299{
Florian Fainelli450b05c2014-09-24 17:05:22 -0700300 int ret;
301
Florian Fainelli450b05c2014-09-24 17:05:22 -0700302 ret = phy_init_eee(phy, 0);
303 if (ret)
304 return 0;
305
306 bcm_sf2_eee_enable_set(ds, port, true);
307
308 return 1;
309}
310
Vivien Didelot08f50062017-08-01 16:32:41 -0400311static int bcm_sf2_sw_get_mac_eee(struct dsa_switch *ds, int port,
312 struct ethtool_eee *e)
Florian Fainelli450b05c2014-09-24 17:05:22 -0700313{
Florian Fainellif4589952016-08-26 12:18:33 -0700314 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700315 struct ethtool_eee *p = &priv->port_sts[port].eee;
316 u32 reg;
317
318 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
319 e->eee_enabled = p->eee_enabled;
320 e->eee_active = !!(reg & (1 << port));
321
322 return 0;
323}
324
Vivien Didelot08f50062017-08-01 16:32:41 -0400325static int bcm_sf2_sw_set_mac_eee(struct dsa_switch *ds, int port,
326 struct ethtool_eee *e)
Florian Fainelli450b05c2014-09-24 17:05:22 -0700327{
Florian Fainellif4589952016-08-26 12:18:33 -0700328 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700329 struct ethtool_eee *p = &priv->port_sts[port].eee;
330
331 p->eee_enabled = e->eee_enabled;
Vivien Didelotc48f7eb2017-08-01 16:32:38 -0400332 bcm_sf2_eee_enable_set(ds, port, e->eee_enabled);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700333
334 return 0;
335}
336
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700337static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
338 int regnum, u16 val)
339{
340 int ret = 0;
341 u32 reg;
342
343 reg = reg_readl(priv, REG_SWITCH_CNTRL);
344 reg |= MDIO_MASTER_SEL;
345 reg_writel(priv, reg, REG_SWITCH_CNTRL);
346
347 /* Page << 8 | offset */
348 reg = 0x70;
349 reg <<= 2;
350 core_writel(priv, addr, reg);
351
352 /* Page << 8 | offset */
353 reg = 0x80 << 8 | regnum << 1;
354 reg <<= 2;
355
356 if (op)
357 ret = core_readl(priv, reg);
358 else
359 core_writel(priv, val, reg);
360
361 reg = reg_readl(priv, REG_SWITCH_CNTRL);
362 reg &= ~MDIO_MASTER_SEL;
363 reg_writel(priv, reg, REG_SWITCH_CNTRL);
364
365 return ret & 0xffff;
366}
367
368static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
369{
370 struct bcm_sf2_priv *priv = bus->priv;
371
372 /* Intercept reads from Broadcom pseudo-PHY address, else, send
373 * them to our master MDIO bus controller
374 */
375 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
376 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
377 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800378 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700379}
380
381static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
382 u16 val)
383{
384 struct bcm_sf2_priv *priv = bus->priv;
385
386 /* Intercept writes to the Broadcom pseudo-PHY address, else,
387 * send them to our master MDIO bus controller
388 */
389 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
390 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
391 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800392 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700393
394 return 0;
395}
396
Florian Fainelli246d7f72014-08-27 17:04:56 -0700397static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
398{
399 struct bcm_sf2_priv *priv = dev_id;
400
401 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
402 ~priv->irq0_mask;
403 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
404
405 return IRQ_HANDLED;
406}
407
408static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
409{
410 struct bcm_sf2_priv *priv = dev_id;
411
412 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
413 ~priv->irq1_mask;
414 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
415
416 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
417 priv->port_sts[7].link = 1;
418 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
419 priv->port_sts[7].link = 0;
420
421 return IRQ_HANDLED;
422}
423
Florian Fainelli33f84612014-11-25 18:08:49 -0800424static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
425{
426 unsigned int timeout = 1000;
427 u32 reg;
428
429 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
430 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
431 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
432
433 do {
434 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
435 if (!(reg & SOFTWARE_RESET))
436 break;
437
438 usleep_range(1000, 2000);
439 } while (timeout-- > 0);
440
441 if (timeout == 0)
442 return -ETIMEDOUT;
443
444 return 0;
445}
446
Florian Fainelli691c9a82015-01-20 16:42:00 -0800447static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
448{
Florian Fainellif01d5982016-08-25 15:23:41 -0700449 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800450 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700451 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800452 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800453}
454
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700455static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
456 struct device_node *dn)
457{
458 struct device_node *port;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700459 int mode;
460 unsigned int port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700461
462 priv->moca_port = -1;
463
464 for_each_available_child_of_node(dn, port) {
465 if (of_property_read_u32(port, "reg", &port_num))
466 continue;
467
468 /* Internal PHYs get assigned a specific 'phy-mode' property
469 * value: "internal" to help flag them before MDIO probing
470 * has completed, since they might be turned off at that
471 * time
472 */
473 mode = of_get_phy_mode(port);
Florian Fainellibedd00c2017-06-23 10:33:16 -0700474 if (mode < 0)
475 continue;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700476
Florian Fainellibedd00c2017-06-23 10:33:16 -0700477 if (mode == PHY_INTERFACE_MODE_INTERNAL)
478 priv->int_phy_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700479
480 if (mode == PHY_INTERFACE_MODE_MOCA)
481 priv->moca_port = port_num;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800482
483 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
484 priv->brcm_tag_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700485 }
486}
487
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700488static int bcm_sf2_mdio_register(struct dsa_switch *ds)
489{
Florian Fainellif4589952016-08-26 12:18:33 -0700490 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700491 struct device_node *dn;
492 static int index;
493 int err;
494
495 /* Find our integrated MDIO bus node */
496 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
497 priv->master_mii_bus = of_mdio_find_bus(dn);
498 if (!priv->master_mii_bus)
499 return -EPROBE_DEFER;
500
501 get_device(&priv->master_mii_bus->dev);
502 priv->master_mii_dn = dn;
503
504 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
505 if (!priv->slave_mii_bus)
506 return -ENOMEM;
507
508 priv->slave_mii_bus->priv = priv;
509 priv->slave_mii_bus->name = "sf2 slave mii";
510 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
511 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
512 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
513 index++);
514 priv->slave_mii_bus->dev.of_node = dn;
515
516 /* Include the pseudo-PHY address to divert reads towards our
517 * workaround. This is only required for 7445D0, since 7445E0
518 * disconnects the internal switch pseudo-PHY such that we can use the
519 * regular SWITCH_MDIO master controller instead.
520 *
521 * Here we flag the pseudo PHY as needing special treatment and would
522 * otherwise make all other PHY read/writes go to the master MDIO bus
523 * controller that comes with this switch backed by the "mdio-unimac"
524 * driver.
525 */
526 if (of_machine_is_compatible("brcm,bcm7445d0"))
527 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
528 else
529 priv->indir_phy_mask = 0;
530
531 ds->phys_mii_mask = priv->indir_phy_mask;
532 ds->slave_mii_bus = priv->slave_mii_bus;
533 priv->slave_mii_bus->parent = ds->dev->parent;
534 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
535
536 if (dn)
537 err = of_mdiobus_register(priv->slave_mii_bus, dn);
538 else
539 err = mdiobus_register(priv->slave_mii_bus);
540
541 if (err)
542 of_node_put(dn);
543
544 return err;
545}
546
547static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
548{
549 mdiobus_unregister(priv->slave_mii_bus);
550 if (priv->master_mii_dn)
551 of_node_put(priv->master_mii_dn);
552}
553
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700554static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
555{
Florian Fainellif4589952016-08-26 12:18:33 -0700556 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700557
558 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
559 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
560 * the REG_PHY_REVISION register layout is.
561 */
562
563 return priv->hw_params.gphy_rev;
564}
565
Florian Fainelli246d7f72014-08-27 17:04:56 -0700566static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
567 struct phy_device *phydev)
568{
Florian Fainellif4589952016-08-26 12:18:33 -0700569 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli76da8702016-11-22 11:40:58 -0800570 struct ethtool_eee *p = &priv->port_sts[port].eee;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700571 u32 id_mode_dis = 0, port_mode;
572 const char *str = NULL;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800573 u32 reg, offset;
574
575 if (priv->type == BCM7445_DEVICE_ID)
576 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
577 else
578 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700579
580 switch (phydev->interface) {
581 case PHY_INTERFACE_MODE_RGMII:
582 str = "RGMII (no delay)";
583 id_mode_dis = 1;
584 case PHY_INTERFACE_MODE_RGMII_TXID:
585 if (!str)
586 str = "RGMII (TX delay)";
587 port_mode = EXT_GPHY;
588 break;
589 case PHY_INTERFACE_MODE_MII:
590 str = "MII";
591 port_mode = EXT_EPHY;
592 break;
593 case PHY_INTERFACE_MODE_REVMII:
594 str = "Reverse MII";
595 port_mode = EXT_REVMII;
596 break;
597 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700598 /* All other PHYs: internal and MoCA */
599 goto force_link;
600 }
601
602 /* If the link is down, just disable the interface to conserve power */
603 if (!phydev->link) {
604 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
605 reg &= ~RGMII_MODE_EN;
606 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700607 goto force_link;
608 }
609
610 /* Clear id_mode_dis bit, and the existing port mode, but
611 * make sure we enable the RGMII block for data to pass
612 */
613 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
614 reg &= ~ID_MODE_DIS;
615 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
616 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
617
618 reg |= port_mode | RGMII_MODE_EN;
619 if (id_mode_dis)
620 reg |= ID_MODE_DIS;
621
622 if (phydev->pause) {
623 if (phydev->asym_pause)
624 reg |= TX_PAUSE_EN;
625 reg |= RX_PAUSE_EN;
626 }
627
628 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
629
630 pr_info("Port %d configured for %s\n", port, str);
631
632force_link:
633 /* Force link settings detected from the PHY */
634 reg = SW_OVERRIDE;
635 switch (phydev->speed) {
636 case SPEED_1000:
637 reg |= SPDSTS_1000 << SPEED_SHIFT;
638 break;
639 case SPEED_100:
640 reg |= SPDSTS_100 << SPEED_SHIFT;
641 break;
642 }
643
644 if (phydev->link)
645 reg |= LINK_STS;
646 if (phydev->duplex == DUPLEX_FULL)
647 reg |= DUPLX_MODE;
648
Florian Fainelli0fe99332017-01-20 12:36:30 -0800649 core_writel(priv, reg, offset);
Florian Fainelli76da8702016-11-22 11:40:58 -0800650
651 if (!phydev->is_pseudo_fixed_link)
652 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700653}
654
655static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
656 struct fixed_phy_status *status)
657{
Florian Fainellif4589952016-08-26 12:18:33 -0700658 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800659 u32 duplex, pause, offset;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700660 u32 reg;
661
Florian Fainelli0fe99332017-01-20 12:36:30 -0800662 if (priv->type == BCM7445_DEVICE_ID)
663 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
664 else
665 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
666
Florian Fainelli246d7f72014-08-27 17:04:56 -0700667 duplex = core_readl(priv, CORE_DUPSTS);
668 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700669
670 status->link = 0;
671
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700672 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700673 * which means that we need to force the link at the port override
674 * level to get the data to flow. We do use what the interrupt handler
675 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -0800676 *
677 * For the other ports, we just force the link status, since this is
678 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700679 */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700680 if (port == priv->moca_port) {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700681 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -0700682 /* For MoCA interfaces, also force a link down notification
683 * since some version of the user-space daemon (mocad) use
684 * cmd->autoneg to force the link, which messes up the PHY
685 * state machine and make it go in PHY_FORCING state instead.
686 */
687 if (!status->link)
Andrew Lunnc8b09802016-06-04 21:16:57 +0200688 netif_carrier_off(ds->ports[port].netdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700689 status->duplex = 1;
690 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -0800691 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700692 status->duplex = !!(duplex & (1 << port));
693 }
694
Florian Fainelli0fe99332017-01-20 12:36:30 -0800695 reg = core_readl(priv, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800696 reg |= SW_OVERRIDE;
697 if (status->link)
698 reg |= LINK_STS;
699 else
700 reg &= ~LINK_STS;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800701 core_writel(priv, reg, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800702
Florian Fainelli246d7f72014-08-27 17:04:56 -0700703 if ((pause & (1 << port)) &&
704 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
705 status->asym_pause = 1;
706 status->pause = 1;
707 }
708
709 if (pause & (1 << port))
710 status->pause = 1;
711}
712
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700713static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
714{
Florian Fainellif4589952016-08-26 12:18:33 -0700715 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700716 unsigned int port;
717
Florian Fainelli691c9a82015-01-20 16:42:00 -0800718 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700719
720 /* Disable all ports physically present including the IMP
721 * port, the other ones have already been disabled during
722 * bcm_sf2_sw_setup
723 */
724 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200725 if ((1 << port) & ds->enabled_port_mask ||
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700726 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700727 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700728 }
729
730 return 0;
731}
732
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700733static int bcm_sf2_sw_resume(struct dsa_switch *ds)
734{
Florian Fainellif4589952016-08-26 12:18:33 -0700735 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700736 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700737 int ret;
738
739 ret = bcm_sf2_sw_rst(priv);
740 if (ret) {
741 pr_err("%s: failed to software reset switch\n", __func__);
742 return ret;
743 }
744
Florian Fainellib0836682015-02-05 11:40:41 -0800745 if (priv->hw_params.num_gphy == 1)
746 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700747
748 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200749 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700750 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700751 else if (dsa_is_cpu_port(ds, port))
752 bcm_sf2_imp_setup(ds, port);
753 }
754
755 return 0;
756}
757
Florian Fainelli96e65d72014-09-18 17:31:25 -0700758static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
759 struct ethtool_wolinfo *wol)
760{
Vivien Didelot3a8f6f82017-08-02 15:48:25 -0400761 struct net_device *p = ds->dst->cpu_dp->netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700762 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700763 struct ethtool_wolinfo pwol;
764
765 /* Get the parent device WoL settings */
766 p->ethtool_ops->get_wol(p, &pwol);
767
768 /* Advertise the parent device supported settings */
769 wol->supported = pwol.supported;
770 memset(&wol->sopass, 0, sizeof(wol->sopass));
771
772 if (pwol.wolopts & WAKE_MAGICSECURE)
773 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
774
775 if (priv->wol_ports_mask & (1 << port))
776 wol->wolopts = pwol.wolopts;
777 else
778 wol->wolopts = 0;
779}
780
781static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
782 struct ethtool_wolinfo *wol)
783{
Vivien Didelot3a8f6f82017-08-02 15:48:25 -0400784 struct net_device *p = ds->dst->cpu_dp->netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700785 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400786 s8 cpu_port = ds->dst->cpu_dp->index;
Florian Fainelli96e65d72014-09-18 17:31:25 -0700787 struct ethtool_wolinfo pwol;
788
789 p->ethtool_ops->get_wol(p, &pwol);
790 if (wol->wolopts & ~pwol.supported)
791 return -EINVAL;
792
793 if (wol->wolopts)
794 priv->wol_ports_mask |= (1 << port);
795 else
796 priv->wol_ports_mask &= ~(1 << port);
797
798 /* If we have at least one port enabled, make sure the CPU port
799 * is also enabled. If the CPU port is the last one enabled, we disable
800 * it since this configuration does not make sense.
801 */
802 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
803 priv->wol_ports_mask |= (1 << cpu_port);
804 else
805 priv->wol_ports_mask &= ~(1 << cpu_port);
806
807 return p->ethtool_ops->set_wol(p, wol);
808}
809
Florian Fainellide0b9d32016-08-26 12:18:34 -0700810static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
Florian Fainelli9c57a772016-06-09 17:42:08 -0700811{
Florian Fainellide0b9d32016-08-26 12:18:34 -0700812 unsigned int timeout = 10;
813 u32 reg;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700814
Florian Fainellide0b9d32016-08-26 12:18:34 -0700815 do {
816 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
817 if (!(reg & ARLA_VTBL_STDN))
818 return 0;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700819
Florian Fainellide0b9d32016-08-26 12:18:34 -0700820 usleep_range(1000, 2000);
821 } while (timeout--);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700822
Florian Fainellide0b9d32016-08-26 12:18:34 -0700823 return -ETIMEDOUT;
824}
Florian Fainelli9c57a772016-06-09 17:42:08 -0700825
Florian Fainellide0b9d32016-08-26 12:18:34 -0700826static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
827{
828 core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
829
830 return bcm_sf2_vlan_op_wait(priv);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700831}
832
833static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
834{
Florian Fainellif4589952016-08-26 12:18:33 -0700835 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700836 unsigned int port;
837
838 /* Clear all VLANs */
839 bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
840
841 for (port = 0; port < priv->hw_params.num_ports; port++) {
842 if (!((1 << port) & ds->enabled_port_mask))
843 continue;
844
845 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
846 }
847}
848
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700849static int bcm_sf2_sw_setup(struct dsa_switch *ds)
850{
Florian Fainellif4589952016-08-26 12:18:33 -0700851 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700852 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700853
Florian Fainellie85ec742017-09-19 10:46:46 -0700854 /* Disable unused ports and configure IMP port */
Florian Fainellid9338022016-08-18 15:30:14 -0700855 for (port = 0; port < priv->hw_params.num_ports; port++) {
Florian Fainellie85ec742017-09-19 10:46:46 -0700856 if (dsa_is_cpu_port(ds, port))
Florian Fainellid9338022016-08-18 15:30:14 -0700857 bcm_sf2_imp_setup(ds, port);
Florian Fainellie85ec742017-09-19 10:46:46 -0700858 else if (!((1 << port) & ds->enabled_port_mask))
Florian Fainellid9338022016-08-18 15:30:14 -0700859 bcm_sf2_port_disable(ds, port, NULL);
860 }
861
862 bcm_sf2_sw_configure_vlan(ds);
863
864 return 0;
865}
866
Florian Fainellif4589952016-08-26 12:18:33 -0700867/* The SWITCH_CORE register space is managed by b53 but operates on a page +
868 * register basis so we need to translate that into an address that the
869 * bus-glue understands.
870 */
871#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
872
873static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
874 u8 *val)
875{
876 struct bcm_sf2_priv *priv = dev->priv;
877
878 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
879
880 return 0;
881}
882
883static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
884 u16 *val)
885{
886 struct bcm_sf2_priv *priv = dev->priv;
887
888 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
889
890 return 0;
891}
892
893static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
894 u32 *val)
895{
896 struct bcm_sf2_priv *priv = dev->priv;
897
898 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
899
900 return 0;
901}
902
903static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
904 u64 *val)
905{
906 struct bcm_sf2_priv *priv = dev->priv;
907
908 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
909
910 return 0;
911}
912
913static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
914 u8 value)
915{
916 struct bcm_sf2_priv *priv = dev->priv;
917
918 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
919
920 return 0;
921}
922
923static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
924 u16 value)
925{
926 struct bcm_sf2_priv *priv = dev->priv;
927
928 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
929
930 return 0;
931}
932
933static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
934 u32 value)
935{
936 struct bcm_sf2_priv *priv = dev->priv;
937
938 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
939
940 return 0;
941}
942
943static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
944 u64 value)
945{
946 struct bcm_sf2_priv *priv = dev->priv;
947
948 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
949
950 return 0;
951}
952
Bhumika Goyal7e3108f2017-08-29 22:17:52 +0530953static const struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -0700954 .read8 = bcm_sf2_core_read8,
955 .read16 = bcm_sf2_core_read16,
956 .read32 = bcm_sf2_core_read32,
957 .read48 = bcm_sf2_core_read64,
958 .read64 = bcm_sf2_core_read64,
959 .write8 = bcm_sf2_core_write8,
960 .write16 = bcm_sf2_core_write16,
961 .write32 = bcm_sf2_core_write32,
962 .write48 = bcm_sf2_core_write64,
963 .write64 = bcm_sf2_core_write64,
964};
965
Florian Fainellia82f67a2017-01-08 14:52:08 -0800966static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli73095cb2017-01-08 14:52:06 -0800967 .get_tag_protocol = bcm_sf2_sw_get_tag_protocol,
968 .setup = bcm_sf2_sw_setup,
969 .get_strings = b53_get_strings,
970 .get_ethtool_stats = b53_get_ethtool_stats,
971 .get_sset_count = b53_get_sset_count,
972 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
973 .adjust_link = bcm_sf2_sw_adjust_link,
974 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
975 .suspend = bcm_sf2_sw_suspend,
976 .resume = bcm_sf2_sw_resume,
977 .get_wol = bcm_sf2_sw_get_wol,
978 .set_wol = bcm_sf2_sw_set_wol,
979 .port_enable = bcm_sf2_port_setup,
980 .port_disable = bcm_sf2_port_disable,
Vivien Didelot08f50062017-08-01 16:32:41 -0400981 .get_mac_eee = bcm_sf2_sw_get_mac_eee,
982 .set_mac_eee = bcm_sf2_sw_set_mac_eee,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800983 .port_bridge_join = b53_br_join,
984 .port_bridge_leave = b53_br_leave,
985 .port_stp_state_set = b53_br_set_stp_state,
986 .port_fast_age = b53_br_fast_age,
987 .port_vlan_filtering = b53_vlan_filtering,
988 .port_vlan_prepare = b53_vlan_prepare,
989 .port_vlan_add = b53_vlan_add,
990 .port_vlan_del = b53_vlan_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800991 .port_fdb_dump = b53_fdb_dump,
992 .port_fdb_add = b53_fdb_add,
993 .port_fdb_del = b53_fdb_del,
Florian Fainelli73181662017-01-30 09:48:43 -0800994 .get_rxnfc = bcm_sf2_get_rxnfc,
995 .set_rxnfc = bcm_sf2_set_rxnfc,
Florian Fainelliec960de2017-01-30 12:41:43 -0800996 .port_mirror_add = b53_mirror_add,
997 .port_mirror_del = b53_mirror_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -0800998};
999
Florian Fainellia78e86e2017-01-20 12:36:29 -08001000struct bcm_sf2_of_data {
1001 u32 type;
1002 const u16 *reg_offsets;
1003 unsigned int core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001004 unsigned int num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001005};
1006
1007/* Register offsets for the SWITCH_REG_* block */
1008static const u16 bcm_sf2_7445_reg_offsets[] = {
1009 [REG_SWITCH_CNTRL] = 0x00,
1010 [REG_SWITCH_STATUS] = 0x04,
1011 [REG_DIR_DATA_WRITE] = 0x08,
1012 [REG_DIR_DATA_READ] = 0x0C,
1013 [REG_SWITCH_REVISION] = 0x18,
1014 [REG_PHY_REVISION] = 0x1C,
1015 [REG_SPHY_CNTRL] = 0x2C,
1016 [REG_RGMII_0_CNTRL] = 0x34,
1017 [REG_RGMII_1_CNTRL] = 0x40,
1018 [REG_RGMII_2_CNTRL] = 0x4c,
1019 [REG_LED_0_CNTRL] = 0x90,
1020 [REG_LED_1_CNTRL] = 0x94,
1021 [REG_LED_2_CNTRL] = 0x98,
1022};
1023
1024static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1025 .type = BCM7445_DEVICE_ID,
1026 .core_reg_align = 0,
1027 .reg_offsets = bcm_sf2_7445_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001028 .num_cfp_rules = 256,
Florian Fainellia78e86e2017-01-20 12:36:29 -08001029};
1030
Florian Fainelli0fe99332017-01-20 12:36:30 -08001031static const u16 bcm_sf2_7278_reg_offsets[] = {
1032 [REG_SWITCH_CNTRL] = 0x00,
1033 [REG_SWITCH_STATUS] = 0x04,
1034 [REG_DIR_DATA_WRITE] = 0x08,
1035 [REG_DIR_DATA_READ] = 0x0c,
1036 [REG_SWITCH_REVISION] = 0x10,
1037 [REG_PHY_REVISION] = 0x14,
1038 [REG_SPHY_CNTRL] = 0x24,
1039 [REG_RGMII_0_CNTRL] = 0xe0,
1040 [REG_RGMII_1_CNTRL] = 0xec,
1041 [REG_RGMII_2_CNTRL] = 0xf8,
1042 [REG_LED_0_CNTRL] = 0x40,
1043 [REG_LED_1_CNTRL] = 0x4c,
1044 [REG_LED_2_CNTRL] = 0x58,
1045};
1046
1047static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1048 .type = BCM7278_DEVICE_ID,
1049 .core_reg_align = 1,
1050 .reg_offsets = bcm_sf2_7278_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001051 .num_cfp_rules = 128,
Florian Fainelli0fe99332017-01-20 12:36:30 -08001052};
1053
Florian Fainellia78e86e2017-01-20 12:36:29 -08001054static const struct of_device_id bcm_sf2_of_match[] = {
1055 { .compatible = "brcm,bcm7445-switch-v4.0",
1056 .data = &bcm_sf2_7445_data
1057 },
Florian Fainelli0fe99332017-01-20 12:36:30 -08001058 { .compatible = "brcm,bcm7278-switch-v4.0",
1059 .data = &bcm_sf2_7278_data
1060 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001061 { /* sentinel */ },
1062};
1063MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1064
Florian Fainellid9338022016-08-18 15:30:14 -07001065static int bcm_sf2_sw_probe(struct platform_device *pdev)
1066{
1067 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1068 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001069 const struct of_device_id *of_id = NULL;
1070 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001071 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001072 struct dsa_switch_ops *ops;
Florian Fainellid9338022016-08-18 15:30:14 -07001073 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001074 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001075 struct dsa_switch *ds;
1076 void __iomem **base;
Florian Fainelli4bd11672016-08-18 15:30:15 -07001077 struct resource *r;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001078 unsigned int i;
1079 u32 reg, rev;
1080 int ret;
1081
Florian Fainellif4589952016-08-26 12:18:33 -07001082 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1083 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001084 return -ENOMEM;
1085
Florian Fainellia4c61b92017-01-07 21:01:56 -08001086 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1087 if (!ops)
1088 return -ENOMEM;
1089
Florian Fainellif4589952016-08-26 12:18:33 -07001090 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1091 if (!dev)
1092 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001093
Florian Fainellif4589952016-08-26 12:18:33 -07001094 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1095 if (!pdata)
1096 return -ENOMEM;
1097
Florian Fainellia78e86e2017-01-20 12:36:29 -08001098 of_id = of_match_node(bcm_sf2_of_match, dn);
1099 if (!of_id || !of_id->data)
1100 return -EINVAL;
1101
1102 data = of_id->data;
1103
1104 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1105 priv->type = data->type;
1106 priv->reg_offsets = data->reg_offsets;
1107 priv->core_reg_align = data->core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001108 priv->num_cfp_rules = data->num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001109
Florian Fainellif4589952016-08-26 12:18:33 -07001110 /* Auto-detection using standard registers will not work, so
1111 * provide an indication of what kind of device we are for
1112 * b53_common to work with
1113 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001114 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001115 dev->pdata = pdata;
1116
1117 priv->dev = dev;
1118 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001119 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001120
Florian Fainelli181183772017-09-03 20:27:02 -07001121 /* Advertise the 8 egress queues */
1122 ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1123
Florian Fainellif4589952016-08-26 12:18:33 -07001124 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001125
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001126 spin_lock_init(&priv->indir_lock);
1127 mutex_init(&priv->stats_mutex);
Florian Fainelli73181662017-01-30 09:48:43 -08001128 mutex_init(&priv->cfp.lock);
1129
1130 /* CFP rule #0 cannot be used for specific classifications, flag it as
1131 * permanently used
1132 */
1133 set_bit(0, priv->cfp.used);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001134
Florian Fainellid9338022016-08-18 15:30:14 -07001135 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001136
1137 priv->irq0 = irq_of_parse_and_map(dn, 0);
1138 priv->irq1 = irq_of_parse_and_map(dn, 1);
1139
1140 base = &priv->core;
1141 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainelli4bd11672016-08-18 15:30:15 -07001142 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1143 *base = devm_ioremap_resource(&pdev->dev, r);
1144 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001145 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001146 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001147 }
1148 base++;
1149 }
1150
1151 ret = bcm_sf2_sw_rst(priv);
1152 if (ret) {
1153 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001154 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001155 }
1156
1157 ret = bcm_sf2_mdio_register(ds);
1158 if (ret) {
1159 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001160 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001161 }
1162
Florian Fainelli73181662017-01-30 09:48:43 -08001163 ret = bcm_sf2_cfp_rst(priv);
1164 if (ret) {
1165 pr_err("failed to reset CFP\n");
1166 goto out_mdio;
1167 }
1168
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001169 /* Disable all interrupts and request them */
1170 bcm_sf2_intr_disable(priv);
1171
Florian Fainelli4bd11672016-08-18 15:30:15 -07001172 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1173 "switch_0", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001174 if (ret < 0) {
1175 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001176 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001177 }
1178
Florian Fainelli4bd11672016-08-18 15:30:15 -07001179 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1180 "switch_1", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001181 if (ret < 0) {
1182 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001183 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001184 }
1185
1186 /* Reset the MIB counters */
1187 reg = core_readl(priv, CORE_GMNCFGCFG);
1188 reg |= RST_MIB_CNT;
1189 core_writel(priv, reg, CORE_GMNCFGCFG);
1190 reg &= ~RST_MIB_CNT;
1191 core_writel(priv, reg, CORE_GMNCFGCFG);
1192
1193 /* Get the maximum number of ports for this switch */
1194 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1195 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1196 priv->hw_params.num_ports = DSA_MAX_PORTS;
1197
1198 /* Assume a single GPHY setup if we can't read that property */
1199 if (of_property_read_u32(dn, "brcm,num-gphy",
1200 &priv->hw_params.num_gphy))
1201 priv->hw_params.num_gphy = 1;
1202
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001203 rev = reg_readl(priv, REG_SWITCH_REVISION);
1204 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1205 SWITCH_TOP_REV_MASK;
1206 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1207
1208 rev = reg_readl(priv, REG_PHY_REVISION);
1209 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1210
Florian Fainellif4589952016-08-26 12:18:33 -07001211 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001212 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001213 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001214
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001215 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1216 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1217 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1218 priv->core, priv->irq0, priv->irq1);
1219
1220 return 0;
1221
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001222out_mdio:
1223 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001224 return ret;
1225}
1226
Florian Fainellid9338022016-08-18 15:30:14 -07001227static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001228{
Florian Fainellif4589952016-08-26 12:18:33 -07001229 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001230
1231 /* Disable all ports and interrupts */
1232 priv->wol_ports_mask = 0;
Florian Fainellif4589952016-08-26 12:18:33 -07001233 bcm_sf2_sw_suspend(priv->dev->ds);
1234 dsa_unregister_switch(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001235 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001236
1237 return 0;
1238}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001239
Florian Fainelli2399d612016-10-20 09:32:19 -07001240static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1241{
1242 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1243
1244 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1245 * successful MDIO bus scan to occur. If we did turn off the GPHY
1246 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001247 *
1248 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001249 */
1250 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001251 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001252}
1253
Florian Fainellid9338022016-08-18 15:30:14 -07001254#ifdef CONFIG_PM_SLEEP
1255static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001256{
Florian Fainellid9338022016-08-18 15:30:14 -07001257 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001258 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001259
Florian Fainellif4589952016-08-26 12:18:33 -07001260 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001261}
Florian Fainellid9338022016-08-18 15:30:14 -07001262
1263static int bcm_sf2_resume(struct device *dev)
1264{
1265 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001266 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001267
Florian Fainellif4589952016-08-26 12:18:33 -07001268 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001269}
1270#endif /* CONFIG_PM_SLEEP */
1271
1272static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1273 bcm_sf2_suspend, bcm_sf2_resume);
1274
Florian Fainellid9338022016-08-18 15:30:14 -07001275
1276static struct platform_driver bcm_sf2_driver = {
1277 .probe = bcm_sf2_sw_probe,
1278 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001279 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001280 .driver = {
1281 .name = "brcm-sf2",
1282 .of_match_table = bcm_sf2_of_match,
1283 .pm = &bcm_sf2_pm_ops,
1284 },
1285};
1286module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001287
1288MODULE_AUTHOR("Broadcom Corporation");
1289MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1290MODULE_LICENSE("GPL");
1291MODULE_ALIAS("platform:brcm-sf2");