blob: 8acbd17bc1fd3f14517def72ce2e42360f147d6b [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 -080063static void bcm_sf2_brcm_hdr_setup(struct bcm_sf2_priv *priv, int port)
Florian Fainellib6d045d2014-09-24 17:05:20 -070064{
Florian Fainelliebb2ac42017-01-20 12:36:31 -080065 u32 reg, val;
Florian Fainelli246d7f72014-08-27 17:04:56 -070066
67 /* Resolve which bit controls the Broadcom tag */
68 switch (port) {
69 case 8:
70 val = BRCM_HDR_EN_P8;
71 break;
72 case 7:
73 val = BRCM_HDR_EN_P7;
74 break;
75 case 5:
76 val = BRCM_HDR_EN_P5;
77 break;
78 default:
79 val = 0;
80 break;
81 }
82
83 /* Enable Broadcom tags for IMP port */
84 reg = core_readl(priv, CORE_BRCM_HDR_CTRL);
85 reg |= val;
86 core_writel(priv, reg, CORE_BRCM_HDR_CTRL);
87
88 /* Enable reception Broadcom tag for CPU TX (switch RX) to
89 * allow us to tag outgoing frames
90 */
91 reg = core_readl(priv, CORE_BRCM_HDR_RX_DIS);
92 reg &= ~(1 << port);
93 core_writel(priv, reg, CORE_BRCM_HDR_RX_DIS);
94
95 /* Enable transmission of Broadcom tags from the switch (CPU RX) to
96 * allow delivering frames to the per-port net_devices
97 */
98 reg = core_readl(priv, CORE_BRCM_HDR_TX_DIS);
99 reg &= ~(1 << port);
100 core_writel(priv, reg, CORE_BRCM_HDR_TX_DIS);
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800101}
102
103static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
104{
105 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellic837fc82017-09-03 20:27:03 -0700106 unsigned int i;
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800107 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
Florian Fainellic837fc82017-09-03 20:27:03 -0700133 /* Configure Traffic Class to QoS mapping, allow each priority to map
134 * to a different queue number
135 */
136 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
137 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
138 reg |= i << (PRT_TO_QID_SHIFT * i);
139 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
140
Florian Fainelliebb2ac42017-01-20 12:36:31 -0800141 bcm_sf2_brcm_hdr_setup(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700142
143 /* Force link status for IMP port */
Florian Fainelli0fe99332017-01-20 12:36:30 -0800144 reg = core_readl(priv, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700145 reg |= (MII_SW_OR | LINK_STS);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800146 core_writel(priv, reg, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700147}
148
Florian Fainelli450b05c2014-09-24 17:05:22 -0700149static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
150{
Florian Fainellif4589952016-08-26 12:18:33 -0700151 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700152 u32 reg;
153
154 reg = core_readl(priv, CORE_EEE_EN_CTRL);
155 if (enable)
156 reg |= 1 << port;
157 else
158 reg &= ~(1 << port);
159 core_writel(priv, reg, CORE_EEE_EN_CTRL);
160}
161
Florian Fainellib0836682015-02-05 11:40:41 -0800162static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
163{
Florian Fainellif4589952016-08-26 12:18:33 -0700164 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -0800165 u32 reg;
166
Florian Fainelli9af197a2015-02-05 11:40:42 -0800167 reg = reg_readl(priv, REG_SPHY_CNTRL);
168 if (enable) {
169 reg |= PHY_RESET;
170 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
171 reg_writel(priv, reg, REG_SPHY_CNTRL);
172 udelay(21);
173 reg = reg_readl(priv, REG_SPHY_CNTRL);
174 reg &= ~PHY_RESET;
175 } else {
176 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
177 reg_writel(priv, reg, REG_SPHY_CNTRL);
178 mdelay(1);
179 reg |= CK25_DIS;
180 }
181 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800182
Florian Fainelli9af197a2015-02-05 11:40:42 -0800183 /* Use PHY-driven LED signaling */
184 if (!enable) {
185 reg = reg_readl(priv, REG_LED_CNTRL(0));
186 reg |= SPDLNK_SRC_SEL;
187 reg_writel(priv, reg, REG_LED_CNTRL(0));
188 }
Florian Fainellib0836682015-02-05 11:40:41 -0800189}
190
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700191static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
192 int port)
193{
194 unsigned int off;
195
196 switch (port) {
197 case 7:
198 off = P7_IRQ_OFF;
199 break;
200 case 0:
201 /* Port 0 interrupts are located on the first bank */
202 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
203 return;
204 default:
205 off = P_IRQ_OFF(port);
206 break;
207 }
208
209 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
210}
211
212static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
213 int port)
214{
215 unsigned int off;
216
217 switch (port) {
218 case 7:
219 off = P7_IRQ_OFF;
220 break;
221 case 0:
222 /* Port 0 interrupts are located on the first bank */
223 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
224 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
225 return;
226 default:
227 off = P_IRQ_OFF(port);
228 break;
229 }
230
231 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
232 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
233}
234
Florian Fainellib6d045d2014-09-24 17:05:20 -0700235static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
236 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700237{
Florian Fainellif4589952016-08-26 12:18:33 -0700238 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400239 s8 cpu_port = ds->dst->cpu_dp->index;
Florian Fainellie1b91472017-01-30 09:48:41 -0800240 unsigned int i;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700241 u32 reg;
242
243 /* Clear the memory power down */
244 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
245 reg &= ~P_TXQ_PSM_VDD(port);
246 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
247
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800248 /* Enable Broadcom tags for that port if requested */
249 if (priv->brcm_tag_mask & BIT(port))
250 bcm_sf2_brcm_hdr_setup(priv, port);
251
Florian Fainellie1b91472017-01-30 09:48:41 -0800252 /* Configure Traffic Class to QoS mapping, allow each priority to map
253 * to a different queue number
254 */
255 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
Florian Fainelli181183772017-09-03 20:27:02 -0700256 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
Florian Fainellie1b91472017-01-30 09:48:41 -0800257 reg |= i << (PRT_TO_QID_SHIFT * i);
258 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
259
Florian Fainelli246d7f72014-08-27 17:04:56 -0700260 /* Clear the Rx and Tx disable bits and set to no spanning tree */
261 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
262
Florian Fainelli9af197a2015-02-05 11:40:42 -0800263 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700264 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800265 bcm_sf2_gphy_enable_set(ds, true);
266 if (phy) {
267 /* if phy_stop() has been called before, phy
268 * will be in halted state, and phy_start()
269 * will call resume.
270 *
271 * the resume path does not configure back
272 * autoneg settings, and since we hard reset
273 * the phy manually here, we need to reset the
274 * state machine also.
275 */
276 phy->state = PHY_READY;
277 phy_init_hw(phy);
278 }
279 }
280
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700281 /* Enable MoCA port interrupts to get notified */
282 if (port == priv->moca_port)
283 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700284
Florian Fainelli12f460f2015-02-24 13:15:34 -0800285 /* Set this port, and only this one to be in the default VLAN,
286 * if member of a bridge, restore its membership prior to
287 * bringing down this port.
288 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700289 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
290 reg &= ~PORT_VLAN_CTRL_MASK;
291 reg |= (1 << port);
Florian Fainelli02154922016-09-10 12:39:03 -0700292 reg |= priv->dev->ports[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700293 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700294
295 bcm_sf2_imp_vlan_setup(ds, cpu_port);
296
Florian Fainelli450b05c2014-09-24 17:05:22 -0700297 /* If EEE was enabled, restore it */
298 if (priv->port_sts[port].eee.eee_enabled)
299 bcm_sf2_eee_enable_set(ds, port, true);
300
Florian Fainellib6d045d2014-09-24 17:05:20 -0700301 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700302}
303
Florian Fainellib6d045d2014-09-24 17:05:20 -0700304static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
305 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700306{
Florian Fainellif4589952016-08-26 12:18:33 -0700307 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700308 u32 off, reg;
309
Florian Fainelli96e65d72014-09-18 17:31:25 -0700310 if (priv->wol_ports_mask & (1 << port))
311 return;
312
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700313 if (port == priv->moca_port)
314 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700315
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700316 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800317 bcm_sf2_gphy_enable_set(ds, false);
318
Florian Fainelli246d7f72014-08-27 17:04:56 -0700319 if (dsa_is_cpu_port(ds, port))
320 off = CORE_IMP_CTL;
321 else
322 off = CORE_G_PCTL_PORT(port);
323
324 reg = core_readl(priv, off);
325 reg |= RX_DIS | TX_DIS;
326 core_writel(priv, reg, off);
327
328 /* Power down the port memory */
329 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
330 reg |= P_TXQ_PSM_VDD(port);
331 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
332}
333
Florian Fainelli450b05c2014-09-24 17:05:22 -0700334/* Returns 0 if EEE was not enabled, or 1 otherwise
335 */
336static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
337 struct phy_device *phy)
338{
Florian Fainelli450b05c2014-09-24 17:05:22 -0700339 int ret;
340
Florian Fainelli450b05c2014-09-24 17:05:22 -0700341 ret = phy_init_eee(phy, 0);
342 if (ret)
343 return 0;
344
345 bcm_sf2_eee_enable_set(ds, port, true);
346
347 return 1;
348}
349
Vivien Didelot08f50062017-08-01 16:32:41 -0400350static int bcm_sf2_sw_get_mac_eee(struct dsa_switch *ds, int port,
351 struct ethtool_eee *e)
Florian Fainelli450b05c2014-09-24 17:05:22 -0700352{
Florian Fainellif4589952016-08-26 12:18:33 -0700353 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700354 struct ethtool_eee *p = &priv->port_sts[port].eee;
355 u32 reg;
356
357 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
358 e->eee_enabled = p->eee_enabled;
359 e->eee_active = !!(reg & (1 << port));
360
361 return 0;
362}
363
Vivien Didelot08f50062017-08-01 16:32:41 -0400364static int bcm_sf2_sw_set_mac_eee(struct dsa_switch *ds, int port,
365 struct ethtool_eee *e)
Florian Fainelli450b05c2014-09-24 17:05:22 -0700366{
Florian Fainellif4589952016-08-26 12:18:33 -0700367 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700368 struct ethtool_eee *p = &priv->port_sts[port].eee;
369
370 p->eee_enabled = e->eee_enabled;
Vivien Didelotc48f7eb2017-08-01 16:32:38 -0400371 bcm_sf2_eee_enable_set(ds, port, e->eee_enabled);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700372
373 return 0;
374}
375
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700376static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
377 int regnum, u16 val)
378{
379 int ret = 0;
380 u32 reg;
381
382 reg = reg_readl(priv, REG_SWITCH_CNTRL);
383 reg |= MDIO_MASTER_SEL;
384 reg_writel(priv, reg, REG_SWITCH_CNTRL);
385
386 /* Page << 8 | offset */
387 reg = 0x70;
388 reg <<= 2;
389 core_writel(priv, addr, reg);
390
391 /* Page << 8 | offset */
392 reg = 0x80 << 8 | regnum << 1;
393 reg <<= 2;
394
395 if (op)
396 ret = core_readl(priv, reg);
397 else
398 core_writel(priv, val, reg);
399
400 reg = reg_readl(priv, REG_SWITCH_CNTRL);
401 reg &= ~MDIO_MASTER_SEL;
402 reg_writel(priv, reg, REG_SWITCH_CNTRL);
403
404 return ret & 0xffff;
405}
406
407static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
408{
409 struct bcm_sf2_priv *priv = bus->priv;
410
411 /* Intercept reads from Broadcom pseudo-PHY address, else, send
412 * them to our master MDIO bus controller
413 */
414 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
415 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
416 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800417 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700418}
419
420static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
421 u16 val)
422{
423 struct bcm_sf2_priv *priv = bus->priv;
424
425 /* Intercept writes to the Broadcom pseudo-PHY address, else,
426 * send them to our master MDIO bus controller
427 */
428 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
429 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
430 else
Florian Fainelli2cfe8f822017-01-07 21:01:57 -0800431 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700432
433 return 0;
434}
435
Florian Fainelli246d7f72014-08-27 17:04:56 -0700436static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
437{
438 struct bcm_sf2_priv *priv = dev_id;
439
440 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
441 ~priv->irq0_mask;
442 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
443
444 return IRQ_HANDLED;
445}
446
447static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
448{
449 struct bcm_sf2_priv *priv = dev_id;
450
451 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
452 ~priv->irq1_mask;
453 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
454
455 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
456 priv->port_sts[7].link = 1;
457 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
458 priv->port_sts[7].link = 0;
459
460 return IRQ_HANDLED;
461}
462
Florian Fainelli33f84612014-11-25 18:08:49 -0800463static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
464{
465 unsigned int timeout = 1000;
466 u32 reg;
467
468 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
469 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
470 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
471
472 do {
473 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
474 if (!(reg & SOFTWARE_RESET))
475 break;
476
477 usleep_range(1000, 2000);
478 } while (timeout-- > 0);
479
480 if (timeout == 0)
481 return -ETIMEDOUT;
482
483 return 0;
484}
485
Florian Fainelli691c9a82015-01-20 16:42:00 -0800486static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
487{
Florian Fainellif01d5982016-08-25 15:23:41 -0700488 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800489 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700490 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800491 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800492}
493
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700494static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
495 struct device_node *dn)
496{
497 struct device_node *port;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700498 int mode;
499 unsigned int port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700500
501 priv->moca_port = -1;
502
503 for_each_available_child_of_node(dn, port) {
504 if (of_property_read_u32(port, "reg", &port_num))
505 continue;
506
507 /* Internal PHYs get assigned a specific 'phy-mode' property
508 * value: "internal" to help flag them before MDIO probing
509 * has completed, since they might be turned off at that
510 * time
511 */
512 mode = of_get_phy_mode(port);
Florian Fainellibedd00c2017-06-23 10:33:16 -0700513 if (mode < 0)
514 continue;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700515
Florian Fainellibedd00c2017-06-23 10:33:16 -0700516 if (mode == PHY_INTERFACE_MODE_INTERNAL)
517 priv->int_phy_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700518
519 if (mode == PHY_INTERFACE_MODE_MOCA)
520 priv->moca_port = port_num;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800521
522 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
523 priv->brcm_tag_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700524 }
525}
526
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700527static int bcm_sf2_mdio_register(struct dsa_switch *ds)
528{
Florian Fainellif4589952016-08-26 12:18:33 -0700529 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700530 struct device_node *dn;
531 static int index;
532 int err;
533
534 /* Find our integrated MDIO bus node */
535 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
536 priv->master_mii_bus = of_mdio_find_bus(dn);
537 if (!priv->master_mii_bus)
538 return -EPROBE_DEFER;
539
540 get_device(&priv->master_mii_bus->dev);
541 priv->master_mii_dn = dn;
542
543 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
544 if (!priv->slave_mii_bus)
545 return -ENOMEM;
546
547 priv->slave_mii_bus->priv = priv;
548 priv->slave_mii_bus->name = "sf2 slave mii";
549 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
550 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
551 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
552 index++);
553 priv->slave_mii_bus->dev.of_node = dn;
554
555 /* Include the pseudo-PHY address to divert reads towards our
556 * workaround. This is only required for 7445D0, since 7445E0
557 * disconnects the internal switch pseudo-PHY such that we can use the
558 * regular SWITCH_MDIO master controller instead.
559 *
560 * Here we flag the pseudo PHY as needing special treatment and would
561 * otherwise make all other PHY read/writes go to the master MDIO bus
562 * controller that comes with this switch backed by the "mdio-unimac"
563 * driver.
564 */
565 if (of_machine_is_compatible("brcm,bcm7445d0"))
566 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
567 else
568 priv->indir_phy_mask = 0;
569
570 ds->phys_mii_mask = priv->indir_phy_mask;
571 ds->slave_mii_bus = priv->slave_mii_bus;
572 priv->slave_mii_bus->parent = ds->dev->parent;
573 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
574
575 if (dn)
576 err = of_mdiobus_register(priv->slave_mii_bus, dn);
577 else
578 err = mdiobus_register(priv->slave_mii_bus);
579
580 if (err)
581 of_node_put(dn);
582
583 return err;
584}
585
586static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
587{
588 mdiobus_unregister(priv->slave_mii_bus);
589 if (priv->master_mii_dn)
590 of_node_put(priv->master_mii_dn);
591}
592
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700593static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
594{
Florian Fainellif4589952016-08-26 12:18:33 -0700595 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700596
597 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
598 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
599 * the REG_PHY_REVISION register layout is.
600 */
601
602 return priv->hw_params.gphy_rev;
603}
604
Florian Fainelli246d7f72014-08-27 17:04:56 -0700605static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
606 struct phy_device *phydev)
607{
Florian Fainellif4589952016-08-26 12:18:33 -0700608 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli76da8702016-11-22 11:40:58 -0800609 struct ethtool_eee *p = &priv->port_sts[port].eee;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700610 u32 id_mode_dis = 0, port_mode;
611 const char *str = NULL;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800612 u32 reg, offset;
613
614 if (priv->type == BCM7445_DEVICE_ID)
615 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
616 else
617 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700618
619 switch (phydev->interface) {
620 case PHY_INTERFACE_MODE_RGMII:
621 str = "RGMII (no delay)";
622 id_mode_dis = 1;
623 case PHY_INTERFACE_MODE_RGMII_TXID:
624 if (!str)
625 str = "RGMII (TX delay)";
626 port_mode = EXT_GPHY;
627 break;
628 case PHY_INTERFACE_MODE_MII:
629 str = "MII";
630 port_mode = EXT_EPHY;
631 break;
632 case PHY_INTERFACE_MODE_REVMII:
633 str = "Reverse MII";
634 port_mode = EXT_REVMII;
635 break;
636 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700637 /* All other PHYs: internal and MoCA */
638 goto force_link;
639 }
640
641 /* If the link is down, just disable the interface to conserve power */
642 if (!phydev->link) {
643 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
644 reg &= ~RGMII_MODE_EN;
645 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700646 goto force_link;
647 }
648
649 /* Clear id_mode_dis bit, and the existing port mode, but
650 * make sure we enable the RGMII block for data to pass
651 */
652 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
653 reg &= ~ID_MODE_DIS;
654 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
655 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
656
657 reg |= port_mode | RGMII_MODE_EN;
658 if (id_mode_dis)
659 reg |= ID_MODE_DIS;
660
661 if (phydev->pause) {
662 if (phydev->asym_pause)
663 reg |= TX_PAUSE_EN;
664 reg |= RX_PAUSE_EN;
665 }
666
667 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
668
669 pr_info("Port %d configured for %s\n", port, str);
670
671force_link:
672 /* Force link settings detected from the PHY */
673 reg = SW_OVERRIDE;
674 switch (phydev->speed) {
675 case SPEED_1000:
676 reg |= SPDSTS_1000 << SPEED_SHIFT;
677 break;
678 case SPEED_100:
679 reg |= SPDSTS_100 << SPEED_SHIFT;
680 break;
681 }
682
683 if (phydev->link)
684 reg |= LINK_STS;
685 if (phydev->duplex == DUPLEX_FULL)
686 reg |= DUPLX_MODE;
687
Florian Fainelli0fe99332017-01-20 12:36:30 -0800688 core_writel(priv, reg, offset);
Florian Fainelli76da8702016-11-22 11:40:58 -0800689
690 if (!phydev->is_pseudo_fixed_link)
691 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700692}
693
694static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
695 struct fixed_phy_status *status)
696{
Florian Fainellif4589952016-08-26 12:18:33 -0700697 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800698 u32 duplex, pause, offset;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700699 u32 reg;
700
Florian Fainelli0fe99332017-01-20 12:36:30 -0800701 if (priv->type == BCM7445_DEVICE_ID)
702 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
703 else
704 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
705
Florian Fainelli246d7f72014-08-27 17:04:56 -0700706 duplex = core_readl(priv, CORE_DUPSTS);
707 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700708
709 status->link = 0;
710
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700711 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700712 * which means that we need to force the link at the port override
713 * level to get the data to flow. We do use what the interrupt handler
714 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -0800715 *
716 * For the other ports, we just force the link status, since this is
717 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700718 */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700719 if (port == priv->moca_port) {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700720 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -0700721 /* For MoCA interfaces, also force a link down notification
722 * since some version of the user-space daemon (mocad) use
723 * cmd->autoneg to force the link, which messes up the PHY
724 * state machine and make it go in PHY_FORCING state instead.
725 */
726 if (!status->link)
Andrew Lunnc8b09802016-06-04 21:16:57 +0200727 netif_carrier_off(ds->ports[port].netdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700728 status->duplex = 1;
729 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -0800730 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700731 status->duplex = !!(duplex & (1 << port));
732 }
733
Florian Fainelli0fe99332017-01-20 12:36:30 -0800734 reg = core_readl(priv, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800735 reg |= SW_OVERRIDE;
736 if (status->link)
737 reg |= LINK_STS;
738 else
739 reg &= ~LINK_STS;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800740 core_writel(priv, reg, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800741
Florian Fainelli246d7f72014-08-27 17:04:56 -0700742 if ((pause & (1 << port)) &&
743 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
744 status->asym_pause = 1;
745 status->pause = 1;
746 }
747
748 if (pause & (1 << port))
749 status->pause = 1;
750}
751
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700752static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
753{
Florian Fainellif4589952016-08-26 12:18:33 -0700754 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700755 unsigned int port;
756
Florian Fainelli691c9a82015-01-20 16:42:00 -0800757 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700758
759 /* Disable all ports physically present including the IMP
760 * port, the other ones have already been disabled during
761 * bcm_sf2_sw_setup
762 */
763 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200764 if ((1 << port) & ds->enabled_port_mask ||
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700765 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700766 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700767 }
768
769 return 0;
770}
771
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700772static int bcm_sf2_sw_resume(struct dsa_switch *ds)
773{
Florian Fainellif4589952016-08-26 12:18:33 -0700774 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700775 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700776 int ret;
777
778 ret = bcm_sf2_sw_rst(priv);
779 if (ret) {
780 pr_err("%s: failed to software reset switch\n", __func__);
781 return ret;
782 }
783
Florian Fainellib0836682015-02-05 11:40:41 -0800784 if (priv->hw_params.num_gphy == 1)
785 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700786
787 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200788 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700789 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700790 else if (dsa_is_cpu_port(ds, port))
791 bcm_sf2_imp_setup(ds, port);
792 }
793
794 return 0;
795}
796
Florian Fainelli96e65d72014-09-18 17:31:25 -0700797static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
798 struct ethtool_wolinfo *wol)
799{
Vivien Didelot3a8f6f82017-08-02 15:48:25 -0400800 struct net_device *p = ds->dst->cpu_dp->netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700801 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700802 struct ethtool_wolinfo pwol;
803
804 /* Get the parent device WoL settings */
805 p->ethtool_ops->get_wol(p, &pwol);
806
807 /* Advertise the parent device supported settings */
808 wol->supported = pwol.supported;
809 memset(&wol->sopass, 0, sizeof(wol->sopass));
810
811 if (pwol.wolopts & WAKE_MAGICSECURE)
812 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
813
814 if (priv->wol_ports_mask & (1 << port))
815 wol->wolopts = pwol.wolopts;
816 else
817 wol->wolopts = 0;
818}
819
820static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
821 struct ethtool_wolinfo *wol)
822{
Vivien Didelot3a8f6f82017-08-02 15:48:25 -0400823 struct net_device *p = ds->dst->cpu_dp->netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700824 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400825 s8 cpu_port = ds->dst->cpu_dp->index;
Florian Fainelli96e65d72014-09-18 17:31:25 -0700826 struct ethtool_wolinfo pwol;
827
828 p->ethtool_ops->get_wol(p, &pwol);
829 if (wol->wolopts & ~pwol.supported)
830 return -EINVAL;
831
832 if (wol->wolopts)
833 priv->wol_ports_mask |= (1 << port);
834 else
835 priv->wol_ports_mask &= ~(1 << port);
836
837 /* If we have at least one port enabled, make sure the CPU port
838 * is also enabled. If the CPU port is the last one enabled, we disable
839 * it since this configuration does not make sense.
840 */
841 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
842 priv->wol_ports_mask |= (1 << cpu_port);
843 else
844 priv->wol_ports_mask &= ~(1 << cpu_port);
845
846 return p->ethtool_ops->set_wol(p, wol);
847}
848
Florian Fainellide0b9d32016-08-26 12:18:34 -0700849static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
Florian Fainelli9c57a772016-06-09 17:42:08 -0700850{
Florian Fainellide0b9d32016-08-26 12:18:34 -0700851 unsigned int timeout = 10;
852 u32 reg;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700853
Florian Fainellide0b9d32016-08-26 12:18:34 -0700854 do {
855 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
856 if (!(reg & ARLA_VTBL_STDN))
857 return 0;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700858
Florian Fainellide0b9d32016-08-26 12:18:34 -0700859 usleep_range(1000, 2000);
860 } while (timeout--);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700861
Florian Fainellide0b9d32016-08-26 12:18:34 -0700862 return -ETIMEDOUT;
863}
Florian Fainelli9c57a772016-06-09 17:42:08 -0700864
Florian Fainellide0b9d32016-08-26 12:18:34 -0700865static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
866{
867 core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
868
869 return bcm_sf2_vlan_op_wait(priv);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700870}
871
872static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
873{
Florian Fainellif4589952016-08-26 12:18:33 -0700874 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700875 unsigned int port;
876
877 /* Clear all VLANs */
878 bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
879
880 for (port = 0; port < priv->hw_params.num_ports; port++) {
881 if (!((1 << port) & ds->enabled_port_mask))
882 continue;
883
884 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
885 }
886}
887
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700888static int bcm_sf2_sw_setup(struct dsa_switch *ds)
889{
Florian Fainellif4589952016-08-26 12:18:33 -0700890 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700891 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700892
Florian Fainellie85ec742017-09-19 10:46:46 -0700893 /* Disable unused ports and configure IMP port */
Florian Fainellid9338022016-08-18 15:30:14 -0700894 for (port = 0; port < priv->hw_params.num_ports; port++) {
Florian Fainellie85ec742017-09-19 10:46:46 -0700895 if (dsa_is_cpu_port(ds, port))
Florian Fainellid9338022016-08-18 15:30:14 -0700896 bcm_sf2_imp_setup(ds, port);
Florian Fainellie85ec742017-09-19 10:46:46 -0700897 else if (!((1 << port) & ds->enabled_port_mask))
Florian Fainellid9338022016-08-18 15:30:14 -0700898 bcm_sf2_port_disable(ds, port, NULL);
899 }
900
901 bcm_sf2_sw_configure_vlan(ds);
902
903 return 0;
904}
905
Florian Fainellif4589952016-08-26 12:18:33 -0700906/* The SWITCH_CORE register space is managed by b53 but operates on a page +
907 * register basis so we need to translate that into an address that the
908 * bus-glue understands.
909 */
910#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
911
912static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
913 u8 *val)
914{
915 struct bcm_sf2_priv *priv = dev->priv;
916
917 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
918
919 return 0;
920}
921
922static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
923 u16 *val)
924{
925 struct bcm_sf2_priv *priv = dev->priv;
926
927 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
928
929 return 0;
930}
931
932static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
933 u32 *val)
934{
935 struct bcm_sf2_priv *priv = dev->priv;
936
937 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
938
939 return 0;
940}
941
942static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
943 u64 *val)
944{
945 struct bcm_sf2_priv *priv = dev->priv;
946
947 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
948
949 return 0;
950}
951
952static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
953 u8 value)
954{
955 struct bcm_sf2_priv *priv = dev->priv;
956
957 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
958
959 return 0;
960}
961
962static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
963 u16 value)
964{
965 struct bcm_sf2_priv *priv = dev->priv;
966
967 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
968
969 return 0;
970}
971
972static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
973 u32 value)
974{
975 struct bcm_sf2_priv *priv = dev->priv;
976
977 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
978
979 return 0;
980}
981
982static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
983 u64 value)
984{
985 struct bcm_sf2_priv *priv = dev->priv;
986
987 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
988
989 return 0;
990}
991
Bhumika Goyal7e3108f2017-08-29 22:17:52 +0530992static const struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -0700993 .read8 = bcm_sf2_core_read8,
994 .read16 = bcm_sf2_core_read16,
995 .read32 = bcm_sf2_core_read32,
996 .read48 = bcm_sf2_core_read64,
997 .read64 = bcm_sf2_core_read64,
998 .write8 = bcm_sf2_core_write8,
999 .write16 = bcm_sf2_core_write16,
1000 .write32 = bcm_sf2_core_write32,
1001 .write48 = bcm_sf2_core_write64,
1002 .write64 = bcm_sf2_core_write64,
1003};
1004
Florian Fainellia82f67a2017-01-08 14:52:08 -08001005static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli73095cb2017-01-08 14:52:06 -08001006 .get_tag_protocol = bcm_sf2_sw_get_tag_protocol,
1007 .setup = bcm_sf2_sw_setup,
1008 .get_strings = b53_get_strings,
1009 .get_ethtool_stats = b53_get_ethtool_stats,
1010 .get_sset_count = b53_get_sset_count,
1011 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
1012 .adjust_link = bcm_sf2_sw_adjust_link,
1013 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
1014 .suspend = bcm_sf2_sw_suspend,
1015 .resume = bcm_sf2_sw_resume,
1016 .get_wol = bcm_sf2_sw_get_wol,
1017 .set_wol = bcm_sf2_sw_set_wol,
1018 .port_enable = bcm_sf2_port_setup,
1019 .port_disable = bcm_sf2_port_disable,
Vivien Didelot08f50062017-08-01 16:32:41 -04001020 .get_mac_eee = bcm_sf2_sw_get_mac_eee,
1021 .set_mac_eee = bcm_sf2_sw_set_mac_eee,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001022 .port_bridge_join = b53_br_join,
1023 .port_bridge_leave = b53_br_leave,
1024 .port_stp_state_set = b53_br_set_stp_state,
1025 .port_fast_age = b53_br_fast_age,
1026 .port_vlan_filtering = b53_vlan_filtering,
1027 .port_vlan_prepare = b53_vlan_prepare,
1028 .port_vlan_add = b53_vlan_add,
1029 .port_vlan_del = b53_vlan_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001030 .port_fdb_dump = b53_fdb_dump,
1031 .port_fdb_add = b53_fdb_add,
1032 .port_fdb_del = b53_fdb_del,
Florian Fainelli73181662017-01-30 09:48:43 -08001033 .get_rxnfc = bcm_sf2_get_rxnfc,
1034 .set_rxnfc = bcm_sf2_set_rxnfc,
Florian Fainelliec960de2017-01-30 12:41:43 -08001035 .port_mirror_add = b53_mirror_add,
1036 .port_mirror_del = b53_mirror_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001037};
1038
Florian Fainellia78e86e2017-01-20 12:36:29 -08001039struct bcm_sf2_of_data {
1040 u32 type;
1041 const u16 *reg_offsets;
1042 unsigned int core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001043 unsigned int num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001044};
1045
1046/* Register offsets for the SWITCH_REG_* block */
1047static const u16 bcm_sf2_7445_reg_offsets[] = {
1048 [REG_SWITCH_CNTRL] = 0x00,
1049 [REG_SWITCH_STATUS] = 0x04,
1050 [REG_DIR_DATA_WRITE] = 0x08,
1051 [REG_DIR_DATA_READ] = 0x0C,
1052 [REG_SWITCH_REVISION] = 0x18,
1053 [REG_PHY_REVISION] = 0x1C,
1054 [REG_SPHY_CNTRL] = 0x2C,
1055 [REG_RGMII_0_CNTRL] = 0x34,
1056 [REG_RGMII_1_CNTRL] = 0x40,
1057 [REG_RGMII_2_CNTRL] = 0x4c,
1058 [REG_LED_0_CNTRL] = 0x90,
1059 [REG_LED_1_CNTRL] = 0x94,
1060 [REG_LED_2_CNTRL] = 0x98,
1061};
1062
1063static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1064 .type = BCM7445_DEVICE_ID,
1065 .core_reg_align = 0,
1066 .reg_offsets = bcm_sf2_7445_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001067 .num_cfp_rules = 256,
Florian Fainellia78e86e2017-01-20 12:36:29 -08001068};
1069
Florian Fainelli0fe99332017-01-20 12:36:30 -08001070static const u16 bcm_sf2_7278_reg_offsets[] = {
1071 [REG_SWITCH_CNTRL] = 0x00,
1072 [REG_SWITCH_STATUS] = 0x04,
1073 [REG_DIR_DATA_WRITE] = 0x08,
1074 [REG_DIR_DATA_READ] = 0x0c,
1075 [REG_SWITCH_REVISION] = 0x10,
1076 [REG_PHY_REVISION] = 0x14,
1077 [REG_SPHY_CNTRL] = 0x24,
1078 [REG_RGMII_0_CNTRL] = 0xe0,
1079 [REG_RGMII_1_CNTRL] = 0xec,
1080 [REG_RGMII_2_CNTRL] = 0xf8,
1081 [REG_LED_0_CNTRL] = 0x40,
1082 [REG_LED_1_CNTRL] = 0x4c,
1083 [REG_LED_2_CNTRL] = 0x58,
1084};
1085
1086static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1087 .type = BCM7278_DEVICE_ID,
1088 .core_reg_align = 1,
1089 .reg_offsets = bcm_sf2_7278_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001090 .num_cfp_rules = 128,
Florian Fainelli0fe99332017-01-20 12:36:30 -08001091};
1092
Florian Fainellia78e86e2017-01-20 12:36:29 -08001093static const struct of_device_id bcm_sf2_of_match[] = {
1094 { .compatible = "brcm,bcm7445-switch-v4.0",
1095 .data = &bcm_sf2_7445_data
1096 },
Florian Fainelli0fe99332017-01-20 12:36:30 -08001097 { .compatible = "brcm,bcm7278-switch-v4.0",
1098 .data = &bcm_sf2_7278_data
1099 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001100 { /* sentinel */ },
1101};
1102MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1103
Florian Fainellid9338022016-08-18 15:30:14 -07001104static int bcm_sf2_sw_probe(struct platform_device *pdev)
1105{
1106 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1107 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001108 const struct of_device_id *of_id = NULL;
1109 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001110 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001111 struct dsa_switch_ops *ops;
Florian Fainellid9338022016-08-18 15:30:14 -07001112 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001113 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001114 struct dsa_switch *ds;
1115 void __iomem **base;
Florian Fainelli4bd11672016-08-18 15:30:15 -07001116 struct resource *r;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001117 unsigned int i;
1118 u32 reg, rev;
1119 int ret;
1120
Florian Fainellif4589952016-08-26 12:18:33 -07001121 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1122 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001123 return -ENOMEM;
1124
Florian Fainellia4c61b92017-01-07 21:01:56 -08001125 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1126 if (!ops)
1127 return -ENOMEM;
1128
Florian Fainellif4589952016-08-26 12:18:33 -07001129 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1130 if (!dev)
1131 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001132
Florian Fainellif4589952016-08-26 12:18:33 -07001133 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1134 if (!pdata)
1135 return -ENOMEM;
1136
Florian Fainellia78e86e2017-01-20 12:36:29 -08001137 of_id = of_match_node(bcm_sf2_of_match, dn);
1138 if (!of_id || !of_id->data)
1139 return -EINVAL;
1140
1141 data = of_id->data;
1142
1143 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1144 priv->type = data->type;
1145 priv->reg_offsets = data->reg_offsets;
1146 priv->core_reg_align = data->core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001147 priv->num_cfp_rules = data->num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001148
Florian Fainellif4589952016-08-26 12:18:33 -07001149 /* Auto-detection using standard registers will not work, so
1150 * provide an indication of what kind of device we are for
1151 * b53_common to work with
1152 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001153 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001154 dev->pdata = pdata;
1155
1156 priv->dev = dev;
1157 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001158 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001159
Florian Fainelli181183772017-09-03 20:27:02 -07001160 /* Advertise the 8 egress queues */
1161 ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1162
Florian Fainellif4589952016-08-26 12:18:33 -07001163 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001164
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001165 spin_lock_init(&priv->indir_lock);
1166 mutex_init(&priv->stats_mutex);
Florian Fainelli73181662017-01-30 09:48:43 -08001167 mutex_init(&priv->cfp.lock);
1168
1169 /* CFP rule #0 cannot be used for specific classifications, flag it as
1170 * permanently used
1171 */
1172 set_bit(0, priv->cfp.used);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001173
Florian Fainellid9338022016-08-18 15:30:14 -07001174 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001175
1176 priv->irq0 = irq_of_parse_and_map(dn, 0);
1177 priv->irq1 = irq_of_parse_and_map(dn, 1);
1178
1179 base = &priv->core;
1180 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainelli4bd11672016-08-18 15:30:15 -07001181 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1182 *base = devm_ioremap_resource(&pdev->dev, r);
1183 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001184 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001185 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001186 }
1187 base++;
1188 }
1189
1190 ret = bcm_sf2_sw_rst(priv);
1191 if (ret) {
1192 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001193 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001194 }
1195
1196 ret = bcm_sf2_mdio_register(ds);
1197 if (ret) {
1198 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001199 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001200 }
1201
Florian Fainelli73181662017-01-30 09:48:43 -08001202 ret = bcm_sf2_cfp_rst(priv);
1203 if (ret) {
1204 pr_err("failed to reset CFP\n");
1205 goto out_mdio;
1206 }
1207
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001208 /* Disable all interrupts and request them */
1209 bcm_sf2_intr_disable(priv);
1210
Florian Fainelli4bd11672016-08-18 15:30:15 -07001211 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1212 "switch_0", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001213 if (ret < 0) {
1214 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001215 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001216 }
1217
Florian Fainelli4bd11672016-08-18 15:30:15 -07001218 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1219 "switch_1", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001220 if (ret < 0) {
1221 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001222 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001223 }
1224
1225 /* Reset the MIB counters */
1226 reg = core_readl(priv, CORE_GMNCFGCFG);
1227 reg |= RST_MIB_CNT;
1228 core_writel(priv, reg, CORE_GMNCFGCFG);
1229 reg &= ~RST_MIB_CNT;
1230 core_writel(priv, reg, CORE_GMNCFGCFG);
1231
1232 /* Get the maximum number of ports for this switch */
1233 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1234 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1235 priv->hw_params.num_ports = DSA_MAX_PORTS;
1236
1237 /* Assume a single GPHY setup if we can't read that property */
1238 if (of_property_read_u32(dn, "brcm,num-gphy",
1239 &priv->hw_params.num_gphy))
1240 priv->hw_params.num_gphy = 1;
1241
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001242 rev = reg_readl(priv, REG_SWITCH_REVISION);
1243 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1244 SWITCH_TOP_REV_MASK;
1245 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1246
1247 rev = reg_readl(priv, REG_PHY_REVISION);
1248 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1249
Florian Fainellif4589952016-08-26 12:18:33 -07001250 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001251 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001252 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001253
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001254 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1255 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1256 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1257 priv->core, priv->irq0, priv->irq1);
1258
1259 return 0;
1260
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001261out_mdio:
1262 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001263 return ret;
1264}
1265
Florian Fainellid9338022016-08-18 15:30:14 -07001266static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001267{
Florian Fainellif4589952016-08-26 12:18:33 -07001268 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001269
1270 /* Disable all ports and interrupts */
1271 priv->wol_ports_mask = 0;
Florian Fainellif4589952016-08-26 12:18:33 -07001272 bcm_sf2_sw_suspend(priv->dev->ds);
1273 dsa_unregister_switch(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001274 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001275
1276 return 0;
1277}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001278
Florian Fainelli2399d612016-10-20 09:32:19 -07001279static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1280{
1281 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1282
1283 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1284 * successful MDIO bus scan to occur. If we did turn off the GPHY
1285 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001286 *
1287 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001288 */
1289 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001290 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001291}
1292
Florian Fainellid9338022016-08-18 15:30:14 -07001293#ifdef CONFIG_PM_SLEEP
1294static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001295{
Florian Fainellid9338022016-08-18 15:30:14 -07001296 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001297 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001298
Florian Fainellif4589952016-08-26 12:18:33 -07001299 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001300}
Florian Fainellid9338022016-08-18 15:30:14 -07001301
1302static int bcm_sf2_resume(struct device *dev)
1303{
1304 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001305 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001306
Florian Fainellif4589952016-08-26 12:18:33 -07001307 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001308}
1309#endif /* CONFIG_PM_SLEEP */
1310
1311static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1312 bcm_sf2_suspend, bcm_sf2_resume);
1313
Florian Fainellid9338022016-08-18 15:30:14 -07001314
1315static struct platform_driver bcm_sf2_driver = {
1316 .probe = bcm_sf2_sw_probe,
1317 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001318 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001319 .driver = {
1320 .name = "brcm-sf2",
1321 .of_match_table = bcm_sf2_of_match,
1322 .pm = &bcm_sf2_pm_ops,
1323 },
1324};
1325module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001326
1327MODULE_AUTHOR("Broadcom Corporation");
1328MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1329MODULE_LICENSE("GPL");
1330MODULE_ALIAS("platform:brcm-sf2");