]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/net/dsa/bcm_sf2.c
net: dsa: call phy_init_eee in DSA layer
[linux.git] / drivers / net / dsa / bcm_sf2.c
1 /*
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>
24 #include <linux/of_net.h>
25 #include <linux/of_mdio.h>
26 #include <net/dsa.h>
27 #include <linux/ethtool.h>
28 #include <linux/if_bridge.h>
29 #include <linux/brcmphy.h>
30 #include <linux/etherdevice.h>
31 #include <linux/platform_data/b53.h>
32
33 #include "bcm_sf2.h"
34 #include "bcm_sf2_regs.h"
35 #include "b53/b53_priv.h"
36 #include "b53/b53_regs.h"
37
38 static enum dsa_tag_protocol bcm_sf2_sw_get_tag_protocol(struct dsa_switch *ds)
39 {
40         return DSA_TAG_PROTO_BRCM;
41 }
42
43 static void bcm_sf2_imp_vlan_setup(struct dsa_switch *ds, int cpu_port)
44 {
45         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
46         unsigned int i;
47         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++) {
54                 if (!((1 << i) & ds->enabled_port_mask))
55                         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
63 static void bcm_sf2_brcm_hdr_setup(struct bcm_sf2_priv *priv, int port)
64 {
65         u32 reg, val;
66
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);
101 }
102
103 static 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);
133
134         /* Force link status for IMP port */
135         reg = core_readl(priv, offset);
136         reg |= (MII_SW_OR | LINK_STS);
137         core_writel(priv, reg, offset);
138 }
139
140 static void bcm_sf2_eee_enable_set(struct dsa_switch *ds, int port, bool enable)
141 {
142         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
143         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
153 static void bcm_sf2_gphy_enable_set(struct dsa_switch *ds, bool enable)
154 {
155         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
156         u32 reg;
157
158         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);
173
174         /* 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         }
180 }
181
182 static 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
203 static 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
226 static int bcm_sf2_port_setup(struct dsa_switch *ds, int port,
227                               struct phy_device *phy)
228 {
229         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
230         s8 cpu_port = ds->dst->cpu_dp->index;
231         unsigned int i;
232         u32 reg;
233
234         /* Clear the memory power down */
235         reg = core_readl(priv, CORE_MEM_PSM_VDD_CTRL);
236         reg &= ~P_TXQ_PSM_VDD(port);
237         core_writel(priv, reg, CORE_MEM_PSM_VDD_CTRL);
238
239         /* 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
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 < 8; i++)
248                 reg |= i << (PRT_TO_QID_SHIFT * i);
249         core_writel(priv, reg, CORE_PORT_TC2_QOS_MAP_PORT(port));
250
251         /* Clear the Rx and Tx disable bits and set to no spanning tree */
252         core_writel(priv, 0, CORE_G_PCTL_PORT(port));
253
254         /* Re-enable the GPHY and re-apply workarounds */
255         if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1) {
256                 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
272         /* Enable MoCA port interrupts to get notified */
273         if (port == priv->moca_port)
274                 bcm_sf2_port_intr_enable(priv, port);
275
276         /* 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          */
280         reg = core_readl(priv, CORE_PORT_VLAN_CTL_PORT(port));
281         reg &= ~PORT_VLAN_CTRL_MASK;
282         reg |= (1 << port);
283         reg |= priv->dev->ports[port].vlan_ctl_mask;
284         core_writel(priv, reg, CORE_PORT_VLAN_CTL_PORT(port));
285
286         bcm_sf2_imp_vlan_setup(ds, cpu_port);
287
288         /* 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
292         return 0;
293 }
294
295 static void bcm_sf2_port_disable(struct dsa_switch *ds, int port,
296                                  struct phy_device *phy)
297 {
298         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
299         u32 off, reg;
300
301         if (priv->wol_ports_mask & (1 << port))
302                 return;
303
304         if (port == priv->moca_port)
305                 bcm_sf2_port_intr_disable(priv, port);
306
307         if (priv->int_phy_mask & 1 << port && priv->hw_params.num_gphy == 1)
308                 bcm_sf2_gphy_enable_set(ds, false);
309
310         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
325 /* Returns 0 if EEE was not enabled, or 1 otherwise
326  */
327 static int bcm_sf2_eee_init(struct dsa_switch *ds, int port,
328                             struct phy_device *phy)
329 {
330         int ret;
331
332         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
341 static int bcm_sf2_sw_get_eee(struct dsa_switch *ds, int port,
342                               struct ethtool_eee *e)
343 {
344         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
345         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
355 static int bcm_sf2_sw_set_eee(struct dsa_switch *ds, int port,
356                               struct phy_device *phydev,
357                               struct ethtool_eee *e)
358 {
359         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
360         struct ethtool_eee *p = &priv->port_sts[port].eee;
361
362         p->eee_enabled = e->eee_enabled;
363         bcm_sf2_eee_enable_set(ds, port, e->eee_enabled);
364
365         return 0;
366 }
367
368 static int bcm_sf2_sw_indir_rw(struct bcm_sf2_priv *priv, int op, int addr,
369                                int regnum, u16 val)
370 {
371         int ret = 0;
372         u32 reg;
373
374         reg = reg_readl(priv, REG_SWITCH_CNTRL);
375         reg |= MDIO_MASTER_SEL;
376         reg_writel(priv, reg, REG_SWITCH_CNTRL);
377
378         /* Page << 8 | offset */
379         reg = 0x70;
380         reg <<= 2;
381         core_writel(priv, addr, reg);
382
383         /* Page << 8 | offset */
384         reg = 0x80 << 8 | regnum << 1;
385         reg <<= 2;
386
387         if (op)
388                 ret = core_readl(priv, reg);
389         else
390                 core_writel(priv, val, reg);
391
392         reg = reg_readl(priv, REG_SWITCH_CNTRL);
393         reg &= ~MDIO_MASTER_SEL;
394         reg_writel(priv, reg, REG_SWITCH_CNTRL);
395
396         return ret & 0xffff;
397 }
398
399 static int bcm_sf2_sw_mdio_read(struct mii_bus *bus, int addr, int regnum)
400 {
401         struct bcm_sf2_priv *priv = bus->priv;
402
403         /* Intercept reads from Broadcom pseudo-PHY address, else, send
404          * them to our master MDIO bus controller
405          */
406         if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
407                 return bcm_sf2_sw_indir_rw(priv, 1, addr, regnum, 0);
408         else
409                 return mdiobus_read_nested(priv->master_mii_bus, addr, regnum);
410 }
411
412 static int bcm_sf2_sw_mdio_write(struct mii_bus *bus, int addr, int regnum,
413                                  u16 val)
414 {
415         struct bcm_sf2_priv *priv = bus->priv;
416
417         /* Intercept writes to the Broadcom pseudo-PHY address, else,
418          * send them to our master MDIO bus controller
419          */
420         if (addr == BRCM_PSEUDO_PHY_ADDR && priv->indir_phy_mask & BIT(addr))
421                 bcm_sf2_sw_indir_rw(priv, 0, addr, regnum, val);
422         else
423                 mdiobus_write_nested(priv->master_mii_bus, addr, regnum, val);
424
425         return 0;
426 }
427
428 static irqreturn_t bcm_sf2_switch_0_isr(int irq, void *dev_id)
429 {
430         struct bcm_sf2_priv *priv = dev_id;
431
432         priv->irq0_stat = intrl2_0_readl(priv, INTRL2_CPU_STATUS) &
433                                 ~priv->irq0_mask;
434         intrl2_0_writel(priv, priv->irq0_stat, INTRL2_CPU_CLEAR);
435
436         return IRQ_HANDLED;
437 }
438
439 static irqreturn_t bcm_sf2_switch_1_isr(int irq, void *dev_id)
440 {
441         struct bcm_sf2_priv *priv = dev_id;
442
443         priv->irq1_stat = intrl2_1_readl(priv, INTRL2_CPU_STATUS) &
444                                 ~priv->irq1_mask;
445         intrl2_1_writel(priv, priv->irq1_stat, INTRL2_CPU_CLEAR);
446
447         if (priv->irq1_stat & P_LINK_UP_IRQ(P7_IRQ_OFF))
448                 priv->port_sts[7].link = 1;
449         if (priv->irq1_stat & P_LINK_DOWN_IRQ(P7_IRQ_OFF))
450                 priv->port_sts[7].link = 0;
451
452         return IRQ_HANDLED;
453 }
454
455 static int bcm_sf2_sw_rst(struct bcm_sf2_priv *priv)
456 {
457         unsigned int timeout = 1000;
458         u32 reg;
459
460         reg = core_readl(priv, CORE_WATCHDOG_CTRL);
461         reg |= SOFTWARE_RESET | EN_CHIP_RST | EN_SW_RESET;
462         core_writel(priv, reg, CORE_WATCHDOG_CTRL);
463
464         do {
465                 reg = core_readl(priv, CORE_WATCHDOG_CTRL);
466                 if (!(reg & SOFTWARE_RESET))
467                         break;
468
469                 usleep_range(1000, 2000);
470         } while (timeout-- > 0);
471
472         if (timeout == 0)
473                 return -ETIMEDOUT;
474
475         return 0;
476 }
477
478 static void bcm_sf2_intr_disable(struct bcm_sf2_priv *priv)
479 {
480         intrl2_0_mask_set(priv, 0xffffffff);
481         intrl2_0_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
482         intrl2_1_mask_set(priv, 0xffffffff);
483         intrl2_1_writel(priv, 0xffffffff, INTRL2_CPU_CLEAR);
484 }
485
486 static void bcm_sf2_identify_ports(struct bcm_sf2_priv *priv,
487                                    struct device_node *dn)
488 {
489         struct device_node *port;
490         int mode;
491         unsigned int port_num;
492
493         priv->moca_port = -1;
494
495         for_each_available_child_of_node(dn, port) {
496                 if (of_property_read_u32(port, "reg", &port_num))
497                         continue;
498
499                 /* Internal PHYs get assigned a specific 'phy-mode' property
500                  * value: "internal" to help flag them before MDIO probing
501                  * has completed, since they might be turned off at that
502                  * time
503                  */
504                 mode = of_get_phy_mode(port);
505                 if (mode < 0)
506                         continue;
507
508                 if (mode == PHY_INTERFACE_MODE_INTERNAL)
509                         priv->int_phy_mask |= 1 << port_num;
510
511                 if (mode == PHY_INTERFACE_MODE_MOCA)
512                         priv->moca_port = port_num;
513
514                 if (of_property_read_bool(port, "brcm,use-bcm-hdr"))
515                         priv->brcm_tag_mask |= 1 << port_num;
516         }
517 }
518
519 static int bcm_sf2_mdio_register(struct dsa_switch *ds)
520 {
521         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
522         struct device_node *dn;
523         static int index;
524         int err;
525
526         /* Find our integrated MDIO bus node */
527         dn = of_find_compatible_node(NULL, NULL, "brcm,unimac-mdio");
528         priv->master_mii_bus = of_mdio_find_bus(dn);
529         if (!priv->master_mii_bus)
530                 return -EPROBE_DEFER;
531
532         get_device(&priv->master_mii_bus->dev);
533         priv->master_mii_dn = dn;
534
535         priv->slave_mii_bus = devm_mdiobus_alloc(ds->dev);
536         if (!priv->slave_mii_bus)
537                 return -ENOMEM;
538
539         priv->slave_mii_bus->priv = priv;
540         priv->slave_mii_bus->name = "sf2 slave mii";
541         priv->slave_mii_bus->read = bcm_sf2_sw_mdio_read;
542         priv->slave_mii_bus->write = bcm_sf2_sw_mdio_write;
543         snprintf(priv->slave_mii_bus->id, MII_BUS_ID_SIZE, "sf2-%d",
544                  index++);
545         priv->slave_mii_bus->dev.of_node = dn;
546
547         /* Include the pseudo-PHY address to divert reads towards our
548          * workaround. This is only required for 7445D0, since 7445E0
549          * disconnects the internal switch pseudo-PHY such that we can use the
550          * regular SWITCH_MDIO master controller instead.
551          *
552          * Here we flag the pseudo PHY as needing special treatment and would
553          * otherwise make all other PHY read/writes go to the master MDIO bus
554          * controller that comes with this switch backed by the "mdio-unimac"
555          * driver.
556          */
557         if (of_machine_is_compatible("brcm,bcm7445d0"))
558                 priv->indir_phy_mask |= (1 << BRCM_PSEUDO_PHY_ADDR);
559         else
560                 priv->indir_phy_mask = 0;
561
562         ds->phys_mii_mask = priv->indir_phy_mask;
563         ds->slave_mii_bus = priv->slave_mii_bus;
564         priv->slave_mii_bus->parent = ds->dev->parent;
565         priv->slave_mii_bus->phy_mask = ~priv->indir_phy_mask;
566
567         if (dn)
568                 err = of_mdiobus_register(priv->slave_mii_bus, dn);
569         else
570                 err = mdiobus_register(priv->slave_mii_bus);
571
572         if (err)
573                 of_node_put(dn);
574
575         return err;
576 }
577
578 static void bcm_sf2_mdio_unregister(struct bcm_sf2_priv *priv)
579 {
580         mdiobus_unregister(priv->slave_mii_bus);
581         if (priv->master_mii_dn)
582                 of_node_put(priv->master_mii_dn);
583 }
584
585 static u32 bcm_sf2_sw_get_phy_flags(struct dsa_switch *ds, int port)
586 {
587         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
588
589         /* The BCM7xxx PHY driver expects to find the integrated PHY revision
590          * in bits 15:8 and the patch level in bits 7:0 which is exactly what
591          * the REG_PHY_REVISION register layout is.
592          */
593
594         return priv->hw_params.gphy_rev;
595 }
596
597 static void bcm_sf2_sw_adjust_link(struct dsa_switch *ds, int port,
598                                    struct phy_device *phydev)
599 {
600         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
601         struct ethtool_eee *p = &priv->port_sts[port].eee;
602         u32 id_mode_dis = 0, port_mode;
603         const char *str = NULL;
604         u32 reg, offset;
605
606         if (priv->type == BCM7445_DEVICE_ID)
607                 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
608         else
609                 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
610
611         switch (phydev->interface) {
612         case PHY_INTERFACE_MODE_RGMII:
613                 str = "RGMII (no delay)";
614                 id_mode_dis = 1;
615         case PHY_INTERFACE_MODE_RGMII_TXID:
616                 if (!str)
617                         str = "RGMII (TX delay)";
618                 port_mode = EXT_GPHY;
619                 break;
620         case PHY_INTERFACE_MODE_MII:
621                 str = "MII";
622                 port_mode = EXT_EPHY;
623                 break;
624         case PHY_INTERFACE_MODE_REVMII:
625                 str = "Reverse MII";
626                 port_mode = EXT_REVMII;
627                 break;
628         default:
629                 /* All other PHYs: internal and MoCA */
630                 goto force_link;
631         }
632
633         /* If the link is down, just disable the interface to conserve power */
634         if (!phydev->link) {
635                 reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
636                 reg &= ~RGMII_MODE_EN;
637                 reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
638                 goto force_link;
639         }
640
641         /* Clear id_mode_dis bit, and the existing port mode, but
642          * make sure we enable the RGMII block for data to pass
643          */
644         reg = reg_readl(priv, REG_RGMII_CNTRL_P(port));
645         reg &= ~ID_MODE_DIS;
646         reg &= ~(PORT_MODE_MASK << PORT_MODE_SHIFT);
647         reg &= ~(RX_PAUSE_EN | TX_PAUSE_EN);
648
649         reg |= port_mode | RGMII_MODE_EN;
650         if (id_mode_dis)
651                 reg |= ID_MODE_DIS;
652
653         if (phydev->pause) {
654                 if (phydev->asym_pause)
655                         reg |= TX_PAUSE_EN;
656                 reg |= RX_PAUSE_EN;
657         }
658
659         reg_writel(priv, reg, REG_RGMII_CNTRL_P(port));
660
661         pr_info("Port %d configured for %s\n", port, str);
662
663 force_link:
664         /* Force link settings detected from the PHY */
665         reg = SW_OVERRIDE;
666         switch (phydev->speed) {
667         case SPEED_1000:
668                 reg |= SPDSTS_1000 << SPEED_SHIFT;
669                 break;
670         case SPEED_100:
671                 reg |= SPDSTS_100 << SPEED_SHIFT;
672                 break;
673         }
674
675         if (phydev->link)
676                 reg |= LINK_STS;
677         if (phydev->duplex == DUPLEX_FULL)
678                 reg |= DUPLX_MODE;
679
680         core_writel(priv, reg, offset);
681
682         if (!phydev->is_pseudo_fixed_link)
683                 p->eee_enabled = bcm_sf2_eee_init(ds, port, phydev);
684 }
685
686 static void bcm_sf2_sw_fixed_link_update(struct dsa_switch *ds, int port,
687                                          struct fixed_phy_status *status)
688 {
689         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
690         u32 duplex, pause, offset;
691         u32 reg;
692
693         if (priv->type == BCM7445_DEVICE_ID)
694                 offset = CORE_STS_OVERRIDE_GMIIP_PORT(port);
695         else
696                 offset = CORE_STS_OVERRIDE_GMIIP2_PORT(port);
697
698         duplex = core_readl(priv, CORE_DUPSTS);
699         pause = core_readl(priv, CORE_PAUSESTS);
700
701         status->link = 0;
702
703         /* MoCA port is special as we do not get link status from CORE_LNKSTS,
704          * which means that we need to force the link at the port override
705          * level to get the data to flow. We do use what the interrupt handler
706          * did determine before.
707          *
708          * For the other ports, we just force the link status, since this is
709          * a fixed PHY device.
710          */
711         if (port == priv->moca_port) {
712                 status->link = priv->port_sts[port].link;
713                 /* For MoCA interfaces, also force a link down notification
714                  * since some version of the user-space daemon (mocad) use
715                  * cmd->autoneg to force the link, which messes up the PHY
716                  * state machine and make it go in PHY_FORCING state instead.
717                  */
718                 if (!status->link)
719                         netif_carrier_off(ds->ports[port].netdev);
720                 status->duplex = 1;
721         } else {
722                 status->link = 1;
723                 status->duplex = !!(duplex & (1 << port));
724         }
725
726         reg = core_readl(priv, offset);
727         reg |= SW_OVERRIDE;
728         if (status->link)
729                 reg |= LINK_STS;
730         else
731                 reg &= ~LINK_STS;
732         core_writel(priv, reg, offset);
733
734         if ((pause & (1 << port)) &&
735             (pause & (1 << (port + PAUSESTS_TX_PAUSE_SHIFT)))) {
736                 status->asym_pause = 1;
737                 status->pause = 1;
738         }
739
740         if (pause & (1 << port))
741                 status->pause = 1;
742 }
743
744 static int bcm_sf2_sw_suspend(struct dsa_switch *ds)
745 {
746         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
747         unsigned int port;
748
749         bcm_sf2_intr_disable(priv);
750
751         /* Disable all ports physically present including the IMP
752          * port, the other ones have already been disabled during
753          * bcm_sf2_sw_setup
754          */
755         for (port = 0; port < DSA_MAX_PORTS; port++) {
756                 if ((1 << port) & ds->enabled_port_mask ||
757                     dsa_is_cpu_port(ds, port))
758                         bcm_sf2_port_disable(ds, port, NULL);
759         }
760
761         return 0;
762 }
763
764 static int bcm_sf2_sw_resume(struct dsa_switch *ds)
765 {
766         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
767         unsigned int port;
768         int ret;
769
770         ret = bcm_sf2_sw_rst(priv);
771         if (ret) {
772                 pr_err("%s: failed to software reset switch\n", __func__);
773                 return ret;
774         }
775
776         if (priv->hw_params.num_gphy == 1)
777                 bcm_sf2_gphy_enable_set(ds, true);
778
779         for (port = 0; port < DSA_MAX_PORTS; port++) {
780                 if ((1 << port) & ds->enabled_port_mask)
781                         bcm_sf2_port_setup(ds, port, NULL);
782                 else if (dsa_is_cpu_port(ds, port))
783                         bcm_sf2_imp_setup(ds, port);
784         }
785
786         return 0;
787 }
788
789 static void bcm_sf2_sw_get_wol(struct dsa_switch *ds, int port,
790                                struct ethtool_wolinfo *wol)
791 {
792         struct net_device *p = ds->dst[ds->index].cpu_dp->netdev;
793         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
794         struct ethtool_wolinfo pwol;
795
796         /* Get the parent device WoL settings */
797         p->ethtool_ops->get_wol(p, &pwol);
798
799         /* Advertise the parent device supported settings */
800         wol->supported = pwol.supported;
801         memset(&wol->sopass, 0, sizeof(wol->sopass));
802
803         if (pwol.wolopts & WAKE_MAGICSECURE)
804                 memcpy(&wol->sopass, pwol.sopass, sizeof(wol->sopass));
805
806         if (priv->wol_ports_mask & (1 << port))
807                 wol->wolopts = pwol.wolopts;
808         else
809                 wol->wolopts = 0;
810 }
811
812 static int bcm_sf2_sw_set_wol(struct dsa_switch *ds, int port,
813                               struct ethtool_wolinfo *wol)
814 {
815         struct net_device *p = ds->dst[ds->index].cpu_dp->netdev;
816         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
817         s8 cpu_port = ds->dst->cpu_dp->index;
818         struct ethtool_wolinfo pwol;
819
820         p->ethtool_ops->get_wol(p, &pwol);
821         if (wol->wolopts & ~pwol.supported)
822                 return -EINVAL;
823
824         if (wol->wolopts)
825                 priv->wol_ports_mask |= (1 << port);
826         else
827                 priv->wol_ports_mask &= ~(1 << port);
828
829         /* If we have at least one port enabled, make sure the CPU port
830          * is also enabled. If the CPU port is the last one enabled, we disable
831          * it since this configuration does not make sense.
832          */
833         if (priv->wol_ports_mask && priv->wol_ports_mask != (1 << cpu_port))
834                 priv->wol_ports_mask |= (1 << cpu_port);
835         else
836                 priv->wol_ports_mask &= ~(1 << cpu_port);
837
838         return p->ethtool_ops->set_wol(p, wol);
839 }
840
841 static int bcm_sf2_vlan_op_wait(struct bcm_sf2_priv *priv)
842 {
843         unsigned int timeout = 10;
844         u32 reg;
845
846         do {
847                 reg = core_readl(priv, CORE_ARLA_VTBL_RWCTRL);
848                 if (!(reg & ARLA_VTBL_STDN))
849                         return 0;
850
851                 usleep_range(1000, 2000);
852         } while (timeout--);
853
854         return -ETIMEDOUT;
855 }
856
857 static int bcm_sf2_vlan_op(struct bcm_sf2_priv *priv, u8 op)
858 {
859         core_writel(priv, ARLA_VTBL_STDN | op, CORE_ARLA_VTBL_RWCTRL);
860
861         return bcm_sf2_vlan_op_wait(priv);
862 }
863
864 static void bcm_sf2_sw_configure_vlan(struct dsa_switch *ds)
865 {
866         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
867         unsigned int port;
868
869         /* Clear all VLANs */
870         bcm_sf2_vlan_op(priv, ARLA_VTBL_CMD_CLEAR);
871
872         for (port = 0; port < priv->hw_params.num_ports; port++) {
873                 if (!((1 << port) & ds->enabled_port_mask))
874                         continue;
875
876                 core_writel(priv, 1, CORE_DEFAULT_1Q_TAG_P(port));
877         }
878 }
879
880 static int bcm_sf2_sw_setup(struct dsa_switch *ds)
881 {
882         struct bcm_sf2_priv *priv = bcm_sf2_to_priv(ds);
883         unsigned int port;
884
885         /* Enable all valid ports and disable those unused */
886         for (port = 0; port < priv->hw_params.num_ports; port++) {
887                 /* IMP port receives special treatment */
888                 if ((1 << port) & ds->enabled_port_mask)
889                         bcm_sf2_port_setup(ds, port, NULL);
890                 else if (dsa_is_cpu_port(ds, port))
891                         bcm_sf2_imp_setup(ds, port);
892                 else
893                         bcm_sf2_port_disable(ds, port, NULL);
894         }
895
896         bcm_sf2_sw_configure_vlan(ds);
897
898         return 0;
899 }
900
901 /* The SWITCH_CORE register space is managed by b53 but operates on a page +
902  * register basis so we need to translate that into an address that the
903  * bus-glue understands.
904  */
905 #define SF2_PAGE_REG_MKADDR(page, reg)  ((page) << 10 | (reg) << 2)
906
907 static int bcm_sf2_core_read8(struct b53_device *dev, u8 page, u8 reg,
908                               u8 *val)
909 {
910         struct bcm_sf2_priv *priv = dev->priv;
911
912         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
913
914         return 0;
915 }
916
917 static int bcm_sf2_core_read16(struct b53_device *dev, u8 page, u8 reg,
918                                u16 *val)
919 {
920         struct bcm_sf2_priv *priv = dev->priv;
921
922         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
923
924         return 0;
925 }
926
927 static int bcm_sf2_core_read32(struct b53_device *dev, u8 page, u8 reg,
928                                u32 *val)
929 {
930         struct bcm_sf2_priv *priv = dev->priv;
931
932         *val = core_readl(priv, SF2_PAGE_REG_MKADDR(page, reg));
933
934         return 0;
935 }
936
937 static int bcm_sf2_core_read64(struct b53_device *dev, u8 page, u8 reg,
938                                u64 *val)
939 {
940         struct bcm_sf2_priv *priv = dev->priv;
941
942         *val = core_readq(priv, SF2_PAGE_REG_MKADDR(page, reg));
943
944         return 0;
945 }
946
947 static int bcm_sf2_core_write8(struct b53_device *dev, u8 page, u8 reg,
948                                u8 value)
949 {
950         struct bcm_sf2_priv *priv = dev->priv;
951
952         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
953
954         return 0;
955 }
956
957 static int bcm_sf2_core_write16(struct b53_device *dev, u8 page, u8 reg,
958                                 u16 value)
959 {
960         struct bcm_sf2_priv *priv = dev->priv;
961
962         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
963
964         return 0;
965 }
966
967 static int bcm_sf2_core_write32(struct b53_device *dev, u8 page, u8 reg,
968                                 u32 value)
969 {
970         struct bcm_sf2_priv *priv = dev->priv;
971
972         core_writel(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
973
974         return 0;
975 }
976
977 static int bcm_sf2_core_write64(struct b53_device *dev, u8 page, u8 reg,
978                                 u64 value)
979 {
980         struct bcm_sf2_priv *priv = dev->priv;
981
982         core_writeq(priv, value, SF2_PAGE_REG_MKADDR(page, reg));
983
984         return 0;
985 }
986
987 static struct b53_io_ops bcm_sf2_io_ops = {
988         .read8  = bcm_sf2_core_read8,
989         .read16 = bcm_sf2_core_read16,
990         .read32 = bcm_sf2_core_read32,
991         .read48 = bcm_sf2_core_read64,
992         .read64 = bcm_sf2_core_read64,
993         .write8 = bcm_sf2_core_write8,
994         .write16 = bcm_sf2_core_write16,
995         .write32 = bcm_sf2_core_write32,
996         .write48 = bcm_sf2_core_write64,
997         .write64 = bcm_sf2_core_write64,
998 };
999
1000 static const struct dsa_switch_ops bcm_sf2_ops = {
1001         .get_tag_protocol       = bcm_sf2_sw_get_tag_protocol,
1002         .setup                  = bcm_sf2_sw_setup,
1003         .get_strings            = b53_get_strings,
1004         .get_ethtool_stats      = b53_get_ethtool_stats,
1005         .get_sset_count         = b53_get_sset_count,
1006         .get_phy_flags          = bcm_sf2_sw_get_phy_flags,
1007         .adjust_link            = bcm_sf2_sw_adjust_link,
1008         .fixed_link_update      = bcm_sf2_sw_fixed_link_update,
1009         .suspend                = bcm_sf2_sw_suspend,
1010         .resume                 = bcm_sf2_sw_resume,
1011         .get_wol                = bcm_sf2_sw_get_wol,
1012         .set_wol                = bcm_sf2_sw_set_wol,
1013         .port_enable            = bcm_sf2_port_setup,
1014         .port_disable           = bcm_sf2_port_disable,
1015         .get_eee                = bcm_sf2_sw_get_eee,
1016         .set_eee                = bcm_sf2_sw_set_eee,
1017         .port_bridge_join       = b53_br_join,
1018         .port_bridge_leave      = b53_br_leave,
1019         .port_stp_state_set     = b53_br_set_stp_state,
1020         .port_fast_age          = b53_br_fast_age,
1021         .port_vlan_filtering    = b53_vlan_filtering,
1022         .port_vlan_prepare      = b53_vlan_prepare,
1023         .port_vlan_add          = b53_vlan_add,
1024         .port_vlan_del          = b53_vlan_del,
1025         .port_vlan_dump         = b53_vlan_dump,
1026         .port_fdb_prepare       = b53_fdb_prepare,
1027         .port_fdb_dump          = b53_fdb_dump,
1028         .port_fdb_add           = b53_fdb_add,
1029         .port_fdb_del           = b53_fdb_del,
1030         .get_rxnfc              = bcm_sf2_get_rxnfc,
1031         .set_rxnfc              = bcm_sf2_set_rxnfc,
1032         .port_mirror_add        = b53_mirror_add,
1033         .port_mirror_del        = b53_mirror_del,
1034 };
1035
1036 struct bcm_sf2_of_data {
1037         u32 type;
1038         const u16 *reg_offsets;
1039         unsigned int core_reg_align;
1040 };
1041
1042 /* Register offsets for the SWITCH_REG_* block */
1043 static const u16 bcm_sf2_7445_reg_offsets[] = {
1044         [REG_SWITCH_CNTRL]      = 0x00,
1045         [REG_SWITCH_STATUS]     = 0x04,
1046         [REG_DIR_DATA_WRITE]    = 0x08,
1047         [REG_DIR_DATA_READ]     = 0x0C,
1048         [REG_SWITCH_REVISION]   = 0x18,
1049         [REG_PHY_REVISION]      = 0x1C,
1050         [REG_SPHY_CNTRL]        = 0x2C,
1051         [REG_RGMII_0_CNTRL]     = 0x34,
1052         [REG_RGMII_1_CNTRL]     = 0x40,
1053         [REG_RGMII_2_CNTRL]     = 0x4c,
1054         [REG_LED_0_CNTRL]       = 0x90,
1055         [REG_LED_1_CNTRL]       = 0x94,
1056         [REG_LED_2_CNTRL]       = 0x98,
1057 };
1058
1059 static const struct bcm_sf2_of_data bcm_sf2_7445_data = {
1060         .type           = BCM7445_DEVICE_ID,
1061         .core_reg_align = 0,
1062         .reg_offsets    = bcm_sf2_7445_reg_offsets,
1063 };
1064
1065 static const u16 bcm_sf2_7278_reg_offsets[] = {
1066         [REG_SWITCH_CNTRL]      = 0x00,
1067         [REG_SWITCH_STATUS]     = 0x04,
1068         [REG_DIR_DATA_WRITE]    = 0x08,
1069         [REG_DIR_DATA_READ]     = 0x0c,
1070         [REG_SWITCH_REVISION]   = 0x10,
1071         [REG_PHY_REVISION]      = 0x14,
1072         [REG_SPHY_CNTRL]        = 0x24,
1073         [REG_RGMII_0_CNTRL]     = 0xe0,
1074         [REG_RGMII_1_CNTRL]     = 0xec,
1075         [REG_RGMII_2_CNTRL]     = 0xf8,
1076         [REG_LED_0_CNTRL]       = 0x40,
1077         [REG_LED_1_CNTRL]       = 0x4c,
1078         [REG_LED_2_CNTRL]       = 0x58,
1079 };
1080
1081 static const struct bcm_sf2_of_data bcm_sf2_7278_data = {
1082         .type           = BCM7278_DEVICE_ID,
1083         .core_reg_align = 1,
1084         .reg_offsets    = bcm_sf2_7278_reg_offsets,
1085 };
1086
1087 static const struct of_device_id bcm_sf2_of_match[] = {
1088         { .compatible = "brcm,bcm7445-switch-v4.0",
1089           .data = &bcm_sf2_7445_data
1090         },
1091         { .compatible = "brcm,bcm7278-switch-v4.0",
1092           .data = &bcm_sf2_7278_data
1093         },
1094         { /* sentinel */ },
1095 };
1096 MODULE_DEVICE_TABLE(of, bcm_sf2_of_match);
1097
1098 static 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;
1102         const struct of_device_id *of_id = NULL;
1103         const struct bcm_sf2_of_data *data;
1104         struct b53_platform_data *pdata;
1105         struct dsa_switch_ops *ops;
1106         struct bcm_sf2_priv *priv;
1107         struct b53_device *dev;
1108         struct dsa_switch *ds;
1109         void __iomem **base;
1110         struct resource *r;
1111         unsigned int i;
1112         u32 reg, rev;
1113         int ret;
1114
1115         priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
1116         if (!priv)
1117                 return -ENOMEM;
1118
1119         ops = devm_kzalloc(&pdev->dev, sizeof(*ops), GFP_KERNEL);
1120         if (!ops)
1121                 return -ENOMEM;
1122
1123         dev = b53_switch_alloc(&pdev->dev, &bcm_sf2_io_ops, priv);
1124         if (!dev)
1125                 return -ENOMEM;
1126
1127         pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1128         if (!pdata)
1129                 return -ENOMEM;
1130
1131         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;
1141
1142         /* Auto-detection using standard registers will not work, so
1143          * provide an indication of what kind of device we are for
1144          * b53_common to work with
1145          */
1146         pdata->chip_id = priv->type;
1147         dev->pdata = pdata;
1148
1149         priv->dev = dev;
1150         ds = dev->ds;
1151         ds->ops = &bcm_sf2_ops;
1152
1153         dev_set_drvdata(&pdev->dev, priv);
1154
1155         spin_lock_init(&priv->indir_lock);
1156         mutex_init(&priv->stats_mutex);
1157         mutex_init(&priv->cfp.lock);
1158
1159         /* CFP rule #0 cannot be used for specific classifications, flag it as
1160          * permanently used
1161          */
1162         set_bit(0, priv->cfp.used);
1163
1164         bcm_sf2_identify_ports(priv, dn->child);
1165
1166         priv->irq0 = irq_of_parse_and_map(dn, 0);
1167         priv->irq1 = irq_of_parse_and_map(dn, 1);
1168
1169         base = &priv->core;
1170         for (i = 0; i < BCM_SF2_REGS_NUM; i++) {
1171                 r = platform_get_resource(pdev, IORESOURCE_MEM, i);
1172                 *base = devm_ioremap_resource(&pdev->dev, r);
1173                 if (IS_ERR(*base)) {
1174                         pr_err("unable to find register: %s\n", reg_names[i]);
1175                         return PTR_ERR(*base);
1176                 }
1177                 base++;
1178         }
1179
1180         ret = bcm_sf2_sw_rst(priv);
1181         if (ret) {
1182                 pr_err("unable to software reset switch: %d\n", ret);
1183                 return ret;
1184         }
1185
1186         ret = bcm_sf2_mdio_register(ds);
1187         if (ret) {
1188                 pr_err("failed to register MDIO bus\n");
1189                 return ret;
1190         }
1191
1192         ret = bcm_sf2_cfp_rst(priv);
1193         if (ret) {
1194                 pr_err("failed to reset CFP\n");
1195                 goto out_mdio;
1196         }
1197
1198         /* Disable all interrupts and request them */
1199         bcm_sf2_intr_disable(priv);
1200
1201         ret = devm_request_irq(&pdev->dev, priv->irq0, bcm_sf2_switch_0_isr, 0,
1202                                "switch_0", priv);
1203         if (ret < 0) {
1204                 pr_err("failed to request switch_0 IRQ\n");
1205                 goto out_mdio;
1206         }
1207
1208         ret = devm_request_irq(&pdev->dev, priv->irq1, bcm_sf2_switch_1_isr, 0,
1209                                "switch_1", priv);
1210         if (ret < 0) {
1211                 pr_err("failed to request switch_1 IRQ\n");
1212                 goto out_mdio;
1213         }
1214
1215         /* Reset the MIB counters */
1216         reg = core_readl(priv, CORE_GMNCFGCFG);
1217         reg |= RST_MIB_CNT;
1218         core_writel(priv, reg, CORE_GMNCFGCFG);
1219         reg &= ~RST_MIB_CNT;
1220         core_writel(priv, reg, CORE_GMNCFGCFG);
1221
1222         /* Get the maximum number of ports for this switch */
1223         priv->hw_params.num_ports = core_readl(priv, CORE_IMP0_PRT_ID) + 1;
1224         if (priv->hw_params.num_ports > DSA_MAX_PORTS)
1225                 priv->hw_params.num_ports = DSA_MAX_PORTS;
1226
1227         /* Assume a single GPHY setup if we can't read that property */
1228         if (of_property_read_u32(dn, "brcm,num-gphy",
1229                                  &priv->hw_params.num_gphy))
1230                 priv->hw_params.num_gphy = 1;
1231
1232         rev = reg_readl(priv, REG_SWITCH_REVISION);
1233         priv->hw_params.top_rev = (rev >> SWITCH_TOP_REV_SHIFT) &
1234                                         SWITCH_TOP_REV_MASK;
1235         priv->hw_params.core_rev = (rev & SF2_REV_MASK);
1236
1237         rev = reg_readl(priv, REG_PHY_REVISION);
1238         priv->hw_params.gphy_rev = rev & PHY_REVISION_MASK;
1239
1240         ret = b53_switch_register(dev);
1241         if (ret)
1242                 goto out_mdio;
1243
1244         pr_info("Starfighter 2 top: %x.%02x, core: %x.%02x base: 0x%p, IRQs: %d, %d\n",
1245                 priv->hw_params.top_rev >> 8, priv->hw_params.top_rev & 0xff,
1246                 priv->hw_params.core_rev >> 8, priv->hw_params.core_rev & 0xff,
1247                 priv->core, priv->irq0, priv->irq1);
1248
1249         return 0;
1250
1251 out_mdio:
1252         bcm_sf2_mdio_unregister(priv);
1253         return ret;
1254 }
1255
1256 static int bcm_sf2_sw_remove(struct platform_device *pdev)
1257 {
1258         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1259
1260         /* Disable all ports and interrupts */
1261         priv->wol_ports_mask = 0;
1262         bcm_sf2_sw_suspend(priv->dev->ds);
1263         dsa_unregister_switch(priv->dev->ds);
1264         bcm_sf2_mdio_unregister(priv);
1265
1266         return 0;
1267 }
1268
1269 static void bcm_sf2_sw_shutdown(struct platform_device *pdev)
1270 {
1271         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1272
1273         /* For a kernel about to be kexec'd we want to keep the GPHY on for a
1274          * successful MDIO bus scan to occur. If we did turn off the GPHY
1275          * before (e.g: port_disable), this will also power it back on.
1276          *
1277          * Do not rely on kexec_in_progress, just power the PHY on.
1278          */
1279         if (priv->hw_params.num_gphy == 1)
1280                 bcm_sf2_gphy_enable_set(priv->dev->ds, true);
1281 }
1282
1283 #ifdef CONFIG_PM_SLEEP
1284 static int bcm_sf2_suspend(struct device *dev)
1285 {
1286         struct platform_device *pdev = to_platform_device(dev);
1287         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1288
1289         return dsa_switch_suspend(priv->dev->ds);
1290 }
1291
1292 static int bcm_sf2_resume(struct device *dev)
1293 {
1294         struct platform_device *pdev = to_platform_device(dev);
1295         struct bcm_sf2_priv *priv = platform_get_drvdata(pdev);
1296
1297         return dsa_switch_resume(priv->dev->ds);
1298 }
1299 #endif /* CONFIG_PM_SLEEP */
1300
1301 static SIMPLE_DEV_PM_OPS(bcm_sf2_pm_ops,
1302                          bcm_sf2_suspend, bcm_sf2_resume);
1303
1304
1305 static struct platform_driver bcm_sf2_driver = {
1306         .probe  = bcm_sf2_sw_probe,
1307         .remove = bcm_sf2_sw_remove,
1308         .shutdown = bcm_sf2_sw_shutdown,
1309         .driver = {
1310                 .name = "brcm-sf2",
1311                 .of_match_table = bcm_sf2_of_match,
1312                 .pm = &bcm_sf2_pm_ops,
1313         },
1314 };
1315 module_platform_driver(bcm_sf2_driver);
1316
1317 MODULE_AUTHOR("Broadcom Corporation");
1318 MODULE_DESCRIPTION("Driver for Broadcom Starfighter 2 ethernet switch chip");
1319 MODULE_LICENSE("GPL");
1320 MODULE_ALIAS("platform:brcm-sf2");