blob: 6b184bafa235ba9be9f8c8561932a6074db5839d [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);
106 u32 reg, offset;
107
108 if (priv->type == BCM7445_DEVICE_ID)
109 offset = CORE_STS_OVERRIDE_IMP;
110 else
111 offset = CORE_STS_OVERRIDE_IMP2;
112
113 /* Enable the port memories */
114 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
115 reg &= ~P_TXQ_PSM_VDD(port);
116 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
117
118 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
119 reg = core_readl(priv, CORE_IMP_CTL);
120 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
121 reg &= ~(RX_DIS | TX_DIS);
122 core_writel(priv, reg, CORE_IMP_CTL);
123
124 /* Enable forwarding */
125 core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
126
127 /* Enable IMP port in dumb mode */
128 reg = core_readl(priv, CORE_SWITCH_CTRL);
129 reg |= MII_DUMB_FWDG_EN;
130 core_writel(priv, reg, CORE_SWITCH_CTRL);
131
132 bcm_sf2_brcm_hdr_setup(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700133
134 /* Force link status for IMP port */
Florian Fainelli0fe99332017-01-20 12:36:30 -0800135 reg = core_readl(priv, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700136 reg |= (MII_SW_OR | LINK_STS);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800137 core_writel(priv, reg, offset);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700138}
139
Florian Fainelli450b05c2014-09-24 17:05:22 -0700140static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
141{
Florian Fainellif4589952016-08-26 12:18:33 -0700142 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700143 u32 reg;
144
145 reg = core_readl(priv, CORE_EEE_EN_CTRL);
146 if (enable)
147 reg |= 1 << port;
148 else
149 reg &= ~(1 << port);
150 core_writel(priv, reg, CORE_EEE_EN_CTRL);
151}
152
Florian Fainellib0836682015-02-05 11:40:41 -0800153static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
154{
Florian Fainellif4589952016-08-26 12:18:33 -0700155 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainellib0836682015-02-05 11:40:41 -0800156 u32 reg;
157
Florian Fainelli9af197a2015-02-05 11:40:42 -0800158 reg = reg_readl(priv, REG_SPHY_CNTRL);
159 if (enable) {
160 reg |= PHY_RESET;
161 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | CK25_DIS);
162 reg_writel(priv, reg, REG_SPHY_CNTRL);
163 udelay(21);
164 reg = reg_readl(priv, REG_SPHY_CNTRL);
165 reg &= ~PHY_RESET;
166 } else {
167 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
168 reg_writel(priv, reg, REG_SPHY_CNTRL);
169 mdelay(1);
170 reg |= CK25_DIS;
171 }
172 reg_writel(priv, reg, REG_SPHY_CNTRL);
Florian Fainellib0836682015-02-05 11:40:41 -0800173
Florian Fainelli9af197a2015-02-05 11:40:42 -0800174 /* Use PHY-driven LED signaling */
175 if (!enable) {
176 reg = reg_readl(priv, REG_LED_CNTRL(0));
177 reg |= SPDLNK_SRC_SEL;
178 reg_writel(priv, reg, REG_LED_CNTRL(0));
179 }
Florian Fainellib0836682015-02-05 11:40:41 -0800180}
181
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700182static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
183 int port)
184{
185 unsigned int off;
186
187 switch (port) {
188 case 7:
189 off = P7_IRQ_OFF;
190 break;
191 case 0:
192 /* Port 0 interrupts are located on the first bank */
193 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
194 return;
195 default:
196 off = P_IRQ_OFF(port);
197 break;
198 }
199
200 intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
201}
202
203static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
204 int port)
205{
206 unsigned int off;
207
208 switch (port) {
209 case 7:
210 off = P7_IRQ_OFF;
211 break;
212 case 0:
213 /* Port 0 interrupts are located on the first bank */
214 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
215 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
216 return;
217 default:
218 off = P_IRQ_OFF(port);
219 break;
220 }
221
222 intrl2_1_mask_set(priv, P_IRQ_MASK(off));
223 intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
224}
225
Florian Fainellib6d045d2014-09-24 17:05:20 -0700226static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
227 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700228{
Florian Fainellif4589952016-08-26 12:18:33 -0700229 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400230 s8 cpu_port = ds->dst->cpu_dp->index;
Florian Fainellie1b91472017-01-30 09:48:41 -0800231 unsigned int i;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700232 u32 reg;
233
234 /* Clear the memory power down */
235 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
236 reg &= ~P_TXQ_PSM_VDD(port);
237 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
238
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800239 /* Enable Broadcom tags for that port if requested */
240 if (priv->brcm_tag_mask & BIT(port))
241 bcm_sf2_brcm_hdr_setup(priv, port);
242
Florian Fainellie1b91472017-01-30 09:48:41 -0800243 /* Configure Traffic Class to QoS mapping, allow each priority to map
244 * to a different queue number
245 */
246 reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
Florian Fainelli181183772017-09-03 20:27:02 -0700247 for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
Florian Fainellie1b91472017-01-30 09:48:41 -0800248 reg |= i << (PRT_TO_QID_SHIFT * i);
249 core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
250
Florian Fainelli246d7f72014-08-27 17:04:56 -0700251 /* Clear the Rx and Tx disable bits and set to no spanning tree */
252 core_writel(priv, 0, CORE_G_PCTL_PORT(port));
253
Florian Fainelli9af197a2015-02-05 11:40:42 -0800254 /* Re-enable the GPHY and re-apply workarounds */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700255 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
Florian Fainelli9af197a2015-02-05 11:40:42 -0800256 bcm_sf2_gphy_enable_set(ds, true);
257 if (phy) {
258 /* if phy_stop() has been called before, phy
259 * will be in halted state, and phy_start()
260 * will call resume.
261 *
262 * the resume path does not configure back
263 * autoneg settings, and since we hard reset
264 * the phy manually here, we need to reset the
265 * state machine also.
266 */
267 phy->state = PHY_READY;
268 phy_init_hw(phy);
269 }
270 }
271
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700272 /* Enable MoCA port interrupts to get notified */
273 if (port == priv->moca_port)
274 bcm_sf2_port_intr_enable(priv, port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700275
Florian Fainelli12f460f2015-02-24 13:15:34 -0800276 /* Set this port, and only this one to be in the default VLAN,
277 * if member of a bridge, restore its membership prior to
278 * bringing down this port.
279 */
Florian Fainelli246d7f72014-08-27 17:04:56 -0700280 reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
281 reg &= ~PORT_VLAN_CTRL_MASK;
282 reg |= (1 << port);
Florian Fainelli02154922016-09-10 12:39:03 -0700283 reg |= priv->dev->ports[port].vlan_ctl_mask;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700284 core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
Florian Fainellib6d045d2014-09-24 17:05:20 -0700285
286 bcm_sf2_imp_vlan_setup(ds, cpu_port);
287
Florian Fainelli450b05c2014-09-24 17:05:22 -0700288 /* If EEE was enabled, restore it */
289 if (priv->port_sts[port].eee.eee_enabled)
290 bcm_sf2_eee_enable_set(ds, port, true);
291
Florian Fainellib6d045d2014-09-24 17:05:20 -0700292 return 0;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700293}
294
Florian Fainellib6d045d2014-09-24 17:05:20 -0700295static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
296 struct phy_device *phy)
Florian Fainelli246d7f72014-08-27 17:04:56 -0700297{
Florian Fainellif4589952016-08-26 12:18:33 -0700298 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700299 u32 off, reg;
300
Florian Fainelli96e65d72014-09-18 17:31:25 -0700301 if (priv->wol_ports_mask & (1 << port))
302 return;
303
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700304 if (port == priv->moca_port)
305 bcm_sf2_port_intr_disable(priv, port);
Florian Fainellib6d045d2014-09-24 17:05:20 -0700306
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700307 if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
Florian Fainelli9af197a2015-02-05 11:40:42 -0800308 bcm_sf2_gphy_enable_set(ds, false);
309
Florian Fainelli246d7f72014-08-27 17:04:56 -0700310 if (dsa_is_cpu_port(ds, port))
311 off = CORE_IMP_CTL;
312 else
313 off = CORE_G_PCTL_PORT(port);
314
315 reg = core_readl(priv, off);
316 reg |= RX_DIS | TX_DIS;
317 core_writel(priv, reg, off);
318
319 /* Power down the port memory */
320 reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
321 reg |= P_TXQ_PSM_VDD(port);
322 core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
323}
324
Florian Fainelli450b05c2014-09-24 17:05:22 -0700325/* Returns 0 if EEE was not enabled, or 1 otherwise
326 */
327static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
328 struct phy_device *phy)
329{
Florian Fainelli450b05c2014-09-24 17:05:22 -0700330 int ret;
331
Florian Fainelli450b05c2014-09-24 17:05:22 -0700332 ret = phy_init_eee(phy, 0);
333 if (ret)
334 return 0;
335
336 bcm_sf2_eee_enable_set(ds, port, true);
337
338 return 1;
339}
340
Vivien Didelot08f50062017-08-01 16:32:41 -0400341static int bcm_sf2_sw_get_mac_eee(struct dsa_switch *ds, int port,
342 struct ethtool_eee *e)
Florian Fainelli450b05c2014-09-24 17:05:22 -0700343{
Florian Fainellif4589952016-08-26 12:18:33 -0700344 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700345 struct ethtool_eee *p = &priv->port_sts[port].eee;
346 u32 reg;
347
348 reg = core_readl(priv, CORE_EEE_LPI_INDICATE);
349 e->eee_enabled = p->eee_enabled;
350 e->eee_active = !!(reg & (1 << port));
351
352 return 0;
353}
354
Vivien Didelot08f50062017-08-01 16:32:41 -0400355static int bcm_sf2_sw_set_mac_eee(struct dsa_switch *ds, int port,
356 struct ethtool_eee *e)
Florian Fainelli450b05c2014-09-24 17:05:22 -0700357{
Florian Fainellif4589952016-08-26 12:18:33 -0700358 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700359 struct ethtool_eee *p = &priv->port_sts[port].eee;
360
361 p->eee_enabled = e->eee_enabled;
Vivien Didelotc48f7eb2017-08-01 16:32:38 -0400362 bcm_sf2_eee_enable_set(ds, port, e->eee_enabled);
Florian Fainelli450b05c2014-09-24 17:05:22 -0700363
364 return 0;
365}
366
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700367static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
368 int regnum, u16 val)
369{
370 int ret = 0;
371 u32 reg;
372
373 reg = reg_readl(priv, REG_SWITCH_CNTRL);
374 reg |= MDIO_MASTER_SEL;
375 reg_writel(priv, reg, REG_SWITCH_CNTRL);
376
377 /* Page << 8 | offset */
378 reg = 0x70;
379 reg <<= 2;
380 core_writel(priv, addr, reg);
381
382 /* Page << 8 | offset */
383 reg = 0x80 << 8 | regnum << 1;
384 reg <<= 2;
385
386 if (op)
387 ret = core_readl(priv, reg);
388 else
389 core_writel(priv, val, reg);
390
391 reg = reg_readl(priv, REG_SWITCH_CNTRL);
392 reg &= ~MDIO_MASTER_SEL;
393 reg_writel(priv, reg, REG_SWITCH_CNTRL);
394
395 return ret & 0xffff;
396}
397
398static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
399{
400 struct bcm_sf2_priv *priv = bus->priv;
401
402 /* Intercept reads from Broadcom pseudo-PHY address, else, send
403 * them to our master MDIO bus controller
404 */
405 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
406 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
407 else
Florian Fainelli2cfe8f82017-01-07 21:01:57 -0800408 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700409}
410
411static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
412 u16 val)
413{
414 struct bcm_sf2_priv *priv = bus->priv;
415
416 /* Intercept writes to the Broadcom pseudo-PHY address, else,
417 * send them to our master MDIO bus controller
418 */
419 if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
420 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
421 else
Florian Fainelli2cfe8f82017-01-07 21:01:57 -0800422 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700423
424 return 0;
425}
426
Florian Fainelli246d7f72014-08-27 17:04:56 -0700427static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
428{
429 struct bcm_sf2_priv *priv = dev_id;
430
431 priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
432 ~priv->irq0_mask;
433 intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
434
435 return IRQ_HANDLED;
436}
437
438static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
439{
440 struct bcm_sf2_priv *priv = dev_id;
441
442 priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
443 ~priv->irq1_mask;
444 intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
445
446 if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
447 priv->port_sts[7].link = 1;
448 if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
449 priv->port_sts[7].link = 0;
450
451 return IRQ_HANDLED;
452}
453
Florian Fainelli33f84612014-11-25 18:08:49 -0800454static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
455{
456 unsigned int timeout = 1000;
457 u32 reg;
458
459 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
460 reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
461 core_writel(priv, reg, CORE_WATCHDOG_CTRL);
462
463 do {
464 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
465 if (!(reg & SOFTWARE_RESET))
466 break;
467
468 usleep_range(1000, 2000);
469 } while (timeout-- > 0);
470
471 if (timeout == 0)
472 return -ETIMEDOUT;
473
474 return 0;
475}
476
Florian Fainelli691c9a82015-01-20 16:42:00 -0800477static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
478{
Florian Fainellif01d5982016-08-25 15:23:41 -0700479 intrl2_0_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800480 intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainellif01d5982016-08-25 15:23:41 -0700481 intrl2_1_mask_set(priv, 0xffffffff);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800482 intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
Florian Fainelli691c9a82015-01-20 16:42:00 -0800483}
484
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700485static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
486 struct device_node *dn)
487{
488 struct device_node *port;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700489 int mode;
490 unsigned int port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700491
492 priv->moca_port = -1;
493
494 for_each_available_child_of_node(dn, port) {
495 if (of_property_read_u32(port, "reg", &port_num))
496 continue;
497
498 /* Internal PHYs get assigned a specific 'phy-mode' property
499 * value: "internal" to help flag them before MDIO probing
500 * has completed, since they might be turned off at that
501 * time
502 */
503 mode = of_get_phy_mode(port);
Florian Fainellibedd00c2017-06-23 10:33:16 -0700504 if (mode < 0)
505 continue;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700506
Florian Fainellibedd00c2017-06-23 10:33:16 -0700507 if (mode == PHY_INTERFACE_MODE_INTERNAL)
508 priv->int_phy_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700509
510 if (mode == PHY_INTERFACE_MODE_MOCA)
511 priv->moca_port = port_num;
Florian Fainelli64ff2ae2017-01-20 12:36:32 -0800512
513 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
514 priv->brcm_tag_mask |= 1 << port_num;
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700515 }
516}
517
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700518static int bcm_sf2_mdio_register(struct dsa_switch *ds)
519{
Florian Fainellif4589952016-08-26 12:18:33 -0700520 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli461cd1b02016-06-07 16:32:43 -0700521 struct device_node *dn;
522 static int index;
523 int err;
524
525 /* Find our integrated MDIO bus node */
526 dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
527 priv->master_mii_bus = of_mdio_find_bus(dn);
528 if (!priv->master_mii_bus)
529 return -EPROBE_DEFER;
530
531 get_device(&priv->master_mii_bus->dev);
532 priv->master_mii_dn = dn;
533
534 priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
535 if (!priv->slave_mii_bus)
536 return -ENOMEM;
537
538 priv->slave_mii_bus->priv = priv;
539 priv->slave_mii_bus->name = "sf2 slave mii";
540 priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
541 priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
542 snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
543 index++);
544 priv->slave_mii_bus->dev.of_node = dn;
545
546 /* Include the pseudo-PHY address to divert reads towards our
547 * workaround. This is only required for 7445D0, since 7445E0
548 * disconnects the internal switch pseudo-PHY such that we can use the
549 * regular SWITCH_MDIO master controller instead.
550 *
551 * Here we flag the pseudo PHY as needing special treatment and would
552 * otherwise make all other PHY read/writes go to the master MDIO bus
553 * controller that comes with this switch backed by the "mdio-unimac"
554 * driver.
555 */
556 if (of_machine_is_compatible("brcm,bcm7445d0"))
557 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
558 else
559 priv->indir_phy_mask = 0;
560
561 ds->phys_mii_mask = priv->indir_phy_mask;
562 ds->slave_mii_bus = priv->slave_mii_bus;
563 priv->slave_mii_bus->parent = ds->dev->parent;
564 priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
565
566 if (dn)
567 err = of_mdiobus_register(priv->slave_mii_bus, dn);
568 else
569 err = mdiobus_register(priv->slave_mii_bus);
570
571 if (err)
572 of_node_put(dn);
573
574 return err;
575}
576
577static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
578{
579 mdiobus_unregister(priv->slave_mii_bus);
580 if (priv->master_mii_dn)
581 of_node_put(priv->master_mii_dn);
582}
583
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700584static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
585{
Florian Fainellif4589952016-08-26 12:18:33 -0700586 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelliaa9aef72014-09-19 13:07:55 -0700587
588 /* The BCM7xxx PHY driver expects to find the integrated PHY revision
589 * in bits 15:8 and the patch level in bits 7:0 which is exactly what
590 * the REG_PHY_REVISION register layout is.
591 */
592
593 return priv->hw_params.gphy_rev;
594}
595
Florian Fainelli246d7f72014-08-27 17:04:56 -0700596static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
597 struct phy_device *phydev)
598{
Florian Fainellif4589952016-08-26 12:18:33 -0700599 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli76da8702016-11-22 11:40:58 -0800600 struct ethtool_eee *p = &priv->port_sts[port].eee;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700601 u32 id_mode_dis = 0, port_mode;
602 const char *str = NULL;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800603 u32 reg, offset;
604
605 if (priv->type == BCM7445_DEVICE_ID)
606 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
607 else
608 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700609
610 switch (phydev->interface) {
611 case PHY_INTERFACE_MODE_RGMII:
612 str = "RGMII (no delay)";
613 id_mode_dis = 1;
614 case PHY_INTERFACE_MODE_RGMII_TXID:
615 if (!str)
616 str = "RGMII (TX delay)";
617 port_mode = EXT_GPHY;
618 break;
619 case PHY_INTERFACE_MODE_MII:
620 str = "MII";
621 port_mode = EXT_EPHY;
622 break;
623 case PHY_INTERFACE_MODE_REVMII:
624 str = "Reverse MII";
625 port_mode = EXT_REVMII;
626 break;
627 default:
Florian Fainelli7de15572014-09-24 17:05:19 -0700628 /* All other PHYs: internal and MoCA */
629 goto force_link;
630 }
631
632 /* If the link is down, just disable the interface to conserve power */
633 if (!phydev->link) {
634 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
635 reg &= ~RGMII_MODE_EN;
636 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
Florian Fainelli246d7f72014-08-27 17:04:56 -0700637 goto force_link;
638 }
639
640 /* Clear id_mode_dis bit, and the existing port mode, but
641 * make sure we enable the RGMII block for data to pass
642 */
643 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
644 reg &= ~ID_MODE_DIS;
645 reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
646 reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
647
648 reg |= port_mode | RGMII_MODE_EN;
649 if (id_mode_dis)
650 reg |= ID_MODE_DIS;
651
652 if (phydev->pause) {
653 if (phydev->asym_pause)
654 reg |= TX_PAUSE_EN;
655 reg |= RX_PAUSE_EN;
656 }
657
658 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
659
660 pr_info("Port %d configured for %s\n", port, str);
661
662force_link:
663 /* Force link settings detected from the PHY */
664 reg = SW_OVERRIDE;
665 switch (phydev->speed) {
666 case SPEED_1000:
667 reg |= SPDSTS_1000 << SPEED_SHIFT;
668 break;
669 case SPEED_100:
670 reg |= SPDSTS_100 << SPEED_SHIFT;
671 break;
672 }
673
674 if (phydev->link)
675 reg |= LINK_STS;
676 if (phydev->duplex == DUPLEX_FULL)
677 reg |= DUPLX_MODE;
678
Florian Fainelli0fe99332017-01-20 12:36:30 -0800679 core_writel(priv, reg, offset);
Florian Fainelli76da8702016-11-22 11:40:58 -0800680
681 if (!phydev->is_pseudo_fixed_link)
682 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700683}
684
685static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
686 struct fixed_phy_status *status)
687{
Florian Fainellif4589952016-08-26 12:18:33 -0700688 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli0fe99332017-01-20 12:36:30 -0800689 u32 duplex, pause, offset;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700690 u32 reg;
691
Florian Fainelli0fe99332017-01-20 12:36:30 -0800692 if (priv->type == BCM7445_DEVICE_ID)
693 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
694 else
695 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
696
Florian Fainelli246d7f72014-08-27 17:04:56 -0700697 duplex = core_readl(priv, CORE_DUPSTS);
698 pause = core_readl(priv, CORE_PAUSESTS);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700699
700 status->link = 0;
701
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700702 /* MoCA port is special as we do not get link status from CORE_LNKSTS,
Florian Fainelli246d7f72014-08-27 17:04:56 -0700703 * which means that we need to force the link at the port override
704 * level to get the data to flow. We do use what the interrupt handler
705 * did determine before.
Florian Fainelli7855f672014-12-11 18:12:42 -0800706 *
707 * For the other ports, we just force the link status, since this is
708 * a fixed PHY device.
Florian Fainelli246d7f72014-08-27 17:04:56 -0700709 */
Florian Fainelli8b7c94e2015-10-23 12:11:08 -0700710 if (port == priv->moca_port) {
Florian Fainelli246d7f72014-08-27 17:04:56 -0700711 status->link = priv->port_sts[port].link;
Florian Fainelli4ab7f912015-05-15 12:38:01 -0700712 /* For MoCA interfaces, also force a link down notification
713 * since some version of the user-space daemon (mocad) use
714 * cmd->autoneg to force the link, which messes up the PHY
715 * state machine and make it go in PHY_FORCING state instead.
716 */
717 if (!status->link)
Andrew Lunnc8b09802016-06-04 21:16:57 +0200718 netif_carrier_off(ds->ports[port].netdev);
Florian Fainelli246d7f72014-08-27 17:04:56 -0700719 status->duplex = 1;
720 } else {
Florian Fainelli7855f672014-12-11 18:12:42 -0800721 status->link = 1;
Florian Fainelli246d7f72014-08-27 17:04:56 -0700722 status->duplex = !!(duplex & (1 << port));
723 }
724
Florian Fainelli0fe99332017-01-20 12:36:30 -0800725 reg = core_readl(priv, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800726 reg |= SW_OVERRIDE;
727 if (status->link)
728 reg |= LINK_STS;
729 else
730 reg &= ~LINK_STS;
Florian Fainelli0fe99332017-01-20 12:36:30 -0800731 core_writel(priv, reg, offset);
Florian Fainelli7855f672014-12-11 18:12:42 -0800732
Florian Fainelli246d7f72014-08-27 17:04:56 -0700733 if ((pause & (1 << port)) &&
734 (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
735 status->asym_pause = 1;
736 status->pause = 1;
737 }
738
739 if (pause & (1 << port))
740 status->pause = 1;
741}
742
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700743static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
744{
Florian Fainellif4589952016-08-26 12:18:33 -0700745 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700746 unsigned int port;
747
Florian Fainelli691c9a82015-01-20 16:42:00 -0800748 bcm_sf2_intr_disable(priv);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700749
750 /* Disable all ports physically present including the IMP
751 * port, the other ones have already been disabled during
752 * bcm_sf2_sw_setup
753 */
754 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200755 if ((1 << port) & ds->enabled_port_mask ||
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700756 dsa_is_cpu_port(ds, port))
Florian Fainellib6d045d2014-09-24 17:05:20 -0700757 bcm_sf2_port_disable(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700758 }
759
760 return 0;
761}
762
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700763static int bcm_sf2_sw_resume(struct dsa_switch *ds)
764{
Florian Fainellif4589952016-08-26 12:18:33 -0700765 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700766 unsigned int port;
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700767 int ret;
768
769 ret = bcm_sf2_sw_rst(priv);
770 if (ret) {
771 pr_err("%s: failed to software reset switch\n", __func__);
772 return ret;
773 }
774
Florian Fainellib0836682015-02-05 11:40:41 -0800775 if (priv->hw_params.num_gphy == 1)
776 bcm_sf2_gphy_enable_set(ds, true);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700777
778 for (port = 0; port < DSA_MAX_PORTS; port++) {
Andrew Lunn74c3e2a2016-04-13 02:40:44 +0200779 if ((1 << port) & ds->enabled_port_mask)
Florian Fainellib6d045d2014-09-24 17:05:20 -0700780 bcm_sf2_port_setup(ds, port, NULL);
Florian Fainelli8cfa9492014-09-18 17:31:23 -0700781 else if (dsa_is_cpu_port(ds, port))
782 bcm_sf2_imp_setup(ds, port);
783 }
784
785 return 0;
786}
787
Florian Fainelli96e65d72014-09-18 17:31:25 -0700788static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
789 struct ethtool_wolinfo *wol)
790{
Vivien Didelot3a8f6f82017-08-02 15:48:25 -0400791 struct net_device *p = ds->dst->cpu_dp->netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700792 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli96e65d72014-09-18 17:31:25 -0700793 struct ethtool_wolinfo pwol;
794
795 /* Get the parent device WoL settings */
796 p->ethtool_ops->get_wol(p, &pwol);
797
798 /* Advertise the parent device supported settings */
799 wol->supported = pwol.supported;
800 memset(&wol->sopass, 0, sizeof(wol->sopass));
801
802 if (pwol.wolopts & WAKE_MAGICSECURE)
803 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
804
805 if (priv->wol_ports_mask & (1 << port))
806 wol->wolopts = pwol.wolopts;
807 else
808 wol->wolopts = 0;
809}
810
811static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
812 struct ethtool_wolinfo *wol)
813{
Vivien Didelot3a8f6f82017-08-02 15:48:25 -0400814 struct net_device *p = ds->dst->cpu_dp->netdev;
Florian Fainellif4589952016-08-26 12:18:33 -0700815 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Vivien Didelot8b0d3ea2017-05-16 14:10:33 -0400816 s8 cpu_port = ds->dst->cpu_dp->index;
Florian Fainelli96e65d72014-09-18 17:31:25 -0700817 struct ethtool_wolinfo pwol;
818
819 p->ethtool_ops->get_wol(p, &pwol);
820 if (wol->wolopts & ~pwol.supported)
821 return -EINVAL;
822
823 if (wol->wolopts)
824 priv->wol_ports_mask |= (1 << port);
825 else
826 priv->wol_ports_mask &= ~(1 << port);
827
828 /* If we have at least one port enabled, make sure the CPU port
829 * is also enabled. If the CPU port is the last one enabled, we disable
830 * it since this configuration does not make sense.
831 */
832 if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
833 priv->wol_ports_mask |= (1 << cpu_port);
834 else
835 priv->wol_ports_mask &= ~(1 << cpu_port);
836
837 return p->ethtool_ops->set_wol(p, wol);
838}
839
Florian Fainellide0b9d32016-08-26 12:18:34 -0700840static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
Florian Fainelli9c57a772016-06-09 17:42:08 -0700841{
Florian Fainellide0b9d32016-08-26 12:18:34 -0700842 unsigned int timeout = 10;
843 u32 reg;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700844
Florian Fainellide0b9d32016-08-26 12:18:34 -0700845 do {
846 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
847 if (!(reg & ARLA_VTBL_STDN))
848 return 0;
Florian Fainelli9c57a772016-06-09 17:42:08 -0700849
Florian Fainellide0b9d32016-08-26 12:18:34 -0700850 usleep_range(1000, 2000);
851 } while (timeout--);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700852
Florian Fainellide0b9d32016-08-26 12:18:34 -0700853 return -ETIMEDOUT;
854}
Florian Fainelli9c57a772016-06-09 17:42:08 -0700855
Florian Fainellide0b9d32016-08-26 12:18:34 -0700856static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
857{
858 core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
859
860 return bcm_sf2_vlan_op_wait(priv);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700861}
862
863static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
864{
Florian Fainellif4589952016-08-26 12:18:33 -0700865 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli9c57a772016-06-09 17:42:08 -0700866 unsigned int port;
867
868 /* Clear all VLANs */
869 bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
870
871 for (port = 0; port < priv->hw_params.num_ports; port++) {
872 if (!((1 << port) & ds->enabled_port_mask))
873 continue;
874
875 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
876 }
877}
878
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700879static int bcm_sf2_sw_setup(struct dsa_switch *ds)
880{
Florian Fainellif4589952016-08-26 12:18:33 -0700881 struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -0700882 unsigned int port;
Florian Fainellid9338022016-08-18 15:30:14 -0700883
884 /* Enable all valid ports and disable those unused */
885 for (port = 0; port < priv->hw_params.num_ports; port++) {
886 /* IMP port receives special treatment */
887 if ((1 << port) & ds->enabled_port_mask)
888 bcm_sf2_port_setup(ds, port, NULL);
889 else if (dsa_is_cpu_port(ds, port))
890 bcm_sf2_imp_setup(ds, port);
891 else
892 bcm_sf2_port_disable(ds, port, NULL);
893 }
894
895 bcm_sf2_sw_configure_vlan(ds);
896
897 return 0;
898}
899
Florian Fainellif4589952016-08-26 12:18:33 -0700900/* The SWITCH_CORE register space is managed by b53 but operates on a page +
901 * register basis so we need to translate that into an address that the
902 * bus-glue understands.
903 */
904#define SF2_PAGE_REG_MKADDR(page, reg) ((page) << 10 | (reg) << 2)
905
906static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
907 u8 *val)
908{
909 struct bcm_sf2_priv *priv = dev->priv;
910
911 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
912
913 return 0;
914}
915
916static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
917 u16 *val)
918{
919 struct bcm_sf2_priv *priv = dev->priv;
920
921 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
922
923 return 0;
924}
925
926static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
927 u32 *val)
928{
929 struct bcm_sf2_priv *priv = dev->priv;
930
931 *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
932
933 return 0;
934}
935
936static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
937 u64 *val)
938{
939 struct bcm_sf2_priv *priv = dev->priv;
940
941 *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
942
943 return 0;
944}
945
946static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
947 u8 value)
948{
949 struct bcm_sf2_priv *priv = dev->priv;
950
951 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
952
953 return 0;
954}
955
956static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
957 u16 value)
958{
959 struct bcm_sf2_priv *priv = dev->priv;
960
961 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
962
963 return 0;
964}
965
966static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
967 u32 value)
968{
969 struct bcm_sf2_priv *priv = dev->priv;
970
971 core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
972
973 return 0;
974}
975
976static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
977 u64 value)
978{
979 struct bcm_sf2_priv *priv = dev->priv;
980
981 core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
982
983 return 0;
984}
985
Bhumika Goyal7e3108f2017-08-29 22:17:52 +0530986static const struct b53_io_ops bcm_sf2_io_ops = {
Florian Fainellif4589952016-08-26 12:18:33 -0700987 .read8 = bcm_sf2_core_read8,
988 .read16 = bcm_sf2_core_read16,
989 .read32 = bcm_sf2_core_read32,
990 .read48 = bcm_sf2_core_read64,
991 .read64 = bcm_sf2_core_read64,
992 .write8 = bcm_sf2_core_write8,
993 .write16 = bcm_sf2_core_write16,
994 .write32 = bcm_sf2_core_write32,
995 .write48 = bcm_sf2_core_write64,
996 .write64 = bcm_sf2_core_write64,
997};
998
Florian Fainellia82f67a2017-01-08 14:52:08 -0800999static const struct dsa_switch_ops bcm_sf2_ops = {
Florian Fainelli73095cb2017-01-08 14:52:06 -08001000 .get_tag_protocol = bcm_sf2_sw_get_tag_protocol,
1001 .setup = bcm_sf2_sw_setup,
1002 .get_strings = b53_get_strings,
1003 .get_ethtool_stats = b53_get_ethtool_stats,
1004 .get_sset_count = b53_get_sset_count,
1005 .get_phy_flags = bcm_sf2_sw_get_phy_flags,
1006 .adjust_link = bcm_sf2_sw_adjust_link,
1007 .fixed_link_update = bcm_sf2_sw_fixed_link_update,
1008 .suspend = bcm_sf2_sw_suspend,
1009 .resume = bcm_sf2_sw_resume,
1010 .get_wol = bcm_sf2_sw_get_wol,
1011 .set_wol = bcm_sf2_sw_set_wol,
1012 .port_enable = bcm_sf2_port_setup,
1013 .port_disable = bcm_sf2_port_disable,
Vivien Didelot08f50062017-08-01 16:32:41 -04001014 .get_mac_eee = bcm_sf2_sw_get_mac_eee,
1015 .set_mac_eee = bcm_sf2_sw_set_mac_eee,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001016 .port_bridge_join = b53_br_join,
1017 .port_bridge_leave = b53_br_leave,
1018 .port_stp_state_set = b53_br_set_stp_state,
1019 .port_fast_age = b53_br_fast_age,
1020 .port_vlan_filtering = b53_vlan_filtering,
1021 .port_vlan_prepare = b53_vlan_prepare,
1022 .port_vlan_add = b53_vlan_add,
1023 .port_vlan_del = b53_vlan_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001024 .port_fdb_dump = b53_fdb_dump,
1025 .port_fdb_add = b53_fdb_add,
1026 .port_fdb_del = b53_fdb_del,
Florian Fainelli73181662017-01-30 09:48:43 -08001027 .get_rxnfc = bcm_sf2_get_rxnfc,
1028 .set_rxnfc = bcm_sf2_set_rxnfc,
Florian Fainelliec960de2017-01-30 12:41:43 -08001029 .port_mirror_add = b53_mirror_add,
1030 .port_mirror_del = b53_mirror_del,
Florian Fainelli73095cb2017-01-08 14:52:06 -08001031};
1032
Florian Fainellia78e86e2017-01-20 12:36:29 -08001033struct bcm_sf2_of_data {
1034 u32 type;
1035 const u16 *reg_offsets;
1036 unsigned int core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001037 unsigned int num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001038};
1039
1040/* Register offsets for the SWITCH_REG_* block */
1041static const u16 bcm_sf2_7445_reg_offsets[] = {
1042 [REG_SWITCH_CNTRL] = 0x00,
1043 [REG_SWITCH_STATUS] = 0x04,
1044 [REG_DIR_DATA_WRITE] = 0x08,
1045 [REG_DIR_DATA_READ] = 0x0C,
1046 [REG_SWITCH_REVISION] = 0x18,
1047 [REG_PHY_REVISION] = 0x1C,
1048 [REG_SPHY_CNTRL] = 0x2C,
1049 [REG_RGMII_0_CNTRL] = 0x34,
1050 [REG_RGMII_1_CNTRL] = 0x40,
1051 [REG_RGMII_2_CNTRL] = 0x4c,
1052 [REG_LED_0_CNTRL] = 0x90,
1053 [REG_LED_1_CNTRL] = 0x94,
1054 [REG_LED_2_CNTRL] = 0x98,
1055};
1056
1057static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1058 .type = BCM7445_DEVICE_ID,
1059 .core_reg_align = 0,
1060 .reg_offsets = bcm_sf2_7445_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001061 .num_cfp_rules = 256,
Florian Fainellia78e86e2017-01-20 12:36:29 -08001062};
1063
Florian Fainelli0fe99332017-01-20 12:36:30 -08001064static const u16 bcm_sf2_7278_reg_offsets[] = {
1065 [REG_SWITCH_CNTRL] = 0x00,
1066 [REG_SWITCH_STATUS] = 0x04,
1067 [REG_DIR_DATA_WRITE] = 0x08,
1068 [REG_DIR_DATA_READ] = 0x0c,
1069 [REG_SWITCH_REVISION] = 0x10,
1070 [REG_PHY_REVISION] = 0x14,
1071 [REG_SPHY_CNTRL] = 0x24,
1072 [REG_RGMII_0_CNTRL] = 0xe0,
1073 [REG_RGMII_1_CNTRL] = 0xec,
1074 [REG_RGMII_2_CNTRL] = 0xf8,
1075 [REG_LED_0_CNTRL] = 0x40,
1076 [REG_LED_1_CNTRL] = 0x4c,
1077 [REG_LED_2_CNTRL] = 0x58,
1078};
1079
1080static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1081 .type = BCM7278_DEVICE_ID,
1082 .core_reg_align = 1,
1083 .reg_offsets = bcm_sf2_7278_reg_offsets,
Florian Fainellidf191632017-08-30 12:39:33 -07001084 .num_cfp_rules = 128,
Florian Fainelli0fe99332017-01-20 12:36:30 -08001085};
1086
Florian Fainellia78e86e2017-01-20 12:36:29 -08001087static const struct of_device_id bcm_sf2_of_match[] = {
1088 { .compatible = "brcm,bcm7445-switch-v4.0",
1089 .data = &bcm_sf2_7445_data
1090 },
Florian Fainelli0fe99332017-01-20 12:36:30 -08001091 { .compatible = "brcm,bcm7278-switch-v4.0",
1092 .data = &bcm_sf2_7278_data
1093 },
Florian Fainellia78e86e2017-01-20 12:36:29 -08001094 { /* sentinel */ },
1095};
1096MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1097
Florian Fainellid9338022016-08-18 15:30:14 -07001098static int bcm_sf2_sw_probe(struct platform_device *pdev)
1099{
1100 const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1101 struct device_node *dn = pdev->dev.of_node;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001102 const struct of_device_id *of_id = NULL;
1103 const struct bcm_sf2_of_data *data;
Florian Fainellif4589952016-08-26 12:18:33 -07001104 struct b53_platform_data *pdata;
Florian Fainellia4c61b92017-01-07 21:01:56 -08001105 struct dsa_switch_ops *ops;
Florian Fainellid9338022016-08-18 15:30:14 -07001106 struct bcm_sf2_priv *priv;
Florian Fainellif4589952016-08-26 12:18:33 -07001107 struct b53_device *dev;
Florian Fainellid9338022016-08-18 15:30:14 -07001108 struct dsa_switch *ds;
1109 void __iomem **base;
Florian Fainelli4bd11672016-08-18 15:30:15 -07001110 struct resource *r;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001111 unsigned int i;
1112 u32 reg, rev;
1113 int ret;
1114
Florian Fainellif4589952016-08-26 12:18:33 -07001115 priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1116 if (!priv)
Florian Fainellid9338022016-08-18 15:30:14 -07001117 return -ENOMEM;
1118
Florian Fainellia4c61b92017-01-07 21:01:56 -08001119 ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1120 if (!ops)
1121 return -ENOMEM;
1122
Florian Fainellif4589952016-08-26 12:18:33 -07001123 dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1124 if (!dev)
1125 return -ENOMEM;
Florian Fainellid9338022016-08-18 15:30:14 -07001126
Florian Fainellif4589952016-08-26 12:18:33 -07001127 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1128 if (!pdata)
1129 return -ENOMEM;
1130
Florian Fainellia78e86e2017-01-20 12:36:29 -08001131 of_id = of_match_node(bcm_sf2_of_match, dn);
1132 if (!of_id || !of_id->data)
1133 return -EINVAL;
1134
1135 data = of_id->data;
1136
1137 /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1138 priv->type = data->type;
1139 priv->reg_offsets = data->reg_offsets;
1140 priv->core_reg_align = data->core_reg_align;
Florian Fainellidf191632017-08-30 12:39:33 -07001141 priv->num_cfp_rules = data->num_cfp_rules;
Florian Fainellia78e86e2017-01-20 12:36:29 -08001142
Florian Fainellif4589952016-08-26 12:18:33 -07001143 /* Auto-detection using standard registers will not work, so
1144 * provide an indication of what kind of device we are for
1145 * b53_common to work with
1146 */
Florian Fainellia78e86e2017-01-20 12:36:29 -08001147 pdata->chip_id = priv->type;
Florian Fainellif4589952016-08-26 12:18:33 -07001148 dev->pdata = pdata;
1149
1150 priv->dev = dev;
1151 ds = dev->ds;
Florian Fainelli73095cb2017-01-08 14:52:06 -08001152 ds->ops = &bcm_sf2_ops;
Florian Fainellif4589952016-08-26 12:18:33 -07001153
Florian Fainelli181183772017-09-03 20:27:02 -07001154 /* Advertise the 8 egress queues */
1155 ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1156
Florian Fainellif4589952016-08-26 12:18:33 -07001157 dev_set_drvdata(&pdev->dev, priv);
Florian Fainellid9338022016-08-18 15:30:14 -07001158
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001159 spin_lock_init(&priv->indir_lock);
1160 mutex_init(&priv->stats_mutex);
Florian Fainelli73181662017-01-30 09:48:43 -08001161 mutex_init(&priv->cfp.lock);
1162
1163 /* CFP rule #0 cannot be used for specific classifications, flag it as
1164 * permanently used
1165 */
1166 set_bit(0, priv->cfp.used);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001167
Florian Fainellid9338022016-08-18 15:30:14 -07001168 bcm_sf2_identify_ports(priv, dn->child);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001169
1170 priv->irq0 = irq_of_parse_and_map(dn, 0);
1171 priv->irq1 = irq_of_parse_and_map(dn, 1);
1172
1173 base = &priv->core;
1174 for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
Florian Fainelli4bd11672016-08-18 15:30:15 -07001175 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1176 *base = devm_ioremap_resource(&pdev->dev, r);
1177 if (IS_ERR(*base)) {
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001178 pr_err("unable to find register: %s\n", reg_names[i]);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001179 return PTR_ERR(*base);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001180 }
1181 base++;
1182 }
1183
1184 ret = bcm_sf2_sw_rst(priv);
1185 if (ret) {
1186 pr_err("unable to software reset switch: %d\n", ret);
Florian Fainelli4bd11672016-08-18 15:30:15 -07001187 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001188 }
1189
1190 ret = bcm_sf2_mdio_register(ds);
1191 if (ret) {
1192 pr_err("failed to register MDIO bus\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001193 return ret;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001194 }
1195
Florian Fainelli73181662017-01-30 09:48:43 -08001196 ret = bcm_sf2_cfp_rst(priv);
1197 if (ret) {
1198 pr_err("failed to reset CFP\n");
1199 goto out_mdio;
1200 }
1201
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001202 /* Disable all interrupts and request them */
1203 bcm_sf2_intr_disable(priv);
1204
Florian Fainelli4bd11672016-08-18 15:30:15 -07001205 ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1206 "switch_0", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001207 if (ret < 0) {
1208 pr_err("failed to request switch_0 IRQ\n");
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001209 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001210 }
1211
Florian Fainelli4bd11672016-08-18 15:30:15 -07001212 ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1213 "switch_1", priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001214 if (ret < 0) {
1215 pr_err("failed to request switch_1 IRQ\n");
Florian Fainelli4bd11672016-08-18 15:30:15 -07001216 goto out_mdio;
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001217 }
1218
1219 /* Reset the MIB counters */
1220 reg = core_readl(priv, CORE_GMNCFGCFG);
1221 reg |= RST_MIB_CNT;
1222 core_writel(priv, reg, CORE_GMNCFGCFG);
1223 reg &= ~RST_MIB_CNT;
1224 core_writel(priv, reg, CORE_GMNCFGCFG);
1225
1226 /* Get the maximum number of ports for this switch */
1227 priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1228 if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1229 priv->hw_params.num_ports = DSA_MAX_PORTS;
1230
1231 /* Assume a single GPHY setup if we can't read that property */
1232 if (of_property_read_u32(dn, "brcm,num-gphy",
1233 &priv->hw_params.num_gphy))
1234 priv->hw_params.num_gphy = 1;
1235
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001236 rev = reg_readl(priv, REG_SWITCH_REVISION);
1237 priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1238 SWITCH_TOP_REV_MASK;
1239 priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1240
1241 rev = reg_readl(priv, REG_PHY_REVISION);
1242 priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1243
Florian Fainellif4589952016-08-26 12:18:33 -07001244 ret = b53_switch_register(dev);
Florian Fainellid9338022016-08-18 15:30:14 -07001245 if (ret)
Florian Fainelli4bd11672016-08-18 15:30:15 -07001246 goto out_mdio;
Florian Fainellid9338022016-08-18 15:30:14 -07001247
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001248 pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1249 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1250 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1251 priv->core, priv->irq0, priv->irq1);
1252
1253 return 0;
1254
Florian Fainellibb9c0fa2016-07-29 12:35:57 -07001255out_mdio:
1256 bcm_sf2_mdio_unregister(priv);
Florian Fainelli7fbb1a92016-06-09 17:42:06 -07001257 return ret;
1258}
1259
Florian Fainellid9338022016-08-18 15:30:14 -07001260static int bcm_sf2_sw_remove(struct platform_device *pdev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001261{
Florian Fainellif4589952016-08-26 12:18:33 -07001262 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001263
1264 /* Disable all ports and interrupts */
1265 priv->wol_ports_mask = 0;
Florian Fainellif4589952016-08-26 12:18:33 -07001266 bcm_sf2_sw_suspend(priv->dev->ds);
1267 dsa_unregister_switch(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001268 bcm_sf2_mdio_unregister(priv);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001269
1270 return 0;
1271}
Florian Fainelli246d7f72014-08-27 17:04:56 -07001272
Florian Fainelli2399d612016-10-20 09:32:19 -07001273static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1274{
1275 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1276
1277 /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1278 * successful MDIO bus scan to occur. If we did turn off the GPHY
1279 * before (e.g: port_disable), this will also power it back on.
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001280 *
1281 * Do not rely on kexec_in_progress, just power the PHY on.
Florian Fainelli2399d612016-10-20 09:32:19 -07001282 */
1283 if (priv->hw_params.num_gphy == 1)
Florian Fainelli4a2947e2016-10-21 14:21:56 -07001284 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
Florian Fainelli2399d612016-10-20 09:32:19 -07001285}
1286
Florian Fainellid9338022016-08-18 15:30:14 -07001287#ifdef CONFIG_PM_SLEEP
1288static int bcm_sf2_suspend(struct device *dev)
Florian Fainelli246d7f72014-08-27 17:04:56 -07001289{
Florian Fainellid9338022016-08-18 15:30:14 -07001290 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001291 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001292
Florian Fainellif4589952016-08-26 12:18:33 -07001293 return dsa_switch_suspend(priv->dev->ds);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001294}
Florian Fainellid9338022016-08-18 15:30:14 -07001295
1296static int bcm_sf2_resume(struct device *dev)
1297{
1298 struct platform_device *pdev = to_platform_device(dev);
Florian Fainellif4589952016-08-26 12:18:33 -07001299 struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
Florian Fainellid9338022016-08-18 15:30:14 -07001300
Florian Fainellif4589952016-08-26 12:18:33 -07001301 return dsa_switch_resume(priv->dev->ds);
Florian Fainellid9338022016-08-18 15:30:14 -07001302}
1303#endif /* CONFIG_PM_SLEEP */
1304
1305static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1306 bcm_sf2_suspend, bcm_sf2_resume);
1307
Florian Fainellid9338022016-08-18 15:30:14 -07001308
1309static struct platform_driver bcm_sf2_driver = {
1310 .probe = bcm_sf2_sw_probe,
1311 .remove = bcm_sf2_sw_remove,
Florian Fainelli2399d612016-10-20 09:32:19 -07001312 .shutdown = bcm_sf2_sw_shutdown,
Florian Fainellid9338022016-08-18 15:30:14 -07001313 .driver = {
1314 .name = "brcm-sf2",
1315 .of_match_table = bcm_sf2_of_match,
1316 .pm = &bcm_sf2_pm_ops,
1317 },
1318};
1319module_platform_driver(bcm_sf2_driver);
Florian Fainelli246d7f72014-08-27 17:04:56 -07001320
1321MODULE_AUTHOR("Broadcom Corporation");
1322MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1323MODULE_LICENSE("GPL");
1324MODULE_ALIAS("platform:brcm-sf2");