]> Git Repo - J-linux.git/blob - drivers/net/dsa/bcm_sf2.c
net: dsa: bcm_sf2: support BCM4908's integrated switch
[J-linux.git] / drivers / net / dsa / bcm_sf2.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Broadcom Starfighter 2 DSA switch driver
4  *
5  * Copyright (C) 2014, Broadcom Corporation
6  */
7
8 #include <linux/list.h>
9 #include <linux/module.h>
10 #include <linux/netdevice.h>
11 #include <linux/interrupt.h>
12 #include <linux/platform_device.h>
13 #include <linux/phy.h>
14 #include <linux/phy_fixed.h>
15 #include <linux/phylink.h>
16 #include <linux/mii.h>
17 #include <linux/clk.h>
18 #include <linux/of.h>
19 #include <linux/of_irq.h>
20 #include <linux/of_address.h>
21 #include <linux/of_net.h>
22 #include <linux/of_mdio.h>
23 #include <net/dsa.h>
24 #include <linux/ethtool.h>
25 #include <linux/if_bridge.h>
26 #include <linux/brcmphy.h>
27 #include <linux/etherdevice.h>
28 #include <linux/platform_data/b53.h>
29
30 #include "bcm_sf2.h"
31 #include "bcm_sf2_regs.h"
32 #include "b53/b53_priv.h"
33 #include "b53/b53_regs.h"
34
35 /* Return the number of active ports, not counting the IMP (CPU) port */
36 static unsigned int bcm_sf2_num_active_ports(struct dsa_switch *ds)
37 {
38         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
39         unsigned int port, count = 0;
40
41         for (port = 0; port < ARRAY_SIZE(priv->port_sts); port++) {
42                 if (dsa_is_cpu_port(ds, port))
43                         continue;
44                 if (priv->port_sts[port].enabled)
45                         count++;
46         }
47
48         return count;
49 }
50
51 static void bcm_sf2_recalc_clock(struct dsa_switch *ds)
52 {
53         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
54         unsigned long new_rate;
55         unsigned int ports_active;
56         /* Frequenty in Mhz */
57         static const unsigned long rate_table[] = {
58                 59220000,
59                 60820000,
60                 62500000,
61                 62500000,
62         };
63
64         ports_active = bcm_sf2_num_active_ports(ds);
65         if (ports_active == 0 || !priv->clk_mdiv)
66                 return;
67
68         /* If we overflow our table, just use the recommended operational
69          * frequency
70          */
71         if (ports_active > ARRAY_SIZE(rate_table))
72                 new_rate = 90000000;
73         else
74                 new_rate = rate_table[ports_active - 1];
75         clk_set_rate(priv->clk_mdiv, new_rate);
76 }
77
78 static void bcm_sf2_imp_setup(struct dsa_switch *ds, int port)
79 {
80         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
81         unsigned int i;
82         u32 reg, offset;
83
84         /* Enable the port memories */
85         reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
86         reg &= ~P_TXQ_PSM_VDD(port);
87         core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
88
89         /* Enable forwarding */
90         core_writel(priv, SW_FWDG_EN, CORE_SWMODE);
91
92         /* Enable IMP port in dumb mode */
93         reg = core_readl(priv, CORE_SWITCH_CTRL);
94         reg |= MII_DUMB_FWDG_EN;
95         core_writel(priv, reg, CORE_SWITCH_CTRL);
96
97         /* Configure Traffic Class to QoS mapping, allow each priority to map
98          * to a different queue number
99          */
100         reg = core_readl(priv, CORE_PORT_TC2_QOS_MAP_PORT(port));
101         for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
102                 reg |= i << (PRT_TO_QID_SHIFT * i);
103         core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
104
105         b53_brcm_hdr_setup(ds, port);
106
107         if (port == 8) {
108                 if (priv->type == BCM4908_DEVICE_ID ||
109                     priv->type == BCM7445_DEVICE_ID)
110                         offset = CORE_STS_OVERRIDE_IMP;
111                 else
112                         offset = CORE_STS_OVERRIDE_IMP2;
113
114                 /* Force link status for IMP port */
115                 reg = core_readl(priv, offset);
116                 reg |= (MII_SW_OR | LINK_STS);
117                 reg &= ~GMII_SPEED_UP_2G;
118                 core_writel(priv, reg, offset);
119
120                 /* Enable Broadcast, Multicast, Unicast forwarding to IMP port */
121                 reg = core_readl(priv, CORE_IMP_CTL);
122                 reg |= (RX_BCST_EN | RX_MCST_EN | RX_UCST_EN);
123                 reg &= ~(RX_DIS | TX_DIS);
124                 core_writel(priv, reg, CORE_IMP_CTL);
125         } else {
126                 reg = core_readl(priv, CORE_G_PCTL_PORT(port));
127                 reg &= ~(RX_DIS | TX_DIS);
128                 core_writel(priv, reg, CORE_G_PCTL_PORT(port));
129         }
130
131         priv->port_sts[port].enabled = true;
132 }
133
134 static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
135 {
136         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
137         u32 reg;
138
139         reg = reg_readl(priv, REG_SPHY_CNTRL);
140         if (enable) {
141                 reg |= PHY_RESET;
142                 reg &= ~(EXT_PWR_DOWN | IDDQ_BIAS | IDDQ_GLOBAL_PWR | CK25_DIS);
143                 reg_writel(priv, reg, REG_SPHY_CNTRL);
144                 udelay(21);
145                 reg = reg_readl(priv, REG_SPHY_CNTRL);
146                 reg &= ~PHY_RESET;
147         } else {
148                 reg |= EXT_PWR_DOWN | IDDQ_BIAS | PHY_RESET;
149                 reg_writel(priv, reg, REG_SPHY_CNTRL);
150                 mdelay(1);
151                 reg |= CK25_DIS;
152         }
153         reg_writel(priv, reg, REG_SPHY_CNTRL);
154
155         /* Use PHY-driven LED signaling */
156         if (!enable) {
157                 reg = reg_readl(priv, REG_LED_CNTRL(0));
158                 reg |= SPDLNK_SRC_SEL;
159                 reg_writel(priv, reg, REG_LED_CNTRL(0));
160         }
161 }
162
163 static inline void bcm_sf2_port_intr_enable(struct bcm_sf2_priv *priv,
164                                             int port)
165 {
166         unsigned int off;
167
168         switch (port) {
169         case 7:
170                 off = P7_IRQ_OFF;
171                 break;
172         case 0:
173                 /* Port 0 interrupts are located on the first bank */
174                 intrl2_0_mask_clear(priv, P_IRQ_MASK(P0_IRQ_OFF));
175                 return;
176         default:
177                 off = P_IRQ_OFF(port);
178                 break;
179         }
180
181         intrl2_1_mask_clear(priv, P_IRQ_MASK(off));
182 }
183
184 static inline void bcm_sf2_port_intr_disable(struct bcm_sf2_priv *priv,
185                                              int port)
186 {
187         unsigned int off;
188
189         switch (port) {
190         case 7:
191                 off = P7_IRQ_OFF;
192                 break;
193         case 0:
194                 /* Port 0 interrupts are located on the first bank */
195                 intrl2_0_mask_set(priv, P_IRQ_MASK(P0_IRQ_OFF));
196                 intrl2_0_writel(priv, P_IRQ_MASK(P0_IRQ_OFF), INTRL2_CPU_CLEAR);
197                 return;
198         default:
199                 off = P_IRQ_OFF(port);
200                 break;
201         }
202
203         intrl2_1_mask_set(priv, P_IRQ_MASK(off));
204         intrl2_1_writel(priv, P_IRQ_MASK(off), INTRL2_CPU_CLEAR);
205 }
206
207 static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
208                               struct phy_device *phy)
209 {
210         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
211         unsigned int i;
212         u32 reg;
213
214         if (!dsa_is_user_port(ds, port))
215                 return 0;
216
217         priv->port_sts[port].enabled = true;
218
219         bcm_sf2_recalc_clock(ds);
220
221         /* Clear the memory power down */
222         reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
223         reg &= ~P_TXQ_PSM_VDD(port);
224         core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
225
226         /* Enable learning */
227         reg = core_readl(priv, CORE_DIS_LEARN);
228         reg &= ~BIT(port);
229         core_writel(priv, reg, CORE_DIS_LEARN);
230
231         /* Enable Broadcom tags for that port if requested */
232         if (priv->brcm_tag_mask & BIT(port)) {
233                 b53_brcm_hdr_setup(ds, port);
234
235                 /* Disable learning on ASP port */
236                 if (port == 7) {
237                         reg = core_readl(priv, CORE_DIS_LEARN);
238                         reg |= BIT(port);
239                         core_writel(priv, reg, CORE_DIS_LEARN);
240                 }
241         }
242
243         /* 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));
247         for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++)
248                 reg |= i << (PRT_TO_QID_SHIFT * i);
249         core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
250
251         /* Re-enable the GPHY and re-apply workarounds */
252         if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
253                 bcm_sf2_gphy_enable_set(ds, true);
254                 if (phy) {
255                         /* if phy_stop() has been called before, phy
256                          * will be in halted state, and phy_start()
257                          * will call resume.
258                          *
259                          * the resume path does not configure back
260                          * autoneg settings, and since we hard reset
261                          * the phy manually here, we need to reset the
262                          * state machine also.
263                          */
264                         phy->state = PHY_READY;
265                         phy_init_hw(phy);
266                 }
267         }
268
269         /* Enable MoCA port interrupts to get notified */
270         if (port == priv->moca_port)
271                 bcm_sf2_port_intr_enable(priv, port);
272
273         /* Set per-queue pause threshold to 32 */
274         core_writel(priv, 32, CORE_TXQ_THD_PAUSE_QN_PORT(port));
275
276         /* Set ACB threshold to 24 */
277         for (i = 0; i < SF2_NUM_EGRESS_QUEUES; i++) {
278                 reg = acb_readl(priv, ACB_QUEUE_CFG(port *
279                                                     SF2_NUM_EGRESS_QUEUES + i));
280                 reg &= ~XOFF_THRESHOLD_MASK;
281                 reg |= 24;
282                 acb_writel(priv, reg, ACB_QUEUE_CFG(port *
283                                                     SF2_NUM_EGRESS_QUEUES + i));
284         }
285
286         return b53_enable_port(ds, port, phy);
287 }
288
289 static void bcm_sf2_port_disable(struct dsa_switch *ds, int port)
290 {
291         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
292         u32 reg;
293
294         /* Disable learning while in WoL mode */
295         if (priv->wol_ports_mask & (1 << port)) {
296                 reg = core_readl(priv, CORE_DIS_LEARN);
297                 reg |= BIT(port);
298                 core_writel(priv, reg, CORE_DIS_LEARN);
299                 return;
300         }
301
302         if (port == priv->moca_port)
303                 bcm_sf2_port_intr_disable(priv, port);
304
305         if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
306                 bcm_sf2_gphy_enable_set(ds, false);
307
308         b53_disable_port(ds, port);
309
310         /* Power down the port memory */
311         reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
312         reg |= P_TXQ_PSM_VDD(port);
313         core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
314
315         priv->port_sts[port].enabled = false;
316
317         bcm_sf2_recalc_clock(ds);
318 }
319
320
321 static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
322                                int regnum, u16 val)
323 {
324         int ret = 0;
325         u32 reg;
326
327         reg = reg_readl(priv, REG_SWITCH_CNTRL);
328         reg |= MDIO_MASTER_SEL;
329         reg_writel(priv, reg, REG_SWITCH_CNTRL);
330
331         /* Page << 8 | offset */
332         reg = 0x70;
333         reg <<= 2;
334         core_writel(priv, addr, reg);
335
336         /* Page << 8 | offset */
337         reg = 0x80 << 8 | regnum << 1;
338         reg <<= 2;
339
340         if (op)
341                 ret = core_readl(priv, reg);
342         else
343                 core_writel(priv, val, reg);
344
345         reg = reg_readl(priv, REG_SWITCH_CNTRL);
346         reg &= ~MDIO_MASTER_SEL;
347         reg_writel(priv, reg, REG_SWITCH_CNTRL);
348
349         return ret & 0xffff;
350 }
351
352 static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
353 {
354         struct bcm_sf2_priv *priv = bus->priv;
355
356         /* Intercept reads from Broadcom pseudo-PHY address, else, send
357          * them to our master MDIO bus controller
358          */
359         if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
360                 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
361         else
362                 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
363 }
364
365 static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
366                                  u16 val)
367 {
368         struct bcm_sf2_priv *priv = bus->priv;
369
370         /* Intercept writes to the Broadcom pseudo-PHY address, else,
371          * send them to our master MDIO bus controller
372          */
373         if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
374                 return bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
375         else
376                 return mdiobus_write_nested(priv->master_mii_bus, addr,
377                                 regnum, val);
378 }
379
380 static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
381 {
382         struct dsa_switch *ds = dev_id;
383         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
384
385         priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
386                                 ~priv->irq0_mask;
387         intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
388
389         return IRQ_HANDLED;
390 }
391
392 static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
393 {
394         struct dsa_switch *ds = dev_id;
395         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
396
397         priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
398                                 ~priv->irq1_mask;
399         intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
400
401         if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF)) {
402                 priv->port_sts[7].link = true;
403                 dsa_port_phylink_mac_change(ds, 7, true);
404         }
405         if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF)) {
406                 priv->port_sts[7].link = false;
407                 dsa_port_phylink_mac_change(ds, 7, false);
408         }
409
410         return IRQ_HANDLED;
411 }
412
413 static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
414 {
415         unsigned int timeout = 1000;
416         u32 reg;
417         int ret;
418
419         /* The watchdog reset does not work on 7278, we need to hit the
420          * "external" reset line through the reset controller.
421          */
422         if (priv->type == BCM7278_DEVICE_ID && !IS_ERR(priv->rcdev)) {
423                 ret = reset_control_assert(priv->rcdev);
424                 if (ret)
425                         return ret;
426
427                 return reset_control_deassert(priv->rcdev);
428         }
429
430         reg = core_readl(priv, CORE_WATCHDOG_CTRL);
431         reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
432         core_writel(priv, reg, CORE_WATCHDOG_CTRL);
433
434         do {
435                 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
436                 if (!(reg & SOFTWARE_RESET))
437                         break;
438
439                 usleep_range(1000, 2000);
440         } while (timeout-- > 0);
441
442         if (timeout == 0)
443                 return -ETIMEDOUT;
444
445         return 0;
446 }
447
448 static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
449 {
450         intrl2_0_mask_set(priv, 0xffffffff);
451         intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
452         intrl2_1_mask_set(priv, 0xffffffff);
453         intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
454 }
455
456 static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
457                                    struct device_node *dn)
458 {
459         struct device_node *port;
460         unsigned int port_num;
461         struct property *prop;
462         phy_interface_t mode;
463         int err;
464
465         priv->moca_port = -1;
466
467         for_each_available_child_of_node(dn, port) {
468                 if (of_property_read_u32(port, "reg", &port_num))
469                         continue;
470
471                 /* Internal PHYs get assigned a specific 'phy-mode' property
472                  * value: "internal" to help flag them before MDIO probing
473                  * has completed, since they might be turned off at that
474                  * time
475                  */
476                 err = of_get_phy_mode(port, &mode);
477                 if (err)
478                         continue;
479
480                 if (mode == PHY_INTERFACE_MODE_INTERNAL)
481                         priv->int_phy_mask |= 1 << port_num;
482
483                 if (mode == PHY_INTERFACE_MODE_MOCA)
484                         priv->moca_port = port_num;
485
486                 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
487                         priv->brcm_tag_mask |= 1 << port_num;
488
489                 /* Ensure that port 5 is not picked up as a DSA CPU port
490                  * flavour but a regular port instead. We should be using
491                  * devlink to be able to set the port flavour.
492                  */
493                 if (port_num == 5 && priv->type == BCM7278_DEVICE_ID) {
494                         prop = of_find_property(port, "ethernet", NULL);
495                         if (prop)
496                                 of_remove_property(port, prop);
497                 }
498         }
499 }
500
501 static int bcm_sf2_mdio_register(struct dsa_switch *ds)
502 {
503         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
504         struct device_node *dn, *child;
505         struct phy_device *phydev;
506         struct property *prop;
507         static int index;
508         int err, reg;
509
510         /* Find our integrated MDIO bus node */
511         dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
512         priv->master_mii_bus = of_mdio_find_bus(dn);
513         if (!priv->master_mii_bus)
514                 return -EPROBE_DEFER;
515
516         get_device(&priv->master_mii_bus->dev);
517         priv->master_mii_dn = dn;
518
519         priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
520         if (!priv->slave_mii_bus)
521                 return -ENOMEM;
522
523         priv->slave_mii_bus->priv = priv;
524         priv->slave_mii_bus->name = "sf2 slave mii";
525         priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
526         priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
527         snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
528                  index++);
529         priv->slave_mii_bus->dev.of_node = dn;
530
531         /* Include the pseudo-PHY address to divert reads towards our
532          * workaround. This is only required for 7445D0, since 7445E0
533          * disconnects the internal switch pseudo-PHY such that we can use the
534          * regular SWITCH_MDIO master controller instead.
535          *
536          * Here we flag the pseudo PHY as needing special treatment and would
537          * otherwise make all other PHY read/writes go to the master MDIO bus
538          * controller that comes with this switch backed by the "mdio-unimac"
539          * driver.
540          */
541         if (of_machine_is_compatible("brcm,bcm7445d0"))
542                 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR) | (1 << 0);
543         else
544                 priv->indir_phy_mask = 0;
545
546         ds->phys_mii_mask = priv->indir_phy_mask;
547         ds->slave_mii_bus = priv->slave_mii_bus;
548         priv->slave_mii_bus->parent = ds->dev->parent;
549         priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
550
551         /* We need to make sure that of_phy_connect() will not work by
552          * removing the 'phandle' and 'linux,phandle' properties and
553          * unregister the existing PHY device that was already registered.
554          */
555         for_each_available_child_of_node(dn, child) {
556                 if (of_property_read_u32(child, "reg", &reg) ||
557                     reg >= PHY_MAX_ADDR)
558                         continue;
559
560                 if (!(priv->indir_phy_mask & BIT(reg)))
561                         continue;
562
563                 prop = of_find_property(child, "phandle", NULL);
564                 if (prop)
565                         of_remove_property(child, prop);
566
567                 prop = of_find_property(child, "linux,phandle", NULL);
568                 if (prop)
569                         of_remove_property(child, prop);
570
571                 phydev = of_phy_find_device(child);
572                 if (phydev)
573                         phy_device_remove(phydev);
574         }
575
576         err = mdiobus_register(priv->slave_mii_bus);
577         if (err && dn)
578                 of_node_put(dn);
579
580         return err;
581 }
582
583 static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
584 {
585         mdiobus_unregister(priv->slave_mii_bus);
586         of_node_put(priv->master_mii_dn);
587 }
588
589 static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
590 {
591         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
592
593         /* The BCM7xxx PHY driver expects to find the integrated PHY revision
594          * in bits 15:8 and the patch level in bits 7:0 which is exactly what
595          * the REG_PHY_REVISION register layout is.
596          */
597
598         return priv->hw_params.gphy_rev;
599 }
600
601 static void bcm_sf2_sw_validate(struct dsa_switch *ds, int port,
602                                 unsigned long *supported,
603                                 struct phylink_link_state *state)
604 {
605         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
606         __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
607
608         if (!phy_interface_mode_is_rgmii(state->interface) &&
609             state->interface != PHY_INTERFACE_MODE_MII &&
610             state->interface != PHY_INTERFACE_MODE_REVMII &&
611             state->interface != PHY_INTERFACE_MODE_GMII &&
612             state->interface != PHY_INTERFACE_MODE_INTERNAL &&
613             state->interface != PHY_INTERFACE_MODE_MOCA) {
614                 bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
615                 if (port != core_readl(priv, CORE_IMP0_PRT_ID))
616                         dev_err(ds->dev,
617                                 "Unsupported interface: %d for port %d\n",
618                                 state->interface, port);
619                 return;
620         }
621
622         /* Allow all the expected bits */
623         phylink_set(mask, Autoneg);
624         phylink_set_port_modes(mask);
625         phylink_set(mask, Pause);
626         phylink_set(mask, Asym_Pause);
627
628         /* With the exclusion of MII and Reverse MII, we support Gigabit,
629          * including Half duplex
630          */
631         if (state->interface != PHY_INTERFACE_MODE_MII &&
632             state->interface != PHY_INTERFACE_MODE_REVMII) {
633                 phylink_set(mask, 1000baseT_Full);
634                 phylink_set(mask, 1000baseT_Half);
635         }
636
637         phylink_set(mask, 10baseT_Half);
638         phylink_set(mask, 10baseT_Full);
639         phylink_set(mask, 100baseT_Half);
640         phylink_set(mask, 100baseT_Full);
641
642         bitmap_and(supported, supported, mask,
643                    __ETHTOOL_LINK_MODE_MASK_NBITS);
644         bitmap_and(state->advertising, state->advertising, mask,
645                    __ETHTOOL_LINK_MODE_MASK_NBITS);
646 }
647
648 static void bcm_sf2_sw_mac_config(struct dsa_switch *ds, int port,
649                                   unsigned int mode,
650                                   const struct phylink_link_state *state)
651 {
652         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
653         u32 id_mode_dis = 0, port_mode;
654         u32 reg;
655
656         if (port == core_readl(priv, CORE_IMP0_PRT_ID))
657                 return;
658
659         switch (state->interface) {
660         case PHY_INTERFACE_MODE_RGMII:
661                 id_mode_dis = 1;
662                 fallthrough;
663         case PHY_INTERFACE_MODE_RGMII_TXID:
664                 port_mode = EXT_GPHY;
665                 break;
666         case PHY_INTERFACE_MODE_MII:
667                 port_mode = EXT_EPHY;
668                 break;
669         case PHY_INTERFACE_MODE_REVMII:
670                 port_mode = EXT_REVMII;
671                 break;
672         default:
673                 /* Nothing required for all other PHYs: internal and MoCA */
674                 return;
675         }
676
677         /* Clear id_mode_dis bit, and the existing port mode, let
678          * RGMII_MODE_EN bet set by mac_link_{up,down}
679          */
680         reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
681         reg &= ~ID_MODE_DIS;
682         reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
683
684         reg |= port_mode;
685         if (id_mode_dis)
686                 reg |= ID_MODE_DIS;
687
688         reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
689 }
690
691 static void bcm_sf2_sw_mac_link_set(struct dsa_switch *ds, int port,
692                                     phy_interface_t interface, bool link)
693 {
694         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
695         u32 reg;
696
697         if (!phy_interface_mode_is_rgmii(interface) &&
698             interface != PHY_INTERFACE_MODE_MII &&
699             interface != PHY_INTERFACE_MODE_REVMII)
700                 return;
701
702         /* If the link is down, just disable the interface to conserve power */
703         reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
704         if (link)
705                 reg |= RGMII_MODE_EN;
706         else
707                 reg &= ~RGMII_MODE_EN;
708         reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
709 }
710
711 static void bcm_sf2_sw_mac_link_down(struct dsa_switch *ds, int port,
712                                      unsigned int mode,
713                                      phy_interface_t interface)
714 {
715         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
716         u32 reg, offset;
717
718         if (port != core_readl(priv, CORE_IMP0_PRT_ID)) {
719                 if (priv->type == BCM4908_DEVICE_ID ||
720                     priv->type == BCM7445_DEVICE_ID)
721                         offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
722                 else
723                         offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
724
725                 reg = core_readl(priv, offset);
726                 reg &= ~LINK_STS;
727                 core_writel(priv, reg, offset);
728         }
729
730         bcm_sf2_sw_mac_link_set(ds, port, interface, false);
731 }
732
733 static void bcm_sf2_sw_mac_link_up(struct dsa_switch *ds, int port,
734                                    unsigned int mode,
735                                    phy_interface_t interface,
736                                    struct phy_device *phydev,
737                                    int speed, int duplex,
738                                    bool tx_pause, bool rx_pause)
739 {
740         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
741         struct ethtool_eee *p = &priv->dev->ports[port].eee;
742         u32 reg, offset;
743
744         bcm_sf2_sw_mac_link_set(ds, port, interface, true);
745
746         if (port != core_readl(priv, CORE_IMP0_PRT_ID)) {
747                 if (priv->type == BCM4908_DEVICE_ID ||
748                     priv->type == BCM7445_DEVICE_ID)
749                         offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
750                 else
751                         offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
752
753                 if (interface == PHY_INTERFACE_MODE_RGMII ||
754                     interface == PHY_INTERFACE_MODE_RGMII_TXID ||
755                     interface == PHY_INTERFACE_MODE_MII ||
756                     interface == PHY_INTERFACE_MODE_REVMII) {
757                         reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
758                         reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
759
760                         if (tx_pause)
761                                 reg |= TX_PAUSE_EN;
762                         if (rx_pause)
763                                 reg |= RX_PAUSE_EN;
764
765                         reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
766                 }
767
768                 reg = SW_OVERRIDE | LINK_STS;
769                 switch (speed) {
770                 case SPEED_1000:
771                         reg |= SPDSTS_1000 << SPEED_SHIFT;
772                         break;
773                 case SPEED_100:
774                         reg |= SPDSTS_100 << SPEED_SHIFT;
775                         break;
776                 }
777
778                 if (duplex == DUPLEX_FULL)
779                         reg |= DUPLX_MODE;
780
781                 core_writel(priv, reg, offset);
782         }
783
784         if (mode == MLO_AN_PHY && phydev)
785                 p->eee_enabled = b53_eee_init(ds, port, phydev);
786 }
787
788 static void bcm_sf2_sw_fixed_state(struct dsa_switch *ds, int port,
789                                    struct phylink_link_state *status)
790 {
791         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
792
793         status->link = false;
794
795         /* MoCA port is special as we do not get link status from CORE_LNKSTS,
796          * which means that we need to force the link at the port override
797          * level to get the data to flow. We do use what the interrupt handler
798          * did determine before.
799          *
800          * For the other ports, we just force the link status, since this is
801          * a fixed PHY device.
802          */
803         if (port == priv->moca_port) {
804                 status->link = priv->port_sts[port].link;
805                 /* For MoCA interfaces, also force a link down notification
806                  * since some version of the user-space daemon (mocad) use
807                  * cmd->autoneg to force the link, which messes up the PHY
808                  * state machine and make it go in PHY_FORCING state instead.
809                  */
810                 if (!status->link)
811                         netif_carrier_off(dsa_to_port(ds, port)->slave);
812                 status->duplex = DUPLEX_FULL;
813         } else {
814                 status->link = true;
815         }
816 }
817
818 static void bcm_sf2_enable_acb(struct dsa_switch *ds)
819 {
820         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
821         u32 reg;
822
823         /* Enable ACB globally */
824         reg = acb_readl(priv, ACB_CONTROL);
825         reg |= (ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
826         acb_writel(priv, reg, ACB_CONTROL);
827         reg &= ~(ACB_FLUSH_MASK << ACB_FLUSH_SHIFT);
828         reg |= ACB_EN | ACB_ALGORITHM;
829         acb_writel(priv, reg, ACB_CONTROL);
830 }
831
832 static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
833 {
834         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
835         unsigned int port;
836
837         bcm_sf2_intr_disable(priv);
838
839         /* Disable all ports physically present including the IMP
840          * port, the other ones have already been disabled during
841          * bcm_sf2_sw_setup
842          */
843         for (port = 0; port < ds->num_ports; port++) {
844                 if (dsa_is_user_port(ds, port) || dsa_is_cpu_port(ds, port))
845                         bcm_sf2_port_disable(ds, port);
846         }
847
848         if (!priv->wol_ports_mask)
849                 clk_disable_unprepare(priv->clk);
850
851         return 0;
852 }
853
854 static int bcm_sf2_sw_resume(struct dsa_switch *ds)
855 {
856         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
857         int ret;
858
859         if (!priv->wol_ports_mask)
860                 clk_prepare_enable(priv->clk);
861
862         ret = bcm_sf2_sw_rst(priv);
863         if (ret) {
864                 pr_err("%s: failed to software reset switch\n", __func__);
865                 return ret;
866         }
867
868         ret = bcm_sf2_cfp_resume(ds);
869         if (ret)
870                 return ret;
871
872         if (priv->hw_params.num_gphy == 1)
873                 bcm_sf2_gphy_enable_set(ds, true);
874
875         ds->ops->setup(ds);
876
877         return 0;
878 }
879
880 static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
881                                struct ethtool_wolinfo *wol)
882 {
883         struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
884         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
885         struct ethtool_wolinfo pwol = { };
886
887         /* Get the parent device WoL settings */
888         if (p->ethtool_ops->get_wol)
889                 p->ethtool_ops->get_wol(p, &pwol);
890
891         /* Advertise the parent device supported settings */
892         wol->supported = pwol.supported;
893         memset(&wol->sopass, 0, sizeof(wol->sopass));
894
895         if (pwol.wolopts & WAKE_MAGICSECURE)
896                 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
897
898         if (priv->wol_ports_mask & (1 << port))
899                 wol->wolopts = pwol.wolopts;
900         else
901                 wol->wolopts = 0;
902 }
903
904 static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
905                               struct ethtool_wolinfo *wol)
906 {
907         struct net_device *p = dsa_to_port(ds, port)->cpu_dp->master;
908         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
909         s8 cpu_port = dsa_to_port(ds, port)->cpu_dp->index;
910         struct ethtool_wolinfo pwol =  { };
911
912         if (p->ethtool_ops->get_wol)
913                 p->ethtool_ops->get_wol(p, &pwol);
914         if (wol->wolopts & ~pwol.supported)
915                 return -EINVAL;
916
917         if (wol->wolopts)
918                 priv->wol_ports_mask |= (1 << port);
919         else
920                 priv->wol_ports_mask &= ~(1 << port);
921
922         /* If we have at least one port enabled, make sure the CPU port
923          * is also enabled. If the CPU port is the last one enabled, we disable
924          * it since this configuration does not make sense.
925          */
926         if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
927                 priv->wol_ports_mask |= (1 << cpu_port);
928         else
929                 priv->wol_ports_mask &= ~(1 << cpu_port);
930
931         return p->ethtool_ops->set_wol(p, wol);
932 }
933
934 static int bcm_sf2_sw_setup(struct dsa_switch *ds)
935 {
936         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
937         unsigned int port;
938
939         /* Enable all valid ports and disable those unused */
940         for (port = 0; port < priv->hw_params.num_ports; port++) {
941                 /* IMP port receives special treatment */
942                 if (dsa_is_user_port(ds, port))
943                         bcm_sf2_port_setup(ds, port, NULL);
944                 else if (dsa_is_cpu_port(ds, port))
945                         bcm_sf2_imp_setup(ds, port);
946                 else
947                         bcm_sf2_port_disable(ds, port);
948         }
949
950         b53_configure_vlan(ds);
951         bcm_sf2_enable_acb(ds);
952
953         return b53_setup_devlink_resources(ds);
954 }
955
956 static void bcm_sf2_sw_teardown(struct dsa_switch *ds)
957 {
958         dsa_devlink_resources_unregister(ds);
959 }
960
961 /* The SWITCH_CORE register space is managed by b53 but operates on a page +
962  * register basis so we need to translate that into an address that the
963  * bus-glue understands.
964  */
965 #define SF2_PAGE_REG_MKADDR(page, reg)  ((page) << 10 | (reg) << 2)
966
967 static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
968                               u8 *val)
969 {
970         struct bcm_sf2_priv *priv = dev->priv;
971
972         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
973
974         return 0;
975 }
976
977 static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
978                                u16 *val)
979 {
980         struct bcm_sf2_priv *priv = dev->priv;
981
982         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
983
984         return 0;
985 }
986
987 static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
988                                u32 *val)
989 {
990         struct bcm_sf2_priv *priv = dev->priv;
991
992         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
993
994         return 0;
995 }
996
997 static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
998                                u64 *val)
999 {
1000         struct bcm_sf2_priv *priv = dev->priv;
1001
1002         *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
1003
1004         return 0;
1005 }
1006
1007 static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
1008                                u8 value)
1009 {
1010         struct bcm_sf2_priv *priv = dev->priv;
1011
1012         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1013
1014         return 0;
1015 }
1016
1017 static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
1018                                 u16 value)
1019 {
1020         struct bcm_sf2_priv *priv = dev->priv;
1021
1022         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1023
1024         return 0;
1025 }
1026
1027 static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
1028                                 u32 value)
1029 {
1030         struct bcm_sf2_priv *priv = dev->priv;
1031
1032         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1033
1034         return 0;
1035 }
1036
1037 static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
1038                                 u64 value)
1039 {
1040         struct bcm_sf2_priv *priv = dev->priv;
1041
1042         core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
1043
1044         return 0;
1045 }
1046
1047 static const struct b53_io_ops bcm_sf2_io_ops = {
1048         .read8  = bcm_sf2_core_read8,
1049         .read16 = bcm_sf2_core_read16,
1050         .read32 = bcm_sf2_core_read32,
1051         .read48 = bcm_sf2_core_read64,
1052         .read64 = bcm_sf2_core_read64,
1053         .write8 = bcm_sf2_core_write8,
1054         .write16 = bcm_sf2_core_write16,
1055         .write32 = bcm_sf2_core_write32,
1056         .write48 = bcm_sf2_core_write64,
1057         .write64 = bcm_sf2_core_write64,
1058 };
1059
1060 static void bcm_sf2_sw_get_strings(struct dsa_switch *ds, int port,
1061                                    u32 stringset, uint8_t *data)
1062 {
1063         int cnt = b53_get_sset_count(ds, port, stringset);
1064
1065         b53_get_strings(ds, port, stringset, data);
1066         bcm_sf2_cfp_get_strings(ds, port, stringset,
1067                                 data + cnt * ETH_GSTRING_LEN);
1068 }
1069
1070 static void bcm_sf2_sw_get_ethtool_stats(struct dsa_switch *ds, int port,
1071                                          uint64_t *data)
1072 {
1073         int cnt = b53_get_sset_count(ds, port, ETH_SS_STATS);
1074
1075         b53_get_ethtool_stats(ds, port, data);
1076         bcm_sf2_cfp_get_ethtool_stats(ds, port, data + cnt);
1077 }
1078
1079 static int bcm_sf2_sw_get_sset_count(struct dsa_switch *ds, int port,
1080                                      int sset)
1081 {
1082         int cnt = b53_get_sset_count(ds, port, sset);
1083
1084         if (cnt < 0)
1085                 return cnt;
1086
1087         cnt += bcm_sf2_cfp_get_sset_count(ds, port, sset);
1088
1089         return cnt;
1090 }
1091
1092 static const struct dsa_switch_ops bcm_sf2_ops = {
1093         .get_tag_protocol       = b53_get_tag_protocol,
1094         .setup                  = bcm_sf2_sw_setup,
1095         .teardown               = bcm_sf2_sw_teardown,
1096         .get_strings            = bcm_sf2_sw_get_strings,
1097         .get_ethtool_stats      = bcm_sf2_sw_get_ethtool_stats,
1098         .get_sset_count         = bcm_sf2_sw_get_sset_count,
1099         .get_ethtool_phy_stats  = b53_get_ethtool_phy_stats,
1100         .get_phy_flags          = bcm_sf2_sw_get_phy_flags,
1101         .phylink_validate       = bcm_sf2_sw_validate,
1102         .phylink_mac_config     = bcm_sf2_sw_mac_config,
1103         .phylink_mac_link_down  = bcm_sf2_sw_mac_link_down,
1104         .phylink_mac_link_up    = bcm_sf2_sw_mac_link_up,
1105         .phylink_fixed_state    = bcm_sf2_sw_fixed_state,
1106         .suspend                = bcm_sf2_sw_suspend,
1107         .resume                 = bcm_sf2_sw_resume,
1108         .get_wol                = bcm_sf2_sw_get_wol,
1109         .set_wol                = bcm_sf2_sw_set_wol,
1110         .port_enable            = bcm_sf2_port_setup,
1111         .port_disable           = bcm_sf2_port_disable,
1112         .get_mac_eee            = b53_get_mac_eee,
1113         .set_mac_eee            = b53_set_mac_eee,
1114         .port_bridge_join       = b53_br_join,
1115         .port_bridge_leave      = b53_br_leave,
1116         .port_stp_state_set     = b53_br_set_stp_state,
1117         .port_fast_age          = b53_br_fast_age,
1118         .port_vlan_filtering    = b53_vlan_filtering,
1119         .port_vlan_prepare      = b53_vlan_prepare,
1120         .port_vlan_add          = b53_vlan_add,
1121         .port_vlan_del          = b53_vlan_del,
1122         .port_fdb_dump          = b53_fdb_dump,
1123         .port_fdb_add           = b53_fdb_add,
1124         .port_fdb_del           = b53_fdb_del,
1125         .get_rxnfc              = bcm_sf2_get_rxnfc,
1126         .set_rxnfc              = bcm_sf2_set_rxnfc,
1127         .port_mirror_add        = b53_mirror_add,
1128         .port_mirror_del        = b53_mirror_del,
1129         .port_mdb_prepare       = b53_mdb_prepare,
1130         .port_mdb_add           = b53_mdb_add,
1131         .port_mdb_del           = b53_mdb_del,
1132 };
1133
1134 struct bcm_sf2_of_data {
1135         u32 type;
1136         const u16 *reg_offsets;
1137         unsigned int core_reg_align;
1138         unsigned int num_cfp_rules;
1139 };
1140
1141 static const u16 bcm_sf2_4908_reg_offsets[] = {
1142         [REG_SWITCH_CNTRL]      = 0x00,
1143         [REG_SWITCH_STATUS]     = 0x04,
1144         [REG_DIR_DATA_WRITE]    = 0x08,
1145         [REG_DIR_DATA_READ]     = 0x0c,
1146         [REG_SWITCH_REVISION]   = 0x10,
1147         [REG_PHY_REVISION]      = 0x14,
1148         [REG_SPHY_CNTRL]        = 0x24,
1149         [REG_CROSSBAR]          = 0xc8,
1150         [REG_RGMII_0_CNTRL]     = 0xe0,
1151         [REG_RGMII_1_CNTRL]     = 0xec,
1152         [REG_RGMII_2_CNTRL]     = 0xf8,
1153         [REG_LED_0_CNTRL]       = 0x40,
1154         [REG_LED_1_CNTRL]       = 0x4c,
1155         [REG_LED_2_CNTRL]       = 0x58,
1156 };
1157
1158 static const struct bcm_sf2_of_data bcm_sf2_4908_data = {
1159         .type           = BCM4908_DEVICE_ID,
1160         .core_reg_align = 0,
1161         .reg_offsets    = bcm_sf2_4908_reg_offsets,
1162         .num_cfp_rules  = 0, /* FIXME */
1163 };
1164
1165 /* Register offsets for the SWITCH_REG_* block */
1166 static const u16 bcm_sf2_7445_reg_offsets[] = {
1167         [REG_SWITCH_CNTRL]      = 0x00,
1168         [REG_SWITCH_STATUS]     = 0x04,
1169         [REG_DIR_DATA_WRITE]    = 0x08,
1170         [REG_DIR_DATA_READ]     = 0x0C,
1171         [REG_SWITCH_REVISION]   = 0x18,
1172         [REG_PHY_REVISION]      = 0x1C,
1173         [REG_SPHY_CNTRL]        = 0x2C,
1174         [REG_RGMII_0_CNTRL]     = 0x34,
1175         [REG_RGMII_1_CNTRL]     = 0x40,
1176         [REG_RGMII_2_CNTRL]     = 0x4c,
1177         [REG_LED_0_CNTRL]       = 0x90,
1178         [REG_LED_1_CNTRL]       = 0x94,
1179         [REG_LED_2_CNTRL]       = 0x98,
1180 };
1181
1182 static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1183         .type           = BCM7445_DEVICE_ID,
1184         .core_reg_align = 0,
1185         .reg_offsets    = bcm_sf2_7445_reg_offsets,
1186         .num_cfp_rules  = 256,
1187 };
1188
1189 static const u16 bcm_sf2_7278_reg_offsets[] = {
1190         [REG_SWITCH_CNTRL]      = 0x00,
1191         [REG_SWITCH_STATUS]     = 0x04,
1192         [REG_DIR_DATA_WRITE]    = 0x08,
1193         [REG_DIR_DATA_READ]     = 0x0c,
1194         [REG_SWITCH_REVISION]   = 0x10,
1195         [REG_PHY_REVISION]      = 0x14,
1196         [REG_SPHY_CNTRL]        = 0x24,
1197         [REG_RGMII_0_CNTRL]     = 0xe0,
1198         [REG_RGMII_1_CNTRL]     = 0xec,
1199         [REG_RGMII_2_CNTRL]     = 0xf8,
1200         [REG_LED_0_CNTRL]       = 0x40,
1201         [REG_LED_1_CNTRL]       = 0x4c,
1202         [REG_LED_2_CNTRL]       = 0x58,
1203 };
1204
1205 static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1206         .type           = BCM7278_DEVICE_ID,
1207         .core_reg_align = 1,
1208         .reg_offsets    = bcm_sf2_7278_reg_offsets,
1209         .num_cfp_rules  = 128,
1210 };
1211
1212 static const struct of_device_id bcm_sf2_of_match[] = {
1213         { .compatible = "brcm,bcm4908-switch",
1214           .data = &bcm_sf2_4908_data
1215         },
1216         { .compatible = "brcm,bcm7445-switch-v4.0",
1217           .data = &bcm_sf2_7445_data
1218         },
1219         { .compatible = "brcm,bcm7278-switch-v4.0",
1220           .data = &bcm_sf2_7278_data
1221         },
1222         { .compatible = "brcm,bcm7278-switch-v4.8",
1223           .data = &bcm_sf2_7278_data
1224         },
1225         { /* sentinel */ },
1226 };
1227 MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1228
1229 static int bcm_sf2_sw_probe(struct platform_device *pdev)
1230 {
1231         const char *reg_names[BCM_SF2_REGS_NUM] = BCM_SF2_REGS_NAME;
1232         struct device_node *dn = pdev->dev.of_node;
1233         const struct of_device_id *of_id = NULL;
1234         const struct bcm_sf2_of_data *data;
1235         struct b53_platform_data *pdata;
1236         struct dsa_switch_ops *ops;
1237         struct device_node *ports;
1238         struct bcm_sf2_priv *priv;
1239         struct b53_device *dev;
1240         struct dsa_switch *ds;
1241         void __iomem **base;
1242         unsigned int i;
1243         u32 reg, rev;
1244         int ret;
1245
1246         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1247         if (!priv)
1248                 return -ENOMEM;
1249
1250         ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1251         if (!ops)
1252                 return -ENOMEM;
1253
1254         dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1255         if (!dev)
1256                 return -ENOMEM;
1257
1258         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1259         if (!pdata)
1260                 return -ENOMEM;
1261
1262         of_id = of_match_node(bcm_sf2_of_match, dn);
1263         if (!of_id || !of_id->data)
1264                 return -EINVAL;
1265
1266         data = of_id->data;
1267
1268         /* Set SWITCH_REG register offsets and SWITCH_CORE align factor */
1269         priv->type = data->type;
1270         priv->reg_offsets = data->reg_offsets;
1271         priv->core_reg_align = data->core_reg_align;
1272         priv->num_cfp_rules = data->num_cfp_rules;
1273
1274         priv->rcdev = devm_reset_control_get_optional_exclusive(&pdev->dev,
1275                                                                 "switch");
1276         if (PTR_ERR(priv->rcdev) == -EPROBE_DEFER)
1277                 return PTR_ERR(priv->rcdev);
1278
1279         /* Auto-detection using standard registers will not work, so
1280          * provide an indication of what kind of device we are for
1281          * b53_common to work with
1282          */
1283         pdata->chip_id = priv->type;
1284         dev->pdata = pdata;
1285
1286         priv->dev = dev;
1287         ds = dev->ds;
1288         ds->ops = &bcm_sf2_ops;
1289
1290         /* Advertise the 8 egress queues */
1291         ds->num_tx_queues = SF2_NUM_EGRESS_QUEUES;
1292
1293         dev_set_drvdata(&pdev->dev, priv);
1294
1295         spin_lock_init(&priv->indir_lock);
1296         mutex_init(&priv->cfp.lock);
1297         INIT_LIST_HEAD(&priv->cfp.rules_list);
1298
1299         /* CFP rule #0 cannot be used for specific classifications, flag it as
1300          * permanently used
1301          */
1302         set_bit(0, priv->cfp.used);
1303         set_bit(0, priv->cfp.unique);
1304
1305         /* Balance of_node_put() done by of_find_node_by_name() */
1306         of_node_get(dn);
1307         ports = of_find_node_by_name(dn, "ports");
1308         if (ports) {
1309                 bcm_sf2_identify_ports(priv, ports);
1310                 of_node_put(ports);
1311         }
1312
1313         priv->irq0 = irq_of_parse_and_map(dn, 0);
1314         priv->irq1 = irq_of_parse_and_map(dn, 1);
1315
1316         base = &priv->core;
1317         for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
1318                 *base = devm_platform_ioremap_resource(pdev, i);
1319                 if (IS_ERR(*base)) {
1320                         pr_err("unable to find register: %s\n", reg_names[i]);
1321                         return PTR_ERR(*base);
1322                 }
1323                 base++;
1324         }
1325
1326         priv->clk = devm_clk_get_optional(&pdev->dev, "sw_switch");
1327         if (IS_ERR(priv->clk))
1328                 return PTR_ERR(priv->clk);
1329
1330         clk_prepare_enable(priv->clk);
1331
1332         priv->clk_mdiv = devm_clk_get_optional(&pdev->dev, "sw_switch_mdiv");
1333         if (IS_ERR(priv->clk_mdiv)) {
1334                 ret = PTR_ERR(priv->clk_mdiv);
1335                 goto out_clk;
1336         }
1337
1338         clk_prepare_enable(priv->clk_mdiv);
1339
1340         ret = bcm_sf2_sw_rst(priv);
1341         if (ret) {
1342                 pr_err("unable to software reset switch: %d\n", ret);
1343                 goto out_clk_mdiv;
1344         }
1345
1346         bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1347
1348         ret = bcm_sf2_mdio_register(ds);
1349         if (ret) {
1350                 pr_err("failed to register MDIO bus\n");
1351                 goto out_clk_mdiv;
1352         }
1353
1354         bcm_sf2_gphy_enable_set(priv->dev->ds, false);
1355
1356         ret = bcm_sf2_cfp_rst(priv);
1357         if (ret) {
1358                 pr_err("failed to reset CFP\n");
1359                 goto out_mdio;
1360         }
1361
1362         /* Disable all interrupts and request them */
1363         bcm_sf2_intr_disable(priv);
1364
1365         ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1366                                "switch_0", ds);
1367         if (ret < 0) {
1368                 pr_err("failed to request switch_0 IRQ\n");
1369                 goto out_mdio;
1370         }
1371
1372         ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1373                                "switch_1", ds);
1374         if (ret < 0) {
1375                 pr_err("failed to request switch_1 IRQ\n");
1376                 goto out_mdio;
1377         }
1378
1379         /* Reset the MIB counters */
1380         reg = core_readl(priv, CORE_GMNCFGCFG);
1381         reg |= RST_MIB_CNT;
1382         core_writel(priv, reg, CORE_GMNCFGCFG);
1383         reg &= ~RST_MIB_CNT;
1384         core_writel(priv, reg, CORE_GMNCFGCFG);
1385
1386         /* Get the maximum number of ports for this switch */
1387         priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1388         if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1389                 priv->hw_params.num_ports = DSA_MAX_PORTS;
1390
1391         /* Assume a single GPHY setup if we can't read that property */
1392         if (of_property_read_u32(dn, "brcm,num-gphy",
1393                                  &priv->hw_params.num_gphy))
1394                 priv->hw_params.num_gphy = 1;
1395
1396         rev = reg_readl(priv, REG_SWITCH_REVISION);
1397         priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1398                                         SWITCH_TOP_REV_MASK;
1399         priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1400
1401         rev = reg_readl(priv, REG_PHY_REVISION);
1402         priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1403
1404         ret = b53_switch_register(dev);
1405         if (ret)
1406                 goto out_mdio;
1407
1408         dev_info(&pdev->dev,
1409                  "Starfighter 2 top: %x.%02x, core: %x.%02x, IRQs: %d, %d\n",
1410                  priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1411                  priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1412                  priv->irq0, priv->irq1);
1413
1414         return 0;
1415
1416 out_mdio:
1417         bcm_sf2_mdio_unregister(priv);
1418 out_clk_mdiv:
1419         clk_disable_unprepare(priv->clk_mdiv);
1420 out_clk:
1421         clk_disable_unprepare(priv->clk);
1422         return ret;
1423 }
1424
1425 static int bcm_sf2_sw_remove(struct platform_device *pdev)
1426 {
1427         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1428
1429         priv->wol_ports_mask = 0;
1430         /* Disable interrupts */
1431         bcm_sf2_intr_disable(priv);
1432         dsa_unregister_switch(priv->dev->ds);
1433         bcm_sf2_cfp_exit(priv->dev->ds);
1434         bcm_sf2_mdio_unregister(priv);
1435         clk_disable_unprepare(priv->clk_mdiv);
1436         clk_disable_unprepare(priv->clk);
1437         if (priv->type == BCM7278_DEVICE_ID && !IS_ERR(priv->rcdev))
1438                 reset_control_assert(priv->rcdev);
1439
1440         return 0;
1441 }
1442
1443 static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1444 {
1445         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1446
1447         /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1448          * successful MDIO bus scan to occur. If we did turn off the GPHY
1449          * before (e.g: port_disable), this will also power it back on.
1450          *
1451          * Do not rely on kexec_in_progress, just power the PHY on.
1452          */
1453         if (priv->hw_params.num_gphy == 1)
1454                 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1455 }
1456
1457 #ifdef CONFIG_PM_SLEEP
1458 static int bcm_sf2_suspend(struct device *dev)
1459 {
1460         struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
1461
1462         return dsa_switch_suspend(priv->dev->ds);
1463 }
1464
1465 static int bcm_sf2_resume(struct device *dev)
1466 {
1467         struct bcm_sf2_priv *priv = dev_get_drvdata(dev);
1468
1469         return dsa_switch_resume(priv->dev->ds);
1470 }
1471 #endif /* CONFIG_PM_SLEEP */
1472
1473 static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1474                          bcm_sf2_suspend, bcm_sf2_resume);
1475
1476
1477 static struct platform_driver bcm_sf2_driver = {
1478         .probe  = bcm_sf2_sw_probe,
1479         .remove = bcm_sf2_sw_remove,
1480         .shutdown = bcm_sf2_sw_shutdown,
1481         .driver = {
1482                 .name = "brcm-sf2",
1483                 .of_match_table = bcm_sf2_of_match,
1484                 .pm = &bcm_sf2_pm_ops,
1485         },
1486 };
1487 module_platform_driver(bcm_sf2_driver);
1488
1489 MODULE_AUTHOR("Broadcom Corporation");
1490 MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1491 MODULE_LICENSE("GPL");
1492 MODULE_ALIAS("platform:brcm-sf2");
This page took 0.116558 seconds and 4 git commands to generate.